Skip to content

fix(install): deterministic orphan retention for shared-identity rows (#4060) - #4063

Merged
Daniel Meppiel (danielmeppiel) merged 2 commits into
mainfrom
danielmeppiel-4060-orphan-determinism
Nov 20, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 2 commits into
mainfrom
danielmeppiel-4060-orphan-determinism

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

TL;DR

_pin_retained_ancestors (the targeted-install pin walk in the orphan removal owner) mapped each lock parent identity to one dropped key by iterating a frozenset. When a repository and its virtual child share an identity, which row was pinned depended on PYTHONHASHSEED. Each identity now maps to every dropped row that carries it, and all of them are pinned, so the retained set is always {parent, child} and nothing is removed.

Fixes #4060

Note

Stacked on #4021 (base danielmeppiel-animated-fiesta at fa99ad85, the P1 guard fold), which moves the function into install/orphans.py. CI does not run on a stacked base, so local evidence is below.

Problem (WHY)

  • Across PYTHONHASHSEED=0..7 the same targeted-install decision split: seeds 0, 2 and 6 retained {acme/repo/skills/child} and removed {acme/repo}; the rest retained both.
  • The function's own docstring requires keeping the parent chain so the next targeted install can rediscover the child, so only {parent, child} is correct.
  • apm install --dry-run previews no package orphans for a targeted install (install/presentation/dry_run.py), so on the bad seeds the real run removed a package that the preview did not show.

Approach and implementation

File Change
src/apm_cli/install/orphans.py by_identity becomes dict[str, set[str]]; the walk pins every dropped row that has the parent identity. No other selection semantics change.
tests/unit/install/test_partial_subtree_reconciliation.py New test_shared_identity_retention_is_independent_of_hash_seed replays the decision in a subprocess under 12 fixed seeds (0-11), with two controls: distinct identities (a <- b retained, c removed) and a retained row with no dropped ancestor (x kept, y removed). test_pinned_nested_package_survives_parent_module_removal now asserts the exact partition (retained == {parent, child}, removed == {}), that the targeted --dry-run preview equals the real removal set, and that both module dirs survive.
CHANGELOG.md One line under Fixed.

Docs: no page describes targeted-install orphan retention, so there is no docs change (per the record).

Trade-offs

  • Retain over remove. Pinning every same-identity row keeps a dropped repository row until a later full install. That is the fail-safe direction and matches the docstring; the alternative (deterministically choose one row) would still sometimes drop the parent the child needs.
  • The subprocess test costs about 5s for 12 interpreters; varying the hash seed requires a fresh interpreter.

Validation (local; no CI on the stacked base)

Red on base (fix reverted; first measured on 33c8cc9, re-confirmed on fa99ad8: 2 failed, 8 passed) and green on this branch
# base (src/apm_cli/install/orphans.py at 33c8cc94), new tests:
E  AssertionError: ... 'shared-identity': [['acme/repo/skills/child'], ['acme/repo']] != ... [['acme/repo', 'acme/repo/skills/child'], []]
# controls matched on base (distinct-identities, no-dropped-ancestor)
# base, nested-package test with the exact partition, per seed:
seed 0: 1 failed | seed 1: passed | seed 2: 1 failed | seed 3: passed
seed 4: passed   | seed 5: passed | seed 6: 1 failed | seed 7: passed

# this branch:
$ pytest -q tests/unit/install/test_partial_subtree_reconciliation.py
10 passed
Lint mirror, architecture linter, orphan suites
$ ruff check src/ tests/ scripts/lint_architecture_boundaries.py scripts/architecture_linter/
All checks passed!
$ ruff format --check ...
2465 files already formatted
$ python -m pylint --disable=all --enable=R0801 --min-similarity-lines=10 --fail-on=R0801 src/apm_cli/ ...
Your code has been rated at 10.00/10
$ bash scripts/lint-auth-signals.sh
[+] auth-signal lint clean
$ python3 scripts/lint_architecture_boundaries.py --root .
(rc=0)
$ pytest -n 4 tests/unit/install/ tests/unit/test_prune_casing.py tests/unit/test_uninstall_engine_helpers.py tests/unit/test_drift_detection.py tests/unit/test_install_update.py tests/integration/test_architecture_owner_rule_mutations.py -k "orphan or partial or prune or cleanup or pin or Orphan or uninstall or targeted"
1 failed, 527 passed   (after rebase onto fa99ad85)
$ pytest tests/integration/test_architecture_owner_rule_mutations.py -k "orphan or exactly_once"
25 passed   (closed-world orphan guard accepts the change)

The one failure, test_cache_pin.py::test_sync_markers_never_stamps_a_checkout_at_another_commit, fails identically on the base: this host's commit signing rejects the test's own git commit. It is unrelated.

Scenario evidence

Record "done when" Test
Decision under >= 8 fixed seeds, red on base, retained = {parent, child}, removed = {} test_shared_identity_retention_is_independent_of_hash_seed (12 seeds)
Controls keep today's partition same test: distinct-identities, no-dropped-ancestor
Nested-package test asserts the exact partition test_pinned_nested_package_survives_parent_module_removal
apm install --dry-run reports the same partition same test: targeted preview == real removal set (empty)

Issue and approved scope

Issue: Fixes #4060

Human scope-approval comment: #4060 (comment) (verified record-present with scripts/governance/eligibility.cjs)

Completes the issue. Out of scope per the record: other orphan-selection semantics, deletion evidence (#3701), #3188, #2413.

Type of change

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

Testing

  • Tested locally
  • All existing tests pass (orphan-related suites; one unrelated host-only failure noted above)
  • Added tests for new functionality (if applicable)

Changelog

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

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour.

How to test

  1. git checkout danielmeppiel-4060-orphan-determinism
  2. uv run --extra dev pytest -q tests/unit/install/test_partial_subtree_reconciliation.py -- 10 passed.
  3. git checkout fa99ad85 -- src/apm_cli/install/orphans.py and rerun step 2: the seeded test fails on the split partition. Restore with git checkout HEAD -- src/apm_cli/install/orphans.py.

Co-authored-by: Copilot [email protected]

@danielmeppiel
Daniel Meppiel (danielmeppiel) force-pushed the danielmeppiel-4060-orphan-determinism branch from bbae8e6 to 8645ee7 Compare November 20, 2026 03:21
@danielmeppiel
Daniel Meppiel (danielmeppiel) force-pushed the danielmeppiel-4060-orphan-determinism branch from 8645ee7 to 0b5b6ec Compare November 20, 2026 04:23
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.37.0 milestone Nov 20, 2026
@danielmeppiel Daniel Meppiel (danielmeppiel) changed the title [0.38] fix(install): deterministic orphan retention for shared-identity rows (#4060) fix(install): deterministic orphan retention for shared-identity rows (#4060) Nov 20, 2026
@danielmeppiel
Daniel Meppiel (danielmeppiel) force-pushed the danielmeppiel-4060-orphan-determinism branch from 0b5b6ec to 40edf8d Compare November 20, 2026 08:22
Base automatically changed from danielmeppiel-animated-fiesta to main November 20, 2026 08:36
…#4060)

_pin_retained_ancestors mapped each lock parent identity to one dropped key
by iterating a frozenset, so when a repository and its virtual child shared
an identity, the retained set depended on PYTHONHASHSEED. Map each identity
to every dropped row that carries it and pin all of them, keeping the parent
chain the next targeted install needs.

The new test replays the decision under 12 fixed hash seeds with two
controls; the nested-package test now asserts the exact partition.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel
Daniel Meppiel (danielmeppiel) force-pushed the danielmeppiel-4060-orphan-determinism branch from 40edf8d to bcab245 Compare November 20, 2026 08:36
@danielmeppiel
Daniel Meppiel (danielmeppiel) marked this pull request as ready for review November 20, 2026 08:49
Copilot AI balanced review requested due to automatic review settings November 20, 2026 08:49
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 9d50de1 into main Nov 20, 2026
33 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-4060-orphan-determinism branch November 20, 2026 08:49

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

The orphan traversal repeatedly enqueues shared-identity buckets, causing quadratic work and excessive queue growth when many virtual children share a parent’s identity.

1 open issue
What changed in this PR

Fixes #4060 in the centralized orphan-removal owner introduced by #4021, making targeted installs retain shared-identity repository and virtual-child rows regardless of Python hash seed.

Changes:

  • Pins every dropped row matching a retained entry's parent identity.
  • Adds regression coverage across 12 hash seeds and strengthens retention, directory-survival, and dry-run assertions.
  • Documents the user-visible fix in the changelog.
File Description
tests/​unit/​install/​test_partial_subtree_reconciliation.py Adds seeded regression cases and exact retention assertions.
src/​apm_cli/​install/​orphans.py Maps each parent identity to all matching dropped rows.
CHANGELOG.md Records deterministic retention during targeted installs.

Review effort: Balanced

if parent in by_identity
)
for parent in lockfile.dependencies[key].parent_keys:
queue.extend(by_identity.get(parent, ()))
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.

Orphan selection is nondeterministic for shared-identity lock rows (depends on PYTHONHASHSEED)

2 participants