trusty-code 0.8.2

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! Tests for [`crate::serve::uds`] — the daemon's native transport (#6637).
//!
//! Why: every one of these drives a REAL hardened socket under a tempdir. The
//! properties at stake are transport-level — that a bind failure stops the
//! daemon, that the fallback really does reach all 34 JSON-RPC methods, that
//! the two stream names are not swallowed by it, and that an open tail is not
//! cut by a quiet window — and none of them can be observed against an
//! in-process router.
//! What: a [`TestDaemon`] harness that binds a socket in a tempdir, serves the
//! real router built by `build_router_at` (so nothing touches the developer's
//! `~/.trusty-code`), and shuts down on drop.

use std::os::unix::fs::PermissionsExt as _;
use std::path::{Path, PathBuf};
use std::time::Duration;

use serde_json::{Value, json};
use tempfile::TempDir;
use tokio::sync::oneshot;
use tokio::time::timeout;
use trusty_common::uds::server::{RpcResponse, RpcStreamFrame};
use trusty_common::uds::{send_framed_request, send_framed_stream_request};

use crate::binding::ProjectBinding;
use crate::events::SessionEventEnvelope;

use super::run_daemon_on;

/// A generous per-call budget: these dial a socket in the same process.
const CALL_TIMEOUT: Duration = Duration::from_secs(10);

/// A daemon serving a real router on a tempdir socket.
struct TestDaemon {
    socket: PathBuf,
    stop: Option<oneshot::Sender<()>>,
    joined: Option<tokio::task::JoinHandle<anyhow::Result<()>>>,
    _sockets: TempDir,
    _data: TempDir,
    _project: TempDir,
}

impl TestDaemon {
    /// Bind and serve, returning once the socket answers a probe.
    async fn start() -> Self {
        let sockets = tempfile::tempdir().expect("socket tempdir");
        let data = tempfile::tempdir().expect("data tempdir");
        let project = tempfile::tempdir().expect("project tempdir");
        let socket = sockets.path().join("tcode.sock");
        let binding =
            ProjectBinding::resolve(Some(project.path().to_path_buf())).expect("tempdir must bind");

        let (stop, stopped) = oneshot::channel();
        let serve_socket = socket.clone();
        let serve_data = data.path().to_path_buf();
        let joined = tokio::spawn(async move {
            run_daemon_on(binding, &serve_socket, Some(&serve_data), async move {
                let _ = stopped.await;
            })
            .await
        });

        wait_until_serving(&socket).await;
        Self {
            socket,
            stop: Some(stop),
            joined: Some(joined),
            _sockets: sockets,
            _data: data,
            _project: project,
        }
    }

    /// One unary call, as a client would make it.
    async fn call(&self, method: &str, params: Value) -> RpcResponse {
        let request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": method,
            "params": params,
        });
        send_framed_request::<Value, RpcResponse>(&self.socket, &request, CALL_TIMEOUT)
            .await
            .unwrap_or_else(|e| panic!("{method} must answer over the socket: {e}"))
    }

    /// Stop the serve loop and wait for the socket to be unlinked.
    async fn shutdown(mut self) -> anyhow::Result<()> {
        let stop = self.stop.take().expect("stop sender is taken once");
        let _ = stop.send(());
        let joined = self.joined.take().expect("join handle is taken once");
        timeout(Duration::from_secs(10), joined)
            .await
            .expect("the serve loop must return within the budget")
            .expect("the serve task must not panic")
    }
}

impl Drop for TestDaemon {
    fn drop(&mut self) {
        if let Some(stop) = self.stop.take() {
            let _ = stop.send(());
        }
    }
}

/// Poll until the socket accepts a connection, so no test races the bind.
async fn wait_until_serving(socket: &Path) {
    for _ in 0..200 {
        if trusty_common::uds::socket_is_serving(socket, Duration::from_millis(200)).await {
            return;
        }
        tokio::time::sleep(Duration::from_millis(25)).await;
    }
    panic!("the daemon never began serving {}", socket.display());
}

/// Open a stream and read frames from it.
async fn open_stream(
    socket: &Path,
    method: &str,
    params: Value,
) -> Result<trusty_common::uds::FramedStream<SessionEventEnvelope>, trusty_common::uds::UdsRpcError>
{
    let request = json!({
        "jsonrpc": "2.0",
        "id": 7,
        "method": method,
        "params": params,
        "stream": true,
    });
    send_framed_stream_request(socket, &request, CALL_TIMEOUT).await
}

/// How long a daemon future given a shutdown signal that never fires may run
/// before the test calls it a regression.
///
/// Why a bound at all (#7312): this test's shutdown signal is
/// `std::future::pending()`, so a daemon that manages to bind serves forever.
/// On Linux `bind_singleton_hardened` did exactly that — it read the regular
/// file below as a dead socket, unlinked it, and bound — and the test consumed
/// its whole CI shard until the job's 45-minute cancel, with the shard's other
/// tests never reported. A regression must fail in seconds and say what
/// happened.
const BIND_FAILURE_BUDGET: Duration = Duration::from_secs(10);

/// Run a daemon on `socket` with a shutdown signal that never fires, bounded by
/// [`BIND_FAILURE_BUDGET`], and return the error it must stop with.
async fn expect_refused(socket: &Path) -> anyhow::Error {
    let data = tempfile::tempdir().expect("data tempdir");
    let project = tempfile::tempdir().expect("project tempdir");
    let binding =
        ProjectBinding::resolve(Some(project.path().to_path_buf())).expect("tempdir must bind");
    timeout(
        BIND_FAILURE_BUDGET,
        run_daemon_on(binding, socket, Some(data.path()), std::future::pending()),
    )
    .await
    .expect("the daemon began serving instead of failing the bind — see #7312")
    .expect_err("an unbindable socket must stop the daemon")
}

/// A socket the kernel cannot bind stops the daemon outright (#6637 Fail-Open
/// Check). With the TCP listener gone there is no other door, so a daemon that
/// logged the failure and kept running would be a process nobody can reach.
///
/// What: a path longer than `sun_path` holds (104 bytes on macOS, 108 on
/// Linux), so the bind itself fails rather than any pre-check on the path.
/// Test: this function IS the test.
#[tokio::test]
async fn run_uds_socket_bind_failure_is_fatal() {
    let dir = tempfile::tempdir().expect("socket tempdir");
    let socket = dir.path().join(format!("{}.sock", "s".repeat(120)));
    let rendered = format!("{:#}", expect_refused(&socket).await);
    assert!(
        rendered.contains("bind the daemon socket"),
        "the failure must name the bind, got: {rendered}"
    );
    assert!(!socket.exists(), "nothing may be left at the path");
}

/// Something at the socket path that is not a socket is refused and left
/// alone (#7328) — never unlinked and replaced, which would let a daemon
/// delete a file it did not create.
///
/// Why the bound: #7312 — on Linux `bind_singleton_hardened` once read this
/// regular file as a dead socket, unlinked it, and served forever.
/// Test: this function IS the test.
#[tokio::test]
async fn run_daemon_refuses_a_non_socket_at_the_path() {
    let dir = tempfile::tempdir().expect("socket tempdir");
    let socket = dir.path().join("tcode.sock");
    std::fs::write(&socket, b"not a socket").expect("occupy the socket path");
    let rendered = format!("{:#}", expect_refused(&socket).await);
    assert!(
        rendered.contains("bind the daemon socket"),
        "the failure must name the bind, got: {rendered}"
    );
    assert_eq!(
        std::fs::read(&socket).expect("the occupying file must survive"),
        b"not a socket",
        "the occupying file must survive the refused bind unchanged"
    );
}

/// A socket directory left wider than `0700` is REPAIRED to `0700` before the
/// socket is created, and the socket comes up `0600` (#6637 criterion 1).
///
/// Why repaired, not refused: every install before #5099 created this
/// directory with `create_dir_all`, so it is `0755` on existing machines; the
/// directory's owner is still checked, and a foreign-owned one is refused
/// (`trusty_common::uds::dir::classify_existing_dir`).
/// Test: this function IS the test.
#[tokio::test]
async fn run_daemon_repairs_a_wide_socket_dir() {
    let sockets = tempfile::tempdir().expect("socket tempdir");
    let dir = sockets.path().join("wide");
    std::fs::create_dir(&dir).expect("create the socket dir");
    std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).expect("widen");
    let socket = dir.join("tcode.sock");
    let data = tempfile::tempdir().expect("data tempdir");
    let project = tempfile::tempdir().expect("project tempdir");
    let binding =
        ProjectBinding::resolve(Some(project.path().to_path_buf())).expect("tempdir must bind");

    let (stop, stopped) = oneshot::channel::<()>();
    let serve_socket = socket.clone();
    let serve_data = data.path().to_path_buf();
    let joined = tokio::spawn(async move {
        run_daemon_on(binding, &serve_socket, Some(&serve_data), async move {
            let _ = stopped.await;
        })
        .await
    });
    wait_until_serving(&socket).await;

    let dir_mode = std::fs::metadata(&dir).expect("dir").permissions().mode() & 0o777;
    let socket_mode = std::fs::metadata(&socket)
        .expect("socket")
        .permissions()
        .mode()
        & 0o777;
    let _ = stop.send(());
    timeout(Duration::from_secs(10), joined)
        .await
        .expect("the serve loop must return within the budget")
        .expect("the serve task must not panic")
        .expect("a requested shutdown is a clean stop");
    assert_eq!(
        dir_mode, 0o700,
        "a wide socket dir must be narrowed to 0700"
    );
    assert_eq!(socket_mode, 0o600, "the socket must be owner-only");
}

/// #6637 Fail-Open Check: a serve task that panics ends the daemon with an
/// error. Without this the panic payload dies with the `JoinHandle` and the
/// process lingers with no listener.
/// Test: this function IS the test.
#[tokio::test]
async fn join_serve_task_errors_when_the_loop_panics() {
    let requested = std::sync::atomic::AtomicBool::new(false);
    let task = tokio::spawn(async { panic!("the accept loop died") });
    let err = super::join_serve_task(task, &requested)
        .await
        .expect_err("a panicked accept loop must not read as a clean stop");
    assert!(
        format!("{err:#}").contains("socket accept loop died"),
        "the error names the dead loop: {err:#}"
    );
}

/// #6637 Fail-Open Check: a loop that returns while no shutdown was asked for
/// is an error, so the process exits non-zero instead of 0.
/// Test: this function IS the test.
#[tokio::test]
async fn join_serve_task_errors_when_the_loop_ends_without_a_shutdown() {
    let requested = std::sync::atomic::AtomicBool::new(false);
    let task = tokio::spawn(async {});
    let err = super::join_serve_task(task, &requested)
        .await
        .expect_err("an unrequested return must not read as a clean stop");
    assert!(
        format!("{err:#}").contains("without a shutdown signal"),
        "the error says no shutdown was requested: {err:#}"
    );
}

/// The ordinary stop: the shutdown was requested, then the loop returned.
/// Proves the two error arms above are not an always-`Err` join.
/// Test: this function IS the test.
#[tokio::test]
async fn join_serve_task_is_ok_after_a_requested_shutdown() {
    let requested = std::sync::atomic::AtomicBool::new(true);
    let task = tokio::spawn(async {});
    super::join_serve_task(task, &requested)
        .await
        .expect("a requested shutdown is a clean stop");
}

/// A second daemon on a socket a live daemon holds is refused, and the first
/// keeps answering — the bind is the only duplicate guard (#6637).
/// Test: this function IS the test; `tests/daemon_no_tcp_6637.rs` covers the
/// real binary's exit status.
#[tokio::test]
async fn a_second_daemon_on_a_live_socket_is_refused() {
    let daemon = TestDaemon::start().await;
    let rendered = format!("{:#}", expect_refused(&daemon.socket).await);
    assert!(
        rendered.contains("bind the daemon socket"),
        "the failure must name the bind, got: {rendered}"
    );
    let response = daemon.call("ping", json!({})).await;
    assert!(response.error.is_none(), "the first daemon still answers");
    daemon.shutdown().await.expect("clean shutdown");
}

/// Every method the JSON-RPC router registers must be reachable over the
/// socket through the fallback — the whole point of mounting the table rather
/// than restating it.
#[tokio::test]
async fn fallback_dispatches_every_jsonrpc_router_method() {
    let daemon = TestDaemon::start().await;
    let project = tempfile::tempdir().expect("probe project tempdir");
    let (router, _sessions, _workstreams) = crate::serve::build_router_at(
        ProjectBinding::resolve(Some(project.path().to_path_buf())).expect("tempdir must bind"),
        project.path(),
    )
    .await
    .expect("build_router_at");
    let names = router.method_names();
    assert!(
        names.len() >= 34,
        "the router is expected to carry at least the 34 methods #6637 surveyed, got {}",
        names.len()
    );

    for method in names {
        let response = daemon.call(method, json!({})).await;
        // #6637: the one name the socket refuses on purpose. Its own test,
        // `session_attach_over_the_socket_is_refused_naming_session_events`,
        // pins the refusal; here it must at least be that refusal.
        if method == "session.attach" {
            let error = response.error.expect("session.attach must be refused");
            assert!(
                error.message.contains("session.events"),
                "session.attach's refusal must name session.events, got: {}",
                error.message
            );
            continue;
        }
        // Bare `{}` params are wrong for most of these — what matters is that
        // the name RESOLVED. `method_not_found` is the one answer that proves
        // the fallback did not reach the dispatcher.
        if let Some(error) = response.error {
            assert_ne!(
                error.code,
                trusty_common::uds::server::CODE_METHOD_NOT_FOUND,
                "{method} must be reachable through the fallback, got: {}",
                error.message
            );
        }
    }

    daemon.shutdown().await.expect("clean shutdown");
}

/// `session.attach` over the socket is refused with an error naming
/// `session.events` (#6637), for a real session and for an unknown id alike.
///
/// Why: through the fallback it answered success with the replay and then
/// pushed nothing, because a socket connection is one frame in, one frame out.
/// A caller took that as attached and waited forever. On the pre-fix daemon
/// the first call below returns a `result`, so this fails there.
/// What: creates a real session, then sends `session.attach` for it and for an
/// id no session carries, through the real socket of a real daemon.
/// Test: this function IS the test.
#[tokio::test]
async fn session_attach_over_the_socket_is_refused_naming_session_events() {
    let daemon = TestDaemon::start().await;
    let created = daemon
        .call("session.create", json!({"task": "attach-refusal"}))
        .await;
    let session_id = created
        .result
        .as_ref()
        .and_then(|r| r.get("id"))
        .and_then(Value::as_str)
        .expect("session.create returns an id")
        .to_string();

    for id in [session_id.as_str(), "no-such-session-6637"] {
        let response = daemon
            .call("session.attach", json!({"session_id": id}))
            .await;
        assert!(
            response.result.is_none(),
            "session.attach({id}) must not answer success over the socket, got: {:?}",
            response.result
        );
        let error = response.error.expect("session.attach must be refused");
        assert_eq!(
            error.code,
            trusty_common::uds::server::CODE_METHOD_NOT_FOUND,
            "got: {}",
            error.message
        );
        assert!(
            error.message.contains("session.events"),
            "the refusal must name the stream to open instead, got: {}",
            error.message
        );
        assert_eq!(
            error.data.as_ref().and_then(|d| d.get("use_stream")),
            Some(&json!("session.events")),
            "the refusal's data must name the stream, got: {:?}",
            error.data
        );
    }

    daemon.shutdown().await.expect("clean shutdown");
}

/// A name registered as a stream must be served as one, not swallowed by the
/// catch-all — the router checks its stream table before the fallback, and
/// this is the assertion that keeps that ordering true for these two names.
#[tokio::test]
async fn stream_names_win_over_the_fallback() {
    let daemon = TestDaemon::start().await;
    let created = daemon
        .call("session.create", json!({"task": "stream-ordering"}))
        .await;
    let session_id = created
        .result
        .as_ref()
        .and_then(|r| r.get("id"))
        .and_then(Value::as_str)
        .expect("session.create returns an id")
        .to_string();

    // Without the `stream` flag a streaming method answers CODE_STREAM_REQUIRED
    // rather than being dispatched by the fallback as an unknown name.
    let response = daemon
        .call("session.events", json!({"session_id": session_id}))
        .await;
    let error = response.error.expect("a unary call must be refused");
    assert_eq!(
        error.code,
        trusty_common::uds::server::CODE_STREAM_REQUIRED,
        "session.events must be a stream method, got: {}",
        error.message
    );

    let response = daemon
        .call("workstream.events", json!({"workstream_id": "not-a-uuid"}))
        .await;
    let error = response.error.expect("a unary call must be refused");
    assert_eq!(
        error.code,
        trusty_common::uds::server::CODE_STREAM_REQUIRED,
        "workstream.events must be a stream method, got: {}",
        error.message
    );

    daemon.shutdown().await.expect("clean shutdown");
}

/// `workstream.events` must refuse an id no workstream carries with
/// `not_found` — the code the retired HTTP route answered as a `404`.
#[tokio::test]
async fn workstream_events_refuses_an_unknown_id() {
    let daemon = TestDaemon::start().await;
    let request = json!({
        "jsonrpc": "2.0",
        "id": 3,
        "method": "workstream.events",
        "params": {"workstream_id": uuid::Uuid::new_v4().to_string()},
        "stream": true,
    });
    let frame =
        send_framed_request::<Value, RpcStreamFrame>(&daemon.socket, &request, CALL_TIMEOUT)
            .await
            .expect("a terminal error frame must be written");
    let error = frame.error.expect("the terminal frame must carry an error");
    assert_eq!(
        error.code,
        i64::from(crate::jsonrpc::RpcError::not_found("x").code),
        "got: {}",
        error.message
    );

    daemon.shutdown().await.expect("clean shutdown");
}

/// The socket is unreachable to another uid, and a peer that IS us passes the
/// check every accepted connection runs.
///
/// What this proves, precisely: `trusty_common::uds::server::handle_connection`
/// runs `ensure_peer_is_self` before a byte is read, and that check is tested
/// generically in trusty-common. What this test adds is that THIS daemon binds
/// through the hardened path — a `0600` socket inside a `0700` directory — so a
/// foreign uid cannot even traverse to the socket to be refused. A test cannot
/// dial as a different uid without running as root, which is why the
/// permission bits are the observable half.
#[tokio::test]
async fn uds_peer_uid_mismatch_is_refused() {
    let daemon = TestDaemon::start().await;

    let socket_mode = std::fs::metadata(&daemon.socket)
        .expect("the socket exists")
        .permissions()
        .mode()
        & 0o777;
    assert_eq!(socket_mode, 0o600, "the socket must be owner-only");

    let dir_mode = std::fs::metadata(daemon.socket.parent().expect("socket has a parent"))
        .expect("the socket directory exists")
        .permissions()
        .mode()
        & 0o777;
    assert_eq!(dir_mode, 0o700, "the socket directory must be owner-only");

    // A peer that IS us is accepted and answered.
    let response = daemon.call("ping", json!({})).await;
    assert!(response.error.is_none(), "our own uid must be accepted");

    daemon.shutdown().await.expect("clean shutdown");
}

/// An open tail must survive a quiet window. `RpcServeOptions::read_timeout`
/// bounds how long a peer may take to deliver its REQUEST frame and does not
/// apply between streamed response frames, and this daemon installs no idle
/// policy at all — so a session that says nothing for a while must not have its
/// stream cut.
#[tokio::test]
async fn session_events_stream_stays_open_under_idle_window() {
    let daemon = TestDaemon::start().await;
    let created = daemon
        .call("session.create", json!({"task": "idle-window"}))
        .await;
    let session_id = created
        .result
        .as_ref()
        .and_then(|r| r.get("id"))
        .and_then(Value::as_str)
        .expect("session.create returns an id")
        .to_string();

    let mut stream = open_stream(
        &daemon.socket,
        "session.events",
        json!({"session_id": session_id}),
    )
    .await
    .expect("the stream must open");

    // Quiet for longer than any per-frame cadence the daemon produces.
    tokio::time::sleep(Duration::from_millis(1500)).await;

    let sent = daemon
        .call(
            "session.send",
            json!({"session_id": session_id, "input": "after the quiet window"}),
        )
        .await;
    assert!(sent.error.is_none(), "session.send must succeed");

    let frame = timeout(Duration::from_secs(10), stream.next_frame())
        .await
        .expect("a frame must arrive after the quiet window")
        .expect("the stream must still be open")
        .expect("the frame must not be a terminal error");
    assert_eq!(frame.session_id, session_id);

    // #7217: dropping the tail closes the socket, and `write_stream` now reads
    // that departure off the socket rather than waiting on a quiet producer, so
    // the drain ends with the handler instead of spending its whole budget.
    drop(stream);
    daemon.shutdown().await.expect("clean shutdown");
}

/// The socket sits beside the daemon's other persistent state, which is where
/// every client resolves it from.
#[test]
fn socket_path_sits_in_the_daemon_data_dir() {
    let path = super::socket_path().expect("the data directory must resolve");
    assert_eq!(
        path.file_name().and_then(|n| n.to_str()),
        Some("trusty-code.sock")
    );
}

/// #4600: two projects get two sockets, one project always gets the same one,
/// projectless gets its own, and every one sits beside the shared socket so a
/// data-dir override isolates them all.
#[test]
fn project_socket_path_is_per_project_and_stable() {
    let shared = Path::new("/data/trusty-code/trusty-code.sock");
    let a = super::project_socket_path(shared, Some(Path::new("/work/a")));
    let b = super::project_socket_path(shared, Some(Path::new("/work/b")));
    let none = super::project_socket_path(shared, None);

    assert_ne!(a, b, "two projects must not share a socket");
    assert_eq!(
        a,
        super::project_socket_path(shared, Some(Path::new("/work/a"))),
        "one project must always derive the same socket"
    );
    assert_eq!(
        none,
        Path::new("/data/trusty-code/trusty-code-projectless.sock")
    );
    for path in [&a, &b, &none] {
        assert_ne!(path.as_path(), shared, "never the shared socket itself");
        assert_eq!(path.parent(), shared.parent(), "beside the shared socket");
    }
    let name = a.file_name().and_then(|n| n.to_str()).expect("utf-8 name");
    assert_eq!(
        name.len(),
        "trusty-code-".len() + 12 + ".sock".len(),
        "{name}"
    );
}

/// A name nothing serves must be refused with `method_not_found`, not
/// swallowed — the fallback answers for every unregistered name, so it is the
/// only thing that can report an unknown one.
#[tokio::test]
async fn fallback_reports_an_unknown_method() {
    let daemon = TestDaemon::start().await;
    let response = daemon.call("no.such.method", json!({})).await;
    let error = response.error.expect("an unknown method must be refused");
    assert_eq!(
        error.code,
        trusty_common::uds::server::CODE_METHOD_NOT_FOUND,
        "got: {}",
        error.message
    );

    daemon.shutdown().await.expect("clean shutdown");
}

/// `workstream.events` must refuse an id that is not a UUID with
/// `invalid_params` — the code the retired HTTP route answered as a `400`.
///
/// #6637: ported from `sse_tests::malformed_id_returns_400`; guards the
/// `Uuid::parse_str` branch of `workstreams::events_stream::open`.
/// Test: this function IS the test.
#[tokio::test]
async fn workstream_events_refuses_a_malformed_id() {
    let daemon = TestDaemon::start().await;
    let request = json!({
        "jsonrpc": "2.0",
        "id": 4,
        "method": "workstream.events",
        "params": {"workstream_id": "not-a-uuid"},
        "stream": true,
    });
    let frame =
        send_framed_request::<Value, RpcStreamFrame>(&daemon.socket, &request, CALL_TIMEOUT)
            .await
            .expect("a terminal error frame must be written");
    let error = frame.error.expect("the terminal frame must carry an error");
    assert_eq!(
        error.code,
        i64::from(crate::jsonrpc::RpcError::invalid_params("x").code),
        "got: {}",
        error.message
    );

    daemon.shutdown().await.expect("clean shutdown");
}

/// A `workstream.events` stream must deliver a live event published for a
/// session bound to the workstream, tagged with that session and its type.
///
/// #6637: ported from `sse_tests::route_streams_live_tagged_event_for_bound_
/// session`; the only end-to-end proof that the workstream fan-out reaches a
/// transport. It publishes in a loop because the daemon subscribes to the bus
/// when it handles the open, which this client cannot observe.
/// Test: this function IS the test.
#[tokio::test]
async fn workstream_events_streams_a_live_tagged_event_for_a_bound_session() {
    let daemon = TestDaemon::start().await;
    let created = daemon
        .call("workstream.create", json!({"name": "fan-out"}))
        .await;
    let workstream_id = created
        .result
        .as_ref()
        .and_then(|r| r.get("id"))
        .and_then(Value::as_str)
        .expect("workstream.create returns an id")
        .to_string();
    let session = daemon
        .call(
            "session.create",
            json!({"task": "fan-out", "workstream_id": workstream_id}),
        )
        .await;
    let session_id = session
        .result
        .as_ref()
        .and_then(|r| r.get("id"))
        .and_then(Value::as_str)
        .expect("session.create returns an id")
        .to_string();

    let request = json!({
        "jsonrpc": "2.0",
        "id": 5,
        "method": "workstream.events",
        "params": {"workstream_id": workstream_id},
        "stream": true,
    });
    let mut stream =
        send_framed_stream_request::<Value, Value>(&daemon.socket, &request, CALL_TIMEOUT)
            .await
            .expect("the stream must open");

    let publish = || {
        crate::events::publish(SessionEventEnvelope::new(
            session_id.clone(),
            1,
            chrono::Utc::now(),
            crate::events::Event::SessionStarted {
                session_id: session_id.clone(),
                project: "p".to_string(),
            },
        ));
    };
    let frame = timeout(Duration::from_secs(10), async {
        loop {
            publish();
            if let Ok(next) = timeout(Duration::from_millis(100), stream.next_frame()).await {
                let frame = next
                    .expect("the stream must stay open")
                    .expect("the frame must not be a terminal error");
                // `session.create` may have emitted its own event first.
                if frame["session_id"] == session_id.as_str() {
                    return frame;
                }
            }
        }
    })
    .await
    .expect("a tagged frame for the bound session must arrive");
    assert_eq!(frame["event_type"], "session_started", "got: {frame}");

    // An open stream holds the serve loop's connection task; close it first.
    drop(stream);
    daemon.shutdown().await.expect("clean shutdown");
}