harn-vm 0.10.153

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
574
575
576
577
578
579
//! 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());
}

/// Opus 5.5 and the Fable/Mythos tier always think: `{type:"disabled"}` is a
/// 400 (live API, 2026-09-22). A thinking-off request must therefore omit the
/// field and ask for the lowest effort, or the model thinks at the API
/// default (`medium` on Opus 5.5) — the opposite of what was asked.
#[test]
fn always_on_models_lower_thinking_off_to_the_lowest_effort() {
    for model in ["claude-opus-5-5", "claude-fable-5-1"] {
        let mut payload = base_payload();
        payload.model = model.to_string();
        payload.thinking = ThinkingConfig::Disabled;
        let body = AnthropicProvider::build_request_body(&payload);
        assert!(body.get("thinking").is_none(), "{model}: {body}");
        assert_eq!(body["output_config"]["effort"], "low", "{model}: {body}");

        // `ReasoningEffort::None` is the same request spelled as an effort.
        let mut none = base_payload();
        none.model = model.to_string();
        none.thinking = ThinkingConfig::Effort {
            level: crate::llm::api::ReasoningEffort::None,
        };
        let body = AnthropicProvider::build_request_body(&none);
        assert!(body.get("thinking").is_none(), "{model}: {body}");
        assert_eq!(body["output_config"]["effort"], "low", "{model}: {body}");
    }

    // Control: Opus 5 accepts the explicit off switch, so it keeps it and
    // gains no effort the caller did not ask for.
    let mut opus5 = base_payload();
    opus5.model = "claude-opus-5".to_string();
    opus5.thinking = ThinkingConfig::Disabled;
    let body = AnthropicProvider::build_request_body(&opus5);
    assert_eq!(body["thinking"], serde_json::json!({ "type": "disabled" }));
    assert!(body.pointer("/output_config/effort").is_none(), "{body}");
}

/// A caller override can put `thinking:{disabled}` back after the builder ran.
/// The egress seam re-lowers it for always-on models and keeps an effort the
/// caller chose.
#[test]
fn always_on_models_relower_a_disabled_thinking_override_at_egress() {
    let reconciled = |model: &str, body: serde_json::Value| {
        let mut body = body;
        reconcile_request_body(
            &mut body,
            "anthropic",
            model,
            &ThinkingConfig::Disabled,
            None,
        );
        body
    };

    let body = reconciled(
        "claude-opus-5-5",
        serde_json::json!({"model": "claude-opus-5-5", "thinking": {"type": "disabled"}}),
    );
    assert!(body.get("thinking").is_none(), "{body}");
    assert_eq!(body["output_config"]["effort"], "low", "{body}");

    let body = reconciled(
        "claude-opus-5-5",
        serde_json::json!({
            "model": "claude-opus-5-5",
            "thinking": {"type": "disabled"},
            "output_config": {"effort": "high"},
        }),
    );
    assert!(body.get("thinking").is_none(), "{body}");
    assert_eq!(body["output_config"]["effort"], "high", "{body}");

    // Control: Opus 5 keeps the override it accepts.
    let body = reconciled(
        "claude-opus-5",
        serde_json::json!({"model": "claude-opus-5", "thinking": {"type": "disabled"}}),
    );
    assert_eq!(body["thinking"], serde_json::json!({ "type": "disabled" }));
}

/// Sonnet 5.5 turns thinking off with `between_tools`; `disabled` is a 400
/// there, while Sonnet 5 (the control) still takes `disabled`. The same shape
/// must reach the wire from the builder, from a caller override at egress,
/// and under the `high` effort ceiling both off switches share.
#[test]
fn sonnet_55_lowers_thinking_off_to_between_tools() {
    let built = |model: &str| {
        let mut payload = base_payload();
        payload.model = model.to_string();
        payload.thinking = ThinkingConfig::Disabled;
        AnthropicProvider::build_request_body(&payload)
    };
    assert_eq!(
        built("claude-sonnet-5-5")["thinking"],
        serde_json::json!({ "type": "between_tools" })
    );
    assert_eq!(
        built("claude-sonnet-5")["thinking"],
        serde_json::json!({ "type": "disabled" })
    );

    let reconciled = |model: &str, thinking: &str, effort: &str| {
        let mut body = serde_json::json!({
            "model": model,
            "thinking": {"type": thinking},
            "output_config": {"effort": effort},
        });
        reconcile_request_body(
            &mut body,
            "anthropic",
            model,
            &ThinkingConfig::Disabled,
            None,
        );
        body
    };
    // A caller override written for the older models is re-lowered.
    let body = reconciled("claude-sonnet-5-5", "disabled", "medium");
    assert_eq!(
        body["thinking"],
        serde_json::json!({ "type": "between_tools" })
    );
    assert_eq!(body["output_config"]["effort"], "medium");
    // ...and the reverse, so a Sonnet 5.5 override cannot 400 on Sonnet 5.
    let body = reconciled("claude-sonnet-5", "between_tools", "medium");
    assert_eq!(body["thinking"], serde_json::json!({ "type": "disabled" }));
    // Before generation 5 an omitted field is the off switch, so an unknown
    // `between_tools` is dropped rather than sent.
    let body = reconciled("claude-opus-4-8", "between_tools", "medium");
    assert!(body.get("thinking").is_none(), "{body}");
    // `between_tools` above `high` is a 400, like `disabled`.
    for effort in ["xhigh", "max"] {
        let body = reconciled("claude-sonnet-5-5", "between_tools", effort);
        assert_eq!(body["output_config"]["effort"], "high", "{effort}");
        assert_eq!(
            body["thinking"],
            serde_json::json!({ "type": "between_tools" })
        );
    }
}

#[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"}));
}

#[test]
fn an_unnamed_thinking_budget_stays_under_the_output_cap() {
    // Anthropic 400s on `budget_tokens >= max_tokens`; the old fixed 10,000
    // default did exactly that at any cap up to 10,000.
    for (max_tokens, expected) in [(2048, 2047), (8192, 8191), (64_000, 10_000), (512, 1024)] {
        let mut payload = base_payload();
        payload.model = "claude-sonnet-4-6".to_string();
        payload.max_tokens = max_tokens;
        payload.thinking = ThinkingConfig::Enabled {
            budget_tokens: None,
        };
        let body = AnthropicProvider::build_request_body(&payload);
        assert_eq!(
            body["thinking"],
            serde_json::json!({"type": "enabled", "budget_tokens": expected}),
            "max_tokens {max_tokens}"
        );
    }
    // A named budget is the caller's, unchanged.
    let mut payload = base_payload();
    payload.model = "claude-sonnet-4-6".to_string();
    payload.max_tokens = 2048;
    payload.thinking = ThinkingConfig::Enabled {
        budget_tokens: Some(1500),
    };
    let body = AnthropicProvider::build_request_body(&payload);
    assert_eq!(body["thinking"]["budget_tokens"], 1500);
}