fix(kyberforge): bound the SessionStart hook portably and sharpen its notices

Review round 1 of #270.

- Resolve `timeout`, else `gtimeout`, once after choosing apm, and call it
  through "$to" at all three sites. Stock macOS has neither `timeout` nor a
  GNU coreutils default, so the `--version` call failed there and every
  session printed a false too-old notice. With neither binary the hook exits
  0 silently. The budget test now parses `"$to" N` too.
- Check that the project environment ($UV_PROJECT_ENVIRONMENT, else
  <root>/.venv) exists before taking the uv path. If it is missing, print a
  fixed not-synced notice and never call uv, because `uv run --no-sync`
  would create an empty .venv in the consumer repo.
- Separate "version too old" from "version unreadable or timed out", with a
  fixed literal for each on each path, so a timeout no longer blames an
  unsynced environment.
- Raise the hooks.json timeout from 380 to 400 for the 375s script budget,
  refresh the outdated-timing comment, and note the budget in ADR-0019.
- Tests: no timeout binary, gtimeout only, missing and relocated project
  environment, a failing `uv run`, `--offline` required on every uv call,
  and the hostile-output notice compared to its exact literal.
- ADR-0034: until the #271 cutover, this repo prints the too-old notice and
  auto-refresh is paused.

Refs: #270

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
This commit is contained in:
Defame1297andClaude Opus 5.5 committed 2026-12-02 00:25:14 +00:00
1 parent 1fb8cc797b
commit 367cc1c259
6 files changed
+169 -40

No files matched your search

+91 -13
View File
@@ -342,6 +342,7 @@ cat > "$UV_BIN/uv" << EOF
#!/usr/bin/env bash
printf '%s\n' "\$*" >> "$WORK/uv-calls"
[[ "\${1:-}" == run ]] || exit 2
[[ -f "$WORK/uv-run-fails" ]] && exit 1
shift
while [[ \$# -gt 0 && "\$1" != apm ]]; do
case "\$1" in --project) shift 2 ;; *) shift ;; esac
@@ -366,6 +367,8 @@ package = false
dev = ["apm-cli==0.39.3"]
EOF
touch "$PINNED/uv.lock" "$PINNED/apm.lock.yaml"
# A synced project environment. Without one the hook must not call uv at all.
mkdir -p "$PINNED/.venv"
STALE="[!] 4 outdated dependencies found"
CURRENT="[*] All dependencies are up-to-date"
@@ -383,7 +386,7 @@ run_pinned() {
apm) p+="$PATH_APM_BIN:" ;;
esac
done
(cd "$PINNED" && env -u CLAUDE_PROJECT_DIR PATH="${p}$TOOLS" bash "$HOOK" 2>/dev/null)
(cd "$PINNED" && env -u CLAUDE_PROJECT_DIR -u UV_PROJECT_ENVIRONMENT PATH="${p}${RUN_TOOLS:-$TOOLS}" bash "$HOOK" 2>/dev/null)
}
ctx_of() { json_field additionalContext <<< "$1" 2>/dev/null || true; }
@@ -396,13 +399,13 @@ assert_uv_calls_run_only() {
n=$((n + 1))
[[ "$line" == "run "* ]] || bad=1
[[ " $line " == *" sync "* ]] && bad=1
[[ " $line " == *" --frozen "* && " $line " == *" --no-sync "* ]] || bad=1
[[ " $line " == *" --frozen "* && " $line " == *" --no-sync "* && " $line " == *" --offline "* ]] || bad=1
[[ " $line " == *" --project $PINNED "* ]] || bad=1
done < <(calls_of uv)
if [[ $n -eq 0 ]]; then
fail "$label: expected uv to be called at least once, it was not"
elif [[ $bad -eq 0 ]]; then
pass "$label: all $n uv call(s) are \`uv run --project <root> --frozen --no-sync\`, none is a sync"
pass "$label: all $n uv call(s) are \`uv run --project <root> --frozen --no-sync --offline\`, none is a sync"
else
fail "$label: uv was called with something other than a frozen no-sync \`run\`: $(tr '\n' '|' <<< "$(calls_of uv)")"
fi
@@ -464,7 +467,7 @@ out="$(run_pinned "uv")"; rc=$?
[[ $rc -eq 0 ]] && pass "uv path, apm 0.28.0: exits 0" || fail "uv path, apm 0.28.0: exit $rc"
grep -q "^outdated" <<< "$(calls_of pin)" && fail "uv path, apm 0.28.0: ran the outdated check on a too-old apm" \
|| pass "uv path, apm 0.28.0: runs no outdated check"
grep -qF "Could not confirm that \`uv run apm\` is apm 0.39.2 or later" <<< "$(ctx_of "$out")" \
grep -qF "\`uv run apm\` is older than apm 0.39.2" <<< "$(ctx_of "$out")" \
&& grep -qF "Run: uv sync --frozen" <<< "$(ctx_of "$out")" \
&& pass "uv path, apm 0.28.0: prints the fixed too-old line naming \`uv run apm\`" \
|| fail "uv path, apm 0.28.0: expected the uv too-old line, got: $out"
@@ -532,14 +535,18 @@ out="$(run_pinned "")"; rc=$?
# --- PATH apm below 0.39.2: fixed too-old line, no outdated check ---
for v in 0.28.0 0.39.1 "no version here"; do
make_fake_apm "$PATH_APM_BIN" path "$STALE" 0 "Agent Package Manager (APM) CLI version $v"
[[ "$v" == "no version here" ]] && make_fake_apm "$PATH_APM_BIN" path "$STALE" 0 "$v"
want="\`apm\` on PATH is older than apm 0.39.2"
if [[ "$v" == "no version here" ]]; then
make_fake_apm "$PATH_APM_BIN" path "$STALE" 0 "$v"
want="Could not read the version of \`apm\` on PATH"
fi
reset_calls
out="$(run_pinned "apm")"; rc=$?
ctx="$(ctx_of "$out")"
if [[ $rc -eq 0 ]] && ! grep -qE "^(outdated|update)" <<< "$(calls_of path)" \
&& grep -qF "Could not confirm that \`apm\` is apm 0.39.2 or later" <<< "$ctx" \
&& grep -qF "$want" <<< "$ctx" \
&& [[ "$(json_field reloadSkills <<< "$out" 2>/dev/null)" == "False" ]]; then
pass "PATH apm reporting '$v': fixed too-old line, no outdated check, no update"
pass "PATH apm reporting '$v': fixed notice ($want), no outdated check, no update"
else
fail "PATH apm reporting '$v': expected the too-old line and no outdated call; rc=$rc calls=$(calls_of path | tr '\n' '|') out=$out"
fi
@@ -557,6 +564,73 @@ for v in 0.39.2 0.40.0 1.0.0; do
|| fail "PATH apm $v: should be accepted as new enough"
done
# --- pin and uv, but no project environment: never call uv ---
# `uv run --no-sync` would create an empty .venv in the consumer's repo.
rm -rf "$PINNED/.venv"
make_fake_apm "$PIN_BIN" pin "$STALE" 0 "$V_NEW"
make_fake_apm "$PATH_APM_BIN" path "$STALE" 0 "$V_NEW"
reset_calls
out="$(run_pinned "uv apm")"; rc=$?
[[ $rc -eq 0 ]] && grep -qF "its environment has not been synced" <<< "$(ctx_of "$out")" \
&& grep -qF "Run: uv sync --frozen" <<< "$(ctx_of "$out")" \
&& pass "pin + uv, no .venv: prints the fixed not-synced notice" \
|| fail "pin + uv, no .venv: expected the not-synced notice, got rc=$rc: $out"
[[ -z "$(calls_of uv)" && -z "$(calls_of path)" ]] \
&& pass "pin + uv, no .venv: calls neither uv nor the PATH apm" \
|| fail "pin + uv, no .venv: uv saw '$(calls_of uv | tr '\n' '|')', PATH apm saw '$(calls_of path | tr '\n' '|')'"
[[ ! -e "$PINNED/.venv" ]] && pass "pin + uv, no .venv: creates no .venv" \
|| fail "pin + uv, no .venv: a .venv appeared in the project"
# UV_PROJECT_ENVIRONMENT relocates the environment; an existing one there counts.
ALT_ENV="$WORK/alt-env"
mkdir -p "$ALT_ENV"
reset_calls
out="$(cd "$PINNED" && env -u CLAUDE_PROJECT_DIR UV_PROJECT_ENVIRONMENT="$ALT_ENV" PATH="$UV_BIN:$TOOLS" bash "$HOOK" 2>/dev/null)"
grep -q "^outdated" <<< "$(calls_of pin)" \
&& pass "UV_PROJECT_ENVIRONMENT pointing at an existing env: takes the uv path" \
|| fail "UV_PROJECT_ENVIRONMENT should satisfy the environment check, got: $out"
reset_calls
out="$(cd "$PINNED" && env -u CLAUDE_PROJECT_DIR UV_PROJECT_ENVIRONMENT="$WORK/no-such-env" PATH="$UV_BIN:$TOOLS" bash "$HOOK" 2>/dev/null)"
[[ -z "$(calls_of uv)" ]] && grep -qF "has not been synced" <<< "$(ctx_of "$out")" \
&& pass "UV_PROJECT_ENVIRONMENT pointing at nothing: not-synced notice, no uv call" \
|| fail "UV_PROJECT_ENVIRONMENT pointing at nothing should not call uv, got: $out"
mkdir -p "$PINNED/.venv"
# --- pin and uv, but `uv run` itself fails ---
for how in "uv run exits non-zero" "no pinned apm to exec"; do
make_fake_apm "$PIN_BIN" pin "$STALE" 0 "$V_NEW"
if [[ "$how" == "uv run exits non-zero" ]]; then touch "$WORK/uv-run-fails"; else rm -f "$PIN_BIN/apm"; fi
reset_calls
out="$(run_pinned "uv")"; rc=$?
ctx="$(ctx_of "$out")"
[[ $rc -eq 0 ]] && grep -qF "Could not read the version of \`uv run apm\` (it failed or timed out)" <<< "$ctx" \
&& ! grep -qF "not synced" <<< "$ctx" \
&& pass "pin + uv, $how: exits 0 with the fixed unreadable-version notice, not the not-synced one" \
|| fail "pin + uv, $how: expected the unreadable-version notice, got rc=$rc: $out"
assert_uv_calls_run_only "pin + uv, $how"
rm -f "$WORK/uv-run-fails"
done
# --- no timeout binary: nothing can be bounded, so stay silent ---
NO_TO="$WORK/tools-no-timeout"
G_TO="$WORK/tools-gtimeout"
mkdir -p "$NO_TO" "$G_TO"
for tool in bash grep git cat touch head; do
[[ -e "$TOOLS/$tool" ]] && ln -s "$(readlink "$TOOLS/$tool")" "$NO_TO/$tool" && ln -s "$(readlink "$TOOLS/$tool")" "$G_TO/$tool"
done
ln -s "$(readlink "$TOOLS/timeout")" "$G_TO/gtimeout"
make_fake_apm "$PATH_APM_BIN" path "$STALE" 0 "$V_NEW"
reset_calls
out="$(RUN_TOOLS="$NO_TO" run_pinned "apm")"; rc=$?
[[ $rc -eq 0 && -z "$out" && -z "$(calls_of path)" ]] \
&& pass "no timeout or gtimeout on PATH: silent, exit 0, apm not called" \
|| fail "no timeout binary: expected silence and no apm call, got rc=$rc calls=$(calls_of path | tr '\n' '|') out=$out"
reset_calls
out="$(RUN_TOOLS="$G_TO" run_pinned "apm")"
grep -q "^update" <<< "$(calls_of path)" && [[ "$(json_field reloadSkills <<< "$out" 2>/dev/null)" == "True" ]] \
&& pass "only gtimeout on PATH (Homebrew coreutils on macOS): uses it and refreshes" \
|| fail "gtimeout should stand in for timeout, got calls=$(calls_of path | tr '\n' '|') out=$out"
# --- hostile command output never reaches the advice text ---
# Every advice string is a fixed literal; only a digit-checked count is
# spliced in. Output stuffed with ANSI escapes, command substitutions,
@@ -578,15 +652,18 @@ for route in uv apm; do
fail "hostile output via $route: rc=$rc, output not valid JSON: $(cat -v <<< "$out")"
fi
leaked=""
for needle in $'\e' '$(' 'pwned' 'rm -rf' ';' '\\' $'\x01' '}]}' 'id`'; do
for needle in $'\e' '$(' 'pwned' 'rm -rf' '\\' $'\x01' '}]}' 'id`'; do
grep -qF -- "$needle" <<< "$out" && leaked+="[$needle]"
done
[[ -z "$leaked" ]] && pass "hostile output via $route: none of it reaches the emitted text" \
|| fail "hostile output via $route: leaked $(cat -v <<< "$leaked") into: $(cat -v <<< "$out")"
[[ ! -e "$WORK/pwned" && ! -e "$WORK/pwned2" ]] && pass "hostile output via $route: nothing in it was executed" \
|| fail "hostile output via $route: a substitution in apm's output was executed"
grep -q "7 package" <<< "$ctx" && pass "hostile output via $route: the digit-checked count still comes through" \
|| fail "hostile output via $route: count lost: $ctx"
expected="apm install is 7 package(s) behind the remote default branch and the automatic refresh failed. Deployed skills and agents may be stale. Run: apm update --yes"
[[ "$route" == uv ]] && expected="${expected%apm update --yes}uv run apm update --yes"
[[ "$ctx" == "$expected" ]] \
&& pass "hostile output via $route: the notice is exactly the fixed literal plus the digit-checked count" \
|| fail "hostile output via $route: notice differs from the fixed literal: $(cat -v <<< "$ctx")"
[[ -f "$PINNED/pyproject.toml.off" ]] && mv "$PINNED/pyproject.toml.off" "$PINNED/pyproject.toml"
done
@@ -616,7 +693,8 @@ matcher="$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(d[
# literal, so raising either internal `timeout` without raising the host budget
# fails here instead of reintroducing the gap quietly.
#
# Every `timeout N` in the script counts, comments included: a stray "timeout
# Every `timeout N` or `"$to" N` in the script counts (the hook resolves
# `timeout` or `gtimeout` into $to), comments included: a stray "timeout
# 300" in prose only makes this stricter, which is the safe direction.
script_budget=0
timeout_count=0
@@ -624,12 +702,12 @@ while read -r n; do
[[ -n "$n" ]] || continue
script_budget=$((script_budget + n))
timeout_count=$((timeout_count + 1))
done < <(grep -oE '\btimeout [0-9]+\b' "$HOOK" | grep -oE '[0-9]+')
done < <(grep -oE '(\btimeout|"\$to") [0-9]+\b' "$HOOK" | grep -oE '[0-9]+$')
hook_timeout="$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(d["hooks"]["SessionStart"][0]["hooks"][0]["timeout"])' "$HOOKS_JSON")"
if [[ $timeout_count -eq 0 ]]; then
fail "found no 'timeout N' in $HOOK — the budget assertion below would be vacuous"
fail "found no 'timeout N' or '\"\$to\" N' in $HOOK — the budget assertion below would be vacuous"
else
pass "parsed $timeout_count internal timeout(s) totalling ${script_budget}s from the hook script"
[[ $hook_timeout -gt $script_budget ]] \