gadk-engine 0.4.3

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
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// 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.

//! Port of ADK-Go `internal/compactioninternal/telemetry_test.go`.
//!
//! Spans are captured with `capture_spans` (a string map of fields): Go's FLOAT64 type checks
//! become "parses as an f64 equal to Go's epoch seconds", and Go's `exception` event /
//! `error.type` become the span error fields (`otel.status_code = "ERROR"`, `error.message`,
//! and the error event's `otel.status_description`).

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

use async_trait::async_trait;
use futures::FutureExt;
use gadk_genai::GenerateContentResponseUsageMetadata as Usage;
use gadk_model::Backend;
use gadk_platform::Platform;
use gadk_session::compaction::{Config, SummarizeFailure, SummarizeResult, Summarizer};
use gadk_session::{Event, Session, zero_time};
use pretty_assertions::assert_eq;

use crate::compaction::compactor::sliding_window;
use crate::compaction::tail_retention::{TokenCounter, TurnScope};
use crate::compaction::test_support::{
    StaticSession, at, fakeSummarizer, model_text_event, sliding_window_stored,
    tail_retention_stored, text_event,
};
use crate::llm::flow::span_capture::{CapturedSpan, capture_spans};
use crate::telemetry::gen_ai_system;

type BoxErr = Box<dyn std::error::Error + Send + Sync>;

fn two_invocations() -> Vec<Event> {
    vec![
        text_event("a", "inv1", 1, "q1"),
        model_text_event("b", "inv1", 2, "a1"),
        text_event("c", "inv2", 3, "q2"),
        model_text_event("d", "inv2", 4, "a2"),
    ]
}

fn cfg_with(interval: i32, overlap: i32, s: Arc<dyn Summarizer>) -> Config {
    Config {
        compaction_interval: interval,
        overlap_size: overlap,
        summarizer: Some(s),
        ..Default::default()
    }
}

/// Go exp.GetSpans(): the closed compaction spans.
fn compaction_spans(spans: Vec<CapturedSpan>) -> Vec<CapturedSpan> {
    spans
        .into_iter()
        .filter(|s| s.name == "compact_events" && s.closed)
        .collect()
}

fn is_error(s: &CapturedSpan) -> bool {
    s.field("otel.status_code").as_deref() == Some("ERROR")
}

/// Go `float64(t.UnixNano()) / float64(time.Second)`, computed independently of the code under test.
fn go_epoch_seconds(t: chrono::DateTime<chrono::Utc>) -> f64 {
    t.timestamp_nanos_opt().unwrap() as f64 / 1e9
}

fn float_field(s: &CapturedSpan, k: &str) -> Option<f64> {
    s.field(k).map(|v| {
        v.parse::<f64>()
            .unwrap_or_else(|_| panic!("{k} = {v:?} is not a float"))
    })
}

/// Go panickingSummarizer.
#[allow(non_camel_case_types)]
struct panickingSummarizer;

#[async_trait]
impl Summarizer for panickingSummarizer {
    async fn summarize_events(&self, _: &[Event]) -> Result<SummarizeResult, BoxErr> {
        panic!("summarizer exploded")
    }
}

/// Go bothSummarizer. A Rust result cannot carry content and an error together, so it returns
/// the error only (see the adapted annotation on its test).
#[allow(non_camel_case_types)]
struct bothSummarizer;

#[async_trait]
impl Summarizer for bothSummarizer {
    async fn summarize_events(&self, _: &[Event]) -> Result<SummarizeResult, BoxErr> {
        Err("boom".into())
    }
}

/// Go geminiSummarizer: a fakeSummarizer that reports a backend.
#[allow(non_camel_case_types)]
struct geminiSummarizer {
    inner: Arc<fakeSummarizer>,
    backend: Option<Backend>,
}

#[async_trait]
impl Summarizer for geminiSummarizer {
    async fn summarize_events(&self, events: &[Event]) -> Result<SummarizeResult, BoxErr> {
        self.inner.summarize_events(events).await
    }
    fn google_variant(&self) -> Option<Backend> {
        self.backend
    }
}

/// Go usageSummarizer.
#[allow(non_camel_case_types)]
struct usageSummarizer {
    inner: Arc<fakeSummarizer>,
    prompt: i32,
    output: i32,
}

#[async_trait]
impl Summarizer for usageSummarizer {
    async fn summarize_events(&self, events: &[Event]) -> Result<SummarizeResult, BoxErr> {
        let mut res = self.inner.summarize_events(events).await?;
        if res.content.is_none() {
            return Ok(res);
        }
        res.usage = Some(Usage {
            prompt_token_count: self.prompt,
            candidates_token_count: self.output,
            ..Default::default()
        });
        Ok(res)
    }
}

/// Go usageReportingDecliner.
#[allow(non_camel_case_types)]
struct usageReportingDecliner {
    usage: Usage,
}

#[async_trait]
impl Summarizer for usageReportingDecliner {
    async fn summarize_events(&self, _: &[Event]) -> Result<SummarizeResult, BoxErr> {
        Ok(SummarizeResult {
            content: None,
            usage: Some(self.usage.clone()),
        })
    }
}

/// Go usageReportingFailer: fails while reporting what it spent (Go-parity note 4).
#[allow(non_camel_case_types)]
struct usageReportingFailer {
    usage: Usage,
}

#[async_trait]
impl Summarizer for usageReportingFailer {
    async fn summarize_events(&self, _: &[Event]) -> Result<SummarizeResult, BoxErr> {
        Err(Box::new(SummarizeFailure {
            usage: Some(self.usage.clone()),
            source: "summarizer stopped before finishing (finish reason \"MAX_TOKENS\"), so the summary is incomplete".into(),
        }))
    }
}

// parity: adapted: FLOAT64 type checks become "parses as an f64 equal to Go's epoch seconds" (CapturedSpan fields are strings)
#[tokio::test]
async fn test_sliding_window_emits_span() {
    let sess = StaticSession::new(two_invocations());
    let cfg = cfg_with(2, 1, fakeSummarizer::new("sum"));
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    let got = got
        .unwrap()
        .expect("slidingWindowStored() produced no summary");
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1);
    let span = &spans[0];
    assert_eq!(span.otel_name, "compact_events sliding_window");
    assert!(!is_error(span), "span status = error, want unset");
    for (k, want) in [
        ("gen_ai.operation.name", "compact_events"),
        ("gen_ai.conversation.id", "sess"),
        ("gen_ai.compaction.trigger", "sliding_window"),
        ("gen_ai.compaction.summarizer_type", "fakeSummarizer"),
        ("gen_ai.compaction.result_event_id", got.id.as_str()),
    ] {
        assert_eq!(span.field(k).as_deref(), Some(want), "attribute {k}");
    }
    assert_eq!(
        span.field("gen_ai.compaction.event_count").as_deref(),
        Some("4")
    );
    assert_eq!(
        span.field("gen_ai.compaction.compaction_interval")
            .as_deref(),
        Some("2")
    );
    assert_eq!(
        span.field("gen_ai.compaction.overlap_size").as_deref(),
        Some("1")
    );
    assert!(span.field("gen_ai.compaction.token_threshold").is_none());
    assert_eq!(
        float_field(span, "gen_ai.compaction.start_timestamp"),
        Some(go_epoch_seconds(at(1)))
    );
    assert_eq!(
        float_field(span, "gen_ai.compaction.end_timestamp"),
        Some(go_epoch_seconds(at(4)))
    );
    assert!(!got.id.is_empty(), "result_event_id is empty");
}

// parity: adapted: the OTel exception event and error.type become otel.status_code = "ERROR" and error.message
#[tokio::test]
async fn test_compaction_span_marks_a_panic() {
    let sess = StaticSession::new(two_invocations());
    let cfg = cfg_with(2, 0, Arc::new(panickingSummarizer));
    let (outcome, spans) = capture_spans(
        AssertUnwindSafe(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).catch_unwind(),
    )
    .await;
    assert!(
        outcome.is_err(),
        "the panic did not propagate; compaction must not swallow it"
    );
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1);
    assert!(
        is_error(&spans[0]),
        "a panicking summarizer must not look healthy"
    );
    assert_eq!(
        spans[0].field("error.message").as_deref(),
        Some("summarizer panicked: summarizer exploded"),
        "the failed span has no error"
    );
}

// parity: adapted: Go checks the span carries no FLOAT64 error.type; Rust checks the usage fields as strings and the error fields of the failed span
#[tokio::test]
async fn test_failed_summarization_reports_what_it_spent() {
    let sess = StaticSession::new(two_invocations());
    let usage = Usage {
        prompt_token_count: 4000,
        candidates_token_count: 8,
        ..Default::default()
    };
    let cfg = cfg_with(2, 1, Arc::new(usageReportingFailer { usage }));
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    assert!(
        got.is_err(),
        "slidingWindowStored() error = None, want the summarizer's failure"
    );
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1);
    assert!(
        spans[0].field("gen_ai.usage.input_tokens").is_some(),
        "no input tokens for a failure that reported usage"
    );
    assert!(
        spans[0].field("gen_ai.usage.output_tokens").is_some(),
        "no output tokens for a failure that reported usage"
    );
    assert!(
        spans[0]
            .field("gen_ai.compaction.result_event_id")
            .is_none(),
        "a failed compaction named a result event"
    );
}

// parity: adapted: the attribute key set is compared with the otel.* keys excluded (tracing's otel.name carries Go's span name)
#[tokio::test]
async fn test_compaction_span_attribute_key_set() {
    let sess = StaticSession::new(two_invocations());
    let cfg = cfg_with(2, 1, fakeSummarizer::new("SUM"));
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    got.unwrap();
    let spans = compaction_spans(spans);
    let mut keys: Vec<&str> = spans[0]
        .fields
        .keys()
        .map(String::as_str)
        .filter(|k| !k.starts_with("otel."))
        .collect();
    keys.sort_unstable();
    assert_eq!(
        keys,
        [
            "gcp.vertex.agent.invocation_id",
            "gen_ai.compaction.compaction_interval",
            "gen_ai.compaction.end_timestamp",
            "gen_ai.compaction.event_count",
            "gen_ai.compaction.overlap_size",
            "gen_ai.compaction.result_event_id",
            "gen_ai.compaction.start_timestamp",
            "gen_ai.compaction.summarizer_type",
            "gen_ai.compaction.trigger",
            "gen_ai.conversation.id",
            "gen_ai.operation.name",
        ],
        "these keys are shared with adk-python; change them together"
    );
}

#[tokio::test]
async fn test_compaction_span_records_gen_ai_system() {
    let cases = [
        ("vertex ai", Some(Backend::VertexAi), Some("gcp.vertex_ai")),
        ("gemini api", Some(Backend::GeminiApi), Some("gcp.gemini")),
        ("summarizer that does not say", None, None),
    ];
    for (name, backend, want) in cases {
        // The expectation comes from the shared mapping, so this table tracks it.
        assert_eq!(
            gen_ai_system(backend),
            want,
            "{name}: the shared mapping changed; this table is stale"
        );
        let sess = StaticSession::new(two_invocations());
        let cfg = cfg_with(
            2,
            0,
            Arc::new(geminiSummarizer {
                inner: fakeSummarizer::new("SUM"),
                backend,
            }),
        );
        let (got, spans) =
            capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
        got.unwrap();
        let spans = compaction_spans(spans);
        assert_eq!(spans.len(), 1, "{name}");
        assert_eq!(spans[0].field("gen_ai.system").as_deref(), want, "{name}");
    }
}

// parity: adapted: the OTel exception event becomes the error event's otel.status_description on the captured span
#[tokio::test]
async fn test_compaction_span_records_failure() {
    let sess = StaticSession::new(two_invocations());
    let cfg = cfg_with(2, 0, fakeSummarizer::failing("boom"));
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    assert!(
        got.is_err(),
        "slidingWindowStored() succeeded, want an error"
    );
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1);
    assert!(is_error(&spans[0]), "span status is not error");
    assert!(
        spans[0].field("otel.status_description").is_some(),
        "span records no exception event, so the failure reason is lost"
    );
    for key in [
        "gen_ai.compaction.result_event_id",
        "gen_ai.compaction.start_timestamp",
        "gen_ai.compaction.end_timestamp",
    ] {
        assert!(
            spans[0].field(key).is_none(),
            "{key} is present on a failed compaction span, want it omitted"
        );
    }
}

#[tokio::test]
async fn test_no_span_when_nothing_to_compact() {
    let sess = StaticSession::new(vec![text_event("a", "inv1", 1, "q1")]);
    let cfg = cfg_with(5, 0, fakeSummarizer::new("sum"));
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    let got = got.unwrap();
    assert!(got.is_none(), "slidingWindowStored() = {got:?}, want None");
    let n = spans.iter().filter(|s| s.name == "compact_events").count();
    assert_eq!(
        n, 0,
        "got {n} spans when the interval was not reached, want 0"
    );
}

#[tokio::test]
async fn test_span_records_declining_summarizer() {
    let sess = StaticSession::new(two_invocations());
    let cfg = cfg_with(2, 0, fakeSummarizer::new(""));
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    got.unwrap();
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1);
    assert!(
        !is_error(&spans[0]),
        "span status = error, want success for a summarizer that merely declined"
    );
    assert!(
        spans[0]
            .field("gen_ai.compaction.result_event_id")
            .is_none(),
        "result_event_id is set although no summary was produced"
    );
}

// parity: adapted: a Rust Summarizer cannot return content and an error together, so bothSummarizer returns the error only; the failed-span assertions are Go's
#[tokio::test]
async fn test_compaction_span_omits_result_when_summarizer_also_errors() {
    let sess = StaticSession::new(two_invocations());
    let cfg = cfg_with(2, 0, Arc::new(bothSummarizer));
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    assert!(
        got.is_err(),
        "slidingWindowStored() succeeded, want an error"
    );
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1);
    assert!(is_error(&spans[0]), "span status is not error");
    for key in [
        "gen_ai.compaction.result_event_id",
        "gen_ai.compaction.start_timestamp",
        "gen_ai.compaction.end_timestamp",
    ] {
        assert!(
            spans[0].field(key).is_none(),
            "{key} = {:?} on a failed compaction span, want it omitted",
            spans[0].field(key)
        );
    }
}

#[tokio::test]
async fn test_compaction_span_carries_invocation_and_usage() {
    let sess = StaticSession::new(two_invocations());
    let cfg = cfg_with(
        2,
        0,
        Arc::new(usageSummarizer {
            inner: fakeSummarizer::new("SUM"),
            prompt: 1234,
            output: 56,
        }),
    );
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    got.unwrap();
    let spans = compaction_spans(spans);
    let a = &spans[0];
    assert_eq!(
        a.field("gcp.vertex.agent.invocation_id").as_deref(),
        Some("inv2"),
        "invocation_id, want the turn that triggered compaction"
    );
    assert_eq!(
        a.field("gen_ai.usage.input_tokens").as_deref(),
        Some("1234")
    );
    assert_eq!(a.field("gen_ai.usage.output_tokens").as_deref(), Some("56"));
}

#[tokio::test]
async fn test_compaction_span_omits_absent_timestamps() {
    // Only the oldest event is unstamped, so the window still has a real upper bound.
    let mut first = text_event("a", "inv1", 1, "q1");
    first.timestamp = zero_time();
    let sess = StaticSession::new(vec![
        first,
        model_text_event("b", "inv1", 2, "a1"),
        text_event("c", "inv2", 3, "q2"),
        model_text_event("d", "inv2", 4, "a2"),
    ]);
    let cfg = cfg_with(2, 0, fakeSummarizer::new("SUM"));
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    got.unwrap();
    let spans = compaction_spans(spans);
    let a = &spans[0];
    assert!(
        a.field("gen_ai.compaction.start_timestamp").is_none(),
        "start_timestamp = {:?}, want the key to be absent for an unset bound",
        a.field("gen_ai.compaction.start_timestamp")
    );
    assert!(
        a.field("gen_ai.compaction.end_timestamp").is_some(),
        "end_timestamp is missing, want the bound that was recorded"
    );
}

#[tokio::test]
async fn test_compaction_span_reports_a_discarded_summary() {
    let sess = StaticSession::new(two_invocations());
    let cfg = cfg_with(2, 0, fakeSummarizer::new("SUM"));
    let ((), spans) = capture_spans(async {
        let (summary, finish) =
            sliding_window(&Platform::default(), &cfg, Some(&*sess as &dyn Session), "")
                .await
                .unwrap();
        assert!(summary.is_some(), "SlidingWindow() produced no summary");
        // The caller decides not to keep it.
        finish.finish(
            None,
            "another compaction covering the same events landed while summarizing",
        );
    })
    .await;
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1);
    let declined = spans[0]
        .field("gen_ai.compaction.declined")
        .expect("the span does not say the summary was discarded");
    assert!(!declined.is_empty(), "the discard reason is empty");
    assert!(
        spans[0]
            .field("gen_ai.compaction.result_event_id")
            .is_none(),
        "the span names a result event, but nothing reached the session"
    );
}

// parity: adapted: the OTel exception event becomes the error event recorded on the span (otel.status_description)
#[tokio::test]
async fn test_compaction_span_records_a_panic_as_an_exception() {
    let sess = StaticSession::new(two_invocations());
    let cfg = cfg_with(2, 0, Arc::new(panickingSummarizer));
    let (_, spans) = capture_spans(
        AssertUnwindSafe(sliding_window(
            &Platform::default(),
            &cfg,
            Some(&*sess as &dyn Session),
            "",
        ))
        .catch_unwind(),
    )
    .await;
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1);
    assert_eq!(
        spans[0].field("otel.status_description").as_deref(),
        Some("summarizer panicked: summarizer exploded"),
        "no exception event was recorded"
    );
}

#[tokio::test]
async fn test_declined_summarization_reports_what_it_spent() {
    let sess = StaticSession::new(two_invocations());
    let usage = Usage {
        prompt_token_count: 120,
        candidates_token_count: 15,
        ..Default::default()
    };
    let cfg = cfg_with(2, 1, Arc::new(usageReportingDecliner { usage }));
    let (got, spans) =
        capture_spans(sliding_window_stored(&cfg, Some(&*sess as &dyn Session))).await;
    got.unwrap();
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1);
    assert!(
        spans[0].field("gen_ai.compaction.declined").is_some(),
        "a decline left no marker on the span"
    );
    assert!(
        spans[0].field("gen_ai.usage.input_tokens").is_some(),
        "the span records no input tokens for a decline that reported usage"
    );
    assert!(
        spans[0].field("gen_ai.usage.output_tokens").is_some(),
        "the span records no output tokens for a decline that reported usage"
    );
}

/// Go withUsage: tags an event with an observed prompt token count.
fn with_usage(mut ev: Event, prompt_tokens: i32) -> Event {
    ev.response.usage_metadata = Some(Usage {
        prompt_token_count: prompt_tokens,
        ..Default::default()
    });
    ev
}

fn tail_cfg(token_threshold: i32, event_retention_size: i32, summary: &str) -> Config {
    Config {
        token_threshold,
        event_retention_size,
        summarizer: Some(fakeSummarizer::new(summary)),
        ..Default::default()
    }
}

#[tokio::test]
async fn test_tail_retention_emits_span() {
    let sess = StaticSession::new(vec![
        text_event("a", "inv1", 1, "q1"),
        with_usage(model_text_event("b", "inv1", 2, "a1"), 900),
    ]);
    let cfg = tail_cfg(100, 0, "sum");
    let (res, spans) = capture_spans(tail_retention_stored(
        &cfg,
        Some(&*sess as &dyn Session),
        &TurnScope::default(),
        None,
        None,
    ))
    .await;
    res.expect("tail_retention_stored");
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1, "got {} spans, want 1", spans.len());
    assert_eq!(spans[0].otel_name, "compact_events token_threshold");
    assert_eq!(
        spans[0]
            .field("gen_ai.compaction.token_threshold")
            .as_deref(),
        Some("100")
    );
    assert!(
        spans[0]
            .field("gen_ai.compaction.compaction_interval")
            .is_none(),
        "compaction_interval attribute is present on a tail-retention span, want it omitted"
    );
}

#[tokio::test]
async fn test_compaction_span_records_tail_retention_thresholds() {
    let sess = StaticSession::new(two_invocations());
    let cfg = tail_cfg(10, 1, "SUM");
    let est: &TokenCounter = &|_: &[Event]| 1000;
    let (res, spans) = capture_spans(tail_retention_stored(
        &cfg,
        Some(&*sess as &dyn Session),
        &TurnScope::default(),
        Some(est),
        None,
    ))
    .await;
    res.expect("tail_retention_stored");
    let spans = compaction_spans(spans);
    assert_eq!(spans.len(), 1, "got {} spans, want 1", spans.len());
    assert_eq!(
        spans[0]
            .field("gen_ai.compaction.token_threshold")
            .as_deref(),
        Some("10")
    );
    assert_eq!(
        spans[0]
            .field("gen_ai.compaction.event_retention_size")
            .as_deref(),
        Some("1")
    );
    // The knobs of the strategy that is not configured stay off the span.
    assert!(
        spans[0]
            .field("gen_ai.compaction.compaction_interval")
            .is_none(),
        "compaction_interval is present on a tail-retention span, want it omitted"
    );
}

#[tokio::test]
async fn test_compaction_span_records_a_decline() {
    // Threshold crossed, but the retained tail is the entire history.
    let sess = StaticSession::new(vec![
        text_event("a", "inv1", 1, "q1"),
        model_text_event("b", "inv1", 2, "a1"),
    ]);
    let cfg = tail_cfg(10, 50, "SUM");
    let est: &TokenCounter = &|_: &[Event]| 1000;
    let (res, spans) = capture_spans(tail_retention_stored(
        &cfg,
        Some(&*sess as &dyn Session),
        &TurnScope::default(),
        Some(est),
        None,
    ))
    .await;
    let got = res.expect("tail_retention_stored");
    assert!(
        got.is_none(),
        "tail_retention_stored() = {got:?}, want None"
    );
    let spans = compaction_spans(spans);
    assert_eq!(
        spans.len(),
        1,
        "got {} spans for a declined compaction, want 1",
        spans.len()
    );
    let reason = spans[0]
        .field("gen_ai.compaction.declined")
        .expect("the span does not say it declined");
    assert!(!reason.is_empty(), "the decline reason is empty");
    assert_eq!(
        spans[0].field("gen_ai.compaction.event_count").as_deref(),
        Some("0"),
        "event_count on a declined compaction, want 0"
    );
}