1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//! Interaction-scoped event tap for streaming events to per-interaction subscribers.
//!
//! The `EventTap` is a shared mutex checked by both the `LlmClientAdapter` (for TextDelta)
//! and the agent's `run_loop` (for lifecycle events). The host loop installs/clears a
//! subscriber per interaction.
use crate::event::AgentEvent;
#[cfg(target_arch = "wasm32")]
use crate::tokio;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::mpsc;
/// State held inside an active event tap subscriber.
pub struct EventTapState {
/// Channel sender for streaming events to the subscriber.
pub tx: mpsc::Sender<AgentEvent>,
/// Whether any events were dropped due to channel backpressure.
pub truncated: AtomicBool,
}
/// A shared, optionally-active event tap.
///
/// When `Some(EventTapState)`, events are forwarded to the subscriber.
/// When `None`, no subscriber is active and tap operations are no-ops.
pub type EventTap = Arc<parking_lot::Mutex<Option<EventTapState>>>;
/// Create a new event tap with no active subscriber.
pub fn new_event_tap() -> EventTap {
Arc::new(parking_lot::Mutex::new(None))
}
/// Best-effort send to the tap for streaming events (TextDelta, lifecycle).
///
/// Takes a reference to avoid unconditional cloning — the event is only cloned
/// when a subscriber is active and the channel has capacity.
///
/// On `TrySendError::Full`: sets `truncated = true`, attempts a one-time
/// `StreamTruncated` marker via direct `tx.try_send(...)`.
/// On `TrySendError::Closed`: no-op.
///
/// This is synchronous (no await) — safe to call from both sync and async contexts.
pub fn tap_try_send(tap: &EventTap, event: &AgentEvent) {
let guard = tap.lock();
let Some(state) = guard.as_ref() else {
return;
};
match state.tx.try_send(event.clone()) {
Ok(()) | Err(mpsc::error::TrySendError::Closed(_)) => {
// Ok: delivered. Closed: subscriber dropped — no-op.
}
Err(mpsc::error::TrySendError::Full(_)) => {
if !state.truncated.swap(true, Ordering::Relaxed) {
// First truncation — attempt one-time marker (also best-effort)
let _ = state.tx.try_send(AgentEvent::StreamTruncated {
reason: "channel full".to_string(),
});
}
}
}
}
/// Send a terminal interaction event to the tap.
///
/// Clones the sender outside the lock, then uses `tokio::time::timeout(5s)`
/// to avoid stalling the host loop. On timeout: logs a warning and continues.
pub async fn tap_send_terminal(tap: &EventTap, event: AgentEvent) {
let tx = {
let guard = tap.lock();
match guard.as_ref() {
Some(state) => state.tx.clone(),
None => return,
}
};
match tokio::time::timeout(std::time::Duration::from_secs(5), tx.send(event)).await {
Ok(Ok(()) | Err(_)) => {
// Ok: delivered. Err: receiver dropped — no-op.
}
Err(_) => {
tracing::warn!("tap_send_terminal timed out after 5s; continuing");
}
}
}
/// Convenience: send event to tap (best-effort), then to primary channel.
///
/// The event is only cloned for the tap when a subscriber is active.
/// The owned event is moved into the primary channel send.
///
/// Returns `false` if the primary send failed (receiver dropped).
pub async fn tap_emit(
tap: &EventTap,
tx: Option<&mpsc::Sender<AgentEvent>>,
event: AgentEvent,
) -> bool {
tap_try_send(tap, &event);
if let Some(tx) = tx {
return tx.send(event).await.is_ok();
}
true
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn tap_try_send_none_is_noop() {
let tap = new_event_tap();
// Should not panic
tap_try_send(
&tap,
&AgentEvent::TextDelta {
delta: "hello".to_string(),
},
);
}
#[test]
fn tap_try_send_delivers_to_active_subscriber() {
let tap = new_event_tap();
let (tx, mut rx) = mpsc::channel(16);
{
let mut guard = tap.lock();
*guard = Some(EventTapState {
tx,
truncated: AtomicBool::new(false),
});
}
tap_try_send(
&tap,
&AgentEvent::TextDelta {
delta: "hello".to_string(),
},
);
let event = rx.try_recv().unwrap();
let AgentEvent::TextDelta { delta } = event else {
unreachable!("Expected TextDelta, got {:?}", event);
};
assert_eq!(delta, "hello");
}
#[test]
fn tap_try_send_full_channel_sets_truncated_and_sends_marker() {
let tap = new_event_tap();
// Channel with capacity 1
let (tx, mut rx) = mpsc::channel(1);
{
let mut guard = tap.lock();
*guard = Some(EventTapState {
tx,
truncated: AtomicBool::new(false),
});
}
// Fill the channel
tap_try_send(
&tap,
&AgentEvent::TextDelta {
delta: "first".to_string(),
},
);
// This should trigger truncation
tap_try_send(
&tap,
&AgentEvent::TextDelta {
delta: "second".to_string(),
},
);
// Verify truncated flag
{
let guard = tap.lock();
assert!(guard.as_ref().unwrap().truncated.load(Ordering::Relaxed));
}
// Drain and check: first event should be the TextDelta "first"
let first = rx.try_recv().unwrap();
assert!(
matches!(&first, AgentEvent::TextDelta { delta } if delta == "first"),
"Expected first TextDelta, got {first:?}"
);
}
#[tokio::test]
async fn tap_send_terminal_delivers_within_timeout() {
let tap = new_event_tap();
let (tx, mut rx) = mpsc::channel(16);
{
let mut guard = tap.lock();
*guard = Some(EventTapState {
tx,
truncated: AtomicBool::new(false),
});
}
tap_send_terminal(
&tap,
AgentEvent::RunCompleted {
session_id: crate::types::SessionId::new(),
result: "done".to_string(),
structured_output: None,
extraction_required: false,
usage: crate::types::Usage::default(),
terminal_cause_kind: None,
},
)
.await;
let event = rx.try_recv().unwrap();
assert!(matches!(event, AgentEvent::RunCompleted { .. }));
}
#[tokio::test]
async fn tap_send_terminal_no_subscriber_is_noop() {
let tap = new_event_tap();
// Should not panic or hang
tap_send_terminal(
&tap,
AgentEvent::RunCompleted {
session_id: crate::types::SessionId::new(),
result: "done".to_string(),
structured_output: None,
extraction_required: false,
usage: crate::types::Usage::default(),
terminal_cause_kind: None,
},
)
.await;
}
#[tokio::test]
async fn tap_emit_sends_to_both_tap_and_primary() {
let tap = new_event_tap();
let (tap_tx, mut tap_rx) = mpsc::channel(16);
let (primary_tx, mut primary_rx) = mpsc::channel(16);
{
let mut guard = tap.lock();
*guard = Some(EventTapState {
tx: tap_tx,
truncated: AtomicBool::new(false),
});
}
let ok = tap_emit(
&tap,
Some(&primary_tx),
AgentEvent::TextDelta {
delta: "both".to_string(),
},
)
.await;
assert!(ok);
// Both channels should have the event
let tap_event = tap_rx.try_recv().unwrap();
let primary_event = primary_rx.try_recv().unwrap();
assert!(matches!(tap_event, AgentEvent::TextDelta { delta } if delta == "both"));
assert!(matches!(primary_event, AgentEvent::TextDelta { delta } if delta == "both"));
}
#[tokio::test]
async fn tap_emit_primary_none_returns_true() {
let tap = new_event_tap();
let ok = tap_emit(
&tap,
None,
AgentEvent::TextDelta {
delta: "x".to_string(),
},
)
.await;
assert!(ok);
}
}