supercode-harness 0.4.5

The optional native Supercode agent and tool harness
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
//! Acceptance tests for SPEC.md TR-1 (T12 — model-invocable rehydration):
//! the `expand_reduction`/`sidecar_search` agent intrinsics.
//!
//! Each test is labeled with the TR-1.md acceptance-criterion id it proves.
//! Follows the ScriptedProvider/Recorder patterns of `tests/agent_loop.rs`,
//! `tests/tool_deferral.rs` (B6), and `tests/reduce_loop.rs` (A7/A9/A10).

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;

use async_trait::async_trait;
use supercode_harness::reduce::rehydrate::{
    expand_reduction, sidecar_search, ExpandOutcome, SidecarSearchResult,
};
use supercode_harness::reduce::{
    export_session, project_messages, reduction_id, ReductionKind, ReductionLog, ReductionPolicy,
    REDUCTION_SENTINEL,
};
use supercode_harness::session::{Session, SessionFormat};
use supercode_harness::sidecar::SidecarWriter;
use supercode_harness::{
    Agent, ChatMessage, ChatRequest, Config, FunctionCall, Provider, Role, ToolCall, Usage,
};

fn temp_dir(tag: &str) -> PathBuf {
    static N: AtomicUsize = AtomicUsize::new(0);
    let dir = std::env::temp_dir().join(format!(
        "supercode-rehydrate-{tag}-{}-{}",
        std::process::id(),
        N.fetch_add(1, Ordering::SeqCst)
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

/// Build an assistant message issuing one tool call, matching the
/// `tool_deferral.rs`/`reduce_loop.rs` inline-construction idiom (no public
/// `ChatMessage` constructor exists for an assistant tool call).
fn tool_call_msg(id: &str, name: &str, args: serde_json::Value) -> ChatMessage {
    ChatMessage {
        role: Role::Assistant,
        content: None,
        content_parts: None,
        tool_calls: Some(vec![ToolCall {
            id: id.to_string(),
            kind: "function".to_string(),
            function: FunctionCall {
                name: name.to_string(),
                arguments: args.to_string(),
            },
        }]),
        tool_call_id: None,
        name: None,
        metadata: Default::default(),
    }
}

/// A deterministic 20,000-byte tool output whose first 4,096 bytes are `Q`
/// (the A7 kept prefix) and which carries a distinctive `needle` string well
/// past that prefix (in the portion A7 hides).
fn big_output_with_needle(needle: &str) -> String {
    let mut s = "Q".repeat(4096);
    s.push_str(&"x".repeat(10_000));
    s.push_str(needle);
    s.push_str(&"y".repeat(20_000 - s.len()));
    debug_assert_eq!(s.len(), 20_000);
    s
}

/// A `list_dir`-shaped tool returning a fixed big output — same idiom as
/// `reduce_loop.rs::BigOutputTool`.
struct BigOutputTool(String);
#[async_trait]
impl supercode_harness::tools::Tool for BigOutputTool {
    fn name(&self) -> &str {
        "list_dir"
    }
    fn description(&self) -> &str {
        "x"
    }
    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({"type": "object"})
    }
    async fn execute(
        &self,
        _a: serde_json::Value,
        _c: &supercode_harness::tools::ToolContext,
    ) -> supercode_harness::Result<String> {
        Ok(self.0.clone())
    }
}

// ---------------------------------------------------------------------------
// dev/01 + dev/04: schemas advertised; sidecar_search finds hidden content;
// expand_reduction returns byte-exact content, whole and ranged.
// ---------------------------------------------------------------------------

/// Turn 0: call `list_dir` (the big-output tool). Turn 1: `sidecar_search`
/// for a needle buried in the hidden tail. Turn 2: `expand_reduction` (whole).
/// Turn 3: `expand_reduction` with a `byte_range`. Turn 4: plain answer.
struct SearchThenExpand {
    calls: AtomicUsize,
    found_id: Mutex<Option<String>>,
}
#[async_trait]
impl Provider for SearchThenExpand {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let n = self.calls.fetch_add(1, Ordering::SeqCst);
        match n {
            0 => Ok((
                tool_call_msg("c1", "list_dir", serde_json::json!({})),
                Usage::default(),
            )),
            1 => {
                // dev/01: a reduced session advertises both intrinsics.
                assert!(
                    req.tools.iter().any(|t| t.name == "expand_reduction"),
                    "expand_reduction must be advertised once a ReductionPolicy is installed"
                );
                assert!(
                    req.tools.iter().any(|t| t.name == "sidecar_search"),
                    "sidecar_search must be advertised once a ReductionPolicy is installed"
                );
                // The needle must not be visible in the wire body (it's past
                // the A7 kept prefix).
                let body = serde_json::to_string(&req.messages).unwrap();
                assert!(body.contains(REDUCTION_SENTINEL));
                assert!(!body.contains("NEEDLE-XYZ-123"));
                Ok((
                    tool_call_msg(
                        "s1",
                        "sidecar_search",
                        serde_json::json!({"query": "NEEDLE-XYZ-123"}),
                    ),
                    Usage::default(),
                ))
            }
            2 => {
                // dev/04: sidecar_search's result names the hiding reduction.
                let last = req.messages.last().unwrap();
                assert_eq!(last.role, Role::Tool);
                let result: SidecarSearchResult =
                    serde_json::from_str(last.content.as_deref().unwrap()).unwrap();
                assert_eq!(
                    result.matches.len(),
                    1,
                    "expected exactly one match: {result:?}"
                );
                assert!(!result.truncated);
                assert_eq!(result.matches[0].kind, "tool-output");
                assert!(result.matches[0].snippet.contains("NEEDLE-XYZ-123"));
                *self.found_id.lock().unwrap() = Some(result.matches[0].reduction_id.clone());
                Ok((
                    tool_call_msg(
                        "e1",
                        "expand_reduction",
                        serde_json::json!({"reduction_id": result.matches[0].reduction_id}),
                    ),
                    Usage::default(),
                ))
            }
            3 => {
                // Next call: fetch a `byte_range` slice of the same reduction
                // (byte-exactness of both calls is checked afterward, against
                // `agent.history()` — see the test body's comment on why that
                // beats inspecting THIS request's view: a further oversized,
                // just-created tool result is itself an immediate A7
                // candidate under `protect_last_n_tool_results: 0`, which is
                // the dev/05 behavior, not what this test is checking).
                let id = self.found_id.lock().unwrap().clone().unwrap();
                Ok((
                    tool_call_msg(
                        "e2",
                        "expand_reduction",
                        serde_json::json!({"reduction_id": id, "byte_range": [0, 4]}),
                    ),
                    Usage::default(),
                ))
            }
            _ => Ok((ChatMessage::assistant("done"), Usage::default())),
        }
    }
}

#[tokio::test]
async fn dev01_dev04_search_then_expand_whole_and_ranged() {
    let dir = temp_dir("search-expand");
    let original = big_output_with_needle("NEEDLE-XYZ-123");

    let config = Config::builder().cwd(dir.clone()).build();
    let mut reg = supercode_harness::tools::ToolRegistry::new();
    reg.register(BigOutputTool(original.clone()));
    let mut agent = Agent::with_parts(
        config,
        Box::new(SearchThenExpand {
            calls: AtomicUsize::new(0),
            found_id: Mutex::new(None),
        }),
        reg,
    );
    // `protect_last_n_tool_results: 0` so `list_dir`'s own output — the ONLY
    // tool result at the time `sidecar_search` needs to find it — is reduced
    // immediately (dev/04 depends on this: nothing to search until it is).
    agent.set_reduction_policy(ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 8192,
        protect_last_n_tool_results: 0,
        ..ReductionPolicy::default()
    });

    let reply = agent.send("investigate").await.unwrap();
    assert_eq!(reply, "done");

    // dev/01 byte-exactness, checked against `agent.history()` — the tool
    // result's OWN content the instant `run_expand_reduction` produced it,
    // not a later wire view (which a further, unrelated A7 pass is free to
    // re-truncate on a subsequent projection — that's dev/05, a different
    // property, exercised in its own test below).
    let result_for = |call_id: &str| {
        agent
            .history()
            .iter()
            .find(|m| m.role == Role::Tool && m.tool_call_id.as_deref() == Some(call_id))
            .unwrap_or_else(|| panic!("no tool result for call `{call_id}` in history"))
            .content
            .clone()
            .unwrap_or_default()
    };
    assert_eq!(
        result_for("e1"),
        original,
        "expand_reduction with no byte_range must return the exact original bytes"
    );
    // A ranged expand carries a one-line provenance header naming the slice
    // and the TRUE total (F2: the model needs the total to plan its next
    // slice), then exactly the requested bytes.
    let ranged = result_for("e2");
    let (header, body) = ranged
        .split_once('\n')
        .expect("ranged expand result should have a header line");
    assert!(
        header.contains("bytes 0..4 of 20000"),
        "ranged expand header must name the slice and the total: {header}"
    );
    assert_eq!(
        body, "QQQQ",
        "expand_reduction with byte_range=[0,4] must return exactly that slice"
    );

    std::fs::remove_dir_all(&dir).ok();
}

// ---------------------------------------------------------------------------
// dev/02: stub ids in the projected VIEW are sufficient, on their own, to
// expand every kind (A7/A8/A10) — extracted purely from stub text, the way a
// model reading only the view would.
// ---------------------------------------------------------------------------

fn read_call(id: &str, path: &Path) -> ChatMessage {
    ChatMessage {
        role: Role::Assistant,
        content: None,
        content_parts: None,
        tool_calls: Some(vec![ToolCall {
            id: id.to_string(),
            kind: "function".to_string(),
            function: FunctionCall {
                name: "read_file".to_string(),
                arguments: serde_json::json!({"path": path.to_string_lossy()}).to_string(),
            },
        }]),
        tool_call_id: None,
        name: None,
        metadata: Default::default(),
    }
}

/// All ids embedded in `[sc-reduced ...]` stub lines within `text` — exactly
/// what a model reading only the projected view could extract, with no
/// access to the `ReductionLog` struct itself.
fn stub_ids_in(text: &str) -> Vec<String> {
    let re = regex::Regex::new(r"r\d{4}-[0-9a-f]{4}").unwrap();
    re.find_iter(text).map(|m| m.as_str().to_string()).collect()
}

#[test]
fn dev02_stub_ids_are_sufficient_to_expand_every_kind() {
    let dir = temp_dir("dev02-stub-ids");
    let file_path = dir.join("f.txt");
    let fresh_content = "b".repeat(4096); // < 8192 trigger: never an A7 candidate.
    std::fs::write(&file_path, &fresh_content).unwrap();

    // 4 old (user, assistant) filler turns — the block A10 will clear.
    let mut msgs = Vec::new();
    for i in 0..4 {
        msgs.push(ChatMessage::user(format!("filler {i}")));
        msgs.push(ChatMessage::assistant(format!("filler reply {i}")));
    }
    let old_block_len = msgs.len(); // 8

    // Recent tail, never touched by A10: a fresh file read (A8 candidate)
    // and an oversized tool output (A7 candidate).
    msgs.push(read_call("rc1", &file_path));
    msgs.push(ChatMessage::tool_result(
        "rc1",
        "read_file",
        fresh_content.clone(),
    ));
    let big_output = "z".repeat(50_000);
    msgs.push(ChatMessage::tool_result(
        "bc1",
        "big_tool",
        big_output.clone(),
    ));
    msgs.push(ChatMessage::user("what did you find?"));
    msgs.push(ChatMessage::assistant("let me check"));
    let tail_len = msgs.len() - old_block_len; // 5

    // threshold/keep_recent chosen so the kept tail (5 messages) survives
    // whole and only the old block (8 messages) is cleared.
    let threshold = 10; // keep_recent = max(threshold/2, 2) = 5
    assert_eq!(tail_len, 5);

    let freshness = supercode_harness::reduce::probe_read_freshness(&msgs);
    let policy = ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 8192,
        protect_last_n_tool_results: 0,
        elide_stale_reads: true,
        read_freshness: freshness,
        clear_turns_older_than: Some(threshold),
        ..ReductionPolicy::default()
    };
    let (view, log) = project_messages(&msgs, &policy, &ReductionLog::default());

    // Sanity: exactly one of each kind present, none subsumed by A10.
    let kinds: Vec<&ReductionKind> = log.reductions.iter().map(|r| &r.kind).collect();
    assert!(kinds
        .iter()
        .any(|k| matches!(k, ReductionKind::ToolOutputTruncated { .. })));
    assert!(kinds
        .iter()
        .any(|k| matches!(k, ReductionKind::FileReadElided { .. })));
    assert!(kinds
        .iter()
        .any(|k| matches!(k, ReductionKind::TurnsCleared { .. })));
    assert_eq!(log.reductions.len(), 3, "{log:#?}");

    // The model's-eye view: render every message's visible text and extract
    // ids purely from stub TEXT (never touch `log` directly here).
    let rendered: String = view
        .iter()
        .map(|m| m.content.clone().unwrap_or_default())
        .collect::<Vec<_>>()
        .join("\n");
    let ids = stub_ids_in(&rendered);
    assert_eq!(ids.len(), 3, "expected one stub id per kind: {rendered}");

    for id in &ids {
        let outcome = expand_reduction(&log, &msgs, None, id, None)
            .unwrap_or_else(|e| panic!("expand_reduction({id}) failed: {e}"));
        let r = log.reductions.iter().find(|r| &r.id == id).unwrap();
        match &r.kind {
            ReductionKind::ToolOutputTruncated { .. } => {
                assert_eq!(outcome.content, big_output, "tool-output expand mismatch");
            }
            ReductionKind::FileReadElided { .. } => {
                assert_eq!(outcome.content, fresh_content, "file-read expand mismatch");
            }
            ReductionKind::TurnsCleared { .. } => {
                assert!(
                    outcome.content.contains("filler 0")
                        && outcome.content.contains("filler reply 3"),
                    "turns-cleared expand should render the cleared range: {}",
                    outcome.content
                );
            }
            other => panic!("unexpected kind: {other:?}"),
        }
    }

    std::fs::remove_dir_all(&dir).ok();
}

// ---------------------------------------------------------------------------
// dev/03: the expand_reduction call/result pair is honest transcript content
// — exports natively, no leak-guard special case, full-fidelity round trip.
// ---------------------------------------------------------------------------

/// Turn 0: call `list_dir`. Turn 1: `expand_reduction` on the resulting
/// stub. Turn 2: plain answer.
struct ExpandThenAnswer {
    calls: AtomicUsize,
}
#[async_trait]
impl Provider for ExpandThenAnswer {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let n = self.calls.fetch_add(1, Ordering::SeqCst);
        match n {
            0 => Ok((
                tool_call_msg("c1", "list_dir", serde_json::json!({})),
                Usage::default(),
            )),
            1 => {
                let body = serde_json::to_string(&req.messages).unwrap();
                let re = regex::Regex::new(r"r\d{4}-[0-9a-f]{4}").unwrap();
                let id = re.find(&body).expect("a stub id must be present").as_str();
                Ok((
                    tool_call_msg(
                        "e1",
                        "expand_reduction",
                        serde_json::json!({"reduction_id": id}),
                    ),
                    Usage::default(),
                ))
            }
            _ => Ok((ChatMessage::assistant("done"), Usage::default())),
        }
    }
}

#[tokio::test]
async fn dev03_expand_reduction_call_result_exports_honestly() {
    let dir = temp_dir("dev03-export");
    let sidecar_path = dir.join("sess.sidecar.jsonl");
    let original = "Q".repeat(20_000);

    let config = Config::builder().cwd(dir.clone()).build();
    let mut reg = supercode_harness::tools::ToolRegistry::new();
    reg.register(BigOutputTool(original.clone()));
    let mut agent = Agent::with_parts(
        config,
        Box::new(ExpandThenAnswer {
            calls: AtomicUsize::new(0),
        }),
        reg,
    );

    let empty_session = Session::from_claude_code_str("").unwrap();
    let writer = SidecarWriter::create(&sidecar_path, &empty_session).unwrap();
    agent.set_recorder(writer);
    agent.set_reduction_policy(ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 8192,
        protect_last_n_tool_results: 0,
        ..ReductionPolicy::default()
    });

    let reply = agent.send("go").await.unwrap();
    assert_eq!(reply, "done");

    // `agent.history()` carries the expand_reduction call and its (byte-exact)
    // result as an ordinary tool call/result pair.
    let expand_call = agent
        .history()
        .iter()
        .find_map(|m| {
            m.tool_calls()
                .iter()
                .find(|c| c.function.name == "expand_reduction")
        })
        .expect("expand_reduction call must be in history");
    let expand_result = agent
        .history()
        .iter()
        .find(|m| {
            m.role == Role::Tool && m.tool_call_id.as_deref() == Some(expand_call.id.as_str())
        })
        .expect("matching expand_reduction result must be in history");
    assert_eq!(expand_result.content.as_deref(), Some(original.as_str()));

    let sidecar_jsonl = std::fs::read_to_string(&sidecar_path).unwrap();

    for format in [SessionFormat::ClaudeCode, SessionFormat::Codex] {
        let exported = export_session(&sidecar_jsonl, format)
            .unwrap_or_else(|e| panic!("export_session({format:?}) failed: {e}"));
        assert!(
            !exported.contains(REDUCTION_SENTINEL),
            "export_session({format:?}) leaked the reduction sentinel — the intrinsic pair \
             should need no leak-guard special case:\n{exported}"
        );

        let reloaded = Session::load_str(&exported, format)
            .unwrap_or_else(|e| panic!("reloading export_session({format:?}) failed: {e}"));
        let reloaded_call = reloaded
            .messages
            .iter()
            .find_map(|m| {
                m.tool_calls()
                    .iter()
                    .find(|c| c.function.name == "expand_reduction")
                    .cloned()
            })
            .unwrap_or_else(|| panic!("{format:?} reload lost the expand_reduction call"));
        let reloaded_result = reloaded
            .messages
            .iter()
            .find(|m| m.tool_call_id.as_deref() == Some(reloaded_call.id.as_str()))
            .unwrap_or_else(|| panic!("{format:?} reload lost the matching tool result"));
        assert_eq!(
            reloaded_result.content.as_deref(),
            Some(original.as_str()),
            "{format:?}: exported/reloaded expand result must still be byte-exact"
        );
    }

    std::fs::remove_dir_all(&dir).ok();
}

// ---------------------------------------------------------------------------
// dev/05: expand results are reduction-eligible — an oversized expand result
// is itself truncated-with-stub (A7) by the ordinary projection pass once it
// lands in history, and that fresh stub is re-expandable. No new machinery:
// this is exactly what SPEC.md TR-1's "no leak-guard special case needed"
// line implies (`reduce::rehydrate` never mints its own stubs).
// ---------------------------------------------------------------------------

#[test]
fn dev05_oversized_expand_result_is_itself_a7_truncated_and_reexpandable() {
    let big = "m".repeat(200_000);
    let msgs = vec![ChatMessage::tool_result("call_1", "bash", big.clone())];

    let policy = ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 8192,
        protect_last_n_tool_results: 0,
        ..ReductionPolicy::default()
    };
    let (_view1, log1) = project_messages(&msgs, &policy, &ReductionLog::default());
    assert_eq!(log1.reductions.len(), 1);
    let original_id = log1.reductions[0].id.clone();

    // The model calls expand_reduction on the original — gets the full,
    // un-truncated 200 KB back (this function never caps on its own).
    let ExpandOutcome { content, .. } =
        expand_reduction(&log1, &msgs, None, &original_id, None).unwrap();
    assert_eq!(
        content, big,
        "expand_reduction must return the exact original bytes"
    );

    // Simulated agent behavior: the expand result lands as an ordinary new
    // tool result in history (exactly what `Agent::run_loop` does with any
    // tool's returned string).
    let mut msgs2 = msgs.clone();
    msgs2.push(ChatMessage::tool_result(
        "call_expand",
        "expand_reduction",
        content.clone(),
    ));
    let expand_result_idx = msgs2.len() - 1;

    // A LATER projection (protect_last_n_tool_results: 0, so nothing is
    // exempt for being "newest") re-truncates it exactly like any other
    // oversized tool output — no bespoke code path involved.
    let (view2, log2) = project_messages(&msgs2, &policy, &log1);
    assert_eq!(
        log2.reductions.len(),
        2,
        "the original reduction reapplies verbatim, plus a fresh one over the expand result"
    );
    let fresh = log2
        .reductions
        .iter()
        .find(|r| r.ptr.addr.index == expand_result_idx)
        .expect("a fresh reduction must cover the just-expanded, still-oversized tool result");
    assert_ne!(
        fresh.id, original_id,
        "the fresh reduction gets its own new id"
    );
    assert!(fresh.placeholder.starts_with(REDUCTION_SENTINEL));
    assert!(
        fresh.placeholder.contains(&fresh.id),
        "the fresh stub carries its own id: {}",
        fresh.placeholder
    );

    // No unbounded regrowth: the reduced VIEW's copy of the expand result is
    // small (bounded by `tool_output_keep_bytes` plus the stub), not 200 KB.
    let reduced_copy = view2[expand_result_idx].content.clone().unwrap();
    assert!(
        reduced_copy.len() < 10_000,
        "the re-truncated view must stay small: {} bytes",
        reduced_copy.len()
    );

    // And it, too, is re-expandable — using the FRESH id — recovering the
    // exact same 200 KB.
    let outcome2 = expand_reduction(&log2, &msgs2, None, &fresh.id, None).unwrap();
    assert_eq!(
        outcome2.content, big,
        "re-expanding the fresh stub must be byte-exact"
    );
}

// ---------------------------------------------------------------------------
// dev/06: Tier-2 demo — a scripted agent loop where the model hits a
// question answerable only from A10-cleared content, calls `sidecar_search`
// then `expand_reduction`, and answers correctly. Transcript archived below.
// ---------------------------------------------------------------------------

const SECRET: &str = "the deploy key is DK-771-ZQ";

/// Turn 0: user shares a secret, model acknowledges. Turns 1-3: plain filler
/// exchanges (grows history past the A10 `compact_after_messages` threshold,
/// clearing the secret's turn out of the view). Turn 4 (the question, model
/// call n=4): the model can't see the secret directly — it calls
/// `sidecar_search`, then (n=5) `expand_reduction` on the match, then (n=6)
/// answers using the recovered content.
struct Tier2Demo {
    calls: AtomicUsize,
}
#[async_trait]
impl Provider for Tier2Demo {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let n = self.calls.fetch_add(1, Ordering::SeqCst);
        match n {
            0 => Ok((
                ChatMessage::assistant("Noted, I'll remember that."),
                Usage::default(),
            )),
            1..=3 => Ok((ChatMessage::assistant(format!("ack {n}")), Usage::default())),
            4 => {
                assert!(req.tools.iter().any(|t| t.name == "sidecar_search"));
                assert!(req.tools.iter().any(|t| t.name == "expand_reduction"));
                let body = serde_json::to_string(&req.messages).unwrap();
                assert!(
                    body.contains(REDUCTION_SENTINEL),
                    "the secret's turn must have been A10-cleared by now: {body}"
                );
                assert!(
                    !body.contains("DK-771-ZQ"),
                    "the secret must not be directly visible in the reduced view"
                );
                Ok((
                    tool_call_msg(
                        "s1",
                        "sidecar_search",
                        serde_json::json!({"query": "deploy key"}),
                    ),
                    Usage::default(),
                ))
            }
            5 => {
                let last = req.messages.last().unwrap();
                assert_eq!(last.role, Role::Tool);
                let result: SidecarSearchResult =
                    serde_json::from_str(last.content.as_deref().unwrap()).unwrap();
                assert_eq!(result.matches.len(), 1, "{result:?}");
                Ok((
                    tool_call_msg(
                        "e1",
                        "expand_reduction",
                        serde_json::json!({"reduction_id": result.matches[0].reduction_id}),
                    ),
                    Usage::default(),
                ))
            }
            _ => {
                let last = req.messages.last().unwrap();
                assert_eq!(last.role, Role::Tool);
                let expanded = last.content.clone().unwrap_or_default();
                assert!(
                    expanded.contains("DK-771-ZQ"),
                    "expand_reduction must have recovered the secret: {expanded}"
                );
                Ok((
                    ChatMessage::assistant("The deploy key is DK-771-ZQ."),
                    Usage::default(),
                ))
            }
        }
    }
}

#[tokio::test]
async fn dev06_tier2_demo_sidecar_search_then_expand_answers_from_cleared_turns() {
    let dir = temp_dir("dev06-demo");
    let sidecar_path = dir.join("demo.sidecar.jsonl");

    let config = Config::builder()
        .cwd(dir.clone())
        .compact_after_messages(6) // keep_recent = max(6/2, 2) = 3
        .build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(Tier2Demo {
            calls: AtomicUsize::new(0),
        }),
    );

    let empty_session = Session::from_claude_code_str("").unwrap();
    let writer = SidecarWriter::create(&sidecar_path, &empty_session).unwrap();
    agent.set_recorder(writer);
    agent.set_reduction_policy(ReductionPolicy::default());

    agent
        .send(format!("Please remember this: {SECRET}"))
        .await
        .unwrap();
    for i in 0..3 {
        agent
            .send(format!("filler turn {i}, just say ack"))
            .await
            .unwrap();
    }
    // A10 must have cleared the secret's turn out of the log by now.
    assert!(
        agent
            .reduction_log()
            .reductions
            .iter()
            .any(|r| matches!(r.kind, ReductionKind::TurnsCleared { .. })),
        "expected a TurnsCleared reduction before the question turn: {:?}",
        agent.reduction_log()
    );

    let reply = agent.send("What was the deploy key?").await.unwrap();
    assert_eq!(reply, "The deploy key is DK-771-ZQ.");

    // Archive the demo transcript (git-ignored `target/`; referenced in the
    // TR-1 build report) — every message the scripted loop actually
    // exchanged, in order, proving the sidecar_search -> expand_reduction ->
    // correct-answer chain end to end.
    let workspace_target = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target");
    std::fs::create_dir_all(&workspace_target).ok();
    let transcript_path = workspace_target.join("tr1-demo-transcript.jsonl");
    agent.save_transcript(&transcript_path).unwrap();
    assert!(transcript_path.exists());
    eprintln!(
        "dev/06 demo transcript archived at {}",
        transcript_path.display()
    );

    std::fs::remove_dir_all(&dir).ok();
}

// ---------------------------------------------------------------------------
// B1 (fix pass) / TR-12: a tool output over `max_tool_output_bytes` (default
// 100 KB), with a live recorder + policy both active. Pre-TR-12, this
// diverged history from the sidecar (history kept a `cap_tool_output`-capped
// copy; the recorder kept the full bytes), and the intrinsics needed the
// `recorded`-upgrade path (`reduce::rehydrate`'s two-source resolution) to
// reach past the cap. TR-12's D6/A7 supersession gate (`Agent::run_loop`)
// means a recorder+policy pair now keeps `cap_tool_output` off entirely, so
// for a freshly-recorded session like this one `history` already holds the
// full bytes and never diverges from the sidecar — the `recorded` upgrade is
// exercised here but is a provable no-op (`recorded == minted`). The
// two-source resolution itself remains load-bearing as defense-in-depth for
// a LEGACY sidecar recorded before this gate existed (unit-tested directly,
// with a hand-built capped/full divergence, in
// `crates/harness/src/reduce/rehydrate.rs`'s
// `recorded_copy_supersedes_a_capped_minted_copy`).
// ---------------------------------------------------------------------------

/// A 150,000-byte output: 4,096 `Q`s (the A7 kept prefix), filler, then a
/// needle at byte 120,000 — past BOTH the A7 keep boundary and the 100 KB
/// `cap_tool_output` cap, so only the recorded (sidecar) copy contains it.
fn over_cap_output_with_needle(needle: &str) -> String {
    let mut s = "Q".repeat(4096);
    s.push_str(&"x".repeat(120_000 - s.len()));
    s.push_str(needle);
    s.push_str(&"y".repeat(150_000 - s.len()));
    s
}

/// Turn 0: call the 150 KB tool. Turn 1: `sidecar_search` for the needle
/// beyond the cap. Turn 2: `expand_reduction` on the match. Turn 3: answer.
struct OverCapSearchThenExpand {
    calls: AtomicUsize,
}
#[async_trait]
impl Provider for OverCapSearchThenExpand {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let n = self.calls.fetch_add(1, Ordering::SeqCst);
        match n {
            0 => Ok((
                tool_call_msg("c1", "list_dir", serde_json::json!({})),
                Usage::default(),
            )),
            1 => {
                let body = serde_json::to_string(&req.messages).unwrap();
                assert!(body.contains(REDUCTION_SENTINEL));
                assert!(
                    !body.contains("TAIL-NEEDLE-B1"),
                    "the needle lives past the cap; it must not be visible in the view"
                );
                Ok((
                    tool_call_msg(
                        "s1",
                        "sidecar_search",
                        serde_json::json!({"query": "TAIL-NEEDLE-B1"}),
                    ),
                    Usage::default(),
                ))
            }
            2 => {
                // The needle sits at byte 120,000 — beyond the 100 KB capped
                // history copy. Finding it proves search resolved the
                // recorder's full bytes, not history's capped copy.
                let last = req.messages.last().unwrap();
                assert_eq!(last.role, Role::Tool);
                let result: SidecarSearchResult =
                    serde_json::from_str(last.content.as_deref().unwrap()).unwrap();
                assert_eq!(
                    result.matches.len(),
                    1,
                    "search must reach past the cap into the recorded bytes: {result:?}"
                );
                assert!(result.matches[0].snippet.contains("TAIL-NEEDLE-B1"));
                Ok((
                    tool_call_msg(
                        "e1",
                        "expand_reduction",
                        serde_json::json!({"reduction_id": result.matches[0].reduction_id}),
                    ),
                    Usage::default(),
                ))
            }
            _ => Ok((ChatMessage::assistant("done"), Usage::default())),
        }
    }
}

#[tokio::test]
async fn b1_over_cap_output_expands_to_full_recorded_bytes() {
    let dir = temp_dir("b1-over-cap");
    let sidecar_path = dir.join("sess.sidecar.jsonl");
    let original = over_cap_output_with_needle("TAIL-NEEDLE-B1");
    assert_eq!(original.len(), 150_000);

    // DEFAULT config: `max_tool_output_bytes` stays at its 100 KB default —
    // pre-TR-12 this was exactly the regime where history and sidecar
    // diverged; post-TR-12, the D6/A7 gate keeps `cap_tool_output` off here
    // (recorder + policy both installed below), so they no longer do.
    let config = Config::builder().cwd(dir.clone()).build();
    let mut reg = supercode_harness::tools::ToolRegistry::new();
    reg.register(BigOutputTool(original.clone()));
    let mut agent = Agent::with_parts(
        config,
        Box::new(OverCapSearchThenExpand {
            calls: AtomicUsize::new(0),
        }),
        reg,
    );

    let empty_session = Session::from_claude_code_str("").unwrap();
    let writer = SidecarWriter::create(&sidecar_path, &empty_session).unwrap();
    agent.set_recorder(writer);
    agent.set_reduction_policy(ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 8192,
        protect_last_n_tool_results: 0,
        ..ReductionPolicy::default()
    });

    let reply = agent.send("go").await.unwrap();
    assert_eq!(reply, "done");

    // history's copy of the 150 KB output is now the FULL original — the
    // D6/A7 gate (TR-12) keeps `cap_tool_output` off whenever a recorder and
    // a policy are both active, so `history` never diverges from the
    // sidecar for a freshly-recorded session like this one. No cap notice is
    // ever appended: `history` holds exactly what the tool returned.
    let history_copy = agent
        .history()
        .iter()
        .find(|m| m.role == Role::Tool && m.tool_call_id.as_deref() == Some("c1"))
        .and_then(|m| m.content.clone())
        .unwrap();
    assert_eq!(
        history_copy.len(),
        original.len(),
        "the gate must keep the FULL bytes in history, not a capped prefix"
    );
    assert_eq!(history_copy, original);
    assert!(
        !history_copy.contains("bytes total, showing first"),
        "no cap notice should ever be appended once the D6/A7 gate is active: {}",
        &history_copy[history_copy.len().saturating_sub(200)..]
    );

    // The expand result — resolved from `history[1..]` (now full, since the
    // gate is on) and cross-checked against the recorder's reload — is the
    // full 150 KB original, byte-exact, whether or not the `recorded` upgrade
    // path ever had to do anything (it doesn't, here — see the comment
    // above this test).
    let sidecar_raw = std::fs::read_to_string(&sidecar_path).unwrap();
    let recorded = Session::from_native_str(&sidecar_raw).unwrap();
    let expand_result = recorded
        .messages
        .iter()
        .find(|m| m.role == Role::Tool && m.tool_call_id.as_deref() == Some("e1"))
        .and_then(|m| m.content.clone())
        .expect("the expand result must be recorded in the sidecar");
    assert_eq!(
        expand_result, original,
        "expand_reduction must return the FULL recorded bytes, not the capped history copy"
    );

    std::fs::remove_dir_all(&dir).ok();
}

// ---------------------------------------------------------------------------
// B3 (fix pass): malformed byte_range shapes error loudly through the real
// agent dispatch — never a silent whole-content (or empty) return.
// ---------------------------------------------------------------------------

/// Issues one `expand_reduction` call per malformed `byte_range` shape,
/// asserting the previous call's tool result was a recoverable error naming
/// `byte_range` (and, where reachable, the true total).
struct MalformedRangeProbe {
    calls: AtomicUsize,
    found_id: Mutex<Option<String>>,
}

impl MalformedRangeProbe {
    /// Every malformed shape probed, in order: wrong arity (both ways),
    /// non-integers, negatives, floats — all shape errors — then a reversed
    /// range (well-formed shape, semantically invalid).
    fn shapes() -> Vec<serde_json::Value> {
        vec![
            serde_json::json!([1]),
            serde_json::json!([1, 2, 3]),
            serde_json::json!(["a", "b"]),
            serde_json::json!([-5, 10]),
            serde_json::json!([1.5, 2]),
            serde_json::json!([100, 5]),
        ]
    }
}

#[async_trait]
impl Provider for MalformedRangeProbe {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let n = self.calls.fetch_add(1, Ordering::SeqCst);
        let shapes = Self::shapes();
        if n == 0 {
            return Ok((
                tool_call_msg("c1", "list_dir", serde_json::json!({})),
                Usage::default(),
            ));
        }
        if n == 1 {
            // Pull the freshly-minted reduction id out of the request view's
            // stub text, the way a model would.
            let body = serde_json::to_string(&req.messages).unwrap();
            let re = regex::Regex::new(r"r\d{4}-[0-9a-f]{4}").unwrap();
            let id = re.find(&body).expect("a stub id must be present").as_str();
            *self.found_id.lock().unwrap() = Some(id.to_string());
        } else {
            // Every probe's reply must be a recoverable error naming
            // byte_range — and for these all-shape-error cases (probes
            // before the last), the true total too.
            let last = req.messages.last().unwrap();
            assert_eq!(last.role, Role::Tool);
            let content = last.content.as_deref().unwrap_or_default();
            assert!(
                content.starts_with("Error:"),
                "probe {} must produce an error result: {content}",
                n - 2
            );
            assert!(
                content.contains("byte_range"),
                "probe {} error must name byte_range: {content}",
                n - 2
            );
            let is_reversed_probe = n - 2 == shapes.len() - 1;
            if is_reversed_probe {
                assert!(
                    content.contains("reversed"),
                    "the reversed-range error must say so: {content}"
                );
            }
            assert!(
                content.contains("20000"),
                "probe {} error must name the true total: {content}",
                n - 2
            );
        }
        let probe = n - 1;
        if probe < shapes.len() {
            let id = self.found_id.lock().unwrap().clone().unwrap();
            Ok((
                tool_call_msg(
                    &format!("p{probe}"),
                    "expand_reduction",
                    serde_json::json!({"reduction_id": id, "byte_range": shapes[probe]}),
                ),
                Usage::default(),
            ))
        } else {
            Ok((ChatMessage::assistant("survived"), Usage::default()))
        }
    }
}

#[tokio::test]
async fn b3_malformed_byte_ranges_error_recoverably_through_dispatch() {
    let dir = temp_dir("b3-ranges");
    let config = Config::builder().cwd(dir.clone()).build();
    let mut reg = supercode_harness::tools::ToolRegistry::new();
    reg.register(BigOutputTool("Q".repeat(20_000)));
    let mut agent = Agent::with_parts(
        config,
        Box::new(MalformedRangeProbe {
            calls: AtomicUsize::new(0),
            found_id: Mutex::new(None),
        }),
        reg,
    );
    agent.set_reduction_policy(ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 8192,
        protect_last_n_tool_results: 0,
        ..ReductionPolicy::default()
    });

    // The loop must survive every malformed probe and reach the final
    // answer — errors are recoverable tool results, not aborts.
    let reply = agent.send("probe").await.unwrap();
    assert_eq!(reply, "survived");

    std::fs::remove_dir_all(&dir).ok();
}

// ---------------------------------------------------------------------------
// B2/F4 (fix pass): empty sidecar_search queries, unknown reduction ids, and
// unparseable arguments all surface as recoverable is_error tool results
// through the real dispatch — the loop continues every time.
// ---------------------------------------------------------------------------

/// n=0: expand_reduction with an unknown id. n=1: expand_reduction with
/// arguments that aren't JSON at all. n=2: sidecar_search with no `query`.
/// n=3: sidecar_search with a whitespace query. n=4: final answer. Each
/// step first asserts the previous step's result was an `Error:` tool
/// result carrying the expected hint.
struct ErrorPathProbe {
    calls: AtomicUsize,
}
#[async_trait]
impl Provider for ErrorPathProbe {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let n = self.calls.fetch_add(1, Ordering::SeqCst);
        let last_error = |hint: &str| {
            let last = req.messages.last().unwrap();
            assert_eq!(last.role, Role::Tool);
            let content = last.content.as_deref().unwrap_or_default();
            assert!(
                content.starts_with("Error:"),
                "expected an error result, got: {content}"
            );
            assert!(
                content.contains(hint),
                "error should mention `{hint}`: {content}"
            );
        };
        match n {
            0 => Ok((
                tool_call_msg(
                    "x1",
                    "expand_reduction",
                    serde_json::json!({"reduction_id": "r9999-dead"}),
                ),
                Usage::default(),
            )),
            1 => {
                last_error("r9999-dead");
                // Arguments that fail JSON parsing entirely.
                let call = ChatMessage {
                    role: Role::Assistant,
                    content: None,
                    content_parts: None,
                    tool_calls: Some(vec![ToolCall {
                        id: "x2".to_string(),
                        kind: "function".to_string(),
                        function: FunctionCall {
                            name: "expand_reduction".to_string(),
                            arguments: "not json".to_string(),
                        },
                    }]),
                    tool_call_id: None,
                    name: None,
                    metadata: Default::default(),
                };
                Ok((call, Usage::default()))
            }
            2 => {
                last_error("expand_reduction");
                Ok((
                    tool_call_msg("x3", "sidecar_search", serde_json::json!({})),
                    Usage::default(),
                ))
            }
            3 => {
                last_error("query");
                Ok((
                    tool_call_msg("x4", "sidecar_search", serde_json::json!({"query": "   "})),
                    Usage::default(),
                ))
            }
            _ => {
                last_error("query");
                Ok((ChatMessage::assistant("recovered"), Usage::default()))
            }
        }
    }
}

#[tokio::test]
async fn b2_f4_error_paths_are_recoverable_through_the_loop() {
    let dir = temp_dir("error-paths");
    let config = Config::builder().cwd(dir.clone()).build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(ErrorPathProbe {
            calls: AtomicUsize::new(0),
        }),
    );
    agent.set_reduction_policy(ReductionPolicy::default());

    let reply = agent.send("probe the error paths").await.unwrap();
    assert_eq!(reply, "recovered");

    std::fs::remove_dir_all(&dir).ok();
}

// ---------------------------------------------------------------------------
// B4 (fix pass): content living ONLY in tool-call arguments inside an
// A10-cleared range is both searchable and present in the expansion.
// ---------------------------------------------------------------------------

#[test]
fn b4_cleared_tool_call_arguments_are_searchable_and_expandable() {
    // An old block containing a write_file call whose file body exists
    // NOWHERE except the call's arguments, plus its result — then filler so
    // the block ages past the clear threshold.
    let mut msgs = vec![
        ChatMessage::user("write my notes file"),
        tool_call_msg(
            "w1",
            "write_file",
            serde_json::json!({
                "path": "notes.txt",
                "content": "ARGS-ONLY-PAYLOAD-77: the real content of the file"
            }),
        ),
        ChatMessage::tool_result("w1", "write_file", "ok, wrote notes.txt"),
        ChatMessage::assistant("Written."),
    ];
    for i in 0..4 {
        msgs.push(ChatMessage::user(format!("filler {i}")));
        msgs.push(ChatMessage::assistant(format!("filler reply {i}")));
    }

    // threshold 8 -> keep_recent = 4: the write_file block (first 4
    // messages) is cleared; the last 4 filler messages survive.
    let policy = ReductionPolicy {
        clear_turns_older_than: Some(8),
        ..ReductionPolicy::default()
    };
    let (_view, log) = project_messages(&msgs, &policy, &ReductionLog::default());
    let cleared = log
        .reductions
        .iter()
        .find(|r| matches!(r.kind, ReductionKind::TurnsCleared { .. }))
        .expect("the old block must have been cleared");

    // Searchable: the payload exists only in the cleared call's ARGUMENTS.
    let hits = sidecar_search(&log, &msgs, None, "ARGS-ONLY-PAYLOAD-77").unwrap();
    assert_eq!(hits.matches.len(), 1, "{hits:?}");
    assert_eq!(hits.matches[0].reduction_id, cleared.id);
    assert!(hits.matches[0].snippet.contains("ARGS-ONLY-PAYLOAD-77"));

    // Expandable: the expansion carries the tool call's id, name, full
    // arguments, and the tool result's attribution.
    let outcome = expand_reduction(&log, &msgs, None, &cleared.id, None).unwrap();
    assert!(
        outcome.content.contains("ARGS-ONLY-PAYLOAD-77"),
        "expansion must include argument-only content: {}",
        outcome.content
    );
    assert!(outcome.content.contains("write_file"));
    assert!(outcome.content.contains("w1"));
    assert!(outcome.content.contains("tool_call_id=w1"));
    assert!(outcome.content.contains("ok, wrote notes.txt"));
}

// ---------------------------------------------------------------------------
// B5 (fix pass): the intrinsics are advertised ONLY when a ReductionPolicy
// is installed — the negative case, so the gate can't silently regress to
// unconditional advertising.
// ---------------------------------------------------------------------------

#[tokio::test]
async fn b5_intrinsics_absent_without_a_reduction_policy() {
    // No policy: neither intrinsic may be advertised.
    let names = std::sync::Arc::new(Mutex::new(None));
    /// Records the tool names of the first request, then answers.
    struct Shared(std::sync::Arc<Mutex<Option<Vec<String>>>>);
    #[async_trait]
    impl Provider for Shared {
        async fn complete(
            &self,
            req: &ChatRequest,
            _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
        ) -> supercode_harness::Result<(ChatMessage, Usage)> {
            let mut names = self.0.lock().unwrap();
            if names.is_none() {
                *names = Some(req.tools.iter().map(|t| t.name.clone()).collect());
            }
            Ok((ChatMessage::assistant("ok"), Usage::default()))
        }
    }

    let config = Config::builder().build();
    let mut agent = Agent::with_provider(config, Box::new(Shared(names.clone())));
    agent.send("hi").await.unwrap();
    let seen = names.lock().unwrap().clone().unwrap();
    assert!(
        !seen.iter().any(|n| n == "expand_reduction"),
        "expand_reduction must not be advertised without a policy: {seen:?}"
    );
    assert!(
        !seen.iter().any(|n| n == "sidecar_search"),
        "sidecar_search must not be advertised without a policy: {seen:?}"
    );

    // Same agent shape WITH a policy: both appear (the positive control,
    // proving this test would catch a regression in either direction).
    let names2 = std::sync::Arc::new(Mutex::new(None));
    let config = Config::builder().build();
    let mut agent = Agent::with_provider(config, Box::new(Shared(names2.clone())));
    agent.set_reduction_policy(ReductionPolicy::default());
    agent.send("hi").await.unwrap();
    let seen = names2.lock().unwrap().clone().unwrap();
    assert!(seen.iter().any(|n| n == "expand_reduction"), "{seen:?}");
    assert!(seen.iter().any(|n| n == "sidecar_search"), "{seen:?}");
}

// ---------------------------------------------------------------------------
// F1 (fix pass): an ImageRedacted reduction's id is reachable from the
// model's-eye view (image stubs land in `content_parts` as a text part) and
// expands to the original data: URL.
// ---------------------------------------------------------------------------

#[test]
fn f1_image_reduction_id_reachable_from_view_and_expands() {
    // A 20 KB data: URL — over the default `image_redact_min_bytes` (8192).
    let data_url = format!("data:image/png;base64,{}", "A".repeat(20_000));
    let msgs = vec![
        ChatMessage::user_with_images("look at this screenshot", std::slice::from_ref(&data_url)),
        ChatMessage::assistant("Looking."),
    ];

    // Default policy: `redact_images` is on by default.
    let policy = ReductionPolicy::default();
    let (view, log) = project_messages(&msgs, &policy, &ReductionLog::default());
    assert_eq!(log.reductions.len(), 1, "{log:#?}");
    assert!(matches!(
        log.reductions[0].kind,
        ReductionKind::ImageRedacted { .. }
    ));

    // The model's-eye rendering MUST include `content_parts` text — that is
    // where image stubs land (the redacted part becomes a `{"type":"text"}`
    // part carrying the stub). Extract the id from that rendering alone.
    let rendered: String = view
        .iter()
        .map(|m| {
            let mut s = m.content.clone().unwrap_or_default();
            for part in m.content_parts.as_deref().unwrap_or_default() {
                if let Some(text) = part.get("text").and_then(|t| t.as_str()) {
                    s.push('\n');
                    s.push_str(text);
                }
            }
            s
        })
        .collect::<Vec<_>>()
        .join("\n");
    assert!(
        !rendered.contains(&data_url),
        "the data URL itself must be gone from the view"
    );
    let ids = stub_ids_in(&rendered);
    assert_eq!(
        ids.len(),
        1,
        "the image stub id must be visible in the model's view: {rendered}"
    );
    assert_eq!(ids[0], log.reductions[0].id);

    // Expanding that id returns the original data: URL, byte-exact.
    let outcome = expand_reduction(&log, &msgs, None, &ids[0], None).unwrap();
    assert_eq!(outcome.content, data_url);
    assert_eq!(outcome.total_bytes, data_url.len());
}

// ---------------------------------------------------------------------------
// TR-3 dev/06: a `FileReadDiffed` stub carries its `reduction_id`, visible in
// the model's-eye view purely from stub text (same idiom as dev/02, above),
// and `expand_reduction` on that id returns the full, verbatim re-read — not
// the diff, not the base — byte for byte.
// ---------------------------------------------------------------------------

#[test]
fn tr3_dev06_file_read_diffed_stub_carries_id_and_expands_to_full_re_read() {
    use std::fmt::Write as _;

    let file_path = PathBuf::from("/workspace/src/tr3_dev06.rs");
    let mut base_content = String::with_capacity(5000);
    for i in 0..500 {
        writeln!(base_content, "line {i:04}").unwrap();
    }
    let mut new_content = String::with_capacity(base_content.len());
    for (i, line) in base_content.lines().enumerate() {
        if (250..252).contains(&i) {
            writeln!(new_content, "CHANGED {i}").unwrap();
        } else {
            writeln!(new_content, "{line}").unwrap();
        }
    }
    assert_ne!(base_content, new_content);

    let msgs = vec![
        ChatMessage::user("read the file"),
        read_call("rd1", &file_path),
        ChatMessage::tool_result("rd1", "read_file", base_content.clone()),
        ChatMessage::user("small edit"),
        ChatMessage::assistant("done"),
        read_call("rd2", &file_path),
        ChatMessage::tool_result("rd2", "read_file", new_content.clone()),
    ];
    let new_idx = 6;

    let policy = ReductionPolicy {
        tool_output_trigger_bytes: usize::MAX, // isolate TR-3 from A7
        protect_last_n_tool_results: 0,
        ..ReductionPolicy::default() // diff_rereads: true (default)
    };
    let (view, log) = project_messages(&msgs, &policy, &ReductionLog::default());

    assert_eq!(log.reductions.len(), 1, "{log:#?}");
    let r = &log.reductions[0];
    assert!(
        matches!(r.kind, ReductionKind::FileReadDiffed { .. }),
        "expected FileReadDiffed, got {:?}",
        r.kind
    );

    // The model's-eye view: the stub id is recoverable from stub TEXT alone.
    let rendered: String = view
        .iter()
        .map(|m| m.content.clone().unwrap_or_default())
        .collect::<Vec<_>>()
        .join("\n");
    let ids = stub_ids_in(&rendered);
    assert_eq!(ids.len(), 1, "{rendered}");
    assert_eq!(ids[0], r.id);
    assert_eq!(reduction_id(&view[new_idx]), Some(r.id.as_str()));

    // expand_reduction returns the full VERBATIM re-read -- not the diff
    // text, not the base's content.
    let outcome = expand_reduction(&log, &msgs, None, &ids[0], None).unwrap();
    assert_eq!(outcome.content, new_content);
    assert_eq!(outcome.total_bytes, new_content.len());
    assert_ne!(outcome.content, base_content);
}