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 whenever the runtime goal state changes — a new status
271 /// (active/complete/blocked/…), an incremented continuation count, or a
272 /// freshly recorded side-channel double-check verdict. Lets the UI reflect
273 /// live goal progress without re-fetching history. Ephemeral: it rides only
274 /// the per-session `/events/{id}` stream; reconnecting clients read the
275 /// authoritative `goal_state` from the history endpoint instead.
276 GoalStatusChanged {
277 /// Session identifier
278 session_id: String,
279 /// Full serialized goal state — identical shape to the history
280 /// response's `goal_state` field (see `bamboo_engine::runtime::goal_state`).
281 goal_state: serde_json::Value,
282 },
283
284 /// Emitted when token budget is prepared (after context truncation)
285 TokenBudgetUpdated {
286 /// Token budget details
287 usage: TokenBudgetUsage,
288 },
289
290 /// Emitted when host-side context compression lifecycle changes.
291 ContextCompressionStatus {
292 /// Compression phase label (for example: pre-turn, mid-turn).
293 phase: String,
294 /// Compression status: started | completed | failed | skipped
295 status: String,
296 },
297
298 /// Emitted when conversation context is summarized
299 ContextSummarized {
300 /// Generated summary text
301 summary: String,
302 /// Number of old messages summarized
303 messages_summarized: usize,
304 /// Tokens saved by summarization
305 tokens_saved: u32,
306 /// Context usage percentage before compression
307 #[serde(default)]
308 usage_before_percent: f64,
309 /// Context usage percentage after compression
310 #[serde(default)]
311 usage_after_percent: f64,
312 /// What triggered the compression: "auto" | "manual" | "critical"
313 #[serde(default)]
314 trigger_type: String,
315 },
316
317 /// Emitted when context pressure reaches warning or critical levels.
318 /// Frontend should display this to the user as a proactive notification.
319 ContextPressureNotification {
320 /// Context usage as a percentage of the context window.
321 percent: f64,
322 /// Severity level: "warning" (70%) or "critical" (90%).
323 level: String,
324 /// Human-readable message describing the pressure state.
325 message: String,
326 },
327
328 /// A child session was spawned from a parent session (async background job).
329 SubAgentStarted {
330 parent_session_id: String,
331 child_session_id: String,
332 /// Optional title (useful for UI lists).
333 #[serde(default, skip_serializing_if = "Option::is_none")]
334 title: Option<String>,
335 },
336
337 /// Forwarded raw child event to the parent session stream.
338 ///
339 /// Child sessions are not allowed to spawn further sessions, so this should not nest.
340 SubAgentEvent {
341 parent_session_id: String,
342 child_session_id: String,
343 event: Box<AgentEvent>,
344 },
345
346 /// Heartbeat emitted while a child session is running.
347 SubAgentHeartbeat {
348 parent_session_id: String,
349 child_session_id: String,
350 timestamp: DateTime<Utc>,
351 },
352
353 /// Child session finished (completed/cancelled/error).
354 SubAgentCompleted {
355 parent_session_id: String,
356 child_session_id: String,
357 /// One of: "completed" | "cancelled" | "error" | "skipped"
358 status: String,
359 #[serde(default, skip_serializing_if = "Option::is_none")]
360 error: Option<String>,
361 },
362
363 /// Plan mode was entered.
364 PlanModeEntered {
365 /// Session identifier
366 session_id: String,
367 /// Optional reason for entering plan mode
368 #[serde(default, skip_serializing_if = "Option::is_none")]
369 reason: Option<String>,
370 /// Previous permission mode before entering plan mode
371 pre_permission_mode: String,
372 /// RFC3339 timestamp when plan mode was entered.
373 entered_at: chrono::DateTime<chrono::Utc>,
374 /// Current plan mode phase/status.
375 status: bamboo_domain::PlanModeStatus,
376 /// Path to the persisted plan file, if already available.
377 #[serde(default, skip_serializing_if = "Option::is_none")]
378 plan_file_path: Option<String>,
379 },
380
381 /// Plan mode was exited.
382 PlanModeExited {
383 /// Session identifier
384 session_id: String,
385 /// Whether the exit was approved by the user
386 approved: bool,
387 /// The permission mode restored after exiting
388 restored_mode: String,
389 /// Plan content that was reviewed, if any
390 #[serde(default, skip_serializing_if = "Option::is_none")]
391 plan: Option<String>,
392 },
393
394 /// Plan file was updated.
395 PlanFileUpdated {
396 /// Session identifier
397 session_id: String,
398 /// Path to the plan file
399 file_path: String,
400 /// Summary of the plan content (truncated)
401 content_summary: String,
402 },
403
404 /// Runner progress update emitted at the start of each agent turn.
405 ///
406 /// Used to track live execution progress (round count, current activity)
407 /// for diagnostic visibility, especially for child sessions.
408 RunnerProgress {
409 /// Session identifier
410 session_id: String,
411 /// Current turn/round count
412 round_count: u32,
413 },
414
415 /// Session title was updated (auto-generated by backend or manually renamed via PATCH).
416 SessionTitleUpdated {
417 session_id: String,
418 title: String,
419 title_version: u64,
420 source: TitleSource,
421 updated_at: chrono::DateTime<chrono::Utc>,
422 },
423
424 /// Session pinned flag was toggled via PATCH.
425 ///
426 /// Replayable metadata event. `pinned` is an idempotent boolean so the
427 /// latest event wins; `updated_at` is used by the frontend to suppress
428 /// stale replays.
429 SessionPinnedUpdated {
430 session_id: String,
431 pinned: bool,
432 updated_at: chrono::DateTime<chrono::Utc>,
433 },
434
435 /// A new session was created.
436 ///
437 /// Change-feed event: durable, journaled, carried on the account `/stream`
438 /// feed so other clients can insert the session into their list without a
439 /// full `GET /sessions` poll.
440 SessionCreated {
441 session_id: String,
442 title: String,
443 kind: bamboo_domain::SessionKind,
444 created_at: chrono::DateTime<chrono::Utc>,
445 },
446
447 /// A session was deleted.
448 ///
449 /// Change-feed event: durable, journaled. Clients remove the session from
450 /// their local list on receipt.
451 SessionDeleted { session_id: String },
452
453 /// A session's message history was cleared (session kept).
454 ///
455 /// Change-feed event: durable, journaled. Clients drop cached messages for
456 /// the session and refetch lazily.
457 SessionCleared { session_id: String },
458
459 /// A message was appended to a session.
460 ///
461 /// Change-feed event: durable, journaled. The `seq` assigned to this event
462 /// on the account feed is the message's feed coordinate (used by
463 /// `GET /history/{id}?since={seq}` to compute deltas). `content` is the
464 /// plain-text body matching what `/history` returns to the UI.
465 MessageAppended {
466 session_id: String,
467 message_id: String,
468 role: bamboo_domain::Role,
469 content: String,
470 created_at: chrono::DateTime<chrono::Utc>,
471 },
472
473 /// Execution run has started and the runner is now active.
474 ///
475 /// Emitted as the first event after a runner reservation succeeds,
476 /// before any token or tool events. Carries the `run_id` so the
477 /// frontend can correlate subsequent SSE events across reconnects.
478 ExecutionStarted {
479 /// Unique identifier for this execution run.
480 run_id: String,
481 /// Session identifier.
482 session_id: String,
483 /// ISO 8601 timestamp when the run started.
484 started_at: String,
485 },
486
487 /// Tool execution requires user approval before proceeding.
488 ///
489 /// Emitted when a permission checker determines that a tool call needs
490 /// explicit user confirmation (e.g., mutating operations in restricted
491 /// permission mode). The frontend should present the approval request and
492 /// either grant or deny it.
493 ToolApprovalRequested {
494 /// Unique identifier for the tool call awaiting approval.
495 tool_call_id: String,
496 /// Name of the tool being executed.
497 tool_name: String,
498 /// Parameters that were passed to the tool.
499 parameters: serde_json::Value,
500 },
501
502 /// A child sub-agent (out-of-process worker) hit a gated tool and proxied
503 /// the approval decision to this parent over the actor protocol (Phase 2).
504 /// The parent surfaces it to the human; the decision is routed back to the
505 /// waiting child via
506 /// `external_agents::live::deliver_approval(child_session_id, request_id, approved)`.
507 ChildApprovalRequested {
508 /// The child session whose gated tool is blocked awaiting approval.
509 child_session_id: String,
510 /// Correlates the eventual approve/deny reply back to the blocked tool.
511 request_id: String,
512 /// Name of the gated tool the child wants to run.
513 tool_name: String,
514 /// Human-readable description of the permission requested.
515 permission: String,
516 /// The concrete resource the action targets.
517 resource: String,
518 },
519
520 /// Agent execution completed successfully.
521 Complete {
522 /// Final token usage statistics
523 usage: TokenUsage,
524 },
525
526 /// Agent execution was cancelled.
527 Cancelled {
528 /// Optional human-readable message explaining the cancellation.
529 #[serde(default, skip_serializing_if = "Option::is_none")]
530 message: Option<String>,
531 },
532
533 /// Agent execution failed.
534 Error {
535 /// Error message
536 message: String,
537 },
538
539 /// A user-facing notification derived from agent activity by the backend
540 /// notification policy. Clients render this (e.g. an OS desktop notification)
541 /// after applying their own presence checks (window focus). The decision of
542 /// *whether* to notify — category, priority, preference gating, dedup — is
543 /// made server-side in `bamboo-notification`; the client just delivers it.
544 Notification {
545 /// Unique id (for client-side dedup / dismissal).
546 id: String,
547 /// Session this notification is about.
548 session_id: String,
549 /// Category, e.g. `needs_approval` | `needs_clarification` | `run_completed`
550 /// | `run_failed` | `subagent_completed` | `context_critical`.
551 category: String,
552 /// Priority: `high` | `normal` | `low`.
553 priority: String,
554 /// Short title line.
555 title: String,
556 /// Body text.
557 body: String,
558 /// Stable key for client-side coalescing within a short window.
559 #[serde(default, skip_serializing_if = "Option::is_none")]
560 dedup_key: Option<String>,
561 /// RFC3339 creation timestamp.
562 created_at: String,
563 },
564}
565
566impl AgentEvent {
567 /// Returns the session this event pertains to, when it carries one.
568 ///
569 /// Used by the account change-feed to route each event to the right
570 /// client-side session without a per-session connection. For sub-agent
571 /// events the *parent* session id is returned (that is the session a client
572 /// observes in its list). Pure streaming/diagnostic variants (`Token`,
573 /// `Complete`, …) return `None`; those are ephemeral and never ride the
574 /// account feed anyway.
575 pub fn session_id(&self) -> Option<&str> {
576 match self {
577 AgentEvent::TaskListUpdated { task_list } => Some(task_list.session_id.as_str()),
578 AgentEvent::TaskListItemProgress { session_id, .. }
579 | AgentEvent::TaskListCompleted { session_id, .. }
580 | AgentEvent::TaskEvaluationStarted { session_id, .. }
581 | AgentEvent::TaskEvaluationCompleted { session_id, .. }
582 | AgentEvent::GoldEvaluationStarted { session_id, .. }
583 | AgentEvent::GoldEvaluationCompleted { session_id, .. }
584 | AgentEvent::GoalStatusChanged { session_id, .. }
585 | AgentEvent::PlanModeEntered { session_id, .. }
586 | AgentEvent::PlanModeExited { session_id, .. }
587 | AgentEvent::PlanFileUpdated { session_id, .. }
588 | AgentEvent::RunnerProgress { session_id, .. }
589 | AgentEvent::SessionTitleUpdated { session_id, .. }
590 | AgentEvent::SessionPinnedUpdated { session_id, .. }
591 | AgentEvent::SessionCreated { session_id, .. }
592 | AgentEvent::SessionDeleted { session_id, .. }
593 | AgentEvent::SessionCleared { session_id, .. }
594 | AgentEvent::MessageAppended { session_id, .. }
595 | AgentEvent::ExecutionStarted { session_id, .. }
596 | AgentEvent::Notification { session_id, .. } => Some(session_id.as_str()),
597 AgentEvent::SubAgentStarted {
598 parent_session_id, ..
599 }
600 | AgentEvent::SubAgentEvent {
601 parent_session_id, ..
602 }
603 | AgentEvent::SubAgentHeartbeat {
604 parent_session_id, ..
605 }
606 | AgentEvent::SubAgentCompleted {
607 parent_session_id, ..
608 } => Some(parent_session_id.as_str()),
609 _ => None,
610 }
611 }
612
613 /// Whether this event belongs on the durable account change feed.
614 ///
615 /// Durable change events are low-volume, journaled to disk, and resumable
616 /// via the account `/stream` feed. Ephemeral events — token-by-token
617 /// streaming (`Token`/`ReasoningToken`/`ToolToken`), heartbeats, live
618 /// budget/pressure gauges, and raw forwarded sub-agent events — return
619 /// `false`: they stay exclusively on the per-session `/events/{id}` stream.
620 /// Keeping them off the journal and the multiplexed feed is the core
621 /// data-transfer win. This method lives in core so both the server and the
622 /// engine forwarder can filter before cloning onto the feed.
623 pub fn is_durable_change(&self) -> bool {
624 matches!(
625 self,
626 AgentEvent::MessageAppended { .. }
627 | AgentEvent::SessionCreated { .. }
628 | AgentEvent::SessionDeleted { .. }
629 | AgentEvent::SessionCleared { .. }
630 | AgentEvent::SessionTitleUpdated { .. }
631 | AgentEvent::SessionPinnedUpdated { .. }
632 | AgentEvent::TaskListUpdated { .. }
633 | AgentEvent::TaskListItemProgress { .. }
634 | AgentEvent::TaskListCompleted { .. }
635 | AgentEvent::TaskEvaluationCompleted { .. }
636 | AgentEvent::PlanModeEntered { .. }
637 | AgentEvent::PlanModeExited { .. }
638 | AgentEvent::PlanFileUpdated { .. }
639 | AgentEvent::SubAgentStarted { .. }
640 | AgentEvent::SubAgentCompleted { .. }
641 | AgentEvent::NeedClarification { .. }
642 | AgentEvent::ToolApprovalRequested { .. }
643 | AgentEvent::ExecutionStarted { .. }
644 | AgentEvent::Complete { .. }
645 | AgentEvent::Cancelled { .. }
646 | AgentEvent::Error { .. }
647 )
648 }
649}
650
651fn default_allow_custom() -> bool {
652 true
653}
654
655/// Gold evaluation checkpoint.
656#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
657#[serde(rename_all = "snake_case")]
658pub enum GoldCheckpoint {
659 PostRound,
660 Terminal,
661}
662
663impl GoldCheckpoint {
664 pub fn as_str(self) -> &'static str {
665 match self {
666 Self::PostRound => "post_round",
667 Self::Terminal => "terminal",
668 }
669 }
670}
671
672/// Gold evaluator decision.
673#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
674#[serde(rename_all = "snake_case")]
675pub enum GoldDecision {
676 Continue,
677 Achieved,
678 Blocked,
679 NeedInput,
680 Exhausted,
681}
682
683impl GoldDecision {
684 pub fn as_str(self) -> &'static str {
685 match self {
686 Self::Continue => "continue",
687 Self::Achieved => "achieved",
688 Self::Blocked => "blocked",
689 Self::NeedInput => "need_input",
690 Self::Exhausted => "exhausted",
691 }
692 }
693}
694
695/// Confidence level for a Gold evaluation result.
696#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
697#[serde(rename_all = "snake_case")]
698pub enum GoldConfidence {
699 Low,
700 Medium,
701 High,
702}
703
704impl GoldConfidence {
705 pub fn as_str(self) -> &'static str {
706 match self {
707 Self::Low => "low",
708 Self::Medium => "medium",
709 Self::High => "high",
710 }
711 }
712
713 /// Ordinal rank for threshold comparisons (`Low` < `Medium` < `High`).
714 pub fn rank(self) -> u8 {
715 match self {
716 Self::Low => 0,
717 Self::Medium => 1,
718 Self::High => 2,
719 }
720 }
721
722 /// Whether this confidence meets or exceeds the given floor.
723 pub fn meets(self, floor: GoldConfidence) -> bool {
724 self.rank() >= floor.rank()
725 }
726}
727
728/// Source that triggered a session title update.
729#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
730#[serde(rename_all = "snake_case")]
731pub enum TitleSource {
732 Auto,
733 Manual,
734 Fallback,
735}
736
737/// Re-exported shared token usage type.
738///
739/// See [`bamboo_domain::TokenUsage`] for the canonical definition.
740pub use bamboo_domain::TokenUsage;
741
742pub use bamboo_domain::budget_types::TokenBudgetUsage;
743
744#[cfg(test)]
745mod tests {
746 use super::*;
747 use bamboo_domain::{TaskItem, TaskItemStatus, TaskList};
748
749 fn sample_task_list() -> TaskList {
750 TaskList {
751 session_id: "session-1".to_string(),
752 title: "Task List".to_string(),
753 items: vec![TaskItem {
754 id: "task_1".to_string(),
755 description: "Implement event rename".to_string(),
756 status: TaskItemStatus::InProgress,
757 depends_on: Vec::new(),
758 notes: "Implementing".to_string(),
759 ..TaskItem::default()
760 }],
761 created_at: Utc::now(),
762 updated_at: Utc::now(),
763 }
764 }
765
766 #[test]
767 fn task_list_updated_serializes_with_task_names() {
768 let event = AgentEvent::TaskListUpdated {
769 task_list: sample_task_list(),
770 };
771
772 let value = serde_json::to_value(event).expect("event should serialize");
773 assert_eq!(value["type"], "task_list_updated");
774 assert!(value.get("task_list").is_some());
775 assert!(value.get("todo_list").is_none());
776 }
777
778 #[test]
779 fn cancelled_serializes_with_snake_case_type() {
780 let event = AgentEvent::Cancelled {
781 message: Some("Agent execution cancelled by user".to_string()),
782 };
783
784 let value = serde_json::to_value(event).expect("event should serialize");
785 assert_eq!(value["type"], "cancelled");
786 assert_eq!(
787 value["message"],
788 serde_json::Value::String("Agent execution cancelled by user".to_string())
789 );
790 }
791
792 #[test]
793 fn task_evaluation_completed_serializes_with_task_type() {
794 let event = AgentEvent::TaskEvaluationCompleted {
795 session_id: "session-1".to_string(),
796 updates_count: 2,
797 reasoning: "Updated statuses".to_string(),
798 };
799
800 let value = serde_json::to_value(event).expect("event should serialize");
801 assert_eq!(value["type"], "task_evaluation_completed");
802 }
803
804 #[test]
805 fn gold_evaluation_completed_serializes_with_gold_type_and_fields() {
806 let event = AgentEvent::GoldEvaluationCompleted {
807 session_id: "session-1".to_string(),
808 checkpoint: GoldCheckpoint::PostRound,
809 iteration: 3,
810 decision: GoldDecision::Continue,
811 confidence: GoldConfidence::Medium,
812 reasoning: "Need one more iteration".to_string(),
813 };
814
815 let value = serde_json::to_value(event).expect("event should serialize");
816 assert_eq!(value["type"], "gold_evaluation_completed");
817 assert_eq!(value["checkpoint"], "post_round");
818 assert_eq!(value["iteration"], 3);
819 assert_eq!(value["decision"], "continue");
820 assert_eq!(value["confidence"], "medium");
821 assert_eq!(value["reasoning"], "Need one more iteration");
822 }
823
824 #[test]
825 fn gold_evaluation_started_deserializes() {
826 let json = serde_json::json!({
827 "type": "gold_evaluation_started",
828 "session_id": "session-1",
829 "checkpoint": "terminal",
830 "iteration": 7
831 });
832
833 let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
834 match event {
835 AgentEvent::GoldEvaluationStarted {
836 session_id,
837 checkpoint,
838 iteration,
839 } => {
840 assert_eq!(session_id, "session-1");
841 assert_eq!(checkpoint, GoldCheckpoint::Terminal);
842 assert_eq!(iteration, 7);
843 }
844 other => panic!("unexpected event: {other:?}"),
845 }
846 }
847
848 #[test]
849 fn context_compression_status_serializes_with_phase_and_status() {
850 let event = AgentEvent::ContextCompressionStatus {
851 phase: "mid-turn".to_string(),
852 status: "started".to_string(),
853 };
854
855 let value = serde_json::to_value(event).expect("event should serialize");
856 assert_eq!(value["type"], "context_compression_status");
857 assert_eq!(value["phase"], "mid-turn");
858 assert_eq!(value["status"], "started");
859 }
860
861 #[test]
862 fn need_clarification_serializes_with_new_fields() {
863 let event = AgentEvent::NeedClarification {
864 question: "Continue?".to_string(),
865 options: Some(vec!["Yes".to_string(), "No".to_string()]),
866 tool_call_id: Some("tool-1".to_string()),
867 tool_name: Some("conclusion_with_options".to_string()),
868 allow_custom: false,
869 };
870
871 let value = serde_json::to_value(event).expect("event should serialize");
872 assert_eq!(value["type"], "need_clarification");
873 assert_eq!(value["question"], "Continue?");
874 assert_eq!(value["options"], serde_json::json!(["Yes", "No"]));
875 assert_eq!(value["tool_call_id"], "tool-1");
876 assert_eq!(value["tool_name"], "conclusion_with_options");
877 assert_eq!(value["allow_custom"], false);
878 }
879
880 #[test]
881 fn need_clarification_deserializes_from_old_format_without_new_fields() {
882 let json = serde_json::json!({
883 "type": "need_clarification",
884 "question": "Continue?",
885 "options": ["Yes", "No"]
886 });
887
888 let event: AgentEvent =
889 serde_json::from_value(json).expect("should deserialize old format");
890 match event {
891 AgentEvent::NeedClarification {
892 question,
893 options,
894 tool_call_id,
895 tool_name,
896 allow_custom,
897 } => {
898 assert_eq!(question, "Continue?");
899 assert_eq!(options, Some(vec!["Yes".to_string(), "No".to_string()]));
900 assert_eq!(tool_call_id, None);
901 assert_eq!(tool_name, None);
902 assert!(allow_custom); // default_allow_custom returns true
903 }
904 other => panic!("unexpected event: {other:?}"),
905 }
906 }
907
908 #[test]
909 fn need_clarification_deserializes_with_allow_custom_false() {
910 let json = serde_json::json!({
911 "type": "need_clarification",
912 "question": "Pick one",
913 "allow_custom": false
914 });
915
916 let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
917 match event {
918 AgentEvent::NeedClarification {
919 question,
920 options,
921 tool_call_id,
922 tool_name,
923 allow_custom,
924 } => {
925 assert_eq!(question, "Pick one");
926 assert_eq!(options, None);
927 assert_eq!(tool_call_id, None);
928 assert_eq!(tool_name, None);
929 assert!(!allow_custom);
930 }
931 other => panic!("unexpected event: {other:?}"),
932 }
933 }
934
935 #[test]
936 fn plan_mode_entered_serializes_correctly() {
937 let entered_at = Utc::now();
938 let event = AgentEvent::PlanModeEntered {
939 session_id: "sess-1".to_string(),
940 reason: Some("Complex refactor".to_string()),
941 pre_permission_mode: "default".to_string(),
942 entered_at,
943 status: bamboo_domain::PlanModeStatus::Exploring,
944 plan_file_path: None,
945 };
946
947 let value = serde_json::to_value(event).expect("event should serialize");
948 assert_eq!(value["type"], "plan_mode_entered");
949 assert_eq!(value["session_id"], "sess-1");
950 assert_eq!(value["reason"], "Complex refactor");
951 assert_eq!(value["pre_permission_mode"], "default");
952 assert_eq!(value["status"], "exploring");
953 // Compare against serde's own serialization (RFC3339 with `Z` for UTC),
954 // not `to_rfc3339()` which emits a `+00:00` offset instead.
955 assert_eq!(
956 value["entered_at"],
957 serde_json::to_value(entered_at).unwrap()
958 );
959 }
960
961 #[test]
962 fn plan_mode_exited_serializes_correctly() {
963 let event = AgentEvent::PlanModeExited {
964 session_id: "sess-1".to_string(),
965 approved: true,
966 restored_mode: "accept_edits".to_string(),
967 plan: Some("# Plan\n1. Step one".to_string()),
968 };
969
970 let value = serde_json::to_value(event).expect("event should serialize");
971 assert_eq!(value["type"], "plan_mode_exited");
972 assert_eq!(value["session_id"], "sess-1");
973 assert_eq!(value["approved"], true);
974 assert_eq!(value["restored_mode"], "accept_edits");
975 assert_eq!(value["plan"], "# Plan\n1. Step one");
976 }
977
978 #[test]
979 fn plan_file_updated_serializes_correctly() {
980 let event = AgentEvent::PlanFileUpdated {
981 session_id: "sess-1".to_string(),
982 file_path: "/tmp/plans/sess-1.md".to_string(),
983 content_summary: "Implementation plan for feature X".to_string(),
984 };
985
986 let value = serde_json::to_value(event).expect("event should serialize");
987 assert_eq!(value["type"], "plan_file_updated");
988 assert_eq!(value["session_id"], "sess-1");
989 assert_eq!(value["file_path"], "/tmp/plans/sess-1.md");
990 assert_eq!(
991 value["content_summary"],
992 "Implementation plan for feature X"
993 );
994 }
995
996 #[test]
997 fn tool_approval_requested_serializes_correctly() {
998 let event = AgentEvent::ToolApprovalRequested {
999 tool_call_id: "call-abc".to_string(),
1000 tool_name: "Write".to_string(),
1001 parameters: serde_json::json!({"file_path": "/tmp/test.txt"}),
1002 };
1003
1004 let value = serde_json::to_value(event).expect("event should serialize");
1005 assert_eq!(value["type"], "tool_approval_requested");
1006 assert_eq!(value["tool_call_id"], "call-abc");
1007 assert_eq!(value["tool_name"], "Write");
1008 assert_eq!(
1009 value["parameters"],
1010 serde_json::json!({"file_path": "/tmp/test.txt"})
1011 );
1012 }
1013
1014 #[test]
1015 fn tool_approval_requested_deserializes_correctly() {
1016 let json = serde_json::json!({
1017 "type": "tool_approval_requested",
1018 "tool_call_id": "call-xyz",
1019 "tool_name": "Bash",
1020 "parameters": {"command": "ls -la"}
1021 });
1022
1023 let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
1024 match event {
1025 AgentEvent::ToolApprovalRequested {
1026 tool_call_id,
1027 tool_name,
1028 parameters,
1029 } => {
1030 assert_eq!(tool_call_id, "call-xyz");
1031 assert_eq!(tool_name, "Bash");
1032 assert_eq!(parameters, serde_json::json!({"command": "ls -la"}));
1033 }
1034 other => panic!("unexpected event: {other:?}"),
1035 }
1036 }
1037
1038 #[test]
1039 fn session_title_updated_round_trips_with_source_variants() {
1040 use chrono::Utc;
1041 let event = AgentEvent::SessionTitleUpdated {
1042 session_id: "sess-1".to_string(),
1043 title: "My title".to_string(),
1044 title_version: 3,
1045 source: TitleSource::Auto,
1046 updated_at: Utc::now(),
1047 };
1048 let json = serde_json::to_string(&event).unwrap();
1049 assert!(
1050 json.contains("\"type\":\"session_title_updated\""),
1051 "json: {json}"
1052 );
1053 assert!(json.contains("\"source\":\"auto\""), "json: {json}");
1054 let _decoded: AgentEvent = serde_json::from_str(&json).unwrap();
1055 }
1056
1057 #[test]
1058 fn plan_mode_events_deserialize_without_optional_fields() {
1059 let json = serde_json::json!({
1060 "type": "plan_mode_entered",
1061 "session_id": "sess-1",
1062 "pre_permission_mode": "default",
1063 "entered_at": "2025-01-01T00:00:00Z",
1064 "status": "exploring"
1065 });
1066
1067 let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
1068 match event {
1069 AgentEvent::PlanModeEntered {
1070 session_id,
1071 reason,
1072 pre_permission_mode,
1073 entered_at,
1074 status,
1075 plan_file_path,
1076 } => {
1077 assert_eq!(session_id, "sess-1");
1078 assert_eq!(reason, None);
1079 assert_eq!(pre_permission_mode, "default");
1080 assert_eq!(entered_at.to_rfc3339(), "2025-01-01T00:00:00+00:00");
1081 assert_eq!(status, bamboo_domain::PlanModeStatus::Exploring);
1082 assert_eq!(plan_file_path, None);
1083 }
1084 other => panic!("unexpected event: {other:?}"),
1085 }
1086 }
1087}