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
//! REST resource gateway over the JSON-RPC method registry (#2983, #587,
//! Slice 1 bridge + Slice 2 `session.*` read routes).
//!
//! Why: the vision spec (#587) wants `tcode serve --http` to expose a
//! conventional REST resource vocabulary (`GET /projects`, `POST
//! /sessions`, …) alongside the existing JSON-RPC `POST /rpc` endpoint, but
//! every one of those resources is already backed by a registered JSON-RPC
//! method (`session.create`, `task.run`, …). Re-implementing the same
//! business logic a second time as bespoke axum handlers would fork the two
//! surfaces the moment either one changed — exactly the drift `trusty-code`'s
//! `Router` was built to prevent between the STDIO and HTTP JSON-RPC
//! transports (see `crate::jsonrpc` module docs). This module is
//! `trusty-memory`'s "Pattern C" (`crates/trusty-memory/src/web/rpc.rs`:
//! one shared dispatch seam, many transports) applied one layer further:
//! a REST route handler will call [`call`] instead of building its own
//! `Request`, so a REST resource and its JSON-RPC method twin always run
//! the exact same handler.
//!
//! What: [`call`] synthesizes a JSON-RPC [`Request`] from a `method` +
//! `params` pair and drives it through [`Router::dispatch`], unwrapping the
//! `Response` envelope back into a plain `Result<Value, RpcError>` — the
//! shape a REST handler wants (`?` onto an HTTP error), rather than the
//! always-200 envelope `POST /rpc` returns. [`rpc_error_to_status`] maps an
//! `RpcError` onto the real HTTP status a REST response should carry,
//! deliberately diverging from `POST /rpc`'s "always 200, error in the
//! envelope" convention — matching how `GET /sessions/{id}/events`
//! (`crate::serve::http::session_events_sse`) already returns a real `404`
//! for `session_not_found` rather than a 200-wrapped error. [`respond`] is
//! the one place that glues those two together into the `Result<Json<Value>,
//! (StatusCode, Json<Response>)>` shape every axum REST handler returns, so
//! a route handler is never more than "extract params, call `respond`".
//! [`throwaway_ctx`] builds the per-request [`ConnectionContext`] every REST
//! handler needs to call [`call`] with — identical in spirit to
//! `crate::serve::http::rpc_handler`'s own throwaway context, since a REST
//! call is exactly as one-shot as a `POST /rpc` call.
//!
//! Slice 1 wired no axum routes — only this bridge. Slice 2
//! ([`sessions`]) adds the first concrete resource group: `GET /sessions`,
//! `GET /sessions/{id}`, `GET /sessions/{id}/transcript`,
//! `GET /sessions/{id}/readiness`, `GET /sessions/{id}/goals`, each a thin
//! handler calling [`respond`] against its `session.*` JSON-RPC twin.
//! Slice 3 ([`sessions_write`]) adds the `session.*` WRITE routes:
//! `POST /sessions`, `POST /sessions/{id}/messages`,
//! `POST /sessions/{id}/cancel`, `PUT /sessions/{id}/goal`,
//! `DELETE /sessions/{id}/goal`. Slice 4 ([`tasks`]) adds
//! `POST /tasks` -> `task.run`. Slice 5 ([`fs`]) adds
//! `GET /fs` -> `fs.list_dir`. Slice 6 ([`agents`]) adds
//! `GET /sessions/{id}/agents` -> `session.get_agents`. Slice 7
//! ([`search_audit`]) adds `GET /sessions/{id}/search-audit` ->
//! `session.get_search_audit` (issue #3072). [`workstreams`] adds the full
//! `workstream.*` surface: `POST /workstreams`, `GET /workstreams/{id}`,
//! `GET /workstreams`, `POST /workstreams/{id}/close` (issue #3295, epic
//! #3292) and `POST /workstreams/{id}/activate`,
//! `POST /workstreams/{id}/deactivate` (DOC-48 §5.2/§6, issue #3294) — see
//! its module docs for why the paths are unprefixed rather than DOC-48
//! §5.2's literal `/api/v1/workstreams`. [`projects`] adds `GET /projects`
//! -> `fs.list_projects` (issue #3365 — the GUI's workstream-first
//! project-picker modal's roster data source). [`agent_catalog`]/
//! [`skill_catalog`] (issue #3449) add the Foundry GUI's Agents/Skills
//! management tabs' full surface — `GET`/`POST /agents`,
//! `DELETE /agents/{name}` and the `skills` twin — over
//! `crate::agents::protocol`/`crate::skills::protocol`; distinct from this
//! module's own [`agents`] group (`GET /sessions/{id}/agents`, the
//! per-session LIVE roster, unrelated to the disk/embedded catalog these two
//! new groups manage). Every slice reuses [`respond`]/[`throwaway_ctx`]
//! rather than reimplementing the glue.
//!
//! Test: `tests::*` — a success round-trip, an error round-trip, and one
//! assertion per `rpc_error_to_status` mapping. See `sessions::tests` for
//! the Slice 2 route-level coverage.
use Json;
use StatusCode;
use Value;
use ;
use crate;
/// Call a registered JSON-RPC method from a REST handler, in-process.
///
/// Why: the single seam every future REST resource handler goes through, so
/// a REST route and its JSON-RPC method twin are provably the same code
/// path — never two handwritten implementations of the same business logic
/// (see module docs).
/// What: synthesizes a `Request { jsonrpc: "2.0", id: Some(0), method,
/// params }` — the `id` is a fixed synthetic value because a REST caller
/// never observes it; `dispatch` never suppresses a response as long as
/// `id` is `Some(_)` (see `Router::dispatch`'s notification rule) — calls
/// `router.dispatch(req, ctx)`, and unwraps the `Response` envelope: `Ok`
/// when `result` is set, `Err` reconstructing the `RpcError` from
/// `Response::error` otherwise. If `dispatch` ever returned a `Response`
/// with neither `result` nor `error` set — a `Router::dispatch` bug, not a
/// caller-triggerable condition, and provably unreachable through this
/// bridge since `call` always sets a non-`None` `id`, so `dispatch` never
/// returns `Response::suppressed()` — this falls back to `Ok(Value::Null)`
/// rather than panicking (no `unwrap`/`expect` in library code). That would
/// surface as a silently-wrong 200 response, not a panic.
/// Test: `call_success_returns_handler_result`, `call_error_returns_rpc_error`.
pub async
/// Map an [`RpcError`] onto the HTTP status a REST response should carry.
///
/// Why: `POST /rpc` always returns HTTP 200 with the JSON-RPC error carried
/// in the envelope — appropriate for a JSON-RPC transport, but REST callers
/// (and REST tooling: curl, browsers, API gateways) expect the status line
/// itself to say "not found" / "forbidden" / etc. This mirrors the mapping
/// `crate::serve::http::session_events_sse` already applies by hand for
/// `session_not_found` -> 404, generalised to every domain error code
/// `crate::jsonrpc::error::RpcError` currently defines (vision spec §13.2).
/// What: matches on the numeric JSON-RPC error code: `-32002 not_found` ->
/// 404, `-32001 permission_denied` -> 403, `-32003 invalid_argument` and
/// `-32602 Invalid params` -> 400, `-32007 session_not_found` -> 404,
/// `-32008 active_conflict` (DOC-48 §5.2, issue #3294) and `-32009
/// already_exists` (issue #3449) -> 409, `-32603
/// Internal error` -> 500. Any other/future code (including
/// `-32601`/`-32600`, which a REST handler should never see because it
/// calls `call` with a hardcoded, known-good method name) falls back to 500
/// — an unmapped domain error is a bug to fix by adding a mapping, not a
/// client-facing 4xx.
/// Test: `status_mapping_*` — one case per mapped code, plus the unknown ->
/// 500 fallback.
/// Build a throwaway [`ConnectionContext`] for a single REST call.
///
/// Why: every REST handler calls [`call`], which requires a
/// `&ConnectionContext`, but a REST request is exactly as one-shot as an
/// HTTP `POST /rpc` call — there is no live connection for a handler like
/// `session.attach` to push notifications back to (that path is the
/// dedicated SSE route, `crate::serve::http::session_events_sse`). Mirrors
/// `crate::serve::http::rpc_handler`'s identical throwaway-channel
/// construction so the two transports build a `ConnectionContext` the same
/// way.
/// What: a fresh `mpsc::unbounded_channel()`, receiver immediately dropped —
/// any handler that queues a notification onto `notify` simply stops on its
/// next send once this function returns.
/// Test: exercised indirectly by every `sessions::tests::*` route test (a
/// route only responds at all if its `ConnectionContext` is constructible).
pub
/// A REST handler's return type: the JSON-RPC result as a `Json` body on
/// success, or a real HTTP status plus a JSON-RPC error envelope on failure.
///
/// Why: `(StatusCode, Json<Response>)` already implements axum's
/// `IntoResponse` for the error arm, so every route handler can just return
/// this type from [`respond`] without a bespoke error type.
pub type RestResult = ;
/// Call `method` through [`call`] and convert the result into a
/// [`RestResult`] — the single glue point every Slice 2+ REST handler goes
/// through.
///
/// Why: keeps route handlers themselves down to "extract path/query params,
/// build `params`, call `respond`" with zero business logic and zero
/// repeated error-mapping boilerplate (see module docs).
/// What: `Ok(value)` becomes `Ok(Json(value))`; `Err(rpc_err)` becomes
/// `Err((rpc_error_to_status(&rpc_err), Json(Response::err(None,
/// rpc_err.code, rpc_err.message))))` — `data` is intentionally dropped from
/// the REST envelope, matching `crate::serve::http::session_events_sse`'s
/// existing `session_not_found` -> 404 mapping (the `id` is always `None`
/// because a REST error response has no JSON-RPC request id to echo).
/// Test: `sessions::tests::get_session_found_returns_200_with_session_json`,
/// `sessions::tests::get_session_missing_returns_404_session_not_found`.
pub async