Skip to content

Commit 659b197

Browse files
fix(setup): refuse to register a hook that does not parse (#3032)
Claude Code runs gstack's hook shims through /bin/sh; a shim that does not parse exits 2, which for a PreToolUse hook blocks the tool call in every session until someone finds the file. Before any hook registration is healed or written, setup now runs bash -n on each registered shim and a Bun parse of its TypeScript entry, and stops with the file, line and fix. A lean rewrite of the PR's idea (one gate block plus a test that runs it). Co-authored-by: BenjaminDSmithy <[email protected]>
1 parent 4441c8f commit 659b197

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎setup‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,6 +3710,33 @@ _hook_command_path() {
37103710
return 1
37113711
}
37123712

3713+
# Hook syntax gate (#3032): Claude Code runs these shims through /bin/sh, and a
3714+
# shell file that does not parse exits 2, which for a PreToolUse hook blocks the
3715+
# tool call in every session on the machine. A half-edited or half-merged hook
3716+
# must stop setup with its file and line before any registration is written or
3717+
# healed, not install silently.
3718+
_HOOK_SYNTAX_PROBLEMS=""
3719+
for _hook in bin/gstack-session-update hosts/claude/hooks/question-log-hook \
3720+
hosts/claude/hooks/question-preference-hook hosts/claude/hooks/auq-error-fallback-hook \
3721+
hosts/claude/hooks/timeline-stop-hook autoplan/bin/phase-publication-hook; do
3722+
_hook_file="$CANONICAL_GSTACK_ROOT/$_hook"
3723+
[ -f "$_hook_file" ] || continue
3724+
if ! _hook_err=$(bash -n "$_hook_file" 2>&1); then
3725+
_HOOK_SYNTAX_PROBLEMS="${_HOOK_SYNTAX_PROBLEMS}
3726+
$_hook_err"
3727+
fi
3728+
if [ -f "$_hook_file.ts" ] && ! _hook_err=$(bun_cmd build --no-bundle --target=bun "$_hook_file.ts" 2>&1 >/dev/null); then
3729+
_HOOK_SYNTAX_PROBLEMS="${_HOOK_SYNTAX_PROBLEMS}
3730+
$_hook_file.ts:
3731+
$(printf '%s\n' "$_hook_err" | sed 's/^/ /')"
3732+
fi
3733+
done
3734+
if [ -n "$_HOOK_SYNTAX_PROBLEMS" ]; then
3735+
echo "gstack setup: refusing to register hooks: a hook file does not parse, and Claude Code would block tool calls with it:$_HOOK_SYNTAX_PROBLEMS" >&2
3736+
echo "Fix: repair the file above (git -C \"$SOURCE_GSTACK_DIR\" status shows half-applied edits or merge conflicts), then re-run ./setup." >&2
3737+
exit 1
3738+
fi
3739+
37133740
# Heal-first: prune dead gstack hook entries and re-point survivors at the
37143741
# stable install BEFORE any tag-presence guard below (a dead entry carrying the
37153742
# tag otherwise blocks re-registration forever — the missing-Stop-hook failure
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* #3032: Claude Code runs gstack's hook shims through /bin/sh. A shim that
3+
* does not parse exits 2, which for a PreToolUse hook blocks the tool call in
4+
* every session. setup's gate parses every registered hook (bash -n on the
5+
* shim, a Bun parse of its TypeScript entry) and stops with the file and line
6+
* before any registration is written or healed.
7+
*
8+
* Runs the real gate block extracted from setup against a fixture install;
9+
* driving all of ./setup here is disproportionate.
10+
*/
11+
import { describe, expect, test } from 'bun:test';
12+
import { spawnSync } from 'node:child_process';
13+
import * as fs from 'node:fs';
14+
import * as os from 'node:os';
15+
import * as path from 'node:path';
16+
17+
const ROOT = path.resolve(import.meta.dir, '..');
18+
const SETUP = fs.readFileSync(path.join(ROOT, 'setup'), 'utf8');
19+
const START = SETUP.indexOf('# Hook syntax gate (#3032)');
20+
const END = SETUP.indexOf('# Heal-first: prune dead gstack hook entries');
21+
const GATE = SETUP.slice(START, END);
22+
const HOOKS = ['bin/gstack-session-update', 'hosts/claude/hooks/question-log-hook', 'hosts/claude/hooks/question-preference-hook',
23+
'hosts/claude/hooks/auq-error-fallback-hook', 'hosts/claude/hooks/timeline-stop-hook', 'autoplan/bin/phase-publication-hook'];
24+
25+
function fixture(): string {
26+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'hook-syntax-gate-'));
27+
for (const hook of HOOKS) {
28+
fs.mkdirSync(path.dirname(path.join(root, hook)), { recursive: true });
29+
fs.copyFileSync(path.join(ROOT, hook), path.join(root, hook));
30+
if (fs.existsSync(path.join(ROOT, `${hook}.ts`))) fs.writeFileSync(path.join(root, `${hook}.ts`), 'export const ok = 1;\n');
31+
}
32+
return root;
33+
}
34+
35+
function runGate(root: string) {
36+
const script = `bun_cmd() { bun "$@"; }\nCANONICAL_GSTACK_ROOT='${root}'\nSOURCE_GSTACK_DIR='${root}'\n${GATE}\necho GATE_PASSED`;
37+
const r = spawnSync('bash', ['-c', script], { encoding: 'utf8', timeout: 30_000 });
38+
return { code: r.status, out: `${r.stdout}${r.stderr}` };
39+
}
40+
41+
describe('#3032: setup refuses to register a hook that does not parse', () => {
42+
test('the gate runs before any hook registration is healed or written', () => {
43+
expect(START).toBeGreaterThan(-1);
44+
expect(END).toBeGreaterThan(START);
45+
const writes = [...SETUP.matchAll(/"\$SETTINGS_HOOK" (?:ensure-event|add-event|prune-stale|remove-source)/g)].map(m => m.index!);
46+
expect(writes.length).toBeGreaterThan(0);
47+
expect(Math.min(...writes)).toBeGreaterThan(END);
48+
});
49+
50+
test('healthy hooks pass', () => {
51+
const root = fixture();
52+
try { expect(runGate(root)).toEqual({ code: 0, out: 'GATE_PASSED\n' }); }
53+
finally { fs.rmSync(root, { recursive: true, force: true }); }
54+
});
55+
56+
test('a half-merged shell shim stops setup with its file and line', () => {
57+
const root = fixture();
58+
try {
59+
const shim = path.join(root, 'hosts/claude/hooks/question-preference-hook');
60+
fs.appendFileSync(shim, '<<<<<<< HEAD\nif [ -n "$x" ]; then\n');
61+
const r = runGate(root);
62+
expect(r.code).toBe(1);
63+
expect(r.out).toContain('refusing to register hooks');
64+
expect(r.out).toContain(shim);
65+
expect(r.out).not.toContain('GATE_PASSED');
66+
} finally { fs.rmSync(root, { recursive: true, force: true }); }
67+
});
68+
69+
test('a TypeScript entry with a syntax error stops setup with its file', () => {
70+
const root = fixture();
71+
try {
72+
const ts = path.join(root, 'hosts/claude/hooks/timeline-stop-hook.ts');
73+
fs.writeFileSync(ts, 'export const x = {;\n');
74+
const r = runGate(root);
75+
expect(r.code).toBe(1);
76+
expect(r.out).toContain(`${ts}:`);
77+
expect(r.out).toContain('Expected identifier');
78+
} finally { fs.rmSync(root, { recursive: true, force: true }); }
79+
});
80+
81+
test("every shipped hook in this checkout parses", () => {
82+
const r = runGate(ROOT);
83+
expect(r.out).toContain('GATE_PASSED');
84+
});
85+
});

0 commit comments

Comments
 (0)