mlua-swarm-server 0.12.2

HTTP + WebSocket server for mlua-swarm (task API, Blueprint store, Operator WS sessions).
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
//! GH #50 Subtask 2 — submit-time verdict contract enforcement, exercised
//! over a real HTTP round trip.
//!
//! Acceptance Criterion #7 requires the process-boundary HTTP contract to
//! be under test (not just handler-level unit behavior — see
//! `crates/mlua-swarm-server/src/worker.rs`'s own
//! `#[cfg(test)] mod tests` GH #50 section for the fast in-process
//! counterpart). This file starts a real `axum::serve` on an ephemeral
//! port and drives it with `reqwest`, mirroring the established pattern
//! already in this crate
//! (`crate::projection::tests::old_ctx_route_returns_404_not_found_by_router`)
//! rather than introducing a new `tower::ServiceExt::oneshot` dependency
//! this crate does not otherwise carry.
//!
//! Seeding a task + registering a `VerdictContract` bypasses `POST
//! /v1/tasks` + `TaskLaunchService::launch` (compiling a live Blueprint's
//! `AgentDef.verdict` into `CompiledAgentTable.verdict_contracts` and
//! wiring THAT into `Engine::register_verdict_contracts` is a follow-up —
//! that call site, `src/service/task_launch.rs`, sits outside this
//! subtask's file scope). Instead this seeds `EngineState` directly via
//! `Engine::with_state`, mirroring `worker.rs`'s own
//! `tests::seed_task_with_handle` helper (duplicated here — an
//! integration test cannot reach a `#[cfg(test)]`-gated helper in the
//! library crate) — the exact minimal state `worker_submit` /
//! `worker_artifact` need to resolve a Bearer handle to a task and agent.

use mlua_swarm::core::config::EngineCfg;
use mlua_swarm::core::engine::Engine;
use mlua_swarm::core::state::{CapTokenRecord, TaskSpec, TaskState};
use mlua_swarm::{CapToken, Role, StepId};
use mlua_swarm_schema::{VerdictChannel, VerdictContract};
use std::collections::HashMap;
use std::time::Duration;

/// Seeds a `Pending` task bound to `agent`, mints a short worker handle
/// for it, and returns the handle.
async fn seed_task_with_handle(engine: &Engine, task_id: &StepId, agent: &str) -> String {
    let handle = format!("wh-{}", mlua_swarm::types::secure_hex(4));
    let task_id = task_id.clone();
    let agent = agent.to_string();
    let handle_clone = handle.clone();
    engine
        .with_state("test.seed_task_with_handle", move |s| {
            let task = TaskState::new(
                task_id.clone(),
                TaskSpec {
                    agent: agent.clone(),
                    initial_directive: serde_json::json!("x"),
                    step_ctx: None,
                    check_policy: None,
                },
            );
            s.tasks.insert(task_id.clone(), task);
            let token = CapToken {
                agent_id: agent,
                role: Role::Worker,
                scopes: vec!["*".to_string()],
                issued_at: 0,
                expire_at: u64::MAX,
                max_uses: None,
                nonce: format!("test-nonce-{task_id}"),
                sig_hex: String::new(),
            };
            let fp = token.fingerprint();
            s.tokens.insert(
                fp.clone(),
                CapTokenRecord {
                    token,
                    uses_left: None,
                    revoked: false,
                    task_id: Some(task_id),
                },
            );
            s.worker_handles.insert(handle_clone, fp);
        })
        .await
        .expect("seed_task_with_handle");
    handle
}

/// GH #51 — seeds a `Pending` task bound to `agent` and mints + registers
/// a properly HMAC-signed `Role::Worker` `CapToken` for it, returning the
/// token itself (not a handle). `POST /v1/worker/result`'s
/// `decode_worker_bearer` requires a full encoded `CapToken` — unlike
/// `worker_submit`/`worker_artifact`'s short-handle Bearer support (what
/// [`seed_task_with_handle`] mints), so this is a distinct helper rather
/// than a variant of that one.
async fn seed_task_with_token(engine: &Engine, task_id: &StepId, agent: &str) -> CapToken {
    let task_id_for_state = task_id.clone();
    let agent_for_state = agent.to_string();
    engine
        .with_state("test.seed_task_with_token", move |s| {
            let task = TaskState::new(
                task_id_for_state.clone(),
                TaskSpec {
                    agent: agent_for_state,
                    initial_directive: serde_json::json!("x"),
                    step_ctx: None,
                    check_policy: None,
                },
            );
            s.tasks.insert(task_id_for_state.clone(), task);
        })
        .await
        .expect("seed_task_with_token");
    let token = engine.signer().session(
        agent.to_string(),
        Role::Worker,
        vec!["*".to_string()],
        Duration::from_secs(600),
    );
    let fp = token.fingerprint();
    let record = CapTokenRecord::from_worker_token(token.clone(), task_id.clone());
    engine
        .with_state("test.register_token", move |s| {
            s.tokens.insert(fp, record);
        })
        .await
        .expect("register token");
    token
}

/// Starts a real HTTP server for `engine` on an ephemeral port and
/// returns its base URL (`http://127.0.0.1:<port>`).
async fn spawn_server(engine: Engine) -> String {
    let router = mlua_swarm_server::build_router(engine);
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind ephemeral port");
    let addr = listener.local_addr().expect("local addr");
    tokio::spawn(async move {
        let _ = axum::serve(listener, router).await;
    });
    format!("http://{addr}")
}

fn body_contract(values: &[&str]) -> VerdictContract {
    VerdictContract {
        channel: VerdictChannel::Body,
        values: values.iter().map(|v| v.to_string()).collect(),
    }
}

fn part_contract(values: &[&str]) -> VerdictContract {
    VerdictContract {
        channel: VerdictChannel::Part,
        values: values.iter().map(|v| v.to_string()).collect(),
    }
}

/// Case 1: a `channel: "body"` contract rejects a `worker_submit` body
/// outside its declared `values` with HTTP 422, echoing the expected
/// token set.
#[tokio::test]
async fn worker_submit_rejects_body_outside_contract_values() {
    let engine = Engine::new(EngineCfg::default());
    engine.register_verdict_contracts(HashMap::from([(
        "gate".to_string(),
        body_contract(&["PASS", "BLOCKED"]),
    )]));
    let task_id = StepId::new();
    let handle = seed_task_with_handle(&engine, &task_id, "gate").await;
    let base_url = spawn_server(engine).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/submit"))
        .header("Authorization", format!("Bearer {handle}"))
        .body("UNKNOWN")
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::UNPROCESSABLE_ENTITY);
    let body: serde_json::Value = resp.json().await.expect("json body");
    let error = body["error"].as_str().expect("error string");
    assert!(
        error.contains("PASS") && error.contains("BLOCKED"),
        "error should echo declared values: {error}"
    );
}

/// Case 2: the same contract accepts a body that IS a member of `values`
/// — HTTP 204, and the value lands in the task's `last_result`.
#[tokio::test]
async fn worker_submit_accepts_body_inside_contract_values() {
    let engine = Engine::new(EngineCfg::default());
    engine.register_verdict_contracts(HashMap::from([(
        "gate".to_string(),
        body_contract(&["PASS", "BLOCKED"]),
    )]));
    let task_id = StepId::new();
    let handle = seed_task_with_handle(&engine, &task_id, "gate").await;
    let base_url = spawn_server(engine.clone()).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/submit"))
        .header("Authorization", format!("Bearer {handle}"))
        .body("PASS")
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::NO_CONTENT);

    let last_result = engine
        .with_state("test.read_last_result", {
            let task_id = task_id.clone();
            move |s| s.tasks.get(&task_id).and_then(|t| t.last_result.clone())
        })
        .await
        .expect("read last_result");
    assert_eq!(last_result, Some(serde_json::json!("PASS")));
}

/// Case 3: a `channel: "part"` contract rejects a
/// `worker_artifact?name=verdict` value outside `values` with HTTP 422.
#[tokio::test]
async fn worker_artifact_verdict_part_rejects_value_outside_contract() {
    let engine = Engine::new(EngineCfg::default());
    engine.register_verdict_contracts(HashMap::from([(
        "gate".to_string(),
        part_contract(&["PASS", "BLOCKED"]),
    )]));
    let task_id = StepId::new();
    let handle = seed_task_with_handle(&engine, &task_id, "gate").await;
    let base_url = spawn_server(engine).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/artifact?name=verdict"))
        .header("Authorization", format!("Bearer {handle}"))
        .body("UNKNOWN")
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::UNPROCESSABLE_ENTITY);
}

/// Case 4: a part named anything OTHER than `"verdict"` skips the gate
/// entirely — even with a `channel: "part"` contract declared, existing
/// (pre-GH-#50) `204` behavior is unchanged.
#[tokio::test]
async fn worker_artifact_non_verdict_part_skips_the_gate() {
    let engine = Engine::new(EngineCfg::default());
    engine.register_verdict_contracts(HashMap::from([(
        "gate".to_string(),
        part_contract(&["PASS", "BLOCKED"]),
    )]));
    let task_id = StepId::new();
    let handle = seed_task_with_handle(&engine, &task_id, "gate").await;
    let base_url = spawn_server(engine).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/artifact?name=notes"))
        .header("Authorization", format!("Bearer {handle}"))
        .body("anything at all")
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::NO_CONTENT);
}

/// Case 5 (opt-in regression guard): an agent with NO declared verdict
/// contract is unaffected — `worker_submit` returns 204 for an arbitrary
/// body, same as pre-GH-#50.
#[tokio::test]
async fn worker_submit_without_a_declared_contract_is_unaffected() {
    let engine = Engine::new(EngineCfg::default());
    // No `register_verdict_contracts` call at all for this agent.
    let task_id = StepId::new();
    let handle = seed_task_with_handle(&engine, &task_id, "undeclared-agent").await;
    let base_url = spawn_server(engine).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/submit"))
        .header("Authorization", format!("Bearer {handle}"))
        .body("anything at all, no contract to violate")
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::NO_CONTENT);
}

// ─── GH #51 — completion-time verdict-contract enforcement (follow-up to
// the above GH #50 submit-time cases): part-presence at completion, and
// gate coverage on `POST /v1/worker/result` (route 2 of 3) ────────────────

/// Route 1 (`POST /v1/worker/submit`), part-presence at completion: a
/// `channel: "part"` contract agent completes via a plain submit WITHOUT
/// ever staging a `"verdict"` part — rejected with HTTP 422 naming the
/// missing part, before the pre-GH-#51 bypass this closes (the old
/// submit-time gate only checked `channel: "body"`, never part
/// PRESENCE).
#[tokio::test]
async fn worker_submit_rejects_missing_verdict_part_when_channel_is_part() {
    let engine = Engine::new(EngineCfg::default());
    engine.register_verdict_contracts(HashMap::from([(
        "gate".to_string(),
        part_contract(&["PASS", "BLOCKED"]),
    )]));
    let task_id = StepId::new();
    let handle = seed_task_with_handle(&engine, &task_id, "gate").await;
    let base_url = spawn_server(engine).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/submit"))
        .header("Authorization", format!("Bearer {handle}"))
        .body("a full report, never staged as a verdict part")
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::UNPROCESSABLE_ENTITY);
    let body: serde_json::Value = resp.json().await.expect("json body");
    let error = body["error"].as_str().expect("error string");
    assert!(
        error.contains("verdict"),
        "error should name the missing part: {error}"
    );
}

/// Route 1 — `ok=false` bypasses the completion-time check entirely,
/// regardless of channel or membership. A `channel: "body"` contract
/// declaring `["PASS", "BLOCKED"]` would reject `"UNKNOWN"` under
/// `ok=true` (Case 1 above); with `ok=false` the same value completes.
#[tokio::test]
async fn worker_submit_ok_false_bypasses_the_gate_regardless_of_value() {
    let engine = Engine::new(EngineCfg::default());
    engine.register_verdict_contracts(HashMap::from([(
        "gate".to_string(),
        body_contract(&["PASS", "BLOCKED"]),
    )]));
    let task_id = StepId::new();
    let handle = seed_task_with_handle(&engine, &task_id, "gate").await;
    let base_url = spawn_server(engine).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/submit?ok=false"))
        .header("Authorization", format!("Bearer {handle}"))
        .body("UNKNOWN")
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::NO_CONTENT);
}

/// Route 2 (`POST /v1/worker/result`), part-presence at completion: this
/// route called no gate at all pre-GH-#51 — a `channel: "part"` contract
/// agent completing here without ever staging `"verdict"` is now
/// rejected with HTTP 422 naming the missing part.
#[tokio::test]
async fn worker_result_rejects_missing_verdict_part_when_channel_is_part() {
    let engine = Engine::new(EngineCfg::default());
    engine.register_verdict_contracts(HashMap::from([(
        "gate".to_string(),
        part_contract(&["PASS", "BLOCKED"]),
    )]));
    let task_id = StepId::new();
    let token = seed_task_with_token(&engine, &task_id, "gate").await;
    let base_url = spawn_server(engine).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/result"))
        .header("Authorization", format!("Bearer {}", token.encode()))
        .json(&serde_json::json!({
            "task_id": task_id.as_str(),
            "value": "a full report, never staged as a verdict part",
            "ok": true,
        }))
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::UNPROCESSABLE_ENTITY);
    let body: serde_json::Value = resp.json().await.expect("json body");
    let error = body["error"].as_str().expect("error string");
    assert!(
        error.contains("verdict"),
        "error should name the missing part: {error}"
    );
}

/// Route 2 — a `channel: "body"` contract's completing value is NOT a
/// member of `values`: this route called no gate at all pre-GH-#51 —
/// closes the gap this subtask's acceptance criteria describes for
/// routes 2 and 3.
#[tokio::test]
async fn worker_result_rejects_body_value_outside_contract() {
    let engine = Engine::new(EngineCfg::default());
    engine.register_verdict_contracts(HashMap::from([(
        "gate".to_string(),
        body_contract(&["PASS", "BLOCKED"]),
    )]));
    let task_id = StepId::new();
    let token = seed_task_with_token(&engine, &task_id, "gate").await;
    let base_url = spawn_server(engine).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/result"))
        .header("Authorization", format!("Bearer {}", token.encode()))
        .json(&serde_json::json!({
            "task_id": task_id.as_str(),
            "value": "UNKNOWN",
            "ok": true,
        }))
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::UNPROCESSABLE_ENTITY);
    let body: serde_json::Value = resp.json().await.expect("json body");
    let error = body["error"].as_str().expect("error string");
    assert!(
        error.contains("PASS") && error.contains("BLOCKED"),
        "error should echo declared values: {error}"
    );
}

/// Route 2 — `ok=false` bypasses the completion-time check entirely,
/// regardless of channel or membership.
#[tokio::test]
async fn worker_result_ok_false_bypasses_the_gate_regardless_of_value() {
    let engine = Engine::new(EngineCfg::default());
    engine.register_verdict_contracts(HashMap::from([(
        "gate".to_string(),
        body_contract(&["PASS", "BLOCKED"]),
    )]));
    let task_id = StepId::new();
    let token = seed_task_with_token(&engine, &task_id, "gate").await;
    let base_url = spawn_server(engine).await;

    let client = reqwest::Client::new();
    let resp = client
        .post(format!("{base_url}/v1/worker/result"))
        .header("Authorization", format!("Bearer {}", token.encode()))
        .json(&serde_json::json!({
            "task_id": task_id.as_str(),
            "value": "UNKNOWN",
            "ok": false,
        }))
        .send()
        .await
        .expect("request");
    assert_eq!(resp.status(), reqwest::StatusCode::NO_CONTENT);
}