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
187 lines
12 KiB
Markdown
187 lines
12 KiB
Markdown
# Hooks
|
|
|
|
Reference for this plugin's hook definitions: where to edit them, what apm does with them, and what
|
|
the host ends up reading.
|
|
|
|
## Where to edit
|
|
|
|
Author hooks in `plugins/kyberforge/.apm/hooks/*.json`. `.apm/` is the only content source and
|
|
`apm install` is the only supported install path (ADR-0024) — there is no generated mirror at the
|
|
plugin root and no per-plugin `plugin.json`, so `.apm/hooks/` is both where you edit and what ships.
|
|
|
|
apm merges every `*.json` in that directory into a single hook definition and writes the event
|
|
bindings into each target the consuming project enables: `.claude/settings.json` for Claude Code,
|
|
and per-target files for Copilot and Codex (see "Deployed shape" and "Other runtimes" below). Scripts a
|
|
hook invokes live in the same directory, alongside the JSON that references them.
|
|
|
|
## Hook file structure
|
|
|
|
The shape Claude Code reads, and therefore the shape to author under `.apm/hooks/`:
|
|
|
|
```json
|
|
{
|
|
"hooks": {
|
|
"PostToolUse": [
|
|
{
|
|
"matcher": "Bash",
|
|
"hooks": [
|
|
{ "type": "command", "command": "echo 'tool used'" }
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
```
|
|
|
|
Events (**partial list**): `PreToolUse`, `PostToolUse`, `Notification`, `Stop`, and `SessionStart`
|
|
(verified end-to-end by the hook below). Claude Code's plugin hook set is larger — `SessionEnd`,
|
|
`UserPromptSubmit`, `PreCompact` and `SubagentStop` also exist — and this repo's vendored corpus does
|
|
not enumerate it anywhere: `docs/research/docs/claude-code-plugins/configuration.md:100` describes
|
|
the file as "Event handlers (PreToolUse, PostToolUse, etc.)", and `agent-definition.md:53` covers
|
|
only the per-agent `hooks` field, not the plugin-level set. Treat the five names above as the ones
|
|
this repo has verified, not as the schema. Check Claude Code's own hooks documentation before wiring
|
|
an event not listed here.
|
|
|
|
## Referencing a script — use the `.apm/` path
|
|
|
|
Use `${CLAUDE_PLUGIN_ROOT}` to reference scripts inside this plugin; the plugin runs from a cache or
|
|
`apm_modules/` path after install, not its original repo location. **Address the script at its
|
|
`.apm/` path:**
|
|
|
|
```json
|
|
"command": "${CLAUDE_PLUGIN_ROOT}/.apm/hooks/check-apm-current.sh"
|
|
```
|
|
|
|
The obvious-looking `${CLAUDE_PLUGIN_ROOT}/hooks/check-apm-current.sh` does not work, and fails
|
|
quietly enough to be worth spelling out. apm resolves the placeholder against the installed package
|
|
root, and there is no `hooks/` directory there at all — the package's content is `.apm/`. apm prints
|
|
`Hook script not found: .../hooks/check-apm-current.sh` and then deploys the hook anyway, pointing at
|
|
a path with no file behind it.
|
|
|
|
`tests/test-apm-current-hook.sh` pins the reference so a well-meaning "simplification" back to
|
|
`hooks/` fails the suite rather than silently disabling the hook.
|
|
|
|
## Deployed shape
|
|
|
|
At install, apm merges the event bindings into `.claude/settings.json`, copies the referenced script
|
|
to `.claude/hooks/<pkg>/` (preserving its executable bit, preserving the `.apm/hooks/` subpath), and
|
|
rewrites `command` to a `${CLAUDE_PROJECT_DIR}`-relative path. Ownership of its own entries is
|
|
tracked in a `.claude/apm-hooks.json` sidecar, so an uninstall removes them without touching
|
|
hand-authored hooks. Both `.claude/hooks/` and the sidecar are gitignored install output.
|
|
|
|
Note that apm's **executable-trust gate is off** unless the consuming project's `apm.yml` has an
|
|
`executables:` block — without one, package hooks deploy with no prompt. The grant is
|
|
**version-blind**: apm also matches an allow key such as `kyberforge#<version>` by its bare package
|
|
name, so a version bump on one side does not stop the hook deploying (verified against apm 0.28.0;
|
|
the correction is recorded in ADR-0019 and `docs/spec/gates.md`). This repo still keeps each key at
|
|
`<name>#<version>` as a record of which version was reviewed, and `check-executables-allow-sync` is
|
|
the pre-push gate that holds every plugin shipping `.apm/hooks/` or `bin/` to that.
|
|
|
|
apm 0.28.0 prints "parked pending approval" for a granted package even when it deploys the hook
|
|
(holocron#208). Check for the deployed script and settings entry, not the warning.
|
|
|
|
## The SessionStart hook
|
|
|
|
`check-apm-current.sh` keeps an apm-consumed install level with its remote: it runs `apm outdated`,
|
|
and if anything is behind, runs `apm update --yes` and returns `reloadSkills: true` so the running
|
|
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. 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 `<root>/.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 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
|
|
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
|
|
`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
|
|
it inert in any project that does not consume packages through apm. Both `apm` invocations run
|
|
against the same resolved directory. The earlier spelling checked a bare `apm.lock.yaml` against the
|
|
session's cwd, so a session opened in a subdirectory of an apm-consuming repo no-opped silently.
|
|
Keep the cwd fallback: a host that sets no `CLAUDE_PROJECT_DIR` must still get inert-but-harmless
|
|
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
|
|
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 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
|
|
stages a genuinely outdated dependency against the **real** `apm` — a local git repo reached through
|
|
`url.<path>.insteadOf` rewrites, so it needs no network — and replays that genuine output through the
|
|
hook.
|
|
|
|
## Other runtimes
|
|
|
|
An earlier version of this section said Copilot loads no hooks from an apm package, because nothing
|
|
pointed Copilot at a hooks file and apm wrote only a Claude-shaped definition. That no longer holds:
|
|
apm 0.28.0 shapes and deploys hooks per target itself, with no `plugin.json` pointer involved.
|
|
Sources: `docs/research/infra-packages/enforcement-hooks.md` on the `research/enforcement-hooks`
|
|
branch, and the [delivery guard hook](https://git.rkdr.net/Mortis/Holocron/issues/190) work.
|
|
|
|
| Target | Where apm writes the hook | Status |
|
|
|---|---|---|
|
|
| `copilot` | One file per hook, `.github/hooks/<pkg>-<name>.json`, with the script under `.github/hooks/scripts/<pkg>/.apm/hooks/`. Event names are renamed to camelCase and `version: 1` is added. | **Unverified.** Entries keep Claude's nested matcher group with `command`/`timeout`. Copilot's docs accept `command`/`timeout` as aliases for `bash`/`powershell`, but don't document the nested group, so whether Copilot CLI runs the hook is untested until a Copilot account exists. |
|
|
| `codex` | Merged into `.codex/hooks.json`, with the script under `.codex/hooks/<pkg>/.apm/hooks/`. Claude's event names and shape are kept. | apm skips the target unless `.codex/` already exists, and Codex runs project hooks only after a human trusts them. |
|
|
|
|
Only enabled targets get a hook: this repo's root `apm.yml` targets `claude` alone, so nothing lands
|
|
in `.github/hooks/` or `.codex/` here. VS Code's Copilot reads both `.github/hooks/` and
|
|
`.claude/settings.json`, so a package installed for both `claude` and `copilot` runs each hook
|
|
twice there.
|
|
|
|
**The `SessionStart` hook is Claude-only in practice.** `check-apm-current.sh` answers with Claude
|
|
Code's `reloadSkills` output and reads `${CLAUDE_PROJECT_DIR}`, so on another runtime it at most
|
|
runs the update without reloading anything.
|
|
|
|
## Symlinks under `.apm/` do not survive, and nothing reports it
|
|
|
|
Do not author any file under `plugins/kyberforge/.apm/` as a symlink. apm's copy path filters
|
|
symlinks out silently: `ignore_non_content()` in `apm_cli/security/gate.py` is a
|
|
`shutil.copytree` ignore callback that drops every entry answering `is_symlink()`, commented
|
|
"Excludes symlinks (security)". The file never reaches the consumer's install, and no warning is
|
|
printed at any point.
|
|
|
|
**No gate catches this.** The pre-push check that used to read the `.apm/` tree and fail on the
|
|
offending path was deleted along with the content mirror, and the decision was taken not to replace
|
|
it (ADR-0024 consequence 7). This document is the only thing standing between a symlink and silent
|
|
content loss. No symlink exists under any `.apm/` today; add one and it is dropped on deploy with
|
|
nothing reporting it. Replace it with a regular file.
|
|
|
|
The old exemption for `.apm/<category>/<name>/tests/` no longer applies either. That directory was
|
|
exempt only because the mirror excluded it; apm deploys it like any other content (ADR-0024
|
|
consequence 2), so a symlink there loses content the same as anywhere else.
|