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.

refactor(cli): move stdin, trace and hook-read helpers out of cli.js

Corey Olson0ed050174eb7
diff --git a/src/cli-support.js b/src/cli-support.js
new file mode 100644
index 00000000..12abac9d
--- /dev/null
+++ b/src/cli-support.js
@@ -0,0 +1,65 @@
+/**
+ * Helpers the command line uses to read its caller's input, trace its startup and read the board for a hook. None
+ * keeps command state, so each is a plain function of its arguments [C9].
+ */
+import {
+ appendFileSync,
+ existsSync,
+} from 'node:fs';
+import { loadavg } from 'node:os';
+import { resolve } from 'node:path';
+import { performance } from 'node:perf_hooks';
+import * as store from './board.js';
+
+/** Append an opt-in startup phase marker for the real CLI fixture measurement [A2,C7]. */
+export 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 one consent line without waiting for EOF in an interactive terminal [V2]. */
+export async function firstInputLine(stdin) {
+ if (!stdin) return '';
+ let text = '';
+ for await (const chunk of stdin) {
+ text += chunk.toString();
+ const newline = text.indexOf('\n');
+ if (newline >= 0) return text.slice(0, newline);
+ if (text.length > 256) return '';
+ }
+ return text;
+}
+
+/**
+ * Read everything git writes on a hook's stdin, or nothing when run by hand.
+ *
+ * @param {any} stdin
+ * @returns {Promise<string>}
+ */
+export async function readStdin(stdin) {
+ if (!stdin || stdin.isTTY) return '';
+ const chunks = [];
+ for await (const chunk of stdin) chunks.push(chunk);
+ return Buffer.concat(chunks).toString('utf8');
+}
+
+/** Wait up to the board's shared busy timeout while reading hook facts, without migrating its schema. */
+export function hookRead(ctx, read) {
+ if (!existsSync(ctx.file)) return null;
+ const startedAt = Date.now();
+ let db;
+ try {
+ db = store.openBoardReadOnly(ctx.file);
+ return read({ db, clock: ctx.clock });
+ } catch (error) {
+ throw store.boardBusyError(ctx.file, startedAt, error);
+ } finally { db?.close(); }
+}
diff --git a/src/cli.js b/src/cli.js
index ecaf27ff..a95659bb 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -11,7 +11,6 @@ import {
spawnSync,
} from 'node:child_process';
import {
- appendFileSync,
existsSync,
mkdirSync,
readdirSync,
@@ -22,7 +21,6 @@ import {
} from 'node:fs';
import {
hostname,
- loadavg,
} from 'node:os';
import {
basename,
@@ -32,7 +30,6 @@ import {
relative,
resolve,
} from 'node:path';
-import { performance } from 'node:perf_hooks';
import { pathToFileURL } from 'node:url';
import { parseArgs } from 'node:util';
import {
@@ -43,6 +40,12 @@ import {
import { reviewerStartup } from './next-steps.js';
import { submitPreflight } from './submit-preflight.js';
import * as store from './board.js';
+import {
+ firstInputLine,
+ hookRead,
+ readStdin,
+ traceServeStartup,
+} from './cli-support.js';
import {
CONFIG_FILE,
COORDINATOR,
@@ -460,20 +463,6 @@ const CHECKOUT_LEASES = new WeakMap();
const CHECKOUT_BOARD_ONLY = 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.
*
@@ -4223,45 +4212,6 @@ async function specCommand(io, { first, second, rest, values }) {
throw new Refused('USAGE', 'pullboard spec --json | check | view | show <id> | unmet [--must] | draft "<text>" | signoff <ids> | signers add | approve <ids> | decline <ids> --reason "why" | answer <id> "<choice or text>" | apply');
}
-/** Read one consent line without waiting for EOF in an interactive terminal [V2]. */
-async function firstInputLine(stdin) {
- if (!stdin) return '';
- let text = '';
- for await (const chunk of stdin) {
- text += chunk.toString();
- const newline = text.indexOf('\n');
- if (newline >= 0) return text.slice(0, newline);
- if (text.length > 256) return '';
- }
- return text;
-}
-
-/**
- * Read everything git writes on a hook's stdin, or nothing when run by hand.
- *
- * @param {any} stdin
- * @returns {Promise<string>}
- */
-async function readStdin(stdin) {
- if (!stdin || stdin.isTTY) return '';
- const chunks = [];
- for await (const chunk of stdin) chunks.push(chunk);
- return Buffer.concat(chunks).toString('utf8');
-}
-
-/** Wait up to the board's shared busy timeout while reading hook facts, without migrating its schema. */
-function hookRead(ctx, read) {
- if (!existsSync(ctx.file)) return null;
- const startedAt = Date.now();
- let db;
- try {
- db = store.openBoardReadOnly(ctx.file);
- return read({ db, clock: ctx.clock });
- } catch (error) {
- throw store.boardBusyError(ctx.file, startedAt, error);
- } finally { db?.close(); }
-}
-
/**
* The hooks git runs. In a repo without pullboard.json they pass, so a stray hook never blocks.
*