/** * Worker process glue. Owns nothing but the process: it loads the config * itself (config modules may hold live engine handles that cannot cross * IPC), resolves each unit's pairs against its own import of the file, and hands all * actual execution to `TargetWorker`. */ import { collectFromRegistration, type CollectedTest } from '../../collect/collect.ts'; import { collectFile } from '../../collect/collect-file.ts'; import type { ModuleRegistration } from '../../collect/registry.ts'; import type { TestTargetPair } from '../../collect/select.ts'; import { loadConfigModule } from '../../config/load.ts'; import { assignPorts } from '@e2e-dev/core/lib/config/resolve'; import { resolveConfig } from '../../config/resolve-run.ts'; import { setSecretRegistry } from '@e2e-dev/core/lib/secrets'; import { loadAiSdk } from '@e2e-dev/core/lib/agent/ai-sdk'; import { AiTraceRecorder, registerAiTraceRecorder } from '@e2e-dev/core/lib/internal/ai-trace'; import { DebugTrace } from '@e2e-dev/core/lib/internal/debug'; import { classifyError, ConfigurationError, serializeError } from '@e2e-dev/core/lib/internal/errors'; import { StreamRedactor } from '@e2e-dev/core/lib/internal/redact'; import { ConfigLoadHold, writtenChunk } from '../process-output.ts'; import { processSecrets, registerStaticSecrets, staticSecretLedger } from '@e2e-dev/core/lib/run/secrecy'; import { SessionStore } from '@e2e-dev/core/lib/run/sessions'; import { copyServiceValues, type ServiceReply } from '../services.ts'; import type { ChildProcessInbound, ChildProcessOutbound, OutputMessage, RunUnitMessage, WirePair, WorkerBootstrap, WorkerToMain, } from './protocol.ts'; import { inHeldTestToolCall } from './held-test-tool-scope.ts'; import { TargetWorker, type ResolveUnitPairs, type TargetWorkerDeps } from './session.ts'; import { isAbandonedRejection } from '@e2e-dev/core/lib/internal/abandoned'; import { claimRunCodeStray } from '../../session-tools/code-strays.ts'; /** * Outbound messages in flight. `process.send` is asynchronous and a * `process.exit` right behind it can drop the message, so exiting waits for * the queue to flush: the runner must see the worker's last word. */ let outbox: Promise = Promise.resolve(); /** Holds what the process prints while `bootstrap` loads the config, until its secrets are known. */ const configLoad = new ConfigLoadHold(); /** How long an exit waits for the outbox: a channel whose runner is gone may never acknowledge. */ const FLUSH_GRACE_MS = 2_000; /** * Hands one message to the channel at once; resolves once the channel has * taken it and every message before it (or is gone). The channel keeps the * order. Waiting for the previous message's ack would take an event-loop * turn: a `pair-start` sent right before a body that exits the process, or an * `attempt-deadline` sent right before one that blocks the loop, would never * leave. */ function send(message: ChildProcessOutbound): Promise { const taken = new Promise((resolve) => { try { if (process.send === undefined || !process.connected) resolve(); else process.send(message, undefined, undefined, () => resolve()); } catch { // channel already closed; nothing left to deliver resolve(); } }); outbox = outbox.then(() => taken); return outbox; } /** * Output messages the channel has not taken yet before `write` answers * false: a producer that honors backpressure then waits for `drain`, so a * test streaming a large log cannot pile the whole of it into the outbox. */ const OUTPUT_HIGH_WATER = 64; function exitAfterFlush(code: 0 | 1): void { const exit = (): void => process.exit(code); setTimeout(exit, FLUSH_GRACE_MS).unref(); void outbox.then(exit); } /** * Routes everything the process writes to stdout or stderr into `output` * messages: the streams are inherited from the runner, whose terminal shows * the live window, so a test's `console.log` written straight through would * land inside it and be painted over. Text is attributed to the pair in * flight, with every secret value this process has seen redacted across * writes: a write that ends mid-line holds back the tail a later write could * complete into a value, and the returned function releases what is held, * attributed to the pair that wrote it. The stream's contract holds: the * callback fires once the channel took the message, `write` answers false * past the high-water mark, and `drain` follows when the backlog has cleared. */ function captureOutput(pairInFlight: () => OutputMessage['pair']): () => void { const flushes: (() => void)[] = []; for (const [stream, name] of [ [process.stdout, 'stdout'], [process.stderr, 'stderr'], ] as const) { type Done = (error?: Error | null) => void; const redactor = new StreamRedactor(processSecrets); let heldFor: OutputMessage['pair'] = undefined; let pending = 0; let needsDrain = false; const output = (text: string, pair: OutputMessage['pair']): Promise => text === '' ? Promise.resolve() : send({ type: 'output', pair, stream: name, text }); flushes.push(() => void output(redactor.flush(), heldFor)); const write = (written: string | Uint8Array, encoding?: BufferEncoding | Done, callback?: Done): boolean => { const done = typeof encoding === 'function' ? encoding : callback; if (inHeldTestToolCall()) { if (done !== undefined) { process.nextTick(done); } return true; } const chunk = writtenChunk(written, encoding); if (configLoad.hold(name, chunk)) { if (done !== undefined) process.nextTick(done); return true; } const pair = pairInFlight(); pending += 1; void output(redactor.push(chunk), pair).then(() => { pending -= 1; if (done !== undefined) done(); if (needsDrain && pending < OUTPUT_HIGH_WATER) { needsDrain = false; stream.emit('drain'); } }); heldFor = pair; if (pending < OUTPUT_HIGH_WATER) return true; needsDrain = true; return false; }; stream.write = write as typeof stream.write; } return () => { for (const flush of flushes) flush(); }; } /** Pending service calls waiting for a `service-reply`, keyed by request id. */ const serviceCalls = new Map void>(); let serviceCallCount = 0; function fatal(cause: unknown): void { send({ type: 'fatal', error: serializeError(classifyError(cause)) }); exitAfterFlush(1); } /** * Loads the config in this process and verifies it matches the runner's, then * assembles everything the worker core needs. */ async function bootstrap( message: WorkerBootstrap, debug: DebugTrace, aiTrace: AiTraceRecorder | undefined, ): Promise { // Registered before the config loads: a config module may construct an // executor that imports the AI SDK itself, and the integration list is // process-wide, so calls from either module instance land in one trace. if (aiTrace !== undefined) await registerAiTraceRecorder(aiTrace, loadAiSdk); // What the config's top-level code prints is held until its secrets are // registered, so it is redacted like a test's output. const { config, target } = await configLoad.during( async () => { const raw = await loadConfigModule(message.configPath); const resolved = assignPorts( resolveConfig(raw, { projectRoot: message.projectRoot, configPath: message.configPath, env: process.env, cli: message.cli }), message.ports, ); if (resolved.configDigest !== message.configDigest) { throw new ConfigurationError( 'CONFIG_NOT_DETERMINISTIC', 'worker resolved a different config digest than the runner; config must be deterministic', ); } const named = resolved.targets.find((candidate) => candidate.name === message.targetName); if (named === undefined) { throw new ConfigurationError('UNKNOWN_TARGET', `unknown target "${message.targetName}"`); } setSecretRegistry(resolved); registerStaticSecrets(resolved.allSecrets); return { config: resolved, target: named }; }, (stream, chunk) => process[stream].write(chunk), (line) => process.stderr.write(`e2e: [warning] ${line}\n`), ); let collectCounter = 0; const importUnit = (unit: RunUnitMessage): Promise => { collectCounter += 1; return collectFile(unit.absolutePath, `worker-collect-${collectCounter}`, staticSecretLedger(config.allSecrets).redact); }; // A registration serves every unit of its file this worker runs, so its tests are collected one time. const testsById = new WeakMap>(); const resolvePairs: ResolveUnitPairs = async (unit, reusable) => { const registration = reusable ?? (await importUnit(unit)); let byId = testsById.get(registration); if (byId === undefined) { const collected = collectFromRegistration(config.projectRoot, unit.absolutePath, registration); testsById.set(registration, (byId = new Map(collected.tests.map((test) => [test.id, test])))); } const pairs: TestTargetPair[] = []; const missing: WirePair[] = []; for (const wire of unit.pairs) { const test = byId.get(wire.test.id); if (test === undefined) { missing.push(wire); continue; } pairs.push({ test, target, agent: wire.agent, repeat: wire.repeat, options: wire.options, disposition: 'run', skip: undefined }); } return { pairs, missing, registration }; }; return { config, target, sessionStore: SessionStore.forWorker( message.runId, message.sessionsRoot, Buffer.from(message.sessionKeyBase64, 'base64'), ), runId: message.runId, artifactsRoot: message.artifactsRoot, rerunDir: message.rerunDir, headed: message.headed, slots: message.slots, env: process.env, isolated: true, resolvePairs, debug, holdSteps: message.holdSteps, services: copyServiceValues(message.services, (call, args) => { serviceCallCount += 1; const requestId = String(serviceCallCount); const reply = new Promise((resolve) => serviceCalls.set(requestId, resolve)); void send({ type: 'service-call', requestId, call, args }); return reply; }), }; } function main(): void { if (process.send === undefined) { process.stderr.write('e2e worker requires an IPC channel\n'); process.exit(1); } // Interrupts arrive over IPC; terminal signals target the runner process. process.on('SIGINT', () => undefined); process.on('SIGTERM', () => undefined); // Code an attached session runs on a held test is not the test: what it throws outside its call fails that call, not the attempt. // After its call ended there is nobody to fail: it is said on stderr, which is redacted like the test's output, and the test goes on. const fromRunCode = (cause: unknown): boolean => claimRunCodeStray(cause, (message) => void process.stderr.write(`e2e: [warning] ${message}\n`)); process.on('uncaughtException', (cause) => { if (!fromRunCode(cause)) fatal(cause); }); // A step or poll the test did not await rejects on a promise nobody holds // once it is cancelled; the attempt has already recorded it as STEP_NOT_AWAITED. // Any other rejection nobody caught is charged to the attempt in flight, // or recorded against the last test that finished; only before any test // has finished is it the worker's. process.on('unhandledRejection', (cause) => { if (isAbandonedRejection(cause)) return; if (fromRunCode(cause)) return; if (worker?.strayRejection(cause) !== true) fatal(cause); }); let worker: TargetWorker | undefined; const flushOutput = captureOutput(() => worker?.pairInFlight); // The channel closes when the runner is gone: killed, crashed, or exited // before this worker. A worker nobody is listening to must not keep driving // a device or a browser: it tears its engine down right away — bounded by // the cleanup budget like every disposal — and exits. process.on('disconnect', () => { if (worker === undefined) process.exit(1); else worker.handle({ type: 'terminate' }); }); process.on('message', (message: ChildProcessInbound) => { if (message.type === 'bootstrap') { // This worker owns its trace outright, so each drain point (unit-done, // shutdown-done) ships the entries accumulated since the previous one. const debug = new DebugTrace(message.bootstrap.debug); const aiTrace = message.bootstrap.aiTrace ? new AiTraceRecorder(processSecrets.redact) : undefined; const emit = (outbound: WorkerToMain): void => { // Output written without a final newline is still held; it leaves // before the message that starts the next pair (a serial member's // line must not land on the member after it), ends the test, the // unit, or the worker. if ( outbound.type === 'pair-start' || outbound.type === 'result' || outbound.type === 'unit-done' || outbound.type === 'shutdown-done' ) { flushOutput(); } if (outbound.type !== 'unit-done' && outbound.type !== 'shutdown-done') { send(outbound); return; } send({ ...outbound, ...(debug.enabled ? { debug: debug.drain() } : {}), ...(aiTrace === undefined ? {} : { aiTrace: aiTrace.drain({ all: outbound.type === 'shutdown-done' }) }), }); }; worker = new TargetWorker( { emit, fatal, finished: () => exitAfterFlush(0) }, () => bootstrap(message.bootstrap, debug, aiTrace), ); worker.start(); return; } if (message.type === 'service-reply') { serviceCalls.get(message.requestId)?.(message.reply); serviceCalls.delete(message.requestId); return; } worker?.handle(message); }); } main();