Skip to content

fix: derive install expected state from the lock - #3900

Merged
Daniel Meppiel (danielmeppiel) merged 14 commits into
mainfrom
fix/expected-state-from-lock
Nov 19, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 14 commits into
mainfrom
fix/expected-state-from-lock

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

apm install worked out its expected state from facts local to the machine instead of from apm.yml, apm.lock.yaml and the locked sources. Three bugs had that cause, and this PR fixes it with one owner commit per decision:

  1. Cache validity (Re-download after a content-hash mismatch drops the per-dependency skills selection #3884). The resolver gate (drift.should_force_ref_recheck) compared only the manifest ref with the lock. A package copy left in apm_modules by an older lock was therefore read during resolution, and its outdated apm.yml widened a locked skills: subset. The first apm install --frozen then failed with dependency selections differ from apm.lock.yaml. Integrate later re-downloaded the copy, so a second identical run passed.
    • New owner drift.locked_copy_proof: git HEAD, then the .apm-pin marker naming the locked resolved_commit, then the recorded content_hash (used for rows without a commit or copies without a marker).
    • The resolver gate, the parallel pre-download skip and the integrate skip all consume this one verdict.
    • Guard: install-deployment-locked-copy-validity.
  2. Committed-file baseline ([BUG] Receipt-less install refuses skills whose links APM rewrites at deploy time #3883). On a fresh clone without deployment receipts, apm install after an apm.yml ref bump (the Renovate flow) refused committed skills with "no trustworthy local deployment baseline". The locked-source baseline from fix(update): adopt unchanged locked skills when receipts are missing #3638 only applied to apm update. integration/skill_adoption.py now supplies it whenever a lock entry exists and the commit being installed differs from it. Lockfile hashes are still never trusted, and hand-edited files still fail closed.
    • Guard: install-deployment-skill-adoption-baseline.
  3. Path independence (MCP servers from a Claude plugin are recorded with an absolute ${CLAUDE_PLUGIN_ROOT} path, so apm install --frozen fails in a clone at another path #3825). Claude plugin ${CLAUDE_PLUGIN_ROOT} MCP/LSP values were expanded to absolute machine paths, both in apm.lock.yaml and in committed native files. A teammate's clone at another path failed --frozen and audit --ci.
    • New owner deps/plugin_root_render.py: the lock keeps the placeholder, and each target renders a project-relative form where one is documented:
      • ${CLAUDE_PROJECT_DIR:-.} for the shared .mcp.json
      • ${workspaceFolder} for VS Code and Cursor
      • ${CLAUDE_PROJECT_DIR} for the Claude LSP plugin
    • Targets with no documented interpolation keep absolute paths, as does user scope.
    • Older lockfiles still replay. The next apm install rewrites mcp_configs / lsp_configs once.
    • Guard: hooks-integrations-plugin-root-render.

Every owner has a registry record, a static architecture guard, a sorted MutationCase and an _EXPECTED_RULE_ID_TEXT entry. Docs and packages/apm-guide are updated where behavior changed.

Measured cost (#3884). Warm apm install --frozen --verbose on a project with 6 git dependencies (40 files each), 5 runs each, main vs final head:

phase main 57ade48 this PR
resolve 0.028 - 0.086 s 0.032 - 0.058 s
download 0.063 - 0.089 s 0.067 - 0.089 s
integrate 1.71 - 2.03 s 1.58 - 2.23 s

The resolver gate adds one small .apm-pin JSON read per dependency. Deployment reuse still proves bytes with git HEAD or the content hash, exactly as on main, so there is no extra hashing and the cost is neutral.

Panel folds (lean panel, CEO ship_with_followups):

  • A readable .git HEAD decides on its own; the marker never outranks it.
  • The marker never proves bytes that are about to be deployed (pre-download and integrate need git HEAD or the verified content hash).
  • sync_markers_for_lockfile stamps only copies the owner proves, so a copy left by an older lock is never marked current.
  • Staleness needs contradicting evidence, so legacy unmarked, unhashed copies keep main's no-network replay (req-rs-015).
  • Unit tests for every owner branch, the integrate paths and the marker writer; each new trap was mutation-broken.

Copilot review (3 threads, all LEGIT, fixed in e14999e by the #3825 owner):

Issue and approved scope

Issue: Fixes #3884, Fixes #3883, Fixes #3825

Human scope-approval comment:

Does this PR complete the issue, or what remains? It completes all three; each reporter scenario has an installed-CLI test in tests/integration/test_lock_derived_expected_state_lifecycle.py. Not touched: #3701, #3720, the obra/superpowers hook filename-routing finding, and replacing user-added files inside apm_modules (APM owns that directory).

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, -p no:xdist, APM_BINARY_PATH=.venv/bin/apm):

Deferred (not filed, listed for the maintainer):

  • A2: drift.py cannot be a selector of the new owner record (unique-selector rule; it already belongs to transport-auth-platform), and the owner still uses the private _should_skip_redownload helper.
  • A3: reuse eligibility (_pd_reuse_allowed in download.py, copy_check in integrate.py) is still written twice and differs in update mode. That difference predates this PR.
  • S3: legacy_absolute_root accepts any normalized absolute prefix in a moved clone. That is needed to replay lockfiles written on another machine, and editing the lock already requires commit access.

Spec conformance (OpenAPM v0.1)

…#3884)

The resolver gate (drift.should_force_ref_recheck) compared only the
manifest ref with the lock, so a package copy left by an older lock was
read during resolution: its outdated apm.yml widened a locked skill
subset and the first `apm install --frozen` failed with "dependency
selections differ", while integrate later re-downloaded the copy and a
second run passed.

drift.locked_copy_proof now owns "does this copy hold the locked
content": git HEAD, then the .apm-pin marker naming the locked
resolved_commit, then the content-hash fallback (rows without a commit
or copies without a marker). The resolver gate, the parallel
pre-download skip and the integrate skip all consume it. Guarded by
install-deployment-locked-copy-validity.

apm-spec-waiver: cache-reuse correctness fix inside the existing resolver gate; no OpenAPM contract change

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

On a fresh clone without deployment receipts, `apm install` after an
apm.yml ref bump (the Renovate flow) refused every file of a changed skill
with "no trustworthy local deployment baseline", although the committed
files are byte-identical to what the currently locked version renders.
The locked-source baseline from #3638 was gated on `apm update` only.

skill_adoption now supplies that baseline whenever a lock entry exists
and the commit being installed (installing_commit) differs from it, and
skips the redundant download when they match. Lockfile hashes are still
never trusted and edited files still fail closed. A new
install-deployment-skill-adoption-baseline guard pins the owner.

apm-spec-waiver: receipt-less skill adoption is APM deployment-state behavior with no OpenAPM requirement
Co-authored-by: Copilot App <[email protected]>
…works in any clone

MCP and LSP servers from Claude plugins were recorded with the placeholder
expanded to the absolute apm_modules path, in apm.lock.yaml and in committed
native configs. A clone at another path failed `apm install --frozen` and
`apm audit --ci`, and an ordinary install rewrote committed files (#3825).

- APMPackage keeps the authored placeholder in MCP/LSP declarations and binds
  the package root (deps/plugin_root_render.py, new owner); the lock write,
  --frozen comparison, audit config-consistency and native receipts all use
  the portable form. The extraction-time expansion path is removed.
- Only target renders expand it: ${CLAUDE_PROJECT_DIR:-.} in the shared root
  .mcp.json (Claude Code, Copilot CLI), ${workspaceFolder} for Cursor and
  VS Code, ${CLAUDE_PROJECT_DIR} in the Claude LSP plugin; other targets and
  user scope keep the absolute path.
- A lock from an earlier release holding the expanded value is equivalent
  (same root, or another clone's root with the same install path), so
  upgrades replay --frozen; the next install rewrites it once, refreshing the
  native receipt.
- Owner registered with mutation_writes.plugin_root_render and a mutation case.

Closes #3825 scope: #3825 (comment)

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 07:11
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.37.0 milestone Nov 19, 2026

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

Active issues can cause legacy MCP ownership checks to block migration or cleanup and Agent Plugin exports to contain unresolved variables pointing to unbundled scripts.

3 open issues
What changed in this PR

This PR makes APM installation decisions depend on the manifest, lockfile, and locked sources, addressing stale package copies, receipt-less skill upgrades, and machine-specific plugin paths.

Changes:

  • Centralizes locked-copy validation across resolution, download, and integration.
  • Extends locked-source skill adoption to installs after manifest ref changes.
  • Preserves portable MCP/LSP declarations and renders paths per target, with regression tests, architecture guards, and documentation updates.
File Description
tests/​unit/​test_plugin_parser.py Expects preserved MCP placeholders.
tests/​unit/​scripts/​test_architecture_runner.py Registers new architecture rules.
tests/​unit/​integration/​test_skill_adoption.py Tests unchanged-commit download avoidance.
tests/​unit/​integration/​test_mcp_config_view.py Checks portable declaration equality.
tests/​unit/​install/​test_legacy_plugin_compat.py Checks placeholder preservation and root binding.
tests/​unit/​deps/​test_shared.py Verifies location-independent generated manifests.
tests/​unit/​deps/​test_plugin_root_render.py Tests target rendering and legacy equivalence.
tests/​unit/​deps/​test_plugin_parser_lsp.py Expects preserved LSP placeholders.
tests/​unit/​deps/​test_hook_package_admission.py Supplies locked-copy marker evidence.
tests/​unit/​deps/​test_apm_resolver_staging_paths.py Separates authored values from rendered paths.
tests/​integration/​test_resolution_staging_mcp_lifecycle.py Updates portable lock expectations.
tests/​integration/​test_lock_derived_expected_state_lifecycle.py Adds three installed-CLI regression scenarios.
tests/​integration/​test_deps_resolver_resolution.py Updates placeholder extraction coverage.
tests/​integration/​test_commands_auth_flow.py Removes obsolete substitution tests.
tests/​integration/​test_architecture_owner_rule_mutations.py Adds mutation cases for new guards.
src/​apm_cli/​models/​dependency/​mcp.py Adds nonserialized plugin-root context.
src/​apm_cli/​models/​dependency/​lsp.py Adds nonserialized plugin-root context.
src/​apm_cli/​models/​apm_package.py Preserves declarations and binds package roots.
src/​apm_cli/​integration/​skill_preflight.py Passes the installing commit into adoption.
src/​apm_cli/​integration/​skill_integrator.py Tracks the current package commit.
src/​apm_cli/​integration/​skill_adoption.py Enables locked-source baselines beyond updates.
src/​apm_cli/​integration/​mcp_integrator.py Carries roots into target rendering.
src/​apm_cli/​integration/​mcp_integrator_install.py Refreshes receipts after frozen migration.
src/​apm_cli/​integration/​mcp_config_view.py Recognizes legacy absolute declarations.
src/​apm_cli/​integration/​lsp_integrator.py Renders plugin roots per LSP target.
src/​apm_cli/​install/​phases/​integrate.py Uses shared locked-copy validation.
src/​apm_cli/​install/​phases/​download.py Uses shared validation before downloads.
src/​apm_cli/​install/​mcp/​ownership.py Binds roots during baseline comparison.
src/​apm_cli/​install/​mcp/​integration.py Normalizes legacy MCP baselines.
src/​apm_cli/​drift.py Owns locked-copy proof and stale detection.
src/​apm_cli/​deps/​plugin_root_render.py Centralizes root rendering and legacy equivalence.
src/​apm_cli/​deps/​plugin_parser.py Keeps MCP/LSP placeholders unexpanded.
src/​apm_cli/​deps/​_shared.py Removes the obsolete substitution option.
src/​apm_cli/​adapters/​client/​vscode.py Uses workspace-relative root rendering.
src/​apm_cli/​adapters/​client/​opencode.py Routes stdio reads through shared rendering.
src/​apm_cli/​adapters/​client/​kiro.py Routes stdio reads through shared rendering.
src/​apm_cli/​adapters/​client/​gemini.py Routes stdio reads through shared rendering.
src/​apm_cli/​adapters/​client/​cursor.py Uses workspace-relative root rendering.
src/​apm_cli/​adapters/​client/​copilot.py Routes stdio reads through shared rendering.
src/​apm_cli/​adapters/​client/​codex.py Routes stdio reads through shared rendering.
src/​apm_cli/​adapters/​client/​claude.py Declares the project-root reference.
src/​apm_cli/​adapters/​client/​base.py Adds shared rendering and finalization.
scripts/​architecture_linter/​checks/​mutation_mcp_target_and_scope.py Guards centralized plugin-root handling.
scripts/​architecture_linter/​checks/​install_skill_adoption_baseline.py Guards commit-based skill adoption.
scripts/​architecture_linter/​checks/​install_locked_copy_validity.py Guards shared locked-copy validation.
scripts/​architecture_linter/​checks/​install_deployment_analyzers.py Registers install architecture checks.
packages/​apm-guide/​.apm/​skills/​apm-usage/​package-authoring.md Documents portable plugin paths.
packages/​apm-guide/​.apm/​skills/​apm-usage/​commands.md Documents receipt-less ref-bump installs.
docs/​src/​content/​docs/​reference/​lockfile-spec.md Describes portable MCP lock declarations.
docs/​src/​content/​docs/​reference/​cli/​install.md Documents stale-copy refresh behavior.
docs/​src/​content/​docs/​consumer/​install-packages.md Explains expanded skill adoption.
docs/​src/​content/​docs/​consumer/​install-mcp-servers.md Documents target paths and legacy migration.
docs/​src/​content/​docs/​consumer/​install-lsp-servers.md Documents LSP root rendering.
docs/​src/​content/​docs/​consumer/​drift-and-secure-by-default.md Explains pre-resolution cache validation.
CHANGELOG.md Records the fixes and migration note.
.apm/​architecture/​owners/​install-deployment.json Registers validation and adoption ownership.
.apm/​architecture/​owners/​hooks-integrations.json Registers plugin-root rendering ownership.

Review effort: Balanced

Comment thread src/apm_cli/install/mcp/integration.py Resolved
Comment thread src/apm_cli/install/mcp/ownership.py Resolved
Comment thread src/apm_cli/models/apm_package.py Resolved
Folds the lean panel on #3900:
- a readable .git HEAD decides alone; the marker cannot outrank it
  (python-architect A1);
- for deployment reuse a marker counts only on rows whose content_hash
  is verified before the bytes are reused, so commit-only rows keep
  main's re-download (supply-chain S2, test-coverage T2);
- sync_markers_for_lockfile stamps only copies the copy-validity owner
  proves (or legacy unmarked, unhashed copies), so a copy left by an
  older lock is never marked as current (supply-chain S1, T3);
- staleness needs contradicting evidence (hash, marker or .git), so
  legacy unmarked, unhashed copies keep main's no-network replay
  (spec req-rs-015 frozen short-pin contract);
- unit coverage for every owner branch, the integrate marker paths and
  the marker writer (T1, T2), each mutation-broken.

Co-authored-by: Copilot App <[email protected]>
…lugin-root agent export

Review follow-ups for #3825 on #3900:

- Ownership keeps the recorded baseline. A lock written before #3825 holds
  ${CLAUDE_PLUGIN_ROOT} expanded; normalizing it to the portable declaration
  made an unfingerprinted Claude or Cursor entry look foreign, so install
  reported a collision instead of migrating it. The stored baseline now
  proves the old write, and the install rewrites it in the portable form.
- `apm install --frozen` replays a legacy-equivalent server at its recorded
  root (CurrentMcpConfigView.replay_recorded_roots), so native files keep
  matching their lock receipts and an immediate uninstall can remove them.
- The legacy match accepts only a normalized absolute root (no empty, "." or
  ".." segments), since --frozen now writes the recorded value.
- `apm pack --format agent-plugin` refuses an MCP server using
  ${CLAUDE_PLUGIN_ROOT}: Agent Plugins do not resolve it and the bundle does
  not include the plugin's files.

Co-authored-by: Copilot App <[email protected]>
The pre-download skip trusted a matching marker on a tampered copy and
left the re-download to integrate's sequential path, which classifies a
Claude plugin differently and rewrote package_type in apm.lock.yaml
(test_plugin_redownload_and_update_restamp_verify_on_fresh_clone).
Deployment reuse now needs the git HEAD or the verified content hash, as
on main; the marker only gates resolution freshness and staleness.

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

# Conflicts:
#	docs/src/content/docs/reference/lockfile-spec.md
Delta panel folds on #3900:
- locked_copy_proof never consults .apm-pin unless trust_marker=True, so
  a new deployment caller cannot trust the marker by omission; only
  locked_copy_is_stale (resolution freshness) opts in, and the guard
  rejects trust_marker=True in download/integrate (python-architect).
- sync_markers_for_lockfile now asks locked_copy_is_stale instead of a
  second legacy rule, so a checkout whose git HEAD contradicts the lock
  is never stamped (python-architect, supply-chain).
- Owner record text updated; unit traps for the .git staleness clause and
  the marker writer, each mutation-broken (test-coverage).

Co-authored-by: Copilot App <[email protected]>
A pre-#3825 lock records ${CLAUDE_PLUGIN_ROOT} expanded, and the moved-clone
match accepts any normalized absolute prefix ending in the install path. The
frozen replay rendered native MCP commands at that root, so lock text chose
the absolute path a command runs (for example a rebuilt, uncommitted
.cursor/mcp.json), and audit --ci accepted it.

- replay_recorded_roots no longer renders the recorded root. It marks the
  server so --frozen keeps a native entry that already equals the recorded
  render (no new path; its lock receipt stays the uninstall proof), and every
  other entry gets this project's own portable render.
- Ownership accepts an entry that is exactly this project's portable render
  of a recorded other-clone baseline (portable_rewrite_equivalent), so the
  portable entries a frozen install writes stay removable without a receipt.

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

Copy link
Copy Markdown
Collaborator Author

APM Review Panel: ship_with_followups

PR #3900 makes install work out its expected state from apm.yml, apm.lock.yaml and the locked sources, not from leftovers on the machine. Each of the three issue promises now has an installed-CLI test, and every finding from the delta pass is resolved.

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

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

A lean pass at d0fc57e and a delta pass at 3bb2fe2 were both checked against head 5f3ecbb.

The lean pass raised the new .apm-pin trust path as the PR's main risk, and the folds closed it:

  • A readable .git HEAD decides on its own.
  • The marker never proves bytes that are about to be deployed.
  • The marker writer asks the copy-validity owner before stamping.
  • A copy counts as stale only when evidence contradicts it.

The delta pass raised one blocking-severity finding: --frozen could write a plugin root taken from the lockfile into native MCP commands. It is fixed in 38fd17c. --frozen never writes a path that came from the lockfile, and an entry counts as APM-owned only when it exactly matches this project's portable form. Lifecycle tests cover the same path and a moved clone.

locked_copy_proof is now strict by default (trust_marker=False), and the architecture guard forbids opting in on deploy paths.

Dissent. None. The specialists converged on every finding.

Aligned with: Portable by manifest (the lock plus locked sources decide; moved clones replay byte-stable). Secure by default (strict proof by default, edited skills fail closed, no root from the lockfile is written). Multi-harness (per-target rendering). Pragmatic as npm (behaves like npm ci; performance is neutral against main).

Panel summary

Persona B R N Takeaway
Python Architect 0 2 0 Delta: A1 fixed; the second legacy rule and the permissive default were folded in 62cf924.
Test Coverage Expert 0 2 0 Delta: T1-T3 resolved; moved-clone legacy replay and .git staleness traps added.
Supply Chain Security Expert 1 1 0 Delta: S1/S2 fixed; the lock-sourced frozen root (blocking) is fixed in 38fd17c.

B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.
Counts are from the delta pass at 3bb2fe2. Every finding listed is resolved at 5f3ecbb.

Top 3 follow-ups

  1. [Supply Chain Security Expert] S3 residual: legacy_absolute_equivalent accepts any normalized absolute prefix in a moved clone. -- After the D-S1 fix this affects only equivalence and ownership checks, not what gets written. It is the last path heuristic sourced from the lockfile.
  2. [Python Architect] A3: decide reuse eligibility in one place (download.py and integrate.py still each encode it). -- That update-mode difference predates this PR.
  3. [Python Architect] A2: make _should_skip_redownload public next to the owner, and reconcile the owner record with its selectors. -- Cosmetic, but it improves owner discoverability.

Folded in this run

Copilot signals reviewed

Deferred (out-of-scope follow-ups)

Regression-trap evidence (mutation-break gate)

  • tests/integration/test_lock_derived_expected_state_lifecycle.py::test_stale_module_copy_keeps_locked_skill_subset_on_first_frozen_install -- deleted the stale-copy gate (src revert); the test FAILED as expected; guard restored.
  • tests/unit/test_drift_detection.py::test_locked_copy_proof_branches, ::test_locked_copy_proof_lets_a_readable_git_head_decide, ::test_pin_never_proves_bytes_for_deployment -- deleted the gate call, the readable-HEAD rule and the trust_marker default in turn; each FAILED; guards restored.
  • tests/unit/install/test_integrate_phase3w5.py::test_marker_proven_copy_still_needs_verified_bytes and tests/unit/install/test_cache_pin.py stamping traps -- deleted the deploy-proof default and the marker-writer check; each FAILED; guards restored.

Lint contract

uvx [email protected] check and format --check (src, tests, architecture linter, probe entry) are silent. pylint R0801 scores 10.00/10. lint-auth-signals.sh and lint-architecture-boundaries.sh exit 0.

CI

All 38 checks on 5f3ecbb: 35 pass, 3 skipping, 0 failing (after 2 CI fix iterations).

The owner-touch gate verified 17 touched owners, each with functional test evidence at this exact head (22 files, 967 passed).

Mergeability status

PR head SHA CEO stance iters folds defers Copilot rounds CI mergeable mergeStateStatus notes
#3900 5f3ecbb ship_with_followups 2 12 3 1 green MERGEABLE BLOCKED pending required review

Recommendation

Ship at 5f3ecbb. The blocking D-S1 finding is fixed and covered by unit and lifecycle tests, and CI is green on this head. Track the three deferred items separately, S3 first.

Full per-persona findings (delta pass, all resolved)

Python Architect

  • [recommended] cache_pin._copy_holds_locked_commit added a second legacy-copy rule that ignored .git at src/apm_cli/install/cache_pin.py:221
    Resolved in 62cf924: it now returns not locked_copy_is_stale(...).
  • [recommended] locked_copy_proof defaulted to the permissive mode at src/apm_cli/drift.py:365
    Resolved in 62cf924: trust_marker=False by default, enforced by the guard.

Test Coverage Expert

Supply Chain Security Expert

  • [blocking] --frozen replay wrote an absolute root from another clone, taken from the lockfile, into native MCP commands at src/apm_cli/integration/mcp_config_view.py:308
    Resolved in 38fd17c: no root sourced from the lockfile is ever written; entries are owned only on an exact portable match.
  • [recommended] The marker writer's legacy branch could stamp a contradicting .git checkout at src/apm_cli/install/cache_pin.py:221
    Resolved in 62cf924.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants