harn-vm 0.10.16

Async bytecode virtual machine for the Harn programming language
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
use super::*;
use crate::trust_graph::{query_trust_records, TrustQueryFilters};
use std::fs;
use std::process::Command;

fn require_git() -> bool {
    Command::new("git")
        .arg("--version")
        .output()
        .map(|output| output.status.success())
        .unwrap_or(false)
}

fn git(cwd: &Path, args: &[&str]) {
    let mut command = Command::new("git");
    command.args(args).current_dir(cwd);
    // Mirror the production `exec_argv` env scrub so these tests
    // also pass when invoked from inside a git hook (which sets
    // `GIT_DIR=.git` for the hook process and its descendants —
    // without this, a child `git init` in a temp dir reuses the
    // outer repo's index and fails).
    for name in super::GIT_ENV_OVERRIDES {
        command.env_remove(name);
    }
    let output = command.output().expect("run git");
    assert!(
        output.status.success(),
        "git {:?} failed\nstdout:\n{}\nstderr:\n{}",
        args,
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

fn init_repo() -> tempfile::TempDir {
    let dir = tempfile::tempdir().expect("temp dir");
    git(dir.path(), &["init", "-b", "main"]);
    git(dir.path(), &["config", "user.email", "harn@example.test"]);
    git(dir.path(), &["config", "user.name", "Harn Test"]);
    fs::write(dir.path().join("README.md"), "initial\n").expect("write readme");
    git(dir.path(), &["add", "README.md"]);
    git(dir.path(), &["commit", "-m", "initial"]);
    dir
}

async fn run_on_local<T>(future: impl std::future::Future<Output = T>) -> T {
    tokio::task::LocalSet::new().run_until(future).await
}

#[tokio::test(flavor = "current_thread")]
async fn git_status_returns_receipt_and_trust_record() {
    if !require_git() {
        return;
    }
    crate::event_log::reset_active_event_log();
    crate::stdlib::reset_stdlib_state();
    let repo = init_repo();
    git(repo.path(), &["mv", "README.md", "renamed file.md"]);

    run_on_local(async {
        let receipt = run_git_command(
            None,
            GitCommand {
                operation: "git.status",
                action: "git.status",
                cwd: repo.path().to_path_buf(),
                argv: vec![
                    "git".to_string(),
                    "status".to_string(),
                    "--porcelain=v1".to_string(),
                    "-z".to_string(),
                    "--branch".to_string(),
                ],
                mutation: GitMutation::Read,
                affected_paths: Vec::new(),
                data_parser: GitDataParser::Status,
            },
        )
        .await
        .expect("git status receipt");
        let json = crate::llm::vm_value_to_json(&receipt);
        assert_eq!(json["schema"], "harn-stdlib-git-receipt-v1");
        assert_eq!(json["operation"], "git.status");
        assert_eq!(json["success"], true);
        assert_eq!(json["data"]["dirty"], true);
        assert_eq!(json["data"]["entries"][0]["path"], "renamed file.md");
        assert_eq!(json["data"]["entries"][0]["original_path"], "README.md");

        let log = active_event_log().expect("git receipt installed event log");
        let records = query_trust_records(
            &log,
            &TrustQueryFilters {
                action: Some("git.status".to_string()),
                ..TrustQueryFilters::default()
            },
        )
        .await
        .expect("query trust records");
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].outcome, TrustOutcome::Success);
        assert_eq!(
            records[0].metadata["receipt"]["operation"],
            JsonValue::String("git.status".to_string())
        );
    })
    .await;
}

#[cfg(target_os = "linux")]
#[tokio::test(flavor = "current_thread")]
async fn git_status_fails_closed_on_non_utf8_paths() {
    use std::ffi::OsString;
    use std::os::unix::ffi::OsStringExt;

    if !require_git() {
        return;
    }
    crate::event_log::reset_active_event_log();
    crate::stdlib::reset_stdlib_state();
    let repo = init_repo();
    let filename = OsString::from_vec(b"invalid-\xff.txt".to_vec());
    fs::write(repo.path().join(&filename), "content\n").expect("write non-UTF-8 path");
    let mut add = Command::new("git");
    add.arg("add")
        .arg("--")
        .arg(&filename)
        .current_dir(repo.path());
    for name in super::GIT_ENV_OVERRIDES {
        add.env_remove(name);
    }
    assert!(add.status().expect("git add non-UTF-8 path").success());

    run_on_local(async {
        let receipt = run_git_command(
            None,
            GitCommand {
                operation: "git.status",
                action: "git.status",
                cwd: repo.path().to_path_buf(),
                argv: vec![
                    "git".to_string(),
                    "status".to_string(),
                    "--porcelain=v1".to_string(),
                    "-z".to_string(),
                    "--branch".to_string(),
                ],
                mutation: GitMutation::Read,
                affected_paths: Vec::new(),
                data_parser: GitDataParser::Status,
            },
        )
        .await
        .expect("invalid output returns a failure receipt");
        let json = crate::llm::vm_value_to_json(&receipt);
        assert_eq!(json["success"], false);
        assert_eq!(json["status"], "failed");
        assert_eq!(json["exit_category"], "output_encoding");
        assert_eq!(json["encoding_error"], "non_utf8_stdout");
        assert_eq!(json["stdout"]["inline"], "");
        assert_eq!(json["data"]["entries"], json!([]));
        assert_eq!(json["data"]["dirty"], false);
        assert!(json["stderr"]["inline"]
            .as_str()
            .is_some_and(|value| value.contains("cannot preserve canonical identity")));
    })
    .await;
}

#[test]
fn structured_git_output_marks_invalid_utf8_as_a_typed_failure() {
    let command = GitCommand {
        operation: "git.status",
        action: "git.status",
        cwd: PathBuf::from("."),
        argv: vec!["git".to_string(), "status".to_string()],
        mutation: GitMutation::Read,
        affected_paths: Vec::new(),
        data_parser: GitDataParser::Status,
    };
    let mut result = json!({
        "success": true,
        "status": "completed",
        "exit_code": 0,
        "stdout": "?? invalid-\u{fffd}.txt\0",
        "stderr": "",
        "stdout_utf8_valid": false,
    });

    fail_closed_on_invalid_structured_output(&command, &mut result);

    assert_eq!(result["success"], false);
    assert_eq!(result["status"], "failed");
    assert_eq!(result["stdout"], "");
    assert_eq!(result["encoding_error"], "non_utf8_stdout");
    assert!(result["stderr"]
        .as_str()
        .is_some_and(|value| value.contains("cannot preserve canonical identity")));
}

#[test]
fn status_parser_preserves_unquoted_paths_and_rename_sources() {
    let parsed = parse_status(
        "## main...origin/main\0R  new name.txt\0old name.txt\0 M line\nbreak.txt\0?? quote\"file.txt\0",
    );
    assert_eq!(parsed["branch"], "main...origin/main");
    assert_eq!(parsed["dirty"], true);
    assert_eq!(parsed["entries"][0]["path"], "new name.txt");
    assert_eq!(parsed["entries"][0]["original_path"], "old name.txt");
    assert_eq!(parsed["entries"][1]["path"], "line\nbreak.txt");
    assert_eq!(parsed["entries"][1]["original_path"], JsonValue::Null);
    assert_eq!(parsed["entries"][2]["path"], "quote\"file.txt");
}

#[tokio::test(flavor = "current_thread")]
async fn git_subprocess_carries_noninteractive_prompt_guard() {
    // Every git subprocess Harn spawns must be non-interactive by
    // default so a credential / host-key prompt fails fast instead
    // of hanging a TTY-less runtime (`harn serve`, `@job`, CI). We
    // run a probe through the same `exec_argv` path the receipt git
    // builtins use and assert the guard env reached the child while
    // an inherited env var survived the merge (proving `env_mode:
    // "merge"`, not a clobbering replace). The inherited var uses a
    // unique name so it can't race other tests in a shared process.
    crate::stdlib::reset_stdlib_state();
    std::env::set_var("HARN_GUARD_PROBE_INHERIT", "kept");
    run_on_local(async {
        let result = exec_argv(&GitCommand {
            operation: "git.status",
            action: "git.status",
            cwd: std::env::temp_dir(),
            argv: vec![
                "sh".to_string(),
                "-c".to_string(),
                "printf '%s|%s' \"$GIT_TERMINAL_PROMPT\" \"$HARN_GUARD_PROBE_INHERIT\"".to_string(),
            ],
            mutation: GitMutation::Read,
            affected_paths: Vec::new(),
            data_parser: GitDataParser::None,
        })
        .await
        .expect("exec_argv probe");
        let json = crate::llm::vm_value_to_json(&result);
        let stdout = json["stdout"].as_str().unwrap_or_default();
        let (prompt, inherited) = stdout.split_once('|').unwrap_or_default();
        assert_eq!(
            prompt, "0",
            "git subprocess must inherit GIT_TERMINAL_PROMPT=0; stdout was {stdout:?}"
        );
        assert_eq!(
            inherited, "kept",
            "env_mode=merge must preserve inherited env; stdout was {stdout:?}"
        );
    })
    .await;
    std::env::remove_var("HARN_GUARD_PROBE_INHERIT");
}

#[tokio::test(flavor = "current_thread")]
async fn force_with_lease_detects_advanced_remote_before_push() {
    if !require_git() {
        return;
    }
    crate::event_log::reset_active_event_log();
    crate::stdlib::reset_stdlib_state();
    let remote = tempfile::tempdir().expect("remote");
    git(remote.path(), &["init", "--bare"]);
    let one = tempfile::tempdir().expect("clone one");
    let two = tempfile::tempdir().expect("clone two");
    git(
        one.path(),
        &["clone", remote.path().to_str().unwrap(), "repo"],
    );
    git(
        two.path(),
        &["clone", remote.path().to_str().unwrap(), "repo"],
    );
    let one_repo = one.path().join("repo");
    let two_repo = two.path().join("repo");
    git(&one_repo, &["config", "user.email", "harn@example.test"]);
    git(&one_repo, &["config", "user.name", "Harn Test"]);
    git(&two_repo, &["config", "user.email", "harn@example.test"]);
    git(&two_repo, &["config", "user.name", "Harn Test"]);
    fs::write(one_repo.join("file.txt"), "one\n").expect("write one");
    git(&one_repo, &["add", "file.txt"]);
    git(&one_repo, &["commit", "-m", "one"]);
    git(&one_repo, &["push", "origin", "HEAD:refs/heads/main"]);
    let expected_oid = String::from_utf8(
        Command::new("git")
            .args(["rev-parse", "refs/remotes/origin/main"])
            .current_dir(&one_repo)
            .output()
            .expect("rev-parse")
            .stdout,
    )
    .expect("utf8")
    .trim()
    .to_string();

    git(&two_repo, &["fetch", "origin", "main"]);
    git(&two_repo, &["checkout", "-B", "main", "origin/main"]);
    fs::write(two_repo.join("file.txt"), "two\n").expect("write two");
    git(&two_repo, &["commit", "-am", "two"]);
    git(&two_repo, &["push", "origin", "HEAD:refs/heads/main"]);

    fs::write(one_repo.join("file.txt"), "one advanced\n").expect("write one advanced");
    git(&one_repo, &["commit", "-am", "one advanced"]);

    run_on_local(async {
        let mismatch =
            verify_force_with_lease(&one_repo, "origin", "refs/heads/main", &expected_oid)
                .await
                .expect("lease check should complete");
        assert!(
            mismatch.is_some(),
            "expected advanced remote to produce a lease mismatch"
        );
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn absent_worktree_remove_is_idempotent_receipt() {
    if !require_git() {
        return;
    }
    crate::event_log::reset_active_event_log();
    crate::stdlib::reset_stdlib_state();
    let root = tempfile::tempdir().expect("root");
    let missing = root.path().join("missing-worktree");
    run_on_local(async {
        let receipt = planned_or_noop_receipt(
            "git.worktree.remove",
            "git.worktree.remove",
            GitMutation::Mutating,
            root.path().to_path_buf(),
            vec![
                "git".to_string(),
                "worktree".to_string(),
                "remove".to_string(),
                display_path(&missing),
            ],
            vec![display_path(&missing)],
            json!({"path": display_path(&missing), "removed": false, "idempotent": true}),
            "no_op",
        )
        .await
        .expect("receipt");
        let json = crate::llm::vm_value_to_json(&receipt);
        assert_eq!(json["status"], "no_op");
        assert_eq!(json["success"], true);
        assert_eq!(json["data"]["idempotent"], true);
    })
    .await;
}

#[test]
fn string_list_value_rejects_non_string_entries() {
    let err = string_list_value(&[VmValue::Int(1)], "git.diff", "paths")
        .expect_err("non-string path should be rejected");
    assert!(
        matches!(err, VmError::TypeError(message) if message.contains("paths entries must be strings"))
    );
}

/// Capture the diagnostics emitted while `body` runs by swapping the
/// thread-local event sinks for a [`CollectorSink`], returning only
/// the `stdlib.git` warnings.
fn capture_git_warnings(body: impl FnOnce()) -> Vec<crate::events::LogEvent> {
    use crate::events::{add_event_sink, clear_event_sinks, reset_event_sinks, CollectorSink};
    use std::rc::Rc;
    let sink = Rc::new(CollectorSink::new());
    clear_event_sinks();
    add_event_sink(sink.clone());
    body();
    reset_event_sinks();
    let warnings = sink
        .logs
        .borrow()
        .iter()
        .filter(|event| event.category == "stdlib.git")
        .cloned()
        .collect();
    warnings
}

#[test]
fn warn_git_failure_dedupes_by_operation_exit_and_stderr_line() {
    use crate::events::EventLevel;
    reset_git_state();
    let warnings = capture_git_warnings(|| {
        // First failure warns.
        warn_git_failure("git.diff", 128, "fatal: not a git repository: /x\ntrailing");
        // Same (op, exit, first stderr line) is suppressed even though
        // line 2 differs — the dedup key is the first line only.
        warn_git_failure("git.diff", 128, "fatal: not a git repository: /x\nother");
        // A different exit code is a distinct key and warns again.
        warn_git_failure("git.diff", 1, "error: bad revision");
    });
    reset_git_state();
    assert_eq!(warnings.len(), 2, "unexpected diagnostics: {warnings:?}");
    assert_eq!(warnings[0].level, EventLevel::Warn);
    assert_eq!(warnings[0].category, "stdlib.git");
    assert!(
        warnings[0]
            .message
            .contains("git.diff failed (exit 128): fatal: not a git repository: /x"),
        "message was: {}",
        warnings[0].message
    );
    assert!(
        warnings[0]
            .message
            .contains("receipt success=false; callers see empty data"),
        "message was: {}",
        warnings[0].message
    );
    assert!(warnings[1].message.contains("git.diff failed (exit 1)"));
}

#[test]
fn warn_git_failure_exempts_probe_operations() {
    reset_git_state();
    let warnings = capture_git_warnings(|| {
        warn_git_failure("git.repo.discover", 128, "fatal: not a git repository: /x");
    });
    reset_git_state();
    assert!(
        warnings.is_empty(),
        "probe operations must stay silent on failure: {warnings:?}"
    );
}

#[test]
fn warn_git_failure_truncates_long_stderr() {
    reset_git_state();
    let long = format!("fatal: {}", "x".repeat(500));
    let warnings = capture_git_warnings(|| {
        warn_git_failure("git.diff", 128, &long);
    });
    reset_git_state();
    assert_eq!(warnings.len(), 1);
    assert!(
        warnings[0].message.contains(''),
        "truncated excerpt should end with an ellipsis: {}",
        warnings[0].message
    );
    // Excerpt is capped at FAILURE_STDERR_MAX_CHARS characters; the
    // surrounding wrapper text adds a bounded, fixed amount.
    assert!(
        warnings[0].message.chars().count() < FAILURE_STDERR_MAX_CHARS + 120,
        "message unexpectedly long: {}",
        warnings[0].message
    );
}

#[tokio::test(flavor = "current_thread")]
async fn failed_git_command_emits_single_diagnostic_end_to_end() {
    if !require_git() {
        return;
    }
    crate::event_log::reset_active_event_log();
    crate::stdlib::reset_stdlib_state();
    // A directory that is definitively not a git repo.
    let not_repo = tempfile::tempdir().expect("temp dir");
    let warnings = run_on_local(async {
        let sink = std::rc::Rc::new(crate::events::CollectorSink::new());
        crate::events::clear_event_sinks();
        crate::events::add_event_sink(sink.clone());
        // Two identical failing calls; the second must be deduped.
        for _ in 0..2 {
            let receipt = run_git_command(
                None,
                GitCommand {
                    operation: "git.diff",
                    action: "git.diff",
                    cwd: not_repo.path().to_path_buf(),
                    argv: vec!["git".to_string(), "diff".to_string(), "HEAD".to_string()],
                    mutation: GitMutation::Read,
                    affected_paths: Vec::new(),
                    data_parser: GitDataParser::Diff,
                },
            )
            .await
            .expect("git.diff returns a receipt, never throws");
            let json = crate::llm::vm_value_to_json(&receipt);
            assert_eq!(json["success"], false);
            // Data stays the empty diff the caller would silently consume.
            assert_eq!(json["data"]["diff"], "");
        }
        crate::events::reset_event_sinks();
        let collected = sink
            .logs
            .borrow()
            .iter()
            .filter(|event| event.category == "stdlib.git")
            .cloned()
            .collect::<Vec<_>>();
        collected
    })
    .await;
    assert_eq!(
        warnings.len(),
        1,
        "two identical failures should warn exactly once: {warnings:?}"
    );
    // The exact exit code and stderr wording vary across git versions
    // (128 "fatal: not a git repository" vs 129 usage-error phrasing),
    // so assert the shape, not the specific code.
    assert!(
        warnings[0].message.contains("git.diff failed (exit "),
        "message was: {}",
        warnings[0].message
    );
    assert!(
        warnings[0]
            .message
            .to_lowercase()
            .contains("not a git repository"),
        "stderr first line should be surfaced: {}",
        warnings[0].message
    );
}

#[tokio::test(flavor = "current_thread")]
async fn discover_probe_failure_stays_silent_end_to_end() {
    if !require_git() {
        return;
    }
    crate::event_log::reset_active_event_log();
    crate::stdlib::reset_stdlib_state();
    let not_repo = tempfile::tempdir().expect("temp dir");
    let warnings = run_on_local(async {
        let sink = std::rc::Rc::new(crate::events::CollectorSink::new());
        crate::events::clear_event_sinks();
        crate::events::add_event_sink(sink.clone());
        let receipt = run_git_command(
            None,
            GitCommand {
                operation: "git.repo.discover",
                action: "git.repo.discover",
                cwd: not_repo.path().to_path_buf(),
                argv: vec![
                    "git".to_string(),
                    "rev-parse".to_string(),
                    "--show-toplevel".to_string(),
                ],
                mutation: GitMutation::Read,
                affected_paths: Vec::new(),
                data_parser: GitDataParser::Discover {
                    input: display_path(not_repo.path()),
                },
            },
        )
        .await
        .expect("git.repo.discover returns a receipt");
        let json = crate::llm::vm_value_to_json(&receipt);
        assert_eq!(json["success"], false);
        crate::events::reset_event_sinks();
        let collected = sink
            .logs
            .borrow()
            .iter()
            .filter(|event| event.category == "stdlib.git")
            .cloned()
            .collect::<Vec<_>>();
        collected
    })
    .await;
    assert!(
        warnings.is_empty(),
        "git.repo.discover is a probe and must stay silent: {warnings:?}"
    );
}