Skip to content

diagram: NaN flow coordinate serializes to JSON null and bricks the model ("invalid type: null, expected f64") #818

Closed

Description

@bpowers

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) through UpdateCloudAndFlow (src/diagram/drawing/Flow.tsx). For a degenerate single-segment flow, the valve clamp / closest-segment math (findClosestSegment / clampToSegment / getSegments on a zero-length segment) can produce a NaN coordinate for the flow's valve (flow.x / flow.y) or for a point.
  • A NaN coordinate serializes to JSON null, which the Rust engine's patch parser rejects with invalid 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:

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 helpers findClosestSegment / clampToSegment / getSegments on zero-length segments)
  • The diagram->engine JSON patch serialization path (where a NaN coordinate becomes null)
  • The Rust engine JSON patch parser (rejects null for an f64)
  • Persisted model data (legacy models created by the pre-71752f3b flow tool)

Possible approaches to evaluate (not yet implemented)

  1. Defensively guard the geometry helpers (UpdateCloudAndFlow / clampToSegment / findClosestSegment) against degenerate / zero-length segments so they never emit NaN.
  2. Validate coordinates with Number.isFinite before building/serializing a JSON patch; surface a clear error or drop the bad op rather than sending null to the engine.
  3. 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

  1. bpowers commented on Jun 29, 2026

    @bpowers
    OwnerAuthor

    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.)

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