Repository navigation
fix(install): hash-check bundle compile staging so edits before apm compile are detected (#3718) - #3898
Conversation
…e are detected (#3718) Bundle installs for Codex, Gemini and OpenCode now record a content hash per staged instruction in local_staged_file_hashes, computed by the same compute_file_hash owner as deployed files. LockFile.staged_hash_drift is the single verifier: apm audit --ci content-integrity fails and apm compile warns on drift. Older lockfiles without staged hashes make no drift claim. apm-spec-waiver: local_staged_files and its hashes are APM lockfile extensions outside the OpenAPM v0.1 key list Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The verifier can hash files outside the project through symlinked ancestors, and watch-mode compilation bypasses hash-drift warnings.
2 open issues
What changed in this PR
This PR addresses #3718 by adding content hashes to the bundle compile staging introduced in #3717. It extends APM's integrity checks to detect staged instruction edits without treating those files as deployment claims.
Changes:
- Records staged hashes and preserves them through migration and declarative installs, removing them on prune.
- Adds staged-content drift reporting to audit and one-shot compilation.
- Updates lifecycle tests, architecture guards, and user documentation.
| File | Description |
|---|---|
| tests/unit/deps/test_lockfile_local_staging.py | Tests hash persistence, migration, retirement, and drift. |
| tests/integration/test_local_bundle_compile_only_targets_lifecycle.py | Covers edited staging and forced-reinstall recovery. |
| tests/integration/test_architecture_owner_rule_mutations.py | Adds mutations checking hash-owner enforcement. |
| src/apm_cli/policy/ci_checks.py | Reports staged drift in content-integrity checks. |
| src/apm_cli/install/services.py | Computes hashes for staged bundle files. |
| src/apm_cli/install/phases/lockfile.py | Preserves staged hashes during declarative installs. |
| src/apm_cli/install/local_bundle_handler.py | Passes staged hashes to the ledger. |
| src/apm_cli/deps/lockfile.py | Stores, verifies, and retires staged hashes. |
| src/apm_cli/core/deployment_ledger.py | Records staged hashes and migrates legacy hashes. |
| src/apm_cli/commands/compile/cli.py | Warns about drift before one-shot compilation. |
| scripts/architecture_linter/checks/install_bundle_compile_staging.py | Extends staging ownership guards to hash verification. |
| packages/apm-guide/.apm/skills/apm-usage/commands.md | Documents staged drift and recovery. |
| docs/src/content/docs/reference/lockfile-spec.md | Defines the staged-hash lockfile field. |
| docs/src/content/docs/reference/baseline-checks.md | Documents audit coverage and remediation. |
| docs/src/content/docs/consumer/deploy-a-bundle.md | Explains staged-content integrity checks. |
| CHANGELOG.md | Adds the fix announcement and repeats existing release entries. |
| .apm/architecture/owners/install-deployment.json | Updates ownership of staged hashing and verification. |
Review effort: Balanced
APM Review Panel:
|
| Persona | B | R | N | Takeaway |
|---|---|---|---|---|
| Python Architect | 0 | 3 | 0 | Owner placement is correct (staging-state owner owns the staged verifier). Tighten the static guard and paired-field writers so the single-owner claim is enforced. |
| Supply Chain Security Expert | 0 | 3 | 0 | Hash binding is sound and single-owner; warn-on-compile / fail-in-audit is defensible. One upgrade-path gap: a collision-skipped edited staged file gets no hash and is never verified. |
| Test Coverage Expert | 0 | 2 | 0 | Core drift lifecycle verified (19 passed). Gap: edit-then-plain-reinstall hash carry and declarative carry-forward are not trapped by any test. |
B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.
Top 5 follow-ups
- [Test Coverage Expert] [IN SCOPE] Extend the lifecycle test: install, edit a staged file, run a plain reinstall, then assert
apm audit --cistill reports staged hash-drift. -- Evidence missing (e2e): dropping the prior-hash carry at deployment_ledger.py:929 still passes all 19 PR tests. - [Test Coverage Expert + Python Architect] [IN SCOPE] After the declarative install, assert that
local_staged_file_hashessurvives. Route the carry-forward through one LockFile helper that copies both fields and enforces the subset invariant, or remove it. -- Evidence missing: deleting the carry-forward still passes. Dropping a hash anywhere silently turns off drift detection for that path. - [Supply Chain Security Expert] [IN SCOPE] When a staged file is skipped on collision, record the bundle digest as a fallback hash. A prior hash still wins. -- A staged file edited before upgrading and then skipped on collision stays unhashed. That is the Record content hashes for bundle compile staging so edits before
apm compileare detected #3718 scenario, still open on the upgrade path. - [Supply Chain Security Expert] [IN SCOPE, guarded] Report staged paths that have no hash, but only when
local_staged_file_hashesis non-empty. -- A clean post-Record content hashes for bundle compile staging so edits beforeapm compileare detected #3718 install cannot produce a mix of hashed and hashless staged paths. Today, deleting one hash line silently turns off the check for that path. - [Supply Chain Security Expert + Python Architect] [IN SCOPE] Replace the ad-hoc
..and leaf-symlink checks with containment underapm_modules(ensure_path_within), matching the install side. -- Today the two verifiers decide 'safe to verify' differently. A symlinkedapm_modules/<slug>would get its out-of-tree bytes hashed.
Recommendation
The accepted #3718 scope is delivered and nothing blocks. Fold follow-ups 1-3 before merge; 4-6 are small in-scope tightening for the same pass.
Full per-persona findings
Python Architect
- [recommended] Guard is presence-only: a second staged-hash verifier or writer elsewhere passes the linter at
scripts/architecture_linter/checks/install_bundle_compile_staging.py:104
check_bundle_compile_staging requires needles but never forbids parallel staged hashing/assignment outside the owner files, so 'only verifier / only writer' is convention not enforcement.
Suggested: Add a negative scan for local_staged_file_hashes assignments / staged hashing outside an allowlist, plus a mutation case. - [recommended] local_staged_files and local_staged_file_hashes are kept in lockstep by hand; phases/lockfile.py carry-forward is an unlisted writer at
src/apm_cli/install/phases/lockfile.py:693
Drift silently disables for any path whose hash is dropped by a copy site; only retire_local_staging enforces the subset invariant.
Suggested: Add LockFile.carry_local_staging_from(other) copying both fields with the subset invariant; route the builder through it and guard it. - [recommended] staged_hash_drift uses a weaker path-safety rule (leaf symlink only) than verify_deployed_file_hashes (any symlink component) at
src/apm_cli/deps/lockfile.py:1270
Two verifiers decide 'safe to verify' differently; if apm_modules/ is a symlink, audit hashes out-of-tree bytes (read-only, not exploitable).
Suggested: Share one containment helper (or ensure_path_within against project_root/apm_modules) between both verifiers.
Supply Chain Security Expert
- [recommended] Upgrade path: a staged file edited before upgrading and then skipped on collision is recorded with no hash, so the Record content hashes for bundle compile staging so edits before
apm compileare detected #3718 edit stays invisible atsrc/apm_cli/install/services.py:1127
In integrate_local_bundle the collision branch continues before staged_hashes is populated; with no prior hash (pre-Record content hashes for bundle compile staging so edits beforeapm compileare detected #3718 lockfile) the path stays in local_staged_files unhashed, staged_hash_drift skips it, audit passes and compile merges the edit silently. In scope: hash every staged file.
Suggested: On collision skip of a staged file, record the bundle digest as a fallback (prior hash still wins) so the edit surfaces as drift.
Proof (unknown):tests/unit/deps/test_lockfile_local_staging.py - [recommended] Hashless staged entries are silently skipped; deployed files have a missing-ownership fail-closed equivalent at
src/apm_cli/deps/lockfile.py:1265
A mixed state (some staged paths hashed, others not) cannot come from a clean post-Record content hashes for bundle compile staging so edits beforeapm compileare detected #3718 install; deleting a hash line silently disables the check (decision 19: lockfile hashes alone are never trusted).
Suggested: Report hashless staged paths only when local_staged_file_hashes is non-empty, with the same restage remedy.
Proof (unknown):tests/unit/deps/test_lockfile_local_staging.py - [recommended] Drift verifier uses an ad-hoc '..' check instead of the path_security containment guard at
src/apm_cli/deps/lockfile.py:1266
Install-side uses ensure_path_within(..., project_root/'apm_modules'); verify side should match.
Suggested: Use ensure_path_within(file_path, project_root/'apm_modules') and skip on PathTraversalError.
Test Coverage Expert
- [recommended] No regression trap for edit-then-plain-reinstall: dropping the prior staged-hash carry in record_local_bundle_files survives all PR tests at
src/apm_cli/core/deployment_ledger.py:929
Persona mutated deployment_ledger.py:929 (drop **lockfile.local_staged_file_hashes): PR suite 19 passed; only a temporary probe (install, edit, plain reinstall, audit exit 1) failed. Behavior is correct today but unguarded.
Suggested: Extend the lifecycle test with a plain reinstall after the edit asserting audit still reports staged hash-drift.
Proof (missing at):tests/integration/test_local_bundle_compile_only_targets_lifecycle.py - [recommended] Declarative-install carry-forward of local_staged_file_hashes is unasserted and the new LockfileBuilder line is not load-bearing at
tests/integration/test_local_bundle_compile_only_targets_lifecycle.py:136
Deleting install/phases/lockfile.py:694 carry-forward still passed with a temp hash assertion, so hashes survive via another path and nothing traps a regression.
Suggested: Assert local_staged_file_hashes after the declarative install; drop or justify the redundant line.
Proof (missing at):tests/integration/test_local_bundle_compile_only_targets_lifecycle.py
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.
Addresses panel follow-ups 1-3, 5 and 6 on #3898. A staged file skipped on collision is bound to the bundle digest when it has no prior hash, so an edit made before upgrading is still drift. Declarative installs carry staging records and hashes together through LockFile.carry_local_staging_from. staged_hash_drift now requires containment under apm_modules, and the bundle-compile-staging guard rejects staged-hash writes outside the lockfile and ledger owners. The lifecycle test covers plain reinstall after an edit and the upgrade path. apm-spec-waiver: local_staged_file_hashes is an APM lockfile extension outside the OpenAPM v0.1 key list Co-authored-by: Copilot App <[email protected]>
The staged-hash fold pushed install/services.py past the 1175-line budget in test_architecture_invariants. Compacts the new collision fallback and tightens the adjacent comments. No behavior change; the bundle-compile-staging guard needle follows the compacted line. Co-authored-by: Copilot App <[email protected]>
…aging Addresses Copilot review on #3898. apm compile --watch now reports staged hash drift before the initial compile and every recompile, through the same _report_staged_hash_drift reporter. staged_hash_drift skips staged files behind any symlinked component, matching compile discovery, which never follows links, so audit makes no claim on content compile will not read. The bundle-compile-staging guard requires both watch call sites. apm-spec-waiver: local_staged_file_hashes is an APM lockfile extension outside the OpenAPM v0.1 key list Co-authored-by: Copilot App <[email protected]>
…verything Addresses delta-panel follow-ups on #3898. An instructions-only bundle whose only staged file was skipped on collision never wrote the lockfile, so the fallback hash was lost and an old-lockfile edit stayed silent; the handler now persists when fallbacks exist. A new lifecycle test proves an upgrade over unedited staging keeps the install-time hash (no false drift) and the all-skipped fallback. The compile drift reporter moves to commands/compile/staging.py, shared by one-shot and watch compile, and the guard rejects non-owner writes to either staged field, including augmented and del writes. apm-spec-waiver: local_staged_file_hashes is an APM lockfile extension outside the OpenAPM v0.1 key list Co-authored-by: Copilot App <[email protected]>
APM Review Panel:
|
| Persona | B | R | N | Takeaway |
|---|---|---|---|---|
| Python Architect | 0 | 2 | 0 | All three prior findings are resolved. Small follow-ups: guard the paired field and augmented writes; move the shared drift reporter out of cli.py. |
| Supply Chain Security Expert | 0 | 2 | 0 | Folds (3) and the (2) deferral boundary are sound. Fold (1) missed instructions-only bundles where every file is skipped; reproduced silent audit pass. |
| Test Coverage Expert | 0 | 1 | 0 | Both prior findings are resolved by lifecycle assertions. Gap at d24cd08: prior-hash-over-fallback precedence was proven only at unit tier. |
B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.
Top 1 follow-ups
- [Supply Chain Security Expert] [DEFERRED, needs product decision] Report staged entries that have no hash (a non-failing count, shown only when
local_staged_file_hashesis non-empty), and decide whether a mix of hashed and hashless entries should eventually fail closed. -- A hashless entry is skipped without a message. It is out of scope here because it adds new user-facing output on legacy lockfiles, which the accepted scope promises make no drift claim.
Recommendation
Merge after the CODEOWNER review from Sergio Sisternes (@sergio-sisternes-epam), which is the only remaining gate: CI is green and the PR is mergeable. Every in-scope finding from both panels and from Copilot review is folded, and the tests behind the folds fail when the fixes are reverted. The deferred hashless-entry report needs a product decision.
Full per-persona findings
Python Architect
- [recommended] Negative writer scan covered only local_staged_file_hashes and missed augmented/del writes at
scripts/architecture_linter/checks/install_bundle_compile_staging.py
The subset invariant spans both fields; a non-owner local_staged_files writer or |= / del would pass the guard. No live writer exists today.
Suggested: Widen the regex to both fields plus |=, del, popitem, append/extend; add a mutation case. - [recommended] _report_staged_hash_drift is a private cli.py helper with call sites in two modules at
src/apm_cli/commands/compile/cli.py:885
Shared compile pre-flight depended on a private symbol of the click command module via lazy imports.
Suggested: Move to commands/compile/staging.py report_staged_hash_drift and import it at the top of both modules.
Supply Chain Security Expert
- [recommended] Fallback hash lost when every bundle file is collision-skipped (instructions-only bundle) at
src/apm_cli/install/local_bundle_handler.py:334
local_bundle_handler wrote the lockfile only if deployed or staged_instructions; an all-skipped reinstall dropped the fallback. Reproduced at d24cd08: '1 skipped', no hashes, audit --ci exit 0.
Suggested: Persist when fallbacks exist; add an instructions-only lifecycle case and a guard pin.
Proof (missing at):tests/integration/test_local_bundle_compile_only_targets_lifecycle.py - [recommended] Deferral of fail-closed on hashless entries is sound; a non-failing info count would surface them at
src/apm_cli/deps/lockfile.py:1281
A retired file has no bytes to bind, so failing closed needs a product decision; silent skip hides future writer bugs.
Suggested: Optional info line with a count of hashless staged entries; widen the write regex to |=, del, popitem.
Test Coverage Expert
- [recommended] Prior-hash-wins-over-bundle-fallback had no lifecycle case (newer bundle over unedited staging) at
tests/integration/test_local_bundle_compile_only_targets_lifecycle.py:203
If the fallback won, a plain v2 install over unedited v1 staging would make audit --ci report false hash-drift; lifecycle could not tell the two apart because pack() always shipped the same bytes.
Suggested: Repack with changed text, plain reinstall, assert hash unchanged and audit clean.
Proof (missing at):tests/integration/test_local_bundle_compile_only_targets_lifecycle.py
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.
Keep the one-line [Unreleased] block; add #3898 and #3905 one-line entries. Co-authored-by: Copilot App <[email protected]>

Description
Since #3717,
apm install <bundle>for Codex, Gemini and OpenCode stages bundle instructions underapm_modules/<slug>/.apm/instructions/and records them inlocal_staged_files, but without a content hash. If someone edited a staged file between install andapm compile, compile merged the edited text intoAGENTS.mdwith no warning, andapm audit --cipassed.This PR mirrors
local_deployed_file_hashesfor staged files:local_staged_file_hashes(path -> sha256:<hex>). It is hashed bycompute_file_hash, the same hash used for deployed files, and written byDeploymentLedgerCodec.record_local_bundle_files(..., staged_hashes=). A pre-#3705 staging claim keeps its install-time hash when it migrates. A path that this install did not restage keeps its prior hash.install/services.py->core/deployment_ledger.py->deps/lockfile.pyLockFile.staged_hash_drift(project_root)is the only verifier. Paths with no hash (older lockfiles), paths outside a staging root, paths containing.., and missing or symlinked files make no drift claim.deps/lockfile.pycontent-integrityfails withhash-drift: <path> (staged, expected=..., actual=...)and suggests'apm install <bundle> --force'to restage.policy/ci_checks.py[!] hash-drift: staged file <path> changed since install ...before compiling.commands/compile/cli.pyapm pruneretirement drops the hashes too. Declarative installs carry records and hashes together throughLockFile.carry_local_staging_from. A staged file that a reinstall skips on collision keeps its prior hash; if it has none (upgrading from an older lockfile), it is bound to the bundle's bytes, so a local edit still shows as drift.deps/lockfile.py,core/deployment_ledger.py(staged_fallback_hashes)Guard: the existing
install-deployment-bundle-compile-stagingrule now also requires the hash field, the verifier, the use ofcompute_file_hash, the collision fallback, the carry helper, and the audit and compile call sites. It also rejects anylocal_staged_file_hasheswrite outsidedeps/lockfile.pyandcore/deployment_ledger.py. A new test,test_staged_compile_input_drift_routes_through_lockfile_owner, adds 7 mutation cases that prove the rule catches regressions. The verifier only checks paths that stay insideapm_modules(viaensure_path_within). The owner registry text is updated.Advisory panel (lean): #3898 (comment). Follow-ups 1, 2, 3, 5 and 6 are folded in 1eadb7d. Follow-up 4 (fail closed on staged entries with no hash) is deferred: a newer bundle that drops a file leaves a legitimately unhashed entry that
--forcecannot fix, so failing on it would break the approved rule that older lockfiles make no drift claim.Before: on main at f8ef817, an edited staged file compiled into AGENTS.md silently, and
apm audit --cireported "All 10 check(s) passed".After:
apm compilewarns,apm audit --cifailscontent-integritywith1 staged file(s) with hash drift, andapm install <bundle> --forceclears it.Issue and approved scope
Issue: Fixes #3718
Human scope-approval comment: #3718 (comment)
This PR completes the issue. Out of scope (per the record): detecting unrecorded files added under a staging root, and blocking
apm compileon drift (it warns).Type of change
Testing
APM_BINARY_PATH=.venv/bin/apm pytest -p no:xdist tests/integration/test_local_bundle_compile_only_targets_lifecycle.py tests/integration/test_frozen_deployed_hash_lifecycle.pyat cd3aaearecord_local_bundle_filestest_staged_instruction_edits_are_reported_as_hash_driftfails; drop the prior-hash carry -> unittest_staged_hashes_carry_with_their_records_and_bundle_fallback_never_winsfails-k "hash_drift or edited", against unfixedsrc/test_staged_instruction_edits_are_reported_as_hash_drift, andedited-skippedmigration case), confirming the regression trappytest -p no:xdist tests/unit/deps/test_lockfile_local_staging.py tests/unit/deps/test_lockfile_consumer_contract.py tests/unit/deps/test_lockfile_field_properties.py/tests/unit/core/test_deployment_state.py tests/unit/install/test_install_local_bundle*.py/tests/unit/policy/test_ci_checks.py tests/unit/test_drift_detection.py tests/unit/install/test_frozen.pyat cd3aaeapytest -p no:xdist tests/unit/policy(first push)pytest -p no:xdist tests/integration/test_architecture_owner_rule_mutations.py -k "staged_compile_input or matrix or bundle-compile-staging"pytest -p no:xdist tests/unit/scripts/test_architecture_runner.pyuvx [email protected] check/format --check(CI paths)pylint R0801(CI args)bash scripts/lint-auth-signals.sh/bash scripts/lint-architecture-boundaries.shLint ran on HEAD merged with main at b3df642.
Spec conformance (OpenAPM v0.1)
CONFORMANCE.{md,json}regeneratedlocal_staged_filesis not one of the top-level keys that req-lk-001 lists, so its new siblinglocal_staged_file_hashesis also an APM lockfile extension and needs no spec amendment. The commit carries anapm-spec-waiver:trailer for Mode B.