Repository navigation
Description
[agent] Found by the architecture refactor routine. Test code only: main at 1b3f422 behaves as the contract says in every case below. What is missing is a test that fails when it stops.
Problem
The gem stale-install guard (CLI_CONTRACT.md, lines 490 to 524) replaces the copies of a gem that Bundler already holds, after a hosted scan pins the gem and after an unwind puts it back. Four of its gates can be switched off without a test failing. With each one off, the binary does something the contract rules out:
| # | Gate | Where | With the gate off |
|---|---|---|---|
| 26 | After a refresh the scan probes again, "and only its verdict counts" (line 509) | CLI commands/scan/hosted.rs, line 1123 |
A copy the refresh could not replace, beside one it did, loses its redirect_gem_stale_install warning. The scan exits 0 and its --vex says not_affected while that copy holds the unpatched code |
| 31 | The unwind probes again after its refresh | CLI commands/gem_refresh.rs, line 1764 |
rollback reports nothing for a copy it left on the patched build, beside one it put back: no upstream_gem_stale_install, no reinstall_required |
| 29 | --offline "uses only an archive already on disk" (line 505) |
CLI commands/gem_refresh.rs, line 1758 |
rollback --offline, remove --offline and vendor --revert --offline download the upstream gem from the registry |
| 30 | A vendored unwind judges only a gem the lock resolves from rubygems.org ("a gem from another remote is not judged", line 524) | CLI commands/gem_refresh.rs, line 1669 |
The unwind of a gem from another remote replaces the installed copy |
The tests miss them for two reasons.
No test has a refresh that fixes one copy and leaves another. In every test that refreshes, the pass replaces every stale copy or none. So "probe again" and "take the refresh's word" give the same answer everywhere. The contract names the layout where they differ: a gem home in the tree of another Ruby (vendor/bundle/ruby/3.2.0 next to ruby/3.3.0).
The suite's lock has no CHECKSUMS section. write_manifest_pair (CLI tests/e2e_redirect_gem_stale_install.rs, line 258) writes "a pre-CHECKSUMS Gemfile.lock (the bundler 1/2 lock shape)", and the unwind tests build on it. On that lock an offline unwind has no upstream sha256, and a gem from another remote has none either, so both stop before the gate that the test names:
vendored_unwind_names_an_agent_patched_install_it_leaves(line 5308) says a copy the unwind "cannot" replace "(--offlinewith the archive pruned) is left exactly as it was". It is left because nothing pins the sha256. Gate 29 is never asked.vendored_unwind_does_not_judge_an_agent_patched_gem_from_another_remote(line 5444) says "a gem the lock resolves from another remote is not judged". WithoutCHECKSUMSthe lookup returns nothing for such a gem. Gate 30 is the branch for a lock that pins it.
A lock with CHECKSUMS is what Bundler 4 writes by default (bundle lock with 4.0.18 over an empty Gemfile writes the section), and what bundle lock --add-checksums gives from 2.6 on.
Evidence
The four mutations, never committed (mutant(n) reads a switch from the environment, so one build carries them all):
--- a/crates/socket-patch-cli/src/commands/scan/hosted.rs
+++ b/crates/socket-patch-cli/src/commands/scan/hosted.rs
@@ -1123 @@ async fn refresh_stale_gems(
- let mut outcome = if pass.changed(&policy) {
+ let mut outcome = if mutant(26) && pass.changed(&policy) {
+ StaleInstallOutcome::default()
+ } else if pass.changed(&policy) {
gem_stale_install_warnings(
--- a/crates/socket-patch-cli/src/commands/gem_refresh.rs
+++ b/crates/socket-patch-cli/src/commands/gem_refresh.rs
@@ -1669 @@ async fn refresh_restored(
- if scope == Scope::Installed && url.is_none() {
+ if !mutant(30) && scope == Scope::Installed && url.is_none() {
@@ -1758 @@
- let registry = (!common.offline && !stale.is_empty()).then(SecureRegistryClient::new);
+ let registry = ((mutant(29) || !common.offline) && !stale.is_empty()).then(SecureRegistryClient::new);
@@ -1764 @@
- let still = if pass.changed(&policy) {
+ let still = if mutant(31) && pass.changed(&policy) {
+ Vec::new()
+ } else if pass.changed(&policy) {With each one on, these all pass:
- the CLI's unit tests (1,083). The four gates are CLI code, so core's tests cannot see them;
- 28 CLI suites:
e2e_redirect_gem_stale_install(72 tests), the three real-tool suitese2e_redirect_gem_build,e2e_vendor_gem_buildande2e_gem_native_ext_buildrun with--ignored(79 tests, Bundler 4.0.18 and Ruby 3.3.6), and every other suite I found that names a gem or runs a hosted or vendored unwind (the list is in the ledger entry of this run). One of the 28 holds only opt-in tests and ran nothing (e2e_artifactory_mirror). The only failures are the three ofin_process_redirectthat fail in this sandbox with no mutation either, because it runs as root.
The same run switched off 22 other gates of the refresh (the pinned sha256, the shared-home rule, --no-gem-install-refresh, --dry-run, the stage directory, the swap's undo, and more). 17 of those are caught; the other five are unreachable, equivalent or claimed by no test, and are in the ledger entry.
What each mutation does to a user, with the suite's own fixtures and stand-in servers:
| Gate | Project | main |
Mutated |
|---|---|---|---|
| 26 | two gem homes under vendor/bundle hold the unpatched gem (ruby/3.3.0, and ruby/3.2.0 whose gemspec is gone); scan --mode hosted --yes --json --vex |
exit 1, the ruby/3.2.0 copy named by redirect_gem_stale_install, no VEX document |
exit 0, "status": "success", no redirect_gem_stale_install warning, and a VEX statement not_affected for the gem; the ruby/3.2.0 copy still holds the unpatched file |
| 31 | the same two homes after a hosted scan replaced both copies, then the ruby/3.2.0 gemspec removed; rollback --yes --json |
the ruby/3.2.0 copy named by upstream_gem_stale_install, and reinstall_required |
neither warning: the one gem line is the refresh note for the other copy, and the ruby/3.2.0 copy still holds the patched file |
| 29 | a gem agent mode patched, then vendored, on a lock with CHECKSUMS, the gem home's cache/ pruned; each of rollback, remove and vendor --revert with --offline |
no request, the gem home as it was, the copy named ("this run is offline and no verified copy of the upstream gem is on disk") | one request, /gems/<leaf>.gem on the registry, and the copy replaced |
| 30 | a gem agent mode patched, then vendored, on a lock with CHECKSUMS that resolves it from https://gems.example.test; rollback --yes --json |
the gem home as it was, no gem warning | the installed copy replaced from the archive in the gem home (upstream_gem_install_refreshed) |
Four tests (below) are green on main and each is red on its own mutation only:
$ cargo test -p socket-patch-cli --test e2e_redirect_gem_stale_install # main
test result: ok. 76 passed; 0 failed
$ SPMUT=26 … test a_copy_the_refresh_left_beside_one_it_replaced_is_still_stale ... FAILED
assertion `left == right` failed: the copy that was left is reported: {"command":"scan","status":"success",…
left: 0
right: 1
$ SPMUT=31 … test a_copy_the_unwind_left_beside_one_it_put_back_is_still_reported ... FAILED
assertion `left == right` failed: the copy left on the patched build: {"command":"rollback","status":"success",…
left: 0
right: 1
$ SPMUT=29 … test an_offline_vendored_unwind_downloads_no_gem_the_lock_pins ... FAILED
assertion `left == right` failed: ["rollback", "--yes"]: an offline run asks for nothing
left: 1
right: 0
$ SPMUT=30 … test vendored_unwind_does_not_judge_a_pinned_gem_from_another_remote ... FAILED
["rollback", "--yes"]: the gem home changed
Planned fix
Add the four tests below to tests/e2e_redirect_gem_stale_install.rs, with the helper that gives the fixture a lock with CHECKSUMS. No production change. Gates 26 and 29 matter most: the first is all that keeps a partly refreshed project out of the same run's VEX document, the second is the unwind's airgap on the lock Bundler 4 writes by default.
Not run
Windows and macOS. The live-API suites (e2e_gem, the ignored halves of e2e_hosted_production and e2e_vendored_production, e2e_artifactory_mirror). The Docker gem suites: the switch does not reach the container. Read instead: neither runs hosted mode, and the one unwind (vendor --revert --offline) is of a gem that was only ever vendored, which has nothing stale to refresh. A hosted unwind under --offline on a lock with CHECKSUMS. Bundler releases other than 4.0.18.
The tests
// ── the second probe, and `--offline` on a lock with CHECKSUMS ─────────
/// A second gem home of the project, in the tree of another Ruby.
fn other_ruby_home(proj: &Path) -> PathBuf {
proj.join("vendor")
.join("bundle")
.join("ruby")
.join("3.2.0")
}
fn remove_gemspec(home: &Path) {
let spec = format!("{DEP}-{DEP_VERSION}.gemspec");
std::fs::remove_file(home.join("specifications").join(spec)).unwrap();
}
/// The refresh's word is not the verdict: the probe runs again after it.
/// Two gem homes of the project hold the stale gem, and the refresh can
/// replace one of them only. The other is still reported, and the same
/// run's `--vex` does not attest the purl.
#[tokio::test(flavor = "multi_thread")]
async fn a_copy_the_refresh_left_beside_one_it_replaced_is_still_stale() {
let server = MockServer::start().await;
let patched = real_gem(PATCHED_LIB);
mount_api_for(&server, None, &sha256_of(&patched)).await;
serve_patched_gem(&server, patched).await;
let tmp = tempfile::tempdir().unwrap();
let proj = tmp.path().join("proj");
std::fs::create_dir_all(&proj).unwrap();
write_manifest_pair(&proj);
let replaced = install_real_gem(&project_home(&proj), UPSTREAM_LIB);
let left = install_real_gem(&other_ruby_home(&proj), UPSTREAM_LIB);
// No gemspec: the refresh cannot tell what the copy builds.
remove_gemspec(&other_ruby_home(&proj));
let vex = proj.join("out.vex.json");
let uri = server.uri();
let flags = [
"--json",
"--vex",
vex.to_str().unwrap(),
"--vex-product",
"pkg:gem/app@1.0.0",
];
let (code, stdout, stderr) = common::run_with_env(&proj, &scan_args(&proj, &uri, &flags), &[]);
let env = common::parse_json_envelope(&stdout);
assert_eq!(installed_lib(&replaced), PATCHED_LIB, "{env}\n{stderr}");
assert_eq!(installed_lib(&left), UPSTREAM_LIB, "{env}");
let details = stale_warnings(&env);
assert_eq!(
details.len(),
1,
"the copy that was left is reported: {env}"
);
assert!(
details[0].contains(&left.display().to_string()),
"{details:?}"
);
let doc = std::fs::read_to_string(&vex).unwrap_or_default();
assert!(
!doc.contains(PURL),
"an unpatched copy is on disk, so nothing is attested:\n{doc}"
);
assert_ne!(code, 0, "{env}");
}
/// The same for the unwind: `rollback` puts one of two patched copies
/// back on the upstream gem, and still names the one it could not.
#[tokio::test(flavor = "multi_thread")]
async fn a_copy_the_unwind_left_beside_one_it_put_back_is_still_reported() {
let server = MockServer::start().await;
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).await;
serve_upstream_gem(&server, &upstream).await;
let tmp = tempfile::tempdir().unwrap();
let proj = tmp.path().join("proj");
std::fs::create_dir_all(&proj).unwrap();
write_manifest_pair(&proj);
let put_back = install_real_gem(&project_home(&proj), UPSTREAM_LIB);
let left = install_real_gem(&other_ruby_home(&proj), UPSTREAM_LIB);
let uri = server.uri();
let (code, _, stderr) = common::run_with_env(&proj, &scan_args(&proj, &uri, &["--json"]), &[]);
assert_eq!(code, 0, "{stderr}");
assert_eq!(installed_lib(&put_back), PATCHED_LIB, "premise");
assert_eq!(installed_lib(&left), PATCHED_LIB, "premise");
std::fs::write(proj.join("Gemfile.lock"), converged_lock(&uri)).unwrap();
remove_gemspec(&other_ruby_home(&proj));
let (code, stdout, stderr) = unwind_run(&proj, &uri, &["rollback", "--yes", "--json"]);
let env: serde_json::Value = serde_json::from_str(&stdout)
.unwrap_or_else(|e| panic!("not JSON: {e}\n{stdout}\n{stderr}"));
assert_eq!(code, 0, "{env}\n{stderr}");
assert_eq!(installed_lib(&put_back), UPSTREAM_LIB, "{env}");
assert_eq!(installed_lib(&left), PATCHED_LIB, "{env}");
let named = details_with_code(&env, "upstream_gem_stale_install");
assert_eq!(named.len(), 1, "the copy left on the patched build: {env}");
assert!(named[0].contains(left.to_str().unwrap()), "{named:?}");
}
/// [`agent_then_vendored_from`] on a lock that pins the upstream gem's
/// sha256 itself (`CHECKSUMS`, Bundler 2.6 and later). Returns the
/// installed gem dir.
async fn agent_then_vendored_pinned(server: &MockServer, proj: &Path, remote: &str) -> PathBuf {
let (patched, upstream) = (real_gem(PATCHED_LIB), real_gem(UPSTREAM_LIB));
mount_api_for(server, None, &sha256_of(&patched)).await;
serve_upstream_gem(server, &upstream).await;
mount_vendored_grant(server, &patched).await;
std::fs::create_dir_all(proj).unwrap();
write_manifest_pair(proj);
let checksums = format!(
"CHECKSUMS\n {DEP} ({DEP_VERSION}) sha256={}\n\nBUNDLED WITH\n 2.6.9\n",
sha256_of(&upstream)
);
for file in ["Gemfile", "Gemfile.lock"] {
let text = std::fs::read_to_string(proj.join(file)).unwrap();
let text = text
.replace("https://rubygems.org", remote)
.replace("BUNDLED WITH\n 1.17.3\n", &checksums);
std::fs::write(proj.join(file), text).unwrap();
}
let lock = std::fs::read_to_string(proj.join("Gemfile.lock")).unwrap();
assert!(
lock.contains("CHECKSUMS") && lock.contains(remote),
"premise:\n{lock}"
);
let gem_dir = agent_patched_install(proj, &project_home(proj));
let takeover = ["scan", "--mode", "vendored", "--yes", "--json"];
let (code, stdout, stderr) = unwind_run(proj, &server.uri(), &takeover);
assert_eq!(code, 0, "takeover:\n{stdout}\n{stderr}");
assert_eq!(installed_lib(&gem_dir), PATCHED_LIB, "premise");
gem_dir
}
/// `--offline` downloads no gem when the lock pins the upstream sha256,
/// so that the only thing missing is the archive. The copy agent mode
/// patched is left as it was and named.
#[tokio::test(flavor = "multi_thread")]
async fn an_offline_vendored_unwind_downloads_no_gem_the_lock_pins() {
for unwind in VENDORED_UNWINDS {
let server = MockServer::start().await;
let tmp = tempfile::tempdir().unwrap();
let proj = tmp.path().join("proj");
agent_then_vendored_pinned(&server, &proj, "https://rubygems.org").await;
let home = project_home(&proj);
std::fs::remove_dir_all(home.join("cache")).unwrap();
let held_before = snapshot(&home);
let asked_before = server.received_requests().await.unwrap().len();
let mut args = unwind.to_vec();
args.extend(["--json", "--offline"]);
let (code, stdout, stderr) = unwind_run(&proj, &server.uri(), &args);
assert_eq!(code, 0, "{unwind:?}:\n{stdout}\n{stderr}");
let asked = server.received_requests().await.unwrap().len() - asked_before;
assert_eq!(asked, 0, "{unwind:?}: an offline run asks for nothing");
assert!(
snapshot(&home) == held_before,
"{unwind:?}: the gem home changed:\n{stdout}"
);
assert!(
stdout.contains("upstream_gem_stale_install") && stdout.contains("this run is offline"),
"{unwind:?}:\n{stdout}"
);
}
}
/// A gem from another remote is not judged on a lock with `CHECKSUMS`
/// either: the sha256 the lock pins proves nothing about where the entry
/// came from.
#[tokio::test(flavor = "multi_thread")]
async fn vendored_unwind_does_not_judge_a_pinned_gem_from_another_remote() {
for unwind in VENDORED_UNWINDS {
let server = MockServer::start().await;
let tmp = tempfile::tempdir().unwrap();
let proj = tmp.path().join("proj");
let gem_dir = agent_then_vendored_pinned(&server, &proj, "https://gems.example.test").await;
let home = project_home(&proj);
let held_before = snapshot(&home);
let mut args = unwind.to_vec();
args.push("--json");
let (code, stdout, stderr) = unwind_run(&proj, &server.uri(), &args);
assert_eq!(code, 0, "{unwind:?}:\n{stdout}\n{stderr}");
let lock = std::fs::read_to_string(proj.join("Gemfile.lock")).unwrap();
assert!(
lock.contains("remote: https://gems.example.test/") && lock.contains("CHECKSUMS"),
"{unwind:?}: premise: back on the other remote:\n{lock}"
);
assert!(
snapshot(&home) == held_before,
"{unwind:?}: the gem home changed:\n{stdout}"
);
assert_eq!(installed_lib(&gem_dir), PATCHED_LIB, "{unwind:?}");
for code in [
"upstream_gem_install_refreshed",
"upstream_gem_stale_install",
] {
assert!(!stdout.contains(code), "{unwind:?}: {code} in\n{stdout}");
}
}
}Generated by Claude Code
Activity
refactor: #2421 filed; 26 gem refresh mutants ran