gadk-engine 0.5.30

The gadk runtime: LLM agent and flow, workflow agents and graphs, and the runner
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
// Copyright 2026 Chris Olson
// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The live flow's session: input and output channels, the streaming-tool registry and the
//! event stream.

use std::any::Any;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context as TaskContext, Poll};

use async_trait::async_trait;
use futures::Stream;
use gadk_agent::{AgentError, EventStream, LiveRequest, LiveSession};
use gadk_session::Event;
use tokio::sync::{mpsc, oneshot};
use tokio_util::sync::CancellationToken;

use crate::llm::live::audio_cache::AudioCacheManager;

type InputMsg = (LiveRequest, oneshot::Sender<()>);
type OutputMsg = (Result<Event, AgentError>, oneshot::Sender<()>);

/// Task accounting for tests.
#[derive(Debug, Default)]
pub(crate) struct LiveProbe {
    /// Tasks spawned by the live flow that are still alive.
    pub(crate) live_tasks: AtomicUsize,
    /// Reconnect waits in progress.
    pub(crate) in_backoff: AtomicUsize,
    /// Completed session teardowns (the flow's drop guard has drained the tool registry).
    pub(crate) teardowns: AtomicUsize,
}

impl LiveProbe {
    /// Counts one live task until the guard is dropped.
    pub(crate) fn task_guard(self: &Arc<Self>) -> LiveTaskGuard {
        self.live_tasks.fetch_add(1, Ordering::SeqCst);
        LiveTaskGuard(self.clone())
    }
}

/// Decrements [`LiveProbe::live_tasks`] on drop (including when the task is aborted).
pub(crate) struct LiveTaskGuard(Arc<LiveProbe>);

impl Drop for LiveTaskGuard {
    fn drop(&mut self) {
        self.0.live_tasks.fetch_sub(1, Ordering::SeqCst);
    }
}

/// A running streaming tool: its call id and cancellation token.
struct ActiveTask {
    call_id: String,
    cancel: CancellationToken,
}

/// The live flow's session.
pub(crate) struct LiveSessionImpl {
    input_tx: mpsc::Sender<InputMsg>,
    input_rx: tokio::sync::Mutex<mpsc::Receiver<InputMsg>>,
    output_tx: mpsc::Sender<OutputMsg>,
    output_rx: parking_lot::Mutex<Option<mpsc::Receiver<OutputMsg>>>,
    done: CancellationToken,
    audio: AudioCacheManager,
    active_tools: parking_lot::Mutex<HashMap<String, Vec<ActiveTask>>>,
    probe: Arc<LiveProbe>,
}

fn eof() -> AgentError {
    // The end-of-stream error.
    AgentError::other("EOF")
}

impl LiveSessionImpl {
    /// Creates a session with empty channels.
    pub(crate) fn new() -> Arc<LiveSessionImpl> {
        let (input_tx, input_rx) = mpsc::channel(1);
        let (output_tx, output_rx) = mpsc::channel(1);
        Arc::new(LiveSessionImpl {
            input_tx,
            input_rx: tokio::sync::Mutex::new(input_rx),
            output_tx,
            output_rx: parking_lot::Mutex::new(Some(output_rx)),
            done: CancellationToken::new(),
            audio: AudioCacheManager::new(),
            active_tools: parking_lot::Mutex::new(HashMap::new()),
            probe: Arc::new(LiveProbe::default()),
        })
    }

    /// Cancelled by `close` (and by dropping the event stream).
    pub(crate) fn done(&self) -> &CancellationToken {
        &self.done
    }

    /// The audio cache manager.
    pub(crate) fn audio(&self) -> &AudioCacheManager {
        &self.audio
    }

    /// Test probe shared by every task of this session.
    pub(crate) fn probe(&self) -> &Arc<LiveProbe> {
        &self.probe
    }

    /// Rendezvous with the event stream. `done` is checked first while queueing, so nothing is
    /// handed over after `close`; once queued, an acknowledgement wins over `done`, because a
    /// completed handoff reports delivery.
    async fn push(&self, item: Result<Event, AgentError>) -> bool {
        let (ack_tx, ack_rx) = oneshot::channel();
        tokio::select! {
            biased;
            _ = self.done.cancelled() => return false,
            sent = self.output_tx.send((item, ack_tx)) => if sent.is_err() { return false; },
        }
        tokio::select! {
            biased;
            acked = ack_rx => acked.is_ok(),
            _ = self.done.cancelled() => false,
        }
    }

    /// Hands `ev` to the event stream; `false` once the session is done.
    pub(crate) async fn push_event(&self, ev: Event) -> bool {
        self.push(Ok(ev)).await
    }

    /// Hands `err` to the event stream; `false` once the session is done.
    pub(crate) async fn push_error(&self, err: AgentError) -> bool {
        self.push(Err(err)).await
    }

    /// Queues `req` on the input channel for a sender task without waiting for it to be taken
    /// (the live flow's function responses): it is sent after the requests queued before
    /// it. `false` once the session is done.
    pub(crate) async fn queue_input(&self, req: LiveRequest) -> bool {
        let (ack_tx, _ack_rx) = oneshot::channel();
        tokio::select! {
            biased;
            _ = self.done.cancelled() => false,
            sent = self.input_tx.send((req, ack_tx)) => sent.is_ok(),
        }
    }

    /// The receiving side of the input channel for one connection's sender task: the next request
    /// (acknowledged, so the caller's `send` returns), or `None` once `conn_token` or `done` is
    /// cancelled. Requests not yet taken stay queued for the next connection's sender.
    pub(crate) async fn next_input(&self, conn_token: &CancellationToken) -> Option<LiveRequest> {
        let mut rx = tokio::select! {
            biased;
            _ = conn_token.cancelled() => return None,
            _ = self.done.cancelled() => return None,
            guard = self.input_rx.lock() => guard,
        };
        tokio::select! {
            biased;
            _ = conn_token.cancelled() => None,
            _ = self.done.cancelled() => None,
            msg = rx.recv() => {
                let (req, ack) = msg?;
                let _ = ack.send(());
                Some(req)
            }
        }
    }

    /// The pushed items until `done`. Called once per session (later calls get an
    /// empty stream). Dropping the stream cancels `done` and detaches `task`, which finishes the
    /// work in flight (closing only cancels `done`; the session task returns on its own).
    pub(crate) fn event_stream(
        self: &Arc<Self>,
        task: Option<tokio::task::JoinHandle<()>>,
    ) -> EventStream {
        let rx = self.output_rx.lock().take();
        let done = self.done.clone();
        let inner: EventStream = Box::pin(async_stream::stream! {
            let Some(mut rx) = rx else { return; };
            loop {
                tokio::select! {
                    biased;
                    _ = done.cancelled() => return,
                    msg = rx.recv() => match msg {
                        None => return,
                        Some((item, ack)) => {
                            let _ = ack.send(());
                            yield item;
                        }
                    },
                }
            }
        });
        Box::pin(GuardedStream {
            inner,
            done: self.done.clone(),
            task,
        })
    }

    /// Registers a running streaming tool instance.
    pub(crate) fn register_streaming_tool(
        &self,
        tool_name: &str,
        call_id: &str,
        cancel: CancellationToken,
    ) {
        self.active_tools
            .lock()
            .entry(tool_name.to_owned())
            .or_default()
            .push(ActiveTask {
                call_id: call_id.to_owned(),
                cancel,
            });
    }

    /// Removes the first entry with `call_id` (without cancelling it) and deletes an emptied
    /// name.
    pub(crate) fn unregister_streaming_tool(&self, tool_name: &str, call_id: &str) {
        let mut tools = self.active_tools.lock();
        let Some(tasks) = tools.get_mut(tool_name) else {
            return;
        };
        if let Some(i) = tasks.iter().position(|t| t.call_id == call_id) {
            tasks.remove(i);
        }
        if tasks.is_empty() {
            tools.remove(tool_name);
        }
    }

    /// Cancels every running instance of `tool_name`; `false` when there was none.
    pub(crate) fn cancel_all_streaming_tools(&self, tool_name: &str) -> bool {
        let mut tools = self.active_tools.lock();
        match tools.remove(tool_name) {
            Some(tasks) if !tasks.is_empty() => {
                for t in &tasks {
                    t.cancel.cancel();
                }
                true
            }
            _ => false,
        }
    }

    /// Teardown: cancels every registered streaming tool, so none outlives the flow.
    pub(crate) fn cancel_every_streaming_tool(&self) {
        let drained: Vec<ActiveTask> = self
            .active_tools
            .lock()
            .drain()
            .flat_map(|(_, tasks)| tasks)
            .collect();
        for t in drained {
            t.cancel.cancel();
        }
    }
}

#[cfg(test)]
impl LiveSessionImpl {
    /// How many instances of `tool_name` are registered.
    pub(crate) fn active_streaming_tool_count(&self, tool_name: &str) -> usize {
        self.active_tools.lock().get(tool_name).map_or(0, Vec::len)
    }
}

#[async_trait]
impl LiveSession for LiveSessionImpl {
    /// Returns once the flow's sender task has taken `req`; `EOF` after `close`. As in
    /// `push`, `done` wins while queueing and a taken request reports success.
    async fn send(&self, req: LiveRequest) -> Result<(), AgentError> {
        let (ack_tx, ack_rx) = oneshot::channel();
        tokio::select! {
            biased;
            _ = self.done.cancelled() => return Err(eof()),
            sent = self.input_tx.send((req, ack_tx)) => if sent.is_err() { return Err(eof()); },
        }
        tokio::select! {
            biased;
            acked = ack_rx => acked.map_err(|_| eof()),
            _ = self.done.cancelled() => Err(eof()),
        }
    }

    /// Closes the session and cancels every streaming tool.
    async fn close(&self) -> Result<(), AgentError> {
        self.done.cancel();
        self.cancel_every_streaming_tool();
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// The event stream plus the teardown its drop performs.
struct GuardedStream {
    inner: EventStream,
    done: CancellationToken,
    task: Option<tokio::task::JoinHandle<()>>,
}

impl Stream for GuardedStream {
    type Item = Result<Event, AgentError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Option<Self::Item>> {
        self.inner.as_mut().poll_next(cx)
    }
}

impl Drop for GuardedStream {
    /// Closing the session (or dropping its stream) cancels `done` and nothing more; the
    /// session task finishes its in-flight function-call handling (after-tool callbacks
    /// included) and returns. The task is detached, not aborted.
    fn drop(&mut self) {
        self.done.cancel();
        drop(self.task.take());
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use std::task::Poll;

    use futures::{StreamExt, poll};
    use gadk_genai::Content;
    use gadk_platform::Platform;

    use super::*;

    fn text_req(t: &str) -> LiveRequest {
        LiveRequest {
            content: Some(Content::from_text(t, "user")),
            ..Default::default()
        }
    }

    fn event(author: &str) -> Event {
        let mut ev = Event::new(&Platform::default(), "inv");
        ev.author = author.into();
        ev
    }

    // The rendezvous tests poll the futures by hand, so they never depend on timing.

    #[tokio::test]
    async fn send_is_a_rendezvous_with_next_input() {
        let sess = LiveSessionImpl::new();
        let mut send = std::pin::pin!(sess.send(text_req("hi")));
        assert!(
            poll!(send.as_mut()).is_pending(),
            "send returned before anyone took the request"
        );
        assert!(
            poll!(send.as_mut()).is_pending(),
            "send returned before anyone took the request"
        );
        let req = sess
            .next_input(&CancellationToken::new())
            .await
            .expect("request");
        assert_eq!(req.content.unwrap().parts[0].text, "hi");
        assert!(
            matches!(poll!(send.as_mut()), Poll::Ready(Ok(()))),
            "send returns once taken"
        );
    }

    #[tokio::test]
    async fn a_second_send_waits_for_its_own_rendezvous() {
        let sess = LiveSessionImpl::new();
        let mut first = std::pin::pin!(sess.send(text_req("1")));
        let mut second = std::pin::pin!(sess.send(text_req("2")));
        assert!(poll!(first.as_mut()).is_pending());
        assert!(poll!(second.as_mut()).is_pending());
        let conn = CancellationToken::new();
        assert_eq!(
            sess.next_input(&conn).await.unwrap().content.unwrap().parts[0].text,
            "1"
        );
        assert!(matches!(poll!(first.as_mut()), Poll::Ready(Ok(()))));
        assert!(
            poll!(second.as_mut()).is_pending(),
            "the second send is not taken yet"
        );
        assert_eq!(
            sess.next_input(&conn).await.unwrap().content.unwrap().parts[0].text,
            "2"
        );
        assert!(matches!(poll!(second.as_mut()), Poll::Ready(Ok(()))));
    }

    #[tokio::test]
    async fn a_waiting_send_is_released_by_close_with_eof() {
        let sess = LiveSessionImpl::new();
        let mut send = std::pin::pin!(sess.send(text_req("never taken")));
        assert!(poll!(send.as_mut()).is_pending());
        sess.close().await.unwrap();
        match poll!(send.as_mut()) {
            Poll::Ready(Err(e)) => assert_eq!(e.to_string(), "EOF"),
            other => panic!("want EOF, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn an_untaken_request_stays_queued_for_the_next_connection() {
        let sess = LiveSessionImpl::new();
        let mut send = std::pin::pin!(sess.send(text_req("carry")));
        assert!(poll!(send.as_mut()).is_pending());
        let old_conn = CancellationToken::new();
        old_conn.cancel();
        assert!(sess.next_input(&old_conn).await.is_none());
        assert!(
            poll!(send.as_mut()).is_pending(),
            "a cancelled connection does not take the request"
        );
        let req = sess.next_input(&CancellationToken::new()).await.unwrap();
        assert_eq!(req.content.unwrap().parts[0].text, "carry");
        assert!(matches!(poll!(send.as_mut()), Poll::Ready(Ok(()))));
    }

    #[tokio::test]
    async fn push_is_a_rendezvous_with_the_stream() {
        let sess = LiveSessionImpl::new();
        let mut stream = sess.event_stream(None);
        let mut push = std::pin::pin!(sess.push_event(event("a")));
        assert!(
            poll!(push.as_mut()).is_pending(),
            "push returned before the stream took the event"
        );
        assert!(
            poll!(push.as_mut()).is_pending(),
            "push returned before the stream took the event"
        );
        assert_eq!(stream.next().await.unwrap().unwrap().author, "a");
        assert!(matches!(poll!(push.as_mut()), Poll::Ready(true)));
    }

    #[tokio::test]
    async fn a_waiting_push_is_released_by_close() {
        let sess = LiveSessionImpl::new();
        let mut stream = sess.event_stream(None);
        let mut push = std::pin::pin!(sess.push_event(event("never taken")));
        assert!(poll!(push.as_mut()).is_pending());
        sess.close().await.unwrap();
        assert!(matches!(poll!(push.as_mut()), Poll::Ready(false)));
        assert!(
            stream.next().await.is_none(),
            "the stream never yields after close"
        );
    }

    #[tokio::test]
    async fn send_after_close_is_eof() {
        let sess = LiveSessionImpl::new();
        sess.close().await.unwrap();
        sess.close().await.unwrap(); // idempotent
        let err = sess.send(text_req("late")).await.unwrap_err();
        assert_eq!(err.to_string(), "EOF");
    }

    #[tokio::test]
    async fn next_input_returns_none_when_the_connection_ends() {
        let sess = LiveSessionImpl::new();
        let conn = CancellationToken::new();
        conn.cancel();
        assert!(sess.next_input(&conn).await.is_none());
        let open = CancellationToken::new();
        sess.close().await.unwrap();
        assert!(sess.next_input(&open).await.is_none());
    }

    #[tokio::test]
    async fn pushed_event_reaches_the_stream_which_ends_at_close() {
        let sess = LiveSessionImpl::new();
        let mut stream = sess.event_stream(None);
        let s2 = sess.clone();
        let pusher = tokio::spawn(async move { s2.push_event(event("a")).await });
        let got = stream.next().await.unwrap().unwrap();
        assert_eq!(got.author, "a");
        assert!(pusher.await.unwrap(), "push_event reports delivery");
        sess.close().await.unwrap();
        assert!(stream.next().await.is_none(), "the stream ends at close");
        assert!(
            !sess.push_error(AgentError::other("late")).await,
            "nothing is delivered after close"
        );
    }

    #[tokio::test]
    async fn pushed_error_is_delivered_before_close() {
        let sess = LiveSessionImpl::new();
        let mut stream = sess.event_stream(None);
        let s2 = sess.clone();
        tokio::spawn(async move {
            s2.push_error(AgentError::other("boom")).await;
            s2.close().await.unwrap();
        });
        assert_eq!(
            stream.next().await.unwrap().unwrap_err().to_string(),
            "boom"
        );
        assert!(stream.next().await.is_none());
    }

    #[tokio::test]
    async fn dropping_the_stream_cancels_done_and_lets_the_task_finish() {
        let sess = LiveSessionImpl::new();
        // The task observes `done`, then finishes its in-flight work (Go's session goroutine
        // after Close): an aborted task would never send `finished`.
        let (finished_tx, finished) = oneshot::channel::<()>();
        let done = sess.done().clone();
        let task = tokio::spawn(async move {
            done.cancelled().await;
            tokio::task::yield_now().await;
            let _ = finished_tx.send(());
        });
        let stream = sess.event_stream(Some(task));
        drop(stream);
        assert!(sess.done().is_cancelled());
        // The timeout only turns an aborted task into a failure instead of a hang.
        let got = tokio::time::timeout(std::time::Duration::from_secs(30), finished).await;
        assert!(matches!(got, Ok(Ok(()))), "the session task was aborted");
    }

    #[test]
    fn streaming_tool_registry_matches_go() {
        let sess = LiveSessionImpl::new();
        let (c1, c2) = (CancellationToken::new(), CancellationToken::new());
        sess.register_streaming_tool("count", "call_1", c1.clone());
        sess.register_streaming_tool("count", "call_2", c2.clone());
        sess.unregister_streaming_tool("count", "call_1");
        sess.unregister_streaming_tool("count", "missing"); // no-op
        sess.unregister_streaming_tool("other", "call_1"); // no-op
        assert!(!c1.is_cancelled(), "unregistering does not cancel");
        assert!(sess.cancel_all_streaming_tools("count"));
        assert!(c2.is_cancelled());
        assert!(
            !sess.cancel_all_streaming_tools("count"),
            "the name was deleted"
        );
        assert!(!sess.cancel_all_streaming_tools("never_registered"));
        // Unregistering the last instance deletes the name.
        let c3 = CancellationToken::new();
        sess.register_streaming_tool("solo", "call_3", c3.clone());
        sess.unregister_streaming_tool("solo", "call_3");
        assert!(!sess.cancel_all_streaming_tools("solo"));
        assert!(!c3.is_cancelled());
    }

    #[tokio::test]
    async fn close_cancels_every_streaming_tool() {
        let sess = LiveSessionImpl::new();
        let (a, b) = (CancellationToken::new(), CancellationToken::new());
        sess.register_streaming_tool("x", "1", a.clone());
        sess.register_streaming_tool("y", "2", b.clone());
        sess.close().await.unwrap();
        assert!(a.is_cancelled() && b.is_cancelled());
    }

    #[test]
    fn probe_counts_task_guards() {
        let sess = LiveSessionImpl::new();
        let g1 = sess.probe().task_guard();
        let g2 = sess.probe().task_guard();
        assert_eq!(sess.probe().live_tasks.load(Ordering::SeqCst), 2);
        drop(g1);
        drop(g2);
        assert_eq!(sess.probe().live_tasks.load(Ordering::SeqCst), 0);
    }
}