fix(harness): move harness-specific output handling into the harness plugins (#1855)

This commit is contained in:
jason.woltje committed 2026-11-06 18:27:58 -06:00
1 parent ad9b2dee42
commit 08b7db413b
14 files changed
+129 -83

No files matched your search

+4 -2
View File
@@ -20,7 +20,7 @@ loader.
| `limit-check` | 2 | list | `interfaceVersion`, `mayLaunch`, `mustStop`, `report` |
| `search-source` | 1 | one | `interfaceVersion`, `search` |
| `channel` | 1 | list | `interfaceVersion`, `dispatch` |
| `harness` | 1 | list | `interfaceVersion`, `adapter`, `levels`, `preflight`, `version`, `countsAccount`, `mounts`, `builtinWeb`, `builtins`, `typedName`, `reliesOn`, `gateTimeout`, `bundle`, `env`, `turnEnd`, `transcript` |
| `harness` | 1 | list | `interfaceVersion`, `adapter`, `levels`, `preflight`, `version`, `countsAccount`, `mounts`, `builtinWeb`, `builtins`, `typedName`, `reliesOn`, `gateTimeout`, `bundle`, `env`, `turnEnd`, `outputPolicy`, `transcript` |
Every module exports `interfaceVersion`, equal to its kind's interface
version, plus the kind's functions. PRD REQ-LIMIT-4 says what the two kinds
@@ -150,7 +150,7 @@ export returns:
- `countsAccount()` says whether its launches spend `launch.account` and get
the row 126 account checks.
- `mounts`, `builtinWeb`, `builtins`, `typedName`, `reliesOn`,
`gateTimeout`, `bundle`, `env`, `turnEnd` and `transcript` give its
`gateTimeout`, `bundle`, `env`, `turnEnd`, `outputPolicy` and `transcript` give its
sandbox binds, web, tool names, S0 lines, bundle files, adapter variables,
how a turn ends and where its transcripts land.
- `compactions({store, cursors, budget, streams, first})` reads a thread
@@ -391,3 +391,5 @@ It supplies proxy-labelled per-account numbers alongside the native per-key
counts; it has no quota authority. See
[the module contract](usage/cliproxy-queue/README.md) for poll settings, ID mapping,
record separation, bounded reads and per-key quota settings.
A harness declares `outputPolicy({ proxy })` as `adapter`, `non-newline-stdout`, or `stdout`. The launcher validates and serializes that declaration; old sessions retain their recorded activity behavior.
@@ -36,6 +36,7 @@ const GATE_TIMEOUT = Object.freeze({ n: 10, k: 2, hook: 20 });
export const adapter = () => join(REPO, "adapters", "claude", "adapter.sh");
export const levels = () => LEVELS;
export const outputPolicy = () => "adapter";
const executable = (path) => {
try {
+2
View File
@@ -34,6 +34,7 @@ const RELIES_ON = Object.freeze(["S0-1 tool_call block", "S0-2 a throw blocks",
export const adapter = () => join(REPO, "adapters", "pi", "adapter.sh");
export const levels = () => LEVELS;
export const outputPolicy = ({ proxy }) => proxy ? "non-newline-stdout" : "stdout";
// The session runs pi from the repository's node_modules, which the sandbox mounts.
export function preflight({ env = {}, proxy = null } = {}) {
@@ -58,6 +59,7 @@ export function version() {
// Pi sessions run on a provider key, never the Claude account limit.
export const countsAccount = () => false;
export const mounts = () => [
{ src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" },
{ src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" },
...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })),
];
@@ -0,0 +1,66 @@
import { firstOutputMarker } from '../../../harness/src/first-output.mjs';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { spawn } from 'node:child_process';
import { constants } from 'node:os';
// The managed Pi adapter uses this monitor to translate its native JSON event
// streams back into the answer-only adapter contract. Startup, headers and
// keepalives are not model output; substantive deltas and named tool calls count.
export function modelEventStream({ runDir }) {
const mark = firstOutputMarker(runDir);
let pending = '', answer = null, failed = false;
const line = value => {
if (!value) return;
const event = JSON.parse(value);
const delta = event.type === 'message_update' && event.assistantMessageEvent;
if (delta && ['text_delta', 'thinking_delta', 'toolcall_delta'].includes(delta.type) && typeof delta.delta === 'string' && delta.delta.length) mark(delta.delta);
if (delta?.type === 'toolcall_start' && typeof delta.id === 'string' && delta.id.length && typeof delta.toolName === 'string' && delta.toolName.length) mark('output');
if (event.type === 'agent_end') {
const last = event.messages?.at(-1);
if (last?.role === 'assistant') {
failed = ['error', 'aborted'].includes(last.stopReason);
answer = (last.content ?? []).filter(part => part.type === 'text').map(part => part.text + '\n').join('');
}
}
};
return {
push(chunk) {
pending += chunk;
if (pending.length > 32 * 1024 * 1024) throw new Error('model event too large');
let i; while ((i = pending.indexOf('\n')) !== -1) { line(pending.slice(0, i)); pending = pending.slice(i + 1); }
},
finish() {
if (pending) line(pending);
if (failed || typeof answer !== 'string') throw new Error('model stream did not complete');
return answer;
},
};
}
// Invoked only by managed proxy adapter turns; the model CLI keeps its normal
// process group and stderr, while stdout remains the final answer alone.
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
const [, , harness, runDir, command, ...args] = process.argv;
const stream = modelEventStream({ harness, runDir });
const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'inherit'] });
let bad = false;
child.stdout.setEncoding('utf8');
child.stdout.on('data', chunk => {
if (bad) return;
try { stream.push(chunk); }
catch { bad = true; child.kill('SIGTERM'); }
});
child.on('error', () => { bad = true; });
child.on('close', (code, signal) => {
if (bad) { process.stderr.write('model event stream failed\n'); process.exitCode = 1; return; }
if (signal) {
process.stderr.write(`model child killed by ${signal}\n`);
const number = constants.signals[signal];
process.exitCode = Number.isInteger(number) && number > 0 ? 128 + number : 1;
return;
}
if (code !== 0) { process.exitCode = code ?? 1; return; }
try { process.stdout.write(stream.finish()); }
catch { process.stderr.write('model event stream failed\n'); process.exitCode = 1; }
});
}
+2 -1
View File
@@ -63,13 +63,14 @@ export const KINDS = Object.freeze({
// bundle({ dir, files, session, write, quote }) -> { files, digests, sources }, its own bundle files
// env({ files, policy, tools, session }) -> its adapter variables, written into session.json
// turnEnd() -> "marker" (a turn ends only with the turn marker) | "exit"
// outputPolicy({ proxy }) -> "adapter" | "non-newline-stdout" | "stdout"
// transcript({ runDir, store }) -> the directory its transcripts land in, for usage
// compactions({ store, cursors, budget, streams, first }) -> { found, cursors, known, reason, version },
// the session store's compactions past the ledger's cursors (row 307)
harness: Object.freeze({
kind: "harness", interfaceVersion: 1, switch: "list",
exports: Object.freeze(["adapter", "levels", "preflight", "version", "countsAccount", "mounts", "builtinWeb", "builtins",
"typedName", "reliesOn", "gateTimeout", "bundle", "env", "turnEnd", "transcript", "compactions"]),
"typedName", "reliesOn", "gateTimeout", "bundle", "env", "turnEnd", "outputPolicy", "transcript", "compactions"]),
}),
// Optional proxy telemetry (row 333): read({ readUsageQueue }) -> the drained events. The bus
// hands the module that one function and nothing else from its management client (R3).
+3 -2
View File
@@ -47,6 +47,7 @@ test("row 65 each module answers for its own harness: adapter, levels, account,
assert.deepEqual([pi.countsAccount(), claude.countsAccount()], [false, true]);
assert.deepEqual([pi.builtinWeb(), claude.builtinWeb()], [false, true]);
assert.deepEqual([pi.turnEnd(), claude.turnEnd()], ["marker", "exit"]);
assert.deepEqual([pi.outputPolicy({ proxy: true }), pi.outputPolicy({ proxy: false }), claude.outputPolicy({ proxy: true }), claude.outputPolicy({ proxy: false })], ["non-newline-stdout", "stdout", "adapter", "adapter"]);
assert.deepEqual([pi.typedName("list_tasks"), claude.typedName("list_tasks")], ["list_tasks", "mcp__mosaic__list_tasks"]);
assert.equal(pi.gateTimeout(), null);
assert.deepEqual(claude.gateTimeout(), { n: 10, k: 2, hook: 20 });
@@ -103,7 +104,7 @@ test("row 65 pi preflight refuses missing credentials, and its version is the pa
const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8"));
assert.equal(pi.version({ env: {} }), pkg.dependencies?.["@earendil-works/pi-coding-agent"] ?? pkg.devDependencies?.["@earendil-works/pi-coding-agent"]);
// Row 66: the unslop extension's files, bound read-only at their own paths.
assert.deepEqual(pi.mounts({ env: {} }).map(m => m.dest ?? m.src), [join(REPO, "node_modules"), ...["extension.ts", "unslop-check.js", "lists.json"].map(f => join(REPO, "tools", "unslop-hook", f))]);
assert.deepEqual(pi.mounts({ env: {} }).map(m => m.dest ?? m.src), [join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), join(REPO, "node_modules"), ...["extension.ts", "unslop-check.js", "lists.json"].map(f => join(REPO, "tools", "unslop-hook", f))]);
});
test("row 65 env passes the thinking level to both adapters, and claude-code's bundle writes its hook and MCP files", async t => {
@@ -216,6 +217,6 @@ test("row 65 no core source outside packages/plugins names a harness, apart from
});
test("row 65 the grep's pattern catches a quoted name and a regex alternation, and not a word in prose", () => {
for (const line of ['if (harness === "pi")', "x = 'claude-code';", "/^(pi|codex)$/", "/^(codex|claude-code)$/"]) assert.ok(NAMED.test(line), line);
for (const line of ['if (harness === "pi")', "x = 'claude-code';", "/^(pi|codex)$/", "/^(codex|claude-code)$/", "if (harness !== 'pi')", "['pi', 'claude-code'].includes(harness)"]) assert.ok(NAMED.test(line), line);
for (const line of ["// pi runs the bundle", "const piModel = 1;", "pipeline"]) assert.ok(!NAMED.test(line), line);
});
+1 -1
View File
@@ -17,7 +17,7 @@ test("REQ-PLUG-1 REQ-LIMIT-4 the registry: usage-source at interface version 1,
assert.deepEqual({ ...KINDS.harness, exports: [...KINDS.harness.exports] }, {
kind: "harness", interfaceVersion: 1, switch: "list",
exports: ["adapter", "levels", "preflight", "version", "countsAccount", "mounts", "builtinWeb", "builtins",
"typedName", "reliesOn", "gateTimeout", "bundle", "env", "turnEnd", "transcript", "compactions"],
"typedName", "reliesOn", "gateTimeout", "bundle", "env", "turnEnd", "outputPolicy", "transcript", "compactions"],
});
assert.ok(Object.isFrozen(KINDS.harness) && Object.isFrozen(KINDS.harness.exports));
assert.ok(Object.isFrozen(KINDS) && Object.isFrozen(KINDS["usage-source"]) && Object.isFrozen(KINDS["limit-check"].exports));