trusty-mpm 0.19.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use super::*;

#[test]
fn base_url_is_stored() {
    let client = DaemonClient::new("http://127.0.0.1:7880");
    assert_eq!(client.base_url(), "http://127.0.0.1:7880");
}

#[test]
fn with_client_reuses_passed_client() {
    // Why: callers that configured a `reqwest::Client` (TLS/timeout/proxy/pool)
    // must keep that configuration; `with_client` adopts the passed client
    // verbatim instead of minting a fresh default one.
    let configured = reqwest::Client::new();
    let client = DaemonClient::with_client(configured, "http://127.0.0.1:7880");
    assert_eq!(client.base_url(), "http://127.0.0.1:7880");
}

#[test]
fn set_base_url_repoints_client() {
    // Why: a long-lived UI must follow the daemon to a new ephemeral port
    // after a restart; `set_base_url` is what makes that re-pointing possible.
    let mut client = DaemonClient::new("http://127.0.0.1:7880");
    client.set_base_url("http://127.0.0.1:54321");
    assert_eq!(client.base_url(), "http://127.0.0.1:54321");
}

#[tokio::test]
async fn launch_session_errors_when_daemon_unreachable() {
    // Why: `/connect <dir>` launches via `launch_session`; when the daemon
    // POST fails (port 0 never connects) the error must surface rather than
    // proceeding to spawn tmux against an unregistered session.
    let client = DaemonClient::new("http://127.0.0.1:0");
    let result = client.launch_session("/tmp/no-such-project").await;
    assert!(result.is_err(), "expected launch to fail with no daemon");
}

#[tokio::test]
async fn connect_session_errors_when_daemon_unreachable() {
    // Why: `tm connect` registers via `POST /api/v1/sessions/connect`
    // before touching tmux; when the daemon POST fails the error must
    // surface rather than proceeding to spawn tmux against an
    // unregistered session.
    let client = DaemonClient::new("http://127.0.0.1:0");
    let result = client.connect_session("/tmp/no-such-project").await;
    assert!(result.is_err(), "expected connect to fail with no daemon");
}

#[test]
fn session_row_deserializes_tmux_name() {
    let json = serde_json::json!({
        "id": "abcd1234-5678-90ab-cdef-1234567890ab",
        "workdir": "/tmp/proj",
        "status": "Active",
        "active_delegations": 1,
        "tmux_name": "tmpm-quiet-falcon"
    });
    let row: SessionRow = serde_json::from_value(json).unwrap();
    assert_eq!(row.tmux_name, "tmpm-quiet-falcon");
}

#[test]
fn session_row_defaults_tmux_name_when_absent() {
    let json = serde_json::json!({
        "id": "abcd1234-5678-90ab-cdef-1234567890ab",
        "workdir": "/tmp/proj",
        "status": "Active"
    });
    let row: SessionRow = serde_json::from_value(json).unwrap();
    assert_eq!(row.tmux_name, "");
    assert_eq!(row.last_seen.secs_since_epoch, 0);
}

#[test]
fn events_deserialize_from_record_shape() {
    let json = serde_json::json!({
        "session": "abcd1234-5678-90ab-cdef-1234567890ab",
        "event": "PreToolUse",
        "at": "2024-01-01T00:00:00Z",
        "payload": {}
    });
    let row: EventRow = serde_json::from_value(json).unwrap();
    assert_eq!(row.event, crate::core::hook::HookEvent::PreToolUse);
    assert_eq!(row.at, "2024-01-01T00:00:00Z");
}

#[test]
fn events_default_payload_when_absent() {
    let json = serde_json::json!({
        "session": "abcd1234-5678-90ab-cdef-1234567890ab",
        "event": "Stop",
        "at": "2024-01-01T00:00:00Z"
    });
    let row: EventRow = serde_json::from_value(json).unwrap();
    assert!(row.payload.is_null());
}

#[test]
fn breakers_deserialize_from_api_shape() {
    let json = serde_json::json!({
        "agent": "research",
        "breaker": { "state": "closed", "consecutive_failures": 0 }
    });
    #[derive(serde::Deserialize)]
    struct WireBreaker {
        state: String,
        consecutive_failures: u32,
    }
    #[derive(serde::Deserialize)]
    struct WireRow {
        agent: String,
        breaker: WireBreaker,
    }
    let row: WireRow = serde_json::from_value(json).unwrap();
    assert_eq!(row.agent, "research");
    assert_eq!(row.breaker.state, "closed");
    assert_eq!(row.breaker.consecutive_failures, 0);
}

#[test]
fn tmux_session_row_accepts_name() {
    // The snapshot helper joins a `lines` array; the name parse is exercised
    // here directly on both wire shapes.
    let obj = serde_json::json!({"name": "tmpm-quiet-falcon"});
    assert_eq!(
        obj.get("name").and_then(|v| v.as_str()),
        Some("tmpm-quiet-falcon")
    );
    let plain = serde_json::json!("external-shell");
    assert_eq!(plain.as_str(), Some("external-shell"));
}

#[test]
fn snapshot_text_handles_each_shape() {
    assert_eq!(snapshot_text(&serde_json::json!("plain")), "plain");
    assert_eq!(
        snapshot_text(&serde_json::json!({"content": "from content"})),
        "from content"
    );
    assert_eq!(
        snapshot_text(&serde_json::json!({"lines": ["a", "b"]})),
        "a\nb"
    );
}

#[test]
fn pair_request_deserializes() {
    let json = serde_json::json!({"code": "A4X9KZ", "expires_in_seconds": 300});
    let req: PairRequest = serde_json::from_value(json).unwrap();
    assert_eq!(req.code, "A4X9KZ");
    assert_eq!(req.expires_in_seconds, 300);
}

#[test]
fn pair_confirm_deserializes_failure() {
    let json = serde_json::json!({"success": false, "error": "invalid or expired code"});
    let confirm: PairConfirm = serde_json::from_value(json).unwrap();
    assert!(!confirm.success);
    assert_eq!(confirm.error.as_deref(), Some("invalid or expired code"));
    assert_eq!(confirm.chat_id, None);
}

#[test]
fn llm_chat_message_round_trips() {
    // A ChatMessage serializes to the `{role, content}` wire shape the
    // daemon expects and deserializes back unchanged.
    let msg = ChatMessage {
        role: "user".into(),
        content: "hello".into(),
    };
    let json = serde_json::to_value(&msg).unwrap();
    assert_eq!(json["role"], "user");
    assert_eq!(json["content"], "hello");
    let back: ChatMessage = serde_json::from_value(json).unwrap();
    assert_eq!(back, msg);
}

#[test]
fn chat_message_constructors_set_role() {
    assert_eq!(ChatMessage::user("x").role, "user");
    assert_eq!(ChatMessage::assistant("y").role, "assistant");
    assert_eq!(ChatMessage::user("x").content, "x");
}

#[test]
fn llm_chat_response_deserializes() {
    // The `POST /llm/chat` response carries the reply and updated history.
    let json = serde_json::json!({
        "reply": "hi there",
        "history": [
            { "role": "user", "content": "hello" },
            { "role": "assistant", "content": "hi there" },
        ],
    });
    let outcome: LlmChatOutcome = serde_json::from_value(json).unwrap();
    assert_eq!(outcome.reply, "hi there");
    assert_eq!(outcome.history.len(), 2);
    assert_eq!(outcome.history[1].role, "assistant");
}

#[test]
fn coordinator_context_deserializes() {
    // The `GET /api/v1/sessions/context` snapshot carries the session
    // summaries; the daemon's `recent_events` field is ignored.
    let json = serde_json::json!({
        "sessions": [{
            "id": "00000000-0000-0000-0000-000000000000",
            "name": "tmpm-aipowerranking",
            "prefix": "aipowerranking",
            "workdir": "/tmp/proj",
            "status": "Active",
            "active_delegations": 3,
            "recent_output": ["building…"],
        }],
        "recent_events": [],
        "generated_at": "2026-05-19T00:00:00Z",
    });
    let context: CoordinatorContext = serde_json::from_value(json).unwrap();
    assert_eq!(context.sessions.len(), 1);
    assert_eq!(context.sessions[0].prefix, "aipowerranking");
    assert_eq!(context.sessions[0].active_delegations, 3);
    // This payload is what an OLDER daemon (pre-#1275) emits — no summary
    // fields. `#[serde(default)]` makes the client tolerant: absent → None/false.
    assert_eq!(context.sessions[0].last_summary, None);
    assert!(!context.sessions[0].summarizing);
}

#[test]
fn coordinator_session_carries_summary_fields() {
    // A current daemon (#1275) emits `last_summary` + `summarizing`; the client
    // deserializes both so the TUI can render the bullet and blink (DOC-16 §6.2).
    let json = serde_json::json!({
        "sessions": [{
            "id": "00000000-0000-0000-0000-000000000000",
            "name": "tmpm-aipowerranking",
            "prefix": "aipowerranking",
            "workdir": "/tmp/proj",
            "status": "Active",
            "active_delegations": 0,
            "recent_output": [],
            "last_summary": "Writing the parser tests",
            "summarizing": true,
        }],
    });
    let context: CoordinatorContext = serde_json::from_value(json).unwrap();
    assert_eq!(
        context.sessions[0].last_summary.as_deref(),
        Some("Writing the parser tests")
    );
    assert!(context.sessions[0].summarizing);
}

#[test]
fn coordinator_chat_outcome_deserializes() {
    // A routed-command outcome carries the session name and pane output.
    let json = serde_json::json!({
        "reply": "Sent to tmpm-foo: run tests",
        "routed_to_session": "tmpm-foo",
        "command_output": "tests passed",
    });
    let outcome: CoordinatorChatOutcome = serde_json::from_value(json).unwrap();
    assert_eq!(outcome.routed_to_session.as_deref(), Some("tmpm-foo"));
    assert_eq!(outcome.command_output.as_deref(), Some("tests passed"));

    // A plain LLM reply omits the routing fields.
    let json = serde_json::json!({ "reply": "two sessions are active" });
    let outcome: CoordinatorChatOutcome = serde_json::from_value(json).unwrap();
    assert_eq!(outcome.reply, "two sessions are active");
    assert!(outcome.routed_to_session.is_none());
    // Additive action-path fields default to None when absent.
    assert!(outcome.actions_taken.is_none());
    assert!(outcome.conv_id.is_none());
}

#[test]
fn coordinator_chat_outcome_deserializes_actions() {
    // The action-capable path returns the audit trail and the conversation id.
    let json = serde_json::json!({
        "reply": "ran a health check and listed the fleet",
        "actions_taken": ["sessions.health", "sessions.list"],
        "conv_id": "conv-42",
    });
    let outcome: CoordinatorChatOutcome = serde_json::from_value(json).unwrap();
    assert_eq!(
        outcome.actions_taken.as_deref(),
        Some(["sessions.health".to_string(), "sessions.list".to_string()].as_slice())
    );
    assert_eq!(outcome.conv_id.as_deref(), Some("conv-42"));
}

#[test]
fn coordinator_chat_serializes_actions_flag() {
    // The request body must carry the `actions` boolean so the daemon can route
    // the action-capable SM branch; history is threaded through verbatim.
    let history = [ChatMessage::user("hi"), ChatMessage::assistant("hello")];
    let body = coordinator_chat_body("spin up a session", &history, true);
    assert_eq!(body["message"], "spin up a session");
    assert_eq!(body["actions"], true);
    assert_eq!(body["history"][0]["role"], "user");
    assert_eq!(body["history"][1]["content"], "hello");

    // The passive path serializes `actions: false`.
    let passive = coordinator_chat_body("just chatting", &[], false);
    assert_eq!(passive["actions"], false);
}

#[test]
fn pair_status_deserializes() {
    let json = serde_json::json!({"paired": true, "chat_id": 12345678});
    let status: PairStatus = serde_json::from_value(json).unwrap();
    assert!(status.paired);
    assert_eq!(status.chat_id, Some(12345678));
}

#[test]
fn catalog_stale_health_body_wire_shape() {
    // The HR-3 `catalog_stale` flag must round-trip from the `/health` body, and
    // a body MISSING the field (an older daemon) must default to `false` so
    // `DaemonClient::catalog_stale` degrades to "no updates" instead of erroring.
    // This mirrors the private `HealthBody` the client parses.
    #[derive(serde::Deserialize)]
    struct HealthBody {
        #[serde(default)]
        catalog_stale: bool,
    }

    let stale: HealthBody =
        serde_json::from_value(serde_json::json!({"status":"ok","catalog_stale":true})).unwrap();
    assert!(stale.catalog_stale);

    let fresh: HealthBody =
        serde_json::from_value(serde_json::json!({"status":"ok","catalog_stale":false})).unwrap();
    assert!(!fresh.catalog_stale);

    // Legacy daemon: no `catalog_stale` field present → defaults to false.
    let legacy: HealthBody = serde_json::from_value(serde_json::json!({"status":"ok"})).unwrap();
    assert!(!legacy.catalog_stale, "missing field defaults to false");
}

// ── Managed session-manager wire-shape round-trips ─────────────────────────────

#[test]
fn managed_session_summary_deserializes() {
    // A full summary round-trips; the optional fields are also tolerant of being
    // absent (an older/leaner daemon) via `#[serde(default)]`.
    let json = serde_json::json!({
        "id": "00000000-0000-0000-0000-000000000001",
        "name": "tmpm-brave-otter",
        "state": "running",
        "workspace_path": "/tmp/ws",
        "repo_url": "https://example.com/r.git",
        "branch": "main",
        "created_at": "2026-06-19T00:00:00Z",
        "last_activity_at": "2026-06-19T01:00:00Z",
        "pending_decision": "overwrite?",
        "proposed_default": "yes",
        "source_id": "owner/repo",
    });
    let s: ManagedSessionSummary = serde_json::from_value(json).unwrap();
    assert_eq!(s.id, "00000000-0000-0000-0000-000000000001");
    assert_eq!(s.name, "tmpm-brave-otter");
    assert_eq!(s.state, "running");
    assert_eq!(s.created_at.as_deref(), Some("2026-06-19T00:00:00Z"));
    assert_eq!(s.pending_decision.as_deref(), Some("overwrite?"));
    // source_id round-trips (#1730).
    assert_eq!(s.source_id.as_deref(), Some("owner/repo"));

    // Minimal body: only the always-present fields.
    let lean = serde_json::json!({"id": "x", "name": "n", "state": "stopped"});
    let s: ManagedSessionSummary = serde_json::from_value(lean).unwrap();
    assert_eq!(s.id, "x");
    assert!(s.workspace_path.is_none());
    // An absent `created_at` deserializes to `None`, not an empty string.
    assert!(s.created_at.is_none());
    assert!(s.pending_decision.is_none());
    // An absent `source_id` defaults to None (legacy sessions without it).
    assert!(s.source_id.is_none());
}

#[test]
fn managed_list_response_deserializes() {
    let json = serde_json::json!({
        "sessions": [
            {"id": "a", "name": "tmpm-a", "state": "running"},
            {"id": "b", "name": "tmpm-b", "state": "stopped"},
        ],
    });
    let body: ManagedListResponse = serde_json::from_value(json).unwrap();
    assert_eq!(body.sessions.len(), 2);
    assert_eq!(body.sessions[1].name, "tmpm-b");

    // An empty / absent `sessions` array defaults to empty.
    let empty: ManagedListResponse = serde_json::from_value(serde_json::json!({})).unwrap();
    assert!(empty.sessions.is_empty());
}

#[test]
fn managed_spawn_request_serializes() {
    // The `git_ref` field must serialize under the wire key `ref`, and absent
    // optionals must be omitted so the daemon sees them as null/defaulted.
    let req = ManagedSpawnRequest {
        repo_url: "https://example.com/r.git".to_string(),
        git_ref: "main".to_string(),
        task: "do the thing".to_string(),
        name_hint: Some("tmpm-custom".to_string()),
        runtime: Some("tcode".to_string()),
    };
    let v = serde_json::to_value(&req).unwrap();
    assert_eq!(v["ref"], "main");
    assert_eq!(v["repo_url"], "https://example.com/r.git");
    assert_eq!(v["name_hint"], "tmpm-custom");
    assert_eq!(v["runtime"], "tcode");
    assert!(v.get("git_ref").is_none(), "must use wire key `ref`");

    let bare = ManagedSpawnRequest {
        repo_url: "r".to_string(),
        git_ref: "r".to_string(),
        task: "t".to_string(),
        name_hint: None,
        runtime: None,
    };
    let v = serde_json::to_value(&bare).unwrap();
    assert!(v.get("name_hint").is_none(), "None name_hint is omitted");
    assert!(v.get("runtime").is_none(), "None runtime is omitted");
}

#[test]
fn managed_spawn_response_deserializes() {
    let json = serde_json::json!({
        "id": "id-1",
        "name": "tmpm-x",
        "state": "running",
        "created_at": "2026-06-19T00:00:00Z",
        "attach_cmd": "tmux attach-session -t tmpm-x",
        "runtime": "claude-code",
    });
    let r: ManagedSpawnResponse = serde_json::from_value(json).unwrap();
    assert_eq!(r.id, "id-1");
    assert_eq!(r.created_at.as_deref(), Some("2026-06-19T00:00:00Z"));
    assert_eq!(r.attach_cmd, "tmux attach-session -t tmpm-x");
    assert_eq!(r.runtime, "claude-code");

    // An absent `created_at` deserializes to `None`, while `attach_cmd` and
    // `runtime` remain plain strings defaulting to empty.
    let lean = serde_json::json!({"id": "id-2", "name": "tmpm-y", "state": "running"});
    let r: ManagedSpawnResponse = serde_json::from_value(lean).unwrap();
    assert!(r.created_at.is_none());
    assert_eq!(r.attach_cmd, "");
    assert_eq!(r.runtime, "");
}

#[test]
fn managed_adopt_request_serializes() {
    // Required fields are always present; absent optionals are omitted so the
    // daemon defaults them (#1433).
    let req = ManagedAdoptRequest {
        tmux_name: "tmpm-hand-started".to_string(),
        cwd: "/Users/op/work/proj".to_string(),
        task: Some("drive it".to_string()),
        runtime: Some("claude-code".to_string()),
    };
    let v = serde_json::to_value(&req).unwrap();
    assert_eq!(v["tmux_name"], "tmpm-hand-started");
    assert_eq!(v["cwd"], "/Users/op/work/proj");
    assert_eq!(v["task"], "drive it");
    assert_eq!(v["runtime"], "claude-code");

    let bare = ManagedAdoptRequest {
        tmux_name: "my-cli-session".to_string(),
        cwd: "/x".to_string(),
        task: None,
        runtime: None,
    };
    let v = serde_json::to_value(&bare).unwrap();
    assert_eq!(v["tmux_name"], "my-cli-session");
    assert!(v.get("task").is_none(), "None task is omitted");
    assert!(v.get("runtime").is_none(), "None runtime is omitted");
}

#[test]
fn managed_adopt_response_deserializes() {
    let json = serde_json::json!({
        "id": "id-9",
        "name": "tmpm-hand-started",
        "state": "active",
        "cwd": "/Users/op/work/proj",
        "runtime": "claude-code",
        "attach_cmd": "tmux attach-session -t tmpm-hand-started",
    });
    let r: ManagedAdoptResponse = serde_json::from_value(json).unwrap();
    assert_eq!(r.id, "id-9");
    assert_eq!(r.name, "tmpm-hand-started");
    assert_eq!(r.state, "active");
    assert_eq!(r.cwd, "/Users/op/work/proj");
    assert_eq!(r.runtime, "claude-code");
    assert_eq!(r.attach_cmd, "tmux attach-session -t tmpm-hand-started");

    // A lean response (only id/name/state) still deserializes; the defaulted
    // string fields fall back to empty.
    let lean = serde_json::json!({"id": "id-10", "name": "x", "state": "active"});
    let r: ManagedAdoptResponse = serde_json::from_value(lean).unwrap();
    assert_eq!(r.cwd, "");
    assert_eq!(r.runtime, "");
    assert_eq!(r.attach_cmd, "");
}

#[test]
fn managed_send_and_answer_round_trip() {
    let v = serde_json::to_value(ManagedSendInputRequest {
        text: "hello".to_string(),
    })
    .unwrap();
    assert_eq!(v["text"], "hello");
    let sent: ManagedSendInputResponse =
        serde_json::from_value(serde_json::json!({"sent": true, "tmux_name": "tmpm-x"})).unwrap();
    assert!(sent.sent);
    assert_eq!(sent.tmux_name, "tmpm-x");

    let v = serde_json::to_value(ManagedAnswerRequest {
        answer: "yes".to_string(),
    })
    .unwrap();
    assert_eq!(v["answer"], "yes");
    let answered: ManagedAnswerResponse =
        serde_json::from_value(serde_json::json!({"injected": true, "tmux_name": "tmpm-x"}))
            .unwrap();
    assert!(answered.injected);
}

#[test]
fn managed_attach_cmd_response_deserializes() {
    let r: ManagedAttachCmdResponse =
        serde_json::from_value(serde_json::json!({"attach_cmd": "tmux attach-session -t tmpm-x"}))
            .unwrap();
    assert_eq!(r.attach_cmd, "tmux attach-session -t tmpm-x");
}

#[test]
fn managed_activity_response_deserializes() {
    // Both with and without the optional classifier overlay.
    let json = serde_json::json!({
        "raw_pane": "line1\nline2",
        "runtime_active": true,
        "state": "working",
        "summary": "running tests",
        "confidence": 0.8_f32,
        "cache_hit": false,
        "input_tokens": 10,
        "output_tokens": 5,
        "latency_ms": 42,
        "total_input_tokens": 100,
        "total_output_tokens": 50,
        "classification": "working",
        "pending_decision": "overwrite?",
        "proposed_default": "yes",
    });
    let a: ManagedActivityResponse = serde_json::from_value(json).unwrap();
    assert_eq!(a.state, "working");
    assert!(a.runtime_active);
    assert_eq!(a.classification.as_deref(), Some("working"));
    assert_eq!(a.pending_decision.as_deref(), Some("overwrite?"));

    // No classifier ran: `classification` null, raw pane still present.
    let json = serde_json::json!({
        "raw_pane": "pane",
        "runtime_active": false,
        "state": "unknown",
        "summary": "",
        "confidence": 0.0_f32,
        "cache_hit": false,
        "input_tokens": 0,
        "output_tokens": 0,
        "latency_ms": 0,
        "total_input_tokens": 0,
        "total_output_tokens": 0,
        "classification": null,
    });
    let a: ManagedActivityResponse = serde_json::from_value(json).unwrap();
    assert_eq!(a.raw_pane, "pane");
    assert!(a.classification.is_none());
    assert!(a.pending_decision.is_none());
}

#[test]
fn managed_session_urls_use_managed_route_family() {
    // The managed methods build URLs off the `/api/v1/sessions/managed` base and
    // must NOT collide with the legacy `resume_session` route (`/sessions/{id}/
    // resume`). This guards the route family contract that STUI/TELUI converge on.
    let client = DaemonClient::new("http://127.0.0.1:7880");
    assert_eq!(client.base_url(), "http://127.0.0.1:7880");
    // Construct the same format strings the methods use, to lock the shape.
    let base = client.base_url();
    assert_eq!(
        format!("{base}/api/v1/sessions/managed/{id}/resume", id = "abc"),
        "http://127.0.0.1:7880/api/v1/sessions/managed/abc/resume"
    );
    // The legacy method targets a different path; the two never overlap.
    assert_eq!(
        format!("{base}/sessions/{id}/resume", id = "abc"),
        "http://127.0.0.1:7880/sessions/abc/resume"
    );
}

#[test]
fn health_snapshot_deserializes() {
    // The `/health` body deserializes into HealthSnapshot; the catalog flags
    // default when absent so an older daemon returning only `status` still parses.
    let full: HealthSnapshot = serde_json::from_value(serde_json::json!({
        "status": "ok",
        "catalog_stale": true,
        "catalog_unknown": false,
        "catalog_changes": ["agents/foo"],
    }))
    .expect("full health body parses");
    assert_eq!(full.status, "ok");
    assert!(full.catalog_stale);
    assert!(!full.catalog_unknown);

    let minimal: HealthSnapshot =
        serde_json::from_value(serde_json::json!({ "status": "ok" })).expect("minimal body parses");
    assert_eq!(minimal.status, "ok");
    assert!(!minimal.catalog_stale);
    assert!(!minimal.catalog_unknown);
}