Files
Holocron/plugins/kyberforge/docs/hooks.md
T
Defame1297andClaude Opus 5.5 1fb8cc797b 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
2026-12-02 00:05:03 +00:00

11 KiB

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/:

{
  "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:

"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. 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 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. 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 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 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.

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.