Skip to content

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.

test(view): report row request failure details [B26]

Corey Olson973bd235165b
diff --git a/test/row-decisions.test.js b/test/row-decisions.test.js
index d8de3192..da03eccf 100644
--- a/test/row-decisions.test.js
+++ b/test/row-decisions.test.js
@@ -5,7 +5,6 @@ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSy
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { DatabaseSync } from 'node:sqlite';
-import { once } from 'node:events';
import { after, test } from 'node:test';
import { parseSpec } from '../src/spec.js';
import { AGENT_SHELL_MARKERS } from '../src/person.js';
@@ -162,45 +161,81 @@ function sourceRow(box, file, id) {
}
/** Start the private real view with clean person credentials and expose authenticated requests. */
-async function startView(t, box) {
+async function startView(t, box, extraEnv = {}) {
const child = spawn(process.execPath, [BIN, 'view', '--no-open', '--port', '0', '--json'], {
- cwd: box.root, env: { ...personEnvironment(box.env), CODEX_SHELL: 'private-agent-view-host' }, stdio: ['ignore', 'pipe', 'pipe'],
+ cwd: box.root, env: { ...personEnvironment(box.env), ...extraEnv, CODEX_SHELL: 'private-agent-view-host' }, stdio: ['ignore', 'pipe', 'pipe'],
});
let stdout = '';
- let stderr = '';
+ let stderrTail = [];
+ let stderrPartial = '';
child.stdout.setEncoding('utf8');
- child.stderr.setEncoding('utf8').on('data', (part) => { stderr += part; });
- const closed = once(child, 'close');
+ child.stderr.setEncoding('utf8').on('data', (part) => {
+ const lines = (stderrPartial + part).split(/\r?\n/);
+ stderrPartial = lines.pop() ?? '';
+ if (stderrPartial.length > 2048) stderrPartial = `[truncated stderr line] ${stderrPartial.slice(-2048)}`;
+ stderrTail.push(...lines.map((line) => line.length > 2048 ? `[truncated stderr line] ${line.slice(-2048)}` : line));
+ if (stderrTail.length > 20) stderrTail = stderrTail.slice(-20);
+ });
+ let spawnError = null;
+ const closed = new Promise((resolveClose) => {
+ child.once('error', (error) => { spawnError = error; });
+ child.once('close', (code, signal) => resolveClose({ code, signal, error: spawnError }));
+ });
+ /** Wait for the child process to close, including when it was stopped by a signal. */
+ function waitForClose() {
+ return closed;
+ }
+ /** Stop the view once and wait until its exit status is available. */
+ async function stopView(signal = 'SIGTERM') {
+ if (child.exitCode === null && child.signalCode === null) child.kill(signal);
+ return waitForClose();
+ }
t.after(async () => {
- if (child.exitCode !== null) return;
- child.kill('SIGTERM');
+ if (child.exitCode !== null || child.signalCode !== null) return;
let timer;
- await Promise.race([closed, new Promise((done) => {
- timer = setTimeout(() => { child.kill('SIGKILL'); done(); }, 5000);
+ await Promise.race([stopView('SIGTERM'), new Promise((done) => {
+ timer = setTimeout(() => done(false), 5000);
})]);
clearTimeout(timer);
+ if (child.exitCode === null && child.signalCode === null) await stopView('SIGKILL');
});
const document = await new Promise((resolveDocument, rejectDocument) => {
- const timer = setTimeout(() => rejectDocument(new Error(`view did not start: ${stderr}`)), 10_000);
+ const timer = setTimeout(() => rejectDocument(new Error(`view did not start: ${stderrTail.join('\n')}\n${stderrPartial}`)), 10_000);
child.stdout.on('data', (chunk) => {
stdout += chunk;
try { resolveDocument(JSON.parse(stdout)); clearTimeout(timer); }
catch { /* Wait until the one JSON document is complete. */ }
});
child.once('error', rejectDocument);
- child.once('close', () => rejectDocument(new Error(`view exited early: ${stderr}`)));
+ child.once('close', () => rejectDocument(new Error(`view exited early: ${stderrTail.join('\n')}\n${stderrPartial}`)));
});
const address = new URL(document.url);
const key = address.searchParams.get('k');
assert.ok(key, 'the private view URL includes its one-use request credential');
/** Send a bounded authenticated request to the live view adapter. */
- function request(path, options = {}) {
- return fetch(new URL(path, address.origin), {
- ...options,
- headers: { 'x-pullboard-key': key, ...options.headers },
- signal: AbortSignal.timeout(10_000),
- });
+ async function request(path, options = {}) {
+ try {
+ return await fetch(new URL(path, address.origin), {
+ ...options,
+ headers: { 'x-pullboard-key': key, ...options.headers },
+ signal: AbortSignal.timeout(10_000),
+ });
+ } catch (error) {
+ let timer;
+ await Promise.race([closed, new Promise((resolveStatus) => { timer = setTimeout(resolveStatus, 500); })]);
+ clearTimeout(timer);
+ const causeCode = error?.cause?.code ?? error?.code ?? 'no-code';
+ const processStatus = child.exitCode !== null
+ ? `exit code=${child.exitCode}`
+ : child.signalCode !== null ? `signal=${child.signalCode}`
+ : spawnError ? `spawn error code=${spawnError.code ?? spawnError.name}` : 'still running';
+ const stderr = [...stderrTail, ...(stderrPartial ? [stderrPartial] : [])].slice(-20);
+ const lines = stderr.length ? stderr.join('\n') : '<no stderr output>';
+ throw new Error(`view request failed after 1 attempt (no retry): fetch cause code=${causeCode}; child ${processStatus}; stderr last 20 lines:\n${lines}`, { cause: error });
+ }
}
+ request.stopView = stopView;
+ request.waitForClose = waitForClose;
return request;
}
@@ -225,6 +260,40 @@ async function viewDecision(request, boardPath, verb, args) {
return response.json();
}
+test('[B26] failed view request names its cause', async (t) => {
+ const box = project();
+ const preload = join(box.dir, 'stderr-preload.mjs');
+ writeFileSync(preload, [
+ ...Array.from({ length: 25 }, (_, index) => `process.stderr.write('stderr-tail-${String(index + 1).padStart(2, '0')}\\n');`),
+ 'process.stderr.write("stderr-split-");',
+ 'await new Promise((resolveWait) => setTimeout(resolveWait, 25));',
+ 'process.stderr.write("line\\n");',
+ ].join('\n'));
+ const request = await startView(t, box, { NODE_OPTIONS: `--import=file://${preload}` });
+ await request.stopView('SIGKILL');
+ let failure;
+ try {
+ await request('/api/v1/boards');
+ } catch (error) {
+ failure = error;
+ }
+ assert.ok(failure, 'requesting a stopped view rejects');
+ const fetchError = failure.cause;
+ const causeCode = fetchError?.cause?.code ?? fetchError?.code;
+ assert.equal(typeof causeCode, 'string', 'the failed fetch provides a cause code');
+ assert.ok(failure.message.includes(`fetch cause code=${causeCode}`));
+ assert.match(failure.message, /child (?:exit code=\d+|signal=\w+)/);
+ assert.match(failure.message, /view request failed after 1 attempt \(no retry\)/, 'the request is not retried');
+ const stderr = failure.message.split('stderr last 20 lines:\n')[1];
+ assert.ok(stderr, 'the diagnostic includes the stderr tail');
+ assert.equal(stderr.split('\n').length, 20, 'the stderr diagnostic contains exactly the last 20 lines');
+ for (const line of Array.from({ length: 6 }, (_, index) => `stderr-tail-${String(index + 20).padStart(2, '0')}`)) {
+ assert.ok(stderr.includes(line), `${line} appears in the last 20 stderr lines`);
+ }
+ assert.ok(stderr.includes('stderr-split-line'), 'stderr text split across writes is reconstructed');
+ assert.ok(!stderr.includes('stderr-tail-01'), 'stderr older than the last 20 lines is omitted');
+});
+
test('[B26,S18,S19] person decisions wait on the board until one apply preserves every other field', async function pendingThenApply(t) {
const box = project();
const specPath = join(box.root, SPEC_FILE);