Skip to content

Commit 2f5652b

Browse files
committed
Fix opt-in metadata refresh and scan output
Refresh same-UUID activation metadata, including clearing it, and record the new fields as known manifest keys. Hosted, repeated-agent, and dry-run scans render sanitized notes once per patch UUID. Link nested apply deduplication to the UUIDs its caller already showed. Enrich scan JSON events from both batch and detailed API records. Assisted-by: Claude Code:kimi-k3p1-pro
1 parent ef1586f commit 2f5652b

15 files changed

Lines changed: 380 additions & 75 deletions

File tree

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use socket_patch_core::api::ranking::severity_order;
1111
use socket_patch_core::api::types::{PatchResponse, PatchSearchResult, VulnerabilityResponse};
1212
use socket_patch_core::crawlers::{CrawlerOptions, Ecosystem};
1313
use socket_patch_core::manifest::operations::{read_manifest, write_manifest};
14-
use socket_patch_core::manifest::records::{build_patch_record, files_for_manifest};
14+
use socket_patch_core::manifest::records::{
15+
build_patch_record, files_for_manifest, refresh_opt_in_metadata,
16+
};
1517
use socket_patch_core::manifest::schema::{PatchFileInfo, PatchManifest, PatchRecord};
1618
use socket_patch_core::patch::apply::{is_valid_blob_hash, select_installed_variants_any};
1719
use socket_patch_core::patch::apply_lock::{LockError, LockGuard};
@@ -388,6 +390,7 @@ pub struct DownloadParams {
388390
/// `--patch-server-url`: the extra origin whose URLs count as hosted
389391
/// when lockfile discovery reads the project's hosted pins.
390392
pub patch_server_url: Option<String>,
393+
pub opt_in_notes_shown: Vec<String>,
391394
}
392395

393396
impl DownloadParams {
@@ -701,6 +704,7 @@ pub(crate) struct FetchBatch {
701704
failed: usize,
702705
/// Fetched, recordable patches in selection order.
703706
fetched: Vec<FetchedPatch>,
707+
metadata_refreshes: Vec<PatchResponse>,
704708
/// Ledger store only: `(purl, record)` reused from a detached entry
705709
/// already at the selected uuid (no fetch).
706710
reused: Vec<(String, PatchRecord)>,
@@ -912,6 +916,7 @@ pub(crate) async fn fetch_selected_patches(
912916
already_recorded: 0,
913917
failed: 0,
914918
fetched: Vec::new(),
919+
metadata_refreshes: Vec::new(),
915920
reused: Vec::new(),
916921
events: Vec::new(),
917922
warnings,
@@ -1039,13 +1044,15 @@ pub(crate) async fn fetch_selected_patches(
10391044
},
10401045
};
10411046
if action == PatchAction::Skipped {
1047+
batch.metadata_refreshes.push(patch.clone());
10421048
if !quiet {
10431049
eprintln!("{}", format_record_skip(&patch.purl, "already in manifest"));
10441050
}
10451051
batch.events.push(
10461052
PatchEvent::new(EventAction::Skipped, patch.purl.as_str())
10471053
.with_uuid(patch.uuid.as_str())
1048-
.with_reason(ALREADY_IN_MANIFEST, "already in manifest"),
1054+
.with_reason(ALREADY_IN_MANIFEST, "already in manifest")
1055+
.with_details(patch_event_metadata(&patch)),
10491056
);
10501057
batch.skipped += 1;
10511058
batch.already_recorded += 1;
@@ -1405,6 +1412,7 @@ pub(crate) async fn run_nested_apply(
14051412
common: GlobalArgs,
14061413
json: bool,
14071414
report_user_patch_skips: bool,
1415+
shown_opt_in_uuids: Vec<String>,
14081416
recorded: &[(String, String)],
14091417
client: &ApiClient,
14101418
lock: LockGuard,
@@ -1419,6 +1427,7 @@ pub(crate) async fn run_nested_apply(
14191427
nested: Some(super::apply::NestedApply {
14201428
caller_json: json,
14211429
report_user_patch_skips,
1430+
shown_opt_in_uuids,
14221431
}),
14231432
};
14241433
// The nested apply runs on the host's client: seeded, never rebuilt.
@@ -1713,6 +1722,12 @@ pub async fn download_and_apply_patches_into(
17131722
// matching the per-patch event (`downloaded` / `updated`) and the
17141723
// single-uuid flow's summary in `save_and_apply_patch`; their sum gates
17151724
// the apply step.
1725+
let mut metadata_changed = false;
1726+
for patch in &batch.metadata_refreshes {
1727+
if let Some(record) = manifest.patches.get_mut(&patch.purl) {
1728+
metadata_changed |= refresh_opt_in_metadata(record, patch);
1729+
}
1730+
}
17161731
let downloaded = batch.fetched.len();
17171732
let mut updated = 0usize;
17181733
let mut new_blobs: Vec<String> = Vec::new();
@@ -1748,7 +1763,7 @@ pub async fn download_and_apply_patches_into(
17481763
let added = downloaded - updated;
17491764
// Write only when a record changed: an all-skipped or all-failed run
17501765
// leaves the manifest bytes (and a fresh project's tree) untouched.
1751-
if downloaded > 0 {
1766+
if downloaded > 0 || metadata_changed {
17521767
if let Err(e) = write_manifest(&manifest_path, &manifest).await {
17531768
// The blobs this run just wrote have no record pointing at them:
17541769
// unwind exactly those (a pre-existing record's blobs stay).
@@ -1819,6 +1834,7 @@ pub async fn download_and_apply_patches_into(
18191834
nested_apply_args_from_params(params, run, &manifest_path),
18201835
params.json,
18211836
!params.user_patch_skips_reported,
1837+
params.opt_in_notes_shown.clone(),
18221838
&recorded,
18231839
run.api_client,
18241840
lock,

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub struct ApplyArgs {
176176
}
177177

178178
/// What a nested apply knows about the command that runs it.
179-
#[derive(Clone, Copy, Debug, Default)]
179+
#[derive(Clone, Debug, Default)]
180180
pub struct NestedApply {
181181
/// The caller runs under `--json`. The nested run itself is never JSON
182182
/// (one envelope per command), but the caller's stdout is, so the
@@ -186,14 +186,15 @@ pub struct NestedApply {
186186
/// (`get`) checked only its own selection. `scan` already reported
187187
/// every protected installed package, so it passes `false`.
188188
pub report_user_patch_skips: bool,
189+
pub shown_opt_in_uuids: Vec<String>,
189190
}
190191

191192
impl ApplyArgs {
192193
/// Whether human-readable error lines go to stderr: never under
193194
/// `--json` (the envelope is the channel), always otherwise — errors
194195
/// are exempt from `--silent`.
195196
fn prints_errors(&self) -> bool {
196-
!self.common.json && !self.nested.is_some_and(|n| n.caller_json)
197+
!self.common.json && !self.nested.as_ref().is_some_and(|n| n.caller_json)
197198
}
198199
}
199200

@@ -1848,14 +1849,14 @@ pub(crate) async fn run_locked(
18481849
for line in block {
18491850
println!("{line}");
18501851
}
1851-
if args.nested.is_none() {
1852-
let notes = format_opt_in_notes(&opt_ins, &results, &unmatched);
1853-
if !notes.is_empty() {
1854-
println!("\nOpt-in required:");
1855-
for note in notes {
1856-
println!("{note}");
1857-
}
1858-
}
1852+
let shown_opt_in_uuids = args
1853+
.nested
1854+
.as_ref()
1855+
.map_or(&[][..], |nested| nested.shown_opt_in_uuids.as_slice());
1856+
if let Some(notes) =
1857+
format_opt_in_notes(&opt_ins, &results, &unmatched, shown_opt_in_uuids)
1858+
{
1859+
println!("\n{notes}");
18591860
}
18601861
if args.common.verbose && !results.is_empty() {
18611862
print_verbose_verification(&results);
@@ -2401,30 +2402,26 @@ fn format_opt_in_notes(
24012402
opt_ins: &OptInPatches,
24022403
results: &[ApplyResult],
24032404
unmatched: &[String],
2404-
) -> Vec<String> {
2405+
shown_opt_in_uuids: &[String],
2406+
) -> Option<String> {
24052407
let shown: HashSet<&str> = results
24062408
.iter()
24072409
.map(|result| result.package_key.as_str())
24082410
.chain(unmatched.iter().map(String::as_str))
24092411
.collect();
2410-
let mut notes: Vec<(&str, &str)> = opt_ins
2411-
.iter()
2412-
.filter_map(|(purl, _, requires_opt_in, opt_in_note)| {
2413-
if requires_opt_in != &Some(true) || !shown.contains(purl.as_str()) {
2412+
crate::ui::opt_in_note_block(opt_ins.iter().filter_map(
2413+
|(purl, uuid, requires_opt_in, opt_in_note)| {
2414+
if !shown.contains(purl.as_str()) || shown_opt_in_uuids.contains(uuid) {
24142415
return None;
24152416
}
2416-
let note = opt_in_note.as_deref()?;
2417-
(!note.trim().is_empty()).then_some((purl.as_str(), note))
2418-
})
2419-
.collect();
2420-
notes.sort_unstable();
2421-
notes
2422-
.into_iter()
2423-
.map(|(purl, note)| {
2424-
let note = crate::ui::terminal_text(note).replace('\n', "\n ");
2425-
format!(" {}: {note}", normalize_purl(purl))
2426-
})
2427-
.collect()
2417+
Some(crate::ui::OptInEntry {
2418+
purl,
2419+
uuid,
2420+
requires_opt_in: *requires_opt_in,
2421+
opt_in_note: opt_in_note.as_deref(),
2422+
})
2423+
},
2424+
))
24282425
}
24292426

24302427
/// One `Patched packages:` line. `copy` names the physical copy when a

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use socket_patch_core::api::types::{
1111
use socket_patch_core::crawlers::fuzzy_match::fuzzy_match_packages;
1212
use socket_patch_core::formats::pnpm::PnpmLock;
1313
use socket_patch_core::manifest::operations::{read_manifest, write_manifest};
14-
use socket_patch_core::manifest::records::{build_patch_record, files_for_manifest};
14+
use socket_patch_core::manifest::records::{
15+
build_patch_record, files_for_manifest, refresh_opt_in_metadata,
16+
};
1517
use socket_patch_core::manifest::schema::PatchManifest;
1618
use socket_patch_core::telemetry::{track_patch_fetch_failed, track_patch_fetched, TelemetryAuth};
1719
use socket_patch_core::utils::concurrent::{api_concurrency_for, ordered_concurrent};
@@ -1810,7 +1812,8 @@ pub async fn run(args: GetArgs) -> i32 {
18101812
}
18111813

18121814
// Download and apply (agent mode), with the run's client and flags.
1813-
let params = get_download_params(&args, args.save_only, /*persist_blobs=*/ true);
1815+
let mut params = get_download_params(&args, args.save_only, /*persist_blobs=*/ true);
1816+
params.opt_in_notes_shown = selected.iter().map(|patch| patch.uuid.clone()).collect();
18141817
let run = DownloadRun {
18151818
api_client: &api_client,
18161819
lock_timeout: args.common.lock_timeout,
@@ -1994,6 +1997,19 @@ async fn save_patch_record(
19941997
// not `added` — consumers diff manifest replacements on that action.
19951998
let action = decide_patch_action(&manifest, &patch.purl, &patch.uuid);
19961999
if action == PatchAction::Skipped {
2000+
let refreshed = manifest
2001+
.patches
2002+
.get_mut(&patch.purl)
2003+
.is_some_and(|record| refresh_opt_in_metadata(record, patch));
2004+
if refreshed {
2005+
if let Err(e) = write_manifest(manifest_path, &manifest).await {
2006+
return Err(report_error(
2007+
&args.common,
2008+
"manifest_write_failed",
2009+
format!("Failed to write manifest: {e}"),
2010+
));
2011+
}
2012+
}
19972013
return Ok(action);
19982014
}
19992015

@@ -2146,6 +2162,7 @@ async fn save_and_apply_patch(
21462162
nested_apply_args(&args.common, &manifest_path, quiet),
21472163
args.common.json,
21482164
true,
2165+
vec![patch.uuid.clone()],
21492166
&recorded,
21502167
client,
21512168
lock,
@@ -2265,6 +2282,7 @@ fn get_download_params(args: &GetArgs, save_only: bool, persist_blobs: bool) ->
22652282
ecosystems: args.common.ecosystems.clone(),
22662283
persist_blobs,
22672284
patch_server_url: args.common.patch_server_url.clone(),
2285+
opt_in_notes_shown: Vec::new(),
22682286
}
22692287
}
22702288

@@ -4069,6 +4087,7 @@ mod tests {
40694087
ecosystems: None,
40704088
persist_blobs: false,
40714089
patch_server_url: None,
4090+
opt_in_notes_shown: Vec::new(),
40724091
}
40734092
}
40744093

@@ -4769,6 +4788,7 @@ mod tests {
47694788
// The vendor-detached posture this fn exists for.
47704789
persist_blobs: false,
47714790
patch_server_url: None,
4791+
opt_in_notes_shown: Vec::new(),
47724792
}
47734793
}
47744794

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,20 @@ async fn run_redirect_selected_untracked(
21472147
"{}",
21482148
format_hosted_summary(confirmed.len(), already, human_files.len(), common.dry_run)
21492149
);
2150+
if let Some(notes) = rollout.as_ref().and_then(|gate| {
2151+
crate::ui::opt_in_note_block(
2152+
gate.rows
2153+
.iter()
2154+
.filter(|row| {
2155+
selected.iter().any(|(purl, uuid)| {
2156+
purl == &row.writer.purl && uuid == &row.writer.uuid
2157+
})
2158+
})
2159+
.map(|row| crate::ui::OptInEntry::from_search(&row.writer)),
2160+
)
2161+
}) {
2162+
println!("\n{notes}");
2163+
}
21502164
// Under it, per package on a hosted patch: the advisories that
21512165
// patch fixes and the ones it leaves unpatched.
21522166
if let Some(gate) = &rollout {

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ async fn discover_selected(
556556
show_progress: bool,
557557
warn: bool,
558558
detail_error_line: bool,
559-
json_warnings: Option<&mut Envelope>,
559+
mut json_warnings: Option<&mut Envelope>,
560560
) -> Result<Discovered, (i32, String)> {
561561
let (all_search_results, failures) =
562562
fetch_patch_details(api_client, packages, show_progress, warn).await;
@@ -577,6 +577,17 @@ async fn discover_selected(
577577
// Some queries failed, some succeeded: a `--json` run has no stderr
578578
// warning (`warn` is human-only), so each failed package becomes a
579579
// run-level `warnings[]` entry — never a silent drop from the envelope.
580+
if let Some(env) = json_warnings.as_deref_mut() {
581+
for patch in &all_search_results {
582+
env.register_opt_in(
583+
&patch.purl,
584+
&patch.uuid,
585+
patch.requires_opt_in,
586+
patch.opt_in_note.as_deref(),
587+
);
588+
}
589+
env.add_registered_opt_in_to_events();
590+
}
580591
let offers = select_accessible(all_search_results, can_access_paid_patches, policy);
581592
if let Some(env) = json_warnings {
582593
for (purl, e) in &failures {
@@ -979,6 +990,7 @@ fn download_params(args: &ScanArgs, save_only: bool, json: bool, silent: bool) -
979990
ecosystems: args.common.ecosystems.clone(),
980991
persist_blobs: args.mode != Some(ScanMode::Vendored),
981992
patch_server_url: args.common.patch_server_url.clone(),
993+
opt_in_notes_shown: Vec::new(),
982994
}
983995
}
984996

@@ -2950,6 +2962,16 @@ async fn scan_project(
29502962

29512963
if args.common.json {
29522964
let mut env = scan_envelope(&args.common);
2965+
for package in &all_packages_with_patches {
2966+
for patch in &package.patches {
2967+
env.register_opt_in(
2968+
&patch.purl,
2969+
&patch.uuid,
2970+
patch.requires_opt_in,
2971+
patch.opt_in_note.as_deref(),
2972+
);
2973+
}
2974+
}
29532975
env.set_extra("scannedPackages", serde_json::json!(package_count));
29542976
env.set_extra(
29552977
"lockfileOnlyPackages",
@@ -3593,6 +3615,14 @@ async fn scan_project(
35933615

35943616
if selected.is_empty() && (!reapply || already_recorded.is_empty()) {
35953617
if !silent {
3618+
if let Some(notes) = ui::opt_in_note_block(
3619+
selected
3620+
.iter()
3621+
.chain(&already_recorded)
3622+
.map(ui::OptInEntry::from_search),
3623+
) {
3624+
println!("\n{notes}");
3625+
}
35963626
open_paragraph(&mut skip_paragraph);
35973627
if !stage.deferred_keys().is_empty() {
35983628
if args.common.dry_run {
@@ -3659,6 +3689,17 @@ async fn scan_project(
36593689
}
36603690
}
36613691

3692+
if !silent && (vendor || report_only || args.common.dry_run) {
3693+
if let Some(notes) = ui::opt_in_note_block(
3694+
selected
3695+
.iter()
3696+
.chain(&already_recorded)
3697+
.map(ui::OptInEntry::from_search),
3698+
) {
3699+
println!("\n{notes}");
3700+
}
3701+
}
3702+
36623703
// A report-only run applies nothing, with or without `--dry-run`: both
36633704
// end with how to apply what was found, and the `--prune` GC (a
36643705
// read-only preview on a dry run). The preview never says it would

0 commit comments

Comments
 (0)