Skip to content

Fix reporting of an installed gem that matches no variant (#2231) - #2237

Merged
Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
agent/fix-gem-unmatched-copy-reporting
Nov 25, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
agent/fix-gem-unmatched-copy-reporting

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

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

Fixes #2231

Summary

In agent mode, a gem whose installed file matches neither hash of its
?platform=… record (edited in place, or rebuilt by a private gem server)
is correctly left untouched. But every command described a different
problem: apply / scan said the gem was "not installed on this host" and
"not found on disk", apply --json reported package_not_installed,
apply --check said to run socket-patch apply, and rollback / remove
said the file was "modified after patching".

No patching behavior changes: the same copies are left untouched and
the same runs exit 1. Only what the run reports changes.

Command Before After
apply, scan --mode agent no matching variant found, then "not installed on this host (resolved by the project lockfile; skipped)", "1 not found on disk" One error that names the copy, the file and bundle pristine <gem>; summary says 1 failed, 0 not found on disk
apply --json skipped / package_not_installed beside partialFailure failed / no_matching_variant, keyed by the base purl (the code apply --check already used)
apply --check "Run socket-patch apply to regenerate them." The drift line says apply leaves the copy as it is and names bundle pristine <gem>; the closing apply remedy is printed only when some other drift needs it
rollback, remove "File has been modified after patching" "File holds neither the patched nor the original content", and rollback lists the two ways out: reinstall (bundle pristine <gem>) and roll back again, or remove <purl> --skip-rollback

Output after the change, for the issue's project (rack with a debug edit):

$ socket-patch apply
Error: Failed to patch pkg:gem/rack@3.1.0: no matching variant found: the copy in
vendor/bundle/ruby/3.4.0/gems/rack-3.1.0 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

$ socket-patch rollback
Failed to roll back:
  pkg:gem/rack@3.1.0?platform=ruby: Cannot roll back: lib/rack.rb - File holds neither the patched nor the original content. Cannot safely roll back.

A file that holds neither content was edited, or is another build's; nothing here can restore it:
  Run `bundle pristine rack` (or `gem pristine rack`) to get the published files back, then roll back again.
  Or drop the record and leave the files as they are: `socket-patch remove <purl> --skip-rollback`.
Error: Some patches could not be rolled back.

Root cause

The release-variant loop of apply (gem and PyPI) fails a primary copy
that no record matches, but never marked the base's records as seen on
disk and pushed no result for the copy. The records fell through to the
"unmatched" list, where a Gemfile.lock that resolves the gem turns them
into the calm lockfile-only skip. The other two messages were written for
a different state: the --check remedy assumes apply heals every drift,
and rollback's HashMismatch text assumes the file was patched first.

What changed

  • apply.rs: a copy that holds a patched file with content no record was
    made for gets its own failed result (no_matching_variant, base purl),
    and the base's records are marked matched. A copy that holds none of
    the files its records are matched by is unchanged
    (package_not_installed, exit 1): an existing JRuby test pins that a
    qualified record cannot be told to belong to such a copy.
  • apply --check: the no_matching_variant drift carries its own remedy
    and no longer triggers the closing apply line.
  • rollback: reworded HashMismatch message (core) and a ways-out block
    under Failed to roll back:. reinstall_remedy moved from
    agent_download to ui so rollback can use it without an import
    cycle.
  • CLI_CONTRACT.md: new no_matching_variant row for apply, and the
    --check, package_not_installed and rollback rows updated.

Reviewer notes

  • Two existing assertions changed in
    cli_gem_variant_mismatch_policy.rs. They pinned "no failed event"
    for a no-variant miss, to tell it apart from a variant that was tried
    and refused. That is the reported bug: the only event was
    package_not_installed. They now require one failed /
    no_matching_variant event (a tried-and-refused variant stays
    apply_failed) and no package_not_installed. The fail-closed
    assertions (exit 1, file bytes untouched) are unchanged.
  • The rollback wording change applies to every ecosystem; four tests that
    quoted the old sentence were updated.
  • scan / get --mode agent should pick the new code up through the
    nested apply's failures (a base-purl failure covers a qualified record
    in apply_key_covers). That is from reading the code; no test here
    drives it through scan.

Evidence

Red first (commit d631e8f, tests only, against unfixed main): the four
apply / --check tests in gem_unmatched_copy_reporting.rs failed; the
rollback test was run red before its fix as well.

Check Result
cargo clippy --locked -p socket-patch-core -p socket-patch-cli --all-features -- -D warnings (CI's command) clean
cargo fmt --check on both crates clean
cargo test -p socket-patch-core 9225 passed, 0 failed
cargo test -p socket-patch-cli --lib plus 25 suites (apply, rollback, remove, get, scan, vex, the in-process gem / PyPI / rollback / get suites, e2e_gem_native_ext_build, command_module_layering, remedy_commands_parse, …) 2273 passed, 0 failed
cargo test -p socket-patch-cli --test e2e_gem_native_ext_build -- --ignored (real Ruby 3.3.6, Bundler 4.0.18) 11 passed

The remaining CLI suites were not run locally: building every test
target exhausted the sandbox disk. An earlier full run, before the review
fixes, got through 60 test binaries and failed only the four tests the
review fixes address. CI runs the rest.

Which test covers what (all in
crates/socket-patch-cli/tests/apply/gem_unmatched_copy_reporting.rs):

  • human apply output: edited_gem_is_reported_as_installed_with_other_content
  • apply --json: edited_gem_is_a_failed_event_not_a_not_installed_skip
  • apply --check: check_does_not_offer_apply_for_a_copy_apply_leaves,
    check_keeps_the_apply_remedy_for_a_drift_apply_heals
  • rollback / remove: rollback_names_the_ways_out_of_a_copy_it_refuses
    (also shows the rollback passing once the published file is back)

Review findings not fixed

/code-review high reported nine findings; seven are fixed in cb3d8b0.
Two are left:

  • apply --check on a bare gem record beside a second build of the
    version
    still reports hash_mismatch with the apply remedy, while
    apply now reports that copy as no_matching_variant. This is
    pre-existing and sits in how --check groups unqualified records, a
    different code path from this fix. Follow-up.
  • Summary count for a base with several records: the copy is one
    failed result for the whole base, so 0 of 3 targeted patches … 1 failed leaves two records in no bucket. Counting per record would key
    events by variant purl, which conflicts with the base-purl key
    --check uses. Documented on NoVariantCopy.

Follow-ups

  • The Maven loop (apply_maven_base) has its own "no matching variant
    found" branch with the old reporting. Not touched here.

https://claude.ai/code/session_011f9aeNixDrp2Etjezc2gZq


Generated by Claude Code

Refs #2231.

Assisted-by: Claude Code:claude-fable-5-2
Failing tests for #2231: an installed gem whose file matches neither
hash of its qualified record is reported by apply as not installed
(lockfile-only note, package_not_installed, "not found on disk"), and
apply --check offers an apply that cannot change the copy.

Assisted-by: Claude Code:claude-fable-5-2
A gem or PyPI copy whose files match no manifest record of its release
is left untouched, as before, but apply never marked its records as
seen on disk. They fell through to the unmatched list, so the run
called an installed gem lockfile-only, package_not_installed and
"not found on disk".

apply now gives the copy its own failed result (no_matching_variant,
keyed by the base purl, as apply --check reports it) whose text names
the copy, the file that differs and, for a gem, bundle pristine. The
records of the release are no longer reported as not installed.

apply --check no longer ends with an apply remedy that cannot change
such a copy. rollback no longer says a file at neither hash was
"modified after patching", and names the ways out.

Fixes #2231.

Assisted-by: Claude Code:claude-fable-5-2
Review fixes for the no-matching-variant report:

- Only a copy that holds a file of the patch with other content is
  reported as installed (no_matching_variant). A copy that holds none
  of the files its records are matched by stays package_not_installed,
  as the JRuby missing-file test pins.
- Build the apply --check remedy command before quoting it, so the
  remedy-command scan reads a real command.
- Move reinstall_remedy to ui: rollback importing it from
  agent_download made the two command modules import each other.
- The rollback message names no cause: a file at neither hash may be
  another build's, not an edit.

Assisted-by: Claude Code:claude-fable-5-2
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 25, 2026 10:15
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Patching behavior on disk is unchanged; risk is limited to CLI messages, JSON errorCodes, and rollback diagnostics.

Overview
Reporting fix (#2231): when an installed gem (or release-variant copy) is on disk but its files match no manifest variant—edited locally or another build—commands now describe that state consistently instead of treating it as absent or “modified after patching.”

apply / nested agent apply emit a dedicated failed event with no_matching_variant (base purl), with messages that name the copy, differing file(s), and gem reinstall (bundle pristine / gem pristine). Copies with patch files but wrong content are no longer counted as package_not_installed.

apply --check adds per-drift remedy text for no_matching_variant and omits the generic “run socket-patch apply to regenerate” footer when that is the only healable drift (since apply leaves those copies untouched).

rollback uses a neutral “holds neither patched nor original content” message (core NEITHER_CONTENT) and prints ways out: reinstall then retry rollback, or remove --skip-rollback. reinstall_remedy moves to ui for shared use.

Docs (CLI_CONTRACT.md) and tests (gem_unmatched_copy_reporting, updated variant-mismatch and rollback assertions) pin the new contract.

Reviewed by Cursor Bugbot for commit cb3d8b0. 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.

✅ Bugbot reviewed your changes and found no new issues!

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

Reviewed by Cursor Bugbot for commit cb3d8b0. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does. Fixes #2231. When an installed gem (or PyPI) copy holds a patched file whose content matches no release variant in the manifest (an edited copy, or another build), apply, scan, apply --check, rollback and remove now say so and name the way out (bundle pristine <gem>), instead of reporting the gem as not installed or as "modified after patching". No patching behavior changes: the same copies are left untouched and the same runs exit 1.

Risk: low. It changes messages and JSON codes only. One JSON change is visible: such a copy is now failed / no_matching_variant keyed by the base purl, where it was skipped / package_not_installed. CLI_CONTRACT.md documents the change. The core rollback message text changes for every ecosystem (NEITHER_CONTENT), so anyone matching on the old string would notice.

Look here

  • apply.rs:3970: a copy with other content now pushes its own failed result and marks the base's records as matched, so they no longer fall through to the lockfile-only "not installed" skip.
  • apply.rs:4951: no_variant_reason finds the file that holds neither side, using the same discriminating file the apply loop matches by.
  • apply.rs:696: apply --check drops its closing "run apply" remedy when every drift is one that apply cannot heal.
  • rollback.rs:288: the new ways-out block after Failed to roll back:.
  • core rollback.rs:57: the reworded HashMismatch message.

Verified. I read the whole diff against the description and #2231. A copy that holds none of the files its records are matched by still stays package_not_installed. The new failed result is keyed by the base purl, and I checked its consumers (the summary tally, restates_a_failure, the JSON event path) for double counting or lookups by manifest key; none are affected. The five new integration tests in tests/apply/gem_unmatched_copy_reporting.rs assert the new text and codes, so they would fail without the change. CHANGELOG.md is untouched. CI on cb3d8b0: ci-ok and clippy pass, and no check fails. Bugbot found no issues, and no review thread is open.

Changes I made. None.

Open questions. One nit, not blocking: the rollback hint prints a literal socket-patch remove <purl> --skip-rollback placeholder, although the failed purls appear just above it.


Generated by Claude Code

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; no major findings.

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

3 participants