orch-cli 0.105.13

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
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
use super::super::*;
use crate::store::control::parse_since_duration;
use chrono::TimeDelta;
use chrono::Utc;

#[tokio::test]
async fn cost_summary_aggregates_correctly() {
    let store = TaskStore::open_memory().await.unwrap();

    let id1 = store
        .create(&NewTask {
            external_id: Some("1".to_string()),
            repo: "owner/repo".to_string(),
            origin: "github".to_string(),
            title: "Task 1".to_string(),
            ..Default::default()
        })
        .await
        .unwrap();
    let id2 = store
        .create(&NewTask {
            external_id: Some("2".to_string()),
            repo: "owner/repo".to_string(),
            origin: "github".to_string(),
            title: "Task 2".to_string(),
            ..Default::default()
        })
        .await
        .unwrap();

    store
        .store_tokens(id1, 1000, 500, Some(0.25))
        .await
        .unwrap();
    store
        .store_tokens(id2, 2000, 1000, Some(0.5))
        .await
        .unwrap();

    let (input, output, cost) = store.cost_summary("owner/repo").await.unwrap();
    assert_eq!(input, 3000);
    assert_eq!(output, 1500);
    assert_eq!(cost, 0.75);
}

#[tokio::test]
async fn status_counts_groups_correctly() {
    let store = TaskStore::open_memory().await.unwrap();

    for i in 0..3 {
        store
            .create(&NewTask {
                external_id: Some(format!("{i}")),
                repo: "owner/repo".to_string(),
                origin: "github".to_string(),
                title: format!("Task {i}"),
                ..Default::default()
            })
            .await
            .unwrap();
    }

    // Move task 1 to routed, task 2 to done
    store.update_status(2, TaskStatus::Routed).await.unwrap();
    store.update_status(3, TaskStatus::Done).await.unwrap();

    let counts = store.status_counts("owner/repo").await.unwrap();
    assert_eq!(counts.get("new"), Some(&1));
    assert_eq!(counts.get("routed"), Some(&1));
    assert_eq!(counts.get("done"), Some(&1));
}

#[tokio::test]
async fn cost_summary_returns_zeros_for_empty_repo() {
    let store = TaskStore::open_memory().await.unwrap();
    let (input, output, cost) = store.cost_summary("empty/repo").await.unwrap();
    assert_eq!(input, 0);
    assert_eq!(output, 0);
    assert!((cost - 0.0).abs() < f64::EPSILON);
}

#[tokio::test]
async fn status_counts_returns_empty_map_for_empty_repo() {
    let store = TaskStore::open_memory().await.unwrap();
    let counts = store.status_counts("empty/repo").await.unwrap();
    assert!(counts.is_empty());
}

#[tokio::test]
async fn cost_summary_across_multiple_tasks() {
    let store = TaskStore::open_memory().await.unwrap();

    for i in 1..=3 {
        let id = store
            .create(&NewTask {
                external_id: Some(i.to_string()),
                repo: "owner/repo".to_string(),
                origin: "github".to_string(),
                title: format!("Task {i}"),
                body: String::new(),
                source: String::new(),
                source_id: String::new(),
                author: String::new(),
                url: String::new(),
                labels: vec![],
                parent_id: None,
            })
            .await
            .unwrap();
        store
            .store_tokens(id, 1000 * i, 500 * i, None)
            .await
            .unwrap();
    }

    let (input, output, _cost) = store.cost_summary("owner/repo").await.unwrap();
    assert_eq!(input, 6000); // 1000 + 2000 + 3000
    assert_eq!(output, 3000); // 500 + 1000 + 1500
}

#[tokio::test]
async fn status_counts_with_mixed_statuses() {
    let store = TaskStore::open_memory().await.unwrap();

    for i in 1..=5 {
        let id = store
            .create(&NewTask {
                external_id: Some(i.to_string()),
                repo: "owner/repo".to_string(),
                origin: "github".to_string(),
                title: format!("Task {i}"),
                body: String::new(),
                source: String::new(),
                source_id: String::new(),
                author: String::new(),
                url: String::new(),
                labels: vec![],
                parent_id: None,
            })
            .await
            .unwrap();
        if i <= 2 {
            store
                .update_status(id, TaskStatus::InProgress)
                .await
                .unwrap();
        } else if i == 3 {
            store.update_status(id, TaskStatus::Done).await.unwrap();
        }
        // Tasks 4-5 stay New
    }

    let counts = store.status_counts("owner/repo").await.unwrap();
    assert_eq!(counts.get("new").copied().unwrap_or(0), 2);
    assert_eq!(counts.get("in_progress").copied().unwrap_or(0), 2);
    assert_eq!(counts.get("done").copied().unwrap_or(0), 1);
}

#[tokio::test]
async fn insert_and_query_task_metric() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();

    let metric = InsertTaskMetric {
        repo: "",
        task_id: "42",
        agent: "claude",
        model: Some("opus"),
        complexity: Some("complex"),
        outcome: "success",
        duration_seconds: 120.5,
        started_at: &now,
        completed_at: &now,
        attempts: 1,
        files_changed: 3,
        error_type: None,
        input_tokens: Some(5000),
        output_tokens: Some(2000),
        total_cost_usd: Some(0.15),
    };

    let id = store.insert_task_metric(&metric).await.unwrap();
    assert!(id > 0);
}

#[tokio::test]
async fn metrics_summary_empty() {
    let store = TaskStore::open_memory().await.unwrap();
    let summary = store.get_metrics_summary_24h().await.unwrap();
    assert_eq!(summary.tasks_completed_24h, 0);
    assert_eq!(summary.tasks_failed_24h, 0);
    assert!(summary.agent_stats.is_empty());
}

#[tokio::test]
async fn metrics_summary_counts_success_and_failure() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();

    // Insert a success
    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "",
            task_id: "1",
            agent: "claude",
            model: None,
            complexity: Some("simple"),
            outcome: "success",
            duration_seconds: 60.0,
            started_at: &now,
            completed_at: &now,
            attempts: 1,
            files_changed: 1,
            error_type: None,
            input_tokens: None,
            output_tokens: None,
            total_cost_usd: None,
        })
        .await
        .unwrap();

    // Insert a failure
    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "",
            task_id: "2",
            agent: "codex",
            model: None,
            complexity: Some("medium"),
            outcome: "failed",
            duration_seconds: 30.0,
            started_at: &now,
            completed_at: &now,
            attempts: 2,
            files_changed: 0,
            error_type: Some("timeout"),
            input_tokens: None,
            output_tokens: None,
            total_cost_usd: None,
        })
        .await
        .unwrap();

    let summary = store.get_metrics_summary_24h().await.unwrap();
    assert_eq!(summary.tasks_completed_24h, 1);
    assert_eq!(summary.tasks_failed_24h, 1);
    assert_eq!(summary.agent_stats.len(), 2);
}

#[tokio::test]
async fn metrics_summary_excludes_rows_before_cutoff_on_same_calendar_day() {
    let store = TaskStore::open_memory().await.unwrap();

    // `completed_at` is stored as RFC3339 ('T' separator) but SQLite's
    // `datetime('now', '-24 hours')` produces a space-separated string. 'T' (0x54)
    // sorts after ' ' (0x20), so a plain string `>=` comparison used to treat any
    // row sharing the cutoff's calendar day as "within the window" regardless of
    // its actual time-of-day. Midnight of the cutoff's own day is unambiguously
    // >=24h in the past yet shares that day's date with the cutoff, so it
    // reproduces the bug deterministically without depending on wall-clock timing.
    let cutoff_str: String = sqlx::query_scalar("SELECT datetime('now', '-24 hours')")
        .fetch_one(&store.pool)
        .await
        .unwrap();
    let cutoff_date = &cutoff_str[..10];

    let midnight = chrono::NaiveDateTime::parse_from_str(
        &format!("{cutoff_date} 00:00:01"),
        "%Y-%m-%d %H:%M:%S",
    )
    .unwrap()
    .and_utc();

    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "",
            task_id: "old",
            agent: "claude",
            model: None,
            complexity: None,
            outcome: "success",
            duration_seconds: 10.0,
            started_at: &midnight,
            completed_at: &midnight,
            attempts: 1,
            files_changed: 0,
            error_type: None,
            input_tokens: None,
            output_tokens: None,
            total_cost_usd: None,
        })
        .await
        .unwrap();

    let summary = store.get_metrics_summary(24).await.unwrap();
    assert_eq!(
        summary.tasks_completed_24h, 0,
        "a row from midnight of the cutoff's calendar day is >=24h old and must not be counted"
    );
}

#[tokio::test]
async fn cost_summary_returns_correct_periods() {
    let store = TaskStore::open_memory().await.unwrap();
    let summary = store.get_cost_summary().await.unwrap();
    assert_eq!(summary.periods.len(), 3);
    assert_eq!(summary.periods[0].label, "24h");
    assert_eq!(summary.periods[1].label, "7d");
    assert_eq!(summary.periods[2].label, "30d");
}

#[tokio::test]
async fn cost_by_agent_empty() {
    let store = TaskStore::open_memory().await.unwrap();
    let groups = store.get_cost_by_agent().await.unwrap();
    assert!(groups.is_empty());
}

#[tokio::test]
async fn cost_by_model_empty() {
    let store = TaskStore::open_memory().await.unwrap();
    let groups = store.get_cost_by_model().await.unwrap();
    assert!(groups.is_empty());
}

#[tokio::test]
async fn cost_by_agent_groups_correctly() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();

    for (agent, cost) in &[("claude", 0.10), ("codex", 0.05), ("claude", 0.20)] {
        store
            .insert_task_metric(&InsertTaskMetric {
                repo: "",
                task_id: "1",
                agent,
                model: None,
                complexity: None,
                outcome: "success",
                duration_seconds: 10.0,
                started_at: &now,
                completed_at: &now,
                attempts: 1,
                files_changed: 0,
                error_type: None,
                input_tokens: None,
                output_tokens: None,
                total_cost_usd: Some(*cost),
            })
            .await
            .unwrap();
    }

    let groups = store.get_cost_by_agent().await.unwrap();
    assert_eq!(groups.len(), 2);
    // claude should be first (higher total cost)
    assert_eq!(groups[0].name, "claude");
    assert_eq!(groups[0].task_count, 2);
    assert!((groups[0].total_cost_usd - 0.30).abs() < 0.001);
}

#[tokio::test]
async fn cost_by_model_groups_correctly() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();

    for (model, cost) in &[("sonnet", 0.05), ("opus", 0.15), ("sonnet", 0.10)] {
        store
            .insert_task_metric(&InsertTaskMetric {
                repo: "",
                task_id: "1",
                agent: "claude",
                model: Some(model),
                complexity: None,
                outcome: "success",
                duration_seconds: 10.0,
                started_at: &now,
                completed_at: &now,
                attempts: 1,
                files_changed: 0,
                error_type: None,
                input_tokens: None,
                output_tokens: None,
                total_cost_usd: Some(*cost),
            })
            .await
            .unwrap();
    }

    let groups = store.get_cost_by_model().await.unwrap();
    assert_eq!(groups.len(), 2);
    // sonnet should be first (higher total cost: 0.15)
    assert_eq!(groups[0].name, "sonnet");
    assert_eq!(groups[0].task_count, 2);
    assert!((groups[0].total_cost_usd - 0.15).abs() < 0.001);
    assert_eq!(groups[1].name, "opus");
    assert_eq!(groups[1].task_count, 1);
}

#[tokio::test]
async fn record_rate_limit_returns_id() {
    let store = TaskStore::open_memory().await.unwrap();
    let id = store
        .record_rate_limit("claude", "rate", Some("42"))
        .await
        .unwrap();
    assert!(id > 0);
}

#[tokio::test]
async fn rate_limits_counted_in_metrics_summary() {
    let store = TaskStore::open_memory().await.unwrap();
    store
        .record_rate_limit("claude", "rate", None)
        .await
        .unwrap();
    store
        .record_rate_limit("codex", "auth", Some("5"))
        .await
        .unwrap();

    let summary = store.get_metrics_summary_24h().await.unwrap();
    assert_eq!(summary.rate_limits_24h, 2);
}

#[tokio::test]
async fn self_improvement_counter_starts_at_zero() {
    let store = TaskStore::open_memory().await.unwrap();
    let count = store.count_self_improvement_issues_7d().await.unwrap();
    assert_eq!(count, 0);
}

#[tokio::test]
async fn self_improvement_counter_increments() {
    let store = TaskStore::open_memory().await.unwrap();
    store.increment_self_improvement_counter().await.unwrap();
    store.increment_self_improvement_counter().await.unwrap();
    let count = store.count_self_improvement_issues_7d().await.unwrap();
    assert_eq!(count, 2);
}

#[tokio::test]
async fn slow_tasks_empty() {
    let store = TaskStore::open_memory().await.unwrap();
    let slow = store.get_slow_tasks_7d().await.unwrap();
    assert!(slow.is_empty());
}

#[tokio::test]
async fn error_distribution_empty() {
    let store = TaskStore::open_memory().await.unwrap();
    let errors = store.get_error_distribution(24 * 7).await.unwrap();
    assert!(errors.is_empty());
}

#[tokio::test]
async fn high_review_cycle_tasks_empty() {
    let store = TaskStore::open_memory().await.unwrap();
    let tasks = store.get_high_review_cycle_tasks_7d().await.unwrap();
    assert!(tasks.is_empty());
}

#[tokio::test]
async fn high_review_cycle_tasks_filters_by_threshold() {
    let store = TaskStore::open_memory().await.unwrap();

    // Create tasks with different review_cycles values
    let id_low = store
        .create_internal("owner/repo", "Low cycles", "", "manual", "", None)
        .await
        .unwrap();
    let id_high = store
        .create_internal("owner/repo", "High cycles", "", "manual", "", None)
        .await
        .unwrap();
    let id_boundary = store
        .create_internal("owner/repo", "At boundary", "", "manual", "", None)
        .await
        .unwrap();

    // Set review_cycles: 0, 2, 3
    store.increment(id_low, "review_cycles").await.unwrap();
    // id_low has review_cycles=1, should be excluded

    store.increment(id_high, "review_cycles").await.unwrap();
    store.increment(id_high, "review_cycles").await.unwrap();
    store.increment(id_high, "review_cycles").await.unwrap();
    // id_high has review_cycles=3, should be included

    store.increment(id_boundary, "review_cycles").await.unwrap();
    store.increment(id_boundary, "review_cycles").await.unwrap();
    // id_boundary has review_cycles=2, should be included (>=2)

    let tasks = store.get_high_review_cycle_tasks_7d().await.unwrap();
    assert_eq!(tasks.len(), 2);
    // Should be sorted by review_cycles DESC
    assert_eq!(tasks[0].title, "High cycles");
    assert_eq!(tasks[0].review_cycles, 3);
    assert_eq!(tasks[1].title, "At boundary");
    assert_eq!(tasks[1].review_cycles, 2);
}

#[tokio::test]
async fn high_review_cycle_tasks_excludes_old_tasks() {
    let store = TaskStore::open_memory().await.unwrap();

    let id_old = store
        .create_internal("owner/repo", "Old task", "", "manual", "", None)
        .await
        .unwrap();
    store.increment(id_old, "review_cycles").await.unwrap();
    store.increment(id_old, "review_cycles").await.unwrap();

    // Backdate to 10 days ago (outside 7d window)
    sqlx::query("UPDATE tasks SET updated_at = datetime('now', '-10 days') WHERE id = ?")
        .bind(id_old)
        .execute(&store.pool)
        .await
        .unwrap();

    let id_recent = store
        .create_internal("owner/repo", "Recent task", "", "manual", "", None)
        .await
        .unwrap();
    store.increment(id_recent, "review_cycles").await.unwrap();
    store.increment(id_recent, "review_cycles").await.unwrap();

    let tasks = store.get_high_review_cycle_tasks_7d().await.unwrap();
    assert_eq!(tasks.len(), 1);
    assert_eq!(tasks[0].title, "Recent task");
}

#[tokio::test]
async fn error_distribution_groups_by_type() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();

    for error_type in &["timeout", "timeout", "rate_limit"] {
        store
            .insert_task_metric(&InsertTaskMetric {
                repo: "",
                task_id: "1",
                agent: "claude",
                model: None,
                complexity: None,
                outcome: "failed",
                duration_seconds: 10.0,
                started_at: &now,
                completed_at: &now,
                attempts: 1,
                files_changed: 0,
                error_type: Some(error_type),
                input_tokens: None,
                output_tokens: None,
                total_cost_usd: None,
            })
            .await
            .unwrap();
    }

    let errors = store.get_error_distribution(24 * 7).await.unwrap();
    assert_eq!(errors.len(), 2);
    // timeout should be first (count=2)
    assert_eq!(errors[0].error_type.as_deref(), Some("timeout"));
    assert_eq!(errors[0].count, 2);
}

#[tokio::test]
async fn slow_tasks_returns_sorted_by_duration() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();

    // Insert three metrics with different durations
    for (task_id, duration) in &[("t1", 120.0), ("t2", 300.0), ("t3", 60.0)] {
        store
            .insert_task_metric(&InsertTaskMetric {
                repo: "",
                task_id,
                agent: "claude",
                model: Some("sonnet"),
                complexity: Some("medium"),
                outcome: "success",
                duration_seconds: *duration,
                started_at: &now,
                completed_at: &now,
                attempts: 1,
                files_changed: 1,
                error_type: None,
                input_tokens: None,
                output_tokens: None,
                total_cost_usd: None,
            })
            .await
            .unwrap();
    }

    let slow = store.get_slow_tasks(24 * 7).await.unwrap();
    assert_eq!(slow.len(), 3);
    // Should be sorted descending by duration
    assert_eq!(slow[0].task_id, "t2");
    assert!((slow[0].duration_seconds - 300.0).abs() < 0.01);
    assert_eq!(slow[1].task_id, "t1");
    assert_eq!(slow[2].task_id, "t3");
}

#[tokio::test]
async fn cost_summary_returns_three_periods() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();

    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "",
            task_id: "cost1",
            agent: "claude",
            model: Some("sonnet"),
            complexity: None,
            outcome: "success",
            duration_seconds: 10.0,
            started_at: &now,
            completed_at: &now,
            attempts: 1,
            files_changed: 1,
            error_type: None,
            input_tokens: Some(1000),
            output_tokens: Some(500),
            total_cost_usd: Some(0.0105),
        })
        .await
        .unwrap();

    let summary = store.get_cost_summary().await.unwrap();
    assert_eq!(summary.periods.len(), 3);
    assert_eq!(summary.periods[0].label, "24h");
    assert_eq!(summary.periods[1].label, "7d");
    assert_eq!(summary.periods[2].label, "30d");
    // All three windows should include our recent metric
    for period in &summary.periods {
        assert_eq!(period.task_count, 1);
        assert!(period.total_cost_usd > 0.0);
    }
}

#[tokio::test]
async fn metrics_summary_by_repo_filters_correctly() {
    let store = TaskStore::open_memory().await.unwrap();

    // Create tasks in different repos
    let id1 = store
        .create_internal("owner/orch", "Task A", "", "test", "", None)
        .await
        .unwrap();
    let id2 = store
        .create_internal("owner/bean", "Task B", "", "test", "", None)
        .await
        .unwrap();

    let now = chrono::Utc::now();
    let ago = now - chrono::Duration::minutes(30);

    // Insert metric for orch
    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "owner/orch",
            task_id: &id1.to_string(),
            agent: "claude",
            model: Some("sonnet"),
            complexity: Some("simple"),
            outcome: "success",
            duration_seconds: 60.0,
            started_at: &ago,
            completed_at: &now,
            attempts: 1,
            files_changed: 2,
            error_type: None,
            input_tokens: None,
            output_tokens: None,
            total_cost_usd: None,
        })
        .await
        .unwrap();

    // Insert metric for bean
    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "owner/bean",
            task_id: &id2.to_string(),
            agent: "codex",
            model: Some("gpt-5"),
            complexity: Some("medium"),
            outcome: "success",
            duration_seconds: 120.0,
            started_at: &ago,
            completed_at: &now,
            attempts: 1,
            files_changed: 1,
            error_type: None,
            input_tokens: None,
            output_tokens: None,
            total_cost_usd: None,
        })
        .await
        .unwrap();

    // Query per-repo: orch
    let orch_stats = store
        .get_metrics_summary_by_repo("owner/orch", 24)
        .await
        .unwrap();
    assert_eq!(orch_stats.tasks_completed_24h, 1);
    assert_eq!(orch_stats.agent_stats.len(), 1);
    assert_eq!(orch_stats.agent_stats[0].agent, "claude");

    // Query per-repo: bean
    let bean_stats = store
        .get_metrics_summary_by_repo("owner/bean", 24)
        .await
        .unwrap();
    assert_eq!(bean_stats.tasks_completed_24h, 1);
    assert_eq!(bean_stats.agent_stats.len(), 1);
    assert_eq!(bean_stats.agent_stats[0].agent, "codex");

    // Global should show both
    let all_stats = store.get_metrics_summary_24h().await.unwrap();
    assert_eq!(all_stats.tasks_completed_24h, 2);
}

/// Regression test: when a metric's task_id matches both an internal id AND an
/// external_id of a DIFFERENT task in the same repo, the metric must NOT fan out
/// to both rows (which would double-count it).
///
/// Uses raw SQL to inspect base query output and detect fan-out directly.
#[tokio::test]
async fn metrics_summary_by_repo_no_duplication_on_id_collision() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = chrono::Utc::now();

    // Create a task and capture its auto-increment id.
    let id1 = store
        .create_internal("owner/orch", "Task A", "", "test", "", None)
        .await
        .unwrap();

    // Set its external_id to "999" — a sentinel value that won't collide naturally.
    store.update_external_id(id1, "999").await.unwrap();

    // Create a second task. Its auto-increment id is unknown, but we need a
    // numeric-string collision. We'll look for it after creation.
    let id2 = store
        .create_internal("owner/orch", "Task B", "", "test", "", None)
        .await
        .unwrap();

    // If id2 happens to collide with id1's external_id, we've found our collision.
    let id2_str = id2.to_string();
    let collision = id2_str == "999";

    // Insert a metric for task A, referencing it by external_id.
    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "",
            task_id: "999",
            agent: "claude",
            model: None,
            complexity: Some("simple"),
            outcome: "success",
            duration_seconds: 30.0,
            started_at: &now,
            completed_at: &now,
            attempts: 1,
            files_changed: 1,
            error_type: None,
            input_tokens: None,
            output_tokens: None,
            total_cost_usd: None,
        })
        .await
        .unwrap();

    // Also insert a metric for task B (non-colliding) so the comparison is meaningful.
    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "",
            task_id: &id2_str,
            agent: "claude",
            model: None,
            complexity: Some("medium"),
            outcome: "success",
            duration_seconds: 20.0,
            started_at: &now,
            completed_at: &now,
            attempts: 1,
            files_changed: 1,
            error_type: None,
            input_tokens: None,
            output_tokens: None,
            total_cost_usd: None,
        })
        .await
        .unwrap();

    // Directly inspect the base CTE output to detect fan-out.
    // If the fix works, id2=999 produces 1 row; without the fix, it produces 2 rows
    // (one via id=999 match, one via external_id=999 match).
    let pool = store.pool();
    let base_row_count: i64 = sqlx::query_scalar(
        "SELECT COUNT(*) FROM task_metrics m
         LEFT JOIN tasks t ON m.task_id = CAST(t.id AS TEXT)
             OR (m.task_id = t.external_id AND NOT EXISTS (
                 SELECT 1 FROM tasks t2 WHERE t2.id = CAST(m.task_id AS INTEGER) AND t2.repo = t.repo
             ))
         WHERE m.task_id = ?",
    )
    .bind(&id2_str)
    .fetch_one(pool)
    .await
    .unwrap();

    // The metric for task B should map to exactly 1 task row, not fan out.
    assert_eq!(
        base_row_count, 1,
        "metric must not fan out to multiple task rows"
    );

    // Also check the full summary — we should have exactly 2 tasks (not 3 if fan-out occurred).
    let summary = store
        .get_metrics_summary_by_repo("owner/orch", 24)
        .await
        .unwrap();
    if collision {
        // When id2 coincidentally equals "999", the single metric for task A
        // must not be double-counted just because both task A (via external_id)
        // and task B (via id) match the same task_id value.
        assert_eq!(
            summary.tasks_completed_24h, 1,
            "metric must not be double-counted on id collision"
        );
    } else {
        // No collision occurred (expected in most test runs).
        // Verify the non-colliding path works: 2 tasks → 2 completed.
        assert_eq!(summary.tasks_completed_24h, 2);
    }
}

#[tokio::test]
async fn metrics_summary_rate_limits_are_repo_scoped() {
    let store = TaskStore::open_memory().await.unwrap();

    let id1 = store
        .create_internal("owner/orch", "Task A", "", "test", "", None)
        .await
        .unwrap();
    let id2 = store
        .create_internal("owner/bean", "Task B", "", "test", "", None)
        .await
        .unwrap();

    store
        .record_rate_limit("claude", "rate_limit", Some(&id1.to_string()))
        .await
        .unwrap();
    store
        .record_rate_limit("claude", "rate_limit", Some(&id1.to_string()))
        .await
        .unwrap();
    store
        .record_rate_limit("codex", "rate_limit", Some(&id2.to_string()))
        .await
        .unwrap();

    let orch_summary = store
        .get_metrics_summary_by_repo("owner/orch", 24)
        .await
        .unwrap();
    assert_eq!(
        orch_summary.rate_limits_24h, 2,
        "orch should only see its own 2 rate limits"
    );

    let bean_summary = store
        .get_metrics_summary_by_repo("owner/bean", 24)
        .await
        .unwrap();
    assert_eq!(
        bean_summary.rate_limits_24h, 1,
        "bean should only see its own 1 rate limit"
    );

    let all_summary = store.get_metrics_summary_24h().await.unwrap();
    assert_eq!(
        all_summary.rate_limits_24h, 3,
        "global should see all 3 rate limits"
    );
}

#[tokio::test]
async fn metrics_summary_rate_limits_no_duplication_on_id_external_id_collision() {
    let store = TaskStore::open_memory().await.unwrap();

    let id1 = store
        .create_internal("owner/orch", "Task A", "", "test", "", None)
        .await
        .unwrap();
    let id2 = store
        .create_internal("owner/orch", "Task B", "", "test", "", None)
        .await
        .unwrap();

    // Force an id/external_id collision across two different tasks.
    store
        .update_external_id(id2, &id1.to_string())
        .await
        .unwrap();

    // This event references task A by numeric id; without the NOT EXISTS guard
    // it would fan out to task A (id match) and task B (external_id match).
    store
        .record_rate_limit("claude", "rate_limit", Some(&id1.to_string()))
        .await
        .unwrap();

    let orch_summary = store
        .get_metrics_summary_by_repo("owner/orch", 24)
        .await
        .unwrap();
    assert_eq!(
        orch_summary.rate_limits_24h, 1,
        "rate limit event must not be double-counted on id/external_id collision"
    );
}

#[tokio::test]
async fn metrics_summary_rate_limits_cross_repo_numeric_collision() {
    let store = TaskStore::open_memory().await.unwrap();

    // Create an internal task in repo B which will get id = 1
    let id_b = store
        .create_internal("owner/bean", "Task B", "", "test", "", None)
        .await
        .unwrap();

    // Upsert an external task in repo A with external_id equal to id_b (numeric string)
    let _id_a = store
        .upsert_external(&UpsertExternal {
            repo: "owner/orch",
            ext_id: &id_b.to_string(),
            title: "External A",
            body: "",
            author: "user",
            url: "https://github.com/owner/orch/issues/1",
            labels: &[],
            origin: "github",
        })
        .await
        .unwrap();

    // Record a rate limit event that references task_id = "<id_b>" — it should
    // resolve to the external task in owner/orch, not be suppressed by the
    // existence of an unrelated internal task with the same numeric id in owner/bean.
    store
        .record_rate_limit("claude", "rate_limit", Some(&id_b.to_string()))
        .await
        .unwrap();

    let orch_summary = store
        .get_metrics_summary_by_repo("owner/orch", 24)
        .await
        .unwrap();

    assert_eq!(
        orch_summary.rate_limits_24h, 1,
        "orch should see its rate limit event even when an unrelated repo has the same numeric id"
    );

    let bean_summary = store
        .get_metrics_summary_by_repo("owner/bean", 24)
        .await
        .unwrap();

    // bean should not see the orch event (it belongs to orch)
    assert_eq!(
        bean_summary.rate_limits_24h, 0,
        "bean must not see orch's rate limit event"
    );
}

#[tokio::test]
async fn cost_summary_by_repo_isolates_per_repo() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();

    // Insert a metric for owner/orch
    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "owner/orch",
            task_id: "orch-1",
            agent: "claude",
            model: Some("sonnet"),
            complexity: None,
            outcome: "success",
            duration_seconds: 10.0,
            started_at: &now,
            completed_at: &now,
            attempts: 1,
            files_changed: 1,
            error_type: None,
            input_tokens: Some(1000),
            output_tokens: Some(500),
            total_cost_usd: Some(0.0105),
        })
        .await
        .unwrap();

    // Insert a metric for owner/bean
    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "owner/bean",
            task_id: "bean-1",
            agent: "codex",
            model: Some("gpt-4"),
            complexity: None,
            outcome: "success",
            duration_seconds: 20.0,
            started_at: &now,
            completed_at: &now,
            attempts: 1,
            files_changed: 2,
            error_type: None,
            input_tokens: Some(2000),
            output_tokens: Some(1000),
            total_cost_usd: Some(0.021),
        })
        .await
        .unwrap();

    // owner/orch should only see its own cost
    let orch_cost = store
        .get_cost_summary_by_repo("owner/orch", 24)
        .await
        .unwrap();
    assert_eq!(orch_cost.periods.len(), 1);
    assert_eq!(orch_cost.periods[0].label, "24h");
    assert_eq!(orch_cost.periods[0].task_count, 1);
    assert!((orch_cost.periods[0].total_cost_usd - 0.0105).abs() < 1e-6);

    // owner/bean should only see its own cost
    let bean_cost = store
        .get_cost_summary_by_repo("owner/bean", 24)
        .await
        .unwrap();
    assert_eq!(bean_cost.periods[0].task_count, 1);
    assert!((bean_cost.periods[0].total_cost_usd - 0.021).abs() < 1e-6);

    // Unknown repo returns zeros
    let unknown = store
        .get_cost_summary_by_repo("unknown/repo", 24)
        .await
        .unwrap();
    assert_eq!(unknown.periods[0].task_count, 0);
    assert_eq!(unknown.periods[0].total_cost_usd, 0.0);
}

#[tokio::test]
async fn cost_summary_by_repo_falls_back_to_tasks_join() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();

    // Create a task with a known repo so the join can resolve it
    let task_id = store
        .create_internal("owner/orch", "task", "body", "manual", "", None)
        .await
        .unwrap();

    // Insert metric with empty repo — repo will be resolved via tasks join
    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "",
            task_id: &task_id.to_string(),
            agent: "claude",
            model: None,
            complexity: None,
            outcome: "success",
            duration_seconds: 5.0,
            started_at: &now,
            completed_at: &now,
            attempts: 1,
            files_changed: 0,
            error_type: None,
            input_tokens: Some(500),
            output_tokens: Some(200),
            total_cost_usd: Some(0.0045),
        })
        .await
        .unwrap();

    let cost = store
        .get_cost_summary_by_repo("owner/orch", 24)
        .await
        .unwrap();
    assert_eq!(cost.periods[0].task_count, 1);
    assert!((cost.periods[0].total_cost_usd - 0.0045).abs() < 1e-6);
}

#[tokio::test]
async fn cost_summary_by_repo_uses_requested_window_label() {
    let store = TaskStore::open_memory().await.unwrap();
    let now = Utc::now();
    let eight_days_ago = now - TimeDelta::days(8);

    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "owner/orch",
            task_id: "recent",
            agent: "claude",
            model: Some("sonnet"),
            complexity: None,
            outcome: "success",
            duration_seconds: 10.0,
            started_at: &now,
            completed_at: &now,
            attempts: 1,
            files_changed: 1,
            error_type: None,
            input_tokens: Some(100),
            output_tokens: Some(50),
            total_cost_usd: Some(0.003),
        })
        .await
        .unwrap();

    store
        .insert_task_metric(&InsertTaskMetric {
            repo: "owner/orch",
            task_id: "older",
            agent: "claude",
            model: Some("sonnet"),
            complexity: None,
            outcome: "success",
            duration_seconds: 12.0,
            started_at: &eight_days_ago,
            completed_at: &eight_days_ago,
            attempts: 1,
            files_changed: 1,
            error_type: None,
            input_tokens: Some(200),
            output_tokens: Some(100),
            total_cost_usd: Some(0.006),
        })
        .await
        .unwrap();

    let summary_7d = store
        .get_cost_summary_by_repo("owner/orch", 24 * 7)
        .await
        .unwrap();
    assert_eq!(summary_7d.periods[0].label, "7d");
    assert_eq!(summary_7d.periods[0].task_count, 1);
    assert!((summary_7d.periods[0].total_cost_usd - 0.003).abs() < 1e-6);

    let summary_30d = store
        .get_cost_summary_by_repo("owner/orch", 24 * 30)
        .await
        .unwrap();
    assert_eq!(summary_30d.periods[0].label, "30d");
    assert_eq!(summary_30d.periods[0].task_count, 2);
    assert!((summary_30d.periods[0].total_cost_usd - 0.009).abs() < 1e-6);
}

#[test]
fn parse_since_duration_days() {
    let ts = parse_since_duration("7d").unwrap();
    // The timestamp must be a well-formed RFC3339 string (e.g. 2026-03-18T09:56:02Z).
    assert_eq!(ts.len(), 20, "unexpected length: {ts:?}");
    // Must be in RFC3339 format: starts with year, contains 'T', ends with 'Z'.
    assert!(
        ts.starts_with("20") && ts.contains('T') && ts.ends_with('Z'),
        "expected RFC3339 format: {ts:?}"
    );
    // Must be in the past (less than now).
    let now_ts = parse_since_duration("0d");
    assert!(now_ts.is_err(), "0d should be rejected");
}

#[test]
fn parse_since_duration_hours() {
    let ts = parse_since_duration("24h").unwrap();
    assert_eq!(ts.len(), 20);
}

#[test]
fn parse_since_duration_minutes() {
    let ts = parse_since_duration("30m").unwrap();
    assert_eq!(ts.len(), 20);
}

#[test]
fn parse_since_duration_invalid() {
    assert!(parse_since_duration("abc").is_err());
    assert!(parse_since_duration("7").is_err());
    assert!(parse_since_duration("").is_err());
    assert!(parse_since_duration("0h").is_err());
}

#[tokio::test]
async fn recent_rate_limit_counts_empty() {
    let store = TaskStore::open_memory().await.unwrap();
    let counts = store.recent_rate_limit_counts(6).await.unwrap();
    assert!(counts.is_empty());
}

#[tokio::test]
async fn recent_rate_limit_counts_groups_by_agent() {
    let store = TaskStore::open_memory().await.unwrap();
    store
        .record_rate_limit("claude", "rate", None)
        .await
        .unwrap();
    store
        .record_rate_limit("claude", "error", None)
        .await
        .unwrap();
    store
        .record_rate_limit("codex", "rate", None)
        .await
        .unwrap();

    let counts = store.recent_rate_limit_counts(6).await.unwrap();
    assert_eq!(counts.get("claude"), Some(&1));
    assert_eq!(counts.get("codex"), Some(&1));
    assert_eq!(counts.get("opencode"), None);
}

#[tokio::test]
async fn recent_rate_limit_counts_skips_rows_before_cleared_at() {
    let store = TaskStore::open_memory().await.unwrap();
    let past = (chrono::Utc::now() - chrono::Duration::seconds(30))
        .format("%Y-%m-%d %H:%M:%S")
        .to_string();
    let future = (chrono::Utc::now() + chrono::Duration::seconds(30))
        .format("%Y-%m-%d %H:%M:%S")
        .to_string();

    for _ in 0..3 {
        store
            .record_rate_limit_at("claude", "rate", None, &past)
            .await
            .unwrap();
    }
    store
        .kv_set(
            "rate_limits_cleared_at:claude",
            &chrono::Utc::now().timestamp().to_string(),
        )
        .await
        .unwrap();
    store
        .record_rate_limit_at("claude", "rate", None, &future)
        .await
        .unwrap();
    store
        .record_rate_limit_at("codex", "rate", None, &past)
        .await
        .unwrap();

    let counts = store.recent_rate_limit_counts(6).await.unwrap();
    assert_eq!(counts.get("claude"), Some(&1));
    assert_eq!(counts.get("codex"), Some(&1));
}