Skip to content

Sketch shared dependency traversal and removal pruning - #934

Draft
zanieb wants to merge 7 commits into
mainfrom
zb/shared-lock-traversal
Draft

zanieb wants to merge 7 commits into
mainfrom
zb/shared-lock-traversal

Conversation

@zanieb

@zanieb zanieb commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

End-to-end sketch of shared dependency traversal, including the removal pruning from astral-sh#20192. The complete sketch comparison shows the four commits together. Lock consumers share package/extra identity and dependency expansion through LockWalker and MarkerReachabilityWalker. The latter uses the same marker-accumulation machinery as resolver graph propagation, while OnceQueue also serves installed-metadata traversal, recursive extra expansion, and tree filtering. Workspace metadata, workspace-member selection, and root-requirement matching are included in the sketch.

The commits separate the traversal foundation, adoption by the remaining consumers, and removal integration. Removal keeps its conservative environment and conflict-selection policy above the shared walker. Metadata validation, path-sensitive tree rendering, and export's conflict inference retain their richer state; their different revisit rules are documented alongside the shared walkers. This is an alternative design sketch and does not change the existing upstream stack.

@astral-automations-bot astral-automations-bot Bot added the internal A refactor or improvement that is not user-facing label Aug 31, 2026
@zanieb zanieb changed the title Share lock dependency traversal Sketch shared dependency traversal and removal pruning Sep 1, 2026
@zaniebot zaniebot added priority:5 Backlog: speculative, cosmetic, or low-value work that can comfortably be deferred. size:5 Project-sized: too much semantic scope for a normal review; needs decomposition or design review. risk:3 Credible risk of meaningful incorrect behavior or regression; bounded or recoverable. labels Sep 9, 2026
@codspeed

codspeed Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 16.58%

⚠️ 8 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

⚡ 2 improved benchmarks
✅ 67 untouched benchmarks
⏩ 35 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ ⚠️ Simulation discover_workspace_from_all_members_with_excludes 165.6 ms 139.9 ms +18.35%
⚡ ⚠️ Simulation discover_workspace_from_all_members 151.5 ms 132 ms +14.83%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing zb/shared-lock-traversal (10cf0b2) with main (64e2ac8)

Open in CodSpeed

Footnotes

  1. 35 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment on lines +264 to +268
let external_reachability = site_packages.reachable_packages(
site_packages
.iter()
.filter(|distribution| !managed.contains(distribution.name()))
.map(|distribution| (distribution.name(), &[] as &[ExtraName])),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Follow replacement dependencies during external retention

The managed filter excludes traversal roots, but reachable_packages can still follow an external package into a managed package's old installed metadata. For example, extend the staged remaining 1→2 upgrade in remove_subset_ignores_replaced_package_dependencies with an unmanaged package requiring remaining. The walk follows remaining==1.0.0 to candidate and retains it, although this operation installs remaining==2.0.0, which no longer needs it. Pruning therefore leaves an orphan. Distinguish replaced distributions during retention traversal and use their replacement dependencies; add coverage for an external path through an upgraded package.

Comment thread crates/uv-install-operations/src/lib.rs Outdated
Comment on lines +307 to +308
modifications: Modifications,
prune: Option<PrunePolicy>,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P3] Represent the removal modes with one input

These inputs represent three mutually exclusive modes, but Some(prune) silently overrides modifications. Consequently, remove passes Exact while retaining unrelated packages, and the API also accepts Sufficient with a policy that deletes extraneous packages. Callers must understand this precedence instead of relying on the documented meaning of Modifications. Use an installation-layer enum for sufficient, exact, and pruning modes, converting the existing configuration value at the boundary.

Comment thread crates/uv-cli/src/lib.rs Outdated
Comment on lines +939 to +940
/// Transitive dependencies of the requested packages will be removed from the environment
/// unless they are required by another dependency in the project.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P3] Document retention by other installed packages

The exception omits unmanaged installed packages, which also prevent pruning. For example, remove_subset_prunes_only_unshared_branches retains candidate because manually installed external requires it, despite no remaining project declaration requiring it. Mention both remaining project declarations and other installed packages so the help describes the implemented cleanup policy.

@zaniebot
zaniebot deployed to automations October 9, 2026 03:40 — with GitHub Actions Active
@zaniebot
zaniebot deployed to automations October 9, 2026 03:46 — with GitHub Actions Active
Comment on lines +249 to +252
let Ok(metadata) = distribution.read_metadata() else {
incomplete.insert(package.clone());
continue;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P3] Preserve the cause of installed metadata failures

When installed metadata is missing, unreadable, or malformed, this discards the InstalledDistError. PrunePolicy subsequently logs only package names and proceeds with pruning, so verbose output cannot explain why dependency information was ignored. Log the error with the distribution path here, or retain it in InstalledReachability, while keeping the intended best-effort behavior.

Comment on lines +87 to +91
├── remaining
│ ├── remaining-1.0.0
│ │ └── requires candidate
│ │ └── satisfied by candidate-1.0.0
│ └── remaining-2.0.0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P3] Refresh the documented replacement dependency graph

The linked fixture diagram omits remaining[foo] for both versions and the remaining-external and remaining-extra-external packages present in the TOML. These edges explain the different retention expectations in the replacement tests, so their omission makes those tests harder to assess. Update the diagram from the current fixture and correct its generation header: cargo dev render-scenario is not an available subcommand in this checkout.

@zaniebot
zaniebot deployed to automations October 9, 2026 03:57 — with GitHub Actions Active
@zaniebot
zaniebot deployed to automations October 9, 2026 04:03 — with GitHub Actions Active
Comment thread crates/uv/tests/project/edit.rs Outdated
Comment on lines +5759 to +5760
assert_eq!(context.read("uv.lock"), "invalid lock");
assert!(!context.read("pyproject.toml").contains("candidate"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P3] Snapshot the frozen removal result

Unlike remove_requirement and neighboring edit tests, this test checks only command success and substring absence. An empty or truncated pyproject.toml would satisfy the assertion, and unexpected command output would go unnoticed. Use uv_snapshot! for the removal command and assert_snapshot! for the edited TOML to verify the expected edit and intact project fields. Keep the exact equality check for the deliberately invalid lockfile.

Comment on lines +240 to +245
if matches!(
distribution.kind,
InstalledDistKind::EggInfoFile(_)
| InstalledDistKind::EggInfoDirectory(_)
| InstalledDistKind::LegacyEditable(_)
) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P3] Classify installed metadata formats exhaustively

This check encodes which installed formats provide complete dependency metadata for pruning. matches! implicitly sends every future InstalledDistKind through the supported path, so adding a format does not require revisiting that assumption. Use an exhaustive match with explicit Registry | Url and legacy-format arms so new variants require a deliberate completeness decision.

Comment on lines +281 to +285
let managed = resolution
.distributions()
.map(Name::name)
.chain(candidates.difference(&retained))
.collect::<BTreeSet<_>>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Include planner-preserved packages in retention roots

This excludes every removal candidate from external roots, even when Planner::build exempts it from plan.extraneous. In a seeded Python 3.11 environment where wheel depends on packaging, removing wheel can therefore retain wheel through the seed-package exemption while pruning packaging. Pass the planned extraneous distributions into this calculation and exclude only candidates actually eligible for removal, so packages preserved by the planner also protect their installed dependencies.

Comment on lines +290 to +293
site_packages
.iter()
.filter(|distribution| !managed.contains(distribution.name()))
.map(|distribution| (distribution.name(), &[] as &[ExtraName])),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Carry declared extras into the installed retention walk

Seeding every retained installed package with empty extras can prune dependencies of an unselected group. For example, sync a non-default group containing a[foo]==1, where foo requires c, then update it to a[foo]==2 with --no-sync, where foo no longer requires c. Removing another dependency that requires c leaves a==1 installed, but the new lock does not retain c and this installed walk never visits a[foo], so c is uninstalled. The extra remains explicitly declared; it is not merely historical pip install state. Carry declared extra activations alongside retained names and use them when traversing packages left installed, extending the existing staged unselected-group regression coverage.

This branch was successfully deployed

1 active deployment
automations — ae72caa8 Deployed Oct 21, 2026 by astral-automations-bot[bot] via review-code-quality / report #11227
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal A refactor or improvement that is not user-facing priority:5 Backlog: speculative, cosmetic, or low-value work that can comfortably be deferred. risk:3 Credible risk of meaningful incorrect behavior or regression; bounded or recoverable. size:5 Project-sized: too much semantic scope for a normal review; needs decomposition or design review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants