liminal-cli 0.3.21

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
//! A real exchange, real Lys approvals and the independent Go conformance worker.
//! The fixture supplies local addresses, declared identities and configured roots.
//! Missing fixture inputs fail by name; no test is skipped or replaced by a mock.

use super::tools::ToolResult;
use crate::config::Config;
use crate::network::{Live, Rail};
use serde_json::{Value, json};
use std::path::Path;
use std::path::PathBuf;
#[path = "site_end_to_end/approval.rs"]
mod approval;
#[path = "site_end_to_end/server.rs"]
mod server;
#[path = "site_end_to_end/wire.rs"]
mod wire;
#[path = "site_end_to_end/worker.rs"]
mod worker;
use approval::{approve, approve_refused, grants, http};
use server::ServerProcess;
use wire::Watch;
use worker::{GoWorker, build_worker};

type TestResult = Result<(), Box<dyn std::error::Error>>;
fn required(name: &str) -> Result<String, Box<dyn std::error::Error>> {
    std::env::var(name)
        .map_err(|_| format!("{name} is required for the real construction fixture").into())
}
fn require(value: bool, reason: &'static str) -> TestResult {
    if value { Ok(()) } else { Err(reason.into()) }
}
fn config(role: &str) -> Result<Config, Box<dyn std::error::Error>> {
    Ok(Config::load(Path::new(&required(&format!(
        "AGT003_{role}_CONFIG"
    ))?))?)
}
fn tool(
    role: &str,
    name: &str,
    arguments: &Value,
) -> Result<ToolResult, Box<dyn std::error::Error>> {
    let config = config(role)?;
    let rail = Live::new(&config)?;
    let arguments = arguments
        .as_object()
        .ok_or("fixture arguments must be an object")?;
    super::plan_tools::tool(&rail, name, arguments)
        .or_else(|| super::template_tools::tool(&rail, name, arguments))
        .ok_or_else(|| "construction tool is absent".into())
}

fn health() -> Result<Value, Box<dyn std::error::Error>> {
    http(
        required("AGT003_ADMIN_HTTP")?.parse()?,
        "GET",
        "/health",
        None,
        &json!({}),
        config("APPROVER")?.limits.record_bytes,
    )
}

fn create_and_bind(site: &str, subject: &str, builder: &str, topology: &mut Watch) -> TestResult {
    let missing = tool(
        "ASSISTANT",
        "create_site",
        &json!({"name":site,"purpose":"Echo validated requests.","subjects":[subject],"contracts":{}}),
    )?;
    require(missing.is_error, "site without contracts was admitted")?;
    require(
        missing.structured["error"].as_str() == Some(&format!("subject {subject} has no schema")),
        "missing contract refusal words differ",
    )?;
    let mut contracts = serde_json::Map::new();
    contracts.insert(subject.to_owned(),json!({"input_schema_json":"{\"type\":\"object\"}","output_schema_json":"{\"type\":\"object\"}"}));
    let created = tool(
        "ASSISTANT",
        "create_site",
        &json!({"name":site,"purpose":"Echo validated requests.","subjects":[subject],"contracts":contracts}),
    )?;
    approve(&created)?;
    let created = topology.station(site, "building")?;
    require(
        created["contracts"]
            .as_array()
            .is_some_and(|contracts| contracts.len() == 1 && contracts[0]["subject"] == subject),
        "building topology omitted the contract",
    )?;
    approve(&tool(
        "ASSISTANT",
        "bind_builder",
        &json!({"service":site,"agent":builder}),
    )?)?;
    let bound = topology.station(site, "building")?;
    require(
        bound["builder"] == builder,
        "building topology omitted the builder",
    )?;
    approve_refused(
        &tool(
            "ASSISTANT",
            "bind_builder",
            &json!({"service":site,"agent":builder}),
        )?,
        &format!("site {site} has builder {builder}"),
    )?;
    approve(&tool(
        "ASSISTANT",
        "unbind_builder",
        &json!({"service":site}),
    )?)?;
    let revoked = grants()?;
    require(
        revoked["grants"]
            .as_array()
            .ok_or("issuer grants absent")?
            .iter()
            .all(|grant| {
                grant["holder"] != builder
                    || grant["resource"]["id"] != site
                    || grant["standing"]["stands"] == false
            }),
        "unbind left a builder site grant standing",
    )?;
    approve(&tool(
        "ASSISTANT",
        "bind_builder",
        &json!({"service":site,"agent":builder}),
    )?)?;
    let rights = grants()?;
    let owned = rights["grants"]
        .as_array()
        .ok_or("issuer grant list absent")?
        .iter()
        .filter(|grant| {
            grant["holder"] == builder
                && grant["resource"]["kind"] == "liminal.service"
                && grant["resource"]["id"] == site
                && grant["standing"]["stands"] == true
        })
        .collect::<Vec<_>>();
    require(
        owned.len() == 2,
        "bound builder does not hold exactly two site grants",
    )?;
    require(
        owned
            .iter()
            .any(|grant| grant["actions"] == json!(["plan"]))
            && owned
                .iter()
                .any(|grant| grant["actions"] == json!(["serve"])),
        "builder's site grants contain other actions",
    )?;
    Ok(())
}

fn check_plan(site: &str) -> Result<Watch, Box<dyn std::error::Error>> {
    let set = tool(
        "BUILDER",
        "plan_set",
        &json!({"service":site,"steps":[{"title":"Design"},{"title":"Build"}]}),
    )?;
    require(!set.is_error, "bound builder plan_set refused")?;
    let mut plan = Watch::new("APPROVER", &format!("services.{site}.plan"))?;
    require(
        plan.value()?["revision"] == 1,
        "retained current plan did not arrive first",
    )?;
    let changed = tool(
        "BUILDER",
        "plan_step",
        &json!({"service":site,"step":1,"status":"doing"}),
    )?;
    require(!changed.is_error, "bound builder plan_step refused")?;
    let next = plan.value()?;
    require(
        next["revision"] == 2 && next["current"] == 1,
        "plan change differs from record",
    )?;
    let foreign = tool(
        "ASSISTANT",
        "plan_set",
        &json!({"service":site,"steps":[{"title":"Design"}]}),
    )?;
    require(foreign.is_error, "assistant wrote a builder's plan")?;
    let duplicate = tool(
        "BUILDER",
        "plan_step",
        &json!({"service":site,"step":2,"status":"doing"}),
    )?;
    require(
        duplicate.is_error && duplicate.structured["error"] == "step 1 is already doing",
        "second doing step was not refused exactly",
    )?;
    let assistant = required("AGT003_ASSISTANT")?;
    require(
        foreign.structured["error"]
            == format!("pass holder {assistant} is not the builder of {site}"),
        "foreign builder refusal words differ",
    )?;
    let done = tool(
        "BUILDER",
        "plan_step",
        &json!({"service":site,"step":1,"status":"done"}),
    )?;
    require(!done.is_error, "builder could not finish its step")?;
    require(
        plan.value()?["revision"] == 3,
        "finished step transition not delivered",
    )?;
    let removed = tool(
        "BUILDER",
        "plan_set",
        &json!({"service":site,"steps":[{"title":"Changed"}]}),
    )?;
    require(
        removed.is_error
            && removed.structured["error"] == "done step 1 cannot be removed or renamed",
        "finished step was removed or renamed",
    )?;
    Ok(plan)
}

fn trial_and_complete(
    site: &str,
    subject: &str,
    binary: &Path,
    builder: &str,
    topology: &mut Watch,
) -> TestResult {
    let mut mismatch = GoWorker::start(binary, subject, "{\"type\":\"string\"}")?;
    let refused = mismatch.signal()?;
    require(
        refused["ok"] == false
            && refused["error"]["message"]
                == format!("subject {subject}: registered output schema differs from site"),
        "wrong output schema was not refused by subject",
    )?;
    drop(mismatch);
    let mut trial = GoWorker::start(binary, subject, "{\"type\":\"object\"}")?;
    require(
        trial.signal()?["event"] == "ready",
        "real Go trial failed registration",
    )?;
    let station = topology.station(site, "trial")?;
    let connection = station["trial_connection"]
        .as_u64()
        .ok_or("trial connection absent")?;
    let approver = Live::new(&config("APPROVER")?)?;
    let third = Live::new(&config("THIRD")?)?;
    let refused = third
        .request(subject, &json!({"echo":true}), true)
        .err()
        .ok_or("third party called trial")?;
    require(
        refused.to_string() == format!("service {site} is a trial"),
        "trial refusal words differ",
    )?;
    require(
        approver.request(subject, &json!({"echo":true}), true)? == Some(json!({"echo":true})),
        "approver trial call was not answered",
    )?;
    let proposal = tool(
        "BUILDER",
        "propose_template",
        &json!({"service":site,"command":binary,"args":[],"env":[],"stop_timeout_ms":1000}),
    )?;
    let words = proposal.structured["words"]
        .as_str()
        .ok_or("template held words absent")?;
    let words: Value = serde_json::from_str(words)?;
    require(
        words["change"]["detail"]["command"] == serde_json::to_value(binary)?,
        "draft does not carry the approved command",
    )?;
    require(
        words["change"]["detail"]["subjects"] == json!([subject]),
        "draft subjects differ from site",
    )?;
    approve(&proposal)?;
    let premature = tool("BUILDER", "complete", &json!({"service":site}))?;
    approve_refused(
        &premature,
        &format!("trial still registered on connection {connection}"),
    )?;
    trial.stop()?;
    topology.state(site, "building")?;
    approve(&tool("BUILDER", "complete", &json!({"service":site}))?)?;
    topology.state(site, "asleep")?;
    let rights = grants()?;
    require(
        rights["grants"]
            .as_array()
            .ok_or("issuer grants absent")?
            .iter()
            .all(|grant| {
                grant["holder"] != builder
                    || grant["resource"]["id"] != site
                    || grant["actions"] != json!(["serve"])
                    || grant["standing"]["stands"] == false
            }),
        "completion left the builder's serve grant standing",
    )?;
    Ok(())
}

fn run_approved_template(
    site: &str,
    subject: &str,
    binary: &Path,
    started: &Value,
    topology: &mut Watch,
) -> TestResult {
    let creator = Live::new(&config("APPROVER")?)?;
    check_changed_command(&creator, site, binary)?;
    let deployed = creator.request(
        "services.deploy",
        &json!({"operation":"agt003-start","payload":{"id":site}}),
        true,
    )?;
    require(
        deployed
            .as_ref()
            .is_some_and(|answer| answer["outcome"] == "acted"),
        "record template deployment was held or refused",
    )?;
    topology.state(site, "live")?;
    require(
        creator.request(subject, &json!({"live":true}), true)? == Some(json!({"live":true})),
        "approved template did not answer",
    )?;
    let after = health()?;
    require(
        &after["started_at"] == started,
        "exchange restarted during template approval",
    )?;
    let stopped = creator.request(
        "services.hold",
        &json!({"operation":"agt003-stop","payload":{"id":site}}),
        true,
    )?;
    require(
        stopped
            .as_ref()
            .is_some_and(|answer| answer["outcome"] == "acted"),
        "template stop was held or refused",
    )?;
    topology.state(site, "held")?;
    Ok(())
}

fn check_changed_command(rail: &Live, site: &str, binary: &Path) -> TestResult {
    use sha2::{Digest, Sha256};
    use std::io::Read;
    let text = std::fs::read_to_string(required("AGT003_SERVER_CONFIG")?)?;
    let config: toml::Value = toml::from_str(&text)?;
    let bound = u64::try_from(
        config["auth"]["lys"]["templates"]["command_bytes"]
            .as_integer()
            .ok_or("template command_bytes absent in real fixture")?,
    )?;
    require(bound > 0, "template command_bytes is zero")?;
    let mut approved = Vec::new();
    std::fs::File::open(binary)?
        .take(bound.saturating_add(1))
        .read_to_end(&mut approved)?;
    require(
        u64::try_from(approved.len())? <= bound,
        "fixture program exceeds template command_bytes",
    )?;
    let changed = b"a replaced approved executable";
    std::fs::write(binary, changed)?;
    let result = rail.request(
        "services.deploy",
        &json!({"operation":"agt003-changed","payload":{"id":site}}),
        true,
    );
    std::fs::write(binary, &approved)?;
    let refused = result.err().ok_or("changed command was executed")?;
    let words = refused.to_string();
    require(
        words.contains("is not the file approved on")
            && words.contains(&format!(
                "digest {} != {}",
                hex::encode(Sha256::digest(&approved)),
                hex::encode(Sha256::digest(changed))
            )),
        "changed command digest refusal words differ",
    )
}

fn site_record(site: &str) -> Result<Value, Box<dyn std::error::Error>> {
    let rail = Live::new(&config("APPROVER")?)?;
    let answer = rail
        .request(
            "services.list",
            &json!({"operation":"agt003-read","payload":null}),
            true,
        )?
        .ok_or("construction record list absent")?;
    answer["services"]
        .as_array()
        .ok_or("construction services absent")?
        .iter()
        .find(|record| record["name"] == site)
        .cloned()
        .ok_or_else(|| "construction record absent".into())
}

#[test]
fn construction_site_go_worker_approval_plan_trial_and_live_template_journey() -> TestResult {
    let site = required("AGT003_SITE_NAME")?;
    let subject = required("AGT003_SITE_SUBJECT")?;
    let root = PathBuf::from(required("AGT003_TEMPLATE_ROOT")?);
    let (built, binary) = build_worker(&root)?;
    let server = ServerProcess::start()?;
    let mut topology = Watch::new("APPROVER", "console.topology")?;
    let builder = required("AGT003_BUILDER")?;
    create_and_bind(&site, &subject, &builder, &mut topology)?;
    let plan = check_plan(&site)?;
    let before = site_record(&site)?;
    require(
        before["plan"]["revision"] == 3
            && before["plan"]["transitions"]
                .as_array()
                .is_some_and(|history| history.len() == 3),
        "record plan history differs from its writes",
    )?;
    drop(plan);
    drop(topology);
    server.stop()?;
    let server = ServerProcess::start()?;
    let mut topology = Watch::new("APPROVER", "console.topology")?;
    topology.state(&site, "building")?;
    let after = site_record(&site)?;
    require(
        after["plan"] == before["plan"],
        "real exchange restart changed plan revision or history",
    )?;
    let mut plan = Watch::new("APPROVER", &format!("services.{site}.plan"))?;
    require(
        plan.value()?["revision"] == 3,
        "restart did not retain the current plan",
    )?;
    let started = health()?["started_at"].clone();
    require(!started.is_null(), "health.started_at is absent")?;
    trial_and_complete(&site, &subject, &binary, &builder, &mut topology)?;
    run_approved_template(&site, &subject, &binary, &started, &mut topology)?;
    drop(topology);
    drop(plan);
    server.stop()?;
    drop(built);
    Ok(())
}