meerkat-mobkit 0.8.13

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
#![allow(
    clippy::expect_used,
    clippy::unwrap_used,
    clippy::panic,
    clippy::uninlined_format_args
)]

//! M2 `MobKitStorageLayout` boot coverage: the dual-name fixture matrix per
//! surface. Every surface (builder, `rpc_gateway`, `mobkit_gateway`) must
//! keep booting against a legacy-named state directory (files used where
//! they lie — the resolver never creates a twin), converge fresh
//! directories on the canonical spellings, and refuse loudly when both
//! spellings of one store exist.

use std::io::{BufRead, BufReader, Write};
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::Arc;
use std::sync::mpsc;
use std::time::Duration;

use async_trait::async_trait;
use meerkat_mobkit::identity_first::contracts::RosterProvider;
use meerkat_mobkit::identity_first::{DurableAgentSpec, RosterContext, RosterError};
use meerkat_mobkit::unified_runtime::UnifiedRuntimeBuilder;
use serde_json::{Value, json};
use tempfile::TempDir;

fn test_definition() -> meerkat_mob::MobDefinition {
    meerkat_mob::MobDefinition::from_toml(
        r#"
[mob]
id = "storage-layout-boot-test"

[profiles.default]
model = "gpt-5.5"
"#,
    )
    .expect("parse test mob definition")
}

fn touch(path: &Path) {
    std::fs::write(path, b"").expect("seed fixture file");
}

/// An empty desired roster: enough to drive the identity-first arm (and its
/// continuity-store open) without materializing any member.
struct EmptyRosterProvider;

#[async_trait]
impl RosterProvider for EmptyRosterProvider {
    async fn roster(&self, _context: &RosterContext) -> Result<Vec<DurableAgentSpec>, RosterError> {
        Ok(Vec::new())
    }
}

// ---------------------------------------------------------------------------
// Builder (library surface)
// ---------------------------------------------------------------------------

#[tokio::test]
async fn builder_fresh_state_dir_converges_on_canonical_names() {
    let tmp = TempDir::new().expect("temp dir");
    let state = tmp.path().join("state");

    let runtime = Box::pin(
        UnifiedRuntimeBuilder::default()
            .definition(test_definition())
            .persistent_state(&state)
            .roster_provider(Arc::new(EmptyRosterProvider))
            .default_llm_client(Arc::new(meerkat_client::TestClient::default()))
            .build(),
    )
    .await
    .expect("fresh persistent build");

    for canonical in [
        "sessions.sqlite3",
        "runtime.sqlite",
        "continuity.sqlite3",
        "mobkit_metadata.sqlite3",
        "mobkit_console.sqlite3",
    ] {
        assert!(
            state.join(canonical).exists(),
            "fresh boot must create canonical {canonical}"
        );
    }
    for legacy in [
        "sessions.db",
        "sessions.sqlite",
        "continuity.db",
        "identity_continuity.sqlite",
        "mobkit_metadata.sqlite",
        "mobkit_console.sqlite",
    ] {
        assert!(
            !state.join(legacy).exists(),
            "fresh boot must not create legacy {legacy}"
        );
    }

    runtime.mob_handle().stop().await.expect("stop");
}

#[tokio::test]
async fn builder_keeps_using_a_legacy_named_state_dir_where_it_lies() {
    let tmp = TempDir::new().expect("temp dir");
    let state = tmp.path().join("state");
    std::fs::create_dir_all(&state).expect("state dir");
    // The builder's own historical spellings (recon census): SQLite opens
    // the zero-byte seeds as empty databases and initializes them in place.
    touch(&state.join("sessions.db"));
    touch(&state.join("identity_continuity.sqlite"));
    touch(&state.join("mobkit_metadata.sqlite"));
    touch(&state.join("mobkit_console.sqlite"));

    let runtime = Box::pin(
        UnifiedRuntimeBuilder::default()
            .definition(test_definition())
            .persistent_state(&state)
            .roster_provider(Arc::new(EmptyRosterProvider))
            .default_llm_client(Arc::new(meerkat_client::TestClient::default()))
            .build(),
    )
    .await
    .expect("legacy-named persistent build");

    for (legacy, canonical) in [
        ("sessions.db", "sessions.sqlite3"),
        ("identity_continuity.sqlite", "continuity.sqlite3"),
        ("mobkit_metadata.sqlite", "mobkit_metadata.sqlite3"),
        ("mobkit_console.sqlite", "mobkit_console.sqlite3"),
    ] {
        assert!(
            state.join(legacy).exists(),
            "legacy {legacy} must keep being used where it lies"
        );
        assert!(
            !state.join(canonical).exists(),
            "boot must not create a canonical twin {canonical} beside {legacy}"
        );
        assert!(
            std::fs::metadata(state.join(legacy))
                .expect("metadata")
                .len()
                > 0,
            "legacy {legacy} must have been opened and initialized in place"
        );
    }

    runtime.mob_handle().stop().await.expect("stop");
}

#[tokio::test]
async fn builder_refuses_session_file_name_twins() {
    let tmp = TempDir::new().expect("temp dir");
    let state = tmp.path().join("state");
    std::fs::create_dir_all(&state).expect("state dir");
    touch(&state.join("sessions.db"));
    touch(&state.join("sessions.sqlite3"));

    let error = Box::pin(
        UnifiedRuntimeBuilder::default()
            .definition(test_definition())
            .persistent_state(&state)
            .default_llm_client(Arc::new(meerkat_client::TestClient::default()))
            .build(),
    )
    .await
    .err()
    .expect("twin spellings must refuse to boot");
    let message = error.to_string();
    assert!(
        message.contains("file-name twins") && message.contains("sessions"),
        "twins refusal must name the slot and point at the doctor: {message}"
    );
}

// ---------------------------------------------------------------------------
// rpc_gateway (SDK stdio surface)
// ---------------------------------------------------------------------------

const RPC_MOB_CONFIG: &str = r#"
[mob]
id = "storage-layout-rpc-test"

[profiles.default]
model = "gpt-5.5"
"#;

/// Spawn the real `rpc_gateway --persistent`, send one `mobkit/init` with
/// `persistent_state`, and return the init response.
fn rpc_gateway_init(state_dir: &Path) -> Value {
    let mut child = Command::new(env!("CARGO_BIN_EXE_rpc_gateway"))
        .arg("--persistent")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .expect("spawn rpc_gateway");
    let mut stdin = child.stdin.take().expect("gateway stdin");
    let init = json!({
        "jsonrpc": "2.0",
        "id": "init",
        "method": "mobkit/init",
        "params": {
            "persistent_state": state_dir,
            "mob_config": RPC_MOB_CONFIG,
        }
    });
    writeln!(
        stdin,
        "{}",
        serde_json::to_string(&init).expect("init json")
    )
    .expect("write init");
    stdin.flush().expect("flush init");

    let stdout = child.stdout.take().expect("gateway stdout");
    let (tx, rx) = mpsc::channel();
    std::thread::spawn(move || {
        let mut reader = BufReader::new(stdout);
        let mut line = String::new();
        let _ = reader.read_line(&mut line);
        let _ = tx.send(line);
    });
    let line = rx
        .recv_timeout(Duration::from_secs(90))
        .expect("rpc_gateway did not answer mobkit/init");
    drop(stdin);
    let _ = child.kill();
    let _ = child.wait();
    serde_json::from_str(line.trim()).expect("init response json")
}

#[test]
fn rpc_gateway_fresh_state_dir_converges_on_canonical_names() {
    let state = TempDir::new().expect("state dir");
    let response = rpc_gateway_init(state.path());
    assert!(
        response.get("error").is_none(),
        "fresh init must succeed: {response}"
    );
    for canonical in [
        "sessions.sqlite3",
        "runtime.sqlite",
        "mobkit_metadata.sqlite3",
        "mobkit_console.sqlite3",
    ] {
        assert!(
            state.path().join(canonical).exists(),
            "fresh boot must create canonical {canonical}"
        );
    }
    for legacy in [
        "sessions.db",
        "mobkit_metadata.sqlite",
        "mobkit_console.sqlite",
    ] {
        assert!(
            !state.path().join(legacy).exists(),
            "fresh boot must not create legacy {legacy}"
        );
    }
}

#[test]
fn rpc_gateway_keeps_using_a_legacy_named_state_dir_where_it_lies() {
    let state = TempDir::new().expect("state dir");
    // rpc_gateway's own historical spellings (recon census).
    touch(&state.path().join("sessions.db"));
    touch(&state.path().join("mobkit_metadata.sqlite"));
    touch(&state.path().join("mobkit_console.sqlite"));

    let response = rpc_gateway_init(state.path());
    assert!(
        response.get("error").is_none(),
        "legacy-named init must succeed: {response}"
    );
    for (legacy, canonical) in [
        ("sessions.db", "sessions.sqlite3"),
        ("mobkit_metadata.sqlite", "mobkit_metadata.sqlite3"),
        ("mobkit_console.sqlite", "mobkit_console.sqlite3"),
    ] {
        assert!(
            state.path().join(legacy).exists(),
            "legacy {legacy} must keep being used where it lies"
        );
        assert!(
            !state.path().join(canonical).exists(),
            "boot must not create a canonical twin {canonical} beside {legacy}"
        );
    }
}

#[test]
fn rpc_gateway_refuses_session_file_name_twins() {
    let state = TempDir::new().expect("state dir");
    touch(&state.path().join("sessions.db"));
    touch(&state.path().join("sessions.sqlite3"));

    let response = rpc_gateway_init(state.path());
    let message = response["error"]["message"].as_str().unwrap_or_default();
    assert!(
        message.contains("file-name twins") && message.contains("sessions"),
        "twin spellings must fail init with the twins refusal: {response}"
    );
}

// ---------------------------------------------------------------------------
// mobkit_gateway (console/HTTP surface)
// ---------------------------------------------------------------------------

/// Spawn the real `mobkit_gateway`, send one `mobkit/init` pointed at
/// `store_dir` with `persistent_sessions: true`, and return the init
/// response. XDG state is isolated per test (shared-home CI flake class).
fn mobkit_gateway_init(workspace: &Path, store_dir: &Path) -> Value {
    let init = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "mobkit/init",
        "params": {
            "workspace_root": workspace,
            "store_path": store_dir,
            "persistent_sessions": true,
        },
    });
    let mut child = Command::new(env!("CARGO_BIN_EXE_mobkit_gateway"))
        .current_dir(workspace)
        // Dummy provider secrets: the fallback mob's LLM client is created
        // at bootstrap but never called (mirrors mobkit_gateway_bootstrap).
        .env("ANTHROPIC_API_KEY", "sk-ant-regression-test")
        .env("OPENAI_API_KEY", "sk-regression-test")
        .env("XDG_STATE_HOME", workspace.join("xdg-state"))
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .expect("spawn mobkit_gateway");

    let mut stdin = child.stdin.take().expect("stdin");
    writeln!(
        stdin,
        "{}",
        serde_json::to_string(&init).expect("init json")
    )
    .expect("write init");
    stdin.flush().expect("flush");

    let stdout = child.stdout.take().expect("stdout");
    let (tx, rx) = mpsc::channel();
    std::thread::spawn(move || {
        let mut reader = BufReader::new(stdout);
        let mut line = String::new();
        let _ = reader.read_line(&mut line);
        let _ = tx.send(line);
    });
    let line = rx
        .recv_timeout(Duration::from_secs(90))
        .expect("mobkit_gateway did not answer mobkit/init");
    drop(stdin);
    let _ = child.kill();
    let _ = child.wait();
    serde_json::from_str(line.trim()).expect("init response json")
}

#[test]
fn mobkit_gateway_fresh_store_dir_converges_on_canonical_names() {
    let workspace = TempDir::new().expect("workspace");
    let store = TempDir::new().expect("store");
    let store_dir = store.path().join("store");

    let response = mobkit_gateway_init(workspace.path(), &store_dir);
    assert!(
        response.get("error").is_none(),
        "fresh init must succeed: {response}"
    );
    for canonical in ["sessions.sqlite3", "runtime.sqlite", "continuity.sqlite3"] {
        assert!(
            store_dir.join(canonical).exists(),
            "fresh boot must create canonical {canonical}"
        );
    }
    for legacy in ["sessions.sqlite", "sessions.db", "continuity.db"] {
        assert!(
            !store_dir.join(legacy).exists(),
            "fresh boot must not create legacy {legacy}"
        );
    }
}

#[test]
fn mobkit_gateway_keeps_using_a_legacy_named_store_dir_where_it_lies() {
    let workspace = TempDir::new().expect("workspace");
    let store = TempDir::new().expect("store");
    let store_dir = store.path().join("store");
    std::fs::create_dir_all(&store_dir).expect("store dir");
    // mobkit_gateway's own historical spellings (recon census).
    touch(&store_dir.join("sessions.sqlite"));
    touch(&store_dir.join("continuity.db"));

    let response = mobkit_gateway_init(workspace.path(), &store_dir);
    assert!(
        response.get("error").is_none(),
        "legacy-named init must succeed: {response}"
    );
    for (legacy, canonical) in [
        ("sessions.sqlite", "sessions.sqlite3"),
        ("continuity.db", "continuity.sqlite3"),
    ] {
        assert!(
            store_dir.join(legacy).exists(),
            "legacy {legacy} must keep being used where it lies"
        );
        assert!(
            !store_dir.join(canonical).exists(),
            "boot must not create a canonical twin {canonical} beside {legacy}"
        );
    }
}