franken_ocr 0.10.0

Pure-Rust, CPU-hyper-optimized runner for the Baidu Unlimited-OCR model (single-binary CLI: focr)
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
//! Per-model `FOCR_*` lever policy (U2-A7, plan D18).
//!
//! Unlimited-OCR-2 must never run a lever built for v1's R-SWA decode or for
//! the all-int8 decoder: that would produce confident garbage with exit 0.
//! Every `FOCR_*` variable this crate reads has one row in the model's table.
//! A row is refused before a weight byte is read (exit 2, lever named),
//! supported, ignored with an `ignored_lever` warning, not applicable (another
//! model's variable), or allow-listed tooling. A lever is refused only when
//! its value arms it, matching its reader's own parse; a kill-switch value is
//! a no-op and is ignored. The census test fails on a `FOCR_*` literal in
//! `src/` without a row, so a new lever cannot reach v2 silently.
//!
//! Models without a table (v1 and the zoo) are untouched.

use super::RunWarning;
use super::model_arch::{ModelArch, UnlimitedVariant};
use crate::error::{FocrError, FocrResult};

/// When a lever's value changes behaviour from its default, matching its
/// reader's parse. `NotFalsy` compares trimmed and case-insensitively; a
/// reader with a stricter parse is over-approximated, which only refuses more.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Armed {
    /// Any value, `0` and the empty string included (`var_os(..).is_some()`).
    Present,
    /// `1`, `true`, `on` or `yes` ([`crate::quant::recipe::is_truthy`]).
    Truthy,
    /// Anything but empty, `0`, `off`, `false` or `no`.
    NotFalsy,
}

impl Armed {
    /// Whether `value` arms the lever.
    pub(crate) fn by(self, value: &str) -> bool {
        let value = value.trim().to_ascii_lowercase();
        match self {
            Self::Present => true,
            Self::Truthy => crate::quant::recipe::is_truthy(&value),
            Self::NotFalsy => {
                !value.is_empty() && !matches!(value.as_str(), "0" | "off" | "false" | "no")
            }
        }
    }
}

/// What a model does with one lever. Each carries the reason, which is also
/// the user-facing explanation where one is printed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Decision {
    /// Reaches a path the model must not run, or changes its numerics
    /// unverified: refused (exit 2) when armed, ignored otherwise.
    Refuse(Armed, &'static str),
    /// Reaches the model and is exact there, or carries no numerics.
    Supported(&'static str),
    /// Reaches no path of the model: a warning when set.
    Ignored(&'static str),
    /// Another model's variable, or a transport the model does not use.
    NotApplicable(&'static str),
    /// Test, oracle, calibration or conversion tooling, or a literal that
    /// is not a variable; never consulted by an inference run.
    AllowListed(&'static str),
}

/// One `FOCR_*` variable and its decision.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Lever {
    pub(crate) name: &'static str,
    pub(crate) decision: Decision,
}

/// A model's full lever table.
#[derive(Debug)]
pub(crate) struct LeverPolicy {
    pub(crate) model_id: &'static str,
    pub(crate) levers: &'static [Lever],
}

const fn lever(name: &'static str, decision: Decision) -> Lever {
    Lever { name, decision }
}

use Decision::{AllowListed, Ignored, NotApplicable, Refuse, Supported};

const R_SWA_ONLY: &str = "it reaches only v1's R-SWA attention";
const INT8_DECODER_ONLY: &str = "it reaches only the all-int8 decoder, which this model refuses";
const UNDER_SPINE: &str = "it takes effect only under FOCR_BATCH_SPINE, which this model refuses";
const RESIDENT: &str = "it steers the resident daemon, which serves this model's single-image runs";

/// Unlimited-OCR-2's table, sorted by name.
pub(crate) static UOCR2_LEVER_POLICY: LeverPolicy = LeverPolicy {
    model_id: super::model_arch::UNLIMITED_OCR_V2_ID,
    levers: &[
        lever("FOCR_ATTN_GEMM", Ignored(R_SWA_ONLY)),
        lever("FOCR_BATCH_PACK", Ignored(UNDER_SPINE)),
        lever("FOCR_BATCH_SIZE", Ignored(UNDER_SPINE)),
        lever(
            "FOCR_BATCH_SPINE",
            Refuse(
                Armed::NotFalsy,
                "the batch spine runs v1's R-SWA int8 decode (until U2-F5)",
            ),
        ),
        lever("FOCR_BATCH_VISION", Ignored(UNDER_SPINE)),
        lever("FOCR_CALIB_OUT", AllowListed("calibration tooling")),
        lever("FOCR_CORPUS_DIR", AllowListed("model-gated corpus tests")),
        lever(
            "FOCR_DECODE_F32",
            Supported("the all-f32 cached decode is a v2 parity path (U2-C2)"),
        ),
        lever(
            "FOCR_DECODE_INT8",
            Refuse(
                Armed::Truthy,
                "the experimental all-int8 decoder is v1 R-SWA only",
            ),
        ),
        lever(
            "FOCR_DECODE_STATELESS",
            Supported("the stateless f32 decode is a v2 path (U2-B5)"),
        ),
        lever("FOCR_E2E_LONG", AllowListed("long model-gated tests")),
        lever(
            "FOCR_FORCE_ARCH",
            Supported("SIMD tier override; each tier matches its scalar fallback"),
        ),
        lever("FOCR_FUSE_NGRAM_LMHEAD", Ignored(INT8_DECODER_ONLY)),
        lever("FOCR_FUSE_NORM_QUANT", Ignored(INT8_DECODER_ONLY)),
        lever(
            "FOCR_FUSE_SWIGLU",
            Supported("the fused SwiGLU gives v2's decodes their unfused ids (U2-C2)"),
        ),
        lever("FOCR_GOT_FORMAT", NotApplicable("GOT-OCR2's")),
        lever("FOCR_GOT_INT8_LMHEAD", NotApplicable("GOT-OCR2's")),
        lever("FOCR_GOT_MODEL", AllowListed("GOT-OCR2 model-gated tests")),
        lever("FOCR_GOT_NO_REPEAT_NGRAM", NotApplicable("GOT-OCR2's")),
        lever("FOCR_GOT_SEQ_ATTN", NotApplicable("GOT-OCR2's")),
        lever("FOCR_GOT_TIKTOKEN", AllowListed("GOT-OCR2 tokenizer tests")),
        lever(
            "FOCR_INT4_GROUPS",
            Ignored("it is a `focr convert --quant int4` knob, and this model has no int4 recipe"),
        ),
        lever("FOCR_INT8_ATTN", Ignored(INT8_DECODER_ONLY)),
        lever(
            "FOCR_INT8_AUTOVEC",
            Supported("either int8 kernel tier gives v2's decode the same ids (U2-C2)"),
        ),
        lever("FOCR_INT8_KV", Ignored(R_SWA_ONLY)),
        lever("FOCR_INT8_LMHEAD", Ignored(INT8_DECODER_ONLY)),
        lever(
            "FOCR_LMHEAD_SHARD",
            Supported("the vocab-tiled lm_head gives v2's f32 and int8 decodes their ids (U2-C2)"),
        ),
        lever(
            "FOCR_LMHEAD_SHARD_TILES",
            Supported("the tile count only groups lm_head columns (U2-C2)"),
        ),
        lever(
            "FOCR_MANIFEST_URL",
            Supported("only where `focr pull` reads its manifest"),
        ),
        lever(
            "FOCR_MAX_NEW_TOKENS",
            Supported("a per-page generated-token cap for this model (U2-B6)"),
        ),
        lever(
            "FOCR_MEMORY_BUDGET",
            Supported("sizes a memory-sized page window (U2-D3)"),
        ),
        lever(
            "FOCR_MMAP",
            Supported("maps the same bytes the owned load reads"),
        ),
        lever("FOCR_MODEL_DIR", Supported("model resolution")),
        lever("FOCR_MODEL_PATH", Supported("model resolution")),
        lever(
            "FOCR_MOE_POLICY_TEST_CASE",
            AllowListed("MoE policy child-process tests"),
        ),
        lever(
            "FOCR_MOE_SCORE_ORDER",
            Refuse(
                Armed::Truthy,
                "it routes experts in score order, not the reference's torch.topk slot \
                 order, which moves v2's int8 decode off its ids (U2-C2)",
            ),
        ),
        lever("FOCR_NO_PROGRESS", Supported("progress rendering only")),
        lever(
            "FOCR_NO_REPEAT_NGRAM",
            Supported("a per-page generated-only ban for this model (U2-B6)"),
        ),
        lever("FOCR_NO_RESIDENT", Supported(RESIDENT)),
        lever(
            "FOCR_ONECHART_DIR",
            AllowListed("OneChart model-gated tests"),
        ),
        lever("FOCR_ORACLE_HIDDEN0", AllowListed("GOT-OCR2 oracle tests")),
        lever("FOCR_ORACLE_IMAGE", AllowListed("GOT-OCR2 oracle tests")),
        lever("FOCR_ORACLE_LOGITS", AllowListed("GOT-OCR2 oracle tests")),
        lever("FOCR_PARITY_IMAGES", AllowListed("parity-ladder tests")),
        lever(
            "FOCR_PREFILL_CHUNK",
            Refuse(
                Armed::Present,
                "chunked prefill assumes R-SWA attention (until U2-F2)",
            ),
        ),
        lever(
            "FOCR_PROFILE_DECODE",
            Supported("decode timing counters only"),
        ),
        lever("FOCR_QKV_FUSED", Ignored(INT8_DECODER_ONLY)),
        lever("FOCR_QUANT", Supported("model resolution")),
        lever(
            "FOCR_REREAD_CAPPED_PAGES",
            Supported("a capped page is read again alone, as a single page reads (bd-yrr1)"),
        ),
        lever(
            "FOCR_RESAMPLE",
            Ignored("this model's preprocessing fixes its own resampling (U2-B7)"),
        ),
        lever("FOCR_RESIDENT_CLIENT_TIMEOUT_SECS", Supported(RESIDENT)),
        lever("FOCR_RESIDENT_DIR", Supported(RESIDENT)),
        lever("FOCR_RESIDENT_IDLE_SECS", Supported(RESIDENT)),
        lever("FOCR_RESIDENT_LOG", Supported(RESIDENT)),
        lever("FOCR_RESIDENT_SPAWN_WAIT_SECS", Supported(RESIDENT)),
        lever("FOCR_RSWA_PARALLEL_ATTN", Ignored(R_SWA_ONLY)),
        lever(
            "FOCR_RUNAWAY_GUARD",
            Supported("closes a page found repeating, as the per-page cap does (bd-gftg)"),
        ),
        lever("FOCR_RUN_STORE", Supported("run telemetry only")),
        lever(
            "FOCR_SAM_ATTN_PROBE_RUNS",
            AllowListed("SAM attention probe tests"),
        ),
        lever("FOCR_SIGLIP_SEQ", NotApplicable("SmolVLM2's")),
        lever(
            "FOCR_SMOLVLM2_CERT_FULL",
            AllowListed("SmolVLM2 certification tests"),
        ),
        lever(
            "FOCR_SMOLVLM2_DIR",
            AllowListed("SmolVLM2 model-gated tests"),
        ),
        lever(
            "FOCR_SMOLVLM2_MODEL",
            AllowListed("SmolVLM2 model-gated tests"),
        ),
        lever(
            "FOCR_SMOLVLM2_ORACLE_HIDDEN0",
            AllowListed("SmolVLM2 oracle tests"),
        ),
        lever(
            "FOCR_SMOLVLM2_ORACLE_LOGITS",
            AllowListed("SmolVLM2 oracle tests"),
        ),
        lever("FOCR_SMOLVLM2_QUESTION", NotApplicable("SmolVLM2's")),
        lever(
            "FOCR_SMOLVLM2_TOKENIZER_JSON",
            AllowListed("SmolVLM2 tokenizer tests"),
        ),
        lever(
            "FOCR_SOFTMAX_COPY",
            Supported("the copying softmax gives v2's f32 and int8 decodes their ids (U2-C2)"),
        ),
        lever(
            "FOCR_SPEC_DECODE",
            Ignored("speculative decoding runs only inside the all-int8 decoder"),
        ),
        lever(
            "FOCR_SPEC_VERIFY",
            Ignored("speculative verify has no caller in any decode path"),
        ),
        lever(
            "FOCR_SRSWA_DENSE",
            Supported("the dense SR-SWA attention is bit-identical to the gathered one"),
        ),
        lever(
            "FOCR_SRSWA_ENTMAX",
            Supported(
                "the router's entmax domain: HF's top-M window (default) or SGLang's \
                 all candidates (D21, DISC-U2-1)",
            ),
        ),
        lever(
            "FOCR_SRSWA_RETIRE",
            Supported("SR-SWA block retirement frees only blocks no query can select"),
        ),
        lever(
            "FOCR_STAGE_BUDGET",
            AllowListed("not a variable: a music test asserts it is never named"),
        ),
        lever(
            "FOCR_STAGE_BUDGET_FORWARD_MS",
            Supported("the forward stage budget (U2-B6 scales it per page)"),
        ),
        lever(
            "FOCR_STREAM_VISION",
            Ignored("it streams only the GOT-OCR2, OneChart and SmolVLM2 vision towers"),
        ),
        lever("FOCR_TEST_FORCE_ERROR", AllowListed("CLI error-path tests")),
        lever("FOCR_TEST_LILYPOND_PDF", AllowListed("PDF text tests")),
        lever(
            "FOCR_TEST_LILYPOND_WINANSI_NAME_PDF",
            AllowListed("PDF text tests"),
        ),
        lever(
            "FOCR_TEST_MAX_POSITIONS",
            AllowListed("multi-page budget tests (U2-X2)"),
        ),
        lever(
            "FOCR_TEST_UNLIMITED_2_RECIPE_MODEL",
            AllowListed("census tests"),
        ),
        lever(
            "FOCR_TEST_UNLIMITED_2_SOURCE_DIR",
            AllowListed("census tests"),
        ),
        lever(
            "FOCR_TEST_UNLIMITED_RECIPE_MODEL",
            AllowListed("census tests"),
        ),
        lever(
            "FOCR_TEST_UNLIMITED_SOURCE_MODEL",
            AllowListed("census tests"),
        ),
        lever(
            "FOCR_THREADS",
            Supported("thread count; every kernel is thread-count exact"),
        ),
        lever("FOCR_TIMING", Supported("stage timing lines only")),
        lever("FOCR_TOKENIZER_JSON", AllowListed("tokenizer tests")),
        lever(
            "FOCR_TROMR",
            AllowListed("not a variable: a music test asserts it is never named"),
        ),
        lever("FOCR_TROMR_DIR", AllowListed("TrOMR model-gated tests")),
        lever("FOCR_TROMR_SAMPLE", NotApplicable("TrOMR's")),
        lever("FOCR_TROMR_SEED", NotApplicable("TrOMR's")),
        lever("FOCR_TROMR_SPLIT", NotApplicable("TrOMR's")),
        lever(
            "FOCR_UNLIMITED_RECIPE_ARTIFACT",
            AllowListed("conversion tests"),
        ),
        lever(
            "FOCR_UNLIMITED_VISION_CACHE",
            Supported("both arms hydrate the same BF16 vision tensors, byte-identical to v1's"),
        ),
        lever("FOCR_UOCR2_SOURCE_DIR", AllowListed("v2 model-gated tests")),
        lever(
            "FOCR_UOCR2_TOKENIZER_JSON",
            AllowListed("v2 tokenizer tests"),
        ),
    ],
};

/// The lever table of `arch`, if it has one. Exhaustive over the Unlimited
/// variants, so a new variant must decide here.
pub(crate) fn policy_for(arch: &dyn ModelArch) -> Option<&'static LeverPolicy> {
    match arch.unlimited_variant() {
        None | Some(UnlimitedVariant::V1Rswa) => None,
        Some(UnlimitedVariant::V2SrSwa) => Some(&UOCR2_LEVER_POLICY),
    }
}

/// What the check reads: one environment lookup and the process-wide
/// full-int8 force flag (`ocr-batch --experimental-full-int8`).
pub(crate) struct LeverInputs<'a> {
    pub(crate) var: &'a dyn Fn(&str) -> Option<String>,
    pub(crate) full_int8_forced: bool,
}

/// The outcome of [`admit`]: the ignored-lever warnings, which are reported
/// whether or not the run is refused, and the verdict.
#[derive(Debug)]
pub(crate) struct Admission {
    pub(crate) warnings: Vec<RunWarning>,
    pub(crate) verdict: FocrResult<()>,
}

/// Admit `arch` for an inference run, before a weight byte is read.
///
/// Refuses with [`FocrError::Usage`] (exit 2) naming every armed refused
/// lever; then refuses a model that is registered but not implemented.
pub(crate) fn admit(arch: &dyn ModelArch, inputs: &LeverInputs<'_>) -> Admission {
    let Some(policy) = policy_for(arch) else {
        return Admission {
            warnings: Vec::new(),
            verdict: Ok(()),
        };
    };
    let model = policy.model_id;
    let mut refused = Vec::new();
    let mut warnings = Vec::new();
    if inputs.full_int8_forced {
        refused.push(format!(
            "--experimental-full-int8 is not supported for {model}: the all-int8 decoder it \
             forces is v1 R-SWA only"
        ));
    }
    for lever in policy.levers {
        let Some(value) = (inputs.var)(lever.name) else {
            continue;
        };
        match lever.decision {
            Refuse(armed, why) if armed.by(&value) => {
                refused.push(format!(
                    "{} is not supported for {model}: {why}",
                    lever.name
                ));
            }
            Refuse(..) => warnings.push(ignored_lever(
                lever.name,
                model,
                &format!("{value:?} leaves it off"),
            )),
            Ignored(why) => warnings.push(ignored_lever(lever.name, model, why)),
            Supported(_) | NotApplicable(_) | AllowListed(_) => {}
        }
    }
    let verdict = if !refused.is_empty() {
        // A flag is removed from the command line; variables are unset.
        let verb = if inputs.full_int8_forced {
            "remove"
        } else {
            "unset"
        };
        let object = if refused.len() == 1 { "it" } else { "them" };
        Err(FocrError::Usage(format!(
            "{}; {verb} {object}",
            refused.join("; ")
        )))
    } else if !arch.implemented() {
        Err(FocrError::Usage(format!(
            "{model} is registered but not yet implemented in this build; use unlimited-ocr \
             meanwhile"
        )))
    } else {
        Ok(())
    };
    Admission { warnings, verdict }
}

/// [`admit`] over the process environment and force flag.
pub(crate) fn admit_from_process(arch: &dyn ModelArch) -> Admission {
    admit(
        arch,
        &LeverInputs {
            var: &|name| std::env::var_os(name).map(|value| value.to_string_lossy().into_owned()),
            full_int8_forced: super::FORCE_INT8_DECODE.load(std::sync::atomic::Ordering::Relaxed),
        },
    )
}

/// The `ignored_lever` warning for `name`.
fn ignored_lever(name: &str, model: &str, why: &str) -> RunWarning {
    let mut fields = serde_json::Map::new();
    fields.insert("env".into(), name.into());
    RunWarning {
        kind: "ignored_lever",
        message: format!("{name} has no effect on {model}: {why}"),
        recovery: super::warning_recovery(super::WarningKind::IgnoredLever),
        fields,
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{BTreeMap, BTreeSet};
    use std::path::{Path, PathBuf};

    use super::*;
    use crate::native_engine::model_arch;

    fn v2() -> &'static dyn ModelArch {
        model_arch::arch_by_id(model_arch::UNLIMITED_OCR_V2_ID).expect("v2 registered")
    }

    fn line(test: &str, case: &str, event: &str, pass: bool, extra: serde_json::Value) {
        let mut object = serde_json::json!({
            "schema_version": 1, "ts": 0.0, "test": test, "case": case, "run_seq": 0,
            "event": event, "result": if pass { "pass" } else { "fail" },
        });
        if let (Some(object), Some(extra)) = (object.as_object_mut(), extra.as_object()) {
            object.extend(extra.clone());
        }
        println!("{object}");
    }

    /// Admit `arch` with exactly `env` set and the force flag as given.
    fn admit_with(arch: &dyn ModelArch, env: &[(&str, &str)], forced: bool) -> Admission {
        let env: BTreeMap<String, String> = env
            .iter()
            .map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
            .collect();
        admit(
            arch,
            &LeverInputs {
                var: &|name| env.get(name).cloned(),
                full_int8_forced: forced,
            },
        )
    }

    /// A value that arms `decision` (or merely sets it).
    fn armed_sample(decision: Decision) -> &'static str {
        match decision {
            Refuse(Armed::Present, _) => "0",
            Refuse(Armed::Truthy | Armed::NotFalsy, _) => "1",
            Supported(_) | Ignored(_) | NotApplicable(_) | AllowListed(_) => "1",
        }
    }

    /// Every `.rs` file under `dir` with its text.
    fn rust_sources(dir: &Path, out: &mut Vec<(PathBuf, String)>) {
        for entry in std::fs::read_dir(dir).expect("read src dir") {
            let path = entry.expect("dir entry").path();
            if path.is_dir() {
                rust_sources(&path, out);
            } else if path.extension().is_some_and(|ext| ext == "rs") {
                let text = std::fs::read_to_string(&path).expect("read source file");
                out.push((path, text));
            }
        }
    }

    /// The `[A-Z0-9_]+` runs that follow `prefix` and end at a `"`.
    fn quoted_after<'a>(text: &'a str, prefix: &'a str) -> impl Iterator<Item = &'a str> + 'a {
        text.match_indices(prefix).filter_map(move |(at, _)| {
            let rest = &text[at + prefix.len()..];
            let end = rest
                .find(|c: char| !(c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_'))
                .unwrap_or(rest.len());
            (end > 0 && rest[end..].starts_with('"')).then(|| &rest[..end])
        })
    }

    /// The census (U2-A7): every `FOCR_*` literal in `src/` has exactly one
    /// row; every row names a variable read outside this table.
    #[test]
    fn every_focr_variable_in_src_has_exactly_one_uocr2_row() {
        let test = "every_focr_variable_in_src_has_exactly_one_uocr2_row";
        let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
        let mut sources = Vec::new();
        rust_sources(&src, &mut sources);
        let mut literals: BTreeMap<String, BTreeSet<PathBuf>> = BTreeMap::new();
        for (path, text) in &sources {
            for name in quoted_after(text, "\"FOCR_") {
                literals
                    .entry(format!("FOCR_{name}"))
                    .or_default()
                    .insert(path.clone());
            }
        }
        // No variable is named through `format!`: a dynamic name would hide
        // from this scan.
        assert!(
            literals.contains_key("FOCR_STAGE_BUDGET_FORWARD_MS"),
            "the scan finds the forward budget"
        );

        let names: Vec<&str> = UOCR2_LEVER_POLICY
            .levers
            .iter()
            .map(|lever| lever.name)
            .collect();
        assert!(
            names.windows(2).all(|pair| pair[0] < pair[1]),
            "rows are sorted and unique"
        );
        let rows: BTreeSet<&str> = names.iter().copied().collect();
        let missing: Vec<&String> = literals
            .keys()
            .filter(|name| !rows.contains(name.as_str()))
            .collect();
        let this_file = src.join("native_engine").join("lever_policy.rs");
        let stale: Vec<&str> = names
            .iter()
            .copied()
            .filter(|name| {
                !literals
                    .get(*name)
                    .is_some_and(|files| files.iter().any(|file| *file != this_file))
            })
            .collect();
        let pass = missing.is_empty() && stale.is_empty();
        line(
            test,
            "census",
            "assert",
            pass,
            serde_json::json!({
                "assertion": "rows == FOCR_* literals in src",
                "pass": pass, "rows": rows.len(), "literals": literals.len(),
                "missing": missing, "stale": stale,
            }),
        );
        assert!(
            missing.is_empty(),
            "variables without a UOCR2_LEVER_POLICY row: {missing:?}"
        );
        assert!(
            stale.is_empty(),
            "rows naming no variable read in src: {stale:?}"
        );
    }

    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum Expect {
        /// Exit 2 naming the lever.
        Refused,
        /// Admitted, with one `ignored_lever` warning.
        Ignored,
        /// Admitted, no warning.
        Silent,
    }

    /// The bead's path/lever matrix, written independently of the table.
    const MATRIX: &[(&str, &str, Expect)] = &[
        ("FOCR_DECODE_INT8", "1", Expect::Refused),
        ("FOCR_DECODE_INT8", "0", Expect::Ignored),
        ("FOCR_PREFILL_CHUNK", "512", Expect::Refused),
        ("FOCR_PREFILL_CHUNK", "0", Expect::Refused),
        ("FOCR_LMHEAD_SHARD", "0", Expect::Silent),
        ("FOCR_BATCH_SPINE", "1", Expect::Refused),
        ("FOCR_BATCH_SPINE", "0", Expect::Ignored),
        ("FOCR_BATCH_SPINE", "", Expect::Ignored),
        ("FOCR_BATCH_SPINE", " OFF ", Expect::Ignored),
        ("FOCR_MOE_SCORE_ORDER", "1", Expect::Refused),
        ("FOCR_MOE_SCORE_ORDER", "0", Expect::Ignored),
        ("FOCR_FUSE_SWIGLU", "0", Expect::Silent),
        ("FOCR_SOFTMAX_COPY", "yes", Expect::Silent),
        ("FOCR_INT8_AUTOVEC", "0", Expect::Silent),
        ("FOCR_SPEC_DECODE", "1", Expect::Ignored),
        ("FOCR_SPEC_VERIFY", "1", Expect::Ignored),
        ("FOCR_INT8_ATTN", "1", Expect::Ignored),
        ("FOCR_INT8_LMHEAD", "1", Expect::Ignored),
        ("FOCR_INT8_KV", "1", Expect::Ignored),
        ("FOCR_ATTN_GEMM", "1", Expect::Ignored),
        ("FOCR_RSWA_PARALLEL_ATTN", "0", Expect::Ignored),
        ("FOCR_QKV_FUSED", "0", Expect::Ignored),
        ("FOCR_QKV_FUSED", "1", Expect::Ignored),
        ("FOCR_FUSE_NGRAM_LMHEAD", "1", Expect::Ignored),
        ("FOCR_FUSE_NORM_QUANT", "1", Expect::Ignored),
        ("FOCR_RUNAWAY_GUARD", "1", Expect::Silent),
        ("FOCR_RUNAWAY_GUARD", "maybe", Expect::Silent),
        ("FOCR_RESAMPLE", "pil-bicubic", Expect::Ignored),
        ("FOCR_LMHEAD_SHARD_TILES", "4", Expect::Silent),
        ("FOCR_BATCH_PACK", "1", Expect::Ignored),
        ("FOCR_BATCH_VISION", "0", Expect::Ignored),
        ("FOCR_BATCH_SIZE", "4", Expect::Ignored),
        ("FOCR_STREAM_VISION", "1", Expect::Ignored),
        ("FOCR_INT4_GROUPS", "32", Expect::Ignored),
        ("FOCR_MAX_NEW_TOKENS", "100", Expect::Silent),
        ("FOCR_NO_REPEAT_NGRAM", "20", Expect::Silent),
        ("FOCR_THREADS", "4", Expect::Silent),
        ("FOCR_FORCE_ARCH", "scalar", Expect::Silent),
        ("FOCR_PROFILE_DECODE", "0", Expect::Silent),
        ("FOCR_QUANT", "int8", Expect::Silent),
        ("FOCR_DECODE_STATELESS", "1", Expect::Silent),
        ("FOCR_DECODE_F32", "1", Expect::Silent),
        ("FOCR_STAGE_BUDGET_FORWARD_MS", "0", Expect::Silent),
        ("FOCR_REREAD_CAPPED_PAGES", "1", Expect::Silent),
        ("FOCR_UNLIMITED_VISION_CACHE", "0", Expect::Silent),
        ("FOCR_MEMORY_BUDGET", "12GiB", Expect::Silent),
        ("FOCR_MMAP", "1", Expect::Silent),
        ("FOCR_SRSWA_RETIRE", "0", Expect::Silent),
        ("FOCR_SRSWA_DENSE", "1", Expect::Silent),
        ("FOCR_SRSWA_ENTMAX", "all", Expect::Silent),
        ("FOCR_CALIB_OUT", "/tmp/calib", Expect::Silent),
        ("FOCR_SAM_ATTN_PROBE_RUNS", "3", Expect::Silent),
        ("FOCR_TEST_MAX_POSITIONS", "100", Expect::Silent),
        ("FOCR_NO_RESIDENT", "1", Expect::Silent),
        ("FOCR_RESIDENT_DIR", "/tmp", Expect::Silent),
        ("FOCR_GOT_FORMAT", "1", Expect::Silent),
    ];

    fn observed(name: &str, admission: &Admission) -> Expect {
        let warned: Vec<&RunWarning> = admission
            .warnings
            .iter()
            .filter(|warning| warning.fields.get("env").and_then(|v| v.as_str()) == Some(name))
            .collect();
        match &admission.verdict {
            Err(error)
                if error
                    .to_string()
                    .contains(&format!("{name} is not supported for unlimited-ocr-2"))
                    && warned.is_empty() =>
            {
                Expect::Refused
            }
            Ok(()) if warned.len() == 1 && admission.warnings.len() == 1 => {
                assert_eq!(warned[0].kind, "ignored_lever");
                assert!(warned[0].message.contains(name), "{}", warned[0].message);
                Expect::Ignored
            }
            Ok(()) if admission.warnings.is_empty() => Expect::Silent,
            _ => panic!("{name}: unclassifiable admission {admission:?}"),
        }
    }

    /// The matrix (U2-A7): each lever alone, against v2 (Experimental since
    /// U2-B6, so only a refused lever stops the run).
    #[test]
    fn v2_lever_matrix_refuses_ignores_or_admits_each_lever_before_load() {
        let test = "v2_lever_matrix_refuses_ignores_or_admits_each_lever_before_load";
        for &(name, value, expect) in MATRIX {
            let admission = admit_with(v2(), &[(name, value)], false);
            let got = observed(name, &admission);
            let error = admission.verdict.as_ref().err();
            line(
                test,
                &format!("{name}={value}"),
                "assert",
                got == expect,
                serde_json::json!({
                    "assertion": "admission decision matches the bead matrix",
                    "pass": got == expect, "arch": "unlimited-ocr-2", "lever": name,
                    "value": value, "expected": format!("{expect:?}"), "got": format!("{got:?}"),
                    "exit_code": error.map(FocrError::exit_code),
                    "message": error.map(ToString::to_string),
                    "warnings": admission.warnings.len(), "weights_touched": false,
                }),
            );
            assert_eq!(got, expect, "{name}={value:?}: {admission:?}");
            if let Some(error) = error {
                assert!(matches!(error, FocrError::Usage(_)), "{error:?}");
                assert_eq!(error.exit_code(), 2);
            }
        }
    }

    /// Every row, armed, against the table's own decision; nothing panics
    /// and no row escapes the three outcomes.
    #[test]
    fn every_row_armed_alone_lands_on_its_decision() {
        for lever in UOCR2_LEVER_POLICY.levers {
            let admission = admit_with(v2(), &[(lever.name, armed_sample(lever.decision))], false);
            let expect = match lever.decision {
                Refuse(..) => Expect::Refused,
                Ignored(_) => Expect::Ignored,
                Supported(_) | NotApplicable(_) | AllowListed(_) => Expect::Silent,
            };
            assert_eq!(observed(lever.name, &admission), expect, "{}", lever.name);
        }
    }

    /// v1 and every zoo model: every lever set at once, and the force flag,
    /// change nothing (they have no table).
    #[test]
    fn v1_and_zoo_admission_is_untouched_by_every_lever() {
        let test = "v1_and_zoo_admission_is_untouched_by_every_lever";
        let env: Vec<(&str, &str)> = UOCR2_LEVER_POLICY
            .levers
            .iter()
            .map(|lever| (lever.name, armed_sample(lever.decision)))
            .collect();
        for arch in model_arch::registry() {
            if arch.id() == model_arch::UNLIMITED_OCR_V2_ID {
                continue;
            }
            for forced in [false, true] {
                let admission = admit_with(*arch, &env, forced);
                let pass = admission.verdict.is_ok() && admission.warnings.is_empty();
                line(
                    test,
                    &format!("{}:forced={forced}", arch.id()),
                    "assert",
                    pass,
                    serde_json::json!({
                        "assertion": "no lever table, admission unchanged", "pass": pass,
                        "arch": arch.id(), "levers_set": env.len(), "weights_touched": false,
                    }),
                );
                assert!(pass, "{}: {admission:?}", arch.id());
            }
        }
    }

    #[test]
    fn the_full_int8_force_flag_and_several_levers_are_all_named() {
        let forced = admit_with(v2(), &[], true);
        let message = forced
            .verdict
            .expect_err("forced full-int8 refused")
            .to_string();
        assert!(
            message.contains("--experimental-full-int8 is not supported for unlimited-ocr-2"),
            "{message}"
        );
        assert!(message.ends_with("; remove it"), "{message}");

        let one = admit_with(v2(), &[("FOCR_PREFILL_CHUNK", "64")], false);
        let message = one.verdict.expect_err("refused").to_string();
        assert!(message.ends_with("; unset it"), "{message}");

        let several = admit_with(
            v2(),
            &[
                ("FOCR_PREFILL_CHUNK", "64"),
                ("FOCR_BATCH_SPINE", "1"),
                ("FOCR_QKV_FUSED", "0"),
            ],
            true,
        );
        let message = several.verdict.expect_err("refused").to_string();
        for name in [
            "--experimental-full-int8",
            "FOCR_PREFILL_CHUNK",
            "FOCR_BATCH_SPINE",
        ] {
            assert!(message.contains(name), "{message}");
        }
        assert!(!message.contains("FOCR_QKV_FUSED"), "{message}");
        assert!(message.ends_with("; remove them"), "{message}");
        assert_eq!(several.warnings.len(), 1, "the ignored lever still warns");
    }

    /// v2 (Experimental since U2-B6) is admitted in an unarmed environment.
    /// A policed model that is still Planned is refused after its levers are
    /// judged: an armed refused lever is named, and ignored levers still warn.
    #[test]
    fn a_planned_policed_model_is_refused_after_its_levers() {
        let real = admit_with(v2(), &[("FOCR_THREADS", "4")], false);
        assert!(real.verdict.is_ok() && real.warnings.is_empty(), "{real:?}");

        struct PlannedV2;
        impl ModelArch for PlannedV2 {
            fn id(&self) -> &'static str {
                v2().id()
            }
            fn display_name(&self) -> &'static str {
                v2().display_name()
            }
            fn license_notice(&self) -> &'static str {
                v2().license_notice()
            }
            fn default_artifact_basename(&self) -> &'static str {
                v2().default_artifact_basename()
            }
            fn vision_encoder(&self) -> model_arch::VisionEncoder {
                v2().vision_encoder()
            }
            fn decoder(&self) -> model_arch::Decoder {
                v2().decoder()
            }
            fn tokenizer(&self) -> model_arch::TokenizerKind {
                v2().tokenizer()
            }
            fn decode_contract(&self) -> model_arch::DecodeContract {
                v2().decode_contract()
            }
            fn tasks(&self) -> &'static [model_arch::Task] {
                v2().tasks()
            }
            fn maturity(&self) -> model_arch::Maturity {
                model_arch::Maturity::Planned
            }
            fn required_quant_recipe(&self, quant: &str) -> Option<&'static str> {
                v2().required_quant_recipe(quant)
            }
            fn unlimited_variant(&self) -> Option<UnlimitedVariant> {
                v2().unlimited_variant()
            }
        }
        const NOT_IMPLEMENTED: &str =
            "unlimited-ocr-2 is registered but not yet implemented in this build";
        let clean = admit_with(&PlannedV2, &[("FOCR_THREADS", "4")], false);
        assert!(
            matches!(clean.verdict, Err(FocrError::Usage(ref m)) if m.contains(NOT_IMPLEMENTED)),
            "{clean:?}"
        );
        assert!(clean.warnings.is_empty(), "{clean:?}");
        let ignored = admit_with(&PlannedV2, &[("FOCR_QKV_FUSED", "0")], false);
        assert!(
            matches!(ignored.verdict, Err(FocrError::Usage(ref m)) if m.contains(NOT_IMPLEMENTED)),
            "{ignored:?}"
        );
        assert_eq!(ignored.warnings.len(), 1);
        let json = ignored.warnings[0].to_json();
        assert_eq!(json["kind"], "ignored_lever");
        assert_eq!(json["env"], "FOCR_QKV_FUSED");
        assert!(super::super::RUN_WARNING_KINDS.contains(&"ignored_lever"));
        let refused = admit_with(&PlannedV2, &[("FOCR_PREFILL_CHUNK", "")], false);
        assert!(
            matches!(refused.verdict, Err(FocrError::Usage(ref m))
                if m.contains("FOCR_PREFILL_CHUNK") && !m.contains(NOT_IMPLEMENTED)),
            "{refused:?}"
        );
    }

    #[test]
    fn armed_matches_each_reader_parse() {
        let cases = [
            (Armed::Present, "", true),
            (Armed::Present, "0", true),
            (Armed::Truthy, " YES ", true),
            (Armed::Truthy, "2", false),
            (Armed::Truthy, "off", false),
            (Armed::NotFalsy, "", false),
            (Armed::NotFalsy, " Off ", false),
            (Armed::NotFalsy, "anything", true),
        ];
        for (armed, value, expected) in cases {
            assert_eq!(armed.by(value), expected, "{armed:?} by {value:?}");
        }
    }
}