eli 0.5.2

Ease Lives Instantly — hook-first AI agent framework with multi-channel support
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
//! Agent execution loop and command dispatch.

use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::time::Instant;

use chrono::Utc;
use nexil::core::results::ToolAutoResultKind;
use nexil::llm::{ChatRequest, LLM};
use nexil::{
    AnchorSelector, ConduitError, ErrorKind, TapeContext, TapeEntry, TapeEntryKind, TapeQuery,
    ToolAutoResult,
};
use serde_json::Value;

use crate::builtin::settings::AgentSettings;
use crate::builtin::tape::TapeService;
use crate::builtin::tools::with_tape_runtime;
use crate::types::PromptValue;

use super::agent_request::{
    build_tool_context, create_llm, lookup_registered_tool, run_tools_once, system_prompt_for_turn,
};

// ---------------------------------------------------------------------------
// Command parsing (inlined from agent_command)
// ---------------------------------------------------------------------------

fn parse_internal_command(line: &str) -> (String, Vec<String>) {
    let parts: Vec<String> = shell_words::split(line)
        .unwrap_or_else(|_| line.split_whitespace().map(|s| s.to_owned()).collect());
    if parts.is_empty() {
        return (String::new(), Vec::new());
    }
    let name = parts[0].clone();
    let rest = parts[1..].to_vec();
    (name, rest)
}

fn parse_args_to_json(tokens: &[String]) -> Value {
    let kwargs: serde_json::Map<String, Value> = tokens
        .iter()
        .filter_map(|t| {
            t.find('=').map(|pos| {
                let key = t[..pos].to_owned();
                let raw = &t[pos + 1..];
                let value = parse_scalar(raw);
                (key, value)
            })
        })
        .collect();

    if !kwargs.is_empty() {
        tokens
            .iter()
            .filter(|t| !t.contains('='))
            .for_each(|t| tracing::warn!("positional argument '{t}' after keyword arguments"));
        return Value::Object(kwargs);
    }

    let joined: String = tokens
        .iter()
        .map(|s| s.as_str())
        .collect::<Vec<_>>()
        .join(" ");
    let mut map = serde_json::Map::new();
    if !joined.is_empty() {
        map.insert("value".to_owned(), Value::String(joined));
    }
    Value::Object(map)
}

/// Parse a scalar string value into the appropriate JSON type.
///
/// Handles booleans ("true"/"false"), integers, floats, JSON objects/arrays,
/// and falls back to strings. For objects/arrays, also supports the unquoted
/// form produced when the shell strips double quotes (e.g. `{FOO:bar}`).
fn parse_scalar(raw: &str) -> Value {
    match raw {
        "true" => Value::Bool(true),
        "false" => Value::Bool(false),
        _ => {
            if let Ok(n) = raw.parse::<i64>() {
                Value::from(n)
            } else if let Ok(n) = raw.parse::<f64>() {
                Value::from(n)
            } else if (raw.starts_with('{') && raw.ends_with('}'))
                || (raw.starts_with('[') && raw.ends_with(']'))
            {
                parse_relaxed_json(raw)
            } else {
                Value::String(raw.to_owned())
            }
        }
    }
}

/// Parse a JSON-like string that may have unquoted keys/values.
///
/// The shell strips double quotes from CLI arguments, so `{"FOO":"bar"}`
/// arrives as `{FOO:bar}`. This function adds quotes around bare identifiers
/// and string values before delegating to `serde_json`.
fn parse_relaxed_json(raw: &str) -> Value {
    // First try strict JSON.
    if let Ok(v) = serde_json::from_str(raw) {
        return v;
    }
    // Fall back: quote bare identifiers (keys and unquoted string values).
    // This handles the common case of {KEY:VALUE,KEY2:VALUE2} where values
    // don't contain spaces or special characters.
    let quoted = raw
        .replace('{', "{\"")
        .replace('}', "\"}")
        .replace('[', "[\"")
        .replace(']', "\"]")
        .replace(':', "\":\"")
        .replace(',', "\",\"");
    serde_json::from_str(&quoted).unwrap_or_else(|_| Value::String(raw.to_owned()))
}

fn command_tool_name(name: &str) -> &str {
    match name {
        "handoff" => "tape.handoff",
        _ => name,
    }
}

// ---------------------------------------------------------------------------
// Internal command execution
// ---------------------------------------------------------------------------

pub(super) async fn run_command(
    tapes: &TapeService,
    tape_name: &str,
    line: &str,
    tool_state: &HashMap<String, Value>,
) -> Result<String, ConduitError> {
    let body = line[1..].trim();
    if body.is_empty() {
        return Err(ConduitError::new(ErrorKind::InvalidInput, "empty command"));
    }

    let (name, arg_tokens) = parse_internal_command(body);
    let start = Instant::now();

    let result = with_tape_runtime(
        tapes.clone(),
        execute_tool_or_bash(&name, &arg_tokens, body, tape_name, tool_state),
    )
    .await;

    let elapsed_ms = start.elapsed().as_millis() as i64;
    record_command_event(body, &name, elapsed_ms, &result);

    match result {
        Ok(val) => Ok(value_to_string(val)),
        Err(e) => Err(e),
    }
}

async fn execute_tool_or_bash(
    name: &str,
    arg_tokens: &[String],
    body: &str,
    tape_name: &str,
    tool_state: &HashMap<String, Value>,
) -> Result<Value, ConduitError> {
    let ctx = build_tool_context("run_command", tape_name, tool_state);
    if let Some(tool) = lookup_registered_tool(command_tool_name(name)) {
        let json_args = parse_args_to_json(arg_tokens);
        let ctx_arg = if tool.context { Some(ctx) } else { None };
        tool.run(json_args, ctx_arg).await
    } else {
        let bash_tool = lookup_registered_tool("bash")
            .ok_or_else(|| ConduitError::new(ErrorKind::Tool, "bash tool not found"))?;
        bash_tool
            .run(serde_json::json!({"cmd": body}), Some(ctx))
            .await
    }
}

fn value_to_string(val: Value) -> String {
    match val {
        Value::String(s) => s,
        other => serde_json::to_string(&other).unwrap_or_default(),
    }
}

fn record_command_event(
    body: &str,
    name: &str,
    elapsed_ms: i64,
    result: &Result<Value, ConduitError>,
) {
    let (status, output) = match result {
        Ok(val) => ("ok", value_to_string(val.clone())),
        Err(e) => ("error", e.message.clone()),
    };
    let event = serde_json::json!({
        "raw": body,
        "name": name,
        "status": status,
        "elapsed_ms": elapsed_ms,
        "output": output,
        "date": Utc::now().to_rfc3339(),
    });
    crate::control_plane::push_save_event("command", event);
}

// ---------------------------------------------------------------------------
// Outbound media extraction
// ---------------------------------------------------------------------------

/// Scan tool results for media file paths and push them to the turn context.
fn extract_outbound_media(tool_results: &[Value]) {
    use crate::control_plane::{
        OutboundMedia, media_type_from_mime, mime_from_extension, push_outbound_media,
    };

    for result in tool_results {
        let obj = match result {
            Value::Object(_) => Some(result.clone()),
            Value::String(s) => serde_json::from_str::<Value>(s).ok(),
            _ => None,
        };
        let Some(obj) = obj else { continue };
        let Some(map) = obj.as_object() else { continue };

        // Only extract from successful results.
        if map.get("success").and_then(|v| v.as_bool()) != Some(true) {
            continue;
        }

        // Check known path keys: image_path, path.
        for key in &["image_path", "path"] {
            if let Some(path_str) = map.get(*key).and_then(|v| v.as_str()) {
                let path = Path::new(path_str);
                if path.exists() {
                    let mime = mime_from_extension(path);
                    let media_type = media_type_from_mime(mime);
                    tracing::info!(path = %path_str, media_type, "outbound_media: extracted from tool result");
                    push_outbound_media(OutboundMedia {
                        path: path_str.to_owned(),
                        media_type: media_type.to_owned(),
                        mime_type: mime.to_owned(),
                    });
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Agent loop helpers
// ---------------------------------------------------------------------------

async fn log_active_decisions(tapes: &TapeService, tape_name: &str) {
    if let Ok(all_entries) = tapes
        .store()
        .fetch_all(&nexil::TapeQuery::new(tape_name))
        .await
    {
        let decisions = nexil::collect_active_decisions(&all_entries);
        if !decisions.is_empty() {
            tracing::info!("Resuming session. Active decisions:");
            for (i, d) in decisions.iter().enumerate() {
                tracing::info!("  {}. {}", i + 1, d);
            }
        }
    }
}

async fn resolve_tape_context_override(
    tapes: &TapeService,
    tape_name: &str,
) -> Option<TapeContext> {
    match tapes.auto_handoff_grace(tape_name).await {
        Ok(Some((remaining, ref prev_anchor))) if remaining > 0 && !prev_anchor.is_empty() => {
            tracing::info!(
                tape = tape_name,
                remaining,
                prev_anchor,
                "auto-handoff grace: using prev anchor"
            );
            Some(TapeContext {
                anchor: AnchorSelector::Named(prev_anchor.clone()),
                ..TapeContext::default()
            })
        }
        _ => None,
    }
}

async fn maybe_auto_handoff(
    tapes: &TapeService,
    tape_name: &str,
    output: &ToolAutoResult,
    response_text: &str,
    settings: &AgentSettings,
    compaction_summary: Option<String>,
) {
    // Prefer a real LLM distillation (when compaction is enabled and one was
    // produced); otherwise fall back to the historical 500-char-prefix summary.
    let summary = compaction_summary.unwrap_or_else(|| response_text.chars().take(500).collect());
    if try_decrement_grace(tapes, tape_name).await {
        // Bug B: new tool results appended during grace can push context over the
        // threshold again. If that happens, don't wait for grace to expire — fire a
        // new handoff immediately so the tape stays manageable.
        if let Some(input_tokens) = should_handoff(output, settings) {
            tracing::warn!(
                tape = tape_name,
                input_tokens,
                "auto-handoff: context re-exceeded threshold during grace period, \
                 triggering immediate handoff"
            );
            place_handoff_anchor(tapes, tape_name, &summary, input_tokens, settings).await;
        }
        return;
    }
    if let Some(input_tokens) = should_handoff(output, settings) {
        place_handoff_anchor(tapes, tape_name, &summary, input_tokens, settings).await;
    }
}

/// Returns true when the error is a context overflow or SSE timeout — both are
/// cases where a handoff is necessary even though we have no model response to
/// use as a summary.
///
/// Only matches against `e.message` (the provider error message), never against
/// user prompt content. Patterns are annotated with the providers that emit them.
fn is_context_or_timeout_error(e: &nexil::ConduitError) -> bool {
    // Use only the error message field, not any user content that may appear
    // elsewhere in a structured error payload.
    let msg = e.message.to_lowercase();

    // OpenAI / Azure OpenAI: error code "context_length_exceeded"
    msg.contains("context_length_exceeded")
    // OpenAI: "This model's maximum context length is N tokens"
    || msg.contains("maximum context length")
    // Anthropic: "prompt is too long"
    || msg.contains("prompt is too long")
    // Anthropic / common: "input too long"
    || msg.contains("input too long")
    // OpenAI / many providers: "context window"
    || msg.contains("context window")
    // Generic: "context length"
    || msg.contains("context length")
    // OpenRouter relay / OpenAI legacy: "context_length" (underscore form)
    || msg.contains("context_length")
    // Gemini / Google: "N tokens exceeds the limit"
    || msg.contains("tokens exceeds")
    // Common: "too many tokens"
    || msg.contains("too many tokens")
    // Cohere: "context limit exceeded"
    || msg.contains("context limit")
    // Mistral / Together AI: "Request too large"
    || msg.contains("request too large")
    // Provider-agnostic SSE network layer error
    || msg.contains("sse_stream_error")
    // Timeout conditions (reqwest / provider-side)
    || msg.contains("timed out")
    || msg.contains("timeout")
}

/// Bug 2: trigger handoff from the error path so that SSE timeouts and context
/// overflow errors (which bypass the normal success branch) still rotate the
/// tape anchor.
async fn maybe_auto_handoff_on_error(
    tapes: &TapeService,
    tape_name: &str,
    error: &nexil::ConduitError,
    settings: &AgentSettings,
) {
    if !is_context_or_timeout_error(error) {
        return;
    }
    let was_in_grace = try_decrement_grace(tapes, tape_name).await;
    tracing::info!(
        tape = tape_name,
        error = %error.message,
        was_in_grace,
        "auto-handoff: triggering from error path (context overflow or timeout)"
    );
    place_handoff_anchor(
        tapes,
        tape_name,
        "[auto-handoff triggered by context overflow or timeout]",
        settings.context_window,
        settings,
    )
    .await;
}

async fn try_decrement_grace(tapes: &TapeService, tape_name: &str) -> bool {
    let Ok(Some((remaining, prev_anchor))) = tapes.auto_handoff_grace(tape_name).await else {
        return false;
    };
    if remaining == 0 {
        return false;
    }
    let new_remaining = remaining - 1;
    let _ = tapes
        .append_event(
            tape_name,
            "auto-handoff.grace",
            serde_json::json!({ "remaining": new_remaining, "prev_anchor": prev_anchor }),
        )
        .await;
    if new_remaining == 0 {
        tracing::info!(
            tape = tape_name,
            "auto-handoff grace ended, context will be trimmed next turn"
        );
    }
    true
}

fn should_handoff(output: &ToolAutoResult, settings: &AgentSettings) -> Option<usize> {
    // Bug 5: use max() across all rounds instead of last() to avoid under-counting
    // in multi-round turns where an earlier round may have had the peak token count.
    let reported = output
        .usage
        .iter()
        .map(|u| u.input_tokens)
        .max()
        .unwrap_or(0) as usize;

    // Bug A: some providers omit usage fields entirely, leaving input_tokens=0 so
    // handoff never fires. Fall back to a char-count estimate from the response
    // content so we still have a signal even without server-side token counts.
    let input_tokens = if reported == 0 {
        let estimated = estimate_tokens_from_output(output);
        if estimated > 0 {
            tracing::warn!(
                estimated_tokens = estimated,
                "auto-handoff: provider did not return usage, \
                 estimating input tokens from response char count"
            );
        }
        estimated
    } else {
        reported
    };

    // Bug 1: default to 40% (was 70%) so large models (1M/200K windows) actually trigger.
    // Override with ELI_HANDOFF_THRESHOLD_PCT (1–99) for custom tuning.
    let pct: usize = std::env::var("ELI_HANDOFF_THRESHOLD_PCT")
        .ok()
        .and_then(|v| v.parse::<usize>().ok())
        .map(|p| p.clamp(1, 99))
        .unwrap_or(40);
    let threshold = settings.context_window * pct / 100;
    (input_tokens >= threshold).then_some(input_tokens)
}

/// Estimate input token count from the output content when the provider does
/// not return usage data. Uses a conservative mixed-language heuristic:
///   - ASCII/Latin chars ≈ 4 chars / token
///   - CJK / wide chars  ≈ 1.5 chars / token  (i.e. chars × 2 / 3)
fn estimate_tokens_from_output(output: &ToolAutoResult) -> usize {
    let text = output.text.as_deref().unwrap_or("");
    let tool_str: String = output
        .tool_results
        .iter()
        .map(|v| match v {
            Value::String(s) => s.clone(),
            other => serde_json::to_string(other).unwrap_or_default(),
        })
        .collect::<Vec<_>>()
        .join("");

    let mut ascii = 0usize;
    let mut cjk = 0usize;
    for c in text.chars().chain(tool_str.chars()) {
        if is_cjk_char(c) {
            cjk += 1;
        } else {
            ascii += 1;
        }
    }
    ascii / 4 + cjk * 2 / 3
}

/// Returns true for characters in common CJK / wide-character Unicode blocks.
fn is_cjk_char(c: char) -> bool {
    matches!(c as u32,
        0x4E00..=0x9FFF   // CJK Unified Ideographs
        | 0x3400..=0x4DBF  // CJK Extension A
        | 0x20000..=0x2A6DF// CJK Extension B
        | 0x3000..=0x303F  // CJK Symbols and Punctuation
        | 0xFF00..=0xFFEF  // Halfwidth and Fullwidth Forms
        | 0x3040..=0x309F  // Hiragana
        | 0x30A0..=0x30FF  // Katakana
        | 0xAC00..=0xD7AF  // Hangul Syllables
    )
}

/// Opt-in (`ELI_CONTEXT_COMPACTION=1`): when set, auto-handoff summaries are a
/// real LLM distillation of the work since the last anchor instead of a 500-char
/// prefix of the last reply. Off by default, so behavior is unchanged.
fn compaction_enabled() -> bool {
    std::env::var("ELI_CONTEXT_COMPACTION")
        .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
        .unwrap_or(false)
}

const COMPACTION_SYSTEM_PROMPT: &str = "\
You are compacting an agent's working context before older turns leave the \
window. Summarize the transcript below into a dense handoff note so work can \
continue without re-reading it. Preserve, in priority order: (1) architecture / \
design decisions; (2) files modified and their key changes; (3) current \
verification status (build/test pass or fail); (4) open TODOs and next steps; \
(5) important facts, constraints, and identifiers. Drop chit-chat and verbose \
tool output, keeping only pass/fail and key values. The full history stays \
retrievable via tape.search. Output only the summary.";

/// Char cap on the digest fed to the summarizer so the compaction call itself
/// can't overflow the window. The most recent content is kept (tail).
const COMPACTION_INPUT_CHAR_CAP: usize = 120_000;
/// Output token budget for the compaction summary.
const COMPACTION_MAX_TOKENS: u32 = 1024;

/// Flatten a message `content` (string or multimodal block array) to text.
fn content_to_text(value: Option<&Value>) -> String {
    match value {
        Some(Value::String(s)) => s.clone(),
        Some(Value::Array(parts)) => parts
            .iter()
            .map(|p| {
                p.get("text")
                    .and_then(|t| t.as_str())
                    .map(str::to_owned)
                    .unwrap_or_else(|| p.to_string())
            })
            .collect::<Vec<_>>()
            .join(" "),
        Some(other) => other.to_string(),
        None => String::new(),
    }
}

/// Keep at most the last `max` chars, prefixing a marker when truncated.
fn tail_chars(s: &str, max: usize) -> String {
    let count = s.chars().count();
    if count <= max {
        return s.to_owned();
    }
    let kept: String = s.chars().skip(count - max).collect();
    format!("[... earlier detail omitted from summary input ...]\n{kept}")
}

/// Render one digest line for an entry that carries summarizable content.
fn entry_digest_line(entry: &TapeEntry) -> Option<String> {
    match entry.kind {
        TapeEntryKind::Message => {
            let role = entry
                .payload
                .get("role")
                .and_then(|v| v.as_str())
                .unwrap_or("?");
            let content = content_to_text(entry.payload.get("content"));
            (!content.trim().is_empty()).then(|| format!("{role}: {content}"))
        }
        TapeEntryKind::ToolCall => {
            let names: Vec<&str> = entry
                .payload
                .get("calls")
                .and_then(|c| c.as_array())
                .map(|calls| {
                    calls
                        .iter()
                        .filter_map(|c| {
                            c.get("function")
                                .and_then(|f| f.get("name"))
                                .and_then(|n| n.as_str())
                        })
                        .collect()
                })
                .unwrap_or_default();
            (!names.is_empty()).then(|| format!("assistant called tools: {}", names.join(", ")))
        }
        TapeEntryKind::ToolResult => {
            let preview = content_to_text(entry.payload.get("results"));
            let preview: String = preview.chars().take(500).collect();
            (!preview.trim().is_empty()).then(|| format!("tool result: {preview}"))
        }
        _ => None,
    }
}

/// Build a text digest of the work recorded since the last anchor, capped to the
/// most recent [`COMPACTION_INPUT_CHAR_CAP`] chars.
fn digest_since_last_anchor(entries: &[TapeEntry]) -> String {
    let start = entries
        .iter()
        .rposition(|e| e.kind == TapeEntryKind::Anchor)
        .map(|i| i + 1)
        .unwrap_or(0);
    let mut out = String::new();
    for entry in &entries[start..] {
        if let Some(line) = entry_digest_line(entry) {
            out.push_str(&line);
            out.push('\n');
        }
    }
    tail_chars(&out, COMPACTION_INPUT_CHAR_CAP)
}

/// Summarize the work since the last anchor via a single model call. Returns
/// `None` on any failure (empty span, read error, model error) so the caller
/// falls back to the prefix summary.
async fn summarize_since_anchor(
    llm: &mut LLM,
    tapes: &TapeService,
    tape_name: &str,
) -> Option<String> {
    let entries = tapes
        .store()
        .fetch_all(&TapeQuery::new(tape_name))
        .await
        .ok()?;
    let digest = digest_since_last_anchor(&entries);
    if digest.trim().is_empty() {
        return None;
    }
    let summary = llm
        .chat_async(ChatRequest {
            system_prompt: Some(COMPACTION_SYSTEM_PROMPT),
            prompt: Some(&digest),
            max_tokens: Some(COMPACTION_MAX_TOKENS),
            ..Default::default()
        })
        .await
        .ok()?;
    let trimmed = summary.trim();
    (!trimmed.is_empty()).then(|| trimmed.to_owned())
}

/// When compaction is enabled and a handoff will fire this turn, produce a real
/// LLM summary of the since-anchor span. `None` ⇒ caller uses the prefix summary.
async fn maybe_make_compaction_summary(
    llm: &mut LLM,
    tapes: &TapeService,
    tape_name: &str,
    result: &Result<ToolAutoResult, ConduitError>,
    settings: &AgentSettings,
) -> Option<String> {
    if !compaction_enabled() {
        return None;
    }
    let output = match result {
        Ok(o) if o.kind == ToolAutoResultKind::Text => o,
        _ => return None,
    };
    // Only spend a summarization call when a handoff is actually going to fire.
    should_handoff(output, settings)?;
    summarize_since_anchor(llm, tapes, tape_name).await
}

async fn place_handoff_anchor(
    tapes: &TapeService,
    tape_name: &str,
    summary: &str,
    input_tokens: usize,
    settings: &AgentSettings,
) {
    let prev_anchor_name = tapes
        .last_anchor_name(tape_name)
        .await
        .ok()
        .flatten()
        .unwrap_or_default();

    write_handoff_anchor(tapes, tape_name, summary, input_tokens, settings).await;
    write_handoff_summary(tapes, tape_name, summary).await;
    write_handoff_grace(tapes, tape_name, &prev_anchor_name).await;

    tracing::info!(
        tape = tape_name,
        input_tokens,
        context_window = settings.context_window,
        "auto-handoff: anchor placed, grace period 2"
    );
}

async fn write_handoff_anchor(
    tapes: &TapeService,
    tape_name: &str,
    summary: &str,
    input_tokens: usize,
    settings: &AgentSettings,
) {
    let anchor_name = format!("auto-handoff/{}", Utc::now().format("%Y%m%dT%H%M%S"));
    let anchor_state = serde_json::json!({
        "reason": "auto-handoff: context approaching limit",
        "input_tokens": input_tokens,
        "context_window": settings.context_window,
        "summary": summary,
    });
    if let Err(e) = tapes
        .handoff(tape_name, &anchor_name, Some(anchor_state))
        .await
    {
        tracing::warn!(error = %e, "auto-handoff: failed to write anchor");
    }
}

async fn write_handoff_summary(tapes: &TapeService, tape_name: &str, summary: &str) {
    let sys_entry = TapeEntry::system(
        &format!("[Context summary from auto-handoff]\n{summary}"),
        Value::Object(Default::default()),
    );
    if let Err(e) = tapes.store().append(tape_name, &sys_entry).await {
        tracing::warn!(error = %e, "auto-handoff: failed to write summary");
    }
}

async fn write_handoff_grace(tapes: &TapeService, tape_name: &str, prev_anchor_name: &str) {
    if let Err(e) = tapes
        .append_event(
            tape_name,
            "auto-handoff.grace",
            serde_json::json!({ "remaining": 2, "prev_anchor": prev_anchor_name }),
        )
        .await
    {
        tracing::warn!(error = %e, "auto-handoff: failed to write grace event");
    }
}

fn record_run_event(
    elapsed_ms: i64,
    status: &str,
    error: Option<&str>,
    usage: &[nexil::UsageEvent],
    tool_calls: usize,
) {
    let total_input: u64 = usage.iter().map(|u| u.input_tokens).sum();
    let total_output: u64 = usage.iter().map(|u| u.output_tokens).sum();
    let total_tokens = total_input + total_output;
    let cache_read: u64 = usage.iter().map(|u| u.cache_read_input_tokens).sum();
    let cache_write: u64 = usage.iter().map(|u| u.cache_creation_input_tokens).sum();
    // Fraction of prompt tokens served from cache — the prompt-caching win (#2)
    // made observable per turn. Denominator includes cache reads since the
    // provider reports them separately from `input_tokens`.
    let cache_hit_ratio = {
        let prompt = total_input + cache_read + cache_write;
        if prompt == 0 {
            0.0
        } else {
            cache_read as f64 / prompt as f64
        }
    };

    crate::control_plane::record_turn_usage(total_input, total_output, cache_read);

    let mut event = serde_json::json!({
        "elapsed_ms": elapsed_ms,
        "status": status,
        "date": Utc::now().to_rfc3339(),
        "usage": {
            "input_tokens": total_input,
            "output_tokens": total_output,
            "total_tokens": total_tokens,
            "rounds": usage.len(),
            "cache_read_tokens": cache_read,
            "cache_write_tokens": cache_write,
            "cache_hit_ratio": cache_hit_ratio,
        },
        "tool_calls": tool_calls,
    });
    if let Some(err) = error {
        event["error"] = Value::String(err.to_owned());
    }
    // Structured per-turn summary: also emit to tracing so cache savings and
    // tool activity are visible in logs without reading the tape.
    tracing::info!(
        status,
        elapsed_ms,
        input_tokens = total_input,
        output_tokens = total_output,
        cache_read_tokens = cache_read,
        cache_write_tokens = cache_write,
        cache_hit_ratio = format!("{:.2}", cache_hit_ratio),
        tool_calls,
        rounds = usage.len(),
        "agent.run summary"
    );
    crate::control_plane::push_save_event("agent.run", event);
}

// ---------------------------------------------------------------------------
// Verify / self-correct (opt-in, hard-signal grounded)
// ---------------------------------------------------------------------------

/// Hard-signal verification command (`ELI_VERIFY_CMD`, e.g. "cargo test").
/// `None` (default) disables the whole verify loop, so behavior is unchanged.
fn verify_command() -> Option<String> {
    std::env::var("ELI_VERIFY_CMD")
        .ok()
        .filter(|s| !s.trim().is_empty())
}

/// Max self-correction re-runs after a failing verification (`ELI_VERIFY_MAX_RETRIES`, default 1).
fn verify_max_retries() -> u32 {
    std::env::var("ELI_VERIFY_MAX_RETRIES")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(1)
}

/// Run the verification command in `workspace`. `Ok(())` on exit 0; otherwise
/// `Err` with a tail-capped stdout+stderr the model can act on.
async fn run_verify_command(cmd: &str, workspace: &Path) -> Result<(), String> {
    let output = tokio::process::Command::new("sh")
        .arg("-c")
        .arg(cmd)
        .current_dir(workspace)
        .output()
        .await
        .map_err(|e| format!("verify command failed to start: {e}"))?;
    if output.status.success() {
        return Ok(());
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    Err(tail_chars(&format!("{stdout}\n{stderr}"), 4000))
}

/// Reflexion-style verify loop: when `ELI_VERIFY_CMD` is set, run it after a
/// successful, tool-using turn and — on failure — feed the failure back as a
/// follow-up turn for the model to fix, bounded by `ELI_VERIFY_MAX_RETRIES`.
/// Grounded in a hard external signal (build/test), not LLM self-judgment.
#[allow(clippy::too_many_arguments)]
async fn verify_and_correct(
    llm: &mut LLM,
    tapes: &TapeService,
    tape_name: &str,
    system_prompt: &str,
    tool_state: &HashMap<String, Value>,
    settings: &AgentSettings,
    allowed_tools: Option<&HashSet<String>>,
    tape_ctx: Option<&TapeContext>,
    session_id: &str,
    workspace: &Path,
    mut result: Result<ToolAutoResult, ConduitError>,
) -> Result<ToolAutoResult, ConduitError> {
    let Some(cmd) = verify_command() else {
        return result;
    };
    let max_retries = verify_max_retries();
    let mut attempt = 0;
    loop {
        // Only verify successful turns that actually used tools (a pure-chat
        // turn changed nothing to verify).
        let verifiable = matches!(&result,
            Ok(o) if o.kind == ToolAutoResultKind::Text && !o.tool_calls.is_empty());
        if !verifiable {
            return result;
        }
        match run_verify_command(&cmd, workspace).await {
            Ok(()) => {
                tracing::info!(tape = tape_name, attempt, "verify passed");
                return result;
            }
            Err(failure) if attempt < max_retries => {
                attempt += 1;
                tracing::warn!(
                    tape = tape_name,
                    attempt,
                    "verify failed — re-running for self-correction"
                );
                let _ = tapes
                    .append_event(
                        tape_name,
                        "agent.verify.failed",
                        serde_json::json!({ "attempt": attempt, "cmd": cmd }),
                    )
                    .await;
                let followup = PromptValue::Text(format!(
                    "Verification failed. The command `{cmd}` reported:\n\n{failure}\n\n\
                     Fix the cause and continue. Do not claim success until it passes."
                ));
                result = with_tape_runtime(
                    tapes.clone(),
                    run_tools_once(
                        llm,
                        system_prompt,
                        tape_name,
                        &followup,
                        tool_state,
                        settings,
                        allowed_tools,
                        tape_ctx,
                        session_id,
                    ),
                )
                .await;
            }
            Err(_failure) => {
                tracing::warn!(
                    tape = tape_name,
                    attempt,
                    "verify still failing after max retries"
                );
                return result;
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Agent loop
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
pub(super) async fn agent_loop(
    tapes: &TapeService,
    tape_name: &str,
    initial_prompt: PromptValue,
    settings: &AgentSettings,
    model: Option<&str>,
    state: &HashMap<String, Value>,
    allowed_skills: Option<&HashSet<String>>,
    allowed_tools: Option<&HashSet<String>>,
    tool_state: &HashMap<String, Value>,
    workspace: &Path,
    session_id: &str,
) -> Result<String, ConduitError> {
    let mut llm = create_llm(settings, model, tapes.store().clone())?;
    let prompt_text = initial_prompt.strict_text();
    let system_prompt =
        system_prompt_for_turn(settings, &prompt_text, state, allowed_skills, workspace);
    let display_model = model.unwrap_or(&settings.model);

    let start = Instant::now();
    tracing::info!(tape = tape_name, model = display_model, "agent.run");
    let _ = tapes
        .append_event(
            tape_name,
            "agent.run.start",
            serde_json::json!({"prompt": prompt_text}),
        )
        .await;

    log_active_decisions(tapes, tape_name).await;
    let tape_ctx_override = resolve_tape_context_override(tapes, tape_name).await;

    let result = with_tape_runtime(
        tapes.clone(),
        run_tools_once(
            &mut llm,
            &system_prompt,
            tape_name,
            &initial_prompt,
            tool_state,
            settings,
            allowed_tools,
            tape_ctx_override.as_ref(),
            session_id,
        ),
    )
    .await;

    // Hard-signal verify / self-correct loop (opt-in via ELI_VERIFY_CMD; a no-op
    // otherwise, so normal turns are unaffected).
    let result = verify_and_correct(
        &mut llm,
        tapes,
        tape_name,
        &system_prompt,
        tool_state,
        settings,
        allowed_tools,
        tape_ctx_override.as_ref(),
        session_id,
        workspace,
        result,
    )
    .await;

    // Generate the LLM compaction summary here, where `llm` is in scope, before
    // the result is moved into process_agent_result. No-op unless compaction is
    // enabled and a handoff will fire this turn.
    let compaction_summary =
        maybe_make_compaction_summary(&mut llm, tapes, tape_name, &result, settings).await;

    let elapsed_ms = start.elapsed().as_millis() as i64;
    process_agent_result(
        tapes,
        tape_name,
        result,
        elapsed_ms,
        settings,
        compaction_summary,
    )
    .await
}

async fn process_agent_result(
    tapes: &TapeService,
    tape_name: &str,
    result: Result<ToolAutoResult, ConduitError>,
    elapsed_ms: i64,
    settings: &AgentSettings,
    compaction_summary: Option<String>,
) -> Result<String, ConduitError> {
    match result {
        Err(e) => {
            tracing::warn!(
                tape = tape_name,
                elapsed_ms,
                error = %e.message,
                "agent.run finished with error"
            );
            record_run_event(elapsed_ms, "error", Some(&e.message), &[], 0);
            // Bug 2: context overflow and SSE timeouts never reach the success
            // branch, so check them here and trigger handoff if needed.
            maybe_auto_handoff_on_error(tapes, tape_name, &e, settings).await;
            Err(e)
        }
        Ok(ref output) if output.kind == ToolAutoResultKind::Text => {
            let text = output.text.clone().unwrap_or_default();
            tracing::info!(
                tape = tape_name,
                elapsed_ms,
                text_len = text.len(),
                tool_calls = output.tool_calls.len(),
                "agent.run finished ok"
            );
            extract_outbound_media(&output.tool_results);
            record_run_event(
                elapsed_ms,
                "ok",
                None,
                &output.usage,
                output.tool_calls.len(),
            );
            maybe_auto_handoff(
                tapes,
                tape_name,
                output,
                &text,
                settings,
                compaction_summary,
            )
            .await;
            Ok(text)
        }
        Ok(ref output) => {
            let error_msg = output
                .error
                .as_ref()
                .map(|e| format!("{}: {}", e.kind.as_str(), e.message))
                .unwrap_or_else(|| "tool_auto_error: unknown".to_owned());
            tracing::warn!(
                tape = tape_name,
                elapsed_ms,
                error = %error_msg,
                kind = ?output.kind,
                "agent.run finished with non-text result"
            );
            record_run_event(
                elapsed_ms,
                "error",
                Some(&error_msg),
                &output.usage,
                output.tool_calls.len(),
            );
            Err(ConduitError::new(ErrorKind::Unknown, error_msg))
        }
    }
}

#[cfg(test)]
mod tests {
    use std::future::Future;

    use super::*;
    use crate::builtin::store::{FileTapeStore, ForkTapeStore};
    use crate::control_plane::{TurnContext, drain_save_events, with_turn_context};
    use nexil::{TapeEntryKind, TapeQuery, UsageEvent};

    const OVERFLOW_SUMMARY: &str = "[auto-handoff triggered by context overflow or timeout]";
    const SUMMARY_PREFIX: &str = "[Context summary from auto-handoff]\n";

    // -- compaction digest helpers (pure) -------------------------------------

    #[test]
    fn content_to_text_handles_string_array_and_none() {
        assert_eq!(content_to_text(Some(&serde_json::json!("hi"))), "hi");
        let blocks = serde_json::json!([{"type":"text","text":"a"},{"type":"text","text":"b"}]);
        assert_eq!(content_to_text(Some(&blocks)), "a b");
        assert_eq!(content_to_text(None), "");
    }

    #[test]
    fn tail_chars_keeps_recent_and_marks_truncation() {
        assert_eq!(tail_chars("short", 100), "short");
        let big = "z".repeat(200);
        let tail = tail_chars(&big, 50);
        assert!(tail.contains("earlier detail omitted"));
        assert!(tail.ends_with(&"z".repeat(50)));
    }

    #[test]
    fn entry_digest_line_renders_messages_skips_anchors() {
        let msg = TapeEntry::message(
            serde_json::json!({"role": "user", "content": "hello"}),
            serde_json::json!({}),
        );
        assert_eq!(entry_digest_line(&msg).as_deref(), Some("user: hello"));
        let anchor = TapeEntry::anchor("a", None, serde_json::json!({}));
        assert!(entry_digest_line(&anchor).is_none());
    }

    #[test]
    fn digest_covers_only_work_since_last_anchor() {
        let entries = vec![
            TapeEntry::message(
                serde_json::json!({"role": "user", "content": "BEFORE the anchor"}),
                serde_json::json!({}),
            ),
            TapeEntry::anchor("a1", None, serde_json::json!({})),
            TapeEntry::message(
                serde_json::json!({"role": "user", "content": "AFTER one"}),
                serde_json::json!({}),
            ),
            TapeEntry::message(
                serde_json::json!({"role": "assistant", "content": "AFTER two"}),
                serde_json::json!({}),
            ),
        ];
        let digest = digest_since_last_anchor(&entries);
        assert!(!digest.contains("BEFORE the anchor"));
        assert!(digest.contains("AFTER one"));
        assert!(digest.contains("AFTER two"));
        assert!(digest.contains("user:") && digest.contains("assistant:"));
    }

    // -- verify command (hard-signal) -----------------------------------------

    #[tokio::test]
    async fn verify_command_passes_on_zero_exit() {
        assert!(run_verify_command("true", Path::new(".")).await.is_ok());
    }

    #[tokio::test]
    async fn verify_command_reports_output_on_nonzero_exit() {
        let err = run_verify_command("echo boom 1>&2; exit 1", Path::new("."))
            .await
            .unwrap_err();
        assert!(err.contains("boom"), "err was: {err}");
    }

    fn make_tape_service() -> (tempfile::TempDir, TapeService) {
        let tmp = tempfile::tempdir().unwrap();
        let tapes_dir = tmp.path().join("tapes");
        let store = ForkTapeStore::from_sync(FileTapeStore::new(tapes_dir.clone()));
        (tmp, TapeService::new(tapes_dir, store))
    }

    fn test_settings(context_window: usize) -> AgentSettings {
        let mut settings = AgentSettings::from_env();
        settings.context_window = context_window;
        settings
    }

    fn make_usage(input: u64, output: u64) -> Vec<UsageEvent> {
        vec![UsageEvent {
            model: "test-model".into(),
            input_tokens: input,
            output_tokens: output,
            cache_creation_input_tokens: 0,
            cache_read_input_tokens: 0,
            timestamp: "2026-01-01T00:00:00Z".into(),
        }]
    }

    fn test_turn_context() -> TurnContext {
        TurnContext {
            cancellation: nexil::CancellationToken::new(),
            wrap_tools: None,
            usage: Default::default(),
            save_events: Default::default(),
            dispatch: None,
            outbound_media: Default::default(),
            text_sink: None,
        }
    }

    fn text_with_usage(text: &str, input_tokens: u64) -> ToolAutoResult {
        let mut result = ToolAutoResult::text_result(text);
        result.usage = make_usage(input_tokens, 0);
        result
    }

    fn tool_result_without_usage(chars: usize) -> ToolAutoResult {
        let mut result = ToolAutoResult::text_result("");
        result.tool_results = vec![serde_json::json!({ "payload": "x".repeat(chars) })];
        result
    }

    fn context_overflow_error() -> ConduitError {
        ConduitError::new(ErrorKind::Provider, "context_length_exceeded")
    }

    async fn run_injected_turn<F>(
        tapes: &TapeService,
        tape_name: &str,
        settings: &AgentSettings,
        injected: F,
    ) -> (Option<TapeContext>, Result<String, ConduitError>)
    where
        F: FnOnce(Option<&TapeContext>) -> Result<ToolAutoResult, ConduitError>,
    {
        let tape_context = resolve_tape_context_override(tapes, tape_name).await;
        let result = injected(tape_context.as_ref());
        let text = process_agent_result(tapes, tape_name, result, 1, settings, None).await;
        (tape_context, text)
    }

    async fn inject_text(
        tapes: &TapeService,
        tape_name: &str,
        settings: &AgentSettings,
        text: &str,
        input_tokens: u64,
    ) -> Result<String, ConduitError> {
        run_injected_turn(tapes, tape_name, settings, |_| {
            Ok(text_with_usage(text, input_tokens))
        })
        .await
        .1
    }

    async fn inject_overflow_error(
        tapes: &TapeService,
        tape_name: &str,
        settings: &AgentSettings,
    ) -> Result<String, ConduitError> {
        run_injected_turn(
            tapes,
            tape_name,
            settings,
            |_| Err(context_overflow_error()),
        )
        .await
        .1
    }

    async fn assert_auto_anchor_count(tapes: &TapeService, tape_name: &str, expected: usize) {
        let actual = auto_anchor_names(tapes, tape_name).await.len();
        assert_eq!(actual, expected);
    }

    async fn auto_anchor_names(tapes: &TapeService, tape_name: &str) -> Vec<String> {
        tapes
            .anchors(tape_name, 20)
            .await
            .unwrap()
            .into_iter()
            .filter(|a| a.name.starts_with("auto-handoff/"))
            .map(|a| a.name)
            .collect()
    }

    async fn assert_last_auto_anchor(
        tapes: &TapeService,
        tape_name: &str,
        input_tokens: u64,
        summary: &str,
    ) {
        let anchors = tapes.anchors(tape_name, 20).await.unwrap();
        let anchor = anchors.last().unwrap();
        assert!(anchor.name.starts_with("auto-handoff/"));
        assert_eq!(anchor.state["input_tokens"].as_u64(), Some(input_tokens));
        assert_eq!(anchor.state["summary"].as_str(), Some(summary));
    }

    async fn assert_grace(tapes: &TapeService, tape_name: &str, remaining: u32, anchor: &str) {
        let grace = tapes.auto_handoff_grace(tape_name).await.unwrap();
        assert_eq!(grace, Some((remaining, anchor.to_owned())));
    }

    async fn latest_system_content(tapes: &TapeService, tape_name: &str) -> String {
        let query = TapeQuery::new(tape_name);
        let entries = tapes.store().fetch_all(&query).await.unwrap();
        entries
            .iter()
            .rev()
            .find(|e| e.kind == TapeEntryKind::System)
            .and_then(|e| e.payload.get("content").and_then(Value::as_str))
            .unwrap_or("")
            .to_owned()
    }

    async fn assert_summary_written(tapes: &TapeService, tape_name: &str, summary: &str) {
        let expected = format!("{SUMMARY_PREFIX}{summary}");
        assert_eq!(latest_system_content(tapes, tape_name).await, expected);
    }

    fn assert_named_context(ctx: Option<TapeContext>, expected: &str) {
        let Some(ctx) = ctx else {
            panic!("expected injected turn to receive tape context");
        };
        match ctx.anchor {
            AnchorSelector::Named(name) => assert_eq!(name, expected),
            other => panic!("expected named anchor, got {other:?}"),
        }
    }

    async fn with_injected_tape<F, Fut>(tape_name: &str, f: F)
    where
        F: FnOnce(TapeService, String, AgentSettings) -> Fut,
        Fut: Future<Output = ()>,
    {
        let (_tmp, tapes) = make_tape_service();
        tapes.ensure_bootstrap_anchor(tape_name).await.unwrap();
        let settings = test_settings(1000);
        with_turn_context(
            test_turn_context(),
            f(tapes, tape_name.to_owned(), settings),
        )
        .await;
    }

    async fn injected_small_result_does_not_handoff(
        tapes: TapeService,
        tape_name: String,
        settings: AgentSettings,
    ) {
        let (_, result) = run_injected_turn(&tapes, &tape_name, &settings, |_| {
            Ok(text_with_usage("small", 0))
        })
        .await;
        assert_eq!(result.unwrap(), "small");
        assert_auto_anchor_count(&tapes, &tape_name, 0).await;
    }

    async fn injected_high_usage_result_places_handoff_anchor(
        tapes: TapeService,
        tape_name: String,
        settings: AgentSettings,
    ) {
        let result = inject_text(&tapes, &tape_name, &settings, "large response", 1000).await;
        assert_eq!(result.unwrap(), "large response");
        assert_last_auto_anchor(&tapes, &tape_name, 1000, "large response").await;
        assert_summary_written(&tapes, &tape_name, "large response").await;
        assert_grace(&tapes, &tape_name, 2, "session/start").await;
    }

    async fn injected_tool_result_estimate_places_handoff_anchor(
        tapes: TapeService,
        tape_name: String,
        settings: AgentSettings,
    ) {
        let (_, result) = run_injected_turn(&tapes, &tape_name, &settings, |_| {
            Ok(tool_result_without_usage(5000))
        })
        .await;
        assert_eq!(result.unwrap(), "");
        assert_auto_anchor_count(&tapes, &tape_name, 1).await;
    }

    async fn injected_overflow_error_places_handoff_anchor(
        tapes: TapeService,
        tape_name: String,
        settings: AgentSettings,
    ) {
        let result = inject_overflow_error(&tapes, &tape_name, &settings).await;
        assert!(result.is_err());
        assert_last_auto_anchor(&tapes, &tape_name, 1000, OVERFLOW_SUMMARY).await;
        assert_summary_written(&tapes, &tape_name, OVERFLOW_SUMMARY).await;
    }

    async fn injected_grace_turn_uses_previous_anchor(
        tapes: TapeService,
        tape_name: String,
        settings: AgentSettings,
    ) {
        inject_text(&tapes, &tape_name, &settings, "large", 1000)
            .await
            .unwrap();
        let (ctx, _) = run_injected_turn(&tapes, &tape_name, &settings, |_| {
            Ok(text_with_usage("small", 0))
        })
        .await;
        assert_named_context(ctx, "session/start");
        assert_grace(&tapes, &tape_name, 1, "session/start").await;
    }

    async fn injected_overflow_error_during_grace_advances_handoff(
        tapes: TapeService,
        tape_name: String,
        settings: AgentSettings,
    ) {
        inject_text(&tapes, &tape_name, &settings, "large", 1000)
            .await
            .unwrap();
        assert!(
            inject_overflow_error(&tapes, &tape_name, &settings)
                .await
                .is_err()
        );
        let anchors = auto_anchor_names(&tapes, &tape_name).await;
        assert_eq!(anchors.len(), 2);
        assert_grace(&tapes, &tape_name, 2, &anchors[0]).await;
    }

    #[tokio::test]
    async fn test_record_run_event_pushes_save_event() {
        with_turn_context(test_turn_context(), async {
            record_run_event(500, "ok", None, &make_usage(1000, 200), 0);

            let events = drain_save_events();
            assert_eq!(events.len(), 1);
            assert_eq!(events[0].0, "agent.run");
            let data = &events[0].1;
            assert_eq!(data["usage"]["input_tokens"], 1000);
            assert_eq!(data["usage"]["output_tokens"], 200);
            assert_eq!(data["usage"]["total_tokens"], 1200);
            assert_eq!(data["usage"]["rounds"], 1);
        })
        .await;
    }

    #[tokio::test]
    async fn test_record_run_event_aggregates_multi_round_usage() {
        with_turn_context(test_turn_context(), async {
            let usage = vec![
                UsageEvent {
                    model: "m".into(),
                    input_tokens: 500,
                    output_tokens: 100,
                    cache_creation_input_tokens: 0,
                    cache_read_input_tokens: 0,
                    timestamp: "2026-01-01T00:00:00Z".into(),
                },
                UsageEvent {
                    model: "m".into(),
                    input_tokens: 800,
                    output_tokens: 150,
                    cache_creation_input_tokens: 0,
                    cache_read_input_tokens: 0,
                    timestamp: "2026-01-01T00:00:01Z".into(),
                },
            ];
            record_run_event(1000, "ok", None, &usage, 0);

            let events = drain_save_events();
            assert_eq!(events.len(), 1);
            let data = &events[0].1;
            assert_eq!(data["usage"]["input_tokens"], 1300);
            assert_eq!(data["usage"]["output_tokens"], 250);
            assert_eq!(data["usage"]["total_tokens"], 1550);
            assert_eq!(data["usage"]["rounds"], 2);
        })
        .await;
    }

    #[tokio::test]
    async fn test_record_run_event_surfaces_cache_and_tool_metrics() {
        with_turn_context(test_turn_context(), async {
            let usage = vec![UsageEvent {
                model: "m".into(),
                input_tokens: 200,
                output_tokens: 50,
                cache_creation_input_tokens: 100,
                cache_read_input_tokens: 700,
                timestamp: "2026-01-01T00:00:00Z".into(),
            }];
            record_run_event(42, "ok", None, &usage, 3);

            let events = drain_save_events();
            let data = &events[0].1;
            assert_eq!(data["usage"]["cache_read_tokens"], 700);
            assert_eq!(data["usage"]["cache_write_tokens"], 100);
            assert_eq!(data["tool_calls"], 3);
            // hit ratio = cache_read / (input + cache_read + cache_write)
            //           = 700 / (200 + 700 + 100) = 0.7
            let ratio = data["usage"]["cache_hit_ratio"].as_f64().unwrap();
            assert!((ratio - 0.7).abs() < 1e-9, "ratio was {ratio}");
        })
        .await;
    }

    #[tokio::test]
    async fn test_process_agent_result_ok_pushes_save_event() {
        let (_tmp, tapes) = make_tape_service();
        let tape_name = "test_tape";
        tapes.ensure_bootstrap_anchor(tape_name).await.unwrap();

        with_turn_context(test_turn_context(), async {
            let result = Ok(ToolAutoResult {
                kind: ToolAutoResultKind::Text,
                text: Some("hello".into()),
                tool_calls: vec![],
                tool_results: vec![],
                error: None,
                usage: make_usage(2000, 400),
            });

            let settings = AgentSettings::from_env();
            let text = process_agent_result(&tapes, tape_name, result, 100, &settings, None)
                .await
                .unwrap();
            assert_eq!(text, "hello");

            let events = drain_save_events();
            assert_eq!(events.len(), 1);
            assert_eq!(events[0].0, "agent.run");
            assert_eq!(events[0].1["usage"]["total_tokens"], 2400);
        })
        .await;
    }

    #[tokio::test]
    async fn test_injected_small_result_does_not_handoff() {
        with_injected_tape("inject_small", injected_small_result_does_not_handoff).await;
    }

    #[tokio::test]
    async fn test_injected_high_usage_result_places_handoff_anchor() {
        with_injected_tape(
            "inject_high_usage",
            injected_high_usage_result_places_handoff_anchor,
        )
        .await;
    }

    #[tokio::test]
    async fn test_injected_tool_result_estimate_places_handoff_anchor() {
        with_injected_tape(
            "inject_tool_estimate",
            injected_tool_result_estimate_places_handoff_anchor,
        )
        .await;
    }

    #[tokio::test]
    async fn test_injected_overflow_error_places_handoff_anchor() {
        with_injected_tape(
            "inject_overflow_error",
            injected_overflow_error_places_handoff_anchor,
        )
        .await;
    }

    #[tokio::test]
    async fn test_injected_grace_turn_uses_previous_anchor() {
        with_injected_tape(
            "inject_grace_context",
            injected_grace_turn_uses_previous_anchor,
        )
        .await;
    }

    #[tokio::test]
    async fn test_injected_overflow_error_during_grace_advances_handoff() {
        with_injected_tape(
            "inject_grace_error",
            injected_overflow_error_during_grace_advances_handoff,
        )
        .await;
    }

    #[tokio::test]
    async fn test_extract_outbound_media_from_tool_results() {
        use crate::control_plane::drain_outbound_media;

        // Create a real temp file so path.exists() passes.
        let tmp = tempfile::NamedTempFile::with_suffix(".png").unwrap();
        let path = tmp.path().to_str().unwrap().to_owned();

        with_turn_context(test_turn_context(), async {
            let results = vec![serde_json::json!({
                "success": true,
                "image_path": path,
            })];
            extract_outbound_media(&results);

            let media = drain_outbound_media();
            assert_eq!(media.len(), 1);
            assert_eq!(media[0].path, path);
            assert_eq!(media[0].media_type, "image");
            assert_eq!(media[0].mime_type, "image/png");
        })
        .await;
    }

    #[tokio::test]
    async fn test_extract_outbound_media_ignores_failed_results() {
        use crate::control_plane::drain_outbound_media;

        let tmp = tempfile::NamedTempFile::with_suffix(".png").unwrap();
        let path = tmp.path().to_str().unwrap().to_owned();

        with_turn_context(test_turn_context(), async {
            let results = vec![serde_json::json!({
                "success": false,
                "image_path": path,
            })];
            extract_outbound_media(&results);

            assert!(
                drain_outbound_media().is_empty(),
                "failed results must be ignored"
            );
        })
        .await;
    }

    #[tokio::test]
    async fn test_extract_outbound_media_ignores_missing_files() {
        use crate::control_plane::drain_outbound_media;

        with_turn_context(test_turn_context(), async {
            let results = vec![serde_json::json!({
                "success": true,
                "image_path": "/tmp/nonexistent_file_12345.png",
            })];
            extract_outbound_media(&results);

            assert!(
                drain_outbound_media().is_empty(),
                "non-existent files must be skipped"
            );
        })
        .await;
    }

    #[tokio::test]
    async fn test_extract_outbound_media_handles_path_key() {
        use crate::control_plane::drain_outbound_media;

        let tmp = tempfile::NamedTempFile::with_suffix(".mp4").unwrap();
        let path = tmp.path().to_str().unwrap().to_owned();

        with_turn_context(test_turn_context(), async {
            let results = vec![serde_json::json!({
                "success": true,
                "path": path,
            })];
            extract_outbound_media(&results);

            let media = drain_outbound_media();
            assert_eq!(media.len(), 1);
            assert_eq!(media[0].media_type, "video");
            assert_eq!(media[0].mime_type, "video/mp4");
        })
        .await;
    }

    #[tokio::test]
    async fn test_extract_outbound_media_from_stringified_json() {
        use crate::control_plane::drain_outbound_media;

        let tmp = tempfile::NamedTempFile::with_suffix(".jpg").unwrap();
        let path = tmp.path().to_str().unwrap().to_owned();

        with_turn_context(test_turn_context(), async {
            // Tool results can be stringified JSON.
            let results = vec![Value::String(
                serde_json::to_string(&serde_json::json!({
                    "success": true,
                    "image_path": path,
                }))
                .unwrap(),
            )];
            extract_outbound_media(&results);

            let media = drain_outbound_media();
            assert_eq!(media.len(), 1);
            assert_eq!(media[0].mime_type, "image/jpeg");
        })
        .await;
    }

    #[tokio::test]
    async fn test_process_agent_result_extracts_media() {
        use crate::control_plane::drain_outbound_media;

        let (_tmp_dir, tapes) = make_tape_service();
        let tape_name = "test_media_tape";
        tapes.ensure_bootstrap_anchor(tape_name).await.unwrap();

        // Create a real temp file.
        let tmp = tempfile::NamedTempFile::with_suffix(".png").unwrap();
        let path = tmp.path().to_str().unwrap().to_owned();

        with_turn_context(test_turn_context(), async {
            let result = Ok(ToolAutoResult {
                kind: ToolAutoResultKind::Text,
                text: Some("Generated an image".into()),
                tool_calls: vec![],
                tool_results: vec![serde_json::json!({
                    "success": true,
                    "image_path": path,
                })],
                error: None,
                usage: make_usage(1000, 200),
            });

            let settings = AgentSettings::from_env();
            let text = process_agent_result(&tapes, tape_name, result, 100, &settings, None)
                .await
                .unwrap();
            assert_eq!(text, "Generated an image");

            let media = drain_outbound_media();
            assert_eq!(media.len(), 1, "media must be extracted from tool_results");
            assert_eq!(media[0].path, path);
        })
        .await;
    }
}