harn-vm 0.10.125

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
//! Claude generation gating: adaptive thinking, effort, and the sampling
//! parameters the 4.7+ surface rejects. Split out of `anthropic.rs` to keep
//! that module under the source-length ratchet.

use super::anthropic::{
    claude_generation, claude_model_supports_tool_search, model_defaults_to_adaptive_thinking,
    reconcile_request_body, strip_unsupported_bedrock_converse_sampling_params,
    strip_unsupported_sampling_params, AnthropicProvider,
};
use super::anthropic_test_support::base_payload;
use crate::llm::api::{ReasoningEffort, ThinkingConfig};

#[test]
fn fable_and_mythos_parse_generation_and_inherit_guards() {
    // Fable/Mythos 5 (launched 2026-06-09) share the Opus 4.7+ request
    // surface; the generation parser must recognize the families or none
    // of the >= (4, 6) / (4, 7) guards (prefill removal, sampling strip,
    // adaptive-thinking rewrite) fire for them.
    assert_eq!(claude_generation("claude-fable-5"), Some((5, 0)));
    assert_eq!(claude_generation("claude-mythos-5"), Some((5, 0)));
    assert_eq!(claude_generation("anthropic/claude-fable-5"), Some((5, 0)));
    assert_eq!(
        claude_generation("anthropic.claude-opus-4-7-v1:0"),
        Some((4, 7))
    );
    assert_eq!(
        claude_generation("anthropic.claude-3-5-sonnet-20240620-v1:0"),
        Some((3, 5))
    );
    // Mythos Preview has no numeric generation — stays unrecognized.
    assert_eq!(claude_generation("claude-mythos-preview"), None);
    assert!(claude_model_supports_tool_search("claude-fable-5"));
}

#[test]
fn fable_thinking_payloads_match_always_on_surface() {
    // Extended-thinking budgets are a 400 on Fable — rewritten to adaptive.
    let mut payload = base_payload();
    payload.model = "claude-fable-5".to_string();
    payload.thinking = ThinkingConfig::Enabled {
        budget_tokens: Some(4096),
    };
    let body = AnthropicProvider::build_request_body(&payload);
    assert_eq!(body["thinking"], serde_json::json!({ "type": "adaptive" }));

    // Thinking is always on for Fable, and an explicit
    // `thinking: {type: "disabled"}` is also a 400 — a Disabled config
    // must leave the field out of the payload entirely.
    let mut payload2 = base_payload();
    payload2.model = "claude-fable-5".to_string();
    payload2.thinking = ThinkingConfig::Disabled;
    payload2.temperature = Some(0.0);
    let body2 = AnthropicProvider::build_request_body(&payload2);
    assert!(body2.get("thinking").is_none());
    // Sampling params are rejected on the 4.7+ surface — stripped.
    assert!(
        body2.get("temperature").is_none(),
        "temperature must be stripped for claude-fable-5"
    );
}

#[test]
fn opus_5_disabled_thinking_is_explicit_not_omitted() {
    // Through Opus 4.8, omitting `thinking` was the off switch. Opus 5
    // defaults it to adaptive, so an omitted field silently buys thinking
    // tokens the caller asked not to spend. Verified against the live API
    // on 2026-07-24: an omitted `thinking` returns thinking blocks.
    let mut payload = base_payload();
    payload.model = "claude-opus-5".to_string();
    payload.thinking = ThinkingConfig::Disabled;
    let body = AnthropicProvider::build_request_body(&payload);
    assert_eq!(
        body["thinking"],
        serde_json::json!({ "type": "disabled" }),
        "Opus 5 thinks when `thinking` is omitted; Disabled must be explicit"
    );

    // Opus 4.8 keeps the omit-means-off surface.
    let mut prior = base_payload();
    prior.model = "claude-opus-4-8".to_string();
    prior.thinking = ThinkingConfig::Disabled;
    assert!(AnthropicProvider::build_request_body(&prior)
        .get("thinking")
        .is_none());
}

#[test]
fn opus_5_clamps_effort_when_thinking_is_disabled() {
    // `thinking:{disabled}` above effort `high` is a 400 on generation-5
    // models. The two halves are set independently — thinking by the
    // request builder, effort by a caller override merged in afterwards —
    // so the guard runs on the final body at the egress seam.
    let reconciled = |model: &str, effort: &str| {
        let mut body = serde_json::json!({
            "model": model,
            "thinking": {"type": "disabled"},
            "output_config": {"effort": effort},
        });
        reconcile_request_body(
            &mut body,
            "anthropic",
            model,
            &ThinkingConfig::Disabled,
            None,
        );
        body
    };

    for requested in ["xhigh", "max"] {
        let body = reconciled("claude-opus-5", requested);
        assert_eq!(
            body["output_config"]["effort"],
            serde_json::json!("high"),
            "effort `{requested}` must clamp to `high` when thinking is disabled"
        );
        assert_eq!(body["thinking"], serde_json::json!({ "type": "disabled" }));
    }

    // `high` and below are legal on the same pairing.
    assert_eq!(
        reconciled("claude-opus-5", "medium")["output_config"]["effort"],
        serde_json::json!("medium")
    );

    // Thinking left on: `xhigh` is legal and must survive untouched.
    let mut thinking_on = serde_json::json!({
        "model": "claude-opus-5",
        "output_config": {"effort": "xhigh"},
    });
    reconcile_request_body(
        &mut thinking_on,
        "anthropic",
        "claude-opus-5",
        &ThinkingConfig::Effort {
            level: ReasoningEffort::XHigh,
        },
        None,
    );
    assert_eq!(
        thinking_on["output_config"]["effort"],
        serde_json::json!("xhigh"),
        "effort must not be clamped while thinking is active"
    );

    // Pre-generation-5 models accept the pair; nothing is clamped.
    assert_eq!(
        reconciled("claude-opus-4-8", "xhigh")["output_config"]["effort"],
        serde_json::json!("xhigh")
    );
}

#[test]
fn generation_5_drives_default_on_thinking_not_a_model_id_list() {
    // The rule is "generation >= 5", so a new gen-5 family inherits the
    // right surface instead of falling back to the 4.x omit-means-off
    // assumption.
    for model in [
        "claude-opus-5",
        "claude-sonnet-5",
        "claude-fable-5",
        "claude-mythos-5",
        "anthropic/claude-opus-5",
    ] {
        assert!(
            model_defaults_to_adaptive_thinking(model),
            "{model} should default adaptive thinking on"
        );
    }
    for model in ["claude-opus-4-8", "claude-sonnet-4-6", "claude-haiku-4-5"] {
        assert!(
            !model_defaults_to_adaptive_thinking(model),
            "{model} needs an explicit thinking field to reason"
        );
    }
}

#[test]
fn sonnet_5_effort_uses_output_config_and_default_on_thinking() {
    let mut payload = base_payload();
    payload.model = "claude-sonnet-5".to_string();
    payload.thinking = ThinkingConfig::Effort {
        level: ReasoningEffort::High,
    };
    let body = AnthropicProvider::build_request_body(&payload);
    assert_eq!(body["output_config"]["effort"], serde_json::json!("high"));
    assert!(
        body.get("thinking").is_none(),
        "Sonnet 5 defaults adaptive thinking on; effort should not send legacy thinking budgets"
    );

    let mut disabled = base_payload();
    disabled.model = "claude-sonnet-5".to_string();
    disabled.thinking = ThinkingConfig::Disabled;
    let disabled_body = AnthropicProvider::build_request_body(&disabled);
    assert_eq!(
        disabled_body["thinking"],
        serde_json::json!({ "type": "disabled" })
    );
    assert!(
        disabled_body.get("output_config").is_none(),
        "turning Sonnet 5 thinking off should not also send an effort level"
    );
}

#[test]
fn opus_adaptive_effort_uses_output_config_with_adaptive_thinking() {
    let mut payload = base_payload();
    payload.model = "claude-opus-4-7".to_string();
    payload.thinking = ThinkingConfig::Effort {
        level: ReasoningEffort::Max,
    };
    let body = AnthropicProvider::build_request_body(&payload);
    assert_eq!(body["thinking"], serde_json::json!({ "type": "adaptive" }));
    assert_eq!(body["output_config"]["effort"], serde_json::json!("max"));
}

#[test]
fn temperature_stripped_when_thinking_active() {
    // Anthropic rejects HTTP 400 if `temperature != 1` when thinking is
    // active. Strip the temperature transparently so callers can default
    // to temperature=0 for determinism without having to know which
    // models silently auto-enable thinking.
    let mut payload = base_payload();
    payload.temperature = Some(0.0);
    payload.thinking = ThinkingConfig::Adaptive;
    let body = AnthropicProvider::build_request_body(&payload);
    assert!(
        body.get("temperature").is_none(),
        "temperature must be stripped when thinking is active to avoid HTTP 400"
    );
    // Sanity: temperature is preserved when thinking is disabled.
    let mut payload2 = base_payload();
    payload2.temperature = Some(0.0);
    payload2.thinking = ThinkingConfig::Disabled;
    let body2 = AnthropicProvider::build_request_body(&payload2);
    assert_eq!(body2["temperature"], serde_json::json!(0.0));
}

#[test]
fn sampling_params_stripped_by_shared_helper_for_rejecting_models() {
    let mut body = serde_json::json!({
        "model": "claude-opus-4-7",
        "temperature": 0.2,
        "top_p": 0.9,
        "top_k": 20,
    });

    strip_unsupported_sampling_params(
        &mut body,
        "claude-opus-4-7",
        &ThinkingConfig::Disabled,
        None,
    );

    assert!(body.get("temperature").is_none());
    assert!(body.get("top_p").is_none());
    assert!(body.get("top_k").is_none());
    assert_eq!(body["model"], serde_json::json!("claude-opus-4-7"));
}

#[test]
fn option_probe_preserves_only_its_selected_sampling_field() {
    use crate::llm::capabilities::PortableOption;

    let mut direct = serde_json::json!({
        "model": "claude-opus-4-7",
        "temperature": 0.2,
        "top_p": 0.9,
        "top_k": 20,
    });
    strip_unsupported_sampling_params(
        &mut direct,
        "claude-opus-4-7",
        &ThinkingConfig::Disabled,
        Some(PortableOption::TopP),
    );

    assert!(direct.get("temperature").is_none());
    assert_eq!(direct["top_p"], serde_json::json!(0.9));
    assert!(direct.get("top_k").is_none());

    let mut bedrock = serde_json::json!({
        "inferenceConfig": {"temperature": 0.2, "topP": 0.9, "topK": 20},
    });
    strip_unsupported_bedrock_converse_sampling_params(
        &mut bedrock,
        "anthropic.claude-opus-4-7-v1:0",
        &ThinkingConfig::Disabled,
        Some(PortableOption::TopP),
    );

    assert!(bedrock["inferenceConfig"].get("temperature").is_none());
    assert_eq!(bedrock["inferenceConfig"]["topP"], serde_json::json!(0.9));
    assert!(bedrock["inferenceConfig"].get("topK").is_none());
}

#[test]
fn final_reconciliation_preserves_probe_option_and_removes_rejected_prefill() {
    use crate::llm::capabilities::PortableOption;

    let mut body = serde_json::json!({
        "model": "claude-opus-5",
        "messages": [
            {"role": "user", "content": "Reply with ok"},
            {"role": "assistant", "content": "persisted prefill"},
        ],
        "temperature": 0.2,
        "top_p": 0.9,
    });
    reconcile_request_body(
        &mut body,
        "anthropic",
        "claude-opus-5",
        &ThinkingConfig::Disabled,
        Some(PortableOption::Temperature),
    );

    assert_eq!(body["temperature"], serde_json::json!(0.2));
    assert!(body.get("top_p").is_none());
    assert_eq!(
        body["messages"],
        serde_json::json!([{"role": "user", "content": "Reply with ok"}])
    );
}

#[test]
fn sampling_params_stripped_by_shared_helper_when_thinking_active() {
    let mut body = serde_json::json!({
        "model": "claude-sonnet-4-6",
        "temperature": 0.0,
        "top_p": 0.9,
        "top_k": 20,
    });

    strip_unsupported_sampling_params(
        &mut body,
        "claude-sonnet-4-6",
        &ThinkingConfig::Adaptive,
        None,
    );

    assert!(body.get("temperature").is_none());
    assert!(body.get("top_p").is_none());
    assert!(body.get("top_k").is_none());
}

#[test]
fn sampling_params_preserved_by_shared_helper_for_supported_disabled_thinking() {
    let mut body = serde_json::json!({
        "model": "claude-sonnet-4-6",
        "temperature": 0.2,
        "top_p": 0.9,
        "top_k": 20,
    });

    strip_unsupported_sampling_params(
        &mut body,
        "claude-sonnet-4-6",
        &ThinkingConfig::Disabled,
        None,
    );

    assert_eq!(body["temperature"], serde_json::json!(0.2));
    assert_eq!(body["top_p"], serde_json::json!(0.9));
    assert_eq!(body["top_k"], serde_json::json!(20));
}

#[test]
fn sampling_params_stripped_when_body_thinking_override_is_active() {
    let mut body = serde_json::json!({
        "model": "claude-sonnet-4-6",
        "temperature": 0.2,
        "top_p": 0.9,
        "thinking": {"type": "enabled", "budget_tokens": 1024},
    });

    strip_unsupported_sampling_params(
        &mut body,
        "claude-sonnet-4-6",
        &ThinkingConfig::Disabled,
        None,
    );

    assert!(body.get("temperature").is_none());
    assert!(body.get("top_p").is_none());
    assert_eq!(
        body["thinking"],
        serde_json::json!({"type": "enabled", "budget_tokens": 1024})
    );
}

#[test]
fn sampling_params_stripped_when_body_output_config_effort_override_is_active() {
    let mut body = serde_json::json!({
        "model": "claude-sonnet-4-6",
        "temperature": 0.2,
        "top_p": 0.9,
        "output_config": {"effort": "high"},
    });

    strip_unsupported_sampling_params(
        &mut body,
        "claude-sonnet-4-6",
        &ThinkingConfig::Disabled,
        None,
    );

    assert!(body.get("temperature").is_none());
    assert!(body.get("top_p").is_none());
    assert_eq!(body["output_config"], serde_json::json!({"effort": "high"}));
}