also looking at this
feat: add scoped cargo receiving mission tasks
#102
21 files
+1186
-15
| @@ -0,0 +1,61 @@ | ||
| 1 | import { browserFileErrorCode } from './browser-launch-evidence' | |
| 2 | import type { createLinuxStartupObservation } from './browser-startup-linux' | |
| 3 | ||
| 4 | type NativeSnapshot = Awaited<ReturnType<ReturnType<typeof createLinuxStartupObservation>['snapshot']>> | |
| 5 | export interface StartupProcessAdapter { | |
| 6 | snapshot: (signal: AbortSignal) => Promise<NativeSnapshot | { status: 'unavailable', code: string | null }> | |
| 7 | settleExecutables: (hash: boolean) => Promise<void> | |
| 8 | evidence: ReturnType<typeof createLinuxStartupObservation>['evidence'] | |
| 9 | } | |
| 10 | ||
| 11 | type Probe = 'present' | 'missing' | 'denied' | 'unavailable' | |
| 12 | function rootProbe(pid: number): Probe { | |
| 13 | try { process.kill(pid, 0); return 'present' } | |
| 14 | catch (error) { | |
| 15 | const code = browserFileErrorCode(error) | |
| 16 | return code === 'ESRCH' ? 'missing' : code === 'EPERM' ? 'denied' : 'unavailable' | |
| 17 | } | |
| 18 | } | |
| 19 | ||
| 20 | /** Startup-only facts. A PID probe does not establish complete process-group ownership. */ | |
| 21 | export function createBrowserStartupObserver(pid: number, began: number, linux: StartupProcessAdapter | null) { | |
| 22 | type Linux = NativeSnapshot | { status: 'unsupported' | 'timeout' | 'unavailable', code: string | null } | |
| 23 | const snapshots: Array<{ label: string, elapsedMs: number, completedAtMs: number, root: { pid: number, probe: Probe }, linux: Linux }> = [] | |
| 24 | const milestones = [0, 1000, 5000, 10000, 15000] | |
| 25 | let next = 0, sealed = false, inFlight = false, canceled = false, browserVersion: string | null = null | |
| 26 | const sample = async (label: string) => { | |
| 27 | const elapsedMs = performance.now() - began, root = { pid, probe: rootProbe(pid) } | |
| 28 | const abort = new AbortController() | |
| 29 | let timer: ReturnType<typeof setTimeout> | undefined, expired = false | |
| 30 | try { | |
| 31 | let observation: Linux = { status: 'unsupported', code: null } | |
| 32 | if (linux && canceled) observation = { status: 'unavailable', code: 'OBSERVATION_CANCELED' } | |
| 33 | else if (linux) { | |
| 34 | timer = setTimeout(() => { | |
| 35 | expired = true; canceled = true; abort.abort() | |
| 36 | console.error(JSON.stringify({ oracle: 'browser-startup-observation-pending', pid, elapsedMs: performance.now() - began })) | |
| 37 | }, 500) | |
| 38 | // Cancellation requests are not settlement. Own this actual task until it returns. | |
| 39 | observation = await linux.snapshot(abort.signal).catch(error => ({ status: 'unavailable' as const, code: browserFileErrorCode(error) })) | |
| 40 | if (expired) observation = { status: 'timeout', code: 'OBSERVATION_BUDGET' } | |
| 41 | } | |
| 42 | snapshots.push({ label, elapsedMs, completedAtMs: performance.now() - began, root, linux: observation }) | |
| 43 | } | |
| 44 | finally { if (timer) clearTimeout(timer) } | |
| 45 | } | |
| 46 | let pending: Promise<void> = Promise.resolve() | |
| 47 | return { | |
| 48 | tick() { | |
| 49 | if (!sealed && !canceled && !inFlight && next < milestones.length && performance.now() - began >= milestones[next]!) { | |
| 50 | const at = milestones[next++]! | |
| 51 | inFlight = true | |
| 52 | pending = sample(at === 0 ? 'launch' : `elapsed-${at}ms`).finally(() => { inFlight = false }) | |
| 53 | } | |
| 54 | }, | |
| 55 | async finishDiscovery() { sealed = true; await pending; await sample('discovery-ended'); await linux?.settleExecutables(!canceled) }, | |
| 56 | async cleanupEnded() { await sample('cleanup-ended') }, | |
| 57 | recordVersion(value: unknown) { if (typeof value === 'string' && /^(?:HeadlessChrome|Chrome)\/\d{1,6}(?:\.\d{1,8}){3}$/.test(value)) browserVersion = value }, | |
| 58 | evidence() { return { method: 'startup-only-process-observation-v1' as const, platform: process.platform, browserVersion, snapshots: structuredClone(snapshots), ...linux?.evidence() ?? { executables: [], identityErrors: [], executableLimitReached: false } } }, | |
| 59 | } | |
| 60 | } | |
| 61 | export type BrowserStartupEvidence = ReturnType<ReturnType<typeof createBrowserStartupObserver>['evidence']> | |