keleusma-cli 0.2.2

Standalone command-line frontend for Keleusma. Provides a script runner, a bytecode compiler, and an interactive REPL so users can work with Keleusma scripts without writing any Rust host code.
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
//! Manifest parsing and validation for `keleusma run-tasks`.
//!
//! The manifest is TOML. The schema is documented in
//! `docs/architecture/RUN_TASKS.md`. This module produces a validated
//! `Manifest` struct from a TOML source string, rejecting malformed
//! input fail-closed.

use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::Duration;

use serde::Deserialize;

use crate::duration;

/// Maximum number of tasks admissible in a single manifest. The
/// fixed cap simplifies the scheduler's data structures and bounds
/// the worst-case resident set. Operators with more tasks should
/// split into multiple runner processes.
pub const MAX_TASKS: usize = 16;

/// Maximum capacity of the event queue. Operators with bursty
/// workloads beyond this should write their own host.
pub const MAX_EVENT_QUEUE: usize = 64;

const DEFAULT_SHUTDOWN_GRACE: Duration = Duration::from_secs(5);
const DEFAULT_TICK_INTERVAL: Duration = Duration::from_millis(10);
const DEFAULT_RESTART_LIMIT: u32 = 5;
const DEFAULT_RESTART_WINDOW: Duration = Duration::from_secs(60);
const DEFAULT_ARENA_CAPACITY: usize = 64 * 1024;

/// Errors surfaced by manifest parsing and validation.
#[derive(Debug)]
pub enum ManifestError {
    /// TOML well-formedness failure.
    Parse(String),
    /// Required field absent.
    #[allow(dead_code)]
    MissingField(String),
    /// Field has the wrong shape (range, type, format).
    InvalidField(String),
    /// Duplicate task name.
    DuplicateTaskName(String),
    /// Duplicate event name.
    DuplicateEventName(String),
    /// Too many tasks declared.
    TooManyTasks(usize),
    /// Bytecode file does not exist on disk.
    BytecodeMissing(PathBuf),
}

impl std::fmt::Display for ManifestError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Parse(s) => write!(f, "manifest: {}", s),
            Self::MissingField(s) => write!(f, "manifest: missing field: {}", s),
            Self::InvalidField(s) => write!(f, "manifest: invalid field: {}", s),
            Self::DuplicateTaskName(s) => {
                write!(f, "manifest: duplicate task name: `{}`", s)
            }
            Self::DuplicateEventName(s) => {
                write!(f, "manifest: duplicate event name: `{}`", s)
            }
            Self::TooManyTasks(n) => write!(
                f,
                "manifest: {} tasks declared; maximum is {}",
                n, MAX_TASKS
            ),
            Self::BytecodeMissing(p) => {
                write!(f, "manifest: bytecode file does not exist: {:?}", p)
            }
        }
    }
}

impl std::error::Error for ManifestError {}

/// Restart policy for a single task.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RestartPolicy {
    /// Task exit or error is terminal for that task.
    Never,
    /// Task is restarted on any `VmError`. Voluntary termination is
    /// terminal.
    OnError,
    /// Task is restarted on both error and voluntary termination.
    Always,
}

impl RestartPolicy {
    fn parse(s: &str) -> Result<Self, ManifestError> {
        match s {
            "never" => Ok(Self::Never),
            "on_error" => Ok(Self::OnError),
            "always" => Ok(Self::Always),
            other => Err(ManifestError::InvalidField(format!(
                "restart = `{}`; expected one of: never, on_error, always",
                other
            ))),
        }
    }
}

/// Top-level manifest after parsing and validation. Fields are
/// concrete types rather than `Option<String>` strings; durations
/// have been parsed through [`duration::parse`].
#[derive(Debug)]
pub struct Manifest {
    pub scheduler: SchedulerConfig,
    pub tasks: Vec<TaskConfig>,
    pub events: BTreeMap<String, u8>,
}

/// Scheduler-wide configuration drawn from the `[scheduler]` table.
#[derive(Debug)]
pub struct SchedulerConfig {
    pub tick_interval: Duration,
    pub shutdown_grace: Duration,
}

/// One entry from the `[[task]]` table array.
#[derive(Debug)]
pub struct TaskConfig {
    pub name: String,
    pub bytecode: PathBuf,
    pub period: Option<Duration>,
    pub restart: RestartPolicy,
    pub restart_limit: u32,
    pub restart_window: Duration,
    pub arena_capacity: usize,
    pub priority: i32,
}

// ---- Raw TOML structures used only for deserialisation. ----

#[derive(Deserialize)]
struct RawManifest {
    #[serde(default)]
    scheduler: Option<RawScheduler>,
    #[serde(default)]
    task: Vec<RawTask>,
    #[serde(default)]
    events: BTreeMap<String, i64>,
}

#[derive(Deserialize)]
struct RawScheduler {
    tick_interval: Option<String>,
    shutdown_grace: Option<String>,
}

#[derive(Deserialize)]
struct RawTask {
    name: String,
    bytecode: String,
    period: Option<String>,
    restart: Option<String>,
    restart_limit: Option<u32>,
    restart_window: Option<String>,
    arena_capacity: Option<String>,
    priority: Option<i32>,
}

impl Manifest {
    /// Parse a manifest from a TOML source string and validate it.
    ///
    /// The `base_dir` argument is the directory the manifest file
    /// lives in; relative `bytecode` paths are resolved against it.
    pub fn parse(source: &str, base_dir: &std::path::Path) -> Result<Self, ManifestError> {
        let raw: RawManifest =
            toml::from_str(source).map_err(|e| ManifestError::Parse(e.message().to_string()))?;

        if raw.task.is_empty() {
            return Err(ManifestError::InvalidField(String::from(
                "[[task]] array must declare at least one task",
            )));
        }
        if raw.task.len() > MAX_TASKS {
            return Err(ManifestError::TooManyTasks(raw.task.len()));
        }

        // Scheduler configuration with defaults.
        let scheduler = {
            let raw_sched = raw.scheduler.unwrap_or(RawScheduler {
                tick_interval: None,
                shutdown_grace: None,
            });
            let tick_interval = match raw_sched.tick_interval {
                Some(s) => duration::parse(&s).map_err(|e| {
                    ManifestError::InvalidField(format!("scheduler.tick_interval: {}", e))
                })?,
                None => DEFAULT_TICK_INTERVAL,
            };
            let shutdown_grace = match raw_sched.shutdown_grace {
                Some(s) => duration::parse(&s).map_err(|e| {
                    ManifestError::InvalidField(format!("scheduler.shutdown_grace: {}", e))
                })?,
                None => DEFAULT_SHUTDOWN_GRACE,
            };
            SchedulerConfig {
                tick_interval,
                shutdown_grace,
            }
        };

        // Tasks.
        let mut seen_names = std::collections::BTreeSet::new();
        let mut tasks = Vec::with_capacity(raw.task.len());
        for rt in raw.task {
            if !seen_names.insert(rt.name.clone()) {
                return Err(ManifestError::DuplicateTaskName(rt.name));
            }
            let bytecode = {
                let p = std::path::PathBuf::from(&rt.bytecode);
                let resolved = if p.is_absolute() { p } else { base_dir.join(p) };
                if !resolved.exists() {
                    return Err(ManifestError::BytecodeMissing(resolved));
                }
                resolved
            };
            let period = match rt.period {
                Some(s) => Some(duration::parse(&s).map_err(|e| {
                    ManifestError::InvalidField(format!("task[{}].period: {}", rt.name, e))
                })?),
                None => None,
            };
            let restart = match rt.restart {
                Some(s) => RestartPolicy::parse(&s)?,
                None => RestartPolicy::OnError,
            };
            let restart_limit = rt.restart_limit.unwrap_or(DEFAULT_RESTART_LIMIT);
            if !(1..=1000).contains(&restart_limit) {
                return Err(ManifestError::InvalidField(format!(
                    "task[{}].restart_limit = {}; must be in 1..=1000",
                    rt.name, restart_limit
                )));
            }
            let restart_window = match rt.restart_window {
                Some(s) => duration::parse(&s).map_err(|e| {
                    ManifestError::InvalidField(format!("task[{}].restart_window: {}", rt.name, e))
                })?,
                None => DEFAULT_RESTART_WINDOW,
            };
            if restart_window < Duration::from_secs(1) || restart_window > Duration::from_secs(3600)
            {
                return Err(ManifestError::InvalidField(format!(
                    "task[{}].restart_window: must be between 1s and 1h",
                    rt.name
                )));
            }
            let arena_capacity = match rt.arena_capacity {
                Some(s) => parse_capacity(&s).map_err(|e| {
                    ManifestError::InvalidField(format!("task[{}].arena_capacity: {}", rt.name, e))
                })?,
                None => DEFAULT_ARENA_CAPACITY,
            };
            let priority = rt.priority.unwrap_or(0);
            tasks.push(TaskConfig {
                name: rt.name,
                bytecode,
                period,
                restart,
                restart_limit,
                restart_window,
                arena_capacity,
                priority,
            });
        }

        // Events. Validate id range and uniqueness; numeric ids must
        // fit in a Byte because the yield-reason EventWait payload is
        // a single Word, but only the low 8 bits identify the event.
        let mut events: BTreeMap<String, u8> = BTreeMap::new();
        for (name, id) in raw.events {
            if !(0..=255).contains(&id) {
                return Err(ManifestError::InvalidField(format!(
                    "events.{} = {}; event id must be in 0..=255",
                    name, id
                )));
            }
            if events.values().any(|&v| v == id as u8) {
                return Err(ManifestError::DuplicateEventName(format!(
                    "id {} declared more than once",
                    id
                )));
            }
            events.insert(name, id as u8);
        }

        Ok(Manifest {
            scheduler,
            tasks,
            events,
        })
    }
}

/// Parse a capacity spec like `64KB` or `4MB` or a bare byte count.
/// Returns the byte count.
fn parse_capacity(s: &str) -> Result<usize, String> {
    let s = s.trim();
    if let Some(stripped) = s.strip_suffix("MB") {
        let n: usize = stripped
            .trim()
            .parse()
            .map_err(|e| format!("cannot parse `{}` as integer: {}", stripped, e))?;
        Ok(n * 1024 * 1024)
    } else if let Some(stripped) = s.strip_suffix("KB") {
        let n: usize = stripped
            .trim()
            .parse()
            .map_err(|e| format!("cannot parse `{}` as integer: {}", stripped, e))?;
        Ok(n * 1024)
    } else {
        let n: usize = s
            .parse()
            .map_err(|e| format!("cannot parse `{}` as integer: {}", s, e))?;
        Ok(n)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn write_tmp_bytecode(name: &str) -> std::path::PathBuf {
        let mut p = std::env::temp_dir();
        p.push(format!("keleusma_runtasks_test_{}.bin", name));
        // Minimal KELE magic so the file at least looks like bytecode
        // for the existence check.
        std::fs::write(&p, b"KELE").expect("write tmp bytecode");
        p
    }

    #[test]
    fn minimal_manifest_parses() {
        let bc = write_tmp_bytecode("min");
        let toml_src = format!(
            r#"
[[task]]
name = "t1"
bytecode = "{}"
"#,
            bc.display()
        );
        let m = Manifest::parse(&toml_src, std::path::Path::new("/")).expect("parse");
        assert_eq!(m.tasks.len(), 1);
        assert_eq!(m.tasks[0].name, "t1");
        assert_eq!(m.tasks[0].restart, RestartPolicy::OnError);
        assert_eq!(m.scheduler.shutdown_grace, DEFAULT_SHUTDOWN_GRACE);
        let _ = std::fs::remove_file(&bc);
    }

    #[test]
    fn rejects_empty_task_array() {
        let toml_src = "";
        let err = Manifest::parse(toml_src, std::path::Path::new("/")).expect_err("empty");
        assert!(matches!(err, ManifestError::InvalidField(_)));
    }

    #[test]
    fn rejects_duplicate_task_name() {
        let bc = write_tmp_bytecode("dup");
        let toml_src = format!(
            r#"
[[task]]
name = "t1"
bytecode = "{0}"

[[task]]
name = "t1"
bytecode = "{0}"
"#,
            bc.display()
        );
        let err = Manifest::parse(&toml_src, std::path::Path::new("/")).expect_err("dup");
        assert!(matches!(err, ManifestError::DuplicateTaskName(_)));
        let _ = std::fs::remove_file(&bc);
    }

    #[test]
    fn rejects_too_many_tasks() {
        let bc = write_tmp_bytecode("many");
        let mut toml_src = String::new();
        for i in 0..(MAX_TASKS + 1) {
            toml_src.push_str(&format!(
                "[[task]]\nname = \"t{}\"\nbytecode = \"{}\"\n\n",
                i,
                bc.display()
            ));
        }
        let err = Manifest::parse(&toml_src, std::path::Path::new("/")).expect_err("too many");
        assert!(matches!(err, ManifestError::TooManyTasks(_)));
        let _ = std::fs::remove_file(&bc);
    }

    #[test]
    fn rejects_missing_bytecode() {
        let toml_src = r#"
[[task]]
name = "t1"
bytecode = "/nonexistent/missing.bin"
"#;
        let err = Manifest::parse(toml_src, std::path::Path::new("/")).expect_err("missing");
        assert!(matches!(err, ManifestError::BytecodeMissing(_)));
    }

    #[test]
    fn rejects_bad_restart() {
        let bc = write_tmp_bytecode("badrestart");
        let toml_src = format!(
            r#"
[[task]]
name = "t1"
bytecode = "{}"
restart = "occasionally"
"#,
            bc.display()
        );
        let err = Manifest::parse(&toml_src, std::path::Path::new("/")).expect_err("bad restart");
        assert!(matches!(err, ManifestError::InvalidField(_)));
        let _ = std::fs::remove_file(&bc);
    }

    #[test]
    fn parses_full_manifest() {
        let bc = write_tmp_bytecode("full");
        let toml_src = format!(
            r#"
[scheduler]
tick_interval = "20ms"
shutdown_grace = "10s"

[[task]]
name = "sensor"
bytecode = "{0}"
period = "100ms"
restart = "always"
restart_limit = 10
restart_window = "5m"
arena_capacity = "128KB"
priority = 1

[events]
data_ready = 1
shutdown_requested = 99
"#,
            bc.display()
        );
        let m = Manifest::parse(&toml_src, std::path::Path::new("/")).expect("parse");
        assert_eq!(m.scheduler.tick_interval, Duration::from_millis(20));
        assert_eq!(m.scheduler.shutdown_grace, Duration::from_secs(10));
        assert_eq!(m.tasks[0].period, Some(Duration::from_millis(100)));
        assert_eq!(m.tasks[0].restart, RestartPolicy::Always);
        assert_eq!(m.tasks[0].restart_limit, 10);
        assert_eq!(m.tasks[0].restart_window, Duration::from_secs(300));
        assert_eq!(m.tasks[0].arena_capacity, 128 * 1024);
        assert_eq!(m.tasks[0].priority, 1);
        assert_eq!(m.events.get("data_ready"), Some(&1));
        assert_eq!(m.events.get("shutdown_requested"), Some(&99));
        let _ = std::fs::remove_file(&bc);
    }

    #[test]
    fn parse_capacity_units() {
        assert_eq!(parse_capacity("65536").unwrap(), 65536);
        assert_eq!(parse_capacity("64KB").unwrap(), 65536);
        assert_eq!(parse_capacity("4MB").unwrap(), 4 * 1024 * 1024);
    }
}