vela-cli 0.940.5

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
use crate::cli::{collect_witness_files, fail, fail_return, parse_witness, print_json};
use crate::cli_commands::*;
use serde_json::{Value, json};
use std::path::{Component, Path, PathBuf};
use vela_protocol::repo;

pub(crate) fn cmd_verify_evidence(action: VerifyAction) {
    match action {
        VerifyAction::Import {
            frontier,
            record,
            actor,
            push,
            json,
        } => {
            crate::ui::set_mode("verification.import", json);
            let bytes = std::fs::read(&record).unwrap_or_else(|error| {
                fail_return(&format!("read {}: {error}", record.display()))
            });
            let record = vela_protocol::verification_record::VerificationRecordV1::parse(&bytes)
                .unwrap_or_else(|error| {
                    fail_return(&format!(
                        "parse {} as Verification Record v1: {error}",
                        record.display()
                    ))
                });
            let result = crate::workflow::import_verification(&frontier, &record, &actor, push)
                .unwrap_or_else(|error| fail_return(&error));
            if json {
                print_json(&result);
            } else {
                println!(
                    "verification import: retained {} for proposal {}",
                    result.verification_record_id, result.proposal_id
                );
                println!("  acceptance: unchanged (delta 0)");
                println!("  outcome: {}", result.outcome);
            }
        }
    }
}

fn verified_local_artifact(
    frontier: &Path,
    artifact: &vela_protocol::bundle::Artifact,
) -> Result<PathBuf, String> {
    verified_frontier_file(
        frontier,
        &artifact.id,
        &artifact.storage_mode,
        artifact.locator.as_deref(),
        &artifact.content_hash,
    )
}

fn verified_frontier_file(
    frontier: &Path,
    artifact_id: &str,
    storage_mode: &str,
    locator: Option<&str>,
    expected_root: &str,
) -> Result<PathBuf, String> {
    if !matches!(storage_mode, "local_blob" | "local_file") {
        return Err(format!(
            "proposal verifier artifact {} is not locally reproducible",
            artifact_id
        ));
    }
    let locator = locator
        .ok_or_else(|| format!("proposal verifier artifact {artifact_id} has no locator"))?;
    let relative = Path::new(locator);
    if relative.is_absolute()
        || relative.components().any(|component| {
            matches!(
                component,
                Component::ParentDir | Component::RootDir | Component::Prefix(_)
            )
        })
    {
        return Err(format!(
            "proposal verifier artifact {} locator must remain frontier-relative",
            artifact_id
        ));
    }
    let frontier_root = std::fs::canonicalize(frontier)
        .map_err(|error| format!("resolve frontier root: {error}"))?;
    let file = frontier.join(relative);
    let metadata = std::fs::symlink_metadata(&file).map_err(|error| {
        format!(
            "inspect proposal verifier artifact {}: {error}",
            artifact_id
        )
    })?;
    if !metadata.file_type().is_file() || metadata.file_type().is_symlink() {
        return Err(format!(
            "proposal verifier artifact {} must be a regular non-symlink file",
            artifact_id
        ));
    }
    let resolved = std::fs::canonicalize(&file).map_err(|error| {
        format!(
            "resolve proposal verifier artifact {}: {error}",
            artifact_id
        )
    })?;
    if !resolved.starts_with(&frontier_root) {
        return Err(format!(
            "proposal verifier artifact {} resolves outside the frontier",
            artifact_id
        ));
    }
    use sha2::{Digest, Sha256};
    let bytes = std::fs::read(&resolved)
        .map_err(|error| format!("read proposal verifier artifact {}: {error}", artifact_id))?;
    let observed = format!("sha256:{}", hex::encode(Sha256::digest(&bytes)));
    if observed != expected_root {
        return Err(format!(
            "proposal verifier artifact {} content root does not match retained bytes",
            artifact_id
        ));
    }
    Ok(resolved)
}

fn reproduction_result_path(frontier: &Path, file: &Path, proposal_scoped: bool) -> String {
    if !proposal_scoped {
        return file.display().to_string();
    }
    let root = std::fs::canonicalize(frontier).unwrap_or_else(|_| frontier.to_path_buf());
    let resolved = std::fs::canonicalize(file).unwrap_or_else(|_| file.to_path_buf());
    resolved.strip_prefix(&root).map_or_else(
        |_| {
            file.file_name().map_or_else(
                || "artifact".to_string(),
                |name| name.to_string_lossy().into(),
            )
        },
        |relative| relative.display().to_string(),
    )
}

pub(crate) fn proposal_reproduction_files(
    path: &Path,
    proposal_id: &str,
) -> Result<Vec<PathBuf>, String> {
    let project =
        repo::load_from_path(path).map_err(|error| format!("load proposal frontier: {error}"))?;
    let proposal = project
        .proposals
        .iter()
        .find(|proposal| proposal.id == proposal_id)
        .ok_or_else(|| format!("proposal {proposal_id} does not exist"))?;
    if proposal.status != "pending_review" {
        return Err(format!(
            "proposal {proposal_id} is {}, not pending_review",
            proposal.status
        ));
    }
    if proposal.kind != "finding.add" {
        return Err(format!(
            "proposal-scoped reproduction requires finding.add, got {}",
            proposal.kind
        ));
    }
    let finding_id = proposal.target.id.as_str();
    let retained = project
        .artifacts
        .iter()
        .filter(|artifact| {
            artifact.media_type.as_deref() == Some("application/json")
                && artifact.metadata.contains_key("verifier")
                && artifact
                    .target_findings
                    .iter()
                    .any(|target| target == finding_id)
        })
        .collect::<Vec<_>>();
    if !retained.is_empty() {
        return retained
            .into_iter()
            .map(|artifact| {
                let file = verified_local_artifact(path, artifact)?;
                let raw = std::fs::read_to_string(&file).map_err(|error| {
                    format!("read proposal verifier artifact {}: {error}", artifact.id)
                })?;
                parse_witness(&raw).map_err(|error| {
                    format!(
                        "proposal verifier artifact {} is not a frozen witness: {error}",
                        artifact.id
                    )
                })?;
                Ok(file)
            })
            .collect::<Result<Vec<_>, _>>();
    }

    let submission = proposal
        .payload
        .get("vela_submission")
        .and_then(Value::as_object)
        .ok_or_else(|| "pending proposal has no Receipt binding".to_string())?;
    let receipt_path = submission
        .get("receipt_path")
        .and_then(Value::as_str)
        .ok_or_else(|| "proposal Receipt path is unavailable".to_string())?;
    let expected_root = submission
        .get("receipt_root")
        .and_then(Value::as_str)
        .ok_or_else(|| "proposal Receipt root is unavailable".to_string())?;
    let receipt_file = verified_frontier_relative_file(path, "proposal Receipt", receipt_path)?;
    let receipt_bytes = std::fs::read(&receipt_file)
        .map_err(|error| format!("read proposal Receipt {receipt_path}: {error}"))?;
    let receipt = vela_protocol::receipt_v1::ReceiptV1::parse(&receipt_bytes)
        .map_err(|error| format!("parse proposal Receipt: {error}"))?;
    let observed_root = receipt
        .canonical_root()
        .map_err(|error| format!("root proposal Receipt: {error}"))?;
    if observed_root != expected_root {
        return Err("proposal Receipt root does not match its retained bytes".to_string());
    }

    receipt
        .as_value()
        .get("artifacts")
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter(|artifact| {
            artifact
                .get("kind")
                .and_then(Value::as_str)
                .is_some_and(|kind| kind.contains("witness"))
                || artifact
                    .get("path")
                    .and_then(Value::as_str)
                    .is_some_and(|path| path.ends_with(".witness.json"))
        })
        .filter_map(|artifact| artifact.get("sha256").and_then(Value::as_str))
        .map(|digest| {
            use sha2::{Digest, Sha256};
            let relative = format!("records/artifacts/sha256/{digest}");
            let file = verified_frontier_relative_file(path, "proposal artifact", &relative)?;
            let bytes = std::fs::read(&file)
                .map_err(|error| format!("read proposal artifact {}: {error}", file.display()))?;
            let observed = hex::encode(Sha256::digest(&bytes));
            if observed != digest {
                return Err("proposal artifact digest does not match retained bytes".to_string());
            }
            Ok(file)
        })
        .collect::<Result<Vec<_>, String>>()
}

fn verified_frontier_relative_file(
    frontier: &Path,
    label: &str,
    locator: &str,
) -> Result<PathBuf, String> {
    let relative = Path::new(locator);
    if relative.is_absolute()
        || relative.components().any(|component| {
            matches!(
                component,
                Component::ParentDir | Component::RootDir | Component::Prefix(_)
            )
        })
    {
        return Err(format!("{label} path must remain frontier-relative"));
    }
    let frontier_root = std::fs::canonicalize(frontier)
        .map_err(|error| format!("resolve frontier root: {error}"))?;
    let file = frontier.join(relative);
    let metadata =
        std::fs::symlink_metadata(&file).map_err(|error| format!("inspect {label}: {error}"))?;
    if !metadata.file_type().is_file() || metadata.file_type().is_symlink() {
        return Err(format!("{label} must be a regular non-symlink file"));
    }
    let resolved =
        std::fs::canonicalize(&file).map_err(|error| format!("resolve {label}: {error}"))?;
    if !resolved.starts_with(&frontier_root) {
        return Err(format!("{label} resolves outside the frontier"));
    }
    Ok(resolved)
}

pub(crate) fn cmd_reproduce(path: &Path, proposal_id: Option<&str>, json_output: bool) {
    crate::ui::set_mode("reproduce", json_output);
    if path.is_dir()
        && path.join("frontier.yaml").is_file()
        && !path.join(".vela/epoch.json").is_file()
    {
        crate::ui::fail_with(
            crate::ui::ErrorKind::Domain,
            "this Vela release reproduces only current repository epochs",
            Some(
                "inspect a predecessor with its pinned historical Vela release; current repositories contain `.vela/epoch.json`",
            ),
        );
    }
    let mut scope = if path.is_file() {
        "standalone_artifact"
    } else {
        "accepted_frontier"
    };
    if !json_output {
        crate::ui::header("REPRODUCE", &path.display().to_string(), None);
    }
    let mut files = if let Some(proposal_id) = proposal_id {
        scope = "pending_proposal";
        proposal_reproduction_files(path, proposal_id).unwrap_or_else(|error| fail_return(&error))
    } else {
        collect_witness_files(path)
    };
    // Also re-verify content-addressed witness blobs landed as verifier-tagged
    // artifacts under `.vela/artifact-blobs/`, bound to their findings rather
    // than stored as loose files. Cover any blob not already present as a loose
    // witness (deduped by content hash).
    if proposal_id.is_none()
        && !path.join(".vela/epoch.json").is_file()
        && let Ok(source) = repo::detect(path)
        && let Ok(proj) = repo::load(&source)
    {
        use sha2::{Digest, Sha256};
        let loose: std::collections::HashSet<String> = files
            .iter()
            .filter_map(|f| std::fs::read(f).ok())
            .map(|b| format!("sha256:{}", hex::encode(Sha256::digest(&b))))
            .collect();
        for art in &proj.artifacts {
            if art.media_type.as_deref() != Some("application/json")
                || !art.metadata.contains_key("verifier")
                || loose.contains(&art.content_hash)
            {
                continue;
            }
            if let ("local_blob" | "local_file", Some(loc)) =
                (art.storage_mode.as_str(), &art.locator)
            {
                let blob = path.join(loc);
                if blob.exists() {
                    files.push(blob);
                }
            }
        }
    }
    if files.is_empty() {
        if let Some(proposal_id) = proposal_id {
            fail(&format!(
                "proposal {proposal_id} has no frontier-local frozen witness to reproduce; inspect its retained artifacts and verifier evidence, or use the producer's exact replay bundle"
            ));
        }
        fail(&format!(
            "no witnesses found at {} (expected a `*.witness.json` file, or a directory containing them / a `witnesses/` subdir)",
            path.display()
        ));
    }
    let spinner = (!json_output).then(|| {
        crate::cli::progress::Spinner::start(&format!(
            "re-verifying {} witness(es) with the frozen verifiers",
            files.len()
        ))
    });
    let mut results: Vec<Value> = Vec::new();
    let mut passed = 0usize;
    let mut failed = 0usize;
    for file in &files {
        let result_path = reproduction_result_path(path, file, proposal_id.is_some());
        let raw = match std::fs::read_to_string(file) {
            Ok(r) => r,
            Err(e) => {
                failed += 1;
                if !json_output {
                    println!("  FAIL  {result_path}  ·  read error: {e}");
                }
                results.push(json!({"path": result_path, "ok": false, "message": format!("read error: {e}")}));
                continue;
            }
        };
        let witness = match parse_witness(&raw) {
            Ok(w) => w,
            Err(e) => {
                failed += 1;
                if !json_output {
                    println!("  FAIL  {result_path}  ·  parse error: {e}");
                }
                results.push(json!({"path": result_path, "ok": false, "message": format!("parse error: {e}")}));
                continue;
            }
        };
        let mut outcome = vela_verify::verify_witness(&witness);
        // Machine-checked novelty: a witness may declare `improves_on`
        // (a sibling witness path relative to its own directory). The
        // claim then verifies ONLY if it also strictly dominates the
        // referenced witness — dominance is arithmetic, not opinion.
        if outcome.ok
            && let Ok(value) = serde_json::from_str::<Value>(&raw)
            && let Some(prior_rel) = value.get("improves_on").and_then(Value::as_str)
        {
            let prior_path = file
                .parent()
                .map(|d| d.join(prior_rel))
                .unwrap_or_else(|| std::path::PathBuf::from(prior_rel));
            match std::fs::read_to_string(&prior_path)
                .map_err(|e| format!("improves_on read {}: {e}", prior_path.display()))
                .and_then(|p| parse_witness(&p))
                .and_then(|prior| vela_verify::dominates(&witness, &prior))
            {
                Ok(true) => {
                    outcome.message =
                        format!("{} · strictly improves on {prior_rel}", outcome.message);
                }
                Ok(false) => {
                    outcome = vela_verify::VerifyResult::fail(format!(
                        "claims improves_on {prior_rel} but does NOT strictly dominate it"
                    ));
                }
                Err(e) => {
                    outcome =
                        vela_verify::VerifyResult::fail(format!("improves_on check failed: {e}"));
                }
            }
        }
        if outcome.ok {
            passed += 1;
        } else {
            failed += 1;
        }
        if !json_output {
            let status = if outcome.ok { "ok  " } else { "FAIL" };
            println!(
                "  {status}  {} [{}]  ·  {}",
                result_path,
                witness.kind(),
                outcome.message
            );
        }
        results.push(json!({
            "path": result_path,
            "kind": witness.kind(),
            "ok": outcome.ok,
            "message": outcome.message,
        }));
    }
    if let Some(s) = spinner {
        s.finish(&format!("{passed} verified, {failed} failed"));
    }
    if json_output {
        println!(
            "{}",
            serde_json::to_string_pretty(&json!({
                "command": "reproduce",
                "scope": scope,
                "proposal_id": proposal_id,
                "authority_effect": "none",
                "witnesses": files.len(),
                "passed": passed,
                "failed": failed,
                "results": results,
            }))
            .expect("serialize reproduce response")
        );
    } else {
        println!();
        println!("  scope: {scope}");
        if let Some(proposal_id) = proposal_id {
            println!("  proposal: {proposal_id} (pending; acceptance unchanged)");
        }
        if failed == 0 {
            println!(
                "  reproduce: ok ({passed}/{}) — every witness re-verified from scratch by the frozen verifiers.",
                files.len()
            );
        } else {
            println!(
                "  reproduce: FAIL ({failed}/{} did not re-verify). Investigate before trusting.",
                files.len()
            );
        }
    }
    if failed > 0 {
        std::process::exit(1);
    }
}

#[cfg(test)]
mod gate_tests {
    use super::*;
    use sha2::{Digest, Sha256};

    // The exact-lane vouch gate. An adversarial review showed the prior vouch
    // (a "registered non-agent reviewer" signing a verifier_attachment.added
    // event, or accepting a verifier.attach proposal) is forgeable: actor
    // registration is open self-enrollment, so an agent mints a key, registers
    // `reviewer:x`, and honestly signs. The fix scopes the vouch to where
    // attachments are load-bearing (the non-floor lane), and admits the exact
    // lane on the un-forgeable FLOOR alone.

    #[test]
    fn proposal_reproduction_reads_only_rooted_frontier_files() {
        let frontier = tempfile::tempdir().unwrap();
        std::fs::create_dir(frontier.path().join("records")).unwrap();
        let bytes = br#"{"schema":"fixture"}"#;
        std::fs::write(frontier.path().join("records/witness.json"), bytes).unwrap();
        let root = format!("sha256:{}", hex::encode(Sha256::digest(bytes)));
        let resolved = verified_frontier_file(
            frontier.path(),
            "va_fixture",
            "local_file",
            Some("records/witness.json"),
            &root,
        )
        .unwrap();
        assert!(resolved.starts_with(std::fs::canonicalize(frontier.path()).unwrap()));
        assert_eq!(
            reproduction_result_path(frontier.path(), &resolved, true),
            "records/witness.json"
        );

        let traversal = verified_frontier_file(
            frontier.path(),
            "va_fixture",
            "local_file",
            Some("../secret.json"),
            &root,
        )
        .unwrap_err();
        assert!(traversal.contains("frontier-relative"));

        let tampered = verified_frontier_file(
            frontier.path(),
            "va_fixture",
            "local_file",
            Some("records/witness.json"),
            &format!("sha256:{}", "0".repeat(64)),
        )
        .unwrap_err();
        assert!(tampered.contains("content root"));
    }
}