Skip to content

Commit 53a6a67

Browse files
committed
Judge every installed copy of the native gem
Review of the skip found two cases it should not cover. The agent verdict read one installed copy, where vex and apply --check judge every copy, so an unpatched copy in the tree of another Ruby was skipped as patched. And a hosted pin counted on its uuid alone, so a pin over an install that is still the upstream gem closed the scan with "Nothing left to patch". The skip now needs every installed copy of the gem to hold the patch: a hosted copy by its files, a copy patched in place by its rebuilt extension as well. Otherwise the refusal stands as before. The second place that raises native_extensions_unsupported uses the constant. Refs #2269 Assisted-by: Claude Code:claude-fable-5-2
1 parent 84a1328 commit 53a6a67

7 files changed

Lines changed: 141 additions & 80 deletions

File tree

‎crates/socket-patch-cli/CLI_CONTRACT.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2515,7 +2515,7 @@ command's integration tests check its output.
25152515
| `vendor_unsupported_ecosystem` | `skipped` | vendor: no vendor backend for this purl's ecosystem (jsr). |
25162516
| `already_vendored` | `skipped` | vendor: artifact + wiring already in sync for this patch uuid. |
25172517
| `vendor_gem_version_not_locked` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (gem): no section of the project's `Gemfile.lock` resolves the gem at this version (typically a gem Ruby ships in the gem home, or another project's copy in a shared gem home, which the crawl of that gem home returns). Decided before any download: the patch is not fetched, the Gemfile and lock stay byte-identical, and nothing is written. Human: `[skip] <purl> (vendor_gem_version_not_locked): …` in the download phase of `scan` / `get` (both identifier paths) and in their dry run, `Skipping <purl>: …` in `vendor`. The vendored twin of `redirect_gem_version_not_locked`. |
2518-
| `vendor_gem_patched_in_other_mode` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (gem): the gem builds a native extension, which vendored mode refuses (`native_extensions_unsupported`), and it already holds the selected patch in a mode that refusal names. Either `Gemfile.lock` still pins that patch hosted, or the manifest records it and the installed copy holds it as agent mode leaves it (the verdict `vex` and `apply --check` use). The reason names the mode and the patch uuid. Nothing is written: the hosted pin, the manifest record and the installed copy stay as they were. Human: `Skipping <purl>: <gem> builds a native extension, which vendored mode cannot vendor; it stays patched in hosted mode (patch <uuid>)`. The closing totals of `scan` count the gem as patched. A native gem that holds another patch, or none, is still refused with `native_extensions_unsupported` (exit 1). A dry run of `scan` / `get` previews neither the refusal nor this skip. |
2518+
| `vendor_gem_patched_in_other_mode` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (gem): the gem builds a native extension, which vendored mode refuses (`native_extensions_unsupported`), and it already holds the selected patch in a mode that refusal names. Either `Gemfile.lock` still pins that patch hosted, or the manifest records it for agent mode; and every installed copy of the gem that the crawl finds holds the patch (a hosted copy by its files; a copy patched in place as `vex` and `apply --check` judge it, its rebuilt extension included). The reason names the mode and the patch uuid. Nothing is written: the hosted pin, the manifest record and the installed copy stay as they were. Human: `Skipping <purl>: <gem> builds a native extension, which vendored mode cannot vendor; it stays patched in hosted mode (patch <uuid>)`. The closing totals of `scan` count the gem as patched. A native gem that holds another patch or none, or with an installed copy that does not hold the patch (a hosted pin whose gem was not reinstalled, an unpatched copy in the tree of another Ruby), is still refused with `native_extensions_unsupported` (exit 1). A dry run of `scan` / `get` previews neither the refusal nor this skip. |
25192519
| `vendor_maven_version_not_selected` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (Maven): no module of the build resolves the artifact at the patch's base version; the reason names the versions Maven selects instead (a nearer declaration, the project's own declaration or management, an `<exclusion>`, a version range), or says that no module resolves the artifact. Decided before any download: the patch is not fetched and no file is written, except that a pin an earlier run wrote for that version is taken out of the poms (the reason says so), since it would keep forcing a version the build no longer selects. Human: `[skip] <purl> (vendor_maven_version_not_selected): …` in the download phase of `scan` / `get` and in their dry run, `Skipping <purl>: …` in `vendor`. The vendored twin of `redirect_maven_version_not_selected`. See [Maven version selection](#maven-version-selection-hosted-and-vendored-maven). |
25202520
| `vendor_maven_selection_undetermined` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (Maven): what the build selects for the artifact cannot be read (the reason says why: typically a pom the local repository does not hold, because the project was never resolved) and no declaration names the patch's base version, so nothing is pinned on a guess and no file is written. The reason carries the remedy that fits its cause (resolve the project once and re-run when a pom is missing from the local repository; otherwise declare the artifact at the base version). The vendored twin of `redirect_maven_selection_undetermined`. |
25212521
| `vendor_maven_pin_would_move_module` | `skipped` (benign; exit 0) | vendor / scan / get `--mode vendored` (Maven reactor): the only poms that resolve the artifact at the patch's base version are parent poms above a module that resolves another version. A `<dependencyManagement>` pin in the parent is inherited by that module and would move it, and Maven has no management that reaches a parent and not its children, so nothing is pinned and no file is written; the reason names the parent, the module and its version. When a sibling module does select the base version it is pinned and the same sentence is the `vendor_jvm_degraded` reason `pin_would_move_module`. |

‎crates/socket-patch-cli/src/commands/vendor.rs‎

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ use crate::commands::vex::{
6363
generate_vex_from_manifest_path, generate_vex_without_manifest, ManifestlessVex, VexEmbedArgs,
6464
};
6565
use crate::ecosystem_dispatch::{
66-
find_packages_for_rollback_reusing, npm_paths_by_identity, npm_paths_by_identity_in,
67-
partition_purls, NpmCrawlSnapshot,
66+
find_all_packages_for_purls, find_packages_for_rollback_reusing, npm_paths_by_identity,
67+
npm_paths_by_identity_in, partition_purls, NpmCrawlSnapshot,
6868
};
6969
use crate::json_envelope::{
7070
Command, Envelope, EnvelopeError, PatchAction, PatchEvent, RunWarning, Status, VexSummary,
@@ -207,38 +207,67 @@ async fn keep_older_vendored_patch(
207207
})
208208
}
209209

210-
/// The mode in which the gem `candidate` already holds the patch the run
211-
/// selected for it (`uuid`), for a gem the vendored backend refused for
212-
/// its native extension: `hosted` when the lock still pins that patch
210+
/// The mode in which the gem `candidate` already holds `record`, the
211+
/// patch the run selected for it, for a gem the vendored backend refused
212+
/// for its native extension: `hosted` when the lock still pins that patch
213213
/// (`live_pin`, the candidate's hosted pin unless this run restored its
214-
/// upstream entry), `agent` when the manifest records it and the
215-
/// installed copy at `installed` holds it as agent mode leaves it (the
216-
/// verdict `vex` attests the gem by). `None` when it holds another patch
217-
/// or none: the refusal then stands.
214+
/// upstream entry), `agent` when the manifest records it. Either way
215+
/// every installed copy of the gem must hold the patch, as `vex` and
216+
/// `apply --check` judge it: a hosted copy by its files, a copy patched
217+
/// in place by its rebuilt extension as well. `None` when the gem holds
218+
/// another patch or none, or some installed copy does not hold this one
219+
/// (a hosted pin whose gem was never reinstalled, say): the refusal then
220+
/// stands, and the remedy it names puts the patch in place.
218221
async fn gem_patch_held_in(
219222
common: &GlobalArgs,
220223
candidate: &str,
221-
uuid: &str,
224+
record: &PatchRecord,
222225
live_pin: Option<&HostedPin>,
223-
installed: &Path,
224226
) -> Option<&'static str> {
225-
if live_pin.is_some_and(|pin| pin.uuid == uuid) {
226-
return Some("hosted");
227+
let hosted = live_pin.is_some_and(|pin| pin.uuid == record.uuid);
228+
let in_place = match hosted {
229+
true => None,
230+
false => read_manifest(&common.resolved_manifest_path())
231+
.await
232+
.ok()
233+
.flatten()?
234+
.patches
235+
.into_iter()
236+
.find(|(purl, r)| r.uuid == record.uuid && PurlKey::same(purl, candidate)),
237+
};
238+
if !hosted && in_place.is_none() {
239+
return None;
227240
}
228-
let manifest = read_manifest(&common.resolved_manifest_path())
229-
.await
230-
.ok()
231-
.flatten()?;
232-
for (purl, record) in &manifest.patches {
233-
if record.uuid == uuid
234-
&& PurlKey::same(purl, candidate)
235-
&& socket_patch_core::vex::verify::gem_copy_patched_in_place(purl, installed, record)
241+
// Every installed copy, not the one the loop vendors from.
242+
let purls = [candidate.to_string()];
243+
let copies: Vec<std::path::PathBuf> = find_all_packages_for_purls(
244+
&partition_purls(&purls, None),
245+
&common.crawler_options(),
246+
true,
247+
)
248+
.await
249+
.into_iter()
250+
.filter(|(purl, _)| PurlKey::same(purl, candidate))
251+
.flat_map(|(_, paths)| paths)
252+
.collect();
253+
if copies.is_empty() {
254+
return None;
255+
}
256+
match in_place {
257+
Some((purl, recorded)) => {
258+
socket_patch_core::vex::verify::gem_copies_patched_in_place(&purl, &copies, &recorded)
236259
.await
237-
{
238-
return Some("agent");
260+
.then_some("agent")
261+
}
262+
None => {
263+
for copy in &copies {
264+
socket_patch_core::vex::verify::verify_patch_record(copy, record)
265+
.await
266+
.ok()?;
267+
}
268+
Some("hosted")
239269
}
240270
}
241-
None
242271
}
243272

244273
/// A backend refusal as the run reports it: a gem refused for its native
@@ -248,23 +277,22 @@ async fn gem_patch_held_in(
248277
async fn settle_gem_refusal(
249278
common: &GlobalArgs,
250279
candidate: &str,
251-
uuid: &str,
280+
record: &PatchRecord,
252281
live_pin: Option<&HostedPin>,
253-
installed: &Path,
254282
code: &'static str,
255283
detail: String,
256284
) -> (&'static str, String) {
257285
if code != vendor::gem::NATIVE_EXTENSIONS_UNSUPPORTED {
258286
return (code, detail);
259287
}
260-
match gem_patch_held_in(common, candidate, uuid, live_pin, installed).await {
288+
match gem_patch_held_in(common, candidate, record, live_pin).await {
261289
Some(mode) => {
262290
let purl = normalize_purl(candidate);
263291
let name = socket_patch_core::utils::purl::purl_name_version(&purl)
264292
.map_or(&*purl, |(name, _)| name);
265293
(
266294
vendor::gem::GEM_PATCHED_IN_OTHER_MODE,
267-
vendor::gem::patched_in_other_mode_detail(name, mode, uuid),
295+
vendor::gem::patched_in_other_mode_detail(name, mode, &record.uuid),
268296
)
269297
}
270298
None => (code, detail),
@@ -4365,16 +4393,8 @@ pub(crate) async fn vendor_records_reusing(
43654393
);
43664394
}
43674395
Some(VendorOutcome::Refused { code, detail }) => {
4368-
let (code, detail) = settle_gem_refusal(
4369-
common,
4370-
candidate,
4371-
&record.uuid,
4372-
live_pin,
4373-
pkg_source.path(),
4374-
code,
4375-
detail,
4376-
)
4377-
.await;
4396+
let (code, detail) =
4397+
settle_gem_refusal(common, candidate, record, live_pin, code, detail).await;
43784398
if refusal_is_benign(code) {
43794399
// An expected skip, not an error: informational.
43804400
if !common.silent && !common.json {

‎crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs‎

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5986,31 +5986,58 @@ async fn vendored_scan_skips_a_native_gem_that_holds_the_patch_in_agent_mode() {
59865986
assert!(shown.contains("patched in agent mode"), "{shown}");
59875987
}
59885988

5989-
/// The other side of #2269: a native gem that holds the patch in no mode
5990-
/// is still refused, and the run still fails. Neither an upstream install
5991-
/// with no record, nor a manifest record whose patch is not in the
5992-
/// installed copy, counts as patched.
5989+
/// The other side of #2269: a native gem that does not hold the patch
5990+
/// everywhere is still refused, and the run still fails. None of these
5991+
/// counts as patched: an upstream install with no record; a manifest
5992+
/// record whose patch is not in the installed copy; a patched copy beside
5993+
/// an unpatched one in the tree of another Ruby (`vex` and `apply
5994+
/// --check` judge every copy); a hosted pin whose installed copy is still
5995+
/// the upstream gem.
59935996
#[tokio::test(flavor = "multi_thread")]
59945997
async fn vendored_scan_still_refuses_a_native_gem_that_is_not_patched() {
5995-
for recorded in [false, true] {
5998+
for case in [
5999+
"no record",
6000+
"record not applied",
6001+
"one of two copies unpatched",
6002+
"hosted pin over an upstream install",
6003+
] {
59966004
let server = MockServer::start().await;
59976005
let tmp = tempfile::tempdir().unwrap();
59986006
let proj = tmp.path().join("proj");
59996007
let home = vendorable_project(&server, &proj).await;
6000-
let gem_dir = if recorded {
6001-
let gem_dir = agent_patched_install(&proj, &home);
6002-
// The record is there; the patch is not in the copy.
6003-
std::fs::write(gem_dir.join("lib").join("stale_probe_gem.rb"), UPSTREAM_LIB).unwrap();
6004-
gem_dir
6005-
} else {
6006-
install_real_gem(&home, UPSTREAM_LIB)
6008+
let uri = server.uri();
6009+
let (gem_dir, left) = match case {
6010+
"no record" => (install_real_gem(&home, UPSTREAM_LIB), UPSTREAM_LIB),
6011+
"record not applied" => {
6012+
let gem_dir = agent_patched_install(&proj, &home);
6013+
std::fs::write(gem_dir.join("lib").join("stale_probe_gem.rb"), UPSTREAM_LIB)
6014+
.unwrap();
6015+
(gem_dir, UPSTREAM_LIB)
6016+
}
6017+
"one of two copies unpatched" => {
6018+
let other = home.with_file_name("3.2.0");
6019+
install_real_gem(&other, UPSTREAM_LIB);
6020+
declare_native_extension(&other);
6021+
(agent_patched_install(&proj, &home), PATCHED_LIB)
6022+
}
6023+
_ => {
6024+
let gem_dir = install_real_gem(&home, UPSTREAM_LIB);
6025+
let (code, _, stderr) =
6026+
common::run_with_env(&proj, &scan_args(&proj, &uri, &["--json"]), &[]);
6027+
assert_eq!(code, 0, "{case}: hosted scan:\n{stderr}");
6028+
std::fs::write(proj.join("Gemfile.lock"), converged_lock(&uri)).unwrap();
6029+
// The gem as it is before the pinned build is installed.
6030+
std::fs::write(gem_dir.join("lib").join("stale_probe_gem.rb"), UPSTREAM_LIB)
6031+
.unwrap();
6032+
(gem_dir, UPSTREAM_LIB)
6033+
}
60076034
};
60086035
declare_native_extension(&home);
60096036
let pair_before = read_pair(&proj);
60106037

60116038
let (code, stdout, stderr) = unwind_run(
60126039
&proj,
6013-
&server.uri(),
6040+
&uri,
60146041
&["scan", "--mode", "vendored", "--yes", "--json"],
60156042
);
60166043
let env = common::parse_json_envelope(&stdout);
@@ -6019,14 +6046,14 @@ async fn vendored_scan_still_refuses_a_native_gem_that_is_not_patched() {
60196046
events
60206047
.iter()
60216048
.any(|e| e == "failed/native_extensions_unsupported"),
6022-
"recorded={recorded}: {events:?}\n{env}\nstderr:\n{stderr}"
6049+
"{case}: {events:?}\n{env}\nstderr:\n{stderr}"
60236050
);
60246051
assert!(
60256052
!env.to_string().contains(PATCHED_IN_OTHER_MODE),
6026-
"recorded={recorded}: {env}"
6053+
"{case}: {env}"
60276054
);
6028-
assert_ne!(code, 0, "recorded={recorded}: {env}");
6029-
assert_eq!(read_pair(&proj), pair_before, "recorded={recorded}");
6030-
assert_eq!(installed_lib(&gem_dir), UPSTREAM_LIB, "recorded={recorded}");
6055+
assert_ne!(code, 0, "{case}: {env}");
6056+
assert_eq!(read_pair(&proj), pair_before, "{case}");
6057+
assert_eq!(installed_lib(&gem_dir), left, "{case}");
60316058
}
60326059
}

‎crates/socket-patch-core/src/vendor/gem.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,17 @@ fn version_not_locked_detail(name: &str, version: &str) -> String {
319319
}
320320

321321
/// Refusal code of a gem that builds a native extension and is locked for
322-
/// the `ruby` platform alone: Bundler builds nothing for a path-sourced
323-
/// gem, and the refusal names the modes that do build the extension
324-
/// ([`native_extensions_remedy`]).
322+
/// the `ruby` platform alone, as its installed gemspec or the stub the
323+
/// patch service serves declares it: Bundler builds nothing for a
324+
/// path-sourced gem, and the refusal names the modes that do build the
325+
/// extension ([`native_extensions_remedy`]).
325326
pub const NATIVE_EXTENSIONS_UNSUPPORTED: &str = "native_extensions_unsupported";
326327

327328
/// Skip code of a gem refused with [`NATIVE_EXTENSIONS_UNSUPPORTED`] that
328329
/// already holds the selected patch in a mode the refusal names: the
329-
/// lock pins that patch hosted, or agent mode patched the installed copy
330-
/// in place. An expected skip, not a failure: the remedy the refusal
330+
/// lock pins that patch hosted, or the manifest records it for agent
331+
/// mode, and every installed copy holds it. An expected skip, not a
332+
/// failure: the remedy the refusal
331333
/// offers is already in place, and a run that asked for it again would
332334
/// fail every time (#2269).
333335
pub const GEM_PATCHED_IN_OTHER_MODE: &str = "vendor_gem_patched_in_other_mode";
@@ -1596,7 +1598,7 @@ fn refuse_unusable_stub(
15961598
// sources, so a native gem would install and then fail at `require` time.
15971599
if gemspec_declares_extensions(stub_text) {
15981600
return Err(policy.hard(
1599-
"native_extensions_unsupported",
1601+
NATIVE_EXTENSIONS_UNSUPPORTED,
16001602
format!(
16011603
"the served stub gemspec for {subject} declares native extensions; bundler does \
16021604
not build extensions for path-sourced gems. {}",

‎crates/socket-patch-core/src/vendor/refusals.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,13 @@ mod tests {
461461
/// is still a failure.
462462
#[test]
463463
fn a_gem_patched_in_another_mode_is_a_skip_and_its_refusal_is_not() {
464-
assert!(refusal_is_benign(gem::GEM_PATCHED_IN_OTHER_MODE));
465-
assert!(!refusal_is_benign(gem::NATIVE_EXTENSIONS_UNSUPPORTED));
466-
let detail = gem::patched_in_other_mode_detail("json", "hosted", "u-1");
464+
assert!(refusal_is_benign(
465+
crate::vendor::gem::GEM_PATCHED_IN_OTHER_MODE
466+
));
467+
assert!(!refusal_is_benign(
468+
crate::vendor::gem::NATIVE_EXTENSIONS_UNSUPPORTED
469+
));
470+
let detail = crate::vendor::gem::patched_in_other_mode_detail("json", "hosted", "u-1");
467471
assert!(
468472
detail.contains("json builds a native extension")
469473
&& detail.contains("patched in hosted mode (patch u-1)"),

‎crates/socket-patch-core/src/vex/verify.rs‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,16 +556,22 @@ async fn verify_every_gem_copy(
556556
Ok(verdict)
557557
}
558558

559-
/// Whether the installed gem copy at `pkg_path` holds `record`'s patch as
560-
/// agent mode leaves it: the files of the record the copy takes are all
561-
/// patched, there is at least one, and a native extension the record
562-
/// changes was rebuilt from them ([`verify_every_gem_copy`], the verdict
563-
/// `vex` and `apply --check` attest an agent-patched gem by).
564-
pub async fn gem_copy_patched_in_place(purl: &str, pkg_path: &Path, record: &PatchRecord) -> bool {
565-
matches!(
566-
verify_every_gem_copy(purl, &[pkg_path.to_path_buf()], record).await,
567-
Ok(GemVerdict::Patched)
568-
)
559+
/// Whether the installed copies of a gem (`paths`, every copy the crawl
560+
/// found) hold `record`'s patch as agent mode leaves it: each copy's
561+
/// files of the record are patched, some copy takes one, and a native
562+
/// extension the record changes was rebuilt from them
563+
/// ([`verify_every_gem_copy`], the verdict `vex` and `apply --check`
564+
/// attest an agent-patched gem by). No copy at all holds nothing.
565+
pub async fn gem_copies_patched_in_place(
566+
purl: &str,
567+
paths: &[PathBuf],
568+
record: &PatchRecord,
569+
) -> bool {
570+
!paths.is_empty()
571+
&& matches!(
572+
verify_every_gem_copy(purl, paths, record).await,
573+
Ok(GemVerdict::Patched)
574+
)
569575
}
570576

571577
/// Why the installed copies of a gem pass ([`verify_every_gem_copy`]).

0 commit comments

Comments
 (0)