harn-cli 0.10.122

CLI for the Harn programming language — run, test, REPL, format, and lint
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
use super::*;
use crate::package::test_support::{test_vm, write_trigger_project};

fn write_local_trigger_project(
    root: &std::path::Path,
    handler: &str,
    module_source: &str,
) -> std::path::PathBuf {
    let manifest = format!(
        r#"
[package]
name = "workspace"

[exports]
handlers = "lib.harn"

[[triggers]]
id = "github-new-issue"
kind = "webhook"
provider = "webhook"
tier = "suggest"
match = {{ events = ["issues.opened"] }}
handler = "{handler}"
secrets = {{ signing_secret = "webhook/signing-secret" }}
"#,
    );
    write_trigger_project(root, &manifest, Some(module_source))
}

fn write_local_hook_project(
    root: &std::path::Path,
    handler: &str,
    module_source: &str,
) -> std::path::PathBuf {
    let manifest = format!(
        r#"
[package]
name = "workspace"

[exports]
handlers = "lib.harn"

[[hooks]]
event = "PostToolUse"
pattern = "*"
handler = "{handler}"
"#,
    );
    write_trigger_project(root, &manifest, Some(module_source))
}

fn write_local_predicate_project(
    root: &std::path::Path,
    module_source: &str,
) -> std::path::PathBuf {
    let manifest = r#"
[package]
name = "workspace"

[exports]
handlers = "lib.harn"

[[triggers]]
id = "github-new-issue"
kind = "webhook"
provider = "webhook"
tier = "suggest"
match = { events = ["issues.opened"] }
when = "handlers::should_handle"
handler = "handlers::on_new_issue"
secrets = { signing_secret = "webhook/signing-secret" }
"#;
    write_trigger_project(root, manifest, Some(module_source))
}

fn write_cross_trigger_predicate_project(
    root: &std::path::Path,
    module_source: &str,
) -> std::path::PathBuf {
    let manifest = r#"
[package]
name = "workspace"

[exports]
handlers = "lib.harn"

[[triggers]]
id = "first-local-handler"
kind = "webhook"
provider = "webhook"
tier = "suggest"
match = { events = ["issues.opened"] }
handler = "handlers::on_new_issue"
secrets = { signing_secret = "webhook/signing-secret" }

[[triggers]]
id = "second-invalid-predicate"
kind = "webhook"
provider = "webhook"
tier = "suggest"
match = { events = ["issues.edited"] }
when = "handlers::should_handle"
handler = "worker://issues"
secrets = { signing_secret = "webhook/signing-secret" }
"#;
    write_trigger_project(root, manifest, Some(module_source))
}

#[tokio::test(flavor = "current_thread")]
async fn lazy_manifest_trigger_collection_defers_handler_module_initialization() {
    let tmp = tempfile::tempdir().unwrap();
    let harn_file = write_local_trigger_project(
        tmp.path(),
        "handlers::on_new_issue",
        r#"
import "std/triggers"

let must_not_initialize = 1 / 0

pub fn on_new_issue(harness: Harness, event: TriggerEvent) -> string {
  return event.kind
}
"#,
    );
    let extensions = load_runtime_extensions(&harn_file);
    assert!(extensions.triggers[0].execution_guard.is_none());
    let mut vm = test_vm();

    let collected = collect_manifest_triggers(&mut vm, &extensions)
        .await
        .expect("default collection must not initialize the handler module");
    let CollectedTriggerHandler::Local { callable, .. } = &collected[0].handler else {
        panic!("expected local handler");
    };
    assert!(matches!(callable, harn_vm::VmCallable::Lazy(_)));

    assert!(
        collect_manifest_triggers_with_initialization(
            &mut vm,
            &extensions,
            ManifestHandlerInitialization::Eager,
        )
        .await
        .is_err(),
        "explicit eager collection must initialize and validate the handler module"
    );
}

#[tokio::test(flavor = "current_thread")]
async fn eager_and_lazy_manifest_trigger_collection_reject_missing_and_private_handlers() {
    for (case, module_source) in [
        (
            "missing",
            "let must_not_initialize = 1 / 0\npub fn other(event) { return event.kind }",
        ),
        (
            "private",
            "let must_not_initialize = 1 / 0\nfn on_new_issue(event) { return event.kind }",
        ),
    ] {
        let tmp = tempfile::tempdir().unwrap();
        let harn_file =
            write_local_trigger_project(tmp.path(), "handlers::on_new_issue", module_source);
        let extensions = load_runtime_extensions(&harn_file);

        for initialization in [
            ManifestHandlerInitialization::Eager,
            ManifestHandlerInitialization::OnDispatch,
        ] {
            let result = collect_manifest_triggers_with_initialization(
                &mut test_vm(),
                &extensions,
                initialization,
            )
            .await;
            assert!(
                result.is_err(),
                "{case} trigger handler unexpectedly collected in {initialization:?} mode"
            );
            assert!(
                result
                    .unwrap_err()
                    .to_string()
                    .contains("handler 'handlers::on_new_issue' is not exported"),
                "{case} trigger handler must fail the export boundary in {initialization:?} mode"
            );
        }
    }
}

#[tokio::test(flavor = "current_thread")]
async fn eager_and_lazy_manifest_trigger_collection_validate_predicates_before_initialization() {
    let tmp = tempfile::tempdir().unwrap();
    let harn_file = write_local_predicate_project(
        tmp.path(),
        r#"
import "std/triggers"

let must_not_initialize = 1 / 0

pub fn on_new_issue(harness: Harness, event: TriggerEvent) -> string {
  return event.kind
}

pub fn should_handle(event: TriggerEvent) -> string {
  return event.kind
}
"#,
    );
    let extensions = load_runtime_extensions(&harn_file);

    for initialization in [
        ManifestHandlerInitialization::Eager,
        ManifestHandlerInitialization::OnDispatch,
    ] {
        let error = collect_manifest_triggers_with_initialization(
            &mut test_vm(),
            &extensions,
            initialization,
        )
        .await
        .expect_err("invalid predicate must fail before module initialization");
        assert!(
            error
                .to_string()
                .contains("must have signature fn(TriggerEvent) -> bool or Result<bool, _>"),
            "predicate signature must win over {initialization:?} initialization: {error}"
        );
    }
}

#[tokio::test(flavor = "current_thread")]
async fn all_trigger_declarations_validate_before_any_eager_initialization() {
    let tmp = tempfile::tempdir().unwrap();
    let harn_file = write_cross_trigger_predicate_project(
        tmp.path(),
        r#"
import "std/triggers"

let must_not_initialize = 1 / 0

pub fn on_new_issue(harness: Harness, event: TriggerEvent) -> string {
  return event.kind
}

pub fn should_handle(event: TriggerEvent) -> string {
  return event.kind
}
"#,
    );
    let extensions = load_runtime_extensions(&harn_file);

    for initialization in [
        ManifestHandlerInitialization::Eager,
        ManifestHandlerInitialization::OnDispatch,
    ] {
        let error = collect_manifest_triggers_with_initialization(
            &mut test_vm(),
            &extensions,
            initialization,
        )
        .await
        .expect_err("the second trigger predicate must fail before the first module initializes");
        assert!(
            error
                .to_string()
                .contains("must have signature fn(TriggerEvent) -> bool or Result<bool, _>"),
            "whole-set predicate validation must win over {initialization:?} initialization: {error}"
        );
    }
}

#[tokio::test(flavor = "current_thread")]
async fn lazy_hook_installation_defers_missing_and_private_handlers() {
    for (case, module_source) in [
        ("missing", "pub fn other(ctx) { return ctx }"),
        ("private", "fn handle(ctx) { return ctx }"),
    ] {
        harn_vm::reset_thread_local_state();
        let tmp = tempfile::tempdir().unwrap();
        let harn_file = write_local_hook_project(tmp.path(), "handlers::handle", module_source);
        let extensions = load_runtime_extensions(&harn_file);
        assert_eq!(extensions.hooks.len(), 1, "{case} hook fixture must load");

        install_manifest_hooks_with_initialization(
            &mut test_vm(),
            &extensions,
            ManifestHandlerInitialization::OnDispatch,
        )
        .await
        .unwrap_or_else(|error| panic!("{case} unused handler initialized eagerly: {error}"));

        let error = install_manifest_hooks_with_initialization(
            &mut test_vm(),
            &extensions,
            ManifestHandlerInitialization::Eager,
        )
        .await
        .expect_err("eager validation must reject the broken handler");
        assert!(
            error
                .to_string()
                .contains("hook handler 'handle' is not exported by module 'handlers'"),
            "{case} handler must be rejected in eager mode: {error}"
        );
        harn_vm::reset_thread_local_state();
    }
}

#[tokio::test(flavor = "current_thread")]
async fn lazy_broken_hook_fails_closed_when_its_event_dispatches() {
    harn_vm::reset_thread_local_state();
    let tmp = tempfile::tempdir().unwrap();
    let harn_file = write_local_hook_project(
        tmp.path(),
        "handlers::handle",
        "pub fn other(ctx) { return ctx }",
    );
    let extensions = load_runtime_extensions(&harn_file);
    let mut vm = test_vm();
    install_manifest_hooks_with_initialization(
        &mut vm,
        &extensions,
        ManifestHandlerInitialization::OnDispatch,
    )
    .await
    .expect("lazy hook registration");
    let ctx = harn_vm::AsyncBuiltinCtx::from_vm(vm);

    let error = harn_vm::orchestration::run_post_tool_hooks_with_ctx(
        Some(&ctx),
        "write_file",
        &serde_json::json!({"path": "guarded.txt"}),
        "ok",
    )
    .await
    .expect_err("a matching unresolved policy hook must stop dispatch");
    assert!(
        error
            .to_string()
            .contains("function 'handle' is not exported by module"),
        "unexpected dispatch error: {error}"
    );
    harn_vm::reset_thread_local_state();
}

#[tokio::test(flavor = "current_thread")]
async fn lazy_missing_hook_module_fails_only_when_its_event_dispatches() {
    harn_vm::reset_thread_local_state();
    let tmp = tempfile::tempdir().unwrap();
    let harn_file = write_local_hook_project(
        tmp.path(),
        "handlers::handle",
        "pub fn handle(ctx) { return ctx }",
    );
    std::fs::remove_file(tmp.path().join("lib.harn")).expect("remove hook module");
    let extensions = load_runtime_extensions(&harn_file);
    let eager_error = install_manifest_hooks_with_initialization(
        &mut test_vm(),
        &extensions,
        ManifestHandlerInitialization::Eager,
    )
    .await
    .expect_err("eager validation must reject the missing hook module");
    assert!(eager_error.to_string().contains("does not exist"));
    let mut vm = test_vm();
    install_manifest_hooks_with_initialization(
        &mut vm,
        &extensions,
        ManifestHandlerInitialization::OnDispatch,
    )
    .await
    .expect("an unused missing hook module must stay deferred");

    let ctx = harn_vm::AsyncBuiltinCtx::from_vm(vm);
    let dispatch_error = harn_vm::orchestration::run_post_tool_hooks_with_ctx(
        Some(&ctx),
        "write_file",
        &serde_json::json!({"path": "guarded.txt"}),
        "ok",
    )
    .await
    .expect_err("a matching missing policy hook must stop dispatch");
    assert!(
        dispatch_error.to_string().contains("lib.harn"),
        "unexpected dispatch error: {dispatch_error}"
    );
    harn_vm::reset_thread_local_state();
}

#[tokio::test(flavor = "current_thread")]
async fn lazy_hook_materializes_a_reachable_dependency_only_when_it_dispatches() {
    harn_vm::reset_thread_local_state();
    let tmp = tempfile::tempdir().unwrap();
    let harn_file = write_local_hook_project(
        tmp.path(),
        "handlers::handle",
        r#"
import "missing"

pub fn handle(_harness: Harness, _payload: dict) { return nil }
"#,
    );
    let manifest = std::fs::read_to_string(tmp.path().join(MANIFEST)).expect("read manifest");
    std::fs::write(
        tmp.path().join(MANIFEST),
        format!(
            "{manifest}\n[dependencies]\nmissing = {{ git = \"https://example.invalid/missing.git\" }}\n"
        ),
    )
    .expect("add unreachable dependency");
    let extensions = try_load_root_runtime_extensions(&harn_file)
        .expect("root hook registration must not prepare dependencies");
    let mut vm = test_vm();
    install_manifest_hooks_with_initialization(
        &mut vm,
        &extensions,
        ManifestHandlerInitialization::OnDispatch,
    )
    .await
    .expect("lazy hook registration");
    assert!(!tmp.path().join(LOCK_FILE).exists());

    let ctx = harn_vm::AsyncBuiltinCtx::from_vm(vm);
    let error = harn_vm::orchestration::run_post_tool_hooks_with_ctx(
        Some(&ctx),
        "write_file",
        &serde_json::json!({"path": "guarded.txt"}),
        "ok",
    )
    .await
    .expect_err("matching hook must reach dependency preparation and fail closed");
    assert!(
        error.to_string().contains("harn.lock"),
        "dispatch must report the missing dependency lock: {error}"
    );
    harn_vm::reset_thread_local_state();
}

#[tokio::test(flavor = "current_thread")]
async fn lazy_hook_resolves_a_path_dependency_from_a_fresh_generation() {
    harn_vm::reset_thread_local_state();
    let tmp = tempfile::tempdir().unwrap();
    let harn_file = write_local_hook_project(
        tmp.path(),
        "handlers::handle",
        r#"
import { policy_value } from "fixture_dep/policy"

pub fn handle(_harness: Harness, _payload: dict) {
  if policy_value() != 42 { return Err("dependency returned the wrong value") }
  return nil
}
"#,
    );
    let manifest = std::fs::read_to_string(tmp.path().join(MANIFEST)).expect("read manifest");
    std::fs::write(
        tmp.path().join(MANIFEST),
        format!(
            "{manifest}\n[dependencies]\nfixture_dep = {{ path = \"./vendor/fixture_dep\" }}\n"
        ),
    )
    .expect("add path dependency");
    let dependency = tmp.path().join("vendor/fixture_dep");
    std::fs::create_dir_all(&dependency).expect("create path dependency");
    std::fs::write(
        dependency.join(MANIFEST),
        "[package]\nname = \"fixture_dep\"\n",
    )
    .expect("write dependency manifest");
    std::fs::write(
        dependency.join("policy.harn"),
        "pub fn policy_value() -> int { return 42 }\n",
    )
    .expect("write dependency module");
    let cache = tempfile::tempdir().expect("package cache");
    let workspace = PackageWorkspace::for_test(tmp.path(), cache.path());
    install_packages_in(&workspace, false, None, false)
        .expect("create lock and initial generation");
    std::fs::remove_dir_all(tmp.path().join(".harn")).expect("remove initial generation");

    let extensions = try_load_root_runtime_extensions(&harn_file)
        .expect("root hook registration must leave the generation absent");
    let mut vm = test_vm();
    install_manifest_hooks_with_initialization(
        &mut vm,
        &extensions,
        ManifestHandlerInitialization::OnDispatch,
    )
    .await
    .expect("lazy hook registration");
    assert!(!tmp.path().join(".harn/package-current.toml").exists());

    let ctx = harn_vm::AsyncBuiltinCtx::from_vm(vm);
    harn_vm::orchestration::run_post_tool_hooks_with_ctx(
        Some(&ctx),
        "write_file",
        &serde_json::json!({"path": "guarded.txt"}),
        "ok",
    )
    .await
    .expect("matching hook must materialize and execute its path dependency");
    assert!(tmp.path().join(".harn/package-current.toml").is_file());
    harn_vm::reset_thread_local_state();
}