meerkat-core 0.8.3

Core agent logic for Meerkat (no I/O deps)
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
//! Shared turn-terminality classifier (multi-host mobs §18 O2, phase 6).
//!
//! The ONE owner of "which [`AgentEvent`] ends a tracked turn, and with what
//! payload" (plan line 937; gotcha 15: a second hand-maintained terminal
//! list is the split-terminality bug class by construction). Three
//! consumers, zero divergence window:
//!
//! 1. the controlling host's local flow-turn subscription bridge
//!    (`meerkat-mob` actor turn executor);
//! 2. the member host's tracked-turn journal writer (durable
//!    `RecordTurnOutcome` rows);
//! 3. the controlling host's remote flow ticket registry (per-member fold
//!    over pumped durable rows).
//!
//! Pure, synchronous, wasm-clean: consumes only [`AgentEvent`] values; no
//! I/O, no clock, no allocation on the non-terminal path.

use crate::event::AgentEvent;
use serde_json::Value;

/// The seven turn terminals plus channel-close. 1:1 with the wire
/// `WireFlowTurnOutcome` (meerkat-contracts) and the host journal's DSL
/// `FlowTurnOutcomeKind`; the kind mappings live BESIDE those consumers —
/// this module carries no wire or catalog vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TurnTerminalKind {
    RunCompleted,
    ExtractionSucceeded,
    ExtractionFailed,
    RunFailed,
    InteractionComplete,
    InteractionCallbackPending,
    InteractionFailed,
    ChannelClosed,
}

impl TurnTerminalKind {
    /// Stable snake_case name (log/diagnostic rendering only).
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::RunCompleted => "run_completed",
            Self::ExtractionSucceeded => "extraction_succeeded",
            Self::ExtractionFailed => "extraction_failed",
            Self::RunFailed => "run_failed",
            Self::InteractionComplete => "interaction_complete",
            Self::InteractionCallbackPending => "interaction_callback_pending",
            Self::InteractionFailed => "interaction_failed",
            Self::ChannelClosed => "channel_closed",
        }
    }
}

impl std::fmt::Display for TurnTerminalKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// The classified payload of a turn terminal: what a flow step (or journal
/// detail rendering) receives.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TurnTerminalOutcome {
    Completed {
        output: String,
        structured_output: Option<Value>,
    },
    Failed {
        reason: String,
    },
}

/// One classified terminal: its kind plus its payload.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClassifiedTurnTerminal {
    pub kind: TurnTerminalKind,
    pub outcome: TurnTerminalOutcome,
}

/// Streaming fold over a turn's [`AgentEvent`]s yielding its terminal.
///
/// Semantics are byte-compatible with the pre-phase-6 hand-rolled fold in
/// the mob actor turn executor: `RunCompleted { extraction_required: true }`
/// parks the `(result, structured_output)` pair and is NOT terminal;
/// `ExtractionSucceeded` completes with the parked output (falling back to
/// `structured_output.to_string()` when the pending pair is absent — the
/// fold-gap posture); failure reasons preserve the exact legacy format
/// strings. Display events return `None`.
#[derive(Debug, Default)]
pub struct TurnTerminalClassifier {
    pending_completed_run: Option<(String, Option<Value>)>,
}

impl TurnTerminalClassifier {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Observe one event; `Some` iff it is this turn's terminal.
    pub fn observe(&mut self, event: &AgentEvent) -> Option<ClassifiedTurnTerminal> {
        match event {
            AgentEvent::RunCompleted {
                result,
                structured_output,
                extraction_required,
                ..
            } => {
                if *extraction_required {
                    self.pending_completed_run = Some((result.clone(), structured_output.clone()));
                    return None;
                }
                Some(ClassifiedTurnTerminal {
                    kind: TurnTerminalKind::RunCompleted,
                    outcome: TurnTerminalOutcome::Completed {
                        output: result.clone(),
                        structured_output: structured_output.clone(),
                    },
                })
            }
            AgentEvent::ExtractionSucceeded {
                structured_output, ..
            } => {
                let (output, _) = self
                    .pending_completed_run
                    .take()
                    .unwrap_or_else(|| (structured_output.to_string(), None));
                Some(ClassifiedTurnTerminal {
                    kind: TurnTerminalKind::ExtractionSucceeded,
                    outcome: TurnTerminalOutcome::Completed {
                        output,
                        structured_output: Some(structured_output.clone()),
                    },
                })
            }
            AgentEvent::ExtractionFailed {
                last_output,
                attempts,
                reason,
                ..
            } => {
                let (output, _) = self
                    .pending_completed_run
                    .take()
                    .unwrap_or_else(|| (last_output.clone(), None));
                Some(ClassifiedTurnTerminal {
                    kind: TurnTerminalKind::ExtractionFailed,
                    outcome: TurnTerminalOutcome::Failed {
                        reason: format!(
                            "structured output extraction failed after {attempts} attempt(s): {reason}; last_output={output:?}"
                        ),
                    },
                })
            }
            AgentEvent::InteractionComplete {
                result,
                structured_output,
                ..
            } => Some(ClassifiedTurnTerminal {
                kind: TurnTerminalKind::InteractionComplete,
                outcome: TurnTerminalOutcome::Completed {
                    output: result.clone(),
                    structured_output: structured_output.clone(),
                },
            }),
            AgentEvent::InteractionCallbackPending {
                tool_name, args, ..
            } => Some(ClassifiedTurnTerminal {
                kind: TurnTerminalKind::InteractionCallbackPending,
                outcome: TurnTerminalOutcome::Failed {
                    reason: format!("callback pending for tool '{tool_name}': {args}"),
                },
            }),
            AgentEvent::RunFailed { error_report, .. } => Some(ClassifiedTurnTerminal {
                kind: TurnTerminalKind::RunFailed,
                outcome: TurnTerminalOutcome::Failed {
                    reason: error_report.message.clone(),
                },
            }),
            AgentEvent::InteractionFailed { reason, .. } => Some(ClassifiedTurnTerminal {
                kind: if matches!(
                    reason,
                    crate::event::InteractionFailureReason::ExtractionFailed { .. }
                ) {
                    TurnTerminalKind::ExtractionFailed
                } else {
                    TurnTerminalKind::InteractionFailed
                },
                outcome: TurnTerminalOutcome::Failed {
                    reason: reason.to_string(),
                },
            }),
            _ => None,
        }
    }

    /// The turn's event stream closed without a terminal.
    #[must_use]
    pub fn close(self) -> ClassifiedTurnTerminal {
        ClassifiedTurnTerminal {
            kind: TurnTerminalKind::ChannelClosed,
            outcome: TurnTerminalOutcome::Failed {
                reason: "turn event stream closed before terminal outcome".to_string(),
            },
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use crate::event::{AgentErrorClass, AgentErrorReport, InteractionFailureReason};
    use crate::interaction::InteractionId;
    use crate::types::SessionId;

    fn run_completed(result: &str, structured: Option<Value>, extraction: bool) -> AgentEvent {
        AgentEvent::RunCompleted {
            session_id: SessionId::new(),
            result: result.to_string(),
            structured_output: structured,
            extraction_required: extraction,
            usage: crate::Usage::default(),
            terminal_cause_kind: None,
        }
    }

    /// U-1: table-driven coverage — each terminal event maps to its kind and
    /// payload; display events map to `None`.
    #[test]
    fn classifier_covers_all_seven_terminals_and_channel_close() {
        // 1. RunCompleted (no extraction) — Completed with both payloads.
        let mut classifier = TurnTerminalClassifier::new();
        let terminal = classifier
            .observe(&run_completed(
                "answer",
                Some(serde_json::json!({"answer": 42})),
                false,
            ))
            .expect("terminal");
        assert_eq!(terminal.kind, TurnTerminalKind::RunCompleted);
        assert_eq!(
            terminal.outcome,
            TurnTerminalOutcome::Completed {
                output: "answer".to_string(),
                structured_output: Some(serde_json::json!({"answer": 42})),
            }
        );

        // 2. Extraction pair: park → ExtractionSucceeded completes with the
        //    parked MAIN output.
        let mut classifier = TurnTerminalClassifier::new();
        assert!(
            classifier
                .observe(&run_completed("main answer", None, true))
                .is_none(),
            "extraction-required RunCompleted is NOT terminal"
        );
        let terminal = classifier
            .observe(&AgentEvent::ExtractionSucceeded {
                session_id: SessionId::new(),
                structured_output: serde_json::json!({"answer": 42}),
                schema_warnings: None,
            })
            .expect("terminal");
        assert_eq!(terminal.kind, TurnTerminalKind::ExtractionSucceeded);
        assert_eq!(
            terminal.outcome,
            TurnTerminalOutcome::Completed {
                output: "main answer".to_string(),
                structured_output: Some(serde_json::json!({"answer": 42})),
            }
        );

        // 3. Missing-pending fallback == structured_output.to_string()
        //    (the fold-gap posture, byte-identical to the legacy bridge).
        let mut classifier = TurnTerminalClassifier::new();
        let terminal = classifier
            .observe(&AgentEvent::ExtractionSucceeded {
                session_id: SessionId::new(),
                structured_output: serde_json::json!({"answer": 42}),
                schema_warnings: None,
            })
            .expect("terminal");
        assert_eq!(
            terminal.outcome,
            TurnTerminalOutcome::Completed {
                output: serde_json::json!({"answer": 42}).to_string(),
                structured_output: Some(serde_json::json!({"answer": 42})),
            }
        );

        // 4. Extraction pair: park → ExtractionFailed fails with the exact
        //    legacy format string (attempts + reason + parked output).
        let mut classifier = TurnTerminalClassifier::new();
        assert!(
            classifier
                .observe(&run_completed("main answer", None, true))
                .is_none()
        );
        let terminal = classifier
            .observe(&AgentEvent::ExtractionFailed {
                session_id: SessionId::new(),
                last_output: "ignored (parked wins)".to_string(),
                attempts: 2,
                reason: "Invalid JSON".to_string(),
            })
            .expect("terminal");
        assert_eq!(terminal.kind, TurnTerminalKind::ExtractionFailed);
        assert_eq!(
            terminal.outcome,
            TurnTerminalOutcome::Failed {
                reason: "structured output extraction failed after 2 attempt(s): \
                         Invalid JSON; last_output=\"main answer\""
                    .to_string(),
            }
        );

        // 5. RunFailed — reason is error_report.message verbatim.
        let mut classifier = TurnTerminalClassifier::new();
        let terminal = classifier
            .observe(&AgentEvent::RunFailed {
                session_id: SessionId::new(),
                terminal_cause_kind: None,
                error_report: AgentErrorReport {
                    class: AgentErrorClass::Terminal,
                    reason: None,
                    message: "LLM failure terminal turn".to_string(),
                },
            })
            .expect("terminal");
        assert_eq!(terminal.kind, TurnTerminalKind::RunFailed);
        assert_eq!(
            terminal.outcome,
            TurnTerminalOutcome::Failed {
                reason: "LLM failure terminal turn".to_string(),
            }
        );

        // 6. InteractionComplete — Completed with both payloads.
        let mut classifier = TurnTerminalClassifier::new();
        let terminal = classifier
            .observe(&AgentEvent::InteractionComplete {
                interaction_id: InteractionId(uuid::Uuid::new_v4()),
                result: "{\"answer\":42}".to_string(),
                structured_output: Some(serde_json::json!({"answer": 42})),
            })
            .expect("terminal");
        assert_eq!(terminal.kind, TurnTerminalKind::InteractionComplete);
        assert_eq!(
            terminal.outcome,
            TurnTerminalOutcome::Completed {
                output: "{\"answer\":42}".to_string(),
                structured_output: Some(serde_json::json!({"answer": 42})),
            }
        );

        // 7. InteractionCallbackPending — legacy format string.
        let mut classifier = TurnTerminalClassifier::new();
        let terminal = classifier
            .observe(&AgentEvent::InteractionCallbackPending {
                interaction_id: InteractionId(uuid::Uuid::new_v4()),
                tool_name: "ask_user".to_string(),
                args: serde_json::json!({"q": 1}),
            })
            .expect("terminal");
        assert_eq!(terminal.kind, TurnTerminalKind::InteractionCallbackPending);
        assert_eq!(
            terminal.outcome,
            TurnTerminalOutcome::Failed {
                reason: format!(
                    "callback pending for tool 'ask_user': {}",
                    serde_json::json!({"q": 1})
                ),
            }
        );

        // 8. InteractionFailed — reason.to_string().
        let mut classifier = TurnTerminalClassifier::new();
        let reason = InteractionFailureReason::Abandoned {
            detail: "boom".to_string(),
        };
        let rendered = reason.to_string();
        let terminal = classifier
            .observe(&AgentEvent::InteractionFailed {
                interaction_id: InteractionId(uuid::Uuid::new_v4()),
                reason,
            })
            .expect("terminal");
        assert_eq!(terminal.kind, TurnTerminalKind::InteractionFailed);
        assert_eq!(
            terminal.outcome,
            TurnTerminalOutcome::Failed { reason: rendered }
        );

        // An interaction-scoped extraction failure keeps the extraction kind
        // so journal sidecars agree with the pre-existing wire category.
        let mut classifier = TurnTerminalClassifier::new();
        let terminal = classifier
            .observe(&AgentEvent::InteractionFailed {
                interaction_id: InteractionId(uuid::Uuid::new_v4()),
                reason: InteractionFailureReason::ExtractionFailed {
                    last_output: "main output".to_string(),
                    attempts: 2,
                    reason: "invalid schema".to_string(),
                },
            })
            .expect("terminal");
        assert_eq!(terminal.kind, TurnTerminalKind::ExtractionFailed);

        // 9. ChannelClosed — the close-without-terminal shape.
        let classifier = TurnTerminalClassifier::new();
        let terminal = classifier.close();
        assert_eq!(terminal.kind, TurnTerminalKind::ChannelClosed);
        assert_eq!(
            terminal.outcome,
            TurnTerminalOutcome::Failed {
                reason: "turn event stream closed before terminal outcome".to_string(),
            }
        );

        // Display events are never terminal.
        let mut classifier = TurnTerminalClassifier::new();
        assert!(
            classifier
                .observe(&AgentEvent::TextDelta {
                    delta: "streaming".to_string(),
                })
                .is_none()
        );
        assert!(
            classifier
                .observe(&AgentEvent::TurnStarted { turn_number: 1 })
                .is_none()
        );
    }
}