ReviewOS

also looking at this

stacks/drone

feat: add bounded VTOL wind braking

#88
Merged chris wants to merge codex/owned-vtol-wind into main
13 files +447 -25
tests/helpers/execution-worker-observation.tsadded+63-0
Changes to tests/helpers/execution-worker-observation.ts
@@ -0,0 +1,63 @@
1type CallbackKind = 'event' | 'result' | 'finished' | 'other'
2interface CallbackCost { calls: number, totalMs: number, maxMs: number }
3interface CallbackObservation { ordinal: number, inCallback: boolean, capped: boolean, costs: Record<CallbackKind, CallbackCost> }
4type CallbackMarker = { evidence: 'native-worker-callback', ordinal: number, kind: 'result' | 'finished', phase: 'entered', sinceObservationMs: number | null }
5 | { evidence: 'native-worker-callback', ordinal: number, kind: 'result' | 'finished', phase: 'settled', callbackMs: number | null, sinceObservationMs: number | null }
6const scalar = (value: number) => Number.isFinite(value) && Math.abs(value) <= Number.MAX_SAFE_INTEGER ? value : null
7const field = (value: unknown, name: string): unknown => value && typeof value === 'object' ? Object.getOwnPropertyDescriptor(value, name)?.value : undefined
8const choice = <T extends string>(value: unknown, values: readonly T[]): T | null => values.find(item => item === value) ?? null
9
10/** Private fixture observation only. Measures synchronous callback work, never async completion. */
11export function createExecutionCallbackObserver(marker: (value: CallbackMarker) => void, now: () => number = () => performance.now()) {
12 const callbackObservations: CallbackObservation[] = [], observedWorkers = new WeakSet<Worker>()
13 let callbackObservationOverflow = false, callbackObservationUnavailable = false
14 const snapshot = () => ({ overflow: callbackObservationOverflow, unavailable: callbackObservationUnavailable, observations: structuredClone(callbackObservations) })
15 function observe(worker: Worker, acceptedOrdinal: number) {
16 try {
17 if (observedWorkers.has(worker)) return
18 if (callbackObservations.length >= 2) { callbackObservationOverflow = true; return }
19 const callback = worker.onmessage
20 if (typeof callback !== 'function') { callbackObservationUnavailable = true; return }
21 const cost = (): CallbackCost => ({ calls: 0, totalMs: 0, maxMs: 0 })
22 const observation: CallbackObservation = { ordinal: acceptedOrdinal, inCallback: false, capped: false, costs: { event: cost(), result: cost(), finished: cost(), other: cost() } }
23 const installedAt = now()
24 let markerCount = 0
25 const emitMarker = (value: CallbackMarker) => {
26 if (markerCount >= 4) { observation.capped = true; return }
27 markerCount++
28 marker(value)
29 }
30 worker.onmessage = function (event) {
31 let began: number | null = null, kind: CallbackKind = 'other'
32 try {
33 kind = choice(field(event.data, 'kind'), ['event', 'result', 'finished']) ?? 'other'
34 if (kind === 'result' || kind === 'finished') emitMarker({ evidence: 'native-worker-callback', ordinal: acceptedOrdinal, kind, phase: 'entered', sinceObservationMs: scalar(now() - installedAt) })
35 }
36 catch { callbackObservationUnavailable = true }
37 try { observation.inCallback = true; began = now() }
38 catch { callbackObservationUnavailable = true }
39 try { return callback.call(this, event) }
40 finally {
41 // Timing is observational: preserve the exact native callback outcome,
42 // including an exception, even if collection or its closed console marker fails.
43 try {
44 const ended = now()
45 observation.inCallback = false
46 const elapsed = began === null ? null : scalar(ended - began)
47 if (elapsed === null || elapsed < 0) callbackObservationUnavailable = true
48 else {
49 const bucket = observation.costs[kind]
50 if (bucket.calls >= 1000000 || bucket.totalMs + elapsed > Number.MAX_SAFE_INTEGER) observation.capped = true
51 else { bucket.calls++; bucket.totalMs += elapsed; bucket.maxMs = Math.max(bucket.maxMs, elapsed) }
52 }
53 if (kind === 'result' || kind === 'finished') emitMarker({ evidence: 'native-worker-callback', ordinal: acceptedOrdinal, kind, phase: 'settled', callbackMs: elapsed, sinceObservationMs: scalar(now() - installedAt) })
54 }
55 catch { observation.inCallback = false; callbackObservationUnavailable = true }
56 }
57 }
58 observedWorkers.add(worker); callbackObservations.push(observation)
59 }
60 catch { callbackObservationUnavailable = true }
61 }
62 return { observe, snapshot }
63}