simulator-client 0.29.0

Async WebSocket client for the Solana simulator backtest API
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
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//! Sessions that reconnect on their own.
//!
//! Use [`ManagedBacktestSession`] for one session, or [`ManagedParallelSession`] for a run
//! the server splits across concurrent sessions. Each one:
//!
//! - opens the session control channel and returns once the server has created the session;
//! - opens one WebSocket on the session's RPC endpoint per `subscribe_*` call;
//! - waits until every connection is up before it sends a continue;
//! - reconnects a dropped connection and reattaches to the session;
//! - closes the session on `shutdown`.
//!
//! # Connection drops
//!
//! - A dropped connection is retried with backoff, up to [`RECONNECT_MAX_ATTEMPTS`] attempts
//!   within [`RECONNECT_MAX_TOTAL`]. The session keeps running on the server meanwhile.
//! - The session control channel reattaches and the server replays the responses you missed.
//!   You see no gap.
//! - A subscription resumes from the last slot it delivered and sends that slot again: dedupe
//!   transactions by signature, and account diffs by (signature, account, slot).
//! - When the budget runs out, the next continue fails with
//!   [`ManagedSessionError::ControlFailed`] or [`ManagedSessionError::SubscriptionFailed`].
//! - A rejected API key (HTTP 401 or 403) is never retried: `start` fails with
//!   [`ManagedSessionError::ApiKeyRejected`].

use std::{
    future::Future,
    sync::{
        Arc,
        atomic::{AtomicU64, AtomicUsize, Ordering},
    },
    time::{Duration, Instant},
};

use bon::Builder;
use futures::SinkExt;
use http::{HeaderMap, StatusCode};
use rand::RngExt;
use simulator_api::{
    BacktestError, BacktestRequest, CreateBacktestSessionRequest, SessionId,
    client_identity::{ClientProduct, ClientSurface},
};
use tokio::{
    net::TcpStream,
    sync::{Notify, OwnedSemaphorePermit, Semaphore, watch},
};
use tokio_tungstenite::{
    MaybeTlsStream, WebSocketStream, connect_async,
    tungstenite::{Error as WsError, Message, client::IntoClientRequest, http::HeaderValue},
};
use tokio_util::sync::CancellationToken;
use tracing::warn;

use crate::{error::err_chain, urls::backtest_ws_url};

mod control;
mod parallel;
mod session;
mod subscription;

pub use control::{ControlEvent, ControlHandle, spawn_control_manager};
pub use parallel::{ManagedParallelSession, ParallelSubSession};
pub use session::{CreateError, ManagedBacktestSession, ManagedEvent, ManagedSessionError};
pub use subscription::{
    SubscriptionHandle, SubscriptionNotification, spawn_account_diff_subscription_manager,
    spawn_action_subscription_manager, spawn_diagnostic_subscription_manager,
    spawn_replacement_subscription_manager, spawn_transaction_subscription_manager,
};

/// Timeout for the initial WebSocket connect (TCP + TLS + HTTP upgrade).
#[doc(hidden)]
pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(30);

/// Timeout for each server response while a connection is set up: the create, reattach and
/// subscribe replies. An established connection is checked with pings instead
/// ([`KEEPALIVE_MISS_DEADLINE`]).
// Generous because the server can hold a subscribe until the session has started (~100 s on a
// cold start); a shorter timeout reports a healthy session as "subscribe ack timeout".
#[doc(hidden)]
pub const HANDSHAKE_RESPONSE_TIMEOUT: Duration = Duration::from_secs(120);

/// How often to send a WebSocket ping during an established connection.
#[doc(hidden)]
pub const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(15);

/// How long without inbound traffic before declaring the connection dead.
/// Roughly three missed pings.
#[doc(hidden)]
pub const KEEPALIVE_MISS_DEADLINE: Duration = Duration::from_secs(45);

/// Total budget for the graceful close handshake (close request + close frame).
/// A supervisor's SIGTERM→SIGKILL grace must exceed this or the close is cut short.
#[doc(hidden)]
pub const GRACEFUL_CLOSE_TIMEOUT: Duration = Duration::from_secs(5);

/// Wait before the first retry. Each later wait is [`RECONNECT_BACKOFF_MULTIPLIER`] times the
/// previous one, up to [`RECONNECT_MAX_BACKOFF`].
#[doc(hidden)]
pub const RECONNECT_INITIAL_BACKOFF: Duration = Duration::from_secs(1);
/// Longest wait between two retries.
#[doc(hidden)]
pub const RECONNECT_MAX_BACKOFF: Duration = Duration::from_secs(30);
/// Factor applied to the wait after each failed retry.
#[doc(hidden)]
pub const RECONNECT_BACKOFF_MULTIPLIER: f64 = 2.0;
/// Random spread of each wait, as a fraction: `0.2` is ±20%.
#[doc(hidden)]
pub const RECONNECT_JITTER: f64 = 0.2;
/// Time budget for retrying one connection. When it, or [`RECONNECT_MAX_ATTEMPTS`], runs out,
/// the connection becomes [`ConnectionStatus::Failed`].
pub const RECONNECT_MAX_TOTAL: Duration = Duration::from_secs(5 * 60);
/// Attempt budget for retrying one connection. See [`RECONNECT_MAX_TOTAL`].
pub const RECONNECT_MAX_ATTEMPTS: u32 = 20;

/// In a parallel run, the retries a dropped subscription makes right away. After these, it
/// waits until the run's other sessions stop streaming, then reconnects into the freed link.
#[doc(hidden)]
pub const RECONNECT_UNGATED_ATTEMPTS: u32 = 5;

/// A connection that stays `Up` for this long gets its full retry budget back.
#[doc(hidden)]
pub const RECONNECT_UPTIME_RESET: Duration = Duration::from_secs(30);

/// How long the server keeps a session alive with no client attached, when a single
/// session's create leaves `disconnect_timeout_secs` unset.
///
/// Covers the whole retry budget ([`RECONNECT_MAX_TOTAL`]) plus a minute to reattach, so a
/// server restart does not lose the session. Stays under the server's cap
/// ([`simulator_api::MAX_DISCONNECT_TIMEOUT_SECS`]). A parallel run keeps the server's own
/// default.
pub const DEFAULT_DISCONNECT_TIMEOUT_SECS: u16 = RECONNECT_MAX_TOTAL.as_secs() as u16 + 60;

/// State of one managed connection: the session control channel or a subscription.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ConnectionStatus {
    /// Connected and, for a subscription, subscribed.
    Up,
    /// Not connected yet, or reconnecting.
    Down,
    /// Gave up, for the reason given. Terminal: the retry budget ran out, the API key was
    /// rejected, or the server ended the session.
    Failed(String),
}

/// What the server reported when it created the session.
#[derive(Clone, Debug)]
pub struct SessionInfo {
    #[allow(missing_docs)]
    pub session_id: SessionId,
    /// A session of a parallel run carries the run's name.
    pub session_name: Option<String>,
    /// Absolute `http(s)` JSON-RPC URL for chain reads at the session's slot; serves state from
    /// the first `ReadyForContinue` until `Completed` (502 after). Use
    /// [`ManagedBacktestSession::rpc`], or pass it to `RpcClient::new`.
    pub rpc_endpoint: String,
    /// Opaque `task_id` reported by the server for this session, if any.
    pub task_id: Option<String>,
}

/// Parameters shared by [`ManagedBacktestSession`] and
/// [`ManagedParallelSession`] startup.
///
/// A standalone session only needs `url`, `api_key` and `create`; the rest
/// default to no extra headers, a session-owned cancellation token and its own
/// counters.
#[derive(Debug, Clone, Builder)]
pub struct StartSessionParams {
    /// The server's address. The builder normalizes it with [`crate::backtest_ws_url`]: a bare
    /// host becomes `wss://<host>/backtest`, and a `ws://` or `wss://` URL gains `/backtest`
    /// if it lacks it.
    #[builder(with = |url: impl Into<String>| backtest_ws_url(&url.into()))]
    pub url: String,
    /// Your API key, sent as the `X-API-Key` header.
    pub api_key: String,
    /// Extra headers sent when the session control channel connects, beside the API key.
    #[builder(default)]
    pub extra_headers: HeaderMap,
    /// The create request, built with [`crate::CreateSession`] and its `into_request`. Set
    /// `parallel` on it to start a [`ManagedParallelSession`].
    pub create: CreateBacktestSessionRequest,
    /// Cancelling this aborts startup, or stops a started session; its calls then return
    /// [`ManagedSessionError::Cancelled`] from `next_event`.
    #[builder(default)]
    pub parent_cancel: CancellationToken,
    /// Collects counters from every subscription the session spawns;
    /// share one across sessions to aggregate a whole run.
    #[builder(default)]
    pub subscription_diagnostics: Arc<SubscriptionDiagnostics>,
}

/// Subscription diagnostics counters, shared across every subscription manager a
/// run spawns.
#[derive(Debug, Default)]
pub struct SubscriptionDiagnostics {
    /// Excludes each subscription task's first connection.
    reconnects: AtomicU64,
}

impl SubscriptionDiagnostics {
    pub(crate) fn record_reconnect(&self) {
        self.reconnects.fetch_add(1, Ordering::Relaxed);
    }

    /// Subscription reconnects so far, across every session sharing these counters. First
    /// connections do not count.
    pub fn reconnects(&self) -> u64 {
        self.reconnects.load(Ordering::Relaxed)
    }
}

/// Tracks reconnect attempts and enforces the reconnect policy.
pub(crate) struct ReconnectBudget {
    attempts: u32,
    started_at: std::time::Instant,
    current_backoff: Duration,
}

impl ReconnectBudget {
    pub fn new() -> Self {
        Self {
            attempts: 0,
            started_at: std::time::Instant::now(),
            current_backoff: RECONNECT_INITIAL_BACKOFF,
        }
    }

    pub fn reset(&mut self) {
        self.attempts = 0;
        self.started_at = std::time::Instant::now();
        self.current_backoff = RECONNECT_INITIAL_BACKOFF;
    }

    pub fn attempt(&self) -> u32 {
        self.attempts
    }

    /// Discount time the subscription spent parked behind the reconnect
    /// coordinator. Parking is deliberate waiting for siblings to free the
    /// link, not retry thrashing, so it must not count against the wall-clock
    /// budget — otherwise a session that correctly steps aside for minutes is
    /// failed the moment it reconnects. The attempt-count ceiling still bounds
    /// total retries.
    pub fn discount_parked(&mut self, parked: Duration) {
        self.started_at += parked;
    }

    /// Record an attempt and return its backoff, or `None` if the budget is
    /// exhausted.
    pub fn next_backoff(&mut self) -> Option<Duration> {
        if self.attempts >= RECONNECT_MAX_ATTEMPTS
            || self.started_at.elapsed() >= RECONNECT_MAX_TOTAL
        {
            return None;
        }
        self.attempts += 1;
        let backoff = with_jitter(self.current_backoff);
        self.current_backoff = std::cmp::min(
            RECONNECT_MAX_BACKOFF,
            Duration::from_secs_f64(
                self.current_backoff.as_secs_f64() * RECONNECT_BACKOFF_MULTIPLIER,
            ),
        );
        Some(backoff)
    }
}

/// Coordinates subscription reconnects across a parallel batch sharing one
/// bandwidth-constrained link.
///
/// A dropped subscription competing with its still-streaming siblings just
/// starves — the handshake can't get through a saturated pipe. So once a session
/// has burned its fast ungated attempts, it stops fighting and **parks until no
/// sibling is streaming** (the connected sessions have finished and freed their
/// bandwidth), then reconnects into the quiet link and resumes via
/// `replayFromSlot`. Stepping aside also speeds the streaming siblings up, so the
/// batch drains and the parked sessions recover — serially, one at a time, since
/// at a tight rate only one stream fits.
#[doc(hidden)]
pub struct ReconnectCoordinator {
    /// Subscriptions currently `Up` and streaming. A parked session waits for
    /// this to reach zero before reconnecting.
    streaming: AtomicUsize,
    /// Woken whenever `streaming` decreases, so parked sessions re-check.
    drained: Notify,
    /// One in-flight reconnect handshake at a time, so the all-parked case
    /// recovers serially rather than storming the link the moment it goes quiet.
    handshake: Arc<Semaphore>,
}

impl Default for ReconnectCoordinator {
    fn default() -> Self {
        Self::new()
    }
}

impl ReconnectCoordinator {
    pub fn new() -> Self {
        Self {
            streaming: AtomicUsize::new(0),
            drained: Notify::new(),
            handshake: Arc::new(Semaphore::new(1)),
        }
    }

    /// Mark this subscription as streaming until the returned guard drops.
    /// Call once a (re)subscribe succeeds; the guard's `Drop` decrements the
    /// count and wakes any sibling parked in [`Self::reconnect_slot`]. RAII so
    /// a panicking or aborted task can't leak the count and wedge the batch.
    pub fn enter(self: &Arc<Self>) -> StreamingGuard {
        self.streaming.fetch_add(1, Ordering::SeqCst);
        StreamingGuard(self.clone())
    }

    /// Park until no sibling is streaming, then take the single reconnect slot.
    /// The returned permit must be held across connect + subscribe and dropped
    /// once [`Self::enter`] has been called (or the attempt failed), so the next
    /// parked session proceeds only after this one is streaming again. Returns
    /// `None` if cancelled.
    pub async fn reconnect_slot(&self, cancel: &CancellationToken) -> Option<OwnedSemaphorePermit> {
        loop {
            // Wait for the link to go quiet. Register interest before reading so
            // a `leave()` between the check and the wait isn't lost.
            loop {
                let drained = self.drained.notified();
                if self.streaming.load(Ordering::SeqCst) == 0 {
                    break;
                }
                tokio::select! {
                    biased;
                    _ = cancel.cancelled() => return None,
                    _ = drained => {}
                }
            }
            let permit = tokio::select! {
                biased;
                _ = cancel.cancelled() => return None,
                p = self.handshake.clone().acquire_owned() => p.ok()?,
            };
            // A sibling may have re-entered while we queued for the permit; if so,
            // release it and wait for quiet again rather than reconnect into
            // contention.
            if self.streaming.load(Ordering::SeqCst) == 0 {
                return Some(permit);
            }
        }
    }
}

/// Holds a [`ReconnectCoordinator`]'s streaming count for the lifetime of one
/// connection; decrements and wakes parked siblings on drop.
#[doc(hidden)]
pub struct StreamingGuard(Arc<ReconnectCoordinator>);

impl Drop for StreamingGuard {
    fn drop(&mut self) {
        self.0.streaming.fetch_sub(1, Ordering::SeqCst);
        self.0.drained.notify_waiters();
    }
}

fn with_jitter(d: Duration) -> Duration {
    let jitter = rand::rng().random_range(-RECONNECT_JITTER..RECONNECT_JITTER);
    let secs = (d.as_secs_f64() * (1.0 + jitter)).max(0.0);
    Duration::from_secs_f64(secs)
}

/// Sleep for `delay`, returning early on cancellation. Returns `true` if the
/// sleep completed, `false` if cancelled.
pub(crate) async fn cancellable_sleep(delay: Duration, cancel: &CancellationToken) -> bool {
    tokio::select! {
        _ = tokio::time::sleep(delay) => true,
        _ = cancel.cancelled() => false,
    }
}

/// A managed control WebSocket stream.
pub(super) type Ws = WebSocketStream<MaybeTlsStream<TcpStream>>;

/// Why [`connect_ws`] failed.
pub(super) enum ConnectError {
    /// The upgrade was refused with 401/403: the key is bad, so retrying only
    /// stalls the caller for the whole reconnect budget.
    ApiKeyRejected(StatusCode),
    /// Anything else, worth retrying.
    Other(String),
}

/// Open a control WebSocket to `url`, attaching `extra_headers` and the API-key
/// and identity headers, bounded by [`CONNECT_TIMEOUT`]. Shared by the
/// single-session and parallel managers.
pub(super) async fn connect_ws(
    url: &str,
    api_key: &str,
    extra_headers: &HeaderMap,
) -> Result<Ws, ConnectError> {
    let mut request = url
        .into_client_request()
        .map_err(|e| ConnectError::Other(format!("build request: {}", err_chain(&e))))?;
    request.headers_mut().extend(extra_headers.clone());
    request.headers_mut().insert(
        "X-API-Key",
        HeaderValue::from_str(api_key)
            .map_err(|e| ConnectError::Other(format!("api key header: {}", err_chain(&e))))?,
    );
    request.headers_mut().insert(
        "user-agent",
        HeaderValue::from_str(&crate::client::user_agent(
            ClientProduct::Sdk,
            None,
            ClientSurface::Managed,
        ))
        .map_err(|e| ConnectError::Other(format!("build user-agent: {e}")))?,
    );

    let connect = tokio::time::timeout(CONNECT_TIMEOUT, connect_async(request))
        .await
        .map_err(|_| ConnectError::Other(format!("connect timeout after {CONNECT_TIMEOUT:?}")))?
        .map_err(|e| match &e {
            WsError::Http(response)
                if matches!(
                    response.status(),
                    StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN
                ) =>
            {
                ConnectError::ApiKeyRejected(response.status())
            }
            _ => ConnectError::Other(format!("connect: {}", err_chain(&e))),
        })?;
    Ok(connect.0)
}

/// Serialize and send a `BacktestRequest` over a control WebSocket.
pub(super) async fn send_request(ws: &mut Ws, req: &BacktestRequest) -> Result<(), String> {
    let text = serde_json::to_string(req).map_err(|e| format!("serialize: {}", err_chain(&e)))?;
    ws.send(Message::Text(text))
        .await
        .map_err(|e| format!("send: {}", err_chain(&e)))
}

/// Resolve a server-reported `rpc_endpoint` to an absolute URL: pass through an
/// already-absolute endpoint, otherwise join it onto the control `base`.
pub(super) fn resolve_rpc_url(base: &str, endpoint: &str) -> String {
    if endpoint.starts_with("http://") || endpoint.starts_with("https://") {
        endpoint.to_string()
    } else {
        format!("{}/{}", base, endpoint.trim_start_matches('/'))
    }
}

/// Outcome of a control-WS handshake (create/attach/resume).
pub(super) enum HandshakeError {
    /// Recoverable (network blip, or the session is momentarily contended).
    Transient(String),
    /// Unrecoverable — the server rejected the session outright.
    Fatal(CreateError),
}

/// Classify a `BacktestError` returned during a handshake `stage`: a contended
/// session is expected to become claimable shortly (transient); anything else is
/// fatal.
pub(super) fn handshake_error_for_response(
    stage: &'static str,
    err: BacktestError,
) -> HandshakeError {
    match err {
        BacktestError::SessionOwnershipBusy { .. } => {
            HandshakeError::Transient(format!("{stage} contended: {}", err_chain(&err)))
        }
        _ => HandshakeError::Fatal(CreateError::Rejected(err)),
    }
}

/// Why a control task's message loop exited. Shared by the single-session and
/// parallel control managers.
pub(super) enum MessageLoopExit {
    /// Server reported the session(s) done, or the driver(s) dropped the request
    /// channel. Calls for a graceful WS close.
    SessionEnded,
    /// Cancellation token fired. Still graceful-closes so the server reaps the
    /// runtime now instead of after the disconnect timeout.
    Cancelled,
    /// Connection lost — attempt to reconnect.
    ConnectionLost(String),
    /// Protocol or application error with no sensible recovery.
    Terminal(String),
}

/// Publish a `ConnectionStatus` transition, suppressing no-op updates.
pub(super) fn publish_status(
    status_tx: &watch::Sender<ConnectionStatus>,
    status: ConnectionStatus,
) {
    status_tx.send_if_modified(|current| {
        if *current == status {
            false
        } else {
            *current = status;
            true
        }
    });
}

/// Send `CloseBacktestSession` then the WS close frame, bounded by a single
/// [`GRACEFUL_CLOSE_TIMEOUT`] budget. The close request makes the server reap
/// the runtime(s) immediately instead of waiting out the disconnect timeout; the
/// close frame is best-effort cleanup.
pub(super) async fn graceful_close(ws: &mut Ws) {
    let _ = tokio::time::timeout(GRACEFUL_CLOSE_TIMEOUT, async {
        let _ = send_request(ws, &BacktestRequest::CloseBacktestSession).await;
        let _ = ws.close(None).await;
    })
    .await;
}

/// An inbound control frame, classified for the message loop.
pub(super) enum InboundFrame {
    /// A text (or UTF-8 binary) payload to parse.
    Text(String),
    /// A frame with no application payload (ping/pong/raw/non-UTF-8 binary).
    Ignore,
    /// The connection ended or errored; carries the loss reason.
    Lost(String),
}

/// Classify the result of `ws.next()` for a control message loop.
pub(super) fn classify_inbound(msg: Option<Result<Message, WsError>>) -> InboundFrame {
    match msg {
        Some(Ok(Message::Text(t))) => InboundFrame::Text(t),
        Some(Ok(Message::Binary(b))) => match String::from_utf8(b) {
            Ok(t) => InboundFrame::Text(t),
            Err(_) => InboundFrame::Ignore,
        },
        Some(Ok(Message::Pong(_) | Message::Ping(_) | Message::Frame(_))) => InboundFrame::Ignore,
        Some(Ok(Message::Close(frame))) => InboundFrame::Lost(format!("remote close: {frame:?}")),
        Some(Err(e)) => InboundFrame::Lost(format!("ws read: {}", err_chain(&e))),
        None => InboundFrame::Lost("ws stream ended".into()),
    }
}

/// Send a keepalive ping unless inbound traffic has been silent past the
/// deadline. Returns the loss reason if the connection should be considered
/// dead, else `None`.
pub(super) async fn send_keepalive_ping(ws: &mut Ws, last_inbound: Instant) -> Option<String> {
    if last_inbound.elapsed() > KEEPALIVE_MISS_DEADLINE {
        return Some(format!("no traffic for {:?}", last_inbound.elapsed()));
    }
    if let Err(e) = ws.send(Message::Ping(vec![])).await {
        return Some(format!("ping send: {}", err_chain(&e)));
    }
    None
}

/// A control WebSocket task: connect, handshake, run a message loop, reconnect.
/// [`run_control_loop`] drives the shared connect/backoff/exit state machine;
/// implementors supply the protocol-specific handshake and message loop.
pub(super) trait ControlConnection: Send + 'static {
    fn url(&self) -> &str;
    fn api_key(&self) -> &str;
    fn extra_headers(&self) -> &HeaderMap;
    fn cancel(&self) -> &CancellationToken;
    /// Prefix for reconnect log lines, e.g. `"control"` / `"parallel control"`.
    fn label(&self) -> &'static str;
    fn status_tx(&self) -> &watch::Sender<ConnectionStatus>;
    /// Fail the pending create one-shot with `error` (no-op once fired).
    fn fail_pending(&mut self, error: ManagedSessionError);
    /// Create on first connect, attach/resume on reconnect.
    fn handshake(&mut self, ws: Ws) -> impl Future<Output = Result<Ws, HandshakeError>> + Send;
    /// Drive the established connection until it exits.
    fn message_loop(&mut self, ws: Ws) -> impl Future<Output = MessageLoopExit> + Send;

    fn publish(&self, status: ConnectionStatus) {
        publish_status(self.status_tx(), status);
    }

    fn finish_failed(&mut self, error: CreateError) {
        self.publish(ConnectionStatus::Failed(error.to_string()));
        self.fail_pending(ManagedSessionError::Create(error));
    }
}

/// Drive a [`ControlConnection`] through its connect → handshake → run →
/// reconnect lifecycle until the session ends, is cancelled, or the reconnect
/// budget is exhausted.
pub(super) async fn run_control_loop<T: ControlConnection>(mut task: T) {
    let mut budget = ReconnectBudget::new();

    loop {
        if task.cancel().is_cancelled() {
            task.fail_pending(ManagedSessionError::Cancelled);
            return;
        }
        task.publish(ConnectionStatus::Down);

        let ws = match connect_ws(task.url(), task.api_key(), task.extra_headers()).await {
            Ok(ws) => ws,
            Err(ConnectError::ApiKeyRejected(status)) => {
                let rejected = ManagedSessionError::ApiKeyRejected(status);
                task.publish(ConnectionStatus::Failed(format!("connect: {rejected}")));
                task.fail_pending(rejected);
                return;
            }
            Err(ConnectError::Other(why)) => {
                if let Some(delay) = budget.next_backoff() {
                    warn!(attempt = budget.attempt(), error = %why, ?delay, "{} connect failed, retrying", task.label());
                    if !cancellable_sleep(delay, task.cancel()).await {
                        return;
                    }
                    continue;
                }
                task.finish_failed(CreateError::Connection(format!("connect: {why}")));
                return;
            }
        };

        let ws = match task.handshake(ws).await {
            Ok(ws) => ws,
            Err(HandshakeError::Fatal(error)) => {
                task.finish_failed(error);
                return;
            }
            Err(HandshakeError::Transient(why)) => {
                if let Some(delay) = budget.next_backoff() {
                    warn!(attempt = budget.attempt(), error = %why, ?delay, "{} handshake failed, retrying", task.label());
                    if !cancellable_sleep(delay, task.cancel()).await {
                        return;
                    }
                    continue;
                }
                task.finish_failed(CreateError::Connection(format!("handshake: {why}")));
                return;
            }
        };

        task.publish(ConnectionStatus::Up);
        let connected_at = Instant::now();

        match task.message_loop(ws).await {
            MessageLoopExit::SessionEnded | MessageLoopExit::Cancelled => return,
            MessageLoopExit::ConnectionLost(why) => {
                if connected_at.elapsed() >= RECONNECT_UPTIME_RESET {
                    budget.reset();
                }
                if let Some(delay) = budget.next_backoff() {
                    warn!(attempt = budget.attempt(), reason = %why, ?delay, "{} connection lost, reconnecting", task.label());
                    if !cancellable_sleep(delay, task.cancel()).await {
                        return;
                    }
                    continue;
                }
                task.finish_failed(CreateError::Connection(format!("connection lost: {why}")));
                return;
            }
            MessageLoopExit::Terminal(why) => {
                // The create resolved before the message loop started: nothing is pending.
                task.publish(ConnectionStatus::Failed(why));
                return;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The request head `connect_ws` sends with API key `k`. Never completes the handshake;
    /// the headers are in the request head.
    async fn handshake_head(extra_headers: &HeaderMap) -> String {
        use tokio::io::AsyncReadExt;

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind");
        let addr = listener.local_addr().expect("addr");
        let accepted = tokio::spawn(async move {
            let (mut stream, _) = listener.accept().await.expect("accept");
            let mut buf = vec![0_u8; 4096];
            let read = stream.read(&mut buf).await.expect("read");
            String::from_utf8_lossy(&buf[..read]).to_string()
        });

        let _ = connect_ws(&format!("ws://{addr}"), "k", extra_headers).await;

        accepted.await.expect("join")
    }

    fn header_value(head: &str, name: &str) -> Option<String> {
        head.lines().find_map(|line| {
            let (key, value) = line.split_once(':')?;
            key.eq_ignore_ascii_case(name)
                .then(|| value.trim().to_string())
        })
    }

    /// The only connect path that sends the managed form.
    #[tokio::test]
    async fn connect_ws_identifies_itself_as_managed() {
        let head = handshake_head(&HeaderMap::new()).await;
        let user_agent = header_value(&head, "user-agent").expect("connect_ws sent a User-Agent");
        assert_eq!(
            simulator_api::client_identity::classify(Some(&user_agent)).0,
            simulator_api::client_identity::ClientKind::Managed,
        );
    }

    #[tokio::test]
    async fn connect_ws_sends_the_extra_headers_beside_the_api_key() {
        let extra_headers = HeaderMap::from_iter([(
            http::HeaderName::from_static("x-extra"),
            HeaderValue::from_static("extra"),
        )]);

        let head = handshake_head(&extra_headers).await;

        assert_eq!(header_value(&head, "x-extra").as_deref(), Some("extra"));
        assert_eq!(header_value(&head, "x-api-key").as_deref(), Some("k"));
    }

    #[test]
    fn budget_exhausts_after_max_attempts() {
        let mut b = ReconnectBudget::new();
        for _ in 0..RECONNECT_MAX_ATTEMPTS {
            assert!(b.next_backoff().is_some());
        }
        assert!(b.next_backoff().is_none());
    }

    #[test]
    fn budget_reset_restores_full_budget() {
        let mut b = ReconnectBudget::new();
        b.next_backoff();
        b.next_backoff();
        b.reset();
        assert_eq!(b.attempt(), 0);
    }

    #[test]
    fn streaming_guard_balances_the_count() {
        let coord = Arc::new(ReconnectCoordinator::new());
        assert_eq!(coord.streaming.load(Ordering::SeqCst), 0);
        let g = coord.enter();
        assert_eq!(coord.streaming.load(Ordering::SeqCst), 1);
        drop(g);
        assert_eq!(coord.streaming.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn reconnect_slot_available_when_link_is_quiet() {
        let coord = Arc::new(ReconnectCoordinator::new());
        let cancel = CancellationToken::new();
        assert!(coord.reconnect_slot(&cancel).await.is_some());
    }

    #[tokio::test]
    async fn reconnect_slot_unparks_when_last_sibling_leaves() {
        let coord = Arc::new(ReconnectCoordinator::new());
        let cancel = CancellationToken::new();
        let guard = coord.enter(); // streaming == 1, so a slot request must park

        let waiter = tokio::spawn({
            let coord = coord.clone();
            let cancel = cancel.clone();
            async move { coord.reconnect_slot(&cancel).await.is_some() }
        });

        // While a sibling streams, the parked request stays pending.
        tokio::task::yield_now().await;
        assert!(!waiter.is_finished());

        drop(guard); // streaming -> 0, wakes the parked request
        assert!(waiter.await.unwrap());
    }

    #[tokio::test]
    async fn reconnect_slot_returns_none_on_cancel_while_parked() {
        let coord = Arc::new(ReconnectCoordinator::new());
        let _guard = coord.enter(); // keep the link busy so the request parks
        let cancel = CancellationToken::new();
        cancel.cancel();
        assert!(coord.reconnect_slot(&cancel).await.is_none());
    }

    #[test]
    fn discount_parked_does_not_consume_the_budget() {
        let mut b = ReconnectBudget::new();
        // A long park must leave the full attempt budget intact afterwards.
        b.discount_parked(2 * RECONNECT_MAX_TOTAL);
        for _ in 0..RECONNECT_MAX_ATTEMPTS {
            assert!(b.next_backoff().is_some());
        }
    }
}