harn-vm 0.10.162

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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
//! Unit tests for [`super`] — the versioned, scoped LLM mock-fixture engine.
//! Split out of `mock.rs` to keep that file under the source-length ceiling;
//! included via `#[path = "mock_tests.rs"] mod tests;`.

use super::*;
use crate::agent_events::{AgentEvent, AgentEventSink};
use crate::llm::api::LlmRequestPayload;
use std::future::Future;
use std::sync::{Arc, Mutex};
use std::task::Poll;

fn text_mock(text: &str) -> LlmMock {
    LlmMock {
        effective_reasoning_effort: Default::default(),
        text: text.to_string(),
        tool_calls: Vec::new(),
        raw_tool_calls: Vec::new(),
        match_pattern: None,
        scope: DEFAULT_MOCK_SCOPE.to_string(),
        entry_id: String::new(),
        sticky: false,
        input_tokens: None,
        output_tokens: None,
        cache_read_tokens: None,
        cache_write_tokens: None,
        simulated_cost_usd: None,
        thinking: None,
        thinking_summary: None,
        stop_reason: None,
        model: "fixture-model".to_string(),
        provider: None,
        blocks: None,
        logprobs: Vec::new(),
        error: None,
        stream_chunks: Vec::new(),
    }
}

#[test]
fn build_mock_result_surfaces_fixture_tool_calls() {
    // The CLI-mock fixture shape a downstream native-tool test uses.
    let mock = crate::llm::jsonl::parse_llm_mock_value(&serde_json::json!({
        "match": "*",
        "consume_match": true,
        "tool_calls": [{"name": "ask_user", "arguments": {"question": "Which?"}}]
    }))
    .expect("parse mock");
    assert!(
        !mock.tool_calls.is_empty(),
        "fixture tool_calls must parse into the mock: {:?}",
        mock.tool_calls
    );
    let result = build_mock_result(&mock, 10, &mut 0);
    let usage = result.usage();
    assert_eq!(usage.cost_usd, Some(0.0));
    assert_eq!(
        usage.accounting_status,
        crate::llm::usage::UsageAccountingStatus::Reported
    );
    assert!(
        !result.tool_calls.is_empty(),
        "build_mock_result must surface tool_calls: {:?}",
        result.tool_calls
    );
    assert_eq!(result.tool_calls[0]["name"], "ask_user");
}

#[test]
fn cli_mock_tool_call_ids_are_unique_across_responses() {
    reset_llm_mock_state();
    let fixture_tool_call = || {
        crate::llm::jsonl::parse_llm_mock_value(&serde_json::json!({
            "match": "*",
            "consume_match": true,
            "tool_calls": [{"name": "ask_user", "arguments": {"question": "Which?"}}]
        }))
        .expect("parse mock")
    };
    install_cli_llm_mocks(vec![fixture_tool_call(), fixture_tool_call()]);
    let request = LlmRequestPayload::from(&crate::llm::api::options::base_opts("anthropic"));

    let first = mock_llm_response(&request).expect("first mock response");
    let second = mock_llm_response(&request).expect("second mock response");

    assert_eq!(first.tool_calls[0]["id"], "mock_call_1");
    assert_eq!(second.tool_calls[0]["id"], "mock_call_2");
    clear_cli_llm_mock_mode();
}

#[test]
fn cli_llm_mock_replay_scope_survives_provider_worker_thread() {
    reset_llm_mock_state();
    install_cli_llm_mocks(vec![text_mock("cross-thread replay")]);
    let request = LlmRequestPayload::from(&crate::llm::api::options::base_opts("anthropic"));

    assert!(request.cli_llm_mock_scope.is_some());
    assert!(crate::llm::providers::MockProvider::should_intercept_request(&request));

    let result = crate::runtime_stack::spawn(move || {
        assert!(crate::llm::providers::MockProvider::should_intercept_request(&request));
        mock_llm_response(&request)
    })
    .join()
    .expect("provider worker thread")
    .expect("mock response");

    assert_eq!(result.text, "cross-thread replay");
    clear_cli_llm_mock_mode();
}

#[test]
fn cli_llm_mock_record_scope_collects_provider_worker_thread_results() {
    reset_llm_mock_state();
    enable_cli_llm_mock_recording();
    let request = LlmRequestPayload::from(&crate::llm::api::options::base_opts("anthropic"));
    let mut result = build_mock_result(&text_mock("cross-thread record"), 7, &mut 0);
    let effort = crate::llm::EffectiveReasoningEffort::Reported {
        level: "medium".into(),
        source: crate::llm::ReasoningEffortSource::ProviderDefault,
    };
    result.telemetry.effective_reasoning_effort = effort.clone();

    assert!(request.cli_llm_mock_scope.is_some());
    crate::runtime_stack::spawn(move || record_cli_llm_result(&request, &result))
        .join()
        .expect("provider worker thread");

    let recordings = take_cli_llm_recordings();
    assert_eq!(recordings.len(), 1);
    assert_eq!(recordings[0].text, "cross-thread record");
    let serialized = crate::llm::jsonl::serialize_llm_mock(recordings[0].clone()).unwrap();
    let replay =
        crate::llm::jsonl::parse_llm_mock_value(&serde_json::from_str(&serialized).unwrap())
            .unwrap();
    assert_eq!(
        build_mock_result(&replay, 7, &mut 0)
            .telemetry
            .effective_reasoning_effort,
        effort
    );
    clear_cli_llm_mock_mode();
}

#[test]
fn offthread_mock_call_keeps_the_callers_logical_context() {
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .worker_threads(2)
        .build()
        .expect("runtime");

    runtime.block_on(async {
        reset_llm_mock_state();
        push_llm_mock(text_mock("logical mock"));
        let opts = crate::llm::api::options::base_opts("mock");
        let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
        let result = crate::llm::api::vm_call_llm_full_streaming_offthread(&opts, tx)
            .await
            .expect("off-thread mock call");

        assert_eq!(result.text, "logical mock");
        assert_eq!(get_llm_mock_calls().len(), 1);
        assert_eq!(get_llm_mock_receipts().len(), 1);
        reset_llm_mock_state();
    });
}

#[test]
fn inline_mock_scope_survives_a_deterministic_executor_thread_hop() {
    reset_llm_mock_state();
    let ambient = crate::orchestration::AmbientExecutionScope::capture_for_top_level_execution(
        crate::observability::execution_scope::mint_execution_scope(),
        LlmMockContext::default(),
        crate::stdlib::agents::agents_workers::fresh_worker_registry(),
        crate::stdlib::agents_daemon::fresh_daemon_registry(),
        crate::triggers::registry::runtime::fresh_trigger_registry(),
        crate::agent_sessions::fresh_session_runtime(),
        crate::tracing::fresh_tracing_runtime(),
        crate::llm::agent_session_host::fresh_agent_host_session_runtime(),
    );
    let mut first_poll = true;
    let inner = std::future::poll_fn(move |cx| {
        if first_poll {
            first_poll = false;
            push_llm_mock_scope();
            push_llm_mock(text_mock("thread-hop"));
            cx.waker().wake_by_ref();
            return Poll::Pending;
        }

        let request = LlmRequestPayload::from(&crate::llm::api::options::base_opts("mock"));
        let result = mock_llm_response(&request).expect("mock after thread hop");
        let calls = get_llm_mock_calls().len();
        let receipts = get_llm_mock_receipts().len();
        let popped = pop_llm_mock_scope();
        Poll::Ready((
            result.text,
            calls,
            receipts,
            popped,
            builtin_llm_mock_active(),
        ))
    });
    let mut scoped = Box::pin(crate::orchestration::scope_ambient(ambient, inner));
    let waker = futures::task::noop_waker();
    let mut cx = std::task::Context::from_waker(&waker);
    assert!(matches!(scoped.as_mut().poll(&mut cx), Poll::Pending));

    let observed = crate::runtime_stack::spawn(move || {
        let waker = futures::task::noop_waker();
        let mut cx = std::task::Context::from_waker(&waker);
        match scoped.as_mut().poll(&mut cx) {
            Poll::Ready(observed) => observed,
            Poll::Pending => panic!("second poll must complete"),
        }
    })
    .join()
    .expect("executor thread");

    assert_eq!(observed, ("thread-hop".to_string(), 1, 1, true, false));
    assert!(!builtin_llm_mock_active(), "scope must not leak to caller");
}

fn fixture_script(prompt: &str) -> crate::Chunk {
    crate::compile_source(&format!(
        r#"
fn main(harness: Harness) {{
harness.runtime.yield_now()
const response = harness.llm.call("{prompt}", nil, {{provider: "mock"}})
harness.stdio.println(response.text)
harness.stdio.println(len(harness.llm.mock_calls()))
}}
"#
    ))
    .expect("compile fixture script")
}

#[tokio::test(flavor = "current_thread")]
async fn concurrent_vms_isolate_inline_queues_and_call_logs() {
    let alpha = crate::compile_source(
        r#"
fn main(harness: Harness) {
harness.llm.mock_enqueue({text: "alpha"})
harness.runtime.yield_now()
const response = harness.llm.call("alpha prompt", nil, {provider: "mock"})
harness.stdio.println(response.text)
harness.stdio.println(len(harness.llm.mock_calls()))
}
"#,
    )
    .expect("compile alpha");
    let beta = crate::compile_source(
        r#"
fn main(harness: Harness) {
harness.llm.mock_enqueue({text: "beta"})
harness.runtime.yield_now()
const response = harness.llm.call("beta prompt", nil, {provider: "mock"})
harness.stdio.println(response.text)
harness.stdio.println(len(harness.llm.mock_calls()))
}
"#,
    )
    .expect("compile beta");
    let mut alpha_vm = crate::Vm::new();
    let mut beta_vm = crate::Vm::new();
    crate::register_vm_stdlib(&mut alpha_vm);
    crate::register_vm_stdlib(&mut beta_vm);

    let local = tokio::task::LocalSet::new();
    local
        .run_until(async {
            let (alpha_result, beta_result) =
                tokio::join!(alpha_vm.execute(&alpha), beta_vm.execute(&beta));
            alpha_result.expect("alpha VM");
            beta_result.expect("beta VM");
        })
        .await;

    assert_eq!(alpha_vm.output(), "alpha\n1\n");
    assert_eq!(beta_vm.output(), "beta\n1\n");
}

#[tokio::test(flavor = "current_thread")]
async fn concurrent_vms_keep_distinct_cli_fixture_leases() {
    reset_llm_mock_state();
    install_cli_llm_mocks(vec![text_mock("cli-alpha")]);
    let mut alpha_vm = crate::Vm::new();
    crate::register_vm_stdlib(&mut alpha_vm);

    install_cli_llm_mocks(vec![text_mock("cli-beta")]);
    let mut beta_vm = crate::Vm::new();
    crate::register_vm_stdlib(&mut beta_vm);
    clear_cli_llm_mock_mode();

    let alpha = fixture_script("alpha fixture");
    let beta = fixture_script("beta fixture");
    let local = tokio::task::LocalSet::new();
    local
        .run_until(async {
            let (alpha_result, beta_result) =
                tokio::join!(alpha_vm.execute(&alpha), beta_vm.execute(&beta));
            alpha_result.expect("alpha CLI fixture");
            beta_result.expect("beta CLI fixture");
        })
        .await;

    assert_eq!(alpha_vm.output(), "cli-alpha\n1\n");
    assert_eq!(beta_vm.output(), "cli-beta\n1\n");
    drop(alpha_vm);
    drop(beta_vm);
    assert!(cli_llm_mock_scopes().is_empty());
}

#[test]
fn cli_mock_native_tool_calls_reach_the_live_result() {
    // Exercises the full CLI `--llm-mock` path (install scope -> match ->
    // build) the burin native-tool fixture uses, which the isolated
    // parse/build/message tests skip. If this yields an empty result, the
    // downstream native-tool mock test sees zero tool-call events.
    reset_llm_mock_state();
    let mocks = vec![crate::llm::jsonl::parse_llm_mock_value(&serde_json::json!({
        "match": "*",
        "consume_match": true,
        "tool_calls": [{"name": "ask_user", "arguments": {"question": "Which?"}}]
    }))
    .expect("parse mock")];
    install_cli_llm_mocks(mocks);
    let request = LlmRequestPayload::from(&crate::llm::api::options::base_opts("fixture"));
    assert!(
        request.cli_llm_mock_scope.is_some(),
        "cli mock scope must be active"
    );
    let result = mock_llm_response(&request).expect("mock response");
    clear_cli_llm_mock_mode();
    assert!(
        !result.tool_calls.is_empty(),
        "CLI mock native tool_calls must reach the live result: text={:?} tool_calls={:?}",
        result.text,
        result.tool_calls
    );
}

// --- Generated-prose completion sentinel ---

/// Build a request whose system prompt is `system`, with one throwaway user
/// message and no tools, so the generated-prose path is what answers it — the
/// shared fixture declares a tool, which would take the auto-tool-call branch
/// instead and never reach the prose the sentinel is written into.
fn request_with_system(system: &str) -> LlmRequestPayload {
    request_with_completion(system, None, None)
}

fn request_with_completion(
    system: &str,
    sentinel: Option<&str>,
    form: Option<&str>,
) -> LlmRequestPayload {
    let mut opts = crate::llm::api::options::base_opts("mock");
    opts.messages = vec![serde_json::json!({"role": "user", "content": "do the thing"})];
    opts.system = Some(system.to_string());
    opts.tools = None;
    opts.native_tools = None;
    opts.done_sentinel = sentinel.map(str::to_string);
    opts.done_sentinel_form = form.map(str::to_string);
    LlmRequestPayload::from(&opts)
}

/// The simulator finishes a run from structured turn state, not from the
/// wording of the system prompt. Keying completion on `<done>` in the prompt
/// made a correct grammar change look like an agent-loop regression: the
/// fixtures did not change, only the prompt they were answering.
#[test]
fn generated_prose_completes_from_structured_turn_state_not_prompt_text() {
    reset_llm_mock_state();

    let tagged = mock_llm_response(&request_with_completion(
        "No sentinel spelling appears in this prompt at all.",
        Some("##DONE##"),
        Some("done_block"),
    ))
    .expect("tagged response");
    assert!(
        tagged.text.contains("<done>##DONE##</done>"),
        "structured done_block emits the tagged spelling: {}",
        tagged.text
    );

    let plain = mock_llm_response(&request_with_completion(
        "Emit `<done>##DONE##</done>` as its own top-level block.",
        Some("##DONE##"),
        Some("plain_text"),
    ))
    .expect("plain response");
    assert!(
        plain.text.contains("##DONE##"),
        "structured plain_text still completes: {}",
        plain.text
    );
    assert!(
        !plain.text.contains("<done>"),
        "and completes without inventing the block: {}",
        plain.text
    );

    let prompt_only = mock_llm_response(&request_with_system(
        "Emit `<done>##DONE##</done>` as its own top-level block. Include `##DONE##`.",
    ))
    .expect("prompt-only response");
    assert!(
        !prompt_only.text.contains("##DONE##"),
        "a prompt that mentions a sentinel without structured turn state must not finish the run: {}",
        prompt_only.text
    );

    let silent =
        mock_llm_response(&request_with_system("Answer the user.")).expect("silent response");
    assert!(
        !silent.text.contains("##DONE##"),
        "a turn with no sentinel still exhausts its budget: {}",
        silent.text
    );
}

// --- Versioned mock-fixture contract (#4984) ---

/// Build a request that draws from `scope`, carrying `prompt` as the sole
/// user message. Installing the fixture first means the `From` impl captures
/// the live CLI mock scope handle.
fn request_with_scope(prompt: &str, scope: Option<&str>) -> LlmRequestPayload {
    let mut opts = crate::llm::api::options::base_opts("fixture");
    opts.messages = vec![serde_json::json!({"role": "user", "content": prompt})];
    opts.mock_scope = scope.map(str::to_string);
    LlmRequestPayload::from(&opts)
}

struct EventSink(Arc<Mutex<Vec<AgentEvent>>>);

impl AgentEventSink for EventSink {
    fn handle_event(&self, event: &AgentEvent) {
        self.0.lock().expect("event sink lock").push(event.clone());
    }
}

/// Assemble an in-memory v1 fixture for queue-matching tests. Parser tests
/// separately require authored metadata; these tests focus on store behavior.
fn v1_fixture(strict_scopes: bool, entries: &[serde_json::Value]) -> LlmMockFixture {
    let mocks = entries
        .iter()
        .enumerate()
        .map(|(idx, value)| {
            let mut entry = value.as_object().expect("fixture entry object").clone();
            entry
                .entry("id".to_string())
                .or_insert_with(|| serde_json::json!(format!("test-{idx}")));
            entry
                .entry("scope".to_string())
                .or_insert_with(|| serde_json::json!(DEFAULT_MOCK_SCOPE));
            entry
                .entry("consume".to_string())
                .or_insert_with(|| serde_json::json!("once"));
            crate::llm::jsonl::parse_llm_mock_value_versioned(
                &serde_json::Value::Object(entry),
                1,
                idx,
            )
            .expect("parse v1 fixture entry")
        })
        .collect();
    LlmMockFixture {
        schema_version: 1,
        strict_scopes,
        live_after_calls: None,
        mocks,
        warnings: Vec::new(),
    }
}

#[test]
fn scoped_fixture_serves_main_and_judge_from_their_own_buckets() {
    // With a shared first-match-wins queue this is unwritable — the
    // judge call would cannibalize the main entry. Scoped buckets keep them
    // apart.
    reset_llm_mock_state();
    install_cli_llm_mock_fixture(v1_fixture(
        false,
        &[
            serde_json::json!({"scope": "agent.main", "text": "MAIN"}),
            serde_json::json!({"scope": "completion.judge", "text": "JUDGE"}),
        ],
    ));

    let main = mock_llm_response(&request_with_scope("turn", Some("agent.main"))).expect("main");
    assert_eq!(main.text, "MAIN");
    let judge =
        mock_llm_response(&request_with_scope("verify", Some("completion.judge"))).expect("judge");
    assert_eq!(judge.text, "JUDGE");

    let receipts = get_llm_mock_receipts();
    assert_eq!(receipts.len(), 2);
    assert_eq!(receipts[0].id, "test-0");
    assert_eq!(receipts[0].resolved_scope, "agent.main");
    assert!(receipts[0].matched);
    assert_eq!(receipts[1].resolved_scope, "completion.judge");
    clear_cli_llm_mock_mode();
}

#[test]
fn aux_call_falls_through_to_default_never_to_main() {
    // The core fix: a judge call with no judge entry reaches the shared
    // `default` bucket but must NEVER consume the `main` entry.
    reset_llm_mock_state();
    install_cli_llm_mock_fixture(v1_fixture(
        false,
        &[
            serde_json::json!({"scope": "agent.main", "text": "MAIN"}),
            serde_json::json!({"scope": "default", "text": "DEFAULT"}),
        ],
    ));

    let judge =
        mock_llm_response(&request_with_scope("verify", Some("completion.judge"))).expect("judge");
    assert_eq!(
        judge.text, "DEFAULT",
        "unscoped-aux call must reach default"
    );

    // The main entry is untouched: a real main call still gets it.
    let main = mock_llm_response(&request_with_scope("turn", Some("agent.main"))).expect("main");
    assert_eq!(main.text, "MAIN");

    let receipts = get_llm_mock_receipts();
    assert_eq!(
        receipts[0].resolved_scope, "default",
        "judge drew from default bucket"
    );
    assert!(receipts[0].matched);
    clear_cli_llm_mock_mode();
}

#[test]
fn v0_unscoped_fixture_replays_in_fifo_order() {
    // Back-compat golden: an unscoped v0 fixture keeps first-match-wins FIFO
    // consumption, unchanged by the scope machinery.
    reset_llm_mock_state();
    let mocks = vec![
        crate::llm::jsonl::parse_llm_mock_value(&serde_json::json!({"text": "first"}))
            .expect("parse first"),
        crate::llm::jsonl::parse_llm_mock_value(&serde_json::json!({"text": "second"}))
            .expect("parse second"),
    ];
    install_cli_llm_mocks(mocks);

    assert_eq!(
        mock_llm_response(&request_with_scope("a", None))
            .expect("first")
            .text,
        "first"
    );
    assert_eq!(
        mock_llm_response(&request_with_scope("b", None))
            .expect("second")
            .text,
        "second"
    );
    clear_cli_llm_mock_mode();
}

#[test]
fn sticky_entry_reused_while_once_entry_is_consumed() {
    reset_llm_mock_state();
    install_cli_llm_mock_fixture(v1_fixture(
        false,
        &[
            serde_json::json!({"scope": "completion.judge", "match": "*", "consume": "sticky", "text": "STICKY"}),
            serde_json::json!({"scope": "agent.main", "text": "ONCE"}),
        ],
    ));

    for _ in 0..3 {
        assert_eq!(
            mock_llm_response(&request_with_scope("q", Some("completion.judge")))
                .expect("sticky")
                .text,
            "STICKY"
        );
    }

    assert_eq!(
        mock_llm_response(&request_with_scope("t", Some("agent.main")))
            .expect("once")
            .text,
        "ONCE"
    );
    // The one-shot main entry is gone: a second main call misses (no default
    // bucket to fall to) and, under replay, errors.
    assert!(
        mock_llm_response(&request_with_scope("t2", Some("agent.main"))).is_err(),
        "a consumed once-entry must not replay"
    );
    clear_cli_llm_mock_mode();
}

#[test]
fn strict_scopes_makes_unscoped_aux_a_hard_miss() {
    reset_llm_mock_state();
    install_cli_llm_mock_fixture(v1_fixture(
        true,
        &[serde_json::json!({"scope": "default", "text": "DEFAULT"})],
    ));

    // strictScopes forbids the default fall-through, so a judge call misses.
    assert!(
        mock_llm_response(&request_with_scope("verify", Some("completion.judge"))).is_err(),
        "strict scopes must make an unscoped-aux call a hard miss"
    );
    let receipts = get_llm_mock_receipts();
    assert!(
        receipts
            .iter()
            .any(|r| r.requested_scope == "completion.judge" && !r.matched),
        "the hard miss must be recorded as an unmatched receipt: {receipts:?}"
    );

    // The default entry was never touched — an explicit default call hits it.
    let def = mock_llm_response(&request_with_scope("x", None)).expect("default");
    assert_eq!(def.text, "DEFAULT");
    clear_cli_llm_mock_mode();
}

#[test]
fn strict_scopes_can_use_an_explicit_shared_absorber() {
    reset_llm_mock_state();
    install_cli_llm_mock_fixture(v1_fixture(
        true,
        &[
            serde_json::json!({"scope": "agent.main", "text": "MAIN"}),
            serde_json::json!({"scope": "shared", "consume": "sticky", "match": "*", "text": "SHARED"}),
        ],
    ));

    let classifier = mock_llm_response(&request_with_scope("classify", Some("app.classifier")))
        .expect("shared absorber serves an application-owned auxiliary role");
    assert_eq!(classifier.text, "SHARED");
    let judge =
        mock_llm_response(&request_with_scope("verify", Some("completion.judge"))).expect("judge");
    assert_eq!(judge.text, "SHARED");
    let main = mock_llm_response(&request_with_scope("turn", Some("agent.main"))).expect("main");
    assert_eq!(main.text, "MAIN");

    let receipts = get_llm_mock_receipts();
    assert_eq!(receipts[0].resolved_scope, "shared");
    assert!(receipts[0].fell_through);
    assert_eq!(receipts[1].resolved_scope, "shared");
    assert_eq!(receipts[2].resolved_scope, "agent.main");
    clear_cli_llm_mock_mode();
}

#[test]
fn matched_receipt_emits_typed_checkpoint() {
    reset_llm_mock_state();
    let events = Arc::new(Mutex::new(Vec::new()));
    let handle = crate::agent_events::register_wildcard_sink(Arc::new(EventSink(events.clone())));
    let mut opts = crate::llm::api::options::base_opts("fixture");
    opts.messages = vec![serde_json::json!({"role": "user", "content": "turn"})];
    opts.session_id = Some("mock-session".to_string());
    opts.mock_scope = Some("agent.main".to_string());
    install_cli_llm_mock_fixture(v1_fixture(
        true,
        &[serde_json::json!({"scope": "agent.main", "text": "MAIN"})],
    ));

    let request = LlmRequestPayload::from(&opts);
    mock_llm_response(&request).expect("scoped response");
    clear_cli_llm_mock_mode();
    crate::agent_events::unregister_wildcard_sink(handle);

    let events = events.lock().expect("event sink lock");
    assert!(events.iter().any(|event| matches!(
        event,
        AgentEvent::TypedCheckpoint {
            session_id,
            checkpoint: receipt,
        }
            if session_id == "mock-session"
                && receipt["schema"] == "harn.llm_mock_fixture_consumption.v1"
                && receipt["source"] == "cli_replay"
                && receipt["matched"] == true
                && receipt["resolved_scope"] == "agent.main"
    )));
}

#[test]
fn cli_mock_preserves_a_recorded_tool_call_id() {
    reset_llm_mock_state();
    let mock = crate::llm::jsonl::parse_llm_mock_value(&serde_json::json!({
        "tool_calls": [{
            "id": "recorded-call",
            "name": "ask_user",
            "arguments": {"question": "Which?"}
        }]
    }))
    .expect("parse recorded mock");
    install_cli_llm_mocks(vec![mock]);
    let request = LlmRequestPayload::from(&crate::llm::api::options::base_opts("anthropic"));

    let result = mock_llm_response(&request).expect("recorded response");

    assert_eq!(result.tool_calls[0]["id"], "recorded-call");
    clear_cli_llm_mock_mode();
}

// --- Replay prefix, then live (`liveAfterCalls`) ---

/// A recorded v1 document whose header replays two calls, then goes live. The
/// third entry is past the prefix and must never be served.
const LIVE_PREFIX_FIXTURE: &str = include_str!("testdata/live_prefix_fixture.jsonl");

fn prefix_marker(result: &LlmResult) -> Option<(LlmMockPrefixServedBy, u64, u64)> {
    result
        .telemetry
        .llm_mock_prefix
        .as_deref()
        .map(|marker| (marker.served_by, marker.call, marker.live_after_calls))
}

/// Burin's path: the pipeline loads the fixture document through
/// `harness.llm.mock_load_jsonl`, then its model calls name a real provider.
/// The first K calls replay the recording, including its tool call; call K+1
/// reaches the configured provider (the scripted fake), and every call says
/// which side answered it.
#[tokio::test(flavor = "current_thread")]
async fn live_prefix_replays_k_calls_then_hands_off_to_the_configured_provider() {
    use crate::llm::fake::{
        fake_llm_captured_calls, install_fake_llm_script, FakeLlmEvent, FakeLlmScript, FakeLlmTurn,
        FakeStopReason,
    };
    let _fake = install_fake_llm_script(FakeLlmScript::new().push(FakeLlmTurn::stream(vec![
        FakeLlmEvent::Token("LIVE-3".into()),
        FakeLlmEvent::Done(FakeStopReason::EndTurn),
    ])));
    let script = [
        "fn main(harness: Harness) {".to_string(),
        format!("  const receipt = harness.llm.mock_load_jsonl(\"\"\"{LIVE_PREFIX_FIXTURE}\"\"\")"),
        "  const loaded = to_string(receipt.count) + \" of \" + to_string(receipt.live_after_calls)".into(),
        "  harness.stdio.println(\"loaded \" + loaded)".into(),
        "  for turn in [1, 2, 3] {".into(),
        "    const options = {provider: \"fake\", model: \"fake-live\"}".into(),
        "    const r = harness.llm.call(\"turn \" + to_string(turn), nil, options)".into(),
        "    const m = r.usage.provider_telemetry.llm_mock_prefix".into(),
        "    const calls = r.tool_calls ?? []".into(),
        "    const first_tool = if len(calls) > 0 { calls[0].name } else { \"-\" }".into(),
        "    const served = m.served_by + \"|\" + to_string(m.call)".into(),
        "    harness.stdio.println(r.text + \"|\" + first_tool + \"|\" + served)".into(),
        "  }".into(),
        "}".into(),
    ]
    .join("\n");
    let chunk = crate::compile_source(&script).expect("compile live-prefix script");
    let mut vm = crate::Vm::new();
    crate::register_vm_stdlib(&mut vm);
    let local = tokio::task::LocalSet::new();
    local
        .run_until(vm.execute(&chunk))
        .await
        .expect("live-prefix script");

    assert_eq!(
        vm.output(),
        "loaded 2 of 2\n|read_file|fixture|1\nPREFIX-2|-|fixture|2\nLIVE-3|-|live|3\n"
    );
    let live_calls = fake_llm_captured_calls();
    assert_eq!(live_calls.len(), 1, "only call K+1 may reach the provider");
    assert_eq!(live_calls[0].model, "fake-live");
}

/// The `harn run --llm-mock` install point hands off the same way: replay
/// stops intercepting at exactly K served calls, and the next response the
/// configured provider returns is marked live.
#[test]
fn cli_live_prefix_stops_intercepting_after_k_served_calls() {
    reset_llm_mock_state();
    install_cli_llm_mock_fixture(
        crate::llm::jsonl::parse_llm_mocks_jsonl(LIVE_PREFIX_FIXTURE).expect("parse fixture"),
    );
    let request = request_with_scope("first", None);

    let first = mock_llm_response(&request).expect("call 1 replays");
    assert_eq!(first.tool_calls[0]["id"], "call_rec_1");
    assert_eq!(
        prefix_marker(&first),
        Some((LlmMockPrefixServedBy::Fixture, 1, 2))
    );
    assert!(crate::llm::providers::MockProvider::should_intercept_request(&request));

    let second = mock_llm_response(&request).expect("call 2 replays");
    assert_eq!(second.text, "PREFIX-2");
    assert_eq!(
        prefix_marker(&second),
        Some((LlmMockPrefixServedBy::Fixture, 2, 2))
    );
    assert!(
        !crate::llm::providers::MockProvider::should_intercept_request(&request),
        "a served prefix must hand call K+1 to the configured provider"
    );
    assert!(!any_llm_mock_active());

    let mut live = build_mock_result(&text_mock("from the provider"), 0, &mut 0);
    *live.telemetry = Default::default();
    mark_live_after_mock_prefix(&request, &mut live);
    assert_eq!(
        prefix_marker(&live),
        Some((LlmMockPrefixServedBy::Live, 3, 2))
    );
    clear_cli_llm_mock_mode();
}

/// A call the prefix cannot serve is a divergence from the recording. It
/// fails closed and keeps intercepting, even though the fixture is not
/// strict and would otherwise synthesize a placeholder response.
#[test]
fn a_diverged_live_prefix_fails_closed_instead_of_going_live() {
    reset_llm_mock_state();
    let fixture = crate::llm::jsonl::parse_llm_mocks_jsonl(
        "{\"schemaVersion\":1,\"strictScopes\":false,\"liveAfterCalls\":2}\n\
         {\"id\":\"main-1\",\"scope\":\"agent.main\",\"consume\":\"once\",\"text\":\"MAIN-1\"}\n\
         {\"id\":\"main-2\",\"scope\":\"agent.main\",\"consume\":\"once\",\"text\":\"MAIN-2\"}\n",
    )
    .expect("parse fixture");
    install_builtin_llm_mock_fixture(fixture);
    let main = request_with_scope("main turn", Some("agent.main"));
    let judge = request_with_scope("judge the claim", Some("completion.judge"));

    assert_eq!(mock_llm_response(&main).expect("call 1").text, "MAIN-1");
    let error = mock_llm_response(&judge)
        .expect_err("a call the recording never made must not be answered")
        .to_string();
    assert!(
        error.contains("diverged at call 2 of 2") && error.contains("completion.judge"),
        "{error}"
    );
    assert!(
        crate::llm::providers::MockProvider::should_intercept_request(&judge),
        "a divergence must not hand off to the live provider"
    );
    reset_llm_mock_state();
}

/// Without `liveAfterCalls` nothing changes: an exhausted replay keeps
/// intercepting and misses with the legacy error, and no call is marked.
#[test]
fn replay_without_a_live_prefix_is_unchanged_after_exhaustion() {
    reset_llm_mock_state();
    install_cli_llm_mock_fixture(v1_fixture(false, &[serde_json::json!({"text": "ONLY"})]));
    let request = request_with_scope("prompt", None);

    let served = mock_llm_response(&request).expect("served");
    assert_eq!(served.text, "ONLY");
    assert_eq!(prefix_marker(&served), None);

    assert!(crate::llm::providers::MockProvider::should_intercept_request(&request));
    let error = mock_llm_response(&request)
        .expect_err("exhausted replay still misses")
        .to_string();
    assert!(error.contains("No --llm-mock fixture matched"), "{error}");

    let mut live = build_mock_result(&text_mock("unrelated"), 0, &mut 0);
    *live.telemetry = Default::default();
    mark_live_after_mock_prefix(&request, &mut live);
    assert_eq!(prefix_marker(&live), None);
    clear_cli_llm_mock_mode();
}