Files
Holocron/plugins/kyberforge/docs/research/docs/microsoft-apm/hooks-primitive-schema.md
T
Defame1297andClaude Opus 5.5 5ca2056046 docs(research): refresh apm reference set to 0.39.3 (#265)
Add lockfile-and-compatibility, approvals-and-executables and
migration-0-28-to-0-39 topics; update installation, cli-reference, the
prompt, hooks and instructions topics for 0.39.x behaviour; label the
remaining 0.28.0 observations by version. Sources: the v0.39.3 source,
CHANGELOG and docs; Context7 was unavailable.

Refs #265

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
2026-12-01 21:46:06 +00:00

10 KiB

topic, source_keys
topic source_keys
hooks-primitive-schema
context7-microsoft-apm
apm-github-repo
apm-v0-39-3-source
apm-v0-39-3-changelog
apm-v0-39-3-docs

File location, naming, and format — confirmed .json, not assumed

.apm/hooks/*.json (legacy fallback: bare hooks/*.json at package root, still discovered — _has_hook_json() checks both hooks/ and .apm/hooks/). This is genuinely JSON, not YAML or Markdown-with-frontmatter like every other primitive — confirmed directly from source (apm_cli/integration/hook_integrator.py module docstring: "Integrates hook JSON files...") and from apm_cli/models/validation.py, which states a hook-only package's files define "hook handlers per the Claude Code hooks specification" — i.e. the canonical authoring shape APM expects is Claude Code's own native hook JSON shape, not an APM-invented one. This is consistent with APM's general P1 principle (no invented primitive frontmatter/format) extending even to hooks: author in whichever native harness shape you like, and APM normalizes.

Accepted input shapes (APM normalizes both into an internal vendor-neutral IR before rendering per target):

// "Nested" wrapper (what the docs' canonical example shows)
{ "hooks": { "PreToolUse": [ { "hooks": [ {"type": "command", "command": "./scripts/validate.sh", "timeout": 10} ] } ] } }

// "Naked" top-level settings-slice (Claude Code settings.json shape, unwrapped)
{ "PreToolUse": [ { "hooks": [ {"type": "command", "command": "./scripts/validate.sh", "timeout": 10} ] } ] }

Both are accepted; APM's discovery/parsing layer detects and unwraps either. There is no separate Hook/HookPrimitive dataclass in primitives/models.py (unlike Instruction) — hooks are represented instead by a dedicated vendor-neutral IR (apm_cli/integration/hook_ir.py): HookHandler(command, platform="all", timeout_seconds, provenance, metadata) grouped into HookBinding(event, handlers, matcher, provenance, metadata) grouped into HookDocument(bindings). This IR is populated during install-time integration, not during the generic primitive-discovery pass used for instructions/contexts/agents.

Event names are case-convention-sensitive by target and get remapped, not just passed through. Author in either PascalCase (Claude convention: PreToolUse, PostToolUse, UserPromptSubmit, SessionStart, Stop) or camelCase (Copilot convention: preToolUse, postToolUse, etc.) — _HOOK_EVENT_MAP per-target dictionaries translate between them during merge/deploy. An event name whose casing doesn't match the target's expected convention and has no explicit mapping entry triggers a non-fatal warning at install time (_emit_hook_event_diagnostics) — not a hard failure, but a real signal that the event likely won't fire.

Script path placeholders are rewritten per target during deploy: ${CLAUDE_PLUGIN_ROOT}/path, ${CURSOR_PLUGIN_ROOT}/path, ${PLUGIN_ROOT}/path, and bare ./path all get resolved relative to the package root and rewritten to whatever the target expects; bare system commands (no path separators) pass through unchanged.

Compile-time mapping per target — both are real reconstruction, differently shaped

Neither Claude nor Copilot receives a byte-verbatim copy of the source hook JSON — this is a genuine, structural transform on both sides, driven by apm_cli/integration/hook_native_formats.py and hook_integrator.py.

Claude Code — merged into .claude/settings.json, not a standalone file. claude is registered in _MERGE_HOOK_TARGETS with config_filename="settings.json", schema_strict=True. Behavior (per the integrator's own class docstring: "Claude: Merged into .claude/settings.json hooks key + .claude/hooks//"):

  • Event bindings are merged into the "hooks" key of .claude/settings.json, using Claude's native nested-matcher-group shape ({"hooks": {"PreToolUse": [{"hooks": [{"type": "command", "command": "...", "timeout": N}]}]}}), with PascalCase event names.
  • Any referenced script files are physically copied to .claude/hooks/<package-name>/, and the command field is rewritten to point at the copied location.
  • An ownership sidecar (apm-hooks.json) tracks which entries in the shared settings.json were APM-installed, so apm install/uninstall can cleanly add/remove only its own entries without clobbering hand-authored hooks a user already had in that file.

Copilot CLI — dedicated per-file deployment, flat/camelCase, field-renamed. copilot is deliberately not in _MERGE_HOOK_TARGETS (confirmed in _hook_dropped_targets.py: "Names not registered in _MERGE_HOOK_TARGETS (e.g. copilot, which uses per-file, not merged, hook deployment...)"). Instead PrimitiveMapping("hooks", ".json", "github_hooks") deploys a dedicated file per source hook file. The native Copilot shape differs structurally from Claude's, per the module docstring:

{
  "version": 1,
  "hooks": { "preToolUse": [ {"type": "command", "bash": "./scripts/validate.sh", "timeoutSec": 10} ] }
}

Differences from the Claude/source shape: flat arrays (no nested matcher-group wrapper), camelCase event keys, a required top-level "version": 1, and handler commands split by platform (bash / powershell keys) instead of a single command key, with timeoutSec replacing timeout.

Compile-time file placement

Target Output location Mechanism
Claude Code .claude/settings.json ("hooks" key, merged) + scripts copied to .claude/hooks/<pkg>/ Merge into existing shared config file, ownership tracked via apm-hooks.json sidecar
Copilot CLI .github/hooks/<name>.json Dedicated per-file deploy, reshaped to Copilot's flat/camelCase/version:1 schema

Validation constraints and gotchas

  • Copilot's native payload has an enforced shape (_validate_copilot_payload): top-level "version" must equal 1; "hooks" must be an object; each event's value must be a list; each entry must be an object; if an entry has a "hooks" key, its value must be a list of objects. These errors are collected and surfaced before any file is written (fail before mutation, not after).
  • Malformed existing config fails closed, not silently. If .claude/settings.json (or another merge target's config) is unreadable/malformed JSON, APM leaves it byte-identical and logs an actionable warning rather than overwriting or corrupting it — the same fail-closed posture applies to orphaned apm-hooks.json sidecars when their native JSON counterpart is already gone.
  • Dropping a target from apm.yml's targets: list does not auto-clean its merged hook entries unless apm install/reconcile logic explicitly walks the complement set (reconcile_dropped_targets) — a real, documented gap the code works around rather than a design guarantee; relying on "just remove the target and hooks disappear" is not safe without a fresh apm install.
  • Event-casing mismatches are warnings, not errors — a hook authored with the wrong casing for a target and no applicable rename mapping will silently not fire at runtime; APM only logs a warning at install time, it does not block the install or refuse to deploy the file.
  • No dedicated Hook/HookPrimitive validation dataclass exists comparable to Instruction.validate() — validation is distributed across _validate_copilot_payload (Copilot-shape-specific) and general JSON-parseability checks, not a single primitive-level contract. This mirrors the same "no independent validation model" gap already documented for the agent primitive.

Changes through 0.39.3 (source, CHANGELOG and docs; not run)

The sections above were written against 0.28.0. The merge into .claude/settings.json is still the mechanism in 0.39.3. The following changed:

  • Filename routing is gone (0.36.0, #3613). Filenames such as *-<target>-hooks.json and hooks-<target>.json no longer send a hook to one target only, so every hook file reaches every targeted harness.
    • To limit a hook to Claude, ship it in a package whose apm.yml sets targets: [claude], or narrow the dependency with object-form targets:.
    • If dropping the routing would hand a target a file the old name kept from it, install fails for that package and names the file.
  • Event names. SessionStart and sessionStart both map to SessionStart for Claude. Copilot events that have a Claude equivalent are written to .claude/settings.json under the Claude name. For Copilot, SessionStart becomes sessionStart.
  • Where ownership is recorded. apm marks its entries with _apm_source while it works. On write, it moves those markers into a sidecar, .claude/apm-hooks.json, so settings.json itself carries no apm markers. On reinstall the markers are read back from the sidecar.
  • Edited entries are kept (0.38.1, 0.39.0).
    • When a user edits a deployed entry, apm keeps the edited copy and writes the deployed entry beside it, with a warning. A byte-identical user copy does not count as an edit.
    • Install exits 1 when an edited hook file or script blocks new content.
    • Uninstall refuses when a package's entry was edited or its sidecar record is missing.
  • Ordering. Entries merge in full-install order. Do not rely on order across packages. Your own entries never move.
  • Approval. When the executables gate is on, a hook deploys only under a grant: type-level hooks: true, or a per-file content pin. A pin covers the hook JSON and every file deployed with it. See approvals-and-executables.md.
  • Command safety (0.39.0, #5600). Only hook commands that stay inside the package and use no shell expansion are deployed. Shell expansion means $(...), backticks, eval, here-docs and subshells.
  • Settings-file lifecycle.
    • A .claude/settings.json that apm created is deleted once its last entry leaves.
    • An emptied one that apm has no creation record for is kept, with a warning.
    • A hook config that is not valid JSON is never rewritten. A merge into it exits 1; a cleanup-only run warns and exits 0.
  • Audit. These checks cover merged Claude hook entries: hook-approval-pins, exec-status-current, local-bundle-hooks, content-integrity and drift. drift compares only apm-owned entries. apm install --frozen also fails when an apm-managed merged entry was edited.