Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
feat(land): bound gate custody and space landing attempts [E27,E29]
Corey Olson2d06626996b6
diff --git a/src/gate.js b/src/gate.jsindex e37ab40c..621c58c5 100644--- a/src/gate.js+++ b/src/gate.js@@ -261,10 +261,10 @@ export function isStampedGreen(root) {** @param {string} root* @param {string} command- * @param {{ pipefail?: boolean, env?: NodeJS.ProcessEnv, stages?: boolean }} [options] - Gate-only pipefail, additional child environment values and per-stage accounting.+ * @param {{ pipefail?: boolean, env?: NodeJS.ProcessEnv, stages?: boolean, timeoutMs?: number }} [options] - Gate-only pipefail, additional child environment values and per-stage accounting.* @returns {{ isGreen: boolean, output: string, seconds: number, sandboxBlocked: string|null, stages?: Array<{ status: number|null, output: string, pipeline?: Array<{ status: number|null, output: string|null }> }> | null }}*/-export function runShell(root, command, { pipefail = false, env = {}, stages = false } = {}) {+export function runShell(root, command, { pipefail = false, env = {}, stages = false, timeoutMs } = {}) {const started = Date.now();const options = { cwd: root, env: { ...gitChildEnv(root), ...env }, shell: true, stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8', maxBuffer: 2 ** 30 };let prefix = '';@@ -280,19 +280,24 @@ export function runShell(root, command, { pipefail = false, env = {}, stages = fconst marker = `PULLBOARD_STAGE_${randomUUID().replaceAll('-', '')}`;const pipes = split?.some((stage) => stage.pipeline.length > 1) ? mkdtempSync(join(tmpdir(), 'pullboard-pipes-')) : null;// Newlines, not spaces, around the command, so a trailing comment in it cannot swallow the `)`.- const result = spawnSync(`${prefix}(\n${split ? stagedScript(split, marker, pipes) : command}\n) 2>&1`, options);+ const script = `${prefix}(\n${split ? stagedScript(split, marker, pipes) : command}\n) 2>&1`;+ const timed = timeoutMs ? spawnSync(process.execPath, [fileURLToPath(new URL('./timed-shell-worker.js', import.meta.url))], { ...options, shell: false, stdio: ['pipe', 'pipe', 'pipe'], input: JSON.stringify({ command: script, timeoutMs }) }) : null;+ if (timed && (timed.status !== 0 || timed.error)) throw new Refused('LAND_TIMEOUT_CUSTODY', 'the timed gate worker did not prove cleanup; inspect its owned processes before landing again');+ const result = timed ? { ...JSON.parse(timed.stdout || '{}'), stdout: '', stderr: timed.stderr } : spawnSync(script, options);+ if (timed) result.stdout = result.output + (result.timedOut ? `\n[LAND_GATE_TIMEOUT] landing gate exceeded ${timeoutMs}ms; its owned processes are stopped\n` : '');+ if (result.timedOut || result.overflow || result.error) result.status = 1;invalidateGitFacts();const pipeStatuses = pipes ? readPipes(split, pipes) : null;if (pipes) rmSync(pipes, { recursive: true, force: true });const raw = `${result.stdout ?? ''}${result.stderr ?? ''}`;const seconds = Math.round((Date.now() - started) / 1000);const blocked = (output) => (result.status === 0 ? null : sandboxBlockedOperation(root, output));- if (!stages) return { isGreen: result.status === 0, output: raw, seconds, sandboxBlocked: blocked(raw) };- if (!split) return { isGreen: result.status === 0, output: raw, seconds, stages: null, sandboxBlocked: blocked(raw) };+ if (!stages) return { isGreen: result.status === 0, output: raw, seconds, ...(timed ? { timedOut: result.timedOut } : {}), sandboxBlocked: blocked(raw) };+ if (!split) return { isGreen: result.status === 0, output: raw, seconds, stages: null, ...(timed ? { timedOut: result.timedOut } : {}), sandboxBlocked: blocked(raw) };const staged = readStages(raw, marker, split.length);// Each pipeline component's own status rides on its stage, so no failing producer hides behind a later one.const withPipes = staged.stages.map((stage, index) => (pipeStatuses?.[index] ? { ...stage, pipeline: pipeStatuses[index] } : stage));- return { isGreen: result.status === 0, seconds, ...staged, stages: withPipes, sandboxBlocked: blocked(staged.output) };+ return { isGreen: result.status === 0, seconds, ...(timed ? { timedOut: result.timedOut } : {}), ...staged, stages: withPipes, sandboxBlocked: blocked(staged.output) };}/** Refuse an environment-blocked proof after its ordinary raw logs and timings are persisted. */@@ -313,9 +318,9 @@ function safeLog(output) {/** Run the unchanged caller command and profile only timings already present in its output.* @param {string} root* @param {string} command- * @param {{ waitMs?: number, artifactDirectory?: string, artifactPrefix?: string, persistLog?: boolean, profileFile?: string, pipefail?: boolean, gateSlotHeld?: boolean, gateLease?: object, landing?: boolean, stages?: boolean, waivers?: Array<{ kind: string, test: string }> }} [options]+ * @param {{ waitMs?: number, artifactDirectory?: string, artifactPrefix?: string, persistLog?: boolean, profileFile?: string, pipefail?: boolean, gateSlotHeld?: boolean, gateLease?: object, landing?: boolean, stages?: boolean, timeoutMs?: number, waivers?: Array<{ kind: string, test: string }> }} [options]*/-export function runProfiledShell(root, command, { waitMs = 0, artifactDirectory, artifactPrefix = 'pullboard-gate', persistLog = false, profileFile, pipefail = false, gateSlotHeld = false, gateLease, landing = false, stages = false, waivers = [] } = {}) {+export function runProfiledShell(root, command, { waitMs = 0, artifactDirectory, artifactPrefix = 'pullboard-gate', persistLog = false, profileFile, pipefail = false, gateSlotHeld = false, gateLease, landing = false, stages = false, waivers = [], timeoutMs } = {}) {const directory = artifactDirectory ?? dirname(gitPath(root, LOG));mkdirSync(directory, { recursive: true, mode: 0o700 });const id = randomUUID();@@ -332,8 +337,8 @@ export function runProfiledShell(root, command, { waitMs = 0, artifactDirectory,// The registered worker returns the same complete stage/component accounting as a local run.gateLease?.tracker?.mark('runAt');const run = gateLease?.runner ? { ...runOnRunner(root, command, { runner: gateLease.runner, source: gateLease.source,- browser: gateLease.browser, pipefail, stages, waivers: landing ? waivers : [], metadata: { ...(gateLease.runMetadata ?? {}), landing } }) }- : runShell(root, command, { pipefail, env: inherited, stages });+ browser: gateLease.browser, pipefail, stages, waivers: landing ? waivers : [], metadata: { ...(gateLease.runMetadata ?? {}), landing, ...(timeoutMs ? { timeoutMs } : {}) } }) }+ : runShell(root, command, { pipefail, env: inherited, stages, timeoutMs });gateLease?.tracker?.mark('ranAt');const sandboxBlocked = run.isGreen ? null : run.sandboxBlocked ?? (run.executionRunner// The controller cannot realpath remote files; retain the network denial signal without treating remote paths as local paths.@@ -624,7 +629,7 @@ export async function runGate(root, config, { trustStamp = true, onWait, landingreturn await withGateSlot(root, async (lease) => {pinRunMain(lease, onFailure);const run = fullGateRun(root, config, trustStamp, { waitMs: lease.waitMs, gateLease: lease, landing, stages, waivers, kind: landing ? 'land' : 'gate' });- if (!run.isGreen && onFailure) await onFailure(run, { root, command: config.gate, lease });+ if (!run.isGreen && !run.timedOut && onFailure) await onFailure(run, { root, command: config.gate, lease });await recordRun(onFailure, run);return run;}, {@@ -672,7 +677,7 @@ function leaveRunReceipt(root, config, run, { before, kind }) {function fullGateRun(root, config, trustStamp = false, { waitMs = 0, log = gitPath(root, LOG), gateLease, landing = false, stages = false, waivers = [], kind = 'gate' } = {}) {if (trustStamp && isStampedGreen(root)) return { isGreen: true, isCached: true, output: '', seconds: 0, log: '' };const before = committedTree(root);- const run = runProfiledShell(root, config.gate, { waitMs, pipefail: true, profileFile: `${log}.profile.json`, gateLease, landing, stages, waivers });+ const run = runProfiledShell(root, config.gate, { waitMs, pipefail: true, profileFile: `${log}.profile.json`, gateLease, landing, stages, waivers, timeoutMs: landing ? config.landing?.timeoutMs : undefined });const { isGreen, output, seconds, profile, profilePath } = run;writeFileSync(log, output);// The environment refusing a run says nothing about the gate, so only a real result is recorded (U41).@@ -680,7 +685,7 @@ function fullGateRun(root, config, trustStamp = false, { waitMs = 0, log = gitPaconst receiptId = run.sandboxBlocked ? null : leaveRunReceipt(root, config, run, { before, kind });refuseSandboxBlocked(run);if (isGreen && before !== null && committedTree(root) === before) writeFileSync(gitPath(root, STAMP), `${before}\n`);- return { isGreen, isCached: false, output, seconds, log, profile, profilePath, receiptId, ...(run.executionRoot ? { executionRoot: run.executionRoot } : {}),+ return { isGreen, isCached: false, output, seconds, log, profile, profilePath, receiptId, ...(run.timedOut ? { timedOut: true } : {}), ...(run.executionRoot ? { executionRoot: run.executionRoot } : {}),// A remote run's host load and pinned main reach the flake diagnostics with it (#925)....(run.executionRunner ? { executionRunner: run.executionRunner, executionMain: run.executionMain ?? null, executionLoad: run.executionLoad ?? null } : {}), ...(stages ? { stages: run.stages } : {}) };}diff --git a/src/land.js b/src/land.jsindex 4acc5f12..7ef0af33 100644--- a/src/land.js+++ b/src/land.js@@ -292,6 +292,7 @@ async function gateGroup(state, label) {const tree = headTree(state.batch.root);const commit = headCommit(state.batch.root);const cached = state.gates.get(tree);+ if (cached?.timedOut) throw new Refused('LAND_GATE_TIMEOUT', 'this exact tree has a recorded landing timeout; inspect its log before starting a new batch');if (cached && (completeGate(cached) || !coveredFailures(state, cached))) return cached;const directory = join(dirname(state.batch.root), 'logs');mkdirSync(directory, { recursive: true });@@ -319,10 +320,14 @@ async function gateGroup(state, label) {submissions: state.batch.items.map(({ id, commit }) => ({ id, commit })),failures: [...new Set(landingFailures(gate.output, state.batch.root, { excludeCancelled: true }).map(failure => failure.file).filter(Boolean))],fileTimes: landingFileTimes(state.batch.root, gate.profile) };- await save(state, { gateResults: [...state.gates.values()].map(({ tree: testedTree, isGreen, failures, accounted, complete, requiresGreen, logPath: log, flakeDiagnostics }) =>- ({ tree: testedTree, isGreen, failures, accounted: accounted ?? null, complete: complete ?? null, requiresGreen: Boolean(requiresGreen), logPath: log,+ await save(state, { gateResults: [...state.gates.values()].map(({ tree: testedTree, isGreen, failures, accounted, complete, requiresGreen, logPath: log, flakeDiagnostics, timedOut }) =>+ ({ tree: testedTree, isGreen, ...(timedOut ? { timedOut: true } : {}), failures, accounted: accounted ?? null, complete: complete ?? null, requiresGreen: Boolean(requiresGreen), logPath: log,...(flakeDiagnostics ? { flakeDiagnostics } : {}) })),invariantEvidence: { ...evidence, gates: [...evidence.gates, tested] } });+ if (gate.timedOut) {+ await save(state, { state: 'blocked', timedOut: true });+ throw new Refused('LAND_GATE_TIMEOUT', 'the landing gate timed out; its process tree is stopped and no candidate is a culprit');+ }return result;}@@ -746,6 +751,17 @@ function emptyLanding(selected, say) {return { batch: null, landed: [], conflicts: [], blocked: selected.blocked, culprits: [], flakes: [] };}+/** Defer a new ordinary attempt without taking capacity or sleeping between landing batches. */+function cadenceDeferral(board, config, branch, say) {+ const interval = config.landing?.cadenceMs;+ if (!interval) return null;+ const latest = store.landingBatches(board).filter(batch => batch.branch === branch).map(batch => Date.parse(batch.startedAt ?? '')).filter(Number.isFinite).sort((a, b) => b - a)[0];+ if (latest === undefined || latest + interval <= Date.now()) return null;+ const deferredUntil = new Date(latest + interval).toISOString();+ say(`landing deferred until ${deferredUntil}: the trunk's landing cadence has not elapsed`);+ return { batch: null, landed: [], conflicts: [], blocked: [], culprits: [], flakes: [], deferredUntil };+}+/** Build, gate, diagnose and durably land one coordinator batch; all external actions stay in its worktree. */export async function land(options) {if (options.dryRun) return runLandingBatch(options);@@ -753,6 +769,11 @@ export async function land(options) {const trunk = mainCheckout(options.root);const branch = trunk?.branch;if (!branch) throw new Refused('LAND_TRUNK', 'landing needs the coordinator’s primary branch; return its checkout to the trunk');+ if (trunk.commit && !options.adopt) {+ requireConfirmedLanding(options.board);+ const deferred = cadenceDeferral(options.board, policyAt(options.root, trunk.commit).config, branch, options.say);+ if (deferred) return deferred;+ }if (trunk.commit && !options.adopt && idleLanding(options.board, trunk.commit)) {requireConfirmedLanding(options.board);// Only accepted trunk policy may inform this read-only preflight.@@ -822,6 +843,10 @@ export async function runLandingBatch({ root, board, config, write, say, owner,// Pin accepted policy after any recorded-landed repair, never before admission or from candidate files.config ??= policyAt(root, trunk.commit).config;board.quarantineEnabled = () => config.flakes?.quarantine === true;+ if (!dryRun && !adopt && !unfinished) {+ const deferred = cadenceDeferral(board, config, trunk.branch, say);+ if (deferred) return deferred;+ }const selected = candidates(board, root, trunk.commit, maximum);const stale = selected.blocked.find(item => item.code === 'STALE_VERDICT');if (!selected.ready.length && stale) throw staleVerdictRefusal({ item_id: stale.id }, stale);@@ -858,7 +883,7 @@ export async function runLandingBatch({ root, board, config, write, say, owner,if (!landsOnTrunk(config)) requirePushTarget(root, trunk.branch);const id = randomUUID();const directory = join(repoInfo(root).commonDir, 'pullboard', 'landings', id);- batch = { version: 1, id, base: trunk.commit, tip: trunk.commit, branch: trunk.branch, root: join(directory, 'worktree'),+ batch = { version: 1, id, startedAt: new Date().toISOString(), base: trunk.commit, tip: trunk.commit, branch: trunk.branch, root: join(directory, 'worktree'),candidates: selected.ready.map(landingEntry),items: selected.ready.map(landingEntry), conflicts: [], blocked: selected.blocked,state: 'merging', owner, logPath: join(directory, 'logs', 'batch.log'), revision: 0 };@@ -867,51 +892,56 @@ export async function runLandingBatch({ root, board, config, write, say, owner,git(root, ['worktree', 'add', '--detach', batch.root, batch.base]);}const state = { root, board, config, write, say, onWait, checkLease, checkLeaseNow, batch, culprits: batch.culprits ?? [], flaky: false, gates: new Map((batch.gateResults ?? []).map((result) => [result.tree, result])), diagnosed: new Map((batch.diagnostics ?? []).map((result) => [result.test, result.isGreen])) };- await save(state, { owner });- await returnCulprits(state);- if (state.batch.state === 'trunk') await finishTrunk(state);- else if (state.batch.state === 'pushed' || state.batch.state === 'ready') await publish(state);- else {- const initialItems = (state.batch.candidates ?? state.batch.items)- .filter((entry) => !state.culprits.some((culprit) => culprit.id === entry.id && culprit.commit === entry.commit));- if (!existsSync(state.batch.root)) throw new Refused('LAND_WORKTREE', `the recorded landing worktree is missing at ${state.batch.root}; restore it before adopting`);- const build = await mergeGroup(state, initialItems);- await save(state, { ...build, conflicts: [...state.batch.conflicts, ...build.conflicts] });- for (const entry of state.batch.blocked ?? []) say(`${entry.code} #${entry.id}${entry.stacked ? ` carries #${entry.stacked}` : ''}`);- if (!build.items.length) await save(state, { state: 'blocked' });- else if (landsOnTrunk(state.config)) await landOnTrunk(state);- else if (!await inlineDeferredGuards(state)) await save(state, { state: 'blocked' });+ try {+ await save(state, { owner });+ await returnCulprits(state);+ if (state.batch.state === 'trunk') await finishTrunk(state);+ else if (state.batch.state === 'pushed' || state.batch.state === 'ready') await publish(state);else {- let gate = await gateGroup(state, 'batch');- let proof = gate.isGreen ? null : waivedProof(state, gate);- if (!gate.isGreen && !proof) {- const flakyFiles = await diagnose(state, gate.failures, gate);- if (flakyFiles.length) { await fileFlakes(state, gate, flakyFiles); state.flaky = true; }- else {- const green = await bisect(state, build.items, gate.failures);- if (!state.flaky && green.length) {- const rebuilt = await mergeGroup(state, green);- await save(state, { ...rebuilt, culprits: state.culprits });- // The smaller final tree lost the removed items' changes, so the kept items' deferred files run again on it.- if (!await inlineDeferredGuards(state)) {- await save(state, { state: 'blocked', culprits: state.culprits });- gate = null;- } else {- gate = await gateGroup(state, 'green-items');- proof = gate.isGreen ? null : waivedProof(state, gate);- if (!gate.isGreen && !proof) {- await save(state, { state: 'blocked', failures: gate.failures, culprits: state.culprits });- throw new Refused('LAND_INTERACTION', 'the individually green items fail together; nothing retries or lands; inspect the batch’s failing tests');+ const initialItems = (state.batch.candidates ?? state.batch.items)+ .filter((entry) => !state.culprits.some((culprit) => culprit.id === entry.id && culprit.commit === entry.commit));+ if (!existsSync(state.batch.root)) throw new Refused('LAND_WORKTREE', `the recorded landing worktree is missing at ${state.batch.root}; restore it before adopting`);+ const build = await mergeGroup(state, initialItems);+ await save(state, { ...build, conflicts: [...state.batch.conflicts, ...build.conflicts] });+ for (const entry of state.batch.blocked ?? []) say(`${entry.code} #${entry.id}${entry.stacked ? ` carries #${entry.stacked}` : ''}`);+ if (!build.items.length) await save(state, { state: 'blocked' });+ else if (landsOnTrunk(state.config)) await landOnTrunk(state);+ else if (!await inlineDeferredGuards(state)) await save(state, { state: 'blocked' });+ else {+ let gate = await gateGroup(state, 'batch');+ let proof = gate.isGreen ? null : waivedProof(state, gate);+ if (!gate.isGreen && !proof) {+ const flakyFiles = await diagnose(state, gate.failures, gate);+ if (flakyFiles.length) { await fileFlakes(state, gate, flakyFiles); state.flaky = true; }+ else {+ const green = await bisect(state, build.items, gate.failures);+ if (!state.flaky && green.length) {+ const rebuilt = await mergeGroup(state, green);+ await save(state, { ...rebuilt, culprits: state.culprits });+ // The smaller final tree lost the removed items' changes, so the kept items' deferred files run again on it.+ if (!await inlineDeferredGuards(state)) {+ await save(state, { state: 'blocked', culprits: state.culprits });+ gate = null;+ } else {+ gate = await gateGroup(state, 'green-items');+ proof = gate.isGreen ? null : waivedProof(state, gate);+ if (!gate.isGreen && !proof) {+ await save(state, { state: 'blocked', failures: gate.failures, culprits: state.culprits });+ throw new Refused('LAND_INTERACTION', 'the individually green items fail together; nothing retries or lands; inspect the batch’s failing tests');+ }}- }- } else if (!state.flaky) await save(state, { state: 'blocked', culprits: state.culprits });+ } else if (!state.flaky) await save(state, { state: 'blocked', culprits: state.culprits });+ }+ }+ if (!state.flaky && gate && (gate.isGreen || proof)) {+ await save(state, { state: 'ready', proof: proof ?? { tree: gate.tree, failures: [], logPath: gate.logPath }, culprits: state.culprits });+ await publish(state);}- }- if (!state.flaky && gate && (gate.isGreen || proof)) {- await save(state, { state: 'ready', proof: proof ?? { tree: gate.tree, failures: [], logPath: gate.logPath }, culprits: state.culprits });- await publish(state);}}+ } catch (error) {+ if (!(error instanceof Refused) || error.code !== 'LAND_GATE_TIMEOUT') throw error;+ say(error.message);}return { batch: state.batch, landed: state.batch.state === 'landed' ? state.batch.items.map((entry) => entry.id) : [],conflicts: state.batch.conflicts ?? [], blocked: state.batch.blocked ?? [], culprits: state.culprits, flakes: state.batch.flakes ?? [] };diff --git a/src/runner-worker.js b/src/runner-worker.jsindex be89c2fc..11ee4481 100644--- a/src/runner-worker.js+++ b/src/runner-worker.js@@ -171,6 +171,7 @@ async function readSpec() {|| (value.browserByLaunch !== undefined && typeof value.browserByLaunch !== 'boolean')|| !value.runtime || typeof value.runtime !== 'object' || !value.runtime.files || typeof value.runtime.files !== 'object'|| typeof value.runtime.sha256 !== 'string') throw new Error('invalid work specification');+ if (value.timeoutMs !== undefined && (value.landing !== true || !Number.isSafeInteger(value.timeoutMs) || value.timeoutMs < 1 || value.timeoutMs > 2_147_483_647)) throw new Error('invalid landing timeout');if (value.stageMarker !== undefined && !/^PULLBOARD_STAGE_[a-f0-9]{32}$/u.test(value.stageMarker)) throw new Error('invalid stage marker');if (value.waivers !== undefined && (!Array.isArray(value.waivers) || value.waivers.some(entry => !entry || !['file', 'name'].includes(entry.kind) || typeof entry.test !== 'string' || !entry.test || Object.keys(entry).some(key => !['kind', 'test'].includes(key))))) throw new Error('invalid continuation projection');if (value.origin !== undefined) {@@ -711,7 +712,16 @@ async function runCommand(spec, root, env, sink, tracker) {if (!outputError) writes = writes.then(() => { denials.push(part); return sink.write(part); }).then(() => stream.resume()).catch(error => { outputError = error; onDrop(); });});forward(child.stdout); forward(child.stderr);+ let timedOut = false;+ let timeoutDrain;+ const timeout = spec.timeoutMs ? setTimeout(() => {+ timedOut = true; beginCleanup();+ timeoutDrain = tracker.stopDescendants().then(() => drainOwned(owned, true));+ timeoutDrain.catch(() => {});+ }, spec.timeoutMs) : null;const result = await closed;+ clearTimeout(timeout);+ if (timeoutDrain) await timeoutDrain;beginCleanup();cleanupStage = 'command output';await writes;@@ -728,7 +738,7 @@ async function runCommand(spec, root, env, sink, tracker) {let pipeStatuses = pipes ? readPipes(split, pipes, 8 * 1024 ** 2) : null;// Accounting beyond this bound fails closed; it never turns an unaccounted failure green.if (Buffer.byteLength(JSON.stringify(pipeStatuses)) > 8 * 1024 ** 2) pipeStatuses = null;- return { code, signal: result.signal ?? null, ...(spec.stageMarker ? { pipeStatuses } : {}), ...(denial ? { denial } : {}) };+ return { code: timedOut ? 1 : code, signal: result.signal ?? null, ...(timedOut ? { timedOut: true } : {}), ...(spec.stageMarker ? { pipeStatuses } : {}), ...(denial ? { denial } : {}) };}/** Send the single nonce-bound result line after the complete merged output has drained. */@@ -738,7 +748,7 @@ async function receipt(spec, fields, sink) {runtimeSha256: spec.runtime.sha256, toolingSha256: spec.tooling.sha256, root: fields.root ?? '', outputSha256: sink.hash.digest('hex'), wallMs: Math.max(0, Date.now() - sink.wallStart),// The execution host's load and the pinned main this run used, so a paired rerun here can be judged (#925).main: typeof spec.main === 'string' ? spec.main : null, load: { average: loadavg(), cpus: availableParallelism(), freeMB: Math.floor(freemem() / 1048576) },- ...(spec.stageMarker ? { pipeStatuses: fields.pipeStatuses ?? null } : {}), ...(fields.refused ? { refused: fields.refused } : {}), ...(fields.browserRefused ? { browserRefused: true } : {}), ...(fields.denial ? { denial: fields.denial } : {}) };+ ...(spec.stageMarker ? { pipeStatuses: fields.pipeStatuses ?? null } : {}), ...(fields.timedOut ? { timedOut: true } : {}), ...(fields.refused ? { refused: fields.refused } : {}), ...(fields.browserRefused ? { browserRefused: true } : {}), ...(fields.denial ? { denial: fields.denial } : {}) };const encoded = Buffer.from(JSON.stringify(body)).toString('base64');await new Promise((resolveWrite, rejectWrite) => process.stderr.write(`${RECEIPT} ${spec.id} ${encoded}\n`, error => error ? rejectWrite(error) : resolveWrite()));}diff --git a/src/runners.js b/src/runners.jsindex 3936fa28..82f9aa3e 100644--- a/src/runners.js+++ b/src/runners.js@@ -490,6 +490,7 @@ function requestFor(root, command, { runner, source, browser = false, byLaunch,if (secretsIn([{ path: 'runner continuation', line: 1, text: JSON.stringify(projection) }]).length) throw new Refused('RUNNER_SECRET', 'the continuation projection contains a possible secret; keep this run local');const spec = { version: 1, id: randomUUID(), repoId, ...(originBase ? { origin, originBase } : {}), runtime, tooling, tags, ...current, command, pipefail, slots: runner.slots,browser: runner.browser, browserRequired: browser, landing: metadata.landing === true,+ ...(metadata.landing === true && metadata.timeoutMs ? { timeoutMs: metadata.timeoutMs } : {}),// The classification this controller used, so the runner never reclassifies by its own settings (#1043).browserByLaunch: byLaunch ?? carriedBrowserByLaunch() ?? readMachineSettingsFile()?.gate?.browserByLaunch === true,...(stages ? { stageMarker: `PULLBOARD_STAGE_${randomUUID().replaceAll('-', '')}` } : {}), waivers: projection,@@ -527,6 +528,7 @@ function resultFrom(request, result) {|| receipt.outputSha256 !== createHash('sha256').update(output).digest('hex')) {throw new RunnerDropped(request.runner.host);}+ if (receipt.timedOut !== undefined && (receipt.timedOut !== true || !request.spec.timeoutMs || receipt.code === 0)) throw new RunnerDropped(request.runner.host);if (receipt.refused !== undefined) {if (typeof receipt.refused !== 'string' || !/^[A-Z][A-Z0-9_]{0,63}$/u.test(receipt.refused) || receipt.code === 0) throw new RunnerDropped(request.runner.host);// The runner answered, so this is a named failure, not a dropped transport to requeue.@@ -555,7 +557,7 @@ function resultFrom(request, result) {refused.runnerAttempt = { host: request.runner.host, exitCode: receipt.code, output: text };throw refused;}- return { isGreen: receipt.code === 0 && !receipt.signal, output: text, sandboxBlocked,+ return { isGreen: receipt.code === 0 && !receipt.signal, output: text, sandboxBlocked, ...(receipt.timedOut ? { timedOut: true } : {}),...(request.spec.stageMarker ? { stages: staged?.stages ?? null } : {}),seconds: Math.round(receipt.wallMs / 1000), exitCode: receipt.code,executionRunner: request.runner.host, executionRoot: receipt.root, remoteWallMs: receipt.wallMs,diff --git a/src/timed-shell-worker.js b/src/timed-shell-worker.jsnew file mode 100644index 00000000..1517166a--- /dev/null+++ b/src/timed-shell-worker.js@@ -0,0 +1,63 @@+/** Run a bounded shell under identity-checked process custody before returning its result [E27]. */+import { spawn } from 'node:child_process';+import { mkdtempSync, readFileSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { createDescendantTracker } from './runner-custody.js';++const request = JSON.parse(readFileSync(0, 'utf8'));+if (!Number.isSafeInteger(request.timeoutMs) || request.timeoutMs < 1 || request.timeoutMs > 2_147_483_647) throw new Error('invalid shell timeout');+const directory = mkdtempSync(join(tmpdir(), 'pullboard-timed-shell-'));+const tracker = createDescendantTracker(directory);+const env = { ...process.env, NODE_OPTIONS: [process.env.NODE_OPTIONS, tracker.nodeOptions].filter(Boolean).join(' '), PULLBOARD_RUNNER_PRELOAD: tracker.nodeOptions };+const child = spawn('/bin/sh', ['-c', request.command], { env, detached: true, stdio: ['ignore', 'pipe', 'pipe'] });+tracker.registerRoot(child.pid);+tracker.observe();+let timedOut = false;+let overflow = false;+let error = null;+let bytes = 0;+let stopping;+const chunks = [];+const closed = new Promise(resolveClose => {+ child.once('error', cause => { error = cause.message; resolveClose({ status: null, signal: null }); });+ child.once('close', (status, signal) => resolveClose({ status, signal }));+});++/** Drain only currently proven descendants and root groups; ambiguity keeps custody rather than freeing a lease. */+function stop() {+ stopping ??= (async () => {+ await tracker.stopDescendants();+ for (const signal of ['SIGTERM', 'SIGKILL']) {+ const deadline = Date.now() + 2_000;+ for (;;) {+ const sent = tracker.signalOwnedGroup(child.pid, signal);+ if (sent === false) break;+ if (sent === true && Date.now() >= deadline) break;+ await new Promise(resolvePause => setTimeout(resolvePause, 50));+ }+ }+ await closed;+ await tracker.verifyAfterGroups();+ })();+ return stopping;+}++/** Retain the gate's merged output within its existing one-GiB bound. */+function capture(chunk) {+ bytes += chunk.length;+ if (bytes > 2 ** 30) { overflow = true; void stop(); }+ else chunks.push(chunk);+}+child.stdout.on('data', capture);+child.stderr.on('data', capture);+child.once('exit', () => { void stop(); });+const timer = setTimeout(() => { timedOut = true; void stop(); }, request.timeoutMs);+process.on('SIGTERM', () => { timedOut = true; void stop(); });+process.on('SIGHUP', () => { timedOut = true; void stop(); });+const result = await closed;+clearTimeout(timer);+await stop();+tracker.stopMonitoring();+rmSync(directory, { recursive: true, force: true });+process.stdout.write(JSON.stringify({ ...result, timedOut, overflow, error, output: Buffer.concat(chunks).toString('utf8') }));