Expand description
Process-wide broadcast event bus for real-time UI streaming.
Why: Centralises telemetry emission so any code path can publish events
without threading a sender through dozens of call sites.
What: Event enum, publish/subscribe/emit helpers, the
EVENT_LINE_PREFIX constant for subprocess relay.
Test: events::tests::publish_round_trips_through_subscribe.
Process-wide event bus for real-time UI streaming (#192 Phase B; #2055
formalises the session-attach envelope on top of it).
Why: The 2-second stderr polling model from #149 is slow, lossy, and only
handles workflow phase transitions. The UI needs immediate feedback on
every PM thinking turn, every sub-agent message, every tool call — across
both the in-process Axum server path AND the subprocess workflow path.
A tokio::sync::broadcast channel exposed via a process-global OnceLock
lets any code path emit events without threading the bus through dozens of
function signatures, while SSE subscribers fan out to all browsers.
#2055 adds SessionEventEnvelope: every event the session.attach
protocol (#2054) cares about is session-scoped, so the bus now carries the
ENVELOPE (session_id, a per-session monotonic seq, a UTC timestamp,
and the tagged Event payload) rather than a bare Event. This is the
same “envelope wraps a domain-tagged payload” shape as
trusty-agents-common’s HarnessEvent/HarnessPayload convention, with
two deliberate divergences (documented on the struct itself): seq is
PER-SESSION here (not process-global) because the session-attach
replay->live continuity this ticket requires is scoped to one session’s
stream, and the payload stays a flat #[serde(tag = "type")] enum (not a
nested {domain, event} wrapper) because #2054 already shipped that wire
shape and rewriting it would break the shipped session.attach contract.
session::registry::SessionRegistry is the sole assigner of seq
(see its record method) since it already owns the per-session ring
buffer this must stay consistent with.
What: Defines the Event enum (the tagged payload), SessionEventEnvelope
(the wire envelope), a process-global EVENT_BUS initialised lazily from
any thread carrying envelopes, helpers to publish and subscribe, and a
__OMPM_EVENT__ <json>\n stderr-relay protocol so events emitted by
--workflow subprocesses can be re-broadcast by the parent API server.
Test: see events_tests (sibling file, #[cfg(test)]) — events::publish
round-trips through subscribe(), kind() is pinned against the serde tag
for every variant, and the relay prefix is stable so the parent stderr
reader can detect and re-publish.
Structs§
- Context
Budget Snapshot - A cached snapshot of the
Event::ContextBudgetpayload (issue #3015 —session.get_context_budgetquery RPC; surfaced by PR #3014’s GUI status bar, which renders “budget: unavailable” waiting for exactly this API). - Index
Readiness Snapshot - A cached snapshot of the
Event::IndexReadinesspayload (DOC-39 §5.6 Slice D —session.get_readinessquery RPC). - Recalled
Memory - One memory recalled by
recall_session, with the flag that says whether it actually reached the model (UI Phase 1;text/run_idadded for DOC-39 Slice C — the “what memory drove this / what was held back” context-construction debugging view). - Search
Hit - One search hit’s path and relevance score, as parsed from a
search_codecall’s underlying trusty-search response (UI “what drove this” search-audit detail — DOC-39 Slice B). - Session
Event Envelope - The stable, sequenced wire envelope for one session’s event (#2055).
- Todo
Item - One step of an agent’s session checklist (#8235).
Enums§
- Event
- Real-time event types streamed to UI clients (browser, future Tauri).
- Search
Audit Record - One row in a session’s RETAINED search/recall audit trail (issue #3072;
backs
session.get_search_audit/GET /sessions/{id}/search-audit— DOC-39 §4.7’s Search tab (10d) and the #3027 8b monitor card). - Todo
Status - One session-checklist step’s state (#8235).
Constants§
- EVENT_
LINE_ PREFIX - Stderr line prefix used to relay events from a workflow subprocess to its
parent API server. The parent reads stderr line-by-line; lines starting
with this prefix are parsed as
EventJSON and re-published on the parent’s event bus instead of being mirrored to terminal stderr. - UNATTRIBUTED_
AGENT - The
agentattribution value used when a tool event’sAgentLoopwas never told which agent it is running as (UI Phase 1). - UNATTRIBUTED_
AGENT_ ID - The
agent_idattribution value used when a tool event’sAgentLoopwas never told a stable per-spawn id (DOC-39 AC-13.1/13.2).
Functions§
- bus
- Get (or initialise) the process-global event bus sender.
- emit
- Publish an envelope AND emit it on stderr with the
__OMPM_EVENT__prefix so a parent process can re-broadcast on its own bus. - format_
event_ line - Format the single stderr relay line
emitwrites forenvelope, orNoneif it cannot be serialised. - preview
- Truncate a string to at most
maxcharacters, appending an ellipsis marker when truncation occurred. Operates on chars (not bytes) so multi-byte characters are not split. - publish
- Publish one envelope to the bus (best-effort, never panics).
- subscribe
- Subscribe to all future events on the global bus.