ReviewOS

also looking at this

stacks/drone

feat: add scoped cargo receiving mission tasks

#102
Merged chris wants to merge codex/cargo-fleet-authority into main
21 files +1186 -15
tests/unit/public-safety-browser-startup-process.test.tsadded+80-0
Changes to tests/unit/public-safety-browser-startup-process.test.ts
@@ -0,0 +1,80 @@
1import { expect, test } from 'bun:test'
2import { mkdtemp, readlink, readdir, rm } from 'node:fs/promises'
3import { tmpdir } from 'node:os'
4import { join } from 'node:path'
5import { createBrowserStartupObserver } from '../helpers/browser-startup-process'
6import type { StartupProcessAdapter } from '../helpers/browser-startup-process'
7import { createLinuxStartupObservation } from '../helpers/browser-startup-linux'
8
9// OS adapter seam: a held syscall promise checks ownership/cancellation, not Linux field acquisition.
10test('an expired observation remains owned through actual settlement and prevents later provider work', async () => {
11 let release!: () => void
12 const held = new Promise<void>(resolve => { release = resolve })
13 let calls = 0, active = 0, maximumActive = 0, finalized = false
14 const settlements: boolean[] = []
15 const adapter: StartupProcessAdapter = {
16 async snapshot() {
17 calls++; active++; maximumActive = Math.max(maximumActive, active)
18 try { if (calls === 1) await held; return { status: 'unavailable', code: 'CONTROLLED_OS' } }
19 finally { active-- }
20 },
21 async settleExecutables(hash) { expect(active).toBe(0); settlements.push(hash) },
22 evidence: () => ({ executables: [], identityErrors: [], executableLimitReached: false }),
23 }
24 const observer = createBrowserStartupObserver(process.pid, performance.now() - 1001, adapter)
25 let finishing: Promise<void> | undefined
26 try {
27 observer.tick()
28 await Bun.sleep(600)
29 observer.tick()
30 expect(calls).toBe(1)
31 finishing = observer.finishDiscovery().then(() => { finalized = true })
32 await Bun.sleep(10)
33 expect(finalized).toBe(false)
34 expect(settlements).toEqual([])
35 release(); await finishing
36 expect(maximumActive).toBe(1)
37 expect(calls).toBe(1)
38 expect(settlements).toEqual([false])
39 const final = observer.evidence()
40 observer.tick(); await Bun.sleep(10)
41 expect(observer.evidence()).toEqual(final)
42 expect(final.snapshots[0]!.linux.status).toBe('timeout')
43 expect(final.snapshots[0]!.completedAtMs).toBeGreaterThan(final.snapshots[0]!.elapsedMs + 500)
44 }
45 finally { release(); await finishing; await Bun.sleep(0) }
46})
47
48test.skipIf(process.platform !== 'linux')('cancellation closes actual pinned executable descriptors without hashing after the held native sample settles', async () => {
49 const root = await mkdtemp(join(tmpdir(), 'drone-observer-descriptors-'))
50 const executable = await readlink('/proc/self/exe')
51 const descriptors = async () => {
52 const ids = await readdir('/proc/self/fd')
53 const targets = await Promise.all(ids.map(id => readlink(`/proc/self/fd/${id}`).catch(() => null)))
54 return targets.filter(path => path === executable).length
55 }
56 const before = await descriptors(), began = performance.now()
57 const native = createLinuxStartupObservation(process.pid, root, began)
58 let release!: () => void, ready!: () => void
59 const held = new Promise<void>(resolve => { release = resolve }), captured = new Promise<void>(resolve => { ready = resolve })
60 const observer = createBrowserStartupObserver(process.pid, began, {
61 async snapshot(signal) {
62 const result = await native.snapshot(signal).catch(() => ({ status: 'unavailable' as const, code: 'NATIVE_UNAVAILABLE' }))
63 ready(); await held; return result
64 },
65 settleExecutables: hash => native.settleExecutables(hash), evidence: native.evidence,
66 })
67 let finishing: Promise<void> | undefined
68 try {
69 observer.tick(); await captured
70 expect(await descriptors()).toBeGreaterThan(before)
71 await Bun.sleep(600)
72 finishing = observer.finishDiscovery()
73 release(); await finishing
74 const report = observer.evidence()
75 expect(report.executables.length).toBeGreaterThan(0)
76 expect(report.executables.every(e => e.sha256 === null && e.code === 'OBSERVATION_CANCELED' && e.closeCode === null)).toBe(true)
77 expect(await descriptors()).toBe(before)
78 }
79 finally { release(); await (finishing ?? observer.finishDiscovery()); await rm(root, { recursive: true, force: true }) }
80})