liminal-cli 0.3.30

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
//! SDK-017 R1: every missing or zero field is refused by name (the server
//! alone defaults, to the central exchange, AGT-006 R3), and the pass's bytes
//! appear in no error and no Debug.

use std::error::Error;
use std::path::Path;

use super::{Config, Credential};

const FIELDS: [&str; 8] = [
    "server",
    "channel",
    "token_file",
    "frame_bytes",
    "record_bytes",
    "reply_timeout_ms",
    "flow_steps",
    "request_bytes",
];

fn written(token: &Path, without: Option<&str>, zero: Option<&str>) -> String {
    let mut top = String::new();
    let mut limits = String::new();
    for field in FIELDS {
        if without == Some(field) {
            continue;
        }
        let value = match field {
            "server" => "\"127.0.0.1:4100\"".to_owned(),
            "channel" => "\"events\"".to_owned(),
            "token_file" => format!("{:?}", token.display().to_string()),
            _ if zero == Some(field) => "0".to_owned(),
            _ => "16".to_owned(),
        };
        if matches!(field, "server" | "channel" | "token_file") {
            top.extend([field, " = ", value.as_str(), "\n"]);
        } else {
            limits.extend([field, " = ", value.as_str(), "\n"]);
        }
    }
    format!("{top}\n[limits]\n{limits}")
}

#[test]
fn config_refuses_each_missing_or_zero_field_by_name() -> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir()?;
    let token = dir.path().join("token");
    std::fs::write(&token, "pass-bytes\n")?;
    let path = dir.path().join("ticket.toml");
    // AGT-006 R3: a seat that names no server is the central exchange.
    std::fs::write(&path, written(&token, Some("server"), None))?;
    assert_eq!(Config::load(&path)?.server, crate::seat::CENTRAL_SERVER);
    // ACCESS-010 R3: token_file may be absent (an open seat); every other
    // field is still named when missing.
    for field in FIELDS[1..].iter().filter(|field| **field != "token_file") {
        std::fs::write(&path, written(&token, Some(field), None))?;
        let refusal = Config::load(&path).err().map(|e| e.to_string());
        let refusal = refusal.ok_or("a missing field was accepted")?;
        assert!(refusal.contains(field), "missing {field}: {refusal}");
    }
    for field in &FIELDS[3..] {
        std::fs::write(&path, written(&token, None, Some(field)))?;
        let refusal = Config::load(&path).err().map(|e| e.to_string());
        let refusal = refusal.ok_or("a zero field was accepted")?;
        assert!(refusal.contains(field), "zero {field}: {refusal}");
    }
    std::fs::write(&path, written(&token, None, None))?;
    let config = Config::load(&path)?;
    assert_eq!(config.limits.flow_steps, 16);
    let Credential::Token(token) = &config.credential else {
        return Err("a token_file configuration did not hold a token".into());
    };
    assert_eq!(token.as_str(), "pass-bytes");
    Ok(())
}

#[test]
fn pass_bytes_appear_in_no_debug_and_no_error() -> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir()?;
    let token = dir.path().join("token");
    std::fs::write(&token, "distinct-pass-7f3a\n")?;
    let path = dir.path().join("ticket.toml");
    std::fs::write(&path, written(&token, None, None))?;
    let config = Config::load(&path)?;
    let debug = format!("{config:?}");
    assert!(!debug.contains("distinct-pass-7f3a"), "{debug}");
    assert!(debug.contains("Token(18 bytes)"), "{debug}");
    std::fs::write(&token, "")?;
    let refusal = Config::load(&path).err().map(|e| e.to_string());
    let refusal = refusal.ok_or("an empty token was accepted")?;
    assert!(refusal.contains("token file is empty"), "{refusal}");
    Ok(())
}

#[test]
fn agt001_listen_defaults_and_every_zero_bound_are_named() -> Result<(), Box<dyn Error>> {
    let directory = tempfile::tempdir()?;
    let token = directory.path().join("token");
    std::fs::write(&token, "fixture-pass")?;
    let path = directory.path().join("seat.toml");
    std::fs::write(&path, written(&token, None, None))?;
    let config = Config::load(&path)?;
    assert_eq!(config.listen.listen_bound, 16);
    assert_eq!(config.listen.content_bytes, 65536);
    for field in [
        "listen_bound",
        "content_bytes",
        "join_bound",
        "join_bytes",
        "batch_messages",
        "subject_bytes",
        "key_bytes",
        "agent_bytes",
        "issuer_response_bytes",
        "run_pass_bytes",
    ] {
        let text = format!("{}\n[listen]\n{field} = 0\n", written(&token, None, None));
        std::fs::write(&path, text)?;
        let error = Config::load(&path)
            .err()
            .ok_or("a zero listen bound was accepted")?;
        assert!(
            error.to_string().contains(field),
            "the listen refusal did not name its bound"
        );
    }
    Ok(())
}

#[test]
fn agt001_listen_refuses_unknown_fields_and_non_subject_text() -> Result<(), Box<dyn Error>> {
    let directory = tempfile::tempdir()?;
    let token = directory.path().join("token");
    std::fs::write(&token, "fixture-pass")?;
    let path = directory.path().join("seat.toml");
    for table in [
        "[listen]\nsubjects = [\"a..b\"]\n",
        "[listen]\nunknown = true\n",
    ] {
        std::fs::write(&path, format!("{}\n{table}", written(&token, None, None)))?;
        assert!(
            Config::load(&path).is_err(),
            "an invalid listen table was accepted"
        );
    }
    Ok(())
}

#[test]
fn access010_an_open_seat_carries_no_credential_and_reaches_loopback_only()
-> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir()?;
    let token = dir.path().join("token");
    let path = dir.path().join("seat.toml");
    for server in ["127.0.0.1:4100", "localhost:7900", "ws://[::1]:7902/ws"] {
        let text = written(&token, Some("token_file"), None).replace("127.0.0.1:4100", server);
        std::fs::write(&path, text)?;
        let config = Config::load(&path)?;
        assert!(
            matches!(config.credential, Credential::Open { .. }),
            "{config:?}"
        );
        assert_eq!(config.server, server);
    }
    assert!(!token.exists(), "an open seat reads no token file");
    for server in ["192.168.50.40:7900", "wss://liminal.ablative.com.au/ws"] {
        let text = written(&token, Some("token_file"), None).replace("127.0.0.1:4100", server);
        std::fs::write(&path, text)?;
        let refusal = Config::load(&path).err().map(|e| e.to_string());
        let refusal = refusal.ok_or("an open seat off loopback was accepted")?;
        assert!(
            refusal.contains(&format!(
                "an open seat (no token_file, no [lys]) reaches a loopback exchange only: server {server}"
            )),
            "{refusal}"
        );
    }
    // Both credentials given is still refused with its own words.
    std::fs::write(&token, "pass-bytes\n")?;
    let both = format!(
        "{}\n[lys]\nissuer = \"http://127.0.0.1:8490\"\nagent = \"agent-x\"\n",
        written(&token, None, None)
    );
    std::fs::write(&path, both)?;
    let refusal = Config::load(&path).err().map(|e| e.to_string());
    let refusal = refusal.ok_or("two credentials were accepted")?;
    assert!(
        refusal.contains("give exactly one of token_file and [lys]"),
        "{refusal}"
    );
    Ok(())
}

#[test]
fn access011_an_open_seat_reaching_the_network_loads_off_loopback() -> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir()?;
    let token = dir.path().join("token");
    let path = dir.path().join("seat.toml");
    for server in ["192.168.50.40:7900", "ws://liminal.lan:7902/ws"] {
        let text = format!(
            "{}\n[open]\nreach = \"network\"\n",
            written(&token, Some("token_file"), None).replace("127.0.0.1:4100", server)
        );
        std::fs::write(&path, text)?;
        let config = Config::load(&path)?;
        assert!(
            matches!(config.credential, Credential::Open { .. }),
            "{config:?}"
        );
        assert_eq!(config.server, server);
    }
    // reach = "loopback" written out keeps the ACCESS-010 refusal.
    let text = format!(
        "{}\n[open]\nreach = \"loopback\"\n",
        written(&token, Some("token_file"), None).replace("127.0.0.1:4100", "192.168.50.40:7900")
    );
    std::fs::write(&path, text)?;
    let refusal = Config::load(&path).err().map(|e| e.to_string());
    let refusal = refusal.ok_or("reach = \"loopback\" off loopback was accepted")?;
    assert!(
        refusal.contains("reaches a loopback exchange only: server 192.168.50.40:7900"),
        "{refusal}"
    );
    Ok(())
}

#[test]
fn access011_open_beside_a_credential_and_an_unknown_reach_are_refused_by_name()
-> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir()?;
    let token = dir.path().join("token");
    let path = dir.path().join("seat.toml");
    std::fs::write(&token, "pass-bytes\n")?;
    let text = format!(
        "{}\n[open]\nreach = \"network\"\n",
        written(&token, None, None)
    );
    std::fs::write(&path, text)?;
    let refusal = Config::load(&path).err().map(|e| e.to_string());
    let refusal = refusal.ok_or("[open] beside token_file was accepted")?;
    assert!(
        refusal.contains(
            "[open] names a seat with no credential; this seat has token_file or [lys], remove one"
        ),
        "{refusal}"
    );
    let text = format!(
        "{}\n[open]\nreach = \"anywhere\"\n",
        written(&token, Some("token_file"), None)
    );
    std::fs::write(&path, text)?;
    let refusal = Config::load(&path).err().map(|e| e.to_string());
    let refusal = refusal.ok_or("reach = \"anywhere\" was accepted")?;
    for word in ["anywhere", "loopback", "network"] {
        assert!(refusal.contains(word), "{word}: {refusal}");
    }
    Ok(())
}

// ---- ACCESS-012 R2 (config_tests.rs) ----

#[test]
fn access012_an_open_seat_declares_its_name() -> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("seat.toml");
    std::fs::write(
        &path,
        format!(
            "{}\n[open]\nname = \"hermes\"\n",
            written(&dir.path().join("token"), Some("token_file"), None)
                .replace("127.0.0.1:4100", "127.0.0.1:7900")
        ),
    )?;
    let config = Config::load(&path)?;
    assert!(
        matches!(&config.credential, Credential::Open { name: Some(name) } if name == "hermes"),
        "{config:?}"
    );
    assert_eq!(config.credential.holder(), "open:hermes");
    assert_eq!(config.credential.open_record(), b"open:hermes".to_vec());

    let bare = dir.path().join("bare.toml");
    std::fs::write(
        &bare,
        written(&dir.path().join("token"), Some("token_file"), None)
            .replace("127.0.0.1:4100", "127.0.0.1:7900"),
    )?;
    let config = Config::load(&bare)?;
    assert!(
        matches!(&config.credential, Credential::Open { name: None }),
        "{config:?}"
    );
    assert_eq!(config.credential.holder(), "open:anonymous");
    assert!(config.credential.open_record().is_empty());
    Ok(())
}

#[test]
fn access012_an_invalid_open_name_is_refused_naming_the_member() -> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir()?;
    for bad in ["Hermes", "her mes", "", "a/b", &"x".repeat(65)] {
        let path = dir.path().join("seat.toml");
        std::fs::write(
            &path,
            format!(
                "{}\n[open]\nname = \"{bad}\"\n",
                written(&dir.path().join("token"), Some("token_file"), None)
                    .replace("127.0.0.1:4100", "127.0.0.1:7900")
            ),
        )?;
        let refusal = Config::load(&path)
            .err()
            .ok_or_else(|| format!("name {bad:?} was accepted"))?;
        assert!(
            refusal
                .to_string()
                .contains("[open] name must be 1 to 64 of a-z, 0-9, '.', '_' or '-'"),
            "{bad:?}: {refusal}"
        );
    }
    Ok(())
}

/// Waffles' brief of 25 Oct (Tom: "can we have a generic one and set id at
/// runtime?"): `liminal mcp`'s flags name the seat, each winning over the
/// file, and with no file the seat-init defaults stand under them.
mod runtime_identity {
    use std::error::Error;

    use super::super::{Config, Credential, Overrides};

    const OPEN: &str = "server = \"127.0.0.1:4100\"\nchannel = \"events\"\n\n[open]\nname = \"ada\"\n\n\
                        [limits]\nframe_bytes = 16\nrecord_bytes = 16\nreply_timeout_ms = 16\n\
                        flow_steps = 16\nrequest_bytes = 16\n";
    const LYS: &str = "server = \"127.0.0.1:4100\"\nchannel = \"events\"\n\n[lys]\nissuer = \"http://127.0.0.1:4300\"\n\
                       agent = \"agent-ada\"\nkey_file = \"/keys/ada\"\n\n[limits]\nframe_bytes = 16\n\
                       record_bytes = 16\nreply_timeout_ms = 16\nflow_steps = 16\nrequest_bytes = 16\n";

    fn refused(
        path: Option<&std::path::Path>,
        overrides: &Overrides,
    ) -> Result<String, Box<dyn Error>> {
        Ok(Config::load_with(path, overrides)
            .err()
            .ok_or("the flags were accepted")?
            .to_string())
    }

    #[test]
    fn a_flag_beside_the_file_wins_over_its_value() -> Result<(), Box<dyn Error>> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("seat.toml");
        std::fs::write(&path, OPEN)?;
        let overrides = Overrides {
            server: Some("127.0.0.1:4200".into()),
            channel: Some("other".into()),
            name: Some("bea".into()),
            ..Overrides::default()
        };
        let config = Config::load_with(Some(&path), &overrides)?;
        assert_eq!(config.server, "127.0.0.1:4200");
        assert_eq!(config.channel, "other");
        assert!(
            matches!(&config.credential, Credential::Open { name: Some(name) } if name == "bea"),
            "{:?}",
            config.credential
        );
        // The declared name is the seat's station, placed on its first listen.
        assert_eq!(config.agent.as_deref(), Some("bea"));
        assert_eq!(config.limits.frame_bytes, 16);
        // Without flags the file is read as it always was.
        let config = Config::load_with(Some(&path), &Overrides::default())?;
        assert_eq!(config.agent.as_deref(), Some("ada"));
        Ok(())
    }

    #[test]
    fn with_no_file_the_flags_and_the_seat_init_defaults_make_the_seat()
    -> Result<(), Box<dyn Error>> {
        // Off loopback the open seat reaches the network, as seat init --open writes.
        let overrides = Overrides {
            server: Some("100.64.0.7:7600".into()),
            name: Some("tom".into()),
            ..Overrides::default()
        };
        let config = Config::load_with(None, &overrides)?;
        assert_eq!(config.server, "100.64.0.7:7600");
        assert_eq!(config.channel, "console.topology");
        assert_eq!(config.limits.frame_bytes, 65536);
        assert_eq!(config.limits.request_bytes, 65536);
        assert!(
            matches!(&config.credential, Credential::Open { name: Some(name) } if name == "tom")
        );
        assert_eq!(config.agent.as_deref(), Some("tom"));
        let overrides = Overrides {
            agent: Some("agent-ada".into()),
            key_file: Some("/keys/ada".into()),
            ..Overrides::default()
        };
        let config = Config::load_with(None, &overrides)?;
        assert_eq!(config.server, crate::seat::CENTRAL_SERVER);
        let Credential::Lys(lys) = &config.credential else {
            return Err(format!("not a Lys seat: {:?}", config.credential).into());
        };
        assert_eq!(
            (
                lys.agent.as_str(),
                lys.key_file.as_path(),
                lys.issuer.as_str()
            ),
            (
                "agent-ada",
                std::path::Path::new("/keys/ada"),
                crate::seat::CENTRAL_ISSUER
            )
        );
        assert_eq!(config.agent.as_deref(), Some("agent-ada"));
        Ok(())
    }

    #[test]
    fn a_lys_seat_refuses_as_and_an_open_seat_refuses_agent() -> Result<(), Box<dyn Error>> {
        let dir = tempfile::tempdir()?;
        let lys = dir.path().join("lys.toml");
        std::fs::write(&lys, LYS)?;
        let name = Overrides {
            name: Some("bea".into()),
            ..Overrides::default()
        };
        let words = refused(Some(&lys), &name)?;
        assert!(words.contains("--as") && words.contains("[lys]"), "{words}");
        // The Lys seat's own agent and key move by flag.
        let moved = Overrides {
            agent: Some("agent-bea".into()),
            key_file: Some("/keys/bea".into()),
            ..Overrides::default()
        };
        let config = Config::load_with(Some(&lys), &moved)?;
        let Credential::Lys(table) = &config.credential else {
            return Err("not a Lys seat".into());
        };
        assert_eq!(table.issuer, "http://127.0.0.1:4300");
        assert_eq!(table.agent, "agent-bea");
        let open = dir.path().join("open.toml");
        std::fs::write(&open, OPEN)?;
        let words = refused(Some(&open), &moved)?;
        assert!(
            words.contains("--agent") && words.contains("[open]"),
            "{words}"
        );
        let both = Overrides {
            name: Some("bea".into()),
            ..moved
        };
        let words = refused(None, &both)?;
        assert!(
            words.contains("--as") && words.contains("--agent"),
            "{words}"
        );
        Ok(())
    }

    #[test]
    fn agent_and_key_file_are_refused_alone_by_name() -> Result<(), Box<dyn Error>> {
        let agent = Overrides {
            agent: Some("agent-ada".into()),
            ..Overrides::default()
        };
        let words = refused(None, &agent)?;
        assert!(words.contains("--key-file"), "{words}");
        let key = Overrides {
            key_file: Some("/keys/ada".into()),
            ..Overrides::default()
        };
        let words = refused(None, &key)?;
        assert!(words.contains("--agent"), "{words}");
        let bad = Overrides {
            server: Some("127.0.0.1:4100".into()),
            name: Some("Not A Name".into()),
            ..Overrides::default()
        };
        let words = refused(None, &bad)?;
        assert!(words.contains("--as"), "{words}");
        Ok(())
    }
}

/// SRV-052 R2: an answer's bound is its own field, 16 MiB when unnamed,
/// refused by name at zero, and never the stdin line bound.
#[test]
fn answer_bytes_defaults_to_sixteen_mebibytes_and_is_refused_at_zero() -> Result<(), Box<dyn Error>>
{
    let dir = tempfile::tempdir()?;
    let token = dir.path().join("token");
    std::fs::write(&token, "pass-bytes\n")?;
    let path = dir.path().join("ticket.toml");
    std::fs::write(&path, written(&token, None, None))?;
    let config = Config::load(&path)?;
    assert_eq!(config.limits.answer_bytes, 16_777_216);
    assert_eq!(config.limits.frame_bytes, 16);
    std::fs::write(
        &path,
        format!("{}answer_bytes = 0\n", written(&token, None, None)),
    )?;
    let refusal = Config::load(&path).err().map(|e| e.to_string());
    let refusal = refusal.ok_or("a zero answer_bytes was accepted")?;
    assert!(refusal.contains("answer_bytes"), "{refusal}");
    Ok(())
}