terraphim_orchestrator 1.20.1

AI Dark Factory orchestrator wiring spawner, router, supervisor into a reconciliation loop
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
//! Pure-function module for reconciling PR gate state against branch protection requirements.
//!
//! This module has zero dependency on orchestrator runtime state and performs no I/O.
//! It reads a [`PrGateSnapshot`] capturing the current state of a PR head and produces
//! a deterministic [`PrGateDecision`] indicating what action the reconciler should take.
//!
//! See `.docs/design-adf-pr-merge-progress-2026-05-01.md` for the full design.

/// Terminal state of a single commit status context.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CommitStatusState {
    Pending,
    Success,
    Failure,
    Error,
}

impl CommitStatusState {
    /// Parse from the Gitea API string representation.
    pub fn from_api_str(s: &str) -> Self {
        match s {
            "success" => Self::Success,
            "failure" => Self::Failure,
            "error" => Self::Error,
            _ => Self::Pending,
        }
    }

    /// True when the context has reached a terminal (non-pending) state.
    pub fn is_terminal(&self) -> bool {
        !matches!(self, Self::Pending)
    }
}

/// One commit status entry posted against a SHA.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommitStatusSummary {
    pub context: String,
    pub state: CommitStatusState,
    /// Unix timestamp (seconds) when the status was created, if available.
    pub created_at_unix: Option<i64>,
}

/// Default stale pending timeout in seconds (60 minutes).
pub const STALE_PENDING_TIMEOUT_SECS: i64 = 3600;

/// Snapshot of everything the reconciler needs to classify a PR head.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrGateSnapshot {
    pub pr_number: u64,
    pub head_sha: String,
    pub base_branch: String,
    /// Context names required by branch protection (e.g. `["adf/build", "adf/pr-reviewer"]`).
    pub required_contexts: Vec<String>,
    /// Commit statuses actually posted on the head SHA.
    pub head_statuses: Vec<CommitStatusSummary>,
    /// Current time as Unix timestamp (seconds). Used for stale pending detection.
    pub now_unix: i64,
}

/// Deterministic classification of a PR head's gate state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PrGateDecision {
    /// All required contexts green; proceed to auto-merge policy evaluation.
    ReadyForPolicy,
    /// Required contexts not yet posted; enqueue the responsible agents.
    EnqueueMissingChecks { missing: Vec<String> },
    /// Required contexts posted but still pending; wait for next reconcile tick.
    AwaitingChecks { pending: Vec<String> },
    /// At least one required context failed; open remediation issue.
    BlockedByFailedChecks { failed: Vec<(String, String)> },
    /// Status API or branch protection API failure; service fault.
    FactoryFault { error: String },
}

/// Reconcile the PR gate state from a snapshot. Pure function.
///
/// Decision logic:
/// 1. If required_contexts is empty -> `ReadyForPolicy` (no gate configured).
/// 2. Find missing contexts (required but not posted) -> `EnqueueMissingChecks`.
/// 3. Find pending contexts (posted but not terminal) -> `AwaitingChecks`.
/// 4. Find failed contexts (terminal but not success) -> `BlockedByFailedChecks`.
/// 5. All required contexts are terminal success -> `ReadyForPolicy`.
pub fn reconcile_pr_gate(snapshot: &PrGateSnapshot) -> PrGateDecision {
    if snapshot.required_contexts.is_empty() {
        return PrGateDecision::ReadyForPolicy;
    }

    let missing = missing_required_contexts(&snapshot.required_contexts, &snapshot.head_statuses);
    if !missing.is_empty() {
        return PrGateDecision::EnqueueMissingChecks { missing };
    }

    let stale = stale_pending_contexts(
        &snapshot.required_contexts,
        &snapshot.head_statuses,
        snapshot.now_unix,
        STALE_PENDING_TIMEOUT_SECS,
    );
    if !stale.is_empty() {
        return PrGateDecision::FactoryFault {
            error: format!(
                "stale pending contexts (>{:>0}min): {}",
                STALE_PENDING_TIMEOUT_SECS / 60,
                stale.join(", ")
            ),
        };
    }

    let pending = pending_required_contexts(&snapshot.required_contexts, &snapshot.head_statuses);
    if !pending.is_empty() {
        return PrGateDecision::AwaitingChecks { pending };
    }

    let failed = failed_required_contexts(&snapshot.required_contexts, &snapshot.head_statuses);
    if !failed.is_empty() {
        return PrGateDecision::BlockedByFailedChecks { failed };
    }

    PrGateDecision::ReadyForPolicy
}

/// Compute which required contexts have no status posted at all on the head SHA.
pub fn missing_required_contexts(
    required: &[String],
    statuses: &[CommitStatusSummary],
) -> Vec<String> {
    let posted: std::collections::HashSet<&str> =
        statuses.iter().map(|s| s.context.as_str()).collect();
    required
        .iter()
        .filter(|ctx| !posted.contains(ctx.as_str()))
        .cloned()
        .collect()
}

/// Groups statuses by context, keeping the latest one for each context.
///
/// Prefers entries with a higher `created_at_unix`. If `created_at_unix` is None
/// for some entries, prefers the ones that have a timestamp. If multiple entries
/// have None for `created_at_unix`, keeps the last one in the slice.
fn latest_status_per_context<'a>(
    statuses: &'a [CommitStatusSummary],
) -> std::collections::HashMap<&'a str, &'a CommitStatusSummary> {
    let mut map: std::collections::HashMap<&'a str, &'a CommitStatusSummary> =
        std::collections::HashMap::new();
    for status in statuses {
        let should_replace = match map.get(status.context.as_str()) {
            None => true,
            Some(entry) => match (entry.created_at_unix, status.created_at_unix) {
                // Existing has timestamp, new doesn't -> keep existing
                (Some(_), None) => false,
                // Existing doesn't have timestamp, new does -> replace
                (None, Some(_)) => true,
                // Both have timestamps -> replace if new is higher
                (Some(existing), Some(new)) => new > existing,
                // Neither has timestamp -> keep last one in slice (replace)
                (None, None) => true,
            },
        };
        if should_replace {
            map.insert(status.context.as_str(), status);
        }
    }
    map
}

/// Compute which required contexts are posted but still pending.
pub fn pending_required_contexts(
    required: &[String],
    statuses: &[CommitStatusSummary],
) -> Vec<String> {
    let status_map = latest_status_per_context(statuses);
    required
        .iter()
        .filter(|ctx| {
            status_map
                .get(ctx.as_str())
                .is_some_and(|status| !status.state.is_terminal())
        })
        .cloned()
        .collect()
}

/// Compute which required contexts have been pending longer than the timeout.
/// Uses `created_at_unix` from the status entry and `now_unix` from the snapshot.
pub fn stale_pending_contexts(
    required: &[String],
    statuses: &[CommitStatusSummary],
    now_unix: i64,
    timeout_secs: i64,
) -> Vec<String> {
    let status_map = latest_status_per_context(statuses);
    required
        .iter()
        .filter(|ctx| {
            let Some(status) = status_map.get(ctx.as_str()) else {
                return false;
            };
            if status.state.is_terminal() {
                return false;
            }
            let Some(created_ts) = status.created_at_unix else {
                return false;
            };
            now_unix - created_ts > timeout_secs
        })
        .cloned()
        .collect()
}

/// Compute which required contexts have reached a failed/error terminal state.
/// Returns `(context_name, state_label)` pairs.
pub fn failed_required_contexts(
    required: &[String],
    statuses: &[CommitStatusSummary],
) -> Vec<(String, String)> {
    let status_map = latest_status_per_context(statuses);
    required
        .iter()
        .filter(|ctx| {
            status_map.get(ctx.as_str()).is_some_and(|status| {
                matches!(
                    status.state,
                    CommitStatusState::Failure | CommitStatusState::Error
                )
            })
        })
        .map(|ctx| {
            let status = status_map[ctx.as_str()];
            let label = match status.state {
                CommitStatusState::Failure => "failure",
                CommitStatusState::Error => "error",
                _ => unreachable!(),
            };
            (ctx.clone(), label.to_string())
        })
        .collect()
}

/// Deterministic dedup key for remediation issues.
///
/// Format: `"pr-gate:{pr_number}:{head_sha}:{context}"` for failed checks,
/// or `"pr-gate:{pr_number}:{head_sha}:factory-fault:{error}"` for factory faults.
pub fn remediation_key(
    project: &str,
    pr_number: u64,
    head_sha: &str,
    decision: &PrGateDecision,
) -> String {
    match decision {
        PrGateDecision::BlockedByFailedChecks { failed } => {
            let contexts: Vec<&str> = failed.iter().map(|(ctx, _)| ctx.as_str()).collect();
            format!(
                "pr-gate:{}:{}:{}:{}",
                project,
                pr_number,
                &head_sha[..head_sha.len().min(12)],
                contexts.join(",")
            )
        }
        PrGateDecision::FactoryFault { error } => {
            format!(
                "pr-gate:{}:{}:{}:factory-fault:{}",
                project,
                pr_number,
                &head_sha[..head_sha.len().min(12)],
                error.chars().take(40).collect::<String>()
            )
        }
        other => format!(
            "pr-gate:{}:{}:{}:{:?}",
            project,
            pr_number,
            &head_sha[..head_sha.len().min(12)],
            other
        ),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sha(s: &str) -> String {
        s.to_string()
    }

    fn required(contexts: &[&str]) -> Vec<String> {
        contexts.iter().map(|s| s.to_string()).collect()
    }

    fn statuses(items: &[(&str, CommitStatusState)]) -> Vec<CommitStatusSummary> {
        statuses_with_times(
            &items
                .iter()
                .map(|(ctx, state)| (*ctx, *state, None))
                .collect::<Vec<_>>(),
        )
    }

    fn statuses_with_times(
        items: &[(&str, CommitStatusState, Option<i64>)],
    ) -> Vec<CommitStatusSummary> {
        items
            .iter()
            .map(|(ctx, state, ts)| CommitStatusSummary {
                context: ctx.to_string(),
                state: *state,
                created_at_unix: *ts,
            })
            .collect()
    }

    fn snapshot_ctx(pr: u64, req: &[&str], sts: &[(&str, CommitStatusState)]) -> PrGateSnapshot {
        PrGateSnapshot {
            pr_number: pr,
            head_sha: sha("abc123def456"),
            base_branch: "main".into(),
            required_contexts: required(req),
            head_statuses: statuses(sts),
            now_unix: 10_000,
        }
    }

    fn snapshot_ctx_with_times(
        pr: u64,
        req: &[&str],
        sts: &[(&str, CommitStatusState, Option<i64>)],
        now_unix: i64,
    ) -> PrGateSnapshot {
        PrGateSnapshot {
            pr_number: pr,
            head_sha: sha("abc123def456"),
            base_branch: "main".into(),
            required_contexts: required(req),
            head_statuses: statuses_with_times(sts),
            now_unix,
        }
    }

    #[test]
    fn no_required_contexts_is_ready() {
        let snap = PrGateSnapshot {
            pr_number: 1,
            head_sha: sha("aaa"),
            base_branch: "main".into(),
            required_contexts: vec![],
            head_statuses: vec![],
            now_unix: 10_000,
        };
        assert_eq!(reconcile_pr_gate(&snap), PrGateDecision::ReadyForPolicy);
    }

    #[test]
    fn all_green_is_ready() {
        let snap = snapshot_ctx(
            42,
            &["adf/build", "adf/pr-reviewer"],
            &[
                ("adf/build", CommitStatusState::Success),
                ("adf/pr-reviewer", CommitStatusState::Success),
            ],
        );
        assert_eq!(reconcile_pr_gate(&snap), PrGateDecision::ReadyForPolicy);
    }

    #[test]
    fn missing_both_contexts_enqueues() {
        let snap = snapshot_ctx(1099, &["adf/build", "adf/pr-reviewer"], &[]);
        let decision = reconcile_pr_gate(&snap);
        assert_eq!(
            decision,
            PrGateDecision::EnqueueMissingChecks {
                missing: vec!["adf/build".into(), "adf/pr-reviewer".into()]
            }
        );
    }

    #[test]
    fn missing_one_context_enqueues_only_that() {
        let snap = snapshot_ctx(
            42,
            &["adf/build", "adf/pr-reviewer"],
            &[("adf/build", CommitStatusState::Success)],
        );
        let decision = reconcile_pr_gate(&snap);
        assert_eq!(
            decision,
            PrGateDecision::EnqueueMissingChecks {
                missing: vec!["adf/pr-reviewer".into()]
            }
        );
    }

    #[test]
    fn pending_contexts_waits() {
        let snap = snapshot_ctx(
            42,
            &["adf/build", "adf/pr-reviewer"],
            &[
                ("adf/build", CommitStatusState::Success),
                ("adf/pr-reviewer", CommitStatusState::Pending),
            ],
        );
        let decision = reconcile_pr_gate(&snap);
        assert_eq!(
            decision,
            PrGateDecision::AwaitingChecks {
                pending: vec!["adf/pr-reviewer".into()]
            }
        );
    }

    #[test]
    fn failed_context_blocks() {
        let snap = snapshot_ctx(
            42,
            &["adf/build", "adf/pr-reviewer"],
            &[
                ("adf/build", CommitStatusState::Failure),
                ("adf/pr-reviewer", CommitStatusState::Success),
            ],
        );
        let decision = reconcile_pr_gate(&snap);
        assert_eq!(
            decision,
            PrGateDecision::BlockedByFailedChecks {
                failed: vec![("adf/build".into(), "failure".into())]
            }
        );
    }

    #[test]
    fn error_context_blocks() {
        let snap = snapshot_ctx(
            42,
            &["adf/build"],
            &[("adf/build", CommitStatusState::Error)],
        );
        let decision = reconcile_pr_gate(&snap);
        assert_eq!(
            decision,
            PrGateDecision::BlockedByFailedChecks {
                failed: vec![("adf/build".into(), "error".into())]
            }
        );
    }

    #[test]
    fn all_pending_waits() {
        let snap = snapshot_ctx(
            42,
            &["adf/build", "adf/pr-reviewer"],
            &[
                ("adf/build", CommitStatusState::Pending),
                ("adf/pr-reviewer", CommitStatusState::Pending),
            ],
        );
        let decision = reconcile_pr_gate(&snap);
        assert_eq!(
            decision,
            PrGateDecision::AwaitingChecks {
                pending: vec!["adf/build".into(), "adf/pr-reviewer".into()]
            }
        );
    }

    #[test]
    fn extra_statuses_ignored() {
        let snap = snapshot_ctx(
            42,
            &["adf/build"],
            &[
                ("adf/build", CommitStatusState::Success),
                ("unrelated/context", CommitStatusState::Failure),
            ],
        );
        assert_eq!(reconcile_pr_gate(&snap), PrGateDecision::ReadyForPolicy);
    }

    #[test]
    fn pr_1099_fixture_both_missing() {
        let snap = snapshot_ctx(1099, &["adf/build", "adf/pr-reviewer"], &[]);
        let decision = reconcile_pr_gate(&snap);
        match &decision {
            PrGateDecision::EnqueueMissingChecks { missing } => {
                assert_eq!(missing.len(), 2);
                assert!(missing.contains(&"adf/build".to_string()));
                assert!(missing.contains(&"adf/pr-reviewer".to_string()));
            }
            other => panic!("expected EnqueueMissingChecks, got {:?}", other),
        }
    }

    #[test]
    fn commit_status_state_parsing() {
        assert_eq!(
            CommitStatusState::from_api_str("success"),
            CommitStatusState::Success
        );
        assert_eq!(
            CommitStatusState::from_api_str("failure"),
            CommitStatusState::Failure
        );
        assert_eq!(
            CommitStatusState::from_api_str("error"),
            CommitStatusState::Error
        );
        assert_eq!(
            CommitStatusState::from_api_str("pending"),
            CommitStatusState::Pending
        );
        assert_eq!(
            CommitStatusState::from_api_str("unknown"),
            CommitStatusState::Pending
        );
    }

    #[test]
    fn commit_status_terminal_check() {
        assert!(!CommitStatusState::Pending.is_terminal());
        assert!(CommitStatusState::Success.is_terminal());
        assert!(CommitStatusState::Failure.is_terminal());
        assert!(CommitStatusState::Error.is_terminal());
    }

    #[test]
    fn remediation_key_dedup_format() {
        let snap = snapshot_ctx(
            42,
            &["adf/build"],
            &[("adf/build", CommitStatusState::Failure)],
        );
        let decision = reconcile_pr_gate(&snap);
        let key = remediation_key("test-project", 42, "abc123def456", &decision);
        assert!(key.starts_with("pr-gate:test-project:42:abc123def456:"));
        assert!(key.contains("adf/build"));
    }

    #[test]
    fn remediation_key_factory_fault() {
        let decision = PrGateDecision::FactoryFault {
            error: "connection refused".into(),
        };
        let key = remediation_key("test-project", 42, "abc123def456", &decision);
        assert!(key.contains("factory-fault"));
        assert!(key.contains("connection refused"));
    }

    #[test]
    fn remediation_keys_different_for_different_shas() {
        let d1 = PrGateDecision::BlockedByFailedChecks {
            failed: vec![("adf/build".into(), "failure".into())],
        };
        let key1 = remediation_key("p", 42, "aaa111", &d1);
        let key2 = remediation_key("p", 42, "bbb222", &d1);
        assert_ne!(key1, key2);
    }

    #[test]
    fn stale_pending_triggers_factory_fault() {
        let snap = snapshot_ctx_with_times(
            42,
            &["adf/build", "adf/pr-reviewer"],
            &[
                ("adf/build", CommitStatusState::Pending, Some(5_000)),
                ("adf/pr-reviewer", CommitStatusState::Success, Some(5_000)),
            ],
            10_000,
        );
        let decision = reconcile_pr_gate(&snap);
        match &decision {
            PrGateDecision::FactoryFault { error } => {
                assert!(error.contains("adf/build"));
                assert!(error.contains("stale pending"));
            }
            other => panic!("expected FactoryFault, got {:?}", other),
        }
    }

    #[test]
    fn recent_pending_does_not_trigger_stale() {
        let snap = snapshot_ctx_with_times(
            42,
            &["adf/build", "adf/pr-reviewer"],
            &[
                ("adf/build", CommitStatusState::Pending, Some(9_500)),
                ("adf/pr-reviewer", CommitStatusState::Success, Some(5_000)),
            ],
            10_000,
        );
        let decision = reconcile_pr_gate(&snap);
        assert_eq!(
            decision,
            PrGateDecision::AwaitingChecks {
                pending: vec!["adf/build".into()]
            }
        );
    }

    #[test]
    fn stale_without_created_at_does_not_trigger() {
        let snap = snapshot_ctx_with_times(
            42,
            &["adf/build"],
            &[("adf/build", CommitStatusState::Pending, None)],
            10_000,
        );
        let decision = reconcile_pr_gate(&snap);
        assert_eq!(
            decision,
            PrGateDecision::AwaitingChecks {
                pending: vec!["adf/build".into()]
            }
        );
    }

    // ------------------------------------------------------------------
    // latest_status_per_context helper tests
    // ------------------------------------------------------------------

    #[test]
    fn latest_status_per_context_prefers_higher_timestamp() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Failure, Some(1_000)),
            ("adf/build", CommitStatusState::Success, Some(2_000)),
        ]);
        let map = latest_status_per_context(&statuses);
        assert_eq!(map.len(), 1);
        assert_eq!(map["adf/build"].state, CommitStatusState::Success);
        assert_eq!(map["adf/build"].created_at_unix, Some(2_000));
    }

    #[test]
    fn latest_status_per_context_prefers_timestamp_over_none() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Success, None),
            ("adf/build", CommitStatusState::Failure, Some(1_000)),
        ]);
        let map = latest_status_per_context(&statuses);
        assert_eq!(map.len(), 1);
        assert_eq!(map["adf/build"].state, CommitStatusState::Failure);
        assert_eq!(map["adf/build"].created_at_unix, Some(1_000));
    }

    #[test]
    fn latest_status_per_context_keeps_existing_timestamp_over_none() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Failure, Some(1_000)),
            ("adf/build", CommitStatusState::Success, None),
        ]);
        let map = latest_status_per_context(&statuses);
        assert_eq!(map.len(), 1);
        assert_eq!(map["adf/build"].state, CommitStatusState::Failure);
        assert_eq!(map["adf/build"].created_at_unix, Some(1_000));
    }

    #[test]
    fn latest_status_per_context_last_wins_when_both_no_timestamp() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Failure, None),
            ("adf/build", CommitStatusState::Success, None),
        ]);
        let map = latest_status_per_context(&statuses);
        assert_eq!(map.len(), 1);
        assert_eq!(map["adf/build"].state, CommitStatusState::Success);
        assert_eq!(map["adf/build"].created_at_unix, None);
    }

    #[test]
    fn latest_status_per_context_different_contexts_independent() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Failure, Some(1_000)),
            ("adf/build", CommitStatusState::Success, Some(2_000)),
            ("adf/pr-reviewer", CommitStatusState::Pending, Some(500)),
            ("adf/pr-reviewer", CommitStatusState::Success, Some(1_500)),
        ]);
        let map = latest_status_per_context(&statuses);
        assert_eq!(map.len(), 2);
        assert_eq!(map["adf/build"].state, CommitStatusState::Success);
        assert_eq!(map["adf/pr-reviewer"].state, CommitStatusState::Success);
    }

    // ------------------------------------------------------------------
    // Integration: duplicate contexts in gate functions
    // ------------------------------------------------------------------

    #[test]
    fn pending_uses_latest_status_for_duplicate_contexts() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Pending, Some(1_000)),
            ("adf/build", CommitStatusState::Success, Some(2_000)),
        ]);
        let pending = pending_required_contexts(&required(&["adf/build"]), &statuses);
        assert!(pending.is_empty());
    }

    #[test]
    fn pending_uses_latest_status_when_still_pending() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Success, Some(1_000)),
            ("adf/build", CommitStatusState::Pending, Some(2_000)),
        ]);
        let pending = pending_required_contexts(&required(&["adf/build"]), &statuses);
        assert_eq!(pending, vec!["adf/build"]);
    }

    #[test]
    fn stale_uses_latest_status_for_duplicate_contexts() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Pending, Some(1_000)),
            ("adf/build", CommitStatusState::Pending, Some(9_500)),
        ]);
        let stale = stale_pending_contexts(
            &required(&["adf/build"]),
            &statuses,
            10_000,
            STALE_PENDING_TIMEOUT_SECS,
        );
        assert!(stale.is_empty());
    }

    #[test]
    fn stale_uses_latest_status_when_still_stale() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Pending, Some(9_500)),
            ("adf/build", CommitStatusState::Pending, Some(1_000)),
        ]);
        let stale = stale_pending_contexts(
            &required(&["adf/build"]),
            &statuses,
            10_000,
            STALE_PENDING_TIMEOUT_SECS,
        );
        assert!(stale.is_empty());
    }

    #[test]
    fn failed_uses_latest_status_for_duplicate_contexts() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Failure, Some(1_000)),
            ("adf/build", CommitStatusState::Success, Some(2_000)),
        ]);
        let failed = failed_required_contexts(&required(&["adf/build"]), &statuses);
        assert!(failed.is_empty());
    }

    #[test]
    fn failed_uses_latest_status_when_still_failed() {
        let statuses = statuses_with_times(&[
            ("adf/build", CommitStatusState::Success, Some(1_000)),
            ("adf/build", CommitStatusState::Failure, Some(2_000)),
        ]);
        let failed = failed_required_contexts(&required(&["adf/build"]), &statuses);
        assert_eq!(failed, vec![("adf/build".into(), "failure".into())]);
    }

    #[test]
    fn reconcile_uses_latest_status_across_duplicate_contexts() {
        let snap = snapshot_ctx_with_times(
            42,
            &["adf/build", "adf/pr-reviewer"],
            &[
                ("adf/build", CommitStatusState::Failure, Some(1_000)),
                ("adf/build", CommitStatusState::Success, Some(2_000)),
                ("adf/pr-reviewer", CommitStatusState::Pending, Some(1_000)),
                ("adf/pr-reviewer", CommitStatusState::Success, Some(2_000)),
            ],
            10_000,
        );
        assert_eq!(reconcile_pr_gate(&snap), PrGateDecision::ReadyForPolicy);
    }
}