meerkat-runtime 0.7.28

v9 runtime control-plane for Meerkat agent lifecycle
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
//! §14 AcceptOutcome — result of accepting an input.

use meerkat_core::lifecycle::InputId;
use meerkat_core::types::HandlingMode;
use serde::{Deserialize, Serialize};
use std::fmt;

use crate::input_state::{InputState, InputStateSeed};
use crate::meerkat_machine::dsl as mm_dsl;
use crate::policy::PolicyDecision;
use crate::runtime_state::RuntimeState;

// `AcceptOutcome` is a domain envelope. The wire shape lives in
// `meerkat-contracts::wire::runtime::RuntimeAcceptResult` and is materialized
// by per-surface handlers (see `meerkat-rpc::handlers::runtime`). The envelope
// therefore carries the live `InputState` shell (no Serialize/Deserialize) and
// a typed `RejectReason` that retains its own serde derives because rejection
// payloads are translated into wire-facing strings.

/// Machine-owned queue action for an admitted input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdmissionQueueAction {
    None,
    EnqueueTo { target: HandlingMode },
    EnqueueFront { target: HandlingMode },
}

/// Machine-owned action against an existing queued input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExistingQueuedAdmissionAction {
    Coalesce { existing_id: InputId },
    Supersede { existing_id: InputId },
}

/// Machine-owned admission plan for an accepted input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdmissionPlan {
    ConsumedOnAccept,
    Queued {
        persist_and_queue: bool,
        queue_action: AdmissionQueueAction,
        existing_action: Option<ExistingQueuedAdmissionAction>,
    },
}

/// Coarse accept flags used by the MeerkatMachine DSL's
/// `AcceptWithCompletion` branches.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CoarseAdmissionFlags {
    pub request_immediate_processing: bool,
    pub interrupt_yielding: bool,
    pub wake_if_idle: bool,
}

/// Typed machine input that authorized a live admission resolution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct MachineAdmissionAuthority {
    input_id: String,
    input_kind: mm_dsl::AdmissionInputKind,
    requested_lane: Option<mm_dsl::InputLane>,
    continuation_kind: mm_dsl::AdmissionContinuationKind,
    silent_intent_match: bool,
    existing_superseded_input_id: Option<String>,
    runtime_running: bool,
    active_turn_boundary_available: bool,
    without_wake: bool,
}

impl MachineAdmissionAuthority {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new(
        input_id: String,
        input_kind: mm_dsl::AdmissionInputKind,
        requested_lane: Option<mm_dsl::InputLane>,
        continuation_kind: mm_dsl::AdmissionContinuationKind,
        silent_intent_match: bool,
        existing_superseded_input_id: Option<InputId>,
        runtime_running: bool,
        active_turn_boundary_available: bool,
        without_wake: bool,
    ) -> Self {
        Self {
            input_id,
            input_kind,
            requested_lane,
            continuation_kind,
            silent_intent_match,
            existing_superseded_input_id: existing_superseded_input_id.map(|id| id.to_string()),
            runtime_running,
            active_turn_boundary_available,
            without_wake,
        }
    }

    pub(crate) fn input_id(&self) -> &str {
        &self.input_id
    }

    pub(crate) fn without_wake(&self) -> bool {
        self.without_wake
    }

    pub(crate) fn active_turn_boundary_available(&self) -> bool {
        self.active_turn_boundary_available
    }

    pub(crate) fn to_dsl_input(&self) -> mm_dsl::MeerkatMachineInput {
        mm_dsl::MeerkatMachineInput::ResolveAdmissionPlan {
            input_id: self.input_id.clone(),
            input_kind: self.input_kind,
            requested_lane: self.requested_lane,
            continuation_kind: self.continuation_kind,
            silent_intent_match: self.silent_intent_match,
            existing_superseded_input_id: self.existing_superseded_input_id.clone(),
            runtime_running: self.runtime_running,
            active_turn_boundary_available: self.active_turn_boundary_available,
            without_wake: self.without_wake,
        }
    }
}

/// Runtime-local proof that generated admission authority authorized execution
/// of the accepted input's semantic admission plan.
#[must_use = "runtime ingress execution capability must be consumed by accept_resolved_input"]
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct RuntimeIngressExecutionCapability {
    input_id: String,
    lane: mm_dsl::InputLane,
    plan: mm_dsl::AdmissionPlanKind,
}

impl RuntimeIngressExecutionCapability {
    fn from_admission_resolved_effect(
        input_id: String,
        lane: mm_dsl::InputLane,
        plan: mm_dsl::AdmissionPlanKind,
    ) -> Self {
        Self {
            input_id,
            lane,
            plan,
        }
    }

    fn validate_for(
        self,
        input_id: &InputId,
        handling_mode: HandlingMode,
        admission_plan: &AdmissionPlan,
    ) -> Result<(), String> {
        if self.input_id != input_id.to_string() {
            return Err(format!(
                "runtime ingress capability id '{}' did not match accepted input '{input_id}'",
                self.input_id
            ));
        }

        let expected_lane = mm_dsl::InputLane::from(handling_mode);
        if self.lane != expected_lane {
            return Err(format!(
                "runtime ingress capability lane {:?} did not match accepted input lane {expected_lane:?}",
                self.lane
            ));
        }

        let expected_plan = match admission_plan {
            AdmissionPlan::ConsumedOnAccept => mm_dsl::AdmissionPlanKind::ConsumedOnAccept,
            AdmissionPlan::Queued { .. } => mm_dsl::AdmissionPlanKind::Queued,
        };
        if self.plan != expected_plan {
            return Err(format!(
                "runtime ingress capability plan {:?} did not match accepted input plan {expected_plan:?}",
                self.plan
            ));
        }

        Ok(())
    }
}

/// Machine-owned resolution of an accepted input's semantic admission path.
// Cannot derive `Eq`: `RuntimeInputProjection` carries a typed
// `peer_response_terminal` fact whose render payload is a `serde_json::Value`.
#[derive(Debug, PartialEq)]
pub struct ResolvedAdmission {
    policy: PolicyDecision,
    handling_mode: HandlingMode,
    runtime_semantics: crate::ingress_types::RuntimeInputSemantics,
    primitive_projection: crate::ingress_types::RuntimeInputProjection,
    admission_plan: AdmissionPlan,
    coarse_flags: CoarseAdmissionFlags,
    requires_active_pre_admission: bool,
    authority: MachineAdmissionAuthority,
    execution_capability: Option<RuntimeIngressExecutionCapability>,
}

impl ResolvedAdmission {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn from_machine_resolution(
        policy: PolicyDecision,
        handling_mode: HandlingMode,
        runtime_semantics: crate::ingress_types::RuntimeInputSemantics,
        primitive_projection: crate::ingress_types::RuntimeInputProjection,
        admission_plan: AdmissionPlan,
        coarse_flags: CoarseAdmissionFlags,
        requires_active_pre_admission: bool,
        authority: MachineAdmissionAuthority,
        execution_capability: Option<(String, mm_dsl::InputLane, mm_dsl::AdmissionPlanKind)>,
    ) -> Self {
        Self {
            policy,
            handling_mode,
            runtime_semantics,
            primitive_projection,
            admission_plan,
            coarse_flags,
            requires_active_pre_admission,
            authority,
            execution_capability: execution_capability.map(|(input_id, lane, plan)| {
                RuntimeIngressExecutionCapability::from_admission_resolved_effect(
                    input_id, lane, plan,
                )
            }),
        }
    }

    pub(crate) fn coarse_flags(&self) -> CoarseAdmissionFlags {
        self.coarse_flags
    }

    pub(crate) fn requires_active_runtime_pre_admission(&self) -> bool {
        self.requires_active_pre_admission
    }

    #[cfg(test)]
    pub(crate) fn policy(&self) -> &PolicyDecision {
        &self.policy
    }

    pub(crate) fn stages_run_boundary(&self) -> bool {
        self.policy.apply_mode == crate::policy::ApplyMode::StageRunBoundary
    }

    pub(crate) fn authority(&self) -> &MachineAdmissionAuthority {
        &self.authority
    }

    pub(crate) fn semantically_equivalent_to(&self, other: &Self) -> bool {
        self.policy == other.policy
            && self.handling_mode == other.handling_mode
            && self.runtime_semantics == other.runtime_semantics
            && self.primitive_projection == other.primitive_projection
            && self.admission_plan == other.admission_plan
            && self.coarse_flags == other.coarse_flags
            && self.requires_active_pre_admission == other.requires_active_pre_admission
            && self.authority == other.authority
    }

    pub(crate) fn consume_execution_capability(
        self,
        input_id: &InputId,
    ) -> Result<
        (
            PolicyDecision,
            HandlingMode,
            crate::ingress_types::RuntimeInputSemantics,
            crate::ingress_types::RuntimeInputProjection,
            AdmissionPlan,
        ),
        String,
    > {
        let Self {
            policy,
            handling_mode,
            runtime_semantics,
            primitive_projection,
            admission_plan,
            execution_capability,
            ..
        } = self;
        let execution_capability = execution_capability.ok_or_else(|| {
            "runtime ingress execution capability was not minted for this admission resolution"
                .to_string()
        })?;
        execution_capability.validate_for(input_id, handling_mode, &admission_plan)?;
        Ok((
            policy,
            handling_mode,
            runtime_semantics,
            primitive_projection,
            admission_plan,
        ))
    }
}

/// Typed reason why an input was rejected at the accept boundary.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "reject_type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum RejectReason {
    /// Runtime is not in a state that accepts input (e.g. stopped, destroyed).
    NotReady {
        /// The runtime state that caused the rejection.
        state: RuntimeState,
    },
    /// Input failed durability validation.
    DurabilityViolation {
        /// Description of the violation.
        detail: String,
    },
    /// Peer input carried a forbidden handling_mode.
    PeerHandlingModeInvalid {
        /// Description of the violation.
        detail: String,
    },
    /// Peer response terminal fact failed typed validation.
    PeerResponseTerminalInvalid {
        /// Description of the violation.
        detail: String,
    },
}

impl fmt::Display for RejectReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotReady { state } => {
                write!(f, "runtime not accepting input while in state: {state}")
            }
            Self::DurabilityViolation { detail } => write!(f, "{detail}"),
            Self::PeerHandlingModeInvalid { detail } => write!(f, "{detail}"),
            Self::PeerResponseTerminalInvalid { detail } => write!(f, "{detail}"),
        }
    }
}

/// Outcome of `RuntimeDriver::accept_input()`.
///
/// Domain envelope returned to in-process callers. Surface crates translate it
/// into the wire shape (`RuntimeAcceptResult` in `meerkat-contracts`) before
/// emitting it on the network, so this type intentionally has no
/// `Serialize`/`Deserialize` derives.
#[derive(Debug, Clone)]
#[non_exhaustive]
#[allow(clippy::large_enum_variant)]
pub enum AcceptOutcome {
    /// Input was accepted and processing has begun.
    Accepted {
        /// The assigned input ID.
        input_id: InputId,
        /// The policy decision applied to this input.
        policy: PolicyDecision,
        /// Current input state.
        state: InputState,
        /// Machine-owned lifecycle seed paired with `state`.
        seed: InputStateSeed,
    },
    /// Input was deduplicated (idempotency key matched an existing input).
    Deduplicated {
        /// The new input ID that was deduplicated.
        input_id: InputId,
        /// The existing input ID that was matched.
        existing_id: InputId,
    },
    /// Input was rejected (validation failed, durability violation, etc.).
    Rejected {
        /// Why the input was rejected.
        reason: RejectReason,
    },
}

impl AcceptOutcome {
    /// Check if the input was accepted.
    pub fn is_accepted(&self) -> bool {
        matches!(self, Self::Accepted { .. })
    }

    /// Check if the input was deduplicated.
    pub fn is_deduplicated(&self) -> bool {
        matches!(self, Self::Deduplicated { .. })
    }

    /// Check if the input was rejected.
    pub fn is_rejected(&self) -> bool {
        matches!(self, Self::Rejected { .. })
    }
}

/// Derive the handling mode from a resolved policy decision.
pub fn handling_mode_from_policy(policy: &PolicyDecision) -> HandlingMode {
    match policy.routing_disposition {
        crate::policy::RoutingDisposition::Steer | crate::policy::RoutingDisposition::Immediate => {
            // Immediate routing must use the steer lane so runtime-owned
            // semantic facts (for example terminal peer responses) cannot get
            // stranded behind ordinary queued prompts before their immediate
            // apply boundary is drained. This preserves the checked-in policy
            // contract without upgrading WakeIfIdle into an active-turn
            // interrupt on this branch.
            HandlingMode::Steer
        }
        _ => HandlingMode::Queue,
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::identifiers::PolicyVersion;
    use crate::policy::{
        ApplyMode, ConsumePoint, DrainPolicy, QueueMode, RoutingDisposition, WakeMode,
    };

    #[test]
    fn accepted_classifier() {
        let outcome = AcceptOutcome::Accepted {
            input_id: InputId::new(),
            policy: PolicyDecision {
                apply_mode: ApplyMode::StageRunStart,
                wake_mode: WakeMode::WakeIfIdle,
                queue_mode: QueueMode::Fifo,
                consume_point: ConsumePoint::OnRunComplete,
                drain_policy: DrainPolicy::QueueNextTurn,
                routing_disposition: RoutingDisposition::Queue,
                record_transcript: true,
                emit_operator_content: true,
                policy_version: PolicyVersion(1),
            },
            state: InputState::new_accepted(InputId::new()),
            seed: InputStateSeed::new_accepted(),
        };
        assert!(outcome.is_accepted());
        assert!(!outcome.is_deduplicated());
        assert!(!outcome.is_rejected());
    }

    #[test]
    fn deduplicated_classifier() {
        let outcome = AcceptOutcome::Deduplicated {
            input_id: InputId::new(),
            existing_id: InputId::new(),
        };
        assert!(!outcome.is_accepted());
        assert!(outcome.is_deduplicated());
        assert!(!outcome.is_rejected());
    }

    #[test]
    fn rejected_classifier() {
        let outcome = AcceptOutcome::Rejected {
            reason: RejectReason::DurabilityViolation {
                detail: "durability violation".into(),
            },
        };
        assert!(!outcome.is_accepted());
        assert!(!outcome.is_deduplicated());
        assert!(outcome.is_rejected());
    }

    #[test]
    fn reject_reason_display() {
        let not_ready = RejectReason::NotReady {
            state: RuntimeState::Stopped,
        };
        assert_eq!(
            not_ready.to_string(),
            "runtime not accepting input while in state: stopped"
        );

        let durability = RejectReason::DurabilityViolation {
            detail: "Derived durability forbidden for prompt".into(),
        };
        assert_eq!(
            durability.to_string(),
            "Derived durability forbidden for prompt"
        );

        let peer = RejectReason::PeerHandlingModeInvalid {
            detail: "handling_mode is forbidden on ResponseProgress peer inputs".into(),
        };
        assert_eq!(
            peer.to_string(),
            "handling_mode is forbidden on ResponseProgress peer inputs"
        );

        let terminal = RejectReason::PeerResponseTerminalInvalid {
            detail: "correlation id cannot be empty".into(),
        };
        assert_eq!(terminal.to_string(), "correlation id cannot be empty");
    }

    #[test]
    fn reject_reason_serde_round_trip() {
        let reasons = vec![
            RejectReason::NotReady {
                state: RuntimeState::Destroyed,
            },
            RejectReason::DurabilityViolation {
                detail: "external derived".into(),
            },
            RejectReason::PeerHandlingModeInvalid {
                detail: "forbidden".into(),
            },
            RejectReason::PeerResponseTerminalInvalid {
                detail: "bad terminal".into(),
            },
        ];
        for reason in reasons {
            let json = serde_json::to_value(&reason).unwrap();
            let parsed: RejectReason = serde_json::from_value(json).unwrap();
            assert_eq!(parsed, reason);
        }
    }

    #[test]
    fn immediate_routing_uses_steer_handling_mode() {
        let policy = PolicyDecision {
            apply_mode: ApplyMode::InjectNow,
            wake_mode: WakeMode::WakeIfIdle,
            queue_mode: QueueMode::None,
            consume_point: ConsumePoint::OnApply,
            drain_policy: DrainPolicy::Immediate,
            routing_disposition: RoutingDisposition::Immediate,
            record_transcript: true,
            emit_operator_content: true,
            policy_version: PolicyVersion(1),
        };

        assert_eq!(handling_mode_from_policy(&policy), HandlingMode::Steer);
    }
}