boxlite 0.9.4

Embeddable virtual machine runtime for secure, isolated code execution
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//! Core data types for box lifecycle management.

use chrono::{DateTime, Utc};
use rand::RngCore;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;

pub use crate::litebox::{BoxState, BoxStatus, HealthStatus};
use crate::runtime::id::BoxID;

// ============================================================================
// RESOURCE LIMIT TYPES (C-NEWTYPE: Semantic newtypes for distinct concepts)
// ============================================================================

/// Byte size for memory and file size limits.
///
/// Using a dedicated type prevents mixing bytes with counts or other units.
/// Provides convenient constructors for common size notations.
///
/// # Example
///
/// ```
/// use boxlite::runtime::types::Bytes;
///
/// let size = Bytes::from_mib(512);
/// assert_eq!(size.as_bytes(), 512 * 1024 * 1024);
///
/// // From raw bytes
/// let exact = Bytes::from_bytes(1_000_000);
/// assert_eq!(exact.as_bytes(), 1_000_000);
/// ```
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct Bytes(pub u64);

impl Bytes {
    /// Create from raw byte count.
    #[inline]
    pub const fn from_bytes(bytes: u64) -> Self {
        Self(bytes)
    }

    /// Create from kibibytes (1 KiB = 1024 bytes).
    #[inline]
    pub const fn from_kib(kib: u64) -> Self {
        Self(kib * 1024)
    }

    /// Create from mebibytes (1 MiB = 1024² bytes).
    #[inline]
    pub const fn from_mib(mib: u64) -> Self {
        Self(mib * 1024 * 1024)
    }

    /// Create from gibibytes (1 GiB = 1024³ bytes).
    #[inline]
    pub const fn from_gib(gib: u64) -> Self {
        Self(gib * 1024 * 1024 * 1024)
    }

    /// Get the raw byte count.
    #[inline]
    pub const fn as_bytes(&self) -> u64 {
        self.0
    }

    /// Get size in kibibytes (truncating).
    #[inline]
    pub const fn as_kib(&self) -> u64 {
        self.0 / 1024
    }

    /// Get size in mebibytes (truncating).
    #[inline]
    pub const fn as_mib(&self) -> u64 {
        self.0 / (1024 * 1024)
    }
}

impl From<u64> for Bytes {
    fn from(bytes: u64) -> Self {
        Self(bytes)
    }
}

impl From<Bytes> for u64 {
    fn from(b: Bytes) -> Self {
        b.0
    }
}

impl fmt::Display for Bytes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.0 >= 1024 * 1024 * 1024 && self.0.is_multiple_of(1024 * 1024 * 1024) {
            write!(f, "{} GiB", self.0 / (1024 * 1024 * 1024))
        } else if self.0 >= 1024 * 1024 && self.0.is_multiple_of(1024 * 1024) {
            write!(f, "{} MiB", self.0 / (1024 * 1024))
        } else if self.0 >= 1024 && self.0.is_multiple_of(1024) {
            write!(f, "{} KiB", self.0 / 1024)
        } else {
            write!(f, "{} bytes", self.0)
        }
    }
}

/// Duration in seconds for CPU time limits.
///
/// Using a dedicated type prevents mixing seconds with milliseconds or counts.
///
/// # Example
///
/// ```
/// use boxlite::runtime::types::Seconds;
///
/// let timeout = Seconds::from_minutes(5);
/// assert_eq!(timeout.as_seconds(), 300);
///
/// let short = Seconds::from_seconds(30);
/// assert_eq!(short.as_seconds(), 30);
/// ```
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct Seconds(pub u64);

impl Seconds {
    /// Create from raw seconds.
    #[inline]
    pub const fn from_seconds(s: u64) -> Self {
        Self(s)
    }

    /// Create from minutes.
    #[inline]
    pub const fn from_minutes(m: u64) -> Self {
        Self(m * 60)
    }

    /// Create from hours.
    #[inline]
    pub const fn from_hours(h: u64) -> Self {
        Self(h * 60 * 60)
    }

    /// Get the raw seconds count.
    #[inline]
    pub const fn as_seconds(&self) -> u64 {
        self.0
    }

    /// Get duration in minutes (truncating).
    #[inline]
    pub const fn as_minutes(&self) -> u64 {
        self.0 / 60
    }
}

impl From<u64> for Seconds {
    fn from(s: u64) -> Self {
        Self(s)
    }
}

impl From<Seconds> for u64 {
    fn from(s: Seconds) -> Self {
        s.0
    }
}

impl fmt::Display for Seconds {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.0 >= 3600 && self.0.is_multiple_of(3600) {
            write!(f, "{} hours", self.0 / 3600)
        } else if self.0 >= 60 && self.0.is_multiple_of(60) {
            write!(f, "{} minutes", self.0 / 60)
        } else {
            write!(f, "{} seconds", self.0)
        }
    }
}

// ============================================================================
// CONTAINER ID
// ============================================================================

/// Container identifier (64-character lowercase hex).
///
/// Follows the OCI convention: SHA256 hash encoded as 64 lowercase hex characters.
/// This format matches Docker/containerd container IDs.
///
/// # Example
///
/// ```
/// use boxlite::runtime::types::ContainerID;
///
/// let id = ContainerID::new();
/// assert_eq!(id.as_str().len(), 64);
/// assert_eq!(id.short().len(), 12);
/// ```
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ContainerID(String);

impl ContainerID {
    /// Length of full container ID (64 hex chars = 256 bits).
    pub const FULL_LENGTH: usize = 64;

    /// Length of short container ID for display (12 hex chars).
    pub const SHORT_LENGTH: usize = 12;

    /// Generate a new random container ID.
    ///
    /// Uses SHA256 of 32 random bytes to produce a 64-char hex string.
    pub fn new() -> Self {
        let mut random_bytes = [0u8; 32];
        rand::rng().fill_bytes(&mut random_bytes);

        let mut hasher = Sha256::new();
        hasher.update(random_bytes);
        let result = hasher.finalize();

        Self(hex::encode(result))
    }

    /// Parse a ContainerID from an existing string.
    ///
    /// Returns `None` if the string is not a valid 64-char lowercase hex string.
    pub fn parse(s: &str) -> Option<Self> {
        if Self::is_valid(s) {
            Some(Self(s.to_string()))
        } else {
            None
        }
    }

    /// Check if a string is a valid container ID format.
    pub fn is_valid(s: &str) -> bool {
        s.len() == Self::FULL_LENGTH
            && s.chars()
                .all(|c| c.is_ascii_hexdigit() && !c.is_uppercase())
    }

    /// Get the full container ID as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Get the short form (first 12 characters) for display.
    pub fn short(&self) -> &str {
        &self.0[..Self::SHORT_LENGTH]
    }
}

impl Default for ContainerID {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for ContainerID {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl fmt::Debug for ContainerID {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ContainerID({})", self.short())
    }
}

impl AsRef<str> for ContainerID {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

/// Public metadata about a box (returned by list operations).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BoxInfo {
    /// Unique box identifier.
    pub id: BoxID,

    /// User-defined name (optional).
    pub name: Option<String>,

    /// Current lifecycle status.
    pub status: BoxStatus,

    /// Creation timestamp (UTC).
    pub created_at: DateTime<Utc>,

    /// Last state change timestamp (UTC).
    pub last_updated: DateTime<Utc>,

    /// Process ID of the VMM subprocess (None if not running).
    pub pid: Option<u32>,

    /// Image reference or rootfs path.
    pub image: String,

    /// Allocated CPU count.
    pub cpus: u8,

    /// Allocated memory in MiB.
    pub memory_mib: u32,

    /// User-defined labels for filtering and organization.
    pub labels: HashMap<String, String>,

    /// Health status.
    pub health_status: HealthStatus,
}

impl BoxInfo {
    /// Create BoxInfo from config and state.
    pub fn new(config: &crate::litebox::config::BoxConfig, state: &BoxState) -> Self {
        use crate::runtime::options::RootfsSpec;

        Self {
            id: config.id.clone(),
            name: config.name.clone(),
            status: state.status,
            created_at: config.created_at,
            last_updated: state.last_updated,
            pid: state.pid,
            image: match &config.options.rootfs {
                RootfsSpec::Image(r) => r.clone(),
                RootfsSpec::RootfsPath(p) => format!("rootfs:{}", p),
            },
            cpus: config.options.cpus.unwrap_or(2),
            memory_mib: config.options.memory_mib.unwrap_or(512),
            labels: HashMap::new(),
            health_status: state.health_status,
        }
    }
}

impl PartialEq for BoxInfo {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self.status == other.status
            && self.created_at == other.created_at
            && self.pid == other.pid
            && self.image == other.image
            && self.cpus == other.cpus
            && self.memory_mib == other.memory_mib
            && self.labels == other.labels
            && self.health_status == other.health_status
    }
}

// ============================================================================
// BOX STATE INFO (Docker-like State object)
// ============================================================================

/// Runtime state information (like Docker's State object).
///
/// Contains dynamic state that changes during the box lifecycle.
/// This is separate from BoxInfo which includes static configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BoxStateInfo {
    /// Current lifecycle status.
    pub status: BoxStatus,

    /// Whether the box is currently running.
    pub running: bool,

    /// Process ID of the VMM subprocess (None if not running).
    pub pid: Option<u32>,
}

impl BoxStateInfo {
    /// Create BoxStateInfo from internal BoxState.
    pub fn new(state: &BoxState) -> Self {
        Self {
            status: state.status,
            running: state.status.is_running(),
            pid: state.pid,
        }
    }
}

impl From<&BoxInfo> for BoxStateInfo {
    /// Build state view from public BoxInfo (e.g. for CLI templates).
    /// Equivalent to `BoxStateInfo::new(state)` when the same box is BoxInfo.
    fn from(info: &BoxInfo) -> Self {
        Self {
            status: info.status,
            running: info.status.is_running(),
            pid: info.pid,
        }
    }
}

// ============================================================================
// IMAGE INFO
// ============================================================================

/// Public metadata about a cached image.
///
/// Designed to provide all necessary information  without exposing internal storage details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageInfo {
    /// Full image reference (e.g., "docker.io/library/alpine:latest")
    pub reference: String,

    /// Parsed repository name (e.g. "docker.io/library/alpine")
    pub repository: String,

    /// Parsed image tag (e.g. "latest")
    pub tag: String,

    /// Image ID (Manifest Digest)
    pub id: String,

    /// When this image was pulled/cached locally.
    /// Note: This is NOT the image build time (which requires reading config blob).
    pub cached_at: DateTime<Utc>,

    /// Image size in bytes (if available)
    pub size: Option<Bytes>,
}

// ============================================================================
// BOX CONFIG (Podman-style separation)
// ============================================================================

// BoxMetadata is replaced by BoxConfig + BoxState
// Old BoxMetadata struct removed - use BoxConfig + BoxState instead

#[cfg(test)]
mod tests {
    use super::*;
    use crate::litebox::config::{BoxConfig, ContainerRuntimeConfig};
    use crate::runtime::options::{BoxOptions, RootfsSpec};
    use boxlite_shared::Transport;
    use std::path::PathBuf;

    #[test]
    fn test_config_state_to_info() {
        let now = Utc::now();
        let box_id = BoxID::parse("01HJK4TNRPQSXYZ8WM6NCVT9R5").unwrap();
        let config = BoxConfig {
            id: box_id,
            name: None,
            created_at: now,
            container: ContainerRuntimeConfig {
                id: ContainerID::new(),
            },
            options: BoxOptions {
                rootfs: RootfsSpec::Image("python:3.11".to_string()),
                cpus: Some(4),
                memory_mib: Some(1024),
                ..Default::default()
            },
            engine_kind: crate::vmm::VmmKind::Libkrun,
            transport: Transport::unix(PathBuf::from("/tmp/boxlite.sock")),
            box_home: PathBuf::from("/tmp/box"),
            ready_socket_path: PathBuf::from("/tmp/ready.sock"),
        };

        let mut state = BoxState::new();
        state.set_pid(Some(12345));
        let _ = state.transition_to(BoxStatus::Running);

        let info = BoxInfo::new(&config, &state);

        assert_eq!(info.id, config.id);
        assert_eq!(info.status, state.status);
        assert_eq!(info.created_at, config.created_at);
        assert_eq!(info.pid, state.pid);
        assert_eq!(info.image, "python:3.11");
        assert_eq!(info.cpus, 4);
        assert_eq!(info.memory_mib, 1024);
    }

    #[test]
    fn test_container_id_new() {
        let id1 = ContainerID::new();
        let id2 = ContainerID::new();

        // IDs should be 64 characters
        assert_eq!(id1.as_str().len(), ContainerID::FULL_LENGTH);
        assert_eq!(id2.as_str().len(), ContainerID::FULL_LENGTH);

        // IDs should be unique
        assert_ne!(id1, id2);

        // IDs should be lowercase hex
        assert!(
            id1.as_str()
                .chars()
                .all(|c| c.is_ascii_hexdigit() && !c.is_uppercase())
        );
    }

    #[test]
    fn test_container_id_short() {
        let id = ContainerID::new();

        // Short form should be 12 characters
        assert_eq!(id.short().len(), ContainerID::SHORT_LENGTH);

        // Short form should be prefix of full ID
        assert!(id.as_str().starts_with(id.short()));
    }

    #[test]
    fn test_container_id_from_str() {
        // Valid ID
        let valid = "a".repeat(64);
        assert!(ContainerID::parse(&valid).is_some());

        // Invalid: too short
        assert!(ContainerID::parse("abc123").is_none());

        // Invalid: uppercase
        let uppercase = "A".repeat(64);
        assert!(ContainerID::parse(&uppercase).is_none());

        // Invalid: non-hex
        let non_hex = "g".repeat(64);
        assert!(ContainerID::parse(&non_hex).is_none());
    }

    #[test]
    fn test_container_id_display() {
        let id = ContainerID::new();
        let display = format!("{}", id);
        assert_eq!(display, id.as_str());
    }

    #[test]
    fn test_container_id_debug() {
        let id = ContainerID::new();
        let debug = format!("{:?}", id);
        assert!(debug.contains(id.short()));
        assert!(debug.starts_with("ContainerID("));
    }

    // ========================================================================
    // Bytes tests
    // ========================================================================

    #[test]
    fn test_bytes_constructors() {
        assert_eq!(Bytes::from_bytes(1000).as_bytes(), 1000);
        assert_eq!(Bytes::from_kib(1).as_bytes(), 1024);
        assert_eq!(Bytes::from_mib(1).as_bytes(), 1024 * 1024);
        assert_eq!(Bytes::from_gib(1).as_bytes(), 1024 * 1024 * 1024);
    }

    #[test]
    fn test_bytes_conversions() {
        let size = Bytes::from_mib(512);
        assert_eq!(size.as_kib(), 512 * 1024);
        assert_eq!(size.as_mib(), 512);

        // Truncating division
        let odd_size = Bytes::from_bytes(1500);
        assert_eq!(odd_size.as_kib(), 1); // 1500 / 1024 = 1
    }

    #[test]
    fn test_bytes_from_u64() {
        let b: Bytes = 1024u64.into();
        assert_eq!(b.as_bytes(), 1024);

        let raw: u64 = Bytes::from_mib(1).into();
        assert_eq!(raw, 1024 * 1024);
    }

    #[test]
    fn test_bytes_display() {
        assert_eq!(format!("{}", Bytes::from_gib(2)), "2 GiB");
        assert_eq!(format!("{}", Bytes::from_mib(512)), "512 MiB");
        assert_eq!(format!("{}", Bytes::from_kib(64)), "64 KiB");
        assert_eq!(format!("{}", Bytes::from_bytes(500)), "500 bytes");

        // Non-even values show in smaller unit
        assert_eq!(format!("{}", Bytes::from_bytes(1500)), "1500 bytes");
    }

    #[test]
    fn test_bytes_ordering() {
        assert!(Bytes::from_mib(1) < Bytes::from_mib(2));
        assert!(Bytes::from_gib(1) > Bytes::from_mib(512));
    }

    #[test]
    fn test_bytes_default() {
        assert_eq!(Bytes::default().as_bytes(), 0);
    }

    // ========================================================================
    // Seconds tests
    // ========================================================================

    #[test]
    fn test_seconds_constructors() {
        assert_eq!(Seconds::from_seconds(30).as_seconds(), 30);
        assert_eq!(Seconds::from_minutes(5).as_seconds(), 300);
        assert_eq!(Seconds::from_hours(1).as_seconds(), 3600);
    }

    #[test]
    fn test_seconds_conversions() {
        let duration = Seconds::from_hours(2);
        assert_eq!(duration.as_minutes(), 120);
        assert_eq!(duration.as_seconds(), 7200);

        // Truncating division
        let odd_duration = Seconds::from_seconds(90);
        assert_eq!(odd_duration.as_minutes(), 1); // 90 / 60 = 1
    }

    #[test]
    fn test_seconds_from_u64() {
        let s: Seconds = 300u64.into();
        assert_eq!(s.as_seconds(), 300);

        let raw: u64 = Seconds::from_minutes(5).into();
        assert_eq!(raw, 300);
    }

    #[test]
    fn test_seconds_display() {
        assert_eq!(format!("{}", Seconds::from_hours(2)), "2 hours");
        assert_eq!(format!("{}", Seconds::from_minutes(30)), "30 minutes");
        assert_eq!(format!("{}", Seconds::from_seconds(45)), "45 seconds");

        // Non-even values show in seconds
        assert_eq!(format!("{}", Seconds::from_seconds(90)), "90 seconds");
    }

    #[test]
    fn test_seconds_ordering() {
        assert!(Seconds::from_seconds(30) < Seconds::from_minutes(1));
        assert!(Seconds::from_hours(1) > Seconds::from_minutes(30));
    }

    #[test]
    fn test_seconds_default() {
        assert_eq!(Seconds::default().as_seconds(), 0);
    }
}