Skip to main content

meerkat_runtime/
ingress_types.rs

1//! Wire-type shells preserved from the deleted runtime ingress authority.
2//!
3//! These types name admission metadata that persists beyond the authority
4//! itself. They are pure data — no authority methods, no shadow state. The
5//! DSL owns ingress semantics (queue lanes, input phases, admission
6//! ordering); these types just carry content-shape / correlation metadata
7//! from the admission point to observability readers.
8
9use meerkat_core::lifecycle::RuntimeExecutionKind;
10use meerkat_core::lifecycle::run_primitive::{
11    ConversationAppend, ConversationContextAppend, PeerResponseTerminalApplyIntent,
12    RunApplyBoundary,
13};
14use meerkat_core::types::HandlingMode;
15use serde::{Deserialize, Serialize};
16
17use crate::identifiers::{InputKind, KindId};
18
19/// Content shape classification for admitted inputs.
20///
21/// Used by the admitted-input snapshot surface so callers can correlate
22/// admissions by content type without re-parsing the Input payload.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub struct ContentShape(InputKind);
25
26impl ContentShape {
27    pub const fn from_kind(kind: InputKind) -> Self {
28        Self(kind)
29    }
30
31    pub const fn from_kind_id(kind_id: KindId) -> Self {
32        Self(kind_id.kind())
33    }
34
35    pub const fn kind(self) -> InputKind {
36        self.0
37    }
38
39    pub fn as_str(self) -> &'static str {
40        self.0.as_str()
41    }
42}
43
44impl From<InputKind> for ContentShape {
45    fn from(kind: InputKind) -> Self {
46        Self::from_kind(kind)
47    }
48}
49
50impl From<KindId> for ContentShape {
51    fn from(kind_id: KindId) -> Self {
52        Self::from_kind_id(kind_id)
53    }
54}
55
56impl std::fmt::Display for ContentShape {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        f.write_str(self.as_str())
59    }
60}
61
62/// Reservation key for admitted inputs.
63#[derive(Debug, Clone, PartialEq, Eq, Hash)]
64pub struct ReservationKey(pub String);
65
66/// Request ID for correlation tracking.
67#[derive(Debug, Clone, PartialEq, Eq, Hash)]
68pub struct RequestId(pub String);
69
70/// Machine-owned runtime-loop semantics captured at admission.
71///
72/// The runtime loop must not re-read peer conventions, continuation payloads,
73/// or handling-mode hints to decide how a dequeued input runs. Admission has
74/// already resolved the typed policy/kind tuple; this record is the canonical
75/// carrier from that decision point to `RunPrimitive` construction.
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
77pub struct RuntimeInputSemantics {
78    pub(crate) boundary: RunApplyBoundary,
79    pub(crate) execution_kind: RuntimeExecutionKind,
80    pub(crate) execution_handling_mode: Option<HandlingMode>,
81    pub(crate) peer_response_terminal_apply_intent: Option<PeerResponseTerminalApplyIntent>,
82    /// #338: machine-owned verdict that this admitted input requires a live
83    /// channel interrupt (true iff the admitted lane is `Steer`). Carried
84    /// end-to-end so the live-projection consumer reads the typed fact instead
85    /// of re-scanning `handling_mode == Steer`.
86    #[serde(default)]
87    pub(crate) live_interrupt_required: bool,
88}
89
90/// Admitted conversation projection for one input.
91///
92/// The raw input payload is still retained for durability/replay, but the
93/// runtime loop consumes this admitted projection when constructing
94/// `RunPrimitive` so dequeue mechanics do not reinterpret peer conventions or
95/// terminal status payloads.
96// Cannot derive `Eq`: `peer_response_terminal` carries a typed fact whose
97// render payload is a `serde_json::Value`, which is `PartialEq` but not `Eq`.
98#[derive(Debug, Clone, Default, PartialEq)]
99pub struct RuntimeInputProjection {
100    pub append: Option<ConversationAppend>,
101    pub additional_appends: Vec<ConversationAppend>,
102    pub context_append: Option<ConversationContextAppend>,
103    /// Typed terminal-peer-response fact this projection carries when the
104    /// admitted input is a peer terminal response. The producer threads the
105    /// typed [`meerkat_core::PeerResponseTerminalFact`] all the way to the
106    /// realtime/live consumer instead of re-deriving it from the flattened
107    /// `context_append` prose text.
108    pub peer_response_terminal: Option<meerkat_core::PeerResponseTerminalFact>,
109}
110
111impl RuntimeInputSemantics {
112    pub fn try_from_generated_admission(
113        input: &crate::input::Input,
114        runtime_idle: bool,
115    ) -> Result<Self, String> {
116        crate::policy_table::generated_admission_projection_for_input(input, runtime_idle)
117            .map(|projection| projection.runtime_semantics)
118    }
119
120    pub fn boundary(&self) -> RunApplyBoundary {
121        self.boundary
122    }
123
124    pub fn execution_kind(&self) -> RuntimeExecutionKind {
125        self.execution_kind
126    }
127
128    pub fn peer_response_terminal_apply_intent(&self) -> Option<PeerResponseTerminalApplyIntent> {
129        self.peer_response_terminal_apply_intent
130    }
131}
132
133#[cfg(test)]
134mod tests {
135    use super::*;
136
137    #[test]
138    fn terminal_peer_response_keeps_content_turn_execution_kind() {
139        let semantics = crate::policy_table::generated_admission_projection_for_kind(
140            KindId::new(InputKind::PeerResponseTerminal),
141            false,
142        )
143        .expect("generated admission projection")
144        .runtime_semantics;
145
146        assert_eq!(semantics.boundary, RunApplyBoundary::RunStart);
147        assert_eq!(semantics.execution_kind, RuntimeExecutionKind::ContentTurn);
148        assert_eq!(semantics.execution_handling_mode, None);
149        assert_eq!(
150            semantics.peer_response_terminal_apply_intent,
151            Some(PeerResponseTerminalApplyIntent::AppendContextAndRun)
152        );
153    }
154
155    #[test]
156    fn continuation_is_the_only_resume_pending_execution_kind() {
157        let semantics = crate::policy_table::generated_admission_projection_for_kind(
158            KindId::new(InputKind::Continuation),
159            false,
160        )
161        .expect("generated admission projection")
162        .runtime_semantics;
163
164        assert_eq!(semantics.boundary, RunApplyBoundary::RunCheckpoint);
165        assert_eq!(
166            semantics.execution_kind,
167            RuntimeExecutionKind::ResumePending
168        );
169        assert_eq!(semantics.execution_handling_mode, None);
170        assert_eq!(semantics.peer_response_terminal_apply_intent, None);
171    }
172
173    #[test]
174    fn admitted_content_shape_is_closed_to_input_kind_contract() {
175        let shapes = [
176            (InputKind::Prompt, "prompt"),
177            (InputKind::PeerMessage, "peer_message"),
178            (InputKind::PeerRequest, "peer_request"),
179            (InputKind::PeerResponseProgress, "peer_response_progress"),
180            (InputKind::PeerResponseTerminal, "peer_response_terminal"),
181            (InputKind::FlowStep, "flow_step"),
182            (InputKind::ExternalEvent, "external_event"),
183            (InputKind::Continuation, "continuation"),
184            (InputKind::Operation, "operation"),
185        ];
186
187        for (kind, label) in shapes {
188            let shape = ContentShape::from_kind(kind);
189            assert_eq!(shape.kind(), kind);
190            assert_eq!(shape.as_str(), label);
191            assert_eq!(shape.to_string(), label);
192        }
193    }
194
195    #[test]
196    fn admitted_content_shape_source_has_no_string_newtype_contract() {
197        let source = std::fs::read_to_string(
198            std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
199                .join("src")
200                .join("ingress_types.rs"),
201        )
202        .expect("read ingress types source");
203
204        let forbidden = ["pub struct ContentShape", "(pub String)"].concat();
205        assert!(
206            !source.contains(&forbidden),
207            "runtime admitted-input ContentShape must not be a public arbitrary string newtype"
208        );
209    }
210}