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
//! Execution receipts for verifiable command runs.
//!
//! A receipt captures invocation metadata, outcome, and a tamper-evident hash.

use anyhow::{Context, Result, bail};
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as BASE64;
use ring::rand::SystemRandom;
use ring::signature::{ED25519, Ed25519KeyPair, KeyPair, UnparsedPublicKey};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};

const RECEIPT_VERSION: u32 = 1;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionReceipt {
    pub version: u32,
    pub receipt_id: String,
    pub recorded_at: String,
    pub invocation: Invocation,
    pub outcome: ExecutionOutcome,
    pub payload_hash_sha256: String,
    pub signature: Option<ReceiptSignature>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReceiptSignature {
    pub algorithm: String,
    pub key_id: String,
    pub public_key: String,
    pub signature: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "mode", content = "input", rename_all = "snake_case")]
pub enum Invocation {
    Run(RunInvocation),
    Exec(ExecInvocation),
}

impl Invocation {
    pub fn mode_name(&self) -> &'static str {
        match self {
            Invocation::Run(_) => "run",
            Invocation::Exec(_) => "exec",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunInvocation {
    pub command: Vec<String>,
    pub image: Option<String>,
    pub backend: Option<String>,
    pub profile: String,
    pub no_network: bool,
    pub fast: bool,
    pub keep: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecInvocation {
    pub name: String,
    pub command: Vec<String>,
    pub env: Vec<String>,
    pub workdir: Option<String>,
    pub sudo: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionOutcome {
    pub exit_code: i32,
    pub success: bool,
    pub output_sha256: String,
    pub output_bytes: usize,
    pub error: Option<String>,
}

impl ExecutionOutcome {
    pub fn from_combined_output(exit_code: i32, output: &str, error: Option<String>) -> Self {
        Self {
            exit_code,
            success: exit_code == 0,
            output_sha256: hash_output(output),
            output_bytes: output.len(),
            error,
        }
    }
}

#[derive(Serialize)]
struct HashableReceipt<'a> {
    version: u32,
    receipt_id: &'a str,
    recorded_at: &'a str,
    invocation: &'a Invocation,
    outcome: &'a ExecutionOutcome,
}

impl ExecutionReceipt {
    pub fn new(invocation: Invocation, outcome: ExecutionOutcome) -> Result<Self> {
        let mut receipt = Self {
            version: RECEIPT_VERSION,
            receipt_id: uuid::Uuid::now_v7().to_string(),
            recorded_at: chrono::Utc::now().to_rfc3339(),
            invocation,
            outcome,
            payload_hash_sha256: String::new(),
            signature: None,
        };
        let payload = receipt.payload_bytes()?;
        receipt.payload_hash_sha256 = hash_bytes(&payload);
        receipt.signature = Some(sign_payload(&payload)?);
        Ok(receipt)
    }

    fn payload_bytes(&self) -> Result<Vec<u8>> {
        let hashable = HashableReceipt {
            version: self.version,
            receipt_id: &self.receipt_id,
            recorded_at: &self.recorded_at,
            invocation: &self.invocation,
            outcome: &self.outcome,
        };
        serde_json::to_vec(&hashable).context("Failed to serialize receipt payload")
    }
}

pub fn hash_output(output: &str) -> String {
    hash_bytes(output.as_bytes())
}

fn hash_bytes(bytes: &[u8]) -> String {
    let digest = Sha256::digest(bytes);
    format!("{:x}", digest)
}

fn receipts_dir() -> PathBuf {
    if let Some(home) = std::env::var_os("HOME") {
        PathBuf::from(home).join(".agentkernel").join("receipts")
    } else {
        PathBuf::from("/tmp/agentkernel/receipts")
    }
}

fn signing_key_path() -> PathBuf {
    receipts_dir().join("ed25519_signing_key.pkcs8")
}

fn load_or_create_signing_key() -> Result<Ed25519KeyPair> {
    let path = signing_key_path();

    if path.exists() {
        let key_bytes = std::fs::read(&path)
            .with_context(|| format!("Failed to read signing key {}", path.display()))?;
        return Ed25519KeyPair::from_pkcs8(&key_bytes)
            .map_err(|_| anyhow::anyhow!("Invalid Ed25519 signing key at {}", path.display()));
    }

    let rng = SystemRandom::new();
    let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng)
        .map_err(|_| anyhow::anyhow!("Failed to generate Ed25519 signing key"))?;
    crate::secure_fs::write_private_file_atomic(&path, pkcs8.as_ref())
        .with_context(|| format!("Failed to write signing key {}", path.display()))?;

    let key_bytes = std::fs::read(&path)
        .with_context(|| format!("Failed to read signing key {}", path.display()))?;
    Ed25519KeyPair::from_pkcs8(&key_bytes).map_err(|_| {
        anyhow::anyhow!(
            "Invalid generated Ed25519 signing key at {}",
            path.display()
        )
    })
}

fn short_key_id(public_key: &[u8]) -> String {
    hash_bytes(public_key).chars().take(16).collect()
}

fn sign_payload(payload: &[u8]) -> Result<ReceiptSignature> {
    let keypair = load_or_create_signing_key()?;
    let public_key = keypair.public_key().as_ref().to_vec();
    let signature = keypair.sign(payload);
    Ok(ReceiptSignature {
        algorithm: "ed25519".to_string(),
        key_id: short_key_id(&public_key),
        public_key: BASE64.encode(public_key),
        signature: BASE64.encode(signature.as_ref()),
    })
}

fn verify_signature(signature: &ReceiptSignature, payload: &[u8]) -> Result<()> {
    if !signature.algorithm.eq_ignore_ascii_case("ed25519") {
        bail!(
            "Unsupported signature algorithm '{}'; expected ed25519",
            signature.algorithm
        );
    }

    let public_key = BASE64
        .decode(&signature.public_key)
        .context("Invalid base64 public key in receipt signature")?;
    let sig = BASE64
        .decode(&signature.signature)
        .context("Invalid base64 signature bytes in receipt signature")?;

    let expected_key_id = short_key_id(&public_key);
    if expected_key_id != signature.key_id {
        bail!(
            "Signature key ID mismatch: expected {}, found {}",
            expected_key_id,
            signature.key_id
        );
    }

    UnparsedPublicKey::new(&ED25519, &public_key)
        .verify(payload, &sig)
        .map_err(|_| anyhow::anyhow!("Invalid receipt signature"))?;

    Ok(())
}

pub fn write_receipt(path: &Path, receipt: &ExecutionReceipt) -> Result<()> {
    crate::secure_fs::write_private_json(path, receipt)
        .with_context(|| format!("Failed to write receipt {}", path.display()))
}

pub fn load_receipt(path: &Path) -> Result<ExecutionReceipt> {
    let content = std::fs::read_to_string(path)
        .with_context(|| format!("Failed to read receipt {}", path.display()))?;
    let receipt: ExecutionReceipt = serde_json::from_str(&content)
        .with_context(|| format!("Invalid receipt JSON in {}", path.display()))?;
    Ok(receipt)
}

pub fn verify_receipt(receipt: &ExecutionReceipt, allow_unsigned: bool) -> Result<()> {
    if receipt.version != RECEIPT_VERSION {
        bail!(
            "Unsupported receipt version {} (expected {})",
            receipt.version,
            RECEIPT_VERSION
        );
    }

    let payload = receipt.payload_bytes()?;
    let expected = hash_bytes(&payload);
    if expected != receipt.payload_hash_sha256 {
        bail!(
            "Receipt hash mismatch: expected {}, found {}",
            expected,
            receipt.payload_hash_sha256
        );
    }

    match &receipt.signature {
        Some(signature) => verify_signature(signature, &payload)?,
        None => {
            if !allow_unsigned {
                bail!("Receipt is unsigned. Pass --allow-unsigned to verify legacy receipts.");
            }
        }
    }

    Ok(())
}

pub fn verify_receipt_file(path: &Path, allow_unsigned: bool) -> Result<ExecutionReceipt> {
    let receipt = load_receipt(path)?;
    verify_receipt(&receipt, allow_unsigned)?;
    Ok(receipt)
}

pub fn replay_args(receipt: &ExecutionReceipt) -> Vec<String> {
    match &receipt.invocation {
        Invocation::Run(run) => {
            let mut args = vec!["run".to_string()];
            if let Some(image) = &run.image {
                args.push("--image".to_string());
                args.push(image.clone());
            }
            if let Some(backend) = &run.backend {
                args.push("-B".to_string());
                args.push(backend.clone());
            }
            if run.fast {
                args.push("--fast".to_string());
            }
            if run.keep {
                args.push("--keep".to_string());
            }
            if run.no_network {
                args.push("--no-network".to_string());
            }
            args.push("--profile".to_string());
            args.push(run.profile.clone());
            args.push("--".to_string());
            args.extend(run.command.clone());
            args
        }
        Invocation::Exec(exec) => {
            let mut args = vec!["exec".to_string(), exec.name.clone()];
            for env in &exec.env {
                args.push("--env".to_string());
                args.push(env.clone());
            }
            if let Some(workdir) = &exec.workdir {
                args.push("--workdir".to_string());
                args.push(workdir.clone());
            }
            if exec.sudo {
                args.push("--sudo".to_string());
            }
            args.push("--".to_string());
            args.extend(exec.command.clone());
            args
        }
    }
}

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

    fn sample_invocation() -> Invocation {
        Invocation::Run(RunInvocation {
            command: vec![
                "python3".to_string(),
                "-c".to_string(),
                "print('ok')".to_string(),
            ],
            image: Some("python:3.12-alpine".to_string()),
            backend: Some("docker".to_string()),
            profile: "moderate".to_string(),
            no_network: false,
            fast: false,
            keep: false,
        })
    }

    fn unsigned_receipt() -> ExecutionReceipt {
        let mut rec = ExecutionReceipt {
            version: RECEIPT_VERSION,
            receipt_id: "test-receipt-1".to_string(),
            recorded_at: "2026-02-23T00:00:00Z".to_string(),
            invocation: sample_invocation(),
            outcome: ExecutionOutcome::from_combined_output(0, "ok\n", None),
            payload_hash_sha256: String::new(),
            signature: None,
        };
        let payload = rec.payload_bytes().expect("serialize payload");
        rec.payload_hash_sha256 = hash_bytes(&payload);
        rec
    }

    #[test]
    fn verify_unsigned_receipt_requires_flag() {
        let rec = unsigned_receipt();
        assert!(verify_receipt(&rec, false).is_err());
        assert!(verify_receipt(&rec, true).is_ok());
    }

    #[test]
    fn verify_receipt_detects_tampering() {
        let mut rec = unsigned_receipt();
        if let Invocation::Run(run) = &mut rec.invocation {
            run.command.push("--tampered".to_string());
        }
        let err = verify_receipt(&rec, true).expect_err("tampered receipt should fail");
        assert!(err.to_string().contains("Receipt hash mismatch"));
    }

    #[test]
    fn write_and_load_receipt_roundtrip() {
        let rec = unsigned_receipt();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("receipt.json");

        write_receipt(&path, &rec).expect("write receipt");
        let loaded = load_receipt(&path).expect("load receipt");

        assert_eq!(loaded.version, rec.version);
        assert_eq!(loaded.receipt_id, rec.receipt_id);
        assert_eq!(loaded.payload_hash_sha256, rec.payload_hash_sha256);
        assert!(verify_receipt(&loaded, true).is_ok());
    }

    #[test]
    fn replay_args_for_exec_include_options() {
        let exec_receipt = ExecutionReceipt {
            version: RECEIPT_VERSION,
            receipt_id: "exec-test".to_string(),
            recorded_at: "2026-02-23T00:00:00Z".to_string(),
            invocation: Invocation::Exec(ExecInvocation {
                name: "dev".to_string(),
                command: vec!["pytest".to_string(), "-q".to_string()],
                env: vec!["FOO=bar".to_string(), "DEBUG=1".to_string()],
                workdir: Some("/workspace".to_string()),
                sudo: true,
            }),
            outcome: ExecutionOutcome::from_combined_output(0, "", None),
            payload_hash_sha256: String::new(),
            signature: None,
        };

        let args = replay_args(&exec_receipt);
        assert_eq!(
            args,
            vec![
                "exec",
                "dev",
                "--env",
                "FOO=bar",
                "--env",
                "DEBUG=1",
                "--workdir",
                "/workspace",
                "--sudo",
                "--",
                "pytest",
                "-q",
            ]
            .into_iter()
            .map(std::string::ToString::to_string)
            .collect::<Vec<_>>()
        );
    }
}