Repository navigation
Description
Problem
In the diagram editor, interacting with (clicking / micro-moving) certain flows triggers a red error toast:
failed to parse JSON patch: invalid type: null, expected f64 at line 1 column 465
After this the flow renders displaced/broken (its valve jumps to the canvas top edge) and the model effectively cannot be edited: every subsequent move patch serializes the same bad coordinate and is rejected by the engine, so the element -- and effectively the whole model -- becomes uneditable.
Root-cause hypothesis
- The affected flows were created by the OLD buggy flow-tool creation path (before commit 71752f3). That path attached a created flow's sink to a stock by forcing
cursorMoveDelta = {0,0}and routing a DEGENERATE 2-point flow (both points at the press location) throughUpdateCloudAndFlow(src/diagram/drawing/Flow.tsx). For a degenerate single-segment flow, the valve clamp / closest-segment math (findClosestSegment/clampToSegment/getSegmentson a zero-length segment) can produce aNaNcoordinate for the flow's valve (flow.x/flow.y) or for a point. - A
NaNcoordinate serializes to JSONnull, which the Rust engine's patch parser rejects withinvalid type: null, expected f64. Because the bad coordinate is now PERSISTED in the model, every later move/edit re-serializes the null and fails. - Commit 71752f3 FIXED the creation path so newly created flows get clean numeric coordinates (sink snapped to the target stock's center; valve set to the source/target midpoint), so this corruption should no longer be produced going forward. What remains: (a) legacy models may already contain NaN/null coordinates, and (b) there is no defense preventing a NaN coordinate from being serialized in the first place, nor graceful handling when one is encountered.
Why it matters
Data-integrity / availability: a single NaN coordinate makes a model permanently uneditable, and the failure is sticky because the bad value is persisted. There is no in-app recovery path.
Relationship to #812 and #817 (sibling, not duplicate)
This shares the "permanently uneditable model" symptom with #812 but is a DISTINCT root cause:
- Diagram editor: dangling link/flow UID reference makes a model permanently uneditable #812 = a persisted dangling UID reference throws during render (
getElementByUid->defined()), dropping the editor to the ErrorBoundary. - diagram: undo after creating a flow crashes the editor (ErrorBoundary "expected non-undefined object") #817 = a transient undo-path state crash (recoverable by reload).
- This issue = a persisted NaN/null coordinate rejected by the engine's JSON-patch parser (a red toast + broken render, not an ErrorBoundary). It is a sibling mechanism to Diagram editor: dangling link/flow UID reference makes a model permanently uneditable #812: a bad coordinate rather than a bad UID.
Also related: #720 (closed) -- degenerate self-loop flow geometry crash -- same family of "degenerate flow geometry produces bad output," different surface.
Components affected
src/diagram/drawing/Flow.tsx(UpdateCloudAndFlow, and the valve geometry helpersfindClosestSegment/clampToSegment/getSegmentson zero-length segments)- The diagram->engine JSON patch serialization path (where a NaN coordinate becomes
null) - The Rust engine JSON patch parser (rejects
nullfor anf64) - Persisted model data (legacy models created by the pre-71752f3b flow tool)
Possible approaches to evaluate (not yet implemented)
- Defensively guard the geometry helpers (
UpdateCloudAndFlow/clampToSegment/findClosestSegment) against degenerate / zero-length segments so they never emitNaN. - Validate coordinates with
Number.isFinitebefore building/serializing a JSON patch; surface a clear error or drop the bad op rather than sendingnullto the engine. - One-time repair/migration or a load-time sanitizer for models that already contain null/NaN coordinates so they don't stay permanently uneditable.
Discovery
Found while testing flow interaction in the diagram editor. The creation-side root cause is already fixed by commit 71752f3; the NaN-serialization defense and the legacy-data repair remain. Repro is data-dependent: it needs a model whose flow already has a null/NaN coordinate (e.g. one created by the pre-71752f3b flow tool).
Activity
diagram: harden the editor against dangling refs, undo races, and NaN…
bpowers commentedon Jun 29, 2026
Fixed in a2ee852 (on main). Root cause: adjustFlows divided by (origStock - otherEnd) -- zero for a vertical/horizontal flow -- and the cloud branch lacked the || 1 guard the non-cloud branch had, producing a NaN/Infinity coordinate that serialized to JSON null and was rejected by the engine's patch parser. Fixed the division and added two defense layers in core: ProjectController refuses any view update with a non-finite coordinate (findNonFiniteViewCoord), and stockFlowViewFromJson repairs non-finite coordinates on load. (Link x/y are NaN by design and never serialized, so they are deliberately excluded.)