Files
seal-use/src/streams.test.ts
T
gitea faf26b4c5b Fail the suite when code writes outside the stdout allow-list
The stdout contract was enforced by review alone, which is how `probe` shipped
prose on the machine channel: 161 output sites and no test ever asked which
stream they used. This test scans `src/` for `console.log` / `process.stdout.write`
calls and fails on any file outside the documented exceptions — `printer.ts`
(the machine-channel writer), the two document-dump commands (`doctor guide`,
`skill dump`), and the entry points that echo the captured result.

Asserted in both directions: a new writer fails, and an allow-listed file that
stops writing fails too, so the exception list cannot outlive its justification.
2026-09-30 01:16:25 +08:00

79 lines
2.9 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { readdirSync, readFileSync } from "node:fs";
import { join, relative } from "node:path";
/**
* The stdout contract, enforced as a test rather than as review vigilance.
*
* `stdout` is the machine channel: the Agent Harness parses it directly into
* typed payloads, so a stray `console.log("[probe] …")` between two JSON blobs
* breaks every consumer (the repo shipped exactly that bug in `seal-use probe`).
* Diagnostics belong on `stderr`, which needs no permission.
*
* The allow-list below mirrors the exceptions AGENTS.md already documents:
* `printer.ts` is *the* machine-channel writer (`printJson` / `printHelp`), the
* two document-dump commands are exempt because their output *is* a document,
* and `cli.ts` / `server.ts` are the entry points that echo the captured result.
*
* Both directions are asserted: a new writer fails the test, and an exception
* whose file stopped writing fails too, so the list cannot rot.
*/
const SRC_DIR = join(import.meta.dir);
const STDOUT_WRITERS = new Set([
"cli/printer.ts",
"cli/commands/doctor.ts",
"cli/commands/skill.ts",
"cli.ts",
"server.ts"
]);
const STDOUT_CALL = /(^|[^.\w])console\.log\s*\(|process\.stdout\.write\s*\(/;
function listTypeScriptFiles(dir: string): string[] {
const found: string[] = [];
for (const entry of readdirSync(dir, { withFileTypes: true })) {
if (entry.name.startsWith(".")) continue;
const full = join(dir, entry.name);
if (entry.isDirectory()) found.push(...listTypeScriptFiles(full));
else if (entry.isFile() && entry.name.endsWith(".ts")) found.push(full);
}
return found;
}
function codeLines(source: string): string[] {
// Comments may legitimately discuss console.log; only executable code counts.
return source
.replace(/\/\*[\s\S]*?\*\//g, "")
.replace(/^[ \t]*\/\/.*$/gm, "")
.split("\n");
}
describe("stdout stream contract", () => {
test("nothing writes to stdout outside the documented machine channel", () => {
const violations: string[] = [];
const actualWriters = new Set<string>();
for (const file of listTypeScriptFiles(SRC_DIR)) {
const rel = relative(SRC_DIR, file).split("\\").join("/");
if (rel.endsWith(".test.ts") || rel.endsWith(".generated.ts")) continue;
codeLines(readFileSync(file, "utf8")).forEach((line, index) => {
if (!STDOUT_CALL.test(line)) return;
actualWriters.add(rel);
if (!STDOUT_WRITERS.has(rel)) {
violations.push(`${rel}:${index + 1}: ${line.trim()}`);
}
});
}
expect(violations).toEqual([]);
// An allow-listed file that no longer writes to stdout has lost its reason
// to be exempt; drop it rather than let the exception outlive the code.
const staleExceptions = [...STDOUT_WRITERS].filter((file) => !actualWriters.has(file));
expect(staleExceptions).toEqual([]);
});
});