@@ -10,18 +10,22 @@ use layer2_core::sandbox::{
1010 Agent , AgentLogKind , AgentStatus , AgentWorkReceipt , AgentWorkStep , FirstMessageDelivery ,
1111 Sandbox , SandboxEventKind , SandboxWorkStep , Worktree , WorktreeStatus ,
1212} ;
13- use layer3_adapter:: AgentOpenRequest ;
13+ use layer3_adapter:: { reopen_diagnostics :: Trace , AgentOpenRequest } ;
1414use tokio:: sync:: mpsc;
1515use tokio:: time:: Instant ;
1616
1717use super :: super :: support:: { runtime, WorkerFailure } ;
1818use super :: super :: SandboxWorker ;
19- use super :: checkpoint:: { pinned_checkpoint, require_quiet_agent } ;
19+ use super :: checkpoint:: pinned_checkpoint;
2020use super :: reopen_refusal:: within_deadline;
2121use super :: support:: { lifecycle_lease, model_error} ;
2222use crate :: sandbox:: live_agents:: { LiveAgentAttachment , LiveAgentRegistryError } ;
2323use crate :: sandbox:: runtime_mapping:: { runtime_capabilities, runtime_options} ;
2424
25+ #[ path = "conversations_validation.rs" ]
26+ mod validation;
27+ use validation:: validate_reopen_set;
28+
2529const UPDATE_BUFFER : usize = 256 ;
2630const REOPEN_LIMIT : Duration = Duration :: from_secs ( layer3_adapter:: DEFAULT_TOTAL_TIMEOUT_SECONDS ) ;
2731
@@ -79,6 +83,7 @@ impl SandboxWorker {
7983
8084 let mut attachments = Vec :: new ( ) ;
8185 let mut receipts = BTreeMap :: new ( ) ;
86+ let mut reopen_traces = Vec :: new ( ) ;
8287 // Earlier restore phases have their own budgets, and an empty set has
8388 // no conversation work to time out.
8489 let deadline = if live. is_empty ( ) {
@@ -97,9 +102,12 @@ impl SandboxWorker {
97102 let prove_refusal = refusal. is_some ( ) ;
98103 if let Some ( relay) = self . reopen_relay . get ( ) {
99104 let relay = relay. upgrade ( ) . ok_or ( SandboxStoreError :: Conflict ) ?;
105+ let trace = Trace :: new ( uuid:: Uuid :: new_v4 ( ) , "publisher" ) ;
100106 match within_deadline (
101107 deadline,
102- relay. reopen_agent ( & sandbox, & agent. id , prove_refusal) ,
108+ trace
109+ . clone ( )
110+ . run ( relay. reopen_agent ( & sandbox, & agent. id , prove_refusal) ) ,
103111 refusal,
104112 true ,
105113 )
@@ -110,6 +118,7 @@ impl SandboxWorker {
110118 // timestamp, which can differ from the remote clock.
111119 receipt. completed_at = rfc3339_at ( now) ;
112120 receipts. insert ( agent. id . clone ( ) , receipt) ;
121+ reopen_traces. push ( trace) ;
113122 }
114123 Err ( failure) => {
115124 tracing:: warn!( sandbox_id = %sandbox. id, agent_id = %agent. id,
@@ -339,6 +348,10 @@ impl SandboxWorker {
339348 events. push ( SandboxEventKind :: WorkStepCompleted {
340349 step : SandboxWorkStep :: RunLifecycleOperation ,
341350 } ) ;
351+ let commit_timings: Vec < _ > = reopen_traces
352+ . iter ( )
353+ . map ( |trace| trace. phase ( "parent_ready_commit" ) )
354+ . collect ( ) ;
342355 let committed = self
343356 . commit_lifecycle_phase (
344357 & sandbox,
@@ -349,6 +362,13 @@ impl SandboxWorker {
349362 events,
350363 )
351364 . await ;
365+ for phase in commit_timings {
366+ phase. finish ( if committed. is_ok ( ) {
367+ "ok"
368+ } else {
369+ "commit_error"
370+ } ) ;
371+ }
352372 let durable = match committed {
353373 Ok ( ( ) ) => true ,
354374 Err ( error) => match self . lifecycle_state_matches ( & next) . await {
@@ -442,46 +462,6 @@ impl SandboxWorker {
442462 }
443463}
444464
445- fn validate_reopen_set < ' a > (
446- sandbox : & Sandbox ,
447- agents : & ' a [ Agent ] ,
448- checkpoint : Option < & layer2_core:: sandbox:: SandboxCheckpointRef > ,
449- ) -> Result < Vec < & ' a Agent > , WorkerFailure > {
450- let by_id = checkpoint
451- . into_iter ( )
452- . flat_map ( |checkpoint| & checkpoint. conversations )
453- . map ( |conversation| {
454- (
455- & conversation. agent_id ,
456- conversation. conversation_id . as_str ( ) ,
457- )
458- } )
459- . collect :: < BTreeMap < _ , _ > > ( ) ;
460- let live = agents
461- . iter ( )
462- . filter ( |agent| sandbox. restores_agent ( agent) )
463- . collect :: < Vec < _ > > ( ) ;
464- let matches = live. iter ( ) . all ( |agent| {
465- agent. status == AgentStatus :: Paused
466- && require_quiet_agent ( agent) . is_ok ( )
467- && agent
468- . conversation
469- . conversation_id
470- . as_deref ( )
471- . is_some_and ( |current| {
472- sandbox. retained_home_recovery ( ) . is_some ( )
473- || by_id. get ( & agent. id ) . copied ( ) == Some ( current)
474- } )
475- } ) ;
476- if matches {
477- Ok ( live)
478- } else {
479- Err ( WorkerFailure :: terminal_security (
480- "restored live agents do not match the pinned checkpoint conversations" ,
481- ) )
482- }
483- }
484-
485465fn rollback_attachments ( worker : & SandboxWorker , attachments : & [ LiveAgentAttachment ] ) {
486466 for attachment in attachments {
487467 worker. dependencies . registry . rollback ( attachment) ;
0 commit comments