adk-agent 3.0.0

Agent implementations for Rust Agent Development Kit (ADK-Rust): LLM, custom, workflow, and team 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
//! Capability conformance for the workflow context wrappers.
//!
//! Every wrapper here re-implements [`InvocationContext`] and delegates to an
//! inner context. Most of the trait's capability methods have permissive
//! defaults — `is_cancelled()` returns `false`, `get_secret()` returns `None`,
//! `shared_state()`/`user_scopes()`/`request_metadata()` return empty — so a
//! wrapper that forgets one still compiles and silently drops it. That is how
//! cancellation, secrets, and shared state came to be lost depending on which
//! workflow agent a sub-agent happened to run under.
//!
//! These tests push a sentinel context with a non-default value for every
//! capability through each wrapper and assert all of them survive. A new wrapper,
//! or a new capability method, should be added here.

use super::branch_context::BranchContext;
use super::shared_state_context::SharedStateContext;
use adk_core::{
    Agent, CallbackContext, Content, Event, InvocationContext, Memory, Part, ReadonlyContext,
    Result, RunConfig, Session, SharedState, ToolOutcome,
};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;

// ── Sentinel context: every capability set to a non-default value ──────

const SENTINEL_SECRET_NAME: &str = "sentinel-secret";
const SENTINEL_SECRET_VALUE: &str = "sentinel-value";
const SENTINEL_SCOPE: &str = "sentinel:scope";
const SENTINEL_AGENT_ID: &str = "spiffe://sentinel.test/agent/sentinel";
const SENTINEL_METADATA_KEY: &str = "sentinel-metadata";
const SENTINEL_STATE_KEY: &str = "sentinel-shared-key";

const SENTINEL_ARTIFACT: &str = "sentinel-artifact";
const SENTINEL_MEMORY_AUTHOR: &str = "sentinel-memory-author";

struct SentinelArtifacts;

#[async_trait]
impl adk_core::Artifacts for SentinelArtifacts {
    async fn save(&self, _name: &str, _data: &Part) -> Result<i64> {
        Ok(1)
    }
    async fn load(&self, _name: &str) -> Result<Part> {
        Ok(Part::Text { text: "sentinel".to_string() })
    }
    async fn list(&self) -> Result<Vec<String>> {
        Ok(vec![SENTINEL_ARTIFACT.to_string()])
    }
}

struct SentinelMemory;

#[async_trait]
impl Memory for SentinelMemory {
    async fn search(&self, _query: &str) -> Result<Vec<adk_core::MemoryEntry>> {
        Ok(vec![adk_core::MemoryEntry {
            content: Content {
                role: "model".to_string(),
                parts: vec![Part::Text { text: "sentinel".to_string() }],
            },
            author: SENTINEL_MEMORY_AUTHOR.to_string(),
        }])
    }
}

struct SentinelState;

impl adk_core::State for SentinelState {
    fn get(&self, _key: &str) -> Option<serde_json::Value> {
        None
    }
    fn set(&mut self, _key: String, _value: serde_json::Value) {}
    fn all(&self) -> HashMap<String, serde_json::Value> {
        HashMap::new()
    }
}

struct SentinelSession {
    state: SentinelState,
}

impl Session for SentinelSession {
    fn id(&self) -> &str {
        "sentinel-session"
    }
    fn app_name(&self) -> &str {
        "sentinel-app"
    }
    fn user_id(&self) -> &str {
        "sentinel-user"
    }
    fn state(&self) -> &dyn adk_core::State {
        &self.state
    }
    fn conversation_history(&self) -> Vec<Content> {
        Vec::new()
    }
}

struct SentinelContext {
    content: Content,
    config: RunConfig,
    session: SentinelSession,
    shared: Arc<SharedState>,
    inbox: adk_core::SteeringInbox,
}

impl SentinelContext {
    /// `SharedState` mutation is async, so callers seed it via [`seeded_shared_state`].
    fn with_shared(shared: Arc<SharedState>) -> Self {
        Self {
            content: Content {
                role: "user".to_string(),
                parts: vec![Part::Text { text: "sentinel".to_string() }],
            },
            config: RunConfig::default(),
            session: SentinelSession { state: SentinelState },
            shared,
            inbox: sentinel_inbox(),
        }
    }
}

/// An inbox holding one steering message, so a forwarded inbox is told from a new one.
fn sentinel_inbox() -> adk_core::SteeringInbox {
    let inbox = adk_core::SteeringInbox::new();
    let message = Content::new("user").with_text("sentinel steering");
    let _receipt = inbox
        .push(message.clone(), message, "sentinel-user", false)
        .expect("the sentinel inbox is open");
    inbox
}

/// Assert that `wrapped` forwards the sentinel's steering inbox.
fn assert_steering_forwarded(label: &str, wrapped: &Arc<dyn InvocationContext>) {
    let inbox =
        wrapped.steering_inbox().unwrap_or_else(|| panic!("{label}: steering inbox was dropped"));
    assert_eq!(
        inbox.drain().len(),
        1,
        "{label}: steering inbox was replaced rather than forwarded"
    );
}

/// A `SharedState` carrying the sentinel key.
async fn seeded_shared_state(key: &str) -> Arc<SharedState> {
    let shared = Arc::new(SharedState::new());
    shared.set_shared(key, serde_json::json!(true)).await.expect("seeding shared state failed");
    shared
}

#[async_trait]
impl ReadonlyContext for SentinelContext {
    fn invocation_id(&self) -> &str {
        "sentinel-invocation"
    }
    fn agent_name(&self) -> &str {
        "sentinel-agent"
    }
    fn user_id(&self) -> &str {
        "sentinel-user"
    }
    fn app_name(&self) -> &str {
        "sentinel-app"
    }
    fn session_id(&self) -> &str {
        "sentinel-session"
    }
    fn branch(&self) -> &str {
        "sentinel-branch"
    }
    fn user_content(&self) -> &Content {
        &self.content
    }
    fn acting_context(&self) -> adk_core::ActingContext {
        sentinel_acting()
    }
    fn is_authenticated(&self) -> bool {
        true
    }
}

fn sentinel_acting() -> adk_core::ActingContext {
    adk_core::ActingContext::new(adk_core::AgentPrincipal::new(
        adk_core::AgentId::new(SENTINEL_AGENT_ID).expect("valid sentinel agent id"),
    ))
}

#[async_trait]
impl CallbackContext for SentinelContext {
    fn artifacts(&self) -> Option<Arc<dyn adk_core::Artifacts>> {
        Some(Arc::new(SentinelArtifacts))
    }
    fn tool_outcome(&self) -> Option<ToolOutcome> {
        Some(ToolOutcome {
            tool_name: "sentinel-tool".to_string(),
            tool_args: serde_json::json!({}),
            success: true,
            duration: std::time::Duration::from_millis(1),
            error_message: None,
            error: None,
            attempt: 0,
        })
    }
    fn tool_name(&self) -> Option<&str> {
        Some("sentinel-tool")
    }
    fn shared_state(&self) -> Option<Arc<SharedState>> {
        Some(self.shared.clone())
    }
}

#[async_trait]
impl InvocationContext for SentinelContext {
    fn agent(&self) -> Arc<dyn Agent> {
        unimplemented!("not exercised by wrapper conformance")
    }
    fn memory(&self) -> Option<Arc<dyn Memory>> {
        Some(Arc::new(SentinelMemory))
    }
    fn session(&self) -> &dyn Session {
        &self.session
    }
    fn run_config(&self) -> &RunConfig {
        &self.config
    }
    fn end_invocation(&self) {}
    fn ended(&self) -> bool {
        false
    }

    // Non-default values for each capability the wrappers must preserve.
    fn is_cancelled(&self) -> bool {
        true
    }
    fn user_scopes(&self) -> Vec<String> {
        vec![SENTINEL_SCOPE.to_string()]
    }
    fn request_metadata(&self) -> HashMap<String, serde_json::Value> {
        HashMap::from([(SENTINEL_METADATA_KEY.to_string(), serde_json::json!(1))])
    }
    fn steering_inbox(&self) -> Option<adk_core::SteeringInbox> {
        Some(self.inbox.clone())
    }
    async fn get_secret(&self, name: &str) -> Result<Option<String>> {
        Ok(if name == SENTINEL_SECRET_NAME {
            Some(SENTINEL_SECRET_VALUE.to_string())
        } else {
            None
        })
    }
}

// ── Shared assertion ──────────────────────────────────────────────────

/// Assert that every capability set on the sentinel survives `wrapped`.
async fn assert_capabilities_preserved(label: &str, wrapped: Arc<dyn InvocationContext>) {
    assert!(wrapped.is_cancelled(), "{label}: cancellation was dropped");

    assert_eq!(
        wrapped.user_scopes(),
        vec![SENTINEL_SCOPE.to_string()],
        "{label}: authenticated scopes were dropped"
    );
    assert_eq!(wrapped.acting_context(), sentinel_acting(), "{label}: acting context dropped");
    assert!(wrapped.is_authenticated(), "{label}: the authenticated identity was dropped");

    assert!(
        wrapped.request_metadata().contains_key(SENTINEL_METADATA_KEY),
        "{label}: request metadata was dropped"
    );

    let secret = wrapped.get_secret(SENTINEL_SECRET_NAME).await.expect("secret lookup failed");
    assert_eq!(
        secret.as_deref(),
        Some(SENTINEL_SECRET_VALUE),
        "{label}: secret access was dropped"
    );

    let shared =
        wrapped.shared_state().unwrap_or_else(|| panic!("{label}: shared state was dropped"));
    assert!(
        shared.get_shared(SENTINEL_STATE_KEY).await.is_some(),
        "{label}: shared state was replaced rather than forwarded"
    );

    let artifacts =
        wrapped.artifacts().unwrap_or_else(|| panic!("{label}: artifact service was dropped"));
    assert_eq!(
        artifacts.list().await.expect("artifact list failed"),
        vec![SENTINEL_ARTIFACT.to_string()],
        "{label}: artifact service was replaced rather than forwarded"
    );

    let memory = wrapped.memory().unwrap_or_else(|| panic!("{label}: memory service was dropped"));
    let hits = memory.search("sentinel").await.expect("memory search failed");
    assert_eq!(
        hits.first().map(|e| e.author.as_str()),
        Some(SENTINEL_MEMORY_AUTHOR),
        "{label}: memory service was replaced rather than forwarded"
    );

    // Identity must survive untouched too.
    assert_eq!(wrapped.app_name(), "sentinel-app", "{label}: app name was lost");
    assert_eq!(wrapped.user_id(), "sentinel-user", "{label}: user id was lost");
    assert_eq!(wrapped.session_id(), "sentinel-session", "{label}: session id was lost");
}

async fn sentinel() -> Arc<dyn InvocationContext> {
    Arc::new(SentinelContext::with_shared(seeded_shared_state(SENTINEL_STATE_KEY).await))
}

// ── Per-wrapper tests ─────────────────────────────────────────────────

#[tokio::test]
async fn shared_state_context_preserves_capabilities() {
    // This wrapper deliberately replaces `shared_state`, so it is checked
    // separately from the shared assertion.
    let injected = seeded_shared_state("injected").await;
    let wrapped: Arc<dyn InvocationContext> =
        Arc::new(SharedStateContext::new(sentinel().await, injected));

    assert!(wrapped.is_cancelled(), "SharedStateContext: cancellation was dropped");
    assert_eq!(wrapped.user_scopes(), vec![SENTINEL_SCOPE.to_string()]);
    assert_eq!(
        wrapped.acting_context(),
        sentinel_acting(),
        "SharedStateContext: acting context dropped"
    );
    assert!(wrapped.request_metadata().contains_key(SENTINEL_METADATA_KEY));
    assert!(
        wrapped.is_authenticated(),
        "SharedStateContext: the authenticated identity was dropped"
    );
    let secret = wrapped.get_secret(SENTINEL_SECRET_NAME).await.unwrap();
    assert_eq!(secret.as_deref(), Some(SENTINEL_SECRET_VALUE));
    let shared = wrapped.shared_state().expect("shared state missing");
    assert!(
        shared.get_shared("injected").await.is_some(),
        "SharedStateContext must expose the state it injects"
    );
    assert!(wrapped.artifacts().is_some(), "SharedStateContext: artifacts were dropped");
    assert!(wrapped.memory().is_some(), "SharedStateContext: memory was dropped");
    assert_steering_forwarded("SharedStateContext", &wrapped);
}

#[tokio::test]
async fn branch_context_preserves_capabilities() {
    let wrapped: Arc<dyn InvocationContext> =
        Arc::new(BranchContext::new(sentinel().await, "sentinel-branch.child".to_string()));
    assert_capabilities_preserved("BranchContext", wrapped.clone()).await;
    assert_eq!(wrapped.branch(), "sentinel-branch.child", "BranchContext must override branch");
    assert!(wrapped.steering_inbox().is_none(), "a parallel branch takes no steering");
}

#[tokio::test]
async fn skill_context_preserves_capabilities() {
    let overridden = Content {
        role: "user".to_string(),
        parts: vec![Part::Text { text: "overridden".to_string() }],
    };
    let wrapped =
        super::skill_context::with_user_content_override(sentinel().await, overridden.clone());
    assert_capabilities_preserved("UserContentOverrideContext", wrapped.clone()).await;
    assert_steering_forwarded("UserContentOverrideContext", &wrapped);
    assert_eq!(
        wrapped.user_content().parts.len(),
        overridden.parts.len(),
        "UserContentOverrideContext must override user content"
    );
}

#[tokio::test]
async fn loop_history_context_preserves_capabilities() {
    let wrapped = super::loop_agent::history_tracking_context_for_test(sentinel().await);
    assert_steering_forwarded("HistoryTrackingContext", &wrapped);
    assert_capabilities_preserved("HistoryTrackingContext", wrapped).await;
}

/// Wrappers compose, so a capability dropped by an inner wrapper is lost even if
/// the outer one forwards correctly. `ParallelAgent` builds exactly this stack.
#[tokio::test]
async fn composed_wrappers_preserve_capabilities() {
    let injected = seeded_shared_state(SENTINEL_STATE_KEY).await;
    let inner: Arc<dyn InvocationContext> =
        Arc::new(SharedStateContext::new(sentinel().await, injected));
    let outer: Arc<dyn InvocationContext> =
        Arc::new(BranchContext::new(inner, "sentinel-branch.parallel.a".to_string()));

    assert_capabilities_preserved("BranchContext(SharedStateContext)", outer.clone()).await;
    assert_eq!(outer.branch(), "sentinel-branch.parallel.a");
    assert!(outer.steering_inbox().is_none(), "a parallel branch takes no steering");
}

/// An `Event` carries no capabilities, but this pins the sentinel itself: if the
/// trait gains a capability method, the sentinel must set a non-default value or
/// these tests silently stop covering it.
#[tokio::test]
async fn sentinel_reports_non_default_capabilities() {
    let ctx = SentinelContext::with_shared(seeded_shared_state(SENTINEL_STATE_KEY).await);
    assert!(ctx.is_cancelled());
    assert!(ctx.is_authenticated());
    assert!(!ctx.user_scopes().is_empty());
    assert!(!ctx.request_metadata().is_empty());
    assert!(ctx.shared_state().is_some());
    assert!(ctx.artifacts().is_some());
    assert!(ctx.memory().is_some());
    assert!(ctx.steering_inbox().is_some());
    let _ = Event::new("sentinel-invocation");
}

#[tokio::test]
async fn acting_agent_context_preserves_capabilities_and_names_its_agent() {
    use crate::acting_agent_context::ActingAgentContext;

    let parent = sentinel().await;
    let same = ActingAgentContext::scope(parent.clone(), parent.agent_name());
    assert!(Arc::ptr_eq(&same, &parent), "a context naming the agent is used as is");

    let wrapped = ActingAgentContext::scope(sentinel().await, "sub-agent");
    assert!(wrapped.is_cancelled(), "ActingAgentContext: cancellation was dropped");
    assert_eq!(wrapped.user_scopes(), vec![SENTINEL_SCOPE.to_string()]);
    assert!(wrapped.request_metadata().contains_key(SENTINEL_METADATA_KEY));
    assert!(
        wrapped.is_authenticated(),
        "ActingAgentContext: the authenticated identity was dropped"
    );
    let secret = wrapped.get_secret(SENTINEL_SECRET_NAME).await.unwrap();
    assert_eq!(secret.as_deref(), Some(SENTINEL_SECRET_VALUE));
    assert!(wrapped.shared_state().is_some(), "ActingAgentContext: shared state was dropped");
    assert!(wrapped.artifacts().is_some(), "ActingAgentContext: artifacts were dropped");
    assert!(wrapped.memory().is_some(), "ActingAgentContext: memory was dropped");
    assert_steering_forwarded("ActingAgentContext", &wrapped);
    assert_eq!(wrapped.agent_name(), parent.agent_name(), "the execution identity is unchanged");

    // The subject and chain are the parent's; the principal is the sub-agent's own.
    let expected = adk_core::ActingContext {
        agent: adk_core::AgentPrincipal::derived("sentinel-app", "sub-agent"),
        ..sentinel_acting()
    };
    assert_eq!(wrapped.acting_context(), expected);
}