import { spawn } from 'node:child_process'; import { mkdtemp, mkdir, rm, writeFile, readFile, realpath, stat } from 'node:fs/promises'; import { once } from 'node:events'; import { createServer } from 'node:http'; import { createHash } from 'node:crypto'; import { tmpdir } from 'node:os'; import { extname, join, resolve, sep } from 'node:path'; import { setTimeout as delay } from 'node:timers/promises'; // Real-browser acceptance for starting a pipeline run. // // Usage (from app/): // flutter build web --target=tool/run_pipeline_probe.dart -o build/run_probe_web // OPENCODE_ARTIFACT_DIR= node tool/run_pipeline_probe.mjs build/run_probe_web // // The fixture serves the real bundle over loopback and answers the same // websocket protocol as the app server: one pipeline, its detail, an empty run // list, and `run_pipeline`/`get_run`. It records the exact launch payload and // creates the run only after that request, so the real app must open the id the // server named and re-read its runs list. Both host control sets are exercised. const output = process.env.OPENCODE_ARTIFACT_DIR; if (!output) throw new Error('Set OPENCODE_ARTIFACT_DIR for the probe captures'); await mkdir(output, { recursive: true }); const chromePath = process.env.CHROME_PATH ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; const root = await realpath(process.argv[2] ?? 'build/run_probe_web'); const PIPELINE = { id: 1, name: 'Release review', version: 2, stages: 1, created_at: '2026-10-20T09:00:00Z', updated_at: '2026-10-21T09:00:00Z', scheduled: false, schedule_count: 0, enabled_schedule_count: 0, }; const DETAIL = { ...PIPELINE, latest_version: 2, version_created_at: '2026-10-21T09:00:00Z', content_hash: 'probe-hash', source: 'name: Release review\nstages:\n publish:\n type: model', versions: [{ version: 2, stages: 1, created_at: '2026-10-21T09:00:00Z' }], }; const runs = new Map(); const admittedByKey = new Map(); const launches = []; const counters = { listRuns: 0, getRun: 0, getByKey: 0 }; let nextRunId = 100; function runRow(run) { return { id: run.id, pipeline_id: run.pipeline_id, title: run.title, name: run.name, version: run.version, status: run.status, executing: run.executing, created_at: run.created_at, finished_at: run.finished_at, }; } function runDetail(run) { return { ...runRow(run), input: { prompt: run.prompt }, steps: [], }; } const MIME = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.mjs': 'text/javascript; charset=utf-8', '.json': 'application/json; charset=utf-8', '.wasm': 'application/wasm', '.css': 'text/css; charset=utf-8', '.png': 'image/png', '.svg': 'image/svg+xml', '.ico': 'image/x-icon', '.ttf': 'font/ttf', '.otf': 'font/otf', '.woff': 'font/woff', '.woff2': 'font/woff2', }; function accept(key) { return createHash('sha1') .update(`${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11`) .digest('base64'); } function frame(opcode, payload) { const length = payload.length; let header; if (length < 126) { header = Buffer.from([0x80 | opcode, length]); } else if (length < 65536) { header = Buffer.alloc(4); header[0] = 0x80 | opcode; header[1] = 126; header.writeUInt16BE(length, 2); } else { header = Buffer.alloc(10); header[0] = 0x80 | opcode; header[1] = 127; header.writeBigUInt64BE(BigInt(length), 2); } return Buffer.concat([header, payload]); } class Connection { constructor(socket) { this.socket = socket; this.buffer = Buffer.alloc(0); } push(chunk) { if (chunk?.length) this.buffer = Buffer.concat([this.buffer, chunk]); while (this.buffer.length >= 2) { const first = this.buffer[0]; const second = this.buffer[1]; const opcode = first & 0x0f; let length = second & 0x7f; let offset = 2; if (length === 126) { if (this.buffer.length < 4) return; length = this.buffer.readUInt16BE(2); offset = 4; } else if (length === 127) { if (this.buffer.length < 10) return; length = Number(this.buffer.readBigUInt64BE(2)); offset = 10; } const masked = (second & 0x80) !== 0; const maskOffset = offset; if (masked) { if (this.buffer.length < offset + 4) return; offset += 4; } if (this.buffer.length < offset + length) return; let payload = Buffer.from(this.buffer.subarray(offset, offset + length)); if (masked) { const mask = this.buffer.subarray(maskOffset, maskOffset + 4); for (let index = 0; index < payload.length; index++) { payload[index] ^= mask[index % 4]; } } this.buffer = this.buffer.subarray(offset + length); if (opcode === 0x8) return this.close(); if (opcode === 0x9) { this.socket.write(frame(0x0a, payload)); continue; } if (opcode !== 0x1) continue; let request; try { request = JSON.parse(payload.toString('utf8')); } catch { continue; } this.message(request); } } message(request) { if (request.type === 'unsubscribe') return; if (request.type === 'subscribe') { this.send({ id: request.id, type: 'subscribe' }); return; } if (request.type === 'ping') { this.send({ id: request.id, type: 'pong', build: { version: 'Run fixture' } }); return; } if (request.type === 'list_pipelines') { this.send({ id: request.id, type: 'pipelines', pipelines: [PIPELINE] }); return; } if (request.type === 'get_pipeline') { if (request.pipeline_id !== PIPELINE.id || request.version !== 2) { this.send({ id: request.id, type: 'error', error: 'pipeline not found' }); return; } this.send({ id: request.id, type: 'pipeline', detail: DETAIL }); return; } if (request.type === 'list_schedules') { this.send({ id: request.id, type: 'schedules', schedules: [] }); return; } if (request.type === 'list_runs') { counters.listRuns++; this.send({ id: request.id, type: 'runs', runs: [...runs.values()].map(runRow), usage: null, }); return; } if (request.type === 'get_run') { if (typeof request.request_key === 'string') { counters.getByKey++; const admitted = admittedByKey.get(request.request_key); this.send({ id: request.id, type: 'run', run_detail: admitted ? runDetail(admitted) : null, }); return; } counters.getRun++; const run = runs.get(request.run_id); if (!run) { this.send({ id: request.id, type: 'error', error: `no run ${request.run_id}` }); return; } this.send({ id: request.id, type: 'run', run_detail: runDetail(run) }); return; } if (request.type === 'run_pipeline') { const record = { keys: Object.keys(request).filter((key) => !['type', 'id'].includes(key)).sort(), pipeline_id: request.pipeline_id, version: request.version, request_key: request.request_key, workspace: request.workspace, input: request.input, }; launches.push(record); if (!request.request_key) { this.send({ id: request.id, type: 'error', error: 'run_pipeline requires a request key' }); return; } // The server's idempotency contract: one admitted run per request key. let run = admittedByKey.get(request.request_key); if (!run) { const id = nextRunId++; run = { id, pipeline_id: request.pipeline_id, title: `Probe run ${id}`, name: PIPELINE.name, version: request.version, status: 'running', executing: true, created_at: '2026-10-22T00:00:00Z', finished_at: '', prompt: request.input?.prompt ?? '', }; runs.set(id, run); admittedByKey.set(request.request_key, run); } this.send({ id: request.id, type: 'run_started', run_detail: runDetail(run) }); this.send({ type: 'run_event', run_id: run.id, pipeline_id: run.pipeline_id, name: run.name, title: run.title, version: run.version, status: run.status, created_at: run.created_at, finished_at: '', }); return; } this.send({ id: request.id, type: 'error', error: `Fixture does not implement ${request.type}` }); } send(value) { if (!this.socket.destroyed) { this.socket.write(frame(0x1, Buffer.from(JSON.stringify(value)))); } } close() { if (!this.socket.destroyed) this.socket.end(); } } async function staticFile(base, pathname, res) { let file = resolve(base, `.${pathname}`); if (file !== base && !file.startsWith(base + sep)) { res.writeHead(403).end(); return; } try { if ((await stat(file)).isDirectory()) file = join(file, 'index.html'); } catch { if (extname(pathname)) { res.writeHead(404).end('not found'); return; } file = join(base, 'index.html'); } try { const body = await readFile(file); res .writeHead(200, { 'content-type': MIME[extname(file)] ?? 'application/octet-stream', 'cache-control': 'no-store', }) .end(body); } catch { res.writeHead(404).end('not found'); } } const sockets = new Set(); const server = createServer(async (req, res) => { const pathname = new URL(req.url, 'http://fixture').pathname; if (pathname === '/fixture/state') { res .writeHead(200, { 'content-type': 'application/json' }) .end(JSON.stringify({ launches, runs: [...runs.values()], ...counters })); return; } if (req.method !== 'GET' && req.method !== 'HEAD') { res.writeHead(405).end(); return; } await staticFile(root, pathname, res); }); server.on('upgrade', (req, socket, head) => { const url = new URL(req.url, 'http://fixture'); if ( url.pathname !== '/connect' || req.headers.upgrade?.toLowerCase() !== 'websocket' || !req.headers['sec-websocket-key'] ) { socket.end('HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'); return; } socket.write( 'HTTP/1.1 101 Switching Protocols\r\n' + 'Upgrade: websocket\r\n' + 'Connection: Upgrade\r\n' + `Sec-WebSocket-Accept: ${accept(req.headers['sec-websocket-key'])}\r\n\r\n`, ); const connection = new Connection(socket); socket.on('data', (chunk) => connection.push(chunk)); socket.on('error', () => socket.destroy()); sockets.add(socket); socket.on('close', () => sockets.delete(socket)); connection.push(head); }); server.listen(0, '127.0.0.1'); await once(server, 'listening'); const fixtureUrl = `http://127.0.0.1:${server.address().port}`; const profile = await mkdtemp(join(tmpdir(), 'p3-run-chrome-')); const chrome = spawn( chromePath, [ '--headless=new', '--no-first-run', '--no-default-browser-check', '--disable-gpu', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank', ], { stdio: ['ignore', 'ignore', 'pipe'] }, ); let stderr = ''; chrome.stderr.on('data', (data) => { stderr += data; }); async function waitFor(check, label, attempts = 200) { for (let attempt = 0; attempt < attempts; attempt++) { const value = await check(); if (value) return value; await delay(100); } throw new Error(`Timed out waiting for ${label}\n${stderr.slice(-1200)}`); } let ws; try { const endpoint = await waitFor( () => stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1], 'Chrome DevTools', ); ws = new WebSocket(endpoint); await once(ws, 'open'); let id = 0; const pending = new Map(); ws.addEventListener('message', (event) => { const message = JSON.parse(event.data); if (message.method === 'Runtime.exceptionThrown') { console.error('PAGE EXCEPTION', JSON.stringify(message.params?.exceptionDetails)); } if (message.method === 'Runtime.consoleAPICalled') { const values = (message.params?.args ?? []).map((arg) => arg.value ?? arg.description); if (values.some((value) => String(value).includes('ERROR'))) { console.error('PAGE CONSOLE', values.join(' ')); } } if (message.id && pending.has(message.id)) { pending.get(message.id)(message); pending.delete(message.id); } }); const send = (method, params = {}, sessionId) => new Promise((resolveCall) => { const callId = ++id; pending.set(callId, resolveCall); ws.send(JSON.stringify({ id: callId, method, params, ...(sessionId ? { sessionId } : {}) })); }); const target = (await send('Target.createTarget', { url: 'about:blank' })).result.targetId; const session = (await send('Target.attachToTarget', { targetId: target, flatten: true })).result.sessionId; await send('Page.enable', {}, session); await send('Runtime.enable', {}, session); const evaluate = async (expression) => { const response = await send( 'Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }, session, ); if (response.error) throw new Error(`Runtime.evaluate failed: ${JSON.stringify(response)}`); if (response.result?.exceptionDetails) { throw new Error(`Page exception: ${JSON.stringify(response.result.exceptionDetails)}`); } return response.result?.result?.value; }; const enableSemantics = async () => { await waitFor( () => evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 'Flutter semantics placeholder', ); await evaluate("document.querySelector('flt-semantics-placeholder')?.click()"); await delay(700); }; const semanticText = () => evaluate(`(() => { const chunks = []; for (const el of document.querySelectorAll('flt-semantics')) { const label = el.getAttribute('aria-label'); if (label) chunks.push(label); const value = el.getAttribute('aria-valuetext'); if (value) chunks.push(value); if (el.value) chunks.push(String(el.value)); if (!el.children.length && el.innerText) chunks.push(el.innerText); } for (const el of document.querySelectorAll('input, textarea')) { const label = el.getAttribute('aria-label'); if (label) chunks.push(label); if (el.placeholder) chunks.push(el.placeholder); if (el.value) chunks.push(String(el.value)); } if (document.body?.innerText) chunks.push(document.body.innerText); return [...new Set(chunks)].join('\\n'); })()`); const assertBody = async (text) => { try { await waitFor( async () => (await semanticText()).includes(text), `body to contain ${JSON.stringify(text)}`, ); } catch (error) { throw new Error(`${error.message}\nBody now:\n${(await semanticText()).slice(0, 3000)}`); } }; const clickText = async (text, { last = false } = {}) => { const attemptClick = () => evaluate(`(() => { const wanted = ${JSON.stringify(text)}; const all = [...document.querySelectorAll('flt-semantics')].filter((el) => { const label = (el.getAttribute('aria-label') || el.innerText || '').trim(); return label === wanted; }); const candidates = all.filter((el) => ['button', 'tab', 'link', 'checkbox', 'menuitem'].includes(el.getAttribute('role')), ); const pool = candidates.length ? candidates : all; const target = ${last ? 'pool.at(-1)' : 'pool[0]'}; if (!target) return false; target.click(); return true; })()`); let clicked = false; for (let attempt = 0; attempt < 25 && !clicked; attempt++) { clicked = await attemptClick(); if (!clicked) await delay(120); } if (!clicked) { const labels = await evaluate(`[...document.querySelectorAll('flt-semantics')] .map((el) => (el.getAttribute('aria-label') || el.innerText || '').trim()).filter(Boolean)`); throw new Error(`Could not click ${JSON.stringify(text)}. Labels: ${JSON.stringify(labels)}`); } await delay(400); }; const clickContains = async (text) => { const clicked = await evaluate(`(() => { const wanted = ${JSON.stringify(text)}; const all = [...document.querySelectorAll('flt-semantics')].filter((el) => { const label = (el.getAttribute('aria-label') || el.innerText || '').trim(); return label.includes(wanted); }); const target = all.find((el) => ['button', 'link', 'tab', 'row'].includes(el.getAttribute('role')), ) ?? all.at(-1); if (!target) return false; target.click(); return true; })()`); if (!clicked) throw new Error(`Could not click a row containing ${JSON.stringify(text)}`); await delay(500); }; const capture = async (name) => { const data = (await send('Page.captureScreenshot', { format: 'png' }, session)).result.data; const path = join(output, `${name}.png`); await writeFile(path, Buffer.from(data, 'base64')); return path; }; const focusField = async (hint) => { const focused = await evaluate(`(() => { const nodes = [...document.querySelectorAll('input, textarea')]; const wanted = ${JSON.stringify(hint)}; const node = nodes.find((el) => (el.placeholder || '').includes(wanted) || (el.getAttribute('aria-label') || '').includes(wanted) ) ?? (wanted.includes('absolute') ? nodes.find((el) => el.tagName === 'INPUT') : nodes.find((el) => el.tagName === 'TEXTAREA')) ?? nodes.find((el) => el.tagName === 'INPUT') ?? nodes[0]; if (!node) return false; window.__probeField = node; node.focus(); node.setSelectionRange?.(0, (node.value || '').length); return true; })()`); if (!focused) throw new Error(`No editable node for ${JSON.stringify(hint)}`); await delay(250); }; const typeText = async (text) => { await send('Input.insertText', { text }, session); await delay(350); }; const clearAndType = async (hint, text) => { await focusField(hint); const selected = await evaluate(`(() => { const node = window.__probeField; if (!node) return false; node.focus(); node.setSelectionRange?.(0, (node.value || '').length); return true; })()`); if (!selected) throw new Error(`Could not select the ${JSON.stringify(hint)} field`); await typeText(text); }; const state = async () => (await (await fetch(`${fixtureUrl}/fixture/state`)).json()); const setView = async ({ width, height, deviceScaleFactor = 1 }) => { await send( 'Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor, mobile: width < 600 }, session, ); await send( 'Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: 'dark' }] }, session, ); await delay(500); }; const openPipelines = async () => { const navigation = await send( 'Page.navigate', { url: `${fixtureUrl}/pipelines` }, session, ); if (navigation.error || navigation.result?.errorText) throw new Error(JSON.stringify(navigation)); await enableSemantics(); await assertBody('Release review'); }; const openPipelineDetail = async () => { await clickContains('Release review'); await assertBody('Summary'); await assertBody('Run'); if ((await evaluate('location.pathname')) !== '/pipelines') { throw new Error(`Detail moved to ${await evaluate('location.pathname')}`); } }; const dialogOpen = async () => (await semanticText()).includes('Run Release review'); const dialogClosed = async () => !(await dialogOpen()); const assertPayload = (record, check) => { if (!record) throw new Error(`${check}: no launch recorded`); const expected = ['input', 'pipeline_id', 'request_key', 'version', 'workspace']; if (JSON.stringify(record.keys) !== JSON.stringify(expected)) { throw new Error(`${check}: unexpected request fields ${JSON.stringify(record)}`); } if (record.pipeline_id !== 1 || record.version !== 2) { throw new Error(`${check}: wrong pinned identity ${JSON.stringify(record)}`); } if (record.workspace !== '/work/release') { throw new Error(`${check}: wrong workspace ${JSON.stringify(record)}`); } if (record.input?.prompt !== 'Review the release.') { throw new Error(`${check}: wrong prompt ${JSON.stringify(record.input)}`); } if (!String(record.request_key).startsWith('app-run-1-2-')) { throw new Error(`${check}: wrong request key ${record.request_key}`); } if (String(record.request_key).length > 128) { throw new Error(`${check}: request key too long ${record.request_key}`); } return record; }; const waitRun = async (id) => { await waitFor( async () => (await evaluate('location.pathname')) === `/runs/${id}`, `the route to open run ${id}`, ); await assertBody(`Probe run ${id}`); }; const captures = []; const checks = []; await setView({ width: 420, height: 900 }); await openPipelines(); captures.push(await capture('p3-run-cupertino-list')); await openPipelineDetail(); captures.push(await capture('p3-run-cupertino-detail')); // Opening and Cancel send nothing. await clickText('Run'); await waitFor(dialogOpen, 'the launch dialog'); await assertBody('Prompt'); await assertBody('Working directory (on the server)'); await assertBody('Notifications for this run'); await assertBody('Cancel'); captures.push(await capture('p3-run-cupertino-dialog')); await clickText('Cancel'); await waitFor(dialogClosed, 'the cancelled dialog'); if ((await state()).launches.length !== 0) throw new Error('Cancel sent run_pipeline'); checks.push({ check: 'Cupertino Cancel sent nothing', sent: 0 }); // Both local validations, exactly as the web. await clickText('Run'); await waitFor(dialogOpen, 'the launch dialog again'); await clickText('Run', { last: true }); await assertBody('Enter a prompt for this run.'); captures.push(await capture('p3-run-cupertino-prompt-validation')); if ((await state()).launches.length !== 0) throw new Error('Validation sent run_pipeline'); await focusField('What should this run do?'); await typeText('Review the release.'); await clearAndType('/absolute/path/on/the/server', 'relative/path'); await clickText('Run', { last: true }); await assertBody('Choose an absolute working directory on the server.'); captures.push(await capture('p3-run-cupertino-workspace-validation')); if ((await state()).launches.length !== 0) throw new Error('Workspace validation sent run_pipeline'); await clearAndType('/absolute/path/on/the/server', '/work/release'); const beforeRunsList = (await state()).listRuns; await clickText('Run', { last: true }); await waitFor(async () => (await state()).launches.length === 1, 'the Cupertino launch request'); const cupertinoLaunch = assertPayload((await state()).launches[0], 'Cupertino launch'); await waitRun(100); const afterRunsList = (await state()).listRuns; if (afterRunsList <= beforeRunsList) { throw new Error('The accepted launch did not trigger a fresh runs list read'); } captures.push(await capture('p3-run-cupertino-run-detail')); checks.push({ check: 'Cupertino dialog → validation → accepted run detail', launch: cupertinoLaunch, listReads: afterRunsList - beforeRunsList, path: await evaluate('location.pathname'), }); // Material host: the same normal bundle after a Linux browser identity. await send( 'Emulation.setUserAgentOverride', { userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' + '(KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36', platform: 'Linux x86_64', }, session, ); await openPipelines(); await openPipelineDetail(); captures.push(await capture('p3-run-material-detail')); await clickText('Run'); await waitFor(dialogOpen, 'the material launch dialog'); await assertBody('Prompt'); captures.push(await capture('p3-run-material-dialog')); await clickText('Run', { last: true }); await assertBody('Enter a prompt for this run.'); captures.push(await capture('p3-run-material-validation')); if ((await state()).launches.length !== 1) throw new Error('Material validation sent run_pipeline'); await focusField('What should this run do?'); await typeText('Review the release.'); await clearAndType('/absolute/path/on/the/server', 'relative/path'); await clickText('Run', { last: true }); await assertBody('Choose an absolute working directory on the server.'); await clearAndType('/absolute/path/on/the/server', '/work/release'); const beforeMaterialList = (await state()).listRuns; await clickText('Run', { last: true }); await waitFor(async () => (await state()).launches.length === 2, 'the Material launch request'); const materialLaunch = assertPayload((await state()).launches[1], 'Material launch'); await waitRun(101); const afterMaterialList = (await state()).listRuns; if (afterMaterialList <= beforeMaterialList) { throw new Error('The Material acceptance did not trigger a fresh runs list read'); } captures.push(await capture('p3-run-material-run-detail')); checks.push({ check: 'Material dialog → validation → accepted run detail', launch: materialLaunch, listReads: afterMaterialList - beforeMaterialList, path: await evaluate('location.pathname'), }); const finalState = await state(); if (finalState.launches.length !== 2) { throw new Error(`Expected two launches, saw ${JSON.stringify(finalState.launches)}`); } if (finalState.runs.length !== 2) { throw new Error(`Expected two server-created runs, saw ${JSON.stringify(finalState.runs)}`); } console.log(JSON.stringify({ ok: true, fixture: fixtureUrl, checks, captures }, null, 2)); } finally { ws?.close(); for (const socket of sockets) socket.destroy(); await new Promise((resolveClose) => server.close(resolveClose)); chrome.kill('SIGTERM'); await Promise.race([once(chrome, 'exit'), delay(3000)]); await rm(profile, { recursive: true, force: true }); }