diff --git a/docs/adr/0019-session-start-hook-keeps-the-apm-install-current.md b/docs/adr/0019-session-start-hook-keeps-the-apm-install-current.md index 411ec74..c00b07f 100644 --- a/docs/adr/0019-session-start-hook-keeps-the-apm-install-current.md +++ b/docs/adr/0019-session-start-hook-keeps-the-apm-install-current.md @@ -140,6 +140,11 @@ was below the 360 s the script can legitimately take. A test asserts the invaria literal — it parses every `timeout N` out of the script, sums them, and requires the `hooks.json` value to be larger — so changing either side without the other fails the suite. +> **Amendment (2026-12-02, holocron#270) — the budget is now 375 s and the host timeout 400 s.** The +> hook also reads `apm --version` under a 15 s bound before the outdated check (ADR-0034), and calls +> `timeout` or `gtimeout` through a variable, which the budget test parses too. The current numbers +> and their breakdown live in `plugins/kyberforge/docs/hooks.md`. + > **Amendment (2026-09-16) — the refresh is slower than first measured, still inside the budget.** > Re-measured: ~24–26 s for the same six-behind refresh, warm, on a LAN remote — well inside the > 380 s above. diff --git a/docs/adr/0034-apm-is-pinned-per-repo-with-uv.md b/docs/adr/0034-apm-is-pinned-per-repo-with-uv.md index d62a4cb..349dbc1 100644 --- a/docs/adr/0034-apm-is-pinned-per-repo-with-uv.md +++ b/docs/adr/0034-apm-is-pinned-per-repo-with-uv.md @@ -43,6 +43,9 @@ Issue #265. apm is installed once per machine with pipx, at 0.28.0, and every re scripts and their tests) follows as one PR. The order matters because the deployed hook comes from the `main` commit that `apm.lock.yaml` records. If the v4 lock landed first, the old hook would run `PATH` apm 0.28.0, which refuses the lock, and staleness detection would be lost with no error. + The cost of this order: between the hook landing and the cutover, this repo has no pin, so the hook + runs `PATH` apm 0.28.0 and prints the fixed too-old notice at every session start. Auto-refresh + is paused here until the #271 cutover adds the pin. - **The cutover PR commits a lock rewrite (v1 to v4) on purpose.** It is the one sanctioned exception to the rule that `apm.lock.yaml` churn is discarded on feature branches (ADR-0019). - **Consumer hook contract.** The shipped hook (`check-apm-current.sh`) resolves apm in this order: diff --git a/plugins/kyberforge/.apm/hooks/check-apm-current.sh b/plugins/kyberforge/.apm/hooks/check-apm-current.sh index 2276065..7e06700 100755 --- a/plugins/kyberforge/.apm/hooks/check-apm-current.sh +++ b/plugins/kyberforge/.apm/hooks/check-apm-current.sh @@ -42,18 +42,33 @@ project_dir="${CLAUDE_PROJECT_DIR:-$PWD}" # uv is only ever invoked as `uv run`, and with `--no-sync`: a plain `uv run` # syncs the environment first, which installs packages and can reach the # network, and a SessionStart hook must do neither. `--offline` closes the -# remaining gap (uv fetching a Python). An environment that was never synced -# has no apm of its own, so `uv run` falls through to whatever apm is on PATH, -# and the version check below catches an old one and tells the reader to sync. +# remaining gap (uv fetching a Python). Even `--no-sync` creates an empty +# .venv when none exists, so a project environment that is missing altogether +# (`$UV_PROJECT_ENVIRONMENT`, else /.venv) gets a fixed "not synced" +# notice and uv is never called. One that exists but was never synced has no +# apm of its own, so `uv run` falls through to whatever apm is on PATH, and the +# version check below catches an old one and tells the reader to sync. # # apm_label names the chosen binary in advice text. It selects between fixed # literals and is never built from command output. apm_cmd is never seeded # empty: every branch below either sets it or exits, which keeps its expansions # safe under `set -u` on bash 3.2 (tests/test-vale-wrap.sh scans for this). +# Only ever emit fixed text plus a digit-checked count — never interpolate +# command output into the JSON, which would need escaping this cannot do safely. +emit() { + printf '{"hookSpecificOutput":{"hookEventName":"SessionStart","reloadSkills":%s,"additionalContext":"%s"}}\n' "$1" "$2" +} + apm_label="" if [[ -f "$project_dir/pyproject.toml" && -f "$project_dir/uv.lock" ]] \ && grep -qiE "[\"']apm[-_.]cli[^a-z0-9_.-]" "$project_dir/pyproject.toml" 2> /dev/null \ && command -v uv > /dev/null 2>&1; then + uv_env="${UV_PROJECT_ENVIRONMENT:-$project_dir/.venv}" + [[ "$uv_env" == /* ]] || uv_env="$project_dir/$uv_env" + if [[ ! -d "$uv_env" ]]; then + emit false "This project pins apm with uv but its environment has not been synced, so the apm install was not checked for staleness. Run: uv sync --frozen" + exit 0 + fi apm_cmd=(uv run --project "$project_dir" --frozen --no-sync --offline apm) apm_label="uv" elif command -v apm > /dev/null 2>&1; then @@ -63,26 +78,37 @@ else exit 0 fi +# Every apm call below is bounded, and stock macOS ships no `timeout`; Homebrew +# coreutils installs it as `gtimeout`. Without either, nothing can be bounded, +# so the hook stays silent rather than risk holding the session open — or +# misreporting an apm whose version it could not read. The budget test in +# tests/test-apm-current-hook.sh sums every `"$to" N` and `timeout N` here. +if command -v timeout > /dev/null 2>&1; then + to=timeout +elif command -v gtimeout > /dev/null 2>&1; then + to=gtimeout +else + exit 0 +fi + # Every apm call below must see the same directory the guard just checked — # `apm outdated` and `apm update` both resolve the lockfile from the cwd. cd "$project_dir" || exit 0 -# Only ever emit fixed text plus a digit-checked count — never interpolate -# command output into the JSON, which would need escaping this cannot do safely. -emit() { - printf '{"hookSpecificOutput":{"hookEventName":"SessionStart","reloadSkills":%s,"additionalContext":"%s"}}\n' "$1" "$2" -} - # apm older than 0.39.2 refuses a lockfile v4 and would then report it as up to # date, so its `outdated` verdict is worthless (ADR-0034). Below the minimum, or # when the version cannot be read at all, say so in a fixed line and skip the # check: a silent pass here is exactly the false "up to date" to avoid. Only the # first dotted triple is taken, and only its digits are compared. +# +# Returns 0 when new enough, 1 when the version was read and is too old, and 2 +# when it could not be read (the call failed or timed out, or printed no +# version), so the notice can say which. version_ok() { local version_output version - version_output="$(timeout 15 "${apm_cmd[@]}" --version 2> /dev/null)" || return 1 + version_output="$("$to" 15 "${apm_cmd[@]}" --version 2> /dev/null)" || return 2 version="$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+' <<< "$version_output" | head -n 1)" - [[ "$version" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] || return 1 + [[ "$version" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] || return 2 local -a have=("${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}") need=(0 39 2) local i for i in 0 1 2; do @@ -92,25 +118,32 @@ version_ok() { return 0 } -if ! version_ok; then - if [[ "$apm_label" == "uv" ]]; then - emit false "Could not confirm that \`uv run apm\` is apm 0.39.2 or later, so the apm install was not checked for staleness (older apm refuses lockfile v4 and would report it as up to date). The project environment is probably not synced. Run: uv sync --frozen" +version_ok +version_status=$? +if [[ $version_status -ne 0 ]]; then + if [[ "$apm_label" == "uv" && $version_status -eq 1 ]]; then + emit false "\`uv run apm\` is older than apm 0.39.2, so the apm install was not checked for staleness (older apm refuses lockfile v4 and would report it as up to date). The project environment is probably not synced to its pin. Run: uv sync --frozen" + elif [[ "$apm_label" == "uv" ]]; then + emit false "Could not read the version of \`uv run apm\` (it failed or timed out), so the apm install was not checked for staleness. Check that this works: uv run apm --version" + elif [[ $version_status -eq 1 ]]; then + emit false "\`apm\` on PATH is older than apm 0.39.2, so the apm install was not checked for staleness (older apm refuses lockfile v4 and would report it as up to date). Upgrade the apm on PATH to 0.39.2 or later, or pin apm-cli in this project with uv." else - emit false "Could not confirm that \`apm\` is apm 0.39.2 or later, so the apm install was not checked for staleness (older apm refuses lockfile v4 and would report it as up to date). Upgrade the apm on PATH to 0.39.2 or later, or pin apm-cli in this project with uv." + emit false "Could not read the version of \`apm\` on PATH (it failed or timed out), so the apm install was not checked for staleness. Check that this works: apm --version" fi exit 0 fi # `apm outdated` exits 0 whether or not anything is stale, so the answer has to -# come from its output. ~0.7s against six remote refs; a hung remote must not -# hold the session open. +# come from its output. Measured ~0.7s against six remote refs on apm 0.28.0 +# from PATH, and ~3.6s through `uv run` on 0.39.3; a hung remote must not hold +# the session open. # # There is no --json/machine-readable flag on `apm outdated` (verified against # apm 0.28.0 and 0.39.3), so the phrase match is forced rather than chosen. Note the # singular: apm prints "1 outdated dependency found" when exactly one package is # behind, so matching only "dependencies" would silently miss a one-package # drift. tests/test-apm-current-hook.sh pins both spellings against the real apm. -outdated_output="$(timeout 60 "${apm_cmd[@]}" outdated 2>&1)" || exit 0 +outdated_output="$("$to" 60 "${apm_cmd[@]}" outdated 2>&1)" || exit 0 grep -qE 'outdated dependenc(y|ies) found' <<< "$outdated_output" || exit 0 stale_count="$(grep -oE '[0-9]+ outdated dependenc(y|ies) found' <<< "$outdated_output" | grep -oE '^[0-9]+' || true)" @@ -149,7 +182,7 @@ fi manual_update="apm update --yes" [[ "$apm_label" == "uv" ]] && manual_update="uv run apm update --yes" -if timeout 300 "${apm_cmd[@]}" update --yes > /dev/null 2>&1; then +if "$to" 300 "${apm_cmd[@]}" update --yes > /dev/null 2>&1; then emit true "apm install was ${stale_count} package(s) behind the remote default branch and has been refreshed automatically; skills and agents were redeployed and re-scanned. apm.lock.yaml has been rewritten and is now a modified file in the working tree - ${lock_advice}" else emit false "apm install is ${stale_count} package(s) behind the remote default branch and the automatic refresh failed. Deployed skills and agents may be stale. Run: ${manual_update}" diff --git a/plugins/kyberforge/.apm/hooks/hooks.json b/plugins/kyberforge/.apm/hooks/hooks.json index 4d1c7c4..549360f 100644 --- a/plugins/kyberforge/.apm/hooks/hooks.json +++ b/plugins/kyberforge/.apm/hooks/hooks.json @@ -5,7 +5,7 @@ "hooks": [ { "command": "${CLAUDE_PLUGIN_ROOT}/.apm/hooks/check-apm-current.sh", - "timeout": 380, + "timeout": 400, "type": "command" } ], diff --git a/plugins/kyberforge/docs/hooks.md b/plugins/kyberforge/docs/hooks.md index 53d4fa6..39618ce 100644 --- a/plugins/kyberforge/docs/hooks.md +++ b/plugins/kyberforge/docs/hooks.md @@ -96,14 +96,24 @@ ADR-0019. 3. Otherwise nothing: it exits 0 silently. uv is only ever called as `uv run`, with `--no-sync`, so the hook never installs into the environment -and never reaches the network through uv. An environment that was never synced has no apm of its own. -`uv run` then falls through to the `PATH` apm, and the version check below catches an old one. +and never reaches the network through uv. Even `--no-sync` creates an empty `.venv` when none exists, +so before taking the uv path the hook checks that the project environment exists. That is +`$UV_PROJECT_ENVIRONMENT` when set, else `/.venv`. If it is missing, the hook prints a fixed +"not synced, run `uv sync --frozen`" notice and never calls uv. An environment that exists but was +never synced has no apm of its own. `uv run` then falls through to the `PATH` apm, and the version +check below catches an old one. + +**It needs `timeout` or `gtimeout`.** Every apm call is bounded. Stock macOS ships no `timeout`, and +Homebrew coreutils installs it as `gtimeout`, so the hook uses whichever exists. With neither, it +exits 0 silently rather than run an unbounded call. **apm older than 0.39.2 is not trusted.** Before the outdated check the hook reads `--version`. If the -chosen apm is older than 0.39.2, or its version cannot be read, the hook prints one fixed notice that +chosen apm is older than 0.39.2, or its version cannot be read, the hook prints a fixed notice that names the chosen binary and skips the check. The reason is that an older apm refuses a lockfile v4 and -would then report the install as up to date. The notice says `uv sync --frozen` on the uv path, and -says to upgrade or pin apm on the `PATH` path. +would then report the install as up to date. "Too old" and "could not read the version" are separate +notices. On the uv path, too old says to run `uv sync --frozen`, and unreadable (the call failed or +timed out) says to check `uv run apm --version`. On the `PATH` path, too old says to upgrade or pin +apm, and unreadable says to check `apm --version`. **Advice text is fixed.** Every notice is a literal string, and the only variable part is a digit-checked package count. The commands it names follow the chosen binary: `uv run apm install` or @@ -120,8 +130,8 @@ Keep the cwd fallback: a host that sets no `CLAUDE_PROJECT_DIR` must still get i behaviour, not an unset-variable error. **The `timeout` in `hooks.json` must exceed the script's own budget.** The script spends at most -15s on `apm --version`, 60s on `apm outdated` and 300s on `apm update`. The hook entry declares -`timeout: 380`, the sum plus a buffer. Set it lower and a slow remote gets the hook SIGKILLed mid-`apm update`, leaving a +15s on `apm --version`, 60s on `apm outdated` and 300s on `apm update`, 375s in all. The hook entry +declares `timeout: 400`, the sum plus a buffer. Set it lower and a slow remote gets the hook SIGKILLed mid-`apm update`, leaving a partially redeployed `.claude/skills/` and emitting no notice — precisely the silent failure the hook exists to prevent. `tests/test-apm-current-hook.sh` pins the relationship (host timeout > sum of the script's internal timeouts) rather than the literal, so raising either side alone fails the suite. diff --git a/tests/test-apm-current-hook.sh b/tests/test-apm-current-hook.sh index 94e035a..5cd3335 100755 --- a/tests/test-apm-current-hook.sh +++ b/tests/test-apm-current-hook.sh @@ -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 --frozen --no-sync\`, none is a sync" + pass "$label: all $n uv call(s) are \`uv run --project --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 ]] \