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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! HTTP JSON-RPC 2.0 transport for `tcode serve --http` (#2053, #2054).
//!
//! Why: matches how `trusty-memory` (`web::router` + `run_http_on`) and
//! `trusty-search` (`service::server::build_router`) stand up their axum
//! daemons: a pure `axum::Router` builder — testable via `oneshot`, no real
//! socket — wrapped in the shared guarded middleware stack
//! (`trusty_common::server::with_guarded_middleware_same_origin_cors`, #6003),
//! plus a bind+serve entry point that installs the
//! shared `trusty_common::shutdown_signal()` graceful-shutdown watcher
//! (SIGTERM + SIGINT), exactly like `trusty-memory`'s `run_http_on`. `POST
//! /rpc` and `GET /health` dispatch through the SAME [`crate::jsonrpc::Router`]
//! the STDIO transport (`crate::serve::transport`) uses via
//! [`Router::dispatch_json`] — there is exactly one implementation of each
//! method (`ping`, `health`, `session.*`, …), not one per transport.
//!
//! #2054 adds `GET /sessions/{id}/events`: an SSE route that is HTTP's real
//! live-streaming mechanism for `session.attach`. HTTP `POST /rpc` is
//! fundamentally one request/one response, so `session.attach` over HTTP
//! acknowledges immediately (replay burst + this endpoint's URL in
//! `stream_url`) rather than holding the connection open — a browser or CLI
//! client then opens this endpoint to receive the ring-buffer replay
//! followed by live events, and simply closing that connection IS "detach"
//! for HTTP (there is nothing further to call). See
//! `crate::jsonrpc::ConnectionContext`'s docs and `session::protocol::attach`
//! for the full STDIO-vs-HTTP rationale. Multi-client attach falls out for
//! free here: every `GET` gets its own `crate::events::subscribe()`
//! receiver.
//!
//! What: [`build_axum_router`] (pure, unit-testable) and [`run_http`] (binds
//! a `TcpListener` — port `0` yields an OS-assigned ephemeral port — logs
//! the bound address to stderr, never stdout, and serves until graceful
//! shutdown).
//!
//! #5439 puts every route behind a credential. `trusty_common::daemon_token`
//! mints and stores it at `0600`; `trusty_common::server::bearer_auth` is the
//! router-wide guard, applied here to the FULLY-MERGED router so a route added
//! later cannot slip past it. [`PUBLIC_HEALTH_PATH`] is the single exemption
//! and discloses only liveness to an anonymous caller (#6472);
//! [`SSE_TICKET_PATH`] is how a browser `EventSource`, which cannot send a
//! header, carries an existing right onto a URL.
//!
//! Deferred: batch JSON-RPC requests (a JSON array body) are not supported —
//! `POST /rpc` accepts a single request object per call, matching what the
//! ticket asked for ("single-request is fine for now").
//!
//! Also deferred, and larger: this whole listener is scheduled for replacement
//! by a Unix socket fronted by trusty-console (ADR-0032, the #5439 owner
//! ruling of 2026-08-19), which authenticates by peer credential rather than
//! by a shared secret. This layer is the hardening that stands until then, not
//! the end state.
//!
//! Test: `http::tests::*` drive [`build_axum_router`] via
//! `tower::util::ServiceExt::oneshot` (no real socket): `POST /rpc` ping
//! success, `POST /rpc` malformed JSON -> `-32700`, `GET /health`,
//! `session.create` + `GET /sessions/{id}/events` SSE replay + live event.
use Infallible;
use SocketAddr;
use Arc;
use ;
use ;
use ;
use TcpListener;
use mpsc;
use BroadcastStream;
use info;
use SelfOrigins;
use ;
use Response;
use crateProjectBinding;
use crate;
use crate;
use craterest;
use crateSessionRegistry;
use crateSharedWorkstreamStore;
/// The app name whose data directory holds this daemon's credential file.
///
/// Why: server (`run_http`) and client (`crate::tui_client`) resolve the token
/// through the same constant, so they cannot end up reading two files.
pub const TOKEN_APP_NAME: &str = "trusty-code";
/// The one route that answers an unauthenticated caller (#6472).
///
/// Why: trusty-console's gateway polls a daemon for liveness and holds no
/// credential, and the ecosystem convention (trusty-search, trusty-memory) is
/// an open `GET /health`. Keeping the ROUTE public while moving its
/// disclosures behind the credential preserves that poll and still stops an
/// anonymous caller from reading the daemon's pid, bound project, and version.
pub const PUBLIC_HEALTH_PATH: &str = "/health";
/// `POST` here (authenticated) to mint a single-use SSE ticket.
///
/// Why: `EventSource` cannot send an `Authorization` header, so a browser
/// client exchanges its credential for a ticket here and puts THAT in the SSE
/// URL — see `trusty_common::server::bearer_auth`'s module docs.
pub const SSE_TICKET_PATH: &str = "/auth/sse-ticket";
/// Shared axum state: the JSON-RPC router (for `POST /rpc`), the session
/// registry (for the SSE route, which reads it directly rather than going
/// through the router), and the daemon's project binding (for `GET /health`).
///
/// Why: every field is an `Arc` so cloning per-request (axum requires
/// `State` to be `Clone`) is cheap and every route sees the same daemon-wide
/// instances. `binding` is here rather than resolved per request because a
/// daemon binds exactly ONE project for its whole life — that immutability
/// is precisely what makes it worth publishing on `/health` (#4512).
/// Build the pure axum router (no listener bound) exposing `POST /rpc`,
/// `GET /health`, `GET /sessions/{id}/events`, and (#2983 Slice 2 + Slice 3)
/// the full `/sessions*` REST resource group.
///
/// Why: kept separate from [`run_http`] so unit tests exercise routing and
/// dispatch via `tower::util::ServiceExt::oneshot` without opening a real
/// socket, and so the shared guarded middleware stack wraps it identically to
/// trusty-agents/trusty-memory/trusty-search.
///
/// #6003: the stack is
/// `trusty_common::server::with_guarded_middleware_same_origin_cors`, not the
/// permissive `with_standard_middleware` this router used to call. Two things
/// change. `guard_write_origin` runs router-wide and innermost, so a browser
/// POST/PUT/PATCH/DELETE carrying a foreign `Origin` is `403`ed before any
/// handler runs — `POST /rpc` (the whole JSON-RPC method surface), `/tasks`,
/// `/sessions`, `/sessions/{id}/messages`, `/agents`, and the `workstream.*`
/// write routes. And the CORS policy reflects only same-machine origins
/// instead of `Any`, so a page on a foreign origin cannot READ
/// `GET /sessions/{id}/transcript` or either SSE stream either — those are
/// `GET`, which the method-gated guard deliberately lets through, and they
/// carry conversation content. Callers that send no `Origin` at all (the
/// `tcode tui` client, the console reverse proxy, `curl`) are untouched: the
/// guard passes an absent `Origin` through, and CORS is browser-enforced.
/// What: `POST /rpc` dispatches the request body through
/// `Router::dispatch_json` (shared with the STDIO transport); `GET /health`
/// returns [`health_payload`]; `GET /sessions/{id}/events` streams that
/// session's ring-buffer replay then live events as SSE; `crate::serve::rest`
/// merges in the `session.*` REST read routes (`GET /sessions`,
/// `GET /sessions/{id}`, `.../transcript`, `.../readiness`, `.../goals`,
/// (issue #3015) `.../budget`),
/// (Slice 3) the write routes (`POST /sessions`,
/// `POST /sessions/{id}/messages`, `POST /sessions/{id}/cancel`,
/// `PUT`/`DELETE /sessions/{id}/goal`), (Slice 4) `POST /tasks`, (Slice 5)
/// `GET /fs`, (Slice 6) `GET /sessions/{id}/agents`, (Slice 7, issue #3072)
/// `GET /sessions/{id}/search-audit`, and the full `workstream.*` REST
/// surface: `POST /workstreams`, `GET /workstreams/{id}`, `GET /workstreams`,
/// `POST /workstreams/{id}/close` (issue #3295) plus
/// `POST /workstreams/{id}/activate`/`.../deactivate` (DOC-48 §5.2, issue
/// #3294) — plus (issue #3297) `GET /workstreams/{id}/events` (the
/// workstream-level SSE aggregation route, `crate::workstreams::sse::routes`)
/// — plus (issue #3449) `GET`/`POST /agents`, `DELETE /agents/{name}` and the
/// `skills` twin (the Foundry GUI's Agents/Skills management tabs,
/// `rest::agent_catalog`/`rest::skill_catalog`) — none of which collide with
/// the paths registered directly on this router or with each other.
/// Test: `http_rpc_ping_returns_pong`,
/// `http_rpc_malformed_json_returns_parse_error`,
/// `http_health_matches_jsonrpc_health_payload`,
/// `http_session_events_sse_streams_replay_then_live_event`,
/// `http_rest_sessions_route_is_merged_in`,
/// `http_rest_post_sessions_route_is_merged_in`,
/// `http_rest_post_tasks_route_is_merged_in`,
/// `http_rest_get_fs_route_is_merged_in`,
/// `http_rest_get_session_agents_route_is_merged_in`,
/// `http_rest_get_session_budget_route_is_merged_in`,
/// `http_rest_get_session_search_audit_route_is_merged_in`,
/// `http_rest_get_workstreams_route_is_merged_in` (also pins the
/// `GET /workstreams/{id}/events` merge),
/// `http_rest_get_projects_route_is_merged_in`,
/// `http_rest_get_agent_and_skill_catalog_routes_are_merged_in`;
/// the #6003 guard arms live in `http_origin_guard_tests`.
/// `POST /rpc` — JSON-RPC 2.0 dispatch endpoint.
///
/// Why: lets browser clients, curl, and any HTTP-only caller reach the same
/// method surface the STDIO transport exposes, without learning a REST
/// vocabulary per method.
/// What: builds a throwaway [`ConnectionContext`] for this one call (its
/// `notify` receiver is dropped once this function returns, so any handler
/// that queued a notification via it — e.g. `session.attach`'s forwarder —
/// simply stops on its next send; the real HTTP live-streaming path is
/// `GET /sessions/{id}/events`, not this channel), then dispatches the raw
/// request body through `Router::dispatch_json` (single request per call —
/// batch/array bodies are not supported, see module docs). Always returns
/// HTTP 200; JSON-RPC errors, including malformed JSON (mapped to
/// `-32700`), are carried in the response envelope's `error` field rather
/// than the HTTP status.
/// Test: `http_rpc_ping_returns_pong`, `http_rpc_malformed_json_returns_parse_error`.
async
/// `GET /health` — liveness for anyone, identity for a credentialed caller.
///
/// Why: two callers with different rights meet on this one route. The
/// ecosystem convention (trusty-search, trusty-memory) is an unauthenticated
/// `GET /health`, and trusty-console's gateway polls it holding no credential,
/// so the route must keep answering an anonymous caller. But #6472: the
/// payload it used to answer with carried the daemon's pid, its bound project
/// ROOT PATH, and its version — process and filesystem facts an anonymous
/// local caller has no claim on, and a fingerprint for anything probing
/// loopback ports. Splitting by credential rather than by route keeps both the
/// gateway poll and `tcode tui`'s auto-attach working: the TUI reads the same
/// credential file the daemon writes, so it still gets the `binding` it needs
/// (#4512) to refuse a daemon bound to a different project.
///
/// What: an anonymous caller gets [`liveness_payload`] — `{"status":"ok"}`,
/// nothing else. A caller the guard marked [`Authenticated`] gets
/// [`health_payload`], the exact payload the `health` JSON-RPC method returns,
/// unchanged. Always HTTP 200 either way; a health route must not turn a
/// missing credential into a failure, or every liveness poller would read a
/// live daemon as down.
/// Test: `health_is_public_but_discloses_only_liveness`,
/// `health_with_a_credential_still_reports_pid_and_binding`,
/// `http_health_matches_jsonrpc_health_payload`,
/// `http_health_reports_the_daemons_project_binding`.
async
/// `POST /auth/sse-ticket` — exchange a bearer credential for a single-use SSE
/// ticket.
///
/// Why: the two SSE routes (`GET /sessions/{id}/events`,
/// `GET /workstreams/{id}/events`) are opened by browser `EventSource`, which
/// cannot attach an `Authorization` header. Putting the durable token in the
/// query string instead would write it into every access log and tracing span;
/// a ticket that expires in seconds and dies on first use does not carry that
/// cost. Reaching this route already requires the header, so a ticket is never
/// a way IN — only a way to carry an existing right onto a URL.
/// What: takes the path the caller means to open (`?path=`), refuses anything
/// that is not one of this daemon's two SSE routes, and mints a ticket bound to
/// that exact path. The route is NOT public, so an anonymous `POST` is `401`ed
/// by the guard before this function runs.
///
/// The binding is what keeps a ticket cheap to leak. Unbound, a ticket read
/// from a trace log within its TTL bought one arbitrary authenticated request —
/// `POST /rpc`, the whole method surface, included. Bound, it buys the one
/// stream its holder already had the credential to open. The route allowlist
/// lives here rather than in `trusty_common` because it is this daemon's route
/// table, not a property of the guard.
/// Test: `http_auth_tests::sse_ticket_requires_a_credential`,
/// `http_auth_tests::an_sse_ticket_opens_one_stream_then_is_spent`,
/// `http_auth_tests::a_ticket_cannot_be_minted_for_a_non_sse_path`.
async
/// The `?path=` a ticket request names.
/// Is `path` one of the two SSE streams a ticket may open?
///
/// Why: a ticket is only as narrow as the set of paths it can be minted for. A
/// handler that minted for whatever it was handed would give back the arbitrary
/// authenticated request the binding exists to remove.
/// What: exact shape match on `/sessions/{id}/events` and
/// `/workstreams/{id}/events` — three segments, the first and last fixed, the
/// id non-empty and free of `/`. Anything else, `/rpc` included, is refused.
/// Test: `http_auth_tests::a_ticket_cannot_be_minted_for_a_non_sse_path`.
/// `GET /sessions/{id}/events` — Server-Sent Events stream of a session's
/// ring-buffer replay followed by its live events.
///
/// Why: HTTP's real live-streaming mechanism for `session.attach` (see
/// module docs for why `POST /rpc` itself can't hold the stream open).
/// Multiple concurrent `GET`s against the same session id each get their
/// own subscription (multi-client attach, vision spec Axiom 4) — no extra
/// bookkeeping needed because `crate::events::subscribe()` already hands
/// out an independent `broadcast::Receiver` per call.
/// What: 404 with a JSON-RPC error envelope if the session id is unknown
/// (`SessionRegistry::replay` -> `session_not_found`, spec-mapped HTTP
/// status per §13.2). Otherwise: an SSE stream that first emits the
/// ring-buffer replay (oldest-first) then chains the live bus, filtered to
/// this `session_id`, forever (until the client disconnects — which is
/// "detach" for HTTP).
/// Test: `http_session_events_sse_streams_replay_then_live_event`,
/// `http_session_events_sse_unknown_session_returns_404`.
async
/// Serialise one `crate::events::SessionEventEnvelope` as an SSE `data:`
/// frame.
///
/// Why: centralises the (practically infallible — the envelope has no
/// untagged maps or non-string keys) JSON encoding so `session_events_sse`
/// doesn't repeat the fallback logic at two call sites. Sending the full
/// envelope (not just the bare `event`) means the HTTP SSE transport carries
/// exactly the same `session_id`/`seq`/`at`/`kind`/`event` shape the STDIO
/// `session.event` notification does (#2055 requirement: both transports
/// carry the full taxonomy with the envelope).
/// What: `SseEvent::default().json_data(envelope)`, falling back to an empty
/// JSON object on the (unreachable in practice) serialisation error rather
/// than `unwrap`ing.
/// Bind a `TcpListener` and serve `POST /rpc` + `GET /health` +
/// `GET /sessions/{id}/events` until SIGTERM/SIGINT (graceful) or an
/// internal axum error.
///
/// Why: the top-level entry point `crate::serve::run_http` delegates to this
/// once the router and session registry are assembled.
/// What: binds `127.0.0.1:port` (`port = 0` lets the OS assign an ephemeral
/// port), logs the real bound address to stderr — mirroring
/// `trusty-memory`'s `run_http_on`, and never touching stdout — then serves
/// with `trusty_common::shutdown_signal()` installed via
/// `with_graceful_shutdown`: SIGTERM/SIGINT stop new connections and drain
/// in-flight requests before this function returns (issue #534 convention).
/// Test: not directly unit-tested (would require a real socket + a real
/// signal); [`build_axum_router`]'s tests cover the routing/dispatch logic
/// this serves.
pub async