From effe3c52521442de614ae4bee48a235e1e48675c Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 05:25:23 +0000 Subject: [PATCH 1/4] Start fix for vendored re-scan of a native gem Refs #2269. A vendored scan fails on every run for a gem it refuses to vendor even after the gem holds its patch in the mode the refusal names. Assisted-by: Claude Code:claude-fable-5-2 From 808c22bd6d126ae18ed21409bd5dcc8cc6cedd82 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 05:30:17 +0000 Subject: [PATCH 2/4] Test vendored re-scan of a patched native gem A gem with a native extension that holds the selected patch in hosted or agent mode should be a skip of scan --mode vendored, not a failure on every run (#2269). Both tests fail today; the third pins that a native gem patched in no mode is still refused. Assisted-by: Claude Code:claude-fable-5-2 --- .../tests/e2e_redirect_gem_stale_install.rs | 205 ++++++++++++++++++ 1 file changed, 205 insertions(+) diff --git a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs index c7e706537..ac1080652 100644 --- a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs +++ b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs @@ -5810,3 +5810,208 @@ async fn vendored_takeover_of_a_superseded_native_gem_names_the_extension() { ); assert!(!proj.join(".socket").join("vendor").exists()); } + +// ── a gem vendored mode refuses, patched in the mode its refusal names ── + +/// The skip of a gem vendored mode cannot vendor that already holds the +/// selected patch in hosted or agent mode. +const PATCHED_IN_OTHER_MODE: &str = "vendor_gem_patched_in_other_mode"; + +/// Make the installed fixture gem one that builds a native extension, as +/// its gemspec in `home` declares it. +fn declare_native_extension(home: &Path) { + let spec = home + .join("specifications") + .join(format!("{DEP}-{DEP_VERSION}.gemspec")); + let text = std::fs::read_to_string(&spec).unwrap(); + assert!(text.contains(" s.require_paths"), "premise: {text}"); + std::fs::write( + &spec, + text.replace( + " s.require_paths", + " s.extensions = [\"ext/probe/extconf.rb\".freeze]\n s.require_paths", + ), + ) + .unwrap(); +} + +/// The Gemfile and the lock of `proj`. +fn read_pair(proj: &Path) -> (String, String) { + ( + std::fs::read_to_string(proj.join("Gemfile")).unwrap(), + std::fs::read_to_string(proj.join("Gemfile.lock")).unwrap(), + ) +} + +/// The patch API of [`UUID`] with its vendored artifacts and the upstream +/// gem, and a project whose pair locks the fixture gem. Returns the gem +/// home. +async fn vendorable_project(server: &MockServer, proj: &Path) -> PathBuf { + let (patched, upstream) = (real_gem(PATCHED_LIB), real_gem(UPSTREAM_LIB)); + mount_api_for(server, None, &sha256_of(&patched)).await; + serve_patched_gem(server, patched.clone()).await; + serve_upstream_gem(server, &upstream).await; + mount_vendored_grant(server, &patched).await; + std::fs::create_dir_all(proj).unwrap(); + write_manifest_pair(proj); + project_home(proj) +} + +/// #2269: vendored mode refuses a gem that builds a native extension and +/// names hosted mode as the way to patch it. Once the gem holds the same +/// patch in hosted mode, a later `scan --mode vendored` has nothing left +/// to do for it: the gem is a skip that says where its patch is, the run +/// exits 0, the closing totals count the gem as patched, and the hosted +/// pin stays as it was. +#[tokio::test(flavor = "multi_thread")] +async fn vendored_scan_skips_a_native_gem_that_holds_the_patch_in_hosted_mode() { + let server = MockServer::start().await; + let tmp = tempfile::tempdir().unwrap(); + let proj = tmp.path().join("proj"); + let home = vendorable_project(&server, &proj).await; + let gem_dir = install_real_gem(&home, UPSTREAM_LIB); + let uri = server.uri(); + let (code, _, stderr) = common::run_with_env(&proj, &scan_args(&proj, &uri, &["--json"]), &[]); + assert_eq!(code, 0, "hosted scan:\n{stderr}"); + std::fs::write(proj.join("Gemfile.lock"), converged_lock(&uri)).unwrap(); + assert_eq!(installed_lib(&gem_dir), PATCHED_LIB, "premise"); + declare_native_extension(&home); + let pair_before = read_pair(&proj); + let home_before = snapshot(&home); + + for _ in 0..2 { + let (code, stdout, stderr) = unwind_run( + &proj, + &uri, + &["scan", "--mode", "vendored", "--yes", "--json"], + ); + let env = common::parse_json_envelope(&stdout); + assert_eq!( + gem_events(&env), + [format!("skipped/{PATCHED_IN_OTHER_MODE}")], + "{env}\nstderr:\n{stderr}" + ); + assert_eq!(code, 0, "{env}\nstderr:\n{stderr}"); + assert_eq!(env["status"], "success", "{env}"); + let skip = env["events"] + .as_array() + .into_iter() + .flatten() + .find(|e| e["errorCode"] == PATCHED_IN_OTHER_MODE) + .expect("the skip"); + let why = skip["error"].as_str().unwrap_or_default(); + assert!( + why.contains("hosted mode") && why.contains("native extension"), + "{skip}" + ); + assert_eq!(read_pair(&proj), pair_before, "the hosted pin stays"); + assert!(!proj.join(".socket").join("vendor").exists()); + assert!(snapshot(&home) == home_before, "the install is untouched"); + } + + // A human run: no error, and nothing of the gem is left to patch. + let (code, stdout, stderr) = unwind_run(&proj, &uri, &["scan", "--mode", "vendored", "--yes"]); + assert_eq!(code, 0, "{stdout}\n{stderr}"); + let shown = format!("{stdout}\n{stderr}"); + assert!(!shown.contains("Error"), "{shown}"); + assert!(!shown.contains("left to patch"), "{shown}"); + assert!(!shown.contains("failed"), "{shown}"); + assert!(shown.contains("patched in hosted mode"), "{shown}"); + assert_eq!(read_pair(&proj), pair_before, "the hosted pin stays"); +} + +/// #2269, the agent half: the same gem patched in place by agent mode +/// (the manifest records the patch and the installed copy holds it) is a +/// skip of the vendored scan, and its manifest record stays. (The scan's +/// dry run previews no native-extension refusal, so it is not held to +/// this.) +#[tokio::test(flavor = "multi_thread")] +async fn vendored_scan_skips_a_native_gem_that_holds_the_patch_in_agent_mode() { + let server = MockServer::start().await; + let tmp = tempfile::tempdir().unwrap(); + let proj = tmp.path().join("proj"); + let home = vendorable_project(&server, &proj).await; + let gem_dir = agent_patched_install(&proj, &home); + declare_native_extension(&home); + let uri = server.uri(); + let pair_before = read_pair(&proj); + let manifest_path = proj.join(".socket").join("manifest.json"); + let manifest_before = std::fs::read_to_string(&manifest_path).unwrap(); + + for extra in ["first run", "second run"] { + let args = ["scan", "--mode", "vendored", "--yes", "--json"]; + let (code, stdout, stderr) = unwind_run(&proj, &uri, &args); + let env = common::parse_json_envelope(&stdout); + let events = gem_events(&env); + assert!( + events.contains(&format!("skipped/{PATCHED_IN_OTHER_MODE}")) + && !events.iter().any(|e| e.starts_with("failed/")), + "{extra:?}: {events:?}\n{env}\nstderr:\n{stderr}" + ); + assert_eq!(code, 0, "{extra:?}: {env}\nstderr:\n{stderr}"); + assert!( + env.to_string().contains("patched in agent mode"), + "{extra:?}: {env}" + ); + assert_eq!(read_pair(&proj), pair_before, "{extra:?}"); + assert!(!proj.join(".socket").join("vendor").exists(), "{extra:?}"); + assert_eq!( + std::fs::read_to_string(&manifest_path).unwrap(), + manifest_before, + "{extra:?}: the agent record stays" + ); + assert_eq!(installed_lib(&gem_dir), PATCHED_LIB, "{extra:?}"); + } + + let (code, stdout, stderr) = unwind_run(&proj, &uri, &["scan", "--mode", "vendored", "--yes"]); + assert_eq!(code, 0, "{stdout}\n{stderr}"); + let shown = format!("{stdout}\n{stderr}"); + assert!(!shown.contains("Error"), "{shown}"); + assert!(!shown.contains("left to patch"), "{shown}"); + assert!(shown.contains("patched in agent mode"), "{shown}"); +} + +/// The other side of #2269: a native gem that holds the patch in no mode +/// is still refused, and the run still fails. Neither an upstream install +/// with no record, nor a manifest record whose patch is not in the +/// installed copy, counts as patched. +#[tokio::test(flavor = "multi_thread")] +async fn vendored_scan_still_refuses_a_native_gem_that_is_not_patched() { + for recorded in [false, true] { + let server = MockServer::start().await; + let tmp = tempfile::tempdir().unwrap(); + let proj = tmp.path().join("proj"); + let home = vendorable_project(&server, &proj).await; + let gem_dir = if recorded { + let gem_dir = agent_patched_install(&proj, &home); + // The record is there; the patch is not in the copy. + std::fs::write(gem_dir.join("lib").join("stale_probe_gem.rb"), UPSTREAM_LIB).unwrap(); + gem_dir + } else { + install_real_gem(&home, UPSTREAM_LIB) + }; + declare_native_extension(&home); + let pair_before = read_pair(&proj); + + let (code, stdout, stderr) = unwind_run( + &proj, + &server.uri(), + &["scan", "--mode", "vendored", "--yes", "--json"], + ); + let env = common::parse_json_envelope(&stdout); + let events = gem_events(&env); + assert!( + events + .iter() + .any(|e| e == "failed/native_extensions_unsupported"), + "recorded={recorded}: {events:?}\n{env}\nstderr:\n{stderr}" + ); + assert!( + !env.to_string().contains(PATCHED_IN_OTHER_MODE), + "recorded={recorded}: {env}" + ); + assert_ne!(code, 0, "recorded={recorded}: {env}"); + assert_eq!(read_pair(&proj), pair_before, "recorded={recorded}"); + assert_eq!(installed_lib(&gem_dir), UPSTREAM_LIB, "recorded={recorded}"); + } +} From 84a13288a833cae45a09fb71f0c8b876e5ca21b6 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 05:34:12 +0000 Subject: [PATCH 3/4] Skip a native gem patched in another mode Vendored mode refuses a gem that builds a native extension and names hosted or agent mode as the way to patch it. The vendor loop had no outcome for a gem that already holds the selected patch there, so every later scan --mode vendored raised the refusal again, counted the gem as failed and as left to patch, and exited 1. A gem refused with native_extensions_unsupported is now the skip vendor_gem_patched_in_other_mode when the lock still pins the same patch hosted, or the manifest records it and the installed copy holds it as agent mode leaves it. The closing totals count it as patched. A native gem that holds another patch, or none, is refused as before. Fixes #2269 Assisted-by: Claude Code:claude-fable-5-2 --- crates/socket-patch-cli/CLI_CONTRACT.md | 1 + .../socket-patch-cli/src/commands/vendor.rs | 85 ++++++++++++++++++- .../tests/e2e_redirect_gem_stale_install.rs | 29 +++++-- crates/socket-patch-core/src/vendor/gem.rs | 25 +++++- .../socket-patch-core/src/vendor/refusals.rs | 18 ++++ crates/socket-patch-core/src/vex/verify.rs | 12 +++ docs/ecosystems.md | 10 +++ 7 files changed, 171 insertions(+), 9 deletions(-) diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index 10b3aecec..2e7cb7cf2 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -2515,6 +2515,7 @@ command's integration tests check its output. | `vendor_unsupported_ecosystem` | `skipped` | vendor: no vendor backend for this purl's ecosystem (jsr). | | `already_vendored` | `skipped` | vendor: artifact + wiring already in sync for this patch uuid. | | `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] (vendor_gem_version_not_locked): …` in the download phase of `scan` / `get` (both identifier paths) and in their dry run, `Skipping : …` in `vendor`. The vendored twin of `redirect_gem_version_not_locked`. | +| `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 : builds a native extension, which vendored mode cannot vendor; it stays patched in hosted mode (patch )`. 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. | | `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 ``, 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] (vendor_maven_version_not_selected): …` in the download phase of `scan` / `get` and in their dry run, `Skipping : …` in `vendor`. The vendored twin of `redirect_maven_version_not_selected`. See [Maven version selection](#maven-version-selection-hosted-and-vendored-maven). | | `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`. | | `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 `` 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`. | diff --git a/crates/socket-patch-cli/src/commands/vendor.rs b/crates/socket-patch-cli/src/commands/vendor.rs index e428ac5be..12a06401f 100644 --- a/crates/socket-patch-cli/src/commands/vendor.rs +++ b/crates/socket-patch-cli/src/commands/vendor.rs @@ -207,6 +207,70 @@ async fn keep_older_vendored_patch( }) } +/// The mode in which the gem `candidate` already holds the patch the run +/// selected for it (`uuid`), for a gem the vendored backend refused for +/// its native extension: `hosted` when the lock still pins that patch +/// (`live_pin`, the candidate's hosted pin unless this run restored its +/// upstream entry), `agent` when the manifest records it and the +/// installed copy at `installed` holds it as agent mode leaves it (the +/// verdict `vex` attests the gem by). `None` when it holds another patch +/// or none: the refusal then stands. +async fn gem_patch_held_in( + common: &GlobalArgs, + candidate: &str, + uuid: &str, + live_pin: Option<&HostedPin>, + installed: &Path, +) -> Option<&'static str> { + if live_pin.is_some_and(|pin| pin.uuid == uuid) { + return Some("hosted"); + } + let manifest = read_manifest(&common.resolved_manifest_path()) + .await + .ok() + .flatten()?; + for (purl, record) in &manifest.patches { + if record.uuid == uuid + && PurlKey::same(purl, candidate) + && socket_patch_core::vex::verify::gem_copy_patched_in_place(purl, installed, record) + .await + { + return Some("agent"); + } + } + None +} + +/// A backend refusal as the run reports it: a gem refused for its native +/// extension that already holds the selected patch in hosted or agent +/// mode is the skip [`vendor::gem::GEM_PATCHED_IN_OTHER_MODE`] (#2269); +/// every other refusal is itself. +async fn settle_gem_refusal( + common: &GlobalArgs, + candidate: &str, + uuid: &str, + live_pin: Option<&HostedPin>, + installed: &Path, + code: &'static str, + detail: String, +) -> (&'static str, String) { + if code != vendor::gem::NATIVE_EXTENSIONS_UNSUPPORTED { + return (code, detail); + } + match gem_patch_held_in(common, candidate, uuid, live_pin, installed).await { + Some(mode) => { + let purl = normalize_purl(candidate); + let name = socket_patch_core::utils::purl::purl_name_version(&purl) + .map_or(&*purl, |(name, _)| name); + ( + vendor::gem::GEM_PATCHED_IN_OTHER_MODE, + vendor::gem::patched_in_other_mode_detail(name, mode, uuid), + ) + } + None => (code, detail), + } +} + /// The `vendor_dir_symlink_unsupported` detail when `purl`'s vendor dir /// (`.socket/vendor`, its ecosystem dir, or the `uuid` unit) is a link. fn linked_vendor_dir_refusal(project_root: &Path, purl: &str, uuid: &str) -> Option { @@ -3922,6 +3986,9 @@ pub(crate) async fn vendor_records_reusing( // does not vendor the purl, and the advisories that only hold // once it does (see `TakeoverUndo`). let mut takeover_undo: Option = None; + // The candidate's hosted pin while the lock still holds it: + // gone once this run's restore of its upstream entry stands. + let mut live_pin = hosted_pin_of(candidate); if let Some(pin) = hosted_pin_of(candidate) { let origins = crate::commands::hosted_unwind::patch_server_origins(common); let restore_opts = socket_patch_core::patch::redirect::upstream::RestoreOptions { @@ -4179,6 +4246,7 @@ pub(crate) async fn vendor_records_reusing( takeover_undo = Some(undo); } else { undo.settle(env, common, candidate, &mut vlt_takeover_targets); + live_pin = None; } } } @@ -4279,6 +4347,7 @@ pub(crate) async fn vendor_records_reusing( matches!(&outcome, Some(VendorOutcome::Done { entry, .. }) if entry.is_some()); if vendored || recorded || undo.savepoint.is_none() || group.is_none() { undo.settle(env, common, candidate, &mut vlt_takeover_targets); + live_pin = None; } else { undo.abandon(group.as_ref()); } @@ -4296,6 +4365,16 @@ pub(crate) async fn vendor_records_reusing( ); } Some(VendorOutcome::Refused { code, detail }) => { + let (code, detail) = settle_gem_refusal( + common, + candidate, + &record.uuid, + live_pin, + pkg_source.path(), + code, + detail, + ) + .await; if refusal_is_benign(code) { // An expected skip, not an error: informational. if !common.silent && !common.json { @@ -4767,7 +4846,11 @@ fn print_vendor_closing( .iter() .filter(|e| { e.action == PatchAction::Applied - || e.error_code.as_deref() == Some("already_vendored") + || matches!( + e.error_code.as_deref(), + // In place before the run, here or in another mode. + Some("already_vendored" | vendor::gem::GEM_PATCHED_IN_OTHER_MODE) + ) }) .filter_map(|e| e.purl.as_deref()) .collect(); diff --git a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs index ac1080652..ba52840a8 100644 --- a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs +++ b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs @@ -5886,10 +5886,11 @@ async fn vendored_scan_skips_a_native_gem_that_holds_the_patch_in_hosted_mode() &["scan", "--mode", "vendored", "--yes", "--json"], ); let env = common::parse_json_envelope(&stdout); - assert_eq!( - gem_events(&env), - [format!("skipped/{PATCHED_IN_OTHER_MODE}")], - "{env}\nstderr:\n{stderr}" + let events = gem_events(&env); + assert!( + events.contains(&format!("skipped/{PATCHED_IN_OTHER_MODE}")) + && !events.iter().any(|e| e.starts_with("failed/")), + "{events:?}\n{env}\nstderr:\n{stderr}" ); assert_eq!(code, 0, "{env}\nstderr:\n{stderr}"); assert_eq!(env["status"], "success", "{env}"); @@ -5899,7 +5900,7 @@ async fn vendored_scan_skips_a_native_gem_that_holds_the_patch_in_hosted_mode() .flatten() .find(|e| e["errorCode"] == PATCHED_IN_OTHER_MODE) .expect("the skip"); - let why = skip["error"].as_str().unwrap_or_default(); + let why = skip["reason"].as_str().unwrap_or_default(); assert!( why.contains("hosted mode") && why.contains("native extension"), "{skip}" @@ -5914,10 +5915,24 @@ async fn vendored_scan_skips_a_native_gem_that_holds_the_patch_in_hosted_mode() assert_eq!(code, 0, "{stdout}\n{stderr}"); let shown = format!("{stdout}\n{stderr}"); assert!(!shown.contains("Error"), "{shown}"); - assert!(!shown.contains("left to patch"), "{shown}"); + assert!(shown.contains("Nothing left to patch"), "{shown}"); assert!(!shown.contains("failed"), "{shown}"); assert!(shown.contains("patched in hosted mode"), "{shown}"); assert_eq!(read_pair(&proj), pair_before, "the hosted pin stays"); + + // `get --mode vendored` reads the same answer. + let (code, stdout, stderr) = unwind_run( + &proj, + &uri, + &["get", UUID, "--mode", "vendored", "--yes", "--json"], + ); + let env = common::parse_json_envelope(&stdout); + assert!( + gem_events(&env).contains(&format!("skipped/{PATCHED_IN_OTHER_MODE}")), + "{env}\nstderr:\n{stderr}" + ); + assert_eq!(code, 0, "{env}\nstderr:\n{stderr}"); + assert_eq!(read_pair(&proj), pair_before, "the hosted pin stays"); } /// #2269, the agent half: the same gem patched in place by agent mode @@ -5967,7 +5982,7 @@ async fn vendored_scan_skips_a_native_gem_that_holds_the_patch_in_agent_mode() { assert_eq!(code, 0, "{stdout}\n{stderr}"); let shown = format!("{stdout}\n{stderr}"); assert!(!shown.contains("Error"), "{shown}"); - assert!(!shown.contains("left to patch"), "{shown}"); + assert!(shown.contains("Nothing left to patch"), "{shown}"); assert!(shown.contains("patched in agent mode"), "{shown}"); } diff --git a/crates/socket-patch-core/src/vendor/gem.rs b/crates/socket-patch-core/src/vendor/gem.rs index bf81f0a35..0b4063ada 100644 --- a/crates/socket-patch-core/src/vendor/gem.rs +++ b/crates/socket-patch-core/src/vendor/gem.rs @@ -318,6 +318,29 @@ fn version_not_locked_detail(name: &str, version: &str) -> String { ) } +/// Refusal code of a gem that builds a native extension and is locked for +/// the `ruby` platform alone: Bundler builds nothing for a path-sourced +/// gem, and the refusal names the modes that do build the extension +/// ([`native_extensions_remedy`]). +pub const NATIVE_EXTENSIONS_UNSUPPORTED: &str = "native_extensions_unsupported"; + +/// Skip code of a gem refused with [`NATIVE_EXTENSIONS_UNSUPPORTED`] that +/// already holds the selected patch in a mode the refusal names: the +/// lock pins that patch hosted, or agent mode patched the installed copy +/// in place. An expected skip, not a failure: the remedy the refusal +/// offers is already in place, and a run that asked for it again would +/// fail every time (#2269). +pub const GEM_PATCHED_IN_OTHER_MODE: &str = "vendor_gem_patched_in_other_mode"; + +/// The detail of a [`GEM_PATCHED_IN_OTHER_MODE`] skip of the gem `name`, +/// which holds the patch `uuid` in `mode` (`hosted` or `agent`). +pub fn patched_in_other_mode_detail(name: &str, mode: &str, uuid: &str) -> String { + format!( + "{name} builds a native extension, which vendored mode cannot vendor; it stays patched \ + in {mode} mode (patch {uuid})" + ) +} + /// The refusals the backend raises for the gem purls of `purls` from /// `Gemfile.lock` alone, each with its purl, decided before the patch /// service is asked for anything: @@ -653,7 +676,7 @@ async fn gem_prelude( if let Some(text) = local_stub.as_ref().filter(|_| locked.platforms.is_empty()) { if gemspec_declares_extensions(text) { return Err(refused( - "native_extensions_unsupported", + NATIVE_EXTENSIONS_UNSUPPORTED, format!( "{leaf}.gemspec declares native extensions; bundler does not build extensions for path-sourced gems. {}", native_extensions_remedy(name, !declaration_selects_source(&gemfile_text, name)) diff --git a/crates/socket-patch-core/src/vendor/refusals.rs b/crates/socket-patch-core/src/vendor/refusals.rs index 521e737d7..22abafafb 100644 --- a/crates/socket-patch-core/src/vendor/refusals.rs +++ b/crates/socket-patch-core/src/vendor/refusals.rs @@ -77,6 +77,9 @@ pub fn refusal_is_benign(code: &str) -> bool { // A gem version another project (or Ruby) put in the shared gem // home: this project doesn't lock it, so there is nothing to wire. || code == gem::GEM_VERSION_NOT_LOCKED + // A gem vendored mode cannot vendor that already holds the patch + // in the mode its refusal names. + || code == gem::GEM_PATCHED_IN_OTHER_MODE // An older vendored patch kept in force while the newer one is // unserved. || super::is_unserved(code) @@ -452,6 +455,21 @@ pub async fn pristine_fetch_is_verifiable( #[cfg(test)] mod tests { use super::*; + + /// #2269: a native gem that already holds the patch in hosted or + /// agent mode is an expected skip; the refusal of one that does not + /// is still a failure. + #[test] + fn a_gem_patched_in_another_mode_is_a_skip_and_its_refusal_is_not() { + assert!(refusal_is_benign(gem::GEM_PATCHED_IN_OTHER_MODE)); + assert!(!refusal_is_benign(gem::NATIVE_EXTENSIONS_UNSUPPORTED)); + let detail = gem::patched_in_other_mode_detail("json", "hosted", "u-1"); + assert!( + detail.contains("json builds a native extension") + && detail.contains("patched in hosted mode (patch u-1)"), + "{detail}" + ); + } use crate::vendor::load_state; const UUID: &str = "22222222-2222-4222-8222-222222222222"; diff --git a/crates/socket-patch-core/src/vex/verify.rs b/crates/socket-patch-core/src/vex/verify.rs index decda81f4..f8792a6c1 100644 --- a/crates/socket-patch-core/src/vex/verify.rs +++ b/crates/socket-patch-core/src/vex/verify.rs @@ -556,6 +556,18 @@ async fn verify_every_gem_copy( Ok(verdict) } +/// Whether the installed gem copy at `pkg_path` holds `record`'s patch as +/// agent mode leaves it: the files of the record the copy takes are all +/// patched, there is at least one, and a native extension the record +/// changes was rebuilt from them ([`verify_every_gem_copy`], the verdict +/// `vex` and `apply --check` attest an agent-patched gem by). +pub async fn gem_copy_patched_in_place(purl: &str, pkg_path: &Path, record: &PatchRecord) -> bool { + matches!( + verify_every_gem_copy(purl, &[pkg_path.to_path_buf()], record).await, + Ok(GemVerdict::Patched) + ) +} + /// Why the installed copies of a gem pass ([`verify_every_gem_copy`]). #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum GemVerdict { diff --git a/docs/ecosystems.md b/docs/ecosystems.md index 14dfbf8b6..7efdd6eef 100644 --- a/docs/ecosystems.md +++ b/docs/ecosystems.md @@ -5089,6 +5089,16 @@ other ecosystems (npm, PyPI, Cargo, Go, Composer, Gradle) to hosted mode. Once a patches of two modes, a `scan` with no `--mode` stops and asks which mode to use, so pass `--mode` on later runs. +A later `scan --mode vendored` doesn't refuse a gem with a native extension again once it holds +the selected patch in hosted or agent mode. It skips the gem with +`vendor_gem_patched_in_other_mode`, a line that names the mode (`Skipping pkg:gem/json@2.9.1: json +builds a native extension, which vendored mode cannot vendor; it stays patched in hosted mode +(patch )`), counts it as patched in the closing totals, and exits 0 when nothing else +failed. The hosted pin or the agent-mode patch stays exactly as it was. The gem is refused again, +and the run exits 1, when it holds no patch or a different one from the patch the scan selects +(after the patch service published a newer one, say): `socket-patch get --mode hosted` +brings it up to date. + ## Ruby gems: native extensions Some gem fixes change only what a native extension is built from: its C sources From 53a6a67ce02ee968bef398d41fd95ec4a400b8ae Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 06:04:30 +0000 Subject: [PATCH 4/4] 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 --- crates/socket-patch-cli/CLI_CONTRACT.md | 2 +- .../socket-patch-cli/src/commands/vendor.rs | 96 +++++++++++-------- .../tests/e2e_redirect_gem_stale_install.rs | 63 ++++++++---- crates/socket-patch-core/src/vendor/gem.rs | 14 +-- .../socket-patch-core/src/vendor/refusals.rs | 10 +- crates/socket-patch-core/src/vex/verify.rs | 26 +++-- docs/ecosystems.md | 10 +- 7 files changed, 141 insertions(+), 80 deletions(-) diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index 2e7cb7cf2..c0baab409 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -2515,7 +2515,7 @@ command's integration tests check its output. | `vendor_unsupported_ecosystem` | `skipped` | vendor: no vendor backend for this purl's ecosystem (jsr). | | `already_vendored` | `skipped` | vendor: artifact + wiring already in sync for this patch uuid. | | `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] (vendor_gem_version_not_locked): …` in the download phase of `scan` / `get` (both identifier paths) and in their dry run, `Skipping : …` in `vendor`. The vendored twin of `redirect_gem_version_not_locked`. | -| `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 : builds a native extension, which vendored mode cannot vendor; it stays patched in hosted mode (patch )`. 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. | +| `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 : builds a native extension, which vendored mode cannot vendor; it stays patched in hosted mode (patch )`. 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. | | `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 ``, 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] (vendor_maven_version_not_selected): …` in the download phase of `scan` / `get` and in their dry run, `Skipping : …` in `vendor`. The vendored twin of `redirect_maven_version_not_selected`. See [Maven version selection](#maven-version-selection-hosted-and-vendored-maven). | | `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`. | | `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 `` 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`. | diff --git a/crates/socket-patch-cli/src/commands/vendor.rs b/crates/socket-patch-cli/src/commands/vendor.rs index 12a06401f..7a8375d33 100644 --- a/crates/socket-patch-cli/src/commands/vendor.rs +++ b/crates/socket-patch-cli/src/commands/vendor.rs @@ -63,8 +63,8 @@ use crate::commands::vex::{ generate_vex_from_manifest_path, generate_vex_without_manifest, ManifestlessVex, VexEmbedArgs, }; use crate::ecosystem_dispatch::{ - find_packages_for_rollback_reusing, npm_paths_by_identity, npm_paths_by_identity_in, - partition_purls, NpmCrawlSnapshot, + find_all_packages_for_purls, find_packages_for_rollback_reusing, npm_paths_by_identity, + npm_paths_by_identity_in, partition_purls, NpmCrawlSnapshot, }; use crate::json_envelope::{ Command, Envelope, EnvelopeError, PatchAction, PatchEvent, RunWarning, Status, VexSummary, @@ -207,38 +207,67 @@ async fn keep_older_vendored_patch( }) } -/// The mode in which the gem `candidate` already holds the patch the run -/// selected for it (`uuid`), for a gem the vendored backend refused for -/// its native extension: `hosted` when the lock still pins that patch +/// The mode in which the gem `candidate` already holds `record`, the +/// patch the run selected for it, for a gem the vendored backend refused +/// for its native extension: `hosted` when the lock still pins that patch /// (`live_pin`, the candidate's hosted pin unless this run restored its -/// upstream entry), `agent` when the manifest records it and the -/// installed copy at `installed` holds it as agent mode leaves it (the -/// verdict `vex` attests the gem by). `None` when it holds another patch -/// or none: the refusal then stands. +/// upstream entry), `agent` when the manifest records it. Either way +/// every installed copy of the gem must hold the patch, as `vex` and +/// `apply --check` judge it: a hosted copy by its files, a copy patched +/// in place by its rebuilt extension as well. `None` when the gem holds +/// another patch or none, or some installed copy does not hold this one +/// (a hosted pin whose gem was never reinstalled, say): the refusal then +/// stands, and the remedy it names puts the patch in place. async fn gem_patch_held_in( common: &GlobalArgs, candidate: &str, - uuid: &str, + record: &PatchRecord, live_pin: Option<&HostedPin>, - installed: &Path, ) -> Option<&'static str> { - if live_pin.is_some_and(|pin| pin.uuid == uuid) { - return Some("hosted"); + let hosted = live_pin.is_some_and(|pin| pin.uuid == record.uuid); + let in_place = match hosted { + true => None, + false => read_manifest(&common.resolved_manifest_path()) + .await + .ok() + .flatten()? + .patches + .into_iter() + .find(|(purl, r)| r.uuid == record.uuid && PurlKey::same(purl, candidate)), + }; + if !hosted && in_place.is_none() { + return None; } - let manifest = read_manifest(&common.resolved_manifest_path()) - .await - .ok() - .flatten()?; - for (purl, record) in &manifest.patches { - if record.uuid == uuid - && PurlKey::same(purl, candidate) - && socket_patch_core::vex::verify::gem_copy_patched_in_place(purl, installed, record) + // Every installed copy, not the one the loop vendors from. + let purls = [candidate.to_string()]; + let copies: Vec = find_all_packages_for_purls( + &partition_purls(&purls, None), + &common.crawler_options(), + true, + ) + .await + .into_iter() + .filter(|(purl, _)| PurlKey::same(purl, candidate)) + .flat_map(|(_, paths)| paths) + .collect(); + if copies.is_empty() { + return None; + } + match in_place { + Some((purl, recorded)) => { + socket_patch_core::vex::verify::gem_copies_patched_in_place(&purl, &copies, &recorded) .await - { - return Some("agent"); + .then_some("agent") + } + None => { + for copy in &copies { + socket_patch_core::vex::verify::verify_patch_record(copy, record) + .await + .ok()?; + } + Some("hosted") } } - None } /// A backend refusal as the run reports it: a gem refused for its native @@ -248,23 +277,22 @@ async fn gem_patch_held_in( async fn settle_gem_refusal( common: &GlobalArgs, candidate: &str, - uuid: &str, + record: &PatchRecord, live_pin: Option<&HostedPin>, - installed: &Path, code: &'static str, detail: String, ) -> (&'static str, String) { if code != vendor::gem::NATIVE_EXTENSIONS_UNSUPPORTED { return (code, detail); } - match gem_patch_held_in(common, candidate, uuid, live_pin, installed).await { + match gem_patch_held_in(common, candidate, record, live_pin).await { Some(mode) => { let purl = normalize_purl(candidate); let name = socket_patch_core::utils::purl::purl_name_version(&purl) .map_or(&*purl, |(name, _)| name); ( vendor::gem::GEM_PATCHED_IN_OTHER_MODE, - vendor::gem::patched_in_other_mode_detail(name, mode, uuid), + vendor::gem::patched_in_other_mode_detail(name, mode, &record.uuid), ) } None => (code, detail), @@ -4365,16 +4393,8 @@ pub(crate) async fn vendor_records_reusing( ); } Some(VendorOutcome::Refused { code, detail }) => { - let (code, detail) = settle_gem_refusal( - common, - candidate, - &record.uuid, - live_pin, - pkg_source.path(), - code, - detail, - ) - .await; + let (code, detail) = + settle_gem_refusal(common, candidate, record, live_pin, code, detail).await; if refusal_is_benign(code) { // An expected skip, not an error: informational. if !common.silent && !common.json { diff --git a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs index ba52840a8..df16b6047 100644 --- a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs +++ b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs @@ -5986,31 +5986,58 @@ async fn vendored_scan_skips_a_native_gem_that_holds_the_patch_in_agent_mode() { assert!(shown.contains("patched in agent mode"), "{shown}"); } -/// The other side of #2269: a native gem that holds the patch in no mode -/// is still refused, and the run still fails. Neither an upstream install -/// with no record, nor a manifest record whose patch is not in the -/// installed copy, counts as patched. +/// The other side of #2269: a native gem that does not hold the patch +/// everywhere is still refused, and the run still fails. None of these +/// counts as patched: an upstream install with no record; a manifest +/// record whose patch is not in the installed copy; a patched copy beside +/// an unpatched one in the tree of another Ruby (`vex` and `apply +/// --check` judge every copy); a hosted pin whose installed copy is still +/// the upstream gem. #[tokio::test(flavor = "multi_thread")] async fn vendored_scan_still_refuses_a_native_gem_that_is_not_patched() { - for recorded in [false, true] { + for case in [ + "no record", + "record not applied", + "one of two copies unpatched", + "hosted pin over an upstream install", + ] { let server = MockServer::start().await; let tmp = tempfile::tempdir().unwrap(); let proj = tmp.path().join("proj"); let home = vendorable_project(&server, &proj).await; - let gem_dir = if recorded { - let gem_dir = agent_patched_install(&proj, &home); - // The record is there; the patch is not in the copy. - std::fs::write(gem_dir.join("lib").join("stale_probe_gem.rb"), UPSTREAM_LIB).unwrap(); - gem_dir - } else { - install_real_gem(&home, UPSTREAM_LIB) + let uri = server.uri(); + let (gem_dir, left) = match case { + "no record" => (install_real_gem(&home, UPSTREAM_LIB), UPSTREAM_LIB), + "record not applied" => { + let gem_dir = agent_patched_install(&proj, &home); + std::fs::write(gem_dir.join("lib").join("stale_probe_gem.rb"), UPSTREAM_LIB) + .unwrap(); + (gem_dir, UPSTREAM_LIB) + } + "one of two copies unpatched" => { + let other = home.with_file_name("3.2.0"); + install_real_gem(&other, UPSTREAM_LIB); + declare_native_extension(&other); + (agent_patched_install(&proj, &home), PATCHED_LIB) + } + _ => { + let gem_dir = install_real_gem(&home, UPSTREAM_LIB); + let (code, _, stderr) = + common::run_with_env(&proj, &scan_args(&proj, &uri, &["--json"]), &[]); + assert_eq!(code, 0, "{case}: hosted scan:\n{stderr}"); + std::fs::write(proj.join("Gemfile.lock"), converged_lock(&uri)).unwrap(); + // The gem as it is before the pinned build is installed. + std::fs::write(gem_dir.join("lib").join("stale_probe_gem.rb"), UPSTREAM_LIB) + .unwrap(); + (gem_dir, UPSTREAM_LIB) + } }; declare_native_extension(&home); let pair_before = read_pair(&proj); let (code, stdout, stderr) = unwind_run( &proj, - &server.uri(), + &uri, &["scan", "--mode", "vendored", "--yes", "--json"], ); let env = common::parse_json_envelope(&stdout); @@ -6019,14 +6046,14 @@ async fn vendored_scan_still_refuses_a_native_gem_that_is_not_patched() { events .iter() .any(|e| e == "failed/native_extensions_unsupported"), - "recorded={recorded}: {events:?}\n{env}\nstderr:\n{stderr}" + "{case}: {events:?}\n{env}\nstderr:\n{stderr}" ); assert!( !env.to_string().contains(PATCHED_IN_OTHER_MODE), - "recorded={recorded}: {env}" + "{case}: {env}" ); - assert_ne!(code, 0, "recorded={recorded}: {env}"); - assert_eq!(read_pair(&proj), pair_before, "recorded={recorded}"); - assert_eq!(installed_lib(&gem_dir), UPSTREAM_LIB, "recorded={recorded}"); + assert_ne!(code, 0, "{case}: {env}"); + assert_eq!(read_pair(&proj), pair_before, "{case}"); + assert_eq!(installed_lib(&gem_dir), left, "{case}"); } } diff --git a/crates/socket-patch-core/src/vendor/gem.rs b/crates/socket-patch-core/src/vendor/gem.rs index 0b4063ada..0e74556bc 100644 --- a/crates/socket-patch-core/src/vendor/gem.rs +++ b/crates/socket-patch-core/src/vendor/gem.rs @@ -319,15 +319,17 @@ fn version_not_locked_detail(name: &str, version: &str) -> String { } /// Refusal code of a gem that builds a native extension and is locked for -/// the `ruby` platform alone: Bundler builds nothing for a path-sourced -/// gem, and the refusal names the modes that do build the extension -/// ([`native_extensions_remedy`]). +/// the `ruby` platform alone, as its installed gemspec or the stub the +/// patch service serves declares it: Bundler builds nothing for a +/// path-sourced gem, and the refusal names the modes that do build the +/// extension ([`native_extensions_remedy`]). pub const NATIVE_EXTENSIONS_UNSUPPORTED: &str = "native_extensions_unsupported"; /// Skip code of a gem refused with [`NATIVE_EXTENSIONS_UNSUPPORTED`] that /// already holds the selected patch in a mode the refusal names: the -/// lock pins that patch hosted, or agent mode patched the installed copy -/// in place. An expected skip, not a failure: the remedy the refusal +/// lock pins that patch hosted, or the manifest records it for agent +/// mode, and every installed copy holds it. An expected skip, not a +/// failure: the remedy the refusal /// offers is already in place, and a run that asked for it again would /// fail every time (#2269). pub const GEM_PATCHED_IN_OTHER_MODE: &str = "vendor_gem_patched_in_other_mode"; @@ -1596,7 +1598,7 @@ fn refuse_unusable_stub( // sources, so a native gem would install and then fail at `require` time. if gemspec_declares_extensions(stub_text) { return Err(policy.hard( - "native_extensions_unsupported", + NATIVE_EXTENSIONS_UNSUPPORTED, format!( "the served stub gemspec for {subject} declares native extensions; bundler does \ not build extensions for path-sourced gems. {}", diff --git a/crates/socket-patch-core/src/vendor/refusals.rs b/crates/socket-patch-core/src/vendor/refusals.rs index 22abafafb..4e244305c 100644 --- a/crates/socket-patch-core/src/vendor/refusals.rs +++ b/crates/socket-patch-core/src/vendor/refusals.rs @@ -461,9 +461,13 @@ mod tests { /// is still a failure. #[test] fn a_gem_patched_in_another_mode_is_a_skip_and_its_refusal_is_not() { - assert!(refusal_is_benign(gem::GEM_PATCHED_IN_OTHER_MODE)); - assert!(!refusal_is_benign(gem::NATIVE_EXTENSIONS_UNSUPPORTED)); - let detail = gem::patched_in_other_mode_detail("json", "hosted", "u-1"); + assert!(refusal_is_benign( + crate::vendor::gem::GEM_PATCHED_IN_OTHER_MODE + )); + assert!(!refusal_is_benign( + crate::vendor::gem::NATIVE_EXTENSIONS_UNSUPPORTED + )); + let detail = crate::vendor::gem::patched_in_other_mode_detail("json", "hosted", "u-1"); assert!( detail.contains("json builds a native extension") && detail.contains("patched in hosted mode (patch u-1)"), diff --git a/crates/socket-patch-core/src/vex/verify.rs b/crates/socket-patch-core/src/vex/verify.rs index f8792a6c1..3bad1c47a 100644 --- a/crates/socket-patch-core/src/vex/verify.rs +++ b/crates/socket-patch-core/src/vex/verify.rs @@ -556,16 +556,22 @@ async fn verify_every_gem_copy( Ok(verdict) } -/// Whether the installed gem copy at `pkg_path` holds `record`'s patch as -/// agent mode leaves it: the files of the record the copy takes are all -/// patched, there is at least one, and a native extension the record -/// changes was rebuilt from them ([`verify_every_gem_copy`], the verdict -/// `vex` and `apply --check` attest an agent-patched gem by). -pub async fn gem_copy_patched_in_place(purl: &str, pkg_path: &Path, record: &PatchRecord) -> bool { - matches!( - verify_every_gem_copy(purl, &[pkg_path.to_path_buf()], record).await, - Ok(GemVerdict::Patched) - ) +/// Whether the installed copies of a gem (`paths`, every copy the crawl +/// found) hold `record`'s patch as agent mode leaves it: each copy's +/// files of the record are patched, some copy takes one, and a native +/// extension the record changes was rebuilt from them +/// ([`verify_every_gem_copy`], the verdict `vex` and `apply --check` +/// attest an agent-patched gem by). No copy at all holds nothing. +pub async fn gem_copies_patched_in_place( + purl: &str, + paths: &[PathBuf], + record: &PatchRecord, +) -> bool { + !paths.is_empty() + && matches!( + verify_every_gem_copy(purl, paths, record).await, + Ok(GemVerdict::Patched) + ) } /// Why the installed copies of a gem pass ([`verify_every_gem_copy`]). diff --git a/docs/ecosystems.md b/docs/ecosystems.md index 7efdd6eef..050f61d0c 100644 --- a/docs/ecosystems.md +++ b/docs/ecosystems.md @@ -5090,14 +5090,16 @@ patches of two modes, a `scan` with no `--mode` stops and asks which mode to use on later runs. A later `scan --mode vendored` doesn't refuse a gem with a native extension again once it holds -the selected patch in hosted or agent mode. It skips the gem with +the selected patch in hosted or agent mode: `Gemfile.lock` pins the patch hosted, or the manifest +records it for agent mode, and every installed copy of the gem holds it. It skips the gem with `vendor_gem_patched_in_other_mode`, a line that names the mode (`Skipping pkg:gem/json@2.9.1: json builds a native extension, which vendored mode cannot vendor; it stays patched in hosted mode (patch )`), counts it as patched in the closing totals, and exits 0 when nothing else failed. The hosted pin or the agent-mode patch stays exactly as it was. The gem is refused again, -and the run exits 1, when it holds no patch or a different one from the patch the scan selects -(after the patch service published a newer one, say): `socket-patch get --mode hosted` -brings it up to date. +and the run exits 1, when it holds no patch, when it holds a different one from the patch the scan +selects (after the patch service published a newer one, say), or when an installed copy doesn't +hold the patch yet (a hosted pin whose gem was not reinstalled). Patch the gem again in the mode +it is in, as the refusal says, and the next vendored scan skips it. ## Ruby gems: native extensions