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
//! Ergonomic async client for the simulator backtest WebSocket API.
//!
//! This wraps the `simulator-api` request/response protocol with helpers for common
//! workflows (create session, wait ready, continue/advance, close), while still allowing full
//! access to raw `BacktestRequest` / `BacktestResponse` when needed.
//!
//! The crate has two layers, and picking the right one matters:
//!
//! - [`ManagedBacktestSession`] (and [`managed::ManagedParallelSession`]) own the
//! control and subscription WebSockets, keepalives, and — critically —
//! **reconnect and reattach**. Use this layer.
//! - [`BacktestClient`] / [`BacktestSession`] are one raw WebSocket with no
//! reconnect and no reattach. A dropped connection ends the run. Reach for it
//! only when driving the protocol by hand.
//!
//! A backtest session outlives its WebSocket, but only if something reattaches
//! to it — see [Reconnects and session handoff](#reconnects-and-session-handoff).
//!
//! ## Quickstart
//! ```no_run
//! use simulator_client::{Continue, CreateSession, ManagedBacktestSession, managed::ManagedEvent};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let create = CreateSession::builder()
//! .start_slot(100)
//! .slot_count(10)
//! .build()
//! .into_request()?;
//!
//! let mut session = ManagedBacktestSession::start(
//! "wss://simulator.termina.technology/backtest".to_string(),
//! "my-api-key".to_string(),
//! create,
//! )
//! .await?;
//!
//! loop {
//! match session.next_event().await? {
//! ManagedEvent::ReadyForContinue => {
//! let advance = Continue::builder().advance_count(1).build().into_params();
//! session.send_continue(advance).await?;
//! }
//! ManagedEvent::Completed { .. } => break,
//! _ => {}
//! }
//! }
//!
//! session.shutdown().await;
//! # Ok(()) }
//! ```
//!
//! ## Reconnects and session handoff
//!
//! The management service is a proxy: the session itself runs on a separate
//! host and survives the loss of your control WebSocket. What it does *not*
//! survive is nobody coming back for it.
//!
//! The management service is redeployed routinely. On shutdown it pauses each
//! live session, records it as handed off, and closes the control WebSocket
//! with code `1012` (`management handoff`). The session then waits to be
//! claimed. A client that reconnects and sends `AttachBacktestSession` +
//! `ResumeAttachedSession` picks it up where it left off, replaying any
//! responses it missed from `last_sequence`. A client that treats the close as
//! fatal loses the run: the server reclaims the unclaimed session once
//! `disconnect_timeout_secs` elapses.
//!
//! Not every disconnect is that graceful — a load balancer draining a target
//! can reset the socket with no close frame at all — so treat *any* transport
//! error as "reconnect and reattach", not as a failed run.
//!
//! [`ManagedBacktestSession`] does all of this for you: its control task
//! reconnects with backoff ([`managed::RECONNECT_MAX_ATTEMPTS`] attempts within
//! [`managed::RECONNECT_MAX_TOTAL`]) and re-handshakes with attach + resume. It
//! also defaults `disconnect_timeout_secs` to
//! [`managed::DEFAULT_DISCONNECT_TIMEOUT_SECS`] so the server holds the session
//! for at least as long as the client will keep trying.
//!
//! On the raw [`BacktestSession`] this is yours to implement: reconnect with
//! [`BacktestClient::attach_session`], passing the last sequence number you
//! observed, then [`BacktestSession::resume_attached_session`]. Set
//! `disconnect_timeout_secs` on the create request to a window that covers your
//! retry policy — the server's own default is deliberately short (tens of
//! seconds).
//!
//! ## Low-level lifecycle notes
//! These describe [`BacktestClient`] / [`BacktestSession`]; the managed layer
//! handles all of it internally.
//! - `BacktestClient::create_session` sends `CreateBacktestSession` and waits for
//! `SessionCreated`, buffering other responses until the session is ready.
//! - `BacktestSession::close` sends `CloseBacktestSession` and then closes the WebSocket.
//! - Dropping `BacktestSession` sends a best-effort WebSocket close frame if a Tokio runtime
//! is available. Call `close` explicitly to ensure server-side session cleanup.
//! - `connect_timeout` governs the initial handshake; `request_timeout` applies to send/receive
//! operations and can be overridden per call.
//! - For streaming workflows, use `BacktestSession::responses` (consumes the session) or
//! `next_event` to keep readiness state in sync while consuming responses.
//!
//! ## Subscribe options
//! [`SubscribeConfig`] carries the cross-cutting options every subscribe accepts
//! (`replayFromSlot`, `compression`, `intraSlotUpdates`); merge it into a raw
//! subscribe's params with [`SubscribeConfig::apply_to`]. This crate leaves every
//! field it doesn't itself need unset, so those follow the server's defaults —
//! notably `accountSubscribe` / `programSubscribe` deliver Agave-parity one
//! notification per account per slot unless you set `intra_slot_updates`.
//!
//! ```no_run
//! use simulator_client::SubscribeConfig;
//!
//! let mut params = serde_json::json!([ "<pubkey>", { "encoding": "base64" } ]);
//! SubscribeConfig {
//! intra_slot_updates: true,
//! ..SubscribeConfig::default()
//! }
//! .apply_to(&mut params);
//! ```
pub use ;
pub use BacktestClient;
pub use ;
pub use SessionGeyserChannel;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;