Skip to content

Agent mode cannot move a gem to a newer patch of the same version: apply and the re-scan fail with "no matching variant found" for a ?platform=ruby record, and CI with a cached vendor/bundle exits 1 #2376

In progress

Description

[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).

Summary

In agent mode, moving a gem to a newer patch of the same gem version always fails. The installed file holds the earlier Socket patch's output, and .socket/blobs/ still has the blob that proves it, but apply (and the apply inside scan --mode agent / get) refuses the gem with no matching variant found and exits 1. This happens when the manifest record carries a platform qualifier (pkg:gem/rack@3.2.9?platform=ruby), which is the purl the repo's own stand-in for the patch service (tests/nokogiri/patch_service.rb) gives every gem patch. The same record without the qualifier (pkg:gem/rack@3.2.9) updates fine.

Not a regression: v5.1.0 fails the same way (with the shorter message).

Impact

  • The re-scan that picks up a newer patch exits 1 for every patched gem. With six patched gems, socket-patch scan --mode agent printed [update] … (replacing …) six times, then Applied 0 of 6 patches: 6 failed. The manifest is already rewritten to the new patches, so from then on apply, apply --check, scan and vex exit 1, and rollback refuses every gem (that aftermath is A newer agent patch whose apply fails still replaces the recorded one: rollback refuses the package, and after a prune every scan and apply exits 1 #2299).
  • CI breaks for everyone else. Agent mode doesn't change Gemfile.lock, so a CI cache of vendor/bundle keyed on the lock is restored with the earlier patch still in the gems. Once the updated .socket/ is committed, the socket-patch apply step that the scan tells people to run after every bundle install exits 1 on every build until someone clears the cache. A teammate who pulls the commit gets the same failure.
  • The message blames the user. It says "The gem was edited after it was installed, or it is another build of this version". Nobody edited the gem; the bytes are what socket-patch wrote.
  • The named remedy does work, but it is one bundle pristine <gem> per gem, then apply again. apply --force also works and is not named.

No file is damaged: the gems keep the earlier patch.

Repro

Offline, with real Bundler and a hand-written manifest (no patch service needed). Pass the binary as $1; pass pkg:gem/rack@3.2.9 as $2 to see the unqualified record succeed.

#!/bin/bash
SP=${1:-socket-patch}; KEY=${2:-'pkg:gem/rack@3.2.9?platform=ruby'}
set -e
rm -rf app && mkdir app && cd app
printf 'source "https://rubygems.org"\ngem "rack", "3.2.9"\n' > Gemfile
bundle config set --local path vendor/bundle >/dev/null
bundle install >/dev/null
F=$(ls -d vendor/bundle/ruby/*/gems/rack-3.2.9)/lib/rack.rb
githash() { (printf 'blob %d\0' "$(wc -c < "$1")"; cat "$1") | sha256sum | cut -d' ' -f1; }
mkdir -p .socket/blobs
cp "$F" before;                       B=$(githash before); cp before .socket/blobs/$B
(cat before; echo '# patch A') > a;   A=$(githash a);      cp a .socket/blobs/$A
(cat before; echo '# patch B') > b;   N=$(githash b);      cp b .socket/blobs/$N
manifest() { cat > .socket/manifest.json <<JSON
{"patches":{"$KEY":{"uuid":"$1","exportedAt":"2026-02-01T00:00:00Z",
"files":{"lib/rack.rb":{"beforeHash":"$B","afterHash":"$2"}},
"vulnerabilities":{"GHSA-aaaa-bbbb-0001":{"cves":[],"summary":"s","severity":"high","description":"d"}},
"description":"d","license":"MIT","tier":"free"}}}
JSON
}
set +e
manifest 11111111-1111-4111-8111-111111111111 $A
echo "== apply patch A";  $SP apply --offline 2>&1 | grep -E 'Summary|Error'; tail -1 "$F"
# what a scan that finds a newer patch for the same gem version records
manifest 22222222-2222-4222-8222-222222222222 $N
echo "== apply patch B"; $SP apply --offline 2>&1 | grep -E 'Summary|Error'; echo "exit ${PIPESTATUS[0]}"; tail -1 "$F"

Output on main (cfc870f), Bundler 4.1.1:

== apply patch A
Summary: 1 of 1 targeted patch applied, 0 already patched, 0 not found on disk
# patch A
== apply patch B
Error: Failed to patch pkg:gem/rack@3.2.9: no matching variant found: the copy in vendor/bundle/ruby/3.3.0/gems/rack-3.2.9 is installed, but lib/rack.rb there holds neither the original nor the patched content of a release the patch was made for, so it was left as it is. The gem was edited after it was installed, or it is another build of this version (one a private gem server rebuilt, or one for another platform). If it was edited, `bundle pristine rack` (or `gem pristine rack`) restores the published gem; then apply the patch again.
Summary: 0 of 1 targeted patch applied, 0 already patched, 1 failed, 0 not found on disk
exit 1
# patch A

With pkg:gem/rack@3.2.9 as the key, the second apply prints 1 of 1 targeted patch applied, exits 0, and the file ends with # patch B.

The same through the real flow, against a loopback stand-in built on tests/nokogiri/patch_service.rb whose patch view carries the file contents (so the scan stores the blobs in .socket/blobs/, as it does with the real API):

  1. A sinatra app with rack 3.2.9, puma 6.6.1, nokogiri 1.19.7, rake 13.4.2, tilt 2.9.0 and mustermann 3.1.1 in vendor/bundle. socket-patch scan --mode agent --yes patches the six gems; commit .socket/ as the next steps say.
  2. The service publishes a newer patch (new uuid) for each of the same gem versions. socket-patch scan --mode agent --yes again: six [update] lines, six no matching variant found errors, Applied 0 of 6 patches: 6 failed., exit 1. The app still loads the earlier patch.
  3. Commit the updated .socket/ (manifest and blobs), clone it, restore the vendor/bundle saved after step 1 (the CI cache), bundle install, socket-patch apply: the same six errors, exit 1.

Expected

crates/socket-patch-cli/CLI_CONTRACT.md ("The in-place apply paths…"): files matching neither hash are rejected by default, and "The exception is content a Socket patch wrote: a file whose hash names a stored .socket/blobs/ blob (as after an update to a newer patch for the same release) is overwritten with the content_mismatch_overwritten warning, except under --strict." docs/usage.md says the same ("unless they hold an earlier Socket patch's content (a patch update)").

So the second apply should write patch B over patch A's output, warn content_mismatch_overwritten, and exit 0, as it does for the unqualified record.

Actual

The gem is refused with no matching variant found before the mismatch policy is consulted. The earlier patch stays installed and the command exits 1.

Versions

socket-patch Bundler Ruby Record key Result
main cfc870f 4.1.1 3.3.6 pkg:gem/rack@3.2.9?platform=ruby fails, exit 1
main cfc870f 2.6.9 3.3.6 pkg:gem/rack@3.2.9?platform=ruby fails, exit 1
v5.1.0 4.1.1 3.3.6 pkg:gem/rack@3.2.9?platform=ruby fails, exit 1 (no matching variant found, counted as "not found on disk")
main cfc870f 4.1.1 3.3.6 pkg:gem/rack@3.2.9 works: patch B applied, exit 0

Linux only. Nothing here depends on the platform, so macOS and Windows should behave the same.

Suspect code

crates/socket-patch-cli/src/commands/apply.rs:3782: the installed-distribution gate. A qualified singleton (variants[0] != base_purl) stays gated, and variant_matches_installed (apply.rs:1170) accepts only Ready or AlreadyPatched for the representative file. A file that holds an earlier Socket patch's output is a HashMismatch, so the variant is skipped and apply_package_patch never runs. The exception for Socket content lives inside it (crates/socket-patch-core/src/patch/apply.rs:1804, is_socket_content). The gate would need the same check: a representative file whose hash names a stored blob is this distribution with an earlier patch on it.

The gate is shared by every qualified record, so a PyPI record with an artifact_id qualifier probably fails the same way; I only ran gems. #2289 (agent to vendored) and #2299 (the state a failed update leaves) start from this same situation.


Generated by Claude Code

Activity

  1. added
    bug
    bughunt
    pm:bundler
    on Nov 30, 2026
  2. mikolalysenko commented on Dec 1, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-qualified-purl-patch-update-gate.


    Generated by Claude Code

  3. mikolalysenko commented on Dec 1, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] #2392 fixes the update for a gem copy whose gemspec names the record's platform: a ruby-platform copy under the ?platform=ruby record (the repro above, rack), and a platform gem's copy under its own platform's record.

    One case from this report is left, so the PR says Refs:

    • The ruby record on a platform gem's copy (a precompiled nokogiri under ?platform=ruby, with no record for its platform) still fails with no matching variant found. Admitting it on the stored blob alone can overwrite a pristine platform copy the record was never made for: a stored blob may be another record's original content, and the gemspec does not say the copy is the ruby record's distribution. A safe fix needs proof of the copy's pristine bytes, for example its own archive in the gem home's cache/.

    The same update for PyPI (?artifact_id=) and Maven records was not measured.


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:claimedagent:triagedbugbughuntpm:bundlerpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions