Skip to main content

fuel_dispatch/fkc/
validate.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Build-time validators (the `V-FKC-*` battery, FKC §10).
3//!
4//! These run at import time, `Result`-returning, never panicking, never a
5//! silent fix-up (FKC G6, constitution: validate-at-build-time, never-panic).
6//! They operate on the **parsed/structured** form ([`FkcFile`] / [`FkcKernel`])
7//! — structural + coherence checks — NOT on a registered binding table. The
8//! register slice (`register.rs`) calls [`validate_file`] after parse+lower so a
9//! bad contract fails import; the CI lint (`tests`) calls it standalone over the
10//! whole checked-in corpus.
11//!
12//! ## Rule → check → error map (FKC §10, rules 1–16)
13//!
14//! | rule | check | `FkcError` |
15//! |------|-------|------------|
16//! | 1  | `fkc_version ∈ FKC_SUPPORTED_VERSIONS` | `UnsupportedVersion` |
17//! | 2  | required fields (kernel; EXACTLY ONE of op_kind/fused_op; blurb; entry_point; ≥1 input; ≥1 output/bundle; cost; precision; determinism). Describe-only (§3.10) EXEMPTS exactly-one-of-op_kind/fused_op AND the ≥1-input rule (a zero-operand documentation op is legitimate). | `MissingRequiredField` / `MissingBlurb` / `OpTargetAmbiguous` |
18//! | 3  | dtype validity; sub-byte ⇒ fdx.quant; ggml_dtype a real `GgmlDType` | `BadScalarType` / `QuantIncoherent` |
19//! | 4  | layout coherence (≥1 of contiguous/strided OK; broadcast/reverse ⇒ strided) | `LayoutIncoherent` |
20//! | 5  | awkward-layout coherence PER OPERAND | `AwkwardStrategyIncoherent` |
21//! | 6  | quant coherence + scale single-place + MX/AFFINE_BLOCK not-yet-registrable | `QuantIncoherent` / `ScaleDoubleDeclared` / `MxNotYetRegistrable` |
22//! | 7  | op-param variant in the correct namespace | `BadOpParamsVariant` |
23//! | 8  | cost expressions parse, in-scope | `CostExprParse` |
24//! | 8a | cost provenance present + non-placeholder | `CostProvenanceMissing` / `PlaceholderCost` |
25//! | 9  | precision coverage (UNAUDITED flagged; nondeterministic ⇒ audited+no-bitstable) | (note / `QuantIncoherent`-shaped) |
26//! | 10 | duplicate KernelRef — at register time (`finalize`), not here |
27//! | 11 | prose/structured blurb agreement — see §10.11 note below |
28//! | 13 | bundle slot rank ≤ 6 | `BundleSlotRankExceeded` |
29//! | 14 | gather (paged) coherence | `GatherIncoherent` / `GatherNotYetSupported` |
30//! | 15 | affine / symbolic extent coherence | `UnknownAdmissibilityEnum` |
31//! | 16 | FDX-subset drift-guard (every dtype/quant/granularity/ggml token ∈ FDX table) | `FdxTokenNotInTable` |
32//!
33//! ### §10.11 (prose blurb) — not feasible on the structured form
34//!
35//! The prose blurb is the first non-empty line of the markdown section, which
36//! the parser drops (it keeps only the structured [`FkcKernel`], FKC §3.1). So
37//! the structured-vs-prose comparison is not available to a validator that
38//! operates on the parsed form. We implement the *structured* half of the rule
39//! (the `blurb:` field must be a non-empty one-line string → `MissingBlurb`) and
40//! leave the prose-vs-structured re-render diff to the `fkc fmt --check` lint
41//! that has the raw markdown ([`FkcError::BlurbMismatch`] stays reserved for it).
42
43use crate::fkc::error::FkcError;
44use crate::fkc::lower;
45use crate::fkc::schema::{
46    AcceptBlock, CapsBlock, CostBlock, CostMemory, FdxSpec, FkcFile, FkcFrontMatter, FkcKernel,
47    FkcProvider, GatherSpec, LayoutSpec, OpParamFieldSpec, OpParamsSchema, OutputDesc,
48    PrecisionBlock, QuantSpec, ReturnBlock, TensorDesc,
49};
50// Rule 7's op-param allowlists are generated from — and held against — these
51// two enums by `variant_table!`; see the V-FKC-5 note above `variant_table!`.
52use crate::kernel::OpParams;
53use fuel_graph::registry::FusedOpParams;
54
55/// The exact set of `fkc_version` values this importer accepts (FKC §10.1 / §11;
56/// KISS #450, narrowing §6.1-0008).
57///
58/// **Membership, not a `<= max` bound.** A version is accepted IFF it is in this
59/// set; a reader MUST NOT accept a version merely because it is *lower* than one
60/// it supports. (GAP-292: the old gate `fkc_version > MAX` accepted `0`, so the
61/// accepted set was silently `{0, 1}` — and `0` is not a version.) A reader
62/// DECLARING its supported set here is exactly what KISS #450 requires.
63///
64/// # Two independent version axes (GAP-038 — KISS-CONTRACT §8-0001..0007)
65///
66/// `fkc_version` is the **wire/schema** axis: the format of the contract file.
67/// The **second** axis is the producing **crate's published semver** (in
68/// `Cargo.toml` and the registry) — deliberately *not* stamped in any contract
69/// (KISS pins the Provenance block to a closed 5-field set with no crate-version
70/// field). The bump rule, transcribed from KISS-CONTRACT §8 and umbrella §5.1
71/// (verify at the KISS tree; this is a pointer, not the authority):
72///
73/// | change                                             | bumps |
74/// |----------------------------------------------------|-------|
75/// | a pure code refactor that preserves the bytes      | crate semver ONLY (§8-0001) |
76/// | a front-matter/section **schema** change (§8-0002) | the schema version — a new entry here |
77/// | an upstream **vocabulary** change — a new KISS-Ops  | NEITHER axis (§8-0003) |
78/// |   op name, a KISS-Grammar shape                    |       |
79///
80/// §8-0001: the two axes are INDEPENDENT — a crate-semver change MUST NOT imply a
81/// schema change, so this set is a literal and MUST NOT be derived from
82/// `CARGO_PKG_VERSION`. ⚠️ §8-0003 is the counter-intuitive one: an upstream
83/// op-name change is NOT a schema change and MUST NOT add an entry here. The
84/// compile-time pin below enforces §8-0002 while, by pinning the schema STRUCTS
85/// rather than the op vocabulary, leaving §8-0003's vocabulary changes untouched.
86pub const FKC_SUPPORTED_VERSIONS: &[u32] = &[1];
87
88/// The single source of truth for "does this importer accept `fkc_version`?"
89/// (KISS #450 membership). Both the production gate in [`validate_file`] and the
90/// checked-in-corpus conformance test call THIS, so the two cannot diverge on the
91/// accepted set — the drift a duplicated inline `> MAX` check invited (GAP-292).
92pub(crate) fn fkc_version_is_supported(version: u32) -> bool {
93    FKC_SUPPORTED_VERSIONS.contains(&version)
94}
95
96/// GAP-038 + GAP-297 / §8-0002 — compile-time schema pin for the ENTIRE FKC
97/// contract schema. Adding, removing, or retyping a field on ANY
98/// `#[derive(Deserialize)]` struct in [`crate::fkc::schema`] fails to compile in
99/// the matching `_fkc_pin_*` fn below (every destructure is exhaustive), forcing
100/// the schema-vs-version decision to be made deliberately: a schema change is a
101/// §8-0002 change and MUST add a new entry to [`FKC_SUPPORTED_VERSIONS`] (and bump
102/// `PINNED_FKC_SCHEMA_VERSION` in the test below).
103///
104/// ⚠️ It pins STRUCTS, not the op vocabulary, so a §8-0003 vocabulary change does
105/// NOT trip it — the guard fires on schema drift, never vocab drift.
106///
107/// GAP-297 DISCHARGES the floor GAP-038 documented: the pin covers the top-level
108/// front-matter AND the `provider` sub-struct AND the whole kernel-block tree
109/// (`accept`/`return`/tensor/layout/fdx/quant/gather/caps/cost/precision). Pinning
110/// only the top level would just relocate the floor one struct down, so EVERY
111/// Deserialize struct is destructured. [`FkcFile`] is deliberately EXCLUDED — it is
112/// the parsed RESULT (`#[derive(Debug, Clone)]`, not `Deserialize`), so it is not
113/// part of the wire schema.
114///
115/// SPLIT INTO GROUPS by block (front-matter / kernel / accept / return / tensor /
116/// caps-cost-precision) so each fn stays under the method-length + parameter-count
117/// limits, coverage unchanged — all 17 structs are still exhaustively destructured
118/// across the six functions (GAP-297 Codacy follow-up). If a new schema struct is
119/// added, destructure it in the group it belongs to (or add a group) — a struct
120/// with no `_fkc_pin_*` arm is a pin the guard cannot enforce.
121///
122/// Never called: an unreferenced private fn is still fully type-checked (the
123/// `dead_code` lint runs *after* type-checking), so the destructures are enforced
124/// on every build; `/// arm binds every field to `_`, which reads (and thereby pins) the field set.
125fn _fkc_pin_front_matter(front_matter: FkcFrontMatter, provider: FkcProvider) {
126    let FkcFrontMatter {
127        fkc_version: _,
128        provider: _,
129    } = front_matter;
130    let FkcProvider {
131        name: _,
132        backend: _,
133        kernel_source: _,
134        link_registry: _,
135        revision_base: _,
136    } = provider;
137}
138
139fn _fkc_pin_kernel(kernel: FkcKernel) {
140    let FkcKernel {
141        kernel: _,
142        registrable: _,
143        op_kind: _,
144        fused_op: _,
145        blurb: _,
146        backend: _,
147        kernel_source: _,
148        entry_point: _,
149        kernel_revision_hash: _,
150        variant: _,
151        accept: _,
152        return_: _,
153        caps: _,
154        cost: _,
155        precision: _,
156        determinism: _,
157    } = kernel;
158}
159
160fn _fkc_pin_accept(
161    accept: AcceptBlock,
162    op_params: OpParamsSchema,
163    op_param_field: OpParamFieldSpec,
164) {
165    let AcceptBlock {
166        inputs: _,
167        op_params: _,
168    } = accept;
169    let OpParamsSchema {
170        variant: _,
171        fields: _,
172    } = op_params;
173    let OpParamFieldSpec {
174        kind: _,
175        constraint: _,
176        note: _,
177    } = op_param_field;
178}
179
180fn _fkc_pin_return(ret: ReturnBlock, output: OutputDesc) {
181    let ReturnBlock {
182        outputs: _,
183        bundle: _,
184    } = ret;
185    let OutputDesc {
186        name: _,
187        dtype_rule: _,
188        shape_rule: _,
189        layout_guarantee: _,
190        aliasing: _,
191    } = output;
192}
193
194fn _fkc_pin_tensor(
195    tensor: TensorDesc,
196    layout: LayoutSpec,
197    fdx: FdxSpec,
198    quant: QuantSpec,
199    gather: GatherSpec,
200) {
201    let TensorDesc {
202        name: _,
203        optional: _,
204        dtypes: _,
205        dtype_class: _,
206        layout: _,
207        rank: _,
208        shape_constraint: _,
209        fdx: _,
210        device: _,
211        substrate: _,
212    } = tensor;
213    let LayoutSpec {
214        contiguous: _,
215        strided: _,
216        broadcast_stride0: _,
217        broadcast_axes: _,
218        start_offset: _,
219        reverse_strides: _,
220        awkward_layout_strategy: _,
221    } = layout;
222    let FdxSpec {
223        requires_ext: _,
224        quant: _,
225        sub_byte: _,
226        symbolic_extent: _,
227        extent_kind: _,
228        gather: _,
229    } = fdx;
230    let QuantSpec {
231        family: _,
232        ggml_dtype: _,
233        granularity: _,
234        block_shape: _,
235        role: _,
236        scale_operand: _,
237    } = quant;
238    let GatherSpec {
239        kind: _,
240        block_table: _,
241        context_lens: _,
242    } = gather;
243}
244
245fn _fkc_pin_caps_cost_precision(
246    caps: CapsBlock,
247    cost: CostBlock,
248    cost_memory: CostMemory,
249    precision: PrecisionBlock,
250) {
251    let CapsBlock {
252        awkward_layout_strategy: _,
253        fast_paths: _,
254        in_place: _,
255        alignment_bytes: _,
256        access_granularity_bits: _,
257    } = caps;
258    let CostBlock {
259        provenance: _,
260        class: _,
261        cost_fn: _,
262        flops: _,
263        bytes_moved: _,
264        overhead_ns: _,
265        memory: _,
266    } = cost;
267    let CostMemory {
268        device_bytes: _,
269        host_bytes: _,
270        disk_bytes: _,
271    } = cost_memory;
272    let PrecisionBlock {
273        bit_stable_on_same_hardware: _,
274        max_ulp: _,
275        max_relative: _,
276        max_absolute: _,
277        audited: _,
278        notes: _,
279    } = precision;
280}
281
282// ===========================================================================
283// FDX normative token tables (§10.16 drift-guard)
284// ===========================================================================
285//
286// FKC re-numbers nothing; FDX owns the codes (§0). The dtype token resolves
287// through `lower::lower_dtype` (the same `DType` set FDX §6.1 owns); the
288// quant `family` / `granularity` / `ggml_dtype` tokens resolve through the
289// tables below, which mirror the FDX §6.2 symbol sets. A token absent from
290// these is `FdxTokenNotInTable` (rule 16) — so FKC's set stays a subset of
291// FDX's and the two specs cannot drift.
292
293/// FDX `FDXQuant.family` symbols (FDX §6.2): `NONE | GGML_BLOCK | MX |
294/// AFFINE_INT | AFFINE_FLOAT | AFFINE_BLOCK`. `"none"` is accepted in either
295/// case (the corpus writes `none`).
296fn is_fdx_quant_family(tok: &str) -> bool {
297    matches!(
298        tok,
299        "none" | "NONE" | "GGML_BLOCK" | "MX" | "AFFINE_INT" | "AFFINE_FLOAT" | "AFFINE_BLOCK"
300    )
301}
302
303/// FDX `FDXScaleGranularity` symbols (FDX §6.2): `PerTensor | PerToken |
304/// PerChannel | PerBlock`.
305fn is_fdx_granularity(tok: &str) -> bool {
306    matches!(tok, "PerTensor" | "PerToken" | "PerChannel" | "PerBlock")
307}
308
309/// The as-built `GgmlDType` variant set (`fuel-core-types/src/quantized.rs`),
310/// matched **by code** per FKC §3.4 — i.e. these are exactly the variant
311/// names, and `Q4_K_M` (a GGUF file-format name, NOT a `GgmlDType` variant) is
312/// deliberately absent (writing it fails §10.6 / rule 16).
313fn ggml_dtype_code(tok: &str) -> Option<u32> {
314    let code = match tok {
315        "F32" => 0,
316        "F16" => 1,
317        "Q4_0" => 2,
318        "Q4_1" => 3,
319        "Q5_0" => 6,
320        "Q5_1" => 7,
321        "Q8_0" => 8,
322        "Q8_1" => 9,
323        "Q2K" => 10,
324        "Q3K" => 11,
325        "Q4K" => 12,
326        "Q5K" => 13,
327        "Q6K" => 14,
328        "Q8K" => 15,
329        "BF16" => 30,
330        _ => return None,
331    };
332    Some(code)
333}
334
335/// The `ScaleGranularity` set that has an as-built `fuel-core-types`
336/// counterpart (`quant_scale.rs`): `{ PerTensor, PerToken, PerChannel }`.
337/// `PerBlock` is FDX/FKC-only with no target type yet (FKC §6 / §10.6).
338fn is_registrable_granularity(tok: &str) -> bool {
339    matches!(tok, "PerTensor" | "PerToken" | "PerChannel")
340}
341
342// ===========================================================================
343// Public entry points
344// ===========================================================================
345
346/// Run the full `V-FKC-*` battery over a parsed file. The caller (the lint /
347/// `import_bundle_str`) runs this after parse so a structurally-bad contract
348/// fails import. Stops at the first violation (a typed `FkcError`).
349pub fn validate_file(file: &FkcFile) -> Result<(), FkcError> {
350    // Rule 1: format version supported (membership, not `<= max` — KISS #450).
351    if !fkc_version_is_supported(file.front_matter.fkc_version) {
352        return Err(FkcError::UnsupportedVersion {
353            found: file.front_matter.fkc_version,
354            supported: FKC_SUPPORTED_VERSIONS,
355        });
356    }
357    for kernel in &file.kernels {
358        match validate_kernel(kernel) {
359            Ok(()) => {}
360            // §3.10 + §14/§6: a describe-only (`registrable: false`) section is
361            // documentation — it carries no dispatch target and is excluded from
362            // lowering/registration (`lower_file` filters it). It may legitimately
363            // trip a CONSUMER-AHEAD gate — an `fdx.gather` operand
364            // (`GatherNotYetSupported`, rule 14) or an MX/AFFINE_BLOCK quant
365            // (`MxNotYetRegistrable`, rule 6) — which is a CORRECT
366            // "describable-but-not-yet-registrable" outcome, NOT a defect. Such a
367            // describe-only gate MUST NOT block a bundle's importable sections, so
368            // it is swallowed HERE (the same "deferred" posture the corpus CI lint
369            // takes). Every OTHER error still fails import — a describe-only
370            // section's DESCRIPTIVE checks (bad dtype, incoherent layout, …) are
371            // still enforced, and a REGISTRABLE section's consumer-ahead gate still
372            // fails (it WOULD try to register).
373            Err(FkcError::GatherNotYetSupported { .. } | FkcError::MxNotYetRegistrable { .. })
374                if !kernel.registrable => {}
375            Err(e) => return Err(e),
376        }
377    }
378    Ok(())
379}
380
381/// Validate one parsed kernel section against the structural + coherence
382/// rules (every rule that operates on the parsed form). The duplicate-
383/// `KernelRef` rule (10) is a *register-time* check (`finalize`), not here.
384pub fn validate_kernel(kernel: &FkcKernel) -> Result<(), FkcError> {
385    let section = kernel.kernel.as_str();
386
387    // §3.10 (rule 17): a describe-only section is documentation. SKIP the
388    // dispatch-resolution checks (exactly-one-of op_kind/fused_op, op resolves,
389    // op-param namespace) but STILL run every descriptive check below.
390    let describe_only = !kernel.registrable;
391
392    // Rule 2: required fields (the describe-only exceptions are applied inside).
393    required_fields(kernel, section, describe_only)?;
394
395    // Determine the op target namespace (primitive vs fused) for rule 7. For a
396    // describe-only section there is no resolved dispatch target, so the
397    // namespace check is skipped (and the op token may be `~` / descriptive).
398    let is_fused = if describe_only {
399        // No dispatch resolution; op_kind/fused_op need not name a real target.
400        // (A descriptive token like `binary`, or `~`, is legal here — §3.10.)
401        false
402    } else {
403        match (kernel.op_kind.as_deref(), kernel.fused_op.as_deref()) {
404            (Some(op), None) => {
405                // Rule 2/3: op_kind token resolves (reuse the lower table).
406                lower::lower_op_kind(op, section)?;
407                false
408            }
409            (None, Some(fused)) => {
410                // fused_op token resolves through the SCREAMING_SNAKE table.
411                lower::lower_fused_op(fused, section)?;
412                true
413            }
414            (op, fused) => {
415                return Err(FkcError::OpTargetAmbiguous {
416                    section: section.to_string(),
417                    op_kind: op.map(String::from),
418                    fused_op: fused.map(String::from),
419                });
420            }
421        }
422    };
423
424    // Per-operand checks (rules 3, 4, 5, 6, 14, 15, 16) — descriptive, run for
425    // describe-only sections too.
426    if let Some(accept) = &kernel.accept {
427        for d in &accept.inputs {
428            let operand = d.name.as_deref().unwrap_or("<input>").to_string();
429            validate_operand(section, &operand, d, accept)?;
430        }
431        // Rule 7: op-param variant in the correct namespace — SKIPPED for a
432        // describe-only section (no resolved dispatch target to bind against).
433        if !describe_only
434            && let Some(op_params) = &accept.op_params
435            && let Some(variant) = op_params.variant.as_deref()
436        {
437            validate_op_params_namespace(section, variant, is_fused)?;
438        }
439    }
440
441    // Rule 16 also covers the OUTPUT dtype tokens (`fixed(DType)` rules) — and
442    // rule 13 covers bundle slot ranks. Returns are validated here.
443    if let Some(ret) = &kernel.return_ {
444        for out in &ret.outputs {
445            // `fixed(DType)` output dtype rules name a dtype token; resolve it
446            // through the lower table (also covers rule 16 dtype membership).
447            if let Some(rule) = out.dtype_rule.as_deref() {
448                check_output_dtype_rule(section, out.name.as_deref().unwrap_or("<output>"), rule)?;
449            }
450        }
451        // Rule 13: bundle slot rank ≤ 6 (when a static rank is declarable).
452        if let Some(bundle) = &ret.bundle {
453            check_bundle_ranks(section, bundle)?;
454        }
455    }
456
457    // Rule 8/8a: cost expressions parse + provenance present + non-placeholder.
458    validate_cost(kernel, section)?;
459
460    // Rule 9 (precision coverage): nondeterministic ⇒ audited + no bit-stable.
461    validate_precision_coherence(kernel, section)?;
462
463    Ok(())
464}
465
466// ===========================================================================
467// Rule 2 — required fields
468// ===========================================================================
469
470fn required_fields(kernel: &FkcKernel, section: &str, describe_only: bool) -> Result<(), FkcError> {
471    // kernel name is always present (it is the struct's required field).
472    // exactly-one-of op_kind/fused_op is checked in validate_kernel's match.
473
474    // blurb: a non-empty one-line string.
475    match kernel.blurb.as_deref() {
476        Some(b) if !b.trim().is_empty() => {}
477        _ => {
478            return Err(FkcError::MissingBlurb {
479                section: section.to_string(),
480            });
481        }
482    }
483
484    // entry_point.
485    require(kernel.entry_point.as_deref(), section, "entry_point")?;
486
487    // ≥1 accept.inputs — EXCEPT a describe-only section (§3.10): a
488    // documentation-only zero-operand op is legitimate (a zero-input random
489    // fill like `rand_uniform`/`rand_normal` whose only "input" is backend-
490    // private RNG state, not a graph tensor). The ≥1-input requirement is
491    // still enforced for every REGISTRABLE section (a real dispatch target
492    // must consume at least one graph operand).
493    if !describe_only {
494        let has_inputs = kernel
495            .accept
496            .as_ref()
497            .map(|a| !a.inputs.is_empty())
498            .unwrap_or(false);
499        if !has_inputs {
500            return Err(FkcError::MissingRequiredField {
501                section: section.to_string(),
502                field: "accept.inputs (≥1)".to_string(),
503            });
504        }
505    }
506
507    // ≥1 return.outputs OR a bundle.
508    let has_outputs = kernel
509        .return_
510        .as_ref()
511        .map(|r| !r.outputs.is_empty() || r.bundle.is_some())
512        .unwrap_or(false);
513    if !has_outputs {
514        return Err(FkcError::MissingRequiredField {
515            section: section.to_string(),
516            field: "return.outputs (≥1) or return.bundle".to_string(),
517        });
518    }
519
520    // a cost block.
521    if kernel.cost.is_none() {
522        return Err(FkcError::MissingRequiredField {
523            section: section.to_string(),
524            field: "cost".to_string(),
525        });
526    }
527    // a precision block.
528    if kernel.precision.is_none() {
529        return Err(FkcError::MissingRequiredField {
530            section: section.to_string(),
531            field: "precision".to_string(),
532        });
533    }
534    // determinism.
535    require(kernel.determinism.as_deref(), section, "determinism")?;
536
537    Ok(())
538}
539
540fn require(v: Option<&str>, section: &str, field: &str) -> Result<(), FkcError> {
541    match v {
542        Some(s) if !s.trim().is_empty() => Ok(()),
543        _ => Err(FkcError::MissingRequiredField {
544            section: section.to_string(),
545            field: field.to_string(),
546        }),
547    }
548}
549
550// ===========================================================================
551// Per-operand: rules 3, 4, 5, 6, 14, 15, 16
552// ===========================================================================
553
554fn validate_operand(
555    section: &str,
556    operand: &str,
557    d: &TensorDesc,
558    accept: &crate::fkc::schema::AcceptBlock,
559) -> Result<(), FkcError> {
560    // Rule 3 / Rule 16: every dtype token is a real DType (and ∈ FDX table).
561    for tok in &d.dtypes {
562        lower::lower_dtype(tok, section, operand).map_err(|_| FkcError::FdxTokenNotInTable {
563            section: section.to_string(),
564            field: format!("{operand}.dtypes"),
565            token: tok.clone(),
566        })?;
567    }
568
569    // Rule 4: layout coherence.
570    if let Some(layout) = &d.layout {
571        layout_coherence(section, operand, layout)?;
572    }
573
574    // Rule 5: awkward-layout coherence PER OPERAND (effective strategy).
575    awkward_strategy_coherence(section, operand, d, accept)?;
576
577    // Rule 3/6/16: quant coherence (incl. sub-byte ⇒ fdx.quant, scale single-
578    // place, ggml/family/granularity membership, MX/AFFINE_BLOCK gate).
579    if let Some(fdx) = &d.fdx {
580        // Rule 3: a sub-byte base dtype MUST carry an fdx.quant block.
581        let is_sub_byte = d
582            .dtypes
583            .iter()
584            .any(|t| matches!(t.as_str(), "F4" | "F6E2M3" | "F6E3M2"));
585        if is_sub_byte && fdx.quant.is_none() && fdx.sub_byte.is_none() {
586            return Err(FkcError::QuantIncoherent {
587                section: section.to_string(),
588                operand: operand.to_string(),
589                reason: "sub-byte dtype requires an fdx.sub_byte + fdx.quant block (no reliance \
590                         on size_in_bytes()==0)"
591                    .to_string(),
592            });
593        }
594
595        if let Some(quant) = &fdx.quant {
596            quant_coherence(section, operand, quant, accept)?;
597        }
598
599        // Rule 14: gather (paged) coherence.
600        if let Some(gather) = &fdx.gather {
601            gather_coherence(section, operand, gather, fdx, accept)?;
602        }
603
604        // Rule 15: affine / symbolic extent coherence.
605        extent_coherence(section, operand, fdx)?;
606    }
607
608    Ok(())
609}
610
611// --- Rule 4: layout coherence ---
612
613fn layout_coherence(
614    section: &str,
615    operand: &str,
616    layout: &crate::fkc::schema::LayoutSpec,
617) -> Result<(), FkcError> {
618    use crate::fkc::caps_map::{Tri, resolve_layout};
619    // Parse the five flags (also validates each value token; bad value →
620    // BadLayoutFlag from resolve_layout).
621    let r = resolve_layout(Some(layout), section, operand)?;
622
623    let contiguous_ok = matches!(r.contiguous, Tri::Required | Tri::Accepted);
624    let strided_ok = matches!(r.strided, Tri::Accepted);
625
626    // At least one of contiguous / strided must be acceptable.
627    if !contiguous_ok && !strided_ok {
628        return Err(FkcError::LayoutIncoherent {
629            section: section.to_string(),
630            operand: operand.to_string(),
631            reason: "neither `contiguous` (required|accepted) nor `strided: accepted` is set — \
632                     the kernel accepts no layout"
633                .to_string(),
634        });
635    }
636
637    // broadcast_stride0: accepted ⇒ strided: accepted.
638    if matches!(r.broadcast_stride0, Tri::Accepted) && !strided_ok {
639        return Err(FkcError::LayoutIncoherent {
640            section: section.to_string(),
641            operand: operand.to_string(),
642            reason: "`broadcast_stride0: accepted` requires `strided: accepted` (broadcast is a \
643                     stride-0 special case)"
644                .to_string(),
645        });
646    }
647    // reverse_strides: accepted ⇒ strided: accepted.
648    if matches!(r.reverse_strides, Tri::Accepted) && !strided_ok {
649        return Err(FkcError::LayoutIncoherent {
650            section: section.to_string(),
651            operand: operand.to_string(),
652            reason: "`reverse_strides: accepted` requires `strided: accepted` (a negative stride \
653                     is still a strided walk)"
654                .to_string(),
655        });
656    }
657
658    // broadcast_axes ⟺ broadcast_stride0: required (§6-additive mask). The
659    // mask names the iteration axes the operand must be stride-0 on; it is
660    // meaningful ONLY for a REQUIRED (baked) broadcast, and REQUIRED needs it
661    // — the shape-blind (op, dtypes, backend) binder can't tell a correctly-
662    // broadcast operand from a wrongly-shaped one without the axis set. So:
663    // present iff required, and non-empty when required.
664    match (
665        &layout.broadcast_axes,
666        matches!(r.broadcast_stride0, Tri::Required),
667    ) {
668        (Some(axes), true) if axes.is_empty() => {
669            return Err(FkcError::LayoutIncoherent {
670                section: section.to_string(),
671                operand: operand.to_string(),
672                reason: "`broadcast_stride0: required` with an EMPTY `broadcast_axes` — name the \
673                         iteration axes the operand must be stride-0 on"
674                    .to_string(),
675            });
676        }
677        (Some(_), false) => {
678            return Err(FkcError::LayoutIncoherent {
679                section: section.to_string(),
680                operand: operand.to_string(),
681                reason: "`broadcast_axes` set without `broadcast_stride0: required` — the mask is \
682                         meaningful only for a required (baked) broadcast"
683                    .to_string(),
684            });
685        }
686        (None, true) => {
687            return Err(FkcError::LayoutIncoherent {
688                section: section.to_string(),
689                operand: operand.to_string(),
690                reason: "`broadcast_stride0: required` needs `broadcast_axes` — the shape-blind \
691                         binder can't verify the broadcast without the axis mask"
692                    .to_string(),
693            });
694        }
695        _ => {}
696    }
697    Ok(())
698}
699
700// --- Rule 5: awkward-layout strategy coherence (per operand) ---
701
702fn awkward_strategy_coherence(
703    section: &str,
704    operand: &str,
705    d: &TensorDesc,
706    _accept: &crate::fkc::schema::AcceptBlock,
707) -> Result<(), FkcError> {
708    use crate::fkc::caps_map::{Tri, resolve_layout};
709
710    // Effective strategy: per-operand override, else (handled by caller for the
711    // kernel-wide default — but the per-operand check only needs the per-operand
712    // value when present, since the kernel-wide default is validated against
713    // each operand by the caps default below).
714    let per_operand = d
715        .layout
716        .as_ref()
717        .and_then(|l| l.awkward_layout_strategy.as_deref());
718
719    let Some(strategy) = per_operand else {
720        // No per-operand override: inheritance from the kernel-wide default is
721        // checked at the kernel level via the operands' flags; nothing to do.
722        return Ok(());
723    };
724
725    let r = resolve_layout(d.layout.as_ref(), section, operand)?;
726    match strategy {
727        "handles_strided" => {
728            if !matches!(r.strided, Tri::Accepted) {
729                return Err(FkcError::AwkwardStrategyIncoherent {
730                    section: section.to_string(),
731                    operand: operand.to_string(),
732                    strategy: strategy.to_string(),
733                    reason: "`handles_strided` requires `strided: accepted` on this operand"
734                        .to_string(),
735                });
736            }
737        }
738        "requires_contiguous" => {
739            if !matches!(r.contiguous, Tri::Required) {
740                return Err(FkcError::AwkwardStrategyIncoherent {
741                    section: section.to_string(),
742                    operand: operand.to_string(),
743                    strategy: strategy.to_string(),
744                    reason: "`requires_contiguous` requires `contiguous: required` on this operand"
745                        .to_string(),
746                });
747            }
748        }
749        "contiguize_internally" => {
750            // Folds this operand's copy into the kernel's bytes_moved; it must
751            // at least *accept* strided input (otherwise there is nothing to
752            // contiguize internally).
753            if !matches!(r.strided, Tri::Accepted) {
754                return Err(FkcError::AwkwardStrategyIncoherent {
755                    section: section.to_string(),
756                    operand: operand.to_string(),
757                    strategy: strategy.to_string(),
758                    reason: "`contiguize_internally` requires `strided: accepted` on this operand \
759                             (it accepts strided then copies internally)"
760                        .to_string(),
761                });
762            }
763        }
764        other => {
765            return Err(FkcError::AwkwardStrategyIncoherent {
766                section: section.to_string(),
767                operand: operand.to_string(),
768                strategy: other.to_string(),
769                reason: "unknown awkward_layout_strategy (meaning-bearing — §11.1)".to_string(),
770            });
771        }
772    }
773    Ok(())
774}
775
776// --- Rule 6 + 16: quant coherence ---
777
778fn quant_coherence(
779    section: &str,
780    operand: &str,
781    quant: &QuantSpec,
782    accept: &crate::fkc::schema::AcceptBlock,
783) -> Result<(), FkcError> {
784    let family = quant.family.as_deref().unwrap_or("none");
785
786    // Rule 16: family token ∈ FDX table.
787    if !is_fdx_quant_family(family) {
788        return Err(FkcError::FdxTokenNotInTable {
789            section: section.to_string(),
790            field: format!("{operand}.fdx.quant.family"),
791            token: family.to_string(),
792        });
793    }
794    // Rule 16: granularity token ∈ FDX table (when present, non-null).
795    if let Some(g) = quant.granularity.as_deref()
796        && !is_fdx_granularity(g)
797    {
798        return Err(FkcError::FdxTokenNotInTable {
799            section: section.to_string(),
800            field: format!("{operand}.fdx.quant.granularity"),
801            token: g.to_string(),
802        });
803    }
804    // Rule 16 + 3: ggml_dtype is a real GgmlDType variant (by code).
805    if let Some(g) = quant.ggml_dtype.as_deref()
806        && ggml_dtype_code(g).is_none()
807    {
808        // `Q4_K_M` (GGUF name, NOT a variant) lands here.
809        return Err(FkcError::QuantIncoherent {
810            section: section.to_string(),
811            operand: operand.to_string(),
812            reason: format!(
813                "ggml_dtype `{g}` is not a real GgmlDType variant (matched by code; \
814                     `Q4_K_M` is a GGUF file-format name → use `Q4K`)"
815            ),
816        });
817    }
818
819    // Scale single-place rule: a separate scale_operand XOR a sidecar scale,
820    // never both for the same scale. We model the "sidecar scale" as a
821    // sidecar `scale_buffer` (not in the schema today) — so we check that
822    // `scale_operand`, when present, names a REAL accept.inputs role and that
823    // it is not also a GGML INLINE family (GGML scales are baked, never a
824    // separate operand) — that is the double-declaration we can detect here.
825    if let Some(scale_role) = quant.scale_operand.as_deref() {
826        // The role must be a real input operand (single-place: it IS the
827        // authority, so it must exist).
828        let names_real = accept
829            .inputs
830            .iter()
831            .any(|i| i.name.as_deref() == Some(scale_role));
832        if !names_real {
833            return Err(FkcError::ScaleDoubleDeclared {
834                section: section.to_string(),
835                operand: operand.to_string(),
836                reason: format!(
837                    "scale_operand `{scale_role}` does not name a real accept.inputs role"
838                ),
839            });
840        }
841        // GGML_BLOCK scales are INLINE-baked; a separate scale_operand for a
842        // GGML family is the double-declaration §10.6 forbids.
843        if family == "GGML_BLOCK" {
844            return Err(FkcError::ScaleDoubleDeclared {
845                section: section.to_string(),
846                operand: operand.to_string(),
847                reason: "GGML_BLOCK bakes its scale INLINE (per ggml_dtype) — it must NOT also \
848                         declare a separate scale_operand"
849                    .to_string(),
850            });
851        }
852    }
853
854    // Per-family coherence (§10.6).
855    match family {
856        "none" | "NONE" => {
857            // Dense — no ggml_dtype, no granularity, no scale.
858        }
859        "GGML_BLOCK" => {
860            // Requires a real ggml_dtype; carries NO granularity / NO PerBlock.
861            if quant.ggml_dtype.is_none() {
862                return Err(FkcError::QuantIncoherent {
863                    section: section.to_string(),
864                    operand: operand.to_string(),
865                    reason: "GGML_BLOCK requires a `ggml_dtype` (the block format)".to_string(),
866                });
867            }
868            if quant.granularity.is_some() {
869                return Err(FkcError::QuantIncoherent {
870                    section: section.to_string(),
871                    operand: operand.to_string(),
872                    reason: "GGML_BLOCK carries NO granularity (the scale is baked INLINE per \
873                             ggml_dtype; no ScalePair, no PerBlock)"
874                        .to_string(),
875                });
876            }
877        }
878        "AFFINE_INT" | "AFFINE_FLOAT" => {
879            // granularity ∈ {PerTensor, PerToken, PerChannel} (NOT PerBlock).
880            match quant.granularity.as_deref() {
881                Some(g) if is_registrable_granularity(g) => {}
882                Some("PerBlock") => {
883                    return Err(FkcError::QuantIncoherent {
884                        section: section.to_string(),
885                        operand: operand.to_string(),
886                        reason: format!(
887                            "{family} granularity must be PerTensor|PerToken|PerChannel \
888                             (PerBlock is MX-only)"
889                        ),
890                    });
891                }
892                _ => {
893                    return Err(FkcError::QuantIncoherent {
894                        section: section.to_string(),
895                        operand: operand.to_string(),
896                        reason: format!(
897                            "{family} requires granularity ∈ PerTensor|PerToken|PerChannel"
898                        ),
899                    });
900                }
901            }
902        }
903        "MX" => {
904            // MX ⇒ granularity: PerBlock; parse-validates but NOT registrable.
905            if quant.granularity.as_deref() != Some("PerBlock") {
906                return Err(FkcError::QuantIncoherent {
907                    section: section.to_string(),
908                    operand: operand.to_string(),
909                    reason: "MX requires granularity: PerBlock (F8E8M0 per-block scale)"
910                        .to_string(),
911                });
912            }
913            return Err(FkcError::MxNotYetRegistrable {
914                section: section.to_string(),
915                family: "MX".to_string(),
916                reason: "no ScaleGranularity::PerBlock target type yet (§6)".to_string(),
917            });
918        }
919        "AFFINE_BLOCK" => {
920            // Block geometry present — EITHER `block_shape` OR a block-shaped
921            // SEPARATE scale operand (§10.6: "block geometry present
922            // (`block_shape` / a block-shaped separate scale operand)"). Both
923            // forms are legal; a contract that carries the per-block absmax as
924            // a separate `scale_operand` (the single-place SEPARATE_BUFFER
925            // form) does not also need an inline `block_shape`. Granularity is
926            // NOT PerBlock (its grain rides block_shape / the absmax operand).
927            // Parse-validates but is NOT registrable yet (no block-quant
928            // descriptor target).
929            let has_block_shape = quant.block_shape.is_some();
930            let has_separate_scale = quant.scale_operand.is_some();
931            if !has_block_shape && !has_separate_scale {
932                return Err(FkcError::QuantIncoherent {
933                    section: section.to_string(),
934                    operand: operand.to_string(),
935                    reason: "AFFINE_BLOCK requires block geometry — either `block_shape` or a \
936                             block-shaped separate `scale_operand` (§10.6)"
937                        .to_string(),
938                });
939            }
940            if quant.granularity.as_deref() == Some("PerBlock") {
941                return Err(FkcError::QuantIncoherent {
942                    section: section.to_string(),
943                    operand: operand.to_string(),
944                    reason: "AFFINE_BLOCK must NOT use granularity: PerBlock (PerBlock stays \
945                             MX-only; its grain rides block_shape)"
946                        .to_string(),
947                });
948            }
949            return Err(FkcError::MxNotYetRegistrable {
950                section: section.to_string(),
951                family: "AFFINE_BLOCK".to_string(),
952                reason: "no block-quant descriptor target type yet (§6)".to_string(),
953            });
954        }
955        _ => unreachable!("family membership already checked against the FDX table"),
956    }
957
958    Ok(())
959}
960
961// --- Rule 14: gather (paged) coherence ---
962
963fn gather_coherence(
964    section: &str,
965    operand: &str,
966    gather: &crate::fkc::schema::GatherSpec,
967    fdx: &crate::fkc::schema::FdxSpec,
968    accept: &crate::fkc::schema::AcceptBlock,
969) -> Result<(), FkcError> {
970    let Some(kind) = gather.kind.as_deref() else {
971        // `kind: ~` ⇒ no gather declared.
972        return Ok(());
973    };
974    match kind {
975        "paged_blocks" => {
976            // (a) requires_ext: true.
977            if fdx.requires_ext != Some(true) {
978                return Err(FkcError::GatherIncoherent {
979                    section: section.to_string(),
980                    operand: operand.to_string(),
981                    reason: "paged_blocks requires `fdx.requires_ext: true`".to_string(),
982                });
983            }
984            // (b) symbolic_extent: required.
985            if fdx.symbolic_extent.as_deref() != Some("required") {
986                return Err(FkcError::GatherIncoherent {
987                    section: section.to_string(),
988                    operand: operand.to_string(),
989                    reason: "paged_blocks requires `fdx.symbolic_extent: required`".to_string(),
990                });
991            }
992            // (c) block_table / context_lens, when non-~, name real input roles.
993            for (label, role) in [
994                ("block_table", gather.block_table.as_deref()),
995                ("context_lens", gather.context_lens.as_deref()),
996            ] {
997                if let Some(role) = role {
998                    let exists = accept
999                        .inputs
1000                        .iter()
1001                        .any(|i| i.name.as_deref() == Some(role));
1002                    if !exists {
1003                        return Err(FkcError::GatherIncoherent {
1004                            section: section.to_string(),
1005                            operand: operand.to_string(),
1006                            reason: format!(
1007                                "gather.{label} `{role}` does not name a real accept.inputs role"
1008                            ),
1009                        });
1010                    }
1011                }
1012            }
1013            // IMPORT-SIDE LIFT (FDX gather-sidecar arc, slice A). The `gather`
1014            // block is boundary METADATA that DESCRIBES what an FDX view of this
1015            // operand will someday carry (`FDXIndexedResidency`, FDX §6.9 —
1016            // "Description only: no cost, no decision"). Registration/dispatch of
1017            // the consuming op (`OpKind::PagedAttn`) does NOT depend on it: the
1018            // as-built ABI passes `block_table` / `context_lens` as ORDINARY U32
1019            // graph inputs (named by the coherence check above) + the geometry in
1020            // `OpParams::PagedAttn`; the kernel reads them directly, never through
1021            // an FDX gather view. So a COHERENT `paged_blocks` operand is
1022            // registrable NOW — we validated its internal coherence (kind /
1023            // requires_ext / symbolic_extent / real block_table+context_lens
1024            // roles); nothing further is a build-time gate. What remains
1025            // [consumer-ahead] is the FDX VIEW layer that would MATERIALIZE this
1026            // descriptor at the kernel boundary (`view_with_gather` +
1027            // `Capability::DlpackExtGather` direct-admission, FKC §3.9.1) — a
1028            // separate seam with no consumer yet, NOT the import gate. An
1029            // INCOHERENT gather still errors above (`GatherIncoherent`), never a
1030            // silent pass. (`GatherNotYetSupported` stays a reserved variant for
1031            // a future ragged/CSR `kind >= 2` that FKC cannot yet key.)
1032            Ok(())
1033        }
1034        other => Err(FkcError::UnknownAdmissibilityEnum {
1035            section: section.to_string(),
1036            field: format!("{operand}.fdx.gather.kind"),
1037            value: other.to_string(),
1038        }),
1039    }
1040}
1041
1042// --- Rule 15: affine / symbolic extent coherence ---
1043
1044fn extent_coherence(
1045    section: &str,
1046    operand: &str,
1047    fdx: &crate::fkc::schema::FdxSpec,
1048) -> Result<(), FkcError> {
1049    // symbolic_extent ∈ {rejected, tolerated, required} (when present).
1050    if let Some(se) = fdx.symbolic_extent.as_deref()
1051        && !matches!(se, "rejected" | "tolerated" | "required")
1052    {
1053        return Err(FkcError::UnknownAdmissibilityEnum {
1054            section: section.to_string(),
1055            field: format!("{operand}.fdx.symbolic_extent"),
1056            value: se.to_string(),
1057        });
1058    }
1059    // extent_kind ∈ {rejected, scalar, range, affine} (when present).
1060    if let Some(ek) = fdx.extent_kind.as_deref() {
1061        if !matches!(ek, "rejected" | "scalar" | "range" | "affine") {
1062            return Err(FkcError::UnknownAdmissibilityEnum {
1063                section: section.to_string(),
1064                field: format!("{operand}.fdx.extent_kind"),
1065                value: ek.to_string(),
1066            });
1067        }
1068        // extent_kind: range|affine ⇒ symbolic_extent: required.
1069        if matches!(ek, "range" | "affine") && fdx.symbolic_extent.as_deref() != Some("required") {
1070            return Err(FkcError::UnknownAdmissibilityEnum {
1071                section: section.to_string(),
1072                field: format!("{operand}.fdx.extent_kind"),
1073                value: format!(
1074                    "{ek} requires symbolic_extent: required (got {:?})",
1075                    fdx.symbolic_extent
1076                ),
1077            });
1078        }
1079    }
1080    Ok(())
1081}
1082
1083// ===========================================================================
1084// Rule 7 — op-param namespace
1085// ===========================================================================
1086
1087/// An `op_kind` contract names an `OpParams` variant; a `fused_op` contract
1088/// names a `FusedOpParams` variant (§3.7, §10.7). The two namespaces are
1089/// distinct; checking the wrong one is `BadOpParamsVariant`.
1090fn validate_op_params_namespace(
1091    section: &str,
1092    variant: &str,
1093    is_fused: bool,
1094) -> Result<(), FkcError> {
1095    let ok = if is_fused {
1096        is_fused_op_params_variant(variant)
1097    } else {
1098        is_op_params_variant(variant)
1099    };
1100    if ok {
1101        Ok(())
1102    } else {
1103        Err(FkcError::BadOpParamsVariant {
1104            section: section.to_string(),
1105            variant: variant.to_string(),
1106            namespace: if is_fused {
1107                "FusedOpParams"
1108            } else {
1109                "OpParams"
1110            }
1111            .to_string(),
1112        })
1113    }
1114}
1115
1116// --- V-FKC-5: the op-param allowlists, tied to the real enums -------------
1117//
1118// These two tables were hand-typed string literals with no link back to
1119// `OpParams`/`FusedOpParams`, and they drifted exactly as that shape
1120// predicts: `OpParams` grew `JitScalars` and `NonZeroIndices` and the
1121// allowlist stayed 42-of-44, so a contract naming the real `NonZeroIndices`
1122// variant was rejected as if it were a typo. A test asserting "the list is
1123// complete" would only have moved the drift from the list to the test, so
1124// the list is now GENERATED, from a declaration the compiler checks against
1125// the enum itself: `variant_table!` emits both an exhaustive `match` (adding,
1126// removing, or renaming a variant is a COMPILE error, not a silent
1127// divergence) and the `&[(name, exclusion)]` table the predicate reads. One
1128// source of truth, and drift is structurally impossible rather than
1129// test-detected.
1130//
1131// `None` in the second column = a contract may name this variant.
1132// `Some(reason)` = deliberately NOT contract-authorable, with the reason
1133// recorded at the entry — the distinction the old silent omission destroyed.
1134
1135/// Emit (a) a never-called exhaustive `match` over `$enum`, so the variant
1136/// list below cannot drift from the enum without failing to compile, and
1137/// (b) the `&[(&str, Option<&str>)]` table the `is_*_variant` predicate
1138/// reads. See the V-FKC-5 note above.
1139macro_rules! variant_table {
1140    ($enum:ident, $guard:ident, $table:ident, $($v:ident => $excluded:expr),+ $(,)?) => {
1141        /// Never called — it exists so the compiler checks the variant list
1142        /// below against the real enum. A new variant makes this match
1143        /// non-exhaustive; a removed or renamed one makes an arm unresolvable.
1144        fn $guard(p: &$enum) {
1145            match p { $( $enum::$v { .. } => (), )+ }
1146        }
1147
1148        /// `(variant name, why it is NOT contract-authorable)`.
1149        const $table: &[(&str, Option<&str>)] = &[ $( (stringify!($v), $excluded) ),+ ];
1150    };
1151}
1152
1153variant_table!(
1154    OpParams,
1155    _op_params_is_exhaustive,
1156    OP_PARAMS_VARIANTS,
1157    None => None,
1158    Reduce => None,
1159    Matmul => None,
1160    Conv1D => None,
1161    Conv2D => None,
1162    Conv2DEpilogue => None,
1163    ConvTranspose1D => None,
1164    ConvTranspose2D => None,
1165    ReduceSumTo => None,
1166    ReduceMaxTo => None,
1167    ReduceMaxToBackward => None,
1168    FlashAttn => None,
1169    PagedAttn => None,
1170    Slice => None,
1171    Cast => None,
1172    Affine => None,
1173    Clamp => None,
1174    PowI => None,
1175    Concat => None,
1176    SoftmaxLastDim => None,
1177    NormLastDim => None,
1178    IndexSelect => None,
1179    Gather => None,
1180    Rope => None,
1181    QMatMul => None,
1182    IndexAdd => None,
1183    ScatterAdd => None,
1184    Flip => None,
1185    Roll => None,
1186    CumSum => None,
1187    Triangular => None,
1188    LogSoftmaxLastDim => None,
1189    MaskedFill => None,
1190    PadBackward => None,
1191    Pad => None,
1192    WriteSlice => None,
1193    WriteSliceRotating => None,
1194    WriteSliceDoff => None,
1195    FusedSoftmaxCrossEntropy => None,
1196    CausalConv1d => None,
1197    SelectiveScan => None,
1198    SsdChunkScan => None,
1199    Nf4Matmul => None,
1200    AwqMatmul => None,
1201    TopKLastDim => None,
1202    MatMulTopK => None,
1203    // A JIT-synthesized runtime op's live scalar args. `kernel.rs` pins the
1204    // producer: "Only produced by `compile_one`'s `is_runtime` arm" — the
1205    // kernel is synthesized at run time, so there is no checked-in source
1206    // file for a `.fkc.md` contract to describe.
1207    JitScalars => Some("runtime-JIT-only: no checked-in kernel to contract"),
1208    NonZeroIndices => None,
1209);
1210
1211variant_table!(
1212    FusedOpParams,
1213    _fused_op_params_is_exhaustive,
1214    FUSED_OP_PARAMS_VARIANTS,
1215    SoftmaxLastDim => None,
1216    FusedLinear => None,
1217    RmsNormLastDim => None,
1218    LayerNormLastDim => None,
1219    Rope => None,
1220    Conv2D => None,
1221    SoftmaxLastDimBackward => None,
1222    LayerNormLastDimBackward => None,
1223    RmsNormLastDimBackward => None,
1224    ReduceMaxToBackward => None,
1225    PowIBackward => None,
1226    ConvTranspose2D => None,
1227    FlashAttn => None,
1228    PagedAttn => None,
1229    QMatMul => None,
1230    InplaceAffine => None,
1231    SsdChunkScan => None,
1232    Nf4Matmul => None,
1233    AwqMatmul => None,
1234    RowScaledMatMul => None,
1235    TopKLastDim => None,
1236    MatMulTopK => None,
1237    Conv2DEpilogue => None,
1238    FlashAttnBackward => None,
1239    SelectiveScan => None,
1240    CausalConv1d => None,
1241    FusedSoftmaxCrossEntropy => None,
1242    // A runtime-REGISTERED fused op (JIT-synthesized or import-time). Its
1243    // identity is a runtime `FusedOpId` and its recipe lives in the
1244    // `runtime_fused` sidecar, so it has no stable name a hand-authored
1245    // contract could bind to.
1246    Runtime => Some("runtime-registered: identity is a runtime FusedOpId"),
1247);
1248
1249/// Whether a contract may name `v` as an `OpParams` variant. Backed by
1250/// [`OP_PARAMS_VARIANTS`], which the compiler holds against the real
1251/// `OpParams` enum (`fuel-dispatch/src/kernel.rs`).
1252fn is_op_params_variant(v: &str) -> bool {
1253    contract_authorable(OP_PARAMS_VARIANTS, v)
1254}
1255
1256/// Whether a contract may name `v` as a `FusedOpParams` variant. Backed by
1257/// [`FUSED_OP_PARAMS_VARIANTS`], which the compiler holds against the real
1258/// `FusedOpParams` enum (`fuel-graph/src/registry.rs`).
1259fn is_fused_op_params_variant(v: &str) -> bool {
1260    contract_authorable(FUSED_OP_PARAMS_VARIANTS, v)
1261}
1262
1263/// `v` names a real variant in `table` AND that variant carries no exclusion
1264/// reason. An excluded variant is rejected exactly like an unknown one — the
1265/// contract still cannot name it — but the reason is recorded at the entry
1266/// rather than being an unexplained absence.
1267fn contract_authorable(table: &[(&str, Option<&str>)], v: &str) -> bool {
1268    table
1269        .iter()
1270        .any(|(name, excluded)| *name == v && excluded.is_none())
1271}
1272
1273// ===========================================================================
1274// Returns: rule 16 (output dtype) + rule 13 (bundle rank)
1275// ===========================================================================
1276
1277/// A `fixed(DType)` output dtype rule names a dtype token that must be a real
1278/// DType (rule 16). `passthrough(role)` / other rules carry no dtype literal.
1279fn check_output_dtype_rule(section: &str, operand: &str, rule: &str) -> Result<(), FkcError> {
1280    let rule = rule.trim();
1281    if let Some(inner) = rule
1282        .strip_prefix("fixed(")
1283        .and_then(|s| s.strip_suffix(")"))
1284    {
1285        let tok = inner.trim();
1286        lower::lower_dtype(tok, section, operand).map_err(|_| FkcError::FdxTokenNotInTable {
1287            section: section.to_string(),
1288            field: format!("{operand}.dtype_rule fixed(...)"),
1289            token: tok.to_string(),
1290        })?;
1291    }
1292    Ok(())
1293}
1294
1295/// Rule 13: every bundle slot's declared shape must be rank ≤ 6 when a static
1296/// shape is given (an inline `[d0, d1, ...]` literal). A `shape_rule:` string
1297/// has no statically-knowable rank without evaluating the rule, so it is not
1298/// rank-checked here — `fkc::return_check::check_slot_rank`, called from
1299/// `cross_check_fused_section` for every bundle slot whose `shape_rule`
1300/// evaluates against a real probe shape, covers that DERIVED case instead
1301/// (Finding 5.3).
1302fn check_bundle_ranks(section: &str, bundle: &serde_yaml_ng::Value) -> Result<(), FkcError> {
1303    let serde_yaml_ng::Value::Sequence(slots) = bundle else {
1304        return Ok(());
1305    };
1306    for (i, slot) in slots.iter().enumerate() {
1307        let serde_yaml_ng::Value::Mapping(map) = slot else {
1308            continue;
1309        };
1310        let slot_name = map
1311            .get(serde_yaml_ng::Value::String("name".into()))
1312            .and_then(|v| v.as_str())
1313            .map(str::to_string)
1314            .unwrap_or_else(|| format!("slot{i}"));
1315        // A static `shape:` literal list is rank-checkable.
1316        if let Some(serde_yaml_ng::Value::Sequence(dims)) =
1317            map.get(serde_yaml_ng::Value::String("shape".into()))
1318            && dims.len() > 6
1319        {
1320            return Err(FkcError::BundleSlotRankExceeded {
1321                section: section.to_string(),
1322                slot: slot_name,
1323                rank: dims.len(),
1324            });
1325        }
1326    }
1327    Ok(())
1328}
1329
1330// ===========================================================================
1331// Rule 8 / 8a — cost
1332// ===========================================================================
1333
1334fn validate_cost(kernel: &FkcKernel, section: &str) -> Result<(), FkcError> {
1335    let Some(cost) = &kernel.cost else {
1336        // required_fields already errored on an absent cost block.
1337        return Ok(());
1338    };
1339
1340    // Rule 8a: provenance present + ∈ {declared, judge_measured}.
1341    match cost.provenance.as_deref() {
1342        Some("declared") | Some("judge_measured") => {}
1343        other => {
1344            return Err(FkcError::CostProvenanceMissing {
1345                section: section.to_string(),
1346                found: other.map(String::from),
1347            });
1348        }
1349    }
1350
1351    // Rule 8: every present cost expression parses (reuse the cost_expr
1352    // parser). A field that is `~`/absent is fine (Unknown).
1353    use crate::fkc::cost_expr;
1354    let parse = |field: &str, src: Option<&str>| -> Result<(), FkcError> {
1355        cost_expr::compile_field(src)
1356            .map(|_| ())
1357            .map_err(|e| FkcError::CostExprParse {
1358                section: section.to_string(),
1359                field: field.to_string(),
1360                expr: src.unwrap_or("").to_string(),
1361                reason: e.to_string(),
1362            })
1363    };
1364    parse("flops", cost.flops.as_deref())?;
1365    parse("bytes_moved", cost.bytes_moved.as_deref())?;
1366    if let Some(serde_yaml_ng::Value::String(s)) = &cost.overhead_ns {
1367        parse("overhead_ns", Some(s))?;
1368    }
1369    if let Some(mem) = &cost.memory {
1370        for (field, v) in [
1371            ("memory.device_bytes", &mem.device_bytes),
1372            ("memory.host_bytes", &mem.host_bytes),
1373            ("memory.disk_bytes", &mem.disk_bytes),
1374        ] {
1375            if let Some(serde_yaml_ng::Value::String(s)) = v {
1376                parse(field, Some(s))?;
1377            }
1378        }
1379    }
1380
1381    // Rule 8a (placeholder): closed classifier (§2.3 / V-FKC-9,
1382    // `cost_compile::classify_cost`). `class: free` is the honest
1383    // metadata-only declaration (license for zero coefficients). A
1384    // `judge_measured` cost legitimately ships shape-hint expressions OR
1385    // all-`~` (the Judge populates it) — the corpus convention is
1386    // `judge_measured` + `~` coefficients, which is NOT a placeholder (it is
1387    // explicitly measured). Otherwise a `declared` cost needs a pinned
1388    // `cost.cost_fn` OR a usable `flops`/`bytes_moved` expression; a
1389    // NON-EMPTY-but-non-`free` `class` alone is NOT a license (closes the old
1390    // `class.is_empty()` escape hatch: a `declared` block with a class but no
1391    // flops/bytes/cost_fn is still a placeholder).
1392    let class = cost.class.as_deref().unwrap_or("");
1393    let has_any_expr = cost.flops.as_deref().is_some_and(|s| !s.trim().is_empty())
1394        || cost
1395            .bytes_moved
1396            .as_deref()
1397            .is_some_and(|s| !s.trim().is_empty());
1398    let has_cost_fn = cost
1399        .cost_fn
1400        .as_deref()
1401        .is_some_and(|s| !s.trim().is_empty());
1402    if cost.provenance.as_deref() == Some("declared")
1403        && crate::fkc::cost_compile::classify_cost("declared", class, has_any_expr, has_cost_fn)
1404            .is_none()
1405    {
1406        return Err(FkcError::PlaceholderCost {
1407            section: section.to_string(),
1408            reason: "provenance: declared with no usable cost path (no flops/bytes_moved \
1409                     expression, no pinned cost.cost_fn) and class is not `free`"
1410                .to_string(),
1411        });
1412    }
1413
1414    Ok(())
1415}
1416
1417// ===========================================================================
1418// Rule 9 — precision coherence (determinism cross-check)
1419// ===========================================================================
1420
1421fn validate_precision_coherence(kernel: &FkcKernel, section: &str) -> Result<(), FkcError> {
1422    let det = kernel.determinism.as_deref().unwrap_or("");
1423    if det == "nondeterministic" {
1424        // nondeterministic ⇒ bit_stable=false + audited:true (no silent
1425        // unaudited nondeterminism; §4.9 / §10.9).
1426        if let Some(p) = &kernel.precision {
1427            if p.bit_stable_on_same_hardware == Some(true) {
1428                return Err(FkcError::QuantIncoherent {
1429                    section: section.to_string(),
1430                    operand: "<precision>".to_string(),
1431                    reason: "determinism: nondeterministic requires bit_stable_on_same_hardware = \
1432                             false"
1433                        .to_string(),
1434                });
1435            }
1436            if p.audited != Some(true) {
1437                return Err(FkcError::QuantIncoherent {
1438                    section: section.to_string(),
1439                    operand: "<precision>".to_string(),
1440                    reason: "determinism: nondeterministic requires audited: true with a \
1441                             none(reason) precision (no silent unaudited nondeterminism)"
1442                        .to_string(),
1443                });
1444            }
1445        }
1446    }
1447    Ok(())
1448}
1449
1450#[cfg(test)]
1451mod tests {
1452    use super::*;
1453    use crate::fkc::parse_file;
1454
1455    // --- A minimal VALID contract builder for unit tests. ---
1456
1457    /// A full, valid single-kernel bundle with the required fields, mutated by
1458    /// the negative tests via string replacement of the `__SLOT__` markers.
1459    fn valid_bundle() -> String {
1460        r#"---
1461fkc_version: 1
1462provider:
1463  name: test-provider
1464  backend: Cpu
1465  kernel_source: "test-cpu"
1466---
1467
1468# test bundle
1469
1470## demo
1471
1472A blurb.
1473
1474```fkc
1475kernel: demo
1476op_kind: AddElementwise
1477blurb: "demo blurb"
1478entry_point: "x::y"
1479accept:
1480  inputs:
1481    - name: lhs
1482      dtypes: [F32]
1483      layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }
1484    - name: rhs
1485      dtypes: [F32]
1486      layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }
1487  op_params: { variant: None }
1488return:
1489  outputs:
1490    - name: out
1491      dtype_rule: passthrough(lhs)
1492caps:
1493  awkward_layout_strategy: requires_contiguous
1494cost:
1495  provenance: declared
1496  class: cheap_elementwise
1497  flops: "n"
1498  bytes_moved: "3 * n * 4"
1499precision:
1500  bit_stable_on_same_hardware: true
1501  audited: true
1502determinism: same_hardware_bitwise
1503```
1504"#
1505        .to_string()
1506    }
1507
1508    fn validate_str(src: &str) -> Result<(), FkcError> {
1509        let file = parse_file(src).expect("fixture parses");
1510        validate_file(&file)
1511    }
1512
1513    #[test]
1514    fn valid_contract_passes() {
1515        validate_str(&valid_bundle()).expect("the valid fixture validates");
1516    }
1517
1518    // ===== Rule 1 =====
1519    #[test]
1520    fn version_too_new_is_unsupported() {
1521        let src = valid_bundle().replace("fkc_version: 1", "fkc_version: 99");
1522        let err = validate_str(&src).expect_err("version 99 unsupported");
1523        assert!(
1524            matches!(err, FkcError::UnsupportedVersion { found: 99, .. }),
1525            "got {err:?}"
1526        );
1527    }
1528
1529    // ===== GAP-292: the accepted set is membership, not `<= max` (KISS #450) =====
1530
1531    /// GAP-292 born-red (LOW side): `fkc_version: 0` MUST be rejected. The pre-fix
1532    /// gate was a bare `fkc_version > FKC_VERSION_MAX`, so 0 (being ≤ max) was
1533    /// silently ACCEPTED — the accepted set was {0, 1} and 0 is not a version.
1534    /// KISS #450: a reader accepts a version IFF it is in its declared supported
1535    /// set, NEVER merely because it is lower than a supported one. This fails on
1536    /// the pre-fix gate (0 passes the `> max` test).
1537    #[test]
1538    fn fkc_version_zero_is_rejected() {
1539        let src = valid_bundle().replace("fkc_version: 1", "fkc_version: 0");
1540        let err = validate_str(&src).expect_err("fkc_version 0 must be rejected");
1541        assert!(
1542            matches!(err, FkcError::UnsupportedVersion { found: 0, .. }),
1543            "expected UnsupportedVersion for fkc_version 0, got {err:?}"
1544        );
1545    }
1546
1547    /// GAP-292 fencepost guard (HIGH-but-sole side, the coverage the pre-fix test
1548    /// set never had): `fkc_version: 1`, the sole supported version, MUST still be
1549    /// accepted after the lower-bound fix. A bound written in haste lands a
1550    /// fencepost exactly here; paired with the low-side test it pins BOTH ends.
1551    #[test]
1552    fn fkc_version_one_is_still_accepted() {
1553        let result = validate_str(&valid_bundle());
1554        assert!(
1555            !matches!(result, Err(FkcError::UnsupportedVersion { .. })),
1556            "fkc_version 1 (sole supported) must pass the version gate, got {result:?}"
1557        );
1558    }
1559
1560    // ===== GAP-038: FKC two version axes (KISS-CONTRACT §8-0001..0007) =====
1561
1562    /// §8-0001: `fkc_version` (wire/schema) and the crate's published semver are
1563    /// INDEPENDENT axes. A crate release that bumps only `Cargo.toml`'s version
1564    /// leaves the wire schema untouched, so `FKC_SUPPORTED_VERSIONS` is a literal —
1565    /// never derived from `CARGO_PKG_VERSION`. If it were, publishing the crate
1566    /// would silently advance the accepted set and reject older contracts.
1567    #[test]
1568    fn fkc_version_is_decoupled_from_crate_semver() {
1569        // A literal evaluated at compile time; the module-scope pin is the
1570        // structural enforcement, this records the decoupling as an executable fact.
1571        assert_eq!(FKC_SUPPORTED_VERSIONS, &[1]);
1572    }
1573
1574    /// §8-0002: the front-matter SCHEMA and the schema version move together. The
1575    /// exhaustive-destructure pin at module scope compile-breaks on a field change;
1576    /// this test pins the *version number* that field set corresponds to — the
1577    /// LATEST supported version (the max of the set) — so the two cannot drift
1578    /// apart. Add a new entry to `FKC_SUPPORTED_VERSIONS` AND bump
1579    /// `PINNED_FKC_SCHEMA_VERSION` when the pin forces a new field.
1580    #[test]
1581    fn fkc_front_matter_schema_is_pinned_to_the_version() {
1582        const PINNED_FKC_SCHEMA_VERSION: u32 = 1;
1583        assert_eq!(
1584            FKC_SUPPORTED_VERSIONS.iter().copied().max(),
1585            Some(PINNED_FKC_SCHEMA_VERSION),
1586            "the latest supported fkc_version moved without updating the pinned \
1587             front-matter schema (or vice-versa) — a §8-0002 schema change bumps both"
1588        );
1589    }
1590
1591    // ===== Rule 2 =====
1592    #[test]
1593    fn missing_blurb_errors() {
1594        let src = valid_bundle().replace("blurb: \"demo blurb\"\n", "");
1595        let err = validate_str(&src).expect_err("no blurb");
1596        assert!(matches!(err, FkcError::MissingBlurb { .. }), "got {err:?}");
1597    }
1598
1599    #[test]
1600    fn missing_cost_block_errors() {
1601        // Remove the whole cost block.
1602        let src = valid_bundle();
1603        let cut = src.replace(
1604            "cost:\n  provenance: declared\n  class: cheap_elementwise\n  flops: \"n\"\n  bytes_moved: \"3 * n * 4\"\n",
1605            "",
1606        );
1607        let err = validate_str(&cut).expect_err("no cost block");
1608        assert!(
1609            matches!(err, FkcError::MissingRequiredField { ref field, .. } if field == "cost"),
1610            "got {err:?}"
1611        );
1612    }
1613
1614    #[test]
1615    fn missing_determinism_errors() {
1616        let src = valid_bundle().replace("determinism: same_hardware_bitwise\n", "");
1617        let err = validate_str(&src).expect_err("no determinism");
1618        assert!(
1619            matches!(err, FkcError::MissingRequiredField { ref field, .. } if field == "determinism"),
1620            "got {err:?}"
1621        );
1622    }
1623
1624    #[test]
1625    fn both_op_kind_and_fused_op_is_ambiguous() {
1626        let src = valid_bundle().replace(
1627            "op_kind: AddElementwise",
1628            "op_kind: AddElementwise\nfused_op: SOFTMAX_LAST_DIM",
1629        );
1630        let err = validate_str(&src).expect_err("both targets");
1631        assert!(
1632            matches!(err, FkcError::OpTargetAmbiguous { .. }),
1633            "got {err:?}"
1634        );
1635    }
1636
1637    // ===== Rule 3 / 16 — dtype =====
1638    #[test]
1639    fn bad_dtype_token_is_fdx_token_not_in_table() {
1640        let src = valid_bundle().replace("dtypes: [F32]", "dtypes: [F99]");
1641        let err = validate_str(&src).expect_err("F99 not a dtype");
1642        assert!(
1643            matches!(err, FkcError::FdxTokenNotInTable { .. }),
1644            "got {err:?}"
1645        );
1646    }
1647
1648    // ===== Rule 4 — layout coherence =====
1649    #[test]
1650    fn layout_no_acceptable_form_is_incoherent() {
1651        // contiguous: n/a + strided: rejected ⇒ accepts no layout.
1652        let src = valid_bundle().replace(
1653            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1654            "layout: { contiguous: \"n/a\", strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1655        );
1656        let err = validate_str(&src).expect_err("no acceptable layout");
1657        assert!(
1658            matches!(err, FkcError::LayoutIncoherent { .. }),
1659            "got {err:?}"
1660        );
1661    }
1662
1663    #[test]
1664    fn broadcast_without_strided_is_incoherent() {
1665        let src = valid_bundle().replace(
1666            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1667            "layout: { contiguous: required, strided: rejected, broadcast_stride0: accepted, start_offset: rejected, reverse_strides: rejected }",
1668        );
1669        let err = validate_str(&src).expect_err("broadcast w/o strided");
1670        assert!(
1671            matches!(err, FkcError::LayoutIncoherent { .. }),
1672            "got {err:?}"
1673        );
1674    }
1675
1676    #[test]
1677    fn reverse_without_strided_is_incoherent() {
1678        let src = valid_bundle().replace(
1679            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1680            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: accepted }",
1681        );
1682        let err = validate_str(&src).expect_err("reverse w/o strided");
1683        assert!(
1684            matches!(err, FkcError::LayoutIncoherent { .. }),
1685            "got {err:?}"
1686        );
1687    }
1688
1689    // ===== Rule 4 — broadcast_axes mask (§6-additive, path 1a) =====
1690    #[test]
1691    fn required_broadcast_with_axes_is_valid() {
1692        // Baracuda's §6-additive spelling for a baked bias-add cell.
1693        let src = valid_bundle()
1694            .replace(
1695                "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1696                "layout: { contiguous: accepted, strided: accepted, broadcast_stride0: required, broadcast_axes: [0], start_offset: rejected, reverse_strides: rejected }",
1697            )
1698            .replace(
1699                "awkward_layout_strategy: requires_contiguous",
1700                "awkward_layout_strategy: handles_strided",
1701            );
1702        validate_str(&src)
1703            .expect("required broadcast with a non-empty broadcast_axes mask validates");
1704    }
1705
1706    #[test]
1707    fn required_broadcast_without_axes_is_incoherent() {
1708        let src = valid_bundle().replace(
1709            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1710            "layout: { contiguous: accepted, strided: accepted, broadcast_stride0: required, start_offset: rejected, reverse_strides: rejected }",
1711        );
1712        let err = validate_str(&src).expect_err("required broadcast needs a broadcast_axes mask");
1713        assert!(
1714            matches!(err, FkcError::LayoutIncoherent { .. }),
1715            "got {err:?}"
1716        );
1717    }
1718
1719    #[test]
1720    fn broadcast_axes_without_required_is_incoherent() {
1721        let src = valid_bundle().replace(
1722            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1723            "layout: { contiguous: accepted, strided: accepted, broadcast_stride0: accepted, broadcast_axes: [0], start_offset: rejected, reverse_strides: rejected }",
1724        );
1725        let err = validate_str(&src).expect_err("broadcast_axes is meaningful only with required");
1726        assert!(
1727            matches!(err, FkcError::LayoutIncoherent { .. }),
1728            "got {err:?}"
1729        );
1730    }
1731
1732    #[test]
1733    fn required_broadcast_with_empty_axes_is_incoherent() {
1734        let src = valid_bundle().replace(
1735            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1736            "layout: { contiguous: accepted, strided: accepted, broadcast_stride0: required, broadcast_axes: [], start_offset: rejected, reverse_strides: rejected }",
1737        );
1738        let err = validate_str(&src).expect_err("empty broadcast_axes on a required broadcast");
1739        assert!(
1740            matches!(err, FkcError::LayoutIncoherent { .. }),
1741            "got {err:?}"
1742        );
1743    }
1744
1745    // ===== Rule 5 — awkward strategy =====
1746    #[test]
1747    fn handles_strided_without_strided_accepted_is_incoherent() {
1748        let src = valid_bundle().replace(
1749            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1750            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected, awkward_layout_strategy: handles_strided }",
1751        );
1752        let err = validate_str(&src).expect_err("handles_strided but strided rejected");
1753        assert!(
1754            matches!(err, FkcError::AwkwardStrategyIncoherent { .. }),
1755            "got {err:?}"
1756        );
1757    }
1758
1759    #[test]
1760    fn unknown_awkward_strategy_is_typed_error() {
1761        let src = valid_bundle().replace(
1762            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
1763            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected, awkward_layout_strategy: teleports }",
1764        );
1765        let err = validate_str(&src).expect_err("unknown strategy");
1766        assert!(
1767            matches!(err, FkcError::AwkwardStrategyIncoherent { .. }),
1768            "got {err:?}"
1769        );
1770    }
1771
1772    // ===== Rule 6 — quant =====
1773    fn quant_bundle(family: &str, extra: &str) -> String {
1774        // A QMatMul-shaped contract whose weight carries an fdx.quant block.
1775        format!(
1776            r#"---
1777fkc_version: 1
1778provider:
1779  name: test-provider
1780  backend: Cpu
1781  kernel_source: "test-cpu"
1782---
1783
1784# q
1785
1786## q
1787
1788blurb.
1789
1790```fkc
1791kernel: q
1792op_kind: QMatMul
1793blurb: "q"
1794entry_point: "x::q"
1795accept:
1796  inputs:
1797    - name: act
1798      dtypes: [F32]
1799      layout: {{ contiguous: required, strided: rejected }}
1800    - name: weight
1801      dtypes: [U8]
1802      layout: {{ contiguous: required, strided: rejected }}
1803      fdx:
1804        requires_ext: true
1805        quant:
1806          family: {family}
1807{extra}
1808  op_params: {{ variant: QMatMul }}
1809return:
1810  outputs:
1811    - name: out
1812      dtype_rule: fixed(F32)
1813cost:
1814  provenance: judge_measured
1815  class: gemm_like
1816precision:
1817  bit_stable_on_same_hardware: true
1818  audited: true
1819determinism: same_hardware_bitwise
1820```
1821"#
1822        )
1823    }
1824
1825    #[test]
1826    fn ggml_block_with_real_dtype_ok() {
1827        let src = quant_bundle(
1828            "GGML_BLOCK",
1829            "          ggml_dtype: Q4_0\n          role: weight",
1830        );
1831        validate_str(&src).expect("GGML_BLOCK Q4_0 weight validates");
1832    }
1833
1834    #[test]
1835    fn ggml_dtype_q4_k_m_is_quant_incoherent() {
1836        // The GGUF file-format name `Q4_K_M` is NOT a GgmlDType variant.
1837        let src = quant_bundle(
1838            "GGML_BLOCK",
1839            "          ggml_dtype: Q4_K_M\n          role: weight",
1840        );
1841        let err = validate_str(&src).expect_err("Q4_K_M bad");
1842        assert!(
1843            matches!(err, FkcError::QuantIncoherent { .. }),
1844            "got {err:?}"
1845        );
1846    }
1847
1848    #[test]
1849    fn ggml_block_with_granularity_is_incoherent() {
1850        let src = quant_bundle(
1851            "GGML_BLOCK",
1852            "          ggml_dtype: Q4_0\n          granularity: PerChannel\n          role: weight",
1853        );
1854        let err = validate_str(&src).expect_err("GGML carries no granularity");
1855        assert!(
1856            matches!(err, FkcError::QuantIncoherent { .. }),
1857            "got {err:?}"
1858        );
1859    }
1860
1861    #[test]
1862    fn mx_family_is_not_yet_registrable() {
1863        let src = quant_bundle(
1864            "MX",
1865            "          granularity: PerBlock\n          role: weight",
1866        );
1867        let err = validate_str(&src).expect_err("MX not registrable");
1868        assert!(
1869            matches!(err, FkcError::MxNotYetRegistrable { ref family, .. } if family == "MX"),
1870            "got {err:?}"
1871        );
1872    }
1873
1874    #[test]
1875    fn affine_block_without_block_shape_is_incoherent() {
1876        let src = quant_bundle(
1877            "AFFINE_BLOCK",
1878            "          role: weight\n          scale_operand: scl",
1879        );
1880        // scale_operand `scl` won't exist either, but block_shape missing OR the
1881        // scale-operand check fires; both are coherence errors.
1882        let err = validate_str(&src).expect_err("AFFINE_BLOCK needs block_shape / real scale");
1883        assert!(
1884            matches!(
1885                err,
1886                FkcError::QuantIncoherent { .. } | FkcError::ScaleDoubleDeclared { .. }
1887            ),
1888            "got {err:?}"
1889        );
1890    }
1891
1892    #[test]
1893    fn affine_int_bad_granularity_is_incoherent() {
1894        let src = quant_bundle(
1895            "AFFINE_INT",
1896            "          granularity: PerBlock\n          role: weight",
1897        );
1898        let err = validate_str(&src).expect_err("AFFINE_INT PerBlock bad");
1899        assert!(
1900            matches!(err, FkcError::QuantIncoherent { .. }),
1901            "got {err:?}"
1902        );
1903    }
1904
1905    #[test]
1906    fn unknown_family_is_fdx_token_not_in_table() {
1907        let src = quant_bundle("MADE_UP", "          role: weight");
1908        let err = validate_str(&src).expect_err("MADE_UP not an FDX family");
1909        assert!(
1910            matches!(err, FkcError::FdxTokenNotInTable { .. }),
1911            "got {err:?}"
1912        );
1913    }
1914
1915    #[test]
1916    fn ggml_with_separate_scale_operand_is_double_declared() {
1917        // GGML scales are INLINE; declaring a separate scale_operand is the
1918        // double-declaration §10.6 forbids. (act is a real input role.)
1919        let src = quant_bundle(
1920            "GGML_BLOCK",
1921            "          ggml_dtype: Q4_0\n          role: weight\n          scale_operand: act",
1922        );
1923        let err = validate_str(&src).expect_err("GGML + separate scale");
1924        assert!(
1925            matches!(err, FkcError::ScaleDoubleDeclared { .. }),
1926            "got {err:?}"
1927        );
1928    }
1929
1930    // ===== Rule 7 — op-param namespace =====
1931    #[test]
1932    fn op_kind_with_fused_only_variant_is_bad_namespace() {
1933        // `FusedLinear` is a FusedOpParams variant, NOT an OpParams variant; an
1934        // op_kind contract naming it must fail the namespace check.
1935        let src = valid_bundle().replace(
1936            "op_params: { variant: None }",
1937            "op_params: { variant: FusedLinear }",
1938        );
1939        let err = validate_str(&src).expect_err("FusedLinear not in OpParams");
1940        assert!(
1941            matches!(err, FkcError::BadOpParamsVariant { ref namespace, .. } if namespace == "OpParams"),
1942            "got {err:?}"
1943        );
1944    }
1945
1946    // ===== V-FKC-5 — the op-param allowlists vs the real enums =====
1947
1948    /// The drift this closes, stated as behavior rather than as a list
1949    /// comparison: `OpParams::NonZeroIndices` is a real, kernel-backed variant
1950    /// (1 input, a `[indices; count]` bundle output — the data-dependent
1951    /// dynamic-shape seam), and a contract naming it was rejected as if it
1952    /// were a typo, because the hand-typed allowlist was two variants behind
1953    /// the enum.
1954    #[test]
1955    fn op_params_namespace_accepts_the_real_nonzeroindices_variant() {
1956        let src = valid_bundle().replace(
1957            "op_params: { variant: None }",
1958            "op_params: { variant: NonZeroIndices }",
1959        );
1960        validate_str(&src)
1961            .expect("NonZeroIndices is a real OpParams variant — a contract may name it");
1962    }
1963
1964    /// The predicate must be exactly the generated table, in both directions.
1965    /// The table itself is held against the real enum by the compiler (see
1966    /// `variant_table!`), so this is what closes the loop between "the list
1967    /// matches the enum" and "the checker reads the list".
1968    #[test]
1969    fn op_param_predicates_agree_with_the_generated_variant_tables() {
1970        // Positive control: a macro that expanded to nothing, or a table that
1971        // silently lost its entries, would make every assertion below vacuous.
1972        // These floors are deliberately loose — they catch an empty or
1973        // truncated table, not a single added variant.
1974        assert!(
1975            OP_PARAMS_VARIANTS.len() >= 40,
1976            "OP_PARAMS_VARIANTS looks truncated ({} entries) — the rest of this \
1977             test would pass vacuously",
1978            OP_PARAMS_VARIANTS.len()
1979        );
1980        assert!(
1981            FUSED_OP_PARAMS_VARIANTS.len() >= 20,
1982            "FUSED_OP_PARAMS_VARIANTS looks truncated ({} entries)",
1983            FUSED_OP_PARAMS_VARIANTS.len()
1984        );
1985
1986        for (name, excluded) in OP_PARAMS_VARIANTS {
1987            assert_eq!(
1988                is_op_params_variant(name),
1989                excluded.is_none(),
1990                "OpParams::{name} — excluded={excluded:?} but the predicate says \
1991                 {}",
1992                is_op_params_variant(name)
1993            );
1994        }
1995        for (name, excluded) in FUSED_OP_PARAMS_VARIANTS {
1996            assert_eq!(
1997                is_fused_op_params_variant(name),
1998                excluded.is_none(),
1999                "FusedOpParams::{name} — excluded={excluded:?} but the predicate \
2000                 says {}",
2001                is_fused_op_params_variant(name)
2002            );
2003        }
2004
2005        // An exclusion with no stated reason is the silent omission this whole
2006        // change exists to eliminate.
2007        for (name, excluded) in OP_PARAMS_VARIANTS.iter().chain(FUSED_OP_PARAMS_VARIANTS) {
2008            if let Some(reason) = excluded {
2009                assert!(
2010                    !reason.trim().is_empty(),
2011                    "{name} is excluded with an empty reason"
2012                );
2013            }
2014        }
2015    }
2016
2017    /// The two deliberate exclusions, pinned so a later reader doesn't "fix"
2018    /// them back in: both name kernels that are synthesized or registered at
2019    /// RUN time, so there is no checked-in kernel for a `.fkc.md` contract to
2020    /// describe. This is the one case where rejection is correct — and the
2021    /// reason is now recorded at the table entry instead of being an
2022    /// unexplained gap.
2023    #[test]
2024    fn runtime_only_op_param_variants_stay_out_of_contract_reach() {
2025        assert!(!is_op_params_variant("JitScalars"));
2026        assert!(!is_fused_op_params_variant("Runtime"));
2027
2028        let src = valid_bundle().replace(
2029            "op_params: { variant: None }",
2030            "op_params: { variant: JitScalars }",
2031        );
2032        let err = validate_str(&src).expect_err("JitScalars is runtime-JIT-only");
2033        assert!(
2034            matches!(err, FkcError::BadOpParamsVariant { ref namespace, .. } if namespace == "OpParams"),
2035            "got {err:?}"
2036        );
2037    }
2038
2039    // ===== V-FKC-10 — FKC's quant tokens ⊆ FDX's normative code table =====
2040
2041    /// FDX's `codes` module doc names it "the normative owner of the shared
2042    /// dtype/quant/granularity/... codes (the FKC kernel-contract format
2043    /// references them by symbol)". FKC's [`is_fdx_quant_family`] /
2044    /// [`is_fdx_granularity`] hand-copy that table as string literals, and
2045    /// nothing checked the two still agreed — in either direction.
2046    ///
2047    /// The guard reads `codes.rs` as TEXT rather than by symbol, deliberately.
2048    /// FDX's consts live behind fuel-ir's non-default `dlpack` feature, so a
2049    /// symbol-level test would compile only under `--features dlpack` and
2050    /// would report `test result: ok` while running nothing under the default
2051    /// gate — the exact failure mode that let this drift survive. Reading the
2052    /// source text needs no feature, so this runs in a plain
2053    /// `cargo test -p fuel-dispatch`.
2054    ///
2055    /// Read at run time, not `include_str!`d: `fuel-ir` is a sibling crate, so the
2056    /// file is outside this crate's tarball, and an `include_str!` of it would make
2057    /// the published crate unbuildable (the packaged-crate check fails any `src/`
2058    /// include that leaves the crate). From a tarball the test fails loudly rather
2059    /// than skipping: it is a workspace consistency check.
2060    fn fdx_codes_src() -> String {
2061        let path =
2062            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../fuel-ir/src/dlpack/codes.rs");
2063        std::fs::read_to_string(&path).unwrap_or_else(|e| {
2064            panic!(
2065                "the FDX code table must be readable from the workspace (this test checks \
2066                 fuel-dispatch against fuel-ir's source text); read {path:?}: {e}"
2067            )
2068        })
2069    }
2070
2071    /// `(FDX const name, the token an FKC contract writes)`. Every const named
2072    /// here must exist in `codes.rs` and every `FDX_QUANT_*` const there must
2073    /// appear here — so an FDX addition, removal, or rename all fail loudly.
2074    const FDX_FAMILY_TOKENS: &[(&str, &str)] = &[
2075        ("FDX_QUANT_NONE", "NONE"),
2076        ("FDX_QUANT_GGML_BLOCK", "GGML_BLOCK"),
2077        ("FDX_QUANT_MX", "MX"),
2078        ("FDX_QUANT_AFFINE_INT", "AFFINE_INT"),
2079        ("FDX_QUANT_AFFINE_FLOAT", "AFFINE_FLOAT"),
2080        ("FDX_QUANT_AFFINE_BLOCK", "AFFINE_BLOCK"),
2081    ];
2082
2083    /// As [`FDX_FAMILY_TOKENS`], for `FDXScaleGranularity`.
2084    const FDX_GRAN_TOKENS: &[(&str, &str)] = &[
2085        ("FDX_SCALE_GRAN_PER_TENSOR", "PerTensor"),
2086        ("FDX_SCALE_GRAN_PER_TOKEN", "PerToken"),
2087        ("FDX_SCALE_GRAN_PER_CHANNEL", "PerChannel"),
2088        ("FDX_SCALE_GRAN_PER_BLOCK", "PerBlock"),
2089    ];
2090
2091    /// Every `pub const <prefix>… ` declared in FDX's code table.
2092    fn fdx_consts_with_prefix(prefix: &str) -> std::collections::BTreeSet<String> {
2093        fdx_codes_src()
2094            .lines()
2095            .filter_map(|l| l.trim().strip_prefix("pub const ")?.split(':').next())
2096            .map(|n| n.trim().to_string())
2097            .filter(|n| n.starts_with(prefix))
2098            .collect()
2099    }
2100
2101    #[test]
2102    fn fkc_quant_tokens_are_a_faithful_subset_of_the_fdx_code_table() {
2103        let families = fdx_consts_with_prefix("FDX_QUANT_");
2104        let grans = fdx_consts_with_prefix("FDX_SCALE_GRAN_");
2105
2106        // Positive control. A parse that matched nothing — `codes.rs` reshaped,
2107        // the consts moved, the `pub const` spelling changed — would make every
2108        // set comparison below trivially satisfiable in the *dangerous*
2109        // direction (FKC accepts tokens FDX no longer defines). Assert the
2110        // instrument works before trusting what it reports.
2111        assert!(
2112            families.contains("FDX_QUANT_GGML_BLOCK") && grans.contains("FDX_SCALE_GRAN_PER_BLOCK"),
2113            "parsed {} family / {} granularity consts out of {} lines of codes.rs — \
2114             the parse found neither known anchor, so it is broken, not the table",
2115            families.len(),
2116            grans.len(),
2117            fdx_codes_src().lines().count()
2118        );
2119
2120        let declared_families: std::collections::BTreeSet<String> = FDX_FAMILY_TOKENS
2121            .iter()
2122            .map(|(c, _)| c.to_string())
2123            .collect();
2124        assert_eq!(
2125            families, declared_families,
2126            "FDX's FDX_QUANT_* table and FKC's mirror of it have diverged — update \
2127             FDX_FAMILY_TOKENS *and* is_fdx_quant_family together"
2128        );
2129
2130        let declared_grans: std::collections::BTreeSet<String> =
2131            FDX_GRAN_TOKENS.iter().map(|(c, _)| c.to_string()).collect();
2132        assert_eq!(
2133            grans, declared_grans,
2134            "FDX's FDX_SCALE_GRAN_* table and FKC's mirror of it have diverged"
2135        );
2136
2137        // …and the tokens themselves round-trip through the FKC predicates.
2138        for (konst, token) in FDX_FAMILY_TOKENS {
2139            assert!(
2140                is_fdx_quant_family(token),
2141                "FKC rejects `{token}`, the token for the live FDX const {konst}"
2142            );
2143        }
2144        for (konst, token) in FDX_GRAN_TOKENS {
2145            assert!(
2146                is_fdx_granularity(token),
2147                "FKC rejects `{token}`, the token for the live FDX const {konst}"
2148            );
2149        }
2150
2151        // The corpus writes `none` lowercase; both spellings are accepted.
2152        assert!(is_fdx_quant_family("none"));
2153
2154        // Plausible-but-not-FDX tokens must NOT be accepted, or the subset
2155        // property above is satisfied by a predicate that accepts everything.
2156        for bogus in ["PerGroup", "AWQ", "GPTQ", "affine_int", "PERTENSOR", ""] {
2157            assert!(!is_fdx_quant_family(bogus), "family accepted `{bogus}`");
2158            assert!(!is_fdx_granularity(bogus), "granularity accepted `{bogus}`");
2159        }
2160    }
2161
2162    // ===== Rule 8 / 8a — cost =====
2163    #[test]
2164    fn missing_provenance_errors() {
2165        let src = valid_bundle().replace("  provenance: declared\n", "");
2166        let err = validate_str(&src).expect_err("no provenance");
2167        assert!(
2168            matches!(err, FkcError::CostProvenanceMissing { .. }),
2169            "got {err:?}"
2170        );
2171    }
2172
2173    #[test]
2174    fn bad_cost_expr_is_cost_expr_parse() {
2175        let src = valid_bundle().replace("flops: \"n\"", "flops: \"2 * * n\"");
2176        let err = validate_str(&src).expect_err("malformed flops");
2177        assert!(matches!(err, FkcError::CostExprParse { .. }), "got {err:?}");
2178    }
2179
2180    #[test]
2181    fn declared_with_no_coeffs_and_no_class_is_placeholder() {
2182        let src = valid_bundle()
2183            .replace("  class: cheap_elementwise\n", "")
2184            .replace("  flops: \"n\"\n", "")
2185            .replace("  bytes_moved: \"3 * n * 4\"\n", "");
2186        let err = validate_str(&src).expect_err("bare declared cost");
2187        assert!(
2188            matches!(err, FkcError::PlaceholderCost { .. }),
2189            "got {err:?}"
2190        );
2191    }
2192
2193    #[test]
2194    fn judge_measured_all_tilde_is_not_placeholder() {
2195        // The corpus convention: judge_measured + all-`~` coefficients is an
2196        // explicit measured cost, NOT a placeholder.
2197        let src = valid_bundle()
2198            .replace("provenance: declared", "provenance: judge_measured")
2199            .replace("  flops: \"n\"\n", "  flops: ~\n")
2200            .replace("  bytes_moved: \"3 * n * 4\"\n", "  bytes_moved: ~\n");
2201        validate_str(&src).expect("judge_measured + ~ is fine");
2202    }
2203
2204    #[test]
2205    fn placeholder_cost_class_field() {
2206        use crate::fkc::cost_compile::{CostClassKind, classify_cost};
2207        assert!(classify_cost("declared", "cheap_elementwise", false, false).is_none());
2208        assert!(matches!(
2209            classify_cost("declared", "free", false, false),
2210            Some(CostClassKind::Free)
2211        ));
2212        assert!(matches!(
2213            classify_cost("declared", "gemm_like", true, false),
2214            Some(CostClassKind::DeclaredFormula)
2215        ));
2216        assert!(matches!(
2217            classify_cost("declared", "gemm_like", false, true),
2218            Some(CostClassKind::VendorSpec)
2219        ));
2220        assert!(matches!(
2221            classify_cost("judge_measured", "gemm_like", false, false),
2222            Some(CostClassKind::JudgeMeasured)
2223        ));
2224        // integration: a declared+non-free class with NO flops/bytes/cost_fn is rejected on import
2225        use crate::fkc::register::import_bundle_str;
2226        let src = "---\nfkc_version: 1\nprovider:\n  name: ph-provider\n  backend: Cpu\n  kernel_source: \"ph-cpu\"\n---\n\n# ph\n\n## add_f32\n\nA.\n\n```fkc\nkernel: add_f32\nop_kind: AddElementwise\nblurb: \"a\"\nentry_point: \"x::add_f32\"\naccept:\n  inputs:\n    - name: lhs\n      dtypes: [F32]\n      layout: { contiguous: required, strided: rejected }\n    - name: rhs\n      dtypes: [F32]\n      layout: { contiguous: required, strided: rejected }\n  op_params: { variant: None }\nreturn:\n  outputs:\n    - name: out\n      dtype_rule: passthrough(lhs)\ncost:\n  provenance: declared\n  class: cheap_elementwise\nprecision:\n  bit_stable_on_same_hardware: true\n  audited: true\ndeterminism: same_hardware_bitwise\n```\n";
2227        let err = import_bundle_str(src, &StubLink::new())
2228            .expect_err("declared + non-free class + no usable cost path must reject");
2229        assert!(
2230            matches!(err, FkcError::PlaceholderCost { .. }),
2231            "got {err:?}"
2232        );
2233    }
2234
2235    // ===== Rule 9 — determinism/precision =====
2236    #[test]
2237    fn nondeterministic_with_bitstable_true_is_incoherent() {
2238        let src = valid_bundle().replace(
2239            "determinism: same_hardware_bitwise",
2240            "determinism: nondeterministic",
2241        );
2242        let err = validate_str(&src).expect_err("nondeterministic + bit_stable true");
2243        assert!(
2244            matches!(err, FkcError::QuantIncoherent { .. }),
2245            "got {err:?}"
2246        );
2247    }
2248
2249    // ===== Rule 15 — extent =====
2250    #[test]
2251    fn unknown_extent_kind_is_typed_error() {
2252        let src = quant_bundle("none", "").replace(
2253            "      fdx:\n        requires_ext: true\n        quant:\n          family: none\n",
2254            "      fdx:\n        extent_kind: wobbly\n        symbolic_extent: required\n",
2255        );
2256        let err = validate_str(&src).expect_err("bad extent_kind");
2257        assert!(
2258            matches!(err, FkcError::UnknownAdmissibilityEnum { .. }),
2259            "got {err:?}"
2260        );
2261    }
2262
2263    // ===== Rule 17 / §3.10 — describe-only (non-registrable) sections =====
2264
2265    /// A describe-only chassis-umbrella bundle whose `op_kind:` is the
2266    /// DESCRIPTIVE token `binary` (NOT a real `OpKind`) — legal because
2267    /// `registrable: false` skips the dispatch-resolution checks (§3.10).
2268    fn describe_only_bundle() -> String {
2269        r#"---
2270fkc_version: 1
2271provider:
2272  name: test-provider
2273  backend: Cpu
2274  kernel_source: "test-cpu"
2275---
2276
2277# describe-only bundle
2278
2279## binary
2280
2281A chassis umbrella.
2282
2283```fkc
2284kernel: binary
2285registrable: false
2286op_kind: binary
2287blurb: "shared binary chassis (documentation umbrella)"
2288entry_point: "x::chassis"
2289accept:
2290  inputs:
2291    - name: lhs
2292      dtypes: [F32, F64, BF16, F16]
2293      layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }
2294    - name: rhs
2295      dtypes: [F32, F64, BF16, F16]
2296      layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }
2297return:
2298  outputs:
2299    - name: out
2300      dtype_rule: passthrough(lhs)
2301cost:
2302  provenance: judge_measured
2303  class: cheap_elementwise
2304precision:
2305  bit_stable_on_same_hardware: true
2306  audited: true
2307determinism: same_hardware_bitwise
2308```
2309"#
2310        .to_string()
2311    }
2312
2313    #[test]
2314    fn describe_only_with_descriptive_op_kind_validates() {
2315        // `op_kind: binary` is NOT a real OpKind, but registrable: false skips
2316        // the resolution check — the section validates as documentation.
2317        validate_str(&describe_only_bundle()).expect("describe-only section validates");
2318    }
2319
2320    #[test]
2321    fn describe_only_with_tilde_op_kind_validates() {
2322        // `op_kind: ~` (absent) is legal for a describe-only section — neither
2323        // op_kind nor fused_op need resolve.
2324        let src = describe_only_bundle().replace("op_kind: binary\n", "op_kind: ~\n");
2325        validate_str(&src).expect("describe-only with op_kind: ~ validates");
2326    }
2327
2328    #[test]
2329    fn non_describe_only_still_requires_real_op_kind_regression() {
2330        // REGRESSION: the SAME section WITHOUT registrable: false must still
2331        // fail — a descriptive `binary` token is not a real OpKind, so a
2332        // registrable section is rejected (no relaxation of the validator).
2333        let src = describe_only_bundle().replace("registrable: false\n", "");
2334        let err = validate_str(&src).expect_err("registrable section needs a real op_kind");
2335        assert!(matches!(err, FkcError::UnknownOpKind { .. }), "got {err:?}");
2336    }
2337
2338    #[test]
2339    fn describe_only_still_validates_descriptive_dtypes() {
2340        // A describe-only section's descriptive checks STILL run: a bogus dtype
2341        // token is rejected by the FDX-subset drift-guard even though dispatch
2342        // resolution is skipped.
2343        let src = describe_only_bundle().replace("dtypes: [F32, F64, BF16, F16]", "dtypes: [F99]");
2344        let err = validate_str(&src).expect_err("bad dtype in describe-only docs");
2345        assert!(
2346            matches!(err, FkcError::FdxTokenNotInTable { .. }),
2347            "got {err:?}"
2348        );
2349    }
2350
2351    #[test]
2352    fn describe_only_still_validates_layout_coherence() {
2353        // Layout coherence (rule 4) still runs for describe-only docs.
2354        let src = describe_only_bundle().replace(
2355            "layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
2356            "layout: { contiguous: \"n/a\", strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }",
2357        );
2358        let err = validate_str(&src).expect_err("incoherent layout in describe-only docs");
2359        assert!(
2360            matches!(err, FkcError::LayoutIncoherent { .. }),
2361            "got {err:?}"
2362        );
2363    }
2364
2365    /// A describe-only zero-operand op (e.g. a `rand_uniform`/`rand_normal`
2366    /// random fill whose only "input" is backend-private RNG state, not a graph
2367    /// tensor): `registrable: false` + `accept.inputs: []`. Mirrors the
2368    /// `metal/sort-random.fkc.md` random-fill sections.
2369    fn describe_only_zero_input_bundle() -> String {
2370        r#"---
2371fkc_version: 1
2372provider:
2373  name: test-provider
2374  backend: Cpu
2375  kernel_source: "test-cpu"
2376---
2377
2378# describe-only zero-input bundle
2379
2380## rand_fill
2381
2382A zero-operand random fill.
2383
2384```fkc
2385kernel: rand_fill
2386registrable: false
2387op_kind: RandUniform
2388blurb: "fill a dense buffer with U(min,max); no input tensor (RNG state is backend-private)"
2389entry_point: "x::rand_fill"
2390accept:
2391  inputs: []
2392return:
2393  outputs:
2394    - name: out
2395      dtype_rule: cast(out)
2396cost:
2397  provenance: judge_measured
2398  class: cheap_elementwise
2399precision:
2400  bit_stable_on_same_hardware: false
2401  audited: true
2402determinism: nondeterministic
2403```
2404"#
2405        .to_string()
2406    }
2407
2408    #[test]
2409    fn describe_only_zero_input_fill_validates() {
2410        // §3.10 carve-out: a describe-only section with `accept.inputs: []` is a
2411        // legitimate zero-operand documentation op (random fill) — the ≥1-input
2412        // required-field check is EXEMPTED for describe-only sections.
2413        validate_str(&describe_only_zero_input_bundle())
2414            .expect("describe-only zero-input fill validates");
2415    }
2416
2417    #[test]
2418    fn registrable_zero_input_still_fails_missing_required_field() {
2419        // REGRESSION: the SAME zero-input section WITHOUT registrable: false must
2420        // still fail the ≥1-input rule — the carve-out applies ONLY to
2421        // describe-only sections (a real dispatch target must consume ≥1 graph
2422        // operand). No relaxation for registrable sections.
2423        let src = describe_only_zero_input_bundle()
2424            .replace("registrable: false\n", "")
2425            // Use a real OpKind so the missing-input rule (not the op-resolution
2426            // rule) is the failure under test. The required-field battery runs
2427            // before op resolution, so `accept.inputs (≥1)` fires first regardless.
2428            .replace("op_kind: RandUniform\n", "op_kind: ReluElementwise\n");
2429        let err = validate_str(&src).expect_err("registrable zero-input section is rejected");
2430        assert!(
2431            matches!(err, FkcError::MissingRequiredField { ref field, .. } if field == "accept.inputs (≥1)"),
2432            "got {err:?}"
2433        );
2434    }
2435
2436    // =====================================================================
2437    // Rule 14 — gather (paged_blocks) coherence + the IMPORT-SIDE LIFT
2438    // (FDX gather-sidecar arc, slice A).
2439    // =====================================================================
2440
2441    /// A minimal but COHERENT `registrable: true` paged_blocks bundle: the
2442    /// PagedAttn-shaped operand carries `fdx.gather: paged_blocks` naming the
2443    /// separate `block_table` / `context_lens` U32 input roles (the as-built
2444    /// ABI). `__GATHER__` marks the pool operand's `fdx` block so the negative
2445    /// tests can mutate it into an incoherent form.
2446    fn paged_blocks_bundle() -> String {
2447        r#"---
2448fkc_version: 1
2449provider:
2450  name: test-provider
2451  backend: Cpu
2452  kernel_source: "test-cpu"
2453---
2454
2455# paged bundle
2456
2457## paged_demo
2458
2459A paged attention blurb.
2460
2461```fkc
2462kernel: paged_demo
2463op_kind: PagedAttn
2464blurb: "naive paged attention over a blocked KV cache"
2465entry_point: "x::paged_demo"
2466accept:
2467  inputs:
2468    - name: q
2469      dtypes: [F32]
2470      layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }
2471    - name: k_cache
2472      dtypes: [F32]
2473      layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }
2474      fdx:
2475        requires_ext: true
2476        symbolic_extent: required
2477        gather: { kind: paged_blocks, block_table: block_table, context_lens: context_lens }
2478    - name: block_table
2479      dtypes: [U32]
2480      layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }
2481    - name: context_lens
2482      dtypes: [U32]
2483      layout: { contiguous: required, strided: rejected, broadcast_stride0: rejected, start_offset: rejected, reverse_strides: rejected }
2484  op_params: { variant: PagedAttn }
2485return:
2486  outputs:
2487    - name: out
2488      dtype_rule: passthrough(q)
2489cost:
2490  provenance: declared
2491  class: attention
2492  flops: "4 * b * hq * sq * ctx * d"
2493  bytes_moved: "2 * b * hq * sq * d * 4"
2494precision:
2495  bit_stable_on_same_hardware: true
2496  audited: true
2497determinism: same_hardware_bitwise
2498```
2499"#
2500        .to_string()
2501    }
2502
2503    #[test]
2504    fn coherent_registrable_paged_blocks_validates() {
2505        // BORN-RED → GREEN (slice A). Before the import-side lift, a REGISTRABLE
2506        // section whose operand carries `fdx.gather: paged_blocks` failed import
2507        // with `GatherNotYetSupported` (validate_file re-raises it for a
2508        // registrable section — only describe-only ones are swallowed). After the
2509        // lift, the gather block is validated for COHERENCE (kind / requires_ext /
2510        // symbolic_extent / real block_table+context_lens roles) and then ACCEPTED
2511        // — registration of `OpKind::PagedAttn` does not depend on the FDX gather
2512        // descriptor (the block_table/context_lens ride as ordinary U32 operands).
2513        validate_str(&paged_blocks_bundle())
2514            .expect("a coherent registrable paged_blocks section validates + is registrable");
2515    }
2516
2517    #[test]
2518    fn incoherent_gather_missing_requires_ext_still_errors() {
2519        // GUARD: the lift must NOT be a blanket pass. An INCOHERENT gather (here:
2520        // no `requires_ext: true`) still fails with the typed `GatherIncoherent`,
2521        // never a silent pass (never-panic, no-silent-fixup).
2522        let src = paged_blocks_bundle().replace("        requires_ext: true\n", "");
2523        let err = validate_str(&src).expect_err("gather without requires_ext must error");
2524        assert!(
2525            matches!(err, FkcError::GatherIncoherent { ref reason, .. } if reason.contains("requires_ext")),
2526            "got {err:?}",
2527        );
2528    }
2529
2530    #[test]
2531    fn incoherent_gather_dangling_block_table_role_still_errors() {
2532        // GUARD: `gather.block_table` naming a role that is not a real
2533        // `accept.inputs` name is `GatherIncoherent` — the coherence cross-check
2534        // (a table is described in exactly one place + a consistency check, FDX
2535        // §6.9.3) is preserved by the lift.
2536        let src = paged_blocks_bundle().replace("block_table: block_table,", "block_table: nope,");
2537        let err = validate_str(&src).expect_err("dangling block_table role must error");
2538        assert!(
2539            matches!(err, FkcError::GatherIncoherent { ref reason, .. } if reason.contains("block_table")),
2540            "got {err:?}",
2541        );
2542    }
2543
2544    #[test]
2545    fn unknown_gather_kind_is_unknown_admissibility_enum() {
2546        // GUARD: an unknown `gather.kind` (a future ragged/CSR kind FKC cannot yet
2547        // key) is a typed `UnknownAdmissibilityEnum`, never a silent default.
2548        let src = paged_blocks_bundle().replace("kind: paged_blocks,", "kind: ragged_csr,");
2549        let err = validate_str(&src).expect_err("unknown gather kind must error");
2550        assert!(
2551            matches!(err, FkcError::UnknownAdmissibilityEnum { ref value, .. } if value == "ragged_csr"),
2552            "got {err:?}",
2553        );
2554    }
2555
2556    // =====================================================================
2557    // TASK 3 — the CI lint over the whole checked-in contract corpus.
2558    // =====================================================================
2559
2560    /// A permissive stub `LinkRegistry` that resolves any entry_point to a
2561    /// distinct dummy `KernelRef` (so lowering succeeds). The lint checks
2562    /// parse + lower + validate, NOT real registration.
2563    struct StubLink {
2564        seen: std::sync::Mutex<std::collections::HashMap<String, crate::kernel::KernelRef>>,
2565    }
2566    impl StubLink {
2567        fn new() -> Self {
2568            Self {
2569                seen: std::sync::Mutex::new(std::collections::HashMap::new()),
2570            }
2571        }
2572        fn resolve(&self, symbol: &str) -> Option<crate::kernel::KernelRef> {
2573            use std::sync::Arc;
2574            use std::sync::RwLock;
2575            // A family of distinct fn items so distinct symbols get distinct
2576            // pointers (avoids spurious duplicate-pointer collapse on lower —
2577            // lowering does not finalize, but keep them distinct anyway).
2578            fn k0(
2579                _i: &[Arc<RwLock<fuel_memory::Storage>>],
2580                _o: &mut [Arc<RwLock<fuel_memory::Storage>>],
2581                _l: &[fuel_ir::Layout],
2582                _p: &crate::kernel::OpParams,
2583            ) -> fuel_ir::Result<()> {
2584                Ok(())
2585            }
2586            fn k1(
2587                _i: &[Arc<RwLock<fuel_memory::Storage>>],
2588                _o: &mut [Arc<RwLock<fuel_memory::Storage>>],
2589                _l: &[fuel_ir::Layout],
2590                _p: &crate::kernel::OpParams,
2591            ) -> fuel_ir::Result<()> {
2592                Ok(())
2593            }
2594            fn k2(
2595                _i: &[Arc<RwLock<fuel_memory::Storage>>],
2596                _o: &mut [Arc<RwLock<fuel_memory::Storage>>],
2597                _l: &[fuel_ir::Layout],
2598                _p: &crate::kernel::OpParams,
2599            ) -> fuel_ir::Result<()> {
2600                Ok(())
2601            }
2602            fn k3(
2603                _i: &[Arc<RwLock<fuel_memory::Storage>>],
2604                _o: &mut [Arc<RwLock<fuel_memory::Storage>>],
2605                _l: &[fuel_ir::Layout],
2606                _p: &crate::kernel::OpParams,
2607            ) -> fuel_ir::Result<()> {
2608                Ok(())
2609            }
2610            let table: [crate::kernel::KernelRef; 4] = [k0, k1, k2, k3];
2611            let mut g = self.seen.lock().unwrap();
2612            if let Some(k) = g.get(symbol) {
2613                return Some(*k);
2614            }
2615            let k = table[g.len() % table.len()];
2616            g.insert(symbol.to_string(), k);
2617            Some(k)
2618        }
2619    }
2620    impl crate::fkc::lower::LinkRegistry for StubLink {
2621        fn resolve_primitive(&self, s: &str) -> Option<crate::kernel::KernelRef> {
2622            self.resolve(s)
2623        }
2624        fn resolve_fused(&self, s: &str) -> Option<crate::kernel::KernelRef> {
2625            self.resolve(s)
2626        }
2627        fn resolve_cost_fn(&self, _name: &str) -> Option<crate::kernel::CostFn> {
2628            // Permissive (§4.4 cost-fn trampoline): the corpus lint checks that a
2629            // NAMED `cost.cost_fn` parses + lowers, not that it resolves to a
2630            // specific production fn (that is each provider's real link registry's
2631            // job). Any name resolves to a dummy CostFn so a cost-fn-pinning
2632            // contract lowers cleanly.
2633            fn c(
2634                _s: &[fuel_ir::Shape],
2635                _d: &[fuel_ir::DType],
2636                _p: &crate::kernel::OpParams,
2637                _c: &fuel_ir::backend::BackendCapabilities,
2638            ) -> crate::fused::CostEstimate {
2639                crate::fused::CostEstimate::default()
2640            }
2641            Some(c)
2642        }
2643    }
2644
2645    /// Recursively collect every `*.fkc.md` under `dir`, excluding any path
2646    /// containing an `_inventory` component.
2647    fn collect_fkc_files(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
2648        // GAP-157: a corpus sweep that can fail must FAIL, not silently truncate.
2649        // A read_dir error here would drop `.fkc.md` files from the CI lint's
2650        // corpus, and the lint would report "all contracts validated" over an
2651        // incomplete set. (Test-only helper — panic is the correct hard failure.)
2652        let rd = std::fs::read_dir(dir).unwrap_or_else(|e| {
2653            panic!(
2654                "collect_fkc_files: read_dir({}) failed: {e} — the FKC file sweep must FAIL, not silently truncate",
2655                dir.display()
2656            )
2657        });
2658        for entry in rd.flatten() {
2659            let path = entry.path();
2660            if path.is_dir() {
2661                if path.file_name().and_then(|n| n.to_str()) == Some("_inventory") {
2662                    continue;
2663                }
2664                collect_fkc_files(&path, out);
2665            } else if path.to_string_lossy().ends_with(".fkc.md") {
2666                out.push(path);
2667            }
2668        }
2669    }
2670
2671    /// THE CI LINT (Task 3). Walk `fuel-dispatch/kernel-contracts/**/*.fkc.md`
2672    /// (excluding `_inventory/`), parse + lower + validate EVERY file with a
2673    /// permissive stub LinkRegistry, and report the file/section counts. A
2674    /// failing contract is CAPTURED and REPORTED (file + kernel + the rule it
2675    /// tripped), never silently relaxed.
2676    ///
2677    /// `MxNotYetRegistrable` / `GatherNotYetSupported` are
2678    /// describable-but-not-yet-registrable per spec §6/§3.9.1 — they are a
2679    /// CORRECT validate outcome (the contract is legal, the consumer is
2680    /// behind), so the lint records them separately as "deferred", not as
2681    /// hard failures. `FanoutDtypeMismatch` (§3.4) is the same posture at
2682    /// lower time: a MULTI-AXIS dtype contract (varying operands with
2683    /// DIFFERENT dtype lists) is legal but not-yet-fannable by the uniform
2684    /// fan-out importer — surfaced (never silently picked), recorded as
2685    /// deferred, and awaiting a cartesian / per-axis fan-out follow-up.
2686    ///
2687    /// This is a **real CI gate**: it runs as a normal unit test (no
2688    /// `#[ignore]`) and FAILS the build if any checked-in contract has a hard
2689    /// parse/lower/validate failure. The whole corpus is currently clean of hard
2690    /// failures; only the `MxNotYetRegistrable` / `GatherNotYetSupported`
2691    /// deferred (consumer-ahead) cases remain, which are a CORRECT outcome and
2692    /// are NOT counted as failures. Run it verbosely with
2693    /// `cargo test -p fuel-dispatch --lib -- \
2694    ///  ci_lint_corpus_parse_lower_validate --nocapture` to see the full
2695    /// file/section/deferred counts. It does NOT relax a validator or edit the
2696    /// corpus to hide defects — a new hard failure must be fixed (or the section
2697    /// marked describe-only per §3.10) to make this gate green again.
2698    #[test]
2699    fn ci_lint_corpus_parse_lower_validate() {
2700        // Locate the corpus relative to this crate (CARGO_MANIFEST_DIR =
2701        // .../fuel-dispatch). The corpus lives at kernel-contracts/ inside this crate.
2702        let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
2703        let corpus = manifest.join("kernel-contracts");
2704        assert!(
2705            corpus.is_dir(),
2706            "corpus dir not found at {}",
2707            corpus.display()
2708        );
2709
2710        let mut files = Vec::new();
2711        collect_fkc_files(&corpus, &mut files);
2712        // The Vulkan contracts moved with their kernel bindings to `fuel-vulkan-dispatch` (ROADMAP row
2713        // 137 step 6.1); the corpus is still ONE corpus, so walk that directory too.
2714        let vulkan_corpus = manifest.join("../fuel-vulkan-dispatch/kernel-contracts");
2715        assert!(
2716            vulkan_corpus.is_dir(),
2717            "Vulkan corpus dir not found at {}",
2718            vulkan_corpus.display()
2719        );
2720        collect_fkc_files(&vulkan_corpus, &mut files);
2721        files.sort();
2722        assert!(!files.is_empty(), "no .fkc.md files found");
2723
2724        let link = StubLink::new();
2725        let mut file_count = 0usize;
2726        let mut section_count = 0usize;
2727        let mut hard_failures: Vec<String> = Vec::new();
2728        let mut deferred: Vec<String> = Vec::new();
2729
2730        for path in &files {
2731            file_count += 1;
2732            let rel = path
2733                .strip_prefix(manifest)
2734                .unwrap_or(path)
2735                .display()
2736                .to_string();
2737            let src = std::fs::read_to_string(path).expect("read corpus file");
2738
2739            // 1) parse.
2740            let file = match parse_file(&src) {
2741                Ok(f) => f,
2742                Err(e) => {
2743                    hard_failures.push(format!("{rel}: PARSE: {e}"));
2744                    continue;
2745                }
2746            };
2747            section_count += file.kernels.len();
2748
2749            // 2) validate (whole file) — but to attribute a failure to a
2750            // specific kernel + rule, validate each kernel individually too.
2751            // First the file-level (version) check.
2752            if !fkc_version_is_supported(file.front_matter.fkc_version) {
2753                hard_failures.push(format!(
2754                    "{rel}: fkc_version {} not in supported set {:?}",
2755                    file.front_matter.fkc_version, FKC_SUPPORTED_VERSIONS
2756                ));
2757                continue;
2758            }
2759            for kernel in &file.kernels {
2760                let kname = kernel.kernel.as_str();
2761                match validate_kernel(kernel) {
2762                    Ok(()) => {}
2763                    Err(
2764                        e @ (FkcError::MxNotYetRegistrable { .. }
2765                        | FkcError::GatherNotYetSupported { .. }),
2766                    ) => {
2767                        deferred.push(format!("{rel} :: {kname}: {e}"));
2768                    }
2769                    Err(e) => {
2770                        hard_failures.push(format!("{rel} :: {kname}: {e}"));
2771                        continue;
2772                    }
2773                }
2774                // 3) lower (with the stub link) — only attempt when validate
2775                // passed (or deferred). Lowering catches link/dtype/cost issues
2776                // validate doesn't; surface those too.
2777                if let Err(e) = lower_one_kernel(&file, kernel, &link) {
2778                    // MxNotYetRegistrable is a register-time gate, not lower —
2779                    // lowering an MX/AFFINE_BLOCK contract succeeds; any lower
2780                    // error here is a real finding.
2781                    match e {
2782                        FkcError::MxNotYetRegistrable { .. }
2783                        | FkcError::GatherNotYetSupported { .. } => {}
2784                        // Describable but NOT-YET-FANNABLE (§3.4 multi-dtype
2785                        // fan-out): a MULTI-AXIS dtype contract whose varying
2786                        // operands enumerate DIFFERENT dtype lists (e.g. a
2787                        // mixed-precision matmul, or an indexing op with an
2788                        // independent data-dtype axis and index-dtype axis).
2789                        // The uniform fan-out importer surfaces this as a typed
2790                        // error rather than silently picking one operand's list
2791                        // (never a silent pick); registering it needs a
2792                        // cartesian / per-axis fan-out follow-up. Recorded as
2793                        // deferred (consumer-behind), not a hard failure.
2794                        FkcError::FanoutDtypeMismatch { .. } => {
2795                            deferred.push(format!("{rel} :: {kname}: LOWER: {e}"));
2796                        }
2797                        // Describable but NOT-YET-FANNABLE (§3.4 optional-operand
2798                        // fan-out): a section with MULTIPLE / chained trailing
2799                        // optional operands (e.g. Metal layernorm's `alpha` AND
2800                        // `beta`, so `alpha` is optional but not last). The
2801                        // key-builder supports a SINGLE optional LAST input
2802                        // (conv's `bias`); a chained-optional ABI needs a nested
2803                        // fan follow-up. The importer surfaces it as a typed error
2804                        // rather than silently mis-keying (before optional support
2805                        // it was silently coerced to an all-present required key).
2806                        // Recorded as deferred (consumer-behind), not a hard fail.
2807                        FkcError::OptionalOperandNotLast { .. } => {
2808                            deferred.push(format!("{rel} :: {kname}: LOWER: {e}"));
2809                        }
2810                        _ => hard_failures.push(format!("{rel} :: {kname}: LOWER: {e}")),
2811                    }
2812                }
2813            }
2814        }
2815
2816        eprintln!(
2817            "FKC corpus lint: {file_count} files, {section_count} sections; {} deferred (MX/gather not-yet-registrable + multi-axis dtype / chained-optional not-yet-fannable), {} hard failures",
2818            deferred.len(),
2819            hard_failures.len()
2820        );
2821        if !deferred.is_empty() {
2822            eprintln!(
2823                "--- deferred (describable, not-yet-registrable / not-yet-fannable; spec §6/§3.9.1/§3.4) ---"
2824            );
2825            for d in &deferred {
2826                eprintln!("  {d}");
2827            }
2828        }
2829        if !hard_failures.is_empty() {
2830            eprintln!("--- HARD FAILURES (file :: kernel: rule) ---");
2831            for f in &hard_failures {
2832                eprintln!("  {f}");
2833            }
2834            panic!(
2835                "{} corpus contract(s) failed parse/lower/validate — see the list above \
2836                 (REPORT these, do not relax the validators)",
2837                hard_failures.len()
2838            );
2839        }
2840    }
2841
2842    /// Lower a single kernel within a file using the stub link, mirroring what
2843    /// V-FKC-3, corpus arm: **no two registrable contracts anywhere in the
2844    /// checked-in corpus may claim the same `(kernel_source, entry_point)`.**
2845    ///
2846    /// `register_into` already rejects a duplicate `KernelRef` — but only for
2847    /// the providers a given process actually imports, and the corpus lint
2848    /// above stops at *lower*, so cross-file collisions in the checked-in
2849    /// corpus had no safety net. That is the gap this closes.
2850    ///
2851    /// It deliberately does NOT go through `register_into`, and the reason is
2852    /// worth recording: the lint's `StubLink` hands out
2853    /// `table[g.len() % table.len()]` — **four** fn pointers, cycling — so the
2854    /// 5th distinct symbol aliases the 1st. Registering the corpus through it
2855    /// would report a flood of duplicates that are artifacts of the stub, not
2856    /// of the corpus. Synthesizing an unbounded number of distinct `fn`
2857    /// pointers at runtime is not possible, which is why this net could not be
2858    /// built the obvious way.
2859    ///
2860    /// So it asserts the property at its source instead: two contracts naming
2861    /// the same entry point in the same `kernel_source` are exactly the ones a
2862    /// real link registry resolves to one `KernelRef`. A *structural*
2863    /// assertion — it tests the property the duplicate gate is built on, needs
2864    /// no oracle, and diagnoses by NAME rather than by index.
2865    #[test]
2866    fn ci_lint_corpus_has_no_cross_file_entry_point_collisions() {
2867        let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
2868        let corpus = manifest.join("kernel-contracts");
2869        assert!(
2870            corpus.is_dir(),
2871            "corpus dir not found at {}",
2872            corpus.display()
2873        );
2874
2875        let mut files = Vec::new();
2876        collect_fkc_files(&corpus, &mut files);
2877        // The Vulkan contracts moved with their kernel bindings to `fuel-vulkan-dispatch` (ROADMAP row
2878        // 137 step 6.1); the corpus is still ONE corpus, so walk that directory too.
2879        let vulkan_corpus = manifest.join("../fuel-vulkan-dispatch/kernel-contracts");
2880        assert!(
2881            vulkan_corpus.is_dir(),
2882            "Vulkan corpus dir not found at {}",
2883            vulkan_corpus.display()
2884        );
2885        collect_fkc_files(&vulkan_corpus, &mut files);
2886        files.sort();
2887        assert!(!files.is_empty(), "no .fkc.md files found");
2888
2889        // (kernel_source, entry_point) -> first "file :: kernel" that claimed it
2890        let mut claimed: std::collections::HashMap<(String, String), String> =
2891            std::collections::HashMap::new();
2892        // (kernel_source, entry_point, human description)
2893        let mut collisions: Vec<(String, String, String)> = Vec::new();
2894        let mut checked = 0usize;
2895
2896        for path in &files {
2897            let rel = path
2898                .strip_prefix(manifest)
2899                .unwrap_or(path)
2900                .display()
2901                .to_string();
2902            let src = std::fs::read_to_string(path).expect("read corpus file");
2903            let Ok(file) = parse_file(&src) else {
2904                // Parse failures are the other lint's business; skip rather
2905                // than double-report them here.
2906                continue;
2907            };
2908            let kernel_source = file.front_matter.provider.kernel_source.clone();
2909            for kernel in &file.kernels {
2910                // Describe-only sections (§3.10) never reach the binding table,
2911                // so they cannot collide.
2912                if !kernel.registrable {
2913                    continue;
2914                }
2915                let Some(entry) = kernel.entry_point.as_ref() else {
2916                    continue;
2917                };
2918                checked += 1;
2919                let key = (kernel_source.clone(), entry.clone());
2920                let who = format!("{rel} :: {}", kernel.kernel);
2921                match claimed.get(&key) {
2922                    Some(first) => collisions.push((
2923                        key.0.clone(),
2924                        key.1.clone(),
2925                        format!("claimed by BOTH {first} AND {who}"),
2926                    )),
2927                    None => {
2928                        claimed.insert(key, who);
2929                    }
2930                }
2931            }
2932        }
2933
2934        // Report what the instrument actually SAW — a loop that silently
2935        // examined nothing passes identically to one that examined everything.
2936        eprintln!(
2937            "FKC corpus entry-point uniqueness: {checked} registrable contracts across {} files, {} distinct (kernel_source, entry_point) keys",
2938            files.len(),
2939            claimed.len()
2940        );
2941        assert!(
2942            checked > 0,
2943            "the lint examined zero registrable contracts — it is not looking where it thinks"
2944        );
2945
2946        // --- Ratchet, not a cliff ---------------------------------------
2947        //
2948        // The corpus has 13 PRE-EXISTING collisions, all of the same shape: an
2949        // IMPORTED file colliding with an UN-IMPORTED orphan. Verified by
2950        // counting `include_str!` sites — `dispatch/reduce.fkc.md` and
2951        // `vulkan/conv-attn-rope.fkc.md` have ZERO, while their partners
2952        // `cpu/reduce{,-to}.fkc.md` and `vulkan/attention.fkc.md` have two
2953        // each. So none of them registers today and none is a live duplicate.
2954        //
2955        // They are still real debt: two contracts claiming one kernel can
2956        // DISAGREE about precision/cost, which is the divergence FKC exists to
2957        // prevent, and wiring either orphan up would fail registration. They
2958        // are listed rather than deleted because removing corpus contracts is a
2959        // content decision for their authors, not a lint's to make.
2960        //
2961        // The value here is the ratchet: a NEW collision fails immediately.
2962        const KNOWN: &[(&str, &str)] = &[
2963            // dispatch/reduce.fkc.md (orphan) vs cpu/reduce{,-to}.fkc.md
2964            (
2965                "portable-cpu",
2966                "fuel_cpu_backend::byte_kernels::sum_reduce_f32",
2967            ),
2968            (
2969                "portable-cpu",
2970                "fuel_cpu_backend::byte_kernels::max_reduce_f32",
2971            ),
2972            (
2973                "portable-cpu",
2974                "fuel_cpu_backend::byte_kernels::min_reduce_f32",
2975            ),
2976            (
2977                "portable-cpu",
2978                "fuel_cpu_backend::byte_kernels::mean_reduce_f32",
2979            ),
2980            (
2981                "portable-cpu",
2982                "fuel_cpu_backend::byte_kernels::reduce_sum_to_f32",
2983            ),
2984            (
2985                "portable-cpu",
2986                "fuel_cpu_backend::byte_kernels::reduce_max_to_f32",
2987            ),
2988            (
2989                "portable-cpu",
2990                "fuel_cpu_backend::byte_kernels::reduce_max_to_backward_f32",
2991            ),
2992            // vulkan/conv-attn-rope.fkc.md (orphan) vs vulkan/rope.fkc.md: rope_f32 was split
2993            // out of rope.fkc.md's fanned `rope` section to carry its own measured precision
2994            // block (board 240); the orphan names the same literal symbol. The other three
2995            // dtypes still fan from rope.fkc.md's base `rope` symbol and do not collide.
2996            ("vulkan-slang", "fuel_vulkan_backend::fkc::rope_f32"),
2997            // vulkan/conv-attn-rope.fkc.md (orphan) vs vulkan/attention.fkc.md
2998            ("vulkan-slang", "fuel_vulkan_backend::fkc::flash_attn_f32"),
2999            ("vulkan-slang", "fuel_vulkan_backend::fkc::flash_attn_bf16"),
3000            ("vulkan-slang", "fuel_vulkan_backend::fkc::flash_attn_f16"),
3001            (
3002                "vulkan-slang",
3003                "fuel_vulkan_backend::fkc::flash_attn_backward_q_f32",
3004            ),
3005            (
3006                "vulkan-slang",
3007                "fuel_vulkan_backend::fkc::flash_attn_backward_k_f32",
3008            ),
3009            (
3010                "vulkan-slang",
3011                "fuel_vulkan_backend::fkc::flash_attn_backward_v_f32",
3012            ),
3013        ];
3014        let is_known =
3015            |key: &(String, String)| KNOWN.iter().any(|(ks, ep)| *ks == key.0 && *ep == key.1);
3016
3017        let novel: Vec<&(String, String, String)> = collisions
3018            .iter()
3019            .filter(|c| !is_known(&(c.0.clone(), c.1.clone())))
3020            .collect();
3021
3022        // An allowlist that is never re-checked ROTS: a fixed collision would
3023        // sit here forever, and the next reader would believe the debt is
3024        // larger than it is. Fail on a stale entry too.
3025        let seen: std::collections::HashSet<(String, String)> = collisions
3026            .iter()
3027            .map(|c| (c.0.clone(), c.1.clone()))
3028            .collect();
3029        let stale: Vec<&(&str, &str)> = KNOWN
3030            .iter()
3031            .filter(|(ks, ep)| !seen.contains(&((*ks).to_string(), (*ep).to_string())))
3032            .collect();
3033
3034        if !stale.is_empty() {
3035            for (ks, ep) in &stale {
3036                eprintln!("  STALE ALLOWLIST ENTRY (no longer collides): {ks} :: {ep}");
3037            }
3038            panic!(
3039                "{} allowlisted collision(s) no longer occur — delete them from KNOWN so the list keeps reflecting the real debt",
3040                stale.len()
3041            );
3042        }
3043
3044        if !novel.is_empty() {
3045            for (ks, ep, who) in &novel {
3046                eprintln!("  NEW COLLISION: kernel_source `{ks}` entry_point `{ep}` — {who}");
3047            }
3048            panic!(
3049                "{} NEW cross-file entry-point collision(s) — each would resolve to one KernelRef and trip the duplicate gate at registration. Give the new contract its own entry point, or mark it `registrable: false` if it is documentation.",
3050                novel.len()
3051            );
3052        }
3053    }
3054
3055    /// `lower_file` does per-kernel (front-matter defaults applied).
3056    ///
3057    /// (See `ci_lint_corpus_has_no_cross_file_entry_point_collisions` below for
3058    /// the V-FKC-3 corpus arm.)
3059    fn lower_one_kernel(
3060        file: &FkcFile,
3061        kernel: &FkcKernel,
3062        link: &dyn crate::fkc::lower::LinkRegistry,
3063    ) -> Result<(), FkcError> {
3064        // Reuse the public lower_file by lowering the whole file would re-lower
3065        // everything; instead lower just this kernel via a one-kernel clone.
3066        let mut one = file.clone();
3067        one.kernels = vec![kernel.clone()];
3068        crate::fkc::lower::lower_file(&one, link, &mut Vec::new()).map(|_| ())
3069    }
3070}