Repository navigation
feat(serve): SSE event stream GET /sessions/{id}/events - #546
Conversation
Third and last part of #524. Durable entry frames replay losslessly from a seq (after or Last-Event-ID); ephemeral text, tool and approval frames are best-effort with a resync marker; bounded non-blocking per-stream queue; heartbeat; at most 8 open streams. New events module; shared serve test harness.
🔍 Cora AI Code Review✅ No issues found. Code looks good! Review powered by cora-code · BYOK · MIT |
…EARTBEAT_SECS (#524) The event stream reset its last-write time every pass even when the approvals poll wrote nothing, so the heartbeat could never fire and a dead peer pinned its stream slot. The clock is now a pure Heartbeat type that only a real write resets, with an interval override (TOLE_SERVE_HEARTBEAT_SECS) used by an e2e test that fails on the old behavior.
|
Cora's finding is real and fixed in the next push. After Unrelated note: one local run of the full workspace had Generated by Claude Code |
What
Third and last part of #524 (UI epic #523):
GET /sessions/{id}/events?after=<seq>, a Server-Sent Events stream so a graphical client can show a session live. Parts 1 and 2 (transcript read #544, resume-by-id #545) are merged. This completes #524. Closes #524.Why
POST /sessions/{id}/promptblocks until the turn ends, so a UI could not show streamed text, tool cards or approval requests as they happen. Design choices were agreed with the owner on 2026-11-10: SSE, durable frames from the log plus best-effort ephemeral frames, and a separate cap of 8 streams.Changes
crates/tole-cli/src/events.rs(new, lib), pure and unit-tested:entryframes use the entry'sseqas the SSEid(soLast-Event-IDand?after=replay losslessly), ephemeral frames carry no id so a reconnect can never skip a real entry.Bus/BusRegistry/BusObserver: fan-out of a turn'sTurnObservercallbacks (text_delta,reasoning,tool_started,tool_finished) to open streams through a bounded, non-blocking queue per stream; a full queue drops the frame, counts it and the stream saysresync. A slow client can never slow a turn.EntryTail: follows the session file byseq(onestatwhile unchanged; a read-only replay when it changed, a torn tail is skipped, never truncated), so it works while a turn holds the live storage lock. Writes that land between the stat and the replay are picked up next poll, never missed (seq filters duplicates).ApprovalWatch:approval_requested/approval_resolvedframes from the existing approvals queue (a late joiner sees what is pending; old decisions are not replayed).StreamLimiter: at most 8 open streams (429beyond that), freed on drop.Heartbeat: a pure clock that is only reset by a real write (injected time, unit-tested);TOLE_SERVE_HEARTBEAT_SECS(1 to 3600, default 15) overrides the interval, e.g. behind a proxy with a short idle timeout.serve.rs:Last-Event-IDis parsed; the stream is served before normal routing and owns its connection (readymarks the end of the replay, then durable entries, approvals and ephemeral frames, a heartbeat comment after 15 s without a write, andclosedwhen the session leaves the live map). Streams count within the 32-connection cap but cannot starve/approvalsand/prompt. Approval frames go out before the durable entries of the same pass, so a decision is announced ahead of its effects.tests/serve_harness(the entries/resume tests from feat(serve): paginated transcript read GET /sessions/{id}/entries #544/feat(serve): resume a session by id with POST /sessions {"resume"} #545 use it too).docs/mobile-control.md(route table and an Event stream section with the frame table, replay semantics, limits and the browserEventSourcecaveat),docs/epics.md,docs/daemon-design.md,docs/threat-model.md(new Event streams section), CHANGELOG,serve.rsheader.Not included (stated, not hidden)
allow: that path (resume_turn_with_cancel) has no observer variant intole-core, so adding one is a core change beyond this issue. Its entries (the final answer) still arrive as durable frames; only live text/tool frames are missing for it.EventSource(cannot sendAuthorization): left to the web client (feat(web): web client of tole serve (low priority — Svelte vs React undecided) #529).Testing
cargo fmt --all -- --check,cargo clippy --workspace --all-targets -- -D warnings,cargo check -p tole-cli --no-default-features,cargo test --workspace(1178 passed)cora review --staged: not run (cora is not installed in this environment); the CI Cora Review job is the verdict.events): frame format and escaping, ephemeral shapes, bus fan-out/drop counting/unsubscribe, observer only while watched, limiter, approval diff (late joiner, resolve, expiry, decided-between-polls), tail (each entry once, torn tail, unchanged = no replay, start after a seq), the heartbeat clock (only a real write resets it) and its interval parsing.tole servebinary: replay then follow a live turn (entries equal the stored log, ids are seqs, ordered, no duplicates, atext_deltaarrives, the prompt answer is unchanged); reconnect withLast-Event-IDand with?after=gets exactly the entries it missed, no stray frames,afterbeats a stale header; 401/404/400 and the cap (8 open, the 9th is 429,/sessionsand/approvalsstill answer); approval flow (approval_requested, allow,approval_resolvedbefore the resumed turn's answer, the work executed); an idle stream heartbeats (interval set to 1 s).seq >=in the tail (duplicates: e2e + unit), cap off by one (e2e + unit), an empty approvals pass resetting the heartbeat clock (the bug Cora found: heartbeat e2e times out).Related Issues
Closes #524, part of #523. Follow-ups in the epic: #525, #526.
Generated by Claude Code