rkat 0.8.0

CLI for the Meerkat agent platform — run LLM agents from the terminal
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
//! Multi-host mobs phase 7 — §17.11 e2e-system row: REAL `rkat mob <verb>`
//! console wiring (T-B14).
//!
//! Everything the deterministic in-process lanes cannot pin: the shipped
//! binary drives the phase-7 console verbs end-to-end over real loopback
//! TCP against a live `rkat mob host` daemon — descriptor hand-off →
//! `bind-host` → `hosts` → `route-installs` → `member-history` →
//! `revoke-host` — and the §17.4 typed exit path is OBSERVED on the real
//! process: a reply-deadline bind against a stopped daemon exits 46 with
//! the typed `detail:` stderr line, while a deliberately-unmapped error
//! (member not found) keeps EXIT_ERROR=1.
//!
//! Lane: `e2e-system` (local `make e2e-system` / nightly — NOT GitHub CI).
//! Registered as the `cli-mob-verbs` suite in
//! `tests/integration/src/e2e_lanes.rs`.

#![cfg(feature = "integration-real-tests")]
#![cfg(unix)]
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::Duration;

use meerkat_mob::runtime::bridge_protocol::WireHostBindingDescriptor;
use tempfile::TempDir;
use tokio::process::{Child, Command};

const DESCRIPTOR_TIMEOUT: Duration = Duration::from_secs(60);
/// The bind bridge reply deadline is 60s (actor bind_host send); the verb
/// budget must comfortably contain it.
const VERB_TIMEOUT: Duration = Duration::from_secs(150);

const MOB_ID: &str = "mob-cli-verbs";
const REALM: &str = "mob-cli-verbs-e2e";

fn rkat_binary_path() -> Option<PathBuf> {
    if let Some(path) = std::env::var_os("CARGO_BIN_EXE_rkat") {
        let path = PathBuf::from(path);
        if path.exists() {
            return Some(path.canonicalize().unwrap_or(path));
        }
    }
    if let Some(target_dir) = std::env::var_os("CARGO_TARGET_DIR") {
        let target_dir = PathBuf::from(target_dir);
        for profile in ["debug", "release"] {
            let candidate = target_dir.join(profile).join("rkat");
            if candidate.exists() {
                return Some(candidate);
            }
        }
    }
    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let workspace_root = manifest_dir.parent()?;
    for target in ["target-codex", "target"] {
        for profile in ["debug", "release"] {
            let candidate = workspace_root.join(target).join(profile).join("rkat");
            if candidate.exists() {
                return Some(candidate);
            }
        }
    }
    None
}

struct DaemonHome {
    temp: TempDir,
}

impl DaemonHome {
    fn new() -> Self {
        Self {
            temp: TempDir::new().expect("daemon home tempdir"),
        }
    }

    fn descriptor_path(&self) -> PathBuf {
        self.temp.path().join("host-binding.json")
    }

    fn identity_dir(&self) -> PathBuf {
        self.temp.path().join("host-identity")
    }

    fn spawn_daemon(&self, rkat: &Path) -> Child {
        Command::new(rkat)
            .current_dir(self.temp.path())
            .env("HOME", self.temp.path())
            .env("XDG_CONFIG_HOME", self.temp.path().join("config"))
            .args([
                "--state-root",
                self.temp.path().join("realms").to_str().expect("utf8"),
                "--realm",
                "mob-cli-verbs-host",
                "mob",
                "host",
                "--listen-tcp",
                "127.0.0.1:0",
                "--identity-dir",
                self.identity_dir().to_str().expect("utf8"),
                "--descriptor-out",
                self.descriptor_path().to_str().expect("utf8"),
            ])
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .kill_on_drop(true)
            .spawn()
            .expect("spawn rkat mob host")
    }

    /// Poll the descriptor slot until it parses and (when `differs_from` is
    /// set) carries a DIFFERENT bootstrap token — the daemon rewrites the
    /// file in place on re-mint.
    async fn wait_for_descriptor(
        &self,
        differs_from: Option<&str>,
        daemon: &mut Child,
    ) -> WireHostBindingDescriptor {
        let deadline = tokio::time::Instant::now() + DESCRIPTOR_TIMEOUT;
        loop {
            if let Ok(bytes) = std::fs::read(self.descriptor_path())
                && let Ok(descriptor) = serde_json::from_slice::<WireHostBindingDescriptor>(&bytes)
                && differs_from.is_none_or(|old| descriptor.bootstrap_token.as_str() != old)
            {
                return descriptor;
            }
            if let Ok(Some(status)) = daemon.try_wait() {
                let mut stderr = String::new();
                if let Some(mut pipe) = daemon.stderr.take() {
                    use tokio::io::AsyncReadExt as _;
                    let _ = pipe.read_to_string(&mut stderr).await;
                }
                panic!("daemon exited before publishing a descriptor: {status}\n{stderr}");
            }
            assert!(
                tokio::time::Instant::now() < deadline,
                "timed out waiting for the host binding descriptor"
            );
            tokio::time::sleep(Duration::from_millis(50)).await;
        }
    }

    fn signal(&self, daemon: &Child, signal: &str) {
        let pid = daemon.id().expect("daemon pid").to_string();
        let status = std::process::Command::new("kill")
            .args([signal, pid.as_str()])
            .status()
            .expect("send signal to daemon");
        assert!(status.success(), "kill {signal} {pid} failed");
    }
}

struct ConsoleHome {
    temp: TempDir,
}

impl ConsoleHome {
    fn new() -> Self {
        Self {
            temp: TempDir::new().expect("console home tempdir"),
        }
    }

    fn path(&self) -> &Path {
        self.temp.path()
    }

    async fn run_rkat(&self, rkat: &Path, args: &[&str]) -> std::process::Output {
        let mut cmd = Command::new(rkat);
        cmd.current_dir(self.temp.path())
            .env("HOME", self.temp.path())
            .env("XDG_CONFIG_HOME", self.temp.path().join("config"))
            .arg("--state-root")
            .arg(self.temp.path().join("realms"))
            .arg("--realm")
            .arg(REALM)
            .args(args)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        tokio::time::timeout(VERB_TIMEOUT, cmd.output())
            .await
            .unwrap_or_else(|_| panic!("rkat {args:?} exceeded the verb budget"))
            .expect("spawn rkat")
    }
}

fn stdout_str(output: &std::process::Output) -> String {
    String::from_utf8_lossy(&output.stdout).to_string()
}

fn stderr_str(output: &std::process::Output) -> String {
    String::from_utf8_lossy(&output.stderr).to_string()
}

fn assert_success(output: &std::process::Output, what: &str) {
    assert!(
        output.status.success(),
        "{what} failed ({:?}):\nstdout: {}\nstderr: {}",
        output.status.code(),
        stdout_str(output),
        stderr_str(output),
    );
}

/// Minimal mobpack fixture with a callable flow so `rkat mob run --detach`
/// installs the mob into the console realm's persistent mob state without
/// waiting on any model turn.
async fn write_mobpack_fixture(project_dir: &Path) -> PathBuf {
    let mob_dir = project_dir.join("mob-cli-verbs-fixture");
    tokio::fs::create_dir_all(&mob_dir)
        .await
        .expect("fixture dir");
    tokio::fs::write(
        mob_dir.join("manifest.toml"),
        format!("[mobpack]\nname = \"{MOB_ID}\"\nversion = \"1.0.0\"\n"),
    )
    .await
    .expect("write manifest");
    let definition = format!(
        r#"{{
  "id":"{MOB_ID}",
  "profiles":{{
    "worker":{{
      "model":"claude-sonnet-4-5",
      "tools":{{"comms":true}},
      "peer_description":"Worker"
    }}
  }},
  "flows":{{
    "main":{{
      "description":"phase-7 CLI verb fixture flow",
      "steps":{{
        "work":{{
          "role":"worker",
          "message":"Say ok.",
          "timeout_ms":60000
        }}
      }}
    }}
  }},
  "skills":{{}}
}}"#
    );
    tokio::fs::write(mob_dir.join("definition.json"), definition)
        .await
        .expect("write definition");
    mob_dir
}

async fn shutdown(mut daemon: Child) {
    let _ = daemon.kill().await;
    let _ = daemon.wait().await;
}

#[tokio::test(flavor = "multi_thread")]
#[ignore = "lane:e2e-system"]
async fn integration_real_mob_cli_verbs_over_tcp() {
    let rkat = rkat_binary_path().expect("rkat binary not found");
    let console = ConsoleHome::new();

    // ---- Install the mob into the console realm (mobpack --detach) ----
    let fixture = write_mobpack_fixture(console.path()).await;
    let pack_path = console.path().join("mob-cli-verbs.mobpack");
    let packed = console
        .run_rkat(
            &rkat,
            &[
                "mob",
                "pack",
                fixture.to_str().expect("utf8"),
                "-o",
                pack_path.to_str().expect("utf8"),
            ],
        )
        .await;
    assert_success(&packed, "rkat mob pack");

    let ran = console
        .run_rkat(
            &rkat,
            &[
                "mob",
                "run",
                pack_path.to_str().expect("utf8"),
                // The fixture pack is locally built and unsigned; permissive
                // is the sanctioned dev/test posture (strict-mode rejection
                // of unsigned packs is pinned separately in main.rs tests).
                "--trust-policy",
                "permissive",
                "--detach",
                "--json",
            ],
        )
        .await;
    assert_success(&ran, "rkat mob run --detach");
    // `mob run --json` appends `warning\t…` lines after the JSON document
    // (pre-existing render_mob_run_pack_with_warnings behavior — permissive
    // trust warns about the unsigned pack), so parse the FIRST document.
    let run_stdout = stdout_str(&ran);
    let run_doc: serde_json::Value = serde_json::Deserializer::from_str(run_stdout.trim())
        .into_iter()
        .next()
        .expect("detach run emits a JSON document")
        .expect("detach run JSON");
    assert_eq!(run_doc["mob_id"], MOB_ID, "mob installed under its id");

    // ---- Daemon up, descriptor on disk ----
    let home = DaemonHome::new();
    let mut daemon = home.spawn_daemon(&rkat);
    let d0 = home.wait_for_descriptor(None, &mut daemon).await;

    // ---- hosts: empty roster before any bind ----
    let hosts = console
        .run_rkat(&rkat, &["mob", "hosts", MOB_ID, "--json"])
        .await;
    assert_success(&hosts, "rkat mob hosts (pre-bind)");
    let hosts_doc: serde_json::Value =
        serde_json::from_str(stdout_str(&hosts).trim()).expect("hosts JSON");
    assert_eq!(
        hosts_doc["hosts"],
        serde_json::json!([]),
        "no hosts before the bind ceremony"
    );

    // ---- bind-host over real loopback TCP ----
    let bound = console
        .run_rkat(
            &rkat,
            &[
                "mob",
                "bind-host",
                MOB_ID,
                "--descriptor",
                home.descriptor_path().to_str().expect("utf8"),
            ],
        )
        .await;
    assert_success(&bound, "rkat mob bind-host");
    let report: serde_json::Value =
        serde_json::from_str(stdout_str(&bound).trim()).expect("bind report JSON");
    let host_id = report["host_id"].as_str().expect("host_id").to_string();
    assert!(!host_id.is_empty());
    assert!(
        report["authority_epoch"].as_u64().is_some(),
        "bind report carries the authority epoch"
    );
    assert!(
        report.get("epoch").is_none(),
        "bind report must use the canonical MobBindHostResult field name"
    );
    assert!(
        report["capabilities"]["durable_sessions"].is_boolean(),
        "bind report carries the capability record"
    );

    // ---- hosts: bound row with capability labeling ----
    let hosts = console
        .run_rkat(&rkat, &["mob", "hosts", MOB_ID, "--json"])
        .await;
    assert_success(&hosts, "rkat mob hosts (post-bind)");
    let hosts_doc: serde_json::Value =
        serde_json::from_str(stdout_str(&hosts).trim()).expect("hosts JSON");
    let rows = hosts_doc["hosts"].as_array().expect("hosts array");
    assert_eq!(rows.len(), 1, "exactly the bound host");
    assert_eq!(rows[0]["host_id"], serde_json::json!(host_id));
    assert_eq!(rows[0]["bind_phase"], "bound");
    assert!(rows[0]["capabilities"]["durable_sessions"].is_boolean());

    // Human mode renders the tab row with the durable_sessions label.
    let human = console.run_rkat(&rkat, &["mob", "hosts", MOB_ID]).await;
    assert_success(&human, "rkat mob hosts (human)");
    let human_out = stdout_str(&human);
    assert!(
        human_out.contains("durable_sessions=") && human_out.contains(&host_id),
        "human hosts row labels durable_sessions: {human_out}"
    );

    // ---- route-installs: nothing outstanding ⇒ complete ----
    let installs = console
        .run_rkat(&rkat, &["mob", "route-installs", MOB_ID])
        .await;
    assert_success(&installs, "rkat mob route-installs");
    let installs_doc: serde_json::Value =
        serde_json::from_str(stdout_str(&installs).trim()).expect("route installs JSON");
    assert_eq!(installs_doc["complete"], serde_json::json!(true));
    assert_eq!(installs_doc["outstanding"], serde_json::json!([]));

    // ---- member-history: verb wired; missing member is a deliberately
    // UNMAPPED error ⇒ EXIT_ERROR=1, no typed detail line ----
    let history = console
        .run_rkat(&rkat, &["mob", "member-history", MOB_ID, "no-such-member"])
        .await;
    assert_eq!(
        history.status.code(),
        Some(1),
        "missing member keeps EXIT_ERROR (unmapped by design):\n{}",
        stderr_str(&history),
    );
    let history_err = stderr_str(&history);
    assert!(
        history_err.contains("mob member not found"),
        "typed message surfaces: {history_err}"
    );
    assert!(
        !history_err.contains("detail:"),
        "unmapped errors carry no typed detail line: {history_err}"
    );

    // ---- revoke-host: typed report, no fabricated releases ----
    let revoked = console
        .run_rkat(&rkat, &["mob", "revoke-host", MOB_ID, host_id.as_str()])
        .await;
    assert_success(&revoked, "rkat mob revoke-host");
    let revoke_doc: serde_json::Value =
        serde_json::from_str(stdout_str(&revoked).trim()).expect("revoke report JSON");
    assert_eq!(revoke_doc["host_id"], serde_json::json!(host_id));
    assert_eq!(
        revoke_doc["released_members"],
        serde_json::json!([]),
        "no materialized members were placed on the host"
    );

    // ---- §17.4 observed: bind against a STOPPED daemon fails at the COMMS
    // ack layer (peer offline) BEFORE any bridge reply deadline engages —
    // and the ratified DEC-P7B-8 mapping deliberately pins CommsError ∌
    // HostUnavailable (a member-transport fact must not launder into a
    // host-availability claim). The honest console outcome is therefore the
    // UNMAPPED mob-family exit (1) with the raw typed message, NOT 46.
    // Exit 46 (HostUnavailable ← bridge reply deadline / typed Unavailable
    // rejection) is pinned at unit level (mob_exit_code) and via the MCP
    // dispatch rows per FLAG-WB-5's recorded disposition: a delivered-but-
    // unanswered bridge request is not deterministically manufacturable in
    // this lane without fault injection.
    let d1 = home
        .wait_for_descriptor(Some(d0.bootstrap_token.as_str()), &mut daemon)
        .await;
    let _ = d1;
    home.signal(&daemon, "-STOP");
    let timed_out = console
        .run_rkat(
            &rkat,
            &[
                "mob",
                "bind-host",
                MOB_ID,
                "--descriptor",
                home.descriptor_path().to_str().expect("utf8"),
            ],
        )
        .await;
    home.signal(&daemon, "-CONT");
    assert_eq!(
        timed_out.status.code(),
        Some(1),
        "stopped-daemon bind fails comms-class, honestly unmapped:\nstdout: {}\nstderr: {}",
        stdout_str(&timed_out),
        stderr_str(&timed_out),
    );
    let timed_out_err = stderr_str(&timed_out);
    assert!(
        timed_out_err.contains("comms error"),
        "the typed comms cause reaches stderr un-laundered: {timed_out_err}"
    );

    shutdown(daemon).await;
}