synaps-engine 0.11.0

Runtime engine — streaming, tools, MCP, skills, extensions, sidecar
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
//! `ClientTransport` + `LocalTransport` (PLAN-phase2 §2.7).
//!
//! `LocalTransport` is the in-process client: same `Runtime`, same
//! `StreamEvent` values moved through a channel, never serialised. The
//! socket flavour (`SocketTransport`, B2) implements the same trait.

use std::sync::Arc;

use tokio::sync::broadcast;

use super::handle::SessionHandle;
use super::types::{
    AttachMode, AttachSnapshot, ClientId, ClientInput, ClientMeta, Envelope, Roster, SessionCommand,
    SessionEventWire, SessionId, SessionMeta,
};
use super::view::RuntimeView;

#[derive(Debug, thiserror::Error)]
pub enum TransportError {
    #[error("session closed")]
    Closed,
    #[error("backpressure")]
    Backpressure,
    #[error("io: {0}")]
    Io(#[from] std::io::Error),
    #[error("protocol: {0}")]
    Protocol(String),
    #[error("version: client {client} daemon {daemon}")]
    Version { client: u32, daemon: u32 },
    /// The operation does not exist on this transport (e.g. `reconnect` on
    /// `LocalTransport`).
    #[error("unsupported: {0}")]
    Unsupported(&'static str),
    /// The daemon announced a reload and closed; `reconnect` applies.
    #[error("daemon reloading (generation {generation})")]
    Reloading { generation: u64 },
    /// The daemon/actor refused the attach or command (`AttachRefused`,
    /// `DaemonFrame::Error` during attach).
    #[error("refused: {0}")]
    Refused(String),
    /// The daemon has no live session under that id
    /// (`Refused{UnknownSession}` to a control request such as `Close`).
    #[error("unknown session: {0}")]
    UnknownSession(String),
    /// The daemon predates this control request: it answered the frame
    /// with `Error{"malformed frame: unknown variant …"}`. A
    /// `synaps daemon reload` brings it up to this binary.
    #[error("daemon {daemon_version} predates `{request}`; run `synaps daemon reload`")]
    DaemonTooOld { daemon_version: String, request: &'static str },
}

#[async_trait::async_trait]
pub trait ClientTransport: Send {
    fn session_id(&self) -> &SessionId;
    fn meta(&self) -> &SessionMeta;
    /// Host-originated send (`from: None`; bypasses input ownership).
    async fn send(&self, cmd: SessionCommand) -> Result<(), TransportError>;
    /// Send stamped with this client's id (the TUI uses this; ownership is
    /// enforced by the actor, B1).
    async fn send_from_self(&self, cmd: SessionCommand) -> Result<(), TransportError>;
    /// Next envelope, or `None` when the session ended / socket closed.
    async fn next_event(&mut self) -> Option<Envelope>;
    /// Sync, cheap, never stale by more than one `SettingChanged`: what the
    /// TUI's getters read on day 2.
    fn view(&self) -> Arc<RuntimeView>;
    /// Client id assigned at Attach (needed for Detach/Resync).
    fn client_id(&self) -> ClientId;
    /// The mode this transport attached with.
    fn mode(&self) -> AttachMode;
    /// Current input owner — updated from `Attached` / `InputOwnerChanged`.
    fn input_owner(&self) -> Option<ClientId>;
    /// Attached clients, named — updated from `Attached` / `ClientJoined` /
    /// `ClientLeft`. Empty for a transport that doesn't track them.
    fn roster(&self) -> Roster {
        Roster::default()
    }
    /// Who may drive (from the attach snapshot). `Exclusive` for a
    /// transport that doesn't know.
    fn client_input(&self) -> ClientInput {
        ClientInput::wire_default()
    }
    /// Socket only (Local returns `Err(Unsupported)`): after `Reloading`/EOF,
    /// reconnect with backoff and re-attach with `mode` (C3/A4).
    async fn reconnect(&mut self, mode: AttachMode) -> Result<AttachSnapshot, TransportError>;
    /// F9: single reconnect attempt (no retry loop). Socket only.
    async fn reconnect_once(&mut self, mode: AttachMode) -> Result<AttachSnapshot, TransportError> {
        self.reconnect(mode).await
    }
    /// `true` if the daemon announced a reload before EOF (graceful). Always
    /// `false` on `LocalTransport`.
    fn is_reload_pending(&self) -> bool {
        false
    }
    /// The daemon pid from `Welcome`. Returns `0` on `LocalTransport`.
    fn daemon_pid(&self) -> u32 {
        0
    }
    /// The daemon's binary from the latest `Welcome` (it changes across
    /// `daemon reload`). `None` on `LocalTransport`: same binary.
    fn daemon_binary(&self) -> Option<BinaryId> {
        None
    }
    /// The daemon control socket this transport talks to. `None` on
    /// `LocalTransport` (no daemon). Lets a client open its own control
    /// connections (session list, pin, close) to the same daemon.
    fn socket_path(&self) -> Option<&std::path::Path> {
        None
    }
}

/// Which binary a process runs: its version and build
/// ([`binary_version`](super::wire::binary_version),
/// [`build_id`](super::wire::build_id)); the build is empty when unknown.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinaryId {
    pub version: String,
    pub build: String,
}

impl BinaryId {
    /// This process's own binary.
    pub fn this() -> Self {
        Self { version: super::wire::binary_version(), build: super::wire::build_id().to_string() }
    }

    /// The daemon's, from its handshake.
    pub fn of(welcome: &super::wire::Welcome) -> Self {
        Self { version: welcome.daemon_version.clone(), build: welcome.daemon_build.clone() }
    }

    fn label(&self, show_build: bool) -> String {
        if show_build && !self.build.is_empty() {
            format!("{} ({})", self.version, self.build)
        } else {
            self.version.clone()
        }
    }
}

/// When a client binary differs from the daemon it talks to: what to tell
/// the user, else `None`. An attached TUI keeps its own binary across
/// `daemon reload` (only the daemon re-execs), so after an upgrade it lacks
/// every client-side change — new slash commands, tray fixes, new events —
/// until it is restarted. Builds count, not just versions: a reload onto a
/// newer build of the same release is the common case. An unknown build
/// (empty: older daemon, packaged build) compares by version only.
///
/// The remedy depends on which side is behind. A client older than the
/// daemon restarts to match it. A client newer than the daemon (a new
/// install with the old daemon still running, the usual upgrade path) must
/// not be told to restart, which relaunches the same binary: the daemon is
/// moved onto this build with `synaps daemon reload`. Same version on
/// another build: which one is newer is unknown, so both remedies.
pub fn version_skew_notice(client: &BinaryId, daemon: &BinaryId, session: &str) -> Option<String> {
    let builds_differ = !client.build.is_empty() && !daemon.build.is_empty() && client.build != daemon.build;
    if client.version == daemon.version && !builds_differ {
        return None;
    }
    let show_build = client.version == daemon.version;
    let (c, d) = (client.label(show_build), daemon.label(show_build));
    Some(match compare_versions(&client.version, &daemon.version) {
        Some(std::cmp::Ordering::Less) => format!(
            "this client is synaps {c} but the daemon is {d} — this client is older, so some \
             commands and fixes are unavailable here. Restart the client to match the daemon: \
             synaps --attach {session}"
        ),
        Some(std::cmp::Ordering::Greater) => format!(
            "this client is synaps {c} but the daemon is {d} — the daemon is older than this \
             client, so some commands and fixes are unavailable. To move it onto this build, \
             run `synaps daemon reload`"
        ),
        _ => format!(
            "this client is synaps {c} but the daemon is {d} — different builds, so some commands \
             and fixes may be unavailable. If this client is the newer one, run `synaps daemon \
             reload`; otherwise restart the client to match the daemon: synaps --attach {session}"
        ),
    })
}

/// Compare dotted numeric versions (`0.10.2`, a `-pre`/`+build` suffix
/// ignored); `None` when either doesn't parse.
fn compare_versions(a: &str, b: &str) -> Option<std::cmp::Ordering> {
    fn parts(v: &str) -> Option<Vec<u64>> {
        let core = v.trim().trim_start_matches('v').split(['-', '+']).next()?;
        core.split('.').map(|p| p.parse().ok()).collect()
    }
    Some(parts(a)?.cmp(&parts(b)?))
}

/// Attach handshake budget.
pub const ATTACH_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
/// Attach budget when the listed lifecycle is `Parked` (unpark ≤ 20 s, B3).
pub const ATTACH_TIMEOUT_PARKED: std::time::Duration = std::time::Duration::from_secs(25);

pub struct LocalTransport {
    handle: SessionHandle,
    rx: broadcast::Receiver<Envelope>,
    client: ClientId,
    meta: SessionMeta,
    mode: AttachMode,
    input_owner: Option<ClientId>,
    roster: Roster,
    client_input: ClientInput,
    /// Set after `Ended` (or actor gone): `next_event` returns `None` from then on.
    ended: bool,
}

impl LocalTransport {
    /// Attach in-process as a `Mirror` (= `attach_with(.., Mirror)`).
    pub async fn attach(
        handle: SessionHandle,
        meta: ClientMeta,
    ) -> Result<(Self, AttachSnapshot), TransportError> {
        Self::attach_with(handle, meta, AttachMode::Mirror).await
    }

    /// Attach in-process: sends Attach, awaits the Attached envelope
    /// (bounded 5 s), returns the transport + snapshot.
    pub async fn attach_with(
        handle: SessionHandle,
        meta: ClientMeta,
        mode: AttachMode,
    ) -> Result<(Self, AttachSnapshot), TransportError> {
        // Subscribe BEFORE sending so the Attached reply cannot be missed.
        // `Attached` is broadcast; with one attach in flight per handle at a
        // time (every host today) the first one seen is ours.
        let mut rx = handle.subscribe();
        handle
            .send(SessionCommand::Attach {
                client: meta,
                mode,
            })
            .await?;

        let wait = async {
            loop {
                match rx.recv().await {
                    Ok(env) => match env.event {
                        SessionEventWire::Attached { client, snapshot } => {
                            return Ok((client, snapshot));
                        }
                        SessionEventWire::AttachRefused { message } => {
                            return Err(TransportError::Refused(message));
                        }
                        _ => {}
                    },
                    Err(broadcast::error::RecvError::Lagged(_)) => continue,
                    Err(broadcast::error::RecvError::Closed) => {
                        return Err(TransportError::Closed)
                    }
                }
            }
        };
        let timeout = if handle.lifecycle() == super::types::SessionLifecycle::Parked {
            ATTACH_TIMEOUT_PARKED
        } else {
            ATTACH_TIMEOUT
        };
        let (client, snapshot) = match tokio::time::timeout(timeout, wait).await {
            Ok(r) => r?,
            Err(_) => return Err(TransportError::Protocol("attach timed out".into())),
        };
        let meta = snapshot.meta.clone();
        Ok((
            Self {
                handle,
                rx,
                client,
                meta,
                mode,
                input_owner: snapshot.input_owner,
                roster: Roster::from_snapshot(&snapshot),
                client_input: snapshot.client_input,
                ended: false,
            },
            snapshot,
        ))
    }

    pub fn handle(&self) -> &SessionHandle {
        &self.handle
    }
}

#[async_trait::async_trait]
impl ClientTransport for LocalTransport {
    fn session_id(&self) -> &SessionId {
        &self.handle.id
    }

    fn meta(&self) -> &SessionMeta {
        &self.meta
    }

    async fn send(&self, cmd: SessionCommand) -> Result<(), TransportError> {
        self.handle.send(cmd).await
    }

    async fn send_from_self(&self, cmd: SessionCommand) -> Result<(), TransportError> {
        self.handle.send_from(self.client, cmd).await
    }

    async fn next_event(&mut self) -> Option<Envelope> {
        if self.ended {
            return None;
        }
        // The handle itself holds a broadcast sender, so `Closed` never
        // fires on its own: end-of-stream is `Ended` or the actor going away
        // (drain what it left behind first).
        let recv = tokio::select! {
            biased;
            r = self.rx.recv() => r,
            _ = self.handle.closed() => match self.rx.try_recv() {
                Ok(env) => Ok(env),
                Err(broadcast::error::TryRecvError::Lagged(n)) => {
                    Err(broadcast::error::RecvError::Lagged(n))
                }
                Err(_) => Err(broadcast::error::RecvError::Closed),
            },
        };
        match recv {
            Ok(env) => {
                match &env.event {
                    SessionEventWire::Ended { .. } => self.ended = true,
                    SessionEventWire::InputOwnerChanged { to, .. } => self.input_owner = *to,
                    other => self.roster.apply(other),
                }
                Some(env)
            }
            Err(broadcast::error::RecvError::Lagged(n)) => {
                // server.rs Lagged pattern: warn, tell the client once, keep going.
                tracing::warn!(session = %self.handle.id, dropped = n, "session event stream lagged");
                Some(Envelope {
                    session_id: self.handle.id.clone(),
                    seq: u64::MAX,
                    ts: chrono::Utc::now(),
                    event: SessionEventWire::SystemNotice(format!(
                        "event stream lagged; {n} dropped"
                    )),
                })
            }
            Err(broadcast::error::RecvError::Closed) => {
                self.ended = true;
                None
            }
        }
    }

    fn view(&self) -> Arc<RuntimeView> {
        Arc::clone(&self.handle.view())
    }

    fn client_id(&self) -> ClientId {
        self.client
    }

    fn mode(&self) -> AttachMode {
        self.mode
    }

    fn input_owner(&self) -> Option<ClientId> {
        self.input_owner
    }

    fn roster(&self) -> Roster {
        self.roster.clone()
    }

    fn client_input(&self) -> ClientInput {
        self.client_input
    }

    async fn reconnect(&mut self, _mode: AttachMode) -> Result<AttachSnapshot, TransportError> {
        Err(TransportError::Unsupported("reconnect: in-process transport"))
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn skew_notice_only_when_the_binaries_differ() {
        use super::{version_skew_notice, BinaryId};
        let bin = |version: &str, build: &str| BinaryId { version: version.into(), build: build.into() };
        assert_eq!(version_skew_notice(&bin("0.10.2", "cd1fbf50"), &bin("0.10.2", "cd1fbf50"), "s"), None);
        // An older release.
        let n = version_skew_notice(&bin("0.10.1", "be670c09"), &bin("0.10.2", "cd1fbf50"), "20261011-041552-79cc")
            .unwrap();
        assert!(n.starts_with("this client is synaps 0.10.1 but the daemon is 0.10.2 —"), "{n}");
        assert!(n.ends_with("synaps --attach 20261011-041552-79cc"), "{n}");
        // Same release, another build (a reload onto a newer merge batch).
        let n = version_skew_notice(&bin("0.10.2", "ae490b32"), &bin("0.10.2", "cd1fbf50"), "s").unwrap();
        assert!(n.starts_with("this client is synaps 0.10.2 (ae490b32) but the daemon is 0.10.2 (cd1fbf50)"), "{n}");
        // Unknown build on either side (an older daemon, a packaged build):
        // versions only.
        assert_eq!(version_skew_notice(&bin("0.10.2", "ae490b32"), &bin("0.10.2", ""), "s"), None);
        assert_eq!(version_skew_notice(&bin("0.10.2", ""), &bin("0.10.2", "cd1fbf50"), "s"), None);
    }

    /// 04-tui I3: a client NEWER than the daemon was told to restart itself,
    /// which relaunches the same new binary. It must say to reload the daemon.
    #[test]
    fn skew_notice_names_the_side_that_is_behind() {
        use super::{version_skew_notice, BinaryId};
        let bin = |version: &str, build: &str| BinaryId { version: version.into(), build: build.into() };
        // Client newer (the usual upgrade path): reload the daemon.
        let n = version_skew_notice(&bin("0.10.3", "be670c09"), &bin("0.10.2", "cd1fbf50"), "s").unwrap();
        assert!(n.starts_with("this client is synaps 0.10.3 but the daemon is 0.10.2 —"), "{n}");
        assert!(n.contains("run `synaps daemon reload`"), "{n}");
        assert!(!n.to_lowercase().contains("restart the client"), "{n}");
        assert!(!n.contains("synaps --attach"), "{n}");
        // Numeric, not lexical: 0.10.0 is newer than 0.9.9.
        let n = version_skew_notice(&bin("0.10.0", ""), &bin("0.9.9", ""), "s").unwrap();
        assert!(n.contains("run `synaps daemon reload`"), "{n}");
        // Client older: restart the client, as before.
        let n = version_skew_notice(&bin("0.9.9", ""), &bin("0.10.0", ""), "s").unwrap();
        assert!(n.contains("Restart the client"), "{n}");
        assert!(!n.contains("daemon reload"), "{n}");
        // Same version, another build: which is newer is unknown; both remedies.
        let n = version_skew_notice(&bin("0.10.2", "ae490b32"), &bin("0.10.2", "cd1fbf50"), "s").unwrap();
        assert!(n.contains("run `synaps daemon reload`"), "{n}");
        assert!(n.contains("restart the client"), "{n}");
        assert!(n.contains("synaps --attach s"), "{n}");
    }

    /// An older daemon's `Welcome` has no build: it parses, build unknown.
    #[test]
    fn welcome_without_a_build_parses() {
        let w: super::super::wire::Welcome = serde_json::from_value(serde_json::json!({
            "protocol_version": 3, "daemon_version": "0.10.1", "pid": 7, "profile": null,
            "sessions": [], "progressive_tool_disclosure": true, "generation": 2
        }))
        .unwrap();
        assert_eq!(super::BinaryId::of(&w), super::BinaryId { version: "0.10.1".into(), build: String::new() });
    }

    use super::*;
    use crate::session::types::*;
    use crate::{LlmEvent, SessionEvent, StreamEvent};

    async fn next(t: &mut LocalTransport) -> Envelope {
        tokio::time::timeout(std::time::Duration::from_secs(2), t.next_event())
            .await
            .expect("timely")
            .expect("open")
    }

    /// Debug strings of every `Stream(_)` up to and including the next `Conversation`.
    async fn collect(t: &mut LocalTransport) -> Vec<String> {
        let mut out = Vec::new();
        loop {
            let env = next(t).await;
            let done = matches!(env.event, SessionEventWire::Conversation(_));
            if matches!(env.event, SessionEventWire::Stream(_)) {
                out.push(format!("{:?}", env.event));
            }
            if done {
                break;
            }
        }
        out
    }

    #[tokio::test]
    async fn local_transport_round_trip_over_echo() {
        let (handle, task) = SessionHandle::echo_for_test(SessionId::from("lt-1"));
        let (mut t, snap) = LocalTransport::attach(handle, ClientMeta::new(ClientKind::Test))
            .await
            .unwrap();
        assert_eq!(t.session_id().as_str(), "lt-1");
        assert_eq!(t.client_id(), ClientId(1));
        assert_eq!(t.mode(), AttachMode::Mirror);
        assert!(t.input_owner().is_none());
        assert!(matches!(t.reconnect(AttachMode::Mirror).await, Err(TransportError::Unsupported(_))));
        assert_eq!(snap.meta.model, "echo");
        assert_eq!(t.view().model, "echo");
        assert!(snap.conversation.api_messages.is_empty());

        // ClientJoined follows Attached on the shared channel.
        let joined = next(&mut t).await;
        assert!(matches!(
            joined.event,
            SessionEventWire::ClientJoined {
                client: ClientId(1),
                kind: ClientKind::Test,
                ..
            }
        ));

        t.send(SessionCommand::Submit {
            text: "ping".into(),
            attachments: vec![],
        })
        .await
        .unwrap();

        let mut seen = Vec::new();
        loop {
            let env = next(&mut t).await;
            let done = matches!(env.event, SessionEventWire::Conversation(_));
            seen.push(env);
            if done {
                break;
            }
        }
        assert!(matches!(seen[0].event, SessionEventWire::TurnStarted { .. }));
        match &seen[1].event {
            SessionEventWire::Stream(StreamEvent::Llm(LlmEvent::Text(t))) => assert_eq!(t, "ping"),
            other => panic!("unexpected {other:?}"),
        }
        assert!(matches!(
            seen[3].event,
            SessionEventWire::Stream(StreamEvent::Session(SessionEvent::Done))
        ));
        let seqs: Vec<u64> = seen.iter().map(|e| e.seq).collect();
        assert!(seqs.windows(2).all(|w| w[1] == w[0] + 1), "gapless: {seqs:?}");

        // Query{View} answered with the published view.
        t.send(SessionCommand::Query {
            id: 7,
            query: SessionQuery::View,
        })
        .await
        .unwrap();
        let q = next(&mut t).await;
        match q.event {
            SessionEventWire::QueryResult { id, value } => {
                assert_eq!(id, 7);
                assert_eq!(value["model"], "echo");
            }
            other => panic!("unexpected {other:?}"),
        }

        // Detach does not end the session; End does.
        t.send(SessionCommand::Detach { client: t.client_id() })
            .await
            .unwrap();
        let left = next(&mut t).await;
        assert!(matches!(left.event, SessionEventWire::ClientLeft { client: ClientId(1) }));
        assert!(t.handle().is_alive());

        t.send(SessionCommand::End {
            reason: EndReason::HostShutdown,
        })
        .await
        .unwrap();
        let ended = next(&mut t).await;
        assert!(matches!(ended.event, SessionEventWire::Ended { .. }));
        task.await.unwrap();
        assert!(t.next_event().await.is_none());
        assert!(matches!(
            t.send(SessionCommand::Save).await,
            Err(TransportError::Closed)
        ));
    }

    #[tokio::test]
    async fn two_local_clients_see_the_same_stream() {
        let (handle, task) = SessionHandle::echo_for_test(SessionId::from("lt-2"));
        let (mut a, _) = LocalTransport::attach(handle.clone(), ClientMeta::new(ClientKind::Tui))
            .await
            .unwrap();
        let (mut b, snap_b) = LocalTransport::attach(handle.clone(), ClientMeta::new(ClientKind::Attach))
            .await
            .unwrap();
        assert_ne!(a.client_id(), b.client_id());
        assert_eq!(snap_b.clients.len(), 2);

        a.send(SessionCommand::Submit {
            text: "x".into(),
            attachments: vec![],
        })
        .await
        .unwrap();

        let sa = collect(&mut a).await;
        let sb = collect(&mut b).await;
        assert_eq!(sa, sb);
        assert_eq!(sa.len(), 3);

        drop((a, b, handle));
        task.await.unwrap();
    }
}