use meerkat_core::lifecycle::RuntimeExecutionKind;
use meerkat_core::lifecycle::run_primitive::{
ConversationAppend, ConversationContextAppend, PeerResponseTerminalApplyIntent,
RunApplyBoundary,
};
use meerkat_core::types::HandlingMode;
use serde::{Deserialize, Serialize};
use crate::identifiers::{InputKind, KindId};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ContentShape(InputKind);
impl ContentShape {
pub const fn from_kind(kind: InputKind) -> Self {
Self(kind)
}
pub const fn from_kind_id(kind_id: KindId) -> Self {
Self(kind_id.kind())
}
pub const fn kind(self) -> InputKind {
self.0
}
pub fn as_str(self) -> &'static str {
self.0.as_str()
}
}
impl From<InputKind> for ContentShape {
fn from(kind: InputKind) -> Self {
Self::from_kind(kind)
}
}
impl From<KindId> for ContentShape {
fn from(kind_id: KindId) -> Self {
Self::from_kind_id(kind_id)
}
}
impl std::fmt::Display for ContentShape {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ReservationKey(pub String);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RequestId(pub String);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeInputSemantics {
pub(crate) boundary: RunApplyBoundary,
pub(crate) execution_kind: RuntimeExecutionKind,
pub(crate) execution_handling_mode: Option<HandlingMode>,
pub(crate) peer_response_terminal_apply_intent: Option<PeerResponseTerminalApplyIntent>,
#[serde(default)]
pub(crate) live_interrupt_required: bool,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct RuntimeInputProjection {
pub append: Option<ConversationAppend>,
pub additional_appends: Vec<ConversationAppend>,
pub context_append: Option<ConversationContextAppend>,
pub peer_response_terminal: Option<meerkat_core::PeerResponseTerminalFact>,
}
impl RuntimeInputSemantics {
pub fn try_from_generated_admission(
input: &crate::input::Input,
runtime_idle: bool,
) -> Result<Self, String> {
crate::policy_table::generated_admission_projection_for_input(input, runtime_idle)
.map(|projection| projection.runtime_semantics)
}
pub fn boundary(&self) -> RunApplyBoundary {
self.boundary
}
pub fn execution_kind(&self) -> RuntimeExecutionKind {
self.execution_kind
}
pub fn peer_response_terminal_apply_intent(&self) -> Option<PeerResponseTerminalApplyIntent> {
self.peer_response_terminal_apply_intent
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn terminal_peer_response_keeps_content_turn_execution_kind() {
let semantics = crate::policy_table::generated_admission_projection_for_kind(
KindId::new(InputKind::PeerResponseTerminal),
false,
)
.expect("generated admission projection")
.runtime_semantics;
assert_eq!(semantics.boundary, RunApplyBoundary::RunStart);
assert_eq!(semantics.execution_kind, RuntimeExecutionKind::ContentTurn);
assert_eq!(semantics.execution_handling_mode, None);
assert_eq!(
semantics.peer_response_terminal_apply_intent,
Some(PeerResponseTerminalApplyIntent::AppendContextAndRun)
);
}
#[test]
fn continuation_is_the_only_resume_pending_execution_kind() {
let semantics = crate::policy_table::generated_admission_projection_for_kind(
KindId::new(InputKind::Continuation),
false,
)
.expect("generated admission projection")
.runtime_semantics;
assert_eq!(semantics.boundary, RunApplyBoundary::RunCheckpoint);
assert_eq!(
semantics.execution_kind,
RuntimeExecutionKind::ResumePending
);
assert_eq!(semantics.execution_handling_mode, None);
assert_eq!(semantics.peer_response_terminal_apply_intent, None);
}
#[test]
fn admitted_content_shape_is_closed_to_input_kind_contract() {
let shapes = [
(InputKind::Prompt, "prompt"),
(InputKind::PeerMessage, "peer_message"),
(InputKind::PeerRequest, "peer_request"),
(InputKind::PeerResponseProgress, "peer_response_progress"),
(InputKind::PeerResponseTerminal, "peer_response_terminal"),
(InputKind::FlowStep, "flow_step"),
(InputKind::ExternalEvent, "external_event"),
(InputKind::Continuation, "continuation"),
(InputKind::Operation, "operation"),
];
for (kind, label) in shapes {
let shape = ContentShape::from_kind(kind);
assert_eq!(shape.kind(), kind);
assert_eq!(shape.as_str(), label);
assert_eq!(shape.to_string(), label);
}
}
#[test]
fn admitted_content_shape_source_has_no_string_newtype_contract() {
let source = std::fs::read_to_string(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("src")
.join("ingress_types.rs"),
)
.expect("read ingress types source");
let forbidden = ["pub struct ContentShape", "(pub String)"].concat();
assert!(
!source.contains(&forbidden),
"runtime admitted-input ContentShape must not be a public arbitrary string newtype"
);
}
}