mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-20 23:23:26 +01:00
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>
196 lines
7.8 KiB
Bash
Executable File
196 lines
7.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# gstack-hook-check — parse-check every hook Claude Code runs from a gstack tree.
|
|
#
|
|
# Usage:
|
|
# gstack-hook-check [--setup <file>] --list <root> print the checked hooks
|
|
# gstack-hook-check [--setup <file>] <root> check them
|
|
#
|
|
# 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. setup runs this before it registers hooks, and the auto-updater
|
|
# (bin/gstack-session-update) runs it on the incoming revision before the
|
|
# live checkout moves.
|
|
#
|
|
# 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 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),
|
|
# 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>
|
|
# missing <hook> (no shim: setup skips registering it)
|
|
# fail <hook> <file>:<line>: <message>
|
|
# Exit: 0 when nothing failed, 1 when a hook failed, 2 on usage, 3 when bun
|
|
# (needed for the TypeScript check) is not found. Set GSTACK_HOOK_CHECK_BUN to
|
|
# the bun to run (setup passes its own).
|
|
set -u
|
|
|
|
LIST_ONLY=0 SETUP_FILE=""
|
|
while [ $# -gt 1 ]; do
|
|
case "$1" in
|
|
--list) LIST_ONLY=1; shift ;;
|
|
--setup) SETUP_FILE="$2"; shift 2 ;;
|
|
*) break ;;
|
|
esac
|
|
done
|
|
ROOT="${1:-}"
|
|
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
|
|
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]++')
|
|
|
|
if [ "$LIST_ONLY" -eq 1 ]; then
|
|
printf '%s\n' "$HOOKS"
|
|
exit 0
|
|
fi
|
|
|
|
BUN="${GSTACK_HOOK_CHECK_BUN:-bun}"
|
|
if ! command -v "$BUN" >/dev/null 2>&1; then
|
|
echo "gstack-hook-check: bun not found, so TypeScript hook entries cannot be checked" >&2
|
|
exit 3
|
|
fi
|
|
TMP_OUT=$(mktemp -d "${TMPDIR:-/tmp}/gstack-hook-check.XXXXXX") || { echo "gstack-hook-check: cannot create a temp directory" >&2; exit 1; }
|
|
trap 'rm -rf "$TMP_OUT"' EXIT
|
|
|
|
# first_location <file> <tool output> — "<file>:<line>" from bun's "at f:l:c"
|
|
# or bash's "f: line N:" (falls back to <file>).
|
|
first_location() {
|
|
_loc=$(printf '%s\n' "$2" | sed -n 's/^[[:space:]]*at \(.*\):\([0-9][0-9]*\):[0-9][0-9]*$/\1:\2/p' | head -1)
|
|
[ -n "$_loc" ] || _loc=$(printf '%s\n' "$2" | sed -n 's/^\(.*\): line \([0-9][0-9]*\):.*/\1:\2/p' | head -1)
|
|
printf '%s\n' "${_loc:-$1}"
|
|
}
|
|
first_message() {
|
|
_msg=$(printf '%s\n' "$1" | sed -n 's/^error: //p' | head -1)
|
|
[ -n "$_msg" ] || _msg=$(printf '%s\n' "$1" | sed -n 's/^.*: line [0-9][0-9]*: //p' | head -1)
|
|
[ -n "$_msg" ] || _msg=$(printf '%s\n' "$1" | grep -v '^[[:space:]]*$' | head -1)
|
|
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
|
|
hook_ok=1
|
|
shebang=$(head -1 "$shim")
|
|
case "$shebang" in
|
|
'#!'*bash*) interp=bash ;;
|
|
'#!'*/sh|'#!'*/sh' '*|'#!'*' sh'|'#!'*' sh '*) interp=sh ;;
|
|
*) interp="" ;;
|
|
esac
|
|
if [ -z "$interp" ]; then
|
|
fail "$hook" "$shim:1" "unsupported shebang '$shebang' (expected bash or sh)"
|
|
continue
|
|
fi
|
|
if ! err=$("$interp" -n "$shim" 2>&1); then
|
|
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
|
|
fail "$hook" "$dir/$entry:1" "missing TypeScript entry (the hook runs it)"
|
|
hook_ok=0
|
|
continue
|
|
fi
|
|
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
|
|
exit "$FAILED"
|