* test(#3055): add the process seam and route runGsdTools through it Adds tests/helpers/process-seam.cjs — runNode/runGit/runHook over spawnSync, each returning a typed discriminated union { outcome, exitCode, stdout, stderr, timedOut, signal, killed, code }. Every call is timeout-bounded; there is no unbounded path. runGsdTools becomes an adapter over the seam. Its legacy { success, output, error, exitCode } shape and retry-once-on-kill behaviour are preserved byte-identically, so none of its 136 caller files change. Outcome discrimination was corrected against probed runtime behaviour rather than assumption: a timeout and a maxBuffer overflow are identical on both status (null) and signal (SIGTERM), and differ only by code (ETIMEDOUT vs ENOBUFS). Overflow is therefore classified before timeout. This fixes a live defect — the previous isKilled() treated an overflow as a kill, retried it for a second full 60s run, and then reported "host OOM or scheduler contention" for a child that had merely printed too much. Also widens the ESLint tests glob from tests/**/*.test.cjs to tests/**/*.cjs, which brought 31 previously unlinted shared helpers under the same rules their sibling test files already obey, and fixes the 5 violations that surfaced — including a bare npm invocation without shell:true in tests/helpers/emitted-runtime.cjs (DEFECT.WINDOWS-TEST-PORTABILITY), now routed through the existing portable runNpm helper. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * test(#3055): migrate every local spawn wrapper onto the process seam Replaces the spawn body of all 25 local runHook/runGuard/runGate definitions with a call to tests/helpers/process-seam.cjs. Each wrapper keeps its name, parameter list, return shape and post-processing (JSON parse, ANSI strip, env sanitising, field extraction) — only the spawn mechanism changes, so no test assertion moves. The 4 bash-driven wrappers use the seam's explicit `interpreter` option rather than a fourth primitive; it is explicit rather than inferred from the file extension, because guessing an interpreter from a path fails silently when a script's name does not match its shebang. Seven wrappers were previously unbounded and now carry an explicit timeout sized to what each actually runs, not the seam default. Two of those seven (gsd-write-guard, lint-docs-command-form) were absent from the issue's inventory entirely and were found by scanning after the migration. Adds the CONTEXT.md `### Process seam` glossary entry and a CONTRIBUTING.md reference section covering the three primitives, the discriminated union, and the two rules the seam enforces. Scope disclosure recorded in the phase design notes: the issue scoped three identifier names. A scan for local helpers that spawn AND return the spawn result finds 113 across 82 names, 71 of them unbounded, plus 122 unbounded direct git call sites. This change bounds 25 of those. The remaining surface is the same defect class and is NOT closed by this PR. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(#3055): classify an externally-killed child as KILLED, not EXITED Blocker found in this branch's own diff, independently confirmed by an isolated reviewer. A child killed by an external signal — a genuine bench OOM kill — makes spawnSync return { status: null, signal: 'SIGKILL' } with NO .error field. The seam's "no error implies EXITED" rule therefore classified it as a clean exit, and runGsdTools returned { success: false, exitCode: 1 } without retrying. That silently defeated the #969 kill-discrimination for precisely the case it was built for: the old isKilled() fired on `signal != null`, retried once, then threw a labelled resource-starvation error. A real OOM would have been reported as an ordinary assertion failure. Adds a fifth outcome, KILLED, for "no error but a signal is set", and makes the adapter retry on TIMED_OUT or KILLED — reproducing the old `killed || signal != null || code === 'ETIMEDOUT'` condition exactly. SPAWN_FAILED still does not retry (matching the old behaviour, where signal was null). BUFFER_OVERFLOW still does not retry, which remains a deliberate divergence: the old code retried it because signal was SIGTERM, burning a second 60s run on a child that had merely printed too much. All five outcomes verified against the live runtime rather than assumed: SIGKILL -> killed, exit 0/7 -> exited, timeout -> timed_out (ETIMEDOUT), >1MB stdout -> buffer_overflow (ENOBUFS). Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(#3055): address standards-review findings on this branch Three findings from the standards axis of the review, all in this branch's own diff. The CONTEXT.md glossary entry this branch introduced was already stale on the branch's own last commit: it enumerated a 4-member OUTCOME while the code had 5, because the KILLED fix did not update it. That is precisely the drift the "module changes update Domain-terms" gate exists to catch, so the entry now lists all five and explains KILLED. api-coverage-gate-e2e compared an outcome against the raw string 'exited' rather than OUTCOME.EXITED, the only such outlier; the enum is now imported and used. A sweep for the other four outcome literals found no further comparison sites. Three call sites hand the literal bash flag '-c' to the seam's first parameter, which the JSDoc described as an absolute script path. Rather than add a fourth primitive, the contract is corrected to match reality: the parameter is renamed `target` and documented as the first argv element handed to the interpreter — normally a script path, but for an interpreter invoked with an inline program it may be that interpreter's own flag. No behaviour change. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * test(#3055): assert the cross-platform timeout contract, not the macOS one The remote runner failed on both Linux lanes (node 22 and node 24, identical) while the same tests passed locally on macOS. Two assertions encoded a platform-specific behaviour as a cross-platform guarantee. When spawnSync times out, macOS preserves the child's partial stdout/stderr; Linux discards it and returns empty strings. Verified on node v26.5.1 both ways. The seam passes through whatever spawnSync hands it and cannot manufacture output that was discarded, so the production code was correct — the tests were wrong. Both tests now assert the guarantee the seam actually makes on every platform: outcome TIMED_OUT, timedOut true, and stdout/stderr always being strings rather than undefined or a Buffer. The partial-content assertions are retained behind an explicit process.platform === 'darwin' guard so the macOS coverage is not lost, and the first test is renamed to say what it now guarantees. This is the failure mode the remote matrix exists to catch: local macOS verification would have shipped it. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(#3055): classify a failed spawn as SPAWN_FAILED, not a timeout Windows CI caught two defects the Linux matrix could not. tests/context-predicates-query.test.cjs passes a 32K-char argv value. On Windows that exceeds the argv limit and spawnSync fails with code ENAMETOOLONG, signal null, status null. The seam's fallback rule — "otherwise, status === null implies TIMED_OUT" — swallowed it, so the adapter retried a spawn that can never succeed and then threw the resource-starvation error. The old isKilled() returned false for that shape and returned an ordinary failure result. TIMED_OUT is now identified positively: code === 'ETIMEDOUT' OR signal is set. Anything else carrying an error is SPAWN_FAILED, which covers ENAMETOOLONG, E2BIG, EACCES and ENOENT alike. The signal clause is what keeps a platform whose timeout errno differs classified correctly, so the greedy catch-all is no longer needed. The second defect is a contract regression I introduced and had claimed otherwise. That same test asserts `typeof r.exitCode === 'number'`, and toLegacyShape was returning null for BUFFER_OVERFLOW and SPAWN_FAILED, so the assertion failed on type. The old code returned `err.status ?? 1` on every non-retried failure path. The adapter now returns 1 again for both, and the comment claiming "never coerced to exitCode:1, unlike the pre-seam helper" is retracted: the seam keeps the richer truth (exitCode null plus a distinct outcome), the legacy adapter keeps the old numeric contract its callers actually depend on. Verified on this host: a 4MB argv yields E2BIG -> SPAWN_FAILED; ENOENT -> SPAWN_FAILED; timeout -> TIMED_OUT; >1MB stdout -> BUFFER_OVERFLOW; SIGKILL -> KILLED; clean exit -> EXITED. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: sim <sim@local> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
231 lines
8.3 KiB
JavaScript
231 lines
8.3 KiB
JavaScript
// allow-test-rule: runtime-contract-is-the-product see #1856
|
|
// The orchestrator cwd-drift guard (#48) is shell EMBEDDED in execute-phase.md.
|
|
// The shipped text IS the runtime contract, so these tests extract the block and
|
|
// EXECUTE it against real git fixtures rather than asserting on its characters —
|
|
// the readFileSync here is extraction for execution, not a source-grep assertion.
|
|
'use strict';
|
|
|
|
const { test } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const os = require('node:os');
|
|
const path = require('node:path');
|
|
const { execFileSync } = require('node:child_process');
|
|
const { cleanup } = require('./helpers.cjs');
|
|
const { runHook } = require('./helpers/process-seam.cjs');
|
|
|
|
const ROOT = path.resolve(__dirname, '..');
|
|
const WORKFLOW = path.join(ROOT, 'gsd-core', 'workflows', 'execute-phase.md');
|
|
const GUARD_MARKER = 'gsd:guard=orchestrator-cwd-drift';
|
|
|
|
/**
|
|
* Pull the guard's bash block out of the workflow. Anchored on a stable marker
|
|
* comment rather than a line range so the test does not rot when the file moves.
|
|
*/
|
|
function guardScript() {
|
|
const md = fs.readFileSync(WORKFLOW, 'utf8');
|
|
const fences = md.split('```');
|
|
for (let i = 1; i < fences.length; i += 2) {
|
|
const body = fences[i].replace(/^bash\r?\n/, '');
|
|
if (body.includes(GUARD_MARKER)) return body;
|
|
}
|
|
throw new Error(
|
|
`no fenced block carrying "${GUARD_MARKER}" in ${WORKFLOW} — the guard must be ` +
|
|
'marked so this contract test can execute the shipped text.',
|
|
);
|
|
}
|
|
|
|
const git = (cwd, ...args) =>
|
|
execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
|
|
|
|
/** A real repo with a base branch and one commit. */
|
|
function makeRepo() {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-1856-'));
|
|
git(dir, 'init', '--quiet', '--initial-branch', 'next');
|
|
git(dir, 'config', 'user.email', 'test@example.com');
|
|
git(dir, 'config', 'user.name', 'Test');
|
|
fs.writeFileSync(path.join(dir, 'base.txt'), 'base\n');
|
|
git(dir, 'add', '-A');
|
|
git(dir, 'commit', '--quiet', '-m', 'base');
|
|
return dir;
|
|
}
|
|
|
|
function commitFile(dir, name, msg) {
|
|
fs.writeFileSync(path.join(dir, name), `${name}\n`);
|
|
git(dir, 'add', '-A');
|
|
git(dir, 'commit', '--quiet', '-m', msg);
|
|
}
|
|
|
|
/** Run the extracted guard in `dir`. Never throws — returns the observed result. */
|
|
function runGuard(dir) {
|
|
// 30s: already bounded pre-migration (unchanged) — the guard runs a handful
|
|
// of git plumbing calls (rev-parse, log, status) against a small fixture repo.
|
|
const res = runHook('-c', [guardScript()], {
|
|
interpreter: 'bash',
|
|
cwd: dir,
|
|
timeoutMs: 30_000,
|
|
env: { ...process.env, GIT_TERMINAL_PROMPT: '0' },
|
|
});
|
|
return { status: res.exitCode, stdout: res.stdout || '', stderr: res.stderr || '' };
|
|
}
|
|
|
|
test('#1856: refusal names the stranded commits and the dirty tree', () => {
|
|
const dir = makeRepo();
|
|
try {
|
|
git(dir, 'checkout', '--quiet', '-b', 'worktree-agent-36.12-05-090827');
|
|
for (const m of ['wire roto toggle', 'preserve roto keys', 'simplify controls',
|
|
'count semantics', 'bound regeneration']) {
|
|
commitFile(dir, m.replace(/\s/g, '-') + '.txt', `fix(36.12-05): ${m}`);
|
|
}
|
|
fs.writeFileSync(path.join(dir, 'uncommitted.txt'), 'work in progress\n');
|
|
|
|
const r = runGuard(dir);
|
|
assert.equal(r.status, 1, 'the guard must still REFUSE — #48 is load-bearing');
|
|
assert.match(r.stderr, /worktree-agent-36\.12-05-090827/, 'names the branch');
|
|
|
|
// The whole point of #1856: the refusal must not be a dead end.
|
|
assert.match(
|
|
r.stderr,
|
|
/5\s+commit/i,
|
|
'must report how many commits are stranded on the agent branch — without this the ' +
|
|
'user cannot tell that switching away loses work (#1856)',
|
|
);
|
|
assert.match(
|
|
r.stderr,
|
|
/uncommitted|dirty|unstaged/i,
|
|
'must report that the worktree still has uncommitted changes (#1856)',
|
|
);
|
|
assert.match(
|
|
r.stderr,
|
|
/cherry-pick|merge/i,
|
|
'must give an integration command, not just "re-run from the orchestrator worktree"',
|
|
);
|
|
} finally {
|
|
cleanup(dir);
|
|
}
|
|
});
|
|
|
|
test('#1856: the bare agent- namespace is handled identically', () => {
|
|
const dir = makeRepo();
|
|
try {
|
|
git(dir, 'checkout', '--quiet', '-b', 'agent-a1b2c3');
|
|
commitFile(dir, 'one.txt', 'feat: one');
|
|
const r = runGuard(dir);
|
|
assert.equal(r.status, 1);
|
|
assert.match(r.stderr, /agent-a1b2c3/);
|
|
assert.match(r.stderr, /1\s+commit/i);
|
|
} finally {
|
|
cleanup(dir);
|
|
}
|
|
});
|
|
|
|
test('#1856: a clean agent worktree still refuses, without a wall of empty sections', () => {
|
|
const dir = makeRepo();
|
|
try {
|
|
git(dir, 'checkout', '--quiet', '-b', 'agent-clean');
|
|
const r = runGuard(dir);
|
|
assert.equal(r.status, 1, 'still refuses');
|
|
// Nothing is stranded, so nothing must be claimed to be.
|
|
assert.doesNotMatch(
|
|
r.stderr,
|
|
/\b[1-9]\d*\s+commit/i,
|
|
'a branch with no commits ahead must not report stranded commits',
|
|
);
|
|
assert.doesNotMatch(
|
|
r.stderr,
|
|
/uncommitted changes/i,
|
|
'a clean tree must not be reported as dirty',
|
|
);
|
|
} finally {
|
|
cleanup(dir);
|
|
}
|
|
});
|
|
|
|
test('#1856: boundary — 1 and 2 commits ahead are both reported accurately', () => {
|
|
for (const n of [1, 2]) {
|
|
const dir = makeRepo();
|
|
try {
|
|
git(dir, 'checkout', '--quiet', '-b', 'agent-count');
|
|
for (let i = 0; i < n; i += 1) commitFile(dir, `c${i}.txt`, `feat: c${i}`);
|
|
const r = runGuard(dir);
|
|
assert.equal(r.status, 1);
|
|
assert.match(r.stderr, new RegExp(`\\b${n}\\s+commit`, 'i'), `${n} ahead reported`);
|
|
} finally {
|
|
cleanup(dir);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('#1856: does not fire on an ordinary orchestrator branch', () => {
|
|
const dir = makeRepo();
|
|
try {
|
|
const r = runGuard(dir); // still on `next`
|
|
assert.equal(r.status, 0, `guard must not fire on a normal branch: ${r.stderr}`);
|
|
assert.doesNotMatch(r.stderr, /refusing to execute waves/);
|
|
} finally {
|
|
cleanup(dir);
|
|
}
|
|
});
|
|
|
|
test('#1856: does not fire on a branch that merely starts with "agent"', () => {
|
|
const dir = makeRepo();
|
|
try {
|
|
// The discriminator is the `agent-` NAMESPACE. `agentic-refactor` is an
|
|
// ordinary feature branch and must run.
|
|
git(dir, 'checkout', '--quiet', '-b', 'agentic-refactor');
|
|
const r = runGuard(dir);
|
|
assert.equal(r.status, 0, `guard must not fire on agentic-refactor: ${r.stderr}`);
|
|
} finally {
|
|
cleanup(dir);
|
|
}
|
|
});
|
|
|
|
test('#1856: does not fire on a legitimate feature worktree', () => {
|
|
// Recorded constraint in the guard's own comment: the discriminator is the branch
|
|
// namespace, NOT the .claude/worktrees/ path — the orchestrator may legitimately
|
|
// run from a feature worktree there, and a path check would break that.
|
|
const dir = makeRepo();
|
|
try {
|
|
const wt = path.join(dir, '.claude', 'worktrees', 'feature');
|
|
fs.mkdirSync(path.dirname(wt), { recursive: true });
|
|
git(dir, 'worktree', 'add', '--quiet', '-b', 'feat/legit', wt);
|
|
const r = runGuard(wt);
|
|
assert.equal(r.status, 0, `a feature worktree must run: ${r.stderr}`);
|
|
} finally {
|
|
cleanup(dir);
|
|
}
|
|
});
|
|
|
|
test('#1856: reporting degrades to the plain refusal when no base ref resolves', () => {
|
|
// No origin, no main/next to compare against — the guard must still refuse
|
|
// cleanly rather than crash or hang before printing.
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-1856-nobase-'));
|
|
try {
|
|
git(dir, 'init', '--quiet', '--initial-branch', 'agent-orphan');
|
|
git(dir, 'config', 'user.email', 'test@example.com');
|
|
git(dir, 'config', 'user.name', 'Test');
|
|
fs.writeFileSync(path.join(dir, 'a.txt'), 'a\n');
|
|
git(dir, 'add', '-A');
|
|
git(dir, 'commit', '--quiet', '-m', 'only');
|
|
const r = runGuard(dir);
|
|
assert.equal(r.status, 1, 'still refuses with no resolvable base');
|
|
assert.match(r.stderr, /refusing to execute waves/);
|
|
} finally {
|
|
cleanup(dir);
|
|
}
|
|
});
|
|
|
|
test('#1856: detached HEAD does not crash the guard', () => {
|
|
const dir = makeRepo();
|
|
try {
|
|
const sha = git(dir, 'rev-parse', 'HEAD').trim();
|
|
git(dir, 'checkout', '--quiet', '--detach', sha);
|
|
const r = runGuard(dir);
|
|
// `rev-parse --abbrev-ref HEAD` yields "HEAD" when detached — not an agent
|
|
// branch, so the guard must pass through without erroring.
|
|
assert.equal(r.status, 0, `detached HEAD must not crash the guard: ${r.stderr}`);
|
|
} finally {
|
|
cleanup(dir);
|
|
}
|
|
});
|