zeph-core 0.20.1

Core agent loop, configuration, context builder, metrics, and vault for Zeph
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

/// End-to-end tests for M30 resilient compaction: error detection → compact → retry → success.
use crate::agent::agent_tests::*;
use zeph_llm::LlmError;
use zeph_llm::any::AnyProvider;
use zeph_llm::mock::MockProvider;
use zeph_llm::provider::{Message, MessageMetadata, Role};

/// Verify that the agent recovers from a `ContextLengthExceeded` error during an LLM call,
/// compacts its context, and returns a successful response on the next attempt.
#[tokio::test]
async fn agent_recovers_from_context_length_exceeded_and_produces_response() {
    // Provider: first call raises ContextLengthExceeded, second call succeeds.
    let provider = AnyProvider::Mock(
        MockProvider::with_responses(vec!["final answer".into()])
            .with_errors(vec![LlmError::ContextLengthExceeded]),
    );
    let channel = MockChannel::new(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();

    let mut agent = crate::agent::Agent::new(provider, channel, registry, None, 5, executor)
        // Provide a context budget so compact_context has a compaction target
        .with_context_budget(200_000, 0.20, 0.80, 4, 0);

    // Seed a user message so the agent has something to compact/retry
    agent.msg.messages.push(Message {
        role: Role::User,
        content: "describe the architecture".into(),
        parts: vec![],
        metadata: MessageMetadata::default(),
    });

    // call_llm_with_retry is the direct entry point for the retry/compact flow
    let result = agent.call_llm_with_retry(2).await.unwrap();

    assert!(
        result.is_some(),
        "agent must produce a response after recovering from context length error"
    );
    assert_eq!(result.as_deref(), Some("final answer"));

    // Verify the channel received the recovered response
    let sent = agent.channel.sent_messages();
    assert!(
        sent.iter().any(|m| m.contains("final answer")),
        "recovered response must be forwarded to the channel; got: {sent:?}"
    );
}

/// E2E test: spawn sub-agent in background, verify it runs and produces output.
///
/// Scope: spawn → text response → collect (`MockProvider` only supports text responses).
#[tokio::test]
async fn subagent_spawn_text_collect_e2e() {
    use zeph_subagent::def::{SkillFilter, SubAgentPermissions, ToolPolicy};
    use zeph_subagent::hooks::SubagentHooks;
    use zeph_subagent::{AgentCommand, SubAgentDef, SubAgentManager};

    // Provider shared between main agent and sub-agent via Arc clone.
    // We pre-load a response that the sub-agent loop will consume.
    let provider = mock_provider(vec!["task completed successfully".into()]);
    let channel = MockChannel::new(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);

    let mut mgr = SubAgentManager::new(4);
    mgr.definitions_mut().push(SubAgentDef {
        name: "worker".into(),
        description: "A worker bot".into(),
        model: None,
        tools: ToolPolicy::InheritAll,
        disallowed_tools: vec![],
        permissions: SubAgentPermissions {
            max_turns: 1,
            ..SubAgentPermissions::default()
        },
        skills: SkillFilter::default(),
        system_prompt: "You are a worker.".into(),
        hooks: SubagentHooks::default(),
        memory: None,
        source: None,
        file_path: None,
    });
    agent.services.orchestration.subagent_manager = Some(mgr);

    // Spawn the sub-agent in background — returns immediately with the task id.
    let spawn_resp = agent
        .handle_agent_command(AgentCommand::Background {
            name: "worker".into(),
            prompt: "do a task".into(),
        })
        .await
        .expect("Background spawn must return Some");
    assert!(
        spawn_resp.contains("worker"),
        "spawn response must mention agent name; got: {spawn_resp}"
    );
    assert!(
        spawn_resp.contains("started"),
        "spawn response must confirm start; got: {spawn_resp}"
    );

    // Extract the short id from response: "Sub-agent 'worker' started in background (id: XXXXXXXX)"
    let short_id = spawn_resp
        .split("id: ")
        .nth(1)
        .expect("response must contain 'id: '")
        .trim_end_matches(')')
        .trim()
        .to_string();
    assert!(!short_id.is_empty(), "short_id must not be empty");

    // Poll until the sub-agent reaches a terminal state (max 5s).
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
    let full_id = loop {
        let mgr = agent
            .services
            .orchestration
            .subagent_manager
            .as_ref()
            .unwrap();
        let statuses = mgr.statuses();
        let found = statuses.iter().find(|(id, _)| id.starts_with(&short_id));
        if let Some((id, status)) = found {
            match status.state {
                zeph_subagent::SubAgentState::Completed => break id.clone(),
                zeph_subagent::SubAgentState::Failed => {
                    panic!(
                        "sub-agent reached Failed state unexpectedly: {:?}",
                        status.last_message
                    );
                }
                _ => {}
            }
        }
        assert!(
            std::time::Instant::now() <= deadline,
            "sub-agent did not complete within timeout"
        );
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
    };

    // Collect result and verify output.
    let result = agent
        .services
        .orchestration
        .subagent_manager
        .as_mut()
        .unwrap()
        .collect(&full_id)
        .await
        .expect("collect must succeed for completed sub-agent");
    assert!(
        result.contains("task completed successfully"),
        "collected result must contain sub-agent output; got: {result:?}"
    );
}

/// Unit test for secret bridge in foreground spawn poll loop.
///
/// Verifies that when a sub-agent emits [`REQUEST_SECRET`: api-key], the bridge:
/// - calls `channel.confirm()` with a prompt containing the key name
/// - on approval, delivers the secret to the sub-agent
/// The `MockChannel` `confirm()` is pre-loaded with `true` (approve).
#[tokio::test]
async fn foreground_spawn_secret_bridge_approves() {
    use zeph_subagent::def::{SkillFilter, SubAgentPermissions, ToolPolicy};
    use zeph_subagent::hooks::SubagentHooks;
    use zeph_subagent::{AgentCommand, SubAgentDef, SubAgentManager};

    // Sub-agent loop responses:
    //   turn 1: request a secret
    //   turn 2: final reply after secret delivered
    let provider = mock_provider(vec![
        "[REQUEST_SECRET: api-key]".into(),
        "done with secret".into(),
    ]);

    // MockChannel with confirm() → true (approve)
    let channel = MockChannel::new(vec![]).with_confirmations(vec![true]);

    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);

    let mut mgr = SubAgentManager::new(4);
    mgr.definitions_mut().push(SubAgentDef {
        name: "vault-bot".into(),
        description: "A bot that requests secrets".into(),
        model: None,
        tools: ToolPolicy::InheritAll,
        disallowed_tools: vec![],
        permissions: SubAgentPermissions {
            max_turns: 2,
            secrets: vec!["api-key".into()],
            ..SubAgentPermissions::default()
        },
        skills: SkillFilter::default(),
        system_prompt: "You need a secret.".into(),
        hooks: SubagentHooks::default(),
        memory: None,
        source: None,
        file_path: None,
    });
    agent.services.orchestration.subagent_manager = Some(mgr);

    // Foreground spawn — blocks until sub-agent completes.
    let resp: String = agent
        .handle_agent_command(AgentCommand::Spawn {
            name: "vault-bot".into(),
            prompt: "fetch the api key".into(),
        })
        .await
        .expect("Spawn must return Some");

    // Sub-agent completed after secret was bridged (approve path).
    // The sub-agent had 2 turns: turn 1 = secret request, turn 2 = final reply.
    // If the bridge did NOT call confirm(), the sub-agent would never get the
    // approval outcome and the foreground poll loop would stall or time out.
    // Reaching this point proves the bridge ran and confirm() was called.
    assert!(
        resp.contains("vault-bot"),
        "response must mention agent name; got: {resp}"
    );
    assert!(
        resp.contains("completed"),
        "sub-agent must complete successfully; got: {resp}"
    );
}

// ── /plan handler unit tests ─────────────────────────────────────────────

#[cfg(feature = "scheduler")]
use zeph_orchestration::{GraphStatus, PlanCommand, TaskGraph, TaskNode, TaskResult, TaskStatus};

#[cfg(feature = "scheduler")]
fn agent_with_orchestration() -> Agent<MockChannel> {
    let provider = mock_provider(vec![]);
    let channel = MockChannel::new(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent
}

#[cfg(feature = "scheduler")]
fn make_simple_graph(status: GraphStatus) -> TaskGraph {
    let mut g = TaskGraph::new("test goal");
    let mut node = TaskNode::new(0, "task-0", "do something");
    node.status = match status {
        GraphStatus::Created => TaskStatus::Pending,
        GraphStatus::Running => TaskStatus::Ready,
        _ => TaskStatus::Completed,
    };
    if status == GraphStatus::Running || status == GraphStatus::Completed {
        node.result = Some(TaskResult {
            output: "done".into(),
            artifacts: vec![],
            duration_ms: 0,
            agent_id: None,
            agent_def: None,
        });
        if status == GraphStatus::Completed {
            node.status = TaskStatus::Completed;
        }
    }
    g.tasks.push(node);
    g.status = status;
    g
}

/// GAP-1: `handle_plan_confirm` with `subagent_manager` = None → fallback message,
/// graph restored in `pending_graph`.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_confirm_no_manager_restores_graph() {
    let mut agent = agent_with_orchestration();

    let graph = make_simple_graph(GraphStatus::Created);
    agent.services.orchestration.pending_graph = Some(graph);

    // No subagent_manager set.
    agent
        .handle_plan_command_as_string(PlanCommand::Confirm)
        .await
        .unwrap();

    // Graph must be restored.
    assert!(
        agent.services.orchestration.pending_graph.is_some(),
        "graph must be restored when no manager configured"
    );
    let msgs = agent.channel.sent_messages();
    assert!(
        msgs.iter().any(|m| m.contains("sub-agent")),
        "must send fallback message; got: {msgs:?}"
    );
}

/// GAP-2: `handle_plan_confirm` with `pending_graph` = None → "No pending plan" message.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_confirm_no_pending_graph_sends_message() {
    let mut agent = agent_with_orchestration();

    // No pending_graph.
    agent
        .handle_plan_command_as_string(PlanCommand::Confirm)
        .await
        .unwrap();

    let msgs = agent.channel.sent_messages();
    assert!(
        msgs.iter().any(|m| m.contains("No pending plan")),
        "must send 'No pending plan' message; got: {msgs:?}"
    );
}

/// GAP-3: happy path — pre-built Running graph with one already-Completed task.
/// `resume_from()` accepts it; first `tick()` emits Done{Completed}; aggregation called.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_confirm_completed_graph_aggregates() {
    use zeph_subagent::def::{SkillFilter, SubAgentPermissions, ToolPolicy};
    use zeph_subagent::hooks::SubagentHooks;
    use zeph_subagent::{SubAgentDef, SubAgentManager};

    // MockProvider returns the aggregation synthesis.
    let provider = mock_provider(vec!["synthesis result".into()]);
    let channel = MockChannel::new(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;

    let mut mgr = SubAgentManager::new(4);
    mgr.definitions_mut().push(SubAgentDef {
        name: "worker".into(),
        description: "A worker".into(),
        model: None,
        tools: ToolPolicy::InheritAll,
        disallowed_tools: vec![],
        permissions: SubAgentPermissions::default(),
        skills: SkillFilter::default(),
        system_prompt: "You are helpful.".into(),
        hooks: SubagentHooks::default(),
        memory: None,
        source: None,
        file_path: None,
    });
    agent.services.orchestration.subagent_manager = Some(mgr);

    // Graph with one already-Completed task in Running status: resume_from() accepts it,
    // and the first tick() will find no running/ready tasks → Done{Completed}.
    let mut graph = TaskGraph::new("test goal");
    let mut node = TaskNode::new(0, "task-0", "already done");
    node.status = TaskStatus::Completed;
    node.result = Some(TaskResult {
        output: "task output".into(),
        artifacts: vec![],
        duration_ms: 10,
        agent_id: None,
        agent_def: None,
    });
    graph.tasks.push(node);
    graph.status = GraphStatus::Running;
    agent.services.orchestration.pending_graph = Some(graph);

    agent
        .handle_plan_command_as_string(PlanCommand::Confirm)
        .await
        .unwrap();

    let msgs = agent.channel.sent_messages();
    // Aggregation synthesis should appear in messages.
    assert!(
        msgs.iter().any(|m| m.contains("synthesis result")),
        "aggregation synthesis must be sent to user; got: {msgs:?}"
    );
    // Graph must be cleared after successful completion.
    assert!(
        agent.services.orchestration.pending_graph.is_none(),
        "pending_graph must be cleared after Completed"
    );
}

/// GAP-4: `handle_plan_confirm` with no sub-agents defined but provider fails →
/// task executes inline but provider returns error → plan fails, failure message sent.
///
/// Since the fix for #1463, no agents → `RunInline` (not `spawn_for_task`). So when
/// the provider fails, the scheduler records a Failed `TaskOutcome`, graph fails,
/// and `finalize_plan_execution` sends a failure message.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_confirm_inline_provider_failure_sends_message() {
    use zeph_subagent::SubAgentManager;

    // Failing provider → chat() always returns an error.
    let provider = mock_provider_failing();
    let channel = MockChannel::new(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;

    // Manager with no defined agents → route() returns None → RunInline.
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // Graph in Created status with one task; scheduler emits RunInline,
    // provider fails → TaskOutcome::Failed → graph Failed.
    let mut graph = TaskGraph::new("failing inline goal");
    let node = TaskNode::new(0, "task-0", "will fail inline");
    graph.tasks.push(node);
    graph.status = GraphStatus::Created;
    agent.services.orchestration.pending_graph = Some(graph);

    agent
        .handle_plan_command_as_string(PlanCommand::Confirm)
        .await
        .unwrap();

    let msgs = agent.channel.sent_messages();
    assert!(
        msgs.iter()
            .any(|m| m.contains("failed") || m.contains("Failed")),
        "failure message must be sent after inline provider error; got: {msgs:?}"
    );
}

/// GAP-5: `handle_plan_list` with `pending_graph` → shows summary + status label.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_list_with_pending_graph_shows_summary() {
    let mut agent = agent_with_orchestration();

    agent.services.orchestration.pending_graph = Some(make_simple_graph(GraphStatus::Created));

    let out = agent
        .handle_plan_command_as_string(PlanCommand::List)
        .await
        .unwrap();

    assert!(
        out.contains("awaiting confirmation"),
        "must show 'awaiting confirmation' status; got: {out:?}"
    );
}

/// GAP-6: `handle_plan_list` with no graph → "No recent plans."
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_list_no_graph_shows_no_recent() {
    let mut agent = agent_with_orchestration();

    let out = agent
        .handle_plan_command_as_string(PlanCommand::List)
        .await
        .unwrap();

    assert!(
        out.contains("No recent plans"),
        "must show 'No recent plans'; got: {out:?}"
    );
}

/// GAP-7: `handle_plan_retry` resets Running tasks to Ready and clears `assigned_agent`.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_retry_resets_running_tasks_to_ready() {
    let mut agent = agent_with_orchestration();

    let mut graph = TaskGraph::new("retry test");
    let mut failed = TaskNode::new(0, "failed-task", "desc");
    failed.status = TaskStatus::Failed;
    let mut stale_running = TaskNode::new(1, "stale-task", "desc");
    stale_running.status = TaskStatus::Running;
    stale_running.assigned_agent = Some("old-handle-id".into());
    graph.tasks.push(failed);
    graph.tasks.push(stale_running);
    graph.status = GraphStatus::Failed;
    agent.services.orchestration.pending_graph = Some(graph);

    agent
        .handle_plan_command_as_string(PlanCommand::Retry(None))
        .await
        .unwrap();

    let g = agent
        .services
        .orchestration
        .pending_graph
        .as_ref()
        .expect("graph must be present after retry");

    // Failed task must be reset to Ready.
    assert_eq!(
        g.tasks[0].status,
        TaskStatus::Ready,
        "failed task must be reset to Ready"
    );

    // Stale Running task must be reset to Ready and assigned_agent cleared.
    assert_eq!(
        g.tasks[1].status,
        TaskStatus::Ready,
        "stale Running task must be reset to Ready"
    );
    assert!(
        g.tasks[1].assigned_agent.is_none(),
        "assigned_agent must be cleared for stale Running task"
    );
}

/// GAP-A: `handle_plan_cancel_as_string` with no active plan returns "No active plan".
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_cancel_as_string_no_active_plan() {
    let mut agent = agent_with_orchestration();
    let out = agent.handle_plan_cancel_as_string(None);
    assert!(
        out.contains("No active plan"),
        "must return 'No active plan' message; got: {out:?}"
    );
}

/// GAP-A: `handle_plan_resume_as_string` with no pending graph returns "No paused plan".
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_resume_as_string_no_paused_plan() {
    let mut agent = agent_with_orchestration();
    let out = agent.handle_plan_resume_as_string(None).await;
    assert!(
        out.contains("No paused plan"),
        "must return 'No paused plan' message; got: {out:?}"
    );
}

/// GAP-B: `dispatch_plan_command_as_string` with a parse error returns `Ok(non-empty)`.
/// `/plan list extra_args` is rejected by the parser — the error must be returned as
/// `Ok(message)`, not propagated as `Err`.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn dispatch_plan_command_as_string_invalid_subcommand() {
    let mut agent = agent_with_orchestration();
    let result = agent
        .dispatch_plan_command_as_string("/plan list unexpected_arg")
        .await
        .unwrap();
    assert!(
        !result.is_empty(),
        "parse error must be returned as Ok(non-empty string), not propagated; got: {result:?}"
    );
}

/// Regression test for issue #1454: secret requests queued before the first `tick()` were
/// silently dropped when a single-task plan completed on the very first tick (instant
/// completion) because `process_pending_secret_requests()` was only called inside the
/// `'tick: loop` body, which exits immediately via `break` before reaching the drain call.
///
/// The fix adds a final `process_pending_secret_requests()` drain after the loop exits.
/// This test verifies that drain by:
/// 1. Pre-loading a `SecretRequest` into the manager's channel **before** the plan runs.
/// 2. Using a graph where the first `tick()` emits `Done` (all tasks already Completed).
/// 3. Asserting that `try_recv_secret_request()` returns `None` after the plan loop,
///    proving the drain was executed.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn test_secret_drain_after_instant_completion() {
    use tokio_util::sync::CancellationToken;
    use zeph_subagent::def::{SkillFilter, SubAgentPermissions, ToolPolicy};
    use zeph_subagent::hooks::SubagentHooks;
    use zeph_subagent::{
        PermissionGrants, SecretRequest, SubAgentDef, SubAgentHandle, SubAgentManager,
        SubAgentState, SubAgentStatus,
    };

    // Channel with one pre-loaded confirmation (approve) so the bridge can resolve the
    // pending request when it is finally drained.
    let channel = MockChannel::new(vec![]).with_confirmations(vec![true]);

    // Provider returns aggregation synthesis (needed to satisfy finalize_plan_execution).
    let provider = mock_provider(vec!["synthesis".into()]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;

    // Build a manager with one agent definition (needed by finalize_plan_execution).
    let mut mgr = SubAgentManager::new(4);
    mgr.definitions_mut().push(SubAgentDef {
        name: "worker".into(),
        description: "A worker".into(),
        model: None,
        tools: ToolPolicy::InheritAll,
        disallowed_tools: vec![],
        permissions: SubAgentPermissions::default(),
        skills: SkillFilter::default(),
        system_prompt: "You are helpful.".into(),
        hooks: SubagentHooks::default(),
        memory: None,
        source: None,
        file_path: None,
    });

    // Create a fake handle whose `pending_secret_rx` already contains one SecretRequest.
    // This simulates a sub-agent that queued the request before the plan loop ran.
    let (secret_request_tx, pending_secret_rx) = tokio::sync::mpsc::channel::<SecretRequest>(4);
    let (secret_tx, _secret_rx) = tokio::sync::mpsc::channel(1);
    let (status_tx, status_rx) = watch::channel(SubAgentStatus {
        state: SubAgentState::Completed,
        last_message: None,
        turns_used: 1,
        started_at: std::time::Instant::now(),
    });
    drop(status_tx);

    // Pre-load the secret request into the channel before plan execution starts.
    secret_request_tx
        .send(SecretRequest {
            secret_key: "api-key".into(),
            reason: Some("test drain".into()),
        })
        .await
        .expect("channel must accept request");
    drop(secret_request_tx); // close sender so try_recv returns None after drain

    let fake_handle_id = "deadbeef-0000-0000-0000-000000000001".to_owned();
    let def_clone = mgr.definitions()[0].clone();
    mgr.insert_handle_for_test(
        fake_handle_id.clone(),
        SubAgentHandle {
            id: fake_handle_id.clone(),
            def: def_clone,
            task_id: fake_handle_id.clone(),
            state: SubAgentState::Completed,
            join_handle: None,
            cancel: CancellationToken::new(),
            status_rx,
            grants: PermissionGrants::default(),
            pending_secret_rx,
            secret_tx,
            started_at_str: "2026-01-01T00:00:00Z".to_owned(),
            transcript_dir: None,
        },
    );
    agent.services.orchestration.subagent_manager = Some(mgr);

    // Graph with one already-Completed task in Running status: the first tick() finds no
    // Running/Ready tasks and emits Done{Completed} immediately (instant completion).
    let mut graph = TaskGraph::new("instant goal");
    let mut node = TaskNode::new(0, "task-0", "already done");
    node.status = TaskStatus::Completed;
    node.result = Some(TaskResult {
        output: "task output".into(),
        artifacts: vec![],
        duration_ms: 1,
        agent_id: None,
        agent_def: None,
    });
    graph.tasks.push(node);
    graph.status = GraphStatus::Running;
    agent.services.orchestration.pending_graph = Some(graph);

    // Run the plan loop — the fix adds a post-loop drain call.
    agent
        .handle_plan_command_as_string(PlanCommand::Confirm)
        .await
        .unwrap();

    // After plan completion, the secret request must have been drained.
    // If the drain was NOT called, try_recv_secret_request() would return Some(_).
    let leftover = agent
        .services
        .orchestration
        .subagent_manager
        .as_mut()
        .and_then(SubAgentManager::try_recv_secret_request);
    assert!(
        leftover.is_none(),
        "pending secret request must be drained after instant plan completion; \
         got: {leftover:?}"
    );
}

/// GAP-8: `handle_plan_confirm` with no sub-agents defined executes the task inline
/// via the main provider. Verifies `RunInline` path: plan succeeds, provider output
/// appears in aggregation, `pending_graph` is cleared.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_confirm_no_subagents_executes_inline() {
    use zeph_subagent::SubAgentManager;

    // Provider returns task result then aggregation synthesis.
    let provider = mock_provider(vec!["inline task output".into(), "synthesis done".into()]);
    let channel = MockChannel::new(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;

    // SubAgentManager with no definitions → route() returns None → RunInline.
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // Simple single-task graph.
    let mut graph = TaskGraph::new("inline goal");
    let node = TaskNode::new(0, "task-0", "do something inline");
    graph.tasks.push(node);
    graph.status = GraphStatus::Created;
    agent.services.orchestration.pending_graph = Some(graph);

    agent
        .handle_plan_command_as_string(PlanCommand::Confirm)
        .await
        .unwrap();

    // Graph must be cleared after successful execution.
    assert!(
        agent.services.orchestration.pending_graph.is_none(),
        "pending_graph must be cleared after inline plan completion"
    );
    let msgs = agent.channel.sent_messages();
    assert!(
        msgs.iter().any(|m| m.contains("synthesis done")),
        "aggregation synthesis must appear in messages; got: {msgs:?}"
    );
}

/// COV-01: `/plan cancel` received during `run_scheduler_loop` cancels the plan.
///
/// Verifies that when the channel delivers "/plan cancel" while the scheduler loop
/// is waiting for a task event, `cancel_all()` is called and the loop exits with
/// `GraphStatus::Canceled`. The "Canceling plan..." status must be sent immediately.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_cancel_during_scheduler_loop_cancels_plan() {
    use crate::config::OrchestrationConfig;
    use zeph_orchestration::{DagScheduler, RuleBasedRouter};
    use zeph_subagent::SubAgentManager;

    // Channel pre-loaded with "/plan cancel" — returned immediately on first recv().
    let channel = MockChannel::new(vec!["/plan cancel".to_owned()]);
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // Graph in Running status with one task in Running state: tick() will not emit
    // any actions (no Ready tasks, no timed-out running tasks), so the loop reaches
    // wait_event() / select! immediately.
    let mut graph = TaskGraph::new("cancel test goal");
    let mut node = TaskNode::new(0, "task-0", "will be canceled");
    node.status = TaskStatus::Running;
    graph.tasks.push(node);
    graph.status = GraphStatus::Running;

    let config = OrchestrationConfig {
        enabled: true,
        ..OrchestrationConfig::default()
    };
    let mut scheduler =
        DagScheduler::resume_from(graph, &config, Box::new(RuleBasedRouter), vec![]).unwrap();

    let token = tokio_util::sync::CancellationToken::new();
    let status = agent
        .run_scheduler_loop(&mut scheduler, 1, token)
        .await
        .unwrap();

    assert_eq!(
        status,
        GraphStatus::Canceled,
        "run_scheduler_loop must return Canceled when /plan cancel is received"
    );
    assert!(
        agent
            .channel
            .statuses
            .lock()
            .unwrap()
            .iter()
            .any(|s| s.contains("Canceling plan")),
        "must send 'Canceling plan...' status before processing cancel"
    );
}

/// COV-02: `finalize_plan_execution` with `GraphStatus::Canceled` sends the correct
/// message, does NOT store the graph into `pending_graph`, and updates
/// `orchestration.tasks_completed` with the count of tasks that finished before cancel.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn finalize_plan_execution_canceled_does_not_store_graph() {
    use zeph_subagent::SubAgentManager;

    let channel = MockChannel::new(vec![]);
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let (metrics_tx, metrics_rx) = watch::channel(MetricsSnapshot::default());
    let mut agent =
        Agent::new(provider, channel, registry, None, 5, executor).with_metrics(metrics_tx);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // Graph with one completed task and one canceled task — typical mid-cancel state.
    let mut graph = TaskGraph::new("cancel finalize test");
    let mut completed = TaskNode::new(0, "task-done", "finished");
    completed.status = TaskStatus::Completed;
    completed.result = Some(TaskResult {
        output: "done".into(),
        artifacts: vec![],
        duration_ms: 10,
        agent_id: None,
        agent_def: None,
    });
    let mut canceled = TaskNode::new(1, "task-canceled", "was running");
    canceled.status = TaskStatus::Canceled;
    graph.tasks.push(completed);
    graph.tasks.push(canceled);
    graph.status = GraphStatus::Canceled;

    agent
        .finalize_plan_execution(graph, GraphStatus::Canceled)
        .await
        .unwrap();

    let msgs = agent.channel.sent_messages();
    assert!(
        msgs.iter()
            .any(|m| m.contains("canceled") || m.contains("Canceled")),
        "must send a cancellation message; got: {msgs:?}"
    );
    assert!(
        msgs.iter().any(|m| m.contains("1/2")),
        "must report completed task count (1/2); got: {msgs:?}"
    );
    assert!(
        agent.services.orchestration.pending_graph.is_none(),
        "canceled plan must NOT be stored in pending_graph"
    );
    let snapshot = metrics_rx.borrow().clone();
    assert_eq!(
        snapshot.orchestration.tasks_completed, 1,
        "tasks completed before cancellation must be counted in metrics"
    );
}

/// COV-03: a non-cancel message received via the channel during `run_scheduler_loop`
/// is queued in `message_queue` for processing after the plan completes.
///
/// Verifies the `tokio::select!` path added in #1603: when the channel delivers a
/// non-cancel message while the loop is waiting for a scheduler event, the message
/// is passed to `enqueue_or_merge()` and appears in `agent.msg.message_queue` after
/// `run_scheduler_loop` returns.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn scheduler_loop_queues_non_cancel_message() {
    use crate::config::OrchestrationConfig;
    use zeph_orchestration::{DagScheduler, RuleBasedRouter};
    use zeph_subagent::SubAgentManager;

    // Channel pre-loaded with one non-cancel message; second recv() returns None,
    // which terminates the loop with GraphStatus::Failed — acceptable for this test
    // since we only verify queuing, not plan completion status.
    let channel = MockChannel::new(vec!["hello".to_owned()]);
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // Graph in Running status with one task in Running state: tick() emits no actions
    // (no Ready tasks, running_in_graph_now > 0 suppresses Done), so the loop reaches
    // the select! where channel.recv() delivers "hello" before the loop exits.
    let mut graph = TaskGraph::new("queue test goal");
    let mut node = TaskNode::new(0, "task-0", "long running task");
    node.status = TaskStatus::Running;
    graph.tasks.push(node);
    graph.status = GraphStatus::Running;

    let config = OrchestrationConfig {
        enabled: true,
        ..OrchestrationConfig::default()
    };
    let mut scheduler =
        DagScheduler::resume_from(graph, &config, Box::new(RuleBasedRouter), vec![]).unwrap();

    let token = tokio_util::sync::CancellationToken::new();
    let _ = agent
        .run_scheduler_loop(&mut scheduler, 1, token)
        .await
        .unwrap();

    assert_eq!(
        agent.msg.message_queue.len(),
        1,
        "non-cancel message must be queued in message_queue; got: {:?}",
        agent
            .msg
            .message_queue
            .iter()
            .map(|m| &m.text)
            .collect::<Vec<_>>()
    );
    assert_eq!(
        agent.msg.message_queue[0].text, "hello",
        "queued message text must match the received message"
    );
}

/// COV-04: channel close (`Ok(None)`) on an exit-supporting channel (CLI/TUI) returns
/// `GraphStatus::Canceled` — no retry needed, stdin EOF is a normal termination.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn scheduler_loop_channel_close_supports_exit_returns_canceled() {
    use crate::config::OrchestrationConfig;
    use zeph_orchestration::{DagScheduler, RuleBasedRouter};
    use zeph_subagent::SubAgentManager;

    // Empty channel with exit_supported=true (the default): recv() returns Ok(None) immediately.
    let channel = MockChannel::new(vec![]);
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    let mut graph = TaskGraph::new("channel close test goal");
    let mut node = TaskNode::new(0, "task-0", "will be canceled on channel close");
    node.status = TaskStatus::Running;
    graph.tasks.push(node);
    graph.status = GraphStatus::Running;

    let config = OrchestrationConfig {
        enabled: true,
        ..OrchestrationConfig::default()
    };
    let mut scheduler =
        DagScheduler::resume_from(graph, &config, Box::new(RuleBasedRouter), vec![]).unwrap();

    let token = tokio_util::sync::CancellationToken::new();
    let status = agent
        .run_scheduler_loop(&mut scheduler, 1, token)
        .await
        .unwrap();

    assert_eq!(
        status,
        GraphStatus::Canceled,
        "channel close on exit-supporting channel (CLI/TUI) must return Canceled, not Failed"
    );
}

/// COV-04b: channel close (`Ok(None)`) on a server channel (Telegram/Discord/Slack,
/// `supports_exit()=false`) returns `GraphStatus::Failed` so the user can `/plan retry`
/// after reconnecting.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn scheduler_loop_channel_close_no_exit_support_returns_failed() {
    use crate::config::OrchestrationConfig;
    use zeph_orchestration::{DagScheduler, RuleBasedRouter};
    use zeph_subagent::SubAgentManager;

    // Channel with exit_supported=false simulates Telegram/Discord/Slack.
    let channel = MockChannel::new(vec![]).without_exit_support();
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    let mut graph = TaskGraph::new("server channel close goal");
    let mut node = TaskNode::new(0, "task-0", "interrupted by infra failure");
    node.status = TaskStatus::Running;
    graph.tasks.push(node);
    graph.status = GraphStatus::Running;

    let config = OrchestrationConfig {
        enabled: true,
        ..OrchestrationConfig::default()
    };
    let mut scheduler =
        DagScheduler::resume_from(graph, &config, Box::new(RuleBasedRouter), vec![]).unwrap();

    let token = tokio_util::sync::CancellationToken::new();
    let status = agent
        .run_scheduler_loop(&mut scheduler, 1, token)
        .await
        .unwrap();

    assert_eq!(
        status,
        GraphStatus::Failed,
        "channel close on server channel (no exit support) must return Failed so the plan can be retried"
    );
}

/// COV-04c: a task completion event that arrives between the last tick and the channel
/// close is captured by the drain tick and honored — the loop returns the natural
/// `Done` status from the drain rather than forcing `Canceled`/`Failed`.
///
/// This verifies the drain-before-cancel ordering (architect S1 fix for #2246):
/// `cancel_all()` empties `self.running`, so any completion event processed AFTER it
/// would be silently discarded. The drain tick must come FIRST while `self.running`
/// is still intact.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn scheduler_loop_channel_close_drain_captures_completion() {
    use crate::config::OrchestrationConfig;
    use zeph_orchestration::{DagScheduler, RuleBasedRouter, TaskEvent, TaskOutcome};
    use zeph_subagent::SubAgentManager;

    // Channel is empty: recv() returns Ok(None) immediately, triggering the close path.
    let channel = MockChannel::new(vec![]);
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // Single-task graph in Running state.  The task is assigned an agent handle so
    // resume_from() reconstructs it in the running map — this is required for
    // process_event() to accept the completion event (it checks self.running).
    let mut graph = TaskGraph::new("drain capture goal");
    let mut node = TaskNode::new(0, "task-0", "completes just before channel close");
    node.status = TaskStatus::Running;
    node.assigned_agent = Some("handle-0".to_string());
    node.agent_hint = Some("worker".to_string());
    graph.tasks.push(node);
    graph.status = GraphStatus::Running;

    let config = OrchestrationConfig {
        enabled: true,
        ..OrchestrationConfig::default()
    };
    let mut scheduler =
        DagScheduler::resume_from(graph, &config, Box::new(RuleBasedRouter), vec![]).unwrap();

    // Inject the completion event via the public event_sender() so it sits in event_rx
    // when the drain tick calls event_rx.try_recv().  This simulates the race: the task
    // finished between the last tick and the channel EOF.
    let event_tx = scheduler.event_sender();
    event_tx
        .send(TaskEvent {
            task_id: scheduler.graph().tasks[0].id,
            agent_handle_id: "handle-0".to_string(),
            outcome: TaskOutcome::Completed {
                output: "finished just in time".to_string(),
                artifacts: vec![],
            },
        })
        .await
        .expect("event_tx send must not fail");
    // Drop the sender so the channel is not kept alive beyond this test.
    drop(event_tx);

    let token = tokio_util::sync::CancellationToken::new();
    let status = agent
        .run_scheduler_loop(&mut scheduler, 1, token)
        .await
        .unwrap();

    // The drain tick processes the completion event while self.running is intact,
    // advances the graph to Completed, and emits Done{Completed}.  The loop must
    // honor this natural Done rather than overriding it with Canceled/Failed.
    assert_eq!(
        status,
        GraphStatus::Completed,
        "drain tick must capture the late completion and return Done(Completed); got {status:?}"
    );
    assert_eq!(
        scheduler.graph().tasks[0].status,
        TaskStatus::Completed,
        "task 0 must be Completed, not Canceled, when its completion is captured by the drain tick"
    );
}

/// COV-05: when channel closes (stdin EOF) while sub-agent tasks are still running,
/// `run_scheduler_loop` must NOT cancel them immediately.  Instead it parks the recv
/// arm (`stdin_closed = true`) and waits for the natural completion event.
///
/// Simulates: piped `echo "/plan confirm" | zeph` — stdin closes before sub-agents finish.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn stdin_closed_parks_when_tasks_running() {
    use crate::config::OrchestrationConfig;
    use zeph_orchestration::{DagScheduler, RuleBasedRouter, TaskEvent, TaskOutcome};
    use zeph_subagent::SubAgentManager;

    // Empty channel: recv() returns Ok(None) immediately, simulating piped stdin EOF.
    let channel = MockChannel::new(vec![]);
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // A single task that is already running when the channel closes.
    let mut graph = TaskGraph::new("piped stdin EOF with running task");
    let mut node = TaskNode::new(0, "task-0", "must finish naturally");
    node.status = TaskStatus::Running;
    node.assigned_agent = Some("handle-0".to_string());
    node.agent_hint = Some("worker".to_string());
    graph.tasks.push(node);
    graph.status = GraphStatus::Running;

    let config = OrchestrationConfig {
        enabled: true,
        ..OrchestrationConfig::default()
    };
    let mut scheduler =
        DagScheduler::resume_from(graph, &config, Box::new(RuleBasedRouter), vec![]).unwrap();

    // Deliver the completion event asynchronously after a short delay so that the loop
    // first observes channel-close (stdin_closed = true), then receives the event.
    let event_tx = scheduler.event_sender();
    let task_id = scheduler.graph().tasks[0].id;
    tokio::spawn(async move {
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        let _ = event_tx
            .send(TaskEvent {
                task_id,
                agent_handle_id: "handle-0".to_string(),
                outcome: TaskOutcome::Completed {
                    output: "natural completion".to_string(),
                    artifacts: vec![],
                },
            })
            .await;
    });

    let token = tokio_util::sync::CancellationToken::new();
    let status = agent
        .run_scheduler_loop(&mut scheduler, 1, token)
        .await
        .unwrap();

    assert_eq!(
        status,
        GraphStatus::Completed,
        "loop must wait for natural task completion after stdin EOF, not cancel immediately; got {status:?}"
    );
    assert_eq!(
        scheduler.graph().tasks[0].status,
        TaskStatus::Completed,
        "task must be Completed, not Canceled, when loop parks on stdin EOF"
    );
}

/// COV-06: when channel closes (stdin EOF) and there are no running tasks,
/// `run_scheduler_loop` must exit immediately with `GraphStatus::Canceled`
/// (existing behavior preserved for empty-scheduler case).
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn stdin_closed_exits_when_no_tasks() {
    use crate::config::OrchestrationConfig;
    use zeph_orchestration::{DagScheduler, RuleBasedRouter};
    use zeph_subagent::SubAgentManager;

    // Empty channel: recv() returns Ok(None) immediately.
    let channel = MockChannel::new(vec![]);
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // Graph has a task in Running state, but no entry in scheduler.running map
    // (simulates the case where the task was already drained before channel close).
    let mut graph = TaskGraph::new("no running tasks on channel close");
    let mut node = TaskNode::new(0, "task-0", "already drained");
    node.status = TaskStatus::Running;
    graph.tasks.push(node);
    graph.status = GraphStatus::Running;

    let config = OrchestrationConfig {
        enabled: true,
        ..OrchestrationConfig::default()
    };
    // resume_from without assigned_agent → running map stays empty.
    let mut scheduler =
        DagScheduler::resume_from(graph, &config, Box::new(RuleBasedRouter), vec![]).unwrap();

    let token = tokio_util::sync::CancellationToken::new();
    let status = agent
        .run_scheduler_loop(&mut scheduler, 1, token)
        .await
        .unwrap();

    assert_eq!(
        status,
        GraphStatus::Canceled,
        "channel close with no running tasks on exit-supporting channel must return Canceled; got {status:?}"
    );
}

/// GAP-9: `handle_plan_status` shows the correct message for each graph status.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_status_reflects_graph_status() {
    // No active plan → "No active plan."
    let mut agent = agent_with_orchestration();
    let out = agent
        .handle_plan_command_as_string(PlanCommand::Status(None))
        .await
        .unwrap();
    assert!(
        out.contains("No active plan"),
        "no plan → 'No active plan'; got: {out:?}"
    );

    // GraphStatus::Created → awaiting confirmation.
    let mut agent = agent_with_orchestration();
    agent.services.orchestration.pending_graph = Some(make_simple_graph(GraphStatus::Created));
    let out = agent
        .handle_plan_command_as_string(PlanCommand::Status(None))
        .await
        .unwrap();
    assert!(
        out.contains("awaiting confirmation"),
        "Created graph → 'awaiting confirmation'; got: {out:?}"
    );

    // GraphStatus::Failed → retry message.
    let mut agent = agent_with_orchestration();
    let mut failed_graph = make_simple_graph(GraphStatus::Created);
    failed_graph.status = GraphStatus::Failed;
    agent.services.orchestration.pending_graph = Some(failed_graph);
    let out = agent
        .handle_plan_command_as_string(PlanCommand::Status(None))
        .await
        .unwrap();
    assert!(
        out.contains("failed") || out.contains("Failed"),
        "Failed graph → failure message; got: {out:?}"
    );

    // GraphStatus::Paused → resume message.
    let mut agent = agent_with_orchestration();
    let mut paused_graph = make_simple_graph(GraphStatus::Created);
    paused_graph.status = GraphStatus::Paused;
    agent.services.orchestration.pending_graph = Some(paused_graph);
    let out = agent
        .handle_plan_command_as_string(PlanCommand::Status(None))
        .await
        .unwrap();
    assert!(
        out.contains("paused") || out.contains("Paused"),
        "Paused graph → paused message; got: {out:?}"
    );

    // GraphStatus::Completed → completed message.
    let mut agent = agent_with_orchestration();
    let mut completed_graph = make_simple_graph(GraphStatus::Created);
    completed_graph.status = GraphStatus::Completed;
    agent.services.orchestration.pending_graph = Some(completed_graph);
    let out = agent
        .handle_plan_command_as_string(PlanCommand::Status(None))
        .await
        .unwrap();
    assert!(
        out.contains("completed") || out.contains("Completed"),
        "Completed graph → completed message; got: {out:?}"
    );
}

/// Regression for #1879: `finalize_plan_execution` with `GraphStatus::Failed` where no
/// tasks actually failed (all canceled due to scheduler deadlock) must emit
/// "Plan canceled. N/M tasks did not run." and NOT "Plan failed. 0/N tasks failed".
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn finalize_plan_execution_deadlock_emits_cancelled_message() {
    use zeph_subagent::SubAgentManager;

    let channel = MockChannel::new(vec![]);
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // Simulate deadlock: graph Failed, one task Blocked → Canceled, one task Pending → Canceled.
    let mut graph = TaskGraph::new("deadlock goal");
    let mut task0 = TaskNode::new(0, "upstream", "will be blocked");
    task0.status = TaskStatus::Canceled;
    let mut task1 = TaskNode::new(1, "downstream", "never ran");
    task1.status = TaskStatus::Canceled;
    graph.tasks.push(task0);
    graph.tasks.push(task1);
    graph.status = GraphStatus::Failed;

    agent
        .finalize_plan_execution(graph, GraphStatus::Failed)
        .await
        .unwrap();

    let msgs = agent.channel.sent_messages();
    // Must NOT say "0/2 tasks failed".
    assert!(
        !msgs.iter().any(|m| m.contains("0/2 tasks failed")),
        "misleading '0/2 tasks failed' message must not appear; got: {msgs:?}"
    );
    // Must say "Plan canceled".
    assert!(
        msgs.iter().any(|m| m.contains("Plan canceled")),
        "must contain 'Plan canceled' for pure deadlock; got: {msgs:?}"
    );
    // Must mention the count of tasks that did not run.
    assert!(
        msgs.iter().any(|m| m.contains("2/2")),
        "must report 2/2 canceled; got: {msgs:?}"
    );
}

/// COV-METRICS-01: `handle_plan_goal` increments `api_calls` and `plans_total` after
/// a successful `LlmPlanner` call. This test covers the production metrics path in
/// `handle_plan_goal` that was not exercised by the `status_command_shows_orchestration_*`
/// tests (which set metrics directly via `update_metrics`).
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn plan_goal_increments_api_calls_and_plans_total() {
    let valid_plan_json = r#"{"tasks": [
        {"task_id": "step-one", "title": "Step one", "description": "Do step one", "depends_on": []}
    ]}"#
    .to_string();

    let provider = mock_provider(vec![valid_plan_json]);
    let channel = MockChannel::new(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let (tx, rx) = watch::channel(MetricsSnapshot::default());
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor).with_metrics(tx);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent
        .services
        .orchestration
        .orchestration_config
        .confirm_before_execute = true;

    agent
        .handle_plan_command_as_string(PlanCommand::Goal("build something".to_owned()))
        .await
        .unwrap();

    let snapshot = rx.borrow().clone();
    assert_eq!(
        snapshot.api_calls, 1,
        "api_calls must be incremented by 1 after a successful plan() call; got: {}",
        snapshot.api_calls
    );
    assert_eq!(
        snapshot.orchestration.plans_total, 1,
        "plans_total must be incremented by 1 after plan() succeeds; got: {}",
        snapshot.orchestration.plans_total
    );
    assert_eq!(
        snapshot.orchestration.tasks_total, 1,
        "tasks_total must match the number of tasks in the plan; got: {}",
        snapshot.orchestration.tasks_total
    );
}

/// COV-METRICS-02: `finalize_plan_execution` with `GraphStatus::Completed` increments
/// `api_calls` for the aggregator call and updates `tasks_completed` / `tasks_skipped`.
/// This covers the aggregator metrics path that was not tested end-to-end.
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn finalize_plan_execution_completed_increments_aggregator_metrics() {
    use zeph_subagent::SubAgentManager;

    // Provider returns the aggregation synthesis.
    let provider = mock_provider(vec!["synthesis".into()]);
    let channel = MockChannel::new(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let (tx, rx) = watch::channel(MetricsSnapshot::default());
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor).with_metrics(tx);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    // Graph with one completed and one skipped task.
    let mut graph = TaskGraph::new("metrics finalize test");
    let mut completed = TaskNode::new(0, "task-done", "desc");
    completed.status = TaskStatus::Completed;
    completed.result = Some(TaskResult {
        output: "ok".into(),
        artifacts: vec![],
        duration_ms: 5,
        agent_id: None,
        agent_def: None,
    });
    let mut skipped = TaskNode::new(1, "task-skip", "desc");
    skipped.status = TaskStatus::Skipped;
    graph.tasks.push(completed);
    graph.tasks.push(skipped);
    graph.status = GraphStatus::Completed;

    agent
        .finalize_plan_execution(graph, GraphStatus::Completed)
        .await
        .unwrap();

    let snapshot = rx.borrow().clone();
    assert_eq!(
        snapshot.api_calls, 1,
        "api_calls must be incremented by 1 for the aggregator LLM call; got: {}",
        snapshot.api_calls
    );
    assert_eq!(
        snapshot.orchestration.tasks_completed, 1,
        "tasks_completed must be 1; got: {}",
        snapshot.orchestration.tasks_completed
    );
    assert_eq!(
        snapshot.orchestration.tasks_skipped, 1,
        "tasks_skipped must be 1; got: {}",
        snapshot.orchestration.tasks_skipped
    );
}

/// Regression for #1879: mixed failure — some tasks failed, some canceled.
/// Message must say "Plan failed. X/M tasks failed, Y canceled:" (not misleading).
#[cfg(feature = "scheduler")]
#[tokio::test]
async fn finalize_plan_execution_mixed_failed_and_cancelled() {
    use zeph_subagent::SubAgentManager;

    let channel = MockChannel::new(vec![]);
    let provider = mock_provider(vec![]);
    let registry = create_test_registry();
    let executor = MockToolExecutor::no_tools();
    let mut agent = Agent::new(provider, channel, registry, None, 5, executor);
    agent.services.orchestration.orchestration_config.enabled = true;
    agent.services.orchestration.subagent_manager = Some(SubAgentManager::new(4));

    let mut graph = TaskGraph::new("mixed goal");
    let mut failed = TaskNode::new(0, "failed-task", "really failed");
    failed.status = TaskStatus::Failed;
    failed.result = Some(TaskResult {
        output: "error: something went wrong".into(),
        artifacts: vec![],
        duration_ms: 100,
        agent_id: None,
        agent_def: None,
    });
    let mut cancelled = TaskNode::new(1, "cancelled-task", "never ran");
    cancelled.status = TaskStatus::Canceled;
    graph.tasks.push(failed);
    graph.tasks.push(cancelled);
    graph.status = GraphStatus::Failed;

    agent
        .finalize_plan_execution(graph, GraphStatus::Failed)
        .await
        .unwrap();

    let msgs = agent.channel.sent_messages();
    // Must say "Plan failed." (not "Plan canceled.").
    assert!(
        msgs.iter().any(|m| m.contains("Plan failed")),
        "mixed state must say 'Plan failed'; got: {msgs:?}"
    );
    // Must mention canceled count.
    assert!(
        msgs.iter().any(|m| m.contains("canceled")),
        "must mention canceled tasks in mixed state; got: {msgs:?}"
    );
    // Must list failed task.
    assert!(
        msgs.iter().any(|m| m.contains("failed-task")),
        "must list the failed task; got: {msgs:?}"
    );
}