Skip to main content

nenjo_tools/
runtime.rs

1use std::path::{Path, PathBuf};
2
3/// Runtime adapter — abstracts platform differences so the same agent
4/// code runs on native, Docker, Cloudflare Workers, Raspberry Pi, etc.
5pub trait RuntimeAdapter: Send + Sync {
6    /// Human-readable runtime name
7    fn name(&self) -> &str;
8
9    /// Whether this runtime supports shell access
10    fn has_shell_access(&self) -> bool;
11
12    /// Whether this runtime supports filesystem access
13    fn has_filesystem_access(&self) -> bool;
14
15    /// Base storage path for this runtime
16    fn storage_path(&self) -> PathBuf;
17
18    /// Whether long-running processes (gateway, heartbeat) are supported
19    fn supports_long_running(&self) -> bool;
20
21    /// Maximum memory budget in bytes (0 = unlimited)
22    fn memory_budget(&self) -> u64 {
23        0
24    }
25
26    /// Build a shell command process for this runtime.
27    fn build_shell_command(
28        &self,
29        command: &str,
30        workspace_dir: &Path,
31    ) -> anyhow::Result<tokio::process::Command>;
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    struct DummyRuntime;
39
40    impl RuntimeAdapter for DummyRuntime {
41        fn name(&self) -> &str {
42            "dummy-runtime"
43        }
44
45        fn has_shell_access(&self) -> bool {
46            true
47        }
48
49        fn has_filesystem_access(&self) -> bool {
50            true
51        }
52
53        fn storage_path(&self) -> PathBuf {
54            PathBuf::from("/tmp/dummy-runtime")
55        }
56
57        fn supports_long_running(&self) -> bool {
58            true
59        }
60
61        fn build_shell_command(
62            &self,
63            command: &str,
64            workspace_dir: &Path,
65        ) -> anyhow::Result<tokio::process::Command> {
66            let mut cmd = tokio::process::Command::new("echo");
67            cmd.arg(command);
68            cmd.current_dir(workspace_dir);
69            Ok(cmd)
70        }
71    }
72
73    #[test]
74    fn default_memory_budget_is_zero() {
75        let runtime = DummyRuntime;
76        assert_eq!(runtime.memory_budget(), 0);
77    }
78
79    #[test]
80    fn runtime_reports_capabilities() {
81        let runtime = DummyRuntime;
82
83        assert_eq!(runtime.name(), "dummy-runtime");
84        assert!(runtime.has_shell_access());
85        assert!(runtime.has_filesystem_access());
86        assert!(runtime.supports_long_running());
87        assert_eq!(runtime.storage_path(), PathBuf::from("/tmp/dummy-runtime"));
88    }
89
90    #[tokio::test]
91    async fn build_shell_command_executes() {
92        let runtime = DummyRuntime;
93        let mut cmd = runtime
94            .build_shell_command("hello-runtime", Path::new("."))
95            .unwrap();
96
97        let output = cmd.output().await.unwrap();
98        let stdout = String::from_utf8_lossy(&output.stdout);
99
100        assert!(output.status.success());
101        assert!(stdout.contains("hello-runtime"));
102    }
103}