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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Peer-bus HTTP surface (`/api/v1/bus/*`).
//!
//! Why: DOC-60 §4 hosts the bus in the `tm` daemon, so trusty-agents processes
//! reach it over the daemon's existing loopback HTTP API rather than a second
//! transport. Modeling the endpoints on the existing session routes (SSE for
//! the live stream, JSON for everything else) keeps the idiom uniform — §9 is
//! explicit that this surface should mirror the existing patterns rather than
//! invent a new one.
//! What: five handlers — register/deregister/list an instance, publish a peer
//! message, and subscribe to one instance's inbound stream. #6288 slice 5 had
//! briefly mounted the four request/response verbs as JSON-RPC methods on the
//! daemon's Unix socket too; slice A retires those registrations — the
//! 2026-09-05 owner ruling makes trusty-console the host of the only event
//! bus, so mpm no longer serves one of its own, and this HTTP surface is now
//! the sole caller of the transport-neutral `*_op` bodies ([`register_op`],
//! [`deregister_op`], [`list_op`], [`publish_op`]) each handler delegates to.
//! [`subscribe_instance`] was never among them — it is SSE and was always
//! HTTP-only. This HTTP surface itself retires in #6288 slice 8, once the
//! daemon finishes its move onto the UDS transport.
//! Test: `bus::tests` — `route_register_returns_instance_id`,
//! `route_publish_to_dead_instance_is_410`, `route_list_instances`; the
//! retirement is proven in `daemon::rpc::registry::tests` —
//! `rpc_router_serves_no_bus_method`,
//! `rpc_every_retired_bus_method_answers_method_not_found`.
use Infallible;
use Arc;
use ;
use Stream;
use ;
use crateDaemonError;
use crateDaemonState;
use ;
use BusError;
use InboxItem;
use ;
/// The peer bus's self-contained sub-router, merged into the daemon router.
///
/// Why: owning the route table here (rather than listing the paths in
/// `api.rs`) keeps the bus's HTTP surface beside the registry and envelope it
/// operates on, mirroring `manager_router`. Adding an endpoint later touches
/// one file instead of two.
/// What: registration (list/create/delete), publish, and the per-instance SSE
/// subscription.
/// Test: `route_register_returns_instance_id`, `route_list_instances`,
/// `route_publish_delivers_and_returns_envelope`.
/// Request body for `POST /api/v1/bus/instances`.
///
/// Why: an assistant announces itself by definition, not by id — the id is
/// minted by the daemon so it cannot be forged or collided.
/// What: the §6a definition id plus an optional project scope (recorded, not
/// routed on; cross-project addressing is DOC-60 §12 Q4, deferred).
/// Test: `route_register_returns_instance_id`.
/// Response body for the instance listing.
///
/// Test: `route_list_instances`.
/// The `to` field of a publish request — DOC-60 §5.3's two addressing modes.
///
/// Why: instance bypass is owner-ratified (DOC-60 §12 Q8, decided in this
/// change's favor), so the wire shape must express both modes and must make
/// which one was intended unambiguous. It is NOT a fallback pair: supplying
/// both fields selects bypass and, if that instance is gone, the request
/// fails — it does not quietly retry against the definition. See
/// [`super::registry`]'s module doc for why.
/// What: either or both ids; `instance_id` wins when both are present.
/// Test: `target_prefers_instance_when_both_supplied`,
/// `route_publish_to_dead_instance_is_410`.
/// Request body for `POST /api/v1/bus/publish`.
///
/// Why: `message_id` and `ts` are absent by design — the daemon mints both so
/// ids stay unique and the append-only log stays monotonic.
/// What: the §6c caller, the target, the payload, and an optional thread
/// pointer.
/// Test: `route_publish_to_dead_instance_is_410`.
/// `POST /api/v1/bus/instances` — register a live instance.
///
/// Why: registration is what makes an assistant addressable at all; without
/// it neither addressing mode can resolve anything.
/// What: mints and returns the instance metadata, `201 Created`.
/// Test: `route_register_returns_instance_id`.
pub async
/// [`register_instance`]'s body, with no transport in it (#6288 slice 5).
///
/// Test: `route_register_returns_instance_id`.
/// `DELETE /api/v1/bus/instances/{instance_id}` — deregister on exit.
///
/// Why: a stale registration would let definition-addressed delivery resolve
/// to a dead instance, turning a clean `404` into a confusing `409`.
/// What: `204 No Content` when removed, `404` when it was not registered.
/// Test: `route_deregister_removes_instance`.
pub async
/// [`deregister_instance`]'s body, with no transport in it (#6288 slice 5).
///
/// Why the error is a [`DaemonError`] rather than a [`BusError`]: `BusError` has
/// no "was not registered" variant, and the two candidates both say the wrong
/// thing — `InstanceGone` is `410` (this route has always answered `404`) and
/// `NoLiveInstance` is about a DEFINITION that resolved to nothing. Inventing a
/// bus variant to serve one route would put a fifth addressing failure into an
/// enum DOC-60 §4 keeps deliberately narrow.
///
/// # Errors
///
/// [`DaemonError::NotFound`] when `instance_id` was not a live registration.
///
/// Test: `route_deregister_removes_instance`.
/// `GET /api/v1/bus/instances` — the live roster.
///
/// Why: a sender choosing a peer, and an operator debugging delivery, both
/// need to see what is actually addressable right now.
/// What: every live [`InstanceMeta`], ordered by registration sequence.
/// Test: `route_list_instances`.
pub async
/// [`list_instances`]'s body, with no transport in it (#6288 slice 5).
///
/// Test: `route_list_instances`.
/// `POST /api/v1/bus/publish` — send one peer message.
///
/// Why: the §5.3 delivery path. Every failure reaches the sender as a distinct
/// status (`400` malformed request or a caller kind this path does not carry,
/// `403` the claimed sender is not a live registration, `404` no live
/// instance, `410` the named instance is gone, `409` registered but
/// unattached) per §4's fail-closed rule.
///
/// **A slow recipient no longer refuses the publish (#6462).** Between #4271
/// and #6462 this endpoint answered `503` when the recipient's shared channel
/// was full of unread envelopes, and one stalled subscriber made every publish
/// to that instance answer `503` — healthy co-subscribers included, until it
/// drained, dropped, or the instance was deregistered. Each subscription now
/// has its own buffer, so a stalled client falls behind alone and this endpoint
/// answers `202` while that client's inbox displaces its own oldest unread
/// envelope. The loss is recorded in the DOC-60 §9 durable log and announced to
/// the affected client as a [`LAGGED_EVENT`] frame; the sender is not involved
/// and has nothing to retry. `503` is no longer a status this endpoint returns.
/// What: resolves the target, publishes, and returns the stamped envelope with
/// `202 Accepted` so the sender holds the `message_id` for threading. The
/// `from` field is a CLAIM: [`PeerBus::publish`](crate::daemon::bus::PeerBus::publish)
/// verifies it against the registry and re-stamps the definition, so a caller
/// cannot choose the identity its message is recorded under.
/// Test: `route_publish_to_dead_instance_is_410`,
/// `route_publish_delivers_and_returns_envelope`,
/// `route_publish_forged_user_kind_is_400`,
/// `route_publish_unregistered_sender_is_403`,
/// `route_publish_past_a_stalled_subscriber_is_accepted`.
pub async
/// [`publish_message`]'s body, with no transport in it (#6288 slice 5).
///
/// Why this is a call-signature move and nothing more: DOC-60 §4's fail-closed
/// sequencing lives inside
/// [`PeerBus::publish`](crate::daemon::bus::PeerBus::publish) — structural
/// validation of the caller identity, then the `assistant_instance` edge check,
/// then sender verification against the registry, then target resolution, then
/// the delivery attempt, with the durable record written to state what actually
/// happened. None of that moved, and none of it is re-derived here, so the
/// socket path rejects at exactly the same four points the HTTP path does.
/// Every rejection reaches the caller as a [`BusError`], which both transports
/// render from the same `status` table.
///
/// # Errors
///
/// Every [`BusError`] `PeerBus::publish` can raise, plus
/// [`BusError::InvalidTarget`] when the request names neither addressing mode.
/// A recipient whose subscriber has stopped reading is NOT among them since
/// #6462 — see [`publish_message`]'s doc.
///
/// Test: `route_publish_delivers_and_returns_envelope`,
/// `route_publish_forged_user_kind_is_400`,
/// `route_publish_unregistered_sender_is_403`,
/// `route_publish_to_dead_instance_is_410`,
/// `route_publish_to_unknown_definition_is_404`,
/// `route_publish_past_a_stalled_subscriber_is_accepted`.
/// The SSE event name a lag notice arrives under (#4271).
///
/// Why its own name rather than another default `message` frame: a client
/// deserializing every frame as a [`BusEnvelope`] must not have to guess. A
/// named event is ignored by a client that does not know it, and dispatched by
/// one that does.
/// Test: `subscribe_stream_reports_lag_instead_of_swallowing_it`.
pub const LAGGED_EVENT: &str = "lagged";
/// What a `lagged` SSE frame carries.
///
/// Why: a subscriber that fell behind needs four things to recover — that it
/// happened, how many envelopes it missed, where to read them, and under which
/// key. The durable §9 JSONL stream holds every envelope the bus recorded as
/// delivered plus, since #6462, one
/// [`InboxMiss`](crate::daemon::bus::InboxMiss) per envelope this subscription
/// lost, keyed by `subscription_id` — so naming the path and the id makes the
/// recovery a file read rather than a lost message.
/// What: the instance, the subscription, the count its inbox displaced, and the
/// durable log's resolved path.
/// Test: `subscribe_stream_reports_lag_instead_of_swallowing_it`.
/// `GET /api/v1/bus/subscribe/{instance_id}` — an instance's inbound stream.
///
/// Why: delivery is per-instance, so a subscriber attaches to one instance's
/// own channel rather than filtering a global firehose — the defect DOC-60 §2
/// identifies in the existing `/sessions/{id}/events` path.
/// What: SSE of [`BusEnvelope`] JSON; `410 Gone` when the instance is not
/// registered, so a client cannot sit on a stream that will never produce.
///
/// **Lag is reported, never swallowed (#4271).** This handler used to map
/// `Lagged(n)` to `None`, borrowing the session SSE handlers' idiom — but those
/// streams carry load-sheddable telemetry, which DOC-60 §3 keeps OFF this bus.
/// Here the skipped frames were addressed messages the durable log had already
/// recorded as delivered, so the recipient lost them and was told nothing. A
/// lag now arrives as a [`LAGGED_EVENT`] frame carrying a [`LagNotice`], plus a
/// `warn!`. Since #6462 this stream reads THIS subscription's own inbox, so a
/// lag here means this client fell
/// [`CLIENT_INBOX_CAPACITY`](super::inbox::CLIENT_INBOX_CAPACITY) behind —
/// nobody else's backlog can produce one, and no other client is slowed by it.
///
/// **The subscription ends when its body is dropped or the instance is
/// deregistered.** Dropping the body detaches the inbox, which is what keeps a
/// disconnected client from holding envelopes nobody will read; deregistration
/// lets the reader drain what it already holds and then ends the stream.
/// Test: `subscribe_to_dead_instance_errors`,
/// `publish_reaches_only_target_instance`,
/// `subscribe_stream_reports_lag_instead_of_swallowing_it`,
/// `deregister_ends_a_subscription_after_it_drains`.
pub async
/// Build the [`LAGGED_EVENT`] frame, keeping the count reachable either way.
///
/// Why the fallback: serializing four owned scalars cannot fail, but mapping a
/// hypothetical failure to `None` would reinstate the silent skip this whole
/// change removes. The plain-text form still carries the number that matters.
/// Test: `subscribe_stream_reports_lag_instead_of_swallowing_it`.