meerkat-mob 0.8.9

Multi-agent orchestration runtime for Meerkat
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
//! Recovery reconciliation for FlowRun state after crash/restart.
//!
//! `reconcile_run_state` must be called before resuming a run to ensure stored
//! scheduler projections already agree with the per-frame and per-loop kernel
//! snapshots.

use crate::ids::{FlowNodeId, FrameId, LoopInstanceId};
use crate::run::{
    FrameSnapshot, LoopSnapshot, MobRun, MobRunStatus, mob_machine_run_status_is_terminal,
};
use crate::run::{flow_frame, loop_iteration};

/// Errors that prevent a run from being resumed.
#[derive(Debug, thiserror::Error)]
pub enum RestoreIncompatible {
    #[error("cannot resume pre-v3 frame run: schema_version={schema_version}")]
    PreV3Schema { schema_version: u32 },
    #[error("frame invariant violated: frame {frame_id} has Ready nodes not in ready_queue")]
    FrameInvariantViolation { frame_id: FrameId },
    #[error("run projection mismatch: {field}")]
    ProjectionMismatch { field: &'static str },
    #[error("flow authority projection mismatch: {reason}")]
    FlowAuthorityProjectionMismatch { reason: String },
}

/// Validate run scheduler state against frame/loop snapshots.
///
/// Must be called before resuming a run after a crash or restart.
///
/// Recovery may not rewrite machine-owned scheduler fields from projections.
/// If the persisted projections disagree with frame/loop snapshots, the run is
/// incompatible with machine-authority recovery and must be repaired by replaying
/// accepted MobMachine transitions, not by seeding canonical fields here.
///
/// Returns `Err(RestoreIncompatible)` if the persisted state is irrecoverable.
pub fn reconcile_run_state(run: &mut MobRun) -> Result<(), RestoreIncompatible> {
    // 1. Pre-v3 check: reject active runs that predate descriptor-backed frame recovery.
    let run_terminal =
        mob_machine_run_status_is_terminal(&run.run_id, &run.status).map_err(|error| {
            RestoreIncompatible::FlowAuthorityProjectionMismatch {
                reason: error.to_string(),
            }
        })?;
    if run.schema_version < 3 && run.status != MobRunStatus::Pending && !run_terminal {
        return Err(RestoreIncompatible::PreV3Schema {
            schema_version: run.schema_version,
        });
    }

    // 2. Validate per-frame local invariant before reconciling.
    for (frame_id, frame_snap) in &run.frames {
        check_frame_invariant(frame_id, frame_snap)?;
    }

    validate_ready_frames(run)?;

    validate_pending_body_frame_loops(run)?;

    validate_active_counts(run)?;

    run.validate_flow_authority_projection().map_err(|error| {
        RestoreIncompatible::FlowAuthorityProjectionMismatch {
            reason: error.to_string(),
        }
    })?;

    Ok(())
}

/// Check the per-frame invariant: node_status[n] == Ready ↔ n ∈ ready_queue.
fn check_frame_invariant(
    frame_id: &FrameId,
    snap: &FrameSnapshot,
) -> Result<(), RestoreIncompatible> {
    let ready_by_status: std::collections::BTreeSet<FlowNodeId> = snap
        .kernel_state
        .node_status
        .iter()
        .filter(|(_, status)| *status == &crate::run::flow_frame::NodeRunStatus::Ready)
        .map(|(node_id, _)| node_id.clone())
        .collect();

    let in_ready_queue: std::collections::BTreeSet<FlowNodeId> =
        snap.kernel_state.ready_queue.iter().cloned().collect();

    if ready_by_status != in_ready_queue {
        return Err(RestoreIncompatible::FrameInvariantViolation {
            frame_id: frame_id.clone(),
        });
    }

    Ok(())
}

fn validate_ready_frames(run: &MobRun) -> Result<(), RestoreIncompatible> {
    let mut active_frame_ids: Vec<FrameId> = run
        .frames
        .iter()
        .filter(|(_, snap)| frame_has_nonempty_ready_queue(snap))
        .map(|(frame_id, _)| frame_id.clone())
        .collect();
    active_frame_ids.sort();
    let active_frame_membership = active_frame_ids.iter().cloned().collect();
    if run.flow_state.ready_frames != active_frame_ids {
        return Err(RestoreIncompatible::ProjectionMismatch {
            field: "ready_frames",
        });
    }
    if run.flow_state.ready_frame_membership != active_frame_membership {
        return Err(RestoreIncompatible::ProjectionMismatch {
            field: "ready_frame_membership",
        });
    }
    Ok(())
}

fn validate_pending_body_frame_loops(run: &MobRun) -> Result<(), RestoreIncompatible> {
    let mut pending_loop_ids: Vec<LoopInstanceId> = run
        .loops
        .iter()
        .filter(|(_, snap)| loop_is_pending_body_frame(snap))
        .map(|(loop_id, _)| loop_id.clone())
        .collect();
    pending_loop_ids.sort();
    let pending_loop_membership = pending_loop_ids.iter().cloned().collect();
    if run.flow_state.pending_body_frame_loops != pending_loop_ids {
        return Err(RestoreIncompatible::ProjectionMismatch {
            field: "pending_body_frame_loops",
        });
    }
    if run.flow_state.pending_body_frame_loop_membership != pending_loop_membership {
        return Err(RestoreIncompatible::ProjectionMismatch {
            field: "pending_body_frame_loop_membership",
        });
    }
    Ok(())
}

fn validate_active_counts(run: &MobRun) -> Result<(), RestoreIncompatible> {
    let active_node_count = run
        .frames
        .values()
        .map(count_running_step_nodes)
        .sum::<u32>();
    let active_frame_count = run
        .loops
        .values()
        .filter_map(active_body_frame_id)
        .filter(|frame_id| run.frames.contains_key(frame_id))
        .count() as u32;

    if run.flow_state.active_node_count != active_node_count {
        return Err(RestoreIncompatible::ProjectionMismatch {
            field: "active_node_count",
        });
    }
    if run.flow_state.active_frame_count != active_frame_count {
        return Err(RestoreIncompatible::ProjectionMismatch {
            field: "active_frame_count",
        });
    }
    Ok(())
}

/// Return true if a loop snapshot represents a loop that is pending a body frame start.
///
/// This is the typed owner's predicate — identical to the live engine's
/// pending-body-frame recovery check: the loop kernel is in `Running` phase
/// AND its machine-owned `stage` is `AwaitingBodyFrame`. Pending-ness is never
/// re-derived from the absence of `active_body_frame_id`; a snapshot whose
/// stage says a body frame is active but whose frame id is missing is a
/// corrupt projection and surfaces through the active-count validation rather
/// than being silently reclassified as pending.
fn loop_is_pending_body_frame(snap: &LoopSnapshot) -> bool {
    snap.kernel_state.phase == loop_iteration::Phase::Running
        && snap.kernel_state.stage == loop_iteration::LoopIterationStage::AwaitingBodyFrame
}

fn active_body_frame_id(snap: &LoopSnapshot) -> Option<FrameId> {
    snap.kernel_state.active_body_frame_id.clone()
}

fn count_running_step_nodes(snap: &FrameSnapshot) -> u32 {
    snap.kernel_state
        .node_status
        .iter()
        .filter(|(node_id, status)| {
            *status == &crate::run::flow_frame::NodeRunStatus::Running
                && matches!(
                    snap.kernel_state
                        .node_kind
                        .get(*node_id)
                        .map(flow_frame::FlowNodeKind::as_str),
                    Some("Step")
                )
        })
        .count() as u32
}

/// Return true if the frame's `ready_queue` is present and non-empty.
fn frame_has_nonempty_ready_queue(snap: &FrameSnapshot) -> bool {
    !snap.kernel_state.ready_queue.is_empty()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ids::RunId;
    use crate::run::{MobRun, MobRunStatus};
    use std::collections::BTreeMap;

    fn minimal_authority_backed_run(status: MobRunStatus) -> MobRun {
        MobRun::authority_backed_for_steps(
            RunId::new(),
            crate::MobId::from("test-mob"),
            crate::FlowId::from("test-flow"),
            std::iter::empty::<crate::ids::StepId>(),
            status,
            serde_json::json!({}),
        )
        .expect("authority-backed recovery run")
    }

    fn minimal_v2_run_running() -> MobRun {
        minimal_authority_backed_run(MobRunStatus::Running)
    }

    fn frame_snapshot_with_ready_queue(
        _frame_id: &str,
        ready_nodes: Vec<&str>,
        all_nodes_ready: bool,
    ) -> FrameSnapshot {
        let mut state = crate::run::flow_frame::initial_state();
        state.phase = crate::run::flow_frame::Phase::Running;
        state.ready_queue = ready_nodes.iter().map(|s| FlowNodeId::from(*s)).collect();
        state.tracked_nodes = ready_nodes.iter().map(|s| FlowNodeId::from(*s)).collect();
        if all_nodes_ready {
            state.node_status = ready_nodes
                .iter()
                .map(|s| {
                    (
                        FlowNodeId::from(*s),
                        crate::run::flow_frame::NodeRunStatus::Ready,
                    )
                })
                .collect();
        }
        FrameSnapshot {
            kernel_state: state,
        }
    }

    #[test]
    fn test_pre_v3_pending_run_is_rejected_by_authority_projection() {
        let mut run = minimal_authority_backed_run(MobRunStatus::Pending);
        run.schema_version = 2;
        let result = reconcile_run_state(&mut run);
        assert!(matches!(
            result,
            Err(RestoreIncompatible::FlowAuthorityProjectionMismatch { .. })
        ));
    }

    #[test]
    fn test_pre_v3_running_run_is_rejected() {
        let mut run = minimal_v2_run_running();
        run.schema_version = 2;
        run.status = MobRunStatus::Running;
        let result = reconcile_run_state(&mut run);
        assert!(matches!(
            result,
            Err(RestoreIncompatible::PreV3Schema { .. })
        ));
    }

    #[test]
    fn test_empty_run_reconciles_ok() {
        let mut run = minimal_v2_run_running();
        assert!(reconcile_run_state(&mut run).is_ok());
    }

    #[test]
    fn test_active_counts_without_frame_authority_rejects() {
        let mut run = minimal_v2_run_running();
        let mut root = crate::run::flow_frame::initial_state();
        root.phase = crate::run::flow_frame::Phase::Running;
        root.node_status = BTreeMap::from([(
            FlowNodeId::from("loop-node"),
            crate::run::flow_frame::NodeRunStatus::Running,
        )]);
        root.node_kind = BTreeMap::from([(
            FlowNodeId::from("loop-node"),
            crate::run::flow_frame::FlowNodeKind::Loop,
        )]);
        run.frames
            .insert(FrameId::from("root"), FrameSnapshot { kernel_state: root });

        let mut body = crate::run::flow_frame::initial_state();
        body.phase = crate::run::flow_frame::Phase::Running;
        body.node_status = BTreeMap::from([(
            FlowNodeId::from("body-step"),
            crate::run::flow_frame::NodeRunStatus::Running,
        )]);
        body.node_kind = BTreeMap::from([(
            FlowNodeId::from("body-step"),
            crate::run::flow_frame::FlowNodeKind::Step,
        )]);
        run.frames.insert(
            FrameId::from("body-frame"),
            FrameSnapshot { kernel_state: body },
        );

        let mut loop_state = crate::run::loop_iteration::initial_state();
        loop_state.phase = crate::run::loop_iteration::Phase::Running;
        // The machine-owned stage must agree with the active body frame so the
        // pending-body-frame validation passes and the authority projection
        // check is what rejects the unbacked counts.
        loop_state.stage = crate::run::loop_iteration::LoopIterationStage::BodyFrameActive;
        loop_state.active_body_frame_id = Some(FrameId::from("body-frame"));
        run.loops.insert(
            LoopInstanceId::from("loop-1"),
            LoopSnapshot {
                kernel_state: loop_state,
            },
        );

        run.flow_state.active_node_count = 1;
        run.flow_state.active_frame_count = 1;
        let result = reconcile_run_state(&mut run);
        assert!(matches!(
            result,
            Err(RestoreIncompatible::FlowAuthorityProjectionMismatch { .. })
        ));
    }

    #[test]
    fn test_pending_body_frame_uses_machine_owned_stage_not_field_absence() {
        // The typed owner's predicate: Running + AwaitingBodyFrame is pending.
        let mut awaiting = crate::run::loop_iteration::initial_state();
        awaiting.phase = crate::run::loop_iteration::Phase::Running;
        awaiting.stage = crate::run::loop_iteration::LoopIterationStage::AwaitingBodyFrame;
        assert!(loop_is_pending_body_frame(&LoopSnapshot {
            kernel_state: awaiting,
        }));

        // Regression: a corrupt snapshot whose stage says a body frame is
        // ACTIVE but whose `active_body_frame_id` is missing must NOT be
        // reclassified as pending from the field's absence.
        let mut corrupt = crate::run::loop_iteration::initial_state();
        corrupt.phase = crate::run::loop_iteration::Phase::Running;
        corrupt.stage = crate::run::loop_iteration::LoopIterationStage::BodyFrameActive;
        corrupt.active_body_frame_id = None;
        assert!(
            !loop_is_pending_body_frame(&LoopSnapshot {
                kernel_state: corrupt,
            }),
            "missing active_body_frame_id must not fabricate pending-ness"
        );
    }

    #[test]
    fn test_frame_invariant_valid_empty_queue() {
        let frame_id = crate::FrameId::from("f1");
        let mut state = crate::run::flow_frame::initial_state();
        state.phase = crate::run::flow_frame::Phase::Running;
        state.node_status = BTreeMap::from([(
            FlowNodeId::from("node-a"),
            crate::run::flow_frame::NodeRunStatus::Running,
        )]);
        let snap = FrameSnapshot {
            kernel_state: state,
        };
        assert!(check_frame_invariant(&frame_id, &snap).is_ok());
    }

    #[test]
    fn test_frame_invariant_violation_ready_not_in_queue() {
        let frame_id = crate::FrameId::from("f-bad");
        let mut state = crate::run::flow_frame::initial_state();
        state.phase = crate::run::flow_frame::Phase::Running;
        state.node_status = BTreeMap::from([(
            FlowNodeId::from("node-a"),
            crate::run::flow_frame::NodeRunStatus::Ready,
        )]);
        let snap = FrameSnapshot {
            kernel_state: state,
        };
        let result = check_frame_invariant(&frame_id, &snap);
        assert!(matches!(
            result,
            Err(RestoreIncompatible::FrameInvariantViolation { .. })
        ));
    }

    #[test]
    fn test_reconcile_removes_stale_ready_frames() {
        let mut run = minimal_v2_run_running();
        let stale = frame_snapshot_with_ready_queue("frame-1", vec![], false);
        run.frames.insert(crate::FrameId::from("frame-1"), stale);
        run.flow_state.ready_frames.push(FrameId::from("frame-1"));
        run.flow_state
            .ready_frame_membership
            .insert(FrameId::from("frame-1"));
        let result = reconcile_run_state(&mut run);
        assert!(matches!(
            result,
            Err(RestoreIncompatible::ProjectionMismatch {
                field: "ready_frames"
            })
        ));
    }

    #[test]
    fn test_reconcile_adds_missing_ready_frames() {
        let mut run = minimal_v2_run_running();
        let active = frame_snapshot_with_ready_queue("frame-2", vec!["node-a"], true);
        run.frames.insert(crate::FrameId::from("frame-2"), active);
        let result = reconcile_run_state(&mut run);
        assert!(matches!(
            result,
            Err(RestoreIncompatible::ProjectionMismatch {
                field: "ready_frames"
            })
        ));
    }
}