adk-agent 2.1.0

Agent implementations for Rust Agent Development Kit (ADK-Rust, LLM, Custom, Workflow agents)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{SystemTime, UNIX_EPOCH};

use adk_core::{Event, Part, Result};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use super::{
    RelationshipKind, TeamBudget, TeamError, TeamLifecycleContext, TeamLifecycleDecision,
    TeamLifecycleHook, TeamLifecycleOutcome, TeamRuntimeError, TeamTerminationPolicy,
    lifecycle::TeamLifecycleManager,
};

/// Session state key containing the latest serializable team execution receipt.
pub const TEAM_EXECUTION_STATE_KEY: &str = "__adk_team_execution_v1";
/// Event metadata key containing the stable root team invocation identifier.
pub const TEAM_ROOT_INVOCATION_KEY: &str = "adk.team.root_invocation_id";
/// Event metadata key containing the causal relationship execution identifier.
pub const TEAM_EDGE_ID_KEY: &str = "adk.team.edge_id";

/// Aggregate resource usage recorded for a team invocation.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct TeamExecutionUsage {
    /// Events emitted by all members and nested delegates.
    pub events: u64,
    /// Model responses carrying usage metadata.
    pub model_requests: u64,
    /// Tool calls requested by model events.
    pub tool_calls: u64,
    /// Total reported input and output tokens.
    pub tokens: u64,
    /// Estimated cost in millionths of a US dollar.
    pub cost_microusd: u64,
    /// Delegation relationship executions.
    pub delegations: u64,
    /// Handoff relationship executions.
    pub handoffs: u64,
}

/// Lifecycle state of a team or relationship execution.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum TeamExecutionStatus {
    /// Execution is active.
    Running,
    /// Execution reached a normal terminal response.
    Completed,
    /// A configured clean termination condition matched.
    Terminated,
    /// A member or relationship failed.
    Failed,
    /// A hard team budget was exceeded.
    BudgetExceeded,
}

/// One causal relationship execution in a team invocation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct TeamEdgeExecution {
    /// Unique execution identifier.
    pub id: String,
    /// Parent relationship execution for nested delegation.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<String>,
    /// Relationship source member.
    pub from: String,
    /// Relationship target member.
    pub to: String,
    /// Relationship control-flow semantics.
    pub kind: RelationshipKind,
    /// One-based attempt number.
    pub attempt: u32,
    /// Unix timestamp in milliseconds.
    pub started_at_ms: u64,
    /// Unix timestamp in milliseconds when terminal.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub finished_at_ms: Option<u64>,
    /// Terminal or active state.
    pub status: TeamExecutionStatus,
    /// Failure message when present.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

/// Serializable execution receipt for one root team invocation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct TeamExecutionSnapshot {
    /// Team root name.
    pub team: String,
    /// Stable root invocation identifier.
    pub invocation_id: String,
    /// Frozen member-to-binding roster used for this execution.
    pub roster: Vec<ResolvedTeamMember>,
    /// Unix timestamp in milliseconds.
    pub started_at_ms: u64,
    /// Most recent update timestamp in milliseconds.
    pub updated_at_ms: u64,
    /// Current execution state.
    pub status: TeamExecutionStatus,
    /// Aggregate resource usage.
    pub usage: TeamExecutionUsage,
    /// Causal relationship executions in start order.
    pub edges: Vec<TeamEdgeExecution>,
    /// Reason for termination or failure.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}

/// Immutable member binding captured when a team is compiled or discovered.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ResolvedTeamMember {
    /// Portable member name in [`super::TeamSpec`].
    pub member: String,
    /// Concrete registry or agent binding identifier.
    pub binding: String,
    /// Capabilities frozen at resolution time.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub capabilities: Vec<String>,
    /// Frozen provider or semantic version.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    /// Frozen immutable content/configuration digest.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub digest: Option<String>,
    /// Frozen trust labels used during resolution.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub trust_labels: Vec<String>,
}

#[derive(Debug)]
pub(crate) enum EventDisposition {
    Continue,
    Terminate,
}

pub(crate) struct TeamEdgeStart<'a> {
    pub(crate) execution_id: Option<String>,
    pub(crate) parent_id: Option<String>,
    pub(crate) from: &'a str,
    pub(crate) to: &'a str,
    pub(crate) kind: RelationshipKind,
    pub(crate) attempt: u32,
}

pub(crate) struct TeamRuntimeRegistry {
    team: String,
    roster: Vec<ResolvedTeamMember>,
    budget: TeamBudget,
    termination: TeamTerminationPolicy,
    lifecycle: TeamLifecycleManager,
    invocations: RwLock<HashMap<String, Arc<Mutex<TeamExecutionSnapshot>>>>,
}

impl TeamRuntimeRegistry {
    pub(crate) fn team_name(&self) -> &str {
        &self.team
    }

    pub(crate) fn new(
        team: String,
        roster: Vec<ResolvedTeamMember>,
        budget: TeamBudget,
        termination: TeamTerminationPolicy,
        hooks: Vec<Arc<dyn TeamLifecycleHook>>,
    ) -> Self {
        Self {
            team,
            roster,
            budget,
            termination,
            lifecycle: TeamLifecycleManager::new(hooks),
            invocations: RwLock::new(HashMap::new()),
        }
    }

    pub(crate) async fn before_lifecycle(
        &self,
        context: &TeamLifecycleContext,
    ) -> Result<TeamLifecycleDecision> {
        self.lifecycle.before(context).await
    }

    pub(crate) async fn after_lifecycle(
        &self,
        context: &TeamLifecycleContext,
        outcome: &TeamLifecycleOutcome,
    ) -> Result<()> {
        self.lifecycle.after(context, outcome).await
    }

    pub(crate) fn snapshot(&self, invocation_id: &str) -> Option<TeamExecutionSnapshot> {
        let ledger = self
            .invocations
            .read()
            .unwrap_or_else(|error| error.into_inner())
            .get(invocation_id)
            .cloned()?;
        let snapshot = ledger.lock().unwrap_or_else(|error| error.into_inner()).clone();
        Some(snapshot)
    }

    pub(crate) fn snapshots(&self) -> Vec<TeamExecutionSnapshot> {
        self.invocations
            .read()
            .unwrap_or_else(|error| error.into_inner())
            .values()
            .map(|ledger| ledger.lock().unwrap_or_else(|error| error.into_inner()).clone())
            .collect()
    }

    pub(crate) fn restore(
        &self,
        snapshot: TeamExecutionSnapshot,
    ) -> std::result::Result<(), TeamError> {
        if snapshot.team != self.team {
            return Err(TeamError::IncompatibleExecutionSnapshot(format!(
                "snapshot belongs to team '{}', expected '{}'",
                snapshot.team, self.team
            )));
        }
        if snapshot.roster != self.roster {
            return Err(TeamError::IncompatibleExecutionSnapshot(
                "the frozen member roster does not match".to_string(),
            ));
        }
        if snapshot.invocation_id.trim().is_empty() {
            return Err(TeamError::IncompatibleExecutionSnapshot(
                "invocationId must not be empty".to_string(),
            ));
        }
        self.invocations
            .write()
            .unwrap_or_else(|error| error.into_inner())
            .insert(snapshot.invocation_id.clone(), Arc::new(Mutex::new(snapshot)));
        Ok(())
    }

    pub(crate) fn resume_handoff_target(&self, invocation_id: &str) -> Result<Option<String>> {
        let Some(snapshot) = self.snapshot(invocation_id) else {
            return Ok(None);
        };
        let Some(active) =
            snapshot.edges.iter().rev().find(|edge| edge.status == TeamExecutionStatus::Running)
        else {
            return Ok(None);
        };
        match active.kind {
            RelationshipKind::Handoff => Ok(Some(active.to.clone())),
            RelationshipKind::Delegate => Err(TeamRuntimeError::UnsafeResume(format!(
                "cannot replay unresolved delegation '{}' from '{}' to '{}'; inspect CompiledTeam::resume_plan and use a checkpoint-aware durable host",
                active.id, active.from, active.to
            ))
            .into()),
        }
    }

    fn ledger(&self, invocation_id: &str) -> Arc<Mutex<TeamExecutionSnapshot>> {
        if let Some(ledger) = self
            .invocations
            .read()
            .unwrap_or_else(|error| error.into_inner())
            .get(invocation_id)
            .cloned()
        {
            return ledger;
        }
        let now = now_ms();
        let ledger = Arc::new(Mutex::new(TeamExecutionSnapshot {
            team: self.team.clone(),
            invocation_id: invocation_id.to_string(),
            roster: self.roster.clone(),
            started_at_ms: now,
            updated_at_ms: now,
            status: TeamExecutionStatus::Running,
            usage: TeamExecutionUsage::default(),
            edges: Vec::new(),
            reason: None,
        }));
        self.invocations
            .write()
            .unwrap_or_else(|error| error.into_inner())
            .entry(invocation_id.to_string())
            .or_insert_with(|| ledger.clone())
            .clone()
    }

    pub(crate) fn check_budget(&self, invocation_id: &str) -> Result<()> {
        let ledger = self.ledger(invocation_id);
        let mut snapshot = ledger.lock().unwrap_or_else(|error| error.into_inner());
        if let Some(reason) = self.budget_violation(&snapshot) {
            snapshot.status = TeamExecutionStatus::BudgetExceeded;
            snapshot.reason = Some(reason.clone());
            snapshot.updated_at_ms = now_ms();
            return Err(TeamRuntimeError::BudgetExceeded(reason).into());
        }
        Ok(())
    }

    pub(crate) fn start_edge(
        &self,
        invocation_id: &str,
        start: TeamEdgeStart<'_>,
    ) -> Result<String> {
        self.check_budget(invocation_id)?;
        let ledger = self.ledger(invocation_id);
        let mut snapshot = ledger.lock().unwrap_or_else(|error| error.into_inner());
        snapshot.status = TeamExecutionStatus::Running;
        snapshot.reason = None;
        match start.kind {
            RelationshipKind::Delegate => snapshot.usage.delegations += 1,
            RelationshipKind::Handoff => snapshot.usage.handoffs += 1,
        }
        let id = start.execution_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
        snapshot.edges.push(TeamEdgeExecution {
            id: id.clone(),
            parent_id: start.parent_id,
            from: start.from.to_string(),
            to: start.to.to_string(),
            kind: start.kind,
            attempt: start.attempt,
            started_at_ms: now_ms(),
            finished_at_ms: None,
            status: TeamExecutionStatus::Running,
            error: None,
        });
        snapshot.updated_at_ms = now_ms();
        if let Some(reason) = self.budget_violation(&snapshot) {
            snapshot.status = TeamExecutionStatus::BudgetExceeded;
            snapshot.reason = Some(reason.clone());
            return Err(TeamRuntimeError::BudgetExceeded(reason).into());
        }
        Ok(id)
    }

    pub(crate) fn finish_edge(&self, invocation_id: &str, id: &str, error: Option<String>) {
        let ledger = self.ledger(invocation_id);
        let mut snapshot = ledger.lock().unwrap_or_else(|failure| failure.into_inner());
        if let Some(edge) = snapshot.edges.iter_mut().find(|edge| edge.id == id) {
            edge.finished_at_ms = Some(now_ms());
            edge.status = if error.is_some() {
                TeamExecutionStatus::Failed
            } else {
                TeamExecutionStatus::Completed
            };
            edge.error = error;
        }
        snapshot.updated_at_ms = now_ms();
    }

    pub(crate) fn record_event(
        &self,
        invocation_id: &str,
        edge_id: Option<&str>,
        event: &mut Event,
    ) -> Result<EventDisposition> {
        let ledger = self.ledger(invocation_id);
        let mut snapshot = ledger.lock().unwrap_or_else(|error| error.into_inner());
        let already_recorded = event
            .provider_metadata
            .get(TEAM_ROOT_INVOCATION_KEY)
            .is_some_and(|root| root == invocation_id);
        if !already_recorded {
            snapshot.usage.events += 1;
            snapshot.usage.tool_calls += event.tool_calls().len() as u64;
            if let Some(usage) = &event.llm_response.usage_metadata {
                snapshot.usage.model_requests += 1;
                snapshot.usage.tokens += u64::try_from(usage.total_token_count.max(0)).unwrap_or(0);
                if let Some(cost) = usage.cost
                    && cost.is_finite()
                    && cost > 0.0
                {
                    snapshot.usage.cost_microusd = snapshot
                        .usage
                        .cost_microusd
                        .saturating_add((cost * 1_000_000.0).round() as u64);
                }
            }
        }
        snapshot.updated_at_ms = now_ms();
        event
            .provider_metadata
            .insert(TEAM_ROOT_INVOCATION_KEY.to_string(), invocation_id.to_string());
        if let Some(edge_id) = edge_id {
            event.provider_metadata.insert(TEAM_EDGE_ID_KEY.to_string(), edge_id.to_string());
        }

        if let Some(reason) = self.budget_violation(&snapshot) {
            snapshot.status = TeamExecutionStatus::BudgetExceeded;
            snapshot.reason = Some(reason.clone());
            persist_snapshot_if_absent(event, &snapshot);
            return Err(TeamRuntimeError::BudgetExceeded(reason).into());
        }

        let termination = self.termination_reason(event);
        if let Some(reason) = &termination {
            snapshot.status = TeamExecutionStatus::Terminated;
            snapshot.reason = Some(reason.clone());
        } else if event.is_final_response()
            && edge_id.is_none_or(|id| {
                snapshot
                    .edges
                    .iter()
                    .find(|edge| edge.id == id)
                    .is_none_or(|edge| edge.kind != RelationshipKind::Delegate)
            })
        {
            snapshot.status = TeamExecutionStatus::Completed;
        }
        persist_snapshot_if_absent(event, &snapshot);
        Ok(termination.map_or(EventDisposition::Continue, |_| EventDisposition::Terminate))
    }

    pub(crate) fn fail(&self, invocation_id: &str, reason: String) {
        let ledger = self.ledger(invocation_id);
        let mut snapshot = ledger.lock().unwrap_or_else(|error| error.into_inner());
        snapshot.status = TeamExecutionStatus::Failed;
        snapshot.reason = Some(reason);
        snapshot.updated_at_ms = now_ms();
    }

    fn termination_reason(&self, event: &Event) -> Option<String> {
        if self.termination.stop_on_escalation && event.actions.escalate {
            return Some("team terminated on escalation".to_string());
        }
        if event.is_final_response()
            && self.termination.final_authors.iter().any(|author| author == &event.author)
        {
            return Some(format!("team terminated after final response from '{}'", event.author));
        }
        for marker in &self.termination.text_markers {
            let matched = event
                .content()
                .into_iter()
                .flat_map(|content| &content.parts)
                .any(|part| matches!(part, Part::Text { text } if text.contains(marker)));
            if matched {
                return Some(format!("team termination marker matched: {marker}"));
            }
        }
        None
    }

    fn budget_violation(&self, snapshot: &TeamExecutionSnapshot) -> Option<String> {
        let usage = &snapshot.usage;
        let limits = [
            ("events", usage.events, self.budget.max_events),
            ("model requests", usage.model_requests, self.budget.max_model_requests),
            ("tool calls", usage.tool_calls, self.budget.max_tool_calls),
            ("tokens", usage.tokens, self.budget.max_tokens),
            ("costMicrousd", usage.cost_microusd, self.budget.max_cost_microusd),
            ("delegations", usage.delegations, self.budget.max_delegations),
            ("handoffs", usage.handoffs, self.budget.max_handoffs),
        ];
        if let Some((name, used, limit)) =
            limits.into_iter().find(|(_, used, limit)| limit.is_some_and(|max| *used > max))
        {
            return Some(format!(
                "team budget exceeded for {name}: used {used}, maximum {}",
                limit.unwrap_or_default()
            ));
        }
        if let Some(max_wall_time_ms) = self.budget.max_wall_time_ms {
            let elapsed = now_ms().saturating_sub(snapshot.started_at_ms);
            if elapsed > max_wall_time_ms {
                return Some(format!(
                    "team wall-time budget exceeded: elapsed {elapsed}ms, maximum {max_wall_time_ms}ms"
                ));
            }
        }
        None
    }
}

fn persist_snapshot(event: &mut Event, snapshot: &TeamExecutionSnapshot) {
    if let Ok(value) = serde_json::to_value(snapshot) {
        event.actions.state_delta.insert(TEAM_EXECUTION_STATE_KEY.to_string(), value);
    }
}

fn persist_snapshot_if_absent(event: &mut Event, snapshot: &TeamExecutionSnapshot) {
    if !event.actions.state_delta.contains_key(TEAM_EXECUTION_STATE_KEY) {
        persist_snapshot(event, snapshot);
    }
}

fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis()
        .try_into()
        .unwrap_or(u64::MAX)
}

#[cfg(test)]
mod tests {
    use super::*;
    use adk_core::Content;

    fn registry(budget: TeamBudget, termination: TeamTerminationPolicy) -> TeamRuntimeRegistry {
        TeamRuntimeRegistry::new(
            "portable_team".to_string(),
            vec![ResolvedTeamMember {
                member: "root".to_string(),
                binding: "root-v1".to_string(),
                capabilities: vec!["route".to_string()],
                version: None,
                digest: None,
                trust_labels: Vec::new(),
            }],
            budget,
            termination,
            Vec::new(),
        )
    }

    #[test]
    fn enforces_aggregate_event_budget_and_persists_receipt() {
        let runtime = registry(
            TeamBudget { max_events: Some(1), ..TeamBudget::default() },
            TeamTerminationPolicy::default(),
        );
        let mut first = Event::new("invocation");
        assert!(matches!(
            runtime.record_event("invocation", None, &mut first),
            Ok(EventDisposition::Continue)
        ));
        assert!(first.actions.state_delta.contains_key(TEAM_EXECUTION_STATE_KEY));

        let mut second = Event::new("invocation");
        let error = runtime.record_event("invocation", None, &mut second).unwrap_err();
        assert!(error.to_string().contains("events"));
        assert_eq!(error.code, "agent.team.budget_exceeded");
        assert_eq!(
            runtime.snapshot("invocation").unwrap().status,
            TeamExecutionStatus::BudgetExceeded
        );
    }

    #[test]
    fn records_causal_edges_and_terminates_on_marker() {
        let runtime = registry(
            TeamBudget::default(),
            TeamTerminationPolicy {
                text_markers: vec!["APPROVED".to_string()],
                ..TeamTerminationPolicy::default()
            },
        );
        let parent = runtime
            .start_edge(
                "invocation",
                TeamEdgeStart {
                    execution_id: Some("edge-1".to_string()),
                    parent_id: None,
                    from: "root",
                    to: "a",
                    kind: RelationshipKind::Delegate,
                    attempt: 1,
                },
            )
            .unwrap();
        let child = runtime
            .start_edge(
                "invocation",
                TeamEdgeStart {
                    execution_id: Some("edge-2".to_string()),
                    parent_id: Some(parent.clone()),
                    from: "a",
                    to: "b",
                    kind: RelationshipKind::Handoff,
                    attempt: 1,
                },
            )
            .unwrap();
        runtime.finish_edge("invocation", &child, None);
        runtime.finish_edge("invocation", &parent, None);
        let mut event = Event::new("invocation");
        event.author = "b".to_string();
        event.llm_response.content = Some(Content::new("model").with_text("APPROVED"));
        assert!(matches!(
            runtime.record_event("invocation", Some(&child), &mut event),
            Ok(EventDisposition::Terminate)
        ));
        let snapshot = runtime.snapshot("invocation").unwrap();
        assert_eq!(snapshot.status, TeamExecutionStatus::Terminated);
        assert_eq!(snapshot.edges[1].parent_id.as_deref(), Some("edge-1"));
        assert_eq!(event.provider_metadata.get(TEAM_EDGE_ID_KEY), Some(&child));
    }

    #[test]
    fn restores_only_matching_frozen_rosters() {
        let source = registry(TeamBudget::default(), TeamTerminationPolicy::default());
        source.check_budget("resume-me").unwrap();
        let snapshot = source.snapshot("resume-me").unwrap();

        let restored = registry(TeamBudget::default(), TeamTerminationPolicy::default());
        restored.restore(snapshot.clone()).unwrap();
        assert_eq!(restored.snapshot("resume-me"), Some(snapshot.clone()));

        let other = TeamRuntimeRegistry::new(
            "portable_team".to_string(),
            vec![ResolvedTeamMember {
                member: "root".to_string(),
                binding: "root-v2".to_string(),
                capabilities: vec!["route".to_string()],
                version: None,
                digest: None,
                trust_labels: Vec::new(),
            }],
            TeamBudget::default(),
            TeamTerminationPolicy::default(),
            Vec::new(),
        );
        assert!(matches!(
            other.restore(snapshot),
            Err(TeamError::IncompatibleExecutionSnapshot(_))
        ));
    }

    #[test]
    fn resumes_handoffs_but_refuses_unsafe_delegate_replay() {
        let handoff = registry(TeamBudget::default(), TeamTerminationPolicy::default());
        handoff
            .start_edge(
                "handoff-run",
                TeamEdgeStart {
                    execution_id: Some("handoff-edge".to_string()),
                    parent_id: None,
                    from: "root",
                    to: "specialist",
                    kind: RelationshipKind::Handoff,
                    attempt: 1,
                },
            )
            .unwrap();
        assert_eq!(
            handoff.resume_handoff_target("handoff-run").unwrap().as_deref(),
            Some("specialist")
        );

        let delegate = registry(TeamBudget::default(), TeamTerminationPolicy::default());
        delegate
            .start_edge(
                "delegate-run",
                TeamEdgeStart {
                    execution_id: Some("delegate-edge".to_string()),
                    parent_id: None,
                    from: "root",
                    to: "worker",
                    kind: RelationshipKind::Delegate,
                    attempt: 1,
                },
            )
            .unwrap();
        let error = delegate.resume_handoff_target("delegate-run").unwrap_err();
        assert!(error.to_string().contains("cannot replay unresolved delegation"));
        assert_eq!(error.code, "agent.team.resume_unsafe");
    }
}