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

+87 -9
View File
@@ -13,14 +13,21 @@
#
# The hook list comes from the registration code: every
# `_hook_command_path <path>` argument in <root>/setup (or the setup named by
# --setup, when another tree's setup registers <root>'s hooks), plus the autoplan
# phase-publication hook (registered by its skill frontmatter).
# test/setup-hook-syntax-gate.test.ts keeps the two equal.
# --setup, when another tree's setup registers <root>'s hooks), plus every
# gstack path on a `command:` line in a skill's frontmatter (<root>/*/SKILL.md:
# /autoplan, /careful, /freeze, /guard, /investigate, /plan-ceo-review). Claude
# Code runs those while the skill is active, and a broken PreToolUse one blocks
# the tool call. test/setup-hook-syntax-gate.test.ts keeps the lists equal.
#
# Each shim is parsed by the interpreter its shebang names (bash -n, sh -n).
# Each TypeScript entry the shim runs (a `<name>.ts` beside it) is bundled with
# its local imports into a temp directory, packages external, so a broken or
# missing local import fails; a missing entry fails.
# Each shim is parsed by the interpreter its shebang names (bash -n, sh -n),
# and so is every gstack shell file it sources, transitively. Each TypeScript
# entry the shim runs (a `<name>.ts` beside it) is bundled with its local
# imports into a temp directory, packages external, so a broken or missing
# local import fails; a missing entry fails. Bundles run from <root>, so the
# caller's tsconfig.json and bunfig.toml are never read. The shim, its sourced
# helpers, each entry and every local module the entry bundles are scanned for
# merge conflict marker lines, which still parse inside a heredoc, a string, a
# comment or a template literal.
#
# Output, one line per hook:
# ok <hook>
@@ -44,11 +51,18 @@ if [ -z "$ROOT" ] || [ ! -d "$ROOT" ]; then
echo "usage: gstack-hook-check [--setup <file>] [--list] <gstack root>" >&2
exit 2
fi
case "$ROOT" in /*) ;; *) ROOT="$PWD/$ROOT" ;; esac
[ "$ROOT" = / ] || ROOT=${ROOT%/}
SETUP_FILE="${SETUP_FILE:-$ROOT/setup}"
# Frontmatter is the block between a SKILL.md's first two `---` lines.
hook_list() {
sed -n 's/.*\$(_hook_command_path \([A-Za-z0-9._/-]*\).*/\1/p' "$SETUP_FILE" 2>/dev/null
echo autoplan/bin/phase-publication-hook
for skill in "$ROOT"/*/SKILL.md; do
[ -f "$skill" ] || continue
tr -d '\r' < "$skill" | awk 'NR == 1 && !/^---/ { exit } NR > 1 && /^---/ { exit } /^[[:space:]]*command:/' \
| grep -oE 'skills/gstack/[A-Za-z0-9._/-]+' | sed 's|^skills/gstack/||'
done
}
HOOKS=$(hook_list | awk '!seen[$0]++')
@@ -79,9 +93,50 @@ first_message() {
printf '%s\n' "$_msg"
}
# Lists below are newline-separated, so a root with spaces stays one word.
NL='
'
IFS=$NL
FAILED=0
fail() { echo "fail $1 $2: $3"; FAILED=1; }
# sourced_by <file> — the gstack files <file> sources: each `*.sh` path on a
# `.`/`source` line, in a variable assignment, or in a `# shellcheck source=`
# directive. A path resolves from <root>, then beside <file>, then beside
# <file> without its first segment (a `$DIR/` prefix); only files inside
# <root> count.
sourced_by() {
_sdir=$(dirname "$1")
grep -E '^[[:space:]]*#[[:space:]]*shellcheck[[:space:]].*source=|(^|[;&|({[:space:]])(\.|source)[[:space:]]|^[[:space:]]*(local |export |readonly )?[A-Za-z_][A-Za-z0-9_]*=' "$1" \
| grep -oE '[A-Za-z0-9._/-]*\.sh([^A-Za-z0-9_]|$)' | sed -E 's/\.sh.*$/.sh/' | awk '!seen[$0]++' | while IFS= read -r _tok; do
for _cand in "$ROOT/$_tok" "$_sdir/$_tok" "$_sdir/${_tok#*/}"; do
[ -f "$_cand" ] || continue
_cand="$(cd "$(dirname "$_cand")" && pwd)/$(basename "$_cand")"
case "$_cand" in "$ROOT"/*) [ "$_cand" != "$1" ] && echo "$_cand"; break ;; esac
done
done
}
# helpers_of <shim> — sourced_by, followed transitively.
helpers_of() {
_seen="$NL$1$NL" _queue=$1
while [ -n "$_queue" ]; do
_next=""
for _f in $_queue; do
for _c in $(sourced_by "$_f"); do
case "$_seen" in *"$NL$_c$NL"*) continue ;; esac
_seen="$_seen$_c$NL"
echo "$_c"
_next="$_next$_c$NL"
done
done
_queue=$_next
done
}
META=0
"$BUN" build --help 2>/dev/null | grep -q -- '--metafile' && META=1
for hook in $HOOKS; do
shim="$ROOT/$hook"
if [ ! -f "$shim" ]; then echo "missing $hook"; continue; fi
@@ -100,6 +155,14 @@ for hook in $HOOKS; do
fail "$hook" "$(first_location "$shim" "$err")" "$(first_message "$err")"
hook_ok=0
fi
scan="$shim"
for helper in $(helpers_of "$shim"); do
if ! err=$("$interp" -n "$helper" 2>&1); then
fail "$hook" "$(first_location "$helper" "$err")" "$(first_message "$err")"
hook_ok=0
fi
scan="$scan$NL$helper"
done
dir=$(dirname "$shim")
for entry in $(grep -v '^[[:space:]]*#' "$shim" | grep -oE '[A-Za-z0-9._-]+\.ts([^A-Za-z0-9_]|$)' | sed -E 's/\.ts.*$/.ts/' | awk '!seen[$0]++'); do
if [ ! -f "$dir/$entry" ]; then
@@ -107,10 +170,25 @@ for hook in $HOOKS; do
hook_ok=0
continue
fi
if ! err=$("$BUN" build --target=bun --packages=external --outdir "$TMP_OUT/$(basename "$hook")" "$dir/$entry" 2>&1 >/dev/null); then
meta="$TMP_OUT/meta.json" metaflag=""
rm -f "$meta"
[ "$META" -eq 1 ] && metaflag="--metafile=$meta"
if ! err=$(cd "$ROOT" && "$BUN" build --target=bun --packages=external --outdir "$TMP_OUT/$(basename "$hook")" \
${metaflag:+"$metaflag"} "$dir/$entry" 2>&1 >/dev/null); then
fail "$hook" "$(first_location "$dir/$entry" "$err")" "$(first_message "$err")"
hook_ok=0
fi
scan="$scan$NL$dir/$entry"
[ -f "$meta" ] || continue
for rel in $(cd "$ROOT" && "$BUN" -e 'for (const f of Object.keys(JSON.parse(await Bun.file(process.argv[1]).text()).inputs)) console.log(f)' "$meta" 2>/dev/null); do
case "$rel" in /*|../*) ;; *) scan="$scan$NL$ROOT/$rel" ;; esac
done
done
for file in $(printf '%s\n' "$scan" | awk '!seen[$0]++'); do
marker=$(grep -nE '^(<<<<<<<|=======|>>>>>>>)( |$)' "$file" 2>/dev/null | head -1 | cut -d: -f1)
[ -n "$marker" ] || continue
fail "$hook" "$file:$marker" "unresolved merge conflict marker"
hook_ok=0
done
[ "$hook_ok" -eq 1 ] && echo "ok $hook"
done