Skip to content

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

Merged
Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
fix/pypi-agent-variant-selection
Dec 1, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 5 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, rollback and the download narrowing of scan and get share it, and at apply's own variant gate, where the copy's own wheel's record is tried first. vex reads every record of the release against the copy and attests the ones in effect, as before. 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, or that the copy does not hold (for the Linux record on a macOS install, the Linux build of the extension module).

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.
    A second pass on the reworked version found five more, fixed in the fifth commit: a record rejected for a file the copy lacks was reported with no file named; which of two records that replace one file was written depended on the manifest's map order (the copy's own wheel's record is tried first now); the failure for an edited copy also named a pristine file; the contract said vex shares the rule, which it does not; and two doc comments were stale.

  • 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: 11 CLI tests over hand-built site-packages, offline: apply, apply --force, apply --allow-patch-conflicts, apply --check and rollback. Nine 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.

A second review of the every-file rule found:

- A record kept from a copy because one of its files is missing (another
  platform's extension module, which carries the platform in its name)
  failed the run as a bare "no matching variant found" with no failed
  event. `no_variant_reason` now names that file: the copy is installed,
  and does not hold a file the release the patch was made for does.
- Which of two records that replace one file was written depended on the
  manifest map's order: each record is judged against what the earlier
  ones left on disk. `apply` now tries the copy's own wheel's record
  first, then the others by purl.
- The message for a copy no record matches named a file of the copy that
  only differs from what another artifact's record adds. Such a file is
  named only when no record matched by its own file differs.
- The contract and the module docs said `vex` shares the rule. It does
  not: it reads every PyPI record against every copy and attests the
  ones in effect, as before. The text says so now.
- Two doc comments still said a record's added files never count.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does: When a PyPI release has more than one record in the manifest (wheel and sdist, or several platform wheels), agent mode now picks the record of the installed copy's own wheel, which it reads from the copy's WHEEL tags. It no longer takes the first record whose representative file matches. A record made from another artifact is only applied when every one of its files matches. When no variant matches, the error now names the file that differs or is missing. Fixes #2194.

Risk: medium. It changes how PyPI variants are chosen in apply, check, rollback, scan/get narrowing and vendor. Copies with no WHEEL file, no record of their own, or only one record keep the old behavior.

Look here

  • pypi_wheel.rs:85: parses WHEEL tags and matches them to a record's artifact.
  • pypi_wheel.rs:180: anothers_record, the rule that every file must match.
  • apply.rs:3741: tries the copy's own wheel record first, and the check that runs before --force.
  • apply.rs:5094: no_variant_reason error text.
  • agent_download.rs:607: download narrowing now uses a record's added files in every ecosystem, not only PyPI (see Open questions).

Verified: I reviewed the full diff. CHANGELOG.md is untouched and there is no debug code left in. I ran the following on the head in a separate worktree (stable toolchain, because the pinned 1.93.1 couldn't be installed in the sandbox):

  • core lib: 8676 passed
  • --test apply: 189 passed, including the 11 new pypi_release_variants tests
  • in_process_pypi_* and e2e_pypi_multi_copy: pass

With the source reverted to main and the new tests kept, 9 of the 11 new tests fail; the other 2 guard behavior that shouldn't change. CI on 99dd69a: ci-ok and clippy are green. Bugbot's one finding (a missing file not named in the error) was already fixed in 99dd69a; I answered and resolved that thread.

Changes I made: none.

Open questions (non-blocking)

  • The added-files narrowing in agent_download.rs now applies outside PyPI too. For example, a Maven record that only adds jar members now needs the jar present. That matches what apply does, but the description doesn't mention it.
  • A renamed or retagged platform wheel (WHEEL tags that don't match its file name) gets the every-file check, so a file edited locally now blocks --force where it used to be overwritten.
  • A record path that escapes the package is refused, correctly, but the error reports it as "does not hold ".

Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Dec 1, 2026

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.

[agent] Reviewed; LGTM.

@mikolalysenko
Mikola Lysenko (mikolalysenko) added this pull request to the merge queue Dec 1, 2026
Merged via the queue into main with commit 0120126 Dec 1, 2026
50 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the fix/pypi-agent-variant-selection branch December 1, 2026 21:02
Mikola Lysenko (mikolalysenko) added a commit that referenced this pull request Dec 1, 2026
Main has moved since this branch was cut (thirteen commits, #2409 and
#2392 among them). The merge is clean: the two files both sides changed,
vendor/mod.rs and vendor/verify.rs, merged without a conflict.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

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

3 participants