cpm-planner 0.4.3

Critical Path Method planner as an MCP server: schedules task graphs (slack, critical path, resource levelling, Monte Carlo), leases lock-aware parallel cohorts to agents, versions plan-as-code variants, and tracks earned value.
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
//! Lock-lifecycle tests for `BasicCpmPlanner`.
//!
//! Includes the SPEC-mandated concurrent-acquire race test and the TTL
//! expiry test (both audited via a capturing `MemoryAuditSink`).

use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;

use chrono::{DateTime, TimeZone, Utc};
use cpm_planner::audit::MemoryAuditSink;
use cpm_planner::plan::{
    AcceptRequest, AcquireRequest, CallerId, Deliverable, DeliverableStatus, ForceReleaseRequest,
    HeartbeatRequest, MarkStatusRequest, PlanGraph, PlannerError,
};
use cpm_planner::ports::Planner;
use cpm_planner::{BasicCpmPlanner, MAX_ATTEMPTS, MAX_LAPSES};

fn deliverable(id: &str, files: &[&str], prereqs: &[&str], effort: Option<f32>) -> Deliverable {
    Deliverable {
        id: id.to_string(),
        owned_files: files
            .iter()
            .map(|f| cpm_planner::plan::OwnedFile::from(*f))
            .collect(),
        prerequisites: prereqs.iter().map(|s| (*s).into()).collect(),
        estimated_effort_hours: effort,
        metadata: serde_json::Value::Null,
        duration_hours: None,
        estimate: None,
        milestone: false,
        earning_rule: None,
    }
}

fn appending(id: &str, path: &str) -> Deliverable {
    let mut d = deliverable(id, &[], &[], Some(1.0));
    d.owned_files = vec![cpm_planner::plan::OwnedFile::Claim {
        path: PathBuf::from(path),
        mode: Some(cpm_planner::plan::FileMode::Append),
    }];
    d
}

fn caller(id: &str) -> CallerId {
    CallerId(id.to_string())
}

/// Mutable, thread-safe `now` source. Tests advance it by calling `set`.
#[derive(Clone)]
struct TestClock {
    now: Arc<Mutex<DateTime<Utc>>>,
}

impl TestClock {
    fn new(start: DateTime<Utc>) -> Self {
        Self {
            now: Arc::new(Mutex::new(start)),
        }
    }

    fn set(&self, when: DateTime<Utc>) {
        *self.now.lock().expect("test clock not poisoned") = when;
    }

    fn read(&self) -> DateTime<Utc> {
        *self.now.lock().expect("test clock not poisoned")
    }
}

#[tokio::test]
async fn ttl_expiry_test() {
    let audit = Arc::new(MemoryAuditSink::new());
    let clock = TestClock::new(Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap());
    let clock_arc = clock.clone();
    let planner = BasicCpmPlanner::with_parts(
        audit.clone(),
        Duration::from_secs(60),
        Arc::new(move || clock_arc.read()),
    );

    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("c1").clone(),
            1,
        ))
        .await
        .unwrap();
    assert_eq!(cohort.rows[0].deliverable.id, "a");

    // Advance time past TTL. Next acquire_cohort should reap the expired
    // lock and re-offer `a` to a different caller.
    clock.set(Utc.with_ymd_and_hms(2026, 1, 1, 0, 5, 0).unwrap());

    let cohort2 = planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("c2").clone(),
            1,
        ))
        .await
        .unwrap();
    assert_eq!(cohort2.rows.len(), 1);
    assert_eq!(cohort2.rows[0].deliverable.id, "a");
    assert_eq!(cohort2.rows[0].lock.caller_id, caller("c2"));

    // Audit log carries an expiry event for the original lock.
    let events = audit.snapshot();
    let expiry = events
        .iter()
        .find(|e| e.event_type == "plan.lock.expired")
        .expect("plan.lock.expired emitted");
    assert_eq!(expiry.payload["deliverable_id"], "a");
    assert_eq!(expiry.payload["last_caller_id"], "c1");
}

#[tokio::test]
async fn acquire_with_ttl_sets_lock_expiry() {
    let audit = Arc::new(MemoryAuditSink::new());
    let t0 = Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
    let clock = TestClock::new(t0);
    let clock_arc = clock.clone();
    let planner = BasicCpmPlanner::with_parts(
        audit,
        Duration::from_secs(5 * 60),
        Arc::new(move || clock_arc.read()),
    );
    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    let cohort = planner
        .acquire_cohort(
            AcquireRequest::new(plan_id, caller("c1"), 1)
                .with_ttl(Duration::from_secs(2 * 60 * 60)),
        )
        .await
        .unwrap();
    assert_eq!(
        cohort.rows[0].lock.expires_at,
        t0 + chrono::Duration::hours(2)
    );
}

#[tokio::test]
async fn acquire_ttl_above_max_is_clamped() {
    let audit = Arc::new(MemoryAuditSink::new());
    let t0 = Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
    let clock = TestClock::new(t0);
    let clock_arc = clock.clone();
    let planner = BasicCpmPlanner::with_parts(
        audit,
        Duration::from_secs(5 * 60),
        Arc::new(move || clock_arc.read()),
    )
    .with_max_ttl(Duration::from_secs(60 * 60));
    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    let cohort = planner
        .acquire_cohort(
            AcquireRequest::new(plan_id, caller("c1"), 1)
                .with_ttl(Duration::from_secs(5 * 60 * 60)),
        )
        .await
        .unwrap();
    assert_eq!(
        cohort.rows[0].lock.expires_at,
        t0 + chrono::Duration::hours(1)
    );
}

#[tokio::test]
async fn heartbeat_with_ttl_extends_to_requested_duration() {
    let audit = Arc::new(MemoryAuditSink::new());
    let t0 = Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
    let clock = TestClock::new(t0);
    let clock_arc = clock.clone();
    let planner = BasicCpmPlanner::with_parts(
        audit,
        Duration::from_secs(5 * 60),
        Arc::new(move || clock_arc.read()),
    );
    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    planner
        .acquire_cohort(AcquireRequest::new(plan_id.clone(), caller("c1"), 1))
        .await
        .unwrap();
    planner
        .heartbeat(
            HeartbeatRequest::new(plan_id.clone(), "a", caller("c1"))
                .with_ttl(Duration::from_secs(3 * 60 * 60)),
        )
        .await
        .unwrap();
    let lock = planner
        .status(&plan_id)
        .await
        .unwrap()
        .locks_held
        .into_iter()
        .find(|l| l.deliverable_id == "a")
        .expect("lock held");
    assert_eq!(lock.expires_at, t0 + chrono::Duration::hours(3));
}

#[tokio::test]
async fn lease_with_long_ttl_survives_past_default_ttl() {
    let audit = Arc::new(MemoryAuditSink::new());
    let t0 = Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
    let clock = TestClock::new(t0);
    let clock_arc = clock.clone();
    let planner = BasicCpmPlanner::with_parts(
        audit,
        Duration::from_secs(5 * 60),
        Arc::new(move || clock_arc.read()),
    );
    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    planner
        .acquire_cohort(
            AcquireRequest::new(plan_id.clone(), caller("c1"), 1)
                .with_ttl(Duration::from_secs(60 * 60)),
        )
        .await
        .unwrap();
    clock.set(t0 + chrono::Duration::minutes(30));
    let second = planner
        .acquire_cohort(AcquireRequest::new(plan_id, caller("c2"), 1))
        .await
        .unwrap();
    assert!(
        second.rows.is_empty(),
        "long-TTL lease must not be reaped after the default 5-minute TTL"
    );
}

/// Defect fix: a lease lost to the ENVIRONMENT (driver killed externally,
/// lock lapses via TTL, no terminal mark) is NOT an implementation
/// attempt. MAX_ATTEMPTS environmental lapses must not trip the failure
/// circuit-breaker — the deliverable stays leasable.
#[tokio::test]
async fn environmental_lapses_do_not_trip_the_failure_circuit_breaker() {
    let audit = Arc::new(MemoryAuditSink::new());
    let t0 = Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
    let clock = TestClock::new(t0);
    let clock_arc = clock.clone();
    let planner = BasicCpmPlanner::with_parts(
        audit.clone(),
        Duration::from_secs(60),
        Arc::new(move || clock_arc.read()),
    );

    let graph = PlanGraph {
        deliverables: vec![deliverable("healthy", &["src/healthy.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();

    // MAX_ATTEMPTS leases, each lost environmentally: no mark_status, the
    // lock is left to lapse via TTL, and the acquire-path reap reverts the
    // deliverable to Ready.
    for lapse in 1..=MAX_ATTEMPTS {
        let cohort = planner
            .acquire_cohort(AcquireRequest::new(
                plan_id.clone(),
                caller(&format!("killed-{lapse}")).clone(),
                1,
            ))
            .await
            .unwrap();
        assert_eq!(cohort.rows.len(), 1, "lease {lapse} must be granted");
        clock.set(t0 + chrono::Duration::minutes(5 * i64::from(lapse)));
    }

    // The next acquire must STILL lease it: lapses are not failed attempts.
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("fresh").clone(),
            1,
        ))
        .await
        .unwrap();
    assert_eq!(
        cohort.rows.len(),
        1,
        "a healthy deliverable was circuit-broken by environmental lapses"
    );
    assert_eq!(cohort.rows[0].deliverable.id, "healthy");

    // Counters tell the two stories apart: every lease counted, every
    // lapse counted, zero implementation failures.
    let status = planner.status(&plan_id).await.unwrap();
    let (_, d_status, attempts, failures, lapses) = &status.deliverables[0];
    assert_eq!(*d_status, DeliverableStatus::InProgress);
    assert_eq!(*attempts, MAX_ATTEMPTS + 1, "all leases counted");
    assert_eq!(*failures, 0, "a lapse is not an implementation failure");
    assert_eq!(*lapses, MAX_ATTEMPTS, "every TTL lapse counted");
}

/// Circuit-breaker: a deliverable EXPLICITLY marked failed (plan.mark_status
/// status=failed) on every attempt is auto-failed after MAX_ATTEMPTS real
/// failed attempts instead of being re-leased forever.
#[tokio::test]
async fn poison_deliverable_circuit_breaks_after_max_failed_attempts() {
    let audit = Arc::new(MemoryAuditSink::new());
    let planner = BasicCpmPlanner::with_audit(audit.clone());

    let graph = PlanGraph {
        deliverables: vec![
            deliverable("poison", &["src/poison.rs"], &[], Some(1.0)),
            deliverable("dependent", &["src/dep.rs"], &["poison"], Some(1.0)),
        ],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();

    // MAX_ATTEMPTS real attempts: each lease ends with the driver
    // EXPLICITLY reporting failure, and the orchestrator re-marking the
    // deliverable ready for another try.
    for attempt in 1..=MAX_ATTEMPTS {
        let who = caller(&format!("builder-{attempt}"));
        let cohort = planner
            .acquire_cohort(AcquireRequest::new(plan_id.clone(), who.clone(), 1))
            .await
            .unwrap();
        assert_eq!(cohort.rows.len(), 1, "attempt {attempt} must lease");
        assert_eq!(cohort.rows[0].deliverable.id, "poison");
        planner
            .mark_status(MarkStatusRequest::new(
                plan_id.clone(),
                "poison",
                who.clone(),
                DeliverableStatus::Failed {
                    reason: format!("build attempt {attempt} broke"),
                },
            ))
            .await
            .unwrap();
        if attempt < MAX_ATTEMPTS {
            // Orchestrator retry: back into the pool.
            planner
                .mark_status(MarkStatusRequest::new(
                    plan_id.clone(),
                    "poison",
                    who.clone(),
                    DeliverableStatus::Ready,
                ))
                .await
                .unwrap();
        }
    }
    // Final retry attempt puts it back to Ready with the budget spent.
    planner
        .mark_status(MarkStatusRequest::new(
            plan_id.clone(),
            "poison",
            caller("orchestrator").clone(),
            DeliverableStatus::Ready,
        ))
        .await
        .unwrap();

    // The next acquire must NOT lease it a fourth time: it circuit-breaks
    // to Failed and the cohort comes back empty.
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("fresh").clone(),
            10,
        ))
        .await
        .unwrap();
    assert!(
        cohort.rows.is_empty(),
        "circuit-broken deliverable was re-leased: {:?}",
        cohort
            .rows
            .iter()
            .map(|r| r.deliverable.id.clone())
            .collect::<Vec<_>>()
    );

    let status = planner.status(&plan_id).await.unwrap();
    let (_, poison_status, poison_attempts, poison_failures, poison_lapses) = status
        .deliverables
        .iter()
        .find(|(id, _, _, _, _)| id == "poison")
        .expect("poison entry present");
    match poison_status {
        DeliverableStatus::Failed { reason } => assert_eq!(
            reason,
            &format!("circuit-break: exceeded {MAX_ATTEMPTS} failed attempts"),
            "auto-fail reason must carry the circuit-break marker"
        ),
        other => panic!("expected Failed after circuit-break, got {other:?}"),
    }
    assert_eq!(*poison_attempts, MAX_ATTEMPTS);
    assert_eq!(*poison_failures, MAX_ATTEMPTS);
    assert_eq!(*poison_lapses, 0, "no lease ever lapsed in this scenario");
    assert!(
        status.locks_held.is_empty(),
        "auto-failed deliverable must hold no lock"
    );

    // The dependent of the failed prereq stays Pending — never Ready,
    // never leased. That's correct: a failed prerequisite means it can't
    // run; the plan converges instead of blocking on it.
    let (_, dep_status, dep_attempts, _, _) = status
        .deliverables
        .iter()
        .find(|(id, _, _, _, _)| id == "dependent")
        .expect("dependent entry present");
    assert_eq!(*dep_status, DeliverableStatus::Pending);
    assert_eq!(*dep_attempts, 0);

    // Audit trail carries the circuit-break event.
    let evt = audit
        .snapshot()
        .into_iter()
        .find(|e| e.event_type == "plan.deliverable.circuit_broken")
        .expect("circuit_broken event present");
    assert_eq!(evt.payload["deliverable_id"], "poison");
    assert_eq!(evt.payload["failure_count"], MAX_ATTEMPTS);
    assert_eq!(evt.payload["max_attempts"], MAX_ATTEMPTS);

    // And it is never handed out again on later acquires either.
    let again = planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("much-later").clone(),
            10,
        ))
        .await
        .unwrap();
    assert!(again.rows.is_empty(), "failed deliverable re-leased later");
}

/// Planner whose lease on "stuck" has lapsed `MAX_LAPSES` times (the final
/// reap happens on the next acquire). "healthy" is independent and
/// file-disjoint; it is released between rounds without counting a lapse.
async fn planner_with_lapse_limited_stuck() -> (
    BasicCpmPlanner,
    Arc<MemoryAuditSink>,
    TestClock,
    cpm_planner::plan::PlanId,
) {
    let audit = Arc::new(MemoryAuditSink::new());
    let t0 = Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
    let clock = TestClock::new(t0);
    let clock_arc = clock.clone();
    let planner = BasicCpmPlanner::with_parts(
        audit.clone(),
        Duration::from_secs(60),
        Arc::new(move || clock_arc.read()),
    );
    let graph = PlanGraph {
        deliverables: vec![
            deliverable("stuck", &["src/stuck.rs"], &[], Some(1.0)),
            deliverable("healthy", &["src/healthy.rs"], &[], Some(1.0)),
        ],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    for lapse in 1..=MAX_LAPSES {
        let cohort = planner
            .acquire_cohort(AcquireRequest::new(
                plan_id.clone(),
                caller(&format!("killed-{lapse}")),
                5,
            ))
            .await
            .unwrap();
        assert!(cohort_ids(&cohort).contains(&"stuck".to_string()));
        planner
            .force_release(ForceReleaseRequest::new(
                plan_id.clone(),
                "healthy",
                "reset",
            ))
            .await
            .unwrap();
        clock.set(t0 + chrono::Duration::minutes(5 * i64::from(lapse)));
    }
    (planner, audit, clock, plan_id)
}

fn cohort_ids(cohort: &cpm_planner::plan::Cohort) -> Vec<String> {
    cohort
        .rows
        .iter()
        .map(|r| r.deliverable.id.clone())
        .collect()
}

fn status_of(status: &cpm_planner::plan::PlanStatus, id: &str) -> DeliverableStatus {
    status
        .deliverables
        .iter()
        .find(|row| row.0 == id)
        .expect("deliverable present in status")
        .1
        .clone()
}

fn lapse_count_of(status: &cpm_planner::plan::PlanStatus, id: &str) -> u32 {
    status
        .deliverables
        .iter()
        .find(|row| row.0 == id)
        .expect("deliverable present in status")
        .4
}

#[tokio::test]
async fn requested_lapse_limited_id_is_blocked_lapse_limit() {
    let (planner, _audit, _clock, plan_id) = planner_with_lapse_limited_stuck().await;
    let cohort = planner
        .acquire_cohort(
            AcquireRequest::new(plan_id, caller("fresh"), 5).with_ids(vec!["stuck".to_string()]),
        )
        .await
        .unwrap();
    assert_eq!(
        cohort
            .blocked
            .iter()
            .map(|b| (b.id.as_str(), b.code.as_str()))
            .collect::<Vec<_>>(),
        vec![("stuck", "LAPSE_LIMIT")]
    );
}

#[tokio::test]
async fn acquire_skips_lapse_limited_deliverable_and_leases_the_rest() {
    let (planner, _audit, _clock, plan_id) = planner_with_lapse_limited_stuck().await;
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(plan_id, caller("fresh"), 5))
        .await
        .unwrap();
    assert_eq!(cohort_ids(&cohort), vec!["healthy"]);
}

#[tokio::test]
async fn acquire_reports_lapse_limited_deliverable_as_blocked() {
    let (planner, _audit, _clock, plan_id) = planner_with_lapse_limited_stuck().await;
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(plan_id, caller("fresh"), 5))
        .await
        .unwrap();
    assert_eq!(
        cohort
            .blocked
            .iter()
            .map(|b| (b.id.as_str(), b.code.as_str()))
            .collect::<Vec<_>>(),
        vec![("stuck", "LAPSE_LIMIT")]
    );
}

#[tokio::test]
async fn blocked_reason_names_the_reset_counters_remediation() {
    let (planner, _audit, _clock, plan_id) = planner_with_lapse_limited_stuck().await;
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(plan_id, caller("fresh"), 5))
        .await
        .unwrap();
    assert!(
        cohort.blocked[0]
            .reason
            .contains("plan.force_release {reset_counters: true}")
    );
}

#[tokio::test]
async fn lapse_limit_error_message_keeps_prefix() {
    let err = PlannerError::LapseLimit {
        deliverable_id: "stuck".to_string(),
        lapse_count: MAX_LAPSES,
        max_lapses: MAX_LAPSES,
    };
    assert!(err.to_string().starts_with("LAPSE_LIMIT:"));
}

#[tokio::test]
async fn force_release_with_reset_counters_makes_deliverable_acquirable_again() {
    let (planner, _audit, _clock, plan_id) = planner_with_lapse_limited_stuck().await;
    planner
        .acquire_cohort(AcquireRequest::new(plan_id.clone(), caller("fresh"), 5))
        .await
        .unwrap();
    planner
        .force_release(
            ForceReleaseRequest::new(plan_id.clone(), "stuck", "env fixed").reset_counters(true),
        )
        .await
        .unwrap();
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(plan_id, caller("again"), 5))
        .await
        .unwrap();
    assert!(cohort_ids(&cohort).contains(&"stuck".to_string()));
}

#[tokio::test]
async fn force_release_without_reset_keeps_lapse_count() {
    let (planner, _audit, _clock, plan_id) = planner_with_lapse_limited_stuck().await;
    planner
        .acquire_cohort(AcquireRequest::new(plan_id.clone(), caller("fresh"), 5))
        .await
        .unwrap();
    planner
        .force_release(
            ForceReleaseRequest::new(plan_id.clone(), "stuck", "no reset").reset_counters(false),
        )
        .await
        .unwrap();
    let status = planner.status(&plan_id).await.unwrap();
    assert_eq!(lapse_count_of(&status, "stuck"), MAX_LAPSES);
}

#[tokio::test]
async fn reset_counters_revives_circuit_broken_deliverable() {
    let planner = BasicCpmPlanner::new();
    let graph = PlanGraph {
        deliverables: vec![deliverable("poison", &["src/poison.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    for attempt in 1..=MAX_ATTEMPTS {
        let who = caller(&format!("builder-{attempt}"));
        planner
            .acquire_cohort(AcquireRequest::new(plan_id.clone(), who.clone(), 1))
            .await
            .unwrap();
        planner
            .mark_status(MarkStatusRequest::new(
                plan_id.clone(),
                "poison",
                who.clone(),
                DeliverableStatus::Failed {
                    reason: "broke".to_string(),
                },
            ))
            .await
            .unwrap();
        planner
            .mark_status(MarkStatusRequest::new(
                plan_id.clone(),
                "poison",
                who,
                DeliverableStatus::Ready,
            ))
            .await
            .unwrap();
    }
    // The next acquire circuit-breaks it to Failed.
    planner
        .acquire_cohort(AcquireRequest::new(plan_id.clone(), caller("fresh"), 1))
        .await
        .unwrap();
    planner
        .force_release(
            ForceReleaseRequest::new(plan_id.clone(), "poison", "fixed").reset_counters(true),
        )
        .await
        .unwrap();
    let status = planner.status(&plan_id).await.unwrap();
    assert!(matches!(
        status_of(&status, "poison"),
        DeliverableStatus::Ready
    ));
}

#[tokio::test]
async fn reset_counters_emits_audit_event() {
    let (planner, audit, _clock, plan_id) = planner_with_lapse_limited_stuck().await;
    planner
        .force_release(ForceReleaseRequest::new(plan_id, "stuck", "env fixed").reset_counters(true))
        .await
        .unwrap();
    let events = audit.snapshot();
    assert!(
        events
            .iter()
            .any(|e| e.event_type == "plan.deliverable.counters_reset"
                && e.payload["deliverable_id"] == "stuck")
    );
}

/// A deliverable that completes normally on its first lease is untouched
/// by the circuit-breaker: its attempt_count stops at 1.
#[tokio::test]
async fn completed_deliverable_attempt_count_stops_at_one() {
    let planner = BasicCpmPlanner::new();
    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();

    planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("c1").clone(),
            1,
        ))
        .await
        .unwrap();
    planner
        .mark_status(MarkStatusRequest::new(
            plan_id.clone(),
            "a",
            caller("c1").clone(),
            DeliverableStatus::Complete,
        ))
        .await
        .unwrap();

    let status = planner.status(&plan_id).await.unwrap();
    assert_eq!(
        status.deliverables,
        vec![("a".to_string(), DeliverableStatus::Complete, 1, 0, 0)]
    );

    // A further acquire neither re-leases it nor bumps the counter.
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("c2").clone(),
            1,
        ))
        .await
        .unwrap();
    assert!(cohort.rows.is_empty());
    let status = planner.status(&plan_id).await.unwrap();
    assert_eq!(status.deliverables[0].2, 1);
}

/// attempt_count increments on LEASE only: a Ready candidate left behind
/// because the cohort was already full is not charged an attempt.
#[tokio::test]
async fn unleased_candidate_is_not_charged_an_attempt() {
    let planner = BasicCpmPlanner::new();
    let graph = PlanGraph {
        deliverables: vec![
            deliverable("first", &["src/first.rs"], &[], Some(4.0)),
            deliverable("second", &["src/second.rs"], &[], Some(1.0)),
        ],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();

    // max_count = 1: exactly one of the two Ready candidates is leased.
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("c1").clone(),
            1,
        ))
        .await
        .unwrap();
    assert_eq!(cohort.rows.len(), 1);
    let leased = cohort.rows[0].deliverable.id.clone();

    let status = planner.status(&plan_id).await.unwrap();
    for (id, deliverable_status, attempts, _, _) in &status.deliverables {
        if *id == leased {
            assert_eq!(*attempts, 1, "leased deliverable counts one attempt");
        } else {
            assert_eq!(*attempts, 0, "unleased candidate must not be charged");
            assert_eq!(*deliverable_status, DeliverableStatus::Ready);
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn concurrent_acquire_race_test() {
    // Repeat the test ten times to weed out any latent nondeterminism in
    // the locking implementation. Each iteration uses a fresh planner.
    for iteration in 0..10 {
        let planner = Arc::new(BasicCpmPlanner::new());
        // BasicCpmPlanner enforces graph-level file disjointness at
        // submit time, so we can't encode overlap statically. The race
        // we DO want to exercise is the runtime lock-map check: with 6
        // independent deliverables and 4 callers each requesting 2,
        // total demand (8) exceeds supply (6). The planner MUST
        // serialise via the top-level mutex such that no deliverable
        // (and therefore no owned file) is ever returned in two
        // separate cohorts.
        let graph = PlanGraph {
            deliverables: (0..6)
                .map(|i| deliverable(&format!("d{i}"), &[&format!("src/d{i}.rs")], &[], Some(1.0)))
                .collect(),
            max_chained_dispatch: None,
        };
        let plan_id = planner.submit_plan(graph).await.unwrap();

        let mut handles = Vec::new();
        for c in 0..4 {
            let planner_c = planner.clone();
            let plan_id_c = plan_id.clone();
            handles.push(tokio::spawn(async move {
                planner_c
                    .acquire_cohort(AcquireRequest::new(
                        plan_id_c.clone(),
                        caller(&format!("c{c}")).clone(),
                        2,
                    ))
                    .await
                    .expect("acquire_cohort should not error")
            }));
        }

        let mut all_files: Vec<PathBuf> = Vec::new();
        let mut all_ids: Vec<String> = Vec::new();
        let mut total_returned = 0usize;
        for h in handles {
            let cohort = h.await.unwrap();
            total_returned += cohort.rows.len();
            for row in &cohort.rows {
                let d = &row.deliverable;
                all_ids.push(d.id.clone());
                for f in &d.owned_files {
                    all_files.push(f.path().to_path_buf());
                }
                // F5 INTERFACE_GAP-001: row pairing is type-enforced;
                // this assertion still documents the operator-facing
                // expectation.
                assert_eq!(
                    row.lock.deliverable_id, d.id,
                    "iter {iteration}: row.lock did not match row.deliverable"
                );
            }
        }

        // (a) no file appears in two callers' returned cohorts
        let mut seen = std::collections::HashSet::new();
        for f in &all_files {
            assert!(
                seen.insert(f.clone()),
                "iter {iteration}: file {f:?} appeared in two cohorts"
            );
        }

        // (b) no deliverable appears in two cohorts
        let mut seen_ids = std::collections::HashSet::new();
        for id in &all_ids {
            assert!(
                seen_ids.insert(id.clone()),
                "iter {iteration}: deliverable {id} appeared in two cohorts"
            );
        }

        // (c) total deliverables ≤ 6 (the graph size).
        assert!(
            total_returned <= 6,
            "iter {iteration}: over-allocation {total_returned} > 6"
        );
    }
}

#[tokio::test]
async fn audit_emission_on_lock_lifecycle() {
    let audit = Arc::new(MemoryAuditSink::new());
    let planner = BasicCpmPlanner::with_audit(audit.clone());
    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();

    // Acquire -> acquired event.
    planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("c1").clone(),
            1,
        ))
        .await
        .unwrap();

    // Complete -> released event.
    planner
        .mark_status(MarkStatusRequest::new(
            plan_id.clone(),
            "a",
            caller("c1").clone(),
            DeliverableStatus::Complete,
        ))
        .await
        .unwrap();

    let types: Vec<String> = audit
        .snapshot()
        .iter()
        .map(|e| e.event_type.clone())
        .collect();
    assert!(types.contains(&"plan.lock.acquired".to_string()));
    assert!(types.contains(&"plan.lock.released".to_string()));

    // Released event should carry reason="completed".
    let released = audit
        .snapshot()
        .into_iter()
        .find(|e| e.event_type == "plan.lock.released")
        .expect("released event present");
    assert_eq!(released.payload["reason"], "completed");
}

#[tokio::test]
async fn failed_status_emits_released_with_reason_failed() {
    let audit = Arc::new(MemoryAuditSink::new());
    let planner = BasicCpmPlanner::with_audit(audit.clone());
    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("c1").clone(),
            1,
        ))
        .await
        .unwrap();

    planner
        .mark_status(MarkStatusRequest::new(
            plan_id.clone(),
            "a",
            caller("c1").clone(),
            DeliverableStatus::Failed {
                reason: "compilation broke".to_string(),
            },
        ))
        .await
        .unwrap();

    let released = audit
        .snapshot()
        .into_iter()
        .find(|e| e.event_type == "plan.lock.released")
        .expect("released event present");
    assert_eq!(released.payload["reason"], "failed");
}

#[tokio::test]
async fn force_release_audit_includes_reason() {
    let audit = Arc::new(MemoryAuditSink::new());
    let planner = BasicCpmPlanner::with_audit(audit.clone());
    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    planner
        .acquire_cohort(AcquireRequest::new(
            plan_id.clone(),
            caller("c1").clone(),
            1,
        ))
        .await
        .unwrap();

    let reason = "operator escape: caller wedged";
    planner
        .force_release(ForceReleaseRequest::new(plan_id.clone(), "a", reason))
        .await
        .unwrap();

    let evt = audit
        .snapshot()
        .into_iter()
        .find(|e| e.event_type == "plan.lock.force_released")
        .expect("force_released event present");
    assert_eq!(evt.payload["reason"], reason);
    assert_eq!(evt.payload["deliverable_id"], "a");
    assert_eq!(evt.payload["last_caller_id"], "c1");
}

#[tokio::test]
async fn accept_ignores_expired_foreign_lease() {
    let t0 = Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
    let clock = TestClock::new(t0);
    let clock_arc = clock.clone();
    let planner = BasicCpmPlanner::with_parts(
        Arc::new(MemoryAuditSink::new()),
        Duration::from_secs(60),
        Arc::new(move || clock_arc.read()),
    );
    let graph = PlanGraph {
        deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
        max_chained_dispatch: None,
    };
    let plan_id = planner.submit_plan(graph).await.unwrap();
    planner
        .acquire_cohort(AcquireRequest::new(plan_id.clone(), caller("w1"), 1))
        .await
        .unwrap();
    clock.set(t0 + chrono::Duration::minutes(5));
    let result = planner
        .accept(AcceptRequest::new(plan_id, "a", "owner", "ok"))
        .await;
    assert!(result.is_ok(), "got: {result:?}");
}

async fn lock_expiry(
    planner: &BasicCpmPlanner,
    plan_id: &cpm_planner::plan::PlanId,
) -> DateTime<Utc> {
    planner
        .status(plan_id)
        .await
        .unwrap()
        .locks_held
        .into_iter()
        .find(|l| l.deliverable_id == "a")
        .expect("lock held")
        .expires_at
}

async fn long_lease_planner() -> (BasicCpmPlanner, TestClock, cpm_planner::plan::PlanId) {
    let t0 = Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
    let clock = TestClock::new(t0);
    let clock_arc = clock.clone();
    let planner = BasicCpmPlanner::with_parts(
        Arc::new(MemoryAuditSink::new()),
        Duration::from_secs(5 * 60),
        Arc::new(move || clock_arc.read()),
    );
    let plan_id = planner
        .submit_plan(PlanGraph {
            deliverables: vec![deliverable("a", &["src/a.rs"], &[], Some(1.0))],
            max_chained_dispatch: None,
        })
        .await
        .unwrap();
    planner
        .acquire_cohort(
            AcquireRequest::new(plan_id.clone(), caller("c1"), 1)
                .with_ttl(Duration::from_secs(2 * 60 * 60)),
        )
        .await
        .unwrap();
    (planner, clock, plan_id)
}

#[tokio::test]
async fn heartbeat_without_ttl_never_shortens_long_lease() {
    let (planner, clock, plan_id) = long_lease_planner().await;
    clock.set(Utc.with_ymd_and_hms(2026, 1, 1, 0, 10, 0).unwrap());
    planner
        .heartbeat(HeartbeatRequest::new(plan_id.clone(), "a", caller("c1")))
        .await
        .unwrap();
    assert_eq!(
        lock_expiry(&planner, &plan_id).await,
        Utc.with_ymd_and_hms(2026, 1, 1, 2, 0, 0).unwrap()
    );
}

#[tokio::test]
async fn heartbeat_with_explicit_shorter_ttl_shortens_lease() {
    let (planner, clock, plan_id) = long_lease_planner().await;
    clock.set(Utc.with_ymd_and_hms(2026, 1, 1, 0, 10, 0).unwrap());
    planner
        .heartbeat(
            HeartbeatRequest::new(plan_id.clone(), "a", caller("c1"))
                .with_ttl(Duration::from_secs(60)),
        )
        .await
        .unwrap();
    assert_eq!(
        lock_expiry(&planner, &plan_id).await,
        Utc.with_ymd_and_hms(2026, 1, 1, 0, 11, 0).unwrap()
    );
}

#[tokio::test]
async fn lapse_limited_deliverable_is_not_auto_failed() {
    let (planner, _audit, _clock, plan_id) = planner_with_lapse_limited_stuck().await;
    planner
        .acquire_cohort(AcquireRequest::new(plan_id.clone(), caller("fresh"), 5))
        .await
        .unwrap();
    let status = planner.status(&plan_id).await.unwrap();
    assert_eq!(status_of(&status, "stuck"), DeliverableStatus::Ready);
}

#[tokio::test]
async fn reset_counters_event_is_attributed_to_operator() {
    let (planner, audit, _clock, plan_id) = planner_with_lapse_limited_stuck().await;
    planner
        .force_release(ForceReleaseRequest::new(plan_id, "stuck", "env fixed").reset_counters(true))
        .await
        .unwrap();
    let evt = audit
        .snapshot()
        .into_iter()
        .find(|e| e.event_type == "plan.deliverable.counters_reset")
        .expect("counters_reset event");
    assert_eq!(evt.actor.as_deref(), Some("operator"));
}

#[tokio::test]
async fn append_claims_on_same_path_are_coleased() {
    let planner = BasicCpmPlanner::new();
    let plan_id = planner
        .submit_plan(PlanGraph {
            deliverables: vec![appending("a", "REGISTRY.md"), appending("b", "REGISTRY.md")],
            max_chained_dispatch: None,
        })
        .await
        .unwrap();
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(plan_id, caller("c1"), 2))
        .await
        .unwrap();
    assert_eq!(cohort.rows.len(), 2);
}

#[tokio::test]
async fn cohort_reports_shared_append_paths() {
    let planner = BasicCpmPlanner::new();
    let plan_id = planner
        .submit_plan(PlanGraph {
            deliverables: vec![appending("a", "REGISTRY.md"), appending("b", "REGISTRY.md")],
            max_chained_dispatch: None,
        })
        .await
        .unwrap();
    let cohort = planner
        .acquire_cohort(AcquireRequest::new(plan_id, caller("c1"), 2))
        .await
        .unwrap();
    assert_eq!(cohort.shared_paths, vec![PathBuf::from("REGISTRY.md")]);
}

#[tokio::test]
async fn unordered_exclusive_and_append_claims_are_rejected_at_submit() {
    let planner = BasicCpmPlanner::new();
    let exclusive = deliverable("c", &["REGISTRY.md"], &[], Some(1.0));
    let err = planner
        .submit_plan(PlanGraph {
            deliverables: vec![
                appending("a", "REGISTRY.md"),
                appending("b", "REGISTRY.md"),
                exclusive,
            ],
            max_chained_dispatch: None,
        })
        .await
        .unwrap_err();
    assert!(matches!(err, PlannerError::InvalidGraph { .. }), "{err:?}");
}

#[tokio::test]
async fn releasing_one_append_holder_keeps_the_other_claim() {
    let planner = BasicCpmPlanner::new();
    let plan_id = planner
        .submit_plan(PlanGraph {
            deliverables: vec![appending("a", "REGISTRY.md"), appending("b", "REGISTRY.md")],
            max_chained_dispatch: None,
        })
        .await
        .unwrap();
    planner
        .acquire_cohort(AcquireRequest::new(plan_id.clone(), caller("c1"), 2))
        .await
        .unwrap();
    planner
        .mark_status(MarkStatusRequest::new(
            plan_id.clone(),
            "a",
            caller("c1"),
            DeliverableStatus::Complete,
        ))
        .await
        .unwrap();
    let status = planner.status(&plan_id).await.unwrap();
    assert_eq!(status.locks_held.len(), 1);
}