From a534bc27604c35a36ffdb66feba484faa023ed18 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 00:44:40 +0000 Subject: [PATCH 1/3] fix(abi): pin the coprocessor kind tags on the Rust side and tie them in CI Coprocessor.Foreign maps each CoprocessorKind to a u8 tag (0 to 15). The Rust enum had no discriminants, so those tags matched it only by declaration order. Reordering or inserting a Rust variant would have moved the Rust side silently. - The Rust enum is now `#[repr(u8)]` with explicit discriminants 0 to 15. Nothing in Rust casts the kind today, so behaviour is unchanged (`cargo check --locked --lib` and `cargo fmt --check` pass). - scripts/ci/check-coprocessor-tags.sh compares the two lists as Name=tag pairs. It fails on any difference, on a Rust variant with no explicit discriminant, or when either list is empty. Tested on the tree (16 kinds agree) and on two broken copies: a swapped Idris tag and a bare Rust variant. Both fail with an ::error. The Idris2 ABI workflow runs it on every event, and on pushes to main that touch types.rs. - The header of Coprocessor/Foreign.idr said "Phase 0 declares the Math kind only". It declares sixteen kinds, and the header now says so. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019a95kk88TZ1iegsBK3kox3 --- .github/workflows/idris2-abi-ci.yml | 6 +++++ scripts/ci/check-coprocessor-tags.sh | 37 +++++++++++++++++++++++++++ src/abi/Coprocessor/Foreign.idr | 9 ++++--- src/rust/coprocessor/types.rs | 38 ++++++++++++++++------------ 4 files changed, 70 insertions(+), 20 deletions(-) create mode 100755 scripts/ci/check-coprocessor-tags.sh diff --git a/.github/workflows/idris2-abi-ci.yml b/.github/workflows/idris2-abi-ci.yml index a2ef75e5..304e7460 100644 --- a/.github/workflows/idris2-abi-ci.yml +++ b/.github/workflows/idris2-abi-ci.yml @@ -13,6 +13,8 @@ on: - 'src/idris/**' - 'tests/abi/**' - 'scripts/abi-fixtures.sh' + - 'src/rust/coprocessor/types.rs' + - 'scripts/ci/check-coprocessor-tags.sh' - '.github/workflows/idris2-abi-ci.yml' # No paths-filter on pull_request: this job is a REQUIRED status check, so it # must report on every PR (a path-filtered required check deadlocks PRs that @@ -134,6 +136,10 @@ jobs: exit 1 fi + - name: Coprocessor kind tags agree with the Rust enum + # Runs on every event: a Rust-only renumbering must not skip it. + run: bash scripts/ci/check-coprocessor-tags.sh + - name: Count modules and report if: steps.detect.outputs.relevant == 'true' run: | diff --git a/scripts/ci/check-coprocessor-tags.sh b/scripts/ci/check-coprocessor-tags.sh new file mode 100755 index 00000000..34462faa --- /dev/null +++ b/scripts/ci/check-coprocessor-tags.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell (hyperpolymath) +# +# The FFI tag of each coprocessor kind is written twice: as an explicit +# discriminant of the Rust `CoprocessorKind` (src/rust/coprocessor/types.rs) +# and as a clause of `coprocessorKindToU8` in src/abi/Coprocessor/Foreign.idr. +# Neither compiler sees the other file, so this compares the two lists as +# "Name=tag" pairs. It fails if either list is empty, if a Rust variant has no +# explicit discriminant, or if the pairs differ. +set -euo pipefail +root=${1:-.} +rs="$root/src/rust/coprocessor/types.rs" +idr="$root/src/abi/Coprocessor/Foreign.idr" + +body=$(awk '/^pub enum CoprocessorKind \{/{f=1;next} f&&/^\}/{exit} f' "$rs") +bare=$({ printf '%s\n' "$body" | grep -E '^[[:space:]]+[A-Z][A-Za-z0-9]*,[[:space:]]*$' || true; }) +if [ -n "$bare" ]; then + echo "::error file=$rs::CoprocessorKind variants without an explicit discriminant:" + printf '%s\n' "$bare" + exit 1 +fi +rust=$({ printf '%s\n' "$body" | grep -oE '^[[:space:]]+[A-Z][A-Za-z0-9]*[[:space:]]*=[[:space:]]*[0-9]+' || true; } \ + | tr -d ' ' | sort) +idris=$({ grep -oE '^coprocessorKindToU8[[:space:]]+[A-Z][A-Za-z0-9]*[[:space:]]*=[[:space:]]*[0-9]+' "$idr" || true; } \ + | sed -E 's/^coprocessorKindToU8[[:space:]]+//' | tr -d ' ' | sort) + +if [ -z "$rust" ] || [ -z "$idris" ]; then + echo "::error::could not read the tags (rust: ${rust:+found}${rust:-none}, idris: ${idris:+found}${idris:-none})" + exit 1 +fi +if [ "$rust" != "$idris" ]; then + echo "::error file=$idr::coprocessorKindToU8 disagrees with the Rust CoprocessorKind discriminants" + diff <(printf '%s\n' "$rust") <(printf '%s\n' "$idris") | sed 's/^/idris/' || true + exit 1 +fi +echo "coprocessor tags agree: $(printf '%s\n' "$rust" | wc -l) kinds" diff --git a/src/abi/Coprocessor/Foreign.idr b/src/abi/Coprocessor/Foreign.idr index 23258362..19b595ad 100644 --- a/src/abi/Coprocessor/Foreign.idr +++ b/src/abi/Coprocessor/Foreign.idr @@ -9,9 +9,9 @@ -- src/rust/coprocessor/mod.rs (Coprocessor trait surface) -- src/ada/spark/coprocessor_safety.ads (SPARK trust-tier invariant) -- --- Phase 0 declares the Math kind only. Phase 6 backends (Physics, DSP, --- FPGA, Tensor, Vector, Crypto, Graphics, Audio, IO) extend this module --- alongside their Rust impls — never as stubs. +-- It declares all sixteen kinds the Rust enum declares, with the same u8 tags +-- (scripts/ci/check-coprocessor-tags.sh checks the two agree). A new kind is +-- added here alongside its Rust impl, with the next tag — never as a stub. -- -- NO believe-me anywhere. All safety is enforced by types. @@ -93,7 +93,8 @@ encodeDecodeId PureFormal = Refl ||| ABI surface is unconditional. Phase 2b adds the subprocess CAS ||| backends PariGp / Maxima / Singular / Gap / Macaulay2. The order and the ||| tags follow the declaration order of `CoprocessorKind` in -||| `src/rust/coprocessor/types.rs`, which carries no explicit discriminants. +||| `src/rust/coprocessor/types.rs`, whose explicit `#[repr(u8)]` discriminants +||| equal these tags (scripts/ci/check-coprocessor-tags.sh). public export data CoprocessorKind : Type where Math : CoprocessorKind -- Phase 0 (num-bigint + num-integer) diff --git a/src/rust/coprocessor/types.rs b/src/rust/coprocessor/types.rs index 777dca24..e1f5fe91 100644 --- a/src/rust/coprocessor/types.rs +++ b/src/rust/coprocessor/types.rs @@ -22,82 +22,88 @@ use serde::{Deserialize, Serialize}; /// Only variants with a fully-functional backend are present. Phase 6A /// adds Vector / Tensor / Crypto / Physics; Phase 6B will add DSP / FPGA / /// Graphics / Audio / IO together with their implementations. +/// +/// Discriminants are explicit and `#[repr(u8)]`: they are the FFI tags that +/// `src/abi/Coprocessor/Foreign.idr` (`coprocessorKindToU8`) mirrors, and +/// `scripts/ci/check-coprocessor-tags.sh` fails CI if the two disagree. +/// Append new kinds with the next number; never renumber. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[repr(u8)] pub enum CoprocessorKind { /// Number theory, exact arithmetic, polynomial algebra over Q. /// Native Rust impl backed by `num-bigint` / `num-integer` / `num-rational`. - Math, + Math = 0, /// Dense vector arithmetic (auto-vectorised f64 loops). Backs /// SIMD-batched ops for the GNN inference path and aggregate reasoning. - Vector, + Vector = 1, /// Dense tensor / matrix linear algebra via `nalgebra`. Backs node- /// embedding aggregation, Pareto-frontier dominance computations, and /// the matrix kernels that cross-prover certificate exchange depends on. - Tensor, + Tensor = 2, /// Cryptographic primitives — hashing, signature verification. /// Pairs with the future EasyCrypt / Tamarin / ProVerif backends as /// the *computational* (vs symbolic) crypto surface. - Crypto, + Crypto = 3, /// Numerical integration of ODE / Hamiltonian systems. Pairs with the /// future KeYmaera X backend as a sanity-check oracle for hybrid-system /// trajectories proven correct symbolically. - Physics, + Physics = 4, /// FLINT integer and polynomial algebra — the only CAS backend that links /// the library in-process rather than shelling out. Requires /// `--features flint` and a system FLINT (LGPL-3) installation. /// Enabled by `CoprocessorFactory::native`; absent without the feature. - FlintMath, + FlintMath = 5, /// Digital signal processing — FFT/IFFT, window functions. Backed by /// `rustfft`. Pairs with the Tamarin / ProVerif backends for proofs /// about communication channels and signal-bearing protocols. - Dsp, + Dsp = 6, /// File and stream I/O. Async via `tokio::fs`. Backs proof-artifact /// handling — read a Coq .v file, hash it, line-count it. - Io, + Io = 7, /// 2-D graphics — emits SVG strings. No GPU dependency. Backs the /// proof-graph visualisation that the GNN-guided search consumes, /// and renders Pareto-frontier diagnostics. - Graphics, + Graphics = 8, /// Audio synthesis — generates PCM / WAV. Backs the completion-chime /// machinery and any audio-signal proof obligations (rare but real /// for cyber-physical KeYmaera X targets that involve audible signals). - Audio, + Audio = 9, /// FPGA / hardware verification — subprocess to yosys. Trust tier /// `ExternalSubprocess` (Tier 1). Backs proofs about hardware modules /// expressed in Verilog / SystemVerilog. - Fpga, + Fpga = 10, // ── Phase 2b: subprocess CAS backends (ExternalSubprocess / Tier 1) ── /// PARI/GP number theory — subprocess to `gp`. Advanced factoring (ECM / /// GNFS), multiplicative order, next-prime queries. PARI is the gold /// standard for algorithmic number theory. - PariGp, + PariGp = 11, /// Maxima symbolic algebra — subprocess to `maxima`. Simplification, /// polynomial factoring, symbolic differentiation. Free CAS, GPL. - Maxima, + Maxima = 12, /// Singular polynomial algebra — subprocess to `Singular`. Gröbner bases, /// ideal membership, Krull dimension over arbitrary fields. GPL. - Singular, + Singular = 13, /// GAP group theory — subprocess to `gap`. Permutation group order, /// abelianness test, symmetric group order. GPL. - Gap, + Gap = 14, /// Macaulay2 algebraic geometry — subprocess to `M2`. Gröbner bases, /// Krull dimension, degree of quotient rings over QQ. GPL. - Macaulay2, + Macaulay2 = 15, } /// A request dispatched to a coprocessor. From ab2126355a9a0685ec4213017d71f59d576be202 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 00:46:25 +0000 Subject: [PATCH 2/3] style(ci): use [[ ]] in check-coprocessor-tags.sh (SonarCloud S7688) Same tests as before: the tree passes (16 kinds), and the swapped-tag and bare-variant copies both fail with exit 1. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019a95kk88TZ1iegsBK3kox3 --- scripts/ci/check-coprocessor-tags.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/ci/check-coprocessor-tags.sh b/scripts/ci/check-coprocessor-tags.sh index 34462faa..3bbdd3a4 100755 --- a/scripts/ci/check-coprocessor-tags.sh +++ b/scripts/ci/check-coprocessor-tags.sh @@ -15,7 +15,7 @@ idr="$root/src/abi/Coprocessor/Foreign.idr" body=$(awk '/^pub enum CoprocessorKind \{/{f=1;next} f&&/^\}/{exit} f' "$rs") bare=$({ printf '%s\n' "$body" | grep -E '^[[:space:]]+[A-Z][A-Za-z0-9]*,[[:space:]]*$' || true; }) -if [ -n "$bare" ]; then +if [[ -n "$bare" ]]; then echo "::error file=$rs::CoprocessorKind variants without an explicit discriminant:" printf '%s\n' "$bare" exit 1 @@ -25,11 +25,11 @@ rust=$({ printf '%s\n' "$body" | grep -oE '^[[:space:]]+[A-Z][A-Za-z0-9]*[[:spac idris=$({ grep -oE '^coprocessorKindToU8[[:space:]]+[A-Z][A-Za-z0-9]*[[:space:]]*=[[:space:]]*[0-9]+' "$idr" || true; } \ | sed -E 's/^coprocessorKindToU8[[:space:]]+//' | tr -d ' ' | sort) -if [ -z "$rust" ] || [ -z "$idris" ]; then +if [[ -z "$rust" ]] || [[ -z "$idris" ]]; then echo "::error::could not read the tags (rust: ${rust:+found}${rust:-none}, idris: ${idris:+found}${idris:-none})" exit 1 fi -if [ "$rust" != "$idris" ]; then +if [[ "$rust" != "$idris" ]]; then echo "::error file=$idr::coprocessorKindToU8 disagrees with the Rust CoprocessorKind discriminants" diff <(printf '%s\n' "$rust") <(printf '%s\n' "$idris") | sed 's/^/idris/' || true exit 1 From df3c10d21e704fb47e4b1f8186e44a85ef591187 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 00:55:00 +0000 Subject: [PATCH 3/3] fix(ci): refuse any coprocessor tag that is not a plain decimal Codex (P2): the Rust regex stopped at the first digits, so `Math = 0x10` (Rust tag 16) read as Math=0 and the check reported agreement. The Idris side had the same gap. The script now requires every Rust variant line to read `Name = ` (trailing comma optional) and every coprocessorKindToU8 clause to read `coprocessorKindToU8 Name = `. Any other shape (hex, underscores, an expression, a trailing comment) fails with an ::error that names the line. Tested: the tree passes, as does a copy without the last variant's comma. Each of these fails with exit 1: a swapped Idris tag, a bare Rust variant, `Math = 0x10` in Rust, and `= 0x10` in Idris. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019a95kk88TZ1iegsBK3kox3 --- scripts/ci/check-coprocessor-tags.sh | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/scripts/ci/check-coprocessor-tags.sh b/scripts/ci/check-coprocessor-tags.sh index 3bbdd3a4..b805246b 100755 --- a/scripts/ci/check-coprocessor-tags.sh +++ b/scripts/ci/check-coprocessor-tags.sh @@ -14,16 +14,26 @@ rs="$root/src/rust/coprocessor/types.rs" idr="$root/src/abi/Coprocessor/Foreign.idr" body=$(awk '/^pub enum CoprocessorKind \{/{f=1;next} f&&/^\}/{exit} f' "$rs") -bare=$({ printf '%s\n' "$body" | grep -E '^[[:space:]]+[A-Z][A-Za-z0-9]*,[[:space:]]*$' || true; }) -if [[ -n "$bare" ]]; then - echo "::error file=$rs::CoprocessorKind variants without an explicit discriminant:" - printf '%s\n' "$bare" +# Every variant line must be exactly `Name = ` (comma optional). Anything else (no +# discriminant, a hex or underscored literal, an expression) is refused rather +# than half-parsed: a regex that stopped at the first digits would read +# `Math = 0x10` as Math=0 and pass a real mismatch. +variants=$({ printf '%s\n' "$body" | grep -E '^[[:space:]]+[A-Z]' || true; }) +odd=$({ printf '%s\n' "$variants" | grep -vE '^[[:space:]]+[A-Z][A-Za-z0-9]*[[:space:]]*=[[:space:]]*[0-9]+,?[[:space:]]*$' || true; }) +if [[ -n "$odd" ]]; then + echo "::error file=$rs::CoprocessorKind variants must read 'Name = ' (comma optional); these do not:" + printf '%s\n' "$odd" exit 1 fi -rust=$({ printf '%s\n' "$body" | grep -oE '^[[:space:]]+[A-Z][A-Za-z0-9]*[[:space:]]*=[[:space:]]*[0-9]+' || true; } \ - | tr -d ' ' | sort) -idris=$({ grep -oE '^coprocessorKindToU8[[:space:]]+[A-Z][A-Za-z0-9]*[[:space:]]*=[[:space:]]*[0-9]+' "$idr" || true; } \ - | sed -E 's/^coprocessorKindToU8[[:space:]]+//' | tr -d ' ' | sort) +rust=$(printf '%s\n' "$variants" | sed -E 's/,?[[:space:]]*$//' | tr -d ' ' | sort) +clauses=$({ grep -E '^coprocessorKindToU8[[:space:]]+[A-Z]' "$idr" || true; }) +odd=$({ printf '%s\n' "$clauses" | grep -vE '^coprocessorKindToU8[[:space:]]+[A-Z][A-Za-z0-9]*[[:space:]]*=[[:space:]]*[0-9]+[[:space:]]*$' || true; }) +if [[ -n "$clauses" && -n "$odd" ]]; then + echo "::error file=$idr::coprocessorKindToU8 clauses must read 'coprocessorKindToU8 Name = '; these do not:" + printf '%s\n' "$odd" + exit 1 +fi +idris=$(printf '%s\n' "$clauses" | sed -E 's/^coprocessorKindToU8[[:space:]]+//' | tr -d ' ' | sed '/^$/d' | sort) if [[ -z "$rust" ]] || [[ -z "$idris" ]]; then echo "::error::could not read the tags (rust: ${rust:+found}${rust:-none}, idris: ${idris:+found}${idris:-none})"