basemind 0.10.1

Full AI context layer over MCP — tree-sitter code-map, document RAG (PDF/Office/HTML/email + OCR + reranker), shared agent memory, on-demand web crawl, git history + blame + per-symbol diff. 300+ languages, 10+ coding-agent harnesses, content-addressed Fjall + LanceDB.
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
//! Unit tests for the comms [`Broker`](super::Broker). Split out of `daemon.rs` (via a
//! `#[cfg(test)] #[path = "daemon_tests.rs"] mod tests;` declaration) to keep `daemon.rs` under
//! the 1000-line `rust-max-lines` cap. `super` here resolves to the `daemon` module.

use super::*;

fn temp_broker() -> (tempfile::TempDir, Arc<Broker>) {
    let dir = tempfile::tempdir().expect("tempdir");
    let store = Arc::new(CommsStore::open(dir.path()).expect("store"));
    (dir, Arc::new(Broker::new(store)))
}

fn agent(s: &str) -> AgentId {
    AgentId::parse(s).expect("agent")
}

#[tokio::test]
async fn hello_rejects_proto_skew() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(8);
    let mut session = Session::default();
    let resp = broker
        .handle(
            CommsRequest::Hello {
                agent: agent("a"),
                proto_ver: PROTO_VER + 1,
                remote: None,
                cwd: None,
                session_id: None,
                parent_agent: None,
            },
            &mut session,
            &tx,
        )
        .await;
    assert!(matches!(resp, CommsResponse::Error { code, .. } if code == "proto_skew"));
}

#[tokio::test]
async fn post_requires_hello() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(8);
    let mut session = Session::default();
    let resp = broker
        .handle(
            CommsRequest::Post {
                room: RoomId::parse("r").expect("r"),
                subject: "s".to_string(),
                tags: vec![],
                reply_to: None,
                scope: vec![],
                body: b"b".to_vec(),
            },
            &mut session,
            &tx,
        )
        .await;
    assert!(matches!(resp, CommsResponse::Error { code, .. } if code == "no_hello"));
}

#[tokio::test]
async fn subscribe_then_post_fans_out_notification() {
    let (_d, broker) = temp_broker();
    let (tx, mut rx) = mpsc::channel(8);
    let mut session = Session::default();
    // Hello with no cwd → Global default room.
    broker
        .handle(
            CommsRequest::Hello {
                agent: agent("a"),
                proto_ver: PROTO_VER,
                remote: None,
                cwd: None,
                session_id: None,
                parent_agent: None,
            },
            &mut session,
            &tx,
        )
        .await;
    let room = RoomId::parse("r").expect("r");
    broker
        .handle(
            CommsRequest::CreateRoom {
                room: room.clone(),
                scope: RoomScope::Global,
                title: None,
            },
            &mut session,
            &tx,
        )
        .await;
    let sub_resp = broker
        .handle(
            CommsRequest::Subscribe { room: room.clone() },
            &mut session,
            &tx,
        )
        .await;
    assert!(matches!(sub_resp, CommsResponse::Subscribed { .. }));
    assert_eq!(broker.subscriber_count(), 1);

    let posted = broker
        .handle(
            CommsRequest::Post {
                room: room.clone(),
                subject: "hi".to_string(),
                tags: vec![],
                reply_to: None,
                scope: vec![],
                body: b"hello".to_vec(),
            },
            &mut session,
            &tx,
        )
        .await;
    assert!(matches!(posted, CommsResponse::Posted { .. }));

    let note = rx.recv().await.expect("notification");
    match note {
        CommsOut::Notification(CommsNotification::Message(meta)) => {
            assert_eq!(meta.subject, "hi");
            assert_eq!(meta.room, room);
        }
        other => panic!("expected a Message notification, got {other:?}"),
    }
}

#[test]
fn sanitize_id_maps_to_alphabet() {
    assert_eq!(sanitize_id("github.com/foo/bar"), "github.com-foo-bar");
    assert!(RoomId::parse(sanitize_id("a b!c")).is_ok());
}

/// Drive Hello → CreateRoom → Join for an agent, returning a session bound to it.
async fn hello_join(
    broker: &Broker,
    tx: &mpsc::Sender<CommsOut>,
    who: &str,
    room: &RoomId,
) -> Session {
    let mut session = Session::default();
    broker
        .handle(
            CommsRequest::Hello {
                agent: agent(who),
                proto_ver: PROTO_VER,
                remote: None,
                cwd: None,
                session_id: None,
                parent_agent: None,
            },
            &mut session,
            tx,
        )
        .await;
    broker
        .handle(
            CommsRequest::CreateRoom {
                room: room.clone(),
                scope: RoomScope::Global,
                title: None,
            },
            &mut session,
            tx,
        )
        .await;
    broker
        .handle(CommsRequest::Join { room: room.clone() }, &mut session, tx)
        .await;
    session
}

async fn post(
    broker: &Broker,
    session: &mut Session,
    tx: &mpsc::Sender<CommsOut>,
    room: &RoomId,
    subject: &str,
) -> String {
    match broker
        .handle(
            CommsRequest::Post {
                room: room.clone(),
                subject: subject.to_string(),
                tags: vec![],
                reply_to: None,
                scope: vec![],
                body: subject.as_bytes().to_vec(),
            },
            session,
            tx,
        )
        .await
    {
        CommsResponse::Posted { message_id } => message_id,
        other => panic!("expected Posted, got {other:?}"),
    }
}

async fn inbox(
    broker: &Broker,
    session: &mut Session,
    tx: &mpsc::Sender<CommsOut>,
) -> Vec<SeqMeta> {
    match broker
        .handle(
            CommsRequest::Inbox {
                remote: None,
                cwd: None,
                cursor: None,
                limit: None,
                mark_read: false,
                since_micros: None,
            },
            session,
            tx,
        )
        .await
    {
        CommsResponse::Inbox { messages, .. } => messages,
        other => panic!("expected Inbox, got {other:?}"),
    }
}

/// `AckInbox { message_ids }` advances ONLY the acking agent's cursor: the acked messages
/// vanish from that agent's next inbox read, the shared `History` log still returns them, and
/// a second agent's inbox is untouched.
#[tokio::test]
async fn ack_by_ids_advances_only_the_acking_agents_cursor() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(64);
    let room = RoomId::parse("r").expect("r");

    // Alice posts two messages; Bob and Carol are inbox readers.
    let mut alice = hello_join(&broker, &tx, "alice", &room).await;
    let mut bob = hello_join(&broker, &tx, "bob", &room).await;
    let mut carol = hello_join(&broker, &tx, "carol", &room).await;
    let m1 = post(&broker, &mut alice, &tx, &room, "first").await;
    let _m2 = post(&broker, &mut alice, &tx, &room, "second").await;

    // Bob sees both, acks the first by id.
    assert_eq!(inbox(&broker, &mut bob, &tx).await.len(), 2);
    let resp = broker
        .handle(
            CommsRequest::AckInbox {
                message_ids: vec![m1.clone()],
                room: None,
                to_seq: None,
            },
            &mut bob,
            &tx,
        )
        .await;
    match resp {
        CommsResponse::Acked {
            acked,
            cursors_advanced,
        } => {
            assert_eq!(acked, 1);
            assert_eq!(cursors_advanced, vec![("r".to_string(), 1)]);
        }
        other => panic!("expected Acked, got {other:?}"),
    }

    // Bob's inbox no longer shows the acked message; only "second" remains.
    let bob_after = inbox(&broker, &mut bob, &tx).await;
    assert_eq!(bob_after.len(), 1);
    assert_eq!(bob_after[0].meta.subject, "second");

    // The shared log is intact — History still returns both messages.
    match broker
        .handle(
            CommsRequest::History {
                room: room.clone(),
                cursor: None,
                limit: None,
                since_micros: None,
            },
            &mut bob,
            &tx,
        )
        .await
    {
        CommsResponse::History { messages, .. } => assert_eq!(messages.len(), 2),
        other => panic!("expected History, got {other:?}"),
    }

    // Carol's inbox is unaffected by Bob's ack — per-agent isolation.
    assert_eq!(inbox(&broker, &mut carol, &tx).await.len(), 2);
}

/// The bulk `room` + `to_seq` mode advances a room's cursor straight to `to_seq`, clearing
/// the whole room from the agent's inbox without enumerating ids.
#[tokio::test]
async fn ack_to_seq_bulk_clears_room() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(64);
    let room = RoomId::parse("r").expect("r");
    let mut alice = hello_join(&broker, &tx, "alice", &room).await;
    let mut bob = hello_join(&broker, &tx, "bob", &room).await;
    for i in 0..3 {
        post(&broker, &mut alice, &tx, &room, &format!("m{i}")).await;
    }
    assert_eq!(inbox(&broker, &mut bob, &tx).await.len(), 3);

    let resp = broker
        .handle(
            CommsRequest::AckInbox {
                message_ids: vec![],
                room: Some(room.clone()),
                to_seq: Some(3),
            },
            &mut bob,
            &tx,
        )
        .await;
    assert!(matches!(resp, CommsResponse::Acked { acked: 0, .. }));
    assert!(inbox(&broker, &mut bob, &tx).await.is_empty());
}

/// A `to_seq` at or below the current cursor (e.g. `to_seq = 0`, or re-acking an already-acked
/// position) must report an empty `cursors_advanced` — never a phantom advance.
#[tokio::test]
async fn ack_does_not_report_phantom_advance() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(64);
    let room = RoomId::parse("r").expect("r");
    let mut alice = hello_join(&broker, &tx, "alice", &room).await;
    let mut bob = hello_join(&broker, &tx, "bob", &room).await;
    post(&broker, &mut alice, &tx, &room, "m0").await;

    // to_seq = 0 cannot advance past the default cursor of 0.
    let resp = broker
        .handle(
            CommsRequest::AckInbox {
                message_ids: vec![],
                room: Some(room.clone()),
                to_seq: Some(0),
            },
            &mut bob,
            &tx,
        )
        .await;
    match resp {
        CommsResponse::Acked {
            acked,
            cursors_advanced,
        } => {
            assert_eq!(acked, 0);
            assert!(
                cursors_advanced.is_empty(),
                "to_seq=0 must not report a phantom advance"
            );
        }
        other => panic!("expected Acked, got {other:?}"),
    }

    // Advance to seq 1, then re-ack the same seq: no further advance is reported.
    let _ = broker
        .handle(
            CommsRequest::AckInbox {
                message_ids: vec![],
                room: Some(room.clone()),
                to_seq: Some(1),
            },
            &mut bob,
            &tx,
        )
        .await;
    let resp2 = broker
        .handle(
            CommsRequest::AckInbox {
                message_ids: vec![],
                room: Some(room.clone()),
                to_seq: Some(1),
            },
            &mut bob,
            &tx,
        )
        .await;
    match resp2 {
        CommsResponse::Acked {
            cursors_advanced, ..
        } => assert!(
            cursors_advanced.is_empty(),
            "re-acking an already-acked seq must not report an advance"
        ),
        other => panic!("expected Acked, got {other:?}"),
    }
}

/// An ack with neither mode supplied is rejected with a stable `empty_ack` code.
#[tokio::test]
async fn ack_with_no_input_is_rejected() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(8);
    let room = RoomId::parse("r").expect("r");
    let mut bob = hello_join(&broker, &tx, "bob", &room).await;
    let resp = broker
        .handle(
            CommsRequest::AckInbox {
                message_ids: vec![],
                room: None,
                to_seq: None,
            },
            &mut bob,
            &tx,
        )
        .await;
    assert!(matches!(resp, CommsResponse::Error { code, .. } if code == "empty_ack"));
}

#[tokio::test]
async fn idle_reaper_tracks_links_and_activity() {
    use std::time::Duration;
    let (_d, broker) = temp_broker();

    // A fresh, unused broker is immediately idle past a zero window — the reaper would
    // self-terminate a daemon that was spawned but never used.
    assert!(
        broker.is_idle_for(Duration::ZERO).await,
        "an unused broker is idle past a zero window"
    );

    // A connected link is never idle, even past the window.
    broker.link_connected();
    assert!(
        !broker.is_idle_for(Duration::ZERO).await,
        "a connected link keeps the daemon alive"
    );

    // After the last link closes the broker is idle again.
    broker.link_disconnected();
    assert!(
        broker.is_idle_for(Duration::ZERO).await,
        "the broker is idle once every link has closed"
    );

    // Recent activity (the disconnect just touched it) keeps it out of a real reap window.
    assert!(
        !broker.is_idle_for(Duration::from_secs(3600)).await,
        "recent activity keeps the broker out of the reap window"
    );

    // A draining broker is never reaped — the clean-shutdown path is already underway.
    broker.begin_drain().await;
    assert!(
        !broker.is_idle_for(Duration::ZERO).await,
        "a draining broker is never reaped"
    );
}

/// Drive a `Hello` carrying a `session_id` and return the bound session. No cwd → the base
/// chain is path-empty, so only the explicit session room can match.
async fn hello_session(
    broker: &Broker,
    tx: &mpsc::Sender<CommsOut>,
    who: &str,
    session_id: Option<&str>,
) -> Session {
    let mut session = Session::default();
    broker
        .handle(
            CommsRequest::Hello {
                agent: agent(who),
                proto_ver: PROTO_VER,
                remote: None,
                cwd: None,
                session_id: session_id.map(|s| s.to_string()),
                parent_agent: None,
            },
            &mut session,
            tx,
        )
        .await;
    session
}

/// An agent whose `Hello` carries the room's `session_id` is auto-joined to a
/// `RoomScope::Session` room; an agent with a different / absent `session_id` is not. Verified
/// through the broker: a post by the matching parent lands in the matching child's inbox, and
/// never in the non-matching agent's inbox.
#[tokio::test]
async fn session_scoped_room_auto_joins_only_matching_session() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(64);
    let room = RoomId::parse("session-abc").expect("room");

    // A session-scoped room exists before anyone connects.
    broker
        .handle(
            CommsRequest::CreateRoom {
                room: room.clone(),
                scope: RoomScope::Session("abc".to_string()),
                title: None,
            },
            &mut Session::default(),
            &tx,
        )
        .await;

    // Parent + child share session "abc"; outsider has a different session.
    let mut parent = hello_session(&broker, &tx, "parent", Some("abc")).await;
    let mut child = hello_session(&broker, &tx, "child", Some("abc")).await;
    let mut outsider = hello_session(&broker, &tx, "outsider", Some("zzz")).await;

    // The matching session's agents were auto-joined to the room; the outsider was not.
    let subs = broker.store.subscribers(&room).expect("subs");
    assert!(
        subs.contains(&agent("parent")),
        "parent auto-joins session room"
    );
    assert!(
        subs.contains(&agent("child")),
        "child auto-joins session room"
    );
    assert!(
        !subs.contains(&agent("outsider")),
        "a different session id must not auto-join"
    );

    // Parent posts → child sees it in the inbox, outsider does not.
    let _ = post(&broker, &mut parent, &tx, &room, "hello-child").await;
    let child_inbox = inbox(&broker, &mut child, &tx).await;
    assert_eq!(child_inbox.len(), 1);
    assert_eq!(child_inbox[0].meta.subject, "hello-child");
    assert!(
        inbox(&broker, &mut outsider, &tx).await.is_empty(),
        "outsider's inbox stays empty — never joined the session room"
    );
}

/// Drive a `Hello` carrying both a `session_id` and a `parent_agent`, returning the bound
/// session. Mirrors [`hello_session`] but threads the lineage parent the child presents.
async fn hello_session_with_parent(
    broker: &Broker,
    tx: &mpsc::Sender<CommsOut>,
    who: &str,
    session_id: &str,
    parent_agent: &str,
) -> Session {
    let mut session = Session::default();
    broker
        .handle(
            CommsRequest::Hello {
                agent: agent(who),
                proto_ver: PROTO_VER,
                remote: None,
                cwd: None,
                session_id: Some(session_id.to_string()),
                parent_agent: Some(parent_agent.to_string()),
            },
            &mut session,
            tx,
        )
        .await;
    session
}

/// At the child's `Hello` the broker writes a [`SessionLineage`] row linking the child to its
/// parent and the session-scoped room it was just auto-joined to. The `room_id` is the actual
/// created room (not assumed equal to the `session_id`), and `list_sessions` surfaces the row.
#[tokio::test]
async fn child_hello_records_session_lineage_row() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(8);
    // The room id is deliberately DISTINCT from the session id to prove the write reads the real
    // room id rather than assuming `room_id == session_id`.
    let room = RoomId::parse("session-room-s1").expect("room");

    // The parent creates the session room and joins it (mirrors `shell_spawn`). The parent itself
    // presents no `session_id` for this session — it is the spawner, not a session child — so its
    // own Hello records no lineage for "s1".
    let mut parent = Session::default();
    broker
        .handle(
            CommsRequest::CreateRoom {
                room: room.clone(),
                scope: RoomScope::Session("s1".to_string()),
                title: None,
            },
            &mut parent,
            &tx,
        )
        .await;
    let _ = hello_session(&broker, &tx, "parent", None).await;

    // No lineage exists before the child says Hello.
    assert_eq!(broker.store.get_session("s1").expect("get"), None);

    // The child says Hello carrying session "s1" + parent "parent".
    let _child = hello_session_with_parent(&broker, &tx, "child", "s1", "parent").await;

    let lineage = broker
        .store
        .get_session("s1")
        .expect("get")
        .expect("lineage row written at child Hello");
    assert_eq!(lineage.session_id, "s1");
    assert_eq!(lineage.child_agent, agent("child"));
    assert_eq!(lineage.parent_agent, Some(agent("parent")));
    assert_eq!(lineage.room_id, room, "room id is the real created room");

    // `list_sessions` (and the `ListSessions` request) surfaces the row.
    let listed = broker.on_list_sessions().expect("list");
    match listed {
        CommsResponse::Sessions { sessions } => {
            assert_eq!(sessions, vec![lineage.clone()]);
        }
        other => panic!("expected Sessions, got {other:?}"),
    }
    let via_request = broker
        .handle(CommsRequest::ListSessions {}, &mut Session::default(), &tx)
        .await;
    match via_request {
        CommsResponse::Sessions { sessions } => assert_eq!(sessions, vec![lineage]),
        other => panic!("expected Sessions, got {other:?}"),
    }
}

/// A re-`Hello` for the same session preserves the original `created_at` rather than rewriting
/// the first-seen time. The latest `child_agent` / `parent_agent` are still upserted.
#[tokio::test]
async fn re_hello_preserves_session_created_at() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(8);
    let room = RoomId::parse("session-room-s2").expect("room");
    broker
        .handle(
            CommsRequest::CreateRoom {
                room: room.clone(),
                scope: RoomScope::Session("s2".to_string()),
                title: None,
            },
            &mut Session::default(),
            &tx,
        )
        .await;

    let _ = hello_session_with_parent(&broker, &tx, "child", "s2", "parent").await;
    let first = broker
        .store
        .get_session("s2")
        .expect("get")
        .expect("first row");

    // A reconnect (a second Hello) for the same session must not move `created_at`.
    let _ = hello_session_with_parent(&broker, &tx, "child", "s2", "parent").await;
    let second = broker
        .store
        .get_session("s2")
        .expect("get")
        .expect("second row");
    assert_eq!(
        second.created_at, first.created_at,
        "created_at is preserved across reconnects"
    );
}

/// A top-level agent (no `session_id` on its Hello) writes no lineage row.
#[tokio::test]
async fn top_level_hello_writes_no_session_lineage() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(8);
    let _ = hello_session(&broker, &tx, "lonely", None).await;
    assert!(
        broker.store.list_sessions().expect("list").is_empty(),
        "a top-level agent records no lineage"
    );
}

/// The `DeleteSession` request removes a recorded lineage row; deleting an absent id is a no-op.
#[tokio::test]
async fn delete_session_removes_lineage_row() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(8);
    let room = RoomId::parse("session-room-s3").expect("room");
    broker
        .handle(
            CommsRequest::CreateRoom {
                room: room.clone(),
                scope: RoomScope::Session("s3".to_string()),
                title: None,
            },
            &mut Session::default(),
            &tx,
        )
        .await;
    let _ = hello_session_with_parent(&broker, &tx, "child", "s3", "parent").await;
    assert!(broker.store.get_session("s3").expect("get").is_some());

    // Deleting an absent id is a no-op (idempotent).
    match broker
        .handle(
            CommsRequest::DeleteSession {
                session_id: "does-not-exist".to_string(),
            },
            &mut Session::default(),
            &tx,
        )
        .await
    {
        CommsResponse::Ok => {}
        other => panic!("expected Ok, got {other:?}"),
    }
    assert!(broker.store.get_session("s3").expect("get").is_some());

    // Deleting the real id removes the row.
    match broker
        .handle(
            CommsRequest::DeleteSession {
                session_id: "s3".to_string(),
            },
            &mut Session::default(),
            &tx,
        )
        .await
    {
        CommsResponse::Ok => {}
        other => panic!("expected Ok, got {other:?}"),
    }
    assert_eq!(broker.store.get_session("s3").expect("get"), None);
}

/// An agent that presents NO `session_id` does not auto-join a session-scoped room even when
/// the room already exists.
#[tokio::test]
async fn absent_session_id_does_not_auto_join_session_room() {
    let (_d, broker) = temp_broker();
    let (tx, _rx) = mpsc::channel(8);
    let room = RoomId::parse("session-abc").expect("room");
    broker
        .handle(
            CommsRequest::CreateRoom {
                room: room.clone(),
                scope: RoomScope::Session("abc".to_string()),
                title: None,
            },
            &mut Session::default(),
            &tx,
        )
        .await;

    let _sessionless = hello_session(&broker, &tx, "lonely", None).await;
    let subs = broker.store.subscribers(&room).expect("subs");
    assert!(
        !subs.contains(&agent("lonely")),
        "an agent with no session id must not join a session-scoped room"
    );
}