agentkernel 0.18.1

Run AI coding agents in secure, isolated microVMs
Documentation
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
//! Template system for reusable sandbox configurations.
//!
//! Templates are resolved in order:
//! 1. Local saved templates in `~/.local/share/agentkernel/templates/`
//! 2. Built-in templates bundled in the binary
//! 3. `github:` prefix → fetch from GitHub raw, cache locally
//! 4. File path (`.toml` file)

use anyhow::{Context, Result, bail};
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use crate::config::Config;
use crate::setup::default_data_dir;

/// Built-in templates bundled at compile time
const BUILTIN_TEMPLATES: &[(&str, &str)] = &[
    // Agent-specific sandboxes
    (
        "claude-sandbox",
        include_str!("../templates/claude-sandbox.toml"),
    ),
    (
        "codex-sandbox",
        include_str!("../templates/codex-sandbox.toml"),
    ),
    (
        "gemini-sandbox",
        include_str!("../templates/gemini-sandbox.toml"),
    ),
    (
        "opencode-sandbox",
        include_str!("../templates/opencode-sandbox.toml"),
    ),
    ("amp-sandbox", include_str!("../templates/amp-sandbox.toml")),
    ("pi-sandbox", include_str!("../templates/pi-sandbox.toml")),
    (
        "copilot-sandbox",
        include_str!("../templates/copilot-sandbox.toml"),
    ),
    (
        "hermes-sandbox",
        include_str!("../templates/hermes-sandbox.toml"),
    ),
    (
        "symphony-sandbox",
        include_str!("../templates/symphony-sandbox.toml"),
    ),
    // Language runtimes
    ("bash", include_str!("../templates/bash.toml")),
    ("c", include_str!("../templates/c.toml")),
    ("dotnet", include_str!("../templates/dotnet.toml")),
    ("go", include_str!("../templates/go.toml")),
    ("java", include_str!("../templates/java.toml")),
    ("node", include_str!("../templates/node.toml")),
    ("python", include_str!("../templates/python.toml")),
    ("ruby", include_str!("../templates/ruby.toml")),
    ("rust", include_str!("../templates/rust.toml")),
    ("typescript", include_str!("../templates/typescript.toml")),
    // Browser automation
    ("playwright", include_str!("../templates/playwright.toml")),
    (
        "playwright-stealth",
        include_str!("../templates/playwright-stealth.toml"),
    ),
    // Specialized
    ("python-ml", include_str!("../templates/python-ml.toml")),
    (
        "node-fullstack",
        include_str!("../templates/node-fullstack.toml"),
    ),
    ("rust-ci", include_str!("../templates/rust-ci.toml")),
    ("secure", include_str!("../templates/secure.toml")),
    // Infrastructure / Cloud
    ("terraform", include_str!("../templates/terraform.toml")),
    // Datastores
    ("sqlite", include_str!("../templates/sqlite.toml")),
    ("postgres", include_str!("../templates/postgres.toml")),
    ("mysql", include_str!("../templates/mysql.toml")),
    ("redis", include_str!("../templates/redis.toml")),
    // Dev tools
    ("vscode", include_str!("../templates/vscode.toml")),
    ("coder", include_str!("../templates/coder.toml")),
    ("gitea", include_str!("../templates/gitea.toml")),
    ("openclaw", include_str!("../templates/openclaw.toml")),
];

/// Where a template was found
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TemplateSource {
    BuiltIn,
    Local,
    GitHub,
    FilePath,
}

impl std::fmt::Display for TemplateSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BuiltIn => write!(f, "built-in"),
            Self::Local => write!(f, "local"),
            Self::GitHub => write!(f, "github"),
            Self::FilePath => write!(f, "file"),
        }
    }
}

/// A resolved template
pub struct ResolvedTemplate {
    pub name: String,
    pub source: TemplateSource,
    pub content: String,
}

impl ResolvedTemplate {
    /// Parse the template TOML into a Config
    pub fn parse(&self) -> Result<Config> {
        Config::from_str(&self.content)
            .with_context(|| format!("failed to parse template '{}'", self.name))
    }
}

/// Directory for user-saved templates
fn local_templates_dir() -> PathBuf {
    default_data_dir().join("templates")
}

/// Resolve a template by name or specifier.
///
/// Handles: built-in names, local saved names, `github:owner/repo/path`, file paths.
pub fn resolve(specifier: &str) -> Result<ResolvedTemplate> {
    // 1. File path (contains / or .toml extension, and not github:)
    if !specifier.starts_with("github:")
        && (specifier.contains('/') || specifier.ends_with(".toml"))
    {
        let path = Path::new(specifier);
        if path.exists() {
            let content = fs::read_to_string(path)
                .with_context(|| format!("failed to read template file: {}", specifier))?;
            let name = path
                .file_stem()
                .map(|s| s.to_string_lossy().to_string())
                .unwrap_or_else(|| specifier.to_string());
            return Ok(ResolvedTemplate {
                name,
                source: TemplateSource::FilePath,
                content,
            });
        }
        // Fall through to name-based resolution if it doesn't look like a path
        if specifier.contains('/') && !specifier.starts_with('.') {
            bail!("template file not found: {}", specifier);
        }
    }

    // 2. Local saved templates
    let local_dir = local_templates_dir();
    let local_path = local_dir.join(format!("{}.toml", specifier));
    if local_path.exists() {
        let content = fs::read_to_string(&local_path)?;
        return Ok(ResolvedTemplate {
            name: specifier.to_string(),
            source: TemplateSource::Local,
            content,
        });
    }

    // 3. Built-in templates
    for (name, content) in BUILTIN_TEMPLATES {
        if *name == specifier {
            return Ok(ResolvedTemplate {
                name: name.to_string(),
                source: TemplateSource::BuiltIn,
                content: content.to_string(),
            });
        }
    }

    // 4. GitHub shorthand: github:owner/repo/path
    if let Some(gh_path) = specifier.strip_prefix("github:") {
        return resolve_github(gh_path);
    }

    bail!(
        "template '{}' not found (checked local, built-in, and file paths)",
        specifier
    )
}

/// Resolve a github: template specifier.
///
/// Format: `github:owner/repo/path` → fetches from raw.githubusercontent.com
fn resolve_github(gh_path: &str) -> Result<ResolvedTemplate> {
    let parts: Vec<&str> = gh_path.splitn(3, '/').collect();
    if parts.len() < 3 {
        bail!(
            "github: template requires format 'github:owner/repo/path', got 'github:{}'",
            gh_path
        );
    }
    let (owner, repo, path) = (parts[0], parts[1], parts[2]);

    // Check local cache first
    let cache_dir = local_templates_dir().join("github-cache");
    let cache_key = gh_path.replace('/', "--");
    let cache_path = cache_dir.join(format!("{}.toml", cache_key));
    if cache_path.exists() {
        let content = fs::read_to_string(&cache_path)?;
        let name = path
            .strip_suffix(".toml")
            .unwrap_or(path)
            .rsplit('/')
            .next()
            .unwrap_or(path)
            .to_string();
        return Ok(ResolvedTemplate {
            name,
            source: TemplateSource::GitHub,
            content,
        });
    }

    // Fetch from GitHub raw
    let url = if path.ends_with(".toml") {
        format!(
            "https://raw.githubusercontent.com/{}/{}/main/{}",
            owner, repo, path
        )
    } else {
        format!(
            "https://raw.githubusercontent.com/{}/{}/main/{}.toml",
            owner, repo, path
        )
    };

    // Use curl for fetching (available on all platforms)
    let output = std::process::Command::new("curl")
        .args(["-fsSL", "--max-time", "10", &url])
        .output()
        .context("failed to fetch template from GitHub (is curl installed?)")?;

    if !output.status.success() {
        bail!(
            "failed to fetch template from GitHub: {}\nURL: {}",
            String::from_utf8_lossy(&output.stderr).trim(),
            url
        );
    }

    let content =
        String::from_utf8(output.stdout).context("GitHub template response was not valid UTF-8")?;

    // Validate it parses before caching
    Config::from_str(&content).with_context(|| {
        format!(
            "fetched template from GitHub is not valid TOML config: {}",
            url
        )
    })?;

    // Cache locally
    fs::create_dir_all(&cache_dir)?;
    fs::write(&cache_path, &content)?;

    let name = path
        .strip_suffix(".toml")
        .unwrap_or(path)
        .rsplit('/')
        .next()
        .unwrap_or(path)
        .to_string();

    Ok(ResolvedTemplate {
        name,
        source: TemplateSource::GitHub,
        content,
    })
}

/// A template listing entry
pub struct TemplateInfo {
    pub name: String,
    pub source: TemplateSource,
    pub image: String,
    pub profile: String,
    pub memory_mb: u64,
}

/// List all available templates (local + built-in)
pub fn list_all() -> Vec<TemplateInfo> {
    let mut templates: BTreeMap<String, TemplateInfo> = BTreeMap::new();

    // Built-in templates
    for (name, content) in BUILTIN_TEMPLATES {
        if let Ok(cfg) = Config::from_str(content) {
            let image = cfg
                .sandbox
                .base_image
                .clone()
                .unwrap_or_else(|| cfg.sandbox.runtime.clone());
            templates.insert(
                name.to_string(),
                TemplateInfo {
                    name: name.to_string(),
                    source: TemplateSource::BuiltIn,
                    image,
                    profile: format!("{:?}", cfg.security.profile).to_lowercase(),
                    memory_mb: cfg.resources.memory_mb,
                },
            );
        }
    }

    // Local templates (override built-in if same name)
    let local_dir = local_templates_dir();
    if let Ok(entries) = fs::read_dir(&local_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "toml")
                && let Some(name) = path.file_stem().map(|s| s.to_string_lossy().to_string())
                && let Ok(content) = fs::read_to_string(&path)
                && let Ok(cfg) = Config::from_str(&content)
            {
                let image = cfg
                    .sandbox
                    .base_image
                    .clone()
                    .unwrap_or_else(|| cfg.sandbox.runtime.clone());
                templates.insert(
                    name.clone(),
                    TemplateInfo {
                        name,
                        source: TemplateSource::Local,
                        image,
                        profile: format!("{:?}", cfg.security.profile).to_lowercase(),
                        memory_mb: cfg.resources.memory_mb,
                    },
                );
            }
        }
    }

    templates.into_values().collect()
}

/// Save a config as a local template
pub fn save(name: &str, config: &Config) -> Result<PathBuf> {
    let local_dir = local_templates_dir();
    fs::create_dir_all(&local_dir)?;

    let path = local_dir.join(format!("{}.toml", name));
    let content = toml::to_string_pretty(config)?;
    fs::write(&path, content)?;
    Ok(path)
}

/// Remove a local template
pub fn remove(name: &str) -> Result<()> {
    let local_dir = local_templates_dir();
    let path = local_dir.join(format!("{}.toml", name));
    if !path.exists() {
        // Check if it's a built-in — can't remove those
        if BUILTIN_TEMPLATES.iter().any(|(n, _)| *n == name) {
            bail!("'{}' is a built-in template and cannot be removed", name);
        }
        bail!("template '{}' not found in local templates", name);
    }
    fs::remove_file(&path)?;
    Ok(())
}

/// Fetch and cache a github: template locally
pub fn add_github(gh_specifier: &str) -> Result<ResolvedTemplate> {
    let gh_path = gh_specifier.strip_prefix("github:").unwrap_or(gh_specifier);

    // Clear cache for this entry so we re-fetch
    let cache_dir = local_templates_dir().join("github-cache");
    let cache_key = gh_path.replace('/', "--");
    let cache_path = cache_dir.join(format!("{}.toml", cache_key));
    let _ = fs::remove_file(&cache_path);

    resolve_github(gh_path)
}

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

    #[test]
    fn test_builtin_templates_parse() {
        for (name, content) in BUILTIN_TEMPLATES {
            let result = Config::from_str(content);
            assert!(
                result.is_ok(),
                "built-in template '{}' failed to parse: {:?}",
                name,
                result.err()
            );
        }
    }

    #[test]
    fn test_builtin_template_count() {
        assert_eq!(BUILTIN_TEMPLATES.len(), 34);
    }

    #[test]
    fn test_resolve_builtin() {
        let t = resolve("claude-sandbox").unwrap();
        assert_eq!(t.name, "claude-sandbox");
        assert_eq!(t.source, TemplateSource::BuiltIn);
        let cfg = t.parse().unwrap();
        assert_eq!(cfg.agent.preferred, "claude");
    }

    #[test]
    fn test_resolve_builtin_secure() {
        let t = resolve("secure").unwrap();
        assert_eq!(t.source, TemplateSource::BuiltIn);
        let cfg = t.parse().unwrap();
        assert_eq!(cfg.security.network, Some(false));
    }

    #[test]
    fn test_resolve_not_found() {
        let result = resolve("nonexistent-template-xyz");
        assert!(result.is_err());
    }

    #[test]
    fn test_resolve_file_not_found() {
        let result = resolve("./does-not-exist.toml");
        assert!(result.is_err());
    }

    #[test]
    fn test_list_includes_builtins() {
        let templates = list_all();
        assert!(templates.len() >= 18);
        let names: Vec<&str> = templates.iter().map(|t| t.name.as_str()).collect();
        assert!(names.contains(&"claude-sandbox"));
        assert!(names.contains(&"secure"));
        assert!(names.contains(&"python-ml"));
    }

    #[test]
    fn test_save_and_remove() {
        let cfg = Config::from_str(BUILTIN_TEMPLATES[0].1).unwrap();
        let name = "test-save-remove-template";
        let path = save(name, &cfg).unwrap();
        assert!(path.exists());

        // Should resolve as local now
        let t = resolve(name).unwrap();
        assert_eq!(t.source, TemplateSource::Local);

        // Clean up
        remove(name).unwrap();
        assert!(!path.exists());
    }

    #[test]
    fn test_remove_builtin_fails() {
        let result = remove("claude-sandbox");
        assert!(result.is_err());
    }

    #[test]
    fn test_template_source_display() {
        assert_eq!(TemplateSource::BuiltIn.to_string(), "built-in");
        assert_eq!(TemplateSource::Local.to_string(), "local");
        assert_eq!(TemplateSource::GitHub.to_string(), "github");
        assert_eq!(TemplateSource::FilePath.to_string(), "file");
    }
}