mlua-swarm-cli 0.22.0

Command line interface for mlua-swarm (mse binary with serve / mcp subcommands).
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
//! CLI smoke tests for `mse bp build`.

use assert_cmd::Command;
use predicates::str::contains;
use std::fs;

#[test]
fn bp_build_writes_parseable_json_with_top_level_keys() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let out_path = tmp.path().join("out.json");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build", "tests/fixtures/pipeline.bp.lua", "-o"])
        .arg(&out_path)
        .assert()
        .success();

    let out = fs::read_to_string(&out_path).expect("out.json written");
    let value: serde_json::Value = serde_json::from_str(&out).expect("out.json is valid JSON");
    for key in ["id", "flow", "agents", "operators", "strategy", "metadata"] {
        assert!(value.get(key).is_some(), "missing top-level key: {key}");
    }
}

#[test]
fn bp_build_reports_undeclared_verdict_literal_as_a_compile_error() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let script_path = tmp.path().join("bad_verdict.bp.lua");
    fs::write(
        &script_path,
        r#"
local F = require("flow_dsl")

return {
  id = "bad-verdict-lint",
  flow = F.seq({
    F.step({ agent = "checker", input = F.lit("go"), out = F.p("$.checker") }),
    F.branch({
      cond = F.p('$.checker.parts["verdict"]'):eq("NOT_A_DECLARED_VALUE"),
      on_true = F.assign({ at = F.p("$.halted_at"), value = F.lit("checker") }),
      on_false = F.assign({ at = F.p("$.done"), value = F.lit(true) }),
    }),
  }),
  agents = {
    {
      name = "checker",
      kind = "operator",
      spec = { operator_ref = "main-ai" },
      -- worker_binding is set here so this fixture exercises the
      -- GH #50 verdict-contract lint specifically — without it, the
      -- GH #61 worker_binding lint would short-circuit first.
      profile = { system_prompt = "check", tools = {}, worker_binding = "claude" },
      verdict = { channel = "part", values = { "PASS", "BLOCKED" } },
    },
  },
  operators = {
    { name = "main-ai" },
  },
}
"#,
    )
    .expect("write bad-verdict fixture script");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build"])
        .arg(&script_path)
        .assert()
        .failure()
        .stderr(contains("compile lint FAILED"))
        .stderr(contains("is not a member of the declared values"))
        // GH #62 Axis B.1: the verdict-contract lint gets a canonical
        // fix hint block; assert on the kind key + the actionable text
        // so a regression in `fix_hint_from_compile_error`'s mapping
        // for this lint kind fails here.
        .stderr(contains("fix hint (verdict-value-not-in-contract)"))
        .stderr(contains("`agents[N].verdict.values`"));
}

/// GH #61: `bp_build` compile-lint must fail loud when an operator-kind
/// agent lacks `profile.worker_binding` — the same fail-loud gate the
/// runtime Compiler applies at dispatch (`src/blueprint/compiler.rs` —
/// `profile.worker_binding is required`), front-loaded into the lint
/// stage so the author sees it before the undispatchable Blueprint is
/// registered.
#[test]
fn bp_build_reports_missing_worker_binding_on_operator_agent_as_a_compile_error() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let script_path = tmp.path().join("missing_worker_binding.bp.lua");
    fs::write(
        &script_path,
        r#"
local B = require("bp_dsl")

local flow = B.pipeline({
  B.stage "greet" { agent = "greeter" },
  halted_at = "$.halted_at",
  done = "$.pipeline_complete",
})

return {
  id = "gh61-missing-worker-binding",
  flow = flow,
  agents = {
    { name = "greeter", kind = "operator",
      spec = { operator_ref = "main-ai" },
      profile = { system_prompt = "Say hello.", tools = {} } },  -- worker_binding intentionally missing
  },
  operators = { { name = "main-ai" } },
  strategy = { strict_refs = true, strict_kind = true },
}
"#,
    )
    .expect("write missing-worker-binding fixture script");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build"])
        .arg(&script_path)
        .assert()
        .failure()
        .stderr(contains("compile lint FAILED"))
        .stderr(contains("profile.worker_binding is required"))
        // Both fix paths named in the Compiler message.
        .stderr(contains("agents[N].profile.worker_binding"))
        .stderr(contains("$agent_md file ref"))
        // GH #62 Axis B.1: the worker_binding lint gets a canonical
        // fix hint block naming the offending agent and the concrete
        // patch line.
        .stderr(contains("fix hint (worker-binding-missing)"))
        .stderr(contains("operator agent 'greeter'"))
        .stderr(contains(
            "runner = { backend = \"ws_operator\", variant = \"claude\", tools = {} }",
        ));
}

/// GH #61 regression guard: the bundled `07-dsl-pipeline.bp.lua` sample
/// must still round-trip through `bp build` after every operator agent
/// gained `worker_binding = "claude"` in the same series of edits that
/// tightened compile-lint. If this fails the sample slipped out of sync
/// with the lint.
#[test]
fn bp_build_dsl_pipeline_sample_still_lints_ok_after_worker_binding_tightening() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let out_path = tmp.path().join("out.json");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args([
            "bp",
            "build",
            "src/mcp/resources/samples/07-dsl-pipeline.bp.lua",
            "-o",
        ])
        .arg(&out_path)
        .assert()
        .success()
        // `LintReport::Ok` prints the count line to stderr; assert on
        // it so a regression to `Skipped` (silently degrading the lint
        // to shape-only) also surfaces here.
        .stderr(contains("compile lint: OK"));
}

/// GH #62 Axis A CLI round-trip: `mse bp new pipeline` output must
/// round-trip through `mse bp build` with `compile lint: OK`. Covers the
/// AC on the CLI-invoke path (unit tests already cover the pure render
/// function).
#[test]
fn bp_new_pipeline_scaffold_round_trips_through_bp_build() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let scaffold_path = tmp.path().join("hello.bp.lua");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args([
            "bp",
            "new",
            "pipeline",
            "hello",
            "--stages",
            "greet,echo",
            "-o",
        ])
        .arg(&scaffold_path)
        .assert()
        .success()
        .stderr(contains("mse bp new: wrote"));

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build"])
        .arg(&scaffold_path)
        .assert()
        .success()
        .stderr(contains("compile lint: OK"));
}

/// GH #62 Axis A CLI round-trip: `mse bp new single` — the minimal
/// `flow_dsl`-only shape. Separate test so a regression in either
/// template's DSL surface stays specific.
#[test]
fn bp_new_single_scaffold_round_trips_through_bp_build() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let scaffold_path = tmp.path().join("solo.bp.lua");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "new", "single", "solo-run", "--agent", "solo", "-o"])
        .arg(&scaffold_path)
        .assert()
        .success();

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build"])
        .arg(&scaffold_path)
        .assert()
        .success()
        .stderr(contains("compile lint: OK"));
}

/// GH #62 Axis A CLI round-trip: `mse bp new verdict` — 3-stage
/// verdict-gated shape. This is the most complex template; if it lints
/// OK from the CLI, the two simpler ones do too, but each has its own
/// test to keep failure attribution specific.
#[test]
fn bp_new_verdict_scaffold_round_trips_through_bp_build() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let scaffold_path = tmp.path().join("review-loop.bp.lua");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "new", "verdict", "review-loop", "-o"])
        .arg(&scaffold_path)
        .assert()
        .success();

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build"])
        .arg(&scaffold_path)
        .assert()
        .success()
        .stderr(contains("compile lint: OK"));
}

/// Linker include-cascade Warn/strict-embed:`mse bp build` on a `.bp.lua` whose
/// `$agent_md` ref can not be resolved through the include cascade must
/// still emit the raw wire JSON (refs preserved) and exit zero — the
/// server resolves refs itself at register time. The lint report is
/// `compile lint: WARN`. Regression guard for the Phase 4 → Phase 5
/// default: an unresolved ref never silently degrades the emit path.
#[test]
fn bp_build_unresolved_ref_default_emits_raw_and_warns() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let script_path = tmp.path().join("unresolved.bp.lua");
    let out_path = tmp.path().join("out.json");
    // Reference an `agent.md` file that does not exist in the script's
    // parent dir, in-bp includes, env includes, CLI includes, or the
    // bundled default samples dir — so the include cascade hits none of
    // the 6 tiers and the linker returns Err (→ LintReport::Warn).
    fs::write(
        &script_path,
        r#"
local F = require("flow_dsl")

return {
  id = "unresolved-ref-warn",
  flow = F.step({ id = "solo", agent = "solo", input = F.lit(""), out = F.p("$.solo") }),
  agents = {
    {
      name = "solo",
      kind = "operator",
      spec = { operator_ref = "main-ai" },
      ["$agent_md"] = "definitely-not-present.md",
    },
  },
  operators = { { name = "main-ai", kind = "main_ai" } },
  strategy = { strict_refs = true, strict_kind = true },
}
"#,
    )
    .expect("write unresolved-ref fixture script");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build"])
        .arg(&script_path)
        .args(["-o"])
        .arg(&out_path)
        .assert()
        .success()
        .stderr(contains("compile lint: WARN"))
        .stderr(contains("could not resolve $file/$agent_md refs"));

    let out = fs::read_to_string(&out_path).expect("out.json written");
    let value: serde_json::Value = serde_json::from_str(&out).expect("out.json is valid JSON");
    // Raw wire JSON: the `$agent_md` ref is preserved verbatim on the
    // agent entry — the server will resolve it at register time.
    let agent = value
        .get("agents")
        .and_then(|a| a.get(0))
        .expect("agents[0] present");
    assert_eq!(
        agent.get("$agent_md").and_then(|v| v.as_str()),
        Some("definitely-not-present.md"),
        "raw emit must preserve the unresolved $agent_md ref"
    );
}

/// Linker include-cascade Warn/strict-embed:`--strict-embed` promotes the same
/// unresolved-ref WARN to a hard failure — non-zero exit, no JSON
/// emitted. Mirrors the CLI flag semantic ("require refs to be embedded
/// at build time, hard-fail if any unresolved") and matches the design
/// §Behavior on unresolved refs table (`mse bp build` strict opt-in).
#[test]
fn bp_build_unresolved_ref_strict_embed_hard_fails() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let script_path = tmp.path().join("unresolved-strict.bp.lua");
    let out_path = tmp.path().join("out.json");
    fs::write(
        &script_path,
        r#"
local F = require("flow_dsl")

return {
  id = "unresolved-ref-strict",
  flow = F.step({ id = "solo", agent = "solo", input = F.lit(""), out = F.p("$.solo") }),
  agents = {
    {
      name = "solo",
      kind = "operator",
      spec = { operator_ref = "main-ai" },
      ["$agent_md"] = "definitely-not-present.md",
    },
  },
  operators = { { name = "main-ai", kind = "main_ai" } },
  strategy = { strict_refs = true, strict_kind = true },
}
"#,
    )
    .expect("write unresolved-strict fixture script");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build", "--strict-embed"])
        .arg(&script_path)
        .args(["-o"])
        .arg(&out_path)
        .assert()
        .failure()
        .stderr(contains("compile lint: WARN"))
        .stderr(contains("--strict-embed"))
        .stderr(contains("refusing to emit"));

    assert!(
        !out_path.exists(),
        "--strict-embed must not write out.json when the linker fails"
    );
}

/// Phase 7 (linker refactor 4c4e3eb8) tier-6 round-trip: `mse bp build`
/// must resolve a `$agent_md = "researcher.md"` bare-name ref via the
/// bundled samples/agents directory when no other cascade tier
/// (bp.lua parent, in-bp includes, `MSE_BLUEPRINT_INCLUDES`, `--include`,
/// or a server config) resolves it — exercises the CLI's
/// `bundled_agents_dir()` wiring end-to-end, and guarantees the bundled
/// fixtures stay resolvable by name.
#[test]
fn bp_build_resolves_bare_agent_md_ref_via_bundled_default_tier() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let script_path = tmp.path().join("bundled-tier.bp.lua");
    let out_path = tmp.path().join("out.json");
    fs::write(
        &script_path,
        r#"
local F = require("flow_dsl")

return {
  id = "bundled-tier6-round-trip",
  flow = F.step({ id = "solo", agent = "researcher", input = F.lit(""), out = F.p("$.solo") }),
  agents = {
    {
      -- Bare filename: no `agents/` prefix, so tier 1 (this script's
      -- parent = a fresh tempdir) misses. There are no in-bp includes,
      -- no `MSE_BLUEPRINT_INCLUDES` set for this test, and no `--include`
      -- flag on the command line — so the ref must resolve through the
      -- tier-6 bundled default (`src/mcp/resources/samples/agents/`).
      ["$agent_md"] = "researcher.md",
      spec = { operator_ref = "main-ai" },
    },
  },
  operators = { { name = "main-ai" } },
  strategy = { strict_refs = true, strict_kind = true },
}
"#,
    )
    .expect("write bundled-tier fixture script");

    // Explicitly clear MSE_BLUEPRINT_INCLUDES so a session-level env that
    // happens to hit `researcher.md` does not mask the tier under test.
    Command::cargo_bin("mse")
        .expect("mse binary")
        .env_remove("MSE_BLUEPRINT_INCLUDES")
        .args(["bp", "build"])
        .arg(&script_path)
        .args(["-o"])
        .arg(&out_path)
        .assert()
        .success()
        .stderr(contains("compile lint: OK"));

    // Emit is the raw wire JSON — the `$agent_md` ref is preserved even
    // though the linker resolved it during lint (design table §Behavior
    // on unresolved refs: default `mse bp build` keeps the wire form so
    // the server resolves at register time).
    let out = fs::read_to_string(&out_path).expect("out.json written");
    let value: serde_json::Value = serde_json::from_str(&out).expect("out.json is valid JSON");
    let agent = value
        .get("agents")
        .and_then(|a| a.get(0))
        .expect("agents[0] present");
    assert_eq!(
        agent.get("$agent_md").and_then(|v| v.as_str()),
        Some("researcher.md"),
        "raw emit preserves the ref that tier-6 resolved during lint"
    );
}

/// GH #62 Axis A CLI error path: an unknown template must exit non-zero
/// with the accepted list named — closed set discoverable from the
/// error rather than requiring the author to open the guide.
/// GH #86 regression guard: a Blueprint declaring `kind = "agent_block"`
/// must pass compile lint under the schema default `strategy.strict_kind
/// = true`. This is the issue's own reproduction shape — before the
/// `AgentBlockInProcessSpawnerFactory` registration, `lint_registry`
/// covered four of the five `AgentKind` variants and this exact script
/// failed with `unknown agent kind in SpawnerRegistry: AgentBlock`, with
/// no strategy setting that opened a path (`strict_kind = false` merely
/// moved the failure to `unresolved-agent-ref`, since an unresolvable
/// kind is dropped from the routing table).
#[test]
fn bp_build_accepts_an_agent_block_agent_under_strict_kind() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let script_path = tmp.path().join("agent_block_gate.bp.lua");
    fs::write(
        &script_path,
        r#"
local F = require("flow_dsl")

return {
  id = "gh86-agent-block-gate",
  flow = F.step({
    agent = "gate-danger",
    input = F.p("$.d.gate_danger"),
    out = F.p("$.danger_result"),
  }),
  agents = {
    {
      name = "gate-danger",
      kind = "agent_block",
      -- ScriptBasedAgent mode; `tools = {}` is the enforced-empty grant
      -- this mode requires (the script drives its own `mcp.connect`).
      spec = { script_path = "gate.lua" },
      runner = { backend = "agent_block_in_process", tools = {} },
    },
  },
  operators = {},
  strategy = { strict_refs = true, strict_kind = true },
}
"#,
    )
    .expect("write agent_block fixture script");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build"])
        .arg(&script_path)
        .assert()
        .success()
        .stderr(contains("compile lint: OK"));
}

/// GH #86 sibling: the same Blueprint in ScriptBasedAgent mode with a
/// declared `mcp__` grant is rejected, because a caller script opens its
/// own MCP connections and the host has no choke point to enforce
/// through — an unenforceable grant fails loud instead of being silently
/// dropped. (Non-`mcp__` names select no server and do not trip this.)
#[test]
fn bp_build_rejects_agent_block_script_mode_with_a_declared_mcp_grant() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let script_path = tmp.path().join("agent_block_bad_grant.bp.lua");
    fs::write(
        &script_path,
        r#"
local F = require("flow_dsl")

return {
  id = "gh86-agent-block-bad-grant",
  flow = F.step({
    agent = "gate-danger",
    input = F.p("$.d.gate_danger"),
    out = F.p("$.danger_result"),
  }),
  agents = {
    {
      name = "gate-danger",
      kind = "agent_block",
      spec = { script_path = "gate.lua" },
      runner = { backend = "agent_block_in_process", tools = { "mcp__outline__list_docs" } },
    },
  },
  operators = {},
  strategy = { strict_refs = true, strict_kind = true },
}
"#,
    )
    .expect("write agent_block bad-grant fixture script");

    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "build"])
        .arg(&script_path)
        .assert()
        .failure()
        .stderr(contains("compile lint FAILED"))
        .stderr(contains("mcp__outline__list_docs"))
        .stderr(contains("PromptBasedAgent mode"));
}

#[test]
fn bp_new_unknown_template_exits_error_with_accepted_list() {
    Command::cargo_bin("mse")
        .expect("mse binary")
        .args(["bp", "new", "bogus", "foo"])
        .assert()
        .failure()
        .stderr(contains("unknown template 'bogus'"))
        .stderr(contains("pipeline"))
        .stderr(contains("single"))
        .stderr(contains("verdict"));
}