Skip to content

Fix patch update refused for qualified gem records (#2376) - #2392

Open
Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
agent/fix-qualified-purl-patch-update-gate
Open

Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
agent/fix-qualified-purl-patch-update-gate

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code:claude-fable-5-2

Refs #2376

Summary

In agent mode, moving a gem to a newer patch of the same gem version failed
for every record that names its platform (pkg:gem/rack@3.2.9?platform=ruby):
apply, and the apply inside scan --mode agent / get, refused the gem
with no matching variant found and exited 1, although the installed file
held an earlier Socket patch whose blob was still in .socket/blobs/.

With this change the newer patch is written over the earlier one, with the
content_mismatch_overwritten warning, as it already was for an unqualified
record. It applies to a gem copy whose gemspec names the platform the record
was written for
: a ruby-platform copy under the ruby record, and a platform
gem's copy under its own platform's record.

Root cause

A record that names its platform is applied to a copy only after an
installed-distribution gate: the record's representative file must hold its
original or patched content. A file holding an earlier patch's output holds
neither, so the record was skipped before the engine's mismatch policy (which
overwrites content a Socket patch wrote) was ever consulted.

The gate exists because the file hash was the only thing telling a record's
distribution from another one (a lone x86_64-linux record must never be
written over an arm64-darwin copy). But the copy's gemspec states its
platform whatever its files hold. So the gate now also admits a copy when all
of this holds:

  1. the file's content is a stored Socket blob;
  2. the copy's gemspec names the record's platform;
  3. that blob is not one side (before or after) of another record of the same
    gem version in the manifest;
  4. the run is not --strict.

The engine then handles the files as it does for an unqualified record: every
file is verified first, and nothing is written unless each one is original,
patched, or Socket content.

What stays refused (each has a test)

  • A copy of another platform, even when its file holds a stored blob.
  • A copy with no gemspec in its gem home.
  • A local edit (content that is no stored blob).
  • --strict.
  • A platform copy that holds the ruby record's current patch: its own
    platform's record is not written over it.

Not fixed here (why this is Refs, not Fixes)

The ruby record on a platform gem's copy (a precompiled nokogiri under a
?platform=ruby record, with no record for its platform). The first cut of
this PR admitted it; the review showed it can overwrite a pristine platform
copy whose bytes the record was never made for, because a stored blob may be
another record's original content and the gemspec does not say the copy is
the ruby record's distribution. That branch was removed, so this case still
fails closed with no matching variant found, as on main. A safe fix needs
real proof of the copy's pristine bytes, for example the copy's own archive in
the gem home's cache/.

Other things the review raised that this PR leaves alone:

  • apply --check, vex, rollback and the download filter of get/scan
    select records through the core selection, which still does not match a copy
    holding an earlier patch. Before the update is applied, apply --check
    reports such a copy as no_matching_variant, and get keeps every variant
    of the release. Both were so before; after apply runs the copy matches.
  • PyPI (?artifact_id=) and Maven records are gated the same way and probably
    fail the same update. Not measured, and they need their own distribution
    proof.
  • A platform copy whose own platform's record never matched its bytes, and
    that was patched through the ruby record of an earlier patch: on update
    its own platform's record is now admitted (the earlier blob is no longer in
    the manifest, so nothing tells which record wrote it). The engine still
    verifies every file before writing. On main this case exits 1.

Evidence

The issue's script (real Bundler 4.0.18, Ruby 3.3.6, rack 3.2.9,
pkg:gem/rack@3.2.9?platform=ruby):

apply patch B over patch A file ends with
main 1b3f422 no matching variant found, exit 1 # patch A
this branch 1 of 1 targeted patch applied, warning, exit 0 # patch B

After it, apply --check says Patches are in sync and rollback restores
the published file.

Tests (crates/socket-patch-cli/tests/apply/gem_patch_update_qualified.rs,
real binary, offline). The first commit adds them against main: the three
update cases failed with no_matching_variant, the guards passed.

Local runs, all passing:

cargo clippy --locked --workspace --all-features -- -D warnings
cargo test -p socket-patch-core --lib patch::          # 1496 passed
cargo test -p socket-patch-cli --lib                   # 1083 passed
cargo test -p socket-patch-cli --test apply            # 187 passed
cargo test -p socket-patch-cli --test get --test scan  # 162 + 268 passed
cargo test -p socket-patch-cli --test e2e_gem_native_ext_build  # 43 passed

cargo clippy ... --all-targets reports 71 errors in test code on main
without this change (CI's clippy job does not pass --all-targets); none are
in the lines this PR touches.

CLI_CONTRACT.md gains the rule beside the existing "content a Socket patch
wrote" paragraph. The npm/, pypi/ and gem/ wrappers call the binary and
need no change.

https://claude.ai/code/session_011f9aeRLXgywqBzkw2e1bPs


Generated by Claude Code

Refs #2376.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeRLXgywqBzkw2e1bPs
A gem copy that holds an earlier Socket patch's output must take a
newer patch from a record that names its platform. Four cases fail
today with no_matching_variant; four guards (another platform's copy,
no gemspec, a local edit, --strict) pass and must keep passing.

Refs #2376.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeRLXgywqBzkw2e1bPs
A manifest record that names its platform (pkg:gem/x@1?platform=ruby)
is applied to a copy only when the copy's representative file holds
the record's original or patched content. A copy that holds an earlier
Socket patch for the same release holds neither, so every update to a
newer patch failed with "no matching variant found" before the
mismatch policy, which overwrites Socket content, was consulted.

The gate now also admits a gem copy whose file is a stored blob when
the copy's gemspec gives it to the record: a copy of the platform the
record names, or, for the ruby record, a platform gem's copy that no
record of the version names. The gemspec says which distribution the
copy is whatever its files hold, so a record is still never written
over a copy of another platform, a copy with no gemspec, or a local
edit, and --strict still refuses.

Fixes #2376.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeRLXgywqBzkw2e1bPs
Review of the first cut found two ways it could write a record over
bytes it was not made for:

- The ruby-platform record was admitted on a platform gem's copy that
  no record names. The gemspec does not say such a copy is that
  record's distribution (it is matched to it by files alone), and a
  stored blob may be another record's original content, so a pristine
  platform copy could be overwritten. That fallback is gone: a record
  is admitted only on a copy whose gemspec names its platform.
- A platform copy patched by the ruby-platform record had its own
  platform's record forced over that patch. A blob that is one side of
  another record of the version is a state the manifest describes, not
  an earlier patch, and no longer admits a record.

The contract note says which gated records the rule covers.

Refs #2376.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeRLXgywqBzkw2e1bPs
@mikolalysenko Mikola Lysenko (mikolalysenko) changed the title Fix patch update refused for qualified purls (#2376) Fix patch update refused for qualified gem records (#2376) Dec 1, 2026
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 1, 2026 02:07
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes gem variant matching and overwrite policy during apply; wrong logic could target the wrong platform, but gemspec/blob/manifest guards and broad new tests limit exposure.

Overview
Fixes #2376: apply no longer refuses qualified RubyGems records (?platform=) when the on-disk copy already holds an earlier Socket patch (hash matches neither beforeHash nor afterHash, previously surfaced as no matching variant found).

The installed-distribution gate now also accepts a gem copy when its gemspec names the record's platform, the representative file's content is a stored .socket/blobs/ blob, that hash is not another variant's before/after for the same file, and the run is not --strict—then the newer patch applies with content_mismatch_overwritten like unqualified records. own_platform_variant and the multi-variant apply loop use new variant_is_installed / EarlierPatch wiring; core adds gem_copy_holds_earlier_patch and copy_is_of_record_platform (gemspec-based platform check).

CLI_CONTRACT.md documents the gem-specific rules; refusals (other platform, no gemspec, local edit, strict, sibling-record state) stay unchanged. New offline apply tests in gem_patch_update_qualified.rs pin update and guard behavior.

Reviewed by Cursor Bugbot for commit 0cad491. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
When a gem's manifest record names its platform (pkg:gem/rack@3.2.9?platform=ruby), updating to a newer Socket patch of the same version used to fail with no matching variant found, because the installed file held the earlier patch's output. Now apply admits such a copy when the file is a stored Socket blob, the copy's gemspec names the record's platform, the blob isn't one side of a sibling record, and the run isn't --strict; the engine then overwrites with the usual content_mismatch_overwritten warning. It is Refs #2376, not Fixes: the ruby record on a platform gem's copy still fails closed, on purpose.

Risk: medium. It widens a safety gate that decides which files apply may overwrite. The widening is narrow (it needs a stored blob and a gemspec that names the platform, and the engine still verifies every file before writing), and every refusal case has a test. Gem-only; other ecosystems are unchanged.

Look here

  • apply.rs:1235: variant_is_installed, the new gate (policy check, sibling-record exclusion at L1255)
  • apply.rs:3877: where the variant loop now uses it
  • core apply.rs:2188: gem_copy_holds_earlier_patch
  • gem_ext.rs:5290: copy_is_of_record_platform (the gemspec check; the ruby record never matches a platform copy here)
  • [gem_patch_update_qualified.rs:234](https://github.com/SocketDev/socket-patch/blob/0cad4918a1c375aeb5d338aa3d15c2b2006ce55e/crates/socket-patch-cli/tests/apply/gem_patch_update_qualified.rs#L234): the sibling-record guard test

Verified

Changes I made: none.

Open questions

  • Known gaps listed in the description: apply --check, vex, rollback and get's download filter still report such a copy as no_matching_variant until apply runs, and PyPI and Maven qualified records probably hit the same update failure. They look like follow-up issues, not blockers.

Generated by Claude Code

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.

2 participants