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:
1 parent
0ad15c80bc
commit
1fb8cc797b
7 files changed
+436
-33
No files matched your search
@@ -24,33 +24,98 @@ project_dir="${CLAUDE_PROJECT_DIR:-$PWD}"
|
||||
# No lockfile means nothing was installed through apm here — e.g. a host that
|
||||
# installed this plugin natively. Say nothing and cost nothing.
|
||||
[[ -f "$project_dir/apm.lock.yaml" ]] || exit 0
|
||||
command -v apm > /dev/null 2>&1 || exit 0
|
||||
|
||||
# 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;
|
||||
# 2. otherwise the `apm` on PATH;
|
||||
# 3. otherwise nothing: exit silently.
|
||||
#
|
||||
# "Pins apm-cli" means a pyproject.toml whose text carries a quoted apm-cli
|
||||
# requirement (`"apm-cli==0.39.3"`, `'apm_cli>=0.39'`, ...) AND a uv.lock next
|
||||
# to it. It is a text match, not a TOML parse: the hook must not depend on a
|
||||
# Python being on PATH, and a quoted `apm-cli` requirement string has no other
|
||||
# plausible meaning in a pyproject.toml. The uv.lock is required because
|
||||
# `--frozen` runs from the lock and has nothing to run without one; a pin with
|
||||
# no lock falls through to PATH apm and its version check.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# 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).
|
||||
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
|
||||
apm_cmd=(uv run --project "$project_dir" --frozen --no-sync --offline apm)
|
||||
apm_label="uv"
|
||||
elif command -v apm > /dev/null 2>&1; then
|
||||
apm_cmd=(apm)
|
||||
apm_label="path"
|
||||
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
|
||||
|
||||
# `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.
|
||||
#
|
||||
# There is no --json/machine-readable flag on `apm outdated` (verified against
|
||||
# apm 0.28.0), 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 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)"
|
||||
[[ "$stale_count" =~ ^[0-9]+$ ]] || stale_count="some"
|
||||
|
||||
# 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.
|
||||
version_ok() {
|
||||
local version_output version
|
||||
version_output="$(timeout 15 "${apm_cmd[@]}" --version 2> /dev/null)" || return 1
|
||||
version="$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+' <<< "$version_output" | head -n 1)"
|
||||
[[ "$version" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] || return 1
|
||||
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
|
||||
((10#${have[i]} > need[i])) && return 0
|
||||
((10#${have[i]} < need[i])) && return 1
|
||||
done
|
||||
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"
|
||||
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."
|
||||
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.
|
||||
#
|
||||
# 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
|
||||
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)"
|
||||
[[ "$stale_count" =~ ^[0-9]+$ ]] || stale_count="some"
|
||||
|
||||
# What to do with the rewritten lock depends on the branch (ADR-0019): on the
|
||||
# default branch it is a real update to commit or discard; on a feature branch it
|
||||
# is churn unrelated to the branch and should be discarded. The branch name only
|
||||
@@ -73,14 +138,21 @@ if [[ -n "$current_branch" && -n "$default_branch" ]]; then
|
||||
if [[ "$current_branch" == "$default_branch" ]]; then
|
||||
lock_advice="this is the default branch, so commit it or discard it deliberately."
|
||||
else
|
||||
lock_advice="this is a feature branch, so discard it: git checkout -- apm.lock.yaml && apm install"
|
||||
if [[ "$apm_label" == "uv" ]]; then
|
||||
lock_advice="this is a feature branch, so discard it: git checkout -- apm.lock.yaml && uv run apm install"
|
||||
else
|
||||
lock_advice="this is a feature branch, so discard it: git checkout -- apm.lock.yaml && apm install"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if timeout 300 apm update --yes > /dev/null 2>&1; then
|
||||
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
|
||||
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: apm update --yes"
|
||||
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}"
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,5 +1,5 @@
|
||||
name: kyberforge
|
||||
version: 2.2.1
|
||||
version: 2.3.0
|
||||
description: Skills and agents for creating, maintaining, and managing an apm plugin marketplace for Claude Code and GitHub Copilot.
|
||||
author:
|
||||
name: Mortis
|
||||
|
||||
@@ -87,6 +87,29 @@ and if anything is behind, runs `apm update --yes` and returns `reloadSkills: tr
|
||||
session picks up the redeployed content. Rationale, measurements, and the failure modes are in
|
||||
ADR-0019.
|
||||
|
||||
**Which apm it runs.** The hook picks one apm and uses it for every call (ADR-0034):
|
||||
|
||||
1. `uv run --project <root> --frozen --no-sync --offline apm`, when the project pins apm and `uv` is
|
||||
on `PATH`. A pin is a `pyproject.toml` carrying a quoted `apm-cli` requirement plus a `uv.lock`
|
||||
beside it. It is matched as text so the hook needs no Python.
|
||||
2. Otherwise the `apm` on `PATH`.
|
||||
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.
|
||||
|
||||
**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
|
||||
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.
|
||||
|
||||
**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
|
||||
`apm install`, and `uv run apm update --yes` or `apm update --yes`. No command output is ever
|
||||
interpolated.
|
||||
|
||||
**Where it looks for the lockfile.** The hook resolves a project directory as `${CLAUDE_PROJECT_DIR}`
|
||||
when the host exports it (Claude Code does, for SessionStart hooks) and the current directory
|
||||
otherwise, then exits silently unless that directory holds an `apm.lock.yaml` — which is what makes
|
||||
@@ -97,14 +120,14 @@ 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
|
||||
`timeout 60 apm outdated` plus `timeout 300 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`. 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
|
||||
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`
|
||||
has no `--json` or otherwise machine-readable output (verified against apm 0.28.0), so the hook
|
||||
has no `--json` or otherwise machine-readable output (verified against apm 0.28.0 and 0.39.3), so the hook
|
||||
greps its text. apm prints `1 outdated dependency found` in the singular when exactly one package is
|
||||
behind and `N outdated dependencies found` otherwise; matching only the plural silently misses a
|
||||
one-package drift. Because a mocked `apm` would keep a reworded release invisible, the test suite
|
||||
|
||||
Reference in new issue
Block a user