also looking at this
feat: enroll protected runtime trust anchors
#43
33 files
+1297
-18
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -2,6 +2,7 @@ import assert from 'node:assert/strict' | ||
| 2 | 2 | import { mkdir, readFile } from 'node:fs/promises' |
| 3 | 3 | import { basename, join } from 'node:path' |
| 4 | 4 | import { startWorkspaceBrowser } from './workspace-browser' |
| 5 | import { settleExecutionDiagnostics } from './execution-diagnostics' | |
| 5 | 6 | |
| 6 | 7 | // This driver owns only browser transport. Native credentials stay in a private fixture file. |
| 7 | 8 | const root = process.cwd() |
| @@ -23,8 +24,6 @@ let diagnosticPage: Awaited<ReturnType<typeof browser.page>> | undefined | ||
| 23 | 24 | let startBaseline: number | null = null |
| 24 | 25 | async function failureDiagnostics() { |
| 25 | 26 | const abort = new AbortController() |
| 26 | let timer: ReturnType<typeof setTimeout> | undefined | |
| 27 | const timeout = new Promise<null>(resolve => { timer = setTimeout(() => resolve(null), 1000) }) | |
| 28 | 27 | try { |
| 29 | 28 | const snapshot = async () => { |
| 30 | 29 | const response = await fetch(`${data.url}/__fixture/diagnostics`, { headers: { 'x-fixture-control': data.control }, signal: abort.signal }) |
| @@ -46,19 +45,18 @@ async function failureDiagnostics() { | ||
| 46 | 45 | hasSourceExists: !!document.querySelector('[data-execution-source-exists]'), hasExecutionError: !!document.querySelector('[data-execution-error]') |
| 47 | 46 | }; |
| 48 | 47 | })()`) : Promise.resolve(null) |
| 49 | const collect = Promise.allSettled([snapshot(), dom]).then(([server, page]) => ({ | |
| 48 | const parts = await settleExecutionDiagnostics(snapshot(), dom) | |
| 49 | const base = { | |
| 50 | 50 | label: 'execution-browser-failure-diagnostics', phase, startBaseline, |
| 51 | server: server.status === 'fulfilled' ? server.value : { available: false }, | |
| 52 | dom: page.status === 'fulfilled' ? page.value : null, browserErrorCount: diagnosticPage?.errors.length ?? null, | |
| 51 | browserErrorCount: diagnosticPage?.errors.length ?? null, | |
| 52 | executionStartRequests: diagnosticPage ? diagnosticPage.requests.filter(request => request.method === 'POST' && request.url.endsWith('/workspace/execution')).length : null, | |
| 53 | 53 | admissionRequests: diagnosticPage ? diagnosticPage.requests.filter(request => request.method === 'POST' && request.url.endsWith('/workspace/execution/admission')).length : null, |
| 54 | })) | |
| 55 | const captured = await Promise.race([collect, timeout]) | |
| 56 | const text = `${JSON.stringify(captured ?? { label: 'execution-browser-failure-diagnostics', available: false })}\n` | |
| 57 | console.error(Buffer.byteLength(text, 'utf8') <= 8192 ? text.trimEnd() : '{"label":"execution-browser-failure-diagnostics","available":false}') | |
| 54 | } | |
| 55 | const text = JSON.stringify({ ...base, ...parts }) | |
| 56 | console.error(Buffer.byteLength(text, 'utf8') <= 8192 ? text : JSON.stringify({ ...base, server: { available: false }, dom: null, byteBudgetExceeded: true })) | |
| 58 | 57 | } |
| 59 | catch { console.error('{"label":"execution-browser-failure-diagnostics","available":false}') } | |
| 58 | catch { console.error(JSON.stringify({ label: 'execution-browser-failure-diagnostics', phase, available: false })) } | |
| 60 | 59 | finally { |
| 61 | clearTimeout(timer) | |
| 62 | 60 | abort.abort() |
| 63 | 61 | // The owned socket close rejects pending CDP commands and clears their timers in the existing helper. |
| 64 | 62 | diagnosticPage?.close() |
| @@ -0,0 +1,24 @@ | ||
| 1 | type DiagnosticStatus = 'pending' | 'fulfilled' | 'rejected' | |
| 2 | export interface ExecutionDiagnosticParts { | |
| 3 | server: unknown | |
| 4 | dom: unknown | |
| 5 | serverStatus: DiagnosticStatus | |
| 6 | domStatus: DiagnosticStatus | |
| 7 | } | |
| 8 | ||
| 9 | /** Preserve each completed observation within the existing one-second diagnostic budget. */ | |
| 10 | export async function settleExecutionDiagnostics(server: Promise<unknown>, dom: Promise<unknown>): Promise<ExecutionDiagnosticParts> { | |
| 11 | const parts: ExecutionDiagnosticParts = { server: { available: false }, dom: null, serverStatus: 'pending', domStatus: 'pending' } | |
| 12 | let timer: ReturnType<typeof setTimeout> | undefined | |
| 13 | const timeout = new Promise<void>(resolve => { timer = setTimeout(resolve, 1000) }) | |
| 14 | const tasks = [ | |
| 15 | server.then(value => { parts.server = value; parts.serverStatus = 'fulfilled' }, () => { parts.serverStatus = 'rejected' }), | |
| 16 | dom.then(value => { parts.dom = value; parts.domStatus = 'fulfilled' }, () => { parts.domStatus = 'rejected' }), | |
| 17 | ] | |
| 18 | try { | |
| 19 | await Promise.race([Promise.all(tasks), timeout]) | |
| 20 | // Late responses may update the private accumulator, never this returned observation. | |
| 21 | return { ...parts } | |
| 22 | } | |
| 23 | finally { clearTimeout(timer) } | |
| 24 | } | |
| @@ -0,0 +1,40 @@ | ||
| 1 | import { expect, test } from 'bun:test' | |
| 2 | import { settleExecutionDiagnostics } from '../helpers/execution-diagnostics' | |
| 3 | ||
| 4 | test('a pending DOM diagnostic cannot erase an already received server observation', async () => { | |
| 5 | const result = await settleExecutionDiagnostics(Promise.resolve({ available: true, state: 'running' }), new Promise(() => {})) | |
| 6 | expect(result?.server).toEqual({ available: true, state: 'running' }) | |
| 7 | expect(result?.dom).toBeNull() | |
| 8 | }) | |
| 9 | ||
| 10 | ||
| 11 | test('a pending server diagnostic cannot erase an already received DOM observation', async () => { | |
| 12 | const result = await settleExecutionDiagnostics(new Promise(() => {}), Promise.resolve({ executionPhase: 'completed' })) | |
| 13 | expect(result.dom).toEqual({ executionPhase: 'completed' }) | |
| 14 | expect(result.serverStatus).toBe('pending') | |
| 15 | expect(result.domStatus).toBe('fulfilled') | |
| 16 | }) | |
| 17 | ||
| 18 | test('a rejected arm stays unavailable without discarding the other settled observation', async () => { | |
| 19 | const result = await settleExecutionDiagnostics(Promise.reject(new Error('not logged')), Promise.resolve({ executionPhase: 'running' })) | |
| 20 | expect(result.server).toEqual({ available: false }) | |
| 21 | expect(result.dom).toEqual({ executionPhase: 'running' }) | |
| 22 | expect(result.serverStatus).toBe('rejected') | |
| 23 | expect(result.domStatus).toBe('fulfilled') | |
| 24 | }) | |
| 25 | ||
| 26 | test('two stalled arms return explicit unavailability without inventing an observation', async () => { | |
| 27 | const result = await settleExecutionDiagnostics(new Promise(() => {}), new Promise(() => {})) | |
| 28 | expect(result).toEqual({ server: { available: false }, dom: null, serverStatus: 'pending', domStatus: 'pending' }) | |
| 29 | }) | |
| 30 | ||
| 31 | test('a response after the deadline cannot mutate the returned failure observation', async () => { | |
| 32 | let resolveDom!: (value: unknown) => void | |
| 33 | const dom = new Promise(resolve => { resolveDom = resolve }) | |
| 34 | const result = await settleExecutionDiagnostics(Promise.resolve({ available: true }), dom) | |
| 35 | resolveDom({ executionPhase: 'completed' }) | |
| 36 | await dom | |
| 37 | expect(result.dom).toBeNull() | |
| 38 | expect(result.domStatus).toBe('pending') | |
| 39 | expect(result.serverStatus).toBe('fulfilled') | |
| 40 | }) | |