gadk-engine 0.5.21

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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
// 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/llminternal/contents_processor_test.go`, including the
//! package-shared test helpers it defines (`testModel`, `fakeSession`, `wantWithContinuation`).

use std::any::Any;
use std::sync::{Arc, Mutex};

use chrono::{DateTime, Utc};
use gadk_agent::{
    Agent, AgentConfig, AgentError, ContextBuilder, InvocationContext, Mode,
    REQUEST_CONFIRMATION_FUNCTION_CALL_NAME, new_agent,
};
use gadk_genai::{
    Blob, Content, FunctionCall, FunctionResponse, Part, ROLE_MODEL, ROLE_USER, Transcription,
};
use gadk_model::{Llm, LlmRequest, LlmResponse};
use gadk_session::{Event, Session, SessionError, State, ToolConfirmation, zero_time};
use pretty_assertions::assert_eq;
use serde_json::{Map, Value, json};

use crate::llm::flow::Flow;
use crate::llm::processors::contents::{
    SINGLE_TURN_NUDGE, contents_request_processor, convert_foreign_event,
};
use crate::llm::processors::go_json_marshal;
use crate::llm::{IncludeContents, LlmAgentConfig};
use crate::testutil::MockModel;

/// Go contents_processor.go:109 continuation text (used by the Go test helper wantWithContinuation).
pub(super) const CONTINUATION: &str = "Continue processing previous requests as instructed. Exit or provide a summary if no more outputs are needed.";

/// Port of Go `testModel`: an `LLM` the request processors never call.
pub(super) fn test_model() -> Arc<dyn Llm> {
    MockModel::new(vec![])
}

/// Port of Go `fakeSession`: a session whose events are fixed by the test.
pub(super) struct FakeSession {
    events: Vec<Event>,
    state: FakeState,
}

impl FakeSession {
    pub(super) fn shared(events: Vec<Event>) -> Arc<dyn Session> {
        Arc::new(Self {
            events,
            state: FakeState::default(),
        })
    }
}

/// Go's `fakeSession.State()` returns nil; Rust needs a `&dyn State`, so an empty map stands in.
#[derive(Default)]
pub(super) struct FakeState(Mutex<Map<String, Value>>);

impl State for FakeState {
    fn get(&self, key: &str) -> Result<Value, SessionError> {
        self.0
            .lock()
            .unwrap()
            .get(key)
            .cloned()
            .ok_or(SessionError::StateKeyNotExist)
    }
    fn set(&self, key: &str, value: Value) -> Result<(), SessionError> {
        self.0.lock().unwrap().insert(key.to_owned(), value);
        Ok(())
    }
    fn all(&self) -> Vec<(String, Value)> {
        self.0
            .lock()
            .unwrap()
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    }
}

impl Session for FakeSession {
    fn id(&self) -> &str {
        "test_session"
    }
    fn app_name(&self) -> &str {
        "test_app"
    }
    fn user_id(&self) -> &str {
        "test_user"
    }
    fn state(&self) -> &dyn State {
        &self.state
    }
    fn events(&self) -> Vec<Event> {
        self.events.clone()
    }
    fn last_update_time(&self) -> DateTime<Utc> {
        zero_time()
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Port of `utils.Must(llmagent.New(cfg))` with `Model: &testModel{}`.
pub(super) fn llm_agent(cfg: LlmAgentConfig) -> Arc<dyn Agent> {
    crate::llm::agent::new(LlmAgentConfig {
        model: Some(test_model()),
        ..cfg
    })
    .unwrap()
}

/// `icontext.InvocationContextParams{Agent, Session: &fakeSession{events}}`; callers add
/// `.branch(..)`, `.isolation_scope(..)` or `.user_content(..)` where Go sets those params.
pub(super) fn ctx_builder(agent: &Arc<dyn Agent>, events: Vec<Event>) -> ContextBuilder {
    InvocationContext::builder()
        .session(FakeSession::shared(events))
        .agent(agent.clone())
}

/// Go's loop over `llminternal.ContentsRequestProcessor(ctx, req, &llminternal.Flow{})`:
/// fails on any yielded event, returns `req.Contents`.
pub(super) async fn run_contents(ctx: &InvocationContext) -> Result<Vec<Content>, AgentError> {
    let mut req = LlmRequest::default();
    let events = contents_request_processor(ctx, &mut req, &Flow::default()).await?;
    assert!(
        events.is_empty(),
        "ContentsRequestProcessor generated an unexpected event"
    );
    Ok(req.contents)
}

/// An event authored by `author` carrying `content` (Go `&session.Event{Author, LLMResponse{Content}}`).
pub(super) fn ev(author: &str, content: Content) -> Event {
    Event {
        author: author.into(),
        response: LlmResponse {
            content: Some(content),
            ..Default::default()
        },
        ..Default::default()
    }
}

pub(super) fn text(t: &str, role: &str) -> Content {
    Content::from_text(t, role)
}

/// Port of Go `wantWithContinuation`.
pub(super) fn with_continuation(mut want: Vec<Content>) -> Vec<Content> {
    if want.last().is_some_and(|last| last.role != ROLE_USER) {
        want.push(Content::from_text(CONTINUATION, ROLE_USER));
    }
    want
}

/// Go `genai.NewContentFromFunctionCall(name, nil, role)`.
fn fc(name: &str, role: &str) -> Content {
    Content::from_function_call(
        FunctionCall {
            name: name.into(),
            ..Default::default()
        },
        role,
    )
}

/// Go `genai.NewContentFromFunctionResponse(name, nil, role)`.
fn fr(name: &str, role: &str) -> Content {
    Content::from_function_response(
        FunctionResponse {
            name: name.into(),
            ..Default::default()
        },
        role,
    )
}

/// The "For context:" rewrite of a foreign event (Go convertForeignEvent output).
fn for_context(line: &str) -> Content {
    Content::from_parts(
        vec![Part::from_text("For context:"), Part::from_text(line)],
        ROLE_USER,
    )
}

// parity: adapted: Rust renders empty maps as {} (real Go/Gemini wire is non-nil; nil vs empty unrepresentable); the agentTransfer foreign FC/FR lines assert {} where Go asserts null
#[tokio::test]
async fn test_contents_request_processor_include_contents() {
    const AGENT_NAME: &str = "testAgent";
    let hello_and_good_bye = vec![
        ev("user", text("hello", ROLE_USER)),
        ev("user", text("good bye", ROLE_USER)),
    ];
    let agent_transfer = vec![
        ev("anotherAgent", fc("func1", ROLE_MODEL)),
        ev("anotherAgent", fr("func1", ROLE_USER)),
        ev("anotherAgent", text("transfer to testAgent", ROLE_MODEL)),
        ev(AGENT_NAME, fc("func1", ROLE_MODEL)),
    ];
    let robot = vec![
        ev(AGENT_NAME, text("do func1", ROLE_USER)),
        ev(AGENT_NAME, fc("func1", ROLE_MODEL)),
        ev(AGENT_NAME, fr("func1", ROLE_USER)),
    ];
    let cases: Vec<(&str, IncludeContents, Vec<Event>, Vec<Content>)> = vec![
        ("empty", IncludeContents::Default, vec![], vec![]),
        ("empty", IncludeContents::None, vec![], vec![]),
        (
            "helloAndGoodBye",
            IncludeContents::Unset,
            hello_and_good_bye.clone(),
            vec![text("hello", ROLE_USER), text("good bye", ROLE_USER)],
        ),
        // Go: "same as \"\" here: nothing is bound, so there is no placement to opt out of".
        (
            "helloAndGoodBye",
            IncludeContents::Default,
            hello_and_good_bye.clone(),
            vec![text("hello", ROLE_USER), text("good bye", ROLE_USER)],
        ),
        (
            "helloAndGoodBye",
            IncludeContents::None,
            hello_and_good_bye.clone(),
            vec![text("good bye", ROLE_USER)],
        ),
        (
            "agentTransfer",
            IncludeContents::Unset,
            agent_transfer.clone(),
            vec![
                // Rendered as {} (see the adapted note on this fn).
                for_context("[anotherAgent] called tool `func1` with parameters: {}"),
                for_context("[anotherAgent] `func1` tool returned result: {}"),
                for_context("[anotherAgent] said: transfer to testAgent"),
                fc("func1", ROLE_MODEL),
            ],
        ),
        (
            "agentTransfer",
            IncludeContents::None,
            agent_transfer.clone(),
            vec![
                for_context("[anotherAgent] said: transfer to testAgent"),
                fc("func1", ROLE_MODEL),
            ],
        ),
        (
            "robot",
            IncludeContents::Default,
            robot.clone(),
            vec![
                text("do func1", ROLE_USER),
                fc("func1", ROLE_MODEL),
                fr("func1", ROLE_USER),
            ],
        ),
        (
            "robot",
            IncludeContents::None,
            robot.clone(),
            vec![
                text("do func1", ROLE_USER),
                fc("func1", ROLE_MODEL),
                fr("func1", ROLE_USER),
            ],
        ),
    ];
    for (name, include, events, want) in cases {
        let label = format!("{name}/include_contents={}", include.as_str());
        let agent = llm_agent(LlmAgentConfig {
            name: AGENT_NAME.into(),
            include_contents: include,
            ..Default::default()
        });
        let ctx = ctx_builder(&agent, events).build_invocation();
        let got = run_contents(&ctx)
            .await
            .unwrap_or_else(|e| panic!("{label}: contentRequestProcessor failed: {e}"));
        assert_eq!(with_continuation(want), got, "{label}");
    }
}

#[tokio::test]
async fn test_contents_request_processor_include_contents_none_isolation_scope_pivot() {
    let events = vec![
        ev("user", text("unscoped turn", ROLE_USER)),
        Event {
            isolation_scope: "task-1".into(),
            ..ev("user", text("scoped task", ROLE_USER))
        },
    ];
    let agent = llm_agent(LlmAgentConfig {
        name: "testAgent".into(),
        include_contents: IncludeContents::None,
        ..Default::default()
    });
    // Unscoped agent: the trailing scoped event must be skipped as the pivot, so the
    // unscoped user turn is what the agent sees.
    let ctx = ctx_builder(&agent, events).build_invocation();
    let got = run_contents(&ctx).await.unwrap();
    assert_eq!(
        with_continuation(vec![text("unscoped turn", ROLE_USER)]),
        got
    );
}

#[tokio::test]
async fn test_contents_request_processor_non_llm_agent() {
    let agent = new_agent(AgentConfig {
        name: "test_agent".into(),
        ..Default::default()
    })
    .unwrap();
    // Go passes no session; the builder's in-memory default session stands in.
    let ctx = InvocationContext::builder().agent(agent).build_invocation();
    let mut req = LlmRequest::default();
    let events = contents_request_processor(&ctx, &mut req, &Flow::default())
        .await
        .unwrap();
    assert!(
        events.is_empty(),
        "ContentsRequestProcessor generated an unexpected event"
    );
    // Go cmp.Diff(&model.LLMRequest{}, req): LlmRequest has no PartialEq, so every field is checked.
    assert!(req.model.is_empty(), "model: {:?}", req.model);
    assert!(req.contents.is_empty(), "contents: {:?}", req.contents);
    assert!(req.config.is_none(), "config was set");
    assert!(req.http_options.is_none(), "http_options was set");
    assert!(req.tools.is_empty(), "tools was set");
}

#[tokio::test]
async fn test_contents_request_processor_include_contents_none_ignores_sibling_branch_pivot() {
    const BRANCH: &str = "coordinator.translator_es";
    let events = vec![
        Event {
            branch: BRANCH.into(),
            ..ev("user", text("good morning", ROLE_USER))
        },
        Event {
            branch: "coordinator.translator_fr".into(),
            ..ev("translator_fr", text("Bonjour", ROLE_MODEL))
        },
    ];
    let agent = llm_agent(LlmAgentConfig {
        name: "translator_es".into(),
        mode: Mode::SingleTurn,
        include_contents: IncludeContents::None,
        ..Default::default()
    });
    let ctx = ctx_builder(&agent, events)
        .branch(BRANCH)
        .build_invocation();
    let got = run_contents(&ctx).await.unwrap();
    assert_eq!(vec![text("good morning", ROLE_USER)], got);
}

/// An event with nothing for the model to read, such as another agent's reply that carried
/// only grounding metadata or an error, cannot start the current turn: the user input before
/// it must stay in the request.
#[tokio::test]
async fn test_contents_request_processor_include_contents_none_skips_empty_pivot() {
    let cases = vec![
        (
            "grounding-only reply",
            LlmResponse {
                grounding_metadata: Some(gadk_genai::GroundingMetadata {
                    web_search_queries: vec!["X".into()],
                    ..Default::default()
                }),
                ..Default::default()
            },
        ),
        (
            "error reply",
            LlmResponse {
                error_code: "UNAVAILABLE".into(),
                error_message: "model unavailable".into(),
                ..Default::default()
            },
        ),
        (
            "reply with no parts",
            LlmResponse {
                content: Some(Content {
                    role: ROLE_MODEL.into(),
                    parts: vec![],
                }),
                ..Default::default()
            },
        ),
    ];
    for (name, reply) in cases {
        let events = vec![
            ev("user", text("Find X", ROLE_USER)),
            Event {
                author: "researcher".into(),
                response: reply,
                ..Default::default()
            },
        ];
        let summarizer = llm_agent(LlmAgentConfig {
            name: "summarizer".into(),
            include_contents: IncludeContents::None,
            ..Default::default()
        });
        let ctx = ctx_builder(&summarizer, events).build_invocation();
        let got = run_contents(&ctx)
            .await
            .unwrap_or_else(|e| panic!("{name}: ContentsRequestProcessor() error = {e}"));
        assert_eq!(vec![text("Find X", ROLE_USER)], got, "{name}");
    }
}

/// A tool result the caller posts back does not start the current turn. A long-running tool
/// is finished by running the agent again with its result as the new, user-authored message.
/// Anchoring there would cut the call the result answers out of the request. Mirrors
/// adk-python's `_is_submitted_tool_result` in `_get_current_turn_contents`.
#[tokio::test]
async fn test_contents_request_processor_include_contents_none_submitted_tool_result() {
    const AGENT_NAME: &str = "testAgent";
    let call_c =
        |id: &str| Content::from_parts(vec![fc_part(id, "long_job", json!({}))], ROLE_MODEL);
    let result_c = |id: &str, response: Value| {
        Content::from_parts(vec![fr_part(id, "long_job", response)], ROLE_USER)
    };
    let start = text("start the job", ROLE_USER);
    let call = call_c("call-1");
    let result = result_c("call-1", json!({"status": "done"}));
    let call_without_id = call_c("");
    let result_without_id = result_c("", json!({"status": "done"}));
    let orphan = result_c("no-such-call", json!({"status": "done"}));
    let question = text("is it done yet?", ROLE_USER);
    let not_yet = text("not yet", ROLE_MODEL);
    let earlier = text("hello", ROLE_USER);
    let earlier_reply = text("hi, what should I run?", ROLE_MODEL);
    // A tool that asks for confirmation answers with an error, the agent asks the user, and
    // the user's approval comes back as a function response.
    let confirmation_error = result_c("call-1", json!({"error": "confirmation required"}));
    let confirmation_request = Content::from_parts(
        vec![fc_part(
            "confirm-1",
            REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
            json!({}),
        )],
        ROLE_MODEL,
    );
    let approval = Content::from_parts(
        vec![fr_part(
            "confirm-1",
            REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
            json!({"confirmed": true}),
        )],
        ROLE_USER,
    );
    let e = |author: &str, c: &Content| {
        let mut out = ev(author, c.clone());
        if author == AGENT_NAME {
            out.long_running_tool_ids = c
                .parts
                .iter()
                .filter_map(|p| p.function_call.as_ref().map(|fc| fc.id.clone()))
                .collect();
        }
        out
    };
    let cases: Vec<(&str, Vec<Event>, Vec<Content>)> = vec![
        (
            "result right after the call",
            vec![e("user", &start), e(AGENT_NAME, &call), e("user", &result)],
            vec![start.clone(), call.clone(), result.clone()],
        ),
        (
            "user turn while the tool was pending",
            vec![
                e("user", &start),
                e(AGENT_NAME, &call),
                e("user", &question),
                e(AGENT_NAME, &not_yet),
                e("user", &result),
            ],
            vec![start.clone(), call.clone(), result.clone()],
        ),
        (
            // A finished turn ahead of the call: the anchor is the user message that led to
            // the call, not the first event.
            "earlier turn ahead of the call",
            vec![
                e("user", &earlier),
                e(AGENT_NAME, &earlier_reply),
                e("user", &start),
                e(AGENT_NAME, &call),
                e("user", &question),
                e(AGENT_NAME, &not_yet),
                e("user", &result),
            ],
            vec![start.clone(), call.clone(), result.clone()],
        ),
        (
            // Full-history mode pairs a call and a result that both lack an ID, so none mode
            // reaches back to such a call too.
            "call and result without IDs",
            vec![
                e("user", &start),
                e(AGENT_NAME, &call_without_id),
                e("user", &question),
                e(AGENT_NAME, &not_yet),
                e("user", &result_without_id),
            ],
            vec![
                start.clone(),
                call_without_id.clone(),
                result_without_id.clone(),
            ],
        ),
        (
            "tool resumed after the user confirms it",
            vec![
                e("user", &start),
                e(AGENT_NAME, &call),
                e(AGENT_NAME, &confirmation_error),
                e(AGENT_NAME, &confirmation_request),
                e("user", &approval),
                e(AGENT_NAME, &result),
            ],
            vec![start.clone(), call.clone(), result.clone()],
        ),
        (
            "result with no call stays out of the anchor search",
            vec![
                e("user", &start),
                e(AGENT_NAME, &call),
                e("user", &question),
                e("user", &orphan),
            ],
            vec![question.clone()],
        ),
    ];
    for (name, events, want) in cases {
        let agent = llm_agent(LlmAgentConfig {
            name: AGENT_NAME.into(),
            include_contents: IncludeContents::None,
            ..Default::default()
        });
        let ctx = ctx_builder(&agent, events).build_invocation();
        let got = run_contents(&ctx)
            .await
            .unwrap_or_else(|e| panic!("{name}: ContentsRequestProcessor() error = {e}"));
        assert_eq!(with_continuation(want), got, "{name}: contents");
    }
}

/// The parent's transfer_to_agent call and response do not start the current turn of the
/// agent it hands over to. Anchoring there would leave the user's request out of that agent's
/// prompt. Mirrors adk-python's `_is_direct_transfer` in `_get_current_turn_contents`. With
/// nothing earlier to start the turn, the latest hand-over still starts it.
#[tokio::test]
async fn test_contents_request_processor_include_contents_none_transferred_from_parent() {
    const PARENT_NAME: &str = "coordinator";
    const AGENT_NAME: &str = "geography";
    let question = text("What is the capital of France?", ROLE_USER);
    let transfer_call_part = fc_part(
        "call-1",
        "transfer_to_agent",
        json!({"agent_name": AGENT_NAME}),
    );
    let transfer_call = Content::from_parts(vec![transfer_call_part.clone()], ROLE_MODEL);
    let text_then_transfer_call = Content::from_parts(
        vec![Part::from_text("Handing over."), transfer_call_part],
        ROLE_MODEL,
    );
    let transfer_response = Content::from_parts(
        vec![fr_part("call-1", "transfer_to_agent", json!({}))],
        ROLE_USER,
    );
    let own_call = Content::from_parts(
        vec![fc_part("call-2", "lookup", json!({"country": "France"}))],
        ROLE_MODEL,
    );
    let own_response = Content::from_parts(
        vec![fr_part("call-2", "lookup", json!({"capital": "Paris"}))],
        ROLE_USER,
    );
    let user_event = ev("user", question.clone());
    let transfer_call_event = ev(PARENT_NAME, transfer_call);
    let transfer_response_event = {
        let mut out = ev(PARENT_NAME, transfer_response);
        out.actions.transfer_to_agent = AGENT_NAME.into();
        out
    };
    // An earlier turn ahead of the request. It stays out of the prompt, so a test passes only
    // if the turn starts at the request, not at the first event of the session.
    let earlier_answer_event = ev(AGENT_NAME, text("Madrid.", ROLE_MODEL));
    let earlier_turn = vec![
        ev("user", text("What is the capital of Spain?", ROLE_USER)),
        earlier_answer_event.clone(),
    ];
    let own_transfer_call = Content::from_parts(
        vec![fc_part(
            "call-4",
            "transfer_to_agent",
            json!({"agent_name": "history"}),
        )],
        ROLE_MODEL,
    );
    let own_transfer_error = Content::from_parts(
        vec![fr_part(
            "call-4",
            "transfer_to_agent",
            json!({"error": "agent history not found"}),
        )],
        ROLE_USER,
    );
    // A custom tool that hands over by setting Actions.TransferToAgent, with a call and
    // response named something other than transfer_to_agent.
    let handoff_call = Content::from_parts(
        vec![fc_part("call-3", "handoff", json!({"to": AGENT_NAME}))],
        ROLE_MODEL,
    );
    let handoff_response =
        Content::from_parts(vec![fr_part("call-3", "handoff", json!({}))], ROLE_USER);
    // The transfer itself stays in the prompt, as context from another agent.
    let transfer_call_context = for_context(
        r#"[coordinator] called tool `transfer_to_agent` with parameters: {"agent_name":"geography"}"#,
    );
    let transfer_response_context =
        for_context("[coordinator] `transfer_to_agent` tool returned result: {}");
    let with_earlier_turn = |events: Vec<Event>| {
        let mut out = earlier_turn.clone();
        out.extend(events);
        out
    };
    let router_event = {
        let mut out = ev(
            "router",
            Content::from_parts(
                vec![Part {
                    text: "Routing elsewhere.".into(),
                    thought: true,
                    ..Default::default()
                }],
                ROLE_MODEL,
            ),
        );
        out.actions.transfer_to_agent = "history".into();
        out
    };
    let handoff_response_event = {
        let mut out = ev(PARENT_NAME, handoff_response);
        out.actions.transfer_to_agent = AGENT_NAME.into();
        out
    };
    // (name, events, placed single turn, want)
    let cases: Vec<(&str, Vec<Event>, bool, Vec<Content>)> = vec![
        (
            "transfer is the latest event",
            with_earlier_turn(vec![
                user_event.clone(),
                transfer_call_event.clone(),
                transfer_response_event.clone(),
            ]),
            false,
            vec![
                question.clone(),
                transfer_call_context.clone(),
                transfer_response_context.clone(),
            ],
        ),
        (
            // The call is not the event's first part.
            "parent writes text before the transfer call",
            with_earlier_turn(vec![
                user_event.clone(),
                ev(PARENT_NAME, text_then_transfer_call),
                transfer_response_event.clone(),
            ]),
            false,
            vec![
                question.clone(),
                Content::from_parts(
                    vec![
                        Part::from_text("For context:"),
                        Part::from_text("[coordinator] said: Handing over."),
                        Part::from_text(
                            r#"[coordinator] called tool `transfer_to_agent` with parameters: {"agent_name":"geography"}"#,
                        ),
                    ],
                    ROLE_USER,
                ),
                transfer_response_context.clone(),
            ],
        ),
        (
            // The transfer is no longer the latest event on the agent's next model call,
            // after its own tool call.
            "agent's own tool call after the transfer",
            with_earlier_turn(vec![
                user_event.clone(),
                transfer_call_event.clone(),
                transfer_response_event.clone(),
                ev(AGENT_NAME, own_call.clone()),
                ev(AGENT_NAME, own_response.clone()),
            ]),
            false,
            vec![
                question.clone(),
                transfer_call_context.clone(),
                transfer_response_context.clone(),
                own_call.clone(),
                own_response.clone(),
            ],
        ),
        (
            // The session has no user event, so nothing ahead of the hand-over starts a turn.
            // The turn starts at the latest hand-over, as before hand-overs were skipped, so
            // the agent's own earlier answer stays out.
            "no user event in the session",
            vec![
                earlier_answer_event.clone(),
                transfer_call_event.clone(),
                transfer_response_event.clone(),
            ],
            false,
            vec![transfer_response_context.clone()],
        ),
        (
            // A later hand-over the agent cannot see, here one whose only part is a thought
            // that foreign conversion drops, is not the hand-over the turn falls back to. The
            // checks that hide an event from the agent run before the skip.
            "hand-over the agent cannot see after the hand-over",
            vec![
                earlier_answer_event.clone(),
                transfer_call_event.clone(),
                transfer_response_event.clone(),
                router_event,
            ],
            false,
            vec![transfer_response_context.clone()],
        ),
        (
            // The agent's own transfer_to_agent call never starts its turn, so the turn does
            // not fall back to it either. Here the agent named an unknown agent and is called
            // again with the error.
            "agent's own failed transfer after the hand-over",
            vec![
                earlier_answer_event.clone(),
                transfer_call_event.clone(),
                transfer_response_event.clone(),
                ev(AGENT_NAME, own_transfer_call.clone()),
                ev(AGENT_NAME, own_transfer_error.clone()),
            ],
            false,
            vec![
                transfer_response_context.clone(),
                own_transfer_call,
                own_transfer_error,
            ],
        ),
        (
            // The response is skipped through Actions.TransferToAgent alone. The call is not
            // named transfer_to_agent, so it starts the turn, as in adk-python.
            "custom tool hands over through Actions.TransferToAgent",
            with_earlier_turn(vec![
                user_event.clone(),
                ev(PARENT_NAME, handoff_call),
                handoff_response_event,
            ]),
            false,
            vec![
                for_context(
                    r#"[coordinator] called tool `handoff` with parameters: {"to":"geography"}"#,
                ),
                for_context("[coordinator] `handoff` tool returned result: {}"),
            ],
        ),
        (
            "single_turn placement with IncludeContents unset",
            with_earlier_turn(vec![
                user_event.clone(),
                transfer_call_event.clone(),
                transfer_response_event.clone(),
            ]),
            true,
            vec![
                question.clone(),
                transfer_call_context.clone(),
                transfer_response_context.clone(),
            ],
        ),
    ];
    for (name, events, placed_single_turn, want) in cases {
        let agent = llm_agent(LlmAgentConfig {
            name: AGENT_NAME.into(),
            include_contents: if placed_single_turn {
                IncludeContents::Unset
            } else {
                IncludeContents::None
            },
            ..Default::default()
        });
        let mut ctx = ctx_builder(&agent, events).build_invocation();
        if placed_single_turn {
            ctx = crate::llm::mode::with_bound_mode(
                &ctx,
                AGENT_NAME,
                agent.agent_state(),
                Mode::SingleTurn,
            );
        }
        let got = run_contents(&ctx)
            .await
            .unwrap_or_else(|e| panic!("{name}: ContentsRequestProcessor() error = {e}"));
        assert_eq!(want, got, "{name}: contents");
    }
}

#[tokio::test]
async fn test_contents_request_processor_strict_isolation_filter_excludes_foreign_scope() {
    const MY_SCOPE: &str = "task-mine";
    let events = vec![
        // Foreign-scoped user event: must be filtered out.
        Event {
            isolation_scope: "garbage-scope".into(),
            ..ev("user", text("foreign payload", ROLE_USER))
        },
        // In-scope user event: must reach the LLM.
        Event {
            isolation_scope: MY_SCOPE.into(),
            ..ev("user", text("my task brief", ROLE_USER))
        },
    ];
    let agent = llm_agent(LlmAgentConfig {
        name: "scopedAgent".into(),
        ..Default::default()
    });
    let ctx = ctx_builder(&agent, events)
        .isolation_scope(MY_SCOPE)
        .build_invocation();
    let got = run_contents(&ctx).await.unwrap();
    assert_eq!(
        with_continuation(vec![text("my task brief", ROLE_USER)]),
        got
    );
}

#[tokio::test]
async fn test_contents_request_processor_task_input_from_originating_fc() {
    const TASK_AGENT_NAME: &str = "taskAgent";
    const FC_ID: &str = "fc-1";
    let args = json!({"goal": "summarize"});
    let events = vec![
        ev(
            "coord",
            Content::from_parts(
                vec![Part {
                    function_call: Some(FunctionCall {
                        id: FC_ID.into(),
                        name: TASK_AGENT_NAME.into(),
                        args: args.as_object().cloned().unwrap(),
                        ..Default::default()
                    }),
                    ..Default::default()
                }],
                ROLE_MODEL,
            ),
        ),
        Event {
            isolation_scope: FC_ID.into(),
            ..ev(TASK_AGENT_NAME, text("working on it", ROLE_MODEL))
        },
    ];
    let args_json = go_json_marshal(&args);

    // task mode prepends FC args without nudge
    let task_agent = llm_agent(LlmAgentConfig {
        name: TASK_AGENT_NAME.into(),
        mode: Mode::Task,
        ..Default::default()
    });
    let ctx = ctx_builder(&task_agent, events.clone())
        .isolation_scope(FC_ID)
        .build_invocation();
    let got = run_contents(&ctx).await.unwrap();
    let want = vec![
        Content::from_parts(vec![Part::from_text(&args_json)], ROLE_USER),
        text("working on it", ROLE_MODEL),
    ];
    assert_eq!(
        with_continuation(want),
        got,
        "task mode prepends FC args without nudge"
    );

    // single_turn mode appends nudge after FC args
    let st_agent = llm_agent(LlmAgentConfig {
        name: TASK_AGENT_NAME.into(),
        mode: Mode::SingleTurn,
        include_contents: IncludeContents::None,
        ..Default::default()
    });
    let ctx = ctx_builder(&st_agent, events)
        .isolation_scope(FC_ID)
        .build_invocation();
    let got = run_contents(&ctx).await.unwrap();
    let want_leading = Content::from_parts(
        vec![
            Part::from_text(&args_json),
            Part::from_text(SINGLE_TURN_NUDGE),
        ],
        ROLE_USER,
    );
    assert!(!got.is_empty(), "expected at least one content; got none");
    assert_eq!(
        want_leading, got[0],
        "single_turn mode appends nudge after FC args"
    );
}

#[tokio::test]
async fn test_contents_request_processor_task_input_from_user_content_fallback() {
    const TASK_AGENT_NAME: &str = "taskAgent";
    const SCOPE: &str = "wf:node-1";
    let user_input = text("do the thing", ROLE_USER);
    // Session has no event carrying a FunctionCall with ID == scope.
    let events = vec![Event {
        isolation_scope: SCOPE.into(),
        ..ev(TASK_AGENT_NAME, text("ack", ROLE_MODEL))
    }];
    let task_agent = llm_agent(LlmAgentConfig {
        name: TASK_AGENT_NAME.into(),
        mode: Mode::Task,
        ..Default::default()
    });
    let ctx = ctx_builder(&task_agent, events)
        .isolation_scope(SCOPE)
        .user_content(user_input.clone())
        .build_invocation();
    let got = run_contents(&ctx).await.unwrap();
    let want = vec![
        Content::from_parts(user_input.parts.clone(), ROLE_USER),
        text("ack", ROLE_MODEL),
    ];
    assert_eq!(with_continuation(want), got);
}

fn obj(v: Value) -> Map<String, Value> {
    v.as_object().cloned().unwrap()
}

fn fc_part(id: &str, name: &str, args: Value) -> Part {
    Part {
        function_call: Some(FunctionCall {
            id: id.into(),
            name: name.into(),
            args: obj(args),
            ..Default::default()
        }),
        ..Default::default()
    }
}

fn fr_part(id: &str, name: &str, response: Value) -> Part {
    Part {
        function_response: Some(FunctionResponse {
            id: id.into(),
            name: name.into(),
            response: obj(response),
            ..Default::default()
        }),
        ..Default::default()
    }
}

/// A part referring to a file (Go `&genai.Part{FileData: &genai.FileData{FileURI, MIMEType}}`).
fn file_part(uri: &str, mime_type: &str) -> Part {
    Part {
        file_data: Some(gadk_genai::FileData {
            file_uri: uri.into(),
            mime_type: mime_type.into(),
            ..Default::default()
        }),
        ..Default::default()
    }
}

#[tokio::test]
async fn test_contents_request_processor() {
    const AGENT_NAME: &str = "testAgent";
    let transcription = |author: &str, input: bool, t: &str, finished: bool| {
        let tr = Some(Transcription {
            text: t.into(),
            finished,
            ..Default::default()
        });
        let response = if input {
            LlmResponse {
                input_transcription: tr,
                ..Default::default()
            }
        } else {
            LlmResponse {
                output_transcription: tr,
                ..Default::default()
            }
        };
        Event {
            author: author.into(),
            response,
            ..Default::default()
        }
    };
    let on_branch = |branch: &str, t: &str| Event {
        branch: branch.into(),
        ..ev("user", text(t, ROLE_USER))
    };
    let in_scope = |scope: &str, t: &str| Event {
        isolation_scope: scope.into(),
        ..ev("user", text(t, ROLE_USER))
    };
    // (name, branch, isolation_scope, events, want)
    type Case<'a> = (&'a str, &'a str, &'a str, Vec<Event>, Vec<Content>);
    let cases: Vec<Case> = vec![
        ("NilEvent", "", "", vec![], vec![]),
        ("EmptyEvents", "", "", vec![], vec![]),
        (
            "UserAndAgentEvents",
            "",
            "",
            vec![
                ev("user", text("Hello", ROLE_USER)),
                ev("testAgent", text("Hi there", ROLE_MODEL)),
            ],
            vec![text("Hello", ROLE_USER), text("Hi there", ROLE_MODEL)],
        ),
        (
            "anotherAgentEvent",
            "",
            "",
            vec![ev("anotherAgent", text("Foreign message", ROLE_MODEL))],
            vec![for_context("[anotherAgent] said: Foreign message")],
        ),
        (
            "FilterAdkLiveArtifacts",
            "",
            "",
            vec![ev(
                "user",
                Content::from_parts(
                    vec![
                        Part::from_text("Some text"),
                        file_part("artifact://app/user/session/_adk_live/audio.pcm#1", ""),
                    ],
                    ROLE_USER,
                ),
            )],
            vec![Content::from_parts(
                vec![Part::from_text("Some text")],
                ROLE_USER,
            )],
        ),
        (
            // The audio cache manager writes an event whose only part is the internal
            // reference, so the whole content has to go.
            "FilterAdkLiveArtifactOnlyContent",
            "",
            "",
            vec![
                ev("user", text("before", ROLE_USER)),
                ev(
                    "user",
                    Content::from_parts(
                        vec![file_part(
                            "artifact://app/user/session/_adk_live/adk_live_audio_storage_input_audio_1700000000000.pcm#0",
                            "audio/pcm",
                        )],
                        ROLE_USER,
                    ),
                ),
                ev("user", text("after", ROLE_USER)),
            ],
            vec![text("before", ROLE_USER), text("after", ROLE_USER)],
        ),
        (
            // With SaveLiveBlob on, the output-audio event is authored by the agent that
            // spoke, so another agent sees it as a foreign event. ConvertForeignEvent must
            // not turn it into a bare "For context:" turn: an event whose only parts are
            // internal live refs has to behave as if it were not in the session at all.
            "DropForeignAdkLiveArtifactOnlyContent",
            "",
            "",
            vec![
                ev("user", text("hi", ROLE_USER)),
                ev(
                    "otherAgent",
                    Content::from_parts(
                        vec![file_part(
                            "artifact://app/user/session/_adk_live/adk_live_audio_storage_output_audio_1700000000000.pcm#0",
                            "audio/pcm",
                        )],
                        ROLE_MODEL,
                    ),
                ),
            ],
            vec![text("hi", ROLE_USER)],
        ),
        (
            // A foreign event that also says something keeps the spoken part and loses only
            // the internal ref.
            "FilterForeignAdkLiveArtifactKeepsText",
            "",
            "",
            vec![ev(
                "otherAgent",
                Content::from_parts(
                    vec![
                        Part::from_text("here is the summary"),
                        file_part(
                            "artifact://app/user/session/_adk_live/adk_live_audio_storage_output_audio_1700000000000.pcm#0",
                            "audio/pcm",
                        ),
                    ],
                    ROLE_MODEL,
                ),
            )],
            vec![for_context("[otherAgent] said: here is the summary")],
        ),
        (
            // A user-visible artifact outside the _adk_live namespace stays.
            "KeepNonLiveArtifactRefs",
            "",
            "",
            vec![ev(
                "user",
                Content::from_parts(
                    vec![
                        Part::from_text("look at this"),
                        file_part(
                            "artifact://app/user/session/report.pdf#1",
                            "application/pdf",
                        ),
                        file_part("gs://bucket/recording.mp3", "audio/mp3"),
                    ],
                    ROLE_USER,
                ),
            )],
            vec![Content::from_parts(
                vec![
                    Part::from_text("look at this"),
                    file_part(
                        "artifact://app/user/session/report.pdf#1",
                        "application/pdf",
                    ),
                    file_part("gs://bucket/recording.mp3", "audio/mp3"),
                ],
                ROLE_USER,
            )],
        ),
        (
            "ExcludeToolConfirmation",
            "",
            "",
            vec![
                ev(
                    "AgentA",
                    Content::from_parts(
                        vec![fc_part(
                            "call_confirm_123",
                            REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
                            json!({"message": "Confirm delete?"}),
                        )],
                        ROLE_MODEL,
                    ),
                ),
                ev(
                    "user",
                    Content::from_parts(
                        vec![fr_part(
                            "call_confirm_123",
                            REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
                            json!({"confirmed": true}),
                        )],
                        ROLE_USER,
                    ),
                ),
            ],
            vec![],
        ),
        (
            "FilterByBranch",
            "branch1.task1",
            "",
            vec![
                on_branch("branch1", "In branch 1"),
                on_branch("branch1.task1", "In branch 1 and task 1"),
                on_branch("branch12", "In branch 12"),
                on_branch("branch2", "In branch 2"),
                on_branch("", "empty branch"),
            ],
            vec![
                text("In branch 1", ROLE_USER),
                text("In branch 1 and task 1", ROLE_USER),
                text("empty branch", ROLE_USER),
            ],
        ),
        // Isolation scope is exact-match (unlike branch, where empty is universally visible):
        // a scoped agent sees ONLY events with the same scope, not unscoped ones.
        (
            "FilterByIsolationScope_Scoped",
            "",
            "task-1",
            vec![
                in_scope("task-1", "in task 1"),
                in_scope("task-2", "in task 2"),
                in_scope("", "unscoped"),
            ],
            vec![text("in task 1", ROLE_USER)],
        ),
        // An unscoped agent (empty scope) sees ONLY unscoped events.
        (
            "FilterByIsolationScope_Unscoped",
            "",
            "",
            vec![in_scope("task-1", "in task 1"), in_scope("", "unscoped")],
            vec![text("unscoped", ROLE_USER)],
        ),
        (
            "AuthEvent",
            "",
            "",
            vec![ev(
                AGENT_NAME,
                Content::from_parts(
                    vec![Part {
                        function_call: Some(FunctionCall {
                            name: "adk_request_credential".into(),
                            ..Default::default()
                        }),
                        ..Default::default()
                    }],
                    ROLE_MODEL,
                ),
            )],
            vec![],
        ),
        (
            "EventWithoutContent",
            "",
            "",
            vec![Event {
                author: "user".into(),
                ..Default::default()
            }],
            vec![],
        ),
        (
            "TranscriptionAggregation",
            "",
            "",
            vec![
                transcription("user", true, "hello ", false),
                transcription("user", true, "world", true),
                transcription("testAgent", false, "hi ", false),
                transcription("testAgent", false, "there", true),
                transcription("user", true, "ok", true),
            ],
            vec![
                text("hello world", ROLE_USER),
                text("hi there", ROLE_MODEL),
                text("ok", ROLE_USER),
            ],
        ),
    ];
    for (name, branch, scope, events, want) in cases {
        let agent = llm_agent(LlmAgentConfig {
            name: "testAgent".into(),
            ..Default::default()
        });
        let ctx = ctx_builder(&agent, events)
            .branch(branch)
            .isolation_scope(scope)
            .build_invocation();
        let got = run_contents(&ctx)
            .await
            .unwrap_or_else(|e| panic!("{name}: contentRequestProcessor failed: {e}"));
        assert_eq!(with_continuation(want), got, "{name}");
    }
}

#[test]
fn test_convert_foreign_event() {
    let now = Utc::now();
    let foreign = |parts: Vec<Part>| Event {
        timestamp: now,
        author: "foreign".into(),
        branch: "b".into(),
        response: LlmResponse {
            content: Some(Content::from_parts(parts, ROLE_MODEL)),
            ..Default::default()
        },
        ..Default::default()
    };
    let converted = |line: &str| {
        Some(Event {
            timestamp: now,
            author: "user".into(),
            branch: "b".into(),
            response: LlmResponse {
                content: Some(for_context(line)),
                ..Default::default()
            },
            ..Default::default()
        })
    };
    let thought = |t: &str| Part {
        text: t.into(),
        thought: true,
        ..Default::default()
    };
    let cases: Vec<(&str, Event, Option<Event>)> = vec![
        (
            "Text",
            foreign(vec![Part::from_text("hello")]),
            converted("[foreign] said: hello"),
        ),
        (
            "FunctionCall",
            foreign(vec![fc_part("", "test", json!({"a": "b"}))]),
            converted("[foreign] called tool `test` with parameters: {\"a\":\"b\"}"),
        ),
        (
            "FunctionResponse",
            foreign(vec![fr_part("", "test", json!({"c": "d"}))]),
            converted("[foreign] `test` tool returned result: {\"c\":\"d\"}"),
        ),
        // A thought part (with text) must be excluded so another agent's private reasoning
        // never leaks into the current agent's context; the non-thought text is still presented.
        (
            "ThoughtWithTextExcluded",
            foreign(vec![
                thought("secret reasoning"),
                Part::from_text("the answer"),
            ]),
            converted("[foreign] said: the answer"),
        ),
        // An empty-text thought part must be dropped too.
        (
            "EmptyTextThoughtExcluded",
            foreign(vec![thought(""), Part::from_text("the answer")]),
            converted("[foreign] said: the answer"),
        ),
        // A foreign turn made up entirely of thoughts is dropped rather than emitting a bare header.
        (
            "ThoughtOnlyDropped",
            foreign(vec![thought("only reasoning")]),
            None,
        ),
        (
            "MultipleThoughtsOnlyDropped",
            foreign(vec![
                thought("internal reasoning"),
                thought("more thoughts"),
            ]),
            None,
        ),
    ];
    for (name, event, want) in cases {
        let got = convert_foreign_event(&event);
        assert_eq!(
            want, got,
            "{name}: converted event does not match the expected public content and metadata"
        );
    }
}

fn call(id: &str, name: &str, args: Value) -> FunctionCall {
    FunctionCall {
        id: id.into(),
        name: name.into(),
        args: obj(args),
        ..Default::default()
    }
}

fn resp(id: &str, name: &str, response: Value) -> FunctionResponse {
    FunctionResponse {
        id: id.into(),
        name: name.into(),
        response: obj(response),
        ..Default::default()
    }
}

/// Go `NewContentFromFunctionCall(fc, role)` (the test-file helper).
fn cfc(fc: &FunctionCall, role: &str) -> Content {
    Content::from_function_call(fc.clone(), role)
}

/// Go `NewContentFromFunctionResponse(fr, role)` (the test-file helper).
fn cfr(fr: &FunctionResponse, role: &str) -> Content {
    Content::from_function_response(fr.clone(), role)
}

fn pfc(fc: &FunctionCall) -> Part {
    Part {
        function_call: Some(fc.clone()),
        ..Default::default()
    }
}

fn pfr(fr: &FunctionResponse) -> Part {
    Part {
        function_response: Some(fr.clone()),
        ..Default::default()
    }
}

fn ptext(t: &str) -> Part {
    Part::from_text(t)
}

#[tokio::test]
async fn test_contents_request_processor_rearrange() {
    const A: &str = "test_agent";
    const U: &str = "user";
    const M: &str = ROLE_MODEL;

    // --- Reusable Test Data ---
    // Basic Call/Response
    let fc_basic = call("call_123", "search_tool", json!({"query": "test"}));
    let fr_basic = resp(
        "call_123",
        "search_tool",
        json!({"results": ["item1", "item2"]}),
    );

    // LRO Call/Responses
    let fc_lro = call(
        "long_call_123",
        "long_running_tool",
        json!({"task": "process"}),
    );
    let fr_lro_inter = resp(
        "long_call_123",
        "long_running_tool",
        json!({"status": "processing", "progress": 50}),
    );
    let fr_lro_final = resp(
        "long_call_123",
        "long_running_tool",
        json!({"status": "completed", "result": "done"}),
    );

    // Mixed LRO/Normal Calls/Responses
    let fc_lro_mixed = call(
        "lro_call_456",
        "long_running_tool",
        json!({"task": "analyze"}),
    );
    let fc_normal_mixed = call("normal_call_789", "search_tool", json!({"query": "test"}));
    let fr_lro_inter_mixed = resp(
        "lro_call_456",
        "long_running_tool",
        json!({"status": "processing", "progress": 25}),
    );
    let fr_normal_mixed = resp(
        "normal_call_789",
        "search_tool",
        json!({"results": ["item1", "item2"]}),
    );
    let fr_lro_final_mixed = resp(
        "lro_call_456",
        "long_running_tool",
        json!({"status": "completed", "analysis": "done"}),
    );

    // History LRO Call/Responses
    let fc_hist_lro = call(
        "history_call_123",
        "long_running_tool",
        json!({"task": "process"}),
    );
    let fr_hist_lro_inter = resp(
        "history_call_123",
        "long_running_tool",
        json!({"status": "processing", "progress": 50}),
    );
    let fr_hist_lro_final = resp(
        "history_call_123",
        "long_running_tool",
        json!({"status": "completed", "result": "done"}),
    );

    // History Mixed Call/Responses
    let fc_hist_lro_mixed = call(
        "history_lro_123",
        "long_running_tool",
        json!({"task": "analyze"}),
    );
    let fc_hist_normal_mixed = call(
        "history_normal_456",
        "search_tool",
        json!({"query": "data"}),
    );
    let fr_hist_lro_inter_mixed = resp(
        "history_lro_123",
        "long_running_tool",
        json!({"status": "processing", "progress": 30}),
    );
    let fr_hist_normal_mixed = resp(
        "history_normal_456",
        "search_tool",
        json!({"results": ["result1", "result2"]}),
    );
    let fr_hist_lro_final_mixed = resp(
        "history_lro_123",
        "long_running_tool",
        json!({"status": "completed", "analysis": "finished"}),
    );

    // Preserve Content Call/Responses
    let fc_preserve = call(
        "preserve_test",
        "long_running_tool",
        json!({"test": "value"}),
    );
    let fr_preserve_inter = resp(
        "preserve_test",
        "long_running_tool",
        json!({"status": "processing"}),
    );
    let fr_preserve_final = resp(
        "preserve_test",
        "long_running_tool",
        json!({"output": "preserved"}),
    );

    // Human-in-the-loop confirmation call/responses
    let hint = "Please approve or reject the tool call request_vacation_days().";
    let confirmation_args = |original: &FunctionCall| {
        json!({
            "originalFunctionCall": original,
            "toolConfirmation": ToolConfirmation { hint: hint.into(), confirmed: false, payload: None },
        })
    };
    let fc_hitl_approved = call(
        "hitl_approved_call",
        "request_vacation_days",
        json!({"days": 5, "user_id": "user-123"}),
    );
    let fr_hitl_approved_pending = resp(
        "hitl_approved_call",
        "request_vacation_days",
        json!({"status": "Manager approval is required.", "request_id": "req-1"}),
    );
    let fr_hitl_approved_final = resp(
        "hitl_approved_call",
        "request_vacation_days",
        json!({"status": "The time off request is accepted.", "days_approved": 5, "request_id": "req-1"}),
    );
    let fc_hitl_approval_request = call(
        "hitl_approved_confirmation",
        REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
        confirmation_args(&fc_hitl_approved),
    );
    let fr_hitl_approval_confirmed = resp(
        "hitl_approved_confirmation",
        REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
        json!({"confirmed": true}),
    );
    let fc_hitl_denied = call(
        "hitl_denied_call",
        "request_vacation_days",
        json!({"days": 10, "user_id": "user-123"}),
    );
    let fr_hitl_denied_pending = resp(
        "hitl_denied_call",
        "request_vacation_days",
        json!({"status": "Manager approval is required.", "request_id": "req-2"}),
    );
    let fr_hitl_denied_final = resp(
        "hitl_denied_call",
        "request_vacation_days",
        json!({"status": "The time off request is rejected.", "days_approved": 0, "request_id": "req-2"}),
    );
    let fc_hitl_denial_request = call(
        "hitl_denied_confirmation",
        REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
        confirmation_args(&fc_hitl_denied),
    );
    let fr_hitl_denial_rejected = resp(
        "hitl_denied_confirmation",
        REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
        json!({"confirmed": false}),
    );

    // Late async completion: a long-running call whose final response arrives as the latest
    // event, after an unrelated tool exchange.
    let fc_async_plan = call("async_plan_1", "plan_tool", json!({"feature": "Q"}));
    let fr_async_plan_ack = resp("async_plan_1", "plan_tool", json!({"status": "accepted"}));
    let fr_async_plan_final = resp(
        "async_plan_1",
        "plan_tool",
        json!({"status": "completed", "plan": "add the flag, then wire it up"}),
    );
    let fc_async_status = call("async_status_1", "status_tool", json!({"job": "plan"}));
    let fr_async_status = resp(
        "async_status_1",
        "status_tool",
        json!({"status": "running", "elapsed": "21s"}),
    );
    let fc_async_unanswered = call(
        "async_unanswered_1",
        "notify_tool",
        json!({"channel": "ops"}),
    );

    // Error Call/Response
    let fr_orphaned = resp(
        "no_matching_call",
        "orphaned_tool",
        json!({"error": "no matching call"}),
    );
    let fr_without_id = resp("", "legacy_tool", json!({"result": "ok"}));
    let fc_other = call("call_456", "other_tool", json!({"query": "other"}));
    let fr_other = resp("call_456", "other_tool", json!({"result": "other"}));

    let parts = |parts: Vec<Part>, role: &str| Content::from_parts(parts, role);
    let long_running = |e: Event, id: &str| Event {
        long_running_tool_ids: vec![id.to_owned()],
        ..e
    };
    let image = || Blob {
        mime_type: "image/png".into(),
        data: b"image".to_vec().into(),
        ..Default::default()
    };

    struct Case {
        name: &'static str,
        events: Vec<Event>,
        want: Vec<Content>,
        want_err: Option<&'static str>,
        include_contents: IncludeContents,
    }
    let case = |name: &'static str, events: Vec<Event>, want: Vec<Content>| Case {
        name,
        events,
        want,
        want_err: None,
        include_contents: IncludeContents::Unset,
    };

    // --- Test Cases ---
    let cases = vec![
        case("NilEvent", vec![], vec![]),
        case("EmptyEvents", vec![], vec![]),
        case(
            "EventWithoutContent",
            vec![Event {
                author: "user".into(),
                ..Default::default()
            }],
            vec![],
        ),
        case(
            "Basic function call no rearrangement",
            vec![
                ev(U, text("Search for test", U)),
                ev(A, cfc(&fc_basic, M)),
                ev(U, cfr(&fr_basic, U)),
            ],
            vec![
                text("Search for test", U),
                cfc(&fc_basic, M),
                cfr(&fr_basic, U),
            ],
        ),
        case(
            "Rearrangement with intermediate response",
            vec![
                ev(U, text("Run long process", U)),
                ev(A, cfc(&fc_lro, M)),
                ev(U, cfr(&fr_lro_inter, U)),
                ev(A, text("Still processing...", M)),
                ev(U, cfr(&fr_lro_final, U)),
            ],
            vec![
                text("Run long process", U),
                cfc(&fc_lro, M),
                cfr(&fr_lro_final, U),
            ],
        ),
        // The unrelated search exchange survives (the property #767 established) but sits
        // before the completion, so the freshest response is the final content.
        case(
            "Rearrangement preserves unrelated function events",
            vec![
                ev(U, text("Run long process and search", U)),
                ev(A, cfc(&fc_lro, M)),
                ev(U, cfr(&fr_lro_inter, U)),
                ev(A, cfc(&fc_basic, M)),
                ev(U, cfr(&fr_basic, U)),
                ev(U, cfr(&fr_lro_final, U)),
            ],
            vec![
                text("Run long process and search", U),
                cfc(&fc_basic, M),
                cfr(&fr_basic, U),
                cfc(&fc_lro, M),
                cfr(&fr_lro_final, U),
            ],
        ),
        // Regression test for #1181: the completion of a long-running tool must stay the final
        // content, not be dragged back next to its much earlier call while a stale status
        // exchange takes the tail.
        case(
            "Late async completion stays the final content",
            vec![
                ev(U, text("Plan how to add feature Q", U)),
                ev(A, cfc(&fc_async_plan, M)),
                ev(U, cfr(&fr_async_plan_ack, U)),
                ev(A, text("I dispatched the planning task.", M)),
                ev(U, text("What is the status?", U)),
                ev(A, cfc(&fc_async_status, M)),
                ev(U, cfr(&fr_async_status, U)),
                ev(A, text("Still running.", M)),
                ev(U, cfr(&fr_async_plan_final, U)),
            ],
            vec![
                text("Plan how to add feature Q", U),
                cfc(&fc_async_status, M),
                cfr(&fr_async_status, U),
                cfc(&fc_async_plan, M),
                cfr(&fr_async_plan_final, U),
            ],
        ),
        // A call that never receives a response (a pending long-running call listed in
        // LongRunningToolIDs) keeps its order relative to the events that stay put, which
        // leaves it ahead of the completed pair that moves to the tail.
        case(
            "Late async completion stays final with an unanswered call in history",
            vec![
                ev(U, text("Plan how to add feature Q", U)),
                ev(A, cfc(&fc_async_plan, M)),
                ev(U, cfr(&fr_async_plan_ack, U)),
                long_running(ev(A, cfc(&fc_async_unanswered, M)), &fc_async_unanswered.id),
                ev(U, cfr(&fr_async_plan_final, U)),
            ],
            vec![
                text("Plan how to add feature Q", U),
                cfc(&fc_async_unanswered, M),
                cfc(&fc_async_plan, M),
                cfr(&fr_async_plan_final, U),
            ],
        ),
        // The same history when the unanswered call is not pending: a turn interrupted before
        // the tool ran. The call is dropped.
        case(
            "Late async completion drops an interrupted call from history",
            vec![
                ev(U, text("Plan how to add feature Q", U)),
                ev(A, cfc(&fc_async_plan, M)),
                ev(U, cfr(&fr_async_plan_ack, U)),
                ev(A, cfc(&fc_async_unanswered, M)),
                ev(U, cfr(&fr_async_plan_final, U)),
            ],
            vec![
                text("Plan how to add feature Q", U),
                cfc(&fc_async_plan, M),
                cfr(&fr_async_plan_final, U),
            ],
        ),
        // A turn that ended between a call and its result, followed by a new user message.
        case(
            "Unanswered call at the end of a turn is dropped",
            vec![
                ev(U, text("search for test", U)),
                ev(A, cfc(&fc_async_unanswered, M)),
                ev(U, text("are you still there?", U)),
            ],
            vec![text("search for test", U), text("are you still there?", U)],
        ),
        case(
            "Unanswered call mid-history is dropped and the text around it kept",
            vec![
                ev(U, text("Plan how to add feature Q", U)),
                ev(A, cfc(&fc_async_status, M)),
                ev(U, cfr(&fr_async_status, U)),
                ev(
                    A,
                    parts(
                        vec![ptext("Notifying ops first."), pfc(&fc_async_unanswered)],
                        M,
                    ),
                ),
                ev(U, text("Cancel that", U)),
            ],
            vec![
                text("Plan how to add feature Q", U),
                cfc(&fc_async_status, M),
                cfr(&fr_async_status, U),
                text("Notifying ops first.", M),
                text("Cancel that", U),
            ],
        ),
        // Dropping the trailing call must not leave the async response as the last event, which
        // would let the latest-response rearrangement discard the text turns between it and its
        // call.
        case(
            "Trailing unanswered call after an async completion keeps the interleaved turns",
            vec![
                ev(U, text("Plan how to add feature Q", U)),
                ev(A, cfc(&fc_async_plan, M)),
                ev(U, text("While waiting, tell me a joke", U)),
                ev(A, text("Why did the chicken cross the road?", M)),
                ev(U, cfr(&fr_async_plan_final, U)),
                ev(A, cfc(&fc_async_unanswered, M)),
            ],
            vec![
                text("Plan how to add feature Q", U),
                cfc(&fc_async_plan, M),
                cfr(&fr_async_plan_final, U),
                text("While waiting, tell me a joke", U),
                text("Why did the chicken cross the road?", M),
            ],
        ),
        case(
            "Pending long-running call stays in history",
            vec![
                ev(U, text("Plan how to add feature Q", U)),
                long_running(ev(A, cfc(&fc_async_unanswered, M)), &fc_async_unanswered.id),
                ev(U, text("Any update?", U)),
            ],
            vec![
                text("Plan how to add feature Q", U),
                cfc(&fc_async_unanswered, M),
                text("Any update?", U),
            ],
        ),
        case(
            "HITL confirmation approved preserves resumed tool response",
            vec![
                ev(U, text("Request five vacation days", U)),
                ev(A, cfc(&fc_hitl_approved, M)),
                ev(A, cfr(&fr_hitl_approved_pending, U)),
                ev(A, cfc(&fc_hitl_approval_request, M)),
                ev(U, cfr(&fr_hitl_approval_confirmed, U)),
                ev(A, cfr(&fr_hitl_approved_final, U)),
            ],
            vec![
                text("Request five vacation days", U),
                cfc(&fc_hitl_approved, M),
                cfr(&fr_hitl_approved_final, U),
            ],
        ),
        case(
            "HITL confirmation denied preserves rejected tool response",
            vec![
                ev(U, text("Request ten vacation days", U)),
                ev(A, cfc(&fc_hitl_denied, M)),
                ev(A, cfr(&fr_hitl_denied_pending, U)),
                ev(A, cfc(&fc_hitl_denial_request, M)),
                ev(U, cfr(&fr_hitl_denial_rejected, U)),
                ev(A, cfr(&fr_hitl_denied_final, U)),
            ],
            vec![
                text("Request ten vacation days", U),
                cfc(&fc_hitl_denied, M),
                cfr(&fr_hitl_denied_final, U),
            ],
        ),
        case(
            "Rearrangement with mixed LRO and normal calls",
            vec![
                ev(U, text("Analyze data and search for info", U)),
                ev(A, parts(vec![pfc(&fc_lro_mixed), pfc(&fc_normal_mixed)], M)),
                ev(
                    U,
                    parts(vec![pfr(&fr_lro_inter_mixed), pfr(&fr_normal_mixed)], U),
                ),
                ev(A, text("Analysis in progress, search completed", M)),
                ev(U, cfr(&fr_lro_final_mixed, U)),
            ],
            vec![
                text("Analyze data and search for info", U),
                parts(vec![pfc(&fc_lro_mixed), pfc(&fc_normal_mixed)], M),
                parts(vec![pfr(&fr_lro_final_mixed), pfr(&fr_normal_mixed)], U),
            ],
        ),
        case(
            "Rearrangement in history (non-final event)",
            vec![
                ev(U, text("Start long process", U)),
                ev(A, cfc(&fc_hist_lro, M)),
                ev(U, cfr(&fr_hist_lro_inter, U)),
                ev(A, text("Still processing...", M)),
                ev(U, cfr(&fr_hist_lro_final, U)),
                ev(A, text("Process completed successfully!", M)),
                ev(U, text("Great! What's next?", U)),
            ],
            vec![
                text("Start long process", U),
                cfc(&fc_hist_lro, M),
                cfr(&fr_hist_lro_final, U),
                text("Still processing...", M),
                text("Process completed successfully!", M),
                text("Great! What's next?", U),
            ],
        ),
        case(
            "Mixed rearrangement in history (non-final event)",
            vec![
                ev(U, text("Analyze and search simultaneously", U)),
                ev(
                    A,
                    parts(vec![pfc(&fc_hist_lro_mixed), pfc(&fc_hist_normal_mixed)], M),
                ),
                ev(
                    U,
                    parts(
                        vec![pfr(&fr_hist_lro_inter_mixed), pfr(&fr_hist_normal_mixed)],
                        U,
                    ),
                ),
                ev(A, text("Analysis continuing, search done", M)),
                ev(U, cfr(&fr_hist_lro_final_mixed, U)),
                ev(A, text("Both tasks completed successfully!", M)),
                ev(U, text("Perfect! What should we do next?", U)),
            ],
            vec![
                text("Analyze and search simultaneously", U),
                parts(vec![pfc(&fc_hist_lro_mixed), pfc(&fc_hist_normal_mixed)], M),
                parts(
                    vec![pfr(&fr_hist_lro_final_mixed), pfr(&fr_hist_normal_mixed)],
                    U,
                ),
                text("Analysis continuing, search done", M),
                text("Both tasks completed successfully!", M),
                text("Perfect! What should we do next?", U),
            ],
        ),
        case(
            "Rearrangement preserves mixed text parts",
            vec![
                ev(U, text("Before function call", U)),
                ev(
                    A,
                    parts(
                        vec![ptext("I'll process this for you"), pfc(&fc_preserve)],
                        M,
                    ),
                ),
                ev(
                    U,
                    parts(
                        vec![
                            ptext("Intermediate prefix"),
                            pfr(&fr_preserve_inter),
                            ptext("Processing..."),
                        ],
                        U,
                    ),
                ),
                ev(A, text("Still working on it...", M)),
                ev(
                    U,
                    parts(
                        vec![
                            ptext("Final prefix"),
                            pfr(&fr_preserve_final),
                            ptext("Final suffix"),
                        ],
                        U,
                    ),
                ),
            ],
            vec![
                text("Before function call", U),
                parts(
                    vec![ptext("I'll process this for you"), pfc(&fc_preserve)],
                    M,
                ),
                parts(
                    vec![
                        ptext("Intermediate prefix"),
                        pfr(&fr_preserve_final),
                        ptext("Processing..."),
                        ptext("Final prefix"),
                        ptext("Final suffix"),
                    ],
                    U,
                ),
            ],
        ),
        case(
            "Orphaned function response without matching call is dropped",
            vec![
                ev(U, parts(vec![ptext("Regular message")], U)),
                ev(U, parts(vec![pfr(&fr_orphaned)], U)),
            ],
            vec![text("Regular message", U)],
        ),
        case(
            "Orphaned function response after text reply is dropped",
            vec![
                ev(U, text("Hello", U)),
                ev(A, text("Hi there", M)),
                ev(U, parts(vec![pfr(&fr_orphaned)], U)),
            ],
            vec![text("Hello", U), text("Hi there", M)],
        ),
        case(
            "Orphaned function response is removed from mixed content",
            vec![
                ev(U, text("Hello", U)),
                ev(A, text("Hi there", M)),
                ev(
                    U,
                    parts(vec![ptext("What is the weather?"), pfr(&fr_orphaned)], U),
                ),
            ],
            vec![
                text("Hello", U),
                text("Hi there", M),
                text("What is the weather?", U),
            ],
        ),
        case(
            "Orphaned function response sharing a part preserves text",
            vec![
                ev(U, text("Hello", U)),
                ev(A, text("Hi", M)),
                ev(
                    U,
                    parts(
                        vec![Part {
                            function_response: Some(fr_orphaned.clone()),
                            ..ptext("What is the weather?")
                        }],
                        U,
                    ),
                ),
            ],
            vec![
                text("Hello", U),
                text("Hi", M),
                text("What is the weather?", U),
            ],
        ),
        case(
            "Rearrangement preserves text sharing an orphaned response part",
            vec![
                ev(A, cfc(&fc_basic, M)),
                ev(
                    U,
                    parts(
                        vec![Part {
                            function_response: Some(fr_orphaned.clone()),
                            ..ptext("note")
                        }],
                        U,
                    ),
                ),
                ev(U, cfr(&fr_basic, U)),
            ],
            vec![text("note", U), cfc(&fc_basic, M), cfr(&fr_basic, U)],
        ),
        case(
            "Rearrangement preserves separate text beside an orphaned response",
            vec![
                ev(A, cfc(&fc_basic, M)),
                // Ordinary text without an orphaned response still follows the existing
                // intermediate-event filtering rule.
                ev(U, text("plain note", U)),
                ev(U, parts(vec![ptext("note"), pfr(&fr_orphaned)], U)),
                ev(U, cfr(&fr_basic, U)),
            ],
            vec![text("note", U), cfc(&fc_basic, M), cfr(&fr_basic, U)],
        ),
        // The latest call/response pair stays last, so retaining model text does not trigger a
        // synthetic user continuation.
        case(
            "Rearrangement preserves model orphan remnant without continuation",
            vec![
                ev(A, cfc(&fc_basic, M)),
                ev(
                    A,
                    parts(
                        vec![Part {
                            function_response: Some(fr_orphaned.clone()),
                            ..ptext("note")
                        }],
                        M,
                    ),
                ),
                ev(U, cfr(&fr_basic, U)),
            ],
            vec![text("note", M), cfc(&fc_basic, M), cfr(&fr_basic, U)],
        ),
        case(
            "Rearrangement preserves orphan remnants around unrelated tools",
            vec![
                ev(A, cfc(&fc_lro, M)),
                ev(U, cfr(&fr_lro_inter, U)),
                ev(A, text("Still processing...", M)),
                ev(
                    U,
                    parts(
                        vec![Part {
                            function_response: Some(fr_orphaned.clone()),
                            ..ptext("first note")
                        }],
                        U,
                    ),
                ),
                ev(A, cfc(&fc_basic, M)),
                ev(U, cfr(&fr_basic, U)),
                ev(
                    U,
                    parts(
                        vec![Part {
                            inline_data: Some(image()),
                            function_response: Some(fr_orphaned.clone()),
                            ..Default::default()
                        }],
                        U,
                    ),
                ),
                ev(U, cfr(&fr_orphaned, U)),
                ev(U, cfr(&fr_lro_final, U)),
            ],
            vec![
                text("first note", U),
                cfc(&fc_basic, M),
                cfr(&fr_basic, U),
                parts(
                    vec![Part {
                        inline_data: Some(image()),
                        ..Default::default()
                    }],
                    U,
                ),
                cfc(&fc_lro, M),
                cfr(&fr_lro_final, U),
            ],
        ),
        case(
            "Mid-history mixed content preserves user text",
            vec![
                ev(U, text("Before", U)),
                ev(U, parts(vec![ptext("My question"), pfr(&fr_orphaned)], U)),
                ev(U, text("After", U)),
            ],
            vec![text("Before", U), text("My question", U), text("After", U)],
        ),
        case(
            "Orphaned function response is removed beside matching response",
            vec![
                ev(A, cfc(&fc_basic, M)),
                ev(U, parts(vec![pfr(&fr_basic), pfr(&fr_orphaned)], U)),
            ],
            vec![cfc(&fc_basic, M), cfr(&fr_basic, U)],
        ),
        case(
            "Only orphaned function response is dropped",
            vec![ev(U, cfr(&fr_orphaned, U))],
            vec![],
        ),
        case(
            "Consecutive orphaned function responses are dropped",
            vec![
                ev(U, text("Keep this", U)),
                ev(U, cfr(&fr_orphaned, U)),
                ev(U, cfr(&fr_orphaned, U)),
            ],
            vec![text("Keep this", U)],
        ),
        case(
            "Mid-history orphaned function response is dropped",
            vec![
                ev(U, text("Before", U)),
                ev(U, cfr(&fr_orphaned, U)),
                ev(U, text("After", U)),
            ],
            vec![text("Before", U), text("After", U)],
        ),
        case(
            "Function response without ID is preserved",
            vec![ev(U, cfr(&fr_without_id, U))],
            vec![cfr(&fr_without_id, U)],
        ),
        Case {
            want_err: Some("no function call event found"),
            ..case(
                "Function response with filtered matching call remains an error",
                vec![
                    ev(U, text("Regular message", U)),
                    Event {
                        isolation_scope: "task".into(),
                        ..ev(A, cfc(&fc_basic, M))
                    },
                    ev(U, cfr(&fr_basic, U)),
                ],
                vec![],
            )
        },
        Case {
            want_err: Some("no function call event found"),
            include_contents: IncludeContents::None,
            ..case(
                "Current turn retains validation for a call from an earlier turn",
                vec![
                    ev(A, cfc(&fc_basic, M)),
                    ev(U, text("Next question", U)),
                    ev(A, cfr(&fr_basic, U)),
                ],
                vec![],
            )
        },
        Case {
            want_err: Some(
                "validation failed: last response event has IDs not in the matching call event",
            ),
            ..case(
                "Responses for different function call events remain an error",
                vec![
                    ev(U, text("Run both", U)),
                    ev(A, cfc(&fc_basic, M)),
                    ev(A, cfc(&fc_other, M)),
                    ev(A, text("Working", M)),
                    ev(U, parts(vec![pfr(&fr_basic), pfr(&fr_other)], U)),
                ],
                vec![],
            )
        },
    ];

    for c in cases {
        let before = serde_json::to_string(&c.events).unwrap();
        let agent = llm_agent(LlmAgentConfig {
            name: A.into(),
            include_contents: c.include_contents,
            ..Default::default()
        });
        let ctx = ctx_builder(&agent, c.events.clone()).build_invocation();
        let result = run_contents(&ctx).await;
        let after = serde_json::to_string(&c.events).unwrap();
        assert_eq!(
            before, after,
            "{}: ContentsRequestProcessor mutated session events",
            c.name
        );
        match (c.want_err, result) {
            (Some(want_err), Err(e)) => assert!(
                e.to_string().contains(want_err),
                "{}: Expected error to contain {want_err:?}, got: {e}",
                c.name
            ),
            (Some(_), Ok(_)) => panic!(
                "{}: ContentsRequestProcessor succeeded; expected an error",
                c.name
            ),
            (None, Err(e)) => panic!("{}: ContentsRequestProcessor failed: {e}", c.name),
            (None, Ok(got)) => assert_eq!(with_continuation(c.want), got, "{}", c.name),
        }
    }
}