1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Workstream-level event aggregation (DOC-48 §5.3, §5.3.1; issue #3297,
//! epic #3292); thin `trusty-agents-common::transport` adapter (issue #3299,
//! Phase 1B); transport-free since #6637 slice 2c.
//!
//! # Spec References
//!
//! - [`SPEC-WS-05~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-05~draft) §5.3, §5.3.1
//! - [`SPEC-WS-07~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-07~draft) AC-7
//!
//! Why: `session.events` (`crate::session::events_stream`) is a direct lookup
//! against ONE session's ring buffer + live filter. A
//! workstream is a GROUP of sessions (§2.1), so observing "this workstream"
//! is not a lookup at all — it is a fan-out: subscribe once to the daemon's
//! existing session-scoped event bus (`crate::events::subscribe`, already
//! shared by every per-session tail) and forward only the events
//! whose `session_id` is currently bound to this workstream, tagged with the
//! generic `{session_id, event_type, payload}` envelope AC-7.2 requires.
//! "Currently bound" is re-checked against the store on every event (not a
//! snapshot taken at subscribe time) so a session added to the workstream
//! after the client connected is picked up without a reconnect (§5.3 point
//! 4) — cheap in practice because `WorkstreamStore::get` only re-parses the
//! backing file when its (mtime, len) fingerprint actually changed.
//!
//! **AC-7 harness-agnostic seam (Phase 1B, issue #3299):** the fan-out
//! algorithm itself now lives in
//! [`trusty_agents_common::transport::aggregate_live`] — this module is a
//! thin adapter that implements the shared `EventSource`/`MembershipProvider`
//! traits over `crate::events`/`SharedWorkstreamStore` and supplies the
//! `classify` closure encoding tcode's two group-scoped bypass events
//! (`WorkstreamActivationChanged`, `WorkstreamStateInferred`). Behavior is
//! byte-for-byte identical to the Phase 1A implementation this replaces (see
//! `sse_tests`, ported unchanged); only the fan-out combinator itself moved.
//!
//! What: [`WorkstreamEventEnvelope`] (a type alias over the shared
//! `EventEnvelope<Event>`), [`aggregate_live`] (this module's adapter
//! function — constructs the two trait impls and the `classify` closure,
//! then delegates to the shared combinator), and [`map_store_err`].
//!
//! **This module binds nothing (#6637 slice 2c).** It used to carry the
//! `GET /workstreams/{id}/events` axum route as well; that route retired with
//! the daemon's TCP listener. `crate::workstreams::events_stream` is the one
//! caller now — it serves the same fan-out as the `workstream.events` stream
//! method and owns the id validation the route used to do, so the refusals
//! (`invalid_params` for a non-UUID, `not_found` for an unknown id) are
//! unchanged and a CLOSED workstream stays observable (§4.4). A workstream
//! with zero bound sessions yields a stream that never emits, rather than an
//! error.
//! Test: `sse_tests`.
use async_trait;
use ;
use BroadcastStream;
use ;
use crateEvent;
use crateRpcError;
use SharedWorkstreamStore;
use WorkstreamId;
use StoreError;
/// The harness-agnostic wire envelope AC-7.2 requires: `{session_id,
/// event_type, payload}` — a type alias over
/// `trusty_agents_common::transport::EventEnvelope<Event>` (Phase 1B, issue
/// #3299), deliberately distinct from `crate::events::SessionEventEnvelope`
/// (which carries `seq`/`at`/`kind`/`event`, tuned for one session's
/// ring-buffer replay).
pub type WorkstreamEventEnvelope = EventEnvelope;
/// [`EventSource`] adapter over `crate::events` — subscribes to the SAME
/// daemon-global broadcast bus every per-session SSE connection already
/// reads, wrapping each envelope into the shared crate's
/// session-id/event-type/payload triple.
;
/// [`MembershipProvider`] adapter over [`SharedWorkstreamStore`] — "is this
/// session currently in `workstream_id`'s `session_ids`" re-fetched from the
/// store on every call (never a stale snapshot), matching §5.3's dynamic
/// membership requirement. A store lookup failure (the workstream vanished
/// from under an open connection — not reachable via any current mutation
/// path, since `workstream.close` never removes a record) is treated as
/// "no match" per this trait's documented contract, matching this route's
/// "stay open" bias for a zero-session workstream.
;
/// The `classify` bypass: forwards `WorkstreamActivationChanged` iff it
/// names `target` as its `new_active_id` OR `prior_id` (a workstream
/// observer must learn when IT stops being active, not only when a session
/// it owns emits, §6.3), and `WorkstreamStateInferred` iff its
/// `workstream_id` names `target` (an observer must learn its own state
/// changed — e.g. it was closed — even with zero bound sessions). Any other
/// event falls through to [`WorkstreamMembership`] (`None`). `target` is
/// `&str` (not `&WorkstreamId`) so [`aggregate_live`] can stringify the id
/// ONCE at stream construction rather than on every event flowing over the
/// daemon-global bus.
/// Build the live aggregation stream for one workstream (no ring-buffer
/// replay — see module docs).
///
/// Why: the single seam [`crate::workstreams::events_stream::open`] drives;
/// kept as a free function so a unit test can exercise the fan-out logic
/// directly against a `SharedWorkstreamStore`, without opening a socket.
/// As of Phase 1B (issue #3299) this is a thin adapter over
/// [`trusty_agents_common::transport::aggregate_live`] — see this module's
/// docs for the trait impls it supplies.
/// What: delegates to the shared combinator with [`TcodeEventSource`],
/// [`WorkstreamMembership`], and the [`classify`] bypass closure.
/// Test: `sse_tests::fan_out_tags_events_from_bound_sessions_only`,
/// `sse_tests::activation_changed_event_is_forwarded_regardless_of_session_binding`,
/// `sse_tests::state_inferred_event_is_forwarded_for_this_workstream_only`,
/// `sse_tests::empty_workstream_stream_yields_nothing`.
/// Map a [`StoreError`] onto the JSON-RPC error taxonomy (mirrors
/// `crate::workstreams::protocol::map_store_err`, kept separate since that
/// function is private to `protocol`).
///
/// `pub(super)` since #6637: `crate::workstreams::events_stream` answers the
/// same two refusals over the socket transport and must report the same codes.
pub