meerkat-machine-schema 0.7.23

Formal machine schemas and transition definitions for Meerkat
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
//! SessionTurnAdmissionMachine — canonical ephemeral turn-admission gate.
//!
//! This is the live turn-admission lifecycle owned by
//! `EphemeralSessionService` (the `turn_admission: Arc<Mutex<TurnAdmissionSlot>>`
//! slot). It models the multi-phase admission lifecycle
//! (`Idle`/`Admitted`/`Running`/`Completing`/`ShuttingDown`) and the
//! start-turn disposition / interrupt / shutdown / dispatch-authorization
//! decisions. It was previously an inline `machine!` in
//! `meerkat-session/src/turn_admission.rs` with no TLA model; it is now a
//! canonical machine with a generated schema-free authority and a standalone
//! TLA spec (LUC-524, P0 Dogma Invariant 1).
//!
//! Its `ResolveStartTurnDisposition` consumes the pending-continuation
//! disposition emitted by the canonical
//! [`crate::catalog::dsl::session_document`] machine as a typed input
//! (`PendingContinuationDisposition`); the meerkat-session shell drives the
//! SessionDocumentMachine first and MIRRORS that disposition into this
//! machine's input.
//!
//! Teardown discipline (0.7.2, undisciplined-shell-inputs D1/D2a):
//!
//! - **D1 — machine-owned admission drain.** Every transition INTO
//!   `ShuttingDown` mints a drain obligation (`admission_drain_pending`,
//!   `PendingAdmissionDrainRequested`): the owning shell must flush the
//!   session command queue, resolving every pending turn-admission waiter
//!   with a typed terminal outcome instead of dropping reply channels, then
//!   close the obligation with `ResolvePendingAdmissionDrained`. The final
//!   teardown step (`AuthorizeSessionTeardown` -> `SessionTeardownAuthorized`)
//!   is guarded on the drain obligation being closed, so "torn down" is
//!   *defined* as "pending admission work resolved".
//! - **D2a — total inputs in `ShuttingDown`.** Inputs that legitimately race
//!   archive/discard teardown (`ClaimTurn`, `AbortClaim`, `BeginTurn`,
//!   `ResolveStartTurnDisposition`, `ResolveRuntimeKeepAlive`, and the
//!   runtime-system-context application authorization) are accepted in
//!   `ShuttingDown` with an explicit no-op or typed
//!   "session archived" terminal (`TurnAdmissionShutdownTerminalResolved`,
//!   `RuntimeSystemContextApplicationResolved { SessionArchived }`) instead
//!   of guard rejections, mirroring the existing
//!   `AuthorizeStartTurnDispatchShuttingDown` -> `Cancelled` precedent.

use meerkat_machine_dsl::machine;

/// Execution kind requested for a start-turn (or absent, meaning a direct
/// prompt-driven turn).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum StartTurnExecutionKind {
    #[default]
    ContentTurn,
    ResumePending,
}

/// Resolved start-turn disposition emitted by the machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum StartTurnDisposition {
    #[default]
    RunContentTurn,
    RunPending,
    NoPendingBoundary,
}

/// Public terminal witness emitted alongside a `NoPendingBoundary` disposition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum StartTurnPublicTerminal {
    #[default]
    NoPendingBoundary,
}

/// Start-turn dispatch authorization verdict from the admission phase.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum StartTurnDispatchAuthorization {
    #[default]
    Authorized,
    Cancelled,
}

/// Pending-continuation disposition. Owned by the canonical
/// [`crate::catalog::dsl::session_document`] machine and consumed here as a
/// typed input mirrored from that machine's emitted decision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum PendingContinuationDisposition {
    RunPending,
    #[default]
    NoPendingBoundary,
}

/// Tri-state runtime keep-alive request carried into the admission machine.
///
/// This is the canonical typed decision the shell previously collapsed into an
/// `Option<bool>`: `Some(true)` -> [`Enable`](RuntimeKeepAliveRequest::Enable),
/// `Some(false)` -> [`Disable`](RuntimeKeepAliveRequest::Disable), and absent
/// -> [`Preserve`](RuntimeKeepAliveRequest::Preserve). `Disable` is distinct
/// from `Preserve`: both resolve `persist_keep_alive = false`, but `Disable`
/// records an explicit operator intent to turn keep-alive off, whereas
/// `Preserve` leaves the prior keep-alive standing unchanged.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum RuntimeKeepAliveRequest {
    Enable,
    Disable,
    #[default]
    Preserve,
}

/// Machine-resolved keep-alive persistence decision.
///
/// The former `persist_keep_alive: bool` effect payload collapsed `Disable`
/// (persist keep-alive = false) and `Preserve` (leave the existing setting
/// unchanged) into the same `false`, so an explicit disable could never reach
/// the durable session metadata. This closed tri-state is the canonical
/// decision the shell mirrors: `PersistEnabled` writes `keep_alive = true`,
/// `PersistDisabled` writes `keep_alive = false`, `PreserveExisting` writes
/// nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum RuntimeKeepAlivePersistenceDecision {
    PersistEnabled,
    PersistDisabled,
    #[default]
    PreserveExisting,
}

/// Authorization verdict for applying runtime-owned system context to the
/// live session (the `ApplyRuntimeSystemContext` /
/// `ApplyRuntimeSystemContextForTurn` session commands).
///
/// `SessionArchived` is the typed benign terminal for applications that
/// arrive while the machine is `ShuttingDown` (archive/discard teardown won
/// the race): the arrival is legitimate, the application is dropped with the
/// archived session, and the caller resolves its waiter with the archived
/// outcome instead of a guard rejection or a dropped reply channel.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum RuntimeSystemContextApplicationAuthorization {
    #[default]
    Authorized,
    SessionArchived,
}

/// Typed terminal disposition for turn-admission work that arrives while the
/// machine is `ShuttingDown` (the session was archived or its live handle
/// was discarded in favor of durable authority). This is the machine-owned
/// answer for legitimately-racing inputs: never a guard rejection, never a
/// generic internal error for a committed teardown.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum TurnAdmissionShutdownTerminal {
    #[default]
    SessionArchived,
}

machine! {
    machine SessionTurnAdmissionMachine {
        version: 1,
        rust: "self" / "catalog::dsl::session_turn_admission",

        state {
            lifecycle_phase: TurnAdmissionPhase,
            interrupt_pending: bool,
            shutdown_pending: bool,
            // D1 drain obligation: minted on every entry into ShuttingDown,
            // closed by the owning shell's ResolvePendingAdmissionDrained
            // feedback after it has flushed the session command queue and
            // resolved every pending admission waiter with a typed terminal.
            admission_drain_pending: bool,
            last_public_terminal: Option<Enum<StartTurnPublicTerminal>>,
        }

        init(Idle) {
            interrupt_pending = false,
            shutdown_pending = false,
            admission_drain_pending = false,
            last_public_terminal = None,
        }

        terminal [ShuttingDown]

        phase TurnAdmissionPhase {
            Idle,
            Admitted,
            Running,
            Completing,
            ShuttingDown,
        }

        input SessionTurnAdmissionInput {
            ProjectTurnAdmission,
            ClaimTurn,
            AbortClaim,
            BeginTurn,
            ResolveTurn,
            FinalizeTurn,
            RequestInterrupt,
            RequestShutdown,
            AuthorizeStartTurnDispatch,
            AuthorizeCancelAfterBoundary,
            ResolveLastStartTurnPublicTerminal,
            AuthorizeRuntimeSystemContextApplication,
            ResolvePendingAdmissionDrained,
            AuthorizeSessionTeardown,
            ResolveRuntimeKeepAlive { keep_alive_request: Enum<RuntimeKeepAliveRequest> },
            ResolveStartTurnDisposition {
                execution_kind_present: bool,
                execution_kind: Enum<StartTurnExecutionKind>,
                prompt_trimmed_text_byte_count: u64,
                prompt_non_text_block_count: u64,
                pending_continuation: Enum<PendingContinuationDisposition>,
            },
        }

        effect SessionTurnAdmissionEffect {
            TurnAdmissionProjected {
                phase: TurnAdmissionPhase,
                interrupt_pending: bool,
                shutdown_pending: bool,
                is_active: bool,
            },
            TurnInterruptRequested { wake: bool },
            StartTurnDispatchResolved { authorization: Enum<StartTurnDispatchAuthorization> },
            CancelAfterBoundaryAuthorized,
            StartTurnDispositionResolved { disposition: Enum<StartTurnDisposition> },
            StartTurnPublicTerminalResolved { terminal: Enum<StartTurnPublicTerminal> },
            RuntimeKeepAliveResolved { decision: Enum<RuntimeKeepAlivePersistenceDecision> },
            PendingAdmissionDrainRequested,
            RuntimeSystemContextApplicationResolved {
                authorization: Enum<RuntimeSystemContextApplicationAuthorization>,
            },
            TurnAdmissionShutdownTerminalResolved { terminal: Enum<TurnAdmissionShutdownTerminal> },
            SessionTeardownAuthorized,
        }

        helper is_active_phase(phase: TurnAdmissionPhase) -> bool {
            phase == Phase::Admitted || phase == Phase::Running || phase == Phase::Completing
        }

        helper prompt_has_content(prompt_trimmed_text_byte_count: u64, prompt_non_text_block_count: u64) -> bool {
            prompt_trimmed_text_byte_count > 0 || prompt_non_text_block_count > 0
        }

        invariant shutdown_phase_is_not_active {
            self.lifecycle_phase != Phase::ShuttingDown || is_active_phase(self.lifecycle_phase) == false
        }

        // The drain obligation only exists while shutting down: it is minted
        // exclusively on entries into ShuttingDown and ShuttingDown has no
        // exits, so a pending obligation outside ShuttingDown is
        // unrepresentable.
        invariant drain_obligation_only_while_shutting_down {
            self.admission_drain_pending == false || self.lifecycle_phase == Phase::ShuttingDown
        }

        disposition TurnAdmissionProjected => local seam NoOwnerRealization,
        disposition TurnInterruptRequested => local seam NoOwnerRealization,
        disposition StartTurnDispatchResolved => local seam NoOwnerRealization,
        disposition CancelAfterBoundaryAuthorized => local seam NoOwnerRealization,
        disposition StartTurnDispositionResolved => local seam NoOwnerRealization,
        disposition StartTurnPublicTerminalResolved => local seam NoOwnerRealization,
        disposition RuntimeKeepAliveResolved => local seam NoOwnerRealization,
        disposition PendingAdmissionDrainRequested => local seam NoOwnerRealization,
        disposition RuntimeSystemContextApplicationResolved => local seam NoOwnerRealization,
        disposition TurnAdmissionShutdownTerminalResolved => local seam NoOwnerRealization,
        disposition SessionTeardownAuthorized => local seam NoOwnerRealization,

        transition ProjectTurnAdmission {
            per_phase [Idle, Admitted, Running, Completing, ShuttingDown]
            on input ProjectTurnAdmission
            update {}
            to Idle
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition ClaimTurn {
            on input ClaimTurn
            guard { self.lifecycle_phase == Phase::Idle }
            update {
                self.interrupt_pending = false;
                self.shutdown_pending = false;
                self.last_public_terminal = None;
            }
            to Admitted
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition AbortClaim {
            on input AbortClaim
            guard { self.lifecycle_phase == Phase::Admitted }
            update {
                self.interrupt_pending = false;
                self.shutdown_pending = false;
            }
            to Idle
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        // D2a: a claim attempt that lost the race against archive/discard
        // teardown gets the typed "session archived" terminal instead of a
        // guard rejection. No admission is granted; the machine stays in
        // ShuttingDown. (Deliberately no projection emit: pre-cutover mirrors
        // that only understand projections keep failing closed.)
        transition ClaimTurnShuttingDown {
            on input ClaimTurn
            guard { self.lifecycle_phase == Phase::ShuttingDown }
            update {}
            to ShuttingDown
            emit TurnAdmissionShutdownTerminalResolved {
                terminal: TurnAdmissionShutdownTerminal::SessionArchived,
            }
        }

        // D2a: an abort for a claim that teardown already discarded
        // (RequestShutdownImmediateAdmitted reset the claim state) is an
        // explicit no-op, not an error to be swallowed by the shell.
        transition AbortClaimShuttingDown {
            on input AbortClaim
            guard { self.lifecycle_phase == Phase::ShuttingDown }
            update {}
            to ShuttingDown
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition BeginTurn {
            on input BeginTurn
            guard { self.lifecycle_phase == Phase::Admitted }
            update {}
            to Running
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        // D2a: archive/discard committed RequestShutdownImmediateAdmitted
        // between the admitted preflight and the begin step. The run must not
        // start; the waiter resolves with the typed "session archived"
        // terminal instead of an illegal-transition internal error.
        // (Deliberately no projection emit: the run may only begin on a
        // projected Running transition.)
        transition BeginTurnShuttingDown {
            on input BeginTurn
            guard { self.lifecycle_phase == Phase::ShuttingDown }
            update {}
            to ShuttingDown
            emit TurnAdmissionShutdownTerminalResolved {
                terminal: TurnAdmissionShutdownTerminal::SessionArchived,
            }
        }

        transition ResolveTurn {
            on input ResolveTurn
            guard { self.lifecycle_phase == Phase::Running }
            update {}
            to Completing
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition FinalizeTurnToShutdown {
            on input FinalizeTurn
            guard { self.lifecycle_phase == Phase::Completing && self.shutdown_pending }
            update {
                self.interrupt_pending = false;
                self.admission_drain_pending = true;
            }
            to ShuttingDown
            emit PendingAdmissionDrainRequested
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition FinalizeTurnToIdle {
            on input FinalizeTurn
            guard { self.lifecycle_phase == Phase::Completing && self.shutdown_pending == false }
            update {
                self.interrupt_pending = false;
                self.shutdown_pending = false;
            }
            to Idle
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition RequestInterruptAdmittedFirst {
            on input RequestInterrupt
            guard { self.lifecycle_phase == Phase::Admitted && self.interrupt_pending == false }
            update {
                self.interrupt_pending = true;
            }
            to Admitted
            emit TurnInterruptRequested { wake: true }
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition RequestInterruptAdmittedDuplicate {
            on input RequestInterrupt
            guard { self.lifecycle_phase == Phase::Admitted && self.interrupt_pending }
            update {}
            to Admitted
            emit TurnInterruptRequested { wake: false }
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition RequestInterruptRunningFirst {
            on input RequestInterrupt
            guard { self.lifecycle_phase == Phase::Running && self.interrupt_pending == false }
            update {
                self.interrupt_pending = true;
            }
            to Running
            emit TurnInterruptRequested { wake: true }
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition RequestInterruptRunningDuplicate {
            on input RequestInterrupt
            guard { self.lifecycle_phase == Phase::Running && self.interrupt_pending }
            update {}
            to Running
            emit TurnInterruptRequested { wake: false }
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition RequestShutdownImmediateIdle {
            on input RequestShutdown
            guard { self.lifecycle_phase == Phase::Idle }
            update {
                self.interrupt_pending = false;
                self.shutdown_pending = true;
                self.admission_drain_pending = true;
            }
            to ShuttingDown
            emit PendingAdmissionDrainRequested
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition RequestShutdownImmediateAdmitted {
            on input RequestShutdown
            guard { self.lifecycle_phase == Phase::Admitted }
            update {
                self.interrupt_pending = false;
                self.shutdown_pending = true;
                self.admission_drain_pending = true;
            }
            to ShuttingDown
            emit PendingAdmissionDrainRequested
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition RequestShutdownDeferredRunning {
            on input RequestShutdown
            guard { self.lifecycle_phase == Phase::Running }
            update {
                self.shutdown_pending = true;
            }
            to Running
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition RequestShutdownDeferredCompleting {
            on input RequestShutdown
            guard { self.lifecycle_phase == Phase::Completing }
            update {
                self.shutdown_pending = true;
            }
            to Completing
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        transition RequestShutdownAlreadyShuttingDown {
            on input RequestShutdown
            guard { self.lifecycle_phase == Phase::ShuttingDown }
            update {}
            to ShuttingDown
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        // D1 drain feedback: the owning shell flushed the session command
        // queue and resolved every pending admission waiter with a typed
        // terminal outcome; the obligation closes. A second feedback without
        // an open obligation is a shell bug and stays rejected.
        transition ResolvePendingAdmissionDrained {
            on input ResolvePendingAdmissionDrained
            guard { self.lifecycle_phase == Phase::ShuttingDown && self.admission_drain_pending }
            update {
                self.admission_drain_pending = false;
            }
            to ShuttingDown
            emit TurnAdmissionProjected {
                phase: self.lifecycle_phase,
                interrupt_pending: self.interrupt_pending,
                shutdown_pending: self.shutdown_pending,
                is_active: is_active_phase(self.lifecycle_phase),
            }
        }

        // D1 final teardown gate: dropping the session task / live state is
        // only authorized once the drain obligation has closed, so "torn
        // down" is defined as "pending admission work resolved".
        transition AuthorizeSessionTeardown {
            on input AuthorizeSessionTeardown
            guard {
                self.lifecycle_phase == Phase::ShuttingDown
                && self.admission_drain_pending == false
            }
            update {}
            to ShuttingDown
            emit SessionTeardownAuthorized
        }

        transition AuthorizeCancelAfterBoundaryAdmitted {
            on input AuthorizeCancelAfterBoundary
            guard { self.lifecycle_phase == Phase::Admitted }
            update {}
            to Admitted
            emit CancelAfterBoundaryAuthorized
        }

        transition AuthorizeStartTurnDispatchAdmitted {
            on input AuthorizeStartTurnDispatch
            guard { self.lifecycle_phase == Phase::Admitted }
            update {}
            to Admitted
            emit StartTurnDispatchResolved { authorization: StartTurnDispatchAuthorization::Authorized }
        }

        transition AuthorizeStartTurnDispatchShuttingDown {
            on input AuthorizeStartTurnDispatch
            guard { self.lifecycle_phase == Phase::ShuttingDown }
            update {}
            to ShuttingDown
            emit StartTurnDispatchResolved { authorization: StartTurnDispatchAuthorization::Cancelled }
        }

        // Machine-owned legality decision for applying runtime-owned system
        // context to the live session (the ApplyRuntimeSystemContext /
        // ApplyRuntimeSystemContextForTurn session commands). The shell fires
        // this before touching the live agent; it decides nothing itself.
        transition AuthorizeRuntimeSystemContextApplicationActive {
            per_phase [Idle, Admitted, Running, Completing]
            on input AuthorizeRuntimeSystemContextApplication
            update {}
            to Idle
            emit RuntimeSystemContextApplicationResolved {
                authorization: RuntimeSystemContextApplicationAuthorization::Authorized,
            }
        }

        // D2a: a context application that lost the race against
        // archive/discard teardown resolves with the typed SessionArchived
        // verdict — the appends are dropped with the archived session and the
        // caller's waiter resolves benignly instead of erroring or hanging.
        transition AuthorizeRuntimeSystemContextApplicationShuttingDown {
            on input AuthorizeRuntimeSystemContextApplication
            guard { self.lifecycle_phase == Phase::ShuttingDown }
            update {}
            to ShuttingDown
            emit RuntimeSystemContextApplicationResolved {
                authorization: RuntimeSystemContextApplicationAuthorization::SessionArchived,
            }
        }

        transition AuthorizeCancelAfterBoundaryRunning {
            on input AuthorizeCancelAfterBoundary
            guard { self.lifecycle_phase == Phase::Running }
            update {}
            to Running
            emit CancelAfterBoundaryAuthorized
        }

        transition ResolveDispositionContentTurn {
            on input ResolveStartTurnDisposition {
                execution_kind_present,
                execution_kind,
                prompt_trimmed_text_byte_count,
                prompt_non_text_block_count,
                pending_continuation
            }
            guard {
                self.lifecycle_phase == Phase::Admitted
                && execution_kind_present
                && execution_kind == StartTurnExecutionKind::ContentTurn
            }
            update {
                self.last_public_terminal = None;
            }
            to Admitted
            emit StartTurnDispositionResolved { disposition: StartTurnDisposition::RunContentTurn }
        }

        transition ResolveDispositionResumePendingWithBoundary {
            on input ResolveStartTurnDisposition {
                execution_kind_present,
                execution_kind,
                prompt_trimmed_text_byte_count,
                prompt_non_text_block_count,
                pending_continuation
            }
            guard {
                self.lifecycle_phase == Phase::Admitted
                && execution_kind_present
                && execution_kind == StartTurnExecutionKind::ResumePending
                && pending_continuation == PendingContinuationDisposition::RunPending
            }
            update {
                self.last_public_terminal = None;
            }
            to Admitted
            emit StartTurnDispositionResolved { disposition: StartTurnDisposition::RunPending }
        }

        transition ResolveDispositionResumePendingWithoutBoundary {
            on input ResolveStartTurnDisposition {
                execution_kind_present,
                execution_kind,
                prompt_trimmed_text_byte_count,
                prompt_non_text_block_count,
                pending_continuation
            }
            guard {
                self.lifecycle_phase == Phase::Admitted
                && execution_kind_present
                && execution_kind == StartTurnExecutionKind::ResumePending
                && pending_continuation == PendingContinuationDisposition::NoPendingBoundary
            }
            update {
                self.last_public_terminal = Some(StartTurnPublicTerminal::NoPendingBoundary);
            }
            to Admitted
            emit StartTurnDispositionResolved { disposition: StartTurnDisposition::NoPendingBoundary }
            emit StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary }
        }

        transition ResolveDispositionDirectPrompt {
            on input ResolveStartTurnDisposition {
                execution_kind_present,
                execution_kind,
                prompt_trimmed_text_byte_count,
                prompt_non_text_block_count,
                pending_continuation
            }
            guard {
                self.lifecycle_phase == Phase::Admitted
                && execution_kind_present == false
                && prompt_has_content(prompt_trimmed_text_byte_count, prompt_non_text_block_count)
            }
            update {
                self.last_public_terminal = None;
            }
            to Admitted
            emit StartTurnDispositionResolved { disposition: StartTurnDisposition::RunContentTurn }
        }

        transition ResolveDispositionDirectPending {
            on input ResolveStartTurnDisposition {
                execution_kind_present,
                execution_kind,
                prompt_trimmed_text_byte_count,
                prompt_non_text_block_count,
                pending_continuation
            }
            guard {
                self.lifecycle_phase == Phase::Admitted
                && execution_kind_present == false
                && prompt_has_content(prompt_trimmed_text_byte_count, prompt_non_text_block_count) == false
                && pending_continuation == PendingContinuationDisposition::RunPending
            }
            update {
                self.last_public_terminal = None;
            }
            to Admitted
            emit StartTurnDispositionResolved { disposition: StartTurnDisposition::RunPending }
        }

        transition ResolveDispositionDirectNoPending {
            on input ResolveStartTurnDisposition {
                execution_kind_present,
                execution_kind,
                prompt_trimmed_text_byte_count,
                prompt_non_text_block_count,
                pending_continuation
            }
            guard {
                self.lifecycle_phase == Phase::Admitted
                && execution_kind_present == false
                && prompt_has_content(prompt_trimmed_text_byte_count, prompt_non_text_block_count) == false
                && pending_continuation == PendingContinuationDisposition::NoPendingBoundary
            }
            update {
                self.last_public_terminal = Some(StartTurnPublicTerminal::NoPendingBoundary);
            }
            to Admitted
            emit StartTurnDispositionResolved { disposition: StartTurnDisposition::NoPendingBoundary }
            emit StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary }
        }

        // D2a: a start-turn disposition request that lost the race against
        // archive/discard teardown (RequestShutdownImmediateAdmitted landed
        // between the dispatch authorization and this resolution) gets the
        // typed "session archived" terminal instead of a guard rejection.
        // (Deliberately no StartTurnDispositionResolved emit: no runnable
        // disposition exists for a shutting-down session.)
        transition ResolveStartTurnDispositionShuttingDown {
            on input ResolveStartTurnDisposition {
                execution_kind_present,
                execution_kind,
                prompt_trimmed_text_byte_count,
                prompt_non_text_block_count,
                pending_continuation
            }
            guard { self.lifecycle_phase == Phase::ShuttingDown }
            update {}
            to ShuttingDown
            emit TurnAdmissionShutdownTerminalResolved {
                terminal: TurnAdmissionShutdownTerminal::SessionArchived,
            }
        }

        transition ResolveRuntimeKeepAliveEnable {
            on input ResolveRuntimeKeepAlive { keep_alive_request }
            guard {
                self.lifecycle_phase == Phase::Admitted
                && keep_alive_request == RuntimeKeepAliveRequest::Enable
            }
            update {}
            to Admitted
            emit RuntimeKeepAliveResolved { decision: RuntimeKeepAlivePersistenceDecision::PersistEnabled }
        }

        transition ResolveRuntimeKeepAliveDisable {
            on input ResolveRuntimeKeepAlive { keep_alive_request }
            guard {
                self.lifecycle_phase == Phase::Admitted
                && keep_alive_request == RuntimeKeepAliveRequest::Disable
            }
            update {}
            to Admitted
            emit RuntimeKeepAliveResolved { decision: RuntimeKeepAlivePersistenceDecision::PersistDisabled }
        }

        transition ResolveRuntimeKeepAlivePreserve {
            on input ResolveRuntimeKeepAlive { keep_alive_request }
            guard {
                self.lifecycle_phase == Phase::Admitted
                && keep_alive_request == RuntimeKeepAliveRequest::Preserve
            }
            update {}
            to Admitted
            emit RuntimeKeepAliveResolved { decision: RuntimeKeepAlivePersistenceDecision::PreserveExisting }
        }

        // D2a: a keep-alive resolution that lost the race against
        // archive/discard teardown gets the typed "session archived" terminal
        // instead of a guard rejection. No persistence decision is emitted —
        // nothing may be persisted for a session whose teardown committed.
        transition ResolveRuntimeKeepAliveShuttingDown {
            on input ResolveRuntimeKeepAlive { keep_alive_request }
            guard { self.lifecycle_phase == Phase::ShuttingDown }
            update {}
            to ShuttingDown
            emit TurnAdmissionShutdownTerminalResolved {
                terminal: TurnAdmissionShutdownTerminal::SessionArchived,
            }
        }

        transition ResolveLastStartTurnPublicTerminalNoPending {
            per_phase [Idle, Admitted, Running, Completing, ShuttingDown]
            on input ResolveLastStartTurnPublicTerminal
            guard { self.last_public_terminal == Some(StartTurnPublicTerminal::NoPendingBoundary) }
            update {}
            to Idle
            emit StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary }
        }
    }
}