Skip to main content

bamboo_agent_core/agent/
events.rs

1//! Agent event system for real-time streaming.
2//!
3//! This module defines the event types emitted during agent execution,
4//! which are streamed to clients via Server-Sent Events (SSE).
5//!
6//! # Event Types
7//!
8//! - [`AgentEvent`] - All possible agent execution events
9//! - [`TokenUsage`] - Token consumption statistics
10//! - [`TokenBudgetUsage`] - Detailed token budget information
11//!
12//! # Event Flow
13//!
14//! 1. **Token** events stream generated text
15//! 2. **ToolStart/ToolComplete** track tool execution
16//! 3. **TaskListUpdated** tracks progress
17//! 4. **TokenBudgetUpdated** reports context management
18//! 5. **Complete**, **Cancelled**, or **Error** ends the stream
19//!
20//! # Example
21//!
22//! ```javascript
23//! const eventSource = new EventSource('/api/v1/events/session-id');
24//! eventSource.onmessage = (event) => {
25//!   const data = JSON.parse(event.data);
26//!   switch (data.type) {
27//!     case 'token':
28//!       console.log('Token:', data.content);
29//!       break;
30//!     case 'complete':
31//!       console.log('Done!');
32//!       eventSource.close();
33//!       break;
34//!   }
35//! };
36//! ```
37
38use crate::tools::ToolResult;
39use bamboo_domain::{TaskItemStatus, TaskList};
40use chrono::{DateTime, Utc};
41use serde::{Deserialize, Serialize};
42
43/// Represents events emitted during agent execution.
44///
45/// These events are streamed to clients via SSE to provide real-time
46/// feedback on agent progress, tool execution, and completion.
47///
48/// # Variants
49///
50/// ## Text Generation
51/// - `Token` - Streaming text token
52/// - `ReasoningToken` - Streaming reasoning/thinking token (separate channel)
53///
54/// ## Tool Execution
55/// - `ToolStart` - Tool execution started
56/// - `ToolComplete` - Tool finished successfully
57/// - `ToolError` - Tool execution failed
58///
59/// ## User Interaction
60/// - `NeedClarification` - Agent needs user input
61///
62/// ## Progress Tracking
63/// - `TaskListUpdated` - Task list created or modified
64/// - `TaskListItemProgress` - Individual item progress
65/// - `TaskListCompleted` - All items completed
66/// - `TaskEvaluationStarted` - Task evaluation began
67/// - `TaskEvaluationCompleted` - Task evaluation finished
68/// - `GoldEvaluationStarted` - Gold observe-only evaluation began
69/// - `GoldEvaluationCompleted` - Gold observe-only evaluation finished
70///
71/// ## Context Management
72/// - `TokenBudgetUpdated` - Context budget changed
73/// - `ContextCompressionStatus` - Context compression lifecycle progress
74/// - `ContextSummarized` - Old messages summarized
75///
76/// ## Sub-agents (Async Spawn)
77/// - `SubAgentStarted` - A child session is created and scheduled to run
78/// - `SubAgentEvent` - Forwarded raw child event (full fidelity)
79/// - `SubAgentHeartbeat` - Periodic heartbeat while the child is running
80/// - `SubAgentCompleted` - Child session finished (completed/cancelled/error)
81///
82/// ## Terminal Events
83/// - `Complete` - Execution finished successfully
84/// - `Cancelled` - Execution was cancelled by the user
85/// - `Error` - Execution failed
86///
87/// # Serialization
88///
89/// Events are serialized as JSON with a `type` field for discrimination:
90/// ```json
91/// {"type": "token", "content": "Hello"}
92/// {"type": "complete", "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}}
93/// {"type": "cancelled", "message": "Agent execution cancelled by user"}
94/// ```
95#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(tag = "type", rename_all = "snake_case")]
97pub enum AgentEvent {
98    /// Text token generated by the LLM.
99    Token {
100        /// Generated text content
101        content: String,
102    },
103
104    /// Reasoning/thinking token generated by the LLM.
105    ///
106    /// This is streamed separately from assistant answer tokens so the UI can
107    /// choose whether and how to display model reasoning traces.
108    ReasoningToken {
109        /// Generated reasoning content
110        content: String,
111    },
112
113    /// Streaming output emitted while a specific tool call is running.
114    ///
115    /// This is used to render "live output" inside a tool-call card in the UI
116    /// without mixing tool output into the assistant's main token stream.
117    ToolToken {
118        /// Tool call identifier that this output belongs to.
119        tool_call_id: String,
120        /// Output chunk.
121        content: String,
122    },
123
124    /// Tool execution started.
125    ToolStart {
126        /// Unique tool call identifier
127        tool_call_id: String,
128        /// Name of the tool being executed
129        tool_name: String,
130        /// Tool arguments (JSON)
131        arguments: serde_json::Value,
132    },
133
134    /// Tool execution completed successfully.
135    ToolComplete {
136        /// Tool call identifier
137        tool_call_id: String,
138        /// Tool execution result
139        result: ToolResult,
140    },
141
142    /// Tool execution failed.
143    ToolError {
144        /// Tool call identifier
145        tool_call_id: String,
146        /// Error message
147        error: String,
148    },
149
150    /// Structured lifecycle event for tool execution tracking.
151    ///
152    /// These events complement `ToolStart`/`ToolComplete`/`ToolError` with
153    /// richer metadata (mutability, auto-approval, wall-clock timing) and
154    /// are emitted by `ToolEmitter` (in `bamboo-agent-tools`).
155    ToolLifecycle {
156        /// Tool call identifier
157        tool_call_id: String,
158        /// Canonical tool name
159        tool_name: String,
160        /// Lifecycle phase: "begin", "finished", "error", "cancelled"
161        phase: String,
162        /// Wall-clock milliseconds since the call began (None for begin)
163        #[serde(skip_serializing_if = "Option::is_none")]
164        elapsed_ms: Option<u64>,
165        /// Whether the tool mutates state (writes files, runs commands)
166        is_mutating: bool,
167        /// Whether execution was auto-approved (no user prompt needed)
168        auto_approved: bool,
169        /// Human-readable summary
170        #[serde(skip_serializing_if = "Option::is_none")]
171        summary: Option<String>,
172        /// Error message (if phase == "error")
173        #[serde(skip_serializing_if = "Option::is_none")]
174        error: Option<String>,
175    },
176
177    /// Agent needs clarification from the user.
178    NeedClarification {
179        /// Question to ask the user
180        question: String,
181        /// Optional predefined options
182        options: Option<Vec<String>>,
183        /// Tool call identifier that triggered this clarification
184        #[serde(default, skip_serializing_if = "Option::is_none")]
185        tool_call_id: Option<String>,
186        /// Tool name that triggered this clarification, when known.
187        #[serde(default, skip_serializing_if = "Option::is_none")]
188        tool_name: Option<String>,
189        /// Whether the user can provide a free-text response
190        #[serde(default = "default_allow_custom")]
191        allow_custom: bool,
192    },
193
194    /// Emitted when task list is created or updated.
195    TaskListUpdated {
196        /// Current task list state.
197        task_list: TaskList,
198    },
199
200    /// Emitted when a task item makes progress (delta update).
201    TaskListItemProgress {
202        /// Session identifier
203        session_id: String,
204        /// Item identifier
205        item_id: String,
206        /// New item status
207        status: TaskItemStatus,
208        /// Number of tool calls made
209        tool_calls_count: usize,
210        /// Item version (for optimistic concurrency)
211        version: u64,
212    },
213
214    /// Emitted when all task items are completed.
215    TaskListCompleted {
216        /// Session identifier
217        session_id: String,
218        /// Completion timestamp
219        completed_at: DateTime<Utc>,
220        /// Total agent rounds executed
221        total_rounds: u32,
222        /// Total tool calls made
223        total_tool_calls: usize,
224    },
225
226    /// Emitted when task evaluation starts.
227    TaskEvaluationStarted {
228        /// Session identifier
229        session_id: String,
230        /// Number of items to evaluate
231        items_count: usize,
232    },
233
234    /// Emitted when task evaluation completes.
235    TaskEvaluationCompleted {
236        /// Session identifier
237        session_id: String,
238        /// Number of items updated
239        updates_count: usize,
240        /// Evaluation reasoning
241        reasoning: String,
242    },
243
244    /// Emitted when gold observe-only evaluation starts.
245    GoldEvaluationStarted {
246        /// Session identifier
247        session_id: String,
248        /// Evaluation checkpoint
249        checkpoint: GoldCheckpoint,
250        /// Current iteration / round number associated with the evaluation
251        iteration: u32,
252    },
253
254    /// Emitted when gold observe-only evaluation completes.
255    GoldEvaluationCompleted {
256        /// Session identifier
257        session_id: String,
258        /// Evaluation checkpoint
259        checkpoint: GoldCheckpoint,
260        /// Current iteration / round number associated with the evaluation
261        iteration: u32,
262        /// Gold decision for the current checkpoint
263        decision: GoldDecision,
264        /// Confidence in the decision
265        confidence: GoldConfidence,
266        /// Short reasoning summary
267        reasoning: String,
268    },
269
270    /// Emitted when token budget is prepared (after context truncation)
271    TokenBudgetUpdated {
272        /// Token budget details
273        usage: TokenBudgetUsage,
274    },
275
276    /// Emitted when host-side context compression lifecycle changes.
277    ContextCompressionStatus {
278        /// Compression phase label (for example: pre-turn, mid-turn).
279        phase: String,
280        /// Compression status: started | completed | failed | skipped
281        status: String,
282    },
283
284    /// Emitted when conversation context is summarized
285    ContextSummarized {
286        /// Generated summary text
287        summary: String,
288        /// Number of old messages summarized
289        messages_summarized: usize,
290        /// Tokens saved by summarization
291        tokens_saved: u32,
292        /// Context usage percentage before compression
293        #[serde(default)]
294        usage_before_percent: f64,
295        /// Context usage percentage after compression
296        #[serde(default)]
297        usage_after_percent: f64,
298        /// What triggered the compression: "auto" | "manual" | "critical"
299        #[serde(default)]
300        trigger_type: String,
301    },
302
303    /// Emitted when context pressure reaches warning or critical levels.
304    /// Frontend should display this to the user as a proactive notification.
305    ContextPressureNotification {
306        /// Context usage as a percentage of the context window.
307        percent: f64,
308        /// Severity level: "warning" (70%) or "critical" (90%).
309        level: String,
310        /// Human-readable message describing the pressure state.
311        message: String,
312    },
313
314    /// A child session was spawned from a parent session (async background job).
315    SubAgentStarted {
316        parent_session_id: String,
317        child_session_id: String,
318        /// Optional title (useful for UI lists).
319        #[serde(default, skip_serializing_if = "Option::is_none")]
320        title: Option<String>,
321    },
322
323    /// Forwarded raw child event to the parent session stream.
324    ///
325    /// Child sessions are not allowed to spawn further sessions, so this should not nest.
326    SubAgentEvent {
327        parent_session_id: String,
328        child_session_id: String,
329        event: Box<AgentEvent>,
330    },
331
332    /// Heartbeat emitted while a child session is running.
333    SubAgentHeartbeat {
334        parent_session_id: String,
335        child_session_id: String,
336        timestamp: DateTime<Utc>,
337    },
338
339    /// Child session finished (completed/cancelled/error).
340    SubAgentCompleted {
341        parent_session_id: String,
342        child_session_id: String,
343        /// One of: "completed" | "cancelled" | "error" | "skipped"
344        status: String,
345        #[serde(default, skip_serializing_if = "Option::is_none")]
346        error: Option<String>,
347    },
348
349    /// Plan mode was entered.
350    PlanModeEntered {
351        /// Session identifier
352        session_id: String,
353        /// Optional reason for entering plan mode
354        #[serde(default, skip_serializing_if = "Option::is_none")]
355        reason: Option<String>,
356        /// Previous permission mode before entering plan mode
357        pre_permission_mode: String,
358        /// RFC3339 timestamp when plan mode was entered.
359        entered_at: chrono::DateTime<chrono::Utc>,
360        /// Current plan mode phase/status.
361        status: bamboo_domain::PlanModeStatus,
362        /// Path to the persisted plan file, if already available.
363        #[serde(default, skip_serializing_if = "Option::is_none")]
364        plan_file_path: Option<String>,
365    },
366
367    /// Plan mode was exited.
368    PlanModeExited {
369        /// Session identifier
370        session_id: String,
371        /// Whether the exit was approved by the user
372        approved: bool,
373        /// The permission mode restored after exiting
374        restored_mode: String,
375        /// Plan content that was reviewed, if any
376        #[serde(default, skip_serializing_if = "Option::is_none")]
377        plan: Option<String>,
378    },
379
380    /// Plan file was updated.
381    PlanFileUpdated {
382        /// Session identifier
383        session_id: String,
384        /// Path to the plan file
385        file_path: String,
386        /// Summary of the plan content (truncated)
387        content_summary: String,
388    },
389
390    /// Runner progress update emitted at the start of each agent turn.
391    ///
392    /// Used to track live execution progress (round count, current activity)
393    /// for diagnostic visibility, especially for child sessions.
394    RunnerProgress {
395        /// Session identifier
396        session_id: String,
397        /// Current turn/round count
398        round_count: u32,
399    },
400
401    /// Session title was updated (auto-generated by backend or manually renamed via PATCH).
402    SessionTitleUpdated {
403        session_id: String,
404        title: String,
405        title_version: u64,
406        source: TitleSource,
407        updated_at: chrono::DateTime<chrono::Utc>,
408    },
409
410    /// Session pinned flag was toggled via PATCH.
411    ///
412    /// Replayable metadata event. `pinned` is an idempotent boolean so the
413    /// latest event wins; `updated_at` is used by the frontend to suppress
414    /// stale replays.
415    SessionPinnedUpdated {
416        session_id: String,
417        pinned: bool,
418        updated_at: chrono::DateTime<chrono::Utc>,
419    },
420
421    /// A new session was created.
422    ///
423    /// Change-feed event: durable, journaled, carried on the account `/stream`
424    /// feed so other clients can insert the session into their list without a
425    /// full `GET /sessions` poll.
426    SessionCreated {
427        session_id: String,
428        title: String,
429        kind: bamboo_domain::SessionKind,
430        created_at: chrono::DateTime<chrono::Utc>,
431    },
432
433    /// A session was deleted.
434    ///
435    /// Change-feed event: durable, journaled. Clients remove the session from
436    /// their local list on receipt.
437    SessionDeleted { session_id: String },
438
439    /// A session's message history was cleared (session kept).
440    ///
441    /// Change-feed event: durable, journaled. Clients drop cached messages for
442    /// the session and refetch lazily.
443    SessionCleared { session_id: String },
444
445    /// A message was appended to a session.
446    ///
447    /// Change-feed event: durable, journaled. The `seq` assigned to this event
448    /// on the account feed is the message's feed coordinate (used by
449    /// `GET /history/{id}?since={seq}` to compute deltas). `content` is the
450    /// plain-text body matching what `/history` returns to the UI.
451    MessageAppended {
452        session_id: String,
453        message_id: String,
454        role: bamboo_domain::Role,
455        content: String,
456        created_at: chrono::DateTime<chrono::Utc>,
457    },
458
459    /// Execution run has started and the runner is now active.
460    ///
461    /// Emitted as the first event after a runner reservation succeeds,
462    /// before any token or tool events. Carries the `run_id` so the
463    /// frontend can correlate subsequent SSE events across reconnects.
464    ExecutionStarted {
465        /// Unique identifier for this execution run.
466        run_id: String,
467        /// Session identifier.
468        session_id: String,
469        /// ISO 8601 timestamp when the run started.
470        started_at: String,
471    },
472
473    /// Tool execution requires user approval before proceeding.
474    ///
475    /// Emitted when a permission checker determines that a tool call needs
476    /// explicit user confirmation (e.g., mutating operations in restricted
477    /// permission mode). The frontend should present the approval request and
478    /// either grant or deny it.
479    ToolApprovalRequested {
480        /// Unique identifier for the tool call awaiting approval.
481        tool_call_id: String,
482        /// Name of the tool being executed.
483        tool_name: String,
484        /// Parameters that were passed to the tool.
485        parameters: serde_json::Value,
486    },
487
488    /// Agent execution completed successfully.
489    Complete {
490        /// Final token usage statistics
491        usage: TokenUsage,
492    },
493
494    /// Agent execution was cancelled.
495    Cancelled {
496        /// Optional human-readable message explaining the cancellation.
497        #[serde(default, skip_serializing_if = "Option::is_none")]
498        message: Option<String>,
499    },
500
501    /// Agent execution failed.
502    Error {
503        /// Error message
504        message: String,
505    },
506}
507
508impl AgentEvent {
509    /// Returns the session this event pertains to, when it carries one.
510    ///
511    /// Used by the account change-feed to route each event to the right
512    /// client-side session without a per-session connection. For sub-agent
513    /// events the *parent* session id is returned (that is the session a client
514    /// observes in its list). Pure streaming/diagnostic variants (`Token`,
515    /// `Complete`, …) return `None`; those are ephemeral and never ride the
516    /// account feed anyway.
517    pub fn session_id(&self) -> Option<&str> {
518        match self {
519            AgentEvent::TaskListUpdated { task_list } => Some(task_list.session_id.as_str()),
520            AgentEvent::TaskListItemProgress { session_id, .. }
521            | AgentEvent::TaskListCompleted { session_id, .. }
522            | AgentEvent::TaskEvaluationStarted { session_id, .. }
523            | AgentEvent::TaskEvaluationCompleted { session_id, .. }
524            | AgentEvent::GoldEvaluationStarted { session_id, .. }
525            | AgentEvent::GoldEvaluationCompleted { session_id, .. }
526            | AgentEvent::PlanModeEntered { session_id, .. }
527            | AgentEvent::PlanModeExited { session_id, .. }
528            | AgentEvent::PlanFileUpdated { session_id, .. }
529            | AgentEvent::RunnerProgress { session_id, .. }
530            | AgentEvent::SessionTitleUpdated { session_id, .. }
531            | AgentEvent::SessionPinnedUpdated { session_id, .. }
532            | AgentEvent::SessionCreated { session_id, .. }
533            | AgentEvent::SessionDeleted { session_id, .. }
534            | AgentEvent::SessionCleared { session_id, .. }
535            | AgentEvent::MessageAppended { session_id, .. }
536            | AgentEvent::ExecutionStarted { session_id, .. } => Some(session_id.as_str()),
537            AgentEvent::SubAgentStarted {
538                parent_session_id, ..
539            }
540            | AgentEvent::SubAgentEvent {
541                parent_session_id, ..
542            }
543            | AgentEvent::SubAgentHeartbeat {
544                parent_session_id, ..
545            }
546            | AgentEvent::SubAgentCompleted {
547                parent_session_id, ..
548            } => Some(parent_session_id.as_str()),
549            _ => None,
550        }
551    }
552
553    /// Whether this event belongs on the durable account change feed.
554    ///
555    /// Durable change events are low-volume, journaled to disk, and resumable
556    /// via the account `/stream` feed. Ephemeral events — token-by-token
557    /// streaming (`Token`/`ReasoningToken`/`ToolToken`), heartbeats, live
558    /// budget/pressure gauges, and raw forwarded sub-agent events — return
559    /// `false`: they stay exclusively on the per-session `/events/{id}` stream.
560    /// Keeping them off the journal and the multiplexed feed is the core
561    /// data-transfer win. This method lives in core so both the server and the
562    /// engine forwarder can filter before cloning onto the feed.
563    pub fn is_durable_change(&self) -> bool {
564        matches!(
565            self,
566            AgentEvent::MessageAppended { .. }
567                | AgentEvent::SessionCreated { .. }
568                | AgentEvent::SessionDeleted { .. }
569                | AgentEvent::SessionCleared { .. }
570                | AgentEvent::SessionTitleUpdated { .. }
571                | AgentEvent::SessionPinnedUpdated { .. }
572                | AgentEvent::TaskListUpdated { .. }
573                | AgentEvent::TaskListItemProgress { .. }
574                | AgentEvent::TaskListCompleted { .. }
575                | AgentEvent::TaskEvaluationCompleted { .. }
576                | AgentEvent::PlanModeEntered { .. }
577                | AgentEvent::PlanModeExited { .. }
578                | AgentEvent::PlanFileUpdated { .. }
579                | AgentEvent::SubAgentStarted { .. }
580                | AgentEvent::SubAgentCompleted { .. }
581                | AgentEvent::NeedClarification { .. }
582                | AgentEvent::ToolApprovalRequested { .. }
583                | AgentEvent::ExecutionStarted { .. }
584                | AgentEvent::Complete { .. }
585                | AgentEvent::Cancelled { .. }
586                | AgentEvent::Error { .. }
587        )
588    }
589}
590
591fn default_allow_custom() -> bool {
592    true
593}
594
595/// Gold evaluation checkpoint.
596#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
597#[serde(rename_all = "snake_case")]
598pub enum GoldCheckpoint {
599    PostRound,
600    Terminal,
601}
602
603impl GoldCheckpoint {
604    pub fn as_str(self) -> &'static str {
605        match self {
606            Self::PostRound => "post_round",
607            Self::Terminal => "terminal",
608        }
609    }
610}
611
612/// Gold evaluator decision.
613#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
614#[serde(rename_all = "snake_case")]
615pub enum GoldDecision {
616    Continue,
617    Achieved,
618    Blocked,
619    NeedInput,
620    Exhausted,
621}
622
623impl GoldDecision {
624    pub fn as_str(self) -> &'static str {
625        match self {
626            Self::Continue => "continue",
627            Self::Achieved => "achieved",
628            Self::Blocked => "blocked",
629            Self::NeedInput => "need_input",
630            Self::Exhausted => "exhausted",
631        }
632    }
633}
634
635/// Confidence level for a Gold evaluation result.
636#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
637#[serde(rename_all = "snake_case")]
638pub enum GoldConfidence {
639    Low,
640    Medium,
641    High,
642}
643
644impl GoldConfidence {
645    pub fn as_str(self) -> &'static str {
646        match self {
647            Self::Low => "low",
648            Self::Medium => "medium",
649            Self::High => "high",
650        }
651    }
652
653    /// Ordinal rank for threshold comparisons (`Low` < `Medium` < `High`).
654    pub fn rank(self) -> u8 {
655        match self {
656            Self::Low => 0,
657            Self::Medium => 1,
658            Self::High => 2,
659        }
660    }
661
662    /// Whether this confidence meets or exceeds the given floor.
663    pub fn meets(self, floor: GoldConfidence) -> bool {
664        self.rank() >= floor.rank()
665    }
666}
667
668/// Source that triggered a session title update.
669#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
670#[serde(rename_all = "snake_case")]
671pub enum TitleSource {
672    Auto,
673    Manual,
674    Fallback,
675}
676
677/// Re-exported shared token usage type.
678///
679/// See [`bamboo_domain::TokenUsage`] for the canonical definition.
680pub use bamboo_domain::TokenUsage;
681
682pub use bamboo_domain::budget_types::TokenBudgetUsage;
683
684#[cfg(test)]
685mod tests {
686    use super::*;
687    use bamboo_domain::{TaskItem, TaskItemStatus, TaskList};
688
689    fn sample_task_list() -> TaskList {
690        TaskList {
691            session_id: "session-1".to_string(),
692            title: "Task List".to_string(),
693            items: vec![TaskItem {
694                id: "task_1".to_string(),
695                description: "Implement event rename".to_string(),
696                status: TaskItemStatus::InProgress,
697                depends_on: Vec::new(),
698                notes: "Implementing".to_string(),
699                ..TaskItem::default()
700            }],
701            created_at: Utc::now(),
702            updated_at: Utc::now(),
703        }
704    }
705
706    #[test]
707    fn task_list_updated_serializes_with_task_names() {
708        let event = AgentEvent::TaskListUpdated {
709            task_list: sample_task_list(),
710        };
711
712        let value = serde_json::to_value(event).expect("event should serialize");
713        assert_eq!(value["type"], "task_list_updated");
714        assert!(value.get("task_list").is_some());
715        assert!(value.get("todo_list").is_none());
716    }
717
718    #[test]
719    fn cancelled_serializes_with_snake_case_type() {
720        let event = AgentEvent::Cancelled {
721            message: Some("Agent execution cancelled by user".to_string()),
722        };
723
724        let value = serde_json::to_value(event).expect("event should serialize");
725        assert_eq!(value["type"], "cancelled");
726        assert_eq!(
727            value["message"],
728            serde_json::Value::String("Agent execution cancelled by user".to_string())
729        );
730    }
731
732    #[test]
733    fn task_evaluation_completed_serializes_with_task_type() {
734        let event = AgentEvent::TaskEvaluationCompleted {
735            session_id: "session-1".to_string(),
736            updates_count: 2,
737            reasoning: "Updated statuses".to_string(),
738        };
739
740        let value = serde_json::to_value(event).expect("event should serialize");
741        assert_eq!(value["type"], "task_evaluation_completed");
742    }
743
744    #[test]
745    fn gold_evaluation_completed_serializes_with_gold_type_and_fields() {
746        let event = AgentEvent::GoldEvaluationCompleted {
747            session_id: "session-1".to_string(),
748            checkpoint: GoldCheckpoint::PostRound,
749            iteration: 3,
750            decision: GoldDecision::Continue,
751            confidence: GoldConfidence::Medium,
752            reasoning: "Need one more iteration".to_string(),
753        };
754
755        let value = serde_json::to_value(event).expect("event should serialize");
756        assert_eq!(value["type"], "gold_evaluation_completed");
757        assert_eq!(value["checkpoint"], "post_round");
758        assert_eq!(value["iteration"], 3);
759        assert_eq!(value["decision"], "continue");
760        assert_eq!(value["confidence"], "medium");
761        assert_eq!(value["reasoning"], "Need one more iteration");
762    }
763
764    #[test]
765    fn gold_evaluation_started_deserializes() {
766        let json = serde_json::json!({
767            "type": "gold_evaluation_started",
768            "session_id": "session-1",
769            "checkpoint": "terminal",
770            "iteration": 7
771        });
772
773        let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
774        match event {
775            AgentEvent::GoldEvaluationStarted {
776                session_id,
777                checkpoint,
778                iteration,
779            } => {
780                assert_eq!(session_id, "session-1");
781                assert_eq!(checkpoint, GoldCheckpoint::Terminal);
782                assert_eq!(iteration, 7);
783            }
784            other => panic!("unexpected event: {other:?}"),
785        }
786    }
787
788    #[test]
789    fn context_compression_status_serializes_with_phase_and_status() {
790        let event = AgentEvent::ContextCompressionStatus {
791            phase: "mid-turn".to_string(),
792            status: "started".to_string(),
793        };
794
795        let value = serde_json::to_value(event).expect("event should serialize");
796        assert_eq!(value["type"], "context_compression_status");
797        assert_eq!(value["phase"], "mid-turn");
798        assert_eq!(value["status"], "started");
799    }
800
801    #[test]
802    fn need_clarification_serializes_with_new_fields() {
803        let event = AgentEvent::NeedClarification {
804            question: "Continue?".to_string(),
805            options: Some(vec!["Yes".to_string(), "No".to_string()]),
806            tool_call_id: Some("tool-1".to_string()),
807            tool_name: Some("conclusion_with_options".to_string()),
808            allow_custom: false,
809        };
810
811        let value = serde_json::to_value(event).expect("event should serialize");
812        assert_eq!(value["type"], "need_clarification");
813        assert_eq!(value["question"], "Continue?");
814        assert_eq!(value["options"], serde_json::json!(["Yes", "No"]));
815        assert_eq!(value["tool_call_id"], "tool-1");
816        assert_eq!(value["tool_name"], "conclusion_with_options");
817        assert_eq!(value["allow_custom"], false);
818    }
819
820    #[test]
821    fn need_clarification_deserializes_from_old_format_without_new_fields() {
822        let json = serde_json::json!({
823            "type": "need_clarification",
824            "question": "Continue?",
825            "options": ["Yes", "No"]
826        });
827
828        let event: AgentEvent =
829            serde_json::from_value(json).expect("should deserialize old format");
830        match event {
831            AgentEvent::NeedClarification {
832                question,
833                options,
834                tool_call_id,
835                tool_name,
836                allow_custom,
837            } => {
838                assert_eq!(question, "Continue?");
839                assert_eq!(options, Some(vec!["Yes".to_string(), "No".to_string()]));
840                assert_eq!(tool_call_id, None);
841                assert_eq!(tool_name, None);
842                assert!(allow_custom); // default_allow_custom returns true
843            }
844            other => panic!("unexpected event: {other:?}"),
845        }
846    }
847
848    #[test]
849    fn need_clarification_deserializes_with_allow_custom_false() {
850        let json = serde_json::json!({
851            "type": "need_clarification",
852            "question": "Pick one",
853            "allow_custom": false
854        });
855
856        let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
857        match event {
858            AgentEvent::NeedClarification {
859                question,
860                options,
861                tool_call_id,
862                tool_name,
863                allow_custom,
864            } => {
865                assert_eq!(question, "Pick one");
866                assert_eq!(options, None);
867                assert_eq!(tool_call_id, None);
868                assert_eq!(tool_name, None);
869                assert!(!allow_custom);
870            }
871            other => panic!("unexpected event: {other:?}"),
872        }
873    }
874
875    #[test]
876    fn plan_mode_entered_serializes_correctly() {
877        let entered_at = Utc::now();
878        let event = AgentEvent::PlanModeEntered {
879            session_id: "sess-1".to_string(),
880            reason: Some("Complex refactor".to_string()),
881            pre_permission_mode: "default".to_string(),
882            entered_at,
883            status: bamboo_domain::PlanModeStatus::Exploring,
884            plan_file_path: None,
885        };
886
887        let value = serde_json::to_value(event).expect("event should serialize");
888        assert_eq!(value["type"], "plan_mode_entered");
889        assert_eq!(value["session_id"], "sess-1");
890        assert_eq!(value["reason"], "Complex refactor");
891        assert_eq!(value["pre_permission_mode"], "default");
892        assert_eq!(value["status"], "exploring");
893        // Compare against serde's own serialization (RFC3339 with `Z` for UTC),
894        // not `to_rfc3339()` which emits a `+00:00` offset instead.
895        assert_eq!(value["entered_at"], serde_json::to_value(entered_at).unwrap());
896    }
897
898    #[test]
899    fn plan_mode_exited_serializes_correctly() {
900        let event = AgentEvent::PlanModeExited {
901            session_id: "sess-1".to_string(),
902            approved: true,
903            restored_mode: "accept_edits".to_string(),
904            plan: Some("# Plan\n1. Step one".to_string()),
905        };
906
907        let value = serde_json::to_value(event).expect("event should serialize");
908        assert_eq!(value["type"], "plan_mode_exited");
909        assert_eq!(value["session_id"], "sess-1");
910        assert_eq!(value["approved"], true);
911        assert_eq!(value["restored_mode"], "accept_edits");
912        assert_eq!(value["plan"], "# Plan\n1. Step one");
913    }
914
915    #[test]
916    fn plan_file_updated_serializes_correctly() {
917        let event = AgentEvent::PlanFileUpdated {
918            session_id: "sess-1".to_string(),
919            file_path: "/tmp/plans/sess-1.md".to_string(),
920            content_summary: "Implementation plan for feature X".to_string(),
921        };
922
923        let value = serde_json::to_value(event).expect("event should serialize");
924        assert_eq!(value["type"], "plan_file_updated");
925        assert_eq!(value["session_id"], "sess-1");
926        assert_eq!(value["file_path"], "/tmp/plans/sess-1.md");
927        assert_eq!(
928            value["content_summary"],
929            "Implementation plan for feature X"
930        );
931    }
932
933    #[test]
934    fn tool_approval_requested_serializes_correctly() {
935        let event = AgentEvent::ToolApprovalRequested {
936            tool_call_id: "call-abc".to_string(),
937            tool_name: "Write".to_string(),
938            parameters: serde_json::json!({"file_path": "/tmp/test.txt"}),
939        };
940
941        let value = serde_json::to_value(event).expect("event should serialize");
942        assert_eq!(value["type"], "tool_approval_requested");
943        assert_eq!(value["tool_call_id"], "call-abc");
944        assert_eq!(value["tool_name"], "Write");
945        assert_eq!(
946            value["parameters"],
947            serde_json::json!({"file_path": "/tmp/test.txt"})
948        );
949    }
950
951    #[test]
952    fn tool_approval_requested_deserializes_correctly() {
953        let json = serde_json::json!({
954            "type": "tool_approval_requested",
955            "tool_call_id": "call-xyz",
956            "tool_name": "Bash",
957            "parameters": {"command": "ls -la"}
958        });
959
960        let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
961        match event {
962            AgentEvent::ToolApprovalRequested {
963                tool_call_id,
964                tool_name,
965                parameters,
966            } => {
967                assert_eq!(tool_call_id, "call-xyz");
968                assert_eq!(tool_name, "Bash");
969                assert_eq!(parameters, serde_json::json!({"command": "ls -la"}));
970            }
971            other => panic!("unexpected event: {other:?}"),
972        }
973    }
974
975    #[test]
976    fn session_title_updated_round_trips_with_source_variants() {
977        use chrono::Utc;
978        let event = AgentEvent::SessionTitleUpdated {
979            session_id: "sess-1".to_string(),
980            title: "My title".to_string(),
981            title_version: 3,
982            source: TitleSource::Auto,
983            updated_at: Utc::now(),
984        };
985        let json = serde_json::to_string(&event).unwrap();
986        assert!(
987            json.contains("\"type\":\"session_title_updated\""),
988            "json: {json}"
989        );
990        assert!(json.contains("\"source\":\"auto\""), "json: {json}");
991        let _decoded: AgentEvent = serde_json::from_str(&json).unwrap();
992    }
993
994    #[test]
995    fn plan_mode_events_deserialize_without_optional_fields() {
996        let json = serde_json::json!({
997            "type": "plan_mode_entered",
998            "session_id": "sess-1",
999            "pre_permission_mode": "default",
1000            "entered_at": "2025-01-01T00:00:00Z",
1001            "status": "exploring"
1002        });
1003
1004        let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
1005        match event {
1006            AgentEvent::PlanModeEntered {
1007                session_id,
1008                reason,
1009                pre_permission_mode,
1010                entered_at,
1011                status,
1012                plan_file_path,
1013            } => {
1014                assert_eq!(session_id, "sess-1");
1015                assert_eq!(reason, None);
1016                assert_eq!(pre_permission_mode, "default");
1017                assert_eq!(entered_at.to_rfc3339(), "2025-01-01T00:00:00+00:00");
1018                assert_eq!(status, bamboo_domain::PlanModeStatus::Exploring);
1019                assert_eq!(plan_file_path, None);
1020            }
1021            other => panic!("unexpected event: {other:?}"),
1022        }
1023    }
1024}