Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/socket-patch-cli/CLI_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@ Agent runs fetch patch content as per-file blobs (v5.0 removed `--download-mode`

**Vendored artifact repair (v5.0)**: `repair` checks the committed artifact and downloads a replacement for the same UUID into a temporary location. Before replacement it checks transfer integrity, patched-member hashes, and the original ledger's SHA-256 and size (file artifacts) or complete file inventory (directory artifacts). JVM repair also reproduces and checks the recorded repository metadata. Different downloaded bytes or inventories are refused; the old files, ledger and lockfiles remain intact. No installed package or patch blobs are required, and none are used to construct a replacement. Missing directory inventories cannot establish an exact replacement and require explicit re-vendoring.

Successful redownloads retain the JSON `rebuilt` action and `summary.rebuilt` for compatibility, with `details.redownloaded`. Dry runs report `details.wouldRedownload` without writing. Human output says “Redownloaded”. Failed repair downloads report `vendor_artifact_redownload_failed`; the same failure during `vendor` reports `vendor_redownload_failed`. Offline repair refuses missing or corrupt artifacts even if installed copies and patch blobs exist. Revert and explicitly vendor again to adopt different bytes; repair never refreshes fingerprints or rewrites lockfiles to accept them.
Successful redownloads retain the JSON `rebuilt` action and `summary.rebuilt` for compatibility, with `details.redownloaded`. Dry runs report `details.wouldRedownload` without writing. Human output says “Redownloaded”, from `repair` and from the `vendor` run that restores a copy before it reports the package as already vendored. A wet run that redownloaded an artifact, or wrote the `.gitignore` / `.gitattributes` beside one (`vendor_git_metadata_written`), left files under `.socket/vendor/` that are not committed yet: `repair` ends with a “Next steps:” block that names that commit, and so does a `vendor` run that vendored nothing new, whose summary then reads “restored … files under .socket/vendor/” where it would say “nothing to do”. Failed repair downloads report `vendor_artifact_redownload_failed`; the same failure during `vendor` reports `vendor_redownload_failed`. Offline repair refuses missing or corrupt artifacts even if installed copies and patch blobs exist. Revert and explicitly vendor again to adopt different bytes; repair never refreshes fingerprints or rewrites lockfiles to accept them.

**The ledger is not rebuilt from lockfiles (v5.0).** A lockfile reference to
`.socket/vendor/<eco>/<uuid>/...` with NO ledger entry (state.json deleted or never committed)
Expand Down Expand Up @@ -2918,6 +2918,7 @@ command's integration tests check its output.
| `redirect_yarn_classic_dep_manifest_unlocked` | `warnings[]` (warning) | scan/get `--mode hosted` (yarn classic): the served tarball's own `package.json` depends on a descriptor no `yarn.lock` block is keyed by and that the sub-maps of the registry blocks being pinned don't already name (yarn keys those by the packument's ranges, #1578). yarn 1 installs only what the lock names, so a pin would install the patched package without that dependency (#591). The dep is not pinned and never confirmed; the lock is left as it was. The served tarball is read on every run, so a pin an earlier release wrote without this check (#1462) gets the same warning and is never confirmed either; it stays in the lock until the dependency is locked or the pin is rolled back. Same remedy as `vendor_dep_manifest_unlocked`. |
| `redirect_yarn_classic_dep_manifest_rewritten` | `warnings[]` (warning) | scan/get `--mode hosted` (yarn classic): the served tarball's own `package.json` declares other dependencies than the pinned block's sub-maps, every descriptor already locked; the block's `dependencies:` / `optionalDependencies:` sub-maps are rewritten to match, in the shape yarn 1 writes for that manifest (#591, #1558). |
| `vendor_artifact_gitignored` | `failed` | vendor (vlt, the npm-family tarball flavors — npm, pnpm, bun, yarn classic, yarn berry — NuGet, every PyPI flavor, gems, and the JVM trees of Maven, Gradle, sbt and scala-cli): inside a git work tree, `git check-ignore --no-index` reports the new artifact's uuid directory (JVM: its tree root, such as `.socket/vendor/maven2`) as ignored by a rule its own `.gitignore` cannot override (such as a root `.socket/` or `vendor/` rule; the detail names the rule). Remedy: drop that rule for `.socket/vendor/`. Refused before any write, dry run included. A file rule such as `*.tgz`, `*.nupkg`, `*.whl` or `*.jar` is overridden by the `!*` `.gitignore` vendoring writes into the uuid dir or tree root (PyPI's uuid dir also gets the `.gitattributes` that unsets a `*.whl filter=lfs` rule); if a written tarball, directory payload, nupkg or wheel, or a file of a gem's copy, still reads as ignored, the run refuses and removes the uuid dir it created. |
| `vendor_git_metadata_written` | `skipped` (advisory; human: `Note: …`) | vendor / scan / get `--mode vendored` (gem) and `repair` (gem and the npm family): a run over a copy that was already in place wrote its `<uuid>/.gitignore` or `<uuid>/.gitattributes`, which were missing (a gem that 5.1 or earlier vendored has neither) or changed. The detail names both files. The package is still reported as already vendored; the human summary says files were restored instead of "nothing to do", and "Next steps:" names the commit of `.socket/vendor/`. A dry run writes nothing and reports nothing. |
| `vendor_gem_copy_gitignored` | warning | vendor (a gem vendored as its own directory by 5.1 or earlier): the in-sync run that writes the missing `<uuid>/.gitignore` and `.gitattributes` asks git about every file of the copy, and a `.gitignore` the gem ships, which that release kept as shipped, still keeps a file out of the commit (the detail names the rule and the file), so a fresh clone would fail `vendor --check` with `vendor_inventory_mismatch`. The copy and the ledger are left as they are. Remedy: `git add -f <copy dir>` and commit. Said once, by the run that writes the pair. |
| `vendor_gem_gitignore_neutralized` | warning | vendor / scan / get `--mode vendored` (gem): the patched gem ships a `.gitignore` inside its own tree whose rules would keep git from committing files of the vendored copy, so the run emptied it in the copy: every one in a platform gem's tree, and in a gem's own tree each one whose rules match a file of the copy. The detail names the gem and the emptied files (three, then a count of the rest). The gem is vendored; commit the copy. |
| `vendor_artifact_gitignore_unchecked` | warning | vendor (vlt, the npm-family tarball flavors, NuGet, every PyPI flavor and gems): git is installed but could not answer the ignore check for the written vendored directory (it failed to start, ran past 30 s, or `rev-parse` / `check-ignore` exited with an error); the package is vendored and the detail names what failed. Remedy: make sure no ignore rule covers `.socket/` before committing. Git absent, or a project outside any work tree, raises nothing. |
Expand Down
49 changes: 49 additions & 0 deletions crates/socket-patch-cli/src/commands/repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,24 @@ fn format_cleanup_summary(results: &[(ArtifactNoun, CleanupResult)], dry_run: bo
format_all_in_use(&checked, total)
}

/// Did the vendored leg of a wet run restore files under `.socket/vendor/`
/// (`events` are its own)? A vendored artifact downloaded again (a
/// `rebuilt` event with `details.redownloaded`, which a dry run never
/// records), or the `.gitignore` / `.gitattributes` beside one (the
/// `vendor_git_metadata_written` advisory). Either leaves files to commit.
/// The other `rebuilt` events (a stray file swept from a JVM tree, a Bun
/// workspace member's copy) are no such restore.
fn restored_vendor_files(events: &[PatchEvent]) -> bool {
events.iter().any(|e| {
let redownloaded = || {
let details = e.details.as_ref();
details.is_some_and(|d| d["redownloaded"] == serde_json::json!(true))
};
(e.action == PatchAction::Rebuilt && redownloaded())
|| e.error_code.as_deref() == Some(socket_patch_core::vendor::GIT_METADATA_WRITTEN)
})
}

/// The closing line of a human repair run. `offline_unrestored` counts the
/// missing or corrupt blobs `--offline` left as they were. `other_failure`
/// is a failure recorded elsewhere in the run (a vendored artifact that
Expand Down Expand Up @@ -893,6 +911,14 @@ async fn repair_inner(
}
println!("{line}");
}
// Files restored under `.socket/vendor/` are in this checkout
// alone until they are committed: a fresh clone still fails
// `vendor --check` (#2471).
if restored_vendor_files(&env.events[vendored_since..]) {
for line in crate::ui::next_steps_restored() {
println!("{line}");
}
}
}

// Translate the aggregate counts into envelope events. `repair`
Expand Down Expand Up @@ -1508,6 +1534,29 @@ mod tests {
);
}

/// #2471: the commit step follows a run whose vendored leg restored
/// files, and no other.
#[test]
fn restored_vendor_files_reads_rebuilds_and_written_metadata() {
let purl = "pkg:gem/rack@3.2.9";
let rebuilt = PatchEvent::new(PatchAction::Rebuilt, purl)
.with_details(serde_json::json!({"path": "p", "redownloaded": true}));
// A stray file swept from a JVM tree: nothing restored.
let swept = PatchEvent::new(PatchAction::Rebuilt, purl)
.with_details(serde_json::json!({"unindexedRemoved": ["x"]}));
let written = PatchEvent::new(PatchAction::Skipped, purl).with_reason(
socket_patch_core::vendor::GIT_METADATA_WRITTEN,
"restored the pair",
);
let preview = PatchEvent::new(PatchAction::Verified, purl);
let other =
PatchEvent::new(PatchAction::Skipped, purl).with_reason("cargo_version_tagged", "x");
assert!(restored_vendor_files(std::slice::from_ref(&rebuilt)));
assert!(restored_vendor_files(&[other.clone(), written]));
assert!(!restored_vendor_files(&[preview, other, swept]));
assert!(!restored_vendor_files(&[]));
}

#[test]
fn final_line_reflects_failures_and_dry_run() {
assert_eq!(format_final_line(0, 0, false, false), "Repair complete.");
Expand Down
163 changes: 161 additions & 2 deletions crates/socket-patch-cli/src/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,8 @@ fn advisory_tier(code: &str) -> AdvisoryTier {
| "vendor_would_revert_redirect"
| "vendor_takeover_reverted_redirect"
| "cargo_wiring_migrated"
| "cargo_version_tagged" => AdvisoryTier::Note,
| "cargo_version_tagged"
| vendor::GIT_METADATA_WRITTEN => AdvisoryTier::Note,
_ => AdvisoryTier::Warning,
}
}
Expand Down Expand Up @@ -928,6 +929,26 @@ fn vendored_packages(env: &Envelope) -> u32 {
}))
}

/// The packages whose files under `.socket/vendor/` a wet run restored
/// without vendoring them anew: a copy the run downloaded again (the
/// `rebuilt` event), or the uuid dir's `.gitignore` / `.gitattributes`
/// (the `vendor_git_metadata_written` advisory). Both leave files to
/// commit behind a package the run reports as already vendored (#2471).
/// The events of one package carry one purl, so each counts once.
fn restored_packages(env: &Envelope) -> u32 {
let restored = |e: &&PatchEvent| {
e.action == PatchAction::Rebuilt
|| e.error_code.as_deref() == Some(vendor::GIT_METADATA_WRITTEN)
};
let purls: HashSet<&str> = env
.events
.iter()
.filter(restored)
.filter_map(|e| e.purl.as_deref())
.collect();
purls.len() as u32
}

/// Per-outcome counts behind the human vendor summary line.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct VendorTally {
Expand All @@ -940,6 +961,11 @@ struct VendorTally {
/// Every other skip.
skipped: u32,
failed: u32,
/// Packages whose files under `.socket/vendor/` this run restored
/// without vendoring them anew ([`restored_packages`]). They are
/// counted by their outcome as well (as a rule, already vendored), and
/// the summary must not call the run a no-op.
restored: u32,
}

impl VendorTally {
Expand Down Expand Up @@ -977,6 +1003,7 @@ impl VendorTally {
.summary
.failed
.saturating_sub(repeats(PatchAction::Failed)),
restored: restored_packages(env),
}
}
}
Expand All @@ -991,7 +1018,15 @@ fn format_vendor_summary(dry_run: bool, t: &VendorTally) -> String {
} else {
format!("All {} packages are", t.already)
};
return format!("{all} already vendored; nothing to do.");
// A run that restored files of a vendored copy did something,
// and left files to commit.
return match t.restored {
0 => format!("{all} already vendored; nothing to do."),
_ if t.already == 1 => {
format!("{all} already vendored; restored its files under .socket/vendor/.")
}
n => format!("{all} already vendored; restored files of {n} under .socket/vendor/."),
};
}
let verb = if dry_run { "Would vendor" } else { "Vendored" };
let mut line = format!(
Expand All @@ -1008,6 +1043,13 @@ fn format_vendor_summary(dry_run: bool, t: &VendorTally) -> String {
line.push_str(&format!("; {n} {what}"));
}
}
// A clause of its own: the package may be one the run then failed.
if t.restored > 0 {
line.push_str(&format!(
"; restored files of {} under .socket/vendor/",
t.restored
));
}
line.push('.');
line
}
Expand Down Expand Up @@ -4814,6 +4856,16 @@ pub(crate) async fn vendor_records_reusing(
for warning in &warnings {
record_warning(env, candidate, warning, common);
}
// Said as `repair` says it: the package is
// reported as already vendored below, and the
// files this put back are to be committed (#2471).
if !common.json && !common.silent {
println!(
"Redownloaded {} ({})",
normalize_purl(candidate),
entry.artifact.path
);
}
env.record(PatchEvent::new(PatchAction::Rebuilt, candidate.clone()).with_details(serde_json::json!({"redownloaded": true, "path": entry.artifact.path})));
}
Err(detail) => {
Expand Down Expand Up @@ -5409,6 +5461,16 @@ fn print_vendor_closing(
println!("{line}");
}
}
// Nothing was vendored anew, but files of a vendored copy were
// restored: the lockfiles and the installed packages are as they were,
// and the restored files still have to be committed (#2471). A run
// that vendored something names `.socket/vendor/` in its own commit
// step below.
if env.summary.applied == 0 && tally.restored > 0 && !common.dry_run && next_steps {
for line in crate::ui::next_steps_restored() {
println!("{line}");
}
}
if env.summary.applied > 0 && !common.dry_run && next_steps {
// pnpm >=11 reads `overrides` ONLY from pnpm-workspace.yaml (the
// package.json `pnpm.overrides` mirror is ignored), so pnpm-wired
Expand Down Expand Up @@ -9557,9 +9619,106 @@ mod ui_format_tests {
not_installed,
skipped,
failed,
restored: 0,
}
}

/// #2471: a run that restored files of a copy that was already vendored
/// (its uuid dir's git metadata, or the copy downloaded again) is not
/// a no-op: the summary says what it restored, in every shape of the
/// line.
#[test]
fn vendor_summary_names_restored_files_instead_of_nothing_to_do() {
let restored = |already, restored| VendorTally {
restored,
..tally(0, already, 0, 0, 0)
};
assert_eq!(
format_vendor_summary(false, &restored(3, 3)),
"All 3 packages are already vendored; restored files of 3 under .socket/vendor/."
);
assert_eq!(
format_vendor_summary(false, &restored(3, 1)),
"All 3 packages are already vendored; restored files of 1 under .socket/vendor/."
);
assert_eq!(
format_vendor_summary(false, &restored(1, 1)),
"1 package is already vendored; restored its files under .socket/vendor/."
);
let mixed = VendorTally {
restored: 1,
..tally(1, 2, 0, 0, 1)
};
assert_eq!(
format_vendor_summary(false, &mixed),
"Vendored 1 package; 2 already vendored; 1 failed; restored files of 1 under \
.socket/vendor/."
);
// Restored, then refused: no package is in sync, and the restore
// is still said.
let failed = VendorTally {
restored: 1,
..tally(0, 0, 0, 0, 1)
};
assert_eq!(
format_vendor_summary(false, &failed),
"Vendored 0 packages; 1 failed; restored files of 1 under .socket/vendor/."
);
}

/// The tally counts a package once however many of its events say
/// files were restored: the `rebuilt` event of a copy downloaded again
/// and the advisory for the uuid dir's git metadata. A gem vendored as
/// platform gems carries both under the patch it was vendored through.
#[test]
fn tally_counts_each_package_with_restored_files_once() {
let written = |env: &mut Envelope, purl: &str| {
let detail = vendor::git_metadata_written_detail(".socket/vendor/gem/u", "x@1");
push_advisory_event(
env,
purl,
&VendorWarning::new(vendor::GIT_METADATA_WRITTEN, detail),
);
};
let in_sync = |env: &mut Envelope, purl: &str| {
env.record(
PatchEvent::new(PatchAction::Skipped, purl)
.with_reason("already_vendored", "in sync"),
);
};
let mut env = Envelope::new(Command::Vendor);
// Downloaded again and given its metadata: one package.
env.record(PatchEvent::new(PatchAction::Rebuilt, "pkg:gem/a@1"));
written(&mut env, "pkg:gem/a@1");
in_sync(&mut env, "pkg:gem/a@1");
// Metadata alone.
written(&mut env, "pkg:gem/b@1");
in_sync(&mut env, "pkg:gem/b@1");
// Untouched.
in_sync(&mut env, "pkg:gem/c@1");
let counted = VendorTally::from_envelope(&env, false, 0);
assert_eq!(
counted,
VendorTally {
restored: 2,
..tally(0, 3, 0, 0, 0)
}
);
assert_eq!(
format_vendor_summary(false, &counted),
"All 3 packages are already vendored; restored files of 2 under .socket/vendor/."
);
}

/// The advisory is a note (the run did its job), not a warning.
#[test]
fn git_metadata_written_prints_as_a_note() {
let detail = vendor::git_metadata_written_detail(".socket/vendor/gem/u", "rack@3.2.9");
let line = format_advisory(vendor::GIT_METADATA_WRITTEN, &detail, false).unwrap();
assert!(line.starts_with("Note: restored .socket/vendor/gem/u/.gitignore and "));
assert!(line.ends_with("Commit them with the rest of .socket/vendor/"));
}

#[test]
fn vendor_summary_singular_plural_and_zero() {
assert_eq!(
Expand Down
Loading
Loading