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.

fix(core): report serve startup phases [A2,C7]

Corey Olsonfcd6bafaecb0
diff --git a/src/cli.js b/src/cli.js
index 9794ca2d..cd291221 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -4,8 +4,10 @@
*/
import { createHash, randomUUID } from 'node:crypto';
import { spawn, spawnSync } from 'node:child_process';
-import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
+import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
+import { loadavg } from 'node:os';
import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path';
+import { performance } from 'node:perf_hooks';
import { pathToFileURL } from 'node:url';
import { parseArgs } from 'node:util';
import * as store from './board.js';
@@ -83,6 +85,21 @@ import { engineReceipt } from './engine.js';
import { land } from './land.js';
const CHECKOUT_LEASES = new WeakMap();
+let startupMainTraced = false;
+
+/** Append an opt-in startup phase marker for the real CLI fixture measurement [A2,C7]. */
+function traceServeStartup(phase) {
+ const path = process.env.PULLBOARD_STARTUP_TRACE_FILE;
+ const entry = process.env.PULLBOARD_STARTUP_TRACE_ENTRY;
+ if (!path || !entry || resolve(process.argv[1] ?? '') !== resolve(entry)) return;
+ try {
+ appendFileSync(path, `${JSON.stringify({ phase, at: Date.now(), elapsedMs: performance.now(),
+ processId: process.pid, cpuUsage: process.cpuUsage(), loadAverage: loadavg(),
+ eventLoopUtilization: performance.eventLoopUtilization() })}\n`);
+ } catch {
+ // Diagnostic tracing must not change normal CLI behavior.
+ }
+}
/**
* Read the collision baseline from the primary branch, retained trunk, or detached HEAD default.
@@ -1199,16 +1216,23 @@ async function viewHere(io, values) {
/** Serve API v1 until a shutdown signal, printing its address before waiting (A2). */
async function serveHere(io, values) {
+ traceServeStartup('serve-context-start');
const ctx = context(io);
+ traceServeStartup('serve-context-ready');
const mainRoot = git(ctx.info.root, ['worktree', 'list', '--porcelain']).split('\n')[0].slice('worktree '.length);
+ traceServeStartup('serve-registration-start');
await registerProjectAndLink(mainRoot, new Date(), loadConfig(mainRoot), io);
+ traceServeStartup('serve-registration-ready');
const port = values.port === undefined ? 0 : Number(values.port);
if (!Number.isInteger(port) || port < 0 || port > 65535) throw new Refused('USAGE', '--port is a number from 0 to 65535; use 0 to choose a free one');
+ traceServeStartup('serve-listen-start');
const api = await serveApi({ port, runCommand: main, isMain: ctx.allowSchemaUpgrade });
+ traceServeStartup('serve-listen-ready');
io.result?.({ url: api.url, port: api.port });
io.say(`Pullboard API v1: ${api.url}`);
io.say('Only this machine can reach it, with its session secret. Ctrl-C stops it.');
io.flush?.(0);
+ traceServeStartup('serve-ready-written');
await new Promise((stop) => {
const stopped = () => { process.off('SIGINT', stopped); process.off('SIGTERM', stopped); stop(); };
process.once('SIGINT', stopped);
@@ -2968,6 +2992,10 @@ function rememberTrunk(io) {
/** Run one command and emit its single versioned JSON result when requested (A1). */
export async function main(argv, streams) {
+ if (argv[0] === 'serve' && !startupMainTraced) {
+ startupMainTraced = true;
+ traceServeStartup('cli-main-enter');
+ }
return withGitFacts(() => runMain(argv, streams));
}
@@ -3019,6 +3047,7 @@ async function runMain(argv, streams) {
}
};
if (needsRepo) rememberTrunk(io);
+ traceServeStartup('cli-command-preamble-start');
if (sync) {
try { await autoLinkProject(io.cwd, io); } catch (error) { if (!(error instanceof Refused)) throw error; }
await retry();
@@ -3028,6 +3057,7 @@ async function runMain(argv, streams) {
if (!['NOT_A_REPO', 'NO_REPO', 'NO_CONFIG', 'CORE_BARE'].includes(error.code)) io.err(`pullboard: ${displayedRefusal(io, error)}\nnext: ${refusalDocument(error).error.next}`);
}
}
+ traceServeStartup('cli-command-preamble-ready');
const code = await runCommand(argv, io);
if (code === 0) await checkoutSession?.finish?.();
if (sync && code === 0) await retry();
@@ -3050,3 +3080,9 @@ export function resultCommands() {
'help', 'version', 'tour', 'lifecycle', 'view', 'serve', 'forget', 'spec', 'prompt', 'hook', 'gate', 'settings', 'relay',
])].sort();
}
+
+// This marker is emitted once when the actual CLI entrypoint finishes evaluating.
+if (process.env.PULLBOARD_STARTUP_TRACE_ENTRY
+ && resolve(process.argv[1] ?? '') === resolve(process.env.PULLBOARD_STARTUP_TRACE_ENTRY)) {
+ traceServeStartup('cli-module-load-complete');
+}
diff --git a/test/api.test.js b/test/api.test.js
index 79113ca3..265df43f 100644
--- a/test/api.test.js
+++ b/test/api.test.js
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
import { runFixtureExecFile as execFileSync, startFixtureChild as spawn, runFixtureChild as spawnSync, reportFixtureChildFailure, runFixtureChild, runFixtureGit, safeFixtureDiagnostic } from './fixture-child.js';
import { performance } from 'node:perf_hooks';
import {
+ appendFileSync,
chmodSync,
cpSync,
existsSync,
@@ -106,6 +107,11 @@ function sandbox() {
PULLBOARD_MACHINE_HOME: join(dir, 'home'),
};
delete env.PULLBOARD_RELAY_TOKEN;
+ delete env.PULLBOARD_STARTUP_TRACE_FILE;
+ delete env.PULLBOARD_STARTUP_TRACE_ENTRY;
+ delete env.PULLBOARD_STARTUP_TEST_HOLD_PHASE;
+ delete env.PULLBOARD_STARTUP_TEST_PHASE_REACHED_FILE;
+ delete env.PULLBOARD_STARTUP_TEST_RELEASE_FILE;
for (const marker of SSH_SHELL_MARKERS) delete env[marker];
const git = (cwd, ...args) => runFixtureGit(args, { cwd, env, encoding: 'utf8', stdio: 'pipe' });
const run = (cwd, ...args) => runFixtureChild(process.execPath, [BIN, ...args], { cwd, env, encoding: 'utf8' });
@@ -1009,10 +1015,51 @@ test('[N26,A2] roadmap text, JSON and API state follow live local and registered
});
/** Start the real local API server and stop it when its test finishes. */
-async function startApi(t, box) {
- const args = [BIN, 'serve', '--port', '0', '--json'];
+async function startApi(t, box, { traceStartup = false, traceFile: requestedTraceFile, readinessTimeoutMs = 10_000,
+ holdPhase, phaseReachedFile, releaseFile } = {}) {
+ const traceFile = requestedTraceFile ?? (traceStartup
+ ? process.env.PULLBOARD_STARTUP_TRACE_FILE ?? join(box.dir, 'serve-startup-trace.jsonl')
+ : process.env.PULLBOARD_STARTUP_TRACE_FILE);
+ const preloadFile = traceFile ? join(box.dir, 'startup-trace-preload.mjs') : null;
+ const childEnv = traceFile ? { ...box.env, PULLBOARD_STARTUP_TRACE_FILE: traceFile,
+ PULLBOARD_STARTUP_TRACE_ENTRY: BIN,
+ ...(holdPhase ? { PULLBOARD_STARTUP_TEST_HOLD_PHASE: holdPhase,
+ PULLBOARD_STARTUP_TEST_PHASE_REACHED_FILE: phaseReachedFile,
+ PULLBOARD_STARTUP_TEST_RELEASE_FILE: releaseFile } : {}) } : box.env;
const startedAt = performance.now();
- const child = spawn(process.execPath, args, { cwd: box.repo, env: box.env, stdio: ['ignore', 'pipe', 'pipe'] });
+ const args = traceFile
+ ? ['--import', preloadFile, BIN, 'serve', '--port', '0', '--json']
+ : [BIN, 'serve', '--port', '0', '--json'];
+ if (traceFile) {
+ const preloadLines = [
+ "import fs from 'node:fs';",
+ "import { syncBuiltinESMExports } from 'node:module';",
+ "import { loadavg } from 'node:os';",
+ "import { performance } from 'node:perf_hooks';",
+ 'const originalAppendFileSync = fs.appendFileSync.bind(fs);',
+ 'fs.appendFileSync = (path, data, ...options) => {',
+ ' const result = originalAppendFileSync(path, data, ...options);',
+ " if (String(path) === process.env.PULLBOARD_STARTUP_TRACE_FILE && process.env.PULLBOARD_STARTUP_TEST_HOLD_PHASE) {",
+ " const text = Buffer.isBuffer(data) ? data.toString('utf8') : String(data);",
+ " for (const line of text.split('\\n')) {",
+ ' let event;',
+ ' try { event = JSON.parse(line); } catch { continue; }',
+ ' if (event.phase !== process.env.PULLBOARD_STARTUP_TEST_HOLD_PHASE) continue;',
+ " originalAppendFileSync(process.env.PULLBOARD_STARTUP_TEST_PHASE_REACHED_FILE, 'phase reached\\n');",
+ ' const waitCell = new Int32Array(new SharedArrayBuffer(4));',
+ ' while (!fs.existsSync(process.env.PULLBOARD_STARTUP_TEST_RELEASE_FILE)) Atomics.wait(waitCell, 0, 0, 25);',
+ ' break;',
+ ' }',
+ ' }',
+ ' return result;',
+ '};',
+ 'syncBuiltinESMExports();',
+ "originalAppendFileSync(process.env.PULLBOARD_STARTUP_TRACE_FILE, JSON.stringify({ phase: 'process-entry', at: Date.now(), elapsedMs: performance.now(), processId: process.pid, cpuUsage: process.cpuUsage(), loadAverage: loadavg(), eventLoopUtilization: performance.eventLoopUtilization() }) + '\\n');",
+ ];
+ writeFileSync(preloadFile, `${preloadLines.join('\n')}\n`);
+ appendFileSync(traceFile, `${JSON.stringify({ phase: 'parent-spawn', at: Date.now(), elapsedMs: 0, processId: process.pid })}\n`);
+ }
+ const child = spawn(process.execPath, args, { cwd: box.repo, env: childEnv, stdio: ['ignore', 'pipe', 'pipe'] });
let stdout = '';
let stderr = '';
child.stdout.setEncoding('utf8');
@@ -1034,16 +1081,54 @@ async function startApi(t, box) {
if (settled) return;
settled = true;
clearTimeout(timeout);
- rejectReady(new Error(reportFixtureChildFailure({ command: process.execPath, args, status, signal,
- elapsedMs: performance.now() - startedAt, stderr, env: box.env, detail })));
+ clearInterval(phaseWait);
+ let startupDiagnostics = '';
+ let startupPhases = [];
+ if (traceFile) {
+ try {
+ startupPhases = readFileSync(traceFile, 'utf8').split('\n').filter(Boolean).map((line) => JSON.parse(line));
+ } catch { /* An unavailable trace is reported as an empty capture. */ }
+ const childStartedAt = startupPhases.find(({ phase }) => phase === 'process-entry')?.at ?? startupPhases[0]?.at;
+ const phaseLines = startupPhases.map(({ phase, at }) => {
+ const elapsed = Number.isFinite(at) && Number.isFinite(childStartedAt) ? Math.max(0, at - childStartedAt) : null;
+ return `- ${phase}: ${elapsed === null ? 'time unavailable' : `${elapsed}ms since child start`}`;
+ });
+ const lastPhase = startupPhases.at(-1)?.phase ?? 'no startup phase was recorded';
+ startupDiagnostics = `serve startup phases:\n${phaseLines.length ? phaseLines.join('\n') : '- no startup phases were recorded'}\nlast phase reached: ${lastPhase}`;
+ t.diagnostic(startupDiagnostics);
+ }
+ if (releaseFile && holdPhase) writeFileSync(releaseFile, 'release after timeout trace snapshot\n');
+ const failure = new Error(reportFixtureChildFailure({ command: process.execPath, args, status, signal,
+ elapsedMs: performance.now() - startedAt, stderr, env: box.env,
+ detail: startupDiagnostics ? `${detail}\n${startupDiagnostics}` : detail,
+ }));
+ failure.startupDiagnostics = startupDiagnostics;
+ failure.startupPhases = startupPhases;
+ rejectReady(failure);
};
- const timeout = setTimeout(() => failStartup('serve readiness deadline (10000ms) expired'), 10_000); // budget: hang ceiling for private serve readiness; no throughput claim.
+ let timeout;
+ let phaseWait;
+ const armReadinessDeadline = () => {
+ clearTimeout(timeout);
+ timeout = setTimeout(() => failStartup(`serve readiness deadline (${readinessTimeoutMs}ms) expired`), readinessTimeoutMs); // budget: caller-selected ceiling; default remains 10000ms from spawn.
+ };
+ if (holdPhase && phaseReachedFile) {
+ timeout = setTimeout(() => failStartup(`test fixture did not reach startup phase ${holdPhase} within 10000ms`), 10_000); // budget: hang ceiling for deterministic phase arrival only.
+ phaseWait = setInterval(() => {
+ if (!existsSync(phaseReachedFile)) return;
+ clearInterval(phaseWait);
+ armReadinessDeadline();
+ }, 5);
+ } else {
+ armReadinessDeadline();
+ }
const check = () => {
try {
const parsed = JSON.parse(stdout);
if (settled) return;
settled = true;
clearTimeout(timeout);
+ clearInterval(phaseWait);
resolveReady(parsed);
} catch {
// The startup envelope is pretty-printed across several chunks.
@@ -1056,6 +1141,9 @@ async function startApi(t, box) {
if (code !== 0) { clearTimeout(timeout); rejectReady(new Error(`serve exited ${code}: ${stderr}`)); }
});
});
+ if (traceFile) {
+ appendFileSync(traceFile, `${JSON.stringify({ phase: 'parent-ready-observed', at: Date.now(), elapsedMs: performance.now() - startedAt, processId: process.pid })}\n`);
+ }
assert.equal(document.version, 1);
const parsed = new URL(document.url);
const secret = parsed.searchParams.get('k');
@@ -1166,6 +1254,39 @@ test('[N38] local API and sealed presentation resolve shouts beyond recent state
'addressed reads and refusals do not change the recent-40 projection');
});
+test('[A2,C7] a missed serve readiness deadline names the slow startup phase', async (t) => {
+ const box = project();
+ const phaseReachedFile = join(box.dir, 'startup-phase-reached');
+ const releaseFile = join(box.dir, 'release-held-startup');
+ const traceFile = join(box.dir, 'held-startup-trace.jsonl');
+ rmSync(phaseReachedFile, { force: true });
+ rmSync(releaseFile, { force: true });
+ await assert.rejects(startApi(t, box, {
+ traceStartup: true,
+ traceFile,
+ readinessTimeoutMs: 1_000, // budget: short deterministic observation ceiling after the held registration-start phase.
+ holdPhase: 'serve-registration-start',
+ phaseReachedFile,
+ releaseFile,
+ }), (error) => {
+ assert.match(error.message, /serve readiness deadline \(1000ms\) expired/);
+ assert.match(error.message, /last phase reached: serve-registration-start/);
+ assert.match(error.message, /process-entry: \d+ms since child start/);
+ assert.match(error.message, /cli-module-load-complete: \d+ms since child start/);
+ assert.match(error.message, /cli-command-preamble-ready: \d+ms since child start/);
+ assert.match(error.message, /serve-context-start: \d+ms since child start/);
+ assert.match(error.message, /serve-context-ready: \d+ms since child start/,
+ 'the timeout names the recorded context-ready phase');
+ assert.ok(error.startupPhases.length > 0, 'the timeout retains its captured phases');
+ for (const { phase } of error.startupPhases) assert.ok(error.startupDiagnostics.includes(`- ${phase}:`),
+ `the diagnostic lists recorded phase ${phase}`);
+ assert.ok(error.message.includes(error.startupDiagnostics), 'the thrown error includes the emitted diagnostic lines');
+ assert.deepEqual(error.startupPhases.map(({ phase }) => phase).slice(-1), ['serve-registration-start']);
+ assert.match(error.startupDiagnostics, /last phase reached: serve-registration-start/);
+ return true;
+ });
+});
+
test('[A2] local HTTP v1 versions state and moves, authenticates, and preserves CLI refusals', async (t) => {
const box = project();
let lastStepEvidence = 'create local API';
@@ -1181,7 +1302,7 @@ test('[A2] local HTTP v1 versions state and moves, authenticates, and preserves
(evidence) => { lastStepEvidence = evidence; });
}
try {
- const api = await startApi(t, box);
+ const api = await startApi(t, box, { traceStartup: true });
/** Validate one captured HTTP refusal while keeping its originating step and response in every failure. */
function errorDocument(result) {
assertRequiredShape(result.document, JSON_SHAPES.error.required, `HTTP refusal\n${result.evidence}`);