Skip to content

[0.38] fix(install): restore every deployed file after a failed or interrupted install (#4282) - #4612

Merged
Daniel Meppiel (danielmeppiel) merged 14 commits into
mainfrom
danielmeppiel-p2-4282-lazy-snapshot
Nov 21, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 14 commits into
mainfrom
danielmeppiel-p2-4282-lazy-snapshot

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

A failed or interrupted (SIGINT, SIGTERM) apm install with the install-time audit off left deployed files that apm.lock.yaml did not record. Only an audit-gated install took a deploy snapshot, and it copied every path integration could write.

Every install now journals lazily at one integrator write seam:

  • src/apm_cli/integration/write_seam.py is the only way an integrator writes, replaces or removes a deploy path. Each writer records the path first. Ledger, gitignore and skill-path migration writes call it too.
  • install/deploy_snapshot.py is a lazy DeployJournal under ~/.apm/install-snapshots/deploy-*. It keeps the prior bytes, or notes the absence, of a path the first time it is written. Paths the install never touches cost nothing.
  • InstallTransaction (owner install-commit-order) owns journal_deploy_writes, release_deploy_targets and rollback. Rollback restores every journaled path, removes target roots the attempt created and left empty, and discards the staged lockfile (lockfile_write_authority.discard_staged_lockfile_writes). After release, the journal settles when the staged lockfile commits.
  • The next converging install reaps a journal a killed install left behind (under the lifecycle lock); a journal whose restore failed is kept as retained-*.
  • New architecture guard install-deployment-deploy-write-seam rejects a raw file write in src/apm_cli/integration/ that bypasses the seam, with a sorted MutationCase and parametrized mutation cells. install-deployment-cleanup-target-roots (landed on main after this branch forked) allowlists only the seam's own rmdir wrapper; every caller is still judged at its call site.

Failures after the lockfile commits (for example an MCP connection collision) keep the 0.37 behavior: record what reached disk.

apm-spec-waiver: restores pre-command bytes under the existing req-pl-002 failing-check contract; no new OpenAPM-observable surface.

Issue and approved scope

Issue: Fixes #4282

Human scope-approval comment: #4282 (comment)

Does this PR complete the issue, or what remains? It completes the approved scope. Refs #4594: this also flips the lockfile-write row of tests/lifecycle/scenarios/test_failure_phase.py (its _UNTRACKED_SKILL / _DEPLOY_CHANGED #4164 cells are removed) and retires the ledger's late-lock-write-atomicity known gap; the mcp-config-write row (failure after the lock commits) stays with #4594, sequenced after this PR. Out of scope, as approved: refusal ordering, SIGKILL or power-loss recovery, uninstall/prune/update rollback, snapshotting paths the install does not write.

Type of change

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

Testing

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

Done-when cells in tests/integration/test_lockfile_commit_point_lifecycle.py:

  • test_sigterm_without_a_deploy_snapshot_keeps_the_lock_equal_to_disk and test_the_next_install_reaps_a_killed_installs_deploy_snapshot: install: lazy pre-write snapshot at the integrator write chokepoint #4282 strict-xfail markers removed, now pass.
  • New installed-CLI cell test_a_failure_after_deploy_leaves_every_byte_as_it_was injects APM_TEST_FAIL_LOCK_REPLACE after deploy and asserts deployed files, apm.yml, receipts and apm.lock.yaml are byte-identical to pre-command, with no journal left behind.
  • Rewritten to the restore contract: test_a_failed_lockfile_write_restores_deploys_and_the_lock (was ..._is_reported_by_audit_and_converged) and test_sigterm_without_an_audit_restores_deploys_and_the_lock (was test_sigterm_without_a_deploy_snapshot_is_reported_by_audit_and_converged).
  • Controls unchanged and green: test_interrupt_at_the_commit_point_restores_deploys_and_discards_the_lock (SIGINT, SIGTERM) and test_a_killed_install_is_reported_by_audit_and_converged_by_the_next_install (SIGKILL backstop).

Validation evidence (local, macOS, merge of main 0f9b391):

Command Result
pytest tests/integration/test_lockfile_commit_point_lifecycle.py (installed CLI, APM_BINARY_PATH=.venv/bin/apm) 14 passed, 1 skipped (inotify, Linux only), 1 xfailed (#4268)
Same 5 key cells with src/ from main 5337f2f 5 failed (hash-drift / modified files left on disk): red on main
pytest tests/integration/test_architecture_owner_rule_mutations.py tests/unit/scripts/test_architecture_runner.py tests/unit/install/test_deploy_snapshot.py tests/unit/install/test_failed_install_residue.py tests/unit/install/test_pipeline_ownership_snapshot.py 1378 passed
python scripts/lint_architecture_boundaries.py --root . exit 0
pytest tests/integration/test_required_lifecycle_state_machine.py::test_required_failed_lock_write_restores_state_and_recovers tests/lifecycle/scenarios/test_failure_phase.py 10 passed, 1 skipped
pytest tests/quality/test_lifecycle_bug_ledger.py tests/unit/scripts/test_lifecycle_evidence.py 220 passed
GHA PR checks at head 1dca1d2 38 passed, 0 failed
ci-integration.yml dispatch at 1dca1d2 all shards green except shard 4 (Codex runtime fetch from GitHub API, infra; rerun)
uvx [email protected] check / format --check (CI paths) clean
pylint --enable=R0801 --min-similarity-lines=10 10.00/10
bash scripts/lint-auth-signals.sh clean
python scripts/check_complexity_ratchet.py --base-ref <main> clean
python -m tests.quality.taxonomy --changed clean

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.

)

Every install now records, at integration/write_seam.py, the prior bytes
(or absence) of each path the first time an integrator writes it.
InstallTransaction owns record, rollback and release: a rollback restores
the journaled paths and discards the staged lockfile; after the audit the
journal settles once the staged lockfile reaches disk. The next install
reaps a journal a killed install left. A registered architecture guard
(install-deployment-deploy-write-seam) rejects integrator writes that
bypass the seam.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
… guard (#4282)

The cleanup-target-roots guard landed on main after this branch forked.
Callers of write_seam.rmdir are still judged at their own call sites.

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

apm-spec-waiver: restores pre-command bytes under the existing req-pl-002 failing-check contract; no new OpenAPM-observable surface.

Co-authored-by: Copilot App <[email protected]>
Keeps deployment_ledger.py under the 2100-line cap after merging main.

Co-authored-by: Copilot App <[email protected]>
…ine and failure-phase cells (#4282)

The lockfile-write fault row drops its #4164 expectations, the required
state-machine cell asserts the restored state, and the ledger retires the
late-lock-write-atomicity known gap.

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

R4 exits 1 before its lockfile commits, so references/a.md keeps its prior
bytes instead of an unrecorded v2.

Co-authored-by: Copilot App <[email protected]>
Ports the 4282 bug row and the retired late-lock-write-atomicity gap to the
per-row ledger layout (#4583).

Co-authored-by: Copilot App <[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 critical transaction-ordering and recovery-retention flaws can leave lockfiles inconsistent with deployed files, eliminate rollback protection, or destroy the only recovery backup.

6 open issues
What changed in this PR

This PR addresses #4282 by replacing audit-only deployment snapshots with lazy journaling for every install. It aims to restore pre-command deployment state on failures or interrupts before the lockfile commits, while preserving existing post-commit behavior.

Changes:

  • Routes deployment writes through a shared seam that records each touched path before modification.
  • Connects journal restoration, cleanup, and staged-lockfile disposal to the install transaction.
  • Adds architecture enforcement and updates regression tests, lifecycle evidence, and documentation.
File Description
tests/​unit/​scripts/​test_architecture_runner.py Registers the new guard in the expected inventory.
tests/​unit/​install/​test_pipeline_ownership_snapshot.py Updates failure expectations for deployment restoration.
tests/​unit/​install/​test_failed_install_residue.py Checks restored files and absence of residue.
tests/​unit/​install/​test_deploy_snapshot.py Tests lazy recording, rollback, settlement, and reaping.
tests/​lifecycle/​scenarios/​test_failure_phase.py Removes the known lock-write failure exception.
tests/​integration/​test_shared_skill_preservation_lifecycle.py Expects failed shared-skill writes to roll back.
tests/​integration/​test_required_lifecycle_state_machine.py Requires unchanged state after lock-write failure.
tests/​integration/​test_lockfile_commit_point_lifecycle.py Expands failure, interrupt, and recovery coverage.
tests/​integration/​test_checkout_switch_skill_lifecycle.py Updates failed checkout-switch restoration expectations.
tests/​integration/​test_architecture_owner_rule_mutations.py Adds journal ownership and write-seam mutations.
tests/​fixtures/​lifecycle_bug_ledger/​known_gaps/​late-lock-write-atomicity.json Removes the retired known-gap record.
tests/​fixtures/​lifecycle_bug_ledger/​bugs/​4282.json Records the bug and linked regression tests.
src/​apm_cli/​integration/​write_seam.py Introduces shared pre-write recording wrappers.
src/​apm_cli/​integration/​targets.py Routes target-directory creation through the seam.
src/​apm_cli/​integration/​skill_transformer.py Routes transformed-agent writes through the seam.
src/​apm_cli/​integration/​skill_integrator.py Routes skill directory creation and copies through the seam.
src/​apm_cli/​integration/​skill_adoption.py Marks private scratch cleanup as exempt.
src/​apm_cli/​integration/​prompt_integrator.py Routes prompt deployment through the seam.
src/​apm_cli/​integration/​mcp_integrator.py Imports configuration writers from the seam.
src/​apm_cli/​integration/​lsp_integrator.py Routes configuration writes and cleanup through the seam.
src/​apm_cli/​integration/​kiro_hook_integrator.py Routes Kiro hook deployment through the seam.
src/​apm_cli/​integration/​instruction_integrator.py Routes instruction deployment through the seam.
src/​apm_cli/​integration/​hook_ownership.py Journals hook ownership sidecar changes.
src/​apm_cli/​integration/​hook_integrator.py Routes hook writes and removals through the seam.
src/​apm_cli/​integration/​hook_bundle.py Journals deployed hook bundle files.
src/​apm_cli/​integration/​command_integrator.py Routes command-directory creation through the seam.
src/​apm_cli/​integration/​cleanup.py Journals stale-file removals and renames.
src/​apm_cli/​integration/​canvas_integrator.py Journals canvas bundle copies.
src/​apm_cli/​integration/​base_integrator.py Connects shared deployment and cleanup operations to journaling.
src/​apm_cli/​integration/​agent_integrator.py Routes agent deployment through the seam.
src/​apm_cli/​integration/​_hook_dropped_targets.py Journals orphaned hook sidecar removal.
src/​apm_cli/​install/​transaction.py Owns journal activation, settlement, and rollback.
src/​apm_cli/​install/​skill_path_migration.py Records migration removals before modification.
src/​apm_cli/​install/​pipeline.py Starts journaling before integration.
src/​apm_cli/​install/​phases/​audit.py Updates comments to describe journal-backed restoration.
src/​apm_cli/​install/​helpers/​gitignore.py Routes gitignore appends through the seam.
src/​apm_cli/​install/​failure_residue.py Updates documentation of rollback boundaries.
src/​apm_cli/​install/​deploy_snapshot.py Replaces eager snapshots with lazy deployment journals.
src/​apm_cli/​install/​commit_gate.py Documents the pre-lockfile restoration boundary.
src/​apm_cli/​deps/​lockfile_write_authority.py Adds explicit disposal of staged lockfile writes.
src/​apm_cli/​core/​deployment_ledger.py Journals deployment evidence persistence.
scripts/​architecture_linter/​checks/​install_entry_and_commit.py Updates transaction ownership checks for journaling.
scripts/​architecture_linter/​checks/​install_deployment_analyzers.py Registers the deployment write-seam guard.
scripts/​architecture_linter/​checks/​install_deploy_write_seam.py Adds checks for writes that bypass journaling.
scripts/​architecture_linter/​checks/​install_cleanup_target_roots.py Allows the seam’s directory-removal wrapper.
docs/​src/​content/​docs/​reference/​cli/​install.md Documents restoration and post-commit behavior.
CHANGELOG.md Adds a release note for install restoration.
.apm/​architecture/​owners/​install-deployment.json Updates journal ownership and guard references.

Review effort: Balanced

Comment thread src/apm_cli/install/deploy_snapshot.py Resolved
Comment thread src/apm_cli/install/transaction.py Resolved
Comment thread src/apm_cli/install/transaction.py Resolved
Comment thread src/apm_cli/install/transaction.py Resolved
Comment thread src/apm_cli/install/deploy_snapshot.py Resolved
- A store kept after a failed restore carries a RETAINED marker the reaper
  honors, so its copies survive a failed rename.
- Writers that write through a symlink (append, in-place write, copy) also
  journal the link's destination.
- The journal settles first at the lockfile commit, before receipt binding
  or any other fallible post-commit action.
- A rollback with nothing deployed still discards the staged lockfile.
- The write-seam guard rejects Path.open in a writing mode and one-argument
  Path.replace, with mutation cells.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

install: lazy pre-write snapshot at the integrator write chokepoint

2 participants