Skip to content

Commit 54692fa

Browse files
committed
Fetch views before opt-in warnings
Fetch authoritative patch views for same-UUID detached reuse, including search-based reuse, while preserving existing fields offline. Move human activation notes after download and persistence. Use one shared formatter keyed by UUID and note, and carry that exact displayed metadata into nested apply deduplication. Assisted-by: Claude Code:kimi-k3p1-pro
1 parent d1f94b3 commit 54692fa

9 files changed

Lines changed: 184 additions & 59 deletions

File tree

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

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ pub struct DownloadParams {
364364
pub global_prefix: Option<PathBuf>,
365365
pub json: bool,
366366
pub silent: bool,
367+
pub offline: bool,
367368
/// When `false` (the default — narrow), a release-variant package (PyPI
368369
/// `?artifact_id=`, RubyGems `?platform=`, Maven `?classifier=`) is
369370
/// filtered down to the variant(s) matching the locally-installed
@@ -390,7 +391,6 @@ pub struct DownloadParams {
390391
/// `--patch-server-url`: the extra origin whose URLs count as hosted
391392
/// when lockfile discovery reads the project's hosted pins.
392393
pub patch_server_url: Option<String>,
393-
pub opt_in_notes_shown: Vec<crate::ui::ShownOptIn>,
394394
}
395395

396396
impl DownloadParams {
@@ -853,7 +853,8 @@ pub(crate) fn claimed_purls(
853853
}
854854

855855
/// The record a detached ledger entry already carries for `purl` at
856-
/// exactly `uuid` — the ledger store's idempotency skip (no view fetch).
856+
/// exactly `uuid`. Online reuse still fetches its view for authoritative
857+
/// activation metadata; offline reuse keeps the existing fields.
857858
/// Always `None` for the manifest store.
858859
pub(crate) fn detached_ledger_record<'a>(
859860
store: RecordStore<'a>,
@@ -973,12 +974,39 @@ pub(crate) async fn fetch_selected_patches(
973974
}
974975

975976
// Idempotency (ledger store): a detached entry already at this uuid
976-
// carries its own record — no view fetch needed.
977+
// retains its artifact and wiring; its view still governs activation metadata.
977978
if let Some(mut record) = detached_ledger_record(store, purl, uuid).cloned() {
978-
if let Some(patch) = prefetched.get(uuid) {
979+
if !params.offline {
980+
let view = match prefetched.remove(uuid) {
981+
Some(patch) => Ok(Some(patch)),
982+
None => api_client.fetch_patch(uuid).await,
983+
};
984+
let patch = match view {
985+
Ok(Some(patch)) => patch,
986+
Ok(None) => {
987+
batch.fail(
988+
params.json,
989+
Some(format!("[fail] {purl} (could not fetch details)")),
990+
(purl, uuid),
991+
DOWNLOAD_FAILED,
992+
"could not fetch details",
993+
);
994+
continue;
995+
}
996+
Err(e) => {
997+
batch.fail(
998+
params.json,
999+
Some(format!("[fail] {purl} ({e})")),
1000+
(purl, uuid),
1001+
DOWNLOAD_FAILED,
1002+
&e.to_string(),
1003+
);
1004+
continue;
1005+
}
1006+
};
9791007
record.requires_opt_in = patch.requires_opt_in;
9801008
record.opt_in_note = patch.opt_in_note.clone();
981-
batch.metadata_refreshes.push(patch.clone());
1009+
batch.metadata_refreshes.push(patch);
9821010
}
9831011
if !quiet {
9841012
eprintln!("{}", format_record_skip(purl, "already vendored"));
@@ -1182,6 +1210,13 @@ impl DetachedDownload {
11821210
record.requires_opt_in,
11831211
record.opt_in_note.as_deref(),
11841212
);
1213+
} else {
1214+
env.register_opt_in(
1215+
purl,
1216+
&record.uuid,
1217+
record.requires_opt_in,
1218+
record.opt_in_note.as_deref(),
1219+
);
11851220
}
11861221
}
11871222
for event in self.events {
@@ -1859,6 +1894,16 @@ pub async fn download_and_apply_patches_into(
18591894
);
18601895
}
18611896

1897+
if !quiet {
1898+
if let Some(notes) = env.opt_in_note_block() {
1899+
println!("\n{notes}");
1900+
}
1901+
}
1902+
let shown_opt_ins: Vec<_> = recorded
1903+
.iter()
1904+
.filter_map(|(_, uuid)| env.shown_opt_ins(uuid))
1905+
.collect();
1906+
18621907
// Auto-apply unless --save-only (the lock decision above).
18631908
let mut apply_report: Option<ApplyRunReport> = None;
18641909
if let Some(lock) = apply_lock {
@@ -1874,7 +1919,7 @@ pub async fn download_and_apply_patches_into(
18741919
nested_apply_args_from_params(params, run, &manifest_path),
18751920
params.json,
18761921
!params.user_patch_skips_reported,
1877-
params.opt_in_notes_shown.clone(),
1922+
shown_opt_ins,
18781923
&recorded,
18791924
run.api_client,
18801925
lock,

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

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,6 @@ fn format_search_results(
369369
if !desc.is_empty() {
370370
out.push_str(&format!(" Description: {desc}\n"));
371371
}
372-
if patch.requires_opt_in == Some(true) {
373-
if let Some(note) = &patch.opt_in_note {
374-
let note = crate::ui::terminal_text(note).replace('\n', "\n ");
375-
out.push_str(&format!(" Opt-in required: {note}\n"));
376-
}
377-
}
378372
let mut fixes: Vec<(String, String)> = patch
379373
.vulnerabilities
380374
.iter()
@@ -1292,15 +1286,6 @@ pub async fn run(args: GetArgs) -> i32 {
12921286
crate::ui::stderr_color(),
12931287
)
12941288
);
1295-
if patch.requires_opt_in == Some(true) {
1296-
if let Some(note) = &patch.opt_in_note {
1297-
let note = crate::ui::terminal_text(note).replace('\n', "\n ");
1298-
println!(
1299-
"Opt-in required for {}: {note}",
1300-
crate::ui::terminal_purl(&patch.purl)
1301-
);
1302-
}
1303-
}
13041289
}
13051290

13061291
// Record the fetch BEFORE the save+apply step so the
@@ -1349,7 +1334,7 @@ pub async fn run(args: GetArgs) -> i32 {
13491334
// re-fetching with a fresh one, which would re-hit the
13501335
// 401/403 the fallback just recovered from. An explicit
13511336
// UUID is exempt from installed narrowing (exact intent).
1352-
return match mode {
1337+
let code = match mode {
13531338
// Save to manifest and apply in place.
13541339
super::scan::ScanMode::Agent => {
13551340
save_and_apply_patch(&args, &api_client, &patch, &uuid_warnings).await
@@ -1371,6 +1356,14 @@ pub async fn run(args: GetArgs) -> i32 {
13711356
.await
13721357
}
13731358
};
1359+
if !quiet {
1360+
if let Some(notes) =
1361+
crate::ui::opt_in_note_block([crate::ui::OptInEntry::from_response(&patch)])
1362+
{
1363+
println!("\n{notes}");
1364+
}
1365+
}
1366+
return code;
13741367
}
13751368
// The public proxy answers a paid patch with 403 rather than
13761369
// a tier=paid view: the same outcome as the branch above, not
@@ -1812,11 +1805,7 @@ pub async fn run(args: GetArgs) -> i32 {
18121805
}
18131806

18141807
// Download and apply (agent mode), with the run's client and flags.
1815-
let mut params = get_download_params(&args, args.save_only, /*persist_blobs=*/ true);
1816-
params.opt_in_notes_shown = selected
1817-
.iter()
1818-
.map(crate::ui::ShownOptIn::from_search)
1819-
.collect();
1808+
let params = get_download_params(&args, args.save_only, /*persist_blobs=*/ true);
18201809
let run = DownloadRun {
18211810
api_client: &api_client,
18221811
lock_timeout: args.common.lock_timeout,
@@ -2285,7 +2274,7 @@ fn get_download_params(args: &GetArgs, save_only: bool, persist_blobs: bool) ->
22852274
ecosystems: args.common.ecosystems.clone(),
22862275
persist_blobs,
22872276
patch_server_url: args.common.patch_server_url.clone(),
2288-
opt_in_notes_shown: Vec::new(),
2277+
offline: args.common.offline,
22892278
}
22902279
}
22912280

@@ -2319,7 +2308,7 @@ async fn run_get_hosted(
23192308
// default-off VexEmbedArgs — deliberately NOT env-bound here, so an
23202309
// ambient SOCKET_VEX only affects commands that declare the flag.
23212310
let vex = crate::commands::vex::VexEmbedArgs::default();
2322-
super::scan::boxed_run_redirect_selected(
2311+
let code = super::scan::boxed_run_redirect_selected(
23232312
&args.common,
23242313
&vex,
23252314
/*prune_requested=*/ false,
@@ -2331,7 +2320,15 @@ async fn run_get_hosted(
23312320
None,
23322321
"get",
23332322
)
2334-
.await
2323+
.await;
2324+
if !args.common.json && !args.common.silent {
2325+
if let Some(notes) =
2326+
crate::ui::opt_in_note_block(selected.iter().map(crate::ui::OptInEntry::from_search))
2327+
{
2328+
println!("\n{notes}");
2329+
}
2330+
}
2331+
code
23352332
}
23362333

23372334
/// `get … --mode vendored`, both identifier paths: scan's vendored posture
@@ -2462,7 +2459,7 @@ async fn run_get_vendored(
24622459
// records fetched above (moved in — nothing here needs them afterwards)
24632460
// and over this run's client, then the run's telemetry. A per-patch
24642461
// download failure does not skip it (scan parity).
2465-
match super::scan::boxed_vendor_step(super::scan::VendorStep {
2462+
let code = match super::scan::boxed_vendor_step(super::scan::VendorStep {
24662463
common: &args.common,
24672464
records,
24682465
client: api_client.clone(),
@@ -2503,7 +2500,13 @@ async fn run_get_vendored(
25032500
}
25042501
1
25052502
}
2503+
};
2504+
if !args.common.json && !args.common.silent {
2505+
if let Some(notes) = env.opt_in_note_block() {
2506+
println!("\n{notes}");
2507+
}
25062508
}
2509+
code
25072510
}
25082511

25092512
#[cfg(test)]
@@ -4090,7 +4093,7 @@ mod tests {
40904093
ecosystems: None,
40914094
persist_blobs: false,
40924095
patch_server_url: None,
4093-
opt_in_notes_shown: Vec::new(),
4096+
offline: false,
40944097
}
40954098
}
40964099

@@ -4791,7 +4794,7 @@ mod tests {
47914794
// The vendor-detached posture this fn exists for.
47924795
persist_blobs: false,
47934796
patch_server_url: None,
4794-
opt_in_notes_shown: Vec::new(),
4797+
offline: false,
47954798
}
47964799
}
47974800

@@ -5540,6 +5543,8 @@ mod tests {
55405543
"description": "embedded",
55415544
"license": "MIT",
55425545
"tier": "free",
5546+
"requiresOptIn": true,
5547+
"optInNote": "Keep the embedded instruction offline",
55435548
}
55445549
}}
55455550
}))
@@ -5551,6 +5556,7 @@ mod tests {
55515556
let mut params = detached_params(tmp.path());
55525557
params.json = false;
55535558
params.silent = false;
5559+
params.offline = true;
55545560
let (code, json, records) = download_patch_records(&selected, &params, &server.uri()).await;
55555561

55565562
assert_eq!(code, 0, "json={json}");
@@ -5562,6 +5568,11 @@ mod tests {
55625568
Some(uuid),
55635569
"the ledger's embedded record must be reused"
55645570
);
5571+
assert_eq!(records[purl].requires_opt_in, Some(true));
5572+
assert_eq!(
5573+
records[purl].opt_in_note.as_deref(),
5574+
Some("Keep the embedded instruction offline")
5575+
);
55655576
assert!(
55665577
server
55675578
.received_requests()
@@ -6159,16 +6170,22 @@ mod tests {
61596170
)
61606171
.mount(&server)
61616172
.await;
6162-
// `d` is held in memory and `e` is reused from the ledger: never
6163-
// requested.
6164-
for u in [&d, &e] {
6173+
// `d` is held in memory. `e` is reused from the ledger, but its view
6174+
// still supplies current activation metadata without a download event.
6175+
for u in [&d] {
61656176
Mock::given(method("GET"))
61666177
.and(wm_path(format!("/v0/orgs/test-org/patches/view/{u}")))
61676178
.respond_with(ResponseTemplate::new(500))
61686179
.expect(0)
61696180
.mount(&server)
61706181
.await;
61716182
}
6183+
mount(
6184+
&e,
6185+
ResponseTemplate::new(200).set_body_json(view(&e, &purl("e"))),
6186+
)
6187+
.mount(&server)
6188+
.await;
61726189
mount(
61736190
&f,
61746191
ResponseTemplate::new(200).set_body_json(view(&f, &purl("f"))),
@@ -6252,8 +6269,8 @@ mod tests {
62526269
row("b", "failed", "could not fetch details"),
62536270
row("c", "failed", "API request failed with status 500: boom"),
62546271
row("d", "downloaded", ""),
6255-
// e: the ledger already holds it at this uuid — reused, no
6256-
// fetch and no event (the vendor engine reports it).
6272+
// e: its view refreshes activation metadata; the vendor engine
6273+
// reports the reuse, so the download phase still has no event.
62576274
row("f", "downloaded", ""),
62586275
],
62596276
"json={json}"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ fn download_params(args: &ScanArgs, save_only: bool, json: bool, silent: bool) -
990990
ecosystems: args.common.ecosystems.clone(),
991991
persist_blobs: args.mode != Some(ScanMode::Vendored),
992992
patch_server_url: args.common.patch_server_url.clone(),
993-
opt_in_notes_shown: Vec::new(),
993+
offline: args.common.offline,
994994
}
995995
}
996996

‎crates/socket-patch-cli/src/json_envelope.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,26 @@ impl Envelope {
289289
event.details = Some(details);
290290
}
291291

292+
pub(crate) fn opt_in_note_block(&self) -> Option<String> {
293+
crate::ui::opt_in_note_block(self.opt_in_by_patch.iter().map(|((purl, uuid), metadata)| {
294+
crate::ui::OptInEntry {
295+
purl,
296+
uuid,
297+
requires_opt_in: metadata.requires_opt_in,
298+
opt_in_note: metadata.opt_in_note.as_deref(),
299+
}
300+
}))
301+
}
302+
303+
pub(crate) fn shown_opt_ins(&self, uuid: &str) -> Option<crate::ui::ShownOptIn> {
304+
let metadata = self.opt_in_by_uuid.get(uuid)?;
305+
Some(crate::ui::ShownOptIn::new(
306+
uuid,
307+
metadata.requires_opt_in,
308+
metadata.opt_in_note.as_deref(),
309+
))
310+
}
311+
292312
pub(crate) fn add_registered_opt_in_to_events(&mut self) {
293313
let mut events = std::mem::take(&mut self.events);
294314
for event in &mut events {

‎crates/socket-patch-cli/src/ui/mod.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub(crate) use status::StatusLine;
2626
pub use text::ShownOptIn;
2727
pub(crate) use text::{
2828
agent_next_steps, manifest_error_message, next_steps, next_steps_installed, opt_in_note_block,
29-
plural, sentence_case, shell_word, short_uuid, sweep_failure, terminal_purl, terminal_text,
30-
truncate, OptInEntry,
29+
plural, sentence_case, shell_word, short_uuid, sweep_failure, terminal_text, truncate,
30+
OptInEntry,
3131
};
3232

3333
/// [`text::no_patches_message`] for this run: the manifest path is named

0 commit comments

Comments
 (0)