Skip to main content

github_copilot_sdk/
session.rs

1use std::collections::HashMap;
2use std::path::{Path, PathBuf};
3use std::sync::Arc;
4use std::time::{Duration, Instant};
5
6use parking_lot::Mutex as ParkingLotMutex;
7use serde_json::Value;
8use tokio::sync::oneshot;
9use tokio::task::JoinHandle;
10use tokio_util::sync::CancellationToken;
11use tracing::{Instrument, warn};
12
13use crate::canvas::CanvasHandler;
14use crate::generated::api_types::{
15    LogRequest, ModelSwitchToRequest, OpenCanvasInstance, PermissionDecisionRequest,
16    RegisterEventInterestParams, ToolsGetCurrentMetadataResult, rpc_methods,
17};
18use crate::generated::session_events::{
19    CommandExecuteData, ElicitationRequestedData, ExternalToolRequestedData, McpOauthRequiredData,
20    SessionCanvasClosedData, SessionErrorData, SessionEventType, SessionIdleData, SessionMode,
21};
22use crate::handler::{
23    AutoModeSwitchHandler, AutoModeSwitchResponse, ElicitationHandler, ExitPlanModeHandler,
24    McpAuthHandler, McpAuthRequest, McpAuthResult, PermissionHandler, PermissionResult,
25    UserInputHandler, UserInputResponse,
26};
27use crate::hooks::SessionHooks;
28use crate::provider_token::BearerTokenProvider;
29use crate::session_fs::SessionFsProvider;
30use crate::trace_context::inject_trace_context;
31use crate::transforms::SystemMessageTransform;
32use crate::types::{
33    CommandContext, CommandDefinition, CommandHandler, CreateSessionResult, ElicitationRequest,
34    ElicitationResult, ExitPlanModeData, GetMessagesResponse, MessageOptions,
35    PermissionRequestData, RequestId, ResumeSessionConfig, ResumeSessionResult, SectionOverride,
36    SessionCapabilities, SessionConfig, SessionEvent, SessionId, SetModelOptions,
37    SystemMessageConfig, ToolInvocation, ToolResult, ToolResultExpanded, TraceContext,
38    UiInputOptions, ensure_attachment_display_names,
39};
40use crate::{
41    Client, Error, ErrorKind, JsonRpcResponse, SessionErrorKind, SessionEventNotification,
42    error_codes,
43};
44
45/// Fixed name of the runtime's built-in tool-search tool. A client can replace
46/// its behavior by registering a tool with this exact name and
47/// `overrides_built_in_tool` set to `true`.
48const TOOL_SEARCH_TOOL_NAME: &str = "tool_search_tool";
49
50/// Bundle of the per-session callbacks the SDK dispatches to. Built from a
51/// [`SessionConfig`] / [`ResumeSessionConfig`] at
52/// [`Client::create_session`] / [`Client::resume_session`] time. Each
53/// field is `None` (or an empty map for tools) when the caller didn't
54/// install a handler -- in that case the SDK skips dispatch for that
55/// event type. The wire flags on `session.create` / `session.resume`
56/// are derived from these fields.
57#[derive(Clone)]
58pub(crate) struct SessionHandlers {
59    pub permission: Option<Arc<dyn PermissionHandler>>,
60    pub managed_settings_enabled: bool,
61    pub elicitation: Option<Arc<dyn ElicitationHandler>>,
62    pub mcp_auth: Option<Arc<dyn McpAuthHandler>>,
63    pub user_input: Option<Arc<dyn UserInputHandler>>,
64    pub exit_plan_mode: Option<Arc<dyn ExitPlanModeHandler>>,
65    pub auto_mode_switch: Option<Arc<dyn AutoModeSwitchHandler>>,
66    pub tools: Arc<HashMap<String, Arc<dyn crate::tool::ToolHandler>>>,
67}
68
69fn has_managed_settings(
70    enable_managed_settings: Option<bool>,
71    managed_settings: Option<&crate::types::ManagedSettings>,
72) -> bool {
73    enable_managed_settings == Some(true) || managed_settings.is_some()
74}
75
76/// Shared state between a [`Session`] and its event loop, used by [`Session::send_and_wait`].
77struct IdleWaiter {
78    tx: oneshot::Sender<Result<Option<SessionEvent>, Error>>,
79    last_assistant_message: Option<SessionEvent>,
80    started_at: Instant,
81    first_assistant_message_seen: bool,
82}
83
84/// RAII guard that clears the [`Session::idle_waiter`] slot on drop. Used
85/// by [`Session::send_and_wait`] to ensure the slot doesn't leak if the
86/// caller's future is cancelled (outer `tokio::time::timeout` / `select!`
87/// / dropped JoinHandle). Synchronous clear via `parking_lot::Mutex` —
88/// no async drop needed.
89///
90/// Without this, an outer cancellation between "install waiter" and
91/// "drain channel" would leave the slot occupied, causing all subsequent
92/// `send` and `send_and_wait` calls on the session to return
93/// [`SendWhileWaiting`](SessionErrorKind::SendWhileWaiting). Closes RFD-400
94/// review finding #2.
95struct WaiterGuard {
96    slot: Arc<ParkingLotMutex<Option<IdleWaiter>>>,
97}
98
99impl Drop for WaiterGuard {
100    fn drop(&mut self) {
101        self.slot.lock().take();
102    }
103}
104
105struct PendingSessionRegistration {
106    client: Client,
107    session_id: SessionId,
108    shutdown: CancellationToken,
109    disarmed: bool,
110}
111
112impl PendingSessionRegistration {
113    fn new(client: Client, session_id: SessionId, shutdown: CancellationToken) -> Self {
114        Self {
115            client,
116            session_id,
117            shutdown,
118            disarmed: false,
119        }
120    }
121
122    async fn cleanup(mut self, event_loop: JoinHandle<()>) {
123        self.shutdown.cancel();
124        let _ = event_loop.await;
125        self.client.unregister_session(&self.session_id);
126        self.disarmed = true;
127    }
128
129    fn disarm(&mut self) {
130        self.disarmed = true;
131    }
132}
133
134impl Drop for PendingSessionRegistration {
135    fn drop(&mut self) {
136        if !self.disarmed {
137            self.shutdown.cancel();
138            self.client.unregister_session(&self.session_id);
139        }
140    }
141}
142
143/// A session on a GitHub Copilot CLI server.
144///
145/// Created via [`Client::create_session`] or [`Client::resume_session`].
146/// Owns an internal event loop that dispatches events to the per-callback
147/// handlers installed on the session config.
148///
149/// Protocol methods (`send`, `get_events`, `abort`, etc.) automatically
150/// inject the session ID into RPC params.
151///
152/// Call [`destroy`](Self::destroy) for graceful cleanup (RPC + local). If dropped
153/// without calling `destroy`, the `Drop` impl aborts the event loop and
154/// unregisters from the router as a best-effort safety net.
155pub struct Session {
156    id: SessionId,
157    cwd: PathBuf,
158    workspace_path: Option<PathBuf>,
159    remote_url: Option<String>,
160    client: Client,
161    /// Handle to the spawned event-loop task. Sync `parking_lot::Mutex`
162    /// because the lock is never held across an `.await` and the `Drop`
163    /// impl needs to take the handle synchronously without `try_lock`
164    /// fallibility.
165    event_loop: ParkingLotMutex<Option<JoinHandle<()>>>,
166    /// Cooperative shutdown signal for the event loop. The loop selects
167    /// on [`shutdown.cancelled()`](CancellationToken::cancelled) alongside
168    /// its inbound channels; [`Session::stop_event_loop`] and [`Drop`]
169    /// both call [`cancel()`](CancellationToken::cancel) to ask the loop
170    /// to exit between iterations rather than aborting the task (which
171    /// can land at any await point and leave the session mid-protocol).
172    /// See RFD-400 review finding #3.
173    ///
174    /// `CancellationToken` is the canonical signalling primitive in
175    /// `tokio_util`; it is what `tonic` uses for the equivalent task-
176    /// coordination case. Advanced consumers can obtain a child token
177    /// via [`Session::cancellation_token`] to bind their own work to
178    /// the session lifetime.
179    shutdown: CancellationToken,
180    /// Only populated while a `send_and_wait` call is in flight.
181    ///
182    /// Sync `parking_lot::Mutex` because the lock is never held across an
183    /// `.await`, and synchronous access lets the `WaiterGuard` RAII helper
184    /// in `send_and_wait` clear the slot from a `Drop` impl on caller-side
185    /// cancellation. See RFD-400 review (cancel-safety hardening).
186    idle_waiter: Arc<ParkingLotMutex<Option<IdleWaiter>>>,
187    /// Capabilities negotiated with the CLI, updated on `capabilities.changed` events.
188    capabilities: Arc<parking_lot::RwLock<SessionCapabilities>>,
189    /// Canvas instances currently known to be open for this session.
190    open_canvases: Arc<parking_lot::RwLock<Vec<OpenCanvasInstance>>>,
191    /// Broadcast channel for runtime event subscribers — see [`Session::subscribe`].
192    event_tx: tokio::sync::broadcast::Sender<SessionEvent>,
193    github_token_registration:
194        ParkingLotMutex<Option<crate::github_token::GitHubTokenRegistration>>,
195}
196
197impl Session {
198    /// Session ID assigned by the CLI.
199    pub fn id(&self) -> &SessionId {
200        &self.id
201    }
202
203    /// Working directory of the CLI process.
204    pub fn cwd(&self) -> &PathBuf {
205        &self.cwd
206    }
207
208    /// Workspace directory for the session (if using infinite sessions).
209    pub fn workspace_path(&self) -> Option<&Path> {
210        self.workspace_path.as_deref()
211    }
212
213    /// Remote session URL, if the session is running remotely.
214    pub fn remote_url(&self) -> Option<&str> {
215        self.remote_url.as_deref()
216    }
217
218    /// Session capabilities negotiated with the CLI.
219    ///
220    /// Capabilities are set during session creation and updated at runtime
221    /// via `capabilities.changed` events.
222    pub fn capabilities(&self) -> SessionCapabilities {
223        self.capabilities.read().clone()
224    }
225
226    /// Open canvas instances reported by the most recent `session.resume`
227    /// response or surfaced by inbound `canvas.opened` events.
228    pub fn open_canvases(&self) -> Vec<OpenCanvasInstance> {
229        self.open_canvases.read().clone()
230    }
231
232    /// Returns a [`CancellationToken`] that fires when this session shuts
233    /// down (via [`Session::stop_event_loop`], [`Session::destroy`], or
234    /// [`Drop`]).
235    ///
236    /// Use this to bind an external task's lifetime to the session — when
237    /// the session shuts down, awaiting [`cancelled()`](CancellationToken::cancelled)
238    /// resolves so cooperative consumers can stop cleanly.
239    ///
240    /// The returned handle is a *child* token: calling
241    /// [`cancel()`](CancellationToken::cancel) on it cancels only the
242    /// caller's child, not the session itself. To cancel the session, call
243    /// [`Session::stop_event_loop`].
244    ///
245    /// # Example
246    ///
247    /// ```no_run
248    /// # async fn example(session: github_copilot_sdk::session::Session) {
249    /// let token = session.cancellation_token();
250    /// tokio::select! {
251    ///     _ = token.cancelled() => println!("session shut down"),
252    ///     _ = tokio::time::sleep(std::time::Duration::from_secs(60)) => {
253    ///         println!("60s elapsed, session still alive");
254    ///     }
255    /// }
256    /// # }
257    /// ```
258    pub fn cancellation_token(&self) -> CancellationToken {
259        self.shutdown.child_token()
260    }
261
262    /// Subscribe to events for this session.
263    ///
264    /// Returns an [`EventSubscription`](crate::subscription::EventSubscription)
265    /// that yields every [`SessionEvent`] dispatched on this session's
266    /// event loop. Drop the value to unsubscribe; there is no separate
267    /// cancel handle.
268    ///
269    /// **Observe-only.** Subscribers receive a clone of every
270    /// [`SessionEvent`] but cannot influence permission decisions, tool
271    /// results, or anything else that requires returning a value. Those
272    /// remain the responsibility of the per-callback handlers passed via
273    /// [`SessionConfig`]'s `with_*_handler`
274    /// builder methods.
275    ///
276    /// The returned handle implements both an inherent
277    /// [`recv`](crate::subscription::EventSubscription::recv) method and
278    /// [`Stream`](tokio_stream::Stream), so callers can use a `while let`
279    /// loop or any combinator from `tokio_stream::StreamExt` /
280    /// `futures::StreamExt`.
281    ///
282    /// Each subscriber maintains its own queue. If a consumer cannot keep
283    /// up, the oldest events are dropped and `recv` returns
284    /// [`RecvErrorKind::Lagged`](crate::subscription::RecvErrorKind::Lagged)
285    /// reporting the count of skipped events. Slow consumers do not block
286    /// the session's event loop.
287    ///
288    /// # Example
289    ///
290    /// ```no_run
291    /// # async fn example(session: github_copilot_sdk::session::Session) {
292    /// let mut events = session.subscribe();
293    /// tokio::spawn(async move {
294    ///     while let Ok(event) = events.recv().await {
295    ///         println!("[{}] event {}", event.id, event.event_type);
296    ///     }
297    /// });
298    /// # }
299    /// ```
300    pub fn subscribe(&self) -> crate::subscription::EventSubscription {
301        crate::subscription::EventSubscription::new(self.event_tx.subscribe())
302    }
303
304    /// The underlying Client (for advanced use cases).
305    pub fn client(&self) -> &Client {
306        &self.client
307    }
308
309    /// Typed RPC namespace for this session.
310    ///
311    /// Every protocol method lives here under its schema-aligned path —
312    /// e.g. `session.rpc().workspaces().list_files()`. Wire method names
313    /// and request/response types are generated from the protocol schema,
314    /// so the typed namespace can't drift from the wire contract.
315    ///
316    /// The hand-authored helpers on [`Session`] delegate to this namespace
317    /// and remain the recommended entry point for everyday use; reach for
318    /// `rpc()` when you want a method without a hand-written wrapper.
319    pub fn rpc(&self) -> crate::generated::rpc::SessionRpc<'_> {
320        crate::generated::rpc::SessionRpc { session: self }
321    }
322
323    /// Stop the internal event loop. Called automatically on [`destroy`](Self::destroy).
324    ///
325    /// Cooperative: signals shutdown via the session's [`CancellationToken`]
326    /// and awaits the loop's natural exit rather than aborting the task.
327    /// Any in-flight handler (permission callback, tool call, elicitation
328    /// response) completes before the loop exits, so the CLI never sees a
329    /// half-handled request. See RFD-400 review finding #3.
330    pub async fn stop_event_loop(&self) {
331        self.shutdown.cancel();
332        let handle = self.event_loop.lock().take();
333        if let Some(handle) = handle {
334            let _ = handle.await;
335        }
336        // Fail any pending send_and_wait so it returns immediately.
337        if let Some(waiter) = self.idle_waiter.lock().take() {
338            let _ = waiter.tx.send(Err(
339                ErrorKind::Session(SessionErrorKind::EventLoopClosed).into()
340            ));
341        }
342    }
343
344    /// Send a user message to the agent.
345    ///
346    /// Accepts anything convertible to [`MessageOptions`] — pass a `&str` for the
347    /// trivial case, or build a `MessageOptions` for mode/attachments. The
348    /// `wait_timeout` field on `MessageOptions` is ignored here (use
349    /// [`send_and_wait`](Self::send_and_wait) if you need to wait).
350    ///
351    /// Returns the assigned message ID, which can be used to correlate the
352    /// send with later [`SessionEvent`]s emitted in
353    /// response (assistant messages, tool requests, etc.).
354    ///
355    /// Returns an error if a [`send_and_wait`](Self::send_and_wait) call is
356    /// currently in flight, since the plain send would race with the waiter.
357    ///
358    /// # Cancel safety
359    ///
360    /// **Cancel-safe.** The underlying `session.send` RPC is dispatched
361    /// through the writer-actor (see [`Client::call`](crate::Client::call)),
362    /// so dropping this future after the actor has committed to writing
363    /// will not produce a partial frame on the wire. If the caller's
364    /// future is dropped between "frame enqueued" and "response received",
365    /// the message has already landed on the wire — the agent will process
366    /// it and emit events normally; the caller just won't see the returned
367    /// message ID.
368    pub async fn send(&self, opts: impl Into<MessageOptions>) -> Result<String, Error> {
369        if self.idle_waiter.lock().is_some() {
370            return Err(ErrorKind::Session(SessionErrorKind::SendWhileWaiting).into());
371        }
372        self.send_inner(opts.into()).await
373    }
374
375    async fn send_inner(&self, opts: MessageOptions) -> Result<String, Error> {
376        let mut params = serde_json::json!({
377            "sessionId": self.id,
378            "prompt": opts.prompt,
379        });
380        if let Some(m) = opts.mode {
381            params["mode"] = serde_json::to_value(m)?;
382        }
383        if let Some(am) = opts.agent_mode {
384            params["agentMode"] = serde_json::to_value(am)?;
385        }
386        if let Some(mut a) = opts.attachments {
387            ensure_attachment_display_names(&mut a);
388            params["attachments"] = serde_json::to_value(a)?;
389        }
390        if let Some(headers) = opts.request_headers
391            && !headers.is_empty()
392        {
393            params["requestHeaders"] = serde_json::to_value(headers)?;
394        }
395        if let Some(display_prompt) = opts.display_prompt {
396            params["displayPrompt"] = serde_json::to_value(display_prompt)?;
397        }
398        let trace_ctx = if opts.traceparent.is_some() || opts.tracestate.is_some() {
399            TraceContext {
400                traceparent: opts.traceparent,
401                tracestate: opts.tracestate,
402            }
403        } else {
404            self.client.resolve_trace_context().await
405        };
406        inject_trace_context(&mut params, &trace_ctx);
407        let rpc_start = Instant::now();
408        let result = self.client.call("session.send", Some(params)).await?;
409        let message_id = result
410            .get("messageId")
411            .and_then(|v| v.as_str())
412            .map(|s| s.to_string())
413            .unwrap_or_default();
414        tracing::debug!(
415            elapsed_ms = rpc_start.elapsed().as_millis(),
416            session_id = %self.id,
417            message_id = %message_id,
418            "Session::send completed successfully"
419        );
420        Ok(message_id)
421    }
422
423    /// Send a user message and wait for the agent to finish processing.
424    ///
425    /// Accepts anything convertible to [`MessageOptions`] — pass a `&str` for the
426    /// trivial case, or build a `MessageOptions` for mode/attachments/timeout.
427    /// Blocks until `session.idle` (success) or `session.error` (failure),
428    /// returning the last `assistant.message` event captured during streaming.
429    /// Times out after `MessageOptions::wait_timeout` (default 60 seconds).
430    ///
431    /// Only one `send_and_wait` call may be active per session at a time.
432    /// Calling [`send`](Self::send) while a `send_and_wait`
433    /// is in flight will also return an error.
434    ///
435    /// # Cancel safety
436    ///
437    /// **Cancel-safe.** A `WaiterGuard` clears the in-flight slot on every
438    /// exit path (success, internal failure, internal timeout, *and*
439    /// external cancellation via `tokio::time::timeout` / `select!` /
440    /// dropped JoinHandle). Subsequent `send` and `send_and_wait` calls on
441    /// this session will succeed normally — the slot is never leaked.
442    pub async fn send_and_wait(
443        &self,
444        opts: impl Into<MessageOptions>,
445    ) -> Result<Option<SessionEvent>, Error> {
446        let total_start = Instant::now();
447        let opts = opts.into();
448        let timeout_duration = opts.wait_timeout.unwrap_or(Duration::from_secs(60));
449        let (tx, rx) = oneshot::channel();
450
451        {
452            let mut guard = self.idle_waiter.lock();
453            if guard.is_some() {
454                return Err(ErrorKind::Session(SessionErrorKind::SendWhileWaiting).into());
455            }
456            *guard = Some(IdleWaiter {
457                tx,
458                last_assistant_message: None,
459                started_at: total_start,
460                first_assistant_message_seen: false,
461            });
462        }
463
464        // RAII: clears the idle_waiter slot on every exit path, including
465        // external cancellation (caller's outer `select!` / `timeout` /
466        // dropped future). Without this, an outer cancellation would leak
467        // the slot and brick subsequent `send`/`send_and_wait` calls.
468        let _waiter_guard = WaiterGuard {
469            slot: self.idle_waiter.clone(),
470        };
471
472        let result = tokio::time::timeout(timeout_duration, async {
473            self.send_inner(opts).await?;
474            match rx.await {
475                Ok(result) => result,
476                Err(_) => Err(ErrorKind::Session(SessionErrorKind::EventLoopClosed).into()),
477            }
478        })
479        .await;
480
481        match result {
482            Ok(inner) => {
483                tracing::debug!(
484                    elapsed_ms = total_start.elapsed().as_millis(),
485                    session_id = %self.id,
486                    completed_by = if inner.is_ok() { "idle" } else { "error" },
487                    "Session::send_and_wait complete"
488                );
489                inner
490            }
491            Err(_) => {
492                tracing::warn!(
493                    elapsed_ms = total_start.elapsed().as_millis(),
494                    session_id = %self.id,
495                    completed_by = "timeout",
496                    "Session::send_and_wait failed"
497                );
498                Err(ErrorKind::Session(SessionErrorKind::Timeout(timeout_duration)).into())
499            }
500        }
501    }
502
503    /// Retrieve the session's timeline events.
504    pub async fn get_events(&self) -> Result<Vec<SessionEvent>, Error> {
505        let result = self
506            .client
507            .call(
508                "session.getMessages",
509                Some(serde_json::json!({ "sessionId": self.id })),
510            )
511            .await?;
512        let response: GetMessagesResponse = serde_json::from_value(result)?;
513        Ok(response.events)
514    }
515
516    /// Deprecated alias for [`get_events`](Self::get_events).
517    #[deprecated(since = "0.1.0", note = "Use `get_events()` instead")]
518    pub async fn get_messages(&self) -> Result<Vec<SessionEvent>, Error> {
519        self.get_events().await
520    }
521
522    /// Abort the current agent turn.
523    ///
524    /// # Cancel safety
525    ///
526    /// **Cancel-safe.** Single `session.abort` RPC; the underlying
527    /// [`Client::call`](crate::Client::call) is cancel-safe via the
528    /// writer-actor.
529    pub async fn abort(&self) -> Result<(), Error> {
530        self.client
531            .call(
532                "session.abort",
533                Some(serde_json::json!({ "sessionId": self.id })),
534            )
535            .await?;
536        Ok(())
537    }
538
539    /// Switch to a different model.
540    ///
541    /// Pass `None` for `opts` if no extra configuration is needed.
542    pub async fn set_model(&self, model: &str, opts: Option<SetModelOptions>) -> Result<(), Error> {
543        let opts = opts.unwrap_or_default();
544        let request = ModelSwitchToRequest {
545            compaction_decision: None,
546            context_tier: opts.context_tier,
547            defer_if_model_change_queued: None,
548            model_capabilities: opts.model_capabilities,
549            model_change_scope: None,
550            model_id: model.to_string(),
551            picker_persistence: None,
552            reasoning_effort: opts.reasoning_effort,
553            reasoning_summary: opts.reasoning_summary,
554            repo_scope: None,
555            require_available: None,
556            run_compaction_preflight: None,
557            source: None,
558            verbosity: None,
559        };
560        self.rpc().model().switch_to(request).await?;
561        Ok(())
562    }
563
564    /// Disconnect this session from the CLI.
565    ///
566    /// Sends the `session.destroy` RPC, stops the event loop, and unregisters
567    /// the session from the client. **Session state on disk** (conversation
568    /// history, planning state, artifacts) is **preserved**, so the
569    /// conversation can be resumed later via [`Client::resume_session`]
570    /// using this session's ID. To permanently remove all on-disk session
571    /// data, use [`Client::delete_session`] instead.
572    ///
573    /// The caller should ensure the session is idle (e.g. [`send_and_wait`]
574    /// has returned) before disconnecting; in-flight tool or event handlers
575    /// may otherwise observe failures.
576    ///
577    /// [`Client::resume_session`]: crate::Client::resume_session
578    /// [`Client::delete_session`]: crate::Client::delete_session
579    /// [`send_and_wait`]: Self::send_and_wait
580    pub async fn disconnect(&self) -> Result<(), Error> {
581        self.client
582            .call(
583                "session.destroy",
584                Some(serde_json::json!({ "sessionId": self.id })),
585            )
586            .await?;
587        self.stop_event_loop().await;
588        self.client.unregister_session(&self.id);
589        self.github_token_registration.lock().take();
590        Ok(())
591    }
592
593    /// Deprecated alias for [`disconnect`](Self::disconnect). The
594    /// underlying wire RPC happens to be named `session.destroy`, but it
595    /// only severs the connection — on-disk session state is preserved.
596    /// Prefer `disconnect` in new code.
597    #[deprecated(since = "0.1.0", note = "Use `disconnect()` instead")]
598    pub async fn destroy(&self) -> Result<(), Error> {
599        self.disconnect().await
600    }
601
602    /// Write a log message to the session.
603    ///
604    /// Pass `None` for `opts` to use defaults (info level, persisted).
605    pub async fn log(
606        &self,
607        message: &str,
608        opts: Option<crate::types::LogOptions>,
609    ) -> Result<(), Error> {
610        let opts = opts.unwrap_or_default();
611        let level = match opts.level {
612            Some(level) => Some(serde_json::from_value(serde_json::to_value(level)?)?),
613            None => None,
614        };
615        let request = LogRequest {
616            message: message.to_string(),
617            level,
618            ephemeral: opts.ephemeral,
619            r#type: None,
620            tip: None,
621            url: None,
622        };
623        self.rpc().log(request).await?;
624        Ok(())
625    }
626
627    /// Returns the UI sub-API for elicitation, confirmation, selection, and
628    /// free-form input.
629    ///
630    /// All UI methods route through `session.ui.*` RPCs and require host
631    /// support — check `session.capabilities().ui.elicitation` before use.
632    pub fn ui(&self) -> SessionUi<'_> {
633        SessionUi { session: self }
634    }
635
636    /// Returns an error if the host doesn't support elicitation.
637    fn assert_elicitation(&self) -> Result<(), Error> {
638        if self
639            .capabilities
640            .read()
641            .ui
642            .as_ref()
643            .and_then(|u| u.elicitation)
644            != Some(true)
645        {
646            return Err(ErrorKind::Session(SessionErrorKind::ElicitationNotSupported).into());
647        }
648        Ok(())
649    }
650}
651
652impl Drop for Session {
653    fn drop(&mut self) {
654        // Cooperative shutdown: cancel the event loop's token to signal
655        // exit between iterations. The loop will see the cancellation on
656        // its next select poll and break cleanly without interrupting an
657        // in-flight handler. We do NOT abort the JoinHandle — that would
658        // land at any await point in the loop body, potentially leaving
659        // the CLI with an unanswered request id. RFD-400 review finding
660        // #3.
661        //
662        // The handle itself is left in `event_loop` to be reaped by the
663        // tokio runtime when it next polls; we intentionally don't await
664        // it here because Drop is sync.
665        self.shutdown.cancel();
666        self.client.unregister_session(&self.id);
667        self.github_token_registration.lock().take();
668    }
669}
670
671/// UI sub-API for a [`Session`] — elicitation, confirmation, selection,
672/// and free-form input.
673///
674/// Acquired via [`Session::ui`]. Methods route to `session.ui.*` RPCs and
675/// require host elicitation support — check
676/// `session.capabilities().ui.elicitation` before use.
677pub struct SessionUi<'a> {
678    session: &'a Session,
679}
680
681impl<'a> SessionUi<'a> {
682    /// Request user input via an interactive UI form (elicitation).
683    ///
684    /// Sends a JSON Schema describing form fields to the CLI host. The host
685    /// renders a form dialog and returns the user's response.
686    ///
687    /// Prefer the typed convenience methods [`confirm`](Self::confirm),
688    /// [`select`](Self::select), and [`input`](Self::input) for common cases.
689    pub async fn elicitation(
690        &self,
691        message: &str,
692        schema: Value,
693    ) -> Result<ElicitationResult, Error> {
694        self.session.assert_elicitation()?;
695        let result = self
696            .session
697            .client
698            .call(
699                "session.ui.elicitation",
700                Some(serde_json::json!({
701                    "sessionId": self.session.id,
702                    "message": message,
703                    "requestedSchema": schema,
704                })),
705            )
706            .await?;
707        let elicitation: ElicitationResult = serde_json::from_value(result)?;
708        Ok(elicitation)
709    }
710
711    /// Ask the user a yes/no confirmation question.
712    ///
713    /// Returns `true` if the user accepted and confirmed, `false` otherwise.
714    pub async fn confirm(&self, message: &str) -> Result<bool, Error> {
715        self.session.assert_elicitation()?;
716        let schema = serde_json::json!({
717            "type": "object",
718            "properties": {
719                "confirmed": {
720                    "type": "boolean",
721                    "default": true,
722                }
723            },
724            "required": ["confirmed"]
725        });
726        let result = self.elicitation(message, schema).await?;
727        Ok(result.action == "accept"
728            && result
729                .content
730                .and_then(|c| c.get("confirmed").and_then(|v| v.as_bool()))
731                == Some(true))
732    }
733
734    /// Ask the user to select from a list of options.
735    ///
736    /// Returns the selected option string on accept, or `None` on decline/cancel.
737    pub async fn select(&self, message: &str, options: &[&str]) -> Result<Option<String>, Error> {
738        self.session.assert_elicitation()?;
739        let schema = serde_json::json!({
740            "type": "object",
741            "properties": {
742                "selection": {
743                    "type": "string",
744                    "enum": options,
745                }
746            },
747            "required": ["selection"]
748        });
749        let result = self.elicitation(message, schema).await?;
750        if result.action != "accept" {
751            return Ok(None);
752        }
753        let selection = result.content.and_then(|c| {
754            c.get("selection")
755                .and_then(|v| v.as_str())
756                .map(String::from)
757        });
758        Ok(selection)
759    }
760
761    /// Ask the user for free-form text input.
762    ///
763    /// Returns the input string on accept, or `None` on decline/cancel.
764    /// Use [`UiInputOptions`] to set validation constraints and field metadata.
765    pub async fn input(
766        &self,
767        message: &str,
768        options: Option<&UiInputOptions<'_>>,
769    ) -> Result<Option<String>, Error> {
770        self.session.assert_elicitation()?;
771        let mut field = serde_json::json!({ "type": "string" });
772        if let Some(opts) = options {
773            if let Some(title) = opts.title {
774                field["title"] = Value::String(title.to_string());
775            }
776            if let Some(desc) = opts.description {
777                field["description"] = Value::String(desc.to_string());
778            }
779            if let Some(min) = opts.min_length {
780                field["minLength"] = Value::Number(min.into());
781            }
782            if let Some(max) = opts.max_length {
783                field["maxLength"] = Value::Number(max.into());
784            }
785            if let Some(fmt) = &opts.format {
786                field["format"] = Value::String(fmt.as_str().to_string());
787            }
788            if let Some(default) = opts.default {
789                field["default"] = Value::String(default.to_string());
790            }
791        }
792        let schema = serde_json::json!({
793            "type": "object",
794            "properties": { "value": field },
795            "required": ["value"]
796        });
797        let result = self.elicitation(message, schema).await?;
798        if result.action != "accept" {
799            return Ok(None);
800        }
801        let value = result
802            .content
803            .and_then(|c| c.get("value").and_then(|v| v.as_str()).map(String::from));
804        Ok(value)
805    }
806}
807
808impl Client {
809    /// Create a new session on the CLI.
810    ///
811    /// Sends `session.create`, registers the session on the router,
812    /// and spawns an internal event loop that dispatches to the handler.
813    ///
814    /// All callbacks (per-event handlers, tool handlers, hooks, transform)
815    /// are configured via [`SessionConfig`] using its `with_*_handler` /
816    /// `with_tools` / `with_hooks` / `with_system_message_transform` builder
817    /// methods.
818    ///
819    /// If [`hooks_handler`](SessionConfig::hooks_handler) is set, the
820    /// wire-level `hooks` flag is automatically enabled.
821    ///
822    /// If [`system_message_transform`](SessionConfig::system_message_transform) is set, the SDK injects
823    /// `action: "transform"` sections into the [`SystemMessageConfig`] wire
824    /// format and handles `systemMessage.transform` RPC callbacks during
825    /// the session.
826    ///
827    /// Each per-event handler is independently optional. If a handler is
828    /// not installed, the SDK signals the runtime not to emit the matching
829    /// broadcast (and silently skips dispatch if one arrives anyway).
830    pub async fn create_session(&self, mut config: SessionConfig) -> Result<Session, Error> {
831        let total_start = Instant::now();
832        // For cloud sessions, let the CLI/server assign the session id and
833        // register the session lazily once the response arrives. For non-cloud
834        // sessions we generate the id client-side (when the caller didn't
835        // supply one) so the session can be registered BEFORE the RPC — the
836        // CLI may issue session-scoped requests (e.g. sessionFs.writeFile for
837        // workspace metadata) during session.create processing, before it has
838        // sent the response.
839        let caller_session_id = config.session_id.clone();
840        let use_server_generated_id = config.cloud.is_some() && caller_session_id.is_none();
841        let local_session_id: Option<SessionId> = if use_server_generated_id {
842            None
843        } else {
844            Some(
845                caller_session_id
846                    .clone()
847                    .unwrap_or_else(|| SessionId::new(uuid::Uuid::new_v4().to_string())),
848            )
849        };
850        if config.hooks_handler.is_some() && config.hooks.is_none() {
851            config.hooks = Some(true);
852        }
853        if let Some(transforms) = config.system_message_transform.clone() {
854            inject_transform_sections(&mut config, transforms.as_ref());
855        }
856        let mode = self.inner.mode;
857        if mode == crate::ClientMode::Empty && config.available_tools.is_none() {
858            return Err(Error::with_message(
859                ErrorKind::InvalidConfig,
860                "ClientMode::Empty requires available_tools to be set on the session config. \
861                 Use ToolSet to specify which tools the session may use (e.g. \
862                 ToolSet::new().add_builtin_many(BUILTIN_TOOLS_ISOLATED)).",
863            ));
864        }
865        crate::mode::validate_tool_filter_list(
866            "available_tools",
867            config.available_tools.as_deref(),
868        )?;
869        crate::mode::validate_tool_filter_list("excluded_tools", config.excluded_tools.as_deref())?;
870        config.system_message =
871            crate::mode::system_message_for_mode(mode, config.system_message.take());
872        config.memory = crate::mode::memory_for_mode(mode, config.memory.take());
873        config.enable_experimental_mode =
874            crate::mode::experimental_mode_for_mode(mode, config.enable_experimental_mode);
875        if mode == crate::ClientMode::Empty {
876            if config.enable_session_telemetry.is_none() {
877                config.enable_session_telemetry = Some(false);
878            }
879            if config.skip_embedding_retrieval.is_none() {
880                config.skip_embedding_retrieval = Some(true);
881            }
882            if config.enable_on_demand_instruction_discovery.is_none() {
883                config.enable_on_demand_instruction_discovery = Some(false);
884            }
885            if config.enable_file_hooks.is_none() {
886                config.enable_file_hooks = Some(false);
887            }
888            if config.enable_host_git_operations.is_none() {
889                config.enable_host_git_operations = Some(false);
890            }
891            if config.enable_session_store.is_none() {
892                config.enable_session_store = Some(false);
893            }
894            if config.enable_skills.is_none() {
895                config.enable_skills = Some(false);
896            }
897        }
898        if mode == crate::ClientMode::Empty && config.mcp_oauth_token_storage.is_none() {
899            config.mcp_oauth_token_storage = Some("in-memory".into());
900        }
901        if mode == crate::ClientMode::Empty && config.embedding_cache_storage.is_none() {
902            config.embedding_cache_storage = Some("in-memory".into());
903        }
904        config.custom_agents_local_only =
905            crate::mode::resolve_custom_agents_local_only(mode, config.custom_agents_local_only);
906        let opt_skip_custom_instructions = config.skip_custom_instructions;
907        let opt_custom_agents_local_only = config.custom_agents_local_only;
908        let opt_coauthor_enabled = config.coauthor_enabled;
909        let opt_manage_schedule_enabled = config.manage_schedule_enabled;
910        let opt_included_builtin_skills = config.included_builtin_skills.take();
911        let (mut wire, mut runtime) = config.into_wire(local_session_id.clone())?;
912        wire.enable_github_telemetry_forwarding =
913            self.inner.on_github_telemetry.is_some().then_some(true);
914
915        let permission_handler = crate::permission::resolve_handler(
916            runtime.permission_handler.take(),
917            runtime.permission_policy.take(),
918        );
919        let handlers = SessionHandlers {
920            permission: permission_handler,
921            managed_settings_enabled: has_managed_settings(
922                wire.enable_managed_settings,
923                wire.managed_settings.as_ref(),
924            ),
925            elicitation: runtime.elicitation_handler.take(),
926            mcp_auth: runtime.mcp_auth_handler.take(),
927            user_input: runtime.user_input_handler.take(),
928            exit_plan_mode: runtime.exit_plan_mode_handler.take(),
929            auto_mode_switch: runtime.auto_mode_switch_handler.take(),
930            tools: Arc::new(std::mem::take(&mut runtime.tool_handlers)),
931        };
932        let hooks = runtime.hooks_handler.take();
933        let transforms = runtime.system_message_transform.take();
934        let tools_count = wire.tools.as_ref().map_or(0, Vec::len);
935        let commands_count = runtime.commands.as_ref().map_or(0, Vec::len);
936        let has_hooks = hooks.is_some();
937        let command_handlers = build_command_handler_map(runtime.commands.as_deref());
938        let canvas_handler = runtime.canvas_handler.take();
939        let session_fs_provider = runtime.session_fs_provider.take();
940        let bearer_token_providers = std::mem::take(&mut runtime.bearer_token_providers);
941        let github_token_registration = runtime
942            .github_token_provider
943            .take()
944            .map(|provider| self.register_github_token_provider(provider));
945        wire.github_token_provider_registration_id = github_token_registration
946            .as_ref()
947            .map(|registration| registration.id().to_string());
948        let has_mcp_auth_handler = handlers.mcp_auth.is_some();
949        if self.inner.session_fs_configured && session_fs_provider.is_none() {
950            return Err(ErrorKind::Session(SessionErrorKind::SessionFsProviderRequired).into());
951        }
952        if self.inner.session_fs_sqlite_declared
953            && let Some(ref provider) = session_fs_provider
954            && provider.sqlite().is_none()
955        {
956            return Err(Error::with_message(
957                ErrorKind::InvalidConfig,
958                "SessionFs capabilities declare SQLite support but the provider \
959                 does not implement SessionFsSqliteProvider",
960            ));
961        }
962
963        let mut params = serde_json::to_value(&wire)?;
964        let trace_ctx = self.resolve_trace_context().await;
965        inject_trace_context(&mut params, &trace_ctx);
966
967        let setup_start = Instant::now();
968        let capabilities = Arc::new(parking_lot::RwLock::new(SessionCapabilities::default()));
969        let idle_waiter = Arc::new(ParkingLotMutex::new(None));
970        let open_canvases = Arc::new(parking_lot::RwLock::new(Vec::new()));
971        let shutdown = CancellationToken::new();
972        let (event_tx, _) = tokio::sync::broadcast::channel(512);
973
974        // For cloud sessions (use_server_generated_id), defer session
975        // registration to the inline callback so the read task registers
976        // the session synchronously the instant the response arrives.
977        // For non-cloud sessions, register up-front so the CLI can issue
978        // session-scoped requests during session.create processing.
979        let inline_stash: Arc<
980            ParkingLotMutex<Option<(SessionId, crate::router::SessionChannels)>>,
981        > = Arc::new(ParkingLotMutex::new(None));
982
983        let inline_callback: Option<crate::jsonrpc::InlineResponseCallback> = if let Some(ref sid) =
984            local_session_id
985        {
986            let channels = self.register_session(sid);
987            *inline_stash.lock() = Some((sid.clone(), channels));
988            None
989        } else {
990            let client = self.clone();
991            let stash = inline_stash.clone();
992            let expected = caller_session_id.clone();
993            Some(Box::new(move |response| {
994                let result = response.result.as_ref().ok_or_else(|| {
995                    Error::with_message(ErrorKind::Json, "session.create response had no result")
996                })?;
997                let parsed: CreateSessionResult =
998                    serde_json::from_value(result.clone()).map_err(Error::from)?;
999                if let Some(requested) = expected.as_ref()
1000                    && parsed.session_id != *requested
1001                {
1002                    return Err(ErrorKind::Session(SessionErrorKind::SessionIdMismatch {
1003                        requested: requested.clone(),
1004                        returned: parsed.session_id,
1005                    })
1006                    .into());
1007                }
1008                let channels = client.register_session(&parsed.session_id);
1009                *stash.lock() = Some((parsed.session_id, channels));
1010                Ok(())
1011            }))
1012        };
1013
1014        let rpc_start = Instant::now();
1015        let result = match self
1016            .call_with_inline_callback("session.create", Some(params), inline_callback)
1017            .await
1018        {
1019            Ok(result) => result,
1020            Err(error) => {
1021                if let Some((id, _channels)) = inline_stash.lock().take() {
1022                    self.unregister_session(&id);
1023                }
1024                return Err(error);
1025            }
1026        };
1027        tracing::debug!(
1028            elapsed_ms = rpc_start.elapsed().as_millis(),
1029            "Client::create_session session creation request completed successfully"
1030        );
1031        let create_result: CreateSessionResult = match serde_json::from_value(result) {
1032            Ok(result) => result,
1033            Err(error) => {
1034                if let Some((id, _channels)) = inline_stash.lock().take() {
1035                    self.unregister_session(&id);
1036                }
1037                return Err(error.into());
1038            }
1039        };
1040
1041        if let Some(ref requested) = local_session_id
1042            && create_result.session_id != *requested
1043        {
1044            if let Some((id, _channels)) = inline_stash.lock().take() {
1045                self.unregister_session(&id);
1046            }
1047            return Err(ErrorKind::Session(SessionErrorKind::SessionIdMismatch {
1048                requested: requested.clone(),
1049                returned: create_result.session_id.clone(),
1050            })
1051            .into());
1052        }
1053
1054        let (session_id, channels) = inline_stash
1055            .lock()
1056            .take()
1057            .expect("session registration must have populated stash on success");
1058        let event_loop = spawn_event_loop(
1059            session_id.clone(),
1060            self.clone(),
1061            handlers,
1062            hooks,
1063            transforms,
1064            command_handlers,
1065            canvas_handler,
1066            session_fs_provider,
1067            bearer_token_providers,
1068            channels,
1069            idle_waiter.clone(),
1070            capabilities.clone(),
1071            open_canvases.clone(),
1072            event_tx.clone(),
1073            shutdown.clone(),
1074        );
1075        tracing::debug!(
1076            elapsed_ms = setup_start.elapsed().as_millis(),
1077            session_id = %session_id,
1078            tools_count,
1079            commands_count,
1080            has_hooks,
1081            "Client::create_session local setup complete"
1082        );
1083        *capabilities.write() = create_result.capabilities.unwrap_or_default();
1084        if has_mcp_auth_handler {
1085            register_mcp_auth_interest(self, &session_id).await?;
1086        }
1087
1088        tracing::debug!(
1089            elapsed_ms = total_start.elapsed().as_millis(),
1090            session_id = %session_id,
1091            "Client::create_session complete"
1092        );
1093        let session = Session {
1094            id: session_id,
1095            cwd: self.cwd().clone(),
1096            workspace_path: create_result.workspace_path,
1097            remote_url: create_result.remote_url,
1098            client: self.clone(),
1099            event_loop: ParkingLotMutex::new(Some(event_loop)),
1100            shutdown,
1101            idle_waiter,
1102            capabilities,
1103            open_canvases,
1104            event_tx,
1105            github_token_registration: ParkingLotMutex::new(github_token_registration),
1106        };
1107        apply_mode_post_create_patch(
1108            &session,
1109            mode,
1110            opt_skip_custom_instructions,
1111            opt_custom_agents_local_only,
1112            opt_coauthor_enabled,
1113            opt_manage_schedule_enabled,
1114            opt_included_builtin_skills,
1115        )
1116        .await?;
1117        if let Some(registration) = session.github_token_registration.lock().as_ref() {
1118            registration.claim(session.id.clone());
1119        } else {
1120            self.retire_github_token_provider(&session.id);
1121        }
1122        Ok(session)
1123    }
1124
1125    /// Resume an existing session on the CLI.
1126    ///
1127    /// Sends `session.resume` and `session.skills.reload`, registers the
1128    /// session on the router, and spawns the event loop.
1129    ///
1130    /// All callbacks (event handler, hooks, transform) are configured
1131    /// via [`ResumeSessionConfig`] using its `with_*` builder methods.
1132    ///
1133    /// See [`Self::create_session`] for the defaults applied when callback
1134    /// fields are unset.
1135    pub async fn resume_session(&self, mut config: ResumeSessionConfig) -> Result<Session, Error> {
1136        let total_start = Instant::now();
1137        let session_id = config.session_id.clone();
1138        if config.hooks_handler.is_some() && config.hooks.is_none() {
1139            config.hooks = Some(true);
1140        }
1141        if let Some(transforms) = config.system_message_transform.clone() {
1142            inject_transform_sections_resume(&mut config, transforms.as_ref());
1143        }
1144        let mode = self.inner.mode;
1145        if mode == crate::ClientMode::Empty && config.available_tools.is_none() {
1146            return Err(Error::with_message(
1147                ErrorKind::InvalidConfig,
1148                "ClientMode::Empty requires available_tools to be set on the session config. \
1149                 Use ToolSet to specify which tools the session may use (e.g. \
1150                 ToolSet::new().add_builtin_many(BUILTIN_TOOLS_ISOLATED)).",
1151            ));
1152        }
1153        crate::mode::validate_tool_filter_list(
1154            "available_tools",
1155            config.available_tools.as_deref(),
1156        )?;
1157        crate::mode::validate_tool_filter_list("excluded_tools", config.excluded_tools.as_deref())?;
1158        config.system_message =
1159            crate::mode::system_message_for_mode(mode, config.system_message.take());
1160        config.memory = crate::mode::memory_for_mode(mode, config.memory.take());
1161        config.enable_experimental_mode =
1162            crate::mode::experimental_mode_for_mode(mode, config.enable_experimental_mode);
1163        if mode == crate::ClientMode::Empty {
1164            if config.enable_session_telemetry.is_none() {
1165                config.enable_session_telemetry = Some(false);
1166            }
1167            if config.skip_embedding_retrieval.is_none() {
1168                config.skip_embedding_retrieval = Some(true);
1169            }
1170            if config.enable_on_demand_instruction_discovery.is_none() {
1171                config.enable_on_demand_instruction_discovery = Some(false);
1172            }
1173            if config.enable_file_hooks.is_none() {
1174                config.enable_file_hooks = Some(false);
1175            }
1176            if config.enable_host_git_operations.is_none() {
1177                config.enable_host_git_operations = Some(false);
1178            }
1179            if config.enable_session_store.is_none() {
1180                config.enable_session_store = Some(false);
1181            }
1182            if config.enable_skills.is_none() {
1183                config.enable_skills = Some(false);
1184            }
1185        }
1186        if mode == crate::ClientMode::Empty && config.mcp_oauth_token_storage.is_none() {
1187            config.mcp_oauth_token_storage = Some("in-memory".into());
1188        }
1189        if mode == crate::ClientMode::Empty && config.embedding_cache_storage.is_none() {
1190            config.embedding_cache_storage = Some("in-memory".into());
1191        }
1192        config.custom_agents_local_only =
1193            crate::mode::resolve_custom_agents_local_only(mode, config.custom_agents_local_only);
1194        let opt_skip_custom_instructions = config.skip_custom_instructions;
1195        let opt_custom_agents_local_only = config.custom_agents_local_only;
1196        let opt_coauthor_enabled = config.coauthor_enabled;
1197        let opt_manage_schedule_enabled = config.manage_schedule_enabled;
1198        let opt_included_builtin_skills = config.included_builtin_skills.take();
1199        let (mut wire, mut runtime) = config.into_wire()?;
1200        wire.enable_github_telemetry_forwarding =
1201            self.inner.on_github_telemetry.is_some().then_some(true);
1202
1203        let permission_handler = crate::permission::resolve_handler(
1204            runtime.permission_handler.take(),
1205            runtime.permission_policy.take(),
1206        );
1207        let handlers = SessionHandlers {
1208            permission: permission_handler,
1209            managed_settings_enabled: has_managed_settings(
1210                wire.enable_managed_settings,
1211                wire.managed_settings.as_ref(),
1212            ),
1213            elicitation: runtime.elicitation_handler.take(),
1214            mcp_auth: runtime.mcp_auth_handler.take(),
1215            user_input: runtime.user_input_handler.take(),
1216            exit_plan_mode: runtime.exit_plan_mode_handler.take(),
1217            auto_mode_switch: runtime.auto_mode_switch_handler.take(),
1218            tools: Arc::new(std::mem::take(&mut runtime.tool_handlers)),
1219        };
1220        let hooks = runtime.hooks_handler.take();
1221        let transforms = runtime.system_message_transform.take();
1222        let tools_count = wire.tools.as_ref().map_or(0, Vec::len);
1223        let commands_count = runtime.commands.as_ref().map_or(0, Vec::len);
1224        let has_hooks = hooks.is_some();
1225        let command_handlers = build_command_handler_map(runtime.commands.as_deref());
1226        let canvas_handler = runtime.canvas_handler.take();
1227        let session_fs_provider = runtime.session_fs_provider.take();
1228        let bearer_token_providers = std::mem::take(&mut runtime.bearer_token_providers);
1229        let github_token_registration = runtime
1230            .github_token_provider
1231            .take()
1232            .map(|provider| self.register_github_token_provider(provider));
1233        wire.github_token_provider_registration_id = github_token_registration
1234            .as_ref()
1235            .map(|registration| registration.id().to_string());
1236        let has_mcp_auth_handler = handlers.mcp_auth.is_some();
1237        if self.inner.session_fs_configured && session_fs_provider.is_none() {
1238            return Err(ErrorKind::Session(SessionErrorKind::SessionFsProviderRequired).into());
1239        }
1240        if self.inner.session_fs_sqlite_declared
1241            && let Some(ref provider) = session_fs_provider
1242            && provider.sqlite().is_none()
1243        {
1244            return Err(Error::with_message(
1245                ErrorKind::InvalidConfig,
1246                "SessionFs capabilities declare SQLite support but the provider \
1247                 does not implement SessionFsSqliteProvider",
1248            ));
1249        }
1250
1251        let mut params = serde_json::to_value(&wire)?;
1252        let trace_ctx = self.resolve_trace_context().await;
1253        inject_trace_context(&mut params, &trace_ctx);
1254
1255        let capabilities = Arc::new(parking_lot::RwLock::new(SessionCapabilities::default()));
1256        let setup_start = Instant::now();
1257        let channels = self.register_session(&session_id);
1258        let idle_waiter = Arc::new(ParkingLotMutex::new(None));
1259        let open_canvases = Arc::new(parking_lot::RwLock::new(Vec::new()));
1260        let shutdown = CancellationToken::new();
1261        let (event_tx, _) = tokio::sync::broadcast::channel(512);
1262        let event_loop = spawn_event_loop(
1263            session_id.clone(),
1264            self.clone(),
1265            handlers,
1266            hooks,
1267            transforms,
1268            command_handlers,
1269            canvas_handler,
1270            session_fs_provider,
1271            bearer_token_providers,
1272            channels,
1273            idle_waiter.clone(),
1274            capabilities.clone(),
1275            open_canvases.clone(),
1276            event_tx.clone(),
1277            shutdown.clone(),
1278        );
1279        let mut registration =
1280            PendingSessionRegistration::new(self.clone(), session_id.clone(), shutdown.clone());
1281        tracing::debug!(
1282            elapsed_ms = setup_start.elapsed().as_millis(),
1283            session_id = %session_id,
1284            tools_count,
1285            commands_count,
1286            has_hooks,
1287            "Client::resume_session local setup complete"
1288        );
1289
1290        let rpc_start = Instant::now();
1291        let result = match self.call("session.resume", Some(params)).await {
1292            Ok(result) => result,
1293            Err(error) => {
1294                registration.cleanup(event_loop).await;
1295                return Err(error);
1296            }
1297        };
1298        tracing::debug!(
1299            elapsed_ms = rpc_start.elapsed().as_millis(),
1300            session_id = %session_id,
1301            "Client::resume_session session resume request completed successfully"
1302        );
1303
1304        let resume_result: ResumeSessionResult = match serde_json::from_value(result) {
1305            Ok(result) => result,
1306            Err(error) => {
1307                registration.cleanup(event_loop).await;
1308                return Err(error.into());
1309            }
1310        };
1311        let cli_session_id = resume_result
1312            .session_id
1313            .clone()
1314            .unwrap_or_else(|| session_id.clone());
1315        if cli_session_id != session_id {
1316            registration.cleanup(event_loop).await;
1317            return Err(ErrorKind::Session(SessionErrorKind::SessionIdMismatch {
1318                requested: session_id,
1319                returned: cli_session_id,
1320            })
1321            .into());
1322        }
1323        if has_mcp_auth_handler {
1324            register_mcp_auth_interest(self, &session_id).await?;
1325        }
1326
1327        // Reload skills after resume (best-effort).
1328        let skills_reload_start = Instant::now();
1329        if let Err(e) = self
1330            .call(
1331                "session.skills.reload",
1332                Some(serde_json::json!({ "sessionId": session_id })),
1333            )
1334            .await
1335        {
1336            warn!(
1337                elapsed_ms = skills_reload_start.elapsed().as_millis(),
1338                session_id = %session_id,
1339                error = %e,
1340                "Client::resume_session skills reload request failed"
1341            );
1342        } else {
1343            tracing::debug!(
1344                elapsed_ms = skills_reload_start.elapsed().as_millis(),
1345                session_id = %session_id,
1346                "Client::resume_session skills reload request completed successfully"
1347            );
1348        }
1349
1350        *capabilities.write() = resume_result.capabilities.unwrap_or_default();
1351        // Upsert resume snapshots rather than replacing wholesale. Live
1352        // `session.canvas.opened` notifications can arrive on the event loop
1353        // while `session.resume` is in flight; a wholesale replace would
1354        // discard those updates.
1355        {
1356            let mut snapshots = open_canvases.write();
1357            for snapshot in resume_result.open_canvases.unwrap_or_default() {
1358                upsert_open_canvas_snapshot(&mut snapshots, snapshot);
1359            }
1360        }
1361
1362        tracing::debug!(
1363            elapsed_ms = total_start.elapsed().as_millis(),
1364            session_id = %session_id,
1365            "Client::resume_session complete"
1366        );
1367        registration.disarm();
1368        let session = Session {
1369            id: session_id,
1370            cwd: self.cwd().clone(),
1371            workspace_path: resume_result.workspace_path,
1372            remote_url: resume_result.remote_url,
1373            client: self.clone(),
1374            event_loop: ParkingLotMutex::new(Some(event_loop)),
1375            shutdown,
1376            idle_waiter,
1377            capabilities,
1378            open_canvases,
1379            event_tx,
1380            github_token_registration: ParkingLotMutex::new(github_token_registration),
1381        };
1382        apply_mode_post_create_patch(
1383            &session,
1384            mode,
1385            opt_skip_custom_instructions,
1386            opt_custom_agents_local_only,
1387            opt_coauthor_enabled,
1388            opt_manage_schedule_enabled,
1389            opt_included_builtin_skills,
1390        )
1391        .await?;
1392        if let Some(registration) = session.github_token_registration.lock().as_ref() {
1393            registration.claim(session.id.clone());
1394        } else {
1395            self.retire_github_token_provider(&session.id);
1396        }
1397        Ok(session)
1398    }
1399}
1400
1401type CommandHandlerMap = HashMap<String, Arc<dyn CommandHandler>>;
1402
1403async fn apply_mode_post_create_patch(
1404    session: &Session,
1405    mode: crate::ClientMode,
1406    opt_skip_custom_instructions: Option<bool>,
1407    opt_custom_agents_local_only: Option<bool>,
1408    opt_coauthor_enabled: Option<bool>,
1409    opt_manage_schedule_enabled: Option<bool>,
1410    opt_included_builtin_skills: Option<Vec<String>>,
1411) -> Result<(), Error> {
1412    let Some(patch) = build_mode_post_create_patch(
1413        mode,
1414        opt_skip_custom_instructions,
1415        opt_custom_agents_local_only,
1416        opt_coauthor_enabled,
1417        opt_manage_schedule_enabled,
1418        opt_included_builtin_skills,
1419    ) else {
1420        return Ok(());
1421    };
1422    if let Err(error) = session.rpc().options().update(patch).await {
1423        let _ = session.disconnect().await;
1424        return Err(error);
1425    }
1426    Ok(())
1427}
1428
1429/// Builds the `session.options.update` patch applied immediately after a session
1430/// is created or resumed, or returns `None` when no patch should be sent.
1431///
1432/// Under [`ClientMode::Empty`](crate::ClientMode::Empty) the overridable feature
1433/// flags fall back to safe defaults (caller values win), while
1434/// `installed_plugins` is unconditionally empty. `included_builtin_skills`
1435/// defaults to an empty list, but callers can explicitly allow selected
1436/// runtime-bundled skills. Under other modes only explicitly-set fields are
1437/// forwarded.
1438fn build_mode_post_create_patch(
1439    mode: crate::ClientMode,
1440    opt_skip_custom_instructions: Option<bool>,
1441    opt_custom_agents_local_only: Option<bool>,
1442    opt_coauthor_enabled: Option<bool>,
1443    opt_manage_schedule_enabled: Option<bool>,
1444    opt_included_builtin_skills: Option<Vec<String>>,
1445) -> Option<crate::generated::api_types::SessionUpdateOptionsParams> {
1446    use crate::generated::api_types::SessionUpdateOptionsParams;
1447    let mut patch = SessionUpdateOptionsParams::default();
1448    let should_send = if mode == crate::ClientMode::Empty {
1449        patch.skip_custom_instructions = Some(opt_skip_custom_instructions.unwrap_or(true));
1450        patch.custom_agents_local_only = Some(opt_custom_agents_local_only.unwrap_or(true));
1451        patch.coauthor_enabled = Some(opt_coauthor_enabled.unwrap_or(false));
1452        patch.manage_schedule_enabled = Some(opt_manage_schedule_enabled.unwrap_or(false));
1453        patch.installed_plugins = Some(Vec::new());
1454        patch.included_builtin_skills = Some(opt_included_builtin_skills.unwrap_or_default());
1455        true
1456    } else {
1457        let mut any = false;
1458        if let Some(v) = opt_skip_custom_instructions {
1459            patch.skip_custom_instructions = Some(v);
1460            any = true;
1461        }
1462        if let Some(v) = opt_custom_agents_local_only {
1463            patch.custom_agents_local_only = Some(v);
1464            any = true;
1465        }
1466        if let Some(v) = opt_coauthor_enabled {
1467            patch.coauthor_enabled = Some(v);
1468            any = true;
1469        }
1470        if let Some(v) = opt_manage_schedule_enabled {
1471            patch.manage_schedule_enabled = Some(v);
1472            any = true;
1473        }
1474        if let Some(v) = opt_included_builtin_skills {
1475            patch.included_builtin_skills = Some(v);
1476            any = true;
1477        }
1478        any
1479    };
1480    if !should_send {
1481        return None;
1482    }
1483    Some(patch)
1484}
1485
1486fn build_command_handler_map(commands: Option<&[CommandDefinition]>) -> Arc<CommandHandlerMap> {
1487    let map = match commands {
1488        Some(commands) => commands
1489            .iter()
1490            .filter(|cmd| !cmd.name.is_empty())
1491            .map(|cmd| (cmd.name.clone(), cmd.handler.clone()))
1492            .collect(),
1493        None => HashMap::new(),
1494    };
1495    Arc::new(map)
1496}
1497
1498fn upsert_open_canvas_snapshot(
1499    snapshots: &mut Vec<OpenCanvasInstance>,
1500    snapshot: OpenCanvasInstance,
1501) {
1502    if let Some(existing) = snapshots
1503        .iter_mut()
1504        .find(|open| open.instance_id == snapshot.instance_id)
1505    {
1506        *existing = snapshot;
1507    } else {
1508        snapshots.push(snapshot);
1509    }
1510}
1511
1512fn remove_open_canvas_snapshot(snapshots: &mut Vec<OpenCanvasInstance>, instance_id: &str) {
1513    snapshots.retain(|open| open.instance_id != instance_id);
1514}
1515
1516#[allow(clippy::too_many_arguments)]
1517fn spawn_event_loop(
1518    session_id: SessionId,
1519    client: Client,
1520    handlers: SessionHandlers,
1521    hooks: Option<Arc<dyn SessionHooks>>,
1522    transforms: Option<Arc<dyn SystemMessageTransform>>,
1523    command_handlers: Arc<CommandHandlerMap>,
1524    canvas_handler: Option<Arc<dyn CanvasHandler>>,
1525    session_fs_provider: Option<Arc<dyn SessionFsProvider>>,
1526    bearer_token_providers: HashMap<String, Arc<dyn BearerTokenProvider>>,
1527    channels: crate::router::SessionChannels,
1528    idle_waiter: Arc<ParkingLotMutex<Option<IdleWaiter>>>,
1529    capabilities: Arc<parking_lot::RwLock<SessionCapabilities>>,
1530    open_canvases: Arc<parking_lot::RwLock<Vec<OpenCanvasInstance>>>,
1531    event_tx: tokio::sync::broadcast::Sender<SessionEvent>,
1532    shutdown: CancellationToken,
1533) -> JoinHandle<()> {
1534    let crate::router::SessionChannels {
1535        mut notifications,
1536        mut requests,
1537    } = channels;
1538
1539    let span = tracing::error_span!("session_event_loop", session_id = %session_id);
1540    tokio::spawn(
1541        async move {
1542            loop {
1543                // `mpsc::UnboundedReceiver::recv` and
1544                // `CancellationToken::cancelled` are both cancel-safe per
1545                // RFD 400.
1546                //
1547                // Inbound JSON-RPC *requests* are dispatched fire-and-forget:
1548                // each `handle_request` runs in its own spawned task that
1549                // awaits the handler and sends that request's response. This
1550                // mirrors the other Copilot SDKs and moves concurrency to the
1551                // request-dispatch boundary, so any slow handler — not just
1552                // `userInput.request` (which can stay pending for the full
1553                // input backstop of several minutes), but also `exitPlanMode`,
1554                // `autoModeSwitch`, hooks, transforms, or canvas/session-FS
1555                // providers — cannot park the reader loop and starve sibling
1556                // requests or co-emitted notifications. JSON-RPC permits
1557                // concurrent requests and out-of-order responses, so the SDK
1558                // does not serialize them.
1559                //
1560                // `handle_notification` is awaited inline because it only
1561                // performs fast dispatch work; its slow interactive callbacks
1562                // (permission/tool/elicitation) are themselves spawned as child
1563                // tasks. All of these spawned tasks intentionally outlive the
1564                // parent loop and own their own cleanup — RFD 400's "spawn
1565                // background tasks to perform cancel-unsafe operations" pattern.
1566                tokio::select! {
1567                    _ = shutdown.cancelled() => break,
1568                    Some(notification) = notifications.recv() => {
1569                        handle_notification(
1570                            &session_id, &client, &handlers, &command_handlers, notification, &idle_waiter, &capabilities, &open_canvases, &event_tx,
1571                        ).await;
1572                    }
1573                    Some(request) = requests.recv() => {
1574                        // Clone the Arc-backed dispatch context into the task so
1575                        // the spawned `handle_request` future is `'static`. All
1576                        // clones are cheap (Arc refcount bumps / small maps).
1577                        let span = tracing::error_span!("session_request_handler", session_id = %session_id);
1578                        let session_id = session_id.clone();
1579                        let client = client.clone();
1580                        let handlers = handlers.clone();
1581                        let hooks = hooks.clone();
1582                        let transforms = transforms.clone();
1583                        let canvas_handler = canvas_handler.clone();
1584                        let session_fs_provider = session_fs_provider.clone();
1585                        let bearer_token_providers = bearer_token_providers.clone();
1586                        tokio::spawn(
1587                            async move {
1588                                let ctx = RequestDispatchContext {
1589                                    client: &client,
1590                                    handlers: &handlers,
1591                                    hooks: hooks.as_deref(),
1592                                    transforms: transforms.as_deref(),
1593                                    canvas_handler: canvas_handler.as_ref(),
1594                                    session_fs_provider: session_fs_provider.as_ref(),
1595                                    bearer_token_providers: &bearer_token_providers,
1596                                };
1597                                handle_request(&session_id, ctx, request).await;
1598                            }
1599                            .instrument(span),
1600                        );
1601                    }
1602                    else => break,
1603                }
1604            }
1605            // Channels closed or shutdown signaled — fail any pending
1606            // send_and_wait so the caller observes a clean error.
1607            if let Some(waiter) = idle_waiter.lock().take() {
1608                let _ = waiter
1609                    .tx
1610                    .send(Err(ErrorKind::Session(SessionErrorKind::EventLoopClosed).into()));
1611            }
1612        }
1613        .instrument(span),
1614    )
1615}
1616
1617fn extract_request_id(data: &Value) -> Option<RequestId> {
1618    data.get("requestId")
1619        .and_then(|v| v.as_str())
1620        .filter(|s| !s.is_empty())
1621        .map(RequestId::new)
1622}
1623
1624fn permission_request_data(
1625    event_data: &Value,
1626    managed_settings_enabled: bool,
1627) -> PermissionRequestData {
1628    let request_data = event_data
1629        .get("permissionRequest")
1630        .cloned()
1631        .unwrap_or_else(|| event_data.clone());
1632    let managed_approval_required = match request_data.get("managedApprovalRequired") {
1633        None => None,
1634        Some(Value::Bool(value)) => Some(*value),
1635        Some(_) => Some(true),
1636    };
1637    match serde_json::from_value::<PermissionRequestData>(request_data) {
1638        Ok(mut data) => {
1639            data.extra = event_data.clone();
1640            data.managed_settings_enabled = managed_settings_enabled;
1641            data
1642        }
1643        Err(_) => PermissionRequestData {
1644            kind: None,
1645            tool_call_id: None,
1646            managed_approval_required,
1647            managed_settings_enabled,
1648            extra: event_data.clone(),
1649        },
1650    }
1651}
1652
1653/// Build the full `session.permissions.handlePendingPermissionRequest`
1654/// params for a permission result.
1655///
1656/// `decisionContext` is a sibling of `result` and is only present when the
1657/// handler attributed the decision — omitting it preserves legacy behavior.
1658///
1659/// Returns `None` when the SDK must not send a response.
1660fn permission_response_params(
1661    session_id: &SessionId,
1662    request_id: &RequestId,
1663    result: &PermissionResult,
1664) -> Option<Value> {
1665    let (decision, decision_context) = match result {
1666        PermissionResult::Decision { decision, context } => (decision, context.clone()),
1667        PermissionResult::NoResult => return None,
1668    };
1669    let mut params = serde_json::to_value(PermissionDecisionRequest {
1670        decision_context,
1671        request_id: request_id.clone(),
1672        result: decision.clone(),
1673    })
1674    .expect("serializing permission response should succeed");
1675    params["sessionId"] =
1676        serde_json::to_value(session_id).expect("serializing session ID should succeed");
1677    Some(params)
1678}
1679
1680async fn register_mcp_auth_interest(client: &Client, session_id: &SessionId) -> Result<(), Error> {
1681    let mut params = serde_json::to_value(RegisterEventInterestParams {
1682        event_type: "mcp.oauth_required".to_string(),
1683    })?;
1684    params["sessionId"] = Value::String(session_id.to_string());
1685    client
1686        .call(rpc_methods::SESSION_EVENTLOG_REGISTERINTEREST, Some(params))
1687        .await?;
1688    Ok(())
1689}
1690
1691fn tool_failure_result(message: impl Into<String>) -> ToolResult {
1692    let message = message.into();
1693    ToolResult::Expanded(ToolResultExpanded {
1694        text_result_for_llm: message.clone(),
1695        result_type: "failure".to_string(),
1696        binary_results_for_llm: None,
1697        session_log: None,
1698        error: Some(message),
1699        tool_telemetry: None,
1700        tool_references: None,
1701    })
1702}
1703
1704fn is_autopilot_continuation_idle(event: &SessionEvent) -> bool {
1705    event
1706        .typed_data::<SessionIdleData>()
1707        .is_some_and(|data| data.mode == Some(SessionMode::Autopilot))
1708}
1709
1710/// Process a notification from the CLI's broadcast channel.
1711#[allow(clippy::too_many_arguments)]
1712async fn handle_notification(
1713    session_id: &SessionId,
1714    client: &Client,
1715    handlers: &SessionHandlers,
1716    command_handlers: &Arc<CommandHandlerMap>,
1717    notification: SessionEventNotification,
1718    idle_waiter: &Arc<ParkingLotMutex<Option<IdleWaiter>>>,
1719    capabilities: &Arc<parking_lot::RwLock<SessionCapabilities>>,
1720    open_canvases: &Arc<parking_lot::RwLock<Vec<OpenCanvasInstance>>>,
1721    event_tx: &tokio::sync::broadcast::Sender<SessionEvent>,
1722) {
1723    let dispatch_start = Instant::now();
1724    let event = notification.event.clone();
1725    let event_type = event.parsed_type();
1726    if event_type == SessionEventType::PermissionRequested {
1727        tracing::debug!(
1728            session_id = %session_id,
1729            event_type = %event.event_type,
1730            "Session::handle_notification permission request received"
1731        );
1732    }
1733
1734    // Signal send_and_wait if active. The lock is only contended when
1735    // a send_and_wait call is in flight (idle_waiter is Some).
1736    match event_type {
1737        SessionEventType::AssistantMessage
1738        | SessionEventType::SessionIdle
1739        | SessionEventType::SessionError => {
1740            let mut guard = idle_waiter.lock();
1741            if let Some(waiter) = guard.as_mut() {
1742                match event_type {
1743                    SessionEventType::AssistantMessage => {
1744                        if !waiter.first_assistant_message_seen {
1745                            waiter.first_assistant_message_seen = true;
1746                            tracing::debug!(
1747                                elapsed_ms = waiter.started_at.elapsed().as_millis(),
1748                                session_id = %session_id,
1749                                "Session::send_and_wait first assistant message"
1750                            );
1751                        }
1752                        waiter.last_assistant_message = Some(event.clone());
1753                    }
1754                    SessionEventType::SessionIdle if is_autopilot_continuation_idle(&event) => {}
1755                    SessionEventType::SessionIdle | SessionEventType::SessionError => {
1756                        if let Some(waiter) = guard.take() {
1757                            if event_type == SessionEventType::SessionIdle {
1758                                tracing::debug!(
1759                                    elapsed_ms = waiter.started_at.elapsed().as_millis(),
1760                                    session_id = %session_id,
1761                                    "Session::send_and_wait idle received"
1762                                );
1763                                let _ = waiter.tx.send(Ok(waiter.last_assistant_message));
1764                            } else {
1765                                let error_msg = event
1766                                    .typed_data::<SessionErrorData>()
1767                                    .map(|d| d.message)
1768                                    .or_else(|| {
1769                                        event
1770                                            .data
1771                                            .get("message")
1772                                            .and_then(|v| v.as_str())
1773                                            .map(|s| s.to_string())
1774                                    })
1775                                    .unwrap_or_else(|| "session error".to_string());
1776                                let _ = waiter.tx.send(Err(Error::with_message(
1777                                    ErrorKind::Session(SessionErrorKind::AgentError),
1778                                    error_msg,
1779                                )));
1780                            }
1781                        }
1782                    }
1783                    _ => {}
1784                }
1785            }
1786        }
1787        _ => {}
1788    }
1789
1790    // Update the snapshot caches BEFORE broadcasting so subscribers that
1791    // call `Session::capabilities()` / `Session::open_canvases()` in
1792    // response to the event observe the new state.
1793    if event_type == SessionEventType::CapabilitiesChanged {
1794        match serde_json::from_value::<SessionCapabilities>(notification.event.data.clone()) {
1795            Ok(changed) => *capabilities.write() = changed,
1796            Err(e) => warn!(error = %e, "failed to deserialize capabilities.changed payload"),
1797        }
1798    }
1799    if event_type == SessionEventType::SessionCanvasOpened {
1800        match serde_json::from_value::<OpenCanvasInstance>(notification.event.data.clone()) {
1801            Ok(open_canvas) => {
1802                upsert_open_canvas_snapshot(&mut open_canvases.write(), open_canvas);
1803            }
1804            Err(e) => warn!(error = %e, "failed to deserialize session.canvas.opened payload"),
1805        }
1806    }
1807    if event_type == SessionEventType::SessionCanvasClosed {
1808        match serde_json::from_value::<SessionCanvasClosedData>(notification.event.data.clone()) {
1809            Ok(closed) => {
1810                if closed.instance_id.is_empty() {
1811                    warn!("failed to deserialize session.canvas.closed payload");
1812                } else {
1813                    remove_open_canvas_snapshot(&mut open_canvases.write(), &closed.instance_id);
1814                }
1815            }
1816            Err(e) => warn!(error = %e, "failed to deserialize session.canvas.closed payload"),
1817        }
1818    }
1819
1820    // Fan out the event to runtime subscribers (`Session::subscribe`). `send`
1821    // only errors when there are no receivers, which is the normal case
1822    // before any consumer subscribes.
1823    let _ = event_tx.send(event.clone());
1824
1825    tracing::debug!(
1826        elapsed_ms = dispatch_start.elapsed().as_millis(),
1827        session_id = %session_id,
1828        event_type = %notification.event.event_type,
1829        "Session::handle_notification dispatch"
1830    );
1831
1832    // Notification-based permission/tool/elicitation requests require a
1833    // separate RPC callback. Spawn concurrently since the CLI doesn't block.
1834    match event_type {
1835        SessionEventType::PermissionRequested => {
1836            let Some(request_id) = extract_request_id(&notification.event.data) else {
1837                return;
1838            };
1839            // Honor the runtime's `resolvedByHook` signal — when the
1840            // server has already resolved the permission via a hook,
1841            // clients must not send a second response.
1842            if notification
1843                .event
1844                .data
1845                .get("resolvedByHook")
1846                .and_then(|v| v.as_bool())
1847                .unwrap_or(false)
1848            {
1849                return;
1850            }
1851            // Multi-client safety: if this client has no permission
1852            // handler installed, don't respond — another client on the
1853            // same CLI may handle it.
1854            let Some(permission_handler) = handlers.permission.clone() else {
1855                return;
1856            };
1857            let client = client.clone();
1858            let sid = session_id.clone();
1859            let data = permission_request_data(
1860                &notification.event.data,
1861                handlers.managed_settings_enabled,
1862            );
1863            let span = tracing::error_span!(
1864                "permission_request_handler",
1865                session_id = %sid,
1866                request_id = %request_id
1867            );
1868            tokio::spawn(
1869                async move {
1870                    let handler_start = Instant::now();
1871                    let result = permission_handler
1872                        .handle(sid.clone(), request_id.clone(), data)
1873                        .await;
1874                    tracing::debug!(
1875                        elapsed_ms = handler_start.elapsed().as_millis(),
1876                        session_id = %sid,
1877                        request_id = %request_id,
1878                        "PermissionHandler::handle dispatch"
1879                    );
1880                    let Some(params) = permission_response_params(&sid, &request_id, &result)
1881                    else {
1882                        // Handler returned Deferred / NoResult — it will
1883                        // call handlePendingPermissionRequest itself (or
1884                        // leave the request unanswered).
1885                        return;
1886                    };
1887                    let rpc_start = Instant::now();
1888                    let _ = client
1889                        .call(
1890                            rpc_methods::SESSION_PERMISSIONS_HANDLEPENDINGPERMISSIONREQUEST,
1891                            Some(params),
1892                        )
1893                        .await;
1894                    tracing::debug!(
1895                        elapsed_ms = rpc_start.elapsed().as_millis(),
1896                        session_id = %sid,
1897                        request_id = %request_id,
1898                        "Session::handle_notification response sent successfully"
1899                    );
1900                }
1901                .instrument(span),
1902            );
1903        }
1904        SessionEventType::ExternalToolRequested => {
1905            let Some(request_id) = extract_request_id(&notification.event.data) else {
1906                return;
1907            };
1908            let data: ExternalToolRequestedData =
1909                match serde_json::from_value(notification.event.data.clone()) {
1910                    Ok(d) => d,
1911                    Err(e) => {
1912                        warn!(error = %e, "failed to deserialize external_tool.requested");
1913                        let client = client.clone();
1914                        let sid = session_id.clone();
1915                        let span = tracing::error_span!(
1916                            "external_tool_deserialize_error",
1917                            session_id = %sid,
1918                            request_id = %request_id
1919                        );
1920                        tokio::spawn(
1921                            async move {
1922                                let rpc_start = Instant::now();
1923                                let _ = client
1924                                .call(
1925                                    "session.tools.handlePendingToolCall",
1926                                    Some(serde_json::json!({
1927                                        "sessionId": sid,
1928                                        "requestId": request_id,
1929                                        "error": format!("Failed to deserialize tool request: {e}"),
1930                                    })),
1931                                )
1932                                .await;
1933                                tracing::debug!(
1934                                    elapsed_ms = rpc_start.elapsed().as_millis(),
1935                                    session_id = %sid,
1936                                    request_id = %request_id,
1937                                    "Session::handle_notification response sent successfully"
1938                                );
1939                            }
1940                            .instrument(span),
1941                        );
1942                        return;
1943                    }
1944                };
1945            // Multi-client safety: look up a handler for the requested
1946            // tool name. If this client has no handler installed for that
1947            // tool, don't respond — another connected client may have one.
1948            let tool_handler = if data.tool_name.is_empty() {
1949                None
1950            } else {
1951                handlers.tools.get(&data.tool_name).cloned()
1952            };
1953            let Some(tool_handler) = tool_handler else {
1954                return;
1955            };
1956            let client = client.clone();
1957            let sid = session_id.clone();
1958            let span = tracing::error_span!(
1959                "external_tool_handler",
1960                session_id = %sid,
1961                request_id = %request_id
1962            );
1963            tokio::spawn(
1964                async move {
1965                    // `tool_name.is_empty()` would have produced a `None`
1966                    // lookup in `handlers.tools` and short-circuited at the
1967                    // outer guard above, so only the tool_call_id check is
1968                    // reachable here.
1969                    if data.tool_call_id.is_empty() {
1970                        let error_msg = "Missing toolCallId";
1971                        let rpc_start = Instant::now();
1972                        let _ = client
1973                            .call(
1974                                "session.tools.handlePendingToolCall",
1975                                Some(serde_json::json!({
1976                                    "sessionId": sid,
1977                                    "requestId": request_id,
1978                                    "error": error_msg,
1979                                })),
1980                            )
1981                            .await;
1982                        tracing::debug!(
1983                            elapsed_ms = rpc_start.elapsed().as_millis(),
1984                            session_id = %sid,
1985                            request_id = %request_id,
1986                            "Session::handle_notification response sent successfully"
1987                        );
1988                        return;
1989                    }
1990                    let tool_call_id = data.tool_call_id.clone();
1991                    let tool_name = data.tool_name.clone();
1992                    // The built-in tool-search tool receives a snapshot of the
1993                    // session's currently initialized tools so an override can
1994                    // filter the live catalog without issuing its own RPC. Fetch
1995                    // it only for that tool to avoid a round-trip on every tool
1996                    // call; a failed fetch leaves the snapshot `None` rather than
1997                    // failing the tool.
1998                    let available_tools = if tool_name == TOOL_SEARCH_TOOL_NAME {
1999                        match client
2000                            .call(
2001                                rpc_methods::SESSION_TOOLS_GETCURRENTMETADATA,
2002                                Some(serde_json::json!({ "sessionId": sid })),
2003                            )
2004                            .await
2005                        {
2006                            Ok(value) => {
2007                                serde_json::from_value::<ToolsGetCurrentMetadataResult>(value)
2008                                    .ok()
2009                                    .and_then(|result| result.tools)
2010                            }
2011                            Err(_) => None,
2012                        }
2013                    } else {
2014                        None
2015                    };
2016                    let invocation = ToolInvocation {
2017                        session_id: sid.clone(),
2018                        tool_call_id: data.tool_call_id,
2019                        tool_name: data.tool_name,
2020                        arguments: data
2021                            .arguments
2022                            .unwrap_or(Value::Object(serde_json::Map::new())),
2023                        available_tools,
2024                        traceparent: data.traceparent,
2025                        tracestate: data.tracestate,
2026                    };
2027                    let handler_start = Instant::now();
2028                    let tool_result = match tool_handler.call(invocation).await {
2029                        Ok(r) => r,
2030                        Err(e) => tool_failure_result(e.to_string()),
2031                    };
2032                    tracing::debug!(
2033                        elapsed_ms = handler_start.elapsed().as_millis(),
2034                        session_id = %sid,
2035                        request_id = %request_id,
2036                        tool_call_id = %tool_call_id,
2037                        tool_name = %tool_name,
2038                        "ToolHandler::call dispatch"
2039                    );
2040                    let result_value = serde_json::to_value(tool_result).unwrap_or(Value::Null);
2041                    let rpc_start = Instant::now();
2042                    let _ = client
2043                        .call(
2044                            "session.tools.handlePendingToolCall",
2045                            Some(serde_json::json!({
2046                                "sessionId": sid,
2047                                "requestId": request_id,
2048                                "result": result_value,
2049                            })),
2050                        )
2051                        .await;
2052                    tracing::debug!(
2053                        elapsed_ms = rpc_start.elapsed().as_millis(),
2054                        session_id = %sid,
2055                        request_id = %request_id,
2056                        tool_call_id = %tool_call_id,
2057                        tool_name = %tool_name,
2058                        "Session::handle_notification response sent successfully"
2059                    );
2060                }
2061                .instrument(span),
2062            );
2063        }
2064        SessionEventType::UserInputRequested => {
2065            // Notification-only signal for observers (UI, telemetry).
2066            // The CLI follows up with a `userInput.request` JSON-RPC call
2067            // that drives the `UserInputHandler` dispatch — handling
2068            // the notification here too would double-fire the handler
2069            // and produce duplicate prompts on the consumer side. See
2070            // github/github-app#4249.
2071        }
2072        SessionEventType::ElicitationRequested => {
2073            let Some(request_id) = extract_request_id(&notification.event.data) else {
2074                return;
2075            };
2076            // Multi-client safety: if this client has no elicitation
2077            // handler installed, don't respond — another client on the
2078            // same CLI may handle it.
2079            let Some(elicitation_handler) = handlers.elicitation.clone() else {
2080                return;
2081            };
2082            let elicitation_data: ElicitationRequestedData =
2083                match serde_json::from_value(notification.event.data.clone()) {
2084                    Ok(d) => d,
2085                    Err(e) => {
2086                        warn!(error = %e, "failed to deserialize elicitation request");
2087                        return;
2088                    }
2089                };
2090            let request = ElicitationRequest {
2091                message: elicitation_data.message,
2092                requested_schema: elicitation_data
2093                    .requested_schema
2094                    .map(|s| serde_json::to_value(s).unwrap_or(Value::Null)),
2095                mode: elicitation_data.mode.map(|m| match m {
2096                    crate::generated::session_events::ElicitationRequestedMode::Form => {
2097                        crate::types::ElicitationMode::Form
2098                    }
2099                    crate::generated::session_events::ElicitationRequestedMode::Url => {
2100                        crate::types::ElicitationMode::Url
2101                    }
2102                    _ => crate::types::ElicitationMode::Unknown,
2103                }),
2104                elicitation_source: elicitation_data.elicitation_source,
2105                url: elicitation_data.url,
2106            };
2107            let client = client.clone();
2108            let sid = session_id.clone();
2109            let span = tracing::error_span!(
2110                "elicitation_request_handler",
2111                session_id = %sid,
2112                request_id = %request_id
2113            );
2114            tokio::spawn(
2115                async move {
2116                    let cancel = ElicitationResult {
2117                        action: "cancel".to_string(),
2118                        content: None,
2119                    };
2120                    // Dispatch to a nested task so panics are caught as JoinErrors.
2121                    let handler_task = tokio::spawn({
2122                        let sid = sid.clone();
2123                        let request_id = request_id.clone();
2124                        let span = tracing::error_span!(
2125                            "elicitation_callback",
2126                            session_id = %sid,
2127                            request_id = %request_id
2128                        );
2129                        async move {
2130                            let handler_start = Instant::now();
2131                            let response = elicitation_handler
2132                                .handle(sid.clone(), request_id.clone(), request)
2133                                .await;
2134                            tracing::debug!(
2135                                elapsed_ms = handler_start.elapsed().as_millis(),
2136                                session_id = %sid,
2137                                request_id = %request_id,
2138                                "ElicitationHandler::handle dispatch"
2139                            );
2140                            response
2141                        }
2142                        .instrument(span)
2143                    });
2144                    let result = match handler_task.await {
2145                        Ok(r) => r,
2146                        Err(_) => cancel.clone(),
2147                    };
2148                    let rpc_start = Instant::now();
2149                    if let Err(e) = client
2150                        .call(
2151                            "session.ui.handlePendingElicitation",
2152                            Some(serde_json::json!({
2153                                "sessionId": sid,
2154                                "requestId": request_id,
2155                                "result": result,
2156                            })),
2157                        )
2158                        .await
2159                    {
2160                        // RPC failed — attempt cancel as last resort
2161                        warn!(error = %e, "handlePendingElicitation failed, sending cancel");
2162                        let _ = client
2163                            .call(
2164                                "session.ui.handlePendingElicitation",
2165                                Some(serde_json::json!({
2166                                    "sessionId": sid,
2167                                    "requestId": request_id,
2168                                    "result": cancel,
2169                                })),
2170                            )
2171                            .await;
2172                    } else {
2173                        tracing::debug!(
2174                            elapsed_ms = rpc_start.elapsed().as_millis(),
2175                            session_id = %sid,
2176                            request_id = %request_id,
2177                            "Session::handle_notification response sent successfully"
2178                        );
2179                    }
2180                }
2181                .instrument(span),
2182            );
2183        }
2184        SessionEventType::McpOauthRequired => {
2185            let Some(request_id) = extract_request_id(&notification.event.data) else {
2186                return;
2187            };
2188            let Some(mcp_auth_handler) = handlers.mcp_auth.clone() else {
2189                warn!(
2190                    session_id = %session_id,
2191                    request_id = %request_id,
2192                    "received MCP OAuth request without a registered MCP auth handler"
2193                );
2194                return;
2195            };
2196            let data: McpOauthRequiredData =
2197                match serde_json::from_value(notification.event.data.clone()) {
2198                    Ok(d) => d,
2199                    Err(e) => {
2200                        warn!(error = %e, "failed to deserialize MCP OAuth request");
2201                        return;
2202                    }
2203                };
2204            let request = McpAuthRequest {
2205                request_id: request_id.clone(),
2206                server_name: data.server_name,
2207                server_url: data.server_url,
2208                reason: data.reason,
2209                www_authenticate_params: data.www_authenticate_params,
2210                resource_metadata: data.resource_metadata,
2211                static_client_config: data.static_client_config,
2212            };
2213            let client = client.clone();
2214            let sid = session_id.clone();
2215            let span = tracing::error_span!(
2216                "mcp_auth_request_handler",
2217                session_id = %sid,
2218                request_id = %request_id
2219            );
2220            tokio::spawn(
2221                async move {
2222                    let cancel = McpAuthResult::Cancelled;
2223                    let handler_task = tokio::spawn({
2224                        let sid = sid.clone();
2225                        let request_id = request_id.clone();
2226                        let span = tracing::error_span!(
2227                            "mcp_auth_callback",
2228                            session_id = %sid,
2229                            request_id = %request_id
2230                        );
2231                        async move {
2232                            let handler_start = Instant::now();
2233                            let response = mcp_auth_handler
2234                                .handle(sid.clone(), request_id.clone(), request)
2235                                .await;
2236                            tracing::debug!(
2237                                elapsed_ms = handler_start.elapsed().as_millis(),
2238                                session_id = %sid,
2239                                request_id = %request_id,
2240                                "McpAuthHandler::handle dispatch"
2241                            );
2242                            response
2243                        }
2244                        .instrument(span)
2245                    });
2246                    let result = match handler_task.await {
2247                        Ok(result) => result,
2248                        Err(_) => cancel,
2249                    };
2250                    let rpc_start = Instant::now();
2251                    let _ = client
2252                        .call(
2253                            "session.mcp.oauth.handlePendingRequest",
2254                            Some(serde_json::json!({
2255                                "sessionId": sid,
2256                                "requestId": request_id,
2257                                "result": result.into_wire(),
2258                            })),
2259                        )
2260                        .await;
2261                    tracing::debug!(
2262                        elapsed_ms = rpc_start.elapsed().as_millis(),
2263                        "Session::handle_notification MCP auth response sent"
2264                    );
2265                }
2266                .instrument(span),
2267            );
2268        }
2269        SessionEventType::CommandExecute => {
2270            let data: CommandExecuteData =
2271                match serde_json::from_value(notification.event.data.clone()) {
2272                    Ok(d) => d,
2273                    Err(e) => {
2274                        warn!(error = %e, "failed to deserialize command.execute");
2275                        return;
2276                    }
2277                };
2278            let client = client.clone();
2279            let command_handlers = command_handlers.clone();
2280            let sid = session_id.clone();
2281            let span = tracing::error_span!("command_handler", session_id = %sid);
2282            tokio::spawn(
2283                async move {
2284                    let request_id = data.request_id;
2285                    let ack_error = match command_handlers.get(&data.command_name).cloned() {
2286                        None => Some(format!("Unknown command: {}", data.command_name)),
2287                        Some(handler) => {
2288                            let command_name = data.command_name.clone();
2289                            let ctx = CommandContext {
2290                                session_id: sid.clone(),
2291                                command: data.command,
2292                                command_name: data.command_name,
2293                                args: data.args,
2294                            };
2295                            let handler_start = Instant::now();
2296                            let result = handler.on_command(ctx).await;
2297                            tracing::debug!(
2298                                elapsed_ms = handler_start.elapsed().as_millis(),
2299                                session_id = %sid,
2300                                request_id = %request_id,
2301                                command_name = %command_name,
2302                                "CommandHandler::call dispatch"
2303                            );
2304                            match result {
2305                                Ok(()) => None,
2306                                Err(e) => Some(e.to_string()),
2307                            }
2308                        }
2309                    };
2310                    let mut params = serde_json::json!({
2311                        "sessionId": sid,
2312                        "requestId": request_id,
2313                    });
2314                    if let Some(error_msg) = ack_error {
2315                        params["error"] = serde_json::Value::String(error_msg);
2316                    }
2317                    let rpc_start = Instant::now();
2318                    let _ = client
2319                        .call("session.commands.handlePendingCommand", Some(params))
2320                        .await;
2321                    tracing::debug!(
2322                        elapsed_ms = rpc_start.elapsed().as_millis(),
2323                        session_id = %sid,
2324                        request_id = %request_id,
2325                        "Session::handle_notification response sent successfully"
2326                    );
2327                }
2328                .instrument(span),
2329            );
2330        }
2331        _ => {}
2332    }
2333}
2334
2335struct RequestDispatchContext<'a> {
2336    client: &'a Client,
2337    handlers: &'a SessionHandlers,
2338    hooks: Option<&'a dyn SessionHooks>,
2339    transforms: Option<&'a dyn SystemMessageTransform>,
2340    canvas_handler: Option<&'a Arc<dyn CanvasHandler>>,
2341    session_fs_provider: Option<&'a Arc<dyn SessionFsProvider>>,
2342    bearer_token_providers: &'a HashMap<String, Arc<dyn BearerTokenProvider>>,
2343}
2344
2345/// Process a JSON-RPC request from the CLI.
2346async fn handle_request(
2347    session_id: &SessionId,
2348    ctx: RequestDispatchContext<'_>,
2349    request: crate::JsonRpcRequest,
2350) {
2351    let sid = session_id.clone();
2352    let client = ctx.client;
2353    let handlers = ctx.handlers;
2354    let hooks = ctx.hooks;
2355    let transforms = ctx.transforms;
2356    let canvas_handler = ctx.canvas_handler;
2357    let session_fs_provider = ctx.session_fs_provider;
2358    let bearer_token_providers = ctx.bearer_token_providers;
2359
2360    if request.method.starts_with("sessionFs.") {
2361        crate::session_fs_dispatch::dispatch(client, session_fs_provider, request).await;
2362        return;
2363    }
2364
2365    if request.method.starts_with("canvas.") {
2366        crate::canvas_dispatch::dispatch(client, canvas_handler, request).await;
2367        return;
2368    }
2369
2370    if request.method == crate::generated::api_types::rpc_methods::PROVIDERTOKEN_GETTOKEN {
2371        crate::provider_token_dispatch::dispatch(client, bearer_token_providers, request).await;
2372        return;
2373    }
2374
2375    match request.method.as_str() {
2376        "hooks.invoke" => {
2377            let params = request.params.as_ref();
2378            let hook_type = params
2379                .and_then(|p| p.get("hookType"))
2380                .and_then(|v| v.as_str())
2381                .unwrap_or("");
2382            let input = params
2383                .and_then(|p| p.get("input"))
2384                .cloned()
2385                .unwrap_or(Value::Object(Default::default()));
2386
2387            let rpc_result = if let Some(hooks) = hooks {
2388                match crate::hooks::dispatch_hook(hooks, &sid, hook_type, input).await {
2389                    Ok(output) => output,
2390                    Err(e) => {
2391                        warn!(error = %e, hook_type = hook_type, "hook dispatch failed");
2392                        serde_json::json!({ "output": {} })
2393                    }
2394                }
2395            } else {
2396                serde_json::json!({ "output": {} })
2397            };
2398
2399            let rpc_response = JsonRpcResponse {
2400                jsonrpc: "2.0".to_string(),
2401                id: request.id,
2402                result: Some(rpc_result),
2403                error: None,
2404            };
2405            let _ = client.send_response(&rpc_response).await;
2406        }
2407
2408        "userInput.request" => {
2409            let params = request.params.as_ref();
2410            let Some(question) = params
2411                .and_then(|p| p.get("question"))
2412                .and_then(|v| v.as_str())
2413            else {
2414                warn!("userInput.request missing 'question' field");
2415                let rpc_response = JsonRpcResponse {
2416                    jsonrpc: "2.0".to_string(),
2417                    id: request.id,
2418                    result: None,
2419                    error: Some(crate::JsonRpcError {
2420                        code: error_codes::INVALID_PARAMS,
2421                        message: "missing required field: question".to_string(),
2422                        data: None,
2423                    }),
2424                };
2425                let _ = client.send_response(&rpc_response).await;
2426                return;
2427            };
2428            let question = question.to_string();
2429            let choices = params
2430                .and_then(|p| p.get("choices"))
2431                .and_then(|v| v.as_array())
2432                .map(|arr| {
2433                    arr.iter()
2434                        .filter_map(|v| v.as_str().map(|s| s.to_string()))
2435                        .collect()
2436                });
2437            let allow_freeform = params
2438                .and_then(|p| p.get("allowFreeform"))
2439                .and_then(|v| v.as_bool());
2440
2441            let handler_start = Instant::now();
2442            let response = if let Some(user_input_handler) = handlers.user_input.as_ref() {
2443                user_input_handler
2444                    .handle(sid.clone(), question, choices, allow_freeform)
2445                    .await
2446            } else {
2447                None
2448            };
2449            tracing::debug!(
2450                elapsed_ms = handler_start.elapsed().as_millis(),
2451                session_id = %sid,
2452                "UserInputHandler::handle dispatch"
2453            );
2454
2455            let rpc_result = match response {
2456                Some(UserInputResponse {
2457                    answer,
2458                    was_freeform,
2459                }) => serde_json::json!({
2460                    "answer": answer,
2461                    "wasFreeform": was_freeform,
2462                }),
2463                None => serde_json::json!({ "noResponse": true }),
2464            };
2465            let rpc_response = JsonRpcResponse {
2466                jsonrpc: "2.0".to_string(),
2467                id: request.id,
2468                result: Some(rpc_result),
2469                error: None,
2470            };
2471            let _ = client.send_response(&rpc_response).await;
2472        }
2473
2474        "exitPlanMode.request" => {
2475            let params = request
2476                .params
2477                .as_ref()
2478                .cloned()
2479                .unwrap_or(Value::Object(serde_json::Map::new()));
2480            let data: ExitPlanModeData = match serde_json::from_value(params) {
2481                Ok(d) => d,
2482                Err(e) => {
2483                    warn!(error = %e, "failed to deserialize exitPlanMode.request params, using defaults");
2484                    ExitPlanModeData::default()
2485                }
2486            };
2487
2488            let rpc_result = if let Some(exit_plan_handler) = handlers.exit_plan_mode.as_ref() {
2489                let result = exit_plan_handler.handle(sid, data).await;
2490                serde_json::to_value(result).expect("ExitPlanModeResult serialization cannot fail")
2491            } else {
2492                serde_json::json!({ "approved": true })
2493            };
2494            let rpc_response = JsonRpcResponse {
2495                jsonrpc: "2.0".to_string(),
2496                id: request.id,
2497                result: Some(rpc_result),
2498                error: None,
2499            };
2500            let _ = client.send_response(&rpc_response).await;
2501        }
2502
2503        "autoModeSwitch.request" => {
2504            let error_code = request
2505                .params
2506                .as_ref()
2507                .and_then(|p| p.get("errorCode"))
2508                .and_then(|v| v.as_str())
2509                .map(|s| s.to_string());
2510            let retry_after_seconds = request
2511                .params
2512                .as_ref()
2513                .and_then(|p| p.get("retryAfterSeconds"))
2514                .and_then(|v| v.as_f64());
2515
2516            let answer = if let Some(auto_mode_handler) = handlers.auto_mode_switch.as_ref() {
2517                auto_mode_handler
2518                    .handle(sid, error_code, retry_after_seconds)
2519                    .await
2520            } else {
2521                AutoModeSwitchResponse::No
2522            };
2523            let rpc_response = JsonRpcResponse {
2524                jsonrpc: "2.0".to_string(),
2525                id: request.id,
2526                result: Some(serde_json::json!({ "response": answer })),
2527                error: None,
2528            };
2529            let _ = client.send_response(&rpc_response).await;
2530        }
2531
2532        "systemMessage.transform" => {
2533            let params = request.params.as_ref();
2534            let sections: HashMap<String, crate::transforms::TransformSection> =
2535                match params.and_then(|p| p.get("sections")) {
2536                    Some(v) => match serde_json::from_value(v.clone()) {
2537                        Ok(s) => s,
2538                        Err(e) => {
2539                            let _ = send_error_response(
2540                                client,
2541                                request.id,
2542                                error_codes::INVALID_PARAMS,
2543                                &format!("invalid sections: {e}"),
2544                            )
2545                            .await;
2546                            return;
2547                        }
2548                    },
2549                    None => {
2550                        let _ = send_error_response(
2551                            client,
2552                            request.id,
2553                            error_codes::INVALID_PARAMS,
2554                            "missing sections parameter",
2555                        )
2556                        .await;
2557                        return;
2558                    }
2559                };
2560
2561            let rpc_result = if let Some(transforms) = transforms {
2562                let transform_start = Instant::now();
2563                let response =
2564                    crate::transforms::dispatch_transform(transforms, &sid, sections).await;
2565                tracing::debug!(
2566                    elapsed_ms = transform_start.elapsed().as_millis(),
2567                    session_id = %sid,
2568                    "SystemMessageTransform::transform_section dispatch"
2569                );
2570                match serde_json::to_value(response) {
2571                    Ok(v) => v,
2572                    Err(e) => {
2573                        warn!(error = %e, "failed to serialize transform response");
2574                        serde_json::json!({ "sections": {} })
2575                    }
2576                }
2577            } else {
2578                // No transforms registered — pass through all sections unchanged.
2579                let passthrough: HashMap<String, crate::transforms::TransformSection> = sections;
2580                serde_json::json!({ "sections": passthrough })
2581            };
2582
2583            let rpc_response = JsonRpcResponse {
2584                jsonrpc: "2.0".to_string(),
2585                id: request.id,
2586                result: Some(rpc_result),
2587                error: None,
2588            };
2589            let _ = client.send_response(&rpc_response).await;
2590        }
2591
2592        method => {
2593            warn!(
2594                method = method,
2595                "unhandled request method in session event loop"
2596            );
2597            let _ = send_error_response(
2598                client,
2599                request.id,
2600                error_codes::METHOD_NOT_FOUND,
2601                &format!("unknown method: {method}"),
2602            )
2603            .await;
2604        }
2605    }
2606}
2607
2608async fn send_error_response(
2609    client: &Client,
2610    id: u64,
2611    code: i32,
2612    message: &str,
2613) -> Result<(), Error> {
2614    let response = JsonRpcResponse {
2615        jsonrpc: "2.0".to_string(),
2616        id,
2617        result: None,
2618        error: Some(crate::JsonRpcError {
2619            code,
2620            message: message.to_string(),
2621            data: None,
2622        }),
2623    };
2624    client.send_response(&response).await
2625}
2626
2627/// Inject `action: "transform"` sections into a `SystemMessageConfig`,
2628/// forcing `mode: "customize"` (required by the CLI for transforms to fire).
2629/// Preserves any existing caller-provided section overrides.
2630fn apply_transform_sections(
2631    sys_msg: &mut SystemMessageConfig,
2632    transforms: &dyn SystemMessageTransform,
2633) {
2634    sys_msg.mode = Some("customize".to_string());
2635    let sections = sys_msg.sections.get_or_insert_with(HashMap::new);
2636    for id in transforms.section_ids() {
2637        sections.entry(id).or_insert_with(|| SectionOverride {
2638            action: Some("transform".to_string()),
2639            content: None,
2640        });
2641    }
2642}
2643
2644fn inject_transform_sections(config: &mut SessionConfig, transforms: &dyn SystemMessageTransform) {
2645    let sys_msg = config.system_message.get_or_insert_with(Default::default);
2646    apply_transform_sections(sys_msg, transforms);
2647}
2648
2649fn inject_transform_sections_resume(
2650    config: &mut ResumeSessionConfig,
2651    transforms: &dyn SystemMessageTransform,
2652) {
2653    let sys_msg = config.system_message.get_or_insert_with(Default::default);
2654    apply_transform_sections(sys_msg, transforms);
2655}
2656
2657#[cfg(test)]
2658mod tests {
2659    use serde_json::json;
2660
2661    use super::{
2662        build_mode_post_create_patch, has_managed_settings, is_autopilot_continuation_idle,
2663        permission_request_data, permission_response_params,
2664    };
2665    use crate::handler::PermissionResult;
2666    use crate::types::{
2667        PermissionDecisionContext, PermissionDecisionOutcome, PermissionDecisionSource,
2668        PermissionDecisionSurface, RequestId, SessionEvent, SessionId,
2669    };
2670
2671    #[test]
2672    fn identifies_only_autopilot_continuation_idles() {
2673        let mut event = SessionEvent {
2674            id: "event-1".to_string(),
2675            timestamp: "2026-01-01T00:00:00Z".to_string(),
2676            parent_id: None,
2677            ephemeral: None,
2678            agent_id: None,
2679            debug_cli_received_at_ms: None,
2680            debug_ws_forwarded_at_ms: None,
2681            event_type: "session.idle".to_string(),
2682            data: json!({ "mode": "autopilot" }),
2683        };
2684
2685        assert!(is_autopilot_continuation_idle(&event));
2686
2687        event.data = json!({ "mode": "interactive" });
2688        assert!(!is_autopilot_continuation_idle(&event));
2689
2690        event.data = json!({});
2691        assert!(!is_autopilot_continuation_idle(&event));
2692    }
2693
2694    #[test]
2695    fn empty_mode_post_patch_sets_empty_included_builtin_skills() {
2696        let patch =
2697            build_mode_post_create_patch(crate::ClientMode::Empty, None, None, None, None, None)
2698                .expect("empty mode always sends a patch");
2699        assert_eq!(
2700            patch.included_builtin_skills,
2701            Some(Vec::new()),
2702            "empty mode must fail closed with an empty includedBuiltinSkills list"
2703        );
2704        assert_eq!(patch.installed_plugins.as_ref().map(|p| p.len()), Some(0));
2705        // Serializes as an explicit empty array (not omitted).
2706        let value = serde_json::to_value(&patch).expect("serialize patch");
2707        assert_eq!(value["includedBuiltinSkills"], serde_json::json!([]));
2708    }
2709
2710    #[test]
2711    fn empty_mode_post_patch_preserves_explicit_builtin_skill_allowlist() {
2712        let patch = build_mode_post_create_patch(
2713            crate::ClientMode::Empty,
2714            Some(false),
2715            Some(false),
2716            Some(true),
2717            Some(true),
2718            Some(vec!["code-review".to_string()]),
2719        )
2720        .expect("empty mode always sends a patch");
2721        assert_eq!(
2722            patch.included_builtin_skills,
2723            Some(vec!["code-review".to_string()])
2724        );
2725    }
2726
2727    #[test]
2728    fn copilot_cli_mode_does_not_inject_included_builtin_skills() {
2729        // No fields set -> no patch at all.
2730        assert!(
2731            build_mode_post_create_patch(
2732                crate::ClientMode::CopilotCli,
2733                None,
2734                None,
2735                None,
2736                None,
2737                None
2738            )
2739            .is_none()
2740        );
2741        // A field set -> patch sent, but skills field stays absent.
2742        let patch = build_mode_post_create_patch(
2743            crate::ClientMode::CopilotCli,
2744            Some(true),
2745            None,
2746            None,
2747            None,
2748            None,
2749        )
2750        .expect("a set field triggers a patch");
2751        assert_eq!(patch.included_builtin_skills, None);
2752        assert!(patch.installed_plugins.is_none());
2753        let value = serde_json::to_value(&patch).expect("serialize patch");
2754        assert!(value.get("includedBuiltinSkills").is_none());
2755
2756        let patch = build_mode_post_create_patch(
2757            crate::ClientMode::CopilotCli,
2758            None,
2759            None,
2760            None,
2761            None,
2762            Some(vec!["code-review".to_string()]),
2763        )
2764        .expect("an explicit allowlist triggers a patch");
2765        assert_eq!(
2766            patch.included_builtin_skills,
2767            Some(vec!["code-review".to_string()])
2768        );
2769    }
2770
2771    #[test]
2772    fn direct_injection_enables_managed_safeguards() {
2773        let settings = crate::types::ManagedSettings::default();
2774        assert!(has_managed_settings(None, Some(&settings)));
2775        assert!(!has_managed_settings(None, None));
2776    }
2777
2778    fn attribution_context() -> PermissionDecisionContext {
2779        PermissionDecisionContext {
2780            outcome: PermissionDecisionOutcome::AutoApproved,
2781            response_capability: None,
2782            source: PermissionDecisionSource::AssistedApproval,
2783            surface: PermissionDecisionSurface::CopilotApp,
2784        }
2785    }
2786
2787    #[test]
2788    fn response_params_omit_decision_context_without_attribution() {
2789        for (result, expected) in [
2790            (
2791                PermissionResult::approve_once(),
2792                json!({ "kind": "approve-once" }),
2793            ),
2794            (PermissionResult::reject(None), json!({ "kind": "reject" })),
2795            (
2796                PermissionResult::reject(Some("bad".to_string())),
2797                json!({ "kind": "reject", "feedback": "bad" }),
2798            ),
2799            (
2800                PermissionResult::user_not_available(),
2801                json!({ "kind": "user-not-available" }),
2802            ),
2803        ] {
2804            let params = permission_response_params(
2805                &SessionId::from("session-1"),
2806                &RequestId::from("permission-1"),
2807                &result,
2808            )
2809            .unwrap();
2810            assert_eq!(
2811                params,
2812                json!({
2813                    "sessionId": "session-1",
2814                    "requestId": "permission-1",
2815                    "result": expected,
2816                })
2817            );
2818        }
2819    }
2820
2821    #[test]
2822    fn response_params_forward_decision_context_alongside_result() {
2823        let params = permission_response_params(
2824            &SessionId::from("session-1"),
2825            &RequestId::from("permission-1"),
2826            &PermissionResult::approve_once().with_context(attribution_context()),
2827        )
2828        .unwrap();
2829        assert_eq!(
2830            params,
2831            json!({
2832                "sessionId": "session-1",
2833                "requestId": "permission-1",
2834                "result": { "kind": "approve-once" },
2835                "decisionContext": {
2836                    "outcome": "auto_approved",
2837                    "source": "assisted_approval",
2838                    "surface": "copilot_app",
2839                },
2840            })
2841        );
2842        // The context is a sibling of `result`, never nested inside it.
2843        assert!(params["result"].get("decisionContext").is_none());
2844    }
2845
2846    #[test]
2847    fn response_params_suppressed_for_no_result() {
2848        assert!(
2849            permission_response_params(
2850                &SessionId::from("session-1"),
2851                &RequestId::from("permission-1"),
2852                &PermissionResult::NoResult,
2853            )
2854            .is_none()
2855        );
2856    }
2857
2858    #[test]
2859    fn with_context_is_a_no_op_on_no_result() {
2860        let result = PermissionResult::no_result().with_context(attribution_context());
2861        assert!(matches!(result, PermissionResult::NoResult));
2862    }
2863
2864    #[test]
2865    fn with_context_replaces_rather_than_nests() {
2866        let result = PermissionResult::approve_once()
2867            .with_context(attribution_context())
2868            .with_context(PermissionDecisionContext {
2869                outcome: PermissionDecisionOutcome::PromptedUser,
2870                response_capability: None,
2871                source: PermissionDecisionSource::HumanResponse,
2872                surface: PermissionDecisionSurface::Sdk,
2873            });
2874        let params = permission_response_params(
2875            &SessionId::from("session-1"),
2876            &RequestId::from("permission-1"),
2877            &result,
2878        )
2879        .unwrap();
2880        assert_eq!(
2881            params["decisionContext"],
2882            json!({
2883                "outcome": "prompted_user",
2884                "source": "human_response",
2885                "surface": "sdk",
2886            })
2887        );
2888    }
2889
2890    #[test]
2891    fn permission_request_data_reads_nested_managed_approval_metadata() {
2892        let data = permission_request_data(
2893            &json!({
2894                "requestId": "permission-1",
2895                "permissionRequest": {
2896                    "kind": "read",
2897                    "managedApprovalRequired": true,
2898                    "path": "/workspace/file.txt"
2899                }
2900            }),
2901            false,
2902        );
2903
2904        assert_eq!(data.managed_approval_required, Some(true));
2905        assert_eq!(
2906            data.extra["permissionRequest"]["path"],
2907            "/workspace/file.txt"
2908        );
2909    }
2910
2911    #[test]
2912    fn permission_request_data_preserves_managed_flag_when_other_fields_are_malformed() {
2913        let data = permission_request_data(
2914            &json!({
2915                "requestId": "permission-1",
2916                "permissionRequest": {
2917                    "kind": "read",
2918                    "managedApprovalRequired": true,
2919                    "toolCallId": 42
2920                }
2921            }),
2922            false,
2923        );
2924
2925        assert_eq!(data.managed_approval_required, Some(true));
2926        assert_eq!(data.extra["requestId"], "permission-1");
2927    }
2928
2929    #[test]
2930    fn permission_request_data_fails_closed_for_malformed_managed_flag() {
2931        let data = permission_request_data(
2932            &json!({
2933                "requestId": "permission-1",
2934                "permissionRequest": {
2935                    "kind": "read",
2936                    "managedApprovalRequired": "yes",
2937                    "path": "/workspace/file.txt"
2938                }
2939            }),
2940            false,
2941        );
2942
2943        assert_eq!(data.managed_approval_required, Some(true));
2944    }
2945
2946    #[test]
2947    fn permission_request_data_preserves_valid_false_managed_flag() {
2948        let data = permission_request_data(
2949            &json!({
2950                "requestId": "permission-1",
2951                "permissionRequest": {
2952                    "kind": "read",
2953                    "managedApprovalRequired": false,
2954                    "path": "/workspace/file.txt"
2955                }
2956            }),
2957            false,
2958        );
2959
2960        assert_eq!(data.managed_approval_required, Some(false));
2961    }
2962}