Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(cli): D2 guard seams for per-subcommand JSON contracts and child stdio
garrytan1e19fd08c382
diff --git a/src/cli/command-table.ts b/src/cli/command-table.tsindex ab71bed90e..126503cdbf 100644--- a/src/cli/command-table.ts+++ b/src/cli/command-table.ts@@ -89,6 +89,12 @@ interface CliCommandBase {skipStartupHooks?: true;/** Agent contract v1 (D2): the command's `--json` output contract; turns the stdout guard on. */json?: 'document' | 'ndjson';+ /**+ * D2: per-subcommand `--json` contracts for a record whose subcommands+ * differ in shape (keyed by the first positional after the command); a+ * subcommand not listed keeps its current stdout.+ */+ jsonSubcommands?: Readonly<Record<string, 'document' | 'ndjson'>>;/** The whole invocation, startup included, is read-only (eligible as a `fix.verify`). */read_only?: true;/** A4: consent-gated command; connects probe-only until consent is granted. */diff --git a/src/cli/json-guard.ts b/src/cli/json-guard.tsindex 5830ac77e8..b5d8f94030 100644--- a/src/cli/json-guard.ts+++ b/src/cli/json-guard.ts@@ -1,17 +1,23 @@/*** Agent contract v1 (D2): which `--json` guard mode this invocation runs* under. The guard is on only for commands whose command-table record- * declares `json` (they write their result through writeStdoutFinal /- * writeNdjsonLine), so undeclared commands keep their current stdout.- * Also wires the E11 `json_document_missing` record for the exit-0 bug case.+ * declares `json` (or, for a record whose subcommands differ in shape,+ * `jsonSubcommands[<first positional after the command>]`); they write their+ * result through writeStdoutFinal / writeNdjsonLine, so undeclared commands+ * keep their current stdout. Also wires the E11 `json_document_missing`+ * record for the exit-0 bug case.*/import { jsonRequested, setJsonDocumentMissingHook } from '../core/cli-force-exit.ts';import { recordAgentContractEvent } from '../core/agent-contract-log.ts';import { findCliCommand } from './command-table.ts';export function agentJsonGuardMode(argv: readonly string[]): { json?: 'document' | 'ndjson' } {- const command = argv.find(a => !a.startsWith('-') && findCliCommand(a) !== undefined);- const json = command ? findCliCommand(command)?.json : undefined;+ const at = argv.findIndex(a => !a.startsWith('-') && findCliCommand(a) !== undefined);+ const command = at >= 0 ? argv[at] : undefined;+ const record = command ? findCliCommand(command) : undefined;+ const sub = argv.slice(at + 1).find(a => !a.startsWith('-'));+ const subs = record?.jsonSubcommands;+ const json = record?.json ?? (sub !== undefined && subs && Object.hasOwn(subs, sub) ? subs[sub] : undefined);if (!json || !jsonRequested(argv)) return {};setJsonDocumentMissingHook(() => recordAgentContractEvent({ transport: 'cli', command, code: 'json_document_missing', outcome: 'committed' }));return { json };diff --git a/src/core/cli-force-exit.ts b/src/core/cli-force-exit.tsindex e79fe9682b..61f36def72 100644--- a/src/core/cli-force-exit.ts+++ b/src/core/cli-force-exit.ts@@ -766,7 +766,8 @@ function writeJsonFallbackIfMissing(exitCode: number): void {if (!jsonGuardMode || jsonDocumentWritten) return;if (exitCode === 0) {// Exit 0 with no document is a bug in the command (D5 fails it); log it, never invent a result.- recordJsonDocumentMissing();+ // An NDJSON stream may legitimately be empty (e.g. `eval export` with no rows).+ if (jsonGuardMode === 'document') recordJsonDocumentMissing();return;}jsonDocumentWritten = true;@@ -837,6 +838,22 @@ export function writeJsonDocument(text: string, unguarded: (text: string) => voireturn Promise.resolve();}+/** The guard mode this process runs under (null: no `--json` guard). */+export function jsonGuardActive(): 'document' | 'ndjson' | null {+ return jsonGuardMode;+}++/**+ * D2: one record of an NDJSON command. Under the guard it is one compact+ * line on fd 1 (writeNdjsonLine); unguarded it goes through the caller's+ * legacy writer, so in-process callers and tests see no change.+ */+export function writeJsonLine(line: unknown, unguarded: (line: unknown) => void): Promise<void> {+ if (jsonGuardMode) return writeNdjsonLine(line);+ unguarded(line);+ return Promise.resolve();+}+/** One NDJSON line on fd 1 (the only stdout path for `json: 'ndjson'` commands under the guard). */export async function writeNdjsonLine(line: unknown): Promise<void> {const status = (line as { status?: unknown } | null)?.status;@@ -856,6 +873,15 @@ export function spawnCliChild(cmd: string, args: readonly string[], opts: SpawnOreturn child;}+/**+ * stdio for a synchronous child CLI (execSync / execFileSync): inherited,+ * except that under the `--json` guard the child's stdout is this process's+ * stderr (fd 2), so it can never corrupt the JSON document.+ */+export function cliChildStdio(): 'inherit' | ['inherit', 2, 'inherit'] {+ return jsonGuardMode ? ['inherit', 2, 'inherit'] : 'inherit';+}+export interface FinishCliTeardownOpts {/** Engine to disconnect. A disconnect throw is warned + swallowed (D3). */engine: { disconnect(): Promise<void> };