fix(hook-check): cover skill frontmatter hooks, sourced helpers, conflict markers and the caller's tsconfig

Rewrite of #3066. bin/gstack-hook-check only listed setup's hooks plus
/autoplan's by name, so the /careful and /freeze PreToolUse checks (also run
by /guard and /investigate), /plan-ceo-review's hand-off hook and the helpers
they source were never parse-checked before Claude Code ran them.

- The hook list is setup's _hook_command_path arguments plus every gstack path
  on a `command:` line in a generated */SKILL.md frontmatter (9 hooks, up from
  6). The generated files are read rather than the .tmpl sources because the
  /autoplan and /plan-ceo-review commands come from resolvers.
- Each shell hook's gstack-tree helpers (`.`/`source` lines, assignments and
  shellcheck source= directives, followed transitively) are parsed too:
  careful/bin/hook-extract.sh and bin/gstack-state-root.sh for /careful and
  /freeze.
- The shim, its helpers, each TypeScript entry and every local module the
  bundle used (bun build --metafile; entry only when bun lacks it) are scanned
  for conflict-marker lines, which still parse inside heredocs, strings and
  template literals.
- bun build runs from the gstack root, so the caller's broken tsconfig.json or
  bunfig.toml no longer fails every TypeScript hook. Lists are newline
  separated, so a root with spaces works.

Tests in test/setup-hook-syntax-gate.test.ts cover each case; the gstack-doctor
fixture now copies autoplan/SKILL.md, whose frontmatter registers the hook its
hooks-row test breaks. docs/troubleshooting.md describes the wider check.

Co-authored-by: Benjamin D. Smith <7134745+BenjaminDSmithy@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
committed 2026-10-08 06:23:53 +00:00
1 parent 2ec5275db5
commit f629bd560d
4 files changed
+178 -22

No files matched your search

+1
View File
@@ -22,6 +22,7 @@ const COPIED = [
'setup', 'VERSION',
'bin/gstack-doctor', 'bin/gstack-codex-status.sh', 'bin/gstack-state-root.sh', 'bin/gstack-install-registry.sh',
'bin/gstack-render-claude.sh', 'bin/gstack-bun-version.sh', 'bin/gstack-hook-check', 'bin/gstack-config',
'autoplan/SKILL.md', // its frontmatter registers the autoplan hook the hooks-row test breaks
];
const bases: string[] = [];
afterEach(() => { for (const b of bases.splice(0)) fs.rmSync(b, { recursive: true, force: true }); });
+73 -5
View File
@@ -47,7 +47,13 @@ afterEach(() => { for (const b of bases.splice(0)) fs.rmSync(b, { recursive: tru
const HOOKS = listHooks(ROOT);
/** A gstack tree with the real hook shims and trivial TypeScript entries. */
/** Skills whose frontmatter registers a hook command. */
const SKILLS_WITH_HOOKS = fs.readdirSync(ROOT).filter((dir) => {
const skill = path.join(ROOT, dir, 'SKILL.md');
return fs.existsSync(skill) && /^---\n[\s\S]*?^\s*command:[\s\S]*?\n---/m.test(fs.readFileSync(skill, 'utf8'));
});
/** A gstack tree with the real hook shims and skill frontmatter, sourced helpers, and trivial TypeScript entries. */
function fixtureTree(root: string) {
for (const hook of HOOKS) {
fs.mkdirSync(path.dirname(path.join(root, hook)), { recursive: true });
@@ -55,6 +61,14 @@ function fixtureTree(root: string) {
fs.chmodSync(path.join(root, hook), 0o755);
if (fs.existsSync(path.join(ROOT, `${hook}.ts`))) fs.writeFileSync(path.join(root, `${hook}.ts`), 'export const ok = 1;\n');
}
for (const dir of SKILLS_WITH_HOOKS) {
fs.mkdirSync(path.join(root, dir), { recursive: true });
fs.copyFileSync(path.join(ROOT, dir, 'SKILL.md'), path.join(root, dir, 'SKILL.md'));
}
for (const helper of ['careful/bin/hook-extract.sh', 'bin/gstack-state-root.sh']) {
fs.mkdirSync(path.dirname(path.join(root, helper)), { recursive: true });
fs.copyFileSync(path.join(ROOT, helper), path.join(root, helper));
}
fs.copyFileSync(path.join(ROOT, 'setup'), path.join(root, 'setup'));
}
@@ -70,13 +84,19 @@ function check(root: string) {
}
describe('gstack-hook-check: the hook list is the registration code', () => {
test('equals every hook setup resolves for registration plus the frontmatter-registered autoplan hook', () => {
test('equals every hook setup resolves for registration plus every gstack path a skill frontmatter command runs', () => {
const registered = [...SETUP.matchAll(/_hook_command_path (\S+)/g)].map(m => m[1]!.replace(/\W+$/, ''));
const frontmatter = [...fs.readFileSync(path.join(ROOT, 'autoplan', 'SKILL.md'), 'utf8')
.matchAll(/skills\/gstack\/(autoplan\/bin\/[a-z-]+-hook)\b/g)].map(m => m[1]!);
const frontmatter = fs.readdirSync(ROOT).flatMap((dir) => {
const skill = path.join(ROOT, dir, 'SKILL.md');
if (!fs.existsSync(skill)) return [];
const front = fs.readFileSync(skill, 'utf8').match(/^---\n([\s\S]*?)\n---/)?.[1] ?? '';
return front.split('\n').filter(line => /^\s*command:/.test(line))
.flatMap(line => [...line.matchAll(/skills\/gstack\/([\w./-]+)/g)].map(m => m[1]!));
});
const expected = [...new Set([...registered, ...frontmatter])].sort();
expect(registered.length).toBeGreaterThanOrEqual(5);
expect(frontmatter.length).toBeGreaterThan(0);
for (const hook of ['autoplan/bin/phase-publication-hook', 'careful/bin/check-careful.sh',
'freeze/bin/check-freeze.sh', 'plan-ceo-review/bin/mode-handoff-hook']) expect(frontmatter).toContain(hook);
expect([...HOOKS].sort()).toEqual(expected);
});
@@ -147,6 +167,54 @@ describe('gstack-hook-check: what fails', () => {
expect(r.out).toContain(`fail hosts/claude/hooks/auq-error-fallback-hook ${entry}:1: missing TypeScript entry`);
});
test('a sourced helper that does not parse fails at the helper', () => {
const root = tmpBase();
fixtureTree(root);
const helper = path.join(root, 'careful/bin/hook-extract.sh');
fs.appendFileSync(helper, 'if then\n');
const r = check(root);
expect(r.code).toBe(1);
expect(r.out).toMatch(new RegExp(`^fail careful/bin/check-careful.sh ${helper}:\\d+: syntax error`, 'm'));
expect(r.out).toMatch(new RegExp(`^fail freeze/bin/check-freeze.sh ${helper}:\\d+: syntax error`, 'm'));
});
test('a conflict marker inside a heredoc of a transitively sourced helper still parses, and fails', () => {
const root = tmpBase();
fixtureTree(root);
const helper = path.join(root, 'bin/gstack-state-root.sh');
const lines = fs.readFileSync(helper, 'utf8').split('\n').length;
fs.appendFileSync(helper, ": <<'EOF'\n<<<<<<< HEAD\nours\n=======\ntheirs\n>>>>>>> branch\nEOF\n");
expect(spawnSync('bash', ['-n', helper], { timeout: 10_000 }).status).toBe(0);
const r = check(root);
expect(r.code).toBe(1);
expect(r.out).toContain(`fail careful/bin/check-careful.sh ${helper}:${lines + 1}: unresolved merge conflict marker`);
expect(r.out).toContain(`fail bin/gstack-session-update ${helper}:${lines + 1}: unresolved merge conflict marker`);
});
test('a conflict marker in a module the TypeScript entry imports fails, even inside a template literal', () => {
const root = tmpBase();
fixtureTree(root);
const dir = path.join(root, 'hosts/claude/hooks');
fs.writeFileSync(path.join(dir, 'question-log-hook.ts'), "import { msg } from './hook-msg';\nconsole.log(msg);\n");
fs.writeFileSync(path.join(dir, 'hook-msg.ts'), 'export const msg = `\n<<<<<<< HEAD\na\n=======\nb\n>>>>>>> other\n`;\n');
const r = check(root);
expect(r.code).toBe(1);
expect(r.out).toContain(`fail hosts/claude/hooks/question-log-hook ${path.join(dir, 'hook-msg.ts')}:2: unresolved merge conflict marker`);
});
test("the caller's tsconfig.json and bunfig.toml are not read, and a root with spaces works", () => {
const base = tmpBase();
const root = path.join(base, 'gstack root');
fixtureTree(root);
const caller = path.join(base, 'project');
fs.mkdirSync(caller);
fs.writeFileSync(path.join(caller, 'tsconfig.json'), '{ "compilerOptions": { ,,, ');
fs.writeFileSync(path.join(caller, 'bunfig.toml'), '[[[ not toml');
const r = spawnSync('bash', [CHECK, root], { cwd: caller, encoding: 'utf8', timeout: 30_000 });
expect(`${r.stdout}${r.stderr}`).not.toContain('fail ');
expect(r.status).toBe(0);
});
test('a missing shim is reported as missing, not failed (setup already skips registering it)', () => {
const root = tmpBase();
fixtureTree(root);