ocserve-core 0.1.0

Session domain: inbox admission, agent loop, event bus, permissions
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
//! Event bus: byte- AND count-bounded ring with cursor receivers.
//!
//! Contract (captured live, PLAN §3/§4):
//! - frames: {directory, project, payload:{id,type,properties}}
//! - durable types (session.updated, message.updated, message.part.updated)
//!   also get a sync twin {payload:{type:"sync", syncEvent:{id,type:"<t>.1",
//!   seq, aggregateID, data}}}; seq is per-session monotonic
//! - overflow policy: slow subscribers are disconnected (bounded by
//!   construction — no unbounded queues, AGENTS §2.3); clients recover via
//!   REST snapshot on reconnect (PLAN §4 divergence note)
//!
//! Why not `tokio::sync::broadcast` (the obvious choice): broadcast is
//! **count**-bounded only. Every `message.part.updated` carries the full part
//! (text included — oc-remote's EventReducer stores these snapshots as the
//! rendered chat, so event text cannot be truncated: audit 2026-10-02), and
//! durable emits publish TWO frames (plain + sync twin). A ring of
//! 1024 × multi-MB frames is a GB-class worst case — the exact unbounded-
//! growth class that OOMs upstream opencode (its per-subscriber
//! `Queue.unbounded` + `offerUnsafe`, event.ts:25). This ring is bounded by
//! **bytes as well as count**, stores each frame serialized ONCE (Arc<str>,
//! instead of a Value clone per receiver), and never blocks the publisher:
//! publish = lock + push + evict + watch notify.
//!
//! Per-connection memory stays O(1 frame in flight): the SSE task holds at
//! most one frame while writing to a stalled TCP socket; when the receiver
//! falls behind the ring, it sees `Lagged` and the stream disconnects
//! (existing captured behavior — clients reconnect and REST-snapshot).

use serde_json::{Value, json};
use std::collections::VecDeque;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

/// Count bound of the shared ring (was the broadcast cap; kept).
const BUS_CAPACITY: usize = 1024;
/// Byte bound of the shared ring (new). Sized against MEMORY §1's
/// unallocated headroom (~146 MB): ring ≤32 MB + transient per-connection
/// write copies stay inside it. A SINGLE frame larger than the budget is
/// still admitted (content is contract-critical; the ring sheds everything
/// else instead) — worst case = max(BUDGET, largest frame).
pub const RING_BYTE_BUDGET: usize = 32 * 1024 * 1024;

/// Effective byte budget: `OCSERVE_EVENT_RING_MB` (env, SRE tunability) with
/// the documented default above. Read once (poison-free pattern like the
/// stall watchdog).
fn ring_byte_budget() -> usize {
    static BUDGET: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *BUDGET.get_or_init(|| {
        std::env::var("OCSERVE_EVENT_RING_MB")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
            .filter(|mb| *mb >= 1)
            .map(|mb| mb * 1024 * 1024)
            .unwrap_or(RING_BYTE_BUDGET)
    })
}

struct RingState {
    /// (seq, serialized frame)
    items: VecDeque<(u64, Arc<str>)>,
    /// last issued seq (0 = none yet)
    next_seq: u64,
    total_bytes: usize,
}

pub struct EventBusInner {
    ring: Mutex<RingState>,
    /// watch channel carrying the latest seq (one slot — never grows)
    head: tokio::sync::watch::Sender<u64>,
    receivers: AtomicUsize,
}

#[derive(Clone)]
pub struct EventBus {
    inner: Arc<EventBusInner>,
}

/// Why `recv()` ended: the receiver fell behind (ring evicted its next
/// frame) or the bus was dropped. Mirrors `broadcast::error::RecvError`
/// so SSE code treats lag the same way it did before (disconnect).
#[derive(Debug, PartialEq, Eq)]
pub enum RecvError {
    Lagged(u64),
    Closed,
}

#[derive(Debug, PartialEq, Eq)]
pub enum TryRecvError {
    Empty,
    Lagged(u64),
    Closed,
}

/// Cursor receiver: starts at the head AT SUBSCRIBE TIME (future events
/// only — same semantics as `broadcast::subscribe`). Not `Clone`.
pub struct EventBusReceiver {
    inner: Arc<EventBusInner>,
    last_seq: u64,
    /// already-delivered items spilling out of one ring drain
    pending: VecDeque<String>,
    head_rx: tokio::sync::watch::Receiver<u64>,
}

impl EventBus {
    pub fn new() -> Self {
        let (head, _) = tokio::sync::watch::channel(0u64);
        Self {
            inner: Arc::new(EventBusInner {
                ring: Mutex::new(RingState {
                    items: VecDeque::new(),
                    next_seq: 0,
                    total_bytes: 0,
                }),
                head,
                receivers: AtomicUsize::new(0),
            }),
        }
    }

    /// Live subscriber count (SSE client gauge, SRE §2).
    pub fn subscriber_count(&self) -> usize {
        self.inner.receivers.load(Ordering::Relaxed)
    }

    /// Publish a fully-encoded global-event frame (payload already wrapped).
    /// Never blocks on subscribers: evicts OLDEST frames when the ring
    /// exceeds count or byte bounds (lagging receivers get `Lagged` and
    /// disconnect — backpressure → disconnect, not stall, not growth).
    pub fn publish(&self, frame: Value) {
        let bytes = frame.to_string();
        let len = bytes.len();
        let mut ring = self.inner.ring.lock().expect("event ring poisoned");
        ring.next_seq += 1;
        let seq = ring.next_seq;
        ring.total_bytes += len;
        ring.items.push_back((seq, Arc::from(bytes)));
        // evict oldest until BOTH bounds hold — but never the sole item
        // (an oversize frame must still reach clients; content is contract)
        while ring.items.len() > 1
            && (ring.items.len() > BUS_CAPACITY || ring.total_bytes > ring_byte_budget())
        {
            let (_, evicted) = ring.items.pop_front().expect("non-empty");
            ring.total_bytes = ring.total_bytes.saturating_sub(evicted.len());
            ocserve_metrics::counter("ocserve_event_ring_evicted_total", 1);
        }
        let depth = ring.items.len() as i64;
        let bytes_now = ring.total_bytes as i64;
        drop(ring);
        ocserve_metrics::gauge("ocserve_event_ring_depth", depth);
        ocserve_metrics::gauge("ocserve_event_ring_bytes", bytes_now);
        let _ = self.inner.head.send(seq);
    }

    /// New receiver for events published AFTER this call.
    pub fn subscribe(&self) -> EventBusReceiver {
        let last_seq = self
            .inner
            .ring
            .lock()
            .expect("event ring poisoned")
            .next_seq;
        let head_rx = self.inner.head.subscribe();
        self.inner.receivers.fetch_add(1, Ordering::Relaxed);
        EventBusReceiver {
            inner: self.inner.clone(),
            last_seq,
            pending: VecDeque::new(),
            head_rx,
        }
    }
}

impl Default for EventBus {
    fn default() -> Self {
        Self::new()
    }
}

impl EventBusReceiver {
    /// Move ring items into `pending` (or report lag). Returns false when
    /// caught up.
    fn drain(&mut self) -> Result<bool, RecvError> {
        let (front, next) = {
            let ring = self.inner.ring.lock().expect("event ring poisoned");
            (
                ring.items.front().map(|(s, _)| *s),
                ring.items
                    .iter()
                    .find(|(s, _)| *s > self.last_seq)
                    .map(|(s, a)| (*s, a.clone())),
            )
        };
        // gap between my cursor and the oldest resident frame = I lagged
        if let Some(front) = front
            && front > self.last_seq + 1
        {
            return Err(RecvError::Lagged(front - self.last_seq - 1));
        }
        match next {
            Some((s, frame)) => {
                self.last_seq = s;
                // deliver-one-at-a-time like broadcast: exactly one frame per
                // drain so heartbeat fairness is unchanged
                self.pending.push_back(frame.to_string());
                Ok(true)
            }
            None => Ok(false),
        }
    }

    pub async fn recv(&mut self) -> Result<String, RecvError> {
        loop {
            if let Some(f) = self.pending.pop_front() {
                return Ok(f);
            }
            match self.drain() {
                Ok(true) => continue, // got the next frame into pending
                Err(e) => return Err(e),
                Ok(false) => {}
            }
            // `changed()` MARKS the value seen when it returns — never call
            // has_changed() in this loop (it doesn't mark → busy spin).
            // A publish between drain-empty and changed() returns instantly.
            if self.head_rx.changed().await.is_err() {
                if let Ok(true) = self.drain() {
                    continue;
                }
                if let Some(f) = self.pending.pop_front() {
                    return Ok(f);
                }
                return Err(RecvError::Closed);
            }
        }
    }

    pub fn try_recv(&mut self) -> Result<String, TryRecvError> {
        if let Some(f) = self.pending.pop_front() {
            return Ok(f);
        }
        match self.drain() {
            Ok(true) => Ok(self.pending.pop_front().expect("drained one")),
            // Closed is unreachable here: the receiver holds an Arc to the
            // inner (sender) for its own lifetime — same practical reality
            // as broadcast with the SSE keepalive holding the bus.
            Ok(false) => Err(TryRecvError::Empty),
            Err(RecvError::Lagged(n)) => Err(TryRecvError::Lagged(n)),
            Err(RecvError::Closed) => Err(TryRecvError::Closed),
        }
    }
}

impl Drop for EventBusReceiver {
    fn drop(&mut self) {
        self.inner.receivers.fetch_sub(1, Ordering::Relaxed);
    }
}

/// Encode a plain domain event frame.
pub fn frame(directory: &str, event_type: &str, properties: Value) -> Value {
    json!({
        "directory": directory,
        "project": "global",
        "payload": {
            "id": crate::ids::evt_id(),
            "type": event_type,
            "properties": properties
        }
    })
}

/// Encode a sync-twin frame (EventReducer consumption): the payload is a
/// `sync` envelope wrapping the original event with a per-session seq.
pub fn sync_frame(
    directory: &str,
    event_type: &str,
    properties: Value,
    seq: u64,
    session_id: &str,
) -> Value {
    json!({
        "directory": directory,
        "project": "global",
        "payload": {
            "type": "sync",
            "syncEvent": {
                "id": crate::ids::evt_id(),
                "type": format!("{event_type}.1"),
                "seq": seq,
                "aggregateID": session_id,
                "data": properties
            }
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn frame_shape_matches_capture() {
        let f = frame("/x", "session.idle", json!({"sessionID": "ses_1"}));
        assert_eq!(f["directory"], "/x");
        assert_eq!(f["project"], "global");
        assert_eq!(f["payload"]["type"], "session.idle");
        assert_eq!(f["payload"]["properties"]["sessionID"], "ses_1");
        assert!(f["payload"]["id"].as_str().unwrap().starts_with("evt_"));
    }

    #[test]
    fn sync_frame_shape_matches_capture() {
        let f = sync_frame(
            "/x",
            "message.updated",
            json!({"sessionID": "ses_1"}),
            7,
            "ses_1",
        );
        let se = &f["payload"]["syncEvent"];
        assert_eq!(se["type"], "message.updated.1");
        assert_eq!(se["seq"], 7);
        assert_eq!(se["aggregateID"], "ses_1");
        assert_eq!(se["data"]["sessionID"], "ses_1");
    }

    #[tokio::test]
    async fn bus_delivers_and_bounds_by_count() {
        let bus = EventBus::new();
        let mut rx = bus.subscribe();
        bus.publish(frame("/x", "session.idle", json!({})));
        let got = rx.recv().await.unwrap();
        let v: Value = serde_json::from_str(&got).unwrap();
        assert_eq!(v["payload"]["type"], "session.idle");
        // overflow → Lagged error (not a stall); receiver side disconnects
        for _ in 0..2000 {
            bus.publish(frame("/x", "session.status", json!({})));
        }
        match rx.recv().await {
            Ok(_) | Err(RecvError::Lagged(_)) => {}
            e => panic!("unexpected bus result: {e:?}"),
        }
    }

    /// NEGATIVE-CONTROL pair for the byte bound: publishing frames that
    /// would blow the count bound's byte budget must evict (depth AND bytes
    /// stay ≤ budget) — remove the eviction condition and this fails.
    #[tokio::test]
    async fn ring_is_byte_bounded_not_just_count_bounded() {
        let bus = EventBus::new();
        // one 16 MB frame ×4 = 64 MB > 32 MB budget (count stays under1024)
        let big = "x".repeat(16 * 1024 * 1024);
        for i in 0..4 {
            bus.publish(frame(
                "/x",
                "message.part.updated",
                json!({"i": i, "big": big}),
            ));
        }
        let ring = bus.inner.ring.lock().unwrap();
        assert!(
            ring.total_bytes <= RING_BYTE_BUDGET,
            "ring bytes {} exceeded budget",
            ring.total_bytes
        );
        assert!(
            ring.items.len() < 4,
            "oversize frames must evict: depth {}",
            ring.items.len()
        );
        // newest frame survives
        let last = ring.items.back().unwrap();
        assert_eq!(last.0, ring.next_seq, "newest frame kept");
    }

    /// An oversize frame (larger than the whole budget) is still admitted
    /// (content contract) and evicts everything else — bounded BY it.
    #[tokio::test]
    async fn single_oversize_frame_admitted_alone() {
        let bus = EventBus::new();
        for i in 0..5 {
            bus.publish(frame("/x", "t", json!({"i": i})));
        }
        let huge = "y".repeat(RING_BYTE_BUDGET + 1024);
        bus.publish(frame("/x", "message.part.updated", json!({"big": huge})));
        let ring = bus.inner.ring.lock().unwrap();
        assert_eq!(
            ring.items.len(),
            1,
            "everything else shed for the oversize frame"
        );
        assert!(ring.total_bytes > RING_BYTE_BUDGET);
    }

    /// The publisher never blocks on slow subscribers: no receiver drains
    /// anything and publishes still complete promptly (the upstream OOM
    /// pattern is offering into unbounded per-subscriber queues instead).
    #[tokio::test]
    async fn publisher_never_blocks_on_stalled_receivers() {
        let bus = EventBus::new();
        let _slow: Vec<_> = (0..8).map(|_| bus.subscribe()).collect();
        let t0 = std::time::Instant::now();
        for _ in 0..5000 {
            bus.publish(frame(
                "/x",
                "message.part.updated",
                json!({"pad": "z".repeat(1024)}),
            ));
        }
        assert!(
            t0.elapsed() < Duration::from_secs(5),
            "publish loop took {:?}",
            t0.elapsed()
        );
        // stalled receivers are lagging, not growing: ring stays bounded
        let ring = bus.inner.ring.lock().unwrap();
        assert!(ring.items.len() <= BUS_CAPACITY);
        assert!(ring.total_bytes <= RING_BYTE_BUDGET);
    }

    #[tokio::test]
    async fn subscribe_only_sees_future_and_try_recv_roundtrips() {
        let bus = EventBus::new();
        bus.publish(frame("/x", "old", json!({})));
        let mut rx = bus.subscribe();
        assert!(
            matches!(rx.try_recv(), Err(TryRecvError::Empty)),
            "subscribe is future-only (broadcast parity)"
        );
        bus.publish(frame("/x", "new", json!({})));
        let got = rx.try_recv().unwrap();
        assert!(got.contains("\"type\": \"new\"") || got.contains("\"type\":\"new\""));
        assert!(matches!(rx.try_recv(), Err(TryRecvError::Empty)));
        assert_eq!(bus.subscriber_count(), 1);
        drop(rx);
        assert_eq!(bus.subscriber_count(), 0);
    }
}

#[cfg(test)]
mod unfold_tests {
    use super::*;
    use std::time::Duration;

    /// Replicates ocserve-http's SSE merge loop under paused time.
    #[tokio::test(start_paused = true)]
    async fn heartbeat_emits_at_10s_and_stream_stays_open() {
        let bus = EventBus::new();
        let mut rx = bus.subscribe();
        let mut next_hb = tokio::time::Instant::now() + Duration::from_secs(10);
        let mut got_hb = false;
        for _ in 0..3 {
            let dur = next_hb.saturating_duration_since(tokio::time::Instant::now());
            match tokio::time::timeout(dur.max(Duration::from_millis(1)), rx.recv()).await {
                Ok(Ok(_frame)) => panic!("unexpected frame"),
                Ok(Err(e)) => panic!("recv errored: {e:?}"),
                Err(_elapsed) => {
                    got_hb = true;
                    next_hb += Duration::from_secs(10);
                }
            }
        }
        assert!(got_hb, "heartbeat fired");
        // frames still flow after the heartbeats
        bus.publish(frame("/x", "after", json!({})));
        let got = tokio::time::timeout(Duration::from_secs(1), rx.recv())
            .await
            .expect("frame arrives")
            .expect("not lagged");
        assert!(got.contains("after"));
    }
}