Skip to content

feat(serve): SSE event stream GET /sessions/{id}/events - #546

Merged
ajianaz merged 2 commits into
developfrom
feat/serve-event-stream
Nov 10, 2026
Merged

ajianaz merged 2 commits into
developfrom
feat/serve-event-stream

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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}/prompt blocks 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:
    • SSE framing; durable entry frames use the entry's seq as the SSE id (so Last-Event-ID and ?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's TurnObserver callbacks (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 says resync. A slow client can never slow a turn.
    • EntryTail: follows the session file by seq (one stat while 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_resolved frames from the existing approvals queue (a late joiner sees what is pending; old decisions are not replayed).
    • StreamLimiter: at most 8 open streams (429 beyond 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-ID is parsed; the stream is served before normal routing and owns its connection (ready marks the end of the replay, then durable entries, approvals and ephemeral frames, a heartbeat comment after 15 s without a write, and closed when the session leaves the live map). Streams count within the 32-connection cap but cannot starve /approvals and /prompt. Approval frames go out before the durable entries of the same pass, so a decision is announced ahead of its effects.
  • The prompt route attaches an observer only while a stream is open on that session. Attaching an observer turns on provider streaming, so with nobody watching a turn runs exactly as before streams existed (zero change for existing REST users). Consequence, documented: a stream opened mid-turn gets durable entries but no text deltas for that turn.
  • Tests: the serve process, provider mock and a small SSE client moved into a shared 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 in the same PR (AGENTS.md §7): README status line, docs/mobile-control.md (route table and an Event stream section with the frame table, replay semantics, limits and the browser EventSource caveat), docs/epics.md, docs/daemon-design.md, docs/threat-model.md (new Event streams section), CHANGELOG, serve.rs header.

Not included (stated, not hidden)

  • An observer for the turn that resumes after an approval allow: that path (resume_turn_with_cancel) has no observer variant in tole-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.
  • A stream token for browser EventSource (cannot send Authorization): left to the web client (feat(web): web client of tole serve (low priority — Svelte vs React undecided) #529).
  • A log index: each change replays the session file, so a very large session costs more per change.

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)
  • Local cora review --staged: not run (cora is not installed in this environment); the CI Cora Review job is the verdict.
  • 10 unit tests (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.
  • 5 e2e tests against the real tole serve binary: replay then follow a live turn (entries equal the stored log, ids are seqs, ordered, no duplicates, a text_delta arrives, the prompt answer is unchanged); reconnect with Last-Event-ID and with ?after= gets exactly the entries it missed, no stray frames, after beats a stale header; 401/404/400 and the cap (8 open, the 9th is 429, /sessions and /approvals still answer); approval flow (approval_requested, allow, approval_resolved before the resumed turn's answer, the work executed); an idle stream heartbeats (interval set to 1 s).
  • Mutations, each caught: no observer attached (live-turn test), 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

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.
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

🔍 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.

ajianaz commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Cora's finding is real and fixed in the next push. After ready, the loop reset its last-write time every pass even when the approvals poll wrote nothing (all() over an empty iterator is true), so the 15 s heartbeat could never fire: a dead peer would never be written to and would pin a stream slot and a thread. Fix: the clock is now a pure Heartbeat type in events.rs that only a real write resets (the resync write too, which was also missing), with unit tests on injected time. The interval can be overridden with TOLE_SERVE_HEARTBEAT_SECS (1 to 3600, default 15; also useful behind a proxy with a short idle timeout), which lets an e2e test (an_idle_stream_sends_heartbeats, interval 1 s) prove an idle stream heartbeats. I reintroduced the bug to check: that test fails (timed out waiting for a frame) and passes with the fix. This also corrects the PR description, which said the heartbeat was not covered by a test.

Unrelated note: one local run of the full workspace had tole-core storage::poison_tests::failed_append_poisons_until_reopen fail once with Locked on reopen; it passed on re-run and on the next full run (1178 passed). This PR does not touch tole-core.


Generated by Claude Code

@ajianaz
ajianaz merged commit 21ecba5 into develop Nov 10, 2026
22 of 24 checks passed
@ajianaz
ajianaz deleted the feat/serve-event-stream branch November 11, 2026 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(serve): event stream, transcript read and resume-by-id for remote UI clients

2 participants