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
268
269
270
271
272
273
274
275
276
277
//! The subscribe half of the event-bus wire contract (ADR-0065, DOC-73 §4.4).
//!
//! Why: ADR-0065 runs the bus as its own `trusty-events` daemon, and no other
//! crate links that daemon's library (decision 1). Producers and readers
//! meet it only on its socket, so the socket's path and the
//! `events.subscribe { since_seq, filters }` method are a contract between
//! independently installed binaries. Decision 3 puts that contract here,
//! beside the envelope it carries, so the daemon (#9075) and console's
//! client (#9076) build against one definition.
//! What: The service and method names, [`events_socket_path`], the request
//! params ([`SubscribeParams`]), the server-side predicate
//! ([`SubscribeFilter`]) and the streamed response item
//! ([`SubscribeItem`]). Types only: the request frame that needs the
//! UDS envelope lives in `subscribe_frame.rs`, behind the `uds` feature.
//! Request types reject unknown fields, so a filter the server does not
//! understand fails instead of silently widening the subscription.
//! Response items accept unknown fields, so a newer daemon can add one
//! without breaking an older client. An unknown item `type` is an
//! error, so a new kind is sent only to a client that asked for it,
//! as `heartbeat_ms` asks for [`SubscribeItem::Heartbeat`] (#10071,
//! DOC-73 §3.3).
//! Test: `super::subscribe_tests`.
// #9075: new module, PR1 of epic #9074. The `trusty-events` daemon serves it
// from PR2; console's client follows in #9076.
use PathBuf;
use ;
use ;
use ;
use EventId;
use Filter;
use HarnessSource;
/// The bus daemon's service name: its data-dir name and its socket's stem.
pub const EVENTS_SERVICE: &str = "trusty-events";
/// The streaming JSON-RPC method a reader opens on the bus socket.
pub const METHOD_EVENTS_SUBSCRIBE: &str = "events.subscribe";
/// Resolve the `trusty-events` socket, which producers and readers both dial.
///
/// Why: ADR-0065 decision 2 names the socket after the bus, not its host, and
/// requires every client to resolve it through this one helper. A producer and
/// a reader that each spelled the path would drift, and the daemon would read
/// as down to whichever spelled it wrong (the #4246 class).
/// What: [`crate::daemon_addr::daemon_socket_path`] for [`EVENTS_SERVICE`],
/// that is `<data dir>/trusty-events/trusty-events.sock`. It mirrors that
/// helper exactly, including the ADR-0032 rules it carries: a UDS path, never a
/// TCP port, redirectable in tests through `TRUSTY_DATA_DIR_OVERRIDE`, and
/// unconditional so a caller that only prints the path compiles no socket
/// code. Resolving it creates the data directory, as the mirrored helper does.
///
/// # Errors
///
/// Whatever [`crate::daemon_addr::daemon_socket_path`] returns: the data
/// directory could not be resolved or created. The error is passed through
/// unchanged, so this helper and the one it mirrors fail the same way.
///
/// Test: `events_socket_path_is_named_after_the_bus`.
/// The params of one `events.subscribe` request.
///
/// Why: DOC-73 §4.3 — a reader that opens mid-session replays what it missed
/// from the durable log, then follows the live ring. `since_seq` is that
/// resume point, and it is also how a client reconnects without a gap after a
/// hang-up or a [`SubscribeItem::Gap`].
/// What: `since_seq: None` streams live events only. `Some(n)` first replays
/// every retained event with `seq > n`, in `seq` order, then streams live;
/// `Some(0)` replays everything retained. `since_id` is the id of the event
/// the reader holds at `since_seq`; the bus refuses a resume with `n > 0`
/// whose `since_id` is absent or names a different event, because a `seq` it
/// handed out but never logged can be assigned again after a crash, and
/// resuming past the new event would skip it silently (#9075). `filters` is
/// applied server-side and defaults to match-all, so `{}` is a valid request.
/// `heartbeat_ms: Some(n)` asks the bus to send [`SubscribeItem::Heartbeat`]
/// at once and then every `n` ms; the bus refuses `n` outside
/// [`HEARTBEAT_MS_MIN`]`..=`[`HEARTBEAT_MS_MAX`]. Absent, no heartbeat is sent
/// and the stream is what it was before #10071. Unknown fields are rejected.
/// Test: `subscribe_params_golden_roundtrip`,
/// `subscribe_params_since_id_golden_roundtrip`,
/// `subscribe_params_heartbeat_ms_golden_roundtrip`,
/// `subscribe_params_empty_object_is_live_and_match_all`,
/// `subscribe_params_rejects_an_unknown_field`,
/// `subscribe_params_rejects_a_mistyped_field`.
/// The shortest `heartbeat_ms` the bus accepts (#10071).
///
/// Why: `0` would turn the heartbeat into a busy loop; 100 ms still leaves
/// room for a one-shot health probe that asks for 500 ms.
pub const HEARTBEAT_MS_MIN: u64 = 100;
/// The longest `heartbeat_ms` the bus accepts, one hour (#10071).
///
/// Why: a longer interval proves nothing a reader could wait for, and an
/// absurd value is more likely a unit mistake than a request.
pub const HEARTBEAT_MS_MAX: u64 = 3_600_000;
/// A conjunctive (AND) filter over `HarnessEvent`s that crosses the socket.
///
/// Why: DOC-73 §4.4 — filters apply server-side, so a wall display following
/// one session is not sent every event. [`Filter`] cannot cross a socket: its
/// `domains` are `&'static str` and it is not serde. This is its owned, serde
/// twin, extended with the `kind` and `actor` axes §4.4 names.
/// What: each axis is optional and `None` does not constrain. `source`,
/// `session` and `domains` mean exactly what they mean on [`Filter`].
/// `kind` matches an `Action` payload's kind tag (`"file"`, `"tool"`, ...).
/// `actor` matches an `Action` payload's actor: an agent's `name` or
/// `agent_id`, or the literal `"operator"` or `"system"`. An event that is not
/// an `Action` has neither a kind nor an actor, so it never matches a `kind`
/// or `actor` constraint, as an event with no session never matches a
/// `session` constraint. Values are not checked against a closed set, so a
/// newer client's kind matches nothing on an older daemon; field names are, and
/// an unknown one is a deserialisation error.
/// Test: `subscribe_filter_golden_roundtrip`,
/// `subscribe_filter_agrees_with_filter`,
/// `subscribe_filter_kind_axis_keeps_only_that_action_kind`,
/// `subscribe_filter_actor_axis_keeps_only_that_actor`,
/// `subscribe_filter_rejects_an_unknown_field`.
/// Whether `actor` is the one a filter's `actor` value names.
/// [`Filter`] carried onto the wire, one axis for one axis.
/// One item of an `events.subscribe` stream: the `result` of each
/// `"stream":"item"` frame.
///
/// Why: DOC-73 §4.3 — ring overflow is reported, never silent, and a reader
/// must tell a replayed, durable event from a live one whose write may still be
/// in flight. A bare `HarnessEvent` can say neither.
/// What: internally tagged on `type`. `Event` carries the event, whose `seq` is
/// the bus's own, and `persisted`: `true` when it was read back from the
/// durable log, `false` on live fan-out. `Gap` says the subscriber fell behind
/// the ring and `skipped` events were not delivered; the reader resubscribes
/// with `since_seq` set to the last `seq` it holds to recover them from the
/// log. `Heartbeat` is sent only to a subscription that set `heartbeat_ms`:
/// it says the bus is alive and `head` is its last assigned `seq`; it carries
/// no event and never moves a reader's resume point (#10071). Unknown fields
/// are ignored; an unknown `type` or a missing field is an error, which is
/// why a new kind needs an opt-in. `#[non_exhaustive]` so a later item type
/// is not a Rust-API break.
/// Test: `subscribe_item_event_golden_roundtrip`,
/// `subscribe_item_gap_golden_roundtrip`,
/// `subscribe_item_heartbeat_golden_roundtrip`,
/// `subscribe_item_ignores_an_unknown_field`,
/// `subscribe_item_rejects_a_malformed_item`.