fix(hooks): detect_plan no longer calls the claude CLI (fork bomb)
`claude config get planType` started a full session whose SessionStart hook called detect_plan again: unbounded recursion, OOM, reboot (BLK-043). detect_plan now reads ~/.claude.json only and defaults to pro; the hook gets an env recursion guard.
This commit is contained in:
1 parent
4093cca4f4
commit
49aa2fff78
3 files changed
+84
-12
No files matched your search
@@ -5,6 +5,11 @@
|
|||||||
# git fetch for the version/update check near the end.
|
# git fetch for the version/update check near the end.
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
# Recursion guard (BLK-043): defense-in-depth, no live caller. Relies on
|
||||||
|
# Claude Code passing the env to hooks.
|
||||||
|
[ -n "${SESSION_START_RUNNING:-}" ] && exit 0
|
||||||
|
export SESSION_START_RUNNING=1
|
||||||
|
|
||||||
# ── Quick health check (filesystem only, no subprocesses) ──
|
# ── Quick health check (filesystem only, no subprocesses) ──
|
||||||
BROKEN=()
|
BROKEN=()
|
||||||
for f in CLAUDE.md settings.json agents skills; do
|
for f in CLAUDE.md settings.json agents skills; do
|
||||||
@@ -118,7 +123,6 @@ PLAN=$(detect_plan 2>/dev/null || echo "pro")
|
|||||||
case "$PLAN" in
|
case "$PLAN" in
|
||||||
max) _budget=20000; PLAN_LABEL="Max" ;;
|
max) _budget=20000; PLAN_LABEL="Max" ;;
|
||||||
pro) _budget=11000; PLAN_LABEL="Pro" ;;
|
pro) _budget=11000; PLAN_LABEL="Pro" ;;
|
||||||
free) _budget=5000; PLAN_LABEL="Free" ;;
|
|
||||||
*) _budget=11000; PLAN_LABEL="Pro" ;;
|
*) _budget=11000; PLAN_LABEL="Pro" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
|||||||
+5
-11
@@ -18,7 +18,7 @@ detect_rtk() {
|
|||||||
detect_superpowers() {
|
detect_superpowers() {
|
||||||
# superpowers = 7 vendored skills since 2026-09-28; the plugin is gone.
|
# superpowers = 7 vendored skills since 2026-09-28; the plugin is gone.
|
||||||
# One file test on the linked vendored skill: proves vendored AND
|
# One file test on the linked vendored skill: proves vendored AND
|
||||||
# linked in one shot — no plugin cache glob, no `claude plugin list`.
|
# linked in one shot — no plugin cache glob, no `plugin list` CLI call.
|
||||||
[ -f "$HOME/.claude/skills/brainstorming/SKILL.md" ]
|
[ -f "$HOME/.claude/skills/brainstorming/SKILL.md" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +63,9 @@ detect_graphifyy() {
|
|||||||
# --- Plan detection ---
|
# --- Plan detection ---
|
||||||
|
|
||||||
detect_plan() {
|
detect_plan() {
|
||||||
# Detect Claude plan: max, pro, or free.
|
# Detect Claude plan: max or pro.
|
||||||
|
# Never call the `claude` CLI here: it starts a session whose
|
||||||
|
# SessionStart hook calls detect_plan again (BLK-043).
|
||||||
# Checks ~/.claude.json for model access hints.
|
# Checks ~/.claude.json for model access hints.
|
||||||
# Returns plan name on stdout, always exits 0.
|
# Returns plan name on stdout, always exits 0.
|
||||||
local claude_json="$HOME/.claude.json"
|
local claude_json="$HOME/.claude.json"
|
||||||
@@ -78,13 +80,5 @@ detect_plan() {
|
|||||||
echo "max"; return 0
|
echo "max"; return 0
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
# Fallback: check if claude CLI reports plan
|
echo "pro"
|
||||||
local plan
|
|
||||||
plan=$(claude config get planType 2>/dev/null || true)
|
|
||||||
case "$plan" in
|
|
||||||
max|Max|MAX) echo "max" ;;
|
|
||||||
pro|Pro|PRO) echo "pro" ;;
|
|
||||||
free|Free|FREE) echo "free" ;;
|
|
||||||
*) echo "pro" ;; # default assumption
|
|
||||||
esac
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# lib/tests/detect-plan.test.sh
|
||||||
|
# Regression for BLK-043: detect_plan must never call the claude CLI
|
||||||
|
# (a hook-launched claude re-enters session-start.sh: fork bomb).
|
||||||
|
set -u
|
||||||
|
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||||
|
LIB="${DETECT_PLUGINS_LIB:-$ROOT/lib/detect-plugins.sh}"
|
||||||
|
HOOK="$ROOT/hooks/session-start.sh"
|
||||||
|
WORK=$(mktemp -d)
|
||||||
|
trap 'rm -rf "$WORK"' EXIT
|
||||||
|
pass=0; fail=0
|
||||||
|
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||||
|
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
|
||||||
|
calls() { wc -l <"$CLAUDE_LOG" | tr -d ' '; }
|
||||||
|
|
||||||
|
# Stub claude: logs every call, never starts a session.
|
||||||
|
CLAUDE_LOG="$WORK/claude.log"; : >"$CLAUDE_LOG"
|
||||||
|
mkdir -p "$WORK/stub"
|
||||||
|
printf '#!/bin/sh\necho "$*" >>"%s"\n' "$CLAUDE_LOG" >"$WORK/stub/claude"
|
||||||
|
chmod +x "$WORK/stub/claude"
|
||||||
|
export PATH="$WORK/stub:$PATH"
|
||||||
|
|
||||||
|
# Positive control: the stub is reached through PATH.
|
||||||
|
claude probe
|
||||||
|
check T0-stub-reached "$(calls)" 1
|
||||||
|
: >"$CLAUDE_LOG"
|
||||||
|
|
||||||
|
# detect_plan, no .claude.json: pro, no claude call.
|
||||||
|
mkdir -p "$WORK/empty"
|
||||||
|
out=$(HOME="$WORK/empty" bash -c "source '$LIB'; detect_plan")
|
||||||
|
check T1-no-json-plan "$out" pro
|
||||||
|
check T1-no-json-calls "$(calls)" 0
|
||||||
|
|
||||||
|
# detect_plan, max markers: max, no claude call.
|
||||||
|
mkdir -p "$WORK/maxhome"
|
||||||
|
printf '{"tengu_cobalt_compass": true, "tengu_harbor": true}\n' \
|
||||||
|
>"$WORK/maxhome/.claude.json"
|
||||||
|
: >"$CLAUDE_LOG"
|
||||||
|
out=$(HOME="$WORK/maxhome" bash -c "source '$LIB'; detect_plan")
|
||||||
|
check T2-max-plan "$out" max
|
||||||
|
check T2-max-calls "$(calls)" 0
|
||||||
|
|
||||||
|
# Hook HOME: real ~/.claude entries + max fixture (no fallback needed).
|
||||||
|
mkdir -p "$WORK/hookhome/.claude" "$WORK/proj"
|
||||||
|
for e in "$HOME"/.claude/*; do
|
||||||
|
ln -s "$e" "$WORK/hookhome/.claude/$(basename "$e")"
|
||||||
|
done
|
||||||
|
cp "$WORK/maxhome/.claude.json" "$WORK/hookhome/.claude.json"
|
||||||
|
|
||||||
|
# Guard: hook exits 0 silently when already running.
|
||||||
|
out=$(cd "$WORK/proj" && HOME="$WORK/hookhome" SESSION_START_RUNNING=1 \
|
||||||
|
SESSION_START_OFFLINE=1 bash "$HOOK" </dev/null 2>&1); rc=$?
|
||||||
|
check T3-guard-rc "$rc" 0
|
||||||
|
check T3-guard-silent "$out" ""
|
||||||
|
|
||||||
|
# Export: a child of the hook sees SESSION_START_RUNNING=1.
|
||||||
|
: >"$CLAUDE_LOG"
|
||||||
|
real_git=$(command -v git)
|
||||||
|
GIT_LOG="$WORK/git.log"; : >"$GIT_LOG"
|
||||||
|
{
|
||||||
|
printf '#!/bin/sh\nGIT_LOG="%s"\nREAL_GIT="%s"\n' "$GIT_LOG" "$real_git"
|
||||||
|
cat <<'EOF'
|
||||||
|
echo "${SESSION_START_RUNNING:-unset}" >>"$GIT_LOG"
|
||||||
|
exec "$REAL_GIT" "$@"
|
||||||
|
EOF
|
||||||
|
} >"$WORK/stub/git"
|
||||||
|
chmod +x "$WORK/stub/git"
|
||||||
|
( cd "$WORK/proj" && env -u SESSION_START_RUNNING HOME="$WORK/hookhome" \
|
||||||
|
SESSION_START_OFFLINE=1 bash "$HOOK" </dev/null >/dev/null 2>&1 )
|
||||||
|
check T4-child-sees-guard "$(head -1 "$GIT_LOG")" 1
|
||||||
|
check T4-hook-no-claude-calls "$(calls)" 0
|
||||||
|
|
||||||
|
printf 'detect-plan: %d passed, %d failed\n' "$pass" "$fail"
|
||||||
|
[ "$fail" -eq 0 ]
|
||||||
Reference in new issue
Block a user