liminal-cli 0.3.32

The liminal command: serve the network, maintain its stores, or serve its discovery MCP over stdio
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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! The installed command is exercised through its readiness and stop lines.

use std::error::Error;
use std::fmt::Write as _;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::os::unix::fs::PermissionsExt as _;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc::{self, Receiver, Sender};
use std::time::Duration;

type Result<T = ()> = std::result::Result<T, Box<dyn Error>>;
const GUARD: Duration = Duration::from_secs(60);
static NEXT: AtomicU64 = AtomicU64::new(0);

struct Home(PathBuf);
impl Home {
    fn new() -> Result<Self> {
        let path = std::env::temp_dir().join(format!(
            "liminal-up-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        ));
        std::fs::create_dir(&path)?;
        let bin = path.join("bin");
        std::fs::create_dir(&bin)?;
        for name in ["open", "xdg-open"] {
            let opener = bin.join(name);
            std::fs::write(&opener, "#!/bin/sh\nexit 0\n")?;
            std::fs::set_permissions(opener, std::fs::Permissions::from_mode(0o700))?;
        }
        // The generated configuration names the catalogued harness by program; this stand-in comes
        // first on PATH so a test never starts a real Claude Code on the machine running it.
        let harness = bin.join("claude-agent-acp");
        std::fs::write(
            &harness,
            "#!/bin/sh\necho 'test stand-in: no Claude Code in tests' >&2\nexit 1\n",
        )?;
        std::fs::set_permissions(harness, std::fs::Permissions::from_mode(0o700))?;
        Ok(Self(path))
    }
}
impl Drop for Home {
    fn drop(&mut self) {
        if let Err(error) = std::fs::remove_dir_all(&self.0) {
            eprintln!("up test HOME cleanup: {error}");
        }
    }
}

fn binary() -> PathBuf {
    std::env::var_os("LIMINAL_UP_TEST_BINARY").map_or_else(
        || {
            option_env!("CARGO_BIN_EXE_liminal")
                .map_or_else(|| PathBuf::from("liminal"), PathBuf::from)
        },
        PathBuf::from,
    )
}

fn command(home: &Home) -> Command {
    let mut command = Command::new(binary());
    let mut paths = vec![home.0.join("bin")];
    if let Some(path) = std::env::var_os("PATH") {
        paths.extend(std::env::split_paths(&path));
    }
    if let Ok(path) = std::env::join_paths(paths) {
        command.env("PATH", path);
    }
    command
        .arg("up")
        .env("HOME", &home.0)
        .env_remove("LIMINAL_PERSISTENCE_PATH")
        .env_remove("LIMINAL_LISTEN_ADDRESS")
        .env_remove("LIMINAL_HEALTH_LISTEN_ADDRESS")
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    command
}

struct Process {
    child: Child,
    lines: Receiver<std::io::Result<String>>,
    /// Lets a tap's records join `up`'s own lines, so a wait ends on whichever comes first.
    send: Sender<std::io::Result<String>>,
}
impl Process {
    fn start(command: &mut Command) -> Result<Self> {
        let mut child = command.spawn()?;
        let stdout = child.stdout.take().ok_or("up stdout absent")?;
        let (send, lines) = mpsc::channel();
        let joined = send.clone();
        std::thread::spawn(move || {
            for line in BufReader::new(stdout).lines() {
                if send.send(line).is_err() {
                    return;
                }
            }
        });
        Ok(Self {
            child,
            lines,
            send: joined,
        })
    }
    /// Forwards the tap's next record as the line `reply <record>`.
    fn forward(&self, reader: Box<dyn liminal_cli::network::TapReader>) {
        let send = self.send.clone();
        std::thread::spawn(move || {
            let line = reader
                .receive()
                .map(|record| format!("reply {record}"))
                .map_err(std::io::Error::other);
            if send.send(line).is_err() {
                eprintln!("the test stopped reading before the reply arrived");
            }
        });
    }
    fn line(&self) -> Result<String> {
        Ok(self.lines.recv_timeout(GUARD)??)
    }
    fn stop(&mut self) -> Result {
        let status = Command::new("kill")
            .args(["-INT", &self.child.id().to_string()])
            .status()?;
        assert!(status.success());
        assert_eq!(self.line()?, "assistant stopped");
        assert_eq!(self.line()?, "console stopped");
        assert_eq!(self.line()?, "exchange stopped");
        assert!(self.child.wait()?.success());
        Ok(())
    }
}
impl Drop for Process {
    fn drop(&mut self) {
        if matches!(self.child.try_wait(), Ok(None)) {
            if let Err(error) = self.child.kill() {
                eprintln!("up cleanup kill: {error}");
            }
        }
        if let Err(error) = self.child.wait() {
            eprintln!("up cleanup wait: {error}");
        }
    }
}

fn get(address: SocketAddr, path: &str, method: &str) -> Result<Vec<u8>> {
    let mut stream = TcpStream::connect_timeout(&address, GUARD)?;
    stream.set_read_timeout(Some(GUARD))?;
    write!(
        stream,
        "{method} {path} HTTP/1.1\r\nHost: {address}\r\nConnection: close\r\n\r\n"
    )?;
    let mut bytes = Vec::new();
    stream.read_to_end(&mut bytes)?;
    Ok(bytes)
}

#[test]
fn fresh_home_is_private_explicit_and_reused() -> Result {
    let _ports = default_ports()?;
    let home = Home::new()?;
    let config = home.0.join(".liminal/liminal.toml");
    let mut server = Process::start(&mut command(&home))?;
    assert_eq!(server.line()?, "http://127.0.0.1:7480/");
    let first = std::fs::read(&config)?;
    let modified = std::fs::metadata(&config)?.modified()?;
    assert_eq!(
        std::fs::metadata(&config)?.permissions().mode() & 0o777,
        0o600
    );
    assert_eq!(
        std::fs::metadata(home.0.join(".liminal"))?
            .permissions()
            .mode()
            & 0o777,
        0o700
    );
    let text = std::str::from_utf8(&first)?;
    for key in [
        "extra_listen_addresses",
        "drain_timeout_ms",
        "routing_rules",
        "max_connection_outbound_bytes",
        "reply_timeout_ms",
        "cost_rows",
        "events_rows",
        "[agents]",
        "[participant]",
        "[console]",
    ] {
        assert!(text.contains(key), "generated configuration omits {key}");
    }
    assert!(home.0.join(".liminal/store").is_dir());
    server.stop()?;
    let mut second = Process::start(&mut command(&home))?;
    assert_eq!(second.line()?, "http://127.0.0.1:7480/");
    assert_eq!(std::fs::read(&config)?, first);
    assert_eq!(std::fs::metadata(&config)?.modified()?, modified);
    second.stop()
}

#[test]
fn held_console_port_refuses_before_creating_home_configuration() -> Result {
    let _ports = default_ports()?;
    let held = TcpListener::bind("127.0.0.1:7480")?;
    let home = Home::new()?;
    let output = command(&home).output()?;
    assert!(!output.status.success());
    let words = String::from_utf8(output.stderr)?;
    assert!(
        words.contains("7480") && words.contains("console.listen"),
        "{words}"
    );
    assert!(!home.0.join(".liminal").exists());
    drop(held);
    Ok(())
}

fn address() -> Result<SocketAddr> {
    Ok(TcpListener::bind("127.0.0.1:0")?.local_addr()?)
}

fn existing(home: &Home, console: Option<SocketAddr>) -> Result<(PathBuf, SocketAddr, SocketAddr)> {
    let wire = address()?;
    let health = address()?;
    let path = home.0.join("existing.toml");
    let console = console.map_or_else(String::new, |listen| {
        format!("\n[console]\nlisten = '{listen}'\n")
    });
    let store = home.0.join("existing-store");
    std::fs::create_dir(&store)?;
    std::fs::write(
        &path,
        format!(
            "listen_address = '{wire}'\nhealth_listen_address = '{health}'\ndrain_timeout_ms = 0\nchannels = []\nrouting_rules = []\npersistence_path = '{}'\n{console}",
            store.display()
        ),
    )?;
    Ok((path, wire, health))
}

#[test]
fn existing_configuration_without_console_keeps_the_legacy_listeners() -> Result {
    let _ports = default_ports()?;
    let home = Home::new()?;
    let (path, wire, health) = existing(&home, None)?;
    let bytes = std::fs::read(&path)?;
    let mut cmd = command(&home);
    cmd.arg("--config").arg(&path);
    let mut child = cmd.spawn()?;
    let stderr = child.stderr.take().ok_or("up stderr absent")?;
    let (send, lines) = mpsc::channel();
    let joined = send.clone();
    std::thread::spawn(move || {
        for line in BufReader::new(stderr).lines() {
            if send.send(line).is_err() {
                return;
            }
        }
    });
    let server = Process {
        child,
        lines,
        send: joined,
    };
    loop {
        if server.line()?.contains("liminal server started") {
            break;
        }
    }
    assert!(TcpStream::connect(wire).is_ok());
    assert!(String::from_utf8(get(health, "/health", "GET")?)?.contains("200"));
    assert!(!home.0.join(".liminal").exists());
    assert_eq!(std::fs::read(path)?, bytes);
    Ok(())
}

#[test]
fn existing_configuration_with_console_serves_its_written_address() -> Result {
    let home = Home::new()?;
    let listen = address()?;
    let (path, _, _) = existing(&home, Some(listen))?;
    let mut cmd = command(&home);
    cmd.arg("--config").arg(path);
    let mut server = Process::start(&mut cmd)?;
    assert_eq!(server.line()?, format!("http://{listen}/"));
    assert!(String::from_utf8(get(listen, "/console.json", "GET")?)?.contains("\"signin\":false"));
    server.stop()
}

#[test]
fn embedded_console_serves_one_origin_and_named_refusals() -> Result {
    let home = Home::new()?;
    let listen = address()?;
    let (path, _, _) = existing(&home, Some(listen))?;
    let mut cmd = command(&home);
    cmd.arg("--config").arg(path);
    let mut server = Process::start(&mut cmd)?;
    assert_eq!(server.line()?, format!("http://{listen}/"));
    let page = get(listen, "/", "GET")?;
    assert!(page.starts_with(b"HTTP/1.1 200"));
    assert!(String::from_utf8_lossy(&page).contains("<html"));
    let placement = get(listen, "/console.json", "GET")?;
    assert!(String::from_utf8_lossy(&placement).ends_with("{\"signin\":false,\"lys\":null}"));
    for (path, method, status, words) in [
        ("/exchange", "GET", "426", "WebSocket upgrade"),
        ("/../outside", "GET", "404", "outside the embedded dist"),
        ("/", "POST", "405", "only GET"),
    ] {
        let response = String::from_utf8(get(listen, path, method)?)?;
        assert!(
            response.starts_with(&format!("HTTP/1.1 {status}")) && response.contains(words),
            "{response}"
        );
    }
    server.stop()
}

#[test]
fn sigint_reports_ordered_stops_and_the_next_up_reopens_the_store() -> Result {
    let home = Home::new()?;
    let listen = address()?;
    let (path, _, _) = existing(&home, Some(listen))?;
    let mut cmd = command(&home);
    cmd.arg("--config").arg(&path);
    let mut server = Process::start(&mut cmd)?;
    assert_eq!(server.line()?, format!("http://{listen}/"));
    server.stop()?;
    let mut next = Process::start(&mut cmd)?;
    assert_eq!(next.line()?, format!("http://{listen}/"));
    next.stop()
}

#[test]
fn missing_assistant_harness_keeps_the_console_serving() -> Result {
    let home = Home::new()?;
    let (path, _, listen) = full_configuration(&home, Some(Path::new("/absent/con022-harness")))?;
    let mut cmd = command(&home);
    cmd.arg("--config").arg(path);
    let mut server = Process::start(&mut cmd)?;
    assert_eq!(server.line()?, format!("http://{listen}/"));
    assert!(get(listen, "/console.json", "GET")?.starts_with(b"HTTP/1.1 200"));
    server.stop()
}

fn default_ports() -> Result<std::fs::File> {
    let lock = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(std::env::temp_dir().join("liminal-con022-default-ports.lock"))?;
    rustix::fs::flock(&lock, rustix::fs::FlockOperation::LockExclusive)?;
    Ok(lock)
}

fn full_configuration(
    home: &Home,
    harness: Option<&Path>,
) -> Result<(PathBuf, SocketAddr, SocketAddr)> {
    let listen = address()?;
    let (path, wire, _) = existing(home, Some(listen))?;
    let mut text = std::fs::read_to_string(&path)?;
    text.push_str(
        r"
[limits]
max_channels = 512
[agents]
[participant]
wire_frame_limit = 65536
attach_receipt_ttl_ms = 60000
receipt_provenance_ttl_ms = 600000
live_receipt_server_report_threshold = 1024
max_live_attach_receipts_per_participant = 8
receipt_provenance_server_report_threshold = 4096
receipt_provenance_per_conversation_report_threshold = 256
max_receipt_provenance_per_participant = 64
max_retired_identity_slots_server = 1024
identity_slots = 4
observer_recovery_max_entries = 64
max_semantic_conversations_per_connection = 32
max_ordinary_record_entries = 1
max_ordinary_record_bytes = 131072
max_generated_marker_entries = 1
max_generated_marker_bytes = 4096
mandatory_transaction_bound_entries = 4
mandatory_transaction_bound_bytes = 16384
full_recovery_claim_entries = 4
full_recovery_claim_bytes = 16384
retained_capacity_entries = 2048
retained_capacity_bytes = 16777216
max_retained_record_rows = 1024
closure_episode_churn_limit = 1024
",
    );
    if let Some(harness) = harness {
        text = text.replace("[console]\n", "[console]\nassistant = true\n");
        let program = toml::Value::String(
            harness
                .to_str()
                .ok_or("harness path is not Unicode")?
                .to_owned(),
        );
        writeln!(
            text,
            "\n[console.harness]\nid = 'claude-code'\nprogram = {program}\nargs = []"
        )?;
    }
    std::fs::write(&path, text)?;
    Ok((path, wire, listen))
}

fn rail(server: SocketAddr) -> Result<liminal_cli::Live> {
    let ticket = liminal_cli::Config {
        server: server.to_string(),
        channel: "console.topology".to_owned(),
        credential: liminal_cli::Credential::Open {
            name: Some("checker".to_owned()),
        },
        agent: Some("checker".to_owned()),
        listen: liminal_cli::config::ListenConfig::default(),
        limits: liminal_cli::Limits {
            frame_bytes: 65_536,
            record_bytes: 1_048_576,
            reply_timeout_ms: 10_000,
            flow_steps: 8,
            request_bytes: 65_536,
            connect_timeout_ms: 2000,
            answer_bytes: 16_777_216,
        },
    };
    Ok(liminal_cli::Live::new(&ticket).map_err(std::io::Error::other)?)
}

#[test]
fn websocket_at_the_console_origin_subscribes_the_production_topology() -> Result {
    let home = Home::new()?;
    let (path, _, listen) = full_configuration(&home, None)?;
    let mut cmd = command(&home);
    cmd.arg("--config").arg(path);
    let mut server = Process::start(&mut cmd)?;
    assert_eq!(server.line()?, format!("http://{listen}/"));
    let stream = liminal_sdk::remote::websocket::WebSocketSubscriptionStream::open(
        &format!("ws://{listen}/exchange"),
        "console.topology",
        Vec::new(),
    )?;
    let record = stream.recv_timeout(GUARD)?;
    let value: serde_json::Value = serde_json::from_slice(record.payload())?;
    assert!(value.is_object());
    drop(stream);
    server.stop()
}

#[test]
fn absent_harness_presence_carries_its_words_and_the_signin_action() -> Result {
    use liminal_cli::Rail as _;
    let home = Home::new()?;
    let (path, wire, listen) =
        full_configuration(&home, Some(Path::new("/absent/con022-harness")))?;
    let mut cmd = command(&home);
    cmd.arg("--config").arg(path);
    let mut server = Process::start(&mut cmd)?;
    assert_eq!(server.line()?, format!("http://{listen}/"));
    let watch = rail(wire)?.tap("agents.assistant.presence")?;
    let presence = watch.reader.receive()?;
    assert_eq!(presence["present"], false);
    let reason = presence["reason"]
        .as_str()
        .ok_or("presence reason absent")?;
    assert!(reason.contains("harness.program unavailable"), "{presence}");
    assert!(reason.contains("the assistant needs Claude Code signed in on this computer: run claude once and sign in, then start liminal up again"));
    watch.closer.close()?;
    assert!(get(listen, "/", "GET")?.starts_with(b"HTTP/1.1 200"));
    server.stop()
}

fn harness(home: &Home, refused: bool) -> Result<PathBuf> {
    let path = home.0.join("harness.sh");
    let turn = if refused {
        r#"printf '{"jsonrpc":"2.0","id":%s,"error":{"code":-32603,"message":"Claude Code fixture: sign in required"}}\n' "$id""#
    } else {
        r#"printf '%s\n' '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"fixture","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"fixture answer"}}}}'
printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id""#
    };
    std::fs::write(
        &path,
        format!(
            r#"#!/bin/sh
while IFS= read -r frame; do
  id=${{frame#*\"id\":}}
  id=${{id%%,*}}
  case "$frame" in
    *'"method":"initialize"'*) printf '%s\n' '{{"jsonrpc":"2.0","id":1,"result":{{"protocolVersion":1,"agentCapabilities":{{"loadSession":false}}}}}}' ;;
    *'"method":"session/new"'*) printf '%s\n' '{{"jsonrpc":"2.0","id":2,"result":{{"sessionId":"fixture"}}}}' ;;
    *'"method":"session/prompt"'*) {turn} ;;
  esac
done
"#
        ),
    )?;
    std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o700))?;
    Ok(path)
}

fn assistant_turn(refused: bool) -> Result {
    use liminal_cli::Rail as _;
    let home = Home::new()?;
    let program = harness(&home, refused)?;
    let (path, wire, listen) = full_configuration(&home, Some(&program))?;
    let mut cmd = command(&home);
    cmd.arg("--config").arg(path);
    let mut server = Process::start(&mut cmd)?;
    assert_eq!(server.line()?, format!("http://{listen}/"));
    let rail = rail(wire)?;
    let liminal_cli::network::TapSubscription {
        reader: own,
        closer: own_closer,
    } = rail.tap("agents.checker.inbox")?;
    let presence = rail.tap("agents.assistant.presence")?;
    assert_eq!(presence.reader.receive()?["present"], true);
    let client = liminal_sdk::PushClient::connect_with_auth(&wire.to_string(), b"open:checker")?;
    client.publish(
        "agents.assistant.inbox",
        // the sender names itself, the exchange checks the claim against its holder, and the
        // reply goes to the sender's inbox (CON-022 R3)
        br#"{"text":"a fixture line","from":"checker"}"#.to_vec(),
    )?;
    let closed = client.close()?;
    assert!(closed.is_proven_accepted(), "{closed:?}");
    if refused {
        let failure = presence.reader.receive()?;
        assert!(
            failure["reason"]
                .as_str()
                .is_some_and(|words| words.contains("Claude Code fixture: sign in required")),
            "{failure}"
        );
    } else {
        // The reply, or `up` saying the answer went nowhere: whichever happens first.
        server.forward(own);
        let line = server.line()?;
        let reply: serde_json::Value =
            serde_json::from_str(line.strip_prefix("reply ").ok_or_else(|| line.clone())?)?;
        assert_eq!(reply["text"], "fixture answer", "{line}");
    }
    own_closer.close()?;
    presence.closer.close()?;
    server.stop()
}

#[test]
fn the_sh_harness_answers_the_senders_inbox() -> Result {
    assistant_turn(false)
}

#[test]
fn a_first_turn_refusal_is_shown_in_the_harness_words() -> Result {
    assistant_turn(true)
}