apm icon indicating copy to clipboard operation
apm copied to clipboard

apm audit --ci: false 'modified' drift on .claude/apm-hooks.json when the consumer's settings.json already has hook events (key order)

Open jmagar opened this issue 2 months ago • 4 comments

Summary

apm audit --ci reports the Claude hooks sidecar (.claude/apm-hooks.json) as modified on every run in a consumer project whose .claude/settings.json already contained a hook event group before the package was installed, even though the sidecar on disk is byte-identical across repeated apm install --frozen runs.

Cause

The drift check (apm_cli/install/drift.py, diff_scratch_against_project) replays the install into a scratch root and compares the sidecar byte-for-byte ("sidecars remain byte-for-byte owned"). The two sidecars hold the same entries but in a different top-level key order:

  • The live merge writes the sidecar events in the order the consumer's existing settings.json already lists them (the merge appends into an existing hooks object, so a pre-existing PreToolUse group comes first).
  • The scratch replay starts from an empty settings.json, so the events come out in the package's hooks/hooks.json order.

Both files parse to equal JSON; _normalize does not canonicalize key order, so the comparison fails.

Reproduction (APM 0.31.0, Python 3.12, macOS)

Package: any plugin collection whose hooks/hooks.json declares SessionStart before PreToolUse, for example:

{ "hooks": {
  "SessionStart": [ { "matcher": "startup|resume|compact", "hooks": [ { "type": "command", "command": "python3 \"${CLAUDE_PLUGIN_ROOT}/scripts/hooks/repo_context.py\"" } ] } ],
  "PreToolUse":   [ { "matcher": "Edit|Write|MultiEdit|Bash", "hooks": [ { "type": "command", "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guard.sh\"" } ] } ]
} }

Consumer:

mkdir consumer && cd consumer
apm init --yes --target claude,codex
mkdir -p .claude
cat > .claude/settings.json <<'JSON'
{"hooks":{"PreToolUse":[{"matcher":"Edit|Write","hooks":[{"type":"command","command":"python3 my_guard.py"}]}]}}
JSON
apm install <path-or-ref-to-package> --no-policy --no-audit
apm audit --ci

Result:

│ drift │ drift detected: 1 file(s): .claude/apm-hooks.json │
  modified (1):
    - .claude/apm-hooks.json
  [i] Run 'apm install' to re-sync deployed files with the lockfile.

apm install --frozen again produces the identical sidecar (same SHA-256), and apm audit --ci fails again. The same consumer with no pre-existing .claude/settings.json audits clean. Reordering the package's hooks/hooks.json so PreToolUse comes before SessionStart also makes the audit pass, which confirms the cause is key order rather than content.

Diff of the two sidecars from the replay (project first, scratch second): the PreToolUse and SessionStart objects are swapped; every field inside them is identical.

Expected

The sidecar comparison (and ideally the sidecar writer) should be order-insensitive at the event-key level, for example by comparing json.loads results with sorted keys or by writing the sidecar with sort_keys=True.

Workaround

Consumers pin the package and allow-list .claude/apm-hooks.json in their CI audit step, or package authors order hooks/hooks.json events to match the most common pre-existing consumer order. Neither is robust.

jmagar avatar Sep 22 '26 18:09 jmagar

Automated triage advisory

Target: #3062 | Watermark: 2026-09-22T18:15:10Z (created, no comments)

Suggested issue comment:

Detailed, reproducible bug report: apm audit --ci reports a false 'modified' drift on .claude/apm-hooks.json when a consumer's pre-existing .claude/settings.json causes the live merge and the scratch replay to emit hook events in different (but semantically equal) key order, since _normalize in apm_cli/install/drift.py does not canonicalize key order. Includes root cause, full repro, and a suggested fix (order-insensitive comparison or sort_keys=True on write). A responsible maintainer should record scope approval per CONTRIBUTING.md before implementation. This comment is advisory only and does not authorize implementation.

Generated by Triage Panel · copilot · auto · 89.6 AIC · ⌖ 6.34 AIC · ⊞ 12K · ◷

github-actions[bot] avatar Sep 23 '26 12:09 github-actions[bot]

Decision: approve Area: project Scope: Remove the false Claude hook-sidecar drift report caused solely by differing JSON object-key order between live generation and audit replay, while preserving detection of meaningful configuration changes. Done when: The reported consumer with pre-existing hook event groups audits clean when generated configuration is unchanged apart from irrelevant object-key ordering; repeated installation is stable; changes to commands, values, additions, removals, and meaningful hook execution order remain detectable. Focused regression coverage demonstrates both the false-positive repair and preserved real-drift detection. Out of scope: Blanket audit exemptions for the hook file; disabling drift checks; ignoring list or execution order; broadly relaxing integrity or ownership checks; a general hook translation redesign; executing hooks during audit to establish equivalence. Review contact: @danielmeppiel

Accept the bounded result, not a preselected comparison implementation. Make generated-file production/replay or comparison consistent without weakening the guardrail. Native-plugin delivery does not remove the need for truthful checks on the currently supported integration.

Recorded by Copilot on behalf of @danielmeppiel after his explicit 2026-10-01 choice: "Accept all four bounded repairs (Recommended)" for #3078, #3055, #3035, and #3062. No implementation assignment, merge approval, new parent relationship, concurrent delivery commitment, or release date is implied.

danielmeppiel avatar Oct 01 '26 20:10 danielmeppiel

I'd like to take this within the approved scope. I reproduced the false sidecar drift and prepared a focused JSON comparison fix, with a hermetic install/audit regression and checks that meaningful changes still fail. I'll link the PR here once published.

Pybsama avatar Oct 03 '26 05:10 Pybsama

Decision: approve Area: project Scope: Remove the false Claude hook-sidecar drift report caused solely by differing JSON object-key order between live generation and audit replay, while preserving detection of meaningful configuration changes. Done when: The reported consumer with pre-existing hook event groups audits clean when generated configuration is unchanged apart from irrelevant object-key ordering; repeated installation is stable; changes to commands, values, additions, removals, and meaningful hook execution order remain detectable. Focused regression coverage demonstrates both the false-positive repair and preserved real-drift detection. Out of scope: Blanket audit exemptions for the hook file; disabling drift checks; ignoring list or execution order; broadly relaxing integrity or ownership checks; a general hook translation redesign; executing hooks during audit to establish equivalence. Review contact: @danielmeppiel

danielmeppiel avatar Oct 16 '26 08:10 danielmeppiel