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
//! Daemon domain error type.
//!
//! Why: the HTTP handlers previously returned bare `StatusCode`s, which buries
//! the *reason* a request failed at the call site and forces every handler to
//! repeat the `.ok_or(StatusCode::NOT_FOUND)` boilerplate. A single domain
//! error enum lets the domain services (in `services/`) speak in terms of what
//! went wrong — a missing session, a blocked tool call — while one
//! `IntoResponse` impl maps each variant to the right HTTP status. Business
//! logic stays HTTP-agnostic; the transport mapping lives in one place.
//! What: [`DaemonError`] enumerates every way a daemon request can fail, with a
//! `thiserror`-derived `Display` and an `axum::IntoResponse` that picks the
//! status code and renders a `{ "error": <message> }` body.
//! Test: `error_status_codes_map` asserts the variant → status mapping.
use axum::Json;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use thiserror::Error;
/// A failure surfaced by a daemon domain service or request handler.
///
/// Why: domain services must report *why* an operation failed without knowing
/// they are behind HTTP; a typed enum keeps the failure self-describing and
/// lets `?` propagate it cleanly from a service into a handler.
/// What: one variant per failure mode the daemon can produce; each carries the
/// context needed to render an operator-facing message.
/// Test: `error_status_codes_map`, `error_messages_include_context`.
#[derive(Debug, Error)]
pub enum DaemonError {
/// No session matched the supplied id or friendly name.
#[error("session not found: {id}")]
SessionNotFound {
/// The id or name that failed to resolve.
id: String,
},
/// The session exists but is in a state that forbids the operation.
#[error("session not active: {id} (status: {status})")]
SessionNotActive {
/// The session id or name.
id: String,
/// The session's current status, lowercased.
status: String,
},
/// The overseer halted the request before it could proceed.
#[error("overseer blocked: {reason}")]
OverseerBlocked {
/// The overseer's stated reason for the block.
reason: String,
},
/// A tmux operation could not be completed.
#[error("tmux unavailable: {0}")]
TmuxUnavailable(String),
/// The request body or parameters were malformed.
#[error("invalid request: {0}")]
InvalidRequest(String),
/// No checkpoint matched the supplied id.
#[error("checkpoint not found: {id}")]
CheckpointNotFound {
/// The checkpoint id that failed to resolve.
id: String,
},
/// No Deliverable matched the supplied id (within the named project).
///
/// Why: the Deliverable CRUD routes (#2378) need a 404 distinct from the
/// session/checkpoint not-founds so the error message names the right
/// resource.
/// What: carries the id (or malformed id string); maps to HTTP 404.
/// Test: `error_status_codes_map`, and the deliverable route tests.
#[error("deliverable not found: {id}")]
DeliverableNotFound {
/// The Deliverable id that failed to resolve.
id: String,
},
/// No Milestone matched the supplied id (within the named project).
#[error("milestone not found: {id}")]
MilestoneNotFound {
/// The Milestone id that failed to resolve.
id: String,
},
/// No registered project matches the given `repo_url`, so a Deliverable
/// id cannot be scoped against it (#2379 review MEDIUM).
///
/// Why: distinct from [`DeliverableNotFound`](Self::DeliverableNotFound) —
/// the Deliverable itself may well exist; what's missing is the PROJECT
/// identity to scope it against. Returning `DeliverableNotFound` here
/// would misleadingly imply the id itself was bad, when the actual gap is
/// "no project is registered for this repo at all".
/// What: carries the `repo_url` that failed to resolve; maps to HTTP 404
/// (there is genuinely no project resource for this identity).
/// Test: `error_status_codes_map`, and
/// `validate_deliverable_scope_unknown_project_is_404`.
#[error("no registered project matches repo_url {repo_url:?}; cannot scope deliverable")]
ProjectNotFoundForRepoUrl {
/// The `repo_url` that did not resolve to any registered project.
repo_url: String,
},
/// A requested Deliverable status change is not a legal transition (#2380).
///
/// Why: the §10.3 state machine rejects illegal `set-status` requests (e.g.
/// `proposed → complete`). A dedicated variant lets the error body name the
/// legal next states so the caller can self-correct, and maps to 409 Conflict
/// (the requested change conflicts with the record's current state) — the same
/// class as [`SessionNotActive`](Self::SessionNotActive).
/// What: carries the `from`/`to` labels and the legal `allowed` next states;
/// [`into_response`](Self::into_response) surfaces `allowed` as a JSON array.
/// Test: `error_status_codes_map`, `invalid_transition_body_lists_allowed`.
#[error(
"invalid status transition {from} \u{2192} {to}; legal next states from {from}: [{}]",
allowed.join(", ")
)]
InvalidTransition {
/// The Deliverable's current status.
from: String,
/// The requested (rejected) target status.
to: String,
/// The states that would have been legal transitions from `from`.
allowed: Vec<String>,
},
/// A bot pairing code was wrong or had expired.
#[error("pair code invalid or expired")]
InvalidPairCode,
/// An unexpected internal failure (IO, serialization, ...).
#[error("internal error: {0}")]
Internal(String),
/// A requested capability is not configured on this daemon.
#[error("service unavailable: {0}")]
ServiceUnavailable(String),
/// The request was rejected by a security guard (e.g. the cross-origin CSRF
/// guard on the browser-facing, action-capable chat endpoint).
///
/// Why: distinct from [`OverseerBlocked`](Self::OverseerBlocked) (which is a
/// policy decision about a *session*), this is a transport-level rejection of
/// the *request itself* — it never reached business logic. A dedicated variant
/// keeps the operator message self-describing and maps cleanly to 403.
/// What: carries a human-readable reason; maps to HTTP 403 Forbidden.
/// Test: `error_status_codes_map`, and the origin-guard handler tests.
#[error("forbidden: {0}")]
Forbidden(String),
/// The request is syntactically valid but semantically un-fulfillable.
///
/// Why: a precondition the daemon cannot satisfy (e.g. `POST /sessions`
/// in spawn mode when the `claude` binary is missing from `PATH`) is not
/// a malformed request (`400`) and not a missing resource (`404`) — it
/// maps to HTTP 422 Unprocessable Entity, which exactly describes
/// "well-formed but unprocessable".
/// What: carries a human-readable reason.
/// Test: `error_status_codes_map`, `spawn_session_without_claude_returns_422`.
#[error("unprocessable: {0}")]
Unprocessable(String),
}
impl DaemonError {
/// The HTTP status this error maps to.
///
/// Why: the `IntoResponse` impl and any caller inspecting the error (tests,
/// the MCP backend) need the status without re-deriving the mapping.
/// What: returns the canonical status per variant.
/// Test: `error_status_codes_map`.
pub fn status(&self) -> StatusCode {
match self {
Self::SessionNotFound { .. }
| Self::CheckpointNotFound { .. }
| Self::DeliverableNotFound { .. }
| Self::MilestoneNotFound { .. }
| Self::ProjectNotFoundForRepoUrl { .. } => StatusCode::NOT_FOUND,
Self::SessionNotActive { .. } | Self::InvalidTransition { .. } => StatusCode::CONFLICT,
Self::OverseerBlocked { .. } | Self::Forbidden(_) => StatusCode::FORBIDDEN,
Self::InvalidRequest(_) | Self::InvalidPairCode => StatusCode::BAD_REQUEST,
Self::TmuxUnavailable(_) | Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::ServiceUnavailable(_) => StatusCode::SERVICE_UNAVAILABLE,
Self::Unprocessable(_) => StatusCode::UNPROCESSABLE_ENTITY,
}
}
}
/// Render a [`DaemonError`] as an HTTP response.
///
/// Why: axum handlers return `Result<_, DaemonError>`; this is the single seam
/// that turns a domain failure into a status code plus a JSON error body.
/// What: emits `(status, Json({ "error": <message> }))`.
/// Test: exercised indirectly by every handler test that asserts an error
/// status; the mapping itself is unit-tested in `error_status_codes_map`.
impl IntoResponse for DaemonError {
fn into_response(self) -> Response {
let status = self.status();
// #2380: an invalid transition surfaces the legal next states as a
// structured `allowed_next` array so the caller can self-correct without
// parsing the message string.
let body = match &self {
Self::InvalidTransition { from, to, allowed } => Json(serde_json::json!({
"error": self.to_string(),
"from": from,
"to": to,
"allowed_next": allowed,
})),
_ => Json(serde_json::json!({ "error": self.to_string() })),
};
(status, body).into_response()
}
}
impl From<std::io::Error> for DaemonError {
fn from(e: std::io::Error) -> Self {
Self::Internal(e.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_status_codes_map() {
// Each variant must map to the documented HTTP status.
assert_eq!(
DaemonError::SessionNotFound { id: "x".into() }.status(),
StatusCode::NOT_FOUND
);
assert_eq!(
DaemonError::CheckpointNotFound { id: "x".into() }.status(),
StatusCode::NOT_FOUND
);
assert_eq!(
DaemonError::SessionNotActive {
id: "x".into(),
status: "stopped".into(),
}
.status(),
StatusCode::CONFLICT
);
assert_eq!(
DaemonError::OverseerBlocked { reason: "x".into() }.status(),
StatusCode::FORBIDDEN
);
assert_eq!(
DaemonError::InvalidRequest("x".into()).status(),
StatusCode::BAD_REQUEST
);
assert_eq!(
DaemonError::InvalidPairCode.status(),
StatusCode::BAD_REQUEST
);
assert_eq!(
DaemonError::TmuxUnavailable("x".into()).status(),
StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
DaemonError::Internal("x".into()).status(),
StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
DaemonError::ServiceUnavailable("x".into()).status(),
StatusCode::SERVICE_UNAVAILABLE
);
assert_eq!(
DaemonError::Forbidden("x".into()).status(),
StatusCode::FORBIDDEN
);
assert_eq!(
DaemonError::Unprocessable("x".into()).status(),
StatusCode::UNPROCESSABLE_ENTITY
);
assert_eq!(
DaemonError::DeliverableNotFound { id: "x".into() }.status(),
StatusCode::NOT_FOUND
);
assert_eq!(
DaemonError::MilestoneNotFound { id: "x".into() }.status(),
StatusCode::NOT_FOUND
);
assert_eq!(
DaemonError::ProjectNotFoundForRepoUrl {
repo_url: "x".into()
}
.status(),
StatusCode::NOT_FOUND
);
assert_eq!(
DaemonError::InvalidTransition {
from: "proposed".into(),
to: "complete".into(),
allowed: vec!["in-progress".into()],
}
.status(),
StatusCode::CONFLICT
);
}
#[test]
fn invalid_transition_message_lists_allowed() {
// The Display must name the from-state, to-state, and legal next states
// so an operator reading the plain error string can self-correct.
let e = DaemonError::InvalidTransition {
from: "proposed".into(),
to: "complete".into(),
allowed: vec!["in-progress".into()],
};
let msg = e.to_string();
assert!(msg.contains("proposed"), "{msg}");
assert!(msg.contains("complete"), "{msg}");
assert!(msg.contains("in-progress"), "{msg}");
}
#[test]
fn error_messages_include_context() {
// The `Display` impl must surface the contextual fields so an operator
// reading the JSON error body can tell what failed.
let e = DaemonError::SessionNotFound {
id: "tmpm-blue-fox".into(),
};
assert!(e.to_string().contains("tmpm-blue-fox"));
let e = DaemonError::SessionNotActive {
id: "abc".into(),
status: "stopped".into(),
};
assert!(e.to_string().contains("abc"));
assert!(e.to_string().contains("stopped"));
}
#[test]
fn project_not_found_message_names_repo_url_not_deliverable() {
// #2379 review MEDIUM: the message must talk about the missing
// PROJECT, never imply the deliverable id itself was bad.
let e = DaemonError::ProjectNotFoundForRepoUrl {
repo_url: "/local/path/checkout".into(),
};
let msg = e.to_string();
assert!(msg.contains("/local/path/checkout"), "{msg}");
assert!(msg.contains("project"), "{msg}");
assert!(
!msg.contains("deliverable not found"),
"must not read as a deliverable-not-found error: {msg}"
);
}
}