Skip to content

Fix agent mode failing on PyPI releases that have a wheel and an sdist record (#2194) - #2409

Open
Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
fix/pypi-agent-variant-selection
Open

Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
fix/pypi-agent-variant-selection

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2194.

The problem

A PyPI release can have more than one patch record: one for each artifact of the release a patch was made from (?artifact_id=py3-none-any-whl, a wheel per platform, tar-gz for the sdist). Agent mode decided which record belongs to an installed copy by hashing one file of the record, the first one it replaces. That one file does not tell the artifacts of a release apart, and two things went wrong.

1. A wheel's install also took the sdist's record. This fails today, with the patch service's real records.

For urllib3 2.2.2 the service has a record made from the wheel (it replaces urllib3/response.py) and one made from the sdist. The sdist keeps its sources under src/, so read against an install every one of that record's 36 files is a file the patch adds. A record with no file to hash matched any copy. On main, after pip install urllib3==2.2.2:

$ socket-patch scan --mode agent
…
Error: Failed to patch pkg:pypi/urllib3@2.2.2?artifact_id=tar-gz: Cannot apply patch:
urllib3/__init__.py - File already exists, but the patch adds it as a new file
(its content matches neither hash)
$ echo $?
1

The wheel's record is applied, the copy is patched, and every later scan and apply still exits 1. I ran the same scan for the other urllib3 releases that have both records:

urllib3 main this branch
2.3.0 exit 1 exit 0
2.2.3 exit 1 exit 0
2.2.2 exit 1 exit 0
1.26.20 exit 1 exit 0
1.26.19 exit 1 exit 0
1.20 exit 0 exit 0

Vendored mode fails on the same release for the same reason (scan --mode vendored, exit 1: prebuilt wheel for pkg:pypi/urllib3@2.2.2 does not carry the patched files at their recorded paths, for the sdist's record), and exits 0 on this branch.

The shape in the issue's comment (an sdist record that replaces a module and setup.py, the module sorting first) is the same defect and is covered by a test.

2. A platform wheel's install took another platform's record.

The platform wheels of one release share their Python files byte for byte. A record that replaces a shared .py file and the wheel's compiled module was matched by the .py file, so every platform's record matched every install. With the macOS and the Linux wheel's records in the manifest, a plain apply on main over a macOS install:

Warning: …manylinux_2_28_x86_64-whl PIL/_imaging.abi3.so did not match the patch's expected
original content; applied the full verified patched content instead
Summary: 2 of 2 targeted patches applied
$ echo $?
0

The installed extension module is then the Linux build. The default mismatch policy allowed it because the file held the output of another Socket patch (the macOS record's, applied a moment earlier). With the Linux record alone, the default policy refuses, and --force or --allow-patch-conflicts writes it. No live patch was made from a platform wheel yet, so this one has not happened to anyone; it would with the first such patch.

The fix

An installed copy says which wheel it came from: an installer writes the wheel's WHEEL metadata into <name>-<version>.dist-info, and its Tag: lines (and Build: line) are the tags of the wheel's file name, which is what an artifact_id spells. The record made from that wheel is the copy's own and is matched as before.

One rule, in a new core module patch/pypi_wheel.rs: a record made from another artifact is the copy's only when every file it lists is. Each file it replaces must hold one side of the record, and each file it adds must be absent or hold the patch. That is asked of

  • every other record of a release that has a record of the copy's own wheel (the sdist's, another wheel's), and
  • a platform wheel's record on any copy that does not name that wheel.

No flag relaxes it, as for a conda build: --force consents to overwriting local changes to the distribution a patch was made for, not to writing one artifact's files into another's install.

It runs in select_installed_variants, so apply --check, vex, rollback and the download narrowing of scan and get share it, and at apply's own variant gate. The download narrowing now hands the selector a record's added files too (it dropped them, which is how the sdist's record had "no file"). vendor commits one artifact per package, so its installed-variant probe takes the record of the copy's own wheel and no other. vendor --force bypasses that probe and vendors every recorded variant, as its help text says; that is unchanged.

What stays as it was:

  • A record that says the same as the copy's own (a wheel's and its sdist's replacing the same module with the same content) is still applied or read as already patched. urllib3 1.20 is one.
  • A release with no record of the installed wheel is matched by the first replaced file, so a release whose only record was made from the sdist still patches a wheel's install (idna 3.10 is one).
  • A copy with no readable WHEEL tags (an .egg-info install) names no wheel.
  • A conda build keeps its own rule for a platform wheel's record.

How a record the rule keeps from a copy is reported: skipped / package_not_installed when another record was tried on the copy; when none was, the copy fails as no_matching_variant and the message names the file that holds neither side (for the Linux record on a macOS install, PIL/_imaging.abi3.so).

Review

/code-review high on the first version found six things. Five are addressed by the rework in the third commit; one is left on purpose.

  • A platform wheel's record on a conda build was reported as not installed: fixed, and the conda fixture now writes real Tag: lines so the suite exercises it.
  • A record rejected on a file other than its first was reported as package_not_installed with no file named: it is no_matching_variant now, with the file.
  • The first version made the copy's own record its only record, and only while that record matched the copy. An edited copy, or one holding an earlier patch, fell back to the sdist's record. The rule no longer depends on the own record matching (tested).
  • vendor --force skips the check: left as it is. --force is documented as bypassing the installed-variant probe to vendor every recorded release variant, and this check is part of that probe (tested both ways).
  • The contract's new paragraph disagreed with the conda paragraph: records that say the same are not kept apart any more, so the two agree.
  • Cost: the check reads nothing for the common case (one record of a pure wheel or an sdist), and hashes files in name order. It still reads the copy's WHEEL once per record of a multi-record release; I left that, since production has about ten such releases.

What it does not fix

Tests

  • patch/pypi_wheel.rs: 11 unit tests (tag reading, compressed tag sets, build numbers, the own record, the every-file rule, select_installed_variants and select_installed_variants_any over each shape).
  • tests/apply/pypi_release_variants.rs: 9 CLI tests over hand-built site-packages, offline: apply, apply --force, apply --allow-patch-conflicts, apply --check and rollback. Seven fail against main's binary; the other two pin behaviour that must not change.
  • commands/vendor.rs: one unit test of the vendor gate for the wheel and sdist pair, with and without --force.
  • Against the production API, with real installs from PyPI: the table above (first and second scan), apply over a manifest saved with --all-releases (five records of three releases) with apply --check, vex and rollback, and a vendored scan with vendor --check and a re-scan.

Overlap: #2392 rewrites the gem half of the same variant gate in commands/apply.rs. The hunk here is placed apart from its hunks, so the two should merge without conflicts in either order.

🤖 Generated with Claude Code

A PyPI release can have one patch record per artifact a patch was made
from: a wheel, a wheel per platform, the sdist. Agent mode matched a
record to an installed copy by the first file it replaces, which does not
tell the artifacts of a release apart.

A wheel's install also took the sdist's record. For urllib3 2.2.2 the
patch service has a record made from the wheel and one made from the
sdist, whose sources are under src/: read against an install, every file
of that record is one the patch adds, so it had no file to be matched by
and matched any copy. A plain agent-mode scan then exited 1 on every run
("File already exists, but the patch adds it as a new file"), and a
vendored scan failed on the same record. The same happens for urllib3
2.3.0, 2.2.3, 1.26.20 and 1.26.19.

A platform wheel's install took another platform's record. The platform
wheels of a release share their Python files byte for byte, so a record
that replaces one of them and the wheel's compiled module matched every
install. With two such records in the manifest, a default apply wrote the
other platform's extension module over the installed one and exited 0;
with one, --force and --allow-patch-conflicts did.

An installed copy says which wheel it came from in the Tag lines of its
WHEEL metadata, which is what a record's artifact_id spells. So:

- the record made from the copy's own wheel, when it matches the copy,
  is the copy's only record, as a platform gem's copy takes its
  platform's record. select_installed_variants applies it for apply
  --check, vex, rollback and the download narrowing of scan and get;
  apply and vendor ask it at their own gates;
- a platform wheel's record that the copy's WHEEL does not name as its
  own has to match on every file it replaces, under any flag.

A release with no record of the installed wheel, and a copy with no
WHEEL tags, are matched as before, so a release whose only record was
made from the sdist still patches a wheel's install.

Fixes #2194.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A conda build never takes a platform wheel's record, and `apply` has its
own account of such a record: it is tried last, refused aloud only when
it is the first record tried on the copy, and quiet after another record
of the release. The file-by-file check for another wheel's record ran
first and skipped the record, so a pure-Python conda build whose file is
not the release's reported the platform wheel's record as "no matching
installed package" beside the pure wheel's failure.

The check now passes over the records a conda build never takes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The first version made the record of the copy's own wheel its only
record, while that record matched the copy. Review found what that left
open or broke:

- when the own record did not match (a file edited after the install, a
  copy holding an earlier patch), the sdist's record was tried again and
  failed as before;
- a record that says the same as the copy's own (a wheel's and its
  sdist's replacing one module with the same content) became "not
  installed", which the conda rule and its contract paragraph do not say;
- a record rejected on a file other than its first was reported as not
  installed, with no file named;
- vendor --force skipped the check.

One rule replaces it. The record made from the copy's own wheel is
matched as any record is. A record made from another artifact is the
copy's only when every file it lists is: each file it replaces holds one
side of the record, and each file it adds is absent or holds the patch.
That is asked of every other record of a release that has a record of
the copy's own wheel, and of a platform wheel's record on any copy that
does not name that wheel. It does not depend on the own record matching,
and no flag relaxes it.

- select_installed_variants asks it, so apply --check, vex, rollback and
  the download narrowing share it; apply asks it at its variant gate.
- The download narrowing hands the selector a record's added files too.
  It dropped them, which is why the sdist's record of a src/ layout had
  no file to be matched by.
- A copy no record matches names the file that says so
  (no_matching_variant), also when that file is not a record's first.
- vendor commits one artifact per package: on a copy that names its
  wheel it vendors that wheel's record alone, and asks no file, so
  --force does not skip it.
- The conda fixture writes the Tag lines a real WHEEL has.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The help text of `vendor --force` says it bypasses the installed-variant
probe and vendors every recorded release variant. The own-wheel check for
a PyPI copy is part of that probe, so it runs where the probe runs and
`--force` skips it, as it skips the rest. The previous commit had moved
it out from under `--force`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 1, 2026 14:35
@cursor

cursor Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes core variant selection for all PyPI agent/vendored flows; wrong matching could skip valid patches, but behavior is heavily tested and aligns apply/scan/vendor paths.

Overview
Fixes agent-mode failures when a PyPI release has multiple patch records (wheel, sdist, platform wheels) by matching installs to the artifact they came from, not a single representative file.

A new patch/pypi_wheel module reads WHEEL Tag:/Build: metadata to find the copy's own record. anothers_record rejects sibling artifact records unless every listed file matches (replacements and added paths), shared via select_installed_variants across apply, rollback, scan/get narrowing, and vex. vendor vendors only the installed wheel's record ( --force unchanged).

Download narrowing now builds variant file maps with files_for_manifest (includes patch-added files) instead of files_with_both_hashes, so sdist records are judged correctly. Apply skips non-matching variants before apply and reports no_matching_variant with the differing file when appropriate.

CLI_CONTRACT documents the rule. Coverage: core unit tests, vendor gate test, and offline e2e pypi_release_variants (urllib3 wheel+sdist, platform wheels, --force).

Reviewed by Cursor Bugbot for commit 7cf1dbc. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 7cf1dbc. Configure here.

if let Some((name, VerifyStatus::HashMismatch)) = anothers.await {
differs.insert(name.strip_prefix("package/").unwrap_or(name));
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.

Missing files omitted from variant errors

Low Severity

no_variant_reason only records a file from anothers_record when the status is HashMismatch. A platform record rejected because a compiled module is absent (NotFound) falls through to the representative-file check, which often matches a shared Python file, so differs stays empty and the no_matching_variant message does not name the file that actually disqualified the record.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 7cf1dbc. Configure here.

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.

Agent-mode PyPI variant selection uses one representative file, not the installed wheel's tags, so wheels that share .py files all match

1 participant