polyc-runtime 2026.8.0

Shared Unix-coherence runtime for polychrome binaries: logging, health/metrics side-server, signals.
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
//! Warm drain-swap-resume — the click-to-restart path for a wire- and
//! schema-compatible binary swap (see the update PRD, [`crate::compat`], and
//! [`crate::stager`]).
//!
//! This is the "click to restart" layer. A [`Compatibility::Warm`] release is a
//! new binary whose fingerprint is otherwise identical to the running one — only
//! binary internals moved — so it cannot be picked up at a turn boundary the way
//! a hot config bundle is ([`crate::hot_reload`]); the process itself has to be
//! replaced. But a live conversation must not be yanked mid-turn. This module
//! orchestrates the smallest safe restart: stop admitting new turns, let
//! in-flight turns and paused approvals reach a committed checkpoint, release the
//! single-writer lease, swap the process or image, re-acquire the lease, and
//! resume from the last committed turn.
//!
//! # It orchestrates; it does not reinvent
//!
//! Every invariant this path relies on is already built and tested elsewhere —
//! the job here is to sequence those primitives, not to duplicate them:
//!
//! - **The commit marker is the drain point.** A turn is persisted to the event
//!   log — bracketed by its `turn_start`/`turn_complete` pair — before the
//!   harness is dialed, and a turn missing its `turn_complete` is discarded on
//!   replay (the control plane's `filter_committed_turns`). So "drain" is not a
//!   new barrier: it is waiting for the in-flight turn to reach its existing
//!   commit marker, which doubles as the rollback net.
//! - **The lease is the existing single-writer guard.** The per-conversation
//!   `coordination.k8s.io/v1` Lease already enforces exactly one writer; a warm
//!   swap releases it before the swap and re-acquires it after, so the swapped-in
//!   process is the sole writer with no split-brain window.
//! - **Resume is the existing replay path.** Restarting from the last committed
//!   turn — re-driving any suspended HITL approval — is the same reconstruct the
//!   control plane runs on every reconnect.
//! - **The swap core reuses the stager wholesale.** Verifying the new binary's
//!   signature, applying the pointer flip, health-checking it, and auto-rolling
//!   back on failure are [`crate::stager`]'s job; the only bespoke piece is the
//!   [`Activator`], which for a warm swap is a process or image swap.
//!
//! Because those primitives live in the control plane (a container) while this
//! crate is a foundation component, the orchestrator is expressed over injectable
//! seams — the same shape [`crate::stager`] and [`crate::hot_reload`] use. The
//! control plane wires each seam to its real drain / lease / resume code; the
//! state machine and its ordering invariant are exercised here without a cluster.
//!
//! # The classifier gate
//!
//! Only a release that classifies [`Compatibility::Warm`] takes this path.
//! [`ensure_warm`] refuses every other verdict: a [`Compatibility::Hot`] change
//! reloads at the turn boundary with no restart, a [`Compatibility::Cold`] change
//! needs a coordinated fleet redeploy, and an
//! [`Incompatible`](crate::compat::Compatibility::Incompatible) bundle was
//! authored against a runtime this build cannot satisfy. The gate runs before any
//! seam is touched, so a non-warm release never closes admission, drains, or
//! releases the lease.
//!
//! # The ordering invariant
//!
//! [`WarmSwap::run`] walks a fixed sequence, and the ordering is the whole point:
//!
//! ```text
//!   ensure_warm ─▶ close admission ─▶ drain to committed checkpoint
//!//!//!            release lease ─▶ swap (stager: verify→apply→health→rollback)
//!//!//!            re-acquire lease ─▶ resume from the last committed turn
//! ```
//!
//! - No new turn is admitted once `close` runs, which is *before* the drain
//!   begins.
//! - In-flight turns and paused approvals reach a committed checkpoint *before*
//!   the swap — the checkpoint is captured while the lease is still held.
//! - The lease is released *before* it is re-acquired, so there is never a moment
//!   with two writers.
//! - Resume restarts from exactly the drained checkpoint, so no committed turn is
//!   dropped and none is replayed twice; a paused approval captured in the
//!   checkpoint is re-driven.
//!
//! The lease is re-acquired and the conversation is resumed on *whichever* binary
//! is live after the swap — the new one on a committed swap, the previous one if
//! the health check rolled back — so a conversation is never left drained-but-dead
//! behind a failed swap.

use thiserror::Error;

use crate::compat::Compatibility;
use crate::stager::{
    Activator, HealthCheck, Outcome, ReleaseId, SignatureVerifier, StageError, Stager, UpdateSource,
};

/// The drained resume point: the last fully-committed turn plus the paused
/// approvals that must survive the swap.
///
/// Captured by [`Drain::drain`] while the lease is still held, and threaded into
/// [`Resume::resume`] so the swapped-in process restarts from exactly here. Turns
/// that never reached their `turn_complete` marker are not part of it — they are
/// the interrupted turns replay discards — so resuming from this point neither
/// drops a committed turn nor replays an uncommitted one.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Checkpoint {
    /// The id of the last fully-committed turn — the point resume restarts from.
    last_committed_turn: String,
    /// How many HITL approvals were paused at the checkpoint. They are persisted
    /// with the turn, so they survive the swap and are re-driven on resume.
    paused_approvals: usize,
}

impl Checkpoint {
    /// Build a checkpoint from the last committed turn id and the count of paused
    /// approvals carried across the swap.
    #[must_use]
    pub fn new(last_committed_turn: impl Into<String>, paused_approvals: usize) -> Self {
        Self {
            last_committed_turn: last_committed_turn.into(),
            paused_approvals,
        }
    }

    /// The id of the last fully-committed turn — the resume restart point.
    #[must_use]
    pub fn last_committed_turn(&self) -> &str {
        &self.last_committed_turn
    }

    /// How many paused HITL approvals were captured at the checkpoint and must be
    /// re-driven after the swap.
    #[must_use]
    pub const fn paused_approvals(&self) -> usize {
        self.paused_approvals
    }
}

/// What resume did after the swap: where it restarted and how many paused
/// approvals it re-drove.
///
/// [`WarmSwap::run`] checks this against the drained [`Checkpoint`] so a seam that
/// restarts from the wrong turn — dropping or duplicating a committed turn — is a
/// hard error rather than a silent divergence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResumeReport {
    /// The turn id resume restarted from. Must equal the checkpoint's
    /// [`Checkpoint::last_committed_turn`].
    resumed_from: String,
    /// How many paused approvals were re-driven on resume.
    redriven_approvals: usize,
}

impl ResumeReport {
    /// Build a resume report from the turn resumed and the approvals re-driven.
    #[must_use]
    pub fn new(resumed_from: impl Into<String>, redriven_approvals: usize) -> Self {
        Self {
            resumed_from: resumed_from.into(),
            redriven_approvals,
        }
    }

    /// The turn id resume restarted from.
    #[must_use]
    pub fn resumed_from(&self) -> &str {
        &self.resumed_from
    }

    /// How many paused approvals resume re-drove.
    #[must_use]
    pub const fn redriven_approvals(&self) -> usize {
        self.redriven_approvals
    }
}

/// The result of a warm swap that got as far as resuming.
///
/// Carries the swap [`Outcome`] (committed onto the new binary, or rolled back to
/// the previous one), the drained [`Checkpoint`], and the [`ResumeReport`]. The
/// swap outcome is honest about a rollback, so the surface presenting the restart
/// can say which binary is actually serving.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WarmSwapReport {
    /// What the swap did — [`Outcome::Committed`] onto the new binary or
    /// [`Outcome::RolledBack`] to the previous one after a failed health check.
    pub swap: Outcome,
    /// The point the conversation drained to and resumed from.
    pub checkpoint: Checkpoint,
    /// Where resume restarted and how many approvals it re-drove.
    pub resume: ResumeReport,
}

/// Why a warm swap was refused or could not complete.
#[derive(Debug, Error)]
pub enum WarmSwapError {
    /// The release did not classify [`Compatibility::Warm`], so it does not take
    /// the click-to-restart path. A hot change reloads with no restart, a cold
    /// change needs a coordinated redeploy, and an incompatible bundle was built
    /// for a runtime this binary cannot satisfy.
    #[error("refused: only a warm binary swap takes the click-to-restart path ({})", verdict_label(.0))]
    NotWarm(Compatibility),
    /// Admission could not be closed, so new turns cannot be reliably stopped and
    /// the swap is abandoned before the lease is touched.
    #[error("could not stop admitting new turns: {0}")]
    Admission(String),
    /// The drain could not reach a committed checkpoint, so the swap is abandoned
    /// with the lease still held — nothing was released or swapped.
    #[error("draining to a committed checkpoint failed: {0}")]
    Drain(String),
    /// A lease operation failed — either releasing the lease before the swap or
    /// re-acquiring it after.
    #[error("lease {op} failed: {reason}")]
    Lease {
        /// Which lease operation failed: `"release"` or `"re-acquire"`.
        op: &'static str,
        /// The underlying failure.
        reason: String,
    },
    /// The stager could not verify or apply the binary swap. Wraps the underlying
    /// [`StageError`] — most importantly [`StageError::Unverified`], which means
    /// the new binary's signature did not verify and nothing was swapped.
    #[error(transparent)]
    Stage(#[from] StageError),
    /// Resume could not restart the conversation from the checkpoint after the
    /// swap. The lease has already been re-acquired, so this is a resume failure
    /// on a live binary, not a lost lease.
    #[error("resuming from the last committed turn failed: {0}")]
    Resume(String),
    /// Resume restarted from a different turn than the one drained — a dropped or
    /// duplicated turn. Named separately from [`Self::Resume`] because it is a
    /// correctness violation, not a transport failure.
    #[error(
        "resume restarted from turn {resumed_from:?} but drained to {expected:?} — \
         a committed turn would be dropped or replayed"
    )]
    ResumeMismatch {
        /// The turn resume actually restarted from.
        resumed_from: String,
        /// The checkpoint turn it should have restarted from.
        expected: String,
    },
}

/// A short, plain-language name for a refused verdict, for [`WarmSwapError`].
const fn verdict_label(verdict: &Compatibility) -> &'static str {
    match verdict {
        Compatibility::Warm => "warm",
        Compatibility::Hot => "a config change that reloads with no restart",
        Compatibility::Cold => "a format change that needs a coordinated redeploy",
        Compatibility::Incompatible(_) => "built for a different runtime",
    }
}

/// Gate a classification verdict onto the warm path: [`Ok`] only for
/// [`Compatibility::Warm`].
///
/// This is the classifier interlock in one place — the swap proceeds only when
/// the change is a wire- and schema-compatible binary swap. Any other verdict
/// (`Hot`, `Cold`, or `Incompatible`) is refused before any seam is touched.
///
/// # Errors
///
/// Returns [`WarmSwapError::NotWarm`] carrying the refused verdict for anything
/// other than [`Compatibility::Warm`].
pub fn ensure_warm(verdict: &Compatibility) -> Result<(), WarmSwapError> {
    if *verdict == Compatibility::Warm {
        Ok(())
    } else {
        Err(WarmSwapError::NotWarm(verdict.clone()))
    }
}

/// Stops admitting new turns — the readiness flip / turn-admission gate.
///
/// Called once, before the drain, so no turn is admitted while the conversation
/// drains. In production this flips the readiness signal the edge and the
/// admission check read, so the load balancer stops routing new turns here.
pub trait AdmissionGate {
    /// Stop admitting new turns.
    ///
    /// # Errors
    ///
    /// Returns [`WarmSwapError::Admission`] when admission cannot be closed.
    fn close(&self) -> Result<(), WarmSwapError>;
}

impl<F> AdmissionGate for F
where
    F: Fn() -> Result<(), WarmSwapError>,
{
    fn close(&self) -> Result<(), WarmSwapError> {
        self()
    }
}

/// Drains in-flight turns and paused approvals to a committed checkpoint.
///
/// Runs while the lease is still held. It waits for the in-flight turn to reach
/// its `turn_complete` marker — the commit point every turn is already bracketed
/// by — and captures the last committed turn plus any paused approvals as a
/// [`Checkpoint`]. It never forces a partial turn to commit: an interrupted turn
/// is simply excluded, exactly as replay would discard it.
pub trait Drain {
    /// Drain to a committed checkpoint and return it.
    ///
    /// # Errors
    ///
    /// Returns [`WarmSwapError::Drain`] when the conversation cannot reach a
    /// committed checkpoint (for example a journal sync failed).
    fn drain(&self) -> Result<Checkpoint, WarmSwapError>;
}

impl<F> Drain for F
where
    F: Fn() -> Result<Checkpoint, WarmSwapError>,
{
    fn drain(&self) -> Result<Checkpoint, WarmSwapError> {
        self()
    }
}

/// Releases and re-acquires the single-writer per-conversation lease.
///
/// A warm swap releases the lease after draining and re-acquires it after the
/// swap, so the swapped-in process is the sole writer with no split-brain window.
/// In production this wraps the `coordination.k8s.io/v1` Lease: `release` is the
/// lease handle's drop (clears `holderIdentity`) and `reacquire` is a fresh
/// `try_acquire`.
pub trait LeaseControl {
    /// Release the lease held for this conversation.
    ///
    /// # Errors
    ///
    /// Returns [`WarmSwapError::Lease`] (with `op = "release"`) when the lease
    /// cannot be released.
    fn release(&self) -> Result<(), WarmSwapError>;

    /// Re-acquire the lease for this conversation.
    ///
    /// # Errors
    ///
    /// Returns [`WarmSwapError::Lease`] (with `op = "re-acquire"`) when the lease
    /// cannot be re-acquired.
    fn reacquire(&self) -> Result<(), WarmSwapError>;
}

/// Resumes the conversation from the last committed turn after the swap.
///
/// Given the drained [`Checkpoint`], it replays from the last committed turn and
/// re-drives any paused HITL approval — the same reconstruct the control plane
/// runs on a reconnect. It restarts from exactly the checkpoint turn, so no
/// committed turn is dropped and none is replayed twice.
pub trait Resume {
    /// Resume from `checkpoint` and report where it restarted.
    ///
    /// # Errors
    ///
    /// Returns [`WarmSwapError::Resume`] when the conversation cannot be resumed.
    fn resume(&self, checkpoint: &Checkpoint) -> Result<ResumeReport, WarmSwapError>;
}

impl<F> Resume for F
where
    F: Fn(&Checkpoint) -> Result<ResumeReport, WarmSwapError>,
{
    fn resume(&self, checkpoint: &Checkpoint) -> Result<ResumeReport, WarmSwapError> {
        self(checkpoint)
    }
}

/// The warm drain-swap-resume orchestrator over its four control seams.
///
/// Holds the admission gate, the drain, the lease control, and the resume seam.
/// [`WarmSwap::run`] drives the fixed sequence, delegating the swap core (verify →
/// apply → health check → auto-rollback) to a [`Stager`] whose [`Activator`] is a
/// process or image swap. The orchestrator adds only the ordering: close before
/// drain, drain before release, release before swap, swap before re-acquire,
/// re-acquire before resume.
pub struct WarmSwap<G, D, L, R> {
    gate: G,
    drain: D,
    lease: L,
    resume: R,
}

impl<G, D, L, R> WarmSwap<G, D, L, R>
where
    G: AdmissionGate,
    D: Drain,
    L: LeaseControl,
    R: Resume,
{
    /// Build a warm-swap orchestrator over its four control seams.
    pub const fn new(gate: G, drain: D, lease: L, resume: R) -> Self {
        Self {
            gate,
            drain,
            lease,
            resume,
        }
    }

    /// Run the warm drain-swap-resume for a `Warm`-classified `release`.
    ///
    /// The sequence is fixed and its ordering is the invariant:
    ///
    /// 1. gate on the classifier — refuse anything but [`Compatibility::Warm`]
    ///    before touching any seam;
    /// 2. close admission, so no new turn is admitted while draining;
    /// 3. drain to a committed checkpoint — in-flight turns and paused approvals
    ///    reach their `turn_complete` marker, captured as a [`Checkpoint`], while
    ///    the lease is still held;
    /// 4. release the lease;
    /// 5. swap via the [`Stager`] — verify the new binary's signature, apply the
    ///    pointer flip, health-check, and auto-roll-back on failure;
    /// 6. re-acquire the lease — always, on whichever binary is now live, so the
    ///    conversation is never left drained-but-dead;
    /// 7. resume from the checkpoint, re-driving paused approvals, and verify it
    ///    restarted from exactly the drained turn.
    ///
    /// The lease re-acquire in step 6 runs even when the swap rolled back or its
    /// signature was refused, so a failed swap surfaces its error only after the
    /// conversation is back under a held lease.
    ///
    /// # Errors
    ///
    /// Returns [`WarmSwapError::NotWarm`] when `verdict` is not warm (nothing is
    /// drained or swapped); [`WarmSwapError::Admission`] or
    /// [`WarmSwapError::Drain`] when the pre-swap steps fail (the lease is still
    /// held); [`WarmSwapError::Lease`] when a release or re-acquire fails;
    /// [`WarmSwapError::Stage`] when the stager cannot verify or apply the swap
    /// (surfaced after the lease is re-acquired); and [`WarmSwapError::Resume`] or
    /// [`WarmSwapError::ResumeMismatch`] when resume fails or restarts from the
    /// wrong turn.
    pub fn run<S, V, A, H>(
        &self,
        verdict: &Compatibility,
        stager: &mut Stager<S, V, A, H>,
        release: &ReleaseId,
    ) -> Result<WarmSwapReport, WarmSwapError>
    where
        S: UpdateSource,
        V: SignatureVerifier,
        A: Activator,
        H: HealthCheck,
    {
        // 1. Classifier gate — no seam is touched for a non-warm release.
        ensure_warm(verdict)?;

        // 2. Stop admitting new turns BEFORE the drain begins.
        self.gate.close()?;

        // 3. Drain to a committed checkpoint while the lease is still held. This
        //    is the existing commit marker doubling as the drain point and the
        //    rollback net — an interrupted turn is excluded, never forced.
        let checkpoint = self.drain.drain()?;

        // 4. Release the lease before the swap, so the re-acquire in step 6 never
        //    races a still-held writer.
        self.lease
            .release()
            .map_err(|e| relabel_lease(e, "release"))?;

        // 5. Swap: reuse the stager wholesale for verify → apply → health →
        //    auto-rollback. Capture the result rather than `?`-returning it, so
        //    the lease is re-acquired even when the swap is refused or rolls back.
        let swap = stager.stage_and_apply(release);

        // 6. Re-acquire the lease on whichever binary is now live, ALWAYS — a
        //    failed swap must not leave the conversation drained-but-dead.
        self.lease
            .reacquire()
            .map_err(|e| relabel_lease(e, "re-acquire"))?;

        // A swap that could not even apply (unverified binary, failed flip) is a
        // hard error — surfaced now that the lease is back and the previous binary
        // is live for the caller's retry. We never paper the swap error over.
        let swap = swap?;

        // 7. Resume from exactly the drained checkpoint, re-driving paused
        //    approvals, and verify no committed turn was dropped or duplicated.
        let resume = self.resume.resume(&checkpoint)?;
        if resume.resumed_from() != checkpoint.last_committed_turn() {
            return Err(WarmSwapError::ResumeMismatch {
                resumed_from: resume.resumed_from().to_owned(),
                expected: checkpoint.last_committed_turn().to_owned(),
            });
        }

        Ok(WarmSwapReport {
            swap,
            checkpoint,
            resume,
        })
    }
}

/// Stamp the failing lease operation onto a [`WarmSwapError::Lease`] so the error
/// names whether the release or the re-acquire failed, regardless of how the seam
/// built it.
fn relabel_lease(err: WarmSwapError, op: &'static str) -> WarmSwapError {
    match err {
        WarmSwapError::Lease { reason, .. } => WarmSwapError::Lease { op, reason },
        other => other,
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::pedantic, clippy::nursery, missing_docs)]

    use std::cell::RefCell;
    use std::path::PathBuf;
    use std::rc::Rc;

    use super::*;
    use crate::compat::{Incompatibility, RuntimeTarget, StagedBundle};
    use crate::stager::{Health, StagedArtifact};

    /// A shared ordered log of every step across all seams, so a test can assert
    /// the exact sequence a warm swap walked — the deterministic-runtime style of
    /// the interrupted-turn / resume tests.
    type Trace = Rc<RefCell<Vec<String>>>;

    fn trace() -> Trace {
        Rc::new(RefCell::new(Vec::new()))
    }

    fn log(trace: &Trace, step: impl Into<String>) {
        trace.borrow_mut().push(step.into());
    }

    /// An admission gate that records the close and exposes whether a turn would
    /// still be admitted — so a test can prove no turn is admitted after close.
    #[derive(Clone)]
    struct RecordingGate {
        trace: Trace,
        open: Rc<RefCell<bool>>,
    }

    impl RecordingGate {
        fn new(trace: &Trace) -> Self {
            Self {
                trace: Rc::clone(trace),
                open: Rc::new(RefCell::new(true)),
            }
        }

        /// Whether a new turn would be admitted right now.
        fn admits(&self) -> bool {
            *self.open.borrow()
        }
    }

    impl AdmissionGate for RecordingGate {
        fn close(&self) -> Result<(), WarmSwapError> {
            *self.open.borrow_mut() = false;
            log(&self.trace, "close");
            Ok(())
        }
    }

    /// A drain over a tiny model event log: a list of `(turn_id, committed)`. It
    /// records the drain, asserts admission is already closed, and returns the
    /// last COMMITTED turn — an interrupted turn is excluded exactly as
    /// `filter_committed_turns` would discard it.
    #[derive(Clone)]
    struct RecordingDrain {
        trace: Trace,
        gate: RecordingGate,
        log: Vec<(&'static str, bool)>,
        paused_approvals: usize,
    }

    impl RecordingDrain {
        fn new(
            trace: &Trace,
            gate: &RecordingGate,
            model_log: Vec<(&'static str, bool)>,
            paused_approvals: usize,
        ) -> Self {
            Self {
                trace: Rc::clone(trace),
                gate: gate.clone(),
                log: model_log,
                paused_approvals,
            }
        }
    }

    impl Drain for RecordingDrain {
        fn drain(&self) -> Result<Checkpoint, WarmSwapError> {
            // Admission MUST already be closed before the drain begins.
            assert!(
                !self.gate.admits(),
                "drain began while new turns were still admitted"
            );
            log(&self.trace, "drain");
            // The resume point is the last fully-committed turn; an uncommitted
            // (interrupted) turn is never the checkpoint.
            let last_committed = self
                .log
                .iter()
                .rev()
                .find_map(|(id, committed)| committed.then_some(*id))
                .expect("model log has at least one committed turn");
            Ok(Checkpoint::new(last_committed, self.paused_approvals))
        }
    }

    /// Lease control that records release / re-acquire and tracks held state, so a
    /// test can prove the lease was released before it was re-acquired and never
    /// double-held.
    #[derive(Clone)]
    struct RecordingLease {
        trace: Trace,
        held: Rc<RefCell<bool>>,
        fail_release: bool,
        fail_reacquire: bool,
    }

    impl RecordingLease {
        fn new(trace: &Trace) -> Self {
            Self {
                trace: Rc::clone(trace),
                held: Rc::new(RefCell::new(true)),
                fail_release: false,
                fail_reacquire: false,
            }
        }

        fn failing_reacquire(trace: &Trace) -> Self {
            Self {
                fail_reacquire: true,
                ..Self::new(trace)
            }
        }

        fn is_held(&self) -> bool {
            *self.held.borrow()
        }
    }

    impl LeaseControl for RecordingLease {
        fn release(&self) -> Result<(), WarmSwapError> {
            if self.fail_release {
                return Err(WarmSwapError::Lease {
                    op: "release",
                    reason: "api timeout".to_owned(),
                });
            }
            assert!(self.is_held(), "released a lease that was not held");
            *self.held.borrow_mut() = false;
            log(&self.trace, "lease.release");
            Ok(())
        }

        fn reacquire(&self) -> Result<(), WarmSwapError> {
            if self.fail_reacquire {
                return Err(WarmSwapError::Lease {
                    op: "re-acquire",
                    reason: "held by another".to_owned(),
                });
            }
            assert!(
                !self.is_held(),
                "re-acquired a lease that was never released — split-brain writer"
            );
            *self.held.borrow_mut() = true;
            log(&self.trace, "lease.reacquire");
            Ok(())
        }
    }

    /// A resume seam that records the restart, re-drives the paused approvals from
    /// the checkpoint, and (unless told to drift) restarts from exactly the
    /// checkpoint turn.
    #[derive(Clone)]
    struct RecordingResume {
        trace: Trace,
        gate: RecordingGate,
        lease: RecordingLease,
        drift_to: Option<&'static str>,
    }

    impl RecordingResume {
        fn new(trace: &Trace, gate: &RecordingGate, lease: &RecordingLease) -> Self {
            Self {
                trace: Rc::clone(trace),
                gate: gate.clone(),
                lease: lease.clone(),
                drift_to: None,
            }
        }

        fn drifting(
            trace: &Trace,
            gate: &RecordingGate,
            lease: &RecordingLease,
            to: &'static str,
        ) -> Self {
            Self {
                drift_to: Some(to),
                ..Self::new(trace, gate, lease)
            }
        }
    }

    impl Resume for RecordingResume {
        fn resume(&self, checkpoint: &Checkpoint) -> Result<ResumeReport, WarmSwapError> {
            // Resume runs only under a re-held lease, on a live binary.
            assert!(self.lease.is_held(), "resumed without a re-acquired lease");
            log(
                &self.trace,
                format!("resume@{}", checkpoint.last_committed_turn()),
            );
            // The swapped-in process comes back ready to admit turns again.
            *self.gate.open.borrow_mut() = true;
            let from = self.drift_to.unwrap_or(checkpoint.last_committed_turn());
            Ok(ResumeReport::new(from, checkpoint.paused_approvals()))
        }
    }

    /// A recording swap activator (the process / image swap), sharing its flip log
    /// with the trace.
    #[derive(Clone)]
    struct SwapActivator {
        trace: Trace,
    }

    impl SwapActivator {
        fn new(trace: &Trace) -> Self {
            Self {
                trace: Rc::clone(trace),
            }
        }
    }

    impl Activator for SwapActivator {
        fn activate(&self, release: &ReleaseId) -> Result<(), StageError> {
            log(&self.trace, format!("swap:{release}"));
            Ok(())
        }
    }

    fn artifact_for(release: &ReleaseId) -> StagedArtifact {
        StagedArtifact {
            release: release.clone(),
            staged_path: PathBuf::from(format!("/var/lib/polychrome/staged/{release}")),
            bundle: StagedBundle::new(
                RuntimeTarget::new(3, 7, "polychrome.uno/v1"),
                format!("catalog-{release}"),
            ),
            signed_bytes: format!("bytes-of-{release}").into_bytes(),
            signature: vec![0xAB; 4],
            signer_public_key: vec![0xCD; 4],
        }
    }

    fn ok_source(release: &ReleaseId) -> Result<StagedArtifact, StageError> {
        Ok(artifact_for(release))
    }

    // --- The classifier gate ---------------------------------------------------

    #[test]
    fn ensure_warm_admits_only_warm() {
        assert!(ensure_warm(&Compatibility::Warm).is_ok());
        for verdict in [
            Compatibility::Hot,
            Compatibility::Cold,
            Compatibility::Incompatible(Incompatibility::Wire),
        ] {
            let err = ensure_warm(&verdict).unwrap_err();
            assert!(
                matches!(err, WarmSwapError::NotWarm(v) if v == verdict),
                "non-warm verdict must be refused: {verdict:?}",
            );
        }
    }

    #[test]
    fn a_non_warm_release_touches_no_seam() {
        let trace = trace();
        let gate = RecordingGate::new(&trace);
        let drain = RecordingDrain::new(&trace, &gate, vec![("t1", true)], 0);
        let lease = RecordingLease::new(&trace);
        let resume = RecordingResume::new(&trace, &gate, &lease);
        let swap = WarmSwap::new(gate.clone(), drain, lease.clone(), resume);

        let mut stager = Stager::new(
            |_: &ReleaseId| -> Result<StagedArtifact, StageError> {
                panic!("download must not run for a non-warm release")
            },
            |_: &StagedArtifact| panic!("verify must not run for a non-warm release"),
            SwapActivator::new(&trace),
            || panic!("health check must not run for a non-warm release"),
            ReleaseId::new("v1"),
        );

        // A HOT release must not take the warm path.
        let err = swap
            .run(&Compatibility::Hot, &mut stager, &ReleaseId::new("v2"))
            .unwrap_err();

        assert!(matches!(err, WarmSwapError::NotWarm(Compatibility::Hot)));
        // No seam ran: admission is still open, the lease is still held, and the
        // trace is empty.
        assert!(
            gate.admits(),
            "admission must stay open for a refused release"
        );
        assert!(
            lease.is_held(),
            "the lease must stay held for a refused release"
        );
        assert!(
            trace.borrow().is_empty(),
            "no seam runs for a refused release"
        );
    }

    // --- The ordering invariant, happy path ------------------------------------

    #[test]
    fn warm_swap_walks_close_drain_release_swap_reacquire_resume_in_order() {
        let trace = trace();
        let gate = RecordingGate::new(&trace);
        // Model log: one committed turn, then an INTERRUPTED turn (no complete).
        // The drain checkpoint must be the committed turn, never the orphan.
        let drain = RecordingDrain::new(
            &trace,
            &gate,
            vec![("t1", true), ("t2-interrupted", false)],
            2,
        );
        let lease = RecordingLease::new(&trace);
        let resume = RecordingResume::new(&trace, &gate, &lease);
        let swap = WarmSwap::new(gate.clone(), drain, lease.clone(), resume);

        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| true,
            SwapActivator::new(&trace),
            || Health::Healthy,
            ReleaseId::new("v1"),
        );

        let report = swap
            .run(&Compatibility::Warm, &mut stager, &ReleaseId::new("v2"))
            .unwrap();

        // The exact ordering invariant, observed across every seam.
        assert_eq!(
            *trace.borrow(),
            vec![
                "close".to_owned(),
                "drain".to_owned(),
                "lease.release".to_owned(),
                "swap:v2".to_owned(),
                "lease.reacquire".to_owned(),
                "resume@t1".to_owned(),
            ],
        );
        // The swap committed onto the new binary.
        assert_eq!(
            report.swap,
            Outcome::Committed {
                version: ReleaseId::new("v2"),
            }
        );
        // The checkpoint is the last COMMITTED turn, not the interrupted one.
        assert_eq!(report.checkpoint.last_committed_turn(), "t1");
        // Resume restarted from exactly the drained turn — no drop, no duplicate.
        assert_eq!(report.resume.resumed_from(), "t1");
        // The paused approvals survived the swap and were re-driven on resume.
        assert_eq!(report.checkpoint.paused_approvals(), 2);
        assert_eq!(report.resume.redriven_approvals(), 2);
        // The lease is held again and admission is open on the swapped-in binary.
        assert!(lease.is_held());
        assert!(gate.admits());
    }

    #[test]
    fn no_new_turn_is_admitted_once_the_drain_begins() {
        // The gate's `close` runs before `drain`, and the drain asserts admission
        // is already closed. This test makes the guarantee explicit: after the
        // swap kicks off, a turn arriving mid-drain is refused.
        let trace = trace();
        let gate = RecordingGate::new(&trace);
        assert!(gate.admits(), "admission starts open");

        // Closing admission (step 2) flips it before any drain work.
        gate.close().unwrap();
        assert!(
            !gate.admits(),
            "a turn arriving after admission closes is refused"
        );
    }

    #[test]
    fn a_paused_approval_survives_the_warm_restart() {
        let trace = trace();
        let gate = RecordingGate::new(&trace);
        // Three approvals paused at the checkpoint.
        let drain = RecordingDrain::new(&trace, &gate, vec![("t7", true)], 3);
        let lease = RecordingLease::new(&trace);
        let resume = RecordingResume::new(&trace, &gate, &lease);
        let swap = WarmSwap::new(gate, drain, lease, resume);

        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| true,
            SwapActivator::new(&trace),
            || Health::Healthy,
            ReleaseId::new("v1"),
        );

        let report = swap
            .run(&Compatibility::Warm, &mut stager, &ReleaseId::new("v2"))
            .unwrap();

        // The approvals paused before the swap are carried across it and re-driven
        // after resume — none is lost to the restart.
        assert_eq!(report.checkpoint.paused_approvals(), 3);
        assert_eq!(report.resume.redriven_approvals(), 3);
        assert_eq!(report.resume.resumed_from(), "t7");
    }

    // --- The swap rolled back: lease still re-acquired, conversation resumed ----

    #[test]
    fn a_rolled_back_swap_still_reacquires_the_lease_and_resumes() {
        let trace = trace();
        let gate = RecordingGate::new(&trace);
        let drain = RecordingDrain::new(&trace, &gate, vec![("t1", true)], 1);
        let lease = RecordingLease::new(&trace);
        let resume = RecordingResume::new(&trace, &gate, &lease);
        let swap = WarmSwap::new(gate.clone(), drain, lease.clone(), resume);

        // The new binary fails its health check → the stager auto-rolls back to v1.
        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| true,
            SwapActivator::new(&trace),
            || Health::Unhealthy("readiness probe timed out".to_owned()),
            ReleaseId::new("v1"),
        );

        let report = swap
            .run(&Compatibility::Warm, &mut stager, &ReleaseId::new("v2"))
            .unwrap();

        // The swap rolled back to the previous binary...
        assert_eq!(
            report.swap,
            Outcome::RolledBack {
                stayed_on: ReleaseId::new("v1"),
                reason: "readiness probe timed out".to_owned(),
            }
        );
        // ...but the conversation was NOT left drained-but-dead: the lease is held
        // again and it resumed from the committed turn on the previous binary.
        assert!(
            lease.is_held(),
            "lease re-acquired even on a rolled-back swap"
        );
        assert!(gate.admits(), "admission re-opened on the previous binary");
        assert_eq!(report.resume.resumed_from(), "t1");
        // The pointer flipped forward to v2 then back to v1 — a rollback flip.
        assert_eq!(
            *trace.borrow(),
            vec![
                "close".to_owned(),
                "drain".to_owned(),
                "lease.release".to_owned(),
                "swap:v2".to_owned(),
                "swap:v1".to_owned(),
                "lease.reacquire".to_owned(),
                "resume@t1".to_owned(),
            ],
        );
    }

    // --- An unverified new binary is refused; the lease is put back -------------

    #[test]
    fn an_unverified_binary_is_refused_and_the_lease_is_reacquired() {
        let trace = trace();
        let gate = RecordingGate::new(&trace);
        let drain = RecordingDrain::new(&trace, &gate, vec![("t1", true)], 0);
        let lease = RecordingLease::new(&trace);
        let resume = RecordingResume::new(&trace, &gate, &lease);
        let swap = WarmSwap::new(gate, drain, lease.clone(), resume);

        // The new binary's signature does not verify — the stager applies nothing.
        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| false,
            SwapActivator::new(&trace),
            || panic!("health check must not run for an unverified binary"),
            ReleaseId::new("v1"),
        );

        let err = swap
            .run(&Compatibility::Warm, &mut stager, &ReleaseId::new("v2"))
            .unwrap_err();

        assert!(matches!(err, WarmSwapError::Stage(StageError::Unverified)));
        // Nothing swapped, but the lease is back so the conversation is not stuck
        // behind a released lease: release then re-acquire framed the refused swap.
        assert!(
            lease.is_held(),
            "the lease is re-acquired after a refused swap"
        );
        assert_eq!(
            *trace.borrow(),
            vec![
                "close".to_owned(),
                "drain".to_owned(),
                "lease.release".to_owned(),
                "lease.reacquire".to_owned(),
            ],
        );
        // Resume never runs for a refused swap — the swap error is surfaced first.
        assert!(!trace.borrow().iter().any(|s| s.starts_with("resume")));
    }

    // --- Resume that drifts off the checkpoint is a hard correctness error ------

    #[test]
    fn resume_from_the_wrong_turn_is_a_mismatch_error() {
        let trace = trace();
        let gate = RecordingGate::new(&trace);
        let drain = RecordingDrain::new(&trace, &gate, vec![("t1", true)], 0);
        let lease = RecordingLease::new(&trace);
        // Resume drifts to a different turn than the checkpoint — a dropped or
        // duplicated committed turn.
        let resume = RecordingResume::drifting(&trace, &gate, &lease, "t0-stale");
        let swap = WarmSwap::new(gate, drain, lease, resume);

        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| true,
            SwapActivator::new(&trace),
            || Health::Healthy,
            ReleaseId::new("v1"),
        );

        let err = swap
            .run(&Compatibility::Warm, &mut stager, &ReleaseId::new("v2"))
            .unwrap_err();

        assert!(matches!(
            err,
            WarmSwapError::ResumeMismatch { resumed_from, expected }
                if resumed_from == "t0-stale" && expected == "t1"
        ));
    }

    // --- A failed drain leaves the lease held; nothing is released or swapped ---

    #[test]
    fn a_failed_drain_never_releases_the_lease_or_swaps() {
        let trace = trace();
        let gate = RecordingGate::new(&trace);
        let lease = RecordingLease::new(&trace);
        let failing_drain = {
            let trace = Rc::clone(&trace);
            move || -> Result<Checkpoint, WarmSwapError> {
                log(&trace, "drain");
                Err(WarmSwapError::Drain("journal sync failed".to_owned()))
            }
        };
        let resume = RecordingResume::new(&trace, &gate, &lease);
        let swap = WarmSwap::new(gate.clone(), failing_drain, lease.clone(), resume);

        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| panic!("verify must not run when the drain fails"),
            SwapActivator::new(&trace),
            || panic!("health check must not run when the drain fails"),
            ReleaseId::new("v1"),
        );

        let err = swap
            .run(&Compatibility::Warm, &mut stager, &ReleaseId::new("v2"))
            .unwrap_err();

        assert!(matches!(err, WarmSwapError::Drain(_)));
        // The lease is still held — a failed drain must not release it — and
        // nothing swapped.
        assert!(lease.is_held(), "a failed drain keeps the lease held");
        assert_eq!(
            *trace.borrow(),
            vec!["close".to_owned(), "drain".to_owned()]
        );
    }

    // --- A failed re-acquire is reported as a lease re-acquire error ------------

    #[test]
    fn a_failed_reacquire_surfaces_as_a_lease_error() {
        let trace = trace();
        let gate = RecordingGate::new(&trace);
        let drain = RecordingDrain::new(&trace, &gate, vec![("t1", true)], 0);
        let lease = RecordingLease::failing_reacquire(&trace);
        let resume = RecordingResume::new(&trace, &gate, &lease);
        let swap = WarmSwap::new(gate, drain, lease, resume);

        let mut stager = Stager::new(
            ok_source,
            |_: &StagedArtifact| true,
            SwapActivator::new(&trace),
            || Health::Healthy,
            ReleaseId::new("v1"),
        );

        let err = swap
            .run(&Compatibility::Warm, &mut stager, &ReleaseId::new("v2"))
            .unwrap_err();

        assert!(matches!(
            err,
            WarmSwapError::Lease {
                op: "re-acquire",
                ..
            }
        ));
    }

    #[test]
    fn not_warm_error_reads_plainly() {
        let msg = WarmSwapError::NotWarm(Compatibility::Hot).to_string();
        assert_eq!(
            msg,
            "refused: only a warm binary swap takes the click-to-restart path \
             (a config change that reloads with no restart)",
        );
        for banned in ["sorry", "please", "unfortunately"] {
            assert!(!msg.to_lowercase().contains(banned));
        }
    }
}