feat(kyberforge): SessionStart hook resolves the repo-pinned apm (#270) #274

Merged
Defame1297 merged 3 commits from feat/270-hook-resolves-pinned-apm into main 2026-12-02 08:14:05 +00:00
5 changed files with 74 additions and 31 deletions
Showing only changes of commit 45d740eef1 - Show all commits

No files matched your search

@@ -140,15 +140,15 @@ 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-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.
> **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.
**Reading a human-readable CLI for a control decision cost a silent failure, again.** `apm outdated`
has no `--json` or other machine-readable flag (confirmed against 0.28.0), so the hook must match
its prose. The first attempt matched `outdated dependencies found` — plural only. apm emits
@@ -2,7 +2,7 @@
**Status: accepted (2026-12-01).**
*Amended 2026-12-01 (holocron#270): the hook's test seam is a `PATH` stub of `uv`, not an `APM`
*Amended 2026-12-02 (holocron#270): the hook's test seam is a `PATH` stub of `uv`, not an `APM`
environment override. `tests/test-apm-current-hook.sh` puts a fake `uv` first on a `PATH` that
excludes the machine's real `uv` and `apm`. The fake records every invocation and reaches the pinned
fake apm only through `uv run`, so the shipped hook carries no test-only override. The `APM` variable
@@ -49,9 +49,13 @@ Issue #265. apm is installed once per machine with pipx, at 0.28.0, and every re
- **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:
1. `uv run --frozen`, when the project pins `apm-cli` and uv exists.
1. `uv run --frozen --no-sync --offline`, when the project pins `apm-cli` (a quoted `apm-cli`
requirement in `pyproject.toml` plus a `uv.lock`) and uv exists. If the project environment
(`$UV_PROJECT_ENVIRONMENT`, else `<root>/.venv`) does not exist, the hook prints a fixed "not
synced, run `uv sync --frozen`" notice and never calls uv, because even `--no-sync` creates an
empty `.venv`.
2. `apm` on `PATH`.
3. Neither: exit 0.
3. Neither: exit 0. So does a machine with no `timeout` or `gtimeout`, on every path.
It never triggers a uv sync, and on a cold cache it must not touch the network inside the
`SessionStart` timeout. It never reports "up to date" when it ran on an apm older than 0.39.2. Its
@@ -25,6 +25,20 @@ project_dir="${CLAUDE_PROJECT_DIR:-$PWD}"
# installed this plugin natively. Say nothing and cost nothing.
[[ -f "$project_dir/apm.lock.yaml" ]] || exit 0
# 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 on every path rather than risk holding the session
# open — or misreporting an apm whose version it could not read. It is chosen
# before the apm, so no notice is printed either. 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
# Choose the apm once, and use it for every call below (ADR-0034):
# 1. the repo's own pin, run as `uv run`, when the project pins apm-cli AND
# uv is on PATH;
@@ -53,6 +67,7 @@ project_dir="${CLAUDE_PROJECT_DIR:-$PWD}"
# 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() {
@@ -78,19 +93,6 @@ 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
@@ -124,7 +126,7 @@ 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"
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, or run: uv sync --frozen"
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
+5 -5
View File
@@ -105,14 +105,14 @@ 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.
exits 0 silently on every path, before choosing an apm, 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 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. "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
timed out) says to check `uv run apm --version` or to run `uv sync --frozen`. 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
@@ -131,9 +131,9 @@ 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`, 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
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.
**Staleness is detected by matching apm's summary line, and both spellings count.** `apm outdated`
+42 -5
View File
@@ -311,10 +311,16 @@ PIN_BIN="$(mktemp -d)"
PATH_APM_BIN="$(mktemp -d)"
PINNED="$(mktemp -d)"
trap 'rm -rf "$FAKE_BIN" "$WORK" "$TOOLS" "$UV_BIN" "$PIN_BIN" "$PATH_APM_BIN" "$PINNED" "${PROBE:-}"' EXIT
for tool in bash grep timeout git cat touch head; do
for tool in bash grep git cat touch head; do
src="$(command -v "$tool" || true)"
[[ -n "$src" ]] && ln -s "$src" "$TOOLS/$tool"
done
# A fake `timeout` that drops its duration and runs the command unbounded, so
# the suite does not need coreutils (stock macOS has no `timeout`). The real
# bound is the hook's business; these cases only need it to resolve one.
TIMEOUT_SHIM="$TOOLS/timeout"
printf '#!/usr/bin/env bash\nshift\nexec "$@"\n' > "$TIMEOUT_SHIM"
chmod +x "$TIMEOUT_SHIM"
# A fake apm at $1 that records, under $2, which phases ran. $3 is the
# `apm outdated` line, $4 the `apm update` exit code, $5 the version string.
@@ -526,7 +532,7 @@ run_pinned "uv apm" > /dev/null
rm -f "$PINNED/pyproject.toml"
mv "$PINNED/pyproject.toml.off" "$PINNED/pyproject.toml"
# --- no apm at all: silent, exit 0, with or without the pin ---
# --- no apm at all (the pin is present, uv and apm are not): silent, exit 0 ---
reset_calls
out="$(run_pinned "")"; rc=$?
[[ $rc -eq 0 && -z "$out" ]] && pass "no uv, no apm: silent, exit 0" \
@@ -604,6 +610,7 @@ for how in "uv run exits non-zero" "no pinned apm to exec"; do
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 "or run: uv sync --frozen" <<< "$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"
@@ -618,19 +625,49 @@ 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"
ln -s "$TIMEOUT_SHIM" "$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"
# Chosen before the apm, so the uv path stays silent too: a pin with uv and no
# project environment would otherwise print its not-synced notice.
rm -rf "$PINNED/.venv"
reset_calls
out="$(RUN_TOOLS="$NO_TO" run_pinned "uv apm")"; rc=$?
[[ $rc -eq 0 && -z "$out" && -z "$(calls_of uv)" ]] \
&& pass "no timeout or gtimeout, pin + uv without .venv: still silent, uv not called" \
|| fail "no timeout binary on the uv path: expected silence, got rc=$rc out=$out"
mkdir -p "$PINNED/.venv"
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"
# --- PATH apm whose `--version` exits non-zero: unreadable, not too old ---
make_fake_apm "$PATH_APM_BIN" path "$STALE" 0 "$V_NEW"
cat > "$PATH_APM_BIN/apm" << EOF
#!/usr/bin/env bash
echo "\$*" >> "$WORK/path-calls"
case "\$1" in
--version) echo "$V_NEW"; exit 1 ;;
outdated) cat "$WORK/path-outdated.txt"; exit 0 ;;
esac
exit 0
EOF
reset_calls
mv "$PINNED/pyproject.toml" "$PINNED/pyproject.toml.off"
out="$(run_pinned "apm")"; rc=$?
ctx="$(ctx_of "$out")"
[[ $rc -eq 0 ]] && grep -qF "Could not read the version of \`apm\` on PATH (it failed or timed out)" <<< "$ctx" \
&& ! grep -qF "older than" <<< "$ctx" && ! grep -q "^outdated" <<< "$(calls_of path)" \
&& pass "PATH apm --version exits 1: unreadable-version notice, no outdated check" \
|| fail "PATH apm --version exits 1: expected the unreadable notice, got rc=$rc calls=$(calls_of path | tr '\n' '|') out=$out"
mv "$PINNED/pyproject.toml.off" "$PINNED/pyproject.toml"
# --- 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,
@@ -722,8 +759,8 @@ echo "--- the greped phrase, against the real apm ---"
# Everything above mocks `apm`, so an apm release that reworded its summary line
# would revert this hook to its pre-fix behaviour with a fully green suite.
# `apm outdated` has no --json/machine-readable flag (verified against 0.28.0),
# so the phrase match cannot be replaced — it can only be pinned.
# `apm outdated` has no --json/machine-readable flag (verified against 0.28.0
# and 0.39.3), so the phrase match cannot be replaced — it can only be pinned.
#
# The probe stages a genuinely outdated dependency with no network: a local git
# repo stands in for the upstream, reached through `url.<path>.insteadOf`