vela-cli 0.914.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! Explicit, local consumer pinning for a Frontier's first administrator boundary.
//!
//! Repository bytes may prove a complete boundary chain, but they cannot
//! choose which first administrator fork the consumer intended. This command
//! therefore requires the full boundary root from an out-of-band source,
//! previews an exact local trust-anchor plan without writing, and installs it
//! only after a matching confirmation root is supplied.

use std::path::Path;

use serde::Serialize;
use sha2::{Digest, Sha256};
use vela_edge::frontier_repository::{
    RepositoryTrustAnchor, verify_repository_boundary_context_with_trust_anchor,
};
use vela_edge::repository_write::{
    REPOSITORY_TRUST_ANCHOR_SCHEMA_V1, RepositoryTrustAnchorV1,
    install_repository_trust_anchor_from_home,
};
use vela_protocol::events::event_log_hash;
use vela_protocol::frontier_profile::EffectiveFrontierAuthorityV1;
use vela_protocol::frontier_repo::{FrontierProfileFile, read_repository_profile};
use vela_protocol::frontier_repository::{
    FrontierRepositoryBoundaryMode, FrontierRepositoryTrustMode,
    repository_boundary_event_content_root, repository_boundary_payload_from_event_shape,
    repository_identity_event_content_root,
};

const TRUST_PIN_PLAN_SCHEMA: &str = "vela.repository-trust-pin-plan.v1";
const TRUST_PIN_PLAN_DOMAIN: &[u8] = b"vela.repository-trust-pin-plan.v1\0";

#[derive(Debug, Clone, Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct RepositoryTrustPinPlan {
    schema: String,
    ok: bool,
    command: String,
    frontier: String,
    frontier_id: String,
    identity_root: String,
    identity_head_root: String,
    supplied_first_boundary_root: String,
    administrator_actor_id: String,
    administrator_public_key: String,
    boundary_mode: String,
    boundary_trust_mode: String,
    git_commit: String,
    git_tree: String,
    event_log_root: String,
    event_count: u64,
    trust_anchor: RepositoryTrustAnchorV1,
    trust_anchor_root: String,
    observed_at: String,
    writes: Vec<String>,
    plan_root: String,
}

fn git(frontier: &Path, args: &[&str]) -> Result<String, String> {
    crate::git_hardened::text(frontier, args)
}

fn compute_plan_root(plan: &RepositoryTrustPinPlan) -> Result<String, String> {
    let mut value =
        serde_json::to_value(plan).map_err(|error| format!("encode trust-pin plan: {error}"))?;
    value
        .as_object_mut()
        .ok_or_else(|| "trust-pin plan is not an object".to_string())?
        .remove("plan_root");
    let canonical = vela_protocol::canonical::to_canonical_bytes(&value)?;
    let mut digest = Sha256::new();
    digest.update(TRUST_PIN_PLAN_DOMAIN);
    digest.update(canonical);
    Ok(format!("sha256:{}", hex::encode(digest.finalize())))
}

fn verify_plan(plan: &RepositoryTrustPinPlan) -> Result<(), String> {
    if plan.schema != TRUST_PIN_PLAN_SCHEMA
        || !plan.ok
        || plan.command != "frontier.trust.pin"
        || plan.plan_root != compute_plan_root(plan)?
    {
        return Err("repository trust-pin plan is malformed or has a stale root".to_string());
    }
    Ok(())
}

fn prepare_trust_pin(
    frontier: &Path,
    supplied_first_boundary_root: &str,
    observed_at: &str,
) -> Result<RepositoryTrustPinPlan, String> {
    let Some(digest) = supplied_first_boundary_root.strip_prefix("sha256:") else {
        return Err("supplied first boundary root must use sha256:<64 lowercase hex>".to_string());
    };
    if digest.len() != 64
        || !digest
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    {
        return Err("supplied first boundary root must use sha256:<64 lowercase hex>".to_string());
    }
    chrono::DateTime::parse_from_rfc3339(observed_at)
        .map_err(|error| format!("trust-pin observation time must be RFC3339: {error}"))?;
    let frontier = std::fs::canonicalize(frontier)
        .map_err(|error| format!("resolve Frontier checkout: {error}"))?;
    let dirt = vela_edge::git_read::dirty_worktree_paths(&frontier, true)?;
    if !dirt.is_empty() {
        return Err(format!(
            "trust pinning requires a clean checkout; found {}",
            dirt.join(", ")
        ));
    }
    let project = vela_protocol::repo::load_from_path(&frontier)?;
    let profile = match read_repository_profile(&frontier)? {
        Some(FrontierProfileFile::V1(profile)) => profile,
        Some(FrontierProfileFile::LegacyV0_1(_)) | None => {
            return Err("trust pinning requires vela.frontier-profile.v1".to_string());
        }
    };
    profile.validate()?;
    let authority = EffectiveFrontierAuthorityV1::from_events(&project.events)?;
    profile.assert_frontier_id(&authority.frontier_id)?;
    let projection = profile.project(&project)?;
    let replay = vela_protocol::reducer::verify_replay(&project);
    if !replay.ok {
        return Err(format!(
            "trust pinning requires exact replay: {}",
            replay.diffs.join(" | ")
        ));
    }

    let supplied = project
        .events
        .iter()
        .filter(|event| {
            repository_boundary_event_content_root(event)
                .is_ok_and(|root| root == supplied_first_boundary_root)
        })
        .collect::<Vec<_>>();
    let [supplied] = supplied.as_slice() else {
        return Err(format!(
            "supplied boundary root must identify exactly one repository event, found {}",
            supplied.len()
        ));
    };
    let supplied_payload = repository_boundary_payload_from_event_shape(supplied)?;
    let structurally_first = match supplied_payload.mode {
        FrontierRepositoryBoundaryMode::TemporalizeExisting => {
            supplied_payload.trust_mode == FrontierRepositoryTrustMode::Tofu
                && supplied_payload.previous_identity_event_root.is_none()
        }
        FrontierRepositoryBoundaryMode::UpdateDependencies => {
            let Some(parent) = supplied_payload.previous_identity_event_root.as_deref() else {
                return Err("first native boundary has no genesis parent".to_string());
            };
            supplied_payload.trust_mode == FrontierRepositoryTrustMode::Genesis
                && project.events.iter().any(|event| {
                    event.kind.as_str() == "frontier.created"
                        && repository_identity_event_content_root(event)
                            .is_ok_and(|root| root == parent)
                })
        }
    };
    if !structurally_first {
        return Err("supplied root is not the chain's first administrator boundary".to_string());
    }

    let head = project
        .events
        .iter()
        .filter(|event| {
            repository_identity_event_content_root(event)
                .is_ok_and(|root| root == projection.identity_event_root)
        })
        .collect::<Vec<_>>();
    let [head] = head.as_slice() else {
        return Err(format!(
            "effective repository identity head must identify exactly one event, found {}",
            head.len()
        ));
    };
    verify_repository_boundary_context_with_trust_anchor(
        &project,
        &frontier,
        head,
        Some(&RepositoryTrustAnchor {
            boundary_content_root: supplied_first_boundary_root.to_string(),
            administrator_public_key: supplied_payload.administrator_public_key.clone(),
        }),
    )?;

    let anchor = RepositoryTrustAnchorV1 {
        schema: REPOSITORY_TRUST_ANCHOR_SCHEMA_V1.to_string(),
        frontier_id: projection.frontier_id.clone(),
        identity_root: projection.identity_root.clone(),
        boundary_content_root: supplied_first_boundary_root.to_string(),
        administrator_actor_id: supplied_payload.administrator_actor_id.clone(),
        administrator_public_key: supplied_payload.administrator_public_key.clone(),
    };
    let anchor_root = anchor.root()?;
    let event_count =
        u64::try_from(project.events.len()).map_err(|_| "event count exceeds u64".to_string())?;
    let mut plan = RepositoryTrustPinPlan {
        schema: TRUST_PIN_PLAN_SCHEMA.to_string(),
        ok: true,
        command: "frontier.trust.pin".to_string(),
        frontier: frontier.display().to_string(),
        frontier_id: projection.frontier_id,
        identity_root: projection.identity_root,
        identity_head_root: projection.identity_event_root,
        supplied_first_boundary_root: supplied_first_boundary_root.to_string(),
        administrator_actor_id: supplied_payload.administrator_actor_id,
        administrator_public_key: supplied_payload.administrator_public_key,
        boundary_mode: match supplied_payload.mode {
            FrontierRepositoryBoundaryMode::TemporalizeExisting => "temporalize_existing",
            FrontierRepositoryBoundaryMode::UpdateDependencies => "update_dependencies",
        }
        .to_string(),
        boundary_trust_mode: match supplied_payload.trust_mode {
            FrontierRepositoryTrustMode::Tofu => "tofu",
            FrontierRepositoryTrustMode::Genesis => "genesis",
            FrontierRepositoryTrustMode::PreviousBoundary => "previous_boundary",
        }
        .to_string(),
        git_commit: git(&frontier, &["rev-parse", "HEAD^{commit}"])?,
        git_tree: git(&frontier, &["rev-parse", "HEAD^{tree}"])?,
        event_log_root: format!("sha256:{}", event_log_hash(&project.events)),
        event_count,
        trust_anchor: anchor,
        trust_anchor_root: anchor_root,
        observed_at: observed_at.to_string(),
        writes: vec![format!(
            "<os-account-home>/.vela/trust/frontiers/{}.json",
            project.frontier_id()
        )],
        plan_root: String::new(),
    };
    plan.plan_root = compute_plan_root(&plan)?;
    verify_plan(&plan)?;
    Ok(plan)
}

fn install_confirmed_trust_pin(
    frontier: &Path,
    supplied_first_boundary_root: &str,
    confirm_root: &str,
    confirm_at: &str,
    user_home: &Path,
) -> Result<RepositoryTrustPinPlan, String> {
    let plan = prepare_trust_pin(frontier, supplied_first_boundary_root, confirm_at)?;
    verify_plan(&plan)?;
    if plan.plan_root != confirm_root {
        return Err(format!(
            "trust-pin confirmation root mismatch: supplied {confirm_root}, current {}",
            plan.plan_root
        ));
    }
    let installed = install_repository_trust_anchor_from_home(user_home, &plan.trust_anchor)?;
    if installed.root != plan.trust_anchor_root || installed.anchor != plan.trust_anchor {
        return Err("installed trust anchor does not match the confirmed plan".to_string());
    }
    Ok(plan)
}

pub(crate) fn cmd_frontier_trust_pin(
    frontier: &Path,
    boundary_root: &str,
    confirm_root: Option<&str>,
    confirm_at: Option<&str>,
    json: bool,
) {
    crate::ui::set_mode("frontier.trust.pin", json);
    match (confirm_root, confirm_at) {
        (None, None) => {
            let observed_at = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
            let plan = prepare_trust_pin(frontier, boundary_root, &observed_at)
                .unwrap_or_else(|error| super::fail_return(&error));
            if json {
                super::print_json(&plan);
            } else {
                println!("frontier trust pin · key-free preview");
                println!("  frontier: {}", plan.frontier_id);
                println!("  first boundary: {}", plan.supplied_first_boundary_root);
                println!("  administrator: {}", plan.administrator_actor_id);
                println!("  public key: {}", plan.administrator_public_key);
                println!("  plan root: {}", plan.plan_root);
                println!("  confirm at: {}", plan.observed_at);
                println!("  writes now: none");
            }
        }
        (Some(confirm_root), Some(confirm_at)) => {
            crate::decision_plan::validate_scripted_confirmation_time(confirm_at).unwrap_or_else(
                |error| super::fail_return(&format!("{}: {}", error.code, error.message)),
            );
            let user_home = crate::frontier_txn::operating_system_account_home()
                .unwrap_or_else(|error| super::fail_return(&error.to_string()));
            let plan = install_confirmed_trust_pin(
                frontier,
                boundary_root,
                confirm_root,
                confirm_at,
                &user_home,
            )
            .unwrap_or_else(|error| super::fail_return(&error));
            if json {
                super::print_json(&serde_json::json!({
                    "ok": true,
                    "command": "frontier.trust.pin",
                    "plan": plan,
                    "installed": true,
                }));
            } else {
                println!("pinned {}", plan.supplied_first_boundary_root);
                println!("  trust anchor: {}", plan.trust_anchor_root);
            }
        }
        _ => super::fail_return::<()>(
            "--confirm-root and --confirm-at must be supplied together; omit both for preview",
        ),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ed25519_dalek::SigningKey;
    use vela_protocol::frontier_repo::{ProfileV1InitOptions, initialize_profile_v1_minimal};
    use vela_protocol::frontier_repository::{
        FRONTIER_REPOSITORY_BOUNDARY_SCHEMA, FrontierRepositoryBoundaryPayloadV1, GitObjectFormat,
        exact_dependency_root, new_repository_boundary_event,
    };
    use vela_protocol::sign::{ActorRecord, pubkey_hex, sign_event};

    const OBSERVED_AT: &str = "2026-07-22T12:00:00Z";

    fn run(path: &Path, args: &[&str]) -> String {
        let output = std::process::Command::new("git")
            .arg("-C")
            .arg(path)
            .args(args)
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "git {}: {}",
            args.join(" "),
            String::from_utf8_lossy(&output.stderr)
        );
        String::from_utf8_lossy(&output.stdout).trim().to_string()
    }

    fn fixture() -> (tempfile::TempDir, String) {
        let directory = tempfile::tempdir().unwrap();
        initialize_profile_v1_minimal(
            directory.path(),
            ProfileV1InitOptions {
                name: "Pinned fixture",
                scope: "Can one exact first boundary be pinned?",
                initialize_git: true,
            },
        )
        .unwrap();
        run(directory.path(), &["config", "user.name", "Vela Test"]);
        run(
            directory.path(),
            &["config", "user.email", "vela@example.invalid"],
        );
        let mut project = vela_protocol::repo::load_from_path(directory.path()).unwrap();
        let key = SigningKey::from_bytes(&[53; 32]);
        let actor = ActorRecord {
            id: "reviewer:pin".to_string(),
            public_key: pubkey_hex(&key),
            algorithm: "ed25519".to_string(),
            created_at: "2026-07-22T00:00:00Z".to_string(),
            tier: None,
            orcid: None,
            access_clearance: None,
            revoked_at: None,
            revoked_reason: None,
        };
        project.actors = vec![actor.clone()];
        vela_protocol::repo::save_to_path(directory.path(), &project).unwrap();
        run(directory.path(), &["add", "."]);
        run(
            directory.path(),
            &["commit", "-qm", "native boundary anchor"],
        );

        let profile = match read_repository_profile(directory.path()).unwrap().unwrap() {
            FrontierProfileFile::V1(profile) => profile,
            _ => panic!("Profile v1"),
        };
        let authority = EffectiveFrontierAuthorityV1::from_events(&project.events).unwrap();
        let projection = profile.project(&project).unwrap();
        let anchor = vela_edge::frontier_repository::derive_repository_anchor_facts(
            directory.path(),
            &run(directory.path(), &["rev-parse", "HEAD"]),
        )
        .unwrap();
        let dependencies = Vec::new();
        let payload = FrontierRepositoryBoundaryPayloadV1 {
            schema: FRONTIER_REPOSITORY_BOUNDARY_SCHEMA.to_string(),
            mode: FrontierRepositoryBoundaryMode::UpdateDependencies,
            frontier_id: authority.frontier_id,
            identity_root: projection.identity_root,
            observed_profile_root: profile.profile_root().unwrap(),
            dependency_root: exact_dependency_root(&dependencies).unwrap(),
            dependencies,
            previous_identity_event_root: Some(projection.identity_event_root),
            legacy_identity_preimage_root: None,
            administrator_actor_id: actor.id,
            administrator_public_key: actor.public_key,
            administrator_algorithm: actor.algorithm,
            trust_mode: FrontierRepositoryTrustMode::Genesis,
            git_object_format: GitObjectFormat::Sha1,
            anchor_git_commit: anchor.git_commit,
            anchor_git_tree: anchor.git_tree,
            anchor_event_log_root: anchor.event_log_root,
            anchor_event_count: anchor.event_count,
            anchor_snapshot_root: anchor.snapshot_root,
            anchor_snapshot_schema: anchor.snapshot_schema,
            anchor_proposal_root: anchor.proposal_root,
            anchor_actor_registry_root: anchor.actor_registry_root,
            anchor_artifact_registry_root: anchor.artifact_registry_root,
            anchor_canonical_store_root: anchor.canonical_store_root,
        };
        let mut boundary =
            new_repository_boundary_event(payload, "Bind first administrator", OBSERVED_AT)
                .unwrap();
        boundary.signature = Some(sign_event(&boundary, &key).unwrap());
        let root = repository_boundary_event_content_root(&boundary).unwrap();
        project.events.push(boundary);
        vela_protocol::repo::save_to_path(directory.path(), &project).unwrap();
        run(directory.path(), &["add", "."]);
        run(directory.path(), &["commit", "-qm", "signed boundary"]);
        (directory, root)
    }

    #[test]
    fn preview_is_write_free_and_confirmed_install_is_non_replacing() {
        let (directory, root) = fixture();
        let home = tempfile::tempdir().unwrap();
        let plan = prepare_trust_pin(directory.path(), &root, OBSERVED_AT).unwrap();
        assert!(plan.ok);
        assert_eq!(plan.command, "frontier.trust.pin");
        let wire = serde_json::to_value(&plan).unwrap();
        assert_eq!(wire["ok"], true);
        assert_eq!(wire["command"], "frontier.trust.pin");
        assert!(!home.path().join(".vela").exists());
        let installed = install_confirmed_trust_pin(
            directory.path(),
            &root,
            &plan.plan_root,
            OBSERVED_AT,
            home.path(),
        )
        .unwrap();
        assert_eq!(installed.plan_root, plan.plan_root);
        assert!(
            home.path()
                .join(".vela/trust/frontiers")
                .join(format!("{}.json", plan.frontier_id))
                .is_file()
        );

        let wrong = format!("sha256:{}", "f".repeat(64));
        assert!(prepare_trust_pin(directory.path(), &wrong, OBSERVED_AT).is_err());
        assert!(
            install_confirmed_trust_pin(directory.path(), &root, &wrong, OBSERVED_AT, home.path(),)
                .is_err()
        );
    }

    #[test]
    fn hostile_git_environment_and_replacements_cannot_redirect_or_write_trust() {
        const CHILD_ENV: &str = "VELA_TEST_HOSTILE_TRUST_GIT_CHILD";
        const SOURCE_ENV: &str = "VELA_TEST_HOSTILE_TRUST_SOURCE";
        const DECOY_ENV: &str = "VELA_TEST_HOSTILE_TRUST_DECOY";
        const BOUNDARY_ENV: &str = "VELA_TEST_HOSTILE_TRUST_BOUNDARY";
        const HOME_ENV: &str = "VELA_TEST_HOSTILE_TRUST_HOME";
        const SOURCE_HEAD_ENV: &str = "VELA_TEST_HOSTILE_TRUST_SOURCE_HEAD";
        const SOURCE_TREE_ENV: &str = "VELA_TEST_HOSTILE_TRUST_SOURCE_TREE";
        const DECOY_HEAD_ENV: &str = "VELA_TEST_HOSTILE_TRUST_DECOY_HEAD";
        const DECOY_TREE_ENV: &str = "VELA_TEST_HOSTILE_TRUST_DECOY_TREE";
        const TEST_NAME: &str = concat!(
            "cli::repository_trust::tests::",
            "hostile_git_environment_and_replacements_cannot_redirect_or_write_trust"
        );

        if std::env::var_os(CHILD_ENV).is_none() {
            // Git environment variables are process-global inputs. Exercise
            // the hostile case in an exact single-test child so parallel tests
            // cannot observe them.
            let (source, boundary_root) = fixture();
            let hostile = tempfile::tempdir().unwrap();
            let decoy = hostile.path().join("decoy");
            let clone = std::process::Command::new("git")
                .args([
                    "clone",
                    "--no-hardlinks",
                    source.path().to_str().unwrap(),
                    decoy.to_str().unwrap(),
                ])
                .output()
                .unwrap();
            assert!(
                clone.status.success(),
                "clone decoy: {}",
                String::from_utf8_lossy(&clone.stderr)
            );
            run(&decoy, &["config", "user.name", "Vela Test"]);
            run(&decoy, &["config", "user.email", "vela@example.invalid"]);
            std::fs::write(decoy.join("decoy-only.txt"), b"decoy repository\n").unwrap();
            run(&decoy, &["add", "decoy-only.txt"]);
            run(&decoy, &["commit", "-qm", "decoy-only commit"]);

            let source_head =
                crate::git_hardened::text(source.path(), &["rev-parse", "HEAD^{commit}"]).unwrap();
            let source_tree =
                crate::git_hardened::text(source.path(), &["rev-parse", "HEAD^{tree}"]).unwrap();
            let decoy_head =
                crate::git_hardened::text(&decoy, &["rev-parse", "HEAD^{commit}"]).unwrap();
            let decoy_tree =
                crate::git_hardened::text(&decoy, &["rev-parse", "HEAD^{tree}"]).unwrap();
            assert_ne!(source_head, decoy_head);
            assert_ne!(source_tree, decoy_tree);

            // Install a real replacement ref in the named source. Raw Git now
            // resolves the source commit through the decoy commit, while the
            // trust runner must retain the source's actual tree.
            run(
                source.path(),
                &["fetch", decoy.to_str().unwrap(), &decoy_head],
            );
            run(source.path(), &["replace", &source_head, &decoy_head]);
            assert_eq!(
                run(source.path(), &["rev-parse", "HEAD^{tree}"]),
                decoy_tree,
                "test setup did not activate the replacement ref"
            );
            assert_eq!(
                crate::git_hardened::text(source.path(), &["rev-parse", "HEAD^{tree}"]).unwrap(),
                source_tree,
                "hardened Git must ignore repository replacement refs"
            );

            let trust_home = hostile.path().join("trust-home");
            std::fs::create_dir(&trust_home).unwrap();
            let mut child = std::process::Command::new(std::env::current_exe().unwrap());
            child.args(["--exact", TEST_NAME, "--nocapture"]);
            for (name, _) in std::env::vars_os() {
                if name.to_str().is_some_and(|name| name.starts_with("GIT_")) {
                    child.env_remove(name);
                }
            }
            child
                .env(CHILD_ENV, "1")
                .env(SOURCE_ENV, source.path())
                .env(DECOY_ENV, &decoy)
                .env(BOUNDARY_ENV, &boundary_root)
                .env(HOME_ENV, &trust_home)
                .env(SOURCE_HEAD_ENV, &source_head)
                .env(SOURCE_TREE_ENV, &source_tree)
                .env(DECOY_HEAD_ENV, &decoy_head)
                .env(DECOY_TREE_ENV, &decoy_tree)
                .env("GIT_DIR", decoy.join(".git"))
                .env("GIT_WORK_TREE", &decoy)
                .env("GIT_COMMON_DIR", decoy.join(".git"))
                .env("GIT_INDEX_FILE", decoy.join(".git/index"))
                .env("GIT_OBJECT_DIRECTORY", decoy.join(".git/objects"))
                .env(
                    "GIT_ALTERNATE_OBJECT_DIRECTORIES",
                    source.path().join(".git/objects"),
                )
                .env("GIT_REPLACE_REF_BASE", "refs/replace/hostile")
                .env("GIT_CONFIG_COUNT", "2")
                .env("GIT_CONFIG_KEY_0", "core.bare")
                .env("GIT_CONFIG_VALUE_0", "true")
                .env("GIT_CONFIG_KEY_1", "core.worktree")
                .env("GIT_CONFIG_VALUE_1", &decoy);
            let output = child.output().unwrap();
            assert!(
                output.status.success(),
                "hostile trust child failed:\nstdout:\n{}\nstderr:\n{}",
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr)
            );
            return;
        }

        let source = std::path::PathBuf::from(std::env::var_os(SOURCE_ENV).unwrap());
        let decoy = std::path::PathBuf::from(std::env::var_os(DECOY_ENV).unwrap());
        let boundary_root = std::env::var(BOUNDARY_ENV).unwrap();
        let trust_home = std::path::PathBuf::from(std::env::var_os(HOME_ENV).unwrap());
        let source_head = std::env::var(SOURCE_HEAD_ENV).unwrap();
        let source_tree = std::env::var(SOURCE_TREE_ENV).unwrap();
        let decoy_head = std::env::var(DECOY_HEAD_ENV).unwrap();
        let decoy_tree = std::env::var(DECOY_TREE_ENV).unwrap();

        let plan = prepare_trust_pin(&source, &boundary_root, OBSERVED_AT).unwrap();
        assert_eq!(plan.git_commit, source_head);
        assert_eq!(plan.git_tree, source_tree);
        assert_ne!(plan.git_commit, decoy_head);
        assert_ne!(plan.git_tree, decoy_tree);
        assert!(
            !trust_home.join(".vela").exists(),
            "key-free preview wrote consumer trust state"
        );

        // The exact named source drifts after confirmation while the hostile
        // decoy remains clean. A redirected Git runner would rederive the old
        // plan and install the pin; the hardened runner must fail before write.
        std::fs::write(source.join("post-preview-drift.txt"), b"source drift\n").unwrap();
        let error = install_confirmed_trust_pin(
            &source,
            &boundary_root,
            &plan.plan_root,
            OBSERVED_AT,
            &trust_home,
        )
        .unwrap_err();
        assert!(
            error.contains("clean checkout"),
            "unexpected trust-pin failure: {error}"
        );
        assert!(
            !trust_home.join(".vela").exists(),
            "failed trust-pin confirmation wrote consumer trust state"
        );
        assert!(
            !decoy.join("post-preview-drift.txt").exists(),
            "trust operation wrote into the redirected decoy worktree"
        );
    }
}