// @generated — session turn admission authority for `SessionTurnAdmissionMachine`
// Generated by `xtask protocol-codegen` from `SessionTurnAdmissionMachine`.
#![allow(
    dead_code,
    unused_parens,
    unused_variables,
    clippy::bool_comparison,
    clippy::field_reassign_with_default,
    clippy::nonminimal_bool,
    clippy::partialeq_to_none,
    clippy::redundant_field_names,
    clippy::too_many_arguments
)]

use std::fmt;

pub use meerkat_core::session_document::PendingContinuationDisposition;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum StartTurnExecutionKind {
    #[default]
    ContentTurn,
    ResumePending,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum StartTurnDisposition {
    #[default]
    RunContentTurn,
    RunPending,
    NoPendingBoundary,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum StartTurnPublicTerminal {
    #[default]
    NoPendingBoundary,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum StartTurnDispatchAuthorization {
    #[default]
    Authorized,
    Cancelled,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum RuntimeKeepAliveRequest {
    Enable,
    Disable,
    #[default]
    Preserve,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum RuntimeKeepAlivePersistenceDecision {
    PersistEnabled,
    PersistDisabled,
    #[default]
    PreserveExisting,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum RuntimeSystemContextApplicationAuthorization {
    #[default]
    Authorized,
    SessionArchived,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum TurnAdmissionShutdownTerminal {
    #[default]
    SessionArchived,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionTurnAdmissionInput {
    ProjectTurnAdmission,
    ClaimTurn,
    AbortClaim,
    BeginTurn,
    ResolveTurn,
    FinalizeTurn,
    RequestInterrupt,
    RequestShutdown,
    AuthorizeStartTurnDispatch,
    AuthorizeCancelAfterBoundary,
    ResolveLastStartTurnPublicTerminal,
    AuthorizeRuntimeSystemContextApplication,
    ResolvePendingAdmissionDrained,
    AuthorizeSessionTeardown,
    ResolveRuntimeKeepAlive {
        keep_alive_request: RuntimeKeepAliveRequest,
    },
    ResolveStartTurnDisposition {
        execution_kind_present: bool,
        execution_kind: StartTurnExecutionKind,
        prompt_trimmed_text_byte_count: u64,
        prompt_non_text_block_count: u64,
        pending_continuation: PendingContinuationDisposition,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionTurnAdmissionEffect {
    TurnAdmissionProjected {
        phase: TurnAdmissionPhase,
        interrupt_pending: bool,
        shutdown_pending: bool,
        is_active: bool,
    },
    TurnInterruptRequested {
        wake: bool,
    },
    StartTurnDispatchResolved {
        authorization: StartTurnDispatchAuthorization,
    },
    CancelAfterBoundaryAuthorized,
    StartTurnDispositionResolved {
        disposition: StartTurnDisposition,
    },
    StartTurnPublicTerminalResolved {
        terminal: StartTurnPublicTerminal,
    },
    RuntimeKeepAliveResolved {
        decision: RuntimeKeepAlivePersistenceDecision,
    },
    PendingAdmissionDrainRequested,
    RuntimeSystemContextApplicationResolved {
        authorization: RuntimeSystemContextApplicationAuthorization,
    },
    TurnAdmissionShutdownTerminalResolved {
        terminal: TurnAdmissionShutdownTerminal,
    },
    SessionTeardownAuthorized,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionTurnAdmissionError {
    op: &'static str,
}

impl SessionTurnAdmissionError {
    #[must_use]
    pub fn new(op: &'static str) -> Self {
        Self { op }
    }
    #[must_use]
    pub fn op(&self) -> &'static str {
        self.op
    }
}

impl fmt::Display for SessionTurnAdmissionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "generated session turn admission authority rejected {}",
            self.op
        )
    }
}

impl std::error::Error for SessionTurnAdmissionError {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum TurnAdmissionPhase {
    #[default]
    Idle,
    Admitted,
    Running,
    Completing,
    ShuttingDown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SessionTurnAdmissionMachineState {
    lifecycle_phase: TurnAdmissionPhase,
    pub interrupt_pending: bool,
    pub shutdown_pending: bool,
    pub admission_drain_pending: bool,
    pub last_public_terminal: Option<StartTurnPublicTerminal>,
}

impl SessionTurnAdmissionMachineState {
    #[must_use]
    pub fn phase(&self) -> TurnAdmissionPhase {
        self.lifecycle_phase
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SessionTurnAdmissionTransition {
    ProjectTurnAdmissionIdle,
    ProjectTurnAdmissionAdmitted,
    ProjectTurnAdmissionRunning,
    ProjectTurnAdmissionCompleting,
    ProjectTurnAdmissionShuttingDown,
    ClaimTurn,
    AbortClaim,
    ClaimTurnShuttingDown,
    AbortClaimShuttingDown,
    BeginTurn,
    BeginTurnShuttingDown,
    ResolveTurn,
    FinalizeTurnToShutdown,
    FinalizeTurnToIdle,
    RequestInterruptAdmittedFirst,
    RequestInterruptAdmittedDuplicate,
    RequestInterruptRunningFirst,
    RequestInterruptRunningDuplicate,
    RequestShutdownImmediateIdle,
    RequestShutdownImmediateAdmitted,
    RequestShutdownDeferredRunning,
    RequestShutdownDeferredCompleting,
    RequestShutdownAlreadyShuttingDown,
    ResolvePendingAdmissionDrained,
    AuthorizeSessionTeardown,
    AuthorizeCancelAfterBoundaryAdmitted,
    AuthorizeStartTurnDispatchAdmitted,
    AuthorizeStartTurnDispatchShuttingDown,
    AuthorizeRuntimeSystemContextApplicationActiveIdle,
    AuthorizeRuntimeSystemContextApplicationActiveAdmitted,
    AuthorizeRuntimeSystemContextApplicationActiveRunning,
    AuthorizeRuntimeSystemContextApplicationActiveCompleting,
    AuthorizeRuntimeSystemContextApplicationShuttingDown,
    AuthorizeCancelAfterBoundaryRunning,
    ResolveDispositionContentTurn,
    ResolveDispositionResumePendingWithBoundary,
    ResolveDispositionResumePendingWithoutBoundary,
    ResolveDispositionDirectPrompt,
    ResolveDispositionDirectPending,
    ResolveDispositionDirectNoPending,
    ResolveStartTurnDispositionShuttingDown,
    ResolveRuntimeKeepAliveEnable,
    ResolveRuntimeKeepAliveDisable,
    ResolveRuntimeKeepAlivePreserve,
    ResolveRuntimeKeepAliveShuttingDown,
    ResolveLastStartTurnPublicTerminalNoPendingIdle,
    ResolveLastStartTurnPublicTerminalNoPendingAdmitted,
    ResolveLastStartTurnPublicTerminalNoPendingRunning,
    ResolveLastStartTurnPublicTerminalNoPendingCompleting,
    ResolveLastStartTurnPublicTerminalNoPendingShuttingDown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SessionTurnAdmissionMachineAuthority {
    state: SessionTurnAdmissionMachineState,
}

impl SessionTurnAdmissionMachineAuthority {
    #[must_use]
    pub fn new() -> Self {
        let mut state = SessionTurnAdmissionMachineState::default();
        state.lifecycle_phase = TurnAdmissionPhase::Idle;
        state.interrupt_pending = false;
        state.shutdown_pending = false;
        state.admission_drain_pending = false;
        state.last_public_terminal = None;
        Self { state }
    }

    #[must_use]
    pub fn state(&self) -> &SessionTurnAdmissionMachineState {
        &self.state
    }

    fn single_transition(
        matches: Vec<SessionTurnAdmissionTransition>,
        op: &'static str,
    ) -> Result<SessionTurnAdmissionTransition, SessionTurnAdmissionError> {
        let mut matches = matches.into_iter();
        let Some(first) = matches.next() else {
            return Err(SessionTurnAdmissionError { op });
        };
        if matches.next().is_some() {
            return Err(SessionTurnAdmissionError { op });
        }
        Ok(first)
    }

    fn apply_input(
        &mut self,
        input: SessionTurnAdmissionInput,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        match input {
            SessionTurnAdmissionInput::ProjectTurnAdmission => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Idle) {
                    matches.push(SessionTurnAdmissionTransition::ProjectTurnAdmissionIdle);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted) {
                    matches.push(SessionTurnAdmissionTransition::ProjectTurnAdmissionAdmitted);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Running) {
                    matches.push(SessionTurnAdmissionTransition::ProjectTurnAdmissionRunning);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Completing) {
                    matches.push(SessionTurnAdmissionTransition::ProjectTurnAdmissionCompleting);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown) {
                    matches.push(SessionTurnAdmissionTransition::ProjectTurnAdmissionShuttingDown);
                }
                let transition = Self::single_transition(matches, "ProjectTurnAdmission")?;
                match transition {
                    SessionTurnAdmissionTransition::ProjectTurnAdmissionIdle => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Idle;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    SessionTurnAdmissionTransition::ProjectTurnAdmissionAdmitted => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    SessionTurnAdmissionTransition::ProjectTurnAdmissionRunning => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Running;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    SessionTurnAdmissionTransition::ProjectTurnAdmissionCompleting => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Completing;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    SessionTurnAdmissionTransition::ProjectTurnAdmissionShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "ProjectTurnAdmission_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::ClaimTurn => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Idle) {
                    matches.push(SessionTurnAdmissionTransition::ClaimTurn);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown) {
                    matches.push(SessionTurnAdmissionTransition::ClaimTurnShuttingDown);
                }
                let transition = Self::single_transition(matches, "ClaimTurn")?;
                match transition {
                    SessionTurnAdmissionTransition::ClaimTurn => {
                        self.state.interrupt_pending = false;
                        self.state.shutdown_pending = false;
                        self.state.last_public_terminal = None;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    SessionTurnAdmissionTransition::ClaimTurnShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::TurnAdmissionShutdownTerminalResolved {
                                terminal: TurnAdmissionShutdownTerminal::SessionArchived,
                            },
                        ])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "ClaimTurn_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::AbortClaim => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted) {
                    matches.push(SessionTurnAdmissionTransition::AbortClaim);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown) {
                    matches.push(SessionTurnAdmissionTransition::AbortClaimShuttingDown);
                }
                let transition = Self::single_transition(matches, "AbortClaim")?;
                match transition {
                    SessionTurnAdmissionTransition::AbortClaim => {
                        self.state.interrupt_pending = false;
                        self.state.shutdown_pending = false;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Idle;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    SessionTurnAdmissionTransition::AbortClaimShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "AbortClaim_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::BeginTurn => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted) {
                    matches.push(SessionTurnAdmissionTransition::BeginTurn);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown) {
                    matches.push(SessionTurnAdmissionTransition::BeginTurnShuttingDown);
                }
                let transition = Self::single_transition(matches, "BeginTurn")?;
                match transition {
                    SessionTurnAdmissionTransition::BeginTurn => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Running;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    SessionTurnAdmissionTransition::BeginTurnShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::TurnAdmissionShutdownTerminalResolved {
                                terminal: TurnAdmissionShutdownTerminal::SessionArchived,
                            },
                        ])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "BeginTurn_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::ResolveTurn => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Running) {
                    matches.push(SessionTurnAdmissionTransition::ResolveTurn);
                }
                let transition = Self::single_transition(matches, "ResolveTurn")?;
                match transition {
                    SessionTurnAdmissionTransition::ResolveTurn => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Completing;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "ResolveTurn_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::FinalizeTurn => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Completing)
                    && (self.state.shutdown_pending)
                {
                    matches.push(SessionTurnAdmissionTransition::FinalizeTurnToShutdown);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Completing)
                    && (self.state.shutdown_pending == false)
                {
                    matches.push(SessionTurnAdmissionTransition::FinalizeTurnToIdle);
                }
                let transition = Self::single_transition(matches, "FinalizeTurn")?;
                match transition {
                    SessionTurnAdmissionTransition::FinalizeTurnToShutdown => {
                        self.state.interrupt_pending = false;
                        self.state.admission_drain_pending = true;
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::PendingAdmissionDrainRequested,
                            SessionTurnAdmissionEffect::TurnAdmissionProjected {
                                phase: self.state.lifecycle_phase,
                                interrupt_pending: self.state.interrupt_pending,
                                shutdown_pending: self.state.shutdown_pending,
                                is_active: is_active_phase(self.state.lifecycle_phase),
                            },
                        ])
                    }
                    SessionTurnAdmissionTransition::FinalizeTurnToIdle => {
                        self.state.interrupt_pending = false;
                        self.state.shutdown_pending = false;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Idle;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "FinalizeTurn_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::RequestInterrupt => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && (self.state.interrupt_pending == false)
                {
                    matches.push(SessionTurnAdmissionTransition::RequestInterruptAdmittedFirst);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && (self.state.interrupt_pending)
                {
                    matches.push(SessionTurnAdmissionTransition::RequestInterruptAdmittedDuplicate);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Running)
                    && (self.state.interrupt_pending == false)
                {
                    matches.push(SessionTurnAdmissionTransition::RequestInterruptRunningFirst);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Running)
                    && (self.state.interrupt_pending)
                {
                    matches.push(SessionTurnAdmissionTransition::RequestInterruptRunningDuplicate);
                }
                let transition = Self::single_transition(matches, "RequestInterrupt")?;
                match transition {
                    SessionTurnAdmissionTransition::RequestInterruptAdmittedFirst => {
                        self.state.interrupt_pending = true;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::TurnInterruptRequested { wake: true },
                            SessionTurnAdmissionEffect::TurnAdmissionProjected {
                                phase: self.state.lifecycle_phase,
                                interrupt_pending: self.state.interrupt_pending,
                                shutdown_pending: self.state.shutdown_pending,
                                is_active: is_active_phase(self.state.lifecycle_phase),
                            },
                        ])
                    }
                    SessionTurnAdmissionTransition::RequestInterruptAdmittedDuplicate => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::TurnInterruptRequested { wake: false },
                            SessionTurnAdmissionEffect::TurnAdmissionProjected {
                                phase: self.state.lifecycle_phase,
                                interrupt_pending: self.state.interrupt_pending,
                                shutdown_pending: self.state.shutdown_pending,
                                is_active: is_active_phase(self.state.lifecycle_phase),
                            },
                        ])
                    }
                    SessionTurnAdmissionTransition::RequestInterruptRunningFirst => {
                        self.state.interrupt_pending = true;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Running;
                        Ok(vec![
                            SessionTurnAdmissionEffect::TurnInterruptRequested { wake: true },
                            SessionTurnAdmissionEffect::TurnAdmissionProjected {
                                phase: self.state.lifecycle_phase,
                                interrupt_pending: self.state.interrupt_pending,
                                shutdown_pending: self.state.shutdown_pending,
                                is_active: is_active_phase(self.state.lifecycle_phase),
                            },
                        ])
                    }
                    SessionTurnAdmissionTransition::RequestInterruptRunningDuplicate => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Running;
                        Ok(vec![
                            SessionTurnAdmissionEffect::TurnInterruptRequested { wake: false },
                            SessionTurnAdmissionEffect::TurnAdmissionProjected {
                                phase: self.state.lifecycle_phase,
                                interrupt_pending: self.state.interrupt_pending,
                                shutdown_pending: self.state.shutdown_pending,
                                is_active: is_active_phase(self.state.lifecycle_phase),
                            },
                        ])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "RequestInterrupt_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::RequestShutdown => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Idle) {
                    matches.push(SessionTurnAdmissionTransition::RequestShutdownImmediateIdle);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted) {
                    matches.push(SessionTurnAdmissionTransition::RequestShutdownImmediateAdmitted);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Running) {
                    matches.push(SessionTurnAdmissionTransition::RequestShutdownDeferredRunning);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Completing) {
                    matches.push(SessionTurnAdmissionTransition::RequestShutdownDeferredCompleting);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown) {
                    matches
                        .push(SessionTurnAdmissionTransition::RequestShutdownAlreadyShuttingDown);
                }
                let transition = Self::single_transition(matches, "RequestShutdown")?;
                match transition {
                    SessionTurnAdmissionTransition::RequestShutdownImmediateIdle => {
                        self.state.interrupt_pending = false;
                        self.state.shutdown_pending = true;
                        self.state.admission_drain_pending = true;
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::PendingAdmissionDrainRequested,
                            SessionTurnAdmissionEffect::TurnAdmissionProjected {
                                phase: self.state.lifecycle_phase,
                                interrupt_pending: self.state.interrupt_pending,
                                shutdown_pending: self.state.shutdown_pending,
                                is_active: is_active_phase(self.state.lifecycle_phase),
                            },
                        ])
                    }
                    SessionTurnAdmissionTransition::RequestShutdownImmediateAdmitted => {
                        self.state.interrupt_pending = false;
                        self.state.shutdown_pending = true;
                        self.state.admission_drain_pending = true;
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::PendingAdmissionDrainRequested,
                            SessionTurnAdmissionEffect::TurnAdmissionProjected {
                                phase: self.state.lifecycle_phase,
                                interrupt_pending: self.state.interrupt_pending,
                                shutdown_pending: self.state.shutdown_pending,
                                is_active: is_active_phase(self.state.lifecycle_phase),
                            },
                        ])
                    }
                    SessionTurnAdmissionTransition::RequestShutdownDeferredRunning => {
                        self.state.shutdown_pending = true;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Running;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    SessionTurnAdmissionTransition::RequestShutdownDeferredCompleting => {
                        self.state.shutdown_pending = true;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Completing;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    SessionTurnAdmissionTransition::RequestShutdownAlreadyShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "RequestShutdown_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::AuthorizeStartTurnDispatch => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted) {
                    matches
                        .push(SessionTurnAdmissionTransition::AuthorizeStartTurnDispatchAdmitted);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown) {
                    matches.push(
                        SessionTurnAdmissionTransition::AuthorizeStartTurnDispatchShuttingDown,
                    );
                }
                let transition = Self::single_transition(matches, "AuthorizeStartTurnDispatch")?;
                match transition {
                    SessionTurnAdmissionTransition::AuthorizeStartTurnDispatchAdmitted => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnDispatchResolved {
                                authorization: StartTurnDispatchAuthorization::Authorized,
                            },
                        ])
                    }
                    SessionTurnAdmissionTransition::AuthorizeStartTurnDispatchShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnDispatchResolved {
                                authorization: StartTurnDispatchAuthorization::Cancelled,
                            },
                        ])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "AuthorizeStartTurnDispatch_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::AuthorizeCancelAfterBoundary => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted) {
                    matches
                        .push(SessionTurnAdmissionTransition::AuthorizeCancelAfterBoundaryAdmitted);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Running) {
                    matches
                        .push(SessionTurnAdmissionTransition::AuthorizeCancelAfterBoundaryRunning);
                }
                let transition = Self::single_transition(matches, "AuthorizeCancelAfterBoundary")?;
                match transition {
                    SessionTurnAdmissionTransition::AuthorizeCancelAfterBoundaryAdmitted => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::CancelAfterBoundaryAuthorized,
                        ])
                    }
                    SessionTurnAdmissionTransition::AuthorizeCancelAfterBoundaryRunning => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Running;
                        Ok(vec![
                            SessionTurnAdmissionEffect::CancelAfterBoundaryAuthorized,
                        ])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "AuthorizeCancelAfterBoundary_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::ResolveLastStartTurnPublicTerminal => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Idle)
                    && (self.state.last_public_terminal
                        == Some(StartTurnPublicTerminal::NoPendingBoundary))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingIdle);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && (self.state.last_public_terminal
                        == Some(StartTurnPublicTerminal::NoPendingBoundary))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingAdmitted);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Running)
                    && (self.state.last_public_terminal
                        == Some(StartTurnPublicTerminal::NoPendingBoundary))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingRunning);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Completing)
                    && (self.state.last_public_terminal
                        == Some(StartTurnPublicTerminal::NoPendingBoundary))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingCompleting);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown)
                    && (self.state.last_public_terminal
                        == Some(StartTurnPublicTerminal::NoPendingBoundary))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingShuttingDown);
                }
                let transition =
                    Self::single_transition(matches, "ResolveLastStartTurnPublicTerminal")?;
                match transition {
                    SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingIdle => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Idle;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingAdmitted => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingRunning => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Running;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingCompleting => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Completing;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveLastStartTurnPublicTerminalNoPendingShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary, },
                        ])
                    }
                    #[allow(unreachable_patterns)] _ => Err(SessionTurnAdmissionError { op: "ResolveLastStartTurnPublicTerminal_transition" }),
                }
            }
            SessionTurnAdmissionInput::AuthorizeRuntimeSystemContextApplication => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Idle) {
                    matches.push(SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationActiveIdle);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted) {
                    matches.push(SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationActiveAdmitted);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Running) {
                    matches.push(SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationActiveRunning);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Completing) {
                    matches.push(SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationActiveCompleting);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown) {
                    matches.push(SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationShuttingDown);
                }
                let transition =
                    Self::single_transition(matches, "AuthorizeRuntimeSystemContextApplication")?;
                match transition {
                    SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationActiveIdle => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Idle;
                        Ok(vec![
                            SessionTurnAdmissionEffect::RuntimeSystemContextApplicationResolved { authorization: RuntimeSystemContextApplicationAuthorization::Authorized, },
                        ])
                    }
                    SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationActiveAdmitted => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::RuntimeSystemContextApplicationResolved { authorization: RuntimeSystemContextApplicationAuthorization::Authorized, },
                        ])
                    }
                    SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationActiveRunning => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Running;
                        Ok(vec![
                            SessionTurnAdmissionEffect::RuntimeSystemContextApplicationResolved { authorization: RuntimeSystemContextApplicationAuthorization::Authorized, },
                        ])
                    }
                    SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationActiveCompleting => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Completing;
                        Ok(vec![
                            SessionTurnAdmissionEffect::RuntimeSystemContextApplicationResolved { authorization: RuntimeSystemContextApplicationAuthorization::Authorized, },
                        ])
                    }
                    SessionTurnAdmissionTransition::AuthorizeRuntimeSystemContextApplicationShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::RuntimeSystemContextApplicationResolved { authorization: RuntimeSystemContextApplicationAuthorization::SessionArchived, },
                        ])
                    }
                    #[allow(unreachable_patterns)] _ => Err(SessionTurnAdmissionError { op: "AuthorizeRuntimeSystemContextApplication_transition" }),
                }
            }
            SessionTurnAdmissionInput::ResolvePendingAdmissionDrained => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown)
                    && (self.state.admission_drain_pending)
                {
                    matches.push(SessionTurnAdmissionTransition::ResolvePendingAdmissionDrained);
                }
                let transition =
                    Self::single_transition(matches, "ResolvePendingAdmissionDrained")?;
                match transition {
                    SessionTurnAdmissionTransition::ResolvePendingAdmissionDrained => {
                        self.state.admission_drain_pending = false;
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![SessionTurnAdmissionEffect::TurnAdmissionProjected {
                            phase: self.state.lifecycle_phase,
                            interrupt_pending: self.state.interrupt_pending,
                            shutdown_pending: self.state.shutdown_pending,
                            is_active: is_active_phase(self.state.lifecycle_phase),
                        }])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "ResolvePendingAdmissionDrained_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::AuthorizeSessionTeardown => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown)
                    && (self.state.admission_drain_pending == false)
                {
                    matches.push(SessionTurnAdmissionTransition::AuthorizeSessionTeardown);
                }
                let transition = Self::single_transition(matches, "AuthorizeSessionTeardown")?;
                match transition {
                    SessionTurnAdmissionTransition::AuthorizeSessionTeardown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![SessionTurnAdmissionEffect::SessionTeardownAuthorized])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "AuthorizeSessionTeardown_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::ResolveRuntimeKeepAlive { keep_alive_request } => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && (keep_alive_request == RuntimeKeepAliveRequest::Enable)
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveRuntimeKeepAliveEnable);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && (keep_alive_request == RuntimeKeepAliveRequest::Disable)
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveRuntimeKeepAliveDisable);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && (keep_alive_request == RuntimeKeepAliveRequest::Preserve)
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveRuntimeKeepAlivePreserve);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown) {
                    matches
                        .push(SessionTurnAdmissionTransition::ResolveRuntimeKeepAliveShuttingDown);
                }
                let transition = Self::single_transition(matches, "ResolveRuntimeKeepAlive")?;
                match transition {
                    SessionTurnAdmissionTransition::ResolveRuntimeKeepAliveEnable => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![SessionTurnAdmissionEffect::RuntimeKeepAliveResolved {
                            decision: RuntimeKeepAlivePersistenceDecision::PersistEnabled,
                        }])
                    }
                    SessionTurnAdmissionTransition::ResolveRuntimeKeepAliveDisable => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![SessionTurnAdmissionEffect::RuntimeKeepAliveResolved {
                            decision: RuntimeKeepAlivePersistenceDecision::PersistDisabled,
                        }])
                    }
                    SessionTurnAdmissionTransition::ResolveRuntimeKeepAlivePreserve => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![SessionTurnAdmissionEffect::RuntimeKeepAliveResolved {
                            decision: RuntimeKeepAlivePersistenceDecision::PreserveExisting,
                        }])
                    }
                    SessionTurnAdmissionTransition::ResolveRuntimeKeepAliveShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::TurnAdmissionShutdownTerminalResolved {
                                terminal: TurnAdmissionShutdownTerminal::SessionArchived,
                            },
                        ])
                    }
                    #[allow(unreachable_patterns)]
                    _ => Err(SessionTurnAdmissionError {
                        op: "ResolveRuntimeKeepAlive_transition",
                    }),
                }
            }
            SessionTurnAdmissionInput::ResolveStartTurnDisposition {
                execution_kind_present,
                execution_kind,
                prompt_trimmed_text_byte_count,
                prompt_non_text_block_count,
                pending_continuation,
            } => {
                let mut matches = Vec::new();
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && ((execution_kind_present)
                        && (execution_kind == StartTurnExecutionKind::ContentTurn))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveDispositionContentTurn);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && ((execution_kind_present)
                        && (execution_kind == StartTurnExecutionKind::ResumePending)
                        && (pending_continuation == PendingContinuationDisposition::RunPending))
                {
                    matches.push(
                        SessionTurnAdmissionTransition::ResolveDispositionResumePendingWithBoundary,
                    );
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && ((execution_kind_present)
                        && (execution_kind == StartTurnExecutionKind::ResumePending)
                        && (pending_continuation
                            == PendingContinuationDisposition::NoPendingBoundary))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveDispositionResumePendingWithoutBoundary);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && ((execution_kind_present == false)
                        && (prompt_has_content(
                            prompt_trimmed_text_byte_count,
                            prompt_non_text_block_count,
                        )))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveDispositionDirectPrompt);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && ((execution_kind_present == false)
                        && (prompt_has_content(
                            prompt_trimmed_text_byte_count,
                            prompt_non_text_block_count,
                        ) == false)
                        && (pending_continuation == PendingContinuationDisposition::RunPending))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveDispositionDirectPending);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::Admitted)
                    && ((execution_kind_present == false)
                        && (prompt_has_content(
                            prompt_trimmed_text_byte_count,
                            prompt_non_text_block_count,
                        ) == false)
                        && (pending_continuation
                            == PendingContinuationDisposition::NoPendingBoundary))
                {
                    matches.push(SessionTurnAdmissionTransition::ResolveDispositionDirectNoPending);
                }
                if (self.state.lifecycle_phase == TurnAdmissionPhase::ShuttingDown) {
                    matches.push(
                        SessionTurnAdmissionTransition::ResolveStartTurnDispositionShuttingDown,
                    );
                }
                let transition = Self::single_transition(matches, "ResolveStartTurnDisposition")?;
                match transition {
                    SessionTurnAdmissionTransition::ResolveDispositionContentTurn => {
                        self.state.last_public_terminal = None;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnDispositionResolved { disposition: StartTurnDisposition::RunContentTurn, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveDispositionResumePendingWithBoundary => {
                        self.state.last_public_terminal = None;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnDispositionResolved { disposition: StartTurnDisposition::RunPending, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveDispositionResumePendingWithoutBoundary => {
                        self.state.last_public_terminal = Some(StartTurnPublicTerminal::NoPendingBoundary);
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnDispositionResolved { disposition: StartTurnDisposition::NoPendingBoundary, },
                            SessionTurnAdmissionEffect::StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveDispositionDirectPrompt => {
                        self.state.last_public_terminal = None;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnDispositionResolved { disposition: StartTurnDisposition::RunContentTurn, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveDispositionDirectPending => {
                        self.state.last_public_terminal = None;
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnDispositionResolved { disposition: StartTurnDisposition::RunPending, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveDispositionDirectNoPending => {
                        self.state.last_public_terminal = Some(StartTurnPublicTerminal::NoPendingBoundary);
                        self.state.lifecycle_phase = TurnAdmissionPhase::Admitted;
                        Ok(vec![
                            SessionTurnAdmissionEffect::StartTurnDispositionResolved { disposition: StartTurnDisposition::NoPendingBoundary, },
                            SessionTurnAdmissionEffect::StartTurnPublicTerminalResolved { terminal: StartTurnPublicTerminal::NoPendingBoundary, },
                        ])
                    }
                    SessionTurnAdmissionTransition::ResolveStartTurnDispositionShuttingDown => {
                        self.state.lifecycle_phase = TurnAdmissionPhase::ShuttingDown;
                        Ok(vec![
                            SessionTurnAdmissionEffect::TurnAdmissionShutdownTerminalResolved { terminal: TurnAdmissionShutdownTerminal::SessionArchived, },
                        ])
                    }
                    #[allow(unreachable_patterns)] _ => Err(SessionTurnAdmissionError { op: "ResolveStartTurnDisposition_transition" }),
                }
            }
        }
    }

    pub fn project_turn_admission(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::ProjectTurnAdmission)
    }

    pub fn claim_turn(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::ClaimTurn)
    }

    pub fn abort_claim(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::AbortClaim)
    }

    pub fn begin_turn(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::BeginTurn)
    }

    pub fn resolve_turn(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::ResolveTurn)
    }

    pub fn finalize_turn(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::FinalizeTurn)
    }

    pub fn request_interrupt(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::RequestInterrupt)
    }

    pub fn request_shutdown(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::RequestShutdown)
    }

    pub fn authorize_start_turn_dispatch(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::AuthorizeStartTurnDispatch)
    }

    pub fn authorize_cancel_after_boundary(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::AuthorizeCancelAfterBoundary)
    }

    pub fn resolve_last_start_turn_public_terminal(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::ResolveLastStartTurnPublicTerminal)
    }

    pub fn authorize_runtime_system_context_application(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::AuthorizeRuntimeSystemContextApplication)
    }

    pub fn resolve_pending_admission_drained(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::ResolvePendingAdmissionDrained)
    }

    pub fn authorize_session_teardown(
        &mut self,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::AuthorizeSessionTeardown)
    }

    pub fn resolve_runtime_keep_alive(
        &mut self,
        keep_alive_request: RuntimeKeepAliveRequest,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::ResolveRuntimeKeepAlive { keep_alive_request })
    }

    pub fn resolve_start_turn_disposition(
        &mut self,
        execution_kind_present: bool,
        execution_kind: StartTurnExecutionKind,
        prompt_trimmed_text_byte_count: u64,
        prompt_non_text_block_count: u64,
        pending_continuation: PendingContinuationDisposition,
    ) -> Result<Vec<SessionTurnAdmissionEffect>, SessionTurnAdmissionError> {
        self.apply_input(SessionTurnAdmissionInput::ResolveStartTurnDisposition {
            execution_kind_present,
            execution_kind,
            prompt_trimmed_text_byte_count,
            prompt_non_text_block_count,
            pending_continuation,
        })
    }
}

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

fn 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)
}

impl Default for SessionTurnAdmissionMachineAuthority {
    fn default() -> Self {
        Self::new()
    }
}
