Skip to main content

Module events

Module events 

Source
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: events::publish round-trips through subscribe(). The relay prefix is stable so the parent stderr reader can detect and re-publish.

Structs§

SessionEventEnvelope
The stable, sequenced wire envelope for one session’s event (#2055).

Enums§

Event
Real-time event types streamed to UI clients (browser, future Tauri).

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 Event JSON and re-published on the parent’s event bus instead of being mirrored to terminal stderr.

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.
preview
Truncate a string to at most max characters, 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.