layout is handed out live, outlives its device, accepts anything (NaN boxes, silently lost arrays), and the API reads 0 as missing #1325

Closed
opened 2026-11-10 03:07:53 +00:00 by jaylouisw · 0 comments
Owner

The third thing a board holds, after devices (#1321) and cables (#1323). layout — where each box sits — was
the least guarded of the three.

Measured against the current code:

1. serialize().layout === topo.layout: true
   write to the document -> the board says d1.x = 999
   add a box to the document -> the board has a box at {x:1,y:1}
2. after removeDevice('d2'): layout still holds d2, in memory AND in the next save
3. a document with a position for a device it does not contain -> kept as a ghost
4. {"x":"left","y":"top"} -> adopted; the canvas draws it at NaN
   {"d1":null}            -> adopted
   layout: []             -> accepted (an array is an object), kept in memory,
                             then silently dropped at the next save
5. POST /devices {x:0,y:0} -> the device is placed at 160,160

Why each is a defect and not a taste

  • The document is not the board. This is the same fault #1323 fixed for link endpoints, and layout was still
    handing out its own object: the API response, the UI, the example builder and every test fixture received the live
    layout, so adjusting what you were given adjusted the lab.
  • removeDevice() left the position. The server remembered delete topo.layout[devId]; the engine did not. A
    removal from anywhere else — a script, a test, a future route — left a position in the file for a box that is not
    on the board, and it accumulates with every board that ever had a device removed.
  • A position is drawn. web/canvas.js reads pos.x straight from the layout. {"x": "left"} gives NaN
    coordinates, so the box is not drawn anywhere and nothing says why. layout: [] is worse: accepted, kept in
    memory, and then lost at the next save, because JSON.stringify ignores an array's string keys — the
    positions vanish on the next save, long after the document that caused it.
  • A ghost position is a lie in the file and a target for the next device that happens to take that id.
  • 0 is a coordinate. { x: body.x || 160 } treats zero as missing, so a device placed at the origin — the
    top-left corner, the one place a box is most likely to be dragged to — is quietly moved to 160,160.
  • And /layout stored whatever it was handed, unvalidated: { "d1": { "x": "left" } } over HTTP.

What must be true instead

  • serialize() copies the layout; the document you are given is yours to change;
  • a position belongs to its device: removeDevice() takes it, and a saved board has no box that is not on the board;
  • on load a position must be two finite numbers, else it is dropped and reported (E_LAYOUT_BAD_ENTRY); a layout
    that is not a map is refused (E_LAYOUT_MALFORMED); a position naming a device the document does not contain is a
    warning (E_LAYOUT_UNKNOWN_DEVICE) — all in loadIssues, which every state payload already carries;
  • over the API a bad position is a 400 naming the rule, checked for every entry before any is stored, so a bad
    entry cannot half-apply a layout;
  • and 0 places a device at 0.
The third thing a board holds, after devices (#1321) and cables (#1323). `layout` — where each box sits — was the least guarded of the three. Measured against the current code: ``` 1. serialize().layout === topo.layout: true write to the document -> the board says d1.x = 999 add a box to the document -> the board has a box at {x:1,y:1} 2. after removeDevice('d2'): layout still holds d2, in memory AND in the next save 3. a document with a position for a device it does not contain -> kept as a ghost 4. {"x":"left","y":"top"} -> adopted; the canvas draws it at NaN {"d1":null} -> adopted layout: [] -> accepted (an array is an object), kept in memory, then silently dropped at the next save 5. POST /devices {x:0,y:0} -> the device is placed at 160,160 ``` ## Why each is a defect and not a taste * **The document is not the board.** This is the same fault #1323 fixed for link endpoints, and `layout` was still handing out its own object: the API response, the UI, the example builder and every test fixture received the live layout, so adjusting what you were given adjusted the lab. * **`removeDevice()` left the position.** The server remembered `delete topo.layout[devId]`; the engine did not. A removal from anywhere else — a script, a test, a future route — left a position in the file for a box that is not on the board, and it accumulates with every board that ever had a device removed. * **A position is drawn.** `web/canvas.js` reads `pos.x` straight from the layout. `{"x": "left"}` gives NaN coordinates, so the box is not drawn anywhere and nothing says why. `layout: []` is worse: accepted, kept in memory, and then **lost at the next save**, because `JSON.stringify` ignores an array's string keys — the positions vanish on the *next* save, long after the document that caused it. * **A ghost position is a lie in the file** and a target for the next device that happens to take that id. * **0 is a coordinate.** `{ x: body.x || 160 }` treats zero as missing, so a device placed at the origin — the top-left corner, the one place a box is most likely to be dragged to — is quietly moved to 160,160. * And `/layout` stored whatever it was handed, unvalidated: `{ "d1": { "x": "left" } }` over HTTP. ## What must be true instead * `serialize()` copies the layout; the document you are given is yours to change; * a position belongs to its device: `removeDevice()` takes it, and a saved board has no box that is not on the board; * on load a position must be two finite numbers, else it is dropped and reported (`E_LAYOUT_BAD_ENTRY`); a layout that is not a map is refused (`E_LAYOUT_MALFORMED`); a position naming a device the document does not contain is a warning (`E_LAYOUT_UNKNOWN_DEVICE`) — all in `loadIssues`, which every state payload already carries; * over the API a bad position is a `400` naming the rule, checked for every entry before any is stored, so a bad entry cannot half-apply a layout; * and `0` places a device at `0`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
jaylouisw/packetforge#1325
No description provided.