meerkat-mobkit 0.6.49

Companion orchestration platform for the Meerkat multi-agent runtime
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
#![allow(
    clippy::expect_used,
    clippy::unwrap_used,
    clippy::panic,
    clippy::uninlined_format_args,
    clippy::collapsible_if,
    clippy::redundant_clone,
    clippy::needless_raw_string_hashes,
    clippy::single_match,
    clippy::redundant_closure_for_method_calls,
    clippy::redundant_pattern_matching,
    clippy::ignored_unit_patterns,
    clippy::clone_on_copy,
    clippy::manual_assert,
    clippy::unwrap_in_result,
    clippy::useless_vec
)]
use std::sync::Arc;
use std::time::Duration;

use meerkat::{AgentFactory, Config, build_ephemeral_service};
use meerkat_client::TestClient;
use meerkat_mob::MobHandle;
use meerkat_mob::{MobDefinition, MobSessionService, MobState, MobStorage, SpawnMemberSpec};
use meerkat_mobkit::{
    DiscoverySpec, ErrorEvent, ErrorHook, MobBootstrapOptions, MobBootstrapSpec, MobKitConfig,
    PostReconcileHook, PostSpawnHook, UnifiedRuntime, UnifiedRuntimeReconcileReport,
};
use tokio::sync::Mutex;

fn spawn_spec(profile: &str, member_id: &str) -> SpawnMemberSpec {
    SpawnMemberSpec::from_wire(
        profile.to_string(),
        member_id.to_string(),
        Some(format!("You are {member_id}. Keep responses concise.").into()),
        None,
        None,
    )
}

fn build_session_service(temp_dir: &tempfile::TempDir) -> Arc<dyn MobSessionService> {
    let session_path = temp_dir.path().join("sessions");
    std::fs::create_dir_all(&session_path).expect("session path");
    let factory = AgentFactory::new(&session_path).comms(true);
    Arc::new(build_ephemeral_service(factory, Config::default(), 16))
}

fn build_mob_spec(temp_dir: &tempfile::TempDir) -> MobBootstrapSpec {
    let definition = MobDefinition::from_toml(
        r#"
[mob]
id = "hooks-test-mob"

[profiles.worker]
model = "gpt-5.2"
external_addressable = true

[profiles.worker.tools]
comms = true
"#,
    )
    .expect("parse mob definition");

    MobBootstrapSpec::new(
        definition,
        MobStorage::in_memory(),
        build_session_service(temp_dir),
    )
    .with_options(MobBootstrapOptions {
        allow_ephemeral_sessions: true,
        notify_orchestrator_on_resume: true,
        default_llm_client: Some(Arc::new(TestClient::default())),
    })
}

fn empty_module_config() -> MobKitConfig {
    MobKitConfig {
        modules: vec![],
        discovery: DiscoverySpec {
            namespace: "hooks-test".to_string(),
            modules: vec![],
        },
        pre_spawn: vec![],
    }
}

#[tokio::test]
#[ignore]
async fn post_spawn_hook_receives_spawned_member_id() {
    let temp_dir = tempfile::tempdir().expect("temp dir");
    let spawned_ids: Arc<Mutex<Vec<Vec<String>>>> = Arc::new(Mutex::new(Vec::new()));
    let captured = spawned_ids.clone();

    let hook: PostSpawnHook = Arc::new(move |ids| {
        let captured = captured.clone();
        Box::pin(async move {
            captured.lock().await.push(ids);
        })
    });

    let runtime = UnifiedRuntime::builder()
        .mob_spec(build_mob_spec(&temp_dir))
        .module_config(empty_module_config())
        .timeout(Duration::from_secs(2))
        .post_spawn_hook(hook)
        .build()
        .await
        .expect("build unified runtime");

    assert_eq!(
        runtime.mob_handle().status().await.unwrap(),
        MobState::Running
    );

    runtime
        .spawn(spawn_spec("worker", "hook-worker-1"))
        .await
        .expect("spawn member");

    let captured = spawned_ids.lock().await;
    assert_eq!(captured.len(), 1, "post-spawn hook should be called once");
    assert_eq!(
        captured[0],
        vec!["hook-worker-1".to_string()],
        "post-spawn hook should receive the spawned member id"
    );

    runtime.shutdown().await;
}

#[tokio::test]
#[ignore]
async fn post_reconcile_hook_receives_reconcile_report() {
    let temp_dir = tempfile::tempdir().expect("temp dir");
    let reports: Arc<Mutex<Vec<UnifiedRuntimeReconcileReport>>> = Arc::new(Mutex::new(Vec::new()));
    let captured = reports.clone();

    let hook: PostReconcileHook = Arc::new(move |report| {
        let captured = captured.clone();
        Box::pin(async move {
            captured.lock().await.push(report);
        })
    });

    let runtime = UnifiedRuntime::builder()
        .mob_spec(build_mob_spec(&temp_dir))
        .module_config(empty_module_config())
        .timeout(Duration::from_secs(2))
        .post_reconcile_hook(hook)
        .build()
        .await
        .expect("build unified runtime");

    let desired = vec![
        spawn_spec("worker", "reconcile-a"),
        spawn_spec("worker", "reconcile-b"),
    ];
    let report = runtime
        .reconcile(desired)
        .await
        .expect("reconcile should succeed");

    let captured = reports.lock().await;
    assert_eq!(
        captured.len(),
        1,
        "post-reconcile hook should be called once"
    );
    assert_eq!(
        captured[0], report,
        "post-reconcile hook should receive the same report returned by reconcile"
    );
    assert_eq!(captured[0].mob.spawned.len(), 2);
    let spawned_identities: Vec<&str> = captured[0]
        .mob
        .spawned
        .iter()
        .map(|receipt| receipt.agent_identity.as_str())
        .collect();
    assert!(spawned_identities.contains(&"reconcile-a"));
    assert!(spawned_identities.contains(&"reconcile-b"));

    runtime.shutdown().await;
}

#[tokio::test]
#[ignore]
async fn mob_handle_returns_working_handle() {
    let temp_dir = tempfile::tempdir().expect("temp dir");
    let runtime = UnifiedRuntime::builder()
        .mob_spec(build_mob_spec(&temp_dir))
        .module_config(empty_module_config())
        .timeout(Duration::from_secs(2))
        .build()
        .await
        .expect("build unified runtime");

    let handle: MobHandle = runtime.mob_handle();

    // The handle should report running state
    assert_eq!(handle.status().await.unwrap(), MobState::Running);

    // Spawn via the handle directly
    handle
        .spawn_spec(spawn_spec("worker", "handle-worker-1"))
        .await
        .expect("spawn via mob_handle");

    // Verify the member is visible through the handle
    let members = handle.list_members().await;
    assert_eq!(members.len(), 1);
    assert_eq!(members[0].agent_identity.to_string(), "handle-worker-1");

    runtime.shutdown().await;
}

#[tokio::test]
#[ignore]
async fn no_hook_still_works() {
    let temp_dir = tempfile::tempdir().expect("temp dir");
    let runtime = UnifiedRuntime::builder()
        .mob_spec(build_mob_spec(&temp_dir))
        .module_config(empty_module_config())
        .timeout(Duration::from_secs(2))
        .build()
        .await
        .expect("build unified runtime");

    // spawn without hooks set should work fine
    runtime
        .spawn(spawn_spec("worker", "no-hook-worker"))
        .await
        .expect("spawn without hook");

    // reconcile without hooks set should work fine
    let report = runtime
        .reconcile(vec![
            spawn_spec("worker", "no-hook-worker"),
            spawn_spec("worker", "no-hook-worker-2"),
        ])
        .await
        .expect("reconcile without hook");

    assert_eq!(report.mob.spawned.len(), 1);
    assert_eq!(
        report.mob.spawned[0].agent_identity.as_str(),
        "no-hook-worker-2"
    );

    runtime.shutdown().await;
}

#[tokio::test]
#[ignore]
async fn error_hook_fires_on_spawn_failure() {
    let temp_dir = tempfile::tempdir().expect("temp dir");
    let errors: Arc<Mutex<Vec<ErrorEvent>>> = Arc::new(Mutex::new(Vec::new()));
    let captured = errors.clone();

    let hook: ErrorHook = Arc::new(move |event| {
        let captured = captured.clone();
        Box::pin(async move {
            captured.lock().await.push(event);
        })
    });

    let runtime = UnifiedRuntime::builder()
        .mob_spec(build_mob_spec(&temp_dir))
        .module_config(empty_module_config())
        .timeout(Duration::from_secs(2))
        .on_error(hook)
        .build()
        .await
        .expect("build unified runtime");

    // Spawn with a non-existent profile to trigger a failure
    let result = runtime
        .spawn(SpawnMemberSpec::from_wire(
            "nonexistent-profile".to_string(),
            "error-hook-agent".to_string(),
            None,
            None,
            None,
        ))
        .await;
    assert!(result.is_err(), "spawn should fail for nonexistent profile");

    // Yield to let the fire-and-forget error hook task complete
    tokio::task::yield_now().await;

    let captured = errors.lock().await;
    assert_eq!(captured.len(), 1, "error hook should fire once");
    match &captured[0] {
        ErrorEvent::SpawnFailure {
            member_id,
            profile,
            error,
        } => {
            assert_eq!(member_id, "error-hook-agent");
            assert_eq!(profile, "nonexistent-profile");
            assert!(!error.is_empty(), "error message should not be empty");
        }
        other => panic!("expected SpawnFailure, got {other:?}"),
    }

    runtime.shutdown().await;
}

#[tokio::test]
#[ignore]
async fn list_members_returns_roster() {
    let temp_dir = tempfile::tempdir().expect("temp dir");
    let runtime = UnifiedRuntime::builder()
        .mob_spec(build_mob_spec(&temp_dir))
        .module_config(empty_module_config())
        .timeout(Duration::from_secs(2))
        .build()
        .await
        .expect("build unified runtime");

    runtime
        .spawn(spawn_spec("worker", "roster-a"))
        .await
        .expect("spawn roster-a");
    runtime
        .spawn(spawn_spec("worker", "roster-b"))
        .await
        .expect("spawn roster-b");

    let members = runtime.mob_handle().list_members_including_retiring().await;
    assert_eq!(members.len(), 2, "should list both members");
    let ids: Vec<String> = members
        .iter()
        .map(|m| m.agent_identity.to_string())
        .collect();
    assert!(
        ids.iter().any(|id| id == "roster-a"),
        "should contain roster-a"
    );
    assert!(
        ids.iter().any(|id| id == "roster-b"),
        "should contain roster-b"
    );

    for m in &members {
        assert_eq!(m.role.as_str(), "worker");
        assert_eq!(m.state, meerkat_mob::MemberState::Active);
    }

    runtime.shutdown().await;
}

#[tokio::test]
#[ignore]
async fn get_member_returns_snapshot_or_none() {
    let temp_dir = tempfile::tempdir().expect("temp dir");
    let runtime = UnifiedRuntime::builder()
        .mob_spec(build_mob_spec(&temp_dir))
        .module_config(empty_module_config())
        .timeout(Duration::from_secs(2))
        .build()
        .await
        .expect("build unified runtime");

    runtime
        .spawn(spawn_spec("worker", "get-member-1"))
        .await
        .expect("spawn get-member-1");

    let handle = runtime.mob_handle();
    let entries = handle.list_members_including_retiring().await;
    let found = entries
        .iter()
        .find(|e| e.agent_identity.as_str() == "get-member-1");
    assert!(found.is_some(), "should find spawned member");
    let snapshot = found.unwrap();
    assert_eq!(snapshot.agent_identity.as_str(), "get-member-1");
    assert_eq!(snapshot.role.as_str(), "worker");
    assert_eq!(snapshot.state, meerkat_mob::MemberState::Active);

    let not_found = entries
        .iter()
        .find(|e| e.agent_identity.as_str() == "nonexistent");
    assert!(not_found.is_none(), "should return None for unknown member");

    runtime.shutdown().await;
}

#[tokio::test]
#[ignore]
async fn retire_member_transitions_state() {
    let temp_dir = tempfile::tempdir().expect("temp dir");
    let runtime = UnifiedRuntime::builder()
        .mob_spec(build_mob_spec(&temp_dir))
        .module_config(empty_module_config())
        .timeout(Duration::from_secs(2))
        .build()
        .await
        .expect("build unified runtime");

    runtime
        .spawn(spawn_spec("worker", "retire-me"))
        .await
        .expect("spawn retire-me");

    runtime
        .mob_handle()
        .retire(meerkat_mob::ids::MeerkatId::from("retire-me"))
        .await
        .expect("retire should succeed");

    // After retire, the member either shows as "retiring" (if it has active
    // work to drain) or is already gone (idle member disposes immediately).
    let members = runtime.mob_handle().list_members_including_retiring().await;
    let retired = members
        .iter()
        .find(|m| m.agent_identity.as_str() == "retire-me");
    match retired {
        Some(m) => assert_eq!(
            m.state,
            meerkat_mob::MemberState::Retiring,
            "if still visible, state should be retiring"
        ),
        None => {} // idle member was immediately disposed — acceptable
    }

    runtime.shutdown().await;
}

#[tokio::test]
#[ignore]
async fn respawn_member_replaces_member() {
    let temp_dir = tempfile::tempdir().expect("temp dir");
    let runtime = UnifiedRuntime::builder()
        .mob_spec(build_mob_spec(&temp_dir))
        .module_config(empty_module_config())
        .timeout(Duration::from_secs(2))
        .build()
        .await
        .expect("build unified runtime");

    runtime
        .spawn(spawn_spec("worker", "respawn-me"))
        .await
        .expect("spawn respawn-me");

    runtime
        .mob_handle()
        .respawn(meerkat_mob::ids::MeerkatId::from("respawn-me"), None)
        .await
        .expect("respawn should succeed");

    // After respawn, the member should still exist in the roster
    let entries = runtime.mob_handle().list_members_including_retiring().await;
    let found = entries
        .iter()
        .find(|e| e.agent_identity.as_str() == "respawn-me");
    assert!(found.is_some(), "member should still exist after respawn");

    runtime.shutdown().await;
}