meerkat-mobkit 0.8.46

Companion orchestration platform for the Meerkat multi-agent runtime
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
//! Stopping a mob that may have active flow runs (MobKit 0.8.46 interim).
//!
//! meerkat 0.8.51's Stop settles in-flight member work itself, but MobMachine
//! still refuses `Stop` with `InvalidTransition { Running -> Stopped }` while a
//! flow run is active (`StopRunning`'s `no_active_runs` guard). A gateway going
//! down mid-flow is normal, so teardown settles the flow runs and stops:
//!
//! 1. Subscribe to the mob event ledger, then send Stop. Most stops end here,
//!    with one call.
//! 2. On `InvalidTransition`, cancel each non-terminal flow run and await each
//!    run's terminal event (`FlowCompleted`, `FlowFailed`, `FlowCanceled`).
//!    No runs to settle means the refusal is another guard's (explicit
//!    resume, adaptive lifecycle), returned as is.
//! 3. Re-send Stop. A run's terminal event is appended before the actor
//!    applies the run's `FinishRun` (which is what admits Stop), and no
//!    public signal follows `FinishRun` except the machine-state wake. So a
//!    refused re-send is re-issued after each machine commit, with the next
//!    wake armed before each Stop, until Stop is admitted. Unrelated commits
//!    (member-turn interrupts, roster changes) can land before `FinishRun`,
//!    so the re-issue is not capped by count: a refusal from another guard
//!    after the settled runs left surfaces at the hang guard, in the typed
//!    error's `last_refusal`.
//!
//! Everything is bounded by [`MOB_STOP_FLOW_SETTLE_BUDGET`]; past it the
//! typed [`MobStopFlowRunsUnsettled`] names the unsettled runs and the last
//! refusal. meerkat#1593 (Stop settling flow runs itself, or a typed
//! post-`FinishRun` signal) removes this module's re-issue and settle steps.

use std::collections::BTreeSet;
use std::time::Duration;

use meerkat_mob::{MobError, MobRunStatus, RunId};

use crate::mob_handle_runtime::MobRuntimeError;

/// Bound on settling a mob's active flow runs and stopping it during
/// teardown. Inside the mob quiesce window the published shutdown horizon
/// counts.
pub const MOB_STOP_FLOW_SETTLE_BUDGET: Duration = Duration::from_secs(10);

/// A teardown stop that did not settle the mob's flow runs within
/// [`MOB_STOP_FLOW_SETTLE_BUDGET`].
#[derive(Debug)]
pub struct MobStopFlowRunsUnsettled {
    /// Flow runs the teardown cancelled whose terminal it had not observed.
    pub runs: Vec<RunId>,
    /// The last Stop refusal, if a Stop was refused.
    pub last_refusal: Option<MobError>,
    pub budget: Duration,
}

impl std::fmt::Display for MobStopFlowRunsUnsettled {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "mob stop did not settle its flow runs within {:?}: unsettled runs [",
            self.budget
        )?;
        for (index, run) in self.runs.iter().enumerate() {
            if index > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{run}")?;
        }
        write!(f, "]")?;
        match &self.last_refusal {
            Some(refusal) => write!(f, "; last stop refusal: {refusal}"),
            None => write!(f, "; no stop refusal recorded"),
        }
    }
}

/// The run whose terminal a ledger event records, if any.
fn flow_terminal_run(kind: &meerkat_mob::MobEventKind) -> Option<&RunId> {
    match kind {
        meerkat_mob::MobEventKind::FlowCompleted { run_id, .. }
        | meerkat_mob::MobEventKind::FlowFailed { run_id, .. }
        | meerkat_mob::MobEventKind::FlowCanceled { run_id, .. } => Some(run_id),
        _ => None,
    }
}

/// Flow-run terminals from the mob event ledger.
pub(crate) trait FlowTerminals {
    /// The next flow run terminal. `None`: the ledger stream ended.
    async fn next_terminal(&mut self) -> Option<RunId>;
    /// A terminal already delivered, without waiting.
    fn try_next_terminal(&mut self) -> Option<RunId>;
}

/// Machine commit wakes.
pub(crate) trait MachineCommits {
    /// Resolves at the next machine commit after this receiver was created.
    /// `Err`: the actor is gone.
    async fn changed(&mut self) -> Result<(), ()>;
}

/// What a teardown stop needs from a mob.
pub(crate) trait MobStopTarget {
    type Terminals: FlowTerminals;
    type Commits: MachineCommits;
    async fn subscribe_flow_terminals(&self) -> Result<Self::Terminals, MobError>;
    /// Flow runs not yet terminal.
    async fn active_flow_runs(&self) -> Result<Vec<RunId>, MobError>;
    async fn cancel_flow(&self, run_id: RunId) -> Result<(), MobError>;
    /// A commit receiver that has seen the current machine state.
    fn machine_commits(&self) -> Self::Commits;
    async fn stop(&self) -> Result<(), MobError>;
}

/// Settle the mob's active flow runs as needed and stop it (module docs).
pub(crate) async fn settle_flow_runs_then_stop<T: MobStopTarget>(
    target: &T,
    budget: Duration,
) -> Result<(), MobRuntimeError> {
    let mut unsettled = BTreeSet::new();
    let mut last_refusal = None;
    let settle_and_stop = async {
        // Subscribe before the first Stop: a run that ends after it is in the
        // stream.
        let mut terminals = target.subscribe_flow_terminals().await?;
        let mut commits = target.machine_commits();
        let refusal = match target.stop().await {
            Ok(()) => return Ok(()),
            Err(refusal @ MobError::InvalidTransition { .. }) => refusal,
            Err(error) => return Err(error),
        };
        // Runs that ended since the subscription held the Stop too; they are
        // terminal in the run store, so the listing below omits them.
        let mut settled = 0usize;
        while terminals.try_next_terminal().is_some() {
            settled += 1;
        }
        for run_id in target.active_flow_runs().await? {
            unsettled.insert(run_id);
        }
        if unsettled.is_empty() && settled == 0 {
            // No flow run held the Stop: another guard refused it.
            return Err(refusal);
        }
        last_refusal = Some(refusal);
        for run_id in unsettled.clone() {
            target.cancel_flow(run_id).await?;
        }
        while !unsettled.is_empty() {
            let Some(run_id) = terminals.next_terminal().await else {
                return Err(MobError::ActorCommandChannelClosed);
            };
            unsettled.remove(&run_id);
        }
        // Re-issue on machine commits until the settled runs' `FinishRun`
        // admits Stop, bounded by the hang guard. meerkat#1593: a public
        // post-`FinishRun` signal (a `FlowRunRetired` event or an active-run
        // projection) replaces this commit wake with an exact wait, and an
        // unrelated refusal then returns at once.
        loop {
            if commits.changed().await.is_err() {
                return Err(MobError::ActorCommandChannelClosed);
            }
            // Arm the next wake before this Stop: a commit after it is seen,
            // and an earlier `FinishRun` is already reflected in its answer.
            commits = target.machine_commits();
            match target.stop().await {
                Ok(()) => return Ok(()),
                Err(refusal @ MobError::InvalidTransition { .. }) => {
                    last_refusal = Some(refusal);
                }
                Err(error) => return Err(error),
            }
        }
    };
    match tokio::time::timeout(budget, settle_and_stop).await {
        Ok(result) => result.map_err(MobRuntimeError::from),
        Err(_) => Err(MobRuntimeError::MobStopFlowRunsUnsettled(Box::new(
            MobStopFlowRunsUnsettled {
                runs: unsettled.into_iter().collect(),
                last_refusal,
                budget,
            },
        ))),
    }
}

/// [`MobStopTarget`] over a live mob handle.
pub(crate) struct HandleStopTarget<'a>(pub(crate) &'a meerkat_mob::MobHandle);

pub(crate) struct LedgerFlowTerminals(meerkat_mob::MobEventsSubscription);

impl FlowTerminals for LedgerFlowTerminals {
    async fn next_terminal(&mut self) -> Option<RunId> {
        loop {
            let event = self.0.event_rx.recv().await?;
            if let Some(run_id) = flow_terminal_run(&event.kind) {
                return Some(run_id.clone());
            }
        }
    }

    fn try_next_terminal(&mut self) -> Option<RunId> {
        while let Ok(event) = self.0.event_rx.try_recv() {
            if let Some(run_id) = flow_terminal_run(&event.kind) {
                return Some(run_id.clone());
            }
        }
        None
    }
}

impl MachineCommits for meerkat_mob::MobMachineStateChanges {
    async fn changed(&mut self) -> Result<(), ()> {
        meerkat_mob::MobMachineStateChanges::changed(self)
            .await
            .map_err(|_| ())
    }
}

impl MobStopTarget for HandleStopTarget<'_> {
    type Terminals = LedgerFlowTerminals;
    type Commits = meerkat_mob::MobMachineStateChanges;

    async fn subscribe_flow_terminals(&self) -> Result<Self::Terminals, MobError> {
        Ok(LedgerFlowTerminals(self.0.events().subscribe().await?))
    }

    async fn active_flow_runs(&self) -> Result<Vec<RunId>, MobError> {
        Ok(self
            .0
            .list_runs(None)
            .await?
            .into_iter()
            .filter(|run| match run.status {
                MobRunStatus::Pending | MobRunStatus::Running => true,
                MobRunStatus::Completed | MobRunStatus::Failed | MobRunStatus::Canceled => false,
            })
            .map(|run| run.run_id)
            .collect())
    }

    async fn cancel_flow(&self, run_id: RunId) -> Result<(), MobError> {
        self.0.cancel_flow(run_id).await
    }

    fn machine_commits(&self) -> Self::Commits {
        self.0.machine_state_changes()
    }

    async fn stop(&self) -> Result<(), MobError> {
        let report = self.0.stop().await?;
        // A member whose run starts could not be held may still start a turn
        // while the mob is Stopped; teardown proceeds, so say so.
        for (identity, reason) in report.not_holdable() {
            tracing::warn!(
                agent_identity = %identity,
                ?reason,
                "mob stop could not hold this member's run starts"
            );
        }
        Ok(())
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
mod tests {
    use std::collections::VecDeque;
    use std::sync::Mutex;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use meerkat_mob::MobState;
    use tokio::sync::{mpsc, watch};

    use super::*;

    fn refusal() -> MobError {
        MobError::InvalidTransition {
            from: MobState::Running,
            to: MobState::Stopped,
        }
    }

    /// A mob double: scripted Stop answers (a refusal once the script is
    /// spent), listed flow runs, and a machine commit after a refused Stop
    /// while `refusal_commits` lasts (the first refusal's own quiesce commit,
    /// an unrelated commit, or a run's `FinishRun` landing after it).
    struct FakeTarget {
        runs: Vec<RunId>,
        cancel_ends_run: bool,
        refusal_commits: AtomicUsize,
        stop_answers: Mutex<VecDeque<Result<(), MobError>>>,
        stop_calls: AtomicUsize,
        cancels: AtomicUsize,
        terminal_tx: mpsc::UnboundedSender<RunId>,
        terminal_rx: Mutex<Option<mpsc::UnboundedReceiver<RunId>>>,
        commits: watch::Sender<u64>,
    }

    impl FakeTarget {
        fn new(
            runs: Vec<RunId>,
            cancel_ends_run: bool,
            stop_answers: Vec<Result<(), MobError>>,
        ) -> Self {
            let (terminal_tx, terminal_rx) = mpsc::unbounded_channel();
            Self {
                runs,
                cancel_ends_run,
                refusal_commits: AtomicUsize::new(usize::MAX),
                stop_answers: Mutex::new(stop_answers.into()),
                stop_calls: AtomicUsize::new(0),
                cancels: AtomicUsize::new(0),
                terminal_tx,
                terminal_rx: Mutex::new(Some(terminal_rx)),
                commits: watch::channel(0).0,
            }
        }
    }

    struct FakeTerminals(mpsc::UnboundedReceiver<RunId>);

    impl FlowTerminals for FakeTerminals {
        async fn next_terminal(&mut self) -> Option<RunId> {
            self.0.recv().await
        }

        fn try_next_terminal(&mut self) -> Option<RunId> {
            self.0.try_recv().ok()
        }
    }

    struct FakeCommits(watch::Receiver<u64>);

    impl MachineCommits for FakeCommits {
        async fn changed(&mut self) -> Result<(), ()> {
            self.0.changed().await.map_err(|_| ())
        }
    }

    impl MobStopTarget for FakeTarget {
        type Terminals = FakeTerminals;
        type Commits = FakeCommits;

        async fn subscribe_flow_terminals(&self) -> Result<Self::Terminals, MobError> {
            Ok(FakeTerminals(
                self.terminal_rx
                    .lock()
                    .unwrap()
                    .take()
                    .expect("one subscription"),
            ))
        }

        async fn active_flow_runs(&self) -> Result<Vec<RunId>, MobError> {
            Ok(self.runs.clone())
        }

        async fn cancel_flow(&self, run_id: RunId) -> Result<(), MobError> {
            self.cancels.fetch_add(1, Ordering::SeqCst);
            if self.cancel_ends_run {
                self.terminal_tx.send(run_id).unwrap();
            }
            Ok(())
        }

        fn machine_commits(&self) -> Self::Commits {
            FakeCommits(self.commits.subscribe())
        }

        async fn stop(&self) -> Result<(), MobError> {
            self.stop_calls.fetch_add(1, Ordering::SeqCst);
            let answer = self
                .stop_answers
                .lock()
                .unwrap()
                .pop_front()
                .unwrap_or_else(|| Err(refusal()));
            if answer.is_err()
                && self
                    .refusal_commits
                    .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |left| {
                        left.checked_sub(1)
                    })
                    .is_ok()
            {
                self.commits.send_modify(|commit| *commit += 1);
            }
            answer
        }
    }

    #[tokio::test(start_paused = true)]
    async fn a_mob_that_stops_is_stopped_with_one_call() {
        let target = FakeTarget::new(vec![RunId::new()], true, vec![Ok(())]);
        settle_flow_runs_then_stop(&target, MOB_STOP_FLOW_SETTLE_BUDGET)
            .await
            .expect("stopped");
        assert_eq!(target.stop_calls.load(Ordering::SeqCst), 1);
        assert_eq!(target.cancels.load(Ordering::SeqCst), 0, "nothing settled");
    }

    /// No flow run held the refused Stop: another guard refused it (an
    /// explicit resume, the adaptive lifecycle), and it is returned as is.
    #[tokio::test(start_paused = true)]
    async fn a_refusal_with_no_flow_runs_is_returned_as_is() {
        let target = FakeTarget::new(Vec::new(), true, vec![Err(refusal())]);
        let error = settle_flow_runs_then_stop(&target, MOB_STOP_FLOW_SETTLE_BUDGET)
            .await
            .expect_err("refused");
        assert!(
            matches!(
                error,
                MobRuntimeError::Mob(MobError::InvalidTransition { .. })
            ),
            "{error:?}"
        );
        assert_eq!(target.stop_calls.load(Ordering::SeqCst), 1);
    }

    /// The race: the run's terminal event is observed before the actor
    /// applies its `FinishRun`, so the Stop sent after it is refused; the
    /// `FinishRun` commit then wakes one re-issue, which stops the mob.
    #[tokio::test(start_paused = true)]
    async fn a_stop_refused_before_the_runs_finish_run_is_reissued_on_the_commit() {
        let run = RunId::new();
        let target = FakeTarget::new(
            vec![run],
            true,
            vec![Err(refusal()), Err(refusal()), Ok(())],
        );
        settle_flow_runs_then_stop(&target, MOB_STOP_FLOW_SETTLE_BUDGET)
            .await
            .expect("stopped once the run left the machine");
        assert_eq!(target.cancels.load(Ordering::SeqCst), 1);
        assert_eq!(
            target.stop_calls.load(Ordering::SeqCst),
            3,
            "the first Stop, the one refused before FinishRun, the one after"
        );
    }

    /// The CI failure of the capped re-issue: unrelated commits (member-turn
    /// interrupts, roster changes) land before the settled run's `FinishRun`.
    /// Each wakes a re-issue that is refused; the Stop is admitted once the
    /// run has left the machine, however many commits came first.
    #[tokio::test(start_paused = true)]
    async fn a_finish_run_after_unrelated_commits_still_admits_the_stop() {
        let target = FakeTarget::new(
            vec![RunId::new()],
            true,
            vec![
                Err(refusal()),
                Err(refusal()),
                Err(refusal()),
                Err(refusal()),
                Err(refusal()),
                Ok(()),
            ],
        );
        settle_flow_runs_then_stop(&target, MOB_STOP_FLOW_SETTLE_BUDGET)
            .await
            .expect("stopped once the run left the machine");
        assert_eq!(target.stop_calls.load(Ordering::SeqCst), 6);
    }

    /// A refusal that settling the flow runs did not clear (another guard)
    /// and that no further commit can lift surfaces at the hang guard, as the
    /// typed error carrying that refusal.
    #[tokio::test(start_paused = true)]
    async fn an_unrelated_refusal_after_the_settled_runs_surfaces_at_the_guard() {
        let target = FakeTarget::new(vec![RunId::new()], true, Vec::new());
        target.refusal_commits.store(3, Ordering::SeqCst);
        let error = settle_flow_runs_then_stop(&target, MOB_STOP_FLOW_SETTLE_BUDGET)
            .await
            .expect_err("still refused");
        let MobRuntimeError::MobStopFlowRunsUnsettled(unsettled) = &error else {
            panic!("the typed hang-guard error: {error:?}");
        };
        assert!(unsettled.runs.is_empty(), "the run itself settled");
        assert!(matches!(
            unsettled.last_refusal,
            Some(MobError::InvalidTransition { .. })
        ));
        assert_eq!(
            target.stop_calls.load(Ordering::SeqCst),
            4,
            "the first Stop and one re-issue per commit, no more"
        );
    }

    /// A flow run that never terminates trips the named hang guard, whose
    /// typed error names the run and the last refusal.
    #[tokio::test(start_paused = true)]
    async fn a_flow_run_that_never_ends_trips_the_hang_guard() {
        let run = RunId::new();
        let target = FakeTarget::new(vec![run.clone()], false, vec![Err(refusal())]);
        let error = settle_flow_runs_then_stop(&target, MOB_STOP_FLOW_SETTLE_BUDGET)
            .await
            .expect_err("unsettled");
        let MobRuntimeError::MobStopFlowRunsUnsettled(unsettled) = &error else {
            panic!("the typed hang-guard error: {error:?}");
        };
        assert_eq!(unsettled.runs, vec![run.clone()]);
        assert!(matches!(
            unsettled.last_refusal,
            Some(MobError::InvalidTransition { .. })
        ));
        assert_eq!(unsettled.budget, MOB_STOP_FLOW_SETTLE_BUDGET);
        let rendered = error.to_string();
        assert!(
            rendered.contains(&run.to_string()) && rendered.contains("last stop refusal"),
            "{rendered}"
        );
    }
}