also looking at this
test: retain bounded browser launch evidence on failure
#70
18 files
+620
-23
| @@ -0,0 +1,59 @@ | ||
| 1 | import { Buffer } from 'node:buffer' | |
| 2 | import { writeSync } from 'node:fs' | |
| 3 | import { mkdir, open, writeFile } from 'node:fs/promises' | |
| 4 | import { join } from 'node:path' | |
| 5 | ||
| 6 | export interface BrowserLaunchObservation { | |
| 7 | phase: 'startup-unavailable' | 'discovery-failed' | 'closed' | |
| 8 | elapsedMs: number | |
| 9 | launch: { pid: number, exitObserved: boolean, settledExitCode: number | null, signalCode: string | number | null, exitObservedBeforeCleanup: boolean, cleanupRequested: boolean, exitElapsedMs: number | null } | |
| 10 | portProbe: { attempts: number, successfulReads: number, failedReads: number, lastReadError: string | null, discovered: boolean } | |
| 11 | } | |
| 12 | export function browserFileErrorCode(error: unknown): string { | |
| 13 | const code = (error as NodeJS.ErrnoException | null)?.code | |
| 14 | return typeof code === 'string' && /^[A-Z][A-Z0-9_]{0,31}$/.test(code) ? code : 'OTHER' | |
| 15 | } | |
| 16 | function unavailable(target: 'local' | 'export', error: unknown) { | |
| 17 | const bytes = new TextEncoder().encode(`${JSON.stringify({ oracle: 'browser-launch-evidence-unavailable', target, code: browserFileErrorCode(error) })}\n`) | |
| 18 | for (const descriptor of [2, 1]) { | |
| 19 | try { if (writeSync(descriptor, bytes) === bytes.byteLength) return } | |
| 20 | catch { /* Reporting must preserve the original browser failure. */ } | |
| 21 | } | |
| 22 | } | |
| 23 | /** Public stderr is captured only before any page can receive native fixture credentials. */ | |
| 24 | export async function retainBrowserLaunchEvidence(root: string, observation: BrowserLaunchObservation, options: { startupStderr: boolean, exportDirectory?: string }) { | |
| 25 | let stderr: { status: 'retained' | 'unavailable' | 'withheld', retainedBytes: number, totalBytes: number | null, truncated: boolean, code: string | null } = { status: 'withheld', retainedBytes: 0, totalBytes: null, truncated: false, code: null } | |
| 26 | let retained: Buffer | undefined | |
| 27 | if (options.startupStderr) { | |
| 28 | try { | |
| 29 | const handle = await open(join(root, 'chrome.log'), 'r') | |
| 30 | try { | |
| 31 | const stat = await handle.stat() | |
| 32 | if (!stat.isFile()) throw new Error('Startup stderr is not a regular file') | |
| 33 | const size = Math.min(stat.size, 65536), bytes = Buffer.alloc(size) | |
| 34 | const { bytesRead } = await handle.read(bytes, 0, size, Math.max(0, stat.size - size)) | |
| 35 | retained = bytes.subarray(0, bytesRead) | |
| 36 | stderr = { status: 'retained', retainedBytes: bytesRead, totalBytes: stat.size, truncated: stat.size > bytesRead, code: null } | |
| 37 | } | |
| 38 | finally { await handle.close() } | |
| 39 | } | |
| 40 | catch (error) { stderr = { status: 'unavailable', retainedBytes: 0, totalBytes: null, truncated: false, code: browserFileErrorCode(error) } } | |
| 41 | } | |
| 42 | const outcomes = { local: false, export: false } | |
| 43 | for (const target of ['local', 'export'] as const) { | |
| 44 | const directory = target === 'local' ? root : options.exportDirectory | |
| 45 | if (!directory) continue | |
| 46 | try { | |
| 47 | if (target === 'export') await mkdir(directory, { recursive: true }) | |
| 48 | let snapshot = stderr | |
| 49 | if (retained) { | |
| 50 | try { await writeFile(join(directory, 'chrome-startup-stderr.log'), retained, { mode: 0o600 }) } | |
| 51 | catch (error) { snapshot = { ...stderr, status: 'unavailable', retainedBytes: 0, code: browserFileErrorCode(error) } } | |
| 52 | } | |
| 53 | await writeFile(join(directory, 'browser-launch.json'), `${JSON.stringify({ version: 1, evidence: 'owned-browser-launch-observation', ...observation, stderr: snapshot }, null, 2)}\n`, { mode: 0o600 }) | |
| 54 | outcomes[target] = true | |
| 55 | } | |
| 56 | catch (error) { unavailable(target, error) } | |
| 57 | } | |
| 58 | return outcomes | |
| 59 | } | |