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