-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathsubscription.rs
More file actions
288 lines (257 loc) · 9.58 KB
/
Copy pathsubscription.rs
File metadata and controls
288 lines (257 loc) · 9.58 KB
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
281
282
283
284
285
286
287
288
//! Subscription handles for observing session and lifecycle events.
//!
//! Returned by [`Session::subscribe`](crate::session::Session::subscribe) and
//! [`Client::subscribe_lifecycle`](crate::Client::subscribe_lifecycle).
//!
//! Each subscription is an opt-in **observer** of events that are also
//! delivered to the per-event handlers installed on the session config
//! (see [`crate::handler`]). Subscribers receive a clone of every event but
//! cannot influence permission decisions, tool results, or any other event
//! whose handler return value affects the runtime.
//!
//! # Async iteration
//!
//! The subscription types implement [`tokio_stream::Stream`], so consumers
//! can use adapter combinators from [`tokio_stream::StreamExt`] or
//! `futures::StreamExt` (filtering, mapping, batching, racing with
//! `tokio::select!`, etc.) without learning the SDK's internal channel
//! choice. A simple `while let Ok(event) = sub.recv().await { ... }` loop
//! also works for callers who don't need the [`Stream`](tokio_stream::Stream)
//! surface.
//!
//! # Lag policy
//!
//! Each subscriber maintains its own internal queue. If a consumer cannot
//! keep up, the oldest events are dropped and the next call yields
//! [`Lagged`](crate::subscription::Lagged) reporting how many events were skipped.
//! Slow subscribers do not block the producer.
use std::fmt;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::sync::broadcast::Receiver;
use tokio_stream::wrappers::BroadcastStream;
use tokio_stream::wrappers::errors::BroadcastStreamRecvError;
use tokio_stream::{Stream, StreamExt as _};
use crate::types::{SessionEvent, SessionLifecycleEvent};
use crate::{Custom, Repr};
/// The subscription fell behind the producer.
///
/// Reports the number of events that were dropped from this subscriber's
/// queue because the consumer didn't keep up. The subscription continues
/// after this error, starting from the next live event — callers who care
/// about lag should match on it and decide whether to resync, re-fetch, or
/// log and continue.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Lagged(pub(crate) u64);
impl Lagged {
/// Number of events skipped before this consumer could read them.
pub fn skipped(&self) -> u64 {
self.0
}
}
impl fmt::Display for Lagged {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "subscription lagged behind by {} events", self.0)
}
}
impl std::error::Error for Lagged {}
/// Error kind for subscription receive operations.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RecvErrorKind {
/// The producer is gone — the session has shut down or the client has
/// stopped. No further events will be delivered.
Closed,
/// The subscriber fell behind. See [`Lagged`].
Lagged(Lagged),
}
impl fmt::Display for RecvErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RecvErrorKind::Closed => write!(f, "subscription closed"),
RecvErrorKind::Lagged(l) => write!(f, "{l}"),
}
}
}
/// Error returned by [`crate::subscription::EventSubscription::recv`] and
/// [`crate::subscription::LifecycleSubscription::recv`].
#[derive(Debug)]
pub struct RecvError {
repr: Repr<RecvErrorKind>,
}
impl RecvError {
/// The [`RecvErrorKind`] of this error.
pub fn kind(&self) -> &RecvErrorKind {
match &self.repr {
Repr::Simple(k) | Repr::SimpleMessage(k, ..) | Repr::Custom(Custom { kind: k, .. }) => {
k
}
}
}
}
impl fmt::Display for RecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.repr {
Repr::Simple(k) => write!(f, "{k}"),
Repr::SimpleMessage(_, m) => write!(f, "{m}"),
Repr::Custom(Custom { error, .. }) => write!(f, "{error}"),
}
}
}
impl std::error::Error for RecvError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.repr {
Repr::Custom(Custom { error, .. }) => Some(&**error),
_ => None,
}
}
}
impl From<RecvErrorKind> for RecvError {
fn from(kind: RecvErrorKind) -> Self {
Self {
repr: Repr::Simple(kind),
}
}
}
impl From<Lagged> for RecvError {
fn from(lagged: Lagged) -> Self {
Self::from(RecvErrorKind::Lagged(lagged))
}
}
macro_rules! define_subscription {
(
$(#[$meta:meta])*
$name:ident, $item:ty $(,)?
) => {
$(#[$meta])*
#[must_use = "subscriptions are inert until polled"]
pub struct $name {
inner: BroadcastStream<$item>,
}
impl $name {
pub(crate) fn new(rx: Receiver<$item>) -> Self {
Self {
inner: BroadcastStream::new(rx),
}
}
/// Receive the next event.
///
/// Returns:
///
/// - `Ok(event)` for the next delivered event.
/// - `Err(`[`RecvError`]`)` with [`RecvError::kind()`] [`RecvErrorKind::Lagged`] if the subscriber fell behind;
/// call `recv` again to continue from the next live event.
/// - `Err(`[`RecvError`]`)` with [`RecvError::kind()`] [`RecvErrorKind::Closed`] once the producer is gone.
///
/// # Cancel safety
///
/// **Cancel-safe.** Wraps a `tokio::sync::broadcast::Receiver`
/// via `BroadcastStream`; both are cancel-safe by design.
/// Dropping the future before completion is harmless — events
/// already buffered for this subscriber remain available on
/// the next `recv` call.
pub async fn recv(&mut self) -> Result<$item, RecvError> {
match self.inner.next().await {
Some(Ok(event)) => Ok(event),
Some(Err(BroadcastStreamRecvError::Lagged(n))) => {
Err(Lagged(n).into())
}
None => Err(RecvErrorKind::Closed.into()),
}
}
}
impl Stream for $name {
type Item = Result<$item, Lagged>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
match Pin::new(&mut self.inner).poll_next(cx) {
Poll::Ready(Some(Ok(event))) => Poll::Ready(Some(Ok(event))),
Poll::Ready(Some(Err(BroadcastStreamRecvError::Lagged(n)))) => {
Poll::Ready(Some(Err(Lagged(n))))
}
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}
}
};
}
define_subscription! {
/// Subscription to runtime events for a single
/// [`Session`](crate::session::Session).
///
/// Created by [`Session::subscribe`](crate::session::Session::subscribe).
/// Implements [`Stream`] yielding `Result<SessionEvent, Lagged>`.
/// Drop the value to unsubscribe; there is no separate cancel handle.
EventSubscription, SessionEvent
}
define_subscription! {
/// Subscription to lifecycle events on a [`Client`](crate::Client).
///
/// Created by
/// [`Client::subscribe_lifecycle`](crate::Client::subscribe_lifecycle).
/// Implements [`Stream`] yielding `Result<SessionLifecycleEvent, Lagged>`.
/// Drop the value to unsubscribe; there is no separate cancel handle.
LifecycleSubscription, SessionLifecycleEvent
}
#[cfg(test)]
mod tests {
use tokio::sync::broadcast;
use super::*;
fn make_event(id: &str) -> SessionEvent {
SessionEvent {
id: id.into(),
timestamp: "2025-01-01T00:00:00Z".into(),
parent_id: None,
ephemeral: None,
agent_id: None,
debug_cli_received_at_ms: None,
debug_ws_forwarded_at_ms: None,
event_type: "noop".into(),
data: serde_json::json!({}),
}
}
#[tokio::test]
async fn recv_yields_then_closes_on_drop_sender() {
let (tx, rx) = broadcast::channel(8);
let mut sub = EventSubscription::new(rx);
tx.send(make_event("a")).unwrap();
tx.send(make_event("b")).unwrap();
drop(tx);
assert_eq!(sub.recv().await.unwrap().id, "a");
assert_eq!(sub.recv().await.unwrap().id, "b");
assert!(matches!(
sub.recv().await.unwrap_err().kind(),
RecvErrorKind::Closed
));
}
#[tokio::test]
async fn recv_surfaces_lag() {
let (tx, rx) = broadcast::channel(2);
let mut sub = EventSubscription::new(rx);
for id in ["a", "b", "c", "d"] {
tx.send(make_event(id)).unwrap();
}
let err = sub.recv().await.expect_err("expected a Lagged error");
let RecvErrorKind::Lagged(l) = err.kind() else {
panic!("expected Lagged, got {:?}", err.kind());
};
assert_eq!(l.skipped(), 2);
// Subscription continues with the live tail.
assert_eq!(sub.recv().await.unwrap().id, "c");
assert_eq!(sub.recv().await.unwrap().id, "d");
}
#[tokio::test]
async fn stream_impl_matches_recv_semantics() {
let (tx, rx) = broadcast::channel(8);
let mut sub = EventSubscription::new(rx);
tx.send(make_event("a")).unwrap();
drop(tx);
// poll_next path
let next = sub.next().await;
assert_eq!(next.unwrap().unwrap().id, "a");
assert!(sub.next().await.is_none());
}
}