arcbox-api 0.6.8

API server for ArcBox (gRPC + REST)
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
//! Error types for the API server.

use arcbox_error::CommonError;
use thiserror::Error;

/// Result type alias for API operations.
pub type Result<T> = std::result::Result<T, ApiError>;

/// Errors that can occur in API operations.
#[derive(Debug, Error)]
pub enum ApiError {
    /// Common errors (I/O, config, etc.).
    #[error(transparent)]
    Common(#[from] CommonError),

    /// Core error.
    #[error("core error: {0}")]
    Core(#[from] arcbox_core::CoreError),

    /// Server error.
    #[error("server error: {0}")]
    Server(String),

    /// Transport error.
    #[error("transport error: {0}")]
    Transport(String),
}

// Engine errors classify exactly like their CoreError twins: route through
// the variant-for-variant CoreError conversion so Connect codes stay stable.
impl From<arcbox_engine::EngineError> for ApiError {
    fn from(err: arcbox_engine::EngineError) -> Self {
        Self::Core(arcbox_core::CoreError::from(err))
    }
}

// Allow automatic conversion from std::io::Error to ApiError via CommonError.
impl From<std::io::Error> for ApiError {
    fn from(err: std::io::Error) -> Self {
        Self::Common(CommonError::from(err))
    }
}

/// Every service answers over Connect, so `ApiError` classifies straight
/// into a Connect error code. The assignments predate the tonic retirement
/// (CORE-68/73): clients see the same codes the gRPC services always
/// answered.
impl From<ApiError> for connectrpc::ConnectError {
    fn from(err: ApiError) -> Self {
        use connectrpc::ErrorCode;

        let message = err.to_string();
        let code = match &err {
            ApiError::Common(common) | ApiError::Core(arcbox_core::CoreError::Common(common)) => {
                match common {
                    CommonError::Config(_) => ErrorCode::InvalidArgument,
                    CommonError::NotFound(_) => ErrorCode::NotFound,
                    CommonError::AlreadyExists(_) => ErrorCode::AlreadyExists,
                    CommonError::InvalidState(_) => ErrorCode::FailedPrecondition,
                    CommonError::Timeout(_) => ErrorCode::DeadlineExceeded,
                    CommonError::PermissionDenied(_) => ErrorCode::PermissionDenied,
                    _ => ErrorCode::Internal,
                }
            }
            // Agent-reported errors carry an HTTP-style code over the wire;
            // the raw message classifies further into the error registry.
            ApiError::Core(arcbox_core::CoreError::Agent {
                code,
                message: agent_message,
            }) => {
                let connect_code = match code {
                    400 => ErrorCode::InvalidArgument,
                    404 => ErrorCode::NotFound,
                    409 => ErrorCode::AlreadyExists,
                    412 | 423 => ErrorCode::FailedPrecondition,
                    // Stdin offset gap: resync via GetStdinStatus.
                    416 => ErrorCode::OutOfRange,
                    503 => ErrorCode::Unavailable,
                    // A bounded guest wait elapsed (e.g. WaitForPort).
                    504 => ErrorCode::DeadlineExceeded,
                    _ => ErrorCode::Internal,
                };
                let mut error = Self::new(connect_code, message);
                if let Some(detail) = classify_agent_error(*code, agent_message) {
                    error.details.push(detail);
                }
                return error;
            }
            ApiError::Core(arcbox_core::CoreError::Transport {
                source:
                    arcbox_transport::error::TransportError::NotConnected
                    | arcbox_transport::error::TransportError::ConnectionRefused(_)
                    | arcbox_transport::error::TransportError::ConnectionReset
                    | arcbox_transport::error::TransportError::Common(CommonError::Io(_)),
                ..
            }) => ErrorCode::Unavailable,
            _ => ErrorCode::Internal,
        };
        Self::new(code, message)
    }
}

/// Map a guest agent error onto the sandbox error registry (CORE-58).
///
/// The wire carries only an HTTP-style code + message, so registry
/// precision comes from the message shapes this same tree produces (the
/// daemon and agent ship from one master state, enforced by the protocol
/// handshake): `VmmError`'s `Display` impls and the guest `SandboxError`
/// constructors. The markers are pinned by the tests below — changing a
/// message shape must update both together.
fn classify_agent_error(code: i32, message: &str) -> Option<connectrpc::ErrorDetail> {
    use arcbox_connect::sandbox_v1 as pb;

    match code {
        // VmmError::NotFound → "VM not found: {id}"; the execution
        // registry names its resource explicitly.
        404 if message.starts_with("execution '") => Some(error_info(
            pb::ErrorCode::ExecutionNotFound,
            "list live executions with `abctl sandbox ps`, or start a new one",
            &[],
        )),
        404 if message.starts_with("VM not found:") => Some(error_info(
            pb::ErrorCode::SandboxNotFound,
            "list sandboxes with `abctl sandbox list`",
            &[],
        )),
        // VmmError::TemplateNotFound → "template not found: {reference}"
        // (the template catalog, CORE-107).
        404 if message.starts_with("template not found: ") => {
            let reference = message
                .strip_prefix("template not found: ")
                .unwrap_or_default();
            Some(error_info(
                pb::ErrorCode::TemplateNotFound,
                "list catalog templates with `TemplateService.List`, or build one first",
                &[("reference", reference)],
            ))
        }
        // VmmError::PathNotFound → "path not found: {path}" (the sandbox
        // filesystem verbs, CORE-62).
        404 if message.starts_with("path not found: ") => {
            let path = message.strip_prefix("path not found: ").unwrap_or_default();
            Some(error_info(
                pb::ErrorCode::FileNotFound,
                "check the path with `Stat` or `ListDir` on its parent directory",
                &[("path", path)],
            ))
        }
        // The guest agent's per-file write cap → "file exceeds the
        // {limit}-byte write limit".
        400 if message.contains("-byte write limit") => {
            let limit = message
                .split("exceeds the ")
                .nth(1)
                .and_then(|rest| rest.split("-byte").next())
                .unwrap_or_default();
            Some(error_info(
                pb::ErrorCode::FileTooLarge,
                "split the payload or ship it through the sandbox's own tooling",
                &[("limit_bytes", limit)],
            ))
        }
        // Guest-side nested-virt probe failure (the daemon's own fail-fast
        // gate attaches this detail directly; this covers agent-originated
        // 412s, e.g. a stale capability view).
        412 if message.contains("nested virtualization") => Some(error_info(
            pb::ErrorCode::NestedVirtUnsupported,
            "check `SandboxService.GetCapabilities` for this host's sandbox support",
            &[],
        )),
        // VmmError::WrongState → "VM '{id}' is in wrong state: expected
        // {expected}, got {actual}". FAILED and PAUSED are their own registry
        // codes — both need an action, not a wait; anything else is "exists
        // but not ready for this call" (still STARTING, busy RUNNING under
        // the single-execution model, or mid-transition).
        412 if message.contains("is in wrong state") => {
            let observed = message.rsplit("got ").next().unwrap_or_default();
            match observed {
                "failed" => Some(error_info(
                    pb::ErrorCode::SandboxFailed,
                    "inspect the sandbox for the failure reason, then remove it",
                    &[("state", observed)],
                )),
                // A control-plane call refused because the sandbox is paused
                // — `Stop` is the reachable one, since PAUSED only leads to
                // RESUMING, FAILED, or REMOVING. Waiting never clears it.
                "paused" => Some(error_info(
                    pb::ErrorCode::SandboxPaused,
                    "resume the sandbox first (`SandboxService.Resume`), or remove it — \
                     a paused sandbox has no VM to stop",
                    &[("state", observed)],
                )),
                "starting" | "running" | "stopping" | "stopped" | "pausing" | "resuming" => {
                    Some(error_info(
                        pb::ErrorCode::SandboxNotReady,
                        "wait for the sandbox to reach READY (watch `Events` or poll `Inspect`)",
                        &[("state", observed)],
                    ))
                }
                // Other WrongState shapes (generation races, cleanup
                // tickets) are precondition failures without a precise
                // registry identity.
                _ => None,
            }
        }
        // Guest template parse/resolution rejections name the template.
        400 if message.contains("template") => Some(error_info(
            pb::ErrorCode::TemplateInvalid,
            "use \"\" (built-in), \"docker:<image>\", or a catalog template name",
            &[],
        )),
        // Paused sandbox surfaced without a transparent resume
        // (control-plane call, or the caller opted out), CORE-21.
        423 => Some(error_info(
            pb::ErrorCode::SandboxPaused,
            "resume the sandbox (`abctl sandbox resume <id>`), or retry \
             the data-plane call without `x-arcbox-no-auto-resume`",
            &[],
        )),
        _ => None,
    }
}

/// Build the `arcbox.sandbox.v1.ErrorInfo` Connect error detail from a
/// registry code + actionable suggestion + structured context (CORE-58).
/// The single builder every attachment site uses, so the type URL and
/// shape SDKs parse stay in one place.
pub(crate) fn error_info(
    code: arcbox_connect::sandbox_v1::ErrorCode,
    suggestion: &str,
    context: &[(&str, &str)],
) -> connectrpc::ErrorDetail {
    let info = arcbox_connect::sandbox_v1::ErrorInfo {
        code: code.into(),
        suggestion: suggestion.to_owned(),
        context: context
            .iter()
            .map(|(key, value)| ((*key).to_owned(), (*value).to_owned()))
            .collect(),
        ..Default::default()
    };
    connectrpc::ErrorDetail::from_message("arcbox.sandbox.v1.ErrorInfo", &info)
}

impl ApiError {
    /// Creates a new configuration error.
    #[must_use]
    pub fn config(msg: impl Into<String>) -> Self {
        Self::Common(CommonError::config(msg))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use arcbox_connect::sandbox_v1 as pb;
    use buffa::Message as _;

    fn decode_info(detail: &connectrpc::ErrorDetail) -> pb::ErrorInfo {
        use base64::Engine as _;
        assert_eq!(detail.type_url, "arcbox.sandbox.v1.ErrorInfo");
        let bytes = base64::engine::general_purpose::STANDARD_NO_PAD
            .decode(detail.value.as_deref().expect("detail value"))
            .expect("base64 detail value");
        pb::ErrorInfo::decode_from_slice(&bytes).expect("valid ErrorInfo payload")
    }

    fn registry_code(detail: &connectrpc::ErrorDetail) -> pb::ErrorCode {
        decode_info(detail)
            .code
            .as_known()
            .expect("known registry code")
    }

    fn classified(code: i32, message: &str) -> Option<pb::ErrorCode> {
        classify_agent_error(code, message).map(|detail| registry_code(&detail))
    }

    /// Pins the guest message markers the classifier keys on. If one of
    /// these fails after a message change, update the marker AND this test
    /// together (`VmmError` Display impls / guest `SandboxError` sites).
    #[test]
    fn agent_errors_classify_into_the_registry() {
        assert_eq!(
            classified(404, "VM not found: box1"),
            Some(pb::ErrorCode::SandboxNotFound)
        );
        assert_eq!(
            classified(404, "execution 'e1' in sandbox 'box1'"),
            Some(pb::ErrorCode::ExecutionNotFound)
        );
        assert_eq!(
            classified(423, "sandbox 'box1' is paused"),
            Some(pb::ErrorCode::SandboxPaused)
        );
        assert_eq!(
            classified(
                412,
                "VM 'box1' is in wrong state: expected Ready, got running"
            ),
            Some(pb::ErrorCode::SandboxNotReady)
        );
        assert_eq!(
            classified(
                412,
                "VM 'box1' is in wrong state: expected Ready, got failed"
            ),
            Some(pb::ErrorCode::SandboxFailed)
        );
        // A control-plane refusal on a paused sandbox (Stop is the reachable
        // one) carries the same code as the data-plane 423 — one condition,
        // one registry identity — not "not ready", which never clears.
        assert_eq!(
            classified(
                412,
                "VM 'box1' is in wrong state: expected Ready | Running | Stopping, got paused"
            ),
            Some(pb::ErrorCode::SandboxPaused)
        );
        // Mid-transition states are a genuine wait.
        assert_eq!(
            classified(
                412,
                "VM 'box1' is in wrong state: expected Ready, got resuming"
            ),
            Some(pb::ErrorCode::SandboxNotReady)
        );
        assert_eq!(
            classified(
                412,
                "sandboxes require nested virtualization (/dev/kvm is missing in the guest)"
            ),
            Some(pb::ErrorCode::NestedVirtUnsupported)
        );
        assert_eq!(
            classified(
                400,
                "unknown template \"typo\"; expected \"\" or \"docker:<image>\""
            ),
            Some(pb::ErrorCode::TemplateInvalid)
        );
        // VmmError::TemplateNotFound (the template catalog, CORE-107) —
        // distinct from the sandbox 404 despite sharing the wire code.
        assert_eq!(
            classified(404, "template not found: code:9.9"),
            Some(pb::ErrorCode::TemplateNotFound)
        );
        // VmmError::PathNotFound (the filesystem verbs, CORE-62).
        assert_eq!(
            classified(404, "path not found: /work/missing.txt"),
            Some(pb::ErrorCode::FileNotFound)
        );
        // The guest agent's per-file write cap.
        assert_eq!(
            classified(400, "file exceeds the 268435456-byte write limit"),
            Some(pb::ErrorCode::FileTooLarge)
        );
    }

    #[test]
    fn file_not_found_context_carries_the_path() {
        let detail =
            classify_agent_error(404, "path not found: /work/missing.txt").expect("classified");
        let info = decode_info(&detail);
        assert_eq!(
            info.context.get("path").map(String::as_str),
            Some("/work/missing.txt")
        );
    }

    #[test]
    fn file_too_large_context_carries_the_limit() {
        let detail = classify_agent_error(400, "file exceeds the 268435456-byte write limit")
            .expect("classified");
        let info = decode_info(&detail);
        assert_eq!(
            info.context.get("limit_bytes").map(String::as_str),
            Some("268435456")
        );
    }

    #[test]
    fn deadline_wire_code_maps_to_deadline_exceeded() {
        let error = connectrpc::ConnectError::from(ApiError::Core(arcbox_core::CoreError::Agent {
            code: 504,
            message: "no listener on port 8080 in sandbox 'box1' within 30s".into(),
        }));
        assert_eq!(error.code, connectrpc::ErrorCode::DeadlineExceeded);
    }

    #[test]
    fn unmatched_agent_errors_carry_no_registry_detail() {
        assert_eq!(classified(500, "vsock error: boom"), None);
        assert_eq!(classified(404, "snapshot abc not found"), None);
        // Generation races are precondition failures without a precise
        // registry identity.
        assert_eq!(
            classified(
                412,
                "VM 'box1' is in wrong state: expected the sandbox generation selected by \
                 this operation, got a newer generation now owns this sandbox ID"
            ),
            None
        );
    }

    #[test]
    fn wrong_state_context_carries_the_observed_state() {
        let detail = classify_agent_error(
            412,
            "VM 'box1' is in wrong state: expected Ready, got running",
        )
        .expect("classified");
        let info = decode_info(&detail);
        assert_eq!(
            info.context.get("state").map(String::as_str),
            Some("running")
        );
    }

    #[test]
    fn paused_agent_error_maps_to_failed_precondition_with_detail() {
        let error = connectrpc::ConnectError::from(ApiError::Core(arcbox_core::CoreError::Agent {
            code: 423,
            message: "sandbox 'box1' is paused".into(),
        }));
        assert_eq!(error.code, connectrpc::ErrorCode::FailedPrecondition);
        assert_eq!(error.details.len(), 1);
        assert_eq!(
            registry_code(&error.details[0]),
            pb::ErrorCode::SandboxPaused
        );
    }

    #[test]
    fn core_resource_errors_keep_their_connect_codes() {
        let missing = connectrpc::ConnectError::from(ApiError::Core(
            arcbox_core::CoreError::not_found("machine dev"),
        ));
        assert_eq!(missing.code, connectrpc::ErrorCode::NotFound);

        let stopped = connectrpc::ConnectError::from(ApiError::Core(
            arcbox_core::CoreError::invalid_state("machine 'dev' is not running"),
        ));
        assert_eq!(stopped.code, connectrpc::ErrorCode::FailedPrecondition);

        let command =
            connectrpc::ConnectError::from(ApiError::Core(arcbox_core::CoreError::Agent {
                code: 404,
                message: "command not found: nope".into(),
            }));
        assert_eq!(command.code, connectrpc::ErrorCode::NotFound);

        let transport = connectrpc::ConnectError::from(ApiError::Core(
            arcbox_transport::error::TransportError::ConnectionReset.into(),
        ));
        assert_eq!(transport.code, connectrpc::ErrorCode::Unavailable);

        let protocol = connectrpc::ConnectError::from(ApiError::Core(
            arcbox_transport::error::TransportError::Protocol("bad frame".into()).into(),
        ));
        assert_eq!(protocol.code, connectrpc::ErrorCode::Internal);
    }
}