harn-cli 0.10.166

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
use std::path::Path;

use harn_parser::{DiagnosticCode as Code, Node, SNode};

use super::super::harness_receiver::harness_method_receiver;
use super::{dict_literal_field, literal_string, PreflightDiagnostic};

#[derive(Clone, Copy, PartialEq, Eq)]
enum CallSurface {
    Direct,
    Agent,
}

/// Reject literal provider/model/option compositions that the runtime
/// capability registry cannot represent. Dynamic routes remain runtime-
/// checked; this pass only reports facts it can prove from the source.
///
/// The capability registry remains the sole policy owner. Preflight calls the
/// same `no_viable_tool_channel` / `validate_tool_format` functions used at
/// dispatch instead of maintaining a second table of forbidden combinations.
pub(super) fn scan_llm_capability_composition_preflight(
    file_path: &Path,
    source: &str,
    program: &[SNode],
    diagnostics: &mut Vec<PreflightDiagnostic>,
) {
    for node in program {
        scan_node(node, file_path, source, diagnostics);
    }
}

fn scan_node(
    node: &SNode,
    file_path: &Path,
    source: &str,
    diagnostics: &mut Vec<PreflightDiagnostic>,
) {
    if let Node::FunctionCall { name, args, .. } = &node.node {
        if let Some((surface, options)) = options_arg(name, args) {
            check_literal_composition(name, surface, options, file_path, source, diagnostics);
        }
    }
    // The `llm_*` entries in `options_arg` are removed ambient globals whose
    // supported spelling is `harness.llm.<method>`. Resolving the receiver back
    // to its registry name reuses that one table rather than growing a second
    // one keyed by `(capability, method)`; the `agent_*` entries are `std/agent`
    // module functions and keep matching as plain calls. Registry-backed
    // Harness entries use an internal `__cap_` primary name; stripping that
    // mechanical prefix recovers the existing options-table key without
    // duplicating the method-to-argument-position policy.
    if let Node::MethodCall {
        object,
        method,
        args,
    }
    | Node::OptionalMethodCall {
        object,
        method,
        args,
    } = &node.node
    {
        if let Some(receiver) = harness_method_receiver(object) {
            let builtin =
                harn_vm::stdlib::capability_method_manifest_entry(receiver.capability, method)
                    .map(|entry| entry.canonical_name)
                    .and_then(|name| name.strip_prefix("__cap_"));
            if let Some((surface, options)) = builtin.and_then(|name| options_arg(name, args)) {
                // Name the capability path rather than the registry name: it is
                // the spelling at the call site, and the registry name is the
                // removed global the author was told to stop writing.
                check_literal_composition(
                    &format!("harness.{}.{method}", receiver.field),
                    surface,
                    options,
                    file_path,
                    source,
                    diagnostics,
                );
            }
        }
    }
    for child in harn_parser::visit::immediate_children(node) {
        scan_node(child, file_path, source, diagnostics);
    }
}

/// Return the options argument for public calls that either dispatch a
/// tool-bearing model turn or construct canonical agent-loop options.
fn options_arg<'a>(name: &str, args: &'a [SNode]) -> Option<(CallSurface, &'a SNode)> {
    let (surface, index) = match name {
        "agent_options" | "agent_loop_options" | "agent_preset_options" => (CallSurface::Agent, 0),
        "agent_preset" | "agent_governed_preset" => (CallSurface::Agent, 1),
        "agent_loop" | "agent_stream_call" => (CallSurface::Agent, 2),
        "llm_call"
        | "llm_call_safe"
        | "llm_call_structured"
        | "llm_call_structured_safe"
        | "llm_call_structured_result"
        | "llm_stream"
        | "llm_stream_call" => (CallSurface::Direct, 2),
        "llm_completion" => (CallSurface::Direct, 3),
        _ => return None,
    };
    args.get(index).map(|options| (surface, options))
}

fn check_literal_composition(
    call_name: &str,
    surface: CallSurface,
    options: &SNode,
    file_path: &Path,
    source: &str,
    diagnostics: &mut Vec<PreflightDiagnostic>,
) {
    let Some(raw_model) = dict_literal_field(options, "model").and_then(literal_string) else {
        return;
    };

    let (resolved_model, alias_provider) = harn_vm::llm_config::resolve_model(&raw_model);
    let provider = match dict_literal_field(options, "provider") {
        Some(provider) => match literal_string(provider) {
            Some(provider) if !provider.eq_ignore_ascii_case("auto") => provider,
            Some(_) => {
                alias_provider.unwrap_or_else(|| harn_vm::llm_config::infer_provider(&raw_model))
            }
            None => return,
        },
        None => alias_provider.unwrap_or_else(|| harn_vm::llm_config::infer_provider(&raw_model)),
    };

    let effort_node = dict_literal_field(options, "effort");
    let effort = effort_node.and_then(literal_string).and_then(|value| {
        serde_json::from_value::<harn_vm::llm::capabilities::ReasoningEffort>(
            serde_json::Value::String(value),
        )
        .ok()
    });
    let dynamic_effort = effort_node.is_some_and(|node| {
        literal_string(node).is_none() && !matches!(node.node, Node::NilLiteral)
    });
    let caps = harn_vm::llm::capabilities::lookup(&provider, &resolved_model);
    for option in harn_vm::llm::capabilities::PortableOption::ALL {
        let Some(value) = dict_literal_field(options, option.name()) else {
            continue;
        };
        if !portable_option_intent_is_static(option, value) {
            continue;
        }
        if dynamic_effort && caps.portable_option_effort_support.contains_key(&option) {
            continue;
        }
        let admission = if option == harn_vm::llm::capabilities::PortableOption::PromptCacheTtl {
            harn_vm::llm::capabilities::admit_prompt_cache_ttl(
                &provider,
                &resolved_model,
                &literal_string(value).expect("static TTL intent is a string literal"),
            )
        } else {
            harn_vm::llm::capabilities::admit_portable_option_with_effort(
                &provider,
                &resolved_model,
                option,
                effort,
            )
        };
        let Err(reason) = admission else {
            continue;
        };
        diagnostics.push(PreflightDiagnostic {
            code: Code::LlmCapabilityCompositionInvalid,
            path: file_path.display().to_string(),
            source: source.to_string(),
            span: value.span,
            message: format!(
                "preflight: `{call_name}` requests a known-unsupported LLM option: {reason}"
            ),
            help: Some(
                "remove the option, choose a compatible route, or use the documented provider_options namespace for a provider-native control"
                    .to_string(),
            ),
            tags: None,
        });
    }

    check_literal_reasoning(
        call_name,
        options,
        &provider,
        &resolved_model,
        file_path,
        source,
        diagnostics,
    );
    check_direct_text_tools(
        call_name,
        surface,
        options,
        &provider,
        &resolved_model,
        file_path,
        source,
        diagnostics,
    );

    // Raw LLM calls only care about a tool format when they offer tools.
    // Agent calls and option constructors feed the tool-bearing agent loop.
    if surface == CallSurface::Direct
        && dict_literal_field(options, "tools").and_then(literal_tools_have_entries) != Some(true)
    {
        return;
    }

    // Agent options expose an audited experiment seam. Raw llm_call options do
    // not, so only honor the reason on surfaces that can emit its transcript
    // event at runtime.
    if surface == CallSurface::Agent {
        if let Some(reason) = dict_literal_field(options, "tool_format_override_reason") {
            let Some(reason) = literal_string(reason) else {
                return;
            };
            if !reason.trim().is_empty() {
                return;
            }
        }
    }

    let invalid_tool_format = if let Some(message) =
        harn_vm::llm::capabilities::no_viable_tool_channel(&provider, &resolved_model)
    {
        Some(message)
    } else {
        let Some(requested) = dict_literal_field(options, "tool_format").and_then(literal_string)
        else {
            return;
        };
        if requested == "auto" {
            return;
        }
        harn_vm::llm::capabilities::validate_tool_format(&provider, &resolved_model, &requested)
            .correction
    };
    let Some(reason) = invalid_tool_format else {
        return;
    };

    diagnostics.push(PreflightDiagnostic {
        code: Code::LlmCapabilityCompositionInvalid,
        path: file_path.display().to_string(),
        source: source.to_string(),
        span: options.span,
        message: format!(
            "preflight: `{call_name}` requests a known-unsafe LLM composition: {reason}"
        ),
        help: Some(if surface == CallSurface::Direct {
            "use the catalog-recommended tool_format (or omit it)".to_string()
        } else {
            "use the catalog-recommended tool_format (or omit it), or add a non-empty \
             tool_format_override_reason for a deliberate provider probe"
                .to_string()
        }),
        tags: None,
    });
}

/// Literal `effort` / `thinking` values go through the runtime's own reasoning
/// resolution for the literal route, so a known-bad pair (effort on a route
/// without it) is a check-time error with the runtime's message.
#[allow(clippy::too_many_arguments)]
fn check_literal_reasoning(
    call_name: &str,
    options: &SNode,
    provider: &str,
    model: &str,
    file_path: &Path,
    source: &str,
    diagnostics: &mut Vec<PreflightDiagnostic>,
) {
    let effort_node = dict_literal_field(options, "effort");
    let thinking_node = dict_literal_field(options, "thinking");
    let effort = effort_node.and_then(literal_string);
    let thinking = thinking_node.and_then(|node| match node.node {
        Node::BoolLiteral(value) => Some(value),
        _ => None,
    });
    if effort.is_none() && thinking.is_none() {
        return;
    }
    let Err(reason) =
        harn_vm::llm::admit_reasoning_literals(provider, model, effort.as_deref(), thinking)
    else {
        return;
    };
    let span = effort_node
        .filter(|_| effort.is_some())
        .or(thinking_node)
        .map_or(options.span, |node| node.span);
    diagnostics.push(PreflightDiagnostic {
        code: Code::LlmCapabilityCompositionInvalid,
        path: file_path.display().to_string(),
        source: source.to_string(),
        span,
        message: format!("preflight: `{call_name}` requests a known-unsupported reasoning option: {reason}"),
        help: Some(
            "use the reasoning control the route declares (see `harn provider catalog matrix`), or choose a route that supports it"
                .to_string(),
        ),
        tags: None,
    });
}

/// A direct model call (not `agent_loop`) that offers literal tools on a
/// text-channel format sends no tools at all. Reports the runtime's refusal.
#[allow(clippy::too_many_arguments)]
fn check_direct_text_tools(
    call_name: &str,
    surface: CallSurface,
    options: &SNode,
    provider: &str,
    model: &str,
    file_path: &Path,
    source: &str,
    diagnostics: &mut Vec<PreflightDiagnostic>,
) {
    let Some(tools) = dict_literal_field(options, "tools") else {
        return;
    };
    if surface != CallSurface::Direct
        || literal_tools_have_entries(tools) != Some(true)
        || dict_literal_field(options, "tool_search").is_some_and(|search| {
            !matches!(search.node, Node::NilLiteral | Node::BoolLiteral(false))
        })
    {
        return;
    }
    let requested = match dict_literal_field(options, "tool_format") {
        Some(node) => match literal_string(node) {
            Some(format) if format != "auto" => format,
            Some(_) => harn_vm::llm_config::default_tool_format(model, provider),
            _ => return,
        },
        None => harn_vm::llm_config::default_tool_format(model, provider),
    };
    let effective =
        harn_vm::llm::capabilities::validate_tool_format(provider, model, &requested).effective;
    // A steered format is already reported by the tool-format check, which
    // names the requested channel and the row that forbids it.
    if effective != requested {
        return;
    }
    let Some(reason) =
        harn_vm::llm::capabilities::direct_call_text_tools_refusal(provider, model, &effective)
    else {
        return;
    };
    diagnostics.push(PreflightDiagnostic {
        code: Code::LlmCapabilityCompositionInvalid,
        path: file_path.display().to_string(),
        source: source.to_string(),
        span: tools.span,
        message: format!("preflight: `{call_name}` would drop its tools: {reason}"),
        help: Some(
            "drive the tools through agent_loop, or use tool_format \"native\" on a route that supports it"
                .to_string(),
        ),
        tags: None,
    });
}

/// Unknown expressions remain runtime-checked: they may evaluate to an empty
/// set. Literal registries carry their tools in the same field the runtime reads.
fn literal_tools_have_entries(tools: &SNode) -> Option<bool> {
    match &tools.node {
        Node::NilLiteral => Some(false),
        Node::ListLiteral(items) => Some(!items.is_empty()),
        Node::DictLiteral(_) => dict_literal_field(tools, "tools").and_then(|items| {
            if let Node::ListLiteral(items) = &items.node {
                Some(!items.is_empty())
            } else {
                None
            }
        }),
        _ => None,
    }
}

/// Preflight reports only caller intent visible without evaluation. Dynamic
/// values fall through to runtime admission; `cache: false` is an opt-out,
/// not a request for the caching capability.
fn portable_option_intent_is_static(
    option: harn_vm::llm::capabilities::PortableOption,
    value: &SNode,
) -> bool {
    match option {
        harn_vm::llm::capabilities::PortableOption::Cache => {
            matches!(value.node, Node::BoolLiteral(true))
        }
        harn_vm::llm::capabilities::PortableOption::PromptCacheTtl => {
            literal_string(value).is_some()
        }
        _ => matches!(
            value.node,
            Node::StringLiteral(_)
                | Node::RawStringLiteral(_)
                | Node::IntLiteral(_)
                | Node::FloatLiteral(_)
                | Node::BoolLiteral(_)
                | Node::ListLiteral(_)
                | Node::DictLiteral(_)
        ),
    }
}