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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Workstream-level SSE event aggregation route (DOC-48 §5.3, §5.3.1; issue
//! #3297, epic #3292); thin `trusty-agents-common::transport` adapter (issue
//! #3299, Phase 1B).
//!
//! # 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: `GET /sessions/{id}/events` (`crate::serve::http::session_events_sse`)
//! 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 SSE connection) 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 [`routes`] (the single
//! `GET /workstreams/{id}/events` axum route, merged into
//! `crate::serve::http::build_axum_router` alongside `rest::workstreams`).
//! Unknown workstream id -> `404`; malformed (non-UUID) id -> `400`; a
//! CLOSED workstream is still observable (`200`, not `404`) because its
//! existing bound sessions "remain valid and may accumulate turns" after
//! closure (§4.4) — closing only stops NEW session bindings, not event
//! delivery for the ones it already has. A workstream with zero bound
//! sessions returns `200` with a stream that simply never emits (until a
//! future session is added or the workstream's activation state changes) —
//! it does not error or close early.
//! Test: `sse_tests`.
use Infallible;
use async_trait;
use ;
use ;
use get;
use ;
use ;
use BroadcastStream;
use ;
use Response;
use Uuid;
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 [`routes`]' handler drives; kept as a free function
/// (not inlined into the handler) so a unit test can exercise the fan-out
/// logic directly against a `SharedWorkstreamStore`, without going through
/// axum at all. 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`.
/// Serialise one [`WorkstreamEventEnvelope`] as an SSE `data:` frame — mirrors
/// `crate::serve::http::sse_event_for`'s (practically infallible) fallback
/// convention.
/// 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
/// Shared axum state for this route: just the workstream store.
/// `GET /workstreams/{id}/events` — SSE aggregation over a workstream's
/// bound sessions.
///
/// Why: see module docs for the full fan-out design.
/// What: `400` (`-32602 invalid_params`) if `id` is not a valid UUID; `404`
/// (`-32002 not_found`) if it names no existing workstream (a CLOSED
/// workstream is NOT a 404 — see module docs); otherwise `200` with an SSE
/// stream from [`aggregate_live`], kept alive with axum's default
/// keep-alive comments (matching `session_events_sse`'s convention).
/// Test: `sse_tests::unknown_id_returns_404`,
/// `sse_tests::malformed_id_returns_400`.
async
/// Build the `GET /workstreams/{id}/events` route group.
///
/// Why: kept separate from `crate::serve::http::build_axum_router` so this
/// route is unit-testable via `tower::util::ServiceExt::oneshot` on its own,
/// exactly like every `rest::*` group — `crate::serve::rest::workstreams`
/// merges the CRUD/activation REST surface, this merges the observation
/// surface, both sharing the SAME `SharedWorkstreamStore` handle the daemon
/// boot path constructs.
/// What: one route, `with_state`-erased to `axum::Router<()>` so the caller
/// can `.merge()` it.
/// Test: `sse_tests::*`.