vela-cli 0.910.0

The vela command-line tool: build, check, sign, replay, and publish scientific frontier state.
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! A cloned frontier must not be able to configure the operator.
//!
//! Regression tests for the working-tree `.env` injection: `dotenvy`
//! used to ancestor-walk from cwd, so a frontier repo could commit a
//! `.env` that silently set VELA_ACTOR_ID / VELA_HUB_URL /
//! VELA_NO_PUBLISH for anyone running vela inside it — the attack
//! class git's protected configuration and Codex's project-scope key
//! blocking exist for. The CLI now reads NO .env from the working
//! tree; these tests hold that line.

use std::process::Command;

use ed25519_dalek::SigningKey;
use sha2::{Digest, Sha256};
use vela_protocol::identity::{ActorClass, IdentityBinding, IdentityBindingDraft};
use vela_protocol::receipt_v1::{ArtifactInput, ReceiptBuilder, ReceiptInput};

fn vela_bin() -> &'static str {
    env!("CARGO_BIN_EXE_vela")
}

/// Run vela in `dir` with a SCRUBBED environment (no VELA_* inherited)
/// so the only possible source of the poisoned values is the .env file.
fn run_scrubbed(dir: &std::path::Path, args: &[&str]) -> std::process::Output {
    let mut cmd = Command::new(vela_bin());
    cmd.current_dir(dir).args(args);
    for (k, _) in std::env::vars() {
        if k.starts_with("VELA_") {
            cmd.env_remove(k);
        }
    }
    // Point HOME at the sandbox so the real ~/.vela profile can't leak in.
    cmd.env("HOME", dir);
    cmd.output().expect("spawn vela")
}

fn init_frontier(dir: &std::path::Path) {
    let out = Command::new(vela_bin())
        .current_dir(dir)
        .env("HOME", dir)
        .args([
            "init",
            ".",
            "--name",
            "envtest",
            "--scope",
            "Exercise environment isolation.",
        ])
        .output()
        .expect("init");
    assert!(out.status.success(), "init failed: {out:?}");
}

fn run_in(dir: &std::path::Path, args: &[&str]) -> std::process::Output {
    Command::new(vela_bin())
        .current_dir(dir)
        .env("HOME", dir)
        .env("VELA_NO_PUBLISH", "1")
        .args(args)
        .output()
        .expect("spawn vela")
}

fn promote_fixture_identity_to_human(dir: &std::path::Path, handle: &str) {
    let path = dir.join(".vela/identity.json");
    let mut identity: serde_json::Value =
        serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
    identity["actor_id"] = serde_json::json!(format!("reviewer:{handle}"));
    identity["actor_type"] = serde_json::json!("human");
    std::fs::write(
        path,
        format!("{}\n", serde_json::to_string_pretty(&identity).unwrap()),
    )
    .unwrap();
}

fn write_current_receipt(dir: &std::path::Path, filename: &str, claim: &str, replayability: &str) {
    let artifact_path = "witnesses/w.json";
    let artifact = std::fs::read(dir.join(artifact_path)).unwrap();
    let artifact_digest = hex::encode(Sha256::digest(&artifact));
    let project = vela_protocol::repo::load_from_path(dir).unwrap();
    let event_root = format!(
        "sha256:{}",
        vela_protocol::events::event_log_hash(&project.events)
    );
    let operation_id = format!(
        "vop_{}",
        hex::encode(Sha256::digest(
            format!("{claim}\0{replayability}\0{artifact_digest}").as_bytes()
        ))
    );
    let emitted_at = "2026-07-13T12:00:00Z";
    let identity = IdentityBinding::build(
        IdentityBindingDraft {
            actor_id: "agent:t".to_string(),
            actor_class: ActorClass::Agent,
            created_at: emitted_at.to_string(),
        },
        &SigningKey::from_bytes(&[0x51; 32]),
    )
    .unwrap();
    let input = ReceiptInput::new(
        claim.to_string(),
        "computational".to_string(),
        replayability.to_string(),
        vec![
            ArtifactInput::new(
                artifact_path.to_string(),
                "witness".to_string(),
                Some(artifact_digest),
                None,
            )
            .unwrap(),
        ],
        vec!["fixture evidence only".to_string()],
        Vec::new(),
        "agent:t".to_string(),
        emitted_at.to_string(),
        event_root,
        ".".to_string(),
        operation_id,
        "urn:vela:policy:none".to_string(),
    )
    .unwrap();
    let receipt = ReceiptBuilder::build(input, &identity).unwrap();
    std::fs::write(dir.join(filename), receipt.canonical_bytes().unwrap()).unwrap();
}

/// Malformed invocations across the command families must be exit 2
/// (usage), not the generic exit 1 — the same class fixed in `state`,
/// swept through cli_state / cli_admin / cli_check.
#[test]
fn usage_errors_are_exit_2() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    // `check --json` with no frontier source is a usage error.
    let out = run_in(tmp.path(), &["check", "--json"]);
    assert_eq!(
        out.status.code(),
        Some(2),
        "check --json no source: {out:?}"
    );
    // Retired state writers fail as usage errors before touching the frontier.
    let out = run_in(tmp.path(), &["state", "anchor", ".", "vf_x", "--json"]);
    assert_eq!(
        out.status.code(),
        Some(2),
        "retired state anchor writer: {out:?}"
    );
    // `id rotate-key` with identical old/new id is a usage error.
    let out = run_in(
        tmp.path(),
        &[
            "id",
            "rotate-key",
            "--id",
            "reviewer:x",
            "--new-id",
            "reviewer:x",
            "--json",
        ],
    );
    assert_eq!(out.status.code(), Some(2), "id rotate same id: {out:?}");
}

/// The exit-code contract is what an agent branches on. A missing finding
/// must be 3 (not found), while a malformed invocation is 2 (usage).
#[test]
fn finding_show_honors_the_exit_code_contract() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    let dir = ".";
    // A well-formed but absent finding id → not found (3).
    let out = run_in(
        tmp.path(),
        &[
            "finding",
            "show",
            dir,
            "vf_ffffffffffffffff",
            "--view",
            "standing",
            "--json",
        ],
    );
    assert_eq!(
        out.status.code(),
        Some(3),
        "missing finding must be exit 3: {out:?}"
    );
    // A malformed invocation (no operands) → usage (2).
    let out = run_in(tmp.path(), &["finding", "show", "--json"]);
    assert_eq!(
        out.status.code(),
        Some(2),
        "usage error must be exit 2: {out:?}"
    );
}

/// Landing the same operation identity and normalized receipt is a retry, not
/// a new finding. The
/// second land must be exit 5 (already_exists) and must NOT fork a twin
/// into the sign queue.
#[test]
fn land_is_idempotent_on_the_claim() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    assert!(
        run_in(tmp.path(), &["id", "create", "--handle", "t", "--agent"])
            .status
            .success()
    );
    std::fs::create_dir_all(tmp.path().join("witnesses")).unwrap();
    std::fs::write(tmp.path().join("witnesses/w.json"), "{\"k\":\"d\"}").unwrap();
    write_current_receipt(
        tmp.path(),
        "r.json",
        "idempotency regression claim",
        "exact",
    );

    let first = run_in(tmp.path(), &["land", "r.json", "--as", "agent:t", "--json"]);
    assert!(
        first.status.success(),
        "first land should succeed: {first:?}"
    );
    let second = run_in(tmp.path(), &["land", "r.json", "--as", "agent:t", "--json"]);
    assert!(
        second.status.success(),
        "an exact operation retry must reuse its durable result: {second:?}"
    );
    let second_json: serde_json::Value = serde_json::from_slice(&second.stdout).unwrap();
    assert_eq!(second_json["route"], "exact_retry");

    // Exactly one item in the sign queue — no twin.
    let q = run_in(tmp.path(), &["sign", "--json"]);
    let v: serde_json::Value = serde_json::from_slice(&q.stdout).unwrap();
    assert_eq!(
        v["signable_total"], 1,
        "the retry must not have forked a duplicate: {v}"
    );
}

/// The Receipt v1 replayability class: an explicit honest value lands and a
/// value outside the frozen closed set is rejected.
#[test]
fn land_honors_the_replayability_class() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    assert!(
        run_in(tmp.path(), &["id", "create", "--handle", "t", "--agent"])
            .status
            .success()
    );
    std::fs::create_dir_all(tmp.path().join("witnesses")).unwrap();
    std::fs::write(tmp.path().join("witnesses/w.json"), "{\"k\":\"d\"}").unwrap();

    // A valid, honest replayability value lands.
    write_current_receipt(
        tmp.path(),
        "ok.json",
        "an approximately-replayable hosted-model run",
        "approximate",
    );
    let ok = run_in(
        tmp.path(),
        &["land", "ok.json", "--as", "agent:t", "--json"],
    );
    assert!(
        ok.status.success(),
        "an `approximate` receipt should land: {ok:?}"
    );

    // A value outside the closed set is rejected on the usage contract (exit 2).
    write_current_receipt(tmp.path(), "bad.json", "a mislabeled run", "exact");
    let mut bad_receipt: serde_json::Value =
        serde_json::from_slice(&std::fs::read(tmp.path().join("bad.json")).unwrap()).unwrap();
    bad_receipt["replayability"] =
        serde_json::Value::String("totally-reproducible-trust-me".to_string());
    std::fs::write(
        tmp.path().join("bad.json"),
        serde_json::to_vec(&bad_receipt).unwrap(),
    )
    .unwrap();
    let bad = run_in(
        tmp.path(),
        &["land", "bad.json", "--as", "agent:t", "--json"],
    );
    assert!(
        !bad.status.success(),
        "an unknown replayability class must be rejected: {bad:?}"
    );
    let text = format!(
        "{}{}",
        String::from_utf8_lossy(&bad.stdout),
        String::from_utf8_lossy(&bad.stderr)
    );
    assert!(
        text.contains("replayability") || text.contains("totally-reproducible-trust-me"),
        "the rejection should identify the offending replayability value: {text}"
    );
}

/// Pinning a build-tree binary (the `vela` -> `scripts/vela` wrapper trap)
/// warns: its hash churns on every `cargo build`, so the next ceremony would
/// mismatch. The test binary IS `target/debug/vela`, a dev build, so the guard
/// must fire. (It still records the pin — the human asked — it just says so.)
#[test]
fn pin_binary_warns_on_a_dev_build() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    assert!(
        run_in(
            tmp.path(),
            &["id", "create", "--handle", "probe", "--agent"]
        )
        .status
        .success()
    );
    promote_fixture_identity_to_human(tmp.path(), "probe");
    let out = run_in(tmp.path(), &["id", "pin-binary", "--yes"]);
    assert!(out.status.success(), "pin should record: {out:?}");
    let text = format!(
        "{}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(
        text.contains("build-tree binary"),
        "pinning a target/ binary must warn: {text}"
    );
}

/// The poisoned .env sets VELA_ACTOR_ID=agent:evil. If the CLI loaded
/// it, the sign ceremony would refuse with the CUSTODY exit (4). It must
/// instead fail on identity setup / lookup — anything but 4.
#[test]
fn frontier_env_cannot_set_actor_id() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    std::fs::write(
        tmp.path().join(".env"),
        "VELA_ACTOR_ID=agent:evil\nVELA_KEY_PATH=/tmp/evil.key\n",
    )
    .unwrap();

    let out = run_scrubbed(tmp.path(), &["sign", "vpr_x", "--yes", "--reason", "x"]);
    let code = out.status.code().unwrap_or(-1);
    assert_ne!(
        code,
        4,
        "exit 4 means the custody gate saw agent:evil — the .env was loaded: {}",
        String::from_utf8_lossy(&out.stderr)
    );
}

/// The poisoned .env sets VELA_ADVICE=0. If loaded, error hints vanish.
/// The hint must still render.
#[test]
fn frontier_env_cannot_mute_advice() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    std::fs::write(tmp.path().join(".env"), "VELA_ADVICE=0\n").unwrap();

    // A partial scripted-confirmation token prints a usage error WITH the
    // corrective hint and fires before any identity lookup. A bare proposal
    // id is now a valid key-free preview, so it is not a usage error.
    let out = run_scrubbed(
        tmp.path(),
        &[
            "sign",
            "vpr_x",
            "--confirm-root",
            "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        ],
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("hint:"),
        "the hint vanished — VELA_ADVICE=0 leaked from the frontier .env: {stderr}"
    );
}

/// Real environment variables must still work (the cut removed the
/// working-tree file, not env-var support): VELA_ACTOR_ID from the
/// actual process environment reaches the custody gate.
#[test]
fn real_env_still_resolves() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    let mut cmd = Command::new(vela_bin());
    cmd.current_dir(tmp.path())
        .env("HOME", tmp.path())
        .env("VELA_ACTOR_ID", "agent:probe")
        .args(["sign", "vpr_x", "--yes", "--reason", "x"]);
    let out = cmd.output().expect("spawn");
    assert_eq!(
        out.status.code(),
        Some(4),
        "a REAL env VELA_ACTOR_ID=agent: must hit the custody gate: {}",
        String::from_utf8_lossy(&out.stderr)
    );
}

/// A stale pin refuses the CEREMONY, not the read-only list: `sign
/// --json` is how agents and the plugin render the queue, and a changed
/// binary must never take that down (only the pen stops).
#[test]
fn stale_pin_blocks_ceremony_not_list() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    let run = |args: &[&str]| {
        Command::new(vela_bin())
            .current_dir(tmp.path())
            .env("HOME", tmp.path())
            .env("VELA_NO_PUBLISH", "1")
            .args(args)
            .output()
            .expect("spawn")
    };
    let out = run(&["id", "create", "--handle", "probe", "--agent"]);
    assert!(out.status.success(), "{out:?}");
    promote_fixture_identity_to_human(tmp.path(), "probe");
    let out = run(&["id", "pin-binary", "--yes"]);
    assert!(out.status.success(), "pin failed: {out:?}");
    // Rewrite the pin to a hash the binary cannot match ("it changed").
    let pin_path = tmp.path().join(".vela").join("binary-pin.json");
    let mut pin: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&pin_path).unwrap()).unwrap();
    pin["sha256"] = serde_json::Value::String("0".repeat(64));
    std::fs::write(&pin_path, serde_json::to_string_pretty(&pin).unwrap()).unwrap();

    // The read-only list still serves.
    let out = run(&["sign", "--json"]);
    assert!(
        out.status.success(),
        "sign --json must stay a plain read under a stale pin: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(
        String::from_utf8_lossy(&out.stdout).contains("signable_total"),
        "list shape missing: {}",
        String::from_utf8_lossy(&out.stdout)
    );

    // The ceremony refuses with the custody exit and names the mismatch.
    let out = run(&["sign", "--frontier", "."]);
    assert_eq!(
        out.status.code(),
        Some(4),
        "a stale pin must stop the ceremony: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("does not match your pin"),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Detached exact-byte signing is the same human-key ceremony. It must
    // refuse before reading a key or writing a signature when the binary pin
    // is stale.
    let subject = tmp.path().join("exact-bytes.txt");
    std::fs::write(&subject, b"exact bytes\n").unwrap();
    let out = run(&[
        "sign",
        "exact-bytes.txt",
        "--key",
        "missing-human-key",
        "--json",
    ]);
    assert_eq!(
        out.status.code(),
        Some(4),
        "a stale pin must stop detached signing before key resolution: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let detached_output = format!(
        "{}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(
        detached_output.contains("does not match your pin"),
        "{detached_output}"
    );
    assert!(
        !tmp.path().join("exact-bytes.txt.sig.json").exists(),
        "a refused detached ceremony must not write a signature"
    );
}

/// The binary pin holds: pin a copy of the binary, mutate it, and the
/// ceremony refuses with the custody exit. The clear-signing invariant
/// as a regression test.
#[test]
fn tampered_binary_refuses_ceremony() {
    let tmp = tempfile::TempDir::new().unwrap();
    init_frontier(tmp.path());
    let bin_copy = tmp.path().join("vela-bin");
    std::fs::copy(vela_bin(), &bin_copy).unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&bin_copy, std::fs::Permissions::from_mode(0o755)).unwrap();
    }
    let run = |args: &[&str]| {
        Command::new(&bin_copy)
            .current_dir(tmp.path())
            .env("HOME", tmp.path())
            .env("VELA_NO_PUBLISH", "1")
            .args(args)
            .output()
            .expect("spawn copy")
    };
    // Identity + pin (human act, --yes for the test).
    let out = run(&["id", "create", "--handle", "probe", "--agent"]);
    assert!(out.status.success(), "{out:?}");
    promote_fixture_identity_to_human(tmp.path(), "probe");
    let out = run(&["id", "pin-binary", "--yes"]);
    assert!(out.status.success(), "pin failed: {out:?}");
    // Tamper.
    let mut bytes = std::fs::read(&bin_copy).unwrap();
    bytes.push(0);
    std::fs::write(&bin_copy, bytes).unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&bin_copy, std::fs::Permissions::from_mode(0o755)).unwrap();
    }
    let out = run(&["sign", "--frontier", "."]);
    // Two refusal layers both count: vela's pin check (exit 4), or —
    // on macOS — the kernel killing the copy outright because the
    // mutation broke its code signature (status None = died by
    // signal). Either way, the tampered binary produced no ceremony.
    match out.status.code() {
        Some(4) => {
            let err = String::from_utf8_lossy(&out.stderr);
            assert!(err.contains("does not match your pin"), "{err}");
        }
        None => {} // killed by the OS before main — defense in depth
        other => panic!(
            "a tampered binary must not run the ceremony (got {other:?}): {}",
            String::from_utf8_lossy(&out.stderr)
        ),
    }
}