Skip to main content

laser_wire/
result.rs

1// The unified result-code space. Each managed surface keeps its own typed error
2// for the detail a caller needs, but every one of those errors also projects
3// onto one logical `ResultCode` here, so a generic client, the HTTP status
4// mapper, and a cross-language port all dispatch on one small dictionary
5// instead of parsing per-surface strings. The codes are a pinned cross-repo
6// contract, and an unknown code from a newer peer rides through as
7// `Unrecognized` rather than failing, the same forward-compat shape the growable
8// u8 dictionaries use.
9
10use crate::agent_workflow::AgentError;
11use crate::checkpoint::CheckpointError;
12use crate::fork::ForkError;
13use crate::graph::GraphError;
14use crate::kv::KvError;
15use crate::query::QueryError;
16use serde::{Deserialize, Serialize};
17
18/// One logical outcome code spanning query, key-value, fork, and browse. Built
19/// from a surface's typed error via the `From` impls below. The typed error
20/// keeps the detail, this is the shared classification.
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(rename_all = "snake_case")]
23#[non_exhaustive]
24pub enum ResultCode {
25    /// The operation succeeded (no error to classify).
26    Ok,
27    /// The operation, or the managed surface, is not available here.
28    Unsupported,
29    /// A named entity (index, fork, key) does not exist.
30    NotFound,
31    /// The request was malformed or a field was out of range.
32    InvalidArgument,
33    /// A result or value exceeded a size cap.
34    TooLarge,
35    /// A precondition lost a race (a compare-and-swap version mismatch, a fork
36    /// promote/squash conflict).
37    Conflict,
38    /// A consistency level could not be met within the deadline (the read model
39    /// is still catching up).
40    Stale,
41    /// The wire op version is not accepted by this peer.
42    VersionSkew,
43    /// No credential, or an invalid one: the caller is not authenticated.
44    Unauthenticated,
45    /// The managed backend failed or was unreachable.
46    Backend,
47    /// Authenticated, but the grant needed for the operation is missing.
48    Forbidden,
49    /// Authenticated, but the operation needs a stronger authentication (a
50    /// step-up) than the caller currently holds.
51    StepUpRequired,
52    /// The request could not be served right now, and the same request may
53    /// succeed on a later attempt: the store was momentarily out of reach, a
54    /// connection was refused or dropped, a concurrency conflict was aborted, or
55    /// a resource limit was hit. Distinct from [`Backend`](Self::Backend), which
56    /// is a fault the caller cannot retry away. This is the one code a generic
57    /// client may safely retry with backoff, so it never has to parse a message
58    /// to decide (see [`is_retryable`](Self::is_retryable)).
59    Unavailable,
60    /// The query exceeded a server-enforced resource budget.
61    ResourceLimit,
62    /// The query was cancelled through its execution identity.
63    Cancelled,
64    /// The query did not finish before its absolute deadline.
65    DeadlineExceeded,
66    /// The requested historical snapshot is no longer retained.
67    ExpiredSnapshot,
68    /// The requested destination or backend generation is no longer current.
69    StaleGeneration,
70    /// The resolved target is not ready to serve queries.
71    TargetUnavailable,
72    /// A code from a newer peer this build does not name. Decodes and re-encodes
73    /// byte-for-byte so an old build relays it rather than failing. Only a value
74    /// outside the named range (19 and up) should ever appear here: `from_code`
75    /// never produces `Unrecognized` for `0..=18`, which map to the named
76    /// variants.
77    Unrecognized(u16),
78}
79
80impl ResultCode {
81    /// The pinned numeric code, stable across repos and language ports.
82    pub const fn code(self) -> u16 {
83        match self {
84            ResultCode::Ok => 0,
85            ResultCode::Unsupported => 1,
86            ResultCode::NotFound => 2,
87            ResultCode::InvalidArgument => 3,
88            ResultCode::TooLarge => 4,
89            ResultCode::Conflict => 5,
90            ResultCode::Stale => 6,
91            ResultCode::VersionSkew => 7,
92            ResultCode::Unauthenticated => 8,
93            ResultCode::Backend => 9,
94            ResultCode::Forbidden => 10,
95            ResultCode::StepUpRequired => 11,
96            ResultCode::Unavailable => 12,
97            ResultCode::ResourceLimit => 13,
98            ResultCode::Cancelled => 14,
99            ResultCode::DeadlineExceeded => 15,
100            ResultCode::ExpiredSnapshot => 16,
101            ResultCode::StaleGeneration => 17,
102            ResultCode::TargetUnavailable => 18,
103            ResultCode::Unrecognized(code) => code,
104        }
105    }
106
107    /// The code for a pinned numeric value, where an unknown value becomes
108    /// `Unrecognized` rather than an error.
109    pub const fn from_code(code: u16) -> Self {
110        match code {
111            0 => ResultCode::Ok,
112            1 => ResultCode::Unsupported,
113            2 => ResultCode::NotFound,
114            3 => ResultCode::InvalidArgument,
115            4 => ResultCode::TooLarge,
116            5 => ResultCode::Conflict,
117            6 => ResultCode::Stale,
118            7 => ResultCode::VersionSkew,
119            8 => ResultCode::Unauthenticated,
120            9 => ResultCode::Backend,
121            10 => ResultCode::Forbidden,
122            11 => ResultCode::StepUpRequired,
123            12 => ResultCode::Unavailable,
124            13 => ResultCode::ResourceLimit,
125            14 => ResultCode::Cancelled,
126            15 => ResultCode::DeadlineExceeded,
127            16 => ResultCode::ExpiredSnapshot,
128            17 => ResultCode::StaleGeneration,
129            18 => ResultCode::TargetUnavailable,
130            other => ResultCode::Unrecognized(other),
131        }
132    }
133
134    /// The HTTP status this code maps to, the one mapping every surface shares,
135    /// so a status need not be decided per surface or per route.
136    pub const fn http_status(self) -> u16 {
137        match self {
138            ResultCode::Ok => 200,
139            ResultCode::Unsupported => 501,
140            ResultCode::NotFound => 404,
141            ResultCode::InvalidArgument => 400,
142            ResultCode::TooLarge => 413,
143            ResultCode::Conflict => 409,
144            ResultCode::Stale => 503,
145            ResultCode::VersionSkew => 400,
146            ResultCode::Unauthenticated => 401,
147            ResultCode::Backend => 502,
148            // Authenticated-but-forbidden is 403, distinct from the 401 an
149            // unauthenticated caller gets. Step-up also lands on 403 unless the
150            // HTTP layer has a better challenge status for the chosen scheme.
151            ResultCode::Forbidden => 403,
152            ResultCode::StepUpRequired => 403,
153            // Retry-after territory, the same status a lagging read model gets.
154            ResultCode::Unavailable => 503,
155            ResultCode::ResourceLimit => 429,
156            ResultCode::Cancelled => 409,
157            ResultCode::DeadlineExceeded => 408,
158            ResultCode::ExpiredSnapshot => 410,
159            ResultCode::StaleGeneration => 409,
160            ResultCode::TargetUnavailable => 503,
161            ResultCode::Unrecognized(_) => 500,
162        }
163    }
164
165    /// Whether a caller may retry the identical request and reasonably expect a
166    /// different outcome. True only for the two transient classes: the store was
167    /// momentarily unreachable, or the read model had not caught up yet. Every
168    /// other code needs the request, the credential, or the data to change first,
169    /// so retrying it unchanged only wastes the attempt.
170    pub const fn is_retryable(self) -> bool {
171        matches!(
172            self,
173            ResultCode::Unavailable | ResultCode::Stale | ResultCode::TargetUnavailable
174        )
175    }
176}
177
178/// The canonical surface-agnostic error reply. Every managed surface has its own
179/// typed reply enum (`QueryReply`, `KvReply`, `ForkReply`, `BrowseReply`), but a
180/// server that receives a command code it does not handle (a forwarded
181/// `AGDX_KV_CAS` on a build without compare-and-swap, or any future additive
182/// code) has no one surface to answer in: a query-shaped error reply fails to
183/// decode in a client awaiting a key-value reply, and surfaces as an opaque
184/// transport error instead of a clean classification. This is that fallback. A
185/// server answers an unhandled or unsupported code with a `CommandError`, and a
186/// client that fails to decode the surface's typed reply tries `CommandError`
187/// next, turning the wrong-surface reply into a typed [`ResultCode`].
188#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
189pub struct CommandError {
190    pub code: ResultCode,
191    pub message: String,
192}
193
194impl CommandError {
195    /// A command error from a classified code and a human message.
196    pub fn new(code: ResultCode, message: impl Into<String>) -> Self {
197        Self {
198            code,
199            message: message.into(),
200        }
201    }
202
203    /// The reply for a command code this server does not handle.
204    pub fn unsupported(message: impl Into<String>) -> Self {
205        Self::new(ResultCode::Unsupported, message)
206    }
207}
208
209impl From<&QueryError> for ResultCode {
210    fn from(error: &QueryError) -> Self {
211        match error {
212            QueryError::Unsupported(_) => ResultCode::Unsupported,
213            // The per-source DSL check refused the query: the caller authenticated
214            // but lacks the grant for the named resource, so forbidden not 401.
215            QueryError::Unauthorized(_) => ResultCode::Forbidden,
216            QueryError::IndexNotFound(_) | QueryError::ForkNotFound(_) => ResultCode::NotFound,
217            QueryError::Backend(_) => ResultCode::Backend,
218            QueryError::Unavailable(_) => ResultCode::Unavailable,
219            QueryError::TooLarge { .. } => ResultCode::TooLarge,
220            QueryError::Version { .. } => ResultCode::VersionSkew,
221            QueryError::Stale { .. } => ResultCode::Stale,
222            QueryError::Cancelled { .. } => ResultCode::Cancelled,
223            QueryError::DeadlineExceeded { .. } => ResultCode::DeadlineExceeded,
224            QueryError::ExpiredSnapshot { .. } => ResultCode::ExpiredSnapshot,
225            QueryError::StaleGeneration { .. } => ResultCode::StaleGeneration,
226            QueryError::TargetUnavailable { .. } => ResultCode::TargetUnavailable,
227            QueryError::ResourceLimit { .. } => ResultCode::ResourceLimit,
228        }
229    }
230}
231
232impl From<&KvError> for ResultCode {
233    fn from(error: &KvError) -> Self {
234        match error {
235            KvError::Unsupported(_) => ResultCode::Unsupported,
236            KvError::InvalidKey(_) => ResultCode::InvalidArgument,
237            KvError::InvalidNamespace(_) => ResultCode::InvalidArgument,
238            KvError::TooLarge { .. } => ResultCode::TooLarge,
239            KvError::Backend(_) => ResultCode::Backend,
240            KvError::Unavailable(_) => ResultCode::Unavailable,
241            KvError::Version { .. } => ResultCode::VersionSkew,
242            KvError::VersionConflict { .. } => ResultCode::Conflict,
243            KvError::LeaseLost => ResultCode::Conflict,
244            KvError::NotFound => ResultCode::NotFound,
245            KvError::NotLeader => ResultCode::Unavailable,
246        }
247    }
248}
249
250impl From<&ForkError> for ResultCode {
251    fn from(error: &ForkError) -> Self {
252        match error {
253            ForkError::Unsupported(_) => ResultCode::Unsupported,
254            ForkError::NotFound(_) => ResultCode::NotFound,
255            ForkError::InvalidFork(_) => ResultCode::InvalidArgument,
256            ForkError::Conflict(_) => ResultCode::Conflict,
257            ForkError::Backend(_) => ResultCode::Backend,
258            ForkError::Unavailable(_) => ResultCode::Unavailable,
259            ForkError::Version { .. } => ResultCode::VersionSkew,
260            ForkError::NotLeader => ResultCode::Unavailable,
261        }
262    }
263}
264
265impl From<&GraphError> for ResultCode {
266    fn from(error: &GraphError) -> Self {
267        match error {
268            GraphError::Unsupported(_) => ResultCode::Unsupported,
269            GraphError::Unauthorized(_) => ResultCode::Forbidden,
270            GraphError::InvalidName(_) => ResultCode::InvalidArgument,
271            GraphError::NotFound(_) => ResultCode::NotFound,
272            GraphError::TooLarge { .. } => ResultCode::TooLarge,
273            GraphError::Backend(_) => ResultCode::Backend,
274            GraphError::Unavailable(_) => ResultCode::Unavailable,
275            GraphError::Version { .. } => ResultCode::VersionSkew,
276        }
277    }
278}
279
280impl From<&AgentError> for ResultCode {
281    fn from(error: &AgentError) -> Self {
282        match error {
283            AgentError::Unsupported(_) => ResultCode::Unsupported,
284            AgentError::NotFound(_) => ResultCode::NotFound,
285            AgentError::Invalid(_) => ResultCode::InvalidArgument,
286            AgentError::Backend(_) => ResultCode::Backend,
287            AgentError::Unavailable(_) => ResultCode::Unavailable,
288            AgentError::Version { .. } => ResultCode::VersionSkew,
289            AgentError::NotLeader => ResultCode::Unavailable,
290        }
291    }
292}
293
294impl From<&CheckpointError> for ResultCode {
295    fn from(error: &CheckpointError) -> Self {
296        match error {
297            CheckpointError::Invalid(_) => ResultCode::InvalidArgument,
298            CheckpointError::NotFound => ResultCode::NotFound,
299            CheckpointError::Conflict { .. } | CheckpointError::LeaseLost => ResultCode::Conflict,
300            CheckpointError::Unauthorized => ResultCode::Forbidden,
301            CheckpointError::Unavailable(_) => ResultCode::Unavailable,
302            CheckpointError::Version { .. } => ResultCode::VersionSkew,
303        }
304    }
305}
306
307#[cfg(test)]
308mod tests {
309    use super::*;
310
311    #[test]
312    fn given_result_codes_when_mapped_then_should_round_trip_through_the_numeric_value() {
313        for code in [
314            ResultCode::Ok,
315            ResultCode::Unsupported,
316            ResultCode::NotFound,
317            ResultCode::InvalidArgument,
318            ResultCode::TooLarge,
319            ResultCode::Conflict,
320            ResultCode::Stale,
321            ResultCode::VersionSkew,
322            ResultCode::Unauthenticated,
323            ResultCode::Backend,
324            ResultCode::Forbidden,
325            ResultCode::StepUpRequired,
326            ResultCode::Unavailable,
327        ] {
328            assert_eq!(ResultCode::from_code(code.code()), code);
329        }
330        // An unknown numeric code rides through as Unrecognized.
331        assert_eq!(ResultCode::from_code(900), ResultCode::Unrecognized(900));
332        assert_eq!(ResultCode::Unrecognized(900).code(), 900);
333    }
334
335    #[test]
336    fn given_surface_errors_when_classified_then_should_map_to_the_shared_code() {
337        assert_eq!(
338            ResultCode::from(&QueryError::IndexNotFound("orders".to_owned())),
339            ResultCode::NotFound
340        );
341        assert_eq!(
342            ResultCode::from(&QueryError::Stale {
343                what: "orders".to_owned(),
344                applied: 4,
345                required: 9,
346            }),
347            ResultCode::Stale
348        );
349        assert_eq!(
350            ResultCode::from(&KvError::VersionConflict { current: Some(3) }),
351            ResultCode::Conflict
352        );
353        assert_eq!(
354            ResultCode::from(&ForkError::Conflict("open".to_owned())),
355            ResultCode::Conflict
356        );
357        assert_eq!(
358            ResultCode::from(&KvError::NotLeader),
359            ResultCode::Unavailable
360        );
361        assert_eq!(
362            ResultCode::from(&ForkError::NotLeader),
363            ResultCode::Unavailable
364        );
365        assert_eq!(
366            ResultCode::from(&AgentError::NotLeader),
367            ResultCode::Unavailable
368        );
369    }
370
371    // A transient outcome must be distinguishable from a fault without parsing
372    // the message, so a generic client can back off and retry exactly these.
373    #[test]
374    fn given_transient_outcomes_when_classified_then_should_be_the_only_retryable_codes() {
375        for surface in [
376            ResultCode::from(&QueryError::Unavailable("dropped".to_owned())),
377            ResultCode::from(&KvError::Unavailable("dropped".to_owned())),
378            ResultCode::from(&ForkError::Unavailable("dropped".to_owned())),
379            ResultCode::from(&AgentError::Unavailable("dropped".to_owned())),
380        ] {
381            assert_eq!(surface, ResultCode::Unavailable);
382        }
383        assert_eq!(ResultCode::Unavailable.http_status(), 503);
384        assert!(ResultCode::Unavailable.is_retryable());
385        assert!(ResultCode::Stale.is_retryable());
386        for code in [
387            ResultCode::Ok,
388            ResultCode::Unsupported,
389            ResultCode::NotFound,
390            ResultCode::InvalidArgument,
391            ResultCode::TooLarge,
392            ResultCode::Conflict,
393            ResultCode::VersionSkew,
394            ResultCode::Unauthenticated,
395            ResultCode::Backend,
396            ResultCode::Forbidden,
397            ResultCode::StepUpRequired,
398            ResultCode::Unrecognized(900),
399        ] {
400            assert!(
401                !code.is_retryable(),
402                "{code:?} must not invite a blind retry"
403            );
404        }
405    }
406
407    #[test]
408    fn given_result_codes_when_mapped_to_http_then_should_match_the_binding_table() {
409        assert_eq!(ResultCode::NotFound.http_status(), 404);
410        assert_eq!(ResultCode::Unsupported.http_status(), 501);
411        assert_eq!(ResultCode::TooLarge.http_status(), 413);
412        assert_eq!(ResultCode::Conflict.http_status(), 409);
413        assert_eq!(ResultCode::Stale.http_status(), 503);
414        assert_eq!(ResultCode::Unauthenticated.http_status(), 401);
415        assert_eq!(ResultCode::Forbidden.http_status(), 403);
416        assert_eq!(ResultCode::StepUpRequired.http_status(), 403);
417        assert_eq!(ResultCode::Backend.http_status(), 502);
418        // An unrecognized code from a newer peer maps to a generic 500 rather
419        // than panicking, and keeps its raw numeric.
420        assert_eq!(ResultCode::Unrecognized(777).http_status(), 500);
421        assert_eq!(ResultCode::Unrecognized(777).code(), 777);
422    }
423
424    #[cfg(feature = "cbor")]
425    #[test]
426    fn given_a_result_code_when_round_tripped_through_cbor_then_should_preserve_the_variant() {
427        use crate::framing::{decode_named, encode_named};
428        for code in [
429            ResultCode::Ok,
430            ResultCode::Conflict,
431            ResultCode::Stale,
432            ResultCode::Unrecognized(4242),
433        ] {
434            let bytes = encode_named(&code).expect("serializes");
435            let back: ResultCode = decode_named(&bytes).expect("deserializes");
436            assert_eq!(back, code);
437        }
438    }
439
440    #[cfg(feature = "cbor")]
441    #[test]
442    fn given_a_command_error_when_round_tripped_then_should_preserve_code_and_message() {
443        use crate::framing::{decode_named, encode_named};
444        let error = CommandError::unsupported("AGDX_KV_CAS not served on this build");
445        assert_eq!(error.code, ResultCode::Unsupported);
446        let bytes = encode_named(&error).expect("serializes");
447        let back: CommandError = decode_named(&bytes).expect("deserializes");
448        assert_eq!(back, error);
449    }
450}