Merge oct8-setup: hook-check coverage, team hook cross-platform, gbrain MCP on Windows, verified Bun installs, Actions pins (triage wave)

This commit is contained in:
garrytan committed 2026-10-08 06:38:45 +00:00
commit 5a2dec3f1b
24 files changed
+646 -106

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
+75 -47
View File
@@ -124,24 +124,38 @@ if [ "$MODE" = "required" ]; then
HOOKS_DIR="$REPO_ROOT/.claude/hooks"
SETTINGS="$REPO_ROOT/.claude/settings.json"
# Create enforcement hook script
# Create the enforcement hook. Claude Code and Copilot CLI may run hook
# commands through sh, cmd.exe or PowerShell, so the hook is CommonJS run by
# node, and the registered command holds no shell variable syntax: it reads
# CLAUDE_PROJECT_DIR through process.env. `.cjs` keeps a project's
# package.json "type" from changing how node loads it.
mkdir -p "$HOOKS_DIR"
cat > "$HOOKS_DIR/check-gstack.sh" << 'HOOK_EOF'
#!/bin/bash
# Block skill usage when gstack is not installed globally.
#
# Resolve the install root the way gstack skill preambles do: the GSTACK_ROOT
# env var first, then every host's global install location, then the migrated
# repo location. Block only when NONE exist (#2500 — hardcoding
# ~/.claude/skills/gstack false-blocked Codex-host and migrated-repo installs).
_GSTACK_ROOT=""
for _D in "${GSTACK_ROOT:-}" "$HOME/.claude/skills/gstack" "$HOME/.codex/skills/gstack" "$HOME/.factory/skills/gstack" "$HOME/.kiro/skills/gstack" "$HOME/.config/opencode/skills/gstack" "$HOME/.slate/skills/gstack" "$HOME/.cursor/skills/gstack" "$HOME/.openclaw/skills/gstack" "$HOME/.hermes/skills/gstack" "$HOME/.gbrain/skills/gstack" "$HOME/.copilot/skills/gstack" "$HOME/.gstack/repos/gstack"; do
[ -z "$_GSTACK_ROOT" ] && [ -n "$_D" ] && [ -d "$_D/bin" ] && _GSTACK_ROOT="$_D"
done
cat > "$HOOKS_DIR/check-gstack.cjs" << 'HOOK_EOF'
'use strict';
// Block skill usage when gstack is not installed globally.
//
// Resolve the install root the way gstack skill preambles do: the GSTACK_ROOT
// env var first, then every host's global install location, then the migrated
// repo location. Block only when NONE exist (#2500 — hardcoding
// ~/.claude/skills/gstack false-blocked Codex-host and migrated-repo installs).
const fs = require('fs');
const os = require('os');
const path = require('path');
if [ -z "$_GSTACK_ROOT" ]; then
cat >&2 <<'MSG'
BLOCKED: gstack is not installed globally.
const home = process.env.HOME || process.env.USERPROFILE || os.homedir();
const roots = [process.env.GSTACK_ROOT || ''].concat([
'.claude/skills/gstack', '.codex/skills/gstack', '.factory/skills/gstack', '.kiro/skills/gstack',
'.config/opencode/skills/gstack', '.slate/skills/gstack', '.cursor/skills/gstack', '.openclaw/skills/gstack',
'.hermes/skills/gstack', '.gbrain/skills/gstack', '.copilot/skills/gstack', '.gstack/repos/gstack',
].map((rel) => path.join(home, rel)));
const gstackRoot = roots.find((root) => {
try { return root !== '' && fs.statSync(path.join(root, 'bin')).isDirectory(); } catch { return false; }
});
if (gstackRoot) {
process.stdout.write('{}\n');
} else {
process.stderr.write(`BLOCKED: gstack is not installed globally.
gstack is required for AI-assisted work in this repo.
@@ -150,24 +164,27 @@ Install it:
cd ~/.claude/skills/gstack && ./setup --team
Then restart your AI coding tool.
MSG
# Exit 2 denies on Claude Code (#2413) and on Copilot CLI, which treats a
# preToolUse exit 2 as deny (#2229).
echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"gstack is required but not installed. See stderr for install instructions."}}'
exit 2
fi
echo '{}'
`);
// Exit 2 denies on Claude Code (#2413) and on Copilot CLI, which treats a
// preToolUse exit 2 as deny (#2229).
process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny',
permissionDecisionReason: 'gstack is required but not installed. See stderr for install instructions.' } }) + '\n');
process.exitCode = 2;
}
HOOK_EOF
chmod +x "$HOOKS_DIR/check-gstack.sh"
GENERATED+=(".claude/hooks/check-gstack.sh")
echo " + .claude/hooks/check-gstack.sh — enforcement hook"
chmod +x "$HOOKS_DIR/check-gstack.cjs"
GENERATED+=(".claude/hooks/check-gstack.cjs")
echo " + .claude/hooks/check-gstack.cjs — enforcement hook"
# Add hook to project-level settings.json
if command -v bun >/dev/null 2>&1; then
GSTACK_SETTINGS_PATH="$SETTINGS" bun -e "
HOOK_COMMAND="node -e \"require(require('path').resolve(process.env.CLAUDE_PROJECT_DIR || '.', '.claude', 'hooks', 'check-gstack.cjs'))\""
if ! command -v bun >/dev/null 2>&1; then
echo " ! bun not found — manually add this PreToolUse hook (matcher Skill|skill) to .claude/settings.json:"
echo " $HOOK_COMMAND"
elif GSTACK_SETTINGS_PATH="$SETTINGS" GSTACK_HOOK_COMMAND="$HOOK_COMMAND" bun -e "
const fs = require('fs');
const settingsPath = process.env.GSTACK_SETTINGS_PATH;
const command = process.env.GSTACK_HOOK_COMMAND;
let settings = {};
try { settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8')); } catch {}
@@ -177,32 +194,43 @@ HOOK_EOF
// Claude Code's skill tool is Skill; Copilot CLI's is skill, which no
// Claude tool name maps to, so a bare 'Skill' never fired there (#2229).
// An entry an older team-init wrote is upgraded in place, never duplicated.
// A registration an older team-init wrote (the .sh hook, matcher Skill)
// is replaced in place: exactly one check-gstack handler remains, and
// unrelated hooks in the same entry are kept.
const MATCHER = 'Skill|skill';
const ours = settings.hooks.PreToolUse.filter(entry =>
(entry.matcher === 'Skill' || entry.matcher === MATCHER) &&
entry.hooks && entry.hooks.some(h => h.command && h.command.includes('check-gstack'))
);
for (const entry of ours) entry.matcher = MATCHER;
if (ours.length === 0) {
settings.hooks.PreToolUse.push({
matcher: MATCHER,
hooks: [{
type: 'command',
command: '\"\$CLAUDE_PROJECT_DIR/.claude/hooks/check-gstack.sh\"'
}]
});
}
const isOurs = h => h && typeof h.command === 'string' && h.command.includes('check-gstack');
let placed = false;
settings.hooks.PreToolUse = settings.hooks.PreToolUse.filter(entry => {
if (entry.matcher !== 'Skill' && entry.matcher !== MATCHER) return true;
if (!Array.isArray(entry.hooks) || !entry.hooks.some(isOurs)) return true;
entry.matcher = MATCHER;
const kept = [];
for (const h of entry.hooks) {
if (!isOurs(h)) kept.push(h);
else if (!placed) { kept.push({ type: 'command', command }); placed = true; }
}
entry.hooks = kept;
return kept.length > 0;
});
if (!placed) settings.hooks.PreToolUse.push({ matcher: MATCHER, hooks: [{ type: 'command', command }] });
const tmp = settingsPath + '.tmp';
fs.writeFileSync(tmp, JSON.stringify(settings, null, 2) + '\n');
fs.renameSync(tmp, settingsPath);
" 2>/dev/null
"; then
GENERATED+=(".claude/settings.json")
echo " + .claude/settings.json — PreToolUse hook registered"
# The bash hook an older team-init wrote goes only once its replacement is
# registered; a tracked one is staged as a deletion by the git add below.
if [ -e "$HOOKS_DIR/check-gstack.sh" ]; then
rm -f "$HOOKS_DIR/check-gstack.sh"
git -C "$REPO_ROOT" ls-files --error-unmatch -- .claude/hooks/check-gstack.sh >/dev/null 2>&1 \
&& GENERATED+=(".claude/hooks/check-gstack.sh")
echo " - .claude/hooks/check-gstack.sh — replaced by check-gstack.cjs"
fi
else
echo " ! bun not found — manually add the PreToolUse hook to .claude/settings.json"
echo " ! could not update .claude/settings.json — add this PreToolUse hook (matcher Skill|skill) manually:"
echo " $HOOK_COMMAND"
fi
fi