Skip to content

Commit 9389ca9

Browse files
committed
Judge only ledger-owned gems from rubygems.org
Review follow-ups for the vendored unwind's record judgment. A gem the manifest still has a record for is not judged: its in-place patch is agent mode's and on record there (standalone vendor keeps the record), so vendor --revert leaves the installed copy as it is. Only a detached ledger entry's record is used, and the manifest is matched by PurlKey as the takeover does. With no sha256 for the upstream gem (an offline run or a registry error under a lock without CHECKSUMS), a copy is judged by its record only when the lock resolves the gem from rubygems.org. A gem from another remote stays unjudged, as in every unwind refresh. Refs #2093 Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeQy1d8enbEzCjPrQua
1 parent ffaa6f3 commit 9389ca9

9 files changed

Lines changed: 188 additions & 126 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ The rewriter reads a fixed set of candidate files from the project root: the npm
460460

461461
*What never refreshes.* A hosted → vendored takeover (`vendor`, `scan` / `get --mode vendored` over a hosted pin) puts the gem on its upstream entry only on its way to the vendored `path:` source, which is what Bundler loads from then on. The copies Bundler already holds are unused leftovers there, so the takeover changes none of them and downloads no gem for them: resetting the installed copy to the upstream build would cost a registry download in the mode that needs none, and would turn a patched leftover into an unpatched one that `vex` reports against the vendored artifact (`vendored_tree_out_of_sync`, whose gem wording names the copy and how to remove it). Its `upstream_gem_stale_cache` advisory about a patched cache-dir archive is as before. A vendored unwind (`remove`, `rollback`, `vendor --revert`, `scan --prune`, `vendor`'s reconcile of a vendored gem) likewise only names that archive, and never replaces it.
462462

463-
*A vendored unwind after a hosted or an agent scan.* Once a vendored gem is unwound, Bundler loads its installed copy again, so the leftover is in use: if a hosted scan made that copy the patched build before the gem was vendored, the patched code would keep running under the upstream lock, and on bundler ≥ 2.6 `bundle pristine` would fail on the upstream checksum. If agent mode patched that copy in place before the gem was vendored (#2093), the takeover moved its manifest record into the vendor ledger and kept the patched files, so the patched code would keep running with no record left that `rollback` or `apply --check` could act on. The same holds for a gem standalone `vendor` vendored from an agent-mode manifest once `rollback` or `remove` unwinds it: they drop its manifest record with the vendored gem and restore no file from the record's blobs. `vendor --revert` keeps that manifest record, so the in-place patch stays agent mode's and the installed copy is not judged. A wet `rollback`, `remove`, `vendor --revert` or `vendor` reconcile that put a vendored gem back on its upstream entry therefore applies the unwind direction's refresh to the gem's INSTALLED copies when the lock now resolves the gem from rubygems.org (a gem from another remote is not judged: nothing proves its entry is the upstream one), under the same rules (staleness judged by `<home>/cache/<leaf>.gem` against the upstream sha256, and by the patch record the unwind ends, which is the detached ledger entry's own or the manifest record `rollback` / `remove` drop: a copy whose files all hash to the record's `afterHash` values is stale whatever archive its gem home holds, or when it holds none; a project-local gem home only, unless `--gem-refresh-shared`; `--no-gem-install-refresh`; an archive on disk, else rubygems.org unless `--offline`), with the same `upstream_gem_install_refreshed` note and `upstream_gem_stale_install` warning. `--json` carries them where the command carries a vendored revert's advisories (rollback `warnings[]`; `remove` / `vendor` advisory events); a human run prints the progress line after the gem's own line and one warning for the gems left on the patched build. The upstream sha256 is looked up only for a gem that has an installed copy with its archive, or one holding the record's patched files, so the unwind of a gem that is not installed needs no network. A gem that was only ever vendored has the upstream archive there: nothing is downloaded or changed. A copy agent mode patched is reinstalled from the upstream archive RubyGems kept beside it, so nothing is downloaded for it either unless that archive is gone (a pruned `cache/`); the reinstall also removes what agent mode kept in the gem dir. When nothing proves the upstream sha256 for such a copy (`--offline` under a lock without `CHECKSUMS`), it is left as it is and named by `upstream_gem_stale_install`. When the lock pins no `CHECKSUMS` sha256 for the gem and the registry is refused (a plain-`http` `SOCKET_RUBYGEMS_URL`, see the unwind direction), such an installed copy is reported as one that could not be checked, whichever of the two it is: nothing tells them apart. A `--dry-run` does not preview this (the lock on disk is still the vendored one), and `scan --prune` does not refresh.
463+
*A vendored unwind after a hosted or an agent scan.* Once a vendored gem is unwound, Bundler loads its installed copy again, so the leftover is in use: if a hosted scan made that copy the patched build before the gem was vendored, the patched code would keep running under the upstream lock, and on bundler ≥ 2.6 `bundle pristine` would fail on the upstream checksum. If agent mode patched that copy in place before the gem was vendored (#2093), the takeover moved its manifest record into the vendor ledger and kept the patched files, so the patched code would keep running with no record left that `rollback` or `apply --check` could act on. A gem `.socket/manifest.json` still has a record for is different: its in-place patch is agent mode's and on record (standalone `vendor` works from the manifest and keeps the record), so the vendored unwind does not judge its installed copy by a record. A wet `rollback`, `remove`, `vendor --revert` or `vendor` reconcile that put a vendored gem back on its upstream entry therefore applies the unwind direction's refresh to the gem's INSTALLED copies when the lock now resolves the gem from rubygems.org (a gem from another remote is not judged: nothing proves its entry is the upstream one), under the same rules (staleness judged by `<home>/cache/<leaf>.gem` against the upstream sha256, and by the patch record of a detached ledger entry (`scan` / `get --mode vendored`) whose gem the manifest has no record for: a copy whose files all hash to the record's `afterHash` values is stale whatever archive its gem home holds, or when it holds none; a project-local gem home only, unless `--gem-refresh-shared`; `--no-gem-install-refresh`; an archive on disk, else rubygems.org unless `--offline`), with the same `upstream_gem_install_refreshed` note and `upstream_gem_stale_install` warning. `--json` carries them where the command carries a vendored revert's advisories (rollback `warnings[]`; `remove` / `vendor` advisory events); a human run prints the progress line after the gem's own line and one warning for the gems left on the patched build. The upstream sha256 is looked up only for a gem that has an installed copy with its archive, or one holding the record's patched files, so the unwind of a gem that is not installed needs no network. A gem that was only ever vendored has the upstream archive there: nothing is downloaded or changed. A copy agent mode patched is reinstalled from the upstream archive RubyGems kept beside it, so nothing is downloaded for it either unless that archive is gone (a pruned `cache/`); the reinstall also removes what agent mode kept in the gem dir. When nothing proves the upstream sha256 for such a copy (`--offline` under a lock without `CHECKSUMS`), it is left as it is and named by `upstream_gem_stale_install`. When the lock pins no `CHECKSUMS` sha256 for the gem and the registry is refused (a plain-`http` `SOCKET_RUBYGEMS_URL`, see the unwind direction), such an installed copy is reported as one that could not be checked, whichever of the two it is: nothing tells them apart. A `--dry-run` does not preview this (the lock on disk is still the vendored one), and `scan --prune` does not refresh.
464464

465465
**Pipenv hosted redirect (`Pipfile.lock`, pipfile-spec 6)**: every category other than `_meta` (`default`, `develop`, and Pipenv 2022+ named categories) that pins the package at the patched version is rewritten to the hosted reference — `{"file" | "path": "<artifact url>#sha256=<hex>", "hashes": ["sha256:<hex>"]}` with `markers`/`extras`/`index` kept exactly as Pipenv wrote them (present or absent: whether Pipenv records `index` depends on its release, the Pipfile spelling and the locking environment, so only the entry itself knows) and `version` dropped; `_meta` (the Pipfile content hash) and the Pipfile itself are never touched, so `pipenv install --deploy`/`sync`/`verify` keep passing. The reference KEY depends on the installing Pipenv: releases 7–11 only install `path` references, 2018 and later `file` ones (0–6 write pipfile-spec < 6 and are refused). The release is probed once per command with `pipenv --version`, resolved on ABSOLUTE `PATH` entries only (a relative entry would run a `pipenv` planted in the scanned repository; `.bat`/`.cmd` shims are found through `PATHEXT` on Windows), only when a pypi patch actually targets an entry of the lock, and `SOCKET_PIPENV_MAJOR=<major>` pins the answer without spawning anything. An unknown installer selects `file` and warns `redirect_pipenv_installer_unknown` only when the lock was rewritten. **Refusal scope**: a pin/source CONFLICT (another version pinned, a foreign `file`/`path` source, a VCS/editable dependency) refuses the whole dependency atomically across categories as `redirect_pipenv_refused` AND vetoes the sibling Python rewriters (requirements.txt / uv.lock / pyproject) for that patch — the project's Pipenv install could not pick the patch up, so a half-redirected checkout is refused; anything else (no entry for the package, an old pipfile-spec, an unparseable lock, a digest-less patch) is `redirect_pipenv_skipped` and leaves the siblings alone (a stale Pipfile.lock in a uv/Poetry/requirements project must not block them). The veto applies to a LIVE lock only: a `Pipfile.lock` with no `Pipfile` beside it is abandoned, so its conflict refuses that file but never the siblings. Hash enforcement at install time is split by era — the `#sha256=` URL fragment is what Pipenv 2023+ verifies, the `hashes` list what 2018–2022 verify, Pipenv 11 either — so both are load-bearing. **Pipenv stale-install guard**: Pipenv never reinstalls a release that is already present (`pipenv install`, `install --deploy` and `sync` all exit 0 and keep the installed bytes — measured on 11.10.4, 2018.11.26 and 2026.8.0, hosted and vendored), so after the rewrite the run probes the Python crawler's site-packages (VIRTUAL_ENV, `./.venv`, `./venv`, Pipenv's out-of-tree `WORKON_HOME` venv; `--global`/`--global-prefix` honoured) for each confirmed Pipfile.lock redirect with the Python stale-install guard's evidence (the pinned patched wheel, else the patch record; STALE needs positive evidence, read-only, skipped on `--dry-run`, stale purls excluded from the same-run `--vex` `assume_applied` set) and the Python stale-install guard (`redirect_pypi_stale_install`, see above) names the site-packages dir and the Pipenv-specific verified remedy: `pipenv run pip uninstall -y <pkg> && pipenv sync` (or `pipenv --rm && pipenv sync`), with the `sync` arguments following the lock, since plain `pipenv sync` installs only `default`: the targeted form re-syncs the categories that pin the package (`--dev` for `develop`, `--categories "<Pipfile names>"` for a named category) and the `--rm` form re-syncs every non-empty category — NOT `pipenv uninstall`, which rewrites the Pipfile and re-locks the patch away. The vendored backend emits the twin `pypi_pipenv_stale_install` (`skipped` warning event). **Rollback** (v5.0, upstream restore): each hosted entry gets its registry shape back — `"version": "==<v>"`, the entry's own `index` carried back unchanged (refused unless it — and the Pipfile's explicit `index`, if any — names a PyPI source in `_meta.sources`), and every release file's sha256 from PyPI's JSON API (`SOCKET_PYPI_JSON_API`), sorted by filename as Pipenv records them; an entry that pins another version beside the hosted reference is refused with the `git checkout` remedy (see "Hosted unwind coverage"). A Pipfile names no project, so a same-run `--vex` on a Pipenv project needs `--vex-product` (or a git remote) to detect a product purl. **Discovery**: `Pipfile.lock` is part of the lockfile inventory (every category's `==` pins, with the lock's digest set as `Sha256AnyOf` integrity so a lock-only checkout can be vendored by fetching the pure wheel through PyPI's JSON API — only when `_meta.sources` name the public index; a private-index lock stays discovery-only and never reaches pypi.org), and Socket's own hosted / vendored references stay discoverable as the package they replace, so a re-scan of an already-redirected or already-vendored lock-only checkout re-confirms it (`--vex` attests, vendored reports `already_vendored`) instead of finding nothing.
466466

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,9 @@ async fn refresh_restored(
997997
records: &BTreeMap<String, PatchRecord>,
998998
scope: Scope,
999999
) -> RestoreRefresh {
1000-
use socket_patch_core::patch::redirect::upstream::{gem_upstream_archive, UpstreamArchive};
1000+
use socket_patch_core::patch::redirect::upstream::{
1001+
gem_resolves_from_rubygems, gem_upstream_archive, UpstreamArchive,
1002+
};
10011003
use socket_patch_core::vendor::registry_fetch::SecureRegistryClient;
10021004

10031005
let policy = Policy::of(common);
@@ -1008,10 +1010,11 @@ async fn refresh_restored(
10081010
// with why: their installed copies are reported, not passed over.
10091011
let mut unverified: BTreeMap<String, String> = BTreeMap::new();
10101012
// The records an installed copy is judged by as well: those of the
1011-
// gems known to be back on their upstream entries. A copy holding its
1012-
// record's patch is stale even when nothing proves the upstream
1013-
// sha256 (`--offline` under a lock without `CHECKSUMS`): it is then
1014-
// reported, not refreshed.
1013+
// gems the lock resolves from rubygems.org, which are back on their
1014+
// upstream entries. A copy holding its record's patch is stale even
1015+
// when nothing proves the upstream sha256 (`--offline` or a registry
1016+
// error, under a lock without `CHECKSUMS`): it is then reported, not
1017+
// refreshed.
10151018
let mut judged: BTreeMap<String, PatchRecord> = BTreeMap::new();
10161019
for purl in restored
10171020
.iter()
@@ -1031,23 +1034,21 @@ async fn refresh_restored(
10311034
}
10321035
shas.insert(purl.clone(), Some(sha256));
10331036
urls.insert(purl.clone(), url);
1034-
judged.extend(
1035-
records
1036-
.get_key_value(purl)
1037-
.map(|(k, v)| (k.clone(), v.clone())),
1038-
);
1037+
if let Some(record) = records.get(purl) {
1038+
judged.insert(purl.clone(), record.clone());
1039+
}
10391040
}
10401041
// (Only a gem the lock resolves from rubygems.org comes back
10411042
// unverified, so the vendored unwind's rule holds for it too.)
10421043
Some(UpstreamArchive::Unverified(why)) => {
10431044
unverified.insert(purl.clone(), why);
10441045
}
10451046
None => {
1046-
judged.extend(
1047-
records
1048-
.get_key_value(purl)
1049-
.map(|(k, v)| (k.clone(), v.clone())),
1050-
);
1047+
if let Some(record) = records.get(purl) {
1048+
if gem_resolves_from_rubygems(&common.cwd, purl).await {
1049+
judged.insert(purl.clone(), record.clone());
1050+
}
1051+
}
10511052
}
10521053
}
10531054
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,11 +1399,7 @@ async fn revert_vendored_matches(
13991399
let mut leg = RemoveVendorLeg::default();
14001400
// Stops at the first hard failure: remove aborts there, leaving the
14011401
// remaining matches (and the manifest) untouched.
1402-
let mut backend = VendoredBackend::new(&args.common, None);
1403-
if !args.preserve_state {
1404-
// The manifest record of an unwound package is removed with it.
1405-
backend = backend.dropping_manifest_records();
1406-
}
1402+
let backend = VendoredBackend::new(&args.common, None);
14071403
let reverted = backend.revert(keys, state, opts, true).await;
14081404
for (key, w) in backend.go_undo_warnings(&reverted).await {
14091405
if loud {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,11 +1026,7 @@ async fn run_vendored_leg(
10261026
keep_artifact: preserve,
10271027
};
10281028
let loud = !common.json && !common.silent;
1029-
let mut backend = VendoredBackend::new(common, None);
1030-
if !preserve {
1031-
// The manifest cleanup drops an unwound package's record.
1032-
backend = backend.dropping_manifest_records();
1033-
}
1029+
let backend = VendoredBackend::new(common, None);
10341030
let reverted = backend.revert(keys, state, opts, false).await;
10351031
for (_, w) in backend.go_undo_warnings(&reverted).await {
10361032
if loud {

0 commit comments

Comments
 (0)