The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
Merge upstream/main into feat/gateguard-fewer-round-trips
montjeffrey92947a68dfb9
diff --cc hooks/hooks.metadata.jsonindex 9760d32da,e598a823e..829151763--- a/hooks/hooks.metadata.json+++ b/hooks/hooks.metadata.json@@@ -44,8 -44,20 +44,20 @@@},{"id": "pre:edit-write:gateguard-fact-force",- "description": "Fact-forcing gate: block first Edit/Write/MultiEdit per file and demand investigation (importers, data schemas, user instruction) before allowing",- "fingerprint": "32c4a312b4c2"+ "description": "Fact-forcing gate: deny the first Edit/Write/MultiEdit/NotebookEdit of each file with questions chosen by target class and by what the change touches; a completed search naming the file in the current turn, a comment or whitespace-only edit, or a same-turn sibling of a new file already gated passes with a note instead; sensitive targets (secrets, keys, auth/payment code, migrations, CI workflows) and hard-linked files are always gated, also in subagents",+ "fingerprint": "45fbef2d7f27"+ },+ {+ "id": "pre:hookify-runtime",+ "description": "Evaluate project-local Hookify rules before Bash, PowerShell, and file tools run",+ "fingerprint": "65b5a5d6bbd5"+ }+ ],+ "UserPromptSubmit": [+ {+ "id": "prompt:hookify-runtime",+ "description": "Evaluate project-local Hookify prompt rules before a user prompt is processed",+ "fingerprint": "89b4809a5a7e"}],"PreCompact": [diff --cc manifests/context-packs/[email protected]index 95e9e37f8,bd5dd19ec..51d8fa993--- a/manifests/context-packs/[email protected]+++ b/manifests/context-packs/[email protected]@@@ -1,288 -1,1 +1,1 @@@- {- "coverage": {- "skills": 293,- "withTriggers": 32- },- "generatedAt": "2026-09-24T23:51:22.784Z",- "id": "skill-triggers@1",- "model": {- "effort": null,- "id": "hand-seeded",- "source": "manual-curation-pending-regeneration"- },- "registryDigest": "369bc6c55fae8081fe66b125ddb9ee9a8e377c8266975af1200c0be2d4984311",- "schemaVersion": 1,- "triggers": {- "skill:api-connector-builder": [- "add api integration",- "new provider connector",- "match existing integration pattern"- ],- "skill:api-design": [- "rest endpoint design",- "pagination api",- "status codes",- "api versioning",- "rate limiting api",- "resource naming",- "filtering api",- "api error responses",- "offset pagination",- "limit query parameter",- "pagination defaults"- ],- "skill:backend-patterns": [- "express api",- "node backend architecture",- "nextjs api routes",- "server side patterns",- "data access layer",- "static file server",- "url path handling",- "file server"- ],- "skill:browser-qa": [- "deployed feature test",- "visual regression screenshots",- "core web vitals check",- "axe accessibility audit",- "ship do not ship",- "staging verification"- ],- "skill:canary-watch": [- "post deploy monitoring",- "smoke test url",- "production url check",- "console errors production",- "sse stream check",- "after deploy verification"- ],- "skill:code-tour": [- "onboarding walkthrough",- "explain subsystem",- "architecture tour",- "pr walkthrough",- "rca tour"- ],- "skill:coding-standards": [- "code review standards",- "naming conventions",- "readability review",- "immutability conventions",- "fix naming typo",- "export naming",- "consistent exports"- ],- "skill:content-hash-cache-pattern": [- "cache file processing",- "content addressed cache",- "sha256 hash cache"- ],- "skill:database-migrations": [- "zero downtime migration",- "schema change production",- "add column large table",- "backfill data",- "expand contract",- "concurrent index",- "migration rollback",- "prisma migration",- "django migration"- ],- "skill:deployment-patterns": [- "ci cd setup",- "dockerize app",- "health checks",- "rollback strategy",- "production readiness",- "deploy pipeline",- "containerize application"- ],- "skill:design-system": [- "design tokens",- "visual consistency audit",- "css custom properties",- "ui audit",- "design system bootstrap"- ],- "skill:django-patterns": [- "django orm",- "drf api",- "django rest framework",- "django caching",- "django signals",- "django middleware"- ],- "skill:django-security": [- "django authentication",- "csrf protection",- "sql injection prevention",- "xss prevention",- "django deployment security",- "role based access control",- "authorization middleware",- "permissions checks"- ],- "skill:docker-patterns": [- "dockerfile review",- "docker compose setup",- "container security",- "multi service orchestration"- ],- "skill:error-handling": [- "error types",- "retry logic",- "circuit breaker",- "user facing errors",- "exception handling patterns",- "typed errors",- "error boundaries",- "go error handling",- "custom error class",- "error codes",- "config validation"- ],- "skill:evm-token-decimals": [- "token decimals",- "wei conversion",- "erc20 balance off",- "bridge token precision"- ],- "skill:frontend-a11y": [- "aria attributes",- "screen reader support",- "focus management",- "semantic html",- "form labeling",- "keyboard navigation react",- "a11y lint errors"- ],- "skill:git-workflow": [- "merge vs rebase",- "commit conventions",- "resolve merge conflict",- "branching strategy",- "clean up commits",- "pull request cleanup",- "git history tidy"- ],- "skill:hexagonal-architecture": [- "ports and adapters",- "dependency injection boundaries",- "decouple domain from io"- ],- "skill:kubernetes-patterns": [- "kubernetes manifests",- "kubectl debugging",- "pod probes",- "k8s rbac",- "autoscaling config",- "configmap secrets"- ],- "skill:orch-fix-defect": [- "fix a bug",- "broken behavior",- "regression fix",- "reproduce bug",- "defect repair"- ],- "skill:postgres-patterns": [- "slow postgres query",- "query optimization",- "index design",- "rls policies",- "supabase schema",- "postgres indexing",- "database performance",- "schema design postgres",- "postgres driver",- "node postgres",- "query planner"- ],- "skill:python-patterns": [- "pythonic code",- "pep 8",- "type hints python",- "python code review",- "idiomatic python"- ],- "skill:python-testing": [- "pytest fixtures",- "mocking python",- "parametrized tests",- "coverage python",- "tdd python"- ],- "skill:redis-patterns": [- "cache aside pattern",- "distributed lock",- "redis rate limiting",- "cache invalidation"- ],- "skill:regex-vs-llm-structured-text": [- "parse invoice",- "extract receipt data",- "text extraction pipeline",- "parse form fields",- "cheap document parser",- "extract table data",- "parse log lines",- "parse access logs",- "common log format",- "log line parsing"- ],- "skill:rust-patterns": [- "rust ownership",- "borrow checker",- "rust error handling",- "traits rust",- "rust concurrency",- "idiomatic rust"- ],- "skill:search-first": [- "find existing library",- "npm package research",- "before writing custom code",- "evaluate existing tools",- "add dependency research"- ],- "skill:security-review": [- "security audit",- "authentication review",- "sanitize user input",- "secrets handling",- "payment security checklist",- "prevent injection attacks",- "secure api endpoints",- "authn authz review",- "vulnerability checklist",- "input validation security",- "parameterized queries",- "sql injection"- ],- "skill:security-scan": [- "audit claude config",- "claudemd security",- "mcp server audit",- "agentshield scan",- "hook configuration audit",- "settings json security"- ],- "skill:tdd-workflow": [- "write test first",- "failing test",- "red green refactor",- "test driven development",- "regression test first",- "write a regression test"- ],- "skill:verification-loop": [- "pre pr checks",- "verification report",- "quality gates",- "build lint test coverage",- "before creating a pr"- ]- },- "triggersDigest": "25b97a9e06fc336c7cf95ab854ed1a41033a54bcd6e1fb1cf69dc906332462aa"- }-{"coverage":{"skills":302,"withTriggers":32},"generatedAt":"2026-09-24T23:51:22.784Z","id":"skill-triggers@1","model":{"effort":null,"id":"hand-seeded","source":"manual-curation-pending-regeneration"},"registryDigest":"ffac257ab8a4b8e7450ddf1f933e65335542fc444034c8242dd758346417d76e","schemaVersion":1,"triggers":{"skill:api-connector-builder":["add api integration","new provider connector","match existing integration pattern"],"skill:api-design":["rest endpoint design","pagination api","status codes","api versioning","rate limiting api","resource naming","filtering api","api error responses","offset pagination","limit query parameter","pagination defaults"],"skill:backend-patterns":["express api","node backend architecture","nextjs api routes","server side patterns","data access layer","static file server","url path handling","file server"],"skill:browser-qa":["deployed feature test","visual regression screenshots","core web vitals check","axe accessibility audit","ship do not ship","staging verification"],"skill:canary-watch":["post deploy monitoring","smoke test url","production url check","console errors production","sse stream check","after deploy verification"],"skill:code-tour":["onboarding walkthrough","explain subsystem","architecture tour","pr walkthrough","rca tour"],"skill:coding-standards":["code review standards","naming conventions","readability review","immutability conventions","fix naming typo","export naming","consistent exports"],"skill:content-hash-cache-pattern":["cache file processing","content addressed cache","sha256 hash cache"],"skill:database-migrations":["zero downtime migration","schema change production","add column large table","backfill data","expand contract","concurrent index","migration rollback","prisma migration","django migration"],"skill:deployment-patterns":["ci cd setup","dockerize app","health checks","rollback strategy","production readiness","deploy pipeline","containerize application"],"skill:design-system":["design tokens","visual consistency audit","css custom properties","ui audit","design system bootstrap"],"skill:django-patterns":["django orm","drf api","django rest framework","django caching","django signals","django middleware"],"skill:django-security":["django authentication","csrf protection","sql injection prevention","xss prevention","django deployment security","role based access control","authorization middleware","permissions checks"],"skill:docker-patterns":["dockerfile review","docker compose setup","container security","multi service orchestration"],"skill:error-handling":["error types","retry logic","circuit breaker","user facing errors","exception handling patterns","typed errors","error boundaries","go error handling","custom error class","error codes","config validation"],"skill:evm-token-decimals":["token decimals","wei conversion","erc20 balance off","bridge token precision"],"skill:frontend-a11y":["aria attributes","screen reader support","focus management","semantic html","form labeling","keyboard navigation react","a11y lint errors"],"skill:git-workflow":["merge vs rebase","commit conventions","resolve merge conflict","branching strategy","clean up commits","pull request cleanup","git history tidy"],"skill:hexagonal-architecture":["ports and adapters","dependency injection boundaries","decouple domain from io"],"skill:kubernetes-patterns":["kubernetes manifests","kubectl debugging","pod probes","k8s rbac","autoscaling config","configmap secrets"],"skill:orch-fix-defect":["fix a bug","broken behavior","regression fix","reproduce bug","defect repair"],"skill:postgres-patterns":["slow postgres query","query optimization","index design","rls policies","supabase schema","postgres indexing","database performance","schema design postgres","postgres driver","node postgres","query planner"],"skill:python-patterns":["pythonic code","pep 8","type hints python","python code review","idiomatic python"],"skill:python-testing":["pytest fixtures","mocking python","parametrized tests","coverage python","tdd python"],"skill:redis-patterns":["cache aside pattern","distributed lock","redis rate limiting","cache invalidation"],"skill:regex-vs-llm-structured-text":["parse invoice","extract receipt data","text extraction pipeline","parse form fields","cheap document parser","extract table data","parse log lines","parse access logs","common log format","log line parsing"],"skill:rust-patterns":["rust ownership","borrow checker","rust error handling","traits rust","rust concurrency","idiomatic rust"],"skill:search-first":["find existing library","npm package research","before writing custom code","evaluate existing tools","add dependency research"],"skill:security-review":["security audit","authentication review","sanitize user input","secrets handling","payment security checklist","prevent injection attacks","secure api endpoints","authn authz review","vulnerability checklist","input validation security","parameterized queries","sql injection"],"skill:security-scan":["audit claude config","claudemd security","mcp server audit","agentshield scan","hook configuration audit","settings json security"],"skill:tdd-workflow":["write test first","failing test","red green refactor","test driven development","regression test first","write a regression test"],"skill:verification-loop":["pre pr checks","verification report","quality gates","build lint test coverage","before creating a pr"]},"triggersDigest":"25b97a9e06fc336c7cf95ab854ed1a41033a54bcd6e1fb1cf69dc906332462aa"}++{"coverage":{"skills":302,"withTriggers":32},"generatedAt":"2026-09-24T23:51:22.784Z","id":"skill-triggers@1","model":{"effort":null,"id":"hand-seeded","source":"manual-curation-pending-regeneration"},"registryDigest":"ee5d05c974d849b60567586a81958bb40cb36512d0b985560aa3e0b1433f6149","schemaVersion":1,"triggers":{"skill:api-connector-builder":["add api integration","new provider connector","match existing integration pattern"],"skill:api-design":["rest endpoint design","pagination api","status codes","api versioning","rate limiting api","resource naming","filtering api","api error responses","offset pagination","limit query parameter","pagination defaults"],"skill:backend-patterns":["express api","node backend architecture","nextjs api routes","server side patterns","data access layer","static file server","url path handling","file server"],"skill:browser-qa":["deployed feature test","visual regression screenshots","core web vitals check","axe accessibility audit","ship do not ship","staging verification"],"skill:canary-watch":["post deploy monitoring","smoke test url","production url check","console errors production","sse stream check","after deploy verification"],"skill:code-tour":["onboarding walkthrough","explain subsystem","architecture tour","pr walkthrough","rca tour"],"skill:coding-standards":["code review standards","naming conventions","readability review","immutability conventions","fix naming typo","export naming","consistent exports"],"skill:content-hash-cache-pattern":["cache file processing","content addressed cache","sha256 hash cache"],"skill:database-migrations":["zero downtime migration","schema change production","add column large table","backfill data","expand contract","concurrent index","migration rollback","prisma migration","django migration"],"skill:deployment-patterns":["ci cd setup","dockerize app","health checks","rollback strategy","production readiness","deploy pipeline","containerize application"],"skill:design-system":["design tokens","visual consistency audit","css custom properties","ui audit","design system bootstrap"],"skill:django-patterns":["django orm","drf api","django rest framework","django caching","django signals","django middleware"],"skill:django-security":["django authentication","csrf protection","sql injection prevention","xss prevention","django deployment security","role based access control","authorization middleware","permissions checks"],"skill:docker-patterns":["dockerfile review","docker compose setup","container security","multi service orchestration"],"skill:error-handling":["error types","retry logic","circuit breaker","user facing errors","exception handling patterns","typed errors","error boundaries","go error handling","custom error class","error codes","config validation"],"skill:evm-token-decimals":["token decimals","wei conversion","erc20 balance off","bridge token precision"],"skill:frontend-a11y":["aria attributes","screen reader support","focus management","semantic html","form labeling","keyboard navigation react","a11y lint errors"],"skill:git-workflow":["merge vs rebase","commit conventions","resolve merge conflict","branching strategy","clean up commits","pull request cleanup","git history tidy"],"skill:hexagonal-architecture":["ports and adapters","dependency injection boundaries","decouple domain from io"],"skill:kubernetes-patterns":["kubernetes manifests","kubectl debugging","pod probes","k8s rbac","autoscaling config","configmap secrets"],"skill:orch-fix-defect":["fix a bug","broken behavior","regression fix","reproduce bug","defect repair"],"skill:postgres-patterns":["slow postgres query","query optimization","index design","rls policies","supabase schema","postgres indexing","database performance","schema design postgres","postgres driver","node postgres","query planner"],"skill:python-patterns":["pythonic code","pep 8","type hints python","python code review","idiomatic python"],"skill:python-testing":["pytest fixtures","mocking python","parametrized tests","coverage python","tdd python"],"skill:redis-patterns":["cache aside pattern","distributed lock","redis rate limiting","cache invalidation"],"skill:regex-vs-llm-structured-text":["parse invoice","extract receipt data","text extraction pipeline","parse form fields","cheap document parser","extract table data","parse log lines","parse access logs","common log format","log line parsing"],"skill:rust-patterns":["rust ownership","borrow checker","rust error handling","traits rust","rust concurrency","idiomatic rust"],"skill:search-first":["find existing library","npm package research","before writing custom code","evaluate existing tools","add dependency research"],"skill:security-review":["security audit","authentication review","sanitize user input","secrets handling","payment security checklist","prevent injection attacks","secure api endpoints","authn authz review","vulnerability checklist","input validation security","parameterized queries","sql injection"],"skill:security-scan":["audit claude config","claudemd security","mcp server audit","agentshield scan","hook configuration audit","settings json security"],"skill:tdd-workflow":["write test first","failing test","red green refactor","test driven development","regression test first","write a regression test"],"skill:verification-loop":["pre pr checks","verification report","quality gates","build lint test coverage","before creating a pr"]},"triggersDigest":"25b97a9e06fc336c7cf95ab854ed1a41033a54bcd6e1fb1cf69dc906332462aa"}diff --cc package.jsonindex f15dc9987,38c067438..c2c1cfd53--- a/package.json+++ b/package.json@@@ -124,7 -126,7 +126,8 @@@"scripts/memory.js","scripts/memory-mcp.mjs","scripts/gemini-adapt-agents.js",+ "scripts/gateguard-report.js",+ "scripts/github-coordination.js","scripts/harness-adapter-compliance.js","scripts/harness-audit.js","scripts/eval-harness.js",diff --cc tests/hooks/gateguard-fact-force.test.jsindex bd2310127,e28df0295..eddb03e5c--- a/tests/hooks/gateguard-fact-force.test.js+++ b/tests/hooks/gateguard-fact-force.test.js@@@ -4157,3270 -4183,41 +4199,3270 @@@ function runTests()passed++;else failed++;- // Cleanup only the temp directory created by this test file.- try {- if (fs.existsSync(stateDir)) {- fs.rmSync(stateDir, { recursive: true, force: true });- }- } catch (err) {- console.error(` [cleanup] failed to remove ${stateDir}: ${err.message}`);- }+ // --- Canonical checked-path keys ---+ const decisionOf = result => {+ const output = parseOutput(result.stdout);+ return output && output.hookSpecificOutput ? output.hookSpecificOutput.permissionDecision : undefined;+ };+ const denialCount = () => JSON.parse(fs.readFileSync(stateFile, 'utf8')).fact_force_denials;- // --- sanitizePath dangerous invisible unicode regression ---clearState();if (- test('sanitizePath strips CI-defined dangerous invisible unicode from denial paths', () => {- const file_path =- '/src/eu2028\u2028eu2029\u2029app.js\u200bhidden\u2060name\ufefftail\u00adsoft\u3164x\u0091c1.js';- const input = {- tool_name: 'Edit',- tool_input: { file_path, old_string: 'foo', new_string: 'bar' }- };- const result = runHook(input);- const output = parseOutput(result.stdout);- const reason = String(- output && output.hookSpecificOutput- ? output.hookSpecificOutput.permissionDecisionReason- : ''+ test('a.py, ./a.py and <root>/a.py share one checked key (one denial)', () => {+ const env = { CLAUDE_PROJECT_DIR: '/proj-keys' };+ const edit = file_path => runHook({ tool_name: 'Edit', tool_input: { file_path, old_string: 'a', new_string: 'b' } }, env);+ assert.strictEqual(decisionOf(edit('a.py')), 'deny', 'first touch of a.py is denied');+ assert.notStrictEqual(decisionOf(edit('./a.py')), 'deny', './a.py is the same file');+ assert.notStrictEqual(decisionOf(edit('/proj-keys/a.py')), 'deny', 'absolute spelling is the same file');+ assert.strictEqual(denialCount(), 1, 'exactly one denial counted');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('raw key from an older state file still counts as checked', () => {+ writeState({ checked: ['a.py'], last_active: Date.now(), fact_force_denials: 1 });+ const result = runHook(+ { tool_name: 'Edit', tool_input: { file_path: 'a.py', old_string: 'a', new_string: 'b' } },+ { CLAUDE_PROJECT_DIR: '/proj-keys' });- for (const bad of ['\u2028', '\u2029', '\u200b', '\u2060', '\ufeff', '\u00ad', '\u3164', '\u0091']) {- assert.ok(!reason.includes(bad), `denial reason must not carry U+${bad.codePointAt(0).toString(16)} (${bad})`);+ assert.notStrictEqual(decisionOf(result), 'deny', 'raw legacy key must not be re-denied');+ assert.strictEqual(denialCount(), 1, 'no new denial counted');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('Windows root folds separators and case into one key', () => {+ const env = { CLAUDE_PROJECT_DIR: 'C:\\proj' };+ const write = file_path => runHook({ tool_name: 'Write', tool_input: { file_path, content: 'x' } }, env);+ assert.strictEqual(decisionOf(write('src\\a.py')), 'deny', 'first touch is denied');+ assert.notStrictEqual(decisionOf(write('c:/proj/src/a.py')), 'deny', 'forward-slash lowercase spelling is the same file');+ assert.notStrictEqual(decisionOf(write('C:\\PROJ\\src\\A.py')), 'deny', 'case differs only on win32');+ assert.strictEqual(denialCount(), 1, 'exactly one denial counted');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('MultiEdit entries use the canonical key', () => {+ const env = { CLAUDE_PROJECT_DIR: '/proj-keys' };+ assert.strictEqual(+ decisionOf(runHook({ tool_name: 'Edit', tool_input: { file_path: 'b.py', old_string: 'a', new_string: 'b' } }, env)),+ 'deny'+ );+ const multi = runHook(+ { tool_name: 'MultiEdit', tool_input: { edits: [{ file_path: './b.py', old_string: 'a', new_string: 'b' }] } },+ env+ );+ assert.notStrictEqual(decisionOf(multi), 'deny', 'MultiEdit on ./b.py matches the checked b.py');+ assert.strictEqual(denialCount(), 1, 'exactly one denial counted');+ })+ )+ passed++;+ else failed++;++ // --- Relative targets resolve against the tool cwd, not the project root ---+ clearState();+ if (+ test('relative target resolves against data.cwd before CLAUDE_PROJECT_DIR', () => {+ const env = { CLAUDE_PROJECT_DIR: '/proj-cwd' };+ const edit = file_path =>+ runHook({ tool_name: 'Edit', cwd: '/proj-cwd/sub', tool_input: { file_path, old_string: 'a', new_string: 'b' } }, env);+ assert.strictEqual(decisionOf(edit('src/a.py')), 'deny', 'first touch of src/a.py (cwd sub) is denied');+ assert.notStrictEqual(decisionOf(edit('/proj-cwd/sub/src/a.py')), 'deny', 'absolute spelling of the same file is checked');+ assert.strictEqual(decisionOf(edit('/proj-cwd/src/a.py')), 'deny', 'a different file under the project root is gated');+ assert.strictEqual(denialCount(), 2, 'two distinct files, two denials');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('without data.cwd a relative target still resolves against CLAUDE_PROJECT_DIR', () => {+ const env = { CLAUDE_PROJECT_DIR: '/proj-cwd' };+ const edit = file_path => runHook({ tool_name: 'Edit', tool_input: { file_path, old_string: 'a', new_string: 'b' } }, env);+ assert.strictEqual(decisionOf(edit('src/a.py')), 'deny');+ assert.notStrictEqual(decisionOf(edit('/proj-cwd/src/a.py')), 'deny', 'project-root fallback unchanged');+ })+ )+ passed++;+ else failed++;++ // --- Questions by target class ---+ const CLASS_ENV = { CLAUDE_PROJECT_DIR: '/proj-classes' };+ const QUOTE_LINE = "Quote the user's current instruction verbatim";+ const RETRY_LINE = 'Present the facts, then retry the same operation.';+ const classDenialReason = (toolName, file_path, env = {}) => {+ const tool_input = toolName === 'MultiEdit'+ ? { edits: [{ file_path, old_string: 'a', new_string: 'b' }] }+ : { file_path, old_string: 'a', new_string: 'b', content: 'x' };+ const output = parseOutput(runHook({ tool_name: toolName, tool_input }, { ...CLASS_ENV, ...env }).stdout);+ assert.ok(output && output.hookSpecificOutput, `${toolName} ${file_path} should be denied`);+ assert.strictEqual(output.hookSpecificOutput.permissionDecision, 'deny');+ return output.hookSpecificOutput.permissionDecisionReason;+ };+ const assertNoCodeQuestions = reason => {+ assert.ok(!reason.includes('call this new file'), 'must not ask the code Write questions');+ assert.ok(!reason.includes('import/require'), 'must not ask the code Edit questions');+ assert.ok(!reason.includes('public functions/classes'), 'must not ask the code Edit questions');+ };+ const assertFrame = (reason, verb, file) => {+ assert.ok(reason.startsWith('[Fact-Forcing Gate]\n\n'), 'keeps the gate header');+ assert.ok(reason.includes(`Before ${verb} ${file}, present these facts:`), `header names ${verb} ${file}`);+ assert.ok(reason.includes(QUOTE_LINE), 'list ends with the verbatim-instruction line');+ assert.ok(reason.includes(`If this call was sent in a parallel batch, other edits to ${file}`), 'keeps batch-sibling warning');+ assert.ok(reason.includes(RETRY_LINE), 'keeps the closing retry line');+ assert.ok(reason.includes('GATEGUARD_EXEMPT_GLOBS'), 'keeps the path-scoped recovery hint');+ };++ clearState();+ if (+ test('first-touch Write of docs/guide.md asks prose questions, not code questions', () => {+ const reason = classDenialReason('Write', 'docs/guide.md');+ assertFrame(reason, 'creating', 'docs/guide.md');+ assert.ok(reason.includes('supersedes or duplicates'), 'asks what it supersedes');+ assert.ok(reason.includes('linked or referenced from'), 'asks where it is linked from');+ assert.ok(reason.includes('why a new file rather than editing an existing one'), 'asks why a new file');+ assert.ok(reason.includes(`4. ${QUOTE_LINE}`), 'quote is item 4');+ assertNoCodeQuestions(reason);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('first-touch Edit of docs/guide.md asks prose Edit questions', () => {+ const reason = classDenialReason('Edit', 'docs/guide.md');+ assertFrame(reason, 'editing', 'docs/guide.md');+ assert.ok(reason.includes('reference the section being changed'), 'asks what references the section');+ assert.ok(reason.includes('corrects or adds'), 'asks what the change corrects or adds');+ assert.ok(reason.includes(`3. ${QUOTE_LINE}`), 'quote is item 3');+ assert.ok(!reason.includes('supersedes'), 'Write-only prose question absent');+ assertNoCodeQuestions(reason);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('skills/x/SKILL.md and .claude/agents/a.md get instruction questions', () => {+ for (const file of ['skills/x/SKILL.md', '.claude/agents/a.md', 'CLAUDE.md']) {+ const reason = classDenialReason('Write', file);+ assertFrame(reason, 'creating', file);+ assert.ok(reason.includes('harness/loader'), `${file}: asks which harness reads it`);+ assert.ok(reason.includes('agent behaviour changes'), `${file}: asks what behaviour changes`);+ assert.ok(reason.includes('instruction, skill, or agent file already covers'), `${file}: asks for existing coverage`);+ assert.ok(reason.includes(`4. ${QUOTE_LINE}`), `${file}: quote is item 4`);+ assertNoCodeQuestions(reason);}- assert.ok(reason.includes('app.js'), 'visible path text must remain');})- ) {+ )passed++;- } else {- failed++;+ else failed++;++ clearState();+ if (+ test('src/a.py Write and Edit keep the code text byte-identical', () => {+ const fullReason = tool_input => {+ const output = parseOutput(runHook({ tool_name: tool_input.content === undefined ? 'Edit' : 'Write', tool_input }, CLASS_ENV).stdout);+ assert.ok(output && output.hookSpecificOutput, `${tool_input.file_path} should be denied`);+ return output.hookSpecificOutput.permissionDecisionReason;+ };+ const writeReason = fullReason({ file_path: 'src/a.py', content: 'import json\n' });+ const expectedWrite = [+ '[Fact-Forcing Gate]',+ '',+ 'Before creating src/a.py, present these facts:',+ '',+ '1. Name the file(s) and line(s) that will call this new file',+ '2. Confirm no existing file serves the same purpose (search the tree — Glob/Grep, or find/grep via Bash)',+ '3. If this file reads/writes data files, show field names, structure, and date format (use redacted or synthetic values, not raw production data)',+ '4. Name what outside this repository decides the format, units, timezone or protocol semantics here (the consumer, the producer, or a stated convention), or state that the choice is unconstrained',+ "5. Quote the user's current instruction verbatim",+ '',+ 'If this call was sent in a parallel batch, other edits to src/a.py from that batch may already have been applied. Re-read the file before building on them.',+ '',+ 'Present the facts, then retry the same operation.',+ ''+ ].join('\n');+ assert.ok(writeReason.startsWith(expectedWrite), 'code Write block unchanged');+ const editReason = fullReason({ file_path: 'src/b.py', old_string: 'def load(path):', new_string: 'def load_json(path):' });+ const expectedEdit = [+ '[Fact-Forcing Gate]',+ '',+ 'Before editing src/b.py, present these facts:',+ '',+ '1. List ALL files that import/require this file (search the tree — Glob/Grep, or find/grep via Bash)',+ '2. List the public functions/classes affected by this change',+ '3. If this file reads/writes data files, show field names, structure, and date format (use redacted or synthetic values, not raw production data)',+ '4. Name what outside this repository decides the format, units, timezone or protocol semantics here (the consumer, the producer, or a stated convention), or state that the choice is unconstrained',+ "5. Quote the user's current instruction verbatim",+ '',+ 'If this call was sent in a parallel batch, other edits to src/b.py from that batch may already have been applied. Re-read the file before building on them.',+ '',+ 'Present the facts, then retry the same operation.',+ ''+ ].join('\n');+ assert.ok(editReason.startsWith(expectedEdit), 'code Edit block unchanged');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('src/a.test.ts and tests/helpers.js get test questions', () => {+ for (const file of ['src/a.test.ts', 'tests/helpers.js']) {+ const reason = classDenialReason('Write', file);+ assertFrame(reason, 'creating', file);+ assert.ok(reason.includes('behaviour is under test'), `${file}: asks what is under test`);+ assert.ok(reason.includes('existing test file(s)'), `${file}: asks for existing test files`);+ assert.ok(reason.includes(`3. ${QUOTE_LINE}`), `${file}: quote is item 3`);+ assertNoCodeQuestions(reason);+ }+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('config/app.yaml and .env.local get config questions', () => {+ for (const file of ['config/app.yaml', '.env.local']) {+ const reason = classDenialReason('Write', file);+ assertFrame(reason, 'creating', file);+ assert.ok(reason.includes('process/tool reads this file'), `${file}: asks what reads it`);+ assert.ok(reason.includes('effect of the change'), `${file}: asks for the effect`);+ assert.ok(reason.includes('no secrets or credentials'), `${file}: asks about secrets`);+ assert.ok(reason.includes(`4. ${QUOTE_LINE}`), `${file}: quote is item 4`);+ assertNoCodeQuestions(reason);+ }+ })+ )+ passed++;+ else failed++;++ // --- Questions from the change profile ---+ const PROFILE_ENV = { CLAUDE_PROJECT_DIR: '/proj-profile' };+ const profileDenialReason = (toolName, tool_input, env = {}) => {+ const output = parseOutput(runHook({ tool_name: toolName, tool_input }, { ...PROFILE_ENV, ...env }).stdout);+ assert.ok(output && output.hookSpecificOutput, `${toolName} ${JSON.stringify(tool_input).slice(0, 80)} should be denied`);+ assert.strictEqual(output.hookSpecificOutput.permissionDecision, 'deny');+ return output.hookSpecificOutput.permissionDecisionReason;+ };+ const listedQuestions = reason => reason.split('\n').filter(line => /^\d+\. /.test(line)).map(line => line.replace(/^\d+\. /, ''));+ const QUESTION_LINES = {+ importers: 'List ALL files that import/require this file (search the tree — Glob/Grep, or find/grep via Bash)',+ publicApi: 'List the public functions/classes affected by this change',+ localCallers: 'List the call sites in this file or its module that rely on the changed behaviour (search the tree — Glob/Grep, or find/grep via Bash)',+ callers: 'Name the file(s) and line(s) that will call this new file',+ noDuplicate: 'Confirm no existing file serves the same purpose (search the tree — Glob/Grep, or find/grep via Bash)',+ dataSchema: 'If this file reads/writes data files, show field names, structure, and date format (use redacted or synthetic values, not raw production data)',+ externalContract:+ 'Name what outside this repository decides the format, units, timezone or protocol semantics here (the consumer, the producer, or a stated convention), or state that the choice is unconstrained',+ quote: QUOTE_LINE+ };+ const B2_FULL_EDIT = [QUESTION_LINES.importers, QUESTION_LINES.publicApi, QUESTION_LINES.dataSchema, QUESTION_LINES.externalContract, QUESTION_LINES.quote];+ const B2_FULL_WRITE = [QUESTION_LINES.callers, QUESTION_LINES.noDuplicate, QUESTION_LINES.dataSchema, QUESTION_LINES.externalContract, QUESTION_LINES.quote];++ clearState();+ if (+ test('an Edit that changes an exported signature asks for importers and the affected public API', () => {+ const reason = profileDenialReason('Edit', { file_path: 'src/api.js', old_string: 'export function load(a) {', new_string: 'export function load(a, b) {' });+ assert.deepStrictEqual(listedQuestions(reason), [QUESTION_LINES.importers, QUESTION_LINES.publicApi, QUESTION_LINES.quote]);+ assertFrame(reason, 'editing', 'src/api.js');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('an Edit of a function body asks for call sites in the file or module instead of importers', () => {+ const reason = profileDenialReason('Edit', { file_path: 'src/calc.js', old_string: ' return a + 1;', new_string: ' return a * 2;' });+ assert.deepStrictEqual(listedQuestions(reason), [QUESTION_LINES.localCallers, QUESTION_LINES.quote]);+ assertFrame(reason, 'editing', 'src/calc.js');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('an Edit that handles data keeps the data-schema question', () => {+ const reason = profileDenialReason('Edit', { file_path: 'src/calc.py', old_string: ' x = 1', new_string: ' rows = json.load(fh)' });+ assert.deepStrictEqual(listedQuestions(reason), [QUESTION_LINES.localCallers, QUESTION_LINES.dataSchema, QUESTION_LINES.externalContract, QUESTION_LINES.quote]);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('a Write of a new code file without data handling drops the data-schema question', () => {+ const reason = profileDenialReason('Write', { file_path: 'src/new_mod.js', content: 'export const x = 1;\n' });+ assert.deepStrictEqual(listedQuestions(reason), [QUESTION_LINES.callers, QUESTION_LINES.noDuplicate, QUESTION_LINES.quote]);+ const withData = profileDenialReason('Write', { file_path: 'lib/new_io.js', content: "const fs = require('fs');\n" });+ assert.deepStrictEqual(listedQuestions(withData), B2_FULL_WRITE);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('an unknown or over-bound change keeps the full code questions byte-identical', () => {+ const cases = [+ ['Edit', { file_path: 'src/a.rb', old_string: 'a', new_string: 'b' }, B2_FULL_EDIT],+ ['Edit', { file_path: 'src/big.js', old_string: 'x'.repeat(70 * 1024), new_string: 'y' }, B2_FULL_EDIT],+ ['Edit', { file_path: 'src/shape.js' }, B2_FULL_EDIT],+ ['Edit', { file_path: 'src/shape2.js', old_string: 5, new_string: 'b' }, B2_FULL_EDIT],+ ['Write', { file_path: 'src/d.rb', content: 'x' }, B2_FULL_WRITE],+ ['Write', { file_path: 'src/e.js' }, B2_FULL_WRITE]+ ];+ for (const [tool, input, expected] of cases) {+ clearState();+ const reason = profileDenialReason(tool, input);+ assert.deepStrictEqual(listedQuestions(reason), expected, `${input.file_path}`);+ const header = `Before ${tool === 'Write' ? 'creating' : 'editing'} ${input.file_path}, present these facts:`;+ const block = ['[Fact-Forcing Gate]', '', header, '', ...expected.map((q, i) => `${i + 1}. ${q}`), ''].join('\n');+ assert.ok(reason.startsWith(block), `${input.file_path}: block unchanged`);+ }+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('sensitive code targets always get the full code questions', () => {+ const reason = profileDenialReason('Edit', { file_path: 'src/auth/login.js', old_string: ' return a + 1;', new_string: ' return a * 2;' });+ assert.deepStrictEqual(listedQuestions(reason), B2_FULL_EDIT);+ assert.ok(reason.includes('Sensitive target'), 'sensitive note');+ const write = profileDenialReason('Write', { file_path: 'src/billing/new.js', content: 'export const x = 1;\n' });+ assert.deepStrictEqual(listedQuestions(write), B2_FULL_WRITE);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('config, instruction and test targets keep their class questions whatever the change', () => {+ const config = listedQuestions(profileDenialReason('Write', { file_path: 'config/app.yaml', content: 'a: 1\n' }));+ assert.strictEqual(config.length, 4, 'config keeps three questions and the quote');+ assert.ok(config[2].includes('no secrets or credentials'));+ const instruction = listedQuestions(profileDenialReason('Edit', { file_path: 'CLAUDE.md', old_string: 'a', new_string: 'b' }));+ assert.strictEqual(instruction.length, 4, 'instruction keeps three questions and the quote');+ const testFile = listedQuestions(profileDenialReason('Edit', { file_path: 'src/a.test.js', old_string: ' return 1;', new_string: ' return 2;' }));+ assert.ok(testFile[0].includes('behaviour is under test'), 'test questions');+ assert.strictEqual(testFile.length, 3);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('MultiEdit questions follow every entry for the denied file', () => {+ const surface = profileDenialReason('MultiEdit', {+ edits: [+ { file_path: 'src/m.js', old_string: ' return 1;', new_string: ' return 2;' },+ { file_path: 'src/m.js', old_string: 'function f() {', new_string: 'export function f() {' }+ ]+ });+ assert.deepStrictEqual(listedQuestions(surface), [QUESTION_LINES.importers, QUESTION_LINES.publicApi, QUESTION_LINES.quote]);+ clearState();+ const local = profileDenialReason('MultiEdit', {+ edits: [+ { file_path: 'src/n.js', old_string: ' return 1;', new_string: ' return 2;' },+ { file_path: 'src/other.js', old_string: 'export const y = 1;', new_string: 'export const y = 2;' }+ ]+ });+ assert.deepStrictEqual(listedQuestions(local), [QUESTION_LINES.localCallers, QUESTION_LINES.quote], 'entries for other files do not count');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('shell scripts ask the questions their change warrants', () => {+ const local = profileDenialReason('Edit', { file_path: 'scripts/q1.sh', old_string: ' echo "a"', new_string: ' echo "b"' });+ assert.deepStrictEqual(listedQuestions(local), [QUESTION_LINES.localCallers, QUESTION_LINES.quote]);+ clearState();+ const fn = profileDenialReason('Edit', { file_path: 'scripts/q2.sh', old_string: 'deploy() {', new_string: 'deploy_all() {' });+ assert.deepStrictEqual(listedQuestions(fn), [QUESTION_LINES.importers, QUESTION_LINES.publicApi, QUESTION_LINES.quote]);+ clearState();+ const exported = profileDenialReason('Edit', { file_path: 'scripts/q3.sh', old_string: 'export MODE=a', new_string: 'export MODE=b' });+ assert.deepStrictEqual(listedQuestions(exported), [QUESTION_LINES.importers, QUESTION_LINES.publicApi, QUESTION_LINES.quote]);+ clearState();+ const fetch = profileDenialReason('Edit', { file_path: 'scripts/q4.sh', old_string: ' run', new_string: ' curl -s "$URL" > out.json' });+ assert.deepStrictEqual(listedQuestions(fetch), [QUESTION_LINES.localCallers, QUESTION_LINES.dataSchema, QUESTION_LINES.externalContract, QUESTION_LINES.quote]);+ clearState();+ const ps = profileDenialReason('Edit', { file_path: 'scripts/q5.ps1', old_string: 'function Get-A {', new_string: 'function Get-B {' });+ assert.deepStrictEqual(listedQuestions(ps), [QUESTION_LINES.importers, QUESTION_LINES.publicApi, QUESTION_LINES.quote]);+ clearState();+ const bat = profileDenialReason('Edit', { file_path: 'scripts/q6.bat', old_string: 'echo a', new_string: 'echo b' });+ assert.deepStrictEqual(listedQuestions(bat), [QUESTION_LINES.importers, QUESTION_LINES.publicApi, QUESTION_LINES.quote], 'batch always counts as surface');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('a one-line edit of a Python module constant asks for importers', () => {+ const reason = profileDenialReason('Edit', { file_path: 'tools/q7.py', old_string: 'BASE = "https://a.test/#home"', new_string: 'BASE = "https://a.test/#admin"' });+ assert.deepStrictEqual(listedQuestions(reason), [QUESTION_LINES.importers, QUESTION_LINES.publicApi, QUESTION_LINES.quote]);+ clearState();+ const local = profileDenialReason('Edit', { file_path: 'tools/q8.py', old_string: 'result = compute(a)', new_string: 'result = compute(b)' });+ assert.deepStrictEqual(listedQuestions(local), [QUESTION_LINES.localCallers, QUESTION_LINES.quote]);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('condensed denials follow the change profile', () => {+ const env = { GATEGUARD_FACT_FORCE_FULL_DENIALS: '0' };+ const local = profileDenialReason('Edit', { file_path: 'src/c1.js', old_string: ' return 1;', new_string: ' return 2;' }, env);+ assert.ok(local.includes('the call sites in this file or its module that rely on the change'), local);+ assert.ok(!local.includes('importers/callers'), 'no importer hint for a local change');+ assert.ok(!local.includes('data schemas'), 'no data hint for a change without data');+ const created = profileDenialReason('Write', { file_path: 'src/c3.js', content: 'export const a = 1;\n' }, env);+ assert.ok(created.includes('that no existing file serves the same purpose'), created);+ assert.ok(created.includes('(denial #') && created.includes('parallel batch') && created.includes('ECC_GATEGUARD=off'), 'keeps ordinal and hints');+ const data = profileDenialReason('Edit', { file_path: 'src/c4.js', old_string: ' return 1;', new_string: ' return JSON.parse(raw);' }, env);+ assert.ok(data.includes('the data schemas it reads or writes'), data);+ const full = profileDenialReason('Edit', { file_path: 'src/c2.rb', old_string: 'a', new_string: 'b' }, env);+ assert.ok(full.includes('briefly state importers/callers, affected API, data schemas if any'), 'unknown profile keeps the hint');+ })+ )+ passed++;+ else failed++;++ // --- Comment and whitespace-only edits ---+ const trivialRoot = fs.mkdtempSync(path.join(tmpRoot, 'gateguard-trivial-'));+ const seedTrivialFiles = tool_input => {+ const entries = Array.isArray(tool_input.edits) ? tool_input.edits : [tool_input];+ const byFile = new Map();+ for (const entry of entries) {+ if (!entry || typeof entry !== 'object') continue;+ const rel = entry.file_path || tool_input.file_path;+ if (typeof rel !== 'string' || typeof entry.old_string !== 'string') continue;+ byFile.set(rel, [...(byFile.get(rel) || []), entry.old_string]);+ }+ for (const [rel, parts] of byFile) {+ const file = path.join(trivialRoot, rel);+ if (fs.existsSync(file)) continue;+ fs.mkdirSync(path.dirname(file), { recursive: true });+ fs.writeFileSync(file, `${parts.join('\n')}\n`);+ }+ };+ const trivialRun = (toolName, tool_input, env = {}) => {+ seedTrivialFiles(tool_input);+ const result = runHook({ tool_name: toolName, tool_input }, { CLAUDE_PROJECT_DIR: trivialRoot, ...env });+ const output = parseOutput(result.stdout);+ const hso = output && output.hookSpecificOutput ? output.hookSpecificOutput : {};+ const context = Array.isArray(hso.additionalContext) ? hso.additionalContext.join('\n') : String(hso.additionalContext || '');+ return { result, decision: hso.permissionDecision, context, reason: hso.permissionDecisionReason || '' };+ };+ const trivialState = () => (fs.existsSync(stateFile) ? JSON.parse(fs.readFileSync(stateFile, 'utf8')) : {});+ const trivialKey = rel => `${trivialRoot}/${rel}`;+ const TRIVIAL_NOTE = 'Comment or whitespace-only change to';++ clearState();+ if (+ test('a comment-only Edit of an unchecked code file passes with a note and is not marked checked', () => {+ const out = trivialRun('Edit', { file_path: 'src/t1.js', old_string: '// old note\nfoo();', new_string: '// new note\nfoo();' });+ assert.notStrictEqual(out.decision, 'deny', out.result.stdout);+ assert.ok(out.context.includes(`${TRIVIAL_NOTE} src/t1.js`), out.context);+ assert.strictEqual(out.result.code, 0);+ const state = trivialState();+ assert.ok(!(state.checked || []).includes(trivialKey('src/t1.js')), 'not marked checked');+ assert.strictEqual(state.trivial_allows, 1);+ assert.strictEqual(state.fact_force_denials || 0, 0, 'denial count untouched');+ assert.ok(!out.result.stdout.includes('"allow"'), 'never an allow decision');+ })+ )+ passed++;+ else failed++;++ if (+ test('a later code change to the same file is still gated', () => {+ const out = trivialRun('Edit', { file_path: 'src/t1.js', old_string: 'foo();', new_string: 'bar();' });+ assert.strictEqual(out.decision, 'deny', out.result.stdout);+ const state = trivialState();+ assert.strictEqual(state.fact_force_denials, 1);+ assert.strictEqual(state.trivial_allows, 1, 'trivial counter kept');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('whitespace-only reindents and trivial test-file edits pass', () => {+ const js = trivialRun('Edit', { file_path: 'src/t2.js', old_string: 'if (x) {\n foo();\n}', new_string: 'if (x) {\n foo();\n}' });+ assert.ok(js.context.includes(TRIVIAL_NOTE), js.result.stdout);+ const spec = trivialRun('Edit', { file_path: 'src/t2.test.js', old_string: "it('a', () => {}); // old", new_string: "it('a', () => {}); // new" });+ assert.ok(spec.context.includes(TRIVIAL_NOTE), spec.result.stdout);+ assert.strictEqual(trivialState().trivial_allows, 2);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('comment-only edits of shell, PowerShell and batch scripts pass without marking them checked', () => {+ const cases = [+ { file_path: 'scripts/s1.sh', old_string: '# build\n', new_string: '# Build all targets.\n' },+ { file_path: 'scripts/s2.ps1', old_string: '<# old #>\nGet-Item a', new_string: '<# new #>\nGet-Item a' },+ { file_path: 'scripts/s3.cmd', old_string: 'REM old\necho a', new_string: 'REM new\necho a' }+ ];+ for (const input of cases) {+ const out = trivialRun('Edit', input);+ assert.notStrictEqual(out.decision, 'deny', out.result.stdout);+ assert.ok(out.context.includes(`${TRIVIAL_NOTE} ${input.file_path}`), out.context);+ assert.ok(!(trivialState().checked || []).includes(trivialKey(input.file_path)), 'not marked checked');+ }+ assert.strictEqual(trivialState().trivial_allows, 3);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('trivial-looking edits are still denied where the pass does not apply', () => {+ const cases = [+ ['sensitive target', { file_path: 'src/auth/session.js', old_string: '// old\nfoo();', new_string: '// new\nfoo();' }],+ ['comment plus code', { file_path: 'src/d1.js', old_string: '// old\nfoo(1);', new_string: '// new\nfoo(2);' }],+ ['python indentation', { file_path: 'src/d2.py', old_string: 'if x:\n y()', new_string: 'if x:\n y()' }],+ ['unknown extension', { file_path: 'src/d3.rb', old_string: '# old', new_string: '# new' }],+ ['config target', { file_path: 'config/d4.yaml', old_string: '# old', new_string: '# new' }],+ ['instruction target', { file_path: 'CLAUDE.md', old_string: '<!-- old -->', new_string: '<!-- new -->' }],+ ['instruction code file', { file_path: '.claude/hooks/d5.md', old_string: '// old', new_string: '// new' }],+ ['template literal', { file_path: 'src/d6.js', old_string: '// old\n`;', new_string: '// new\n`;' }],+ ['over the bound', { file_path: 'src/d7.js', old_string: `// ${'x'.repeat(70 * 1024)}`, new_string: '// y' }],+ ['missing new_string', { file_path: 'src/d8.js', old_string: '// old' }],+ ['sensitive shell script', { file_path: 'scripts/auth/rotate.sh', old_string: '# old\nrun', new_string: '# new\nrun' }],+ ['workflow shell script', { file_path: '.github/workflows/d9.sh', old_string: '# old', new_string: '# new' }],+ ['shell heredoc', { file_path: 'scripts/d10.sh', old_string: 'cat <<EOF\n# old\nEOF', new_string: 'cat <<EOF\n# new\nEOF' }],+ ['shell quoted hash', { file_path: 'scripts/d11.sh', old_string: 'echo "a # old"', new_string: 'echo "a # new"' }],+ ['shell shebang', { file_path: 'scripts/d12.sh', old_string: '#!/bin/bash', new_string: '#!/bin/sh' }],+ ['powershell requires', { file_path: 'scripts/d13.ps1', old_string: '#Requires -Version 5', new_string: '#Requires -Version 7' }],+ ['powershell here-string', { file_path: 'scripts/d14.ps1', old_string: '@"\n# old\n"@', new_string: '@"\n# new\n"@' }]+ ];+ for (const [label, input] of cases) {+ clearState();+ const out = trivialRun('Edit', input);+ assert.strictEqual(out.decision, 'deny', `${label}: ${out.result.stdout}`);+ assert.ok(!out.context.includes(TRIVIAL_NOTE), `${label}: no trivial note`);+ assert.strictEqual(trivialState().trivial_allows || 0, 0, `${label}: no trivial count`);+ }+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('comment-looking edits that change code in their file context are denied', () => {+ const cases = [+ ['macro continuation', 'src/ctx/a.c', '#define A 1 \\\n// c\nint x;\n', { old_string: '// c\n', new_string: '' }],+ ['joined next line', 'src/ctx/b.js', 'a();\n// c\nb();\n', { old_string: '// c\n', new_string: '// c ' }],+ ['template literal', 'src/ctx/c.js', 'const q = `\n // hint\n SELECT 1\n`;\n', { old_string: ' // hint', new_string: ' // other' }],+ ['docstring', 'src/ctx/d.py', 'Q = """\n# limit 10\nSELECT 1\n"""\n', { old_string: '# limit 10', new_string: '# limit 99' }],+ ['heredoc body', 'scripts/ctx/e.sh', 'cat > colors.txt <<EOF\n#ff0000\n#00ff00\nEOF\n', { old_string: '#00ff00', new_string: '#0000ff' }],+ ['inside a string', 'src/ctx/f.js', 'const s = "abc // q";\n', { old_string: '// q"', new_string: '// r"' }],+ ['replace_all into a string', 'src/ctx/g.js', 'x = "// a"; // a\n', { old_string: '// a', new_string: '// b', replace_all: true }],+ ['type directive', 'src/ctx/h.py', 'x = f() # type: ignore\n', { old_string: '# type: ignore', new_string: '# ok' }],+ ['suppression marker', 'src/ctx/i.py', 'call(cmd, shell=True) # nosec\n', { old_string: '# nosec', new_string: '# reviewed' }],+ ['build constraint', 'src/ctx/j.go', '//go:build linux\n\npackage j\n', { old_string: '//go:build linux', new_string: '//go:build ignore' }],+ ['cgo preamble', 'src/ctx/k.go', 'package k\n\n// int add(int a) { return a; }\nimport "C"\n', { old_string: 'return a;', new_string: 'return a + 1;' }]+ ];+ for (const [label, rel, content, input] of cases) {+ clearState();+ const file = path.join(trivialRoot, rel);+ fs.mkdirSync(path.dirname(file), { recursive: true });+ fs.writeFileSync(file, content);+ const out = trivialRun('Edit', { file_path: rel, ...input });+ assert.strictEqual(out.decision, 'deny', `${label}: ${out.result.stdout}`);+ assert.strictEqual(trivialState().trivial_allows || 0, 0, `${label}: no trivial count`);+ }+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('the trivial pass needs a readable regular file under the size bound', () => {+ const input = { old_string: '// old', new_string: '// new' };+ const cases = [['missing file', 'src/nf/missing.js']];+ const big = path.join(trivialRoot, 'src/nf/big.js');+ fs.mkdirSync(path.dirname(big), { recursive: true });+ fs.writeFileSync(big, `// old\n${'x();\n'.repeat(300 * 1024)}`);+ cases.push(['file over 1 MiB', 'src/nf/big.js']);+ fs.mkdirSync(path.join(trivialRoot, 'src/nf/dir.js'), { recursive: true });+ cases.push(['directory', 'src/nf/dir.js']);+ if (process.platform !== 'win32' && spawnSync('mkfifo', [path.join(trivialRoot, 'src/nf/pipe.js')]).status === 0) {+ cases.push(['named pipe', 'src/nf/pipe.js']);+ }+ for (const [label, rel] of cases) {+ clearState();+ const started = Date.now();+ const result = runHook({ tool_name: 'Edit', tool_input: { file_path: rel, ...input } }, { CLAUDE_PROJECT_DIR: trivialRoot });+ const hso = (parseOutput(result.stdout) || {}).hookSpecificOutput || {};+ assert.strictEqual(hso.permissionDecision, 'deny', `${label}: ${result.stdout}`);+ assert.ok(Date.now() - started < 10000, `${label}: returned promptly`);+ }+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('a comment edit next to closed multi-line constructs still passes', () => {+ const rel = 'src/ctx/ok.js';+ const file = path.join(trivialRoot, rel);+ fs.mkdirSync(path.dirname(file), { recursive: true });+ fs.writeFileSync(file, 'const q = `a\n${b}\n`;\nconst r = /[/"]+/g;\n\n// old\nfoo();\n');+ const out = trivialRun('Edit', { file_path: rel, old_string: '// old', new_string: '// new' });+ assert.ok(out.context.includes(`${TRIVIAL_NOTE} ${rel}`), out.result.stdout);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('a Write is never a trivial edit', () => {+ const out = trivialRun('Write', { file_path: 'src/w1.js', content: '// only a comment\n' });+ assert.strictEqual(out.decision, 'deny', out.result.stdout);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('MultiEdit passes a file only when all of its entries are trivial', () => {+ const allTrivial = trivialRun('MultiEdit', {+ edits: [+ { file_path: 'src/m1.js', old_string: '// a', new_string: '// b' },+ { file_path: 'src/m1.js', old_string: 'foo(); // c', new_string: 'foo(); // d' }+ ]+ });+ assert.notStrictEqual(allTrivial.decision, 'deny', allTrivial.result.stdout);+ assert.ok(allTrivial.context.includes(`${TRIVIAL_NOTE} src/m1.js`), allTrivial.context);+ assert.strictEqual(trivialState().trivial_allows, 1, 'one pass per file');+ clearState();+ const mixed = trivialRun('MultiEdit', {+ edits: [+ { file_path: 'src/m2.js', old_string: '// a', new_string: '// b' },+ { file_path: 'src/m2.js', old_string: 'foo(1);', new_string: 'foo(2);' }+ ]+ });+ assert.strictEqual(mixed.decision, 'deny', 'a non-trivial entry for the file denies it');+ clearState();+ const twoFiles = trivialRun('MultiEdit', {+ edits: [+ { file_path: 'src/m3.js', old_string: '// a', new_string: '// b' },+ { file_path: 'src/m4.js', old_string: 'foo(1);', new_string: 'foo(2);' }+ ]+ });+ assert.strictEqual(twoFiles.decision, 'deny');+ assert.ok(twoFiles.reason.includes('src/m4.js'), 'the non-trivial file is the one denied');+ const state = trivialState();+ assert.ok(!(state.checked || []).includes(trivialKey('src/m3.js')), 'trivial file not marked checked');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('MultiEdit entries without their own path are gated as the call file_path', () => {+ const sensitive = trivialRun('MultiEdit', { file_path: '.env', edits: [{ old_string: 'A=1', new_string: 'A=2' }] });+ assert.strictEqual(sensitive.decision, 'deny', sensitive.result.stdout);+ assert.ok(sensitive.reason.includes('.env'), sensitive.reason);+ clearState();+ const code = trivialRun('MultiEdit', { file_path: 'src/mt1.js', edits: [{ old_string: 'f(1);', new_string: 'f(2);' }] });+ assert.strictEqual(code.decision, 'deny', code.result.stdout);+ assert.ok(code.reason.includes('src/mt1.js'), code.reason);+ clearState();+ const trivial = trivialRun('MultiEdit', { file_path: 'src/mt2.js', edits: [{ old_string: 'f(); // a', new_string: 'f(); // b' }] });+ assert.ok(trivial.context.includes(`${TRIVIAL_NOTE} src/mt2.js`), trivial.result.stdout);+ clearState();+ const input = { file_path: 'config/.env.local', edits: [{ old_string: 'A=1', new_string: 'A=2' }] };+ seedTrivialFiles(input);+ const result = runHook({ tool_name: 'MultiEdit', tool_input: input, agent_id: 'sub-1' }, { CLAUDE_PROJECT_DIR: trivialRoot });+ const hso = (parseOutput(result.stdout) || {}).hookSpecificOutput || {};+ assert.strictEqual(hso.permissionDecision, 'deny', `subagent: ${result.stdout}`);+ clearState();+ const odd = trivialRun('MultiEdit', { file_path: 'src/mt3.js', edits: [null, 7, { old_string: 'g(1);', new_string: 'g(2);' }] });+ assert.strictEqual(odd.decision, 'deny', `malformed entries: ${odd.result.stdout}`);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('the trivial pass comes before the denial cap and never consumes it', () => {+ const out = trivialRun('Edit', { file_path: 'src/cap1.js', old_string: '// a', new_string: '// b' }, { GATEGUARD_FACT_FORCE_MAX_DENIALS: '0' });+ assert.ok(out.context.includes(TRIVIAL_NOTE), out.result.stdout);+ const state = trivialState();+ assert.strictEqual(state.cap_allows || 0, 0);+ assert.strictEqual(state.trivial_allows, 1);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('trivial_allows merges by maximum with a concurrent state file', () => {+ writeState({ checked: [], last_active: Date.now(), trivial_allows: 5 });+ trivialRun('Edit', { file_path: 'src/merge1.js', old_string: '// a', new_string: '// b' });+ assert.strictEqual(trivialState().trivial_allows, 6);+ writeState({ checked: [], last_active: Date.now(), trivial_allows: 'junk' });+ trivialRun('Edit', { file_path: 'src/merge2.js', old_string: '// a', new_string: '// b' });+ assert.strictEqual(trivialState().trivial_allows, 1, 'malformed counter reads as zero');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('scripts/hooks/x.js is code, not instruction', () => {+ const reason = classDenialReason('Write', 'scripts/hooks/x.js');+ assert.ok(reason.includes('call this new file'), 'code Write questions');+ assert.ok(!reason.includes('harness/loader'), 'not instruction questions');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('MultiEdit on a .md entry uses the prose Edit variant', () => {+ const reason = classDenialReason('MultiEdit', 'docs/multi.md');+ assertFrame(reason, 'editing', 'docs/multi.md');+ assert.ok(reason.includes('reference the section being changed'), 'prose Edit questions');+ assertNoCodeQuestions(reason);+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('condensed denial for .md after the budget carries the prose hint and ordinal', () => {+ const env = { GATEGUARD_FACT_FORCE_FULL_DENIALS: '0' };+ const writeReason = classDenialReason('Write', 'docs/late.md', env);+ assert.ok(!writeReason.includes('\n'), 'condensed denial is one line');+ assert.ok(writeReason.includes('(denial #1 this session) First creation of docs/late.md: '), 'keeps ordinal and target');+ assert.ok(writeReason.includes('briefly state what this supersedes, where it is linked from'), 'prose Write hint');+ assert.ok(!writeReason.includes('importers/callers'), 'no code hint');+ assert.ok(writeReason.includes('parallel batch'), 'keeps batch-sibling warning');+ assert.ok(writeReason.includes('GATEGUARD_EXEMPT_GLOBS') && writeReason.includes('ECC_GATEGUARD=off'), 'keeps exemption hint');+ const editReason = classDenialReason('Edit', 'docs/late-edit.md', env);+ assert.ok(editReason.includes('(denial #2 this session) First edit of docs/late-edit.md: '), 'ordinal advances');+ assert.ok(editReason.includes('what references the changed section'), 'prose Edit hint');+ const codeReason = classDenialReason('Edit', 'src/late.rb', env);+ assert.ok(codeReason.includes('briefly state importers/callers, affected API, data schemas if any'), 'code hint unchanged');+ })+ )+ passed++;+ else failed++;++ clearState();+ if (+ test('class denials still sanitize the path', () => {+ const reason = classDenialReason('Write', 'docs/hid\u200bden.md');+ assert.ok(!reason.includes('\u200b'), 'zero-width space stripped');+ assert.ok(reason.includes('supersedes or duplicates'), 'still prose questions');+ })+ )+ passed++;+ else failed++;++ // --- Prior-search credit in the current human turn ---+ const creditRoot = '/proj-credit';+ // An ambient CLAUDE_TRANSCRIPT_PATH must not leak into cases that pass no transcript.+ const creditEnv = { CLAUDE_PROJECT_DIR: creditRoot, CLAUDE_TRANSCRIPT_PATH: '' };+ const transcriptDir = fs.mkdtempSync(path.join(tmpRoot, 'gateguard-transcripts-'));+ const creditOutputs = [];+ let transcriptSeq = 0;+ let uuidSeq = 0;+ const nextUuid = () => `uuid-${++uuidSeq}`;+ const humanRecord = (text, extra = {}) =>+ ({ type: 'user', uuid: nextUuid(), message: { role: 'user', content: text }, ...extra });+ // Real assistant records carry message.id; a search without one is never credited.+ const toolUseRecord = (id, name, input, extra = {}) => ({+ type: 'assistant',+ uuid: nextUuid(),+ message: { id: `msg_${id}`, role: 'assistant', content: [{ type: 'tool_use', id, name, input }] },+ ...extra+ });+ const toolResultRecord = (id, isError = false, extra = {}) => ({+ type: 'user',+ uuid: nextUuid(),+ message: { role: 'user', content: [{ type: 'tool_result', tool_use_id: id, is_error: isError, content: 'ok' }] },+ ...extra+ });+ const searchRecords = (id, name, input) => [toolUseRecord(id, name, input), toolResultRecord(id)];+ // Like Claude Code, a fixture ends with the pending call's own assistant record and runners pass its id.+ const pendingToolUseIds = new Map();+ const writeTranscript = (records, { pending = true } = {}) => {+ transcriptSeq += 1;+ const file = path.join(transcriptDir, `t-${transcriptSeq}.jsonl`);+ const all = records.slice();+ if (pending) {+ const id = `toolu_pending_${transcriptSeq}`;+ all.push({+ type: 'assistant',+ uuid: nextUuid(),+ message: { id: `msg_pending_${transcriptSeq}`, role: 'assistant', content: [{ type: 'tool_use', id, name: 'Edit', input: {} }] }+ });+ pendingToolUseIds.set(file, id);+ }+ const lines = all.map(r => (typeof r === 'string' ? r : JSON.stringify(r)));+ fs.writeFileSync(file, lines.join('\n') + '\n', 'utf8');+ return file;+ };+ const creditRun = (toolName, toolInput, transcriptPath, env = {}) => {+ const payload = { tool_name: toolName, tool_input: toolInput, cwd: creditRoot };+ if (transcriptPath !== undefined) payload.transcript_path = transcriptPath;+ if (pendingToolUseIds.has(transcriptPath)) payload.tool_use_id = pendingToolUseIds.get(transcriptPath);+ const result = runHook(payload, { ...creditEnv, ...env });+ creditOutputs.push(result.stdout);+ const output = parseOutput(result.stdout);+ const hso = output && output.hookSpecificOutput ? output.hookSpecificOutput : {};+ return { result, decision: hso.permissionDecision, context: hso.additionalContext || '', reason: hso.permissionDecisionReason || '' };+ };+ const creditEdit = (file_path, transcriptPath, env) =>+ creditRun('Edit', { file_path, old_string: 'a', new_string: 'b' }, transcriptPath, env);+ const creditWrite = (file_path, transcriptPath, env) => creditRun('Write', { file_path, content: 'x' }, transcriptPath, env);+ const readState = () => (fs.existsSync(stateFile) ? JSON.parse(fs.readFileSync(stateFile, 'utf8')) : {});+ const gateCase = (name, fn) => {+ clearState();+ if (test(name, fn)) passed++;+ else failed++;+ };+ const assertNotCredited = (out, label) => {+ assert.strictEqual(out.decision, 'deny', `${label}: expected deny`);+ assert.ok(!out.context.includes('Prior search seen'), `${label}: no credit note`);+ };+ const assertCredited = (out, label) => {+ assert.notStrictEqual(out.decision, 'deny', `${label}: must not deny`);+ assert.ok(out.context.includes('Prior search seen in this turn'), `${label}: credit note expected, got ${out.result.stdout}`);+ };++ gateCase('Grep stem in this turn credits the first Edit (note, no denial counted)', () => {+ const t = writeTranscript([+ humanRecord('please fix the widget factory'),+ ...searchRecords('toolu_g1', 'Grep', { pattern: 'widget_factory', path: creditRoot })+ ]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ assertCredited(out, 'Grep stem');+ assert.ok(out.context.includes('(Grep widget_factory, 1 tool call ago)'), out.context);+ assert.ok(out.context.includes(`first-touch check satisfied for ${creditRoot}/src/widget_factory.py`), out.context);+ const state = readState();+ assert.ok(!state.fact_force_denials, 'denial count unchanged');+ assert.strictEqual(state.fact_force_credited, 1, 'credit counted');+ assert.ok(state.checked.includes(stateKey(`${creditRoot}/src/widget_factory.py`)), 'canonical key marked checked');+ const again = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ assert.notStrictEqual(again.decision, 'deny');+ assert.strictEqual(again.context, '', 'already checked: plain allow, no second note');+ assert.strictEqual(readState().fact_force_credited, 1, 'no double credit');+ });++ gateCase('Glob directory credits a Write of a new file there', () => {+ const t = writeTranscript([+ humanRecord('add a component'),+ ...searchRecords('toolu_gl', 'Glob', { pattern: 'src/components/*.tsx', path: creditRoot })+ ]);+ assertCredited(creditWrite(`${creditRoot}/src/components/NewThing.tsx`, t), 'Glob dir');+ assert.ok(!readState().fact_force_denials);+ });++ gateCase('LS directory credits a Write there', () => {+ const t = writeTranscript([humanRecord('add a page'), ...searchRecords('toolu_ls', 'LS', { path: `${creditRoot}/docs` })]);+ assertCredited(creditWrite(`${creditRoot}/docs/brand-new-page.md`, t), 'LS dir');+ });++ gateCase('Bash rg stem credits an Edit', () => {+ const t = writeTranscript([humanRecord('fix it'), ...searchRecords('toolu_b1', 'Bash', { command: 'cd /proj-credit && rg foo_bar src' })]);+ const out = creditEdit(`${creditRoot}/src/foo_bar.js`, t);+ assertCredited(out, 'Bash rg');+ assert.ok(out.context.includes('(Bash rg foo_bar src,'), out.context);+ });++ gateCase('PowerShell Get-ChildItem stem credits an Edit', () => {+ const t = writeTranscript([+ humanRecord('fix it'),+ ...searchRecords('toolu_p1', 'PowerShell', { command: 'Get-ChildItem -Recurse -Filter *foo_bar*' })+ ]);+ assertCredited(creditEdit(`${creditRoot}/src/foo_bar.ps1`, t), 'PowerShell gci');+ });++ gateCase('git grep qualifies, git log does not', () => {+ const ok = writeTranscript([humanRecord('x'), ...searchRecords('toolu_gg', 'Bash', { command: 'git grep -n foo_bar' })]);+ assertCredited(creditEdit(`${creditRoot}/src/foo_bar.js`, ok), 'git grep');+ const no = writeTranscript([humanRecord('x'), ...searchRecords('toolu_gl2', 'Bash', { command: 'git log -- src/baz_qux.js' })]);+ assertNotCredited(creditEdit(`${creditRoot}/src/baz_qux.js`, no), 'git log');+ });++ gateCase('stem only in a non-search shell segment does not credit', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_b2', 'Bash', { command: 'echo foo_bar | grep something; ls' })]);+ assertNotCredited(creditEdit(`${creditRoot}/src/foo_bar.js`, t), 'stem in echo segment');+ });++ gateCase('command substitution in a search command is ambiguous (no credit)', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_b3', 'Bash', { command: 'rg $(echo foo_bar) src' })]);+ assertNotCredited(creditEdit(`${creditRoot}/src/foo_bar.js`, t), 'substitution');+ });++ gateCase('cd before a search disables directory credit (stem still counts)', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_cd', 'Bash', { command: 'cd /elsewhere && ls docs' })]);+ assertNotCredited(creditWrite(`${creditRoot}/docs/brand-new-page.md`, t), 'cd then ls dir');+ });++ gateCase('a search in the same assistant batch as the pending call never credits', () => {+ const batch = (id, name, input) => ({+ type: 'assistant',+ uuid: nextUuid(),+ message: { id: 'msg_batch', role: 'assistant', content: [{ type: 'tool_use', id, name, input }] }+ });+ const t = writeTranscript([+ humanRecord('x'),+ batch('toolu_bg', 'Grep', { pattern: 'widget_factory' }),+ batch('toolu_be', 'Edit', { file_path: `${creditRoot}/src/widget_factory.py` }),+ toolResultRecord('toolu_bg')+ ]);+ const input = { file_path: `${creditRoot}/src/widget_factory.py`, old_string: 'a', new_string: 'b' };+ const same = runHook({ tool_name: 'Edit', tool_use_id: 'toolu_be', cwd: creditRoot, transcript_path: t, tool_input: input }, creditEnv);+ creditOutputs.push(same.stdout);+ assert.strictEqual(parseOutput(same.stdout).hookSpecificOutput.permissionDecision, 'deny', 'same batch denied');+ clearState();+ const later = writeTranscript([+ humanRecord('x'),+ batch('toolu_bg2', 'Grep', { pattern: 'widget_factory' }),+ toolResultRecord('toolu_bg2'),+ { type: 'assistant', uuid: nextUuid(), message: { id: 'msg_next', role: 'assistant', content: [{ type: 'tool_use', id: 'toolu_be2', name: 'Edit', input }] } }+ ]);+ const next = runHook({ tool_name: 'Edit', tool_use_id: 'toolu_be2', cwd: creditRoot, transcript_path: later, tool_input: input }, creditEnv);+ creditOutputs.push(next.stdout);+ assert.ok(next.stdout.includes('Prior search seen'), 'search from an earlier message credits');+ });++ gateCase('no search in the turn -> deny', () => {+ const t = writeTranscript([humanRecord('edit it'), ...searchRecords('toolu_r0', 'Read', { file_path: `${creditRoot}/other.js` })]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ assertNotCredited(out, 'no search');+ assert.strictEqual(readState().fact_force_denials, 1);+ assert.ok(!readState().fact_force_credited, 'no credit counted on deny');+ });++ gateCase('search tool_use without a tool_result (same batch) -> deny', () => {+ const t = writeTranscript([humanRecord('x'), toolUseRecord('toolu_pending', 'Grep', { pattern: 'widget_factory' })]);+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'pending search');+ });++ gateCase('search whose tool_result is_error -> deny', () => {+ const t = writeTranscript([+ humanRecord('x'),+ toolUseRecord('toolu_err', 'Grep', { pattern: 'widget_factory' }),+ toolResultRecord('toolu_err', true)+ ]);+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'errored search');+ });++ gateCase('search before the latest human message -> deny', () => {+ const t = writeTranscript([+ humanRecord('first request'),+ ...searchRecords('toolu_old', 'Grep', { pattern: 'widget_factory' }),+ humanRecord('second request')+ ]);+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'previous turn search');+ });++ gateCase('tool_result user records do not reset the turn', () => {+ const t = writeTranscript([+ humanRecord('x'),+ ...searchRecords('toolu_s1', 'Grep', { pattern: 'widget_factory' }),+ ...searchRecords('toolu_s2', 'Read', { file_path: `${creditRoot}/a.js` }),+ ...searchRecords('toolu_s3', 'Bash', { command: 'npm test' })+ ]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ assertCredited(out, 'tool_results between');+ assert.ok(out.context.includes('3 tool calls ago'), out.context);+ });++ gateCase('isMeta and sidechain user records are not turn boundaries', () => {+ const t = writeTranscript([+ humanRecord('x'),+ ...searchRecords('toolu_m1', 'Grep', { pattern: 'widget_factory' }),+ humanRecord('<system-reminder>meta</system-reminder>', { isMeta: true }),+ humanRecord('subagent prompt', { isSidechain: true })+ ]);+ assertCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'meta/sidechain boundary');+ });++ gateCase('sidechain search records are ignored', () => {+ const t = writeTranscript([+ humanRecord('x'),+ toolUseRecord('toolu_sc', 'Grep', { pattern: 'widget_factory' }, { isSidechain: true }),+ toolResultRecord('toolu_sc', false, { isSidechain: true })+ ]);+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'sidechain search');+ });++ gateCase('Read of the target never credits an Edit', () => {+ const t = writeTranscript([+ humanRecord('x'),+ ...searchRecords('toolu_rd', 'Read', { file_path: `${creditRoot}/src/widget_factory.py` })+ ]);+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'Read target');+ });++ gateCase('directory match does not credit an Edit (stem only)', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_ls2', 'LS', { path: `${creditRoot}/src` })]);+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'LS dir for Edit');+ });++ gateCase('generic stems (index.js) are not credited', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_ix', 'Grep', { pattern: 'index' })]);+ assertNotCredited(creditEdit(`${creditRoot}/src/index.js`, t), 'generic stem');+ });++ gateCase('stems shorter than 4 characters are not credited', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_ab', 'Grep', { pattern: 'abc' })]);+ assertNotCredited(creditEdit(`${creditRoot}/src/abc.js`, t), 'short stem');+ });++ gateCase('missing or non-file transcript -> deny', () => {+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, path.join(transcriptDir, 'nope.jsonl')), 'missing file');+ assertNotCredited(creditEdit(`${creditRoot}/src/other_widget.py`, transcriptDir), 'directory path');+ assertNotCredited(creditEdit(`${creditRoot}/src/third_widget.py`), 'no transcript_path');+ });++ gateCase('garbage lines -> deny; garbage around a valid turn is skipped', () => {+ const junk = writeTranscript(['not json', '{"type":', '{"type":"assistant","message":null}', '[1,2]', 'null']);+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, junk), 'garbage only');+ const mixed = writeTranscript([+ 'garbage {',+ humanRecord('x'),+ '{"broken":',+ ...searchRecords('toolu_gm', 'Grep', { pattern: 'other_widget' }),+ '\u0000\u0001'+ ]);+ assertCredited(creditEdit(`${creditRoot}/src/other_widget.py`, mixed), 'garbage around valid turn');+ });++ const fillerRecords = prefix => {+ const filler = [];+ for (let i = 0; i < 40; i++) {+ const id = `toolu_${prefix}${i}`;+ filler.push(toolUseRecord(id, 'Bash', { command: 'npm test' }));+ filler.push({+ type: 'user',+ message: { role: 'user', content: [{ type: 'tool_result', tool_use_id: id, content: 'y'.repeat(10 * 1024) }] }+ });+ }+ return filler;+ };++ gateCase('tail truncated, no boundary in window, search + result inside -> credited (turn id null)', () => {+ const t = writeTranscript([+ humanRecord('long ago'),+ ...fillerRecords('fill'),+ ...searchRecords('toolu_late', 'Grep', { pattern: 'widget_factory' })+ ]);+ assert.ok(fs.statSync(t).size > 300 * 1024, 'fixture exceeds the tail window');+ const { scanCurrentTurn } = loadDirectHook();+ const scan = scanCurrentTurn(t);+ assert.strictEqual(scan.turnId, null, 'no boundary: no turn id');+ assert.ok(scan.searches.some(s => s.name === 'Grep'), 'search inside the window is collected');+ assertCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'clipped window is the current turn');+ });++ gateCase('tail truncated, search tool_use before the window, result inside -> not credited', () => {+ const t = writeTranscript([+ humanRecord('long ago'),+ toolUseRecord('toolu_early', 'Grep', { pattern: 'widget_factory' }),+ ...fillerRecords('fill2'),+ toolResultRecord('toolu_early')+ ]);+ assert.ok(fs.statSync(t).size > 300 * 1024, 'fixture exceeds the tail window');+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'tool_use outside the window');+ });++ gateCase('whole file scanned with no boundary -> no credit', () => {+ const t = writeTranscript([...searchRecords('toolu_nob', 'Grep', { pattern: 'widget_factory' })]);+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`, t), 'no human record at all');+ });++ gateCase('MultiEdit credits per entry; first uncredited entry is denied', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_me', 'Grep', { pattern: 'alpha_mod' })]);+ const out = creditRun(+ 'MultiEdit',+ {+ edits: [+ { file_path: `${creditRoot}/src/alpha_mod.js`, old_string: 'a', new_string: 'b' },+ { file_path: `${creditRoot}/src/beta_mod.js`, old_string: 'a', new_string: 'b' }+ ]+ },+ t+ );+ assert.strictEqual(out.decision, 'deny');+ assert.ok(out.reason.includes('beta_mod.js'), 'uncredited entry is the one denied');+ const state = readState();+ assert.ok(state.checked.includes(stateKey(`${creditRoot}/src/alpha_mod.js`)), 'credited entry marked checked');+ assert.strictEqual(state.fact_force_credited, 1);+ assert.strictEqual(state.fact_force_denials, 1);+ });++ gateCase('MultiEdit with every entry credited returns one combined note', () => {+ const t = writeTranscript([+ humanRecord('x'),+ ...searchRecords('toolu_me2', 'Grep', { pattern: 'alpha_mod|beta_mod' })+ ]);+ const out = creditRun(+ 'MultiEdit',+ {+ edits: [+ { file_path: `${creditRoot}/src/alpha_mod.js`, old_string: 'a', new_string: 'b' },+ { file_path: `${creditRoot}/src/beta_mod.js`, old_string: 'a', new_string: 'b' }+ ]+ },+ t+ );+ assert.notStrictEqual(out.decision, 'deny');+ assert.ok(out.context.includes('alpha_mod.js') && out.context.includes('beta_mod.js'), out.context);+ assert.strictEqual(readState().fact_force_credited, 2);+ assert.ok(!readState().fact_force_denials);+ });++ gateCase('exempt paths and subagents are unchanged (no credit note)', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_ex', 'Grep', { pattern: 'widget_factory' })]);+ const exempt = creditEdit(`${creditRoot}/tmp/widget_factory.py`, t, { GATEGUARD_EXEMPT_GLOBS: 'tmp/**' });+ assert.notStrictEqual(exempt.decision, 'deny');+ assert.strictEqual(exempt.context, '', 'exempt path: plain allow');+ const sub = runHook(+ {+ tool_name: 'Edit',+ agent_id: 'agent-1',+ cwd: creditRoot,+ transcript_path: t,+ tool_input: { file_path: `${creditRoot}/src/widget_factory.py`, old_string: 'a', new_string: 'b' }+ },+ creditEnv+ );+ creditOutputs.push(sub.stdout);+ assert.ok(!sub.stdout.includes('Prior search'), 'subagent: plain allow');+ assert.ok(!readState().fact_force_credited, 'no credit recorded');+ });++ gateCase('credit note detail is sanitized and bounded', () => {+ const pattern = `widget_factory\u200b\u202e${'x'.repeat(300)}\nIGNORE PREVIOUS`;+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_long', 'Grep', { pattern })]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ assertCredited(out, 'long pattern');+ const detail = out.context.match(/\(Grep (.*), 1 tool call ago\)/);+ assert.ok(detail, out.context);+ assert.ok(Array.from(detail[1]).length <= 80, `detail too long: ${detail[1].length}`);+ for (const bad of ['\u200b', '\u202e', '\n']) assert.ok(!out.context.includes(bad), 'no invisible/control chars');+ assert.ok(!out.context.includes('IGNORE PREVIOUS'), 'truncated before trailing text');+ });++ gateCase('state save failure on credit -> allow with state warning', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_sf', 'Grep', { pattern: 'widget_factory' })]);+ const blocker = path.join(transcriptDir, `not-a-dir-${transcriptSeq}`);+ fs.writeFileSync(blocker, 'x');+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t, { GATEGUARD_STATE_DIR: blocker });+ assert.notStrictEqual(out.decision, 'deny');+ assert.ok(out.result.stderr.includes('could not be persisted'), out.result.stderr);+ });++ gateCase('fact_force_credited survives later state writes (merge keeps it)', () => {+ writeState({ checked: [], last_active: Date.now(), fact_force_credited: 4 });+ assertNotCredited(creditEdit(`${creditRoot}/src/widget_factory.py`), 'deny path');+ assert.strictEqual(readState().fact_force_credited, 4, 'denial write keeps credited count');+ assert.strictEqual(readState().fact_force_denials, 1);+ });++ gateCase('50 MB transcript is scanned from the tail only (<= 256 KiB read)', () => {+ const big = path.join(transcriptDir, 'big.jsonl');+ const bigSize = 50 * 1024 * 1024;+ const tail = [humanRecord('now'), ...searchRecords('toolu_big', 'Grep', { pattern: 'widget_factory' })];+ for (let i = 0; i < 50; i++) tail.push(...searchRecords(`toolu_t${i}`, 'Read', { file_path: `${creditRoot}/f${i}.js` }));+ // Sparse 50 MB body (ftruncate) + the real tail: cheap to build, same shape for the hook.+ const fd = fs.openSync(big, 'w');+ try {+ fs.writeSync(fd, JSON.stringify(humanRecord('ancient')) + '\n');+ fs.ftruncateSync(fd, bigSize);+ fs.writeSync(fd, '\n' + tail.map(r => JSON.stringify(r)).join('\n') + '\n', bigSize);+ } finally {+ fs.closeSync(fd);+ }+ const savedRoot = process.env.CLAUDE_PROJECT_DIR;+ const savedTranscript = process.env.CLAUDE_TRANSCRIPT_PATH;+ const originalOpenSync = fs.openSync;+ const originalReadSync = fs.readSync;+ const bigFds = new Set();+ let bytesRead = 0;+ let result;+ let ms;+ try {+ assert.ok(fs.statSync(big).size > bigSize, 'fixture is > 50 MB');+ const hook = loadDirectHook({ CLAUDE_PROJECT_DIR: creditRoot, CLAUDE_TRANSCRIPT_PATH: '' });+ const payload = {+ tool_name: 'Edit',+ cwd: creditRoot,+ transcript_path: big,+ tool_input: { file_path: `${creditRoot}/src/widget_factory.py`, old_string: 'a', new_string: 'b' }+ };+ fs.openSync = function countingOpenSync(target) {+ const opened = originalOpenSync.apply(fs, arguments);+ if (String(target) === big) bigFds.add(opened);+ return opened;+ };+ fs.readSync = function countingReadSync(readFd) {+ const n = originalReadSync.apply(fs, arguments);+ if (bigFds.has(readFd)) bytesRead += n;+ return n;+ };+ const start = process.hrtime.bigint();+ result = hook.run(payload);+ ms = Number(process.hrtime.bigint() - start) / 1e6;+ } finally {+ fs.openSync = originalOpenSync;+ fs.readSync = originalReadSync;+ if (savedRoot === undefined) delete process.env.CLAUDE_PROJECT_DIR;+ else process.env.CLAUDE_PROJECT_DIR = savedRoot;+ if (savedTranscript === undefined) delete process.env.CLAUDE_TRANSCRIPT_PATH;+ else process.env.CLAUDE_TRANSCRIPT_PATH = savedTranscript;+ fs.rmSync(big, { force: true });+ }+ console.log(` (run() on 50 MB transcript: ${bytesRead} bytes read, ${ms.toFixed(1)} ms)`);+ assert.ok(result && String(result.additionalContext || '').includes('Prior search seen'), 'credited from the tail');+ assert.ok(bigFds.size >= 1 && bytesRead > 0, 'transcript was read through fs.readSync');+ assert.ok(bytesRead <= 256 * 1024, `read ${bytesRead} bytes; must stay within the 256 KiB tail`);+ assert.ok(ms < 1000, `wall-clock backstop: run() took ${ms.toFixed(1)} ms`);+ });++ gateCase('no prior-search credit output carries permissionDecision "allow"', () => {+ assert.ok(creditOutputs.length > 20, 'collected outputs');+ for (const stdout of creditOutputs) {+ const output = parseOutput(stdout);+ const decision = output && output.hookSpecificOutput ? output.hookSpecificOutput.permissionDecision : undefined;+ assert.notStrictEqual(decision, 'allow', stdout);+ }+ });++ // --- Denials name the closest search that did not count ---+ const MISS_PREFIX = 'Closest search this turn did not count';+ const missLine = reason => reason.split('\n').find(line => line.startsWith(MISS_PREFIX)) || '';++ gateCase('a denial names a search that looked outside the file directory', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_o1', 'Grep', { pattern: 'widget_factory', path: `${creditRoot}/docs` })]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ assertNotCredited(out, 'out of scope');+ const line = missLine(out.reason);+ assert.ok(line.includes('(Grep widget_factory)'), line || out.reason);+ assert.ok(line.includes("its search path does not contain this file"), line);+ });++ gateCase('a denial names a search whose filters excluded the file', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_x1', 'Bash', { command: "rg -g '!*.py' widget_factory ." })]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ assertNotCredited(out, 'excluded');+ assert.ok(missLine(out.reason).includes('its filters exclude this file'), out.reason);+ });++ gateCase('a denial names a search sent in the same batch', () => {+ const batch = (id, name, input) => ({+ type: 'assistant',+ uuid: nextUuid(),+ message: { id: 'msg_b4batch', role: 'assistant', content: [{ type: 'tool_use', id, name, input }] }+ });+ const t = writeTranscript([+ humanRecord('x'),+ ...searchRecords('toolu_far', 'Grep', { pattern: 'widget_factory', path: `${creditRoot}/docs` }),+ batch('toolu_sb', 'Grep', { pattern: 'widget_factory' }),+ batch('toolu_se', 'Edit', { file_path: `${creditRoot}/src/widget_factory.py` }),+ toolResultRecord('toolu_sb')+ ], { pending: false });+ const input = { file_path: `${creditRoot}/src/widget_factory.py`, old_string: 'a', new_string: 'b' };+ const result = runHook({ tool_name: 'Edit', tool_use_id: 'toolu_se', cwd: creditRoot, transcript_path: t, tool_input: input }, creditEnv);+ const reason = parseOutput(result.stdout).hookSpecificOutput.permissionDecisionReason;+ const line = missLine(reason);+ assert.ok(line.includes('same batch as this call'), `same batch outranks out of scope: ${line}`);+ });++ gateCase('a denial names a search that only read piped input', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_p1', 'Bash', { command: 'git diff | grep widget_factory' })]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ assertNotCredited(out, 'stdin');+ assert.ok(missLine(out.reason).includes('it searched piped input, not the tree'), out.reason);+ });++ gateCase('a denial names a Read or a non-search command of the file', () => {+ const read = writeTranscript([+ humanRecord('x'),+ toolUseRecord('toolu_r1', 'Read', { file_path: `${creditRoot}/src/widget_factory.py` }),+ toolResultRecord('toolu_r1')+ ]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, read);+ assertNotCredited(out, 'read');+ const line = missLine(out.reason);+ assert.ok(line.includes(`(Read ${creditRoot}/src/widget_factory.py)`), line || out.reason);+ assert.ok(line.includes('only Glob, Grep, LS and shell search commands count'), line);+ clearState();+ const cat = writeTranscript([humanRecord('x'), ...searchRecords('toolu_c1', 'Bash', { command: 'cat src/widget_factory.py' })]);+ assert.ok(missLine(creditEdit(`${creditRoot}/src/widget_factory.py`, cat).reason).includes('(Bash cat src/widget_factory.py)'));+ });++ gateCase('a denial explains that a generic file name never matches', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_g1', 'Grep', { pattern: 'index' })]);+ const out = creditEdit(`${creditRoot}/src/index.py`, t);+ assertNotCredited(out, 'generic');+ assert.ok(missLine(out.reason).includes('this file name is too generic to match a search'), out.reason);+ });++ gateCase('no closest-search line when nothing in the turn mentions the file', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_n1', 'Grep', { pattern: 'unrelated_thing' })]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ assertNotCredited(out, 'unrelated');+ assert.strictEqual(missLine(out.reason), '');+ assert.ok(!out.reason.includes(MISS_PREFIX));+ const none = creditEdit(`${creditRoot}/src/other_file.py`, undefined);+ assert.ok(!none.reason.includes(MISS_PREFIX), 'no transcript, no line');+ });++ gateCase('the closest-search detail is sanitized and bounded', () => {+ const noisy = `widget_factory \u202e\u200b${'z'.repeat(200)}\u0007`;+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_s1', 'Grep', { pattern: noisy, path: `${creditRoot}/docs` })]);+ const out = creditEdit(`${creditRoot}/src/widget_factory.py`, t);+ const line = missLine(out.reason);+ assert.ok(line, out.reason);+ for (const bad of ['\u202e', '\u200b', '\u0007']) assert.ok(!line.includes(bad), `no U+${bad.codePointAt(0).toString(16)}`);+ const detail = line.slice(line.indexOf('(Grep ') + 6, line.indexOf('):'));+ assert.ok(Array.from(detail).length <= 60, `detail bounded: ${detail.length}`);+ assert.ok(detail.endsWith('...'), 'truncation marked');+ });++ gateCase('sensitive targets never get a closest-search line', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_k1', 'Grep', { pattern: 'login_view', path: `${creditRoot}/docs` })]);+ const out = creditEdit(`${creditRoot}/src/auth/login_view.py`, t);+ assertNotCredited(out, 'sensitive');+ assert.ok(!out.reason.includes(MISS_PREFIX), out.reason);+ });++ gateCase('condensed denials and MultiEdit denials carry the closest-search line', () => {+ const t = writeTranscript([humanRecord('x'), ...searchRecords('toolu_m1', 'Grep', { pattern: 'widget_factory', path: `${creditRoot}/docs` })]);+ const condensed = creditEdit(`${creditRoot}/src/widget_factory.py`, t, { GATEGUARD_FACT_FORCE_FULL_DENIALS: '0' });+ assert.ok(!condensed.reason.includes('\n'), 'still one line');+ assert.ok(condensed.reason.includes(`${MISS_PREFIX} (Grep widget_factory)`), condensed.reason);+ clearState();+ const multi = creditRun('MultiEdit', { edits: [{ file_path: `${creditRoot}/src/widget_factory.py`, old_string: 'a', new_string: 'b' }] }, t);+ assert.ok(missLine(multi.reason).includes('(Grep widget_factory)'), multi.reason);+ });++ // --- Exclusion globs never earn search credit ---+ const exclusionTarget = `${creditRoot}/src/widget.py`;+ let exclusionSeq = 0;+ const exclusionTurn = (name, input) => {+ exclusionSeq += 1;+ return writeTranscript([humanRecord('fix the widget'), ...searchRecords(`toolu_r2_${exclusionSeq}`, name, input)]);+ };+ const exclusionDenied = [+ ['Grep glob !widget.py', 'Grep', { pattern: 'TODO', glob: '!widget.py', path: creditRoot }],+ ['Grep glob list with an exclusion', 'Grep', { pattern: 'widget', glob: '*.js,!widget.py', path: creditRoot }],+ ['Grep path-style exclusion covering the target', 'Grep', { pattern: 'widget', glob: '!src/**', path: creditRoot }],+ ['Grep positive basename glob that misses the target', 'Grep', { pattern: 'widget', glob: '*.md', path: creditRoot }],+ ["rg -g '!widget.py'", 'Bash', { command: "rg -g '!widget.py' TODO ." }],+ ['rg --glob=!widget.py', 'Bash', { command: 'rg --glob=!widget.py TODO .' }],+ ['rg --iglob exclusion, other case', 'Bash', { command: "rg --iglob '!WIDGET.py' TODO ." }],+ ['rg -g exclusion of the directory', 'Bash', { command: "rg -g '!src/' widget ." }],+ ['rg -g positive glob that misses the target', 'Bash', { command: "rg -g '*.md' widget ." }],+ ['grep --exclude=widget.py', 'Bash', { command: 'grep -r --exclude=widget.py TODO .' }],+ ['grep --exclude-dir of the target dir', 'Bash', { command: 'grep -rn --exclude-dir=src widget .' }],+ ['grep --exclude-from (unknown exclusions)', 'Bash', { command: 'grep -r --exclude-from=skip.txt widget .' }],+ ['grep --include that misses the target', 'Bash', { command: 'grep -r --include=*.md widget .' }],+ ['find -not -name', 'Bash', { command: 'find . -not -name widget.py' }],+ ['find ! -name', 'Bash', { command: 'find . ! -name widget.py -type f' }],+ ['find -path ... -prune', 'Bash', { command: 'find . -path ./src/widget.py -prune -o -print' }],+ ['find -name that misses the target', 'Bash', { command: 'find . -name widget.md' }],+ ['fd -E widget.py', 'Bash', { command: 'fd -E widget.py py src' }],+ ['fd --exclude widget.py', 'Bash', { command: 'fd --exclude widget.py py .' }],+ ['ls -I widget.py', 'Bash', { command: 'ls -I widget.py src' }],+ ["tree -I 'widget*'", 'Bash', { command: "tree -I 'widget*' src" }],+ ['Get-ChildItem -Exclude', 'PowerShell', { command: 'Get-ChildItem -Recurse -Exclude widget.py' }],+ ['rg brace exclusion', 'Bash', { command: "rg -g '!*.{py,js}' widget ." }],+ ['rg deep directory exclusion', 'Bash', { command: "rg -g '!**/src/**' widget ." }],+ ['rg character-class exclusion', 'Bash', { command: "rg -g '!w[i]dget.py' TODO ." }],+ ['rg exclusion in a short-flag cluster', 'Bash', { command: "rg -ig'!widget.py' TODO ." }],+ ['find negated group', 'Bash', { command: 'find . ! ( -name widget.py -o -name x.py ) -print' }],+ ['git ls-files -x', 'Bash', { command: 'git ls-files -x widget.py src' }]+ ];+ for (const [label, name, input] of exclusionDenied) {+ gateCase(`an exclusion never credits the excluded target (${label})`, () => {+ assertNotCredited(creditEdit(exclusionTarget, exclusionTurn(name, input)), label);+ assert.ok(!readState().fact_force_credited, `${label}: nothing credited`);+ });+ }+ const exclusionCredited = [+ ["rg -g '*.py' widget", 'Bash', { command: "rg -g '*.py' widget ." }],+ ['Grep widget glob *.py', 'Grep', { pattern: 'widget', glob: '*.py', path: creditRoot }],+ ["rg -g '!*.md' widget (exclusion does not cover the target)", 'Bash', { command: "rg -g '!*.md' widget ." }],+ ['Grep widget glob *.{py,js} (braces are one glob)', 'Grep', { pattern: 'widget', glob: '*.{py,js}', path: creditRoot }],+ ['Grep widget glob !node_modules/**', 'Grep', { pattern: 'widget', glob: '!node_modules/**', path: creditRoot }],+ ['grep --exclude-dir=node_modules widget', 'Bash', { command: 'grep -r --exclude-dir=node_modules widget .' }],+ ['find -name widget.py', 'Bash', { command: 'find . -name widget.py' }],+ ['Grep TODO glob widget.py (positive glob names the stem)', 'Grep', { pattern: 'TODO', glob: 'widget.py', path: creditRoot }]+ ];+ for (const [label, name, input] of exclusionCredited) {+ gateCase(`control still credits (${label})`, () => {+ assertCredited(creditEdit(exclusionTarget, exclusionTurn(name, input)), label);+ });+ }++ // --- Bracket classes in filter globs match exactly ---+ const bracketCases = [+ ['include class of a dot does not admit another character', 'src/fooXjs', "rg -g 'foo[.]js' fooXjs .", false],+ ['include class of a dot admits the dot', 'src/widget.py', "rg -g 'widget[.]py' widget .", true],+ ['include negated class admits another character', 'src/widget.py', "rg -g 'widge[!x].py' widget .", true],+ ['include negated class rejects its member', 'src/widget.py', "rg -g 'widge[!t].py' widget .", false],+ ['include caret-negated class rejects its member', 'src/widget.py', "rg -g 'widge[^t].py' widget .", false],+ ['include range admits a member', 'src/widget.py', "rg -g 'widge[r-u].py' widget .", true],+ ['include range rejects a non-member', 'src/widget.py', "rg -g 'widge[a-f].py' widget .", false],+ ['include range is case-insensitive', 'src/widget.py', "rg -g 'widge[R-U].py' widget .", true],+ ['include class with a literal ] first', 'src/widget.py', "rg -g 'widge[]t].py' widget .", true],+ ['Grep include class of a dot does not admit another character', 'src/fooXjs', { pattern: 'fooXjs', glob: 'foo[.]js' }, false],+ ['include with an unclosed class admits nothing', 'src/widget.py', "rg -g 'widget[.py' widget .", false],+ ['include with an empty class admits nothing', 'src/widget.py', "rg -g 'widget[].py' widget .", false],+ ['exclusion class covering the target blocks it', 'src/widget.py', "rg -g '!widge[t].py' widget .", false],+ ['exclusion range covering the target blocks it', 'src/widget.py', "rg -g '!widge[s-u].py' widget .", false],+ ['exclusion class of a dot does not cover another character', 'src/widgetXjs', "rg -g '!widget[.]js' widgetXjs .", true],+ ['exclusion negated class does not cover its member', 'src/widget.py', "rg -g '!widge[!t].py' widget .", true],+ ['exclusion with an unclosed class covers the target', 'src/widget.py', "rg -g '!zzz[' widget .", false],+ ['exclusion with an empty negated class covers the target', 'src/widget.py', "rg -g '!zzz[!]' widget .", false]+ ];+ for (const [label, rel, search, credited] of bracketCases) {+ gateCase(`bracket class: ${label}`, () => {+ const input = typeof search === 'string' ? { command: search } : { ...search, path: creditRoot };+ const out = creditEdit(`${creditRoot}/${rel}`, exclusionTurn(typeof search === 'string' ? 'Bash' : 'Grep', input));+ if (credited) assertCredited(out, label);+ else assertNotCredited(out, label);+ });+ }++ // --- Directory-qualified include globs restrict the search ---+ const longInclude = `${'a'.repeat(300)}*.py`;+ const pathIncludeCases = [+ ['rg include in another directory', 'lib/fooXjs.js', "rg -g 'src/*.js' fooXjs .", false],+ ['rg --glob include in another directory', 'src/widget.py', 'rg --glob=docs/*.py widget .', false],+ ['rg recursive include of another directory', 'src/widget.py', "rg -g 'lib/**' widget .", false],+ ['rg include anchored at the search root', 'vendor/src/widget.py', "rg -g 'src/*.py' widget .", false],+ ['rg include that does not cross a directory', 'src/deep/widget.py', "rg -g 'src/*.py' widget .", false],+ ['rg include relative to a narrower search path', 'src/widget.py', "rg -g 'src/*.py' widget src", false],+ ['Grep include in another directory', 'src/widget.py', { pattern: 'widget', glob: 'lib/*.py' }, false],+ ['grep --include matches base names only', 'src/widget.py', 'grep -r --include=src/*.py widget .', false],+ ['find -name with a slash matches nothing', 'src/widget.py', "find . -name 'src/widget.py'", false],+ ['PowerShell -Include with a path matches nothing', 'src/widget.py', 'Get-ChildItem -Recurse -Include src/widget.py', false],+ ['rg include past the length bound admits nothing', 'src/widget.py', `rg -g '${longInclude}' widget .`, false],+ ['rg include of the target directory', 'src/widget.py', "rg -g 'src/*.py' widget .", true],+ ['rg include with a leading slash', 'src/widget.py', "rg -g '/src/*.py' widget .", true],+ ['rg include with a leading **/', 'vendor/src/widget.py', "rg -g '**/src/*.py' widget .", true],+ ['rg recursive include of the target directory', 'src/deep/widget.py', "rg -g 'src/**' widget .", true],+ ['rg include relative to the search path', 'src/deep/widget.py', "rg -g 'deep/*.py' widget src", true],+ ['Grep include of the target directory', 'src/widget.py', { pattern: 'widget', glob: 'src/**/*.py' }, true]+ ];+ for (const [label, rel, search, credited] of pathIncludeCases) {+ gateCase(`path include: ${label}`, () => {+ const input = typeof search === 'string' ? { command: search } : { ...search, path: creditRoot };+ const tool = typeof search !== 'string' ? 'Grep' : /^Get-ChildItem/.test(search) ? 'PowerShell' : 'Bash';+ const out = creditEdit(`${creditRoot}/${rel}`, exclusionTurn(tool, input));+ if (credited) assertCredited(out, label);+ else assertNotCredited(out, label);+ });+ }++ // --- The search prefilter never hides a search that names the target ---+ const prefilterCases = [+ ["rg wid''get .", true],+ ['rg "wid"get .', true],+ ['rg WIDGET .', true],+ ['rg gadget .', false]+ ];+ for (const [command, credited] of prefilterCases) {+ gateCase(`search prefilter: ${command}`, () => {+ const out = creditEdit(exclusionTarget, exclusionTurn('Bash', { command }));+ if (credited) assertCredited(out, command);+ else assertNotCredited(out, command);+ });+ }++ // --- Every PowerShell spelling of -Exclude is an exclusion ---+ const psExcludeDenied = [+ 'Get-ChildItem -Recurse -ex widget.py',+ 'Get-ChildItem -Recurse -EXC widget.py',+ 'gci -Recurse -Excl widget.py',+ 'Get-ChildItem -Recurse -Exclude:widget.py',+ 'Get-ChildItem -Recurse -exclu:widget.py',+ 'Get-ChildItem -Recurse -Exclude: widget.py',+ 'Get-ChildItem -Recurse -Exclude a.py, widget.py',+ 'Get-ChildItem -Recurse -Exclude a.py ,widget.py',+ 'Get-ChildItem -Recurse -Exclude a.py , widget.py',+ 'Get-ChildItem -Recurse -Exclude a.py,widget.py',+ 'Get-ChildItem -Recurse -Exclude:a.py, widget.py',+ 'Select-String -Pattern TODO -Path src/* -ex widget.py',+ 'sls TODO src/* -Exclude a.py, widget.py',+ 'Get-ChildItem -Recurse -e:widget.py',+ 'Get-ChildItem -Recurse -Nonesuch:widget.py',+ 'Get-ChildItem -Recurse -in widget.py'+ ];+ for (const command of psExcludeDenied) {+ gateCase(`PowerShell exclusion never credits the excluded target (${command})`, () => {+ assertNotCredited(creditEdit(exclusionTarget, exclusionTurn('PowerShell', { command })), command);+ assert.ok(!readState().fact_force_credited, `${command}: nothing credited`);+ });+ }+ const psCredited = [+ 'Get-ChildItem -Path src -Recurse -Filter widget.py',+ 'Get-ChildItem -Recurse -Filt:widget.py',+ 'Get-ChildItem -Recurse -Include a.md, widget.py',+ 'Get-ChildItem -Recurse -Exclude a.md, b.md -Filter widget.py',+ 'Select-String -Pattern widget -Path src/* -Exclude a.md'+ ];+ for (const command of psCredited) {+ gateCase(`PowerShell control still credits (${command})`, () => {+ assertCredited(creditEdit(exclusionTarget, exclusionTurn('PowerShell', { command })), command);+ });+ }+ gateCase('PowerShell include lists spread across tokens still filter the target', () => {+ const command = 'Get-ChildItem -Recurse -Include a.md, widget.md';+ assertNotCredited(creditEdit(exclusionTarget, exclusionTurn('PowerShell', { command })), command);+ });++ // --- Same-turn sibling creations and counters ---+ const projectRoot = fs.realpathSync(fs.mkdtempSync(path.join(tmpRoot, 'gateguard-proj-')));+ const projectEnv = { CLAUDE_PROJECT_DIR: projectRoot, CLAUDE_TRANSCRIPT_PATH: '' };+ const siblingOutputs = [];+ const projectRun = (toolName, toolInput, transcriptPath, env = {}) => {+ const payload = { tool_name: toolName, tool_input: toolInput, cwd: projectRoot };+ if (transcriptPath !== undefined) payload.transcript_path = transcriptPath;+ if (pendingToolUseIds.has(transcriptPath)) payload.tool_use_id = pendingToolUseIds.get(transcriptPath);+ const result = runHook(payload, { ...projectEnv, ...env });+ siblingOutputs.push(result.stdout);+ const output = parseOutput(result.stdout);+ const hso = output && output.hookSpecificOutput ? output.hookSpecificOutput : {};+ return { result, decision: hso.permissionDecision, context: hso.additionalContext || '', reason: hso.permissionDecisionReason || '' };+ };+ const projectWrite = (file_path, transcriptPath, env) => projectRun('Write', { file_path, content: 'x' }, transcriptPath, env);+ const projectEdit = (file_path, transcriptPath, env) =>+ projectRun('Edit', { file_path, old_string: 'a', new_string: 'b' }, transcriptPath, env);+ const assertSibling = (out, label) => {+ assert.notStrictEqual(out.decision, 'deny', `${label}: must not deny (${out.result.stdout})`);+ assert.notStrictEqual(out.decision, 'allow', `${label}: never permissionDecision allow`);+ assert.ok(out.context.includes('[Fact-Forcing Gate] Sibling of '), `${label}: sibling note expected, got ${out.result.stdout}`);+ };+ const assertDeniedNoSibling = (out, label) => {+ assert.strictEqual(out.decision, 'deny', `${label}: expected deny, got ${out.result.stdout}`);+ assert.ok(!out.context.includes('Sibling of'), `${label}: no sibling note`);+ };+ // dir_gates keys are `<class>\u0000<canonicalDir>`.+ const dirGateKey = (dir, cls = 'code') => `${cls}\u0000${stateKey(dir)}`;+ const newTurn = text => {+ const human = humanRecord(text);+ return { human, transcript: writeTranscript([human]) };+ };++ gateCase('checked lookups under a simulated Windows root (both spellings share one key)', () => {+ const winRoot = 'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\gateguard-proj-abc';+ const savedRoot = process.env.CLAUDE_PROJECT_DIR;+ const savedTranscript = process.env.CLAUDE_TRANSCRIPT_PATH;+ let first;+ let second;+ try {+ // Fixed synthetic paths: keys need no real disk, and the target is absent on any host.+ const hook = loadDirectHook({ CLAUDE_PROJECT_DIR: winRoot, CLAUDE_TRANSCRIPT_PATH: '' });+ const write = file_path => hook.run({ tool_name: 'Write', cwd: winRoot, tool_input: { file_path, content: 'x' } });+ first = write(`${winRoot}/src/widgets/w0.js`);+ second = write(`${winRoot}\\src\\widgets\\w1.js`);+ } finally {+ if (savedRoot === undefined) delete process.env.CLAUDE_PROJECT_DIR;+ else process.env.CLAUDE_PROJECT_DIR = savedRoot;+ if (savedTranscript === undefined) delete process.env.CLAUDE_TRANSCRIPT_PATH;+ else process.env.CLAUDE_TRANSCRIPT_PATH = savedTranscript;+ }+ assert.strictEqual(parseOutput(first.stdout).hookSpecificOutput.permissionDecision, 'deny');+ const state = readState();+ if (process.platform === 'win32') {+ assert.ok(second && /Sibling of/.test(String(second.additionalContext || '')), JSON.stringify(second));+ assert.strictEqual(Object.keys(state.dir_gates || {}).length, 1, 'one dir gate for both spellings');+ } else {+ assert.strictEqual(parseOutput(second.stdout).hookSpecificOutput.permissionDecision, 'deny', JSON.stringify(second));+ assert.deepStrictEqual(Object.keys(state.dir_gates || {}), [], 'no dir gate for an unresolvable directory');+ }+ assert.ok(state.checked.includes(stateKey(`${winRoot}/src/widgets/w0.js`)), `w0 checked: ${JSON.stringify(state.checked)}`);+ assert.ok(state.checked.includes(stateKey(`${winRoot}\\src\\widgets\\w1.js`)), 'w1 checked (backslash spelling)');+ });++ gateCase('8 new files in one dir in the same turn -> 1 deny + 7 sibling notes', () => {+ const { human, transcript } = newTurn('scaffold the widgets');+ const first = projectWrite(`${projectRoot}/src/widgets/w0.js`, transcript);+ assertDeniedNoSibling(first, 'first file');+ for (let i = 1; i < 8; i++) {+ const out = projectWrite(`${projectRoot}/src/widgets/w${i}.js`, transcript);+ assertSibling(out, `file ${i}`);+ assert.strictEqual(+ out.context,+ `[Fact-Forcing Gate] Sibling of ${projectRoot}/src/widgets/w0.js (gated earlier at denial #1 this session); proceeding without a repeat denial.`+ );+ }+ const state = readState();+ assert.strictEqual(state.fact_force_denials, 1, 'one denial');+ assert.strictEqual(state.sibling_allows, 7, 'seven sibling allows');+ assert.ok(state.checked.includes(stateKey(`${projectRoot}/src/widgets/w7.js`)), 'sibling marked checked (canonical key)');+ const gate = state.dir_gates[dirGateKey(`${projectRoot}/src/widgets`)];+ assert.ok(gate, 'dir gate keyed by canonical dir');+ assert.strictEqual(gate.turn, human.uuid);+ assert.strictEqual(gate.first, `${projectRoot}/src/widgets/w0.js`);+ assert.strictEqual(gate.ordinal, 1);+ assert.ok(typeof gate.at === 'number' && gate.at > 0);+ const retry = projectWrite(`${projectRoot}/src/widgets/w3.js`, transcript);+ assert.strictEqual(retry.decision, undefined);+ assert.strictEqual(retry.context, '', 'already checked: plain allow');+ });++ gateCase('a different directory gets its own denial', () => {+ const { transcript } = newTurn('x');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/a/one.js`, transcript), 'dir a');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/b/two.js`, transcript), 'dir b');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/a/nested/three.js`, transcript), 'subdirectory is not a sibling');+ assertSibling(projectWrite(`${projectRoot}/src/b/four.js`, transcript), 'dir b sibling');+ });++ gateCase('the next human turn in the same dir is denied again', () => {+ const turn1 = humanRecord('first');+ const t1 = writeTranscript([turn1]);+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/w/one.js`, t1), 'turn 1');+ const t2 = writeTranscript([turn1, humanRecord('second')]);+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/w/two.js`, t2), 'turn 2');+ assertSibling(projectWrite(`${projectRoot}/src/w/three.js`, t2), 'turn 2 sibling');+ assert.ok(projectWrite(`${projectRoot}/src/w/three.js`, t2).context === '', 'checked');+ assert.strictEqual(readState().dir_gates[dirGateKey(`${projectRoot}/src/w`)].ordinal, 2, 'latest denial ordinal recorded');+ });++ gateCase('a Write over an existing file in the gated dir is still denied (per-file)', () => {+ fs.mkdirSync(path.join(projectRoot, 'src', 'e'), { recursive: true });+ fs.writeFileSync(path.join(projectRoot, 'src', 'e', 'existing.js'), 'old');+ const { transcript } = newTurn('x');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/e/new.js`, transcript), 'new file');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/e/existing.js`, transcript), 'existing file');+ assert.ok(!readState().sibling_allows, 'no sibling allow');+ });++ gateCase('an existing-file Write denial does not open a dir gate', () => {+ fs.mkdirSync(path.join(projectRoot, 'src', 'f'), { recursive: true });+ fs.writeFileSync(path.join(projectRoot, 'src', 'f', 'present.js'), 'old');+ const { transcript } = newTurn('x');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/f/present.js`, transcript), 'existing file');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/f/brand_new.js`, transcript), 'new file after existing');+ });++ gateCase('fs error resolving the target -> treated as existing (no collapse)', () => {+ fs.mkdirSync(path.join(projectRoot, 'src'), { recursive: true });+ fs.writeFileSync(path.join(projectRoot, 'src', 'blocker'), 'not a dir');+ const { transcript } = newTurn('x');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/blocker/one.js`, transcript), 'ENOTDIR first');+ if (process.platform === 'win32') return;+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/blocker/two.js`, transcript), 'ENOTDIR second');+ assert.ok(!Object.keys(readState().dir_gates || {}).some(k => k.endsWith(`${projectRoot}/src/blocker`)), 'no dir gate recorded');+ });++ gateCase('Edit and MultiEdit in a gated dir are never collapsed', () => {+ const { transcript } = newTurn('x');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/m/one.js`, transcript), 'write');+ assertDeniedNoSibling(projectEdit(`${projectRoot}/src/m/two.js`, transcript), 'edit sibling');+ const multi = projectRun(+ 'MultiEdit',+ { edits: [{ file_path: `${projectRoot}/src/m/three.js`, old_string: 'a', new_string: 'b' }] },+ transcript+ );+ assertDeniedNoSibling(multi, 'multiedit sibling');+ assertSibling(projectWrite(`${projectRoot}/src/m/four.js`, transcript), 'write sibling still works');+ });++ gateCase('an Edit denial does not open a dir gate', () => {+ const { transcript } = newTurn('x');+ assertDeniedNoSibling(projectEdit(`${projectRoot}/src/n/one.js`, transcript), 'edit');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/n/two.js`, transcript), 'write after edit');+ });++ gateCase('instruction-class siblings are each denied', () => {+ const { transcript } = newTurn('x');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/skills/foo/one.md`, transcript), 'instruction 1');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/skills/foo/two.md`, transcript), 'instruction 2');+ assert.ok(!Object.keys(readState().dir_gates || {}).some(k => k.endsWith(`${projectRoot}/skills/foo`)), 'instruction denial opens no dir gate');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/skills/foo/helper.js`, transcript), 'code file after instruction denials');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/skills/foo/three.md`, transcript), 'instruction after a code dir gate');+ });++ gateCase('no transcript -> sibling within 120 s, denied after', () => {+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/nt/one.js`), 'first');+ assertSibling(projectWrite(`${projectRoot}/src/nt/two.js`), 'within window');+ const state = readState();+ assert.strictEqual(state.dir_gates[dirGateKey(`${projectRoot}/src/nt`)].turn, null, 'turn null without transcript');+ state.dir_gates[dirGateKey(`${projectRoot}/src/nt`)].at = Date.now() - 121000;+ writeState(state);+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/nt/three.js`), 'after window');+ });++ gateCase('a turn-scoped gate is not matched without a turn id, and vice versa', () => {+ const { transcript } = newTurn('x');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/tt/one.js`, transcript), 'turn gate');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/tt/two.js`), 'no-turn call vs turn gate');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/tt/three.js`, transcript), 'turn call vs null-turn gate');+ });++ gateCase('a prior search credits before the sibling rule', () => {+ const human = humanRecord('x');+ const t = writeTranscript([human]);+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/cr/one.js`, t), 'first');+ const t2 = writeTranscript([human, ...searchRecords('toolu_c4g', 'Grep', { pattern: 'widget_factory' })]);+ const out = projectWrite(`${projectRoot}/src/cr/widget_factory.js`, t2);+ assert.ok(out.context.includes('Prior search seen in this turn'), out.result.stdout);+ assert.ok(!out.context.includes('Sibling of'));+ assert.ok(!readState().sibling_allows, 'credit is not a sibling allow');+ });++ gateCase('counters record class names for denial, credit and sibling events', () => {+ const human = humanRecord('x');+ const t = writeTranscript([human, ...searchRecords('toolu_c4c', 'Grep', { pattern: 'widget_factory' })]);+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/cc/one.js`, t), 'code deny');+ assertSibling(projectWrite(`${projectRoot}/src/cc/two.js`, t), 'code sibling');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/docs/guide_page.md`, t), 'prose deny');+ assertDeniedNoSibling(projectEdit(`${projectRoot}/tests/a.test.js`, t), 'test deny');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/skills/x/SKILL.md`, t), 'instruction deny');+ assertCredited(projectEdit(`${projectRoot}/lib/widget_factory.js`, t), 'code credit');+ const state = readState();+ assert.deepStrictEqual(state.denials_by_class, { code: 1, prose: 1, test: 1, instruction: 1 });+ assert.deepStrictEqual(state.credited_by_class, { code: 1 });+ assert.strictEqual(state.sibling_allows, 1);+ assert.strictEqual(state.fact_force_denials, 4);+ assert.strictEqual(state.fact_force_credited, 1);+ const json = JSON.stringify({ d: state.denials_by_class, c: state.credited_by_class });+ assert.ok(!json.includes('/'), 'counters hold class names only, never paths');+ });++ gateCase('a state file without the counter fields loads and gains them', () => {+ writeState({ checked: ['/elsewhere/x.js'], last_active: Date.now(), fact_force_denials: 2 });+ const { transcript } = newTurn('x');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/old/one.js`, transcript), 'deny');+ assertSibling(projectWrite(`${projectRoot}/src/old/two.js`, transcript), 'sibling');+ const state = readState();+ assert.ok(state.checked.includes('/elsewhere/x.js'));+ assert.strictEqual(state.fact_force_denials, 3);+ assert.strictEqual(state.dir_gates[dirGateKey(`${projectRoot}/src/old`)].ordinal, 3);+ assert.deepStrictEqual(state.denials_by_class, { code: 1 });+ assert.deepStrictEqual(state.credited_by_class, {});+ assert.strictEqual(state.sibling_allows, 1);+ assert.strictEqual(state.fact_force_credited, 0);+ });++ gateCase('malformed counter fields are tolerated (treated as empty/zero)', () => {+ writeState({+ checked: [],+ last_active: Date.now(),+ dir_gates: { [dirGateKey(`${projectRoot}/src/bad`)]: 'nope', [dirGateKey(`${projectRoot}/src/bad2`)]: { at: 'x' }, other: [1, 2] },+ denials_by_class: [5],+ credited_by_class: { code: -3, prose: 'x', test: 2 },+ sibling_allows: 'many'+ });+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/bad/one.js`), 'malformed gate ignored');+ assertSibling(projectWrite(`${projectRoot}/src/bad/two.js`), 'fresh gate works');+ const state = readState();+ assert.deepStrictEqual(Object.keys(state.dir_gates), [dirGateKey(`${projectRoot}/src/bad`)]);+ assert.deepStrictEqual(state.denials_by_class, { code: 1 });+ assert.deepStrictEqual(state.credited_by_class, { test: 2 });+ assert.strictEqual(state.sibling_allows, 1);+ writeState({ checked: [], last_active: Date.now(), dir_gates: 'garbage', denials_by_class: null, sibling_allows: -1 });+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/bad3/one.js`), 'garbage dir_gates');+ assert.strictEqual(readState().sibling_allows, 0);+ });++ gateCase('dir_gates is capped at 50 (oldest evicted)', () => {+ const now = Date.now();+ const gates = {};+ for (let i = 0; i < 50; i++) gates[dirGateKey(`/cap/d${i}`)] = { turn: null, at: now - 100000 + i, first: `/cap/d${i}/f.js`, ordinal: i + 1 };+ writeState({ checked: [], last_active: now, dir_gates: gates });+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/cap/one.js`), 'new gate');+ const state = readState();+ assert.strictEqual(Object.keys(state.dir_gates).length, 50, 'capped at 50');+ assert.ok(state.dir_gates[dirGateKey(`${projectRoot}/src/cap`)], 'newest kept');+ assert.ok(!state.dir_gates[dirGateKey('/cap/d0')], 'oldest evicted');+ assert.ok(state.dir_gates[dirGateKey('/cap/d1')], 'second-oldest kept');+ });++ gateCase('dir_gate first path is stored sanitized', () => {+ const file_path = `${projectRoot}/src/san/evil\u202ename\u200b.js`;+ assertDeniedNoSibling(projectWrite(file_path), 'deny');+ const gate = readState().dir_gates[dirGateKey(`${projectRoot}/src/san`)];+ assert.ok(gate && !gate.first.includes('\u202e') && !gate.first.includes('\u200b'), JSON.stringify(gate));+ const out = projectWrite(`${projectRoot}/src/san/ok.js`);+ assertSibling(out, 'sibling');+ assert.ok(!out.context.includes('\u202e'));+ });++ gateCase('merges dir_gates and counters written by another process during save', () => {+ const hook = loadDirectHook({ CLAUDE_PROJECT_DIR: projectRoot, CLAUDE_TRANSCRIPT_PATH: '' });+ const originalMkdirSync = fs.mkdirSync;+ const now = Date.now();+ let injected = false;+ fs.mkdirSync = function patchedMkdirSync(target) {+ const result = originalMkdirSync.apply(fs, arguments);+ if (!injected && path.resolve(String(target)) === path.resolve(stateDir)) {+ injected = true;+ fs.writeFileSync(+ stateFile,+ JSON.stringify({+ checked: ['/src/concurrent.js'],+ last_active: now,+ fact_force_denials: 7,+ dir_gates: {+ [dirGateKey('/other/dir')]: { turn: 'u-other', at: now, first: '/other/dir/a.js', ordinal: 7 },+ [dirGateKey(`${projectRoot}/src/conc`)]: { turn: 'u-old', at: 1, first: 'stale', ordinal: 1 }+ },+ denials_by_class: { code: 5, prose: 2 },+ credited_by_class: { test: 3 },+ sibling_allows: 9+ }),+ 'utf8'+ );+ }+ return result;+ };+ const savedRoot = process.env.CLAUDE_PROJECT_DIR;+ const savedTranscript = process.env.CLAUDE_TRANSCRIPT_PATH;+ try {+ const result = hook.run({ tool_name: 'Write', cwd: projectRoot, tool_input: { file_path: `${projectRoot}/src/conc/new.js`, content: 'x' } });+ assert.strictEqual(parseOutput(result.stdout).hookSpecificOutput.permissionDecision, 'deny');+ } finally {+ fs.mkdirSync = originalMkdirSync;+ if (savedRoot === undefined) delete process.env.CLAUDE_PROJECT_DIR;+ else process.env.CLAUDE_PROJECT_DIR = savedRoot;+ if (savedTranscript === undefined) delete process.env.CLAUDE_TRANSCRIPT_PATH;+ else process.env.CLAUDE_TRANSCRIPT_PATH = savedTranscript;+ }+ const persisted = JSON.parse(fs.readFileSync(stateFile, 'utf8'));+ assert.ok(persisted.dir_gates[dirGateKey('/other/dir')], 'concurrent dir gate preserved');+ const mine = persisted.dir_gates[dirGateKey(`${projectRoot}/src/conc`)];+ assert.ok(mine && mine.first === `${projectRoot}/src/conc/new.js` && mine.turn === null, 'newer in-memory gate wins');+ assert.deepStrictEqual(persisted.denials_by_class, { code: 5, prose: 2 }, 'per-key max(disk, mem)');+ assert.deepStrictEqual(persisted.credited_by_class, { test: 3 });+ assert.strictEqual(persisted.sibling_allows, 9);+ assert.ok(persisted.checked.includes('/src/concurrent.js'));+ });++ gateCase('sibling state save failure -> allow with state warning', () => {+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/sf/one.js`), 'deny');+ const hook = loadDirectHook({ CLAUDE_PROJECT_DIR: projectRoot, CLAUDE_TRANSCRIPT_PATH: '' });+ const originalWriteFileSync = fs.writeFileSync;+ fs.writeFileSync = function patchedWriteFileSync(target) {+ if (String(target).includes('.json.tmp.')) throw Object.assign(new Error('EIO'), { code: 'EIO' });+ return originalWriteFileSync.apply(fs, arguments);+ };+ const savedRoot = process.env.CLAUDE_PROJECT_DIR;+ const savedTranscript = process.env.CLAUDE_TRANSCRIPT_PATH;+ let result;+ try {+ result = hook.run({ tool_name: 'Write', cwd: projectRoot, tool_input: { file_path: `${projectRoot}/src/sf/two.js`, content: 'x' } });+ } finally {+ fs.writeFileSync = originalWriteFileSync;+ if (savedRoot === undefined) delete process.env.CLAUDE_PROJECT_DIR;+ else process.env.CLAUDE_PROJECT_DIR = savedRoot;+ if (savedTranscript === undefined) delete process.env.CLAUDE_TRANSCRIPT_PATH;+ else process.env.CLAUDE_TRANSCRIPT_PATH = savedTranscript;+ }+ assert.ok(!result.stdout, 'no deny and no sibling note');+ assert.ok(String(result.stderr).includes('could not be persisted'), JSON.stringify(result));+ });++ gateCase('clipped window keeps the promptId turn id; a sibling gate from earlier in the turn still matches', () => {+ const { scanCurrentTurn } = loadDirectHook();+ const pid = 'prompt-heavy';+ const human = humanRecord('scaffold', { promptId: pid });+ const early = writeTranscript([human]);+ assert.strictEqual(scanCurrentTurn(early).turnId, pid);+ const gateDir = `${projectRoot}/src/heavy`;+ assertDeniedNoSibling(projectWrite(`${gateDir}/one.js`, early), 'first file, boundary in window');+ assert.strictEqual(readState().dir_gates[dirGateKey(gateDir)].turn, pid, 'gate recorded under the promptId');+ const filler = fillerRecords('pid').map(r => (r.type === 'user' ? { ...r, promptId: pid } : r));+ const late = writeTranscript([human, ...filler]);+ assert.ok(fs.statSync(late).size > 300 * 1024, 'boundary scrolled out of the tail');+ assert.strictEqual(scanCurrentTurn(late).turnId, pid, 'turn id stable after the turn outgrows the tail');+ assertSibling(projectWrite(`${gateDir}/two.js`, late), 'sibling after the boundary left the window');+ const next = writeTranscript([humanRecord('next', { promptId: 'prompt-next' })]);+ assertDeniedNoSibling(projectWrite(`${gateDir}/three.js`, next), 'next prompt is a new turn');+ });++ gateCase('a new human turn uses its own promptId even when a late tool_result carries the previous one', () => {+ const turnOneHuman = humanRecord('turn one', { promptId: 'P1' });+ const t1 = writeTranscript([turnOneHuman]);+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/late-result/aaa1.py`, t1), 'turn P1');+ const late = { ...toolResultRecord('toolu_n3late'), promptId: 'P1' };+ const t2 = writeTranscript([turnOneHuman, humanRecord('turn two', { promptId: 'P2' }), toolUseRecord('toolu_n3late', 'Bash', { command: 'true' }), late]);+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/late-result/aaa2.py`, t2), 'turn P2 with a late P1 tool_result');+ assertSibling(projectWrite(`${projectRoot}/src/late-result/aaa3.py`, t2), 'sibling within turn P2');+ assert.strictEqual(readState().dir_gates[dirGateKey(`${projectRoot}/src/late-result`)].turn, 'P2', 'gate recorded under the boundary promptId');+ });++ gateCase('a transcript that yields no turn id never falls back to the no-turn 120 s rule', () => {+ const asDir = fs.mkdtempSync(path.join(tmpRoot, 'gateguard-transcript-dir-'));+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/null-scan/one.js`, asDir), 'transcript path is a directory (scan null)');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/null-scan/two.js`, asDir), 'no collapse with a null scan');+ const noBoundary = writeTranscript([toolUseRecord('toolu_n4x', 'Bash', { command: 'true' }), toolResultRecord('toolu_n4x')]);+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/no-boundary/one.js`, noBoundary), 'whole file without a boundary (turn id null)');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/no-boundary/two.js`, noBoundary), 'no collapse without a turn id');+ const missing = path.join(tmpRoot, 'gateguard-missing-transcript.jsonl');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/missing-transcript/one.js`, missing), 'missing transcript file');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/missing-transcript/two.js`, missing), 'no collapse with a missing transcript');+ const malformed = path.join(tmpRoot, `gateguard-malformed-transcript-${process.pid}.jsonl`);+ fs.writeFileSync(malformed, [humanRecord('t1', { promptId: 'M1' }), humanRecord('t2', { promptId: 'M2' })].map(r => JSON.stringify(r)).join('\n') + '\n{"type":"assistant"}\n');+ assertDeniedNoSibling(projectWrite(`${projectRoot}/src/malformed-record/one.js`, malformed), 'malformed assistant record');+ assertSibling(projectWrite(`${projectRoot}/src/malformed-record/two.js`, malformed), 'malformed record skipped: same turn M2 still collapses');+ assert.strictEqual(readState().dir_gates[dirGateKey(`${projectRoot}/src/malformed-record`)].turn, 'M2');+ fs.rmSync(asDir, { recursive: true, force: true });+ fs.rmSync(malformed, { force: true });+ });++ gateCase('no sibling-collapse output carries permissionDecision "allow"', () => {+ assert.ok(siblingOutputs.length > 20, 'collected outputs');+ for (const stdout of siblingOutputs) {+ const output = parseOutput(stdout);+ const decision = output && output.hookSpecificOutput ? output.hookSpecificOutput.permissionDecision : undefined;+ assert.notStrictEqual(decision, 'allow', stdout);+ }+ });++ // --- Prior-search credit: scope and matching rules ---+ const scopeRoot = fs.realpathSync(fs.mkdtempSync(path.join(tmpRoot, 'gateguard-scope-proj-')));+ // Shell fixtures use forward slashes: bash treats unquoted backslashes as escapes.+ const scopeRootSh = scopeRoot.replace(/\\/g, '/');+ for (const dir of ['src/handlers', 'handlers', 'f']) fs.mkdirSync(path.join(scopeRoot, dir), { recursive: true });+ for (const file of ['src/payment.py', 'src/auth.py', 'src/payment_service.py', 'top.py']) {+ fs.writeFileSync(path.join(scopeRoot, file), 'x');+ }+ const scopeEnv = { CLAUDE_PROJECT_DIR: scopeRoot, CLAUDE_TRANSCRIPT_PATH: '' };+ const scopeOutputs = [];+ let scopeSession = 0;+ // Each call gets a fresh session unless the case passes session_id explicitly.+ const scopeRun = (toolName, toolInput, transcriptPath, extra = {}) => {+ scopeSession += 1;+ const payload = { tool_name: toolName, tool_input: toolInput, cwd: scopeRoot, transcript_path: transcriptPath, session_id: `scope-${scopeSession}` };+ if (pendingToolUseIds.has(transcriptPath)) payload.tool_use_id = pendingToolUseIds.get(transcriptPath);+ Object.assign(payload, extra);+ if (payload.tool_use_id === null) delete payload.tool_use_id;+ const result = runHook(payload, scopeEnv);+ scopeOutputs.push(result.stdout);+ const output = parseOutput(result.stdout);+ const hso = output && output.hookSpecificOutput ? output.hookSpecificOutput : {};+ return { result, decision: hso.permissionDecision, context: hso.additionalContext || '', reason: hso.permissionDecisionReason || '' };+ };+ const scopePath = rel => `${scopeRoot}/${rel}`;+ const scopeEdit = (rel, t, extra) => scopeRun('Edit', { file_path: scopePath(rel), old_string: 'a', new_string: 'b' }, t, extra);+ const scopeWrite = (rel, t, extra) => scopeRun('Write', { file_path: scopePath(rel), content: 'x' }, t, extra);+ const scopeTurn = (...records) => writeTranscript([humanRecord('fix'), ...records]);+ const bashSearch = (id, command) => searchRecords(id, 'Bash', { command });+ const assertScopeDenied = (out, label) => {+ assert.strictEqual(out.decision, 'deny', `${label}: expected deny, got ${out.result.stdout}`);+ assert.ok(!out.context.includes('Prior search seen'), `${label}: no credit note`);+ };+ const assertScopeCredited = (out, label) => {+ assert.notStrictEqual(out.decision, 'deny', `${label}: must not deny (${out.result.stdout})`);+ assert.ok(out.context.includes('Prior search seen in this turn'), `${label}: credit note expected, got ${out.result.stdout}`);+ };+ const assistantMessage = (id, messageId, name, input) => ({+ type: 'assistant',+ uuid: nextUuid(),+ message: { id: messageId, role: 'assistant', content: [{ type: 'tool_use', id, name, input }] }+ });+ const toolResult = (id, extra) => ({+ type: 'user',+ uuid: nextUuid(),+ message: { role: 'user', content: [{ type: 'tool_result', tool_use_id: id, content: 'ok', ...extra }] }+ });++ gateCase('a directory listing does not credit a Write that overwrites an existing file', () => {+ assertScopeDenied(scopeWrite('src/payment.py', scopeTurn(...searchRecords('toolu_h1a', 'LS', { path: 'src' }))), 'LS src -> overwrite payment.py');+ assertScopeDenied(scopeWrite('top.py', scopeTurn(...bashSearch('toolu_h1b', 'grep -rn TODO .'))), 'grep -rn TODO . -> overwrite top.py');+ assertScopeDenied(scopeWrite('src/handlers/../payment.py', scopeTurn(...searchRecords('toolu_h1c', 'Glob', { pattern: 'src/*.py' }))), 'Glob src/*.py -> overwrite');+ });++ gateCase('shell patterns and flag values are not directories', () => {+ assertScopeDenied(scopeWrite('handlers/zzzz_new.py', scopeTurn(...bashSearch('toolu_h2a', 'grep -r handlers src'))), 'grep pattern as dir');+ assertScopeDenied(scopeWrite('f/qqqq_new.py', scopeTurn(...bashSearch('toolu_h2b', 'find . -type f -name x'))), 'find -type value as dir');+ assertScopeDenied(scopeWrite('handlers/rg_new.py', scopeTurn(...bashSearch('toolu_h2c', 'rg -g handlers TODO src'))), 'rg -g value as dir');+ assertScopeDenied(scopeWrite('handlers/sls_new.py', scopeTurn(...searchRecords('toolu_h2d', 'PowerShell', { command: 'Select-String handlers src/*.py' }))), 'sls pattern');+ });++ gateCase('bare cwd operands and missing directories give no directory credit', () => {+ assertScopeDenied(scopeWrite('zzz_new.py', scopeTurn(...bashSearch('toolu_h2e', 'grep -rn TODO .'))), 'grep -rn TODO .');+ assertScopeDenied(scopeWrite('nothere/new_mod.py', scopeTurn(...bashSearch('toolu_h2f', 'ls nothere'))), 'ls of a missing dir');+ assertScopeDenied(scopeWrite('top.py/new_mod.py', scopeTurn(...bashSearch('toolu_h2g', 'ls top.py'))), 'ls of a file, not a dir');+ });++ gateCase('Glob with no literal prefix, or no glob characters, names no directory', () => {+ assertScopeDenied(scopeWrite('brandnew.py', scopeTurn(...searchRecords('toolu_h2h', 'Glob', { pattern: '**/*' }))), 'Glob **/*');+ assertScopeDenied(scopeWrite('otherzz.py', scopeTurn(...searchRecords('toolu_h2i', 'Glob', { pattern: 'qqq.txt' }))), 'Glob qqq.txt');+ assertScopeDenied(scopeWrite('src/handlers/brand_new.py', scopeTurn(...searchRecords('toolu_h2j', 'Glob', { pattern: 'src/handlers/x.py' }))), 'Glob file lookup');+ });++ gateCase('stems match on word boundaries only', () => {+ assertScopeDenied(scopeEdit('src/auth.py', scopeTurn(...searchRecords('toolu_m1a', 'Grep', { pattern: 'author' }))), 'author vs auth');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_m1b', 'Grep', { pattern: 'TODO', glob: '**/payments/**' }))), 'payments glob');+ assertScopeCredited(scopeEdit('src/auth.py', scopeTurn(...searchRecords('toolu_m1c', 'Grep', { pattern: 'def auth(' }))), 'auth( boundary');+ });++ gateCase('shell segments that only read stdin never credit', () => {+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...bashSearch('toolu_m1d', 'echo payment | grep payment'))), 'echo | grep');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...bashSearch('toolu_m1e', 'cat README | grep -c payment'))), 'cat | grep -c');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...bashSearch('toolu_m1f', 'echo payment | rg payment'))), 'piped rg with no path');+ });++ gateCase('a shell segment that only reads the target is not a search', () => {+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...bashSearch('toolu_m1g', 'ls src/payment.py'))), 'ls target');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...bashSearch('toolu_m1h', 'grep -n . src/payment.py'))), 'grep . target');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...bashSearch('toolu_m1i', 'grep -n payment ./src/payment.py'))), 'grep stem in target only');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_m1j', 'Grep', { pattern: 'payment', path: 'src/payment.py' }))), 'Grep path = target');+ });++ gateCase('Grep/Glob/LS with an explicit path only credit targets inside it', () => {+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_m1k', 'Grep', { pattern: 'payment', path: '/nonexistent/elsewhere' }))), 'Grep elsewhere');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_m1l', 'Glob', { pattern: 'payment.md', path: '/nonexistent-dir' }))), 'Glob elsewhere');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_m1m', 'Grep', { pattern: 'TODO', path: 'x', glob: 'src/payments/**' }))), 'glob in another directory');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_m1n', 'Glob', { pattern: 'lib/**/payment*' }))), 'Glob prefix elsewhere');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_m1o', 'Glob', { pattern: 'payment.md', path: scopeRoot }))), 'file lookup in an ancestor dir');+ assertScopeDenied(scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_m1p', 'Glob', { pattern: 'src/payment.py' }))), 'lookup of the target itself');+ assertScopeCredited(scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_m1q', 'Glob', { pattern: 'src/payment.md' }))), 'file lookup beside the target');+ });++ gateCase('same-batch searches are excluded when the pending call cannot be located', () => {+ const edit = scopePath(`src/payment.py`);+ const sameBatch = writeTranscript(+ [humanRecord('fix'), assistantMessage('g1', 'msg_B', 'Grep', { pattern: 'payment' }), assistantMessage('e1', 'msg_B', 'Edit', { file_path: edit }), toolResult('g1')],+ { pending: false }+ );+ assertScopeDenied(scopeEdit('src/payment.py', sameBatch), 'no tool_use_id: newest message excluded');+ assertScopeDenied(scopeEdit('src/payment.py', sameBatch, { tool_use_id: 'e1' }), 'tool_use_id located');+ const notYet = writeTranscript([humanRecord('fix'), assistantMessage('g2', 'msg_C', 'Grep', { pattern: 'payment' }), toolResult('g2')], { pending: false });+ assertScopeDenied(scopeEdit('src/payment.py', notYet, { tool_use_id: 'e2' }), 'tool_use_id not in transcript yet');+ const noMsgId = writeTranscript([+ humanRecord('fix'),+ { type: 'assistant', uuid: nextUuid(), message: { role: 'assistant', content: [{ type: 'tool_use', id: 'g3', name: 'Grep', input: { pattern: 'payment' } }] } },+ toolResult('g3')+ ]);+ assertScopeDenied(scopeEdit('src/payment.py', noMsgId), 'search without message.id');+ const earlier = writeTranscript(+ [humanRecord('fix'), assistantMessage('g4', 'msg_D', 'Grep', { pattern: 'payment' }), toolResult('g4'), assistantMessage('e4', 'msg_E', 'Edit', { file_path: edit })],+ { pending: false }+ );+ assertScopeCredited(scopeEdit('src/payment.py', earlier), 'no tool_use_id: search in an older message still credits');+ });++ gateCase('a boundary without uuid yields a hashed turn id; siblings do not cross turns', () => {+ const { scanCurrentTurn } = loadDirectHook();+ const noUuid = text => ({ type: 'user', message: { role: 'user', content: text } });+ const t1 = writeTranscript([noUuid('turn one')], { pending: false });+ const t2 = writeTranscript([noUuid('turn one'), noUuid('turn TWO new request')], { pending: false });+ const id1 = scanCurrentTurn(t1).turnId;+ const id2 = scanCurrentTurn(t2).turnId;+ assert.ok(/^h:[0-9a-f]{16}$/.test(id1), `hashed turn id, got ${id1}`);+ assert.notStrictEqual(id1, id2, 'different boundary lines, different turn ids');+ const w1 = scopeWrite('src/hashed-turn/hhh1.py', t1, { tool_use_id: null, session_id: 'hashed-turn-session' });+ assert.strictEqual(w1.decision, 'deny', w1.result.stdout);+ const w2 = scopeWrite('src/hashed-turn/hhh2.py', t2, { tool_use_id: null, session_id: 'hashed-turn-session' });+ assert.strictEqual(w2.decision, 'deny', `turn 2 sibling must be denied: ${w2.result.stdout}`);+ });++ gateCase('compaction is a turn boundary', () => {+ const pre = [humanRecord('orig task'), ...searchRecords('toolu_l2a', 'Grep', { pattern: 'payment' })];+ const summary = { type: 'user', uuid: nextUuid(), isCompactSummary: true, message: { role: 'user', content: 'Summary: ...' } };+ assertScopeDenied(scopeEdit('src/payment.py', writeTranscript([...pre, { type: 'system', subtype: 'compact_boundary' }, summary])), 'summary');+ assertScopeDenied(scopeEdit('src/payment.py', writeTranscript([...pre, { type: 'system', uuid: 'cb-1', subtype: 'compact_boundary' }])), 'boundary only');+ const { scanCurrentTurn } = loadDirectHook();+ assert.strictEqual(scanCurrentTurn(writeTranscript([...pre, summary], { pending: false })).turnId, summary.uuid, 'summary uuid is the turn id');+ });++ gateCase('error results are recognised without a boolean is_error', () => {+ const errored = extra => scopeTurn(toolUseRecord('toolu_l3', 'Grep', { pattern: 'payment' }), toolResult('toolu_l3', extra));+ assertScopeDenied(scopeEdit('src/payment.py', errored({ is_error: 'true', content: 'Error: denied' })), 'is_error "true"');+ assertScopeDenied(scopeEdit('src/payment.py', errored({ content: '<tool_use_error>Permission denied</tool_use_error>' })), 'string content');+ assertScopeDenied(scopeEdit('src/payment.py', errored({ content: [{ type: 'text', text: ' <tool_use_error>x</tool_use_error>' }] })), 'text block');+ });++ gateCase('a tool_use id seen more than once in the turn never qualifies', () => {+ const t = writeTranscript([+ humanRecord('x'),+ assistantMessage('g', 'msg_1', 'Grep', { pattern: 'unrelated' }),+ toolResult('g'),+ assistantMessage('g', 'msg_2', 'Grep', { pattern: 'payment' })+ ]);+ assertScopeDenied(scopeEdit('src/payment.py', t), 'duplicate id');+ });++ gateCase('a cd anywhere in the turn disables shell directory credit (stem counts with a known base)', () => {+ const t = scopeTurn(...bashSearch('toolu_l7a', 'cd src'), ...bashSearch('toolu_l7b', 'ls handlers'));+ assertScopeDenied(scopeWrite('handlers/new_mod.py', t), 'cd then ls handlers');+ const control = scopeTurn(...bashSearch('toolu_l7c', 'ls handlers'));+ assertScopeCredited(scopeWrite('handlers/new_mod2.py', control), 'control without cd');+ const stem = scopeTurn(...bashSearch('toolu_l7d', 'cd src'), ...bashSearch('toolu_l7e', 'rg -n payment ..'));+ assertScopeDenied(scopeEdit('src/payment.py', stem), 'relative operand after a cd in another call');+ const absolute = scopeTurn(...bashSearch('toolu_l7f', 'cd src'), ...bashSearch('toolu_l7g', `rg -n payment ${scopeRootSh}/src`));+ assertScopeCredited(scopeEdit('src/payment.py', absolute), 'stem after cd with an absolute operand');+ });++ gateCase('documented search forms still credit', () => {+ assertScopeCredited(scopeEdit('src/payment.py', scopeTurn(...bashSearch('toolu_p1', 'rg -n payment src'))), 'rg -n payment src');+ assertScopeCredited(scopeEdit('src/payment_service.py', scopeTurn(...searchRecords('toolu_p2', 'Grep', { pattern: 'payment_service', path: 'src' }))), 'Grep path src');+ assertScopeCredited(scopeWrite('src/handlers/new_handler.py', scopeTurn(...searchRecords('toolu_p3', 'Glob', { pattern: 'src/handlers/*.py' }))), 'Glob dir');+ assertScopeCredited(scopeWrite('src/handlers/ls_new.py', scopeTurn(...searchRecords('toolu_p4', 'LS', { path: 'src/handlers' }))), 'LS dir');+ assertScopeCredited(scopeWrite('src/handlers/x_new.py', scopeTurn(...bashSearch('toolu_p5', 'find src/handlers -name "*.py"'))), 'find dir');+ assertScopeCredited(scopeEdit('src/auth.py', scopeTurn(...bashSearch('toolu_p6', 'grep -r . -e auth'))), '-e supplies the pattern');+ assertScopeCredited(scopeWrite('src/handlers/grep_new.py', scopeTurn(...bashSearch('toolu_p7', 'grep -rn --include "*.py" TODO src/handlers/'))), 'grep dir');+ });++ gateCase('a shell search only credits targets inside what it searched', () => {+ const edit = (id, command) => scopeEdit('src/payment.py', scopeTurn(...bashSearch(id, command)));+ assertScopeDenied(edit('toolu_n1a', 'grep -rn payment /usr/share/doc'), 'grep -rn outside the project');+ assertScopeDenied(edit('toolu_n1b', 'rg payment docs'), 'rg in another directory');+ assertScopeDenied(edit('toolu_n1c', 'find /etc -name payment'), 'find outside the project');+ assertScopeDenied(edit('toolu_n1d', 'rg payment < /etc/hostname'), 'rg reading stdin via <');+ assertScopeDenied(edit('toolu_n1e', 'rg payment </etc/hostname'), 'rg reading stdin via <file');+ assertScopeDenied(edit('toolu_n1f', 'grep -rn payment 0< /etc/hostname'), 'grep with only an input redirection');+ assertScopeDenied(edit('toolu_n1g', 'rg payment</etc/hostname'), 'redirection glued to the pattern');+ assertScopeDenied(edit('toolu_n1h', 'fd payment /etc'), 'fd outside the project');+ assertScopeDenied(edit('toolu_n1i', 'cd /elsewhere && rg payment src'), 'relative operand after an absolute cd');+ assertScopeDenied(edit('toolu_n1j', 'git grep -n payment -- docs'), 'git grep limited to another directory');+ assertScopeDenied(+ scopeEdit('src/payment.py', scopeTurn(...searchRecords('toolu_n1k', 'PowerShell', { command: 'Select-String -Path docs/*.md -Pattern payment' }))),+ 'Select-String -Path in another directory'+ );+ });++ gateCase('scoped shell searches still credit (positive controls)', () => {+ const edit = (id, command) => scopeEdit('src/payment.py', scopeTurn(...bashSearch(id, command)));+ assertScopeCredited(edit('toolu_n1p1', 'rg -n payment src'), 'rg -n payment src');+ assertScopeCredited(edit('toolu_n1p2', 'grep -rn payment .'), 'grep -rn payment . (cwd scope)');+ assertScopeCredited(edit('toolu_n1p3', 'find src -name "payment*"'), 'find src');+ assertScopeCredited(edit('toolu_n1p4', 'rg payment'), 'rg with no operand searches the cwd');+ assertScopeCredited(edit('toolu_n1p5', 'find . -name "payment*"'), 'find .');+ assertScopeCredited(edit('toolu_n1p6', 'rg payment docs src'), 'one of several operands contains the target');+ assertScopeCredited(edit('toolu_n1p7', 'rg -n payment src/payment.py src/auth.py'), 'the target itself among other operands');+ assertScopeCredited(edit('toolu_n1p8', 'rg payment src < /dev/null'), 'operand before an input redirection');+ assertScopeCredited(edit('toolu_n1p9', `cd ${scopeRootSh} && rg payment src`), 'relative operand after an absolute cd into the project');+ assertScopeCredited(edit('toolu_n1p10', `grep -rn payment ${scopeRootSh}/src`), 'absolute operand');+ assertScopeCredited(edit('toolu_n1p11', 'rg payment src/*.py'), 'glob operand scopes to its literal directory');+ });++ gateCase('no credit-scoping output carries permissionDecision "allow"', () => {+ assert.ok(scopeOutputs.length > 30, 'collected outputs');+ for (const stdout of scopeOutputs) {+ const output = parseOutput(stdout);+ const decision = output && output.hookSpecificOutput ? output.hookSpecificOutput.permissionDecision : undefined;+ assert.notStrictEqual(decision, 'allow', stdout);+ }+ });++ // --- Sibling collapse keyed by target class ---+ // The project root sits under an ancestor `tests/` on purpose: `src/a.py` must still be code.+ const classBase = fs.realpathSync(fs.mkdtempSync(path.join(tmpRoot, 'gateguard-class-')));+ const classRoot = path.join(classBase, 'tests', 'proj');+ fs.mkdirSync(classRoot, { recursive: true });+ const classEnv = { CLAUDE_PROJECT_DIR: classRoot, CLAUDE_TRANSCRIPT_PATH: '' };+ const classOutputs = [];+ const classPath = rel => `${classRoot}/${rel}`;+ const classRun = (toolName, toolInput, transcriptPath) => {+ const payload = { tool_name: toolName, tool_input: toolInput, cwd: classRoot };+ if (transcriptPath !== undefined) payload.transcript_path = transcriptPath;+ if (pendingToolUseIds.has(transcriptPath)) payload.tool_use_id = pendingToolUseIds.get(transcriptPath);+ const result = runHook(payload, classEnv);+ classOutputs.push(result.stdout);+ const output = parseOutput(result.stdout);+ const hso = output && output.hookSpecificOutput ? output.hookSpecificOutput : {};+ return { result, decision: hso.permissionDecision, context: hso.additionalContext || '', reason: hso.permissionDecisionReason || '' };+ };+ const classWrite = (rel, t) => classRun('Write', { file_path: classPath(rel), content: 'x' }, t);+ const classEdit = (rel, t) => classRun('Edit', { file_path: classPath(rel), old_string: 'a', new_string: 'b' }, t);+ const scaffoldTurn = () => writeTranscript([humanRecord('scaffold it')]);+ const classGateKey = (cls, relDir) => `${cls}\u0000${stateKey(`${classRoot}/${relDir}`)}`;+ const assertClassDenied = (out, label) => {+ assert.strictEqual(out.decision, 'deny', `${label}: expected deny, got ${out.result.stdout}`);+ assert.ok(!out.context.includes('Sibling of'), `${label}: no sibling note`);+ };+ const assertClassSibling = (out, label) => {+ assert.notStrictEqual(out.decision, 'deny', `${label}: must not deny (${out.result.stdout})`);+ assert.ok(out.context.includes('[Fact-Forcing Gate] Sibling of '), `${label}: sibling note expected, got ${out.result.stdout}`);+ };++ gateCase('a code denial does not collapse other classes in the same dir', () => {+ const t = scaffoldTurn();+ assertClassDenied(classWrite('src/newmod.py', t), 'code first');+ assertClassDenied(classWrite('src/.env.production', t), 'config sibling of code');+ assertClassDenied(classWrite('src/newmod.test.py', t), 'test sibling of code');+ assertClassDenied(classWrite('src/README.md', t), 'prose sibling of code');+ assertClassDenied(classWrite('src/settings.yaml', t), 'config sibling of code');+ assertClassDenied(classWrite('src/.env.local', t), '.env sibling of code');+ assertClassSibling(classWrite('src/other.py', t), 'code sibling of code still collapses');+ assertClassSibling(classWrite('src/other.test.py', t), 'test sibling of test collapses');+ assertClassSibling(classWrite('src/CHANGES.md', t), 'prose sibling of prose collapses');+ });++ gateCase('harness, instruction and config paths never collapse', () => {+ const t = scaffoldTurn();+ assertClassDenied(classWrite('.github/notes.md', t), '.github prose');+ assertClassDenied(classWrite('.github/more.md', t), '.github prose never collapses');+ const instructionOut = classWrite('.github/copilot-instructions.md', t);+ assertClassDenied(instructionOut, 'copilot-instructions');+ assert.ok(instructionOut.reason.includes('harness/loader'), 'copilot-instructions.md is instruction class');+ assertClassDenied(classWrite('READMEX.md', t), 'root prose');+ assertClassDenied(classWrite('AGENT.md', t), 'AGENT.md');+ assertClassDenied(classWrite('.cursorrules', t), '.cursorrules');+ assertClassDenied(classWrite('.windsurfrules', t), '.windsurfrules');+ assertClassDenied(classWrite('.mcp.json', t), '.mcp.json');+ assertClassSibling(classWrite('NOTES.md', t), 'ordinary prose sibling at root still collapses');+ assertClassDenied(classWrite('.claude/hooks/a.sh', t), '.claude/hooks');+ assertClassDenied(classWrite('.claude/hooks/evil.sh', t), '.claude/hooks sibling');+ assertClassDenied(classWrite('.husky/pre-commit', t), '.husky');+ assertClassDenied(classWrite('.husky/pre-push', t), '.husky sibling');+ assertClassDenied(classWrite('config/app.yaml', t), 'config');+ assertClassDenied(classWrite('config/db.yaml', t), 'config sibling');+ assertClassDenied(classWrite('lib/.hidden.js', t), 'dotfile');+ assertClassDenied(classWrite('lib/.other.js', t), 'dotfile sibling');+ const gates = readState().dir_gates || {};+ for (const key of Object.keys(gates)) {+ assert.ok(!/\.github|\.claude|\.husky|^config|^instruction/.test(key), `no gate recorded for ${JSON.stringify(key)}`);+ }+ });++ gateCase('classification uses the project-relative path (ancestor tests/ dir, worktrees)', () => {+ const t = scaffoldTurn();+ const code = classWrite('src/a.py', t);+ assertClassDenied(code, 'code');+ assert.ok(code.reason.includes('Name the file(s) and line(s) that will call this new file'), 'code questions');+ const state = readState();+ assert.deepStrictEqual(state.denials_by_class, { code: 1 }, 'counter uses the same class as the questions');+ assert.ok(state.dir_gates[classGateKey('code', 'src')], `gate key uses the same class: ${JSON.stringify(Object.keys(state.dir_gates))}`);+ assert.ok(classEdit('lib/b.py', t).reason.includes('List the call sites in this file or its module'), 'Edit code questions');+ fs.mkdirSync(classPath('.claude/worktrees/feat-x'), { recursive: true });+ fs.writeFileSync(classPath('.claude/worktrees/feat-x/.git'), 'gitdir: ../../../.git/worktrees/feat-x\n');+ const wt = classWrite('.claude/worktrees/feat-x/docs/guide.md', t);+ assertClassDenied(wt, 'worktree prose');+ assert.ok(wt.reason.includes('supersedes or duplicates'), `worktree .md is prose: ${wt.reason}`);+ assertClassSibling(classWrite('.claude/worktrees/feat-x/docs/guide2.md', t), 'worktree prose sibling');+ const wtHook = classWrite('.claude/worktrees/feat-x/.claude/hooks/x.md', t);+ assertClassDenied(wtHook, 'worktree harness');+ assert.ok(wtHook.reason.includes('which harness loads this file'), `worktree .claude/hooks/*.md is instruction: ${wtHook.reason}`);+ assertClassDenied(classWrite('.claude/worktrees/feat-x/.claude/hooks/y.md', t), 'worktree harness sibling');+ });++ gateCase('sibling note wording and class-prefixed gate key', () => {+ const t = scaffoldTurn();+ assertClassDenied(classWrite('src/w/one.js', t), 'first');+ const out = classWrite('src/w/two.js', t);+ assertClassSibling(out, 'sibling');+ assert.strictEqual(+ out.context,+ `[Fact-Forcing Gate] Sibling of ${classRoot}/src/w/one.js (gated earlier at denial #1 this session); proceeding without a repeat denial.`+ );+ const gates = readState().dir_gates;+ assert.deepStrictEqual(Object.keys(gates), [classGateKey('code', 'src/w')]);+ });++ gateCase('a future `at` or a zero ordinal is never honored', () => {+ const now = Date.now();+ const gate = (at, ordinal, turn = null) => ({ turn, at, first: '/forged.js', ordinal });+ writeState({+ checked: [],+ last_active: now,+ dir_gates: {+ [classGateKey('code', 'src/fut')]: gate(now + 3600 * 1000, 1),+ [classGateKey('code', 'src/zero')]: gate(now - 1000, 0),+ [classGateKey('code', 'src/ok')]: gate(now - 1000, 1)+ }+ });+ assertClassDenied(classWrite('src/fut/a.js'), 'future at (no turn)');+ assertClassDenied(classWrite('src/zero/a.js'), 'ordinal 0');+ assertClassSibling(classWrite('src/ok/a.js'), 'control: valid gate still matches');+ const human = humanRecord('x');+ const t = writeTranscript([human]);+ writeState({ checked: [], last_active: now, dir_gates: { [classGateKey('code', 'src/futt')]: gate(now + 3600 * 1000, 1, human.uuid) } });+ assertClassDenied(classWrite('src/futt/a.js', t), 'future at (same turn)');+ });++ gateCase('old-shape dir_gates keys (no class prefix) are ignored and dropped', () => {+ const now = Date.now();+ writeState({ checked: [], last_active: now, dir_gates: { [`${classRoot}/src/legacy`]: { turn: null, at: now - 1000, first: 'forged', ordinal: 1 } } });+ assertClassDenied(classWrite('src/legacy/a.js'), 'old-shape gate ignored');+ const keys = Object.keys(readState().dir_gates);+ assert.deepStrictEqual(keys, [classGateKey('code', 'src/legacy')], 'old-shape key dropped, new key recorded');+ writeState({ checked: [], last_active: now, dir_gates: { [`bogus\u0000${classRoot}/src/b`]: { turn: null, at: now - 1000, first: 'x', ordinal: 1 } } });+ assertClassDenied(classWrite('src/b/a.js'), 'unknown class prefix ignored');+ });++ gateCase('prototype keys in state are rejected and never pollute', () => {+ const now = Date.now();+ const legacy = classGateKey('code', 'src/pp');+ fs.mkdirSync(stateDir, { recursive: true });+ fs.writeFileSync(+ stateFile,+ '{"checked":[],"last_active":' + now + ',"dir_gates":{"__proto__":{"turn":null,"at":1,"first":"x","ordinal":1},' ++ '"constructor":{"turn":null,"at":' + (now - 1000) + ',"first":"x","ordinal":1},' ++ '"prototype":{"turn":null,"at":' + (now - 1000) + ',"first":"x","ordinal":1},' ++ JSON.stringify(legacy) + ':{"turn":null,"at":' + (now - 1000) + ',"first":"forged","ordinal":"NaN"}},' ++ '"denials_by_class":{"__proto__":{"polluted":1},"constructor":5,"prototype":4,"code":7},' ++ '"credited_by_class":{"constructor":2},"sibling_allows":0}',+ 'utf8'+ );+ assertClassDenied(classWrite('src/pp/a.js'), 'NaN ordinal gate not honored');+ assert.strictEqual({}.polluted, undefined, 'Object.prototype not polluted');+ const raw = fs.readFileSync(stateFile, 'utf8');+ const state = JSON.parse(raw);+ for (const field of ['dir_gates', 'denials_by_class', 'credited_by_class']) {+ for (const bad of ['__proto__', 'constructor', 'prototype']) {+ assert.ok(!Object.prototype.hasOwnProperty.call(state[field], bad), `${field} drops ${bad}`);+ }+ }+ assert.strictEqual(state.denials_by_class.code, 8, 'code counter incremented from 7');+ assert.deepStrictEqual(state.credited_by_class, {});+ });++ gateCase('second writes into dot-directories are denied', () => {+ const t = scaffoldTurn();+ for (const [a, b] of [+ ['.devcontainer/a.sh', '.devcontainer/post-create.sh'],+ ['.githooks/a', '.githooks/pre-commit'],+ ['.kiro/steering/a.md', '.kiro/steering/b.md'],+ ['.clinerules/a.md', '.clinerules/b.md'],+ ['.idea/a.xml', '.idea/run.xml'],+ ['.continue/prompts/a.prompt', '.continue/prompts/b.prompt']+ ]) {+ assertClassDenied(classWrite(a, t), `${a} first`);+ assertClassDenied(classWrite(b, t), `${b} second`);+ }+ assert.deepStrictEqual(Object.keys(readState().dir_gates || {}), [], 'no dir gate recorded for any dot-directory');+ });++ gateCase('a symlinked directory is screened (and keyed) by its real location', () => {+ const t = scaffoldTurn();+ fs.mkdirSync(classPath('.claude/hooks'), { recursive: true });+ fs.mkdirSync(classPath('src/realdir'), { recursive: true });+ try {+ fs.symlinkSync(classPath('.claude/hooks'), classPath('src/tools'), 'dir');+ fs.symlinkSync(classPath('src/realdir'), classPath('src/aliasdir'), 'dir');+ } catch (e) {+ if (process.platform === 'win32' && e.code === 'EPERM') return; // no symlink privilege+ throw e;+ }+ assertClassDenied(classWrite('src/tools/a.sh', t), 'first write through the symlink');+ assertClassDenied(classWrite('src/tools/evil.sh', t), 'second write lands in .claude/hooks');+ assertClassDenied(classWrite('src/realdir/one.js', t), 'real dir first');+ assertClassSibling(classWrite('src/aliasdir/two.js', t), 'alias of the same real dir shares its gate');+ assert.deepStrictEqual(Object.keys(readState().dir_gates), [classGateKey('code', 'src/realdir')], 'gate keyed by the real directory');+ });++ gateCase('a .claude/worktrees/<name>/ prefix without a .git is not stripped', () => {+ const t = scaffoldTurn();+ fs.mkdirSync(classPath('.claude/worktrees/fake/src'), { recursive: true });+ assertClassDenied(classWrite('.claude/worktrees/fake/src/a.py', t), 'fake worktree first');+ assertClassDenied(classWrite('.claude/worktrees/fake/src/b.py', t), 'fake worktree sibling');+ assertClassDenied(classWrite('.claude/worktrees/fake/a.sh', t), 'fake worktree root first');+ assertClassDenied(classWrite('.claude/worktrees/fake/statusline.sh', t), 'fake worktree root sibling');+ const md = classWrite('.claude/worktrees/fake/docs/g.md', t);+ assertClassDenied(md, 'fake worktree .md');+ assert.ok(md.reason.includes('which harness loads this file') || md.reason.includes('harness/loader'), `stays instruction: ${md.reason}`);+ });++ gateCase('no class-keyed collapse output carries permissionDecision "allow"', () => {+ assert.ok(classOutputs.length > 30, 'collected outputs');+ for (const stdout of classOutputs) {+ const output = parseOutput(stdout);+ const decision = output && output.hookSpecificOutput ? output.hookSpecificOutput.permissionDecision : undefined;+ assert.notStrictEqual(decision, 'allow', stdout);+ }+ });++ // --- Opt-in session cap on first-touch denials ---+ const capEnvName = 'GATEGUARD_FACT_FORCE_MAX_DENIALS';+ const capOutputs = [];+ const capRun = (toolName, toolInput, transcriptPath, cap) => {+ const out = projectRun(toolName, toolInput, transcriptPath, cap === undefined ? {} : { [capEnvName]: cap });+ capOutputs.push(out.result.stdout);+ return out;+ };+ const capAssertPassThrough = (out, toolName, label) => {+ const output = parseOutput(out.result.stdout);+ assert.ok(output, `${label}: valid JSON`);+ assert.ok(!output.hookSpecificOutput, `${label}: pass-through expected, got ${out.result.stdout}`);+ assert.strictEqual(output.tool_name, toolName, `${label}: pass-through preserves input`);+ };+ const capWarning = value =>+ `[Fact-Forcing Gate] ignoring malformed ${capEnvName}=${value}; the denial cap is not active.`;+ // Same-process run with stderr captured; restores the env var that loadDirectHook sets.+ const capInProcess = (fn) => {+ const saved = process.env[capEnvName];+ const originalStderrWrite = process.stderr.write.bind(process.stderr);+ const captured = [];+ process.stderr.write = chunk => {+ const s = typeof chunk === 'string' ? chunk : chunk.toString();+ if (s.includes(capEnvName)) captured.push(s.replace(/\n$/, ''));+ return true;+ };+ try {+ return fn(captured);+ } finally {+ process.stderr.write = originalStderrWrite;+ if (saved === undefined) delete process.env[capEnvName];+ else process.env[capEnvName] = saved;+ }+ };+ const capDirectEdit = (hook, file) =>+ parseOutput(hook.run(JSON.stringify({ tool_name: 'Edit', cwd: projectRoot, tool_input: { file_path: `${projectRoot}/${file}` } })).stdout);++ for (const malformed of ['3.9', '+3', 'abc', '-1', '3oops', '0x3', ' 3 oops', '99999999999999999999']) {+ gateCase(`malformed cap ${JSON.stringify(malformed)} warns once and leaves the gate uncapped`, () => {+ writeState({ checked: [], last_active: Date.now(), fact_force_denials: 9 });+ capInProcess(captured => {+ const hook = loadDirectHook({ ...projectEnv, [capEnvName]: malformed });+ const first = capDirectEdit(hook, 'src/cap-malformed-a.py');+ const second = capDirectEdit(hook, 'src/cap-malformed-b.py');+ assert.strictEqual(first.hookSpecificOutput.permissionDecision, 'deny', 'uncapped: first denied');+ assert.strictEqual(second.hookSpecificOutput.permissionDecision, 'deny', 'uncapped: second denied');+ assert.deepStrictEqual(captured, [capWarning(malformed.trim())], 'exactly one warning with the sanitized value');+ });+ assert.ok(!readState().cap_allows, 'no cap allow recorded');+ });+ }++ gateCase('a different malformed cap warns again; valid, empty and unset values never warn', () => {+ capInProcess(captured => {+ const hook = loadDirectHook({ ...projectEnv, [capEnvName]: 'abc' });+ capDirectEdit(hook, 'src/cap-warn-1.py');+ process.env[capEnvName] = 'xyz';+ capDirectEdit(hook, 'src/cap-warn-2.py');+ capDirectEdit(hook, 'src/cap-warn-3.py');+ assert.deepStrictEqual(captured, [capWarning('abc'), capWarning('xyz')]);+ process.env[capEnvName] = '50';+ capDirectEdit(hook, 'src/cap-warn-4.py');+ process.env[capEnvName] = '';+ capDirectEdit(hook, 'src/cap-warn-5.py');+ delete process.env[capEnvName];+ capDirectEdit(hook, 'src/cap-warn-6.py');+ assert.strictEqual(captured.length, 2, `no further warnings: ${JSON.stringify(captured)}`);+ });+ });++ gateCase('the warning value is sanitized and bounded', () => {+ capInProcess(captured => {+ const hook = loadDirectHook({ ...projectEnv, [capEnvName]: `3\u202e\u200b${'9'.repeat(200)}x` });+ capDirectEdit(hook, 'src/cap-warn-sanitize.py');+ assert.strictEqual(captured.length, 1, JSON.stringify(captured));+ assert.ok(!/[\u202e\u200b]/.test(captured[0]), 'no invisible characters');+ assert.ok(captured[0].length < 200, `bounded: ${captured[0].length}`);+ });+ });++ gateCase('a malformed cap also warns from the spawned hook (stderr), stdout still denies', () => {+ const out = capRun('Edit', { file_path: `${projectRoot}/src/cap-spawn.py` }, undefined, 'abc');+ assert.strictEqual(out.decision, 'deny');+ assert.ok(out.result.stderr.includes(capWarning('abc')), out.result.stderr);+ });++ gateCase('surrounding whitespace is trimmed (" 2 " caps at 2, no warning)', () => {+ writeState({ checked: [], last_active: Date.now(), fact_force_denials: 2 });+ const out = capRun('Edit', { file_path: `${projectRoot}/src/cap-trim.py` }, undefined, ' 2 ');+ capAssertPassThrough(out, 'Edit', 'trimmed cap');+ assert.ok(!out.result.stderr.includes(capEnvName), out.result.stderr);+ });++ gateCase('a cap pass marks the target checked, counts cap_allows, never counts a denial', () => {+ writeState({ checked: [], last_active: Date.now(), fact_force_denials: 2 });+ const a = `${projectRoot}/src/cap-counted-a.py`;+ capAssertPassThrough(capRun('Edit', { file_path: a }, undefined, '2'), 'Edit', 'first past cap');+ let state = readState();+ assert.strictEqual(state.cap_allows, 1, 'cap_allows counted');+ assert.strictEqual(state.fact_force_denials, 2, 'denials unchanged');+ assert.ok(state.checked.includes(stateKey(a)), 'cap-passed target marked checked');+ capAssertPassThrough(capRun('Edit', { file_path: a }, undefined, '2'), 'Edit', 'retry');+ assert.strictEqual(readState().cap_allows, 1, 'a retry of a checked target is not recounted');+ capAssertPassThrough(capRun('Write', { file_path: `${projectRoot}/src/cap-counted-b.py`, content: 'x' }, undefined, '2'), 'Write', 'second');+ state = readState();+ assert.strictEqual(state.cap_allows, 2);+ assert.strictEqual(state.fact_force_denials, 2);+ });++ gateCase('cap_allows loads from old or malformed state and survives later writes', () => {+ writeState({ checked: [], last_active: Date.now(), fact_force_denials: 1, cap_allows: 'garbage' });+ capRun('Edit', { file_path: `${projectRoot}/src/cap-old-a.py` }, undefined, '1');+ assert.strictEqual(readState().cap_allows, 1, 'malformed counter treated as zero');+ writeState({ ...readState(), cap_allows: 7 });+ capRun('Edit', { file_path: `${projectRoot}/src/cap-old-b.py` }, undefined, '1');+ assert.strictEqual(readState().cap_allows, 8, 'increments the persisted count');+ capRun('Edit', { file_path: `${projectRoot}/src/cap-old-c.py` });+ assert.strictEqual(readState().cap_allows, 8, 'kept across an unrelated state write');+ });++ gateCase('cap 0 passes the first new path without a denial', () => {+ capAssertPassThrough(capRun('Edit', { file_path: `${projectRoot}/src/cap-zero.py` }, undefined, '0'), 'Edit', 'cap 0');+ const state = readState();+ assert.ok(!state.fact_force_denials, 'no denial counted');+ assert.strictEqual(state.cap_allows, 1);+ });++ gateCase('credit and sibling allows run before the cap and do not consume it', () => {+ const t = writeTranscript([+ humanRecord('build the gadget and fix the widget factory'),+ ...searchRecords('toolu_cap_g1', 'Grep', { pattern: 'widget_factory', path: projectRoot }),+ ...searchRecords('toolu_cap_g2', 'Grep', { pattern: 'gadget_maker', path: projectRoot })+ ]);+ const credited = capRun('Edit', { file_path: `${projectRoot}/src/widget_factory.py`, old_string: 'a', new_string: 'b' }, t, '1');+ assert.ok(credited.context.includes('Prior search seen in this turn'), credited.result.stdout);+ assert.ok(!readState().fact_force_denials, 'credit consumes no denial');+ const first = capRun('Write', { file_path: `${projectRoot}/src/capgen/g0.js`, content: 'x' }, t, '1');+ assert.strictEqual(first.decision, 'deny', 'the one denial the cap allows');+ const sibling = capRun('Write', { file_path: `${projectRoot}/src/capgen/g1.js`, content: 'x' }, t, '1');+ assert.ok(sibling.context.includes('[Fact-Forcing Gate] Sibling of '), `sibling note, not a cap pass: ${sibling.result.stdout}`);+ const creditedAfterCap = capRun('Edit', { file_path: `${projectRoot}/src/gadget_maker.py`, old_string: 'a', new_string: 'b' }, t, '1');+ assert.ok(creditedAfterCap.context.includes('Prior search seen in this turn'), 'credit still wins once the cap is reached');+ let state = readState();+ assert.strictEqual(state.fact_force_denials, 1);+ assert.strictEqual(state.sibling_allows, 1);+ assert.strictEqual(state.fact_force_credited, 2);+ assert.ok(!state.cap_allows, 'no cap pass yet');+ const capped = capRun('Edit', { file_path: `${projectRoot}/src/unrelated_module.py`, old_string: 'a', new_string: 'b' }, t, '1');+ capAssertPassThrough(capped, 'Edit', 'uncredited target past the cap');+ state = readState();+ assert.strictEqual(state.cap_allows, 1);+ assert.strictEqual(state.fact_force_denials, 1);+ });++ gateCase('MultiEdit past the cap passes every entry and counts each', () => {+ writeState({ checked: [], last_active: Date.now(), fact_force_denials: 1 });+ const files = ['src/cap-multi-a.py', 'src/cap-multi-b.py', 'src/cap-multi-c.py'].map(f => `${projectRoot}/${f}`);+ const out = capRun('MultiEdit', { edits: files.map(file_path => ({ file_path, old_string: 'a', new_string: 'b' })) }, undefined, '1');+ capAssertPassThrough(out, 'MultiEdit', 'MultiEdit past cap');+ const state = readState();+ assert.strictEqual(state.cap_allows, 3);+ assert.strictEqual(state.fact_force_denials, 1);+ for (const f of files) assert.ok(state.checked.includes(stateKey(f)), `${f} checked`);+ });++ gateCase('destructive and routine Bash stay gated once the cap is reached', () => {+ writeState({ checked: [], last_active: Date.now(), fact_force_denials: 5 });+ const env = { ...projectEnv, [capEnvName]: '0' };+ const destructive = parseOutput(runBashHook({ tool_name: 'Bash', tool_input: { command: 'rm -rf build' } }, env).stdout);+ assert.strictEqual(destructive.hookSpecificOutput.permissionDecision, 'deny', 'destructive gate unchanged');+ const routine = parseOutput(runBashHook({ tool_name: 'Bash', tool_input: { command: 'npm test' } }, env).stdout);+ assert.strictEqual(routine.hookSpecificOutput.permissionDecision, 'deny', 'routine gate unchanged');+ assert.ok(!readState().cap_allows, 'Bash never counts a cap pass');+ });++ gateCase('no cap output carries permissionDecision "allow"', () => {+ assert.ok(capOutputs.length > 10, `collected outputs: ${capOutputs.length}`);+ for (const stdout of capOutputs) {+ const output = parseOutput(stdout);+ const decision = output && output.hookSpecificOutput ? output.hookSpecificOutput.permissionDecision : undefined;+ assert.notStrictEqual(decision, 'allow', stdout);+ }+ });++ // --- Sensitive targets are never auto-passed ---+ const sensLine = 'Sensitive target: prior-search credit, sibling collapse, and the denial cap do not apply.';+ const sensTargets = [+ '.env', '.env.local', 'config/.env.production', 'certs/server.pem', 'certs/server.key', 'certs/client.p12',+ 'certs/client.pfx', 'keys/id_rsa', 'keys/id_rsa.pub', 'keys/id_ed25519', 'keys/id_ecdsa', 'keys/id_dsa',+ '.netrc', 'home/.pgpass', 'config/credentials.json',+ 'config/secrets.yaml', 'src/auth/login_flow.py', 'src/authn/token_check.py', 'src/authz/policy_rules.py',+ 'src/security/hardening.py', 'src/secrets/vault_client.py', 'src/payment/charge_card.py',+ 'src/payments/refund_flow.py', 'src/billing/invoice_maker.py', 'db/migrations/0001_initial.py',+ '.github/workflows/release.yml', 'SRC/Auth/Upper_Case.py'+ ];+ const sensOutputs = [];+ const sensRun = (toolName, toolInput, transcriptPath, env = {}) => {+ const out = projectRun(toolName, toolInput, transcriptPath, env);+ sensOutputs.push(out.result.stdout);+ return out;+ };+ const sensAbs = rel => `${projectRoot}/${rel}`;+ const sensDir = rel => path.posix.dirname(sensAbs(rel));+ const sensStem = rel => path.posix.basename(rel).replace(/\.[^.]*$/, '') || path.posix.basename(rel);+ const sensAssertDenied = (out, label) => {+ assert.strictEqual(out.decision, 'deny', `${label}: expected deny, got ${out.result.stdout}`);+ assert.ok(!out.context, `${label}: no credit/sibling note`);+ assert.ok(out.reason.includes(sensLine), `${label}: sensitive line expected: ${out.reason}`);+ };+ // Search evidence that credits a Write of a new file in `rel`'s directory, and an Edit of its stem.+ const sensCreditTranscript = (rel, n) => writeTranscript([+ humanRecord('update it'),+ ...searchRecords(`toolu_sens_ls_${n}`, 'LS', { path: sensDir(rel) }),+ ...searchRecords(`toolu_sens_gr_${n}`, 'Grep', { pattern: sensStem(rel), path: projectRoot }),+ ...searchRecords(`toolu_sens_ctl_${n}`, 'LS', { path: sensAbs('src/plain') })+ ]);++ gateCase('each sensitive target is denied on first Write despite a qualifying search (control credited)', () => {+ sensTargets.forEach((rel, n) => {+ clearState();+ const t = sensCreditTranscript(rel, n);+ sensAssertDenied(sensRun('Write', { file_path: sensAbs(rel), content: 'x' }, t), `Write ${rel}`);+ const control = sensRun('Write', { file_path: sensAbs(`src/plain/ctl_${n}.py`), content: 'x' }, t);+ assert.ok(control.context.includes('Prior search seen in this turn'), `control credited for ${rel}: ${control.result.stdout}`);+ const state = readState();+ assert.strictEqual(state.fact_force_denials, 1, `${rel}: the denial counts`);+ assert.strictEqual(state.fact_force_credited, 1, `${rel}: only the control was credited`);+ });+ });++ gateCase('sensitive Edit and MultiEdit targets are denied despite a stem search', () => {+ const t = writeTranscript([+ humanRecord('fix the billing'),+ ...searchRecords('toolu_sens_e1', 'Grep', { pattern: 'invoice_maker|login_flow|plain_helper', path: projectRoot })+ ]);+ sensAssertDenied(sensRun('Edit', { file_path: sensAbs('src/billing/invoice_maker.py'), old_string: 'a', new_string: 'b' }, t), 'Edit');+ const multi = sensRun('MultiEdit', {+ edits: [+ { file_path: sensAbs('src/plain/plain_helper.py'), old_string: 'a', new_string: 'b' },+ { file_path: sensAbs('src/auth/login_flow.py'), old_string: 'a', new_string: 'b' }+ ]+ }, t);+ sensAssertDenied(multi, 'MultiEdit sensitive entry');+ const state = readState();+ assert.strictEqual(state.fact_force_credited, 1, 'the ordinary entry was credited');+ assert.strictEqual(state.fact_force_denials, 2);+ });++ gateCase('a sensitive new file never joins a same-dir sibling gate', () => {+ const t = writeTranscript([humanRecord('scaffold keys')]);+ assertDeniedNoSibling(sensRun('Write', { file_path: sensAbs('src/keys/loader.py'), content: 'x' }, t), 'ordinary first');+ for (const rel of ['src/keys/server.key', 'src/keys/server.pem', 'src/keys/credentials.py', 'src/keys/secrets.py', 'src/keys/id_rsa_test_fixture.py']) {+ sensAssertDenied(sensRun('Write', { file_path: sensAbs(rel), content: 'x' }, t), rel);+ }+ assertSibling(sensRun('Write', { file_path: sensAbs('src/keys/other.py'), content: 'x' }, t), 'ordinary sibling still collapses');+ assert.strictEqual(readState().sibling_allows, 1);+ });++ gateCase('a sensitive denial opens no sibling gate; sensitive dirs never collapse', () => {+ const t = writeTranscript([humanRecord('scaffold auth')]);+ sensAssertDenied(sensRun('Write', { file_path: sensAbs('src/vault/api.key'), content: 'x' }, t), 'sensitive first');+ assertDeniedNoSibling(sensRun('Write', { file_path: sensAbs('src/vault/client.py'), content: 'x' }, t), 'ordinary after sensitive');+ sensAssertDenied(sensRun('Write', { file_path: sensAbs('src/auth/a.py'), content: 'x' }, t), 'auth first');+ sensAssertDenied(sensRun('Write', { file_path: sensAbs('src/auth/b.py'), content: 'x' }, t), 'auth second');+ const gates = Object.keys(readState().dir_gates || {});+ assert.ok(!gates.some(k => k.endsWith(stateKey(sensAbs('src/auth')))), `no auth gate: ${JSON.stringify(gates)}`);+ assert.deepStrictEqual(gates, [dirGateKey(sensAbs('src/vault'))], 'only the ordinary denial opened a gate');+ });++ gateCase('sensitive targets are denied after the denial cap is reached (and still count)', () => {+ writeState({ checked: [], last_active: Date.now(), fact_force_denials: 5 });+ const env = { GATEGUARD_FACT_FORCE_MAX_DENIALS: '1' };+ sensTargets.forEach((rel, n) => {+ const tool = n % 2 ? 'Edit' : 'Write';+ const input = tool === 'Edit' ? { file_path: sensAbs(rel), old_string: 'a', new_string: 'b' } : { file_path: sensAbs(rel), content: 'x' };+ sensAssertDenied(sensRun(tool, input, undefined, env), `${tool} ${rel} past cap`);+ });+ const multi = sensRun('MultiEdit', { edits: [+ { file_path: sensAbs('src/plain/capped.py'), old_string: 'a', new_string: 'b' },+ { file_path: sensAbs('src/payments/capped.py'), old_string: 'a', new_string: 'b' }+ ] }, undefined, env);+ sensAssertDenied(multi, 'MultiEdit sensitive entry past cap');+ const control = sensRun('Edit', { file_path: sensAbs('src/plain/also_capped.py'), old_string: 'a', new_string: 'b' }, undefined, env);+ assert.ok(!parseOutput(control.result.stdout).hookSpecificOutput, 'ordinary target passes the cap');+ const state = readState();+ assert.strictEqual(state.fact_force_denials, 5 + sensTargets.length + 1, 'every sensitive denial counts');+ assert.strictEqual(state.cap_allows, 2);+ });++ gateCase('the sensitive line also appears in condensed denials; ordinary denials are unchanged', () => {+ writeState({ checked: [], last_active: Date.now(), fact_force_denials: 9 });+ const condensed = sensRun('Edit', { file_path: sensAbs('src/auth/condensed.py'), old_string: 'a', new_string: 'b' });+ sensAssertDenied(condensed, 'condensed');+ assert.ok(condensed.reason.includes('(denial #10 this session)'), condensed.reason);+ clearState();+ const full = sensRun('Edit', { file_path: sensAbs('src/auth/full.py'), old_string: 'a', new_string: 'b' });+ sensAssertDenied(full, 'full');+ assert.ok(full.reason.includes(`${sensLine}\n\nPresent the facts, then retry the same operation.`), full.reason);+ clearState();+ const ordinary = sensRun('Edit', { file_path: sensAbs('src/author.py'), old_string: 'a', new_string: 'b' });+ assert.strictEqual(ordinary.decision, 'deny');+ assert.ok(!ordinary.reason.includes('Sensitive target'), 'segment-exact: author.py is ordinary');+ for (const rel of ['docs/authoring.md', 'lib/paymentutils.py']) {+ clearState();+ const t = writeTranscript([humanRecord('go'), ...searchRecords(`toolu_ord_${rel.length}`, 'LS', { path: sensDir(rel) })]);+ const out = sensRun('Write', { file_path: sensAbs(rel), content: 'x' }, t);+ assert.ok(out.context.includes('Prior search seen in this turn'), `${rel} is ordinary and credited: ${out.result.stdout}`);+ }+ });++ // --- A symlinked directory cannot launder a sensitive real location ---+ // symlinked/src/tools -> symlinked/src/auth (a real directory inside the project).+ const aliasReady = (() => {+ fs.mkdirSync(sensAbs('symlinked/src/auth'), { recursive: true });+ fs.writeFileSync(sensAbs('symlinked/src/auth/login_flow.py'), 'x');+ fs.mkdirSync(sensAbs('symlinked/src/plain'), { recursive: true });+ fs.writeFileSync(sensAbs('symlinked/src/plain/plain_login.py'), 'x');+ try {+ fs.symlinkSync(sensAbs('symlinked/src/auth'), sensAbs('symlinked/src/tools'), 'dir');+ return true;+ } catch (e) {+ if (process.platform === 'win32' && e.code === 'EPERM') return false; // no symlink privilege+ throw e;+ }+ })();+ const aliasEdit = (rel, t, env) => sensRun('Edit', { file_path: sensAbs(rel), old_string: 'a', new_string: 'b' }, t, env);+ const assertAliasCredited = (out, label) =>+ assert.ok(out.context.includes('Prior search seen in this turn'), `${label}: credit expected, got ${out.result.stdout}`);++ gateCase('an Edit through a symlink into auth/ is denied despite a crediting search (direct control credited)', () => {+ if (!aliasReady) return;+ const t = writeTranscript([+ humanRecord('fix login'),+ ...searchRecords('toolu_r1_g1', 'Grep', { pattern: 'login_flow|plain_login|new_login', path: projectRoot })+ ]);+ sensAssertDenied(aliasEdit('symlinked/src/tools/login_flow.py', t), 'Edit via the alias');+ sensAssertDenied(sensRun('Write', { file_path: sensAbs('symlinked/src/tools/new_login.py'), content: 'x' }, t), 'Write via the alias');+ assertAliasCredited(aliasEdit('symlinked/src/plain/plain_login.py', t), 'direct non-sensitive path');+ const state = readState();+ assert.strictEqual(state.fact_force_credited, 1);+ assert.strictEqual(state.fact_force_denials, 2);+ });++ gateCase('a new file through a symlink into auth/ never joins a same-turn sibling gate', () => {+ if (!aliasReady) return;+ const { human, transcript } = newTurn('scaffold handlers');+ const gate = { turn: human.uuid, at: Date.now() - 1000, first: 'seeded.py', ordinal: 1 };+ writeState({+ checked: [],+ last_active: Date.now(),+ fact_force_denials: 1,+ dir_gates: {+ [dirGateKey(sensAbs('symlinked/src/auth'))]: gate,+ [dirGateKey(sensAbs('symlinked/src/tools'))]: gate,+ [dirGateKey(sensAbs('symlinked/src/plain'))]: gate+ }+ });+ sensAssertDenied(sensRun('Write', { file_path: sensAbs('symlinked/src/tools/new_handler.py'), content: 'x' }, transcript), 'sibling via alias');+ assertSibling(sensRun('Write', { file_path: sensAbs('symlinked/src/plain/new_handler.py'), content: 'x' }, transcript), 'control sibling');+ });++ gateCase('an Edit through a symlink into auth/ is denied past the denial cap (control passes)', () => {+ if (!aliasReady) return;+ const env = { GATEGUARD_FACT_FORCE_MAX_DENIALS: '0' };+ sensAssertDenied(aliasEdit('symlinked/src/tools/login_flow.py', undefined, env), 'alias past cap 0');+ const control = aliasEdit('symlinked/src/plain/plain_login.py', undefined, env);+ assert.ok(!parseOutput(control.result.stdout).hookSpecificOutput, `control passes the cap: ${control.result.stdout}`);+ });++ gateCase('no sensitive output carries permissionDecision "allow"', () => {+ assert.ok(sensOutputs.length > 60, `collected outputs: ${sensOutputs.length}`);+ for (const stdout of sensOutputs) {+ const output = parseOutput(stdout);+ const decision = output && output.hookSpecificOutput ? output.hookSpecificOutput.permissionDecision : undefined;+ assert.notStrictEqual(decision, 'allow', stdout);+ }+ });++ // --- Read-only first shell command ---+ const roDecision = result => {+ const output = parseOutput(result.stdout);+ return output && output.hookSpecificOutput ? output.hookSpecificOutput.permissionDecision : undefined;+ };+ const roReason = result => {+ const output = parseOutput(result.stdout);+ return output && output.hookSpecificOutput ? String(output.hookSpecificOutput.permissionDecisionReason) : '';+ };+ const roBash = (command, extra = {}, env = {}) => runBashHook({ tool_name: 'Bash', tool_input: { command }, ...extra }, env);+ const roPs = command => runPowerShellHook({ tool_name: 'PowerShell', tool_input: { command } });++ gateCase('a read-only first Bash command passes without using the routine gate', () => {+ const first = roBash('ls -la src');+ assert.strictEqual(roDecision(first), undefined, first.stdout);+ assert.ok(!first.stdout.includes('permissionDecision'), first.stdout);+ const state = readState();+ assert.strictEqual(state.routine_readonly_passes, 1);+ assert.ok(!(state.checked || []).includes('__bash_session__'), 'routine gate must stay unchecked');+ const second = roBash('git status --short | head');+ assert.strictEqual(roDecision(second), undefined, second.stdout);+ assert.strictEqual(readState().routine_readonly_passes, 2);+ const routine = roBash('npm test');+ assert.strictEqual(roDecision(routine), 'deny');+ assert.ok(roReason(routine).includes('current user request'));+ assert.strictEqual(roDecision(roBash('npm test')), undefined, 'retry after the routine gate passes');+ });++ gateCase('read-only commands after the routine gate is checked are not counted', () => {+ writeState({ checked: ['__bash_session__'], last_active: Date.now() });+ assert.strictEqual(roDecision(roBash('ls')), undefined);+ assert.ok(!readState().routine_readonly_passes, 'no read-only pass counted once the gate is checked');+ });++ gateCase('a read-only first PowerShell command passes and the next cmdlet is still gated', () => {+ assert.strictEqual(roDecision(roPs('Get-ChildItem -Recurse -Filter *.js')), undefined);+ const routine = roPs('Get-Date');+ assert.strictEqual(roDecision(routine), 'deny');+ assert.ok(roReason(routine).includes('Before the first PowerShell command'));+ });++ gateCase('commands that write, substitute or run unknown programs still draw the routine gate', () => {+ for (const command of ['ls > out.txt', 'ls | tee out.txt', 'cat $(pwd)/a', 'find . -exec ls {} +', 'find . -name x -delete', 'git log | xargs echo', 'FOO=1 ls', 'sed -i s/a/b/ f', 'git commit -m x']) {+ clearState();+ const result = roBash(command);+ assert.strictEqual(roDecision(result), 'deny', `${command}: ${result.stdout}`);+ assert.ok(roReason(result).includes('current user request'), `${command}: routine gate expected`);+ assert.ok(!readState().routine_readonly_passes, `${command}: no read-only pass`);+ }+ for (const command of ['Get-ChildItem | Out-File a.txt', 'Get-Content a | Set-Content b', 'pwsh -Command Get-ChildItem', 'Get-Content $env:X']) {+ clearState();+ assert.strictEqual(roDecision(roPs(command)), 'deny', command);+ }+ });++ gateCase('destructive commands are gated before the read-only check', () => {+ for (const command of ['find . -name x -exec rm {} +', 'rm -rf build', 'git branch -D old']) {+ clearState();+ const result = roBash(command);+ assert.strictEqual(roDecision(result), 'deny', command);+ assert.ok(roReason(result).includes('Destructive command detected'), `${command}: destructive gate expected`);+ }+ clearState();+ const ps = roPs('Remove-Item -Recurse -Force C:/tmp/demo');+ assert.strictEqual(roDecision(ps), 'deny');+ assert.ok(roReason(ps).includes('Destructive command detected'));+ });++ gateCase('a read-only first command in a subagent passes and other commands stay gated', () => {+ assert.strictEqual(roDecision(roBash('pwd', { agent_id: 'agent-ro' })), undefined);+ const routine = roBash('npm test', { agent_id: 'agent-ro' });+ assert.strictEqual(roDecision(routine), 'deny');+ assert.ok(roReason(routine).includes('current user request'));+ });++ // --- Idle window follows the session key ---+ const idleEnvNoIds = { CLAUDE_SESSION_ID: '', ECC_SESSION_ID: '', CLAUDE_TRANSCRIPT_PATH: '' };+ const idleStateFiles = () => fs.readdirSync(stateDir).filter(f => f.startsWith('state-') && f.endsWith('.json'));+ const idleSeed = (fileName, ageMs) => {+ fs.mkdirSync(stateDir, { recursive: true });+ const file = path.join(stateDir, fileName);+ fs.writeFileSync(file, JSON.stringify({ checked: ['__bash_session__'], last_active: Date.now() - ageMs }), 'utf8');+ return file;+ };+ const HOUR = 60 * 60 * 1000;++ gateCase('state keyed by a session id survives two idle hours', () => {+ writeState({ checked: ['__bash_session__'], last_active: Date.now() - 2 * HOUR });+ assert.strictEqual(roDecision(roBash('npm test')), undefined);+ });++ gateCase('state keyed by a session id expires after eight idle hours', () => {+ writeState({ checked: ['__bash_session__'], last_active: Date.now() - 8 * HOUR - 60 * 1000 });+ assert.strictEqual(roDecision(roBash('npm test')), 'deny');+ });++ gateCase('state keyed by a transcript path survives two idle hours', () => {+ const transcript = path.join(stateDir, 'idle-session.jsonl');+ roBash('npm test', { transcript_path: transcript }, idleEnvNoIds);+ const [file] = idleStateFiles();+ assert.ok(/^state-tx-/.test(file), file);+ const state = JSON.parse(fs.readFileSync(path.join(stateDir, file), 'utf8'));+ fs.writeFileSync(path.join(stateDir, file), JSON.stringify({ ...state, last_active: Date.now() - 2 * HOUR }), 'utf8');+ assert.strictEqual(roDecision(roBash('npm test', { transcript_path: transcript }, idleEnvNoIds)), undefined);+ });++ gateCase('state keyed by the project fallback still expires after 30 idle minutes', () => {+ const env = { ...idleEnvNoIds, CLAUDE_PROJECT_DIR: path.join(stateDir, 'idle-project') };+ roBash('npm test', {}, env);+ const [file] = idleStateFiles();+ assert.ok(/^state-proj-/.test(file), file);+ idleSeed(file, 31 * 60 * 1000);+ assert.strictEqual(roDecision(roBash('npm test', {}, env)), 'deny');+ });++ gateCase('module-load pruning keeps session files for the long window and project files for the short one', () => {+ const age = (file, ms) => fs.utimesSync(file, new Date(Date.now() - ms), new Date(Date.now() - ms));+ const recentSession = idleSeed('state-recent-session.json', 0);+ const oldSession = idleSeed('state-old-session.json', 0);+ const recentTx = idleSeed(`state-tx-${'a'.repeat(24)}.json`, 0);+ const oldProj = idleSeed(`state-proj-${'b'.repeat(24)}.json`, 0);+ const oldTmp = path.join(stateDir, 'state-recent-session.json.tmp.1.abcd');+ fs.writeFileSync(oldTmp, '{}', 'utf8');+ age(recentSession, 2 * HOUR);+ age(recentTx, 2 * HOUR);+ age(oldSession, 17 * HOUR);+ age(oldProj, 61 * 60 * 1000);+ age(oldTmp, 61 * 60 * 1000);+ loadDirectHook();+ assert.ok(fs.existsSync(recentSession), 'session file inside the long window is kept');+ assert.ok(fs.existsSync(recentTx), 'transcript file inside the long window is kept');+ assert.ok(!fs.existsSync(oldSession), 'session file past the long window is pruned');+ assert.ok(!fs.existsSync(oldProj), 'project fallback file past the short window is pruned');+ assert.ok(!fs.existsSync(oldTmp), 'stale temp file is pruned on the short window');+ });++ // --- Sensitive targets in subagents ---+ const subRun = (tool, toolInput, extra = {}) =>+ runHook({ tool_name: tool, tool_input: toolInput, session_id: 'subagent-sensitive-session', ...extra }, { CLAUDE_SESSION_ID: '', ECC_SESSION_ID: '' });+ const subEdit = (file_path, extra) => subRun('Edit', { file_path, old_string: 'a', new_string: 'b' }, extra);+ const subAgent = { agent_id: 'agent-sens' };++ gateCase('a subagent Edit of a sensitive file is denied once, then its retry passes', () => {+ const first = subEdit('/src/.env', subAgent);+ assert.strictEqual(roDecision(first), 'deny', first.stdout);+ assert.ok(roReason(first).includes(sensLine));+ assert.strictEqual(roDecision(subEdit('/src/.env', subAgent)), undefined, 'subagent retry passes');+ });++ gateCase('a subagent pass on a sensitive file does not unlock it for the parent', () => {+ subEdit('/src/auth/login.js', subAgent);+ assert.strictEqual(roDecision(subEdit('/src/auth/login.js', subAgent)), undefined);+ const parent = subEdit('/src/auth/login.js');+ assert.strictEqual(roDecision(parent), 'deny', 'parent first touch is still gated');+ });++ gateCase('a sensitive file the parent already gated passes in a subagent', () => {+ assert.strictEqual(roDecision(subEdit('/src/secrets.json')), 'deny');+ assert.strictEqual(roDecision(subEdit('/src/secrets.json', { parent_tool_use_id: 'toolu_parent' })), undefined);+ });++ gateCase('subagent Write and MultiEdit of sensitive targets are denied; ordinary targets still pass', () => {+ const write = subRun('Write', { file_path: '/repo/.github/workflows/ci.yml', content: 'x' }, subAgent);+ assert.strictEqual(roDecision(write), 'deny');+ const multi = subRun('MultiEdit', {+ edits: [+ { file_path: '/src/plain.js', old_string: 'a', new_string: 'b' },+ { file_path: '/src/payments/charge.js', old_string: 'a', new_string: 'b' }+ ]+ }, subAgent);+ assert.strictEqual(roDecision(multi), 'deny');+ assert.ok(roReason(multi).includes('charge.js'));+ assert.strictEqual(roDecision(subEdit('/src/ordinary.js', subAgent)), undefined);+ assert.strictEqual(roDecision(subRun('Write', { file_path: '/src/new-file.js', content: 'x' }, subAgent)), undefined);+ });++ gateCase('subagent sensitive denials ignore prior-search credit and the denial cap', () => {+ const transcript = writeTranscript([humanRecord('fix it'), ...searchRecords('toolu_sub1', 'Grep', { pattern: 'login', path: '/src' })]);+ const result = subRun('Edit', { file_path: '/src/auth/login.js', old_string: 'a', new_string: 'b' }, { ...subAgent, transcript_path: transcript });+ assert.strictEqual(roDecision(result), 'deny');+ clearState();+ const capped = runHook(+ { tool_name: 'Edit', tool_input: { file_path: '/src/.env.local', old_string: 'a', new_string: 'b' }, session_id: 'subagent-sensitive-session', ...subAgent },+ { CLAUDE_SESSION_ID: '', ECC_SESSION_ID: '', GATEGUARD_FACT_FORCE_MAX_DENIALS: '0' }+ );+ assert.strictEqual(roDecision(capped), 'deny');+ });++ // --- NotebookEdit ---+ const notebookInput = (rel, extra = {}) => ({ notebook_path: sensAbs(rel), new_source: 'x = 1', cell_id: 'c1', ...extra });+ const notebookRun = (rel, transcriptPath, env, extra) => projectRun('NotebookEdit', notebookInput(rel, extra), transcriptPath, env);+ const FULL_CODE_QUESTIONS = [+ 'List ALL files that import/require this file',+ 'List the public functions/classes affected by this change',+ 'If this file reads/writes data files',+ "Quote the user's current instruction verbatim"+ ];++ gateCase('a first NotebookEdit is denied with the full code questions, then its retry passes', () => {+ const first = notebookRun('nb/analysis.ipynb');+ assert.strictEqual(first.decision, 'deny', first.result.stdout);+ assert.ok(first.reason.includes(`Before editing ${sensAbs('nb/analysis.ipynb')}`), first.reason);+ for (const line of FULL_CODE_QUESTIONS) assert.ok(first.reason.includes(line), `${line}: ${first.reason}`);+ assert.ok(!first.reason.includes(sensLine), 'ordinary notebook has no sensitive line');+ const retry = notebookRun('nb/analysis.ipynb');+ assert.strictEqual(retry.decision, undefined, retry.result.stdout);+ assert.strictEqual(readState().fact_force_denials, 1);+ });++ gateCase('NotebookEdit and Edit share one checked key for a notebook', () => {+ assert.strictEqual(notebookRun('nb/shared.ipynb').decision, 'deny');+ assert.strictEqual(projectEdit(sensAbs('nb/shared.ipynb')).decision, undefined, 'Edit after a NotebookEdit denial passes');+ assert.strictEqual(projectEdit(sensAbs('nb/other.ipynb')).decision, 'deny');+ assert.strictEqual(notebookRun('nb/other.ipynb').decision, undefined, 'NotebookEdit after an Edit denial passes');+ });++ gateCase('NotebookEdit is gated by notebook_path, not by a file_path field', () => {+ projectEdit(sensAbs('src/checked.py'));+ const out = notebookRun('nb/target.ipynb', undefined, {}, { file_path: sensAbs('src/checked.py') });+ assert.strictEqual(out.decision, 'deny', out.result.stdout);+ assert.ok(out.reason.includes('target.ipynb'), out.reason);+ const exempt = notebookRun('nb/exempt-by-file-path.ipynb', undefined, { GATEGUARD_EXEMPT_GLOBS: '**/other.py' }, { file_path: sensAbs('src/other.py') });+ assert.strictEqual(exempt.decision, 'deny', 'an exempt file_path does not exempt the notebook');+ });++ gateCase('a NotebookEdit is never a trivial pass', () => {+ const out = notebookRun('nb/comment.ipynb', undefined, {}, { new_source: '# just a comment', edit_mode: 'replace' });+ assert.strictEqual(out.decision, 'deny', out.result.stdout);+ assert.ok(!out.context.includes('Comment or whitespace-only'), out.context);+ assert.ok(!readState().trivial_allows, 'no trivial pass counted');+ });++ gateCase('a notebook under tests/ gets the test questions', () => {+ const out = notebookRun('tests/notebooks/check.ipynb');+ assert.strictEqual(out.decision, 'deny');+ assert.ok(out.reason.includes('Name what behaviour is under test'), out.reason);+ });++ gateCase('a search naming the notebook credits its first NotebookEdit', () => {+ const t = writeTranscript([humanRecord('update the notebook'), ...searchRecords('toolu_nb1', 'Grep', { pattern: 'forecast', path: projectRoot })]);+ const out = notebookRun('nb/forecast.ipynb', t);+ assert.notStrictEqual(out.decision, 'deny', out.result.stdout);+ assert.ok(out.context.includes('Prior search seen in this turn'), out.result.stdout);+ });++ gateCase('a sensitive notebook is denied despite a crediting search and past the denial cap', () => {+ const t = writeTranscript([humanRecord('update it'), ...searchRecords('toolu_nb2', 'Grep', { pattern: 'tokens', path: projectRoot })]);+ sensAssertDenied(notebookRun('src/auth/tokens.ipynb', t), 'credited sensitive notebook');+ clearState();+ sensAssertDenied(notebookRun('src/auth/tokens.ipynb', undefined, { GATEGUARD_FACT_FORCE_MAX_DENIALS: '0' }), 'sensitive notebook past cap 0');+ const control = notebookRun('nb/capped.ipynb', undefined, { GATEGUARD_FACT_FORCE_MAX_DENIALS: '0' });+ assert.strictEqual(control.decision, undefined, 'ordinary notebook passes the cap like Edit');+ });++ gateCase('NotebookEdit honours exempt globs', () => {+ const out = notebookRun('scratch/play.ipynb', undefined, { GATEGUARD_EXEMPT_GLOBS: '**/scratch/**' });+ assert.strictEqual(out.decision, undefined, out.result.stdout);+ assert.strictEqual(out.context, '');+ });++ gateCase('a subagent NotebookEdit passes for an ordinary notebook and is denied for a sensitive one', () => {+ const agent = { agent_id: 'agent-notebook' };+ assert.strictEqual(roDecision(subRun('NotebookEdit', { notebook_path: '/src/nb/plain.ipynb', new_source: 'x' }, agent)), undefined);+ const sensitive = subRun('NotebookEdit', { notebook_path: '/src/payments/ledger.ipynb', new_source: 'x' }, agent);+ assert.strictEqual(roDecision(sensitive), 'deny');+ assert.ok(roReason(sensitive).includes(sensLine));+ });++ gateCase('a lower-case notebookedit tool name is gated the same way', () => {+ assert.strictEqual(projectRun('notebookedit', notebookInput('nb/lower.ipynb')).decision, 'deny');+ });++ // --- Hard-linked targets ---+ const linkedLine = 'Hard-linked target: prior-search credit, sibling collapse, and the denial cap do not apply.';+ const linkPair = (rel, alias, content = '// old\nfoo();\n') => {+ fs.mkdirSync(path.dirname(sensAbs(rel)), { recursive: true });+ fs.mkdirSync(path.dirname(sensAbs(alias)), { recursive: true });+ fs.writeFileSync(sensAbs(rel), content);+ try {+ fs.linkSync(sensAbs(rel), sensAbs(alias));+ return true;+ } catch (e) {+ if (['EPERM', 'ENOTSUP', 'EXDEV', 'ENOSYS', 'EOPNOTSUPP'].includes(e.code)) return false;+ throw e;+ }+ };+ const linksReady = linkPair('linked/src/report.js', 'linked/vendor/report.js')+ && linkPair('linked/src/auth/token.js', 'linked/vendor/token.js')+ && linkPair('linked/src/sub.js', 'linked/vendor/sub.js')+ && linkPair('linked/scratch/tmp.js', 'linked/vendor/tmp.js');+ for (const rel of ['linked/src/plain.js', 'linked/src/plain_two.js']) fs.writeFileSync(sensAbs(rel), '// old\nfoo();\n');+ const assertLinkedDenied = (out, label) => {+ assert.strictEqual(out.decision, 'deny', `${label}: expected deny, got ${out.result.stdout}`);+ assert.ok(!out.context, `${label}: no credit, trivial or sibling note`);+ assert.ok(out.reason.includes(linkedLine), `${label}: hard-link line expected: ${out.reason}`);+ assert.ok(!out.reason.includes(sensLine), `${label}: no sensitive wording`);+ };+ const linkedSearch = n => writeTranscript([+ humanRecord('update the report'),+ ...searchRecords(`toolu_linked_${n}`, 'Grep', { pattern: 'report|plain|tmp', path: projectRoot })+ ]);++ gateCase('an Edit of a hard-linked file is denied despite a crediting search (control credited)', () => {+ if (!linksReady) return;+ const t = linkedSearch(1);+ assertLinkedDenied(projectEdit(sensAbs('linked/src/report.js'), t), 'original name');+ assertLinkedDenied(projectEdit(sensAbs('linked/vendor/report.js'), t), 'second name');+ assertCredited(projectEdit(sensAbs('linked/src/plain.js'), t), 'single-link control');+ const state = readState();+ assert.strictEqual(state.fact_force_denials, 2);+ assert.strictEqual(state.fact_force_credited, 1);+ });++ gateCase('the full code questions are asked for a hard-linked file', () => {+ if (!linksReady) return;+ const out = projectRun('Edit', { file_path: sensAbs('linked/src/report.js'), old_string: 'foo();', new_string: 'bar();' });+ assertLinkedDenied(out, 'body-only change');+ for (const line of FULL_CODE_QUESTIONS) assert.ok(out.reason.includes(line), `${line}: ${out.reason}`);+ });++ gateCase('a comment-only Edit of a hard-linked file is denied (control passes as trivial)', () => {+ if (!linksReady) return;+ const input = rel => ({ file_path: sensAbs(rel), old_string: '// old', new_string: '// new' });+ assertLinkedDenied(projectRun('Edit', input('linked/src/report.js')), 'Edit');+ const multi = projectRun('MultiEdit', { file_path: sensAbs('linked/vendor/tmp.js'), edits: [{ old_string: '// old', new_string: '// new' }] });+ assertLinkedDenied(multi, 'MultiEdit');+ const control = projectRun('Edit', input('linked/src/plain_two.js'));+ assert.notStrictEqual(control.decision, 'deny', control.result.stdout);+ assert.ok(control.context.includes('Comment or whitespace-only change'), control.context);+ });++ gateCase('a hard-linked file is denied past the denial cap (control passes)', () => {+ if (!linksReady) return;+ const env = { GATEGUARD_FACT_FORCE_MAX_DENIALS: '0' };+ assertLinkedDenied(projectEdit(sensAbs('linked/src/report.js'), undefined, env), 'Edit past cap 0');+ const multi = projectRun('MultiEdit', { edits: [+ { file_path: sensAbs('linked/src/plain.js'), old_string: 'foo();', new_string: 'bar();' },+ { file_path: sensAbs('linked/vendor/tmp.js'), old_string: 'foo();', new_string: 'bar();' }+ ] }, undefined, env);+ assertLinkedDenied(multi, 'MultiEdit entry past cap 0');+ assert.ok(multi.reason.includes('tmp.js'), multi.reason);+ const control = projectEdit(sensAbs('linked/src/plain_two.js'), undefined, env);+ assert.strictEqual(control.decision, undefined, control.result.stdout);+ });++ gateCase('a MultiEdit with a hard-linked entry is denied despite a crediting search', () => {+ if (!linksReady) return;+ const t = linkedSearch(2);+ const multi = projectRun('MultiEdit', { edits: [+ { file_path: sensAbs('linked/src/plain.js'), old_string: 'foo();', new_string: 'bar();' },+ { file_path: sensAbs('linked/src/report.js'), old_string: 'foo();', new_string: 'bar();' }+ ] }, t);+ assertLinkedDenied(multi, 'MultiEdit');+ assert.ok(multi.reason.includes('report.js'), multi.reason);+ });++ gateCase('a subagent Edit of a hard-linked file is denied once; single-link targets still pass', () => {+ if (!linksReady) return;+ const agent = { agent_id: 'agent-linked', cwd: projectRoot };+ const first = subEdit(sensAbs('linked/vendor/sub.js'), agent);+ assert.strictEqual(roDecision(first), 'deny', first.stdout);+ assert.ok(roReason(first).includes(linkedLine), roReason(first));+ assert.strictEqual(roDecision(subEdit(sensAbs('linked/vendor/sub.js'), agent)), undefined, 'subagent retry passes');+ assert.strictEqual(roDecision(subEdit(sensAbs('linked/src/plain.js'), agent)), undefined);+ });++ gateCase('a hard-linked sensitive file carries the sensitive wording', () => {+ if (!linksReady) return;+ const out = projectEdit(sensAbs('linked/src/auth/token.js'));+ sensAssertDenied(out, 'sensitive and hard-linked');+ assert.ok(!out.reason.includes(linkedLine), out.reason);+ const alias = projectEdit(sensAbs('linked/vendor/token.js'));+ assertLinkedDenied(alias, 'ordinary name of a hard link into auth/');+ });++ gateCase('a new file next to a hard-linked file keeps its allowances', () => {+ if (!linksReady) return;+ const t = writeTranscript([humanRecord('add a module'), ...searchRecords('toolu_linked_ls', 'LS', { path: sensAbs('linked/src') })]);+ const out = projectWrite(sensAbs('linked/src/new_module.js'), t);+ assertCredited(out, 'missing new file');+ });++ gateCase('an exempt glob still exempts a hard-linked file', () => {+ if (!linksReady) return;+ const out = projectEdit(sensAbs('linked/scratch/tmp.js'), undefined, { GATEGUARD_EXEMPT_GLOBS: 'linked/scratch/**' });+ assert.strictEqual(out.decision, undefined, out.result.stdout);+ });++ gateCase('no hard-link output carries permissionDecision "allow"', () => {+ for (const stdout of siblingOutputs) {+ const output = parseOutput(stdout);+ assert.notStrictEqual(output && output.hookSpecificOutput ? output.hookSpecificOutput.permissionDecision : undefined, 'allow', stdout);+ }+ });++ fs.rmSync(classBase, { recursive: true, force: true });+ fs.rmSync(scopeRoot, { recursive: true, force: true });++ fs.rmSync(projectRoot, { recursive: true, force: true });++ fs.rmSync(transcriptDir, { recursive: true, force: true });++ // Cleanup only the temp directory created by this test file.+ try {+ if (fs.existsSync(stateDir)) {+ fs.rmSync(stateDir, { recursive: true, force: true });+ }+ } catch (err) {+ console.error(` [cleanup] failed to remove ${stateDir}: ${err.message}`);+ }++ // --- sanitizePath dangerous invisible unicode regression ---+ clearState();+ if (+ test('sanitizePath strips CI-defined dangerous invisible unicode from denial paths', () => {+ const file_path =- '/src/eu2028\u2028eu2029\u2029app.js\u200bhidden\u2060name\ufefftail\u3164x\u0091c1.js';++ '/src/eu2028\u2028eu2029\u2029app.js\u200bhidden\u2060name\ufefftail\u00adsoft\u3164x\u0091c1.js';+ const input = {+ tool_name: 'Edit',+ tool_input: { file_path, old_string: 'foo', new_string: 'bar' }+ };+ const result = runHook(input);+ const output = parseOutput(result.stdout);+ const reason = String(+ output && output.hookSpecificOutput+ ? output.hookSpecificOutput.permissionDecisionReason+ : ''+ );- for (const bad of ['\u2028', '\u2029', '\u200b', '\u2060', '\ufeff', '\u3164', '\u0091']) {++ for (const bad of ['\u2028', '\u2029', '\u200b', '\u2060', '\ufeff', '\u00ad', '\u3164', '\u0091']) {+ assert.ok(!reason.includes(bad), `denial reason must not carry U+${bad.codePointAt(0).toString(16)} (${bad})`);+ }+ assert.ok(reason.includes('app.js'), 'visible path text must remain');+ })+ ) {+ passed++;+ } else {+ failed++;+ }++ // --- Modules load only on the paths that need them ---+ const loadedLibs = (payload, setup = []) => {+ const dir = fs.mkdtempSync(path.join(tmpRoot, 'gateguard-lazy-'));+ const env = { PATH: process.env.PATH, HOME: dir, GATEGUARD_STATE_DIR: path.join(dir, 'state'), CLAUDE_PROJECT_DIR: dir };+ const probe = [+ 'const hook = require(process.argv[1]);',+ 'hook.run(process.argv[2]);',+ "const names = Object.keys(require.cache).map(f => require('path').basename(f, '.js'));",+ "const libs = names.filter(n => /^(gateguard-|transcript-context$|file-tail$)/.test(n) && !/^gateguard-(fact-force|heredoc)$/.test(n));",+ 'process.stdout.write(JSON.stringify(libs.sort()));'+ ].join('\n');+ try {+ for (const step of setup) spawnSync(process.execPath, ['-e', probe, hookScript, JSON.stringify(step)], { env });+ const out = spawnSync(process.execPath, ['-e', probe, hookScript, JSON.stringify(payload)], { encoding: 'utf8', env });+ return JSON.parse(out.stdout);+ } finally {+ fs.rmSync(dir, { recursive: true, force: true });+ }+ };+ const lazyEdit = { session_id: 'lazy', tool_name: 'Edit', tool_input: { file_path: '/tmp/gateguard-lazy/src/widget.js', old_string: 'a', new_string: 'b' } };+ const lazyBash = command => ({ session_id: 'lazy', tool_name: 'Bash', tool_input: { command } });+ const lazyCases = [+ ['a first shell command loads only the read-only check', lazyBash('npm test'), [], ['gateguard-readonly-shell', 'gateguard-state']],+ ['a shell command after the routine gate loads only the state helpers', lazyBash('npm run build'), [lazyBash('npm test')], ['gateguard-state']],+ ['an edit of a checked file loads only the target classification', lazyEdit, [lazyEdit], ['gateguard-state', 'gateguard-target-class']],+ ['a first touch without a transcript loads no search matching', lazyEdit, [], ['file-tail', 'gateguard-change-profile', 'gateguard-code-lexer', 'gateguard-file-context', 'gateguard-state', 'gateguard-target-class', 'gateguard-turn-scan']],+ ['a first touch of an instruction file skips the change profile', { ...lazyEdit, tool_input: { ...lazyEdit.tool_input, file_path: '/tmp/gateguard-lazy/CLAUDE.md' } }, [], ['file-tail', 'gateguard-state', 'gateguard-target-class', 'gateguard-turn-scan']]+ ];+ for (const [label, payload, setup, expected] of lazyCases) {+ if (test(`lazy loading: ${label}`, () => assert.deepStrictEqual(loadedLibs(payload, setup), expected))) passed++;+ else failed++;}const ddResults = runDdRegressionTests();