Skip to content

Commit 20195b1

Browse files
mikolalysenkoclaude
andcommitted
Merge remote-tracking branch 'origin/main' into bazel-vendored/pypi-download-cache
main holds the branch this one follows as one squashed commit, so every conflict was of that branch's lines against this branch's later edits of them, and each resolves to this branch's text. What main has beyond it (the report of a vendored revert that scan --prune kept or failed, the remedy for a stale gem in another Ruby's tree, the shared test helpers and the Windows pnpm tests) merges in by itself, into the contract and the docs as well. The `bazel` cap of scripts/loc-budget.json stays 5300; the count here is 5202. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2 parents 5579eb3 + 9a2a946 commit 20195b1

39 files changed

Lines changed: 990 additions & 461 deletions

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

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

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

Lines changed: 308 additions & 40 deletions
Large diffs are not rendered by default.

‎crates/socket-patch-cli/src/commands/scan/gc.rs‎

Lines changed: 301 additions & 39 deletions
Large diffs are not rendered by default.

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

Lines changed: 143 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5442,18 +5442,20 @@ pub(crate) struct VendorGcSummary {
54425442
/// (b) entries whose package left the lockfile dependency graph —
54435443
/// reverted, and their manifest entries dropped.
54445444
pub unused_reverted: Vec<String>,
5445-
/// Entries a wet revert drift-kept ([`RevertOutcome::kept_artifact`]):
5446-
/// the backend left the drifted lock alone, so artifacts, ledger entry
5447-
/// and (in (b)) manifest records were all retained — nothing reclaimed.
5448-
/// Always empty on dry runs: backends detect drift only during a wet
5449-
/// wiring replay, so the preview still lists such entries as revertable.
5450-
pub kept: Vec<String>,
5445+
/// Entries a wet revert kept ([`RevertOutcome::kept_artifact`]), each
5446+
/// with why: the backend left a drifted lock alone, or restored the
5447+
/// recorded wiring while another project file still installs from the
5448+
/// artifact. Artifacts, ledger entry and (in (b)) manifest records were
5449+
/// all retained — nothing reclaimed. Always empty on dry runs: the
5450+
/// preview runs no revert, so it still lists such entries as
5451+
/// revertable.
5452+
pub kept: Vec<(String, KeepCause)>,
54515453
/// (c) orphan uuid dirs (no owning ledger entry) swept.
54525454
pub orphan_dirs: usize,
5453-
/// Entries (ledger keys) that could not be reverted — kept in the
5454-
/// ledger, nothing reclaimed. Only purls; the pass-level outcomes below
5455-
/// have their own fields.
5456-
pub failed: Vec<String>,
5455+
/// Entries (ledger keys) that could not be reverted, each with the
5456+
/// backend's reason — kept in the ledger, nothing reclaimed. The
5457+
/// pass-level outcomes below have their own fields.
5458+
pub failed: Vec<(String, String)>,
54575459
/// Post-revert rewrites that failed: `("vendor_state_write_failed" |
54585460
/// "manifest_write_failed", <detail>)`. The reverts themselves already
54595461
/// happened on disk; the stale record is what the caller must report.
@@ -5465,7 +5467,8 @@ pub(crate) struct VendorGcSummary {
54655467
/// `vendor_vlt_reinstall_required`, and, for a manifest-dropped PyPI
54665468
/// entry under pip, PDM, uv or Pipenv, `vendor_pypi_reinstall_required`
54675469
/// (#477). Every other reverting command surfaces these, and the GC
5468-
/// must not drop them.
5470+
/// must not drop them. Also the `vendor_revert_residual_reference`
5471+
/// warning of an entry kept for one: it names the file to fix.
54695472
pub advisories: Vec<(&'static str, String)>,
54705473
}
54715474

@@ -5475,7 +5478,9 @@ pub(crate) struct VendorGcSummary {
54755478
/// revert's other warnings are routine for a prune and stay out:
54765479
/// `vendor_lock_entry_removed` is the normal leg-(b) case (the dependency
54775480
/// was uninstalled), and a drift keep is already reported through
5478-
/// `keptVendoredEntries` and its own `GC: kept …` line.
5481+
/// `keptVendoredEntries` and its own `GC: kept …` line. A keep for a
5482+
/// residual reference forwards the warning that names the file
5483+
/// ([`VendorGcSummary::note_unreclaimed`]).
54795484
const GC_FORWARDED_ADVISORIES: &[&str] = &[
54805485
socket_patch_core::vendor::bun_lock::REINSTALL_REQUIRED,
54815486
socket_patch_core::vendor::vlt_lock::REINSTALL_REQUIRED,
@@ -5485,10 +5490,12 @@ const GC_FORWARDED_ADVISORIES: &[&str] = &[
54855490

54865491
impl VendorGcSummary {
54875492
/// Keep a revert's reinstall advisories. Only a revert that actually
5488-
/// restored the lock (succeeded, not drift-kept) can leave a stale
5489-
/// installed copy behind.
5493+
/// restored the lock can leave a stale installed copy behind: one that
5494+
/// succeeded and was not drift-kept. A keep for a residual reference
5495+
/// restored it too.
54905496
fn take_advisories(&mut self, outcome: &RevertOutcome) {
5491-
if !outcome.success || outcome.kept_artifact {
5497+
let drift_kept = outcome.kept_artifact && KeepCause::of(outcome) == KeepCause::Drift;
5498+
if !outcome.success || drift_kept {
54925499
return;
54935500
}
54945501
self.advisories.extend(
@@ -5499,6 +5506,39 @@ impl VendorGcSummary {
54995506
.map(|w| (w.code, w.detail.clone())),
55005507
);
55015508
}
5509+
5510+
/// File a wet revert of `purl` that reclaimed nothing: a failure with
5511+
/// the backend's reason, or a keep with its cause, classified as for
5512+
/// `remove` and `rollback` ([`KeepCause::of`]). A keep for a residual
5513+
/// reference carries the warning that names the file still installing
5514+
/// from the artifact, without which its remedy cannot be followed; the
5515+
/// purl leads its detail, since the backend names only the uuid dir.
5516+
/// `false` when the revert went through.
5517+
fn note_unreclaimed(&mut self, purl: &str, outcome: &RevertOutcome) -> bool {
5518+
if !outcome.success {
5519+
let why = outcome.error.clone();
5520+
self.failed.push((
5521+
purl.to_string(),
5522+
why.unwrap_or_else(|| "unknown error".into()),
5523+
));
5524+
return true;
5525+
}
5526+
if !outcome.kept_artifact {
5527+
return false;
5528+
}
5529+
let cause = KeepCause::of(outcome);
5530+
if cause == KeepCause::Reference {
5531+
self.advisories.extend(
5532+
outcome
5533+
.warnings
5534+
.iter()
5535+
.filter(|w| w.code == vendor::RESIDUAL_REFERENCE_CODE)
5536+
.map(|w| (w.code, format!("{purl}: {}", w.detail))),
5537+
);
5538+
}
5539+
self.kept.push((purl.to_string(), cause));
5540+
true
5541+
}
55025542
}
55035543
/// The manifest keys an unused vendored `entry`, stored under ledger key
55045544
/// `purl`, owns: every key with the same [`PurlKey`] as the ledger key OR
@@ -5532,12 +5572,12 @@ pub(crate) fn unused_vendored_manifest_keys<V>(
55325572
/// prune + blob sweep reclaims the rest in the same pass;
55335573
/// (c) sweep orphan uuid dirs.
55345574
///
5535-
/// A drift-skipped revert ([`RevertOutcome::kept_artifact`]) keeps the
5536-
/// ledger entry — and, in (b), the purl's manifest records — exactly like
5537-
/// every other `dispatch_revert_one` caller; the kept purl is reported in
5538-
/// [`VendorGcSummary::kept`] so `scan --prune` can explain it. Wet-only: a
5539-
/// dry [`dispatch_revert_one`] returns before the wiring replay that
5540-
/// detects drift.
5575+
/// A revert that kept its artifact ([`RevertOutcome::kept_artifact`]: a
5576+
/// drifted lock, or another project file that still installs from it)
5577+
/// keeps the ledger entry — and, in (b), the purl's manifest records —
5578+
/// exactly like every other `dispatch_revert_one` caller; the kept purl is
5579+
/// reported with its cause in [`VendorGcSummary::kept`] so `scan --prune`
5580+
/// can explain it. Wet-only: the dry pass runs no revert.
55415581
///
55425582
/// Detached entries — every `scan`/`get --mode vendored` entry — are exempt
55435583
/// from (a) alone: they are never manifest-tracked, so "absent from the
@@ -5617,14 +5657,10 @@ pub(crate) async fn run_vendor_gc(
56175657
.await;
56185658
}
56195659
out.take_advisories(&outcome);
5620-
if !outcome.success {
5621-
out.failed.push(purl);
5622-
} else if outcome.kept_artifact {
5623-
// Drift-skip keep: the ledger entry must survive too (which
5624-
// also shields the uuid dir from the (c) orphan sweep), and
5625-
// the purl is reported as kept, never as reverted.
5626-
out.kept.push(purl);
5627-
} else {
5660+
// A kept artifact: the ledger entry must survive too (which
5661+
// also shields the uuid dir from the (c) orphan sweep), and
5662+
// the purl is reported as kept, never as reverted.
5663+
if !out.note_unreclaimed(&purl, &outcome) {
56285664
state.entries.remove(&purl);
56295665
ledger_dirty = true;
56305666
out.dropped_reverted.push(purl);
@@ -5665,16 +5701,11 @@ pub(crate) async fn run_vendor_gc(
56655701
}
56665702
let outcome = dispatch_revert_one(&entry, &common.cwd, false).await;
56675703
out.take_advisories(&outcome);
5668-
if !outcome.success {
5669-
out.failed.push(purl);
5670-
continue;
5671-
}
5672-
if outcome.kept_artifact {
5673-
// Drift-skip keep, same gate as (a) — and the purl's manifest
5674-
// records must survive too: pruning them would make the next
5675-
// `vendor` reconcile re-revert an entry whose backing record is
5676-
// gone.
5677-
out.kept.push(purl);
5704+
// A kept artifact, same gate as (a) — and the purl's manifest
5705+
// records must survive too: pruning them would make the next
5706+
// `vendor` reconcile re-revert an entry whose backing record is
5707+
// gone.
5708+
if out.note_unreclaimed(&purl, &outcome) {
56785709
continue;
56795710
}
56805711
state.entries.remove(&purl);
@@ -7476,7 +7507,7 @@ mod gc_tests {
74767507
assert!(out.failed.is_empty(), "a keep is not a failure: {out:?}");
74777508
assert_eq!(
74787509
out.kept,
7479-
vec![PURL.to_string()],
7510+
vec![(PURL.to_string(), KeepCause::Drift)],
74807511
"the drift-keep must be COUNTED — scan --prune's only signal \
74817512
that the entry its preview listed was deliberately not \
74827513
reclaimed: {out:?}"
@@ -7522,7 +7553,7 @@ mod gc_tests {
75227553
assert!(out.failed.is_empty(), "a keep is not a failure: {out:?}");
75237554
assert_eq!(
75247555
out.kept,
7525-
vec![PURL.to_string()],
7556+
vec![(PURL.to_string(), KeepCause::Drift)],
75267557
"the drift-keep must be COUNTED — scan --prune's only signal \
75277558
that the entry its preview listed was deliberately not \
75287559
reclaimed: {out:?}"
@@ -7775,10 +7806,61 @@ mod gc_tests {
77757806
);
77767807
}
77777808

7809+
/// What the pass keeps of a kept revert follows from why it was kept.
7810+
/// A keep for a residual reference restored the lock: its reinstall
7811+
/// advisory stands, and the warning that names the file is carried
7812+
/// under the purl. A drift keep restored nothing and repeats nothing.
7813+
#[test]
7814+
fn a_kept_revert_is_filed_by_its_cause() {
7815+
use socket_patch_core::vendor::vlt_lock::REINSTALL_REQUIRED;
7816+
let dir = format!(".socket/vendor/npm/{UUID}");
7817+
let reinstall = || VendorWarning::new(REINSTALL_REQUIRED, "run `vlt ci`");
7818+
7819+
let mut reference = RevertOutcome::ok();
7820+
reference.warnings.push(reinstall());
7821+
reference.warnings.push(VendorWarning::new(
7822+
vendor::RESIDUAL_REFERENCE_CODE,
7823+
format!("kept {dir}/: apps/web/package.json still names it"),
7824+
));
7825+
reference.keep_artifact_for_reference(&dir);
7826+
let mut out = VendorGcSummary::default();
7827+
out.take_advisories(&reference);
7828+
assert!(out.note_unreclaimed(PURL, &reference));
7829+
assert_eq!(out.kept, vec![(PURL.to_string(), KeepCause::Reference)]);
7830+
assert_eq!(
7831+
out.advisories,
7832+
vec![
7833+
(REINSTALL_REQUIRED, "run `vlt ci`".to_string()),
7834+
(
7835+
vendor::RESIDUAL_REFERENCE_CODE,
7836+
format!("{PURL}: kept {dir}/: apps/web/package.json still names it"),
7837+
),
7838+
]
7839+
);
7840+
7841+
let mut drift = RevertOutcome::ok();
7842+
drift.warnings.push(reinstall());
7843+
drift.warnings.push(VendorWarning::new(
7844+
"vendor_lock_entry_drifted",
7845+
"node_modules/left-pad was re-resolved",
7846+
));
7847+
drift.keep_artifact(&dir);
7848+
let mut out = VendorGcSummary::default();
7849+
out.take_advisories(&drift);
7850+
assert!(out.note_unreclaimed(PURL, &drift));
7851+
assert_eq!(out.kept, vec![(PURL.to_string(), KeepCause::Drift)]);
7852+
assert!(out.advisories.is_empty(), "{out:?}");
7853+
7854+
// A revert that went through is neither kept nor failed.
7855+
let mut out = VendorGcSummary::default();
7856+
assert!(!out.note_unreclaimed(PURL, &RevertOutcome::ok()));
7857+
assert!(out.kept.is_empty() && out.failed.is_empty(), "{out:?}");
7858+
}
7859+
77787860
/// (a) revert FAILURE accounting: a ledger entry whose ecosystem has no
77797861
/// revert backend (a tampered/hand-edited state.json) lands in
7780-
/// `out.failed`, is KEPT in the ledger, and is excluded from pass (b)
7781-
/// (no double count).
7862+
/// `out.failed` with the backend's reason, is KEPT in the ledger, and
7863+
/// is excluded from pass (b) (no double count).
77827864
#[tokio::test]
77837865
async fn vendor_gc_failed_dropped_revert_keeps_entry() {
77847866
let (tmp, common, manifest_path) = gc_fixture(false).await;
@@ -7790,7 +7872,14 @@ mod gc_tests {
77907872
save_state(tmp.path(), &state).await.unwrap();
77917873

77927874
let out = run_vendor_gc(&common, &manifest_path, false).await;
7793-
assert_eq!(out.failed, vec![PURL.to_string()], "{out:?}");
7875+
assert_eq!(
7876+
out.failed,
7877+
vec![(
7878+
PURL.to_string(),
7879+
"this build has no vendor backend for ecosystem `frobnicate`".to_string()
7880+
)],
7881+
"{out:?}"
7882+
);
77947883
assert!(out.dropped_reverted.is_empty(), "{out:?}");
77957884
assert!(
77967885
out.unused_reverted.is_empty(),
@@ -7824,7 +7913,16 @@ mod gc_tests {
78247913
save_state(tmp.path(), &state).await.unwrap();
78257914

78267915
let out = run_vendor_gc(&common, &manifest_path, false).await;
7827-
assert_eq!(out.failed, vec![PURL.to_string()], "{out:?}");
7916+
assert_eq!(
7917+
out.failed,
7918+
vec![(
7919+
PURL.to_string(),
7920+
"refusing revert: `deadbeef` is not a canonical patch uuid (tampered \
7921+
state.json?)"
7922+
.to_string()
7923+
)],
7924+
"{out:?}"
7925+
);
78287926
assert!(out.unused_reverted.is_empty(), "{out:?}");
78297927
assert!(out.dropped_reverted.is_empty(), "{out:?}");
78307928
assert!(

‎crates/socket-patch-cli/src/commands/vendored_backend/mod.rs‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::path::Path;
1717

1818
use socket_patch_core::manifest::schema::{PatchManifest, PatchRecord};
1919
use socket_patch_core::vendor::{
20-
save_state, RevertOpts, VendorServiceConfig, VendorState, VendorWarning,
20+
save_state, RevertOpts, RevertOutcome, VendorServiceConfig, VendorState, VendorWarning,
2121
};
2222

2323
use crate::args::GlobalArgs;
@@ -186,11 +186,7 @@ impl<'a> VendoredBackend<'a> {
186186
let step = if !outcome.success {
187187
VendorRevertStep::Failed(outcome.error.unwrap_or_else(|| "unknown error".into()))
188188
} else if outcome.kept_artifact {
189-
VendorRevertStep::Kept(if outcome.kept_for_residual_reference() {
190-
KeepCause::Reference
191-
} else {
192-
KeepCause::Drift
193-
})
189+
VendorRevertStep::Kept(KeepCause::of(&outcome))
194190
} else if opts.dry_run {
195191
VendorRevertStep::WouldRevert
196192
} else if opts.keep_artifact {
@@ -501,6 +497,15 @@ pub(crate) enum KeepCause {
501497
}
502498

503499
impl KeepCause {
500+
/// Why `outcome`, a revert that kept its artifact, kept it.
501+
pub(crate) fn of(outcome: &RevertOutcome) -> Self {
502+
if outcome.kept_for_residual_reference() {
503+
KeepCause::Reference
504+
} else {
505+
KeepCause::Drift
506+
}
507+
}
508+
504509
/// The per-entry reason, after "`<what>` kept".
505510
pub(crate) fn reason(self) -> &'static str {
506511
match self {

‎crates/socket-patch-cli/tests/apply/apply_silent_staging_errors.rs‎

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@
2424
use crate::apply_fixture::{run_apply, write_sourceless_npm_patches};
2525
use crate::common::envelope::{codes_in, json_string, parse_json_envelope};
2626

27-
/// Non-error stderr lines: drop the unconditional core API-token warning
28-
/// (both its lead line and its "Got: ... Continuing anyway" continuation)
29-
/// and blank lines, keep everything else.
30-
fn stderr_chatter(stderr: &str) -> Vec<String> {
31-
stderr
32-
.lines()
33-
.filter(|l| {
34-
!l.contains("SOCKET_API_TOKEN")
35-
&& !l.contains("Continuing anyway")
36-
&& !l.trim().is_empty()
37-
})
38-
.map(|l| l.to_string())
39-
.collect()
40-
}
41-
4227
/// A guaranteed-unreachable local endpoint: bind an ephemeral port, then
4328
/// release it, so every request fails fast with connection-refused.
4429
fn dead_endpoint() -> String {
@@ -65,9 +50,8 @@ fn apply_silent_offline_missing_source_keeps_error_output() {
6550
stdout.trim().is_empty(),
6651
"silent human mode writes errors to stderr, not stdout: {stdout}"
6752
);
68-
let chatter = stderr_chatter(&stderr);
6953
assert!(
70-
chatter.iter().any(|l| l.contains("no local source")),
54+
stderr.contains("no local source"),
7155
"--silent must keep the offline no-source error (errors only, \
7256
never nothing); stderr was: {stderr:?}"
7357
);
@@ -102,16 +86,13 @@ fn apply_silent_online_download_failure_keeps_error_output() {
10286
stdout.trim().is_empty(),
10387
"silent human mode writes errors to stderr, not stdout: {stdout}"
10488
);
105-
let chatter = stderr_chatter(&stderr);
10689
assert!(
107-
chatter
108-
.iter()
109-
.any(|l| l.contains("could not be downloaded")),
90+
stderr.contains("could not be downloaded"),
11091
"--silent must keep the download-failure error (errors only, \
11192
never nothing); stderr was: {stderr:?}"
11293
);
11394
assert!(
114-
chatter.iter().any(|l| l.contains("Failed to download")),
95+
stderr.contains("Failed to download"),
11596
"--silent must keep the per-blob failure reasons with the error; \
11697
stderr was: {stderr:?}"
11798
);
@@ -155,9 +136,8 @@ fn apply_json_offline_missing_source_keeps_stderr_clean() {
155136
0,
156137
"the bail must not stage anything into .socket/blobs"
157138
);
158-
let chatter = stderr_chatter(&stderr);
159139
assert!(
160-
chatter.is_empty(),
140+
stderr.is_empty(),
161141
"--json suppresses the staging diagnostics on stderr (the \
162142
envelope is the machine channel); stderr was: {stderr:?}"
163143
);

0 commit comments

Comments
 (0)