also looking at this
test: label native browser transport failures
#66
7 files
+177
-11
| @@ -0,0 +1,25 @@ | ||
| 1 | import { writeSync } from 'node:fs' | |
| 2 | ||
| 3 | export type BrowserControlAction = 'status' | 'release' | 'verify' | 'read-only' | 'revoke' | |
| 4 | type BrowserOperation = 'version-fetch' | 'version-json' | 'browser-socket' | 'target-fetch' | 'target-json' | 'page-socket' | `control-${BrowserControlAction}-${'fetch' | 'json'}` | |
| 5 | ||
| 6 | /** Observe an owned transport boundary without replacing its result, error or timeout. */ | |
| 7 | export async function observeBrowserOperation<T>(operation: BrowserOperation, work: () => Promise<T>): Promise<T> { | |
| 8 | const began = performance.now() | |
| 9 | try { return await work() } | |
| 10 | catch (error) { | |
| 11 | // Reporting is best effort. A closed diagnostic pipe must preserve the native error. | |
| 12 | try { | |
| 13 | const phase = operation.startsWith('version-') || operation === 'browser-socket' ? 'browser-start' : operation.startsWith('control-') ? 'workspace-control' : 'page-open' | |
| 14 | const nativeError = error instanceof Error && ['TimeoutError', 'AbortError', 'SyntaxError'].includes(error.name) ? error.name : 'other' | |
| 15 | const bytes = new TextEncoder().encode(`${JSON.stringify({ oracle: 'browser-operation-failure', phase, operation, nativeError, elapsedMs: performance.now() - began })}\n`) | |
| 16 | const report = (descriptor: number) => { | |
| 17 | try { return writeSync(descriptor, bytes) === bytes.byteLength } | |
| 18 | catch { return false } | |
| 19 | } | |
| 20 | if (!report(2)) report(1) | |
| 21 | } | |
| 22 | catch {} | |
| 23 | throw error | |
| 24 | } | |
| 25 | } | |