Skip to content

fix: trust and integrity consistency hardening (bundle admission, update integrity) - #4157

Merged
Daniel Meppiel (danielmeppiel) merged 20 commits into
mainfrom
danielmeppiel-fictional-lamp
Nov 20, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 20 commits into
mainfrom
danielmeppiel-fictional-lamp

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

TL;DR. Trust and integrity consistency hardening. apm unpack now uses the same shared install owners as apm install <bundle>. A new bundle admission owner (install/bundle_admission.py) runs the org policy preflight and the require_hashes integrity decision for every bundle deploy entry point. apm unpack also holds the lifecycle lock, and it records the files it deploys when the output is an APM project. It also hardens apm update (#4125). When a registry dependency re-resolves to its locked version, apm update checks the archive against the locked resolved_hash and fails closed on a mismatch; a moved version re-locks.

Problem (WHY)

  • Bundle deploy decisions lived in two entry points; this moves them to one shared bundle-admission owner so apm install <bundle> and apm unpack behave the same.
  • Restore workflows (microsoft/apm-action restore mode, used by gh-aw) still run apm unpack <bundle> -o <dir>, so the command must stay and keep its surface.

Approach (WHAT)

Leg Owner apm install <bundle> apm unpack
Org policy (outcome routing, dependency allow/deny, target, registry source, fetch failure) admit_bundle -> run_policy_preflight unchanged, now via the owner new: bundle lock dependencies (with registry names) + pack target, policy and registries: resolved from the output dir; an unknown pack target or a file outside the pack target's paths is refused, and a bundle with no pack target is checked as all targets
Integrity under require_hashes admit_bundle + a per-format check pack.bundle_files (unchanged) per-file deployed_file_hashes: refused under require_hashes, otherwise a warning
Lifecycle lock serialized_lifecycle_unless("dry_run") unchanged new
Lockfile record DeploymentLedgerCodec.record_local_bundle_files unchanged new, at an APM project root only (see design note); the project lockfile is checked as writable before any file is written, and only files this unpack copied are recorded
Cross-target pack hashes enrich_lockfile_for_pack n/a deployed_file_hashes are projected onto mapped bundle paths

Executable content is still refused by the unpacker before admission runs (#4051 behavior, unchanged).

Implementation (HOW)

File Change
src/apm_cli/install/bundle_admission.py (new) admit_bundle (preflight + require_hashes decision), plugin_bundle_integrity (moved verbatim from the handler), apm_bundle_integrity, the unpack adapter apm_bundle_admission, and the record seam (unpack_record_root + record_unpacked_files).
src/apm_cli/install/local_bundle_handler.py The inline preflight and integrity block is replaced by one admit_bundle(...) call. Behavior is unchanged.
src/apm_cli/bundle/unpacker.py A required keyword admit callback (an UnpackCandidate) runs before the dry-run return and the first copy. UnpackResult.written_files lists the regular files written.
src/apm_cli/commands/pack.py unpack_cmd: @serialized_lifecycle_unless("dry_run"), InstallLogger, admission, the record seam, render_summary() (so warn-mode violations print), and a policy refusal exits 1.
scripts/architecture_linter/... New closed-world rule install-deployment-bundle-admission-owner. The existing install-deployment-local-bundle-policy-preflight rule is retargeted to the new owner file; require_hashes banned paths include it.
.apm/architecture/owners/install-deployment.json New owner record bundle-admission-owner.
tests Installed-CLI lifecycle tests, unit tests for admission ordering and apm-format integrity, the rule id registration, a sorted MutationCase, a bug-ledger row, and a complexity-baseline shrink (install_local_bundle no longer trips PLR0915).
docs reference/cli/unpack.md (policy, tracked output, lock, exit codes), apm-usage/commands.md, CHANGELOG.

#4125: apm update verifies unchanged registry versions

  • Consistency. A git dependency whose commit did not move is checked against its locked content hash during apm update; registry dependencies now get the same treatment through one shared download helper used by both registry download sites (phases/resolve.py callback and sources.py).
  • Fix at the owner. install/source_helpers.py, next to the git locked_commit_unchanged / update_accepts_new_content predicates (owner update-content-integrity), gains locked_version_unchanged and download_registry_package. Both download sites route through download_registry_package. For an unchanged version, a resolved_hash mismatch removes the download and raises the same Content hash mismatch error as the git path, naming the package, version, expected hash and actual hash. Lockfile and deployed files stay byte-identical.
  • Guard. Sibling rule install-deployment-update-registry-integrity (closed world: any other registry-resolver download_package caller in src/apm_cli/ fails). MutationCase of the same id. Ledger row: issue 4125.

Design note: record rule

  • Rule. If the output root has apm.yml, the deployed files go into <output>/apm.lock.yaml through DeploymentLedgerCodec.record_local_bundle_files, the same owner call apm install <bundle> uses. The ownership result for an overlapping path (owner . becomes [".", "local-bundle"] with active owner local-bundle) is therefore the owner's decision, not an unpack rule. Any other output dir writes no lockfile and prints [i] unpack output is not tracked: the output directory has no apm.yml. Unpack into an APM project to record the deployed files.
  • Seam. unpack_record_root() is the single decision point. Changing the rule (for example to "never record") is a one-line change there.
  • Why not an untracked sidecar. No existing reader of untracked state exists: apm audit content-integrity reads lock local_deployed_file_hashes, and the materialization receipts only support files the lock already claims. A sidecar would be write-only.
  • Restore repro (apm-action v1.10.0, bundles-file mode, as in .github/workflows/pr-review-panel.lock.yml). The bundle was packed from microsoft/apm#main (target copilot, --archive --format apm) and unpacked with apm unpack <bundle> -o <checkout> into a fresh clone of this repo, with tokens stripped as multibundle.ts does. On origin/main code the checkout is already dirty: 8 tracked .github/ files are overwritten by the bundle. With this PR, apm.lock.yaml is modified too. No later step in that workflow fails on a dirty tree: the only checkout runs before the restore, "Audit pre-agent workspace" is continue-on-error, safe outputs are add_comment only (no patch), and the detection job checks out fresh. The workflow also pins apm-version: 0.28.0. The action's working-directory defaults to . (the job checkout).

Trade-offs

  • unpack_bundle takes a required admit keyword rather than an optional one. No caller can skip admission. Unpacker mechanics tests use a small accepting helper (tests/utils/unpack_admission.py).
  • A hash mismatch without require_hashes is a warning, not a refusal. apm pack --format apm copies on-disk bytes, so a producer whose files drifted after install would otherwise break existing restores.
  • The pack target drives target policy. An unknown target falls back to an unrestricted decision rather than raising.
  • Shared preflight messages still say "Install blocked by org policy" and suggest editing apm.yml; the printed remediation hint names apm unpack correctly. Rewording the shared messages is left out of scope.

Benefits

  1. One owner decides policy and integrity for both bundle deploy entry points, enforced by a closed-world guard with a mutation case.
  2. A policy denial, an unverifiable policy under fetch_failure_default: block, or an integrity mismatch under require_hashes exits 1 with the output tree byte-identical.
  3. Unpacked files in an APM project are visible to apm audit --ci content-integrity and survive a later apm install.
  4. A concurrent apm unpack waits for other APM operations.

Issue and approved scope

Fixes #4080
Fixes #4125

Human scope-approval comments:

Relates to #4051.

This PR completes #4080 and #4125.

Type of change

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

Testing

  • Tested locally
  • All existing tests pass (see note on environment failures below)
  • Added tests for new functionality (if applicable)

Lifecycle evidence (installed CLI, APM_BINARY_PATH=.venv/bin/apm, module tests/integration/test_trust_integrity_consistency_lifecycle.py):

Promise (ledger property) Test Result
outcome.status_matches_state: a dependency the org policy denies is refused; the preview exits 0 and writes nothing test_unpack_applies_org_dependency_policy passed
outcome.status_matches_state: a bundle whose pack target the org target allow-list excludes is refused and the tree is unchanged test_unpack_applies_org_target_policy passed
outcome.status_matches_state: warn-mode violations print and the bundle deploys test_unpack_reports_warn_policy_violations_and_deploys passed
transaction.failed_command_preserves_state: under require_hashes an intact bundle deploys with recorded hashes; altered bytes are refused and the snapshot is unchanged test_unpack_refuses_altered_bytes_when_policy_requires_hashes passed
transaction.failed_command_preserves_state: an unavailable policy under block fails closed test_unpack_fails_closed_when_policy_is_unavailable_under_block passed
ownership: an APM project records the files beside its existing lock entries; a rerun changes no bytes; install keeps them; audit --ci sees altered bytes test_unpack_records_deployed_files_in_the_project_lockfile passed
unapproved executables stay out of a project snapshot test_unpack_into_a_project_keeps_unapproved_executables_out passed
a non-project output announces, writes no lockfile, and the printed hint records test_unpack_into_a_non_project_dir_announces_and_writes_no_lockfile passed
concurrent unpack waits for the lifecycle lock test_unpack_waits_for_the_lifecycle_lock passed
outcome.status_matches_state: an unknown pack target, a file outside the pack target, or no pack target under a Claude-only target policy is refused and the tree is unchanged test_unpack_refuses_files_its_target_policy_cannot_place[bogus,claude,None] passed
transaction.failed_command_preserves_state: a malformed, symlinked or newer-generator project lockfile is refused before any write test_unpack_refuses_an_unwritable_project_lockfile_before_writing[malformed,symlink,newer] passed
transaction.failed_command_preserves_state: a cross-target mapped file deploys under require_hashes; altered mapped bytes are refused test_unpack_verifies_cross_target_mapped_files_under_require_hashes passed

#4125 evidence (same module):

Promise Test Result
source.ref_cache_coherent: an unchanged registry version with different bytes exits 1, and the lockfile and tree are unchanged test_update_refuses_altered_bytes_for_an_unchanged_registry_version passed
outcome.status_matches_state: an unchanged version with intact bytes is accepted on a cold cache; a moved registry version re-locks and prints the new hash test_update_accepts_a_new_registry_version passed

Owner unit tests (tests/unit/test_unpacker.py): TestUnpackWrittenFiles::test_skipped_non_content_files_are_not_claimed, TestApmBundleAdmission::test_registry_dependency_keeps_its_configured_registry_name -- passed.

Guards: install-deployment-bundle-admission-owner (routes extended: admit -> prepare_unpack_record, _bundle_target_decision; prepare_unpack_record -> ensure_lockfile_writable; _bundle_target_decision -> pack_target_prefixes), install-deployment-update-registry-integrity. MutationCase install-deployment-bundle-admission-owner (replaces unpack's admission with a no-op) is caught by only that rule. Ledger rows: issues 4080 and 4125.

Commands and results (head 0b82daa, merged with current main)
APM_BINARY_PATH=$PWD/.venv/bin/apm APM_E2E_TESTS=1 uv run --extra dev pytest \
  tests/integration/test_trust_integrity_consistency_lifecycle.py tests/unit/test_unpacker.py \
  tests/unit/test_unpack_security.py tests/unit/test_packer.py \
  tests/unit/scripts/test_architecture_runner.py tests/quality/test_lifecycle_bug_ledger.py \
  tests/unit/bundle tests/unit/test_lockfile_enrichment.py tests/integration/test_unpack_destination_containment_lifecycle.py \
  tests/unit/install/test_install_local_bundle.py tests/integration/test_install_local_bundle_e2e.py -n 8
  -> 571 passed, 1 failed (test_install_local_bundle_e2e.py::TestLocalBundleHashFormatCrossFlow::
     test_recorded_hash_compares_equal_in_cleanup_provenance_check fails identically on origin/main:
     remove_stale_deployed_files() now requires `evidence`; unrelated to this change)
uv run --extra dev pytest tests/integration/test_architecture_owner_rule_mutations.py -n 8 \
  -k "bundle_admission or bundle-admission or update_content_integrity or update-content-integrity or update_registry or update-registry"
  -> 14 passed
uv run --extra dev python scripts/lint_architecture_boundaries.py --root . -> exit 0
uvx [email protected] check / format --check (src/ tests/ scripts/lint_architecture_boundaries.py
  scripts/architecture_linter/ scripts/windows_native_symlink_probe_entry.py) -> All checks passed; 2514 files already formatted
pylint --disable=all --enable=R0801 --min-similarity-lines=10 --fail-on=R0801 -> 10.00/10
bash scripts/lint-auth-signals.sh -> auth-signal lint clean
python scripts/check_complexity_ratchet.py --base-ref origin/main -> clean
YAML I/O, file-length (2100) and str(relative_to) guards from ci.yml -> no violations

apm-spec-waiver: CLI-side admission consistency for the existing unpack command; no OpenAPM format, manifest or registry contract change.

Changelog

  • One line in one section under ## [Unreleased] in CHANGELOG.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

Not applicable: no req-XXX behavior changes (see the waiver line above).

How to test

  1. APM_BINARY_PATH=$PWD/.venv/bin/apm uv run --extra dev pytest tests/integration/test_trust_integrity_consistency_lifecycle.py -k unpack -q
  2. In an APM project with an org policy that denies a dependency of a --format apm bundle, run apm unpack <bundle>: exit 1, nothing written. With --dry-run: exit 0 and a "Would be blocked by policy" line.
  3. Unpack the bundle into a directory without apm.yml: the [i] unpack output is not tracked line prints and no apm.lock.yaml appears.

Co-authored-by: Copilot [email protected]

Trust and integrity consistency hardening (#4080). A new bundle admission
owner (install/bundle_admission.py) runs the org policy preflight and the
require_hashes integrity decision for every bundle deploy entry point.
apm install <bundle> and apm unpack both route through it. apm unpack also
holds the lifecycle lock and records the files it deploys into an APM project
through DeploymentLedgerCodec.record_local_bundle_files. A non-project output
directory stays untracked and says so.

Guard: install-deployment-bundle-admission-owner, with a mutation case.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Trust and integrity consistency hardening. apm update now treats a
registry package as its name plus exact version: when the re-resolved
version equals the locked version, the downloaded archive must match the
locked resolved_hash, and a mismatch fails closed with exit 1, leaving
the lockfile and deployed files unchanged. A moved version re-locks and
reports its new hash.

Both registry download sites (the resolve callback and the fresh
materialization source) route through install/source_helpers.py
download_registry_package and its locked_version_unchanged predicate,
next to the git commit predicates of the update-content-integrity owner.
A closed-world architecture rule
(install-deployment-update-registry-integrity) refuses direct registry
downloads elsewhere, with one MutationCase. Installed-CLI regression
tests, a bug-ledger row and a CHANGELOG line are included.

Closes #4125

Co-authored-by: copilot-swe-agent[bot] <[email protected]>

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

Unresolved defects allow compilation-target policy bypasses, break cross-target hash verification, and overwrite project files before validating the destination lockfile.

5 open issues
What changed in this PR

Aligns legacy apm unpack deployments with the shared policy, integrity, locking, and ownership mechanisms used by bundle installation, addressing #4080.

Changes:

  • Centralizes bundle admission and adds policy and hash checks before unpacking.
  • Serializes unpack operations and records deployed files in APM project lockfiles.
  • Adds regression coverage, architecture guards, and updated command documentation.
File Description
tests/​utils/​unpack_admission.py Adds an accepting admission helper for mechanics tests.
tests/​unit/​test_unpacker.py Tests admission ordering and bundle hash checks.
tests/​unit/​test_unpack_security.py Adapts security tests to required admission.
tests/​unit/​test_packer.py Updates the pack/unpack test helper.
tests/​unit/​scripts/​test_architecture_runner.py Registers the new guard in the expected inventory.
tests/​unit/​bundle/​test_tar_windows_absolute_path.py Adapts archive path-safety coverage.
tests/​quality/​complexity_baseline.json Removes the resolved statement-count exemption.
tests/​integration/​test_wave4_pure_logic_coverage.py Updates unpack tests for required admission.
tests/​integration/​test_trust_integrity_consistency_lifecycle.py Adds policy, integrity, ownership, and locking scenarios.
tests/​integration/​test_architecture_owner_rule_mutations.py Adds an admission-bypass mutation case.
tests/​fixtures/​lifecycle_bug_ledger.json Records #4080 regression coverage.
src/​apm_cli/​install/​local_bundle_handler.py Routes bundle installation through shared admission.
src/​apm_cli/​install/​bundle_admission.py Introduces admission, integrity, and recording helpers.
src/​apm_cli/​commands/​pack.py Connects unpack to locking, admission, and recording.
src/​apm_cli/​bundle/​unpacker.py Requires admission and reports written files.
scripts/​architecture_linter/​checks/​install_trust_integrity.py Adds the bundle admission ownership guard.
scripts/​architecture_linter/​checks/​install_policy_gitlab_and_bundle.py Retargets policy checks to the shared owner.
scripts/​architecture_linter/​checks/​install_deployment_analyzers.py Registers the admission analyzer.
packages/​apm-guide/​.apm/​skills/​apm-usage/​commands.md Updates unpack usage guidance.
docs/​src/​content/​docs/​reference/​cli/​unpack.md Documents policy, tracking, locking, and exit behavior.
CHANGELOG.md Announces the unpack behavior changes.
.apm/​architecture/​owners/​install-deployment.json Registers the canonical bundle admission owner.

Review effort: Balanced

Comment thread src/apm_cli/commands/pack.py Resolved
Comment thread src/apm_cli/install/bundle_admission.py Resolved
Comment thread src/apm_cli/install/bundle_admission.py Resolved
Comment thread src/apm_cli/bundle/unpacker.py Resolved
Comment thread src/apm_cli/install/bundle_admission.py Resolved
apm-spec-waiver: CLI-side admission and update integrity consistency; no OpenAPM format, manifest or registry contract change.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…copy-validity

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…ntegrity lines

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Unpack keeps existing lock entries, applies the org target allow-list, and deploys an intact bundle under require_hashes; update accepts an unchanged registry version whose bytes match the lock.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…mapped hashes, copied files and registry names

Admission now evaluates the bundle pack target (unknown targets and files outside the target's paths are refused; a bundle without a target is checked as all targets), confirms the project lockfile can be updated before any write, and forwards registry names and the project registries map to the policy preflight. Pack projects deployed_file_hashes onto cross-target mapped paths. The unpacker reports only the files it copied.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added the full-integration Run the full integration suite (ci-integration.yml) on this PR before merge label Nov 20, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good architectural improvement, but there are still important edge cases around bundle admission and deployment integrity. In particular, unknown pack targets must fail closed, cross-target hash keys need correct remapping, and destination lockfile writability must be validated before copying any files. The registry preflight should also preserve configured registry names, and deployment records should include only files actually written. Recommendation: Request Changes until these cases are fixed and the required CI checks pass.

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

# Conflicts:
#	tests/fixtures/lifecycle_bug_ledger.json
#	tests/integration/test_trust_integrity_consistency_lifecycle.py
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 659c25a into main Nov 20, 2026
41 of 43 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-fictional-lamp branch November 20, 2026 13:05
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
…onal conformance recorder

unpack_cmd is serialized on main (#4157), so it leaves the pending list. The #4188 frozen hook-pin tip renders from frozen-hook-pins-stale. The root conftest loads the conformance recorder only when a req marker or collect-only run needs it, so bounded mutation candidates collect.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit to microsoft/apm-action that referenced this pull request Nov 21, 2026
…APM 0.37

Since APM 0.37, 'apm unpack' into an APM project records the files it deploys in the project's apm.lock.yaml (microsoft/apm#4157). That dirties a tracked lockfile after restore and breaks the gh-aw pull_request_target checkout of the PR ref (#26).

The action now snapshots apm.yml and apm.lock.yaml in the output directory before 'apm unpack' (single bundle and bundles-file) and puts them back byte for byte afterwards (in a finally, so a failed unpack restores them too); a file unpack created where none existed is removed. apm unpack still runs against the real project, so its project-level admission checks are unchanged.

Regression coverage:
- unit: extractBundle and restoreMultiBundles keep both files byte-identical when unpack rewrites them, remove a created lockfile, and restore on failure (fail without the fix);
- CI test-restore-clean-workspace: a simulated PR commit changes apm.lock.yaml, and after restore a branch-style 'git checkout <pr-sha>' must succeed (the path checkout '-- .' it already ran never fails on local changes).

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

Labels

full-integration Run the full integration suite (ci-integration.yml) on this PR before merge

Projects

None yet

3 participants