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
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use parking_lot::Mutex;
use tokio::sync::{broadcast, mpsc};
use tracing::warn;
use crate::jsonrpc::{JsonRpcNotification, JsonRpcRequest};
use crate::types::{SessionEventNotification, SessionId};
/// Identity of one specific registration of a session ID.
///
/// Session IDs are not unique over time: a caller can retry a cancelled
/// startup with the same pinned ID, and the retry replaces the previous
/// registration. Removal is therefore compare-and-remove against this
/// token, so a stale owner (an aborted startup future or a superseded
/// [`Session`](crate::session::Session)) can never unregister the live
/// registration that replaced it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct RegistrationToken(u64);
/// Per-session channels plus the identity of the registration that owns
/// them. Returned by [`SessionRouter::register`].
pub(crate) struct SessionRegistration {
pub(crate) channels: SessionChannels,
pub(crate) token: RegistrationToken,
}
/// Per-session channels created by the router during session registration.
pub(crate) struct SessionChannels {
/// Filtered `session.event` notifications for this session.
pub(crate) notifications: mpsc::UnboundedReceiver<SessionEventNotification>,
/// Filtered JSON-RPC requests (tool.call, userInput.request, etc.) for this session.
pub(crate) requests: mpsc::UnboundedReceiver<JsonRpcRequest>,
}
struct SessionSenders {
notifications: mpsc::UnboundedSender<SessionEventNotification>,
requests: mpsc::UnboundedSender<JsonRpcRequest>,
token: RegistrationToken,
}
/// Routes notifications and requests by sessionId to per-session channels.
///
/// Internal to the SDK — consumers interact via `Client::register_session()`.
pub(crate) struct SessionRouter {
sessions: Arc<Mutex<HashMap<SessionId, SessionSenders>>>,
next_token: AtomicU64,
started: Mutex<bool>,
}
impl SessionRouter {
pub(crate) fn new() -> Self {
Self {
sessions: Arc::new(Mutex::new(HashMap::new())),
next_token: AtomicU64::new(0),
started: Mutex::new(false),
}
}
/// Register a session to receive filtered events and requests.
///
/// Replaces any existing registration for the same ID and returns a
/// fresh [`RegistrationToken`] identifying this registration.
pub(crate) fn register(&self, session_id: &SessionId) -> SessionRegistration {
let (notif_tx, notif_rx) = mpsc::unbounded_channel();
let (req_tx, req_rx) = mpsc::unbounded_channel();
let token = RegistrationToken(self.next_token.fetch_add(1, Ordering::Relaxed));
self.sessions.lock().insert(
session_id.clone(),
SessionSenders {
notifications: notif_tx,
requests: req_tx,
token,
},
);
SessionRegistration {
channels: SessionChannels {
notifications: notif_rx,
requests: req_rx,
},
token,
}
}
/// Unregister a session, dropping its channels.
///
/// Unconditional: removes whichever registration currently holds the
/// ID. Only for client-wide teardown, where every session is going away
/// regardless of owner. Owners of a specific registration must use
/// [`unregister_owned`](Self::unregister_owned).
pub(crate) fn unregister(&self, session_id: &SessionId) {
self.sessions.lock().remove(session_id.as_str());
}
/// Unregister a session only if it is still the registration identified
/// by `token`.
///
/// Returns `true` when the entry was removed. A `false` result means
/// the registration had already been replaced by a newer one, which the
/// caller does not own and must leave alone.
pub(crate) fn unregister_owned(
&self,
session_id: &SessionId,
token: RegistrationToken,
) -> bool {
let mut sessions = self.sessions.lock();
if sessions
.get(session_id.as_str())
.is_some_and(|senders| senders.token == token)
{
sessions.remove(session_id.as_str());
true
} else {
false
}
}
/// Snapshot every currently-registered session ID.
///
/// Used by [`Client::stop`](crate::Client::stop) to iterate active
/// sessions for cooperative shutdown without holding the router lock
/// across `.await`.
pub(crate) fn session_ids(&self) -> Vec<SessionId> {
self.sessions.lock().keys().cloned().collect()
}
/// Count the currently-registered sessions without exposing their IDs.
#[cfg(any(test, feature = "test-support"))]
pub(crate) fn session_count(&self) -> usize {
self.sessions.lock().len()
}
/// Drop all registered session channels.
///
/// Used by [`Client::force_stop`](crate::Client::force_stop) to release
/// per-session state without waiting for graceful unregistration.
pub(crate) fn clear(&self) {
self.sessions.lock().clear();
}
/// Start the router tasks if not already running.
///
/// Takes the notification broadcast and request channel from the Client.
/// If `request_rx` is `None` (already taken by `take_request_rx()`),
/// only notification routing is available.
pub(crate) fn ensure_started(
&self,
notification_tx: &broadcast::Sender<JsonRpcNotification>,
request_rx: &Mutex<Option<mpsc::UnboundedReceiver<JsonRpcRequest>>>,
extension_launch_provider: Arc<
crate::extension_launch_provider::ExtensionLaunchProviderDispatcher,
>,
llm_inference: Option<Arc<crate::copilot_request_handler::CopilotRequestDispatcher>>,
github_telemetry: Option<crate::github_telemetry::GitHubTelemetryCallback>,
github_token_registry: Arc<crate::github_token::GitHubTokenRegistry>,
) {
let mut started = self.started.lock();
if *started {
return;
}
*started = true;
// Notification routing task
let sessions = self.sessions.clone();
let mut notif_rx = notification_tx.subscribe();
tokio::spawn(async move {
loop {
match notif_rx.recv().await {
Ok(notification) => {
// Client-global `gitHubTelemetry.event` notifications carry
// no routable session and are surfaced to the consumer
// callback (if any) registered at client construction.
if notification.method == "gitHubTelemetry.event" {
if let Some(ref callback) = github_telemetry {
let Some(ref params) = notification.params else {
continue;
};
match serde_json::from_value::<
crate::github_telemetry::GitHubTelemetryNotification,
>(params.clone())
{
Ok(telemetry) => {
if std::panic::catch_unwind(std::panic::AssertUnwindSafe(
|| callback(telemetry),
))
.is_err()
{
warn!(
"gitHubTelemetry.event callback panicked; \
continuing notification routing"
);
}
}
Err(e) => {
warn!(
error = %e,
"failed to deserialize gitHubTelemetry.event notification"
);
}
}
}
continue;
}
if notification.method != "session.event" {
continue;
}
let Some(ref params) = notification.params else {
continue;
};
let Some(session_id) = params.get("sessionId").and_then(|v| v.as_str())
else {
continue;
};
let sender = {
let guard = sessions.lock();
guard.get(session_id).map(|s| s.notifications.clone())
};
if let Some(sender) = sender {
match serde_json::from_value::<SessionEventNotification>(params.clone())
{
Ok(event_notification) => {
let _ = sender.send(event_notification);
}
Err(e) => {
warn!(
error = %e,
session_id = session_id,
"failed to deserialize session event notification"
);
}
}
}
// Unknown session IDs are silently dropped — the session
// may have been unregistered between dispatch and delivery.
}
Err(broadcast::error::RecvError::Lagged(n)) => {
warn!(missed = n, "notification router lagged");
}
Err(broadcast::error::RecvError::Closed) => break,
}
}
});
// Request routing task (if request_rx is available)
if let Some(mut rx) = request_rx.lock().take() {
let sessions = self.sessions.clone();
tokio::spawn(async move {
while let Some(request) = rx.recv().await {
if request.method == crate::extension_launch_provider::RESOLVE_METHOD {
extension_launch_provider.dispatch(request).await;
continue;
}
if request.method == "gitHubToken.getToken" {
github_token_registry.dispatch(request).await;
continue;
}
// Client-global `llmInference.*` requests carry no routable
// session and are handled by the inference dispatcher.
if request.method.starts_with("llmInference.") {
if let Some(dispatcher) = &llm_inference {
dispatcher.dispatch(request).await;
} else {
warn!(
method = %request.method,
"llmInference request with no provider registered"
);
}
continue;
}
let session_id = request
.params
.as_ref()
.and_then(|p| p.get("sessionId"))
.and_then(|v| v.as_str());
if let Some(sid) = session_id {
let sender = {
let guard = sessions.lock();
guard.get(sid).map(|s| s.requests.clone())
};
if let Some(sender) = sender {
let _ = sender.send(request);
} else {
warn!(
session_id = sid,
method = %request.method,
"request for unregistered session"
);
}
} else {
warn!(
method = %request.method,
"request missing sessionId"
);
}
}
});
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn session_id() -> SessionId {
SessionId::new("router-ownership")
}
#[test]
fn each_registration_gets_a_distinct_token() {
let router = SessionRouter::new();
let first = router.register(&session_id());
let second = router.register(&session_id());
assert_ne!(first.token, second.token);
}
#[test]
fn unregister_owned_removes_only_the_matching_registration() {
let router = SessionRouter::new();
let stale = router.register(&session_id());
let live = router.register(&session_id());
// The stale owner must not evict the registration that replaced it.
assert!(!router.unregister_owned(&session_id(), stale.token));
assert_eq!(router.session_ids(), vec![session_id()]);
assert!(router.unregister_owned(&session_id(), live.token));
assert!(router.session_ids().is_empty());
// Removing twice is a no-op rather than evicting a future tenant.
assert!(!router.unregister_owned(&session_id(), live.token));
}
}