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
//! `POST`/`PUT`/`DELETE /sessions*` REST routes over the `session.*`
//! JSON-RPC methods (#2983 Slice 3 — write routes).
//!
//! Why: Slice 2 (`super::sessions`) covered the five read-only `GET`
//! routes; a REST client also needs to mint sessions, drive them, and
//! manage their goal slots without dropping to raw JSON-RPC. Every handler
//! below stays as thin as Slice 2's — extract path/body, build the
//! JSON-RPC `params`, call `super::respond` (or [`respond_created`] for the
//! one route that needs a non-200 success status) — because the actual
//! behaviour (session lookup, validation, `-32007 session_not_found`
//! mapping, …) already lives in `crate::session::protocol`'s handlers;
//! duplicating it here as bespoke axum logic would fork the two surfaces
//! (see `super` module docs).
//! What: [`routes`] builds a standalone `axum::Router<()>` (its own
//! `SessionsWriteState` carrying just the `Arc<Router>`, `with_state`-erased
//! before return, mirroring `super::sessions::SessionsState`) mapping:
//! - `POST /sessions` -> `session.create` (`201 Created` — this route
//! mints a brand-new resource, so it follows the standard REST
//! creation convention rather than Slice 1-2's uniform `200`/mapped-4xx;
//! every other route below acts on an EXISTING resource, so it keeps
//! `200` on success like every Slice 2 route)
//! - `POST /sessions/{id}/messages` -> `session.send`
//! - `POST /sessions/{id}/cancel` -> `session.cancel`
//! - `PUT /sessions/{id}/goal` -> `session.set_goal`
//! - `DELETE /sessions/{id}/goal` -> `session.clear_goal`
//!
//! Request bodies deserialize into private structs shaped identically to
//! the RPC layer's own `params` structs (`crate::session::protocol`'s
//! `CreateParams`/`SendParams`, `protocol_goals`'s
//! `SetGoalParams`/`ClearGoalParams`) minus whatever the URL path already
//! supplies (`session_id`) — a malformed body never reaches a handler at
//! all; axum's `Json<T>` extractor rejects it before the handler runs
//! (`400`/`422`, see module docs on [`super`]), so no handler here
//! hand-rolls body validation.
//! `crate::serve::http::build_axum_router` merges this into the daemon's
//! main router alongside `POST /rpc`, `GET /health`,
//! `GET /sessions/{id}/events`, and `super::sessions`'s read routes — none
//! of those paths collide with the ones here (`PUT`/`DELETE
//! /sessions/{id}/goal` share a path with different methods, which axum
//! routes independently).
//! Test: `tests::*` (`sessions_write_tests.rs`, split out purely for the
//! 500-SLOC cap — see `crate::session::registry`'s identical
//! `#[path = "registry_tests.rs"]` split for the established convention).
use Arc;
use Json;
use Router as AxumRouter;
use ;
use StatusCode;
use ;
use Deserialize;
use ;
use Response;
use crateRouter;
use ;
/// Shared axum state for every route in this module: just the JSON-RPC
/// router, mirroring `super::sessions::SessionsState` — every handler here
/// goes through [`super::respond`]/[`respond_created`] rather than touching
/// `SessionRegistry` directly.
/// Build the `POST`/`PUT`/`DELETE /sessions*` write route group.
///
/// Why: kept separate from `crate::serve::http::build_axum_router` so this
/// resource group is unit-testable via `tower::util::ServiceExt::oneshot`
/// on its own, exactly like `super::sessions::routes`.
/// What: five write routes (see module docs), all sharing one
/// `SessionsWriteState { router }`, `with_state`-erased to
/// `axum::Router<()>` so the caller can `.merge()` it alongside
/// `super::sessions::routes` into the daemon's main router.
/// Test: `tests::create_session_returns_201_with_running_session`.
/// Request body for `POST /sessions`, shaped like `session::protocol`'s
/// private `CreateParams` (`project` stays a `String` here — it is
/// forwarded verbatim into the RPC `params` `Value`, and `session.create`'s
/// own `PathBuf` deserialization applies identically either way).
/// Request body for `POST /sessions/{id}/messages` — `session_id` comes
/// from the path, so only `input` remains.
/// Request body for `PUT /sessions/{id}/goal` — `session_id` comes from the
/// path, so only `slot`/`text` remain (mirrors `protocol_goals`'s private
/// `SetGoalParams`).
/// Request body for `DELETE /sessions/{id}/goal` — `session_id` comes from
/// the path, so only `slot` remains (mirrors `protocol_goals`'s private
/// `ClearGoalParams`). `DELETE` carrying a JSON body is unusual but valid
/// HTTP; it keeps this route's shape symmetric with `PUT`'s rather than
/// introducing a query-string convention nowhere else in this crate uses.
/// Like [`super::respond`] but reports `201 Created` on success instead of
/// the implicit `200` `Json<Value>` carries — the one route in this module
/// that creates a brand-new resource rather than acting on an existing one.
///
/// Why: every other handler in this crate returns [`RestResult`], whose
/// `Ok` arm is a bare `Json<Value>` (200 via axum's blanket `IntoResponse`);
/// `POST /sessions` needs a different status on the SAME success path
/// without duplicating [`respond`]'s error-mapping logic.
/// What: delegates to [`respond`], then rewraps a successful `Json<Value>`
/// as `(StatusCode::CREATED, Json<Value>)`; the error arm is untouched —
/// `rpc_error_to_status` already returns the correct 4xx/5xx.
/// Test: `tests::create_session_returns_201_with_running_session`.
async
/// `POST /sessions` -> `session.create`.
///
/// Why: the write half of the resource `GET /sessions` (Slice 2) already
/// enumerates — the only way a REST-only client can bring a session into
/// existence at all.
/// What: `201 Created` with the new `Session` JSON on success; `400` if
/// `task` is empty or `project` names no real directory (`-32003
/// invalid_argument`, see `session::protocol::create`'s docs on the AC-16.2
/// project-path reconciliation).
/// Test: `tests::create_session_returns_201_with_running_session`,
/// `tests::create_session_empty_task_returns_400`,
/// `tests::create_session_malformed_body_returns_400`.
async
/// `POST /sessions/{id}/messages` -> `session.send`.
///
/// Why: the client -> daemon input path as a REST resource — a "message"
/// added to the session's input stream.
/// What: `200` with `{"acknowledged": true}` on success; `404` for an
/// unknown `id` (`-32007 session_not_found`).
/// Test: `tests::send_message_returns_200_acknowledged`,
/// `tests::send_message_missing_session_returns_404`,
/// `tests::send_message_malformed_body_returns_400`.
async
/// `POST /sessions/{id}/cancel` -> `session.cancel`.
///
/// Why: explicit REST termination signal, mirroring the JSON-RPC method's
/// cooperative-vs-immediate cancel semantics (see
/// `session::protocol::cancel`'s docs) — this route never needs a body,
/// `session_id` from the path is the whole request.
/// What: `200` with the (possibly still-`running`, cooperative-cancel-
/// pending) `Session` snapshot on success; `404` for an unknown `id`.
/// Test: `tests::cancel_session_returns_200_with_session_json`,
/// `tests::cancel_session_missing_session_returns_404`,
/// `tests::cancel_session_is_idempotent`.
async
/// `PUT /sessions/{id}/goal` -> `session.set_goal`.
///
/// Why: the operator-facing REST entry point onto a session's 5 fixed goal
/// slots (see `protocol_goals` module docs) — `PUT` because it is a full
/// replace-by-slot, idempotent write.
/// What: `200` with `{}` on success; `404` for an unknown `id`; `400` if
/// the session has no transcript yet or `slot` is out of range (`-32003
/// invalid_argument`, see `protocol_goals::set_goal`'s docs).
/// Test: `tests::set_goal_returns_200_empty_object`,
/// `tests::set_goal_missing_session_returns_404`,
/// `tests::set_goal_malformed_body_returns_400`.
async
/// `DELETE /sessions/{id}/goal` -> `session.clear_goal`.
///
/// Why: the operator-facing REST counterpart to `PUT` above.
/// What: `200` with `{}` on success — clearing an already-empty slot is
/// idempotent success, matching `protocol_goals::clear_goal`'s convention,
/// so this route never needs its own idempotency special-case; `404` for an
/// unknown `id`; `400` for an out-of-range `slot` (`-32003
/// invalid_argument`).
/// Test: `tests::clear_goal_returns_200_empty_object`,
/// `tests::clear_goal_missing_session_returns_404`,
/// `tests::clear_goal_malformed_body_returns_400`.
async