harn-cli 0.10.154

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
506
507
508
509
510
511
use super::*;

fn diagnostics(source: &str) -> Vec<PreflightDiagnostic> {
    let file = unique_temp_dir("harn-check-llm-composition").join("main.harn");
    let program = parse_program(source);
    collect_preflight_diagnostics(&file, source, &program, &CheckConfig::default())
        .into_iter()
        .filter(|diagnostic| diagnostic.code.as_str() == "HARN-LLM-006")
        .collect()
}

#[test]
fn direct_tool_preflight_requires_known_nonempty_tools() {
    for tools in ["nil", "[]", "{tools: []}", "empty_tools"] {
        let source = format!(
            r#"
fn main(harness: Harness) {{
  const empty_tools = []
  harness.llm.call("hello", nil, {{
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "text",
    tools: {tools},
  }})
}}
"#
        );
        assert!(diagnostics(&source).is_empty(), "{source}");
    }
    for tools in ["[{name: \"echo\"}]", "{tools: [{name: \"echo\"}]}"] {
        for format in ["text", "auto"] {
            for search in ["", ", tool_search: false", ", tool_search: nil"] {
                let source = format!(
                    r#"
fn main(harness: Harness) {{
  harness.llm.call("hello", nil, {{
    provider: "openrouter", model: "deepseek/deepseek-v3.2",
    tool_format: "{format}", tools: {tools}{search},
  }})
}}
"#
                );
                let found = diagnostics(&source);
                assert_eq!(found.len(), 1, "{source}: {found:?}");
                assert!(found[0].message.contains("would drop its tools"));
            }
        }
    }
}

#[test]
fn harness_direct_calls_keep_direct_channel_policy() {
    let no_tools = r#"
fn main(harness: Harness) {
  harness.llm.call("hello", nil, {
    provider: "openrouter", model: "deepseek/deepseek-v3.2", tool_format: "native",
  })
}
"#;
    assert!(diagnostics(no_tools).is_empty());
    let unaudited_override = r#"
fn main(harness: Harness) {
  harness.llm.call("hello", nil, {
    provider: "openrouter", model: "deepseek/deepseek-v3.2", tool_format: "native",
    tools: [{name: "echo"}], tool_format_override_reason: "probe",
  })
}
"#;
    let found = diagnostics(unaudited_override);
    assert_eq!(found.len(), 1);
    assert!(found[0].message.contains("native_unreliable"));
}

#[test]
fn preflight_rejects_only_provably_unsafe_literal_llm_compositions() {
    let rejected = [
        r#"
fn main(harness: Harness) {
  agent_options({
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "native",
  })
}
"#,
        r#"
fn main(harness: Harness) {
  llm_call("hello", nil, {
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "native",
    tools: [{name: "echo"}],
  })
}
"#,
        r#"
fn main(harness: Harness) {
  llm_completion("prefix", nil, nil, {
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "native",
    tools: [{name: "echo"}],
  })
}
"#,
    ];
    for source in rejected {
        let diagnostics = diagnostics(source);
        assert_eq!(
            diagnostics.len(),
            1,
            "known-unsafe literal composition should fail: {source}"
        );
        assert!(
            diagnostics[0].message.contains("native_unreliable"),
            "diagnostic should preserve the capability registry's reason: {}",
            diagnostics[0].message
        );
    }

    let accepted = [
        // The catalog-recommended text channel is safe on this route.
        r#"
fn main(harness: Harness) {
  agent_options({
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "text",
  })
}
"#,
        // Agent probes retain the existing audited experiment seam.
        r#"
fn main(harness: Harness) {
  agent_options({
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "native",
    tool_format_override_reason: "measure provider behavior",
  })
}
"#,
        // Unknown custom routes remain open-world.
        r#"
fn main(harness: Harness) {
  agent_options({
    provider: "my-proxy",
    model: "custom-model",
    tool_format: "native",
  })
}
"#,
        // Dynamic configuration remains under the runtime gate.
        r#"
fn run(model, format) {
  agent_options({
    provider: "openrouter",
    model: model,
    tool_format: format,
  })
}
"#,
        r#"
fn run(provider) {
  agent_options({
    provider: provider,
    model: "deepseek/deepseek-v3.2",
    tool_format: "native",
  })
}
"#,
        // A raw LLM call without tools has no tool channel to validate.
        r#"
fn main(harness: Harness) {
  llm_call("hello", nil, {
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "native",
  })
}
"#,
    ];
    for source in accepted {
        assert!(
            diagnostics(source).is_empty(),
            "legitimate composition should remain accepted: {source}"
        );
    }
}

#[test]
fn preflight_rejects_literal_portable_options_declared_unsupported() {
    let rejected = r#"
fn main(harness: Harness) {
  llm_call("hello", nil, {
    provider: "moonshot",
    model: "kimi-k3",
    temperature: 0.2,
    top_p: 0.9,
  })
}
"#;
    let found = diagnostics(rejected);
    assert_eq!(found.len(), 2, "one diagnostic per rejected intent");
    assert!(found.iter().any(|diagnostic| diagnostic
        .message
        .contains("option `temperature` is not supported")));
    assert!(found.iter().any(|diagnostic| diagnostic
        .message
        .contains("option `top_p` is not supported")));

    let open_world = r#"
fn main(harness: Harness) {
  llm_call("hello", nil, {
    provider: "my-proxy",
    model: "custom-model",
    temperature: 0.2,
  })
}
"#;
    assert!(diagnostics(open_world).is_empty());
}

#[test]
fn preflight_checks_static_cache_intent_and_defers_dynamic_values() {
    harn_vm::llm::capabilities::set_user_overrides_toml(
        r#"
[[provider.test-provider]]
model_match = "no-cache"
prompt_caching = false

[[provider.test-provider]]
model_match = "cache-with-ttl"
prompt_caching = true
prompt_cache_ttls = ["5m"]

[[provider.test-provider]]
model_match = "no-temperature"
temperature_supported = false
"#,
    )
    .expect("capability overlay");

    let source = r#"
fn main(harness: Harness) {
  let dynamic_temperature = 0.2
  llm_call("cache", nil, {
    provider: "test-provider",
    model: "no-cache",
    cache: true,
  })
  llm_call("ttl", nil, {
    provider: "test-provider",
    model: "cache-with-ttl",
    prompt_cache_ttl: "1h",
  })
  llm_call("deferred", nil, {
    provider: "test-provider",
    model: "no-temperature",
    temperature: dynamic_temperature,
    cache: false,
  })
}
"#;
    let found = diagnostics(source);
    assert_eq!(found.len(), 2, "only provable capability gaps diagnose");
    assert!(found
        .iter()
        .any(|diagnostic| diagnostic.message.contains("option `cache`")));
    assert!(found.iter().any(|diagnostic| diagnostic
        .message
        .contains("option `prompt_cache_ttl` value `1h`")));

    harn_vm::llm::capabilities::clear_user_overrides();
}

#[test]
fn check_report_exposes_unsafe_llm_composition_code() {
    let dir = unique_temp_dir("harn-check-llm-composition-report");
    std::fs::create_dir_all(&dir).unwrap();
    let file = dir.join("main.harn");
    std::fs::write(
        &file,
        r#"
fn main(harness: Harness) {
  agent_options({
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "native",
  })
}
"#,
    )
    .unwrap();
    let files = vec![file.clone()];
    let module_graph = build_module_graph(&files);
    let cross_file_imports = collect_cross_file_imports(&module_graph);
    let mut analysis = harn_parser::analysis::AnalysisDatabase::new();
    let report = check_file_report(
        &mut analysis,
        &file,
        &CheckConfig::default(),
        &cross_file_imports,
        &module_graph,
        true,
    );

    assert!(report.outcome().has_error);
    assert!(
        report
            .diagnostics
            .iter()
            .any(|diagnostic| diagnostic.code.as_deref() == Some("HARN-LLM-006")),
        "public check report should expose HARN-LLM-006: {:?}",
        report.diagnostics
    );
    let _ = std::fs::remove_dir_all(&dir);
}

/// The `llm_*` globals this pass keys on were replaced by `harness.llm.*`, so
/// the check has to hold in three directions at once: it still fires on the
/// removed spelling, it now fires on the one the compiler recommends, and it
/// stays quiet on an unrelated receiver whose method name happens to collide.
/// Only asserting that some spelling errors would have passed before the fix.
#[test]
fn preflight_checks_llm_composition_through_the_capability_method() {
    const OPTIONS: &str = r#"{
    provider: "moonshot",
    model: "kimi-k3",
    temperature: 0.2,
  }"#;

    let legacy =
        format!("fn main(harness: Harness) {{\n  llm_call(\"hello\", nil, {OPTIONS})\n}}\n");
    let legacy_found = diagnostics(&legacy);
    assert_eq!(
        legacy_found.len(),
        1,
        "the removed ambient spelling must keep reporting: {legacy_found:?}"
    );
    assert!(legacy_found[0].message.contains("`llm_call`"));

    let capability = format!(
        "fn main(harness: Harness) {{\n  harness.llm.call(\"hello\", nil, {OPTIONS})\n}}\n"
    );
    let capability_found = diagnostics(&capability);
    assert_eq!(
        capability_found.len(),
        1,
        "the supported capability spelling must report too: {capability_found:?}"
    );
    assert!(
        capability_found[0].message.contains("`harness.llm.call`"),
        "the diagnostic should name the spelling at the call site: {}",
        capability_found[0].message
    );
    assert!(capability_found[0]
        .message
        .contains("option `temperature` is not supported"));

    // `call` is a real `harness.llm` method, so this is the exact collision the
    // receiver check exists to reject.
    let unrelated =
        format!("fn main(harness: Harness) {{\n  client.call(\"hello\", nil, {OPTIONS})\n}}\n");
    assert!(
        diagnostics(&unrelated).is_empty(),
        "a non-harness receiver must not be treated as a capability call"
    );
}

/// `completion` takes its options one slot later than `call`. Resolving the
/// receiver back to its registry name is what keeps that index correct without
/// a second table keyed by `(capability, method)`.
#[test]
fn preflight_uses_the_registry_name_to_find_the_options_slot() {
    let source = r#"
fn main(harness: Harness) {
  harness.llm.completion("prefix", nil, nil, {
    provider: "moonshot",
    model: "kimi-k3",
    temperature: 0.2,
  })
}
"#;
    let found = diagnostics(source);
    assert_eq!(
        found.len(),
        1,
        "completion options live at index 3: {found:?}"
    );
    assert!(found[0].message.contains("`harness.llm.completion`"));
}

/// The check-time error names the catalog rule that forbids the combination,
/// so the author can find the row instead of reverse-engineering the gate.
#[test]
fn preflight_names_the_catalog_rule_behind_a_forbidden_tool_format() {
    let found = diagnostics(
        r#"
fn main(harness: Harness) {
  harness.llm.call("hello", nil, {
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "native",
    tools: [{name: "echo"}],
  })
}
"#,
    );
    assert_eq!(found.len(), 1, "{found:?}");
    assert!(
        found[0]
            .message
            .contains("[[provider.openrouter]] model_match = ")
            && found[0].message.contains("capability_sources"),
        "the diagnostic should name the deciding catalog row: {}",
        found[0].message
    );
}

/// A direct call offering tools on a text-channel format would send no tools;
/// the agent loop's call and the native channel are not this case.
#[test]
fn preflight_rejects_direct_text_channel_tools() {
    let rejected = r#"
fn main(harness: Harness) {
  harness.llm.call("hello", nil, {
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "json",
    tools: [{name: "echo"}],
  })
}
"#;
    let found = diagnostics(rejected);
    assert_eq!(found.len(), 1, "{found:?}");
    assert!(
        found[0].message.contains("would drop its tools")
            && found[0].message.contains("agent_loop"),
        "{}",
        found[0].message
    );
    for accepted in [
        // Agent options feed the loop, which renders the contract.
        r#"
fn main(harness: Harness) {
  agent_options({
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "json",
    tools: [{name: "echo"}],
  })
}
"#,
        // An empty tool set sends nothing either way.
        r#"
fn main(harness: Harness) {
  harness.llm.call("hello", nil, {
    provider: "openrouter",
    model: "deepseek/deepseek-v3.2",
    tool_format: "json",
    tools: [],
  })
}
"#,
    ] {
        assert!(diagnostics(accepted).is_empty(), "{accepted}");
    }
}

/// Literal reasoning options run through the runtime's reasoning gate.
#[test]
fn preflight_rejects_literal_effort_on_a_route_without_it() {
    harn_vm::llm::capabilities::set_user_overrides_toml(
        r#"
[[provider.test-provider]]
model_match = "budget-only"
thinking_modes = ["enabled"]

[[provider.test-provider]]
model_match = "effort-capable"
thinking_modes = ["effort"]
reasoning_effort_supported = true
reasoning_effort_levels = ["low", "medium", "high"]
"#,
    )
    .expect("capability overlay");
    let found = diagnostics(
        r#"
fn main(harness: Harness) {
  harness.llm.call("hello", nil, {provider: "test-provider", model: "budget-only", effort: "high"})
  harness.llm.call("hello", nil, {provider: "test-provider", model: "budget-only", thinking: true})
  harness.llm.call("hello", nil, {provider: "test-provider", model: "effort-capable", effort: "high"})
}
"#,
    );
    harn_vm::llm::capabilities::clear_user_overrides();
    assert_eq!(
        found.len(),
        1,
        "only the effort-less route refuses effort: {found:?}"
    );
    assert!(
        found[0]
            .message
            .contains("option `effort` is not supported")
            && found[0].message.contains("model_match = \"budget-only\""),
        "{}",
        found[0].message
    );
}