Skip to content

fix(install): retire a package's hooks when apm deny or a removed approval gates them - #3944

Merged
Daniel Meppiel (danielmeppiel) merged 10 commits into
mainfrom
danielmeppiel-fix-3924-deny-retires-hooks
Nov 19, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 10 commits into
mainfrom
danielmeppiel-fix-3924-deny-retires-hooks

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Description

After apm deny <pkg> (or an approval removed while the executables gate is on), apm install skipped the package's hooks but left the entries it had deployed earlier in merged harness configs such as .claude/settings.json. The hooks kept firing, often pointing at a copied script that stale-file cleanup had already deleted. apm audit --ci disagreed in both directions: its drift replay ignored the gate, so it replayed denied hooks. That hid the Claude leftover and flagged a correctly cleaned Copilot project as unintegrated.

What changed:

  • Install retires gated hooks (install/services.py). When the gate withholds hooks, install calls HookIntegrator.reconcile_package_target_restriction for every active target. This is the existing source-marker retirement that target restrictions and local-bundle hooks (fix(install): register local plugin bundle hooks through the hook integrator #3897) use, so no second removal path was added. Per-file Copilot hooks and copied scripts are still removed by stale-file cleanup. User-authored and sibling-package entries stay.
  • Audit replay applies the same gate (install/drift.py, install/exec_gate.py). For lock entries whose exec_status is gated_pending_approval or denied, replay passes the deny-wins allow-map from resolve_project_exec_allow_map. Entries recorded as fully deployed replay exactly as before, so CI machines without user approvals see no change for them.
  • Denied packages are named as denied (exec_gate.py, security/executables.py). A new denied_exec_types in the trust owner classifies hard denies. The skip line now reads hooks skipped (denied). Run 'apm policy explain <pkg>' ... instead of telling the user to approve. A package whose only blocked types are hard-denied is no longer parked for approval.
  • Audit catches leftovers (install/drift.py, integration/hook_ownership.py). A shared hook config that replay no longer produces but that still holds APM-owned (non-local-bundle) entries is reported as drift, so a denied hook left registered fails apm audit --ci.
  • One policy per audit run (commands/audit.py, install/audit_replay.py, policy/ci_checks.py). The audit fetches org policy once and passes it to replay through a policy_provider, so --no-policy and --policy apply to the replayed gate too. Replay never migrates user consent.
  • apm deny hint: it now prints Run 'apm install' to remove executables already deployed.
  • The architecture guard install-deployment-executable-trust-context now requires these routings, and a parametrized mutation test proves each one has teeth.

Before (main 0e19cce): deny + reinstall leaves bash "${CLAUDE_PROJECT_DIR}/.claude/hooks/hooky/scripts/hi.sh" in .claude/settings.json with the script gone. apm audit --ci exits 0 for one target and 1 for another.
After: the entry and its sidecar record are removed, the user's own hook stays, and apm audit --ci reports no drift.

Issue and approved scope

Issue: Fixes #3924

Human scope-approval comment: #3924 (comment) (Review contact Daniel Meppiel (@danielmeppiel))

Does this PR complete the issue, or what remains? It completes the scope. Out of scope per the record: MCP/LSP/bin/canvas retirement under deny, local-bundle hooks, and the precedence ladder.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Validation evidence (macOS arm64, head 7dfb5d6 merged with main):

Command Result
APM_BINARY_PATH=.venv/bin/apm .venv/bin/python -m pytest tests/integration/test_executables_deny_retires_hooks_lifecycle.py -p no:xdist -q on unfixed main 3 failed (claude-deny, claude-revoke, copilot-deny)
same, on this branch (claude-deny/revoke, copilot, cursor, codex, gemini, windsurf) 7 passed
.venv/bin/python -m pytest tests/unit/install/test_replay_exec_allow_map.py tests/integration/test_executables_gate_integration.py tests/unit/install/test_ci_audit_materialization.py tests/unit/policy/test_ci_checks.py tests/integration/test_hook_sidecar_drift_e2e.py tests/unit/test_drift_detection.py -p no:xdist -q (one file at a time) 5, 81, 13, 94, 1, 59 passed
Targeted unit + integration sweep (audit, policy, security, commands, drift, gate) 3373 passed
.venv/bin/python -m pytest tests/unit/install -p no:xdist -q (earlier head) 3215 passed; test_drift_perf passes alone (host load)
tests/integration/test_architecture_owner_rule_mutations.py -k "gated_hooks_retire or executable-trust or covers_every or deterministic" + tests/unit/scripts/test_architecture_runner.py passed
Mutation-break: drift leftover branch and per-type parking check removed target tests failed, guards restored
uvx [email protected] check / format --check, pylint R0801, lint-auth-signals.sh, lint-architecture-boundaries.sh, file-length and relative_to guards clean
gh pr checks 3944 31 pass, 3 skipping

Panel advisory: #3944 (comment)

Changelog

  • One line in one section under ## [Unreleased] in CHANGELOG.md:
    user-visible effect first, 25 words or fewer, ending with (#PR);
    breaking changes only under upgrade notes; no Highlights; see
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • Spec edit: docs/src/content/docs/specs/openapm-v0.1.md updated
    (new/changed <a id="req-XXX"></a> anchor + prose + Appendix C
    row).
  • Manifest edit: docs/src/content/docs/specs/manifests/openapm-v0.1.requirements.yml
    updated.
  • Test edit: a @pytest.mark.req("req-XXX") test under
    tests/spec_conformance/ added or extended.
  • CONFORMANCE.{md,json} regenerated via
    uv run --extra dev python -m tests.spec_conformance.gen_statement
    and committed.
  • N/A -- this PR does not change OpenAPM-observable behaviour.

…gate withholds them

apm deny (or an approval removed while the gate is on) made install skip
the package's hooks but left the entries it had deployed earlier in merged
harness configs such as .claude/settings.json. Install now retires them
through HookIntegrator.reconcile_package_target_restriction, the same
source-marker retirement used for target restrictions and local bundles;
copied scripts already leave via stale-file cleanup.

apm audit --ci replay now applies the same deny-wins allow-map for lock
entries recorded as gated or denied, so it no longer replays withheld
hooks. A hard-denied package is reported as denied and is not parked for
approval.

Refs #3924

apm-spec-waiver: executables-gate hook retirement is CLI behavior with no OpenAPM requirement

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 15:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Audit replay can miss denied hooks left registered in Claude and falsely report drift by misinterpreting hook approvals and ignoring the caller’s policy context.

3 open issues
What changed in this PR

This PR addresses #3924 by retiring previously deployed package hooks when denial or removed approval blocks them. It reuses the existing hook retirement mechanism and updates audit replay to account for gated dependencies.

Changes:

  • Retire gated hooks through the hook integrator and distinguish hard denial from missing approval.
  • Apply executable approval checks during audit replay for gated lock entries.
  • Add lifecycle and architecture regression tests, plus user-facing documentation.
File Description
tests/​integration/​test_executables_deny_retires_hooks_lifecycle.py Tests hook retirement, user-hook preservation, messaging, and audit results.
tests/​integration/​test_architecture_owner_rule_mutations.py Adds mutations checking required retirement and trust routing.
src/​apm_cli/​security/​executables.py Adds hard-denied executable type classification.
src/​apm_cli/​integration/​hook_integrator.py Broadens the retirement method's docstring; no functional change.
src/​apm_cli/​install/​services.py Invokes hook retirement when approval blocks deployment.
src/​apm_cli/​install/​exec_gate.py Adds denial-aware messaging and lazy replay approval resolution.
src/​apm_cli/​install/​drift.py Passes approval maps into gated dependency replay.
src/​apm_cli/​install/​context.py Tracks denied executable types for package diagnostics.
scripts/​architecture_linter/​checks/​install_lsp_plugin.py Requires canonical trust and retirement routing.
packages/​apm-guide/​.apm/​skills/​apm-usage/​commands.md Documents hook retirement after denial or revoked approval.
docs/​src/​content/​docs/​reference/​cli/​deny.md Explains retirement on the next install.
CHANGELOG.md Records hook cleanup and denial messaging changes.

Review effort: Balanced

Comment thread src/apm_cli/install/drift.py Resolved
Comment thread src/apm_cli/install/exec_gate.py Resolved
Comment thread src/apm_cli/install/exec_gate.py Resolved
…y selection in replay

Folds Copilot review on #3944:
- A shared hook config that replay no longer produces but that still holds
  APM-owned (non-local-bundle) entries is reported as drift, so a denied
  hook left registered fails apm audit --ci.
- The replayed executables gate uses the audit's --no-policy / --policy
  selection instead of discovering policy on its own.

Refs #3924

apm-spec-waiver: audit replay executables gate follows CLI policy flags; no OpenAPM requirement

Co-authored-by: Copilot App <[email protected]>
…partial denies

Folds the lean panel on #3944:
- apm audit --ci discovers org policy once and hands it to the drift
  replay through a provider; replay builds the gate via
  effective_exec_map_for_project without migrating user consent.
- apm deny tells the user to run apm install to remove executables that
  are already deployed.
- Lifecycle coverage for cursor, codex, gemini and windsurf merge configs;
  partial-deny parking tests; audit plumbing guard tokens.
- Update prepare_ci_audit_replay fakes for the new keyword.

Refs #3924

apm-spec-waiver: audit replay executables gate follows CLI policy flags; no OpenAPM requirement

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

APM Review Panel: ship_with_followups

Closes #3924: denying or revoking a package now removes its hooks from merged harness configs on the next install, and audit replay honors the gate for lock entries recorded as gated or denied.

panel-mode=lean; personas=supply-chain-security-expert,test-coverage-expert,python-architect,apm-ceo

cc Sergio Sisternes (@sergio-sisternes-epam) -- a fresh advisory pass is ready for your review.

Before this PR, apm deny only stopped new hooks from being added. The package's existing entries stayed in .claude/settings.json and the other merged configs and kept firing, and audit replay ignored the gate. This PR closes that gap using code the project already trusts. Install removes gated hooks through the existing source-marker retirement (reconcile_package_target_restriction -> _retire_source_markers), and both gates build their allow maps through the executable-trust owner. All three panelists agree the owner calls are correct. Security found the install-side fix sound because APM assigns the markers and a package cannot fake them. The strongest evidence is the installed-CLI lifecycle test. It covers claude-deny, claude-revoke, copilot-deny, cursor-deny, codex-deny, gemini-deny and windsurf-deny, and each merge-config case also checks that restoring the pre-deny config makes apm audit --ci exit 1.

After the panel, the author folded every finding that fits the scope of #3924:

  • One org-policy fetch per audit run, passed to replay through a policy_provider.
  • No user-config migration during audit.
  • An apm deny hint to run apm install.
  • Partial-deny parking tests.
  • A seven-harness lifecycle matrix.
  • Guard tokens with mutation cases for the audit-to-replay plumbing.

The remaining risk is narrow. Audit still trusts the lock's own exec_status to decide whether to apply the gate. A deny committed to apm.yml without a re-install, or a hand-edited lock, therefore still passes audit. Hook ownership also relies on an editable sidecar. Both need a new audit check or trust model, which goes beyond #3924's "after deny and install" done-when. They are deferred and should be tracked.

Dissent. No panelist disputed another's severity. python-architect and the declined Copilot thread accept the aggregate exec_status contract for now; security points at the same seam. The panel favors deferral, provided the per-type lock evidence and the stale-lock audit check are tracked together.

Aligned with:

  • Secure by default: deny and revoke now remove hooks that are already deployed.
  • Governed by policy: replay applies the deny-wins map with the audit's single policy fetch.
  • Multi-harness: retirement is checked across claude, copilot, cursor, codex, gemini and windsurf.
  • Pragmatic: apm deny tells the user the one command to run.

Panel summary

Persona B R N Takeaway
Supply Chain Security Expert 0 3 0 Install-side retirement is sound: markers are APM-assigned, so a package cannot spoof them. Audit still trusts the lock's own exec_status, so a stale or edited lock hides a denied hook.
Test Coverage Expert 0 3 0 Deny/revoke retirement is proven by an installed-CLI lifecycle test. Gaps: the partial-deny parking split, the cursor/codex/gemini merge configs, and how audit threads its policy flags into replay.
Python Architect 0 3 0 The owner calls are correct: hooks retire through _retire_source_markers and both gates build their maps through the trust owner. Audit replay fetched org policy a second time and re-checks trust only for gated lock entries.

B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.

Top 3 follow-ups

  1. [Supply Chain Security Expert] Add an audit/CI check that compares each lock entry's exec_status with the committed project and org deny layers. -- Today a deny committed without a re-install passes apm audit --ci.
  2. [Supply Chain Security Expert] Make hook ownership fail closed on a missing, unreadable or relabelled sidecar. -- Deleting or relabelling the sidecar can hide denied hooks from drift detection.
  3. [Python Architect] Record per-type withheld-exec evidence in the lockfile. -- Replay would stop keying its re-check on the aggregate exec_status, matching fix(install): --frozen applies the executables gate and MCP omission evidence #3938; plan it together with item 1.

Recommendation

Merge once CI is green on head 7dfb5d6. All in-scope findings are folded and the seven-harness lifecycle test passes. Track the stale-lock audit check (highest priority), sidecar fail-closed ownership, and per-type lock evidence as follow-ups.

Folded in this run

  • (copilot) Flag project-only shared hook configs that still hold APM-owned entries -- resolved in abccb03.
  • (copilot) Replay honors the audit's policy selection -- resolved in abccb03.
  • (panel) [python-architect] One policy fetch per audit via policy_provider; no user-config migration; dead APMPackage load removed -- resolved in f5a6730.
  • (panel) [supply-chain-security-expert] apm deny prints a hint to run apm install -- resolved in f5a6730.
  • (panel) [test-coverage-expert] Partial-deny parking tests with a real ExecTrustContext -- resolved in f5a6730.
  • (panel) [test-coverage-expert] Lifecycle rows for cursor, codex, gemini and windsurf, including the leftover-audit failure -- resolved in f5a6730.
  • (panel) [test-coverage-expert] Guard tokens and mutation cases for the audit-to-replay plumbing -- resolved in 7dfb5d6.

Copilot signals reviewed

  • src/apm_cli/install/drift.py:720 -- LEGIT: replay emitting no shared hook config hid leftover denied hooks (resolved in abccb03).
  • src/apm_cli/install/exec_gate.py:289 -- LEGIT: replay ignored the audit's --no-policy/--policy (resolved in abccb03, refined in f5a6730).
  • src/apm_cli/install/exec_gate.py:282 -- NOT-LEGIT for this PR: replay mirrors install on the auditing machine; per-type lock evidence needs a lockfile schema change (deferred).

Deferred (out-of-scope follow-ups)

  • (panel) [supply-chain-security-expert] Audit check comparing lock exec_status with committed deny layers -- scope boundary: new audit check beyond [BUG] apm deny + reinstall leaves the package's already-deployed hooks registered #3924 done-when (after deny AND install).
  • (panel) [supply-chain-security-expert] Hook ownership fails closed on a missing or tampered sidecar -- scope boundary: hook ownership trust model, not executables-gate retirement.
  • (panel) [python-architect] Per-type withheld-exec evidence in the lockfile -- scope boundary: lockfile schema change.

Regression-trap evidence (mutation-break gate)

  • tests/integration/test_executables_deny_retires_hooks_lifecycle.py -- ran on unfixed main 0e19cce; 3 of 3 rows FAILED as expected.
  • test_gating_a_package_retires_its_deployed_hooks[claude-deny] -- deleted the drift leftover-hook branch; test FAILED as expected; guard restored.
  • test_hard_denied_types_do_not_hide_approvable_ones -- deleted the per-type parking check; test FAILED as expected; guard restored.

Lint contract

uvx [email protected] check and format --check (CI paths), pylint R0801, lint-auth-signals.sh, lint-architecture-boundaries.sh, and the file-length and relative_to guards all ran clean on HEAD merged with main.

CI

gh pr checks 3944 at 7dfb5d6: 31 pass, 3 skipping, 0 failing (after 1 CI fix iteration: Build & Test Shard 1 fakes).

Mergeability status

PR head SHA CEO stance iters folds defers Copilot rounds CI mergeable mergeStateStatus notes
#3944 7dfb5d6 ship_with_followups 2 7 3 1 green MERGEABLE BLOCKED pending required review

Full per-persona findings

Supply Chain Security Expert

  • [recommended] Audit replay decides whether to apply the gate from the lock's exec_status, so a stale or hand-edited lock masks a committed deny. At src/apm_cli/install/exec_gate.py
    The record being audited decides whether the audit enforces the deny. A deny added to apm.yml (or by org policy) without re-install, or exec_status edited to deployed, passes audit. Not a regression versus main. (Deferred.)
  • [recommended] apm deny does not say deployed hooks keep firing until the next apm install. At src/apm_cli/commands/approve.py:257
    (Folded: deny now prints the hint.)
  • [recommended] Hook ownership comes only from a committed, editable sidecar, so deleting or relabelling it hides a denied package's hooks from retirement and audit. At src/apm_cli/integration/hook_ownership.py:247
    Package content cannot spoof markers. The gap is project-side tampering, and unreadable JSON fails open. (Deferred.)

Test Coverage Expert

  • [recommended] No regression trap for a package with hooks denied but another type approvable. At src/apm_cli/install/exec_gate.py:89
    (Folded: partial-deny tests with a real ExecTrustContext, mutation-proven.)
  • [recommended] Lifecycle covered only claude (merge config) and copilot (per-file). At tests/integration/test_executables_deny_retires_hooks_lifecycle.py:92
    (Folded: cursor, codex, gemini and windsurf rows added.)
  • [recommended] No proof that audit --ci --policy/--no-policy reaches the replay gate. At src/apm_cli/commands/audit.py:629
    (Folded: guard tokens and mutation cases on audit.py, audit_replay.py, ci_checks.py and drift.py.)

Python Architect

  • [recommended] Audit replay discovered org policy separately from the audit's own fetch. At src/apm_cli/install/exec_gate.py:292
    (Folded: single cached fetch passed as policy_provider.)
  • [recommended] Replay re-checks trust only for lock entries recorded as gated or denied, which is a mixed contract versus fix(install): --frozen applies the executables gate and MCP omission evidence #3938. At src/apm_cli/install/exec_gate.py:280
    (Contract documented in the docstring; per-type lock evidence deferred.)
  • [recommended] ReplayExecAllowMap parsed APMPackage for nothing and could migrate user config during audit; the guard token names a local alias. At src/apm_cli/install/exec_gate.py:289
    (Folded: owner called directly with migrate_user_legacy=False.)

This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.


Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 0bb0886 into main Nov 19, 2026
35 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-fix-3924-deny-retires-hooks branch November 19, 2026 17:23
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
…cell

Deny wins (#3944): frozen retires a gated hook through ordinary cleanup and,
because it may not rewrite apm.lock.yaml, now exits 1 naming the missing
file. I7 (a failed command preserves state) therefore fires on exactly the
cell gate-cell-c-lock already tracks; record it the same way so the GATE
spine stays strict.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] apm deny + reinstall leaves the package's already-deployed hooks registered

2 participants