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: 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§

ContextBudgetSnapshot
A cached snapshot of the Event::ContextBudget payload (issue #3015 — session.get_context_budget query RPC; surfaced by PR #3014’s GUI status bar, which renders “budget: unavailable” waiting for exactly this API).
IndexReadinessSnapshot
A cached snapshot of the Event::IndexReadiness payload (DOC-39 §5.6 Slice D — session.get_readiness query RPC).
RecalledMemory
One memory recalled by recall_session, with the flag that says whether it actually reached the model (UI Phase 1; text/run_id added for DOC-39 Slice C — the “what memory drove this / what was held back” context-construction debugging view).
SearchHit
One search hit’s path and relevance score, as parsed from a search_code call’s underlying trusty-search response (UI “what drove this” search-audit detail — DOC-39 Slice B).
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).
SearchAuditRecord
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).

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.
UNATTRIBUTED_AGENT
The agent attribution value used when a tool event’s AgentLoop was never told which agent it is running as (UI Phase 1).
UNATTRIBUTED_AGENT_ID
The agent_id attribution value used when a tool event’s AgentLoop was 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 emit writes for envelope, or None if it cannot be serialised.
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.