kap 0.0.1-pre4

Run AI agents in secure capsules. Built on devcontainers with network controls and remote access.
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
/// Generate .devcontainer/.env and docker-compose.kap.yml from host environment.
///
/// Reads kap.toml to find which env vars the proxy sidecar needs
/// (from token_env and ${VAR} references in headers), then writes
/// them to .env so docker-compose passes them to the container.
///
/// Also regenerates the compose overlay (docker-compose.kap.yml) so it
/// always matches the installed kap version and kap.toml config.
use anyhow::{Context, Result};
use std::path::Path;

fn generate_shim(tool_name: &str) -> String {
    format!("#!/bin/sh\nexec /opt/kap/kap cli-shim {tool_name} \"$@\"\n")
}

pub fn run(project_dir: &str) -> Result<()> {
    let project = Path::new(project_dir);
    let devcontainer_dir = project.join(".devcontainer");
    let config_path = devcontainer_dir.join("kap.toml");
    let env_path = devcontainer_dir.join(".env");

    if !config_path.exists() {
        anyhow::bail!(
            "No kap.toml found at {}. Run `kap init` first to set up your devcontainer.",
            config_path.display()
        );
    }

    // Regenerate compose overlay (non-fatal: warn and continue if it fails)
    if let Err(e) = regenerate_overlay(&devcontainer_dir, &config_path) {
        eprintln!("[sidecar-init] warning: could not regenerate overlay: {e}");
    }

    let needed_vars = vars_from_config(&config_path)?;

    // Load existing .env values and shell patterns (# KEY=$(cmd))
    let (existing, existing_patterns) = load_env_file(&env_path);

    let mut lines: Vec<String> = Vec::new();

    for var in &needed_vars {
        // Re-evaluate shell patterns stored as comments (# KEY=$(cmd))
        if let Some(pattern) = existing_patterns.get(var.as_str()) {
            let resolved = eval_shell_substitution(pattern);
            if !resolved.is_empty() {
                lines.push(format!("# {var}={pattern}"));
                lines.push(format!("{var}={resolved}"));
                continue;
            }
        }
        // Keep existing value (or evaluate if it contains $(cmd))
        if let Some(val) = existing.get(var.as_str()) {
            if val.contains("$(") {
                let resolved = eval_shell_substitution(val);
                if !resolved.is_empty() {
                    lines.push(format!("# {var}={val}"));
                    lines.push(format!("{var}={resolved}"));
                    continue;
                }
            } else if !val.is_empty() {
                lines.push(format!("{var}={val}"));
                continue;
            }
        }
        // Otherwise try host environment
        if let Ok(val) = std::env::var(var)
            && !val.is_empty()
        {
            eprintln!("[sidecar-init] {var} (from host env)");
            lines.push(format!("{var}={val}"));
        }
    }

    let content = lines.join("\n");
    if !content.is_empty() {
        std::fs::write(&env_path, content + "\n")?;
        eprintln!(
            "[sidecar-init] wrote {} vars to {}",
            lines.len(),
            env_path.display()
        );
    } else if !env_path.exists() {
        // Only create empty file if none exists
        std::fs::write(&env_path, "")?;
    }

    Ok(())
}

/// Regenerate docker-compose.kap.yml from kap.toml config.
fn regenerate_overlay(devcontainer_dir: &Path, config_path: &Path) -> Result<()> {
    let overlay_path = devcontainer_dir.join(crate::init::OVERLAY_FILENAME);

    // Read service name from devcontainer.json
    let service_name = crate::init::read_service_name(devcontainer_dir)?;

    // Read compose config from kap.toml
    let content = std::fs::read_to_string(config_path)
        .with_context(|| format!("reading {}", config_path.display()))?;
    let config: crate::config::Config =
        toml::from_str(&content).with_context(|| format!("parsing {}", config_path.display()))?;
    let compose_config = config.compose.unwrap_or_default();
    let cli_tools: Vec<String> = config
        .cli
        .as_ref()
        .map(|c| c.tools.iter().map(|t| t.name.clone()).collect())
        .unwrap_or_default();

    // Derive project root from devcontainer_dir (parent of .devcontainer/)
    let project_dir = devcontainer_dir.parent().unwrap_or(devcontainer_dir);
    let subnet_prefix = crate::init::derive_subnet(project_dir);
    let overlay =
        crate::init::generate_overlay(&service_name, &compose_config, &cli_tools, &subnet_prefix);
    std::fs::write(&overlay_path, &overlay)
        .with_context(|| format!("writing {}", overlay_path.display()))?;
    eprintln!(
        "[sidecar-init] regenerated {}",
        crate::init::OVERLAY_FILENAME
    );

    // Write shim scripts for each CLI tool
    for tool_name in &cli_tools {
        let shim_path = devcontainer_dir.join(format!("{tool_name}-shim.sh"));
        let shim = generate_shim(tool_name);
        std::fs::write(&shim_path, shim)
            .with_context(|| format!("writing {}", shim_path.display()))?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&shim_path, std::fs::Permissions::from_mode(0o755))?;
        }
        eprintln!("[sidecar-init] wrote {tool_name}-shim.sh");
    }

    Ok(())
}

/// Evaluate shell command substitutions like `$(gh auth token)`.
/// Returns the value as-is if it doesn't contain `$(...)`.
fn eval_shell_substitution(val: &str) -> String {
    if !val.contains("$(") {
        return val.to_string();
    }
    match std::process::Command::new("sh")
        .arg("-c")
        .arg(format!("printf '%s' {val}"))
        .output()
    {
        Ok(output) if output.status.success() => {
            let result = String::from_utf8_lossy(&output.stdout).trim().to_string();
            if result.is_empty() {
                eprintln!("[sidecar-init] warning: {val} evaluated to empty");
            }
            result
        }
        _ => {
            eprintln!("[sidecar-init] warning: failed to evaluate {val}");
            String::new()
        }
    }
}

/// Load existing KEY=VALUE pairs from a .env file.
/// Also returns shell patterns from comments like `# KEY=$(cmd)`.
fn load_env_file(
    path: &Path,
) -> (
    std::collections::HashMap<String, String>,
    std::collections::HashMap<String, String>,
) {
    let mut values = std::collections::HashMap::new();
    let mut patterns = std::collections::HashMap::new();
    let Ok(content) = std::fs::read_to_string(path) else {
        return (values, patterns);
    };
    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        // Extract shell patterns from comments: # KEY=$(cmd)
        if let Some(comment) = line.strip_prefix("# ") {
            if let Some((key, val)) = comment.split_once('=') {
                let val = val.trim();
                if val.contains("$(") {
                    patterns.insert(key.trim().to_string(), val.to_string());
                }
            }
            continue;
        }
        if line.starts_with('#') {
            continue;
        }
        if let Some((key, val)) = line.split_once('=') {
            values.insert(key.trim().to_string(), val.trim().to_string());
        }
    }
    (values, patterns)
}

/// Parse kap.toml and collect all env var names referenced by MCP server configs.
fn vars_from_config(path: &Path) -> Result<Vec<String>> {
    let content =
        std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
    let config: crate::config::Config =
        toml::from_str(&content).with_context(|| format!("parsing {}", path.display()))?;

    let mut vars = Vec::new();

    if let Some(mcp) = &config.mcp {
        for server in &mcp.servers {
            // token_env is itself an env var name
            if let Some(ref var) = server.token_env {
                vars.push(var.clone());
            }
            // headers can contain ${VAR} references
            for value in server.headers.values() {
                extract_env_refs(value, &mut vars);
            }
        }
    }

    // CLI tools need their env vars on the sidecar
    if let Some(cli) = &config.cli {
        for tool in &cli.tools {
            for var in &tool.env {
                vars.push(var.clone());
            }
        }
    }

    vars.sort();
    vars.dedup();
    Ok(vars)
}

/// Extract ${VAR} references from a string.
fn extract_env_refs(s: &str, vars: &mut Vec<String>) {
    let mut rest = s;
    while let Some(start) = rest.find("${") {
        rest = &rest[start + 2..];
        if let Some(end) = rest.find('}') {
            let var = &rest[..end];
            if !var.is_empty() {
                vars.push(var.to_string());
            }
            rest = &rest[end + 1..];
        } else {
            break;
        }
    }
}

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

    #[test]
    fn extract_env_refs_finds_vars() {
        let mut vars = Vec::new();
        extract_env_refs("Bearer ${GH_TOKEN}", &mut vars);
        assert_eq!(vars, vec!["GH_TOKEN"]);
    }

    #[test]
    fn extract_env_refs_multiple() {
        let mut vars = Vec::new();
        extract_env_refs("${FOO} and ${BAR}", &mut vars);
        assert_eq!(vars, vec!["FOO", "BAR"]);
    }

    #[test]
    fn extract_env_refs_none() {
        let mut vars = Vec::new();
        extract_env_refs("static-value", &mut vars);
        assert!(vars.is_empty());
    }

    #[test]
    fn extract_env_refs_unclosed_brace() {
        let mut vars = Vec::new();
        extract_env_refs("${UNCLOSED", &mut vars);
        assert!(vars.is_empty());
    }

    #[test]
    fn extract_env_refs_empty_var_name() {
        let mut vars = Vec::new();
        extract_env_refs("${}", &mut vars);
        assert!(vars.is_empty());
    }

    #[test]
    fn load_env_file_parses_key_value() {
        let dir = std::env::temp_dir().join(format!("kap-loadenv-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join(".env");
        std::fs::write(&path, "FOO=bar\nBAZ=qux\n").unwrap();

        let (values, patterns) = load_env_file(&path);
        assert_eq!(values["FOO"], "bar");
        assert_eq!(values["BAZ"], "qux");
        assert_eq!(values.len(), 2);
        assert!(patterns.is_empty());

        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn load_env_file_skips_comments_and_blanks() {
        let dir = std::env::temp_dir().join(format!("kap-loadenv2-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join(".env");
        std::fs::write(&path, "# comment\n\nKEY=val\n  \n# another\n").unwrap();

        let (values, _) = load_env_file(&path);
        assert_eq!(values.len(), 1);
        assert_eq!(values["KEY"], "val");

        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn load_env_file_extracts_shell_patterns() {
        let dir = std::env::temp_dir().join(format!("kap-loadenv3-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join(".env");
        std::fs::write(&path, "# GH_TOKEN=$(gh auth token)\nGH_TOKEN=old_value\n").unwrap();

        let (values, patterns) = load_env_file(&path);
        assert_eq!(values["GH_TOKEN"], "old_value");
        assert_eq!(patterns["GH_TOKEN"], "$(gh auth token)");

        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn shell_pattern_survives_two_loads() {
        // Simulates what happens across two init-env runs:
        // 1. .env has GH_TOKEN=$(echo hello)
        // 2. First load: evaluates to "hello", writes # GH_TOKEN=$(echo hello)\nGH_TOKEN=hello
        // 3. Second load: finds pattern in comment, re-evaluates, keeps pattern
        let dir = std::env::temp_dir().join(format!("kap-pattern-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join(".env");

        // First run: raw pattern
        std::fs::write(&path, "GH_TOKEN=$(echo hello)\n").unwrap();
        let (values, patterns) = load_env_file(&path);
        assert_eq!(values["GH_TOKEN"], "$(echo hello)");
        assert!(patterns.is_empty());
        // Simulate what init-env writes
        let resolved = eval_shell_substitution(&values["GH_TOKEN"]);
        assert_eq!(resolved, "hello");
        std::fs::write(
            &path,
            format!("# GH_TOKEN=$(echo hello)\nGH_TOKEN={resolved}\n"),
        )
        .unwrap();

        // Second run: pattern should come from comment, not the raw value
        let (values2, patterns2) = load_env_file(&path);
        assert_eq!(values2["GH_TOKEN"], "hello"); // raw value
        assert_eq!(patterns2["GH_TOKEN"], "$(echo hello)"); // pattern preserved
        // Pattern takes priority - re-evaluate it
        let resolved2 = eval_shell_substitution(&patterns2["GH_TOKEN"]);
        assert_eq!(resolved2, "hello");

        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn eval_shell_substitution_passthrough() {
        assert_eq!(eval_shell_substitution("plain_value"), "plain_value");
    }

    #[test]
    fn eval_shell_substitution_evaluates() {
        let result = eval_shell_substitution("$(echo test123)");
        assert_eq!(result, "test123");
    }

    #[test]
    fn load_env_file_missing_returns_empty() {
        let (values, patterns) = load_env_file(Path::new("/nonexistent/.env"));
        assert!(values.is_empty());
        assert!(patterns.is_empty());
    }

    #[test]
    fn vars_from_config_reads_toml() {
        let dir = std::env::temp_dir().join(format!("kap-initenv-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("kap.toml");
        std::fs::write(
            &path,
            r#"
[mcp]
[[mcp.servers]]
name = "a"
upstream = "https://a.com"
token_env = "A_TOKEN"

[[mcp.servers]]
name = "b"
upstream = "https://b.com"
headers = { "X-Key" = "${B_API_KEY}", "X-Other" = "${C_SECRET}" }
"#,
        )
        .unwrap();

        let vars = vars_from_config(&path).unwrap();
        assert_eq!(vars, vec!["A_TOKEN", "B_API_KEY", "C_SECRET"]);

        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn regenerate_overlay_from_config() {
        let dir = std::env::temp_dir().join(format!("kap-regen-{}", std::process::id()));
        let dc = dir.join(".devcontainer");
        std::fs::create_dir_all(&dc).unwrap();

        std::fs::write(dc.join("devcontainer.json"), r#"{"service": "myapp"}"#).unwrap();
        std::fs::write(
            dc.join("kap.toml"),
            r#"
[proxy.network]
allow = ["github.com"]

[compose]
build = { context = "..", dockerfile = "Dockerfile", target = "proxy" }
"#,
        )
        .unwrap();

        regenerate_overlay(&dc, &dc.join("kap.toml")).unwrap();

        let overlay = std::fs::read_to_string(dc.join(crate::init::OVERLAY_FILENAME)).unwrap();
        assert!(overlay.contains("myapp:"));
        assert!(overlay.contains("build:"));
        assert!(overlay.contains("context: .."));
        assert!(!overlay.contains("image:"));

        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn regenerate_overlay_default_image() {
        let dir = std::env::temp_dir().join(format!("kap-regen-img-{}", std::process::id()));
        let dc = dir.join(".devcontainer");
        std::fs::create_dir_all(&dc).unwrap();

        std::fs::write(dc.join("devcontainer.json"), r#"{}"#).unwrap();
        std::fs::write(
            dc.join("kap.toml"),
            r#"
[proxy.network]
allow = ["github.com"]
"#,
        )
        .unwrap();

        regenerate_overlay(&dc, &dc.join("kap.toml")).unwrap();

        let overlay = std::fs::read_to_string(dc.join(crate::init::OVERLAY_FILENAME)).unwrap();
        assert!(overlay.contains("app:"));
        assert!(overlay.contains("image: ghcr.io/6/kap:latest"));

        std::fs::remove_dir_all(&dir).unwrap();
    }
}