symforge 11.22.2

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
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
//! Whole mutation authority for the V11 index lifecycle (T024).
//!
//! Every identity here is drawn from one process-global monotonic counter, so no
//! identity is ever reused and no two identities of different kinds can collide.
//! Identities are diagnostics for ordering only: nothing in this module grants a
//! mutation because a counter compared equal. A mutation is authorized by a
//! sealed [`CurrentMutationGrantAuthority`] that was consumed from an exact live
//! `Current` publication, or it is refused.

// The identity newtypes and their counter moved to `crate::lifecycle_identity`
// so the V11 provenance types under `protocol` can mint from the SAME counter
// without importing this dark module. Re-exported here so every existing path
// through `authority::` keeps resolving.
//
// Do not reintroduce a local counter: two counters means two identity spaces,
// and two identities minted "fresh" could then compare unequal while both
// claiming uniqueness.
pub use crate::lifecycle_identity::{
    BindingIdentity, CandidateIdentity, GenerationIdentity, ObserverToken, PublicationIdentity,
    SnapshotIdentity,
};

/// Monotonic per-source mutation epoch. An ordering aid, never an authorization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct MutationEpoch(u64);

impl MutationEpoch {
    /// The epoch a source occupies before any mutation has ever been granted.
    pub const fn initial() -> Self {
        Self(0)
    }

    fn advanced(self) -> Self {
        Self(self.0 + 1)
    }

    /// Diagnostic value. Callers must not derive permission from it.
    pub const fn get(self) -> u64 {
        self.0
    }
}

/// Authority binding one source to exactly one physical root.
///
/// Cloneable: a binding is a description of which root is authoritative, not a
/// consumable permission. Equality is exact on both the binding identity and the
/// root lease identity, so a rebind never compares equal to its predecessor.
///
/// **A clone shares its liveness with the original**, because a clone IS this
/// binding rather than a copy of it. Revocation therefore reaches authority that
/// was already handed out — the registry can refuse to hand out a stopped slot's
/// binding, but it cannot reach a clone a holder took before the stop, and a
/// permit granted from such a clone would write to disk under a slot the
/// registry had already retired.
#[derive(Debug, Clone)]
pub struct BindingAuthority {
    identity: BindingIdentity,
    physical_root: crate::live_index::index_lifecycle::physical_root::PhysicalRootIdentity,
    /// Shared across clones on purpose; see the type comment.
    revoked: std::sync::Arc<std::sync::atomic::AtomicBool>,
}

/// Identity, not liveness. Two handles on one binding are the same binding
/// whether or not it has since been revoked, and comparing the flag would make
/// a revocation look like a rebind.
impl PartialEq for BindingAuthority {
    fn eq(&self, other: &Self) -> bool {
        self.identity == other.identity && self.physical_root == other.physical_root
    }
}

impl Eq for BindingAuthority {}

impl BindingAuthority {
    /// Bind a source to a physical root under a fresh never-reused identity.
    pub fn bind(
        physical_root: crate::live_index::index_lifecycle::physical_root::PhysicalRootIdentity,
    ) -> Self {
        Self {
            identity: BindingIdentity::fresh(),
            physical_root,
            revoked: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
        }
    }

    /// Retire this binding and every clone of it. Idempotent, never undone.
    pub fn revoke(&self) {
        self.revoked
            .store(true, std::sync::atomic::Ordering::Release);
    }

    /// Whether this binding still authorizes anything.
    pub fn is_live(&self) -> bool {
        !self.revoked.load(std::sync::atomic::Ordering::Acquire)
    }

    /// The binding's identity.
    pub fn identity(&self) -> BindingIdentity {
        self.identity
    }

    /// The physical root this binding authorizes, and no other.
    pub fn physical_root(
        &self,
    ) -> crate::live_index::index_lifecycle::physical_root::PhysicalRootIdentity {
        self.physical_root
    }
}

/// Authority for one in-progress candidate build.
///
/// A candidate is never a mutation grant: it has not published, so it cannot
/// authorize a source-disk side effect.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CandidateAuthority {
    identity: CandidateIdentity,
    binding: BindingAuthority,
    observer_cut: ObserverToken,
}

impl CandidateAuthority {
    /// Open a candidate under a binding at an observer cut.
    pub fn open(binding: BindingAuthority, observer_cut: ObserverToken) -> Self {
        Self {
            identity: CandidateIdentity::fresh(),
            binding,
            observer_cut,
        }
    }

    /// The candidate's identity.
    pub fn identity(&self) -> CandidateIdentity {
        self.identity
    }

    /// The binding this candidate was opened under.
    pub fn binding(&self) -> &BindingAuthority {
        &self.binding
    }

    /// The observer cut this candidate is building through.
    pub fn observer_cut(&self) -> ObserverToken {
        self.observer_cut
    }
}

/// Generation-bound mutation authority.
///
/// Carries the exact generation, binding, and epoch a mutation is bound to. It
/// is produced only from a consumed [`CurrentMutationGrantAuthority`] and is
/// validated as a whole: no consumer may compare one field and infer the rest.
#[derive(Debug, PartialEq, Eq)]
pub struct MutationAuthority {
    generation: GenerationIdentity,
    binding: BindingAuthority,
    epoch: MutationEpoch,
    publication: PublicationIdentity,
}

impl MutationAuthority {
    /// The generation this authority is bound to.
    pub fn generation(&self) -> GenerationIdentity {
        self.generation
    }

    /// The binding this authority is bound to.
    pub fn binding(&self) -> &BindingAuthority {
        &self.binding
    }

    /// The mutation epoch this authority was granted at.
    pub fn epoch(&self) -> MutationEpoch {
        self.epoch
    }

    /// The exact `Current` publication this authority was consumed from.
    pub fn publication(&self) -> PublicationIdentity {
        self.publication
    }
}

/// Proof that the granting source published a non-`Current` state.
///
/// The permit refuses every source-disk side effect until it holds one of these,
/// so a side effect can never precede the publication that makes the source
/// non-queryable.
#[derive(Debug, PartialEq, Eq)]
pub struct NonCurrentPublicationProof {
    publication: PublicationIdentity,
    epoch: MutationEpoch,
}

impl NonCurrentPublicationProof {
    /// The non-`Current` publication that was actually stored.
    pub fn publication(&self) -> PublicationIdentity {
        self.publication
    }

    /// The epoch that publication carried.
    pub fn epoch(&self) -> MutationEpoch {
        self.epoch
    }
}

/// Sealed, non-cloneable authority to grant exactly one mutation permit.
///
/// Constructible only inside this module, and only by
/// [`SourceRuntime::request_mutation_grant`] after it has validated an exact live
/// `Current` publication and published non-`Current`.
#[derive(Debug)]
pub struct CurrentMutationGrantAuthority {
    authority: MutationAuthority,
    published_non_current: NonCurrentPublicationProof,
}

impl CurrentMutationGrantAuthority {
    /// The whole generation-bound authority this grant carries.
    pub fn authority(&self) -> &MutationAuthority {
        &self.authority
    }

    /// Proof the source was published non-`Current` before this grant existed.
    pub fn published_non_current(&self) -> &NonCurrentPublicationProof {
        &self.published_non_current
    }

    /// Consume the grant, yielding its parts. Consumption is by move, so a grant
    /// can authorize at most one permit.
    pub(crate) fn into_parts(self) -> (MutationAuthority, NonCurrentPublicationProof) {
        (self.authority, self.published_non_current)
    }
}

/// The live `Current` publication of a source.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CurrentPublication {
    publication: PublicationIdentity,
    generation: GenerationIdentity,
    binding: BindingAuthority,
    observer_cut: ObserverToken,
}

impl CurrentPublication {
    /// Promote a verified generation to `Current` under a fresh publication.
    pub fn promote(binding: BindingAuthority, observer_cut: ObserverToken) -> Self {
        Self {
            publication: PublicationIdentity::fresh(),
            generation: GenerationIdentity::fresh(),
            binding,
            observer_cut,
        }
    }

    /// The publication identity a mutation grant must name exactly.
    pub fn publication(&self) -> PublicationIdentity {
        self.publication
    }

    /// The generation this publication makes queryable.
    pub fn generation(&self) -> GenerationIdentity {
        self.generation
    }

    /// The binding this publication was promoted under.
    pub fn binding(&self) -> &BindingAuthority {
        &self.binding
    }

    /// The observer cut this publication is complete through.
    pub fn observer_cut(&self) -> ObserverToken {
        self.observer_cut
    }
}

/// What a caller presents when asking for a mutation grant.
///
/// Only [`MutationGrantInput::LiveCurrent`] naming the exact live publication can
/// ever be accepted. Every other shape is a source of authority that has not
/// published a queryable generation, and is refused by construction rather than
/// by inspecting its contents.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MutationGrantInput {
    /// The publication the caller believes is live and `Current`.
    LiveCurrent(PublicationIdentity),
    /// An in-progress candidate build.
    Candidate(CandidateIdentity),
    /// An untrusted on-disk snapshot seed.
    Snapshot(SnapshotIdentity),
    /// A generation retained by a non-`Current` state for recovery or accounting.
    RetainedGeneration(GenerationIdentity),
    /// A publication that was `Current` at some earlier point.
    StalePublication(PublicationIdentity),
}

impl MutationGrantInput {
    fn provenance(&self) -> Provenance {
        match self {
            Self::LiveCurrent(_) => Provenance::LiveCurrent,
            Self::Candidate(_) => Provenance::Candidate,
            Self::Snapshot(_) => Provenance::Snapshot,
            Self::RetainedGeneration(_) => Provenance::RetainedGeneration,
            Self::StalePublication(_) => Provenance::StalePublication,
        }
    }
}

/// Named provenance of a refused grant input, for refusal reporting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Provenance {
    /// A publication claimed to be the live `Current` one.
    LiveCurrent,
    /// An in-progress candidate.
    Candidate,
    /// An on-disk snapshot seed.
    Snapshot,
    /// A generation retained by a non-`Current` state.
    RetainedGeneration,
    /// A previously-`Current` publication.
    StalePublication,
}

/// Named source phase, for refusal reporting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PhaseName {
    /// No generation retained, none queryable.
    Loading,
    /// Exactly one generation queryable.
    Current,
    /// One generation retained, none queryable.
    Refreshing,
    /// Zero or one generation retained for recovery, none queryable.
    Blocked,
    /// Revoked; zero or one generation retained for accounting.
    Stopping,
}

/// The live runtime phase of one source.
#[derive(Debug)]
enum SourcePhase {
    Loading {
        binding: BindingAuthority,
        observer_phase: ObserverPhase,
        work: NonCurrentWork,
    },
    Current(CurrentPublication),
    Refreshing {
        retained: GenerationIdentity,
        binding: BindingAuthority,
        publication: PublicationIdentity,
        observer_phase: ObserverPhase,
        /// Permits outstanding against this source.
        ///
        /// Plural, per the frozen model. Slice 1 tracked a single
        /// `PermitDrainSignal` and refused a transition on one outstanding
        /// permit, which cannot express a source draining several at once.
        active_permits: ActivePermits,
        work: NonCurrentWork,
    },
    Blocked {
        binding: BindingAuthority,
        observer_phase: ObserverPhase,
        retained: Option<GenerationIdentity>,
        cause: BlockedCause,
    },
    Stopping {
        retained: Option<GenerationIdentity>,
        /// Teardown capacity already charged for this source's revocation.
        ///
        /// The frozen model requires a `Stopping` source to carry the residency
        /// it has committed, so a revocation cannot be started that the process
        /// cannot afford to finish. Slice 2's capacity work refunds against this;
        /// until `capacity.rs` lands it records the charge as an opaque receipt
        /// rather than pretending no charge exists.
        committed_source_revocation_residency: Option<RevocationResidency>,
    },
}

/// Where a source's filesystem observer stands.
///
/// A source may hold no observer at all, hold one, be handing one over, or have
/// lost one and be waiting to retry. Slice 1 carried only a bare `ObserverToken`
/// where a token existed and had no way to say "absent" or "mid-handoff".
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ObserverPhase {
    /// No observer has been registered yet.
    Absent,
    /// An observer is registered and delivering.
    Active {
        /// The stable token identifying this registration.
        token: ObserverToken,
    },
    /// An observer is being handed over and is no longer authoritative.
    Draining {
        /// The token being drained.
        token: ObserverToken,
    },
    /// The observer was lost; a replacement has not yet been registered.
    ObserverFree {
        /// Identifies the handoff attempt, so a retry is not mistaken for a new one.
        handoff: ObserverToken,
    },
}

impl ObserverPhase {
    /// The token this phase stands on, when one exists.
    ///
    /// `Absent` and `ObserverFree` deliberately return `None` rather than
    /// inventing a token: a source with no live observer must not be able to
    /// present one.
    pub fn token(&self) -> Option<ObserverToken> {
        match self {
            Self::Active { token } | Self::Draining { token } => Some(*token),
            Self::Absent | Self::ObserverFree { .. } => None,
        }
    }
}

/// What a non-`Current` source is doing about becoming `Current` again.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NonCurrentWork {
    /// Known stale, nothing started yet.
    Dirty,
    /// Cannot start until capacity is granted.
    WaitingForCapacity,
    /// A candidate is being built under this authority.
    Building {
        /// The candidate doing the work.
        candidate: CandidateIdentity,
    },
    /// A built candidate is being verified.
    Verifying {
        /// The candidate under verification.
        candidate: CandidateIdentity,
    },
    /// A previous attempt failed and a retry is pending.
    RetryWait {
        /// How many attempts have been made.
        attempt: u32,
    },
}

/// The permits outstanding against one source, and whether any ever was.
///
/// A newtype rather than a bare count: the frozen model requires a transition to
/// refuse while ANY permit is outstanding, and a count that could be decremented
/// twice would silently authorize an install over a live permit.
///
/// `ever_recorded` is the second question, and it is not the same one.
/// "Outstanding" answers whether a write is in flight. "Ever recorded" answers
/// whether a write HAPPENED against this freeze — and once it has, the retained
/// generation no longer describes the bytes on disk, so retiring the permit must
/// not restore it. FR-043 says exactly that: no terminal path directly restores
/// the prior publication. Draining is not a time machine.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ActivePermits {
    outstanding: Vec<PublicationIdentity>,
    ever_recorded: bool,
}

impl ActivePermits {
    /// No permits outstanding.
    pub fn none() -> Self {
        Self::default()
    }

    /// Record a permit issued under `grant`.
    pub fn record(&mut self, grant: PublicationIdentity) {
        self.ever_recorded = true;
        if !self.outstanding.contains(&grant) {
            self.outstanding.push(grant);
        }
    }

    /// Whether any permit was ever recorded against this freeze.
    ///
    /// Latched: `retire` never clears it, and `freeze` carries it across, so it
    /// is cleared only by installing a successor `Current`.
    pub fn ever_recorded(&self) -> bool {
        self.ever_recorded
    }

    /// Retire a permit. Returns whether it was outstanding, so a caller cannot
    /// retire a permit twice and drive the count below the truth.
    pub fn retire(&mut self, grant: PublicationIdentity) -> bool {
        let before = self.outstanding.len();
        self.outstanding.retain(|entry| *entry != grant);
        self.outstanding.len() != before
    }

    /// Whether anything is still outstanding.
    pub fn is_drained(&self) -> bool {
        self.outstanding.is_empty()
    }

    /// The permits still outstanding, so a caller can retire them by name
    /// rather than having to have remembered each identity it was issued.
    pub fn outstanding(&self) -> impl Iterator<Item = PublicationIdentity> + '_ {
        self.outstanding.iter().copied()
    }

    /// How many permits are outstanding.
    pub fn len(&self) -> usize {
        self.outstanding.len()
    }

    /// Whether no permit has ever been recorded, or all have retired.
    pub fn is_empty(&self) -> bool {
        self.outstanding.is_empty()
    }
}

/// Why a source is `Blocked`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BlockedCause {
    /// A candidate failed verification and no retry can help without operator action.
    VerificationFailed,
    /// The observer could not be re-registered.
    ObserverUnavailable,
    /// Capacity was refused and the request cannot be satisfied.
    CapacityRefused,
}

/// Teardown capacity committed to a revocation.
///
/// Opaque until `capacity.rs` lands. It exists now so `Stopping` can carry the
/// charge the frozen model requires rather than omitting the field and implying
/// no charge was made.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RevocationResidency {
    charge: PublicationIdentity,
}

impl RevocationResidency {
    /// Record a committed teardown charge under a never-reused identity.
    pub fn committed() -> Self {
        Self {
            charge: PublicationIdentity::fresh(),
        }
    }

    /// The identity of the charge, for refund reconciliation.
    pub fn charge(self) -> PublicationIdentity {
        self.charge
    }
}

impl SourcePhase {
    fn name(&self) -> PhaseName {
        match self {
            Self::Loading { .. } => PhaseName::Loading,
            Self::Current(_) => PhaseName::Current,
            Self::Refreshing { .. } => PhaseName::Refreshing,
            Self::Blocked { .. } => PhaseName::Blocked,
            Self::Stopping { .. } => PhaseName::Stopping,
        }
    }
}

/// Why an authority request was refused.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthorityRefusal {
    /// The source is not `Current`, so no queryable generation exists to grant from.
    PhaseNotCurrent {
        /// The phase actually observed.
        phase: PhaseName,
    },
    /// The presented input is not a live `Current` publication.
    ProvenanceNotLiveCurrent {
        /// The provenance actually presented.
        provenance: Provenance,
    },
    /// The named publication is not the one that is live.
    PublicationIdentityMismatch {
        /// The publication the caller named.
        presented: PublicationIdentity,
        /// The publication that is actually live.
        live: PublicationIdentity,
    },
    /// The permit's pinned physical root is no longer installed.
    PhysicalRootReplaced,
    /// The permit has already reached a terminal state.
    PermitAlreadyTerminal,
    /// A side effect was attempted on a permit that is already in flight.
    SideEffectAlreadyInFlight,
    /// The authority presented does not match the live source as a whole.
    WholeAuthorityMismatch,
    /// The binding the authority names has been revoked, so it authorizes
    /// nothing regardless of how the holder obtained it.
    BindingRevoked {
        /// The retired binding, for diagnosis.
        binding: BindingIdentity,
    },
    /// A transition was attempted while a permit was still outstanding.
    OutstandingPermit,
}

/// The live runtime of one source: the sole owner of its phase, epoch, and the
/// record of permits it has issued.
#[derive(Debug)]
pub struct SourceRuntime {
    phase: SourcePhase,
    mutation_epoch: MutationEpoch,
    grants_issued: u64,
}

impl SourceRuntime {
    /// Start a source in `Loading` with no queryable generation.
    pub fn loading(binding: BindingAuthority) -> Self {
        Self {
            phase: SourcePhase::Loading {
                binding,
                observer_phase: ObserverPhase::Absent,
                work: NonCurrentWork::Dirty,
            },
            mutation_epoch: MutationEpoch::initial(),
            grants_issued: 0,
        }
    }

    /// Start a source already `Current` on the given publication.
    pub fn current(publication: CurrentPublication) -> Self {
        Self {
            phase: SourcePhase::Current(publication),
            mutation_epoch: MutationEpoch::initial(),
            grants_issued: 0,
        }
    }

    /// Start a source in `Refreshing`, retaining one generation.
    pub fn refreshing(binding: BindingAuthority, retained: GenerationIdentity) -> Self {
        Self {
            phase: SourcePhase::Refreshing {
                retained,
                binding,
                publication: PublicationIdentity::fresh(),
                observer_phase: ObserverPhase::Absent,
                active_permits: ActivePermits::none(),
                work: NonCurrentWork::Dirty,
            },
            mutation_epoch: MutationEpoch::initial(),
            grants_issued: 0,
        }
    }

    /// Start a source in `Blocked`.
    pub fn blocked(binding: BindingAuthority, retained: Option<GenerationIdentity>) -> Self {
        Self {
            phase: SourcePhase::Blocked {
                binding,
                observer_phase: ObserverPhase::Absent,
                retained,
                cause: BlockedCause::VerificationFailed,
            },
            mutation_epoch: MutationEpoch::initial(),
            grants_issued: 0,
        }
    }

    /// Start a source in `Stopping`, carrying the teardown capacity it committed.
    pub fn stopping(
        retained: Option<GenerationIdentity>,
        committed_source_revocation_residency: Option<RevocationResidency>,
    ) -> Self {
        Self {
            phase: SourcePhase::Stopping {
                retained,
                committed_source_revocation_residency,
            },
            mutation_epoch: MutationEpoch::initial(),
            grants_issued: 0,
        }
    }

    /// The observer phase this source stands on.
    ///
    /// `Current` sources carry their observer cut inside the publication, so this
    /// reports `Active` on the publication's cut rather than inventing a
    /// separate one.
    pub fn observer_phase(&self) -> ObserverPhase {
        match &self.phase {
            SourcePhase::Current(publication) => ObserverPhase::Active {
                token: publication.observer_cut(),
            },
            SourcePhase::Loading { observer_phase, .. }
            | SourcePhase::Refreshing { observer_phase, .. }
            | SourcePhase::Blocked { observer_phase, .. } => observer_phase.clone(),
            SourcePhase::Stopping { .. } => ObserverPhase::Absent,
        }
    }

    /// What a non-`Current` source is doing about becoming `Current` again.
    pub fn work(&self) -> Option<NonCurrentWork> {
        match &self.phase {
            SourcePhase::Loading { work, .. } | SourcePhase::Refreshing { work, .. } => {
                Some(work.clone())
            }
            SourcePhase::Current(_)
            | SourcePhase::Blocked { .. }
            | SourcePhase::Stopping { .. } => None,
        }
    }

    /// The permits outstanding against this source.
    ///
    /// Only a `Refreshing` source can hold permits: a grant moves the source off
    /// `Current`, and no other phase issues one.
    pub fn active_permits(&self) -> ActivePermits {
        match &self.phase {
            SourcePhase::Refreshing { active_permits, .. } => active_permits.clone(),
            _ => ActivePermits::none(),
        }
    }

    /// The teardown capacity a `Stopping` source has committed, if any.
    pub fn committed_revocation_residency(&self) -> Option<RevocationResidency> {
        match &self.phase {
            SourcePhase::Stopping {
                committed_source_revocation_residency,
                ..
            } => *committed_source_revocation_residency,
            _ => None,
        }
    }

    /// The current phase name.
    pub fn phase(&self) -> PhaseName {
        self.phase.name()
    }

    /// The current mutation epoch. Diagnostic only.
    pub fn mutation_epoch(&self) -> MutationEpoch {
        self.mutation_epoch
    }

    /// How many permits this source has ever issued. A refused request must not
    /// change this.
    pub fn grants_issued(&self) -> u64 {
        self.grants_issued
    }

    /// The generation this phase retains, if any. Accounting, not permission.
    ///
    /// `Loading` retains none, `Refreshing` exactly one, and `Blocked`/`Stopping`
    /// zero or one for recovery and accounting. Retention says nothing about
    /// whether a reader may be served: ask [`Self::queryable_generation`], which
    /// is where A20 lives. This doc previously stated the pre-A20 rule — that a
    /// retained generation is never queryable — twelve lines above the code that
    /// implements the amendment, which is precisely the claim Slice 4 would have
    /// read when it wired reads.
    pub fn retained_generation(&self) -> Option<GenerationIdentity> {
        match &self.phase {
            SourcePhase::Loading { .. } => None,
            SourcePhase::Current(publication) => Some(publication.generation()),
            SourcePhase::Refreshing { retained, .. } => Some(*retained),
            SourcePhase::Blocked { retained, .. } | SourcePhase::Stopping { retained, .. } => {
                *retained
            }
        }
    }

    /// The generation a reader may be served from, if any (F020-V11-A20).
    ///
    /// Queryability closes on COMPLETENESS, not recency. `Current` is queryable.
    /// So is the single generation `Refreshing` retains **while no mutation
    /// permit is outstanding against the source**: it is the complete verified
    /// generation that was `Current` immediately before the refresh began, so
    /// serving it exposes no partial state, and refusing to serve it would take
    /// the source offline for the whole of a rebuild while buying no safety at
    /// all.
    ///
    /// The permit condition is not a detail. `Refreshing` is reached two ways.
    /// A **reload** builds a candidate elsewhere and leaves the retained
    /// generation's bytes untouched, which is the case A20 was written for. A
    /// **mutation** reaches it through `request_mutation_grant`, which freezes
    /// precisely so the source stops being queryable before a source-disk side
    /// effect is authorized; there the retained generation describes files a
    /// permit is in the middle of replacing, so it is complete and must still not
    /// be served. A20 as first written extended the reload argument to the
    /// mutation case silently, and reopened the exact window the freeze ordering
    /// exists to close.
    ///
    /// The condition is `ever_recorded`, not `is_drained`, and the difference is
    /// the whole rule. Draining says the write finished; it says nothing about
    /// whether it happened. Once a permit has existed against this freeze the
    /// retained generation may no longer describe the bytes on disk, so serving
    /// it after the permit retires is the V10 last-valid answer wearing a new
    /// name — and FR-043 forbids it in as many words: no terminal path directly
    /// restores the prior publication. Only a successor `Current` does, which is
    /// what clears the latch.
    ///
    /// `Blocked` and `Stopping` return `None` even when they retain something.
    /// Neither has a successor in flight, so its retention is a remnant rather
    /// than a refresh, and serving it would be serving the last thing that
    /// happened to work rather than a generation something is actively replacing.
    /// `Loading` has nothing.
    pub fn queryable_generation(&self) -> Option<GenerationIdentity> {
        match &self.phase {
            SourcePhase::Current(publication) => Some(publication.generation()),
            SourcePhase::Refreshing {
                retained,
                active_permits,
                ..
            } => (!active_permits.ever_recorded()).then_some(*retained),
            SourcePhase::Loading { .. }
            | SourcePhase::Blocked { .. }
            | SourcePhase::Stopping { .. } => None,
        }
    }

    /// Whether a reader may currently be served at all.
    pub fn is_queryable(&self) -> bool {
        self.queryable_generation().is_some()
    }

    /// The binding a non-`Current` phase is still bound to, if any.
    /// Every phase except `Stopping` is bound to a physical root.
    ///
    /// `Loading` and `Blocked` previously reported `None` here, which read as
    /// "this source has no binding" when in fact both carry one — a `Loading`
    /// source is bound before it has a generation, and a `Blocked` source stays
    /// bound so an operator can act on the right root. Only `Stopping` has
    /// genuinely surrendered its binding.
    pub fn retained_binding(&self) -> Option<&BindingAuthority> {
        match &self.phase {
            SourcePhase::Current(publication) => Some(publication.binding()),
            SourcePhase::Refreshing { binding, .. }
            | SourcePhase::Loading { binding, .. }
            | SourcePhase::Blocked { binding, .. } => Some(binding),
            SourcePhase::Stopping { .. } => None,
        }
    }

    /// Why a `Blocked` source is blocked.
    ///
    /// `None` for every other phase rather than a default cause: a source that
    /// is not blocked has no reason to report.
    pub fn blocked_cause(&self) -> Option<BlockedCause> {
        match &self.phase {
            SourcePhase::Blocked { cause, .. } => Some(cause.clone()),
            _ => None,
        }
    }

    /// The live `Current` publication, if the source is `Current`.
    pub fn live_publication(&self) -> Option<&CurrentPublication> {
        match &self.phase {
            SourcePhase::Current(publication) => Some(publication),
            _ => None,
        }
    }

    /// The identity of the publication this source currently stands on, when it
    /// stands on one.
    ///
    /// `Blocked` and `Stopping` report their immutable phase without inventing a
    /// publication identity they never stored.
    pub fn published_identity(&self) -> Option<PublicationIdentity> {
        match &self.phase {
            SourcePhase::Current(publication) => Some(publication.publication()),
            SourcePhase::Refreshing { publication, .. } => Some(*publication),
            SourcePhase::Loading { .. }
            | SourcePhase::Blocked { .. }
            | SourcePhase::Stopping { .. } => None,
        }
    }

    /// Publish non-`Current`, advancing the mutation epoch, and return the
    /// identity of the publication actually stored.
    ///
    /// Epoch and permit record are carried across, never reset: the epoch is
    /// monotonic for the life of the source, so a reload or rebind cannot rewind
    /// it and let a stale authority compare equal to a later one.
    /// Returns `None` when the phase had nothing to publish.
    ///
    /// `Loading`, `Blocked` and `Stopping` store no publication, so an earlier
    /// version returning a freshly minted identity for them was attesting to a
    /// publication nothing had stored — the same defect this slice already fixed
    /// once. It also advanced the epoch for a freeze that did not happen; it now
    /// leaves the epoch alone on that path too.
    pub fn freeze(&mut self) -> Option<PublicationIdentity> {
        // Observer phase, outstanding permits and in-flight work are CARRIED
        // ACROSS a re-freeze, not reset. A source already `Refreshing` with a
        // live permit that froze again would otherwise publish an empty
        // `active_permits`, and the very next transition would see a drained
        // source and install over that permit -- the defect this slice widened
        // the model to make expressible.
        let (retained, binding, observer_phase, active_permits, work) = match &self.phase {
            SourcePhase::Current(current) => (
                current.generation(),
                current.binding().clone(),
                ObserverPhase::Active {
                    token: current.observer_cut(),
                },
                ActivePermits::none(),
                NonCurrentWork::Dirty,
            ),
            SourcePhase::Refreshing {
                retained,
                binding,
                observer_phase,
                active_permits,
                work,
                ..
            } => (
                *retained,
                binding.clone(),
                observer_phase.clone(),
                active_permits.clone(),
                work.clone(),
            ),
            SourcePhase::Loading { .. }
            | SourcePhase::Blocked { .. }
            | SourcePhase::Stopping { .. } => {
                return None;
            }
        };
        let publication = PublicationIdentity::fresh();
        self.mutation_epoch = self.mutation_epoch.advanced();
        self.phase = SourcePhase::Refreshing {
            retained,
            binding,
            publication,
            observer_phase,
            active_permits,
            work,
        };
        Some(publication)
    }

    /// Record that a permit was issued against this source's current freeze.
    ///
    /// Returns whether it was recorded: only a `Refreshing` source can hold
    /// permits, because a grant is what moved it off `Current`.
    pub fn record_permit(&mut self, grant: PublicationIdentity) -> bool {
        match &mut self.phase {
            SourcePhase::Refreshing { active_permits, .. } => {
                active_permits.record(grant);
                true
            }
            _ => false,
        }
    }

    /// Retire a permit that has reached a terminal path.
    ///
    /// Returns whether this call retired it. A second retire of the same permit
    /// returns `false` rather than draining the source twice, so a
    /// double-terminated permit cannot make a still-busy source look drained.
    pub fn retire_permit(&mut self, grant: PublicationIdentity) -> bool {
        match &mut self.phase {
            SourcePhase::Refreshing { active_permits, .. } => active_permits.retire(grant),
            _ => false,
        }
    }

    /// Record what a non-`Current` source is now doing.
    pub fn set_work(&mut self, next: NonCurrentWork) -> bool {
        match &mut self.phase {
            SourcePhase::Loading { work, .. } | SourcePhase::Refreshing { work, .. } => {
                *work = next;
                true
            }
            _ => false,
        }
    }

    /// Record an observer phase change for a non-`Current` source.
    pub fn set_observer_phase(&mut self, next: ObserverPhase) -> bool {
        match &mut self.phase {
            SourcePhase::Loading { observer_phase, .. }
            | SourcePhase::Refreshing { observer_phase, .. }
            | SourcePhase::Blocked { observer_phase, .. } => {
                *observer_phase = next;
                true
            }
            _ => false,
        }
    }

    /// Install a new `Current` publication, preserving the monotonic epoch and
    /// the grant record.
    ///
    /// Deliberately `pub(crate)`: republishing `Current` makes the source
    /// queryable again, and a permit outstanding from the previous freeze would
    /// then be able to act against a queryable source. `transition::apply` is
    /// the only caller, and it reaches this line only after Drain has confirmed
    /// nothing is outstanding.
    pub(crate) fn install(&mut self, publication: CurrentPublication) {
        self.phase = SourcePhase::Current(publication);
    }

    /// Request the one grant that can authorize a mutation permit.
    ///
    /// On refusal, neither the mutation epoch nor the permit record advances and
    /// the phase is unchanged: a rejected request leaves no trace that a later
    /// step could mistake for permission.
    ///
    /// On acceptance, the source publishes `Refreshing` and advances its epoch
    /// *before* the grant exists, so no holder of a grant can perform a source
    /// side effect while the source is still queryable.
    pub fn request_mutation_grant(
        &mut self,
        input: MutationGrantInput,
    ) -> Result<CurrentMutationGrantAuthority, AuthorityRefusal> {
        let live = match &self.phase {
            SourcePhase::Current(publication) => publication.clone(),
            other => {
                return Err(AuthorityRefusal::PhaseNotCurrent {
                    phase: other.name(),
                });
            }
        };

        let presented = match input {
            MutationGrantInput::LiveCurrent(presented) => presented,
            other => {
                return Err(AuthorityRefusal::ProvenanceNotLiveCurrent {
                    provenance: other.provenance(),
                });
            }
        };

        if presented != live.publication() {
            return Err(AuthorityRefusal::PublicationIdentityMismatch {
                presented,
                live: live.publication(),
            });
        }

        // Publish non-Current before the grant exists. Order is the invariant:
        // the source stops being queryable first, then a mutation is authorized.
        // `freeze` performs and records that publication, and the proof below
        // names the identity it actually stored -- not a freshly minted one that
        // nothing published.
        // The phase was matched as `Current` above and nothing between here and
        // there can change it, so `freeze` has something to publish.
        let publication = self
            .freeze()
            .expect("a Current source always has a publication to freeze");
        // Record the permit this grant will become, on the source itself. The
        // freeze alone no longer makes the source unqueryable — A20 keeps a
        // refreshing source serving its retained generation — so what closes
        // reads for the duration of a mutation is this outstanding permit. A
        // grant that did not record one would leave the source serving the very
        // files its holder is about to replace.
        self.record_permit(publication);
        let epoch = self.mutation_epoch;
        self.grants_issued += 1;

        Ok(CurrentMutationGrantAuthority {
            authority: MutationAuthority {
                generation: live.generation(),
                binding: live.binding().clone(),
                epoch,
                publication: live.publication(),
            },
            published_non_current: NonCurrentPublicationProof { publication, epoch },
        })
    }
}