Skip to content

diagram: three-valued unit-error kind; plain-language inference text - #838

Merged
bpowers merged 1 commit into
mainfrom
unit-inference-error-kind
Jul 5, 2026
Merged

bpowers merged 1 commit into
mainfrom
unit-inference-error-kind

Conversation

@bpowers

@bpowers bpowers commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Fixes #836

Why

The diagram's UnitError collapsed the engine's three-valued unit-error kind (Definition | Consistency | Inference) into a boolean isConsistencyError, so a unit inference error was handled exactly like a definition error: its equation-relative offsets were applied to the units string, red-underlining an arbitrary wrong span of the units box.

What

  • Core: UnitError.isConsistencyError is replaced by kind: 'definition' | 'consistency' | 'inference', mirroring the engine's SimlinUnitErrorKind. (unitErrors are attached at runtime and never serialized, so no proto/compat concern.)
  • Placement (Option A from the issue): inference errors underline nothing in either the units or equation field. They describe a contradiction spanning several variables, so any single-field span would be arbitrary -- and their offsets come from sources.first(), which may point into a different variable's equation. Their text renders in the details panel and errors panel instead. The whole-declaration units underline now fires only when a consistency error is actually present, instead of for any non-definition unit error.
  • User-facing text: the raw inference reason is a 1 == unit-expression constraint dump with @N metavariables -- meaningful in terminal output, noise for modelers. The engine now synthesizes a plain-language details naming the involved variables ("the units of 'births' and 'population' are inconsistent with each other; check the equations and declared units of these variables"); the terminal-formatted message keeps the full dump for CLI/MCP consumers. A single-line reason (e.g. the clarified macro conflict from GH engine: macro unit polymorphism is contained, not resolved #619) passes through untouched. The model-level umbrella diagnostic uses the same helper, and the diagram's model-error list prefers the bare details over the terminal-formatted message.

Notes

  • The per-variable inference path is currently latent -- the salsa pipeline surfaces inference conflicts only through the model-level umbrella (variable: None) -- so this pins the placement rule with tests before it can go live. Per-source attribution (the deferred "Option D") is tracked in engine: unit inference errors are not attached to any variable #837.
  • Browser-verified end-to-end against the rebuilt WASM: consistency squiggle spans the whole units declaration, definition squiggle lands on exact offsets, the friendly umbrella sentence renders in the errors panel, and fixing the units returns the model to error-free.
  • Adversarially reviewed by a sub-agent; both findings addressed (order-preserving name dedupe so a two-location single-variable conflict never reads "'x' and 'x'", plus a test pinning the details ?? message preference).

🤖 Generated with Claude Code

https://claude.ai/code/session_01DyGQwfTBHuwGi7C7m5b3Th

The diagram's UnitError collapsed the engine's three-valued unit-error
kind (Definition | Consistency | Inference) into a boolean
isConsistencyError, so a unit inference error was handled exactly like
a definition error: its equation-relative offsets were applied to the
units string, underlining an arbitrary wrong span of the units box.
Core's UnitError now carries kind: 'definition' | 'consistency' |
'inference', and inference errors underline nothing in either field --
they describe a contradiction spanning several variables, so any
single-field span would be arbitrary (and their offsets may point into
a different variable's equation). Their text renders in the details
panel and errors panel instead.

For that text to be usable by modelers, the engine now synthesizes a
plain-language reason for inference errors: unit_inference_reason
names the involved variables ("the units of 'a' and 'b' are
inconsistent with each other; check the equations and declared units
of these variables") instead of the raw `1 == unit-expression`
constraint dump, which stays in the terminal-formatted message for
CLI consumers. A single-line reason (e.g. the clarified macro
conflict) passes through untouched. The model-level inference umbrella
diagnostic in db/units.rs uses the same helper, format_diagnostic's
Model arm now carries Error.details through to FormattedError.details,
and the diagram's modelErrors prefer details over the
terminal-formatted message. Source names are deduped order-preserving
because upstream dedup is by (var, loc), which permits repeated names.

The per-variable inference path is currently latent (the salsa
pipeline surfaces inference conflicts only through the model-level
umbrella), so this pins the placement rule before it can go live;
per-source attribution is tracked in #837.
@claude

claude Bot commented Jul 5, 2026

Copy link
Copy Markdown

Reviewed the diff — I did not find any bugs that meet the flagging bar. The change is coherent end-to-end (engine emits three-valued kind + user-facing details, diagram maps them correctly, tests pin the new placement rule), and I checked the areas where I expected friction:

  • convertUnitErrorKind defaults NotApplicable → 'definition' — the caller only invokes it for SimlinErrorKind.Units, so the default is unreachable in production; the comment correctly flags it as defensive.
  • Widening format_diagnostic's Model arm to propagate err.details — all existing Model errors that set details (module wiring messages in db/diagnostic.rs, cycle messages, unit checking failed: … in db/units.rs) already carry user-facing text, and refreshCachedErrors' details ?? message fallback drops the redundant "error in model 'main': …" prefix — an improvement, not a regression.
  • Slice-pattern order in join_quoted_names — [a, b, c] correctly wins over [a, b, c, rest @ ..] for exactly three names; rest.len() is only ever ≥1 in the truncated arm.
  • The per-variable inference return undefined in highlightRangeForField — currently latent (salsa surfaces inference conflicts only via the model-level umbrella with variable: None), but the tests pin the placement rule before it can go live, which is the right call.

Minor observation, not a defect: for 4 names join_quoted_names renders as 'a', 'b', 'c', and 1 more, which is grammatically fine but loses one name where listing all four would fit. Intentional per the docstring's readability rationale; noting only in case you want a n == 4 => list all special case later.

Overall verdict: correct.

@codecov

codecov Bot commented Jul 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.36066% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.86%. Comparing base (ed29894) to head (f8d2971).

Files with missing lines Patch % Lines
src/simlin-engine/src/db/units.rs 83.33% 1 Missing ⚠️
src/simlin-engine/src/unit_checking_test.rs 95.45% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main     #838    +/-   ##
========================================
  Coverage   90.86%   90.86%            
========================================
  Files         224      224            
  Lines      143185   143302   +117     
========================================
+ Hits       130101   130218   +117     
  Misses      13084    13084            

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bpowers
bpowers merged commit f6bc72c into main Jul 5, 2026
13 of 15 checks passed
@bpowers
bpowers deleted the unit-inference-error-kind branch July 5, 2026 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

diagram: unit inference errors underlined in the wrong place (UnitError kind collapsed to a boolean)

1 participant