* feat(#3045): deny an executor dispatch that drops its isolation flag Every isolation gate already resolved correctly. The resolved value then reached the executor through a prose instruction telling the model to substitute it into a call the model composes itself, and nothing verified the substitution. When it was dropped, the executor edited and committed in the user's primary checkout with no consent and no warning. A prose backstop would be the same class of artifact as the defect, so this is a shipped PreToolUse hook on the Agent tool. It fires at the instant of the call rather than being read once at the top of a workflow, which is the only placement the model cannot skip. The guard is inert unless it can positively establish that this is a GSD project, that the project resolves to harness isolation, and that the dispatch targets an executor. A non-GSD repo has no invariant to enforce. Where it cannot read the configuration at all, it denies rather than assuming, with its own reason -- a guard that cannot verify must not answer safe. A malformed payload allows rather than throwing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * feat(#3045): extend the isolation guard to Cursor Cursor is the second of only two runtimes that resolve harness isolation, so shipping the guard for Claude alone left half the exposed surface unguarded while the changeset implied it was covered. The two runtimes fail differently. On Claude the harness flag is a per-dispatch kwarg the model must copy into a call it composes, and the defect is that it can be dropped. On Cursor the flag is --worktree, which applies to the whole session, and the subagent-start payload carries no isolation field at all. There is no flag to check, so the guard verifies the effective state instead: whether the workspace is genuinely running outside the user's primary checkout. That is a stronger check than the Claude one because it tests reality rather than intent, and it is commented so nobody later rewrites it into a flag check. Isolation is established two ways, either sufficient: the workspace resolves to a linked git worktree, or it sits under the worktree root Cursor manages. The second matters because a directory Cursor placed there is a legitimate isolated session even before it becomes a distinct git worktree, where linkage alone would report no repository. Detecting linkage required a new primitive rather than the existing context resolver. That resolver short-circuits on finding a local .planning directory before it ever compares the git directory to the common one -- and an isolation worktree normally has its own checked-out .planning. Reusing it would have read a correctly isolated session as unisolated and denied it, which is the failure direction that gets a guard switched off. The comparison is now its own shortcut-free function that the resolver delegates to after its own shortcut, so existing behavior is unchanged, and the case that would have broken is pinned. The subagent type is checked before any configuration is read, so an unreadable config cannot deny a dispatch this guard would never have enforced against. The input-schema comment on the Cursor hook documented only the fields common to every event and omitted the ones specific to this one. That omission cost a halt during this work; it now documents both. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(#3045): enforce the resolved dispatch decision, not the host capability The guard keyed on the registry's dispatch.isolation, which says only that a runtime is CAPABLE of harness worktrees. The decision that actually governs a dispatch is the one the workflow resolves after gating, and that legitimately comes out as sequential in three documented cases: a project setting use_worktrees false, a per-plan submodule intersection, and the base-check auto-degrade. The workflow tells the model to omit the flag in exactly those cases, and the guard was denying every one of them. The third case matters most. The preceding fix made the base-check degrade on git timeouts and a missing git binary, where it had previously answered "safe". That correction is right, and it means a transient hang now degrades to sequential far more often than before -- so the two changes composed into a trap where the workflow behaved exactly as designed and the guard blocked it. The workflow already resolves isolation in shell, deterministically, which is what makes it a trustworthy source in a way the model-authored call is not. It now records that resolved value through a dedicated verb, and both guards read it first. A fresh record is authoritative, so sequential dispatches pass untouched. Absent or stale, the guards fall back to the capability check combined with the project's use_worktrees setting, which still covers the case that never reaches the workflow. Also widened the matcher to accept Task alongside Agent, since a host that names the tool Task would otherwise leave the guard silently inert while implying coverage; stopped assuming Claude when no runtime is declared, which is the shipped default and would have demanded a Claude-only argument elsewhere; and made a non-git project inert rather than denied, since advising a worktree session is not actionable without a repository. The original diagnosis never modeled sequential mode as legitimate. That omission is what let this through, and it is now recorded there. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(#3045): record at resolution and bind the record to its dispatch Two independent reviews converged on the same failure: the guard was fail-open in a default install, so it did not catch the defect it exists to catch. A shipped project carries no runtime key, which made "runtime not confidently known" the common case rather than a corner one. A record asserting that isolation was required but carrying no flag then fell through to a capability lookup that answered "none", and the dispatch was allowed. The flag itself only arrived from a second shell block -- the same block a model dropping the argument would also skip. A test had pinned that behavior as intended. The record is now written by the resolver, as an unavoidable consequence of asking for the value, rather than by a step the model is told in prose to go and run. A guard against a prose-carried value cannot itself depend on prose. Mode, flag and identifiers are written together and atomically, so the flagless window is gone, and a record asserting isolation with no resolvable flag now denies instead of degrading. Runtime is also resolved from the installer's own recorded default, which makes confident resolution the normal case. The per-plan submodule gate degrades after the phase-level decision and never re-recorded, so a plan that legitimately ran sequentially was denied against a still-fresh phase record. It now records its own, scoped to the plan. A record also authorized any dispatch for four hours. One phase degrading to sequential could silently license an unisolated dispatch in the next. Records now carry phase and plan, the guards require them to match, and the window is minutes rather than hours -- the resolver rewrites it before every dispatch, so a long window bought nothing and only widened the hole. The flag validator rejected any value beginning with two dashes, which is exactly the form Cursor and Windsurf declare, so their real value could never have been stored. Writer and reader also derived the record path differently and diverged inside a linked worktree without local planning state. The predictable path remains a way to silence the control without leaving a trace in the diff. It grants no access an agent with shell does not already have, so it is documented as accepted rather than redesigned around. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(#3045): correct the staleness boundary and unmask a vacuous parity test The remote runner returned twenty failures. One was a real production defect the boundary case existed to catch: a record whose age exactly equalled the staleness window was treated as fresh, so it stayed authoritative for one tick past its own expiry. Freshness is now strictly inside the window. The parity test meant to stop the two guards' executor lists from drifting could never have failed. Its project fixture was a bare directory rather than a repository, so the non-git inert branch answered before the executor list was ever consulted. It asserted agreement it never actually measured. The fixture is now a real repository, like every sibling in the file. A test also asserted that Windsurf declares the worktree flag. It does not -- Windsurf resolves to no isolation by design, having no named concurrent dispatch to isolate. The test claimed a registry fact that was never true, and a comment in the resolver repeated it. Both corrected, and the test now proves what it should have all along: that the parser accepts any bare flag value, rather than one runtime's supposed value. The new guard was missing from the bundled-hook whitelist, which is the surface that decides what actually ships, and the per-plan gate had gained calls to the launcher without the preamble those calls require. The changeset carried parenthetical product descriptions the purity rule forbids. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * chore(#3045): backfill changeset pr number Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * test(#3045): make the guard tests hold on Windows Two tests redirect HOME to control where the installer-persisted runtime default is read from. Node resolves the home directory from USERPROFILE on Windows and never consults HOME, so both silently read the real runner profile, found no recorded runtime, and asserted against a project the hook had not recognised. The production code was already correct in asking the platform rather than the variable; only the tests were wrong to assume one variable answers everywhere. The helpers now mirror the override onto both. The symlink spoofing test also created a directory symlink unconditionally, which needs elevated privileges on Windows. It survived on this runner, but it would fail on any host without them, so the creation is now attempted and the test skips explicitly when it cannot be done -- a bare return would have counted as a pass and hidden the gap. Skipping alone would have left the platform uncovered, so the behaviour it proves is now also driven in-process through an injected realpath, following the seam already used for the clock. That case no longer depends on privileges at all, and the end-to-end test keeps its original assertions wherever symlinks work. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
649 lines
29 KiB
JavaScript
649 lines
29 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* gsd-agent-isolation-guard.js — Agent-dispatch isolation guard (#3045)
|
|
*
|
|
* Seam: hooks/gsd-agent-isolation-guard.js (PreToolUse hook, spawned with a
|
|
* JSON payload on stdin, exactly as every runtime bus invokes it).
|
|
*
|
|
* Defect: `gsd-core/workflows/execute-phase/steps/executor-isolation-dispatch.md`
|
|
* resolves dispatch isolation correctly, then relies on PROSE ("substitute
|
|
* $HARNESS_FLAG's value... on Claude Code it is literally isolation=\"worktree\"")
|
|
* to get it into the model-authored `Agent()` call. Nothing verified the
|
|
* substitution happened, so an executor could silently dispatch into the
|
|
* user's primary checkout. This hook enforces the invariant structurally.
|
|
*
|
|
* Matrix source: .gsd/bug/fix-3045-agent-dispatch-isolation-guard/50-test-matrix.md
|
|
* Part 1, rows 1-12. Every row below is annotated with its row number.
|
|
*
|
|
* Two implementation notes that diverge from a literal reading of the design
|
|
* (both intentional, both explained where they're tested):
|
|
*
|
|
* - Rows 8 and 12 ("config unreadable" / "config read times out") collapse
|
|
* to the SAME code path in the real implementation: resolveIsolationState
|
|
* resolves entirely via synchronous, in-process fs reads and require()
|
|
* calls — no subprocess is spawned (the guard prefers reading config
|
|
* directly, per the design's own preference), so there is no literal
|
|
* wall-clock timeout to simulate. Both rows are exercised here via two
|
|
* DIFFERENT real, deterministic, cross-platform-safe failure conditions
|
|
* that both land in the guard's single "cannot verify" catch: row 8 uses
|
|
* `.planning/config.json` being a DIRECTORY (fs.readFileSync → EISDIR),
|
|
* row 12 uses a syntactically invalid config.json (JSON.parse throws).
|
|
* Neither is a chmod/permission trick (CLAUDE.md's cross-platform IO
|
|
* injection rule) — both are real, deterministic file-type/content
|
|
* conditions that behave identically on macOS/Linux/Windows.
|
|
*
|
|
* - Runtime selection for rows 6/7 (orchestrator-worktree / none) uses the
|
|
* real capability-registry.cjs shipped alongside the hook, selected via
|
|
* GSD_RUNTIME (the same precedence resolveIsolationState implements):
|
|
* codex → orchestrator-worktree, windsurf → none. No fixture/mock
|
|
* registry is substituted — this is the real hook reading its real
|
|
* sibling data file, per the "drive the real hook entry point" mandate.
|
|
*/
|
|
|
|
process.env.GSD_TEST_MODE = '1';
|
|
|
|
const { describe, test, before, after } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const { spawnSync } = require('node:child_process');
|
|
const fc = require('./helpers/fast-check-setup.cjs');
|
|
const { createTempDir, cleanup } = require('./helpers.cjs');
|
|
const { SENTINEL_RELATIVE_PATH, SENTINEL_STALE_MS } = require('../hooks/lib/isolation-sentinel.js');
|
|
|
|
const HOOK_PATH = path.join(__dirname, '..', 'hooks', 'gsd-agent-isolation-guard.js');
|
|
|
|
/**
|
|
* Write a #3045 dispatch-isolation sentinel under `dir` (mirrors what
|
|
* `gsd-tools.cjs record-dispatch-isolation` writes). `writtenAt` defaults to
|
|
* "now" (fresh); pass an explicit past timestamp to construct a stale one.
|
|
*/
|
|
function writeSentinel(dir, { isolation, harnessFlag = null, phase = null, plan = null, writtenAt = Date.now() }) {
|
|
const p = path.join(dir, SENTINEL_RELATIVE_PATH);
|
|
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
fs.writeFileSync(p, JSON.stringify({ isolation, harness_flag: harnessFlag, phase, plan, written_at: writtenAt }));
|
|
}
|
|
|
|
/**
|
|
* Run the hook with a given payload against a given cwd.
|
|
* GSD_RUNTIME is deleted by default so ambient environment can never leak a
|
|
* runtime override into a test that expects the config.json `runtime` key
|
|
* (or the 'claude' default) to be used instead.
|
|
*/
|
|
function runHook(payload, cwd, extraEnv = {}) {
|
|
const env = { ...process.env };
|
|
delete env.GSD_RUNTIME;
|
|
Object.assign(env, extraEnv);
|
|
// Production code resolves the home directory via `os.homedir()` (correct,
|
|
// cross-platform), which on Windows reads `USERPROFILE`, not `HOME` —
|
|
// `os.homedir()` never honors `HOME` there. Tests below override `HOME` to
|
|
// redirect `os.homedir()` hermetically; mirror the override onto
|
|
// `USERPROFILE` too so that redirection actually takes effect on Windows
|
|
// instead of silently leaking the real CI runner's profile directory.
|
|
if ('HOME' in extraEnv) env.USERPROFILE = extraEnv.HOME;
|
|
return spawnSync(process.execPath, [HOOK_PATH], {
|
|
input: typeof payload === 'string' ? payload : JSON.stringify(payload),
|
|
encoding: 'utf8',
|
|
cwd,
|
|
env,
|
|
});
|
|
}
|
|
|
|
function agentPayload(overrides = {}) {
|
|
return {
|
|
hook_event_name: 'PreToolUse',
|
|
tool_name: 'Agent',
|
|
tool_input: { subagent_type: 'gsd-executor', ...(overrides.tool_input || {}) },
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function mkProject(prefix) {
|
|
const dir = createTempDir(prefix);
|
|
fs.mkdirSync(path.join(dir, '.planning'), { recursive: true });
|
|
return dir;
|
|
}
|
|
|
|
function writeConfig(dir, content) {
|
|
fs.writeFileSync(path.join(dir, '.planning', 'config.json'), content);
|
|
}
|
|
|
|
describe('gsd-agent-isolation-guard.js: applicability matrix (#3045)', () => {
|
|
let harnessProject; // GSD project resolving to harness-worktree (claude)
|
|
let orchestratorProject; // resolves to orchestrator-worktree (codex)
|
|
let noneProject; // resolves to none (windsurf)
|
|
let noGsdProject; // not a GSD project at all
|
|
let unreadableConfigProject; // config.json is a directory (EISDIR)
|
|
let corruptConfigProject; // config.json is invalid JSON
|
|
|
|
before(() => {
|
|
harnessProject = mkProject('gsd-aig-harness-');
|
|
writeConfig(harnessProject, JSON.stringify({ runtime: 'claude' }));
|
|
|
|
orchestratorProject = mkProject('gsd-aig-orch-');
|
|
writeConfig(orchestratorProject, JSON.stringify({}));
|
|
|
|
noneProject = mkProject('gsd-aig-none-');
|
|
writeConfig(noneProject, JSON.stringify({}));
|
|
|
|
noGsdProject = createTempDir('gsd-aig-nogsd-');
|
|
|
|
unreadableConfigProject = mkProject('gsd-aig-unreadable-');
|
|
// #3050 lesson: force a genuine, cross-platform-safe read failure by
|
|
// making the config path a DIRECTORY instead of a file — fs.readFileSync
|
|
// throws EISDIR deterministically on macOS/Linux/Windows. NOT a
|
|
// chmod/permission trick (CLAUDE.md's IO-failure-injection rule).
|
|
// eslint-disable-next-line local/no-raw-rmsync-in-tests -- removing a single fixture FILE (not a temp dir teardown) to replace it with a directory; helpers.cleanup() tears down whole temp dirs and isn't the right tool here
|
|
fs.rmSync(path.join(unreadableConfigProject, '.planning', 'config.json'), { force: true });
|
|
fs.mkdirSync(path.join(unreadableConfigProject, '.planning', 'config.json'));
|
|
|
|
corruptConfigProject = mkProject('gsd-aig-corrupt-');
|
|
writeConfig(corruptConfigProject, '{ this is not valid json');
|
|
});
|
|
|
|
after(() => {
|
|
cleanup(harnessProject);
|
|
cleanup(orchestratorProject);
|
|
cleanup(noneProject);
|
|
cleanup(noGsdProject);
|
|
cleanup(unreadableConfigProject);
|
|
cleanup(corruptConfigProject);
|
|
});
|
|
|
|
test('row 1: absent isolation param, harness-worktree, GSD project -> DENY', () => {
|
|
const r = runHook(agentPayload(), harnessProject);
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
const out = JSON.parse(r.stdout);
|
|
assert.equal(out.decision, 'block');
|
|
assert.match(out.reason, /harness-worktree/);
|
|
assert.match(out.reason, /isolation="worktree"/);
|
|
assert.equal(r.stderr, out.reason, 'stderr must carry the same reason (Kimi reads stderr on exit 2)');
|
|
});
|
|
|
|
test('row 2: isolation="worktree" present -> allow', () => {
|
|
const r = runHook(agentPayload({ tool_input: { subagent_type: 'gsd-executor', isolation: 'worktree' } }), harnessProject);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout}`);
|
|
assert.equal(r.stdout, '');
|
|
});
|
|
|
|
test('row 3: isolation="" (empty) -> DENY', () => {
|
|
const r = runHook(agentPayload({ tool_input: { subagent_type: 'gsd-executor', isolation: '' } }), harnessProject);
|
|
assert.equal(r.status, 2);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
});
|
|
|
|
test('row 4: isolation="none" -> DENY', () => {
|
|
const r = runHook(agentPayload({ tool_input: { subagent_type: 'gsd-executor', isolation: 'none' } }), harnessProject);
|
|
assert.equal(r.status, 2);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
});
|
|
|
|
test('row 5: subagent_type=gsd-code-reviewer (not an executor) -> allow', () => {
|
|
const r = runHook(agentPayload({ tool_input: { subagent_type: 'gsd-code-reviewer' } }), harnessProject);
|
|
assert.equal(r.status, 0);
|
|
assert.equal(r.stdout, '');
|
|
});
|
|
|
|
test('row 6: resolved mode orchestrator-worktree -> allow (different path)', () => {
|
|
const r = runHook(agentPayload(), orchestratorProject, { GSD_RUNTIME: 'codex' });
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout}`);
|
|
assert.equal(r.stdout, '');
|
|
});
|
|
|
|
test('row 7: resolved mode none -> allow', () => {
|
|
const r = runHook(agentPayload(), noneProject, { GSD_RUNTIME: 'windsurf' });
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout}`);
|
|
assert.equal(r.stdout, '');
|
|
});
|
|
|
|
test('row 8: config unreadable (EISDIR) + GSD project present -> DENY, distinct reason', () => {
|
|
const r = runHook(agentPayload(), unreadableConfigProject);
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
const out = JSON.parse(r.stdout);
|
|
assert.equal(out.decision, 'block');
|
|
assert.match(out.reason, /could not read or resolve/i);
|
|
assert.match(out.reason, /#3050/);
|
|
});
|
|
|
|
test('row 9: no GSD project (.planning/config.json absent) -> allow, inert', () => {
|
|
const r = runHook(agentPayload(), noGsdProject);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout}`);
|
|
assert.equal(r.stdout, '');
|
|
});
|
|
|
|
test('row 10: wrong tool (Bash) -> allow', () => {
|
|
const r = runHook({ hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command: 'echo hi' } }, harnessProject);
|
|
assert.equal(r.status, 0);
|
|
assert.equal(r.stdout, '');
|
|
});
|
|
|
|
test('row 11a: subagent_type absent -> allow, must not throw', () => {
|
|
const r = runHook(agentPayload({ tool_input: {} }), harnessProject);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(r.stderr, '', 'must not crash or log a stack trace');
|
|
});
|
|
|
|
test('row 11b: subagent_type malformed (non-string, e.g. array) -> allow, must not throw', () => {
|
|
const r = runHook(agentPayload({ tool_input: { subagent_type: ['gsd-executor'] } }), harnessProject);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(r.stderr, '');
|
|
});
|
|
|
|
test('row 11c: tool_input entirely absent -> allow, must not throw', () => {
|
|
const r = runHook({ hook_event_name: 'PreToolUse', tool_name: 'Agent' }, harnessProject);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
});
|
|
|
|
test('row 11d: payload is not JSON at all -> allow, must not throw', () => {
|
|
const r = runHook('not json {{{', harnessProject);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
});
|
|
|
|
test('row 11e: payload is JSON null -> allow, must not throw', () => {
|
|
const r = runHook('null', harnessProject);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
});
|
|
|
|
test('row 12: config read fails via corrupt JSON (stands in for "times out" — see file header) -> DENY', () => {
|
|
const r = runHook(agentPayload(), corruptConfigProject);
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
const out = JSON.parse(r.stdout);
|
|
assert.equal(out.decision, 'block');
|
|
assert.match(out.reason, /could not read or resolve/i);
|
|
});
|
|
|
|
test('reason names the exact parameter to add (self-correction requirement)', () => {
|
|
const r = runHook(agentPayload(), harnessProject);
|
|
assert.equal(r.status, 2);
|
|
const out = JSON.parse(r.stdout);
|
|
assert.match(out.reason, /Add isolation="worktree" to the Agent\(\) call/);
|
|
});
|
|
});
|
|
|
|
describe('gsd-agent-isolation-guard.js: property — deny iff isolation param != "worktree" (harness-worktree project)', () => {
|
|
let harnessProject;
|
|
|
|
before(() => {
|
|
harnessProject = mkProject('gsd-aig-prop-');
|
|
writeConfig(harnessProject, JSON.stringify({ runtime: 'claude' }));
|
|
});
|
|
|
|
after(() => {
|
|
cleanup(harnessProject);
|
|
});
|
|
|
|
test('for any string value, dispatch is blocked unless the value is exactly "worktree"', () => {
|
|
fc.assert(
|
|
fc.property(
|
|
fc.string(),
|
|
(isolationValue) => {
|
|
const r = runHook(
|
|
agentPayload({ tool_input: { subagent_type: 'gsd-executor', isolation: isolationValue } }),
|
|
harnessProject
|
|
);
|
|
const expectBlocked = isolationValue !== 'worktree';
|
|
const actualBlocked = r.status === 2;
|
|
assert.equal(
|
|
actualBlocked, expectBlocked,
|
|
`isolation=${JSON.stringify(isolationValue)} expected ${expectBlocked ? 'blocked' : 'allowed'}, got status ${r.status}, stdout: ${r.stdout}`
|
|
);
|
|
}
|
|
),
|
|
{ numRuns: 30 } // each sample spawns the hook process — bound the cost
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('gsd-agent-isolation-guard.js: #3045 BLOCKER regression — sentinel is authoritative over registry capability', () => {
|
|
// These rows pin the exact defect the isolated code review flagged as a
|
|
// BLOCKER: the guard used to key enforcement on the REGISTRY's
|
|
// dispatch.isolation (a host CAPABILITY — "this host CAN isolate"), not
|
|
// the workflow's resolved per-dispatch ISOLATION ("this dispatch SHOULD be
|
|
// isolated"). Sequential ISOLATION=none legitimately happens even on a
|
|
// harness-worktree-capable host. Every row below uses `harnessProject`
|
|
// (registry resolves to harness-worktree for runtime 'claude') so a FAIL
|
|
// here proves the sentinel is actually consulted, not merely coincidental
|
|
// with what the registry alone would already allow.
|
|
let harnessProject;
|
|
let useWorktreesFalseProject;
|
|
|
|
before(() => {
|
|
harnessProject = mkProject('gsd-aig-sentinel-');
|
|
writeConfig(harnessProject, JSON.stringify({ runtime: 'claude' }));
|
|
|
|
useWorktreesFalseProject = mkProject('gsd-aig-uwf-');
|
|
writeConfig(useWorktreesFalseProject, JSON.stringify({ runtime: 'claude', workflow: { use_worktrees: false } }));
|
|
});
|
|
|
|
after(() => {
|
|
cleanup(harnessProject);
|
|
cleanup(useWorktreesFalseProject);
|
|
});
|
|
|
|
test('sentinel says isolation=none -> ALLOW even though registry resolves harness-worktree (the BLOCKER)', () => {
|
|
writeSentinel(harnessProject, { isolation: 'none' });
|
|
try {
|
|
const r = runHook(agentPayload(), harnessProject); // no isolation param on the dispatch
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(r.stdout, '');
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('sentinel says isolation=orchestrator-worktree -> ALLOW', () => {
|
|
writeSentinel(harnessProject, { isolation: 'orchestrator-worktree' });
|
|
try {
|
|
const r = runHook(agentPayload(), harnessProject);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(r.stdout, '');
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('sentinel says isolation=harness-worktree + dispatch missing the flag -> DENY', () => {
|
|
writeSentinel(harnessProject, { isolation: 'harness-worktree', harnessFlag: 'isolation="worktree"' });
|
|
try {
|
|
const r = runHook(agentPayload(), harnessProject);
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('sentinel says isolation=harness-worktree + dispatch carries the flag -> ALLOW', () => {
|
|
writeSentinel(harnessProject, { isolation: 'harness-worktree', harnessFlag: 'isolation="worktree"' });
|
|
try {
|
|
const r = runHook(
|
|
agentPayload({ tool_input: { subagent_type: 'gsd-executor', isolation: 'worktree' } }),
|
|
harnessProject
|
|
);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('STALE sentinel (older than SENTINEL_STALE_MS) is ignored -> falls back to registry (DENY, harness-worktree still applies)', () => {
|
|
// The stale sentinel LIES (says none) — proving the fallback re-derives
|
|
// from the registry instead of trusting it is exactly the point.
|
|
writeSentinel(harnessProject, { isolation: 'none', writtenAt: Date.now() - (SENTINEL_STALE_MS + 60000) });
|
|
try {
|
|
const r = runHook(agentPayload(), harnessProject);
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('MALFORMED sentinel (invalid JSON) is treated as stale, never fatal -> falls back to registry (DENY)', () => {
|
|
const sentinelPath = path.join(harnessProject, SENTINEL_RELATIVE_PATH);
|
|
fs.mkdirSync(path.dirname(sentinelPath), { recursive: true });
|
|
fs.writeFileSync(sentinelPath, '{ this is not valid json');
|
|
try {
|
|
const r = runHook(agentPayload(), harnessProject);
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
assert.equal(r.stderr.length > 0, true, 'must not crash — a clean block reason, not a stack trace');
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('no sentinel + workflow.use_worktrees=false -> ALLOW (project-level opt-out, case (a) from the BLOCKER)', () => {
|
|
const r = runHook(agentPayload(), useWorktreesFalseProject);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(r.stdout, '');
|
|
});
|
|
|
|
test('no sentinel + workflow.use_worktrees absent + registry harness-worktree -> DENY (conservative fallback still enforces)', () => {
|
|
const r = runHook(agentPayload(), harnessProject);
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
});
|
|
|
|
test('tool_name="Task" behaves identically to "Agent" (#3045 MAJOR 1)', () => {
|
|
const r = runHook(
|
|
{ hook_event_name: 'PreToolUse', tool_name: 'Task', tool_input: { subagent_type: 'gsd-executor' } },
|
|
harnessProject
|
|
);
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
});
|
|
|
|
test('tool_name="Task" with isolation="worktree" present -> allow, same as "Agent"', () => {
|
|
const r = runHook(
|
|
{ hook_event_name: 'PreToolUse', tool_name: 'Task', tool_input: { subagent_type: 'gsd-executor', isolation: 'worktree' } },
|
|
harnessProject
|
|
);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout}`);
|
|
});
|
|
});
|
|
|
|
describe('gsd-agent-isolation-guard.js: #3045 MAJOR 2 — undeterminable runtime does not demand the Claude kwarg', () => {
|
|
let unconfiguredProject;
|
|
|
|
before(() => {
|
|
unconfiguredProject = mkProject('gsd-aig-unconfigured-');
|
|
// No `runtime` key at all — mirrors gsd-core/templates/config.json,
|
|
// which ships every new project's scaffold WITHOUT one. GSD_RUNTIME is
|
|
// deleted by runHook(), so this project has NO explicit runtime signal.
|
|
writeConfig(unconfiguredProject, JSON.stringify({}));
|
|
});
|
|
|
|
after(() => {
|
|
cleanup(unconfiguredProject);
|
|
});
|
|
|
|
test('no GSD_RUNTIME override, no config.json runtime key, no ~/.gsd/defaults.json runtime -> ALLOW (cannot-determine degrades to inert, not a guessed "claude" demand)', () => {
|
|
// #3045 BLOCKER 2 fix: resolveRuntimeIdentity now ALSO reads
|
|
// ~/.gsd/defaults.json as a confidence signal. That makes this test's
|
|
// outcome environment-dependent unless HOME is pinned to a directory with
|
|
// no defaults.json (the project fixture dir itself has none) — otherwise
|
|
// a developer machine that ever installed GSD for a non-Claude runtime
|
|
// would have ~/.gsd/defaults.json's real `runtime` leak in here and
|
|
// silently flip this test's expectation depending on who runs it.
|
|
const r = runHook(agentPayload(), unconfiguredProject, { HOME: unconfiguredProject });
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(r.stdout, '');
|
|
});
|
|
});
|
|
|
|
describe('gsd-agent-isolation-guard.js: #3045 BLOCKER 2 — default-install fail-open (isolated two-review finding)', () => {
|
|
let harnessProject; // registry resolves harness-worktree (claude), no defaults.json runtime
|
|
let unconfiguredHarnessProject; // same, but with NO explicit runtime signal at all
|
|
|
|
before(() => {
|
|
harnessProject = mkProject('gsd-aig-b2-harness-');
|
|
writeConfig(harnessProject, JSON.stringify({ runtime: 'claude' }));
|
|
|
|
unconfiguredHarnessProject = mkProject('gsd-aig-b2-unconfigured-');
|
|
// Mirrors gsd-core/templates/config.json exactly: no `runtime` key.
|
|
writeConfig(unconfiguredHarnessProject, JSON.stringify({}));
|
|
});
|
|
|
|
after(() => {
|
|
cleanup(harnessProject);
|
|
cleanup(unconfiguredHarnessProject);
|
|
});
|
|
|
|
test('part A: fresh sentinel confirms harness-worktree but carries NO harness_flag, and the runtime is not confidently resolvable -> DENY (was ALLOW pre-fix)', () => {
|
|
// This is the exact BLOCKER 2 regression: previously this branch fell
|
|
// through to the "not confident -> none" degrade and ALLOWED the
|
|
// dispatch to run unisolated, on the DEFAULT-INSTALL path (no `runtime`
|
|
// key in config.json — gsd-core/templates/config.json's shipped shape —
|
|
// and no ~/.gsd/defaults.json runtime either).
|
|
writeSentinel(unconfiguredHarnessProject, { isolation: 'harness-worktree', harnessFlag: null });
|
|
try {
|
|
const r = runHook(agentPayload(), unconfiguredHarnessProject, { HOME: unconfiguredHarnessProject });
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr} — must DENY, not silently allow`);
|
|
const out = JSON.parse(r.stdout);
|
|
assert.equal(out.decision, 'block');
|
|
assert.match(out.reason, /cannot verify|harness_flag/i);
|
|
} finally {
|
|
cleanup(path.join(unconfiguredHarnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('part B: ~/.gsd/defaults.json runtime (installer-persisted, #2395) is now a confident signal — makes the default install enforce', () => {
|
|
// No sentinel at all here — pure conservative-fallback path. Before this
|
|
// fix, an unconfigured project (no config.json runtime key, the COMMON
|
|
// scaffold shape) always fell back to 'none'/allow regardless of what the
|
|
// machine actually has installed. After the fix, a `runtime` persisted to
|
|
// ~/.gsd/defaults.json (which bin/install.js's writeNonClaudeDefaults
|
|
// already writes for every non-Claude install) is read as confidently as
|
|
// GSD_RUNTIME or config.json's own key.
|
|
const home = mkProject('gsd-aig-b2-home-');
|
|
try {
|
|
fs.mkdirSync(path.join(home, '.gsd'), { recursive: true });
|
|
fs.writeFileSync(path.join(home, '.gsd', 'defaults.json'), JSON.stringify({ runtime: 'claude' }));
|
|
|
|
const r = runHook(agentPayload(), unconfiguredHarnessProject, { HOME: home });
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr} — defaults.json runtime must be enforced`);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
} finally {
|
|
cleanup(home);
|
|
}
|
|
});
|
|
|
|
test('part B (negative control): a project WITH its own config.json runtime key still wins over defaults.json', () => {
|
|
const home = mkProject('gsd-aig-b2-home2-');
|
|
try {
|
|
fs.mkdirSync(path.join(home, '.gsd'), { recursive: true });
|
|
// defaults.json says a runtime with NO harness-worktree capability;
|
|
// config.json's own `runtime: claude` must take precedence.
|
|
fs.writeFileSync(path.join(home, '.gsd', 'defaults.json'), JSON.stringify({ runtime: 'windsurf' }));
|
|
|
|
const r = runHook(agentPayload(), harnessProject, { HOME: home });
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
} finally {
|
|
cleanup(home);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('gsd-agent-isolation-guard.js: #3045 SECURITY F2 — sentinel bound to phase/plan, mismatch is "no applicable sentinel"', () => {
|
|
let harnessProject;
|
|
|
|
before(() => {
|
|
harnessProject = mkProject('gsd-aig-f2-');
|
|
writeConfig(harnessProject, JSON.stringify({ runtime: 'claude' }));
|
|
});
|
|
|
|
after(() => {
|
|
cleanup(harnessProject);
|
|
});
|
|
|
|
test('a fresh "none" sentinel for a DIFFERENT phase than this dispatch is not applied — falls through to conservative fallback and DENIES', () => {
|
|
// Sentinel legitimately recorded 'none' for phase 1 (e.g. a submodule
|
|
// degrade). This dispatch's own description names phase 2 — the guard
|
|
// must not reuse phase 1's stale-but-fresh "none" to authorize it.
|
|
writeSentinel(harnessProject, { isolation: 'none', phase: '1', plan: 'plan-a' });
|
|
try {
|
|
const r = runHook(
|
|
agentPayload({ tool_input: { subagent_type: 'gsd-executor', description: 'Execute plan plan-b of phase 2' } }),
|
|
harnessProject,
|
|
);
|
|
assert.equal(r.status, 2, `stdout: ${r.stdout} stderr: ${r.stderr} — mismatched sentinel must not silently allow`);
|
|
assert.equal(JSON.parse(r.stdout).decision, 'block');
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('a fresh sentinel for the SAME phase/plan as this dispatch is applied normally (positive control)', () => {
|
|
writeSentinel(harnessProject, { isolation: 'none', phase: '2', plan: 'plan-b' });
|
|
try {
|
|
const r = runHook(
|
|
agentPayload({ tool_input: { subagent_type: 'gsd-executor', description: 'Execute plan plan-b of phase 2' } }),
|
|
harnessProject,
|
|
);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('a dispatch whose description does not match the expected shape does not itself trigger a mismatch (best-effort extraction)', () => {
|
|
writeSentinel(harnessProject, { isolation: 'none', phase: '2', plan: 'plan-b' });
|
|
try {
|
|
const r = runHook(
|
|
agentPayload({ tool_input: { subagent_type: 'gsd-executor', description: 'some other free-form text' } }),
|
|
harnessProject,
|
|
);
|
|
assert.equal(r.status, 0, `stdout: ${r.stdout} stderr: ${r.stderr}`);
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('gsd-agent-isolation-guard.js: #3045 MAJOR — clock seam boundary coverage (in-process, no subprocess wall-clock race)', () => {
|
|
const guardModule = require('../hooks/gsd-agent-isolation-guard.js');
|
|
|
|
let harnessProject;
|
|
let savedGsdRuntime;
|
|
|
|
before(() => {
|
|
harnessProject = mkProject('gsd-aig-clock-');
|
|
writeConfig(harnessProject, JSON.stringify({ runtime: 'claude' }));
|
|
// These tests call resolveIsolationState() directly, in-process (not via
|
|
// runHook's subprocess, which already strips GSD_RUNTIME) — guard against
|
|
// ambient env leakage from the CURRENT test process (repo hermeticity
|
|
// rule: an ambient GSD_ env var must never redirect a test's outcome).
|
|
savedGsdRuntime = process.env.GSD_RUNTIME;
|
|
delete process.env.GSD_RUNTIME;
|
|
});
|
|
|
|
after(() => {
|
|
cleanup(harnessProject);
|
|
if (savedGsdRuntime === undefined) delete process.env.GSD_RUNTIME;
|
|
else process.env.GSD_RUNTIME = savedGsdRuntime;
|
|
});
|
|
|
|
function fixedClock(nowMs) {
|
|
return { now: () => nowMs };
|
|
}
|
|
|
|
test('sentinel exactly at SENTINEL_STALE_MS - 1 is still FRESH (trusted)', () => {
|
|
const writtenAt = 1_000_000;
|
|
writeSentinel(harnessProject, { isolation: 'none', writtenAt });
|
|
try {
|
|
const state = guardModule.resolveIsolationState(harnessProject, { clock: fixedClock(writtenAt + SENTINEL_STALE_MS - 1) });
|
|
assert.equal(state.isolation, 'none', 'still within the trust window — must use the sentinel, not the registry fallback');
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('sentinel exactly AT SENTINEL_STALE_MS is STALE (age > threshold is the only fresh condition)', () => {
|
|
const writtenAt = 1_000_000;
|
|
writeSentinel(harnessProject, { isolation: 'none', writtenAt });
|
|
try {
|
|
const state = guardModule.resolveIsolationState(harnessProject, { clock: fixedClock(writtenAt + SENTINEL_STALE_MS) });
|
|
// Registry fallback for this project resolves harness-worktree (claude,
|
|
// no workflow.use_worktrees:false) — proves the sentinel's 'none' was
|
|
// NOT trusted at exactly the boundary.
|
|
assert.equal(state.isolation, 'harness-worktree');
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
|
|
test('sentinel at SENTINEL_STALE_MS + 1 is STALE', () => {
|
|
const writtenAt = 1_000_000;
|
|
writeSentinel(harnessProject, { isolation: 'none', writtenAt });
|
|
try {
|
|
const state = guardModule.resolveIsolationState(harnessProject, { clock: fixedClock(writtenAt + SENTINEL_STALE_MS + 1) });
|
|
assert.equal(state.isolation, 'harness-worktree');
|
|
} finally {
|
|
cleanup(path.join(harnessProject, '.gsd'));
|
|
}
|
|
});
|
|
});
|