Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions bin/gstack-skill-gates
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Print a Claude Code settings JSON that registers the named gates (the
* /investigate gate, the /plan-ceo-review gate) and turns their switches on, for \`claude -p --settings <file>\` or the Agent
* SDK's settings, so a headless run needs no settings.json edit.
* gstack-skill-gates hook-timeout
* Print the hook timeout in seconds (HOOK_TIMEOUT_S) that ./setup registers.
* gstack-skill-gates report [--session <id>] [--expect armed] [--json]
* Count fired, blocked, released, exhausted, observed and error markers per session
* from $GSTACK_STATE_ROOT/skill-gates/<session>/record.json.
Expand All @@ -16,12 +18,12 @@
*/
import * as fs from 'fs';
import * as path from 'path';
import { gatesRoot, readGateRecord, recordPath, type GateHook, type GateRecord } from '../lib/skill-gates';
import { HOOK_TIMEOUT_S, gatesRoot, readGateRecord, recordPath, type GateHook, type GateRecord } from '../lib/skill-gates';

const ROOT = path.resolve(import.meta.dir, '..');
/** Claude Code runs a hook command through a shell: quote the path as gstack-settings-hook's gsQuoteCmd does. */
const shellCommand = (p: string) => (/[\s$`"\\]/.test(p) ? `"${p.replace(/[\\"$`]/g, ch => `\\${ch}`)}"` : p);
const USAGE = 'Usage: gstack-skill-gates settings [--investigate] [--ceo-review]\n gstack-skill-gates report [--session <id>] [--expect armed] [--json]';
const USAGE = 'Usage: gstack-skill-gates settings [--investigate] [--ceo-review]\n gstack-skill-gates hook-timeout\n gstack-skill-gates report [--session <id>] [--expect armed] [--json]';
const HOOKS: Record<string, { env: string; source: GateHook; events: Array<{ event: string; shim: string; matcher?: string }> }> = {
'--investigate': {
env: 'GSTACK_INVESTIGATE_GATE',
Expand Down Expand Up @@ -59,14 +61,16 @@ if (args[0] === 'settings') {
const h = HOOKS[flag];
settings.env[h.env] = 'on';
for (const e of h.events) {
(settings.hooks[e.event] ??= []).push({ ...(e.matcher ? { matcher: e.matcher } : {}), hooks: [{ type: 'command', command: shellCommand(path.join(ROOT, 'hosts', 'claude', 'hooks', e.shim)), timeout: 10 }] });
(settings.hooks[e.event] ??= []).push({ ...(e.matcher ? { matcher: e.matcher } : {}), hooks: [{ type: 'command', command: shellCommand(path.join(ROOT, 'hosts', 'claude', 'hooks', e.shim)), timeout: HOOK_TIMEOUT_S }] });
}
}
process.stdout.write(`${JSON.stringify(settings, null, 2)}\n`);
process.exit(0);
}

if (args[0] !== 'report') usage('expected settings or report');
if (args[0] === 'hook-timeout') { process.stdout.write(`${HOOK_TIMEOUT_S}\n`); process.exit(0); }

if (args[0] !== 'report') usage('expected settings, hook-timeout or report');
const flag = (name: string) => { const i = args.indexOf(name); return i >= 0 ? (args[i + 1] ?? usage(`${name} needs a value`)) : undefined; };
const session = flag('--session');
const expect = flag('--expect');
Expand Down
1 change: 1 addition & 0 deletions changelog.d/gst-1615-gate-hook-timeout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **The /investigate and /plan-ceo-review gates no longer pass a slow scan unchecked.** Their hooks were registered with a 10 second timeout while `GSTACK_SKILL_GATE_DEADLINE_MS` could set a longer gate deadline, and Claude Code kills a hook at its timeout and lets the stop through with no marker. `gstack-skill-gates settings` and `./setup` now register a timeout derived from the longest deadline plus a margin (25 seconds), and a deadline above 20 seconds is clamped to 20. A killed hook still cannot fail closed; docs/skill-gates.md#hook-timeout says why. Re-run `./setup` to update an existing registration. (GST-1615)
11 changes: 10 additions & 1 deletion docs/skill-gates.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,23 @@ A gate never blocks because it broke. On any error it writes an `error` marker w
|---|---|---|
| <a id="gate-bad-payload"></a>`GATE_BAD_PAYLOAD` | the hook input is not a JSON object with `session_id` and `transcript_path`, or is over 1 MiB | a Claude Code version change; update gstack |
| <a id="gate-transcript"></a>`GATE_TRANSCRIPT` | the transcript is missing, over 32 MiB, or not a shape the parser knows, or the /plan-ceo-review load record has no timestamp | see [skill-routing-and-contracts.md](skill-routing-and-contracts.md#exit-4-unknown-shape) |
| <a id="gate-timeout"></a>`GATE_TIMEOUT` | the hook ran past its 5 second deadline (`GSTACK_SKILL_GATE_DEADLINE_MS` changes it) | a very large dirty tree or a slow disk |
| <a id="gate-timeout"></a>`GATE_TIMEOUT` | the hook ran past its 5 second deadline (`GSTACK_SKILL_GATE_DEADLINE_MS` changes it, up to 20 seconds; a larger value is clamped to 20) | a very large dirty tree or a slow disk |
| <a id="gate-git"></a>`GATE_GIT` | a git command failed or HEAD stopped resolving | run the named git command in the repository |
| <a id="gate-state"></a>`GATE_STATE` | the session record or baseline could not be read or written | make `$GSTACK_STATE_ROOT` writable, or delete `skill-gates/<session id>/` |
| <a id="gate-locked"></a>`GATE_LOCKED` | another hook held the session lock for over 3 seconds | transient; a lock older than 30 seconds is taken over |
| <a id="gate-slug"></a>`GATE_SLUG` | /plan-ceo-review: `bin/gstack-slug` failed, so the ceo-plans directory could not be checked | run `gstack-slug` in the repository; `GSTACK_PROJECT_SLUG` pins it |
| <a id="gate-restore-failed"></a>`GATE_RESTORE_FAILED` | a changed file could not be put back | the block lists restored and failed files; run `git diff HEAD -- <files>` and fix them by hand |
| <a id="gate-unexpected"></a>`GATE_UNEXPECTED` | any other throw | report it with the trace log line |

<a id="hook-timeout"></a>
### Hook timeout: a killed hook cannot fail closed

Claude Code kills a hook that runs past the `timeout` in its registration and then lets the stop or tool call through as if the hook had allowed it. It writes nothing to the stream output about the kill, and the hook settings have no option that makes a timed-out hook block. A gate killed this way leaves no `error` marker either, because its own deadline never fired, so `gstack-skill-gates report` cannot see it. There is no way for the gate to fail closed after a kill.

So every gate registration outlives the gate. `HOOK_TIMEOUT_S` in `lib/skill-gates.ts` is the longest deadline (20 seconds) plus a 5 second margin for Bun startup and the `GATE_TIMEOUT` marker write, rounded up: 25 seconds. `gstack-skill-gates settings` writes it on every hook, and `./setup` reads it from `gstack-skill-gates hook-timeout`; re-running setup updates an older 10 second registration. A slow scan then ends in the gate's own `GATE_TIMEOUT`, which is recorded and makes `report` exit 3. Keep your own settings files at or above this number.

Tested with Claude Code 2.1.318 through `claude -p --settings` only: a Stop hook killed at a 3 second timeout let the stop through, and the same hook with a 15 second timeout blocked it (GST-1615).

## Exit codes

`gstack-skill-gates report [--session <id>] [--expect armed] [--json]` lists each session's gate counts: runs, `fired`, `blocked`, `released`, `exhausted`, `observed` and `errors`.
Expand Down
19 changes: 18 additions & 1 deletion lib/skill-gates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,26 @@ export type GateHook = 'investigate-gate' | 'ceo-review-gate';
export type GateEvent = 'fired' | 'blocked' | 'released' | 'observed' | 'exhausted' | 'skipped' | 'error';
export const GATE_DOC = 'docs/skill-gates.md';
export const DEADLINE_MS = 5000;
/** The most GSTACK_SKILL_GATE_DEADLINE_MS can set; a larger value is clamped to it. */
export const DEADLINE_MAX_MS = 20_000;
const STDIN_CAP_BYTES = 1024 * 1024;
const STDIN_DEADLINE_MS = 500;
const LOCK_WAIT_MS = 3000;
/** What a hook needs beyond its deadline: Bun startup before the timer starts, and the GATE_TIMEOUT marker write, which can wait out the session lock. */
export const HOOK_MARGIN_MS = LOCK_WAIT_MS + 2000;
/**
* The timeout, in seconds, on every gate hook registration (gstack-skill-gates
* settings, and ./setup through `gstack-skill-gates hook-timeout`). Claude Code
* kills a hook at its timeout and lets the stop or call through with no marker,
* so the registration must outlive the longest deadline plus the margin.
*/
export const HOOK_TIMEOUT_S = Math.ceil((DEADLINE_MAX_MS + HOOK_MARGIN_MS) / 1000);

/** The hook's deadline: GSTACK_SKILL_GATE_DEADLINE_MS when positive, clamped to DEADLINE_MAX_MS, else DEADLINE_MS. */
export function deadlineFromEnv(env: NodeJS.ProcessEnv = process.env): number {
const ms = Number(env.GSTACK_SKILL_GATE_DEADLINE_MS);
return ms > 0 ? Math.min(ms, DEADLINE_MAX_MS) : DEADLINE_MS;
}
const LOCK_STALE_MS = 30_000;
const SESSION_DIRS_KEPT_MS = 7 * 24 * 60 * 60 * 1000;

Expand Down Expand Up @@ -184,7 +201,7 @@ export async function runGateHook(hook: GateHook, isOn: () => boolean, handler:
process.stderr.write(`${marker} (stop and edits allowed; see ${GATE_DOC}#${code.toLowerCase().replace(/_/g, '-')})\n`);
process.stdout.write(JSON.stringify({ systemMessage: marker }));
};
deadlineMs = Number(process.env.GSTACK_SKILL_GATE_DEADLINE_MS) || DEADLINE_MS;
deadlineMs = deadlineFromEnv();
deadlineAt = Date.now() + deadlineMs;
const timer = setTimeout(() => { fail('GATE_TIMEOUT', `timed out after ${deadlineMs} ms`); process.exit(0); }, deadlineMs);
try {
Expand Down
9 changes: 7 additions & 2 deletions setup
Original file line number Diff line number Diff line change
Expand Up @@ -4971,6 +4971,11 @@ _skill_hook_gate skill_routing "$SKILL_ROUTING_MODE" GSTACK_SKILL_ROUTING UserPr
_skill_hook_gate skill_contracts "$SKILL_CONTRACTS_MODE" GSTACK_SKILL_CONTRACTS Stop \
"$SKILL_CONTRACT_HOOK" gstack-skill-contract 10 \
"a contract skill that ends without its required outputs is blocked once"
# Both skill gates register HOOK_TIMEOUT_S from lib/skill-gates.ts, the longest
# gate deadline plus a margin (GST-1615): Claude Code kills a hook at its
# timeout and lets the stop through unchecked. Empty if bun cannot run, which
# omits the timeout and leaves Claude Code's default.
SKILL_GATE_HOOK_TIMEOUT="$(bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$SOURCE_GSTACK_DIR/bin/gstack-skill-gates" hook-timeout 2>/dev/null || true)"
# The /investigate gate (GST-1461, docs/skill-gates.md) registers one shim per
# event; each basename has its own KNOWN_HOOKS row under one source tag.
INVESTIGATE_GATE_PRE="$(_hook_command_path hosts/claude/hooks/investigate-gate-pre || true)"
Expand All @@ -4979,7 +4984,7 @@ INVESTIGATE_GATE_POST_FAILURE="$(_hook_command_path hosts/claude/hooks/investiga
INVESTIGATE_GATE_STOP="$(_hook_command_path hosts/claude/hooks/investigate-gate-stop || true)"
INVESTIGATE_GATE_EDIT_MATCHER="Edit|Write|MultiEdit|NotebookEdit|Bash"
_skill_hook_gate investigate_gate "$INVESTIGATE_GATE_MODE" GSTACK_INVESTIGATE_GATE PreToolUse \
"$INVESTIGATE_GATE_PRE" gstack-investigate-gate 10 \
"$INVESTIGATE_GATE_PRE" gstack-investigate-gate "$SKILL_GATE_HOOK_TIMEOUT" \
"after /investigate loads, changes to tracked source wait for a stated root cause" \
"$INVESTIGATE_GATE_EDIT_MATCHER" \
PostToolUse "$INVESTIGATE_GATE_POST" Bash \
Expand All @@ -4990,7 +4995,7 @@ _skill_hook_gate investigate_gate "$INVESTIGATE_GATE_MODE" GSTACK_INVESTIGATE_GA
CEO_REVIEW_GATE_STOP="$(_hook_command_path hosts/claude/hooks/ceo-review-gate-stop || true)"
CEO_REVIEW_GATE_SUBAGENT_STOP="$(_hook_command_path hosts/claude/hooks/ceo-review-gate-subagent-stop || true)"
_skill_hook_gate ceo_review_gate "$CEO_REVIEW_GATE_MODE" GSTACK_CEO_REVIEW_GATE Stop \
"$CEO_REVIEW_GATE_STOP" gstack-ceo-review-gate 10 \
"$CEO_REVIEW_GATE_STOP" gstack-ceo-review-gate "$SKILL_GATE_HOOK_TIMEOUT" \
"after /plan-ceo-review loads, its final stop waits for ranked top risks in the output file" "" \
SubagentStop "$CEO_REVIEW_GATE_SUBAGENT_STOP" ""

Expand Down
8 changes: 5 additions & 3 deletions test/pr-shared-input-selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ test('cumulative shared, native and prompt edits retain every gate case and judg

// GST-1575: no paid run executes ./setup (evals.yml only builds), so a narrow screen derives it like any
// other file. Diffs of #4045 (9e809ab) and #4047 (4743132), whose narrow screens (runs 42093259982 and
// 42103721891) fell back to all gate cases on setup alone.
// 42103721891) fell back to all gate cases on setup alone. GST-1615: setup now runs bin/gstack-skill-gates, so
// that file derives setup's consumers and the screen runs in dependents mode, which also keeps the gate case
// whose touchfiles name bin/gstack-config (ship-skipped-queued-finding) instead of intersecting with the profile.
const setupHookCases = ['gstack-upgrade-happy-path', 'ship-local-hook-preservation', 'ship-managed-hook-refresh',
'ship-unmanaged-hook-consent', 'skillmd-setup-discovery'];
'ship-skipped-queued-finding', 'ship-unmanaged-hook-consent', 'skillmd-setup-discovery'];
const setupScreens: Record<string, string[]> = {
'#4045': ['bin/gstack-config', 'bin/gstack-settings-hook', 'bin/gstack-skill-gates', 'bin/gstack-skill-route',
'changelog.d/capy-gst-1461-investigate-gate.md', 'docs/skill-gates.md', 'docs/skill-routing-and-contracts.md',
Expand All @@ -112,7 +114,7 @@ const setupScreens: Record<string, string[]> = {
const screen = (policy: 'narrow' | 'broad', changedFiles: string[]) => computePaidCaseSelection({
profile: 'pr', env: { EVALS_BASE: 'fixture-base', EVALS_SCREEN_POLICY: policy }, changedFiles, packageVersionOnly: false });

test.each(Object.entries(setupScreens))('a narrow screen of %s derives setup and selects its five cases', (_pr, files) => {
test.each(Object.entries(setupScreens))('a narrow screen of %s derives setup and selects its six cases', (_pr, files) => {
const result = screen('narrow', files);
expect(result.coverage?.mode).not.toBe('full-fallback');
expect(result.selection.e2e).toEqual(setupHookCases);
Expand Down
26 changes: 22 additions & 4 deletions test/setup-skill-hooks-gate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } fr
import { spawnSync } from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { HOOK_TIMEOUT_S } from '../lib/skill-gates';
import { SWITCH_DEFAULT } from '../lib/skill-route';
import {
J1_TRANSCRIPT, ROOT, hookEnv, runHook, skillsDir, tmpDir,
Expand All @@ -21,6 +22,7 @@ import {
const setupSrc = fs.readFileSync(path.join(ROOT, 'setup'), 'utf8');
const settingsHookSrc = fs.readFileSync(path.join(ROOT, 'bin', 'gstack-settings-hook'), 'utf8');
const gateFn = setupSrc.slice(setupSrc.indexOf('_skill_hook_gate() {'), setupSrc.indexOf('\n}\n', setupSrc.indexOf('_skill_hook_gate() {')) + 3);
const gateTimeoutLine = setupSrc.match(/^SKILL_GATE_HOOK_TIMEOUT=.*$/m)?.[0] ?? 'echo "setup has no SKILL_GATE_HOOK_TIMEOUT line" >&2; exit 9';

let state = '', home = '';
beforeEach(() => { state = tmpDir('gstack-gate-state-'); home = tmpDir('gstack-gate-home-'); });
Expand All @@ -32,9 +34,9 @@ const settings = () => (fs.existsSync(settingsFile()) ? JSON.parse(fs.readFileSy
const GATES = {
skill_routing: 'GSTACK_SKILL_ROUTING UserPromptSubmit "$2/hosts/claude/hooks/skill-route-hook" gstack-skill-route 5 "routing hint"',
skill_contracts: 'GSTACK_SKILL_CONTRACTS Stop "$2/hosts/claude/hooks/skill-contract-stop-hook" gstack-skill-contract 10 "contract check"',
investigate_gate: 'GSTACK_INVESTIGATE_GATE PreToolUse "$2/hosts/claude/hooks/investigate-gate-pre" gstack-investigate-gate 10 "investigate gate" "Edit|Write|MultiEdit|NotebookEdit|Bash"'
investigate_gate: 'GSTACK_INVESTIGATE_GATE PreToolUse "$2/hosts/claude/hooks/investigate-gate-pre" gstack-investigate-gate "$SKILL_GATE_HOOK_TIMEOUT" "investigate gate" "Edit|Write|MultiEdit|NotebookEdit|Bash"'
+ ' PostToolUse "$2/hosts/claude/hooks/investigate-gate-post" Bash PostToolUseFailure "$2/hosts/claude/hooks/investigate-gate-post-failure" Bash Stop "$2/hosts/claude/hooks/investigate-gate-stop" ""',
ceo_review_gate: 'GSTACK_CEO_REVIEW_GATE Stop "$2/hosts/claude/hooks/ceo-review-gate-stop" gstack-ceo-review-gate 10 "ceo review gate" ""'
ceo_review_gate: 'GSTACK_CEO_REVIEW_GATE Stop "$2/hosts/claude/hooks/ceo-review-gate-stop" gstack-ceo-review-gate "$SKILL_GATE_HOOK_TIMEOUT" "ceo review gate" ""'
+ ' SubagentStop "$2/hosts/claude/hooks/ceo-review-gate-subagent-stop" ""',
};

Expand All @@ -47,6 +49,8 @@ function gate(mode: string, env: Record<string, string> = {}, noTeam = 0, root =
`SETTINGS_HOOK="${path.join(ROOT, 'bin', 'gstack-settings-hook')}"`,
'SKILL_ROUTE_BIN="$2/bin/gstack-skill-route"',
`IS_WINDOWS=0; NO_TEAM_MODE=${noTeam}`,
'SOURCE_GSTACK_DIR="$2"',
gateTimeoutLine,
gateFn,
`_skill_hook_gate ${key} "$1" ${GATES[key]}`,
].join('\n');
Expand Down Expand Up @@ -273,7 +277,7 @@ describe('setup: the investigate_gate switch (GST-1461) registers one shim per e
expect(entries).toHaveLength(1);
expect(entries[0].matcher ?? undefined).toBe(matcher);
expect(entries[0].hooks[0].command).toEndWith(`hosts/claude/hooks/${shim}`);
expect(entries[0].hooks[0].timeout).toBe(10);
expect(entries[0].hooks[0].timeout).toBe(HOOK_TIMEOUT_S);
}
expect(gate('', {}, 0, ROOT, 'investigate_gate')).toEqual({ code: 0, out: '' });
gate('off', {}, 0, ROOT, 'investigate_gate');
Expand Down Expand Up @@ -318,7 +322,7 @@ describe('setup: the ceo_review_gate switch (GST-1461) registers Stop and Subage
expect(entries).toHaveLength(1);
expect(entries[0].matcher ?? undefined).toBeUndefined();
expect(entries[0].hooks[0].command).toEndWith(`hosts/claude/hooks/${shim}`);
expect(entries[0].hooks[0].timeout).toBe(10);
expect(entries[0].hooks[0].timeout).toBe(HOOK_TIMEOUT_S);
}
expect(gate('', {}, 0, ROOT, 'ceo_review_gate')).toEqual({ code: 0, out: '' });
gate('off', {}, 0, ROOT, 'ceo_review_gate');
Expand All @@ -330,3 +334,17 @@ describe('setup: the ceo_review_gate switch (GST-1461) registers Stop and Subage
expect(r.stdout).toMatch(/^ {2}ceo_review_gate: +off$/m);
});
});

describe('setup: both skill gates register the hook timeout lib/skill-gates.ts derives (GST-1615)', () => {
test('source: one SKILL_GATE_HOOK_TIMEOUT from gstack-skill-gates hook-timeout, passed to both gates', () => {
expect(gateTimeoutLine).toBe('SKILL_GATE_HOOK_TIMEOUT="$(bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$SOURCE_GSTACK_DIR/bin/gstack-skill-gates" hook-timeout 2>/dev/null || true)"');
expect(setupSrc).toContain('"$INVESTIGATE_GATE_PRE" gstack-investigate-gate "$SKILL_GATE_HOOK_TIMEOUT" \\');
expect(setupSrc).toContain('"$CEO_REVIEW_GATE_STOP" gstack-ceo-review-gate "$SKILL_GATE_HOOK_TIMEOUT" \\');
expect(setupSrc).not.toMatch(/gstack-(investigate|ceo-review)-gate \d/);
});

test('the setup line evaluates to HOOK_TIMEOUT_S', () => {
const r = spawnSync('bash', ['-c', `SOURCE_GSTACK_DIR="$1"\n${gateTimeoutLine}\nprintf %s "$SKILL_GATE_HOOK_TIMEOUT"`, 'gate', ROOT], { env: { PATH: process.env.PATH ?? '', HOME: home }, encoding: 'utf8', timeout: 30_000 });
expect(r.stdout).toBe(String(HOOK_TIMEOUT_S));
});
});
Loading
Loading