DEV Community

Yamrail
Yamrail

Posted on AI-assisted

YamRail-Calculator: Fail-Closed Python + Rust Gate for LLM Engineering Math

Engineering calculations are an unforgiving domain for language models. A minor unit mismatch, a silently ignored convergence failure, or a malformed JSON payload can turn a plausible-looking report into a serious physical or operational hazard.

We have just published YamRail-Calculator (commit 29a0555, AGPL-3.0-or-later), containing two companion implementations:

  • python/ — Python Reference Edition: Standard-library-only reference implementation and golden test suite.
  • rust/ — Rust SLM Extension Unit: A single-binary CLI (sieve-gate, ~649 KB on Windows 11 x86_64) built to sit next to local Small Language Models (SLMs) or vendor-agnostic LLM workflows without panicking on hostile or malformed inputs.

Non-Authority Notice: YamRail-Calculator is not a design-approval system, a Professional Engineer (PE) substitute, or a structural safety certification tool. Every emitted receipt hardcodes authority_effect: "NONE", canonical_effect: "NONE", human_gate: "NOT_EXECUTED", and external_effect_allowed: false. Its sole role is to act as a deterministic checkpoint for input hygiene, contract fields, dimensional units, process-tree cleanup, and monotonic exit codes.

Standing on CMU Barati Farimani Lab's Shoulders

This work builds directly on the architectural principles and skill contracts published by the Barati Farimani Lab at Carnegie Mellon University:

Their core rule—the model orchestrates while deterministic tools compute and return checks—is the right foundation. The upstream MIT copyright notice (Copyright (c) 2026 Amir Barati Farimani) is preserved in NOTICE, and all Rust crate licenses are enumerated in THIRD_PARTY_LICENSES.md.

What a 33/33 Static Audit Revealed

Before wiring those contracts into a local workflow, we ran a static source audit across all 33 upstream skills. Three practical friction points stood out:

  1. Soft-Gate Bypass (Exit 0 on failure): 17 of 33 skills return OS exit code 0 with a JSON payload even when checks fail (e.g., hard_fail: true, equilibrium_error_pct >= 1.0, or negative margin of safety status: "FAIL"). Downstream LLMs can easily soften or misreport those failures.
  2. Missing Script Gates & Fallbacks: 4 skills have no scripts/ directory (relying on SKILL.md + references/), and several skills include heuristic, synthetic, or self-judgment fallbacks when optional packages are absent.
  3. Copyleft Isolation Boundaries: 6 skills import GPL/LGPL libraries in-process (gmsh GPL-2.0-or-later, cadquery/OCP LGPL-2.1, shapely/GEOS LGPL-2.1), whereas OpenFOAM and FreeCAD are already isolated via CLI subprocesses.

The 3-Stage Serial Gate Architecture

To insulate probabilistic LLM/SLM orchestration from deterministic solvers—especially when small local models emit malformed JSON, out-of-range numbers, or unsafe paths—we placed three serial gates in sequence:

  1. Gate 1 — Ingress Precheck (geom-precheck, Pre-LLM): Rejects non-UTF-8 byte sequences, Shift-JIS/CRLF issues, NUL/control bytes, duplicate JSON keys, NaN/Infinity/1e309, and deeply nested JSON before data reaches downstream parsers. Emits input_sha256.
  2. Gate 2 — Pre-Solver Hook (pre_gate): Reserved for schema/parameter screening (Pravda Screen). Until the upstream v0.5 structural fix is promoted, this gate refuses silent pass-through and forces HOLD (Exit 78 mapped to Exit 12 INCONCLUSIVE) unless an explicit human override token is provided.
  3. Gate 3 — Egress Checkpoint (sieve-gate, Post-Solver): Enforces strict monotonic exit precedence:
13 (SECURITY_VIOLATION / LEAK / TOCTOU)
  > 11 (OUT_OF_SCOPE)
  > 10 (REJECT)
  > 12 (INCONCLUSIVE / HOLD / TAINT)
  > 0  (ACCEPT)
Enter fullscreen mode Exit fullscreen mode

A downstream ACCEPT (0) can never overwrite an upstream HOLD, tainted (synthetic/degraded_mode) parent receipt, or security violation.

What Destructive Testing Actually Caught

Happy-path parity tests passed early, but toxic-combination and Windows-specific destructive tests (39 cases directly injected into sieve-gate.exe, verified alongside stateless editorial/security review) surfaced real bugs before release:

  • Silent float-to-default coercion: Passing mc_samples: 1e9 as a JSON float caused .as_u64() to return None and silently fall back to 10_000. Now strictly rejected with Exit 11.
  • Windows path-escape edge cases: Beyond basic ../ traversal, testing caught uncreated-leaf NTFS junctions, mixed-separator UNC paths (\/, /\), drive-relative paths (C:..\outside), and NT namespace prefixes (\??\...). All now terminate fail-closed with Exit 13.
  • Orphan descendant processes: Using Win32 Job Objects (JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE), child processes that spawn grandchildren and exit 0 (or hang until timeout) no longer leave live orphan PIDs behind.

All 39 destructive cases ran with 0 panics and matched the Python reference implementation across 24/24 cross-implementation parity checks.

Known Limitations & Links

  • Verified Environment: Current destructive and Win32 Job Object tests are verified on Windows 11 x86_64; Linux/macOS CI verification is not yet completed.
  • No External Solvers Bundled: OpenFOAM, FreeCAD, Gmsh, and OpenCASCADE are not bundled or statically linked; they remain out-of-process CLI/IPC workers.
  • Bilingual Documentation: English is authoritative (README.md, docs/WHY_YAMRAIL_INTEGRATION.md, docs/PROVENANCE_AND_AUDIT_TRAIL.md), with Japanese companions (*.ja.md) and a shared docs/BILINGUAL_NORMATIVE_GLOSSARY.md.

Repository: https://github.com/Yamrail/YamRail-Calculator/

Top comments (0)