|
| 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