agent-sdk-core 0.1.0-alpha.3

Product-neutral primitive kernel and contracts for a Rust-first Agent SDK.
Documentation
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Event bus port and in-memory event bus helpers. Use this module for live
//! observation separate from durable journal truth. Publishing mutates the bus state
//! and may notify subscribers.
//!
use std::{
    collections::VecDeque,
    sync::{
        Arc, Mutex,
        atomic::{AtomicU64, Ordering},
    },
};

use crate::{
    domain::{AgentError, AgentId, RunId},
    event::{
        AgentEvent, ArchiveCursor, CompiledEventFilter, EventCursor, EventFilter, EventFrame,
        EventKind, EventOverflowNotice, EventOverflowReason, EventStreamScope,
        SubscriberOverflowPolicy, SubscriberQueueConfig, SubscriptionOptions, cursor_compatible,
    },
};

/// Port or behavior contract for agent event bus. Implementors should
/// preserve policy, redaction, idempotency, and replay expectations
/// from the surrounding module. Implementations may perform side
/// effects only as described by the trait methods.
pub trait AgentEventBus: Send + Sync {
    /// Mutates the in-memory event/subscription state and may wake local
    /// subscribers. It does not persist durable journal truth or call network
    /// sinks.
    fn publish(&self, frame: EventFrame) -> Result<(), AgentError>;

    /// Creates a live stream for all visible event frames from the cursor.
    /// Implementations may register subscriber state or read buffered frames,
    /// but must not publish new events or append journal records.
    fn subscribe_all(&self, cursor: Option<EventCursor>) -> Result<AgentEventStream, AgentError>;

    /// Creates a live stream for all visible event frames with queue options.
    /// Implementations create a live subscription stream from bus state; subscribing must not
    /// publish new events or append journal records.
    fn subscribe_all_with_options(
        &self,
        cursor: Option<EventCursor>,
        options: SubscriptionOptions,
    ) -> Result<AgentEventStream, AgentError>;

    /// Creates a live stream scoped to one run from the cursor.
    /// Implementations may register subscriber state or read buffered frames,
    /// but must not publish new events or append journal records.
    fn subscribe_run(
        &self,
        run_id: RunId,
        cursor: Option<EventCursor>,
    ) -> Result<AgentEventStream, AgentError>;

    /// Creates a live stream scoped to one run with queue options.
    /// Implementations create a live subscription stream from bus state; subscribing must not
    /// publish new events or append journal records.
    fn subscribe_run_with_options(
        &self,
        run_id: RunId,
        cursor: Option<EventCursor>,
        options: SubscriptionOptions,
    ) -> Result<AgentEventStream, AgentError>;

    /// Creates a live stream scoped to one agent from the cursor.
    /// Implementations may register subscriber state or read buffered frames,
    /// but must not publish new events or append journal records.
    fn subscribe_agent(
        &self,
        agent_id: AgentId,
        cursor: Option<EventCursor>,
    ) -> Result<AgentEventStream, AgentError>;

    /// Creates a live stream scoped to one agent with queue options.
    /// Implementations create a live subscription stream from bus state; subscribing must not
    /// publish new events or append journal records.
    fn subscribe_agent_with_options(
        &self,
        agent_id: AgentId,
        cursor: Option<EventCursor>,
        options: SubscriptionOptions,
    ) -> Result<AgentEventStream, AgentError>;

    /// Creates a live stream for frames matching a compiled envelope filter.
    /// Implementations create a live subscription stream from bus state; subscribing must not
    /// publish new events or append journal records.
    fn subscribe_filtered(
        &self,
        filter: CompiledEventFilter,
        cursor: Option<EventCursor>,
    ) -> Result<AgentEventStream, AgentError>;
}

/// Port or behavior contract for event archive. Implementors should
/// preserve policy, redaction, idempotency, and replay expectations
/// from the surrounding module. Implementations may perform side
/// effects only as described by the trait methods.
pub trait EventArchive: Send + Sync {
    /// Replays archived frames matching a compiled envelope filter from the
    /// archive cursor.
    /// Implementations read archived event frames from the requested cursor and return a
    /// stream; replaying must not publish new events or append journal records.
    fn replay_filtered_from_cursor(
        &self,
        filter: CompiledEventFilter,
        cursor: ArchiveCursor,
    ) -> Result<AgentEventStream, AgentError>;
}

#[derive(Clone, Debug)]
/// Carries agent event stream data across a host-port boundary.
/// Constructing the value does not call the host; the port method that receives it documents any adapter, network, or storage effect.
pub struct AgentEventStream {
    frames: VecDeque<EventFrame>,
}

impl AgentEventStream {
    /// Creates a new ports::event_bus value with explicit
    /// caller-provided inputs. This constructor is data-only and
    /// performs no I/O or external side effects.
    pub fn new(frames: impl IntoIterator<Item = EventFrame>) -> Self {
        Self {
            frames: frames.into_iter().collect(),
        }
    }
}

impl Iterator for AgentEventStream {
    type Item = EventFrame;

    fn next(&mut self) -> Option<Self::Item> {
        self.frames.pop_front()
    }
}

#[derive(Clone, Debug, Default)]
/// Carries in memory agent event bus data across a host-port boundary.
/// Constructing the value does not call the host; the port method that receives it documents any adapter, network, or storage effect.
pub struct InMemoryAgentEventBus {
    frames: Arc<Mutex<Vec<EventFrame>>>,
    next_event_seq: Arc<AtomicU64>,
}

impl InMemoryAgentEventBus {
    /// Mutates the in-memory event/subscription state and may wake local
    /// subscribers. It does not persist durable journal truth or call network
    /// sinks.
    pub fn publish(&self, frame: EventFrame) -> Result<(), AgentError> {
        let frame = self.assign_live_sequence(frame);
        self.frames
            .lock()
            .map_err(|_| AgentError::contract_violation("event bus lock poisoned"))?
            .push(frame);
        Ok(())
    }

    /// Mutates the in-memory event/subscription state and may wake local
    /// subscribers. It does not persist durable journal truth or call network
    /// sinks.
    pub fn publish_all(
        &self,
        frames: impl IntoIterator<Item = EventFrame>,
    ) -> Result<(), AgentError> {
        let frames = frames
            .into_iter()
            .map(|frame| self.assign_live_sequence(frame))
            .collect::<Vec<_>>();
        let mut locked = self
            .frames
            .lock()
            .map_err(|_| AgentError::contract_violation("event bus lock poisoned"))?;
        locked.extend(frames);
        Ok(())
    }

    fn filtered_stream(
        &self,
        requested_scope: EventStreamScope,
        filter: CompiledEventFilter,
        cursor: Option<EventCursor>,
        queue: SubscriberQueueConfig,
    ) -> Result<AgentEventStream, AgentError> {
        cursor_compatible(&requested_scope, cursor.as_ref())?;
        reject_live_overflow_policy(&queue)?;
        let start_after = cursor.as_ref().map(|cursor| cursor.event_seq);
        let frames = self
            .frames
            .lock()
            .map_err(|_| AgentError::contract_violation("event bus lock poisoned"))?
            .iter()
            .filter(|frame| start_after.is_none_or(|seq| frame.cursor.event_seq > seq))
            .filter(|frame| filter.matches_envelope(&frame.event.envelope))
            .map(|frame| {
                let mut frame = frame.clone();
                frame.cursor = frame.event.envelope.cursor(requested_scope.clone());
                frame
            })
            .collect::<Vec<_>>();
        let frames = apply_queue_bounds(frames, &queue);
        Ok(AgentEventStream::new(frames))
    }

    fn assign_live_sequence(&self, mut frame: EventFrame) -> EventFrame {
        let event_seq = self.next_event_seq.fetch_add(1, Ordering::SeqCst) + 1;
        frame.event.envelope.event_seq = event_seq;
        frame.cursor = frame.event.envelope.cursor(frame.cursor.scope.clone());
        frame
    }
}

impl AgentEventBus for InMemoryAgentEventBus {
    fn publish(&self, frame: EventFrame) -> Result<(), AgentError> {
        InMemoryAgentEventBus::publish(self, frame)
    }

    fn subscribe_all(&self, cursor: Option<EventCursor>) -> Result<AgentEventStream, AgentError> {
        self.subscribe_all_with_options(cursor, SubscriptionOptions::default())
    }

    fn subscribe_all_with_options(
        &self,
        cursor: Option<EventCursor>,
        options: SubscriptionOptions,
    ) -> Result<AgentEventStream, AgentError> {
        self.filtered_stream(
            EventStreamScope::All,
            EventFilter::default().compile()?,
            cursor,
            options.queue,
        )
    }

    fn subscribe_run(
        &self,
        run_id: RunId,
        cursor: Option<EventCursor>,
    ) -> Result<AgentEventStream, AgentError> {
        self.subscribe_run_with_options(run_id, cursor, SubscriptionOptions::default())
    }

    fn subscribe_run_with_options(
        &self,
        run_id: RunId,
        cursor: Option<EventCursor>,
        options: SubscriptionOptions,
    ) -> Result<AgentEventStream, AgentError> {
        self.filtered_stream(
            EventStreamScope::Run(run_id.clone()),
            EventFilter::run(run_id).compile()?,
            cursor,
            options.queue,
        )
    }

    fn subscribe_agent(
        &self,
        agent_id: AgentId,
        cursor: Option<EventCursor>,
    ) -> Result<AgentEventStream, AgentError> {
        self.subscribe_agent_with_options(agent_id, cursor, SubscriptionOptions::default())
    }

    fn subscribe_agent_with_options(
        &self,
        agent_id: AgentId,
        cursor: Option<EventCursor>,
        options: SubscriptionOptions,
    ) -> Result<AgentEventStream, AgentError> {
        self.filtered_stream(
            EventStreamScope::Agent(agent_id.clone()),
            EventFilter::agent(agent_id).compile()?,
            cursor,
            options.queue,
        )
    }

    fn subscribe_filtered(
        &self,
        filter: CompiledEventFilter,
        cursor: Option<EventCursor>,
    ) -> Result<AgentEventStream, AgentError> {
        let queue = filter.queue.clone();
        self.filtered_stream(filter.cursor_scope(), filter, cursor, queue)
    }
}

fn apply_queue_bounds(
    frames: impl IntoIterator<Item = EventFrame>,
    queue: &SubscriberQueueConfig,
) -> Vec<EventFrame> {
    let capacity = queue.capacity.get();
    let normal_capacity = capacity.saturating_sub(queue.terminal_reserve.get().min(capacity));
    let mut bounded = VecDeque::new();
    let mut overflow = OverflowAccumulator::default();
    let mut summary = ProgressSummaryAccumulator::default();

    for frame in frames {
        if frame.event.envelope.event_kind.is_terminal() {
            flush_progress_summary(
                &mut bounded,
                queue,
                normal_capacity,
                &mut summary,
                &mut overflow,
            );
            while bounded.len() >= capacity {
                if !drop_oldest_nonterminal(
                    &mut bounded,
                    &mut overflow,
                    EventOverflowReason::PolicyDroppedNonTerminal,
                ) {
                    if let Some(dropped) = bounded.pop_front() {
                        overflow.record_drop(&dropped, EventOverflowReason::SubscriberQueueFull);
                    } else {
                        break;
                    }
                }
            }
            push_with_notice(&mut bounded, frame, queue.overflow.clone(), &mut overflow);
            continue;
        }

        match queue.overflow {
            SubscriberOverflowPolicy::DropNonTerminal => {
                if can_accept_nonterminal(&bounded, capacity, normal_capacity) {
                    push_with_notice(&mut bounded, frame, queue.overflow.clone(), &mut overflow);
                } else {
                    overflow.record_drop(&frame, EventOverflowReason::PolicyDroppedNonTerminal);
                }
            }
            SubscriberOverflowPolicy::DropProgress => {
                if can_accept_nonterminal(&bounded, capacity, normal_capacity) {
                    push_with_notice(&mut bounded, frame, queue.overflow.clone(), &mut overflow);
                } else if is_progress_event(&frame.event.envelope.event_kind) {
                    overflow.record_drop(&frame, EventOverflowReason::PolicyDroppedProgress);
                } else if drop_oldest_progress(&mut bounded, &mut overflow) {
                    push_with_notice(&mut bounded, frame, queue.overflow.clone(), &mut overflow);
                } else {
                    overflow.record_drop(&frame, EventOverflowReason::SubscriberQueueFull);
                }
            }
            SubscriberOverflowPolicy::SummarizeAndContinue => {
                if is_progress_event(&frame.event.envelope.event_kind) {
                    summary.record_progress(frame);
                } else {
                    flush_progress_summary(
                        &mut bounded,
                        queue,
                        normal_capacity,
                        &mut summary,
                        &mut overflow,
                    );
                    if can_accept_nonterminal(&bounded, capacity, normal_capacity) {
                        push_with_notice(
                            &mut bounded,
                            frame,
                            queue.overflow.clone(),
                            &mut overflow,
                        );
                    } else {
                        overflow.record_drop(&frame, EventOverflowReason::SubscriberQueueFull);
                    }
                }
            }
            SubscriberOverflowPolicy::FailSubscriber => {
                if can_accept_nonterminal(&bounded, capacity, normal_capacity) {
                    push_with_notice(&mut bounded, frame, queue.overflow.clone(), &mut overflow);
                } else {
                    overflow.record_drop(&frame, EventOverflowReason::SubscriberQueueFull);
                    if let Some(last) = bounded.back_mut() {
                        last.overflow = Some(overflow.take_notice(queue.overflow.clone()));
                    }
                    break;
                }
            }
            SubscriberOverflowPolicy::BackpressureCaller => unreachable!(
                "live event bus rejects backpressure overflow policy before queue bounding"
            ),
        }
    }

    flush_progress_summary(
        &mut bounded,
        queue,
        normal_capacity,
        &mut summary,
        &mut overflow,
    );

    if overflow.has_drop() {
        if let Some(last) = bounded.back_mut() {
            last.overflow = Some(overflow.notice(queue.overflow.clone()));
        }
    }

    bounded.into_iter().collect()
}

fn reject_live_overflow_policy(queue: &SubscriberQueueConfig) -> Result<(), AgentError> {
    if queue.overflow == SubscriberOverflowPolicy::BackpressureCaller {
        return Err(AgentError::contract_violation(
            "InvalidOverflowPolicy: backpressure_caller is rejected for live event bus subscriptions",
        ));
    }
    Ok(())
}

fn can_accept_nonterminal(
    frames: &VecDeque<EventFrame>,
    capacity: usize,
    normal_capacity: usize,
) -> bool {
    frames.len() < capacity && nonterminal_count(frames) < normal_capacity
}

fn push_with_notice(
    frames: &mut VecDeque<EventFrame>,
    mut frame: EventFrame,
    policy: SubscriberOverflowPolicy,
    overflow: &mut OverflowAccumulator,
) {
    if overflow.has_drop() {
        frame.overflow = Some(overflow.take_notice(policy));
    }
    frames.push_back(frame);
}

fn drop_oldest_nonterminal(
    frames: &mut VecDeque<EventFrame>,
    overflow: &mut OverflowAccumulator,
    reason: EventOverflowReason,
) -> bool {
    let Some(index) = frames
        .iter()
        .position(|frame| !frame.event.envelope.event_kind.is_terminal())
    else {
        return false;
    };
    if let Some(dropped) = frames.remove(index) {
        overflow.record_drop(&dropped, reason);
        true
    } else {
        false
    }
}

fn drop_oldest_progress(
    frames: &mut VecDeque<EventFrame>,
    overflow: &mut OverflowAccumulator,
) -> bool {
    let Some(index) = frames
        .iter()
        .position(|frame| is_progress_event(&frame.event.envelope.event_kind))
    else {
        return false;
    };
    if let Some(dropped) = frames.remove(index) {
        overflow.record_drop(&dropped, EventOverflowReason::PolicyDroppedProgress);
        true
    } else {
        false
    }
}

fn nonterminal_count(frames: &VecDeque<EventFrame>) -> usize {
    frames
        .iter()
        .filter(|frame| !frame.event.envelope.event_kind.is_terminal())
        .count()
}

fn is_progress_event(kind: &EventKind) -> bool {
    matches!(
        kind,
        EventKind::ModelStreamDelta
            | EventKind::StreamRuleRepeatStateRecorded
            | EventKind::RealtimeInputSent
            | EventKind::RealtimeOutputReceived
            | EventKind::RealtimeBackpressureApplied
            | EventKind::IsolationProcessIoCaptured
            | EventKind::IsolationProcessStatsRecorded
            | EventKind::UsageRecorded
            | EventKind::CostEstimated
            | EventKind::CostCorrected
    )
}

fn flush_progress_summary(
    frames: &mut VecDeque<EventFrame>,
    queue: &SubscriberQueueConfig,
    normal_capacity: usize,
    summary: &mut ProgressSummaryAccumulator,
    overflow: &mut OverflowAccumulator,
) {
    let Some(frame) = summary.take_summary_frame() else {
        return;
    };
    if can_accept_nonterminal(frames, queue.capacity.get(), normal_capacity) {
        push_with_notice(frames, frame, queue.overflow.clone(), overflow);
    } else {
        overflow.record_drop(&frame, EventOverflowReason::PolicyDroppedProgress);
    }
}

#[derive(Default)]
struct ProgressSummaryAccumulator {
    dropped_count: u64,
    gap_start: Option<EventCursor>,
    gap_end: Option<EventCursor>,
    repair_from: Option<crate::domain::JournalCursor>,
    summary_frame: Option<EventFrame>,
}

impl ProgressSummaryAccumulator {
    fn record_progress(&mut self, frame: EventFrame) {
        self.dropped_count += 1;
        self.gap_start.get_or_insert_with(|| frame.cursor.clone());
        self.gap_end = Some(frame.cursor.clone());
        if self.repair_from.is_none() {
            self.repair_from = frame.cursor.journal_cursor.clone();
        }
        self.summary_frame = Some(frame);
    }

    fn take_summary_frame(&mut self) -> Option<EventFrame> {
        if self.dropped_count == 0 {
            return None;
        }
        let mut frame = self.summary_frame.take()?;
        let notice = EventOverflowNotice {
            policy: SubscriberOverflowPolicy::SummarizeAndContinue,
            dropped_count: self.dropped_count,
            gap_start: self.gap_start.clone(),
            gap_end: self
                .gap_end
                .clone()
                .unwrap_or_else(|| self.gap_start.clone().expect("summary gap start")),
            repair_from: self.repair_from.clone(),
            terminal_preserved: true,
            reason: EventOverflowReason::PolicyDroppedProgress,
        };
        frame.event = AgentEvent::with_redacted_summary(
            frame.event.envelope.clone(),
            format!(
                "redacted progress summary for {} dropped progress frames",
                self.dropped_count
            ),
        );
        frame.overflow = Some(notice);
        *self = Self::default();
        Some(frame)
    }
}

#[derive(Default)]
struct OverflowAccumulator {
    dropped_count: u64,
    gap_start: Option<EventCursor>,
    gap_end: Option<EventCursor>,
    repair_from: Option<crate::domain::JournalCursor>,
    terminal_dropped: bool,
    reason: Option<EventOverflowReason>,
}

impl OverflowAccumulator {
    fn record_drop(&mut self, frame: &EventFrame, reason: EventOverflowReason) {
        self.dropped_count += 1;
        self.gap_start.get_or_insert_with(|| frame.cursor.clone());
        self.gap_end = Some(frame.cursor.clone());
        if self.repair_from.is_none() {
            self.repair_from = frame.cursor.journal_cursor.clone();
        }
        self.terminal_dropped |= frame.event.envelope.event_kind.is_terminal();
        self.reason.get_or_insert(reason);
    }

    fn has_drop(&self) -> bool {
        self.dropped_count > 0
    }

    fn take_notice(&mut self, policy: SubscriberOverflowPolicy) -> EventOverflowNotice {
        let notice = self.notice(policy);
        *self = Self::default();
        notice
    }

    fn notice(&self, policy: SubscriberOverflowPolicy) -> EventOverflowNotice {
        EventOverflowNotice {
            policy,
            dropped_count: self.dropped_count,
            gap_start: self.gap_start.clone(),
            gap_end: self
                .gap_end
                .clone()
                .unwrap_or_else(|| self.gap_start.clone().expect("overflow gap start")),
            repair_from: self.repair_from.clone(),
            terminal_preserved: !self.terminal_dropped,
            reason: if self.terminal_dropped {
                EventOverflowReason::SubscriberQueueFull
            } else {
                self.reason
                    .clone()
                    .unwrap_or(EventOverflowReason::SubscriberQueueFull)
            },
        }
    }
}