trusty-code 0.4.1

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! Tests for `serve::http`'s axum router assembly (`build_axum_router`) and
//! its routes (`POST /rpc`, `GET /health`, `GET /sessions/{id}/events`).
//!
//! Why: split out of `http.rs` per the crate's `_tests.rs` sibling-file
//! convention (see `events_tests`/`workstreams::model_tests` for precedent)
//! so the router's test surface — which grew with issue #3297's new
//! `SharedWorkstreamStore` parameter and its `GET /workstreams/{id}/events`
//! merge — doesn't push the production file past its 500-SLOC cap; test
//! files carry the 1500-SLOC cap.
//! What: `POST /rpc` ping/malformed-JSON, `GET /health`, every REST resource
//! group's merge-in smoke test, and the `GET /sessions/{id}/events` SSE
//! replay/live/404 behaviour.
//! Test: this module is itself the test surface.

use super::*;
use axum::body::{Body, to_bytes};
use axum::http::{Request, StatusCode};
use serde_json::{Value, json};
use tower::util::ServiceExt;
use trusty_mcp::error_codes;

pub(super) async fn router_and_sessions()
-> (Arc<Router>, Arc<SessionRegistry>, SharedWorkstreamStore) {
    let sessions = Arc::new(SessionRegistry::new());
    let workstreams = test_workstreams_store().await;
    let mut router = Router::new();
    crate::serve::methods::register(&mut router, crate::binding::ProjectBinding::None);
    crate::session::protocol::register(&mut router, sessions.clone(), workstreams.clone());
    (Arc::new(router), sessions, workstreams)
}

/// A fresh, tempdir-backed `SharedWorkstreamStore` with no workstreams
/// yet — every test in this file that doesn't specifically exercise
/// `workstream.*` just needs SOMETHING to pass to `build_axum_router`
/// (issue #3297 added the store as a required parameter so
/// `GET /workstreams/{id}/events` can be merged in).
pub(super) async fn test_workstreams_store() -> SharedWorkstreamStore {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("workstreams-test.json");
    let store = crate::workstreams::WorkstreamStore::load(path)
        .await
        .expect("load");
    std::mem::forget(dir);
    Arc::new(tokio::sync::Mutex::new(store))
}

/// The projectless binding every route test that doesn't specifically
/// exercise `GET /health`'s binding field passes to `build_axum_router`
/// (#4512 made it a required parameter so the daemon can publish which
/// project it serves).
pub(super) fn test_binding() -> Arc<crate::binding::ProjectBinding> {
    Arc::new(crate::binding::ProjectBinding::None)
}

async fn body_json(response: axum::response::Response) -> Value {
    let bytes = to_bytes(response.into_body(), 1024 * 1024).await.unwrap();
    serde_json::from_slice(&bytes).unwrap()
}

/// `POST /rpc` with a `ping` request must return HTTP 200 and the same
/// `{"pong": true}` result the STDIO transport returns.
#[tokio::test]
async fn http_rpc_ping_returns_pong() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let app = build_axum_router(router, sessions, workstreams, test_binding());
    let req_body = json!({"jsonrpc": "2.0", "id": 1, "method": "ping"}).to_string();

    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/rpc")
                .header("content-type", "application/json")
                .body(Body::from(req_body))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert_eq!(v["result"], json!({"pong": true}));
    assert_eq!(v["id"], 1);
}

/// `POST /rpc` with a malformed body must still return HTTP 200 with a
/// JSON-RPC `-32700 Parse error` envelope, not a bare HTTP 400.
#[tokio::test]
async fn http_rpc_malformed_json_returns_parse_error() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let app = build_axum_router(router, sessions, workstreams, test_binding());

    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/rpc")
                .header("content-type", "application/json")
                .body(Body::from("not json at all"))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert_eq!(v["error"]["code"], error_codes::PARSE_ERROR);
}

/// `GET /health` must return the exact payload `health_payload()`
/// (and thus the `health` JSON-RPC method) returns.
#[tokio::test]
async fn http_health_matches_jsonrpc_health_payload() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let app = build_axum_router(router, sessions, workstreams, test_binding());

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/health")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert_eq!(v, health_payload(&crate::binding::ProjectBinding::None));
}

/// `GET /health` must publish the daemon's OWN binding, not a placeholder —
/// this is the field `cli::daemon_autospawn` compares before attaching, so a
/// daemon that reported the wrong project would defeat the whole check
/// (#4512).
#[tokio::test]
async fn http_health_reports_the_daemons_project_binding() {
    let project = tempfile::tempdir().expect("project tempdir");
    let binding = crate::binding::ProjectBinding::resolve(Some(project.path().to_path_buf()))
        .expect("tempdir must bind");
    let (router, sessions, workstreams) = router_and_sessions().await;
    let app = build_axum_router(router, sessions, workstreams, Arc::new(binding.clone()));

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/health")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert_eq!(v["binding"], binding.to_json());
    assert_eq!(v["pid"], std::process::id());
}

/// `GET /sessions` (the #2983 Slice 2 REST route group, merged in by
/// `build_axum_router`) must be reachable on the SAME router `POST /rpc`
/// and `GET /sessions/{id}/events` are — proving the merge in
/// `build_axum_router` actually wires `rest::sessions::routes` rather
/// than silently dropping it. Route-level behaviour (found/missing/
/// transcript/readiness/goals) is covered by `rest::sessions::tests`.
#[tokio::test]
async fn http_rest_sessions_route_is_merged_in() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let app = build_axum_router(router, sessions, workstreams, test_binding());

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/sessions")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert!(v["sessions"].is_array());
}

/// `POST /sessions` (the #2983 Slice 3 REST write route group, merged in
/// by `build_axum_router` via `rest::sessions_write::routes`) must be
/// reachable on the SAME router `GET /sessions` is (the previous test) —
/// proving axum's `.merge()` doesn't silently drop one side when two
/// sub-routers register the SAME literal path under different HTTP
/// methods. `GET`+`POST` both claim `/sessions`; `PUT`+`DELETE` both
/// claim `/sessions/{id}/goal` — this test pins both same-literal-path
/// pairs in one pass, driving the created session's id through a real
/// `PUT` then `DELETE` on `/goal` and asserting a clean `200` round trip
/// (seeding a `pm_transcript` first via the registry so `set_goal`/
/// `clear_goal`'s "has a transcript" precondition passes — otherwise a
/// merge bug that silently 404'd the route would be indistinguishable
/// from a legitimate `400 invalid_argument`). Per-route error/malformed-
/// body behaviour is already covered by `rest::sessions_write::tests`;
/// this test only pins the merge itself.
#[tokio::test]
async fn http_rest_post_sessions_route_is_merged_in() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let sessions_for_seeding = sessions.clone();
    let app = build_axum_router(router, sessions, workstreams, test_binding());

    let create_resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/sessions")
                .header("content-type", "application/json")
                .body(Body::from(json!({"task": "do it"}).to_string()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(create_resp.status(), StatusCode::CREATED);
    let created = body_json(create_resp).await;
    assert_eq!(created["status"], "running");
    assert_eq!(created["task"], "do it");
    let session_id = created["id"].as_str().unwrap().to_string();

    let transcript = sessions_for_seeding
        .begin_pm_transcript(&session_id, "you are the pm", "first task")
        .unwrap();
    sessions_for_seeding.store_pm_transcript(&session_id, transcript);

    let put_resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("PUT")
                .uri(format!("/sessions/{session_id}/goal"))
                .header("content-type", "application/json")
                .body(Body::from(
                    json!({"slot": 1, "text": "ship it"}).to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(put_resp.status(), StatusCode::OK);
    assert_eq!(body_json(put_resp).await, json!({}));

    let delete_resp = app
        .oneshot(
            Request::builder()
                .method("DELETE")
                .uri(format!("/sessions/{session_id}/goal"))
                .header("content-type", "application/json")
                .body(Body::from(json!({"slot": 1}).to_string()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(delete_resp.status(), StatusCode::OK);
    assert_eq!(body_json(delete_resp).await, json!({}));
}

/// `POST /tasks` (the #2983 Slice 4 REST route group, merged in by
/// `build_axum_router` via `rest::tasks::routes`) must be reachable on
/// the SAME router `POST /sessions` is — proving the merge actually
/// wires `rest::tasks::routes` rather than silently dropping it.
/// Per-route error/status-code behaviour is already covered by
/// `rest::tasks::tests`; this test only pins the merge itself.
#[tokio::test]
async fn http_rest_post_tasks_route_is_merged_in() {
    let _guard = crate::task::mock_llm::MOCK_LLM_ENV_LOCK.lock().await;
    // SAFETY: test-only env mutation; serialized by `MOCK_LLM_ENV_LOCK`.
    unsafe {
        std::env::set_var(
            crate::task::mock_llm::MOCK_LLM_ENV,
            crate::task::mock_llm::MOCK_LLM_ECHO,
        );
    }
    // `router_and_sessions()` does not register `task.run` (it is not
    // needed by any other test in this file) — build a router that also
    // has it wired, mirroring `task::protocol::tests::agents_dir`.
    let sessions = Arc::new(SessionRegistry::new());
    let agents = tempfile::tempdir().expect("agents tempdir");
    std::fs::write(
        agents.path().join("pm.md"),
        "---\nname: pm\nmodel: openai/gpt-4o-mini\n---\n\nYou are the PM.\n",
    )
    .expect("write pm.md");
    let project = tempfile::tempdir().expect("project tempdir");
    let workstreams = test_workstreams_store().await;
    let mut router = Router::new();
    crate::serve::methods::register(&mut router, crate::binding::ProjectBinding::None);
    crate::session::protocol::register(&mut router, sessions.clone(), workstreams.clone());
    crate::task::protocol::register(
        &mut router,
        sessions.clone(),
        crate::binding::ProjectBinding::resolve(Some(project.path().to_path_buf()))
            .expect("tempdir must bind"),
        agents.path().to_path_buf(),
        workstreams.clone(),
    );
    let app = build_axum_router(Arc::new(router), sessions, workstreams, test_binding());

    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/tasks")
                .header("content-type", "application/json")
                .body(Body::from(
                    json!({"task_description": "say hi"}).to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    unsafe {
        std::env::remove_var(crate::task::mock_llm::MOCK_LLM_ENV);
    }

    assert_eq!(resp.status(), StatusCode::ACCEPTED);
    let v = body_json(resp).await;
    assert_eq!(v["status"], "running");
}

/// `GET /fs` (the #2983 Slice 5 REST route group, merged in by
/// `build_axum_router` via `rest::fs::routes`) must be reachable on the
/// SAME router `POST /rpc` is — proving the merge actually wires
/// `rest::fs::routes` rather than silently dropping it. Per-route
/// error/status-code behaviour is already covered by `rest::fs::tests`;
/// this test only pins the merge itself.
#[tokio::test]
async fn http_rest_get_fs_route_is_merged_in() {
    // `router_and_sessions()` does not register `fs.list_dir` (it is not
    // needed by any other test in this file) — build a router that also
    // has it wired.
    let sessions = Arc::new(SessionRegistry::new());
    let mut router = Router::new();
    crate::serve::methods::register(&mut router, crate::binding::ProjectBinding::None);
    crate::session::protocol::register(
        &mut router,
        sessions.clone(),
        test_workstreams_store().await,
    );
    crate::fs_browse::protocol::register(&mut router);
    let app = build_axum_router(
        Arc::new(router),
        sessions,
        test_workstreams_store().await,
        test_binding(),
    );
    let tmp = tempfile::tempdir().expect("tempdir");

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri(format!("/fs?path={}", tmp.path().display()))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert!(v["entries"].is_array());
}

/// `GET /projects` (issue #3365's REST route group, merged in by
/// `build_axum_router` via `rest::projects::routes`) must be reachable on
/// the SAME router `POST /rpc` is — proving the merge actually wires
/// `rest::projects::routes` rather than silently dropping it. Per-route
/// behaviour (the `entries` array shape) is already covered by
/// `rest::projects::tests`; this test only pins the merge itself.
#[tokio::test]
async fn http_rest_get_projects_route_is_merged_in() {
    // `router_and_sessions()` does not register `fs.list_projects` (it is
    // not needed by any other test in this file) — build a router that
    // also has it wired, mirroring `http_rest_get_fs_route_is_merged_in`.
    let sessions = Arc::new(SessionRegistry::new());
    let mut router = Router::new();
    crate::serve::methods::register(&mut router, crate::binding::ProjectBinding::None);
    crate::session::protocol::register(
        &mut router,
        sessions.clone(),
        test_workstreams_store().await,
    );
    crate::fs_browse::protocol::register(&mut router);
    let app = build_axum_router(
        Arc::new(router),
        sessions,
        test_workstreams_store().await,
        test_binding(),
    );

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/projects")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert!(v["entries"].is_array());
}

/// `GET /agents`/`GET /skills` (issue #3449's Foundry GUI catalog-management
/// REST groups, merged in by `build_axum_router` via
/// `rest::agent_catalog::routes`/`rest::skill_catalog::routes`) must be
/// reachable on the SAME router `POST /rpc` is — proving the merge actually
/// wires both groups rather than silently dropping them. Per-route behaviour
/// is already covered by `rest::agent_catalog::tests`/
/// `rest::skill_catalog::tests`; this test only pins the merge itself.
#[tokio::test]
async fn http_rest_get_agent_and_skill_catalog_routes_are_merged_in() {
    let sessions = Arc::new(SessionRegistry::new());
    let project = tempfile::tempdir().expect("project tempdir");
    let mut router = Router::new();
    crate::serve::methods::register(&mut router, crate::binding::ProjectBinding::None);
    crate::session::protocol::register(
        &mut router,
        sessions.clone(),
        test_workstreams_store().await,
    );
    crate::agents::protocol::register(
        &mut router,
        crate::agents::protocol::AgentsCatalogState::new(project.path().to_path_buf(), true),
    );
    crate::skills::protocol::register(
        &mut router,
        crate::skills::protocol::SkillsCatalogState::new(Some(project.path())),
    );
    let app = build_axum_router(
        Arc::new(router),
        sessions,
        test_workstreams_store().await,
        test_binding(),
    );

    let agents_resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/agents")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(agents_resp.status(), StatusCode::OK);
    assert!(body_json(agents_resp).await["agents"].is_array());

    let skills_resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/skills")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(skills_resp.status(), StatusCode::OK);
    assert!(body_json(skills_resp).await["skills"].is_array());
}

/// `GET /sessions/{id}/agents` (the #2983 Slice 6 REST route group,
/// merged in by `build_axum_router` via `rest::agents::routes`) must be
/// reachable on the SAME router `GET /sessions/{id}/events` is —
/// proving the merge actually wires `rest::agents::routes` rather than
/// silently dropping it. Per-route error/status-code behaviour is
/// already covered by `rest::agents::tests`; this test only pins the
/// merge itself.
#[tokio::test]
async fn http_rest_get_session_agents_route_is_merged_in() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let session = sessions.create("t".to_string(), None, crate::binding::ProjectBinding::None);
    let app = build_axum_router(router, sessions, workstreams, test_binding());

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri(format!("/sessions/{}/agents", session.id))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert!(v["agents"].is_array());
}

/// `GET /sessions/{id}/budget` (issue #3015, part of the `rest::sessions`
/// route group `build_axum_router` already merges via
/// `rest::sessions::routes`) must be reachable on the SAME router
/// `GET /sessions/{id}/events` is — proving the merge actually wires the
/// route rather than silently dropping it. Per-route error/status-code
/// behaviour is already covered by `rest::sessions::tests`; this test
/// only pins the merge itself.
#[tokio::test]
async fn http_rest_get_session_budget_route_is_merged_in() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let session = sessions.create("t".to_string(), None, crate::binding::ProjectBinding::None);
    let app = build_axum_router(router, sessions, workstreams, test_binding());

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri(format!("/sessions/{}/budget", session.id))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert_eq!(v["status"], "never_recorded");
}

/// `GET /sessions/{id}/search-audit` (issue #3072, `rest::search_audit`'s
/// route group `build_axum_router` merges above) must be reachable on
/// the SAME router `GET /sessions/{id}/events` is — proving the merge
/// actually wires `rest::search_audit::routes` rather than silently
/// dropping it. Per-route error/status-code behaviour is already covered
/// by `rest::search_audit::tests`; this test only pins the merge itself.
#[tokio::test]
async fn http_rest_get_session_search_audit_route_is_merged_in() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let session = sessions.create("t".to_string(), None, crate::binding::ProjectBinding::None);
    let app = build_axum_router(router, sessions, workstreams, test_binding());

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri(format!("/sessions/{}/search-audit", session.id))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert_eq!(v["search_audit"].as_array().unwrap().len(), 0);
}

/// `GET /workstreams` (issue #3295, `rest::workstreams`'s route group
/// `build_axum_router` merges above) must be reachable on the SAME
/// router `GET /sessions/{id}/events` is — proving the merge actually
/// wires `rest::workstreams::routes` rather than silently dropping it.
/// Also hits `GET /workstreams/{id}/events` (issue #3297,
/// `crate::workstreams::sse::routes`, merged in the SAME call) on the
/// SAME router — proving that merge too, without a second whole test
/// function. Per-route behaviour for both groups is already covered by
/// `rest::workstreams::tests`/`crate::workstreams::sse::sse_tests`; this
/// test only pins the merges themselves.
#[tokio::test]
async fn http_rest_get_workstreams_route_is_merged_in() {
    let sessions = Arc::new(SessionRegistry::new());
    let dir = tempfile::tempdir().expect("tempdir");
    let store = crate::workstreams::WorkstreamStore::load(dir.path().join("workstreams-test.json"))
        .await
        .expect("load");
    let workstreams = Arc::new(tokio::sync::Mutex::new(store));
    let mut router = Router::new();
    crate::serve::methods::register(&mut router, crate::binding::ProjectBinding::None);
    crate::session::protocol::register(&mut router, sessions.clone(), workstreams.clone());
    crate::workstreams::protocol::register(&mut router, workstreams.clone());
    let id = workstreams.lock().await.create("t").await.expect("create");
    let app = build_axum_router(Arc::new(router), sessions, workstreams, test_binding());

    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/workstreams")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    assert!(v["workstreams"].is_array());

    let events_resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri(format!("/workstreams/{id}/events"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(events_resp.status(), StatusCode::OK);
}

/// `GET /sessions/{id}/events` on an unknown session must 404 with a
/// JSON-RPC `session_not_found` envelope.
#[tokio::test]
async fn http_session_events_sse_unknown_session_returns_404() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let app = build_axum_router(router, sessions, workstreams, test_binding());

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/sessions/does-not-exist/events")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    let v = body_json(resp).await;
    assert_eq!(v["error"]["code"], -32007);
}

/// `GET /sessions/{id}/events` on a real session must stream the
/// ring-buffer replay as SSE `data:` frames.
///
/// Why: the response body never terminates (the live half subscribes to
/// the bus forever), so this reads a bounded prefix of the body as a
/// `Stream` (rather than `axum::body::to_bytes`, which would hang or
/// error waiting for a completion that never comes) until the expected
/// content shows up or a timeout fires.
#[tokio::test]
async fn http_session_events_sse_streams_replay() {
    let (router, sessions, workstreams) = router_and_sessions().await;
    let session = sessions.create("t".to_string(), None, crate::binding::ProjectBinding::None);
    let app = build_axum_router(router, sessions, workstreams, test_binding());

    let resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri(format!("/sessions/{}/events", session.id))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);

    let mut stream = resp.into_body().into_data_stream();
    let mut collected = Vec::new();
    let read_replay = tokio::time::timeout(std::time::Duration::from_secs(5), async {
        while !String::from_utf8_lossy(&collected).contains("session_status_changed") {
            match stream.next().await {
                Some(Ok(chunk)) => collected.extend_from_slice(&chunk),
                _ => break,
            }
        }
    })
    .await;

    assert!(
        read_replay.is_ok(),
        "timed out waiting for the SSE replay burst"
    );
    let text = String::from_utf8(collected).unwrap();
    assert!(text.contains("session_started"), "body so far: {text}");
    assert!(
        text.contains("session_status_changed"),
        "body so far: {text}"
    );
    // #2055: the SSE frames must carry the full envelope, not a bare
    // event — `seq` and the top-level `kind` field must both appear.
    assert!(text.contains("\"seq\":1"), "body so far: {text}");
    assert!(
        text.contains("\"kind\":\"session_started\""),
        "body so far: {text}"
    );
}