gadk-engine 0.5.24

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
// 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 runner's context compaction: config resolution, post-invocation sliding-window compaction,
//! and the end-of-run work (after-run callbacks and compaction) as one guard.

use std::panic::AssertUnwindSafe;
use std::sync::Arc;
use std::time::Duration;

use futures::FutureExt;
use gadk_agent::fiber::{self, Fiber};
use gadk_agent::{Agent, AgentError, ContextDelta, InvocationContext, go_quote};
use gadk_session::compaction::{CompactionError, Config, LlmSummarizer, LlmSummarizerConfig};
use gadk_session::{Event, Session, SessionService};
use tokio_util::sync::{CancellationToken, DropGuard};
use tracing::Instrument;

use super::RunnerInner;
use crate::compaction::{BoxError, apply, compactor, has_sliding_window, runtime, summary_event};
use crate::llm::state::reveal;
use crate::tool::utils::panic_message;

/// The bound on the summarizer the runner installs itself.
pub(crate) const DEFAULT_SUMMARIZER_TIMEOUT: Duration = Duration::from_secs(60);

/// Validates `cfg` and fills in the default summarizer (an [`LlmSummarizer`] over the root agent's
/// model and generation config). The caller's config is never mutated; `None` means compaction is
/// disabled.
pub(crate) fn resolve_compaction_config(
    cfg: Option<&Config>,
    root: &Arc<dyn Agent>,
    summarizer_timeout: Duration,
) -> Result<Option<Arc<Config>>, AgentError> {
    let Some(cfg) = cfg else {
        return Ok(None);
    };
    cfg.validate()
        .map_err(|e| AgentError::Config(format!("invalid compaction config: {e}")))?;
    // Copy so the caller's config is not mutated by the summarizer default.
    let mut resolved = cfg.clone();
    if resolved.summarizer.is_some() {
        return Ok(Some(Arc::new(resolved)));
    }
    let Some(state) = reveal(&**root) else {
        return Err(AgentError::Config(format!(
            "the Compaction config needs a Summarizer: root agent {} is not an LLM agent, so no default model is available",
            go_quote(root.name())
        )));
    };
    let Some(model) = state.cfg.model.clone() else {
        return Err(AgentError::Config(format!(
            "the Compaction config needs a Summarizer: root agent {} has no model",
            go_quote(root.name())
        )));
    };
    let summarizer = LlmSummarizer::new(LlmSummarizerConfig {
        model: Some(model),
        generate_content_config: state.cfg.generate_content_config.clone(),
        timeout: summarizer_timeout,
        ..Default::default()
    })
    .map_err(|e| {
        AgentError::Config(format!(
            "failed to create the default compaction summarizer: {e}"
        ))
    })?;
    resolved.summarizer = Some(Arc::new(summarizer));
    Ok(Some(Arc::new(resolved)))
}

/// Wraps `err` as a compaction error with the `post-invocation` context.
fn post_invocation(err: BoxError) -> AgentError {
    AgentError::Compaction(CompactionError::wrap("post-invocation", err))
}

/// Drops a summary that raced with another compaction, finishing its span without a record.
fn discard_raced(finish: compactor::Finish) {
    finish.finish(
        None,
        "another compaction covering the same events landed while summarizing",
    );
    tracing::warn!(
        "adk: discarding a context compaction summary because the session changed inside its range while summarizing"
    );
}

impl RunnerInner {
    /// Sliding-window compaction once an invocation's events are persisted. The summary is stored,
    /// not yielded. A panic anywhere inside (a summarizer's, resumed by the compactor's span)
    /// becomes a compaction error rather than unwinding into the caller.
    pub(crate) async fn compact_after_invocation(
        &self,
        stored: &Arc<dyn Session>,
        ictx: &InvocationContext,
    ) -> Result<(), AgentError> {
        let Some(cfg) = self.compaction.clone() else {
            return Ok(());
        };
        if !has_sliding_window(Some(&cfg)) {
            return Ok(());
        }
        match AssertUnwindSafe(self.compact_after_invocation_inner(&cfg, stored, ictx))
            .catch_unwind()
            .await
        {
            Ok(r) => r,
            Err(payload) => Err(AgentError::Compaction(CompactionError::new(format!(
                "post-invocation: compaction panicked: {}",
                panic_message(&*payload)
            )))),
        }
    }

    async fn compact_after_invocation_inner(
        &self,
        cfg: &Config,
        stored: &Arc<dyn Session>,
        ictx: &InvocationContext,
    ) -> Result<(), AgentError> {
        // Tail retention already compacted this invocation: do not summarize it again.
        if runtime::already_compacted(ictx) {
            return Ok(());
        }
        // Compaction is an optimisation, so a cancelled run does not start one.
        if ictx.cancellation_token().is_cancelled() {
            return Ok(());
        }

        // Re-read: the handle the invocation began with predates the turn.
        let current = apply::reload_session(Some(&*self.session_service), &**stored)
            .await
            .map_err(post_invocation)?;
        let (summary, finish) =
            compactor::sliding_window(ictx.platform(), cfg, Some(&*current), ictx.invocation_id())
                .await
                .map_err(post_invocation)?;
        let Some(mut summary) = summary else {
            return Ok(());
        };

        if ictx.cancellation_token().is_cancelled() {
            finish.finish(None, "the run ended before the summary could be stored");
            return Ok(());
        }
        // The identities present when the window was chosen, for the repair after the append.
        let known = apply::known_event_ids(&*current);

        match self.summary_raced(stored, &*current, &summary).await {
            Err(e) => {
                finish.finish(Some(&*e), "");
                return Err(post_invocation(e));
            }
            Ok(true) => {
                discard_raced(finish);
                return Ok(());
            }
            Ok(false) => {}
        }

        // Plugins see the summary before it is stored. A plugin's replacement is taken as is (a
        // plugin may rewrite a summary) and filtered like a summarizer's output.
        match self
            .plugin_manager
            .run_on_event(ictx.clone(), summary.clone())
            .await
        {
            Err(e) => {
                finish.finish(Some(&e), "");
                return Err(AgentError::Compaction(CompactionError::wrap(
                    "plugin rejected the summary event",
                    e,
                )));
            }
            Ok(Some(mut modified)) => {
                if !summary_event::sanitize_summary(&mut modified) {
                    finish.finish(None, "a plugin left the summary with nothing usable in it");
                    tracing::warn!(
                        "adk: discarding a context compaction summary because a plugin left no usable content in it"
                    );
                    return Ok(());
                }
                summary = modified;
            }
            Ok(None) => {}
        }

        // The plugin pass is the widest part of the window, so the check is repeated.
        match self.summary_raced(stored, &*current, &summary).await {
            Err(e) => {
                finish.finish(Some(&*e), "");
                return Err(post_invocation(e));
            }
            Ok(true) => {
                discard_raced(finish);
                return Ok(());
            }
            Ok(false) => {}
        }

        if let Err(e) = self
            .session_service
            .append_event(&*current, &mut summary)
            .await
        {
            finish.finish(Some(&e), "");
            return Err(AgentError::Compaction(CompactionError::wrap(
                "failed to append the summary event",
                e,
            )));
        }
        finish.finish(None, "");

        // What lands during the append is repaired after the fact, detached from the run's
        // cancellation (spawned, so dropping this future does not stop it) and bounded by the
        // repair timeout; failures are logged.
        let repaired = apply::repair(repair_after_append(
            Arc::clone(&self.session_service),
            Arc::clone(stored),
            current,
            summary,
            known,
        ))
        .await;
        if repaired.is_none() {
            tracing::warn!(
                "adk: could not check a stored compaction for stragglers: the repair timed out"
            );
        }
        Ok(())
    }

    /// Re-reads the session and reports whether anything landed inside the summary's range since
    /// the window was chosen.
    async fn summary_raced(
        &self,
        stored: &Arc<dyn Session>,
        current: &dyn Session,
        summary: &Event,
    ) -> Result<bool, BoxError> {
        let latest = apply::reload_session(Some(&*self.session_service), &**stored).await?;
        Ok(apply::range_raced(&*latest, current, summary))
    }
}

/// Straggler repair: stores a corrected record naming every event that landed inside the stored
/// summary's range unseen. Owns its inputs so [`apply::repair`] can detach it from the run.
async fn repair_after_append(
    svc: Arc<dyn SessionService>,
    stored: Arc<dyn Session>,
    current: Arc<dyn Session>,
    summary: Event,
    known: std::collections::HashSet<String>,
) {
    let latest = match apply::reload_session(Some(&*svc), &*stored).await {
        Ok(latest) => latest,
        Err(e) => {
            tracing::warn!(
                "adk: could not re-read the session to check a stored compaction for stragglers: {e}"
            );
            return;
        }
    };
    let Some(mut repair) = apply::repair_after_append(&summary, &known, &*latest) else {
        return;
    };
    let excluded = |ev: &Event| {
        ev.actions
            .compaction
            .as_ref()
            .map_or(0, |c| c.excluded_events.len())
    };
    let added = excluded(&repair).saturating_sub(excluded(&summary));
    match svc.append_event(&*current, &mut repair).await {
        Err(e) => tracing::warn!("adk: could not store a corrected compaction record: {e}"),
        Ok(()) => tracing::warn!(
            "adk: corrected a compaction record that would have covered {added} event(s) it did not summarize"
        ),
    }
}

/// The end-of-run work of an invocation as one guard: compaction (pending from construction, so on
/// an early return it runs last) and the after-run callbacks (pending once
/// [`InvocationGuard::defer_after_run`] is called).
///
/// - Normal end: [`InvocationGuard::compact_inline`] (its error is the stream's last item),
///   then [`InvocationGuard::finish`] (after-run callbacks).
/// - Early return inside the stream: [`InvocationGuard::finish_early`]: after-run, then
///   compaction with its error logged.
/// - Stream dropped: `Drop` spawns one task on the current runtime that runs the after-run
///   callbacks and then, if the stream was dropped at a yield, compaction on a context
///   re-derived with a fresh cancellation token (stopping at a yield leaves the caller's context
///   uncancelled), inside the span and the fiber that were current when the guard was made (the
///   guard is made inside the runner's consumer `FiberStream`). Errors are logged.
///
/// Before the after-run callbacks (inline or spawned) the guard awaits the invocation's teardown
/// registry ([`gadk_agent::Teardowns`]): the started work that components dropped during the run
/// handed over (a ParallelAgent's branches, a workflow scheduler's node tasks, in-flight tool
/// calls), so that work finishes before the after-run callbacks. The token is still live while it
/// is awaited after a stop at a yield.
///
/// The guard also owns the invocation token's cancel-on-drop ([`InvocationGuard::cancel_on_drop`]).
/// A drop inside an await is a cancellation: the token is cancelled at once and the after-run
/// callbacks see that. A drop at a yield (the consumer stopped reading) cancels it only once the
/// spawned after-run callbacks return, so they, and every span record holding the token, see a live
/// context.
///
/// Compaction is skipped once the invocation yielded an error ([`InvocationGuard::mark_failed`]),
/// and when the stream is dropped while suspended inside an await rather than at a yield
/// ([`InvocationGuard::at_yield`]).
///
/// A drop before the summary append declines compaction as a cancellation; once the summary is
/// stored, the straggler repair is detached ([`apply::repair`] spawns it) and always runs.
///
/// The live path ([`InvocationGuard::no_compaction`]) uses only the after-run and cancel-on-drop
/// parts: live runs do not compact.
pub(crate) struct InvocationGuard {
    inner: Arc<RunnerInner>,
    stored: Arc<dyn Session>,
    ctx: InvocationContext,
    failed: bool,
    /// The stream is suspended at a `yield` of an event (see [`InvocationGuard::at_yield`]).
    yielding: bool,
    after_run_pending: bool,
    compaction_pending: bool,
    /// Cancels the invocation token when dropped (see [`InvocationGuard::cancel_on_drop`]).
    cancel_on_drop: Option<DropGuard>,
    span: tracing::Span,
    /// The consumer's fiber, re-entered by the task `Drop` spawns.
    fiber: Fiber,
}

impl InvocationGuard {
    /// A guard with compaction pending; the after-run callbacks are not pending yet.
    pub(crate) fn new(
        inner: Arc<RunnerInner>,
        stored: Arc<dyn Session>,
        ctx: InvocationContext,
    ) -> Self {
        Self {
            inner,
            stored,
            ctx,
            failed: false,
            yielding: false,
            after_run_pending: false,
            compaction_pending: true,
            cancel_on_drop: None,
            span: tracing::Span::current(),
            fiber: fiber::current(),
        }
    }

    /// No compaction for this invocation (live runs attach no compaction runtime).
    pub(crate) fn no_compaction(&mut self) {
        self.compaction_pending = false;
    }

    /// Hands the invocation token's cancel-on-drop to the guard, which cancels it when the
    /// guard is dropped, after the after-run callbacks when the stream was dropped at a yield.
    pub(crate) fn cancel_on_drop(&mut self, guard: DropGuard) {
        self.cancel_on_drop = Some(guard);
    }

    /// Marks the after-run callbacks as pending: they run when the guard finishes or is dropped.
    pub(crate) fn defer_after_run(&mut self) {
        self.after_run_pending = true;
    }

    /// Call immediately before yielding any error; compaction is then skipped.
    pub(crate) fn mark_failed(&mut self) {
        self.failed = true;
    }

    /// Brackets every `yield` (of an event or an error): `true` immediately before it, `false`
    /// immediately after the stream resumes. A consumer can only stop early at a yield (the
    /// after-run callbacks and compaction then run on the caller's still-live context; an error
    /// yield has already skipped compaction). A stream dropped anywhere else was dropped inside an
    /// await (a pending model call or append), which is a cancellation: the model call fails or
    /// compaction declines. So `Drop` compacts only when this is set.
    pub(crate) fn at_yield(&mut self, yielding: bool) {
        self.yielding = yielding;
    }

    /// Runs the pending compaction after the event loop; its error, if any, is returned.
    pub(crate) async fn compact_inline(&mut self) -> Option<AgentError> {
        if !std::mem::take(&mut self.compaction_pending) || self.failed {
            return None;
        }
        self.inner
            .compact_after_invocation(&self.stored, &self.ctx)
            .await
            .err()
    }

    /// Runs the after-run callbacks, at the end of a normal run.
    pub(crate) async fn finish(mut self) {
        if std::mem::take(&mut self.after_run_pending) {
            wait_teardowns(&self.ctx).await;
            self.inner
                .plugin_manager
                .run_after_run(self.ctx.clone())
                .await;
        }
    }

    /// An early `return`: the after-run callbacks, then compaction (its error is logged).
    pub(crate) async fn finish_early(mut self) {
        if std::mem::take(&mut self.after_run_pending) {
            wait_teardowns(&self.ctx).await;
            self.inner
                .plugin_manager
                .run_after_run(self.ctx.clone())
                .await;
        }
        if let Some(e) = self.compact_inline().await {
            tracing::warn!("adk: {e}");
        }
    }
}

/// Awaits the started work that components dropped during the invocation handed to its teardown
/// registry ([`gadk_agent::Teardowns`]), so that work finishes before the after-run callbacks run.
async fn wait_teardowns(ctx: &InvocationContext) {
    if let Some(t) = ctx.teardowns() {
        t.wait().await;
    }
}

// A dropped stream cannot block its dropper, so the end-of-run work (teardown, after-run callbacks,
// compaction) runs on a spawned task re-entering the consumer's fiber, which may overlap whatever
// the consumer does next.
impl Drop for InvocationGuard {
    fn drop(&mut self) {
        let after_run = std::mem::take(&mut self.after_run_pending);
        // Dropped mid-await is cancellation, not an early stop (see `at_yield`).
        let pending = std::mem::take(&mut self.compaction_pending) && !self.failed;
        if pending && !self.yielding {
            tracing::debug!("adk: skipping context compaction: the run was dropped mid-await");
        }
        let compact = pending && self.yielding;
        if !after_run && !compact {
            return;
        }
        let Ok(handle) = tokio::runtime::Handle::try_current() else {
            return;
        };
        // At a yield the token outlives the after-run callbacks (the task drops the guard after
        // them, or when it is itself dropped unrun); inside an await the field's drop (after
        // this body) cancels it before the spawned task runs.
        let cancel_later = if self.yielding {
            self.cancel_on_drop.take()
        } else {
            None
        };
        let (inner, stored, ctx) = (self.inner.clone(), self.stored.clone(), self.ctx.clone());
        let task = self.fiber.clone().scope(async move {
            if after_run {
                wait_teardowns(&ctx).await;
                inner.plugin_manager.run_after_run(ctx.clone()).await;
            }
            drop(cancel_later);
            if compact {
                let fresh = ctx.with_delta(ContextDelta {
                    cancellation_token: Some(CancellationToken::new()),
                    ..Default::default()
                });
                if let Err(e) = inner.compact_after_invocation(&stored, &fresh).await {
                    tracing::warn!("adk: {e}");
                }
            }
        });
        handle.spawn(task.instrument(self.span.clone()));
    }
}