supercode-harness 0.4.15

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
//! BP-7 (`.volter/tracker/markdown/BP-7.md`) — agent-loop records and modes,
//! proven as EXECUTED BEHAVIOUR over the RESOLVED `cc-parity` / `cx-parity`
//! presets.
//!
//! Every test here starts from `extends = "<preset>"` through the real
//! resolver (`configfile::resolve`), builds an [`Agent`] over the resolved
//! `Config`, and drives it with a mock provider — never a unit of an
//! unwired function, and never a hand-built `Config` that could disagree
//! with what the preset actually resolves to. The one exception is the
//! retry test, which points the REAL `Agent::new` HTTP path at a local
//! socket that answers 503 once: retry is a transport behaviour, so a mock
//! provider could not prove it.

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use supercode_harness::configfile::{resolve, ResolveOptions};
use supercode_harness::store::SessionStore;
use supercode_harness::turn_record::{FinishReason, TurnMarker};
use supercode_harness::{
    Agent, AgentEvent, ChatMessage, ChatRequest, Config, FunctionCall, Provider, ToolCall, Usage,
};

/// Every preset this build must hold under, in ledger-column order.
const PARITY_PRESETS: &[&str] = &["cc-parity", "cx-parity"];

/// The resolved `Config` for a built-in preset, through the real resolver —
/// plus the two credentials `Agent` needs and no other change, so what is
/// under test is the preset's own resolution.
fn preset_config(name: &str) -> Config {
    let top = format!("extends = \"{name}\"\n");
    let resolved = resolve(&top, None, &ResolveOptions { strict: true })
        .unwrap_or_else(|e| panic!("preset `{name}` failed to resolve: {e}"));
    let mut config = resolved.config;
    config.api_key = Some("test-key".to_string());
    config.base_url = "http://127.0.0.1:1".to_string();
    if config.model.is_empty() {
        // cx-parity pins no model (Codex's default is account-resolved);
        // the loop needs a name to put on the wire and in every record.
        config.model = "openai/gpt-5-codex".to_string();
    }
    config
}

fn temp_dir(tag: &str) -> std::path::PathBuf {
    use std::sync::atomic::AtomicU64;
    static N: AtomicU64 = AtomicU64::new(0);
    let dir = std::env::temp_dir().join(format!(
        "sc-bp7-{tag}-{}-{}",
        std::process::id(),
        N.fetch_add(1, Ordering::SeqCst)
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

// ---------------------------------------------------------------------------
// Mock providers
// ---------------------------------------------------------------------------

/// Answers with plain text, recording every request it was handed.
struct Recorder {
    requests: Arc<Mutex<Vec<ChatRequest>>>,
    reply: String,
    usage: Usage,
}

impl Recorder {
    fn new(reply: &str) -> (Self, Arc<Mutex<Vec<ChatRequest>>>) {
        let requests = Arc::new(Mutex::new(Vec::new()));
        (
            Recorder {
                requests: requests.clone(),
                reply: reply.to_string(),
                usage: Usage {
                    prompt_tokens: 1_000_000,
                    completion_tokens: 1_000_000,
                    total_tokens: 2_000_000,
                    prompt_tokens_details: None,
                },
            },
            requests,
        )
    }
}

#[async_trait]
impl Provider for Recorder {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        self.requests.lock().unwrap().push(req.clone());
        Ok((
            ChatMessage::assistant(self.reply.clone()),
            self.usage.clone(),
        ))
    }
}

/// Asks for `calls_per_turn` tool calls on every turn but the last, so a
/// step/spend cap has something to bite on.
struct ToolCaller {
    turns: AtomicUsize,
    calls_per_turn: usize,
    usage: Usage,
}

impl ToolCaller {
    fn new(calls_per_turn: usize, completion_tokens: u64) -> Self {
        ToolCaller {
            turns: AtomicUsize::new(0),
            calls_per_turn,
            usage: Usage {
                prompt_tokens: 0,
                completion_tokens,
                total_tokens: completion_tokens,
                prompt_tokens_details: None,
            },
        }
    }
}

#[async_trait]
impl Provider for ToolCaller {
    async fn complete(
        &self,
        _req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let n = self.turns.fetch_add(1, Ordering::SeqCst);
        let calls: Vec<ToolCall> = (0..self.calls_per_turn)
            .map(|i| ToolCall {
                id: format!("call_{n}_{i}"),
                kind: "function".into(),
                function: FunctionCall {
                    name: "no_such_tool".into(),
                    arguments: "{}".into(),
                },
            })
            .collect();
        let mut msg = ChatMessage::assistant("working");
        msg.tool_calls = Some(calls);
        Ok((msg, self.usage.clone()))
    }
}

// ===========================================================================
// turn-step-bracketing-records — persisted per-round-trip markers
// ===========================================================================

#[tokio::test]
async fn every_round_trip_writes_context_usage_and_finish_markers_under_both_presets() {
    for preset in PARITY_PRESETS {
        let (provider, _seen) = Recorder::new("done");
        let mut agent = Agent::with_provider(preset_config(preset), Box::new(provider));
        agent.send("hello").await.unwrap();

        let kinds: Vec<&TurnMarker> = agent.turn_records().iter().map(|r| &r.marker).collect();
        assert!(
            matches!(kinds.first(), Some(TurnMarker::Context { messages, .. }) if *messages >= 2),
            "{preset}: the round-trip must OPEN with a context marker: {kinds:?}"
        );
        assert!(
            kinds
                .iter()
                .any(|m| matches!(m, TurnMarker::Usage { total_tokens, .. } if *total_tokens > 0)),
            "{preset}: no usage marker: {kinds:?}"
        );
        assert!(
            matches!(
                kinds.last(),
                Some(TurnMarker::Finish {
                    reason: FinishReason::EndTurn
                })
            ),
            "{preset}: the round-trip must CLOSE with a finish marker: {kinds:?}"
        );
        for record in agent.turn_records() {
            assert_eq!(record.turn, 0, "{preset}: one round-trip, turn 0");
            assert_eq!(record.model, agent.model(), "{preset}: model provenance");
        }
    }
}

#[tokio::test]
async fn the_marker_log_persists_beside_the_session_and_reloads_losslessly() {
    for preset in PARITY_PRESETS {
        let (provider, _seen) = Recorder::new("done");
        let mut agent = Agent::with_provider(preset_config(preset), Box::new(provider));
        agent.send("hello").await.unwrap();

        let store = SessionStore::open(temp_dir("events")).unwrap();
        agent.save_turn_records(&store, "sess").unwrap();
        assert!(
            store.root().join("sess.events.jsonl").exists(),
            "{preset}: the marker log lands on the sidecar family's own events member"
        );
        assert_eq!(
            store.load_turn_records("sess").unwrap(),
            agent.turn_records(),
            "{preset}: reload is lossless"
        );
    }
}

// ===========================================================================
// per-turn-cost-usage-accounting — tokens AND cost, persisted
// ===========================================================================

#[tokio::test]
async fn cc_parity_persists_a_priced_usage_record_per_round_trip() {
    // cc-parity pins Opus, which `crate::pricing`'s table knows, so the
    // cost half of the row's semantics is a real figure here.
    let (provider, _seen) = Recorder::new("done");
    let mut agent = Agent::with_provider(preset_config("cc-parity"), Box::new(provider));
    assert!(
        agent.model_priced(),
        "cc-parity's pinned model is priceable"
    );
    agent.send("hello").await.unwrap();

    let records = agent.usage_records();
    assert_eq!(records.len(), 1);
    let cost = records[0].cost_usd.expect("a priced model yields a cost");
    // 1M input + 1M output at the reference Opus rates.
    let expected = supercode_harness::pricing_ref::REF_INPUT_PER_MTOK
        + supercode_harness::pricing_ref::REF_OUTPUT_PER_MTOK;
    assert!((cost - expected).abs() < 1e-9, "{cost} vs {expected}");
    assert!((agent.total_cost_usd() - expected).abs() < 1e-9);

    let store = SessionStore::open(temp_dir("usage")).unwrap();
    agent.save_usage_log(&store, "sess").unwrap();
    let reloaded = store.load_usage_log("sess").unwrap();
    assert_eq!(reloaded, records, "the persisted log round-trips with cost");
    assert!(store.root().join("sess.usage.jsonl").exists());
}

#[tokio::test]
async fn an_unpriceable_model_records_no_cost_rather_than_a_guess() {
    let mut config = preset_config("cx-parity");
    config.model = "someone-elses/model-1".to_string();
    let (provider, _seen) = Recorder::new("done");
    let mut agent = Agent::with_provider(config, Box::new(provider));
    assert!(!agent.model_priced());
    agent.send("hello").await.unwrap();
    assert_eq!(agent.usage_records()[0].cost_usd, None);
}

// ===========================================================================
// turn-budget-caps — turns, output tokens, spend, steps
// ===========================================================================

#[tokio::test]
async fn all_four_caps_are_enforced_by_the_loop() {
    // (1) turns — the pre-existing iteration cap.
    let mut config = preset_config("cc-parity");
    config.max_iterations = 2;
    let mut agent = Agent::with_provider(config, Box::new(ToolCaller::new(1, 0)));
    let err = agent.send("go").await.unwrap_err();
    assert!(
        err.to_string().contains("2 reasoning/tool iterations"),
        "{err}"
    );
    assert!(matches!(
        agent.turn_records().last().map(|r| &r.marker),
        Some(TurnMarker::Finish {
            reason: FinishReason::MaxIterations
        })
    ));

    // (2) output tokens — the pre-existing cumulative-completion cap.
    let mut config = preset_config("cc-parity");
    config.max_total_output_tokens = Some(10);
    let mut agent = Agent::with_provider(config, Box::new(ToolCaller::new(1, 100)));
    agent.send("go").await.unwrap();
    assert!(agent.turn_records().iter().any(|r| matches!(
        r.marker,
        TurnMarker::Finish {
            reason: FinishReason::OutputTokenBudget
        }
    )));

    // (3) spend — NEW. One round-trip bills 1M output tokens of Opus, well
    // over the one-cent cap, so the loop stops after the first turn.
    let mut config = preset_config("cc-parity");
    config.max_budget_usd = Some(0.01);
    let mut agent = Agent::with_provider(config, Box::new(ToolCaller::new(1, 1_000_000)));
    agent.send("go").await.unwrap();
    assert!(
        agent.total_cost_usd() > 0.01,
        "the turn that tripped the cap is billed: {}",
        agent.total_cost_usd()
    );
    assert!(agent.turn_records().iter().any(|r| matches!(
        r.marker,
        TurnMarker::Finish {
            reason: FinishReason::SpendBudget
        }
    )));
    // The transcript stays well-formed: the unanswered tool call got a
    // synthetic result, so the session can be resumed.
    let last = agent.history().last().unwrap();
    assert_eq!(last.role, supercode_harness::Role::Tool);
    assert!(last.content.as_deref().unwrap().contains("spend budget"));
    // A second send with the budget already gone is refused outright.
    let err = agent.send("more").await.unwrap_err();
    assert!(err.to_string().contains("spend budget exhausted"), "{err}");

    // (4) steps — NEW, and distinct from turns: 3 tool calls in ONE
    // round-trip trip a step cap of 2 that no turn cap would catch.
    let mut config = preset_config("cx-parity");
    config.max_steps = Some(2);
    let mut agent = Agent::with_provider(config, Box::new(ToolCaller::new(3, 0)));
    agent.send("go").await.unwrap();
    assert_eq!(agent.total_steps(), 0, "the batch never ran");
    assert!(agent.turn_records().iter().any(|r| matches!(
        r.marker,
        TurnMarker::Finish {
            reason: FinishReason::StepBudget
        }
    )));
}

#[test]
fn a_spend_cap_on_an_unpriceable_model_is_refused_at_construction() {
    // The cap must never be accepted and then silently never bite.
    let mut config = preset_config("cx-parity");
    config.model = "someone-elses/model-1".to_string();
    config.max_budget_usd = Some(5.0);
    let mut priced = preset_config("cx-parity");
    priced.model = config.model.clone();
    priced.max_budget_usd = config.max_budget_usd;
    let err = match Agent::new(config) {
        Ok(_) => panic!("an unpriceable spend cap must be refused"),
        Err(e) => e,
    };
    assert!(err.to_string().contains("no known price"), "{err}");

    // Pricing it explicitly makes the same cap legal.
    priced.price_input_per_mtok = Some(1.0);
    priced.price_output_per_mtok = Some(2.0);
    assert!(Agent::new(priced).is_ok());
}

// ===========================================================================
// interrupt-abort-with-state-preserved — the abort MARKER
// ===========================================================================

#[tokio::test]
async fn an_interrupted_turn_persists_an_abort_marker_and_keeps_its_partial_work() {
    for preset in PARITY_PRESETS {
        let (provider, _seen) = Recorder::new("first answer");
        let mut agent = Agent::with_provider(preset_config(preset), Box::new(provider));
        agent.send("hello").await.unwrap();
        let before = agent.history().len();

        let seen = Arc::new(Mutex::new(Vec::new()));
        let sink_seen = seen.clone();
        agent.set_event_sink(Box::new(move |e: AgentEvent| {
            if let AgentEvent::TurnAborted { source } = e {
                sink_seen.lock().unwrap().push(source);
            }
        }));
        agent.note_abort("ctrl_c");

        assert_eq!(
            seen.lock().unwrap().as_slice(),
            ["ctrl_c"],
            "{preset}: the live event fires"
        );
        let marker = agent.turn_records().last().unwrap();
        assert!(
            matches!(&marker.marker, TurnMarker::Aborted { source, messages }
                if source == "ctrl_c" && *messages == before),
            "{preset}: {marker:?}"
        );
        assert_eq!(
            agent.history().len(),
            before,
            "{preset}: partial work is preserved, not rewound"
        );

        let store = SessionStore::open(temp_dir("abort")).unwrap();
        agent.save_turn_records(&store, "sess").unwrap();
        assert!(
            store
                .load_turn_records("sess")
                .unwrap()
                .iter()
                .any(|r| matches!(r.marker, TurnMarker::Aborted { .. })),
            "{preset}: the abort survives a reload — the interruption is a FACT, not an inference"
        );
    }
}

// ===========================================================================
// auto-retry-on-transient-provider-errors — the SURFACING half
// ===========================================================================

#[tokio::test]
async fn a_retried_request_surfaces_as_an_event_and_a_persisted_record() {
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let server = tokio::spawn(async move {
        for n in 0..2u32 {
            let (mut sock, _) = listener.accept().await.unwrap();
            let mut buf = [0u8; 8192];
            let _ = sock.read(&mut buf).await;
            let resp = if n == 0 {
                "HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
                    .to_string()
            } else {
                let sse = "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\n\
                           data: [DONE]\n\n";
                format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{sse}",
                    sse.len()
                )
            };
            sock.write_all(resp.as_bytes()).await.unwrap();
            sock.flush().await.unwrap();
        }
    });

    // The REAL construction path — `Agent::new` builds the HTTP provider
    // and installs the retry log; only the endpoint is local.
    let mut config = preset_config("cc-parity");
    config.base_url = format!("http://{addr}");
    config.retry_enabled = true;
    config.retry_max_retries = Some(2);
    config.retry_base_delay_ms = Some(1);
    let mut agent = Agent::new(config).unwrap();

    let events = Arc::new(Mutex::new(Vec::new()));
    let sink_events = events.clone();
    agent.set_event_sink(Box::new(move |e: AgentEvent| {
        if let AgentEvent::ProviderRetry {
            attempt, reason, ..
        } = e
        {
            sink_events.lock().unwrap().push((attempt, reason));
        }
    }));

    let reply = agent.send("hi").await.unwrap();
    assert_eq!(reply, "ok", "the retry succeeded");
    server.await.unwrap();

    let seen = events.lock().unwrap().clone();
    assert_eq!(seen.len(), 1, "one retry surfaced as an event: {seen:?}");
    assert_eq!(seen[0].0, 0, "attempt 0 is the one that failed");
    assert!(seen[0].1.contains("503"), "{:?}", seen[0].1);

    let retries: Vec<&TurnMarker> = agent
        .turn_records()
        .iter()
        .map(|r| &r.marker)
        .filter(|m| matches!(m, TurnMarker::Retry { .. }))
        .collect();
    assert_eq!(retries.len(), 1, "and as a persisted record: {retries:?}");

    let store = SessionStore::open(temp_dir("retry")).unwrap();
    agent.save_turn_records(&store, "sess").unwrap();
    assert!(store
        .load_turn_records("sess")
        .unwrap()
        .iter()
        .any(|r| matches!(r.marker, TurnMarker::Retry { .. })));
}

// ===========================================================================
// goals-persistent-objective-across-turns
// ===========================================================================

#[tokio::test]
async fn a_goal_is_restated_on_every_request_persisted_and_kept_out_of_history() {
    for preset in PARITY_PRESETS {
        let config = preset_config(preset);
        assert!(
            config.goals_enabled,
            "{preset} must arm capabilities.todos.goals"
        );
        let (provider, seen) = Recorder::new("done");
        let mut agent = Agent::with_provider(config, Box::new(provider));

        assert!(agent.set_goal("ship BP-7 with proof"));
        agent.send("first").await.unwrap();
        agent.send("second").await.unwrap();

        let requests = seen.lock().unwrap();
        assert_eq!(requests.len(), 2);
        for (i, req) in requests.iter().enumerate() {
            let tail = req.messages.last().unwrap();
            assert!(
                tail.content
                    .as_deref()
                    .unwrap_or_default()
                    .contains("ship BP-7 with proof"),
                "{preset}: request {i} must restate the standing objective"
            );
        }
        drop(requests);

        assert!(
            !agent.history().iter().any(|m| m
                .content
                .as_deref()
                .unwrap_or_default()
                .contains("<goal>")),
            "{preset}: the goal is harness-tracked, never written into the transcript"
        );

        let store = SessionStore::open(temp_dir("goal")).unwrap();
        agent.save_goal(&store, "sess").unwrap();
        let loaded = store.load_goal("sess").unwrap().expect("persisted goal");
        assert_eq!(loaded.objective, "ship BP-7 with proof");
        assert!(store.root().join("sess.goal.json").exists());

        // A resumed session picks it back up, and clearing removes the file.
        let mut resumed =
            Agent::with_provider(preset_config(preset), Box::new(Recorder::new("x").0));
        resumed.restore_goal(Some(loaded));
        assert_eq!(
            resumed.goal().map(|g| g.objective.as_str()),
            Some("ship BP-7 with proof"),
            "{preset}: the objective crosses a resume"
        );
        assert!(agent.clear_goal());
        agent.save_goal(&store, "sess").unwrap();
        assert!(store.load_goal("sess").unwrap().is_none());
    }
}

#[test]
fn a_goal_is_refused_when_the_module_is_off() {
    let mut config = preset_config("cc-parity");
    config.goals_enabled = false;
    let mut agent = Agent::with_provider(config, Box::new(Recorder::new("x").0));
    assert!(
        !agent.set_goal("nope"),
        "the module gate, not a silent success"
    );
    assert!(agent.goal().is_none());
}

// ===========================================================================
// review-mode-dedicated-code-review-flow
// ===========================================================================

#[tokio::test]
async fn the_review_turn_sends_the_presets_own_report_format() {
    for preset in PARITY_PRESETS {
        let config = preset_config(preset);
        let (provider, seen) = Recorder::new("reviewed");
        let mut agent = Agent::with_provider(config, Box::new(provider));

        let prompt = agent
            .review_prompt("focus on the parser")
            .unwrap_or_else(|| panic!("{preset} must pin a review template"));
        assert!(
            prompt.contains("focus on the parser"),
            "{preset}: args land"
        );
        for section in ["Correctness", "Security", "Verdict"] {
            assert!(
                prompt.contains(section),
                "{preset}: the template IS the report format, missing `{section}`"
            );
        }
        assert!(!prompt.contains("{args}"), "{preset}: the slot is filled");

        agent.review("focus on the parser").await.unwrap();
        let requests = seen.lock().unwrap();
        let sent = requests[0]
            .messages
            .iter()
            .any(|m| m.content.as_deref().unwrap_or_default().contains("Verdict"));
        assert!(
            sent,
            "{preset}: the review turn actually carries the format"
        );
        assert!(
            !requests[0].tools.is_empty(),
            "{preset}: a review is a TURN of this session — it keeps the session's tools"
        );
    }
}

// ===========================================================================
// side-ephemeral-q-a
// ===========================================================================

#[tokio::test]
async fn a_side_question_sees_the_whole_context_has_no_tools_and_records_nothing() {
    for preset in PARITY_PRESETS {
        let (provider, seen) = Recorder::new("the answer");
        let mut agent = Agent::with_provider(preset_config(preset), Box::new(provider));
        agent.send("what is in this repo?").await.unwrap();

        let history_before = agent.history().to_vec();
        let usage_before = agent.usage_records().len();
        let records_before = agent.turn_records().len();

        let answer = agent
            .side_question("by the way, what model are you?")
            .await
            .unwrap();
        assert_eq!(answer, "the answer");

        let requests = seen.lock().unwrap();
        let side = requests.last().unwrap();
        assert!(
            side.tools.is_empty(),
            "{preset}: a side question is TOOL-LESS: {:?}",
            side.tools.iter().map(|t| &t.name).collect::<Vec<_>>()
        );
        assert!(
            side.messages.len() > history_before.len(),
            "{preset}: it is asked OVER the full context, not in a blank one"
        );
        assert!(side
            .messages
            .iter()
            .any(|m| m.content.as_deref().unwrap_or_default() == "what is in this repo?"));
        assert!(side
            .messages
            .last()
            .unwrap()
            .content
            .as_deref()
            .unwrap()
            .contains("by the way, what model are you?"));
        drop(requests);

        assert_eq!(
            agent.history(),
            history_before.as_slice(),
            "{preset}: the exchange never enters history"
        );
        assert_eq!(agent.usage_records().len(), usage_before);
        assert_eq!(agent.turn_records().len(), records_before);
    }
}

// ===========================================================================
// extended-thinking-control
// ===========================================================================

#[tokio::test]
async fn effort_changes_mid_session_including_an_off_toggle() {
    for preset in PARITY_PRESETS {
        let config = preset_config(preset);
        assert_eq!(
            config.effort.as_deref(),
            Some("medium"),
            "{preset} pins the starting level"
        );
        let (provider, seen) = Recorder::new("done");
        let mut agent = Agent::with_provider(config, Box::new(provider));

        agent.send("one").await.unwrap();
        assert_eq!(
            agent.set_effort(Some("high".into())).as_deref(),
            Some("medium")
        );
        agent.send("two").await.unwrap();
        // The OFF toggle, distinct from the level.
        assert_eq!(agent.set_effort(None).as_deref(), Some("high"));
        assert_eq!(agent.effort(), None);
        agent.send("three").await.unwrap();

        let requests = seen.lock().unwrap();
        let efforts: Vec<Option<String>> = requests.iter().map(|r| r.effort.clone()).collect();
        assert_eq!(
            efforts,
            vec![Some("medium".to_string()), Some("high".to_string()), None],
            "{preset}: each change reaches the very next request"
        );
        drop(requests);

        let changes: Vec<(Option<String>, Option<String>)> = agent
            .turn_records()
            .iter()
            .filter_map(|r| match &r.marker {
                TurnMarker::Effort { from, to } => Some((from.clone(), to.clone())),
                _ => None,
            })
            .collect();
        assert_eq!(
            changes,
            vec![
                (Some("medium".to_string()), Some("high".to_string())),
                (Some("high".to_string()), None)
            ],
            "{preset}: every change is recorded, the model_change analog"
        );
    }
}

// ===========================================================================
// auto-title-session-summary-generation — the preset gate
// ===========================================================================

#[test]
fn both_presets_arm_auto_title() {
    for preset in PARITY_PRESETS {
        assert!(
            preset_config(preset).auto_title,
            "{preset}: `[core.session] auto_title` is the gate the built titler waits on"
        );
    }
}

// ===========================================================================
// turn-diff-tracking — the CUMULATIVE diff, under both presets
// ===========================================================================

/// The turn's net effect, driven through the real seams: the agent opens a
/// checkpoint at the top of the turn (`run_loop`), the write observer the
/// file tools call records pre-images into it, and `turn_patch` renders the
/// cumulative diff of the whole turn.
async fn turn_patch_over_preset(preset: &str) -> (String, bool) {
    use supercode_harness::tools::WriteObserver;

    let project = temp_dir(&format!("diff-{preset}"));
    std::fs::write(project.join("kept.txt"), "one\ntwo\nthree\n").unwrap();

    let mut config = preset_config(preset);
    config.cwd = project.clone();
    config.checkpoint_dir = Some(temp_dir(&format!("shadow-{preset}")));
    assert!(
        config.checkpoint_enabled,
        "{preset} must arm capabilities.checkpoint for turn-diff tracking"
    );
    let restore_enabled = config.checkpoint_restore;

    let (provider, _seen) = Recorder::new("done");
    let mut agent = Agent::with_provider(config, Box::new(provider));
    // One turn: the loop opens this turn's checkpoint...
    agent.send("edit the file").await.unwrap();
    let observer = agent
        .checkpoint_observer()
        .expect("the preset arms the observer");
    // ...and a write tool's own observer seam records the pre-images. Two
    // writes to the same file in one turn, plus a create, so the result can
    // only be right if the diff is CUMULATIVE (start of turn → now).
    observer.before_write(&project.join("kept.txt")).await;
    std::fs::write(project.join("kept.txt"), "one\nTWO\nthree\n").unwrap();
    observer.before_write(&project.join("kept.txt")).await;
    std::fs::write(project.join("kept.txt"), "one\nTWO\nthree\nfour\n").unwrap();
    observer.before_write(&project.join("made.txt")).await;
    std::fs::write(project.join("made.txt"), "brand new\n").unwrap();

    let id = observer.current().expect("the turn's checkpoint is open");
    (observer.turn_patch(&id).unwrap(), restore_enabled)
}

#[tokio::test]
async fn both_presets_report_a_turns_cumulative_unified_diff() {
    for preset in PARITY_PRESETS {
        let (patch, _) = turn_patch_over_preset(preset).await;
        assert!(
            patch.contains("--- a/kept.txt") && patch.contains("+++ b/kept.txt"),
            "{preset}: a real unified diff, not a file list:\n{patch}"
        );
        assert!(
            patch.contains("-two") && patch.contains("+TWO") && patch.contains("+four"),
            "{preset}: the turn's NET effect across two writes to one file:\n{patch}"
        );
        assert!(
            !patch.contains("+TWO\n+TWO"),
            "{preset}: cumulative, not per-write:\n{patch}"
        );
        assert!(
            patch.contains("--- a/made.txt") && patch.contains("+brand new"),
            "{preset}: a file created this turn diffs against nothing:\n{patch}"
        );
    }
}

#[tokio::test]
async fn cx_parity_tracks_the_diff_but_refuses_a_restore_it_has_no_surface_for() {
    // Codex has a real `turn_diff_tracker` and no code restore at all; the
    // preset says both, and the module enforces the second half.
    let (patch, cc_restore) = turn_patch_over_preset("cc-parity").await;
    assert!(cc_restore, "cc-parity keeps /rewind's restore half");
    assert!(!patch.is_empty());

    let mut config = preset_config("cx-parity");
    config.cwd = temp_dir("cx-restore");
    config.checkpoint_dir = Some(temp_dir("cx-restore-shadow"));
    assert!(!config.checkpoint_restore);
    let observer = supercode_harness::checkpoint::observer_for_config(&config)
        .expect("cx-parity arms the observer");
    assert!(!observer.restore_enabled());
    let id = observer.store().create_checkpoint("t").unwrap();
    let err = observer.restore(&id).unwrap_err();
    assert!(err.to_string().contains("restore = false"), "{err}");
}

// ===========================================================================
// named-agent-definitions-as-data — prompt + model + tools + PERMISSIONS
// ===========================================================================

#[tokio::test]
async fn a_project_agent_file_is_discovered_under_both_presets_and_carries_permissions() {
    for preset in PARITY_PRESETS {
        let project = temp_dir(&format!("agents-{preset}"));
        std::fs::create_dir_all(project.join(".claude/agents")).unwrap();
        std::fs::write(
            project.join(".claude/agents/auditor.md"),
            "---\n\
             name: auditor\n\
             model: anthropic/claude-haiku-4-5\n\
             tools: [Bash]\n\
             sandbox: read-only\n\
             approval: untrusted\n\
             auto_approved_tools: [Bash]\n\
             deny: [\"bash(rm -rf*)\"]\n\
             ---\n\
             You audit, you never write.\n",
        )
        .unwrap();

        let mut config = preset_config(preset);
        config.cwd = project.clone();
        // The definition is data the harness DISCOVERS — nothing here
        // registers it by hand, and no Claude emulate/resume path runs.
        let agent = Agent::with_provider(config, Box::new(Recorder::new("x").0));

        let def = agent
            .config()
            .subagents_definitions
            .get("auditor")
            .unwrap_or_else(|| panic!("{preset}: .claude/agents/*.md was not discovered"));
        assert_eq!(def.system_prompt, "You audit, you never write.");
        assert_eq!(def.model.as_deref(), Some("anthropic/claude-haiku-4-5"));
        assert_eq!(def.tools.as_deref(), Some(&["bash".to_string()][..]));
        let perms = def
            .permissions
            .as_ref()
            .unwrap_or_else(|| panic!("{preset}: the permissions component is missing"));
        assert_eq!(
            perms.sandbox,
            Some(supercode_harness::SandboxPolicy::ReadOnly)
        );
        assert_eq!(
            perms.approval,
            Some(supercode_harness::ApprovalPolicy::Untrusted)
        );
        assert_eq!(perms.deny, vec!["bash(rm -rf*)".to_string()]);
    }
}

#[tokio::test]
async fn an_agent_definition_can_only_tighten_its_parents_posture() {
    use supercode_harness::{ApprovalPolicy, SandboxPolicy};

    let project = temp_dir("agents-tighten");
    std::fs::create_dir_all(project.join(".claude/agents")).unwrap();
    // One definition strictly tighter than the parent, one strictly looser.
    std::fs::write(
        project.join(".claude/agents/tight.md"),
        "---\nname: tight\nsandbox: read-only\napproval: untrusted\ndeny: [\"bash(curl*)\"]\n---\nStrict.\n",
    )
    .unwrap();
    std::fs::write(
        project.join(".claude/agents/loose.md"),
        "---\nname: loose\nsandbox: danger-full-access\napproval: never\n---\nLoose.\n",
    )
    .unwrap();

    let mut config = preset_config("cc-parity");
    config.cwd = project;
    config.sandbox = SandboxPolicy::WorkspaceWrite;
    config.approval = ApprovalPolicy::OnRequest;
    config.auto_approved_tools = ["read_file".to_string(), "bash".to_string()]
        .into_iter()
        .collect();
    let agent = Agent::with_provider(config, Box::new(Recorder::new("x").0));

    let tight = agent.child_config_for_agent_type("tight").expect("tight");
    assert_eq!(tight.sandbox, SandboxPolicy::ReadOnly, "tighter wins");
    assert_eq!(tight.approval, ApprovalPolicy::Untrusted);
    assert!(
        tight.tool_deny_patterns.iter().any(|p| p == "bash(curl*)"),
        "deny is a union"
    );

    let loose = agent.child_config_for_agent_type("loose").expect("loose");
    assert_eq!(
        loose.sandbox,
        SandboxPolicy::WorkspaceWrite,
        "a looser sandbox in a repo file is IGNORED, never honored"
    );
    assert_eq!(
        loose.approval,
        ApprovalPolicy::OnRequest,
        "a looser approval in a repo file is IGNORED"
    );
}

// ===========================================================================
// background-subagents-resume — detach, mailbox, resume with context
// ===========================================================================

#[test]
fn both_presets_detach_background_children_with_a_c6_policy() {
    for preset in PARITY_PRESETS {
        let config = preset_config(preset);
        assert!(
            config.subagents_enabled && config.subagents_background,
            "{preset}: background children must actually detach"
        );
        assert!(
            config.subagents_background_prompts.is_some(),
            "{preset}: C6 — a detached child needs an approval policy, and \
             `run_spawn_subagent` refuses the spawn without one"
        );
    }
}

#[tokio::test]
async fn both_presets_advertise_the_mailbox_and_resume_intrinsics() {
    for preset in PARITY_PRESETS {
        let agent = Agent::with_provider(preset_config(preset), Box::new(Recorder::new("x").0));
        let names: Vec<String> = agent.tool_schemas().into_iter().map(|t| t.name).collect();
        for intrinsic in [
            "spawn_subagent",
            "subagent_status",
            "subagent_message",
            "subagent_resume",
        ] {
            assert!(
                names.iter().any(|n| n == intrinsic),
                "{preset}: `{intrinsic}` is not advertised: {names:?}"
            );
        }
    }
}

/// Drives a whole parent/child episode through the REAL loop: the parent's
/// model asks to spawn a background child, then to message it, then polls
/// it, then resumes it. Parent and child share this one provider (as they
/// do in production — `Agent::provider_arc`), and turns are told apart by
/// what is actually in the request.
struct TeamProvider {
    /// Set by the CHILD when it begins its first turn; the parent parks
    /// until then, so the message is provably sent to a child that is
    /// already RUNNING rather than one that has not started yet.
    started: Arc<std::sync::atomic::AtomicBool>,
    /// Set once `subagent_message` has been sent; the child's first turn
    /// parks until then, so the mailbox test is deterministic rather than a
    /// sleep race. Together the two flags pin the ordering:
    /// child starts → parent messages → child's NEXT turn sees it.
    delivered: Arc<std::sync::atomic::AtomicBool>,
    child_requests: Arc<Mutex<Vec<Vec<String>>>>,
    parent_step: AtomicUsize,
    subagent_id: Arc<Mutex<Option<String>>>,
}

fn user_texts(req: &ChatRequest) -> Vec<String> {
    req.messages
        .iter()
        .filter(|m| m.role == supercode_harness::Role::User)
        .map(|m| m.content.clone().unwrap_or_default())
        .collect()
}

#[async_trait]
impl Provider for TeamProvider {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let users = user_texts(req);
        let is_child = users.first().map(|t| t.starts_with("CHILD:")) == Some(true);
        if is_child {
            self.started.store(true, Ordering::SeqCst);
            self.child_requests.lock().unwrap().push(users.clone());
            // Turn 1: park until the parent's mailbox message lands, then
            // ask for a tool call so the loop takes another iteration —
            // which is where a steered message is drained.
            if users.len() == 1 {
                for _ in 0..600 {
                    if self.delivered.load(Ordering::SeqCst) {
                        break;
                    }
                    tokio::time::sleep(std::time::Duration::from_millis(5)).await;
                }
                let mut msg = ChatMessage::assistant("thinking");
                msg.tool_calls = Some(vec![ToolCall {
                    id: "child_call".into(),
                    kind: "function".into(),
                    function: FunctionCall {
                        name: "no_such_tool".into(),
                        arguments: "{}".into(),
                    },
                }]);
                return Ok((msg, Usage::default()));
            }
            return Ok((
                ChatMessage::assistant(format!("child heard: {}", users.join(" | "))),
                Usage::default(),
            ));
        }

        // Parent.
        let step = self.parent_step.fetch_add(1, Ordering::SeqCst);
        let id = self.subagent_id.lock().unwrap().clone();
        let call = |name: &str, args: serde_json::Value| {
            let mut msg = ChatMessage::assistant("working");
            msg.tool_calls = Some(vec![ToolCall {
                id: format!("parent_{name}"),
                kind: "function".into(),
                function: FunctionCall {
                    name: name.into(),
                    arguments: args.to_string(),
                },
            }]);
            msg
        };
        // Learn the id the moment `spawn_subagent`'s result comes back.
        if id.is_none() {
            if let Some(text) = req
                .messages
                .iter()
                .rev()
                .find(|m| m.role == supercode_harness::Role::Tool)
                .and_then(|m| m.content.clone())
            {
                if let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) {
                    if let Some(new_id) = v.get("subagent_id").and_then(|x| x.as_str()) {
                        *self.subagent_id.lock().unwrap() = Some(new_id.to_string());
                    }
                }
            }
        }
        let id = self.subagent_id.lock().unwrap().clone();
        match (step, id) {
            (0, _) => Ok((
                call(
                    "spawn_subagent",
                    serde_json::json!({"task": "CHILD: count to three", "background": true}),
                ),
                Usage::default(),
            )),
            (_, Some(id)) => {
                let last_tool = req
                    .messages
                    .iter()
                    .rev()
                    .find(|m| m.role == supercode_harness::Role::Tool)
                    .and_then(|m| m.content.clone())
                    .unwrap_or_default();
                if !self.delivered.load(Ordering::SeqCst) {
                    // Wait for the child to be genuinely in flight first.
                    for _ in 0..600 {
                        if self.started.load(Ordering::SeqCst) {
                            break;
                        }
                        tokio::time::sleep(std::time::Duration::from_millis(5)).await;
                    }
                    self.delivered.store(true, Ordering::SeqCst);
                    return Ok((
                        call(
                            "subagent_message",
                            serde_json::json!({"subagent_id": id, "message": "MAIL: also count backwards"}),
                        ),
                        Usage::default(),
                    ));
                }
                if last_tool.contains("\"status\":\"done\"") {
                    return Ok((
                        ChatMessage::assistant(format!("parent done: {last_tool}")),
                        Usage::default(),
                    ));
                }
                Ok((
                    call("subagent_status", serde_json::json!({"subagent_id": id})),
                    Usage::default(),
                ))
            }
            (_, None) => Ok((ChatMessage::assistant("no child"), Usage::default())),
        }
    }
}

// A detached child is a real `tokio::spawn` task. On the default
// current-thread test runtime it would only run when the parent happens to
// yield, which is not how the product runs — so these two episodes use a
// multi-threaded runtime, same as the CLI's own.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_running_background_child_receives_a_message_from_its_parent() {
    for preset in PARITY_PRESETS {
        let mut config = preset_config(preset);
        config.max_iterations = 40;
        let child_requests = Arc::new(Mutex::new(Vec::new()));
        let provider = TeamProvider {
            started: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            delivered: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            child_requests: child_requests.clone(),
            parent_step: AtomicUsize::new(0),
            subagent_id: Arc::new(Mutex::new(None)),
        };
        let mut agent = Agent::with_provider_arc(config, Arc::new(provider));
        let answer = agent.send("run the team").await.unwrap();
        assert!(answer.contains("parent done"), "{preset}: {answer}");
        assert!(
            answer.contains("child heard"),
            "{preset}: the parent collected the child's result: {answer}"
        );

        // The mailbox actually reached the running child: its LATER request
        // carries the parent's message as a user turn it never started with.
        let seen = child_requests.lock().unwrap();
        assert_eq!(
            seen.first().map(|t| t.as_slice()),
            Some(&["CHILD: count to three".to_string()][..]),
            "{preset}: the child began with only its task — the message it later              receives was NOT part of the spawn: {seen:?}"
        );
        assert!(
            seen.iter().any(|turn| turn
                .iter()
                .any(|t| t.contains("MAIL: also count backwards"))),
            "{preset}: the message never reached the running child: {seen:?}"
        );
    }
}

/// A foreground child, then a resume of it: the resumed child's request must
/// carry its OWN earlier conversation, not a blank one.
struct ResumeProvider {
    parent_step: AtomicUsize,
    child_requests: Arc<Mutex<Vec<Vec<String>>>>,
    subagent_id: Arc<Mutex<Option<String>>>,
}

#[async_trait]
impl Provider for ResumeProvider {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let users = user_texts(req);
        if users.first().map(|t| t.starts_with("CHILD:")) == Some(true) {
            self.child_requests.lock().unwrap().push(users.clone());
            return Ok((
                ChatMessage::assistant("the secret is 41".to_string()),
                Usage::default(),
            ));
        }
        let step = self.parent_step.fetch_add(1, Ordering::SeqCst);
        let mut msg = ChatMessage::assistant("working");
        let (name, args) = match step {
            0 => (
                "spawn_subagent",
                serde_json::json!({"task": "CHILD: find the secret", "background": false}),
            ),
            // End phase one so the test can hand the parent the child's id
            // the way a real caller reads it off the lineage record.
            1 => return Ok((ChatMessage::assistant("phase one done"), Usage::default())),
            2 => {
                // The foreground spawn's own result text is the child's
                // answer, so the id comes from the lineage the parent kept.
                let id = self.subagent_id.lock().unwrap().clone().unwrap_or_default();
                (
                    "subagent_resume",
                    serde_json::json!({"subagent_id": id, "task": "CHILD: repeat the secret"}),
                )
            }
            _ => {
                let last = req
                    .messages
                    .iter()
                    .rev()
                    .find(|m| m.role == supercode_harness::Role::Tool)
                    .and_then(|m| m.content.clone())
                    .unwrap_or_default();
                return Ok((
                    ChatMessage::assistant(format!("parent done: {last}")),
                    Usage::default(),
                ));
            }
        };
        msg.tool_calls = Some(vec![ToolCall {
            id: format!("parent_{step}"),
            kind: "function".into(),
            function: FunctionCall {
                name: name.into(),
                arguments: args.to_string(),
            },
        }]);
        Ok((msg, Usage::default()))
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_finished_child_resumes_with_its_own_context_intact() {
    for preset in PARITY_PRESETS {
        let mut config = preset_config(preset);
        config.max_iterations = 12;
        let child_requests = Arc::new(Mutex::new(Vec::new()));
        let ids = Arc::new(Mutex::new(None));
        let provider = ResumeProvider {
            parent_step: AtomicUsize::new(0),
            child_requests: child_requests.clone(),
            subagent_id: ids.clone(),
        };
        let mut agent = Agent::with_provider_arc(config, Arc::new(provider));
        // The parent needs the child's id for the resume call; a foreground
        // spawn returns the child's ANSWER, so read the id off the agent's
        // own reaped set — the same handle a real caller reads from the
        // lineage record.
        agent.send("phase one").await.unwrap();
        let id = agent
            .reaped_subagent_ids()
            .first()
            .cloned()
            .unwrap_or_else(|| panic!("{preset}: no child was reaped"));
        *ids.lock().unwrap() = Some(id);
        let answer = agent.send("phase two").await.unwrap();
        assert!(answer.contains("\"resumed\":true"), "{preset}: {answer}");

        let seen = child_requests.lock().unwrap();
        assert_eq!(seen.len(), 2, "{preset}: one spawn, one resume: {seen:?}");
        assert_eq!(
            seen[0],
            vec!["CHILD: find the secret".to_string()],
            "{preset}: the first run starts blank"
        );
        assert_eq!(
            seen[1],
            vec![
                "CHILD: find the secret".to_string(),
                "CHILD: repeat the secret".to_string()
            ],
            "{preset}: the RESUMED child carries its own earlier conversation"
        );
    }
}