Skip to main content

claude_codes/io/
message_types.rs

1use serde::{Deserialize, Deserializer, Serialize, Serializer};
2use serde_json::Value;
3use std::fmt;
4use uuid::Uuid;
5
6use super::claude_output::ClaudeOutput;
7use super::content_blocks::{deserialize_content_blocks, ContentBlock};
8use super::rate_limit::RateLimitInfo;
9
10/// Known system message subtypes.
11///
12/// The Claude CLI emits system messages with a `subtype` field indicating what
13/// kind of system event occurred. This enum captures the known subtypes while
14/// preserving unknown values via the `Unknown` variant for forward compatibility.
15#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16pub enum SystemSubtype {
17    Init,
18    Status,
19    CompactBoundary,
20    ThinkingTokens,
21    TaskStarted,
22    TaskProgress,
23    TaskUpdated,
24    TaskNotification,
25    ApiRetry,
26    ControlRequestProgress,
27    ModelRefusalFallback,
28    ModelRefusalNoFallback,
29    LocalCommandOutput,
30    HookStarted,
31    HookProgress,
32    HookResponse,
33    PluginInstall,
34    BackgroundTasksChanged,
35    SessionStateChanged,
36    WorkerShuttingDown,
37    CommandsChanged,
38    Notification,
39    FilesPersisted,
40    MemoryRecall,
41    ElicitationComplete,
42    PermissionDenied,
43    MirrorError,
44    Informational,
45    CodeChangePublished,
46    VcsStateChanged,
47    FeedbackDraftQueued,
48    CloudSessionDelta,
49    DevIntent,
50    TurnHandoffAvailable,
51    TurnPreempted,
52    PeerMessageHold,
53    PerTurnEffortChanged,
54    SessionTitleChanged,
55    InstructionSizeWarning,
56    FileAttachmentsMissing,
57    PermissionCheckStatus,
58    UiFocus,
59    UiInvalidate,
60    UiLog,
61    UiPanes,
62    UiScroll,
63    UiStatus,
64    UiToast,
65    /// A subtype not yet known to this version of the crate.
66    Unknown(String),
67}
68
69impl SystemSubtype {
70    pub fn as_str(&self) -> &str {
71        match self {
72            Self::Init => "init",
73            Self::Status => "status",
74            Self::CompactBoundary => "compact_boundary",
75            Self::ThinkingTokens => "thinking_tokens",
76            Self::TaskStarted => "task_started",
77            Self::TaskProgress => "task_progress",
78            Self::TaskUpdated => "task_updated",
79            Self::TaskNotification => "task_notification",
80            Self::ApiRetry => "api_retry",
81            Self::ControlRequestProgress => "control_request_progress",
82            Self::ModelRefusalFallback => "model_refusal_fallback",
83            Self::ModelRefusalNoFallback => "model_refusal_no_fallback",
84            Self::LocalCommandOutput => "local_command_output",
85            Self::HookStarted => "hook_started",
86            Self::HookProgress => "hook_progress",
87            Self::HookResponse => "hook_response",
88            Self::PluginInstall => "plugin_install",
89            Self::BackgroundTasksChanged => "background_tasks_changed",
90            Self::SessionStateChanged => "session_state_changed",
91            Self::WorkerShuttingDown => "worker_shutting_down",
92            Self::CommandsChanged => "commands_changed",
93            Self::Notification => "notification",
94            Self::FilesPersisted => "files_persisted",
95            Self::MemoryRecall => "memory_recall",
96            Self::ElicitationComplete => "elicitation_complete",
97            Self::PermissionDenied => "permission_denied",
98            Self::MirrorError => "mirror_error",
99            Self::Informational => "informational",
100            Self::CodeChangePublished => "code_change_published",
101            Self::VcsStateChanged => "vcs_state_changed",
102            Self::FeedbackDraftQueued => "feedback_draft_queued",
103            Self::CloudSessionDelta => "cloud_session_delta",
104            Self::DevIntent => "dev_intent",
105            Self::TurnHandoffAvailable => "turn_handoff_available",
106            Self::TurnPreempted => "turn_preempted",
107            Self::PeerMessageHold => "peer_message_hold",
108            Self::PerTurnEffortChanged => "per_turn_effort_changed",
109            Self::SessionTitleChanged => "session_title_changed",
110            Self::InstructionSizeWarning => "instruction_size_warning",
111            Self::FileAttachmentsMissing => "file_attachments_missing",
112            Self::PermissionCheckStatus => "permission_check_status",
113            Self::UiFocus => "ui_focus",
114            Self::UiInvalidate => "ui_invalidate",
115            Self::UiLog => "ui_log",
116            Self::UiPanes => "ui_panes",
117            Self::UiScroll => "ui_scroll",
118            Self::UiStatus => "ui_status",
119            Self::UiToast => "ui_toast",
120            Self::Unknown(s) => s.as_str(),
121        }
122    }
123}
124
125impl fmt::Display for SystemSubtype {
126    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127        f.write_str(self.as_str())
128    }
129}
130
131impl From<&str> for SystemSubtype {
132    fn from(s: &str) -> Self {
133        match s {
134            "init" => Self::Init,
135            "status" => Self::Status,
136            "compact_boundary" => Self::CompactBoundary,
137            "thinking_tokens" => Self::ThinkingTokens,
138            "task_started" => Self::TaskStarted,
139            "task_progress" => Self::TaskProgress,
140            "task_updated" => Self::TaskUpdated,
141            "task_notification" => Self::TaskNotification,
142            "api_retry" => Self::ApiRetry,
143            "control_request_progress" => Self::ControlRequestProgress,
144            "model_refusal_fallback" => Self::ModelRefusalFallback,
145            "model_refusal_no_fallback" => Self::ModelRefusalNoFallback,
146            "local_command_output" => Self::LocalCommandOutput,
147            "hook_started" => Self::HookStarted,
148            "hook_progress" => Self::HookProgress,
149            "hook_response" => Self::HookResponse,
150            "plugin_install" => Self::PluginInstall,
151            "background_tasks_changed" => Self::BackgroundTasksChanged,
152            "session_state_changed" => Self::SessionStateChanged,
153            "worker_shutting_down" => Self::WorkerShuttingDown,
154            "commands_changed" => Self::CommandsChanged,
155            "notification" => Self::Notification,
156            "files_persisted" => Self::FilesPersisted,
157            "memory_recall" => Self::MemoryRecall,
158            "elicitation_complete" => Self::ElicitationComplete,
159            "permission_denied" => Self::PermissionDenied,
160            "mirror_error" => Self::MirrorError,
161            "informational" => Self::Informational,
162            "code_change_published" => Self::CodeChangePublished,
163            "vcs_state_changed" => Self::VcsStateChanged,
164            "feedback_draft_queued" => Self::FeedbackDraftQueued,
165            "cloud_session_delta" => Self::CloudSessionDelta,
166            "dev_intent" => Self::DevIntent,
167            "turn_handoff_available" => Self::TurnHandoffAvailable,
168            "turn_preempted" => Self::TurnPreempted,
169            "peer_message_hold" => Self::PeerMessageHold,
170            "per_turn_effort_changed" => Self::PerTurnEffortChanged,
171            "session_title_changed" => Self::SessionTitleChanged,
172            "instruction_size_warning" => Self::InstructionSizeWarning,
173            "file_attachments_missing" => Self::FileAttachmentsMissing,
174            "permission_check_status" => Self::PermissionCheckStatus,
175            "ui_focus" => Self::UiFocus,
176            "ui_invalidate" => Self::UiInvalidate,
177            "ui_log" => Self::UiLog,
178            "ui_panes" => Self::UiPanes,
179            "ui_scroll" => Self::UiScroll,
180            "ui_status" => Self::UiStatus,
181            "ui_toast" => Self::UiToast,
182            other => Self::Unknown(other.to_string()),
183        }
184    }
185}
186
187impl Serialize for SystemSubtype {
188    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
189        serializer.serialize_str(self.as_str())
190    }
191}
192
193impl<'de> Deserialize<'de> for SystemSubtype {
194    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
195        let s = String::deserialize(deserializer)?;
196        Ok(Self::from(s.as_str()))
197    }
198}
199
200/// Known message roles.
201///
202/// Used in `MessageContent` and `AssistantMessageContent` to indicate the
203/// speaker of a message.
204#[derive(Debug, Clone, PartialEq, Eq, Hash)]
205pub enum MessageRole {
206    User,
207    Assistant,
208    /// A role not yet known to this version of the crate.
209    Unknown(String),
210}
211
212impl MessageRole {
213    pub fn as_str(&self) -> &str {
214        match self {
215            Self::User => "user",
216            Self::Assistant => "assistant",
217            Self::Unknown(s) => s.as_str(),
218        }
219    }
220}
221
222impl fmt::Display for MessageRole {
223    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
224        f.write_str(self.as_str())
225    }
226}
227
228impl From<&str> for MessageRole {
229    fn from(s: &str) -> Self {
230        match s {
231            "user" => Self::User,
232            "assistant" => Self::Assistant,
233            other => Self::Unknown(other.to_string()),
234        }
235    }
236}
237
238impl Serialize for MessageRole {
239    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
240        serializer.serialize_str(self.as_str())
241    }
242}
243
244impl<'de> Deserialize<'de> for MessageRole {
245    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
246        let s = String::deserialize(deserializer)?;
247        Ok(Self::from(s.as_str()))
248    }
249}
250
251/// What triggered a context compaction.
252#[derive(Debug, Clone, PartialEq, Eq, Hash)]
253pub enum CompactionTrigger {
254    /// Automatic compaction triggered by token limit.
255    Auto,
256    /// User-initiated compaction (e.g., /compact command).
257    Manual,
258    /// A trigger not yet known to this version of the crate.
259    Unknown(String),
260}
261
262impl CompactionTrigger {
263    pub fn as_str(&self) -> &str {
264        match self {
265            Self::Auto => "auto",
266            Self::Manual => "manual",
267            Self::Unknown(s) => s.as_str(),
268        }
269    }
270}
271
272impl fmt::Display for CompactionTrigger {
273    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
274        f.write_str(self.as_str())
275    }
276}
277
278impl From<&str> for CompactionTrigger {
279    fn from(s: &str) -> Self {
280        match s {
281            "auto" => Self::Auto,
282            "manual" => Self::Manual,
283            other => Self::Unknown(other.to_string()),
284        }
285    }
286}
287
288impl Serialize for CompactionTrigger {
289    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
290        serializer.serialize_str(self.as_str())
291    }
292}
293
294impl<'de> Deserialize<'de> for CompactionTrigger {
295    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
296        let s = String::deserialize(deserializer)?;
297        Ok(Self::from(s.as_str()))
298    }
299}
300
301/// Reason why the assistant stopped generating.
302#[derive(Debug, Clone, PartialEq, Eq, Hash)]
303pub enum StopReason {
304    /// The assistant reached a natural end of its turn.
305    EndTurn,
306    /// The response hit the maximum token limit.
307    MaxTokens,
308    /// The assistant wants to use a tool.
309    ToolUse,
310    /// A stop reason not yet known to this version of the crate.
311    Unknown(String),
312}
313
314impl StopReason {
315    pub fn as_str(&self) -> &str {
316        match self {
317            Self::EndTurn => "end_turn",
318            Self::MaxTokens => "max_tokens",
319            Self::ToolUse => "tool_use",
320            Self::Unknown(s) => s.as_str(),
321        }
322    }
323}
324
325impl fmt::Display for StopReason {
326    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
327        f.write_str(self.as_str())
328    }
329}
330
331impl From<&str> for StopReason {
332    fn from(s: &str) -> Self {
333        match s {
334            "end_turn" => Self::EndTurn,
335            "max_tokens" => Self::MaxTokens,
336            "tool_use" => Self::ToolUse,
337            other => Self::Unknown(other.to_string()),
338        }
339    }
340}
341
342impl Serialize for StopReason {
343    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
344        serializer.serialize_str(self.as_str())
345    }
346}
347
348impl<'de> Deserialize<'de> for StopReason {
349    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
350        let s = String::deserialize(deserializer)?;
351        Ok(Self::from(s.as_str()))
352    }
353}
354
355/// How the API key was sourced for the session.
356#[derive(Debug, Clone, PartialEq, Eq, Hash)]
357pub enum ApiKeySource {
358    /// No API key provided.
359    None,
360    User,
361    Project,
362    Org,
363    Temporary,
364    Oauth,
365    /// A source not yet known to this version of the crate.
366    Unknown(String),
367}
368
369impl ApiKeySource {
370    pub fn as_str(&self) -> &str {
371        match self {
372            Self::None => "none",
373            Self::User => "user",
374            Self::Project => "project",
375            Self::Org => "org",
376            Self::Temporary => "temporary",
377            Self::Oauth => "oauth",
378            Self::Unknown(s) => s.as_str(),
379        }
380    }
381}
382
383impl fmt::Display for ApiKeySource {
384    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
385        f.write_str(self.as_str())
386    }
387}
388
389impl From<&str> for ApiKeySource {
390    fn from(s: &str) -> Self {
391        match s {
392            "none" => Self::None,
393            "user" => Self::User,
394            "project" => Self::Project,
395            "org" => Self::Org,
396            "temporary" => Self::Temporary,
397            "oauth" => Self::Oauth,
398            other => Self::Unknown(other.to_string()),
399        }
400    }
401}
402
403impl Serialize for ApiKeySource {
404    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
405        serializer.serialize_str(self.as_str())
406    }
407}
408
409impl<'de> Deserialize<'de> for ApiKeySource {
410    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
411        let s = String::deserialize(deserializer)?;
412        Ok(Self::from(s.as_str()))
413    }
414}
415
416/// Output formatting style for the session.
417#[derive(Debug, Clone, PartialEq, Eq, Hash)]
418pub enum OutputStyle {
419    /// Default output style.
420    Default,
421    /// A style not yet known to this version of the crate.
422    Unknown(String),
423}
424
425impl OutputStyle {
426    pub fn as_str(&self) -> &str {
427        match self {
428            Self::Default => "default",
429            Self::Unknown(s) => s.as_str(),
430        }
431    }
432}
433
434impl fmt::Display for OutputStyle {
435    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
436        f.write_str(self.as_str())
437    }
438}
439
440impl From<&str> for OutputStyle {
441    fn from(s: &str) -> Self {
442        match s {
443            "default" => Self::Default,
444            other => Self::Unknown(other.to_string()),
445        }
446    }
447}
448
449impl Serialize for OutputStyle {
450    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
451        serializer.serialize_str(self.as_str())
452    }
453}
454
455impl<'de> Deserialize<'de> for OutputStyle {
456    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
457        let s = String::deserialize(deserializer)?;
458        Ok(Self::from(s.as_str()))
459    }
460}
461
462/// Permission mode reported in init messages.
463#[derive(Debug, Clone, PartialEq, Eq, Hash)]
464pub enum InitPermissionMode {
465    /// Default permission mode.
466    Default,
467    AcceptEdits,
468    BypassPermissions,
469    Plan,
470    DontAsk,
471    Auto,
472    /// A mode not yet known to this version of the crate.
473    Unknown(String),
474}
475
476impl InitPermissionMode {
477    pub fn as_str(&self) -> &str {
478        match self {
479            Self::Default => "default",
480            Self::AcceptEdits => "acceptEdits",
481            Self::BypassPermissions => "bypassPermissions",
482            Self::Plan => "plan",
483            Self::DontAsk => "dontAsk",
484            Self::Auto => "auto",
485            Self::Unknown(s) => s.as_str(),
486        }
487    }
488}
489
490impl fmt::Display for InitPermissionMode {
491    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
492        f.write_str(self.as_str())
493    }
494}
495
496impl From<&str> for InitPermissionMode {
497    fn from(s: &str) -> Self {
498        match s {
499            "default" => Self::Default,
500            "acceptEdits" => Self::AcceptEdits,
501            "bypassPermissions" => Self::BypassPermissions,
502            "plan" => Self::Plan,
503            "dontAsk" => Self::DontAsk,
504            "auto" => Self::Auto,
505            other => Self::Unknown(other.to_string()),
506        }
507    }
508}
509
510impl Serialize for InitPermissionMode {
511    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
512        serializer.serialize_str(self.as_str())
513    }
514}
515
516impl<'de> Deserialize<'de> for InitPermissionMode {
517    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
518        let s = String::deserialize(deserializer)?;
519        Ok(Self::from(s.as_str()))
520    }
521}
522
523/// Status of an ongoing operation (e.g., context compaction).
524#[derive(Debug, Clone, PartialEq, Eq, Hash)]
525pub enum StatusMessageStatus {
526    /// Context compaction is in progress.
527    Compacting,
528    /// The CLI is issuing a request.
529    Requesting,
530    /// A status not yet known to this version of the crate.
531    Unknown(String),
532}
533
534impl StatusMessageStatus {
535    pub fn as_str(&self) -> &str {
536        match self {
537            Self::Compacting => "compacting",
538            Self::Requesting => "requesting",
539            Self::Unknown(s) => s.as_str(),
540        }
541    }
542}
543
544impl fmt::Display for StatusMessageStatus {
545    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
546        f.write_str(self.as_str())
547    }
548}
549
550impl From<&str> for StatusMessageStatus {
551    fn from(s: &str) -> Self {
552        match s {
553            "compacting" => Self::Compacting,
554            "requesting" => Self::Requesting,
555            other => Self::Unknown(other.to_string()),
556        }
557    }
558}
559
560impl Serialize for StatusMessageStatus {
561    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
562        serializer.serialize_str(self.as_str())
563    }
564}
565
566impl<'de> Deserialize<'de> for StatusMessageStatus {
567    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
568        let s = String::deserialize(deserializer)?;
569        Ok(Self::from(s.as_str()))
570    }
571}
572
573/// Serialize an optional UUID as a string
574pub(crate) fn serialize_optional_uuid<S>(
575    uuid: &Option<Uuid>,
576    serializer: S,
577) -> Result<S::Ok, S::Error>
578where
579    S: Serializer,
580{
581    match uuid {
582        Some(id) => serializer.serialize_str(&id.to_string()),
583        None => serializer.serialize_none(),
584    }
585}
586
587/// Deserialize an optional UUID from a string
588pub(crate) fn deserialize_optional_uuid<'de, D>(deserializer: D) -> Result<Option<Uuid>, D::Error>
589where
590    D: Deserializer<'de>,
591{
592    let opt_str: Option<String> = Option::deserialize(deserializer)?;
593    match opt_str {
594        Some(s) => Uuid::parse_str(&s)
595            .map(Some)
596            .map_err(serde::de::Error::custom),
597        None => Ok(None),
598    }
599}
600
601/// Message provenance. The `kind` field is the stable discriminator; variant
602/// specific fields are preserved in `extra` for forward-compatible access.
603#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
604pub struct MessageOrigin {
605    pub kind: String,
606    #[serde(flatten)]
607    pub extra: serde_json::Map<String, Value>,
608}
609
610/// Metadata attached when user-visible transcript content summarizes prior messages.
611#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
612pub struct SummarizeMetadata {
613    pub messages_summarized: u64,
614    #[serde(default, skip_serializing_if = "Option::is_none")]
615    pub user_context: Option<String>,
616    #[serde(default, skip_serializing_if = "Option::is_none")]
617    pub direction: Option<String>,
618}
619
620/// MCP metadata passed through on user-message wrappers.
621#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
622pub struct McpMeta {
623    #[serde(default, skip_serializing_if = "Option::is_none", rename = "_meta")]
624    pub meta: Option<Value>,
625    #[serde(default, skip_serializing_if = "Option::is_none")]
626    pub structured_content: Option<Value>,
627    /// The `resource_link` content blocks the MCP tool returned, collected
628    /// from the raw result before the CLI rewrites each into the
629    /// `[Resource link: NAME] URI` text line the model reads. At most 50
630    /// links and 64 KiB serialized; absent when the result had none.
631    #[serde(default, skip_serializing_if = "Vec::is_empty")]
632    pub resource_links: Vec<ResourceLink>,
633}
634
635/// A file an MCP tool returned by reference — a `resource_link` content block
636/// as carried on [`McpMeta::resource_links`] and
637/// [`TaskNotificationMessage::resource_links`] (CLI 2.1.259+).
638#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
639pub struct ResourceLink {
640    pub uri: String,
641    pub name: String,
642    #[serde(default, skip_serializing_if = "Option::is_none")]
643    pub title: Option<String>,
644    #[serde(default, skip_serializing_if = "Option::is_none")]
645    pub description: Option<String>,
646    #[serde(default, skip_serializing_if = "Option::is_none", rename = "mimeType")]
647    pub mime_type: Option<String>,
648    #[serde(default, skip_serializing_if = "Option::is_none")]
649    pub size: Option<u64>,
650    #[serde(default, skip_serializing_if = "Option::is_none")]
651    pub annotations: Option<Value>,
652}
653
654/// Display metadata for a `tool_result` block carried on the user wrapper.
655#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
656pub struct ToolResultMeta {
657    /// The `tool_use_id` of the matching `tool_result` block.
658    pub id: String,
659    /// Harness-stamped reason an `is_error: true` result did not carry the
660    /// tool's own execution output (`user-rejected`, `permission-rule`,
661    /// `automode-*`, `interrupted`, `cancelled`). Open set — treat
662    /// unrecognized values as valid reasons; absent means the tool ran to
663    /// completion. Optional since CLI 2.1.276, when an entry may carry a
664    /// [`remedy`](Self::remedy) alone.
665    #[serde(default, skip_serializing_if = "Option::is_none")]
666    pub non_execution_kind: Option<String>,
667    /// The deny comment a human typed at a permission prompt, when present.
668    #[serde(default, skip_serializing_if = "Option::is_none")]
669    pub user_feedback: Option<String>,
670    /// The fix a host can offer for what the result reports (CLI 2.1.276+).
671    /// May be present on a result that did run (a staged settings write, a
672    /// sandbox violation on exit 0), so read whether the call ran from
673    /// [`non_execution_kind`](Self::non_execution_kind), not from this.
674    #[serde(default, skip_serializing_if = "Option::is_none")]
675    pub remedy: Option<ToolResultRemedy>,
676    /// How the call's permission check ended (CLI 2.1.293+). On a
677    /// subagent's messages (`parent_tool_use_id` set) entries carry only
678    /// this, so those messages do not say whether a call ran. Absent when
679    /// the call ended before its permission check, and on some results
680    /// built apart from the call's own run (a plugin hook's refusal, a
681    /// hook's rewrite of a denied result, a queued call a stop cancelled).
682    #[serde(default, skip_serializing_if = "Option::is_none")]
683    pub permission_decision: Option<ToolPermissionDecision>,
684}
685
686/// [`ToolResultMeta::permission_decision`] — how a tool call's permission
687/// check ended, as Claude Code's `claude_code.tool.blocked_on_user` trace
688/// span reports it (CLI 2.1.293+).
689#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
690pub struct ToolPermissionDecision {
691    /// The outcome.
692    pub decision: PermissionDecisionOutcome,
693    /// What decided: `config`, `classifier`, `hook`, `user_temporary`,
694    /// `user_permanent`, `user_reject`, `user_abort`, or `invalid_input`,
695    /// `permission_updated_input_invalid` or `server_fallback_tombstone`
696    /// when bad input or the server ended the wait. In SDK sessions a rule
697    /// for this session or in the user's own settings reports
698    /// `user_temporary`, `user_permanent` or `user_reject` and other rules
699    /// report `config`; interactive sessions report every rule as `config`.
700    pub source: String,
701    /// The kind of reason the check gave when it decided by itself, such as
702    /// `rule` whenever a rule decided, or `classifier` for an auto-mode
703    /// allow (which reports source `config`). Absent when a person or the
704    /// SDK host answered, when the outcome was still a question, and for
705    /// `user_abort` and the last three sources above.
706    #[serde(default, skip_serializing_if = "Option::is_none")]
707    pub reason_type: Option<PermissionDecisionReasonType>,
708}
709
710/// [`ToolPermissionDecision::decision`].
711#[derive(Debug, Clone, PartialEq, Eq, Hash)]
712pub enum PermissionDecisionOutcome {
713    /// The call was allowed.
714    Accept,
715    /// The call was denied.
716    Reject,
717    /// The permission wait was cancelled.
718    Cancelled,
719    /// An outcome not yet known to this version of the crate.
720    Unknown(String),
721}
722
723impl PermissionDecisionOutcome {
724    pub fn as_str(&self) -> &str {
725        match self {
726            Self::Accept => "accept",
727            Self::Reject => "reject",
728            Self::Cancelled => "cancelled",
729            Self::Unknown(s) => s.as_str(),
730        }
731    }
732}
733
734impl fmt::Display for PermissionDecisionOutcome {
735    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
736        f.write_str(self.as_str())
737    }
738}
739
740impl From<&str> for PermissionDecisionOutcome {
741    fn from(s: &str) -> Self {
742        match s {
743            "accept" => Self::Accept,
744            "reject" => Self::Reject,
745            "cancelled" => Self::Cancelled,
746            other => Self::Unknown(other.to_string()),
747        }
748    }
749}
750
751impl Serialize for PermissionDecisionOutcome {
752    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
753        serializer.serialize_str(self.as_str())
754    }
755}
756
757impl<'de> Deserialize<'de> for PermissionDecisionOutcome {
758    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
759        let s = String::deserialize(deserializer)?;
760        Ok(Self::from(s.as_str()))
761    }
762}
763
764/// [`ToolPermissionDecision::reason_type`] — the kind of reason a
765/// self-decided permission check gave.
766#[derive(Debug, Clone, PartialEq, Eq, Hash)]
767pub enum PermissionDecisionReasonType {
768    /// A permission rule decided.
769    Rule,
770    /// The permission mode decided.
771    Mode,
772    /// The results of a compound command's subcommands decided.
773    SubcommandResults,
774    /// The `--permission-prompt-tool` decided.
775    PermissionPromptTool,
776    /// A hook decided.
777    Hook,
778    /// An async agent's permission handling decided.
779    AsyncAgent,
780    /// A sandbox override decided.
781    SandboxOverride,
782    /// The working-directory check decided.
783    WorkingDir,
784    /// A safety check decided.
785    SafetyCheck,
786    /// The auto-mode classifier decided.
787    Classifier,
788    /// Some other reason.
789    Other,
790    /// A reason type not yet known to this version of the crate.
791    Unknown(String),
792}
793
794impl PermissionDecisionReasonType {
795    pub fn as_str(&self) -> &str {
796        match self {
797            Self::Rule => "rule",
798            Self::Mode => "mode",
799            Self::SubcommandResults => "subcommandResults",
800            Self::PermissionPromptTool => "permissionPromptTool",
801            Self::Hook => "hook",
802            Self::AsyncAgent => "asyncAgent",
803            Self::SandboxOverride => "sandboxOverride",
804            Self::WorkingDir => "workingDir",
805            Self::SafetyCheck => "safetyCheck",
806            Self::Classifier => "classifier",
807            Self::Other => "other",
808            Self::Unknown(s) => s.as_str(),
809        }
810    }
811}
812
813impl fmt::Display for PermissionDecisionReasonType {
814    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
815        f.write_str(self.as_str())
816    }
817}
818
819impl From<&str> for PermissionDecisionReasonType {
820    fn from(s: &str) -> Self {
821        match s {
822            "rule" => Self::Rule,
823            "mode" => Self::Mode,
824            "subcommandResults" => Self::SubcommandResults,
825            "permissionPromptTool" => Self::PermissionPromptTool,
826            "hook" => Self::Hook,
827            "asyncAgent" => Self::AsyncAgent,
828            "sandboxOverride" => Self::SandboxOverride,
829            "workingDir" => Self::WorkingDir,
830            "safetyCheck" => Self::SafetyCheck,
831            "classifier" => Self::Classifier,
832            "other" => Self::Other,
833            other => Self::Unknown(other.to_string()),
834        }
835    }
836}
837
838impl Serialize for PermissionDecisionReasonType {
839    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
840        serializer.serialize_str(self.as_str())
841    }
842}
843
844impl<'de> Deserialize<'de> for PermissionDecisionReasonType {
845    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
846        let s = String::deserialize(deserializer)?;
847        Ok(Self::from(s.as_str()))
848    }
849}
850
851/// The fix a host can offer for what a `tool_result` reports, stamped from
852/// structured producer state (never parsed from the result text), carried
853/// as [`ToolResultMeta::remedy`] (CLI 2.1.276+). Only the parameters the
854/// [`kind`](Self::kind) needs are present.
855#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
856pub struct ToolResultRemedy {
857    /// What the host can fix. New kinds are additive — treat
858    /// [`ToolResultRemedyKind::Unknown`] as no remedy.
859    pub kind: ToolResultRemedyKind,
860    /// `mcp_needs_auth` / `mcp_disabled`: the server names as configured.
861    /// `mcp_required_missing`: the name patterns the agent definition
862    /// requires that no connected server matches.
863    #[serde(default, skip_serializing_if = "Option::is_none")]
864    pub servers: Option<Vec<String>>,
865    /// `auth_expired` / `auth_overridden` / `auth_missing_scope` /
866    /// `design_needs_authorization`: whose login the fix names.
867    #[serde(default, skip_serializing_if = "Option::is_none")]
868    pub provider: Option<RemedyLoginProvider>,
869    /// `outside_reads_blocked`: the path the read block refused, when the
870    /// refusal names one. `staged_for_review`: the settings file the write
871    /// was held for.
872    #[serde(default, skip_serializing_if = "Option::is_none")]
873    pub path: Option<String>,
874    /// `outside_reads_blocked`: an organization policy sets the block, so
875    /// the user cannot remove it. `auth_overridden`: the overriding
876    /// credential was injected by the host environment, so nothing in this
877    /// session can unset it.
878    #[serde(default, skip_serializing_if = "Option::is_none")]
879    pub managed: Option<bool>,
880    /// `feature_disabled` / `policy_denied`: the feature that is off.
881    #[serde(default, skip_serializing_if = "Option::is_none")]
882    pub feature: Option<RemedyFeature>,
883    /// `feature_disabled`: why the feature is off.
884    #[serde(default, skip_serializing_if = "Option::is_none")]
885    pub cause: Option<RemedyFeatureCause>,
886    /// `policy_denied`: the verdict.
887    #[serde(default, skip_serializing_if = "Option::is_none")]
888    pub policy_kind: Option<RemedyPolicyKind>,
889}
890
891/// What a host can fix for a tool result ([`ToolResultRemedy::kind`]).
892#[derive(Debug, Clone, PartialEq, Eq, Hash)]
893pub enum ToolResultRemedyKind {
894    /// An MCP server needs its login completed.
895    McpNeedsAuth,
896    /// An MCP server is disabled in settings.
897    McpDisabled,
898    /// The agent definition requires MCP servers that are not connected.
899    McpRequiredMissing,
900    /// The named login has expired.
901    AuthExpired,
902    /// A host-injected credential overrides the named login.
903    AuthOverridden,
904    /// The named login lacks a scope the call needed.
905    AuthMissingScope,
906    /// Claude Design needs its own authorization.
907    DesignNeedsAuthorization,
908    /// The command violated the sandbox policy.
909    SandboxViolation,
910    /// A read outside the working directories was refused.
911    OutsideReadsBlocked,
912    /// Memory is paused by `/pause-memory`.
913    MemoryPaused,
914    /// A feature is switched off; see [`ToolResultRemedy::cause`].
915    FeatureDisabled,
916    /// An organization policy denies a feature; see
917    /// [`ToolResultRemedy::policy_kind`].
918    PolicyDenied,
919    /// The command's exec image exceeded the OS argument limit.
920    SpawnArgLimit,
921    /// A settings-file write was held for the machine owner's review.
922    StagedForReview,
923    /// A remedy kind not yet known to this version of the crate.
924    Unknown(String),
925}
926
927impl ToolResultRemedyKind {
928    pub fn as_str(&self) -> &str {
929        match self {
930            Self::McpNeedsAuth => "mcp_needs_auth",
931            Self::McpDisabled => "mcp_disabled",
932            Self::McpRequiredMissing => "mcp_required_missing",
933            Self::AuthExpired => "auth_expired",
934            Self::AuthOverridden => "auth_overridden",
935            Self::AuthMissingScope => "auth_missing_scope",
936            Self::DesignNeedsAuthorization => "design_needs_authorization",
937            Self::SandboxViolation => "sandbox_violation",
938            Self::OutsideReadsBlocked => "outside_reads_blocked",
939            Self::MemoryPaused => "memory_paused",
940            Self::FeatureDisabled => "feature_disabled",
941            Self::PolicyDenied => "policy_denied",
942            Self::SpawnArgLimit => "spawn_arg_limit",
943            Self::StagedForReview => "staged_for_review",
944            Self::Unknown(s) => s.as_str(),
945        }
946    }
947}
948
949impl fmt::Display for ToolResultRemedyKind {
950    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
951        f.write_str(self.as_str())
952    }
953}
954
955impl From<&str> for ToolResultRemedyKind {
956    fn from(s: &str) -> Self {
957        match s {
958            "mcp_needs_auth" => Self::McpNeedsAuth,
959            "mcp_disabled" => Self::McpDisabled,
960            "mcp_required_missing" => Self::McpRequiredMissing,
961            "auth_expired" => Self::AuthExpired,
962            "auth_overridden" => Self::AuthOverridden,
963            "auth_missing_scope" => Self::AuthMissingScope,
964            "design_needs_authorization" => Self::DesignNeedsAuthorization,
965            "sandbox_violation" => Self::SandboxViolation,
966            "outside_reads_blocked" => Self::OutsideReadsBlocked,
967            "memory_paused" => Self::MemoryPaused,
968            "feature_disabled" => Self::FeatureDisabled,
969            "policy_denied" => Self::PolicyDenied,
970            "spawn_arg_limit" => Self::SpawnArgLimit,
971            "staged_for_review" => Self::StagedForReview,
972            other => Self::Unknown(other.to_string()),
973        }
974    }
975}
976
977impl Serialize for ToolResultRemedyKind {
978    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
979        serializer.serialize_str(self.as_str())
980    }
981}
982
983impl<'de> Deserialize<'de> for ToolResultRemedyKind {
984    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
985        let s = String::deserialize(deserializer)?;
986        Ok(Self::from(s.as_str()))
987    }
988}
989
990/// Whose login a remedy names ([`ToolResultRemedy::provider`]).
991#[derive(Debug, Clone, PartialEq, Eq, Hash)]
992pub enum RemedyLoginProvider {
993    /// The claude.ai account (`/login`).
994    ClaudeAi,
995    /// Claude Design's own authorization (`/design login`).
996    ClaudeDesign,
997    /// A provider not yet known to this version of the crate.
998    Unknown(String),
999}
1000
1001impl RemedyLoginProvider {
1002    pub fn as_str(&self) -> &str {
1003        match self {
1004            Self::ClaudeAi => "claude_ai",
1005            Self::ClaudeDesign => "claude_design",
1006            Self::Unknown(s) => s.as_str(),
1007        }
1008    }
1009}
1010
1011impl fmt::Display for RemedyLoginProvider {
1012    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1013        f.write_str(self.as_str())
1014    }
1015}
1016
1017impl From<&str> for RemedyLoginProvider {
1018    fn from(s: &str) -> Self {
1019        match s {
1020            "claude_ai" => Self::ClaudeAi,
1021            "claude_design" => Self::ClaudeDesign,
1022            other => Self::Unknown(other.to_string()),
1023        }
1024    }
1025}
1026
1027impl Serialize for RemedyLoginProvider {
1028    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1029        serializer.serialize_str(self.as_str())
1030    }
1031}
1032
1033impl<'de> Deserialize<'de> for RemedyLoginProvider {
1034    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1035        let s = String::deserialize(deserializer)?;
1036        Ok(Self::from(s.as_str()))
1037    }
1038}
1039
1040/// The feature a `feature_disabled` / `policy_denied` remedy names
1041/// ([`ToolResultRemedy::feature`]).
1042#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1043pub enum RemedyFeature {
1044    Workflows,
1045    Artifacts,
1046    /// A feature not yet known to this version of the crate.
1047    Unknown(String),
1048}
1049
1050impl RemedyFeature {
1051    pub fn as_str(&self) -> &str {
1052        match self {
1053            Self::Workflows => "workflows",
1054            Self::Artifacts => "artifacts",
1055            Self::Unknown(s) => s.as_str(),
1056        }
1057    }
1058}
1059
1060impl fmt::Display for RemedyFeature {
1061    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1062        f.write_str(self.as_str())
1063    }
1064}
1065
1066impl From<&str> for RemedyFeature {
1067    fn from(s: &str) -> Self {
1068        match s {
1069            "workflows" => Self::Workflows,
1070            "artifacts" => Self::Artifacts,
1071            other => Self::Unknown(other.to_string()),
1072        }
1073    }
1074}
1075
1076impl Serialize for RemedyFeature {
1077    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1078        serializer.serialize_str(self.as_str())
1079    }
1080}
1081
1082impl<'de> Deserialize<'de> for RemedyFeature {
1083    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1084        let s = String::deserialize(deserializer)?;
1085        Ok(Self::from(s.as_str()))
1086    }
1087}
1088
1089/// Why a feature is off for a `feature_disabled` remedy
1090/// ([`ToolResultRemedy::cause`]).
1091#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1092pub enum RemedyFeatureCause {
1093    /// `CLAUDE_CODE_DISABLE_WORKFLOWS`, or `disableWorkflows` set by a
1094    /// settings layer above the user's own.
1095    ManagedSettings,
1096    /// An organization policy.
1097    OrgPolicy,
1098    /// Not launched for this account.
1099    Unavailable,
1100    /// The user's own `/config` setting is off (including a
1101    /// `disableWorkflows` in their own settings).
1102    UserSetting,
1103    /// A cause not yet known to this version of the crate.
1104    Unknown(String),
1105}
1106
1107impl RemedyFeatureCause {
1108    pub fn as_str(&self) -> &str {
1109        match self {
1110            Self::ManagedSettings => "managed_settings",
1111            Self::OrgPolicy => "org_policy",
1112            Self::Unavailable => "unavailable",
1113            Self::UserSetting => "user_setting",
1114            Self::Unknown(s) => s.as_str(),
1115        }
1116    }
1117}
1118
1119impl fmt::Display for RemedyFeatureCause {
1120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1121        f.write_str(self.as_str())
1122    }
1123}
1124
1125impl From<&str> for RemedyFeatureCause {
1126    fn from(s: &str) -> Self {
1127        match s {
1128            "managed_settings" => Self::ManagedSettings,
1129            "org_policy" => Self::OrgPolicy,
1130            "unavailable" => Self::Unavailable,
1131            "user_setting" => Self::UserSetting,
1132            other => Self::Unknown(other.to_string()),
1133        }
1134    }
1135}
1136
1137impl Serialize for RemedyFeatureCause {
1138    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1139        serializer.serialize_str(self.as_str())
1140    }
1141}
1142
1143impl<'de> Deserialize<'de> for RemedyFeatureCause {
1144    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1145        let s = String::deserialize(deserializer)?;
1146        Ok(Self::from(s.as_str()))
1147    }
1148}
1149
1150/// The verdict behind a `policy_denied` remedy
1151/// ([`ToolResultRemedy::policy_kind`]).
1152#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1153pub enum RemedyPolicyKind {
1154    /// The organization's policy turns the feature off.
1155    OrgDenied,
1156    /// A HIPAA-regulated organization signed in earlier in this process;
1157    /// restarting clears it.
1158    Latched,
1159    /// A verdict not yet known to this version of the crate.
1160    Unknown(String),
1161}
1162
1163impl RemedyPolicyKind {
1164    pub fn as_str(&self) -> &str {
1165        match self {
1166            Self::OrgDenied => "org_denied",
1167            Self::Latched => "latched",
1168            Self::Unknown(s) => s.as_str(),
1169        }
1170    }
1171}
1172
1173impl fmt::Display for RemedyPolicyKind {
1174    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1175        f.write_str(self.as_str())
1176    }
1177}
1178
1179impl From<&str> for RemedyPolicyKind {
1180    fn from(s: &str) -> Self {
1181        match s {
1182            "org_denied" => Self::OrgDenied,
1183            "latched" => Self::Latched,
1184            other => Self::Unknown(other.to_string()),
1185        }
1186    }
1187}
1188
1189impl Serialize for RemedyPolicyKind {
1190    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1191        serializer.serialize_str(self.as_str())
1192    }
1193}
1194
1195impl<'de> Deserialize<'de> for RemedyPolicyKind {
1196    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1197        let s = String::deserialize(deserializer)?;
1198        Ok(Self::from(s.as_str()))
1199    }
1200}
1201
1202/// User message
1203#[derive(Debug, Clone, Serialize, Deserialize)]
1204pub struct UserMessage {
1205    pub message: MessageContent,
1206    #[serde(skip_serializing_if = "Option::is_none", alias = "sessionId")]
1207    #[serde(
1208        serialize_with = "serialize_optional_uuid",
1209        deserialize_with = "deserialize_optional_uuid"
1210    )]
1211    pub session_id: Option<Uuid>,
1212    /// Parent tool use ID for nested agent messages
1213    #[serde(skip_serializing_if = "Option::is_none")]
1214    pub parent_tool_use_id: Option<String>,
1215    /// Message-level unique identifier
1216    #[serde(skip_serializing_if = "Option::is_none")]
1217    pub uuid: Option<String>,
1218    /// CLI-emitted ISO-8601 timestamp for the message (present on echoed tool results).
1219    #[serde(skip_serializing_if = "Option::is_none")]
1220    pub timestamp: Option<String>,
1221    /// Structured tool result data echoed by the CLI alongside the `tool_result`
1222    /// content block. The shape depends on which tool produced it (e.g. for
1223    /// `AskUserQuestion` it is `{ questions, answers }`; for `Bash` it is
1224    /// `{ stdout, stderr, exit_code, ... }`). Stored as raw JSON to preserve
1225    /// wire fidelity; use [`UserMessage::tool_use_result_as`] to parse into a
1226    /// typed shape when you know which tool was invoked.
1227    #[serde(skip_serializing_if = "Option::is_none")]
1228    pub tool_use_result: Option<serde_json::Value>,
1229    /// ID of the subagent this user message belongs to (the `task_id` of its
1230    /// task events), on the prompt echoed into a `local_agent` subagent.
1231    /// Seen on the wire from CLI 2.1.292, where it is not part of the
1232    /// published user-message schema. Not settable by clients.
1233    #[serde(default, skip_serializing_if = "Option::is_none")]
1234    pub agent_id: Option<String>,
1235    /// Subagent type, when this user message is the prompt echoed into a
1236    /// `local_agent` subagent (e.g. `general-purpose`).
1237    #[serde(skip_serializing_if = "Option::is_none")]
1238    pub subagent_type: Option<String>,
1239    /// Short description of the subagent task, present alongside `subagent_type`.
1240    #[serde(skip_serializing_if = "Option::is_none")]
1241    pub task_description: Option<String>,
1242    #[serde(skip_serializing_if = "Option::is_none")]
1243    pub origin: Option<MessageOrigin>,
1244    #[serde(skip_serializing_if = "Option::is_none")]
1245    pub priority: Option<String>,
1246    #[serde(skip_serializing_if = "Option::is_none", rename = "isSynthetic")]
1247    pub is_synthetic: Option<bool>,
1248    #[serde(skip_serializing_if = "Option::is_none", rename = "shouldQuery")]
1249    pub should_query: Option<bool>,
1250    #[serde(default, skip_serializing_if = "Option::is_none")]
1251    pub is_meta: Option<bool>,
1252    #[serde(default, skip_serializing_if = "Option::is_none")]
1253    pub is_visible_in_transcript_only: Option<bool>,
1254    #[serde(default, skip_serializing_if = "Option::is_none")]
1255    pub is_virtual: Option<bool>,
1256    #[serde(default, skip_serializing_if = "Option::is_none")]
1257    pub is_compact_summary: Option<bool>,
1258    #[serde(skip_serializing_if = "Option::is_none")]
1259    pub summarize_metadata: Option<SummarizeMetadata>,
1260    #[serde(skip_serializing_if = "Option::is_none")]
1261    pub mcp_meta: Option<McpMeta>,
1262    /// Display metadata for this message's `tool_result` blocks, keyed by
1263    /// `tool_use_id`.
1264    #[serde(skip_serializing_if = "Option::is_none")]
1265    pub tool_result_meta: Option<Vec<ToolResultMeta>>,
1266    #[serde(skip_serializing_if = "Option::is_none")]
1267    pub source_tool_use_id: Option<String>,
1268    #[serde(skip_serializing_if = "Option::is_none")]
1269    pub source_tool_assistant_uuid: Option<String>,
1270    #[serde(skip_serializing_if = "Option::is_none")]
1271    pub image_paste_ids: Option<Vec<u64>>,
1272    #[serde(skip_serializing_if = "Option::is_none")]
1273    pub client_platform: Option<String>,
1274    #[serde(skip_serializing_if = "Option::is_none")]
1275    pub inbound_origin: Option<String>,
1276    #[serde(skip_serializing_if = "Option::is_none", rename = "isReplay")]
1277    pub is_replay: Option<bool>,
1278    #[serde(skip_serializing_if = "Option::is_none")]
1279    pub file_attachments: Option<Vec<Value>>,
1280    /// Desktop host only: the host's own seeded summon (CLI 2.1.239+).
1281    #[serde(default, skip_serializing_if = "Option::is_none")]
1282    pub seeded_summon: Option<bool>,
1283    /// True when the client composed this turn from content the user did not
1284    /// type; its text is delivered as written (CLI 2.1.259+).
1285    #[serde(default, skip_serializing_if = "Option::is_none")]
1286    pub client_composed: Option<bool>,
1287    /// True when no person typed this message's text in this session (a
1288    /// server or an app composed it). The text is delivered as written: no
1289    /// slash-command dispatch, no at-mention expansion beyond the message's
1290    /// own uploaded files, no keyword reminders or commands, and a skill it
1291    /// names does not count as typed by the user. Unlike
1292    /// [`client_composed`](Self::client_composed), the turn-start attachment
1293    /// pass still runs. Who sent it stays with [`origin`](Self::origin)
1294    /// (CLI 2.1.300+).
1295    #[serde(default, skip_serializing_if = "Option::is_none")]
1296    pub text_not_typed: Option<bool>,
1297    /// The host's own label for what started this turn (e.g. the desktop
1298    /// app's `scheduled-task`, `sleep-auto-resume`); `user` or absent means a
1299    /// person sent it. Usage attribution only; it does not change how the
1300    /// turn runs (CLI 2.1.278+).
1301    #[serde(default, skip_serializing_if = "Option::is_none")]
1302    pub initiator: Option<String>,
1303    /// Content the user pasted into the prompt rather than typed: each entry
1304    /// a string or an array of content blocks. The CLI appends the text of
1305    /// each entry after the typed text, in order, and may wrap it in
1306    /// `<pasted_content>` tags. Blocks other than text are ignored; send
1307    /// images and documents in `message.content` (CLI 2.1.278+).
1308    #[serde(default, skip_serializing_if = "Option::is_none")]
1309    pub pasted_content: Option<Vec<Value>>,
1310    /// Replayed history rather than a live message: the Remote Control
1311    /// bridge stamps it on the messages it flushes to the session server,
1312    /// which also stamps it on deliveries it replays (CLI 2.1.266+).
1313    #[serde(default, skip_serializing_if = "Option::is_none")]
1314    pub historical: Option<bool>,
1315    /// Epoch milliseconds on the session server's clock when it received the
1316    /// HTTP request that delivered this message. Written by the session
1317    /// server; the CLI copies it to the turn's result as
1318    /// [`user_message_server_received_wall_ms`](crate::io::ResultMessage::user_message_server_received_wall_ms)
1319    /// (CLI 2.1.286+). Not settable by clients.
1320    #[serde(default, skip_serializing_if = "Option::is_none")]
1321    pub server_received_wall_ms: Option<f64>,
1322    /// Only on the synthetic message that re-asks the same model after a
1323    /// refusal: the API `msg_*` id of the refused response. Absent when the
1324    /// CLI has no well-formed id for it (CLI 2.1.290+).
1325    #[serde(default, skip_serializing_if = "Option::is_none")]
1326    pub refused_message_id: Option<String>,
1327}
1328
1329impl UserMessage {
1330    /// Parse the `tool_use_result` field into a caller-specified type.
1331    ///
1332    /// Returns `None` if `tool_use_result` is absent, otherwise returns the
1333    /// deserialization result. The caller must know which tool produced the
1334    /// result and supply a matching type — e.g. for `AskUserQuestion` use
1335    /// [`AskUserQuestionInput`](crate::AskUserQuestionInput), whose
1336    /// `questions` + `answers` fields match the wire result shape.
1337    pub fn tool_use_result_as<T: serde::de::DeserializeOwned>(
1338        &self,
1339    ) -> Option<Result<T, serde_json::Error>> {
1340        self.tool_use_result
1341            .as_ref()
1342            .map(|v| serde_json::from_value(v.clone()))
1343    }
1344
1345    /// Parse the `tool_use_result` as a subagent (`Task`) run result.
1346    ///
1347    /// When this user message echoes the result of a `Task` tool call, the CLI
1348    /// attaches a structured `tool_use_result` carrying the subagent's token,
1349    /// timing, and tool-use accounting. Returns `None` when the field is absent
1350    /// or does not parse as a [`SubagentResult`].
1351    ///
1352    /// Summing [`SubagentResult::total_tokens`] across every `Task` result in a
1353    /// session yields the subagent token rollup the CLI renders as
1354    /// `subagent_tokens` in its terminal `<usage>` block.
1355    pub fn subagent_result(&self) -> Option<SubagentResult> {
1356        self.tool_use_result
1357            .as_ref()
1358            .and_then(|v| serde_json::from_value(v.clone()).ok())
1359    }
1360}
1361
1362/// Token, timing, and tool-use accounting for a completed subagent (`Task`) run.
1363///
1364/// The Claude CLI echoes this object in the `tool_use_result` of a `Task` tool's
1365/// result message. It is the typed source of truth for subagent token
1366/// attribution: the per-run [`total_tokens`](Self::total_tokens),
1367/// [`total_duration_ms`](Self::total_duration_ms), and
1368/// [`total_tool_use_count`](Self::total_tool_use_count) correspond to the
1369/// `subagent_tokens` / `duration_ms` / `tool_uses` line items the CLI renders in
1370/// its human-readable `<usage>` block, and [`usage`](Self::usage) carries the
1371/// full per-model token breakdown for the run.
1372#[derive(Debug, Clone, Serialize, Deserialize)]
1373pub struct SubagentResult {
1374    /// Completion status of the subagent run (e.g. `"completed"`).
1375    #[serde(skip_serializing_if = "Option::is_none")]
1376    pub status: Option<String>,
1377    /// The prompt the subagent was launched with.
1378    #[serde(skip_serializing_if = "Option::is_none")]
1379    pub prompt: Option<String>,
1380    /// Stable identifier of the spawned subagent.
1381    #[serde(rename = "agentId", skip_serializing_if = "Option::is_none")]
1382    pub agent_id: Option<String>,
1383    /// Subagent type that ran (e.g. `general-purpose`, `Explore`).
1384    #[serde(rename = "agentType", skip_serializing_if = "Option::is_none")]
1385    pub agent_type: Option<String>,
1386    /// Final content blocks the subagent returned.
1387    #[serde(
1388        default,
1389        deserialize_with = "deserialize_content_blocks",
1390        skip_serializing_if = "Vec::is_empty"
1391    )]
1392    pub content: Vec<ContentBlock>,
1393    /// Model the subagent actually resolved to (e.g. `claude-sonnet-4-6`).
1394    #[serde(rename = "resolvedModel", skip_serializing_if = "Option::is_none")]
1395    pub resolved_model: Option<String>,
1396    /// Wall-clock duration of the subagent run, in milliseconds.
1397    #[serde(rename = "totalDurationMs", skip_serializing_if = "Option::is_none")]
1398    pub total_duration_ms: Option<u64>,
1399    /// Total tokens consumed by the subagent — the `subagent_tokens` rollup line.
1400    #[serde(rename = "totalTokens", skip_serializing_if = "Option::is_none")]
1401    pub total_tokens: Option<u64>,
1402    /// Number of tool invocations the subagent made.
1403    #[serde(rename = "totalToolUseCount", skip_serializing_if = "Option::is_none")]
1404    pub total_tool_use_count: Option<u64>,
1405    /// Detailed token / cache usage for the subagent run.
1406    #[serde(skip_serializing_if = "Option::is_none")]
1407    pub usage: Option<super::result::UsageInfo>,
1408    /// Per-category tool-use counts, present for some agent types (e.g. `Explore`).
1409    #[serde(rename = "toolStats", skip_serializing_if = "Option::is_none")]
1410    pub tool_stats: Option<SubagentToolStats>,
1411}
1412
1413/// Per-category tool-use counts for a subagent run, from `tool_use_result.toolStats`.
1414///
1415/// The `extra` field captures any counters the CLI adds that aren't modeled here,
1416/// so new wire fields deserialize without error.
1417#[derive(Debug, Clone, Default, Serialize, Deserialize)]
1418#[serde(rename_all = "camelCase")]
1419pub struct SubagentToolStats {
1420    #[serde(default)]
1421    pub read_count: u64,
1422    #[serde(default)]
1423    pub search_count: u64,
1424    #[serde(default)]
1425    pub bash_count: u64,
1426    #[serde(default)]
1427    pub edit_file_count: u64,
1428    #[serde(default)]
1429    pub lines_added: u64,
1430    #[serde(default)]
1431    pub lines_removed: u64,
1432    #[serde(default)]
1433    pub other_tool_count: u64,
1434    #[serde(flatten)]
1435    pub extra: serde_json::Map<String, Value>,
1436}
1437
1438/// Session-level subagent token rollup — the `<subagent_tokens>` /
1439/// `<agent_count>` line items the Claude CLI renders in its terminal
1440/// `<usage>` block.
1441///
1442/// The `stream-json` protocol does **not** carry this rollup on the `result`
1443/// frame's `usage` (confirmed against the CLI binary — the terminal renderer
1444/// computes it from `Task` tool results). Consumers that need it must
1445/// accumulate it the same way: feed every session message through
1446/// [`observe`](Self::observe) and read the totals at any point.
1447///
1448/// A `Task` result observed twice under the same `agentId` (e.g. a replayed
1449/// frame on resume) is counted once. Results with no `agentId` are counted
1450/// every time they are observed.
1451///
1452/// # Example
1453///
1454/// ```
1455/// use claude_codes::{ClaudeOutput, SubagentUsageRollup};
1456///
1457/// let mut rollup = SubagentUsageRollup::default();
1458/// let json = r#"{"type":"user","message":{"role":"user","content":[]},"session_id":"7fbc568e-2bd6-45aa-b217-a1cf80004ba1","tool_use_result":{"status":"completed","agentId":"ab52f22445470d454","totalDurationMs":1853,"totalTokens":10201,"totalToolUseCount":0}}"#;
1459/// let output: ClaudeOutput = serde_json::from_str(json).unwrap();
1460/// rollup.observe(&output);
1461/// assert_eq!(rollup.subagent_tokens, 10201);
1462/// assert_eq!(rollup.agent_count, 1);
1463/// ```
1464#[derive(Debug, Clone, Default, PartialEq, Eq)]
1465pub struct SubagentUsageRollup {
1466    /// Total tokens consumed by subagents — sum of
1467    /// [`SubagentResult::total_tokens`] over every observed `Task` result.
1468    pub subagent_tokens: u64,
1469    /// Number of subagent runs observed (`<agent_count>`).
1470    pub agent_count: u32,
1471    /// Total subagent tool invocations — sum of `total_tool_use_count`.
1472    pub tool_uses: u64,
1473    /// Total subagent wall-clock milliseconds — sum of `total_duration_ms`.
1474    pub duration_ms: u64,
1475    seen_agent_ids: std::collections::BTreeSet<String>,
1476}
1477
1478impl SubagentUsageRollup {
1479    /// Accumulate `output` into the rollup if it is a `Task` tool result.
1480    ///
1481    /// Returns `true` when the message contributed to the totals. Non-user
1482    /// messages, user messages without a `tool_use_result`, results from
1483    /// other tools, and duplicate `agentId`s are all ignored.
1484    pub fn observe(&mut self, output: &ClaudeOutput) -> bool {
1485        match output {
1486            ClaudeOutput::User(user) => self.observe_user(user),
1487            _ => false,
1488        }
1489    }
1490
1491    /// Accumulate a user message's `Task` tool result, if it carries one.
1492    ///
1493    /// Every [`SubagentResult`] field is optional, so any JSON object in
1494    /// `tool_use_result` parses as one (e.g. a `Bash` or `ToolSearch`
1495    /// result). Only results carrying an `agentId` or a `totalTokens`
1496    /// line item are treated as genuine `Task` results.
1497    pub fn observe_user(&mut self, user: &UserMessage) -> bool {
1498        let Some(result) = user.subagent_result() else {
1499            return false;
1500        };
1501        if result.total_tokens.is_none() && result.agent_id.is_none() {
1502            return false;
1503        }
1504        if let Some(agent_id) = &result.agent_id {
1505            if !self.seen_agent_ids.insert(agent_id.clone()) {
1506                return false;
1507            }
1508        }
1509        self.agent_count += 1;
1510        self.subagent_tokens += result.total_tokens.unwrap_or(0);
1511        self.tool_uses += result.total_tool_use_count.unwrap_or(0);
1512        self.duration_ms += result.total_duration_ms.unwrap_or(0);
1513        true
1514    }
1515}
1516
1517/// Message content with role
1518#[derive(Debug, Clone, Serialize, Deserialize)]
1519pub struct MessageContent {
1520    pub role: MessageRole,
1521    #[serde(deserialize_with = "deserialize_content_blocks")]
1522    pub content: Vec<ContentBlock>,
1523}
1524
1525/// System message with metadata
1526#[derive(Debug, Clone, Serialize, Deserialize)]
1527pub struct SystemMessage {
1528    pub subtype: SystemSubtype,
1529    #[serde(flatten)]
1530    pub data: Value, // Captures all other fields
1531}
1532
1533impl SystemMessage {
1534    /// Check if this is an init message
1535    pub fn is_init(&self) -> bool {
1536        self.subtype == SystemSubtype::Init
1537    }
1538
1539    /// Check if this is a status message
1540    pub fn is_status(&self) -> bool {
1541        self.subtype == SystemSubtype::Status
1542    }
1543
1544    /// Check if this is a compact_boundary message
1545    pub fn is_compact_boundary(&self) -> bool {
1546        self.subtype == SystemSubtype::CompactBoundary
1547    }
1548
1549    /// Try to parse as an init message
1550    pub fn as_init(&self) -> Option<InitMessage> {
1551        if self.subtype != SystemSubtype::Init {
1552            return None;
1553        }
1554        serde_json::from_value(self.data.clone()).ok()
1555    }
1556
1557    /// Try to parse as a status message
1558    pub fn as_status(&self) -> Option<StatusMessage> {
1559        if self.subtype != SystemSubtype::Status {
1560            return None;
1561        }
1562        serde_json::from_value(self.data.clone()).ok()
1563    }
1564
1565    /// Try to parse as a compact_boundary message
1566    pub fn as_compact_boundary(&self) -> Option<CompactBoundaryMessage> {
1567        if self.subtype != SystemSubtype::CompactBoundary {
1568            return None;
1569        }
1570        serde_json::from_value(self.data.clone()).ok()
1571    }
1572
1573    /// Check if this is a task_started message
1574    pub fn is_task_started(&self) -> bool {
1575        self.subtype == SystemSubtype::TaskStarted
1576    }
1577
1578    /// Check if this is a task_progress message
1579    pub fn is_task_progress(&self) -> bool {
1580        self.subtype == SystemSubtype::TaskProgress
1581    }
1582
1583    /// Check if this is a task_notification message
1584    pub fn is_task_notification(&self) -> bool {
1585        self.subtype == SystemSubtype::TaskNotification
1586    }
1587
1588    /// Try to parse as a task_started message
1589    pub fn as_task_started(&self) -> Option<TaskStartedMessage> {
1590        if self.subtype != SystemSubtype::TaskStarted {
1591            return None;
1592        }
1593        serde_json::from_value(self.data.clone()).ok()
1594    }
1595
1596    /// Try to parse as a task_progress message
1597    pub fn as_task_progress(&self) -> Option<TaskProgressMessage> {
1598        if self.subtype != SystemSubtype::TaskProgress {
1599            return None;
1600        }
1601        serde_json::from_value(self.data.clone()).ok()
1602    }
1603
1604    /// Try to parse as a task_notification message
1605    pub fn as_task_notification(&self) -> Option<TaskNotificationMessage> {
1606        if self.subtype != SystemSubtype::TaskNotification {
1607            return None;
1608        }
1609        serde_json::from_value(self.data.clone()).ok()
1610    }
1611
1612    /// Check if this is a task_updated message
1613    pub fn is_task_updated(&self) -> bool {
1614        self.subtype == SystemSubtype::TaskUpdated
1615    }
1616
1617    /// Try to parse as a task_updated message
1618    pub fn as_task_updated(&self) -> Option<TaskUpdatedMessage> {
1619        if self.subtype != SystemSubtype::TaskUpdated {
1620            return None;
1621        }
1622        serde_json::from_value(self.data.clone()).ok()
1623    }
1624
1625    /// Check if this is a thinking_tokens message
1626    pub fn is_thinking_tokens(&self) -> bool {
1627        self.subtype == SystemSubtype::ThinkingTokens
1628    }
1629
1630    /// Try to parse as a thinking_tokens message
1631    pub fn as_thinking_tokens(&self) -> Option<ThinkingTokensMessage> {
1632        if self.subtype != SystemSubtype::ThinkingTokens {
1633            return None;
1634        }
1635        serde_json::from_value(self.data.clone()).ok()
1636    }
1637
1638    /// Check if this is a code_change_published message
1639    pub fn is_code_change_published(&self) -> bool {
1640        self.subtype == SystemSubtype::CodeChangePublished
1641    }
1642
1643    /// Try to parse as a code_change_published message
1644    pub fn as_code_change_published(&self) -> Option<CodeChangePublishedMessage> {
1645        if self.subtype != SystemSubtype::CodeChangePublished {
1646            return None;
1647        }
1648        serde_json::from_value(self.data.clone()).ok()
1649    }
1650
1651    /// Check if this is a vcs_state_changed message
1652    pub fn is_vcs_state_changed(&self) -> bool {
1653        self.subtype == SystemSubtype::VcsStateChanged
1654    }
1655
1656    /// Try to parse as a vcs_state_changed message
1657    pub fn as_vcs_state_changed(&self) -> Option<VcsStateChangedMessage> {
1658        if self.subtype != SystemSubtype::VcsStateChanged {
1659            return None;
1660        }
1661        serde_json::from_value(self.data.clone()).ok()
1662    }
1663
1664    /// Check if this is a feedback_draft_queued message.
1665    pub fn is_feedback_draft_queued(&self) -> bool {
1666        self.subtype == SystemSubtype::FeedbackDraftQueued
1667    }
1668
1669    /// Try to parse as a feedback_draft_queued message.
1670    pub fn as_feedback_draft_queued(&self) -> Option<FeedbackDraftQueuedMessage> {
1671        if self.subtype != SystemSubtype::FeedbackDraftQueued {
1672            return None;
1673        }
1674        serde_json::from_value(self.data.clone()).ok()
1675    }
1676
1677    /// Check if this is a cloud_session_delta message.
1678    pub fn is_cloud_session_delta(&self) -> bool {
1679        self.subtype == SystemSubtype::CloudSessionDelta
1680    }
1681
1682    /// Try to parse as a cloud_session_delta message.
1683    pub fn as_cloud_session_delta(&self) -> Option<CloudSessionDeltaMessage> {
1684        if self.subtype != SystemSubtype::CloudSessionDelta {
1685            return None;
1686        }
1687        serde_json::from_value(self.data.clone()).ok()
1688    }
1689
1690    /// Check if this is a dev_intent message.
1691    pub fn is_dev_intent(&self) -> bool {
1692        self.subtype == SystemSubtype::DevIntent
1693    }
1694
1695    /// Try to parse as a dev_intent message.
1696    pub fn as_dev_intent(&self) -> Option<DevIntentMessage> {
1697        if self.subtype != SystemSubtype::DevIntent {
1698            return None;
1699        }
1700        serde_json::from_value(self.data.clone()).ok()
1701    }
1702
1703    /// Check if this is a turn_handoff_available message.
1704    pub fn is_turn_handoff_available(&self) -> bool {
1705        self.subtype == SystemSubtype::TurnHandoffAvailable
1706    }
1707
1708    /// Try to parse as a turn_handoff_available message.
1709    pub fn as_turn_handoff_available(&self) -> Option<TurnHandoffAvailableMessage> {
1710        if self.subtype != SystemSubtype::TurnHandoffAvailable {
1711            return None;
1712        }
1713        serde_json::from_value(self.data.clone()).ok()
1714    }
1715
1716    /// Check if this is a turn_preempted message.
1717    pub fn is_turn_preempted(&self) -> bool {
1718        self.subtype == SystemSubtype::TurnPreempted
1719    }
1720
1721    /// Try to parse as a turn_preempted message.
1722    pub fn as_turn_preempted(&self) -> Option<TurnPreemptedMessage> {
1723        if self.subtype != SystemSubtype::TurnPreempted {
1724            return None;
1725        }
1726        serde_json::from_value(self.data.clone()).ok()
1727    }
1728
1729    /// Check if this is a peer_message_hold message.
1730    pub fn is_peer_message_hold(&self) -> bool {
1731        self.subtype == SystemSubtype::PeerMessageHold
1732    }
1733
1734    /// Try to parse as a peer_message_hold message.
1735    pub fn as_peer_message_hold(&self) -> Option<PeerMessageHoldMessage> {
1736        if self.subtype != SystemSubtype::PeerMessageHold {
1737            return None;
1738        }
1739        serde_json::from_value(self.data.clone()).ok()
1740    }
1741
1742    /// Check if this is a per_turn_effort_changed message.
1743    pub fn is_per_turn_effort_changed(&self) -> bool {
1744        self.subtype == SystemSubtype::PerTurnEffortChanged
1745    }
1746
1747    /// Try to parse as a per_turn_effort_changed message.
1748    pub fn as_per_turn_effort_changed(&self) -> Option<PerTurnEffortChangedMessage> {
1749        if self.subtype != SystemSubtype::PerTurnEffortChanged {
1750            return None;
1751        }
1752        serde_json::from_value(self.data.clone()).ok()
1753    }
1754
1755    /// Check if this is a session_title_changed message.
1756    pub fn is_session_title_changed(&self) -> bool {
1757        self.subtype == SystemSubtype::SessionTitleChanged
1758    }
1759
1760    /// Try to parse as a session_title_changed message.
1761    pub fn as_session_title_changed(&self) -> Option<SessionTitleChangedMessage> {
1762        if self.subtype != SystemSubtype::SessionTitleChanged {
1763            return None;
1764        }
1765        serde_json::from_value(self.data.clone()).ok()
1766    }
1767
1768    /// Check if this is an instruction_size_warning message.
1769    pub fn is_instruction_size_warning(&self) -> bool {
1770        self.subtype == SystemSubtype::InstructionSizeWarning
1771    }
1772
1773    /// Try to parse as an instruction_size_warning message.
1774    pub fn as_instruction_size_warning(&self) -> Option<InstructionSizeWarningMessage> {
1775        if self.subtype != SystemSubtype::InstructionSizeWarning {
1776            return None;
1777        }
1778        serde_json::from_value(self.data.clone()).ok()
1779    }
1780
1781    /// Check if this is a permission_check_status message.
1782    pub fn is_permission_check_status(&self) -> bool {
1783        self.subtype == SystemSubtype::PermissionCheckStatus
1784    }
1785
1786    /// Try to parse as a permission_check_status message.
1787    pub fn as_permission_check_status(&self) -> Option<PermissionCheckStatusMessage> {
1788        if self.subtype != SystemSubtype::PermissionCheckStatus {
1789            return None;
1790        }
1791        serde_json::from_value(self.data.clone()).ok()
1792    }
1793
1794    /// Check if this is a file_attachments_missing message.
1795    pub fn is_file_attachments_missing(&self) -> bool {
1796        self.subtype == SystemSubtype::FileAttachmentsMissing
1797    }
1798
1799    /// Try to parse as a file_attachments_missing message.
1800    pub fn as_file_attachments_missing(&self) -> Option<FileAttachmentsMissingMessage> {
1801        if self.subtype != SystemSubtype::FileAttachmentsMissing {
1802            return None;
1803        }
1804        serde_json::from_value(self.data.clone()).ok()
1805    }
1806
1807    /// Check if this is a ui_focus message.
1808    pub fn is_ui_focus(&self) -> bool {
1809        self.subtype == SystemSubtype::UiFocus
1810    }
1811
1812    /// Try to parse as a ui_focus message.
1813    pub fn as_ui_focus(&self) -> Option<UiFocusMessage> {
1814        if self.subtype != SystemSubtype::UiFocus {
1815            return None;
1816        }
1817        serde_json::from_value(self.data.clone()).ok()
1818    }
1819
1820    /// Check if this is a ui_invalidate message.
1821    pub fn is_ui_invalidate(&self) -> bool {
1822        self.subtype == SystemSubtype::UiInvalidate
1823    }
1824
1825    /// Try to parse as a ui_invalidate message.
1826    pub fn as_ui_invalidate(&self) -> Option<UiInvalidateMessage> {
1827        if self.subtype != SystemSubtype::UiInvalidate {
1828            return None;
1829        }
1830        serde_json::from_value(self.data.clone()).ok()
1831    }
1832
1833    /// Check if this is a ui_log message.
1834    pub fn is_ui_log(&self) -> bool {
1835        self.subtype == SystemSubtype::UiLog
1836    }
1837
1838    /// Try to parse as a ui_log message.
1839    pub fn as_ui_log(&self) -> Option<UiLogMessage> {
1840        if self.subtype != SystemSubtype::UiLog {
1841            return None;
1842        }
1843        serde_json::from_value(self.data.clone()).ok()
1844    }
1845
1846    /// Check if this is a ui_panes message.
1847    pub fn is_ui_panes(&self) -> bool {
1848        self.subtype == SystemSubtype::UiPanes
1849    }
1850
1851    /// Try to parse as a ui_panes message.
1852    pub fn as_ui_panes(&self) -> Option<UiPanesMessage> {
1853        if self.subtype != SystemSubtype::UiPanes {
1854            return None;
1855        }
1856        serde_json::from_value(self.data.clone()).ok()
1857    }
1858
1859    /// Check if this is a ui_scroll message.
1860    pub fn is_ui_scroll(&self) -> bool {
1861        self.subtype == SystemSubtype::UiScroll
1862    }
1863
1864    /// Try to parse as a ui_scroll message.
1865    pub fn as_ui_scroll(&self) -> Option<UiScrollMessage> {
1866        if self.subtype != SystemSubtype::UiScroll {
1867            return None;
1868        }
1869        serde_json::from_value(self.data.clone()).ok()
1870    }
1871
1872    /// Check if this is a ui_status message.
1873    pub fn is_ui_status(&self) -> bool {
1874        self.subtype == SystemSubtype::UiStatus
1875    }
1876
1877    /// Try to parse as a ui_status message.
1878    pub fn as_ui_status(&self) -> Option<UiStatusMessage> {
1879        if self.subtype != SystemSubtype::UiStatus {
1880            return None;
1881        }
1882        serde_json::from_value(self.data.clone()).ok()
1883    }
1884
1885    /// Check if this is a ui_toast message.
1886    pub fn is_ui_toast(&self) -> bool {
1887        self.subtype == SystemSubtype::UiToast
1888    }
1889
1890    /// Try to parse as a ui_toast message.
1891    pub fn as_ui_toast(&self) -> Option<UiToastMessage> {
1892        if self.subtype != SystemSubtype::UiToast {
1893            return None;
1894        }
1895        serde_json::from_value(self.data.clone()).ok()
1896    }
1897
1898    /// Parse any typed system subtype known to this crate version.
1899    pub fn as_known_system_event(&self) -> Option<KnownSystemEvent> {
1900        macro_rules! parse {
1901            ($variant:ident, $ty:ty) => {
1902                serde_json::from_value::<$ty>(self.data.clone())
1903                    .ok()
1904                    .map(KnownSystemEvent::$variant)
1905            };
1906        }
1907
1908        match self.subtype {
1909            SystemSubtype::Init => parse!(Init, InitMessage),
1910            SystemSubtype::Status => parse!(Status, StatusMessage),
1911            SystemSubtype::CompactBoundary => parse!(CompactBoundary, CompactBoundaryMessage),
1912            SystemSubtype::ThinkingTokens => parse!(ThinkingTokens, ThinkingTokensMessage),
1913            SystemSubtype::TaskStarted => parse!(TaskStarted, TaskStartedMessage),
1914            SystemSubtype::TaskProgress => parse!(TaskProgress, TaskProgressMessage),
1915            SystemSubtype::TaskUpdated => parse!(TaskUpdated, TaskUpdatedMessage),
1916            SystemSubtype::TaskNotification => parse!(TaskNotification, TaskNotificationMessage),
1917            SystemSubtype::ApiRetry => parse!(ApiRetry, ApiRetryMessage),
1918            SystemSubtype::ControlRequestProgress => {
1919                parse!(ControlRequestProgress, ControlRequestProgressMessage)
1920            }
1921            SystemSubtype::ModelRefusalFallback => {
1922                parse!(ModelRefusalFallback, ModelRefusalFallbackMessage)
1923            }
1924            SystemSubtype::ModelRefusalNoFallback => {
1925                parse!(ModelRefusalNoFallback, ModelRefusalNoFallbackMessage)
1926            }
1927            SystemSubtype::LocalCommandOutput => {
1928                parse!(LocalCommandOutput, LocalCommandOutputMessage)
1929            }
1930            SystemSubtype::HookStarted => parse!(HookStarted, HookStartedMessage),
1931            SystemSubtype::HookProgress => parse!(HookProgress, HookProgressMessage),
1932            SystemSubtype::HookResponse => parse!(HookResponse, HookResponseMessage),
1933            SystemSubtype::PluginInstall => parse!(PluginInstall, PluginInstallMessage),
1934            SystemSubtype::BackgroundTasksChanged => {
1935                parse!(BackgroundTasksChanged, BackgroundTasksChangedMessage)
1936            }
1937            SystemSubtype::SessionStateChanged => {
1938                parse!(SessionStateChanged, SessionStateChangedMessage)
1939            }
1940            SystemSubtype::WorkerShuttingDown => {
1941                parse!(WorkerShuttingDown, WorkerShuttingDownMessage)
1942            }
1943            SystemSubtype::CommandsChanged => parse!(CommandsChanged, CommandsChangedMessage),
1944            SystemSubtype::Notification => parse!(Notification, NotificationMessage),
1945            SystemSubtype::FilesPersisted => parse!(FilesPersisted, FilesPersistedMessage),
1946            SystemSubtype::MemoryRecall => parse!(MemoryRecall, MemoryRecallMessage),
1947            SystemSubtype::ElicitationComplete => {
1948                parse!(ElicitationComplete, ElicitationCompleteMessage)
1949            }
1950            SystemSubtype::PermissionDenied => parse!(PermissionDenied, PermissionDeniedMessage),
1951            SystemSubtype::MirrorError => parse!(MirrorError, MirrorErrorMessage),
1952            SystemSubtype::Informational => parse!(Informational, InformationalMessage),
1953            SystemSubtype::CodeChangePublished => {
1954                parse!(CodeChangePublished, CodeChangePublishedMessage)
1955            }
1956            SystemSubtype::VcsStateChanged => parse!(VcsStateChanged, VcsStateChangedMessage),
1957            SystemSubtype::FeedbackDraftQueued => {
1958                parse!(FeedbackDraftQueued, FeedbackDraftQueuedMessage)
1959            }
1960            SystemSubtype::CloudSessionDelta => {
1961                parse!(CloudSessionDelta, CloudSessionDeltaMessage)
1962            }
1963            SystemSubtype::DevIntent => parse!(DevIntent, DevIntentMessage),
1964            SystemSubtype::TurnHandoffAvailable => {
1965                parse!(TurnHandoffAvailable, TurnHandoffAvailableMessage)
1966            }
1967            SystemSubtype::TurnPreempted => parse!(TurnPreempted, TurnPreemptedMessage),
1968            SystemSubtype::PeerMessageHold => parse!(PeerMessageHold, PeerMessageHoldMessage),
1969            SystemSubtype::PerTurnEffortChanged => {
1970                parse!(PerTurnEffortChanged, PerTurnEffortChangedMessage)
1971            }
1972            SystemSubtype::SessionTitleChanged => {
1973                parse!(SessionTitleChanged, SessionTitleChangedMessage)
1974            }
1975            SystemSubtype::InstructionSizeWarning => {
1976                parse!(InstructionSizeWarning, InstructionSizeWarningMessage)
1977            }
1978            SystemSubtype::FileAttachmentsMissing => {
1979                parse!(FileAttachmentsMissing, FileAttachmentsMissingMessage)
1980            }
1981            SystemSubtype::PermissionCheckStatus => {
1982                parse!(PermissionCheckStatus, PermissionCheckStatusMessage)
1983            }
1984            SystemSubtype::UiFocus => parse!(UiFocus, UiFocusMessage),
1985            SystemSubtype::UiInvalidate => parse!(UiInvalidate, UiInvalidateMessage),
1986            SystemSubtype::UiLog => parse!(UiLog, UiLogMessage),
1987            SystemSubtype::UiPanes => parse!(UiPanes, UiPanesMessage),
1988            SystemSubtype::UiScroll => parse!(UiScroll, UiScrollMessage),
1989            SystemSubtype::UiStatus => parse!(UiStatus, UiStatusMessage),
1990            SystemSubtype::UiToast => parse!(UiToast, UiToastMessage),
1991            SystemSubtype::Unknown(_) => None,
1992        }
1993    }
1994
1995    /// Re-serialize this system message's payload through the typed view that
1996    /// matches its `subtype`, returning the result as JSON.
1997    ///
1998    /// Used by the wrapping audit ([`crate::io::audit_frame`]) to verify that a
1999    /// subtype's dedicated struct captures every wire field: the audit compares
2000    /// this against the raw [`SystemMessage::data`]. Returns `None` for subtypes
2001    /// this crate version has no dedicated struct for (including
2002    /// [`SystemSubtype::Unknown`]) — those are reported as not fully wrapped.
2003    pub fn typed_value(&self) -> Option<Value> {
2004        fn reserialize<T: Serialize>(parsed: Option<T>) -> Option<Value> {
2005            parsed.and_then(|v| serde_json::to_value(v).ok())
2006        }
2007        match self.subtype {
2008            SystemSubtype::Init => reserialize(self.as_init()),
2009            SystemSubtype::Status => reserialize(self.as_status()),
2010            SystemSubtype::CompactBoundary => reserialize(self.as_compact_boundary()),
2011            SystemSubtype::ThinkingTokens => reserialize(self.as_thinking_tokens()),
2012            SystemSubtype::TaskStarted => reserialize(self.as_task_started()),
2013            SystemSubtype::TaskProgress => reserialize(self.as_task_progress()),
2014            SystemSubtype::TaskUpdated => reserialize(self.as_task_updated()),
2015            SystemSubtype::TaskNotification => reserialize(self.as_task_notification()),
2016            SystemSubtype::ApiRetry => reserialize(parse_system::<ApiRetryMessage>(self)),
2017            SystemSubtype::ControlRequestProgress => {
2018                reserialize(parse_system::<ControlRequestProgressMessage>(self))
2019            }
2020            SystemSubtype::ModelRefusalFallback => {
2021                reserialize(parse_system::<ModelRefusalFallbackMessage>(self))
2022            }
2023            SystemSubtype::ModelRefusalNoFallback => {
2024                reserialize(parse_system::<ModelRefusalNoFallbackMessage>(self))
2025            }
2026            SystemSubtype::LocalCommandOutput => {
2027                reserialize(parse_system::<LocalCommandOutputMessage>(self))
2028            }
2029            SystemSubtype::HookStarted => reserialize(parse_system::<HookStartedMessage>(self)),
2030            SystemSubtype::HookProgress => reserialize(parse_system::<HookProgressMessage>(self)),
2031            SystemSubtype::HookResponse => reserialize(parse_system::<HookResponseMessage>(self)),
2032            SystemSubtype::PluginInstall => reserialize(parse_system::<PluginInstallMessage>(self)),
2033            SystemSubtype::BackgroundTasksChanged => {
2034                reserialize(parse_system::<BackgroundTasksChangedMessage>(self))
2035            }
2036            SystemSubtype::SessionStateChanged => {
2037                reserialize(parse_system::<SessionStateChangedMessage>(self))
2038            }
2039            SystemSubtype::WorkerShuttingDown => {
2040                reserialize(parse_system::<WorkerShuttingDownMessage>(self))
2041            }
2042            SystemSubtype::CommandsChanged => {
2043                reserialize(parse_system::<CommandsChangedMessage>(self))
2044            }
2045            SystemSubtype::Notification => reserialize(parse_system::<NotificationMessage>(self)),
2046            SystemSubtype::FilesPersisted => {
2047                reserialize(parse_system::<FilesPersistedMessage>(self))
2048            }
2049            SystemSubtype::MemoryRecall => reserialize(parse_system::<MemoryRecallMessage>(self)),
2050            SystemSubtype::ElicitationComplete => {
2051                reserialize(parse_system::<ElicitationCompleteMessage>(self))
2052            }
2053            SystemSubtype::PermissionDenied => {
2054                reserialize(parse_system::<PermissionDeniedMessage>(self))
2055            }
2056            SystemSubtype::MirrorError => reserialize(parse_system::<MirrorErrorMessage>(self)),
2057            SystemSubtype::Informational => reserialize(parse_system::<InformationalMessage>(self)),
2058            SystemSubtype::CodeChangePublished => {
2059                reserialize(parse_system::<CodeChangePublishedMessage>(self))
2060            }
2061            SystemSubtype::VcsStateChanged => {
2062                reserialize(parse_system::<VcsStateChangedMessage>(self))
2063            }
2064            SystemSubtype::FeedbackDraftQueued => {
2065                reserialize(parse_system::<FeedbackDraftQueuedMessage>(self))
2066            }
2067            SystemSubtype::CloudSessionDelta => {
2068                reserialize(parse_system::<CloudSessionDeltaMessage>(self))
2069            }
2070            SystemSubtype::DevIntent => reserialize(parse_system::<DevIntentMessage>(self)),
2071            SystemSubtype::TurnHandoffAvailable => {
2072                reserialize(parse_system::<TurnHandoffAvailableMessage>(self))
2073            }
2074            SystemSubtype::TurnPreempted => reserialize(parse_system::<TurnPreemptedMessage>(self)),
2075            SystemSubtype::PeerMessageHold => {
2076                reserialize(parse_system::<PeerMessageHoldMessage>(self))
2077            }
2078            SystemSubtype::PerTurnEffortChanged => {
2079                reserialize(parse_system::<PerTurnEffortChangedMessage>(self))
2080            }
2081            SystemSubtype::SessionTitleChanged => {
2082                reserialize(parse_system::<SessionTitleChangedMessage>(self))
2083            }
2084            SystemSubtype::InstructionSizeWarning => {
2085                reserialize(parse_system::<InstructionSizeWarningMessage>(self))
2086            }
2087            SystemSubtype::FileAttachmentsMissing => {
2088                reserialize(parse_system::<FileAttachmentsMissingMessage>(self))
2089            }
2090            SystemSubtype::PermissionCheckStatus => {
2091                reserialize(parse_system::<PermissionCheckStatusMessage>(self))
2092            }
2093            SystemSubtype::UiFocus => reserialize(parse_system::<UiFocusMessage>(self)),
2094            SystemSubtype::UiInvalidate => reserialize(parse_system::<UiInvalidateMessage>(self)),
2095            SystemSubtype::UiLog => reserialize(parse_system::<UiLogMessage>(self)),
2096            SystemSubtype::UiPanes => reserialize(parse_system::<UiPanesMessage>(self)),
2097            SystemSubtype::UiScroll => reserialize(parse_system::<UiScrollMessage>(self)),
2098            SystemSubtype::UiStatus => reserialize(parse_system::<UiStatusMessage>(self)),
2099            SystemSubtype::UiToast => reserialize(parse_system::<UiToastMessage>(self)),
2100            SystemSubtype::Unknown(_) => None,
2101        }
2102    }
2103}
2104
2105fn parse_system<T: serde::de::DeserializeOwned>(message: &SystemMessage) -> Option<T> {
2106    serde_json::from_value(message.data.clone()).ok()
2107}
2108
2109/// Owned typed view over any known system message subtype.
2110// `InitMessage` outgrew clippy's variant-size threshold when CLI 2.1.232
2111// added fields. This enum is a transient per-parse classification (never
2112// stored in bulk), so boxing would break every match site for no retained-
2113// memory win.
2114#[allow(clippy::large_enum_variant)]
2115#[derive(Debug, Clone, Serialize, Deserialize)]
2116pub enum KnownSystemEvent {
2117    Init(InitMessage),
2118    Status(StatusMessage),
2119    CompactBoundary(CompactBoundaryMessage),
2120    ThinkingTokens(ThinkingTokensMessage),
2121    TaskStarted(TaskStartedMessage),
2122    TaskProgress(TaskProgressMessage),
2123    TaskUpdated(TaskUpdatedMessage),
2124    TaskNotification(TaskNotificationMessage),
2125    ApiRetry(ApiRetryMessage),
2126    ControlRequestProgress(ControlRequestProgressMessage),
2127    ModelRefusalFallback(ModelRefusalFallbackMessage),
2128    ModelRefusalNoFallback(ModelRefusalNoFallbackMessage),
2129    LocalCommandOutput(LocalCommandOutputMessage),
2130    HookStarted(HookStartedMessage),
2131    HookProgress(HookProgressMessage),
2132    HookResponse(HookResponseMessage),
2133    PluginInstall(PluginInstallMessage),
2134    BackgroundTasksChanged(BackgroundTasksChangedMessage),
2135    SessionStateChanged(SessionStateChangedMessage),
2136    WorkerShuttingDown(WorkerShuttingDownMessage),
2137    CommandsChanged(CommandsChangedMessage),
2138    Notification(NotificationMessage),
2139    FilesPersisted(FilesPersistedMessage),
2140    MemoryRecall(MemoryRecallMessage),
2141    ElicitationComplete(ElicitationCompleteMessage),
2142    PermissionDenied(PermissionDeniedMessage),
2143    MirrorError(MirrorErrorMessage),
2144    Informational(InformationalMessage),
2145    CodeChangePublished(CodeChangePublishedMessage),
2146    VcsStateChanged(VcsStateChangedMessage),
2147    FeedbackDraftQueued(FeedbackDraftQueuedMessage),
2148    CloudSessionDelta(CloudSessionDeltaMessage),
2149    DevIntent(DevIntentMessage),
2150    TurnHandoffAvailable(TurnHandoffAvailableMessage),
2151    TurnPreempted(TurnPreemptedMessage),
2152    PeerMessageHold(PeerMessageHoldMessage),
2153    PerTurnEffortChanged(PerTurnEffortChangedMessage),
2154    SessionTitleChanged(SessionTitleChangedMessage),
2155    InstructionSizeWarning(InstructionSizeWarningMessage),
2156    FileAttachmentsMissing(FileAttachmentsMissingMessage),
2157    PermissionCheckStatus(PermissionCheckStatusMessage),
2158    UiFocus(UiFocusMessage),
2159    UiInvalidate(UiInvalidateMessage),
2160    UiLog(UiLogMessage),
2161    UiPanes(UiPanesMessage),
2162    UiScroll(UiScrollMessage),
2163    UiStatus(UiStatusMessage),
2164    UiToast(UiToastMessage),
2165}
2166
2167#[derive(Debug, Clone, Serialize, Deserialize)]
2168pub struct ApiRetryMessage {
2169    pub attempt: u64,
2170    pub max_retries: u64,
2171    pub retry_delay_ms: u64,
2172    pub error_status: Option<u16>,
2173    pub error: String,
2174    /// Present only when the API sent no response headers within the
2175    /// first-byte window (`CLAUDE_STREAM_FIRST_BYTE_TIMEOUT_MS`). For this
2176    /// cause `max_retries` is its own cap (normally one retry), not the
2177    /// session budget (CLI 2.1.261+).
2178    #[serde(default, skip_serializing_if = "Option::is_none")]
2179    pub no_response: Option<ApiRetryNoResponse>,
2180    #[serde(default, skip_serializing_if = "Option::is_none")]
2181    pub uuid: Option<String>,
2182    #[serde(default, skip_serializing_if = "Option::is_none")]
2183    pub session_id: Option<String>,
2184}
2185
2186/// Timing of a first-byte-timeout retry, carried as
2187/// [`ApiRetryMessage::no_response`].
2188#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
2189pub struct ApiRetryNoResponse {
2190    /// How long the failed attempt waited for response headers.
2191    pub waited_ms: u64,
2192    /// How long the retry will wait for them.
2193    pub retry_wait_ms: u64,
2194}
2195
2196#[derive(Debug, Clone, Serialize, Deserialize)]
2197pub struct ControlRequestProgressMessage {
2198    pub request_id: String,
2199    pub status: String,
2200    #[serde(default, skip_serializing_if = "Option::is_none")]
2201    pub attempt: Option<u64>,
2202    #[serde(default, skip_serializing_if = "Option::is_none")]
2203    pub max_retries: Option<u64>,
2204    #[serde(default, skip_serializing_if = "Option::is_none")]
2205    pub retry_delay_ms: Option<u64>,
2206    #[serde(default, skip_serializing_if = "Option::is_none")]
2207    pub error_status: Option<u16>,
2208    #[serde(default, skip_serializing_if = "Option::is_none")]
2209    pub error: Option<String>,
2210    #[serde(default, skip_serializing_if = "Option::is_none")]
2211    pub uuid: Option<String>,
2212    #[serde(default, skip_serializing_if = "Option::is_none")]
2213    pub session_id: Option<String>,
2214}
2215
2216#[derive(Debug, Clone, Serialize, Deserialize)]
2217pub struct ModelRefusalFallbackMessage {
2218    pub trigger: String,
2219    pub direction: String,
2220    /// `"session"`: the main thread fell back and the session model is
2221    /// swapped. `"local"`: a subagent / side-question (`/btw`) / background
2222    /// fork fell back — only that response came from the fallback model and
2223    /// the session model is unchanged. Absent from CLIs before 2.1.222
2224    /// (treat as `"session"`).
2225    #[serde(default, skip_serializing_if = "Option::is_none")]
2226    pub scope: Option<RefusalFallbackScope>,
2227    pub original_model: String,
2228    pub fallback_model: String,
2229    pub request_id: Option<String>,
2230    #[serde(default, skip_serializing_if = "Option::is_none")]
2231    pub api_refusal_category: Option<String>,
2232    /// Present when any hop of this banner's multi-hop episode was a cyber
2233    /// refusal — not only the origin hop `api_refusal_category` describes.
2234    /// Re-arm evidence for the CLI's cyber-exclusion header on session
2235    /// restore; absent on cyber-free episodes and older CLIs (2.1.239+).
2236    #[serde(default, skip_serializing_if = "Option::is_none")]
2237    pub saw_cyber_refusal: Option<bool>,
2238    #[serde(default, skip_serializing_if = "Option::is_none")]
2239    pub api_refusal_explanation: Option<String>,
2240    #[serde(default, skip_serializing_if = "Option::is_none")]
2241    pub retracted_message_uuids: Option<Vec<String>>,
2242    #[serde(default, skip_serializing_if = "Option::is_none")]
2243    pub refused_user_message_uuid: Option<String>,
2244    pub content: Value,
2245    #[serde(default, skip_serializing_if = "Option::is_none")]
2246    pub uuid: Option<String>,
2247    #[serde(default, skip_serializing_if = "Option::is_none")]
2248    pub session_id: Option<String>,
2249}
2250
2251/// Scope of a refusal-fallback model swap, carried by
2252/// [`ModelRefusalFallbackMessage::scope`]. Open — new scopes may ship on the
2253/// wire ahead of schema updates.
2254#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2255pub enum RefusalFallbackScope {
2256    /// The main thread fell back; the session model is swapped.
2257    Session,
2258    /// A subagent / side-question / background fork fell back; only that
2259    /// response used the fallback model, the session model is unchanged.
2260    Local,
2261    /// A scope not yet known to this version of the crate.
2262    Unknown(String),
2263}
2264
2265impl RefusalFallbackScope {
2266    pub fn as_str(&self) -> &str {
2267        match self {
2268            Self::Session => "session",
2269            Self::Local => "local",
2270            Self::Unknown(s) => s.as_str(),
2271        }
2272    }
2273}
2274
2275impl fmt::Display for RefusalFallbackScope {
2276    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2277        f.write_str(self.as_str())
2278    }
2279}
2280
2281impl From<&str> for RefusalFallbackScope {
2282    fn from(s: &str) -> Self {
2283        match s {
2284            "session" => Self::Session,
2285            "local" => Self::Local,
2286            other => Self::Unknown(other.to_string()),
2287        }
2288    }
2289}
2290
2291impl Serialize for RefusalFallbackScope {
2292    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2293        serializer.serialize_str(self.as_str())
2294    }
2295}
2296
2297impl<'de> Deserialize<'de> for RefusalFallbackScope {
2298    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
2299        let s = String::deserialize(deserializer)?;
2300        Ok(Self::from(s.as_str()))
2301    }
2302}
2303
2304#[derive(Debug, Clone, Serialize, Deserialize)]
2305pub struct ModelRefusalNoFallbackMessage {
2306    pub original_model: String,
2307    pub request_id: Option<String>,
2308    #[serde(default, skip_serializing_if = "Option::is_none")]
2309    pub api_refusal_category: Option<String>,
2310    #[serde(default, skip_serializing_if = "Option::is_none")]
2311    pub api_refusal_explanation: Option<String>,
2312    pub content: Value,
2313    #[serde(default, skip_serializing_if = "Option::is_none")]
2314    pub uuid: Option<String>,
2315    #[serde(default, skip_serializing_if = "Option::is_none")]
2316    pub session_id: Option<String>,
2317}
2318
2319#[derive(Debug, Clone, Serialize, Deserialize)]
2320pub struct LocalCommandOutputMessage {
2321    pub content: String,
2322    #[serde(default, skip_serializing_if = "Option::is_none")]
2323    pub uuid: Option<String>,
2324    #[serde(default, skip_serializing_if = "Option::is_none")]
2325    pub session_id: Option<String>,
2326}
2327
2328#[derive(Debug, Clone, Serialize, Deserialize)]
2329pub struct HookStartedMessage {
2330    pub hook_id: String,
2331    pub hook_name: String,
2332    pub hook_event: String,
2333    #[serde(default, skip_serializing_if = "Option::is_none")]
2334    pub uuid: Option<String>,
2335    #[serde(default, skip_serializing_if = "Option::is_none")]
2336    pub session_id: Option<String>,
2337}
2338
2339#[derive(Debug, Clone, Serialize, Deserialize)]
2340pub struct HookProgressMessage {
2341    pub hook_id: String,
2342    pub hook_name: String,
2343    pub hook_event: String,
2344    #[serde(default, skip_serializing_if = "Option::is_none")]
2345    pub stdout: Option<String>,
2346    #[serde(default, skip_serializing_if = "Option::is_none")]
2347    pub stderr: Option<String>,
2348    #[serde(default, skip_serializing_if = "Option::is_none")]
2349    pub output: Option<String>,
2350    #[serde(default, skip_serializing_if = "Option::is_none")]
2351    pub uuid: Option<String>,
2352    #[serde(default, skip_serializing_if = "Option::is_none")]
2353    pub session_id: Option<String>,
2354}
2355
2356#[derive(Debug, Clone, Serialize, Deserialize)]
2357pub struct HookResponseMessage {
2358    pub hook_id: String,
2359    pub hook_name: String,
2360    pub hook_event: String,
2361    #[serde(default, skip_serializing_if = "Option::is_none")]
2362    pub stdout: Option<String>,
2363    #[serde(default, skip_serializing_if = "Option::is_none")]
2364    pub stderr: Option<String>,
2365    #[serde(default, skip_serializing_if = "Option::is_none")]
2366    pub output: Option<String>,
2367    #[serde(default, skip_serializing_if = "Option::is_none")]
2368    pub exit_code: Option<i32>,
2369    pub outcome: String,
2370    #[serde(default, skip_serializing_if = "Option::is_none")]
2371    pub uuid: Option<String>,
2372    #[serde(default, skip_serializing_if = "Option::is_none")]
2373    pub session_id: Option<String>,
2374}
2375
2376#[derive(Debug, Clone, Serialize, Deserialize)]
2377pub struct PluginInstallMessage {
2378    pub status: String,
2379    #[serde(default, skip_serializing_if = "Option::is_none")]
2380    pub name: Option<String>,
2381    #[serde(default, skip_serializing_if = "Option::is_none")]
2382    pub error: Option<String>,
2383    #[serde(default, skip_serializing_if = "Option::is_none")]
2384    pub uuid: Option<String>,
2385    #[serde(default, skip_serializing_if = "Option::is_none")]
2386    pub session_id: Option<String>,
2387}
2388
2389#[derive(Debug, Clone, Serialize, Deserialize)]
2390pub struct BackgroundTasksChangedMessage {
2391    pub tasks: Vec<BackgroundTaskInfo>,
2392    #[serde(default, skip_serializing_if = "Option::is_none")]
2393    pub uuid: Option<String>,
2394    #[serde(default, skip_serializing_if = "Option::is_none")]
2395    pub session_id: Option<String>,
2396}
2397
2398#[derive(Debug, Clone, Serialize, Deserialize)]
2399pub struct BackgroundTaskInfo {
2400    pub task_id: String,
2401    /// Id of the task's current run; see [`TaskStartedMessage::run_id`]
2402    /// (CLI 2.1.292+).
2403    #[serde(default, skip_serializing_if = "Option::is_none")]
2404    pub run_id: Option<String>,
2405    pub task_type: String,
2406    /// Agent type of a `local_agent` task, such as `general-purpose` or a
2407    /// custom agent's name; `main-session` for a backgrounded main session.
2408    /// Not set on other tasks (CLI 2.1.293+).
2409    #[serde(default, skip_serializing_if = "Option::is_none")]
2410    pub subagent_type: Option<String>,
2411    pub description: String,
2412    /// `task_id` of the subagent task that launched this one; see
2413    /// [`TaskStartedMessage::parent_task_id`] (CLI 2.1.292+).
2414    #[serde(default, skip_serializing_if = "Option::is_none")]
2415    pub parent_task_id: Option<String>,
2416    /// True for housekeeping tasks the CLI does not surface as user work;
2417    /// hosts should exclude them from activity indicators (CLI 2.1.259+).
2418    #[serde(default, skip_serializing_if = "Option::is_none")]
2419    pub ambient: Option<bool>,
2420    /// True for an awaited run of a resumed subagent; see
2421    /// [`TaskStartedMessage::awaited`] (CLI 2.1.293+).
2422    #[serde(default, skip_serializing_if = "Option::is_none")]
2423    pub awaited: Option<bool>,
2424}
2425
2426#[derive(Debug, Clone, Serialize, Deserialize)]
2427pub struct SessionStateChangedMessage {
2428    pub state: String,
2429    #[serde(default, skip_serializing_if = "Option::is_none")]
2430    pub uuid: Option<String>,
2431    #[serde(default, skip_serializing_if = "Option::is_none")]
2432    pub session_id: Option<String>,
2433}
2434
2435#[derive(Debug, Clone, Serialize, Deserialize)]
2436pub struct WorkerShuttingDownMessage {
2437    pub reason: String,
2438    #[serde(default, skip_serializing_if = "Option::is_none")]
2439    pub uuid: Option<String>,
2440    #[serde(default, skip_serializing_if = "Option::is_none")]
2441    pub session_id: Option<String>,
2442}
2443
2444#[derive(Debug, Clone, Serialize, Deserialize)]
2445pub struct CommandsChangedMessage {
2446    pub commands: Vec<CommandInfo>,
2447    #[serde(default, skip_serializing_if = "Option::is_none")]
2448    pub uuid: Option<String>,
2449    #[serde(default, skip_serializing_if = "Option::is_none")]
2450    pub session_id: Option<String>,
2451}
2452
2453#[derive(Debug, Clone, Serialize, Deserialize)]
2454pub struct CommandInfo {
2455    pub name: String,
2456    pub description: String,
2457    #[serde(rename = "argumentHint")]
2458    pub argument_hint: String,
2459    #[serde(default, skip_serializing_if = "Option::is_none")]
2460    pub aliases: Option<Vec<String>>,
2461    /// True when the command is Claude Code's own; absent for a command
2462    /// defined by a user, project, plugin or MCP server. Rows can share a
2463    /// name: when a marked row carries it, `/name` runs that one, and an
2464    /// unmarked row is the one `/name` runs only when no marked row shares
2465    /// its name (CLI 2.1.278+).
2466    #[serde(default, skip_serializing_if = "Option::is_none")]
2467    pub builtin: Option<bool>,
2468}
2469
2470#[derive(Debug, Clone, Serialize, Deserialize)]
2471pub struct NotificationMessage {
2472    pub key: String,
2473    pub text: String,
2474    pub priority: String,
2475    #[serde(default, skip_serializing_if = "Option::is_none")]
2476    pub color: Option<String>,
2477    #[serde(default, skip_serializing_if = "Option::is_none")]
2478    pub timeout_ms: Option<u64>,
2479    #[serde(default, skip_serializing_if = "Option::is_none")]
2480    pub uuid: Option<String>,
2481    #[serde(default, skip_serializing_if = "Option::is_none")]
2482    pub session_id: Option<String>,
2483}
2484
2485#[derive(Debug, Clone, Serialize, Deserialize)]
2486pub struct FilesPersistedMessage {
2487    pub files: Vec<PersistedFile>,
2488    pub failed: Vec<FailedPersistedFile>,
2489    pub processed_at: String,
2490    #[serde(default, skip_serializing_if = "Option::is_none")]
2491    pub uuid: Option<String>,
2492    #[serde(default, skip_serializing_if = "Option::is_none")]
2493    pub session_id: Option<String>,
2494}
2495
2496#[derive(Debug, Clone, Serialize, Deserialize)]
2497pub struct PersistedFile {
2498    pub filename: String,
2499    pub file_id: String,
2500}
2501
2502#[derive(Debug, Clone, Serialize, Deserialize)]
2503pub struct FailedPersistedFile {
2504    pub filename: String,
2505    pub error: String,
2506}
2507
2508#[derive(Debug, Clone, Serialize, Deserialize)]
2509pub struct MemoryRecallMessage {
2510    pub mode: String,
2511    pub memories: Vec<MemoryRecallItem>,
2512    #[serde(default, skip_serializing_if = "Option::is_none")]
2513    pub uuid: Option<String>,
2514    #[serde(default, skip_serializing_if = "Option::is_none")]
2515    pub session_id: Option<String>,
2516}
2517
2518#[derive(Debug, Clone, Serialize, Deserialize)]
2519pub struct MemoryRecallItem {
2520    pub path: String,
2521    pub scope: String,
2522    #[serde(default, skip_serializing_if = "Option::is_none")]
2523    pub content: Option<String>,
2524}
2525
2526#[derive(Debug, Clone, Serialize, Deserialize)]
2527pub struct ElicitationCompleteMessage {
2528    pub mcp_server_name: String,
2529    pub elicitation_id: String,
2530    #[serde(default, skip_serializing_if = "Option::is_none")]
2531    pub uuid: Option<String>,
2532    #[serde(default, skip_serializing_if = "Option::is_none")]
2533    pub session_id: Option<String>,
2534}
2535
2536#[derive(Debug, Clone, Serialize, Deserialize)]
2537pub struct PermissionDeniedMessage {
2538    pub tool_name: String,
2539    pub tool_use_id: String,
2540    #[serde(default, skip_serializing_if = "Option::is_none")]
2541    pub agent_id: Option<String>,
2542    #[serde(default, skip_serializing_if = "Option::is_none")]
2543    pub decision_reason_type: Option<String>,
2544    /// A closed-set code for a reason a host can act on, beside
2545    /// `decision_reason_type` (whose values are unchanged). Absent for
2546    /// every other reason; new values are additive (CLI 2.1.276+).
2547    #[serde(default, skip_serializing_if = "Option::is_none")]
2548    pub decision_reason_code: Option<PermissionDeniedReasonCode>,
2549    #[serde(default, skip_serializing_if = "Option::is_none")]
2550    pub decision_reason: Option<String>,
2551    pub message: String,
2552    #[serde(default, skip_serializing_if = "Option::is_none")]
2553    pub uuid: Option<String>,
2554    #[serde(default, skip_serializing_if = "Option::is_none")]
2555    pub session_id: Option<String>,
2556}
2557
2558/// An actionable reason behind a `system/permission_denied` frame
2559/// ([`PermissionDeniedMessage::decision_reason_code`]).
2560#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2561pub enum PermissionDeniedReasonCode {
2562    /// The auto-mode classifier's transcript exceeded its context window.
2563    ClassifierTranscriptTooLong,
2564    /// `permissions.blockReadsOutsideWorkingDirectories` refused the path.
2565    OutsideReadsBlocked,
2566    /// `/pause-memory` has memory paused.
2567    MemoryPaused,
2568    /// A code not yet known to this version of the crate.
2569    Unknown(String),
2570}
2571
2572impl PermissionDeniedReasonCode {
2573    pub fn as_str(&self) -> &str {
2574        match self {
2575            Self::ClassifierTranscriptTooLong => "classifier_transcript_too_long",
2576            Self::OutsideReadsBlocked => "outside_reads_blocked",
2577            Self::MemoryPaused => "memory_paused",
2578            Self::Unknown(s) => s.as_str(),
2579        }
2580    }
2581}
2582
2583impl fmt::Display for PermissionDeniedReasonCode {
2584    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2585        f.write_str(self.as_str())
2586    }
2587}
2588
2589impl From<&str> for PermissionDeniedReasonCode {
2590    fn from(s: &str) -> Self {
2591        match s {
2592            "classifier_transcript_too_long" => Self::ClassifierTranscriptTooLong,
2593            "outside_reads_blocked" => Self::OutsideReadsBlocked,
2594            "memory_paused" => Self::MemoryPaused,
2595            other => Self::Unknown(other.to_string()),
2596        }
2597    }
2598}
2599
2600impl Serialize for PermissionDeniedReasonCode {
2601    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2602        serializer.serialize_str(self.as_str())
2603    }
2604}
2605
2606impl<'de> Deserialize<'de> for PermissionDeniedReasonCode {
2607    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
2608        let s = String::deserialize(deserializer)?;
2609        Ok(Self::from(s.as_str()))
2610    }
2611}
2612
2613#[derive(Debug, Clone, Serialize, Deserialize)]
2614pub struct MirrorErrorMessage {
2615    pub error: String,
2616    pub key: MirrorErrorKey,
2617    #[serde(default, skip_serializing_if = "Option::is_none")]
2618    pub uuid: Option<String>,
2619    #[serde(default, skip_serializing_if = "Option::is_none")]
2620    pub session_id: Option<String>,
2621}
2622
2623#[derive(Debug, Clone, Serialize, Deserialize)]
2624pub struct MirrorErrorKey {
2625    #[serde(rename = "projectKey")]
2626    pub project_key: String,
2627    #[serde(rename = "sessionId")]
2628    pub session_id: String,
2629    #[serde(default, skip_serializing_if = "Option::is_none")]
2630    pub subpath: Option<String>,
2631}
2632
2633#[derive(Debug, Clone, Serialize, Deserialize)]
2634pub struct InformationalMessage {
2635    pub content: String,
2636    pub level: String,
2637    #[serde(default, skip_serializing_if = "Option::is_none")]
2638    pub tool_use_id: Option<String>,
2639    #[serde(default, skip_serializing_if = "Option::is_none")]
2640    pub prevent_continuation: Option<bool>,
2641    /// Opaque feature tag on a line a host may treat specially; absent on
2642    /// ordinary lines. Hosts ignore values they do not know (CLI 2.1.288+).
2643    #[serde(default, skip_serializing_if = "Option::is_none")]
2644    pub tag: Option<String>,
2645    /// Present on the line that ends a submitted prompt before a model call:
2646    /// how the prompt ended, so a host can write its own message or count
2647    /// endings without matching `content`. A follow-up a hook blocks while a
2648    /// turn runs carries it too, so read it as the end of a turn only when
2649    /// the next result has `num_turns` 0 (CLI 2.1.300+).
2650    #[serde(default, skip_serializing_if = "Option::is_none")]
2651    pub prompt_submit_outcome: Option<PromptSubmitOutcome>,
2652    #[serde(default, skip_serializing_if = "Option::is_none")]
2653    pub uuid: Option<String>,
2654    #[serde(default, skip_serializing_if = "Option::is_none")]
2655    pub session_id: Option<String>,
2656}
2657
2658/// How a submitted prompt ended before a model call, carried as
2659/// [`InformationalMessage::prompt_submit_outcome`] (CLI 2.1.300+).
2660#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
2661pub struct PromptSubmitOutcome {
2662    /// What ended the prompt. The CLI drops a kind it does not know, so the
2663    /// key can be absent; treat [`PromptSubmitOutcomeKind::Other`] as absent
2664    /// too, and ignore [`reason`](Self::reason) beside it.
2665    #[serde(default, skip_serializing_if = "Option::is_none")]
2666    pub kind: Option<PromptSubmitOutcomeKind>,
2667    /// Sent with kind `no_verdict`, and says why: `remote_run_failed`,
2668    /// `remote_run_timed_out`, `interrupted`, `remote_runner_refused`,
2669    /// `unsupported_in_cloud_session`, `output_distrusted`,
2670    /// `callback_timed_out`, `hook_failed`, `hook_timed_out`,
2671    /// `hooks_never_ran`, `personal_config_refused` or
2672    /// `rewrite_unplaceable`. Open set — treat an unknown value as absent.
2673    #[serde(default, skip_serializing_if = "Option::is_none")]
2674    pub no_verdict_kind: Option<String>,
2675    /// True when the prompt stays in the conversation, where the model reads
2676    /// it on the next turn. Do not infer it from `kind`.
2677    pub prompt_kept: bool,
2678    /// The hook's text as Claude Code got it, sanitized and possibly cut.
2679    /// Never sent with `no_verdict`; absent when Claude Code got no text.
2680    /// Untrusted and can quote the prompt, so render it as plain text.
2681    #[serde(default, skip_serializing_if = "Option::is_none")]
2682    pub reason: Option<String>,
2683}
2684
2685/// [`PromptSubmitOutcome::kind`] — what ended a submitted prompt.
2686#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2687pub enum PromptSubmitOutcomeKind {
2688    /// A hook answered with a block.
2689    BlockedByHook,
2690    /// A hook answered `continue: false`.
2691    StoppedByHook,
2692    /// A plugin's code module dropped the prompt.
2693    DroppedByHook,
2694    /// A plugin's code module answered and did not pass the prompt on.
2695    NotPassedOn,
2696    /// No hook gave a verdict, and Claude Code ended the prompt itself; see
2697    /// [`PromptSubmitOutcome::no_verdict_kind`].
2698    NoVerdict,
2699    /// A kind not yet known to this version of the crate.
2700    Other(String),
2701}
2702
2703impl PromptSubmitOutcomeKind {
2704    pub fn as_str(&self) -> &str {
2705        match self {
2706            Self::BlockedByHook => "blocked_by_hook",
2707            Self::StoppedByHook => "stopped_by_hook",
2708            Self::DroppedByHook => "dropped_by_hook",
2709            Self::NotPassedOn => "not_passed_on",
2710            Self::NoVerdict => "no_verdict",
2711            Self::Other(s) => s.as_str(),
2712        }
2713    }
2714}
2715
2716impl fmt::Display for PromptSubmitOutcomeKind {
2717    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2718        f.write_str(self.as_str())
2719    }
2720}
2721
2722impl From<&str> for PromptSubmitOutcomeKind {
2723    fn from(s: &str) -> Self {
2724        match s {
2725            "blocked_by_hook" => Self::BlockedByHook,
2726            "stopped_by_hook" => Self::StoppedByHook,
2727            "dropped_by_hook" => Self::DroppedByHook,
2728            "not_passed_on" => Self::NotPassedOn,
2729            "no_verdict" => Self::NoVerdict,
2730            other => Self::Other(other.to_string()),
2731        }
2732    }
2733}
2734
2735impl Serialize for PromptSubmitOutcomeKind {
2736    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2737        serializer.serialize_str(self.as_str())
2738    }
2739}
2740
2741impl<'de> Deserialize<'de> for PromptSubmitOutcomeKind {
2742    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
2743        let s = String::deserialize(deserializer)?;
2744        Ok(Self::from(s.as_str()))
2745    }
2746}
2747
2748/// Plugin info from the init message
2749#[derive(Debug, Clone, Serialize, Deserialize)]
2750pub struct PluginInfo {
2751    /// Plugin name
2752    pub name: String,
2753    /// Path to the plugin on disk
2754    pub path: String,
2755    /// Plugin registry source (e.g., "rust-analyzer-lsp@claude-plugins-official")
2756    #[serde(skip_serializing_if = "Option::is_none")]
2757    pub source: Option<String>,
2758    /// Installed plugin version (e.g., "1.0.0"). Added in CLI 2.1.219.
2759    #[serde(default, skip_serializing_if = "Option::is_none")]
2760    pub version: Option<String>,
2761}
2762
2763/// Plugin load diagnostic reported by system init.
2764#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
2765pub struct PluginDiagnostic {
2766    pub plugin: String,
2767    #[serde(rename = "type")]
2768    pub diagnostic_type: String,
2769    pub message: String,
2770}
2771
2772/// Memory paths reported by system init.
2773#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
2774pub struct MemoryPaths {
2775    #[serde(default, skip_serializing_if = "Option::is_none")]
2776    pub auto: Option<String>,
2777    #[serde(default, skip_serializing_if = "Option::is_none")]
2778    pub team: Option<String>,
2779    #[serde(flatten)]
2780    pub extra: serde_json::Map<String, Value>,
2781}
2782
2783/// An MCP server config entry that failed validation, reported by system
2784/// init (e.g. a `url` entry with no `type`). The affected server is skipped
2785/// and absent from `InitMessage::mcp_servers`.
2786#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
2787pub struct McpServerError {
2788    pub name: String,
2789    /// Stable error category.
2790    #[serde(rename = "type")]
2791    pub error_type: String,
2792    pub message: String,
2793}
2794
2795/// Init system message data - sent at session start
2796#[derive(Debug, Clone, Serialize, Deserialize)]
2797pub struct InitMessage {
2798    /// Session identifier
2799    pub session_id: String,
2800    /// Current working directory
2801    #[serde(skip_serializing_if = "Option::is_none")]
2802    pub cwd: Option<String>,
2803    /// Model being used
2804    #[serde(skip_serializing_if = "Option::is_none")]
2805    pub model: Option<String>,
2806    /// List of available tools
2807    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2808    pub tools: Vec<String>,
2809    /// MCP servers configured
2810    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2811    pub mcp_servers: Vec<Value>,
2812    /// Available slash commands (e.g., "compact", "cost", "review")
2813    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2814    pub slash_commands: Vec<String>,
2815    /// Slash commands only meaningful in a terminal context (CLI 2.1.232+,
2816    /// e.g. "doctor", "color")
2817    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2818    pub terminal_slash_commands: Vec<String>,
2819    /// Available agent types (e.g., "Bash", "Explore", "Plan")
2820    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2821    pub agents: Vec<String>,
2822    /// Installed plugins
2823    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2824    pub plugins: Vec<PluginInfo>,
2825    /// Installed skills
2826    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2827    pub skills: Vec<Value>,
2828    /// Claude Code CLI version
2829    #[serde(skip_serializing_if = "Option::is_none")]
2830    pub claude_code_version: Option<String>,
2831    /// Unix socket path for the harness's inter-session messaging bridge
2832    /// (new in CLI 2.1.232; absent on older CLIs and non-bridged runs)
2833    #[serde(skip_serializing_if = "Option::is_none")]
2834    pub messaging_socket_path: Option<String>,
2835    /// How the API key was sourced
2836    #[serde(skip_serializing_if = "Option::is_none", rename = "apiKeySource")]
2837    pub api_key_source: Option<ApiKeySource>,
2838    /// Output style
2839    #[serde(skip_serializing_if = "Option::is_none")]
2840    pub output_style: Option<OutputStyle>,
2841    /// Permission mode
2842    #[serde(skip_serializing_if = "Option::is_none", rename = "permissionMode")]
2843    pub permission_mode: Option<InitPermissionMode>,
2844
2845    /// Message-level unique identifier
2846    #[serde(skip_serializing_if = "Option::is_none")]
2847    pub uuid: Option<String>,
2848
2849    /// Memory storage paths (e.g., {"auto": "/path/to/memory/"})
2850    #[serde(skip_serializing_if = "Option::is_none")]
2851    pub memory_paths: Option<MemoryPaths>,
2852
2853    /// Absolute path of this session's scratchpad directory, for clients
2854    /// that read files from the session. Omitted when the scratchpad is
2855    /// disabled, and from the redacted init sent over the Remote Control
2856    /// bridge and by the hosted worker (CLI 2.1.278+).
2857    #[serde(default, skip_serializing_if = "Option::is_none")]
2858    pub scratchpad_path: Option<String>,
2859
2860    /// Fast mode toggle state (e.g., "off")
2861    #[serde(skip_serializing_if = "Option::is_none")]
2862    pub fast_mode_state: Option<String>,
2863
2864    /// Why fast mode can't serve right now. Absent when nothing blocks it.
2865    #[serde(default, skip_serializing_if = "Option::is_none")]
2866    pub fast_mode_disabled_reason: Option<super::result::FastModeDisabledReason>,
2867
2868    /// MCP server config entries (from `--mcp-config`) that failed validation
2869    /// and were skipped. Affected servers are absent from `mcp_servers`.
2870    #[serde(default, skip_serializing_if = "Option::is_none")]
2871    pub mcp_server_errors: Option<Vec<McpServerError>>,
2872
2873    /// Whether analytics collection is disabled for this session.
2874    #[serde(default, skip_serializing_if = "Option::is_none")]
2875    pub analytics_disabled: Option<bool>,
2876
2877    /// Whether product-feedback prompts are disabled for this session.
2878    #[serde(default, skip_serializing_if = "Option::is_none")]
2879    pub product_feedback_disabled: Option<bool>,
2880
2881    /// API beta flags active for the session.
2882    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2883    pub betas: Vec<String>,
2884
2885    /// Open-set protocol capability names supported by this CLI.
2886    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2887    pub capabilities: Vec<String>,
2888
2889    /// Plugin load errors.
2890    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2891    pub plugin_errors: Vec<PluginDiagnostic>,
2892
2893    /// Plugin load warnings.
2894    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2895    pub plugin_warnings: Vec<PluginDiagnostic>,
2896
2897    /// The effort level the session will send on its next request — after env
2898    /// overrides, session state, org caps, and model-support downgrades
2899    /// (`"low"` | `"medium"` | `"high"` | `"xhigh"` | `"max"`). `None` when no
2900    /// effort parameter will be sent, or on CLIs before 2.1.239.
2901    #[serde(default, skip_serializing_if = "Option::is_none")]
2902    pub effort: Option<String>,
2903
2904    /// Only on init frames written by the headless stream-json client of a
2905    /// cloud-hosted session: a per-frame snapshot of the cloud session's id,
2906    /// view URL, device binding, and directory-sync state. Absent in every
2907    /// other mode. Stored as raw JSON (the shape is internal and evolving).
2908    #[serde(default, skip_serializing_if = "Option::is_none")]
2909    pub cloud_session: Option<Value>,
2910
2911    /// The terminal's server-configured `◆ <text>` footer pill, carried so a
2912    /// host UI can render the same pill. Absent when nothing is configured
2913    /// (CLI 2.1.259+).
2914    #[serde(default, skip_serializing_if = "Option::is_none")]
2915    pub footer_indicator: Option<FooterIndicator>,
2916
2917    /// This cloud worker's life (`CLAUDE_CODE_WORKER_EPOCH`): a new number
2918    /// each time the session's worker is started. Absent outside cloud
2919    /// workers and on CLIs before 2.1.259.
2920    #[serde(default, skip_serializing_if = "Option::is_none")]
2921    pub worker_epoch: Option<u64>,
2922
2923    /// Windows only: the absolute path of the PowerShell binary this session
2924    /// runs PowerShell commands with, or `Some(None)` (wire `null`) when
2925    /// none was found. Absent on other platforms and on CLIs before 2.1.259.
2926    #[serde(default, skip_serializing_if = "Option::is_none")]
2927    pub powershell_path: Option<Option<String>>,
2928
2929    /// Cold-start telemetry for hosted (CCR) sessions: named startup phases
2930    /// and resume-hydration counters. Absent elsewhere. Stored as raw JSON
2931    /// (the shape is internal and evolving) (CLI 2.1.266+).
2932    #[serde(default, skip_serializing_if = "Option::is_none")]
2933    pub startup_timing: Option<serde_json::Map<String, Value>>,
2934
2935    /// Whether per-turn effort is active for this frame's `model`. `true`:
2936    /// an effort change keeps the prompt cache, because the effort is sent
2937    /// inside the conversation at the turn where it changes. `false`: the
2938    /// effort is sent only as the top-level parameter, so a change rewrites
2939    /// the cached prefix. Mid-session the CLI reports only a change to
2940    /// `false` (`system/per_turn_effort_changed`); `true` comes only from an
2941    /// init and is provisional after a fresh process or once the
2942    /// conversation's refusals are cleared, so keep listening all session.
2943    /// Absent means unknown (CLI 2.1.281+).
2944    #[serde(default, skip_serializing_if = "Option::is_none")]
2945    pub per_turn_effort_active: Option<bool>,
2946
2947    /// Whether the session's transcript is in focus view. Toggled by
2948    /// `/focus`, including over Remote Control. Present on Remote Control
2949    /// bridge init frames and on the per-turn init of headless stream-json
2950    /// runs; absent on hosts that do not publish it and on CLIs before
2951    /// 2.1.281. Re-emitted inits carry the current value — the newest frame
2952    /// wins.
2953    #[serde(default, skip_serializing_if = "Option::is_none")]
2954    pub view_mode: Option<ViewMode>,
2955}
2956
2957/// The server-configured session indicator that the terminal renders as a
2958/// `◆ <text>` pill in the prompt footer, carried on `system/init` and the
2959/// `initialize` response (CLI 2.1.259+).
2960#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
2961pub struct FooterIndicator {
2962    /// The label to show — already sanitized to a single line of plain text,
2963    /// exactly as the terminal footer renders it after its `◆` glyph.
2964    pub text: String,
2965}
2966
2967/// The session's transcript view, carried as [`InitMessage::view_mode`]
2968/// (CLI 2.1.281+).
2969#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2970pub enum ViewMode {
2971    /// The model is told the user sees only its final message per turn, so
2972    /// clients may collapse each turn to the prompt and the final response.
2973    Focus,
2974    /// The ordinary full transcript.
2975    Default,
2976    /// A mode not yet known to this version of the crate.
2977    Unknown(String),
2978}
2979
2980impl ViewMode {
2981    pub fn as_str(&self) -> &str {
2982        match self {
2983            Self::Focus => "focus",
2984            Self::Default => "default",
2985            Self::Unknown(s) => s.as_str(),
2986        }
2987    }
2988}
2989
2990impl fmt::Display for ViewMode {
2991    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2992        f.write_str(self.as_str())
2993    }
2994}
2995
2996impl From<&str> for ViewMode {
2997    fn from(s: &str) -> Self {
2998        match s {
2999            "focus" => Self::Focus,
3000            "default" => Self::Default,
3001            other => Self::Unknown(other.to_string()),
3002        }
3003    }
3004}
3005
3006impl Serialize for ViewMode {
3007    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3008        serializer.serialize_str(self.as_str())
3009    }
3010}
3011
3012impl<'de> Deserialize<'de> for ViewMode {
3013    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
3014        let s = String::deserialize(deserializer)?;
3015        Ok(Self::from(s.as_str()))
3016    }
3017}
3018
3019/// Status system message - sent during operations like context compaction
3020#[derive(Debug, Clone, Serialize, Deserialize)]
3021pub struct StatusMessage {
3022    /// Session identifier
3023    pub session_id: String,
3024    /// Current status (e.g., compacting) or null when complete
3025    pub status: Option<StatusMessageStatus>,
3026    /// Unique identifier for this message
3027    #[serde(skip_serializing_if = "Option::is_none")]
3028    pub uuid: Option<String>,
3029    /// Current permission mode when changed mid-session.
3030    #[serde(skip_serializing_if = "Option::is_none", rename = "permissionMode")]
3031    pub permission_mode: Option<InitPermissionMode>,
3032    #[serde(skip_serializing_if = "Option::is_none")]
3033    pub compact_result: Option<String>,
3034    #[serde(skip_serializing_if = "Option::is_none")]
3035    pub compact_error: Option<String>,
3036    /// On the `compacting` statuses of an automatic compaction (absent on a
3037    /// typed `/compact`): characters of the summary streamed so far (text
3038    /// and tool-input deltas, not thinking). Returns to 0 at each new attempt
3039    /// and stays 0 while a background summary is awaited. A rising count
3040    /// shows progress; it is not a share of the final length. Absent from
3041    /// CLIs before 2.1.295. Marked `@internal` upstream.
3042    #[serde(default, skip_serializing_if = "Option::is_none")]
3043    pub compact_output_chars: Option<u64>,
3044}
3045
3046/// Compact boundary message - marks where context compaction occurred
3047#[derive(Debug, Clone, Serialize, Deserialize)]
3048pub struct CompactBoundaryMessage {
3049    /// Session identifier
3050    pub session_id: String,
3051    /// Metadata about the compaction
3052    pub compact_metadata: CompactMetadata,
3053    /// Human-readable summary of what was compacted, when the CLI emits one.
3054    ///
3055    /// Also accepted under the `content` / `text` wire keys.
3056    #[serde(
3057        default,
3058        skip_serializing_if = "Option::is_none",
3059        alias = "content",
3060        alias = "text"
3061    )]
3062    pub summary: Option<String>,
3063    /// Number of messages summarized in this compaction pass, when present.
3064    ///
3065    /// Also accepted under the `message_count` wire key.
3066    #[serde(
3067        default,
3068        skip_serializing_if = "Option::is_none",
3069        alias = "message_count"
3070    )]
3071    pub leaf_message_count: Option<u32>,
3072    /// Wall-clock duration of the compaction pass in milliseconds, when present.
3073    #[serde(default, skip_serializing_if = "Option::is_none")]
3074    pub duration_ms: Option<u64>,
3075    /// Unique identifier for this message
3076    #[serde(skip_serializing_if = "Option::is_none")]
3077    pub uuid: Option<String>,
3078    /// Logical parent across the compaction boundary.
3079    #[serde(skip_serializing_if = "Option::is_none")]
3080    pub logical_parent_uuid: Option<Option<String>>,
3081    /// Replayed history rather than a live message, stamped by the Remote
3082    /// Control bridge when it flushes history to the session server
3083    /// (CLI 2.1.266+).
3084    #[serde(default, skip_serializing_if = "Option::is_none")]
3085    pub historical: Option<bool>,
3086}
3087
3088/// Metadata about context compaction
3089#[derive(Debug, Clone, Serialize, Deserialize)]
3090pub struct CompactMetadata {
3091    /// Number of tokens before compaction
3092    pub pre_tokens: u64,
3093    /// What triggered the compaction
3094    pub trigger: CompactionTrigger,
3095    #[serde(default, skip_serializing_if = "Option::is_none")]
3096    pub post_tokens: Option<u64>,
3097    #[serde(default, skip_serializing_if = "Option::is_none")]
3098    pub cumulative_dropped_tokens: Option<u64>,
3099    #[serde(default, skip_serializing_if = "Option::is_none")]
3100    pub duration_ms: Option<u64>,
3101    #[serde(default, skip_serializing_if = "Option::is_none")]
3102    pub user_context: Option<String>,
3103    #[serde(default, skip_serializing_if = "Option::is_none")]
3104    pub messages_summarized: Option<u64>,
3105    #[serde(default, skip_serializing_if = "Option::is_none")]
3106    pub precomputed: Option<bool>,
3107    #[serde(default, skip_serializing_if = "Option::is_none")]
3108    pub pre_compact_discovered_tools: Option<Vec<String>>,
3109    /// The artifact versions the conversation held before this compaction,
3110    /// most recently changed first; empty when it held none. A resumed
3111    /// session replays these so it can update those artifacts without
3112    /// reading them again (CLI 2.1.285+).
3113    #[serde(default, skip_serializing_if = "Option::is_none")]
3114    pub pre_compact_artifact_read_versions: Option<Vec<ArtifactReadVersion>>,
3115    #[serde(default, skip_serializing_if = "Option::is_none")]
3116    pub preserved_segment: Option<PreservedSegment>,
3117    #[serde(default, skip_serializing_if = "Option::is_none")]
3118    pub preserved_messages: Option<PreservedMessages>,
3119}
3120
3121/// One artifact version a conversation held before a compaction, carried in
3122/// [`CompactMetadata::pre_compact_artifact_read_versions`].
3123#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
3124pub struct ArtifactReadVersion {
3125    /// The artifact id.
3126    pub slug: String,
3127    /// The version read or published.
3128    pub ver: String,
3129}
3130
3131#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
3132pub struct PreservedSegment {
3133    pub head_uuid: String,
3134    pub anchor_uuid: String,
3135    pub tail_uuid: String,
3136}
3137
3138#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
3139pub struct PreservedMessages {
3140    pub anchor_uuid: String,
3141    pub uuids: Vec<String>,
3142    #[serde(default, skip_serializing_if = "Option::is_none")]
3143    pub all_uuids: Option<Vec<String>>,
3144}
3145
3146// ---------------------------------------------------------------------------
3147// Task system message types (task_started, task_progress, task_notification)
3148// ---------------------------------------------------------------------------
3149
3150/// Cumulative usage statistics for a background task.
3151#[derive(Debug, Clone, Serialize, Deserialize)]
3152pub struct TaskUsage {
3153    /// Wall-clock milliseconds since the task started.
3154    pub duration_ms: u64,
3155    /// Total number of tool calls made so far.
3156    pub tool_uses: u64,
3157    /// Total tokens consumed so far.
3158    pub total_tokens: u64,
3159}
3160
3161/// The kind of background task.
3162#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3163pub enum TaskType {
3164    /// A sub-agent task (e.g., Explore, Plan).
3165    LocalAgent,
3166    /// A background bash command.
3167    LocalBash,
3168    /// A local workflow task.
3169    LocalWorkflow,
3170    /// A task type not yet known to this version of the crate.
3171    Unknown(String),
3172}
3173
3174impl TaskType {
3175    pub fn as_str(&self) -> &str {
3176        match self {
3177            Self::LocalAgent => "local_agent",
3178            Self::LocalBash => "local_bash",
3179            Self::LocalWorkflow => "local_workflow",
3180            Self::Unknown(s) => s.as_str(),
3181        }
3182    }
3183}
3184
3185impl fmt::Display for TaskType {
3186    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3187        f.write_str(self.as_str())
3188    }
3189}
3190
3191impl From<&str> for TaskType {
3192    fn from(s: &str) -> Self {
3193        match s {
3194            "local_agent" => Self::LocalAgent,
3195            "local_bash" => Self::LocalBash,
3196            "local_workflow" => Self::LocalWorkflow,
3197            other => Self::Unknown(other.to_string()),
3198        }
3199    }
3200}
3201
3202impl Serialize for TaskType {
3203    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3204        serializer.serialize_str(self.as_str())
3205    }
3206}
3207
3208impl<'de> Deserialize<'de> for TaskType {
3209    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
3210        let s = String::deserialize(deserializer)?;
3211        Ok(Self::from(s.as_str()))
3212    }
3213}
3214
3215/// Completion status of a background task.
3216#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3217pub enum TaskStatus {
3218    Pending,
3219    Running,
3220    Completed,
3221    Failed,
3222    Killed,
3223    Paused,
3224    Stopped,
3225    Unknown(String),
3226}
3227
3228impl TaskStatus {
3229    pub fn as_str(&self) -> &str {
3230        match self {
3231            Self::Pending => "pending",
3232            Self::Running => "running",
3233            Self::Completed => "completed",
3234            Self::Failed => "failed",
3235            Self::Killed => "killed",
3236            Self::Paused => "paused",
3237            Self::Stopped => "stopped",
3238            Self::Unknown(s) => s.as_str(),
3239        }
3240    }
3241}
3242
3243impl fmt::Display for TaskStatus {
3244    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3245        f.write_str(self.as_str())
3246    }
3247}
3248
3249impl From<&str> for TaskStatus {
3250    fn from(s: &str) -> Self {
3251        match s {
3252            "pending" => Self::Pending,
3253            "running" => Self::Running,
3254            "completed" => Self::Completed,
3255            "failed" => Self::Failed,
3256            "killed" => Self::Killed,
3257            "paused" => Self::Paused,
3258            "stopped" => Self::Stopped,
3259            other => Self::Unknown(other.to_string()),
3260        }
3261    }
3262}
3263
3264impl Serialize for TaskStatus {
3265    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3266        serializer.serialize_str(self.as_str())
3267    }
3268}
3269
3270impl<'de> Deserialize<'de> for TaskStatus {
3271    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
3272        let s = String::deserialize(deserializer)?;
3273        Ok(Self::from(s.as_str()))
3274    }
3275}
3276
3277/// `task_started` system message — emitted once when a background task begins.
3278#[derive(Debug, Clone, Serialize, Deserialize)]
3279pub struct TaskStartedMessage {
3280    pub session_id: String,
3281    pub task_id: String,
3282    /// Id of one run of the task: equal on every `task_*` event and
3283    /// `background_tasks_changed` entry of that run. A resumed task keeps its
3284    /// `task_id` and gets a new `run_id`; the run ids of one task sort, by
3285    /// plain string comparison, in the order the runs opened. Absent for a
3286    /// task this process did not register (CLI 2.1.292+).
3287    #[serde(default, skip_serializing_if = "Option::is_none")]
3288    pub run_id: Option<String>,
3289    /// `task_id` of the subagent (`local_agent`) task whose agent launched
3290    /// this task. Absent when the launcher is the main thread, has no
3291    /// `task_id` of its own, or is no longer tracked. The parent may be a
3292    /// foreground or already-ended task, so treat an unknown id as no parent
3293    /// (CLI 2.1.292+).
3294    #[serde(default, skip_serializing_if = "Option::is_none")]
3295    pub parent_task_id: Option<String>,
3296    #[serde(default, skip_serializing_if = "Option::is_none")]
3297    pub task_type: Option<TaskType>,
3298    #[serde(default, skip_serializing_if = "Option::is_none")]
3299    pub tool_use_id: Option<String>,
3300    pub description: String,
3301    /// The subagent type for `local_agent` tasks (e.g. `general-purpose`,
3302    /// `Explore`). Absent for `local_bash` tasks.
3303    #[serde(default, skip_serializing_if = "Option::is_none")]
3304    pub subagent_type: Option<String>,
3305    /// Whether the task was registered in the background (`true`) or in the
3306    /// foreground with the spawning tool call blocking on it (`false`). A
3307    /// later move to the background arrives as `task_updated`
3308    /// `patch.is_backgrounded`. Set for `local_agent` and `local_bash` tasks
3309    /// (CLI 2.1.239+).
3310    #[serde(default, skip_serializing_if = "Option::is_none")]
3311    pub is_backgrounded: Option<bool>,
3312    /// True for a run of a resumed subagent (`local_agent`) that the tool
3313    /// call which resumed it waits for, as `SendMessage` does when
3314    /// background tasks are disabled; [`tool_use_id`](Self::tool_use_id)
3315    /// then names that call. The run has ended by the time that call
3316    /// returns, and its report goes into that call's result, so expect no
3317    /// follow-up turn when it ends. A later resume that nothing waits for
3318    /// starts a new run without the field (CLI 2.1.293+).
3319    #[serde(default, skip_serializing_if = "Option::is_none")]
3320    pub awaited: Option<bool>,
3321    /// Nesting depth of a spawned subagent (`local_agent`) task: 1 for a
3322    /// top-level spawn, N+1 when spawned from inside a depth-N agent. Not set
3323    /// on other tasks (CLI 2.1.239+).
3324    #[serde(default, skip_serializing_if = "Option::is_none")]
3325    pub spawn_depth: Option<u32>,
3326    /// The prompt handed to the subagent. Present for `local_agent` tasks.
3327    #[serde(default, skip_serializing_if = "Option::is_none")]
3328    pub prompt: Option<String>,
3329    #[serde(default, skip_serializing_if = "Option::is_none")]
3330    pub workflow_name: Option<String>,
3331    #[serde(default, skip_serializing_if = "Option::is_none")]
3332    pub skip_transcript: Option<bool>,
3333    /// True for housekeeping tasks the CLI does not surface as user work
3334    /// (every `skip_transcript` task, plus auto-started live-update
3335    /// watchers); hosts should exclude them from activity indicators
3336    /// (CLI 2.1.259+).
3337    #[serde(default, skip_serializing_if = "Option::is_none")]
3338    pub ambient: Option<bool>,
3339    pub uuid: String,
3340}
3341
3342/// `task_updated` system message — emitted when a background task's state
3343/// changes (e.g. transitions to `completed`). Carries a partial `patch` of the
3344/// fields that changed rather than the full task record.
3345#[derive(Debug, Clone, Serialize, Deserialize)]
3346pub struct TaskUpdatedMessage {
3347    pub session_id: String,
3348    pub task_id: String,
3349    /// Id of the task run this update belongs to; see
3350    /// [`TaskStartedMessage::run_id`] (CLI 2.1.292+).
3351    #[serde(default, skip_serializing_if = "Option::is_none")]
3352    pub run_id: Option<String>,
3353    pub patch: TaskPatch,
3354    pub uuid: String,
3355}
3356
3357/// The partial update carried by a [`TaskUpdatedMessage`]. Every field is
3358/// optional because the CLI only sends the keys that changed.
3359#[derive(Debug, Clone, Default, Serialize, Deserialize)]
3360pub struct TaskPatch {
3361    #[serde(default, skip_serializing_if = "Option::is_none")]
3362    pub status: Option<TaskStatus>,
3363    /// Wall-clock epoch milliseconds when the task finished, when the patch
3364    /// reports completion.
3365    #[serde(default, skip_serializing_if = "Option::is_none")]
3366    pub end_time: Option<u64>,
3367    #[serde(default, skip_serializing_if = "Option::is_none")]
3368    pub description: Option<String>,
3369    #[serde(default, skip_serializing_if = "Option::is_none")]
3370    pub total_paused_ms: Option<u64>,
3371    #[serde(default, skip_serializing_if = "Option::is_none")]
3372    pub error: Option<String>,
3373    #[serde(default, skip_serializing_if = "Option::is_none")]
3374    pub is_backgrounded: Option<bool>,
3375}
3376
3377/// `thinking_tokens` system message — emitted as the model streams extended
3378/// thinking, reporting the running estimate of thinking tokens consumed.
3379#[derive(Debug, Clone, Serialize, Deserialize)]
3380pub struct ThinkingTokensMessage {
3381    pub session_id: String,
3382    /// Running estimate of total thinking tokens for the current turn.
3383    pub estimated_tokens: u64,
3384    /// Increase in the estimate since the previous `thinking_tokens` event.
3385    pub estimated_tokens_delta: u64,
3386    /// Client uuid of the user message that triggered this turn, stamped on
3387    /// every `thinking_tokens` frame of a headless turn so a consumer can
3388    /// attribute thinking progress to the send it answers before any reply
3389    /// frame arrives. Absent on synthetic/scheduled (meta) turns, on turns
3390    /// without a client uuid, on Remote Control sessions, and from CLIs
3391    /// before 2.1.261.
3392    #[serde(default, skip_serializing_if = "Option::is_none")]
3393    pub user_message_uuid: Option<String>,
3394    pub uuid: String,
3395}
3396
3397/// `task_progress` system message — emitted periodically as a background
3398/// agent task executes tools. Not emitted for `local_bash` tasks.
3399#[derive(Debug, Clone, Serialize, Deserialize)]
3400pub struct TaskProgressMessage {
3401    pub session_id: String,
3402    pub task_id: String,
3403    /// Id of the task run this progress belongs to; see
3404    /// [`TaskStartedMessage::run_id`] (CLI 2.1.292+).
3405    #[serde(default, skip_serializing_if = "Option::is_none")]
3406    pub run_id: Option<String>,
3407    #[serde(default, skip_serializing_if = "Option::is_none")]
3408    pub tool_use_id: Option<String>,
3409    pub description: String,
3410    #[serde(default, skip_serializing_if = "Option::is_none")]
3411    pub last_tool_name: Option<String>,
3412    pub usage: TaskUsage,
3413    /// Subagent type for `local_agent` tasks (e.g. `Explore`).
3414    #[serde(default, skip_serializing_if = "Option::is_none")]
3415    pub subagent_type: Option<String>,
3416    #[serde(default, skip_serializing_if = "Option::is_none")]
3417    pub summary: Option<String>,
3418    pub uuid: String,
3419}
3420
3421/// `task_notification` system message — emitted once when a background
3422/// task completes or fails.
3423#[derive(Debug, Clone, Serialize, Deserialize)]
3424pub struct TaskNotificationMessage {
3425    pub session_id: String,
3426    pub task_id: String,
3427    /// Id of the task run this notification closes; see
3428    /// [`TaskStartedMessage::run_id`] (CLI 2.1.292+).
3429    #[serde(default, skip_serializing_if = "Option::is_none")]
3430    pub run_id: Option<String>,
3431    pub status: TaskStatus,
3432    /// Machine-readable cause, set only when the task did not end through an
3433    /// ordinary completion, failure, or stop (CLI 2.1.273+).
3434    #[serde(default, skip_serializing_if = "Option::is_none")]
3435    pub reason: Option<TaskEndReason>,
3436    pub summary: String,
3437    pub output_file: Option<String>,
3438    #[serde(skip_serializing_if = "Option::is_none")]
3439    pub tool_use_id: Option<String>,
3440    #[serde(skip_serializing_if = "Option::is_none")]
3441    pub usage: Option<TaskUsage>,
3442    /// For a backgrounded MCP task that completed, the `resource_link`
3443    /// content blocks of its final result — the files it returned by
3444    /// reference — collected from the raw result before the CLI renders it
3445    /// as text. Join to the originating call via `tool_use_id`. Absent when
3446    /// the result had none or the task is any other type (CLI 2.1.259+).
3447    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3448    pub resource_links: Vec<ResourceLink>,
3449    /// On a `completed` notification of a subagent that reports through the
3450    /// SubagentHandback tool: how its report reached its caller. Left out
3451    /// when the subagent failed, was stopped, or is still waiting on work;
3452    /// `summary` is then not a report. When present, `summary` holds harness
3453    /// notes written for the model. A later notification for the same
3454    /// `task_id` replaces what an earlier one showed (CLI 2.1.292+).
3455    #[serde(default, skip_serializing_if = "Option::is_none")]
3456    pub handback: Option<TaskHandback>,
3457    /// The report a `send` or `flagged` hand-back delivered: when present,
3458    /// render it instead of `summary`, with its `warning`, if any, above its
3459    /// `text`. Absent for `withheld` (CLI 2.1.292+).
3460    #[serde(default, skip_serializing_if = "Option::is_none")]
3461    pub handback_report: Option<HandbackReport>,
3462    #[serde(default, skip_serializing_if = "Option::is_none")]
3463    pub skip_transcript: Option<bool>,
3464    /// True for housekeeping tasks the CLI does not surface as user work;
3465    /// hosts should exclude them from activity indicators (CLI 2.1.259+).
3466    #[serde(default, skip_serializing_if = "Option::is_none")]
3467    pub ambient: Option<bool>,
3468    #[serde(skip_serializing_if = "Option::is_none")]
3469    pub uuid: Option<String>,
3470}
3471
3472/// How a subagent's hand-back report reached its caller
3473/// ([`TaskNotificationMessage::handback`]).
3474#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3475pub enum TaskHandback {
3476    /// The report was delivered.
3477    Send,
3478    /// The report was delivered with an auto-mode warning.
3479    Flagged,
3480    /// The report was held back; no `handback_report` is sent.
3481    Withheld,
3482    /// A value not yet known to this version of the crate.
3483    Unknown(String),
3484}
3485
3486impl TaskHandback {
3487    pub fn as_str(&self) -> &str {
3488        match self {
3489            Self::Send => "send",
3490            Self::Flagged => "flagged",
3491            Self::Withheld => "withheld",
3492            Self::Unknown(s) => s.as_str(),
3493        }
3494    }
3495}
3496
3497impl fmt::Display for TaskHandback {
3498    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3499        f.write_str(self.as_str())
3500    }
3501}
3502
3503impl From<&str> for TaskHandback {
3504    fn from(s: &str) -> Self {
3505        match s {
3506            "send" => Self::Send,
3507            "flagged" => Self::Flagged,
3508            "withheld" => Self::Withheld,
3509            other => Self::Unknown(other.to_string()),
3510        }
3511    }
3512}
3513
3514impl Serialize for TaskHandback {
3515    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3516        serializer.serialize_str(self.as_str())
3517    }
3518}
3519
3520impl<'de> Deserialize<'de> for TaskHandback {
3521    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
3522        let s = String::deserialize(deserializer)?;
3523        Ok(Self::from(s.as_str()))
3524    }
3525}
3526
3527/// A subagent's delivered hand-back report
3528/// ([`TaskNotificationMessage::handback_report`]).
3529#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
3530pub struct HandbackReport {
3531    /// The subagent's whole report, never truncated, with a backslash
3532    /// inserted into text that imitates harness markup.
3533    pub text: String,
3534    /// Auto mode's warning to show above `text`: a security warning, or a
3535    /// note that the review could not run.
3536    #[serde(default, skip_serializing_if = "Option::is_none")]
3537    pub warning: Option<String>,
3538}
3539
3540/// API error category attached to assistant wrapper frames.
3541#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3542pub enum AssistantErrorKind {
3543    AuthenticationFailed,
3544    OauthOrgNotAllowed,
3545    AccountOnHold,
3546    BillingError,
3547    RateLimit,
3548    Overloaded,
3549    InvalidRequest,
3550    ModelNotFound,
3551    ServerError,
3552    UnknownError,
3553    MaxOutputTokens,
3554    Unknown(String),
3555}
3556
3557impl AssistantErrorKind {
3558    pub fn as_str(&self) -> &str {
3559        match self {
3560            Self::AuthenticationFailed => "authentication_failed",
3561            Self::OauthOrgNotAllowed => "oauth_org_not_allowed",
3562            Self::AccountOnHold => "account_on_hold",
3563            Self::BillingError => "billing_error",
3564            Self::RateLimit => "rate_limit",
3565            Self::Overloaded => "overloaded",
3566            Self::InvalidRequest => "invalid_request",
3567            Self::ModelNotFound => "model_not_found",
3568            Self::ServerError => "server_error",
3569            Self::UnknownError => "unknown",
3570            Self::MaxOutputTokens => "max_output_tokens",
3571            Self::Unknown(s) => s.as_str(),
3572        }
3573    }
3574}
3575
3576impl fmt::Display for AssistantErrorKind {
3577    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3578        f.write_str(self.as_str())
3579    }
3580}
3581
3582impl From<&str> for AssistantErrorKind {
3583    fn from(s: &str) -> Self {
3584        match s {
3585            "authentication_failed" => Self::AuthenticationFailed,
3586            "oauth_org_not_allowed" => Self::OauthOrgNotAllowed,
3587            "account_on_hold" => Self::AccountOnHold,
3588            "billing_error" => Self::BillingError,
3589            "rate_limit" => Self::RateLimit,
3590            "overloaded" => Self::Overloaded,
3591            "invalid_request" => Self::InvalidRequest,
3592            "model_not_found" => Self::ModelNotFound,
3593            "server_error" => Self::ServerError,
3594            "unknown" => Self::UnknownError,
3595            "max_output_tokens" => Self::MaxOutputTokens,
3596            other => Self::Unknown(other.to_string()),
3597        }
3598    }
3599}
3600
3601impl Serialize for AssistantErrorKind {
3602    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3603        serializer.serialize_str(self.as_str())
3604    }
3605}
3606
3607impl<'de> Deserialize<'de> for AssistantErrorKind {
3608    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
3609        let s = String::deserialize(deserializer)?;
3610        Ok(Self::from(s.as_str()))
3611    }
3612}
3613
3614/// `code_change_published` system message — the session is now associated
3615/// with a published code change (a pull/merge request). Fires on creation and
3616/// whenever the session contributes to an existing one, so bind on every
3617/// event; re-emission for the same URL is possible and idempotent. Values are
3618/// scraped from captured command output — treat them as a binding hint and
3619/// verify against the forge before routing authenticated requests.
3620#[derive(Debug, Clone, Serialize, Deserialize)]
3621pub struct CodeChangePublishedMessage {
3622    /// Forge classification derived from the URL's shape (`github`,
3623    /// `github-enterprise`, `gitlab`, `bitbucket`, `gerrit` today). Open set
3624    /// — treat an unknown value as a valid provider, never as an error.
3625    pub provider: String,
3626    /// Web URL of the pull/merge request. Unverified.
3627    pub url: String,
3628    /// Repository path from the URL (`owner/name` on GitHub; may carry more
3629    /// segments on GitLab).
3630    pub repo: String,
3631    /// Provider-native change identifier — the PR/MR number as a string.
3632    pub identifier: String,
3633    /// What the session did that produced this announcement: the flag-aware
3634    /// `gh pr` verb it ran (`"created"`, `"edited"`, `"merged"`,
3635    /// `"commented"`, `"closed"`, `"reopened"`, `"ready"`, `"draft"`,
3636    /// `"auto-merge-enabled"`, `"auto-merge-disabled"`), `"pushed"` for a
3637    /// push to a branch that has a PR, `"checked-out"` for `gh pr checkout`,
3638    /// or `"started"` for the open change on the branch a Claude Desktop
3639    /// session began on. Always sent by current producers (CLI 2.1.239+),
3640    /// absent only from older ones. Open set — treat unknown values as valid.
3641    #[serde(default, skip_serializing_if = "Option::is_none")]
3642    pub action: Option<String>,
3643    /// The session's working branch when it produced the change. Sent for
3644    /// providers whose changes have no head branch of their own (`gerrit`),
3645    /// so a host can place the change on that checkout, and with `created`
3646    /// on any provider: the branch the create was opened from (its `--head`
3647    /// / `--source-branch` flag, else the working branch), so a host can show
3648    /// the new change before its own forge lookup answers (CLI 2.1.273+).
3649    /// Absent otherwise (CLI 2.1.259+).
3650    #[serde(default, skip_serializing_if = "Option::is_none")]
3651    pub branch: Option<String>,
3652    pub uuid: String,
3653    pub session_id: String,
3654}
3655
3656/// `vcs_state_changed` system message — a harness-observed shell command
3657/// mutated repository state. A cache-invalidation signal, deliberately
3658/// payload-free beyond classification: consumers re-read state (branch, head,
3659/// PR status) instead of decoding the event.
3660#[derive(Debug, Clone, Serialize, Deserialize)]
3661pub struct VcsStateChangedMessage {
3662    /// What class of mutation was observed. New kinds may be added — treat an
3663    /// unrecognized kind exactly like a recognized one (something changed).
3664    pub kind: VcsMutationKind,
3665    /// The session's working directory — a hint, not necessarily the mutated
3666    /// repo's path (`git -C` or an inner `cd` mutates elsewhere).
3667    pub cwd: String,
3668    /// The branch a commit landed on or a push updated. Commit and push
3669    /// events carry it; a command that pushed several branches emits one push
3670    /// event per branch. A best-effort hint: absent whenever attribution is
3671    /// uncertain, and never a required key (CLI 2.1.239+).
3672    #[serde(default, skip_serializing_if = "Option::is_none")]
3673    pub branch: Option<String>,
3674    pub uuid: String,
3675    pub session_id: String,
3676}
3677
3678/// Mutation class carried by a [`VcsStateChangedMessage`].
3679#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3680pub enum VcsMutationKind {
3681    Commit,
3682    Push,
3683    Merge,
3684    Rebase,
3685    /// A kind not yet known to this version of the crate.
3686    Unknown(String),
3687}
3688
3689impl VcsMutationKind {
3690    pub fn as_str(&self) -> &str {
3691        match self {
3692            Self::Commit => "commit",
3693            Self::Push => "push",
3694            Self::Merge => "merge",
3695            Self::Rebase => "rebase",
3696            Self::Unknown(s) => s.as_str(),
3697        }
3698    }
3699}
3700
3701impl fmt::Display for VcsMutationKind {
3702    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3703        f.write_str(self.as_str())
3704    }
3705}
3706
3707impl From<&str> for VcsMutationKind {
3708    fn from(s: &str) -> Self {
3709        match s {
3710            "commit" => Self::Commit,
3711            "push" => Self::Push,
3712            "merge" => Self::Merge,
3713            "rebase" => Self::Rebase,
3714            other => Self::Unknown(other.to_string()),
3715        }
3716    }
3717}
3718
3719impl Serialize for VcsMutationKind {
3720    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3721        serializer.serialize_str(self.as_str())
3722    }
3723}
3724
3725impl<'de> Deserialize<'de> for VcsMutationKind {
3726    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
3727        let s = String::deserialize(deserializer)?;
3728        Ok(Self::from(s.as_str()))
3729    }
3730}
3731
3732/// `system/feedback_draft_queued` — a feedback draft was queued for submission.
3733#[derive(Debug, Clone, Serialize, Deserialize)]
3734pub struct FeedbackDraftQueuedMessage {
3735    pub draft_id: String,
3736    pub draft_type: String,
3737    pub title: String,
3738    pub details_preview: String,
3739    #[serde(default, skip_serializing_if = "Option::is_none")]
3740    pub uuid: Option<String>,
3741    #[serde(default, skip_serializing_if = "Option::is_none")]
3742    pub session_id: Option<String>,
3743    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
3744    pub extra: serde_json::Map<String, Value>,
3745}
3746
3747/// `system/cloud_session_delta` — written only by the headless stream-json
3748/// client of a cloud-hosted session (the same client that puts
3749/// `cloud_session` on its init frames): the session's status changed between
3750/// two inits. Only after the first init; at most a few per second; none when
3751/// nothing differs. An init is always a complete snapshot, so a host that
3752/// (re)attaches resynchronises from the first init it reads and applies
3753/// these on top. Display-only (CLI 2.1.260+).
3754#[derive(Debug, Clone, Serialize, Deserialize)]
3755pub struct CloudSessionDeltaMessage {
3756    /// Rises by one with each of these frames this client writes (1 for the
3757    /// first); never reset by an init. A reader keeps the highest it has
3758    /// applied and drops a lower one.
3759    pub seq: u64,
3760    /// The top-level keys of `cloud_session` whose value differs from the
3761    /// last block this client wrote, e.g. `["serving"]`; never empty. A hint
3762    /// for what to redraw — the block is complete either way.
3763    pub changed: Vec<String>,
3764    /// The whole block exactly as the next init would carry it (see
3765    /// [`InitMessage::cloud_session`]): replace the held copy, do not merge.
3766    /// Stored as raw JSON (the shape is internal and evolving).
3767    pub cloud_session: Value,
3768    pub uuid: String,
3769    pub session_id: String,
3770    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
3771    pub extra: serde_json::Map<String, Value>,
3772}
3773
3774/// `system/dev_intent` — the conversation, or the git repository it runs in,
3775/// shows a known kind of development work, for hosts that key tooling on it
3776/// (Claude Code Desktop opens its iOS Simulator entry point on `ios_app`).
3777/// Sent with `trigger` as its only other payload. Per conversation per
3778/// process, each kind is sent at most once from conversation evidence and at
3779/// most once from the project scan: conversation evidence when it first
3780/// completes, or at startup when a resumed conversation already has it;
3781/// project evidence (print-mode CLIs only, which is how the Agent SDK starts
3782/// it) at startup, after each `conversation_reset`, and at the first message
3783/// of a conversation whose earlier scan did not find every kind. Either can
3784/// arrive before `system/init`. A rewind or compaction never retracts it and
3785/// only a `conversation_reset` starts over, so treat each kind as a sticky
3786/// fact about the conversation: a client that needs only the kind can ignore
3787/// repeats, and one that needs to know what set it off reads `trigger`
3788/// (CLI 2.1.266+; `trigger` and the project scan from 2.1.273).
3789#[derive(Debug, Clone, Serialize, Deserialize)]
3790pub struct DevIntentMessage {
3791    /// What kind of development the evidence shows.
3792    pub kind: DevIntentKind,
3793    /// The evidence the detection fired on, for a host's own analytics. From
3794    /// the conversation it is the first platform-specific evidence seen;
3795    /// [`DevIntentTrigger::ProjectScan`] marks a scan of the session's git
3796    /// repository. Absent from CLIs before 2.1.273, which send only
3797    /// conversation evidence.
3798    #[serde(default, skip_serializing_if = "Option::is_none")]
3799    pub trigger: Option<DevIntentTrigger>,
3800    pub uuid: String,
3801    pub session_id: String,
3802    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
3803    pub extra: serde_json::Map<String, Value>,
3804}
3805
3806/// The kind of development a [`DevIntentMessage`] reports. Open set: the CLI
3807/// says "more kinds will be added; ignore a kind you do not recognize", so
3808/// unrecognized values deserialize to [`DevIntentKind::Unknown`].
3809#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3810pub enum DevIntentKind {
3811    /// From the conversation: Claude wrote or edited a `.swift` file and
3812    /// something it wrote, read, or ran is iOS-specific (a macOS-only app or
3813    /// server-side Swift package never qualifies). From the project scan: the
3814    /// session's git repository has an Xcode project whose build settings
3815    /// name an iOS SDK or target iPhone or iPad.
3816    IosApp,
3817    /// From the conversation: Claude wrote or edited a `.kt`, `.kts` or
3818    /// `.java` file and something it wrote, read, or ran is Android-specific
3819    /// (a Kotlin server or a multiplatform module with no Android target
3820    /// never qualifies). From the project scan: the repository has an
3821    /// `AndroidManifest.xml` or the Android Gradle plugin in a build script
3822    /// or version catalog (CLI 2.1.273+).
3823    AndroidApp,
3824    /// A kind not yet known to this version of the crate.
3825    Unknown(String),
3826}
3827
3828impl DevIntentKind {
3829    pub fn as_str(&self) -> &str {
3830        match self {
3831            Self::IosApp => "ios_app",
3832            Self::AndroidApp => "android_app",
3833            Self::Unknown(s) => s.as_str(),
3834        }
3835    }
3836}
3837
3838impl fmt::Display for DevIntentKind {
3839    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3840        f.write_str(self.as_str())
3841    }
3842}
3843
3844impl From<&str> for DevIntentKind {
3845    fn from(s: &str) -> Self {
3846        match s {
3847            "ios_app" => Self::IosApp,
3848            "android_app" => Self::AndroidApp,
3849            other => Self::Unknown(other.to_string()),
3850        }
3851    }
3852}
3853
3854impl Serialize for DevIntentKind {
3855    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3856        serializer.serialize_str(self.as_str())
3857    }
3858}
3859
3860impl<'de> Deserialize<'de> for DevIntentKind {
3861    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
3862        let s = String::deserialize(deserializer)?;
3863        Ok(Self::from(s.as_str()))
3864    }
3865}
3866
3867/// The evidence a [`DevIntentMessage`] detection fired on. Open set: the CLI
3868/// says "more values will be added; ignore one you do not recognize", so
3869/// unrecognized values deserialize to [`DevIntentTrigger::Unknown`].
3870#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3871pub enum DevIntentTrigger {
3872    /// Reserved for rules that need no other evidence; no rule sends it yet.
3873    SwiftEdit,
3874    /// `import UIKit` or `.iOS(` in text Claude wrote.
3875    UikitImport,
3876    /// iOS build settings written, or seen in a tool result such as a
3877    /// pbxproj read.
3878    XcodeProject,
3879    /// `simctl`, an iOS SDK or a Simulator destination in a command Claude
3880    /// ran.
3881    IosCommand,
3882    /// Reserved for rules that need no other evidence; no rule sends it yet.
3883    KotlinEdit,
3884    /// Reserved for rules that need no other evidence; no rule sends it yet.
3885    JavaEdit,
3886    /// `import android.` in text Claude wrote.
3887    AndroidImport,
3888    /// A manifest path or body written, or seen in a tool result.
3889    AndroidManifest,
3890    /// The Android Gradle plugin written, or seen in a tool result.
3891    GradlePlugin,
3892    /// `adb`, the emulator, `sdkmanager`, `avdmanager`,
3893    /// `react-native run-android` or a Gradle variant task.
3894    AndroidCommand,
3895    /// For any kind: a scan of the session's git repository rather than
3896    /// conversation evidence.
3897    ProjectScan,
3898    /// A trigger not yet known to this version of the crate.
3899    Unknown(String),
3900}
3901
3902impl DevIntentTrigger {
3903    pub fn as_str(&self) -> &str {
3904        match self {
3905            Self::SwiftEdit => "swift_edit",
3906            Self::UikitImport => "uikit_import",
3907            Self::XcodeProject => "xcode_project",
3908            Self::IosCommand => "ios_command",
3909            Self::KotlinEdit => "kotlin_edit",
3910            Self::JavaEdit => "java_edit",
3911            Self::AndroidImport => "android_import",
3912            Self::AndroidManifest => "android_manifest",
3913            Self::GradlePlugin => "gradle_plugin",
3914            Self::AndroidCommand => "android_command",
3915            Self::ProjectScan => "project_scan",
3916            Self::Unknown(s) => s.as_str(),
3917        }
3918    }
3919}
3920
3921impl fmt::Display for DevIntentTrigger {
3922    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3923        f.write_str(self.as_str())
3924    }
3925}
3926
3927impl From<&str> for DevIntentTrigger {
3928    fn from(s: &str) -> Self {
3929        match s {
3930            "swift_edit" => Self::SwiftEdit,
3931            "uikit_import" => Self::UikitImport,
3932            "xcode_project" => Self::XcodeProject,
3933            "ios_command" => Self::IosCommand,
3934            "kotlin_edit" => Self::KotlinEdit,
3935            "java_edit" => Self::JavaEdit,
3936            "android_import" => Self::AndroidImport,
3937            "android_manifest" => Self::AndroidManifest,
3938            "gradle_plugin" => Self::GradlePlugin,
3939            "android_command" => Self::AndroidCommand,
3940            "project_scan" => Self::ProjectScan,
3941            other => Self::Unknown(other.to_string()),
3942        }
3943    }
3944}
3945
3946impl Serialize for DevIntentTrigger {
3947    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3948        serializer.serialize_str(self.as_str())
3949    }
3950}
3951
3952impl<'de> Deserialize<'de> for DevIntentTrigger {
3953    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
3954        let s = String::deserialize(deserializer)?;
3955        Ok(Self::from(s.as_str()))
3956    }
3957}
3958
3959/// Why a [`TaskNotificationMessage`] ended other than through an ordinary
3960/// completion, failure, or stop. Open set: unrecognized values deserialize
3961/// to [`TaskEndReason::Unknown`].
3962#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3963pub enum TaskEndReason {
3964    /// The worker process restarted and the resumed process found the task
3965    /// orphaned (always with status `stopped`).
3966    WorkerRestart,
3967    /// A reason not yet known to this version of the crate.
3968    Unknown(String),
3969}
3970
3971impl TaskEndReason {
3972    pub fn as_str(&self) -> &str {
3973        match self {
3974            Self::WorkerRestart => "worker_restart",
3975            Self::Unknown(s) => s.as_str(),
3976        }
3977    }
3978}
3979
3980impl fmt::Display for TaskEndReason {
3981    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3982        f.write_str(self.as_str())
3983    }
3984}
3985
3986impl From<&str> for TaskEndReason {
3987    fn from(s: &str) -> Self {
3988        match s {
3989            "worker_restart" => Self::WorkerRestart,
3990            other => Self::Unknown(other.to_string()),
3991        }
3992    }
3993}
3994
3995impl Serialize for TaskEndReason {
3996    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3997        serializer.serialize_str(self.as_str())
3998    }
3999}
4000
4001impl<'de> Deserialize<'de> for TaskEndReason {
4002    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
4003        let s = String::deserialize(deserializer)?;
4004        Ok(Self::from(s.as_str()))
4005    }
4006}
4007
4008/// `system/turn_handoff_available` — emitted once by a cloud worker that
4009/// accepts the `turn_handoff` control request, right after it registers,
4010/// carrying what its registration wrote to `external_metadata.turn_handoff`.
4011/// Lets a session client learn the capability from the event stream instead
4012/// of reading worker state. Durable in the stream: a reader keeps the entry
4013/// with the newest `worker_epoch` it has seen and ignores older ones; a
4014/// worker life that does not accept `turn_handoff` emits nothing
4015/// (CLI 2.1.273+).
4016#[derive(Debug, Clone, Serialize, Deserialize)]
4017pub struct TurnHandoffAvailableMessage {
4018    /// Contract version of the handoff registration (currently `1`).
4019    pub v: u64,
4020    /// The tools whose calls this worker would accept.
4021    pub tools: Vec<String>,
4022    /// The worker life announcing it.
4023    pub worker_epoch: u64,
4024    /// Present, and true, only when this worker uses the `relay_marker`
4025    /// member of a `turn_handoff` request; a client sends that member to no
4026    /// other worker.
4027    #[serde(default, skip_serializing_if = "Option::is_none")]
4028    pub relay_marker: Option<bool>,
4029    /// Present, and true, when this worker starts a handed-off turn's calls
4030    /// only after every `stage_file` control request it received earlier has
4031    /// finished or failed, or a wait limit has passed; a worker that omits
4032    /// it may start them while files are still downloading (CLI 2.1.278+).
4033    /// Exception: a request with `filestore_path` set (a synced file) is not
4034    /// waited for, so a call that names that file may start before it is
4035    /// synced (CLI 2.1.287+).
4036    #[serde(default, skip_serializing_if = "Option::is_none")]
4037    pub staged_files: Option<bool>,
4038    /// True when user messages with `shouldQuery` false that reach this
4039    /// worker before a `turn_handoff` request are appended, and
4040    /// acknowledged, before the handed-off turn produces any output.
4041    /// Exception: a request that continues a turn whose leading lines an
4042    /// earlier request appended; that turn goes first. A worker that omits
4043    /// it may run the turn first (CLI 2.1.278+).
4044    #[serde(default, skip_serializing_if = "Option::is_none")]
4045    pub no_query_first: Option<bool>,
4046    /// Present, and true, only on a worker that takes a `stage_file` control
4047    /// request whose `mount_path` is `/uploads/.home/<path>` as a file for
4048    /// `/home/claude/<path>`: it writes the file there, answers ok with a
4049    /// noop (`already_present`, `path_occupied`, `refused_name`,
4050    /// `home_files_off`, `unchecked_announcement`, `resent_announcement`)
4051    /// and writes nothing, or answers an error. Any other worker treats that
4052    /// path as an ordinary upload (CLI 2.1.287+).
4053    #[serde(default, skip_serializing_if = "Option::is_none")]
4054    pub home_files: Option<bool>,
4055    /// Present, and true, only when this worker uses the `file_names` member
4056    /// of a `turn_handoff` request; a client sends that member to no other
4057    /// worker (CLI 2.1.287+).
4058    #[serde(default, skip_serializing_if = "Option::is_none")]
4059    pub file_names: Option<bool>,
4060    /// Present, and true, only on a worker that writes the files of the
4061    /// Write calls a `turn_handoff` request brings already answered, where
4062    /// their paths are under `/home/claude`. Before it appends a turn that
4063    /// has calls to run, it writes each such call's content to its path,
4064    /// replacing nothing. If it cannot write them all, it appends and runs
4065    /// nothing: the request is refused with an error that starts
4066    /// `invalid_handoff: carried_writes_`, or it is accepted and the turn
4067    /// ends in a result with `num_turns` 0. Any other worker writes no file
4068    /// for these calls (CLI 2.1.288+).
4069    #[serde(default, skip_serializing_if = "Option::is_none")]
4070    pub carried_writes: Option<bool>,
4071    /// Present, and true, only on a worker that rebuilt the conversation
4072    /// from the session's stored transcript before it read any request, and
4073    /// would not have started without it. A client may send such a worker
4074    /// again a `turn_handoff` request an earlier worker accepted, if the
4075    /// request names no calls (empty `tool_use_ids`), is not stopped, and its
4076    /// messages hold no tool call: the worker appends only the turn's
4077    /// messages its conversation lacks and, once the store confirms them,
4078    /// echoes the turn's last message back as an assistant event. The
4079    /// request can still be accepted or refused (`invalid_handoff:`) with no
4080    /// echo, e.g. after a rewind, a compaction or a Stop; an echo proves
4081    /// nothing if the same worker had the request before. The key promises
4082    /// nothing for a turn that holds a tool call. A worker without it may
4083    /// have started with none of the turn's messages (CLI 2.1.296+).
4084    #[serde(default, skip_serializing_if = "Option::is_none")]
4085    pub hydrates_carried_lines: Option<bool>,
4086    /// Present, and true, when this worker accepts a `turn_handoff` request
4087    /// in which an assistant message repeats the message id of the assistant
4088    /// message before it. A worker that omits it refuses such a request
4089    /// (`invalid_handoff`), so a client gives each message an id of its own
4090    /// there (CLI 2.1.300+).
4091    #[serde(default, skip_serializing_if = "Option::is_none")]
4092    pub shared_message_id: Option<bool>,
4093    pub uuid: String,
4094    pub session_id: String,
4095    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
4096    pub extra: serde_json::Map<String, Value>,
4097}
4098
4099/// `system/turn_preempted` — the CLI itself stopped the running turn so a
4100/// user's rapid follow-up message is answered at once, exactly as a priority
4101/// `now` message would have (running shell commands are backgrounded, not
4102/// killed). Sent at the moment of the stop, so it precedes the stopped turn's
4103/// `result` frame (`terminal_reason` `aborted_streaming` or `aborted_tools`)
4104/// and its members' `cancelled` `command_lifecycle` frames: a host renders
4105/// that turn as superseded by the follow-up rather than as interrupted, does
4106/// not resend its messages, and treats only `preempted_by_uuid` as picked
4107/// up. At most one per burst. Emitted in `-p`/SDK sessions only, and only to
4108/// a consumer that declared `rapidFollowupPreempt` on its initialize request
4109/// while the feature's rollout flag is on (CLI 2.1.273+).
4110#[derive(Debug, Clone, Serialize, Deserialize)]
4111pub struct TurnPreemptedMessage {
4112    /// Why the turn was stopped. `rapid_followup`: the user's next message
4113    /// arrived before the running turn showed any output. More reasons may
4114    /// be added; treat an unknown one the same way.
4115    pub reason: String,
4116    /// The client-supplied uuid of the queued user message the turn was
4117    /// stopped for. It runs next, together with any messages queued behind
4118    /// it.
4119    pub preempted_by_uuid: String,
4120    /// The client-supplied uuids of the user messages the stopped turn was
4121    /// running (uuid-less members omitted; may be empty).
4122    #[serde(default)]
4123    pub preempted_message_uuids: Vec<String>,
4124    pub uuid: String,
4125    pub session_id: String,
4126    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
4127    pub extra: serde_json::Map<String, Value>,
4128}
4129
4130/// `system/peer_message_hold` — a cross-session (peer) message this
4131/// session's receive-side policy held rather than queued, and how that hold
4132/// resolved. Lets a host show the human that a message arrived but has not
4133/// reached the model (and may never) instead of nothing at all; the sending
4134/// session is told separately over its own transport where one exists.
4135/// Informational only — there is no host-side approval through this frame.
4136/// Emitted in `-p`/SDK sessions (CLI 2.1.273+).
4137#[derive(Debug, Clone, Serialize, Deserialize)]
4138pub struct PeerMessageHoldMessage {
4139    /// `held` once per message per hold cause (a re-announcement under a
4140    /// different cause emits again); then at most one of `released` (it
4141    /// enters the queue — its `command_lifecycle` `queued` and user replay
4142    /// echo follow under `message_uuid`) or `dropped` (it will never reach
4143    /// the model in this session; see `outcome`).
4144    pub state: PeerMessageHoldState,
4145    /// The parked command's uuid — the value its later `command_lifecycle`
4146    /// frames and user replay echo carry. For lane `bridge`/`stdin` it is
4147    /// the id the host supplied on the inbound message; for lane `socket` it
4148    /// was chosen by the sending session, so correlate it only within
4149    /// `peer_message_hold` / peer replay frames, never against the host's
4150    /// own prompts' lifecycle.
4151    #[serde(default, skip_serializing_if = "Option::is_none")]
4152    pub message_uuid: Option<String>,
4153    /// Which ingress delivered it.
4154    pub lane: PeerMessageLane,
4155    /// The sender's address as the envelope will show it — an address-shaped
4156    /// token with control and invisible code points scrubbed; empty when the
4157    /// sender supplied none or an unshaped one. Sender-asserted on the
4158    /// socket lane: a label, not an identity proof.
4159    pub from: String,
4160    /// The sender's display name when it supplied one, normalized and
4161    /// scrubbed the same way. A claim, like `from`.
4162    #[serde(default, skip_serializing_if = "Option::is_none")]
4163    pub from_name: Option<String>,
4164    /// State `held` only: why the policy parked it.
4165    #[serde(default, skip_serializing_if = "Option::is_none")]
4166    pub cause: Option<PeerMessageHoldCause>,
4167    /// State `dropped` only: how the hold ended.
4168    #[serde(default, skip_serializing_if = "Option::is_none")]
4169    pub outcome: Option<PeerMessageHoldOutcome>,
4170    pub uuid: String,
4171    pub session_id: String,
4172    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
4173    pub extra: serde_json::Map<String, Value>,
4174}
4175
4176/// Lifecycle state carried by a [`PeerMessageHoldMessage`].
4177#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4178pub enum PeerMessageHoldState {
4179    /// The receive-side policy parked the message instead of queueing it.
4180    Held,
4181    /// The policy now accepts it: it enters the queue.
4182    Released,
4183    /// It will never reach the model in this session.
4184    Dropped,
4185    /// A state not yet known to this version of the crate.
4186    Unknown(String),
4187}
4188
4189impl PeerMessageHoldState {
4190    pub fn as_str(&self) -> &str {
4191        match self {
4192            Self::Held => "held",
4193            Self::Released => "released",
4194            Self::Dropped => "dropped",
4195            Self::Unknown(s) => s.as_str(),
4196        }
4197    }
4198}
4199
4200impl fmt::Display for PeerMessageHoldState {
4201    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4202        f.write_str(self.as_str())
4203    }
4204}
4205
4206impl From<&str> for PeerMessageHoldState {
4207    fn from(s: &str) -> Self {
4208        match s {
4209            "held" => Self::Held,
4210            "released" => Self::Released,
4211            "dropped" => Self::Dropped,
4212            other => Self::Unknown(other.to_string()),
4213        }
4214    }
4215}
4216
4217impl Serialize for PeerMessageHoldState {
4218    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
4219        serializer.serialize_str(self.as_str())
4220    }
4221}
4222
4223impl<'de> Deserialize<'de> for PeerMessageHoldState {
4224    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
4225        let s = String::deserialize(deserializer)?;
4226        Ok(Self::from(s.as_str()))
4227    }
4228}
4229
4230/// Which ingress delivered the message a [`PeerMessageHoldMessage`] reports.
4231#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4232pub enum PeerMessageLane {
4233    /// The Remote Control bridge.
4234    Bridge,
4235    /// The host's own stdin stream.
4236    Stdin,
4237    /// The local cross-session socket.
4238    Socket,
4239    /// A lane not yet known to this version of the crate.
4240    Unknown(String),
4241}
4242
4243impl PeerMessageLane {
4244    pub fn as_str(&self) -> &str {
4245        match self {
4246            Self::Bridge => "bridge",
4247            Self::Stdin => "stdin",
4248            Self::Socket => "socket",
4249            Self::Unknown(s) => s.as_str(),
4250        }
4251    }
4252}
4253
4254impl fmt::Display for PeerMessageLane {
4255    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4256        f.write_str(self.as_str())
4257    }
4258}
4259
4260impl From<&str> for PeerMessageLane {
4261    fn from(s: &str) -> Self {
4262        match s {
4263            "bridge" => Self::Bridge,
4264            "stdin" => Self::Stdin,
4265            "socket" => Self::Socket,
4266            other => Self::Unknown(other.to_string()),
4267        }
4268    }
4269}
4270
4271impl Serialize for PeerMessageLane {
4272    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
4273        serializer.serialize_str(self.as_str())
4274    }
4275}
4276
4277impl<'de> Deserialize<'de> for PeerMessageLane {
4278    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
4279        let s = String::deserialize(deserializer)?;
4280        Ok(Self::from(s.as_str()))
4281    }
4282}
4283
4284/// Why the receive-side policy parked a peer message
4285/// ([`PeerMessageHoldMessage::cause`], state `held` only). The `*Setting`
4286/// causes are a standing `crossSessionInbound: "hold"`; `ModeMismatch` and
4287/// `NoModeAsserted` are the permission-mode parity holds (the sender runs in
4288/// a different permission class, or asserted none while this session
4289/// bypasses permissions).
4290#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4291pub enum PeerMessageHoldCause {
4292    ExplicitSetting,
4293    ManagedSetting,
4294    RepoSetting,
4295    InvalidSetting,
4296    BypassDefault,
4297    ModeUnknown,
4298    ModeMismatch,
4299    NoModeAsserted,
4300    /// A cause not yet known to this version of the crate.
4301    Unknown(String),
4302}
4303
4304impl PeerMessageHoldCause {
4305    pub fn as_str(&self) -> &str {
4306        match self {
4307            Self::ExplicitSetting => "explicit-setting",
4308            Self::ManagedSetting => "managed-setting",
4309            Self::RepoSetting => "repo-setting",
4310            Self::InvalidSetting => "invalid-setting",
4311            Self::BypassDefault => "bypass-default",
4312            Self::ModeUnknown => "mode-unknown",
4313            Self::ModeMismatch => "mode-mismatch",
4314            Self::NoModeAsserted => "no-mode-asserted",
4315            Self::Unknown(s) => s.as_str(),
4316        }
4317    }
4318}
4319
4320impl fmt::Display for PeerMessageHoldCause {
4321    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4322        f.write_str(self.as_str())
4323    }
4324}
4325
4326impl From<&str> for PeerMessageHoldCause {
4327    fn from(s: &str) -> Self {
4328        match s {
4329            "explicit-setting" => Self::ExplicitSetting,
4330            "managed-setting" => Self::ManagedSetting,
4331            "repo-setting" => Self::RepoSetting,
4332            "invalid-setting" => Self::InvalidSetting,
4333            "bypass-default" => Self::BypassDefault,
4334            "mode-unknown" => Self::ModeUnknown,
4335            "mode-mismatch" => Self::ModeMismatch,
4336            "no-mode-asserted" => Self::NoModeAsserted,
4337            other => Self::Unknown(other.to_string()),
4338        }
4339    }
4340}
4341
4342impl Serialize for PeerMessageHoldCause {
4343    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
4344        serializer.serialize_str(self.as_str())
4345    }
4346}
4347
4348impl<'de> Deserialize<'de> for PeerMessageHoldCause {
4349    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
4350        let s = String::deserialize(deserializer)?;
4351        Ok(Self::from(s.as_str()))
4352    }
4353}
4354
4355/// How a peer-message hold ended ([`PeerMessageHoldMessage::outcome`],
4356/// state `dropped` only).
4357#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4358pub enum PeerMessageHoldOutcome {
4359    /// No one approved it before the approval deadline (a headless host has
4360    /// no approval surface, so every parity hold ends this way unless the
4361    /// mode changes first).
4362    Expired,
4363    /// The policy turned to refuse while it waited.
4364    Refused,
4365    /// Released or approved, but the ingress guard (rate limit, duplicate,
4366    /// queue cap) discarded it.
4367    Dropped,
4368    /// The session ended with it still parked.
4369    Discarded,
4370    /// An outcome not yet known to this version of the crate.
4371    Unknown(String),
4372}
4373
4374impl PeerMessageHoldOutcome {
4375    pub fn as_str(&self) -> &str {
4376        match self {
4377            Self::Expired => "expired",
4378            Self::Refused => "refused",
4379            Self::Dropped => "dropped",
4380            Self::Discarded => "discarded",
4381            Self::Unknown(s) => s.as_str(),
4382        }
4383    }
4384}
4385
4386impl fmt::Display for PeerMessageHoldOutcome {
4387    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4388        f.write_str(self.as_str())
4389    }
4390}
4391
4392impl From<&str> for PeerMessageHoldOutcome {
4393    fn from(s: &str) -> Self {
4394        match s {
4395            "expired" => Self::Expired,
4396            "refused" => Self::Refused,
4397            "dropped" => Self::Dropped,
4398            "discarded" => Self::Discarded,
4399            other => Self::Unknown(other.to_string()),
4400        }
4401    }
4402}
4403
4404impl Serialize for PeerMessageHoldOutcome {
4405    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
4406        serializer.serialize_str(self.as_str())
4407    }
4408}
4409
4410impl<'de> Deserialize<'de> for PeerMessageHoldOutcome {
4411    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
4412        let s = String::deserialize(deserializer)?;
4413        Ok(Self::from(s.as_str()))
4414    }
4415}
4416
4417/// `system/per_turn_effort_changed` — the conversation stopped sending effort
4418/// per turn, because the server refused its per-turn effort message or a
4419/// `role:"system"` message. From the retried request on, an effort change
4420/// rewrites the cached prefix, for every model, until a later `system/init`
4421/// says otherwise. Sent once, when the conversation first turns per-turn
4422/// effort off, before the retried request's output; sent again only if the
4423/// server refuses again after the conversation's refusals are cleared (for
4424/// example by `/clear` or a compaction). It can repeat a `false` the host
4425/// already holds. Pairs with [`InitMessage::per_turn_effort_active`]
4426/// (CLI 2.1.281+).
4427#[derive(Debug, Clone, Serialize, Deserialize)]
4428pub struct PerTurnEffortChangedMessage {
4429    /// Always `false`: a change to `true` is reported only by `system/init`.
4430    pub per_turn_effort_active: bool,
4431    pub uuid: String,
4432    pub session_id: String,
4433}
4434
4435/// `system/instruction_size_warning` — the session's instruction files
4436/// (CLAUDE.md files, rules files and the files they import) add up to more
4437/// than the recommended limit. Numbers only: the host words the warning in
4438/// the person's language. Sent to Claude Desktop's Code tab alone, at most
4439/// once per process, on a conversation's first turn after `system/init`;
4440/// never stored in the transcript. Safe to ignore (CLI 2.1.288+).
4441#[derive(Debug, Clone, Serialize, Deserialize)]
4442pub struct InstructionSizeWarningMessage {
4443    /// Combined length, in characters, of the instruction files in the
4444    /// session prompt. Always over [`Self::total_limit_chars`].
4445    pub total_chars: u64,
4446    /// The recommended limit for `total_chars` on the turn's model, in
4447    /// characters.
4448    pub total_limit_chars: u64,
4449    /// Number of files counted in `total_chars`. When it is 1, word the
4450    /// warning for a single file rather than a total.
4451    pub file_count: u64,
4452    /// Length of the largest file, in characters. Present only when the
4453    /// warning should mention it: there are several files and this one by
4454    /// itself is over `total_limit_chars`.
4455    #[serde(default, skip_serializing_if = "Option::is_none")]
4456    pub largest_chars: Option<u64>,
4457    pub uuid: String,
4458    pub session_id: String,
4459}
4460
4461/// `system/file_attachments_missing` — some files a user message sent by
4462/// reference (`file_attachments`) did not arrive where Claude can read them,
4463/// and Claude was told so; the turn runs on without them. A host can use it
4464/// to tell the person who sent them. Only sent by a worker connected to
4465/// Claude Code Remote. Each event states the whole list for its message, so
4466/// keep the last one per `message_uuid` (CLI 2.1.290+).
4467#[derive(Debug, Clone, Serialize, Deserialize)]
4468pub struct FileAttachmentsMissingMessage {
4469    /// The uuid of the inbound user message whose `file_attachments` named
4470    /// the files.
4471    pub message_uuid: String,
4472    /// How many well-formed `file_attachments` entries that message had.
4473    pub sent_count: u64,
4474    /// The files Claude was told did not arrive, each once. Never empty.
4475    pub missing: Vec<MissingFileAttachment>,
4476    pub uuid: String,
4477    pub session_id: String,
4478}
4479
4480/// `system/permission_check_status` — a tool call has been waiting on its
4481/// automatic permission check (the auto-mode classifier) for longer than
4482/// usual, so a host can say the step is being checked instead of showing a
4483/// silent pause. Every `checking` is followed by a `done` for the same
4484/// `tool_use_id`; `done` does not say whether the call was allowed (the
4485/// tool's result, `permission_denied` or a `can_use_tool` request does). One
4486/// call can send more than one pair. Display-only and best-effort: also stop
4487/// showing it at any other frame for this `tool_use_id` or at the end of the
4488/// turn (CLI 2.1.292+).
4489#[derive(Debug, Clone, Serialize, Deserialize)]
4490pub struct PermissionCheckStatusMessage {
4491    pub tool_use_id: String,
4492    /// Subagent ID when the tool call originated inside a subagent, as on
4493    /// `permission_denied`.
4494    #[serde(default, skip_serializing_if = "Option::is_none")]
4495    pub agent_id: Option<String>,
4496    pub status: PermissionCheckStatus,
4497    pub uuid: String,
4498    pub session_id: String,
4499}
4500
4501/// Phase of a [`PermissionCheckStatusMessage`].
4502#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4503pub enum PermissionCheckStatus {
4504    /// The call has waited about four seconds on its check and is still
4505    /// waiting.
4506    Checking,
4507    /// That wait is over.
4508    Done,
4509    /// A status not yet known to this version of the crate.
4510    Unknown(String),
4511}
4512
4513impl PermissionCheckStatus {
4514    pub fn as_str(&self) -> &str {
4515        match self {
4516            Self::Checking => "checking",
4517            Self::Done => "done",
4518            Self::Unknown(s) => s.as_str(),
4519        }
4520    }
4521}
4522
4523impl fmt::Display for PermissionCheckStatus {
4524    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4525        f.write_str(self.as_str())
4526    }
4527}
4528
4529impl From<&str> for PermissionCheckStatus {
4530    fn from(s: &str) -> Self {
4531        match s {
4532            "checking" => Self::Checking,
4533            "done" => Self::Done,
4534            other => Self::Unknown(other.to_string()),
4535        }
4536    }
4537}
4538
4539impl Serialize for PermissionCheckStatus {
4540    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
4541        serializer.serialize_str(self.as_str())
4542    }
4543}
4544
4545impl<'de> Deserialize<'de> for PermissionCheckStatus {
4546    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
4547        let s = String::deserialize(deserializer)?;
4548        Ok(Self::from(s.as_str()))
4549    }
4550}
4551
4552/// One entry of [`FileAttachmentsMissingMessage::missing`].
4553#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
4554pub struct MissingFileAttachment {
4555    /// The `file_uuid` of the message's `file_attachments` entry.
4556    pub file_uuid: String,
4557    /// Why, as a short code: `download`, `too_large`, `not_found` (the
4558    /// files route answered 404 or 410, so sending it again rarely helps;
4559    /// CLI 2.1.293+), `write`, `no_token`, `not_finished` or `count_cap`.
4560    /// More codes may be added; read an unknown one as "did not arrive".
4561    pub reason: String,
4562}
4563
4564/// `system/session_title_changed` — the session's name, for a host that
4565/// shows it (an IDE editor tab, say). A headless session sends it at startup
4566/// when the session already has a name, and again after each change to the
4567/// name (not an AI-generated title), including a rename the host itself sent
4568/// with `rename_session`. A cleared name is not sent. The same name can
4569/// arrive more than once, so treat a repeated name as no change. The frame
4570/// can arrive before the `system/init` that first names its `session_id`
4571/// (at startup or after `/clear`); a host that learns the session id from
4572/// init can hold the name until then (CLI 2.1.285+).
4573#[derive(Debug, Clone, Serialize, Deserialize)]
4574pub struct SessionTitleChangedMessage {
4575    /// The session's current name: control, bidirectional and zero-width
4576    /// characters replaced with single spaces, ends trimmed, at most 200
4577    /// Unicode code points. A suffix can make it unique when another session
4578    /// already has the name.
4579    pub title: String,
4580    pub uuid: String,
4581    pub session_id: String,
4582}
4583
4584/// `system/ui_log` — one `$.ui.log(text)` line from a plugin in a headless
4585/// session: what the REPL appends to its transcript as a dim notice under
4586/// the plugin's name. Not sent to the model (CLI 2.1.285+).
4587#[derive(Debug, Clone, Serialize, Deserialize)]
4588pub struct UiLogMessage {
4589    /// The calling plugin's registry name, as the hooks engine knows it.
4590    pub plugin: String,
4591    /// The line the plugin gave: be ready for up to 10000 characters of it
4592    /// and then an ellipsis (4096 before CLI 2.1.289). Plain text: draw it
4593    /// as a text node, never as markup.
4594    pub text: String,
4595    pub uuid: String,
4596    pub session_id: String,
4597}
4598
4599/// `system/ui_toast` — one `$.ui.toast(text)` from a plugin in a headless
4600/// session: what the REPL shows on the notification bar under the prompt
4601/// for a few seconds, under the plugin's name. Toasts arrive at no fixed
4602/// rate: CLI 2.1.289 dropped the hooks host's 2 s spacing (CLI 2.1.285+).
4603#[derive(Debug, Clone, Serialize, Deserialize)]
4604pub struct UiToastMessage {
4605    /// The calling plugin's registry name, as the hooks engine knows it.
4606    pub plugin: String,
4607    /// The line the plugin gave: be ready for up to 10000 characters of it
4608    /// and then an ellipsis (4096 before CLI 2.1.289). Plain text: draw it
4609    /// as a text node, never as markup.
4610    pub text: String,
4611    /// How long the REPL would keep it on the notification bar: the plugin's
4612    /// `timeoutMs`, or the engine's default (4000).
4613    pub timeout_ms: u64,
4614    pub uuid: String,
4615    pub session_id: String,
4616}
4617
4618/// `system/ui_status` — one `$.ui.status(text)` from a plugin in a headless
4619/// session: the line the REPL pins under the prompt until the plugin's next
4620/// call replaces or clears it. One status line per plugin (CLI 2.1.285+).
4621#[derive(Debug, Clone, Serialize, Deserialize)]
4622pub struct UiStatusMessage {
4623    /// The calling plugin's registry name, as the hooks engine knows it.
4624    pub plugin: String,
4625    /// The plugin's pinned line, replacing its previous one; `None` (wire
4626    /// `null`) removes it. Be ready for up to 10000 characters of it and
4627    /// then an ellipsis (4096 before CLI 2.1.289). Plain text: draw it as a
4628    /// text node, never as markup.
4629    pub text: Option<String>,
4630    pub uuid: String,
4631    pub session_id: String,
4632}
4633
4634/// `system/ui_invalidate` — a plugin called `$.ui.invalidate("ui.render")`,
4635/// or a plugin loaded or reloaded: every tree a remote surface drew through
4636/// `ui_render` may be stale, and the surface re-asks each mounted instance.
4637/// Bumps from one plugin fold to at most one per 100 ms (CLI 2.1.285+).
4638#[derive(Debug, Clone, Serialize, Deserialize)]
4639pub struct UiInvalidateMessage {
4640    /// The render event whose version moved — `"ui.render"`; the engine
4641    /// versions it as one event, so no component or instance is named.
4642    pub event: String,
4643    /// Present when a plugin state write, or (CLI 2.1.289+) a `Client`
4644    /// drawn in them that failed and whose plugin heard it (`ui.fault`),
4645    /// made only these instances stale, so the surface may re-ask them
4646    /// alone. Absent, every mounted instance may be stale.
4647    #[serde(default, skip_serializing_if = "Option::is_none")]
4648    pub instances: Option<Vec<UiInvalidatedInstance>>,
4649    /// Present, and true, while a mod may change what is recorded of a typed
4650    /// line (a hook on `prompt.submit`, `command.run`, `session.append` or
4651    /// `session.receive`, mods not yet known, or an organization policy that
4652    /// lists a plugin or cannot be read): what the `prompts_screened_v1`
4653    /// capability says on `system/init`, sent as soon as a plugin load or
4654    /// reload makes it so. Absent says nothing; the next `system/init` does
4655    /// (CLI 2.1.300+).
4656    #[serde(default, skip_serializing_if = "Option::is_none")]
4657    pub prompts_screened: Option<bool>,
4658    pub uuid: String,
4659    pub session_id: String,
4660}
4661
4662/// One stale instance named by [`UiInvalidateMessage::instances`].
4663#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
4664pub struct UiInvalidatedInstance {
4665    /// The surface that drew the instance.
4666    pub surface: String,
4667    /// The component of the instance.
4668    pub component: String,
4669    /// The engine's requestId of the instance, as the `ui_render` that drew
4670    /// it named it.
4671    pub instance_id: String,
4672}
4673
4674/// `system/ui_panes` — the pane roster a remote surface draws changed: a
4675/// plugin opened, retitled or closed a pane, the person showed, focused or
4676/// closed one, or a placing client attached. Deduplicated: an unchanged
4677/// roster is not pushed. A pane opened before the stream armed produces
4678/// none, so read the roster with a `ui_panes` request on mount
4679/// (CLI 2.1.285+).
4680#[derive(Debug, Clone, Serialize, Deserialize)]
4681pub struct UiPanesMessage {
4682    /// The placed open panes in open order (an open appends, a close
4683    /// removes); a pane waiting unplaced is not listed.
4684    pub panes: Vec<UiPane>,
4685    /// The pane the surface shows: the last opened or focused, never
4686    /// displacing one the person holds; `None` with none open.
4687    pub shown_id: Option<String>,
4688    /// The pane the person gave the keyboard; `None` while the composer has
4689    /// it.
4690    pub focused_id: Option<String>,
4691    /// A standing `$.ui.open({ focus: true })` the surface has yet to judge:
4692    /// honour it only while the composer is empty and idle and no dialog is
4693    /// up, else refuse it.
4694    pub focus_requested_id: Option<String>,
4695    pub uuid: String,
4696    pub session_id: String,
4697}
4698
4699/// One placed pane in a [`UiPanesMessage`] roster.
4700#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
4701pub struct UiPane {
4702    /// The pane's id as the plugin opened it: 1 to 64 of letters, digits,
4703    /// `_`, `-`.
4704    pub id: String,
4705    /// The pane's tab label: the `title` its latest open gave, else its id.
4706    /// Plugin-authored text: draw it as a text node, never as markup.
4707    pub title: String,
4708    /// The registry name of the plugin that opened it.
4709    pub plugin: String,
4710    /// `true`: Escape while the pane holds the keyboard, or at an idle empty
4711    /// composer, closes it. Absent, Escape only returns the keyboard.
4712    #[serde(default, skip_serializing_if = "Option::is_none")]
4713    pub close_on_escape: Option<bool>,
4714    /// `true`: the surface holds its transient toasts while this pane is
4715    /// the one shown, and shows them once it closes.
4716    #[serde(default, skip_serializing_if = "Option::is_none")]
4717    pub hold_toasts: Option<bool>,
4718    /// The body rows its content asked for while seated inline; a request
4719    /// the person's own sizing overrides.
4720    #[serde(default, skip_serializing_if = "Option::is_none")]
4721    pub rows: Option<u64>,
4722    /// The body columns its content asked for while docked; a request the
4723    /// person's own sizing overrides.
4724    #[serde(default, skip_serializing_if = "Option::is_none")]
4725    pub columns: Option<u64>,
4726}
4727
4728/// `system/ui_scroll` — a plugin's `$.ui.scroll` moved the window of a site
4729/// a remote surface draws: the surface scrolls the site's body there. The
4730/// person's own scrolls are never pushed back (CLI 2.1.285+).
4731#[derive(Debug, Clone, Serialize, Deserialize)]
4732pub struct UiScrollMessage {
4733    /// The client drawing the site, as it attached.
4734    pub client_id: String,
4735    /// Which site: a pane's body or the band above the prompt.
4736    pub component: UiSiteComponent,
4737    /// The pane's id, or `above-prompt` for the band.
4738    pub instance_id: String,
4739    /// The first content row the window is to show, 0 at the top, clamped
4740    /// to the tree as the surface last reported it.
4741    pub offset: u64,
4742    /// `true`: keep the window on the tree's last rows through the draws to
4743    /// come, until the person or a plugin next moves it.
4744    #[serde(default, skip_serializing_if = "Option::is_none")]
4745    pub follow_end: Option<bool>,
4746    pub uuid: String,
4747    pub session_id: String,
4748}
4749
4750/// `system/ui_focus` — a plugin's `$.ui.focus` moved the focus ring of a
4751/// site a remote surface draws onto one of its Buttons, Inputs or Selects:
4752/// the surface focuses that element if it still draws it. The person's own
4753/// moves are never pushed back (CLI 2.1.285+).
4754#[derive(Debug, Clone, Serialize, Deserialize)]
4755pub struct UiFocusMessage {
4756    /// The client drawing the site, as it attached.
4757    pub client_id: String,
4758    /// Which site: a pane's body or the band above the prompt.
4759    pub component: UiSiteComponent,
4760    /// The pane's id, or `above-prompt` for the band.
4761    pub instance_id: String,
4762    /// Whose element takes the ring: its `press.plugin`.
4763    pub plugin: String,
4764    /// The element's `props.key`, as `ui.press` names it.
4765    pub key: String,
4766    pub uuid: String,
4767    pub session_id: String,
4768}
4769
4770/// The site a [`UiScrollMessage`] or [`UiFocusMessage`] addresses.
4771#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4772pub enum UiSiteComponent {
4773    /// A pane's body.
4774    Pane,
4775    /// The band above the prompt.
4776    AbovePrompt,
4777    /// A site not yet known to this version of the crate.
4778    Unknown(String),
4779}
4780
4781impl UiSiteComponent {
4782    pub fn as_str(&self) -> &str {
4783        match self {
4784            Self::Pane => "Pane",
4785            Self::AbovePrompt => "AbovePrompt",
4786            Self::Unknown(s) => s.as_str(),
4787        }
4788    }
4789}
4790
4791impl fmt::Display for UiSiteComponent {
4792    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4793        f.write_str(self.as_str())
4794    }
4795}
4796
4797impl From<&str> for UiSiteComponent {
4798    fn from(s: &str) -> Self {
4799        match s {
4800            "Pane" => Self::Pane,
4801            "AbovePrompt" => Self::AbovePrompt,
4802            other => Self::Unknown(other.to_string()),
4803        }
4804    }
4805}
4806
4807impl Serialize for UiSiteComponent {
4808    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
4809        serializer.serialize_str(self.as_str())
4810    }
4811}
4812
4813impl<'de> Deserialize<'de> for UiSiteComponent {
4814    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
4815        let s = String::deserialize(deserializer)?;
4816        Ok(Self::from(s.as_str()))
4817    }
4818}
4819
4820/// The run that printed a local-command row, carried as
4821/// [`AssistantMessage::local_command_run`].
4822#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
4823pub struct LocalCommandRun {
4824    /// The command's name as `command.run` carried it (no slash).
4825    pub command: String,
4826    /// Its arguments as the echo shows them (`***` when the command marks
4827    /// them sensitive).
4828    pub args: String,
4829    /// For a command a plugin registered, that plugin's name; for a row
4830    /// whose text plugins' `command.run` hooks answered or retold, the names
4831    /// the text is led by. Untrusted text; absent for the binary's own
4832    /// command in its own words (CLI 2.1.300+).
4833    #[serde(default, skip_serializing_if = "Option::is_none")]
4834    pub plugin: Option<String>,
4835}
4836
4837/// What a local-command row reports, carried as
4838/// [`AssistantMessage::local_command_outcome`] so a host can offer a fix
4839/// instead of relaying the text (CLI 2.1.281+).
4840#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
4841pub struct LocalCommandOutcome {
4842    pub kind: LocalCommandOutcomeKind,
4843    /// Kind `unknown` only: the registered command closest to the typed
4844    /// name, no slash.
4845    #[serde(default, skip_serializing_if = "Option::is_none")]
4846    pub suggestion: Option<String>,
4847}
4848
4849/// The outcome a local-command row reports ([`LocalCommandOutcome::kind`]).
4850/// New kinds are additive; the CLI itself drops the whole outcome when it
4851/// meets a kind it does not know, so treat [`Unknown`](Self::Unknown) as
4852/// absent.
4853#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4854pub enum LocalCommandOutcomeKind {
4855    /// An interactive-panel command this (headless) session cannot open.
4856    UnavailableHeadless,
4857    /// No command has the typed name; see
4858    /// [`LocalCommandOutcome::suggestion`].
4859    Unknown,
4860    /// The command ran and failed.
4861    Failed,
4862    /// The change applies after Claude Code restarts.
4863    RestartRequired,
4864    /// A kind not yet known to this version of the crate.
4865    Other(String),
4866}
4867
4868impl LocalCommandOutcomeKind {
4869    pub fn as_str(&self) -> &str {
4870        match self {
4871            Self::UnavailableHeadless => "unavailable_headless",
4872            Self::Unknown => "unknown",
4873            Self::Failed => "failed",
4874            Self::RestartRequired => "restart_required",
4875            Self::Other(s) => s.as_str(),
4876        }
4877    }
4878}
4879
4880impl fmt::Display for LocalCommandOutcomeKind {
4881    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4882        f.write_str(self.as_str())
4883    }
4884}
4885
4886impl From<&str> for LocalCommandOutcomeKind {
4887    fn from(s: &str) -> Self {
4888        match s {
4889            "unavailable_headless" => Self::UnavailableHeadless,
4890            "unknown" => Self::Unknown,
4891            "failed" => Self::Failed,
4892            "restart_required" => Self::RestartRequired,
4893            other => Self::Other(other.to_string()),
4894        }
4895    }
4896}
4897
4898impl Serialize for LocalCommandOutcomeKind {
4899    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
4900        serializer.serialize_str(self.as_str())
4901    }
4902}
4903
4904impl<'de> Deserialize<'de> for LocalCommandOutcomeKind {
4905    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
4906        let s = String::deserialize(deserializer)?;
4907        Ok(Self::from(s.as_str()))
4908    }
4909}
4910
4911/// Structured twin of a `/usage` result, carried as
4912/// [`AssistantMessage::usage_report`]: the session's totals, the plan's
4913/// usage rows as the server sent them and the extra-usage spend, and nothing
4914/// else from the usage body (the `get_usage` control reply carries the
4915/// rest). Experimental — the shape may change.
4916#[derive(Debug, Clone, Serialize, Deserialize)]
4917pub struct UsageReport {
4918    /// Cost and usage accumulated by the current session.
4919    pub session: super::control::UsageSession,
4920    /// The plan's usage rows and extra-usage spend from the claude.ai usage
4921    /// endpoint; `None` when the CLI could not fetch them (no plan on this
4922    /// lane, or a token without the profile scope).
4923    pub rate_limits: Option<UsageReportRateLimits>,
4924    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
4925    pub extra: serde_json::Map<String, Value>,
4926}
4927
4928/// The plan's usage rows and extra-usage spend in a [`UsageReport`].
4929#[derive(Debug, Clone, Serialize, Deserialize)]
4930pub struct UsageReportRateLimits {
4931    /// The server's usage rows (the usage endpoint's `limits[]`), as sent:
4932    /// which meters apply, their scope, labels, severity and order are the
4933    /// server's, so a client renders them verbatim. Empty when the server
4934    /// reported no meters; `None` when the body carried no rows at all (a
4935    /// server that predates them). When the usage fetch failed and the CLI
4936    /// fell back to rate-limit response headers, this holds at most the one
4937    /// row it synthesizes from them.
4938    pub limits: Option<Vec<UsageReportLimit>>,
4939    /// Extra-usage (overage) spend for the billing period, when the plan
4940    /// has it.
4941    #[serde(default, skip_serializing_if = "Option::is_none")]
4942    pub extra_usage: Option<UsageReportExtraUsage>,
4943    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
4944    pub extra: serde_json::Map<String, Value>,
4945}
4946
4947/// One server usage row in [`UsageReportRateLimits::limits`].
4948#[derive(Debug, Clone, Serialize, Deserialize)]
4949pub struct UsageReportLimit {
4950    /// The server's meter kind, e.g. `session`, `weekly_all` or
4951    /// `weekly_scoped`. Classify a row on this, never on a label.
4952    pub kind: String,
4953    /// The server's row group, e.g. `session` or `weekly`; rows render
4954    /// grouped under it, in the server's order.
4955    pub group: String,
4956    /// Share of the window used, 0–100.
4957    pub percent: f64,
4958    /// ISO 8601 timestamp when the window resets.
4959    pub resets_at: Option<String>,
4960    /// What a scoped row is for, a model or a surface, with the server's
4961    /// display label.
4962    #[serde(default, skip_serializing_if = "Option::is_none")]
4963    pub scope: Option<UsageReportScope>,
4964    /// The server's reading of the row for a meter's colour, e.g. `normal`,
4965    /// `warning` or `critical`; a client falls back to its own thresholds
4966    /// without it.
4967    #[serde(default, skip_serializing_if = "Option::is_none")]
4968    pub severity: Option<String>,
4969    /// The server's headline pick: the row a single-value indicator shows.
4970    #[serde(default, skip_serializing_if = "Option::is_none")]
4971    pub is_active: Option<bool>,
4972    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
4973    pub extra: serde_json::Map<String, Value>,
4974}
4975
4976/// What a scoped [`UsageReportLimit`] row is for.
4977#[derive(Debug, Clone, Serialize, Deserialize)]
4978pub struct UsageReportScope {
4979    #[serde(default, skip_serializing_if = "Option::is_none")]
4980    pub model: Option<UsageReportScopeLabel>,
4981    #[serde(default, skip_serializing_if = "Option::is_none")]
4982    pub surface: Option<UsageReportScopeLabel>,
4983    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
4984    pub extra: serde_json::Map<String, Value>,
4985}
4986
4987/// The server's display label for a [`UsageReportScope`] member.
4988#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
4989pub struct UsageReportScopeLabel {
4990    pub display_name: String,
4991}
4992
4993/// Extra-usage (overage) spend for the billing period in a
4994/// [`UsageReportRateLimits`]. Amounts are in minor units of `currency`
4995/// (cents for USD).
4996#[derive(Debug, Clone, Serialize, Deserialize)]
4997pub struct UsageReportExtraUsage {
4998    /// `false` while extra usage cannot cover sends.
4999    pub is_enabled: bool,
5000    pub monthly_limit: Option<f64>,
5001    pub used_credits: Option<f64>,
5002    pub utilization: Option<f64>,
5003    #[serde(default, skip_serializing_if = "Option::is_none")]
5004    pub currency: Option<String>,
5005    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
5006    pub extra: serde_json::Map<String, Value>,
5007}
5008
5009/// `{id, name}` of an original `Batch*` tool_use block, carried in
5010/// [`AssistantMessage::batch_tool_uses`].
5011#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5012pub struct BatchToolUse {
5013    pub id: String,
5014    pub name: String,
5015}
5016
5017/// Structured twin of the `/context` report, carried as
5018/// [`AssistantMessage::context_usage`] — the data a client needs to render
5019/// the context-usage card without parsing the markdown table. Evolves
5020/// additively; a breaking reshape would ship as a sibling field.
5021#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5022pub struct ContextUsage {
5023    /// Main-loop model the usage was computed for.
5024    pub model: String,
5025    /// Estimated tokens in use, unclamped — may exceed `raw_max_tokens` when
5026    /// over limit.
5027    pub total_tokens: u64,
5028    /// The window usage is measured against: the resolved autocompact window —
5029    /// the model's believed limit, or a smaller compaction-policy window.
5030    pub raw_max_tokens: u64,
5031    /// Rounded `total_tokens / raw_max_tokens`, 0–100+.
5032    pub percentage: u64,
5033    /// Present when `total_tokens` exceeds `raw_max_tokens`.
5034    #[serde(default, skip_serializing_if = "Option::is_none")]
5035    pub over_limit: Option<ContextOverLimit>,
5036    /// Usage-by-category rows (`Messages`, `System prompt`, …).
5037    #[serde(default)]
5038    pub categories: Vec<ContextCategory>,
5039    /// Per-tool token contributions of MCP tools.
5040    #[serde(default)]
5041    pub mcp_tools: Vec<ContextMcpTool>,
5042    /// Per-file token contributions of memory files.
5043    #[serde(default)]
5044    pub memory_files: Vec<ContextMemoryFile>,
5045    /// Per-agent token contributions of agent definitions.
5046    #[serde(default)]
5047    pub agents: Vec<ContextAgent>,
5048    /// Per-skill token contributions. Omitted when no skills contribute.
5049    #[serde(default, skip_serializing_if = "Option::is_none")]
5050    pub skills: Option<Vec<ContextSkill>>,
5051}
5052
5053/// Why and by how much a [`ContextUsage`] exceeds its window.
5054#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5055pub struct ContextOverLimit {
5056    pub tokens_over: u64,
5057    /// How the window was resolved: `"hard_limit"` (the model's believed
5058    /// limit) or `"compaction_window"` (a compaction-policy window).
5059    pub kind: String,
5060}
5061
5062/// One row of the `/context` usage-by-category breakdown.
5063#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5064pub struct ContextCategory {
5065    /// Display name of the row as the CLI renders it, e.g. `"Messages"`.
5066    /// Use `kind` (not this name) to classify the row.
5067    pub name: String,
5068    pub tokens: u64,
5069    /// What the row is: `"used"` content occupies the window; `"free"` is the
5070    /// remaining window; `"buffer"` is the compaction reserve; `"deferred"`
5071    /// rows are out-of-window tool schemas, excluded from usage math.
5072    pub kind: String,
5073}
5074
5075/// An MCP tool's token contribution, in [`ContextUsage::mcp_tools`].
5076#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5077pub struct ContextMcpTool {
5078    /// Wire name, e.g. `"mcp__linear__create_issue"`.
5079    pub name: String,
5080    pub server_name: String,
5081    pub tokens: u64,
5082}
5083
5084/// A memory file's token contribution, in [`ContextUsage::memory_files`].
5085#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5086pub struct ContextMemoryFile {
5087    pub path: String,
5088    /// Display label of the memory-file source, e.g. `"Project"` or `"User"`.
5089    #[serde(rename = "type")]
5090    pub file_type: String,
5091    pub tokens: u64,
5092}
5093
5094/// An agent definition's token contribution, in [`ContextUsage::agents`].
5095#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5096pub struct ContextAgent {
5097    pub agent_type: String,
5098    /// Raw source identifier, e.g. `"projectSettings"`, `"plugin"`.
5099    pub source: String,
5100    pub tokens: u64,
5101}
5102
5103/// A skill's token contribution, in [`ContextUsage::skills`].
5104#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5105pub struct ContextSkill {
5106    pub name: String,
5107    /// Raw source identifier, e.g. `"userSettings"`, `"plugin"`.
5108    pub source: String,
5109    #[serde(default, skip_serializing_if = "Option::is_none")]
5110    pub plugin_name: Option<String>,
5111    pub tokens: u64,
5112}
5113
5114/// Display metadata for a tool-use block carried on the assistant wrapper.
5115#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5116pub struct ToolUseMeta {
5117    pub id: String,
5118    pub display_name: String,
5119    #[serde(default, skip_serializing_if = "Option::is_none")]
5120    pub server_display_name: Option<String>,
5121    #[serde(default, skip_serializing_if = "Option::is_none")]
5122    pub icon_url: Option<String>,
5123    /// The `mcpsrv_` connector id of a server provisioned by claude.ai, set
5124    /// by the CLI from claude.ai's own connector listing, so a client may
5125    /// show the call as that connector's. Not a permission grant (CLI
5126    /// 2.1.300+).
5127    #[serde(default, skip_serializing_if = "Option::is_none")]
5128    pub server_id: Option<String>,
5129    /// The `X-MCP-Server-ID` header value, lower-cased, of an http entry in
5130    /// the dynamic scope (passed at launch or by a later `mcp_set_servers`
5131    /// push). The CLI checks only its shape, not whose it is: look it up in
5132    /// your own server list, treat a miss as an unknown server (never an
5133    /// error), and show a hit as the entry's claim, not proof. Separate from
5134    /// [`server_id`](Self::server_id); both are absent for plugin, repo,
5135    /// settings and agent entries (CLI 2.1.300+).
5136    #[serde(default, skip_serializing_if = "Option::is_none")]
5137    pub server_id_stamp: Option<String>,
5138    /// The tool's name as its server lists it, without the
5139    /// `mcp__<server>__` prefix (CLI 2.1.300+).
5140    #[serde(default, skip_serializing_if = "Option::is_none")]
5141    pub tool_name: Option<String>,
5142}
5143
5144/// Assistant message
5145#[derive(Debug, Clone, Serialize, Deserialize)]
5146pub struct AssistantMessage {
5147    pub message: AssistantMessageContent,
5148    #[serde(alias = "sessionId")]
5149    pub session_id: String,
5150    #[serde(skip_serializing_if = "Option::is_none")]
5151    pub uuid: Option<String>,
5152    #[serde(skip_serializing_if = "Option::is_none")]
5153    pub parent_tool_use_id: Option<String>,
5154    /// Anthropic API request id that produced this message (e.g. `req_...`).
5155    #[serde(skip_serializing_if = "Option::is_none")]
5156    pub request_id: Option<String>,
5157    /// Client uuid of the user message that triggered this turn, stamped on
5158    /// the turn's first top-level assistant message so a consumer can bind
5159    /// the reply to the send it answers without waiting for the result. With
5160    /// `--include-partial-messages` the first non-ping stream event is
5161    /// stamped too, independently (CLI 2.1.269+), so the same uuid may
5162    /// appear on both frames. Absent on every later assistant message of the
5163    /// turn, on subagent frames, on synthetic/scheduled (meta) turns, and
5164    /// from CLIs before 2.1.259.
5165    #[serde(default, skip_serializing_if = "Option::is_none")]
5166    pub user_message_uuid: Option<String>,
5167    /// Client uuids of every user message whose prompt this turn has consumed
5168    /// so far, in consumption order — all members of a prompt batch the host
5169    /// merged into this one turn. Always contains `user_message_uuid`; at
5170    /// most 64 entries. Present exactly when `user_message_uuid` is; absent
5171    /// from CLIs before 2.1.259 (fall back to `user_message_uuid`).
5172    #[serde(default, skip_serializing_if = "Vec::is_empty")]
5173    pub user_message_uuids: Vec<String>,
5174    /// ID of the subagent that produced this message: the `task_id` of its
5175    /// task events, unchanged when the subagent is resumed. Absent on
5176    /// main-thread messages (CLI 2.1.292+).
5177    #[serde(default, skip_serializing_if = "Option::is_none")]
5178    pub agent_id: Option<String>,
5179    /// Subagent type, when this assistant message was produced inside a
5180    /// `local_agent` subagent (e.g. `general-purpose`, `Explore`).
5181    #[serde(skip_serializing_if = "Option::is_none")]
5182    pub subagent_type: Option<String>,
5183    /// Short description of the subagent task, present alongside `subagent_type`.
5184    #[serde(skip_serializing_if = "Option::is_none")]
5185    pub task_description: Option<String>,
5186    #[serde(skip_serializing_if = "Option::is_none")]
5187    pub error: Option<AssistantErrorKind>,
5188    /// True when this message was truncated by an interrupt/abort before the
5189    /// stream completed — `stop_reason` was never received and the content
5190    /// may end mid-word. Absent on normally completed messages.
5191    #[serde(default, skip_serializing_if = "Option::is_none")]
5192    pub aborted: Option<bool>,
5193    /// True when this turn continued the preceding truncated assistant turn
5194    /// inside its trailing signed thinking block (max-output-tokens
5195    /// recovery). Histories replayed through the bridge must carry the flag
5196    /// back so the normalizer keeps the run's prefix on the wire.
5197    #[serde(default, skip_serializing_if = "Option::is_none")]
5198    pub resumed_from_incomplete_thinking: Option<bool>,
5199    #[serde(default, skip_serializing_if = "Vec::is_empty")]
5200    pub supersedes: Vec<String>,
5201    #[serde(skip_serializing_if = "Option::is_none")]
5202    pub timestamp: Option<String>,
5203    #[serde(default, skip_serializing_if = "Vec::is_empty")]
5204    pub tool_use_meta: Vec<ToolUseMeta>,
5205    /// `{id, name}` of the original `Batch*` tool_use block(s) for a message
5206    /// whose content was decomposed into synthetic v1 tool_use blocks.
5207    /// Round-tripped so a replayed history reassembles the batch block on the
5208    /// wire. Wrapper-level sibling — never inside `message.content`.
5209    #[serde(default, skip_serializing_if = "Vec::is_empty")]
5210    pub batch_tool_uses: Vec<BatchToolUse>,
5211    /// `tool_use.input` exactly as the API produced it, keyed by `tool_use`
5212    /// id, for a message whose `message.content` carries the
5213    /// client-normalized input. Round-tripped so a replayed history echoes
5214    /// each earlier tool call back to the API as the API emitted it.
5215    /// Wrapper-level sibling — never inside `message.content` (CLI 2.1.259+).
5216    #[serde(default, skip_serializing_if = "Option::is_none")]
5217    pub wire_tool_inputs: Option<serde_json::Map<String, Value>>,
5218    /// What the client-side input normalization read from process state for
5219    /// the inputs in [`wire_tool_inputs`](Self::wire_tool_inputs), keyed by
5220    /// the same `tool_use` ids. Round-tripped so a replayed history can verify
5221    /// each recorded input against the normalized one. Wrapper-level sibling —
5222    /// never inside `message.content` (CLI 2.1.266+).
5223    #[serde(default, skip_serializing_if = "Option::is_none")]
5224    pub wire_ingest_context: Option<serde_json::Map<String, Value>>,
5225    /// Replayed history rather than a live message, stamped by the Remote
5226    /// Control bridge when it flushes history to the session server, which
5227    /// also stamps it on deliveries it replays (CLI 2.1.266+).
5228    #[serde(default, skip_serializing_if = "Option::is_none")]
5229    pub historical: Option<bool>,
5230    /// Why this frame's turn is the automatic re-run of a turn a worker
5231    /// restart interrupted: the host's `CLAUDE_CODE_RESUME_REASON` when it set
5232    /// one (`host_draining`, `checkpoint_restore`, `container_recreated`,
5233    /// ...), else `interrupted_turn`. Stamped on the same reply frames as
5234    /// `user_message_uuid`, which on such a re-run names the interrupted
5235    /// turn's own last user prompt. Absent on every other turn and from CLIs
5236    /// before 2.1.268.
5237    #[serde(default, skip_serializing_if = "Option::is_none")]
5238    pub resume_reason: Option<String>,
5239    /// The originating `system/local_command` row's wire-form content,
5240    /// carried on the loop-synthesized local-command twin so a bridge/SDK
5241    /// history replay rebuilds the internal system row instead of dropping
5242    /// the output. Wrapper-level sibling — never inside `message.content`
5243    /// (CLI 2.1.259+).
5244    #[serde(default, skip_serializing_if = "Option::is_none")]
5245    pub local_command_source: Option<String>,
5246    /// Ascending zero-based indexes into `message.content` of the thinking
5247    /// blocks whose signature the server tagged as narration (server
5248    /// summaries of the prose between tool calls, not the model's own
5249    /// reasoning), so a renderer can label them as summaries without decoding
5250    /// the signature envelope. Fail-closed: an unparseable or legacy
5251    /// signature is not listed. Omitted when the frame has no such block and
5252    /// by CLIs before 2.1.260; treat unlisted thinking blocks as ordinary
5253    /// thinking. Wrapper-level sibling — never inside `message.content`.
5254    #[serde(default, skip_serializing_if = "Vec::is_empty")]
5255    pub narration_block_indexes: Vec<usize>,
5256    /// How a renderer should treat this frame's narration blocks:
5257    /// `"hidden"` or `"faint"` on CLI 2.1.280+ (kept open for future
5258    /// values). Omitted when the CLI expresses no preference.
5259    #[serde(default, skip_serializing_if = "Option::is_none")]
5260    pub narration_hint: Option<String>,
5261    /// How long the thinking block in this frame took to stream, in whole
5262    /// milliseconds (at least 1): from its `content_block_start` to its
5263    /// `content_block_stop`, so the wait for the response's first token is
5264    /// not counted. For display only. Sent only on a frame whose one content
5265    /// block is a thinking block that was streamed; absent from CLIs before
5266    /// 2.1.287 and from a CLI replaying history it rebuilt from SDK frames,
5267    /// so be ready to show the block without a duration. Wrapper-level
5268    /// sibling — never inside `message.content`.
5269    #[serde(default, skip_serializing_if = "Option::is_none")]
5270    pub thinking_duration_ms: Option<u64>,
5271    /// The `thinking.display` value Claude Code put in the API request body
5272    /// for the response that streamed this frame's thinking block; same
5273    /// source and meaning as
5274    /// [`StreamEventMessage::thinking_display`](crate::io::StreamEventMessage::thinking_display).
5275    /// Sent only on a frame whose one content block is a thinking block that
5276    /// was streamed, and only when the request body had a display. Absent
5277    /// from CLIs before 2.1.296 and from a CLI replaying history it rebuilt
5278    /// from SDK frames; an absent key states nothing. Marked `@internal`
5279    /// upstream, so treat the value set as open. Wrapper-level sibling —
5280    /// never inside `message.content`.
5281    #[serde(default, skip_serializing_if = "Option::is_none")]
5282    pub thinking_display: Option<String>,
5283    /// Structured twin of the `/context` report, carried on the synthetic
5284    /// assistant message that delivers the markdown table. Present only on
5285    /// `/context` results from CLIs new enough to attach it (2.1.239+).
5286    #[serde(default, skip_serializing_if = "Option::is_none")]
5287    pub context_usage: Option<ContextUsage>,
5288    /// Structured twin of the `/usage` report, carried on the synthetic
5289    /// assistant message that delivers its text: the session totals, the
5290    /// plan's usage rows and extra-usage spend, for remote clients that
5291    /// render a card from data. Present only on `/usage` results from CLIs
5292    /// new enough to attach it (2.1.273+) and from claude.ai-subscriber
5293    /// sessions; the text in `message.content` remains the canonical
5294    /// fallback. Wrapper-level sibling — never inside `message.content`.
5295    #[serde(default, skip_serializing_if = "Option::is_none")]
5296    pub usage_report: Option<Box<UsageReport>>,
5297    /// On the local-command twin, the run that printed the row: the
5298    /// command's name (no slash) and its arguments as the echo shows them
5299    /// (`***` when the command marks them sensitive). Present when a `local`
5300    /// command's dispatch printed the row or a `command.run` hook answered
5301    /// it, absent on a never-ran notice. Wrapper-level sibling — never inside
5302    /// `message.content` (CLI 2.1.273+).
5303    #[serde(default, skip_serializing_if = "Option::is_none")]
5304    pub local_command_run: Option<LocalCommandRun>,
5305    /// On the local-command twin, what the row reports (`unavailable_headless`,
5306    /// `unknown`, `failed`, `restart_required`), so a host can offer a fix
5307    /// instead of relaying the text. Absent when the row reports none of
5308    /// these; new kinds are additive, so treat an unknown one as absent.
5309    /// Wrapper-level sibling — never inside `message.content` (CLI 2.1.281+).
5310    #[serde(default, skip_serializing_if = "Option::is_none")]
5311    pub local_command_outcome: Option<LocalCommandOutcome>,
5312    #[serde(default, skip_serializing_if = "Option::is_none")]
5313    pub is_meta: Option<bool>,
5314    #[serde(default, skip_serializing_if = "Option::is_none")]
5315    pub is_virtual: Option<bool>,
5316    #[serde(default, skip_serializing_if = "Option::is_none")]
5317    pub is_api_error_message: Option<bool>,
5318    #[serde(skip_serializing_if = "Option::is_none")]
5319    pub api_error_status: Option<u16>,
5320    /// Typed kind of the API error when `is_api_error_message` is true. An
5321    /// open set the CLI grows release to release: 2.1.274 extends the
5322    /// original `max_output_tokens` / `dlp_request_denied` /
5323    /// `claude_code_version_too_old` with `effort_requires_thinking`,
5324    /// `advisor_incompatible`, `tool_history_mismatch`,
5325    /// `autocompact_thrashing`, `pdf_too_large`, `pdf_password_protected`,
5326    /// `media_removed` (CLI 2.1.288+: the API refused an image or a
5327    /// document, so Claude Code leaves it out of the requests that follow),
5328    /// `no_response`, `tls_untrusted_ca`, `gateway_content_type`,
5329    /// `provider_credentials`, `gateway_signin_required`,
5330    /// `gateway_session_expired`, `api_key_auth_disabled`,
5331    /// `org_disabled_credential`, `invalid_credential_header`,
5332    /// `model_requires_usage_credits`, `long_context_credits_required`,
5333    /// `consent_unanswered`, `no_allowed_fallback`,
5334    /// `model_substitution_disabled`, `field_not_granted` and (CLI 2.1.292+)
5335    /// `usage_limit_reached` — the request was refused by the account's own
5336    /// usage limit, a plan limit or a spend cap on usage credits;
5337    /// [`ApiErrorParams::rate_limit_info`] says which and when it resets.
5338    /// Kinds with parameters carry them in [`Self::api_error_params`].
5339    #[serde(skip_serializing_if = "Option::is_none")]
5340    pub api_error: Option<String>,
5341    /// The server's `error.details.error_code` for this API error, copied
5342    /// through when it is an identifier (`^[a-z][a-z0-9_]{0,63}$`) and
5343    /// dropped otherwise. Carries server gate codes this CLI build has no
5344    /// `api_error` value for, so a host can key on a new gate without a
5345    /// Claude Code release. Absent when the response carried no code and
5346    /// from CLIs before 2.1.274.
5347    #[serde(default, skip_serializing_if = "Option::is_none")]
5348    pub api_error_code: Option<String>,
5349    /// `first_party` when this API error was built from a response
5350    /// Anthropic's own API sent: the first-party provider, the default
5351    /// `https://api.anthropic.com` endpoint answering directly, over TLS
5352    /// verified with no added CA, override or socket tunnel. Absent in every
5353    /// other case (a base-URL override, a gateway, a third-party provider, a
5354    /// connection error, an error Claude Code raised itself, any replayed
5355    /// frame); treat absence as unknown origin. It says which endpoint
5356    /// answered, not that the text is the server's verbatim, so render
5357    /// stamped text as plain text. Open set — treat an unknown value as
5358    /// absent (CLI 2.1.300+).
5359    #[serde(default, skip_serializing_if = "Option::is_none")]
5360    pub api_error_origin: Option<String>,
5361    /// Parameters of [`Self::api_error`], present only for the kinds that
5362    /// have any (CLI 2.1.274+).
5363    #[serde(default, skip_serializing_if = "Option::is_none")]
5364    pub api_error_params: Option<ApiErrorParams>,
5365    #[serde(skip_serializing_if = "Option::is_none")]
5366    pub error_details: Option<String>,
5367    #[serde(skip_serializing_if = "Option::is_none")]
5368    pub advisor_model: Option<String>,
5369    #[serde(skip_serializing_if = "Option::is_none")]
5370    pub attribution_agent: Option<String>,
5371    #[serde(skip_serializing_if = "Option::is_none")]
5372    pub attribution_skill: Option<String>,
5373    #[serde(skip_serializing_if = "Option::is_none")]
5374    pub attribution_plugin: Option<String>,
5375    #[serde(skip_serializing_if = "Option::is_none")]
5376    pub attribution_mcp_server: Option<String>,
5377    #[serde(skip_serializing_if = "Option::is_none")]
5378    pub attribution_mcp_tool: Option<String>,
5379}
5380
5381/// Parameters of an assistant frame's `api_error`, carried as
5382/// [`AssistantMessage::api_error_params`] only for the error kinds that have
5383/// any (CLI 2.1.274+).
5384#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
5385pub struct ApiErrorParams {
5386    /// The effort level the API refused (`effort_requires_thinking`).
5387    #[serde(default, skip_serializing_if = "Option::is_none")]
5388    pub effort: Option<String>,
5389    /// The API provider whose credentials failed (`provider_credentials`,
5390    /// `gateway_session_expired`).
5391    #[serde(default, skip_serializing_if = "Option::is_none")]
5392    pub provider: Option<ApiErrorProvider>,
5393    /// How to repair the failed credentials (`provider_credentials`,
5394    /// `gateway_session_expired`).
5395    #[serde(default, skip_serializing_if = "Option::is_none")]
5396    pub remedy: Option<ApiErrorRemedy>,
5397    /// The kind of block the API refused (`media_removed`); absent for
5398    /// [`ApiErrorMediaReason::MediaBudget`] (CLI 2.1.288+).
5399    #[serde(default, skip_serializing_if = "Option::is_none")]
5400    pub media: Option<ApiErrorMedia>,
5401    /// Why the API refused the block (`media_removed`, CLI 2.1.288+).
5402    #[serde(default, skip_serializing_if = "Option::is_none")]
5403    pub media_reason: Option<ApiErrorMediaReason>,
5404    /// What the refused request's own response said about the limit
5405    /// (`usage_limit_reached`), in the shape `rate_limit_event` carries:
5406    /// `status` is rejected and `isUsingOverage` false, `rateLimitType` names
5407    /// the limit, `resetsAt` is when it resets, and the overage fields and
5408    /// `limitScope` say whose spend cap it is. It is that refusal's record
5409    /// and is never updated, so compare `resetsAt` with the clock before
5410    /// calling the limit current (CLI 2.1.292+).
5411    #[serde(default, skip_serializing_if = "Option::is_none")]
5412    pub rate_limit_info: Option<RateLimitInfo>,
5413}
5414
5415/// The kind of block the API refused ([`ApiErrorParams::media`]).
5416#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5417pub enum ApiErrorMedia {
5418    Image,
5419    Document,
5420    /// A kind not yet known to this version of the crate.
5421    Unknown(String),
5422}
5423
5424impl ApiErrorMedia {
5425    pub fn as_str(&self) -> &str {
5426        match self {
5427            Self::Image => "image",
5428            Self::Document => "document",
5429            Self::Unknown(s) => s.as_str(),
5430        }
5431    }
5432}
5433
5434impl fmt::Display for ApiErrorMedia {
5435    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5436        f.write_str(self.as_str())
5437    }
5438}
5439
5440impl From<&str> for ApiErrorMedia {
5441    fn from(s: &str) -> Self {
5442        match s {
5443            "image" => Self::Image,
5444            "document" => Self::Document,
5445            other => Self::Unknown(other.to_string()),
5446        }
5447    }
5448}
5449
5450impl Serialize for ApiErrorMedia {
5451    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
5452        serializer.serialize_str(self.as_str())
5453    }
5454}
5455
5456impl<'de> Deserialize<'de> for ApiErrorMedia {
5457    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
5458        let s = String::deserialize(deserializer)?;
5459        Ok(Self::from(s.as_str()))
5460    }
5461}
5462
5463/// Why the API refused an image or document
5464/// ([`ApiErrorParams::media_reason`]).
5465#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5466pub enum ApiErrorMediaReason {
5467    /// Every other refusal, a gateway's report of a model's refusal among
5468    /// them.
5469    Unprocessable,
5470    /// The API said that the model does not accept PDF documents.
5471    UnsupportedByModel,
5472    /// The request's images and documents together exceed the API's limit,
5473    /// and the requests that follow may leave out blocks of either kind.
5474    MediaBudget,
5475    /// A reason not yet known to this version of the crate.
5476    Unknown(String),
5477}
5478
5479impl ApiErrorMediaReason {
5480    pub fn as_str(&self) -> &str {
5481        match self {
5482            Self::Unprocessable => "unprocessable",
5483            Self::UnsupportedByModel => "unsupported_by_model",
5484            Self::MediaBudget => "media_budget",
5485            Self::Unknown(s) => s.as_str(),
5486        }
5487    }
5488}
5489
5490impl fmt::Display for ApiErrorMediaReason {
5491    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5492        f.write_str(self.as_str())
5493    }
5494}
5495
5496impl From<&str> for ApiErrorMediaReason {
5497    fn from(s: &str) -> Self {
5498        match s {
5499            "unprocessable" => Self::Unprocessable,
5500            "unsupported_by_model" => Self::UnsupportedByModel,
5501            "media_budget" => Self::MediaBudget,
5502            other => Self::Unknown(other.to_string()),
5503        }
5504    }
5505}
5506
5507impl Serialize for ApiErrorMediaReason {
5508    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
5509        serializer.serialize_str(self.as_str())
5510    }
5511}
5512
5513impl<'de> Deserialize<'de> for ApiErrorMediaReason {
5514    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
5515        let s = String::deserialize(deserializer)?;
5516        Ok(Self::from(s.as_str()))
5517    }
5518}
5519
5520/// The API provider whose credentials failed ([`ApiErrorParams::provider`]).
5521#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5522pub enum ApiErrorProvider {
5523    Bedrock,
5524    AnthropicAws,
5525    Mantle,
5526    AnthropicGoogleCloud,
5527    Vertex,
5528    Foundry,
5529    Gateway,
5530    /// A provider not yet known to this version of the crate.
5531    Unknown(String),
5532}
5533
5534impl ApiErrorProvider {
5535    pub fn as_str(&self) -> &str {
5536        match self {
5537            Self::Bedrock => "bedrock",
5538            Self::AnthropicAws => "anthropicAws",
5539            Self::Mantle => "mantle",
5540            Self::AnthropicGoogleCloud => "anthropicGoogleCloud",
5541            Self::Vertex => "vertex",
5542            Self::Foundry => "foundry",
5543            Self::Gateway => "gateway",
5544            Self::Unknown(s) => s.as_str(),
5545        }
5546    }
5547}
5548
5549impl fmt::Display for ApiErrorProvider {
5550    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5551        f.write_str(self.as_str())
5552    }
5553}
5554
5555impl From<&str> for ApiErrorProvider {
5556    fn from(s: &str) -> Self {
5557        match s {
5558            "bedrock" => Self::Bedrock,
5559            "anthropicAws" => Self::AnthropicAws,
5560            "mantle" => Self::Mantle,
5561            "anthropicGoogleCloud" => Self::AnthropicGoogleCloud,
5562            "vertex" => Self::Vertex,
5563            "foundry" => Self::Foundry,
5564            "gateway" => Self::Gateway,
5565            other => Self::Unknown(other.to_string()),
5566        }
5567    }
5568}
5569
5570impl Serialize for ApiErrorProvider {
5571    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
5572        serializer.serialize_str(self.as_str())
5573    }
5574}
5575
5576impl<'de> Deserialize<'de> for ApiErrorProvider {
5577    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
5578        let s = String::deserialize(deserializer)?;
5579        Ok(Self::from(s.as_str()))
5580    }
5581}
5582
5583/// How to repair failed provider credentials ([`ApiErrorParams::remedy`]).
5584#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5585pub enum ApiErrorRemedy {
5586    /// A configured refresh command (`awsAuthRefresh` / `gcpAuthRefresh`)
5587    /// refreshes the credentials.
5588    RefreshCommand,
5589    /// Refresh the provider's credentials by hand; no command is configured.
5590    RefreshCredentials,
5591    /// Refresh the Google application default credentials or the
5592    /// `GOOGLE_APPLICATION_CREDENTIALS` key file.
5593    Adc,
5594    /// Replace the gateway token supplied through `ANTHROPIC_AUTH_TOKEN` or
5595    /// `ANTHROPIC_CUSTOM_HEADERS`.
5596    GatewayToken,
5597    /// The host application owns these credentials.
5598    HostManaged,
5599    /// The credentials work but the model is not enabled for this account
5600    /// and region (Amazon Bedrock).
5601    ModelAccess,
5602    /// A remedy not yet known to this version of the crate.
5603    Unknown(String),
5604}
5605
5606impl ApiErrorRemedy {
5607    pub fn as_str(&self) -> &str {
5608        match self {
5609            Self::RefreshCommand => "refresh_command",
5610            Self::RefreshCredentials => "refresh_credentials",
5611            Self::Adc => "adc",
5612            Self::GatewayToken => "gateway_token",
5613            Self::HostManaged => "host_managed",
5614            Self::ModelAccess => "model_access",
5615            Self::Unknown(s) => s.as_str(),
5616        }
5617    }
5618}
5619
5620impl fmt::Display for ApiErrorRemedy {
5621    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5622        f.write_str(self.as_str())
5623    }
5624}
5625
5626impl From<&str> for ApiErrorRemedy {
5627    fn from(s: &str) -> Self {
5628        match s {
5629            "refresh_command" => Self::RefreshCommand,
5630            "refresh_credentials" => Self::RefreshCredentials,
5631            "adc" => Self::Adc,
5632            "gateway_token" => Self::GatewayToken,
5633            "host_managed" => Self::HostManaged,
5634            "model_access" => Self::ModelAccess,
5635            other => Self::Unknown(other.to_string()),
5636        }
5637    }
5638}
5639
5640impl Serialize for ApiErrorRemedy {
5641    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
5642        serializer.serialize_str(self.as_str())
5643    }
5644}
5645
5646impl<'de> Deserialize<'de> for ApiErrorRemedy {
5647    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
5648        let s = String::deserialize(deserializer)?;
5649        Ok(Self::from(s.as_str()))
5650    }
5651}
5652
5653/// Nested message content for assistant messages
5654#[derive(Debug, Clone, Serialize, Deserialize)]
5655pub struct AssistantMessageContent {
5656    pub id: String,
5657    /// The Anthropic API message type — always `"message"`.
5658    #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
5659    pub message_type: Option<String>,
5660    pub role: MessageRole,
5661    pub model: String,
5662    pub content: Vec<ContentBlock>,
5663    #[serde(skip_serializing_if = "Option::is_none")]
5664    pub stop_reason: Option<StopReason>,
5665    #[serde(skip_serializing_if = "Option::is_none")]
5666    pub stop_sequence: Option<String>,
5667    #[serde(skip_serializing_if = "Option::is_none")]
5668    pub usage: Option<AssistantUsage>,
5669    /// Details about why generation stopped
5670    #[serde(skip_serializing_if = "Option::is_none")]
5671    pub stop_details: Option<Value>,
5672    /// Context management metadata
5673    #[serde(skip_serializing_if = "Option::is_none")]
5674    pub context_management: Option<Value>,
5675}
5676
5677/// Usage information for assistant messages
5678#[derive(Debug, Clone, Serialize, Deserialize)]
5679pub struct AssistantUsage {
5680    /// Number of input tokens
5681    #[serde(default)]
5682    pub input_tokens: u32,
5683
5684    /// Number of output tokens
5685    #[serde(default)]
5686    pub output_tokens: u32,
5687
5688    /// Tokens used to create cache
5689    #[serde(default)]
5690    pub cache_creation_input_tokens: u32,
5691
5692    /// Tokens read from cache
5693    #[serde(default)]
5694    pub cache_read_input_tokens: u32,
5695
5696    /// Service tier used (e.g., "standard")
5697    #[serde(skip_serializing_if = "Option::is_none")]
5698    pub service_tier: Option<String>,
5699
5700    /// Detailed cache creation breakdown
5701    #[serde(skip_serializing_if = "Option::is_none")]
5702    pub cache_creation: Option<CacheCreationDetails>,
5703
5704    /// Inference geography (e.g., "not_available")
5705    #[serde(skip_serializing_if = "Option::is_none")]
5706    pub inference_geo: Option<String>,
5707
5708    /// Outcome of a fallback-credit token the request presented; `null` on
5709    /// the wire when it presented none (CLI 2.1.285+).
5710    #[serde(default, skip_serializing_if = "Option::is_none")]
5711    pub fallback_credit: Option<Value>,
5712}
5713
5714/// Detailed cache creation information
5715#[derive(Debug, Clone, Serialize, Deserialize)]
5716pub struct CacheCreationDetails {
5717    /// Ephemeral 1-hour input tokens
5718    #[serde(default)]
5719    pub ephemeral_1h_input_tokens: u32,
5720
5721    /// Ephemeral 5-minute input tokens
5722    #[serde(default)]
5723    pub ephemeral_5m_input_tokens: u32,
5724}
5725
5726#[cfg(test)]
5727mod tests {
5728    use crate::io::ClaudeOutput;
5729
5730    #[test]
5731    fn test_subagent_usage_rollup_accumulates_task_results() {
5732        use super::SubagentUsageRollup;
5733
5734        let mut rollup = SubagentUsageRollup::default();
5735
5736        let task_result = r#"{"type":"user","message":{"role":"user","content":[]},"session_id":"7fbc568e-2bd6-45aa-b217-a1cf80004ba1","tool_use_result":{"status":"completed","prompt":"Compute 6 times 7.","agentId":"ab52f22445470d454","agentType":"general-purpose","resolvedModel":"claude-sonnet-4-6","totalDurationMs":1853,"totalTokens":10201,"totalToolUseCount":3}}"#;
5737        let output: ClaudeOutput = serde_json::from_str(task_result).unwrap();
5738        assert!(rollup.observe(&output));
5739        assert_eq!(rollup.subagent_tokens, 10201);
5740        assert_eq!(rollup.agent_count, 1);
5741        assert_eq!(rollup.tool_uses, 3);
5742        assert_eq!(rollup.duration_ms, 1853);
5743
5744        // Replayed frame with the same agentId is counted once.
5745        assert!(!rollup.observe(&output));
5746        assert_eq!(rollup.agent_count, 1);
5747        assert_eq!(rollup.subagent_tokens, 10201);
5748
5749        // A second agent accumulates.
5750        let second = r#"{"type":"user","message":{"role":"user","content":[]},"session_id":"7fbc568e-2bd6-45aa-b217-a1cf80004ba1","tool_use_result":{"status":"completed","agentId":"ffff00001111","totalDurationMs":100,"totalTokens":500,"totalToolUseCount":1}}"#;
5751        let output: ClaudeOutput = serde_json::from_str(second).unwrap();
5752        assert!(rollup.observe(&output));
5753        assert_eq!(rollup.agent_count, 2);
5754        assert_eq!(rollup.subagent_tokens, 10701);
5755    }
5756
5757    #[test]
5758    fn test_subagent_usage_rollup_ignores_non_task_results() {
5759        use super::SubagentUsageRollup;
5760
5761        let mut rollup = SubagentUsageRollup::default();
5762
5763        // A ToolSearch tool_use_result parses as an all-None SubagentResult;
5764        // it must not count as a subagent.
5765        let tool_search = r#"{"type":"user","message":{"role":"user","content":[]},"session_id":"7fbc568e-2bd6-45aa-b217-a1cf80004ba1","tool_use_result":{"matches":["TaskCreate"],"query":"select:TaskCreate","total_deferred_tools":27}}"#;
5766        let output: ClaudeOutput = serde_json::from_str(tool_search).unwrap();
5767        assert!(!rollup.observe(&output));
5768
5769        // Plain user message without tool_use_result.
5770        let plain = r#"{"type":"user","message":{"role":"user","content":[]},"session_id":"7fbc568e-2bd6-45aa-b217-a1cf80004ba1"}"#;
5771        let output: ClaudeOutput = serde_json::from_str(plain).unwrap();
5772        assert!(!rollup.observe(&output));
5773
5774        // Non-user frames are ignored.
5775        let system = r#"{"type":"system","subtype":"status","status":null,"session_id":"7fbc568e-2bd6-45aa-b217-a1cf80004ba1"}"#;
5776        let output: ClaudeOutput = serde_json::from_str(system).unwrap();
5777        assert!(!rollup.observe(&output));
5778
5779        assert_eq!(rollup, SubagentUsageRollup::default());
5780    }
5781
5782    #[test]
5783    fn test_subagent_usage_rollup_over_captured_session() {
5784        use super::SubagentUsageRollup;
5785
5786        let mut rollup = SubagentUsageRollup::default();
5787        let fixture =
5788            include_str!("../../test_cases/subagent_sessions/general_purpose_compute.jsonl");
5789        for line in fixture.lines().filter(|l| !l.trim().is_empty()) {
5790            if let Ok(output) = serde_json::from_str::<ClaudeOutput>(line) {
5791                rollup.observe(&output);
5792            }
5793        }
5794        assert_eq!(rollup.agent_count, 1);
5795        assert_eq!(rollup.subagent_tokens, 10201);
5796    }
5797
5798    #[test]
5799    fn test_system_message_init() {
5800        let json = r#"{
5801            "type": "system",
5802            "subtype": "init",
5803            "session_id": "test-session-123",
5804            "cwd": "/home/user/project",
5805            "model": "claude-sonnet-4",
5806            "tools": ["Bash", "Read", "Write"],
5807            "mcp_servers": [],
5808            "slash_commands": ["compact", "cost", "review"],
5809            "agents": ["Bash", "Explore", "Plan"],
5810            "plugins": [{"name": "rust-analyzer-lsp", "path": "/home/user/.claude/plugins/rust-analyzer-lsp/1.0.0"}],
5811            "skills": [],
5812            "claude_code_version": "2.1.15",
5813            "apiKeySource": "none",
5814            "output_style": "default",
5815            "permissionMode": "default"
5816        }"#;
5817
5818        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
5819        if let ClaudeOutput::System(sys) = output {
5820            assert!(sys.is_init());
5821            assert!(!sys.is_status());
5822            assert!(!sys.is_compact_boundary());
5823
5824            let init = sys.as_init().expect("Should parse as init");
5825            assert_eq!(init.session_id, "test-session-123");
5826            assert_eq!(init.cwd, Some("/home/user/project".to_string()));
5827            assert_eq!(init.model, Some("claude-sonnet-4".to_string()));
5828            assert_eq!(init.tools, vec!["Bash", "Read", "Write"]);
5829            assert_eq!(init.slash_commands, vec!["compact", "cost", "review"]);
5830            assert_eq!(init.agents, vec!["Bash", "Explore", "Plan"]);
5831            assert_eq!(init.plugins.len(), 1);
5832            assert_eq!(init.plugins[0].name, "rust-analyzer-lsp");
5833            assert_eq!(init.claude_code_version, Some("2.1.15".to_string()));
5834            assert_eq!(init.api_key_source, Some(super::ApiKeySource::None));
5835            assert_eq!(init.output_style, Some(super::OutputStyle::Default));
5836            assert_eq!(
5837                init.permission_mode,
5838                Some(super::InitPermissionMode::Default)
5839            );
5840        } else {
5841            panic!("Expected System message");
5842        }
5843    }
5844
5845    #[test]
5846    fn test_system_message_init_from_real_capture() {
5847        let json = include_str!("../../test_cases/tool_use_captures/tool_msg_0.json");
5848        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
5849        if let ClaudeOutput::System(sys) = output {
5850            let init = sys.as_init().expect("Should parse real init capture");
5851            assert_eq!(init.slash_commands.len(), 8);
5852            assert!(init.slash_commands.contains(&"compact".to_string()));
5853            assert!(init.slash_commands.contains(&"review".to_string()));
5854            assert_eq!(init.agents.len(), 5);
5855            assert!(init.agents.contains(&"Bash".to_string()));
5856            assert!(init.agents.contains(&"Explore".to_string()));
5857            assert_eq!(init.plugins.len(), 1);
5858            assert_eq!(init.plugins[0].name, "rust-analyzer-lsp");
5859            assert_eq!(init.claude_code_version, Some("2.1.15".to_string()));
5860        } else {
5861            panic!("Expected System message");
5862        }
5863    }
5864
5865    #[test]
5866    fn test_system_message_status() {
5867        let json = r#"{
5868            "type": "system",
5869            "subtype": "status",
5870            "session_id": "879c1a88-3756-4092-aa95-0020c4ed9692",
5871            "status": "compacting",
5872            "uuid": "32eb9f9d-5ef7-47ff-8fce-bbe22fe7ed93"
5873        }"#;
5874
5875        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
5876        if let ClaudeOutput::System(sys) = output {
5877            assert!(sys.is_status());
5878            assert!(!sys.is_init());
5879
5880            let status = sys.as_status().expect("Should parse as status");
5881            assert_eq!(status.session_id, "879c1a88-3756-4092-aa95-0020c4ed9692");
5882            assert_eq!(status.status, Some(super::StatusMessageStatus::Compacting));
5883            assert_eq!(
5884                status.uuid,
5885                Some("32eb9f9d-5ef7-47ff-8fce-bbe22fe7ed93".to_string())
5886            );
5887        } else {
5888            panic!("Expected System message");
5889        }
5890    }
5891
5892    #[test]
5893    fn test_system_message_status_null() {
5894        let json = r#"{
5895            "type": "system",
5896            "subtype": "status",
5897            "session_id": "879c1a88-3756-4092-aa95-0020c4ed9692",
5898            "status": null,
5899            "uuid": "92d9637e-d00e-418e-acd2-a504e3861c6a"
5900        }"#;
5901
5902        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
5903        if let ClaudeOutput::System(sys) = output {
5904            let status = sys.as_status().expect("Should parse as status");
5905            assert_eq!(status.status, None);
5906        } else {
5907            panic!("Expected System message");
5908        }
5909    }
5910
5911    #[test]
5912    fn test_system_message_task_started() {
5913        let json = r#"{
5914            "type": "system",
5915            "subtype": "task_started",
5916            "session_id": "9abbc466-dad0-4b8e-b6b0-cad5eb7a16b9",
5917            "task_id": "b6daf3f",
5918            "task_type": "local_bash",
5919            "tool_use_id": "toolu_011rfSTFumpJZdCCfzeD7jaS",
5920            "description": "Wait for CI on PR #12",
5921            "uuid": "c4243261-c128-4747-b8c3-5e1c7c10eeb8"
5922        }"#;
5923
5924        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
5925        if let ClaudeOutput::System(sys) = output {
5926            assert!(sys.is_task_started());
5927            assert!(!sys.is_task_progress());
5928            assert!(!sys.is_task_notification());
5929
5930            let task = sys.as_task_started().expect("Should parse as task_started");
5931            assert_eq!(task.session_id, "9abbc466-dad0-4b8e-b6b0-cad5eb7a16b9");
5932            assert_eq!(task.task_id, "b6daf3f");
5933            assert_eq!(task.task_type, Some(super::TaskType::LocalBash));
5934            assert_eq!(
5935                task.tool_use_id.as_deref(),
5936                Some("toolu_011rfSTFumpJZdCCfzeD7jaS")
5937            );
5938            assert_eq!(task.description, "Wait for CI on PR #12");
5939        } else {
5940            panic!("Expected System message");
5941        }
5942    }
5943
5944    #[test]
5945    fn test_system_message_task_started_agent() {
5946        let json = r#"{
5947            "type": "system",
5948            "subtype": "task_started",
5949            "session_id": "bff4f716-17c1-4255-ab7b-eea9d33824e3",
5950            "task_id": "a4a7e0906e5fc64cc",
5951            "task_type": "local_agent",
5952            "tool_use_id": "toolu_01SFz9FwZ1cYgCSy8vRM7wep",
5953            "description": "Explore Scene/ArrayScene duplication",
5954            "uuid": "85a39f5a-e4d4-47f7-9a6d-1125f1a8035f"
5955        }"#;
5956
5957        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
5958        if let ClaudeOutput::System(sys) = output {
5959            let task = sys.as_task_started().expect("Should parse as task_started");
5960            assert_eq!(task.task_type, Some(super::TaskType::LocalAgent));
5961            assert_eq!(task.task_id, "a4a7e0906e5fc64cc");
5962        } else {
5963            panic!("Expected System message");
5964        }
5965    }
5966
5967    #[test]
5968    fn test_system_message_task_progress() {
5969        let json = r#"{
5970            "type": "system",
5971            "subtype": "task_progress",
5972            "session_id": "bff4f716-17c1-4255-ab7b-eea9d33824e3",
5973            "task_id": "a4a7e0906e5fc64cc",
5974            "tool_use_id": "toolu_01SFz9FwZ1cYgCSy8vRM7wep",
5975            "description": "Reading src/jplephem/chebyshev.rs",
5976            "last_tool_name": "Read",
5977            "usage": {
5978                "duration_ms": 13996,
5979                "tool_uses": 9,
5980                "total_tokens": 38779
5981            },
5982            "uuid": "85a39f5a-e4d4-47f7-9a6d-1125f1a8035f"
5983        }"#;
5984
5985        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
5986        if let ClaudeOutput::System(sys) = output {
5987            assert!(sys.is_task_progress());
5988            assert!(!sys.is_task_started());
5989
5990            let progress = sys
5991                .as_task_progress()
5992                .expect("Should parse as task_progress");
5993            assert_eq!(progress.task_id, "a4a7e0906e5fc64cc");
5994            assert_eq!(progress.description, "Reading src/jplephem/chebyshev.rs");
5995            assert_eq!(progress.last_tool_name.as_deref(), Some("Read"));
5996            assert_eq!(progress.usage.duration_ms, 13996);
5997            assert_eq!(progress.usage.tool_uses, 9);
5998            assert_eq!(progress.usage.total_tokens, 38779);
5999        } else {
6000            panic!("Expected System message");
6001        }
6002    }
6003
6004    #[test]
6005    fn test_system_message_task_notification_completed() {
6006        let json = r#"{
6007            "type": "system",
6008            "subtype": "task_notification",
6009            "session_id": "bff4f716-17c1-4255-ab7b-eea9d33824e3",
6010            "task_id": "a0ba761e9dc9c316f",
6011            "tool_use_id": "toolu_01Ho6XVXFLVNjTQ9YqowdBXW",
6012            "status": "completed",
6013            "summary": "Agent \"Write Hipparcos data source doc\" completed",
6014            "output_file": "",
6015            "usage": {
6016                "duration_ms": 172300,
6017                "tool_uses": 11,
6018                "total_tokens": 42005
6019            },
6020            "uuid": "269f49b9-218d-4c8d-9f7e-3a5383a0c5b2"
6021        }"#;
6022
6023        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6024        if let ClaudeOutput::System(sys) = output {
6025            assert!(sys.is_task_notification());
6026
6027            let notif = sys
6028                .as_task_notification()
6029                .expect("Should parse as task_notification");
6030            assert_eq!(notif.status, super::TaskStatus::Completed);
6031            assert_eq!(
6032                notif.summary,
6033                "Agent \"Write Hipparcos data source doc\" completed"
6034            );
6035            assert_eq!(notif.output_file, Some("".to_string()));
6036            assert_eq!(
6037                notif.tool_use_id,
6038                Some("toolu_01Ho6XVXFLVNjTQ9YqowdBXW".to_string())
6039            );
6040            let usage = notif.usage.expect("Should have usage");
6041            assert_eq!(usage.duration_ms, 172300);
6042            assert_eq!(usage.tool_uses, 11);
6043            assert_eq!(usage.total_tokens, 42005);
6044        } else {
6045            panic!("Expected System message");
6046        }
6047    }
6048
6049    #[test]
6050    fn test_system_message_task_notification_failed_no_usage() {
6051        let json = r#"{
6052            "type": "system",
6053            "subtype": "task_notification",
6054            "session_id": "ea629737-3c36-48a8-a1c4-ad761ad35784",
6055            "task_id": "b98f6a3",
6056            "status": "failed",
6057            "summary": "Background command \"Run FSM calibration\" failed with exit code 1",
6058            "output_file": "/tmp/claude-1000/tasks/b98f6a3.output"
6059        }"#;
6060
6061        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6062        if let ClaudeOutput::System(sys) = output {
6063            let notif = sys
6064                .as_task_notification()
6065                .expect("Should parse as task_notification");
6066            assert_eq!(notif.status, super::TaskStatus::Failed);
6067            assert!(notif.tool_use_id.is_none());
6068            assert!(notif.usage.is_none());
6069            assert_eq!(
6070                notif.output_file,
6071                Some("/tmp/claude-1000/tasks/b98f6a3.output".to_string())
6072            );
6073        } else {
6074            panic!("Expected System message");
6075        }
6076    }
6077
6078    /// Task system messages survive a `to_value` → `from_value` round-trip
6079    /// with their typed accessors still resolving. Mirrors the proxy/relay
6080    /// path where output is reparsed from a `serde_json::Value` rather than
6081    /// straight from the CLI's stdout, so a silently dropped or renamed field
6082    /// surfaces here instead of as a `None` downstream.
6083    #[test]
6084    fn test_task_messages_roundtrip_through_value() {
6085        let cases = [
6086            r#"{"type":"system","subtype":"task_started","session_id":"s1",
6087                "task_id":"t1","task_type":"local_bash","tool_use_id":"tu1",
6088                "description":"Sleep 3s","uuid":"u1"}"#,
6089            r#"{"type":"system","subtype":"task_progress","session_id":"s1",
6090                "task_id":"t1","tool_use_id":"tu1","description":"Running ls",
6091                "last_tool_name":"Bash",
6092                "usage":{"duration_ms":100,"tool_uses":1,"total_tokens":500},
6093                "uuid":"u2"}"#,
6094            r#"{"type":"system","subtype":"task_notification","session_id":"s1",
6095                "task_id":"t1","tool_use_id":"tu1","status":"completed",
6096                "summary":"done","output_file":"",
6097                "usage":{"duration_ms":100,"tool_uses":1,"total_tokens":500},
6098                "uuid":"u3"}"#,
6099        ];
6100
6101        for json in cases {
6102            let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6103            let value = serde_json::to_value(&output).unwrap();
6104            let reparsed: ClaudeOutput = serde_json::from_value(value).unwrap();
6105
6106            let ClaudeOutput::System(sys) = reparsed else {
6107                panic!("Expected System variant after round-trip");
6108            };
6109
6110            match sys.subtype {
6111                super::SystemSubtype::TaskStarted => {
6112                    assert!(
6113                        sys.as_task_started().is_some(),
6114                        "as_task_started failed after round-trip"
6115                    );
6116                }
6117                super::SystemSubtype::TaskProgress => {
6118                    assert!(
6119                        sys.as_task_progress().is_some(),
6120                        "as_task_progress failed after round-trip"
6121                    );
6122                }
6123                super::SystemSubtype::TaskNotification => {
6124                    assert!(
6125                        sys.as_task_notification().is_some(),
6126                        "as_task_notification failed after round-trip"
6127                    );
6128                }
6129                other => panic!("unexpected subtype after round-trip: {other:?}"),
6130            }
6131        }
6132    }
6133
6134    #[test]
6135    fn test_system_message_compact_boundary() {
6136        let json = r#"{
6137            "type": "system",
6138            "subtype": "compact_boundary",
6139            "session_id": "879c1a88-3756-4092-aa95-0020c4ed9692",
6140            "compact_metadata": {
6141                "pre_tokens": 155285,
6142                "trigger": "auto"
6143            },
6144            "uuid": "a67780d5-74cb-48b1-9137-7a6e7cee45d7"
6145        }"#;
6146
6147        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6148        if let ClaudeOutput::System(sys) = output {
6149            assert!(sys.is_compact_boundary());
6150            assert!(!sys.is_init());
6151            assert!(!sys.is_status());
6152
6153            let compact = sys
6154                .as_compact_boundary()
6155                .expect("Should parse as compact_boundary");
6156            assert_eq!(compact.session_id, "879c1a88-3756-4092-aa95-0020c4ed9692");
6157            assert_eq!(compact.compact_metadata.pre_tokens, 155285);
6158            assert_eq!(
6159                compact.compact_metadata.trigger,
6160                super::CompactionTrigger::Auto
6161            );
6162            // Per-compaction stats are optional and absent here.
6163            assert!(compact.summary.is_none());
6164            assert!(compact.leaf_message_count.is_none());
6165            assert!(compact.duration_ms.is_none());
6166        } else {
6167            panic!("Expected System message");
6168        }
6169    }
6170
6171    #[test]
6172    fn test_compact_boundary_with_summary_stats() {
6173        // Canonical keys.
6174        let json = r#"{
6175            "type": "system",
6176            "subtype": "compact_boundary",
6177            "session_id": "s1",
6178            "compact_metadata": { "pre_tokens": 1000, "trigger": "manual" },
6179            "summary": "Summarized the earlier exploration.",
6180            "leaf_message_count": 42,
6181            "duration_ms": 1234,
6182            "uuid": "u1"
6183        }"#;
6184        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6185        let ClaudeOutput::System(sys) = output else {
6186            panic!("Expected System message");
6187        };
6188        let compact = sys.as_compact_boundary().expect("compact_boundary");
6189        assert_eq!(
6190            compact.summary.as_deref(),
6191            Some("Summarized the earlier exploration.")
6192        );
6193        assert_eq!(compact.leaf_message_count, Some(42));
6194        assert_eq!(compact.duration_ms, Some(1234));
6195
6196        // Alternate wire keys (`content` for summary, `message_count` for count)
6197        // deserialize into the same fields.
6198        let json_alt = r#"{
6199            "type": "system",
6200            "subtype": "compact_boundary",
6201            "session_id": "s2",
6202            "compact_metadata": { "pre_tokens": 2000, "trigger": "auto" },
6203            "content": "alt-key summary",
6204            "message_count": 7
6205        }"#;
6206        let output: ClaudeOutput = serde_json::from_str(json_alt).unwrap();
6207        let ClaudeOutput::System(sys) = output else {
6208            panic!("Expected System message");
6209        };
6210        let compact = sys.as_compact_boundary().expect("compact_boundary");
6211        assert_eq!(compact.summary.as_deref(), Some("alt-key summary"));
6212        assert_eq!(compact.leaf_message_count, Some(7));
6213    }
6214
6215    #[test]
6216    fn test_init_message_with_new_fields() {
6217        let json = r#"{
6218            "type": "system",
6219            "subtype": "init",
6220            "session_id": "test-session",
6221            "cwd": "/home/user",
6222            "model": "claude-opus-4-7",
6223            "tools": ["Bash"],
6224            "mcp_servers": [],
6225            "permissionMode": "default",
6226            "apiKeySource": "none",
6227            "uuid": "44841a0d-182d-493a-86b5-79800d3d9665",
6228            "memory_paths": {"auto": "/home/user/.claude/projects/memory/"},
6229            "fast_mode_state": "off",
6230            "plugins": [{"name": "lsp", "path": "/plugins/lsp", "source": "lsp@official"}],
6231            "claude_code_version": "2.1.117"
6232        }"#;
6233
6234        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6235        if let ClaudeOutput::System(sys) = output {
6236            let init = sys.as_init().expect("Should parse as init");
6237            assert_eq!(
6238                init.uuid.as_deref(),
6239                Some("44841a0d-182d-493a-86b5-79800d3d9665")
6240            );
6241            assert!(init.memory_paths.is_some());
6242            assert_eq!(init.fast_mode_state.as_deref(), Some("off"));
6243            assert_eq!(init.plugins[0].source.as_deref(), Some("lsp@official"));
6244            assert_eq!(init.claude_code_version.as_deref(), Some("2.1.117"));
6245        } else {
6246            panic!("Expected System message");
6247        }
6248    }
6249
6250    #[test]
6251    fn test_assistant_message_with_new_fields() {
6252        let json = r#"{
6253            "type": "assistant",
6254            "message": {
6255                "id": "msg_1",
6256                "type": "message",
6257                "role": "assistant",
6258                "model": "claude-opus-4-7",
6259                "content": [{"type": "text", "text": "Hello"}],
6260                "stop_reason": "end_turn",
6261                "stop_details": null,
6262                "context_management": null,
6263                "usage": {
6264                    "input_tokens": 100,
6265                    "output_tokens": 10,
6266                    "cache_creation_input_tokens": 50,
6267                    "cache_read_input_tokens": 0,
6268                    "service_tier": "standard",
6269                    "inference_geo": "not_available"
6270                }
6271            },
6272            "session_id": "abc",
6273            "uuid": "msg-uuid-123"
6274        }"#;
6275
6276        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6277        if let ClaudeOutput::Assistant(asst) = output {
6278            assert_eq!(asst.message.stop_details, None);
6279            assert_eq!(asst.message.context_management, None);
6280            let usage = asst.message.usage.unwrap();
6281            assert_eq!(usage.inference_geo.as_deref(), Some("not_available"));
6282        } else {
6283            panic!("Expected Assistant message");
6284        }
6285    }
6286
6287    #[test]
6288    fn test_user_message_with_new_fields() {
6289        let json = r#"{
6290            "type": "user",
6291            "message": {
6292                "role": "user",
6293                "content": [{"type": "text", "text": "Hello"}]
6294            },
6295            "session_id": "9abbc466-dad0-4b8e-b6b0-cad5eb7a16b9",
6296            "parent_tool_use_id": "toolu_123",
6297            "uuid": "user-msg-456"
6298        }"#;
6299
6300        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6301        if let ClaudeOutput::User(user) = output {
6302            assert_eq!(user.parent_tool_use_id.as_deref(), Some("toolu_123"));
6303            assert_eq!(user.uuid.as_deref(), Some("user-msg-456"));
6304        } else {
6305            panic!("Expected User message");
6306        }
6307    }
6308
6309    /// Real wire payload captured from the CLI after answering an
6310    /// AskUserQuestion via the permission control protocol. The top-level
6311    /// `tool_use_result` and `timestamp` fields must round-trip without loss —
6312    /// proxies using this crate to relay messages to a viewer rely on those
6313    /// fields being preserved (the viewer reads `tool_use_result.answers`).
6314    #[test]
6315    fn test_user_message_preserves_tool_use_result_and_timestamp() {
6316        let json = r#"{
6317            "type":"user",
6318            "message":{"role":"user","content":[{"type":"tool_result","content":"User has answered your questions: . You can now continue with the user's answers in mind.","tool_use_id":"toolu_01331duMqP2PrRaqR2yWa8e4"}]},
6319            "parent_tool_use_id":null,
6320            "session_id":"622ae0c3-3d50-4fa7-9ee0-69d691238c6d",
6321            "uuid":"8ef6e997-a849-4d15-bed3-2837c3d3f4cd",
6322            "timestamp":"2026-05-12T23:12:04.121Z",
6323            "tool_use_result":{"questions":[{"question":"Which color do you prefer?","header":"Color","options":[{"label":"Red","description":"A warm color"},{"label":"Blue","description":"A cool color"}],"multiSelect":false}],"answers":{"Color":"Blue"}}
6324        }"#;
6325
6326        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6327        let user = match output {
6328            ClaudeOutput::User(u) => u,
6329            other => panic!("Expected User message, got {:?}", other.message_type()),
6330        };
6331
6332        assert_eq!(user.timestamp.as_deref(), Some("2026-05-12T23:12:04.121Z"));
6333        let raw = user
6334            .tool_use_result
6335            .as_ref()
6336            .expect("tool_use_result must be captured");
6337        assert_eq!(raw["answers"]["Color"], "Blue");
6338        assert_eq!(raw["questions"][0]["header"], "Color");
6339
6340        // Round-trip: re-serialize and confirm tool_use_result + timestamp
6341        // survive — the bug we're guarding against is that the proxy silently
6342        // drops these fields when relaying user messages.
6343        let reser: serde_json::Value = serde_json::to_value(&user).unwrap();
6344        assert_eq!(reser["timestamp"], "2026-05-12T23:12:04.121Z");
6345        assert_eq!(reser["tool_use_result"]["answers"]["Color"], "Blue");
6346        assert_eq!(
6347            reser["tool_use_result"]["questions"][0]["question"],
6348            "Which color do you prefer?"
6349        );
6350
6351        // Typed accessor: AskUserQuestionInput has the same shape as the
6352        // AskUserQuestion tool_use_result.
6353        let typed: crate::AskUserQuestionInput = user
6354            .tool_use_result_as::<crate::AskUserQuestionInput>()
6355            .expect("tool_use_result present")
6356            .expect("AskUserQuestionInput parses");
6357        assert_eq!(typed.questions.len(), 1);
6358        assert_eq!(typed.questions[0].header, "Color");
6359        let answers = typed.answers.expect("answers populated");
6360        assert_eq!(answers.get("Color").map(String::as_str), Some("Blue"));
6361    }
6362
6363    /// User messages without `tool_use_result` / `timestamp` must still
6364    /// deserialize fine and serialize back without spuriously emitting nulls.
6365    #[test]
6366    fn test_user_message_without_tool_use_result_omits_field() {
6367        let json = r#"{
6368            "type":"user",
6369            "message":{"role":"user","content":[{"type":"text","text":"hello"}]},
6370            "session_id":"622ae0c3-3d50-4fa7-9ee0-69d691238c6d"
6371        }"#;
6372
6373        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6374        let user = match output {
6375            ClaudeOutput::User(u) => u,
6376            _ => panic!("Expected User message"),
6377        };
6378        assert!(user.tool_use_result.is_none());
6379        assert!(user.timestamp.is_none());
6380
6381        let reser = serde_json::to_value(&user).unwrap();
6382        assert!(reser.get("tool_use_result").is_none());
6383        assert!(reser.get("timestamp").is_none());
6384    }
6385
6386    /// A `Task` tool result must expose subagent token / timing / tool-use
6387    /// accounting through the typed [`UserMessage::subagent_result`] accessor,
6388    /// including the nested per-model `usage` breakdown and `toolStats`.
6389    #[test]
6390    fn test_subagent_result_exposes_token_accounting() {
6391        let json = r#"{
6392            "type":"user",
6393            "message":{"role":"user","content":[{"tool_use_id":"toolu_01","type":"tool_result","content":[{"type":"text","text":"21"}]}]},
6394            "session_id":"d3fc5942-75e5-4aa1-a87d-b9484a176541",
6395            "tool_use_result":{
6396                "status":"completed",
6397                "prompt":"Count the .rs files.",
6398                "agentId":"ac4f0276e9d4b6232",
6399                "agentType":"Explore",
6400                "content":[{"type":"text","text":"21"}],
6401                "resolvedModel":"claude-haiku-4-5-20251001",
6402                "totalDurationMs":6869,
6403                "totalTokens":7834,
6404                "totalToolUseCount":1,
6405                "usage":{"input_tokens":6,"cache_creation_input_tokens":125,"cache_read_input_tokens":7699,"output_tokens":4,"service_tier":"standard"},
6406                "toolStats":{"readCount":0,"searchCount":0,"bashCount":1,"editFileCount":0,"linesAdded":0,"linesRemoved":0,"otherToolCount":0}
6407            }
6408        }"#;
6409
6410        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6411        let user = match output {
6412            ClaudeOutput::User(u) => u,
6413            _ => panic!("Expected User message"),
6414        };
6415
6416        let result = user.subagent_result().expect("subagent result parses");
6417        assert_eq!(result.agent_type.as_deref(), Some("Explore"));
6418        assert_eq!(
6419            result.resolved_model.as_deref(),
6420            Some("claude-haiku-4-5-20251001")
6421        );
6422        assert_eq!(result.total_tokens, Some(7834));
6423        assert_eq!(result.total_duration_ms, Some(6869));
6424        assert_eq!(result.total_tool_use_count, Some(1));
6425
6426        let usage = result.usage.expect("nested usage present");
6427        assert_eq!(usage.input_tokens, 6);
6428        assert_eq!(usage.cache_read_input_tokens, 7699);
6429
6430        let stats = result.tool_stats.expect("toolStats present");
6431        assert_eq!(stats.bash_count, 1);
6432    }
6433
6434    /// `tool_use_result` shapes that aren't subagent runs (e.g. AskUserQuestion)
6435    /// parse leniently into the all-`Option` [`SubagentResult`] with empty
6436    /// accounting rather than failing, so callers can probe without panicking.
6437    #[test]
6438    fn test_subagent_result_absent_for_non_task_result() {
6439        let json = r#"{
6440            "type":"user",
6441            "message":{"role":"user","content":[{"type":"text","text":"hi"}]},
6442            "session_id":"622ae0c3-3d50-4fa7-9ee0-69d691238c6d",
6443            "tool_use_result":{"questions":[],"answers":{"Color":"Blue"}}
6444        }"#;
6445
6446        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6447        let user = match output {
6448            ClaudeOutput::User(u) => u,
6449            _ => panic!("Expected User message"),
6450        };
6451
6452        let result = user.subagent_result().expect("lenient parse");
6453        assert_eq!(result.total_tokens, None);
6454        assert_eq!(result.agent_type, None);
6455    }
6456
6457    #[test]
6458    fn test_init_fast_mode_reason_and_mcp_server_errors_fully_wrapped() {
6459        use serde_json::Value;
6460
6461        let raw: Value = serde_json::from_str(
6462            r#"{
6463            "type":"system","subtype":"init","session_id":"s1","uuid":"u1",
6464            "fast_mode_state":"off",
6465            "fast_mode_disabled_reason":"not_first_party",
6466            "mcp_server_errors":[{"name":"broken","type":"invalid_config","message":"url entry with no type"}]
6467        }"#,
6468        )
6469        .unwrap();
6470        crate::io::assert_fully_wrapped(&raw);
6471
6472        let output: ClaudeOutput = serde_json::from_value(raw).unwrap();
6473        let ClaudeOutput::System(sys) = output else {
6474            panic!("expected System");
6475        };
6476        let init = sys.as_init().expect("parses as init");
6477        assert_eq!(
6478            init.fast_mode_disabled_reason,
6479            Some(crate::FastModeDisabledReason::NotFirstParty)
6480        );
6481        let errs = init.mcp_server_errors.unwrap();
6482        assert_eq!(errs.len(), 1);
6483        assert_eq!(errs[0].name, "broken");
6484        assert_eq!(errs[0].error_type, "invalid_config");
6485    }
6486
6487    #[test]
6488    fn test_code_change_published_fully_wrapped() {
6489        use super::{KnownSystemEvent, SystemSubtype};
6490        use serde_json::Value;
6491
6492        let raw: Value = serde_json::from_str(
6493            r#"{
6494            "type":"system","subtype":"code_change_published",
6495            "provider":"github","url":"https://github.com/owner/repo/pull/42",
6496            "repo":"owner/repo","identifier":"42",
6497            "uuid":"u1","session_id":"s1"
6498        }"#,
6499        )
6500        .unwrap();
6501        crate::io::assert_fully_wrapped(&raw);
6502
6503        let output: ClaudeOutput = serde_json::from_value(raw).unwrap();
6504        let ClaudeOutput::System(sys) = output else {
6505            panic!("expected System");
6506        };
6507        assert_eq!(sys.subtype, SystemSubtype::CodeChangePublished);
6508        let Some(KnownSystemEvent::CodeChangePublished(msg)) = sys.as_known_system_event() else {
6509            panic!("expected CodeChangePublished event");
6510        };
6511        assert_eq!(msg.provider, "github");
6512        assert_eq!(msg.repo, "owner/repo");
6513        assert_eq!(msg.identifier, "42");
6514
6515        assert!(sys.is_code_change_published());
6516        assert!(!sys.is_vcs_state_changed());
6517        let direct = sys.as_code_change_published().expect("direct accessor");
6518        assert_eq!(direct.url, "https://github.com/owner/repo/pull/42");
6519        assert!(sys.as_vcs_state_changed().is_none());
6520    }
6521
6522    #[test]
6523    fn test_feedback_draft_queued_fully_wrapped() {
6524        use super::{KnownSystemEvent, SystemSubtype};
6525        use serde_json::Value;
6526
6527        let raw: Value = serde_json::from_str(
6528            r#"{
6529            "type":"system","subtype":"feedback_draft_queued",
6530            "draft_id":"draft-1","draft_type":"bug_report",
6531            "title":"Tool output was truncated",
6532            "details_preview":"The last command omitted its final lines",
6533            "uuid":"u1","session_id":"s1","future_field":"preserved"
6534        }"#,
6535        )
6536        .unwrap();
6537        crate::io::assert_fully_wrapped(&raw);
6538
6539        let output: ClaudeOutput = serde_json::from_value(raw).unwrap();
6540        let ClaudeOutput::System(sys) = output else {
6541            panic!("expected System");
6542        };
6543        assert_eq!(sys.subtype, SystemSubtype::FeedbackDraftQueued);
6544        assert!(sys.is_feedback_draft_queued());
6545        assert!(!sys.is_vcs_state_changed());
6546
6547        let direct = sys
6548            .as_feedback_draft_queued()
6549            .expect("direct typed accessor");
6550        assert_eq!(direct.draft_id, "draft-1");
6551        assert_eq!(direct.draft_type, "bug_report");
6552        assert_eq!(direct.extra["future_field"], "preserved");
6553
6554        let Some(KnownSystemEvent::FeedbackDraftQueued(known)) = sys.as_known_system_event() else {
6555            panic!("expected FeedbackDraftQueued event");
6556        };
6557        assert_eq!(known.title, "Tool output was truncated");
6558        assert_eq!(
6559            sys.typed_value().expect("typed value")["future_field"],
6560            "preserved"
6561        );
6562    }
6563
6564    #[test]
6565    fn test_cloud_session_delta_fully_wrapped() {
6566        use super::{KnownSystemEvent, SystemSubtype};
6567        use serde_json::Value;
6568
6569        let raw: Value = serde_json::from_str(
6570            r#"{
6571            "type":"system","subtype":"cloud_session_delta",
6572            "seq":3,"changed":["serving","connection"],
6573            "cloud_session":{"id":"session_abc","view_url":"https://example.invalid/s/abc",
6574                "serving":{"state":"on"},"connection":{"state":"live"}},
6575            "uuid":"u1","session_id":"s1","future_field":"preserved"
6576        }"#,
6577        )
6578        .unwrap();
6579        crate::io::assert_fully_wrapped(&raw);
6580
6581        let output: ClaudeOutput = serde_json::from_value(raw).unwrap();
6582        let ClaudeOutput::System(sys) = output else {
6583            panic!("expected System");
6584        };
6585        assert_eq!(sys.subtype, SystemSubtype::CloudSessionDelta);
6586        assert!(sys.is_cloud_session_delta());
6587        assert!(!sys.is_feedback_draft_queued());
6588
6589        let direct = sys.as_cloud_session_delta().expect("direct typed accessor");
6590        assert_eq!(direct.seq, 3);
6591        assert_eq!(direct.changed, vec!["serving", "connection"]);
6592        assert_eq!(direct.cloud_session["id"], "session_abc");
6593        assert_eq!(direct.extra["future_field"], "preserved");
6594
6595        let Some(KnownSystemEvent::CloudSessionDelta(known)) = sys.as_known_system_event() else {
6596            panic!("expected CloudSessionDelta event");
6597        };
6598        assert_eq!(known.session_id, "s1");
6599        assert_eq!(
6600            sys.typed_value().expect("typed value")["future_field"],
6601            "preserved"
6602        );
6603    }
6604
6605    #[test]
6606    fn test_vcs_state_changed_fully_wrapped() {
6607        use super::{KnownSystemEvent, VcsMutationKind};
6608        use serde_json::Value;
6609
6610        for kind in ["commit", "push", "merge", "rebase"] {
6611            let raw: Value = serde_json::from_str(&format!(
6612                r#"{{"type":"system","subtype":"vcs_state_changed","kind":"{}","cwd":"/repo","uuid":"u1","session_id":"s1"}}"#,
6613                kind
6614            ))
6615            .unwrap();
6616            crate::io::assert_fully_wrapped(&raw);
6617
6618            let output: ClaudeOutput = serde_json::from_value(raw).unwrap();
6619            let ClaudeOutput::System(sys) = output else {
6620                panic!("expected System");
6621            };
6622            let Some(KnownSystemEvent::VcsStateChanged(msg)) = sys.as_known_system_event() else {
6623                panic!("expected VcsStateChanged event");
6624            };
6625            assert_eq!(msg.kind.as_str(), kind);
6626            assert!(!matches!(msg.kind, VcsMutationKind::Unknown(_)));
6627        }
6628
6629        // Unknown kinds are valid per the wire contract.
6630        let raw: Value = serde_json::from_str(
6631            r#"{"type":"system","subtype":"vcs_state_changed","kind":"tag","cwd":"/repo","uuid":"u2","session_id":"s2"}"#,
6632        )
6633        .unwrap();
6634        crate::io::assert_fully_wrapped(&raw);
6635        let output: ClaudeOutput = serde_json::from_value(raw).unwrap();
6636        let ClaudeOutput::System(sys) = output else {
6637            panic!("expected System");
6638        };
6639        let Some(KnownSystemEvent::VcsStateChanged(msg)) = sys.as_known_system_event() else {
6640            panic!("expected VcsStateChanged event");
6641        };
6642        assert_eq!(msg.kind, VcsMutationKind::Unknown("tag".to_string()));
6643
6644        assert!(sys.is_vcs_state_changed());
6645        let direct = sys.as_vcs_state_changed().expect("direct accessor");
6646        assert_eq!(direct.cwd, "/repo");
6647        assert!(sys.as_code_change_published().is_none());
6648    }
6649
6650    #[test]
6651    fn test_assistant_aborted_and_resume_flags_roundtrip() {
6652        let json = r#"{
6653            "type":"assistant",
6654            "message":{"id":"msg_1","role":"assistant","model":"claude-3","content":[{"type":"text","text":"partial"}]},
6655            "session_id":"s1",
6656            "aborted":true,
6657            "resumed_from_incomplete_thinking":true
6658        }"#;
6659        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6660        let ClaudeOutput::Assistant(msg) = &output else {
6661            panic!("expected Assistant");
6662        };
6663        assert_eq!(msg.aborted, Some(true));
6664        assert_eq!(msg.resumed_from_incomplete_thinking, Some(true));
6665        let reserialized = serde_json::to_string(&output).unwrap();
6666        assert!(reserialized.contains("\"aborted\":true"));
6667        assert!(reserialized.contains("\"resumed_from_incomplete_thinking\":true"));
6668
6669        // Absent flags stay absent on the wire.
6670        let json = r#"{
6671            "type":"assistant",
6672            "message":{"id":"msg_2","role":"assistant","model":"claude-3","content":[]},
6673            "session_id":"s2"
6674        }"#;
6675        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6676        let reserialized = serde_json::to_string(&output).unwrap();
6677        assert!(!reserialized.contains("aborted"));
6678        assert!(!reserialized.contains("resumed_from_incomplete_thinking"));
6679    }
6680
6681    #[test]
6682    fn test_user_tool_result_meta_roundtrip() {
6683        let json = r#"{
6684            "type":"user",
6685            "message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_1","content":"denied"}]},
6686            "session_id":"622ae0c3-3d50-4fa7-9ee0-69d691238c6d",
6687            "tool_result_meta":[
6688                {"id":"toolu_1","non_execution_kind":"user-rejected","user_feedback":"use the staging db"},
6689                {"id":"toolu_2","non_execution_kind":"permission-rule"}
6690            ]
6691        }"#;
6692        let output: ClaudeOutput = serde_json::from_str(json).unwrap();
6693        let ClaudeOutput::User(user) = &output else {
6694            panic!("expected User");
6695        };
6696        let meta = user.tool_result_meta.as_ref().unwrap();
6697        assert_eq!(meta.len(), 2);
6698        assert_eq!(meta[0].non_execution_kind.as_deref(), Some("user-rejected"));
6699        assert_eq!(meta[0].user_feedback.as_deref(), Some("use the staging db"));
6700        assert_eq!(meta[1].user_feedback, None);
6701
6702        let reserialized = serde_json::to_string(&output).unwrap();
6703        assert!(reserialized.contains("\"non_execution_kind\":\"user-rejected\""));
6704        assert!(!reserialized.contains("\"user_feedback\":null"));
6705    }
6706
6707    /// CLI 2.1.222 added `scope` to `system/model_refusal_fallback`:
6708    /// "session" (main-thread swap, also the meaning when absent on older
6709    /// CLIs) vs "local" (subagent/side-question fallback only).
6710    #[test]
6711    fn model_refusal_fallback_scope_roundtrips_and_defaults() {
6712        use super::{ModelRefusalFallbackMessage, RefusalFallbackScope};
6713        let with_scope = serde_json::json!({
6714            "trigger": "refusal",
6715            "direction": "retry",
6716            "scope": "local",
6717            "original_model": "claude-fable-5",
6718            "fallback_model": "claude-opus-5",
6719            "request_id": null,
6720            "content": "Refused; retried on fallback model.",
6721            "uuid": "u1",
6722            "session_id": "s1"
6723        });
6724        let msg: ModelRefusalFallbackMessage = serde_json::from_value(with_scope.clone()).unwrap();
6725        assert_eq!(msg.scope, Some(RefusalFallbackScope::Local));
6726        assert_eq!(serde_json::to_value(&msg).unwrap(), with_scope);
6727
6728        // Older CLIs omit scope — absent, not null, and treated as session
6729        // by consumers per the wire docs.
6730        let mut without = with_scope.clone();
6731        without.as_object_mut().unwrap().remove("scope");
6732        let msg: ModelRefusalFallbackMessage = serde_json::from_value(without.clone()).unwrap();
6733        assert_eq!(msg.scope, None);
6734        assert_eq!(serde_json::to_value(&msg).unwrap(), without);
6735
6736        // Open enum: unknown scopes pass through verbatim.
6737        assert_eq!(
6738            RefusalFallbackScope::from("workspace").as_str(),
6739            "workspace"
6740        );
6741    }
6742}