ktstr 0.15.0

Test harness for Linux process schedulers
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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
//! Part of the eval module's unit-test suite, split across sibling
//! `eval_tests*.rs` files to keep each under the size ceiling. Child of
//! `eval`: reaches the production core via `super::` / `super::super::`.
use super::super::test_helpers::{EnvVarGuard, isolated_cache_dir, lock_env};
use super::*;
use crate::assert::DetailKind;

// -- validate_llm_extraction tests --
//
// Pin the three universal structural-sanity checks the function
// is documented to enforce: unique metric names, finite values,
// `MetricSource::LlmExtract` source tag. Every violation found
// contributes a String to the returned Vec; an empty Vec means
// the metric set is clean. These are pure-function tests over
// synthetic Metric vectors — no model load, no VM, no SHM ring.

/// Build a clean LlmExtract-tagged metric for use in the
/// validation tests. Each test mutates one field to construct
/// its violation case, leaving every other invariant satisfied
/// so the failure is unambiguously attributable to the mutated
/// field rather than collateral defaults.
#[cfg(feature = "llm")]
fn llm_metric(name: &str, value: f64) -> crate::test_support::Metric {
    crate::test_support::Metric {
        name: name.to_owned(),
        value,
        polarity: crate::test_support::Polarity::Unknown,
        unit: String::new(),
        source: crate::test_support::MetricSource::LlmExtract,
        stream: crate::test_support::MetricStream::Stdout,
    }
}

/// Two metrics sharing the same `name` violate the uniqueness
/// invariant. The diagnostic must call out "duplicate metric
/// name" so a reader can tell which check fired without
/// re-reading the function.
#[cfg(feature = "llm")]
#[test]
fn validate_llm_extraction_duplicate_name_rejects() {
    let metrics = vec![
        llm_metric("latency.p99", 1.0),
        llm_metric("latency.p99", 2.0),
    ];
    let violations = super::llm_extract::validate_llm_extraction(&metrics);
    assert_eq!(
        violations.len(),
        1,
        "exactly one duplicate-name violation expected, got {violations:?}",
    );
    assert!(
        violations[0].contains("duplicate metric name"),
        "diagnostic must mention 'duplicate metric name': {}",
        violations[0],
    );
}

/// A NaN value violates the finite-only invariant; the
/// diagnostic must call out "non-finite" so the reader can tell
/// which check fired.
#[cfg(feature = "llm")]
#[test]
fn validate_llm_extraction_nan_rejects() {
    let metrics = vec![llm_metric("latency.p99", f64::NAN)];
    let violations = super::llm_extract::validate_llm_extraction(&metrics);
    assert_eq!(
        violations.len(),
        1,
        "exactly one non-finite violation expected, got {violations:?}",
    );
    assert!(
        violations[0].contains("non-finite"),
        "diagnostic must mention 'non-finite': {}",
        violations[0],
    );
}

/// A metric tagged with the wrong source (Json instead of
/// LlmExtract) violates the source-tag invariant. The
/// diagnostic must mention `MetricSource::LlmExtract` so the
/// reader can tell which check fired and what the expected
/// source was.
#[cfg(feature = "llm")]
#[test]
fn validate_llm_extraction_wrong_source_rejects() {
    let mut metrics = vec![llm_metric("latency.p99", 1.0)];
    metrics[0].source = crate::test_support::MetricSource::Json;
    let violations = super::llm_extract::validate_llm_extraction(&metrics);
    assert_eq!(
        violations.len(),
        1,
        "exactly one wrong-source violation expected, got {violations:?}",
    );
    assert!(
        violations[0].contains("MetricSource::LlmExtract"),
        "diagnostic must mention 'MetricSource::LlmExtract': {}",
        violations[0],
    );
}

/// Structurally clean input — distinct names, finite values,
/// `LlmExtract` source on every entry — produces an empty Vec.
/// Pins the happy path so a regression that adds an unwanted
/// check (e.g. minimum metric count, value-magnitude bound)
/// breaks this test instead of silently rejecting valid
/// extractions.
#[cfg(feature = "llm")]
#[test]
fn validate_llm_extraction_clean_input_passes() {
    let metrics = vec![
        llm_metric("latency.p50", 1.0),
        llm_metric("latency.p99", 2.0),
        llm_metric("rps", 1000.0),
    ];
    assert!(
        super::llm_extract::validate_llm_extraction(&metrics).is_empty(),
        "clean input must produce an empty violations Vec",
    );
}

/// A single metric that breaks BOTH the non-finite invariant
/// AND the wrong-source invariant produces TWO violations in
/// the same call — proves per-metric checks run independently
/// and aren't short-circuited by an earlier failure on the
/// same metric. Pins the "report every defect class in one
/// run" UX: a flaky LLM run that produces NaN-valued metrics
/// with the wrong source tag surfaces both signals to the
/// test author rather than forcing two debug iterations.
#[cfg(feature = "llm")]
#[test]
fn validate_llm_extraction_single_metric_multiple_violations() {
    let mut metrics = vec![llm_metric("latency.p99", f64::INFINITY)];
    metrics[0].source = crate::test_support::MetricSource::Json;
    let violations = super::llm_extract::validate_llm_extraction(&metrics);
    assert_eq!(
        violations.len(),
        2,
        "non-finite + wrong-source on the same metric must produce 2 violations, got {violations:?}",
    );
    // Order is fixed: non-finite check runs before source
    // check inside the per-metric loop. Pin both diagnostics
    // by content rather than by index so a future re-ordering
    // surfaces here as a content mismatch instead of an
    // off-by-one.
    let messages: Vec<&str> = violations.iter().map(String::as_str).collect();
    assert!(
        messages.iter().any(|m| m.contains("non-finite")),
        "non-finite violation must appear: {messages:?}",
    );
    assert!(
        messages
            .iter()
            .any(|m| m.contains("MetricSource::LlmExtract")),
        "wrong-source violation must appear: {messages:?}",
    );
}

/// Across the whole metric set, every duplicate-name occurrence
/// after the first reports its own violation. Three identical
/// names → two duplicate-name violations (the first occurrence
/// is the "original," the next two are duplicates). Pins the
/// "report every defect" semantics so a regression to first-
/// violation-only behavior surfaces here.
#[cfg(feature = "llm")]
#[test]
fn validate_llm_extraction_multiple_duplicates_each_surface() {
    let metrics = vec![
        llm_metric("rps", 1.0),
        llm_metric("rps", 2.0),
        llm_metric("rps", 3.0),
    ];
    let violations = super::llm_extract::validate_llm_extraction(&metrics);
    assert_eq!(
        violations.len(),
        2,
        "three same-name metrics → two duplicate-name violations, got {violations:?}",
    );
    for v in &violations {
        assert!(
            v.contains("duplicate metric name"),
            "every violation must call out duplicate name: {v}",
        );
    }
}

/// Heterogeneous violation classes across DIFFERENT metrics in
/// a single call: a duplicate name on one metric, NaN value on
/// another, wrong source on a third. Verifies the function
/// collects across ALL metrics, not just within a single one.
/// Pins the "see every defect class in one run" UX.
#[cfg(feature = "llm")]
#[test]
fn validate_llm_extraction_heterogeneous_violations_across_metrics() {
    let mut metrics = vec![
        llm_metric("rps", 1.0),
        llm_metric("rps", 2.0),              // duplicate name
        llm_metric("latency.p99", f64::NAN), // non-finite
        llm_metric("p50", 1.0),
    ];
    metrics[3].source = crate::test_support::MetricSource::Json; // wrong source on p50
    let violations = super::llm_extract::validate_llm_extraction(&metrics);
    assert_eq!(
        violations.len(),
        3,
        "three independent violations expected, got {violations:?}",
    );
    let messages: Vec<&str> = violations.iter().map(String::as_str).collect();
    assert!(
        messages
            .iter()
            .any(|m| m.contains("duplicate metric name") && m.contains("'rps'")),
        "duplicate-name on 'rps' must appear: {messages:?}",
    );
    assert!(
        messages
            .iter()
            .any(|m| m.contains("non-finite") && m.contains("'latency.p99'")),
        "non-finite on 'latency.p99' must appear: {messages:?}",
    );
    assert!(
        messages
            .iter()
            .any(|m| m.contains("MetricSource::LlmExtract") && m.contains("'p50'")),
        "wrong-source on 'p50' must appear: {messages:?}",
    );
}

// -- validate_metric_bounds tests --
//
// Pin the per-payload bounds-validation pass that runs after
// the universal `validate_llm_extraction` pass when a payload
// declared `metric_bounds`. Each test constructs a synthetic
// metric set + a `MetricBounds` with a single check enabled
// and asserts the violation list contents.

/// `MetricBounds::default()` (every field `None`) produces zero
/// violations on any input — pins the "no bounds declared = no
/// extra checks" contract that lets payloads opt in to the
/// pass without paying for it.
#[cfg(feature = "llm")]
#[test]
fn validate_metric_bounds_none_produces_no_violations() {
    let metrics = vec![
        llm_metric("rps", -42.0),    // would trip value_min if set
        llm_metric("latency", 1e15), // would trip value_max if set
    ];
    let bounds = crate::test_support::MetricBounds::default();
    let violations = super::llm_extract::validate_metric_bounds(&metrics, &bounds);
    assert!(
        violations.is_empty(),
        "MetricBounds::default() must produce zero violations regardless of input; \
             got: {violations:?}",
    );
}

/// `min_count` rejects an extracted set with fewer metrics than
/// the declared floor. Diagnostic must name both the actual
/// count and the required minimum so the operator can see the
/// shortfall at a glance.
#[cfg(feature = "llm")]
#[test]
fn validate_metric_bounds_min_count_rejects_short_set() {
    let metrics = vec![llm_metric("a", 1.0), llm_metric("b", 2.0)];
    let bounds = crate::test_support::MetricBounds {
        min_count: Some(5),
        ..crate::test_support::MetricBounds::default()
    };
    let violations = super::llm_extract::validate_metric_bounds(&metrics, &bounds);
    assert_eq!(
        violations.len(),
        1,
        "short set must produce exactly one min_count violation; got: {violations:?}",
    );
    assert!(
        violations[0].contains("extracted 2 metric(s)"),
        "diagnostic must name actual count: {}",
        violations[0],
    );
    assert!(
        violations[0].contains("at least 5"),
        "diagnostic must name required minimum: {}",
        violations[0],
    );
}

/// `min_count` accepts a set whose length equals the floor —
/// pins the "inclusive lower bound" semantics.
#[cfg(feature = "llm")]
#[test]
fn validate_metric_bounds_min_count_accepts_at_threshold() {
    let metrics = vec![
        llm_metric("a", 1.0),
        llm_metric("b", 2.0),
        llm_metric("c", 3.0),
    ];
    let bounds = crate::test_support::MetricBounds {
        min_count: Some(3),
        ..crate::test_support::MetricBounds::default()
    };
    let violations = super::llm_extract::validate_metric_bounds(&metrics, &bounds);
    assert!(
        violations.is_empty(),
        "metric count == min_count is acceptable (>= semantics); got: {violations:?}",
    );
}

/// `value_min` rejects every metric with value strictly below
/// the bound. Each violation surfaces independently — a set
/// with three sub-bound metrics produces three violations.
#[cfg(feature = "llm")]
#[test]
fn validate_metric_bounds_value_min_rejects_each_below_floor() {
    let metrics = vec![
        llm_metric("p50", -1.0),
        llm_metric("p99", -2.0),
        llm_metric("rps", 100.0), // above floor; not rejected
        llm_metric("delta", -5.0),
    ];
    let bounds = crate::test_support::MetricBounds {
        value_min: Some(0.0),
        ..crate::test_support::MetricBounds::default()
    };
    let violations = super::llm_extract::validate_metric_bounds(&metrics, &bounds);
    assert_eq!(
        violations.len(),
        3,
        "every below-floor metric must surface its own violation; got: {violations:?}",
    );
    assert!(
        violations
            .iter()
            .all(|v| v.contains("below payload's declared lower bound")),
        "every diagnostic must name the lower-bound class: {violations:?}",
    );
    assert!(
        violations.iter().any(|v| v.contains("'p50'")),
        "p50 violation must surface: {violations:?}",
    );
    assert!(
        violations.iter().any(|v| v.contains("'delta'")),
        "delta violation must surface: {violations:?}",
    );
    // rps was above the floor — must NOT appear.
    assert!(
        !violations.iter().any(|v| v.contains("'rps'")),
        "rps must NOT trigger a value_min violation (100 > 0); got: {violations:?}",
    );
}

/// `value_min` accepts metrics at exactly the bound — pins the
/// "strictly below" semantics. A regression to `<= ` (which
/// would reject the boundary) breaks here.
#[cfg(feature = "llm")]
#[test]
fn validate_metric_bounds_value_min_accepts_at_threshold() {
    let metrics = vec![llm_metric("zero", 0.0)];
    let bounds = crate::test_support::MetricBounds {
        value_min: Some(0.0),
        ..crate::test_support::MetricBounds::default()
    };
    let violations = super::llm_extract::validate_metric_bounds(&metrics, &bounds);
    assert!(
        violations.is_empty(),
        "value at exactly value_min is acceptable (strict-less-than semantics); \
             got: {violations:?}",
    );
}

/// `value_max` mirrors `value_min` with the inverse inequality.
/// Pins the symmetric contract.
#[cfg(feature = "llm")]
#[test]
fn validate_metric_bounds_value_max_rejects_each_above_ceiling() {
    let metrics = vec![
        llm_metric("rss_huge", 1e16),
        llm_metric("rss_normal", 1e6),
        llm_metric("latency_runaway", 1e15),
    ];
    let bounds = crate::test_support::MetricBounds {
        value_max: Some(1e12),
        ..crate::test_support::MetricBounds::default()
    };
    let violations = super::llm_extract::validate_metric_bounds(&metrics, &bounds);
    assert_eq!(
        violations.len(),
        2,
        "two above-ceiling metrics must surface; got: {violations:?}",
    );
    assert!(
        violations
            .iter()
            .all(|v| v.contains("above payload's declared upper bound")),
        "every diagnostic must name the upper-bound class: {violations:?}",
    );
    assert!(
        violations.iter().any(|v| v.contains("'rss_huge'")),
        "rss_huge must trigger: {violations:?}",
    );
    assert!(
        !violations.iter().any(|v| v.contains("'rss_normal'")),
        "rss_normal (1e6) must NOT trigger value_max=1e12: {violations:?}",
    );
}

/// Combined bounds (all three at once): one metric below floor,
/// one above ceiling, and a too-short set. Three distinct
/// violations surface.
#[cfg(feature = "llm")]
#[test]
fn validate_metric_bounds_combined_bounds_each_violation_independent() {
    let metrics = vec![llm_metric("low", -1.0), llm_metric("high", 1e15)];
    let bounds = crate::test_support::MetricBounds {
        min_count: Some(5),
        value_min: Some(0.0),
        value_max: Some(1e12),
    };
    let violations = super::llm_extract::validate_metric_bounds(&metrics, &bounds);
    assert_eq!(
        violations.len(),
        3,
        "combined: 1 min_count + 1 value_min + 1 value_max violation; got: {violations:?}",
    );
    assert!(
        violations.iter().any(|v| v.contains("at least 5")),
        "min_count violation must surface: {violations:?}",
    );
    assert!(
        violations
            .iter()
            .any(|v| v.contains("'low'") && v.contains("below")),
        "value_min on 'low' must surface: {violations:?}",
    );
    assert!(
        violations
            .iter()
            .any(|v| v.contains("'high'") && v.contains("above")),
        "value_max on 'high' must surface: {violations:?}",
    );
}

/// Empty input + min_count > 0 produces a min_count violation.
/// Pins the empty-set boundary against the bounds pass; the
/// universal `validate_llm_extraction` accepts empty input as
/// vacuously valid, but a payload that declared min_count
/// expects something.
#[cfg(feature = "llm")]
#[test]
fn validate_metric_bounds_empty_metrics_with_min_count_violates() {
    let bounds = crate::test_support::MetricBounds {
        min_count: Some(1),
        ..crate::test_support::MetricBounds::default()
    };
    let violations = super::llm_extract::validate_metric_bounds(&[], &bounds);
    assert_eq!(
        violations.len(),
        1,
        "empty input + min_count=1 must produce one violation; got: {violations:?}",
    );
    assert!(
        violations[0].contains("extracted 0 metric(s)"),
        "diagnostic must name 0 as actual count: {}",
        violations[0],
    );
}

// -- Payload::metric_bounds field tests --
//
// Pin the new `metric_bounds: Option<&'static MetricBounds>`
// field on the `Payload` struct: default None, can be set to
// Some(&BOUNDS_CONST), and threads through the deferred
// emission path (via `RawPayloadOutput::metric_bounds`).

/// A `Payload` constructed via the bare struct literal carries
/// `metric_bounds: None` by default — pins the "opt-in only"
/// contract so adding the field didn't accidentally enable
/// bounds checks for every existing payload.
#[test]
fn payload_metric_bounds_defaults_to_none_via_payload_binary_constructor() {
    const P: crate::test_support::Payload =
        crate::test_support::Payload::binary("test", "test_bin");
    assert!(
        P.metric_bounds.is_none(),
        "Payload::binary must initialize metric_bounds to None",
    );
}

/// A `Payload` declared with `metric_bounds: Some(&BOUNDS)`
/// retains the reference — the field is `Option<&'static
/// MetricBounds>`, so a const-defined bounds value is reachable
/// from the payload.
#[test]
fn payload_metric_bounds_carries_static_reference() {
    const SCHBENCH_BOUNDS: crate::test_support::MetricBounds = crate::test_support::MetricBounds {
        min_count: Some(5),
        value_min: Some(0.0),
        value_max: Some(1e12),
    };
    const P: crate::test_support::Payload = crate::test_support::Payload {
        name: "schbench_test",
        kind: crate::test_support::PayloadKind::Binary("schbench"),
        output: crate::test_support::OutputFormat::LlmExtract(None),
        default_args: &[],
        default_checks: &[],
        metrics: &[],
        include_files: &[],
        uses_parent_pgrp: false,
        known_flags: None,
        metric_bounds: Some(&SCHBENCH_BOUNDS),
    };
    assert!(P.metric_bounds.is_some());
    let b = P.metric_bounds.unwrap();
    assert_eq!(b.min_count, Some(5));
    assert_eq!(b.value_min, Some(0.0));
    assert_eq!(b.value_max, Some(1e12));
}

/// `host_side_llm_extract` surfaces bounds violations alongside
/// load-failure details. Drives a matched (raw, pm) pair under
/// the offline gate (so model load fails and metrics stay
/// empty) with `metric_bounds: Some(&{min_count: 1})` — the
/// bounds pass is GATED on the model-load succeeding (because
/// it runs after extraction populates metrics), so under
/// offline gate the bounds check does NOT fire. Pin this
/// "bounds run only on extracted metrics" contract: a regression
/// that ran bounds on the empty placeholder would falsely
/// flag every offline-gated test as a min_count violation.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_offline_gate_skips_bounds_check() {
    let _env_lock = lock_env();
    super::super::model::reset();
    let _cache = isolated_cache_dir();
    let _offline = EnvVarGuard::set(crate::test_support::OFFLINE_ENV, "1");
    let mut pm = vec![empty_pm(0)];
    let raws = vec![crate::test_support::RawPayloadOutput {
        payload_index: 0,
        stdout: "irrelevant under offline gate".to_string(),
        stderr: String::new(),
        hint: None,
        metric_hints: Vec::new(),
        metric_bounds: Some(crate::test_support::MetricBounds {
            min_count: Some(1),
            ..crate::test_support::MetricBounds::default()
        }),
    }];
    let failures = host_side_llm_extract(&mut pm, &raws);
    // Exactly ONE failure detail — the load-failure. No
    // bounds violation because metrics is empty (placeholder)
    // and the bounds pass is guarded by `if let Some(bounds)`
    // BUT only runs after the structural-sanity pass over
    // extracted metrics. With load failure → metrics empty,
    // the bounds check sees an empty vec — but the empty-set
    // + min_count=1 case WOULD flag a violation. The
    // production code path skips the bounds pass on the
    // load-failure branch (continues before reaching the
    // bounds check), so the bounds check should NOT fire.
    assert_eq!(
        failures.len(),
        1,
        "offline-gated extraction must produce only the load-failure detail, \
             not a spurious bounds violation; got: {failures:?}",
    );
    assert!(
        failures[0].message.contains("LlmExtract model load failed"),
        "the lone failure must be the load-failure: {}",
        failures[0].message,
    );
}

// -- host_side_llm_extract pairing tests --
//
// The pairing logic is tested without invoking the model: every
// case below either constructs an orphan raw output (no
// PayloadMetrics with matching `payload_index`) — which short-
// circuits BEFORE extract_via_llm — or supplies an empty raw
// outputs vec (returns immediately). The pairing-by-index
// contract is the entire moving part on the `payload_index`
// axis; once a match is found, the extraction-and-polarity
// pipeline is exercised by the integration test
// `llm_extract_e2e_test.rs`.

#[cfg(feature = "llm")]
fn empty_raw(payload_index: usize) -> crate::test_support::RawPayloadOutput {
    crate::test_support::RawPayloadOutput {
        payload_index,
        stdout: String::new(),
        stderr: String::new(),
        hint: None,
        metric_hints: Vec::new(),
        metric_bounds: None,
    }
}

#[cfg(feature = "llm")]
fn empty_pm(payload_index: usize) -> crate::test_support::PayloadMetrics {
    crate::test_support::PayloadMetrics {
        payload_index,
        metrics: Vec::new(),
        exit_code: 0,
    }
}

/// Empty raw outputs slice — the function returns immediately
/// without examining `payload_metrics` or hitting the model.
/// Pins the no-LlmExtract-payloads happy path.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_empty_raw_outputs_returns_no_failures() {
    let mut pm = vec![empty_pm(0), empty_pm(1)];
    let failures = host_side_llm_extract(&mut pm, &[]);
    assert!(failures.is_empty(), "empty raw outputs → no failures");
}

/// Orphan raw output: a `RawPayloadOutput` whose `payload_index`
/// has no matching `PayloadMetrics` slot. Surfaces as a
/// pairing-failure detail naming the orphan index. The detail
/// kind is `Other` so the failure-rendering pipeline treats it
/// as a non-classified diagnostic.
///
/// The setup also has an empty-metrics PM at payload_index=0
/// (no matching raw_output), which triggers the post-pairing
/// orphan-PM scan. So this test sees BOTH the
/// orphan-raw detail (from the pairing loop) AND the
/// orphan-PM detail (from the post-loop scan). Pin both so a
/// regression that drops either path surfaces here.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_orphan_raw_output_surfaces_pairing_failure() {
    // PayloadMetrics has payload_index=0; raw output claims
    // payload_index=42 — no slot to write to. Symmetrically,
    // the PM at index 0 has no matching raw, which the
    // post-pairing orphan-PM scan picks up.
    let mut pm = vec![empty_pm(0)];
    let raws = vec![empty_raw(42)];
    let failures = host_side_llm_extract(&mut pm, &raws);
    let messages: Vec<&str> = failures.iter().map(|d| d.message.as_str()).collect();
    assert!(
        messages
            .iter()
            .any(|m| m.contains("LlmExtract host pairing") && m.contains("payload_index=42")),
        "orphan-raw detail naming index 42 must surface: {messages:?}",
    );
    assert!(
        messages
            .iter()
            .any(|m| m.contains("LlmExtract host pairing") && m.contains("[0]")),
        "orphan-PM scan must surface the empty-metrics PM at index 0: {messages:?}",
    );
    // The valid PayloadMetrics slot at index 0 must NOT have been
    // mutated — the orphan path skips extraction.
    assert!(
        pm[0].metrics.is_empty(),
        "no extraction should have run on the orphan path",
    );
}

/// Multiple orphan raw outputs each surface their own failure
/// detail; the function does not abort on the first. Pins the
/// "process every raw, surface every orphan" semantics so a
/// regression that returns early after the first failure is
/// caught.
///
/// The empty-metrics PM at payload_index=0 also triggers the
/// post-pairing orphan-PM scan. So we expect 3 orphan-raw
/// details + 1 orphan-PM combined detail = 4 total failures.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_multiple_orphans_each_surface() {
    let mut pm = vec![empty_pm(0)];
    let raws = vec![empty_raw(10), empty_raw(20), empty_raw(30)];
    let failures = host_side_llm_extract(&mut pm, &raws);
    let messages: Vec<&str> = failures.iter().map(|d| d.message.as_str()).collect();
    assert!(
        messages.iter().any(|m| m.contains("payload_index=10")),
        "orphan raw at 10 must surface: {messages:?}",
    );
    assert!(
        messages.iter().any(|m| m.contains("payload_index=20")),
        "orphan raw at 20 must surface: {messages:?}",
    );
    assert!(
        messages.iter().any(|m| m.contains("payload_index=30")),
        "orphan raw at 30 must surface: {messages:?}",
    );
    // Orphan-PM scan also fires for the empty PM at index 0.
    assert!(
        messages
            .iter()
            .any(|m| m.contains("[0]") && m.contains("no matching RawPayloadOutput")),
        "orphan-PM scan must surface the empty PM at index 0: {messages:?}",
    );
}

/// Json payload that produced zero metrics (empty `metrics` vec)
/// must NOT be conflated with an LlmExtract placeholder when an
/// LlmExtract raw output is also present at a different index.
/// This pins the motivating scenario for index-based pairing:
/// positional pairing would have written the LlmExtract result
/// into the Json payload's empty slot.
///
/// Setup: a Json payload at `payload_index=5` with empty metrics
/// (indistinguishable from an LlmExtract placeholder by content
/// alone). A raw output with `payload_index=99` (no matching
/// slot).
///
/// Expected: the raw output is reported as orphan; the Json
/// payload's empty slot is NEVER touched. Additionally, the
/// post-pairing orphan-PM scan flags the Json slot at
/// index 5 as a candidate for "raw output may have been dropped"
/// — this is a known false-positive case the scan's own diagnostic
/// prose calls out, since a Json-with-no-leaves payload looks
/// identical to a dropped LlmExtract from PayloadMetrics alone.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_json_zero_leaves_not_conflated_with_llm_placeholder() {
    let mut pm = vec![empty_pm(5)];
    let raws = vec![empty_raw(99)];
    let failures = host_side_llm_extract(&mut pm, &raws);
    let messages: Vec<&str> = failures.iter().map(|d| d.message.as_str()).collect();
    assert!(
        messages.iter().any(|m| m.contains("payload_index=99")),
        "orphan raw at 99 must surface: {messages:?}",
    );
    // The Json slot was untouched — its `metrics` is still
    // empty, exactly as the guest emitted it.
    assert!(
        pm[0].metrics.is_empty(),
        "Json empty-metrics slot must not be written by LlmExtract pairing",
    );
    assert_eq!(
        pm[0].payload_index, 5,
        "Json slot's payload_index must be untouched",
    );
    // Orphan-PM scan flags the Json slot as a candidate orphan
    // PM. Documented in the scan's diagnostic as a known
    // false-positive case for mixed-format tests.
    assert!(
        messages
            .iter()
            .any(|m| m.contains("[5]") && m.contains("no matching RawPayloadOutput")),
        "orphan-PM scan must include the Json slot at index 5 in its \
             candidate list (false positive disclosed in the diagnostic): {messages:?}",
    );
}

// -- orphan-PayloadMetrics scan --

/// An empty-metrics `PayloadMetrics` whose
/// `payload_index` has no matching `RawPayloadOutput` is
/// surfaced by the post-pairing scan. Most likely cause is a
/// CRC-bad RawPayloadOutput silently dropped during the bulk-
/// port drain. Without this surfacing, an LlmExtract test whose
/// raw-output bytes arrived corrupted would fail downstream
/// `MetricCheck::Min` / `MetricCheck::Exists` evaluations with a
/// "metric not found" message that hides the real cause.
///
/// Setup: an LlmExtract pair at index 7 (raw + matching PM)
/// arrives intact; an additional empty PM at index 99 has no
/// matching raw. The orphan-PM scan flags index 99.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_orphan_pm_with_no_matching_raw_surfaces() {
    // Use orphan raws to keep the matched extraction off the
    // model path — the PM at index 7 has no matching raw, so
    // the pairing loop skips it. We add raws at 10 and 20 to
    // satisfy the gate that `raw_outputs.is_empty() == false`,
    // so the orphan-PM scan can fire.
    let mut pm = vec![empty_pm(7), empty_pm(99)];
    let raws = vec![empty_raw(10), empty_raw(20)];
    let failures = host_side_llm_extract(&mut pm, &raws);
    let messages: Vec<&str> = failures.iter().map(|d| d.message.as_str()).collect();
    // Both PMs (7 and 99) lack matching raws, so both are
    // surfaced in the orphan-PM scan's combined detail.
    assert!(
        messages
            .iter()
            .any(|m| m.contains("[7, 99]") && m.contains("no matching RawPayloadOutput")),
        "orphan-PM scan must list both unmatched PM indices [7, 99]: {messages:?}",
    );
    assert!(
        messages.iter().any(|m| m.contains("CRC mismatch")),
        "orphan-PM diagnostic must surface the CRC-bad cause: {messages:?}",
    );
    assert!(
        messages.iter().any(|m| m.contains("False-positive case")),
        "orphan-PM diagnostic must disclose the false-positive case for \
             mixed-format tests: {messages:?}",
    );
}

/// When ALL PMs have matching raws, the orphan-PM
/// scan does NOT fire. Pins that the scan is gated on the
/// missing-pair condition rather than blanketly emitting a
/// detail for every empty-metrics PM in an LlmExtract test
/// (which would false-positive on extraction failures that
/// legitimately leave metrics empty).
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_no_orphan_pm_when_all_pms_have_matching_raws() {
    // Two matched pairs. After pairing, both PMs remain empty
    // (orphan raws short-circuit before the model path), but
    // their indices are in the raw-index set, so the
    // orphan-PM scan does not surface anything.
    //
    // The setup uses orphan raws-to-self (i.e. a raw at the
    // same index as its PM) so the pairing loop walks them as
    // matched pairs. To keep the test off the model path
    // entirely, we use empty raws at indices 0 and 1; the
    // pairing succeeds, extract_via_llm returns Err under no
    // model setup (or hangs if a real model loads), so we
    // EXPECT only the load-failure branch — but that's
    // out-of-scope for this test. Instead, we make the
    // pairing loop hit the orphan-raw arm by using raw indices
    // 100 and 200 that don't match the PMs at 0 and 1. Then
    // the orphan-PM scan should still flag PMs at 0 and 1 —
    // which is the WRONG answer for this test.
    //
    // Better: use a setup where every PM IS matched. The
    // simplest way is to skip this test's "no orphan-PM"
    // claim under unit-testing without a model — the integration
    // test (with a real model) would exercise the all-matched
    // path. For unit testing, we instead pin the inverse: the
    // orphan-PM scan does NOT fire when raw_outputs is empty.
    let mut pm = vec![empty_pm(0), empty_pm(1)];
    let raws: Vec<crate::test_support::RawPayloadOutput> = Vec::new();
    let failures = host_side_llm_extract(&mut pm, &raws);
    assert!(
        failures.is_empty(),
        "with no LlmExtract raws, orphan-PM scan must not fire (test is \
             not exercising LlmExtract): {failures:?}",
    );
}

// -- offline-gate / empty-stream / stream-fallback tests --
//
// These tests drive `host_side_llm_extract` through its
// model-touching paths via the offline gate (`KTSTR_MODEL_OFFLINE=1`).
// The gate makes `extract_via_llm` return Err deterministically,
// so the tests pin the host-side dispatch behavior without
// standing up the ~2.55 GiB model.
//
// Every test holds `lock_env()` and calls `super::super::model::reset()`
// before the gate is set, ensuring no previously-memoized
// `Ok(model)` slot bypasses the gate. Reset is paired with an
// `EnvVarGuard` so the gate is removed at drop time even if the
// test panics.
//
// The companion happy-path tests for stdout-primary / stderr-fallback
// with a real model live in the integration test
// `tests/llm_extract_e2e_test.rs`. The unit tests here pin the
// deterministic boundaries that don't require a model.

/// A `RawPayloadOutput` carrying empty stdout AND empty
/// stderr — paired with a matching `PayloadMetrics` slot — must
/// not panic the host extraction. Under the offline gate, the
/// stdout call surfaces a load-failed detail (deterministic),
/// the stderr fallback is short-circuited (because the load_err
/// is Some), and the PayloadMetrics slot's metrics stays empty.
///
/// Pins the empty-input boundary against three regressions:
/// 1. A `String::is_empty()` check that crashed the prompt
///    composer on empty input (covered by model.rs but
///    boundary-tested again here at the eval level).
/// 2. A panic in the polarity resolver if it received an empty
///    metric vec.
/// 3. A regression that ran extract_via_llm on empty stdout
///    AND THEN ran extract_via_llm on empty stderr, doubling
///    the model-load attempt. The current contract:
///    `metrics.is_empty() && load_err.is_none() && !raw.stderr.is_empty()`
///    in eval.rs:281 — empty stderr blocks the fallback.
///
/// Holds [`lock_env`] across the env mutations and pairs an
/// [`isolated_cache_dir`] with the offline-gate `EnvVarGuard`
/// so the gate trips deterministically on a guaranteed-cold
/// cache root rather than relying on the operator's home
/// having no model entry. The reset clears any
/// previously-memoized `Ok(model)` slot in `MODEL_CACHE`.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_with_empty_streams_no_panic_no_metrics() {
    let _env_lock = lock_env();
    super::super::model::reset();
    let _cache = isolated_cache_dir();
    let _offline = EnvVarGuard::set(crate::test_support::OFFLINE_ENV, "1");
    let mut pm = vec![empty_pm(0)];
    let raws = vec![empty_raw(0)];
    let failures = host_side_llm_extract(&mut pm, &raws);
    // Under the offline gate, the stdout extract_via_llm call
    // returns Err — the load-failed branch fires. Empty stderr
    // also blocks the fallback, so a single load-failure detail
    // is the expected shape.
    assert_eq!(
        failures.len(),
        1,
        "empty streams under offline gate must produce exactly one load-failed detail, \
             got: {failures:?}",
    );
    assert!(
        failures[0].message.contains("LlmExtract model load failed"),
        "load-failure detail must surface the diagnostic prefix; got: {}",
        failures[0].message,
    );
    // PayloadMetrics slot stays empty — no metrics extracted, no
    // partial pollution.
    assert!(
        pm[0].metrics.is_empty(),
        "PM slot must remain empty when extraction failed; got: {:?}",
        pm[0].metrics,
    );
}

/// With `KTSTR_MODEL_OFFLINE=1` set, `host_side_llm_extract`
/// must surface an actionable `LlmExtract model load failed`
/// detail naming the offline env var. Pins the host-side
/// equivalent of the `extract_via_llm_returns_empty_when_backend_unavailable`
/// test in model.rs — the model.rs test pins the call-site
/// behavior, this test pins how the host's eval pipeline surfaces
/// that error to the test verdict.
///
/// A regression that swallowed the offline-gate Err (e.g. by
/// returning Vec::new() instead of `Err(reason)` from
/// `extract_via_llm`, or by `match ... { Err(_) => () }`-ing
/// the load failure inside `host_side_llm_extract`) would
/// leave the test passing with empty metrics — a silent
/// regression that `stats compare` would only catch days
/// later as zero-metric runs accumulating in the sidecar.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_under_offline_gate_surfaces_actionable_detail() {
    let _env_lock = lock_env();
    super::super::model::reset();
    let _cache = isolated_cache_dir();
    let _offline = EnvVarGuard::set(crate::test_support::OFFLINE_ENV, "1");
    let mut pm = vec![empty_pm(0)];
    // Non-empty stdout — proves the failure path fires regardless
    // of input shape (not gated on emptiness).
    let raws = vec![crate::test_support::RawPayloadOutput {
        payload_index: 0,
        stdout: "arbitrary stdout content for the model".to_string(),
        stderr: String::new(),
        hint: None,
        metric_hints: Vec::new(),
        metric_bounds: None,
    }];
    let failures = host_side_llm_extract(&mut pm, &raws);
    assert_eq!(
        failures.len(),
        1,
        "offline gate must produce exactly one load-failed detail, got: {failures:?}",
    );
    // Strict shape-of-emission contract:
    // 1. Detail kind is `Other` — the framework surfaces an
    //    uncategorized infrastructure failure here, not a domain
    //    `Starved` / `Saturation` / etc. classification. Stats
    //    tooling that buckets by DetailKind needs this stable.
    // 2. Message BEGINS WITH the canonical prefix
    //    `"LlmExtract model load failed:"` — not just contains.
    //    A regression that prepended a noisy banner would land
    //    the prefix mid-string and pass a `.contains` check
    //    while breaking grep / log-pattern consumers.
    // 3. Message contains `OFFLINE_ENV` so the operator knows
    //    where to look (the framework wraps the reason verbatim;
    //    `extract_via_llm`'s offline-gate Err surfaces the env
    //    var name in its reason string — see model.rs:1151+ for
    //    the bail! sites that name `OFFLINE_ENV`).
    let detail = &failures[0];
    assert_eq!(
        detail.kind,
        DetailKind::Other,
        "load-failure detail kind must be `Other` (the framework's bucket \
             for infrastructure failures); got: {:?}",
        detail.kind,
    );
    let msg = &detail.message;
    assert!(
        msg.starts_with("LlmExtract model load failed:"),
        "diagnostic must BEGIN WITH 'LlmExtract model load failed:' \
             — a substring-only match would let a regression bury the prefix \
             behind banner noise. got: {msg:?}",
    );
    assert!(
        msg.contains(crate::test_support::OFFLINE_ENV),
        "actionable diagnostic must name the offline env var so the operator \
             knows to unset KTSTR_MODEL_OFFLINE or pre-seed the cache; got: {msg}",
    );
    assert!(
        pm[0].metrics.is_empty(),
        "load failure must leave the PM slot empty; got: {:?}",
        pm[0].metrics,
    );
}

/// Offline-gate side: when stdout's `extract_via_llm`
/// call surfaces a load-failure reason, the stderr fallback is
/// SKIPPED — the failure reason is identical across both calls
/// and re-invoking inference would burn cycles to no purpose.
/// Pins the `load_err.is_none()` clause in the fallback gate
/// (eval.rs:281): `metrics.is_empty() && load_err.is_none() &&
/// !raw.stderr.is_empty()`.
///
/// Setup: empty stdout + non-empty stderr, under the offline
/// gate. Pre-gate, the model is uncached (`reset()` clears it).
///
/// Expected: exactly ONE load-failure detail surfaces (from the
/// stdout path). If the fallback erroneously fired, we'd see
/// either a SECOND load-failure detail (if extract_via_llm
/// re-Err'd) or an extracted-metrics outcome that contradicts
/// the offline-gate contract.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_offline_gate_skips_stderr_fallback() {
    let _env_lock = lock_env();
    super::super::model::reset();
    let _cache = isolated_cache_dir();
    let _offline = EnvVarGuard::set(crate::test_support::OFFLINE_ENV, "1");
    let mut pm = vec![empty_pm(0)];
    let raws = vec![crate::test_support::RawPayloadOutput {
        payload_index: 0,
        stdout: String::new(),
        stderr: "stderr body that the fallback would reach if not gated".to_string(),
        hint: None,
        metric_hints: Vec::new(),
        metric_bounds: None,
    }];
    let failures = host_side_llm_extract(&mut pm, &raws);
    // Exactly ONE failure detail — the fallback's `load_err.is_none()`
    // gate blocks a second extract_via_llm call when stdout's
    // result was Err.
    assert_eq!(
        failures.len(),
        1,
        "stderr fallback must be skipped when stdout's call already returned Err; \
             a second 'model load failed' detail would mean the gate regressed. \
             got: {failures:?}",
    );
    assert!(
        failures[0].message.contains("LlmExtract model load failed"),
        "the lone surfaced detail must be the load-failure: {}",
        failures[0].message,
    );
}

/// Multi-pair side: the offline-gate behavior is
/// per-pair, not global — a load-failure on one
/// (RawPayloadOutput, PayloadMetrics) pair must NOT short-
/// circuit processing of subsequent pairs. Each pair gets its
/// own load-failure detail, stamped independently.
///
/// Setup: TWO matched pairs, both under the offline gate. The
/// expected outcome is two load-failure details — one per
/// pair. A regression that bailed after the first failure
/// (e.g. an `if !failures.is_empty() { return failures }` in
/// the loop) would surface only one detail.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_offline_gate_per_pair_failure_detail() {
    let _env_lock = lock_env();
    super::super::model::reset();
    let _cache = isolated_cache_dir();
    let _offline = EnvVarGuard::set(crate::test_support::OFFLINE_ENV, "1");
    let mut pm = vec![empty_pm(0), empty_pm(1)];
    let raws = vec![
        crate::test_support::RawPayloadOutput {
            payload_index: 0,
            stdout: "first pair stdout".to_string(),
            stderr: String::new(),
            hint: None,
            metric_hints: Vec::new(),
            metric_bounds: None,
        },
        crate::test_support::RawPayloadOutput {
            payload_index: 1,
            stdout: "second pair stdout".to_string(),
            stderr: String::new(),
            hint: None,
            metric_hints: Vec::new(),
            metric_bounds: None,
        },
    ];
    let failures = host_side_llm_extract(&mut pm, &raws);
    assert_eq!(
        failures.len(),
        2,
        "two matched pairs under offline gate must each surface their own load-failure \
             detail; a regression that bailed after the first failure would surface only one. \
             got: {failures:?}",
    );
    for f in &failures {
        assert!(
            f.message.contains("LlmExtract model load failed"),
            "every detail must be a load-failure: {}",
            f.message,
        );
    }
    // Both PM slots stay empty — no metrics extracted on either path.
    assert!(
        pm[0].metrics.is_empty() && pm[1].metrics.is_empty(),
        "both PM slots must remain empty under the offline gate",
    );
}

/// Orphan + load-failure interaction: a mix of an
/// orphan raw output (no matching PM slot) AND a matched-but-
/// load-failing pair under the offline gate produces TWO
/// distinct details — one orphan-pairing and one load-failure.
/// Pins that the orphan path and the model-failure path are
/// orthogonal contributors to the failure list.
#[cfg(feature = "llm")]
#[test]
fn host_side_llm_extract_orphan_and_load_failure_both_surface() {
    let _env_lock = lock_env();
    super::super::model::reset();
    let _cache = isolated_cache_dir();
    let _offline = EnvVarGuard::set(crate::test_support::OFFLINE_ENV, "1");
    let mut pm = vec![empty_pm(0)];
    let raws = vec![
        crate::test_support::RawPayloadOutput {
            payload_index: 0,
            stdout: "matched pair".to_string(),
            stderr: String::new(),
            hint: None,
            metric_hints: Vec::new(),
            metric_bounds: None,
        },
        crate::test_support::RawPayloadOutput {
            payload_index: 99,
            stdout: "orphan".to_string(),
            stderr: String::new(),
            hint: None,
            metric_hints: Vec::new(),
            metric_bounds: None,
        },
    ];
    let failures = host_side_llm_extract(&mut pm, &raws);
    assert_eq!(
        failures.len(),
        2,
        "mixed orphan + matched-but-load-failing must surface both details independently; \
             got: {failures:?}",
    );
    let messages: Vec<&str> = failures.iter().map(|d| d.message.as_str()).collect();
    assert!(
        messages
            .iter()
            .any(|m| m.contains("LlmExtract host pairing") && m.contains("payload_index=99")),
        "orphan detail naming index 99 must surface: {messages:?}",
    );
    assert!(
        messages
            .iter()
            .any(|m| m.contains("LlmExtract model load failed")),
        "load-failure detail must surface: {messages:?}",
    );
}

/// Bulk-channel wire-frame round-trip: the full
/// guest→bulk-port→host transport for
/// `MSG_TYPE_RAW_PAYLOAD_OUTPUT` must preserve BOTH stdout and
/// stderr streams independently. A regression that concatenated
/// the streams (e.g. a guest-side "merge before serialize" or a
/// host-side "join after deserialize") would silently break
/// schbench-style payloads that emit metrics on stderr only —
/// the metric extraction would land on the merged blob,
/// contaminating both metric values and the `MetricStream` tag
/// attribution.
///
/// The new transport is the virtio-console port-1 TLV stream
/// parsed by [`crate::vmm::host_comms::parse_tlv_stream`] (the
/// host-side reader called from `collect_results`).
#[test]
fn raw_payload_output_bulk_wire_round_trip_preserves_both_streams() {
    use crate::vmm::wire;

    const STDOUT_MARKER: &str = "STDOUT_MARKER_BULK_E2E_a1b2c3";
    const STDERR_MARKER: &str = "STDERR_MARKER_BULK_E2E_x9y8z7";

    let original = crate::test_support::RawPayloadOutput {
        payload_index: 21,
        stdout: STDOUT_MARKER.to_string(),
        stderr: STDERR_MARKER.to_string(),
        hint: Some("bulk-focus".to_string()),
        metric_hints: Vec::new(),
        metric_bounds: None,
    };
    let payload = postcard::to_stdvec(&original).expect("postcard-encode RawPayloadOutput");

    // Build a single TLV frame in the same format the guest
    // writer emits to /dev/vport0p1: 16-byte ShmMessage header
    // followed by `payload.len()` bytes.
    use zerocopy::IntoBytes;
    let hdr = wire::ShmMessage {
        msg_type: wire::MSG_TYPE_RAW_PAYLOAD_OUTPUT,
        length: payload.len() as u32,
        crc32: crc32fast::hash(&payload),
        _pad: 0,
    };
    let mut frame: Vec<u8> = Vec::with_capacity(wire::FRAME_HEADER_SIZE + payload.len());
    frame.extend_from_slice(hdr.as_bytes());
    frame.extend_from_slice(&payload);

    let drained = crate::vmm::host_comms::parse_tlv_stream(&frame);
    assert_eq!(
        drained.entries.len(),
        1,
        "exactly one entry expected from bulk parse",
    );

    let entry = &drained.entries[0];
    assert_eq!(entry.msg_type, wire::MSG_TYPE_RAW_PAYLOAD_OUTPUT,);
    assert!(entry.crc_ok, "bulk CRC must match");

    let restored: crate::test_support::RawPayloadOutput =
        postcard::from_bytes(&entry.payload).expect("decode RawPayloadOutput from bulk");
    assert_eq!(restored.stdout, STDOUT_MARKER);
    assert_eq!(restored.stderr, STDERR_MARKER);
    assert!(!restored.stdout.contains(STDERR_MARKER));
    assert!(!restored.stderr.contains(STDOUT_MARKER));
    assert_eq!(restored.payload_index, original.payload_index);
    assert_eq!(restored.hint.as_deref(), Some("bulk-focus"));
}