Skip to content

diagram: engine error codes mistranslated across the libsimlin FFI boundary (three skewed ErrorCode enums) #835

Closed

Description

@bpowers

Problem

The web editor renders nonsense error prefixes because there are three differently-numbered ErrorCode enums and the diagram casts wire codes from one numbering into another:

  1. Rust engine enum simlin_engine::common::ErrorCode (src/simlin-engine/src/common.rs): the full enum, where NoAppInUnits = 33 and UnitMismatch = 44.
  2. libsimlin C-ABI enum SimlinErrorCode (src/libsimlin/src/lib.rs:96): a collapsed enum where all the units-definition codes collapse into Generic = 32, so UnitMismatch = 33 and BadOverride = 34. This is the numbering that actually crosses the WASM/FFI boundary in error details (From<engine::ErrorCode> for SimlinErrorCode at lib.rs:238 does the collapse).
  3. TS enum ErrorCode in src/core/errors.ts (with a hand-maintained duplicate copy in src/engine/src/errors.ts): a copy of the engine numbering, where 33 = NoAppInUnits, 34 = NoSubscriptInUnits, 44 = UnitMismatch, with errorCodeDescription() mapping codes to human strings.

The diagram's convertErrorDetails in src/diagram/project-controller.ts (lines 206, 220; same pattern again at 1111, 1116) does:

code: err.code as unknown as ErrorCode,

casting a libsimlin wire code into the TS engine-numbered enum. Every wire code >= 33 is mistranslated:

  • libsimlin UnitMismatch (33) renders as TS NoAppInUnits -- observed symptom: a unit-inference warning displays in ErrorDetails.tsx as "model error: Function calls are not allowed in unit definition: ..."
  • libsimlin BadOverride (34) would render as TS NoSubscriptInUnits ("subscripts are not allowed in unit definitions"), etc.

Why it matters

Correctness of user-facing diagnostics: users see confidently wrong error explanations for real model problems (unit mismatches are common), which actively misleads debugging. It also undermines any code that branches on specific TS ErrorCode values for FFI-sourced errors.

Components affected

  • src/diagram (project-controller.ts convertErrorDetails, ErrorDetails.tsx display)
  • src/core/errors.ts and src/engine/src/errors.ts (two hand-maintained identical copies; neither is generated from or checked against the Rust enums, so this class of skew will recur without a guard)
  • src/libsimlin (source of the wire numbering)

Possible approaches

  • Make the TS ErrorCode enum mirror the libsimlin C-ABI numbering (the actual wire format), or convert codes explicitly at the FFI boundary instead of casting.
  • Deduplicate src/core/errors.ts vs src/engine/src/errors.ts into a single source.
  • Add a cross-language guard test (or codegen) that asserts the TS numbering matches SimlinErrorCode in src/libsimlin/src/lib.rs, so future enum edits cannot silently skew.

Context

Identified while debugging why a unit-inference warning rendered with a "Function calls are not allowed in unit definition" prefix in the web editor.

Activity

  1. bpowers commented on Jul 4, 2026

    @bpowers
    OwnerAuthor

    Fixed in ed29894 (diagram: fix unit-error display). The TS ErrorCode enums in src/core/errors.ts and src/engine/src/errors.ts now mirror the libsimlin wire numbering (UnitMismatch = 33, BadOverride = 34), the units-definition parse codes are mapped faithfully across the FFI as additive discriminants 35-45 instead of collapsing to Generic, and a comment on each enum pins the sync invariant. The GUI additionally stopped rendering the terminal-formatted message: the bare reason now crosses the FFI in a separate details field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions