hatchery-server 0.2.13

hq-core server: drain, operator control, tasks, m4a, provisioning, memory: one supervisor process, loopback control and health
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
//! P6: the hub behind the server control API: node calls forwarded by C2 and operator (HQ)
//! calls for mail and tasks. All state lives under the data dir; admission is always read from
//! the P2 ledger, never from the request.

use crate::messenger::{Messenger, MessengerError, OPERATOR};
use crate::provision::{read_ledger, LedgerEntry, NodeStatus};
use crate::tasks::{Refused, Session, TaskStore};
use serde_json::{json, Value};
use std::path::{Path, PathBuf};
use std::sync::{Condvar, Mutex};
use std::time::{Duration, Instant};

#[derive(Debug)]
pub struct HubError {
    pub status: u16,
    pub message: String,
}

impl HubError {
    fn new(status: u16, message: impl Into<String>) -> Self {
        Self { status, message: message.into() }
    }
}

impl From<MessengerError> for HubError {
    fn from(e: MessengerError) -> Self {
        match e {
            MessengerError::Forbidden => Self::new(403, "refused"),
            MessengerError::Bad(m) => Self::new(400, m),
            MessengerError::Internal(m) => Self::new(500, m),
        }
    }
}

pub struct Hub {
    data_dir: PathBuf,
    messenger: Mutex<Messenger>,
    tasks: Mutex<TaskStore>,
    /// Bumped on every message sent; lets HQ long-poll for new mail instead of polling blindly.
    mail_signal: (Mutex<u64>, Condvar),
    advice: Mutex<crate::routing::AdviceGate>,
    m4a: Option<Box<dyn crate::m4a_gateway::M4a>>,
}

fn refused(r: Refused) -> HubError {
    match r {
        Refused::NotFound => HubError::new(404, "not found"),
        Refused::Conflict(m) => HubError::new(409, m),
        Refused::Invalid(m) => HubError::new(400, m),
    }
}

fn int(e: crate::error::ServerError) -> HubError {
    HubError::new(500, e.to_string())
}

fn opt_str<'a>(v: &'a Value, name: &str) -> Option<&'a str> {
    v.get(name).and_then(Value::as_str)
}

fn query_param<'a>(query: &'a str, name: &str) -> Option<&'a str> {
    query.split('&').find_map(|kv| kv.strip_prefix(name).and_then(|r| r.strip_prefix('='))).filter(|v| !v.is_empty())
}

fn str_field<'a>(v: &'a Value, name: &str) -> Result<&'a str, HubError> {
    v.get(name).and_then(Value::as_str).ok_or_else(|| HubError::new(400, format!("missing field: {name}")))
}

fn now_ms() -> u64 {
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_millis() as u64
}

impl Hub {
    fn routing(&self) -> Result<(crate::routing::RoutingConfig, std::collections::BTreeMap<(String, String), crate::routing::SeatState>), HubError> {
        let cfg = crate::routing::RoutingConfig::load(&self.data_dir).map_err(|e| HubError::new(500, e))?;
        let tasks = self.tasks.lock().expect("tasks");
        // Fold the recent signal window (bounded table).
        let mut all = Vec::new();
        let mut after = None;
        loop {
            let page = tasks.signals(None, after, 500).map_err(int)?;
            let done = page.len() < 500;
            after = page.last().map(|s| s.id).or(after);
            all.extend(page);
            if done {
                break;
            }
        }
        let state = crate::routing::fold_signals(&all, &cfg);
        Ok((cfg, state))
    }

    /// Talk to sessions over m4a with this gateway (wake + run.close reader).
    pub fn with_m4a(mut self, m4a: Box<dyn crate::m4a_gateway::M4a>) -> Self {
        self.m4a = Some(m4a);
        self
    }

    pub fn m4a_enabled(&self) -> bool {
        self.m4a.is_some()
    }

    /// Read run.close letters from the sessions of active runs. Each letter is handled once
    /// (op id `m4a:<event_id>`). Returns how many runs were closed.
    pub fn poll_m4a(&self) -> usize {
        let Some(m4a) = &self.m4a else { return 0 };
        let runs = match self.tasks.lock().expect("tasks").active_runs_with_nick() {
            Ok(r) => r,
            Err(e) => {
                tracing::warn!(error = %e, "m4a reader: runs unavailable");
                return 0;
            }
        };
        let mut closed = 0;
        for (run, nick) in runs {
            let letters = match m4a.read_dm(&nick, 20) {
                Ok(l) => l,
                Err(e) => {
                    tracing::debug!(error = %e, "m4a reader: read failed");
                    continue;
                }
            };
            for letter in letters {
                let from = crate::m4a_gateway::bare_nick(&letter.from);
                if from != crate::m4a_gateway::bare_nick(&nick) {
                    continue; // our own letters and anyone else's are not commands
                }
                let Some(cmd) = crate::m4a_gateway::parse_close(&letter.body) else { continue };
                let op = format!("m4a:{}", letter.event_id);
                let tasks = self.tasks.lock().expect("tasks");
                if tasks.replay(Some(&op)).ok().flatten().is_some() {
                    continue;
                }
                let outcome = if cmd.run_id != run.id {
                    json!({ "ignored": "letter names another run" })
                } else {
                    match tasks.close_run(run.id, &cmd.status, "session", Some(&run.session_id), cmd.branch.as_deref(), cmd.commit.as_deref(), Some(&letter.event_id)) {
                        Ok(Ok(r)) => {
                            closed += 1;
                            tracing::info!(run = r.id, status = %r.status, "run closed by its session via m4a");
                            json!({ "run": r })
                        }
                        Ok(Err(why)) => json!({ "refused": format!("{why:?}") }),
                        Err(e) => json!({ "error": e.to_string() }),
                    }
                };
                let _ = tasks.record(Some(&op), &outcome);
            }
        }
        closed
    }

    /// Routing recommendation for `role` (operator API and node door).
    pub fn recommend(&self, role: &str) -> Result<Value, HubError> {
        let (cfg, state) = self.routing()?;
        let rec = crate::routing::recommend(&cfg, &state, role, now_ms()).map_err(|e| HubError::new(400, e))?;
        Ok(json!({ "recommendation": rec }))
    }

    pub fn open(data_dir: &Path) -> Result<Self, crate::error::ServerError> {
        Ok(Self {
            data_dir: data_dir.to_owned(),
            messenger: Mutex::new(Messenger::open(data_dir)?),
            tasks: Mutex::new(TaskStore::open(data_dir)?),
            mail_signal: (Mutex::new(0), Condvar::new()),
            advice: Mutex::new(Default::default()),
            m4a: None,
        })
    }

    fn bump_mail(&self) {
        *self.mail_signal.0.lock().expect("signal") += 1;
        self.mail_signal.1.notify_all();
    }

    fn entry(&self, node_id: &str) -> Option<LedgerEntry> {
        read_ledger(&self.data_dir).ok()?.into_iter().find(|e| e.node_id == node_id)
    }

    /// Active (member/trusted) node holding `capability`, per the ledger.
    fn admitted(&self, node_id: &str, capability: &str) -> bool {
        node_id != OPERATOR
            && self.entry(node_id).is_some_and(|e| {
                matches!(e.status, NodeStatus::Member | NodeStatus::Trusted) && e.grants.capabilities.iter().any(|c| c == capability)
            })
    }

    /// A call made on behalf of a node. The caller (C2) has authenticated the node and checked its
    /// policy; the hub checks the ledger again, independently.
    pub fn node_call(&self, node_id: &str, op: &str, payload: &Value) -> Result<Value, HubError> {
        let capability = match op.split_once('.').map(|p| p.0) {
            Some("mail") => "mail",
            Some("task") | Some("run") => "tasks",
            _ => return Err(HubError::new(404, "unknown operation")),
        };
        if !self.admitted(node_id, capability) {
            return Err(HubError::new(403, "refused"));
        }
        match op {
            "mail.send" => {
                let key = payload.get("idempotency_key").and_then(Value::as_str).map(str::to_owned);
                let id = self.messenger.lock().expect("messenger").send(node_id, str_field(payload, "to")?, str_field(payload, "subject")?, str_field(payload, "body")?, key)?;
                self.bump_mail();
                Ok(json!({ "message_id": id }))
            }
            "mail.inbox" => {
                let since = payload.get("since_ms").and_then(Value::as_u64).unwrap_or(0);
                let limit = payload.get("limit").and_then(Value::as_u64).unwrap_or(50).min(256) as u16;
                let mail = self.messenger.lock().expect("messenger").inbox(node_id, since, limit)?;
                Ok(json!({ "messages": mail }))
            }
            "mail.ack" => {
                self.messenger.lock().expect("messenger").ack(node_id, str_field(payload, "message_id")?)?;
                Ok(json!({ "ok": true }))
            }
            "task.list" => {
                let tasks = self.tasks.lock().expect("tasks").list(Some(node_id), true).map_err(|e| HubError::new(500, e.to_string()))?;
                Ok(json!({ "tasks": tasks }))
            }
            "task.update" => {
                let id = payload.get("id").and_then(Value::as_i64).ok_or_else(|| HubError::new(400, "missing field: id"))?;
                let status = str_field(payload, "status")?;
                if !matches!(status, "in_progress" | "done") {
                    return Err(HubError::new(400, "status must be in_progress or done"));
                }
                let changed = self.tasks.lock().expect("tasks").set_status(id, status, Some(node_id)).map_err(|e| HubError::new(500, e.to_string()))?;
                if changed { Ok(json!({ "ok": true })) } else { Err(HubError::new(403, "refused")) }
            }
            "task.route" => self.recommend(str_field(payload, "role")?),
            // Advisor anti-noise gate: an advisor session asks before it sends advice via m4a.
            "task.advice_gate" => {
                let (cfg, _) = self.routing()?;
                let advisor = format!("{node_id}/{}", opt_str(payload, "advisor").unwrap_or("advisor"));
                match self.advice.lock().expect("advice").check(&cfg.advisor, &advisor, str_field(payload, "topic")?, now_ms()) {
                    Ok(()) => Ok(json!({ "allow": true })),
                    Err(why) => Ok(json!({ "allow": false, "reason": why })),
                }
            }
            // Short limit / login / ready signal read off a session screen by the node.
            "task.session_signal" => {
                let instance = payload.get("instance_id").and_then(Value::as_i64).ok_or_else(|| HubError::new(400, "missing field: instance_id"))?;
                let sig = self
                    .tasks
                    .lock()
                    .expect("tasks")
                    .add_signal(node_id, str_field(payload, "workspace_id")?, instance, str_field(payload, "provider")?, str_field(payload, "kind")?, opt_str(payload, "hint"))
                    .map_err(int)?
                    .map_err(refused)?;
                tracing::info!(node = node_id, provider = %sig.provider, kind = %sig.kind, "session signal");
                Ok(json!({ "signal_id": sig.id }))
            }
            // The run's own session closes it (decision 8); the run must live on this node.
            // `task.close_run` is the door-facing name; `run.close` is kept as an alias.
            "task.close_run" | "run.close" => {
                let run_id = payload.get("run_id").and_then(Value::as_i64).ok_or_else(|| HubError::new(400, "missing field: run_id"))?;
                let tasks = self.tasks.lock().expect("tasks");
                let run = tasks.run(run_id).map_err(int)?.ok_or_else(|| HubError::new(404, "not found"))?;
                if run.node_id != node_id {
                    return Err(HubError::new(403, "refused"));
                }
                let run = tasks
                    .close_run(run_id, str_field(payload, "status")?, "session", opt_str(payload, "session_id"), opt_str(payload, "branch"), opt_str(payload, "commit"), opt_str(payload, "result_message_id"))
                    .map_err(int)?
                    .map_err(refused)?;
                Ok(json!({ "run": run }))
            }
            _ => Err(HubError::new(404, "unknown operation")),
        }
    }

    /// Operator (HQ) calls, authenticated by the control bearer in the HTTP layer.
    pub fn operator_call(&self, method: &str, path: &str, body: &Value) -> Result<Value, HubError> {
        let (path, query) = path.split_once('?').unwrap_or((path, ""));
        let parts: Vec<&str> = path.trim_matches('/').split('/').collect();
        match (method, parts.as_slice()) {
            ("POST", ["v1", "tasks"]) => {
                let assignee = opt_str(body, "assignee");
                if let Some(node) = assignee {
                    if !self.admitted(node, "tasks") {
                        return Err(HubError::new(403, "assignee does not hold the tasks capability"));
                    }
                }
                let tasks = self.tasks.lock().expect("tasks");
                let op = opt_str(body, "op_id");
                if let Some(done) = tasks.replay(op).map_err(int)? {
                    return Ok(done);
                }
                let status = opt_str(body, "status");
                if let Some(st) = status {
                    if !matches!(st, "backlog" | "open" | "assigned") {
                        return Err(HubError::new(400, "initial status must be backlog, open or assigned"));
                    }
                }
                let task = tasks
                    .create_full(str_field(body, "title")?, opt_str(body, "body").unwrap_or(""), assignee, opt_str(body, "role"), body.get("parent_id").and_then(Value::as_i64), status)
                    .map_err(int)?;
                let out = json!({ "task": task });
                tasks.record(op, &out).map_err(int)?;
                Ok(out)
            }
            ("GET", ["v1", "tasks"]) => {
                let roles: Option<Vec<String>> = query_param(query, "role").map(|r| r.split(',').map(str::to_owned).collect());
                let tasks = self.tasks.lock().expect("tasks").list_filtered(query_param(query, "assignee"), roles.as_deref(), false).map_err(int)?;
                Ok(json!({ "tasks": tasks }))
            }
            ("GET", ["v1", "tasks", id]) => {
                let id: i64 = id.parse().map_err(|_| HubError::new(400, "bad id"))?;
                let tasks = self.tasks.lock().expect("tasks");
                let task = tasks.get(id).map_err(int)?.ok_or_else(|| HubError::new(404, "not found"))?;
                Ok(json!({ "task": task, "runs": tasks.runs(id).map_err(int)? }))
            }
            ("POST", ["v1", "tasks", id, "move"]) => {
                let id: i64 = id.parse().map_err(|_| HubError::new(400, "bad id"))?;
                let tasks = self.tasks.lock().expect("tasks");
                let op = opt_str(body, "op_id");
                if let Some(done) = tasks.replay(op).map_err(int)? {
                    return Ok(done);
                }
                let task = tasks
                    .move_to(id, str_field(body, "status")?, body.get("expected_revision").and_then(Value::as_i64))
                    .map_err(int)?
                    .map_err(refused)?;
                let out = json!({ "task": task });
                tasks.record(op, &out).map_err(int)?;
                Ok(out)
            }
            ("POST", ["v1", "tasks", id, "runs"]) => {
                let id: i64 = id.parse().map_err(|_| HubError::new(400, "bad id"))?;
                let run = self.tasks.lock().expect("tasks").start_run(id, str_field(body, "session_id")?).map_err(int)?.map_err(refused)?;
                Ok(json!({ "run": run }))
            }
            ("POST", ["v1", "runs", id, "close"]) => {
                let id: i64 = id.parse().map_err(|_| HubError::new(400, "bad id"))?;
                let run = self
                    .tasks
                    .lock()
                    .expect("tasks")
                    .close_run(id, str_field(body, "status")?, "operator", None, opt_str(body, "branch"), opt_str(body, "commit"), opt_str(body, "result_message_id"))
                    .map_err(int)?
                    .map_err(refused)?;
                Ok(json!({ "run": run }))
            }
            ("GET", ["v1", "routing"]) => {
                let (cfg, state) = self.routing()?;
                let seats: Vec<Value> = cfg
                    .seats
                    .iter()
                    .map(|seat| {
                        let st = state.get(&(seat.node.clone(), seat.provider.clone())).cloned().unwrap_or_default();
                        json!({ "seat": seat, "state": st })
                    })
                    .collect();
                Ok(json!({ "roles": cfg.roles, "seats": seats, "advisor": cfg.advisor }))
            }
            ("POST", ["v1", "routing", "recommend"]) => self.recommend(str_field(body, "role")?),
            ("GET", ["v1", "signals"]) => {
                let after = query_param(query, "after").and_then(|v| v.parse().ok());
                let limit = query_param(query, "limit").and_then(|v| v.parse().ok()).unwrap_or(100);
                let signals = self.tasks.lock().expect("tasks").signals(query_param(query, "node"), after, limit).map_err(int)?;
                Ok(json!({ "signals": signals }))
            }
            ("GET", ["v1", "sessions"]) => {
                let sessions = self.tasks.lock().expect("tasks").sessions(query_param(query, "node")).map_err(int)?;
                Ok(json!({ "sessions": sessions }))
            }
            // Wake a session: an m4a letter to its nick (the node turns it into a short wake).
            ("POST", ["v1", "sessions", id, "wake"]) => {
                let Some(m4a) = &self.m4a else { return Err(HubError::new(503, "core m4a is not configured")) };
                let nick = self.tasks.lock().expect("tasks").session_nick(id).map_err(int)?.ok_or_else(|| HubError::new(404, "session has no m4a nick"))?;
                let text = opt_str(body, "text").map(str::trim).filter(|t| !t.is_empty()).unwrap_or("wake: check your tasks and m4a inbox");
                if text.len() > 16 * 1024 {
                    return Err(HubError::new(400, "text too long"));
                }
                m4a.send(&nick, text).map_err(|e| HubError::new(502, e))?;
                Ok(json!({ "woke": id, "via": "m4a" }))
            }
            // Record a real provider session (a provider conversation id); drafts are not recorded.
            ("POST", ["v1", "sessions"]) => {
                let session = Session {
                    session_id: str_field(body, "session_id")?.to_owned(),
                    node_id: str_field(body, "node_id")?.to_owned(),
                    provider: str_field(body, "provider")?.to_owned(),
                    workspace_id: opt_str(body, "workspace_id").map(str::to_owned),
                    role: opt_str(body, "role").map(str::to_owned),
                    state: opt_str(body, "state").unwrap_or("live").to_owned(),
                    created_at_ms: 0,
                    updated_at_ms: 0,
                };
                let tasks = self.tasks.lock().expect("tasks");
                let session = tasks.upsert_session(&session).map_err(int)?;
                if let Some(nick) = opt_str(body, "nick").filter(|n| !n.is_empty() && n.len() <= 128) {
                    tasks.set_session_nick(&session.session_id, nick).map_err(int)?;
                }
                Ok(json!({ "session": session }))
            }
            ("POST", ["v1", "tasks", id, "assign"]) => {
                let id: i64 = id.parse().map_err(|_| HubError::new(400, "bad id"))?;
                let node = str_field(body, "assignee")?;
                if !self.admitted(node, "tasks") {
                    return Err(HubError::new(403, "assignee does not hold the tasks capability"));
                }
                if self.tasks.lock().expect("tasks").assign(id, node).map_err(|e| HubError::new(500, e.to_string()))? { Ok(json!({ "ok": true })) } else { Err(HubError::new(409, "task cannot be assigned")) }
            }
            ("POST", ["v1", "tasks", id, "cancel"]) => {
                let id: i64 = id.parse().map_err(|_| HubError::new(400, "bad id"))?;
                if self.tasks.lock().expect("tasks").set_status(id, "cancelled", None).map_err(|e| HubError::new(500, e.to_string()))? { Ok(json!({ "ok": true })) } else { Err(HubError::new(409, "task is final")) }
            }
            ("POST", ["v1", "mail", "send"]) => {
                let id = self.messenger.lock().expect("messenger").send(OPERATOR, str_field(body, "to")?, str_field(body, "subject")?, str_field(body, "body")?, None)?;
                self.bump_mail();
                Ok(json!({ "message_id": id }))
            }
            // Long-poll for HQ: returns as soon as the operator inbox holds a message newer than
            // `since_ms`, or after `timeout_secs` (max 30) with an empty list.
            ("POST", ["v1", "mail", "wait"]) => {
                let since = body.get("since_ms").and_then(Value::as_u64).unwrap_or(0);
                let deadline = Instant::now() + Duration::from_secs(body.get("timeout_secs").and_then(Value::as_u64).unwrap_or(25).min(30));
                loop {
                    let seen = *self.mail_signal.0.lock().expect("signal");
                    let mail: Vec<_> = self.messenger.lock().expect("messenger").inbox(OPERATOR, since, 100)?.into_iter().filter(|m| m.created_at_ms > since).collect();
                    if !mail.is_empty() {
                        return Ok(json!({ "messages": mail }));
                    }
                    let now = Instant::now();
                    if now >= deadline {
                        return Ok(json!({ "messages": [] }));
                    }
                    let guard = self.mail_signal.0.lock().expect("signal");
                    if *guard == seen {
                        let _ = self.mail_signal.1.wait_timeout(guard, deadline - now).expect("signal wait");
                    }
                }
            }
            ("POST", ["v1", "mail", "thread"]) => {
                let mail = self.messenger.lock().expect("messenger").thread(str_field(body, "node")?, 200)?;
                Ok(json!({ "messages": mail }))
            }
            ("POST", ["v1", "mail", "inbox"]) => {
                let mail = self.messenger.lock().expect("messenger").inbox(OPERATOR, 0, 100)?;
                Ok(json!({ "messages": mail }))
            }
            _ => Err(HubError::new(404, "not found")),
        }
    }
}

#[cfg(test)]
mod task_api_tests {
    use super::*;
    use crate::m4a_gateway::{Letter, M4a};
    use std::sync::{Arc, Mutex as StdMutex};

    #[derive(Clone, Default)]
    struct FakeM4a {
        sent: Arc<StdMutex<Vec<(String, String)>>>,
        inbox: Arc<StdMutex<Vec<Letter>>>,
    }
    impl M4a for FakeM4a {
        fn send(&self, to: &str, text: &str) -> Result<(), String> {
            self.sent.lock().unwrap().push((to.into(), text.into()));
            Ok(())
        }
        fn read_dm(&self, _nick: &str, _n: u32) -> Result<Vec<Letter>, String> {
            Ok(self.inbox.lock().unwrap().clone())
        }
        fn me(&self) -> &str {
            "hq-core"
        }
    }

    #[test]
    fn wake_sends_m4a_and_reader_closes_run_once() {
        let dir = tempfile::tempdir().unwrap();
        let fake = FakeM4a::default();
        let hub = Hub::open(dir.path()).unwrap().with_m4a(Box::new(fake.clone()));
        let op = |m: &str, p: &str, b: Value| hub.operator_call(m, p, &b);
        op("POST", "/v1/sessions", json!({"session_id": "s1", "node_id": "alpha", "provider": "codex", "nick": "s1nick"})).unwrap();
        let t = op("POST", "/v1/tasks", json!({"title": "t"})).unwrap();
        let tid = t["task"]["id"].as_i64().unwrap();
        let rid = op("POST", &format!("/v1/tasks/{tid}/runs"), json!({"session_id": "s1"})).unwrap()["run"]["id"].as_i64().unwrap();
        op("POST", "/v1/sessions/s1/wake", json!({"text": "task #1: fix tests"})).unwrap();
        assert_eq!(fake.sent.lock().unwrap()[0], ("s1nick".to_owned(), "task #1: fix tests".to_owned()));
        assert_eq!(op("POST", "/v1/sessions/nope/wake", json!({})).unwrap_err().status, 404);
        // Letters: one from a stranger, one from us naming another run, then the real close.
        fake.inbox.lock().unwrap().extend([
            Letter { event_id: "$1".into(), from: "@mallory:h".into(), body: format!("run.close {rid} done") },
            Letter { event_id: "$2".into(), from: "@s1nick:h".into(), body: "run.close 999 done".into() },
            Letter { event_id: "$3".into(), from: "@s1nick:h".into(), body: format!("run.close {rid} review feat/x abc") },
        ]);
        assert_eq!(hub.poll_m4a(), 1);
        assert_eq!(hub.poll_m4a(), 0); // handled once
        let got = op("GET", &format!("/v1/tasks/{tid}"), json!({})).unwrap();
        assert_eq!(got["runs"][0]["status"], "review");
        assert_eq!(got["runs"][0]["branch"], "feat/x");
        assert_eq!(got["runs"][0]["result_message_id"], "$3");
    }

    #[test]
    fn operator_task_create_move_cancel_and_runs() {
        let dir = tempfile::tempdir().unwrap();
        let hub = Hub::open(dir.path()).unwrap();
        let op = |m: &str, p: &str, b: Value| hub.operator_call(m, p, &b);
        let t = op("POST", "/v1/tasks", json!({"title": "port", "role": "dev", "status": "backlog", "op_id": "c1"})).unwrap();
        let id = t["task"]["id"].as_i64().unwrap();
        // Idempotent create.
        assert_eq!(op("POST", "/v1/tasks", json!({"title": "port", "op_id": "c1"})).unwrap(), t);
        assert_eq!(op("GET", "/v1/tasks?role=advisor", json!({})).unwrap()["tasks"].as_array().unwrap().len(), 0);
        assert_eq!(op("GET", "/v1/tasks?role=dev", json!({})).unwrap()["tasks"].as_array().unwrap().len(), 1);
        let moved = op("POST", &format!("/v1/tasks/{id}/move"), json!({"status": "open", "expected_revision": 1})).unwrap();
        assert_eq!(moved["task"]["revision"], 2);
        assert_eq!(op("POST", &format!("/v1/tasks/{id}/move"), json!({"status": "open", "expected_revision": 1})).unwrap_err().status, 409);
        // Runs need a recorded real session.
        assert_eq!(op("POST", &format!("/v1/tasks/{id}/runs"), json!({"session_id": "s1"})).unwrap_err().status, 400);
        op("POST", "/v1/sessions", json!({"session_id": "s1", "node_id": "alpha", "provider": "codex"})).unwrap();
        let run = op("POST", &format!("/v1/tasks/{id}/runs"), json!({"session_id": "s1"})).unwrap();
        let rid = run["run"]["id"].as_i64().unwrap();
        // Node path is gated by the ledger.
        assert_eq!(hub.node_call("alpha", "run.close", &json!({"run_id": rid, "status": "done", "session_id": "s1"})).unwrap_err().status, 403);
        let closed = op("POST", &format!("/v1/runs/{rid}/close"), json!({"status": "cancelled"})).unwrap();
        assert_eq!(closed["run"]["closed_by"], "operator");
        let got = op("GET", &format!("/v1/tasks/{id}"), json!({})).unwrap();
        assert_eq!(got["task"]["status"], "cancelled");
        assert_eq!(got["runs"].as_array().unwrap().len(), 1);
        assert_eq!(op("POST", &format!("/v1/tasks/{id}/move"), json!({"status": "open"})).unwrap_err().status, 409);
        assert_eq!(op("GET", "/v1/sessions?node=alpha", json!({})).unwrap()["sessions"].as_array().unwrap().len(), 1);
    }
}