feat(kyberforge): resolve the repo-pinned apm in the SessionStart hook

The hook now picks one apm and uses it for both the outdated check and the
update (ADR-0034): `uv run --project <root> --frozen --no-sync --offline apm`
when the project pins apm-cli (a quoted apm-cli requirement in pyproject.toml
plus a uv.lock) and uv is on PATH, else the PATH apm, else it exits silently.
uv is only ever called as `uv run`; `--no-sync` keeps a plain run from syncing
the environment, and `--offline` keeps uv off the network.

Before the outdated check it reads `--version`. Below 0.39.2, or when the
version cannot be read, it prints a fixed notice naming the chosen binary and
skips the check, since an older apm refuses a lockfile v4 and would report it
as up to date. All advice strings are fixed literals that name the chosen
binary; no command output is interpolated.

Tests use seam A: a fake uv first on a whitelisted PATH logs every call and
reaches the pinned fake apm only through `uv run`. They cover pin plus uv, pin
without uv, pin without uv.lock, no pin, no apm, too-old and boundary
versions, stale and current output on both paths, and hostile output.
ADR-0034 is amended to record that seam.

Impact: kyberforge 2.2.1 -> 2.3.0, catalog 0.9.0 -> 0.9.1.

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:05:03 +00:00
1 parent 0ad15c80bc
commit 1fb8cc797b
7 files changed
+436 -33

No files matched your search

+305 -5
View File
@@ -2,9 +2,11 @@
# Tests for plugins/kyberforge/.apm/hooks/check-apm-current.sh — the SessionStart
# hook that keeps an apm-consumed install level with its remote.
#
# `apm` is mocked throughout: the hook's contract is "read `apm outdated`, decide,
# emit SessionStart JSON", and that is testable without a network or a real
# install.
# `apm` and `uv` are mocked throughout (seam A: fake binaries first on PATH,
# asserting on exit code and stdout, plus the fakes' own call logs): the hook's
# contract is "pick the apm, read `apm outdated`, decide, emit SessionStart
# JSON", and that is testable without a network or a real install. The real
# apm appears once, at the end, to pin the phrase the hook greps for.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
@@ -31,12 +33,15 @@ GIT_CEILING_DIRECTORIES="$(dirname "$WORK")"
# `apm update`. Sentinel files record whether update was actually invoked and
# which directory the calls ran in — the hook must run them against the same
# directory its lockfile guard checked, not the session's cwd.
# `apm --version` answers 0.39.3 unless $3 says otherwise: the hook skips the
# outdated check on anything older than 0.39.2 (ADR-0034).
make_apm() {
local outdated_line="$1" update_exit="$2"
local outdated_line="$1" update_exit="$2" version="${3:-0.39.3}"
cat > "$FAKE_BIN/apm" << EOF
#!/usr/bin/env bash
pwd > "$WORK/apm-cwd"
case "\$1" in
--version) echo "Agent Package Manager (APM) CLI version $version"; exit 0 ;;
outdated) echo "$outdated_line"; exit 0 ;;
update) touch "$WORK/update-was-called"; exit $update_exit ;;
esac
@@ -291,6 +296,300 @@ out="$(run_hook_in "$ELSEWHERE" "$ELSEWHERE")"; rc=$?
&& pass "does not run apm update when there is no lockfile anywhere" \
|| fail "must not touch a project that does not use apm"
# ---------------------------------------------------------------------------
echo ""
echo "--- which apm: repo-pinned uv, else PATH, else nothing (ADR-0034) ---"
# ---------------------------------------------------------------------------
# Seam A: every binary the hook may call is a fake first on a PATH that holds
# nothing else of interest. The machine's real uv and apm must not leak into
# these cases — a real uv would make "pin but no uv" untestable — so PATH is
# built from a whitelist of the system tools the hook and the fakes need.
TOOLS="$(mktemp -d)"
UV_BIN="$(mktemp -d)"
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
src="$(command -v "$tool" || true)"
[[ -n "$src" ]] && ln -s "$src" "$TOOLS/$tool"
done
# 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.
make_fake_apm() {
local bin_dir="$1" tag="$2" outdated_line="$3" update_exit="$4" version="$5"
cat > "$bin_dir/apm" << EOF
#!/usr/bin/env bash
echo "\$*" >> "$WORK/$tag-calls"
case "\$1" in
--version) printf '%s\n' "$version"; exit 0 ;;
outdated) cat "$WORK/$tag-outdated.txt"; exit 0 ;;
update) exit $update_exit ;;
esac
exit 0
EOF
printf '%s\n' "$outdated_line" > "$WORK/$tag-outdated.txt"
chmod +x "$bin_dir/apm"
}
# The fake uv logs every invocation verbatim and only knows `run`: anything
# else exits 2. For `run` it skips uv's own options up to the `apm` argument
# and execs the pinned fake apm, which is NOT on PATH — so the pinned apm is
# reachable only through `uv run`.
cat > "$UV_BIN/uv" << EOF
#!/usr/bin/env bash
printf '%s\n' "\$*" >> "$WORK/uv-calls"
[[ "\${1:-}" == run ]] || exit 2
shift
while [[ \$# -gt 0 && "\$1" != apm ]]; do
case "\$1" in --project) shift 2 ;; *) shift ;; esac
done
[[ "\${1:-}" == apm ]] || exit 2
shift
exec "$PIN_BIN/apm" "\$@"
EOF
chmod +x "$UV_BIN/uv"
# The pin: a pyproject.toml naming apm-cli plus a uv.lock, as ADR-0034 lays out.
cat > "$PINNED/pyproject.toml" << 'EOF'
[project]
name = "consumer"
version = "0"
requires-python = ">=3.11"
[tool.uv]
package = false
[dependency-groups]
dev = ["apm-cli==0.39.3"]
EOF
touch "$PINNED/uv.lock" "$PINNED/apm.lock.yaml"
STALE="[!] 4 outdated dependencies found"
CURRENT="[*] All dependencies are up-to-date"
V_NEW="Agent Package Manager (APM) CLI version 0.39.3"
reset_calls() { rm -f "$WORK"/uv-calls "$WORK"/pin-calls "$WORK"/path-calls; }
calls_of() { cat "$WORK/$1-calls" 2>/dev/null || true; }
# $1: space-separated PATH components drawn from uv, apm. Always ends in TOOLS.
run_pinned() {
local p="" part
for part in $1; do
case "$part" in
uv) p+="$UV_BIN:" ;;
apm) p+="$PATH_APM_BIN:" ;;
esac
done
(cd "$PINNED" && env -u CLAUDE_PROJECT_DIR PATH="${p}$TOOLS" bash "$HOOK" 2>/dev/null)
}
ctx_of() { json_field additionalContext <<< "$1" 2>/dev/null || true; }
# Every recorded uv call must be `uv run` against the project, frozen, and with
# syncing off: a SessionStart hook must never install into the environment.
assert_uv_calls_run_only() {
local label="$1" n=0 bad=0 line
while IFS= read -r line; do
[[ -n "$line" ]] || continue
n=$((n + 1))
[[ "$line" == "run "* ]] || bad=1
[[ " $line " == *" sync "* ]] && bad=1
[[ " $line " == *" --frozen "* && " $line " == *" --no-sync "* ]] || 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"
else
fail "$label: uv was called with something other than a frozen no-sync \`run\`: $(tr '\n' '|' <<< "$(calls_of uv)")"
fi
}
# --- pin plus uv: uses `uv run`, for both the check and the update ---
make_fake_apm "$PIN_BIN" pin "$STALE" 1 "$V_NEW"
make_fake_apm "$PATH_APM_BIN" path "$STALE" 1 "$V_NEW"
reset_calls
out="$(run_pinned "uv apm")"; rc=$?
[[ $rc -eq 0 ]] && pass "pin + uv: exits 0" || fail "pin + uv: exit $rc"
grep -q "^outdated" <<< "$(calls_of pin)" && grep -q "^update" <<< "$(calls_of pin)" \
&& pass "pin + uv: runs the outdated check and the update through \`uv run\`" \
|| fail "pin + uv: the pinned apm should see outdated and update, saw: $(calls_of pin | tr '\n' '|')"
[[ -z "$(calls_of path)" ]] && pass "pin + uv: never touches the PATH apm" \
|| fail "pin + uv: PATH apm was called: $(calls_of path | tr '\n' '|')"
grep -qF "Run: uv run apm update --yes" <<< "$(ctx_of "$out")" \
&& pass "pin + uv: the failed-refresh advice names \`uv run apm\`" \
|| fail "pin + uv: advice should name uv run apm: $(ctx_of "$out")"
assert_uv_calls_run_only "pin + uv, stale"
# --- stale and current on the uv path ---
make_fake_apm "$PIN_BIN" pin "$STALE" 0 "$V_NEW"
reset_calls
out="$(run_pinned "uv")"
[[ "$(json_field reloadSkills <<< "$out" 2>/dev/null)" == "True" ]] \
&& grep -q "4 package" <<< "$(ctx_of "$out")" \
&& pass "uv path, stale: refreshes and reports the count" \
|| fail "uv path, stale: expected a successful-refresh notice, got: $out"
assert_uv_calls_run_only "uv path, stale"
make_fake_apm "$PIN_BIN" pin "$CURRENT" 0 "$V_NEW"
reset_calls
out="$(run_pinned "uv")"; rc=$?
[[ $rc -eq 0 && -z "$out" ]] && pass "uv path, current: silent, exit 0" \
|| fail "uv path, current: expected silence, got rc=$rc: $out"
grep -q "^update" <<< "$(calls_of pin)" && fail "uv path, current: must not update" \
|| pass "uv path, current: does not update"
assert_uv_calls_run_only "uv path, current"
# A feature branch's discard advice names the binary that will reinstall.
git -C "$PINNED" init -q -b main
git -C "$PINNED" -c user.email=[email protected] -c user.name=probe commit -q --allow-empty -m init
git -C "$PINNED" update-ref refs/remotes/origin/main HEAD
git -C "$PINNED" symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
git -C "$PINNED" checkout -q -b feature/y
make_fake_apm "$PIN_BIN" pin "$STALE" 0 "$V_NEW"
reset_calls
out="$(run_pinned "uv")"
grep -qF "git checkout -- apm.lock.yaml && uv run apm install" <<< "$(ctx_of "$out")" \
&& pass "uv path, feature branch: the discard advice names \`uv run apm install\`" \
|| fail "uv path, feature branch: advice should name uv run apm install: $(ctx_of "$out")"
rm -rf "$PINNED/.git"
# --- pinned apm too old (an unsynced env falls through to an old apm) ---
make_fake_apm "$PIN_BIN" pin "$CURRENT" 0 "Agent Package Manager (APM) CLI version 0.28.0"
reset_calls
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 "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"
assert_uv_calls_run_only "uv path, too old"
# --- pin but no uv: falls back to the PATH apm ---
make_fake_apm "$PIN_BIN" pin "$STALE" 0 "$V_NEW"
make_fake_apm "$PATH_APM_BIN" path "$STALE" 1 "$V_NEW"
reset_calls
out="$(run_pinned "apm")"; rc=$?
[[ $rc -eq 0 ]] && pass "pin, no uv: exits 0" || fail "pin, no uv: exit $rc"
grep -q "^outdated" <<< "$(calls_of path)" && grep -q "^update" <<< "$(calls_of path)" \
&& pass "pin, no uv: falls back to the PATH apm for the check and the update" \
|| fail "pin, no uv: PATH apm should run, saw: $(calls_of path | tr '\n' '|')"
[[ -z "$(calls_of pin)" ]] && pass "pin, no uv: the pinned apm is not reached" \
|| fail "pin, no uv: pinned apm ran without uv"
ctx="$(ctx_of "$out")"
grep -qF "Run: apm update --yes" <<< "$ctx" && ! grep -qF "uv run" <<< "$ctx" \
&& pass "pin, no uv: advice names the bare \`apm\`, not \`uv run apm\`" \
|| fail "pin, no uv: advice should name apm only: $ctx"
# --- pin declared but no uv.lock: `--frozen` has nothing to run from ---
rm -f "$PINNED/uv.lock"
reset_calls
out="$(run_pinned "uv apm")"
[[ -z "$(calls_of uv)" ]] && grep -q "^outdated" <<< "$(calls_of path)" \
&& pass "pin without uv.lock: does not call uv, uses the PATH apm" \
|| fail "pin without uv.lock: expected PATH apm only, uv saw: $(calls_of uv | tr '\n' '|')"
touch "$PINNED/uv.lock"
# --- no pin, with a PATH apm: uv on PATH is ignored ---
mv "$PINNED/pyproject.toml" "$PINNED/pyproject.toml.off"
make_fake_apm "$PATH_APM_BIN" path "$STALE" 0 "$V_NEW"
reset_calls
out="$(run_pinned "uv apm")"
[[ -z "$(calls_of uv)" ]] && pass "no pin: never calls uv even when it is on PATH" \
|| fail "no pin: uv was called: $(calls_of uv | tr '\n' '|')"
grep -q "^update" <<< "$(calls_of path)" && [[ "$(json_field reloadSkills <<< "$out" 2>/dev/null)" == "True" ]] \
&& pass "no pin, PATH path, stale: refreshes through the PATH apm" \
|| fail "no pin, PATH path, stale: expected a refresh via PATH apm, got: $out"
make_fake_apm "$PATH_APM_BIN" path "$CURRENT" 0 "$V_NEW"
reset_calls
out="$(run_pinned "uv apm")"; rc=$?
[[ $rc -eq 0 && -z "$out" ]] && ! grep -q "^update" <<< "$(calls_of path)" \
&& pass "no pin, PATH path, current: silent, no update" \
|| fail "no pin, PATH path, current: expected silence, got rc=$rc: $out"
# A pyproject.toml that does not mention apm-cli is not a pin.
printf '[project]\nname = "other"\ndependencies = ["apm-client-lib>=1"]\n' > "$PINNED/pyproject.toml"
make_fake_apm "$PATH_APM_BIN" path "$CURRENT" 0 "$V_NEW"
reset_calls
run_pinned "uv apm" > /dev/null
[[ -z "$(calls_of uv)" ]] && pass "a pyproject.toml without apm-cli is not read as a pin" \
|| fail "apm-client-lib was mistaken for an apm-cli pin"
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 ---
reset_calls
out="$(run_pinned "")"; rc=$?
[[ $rc -eq 0 && -z "$out" ]] && pass "no uv, no apm: silent, exit 0" \
|| fail "no uv, no apm: expected silence, got rc=$rc: $out"
# --- 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"
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" \
&& [[ "$(json_field reloadSkills <<< "$out" 2>/dev/null)" == "False" ]]; then
pass "PATH apm reporting '$v': fixed too-old line, 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
done
grep -qF "uv run" <<< "$ctx" && fail "PATH too-old line must not name uv run: $ctx" \
|| pass "PATH too-old line names the bare apm only"
# The boundary is inclusive: 0.39.2 is new enough, and so is a later minor.
for v in 0.39.2 0.40.0 1.0.0; do
make_fake_apm "$PATH_APM_BIN" path "$STALE" 0 "Agent Package Manager (APM) CLI version $v"
reset_calls
run_pinned "apm" > /dev/null
grep -q "^outdated" <<< "$(calls_of path)" \
&& pass "PATH apm $v: new enough, runs the outdated check" \
|| fail "PATH apm $v: should be accepted as new enough"
done
# --- 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,
# backticks, separators and quotes must leave no trace in what is emitted.
HOSTILE=$'\e[31m[!] 7 outdated dependencies found\e[0m $(touch '"$WORK"'/pwned) `touch '"$WORK"'/pwned2`; rm -rf / ; "}]}\\ \x01'
for route in uv apm; do
if [[ "$route" == uv ]]; then
make_fake_apm "$PIN_BIN" pin "$HOSTILE" 1 "$V_NEW"$'\n\e[0m$(id) `id`;'
else
mv "$PINNED/pyproject.toml" "$PINNED/pyproject.toml.off"
make_fake_apm "$PATH_APM_BIN" path "$HOSTILE" 1 "$V_NEW"$'\n\e[0m$(id) `id`;'
fi
reset_calls
out="$(run_pinned "$route")"; rc=$?
ctx="$(ctx_of "$out")"
if [[ $rc -eq 0 ]] && echo "$out" | python3 -m json.tool > /dev/null 2>&1 && [[ -n "$ctx" ]]; then
pass "hostile output via $route: still exits 0 with valid JSON"
else
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
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"
[[ -f "$PINNED/pyproject.toml.off" ]] && mv "$PINNED/pyproject.toml.off" "$PINNED/pyproject.toml"
done
# ---------------------------------------------------------------------------
echo ""
echo "--- hooks.json wiring ---"
@@ -365,7 +664,7 @@ if ! command -v apm > /dev/null 2>&1 || ! command -v git > /dev/null 2>&1; then
echo " $SKIP_REASON"
else
PROBE="$(mktemp -d)"
trap 'rm -rf "$FAKE_BIN" "$WORK" "$PROBE"' EXIT
trap 'rm -rf "$FAKE_BIN" "$WORK" "$TOOLS" "$UV_BIN" "$PIN_BIN" "$PATH_APM_BIN" "$PINNED" "$PROBE"' EXIT
UPSTREAM="$PROBE/upstream.git"
git init -q "$UPSTREAM"
@@ -431,6 +730,7 @@ else
#!/usr/bin/env bash
pwd > "$WORK/apm-cwd"
case "\$1" in
--version) echo "Agent Package Manager (APM) CLI version 0.39.3"; exit 0 ;;
outdated) cat "$PROBE/genuine-$want.txt"; exit 0 ;;
update) touch "$WORK/update-was-called"; exit 0 ;;
esac