From 32fb7267ccc10170638fa131726aed307f853d92 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 24 Nov 2026 09:13:01 +0000 Subject: [PATCH] :books: Consolidate shape geometry guidance Bring geometry invariants and numeric precision together while keeping component sync comparisons in the component memory. AI-assisted-by: gpt-6.1-terra --- .serena/memories/common/components.md | 1 + .serena/memories/common/core.md | 3 +- .../common/decimals-and-coordinates.md | 59 ------------------- .../memories/common/geometry-invariants.md | 48 --------------- .serena/memories/common/geometry.md | 7 +++ .serena/memories/common/testing.md | 2 +- 6 files changed, 10 insertions(+), 110 deletions(-) delete mode 100644 .serena/memories/common/decimals-and-coordinates.md delete mode 100644 .serena/memories/common/geometry-invariants.md create mode 100644 .serena/memories/common/geometry.md diff --git a/.serena/memories/common/components.md b/.serena/memories/common/components.md index 9dc1ac3305..56a329b29e 100644 --- a/.serena/memories/common/components.md +++ b/.serena/memories/common/components.md @@ -8,6 +8,7 @@ Start at `app.common.types.component`, `app.common.types.container`, `app.common - `:shape-ref` chains can cross files. `find-ref-shape` and `get-ref-chain-until-target-ref` follow them; the nearest component head and the outermost instance root are not interchangeable. - `:touched` stores override groups from `sync-attrs`. `set-shape-attr` marks copies through `set-touched-group`, subject to ignore flags; `:position-data` is derived and never touched. Geometry and content-path comparisons use approximate equality, but width and height are excluded from the `is-geometry?` branch. - Main shapes can acquire touched flags through cloning. `add-touched-from-ref-chain` can propagate upstream flags during variant switch, so a locally untouched shape can still act touched. `make-component-instance` cleans attrs including `:touched`, `:variant-id` and `:variant-name`; duplicating a component master does not have identical clean-copy semantics. Identify the creation path before diagnosing touched. +- Library sync compares exactly first, then tolerates drift in `:x`, `:y`, `:width`, `:height`, `:selrect`, `:points`, path/bool `:content` and numeric fields in text `:position-data`. Other fields remain exact. `close-attrs?` truncates zipped sequences, so `attr-equal?` checks point counts and position-data uses a separate length-aware comparison. See `mem:common/geometry`. - Swap slots are touched groups `swap-slot-` on swapped copy sub-heads. Sync matches those by slot; other children match by `shape-ref`, not index. Child order converges through async sync. `:mov-objects` and `:reorder-children` guard copy structure unless a sync or swap sets `allow-altering-copies`; new structural changes need the same guard. - Deleting inside a component main propagates to referencing copies transitively across pages to prevent dangling refs. `generate-delete-shapes` skips propagation for the main root itself and for `allow-altering-copies` flows. Adding a shape to a copy parent clears its `:remote-synced` unless `ignore-touched`. diff --git a/.serena/memories/common/core.md b/.serena/memories/common/core.md index 4bfb9f46f5..610a6f3d1a 100644 --- a/.serena/memories/common/core.md +++ b/.serena/memories/common/core.md @@ -29,8 +29,7 @@ Model, schema, and persistence shape: - For organization and team permission rules shared across runtimes, start at `app.common.types.organization`. Geometry and layout: -- Shape geometry invariants, redundant geometry fields, and geometry-sensitive tests: `mem:common/geometry-invariants`. -- Coordinate drift and approximate float comparisons: `mem:common/decimals-and-coordinates`. +- Shape geometry invariants, coordinate drift and approximate comparisons: `mem:common/geometry`. - Layout/grid assignment, deassignment, metadata cleanup, and auto-positioning: `mem:common/layout-grid-subtleties`. Change pipeline, validation, and migrations: diff --git a/.serena/memories/common/decimals-and-coordinates.md b/.serena/memories/common/decimals-and-coordinates.md deleted file mode 100644 index 1cc966f452..0000000000 --- a/.serena/memories/common/decimals-and-coordinates.md +++ /dev/null @@ -1,59 +0,0 @@ -# Decimals and Coordinates in Penpot - -Penpot stores all geometry as JS numbers (doubles in CLJS, doubles in -CLJ for the JVM-side common code). Several Penpot-specific facts -about how this plays out are not obvious from reading the code. - -## Sub-pixel drift is routine - -Coordinate values that "should" be integers are routinely off by ~1e-5 -in production data. A `:width` of 107 will frequently appear as -`107.00001275539398` after the value has passed through: - -- the modifier propagation pipeline (`apply-wasm-modifiers` and the - Rust WASM transform engine) -- any rotation/scale composition -- repeated translations - -This drift is invisible in the UI (the renderer rounds at draw time) -but defeats exact equality comparisons in business logic. It does NOT -appear in JVM-only test setups because the WASM pipeline isn't -involved — tests that build shapes via `setup-shape` and `add-sample-shape` -get clean integer values. Bugs that depend on drift will pass tests -but fire in production unless tests explicitly inject drift. - -## Use the close? helpers, not `=` - -For comparing coordinate-like floats, the established convention is: - -- `app.common.math/close?` — scalar tolerance comparison. - Default precision 0.001 (sub-pixel; tight enough to keep distinct - shapes distinct, loose enough to absorb arithmetic noise). - Two-arity uses default precision; three-arity takes a custom one. -- `app.common.geom.point/close?` — element-wise close on `gpt/Point` - records. Compares :x and :y via `mth/close?`. -- `app.common.geom.matrix/close?` — close on transform matrices. -- `app.common.geom.shapes/close-attrs?` — used inside `set-shape-attr` - to decide whether a re-assigned `:width`/`:height` should be treated - as a no-op (suppresses spurious touched marking from drift). - -Treat `=` on `:x`, `:y`, `:width`, `:height`, `:selrect`, or `:points` -fields as a code smell when the inputs may have flowed through any -transform. The `set-shape-attr`-style precedent (already using -`close-attrs?`) is the right model. - -## The redundancy multiplies failure modes - -A shape's position lives in `:x/:y`, `:selrect`, AND `:points` (see -`mem:common/geometry-invariants` memory). Each is a separate set of float -values. After any operation that touches geometry, all three should -agree, but each is computed by a different path and accumulates -its own drift. Comparing `:selrect.width` from shape A to -`:selrect.width` from shape B is comparing two values that -"semantically" should be equal but were computed via different -operation chains — exact equality will often be false. - -## Library sync comparison - -- `update-attrs` (`app.common.logic.libraries`) compares main vs copy attrs exactly first, then with tolerance for: `drifting-geometry-attrs` (`:x :y :width :height :selrect :points`, via `gsh/close-attrs?`); path and bool `:content` (`segment/content-close?`; bool shapes keep their path in `:content`); and text `:position-data` (`position-data-close?` over `mth/close-map?`: every numeric field of every entry within `mth/close?`, other fields such as fills, fonts and text exact). All other attrs (`:opacity`, radii, `:transform`, `:rotation`, text `:content`) stay exact: tolerance there hides real changes. -- `close-attrs?` pairs lists with `d/zip`, which truncates to the shorter list and treats nil vs vector as equal for `:points`/`:position-data`. Do not use it for variable-length data. That is why `:position-data` has its own helper, and why `attr-equal?` checks the `:points` count before calling it. diff --git a/.serena/memories/common/geometry-invariants.md b/.serena/memories/common/geometry-invariants.md deleted file mode 100644 index f90475a285..0000000000 --- a/.serena/memories/common/geometry-invariants.md +++ /dev/null @@ -1,48 +0,0 @@ -# Geometry Invariants in Penpot Shapes - -Core invariant: shape position is stored redundantly, and all geometry fields must stay coherent. - -## Redundant fields - -For a shape at `(x, y)` with width `w` and height `h`: - -- `:x`, `:y`, `:width`, `:height`: top-left and dimensions. -- `:selrect`: `{:x :y :width :height :x1 :y1 :x2 :y2}`, where `x2 = x + w` and `y2 = y + h`. -- `:points`: four corners for an axis-aligned rect, clockwise from top-left. -- `:transform` and `:transform-inverse`: identity for axis-aligned shapes; populated for transformed shapes. - -After a geometric mutation, equivalent fields such as `:y`, `(:y :selrect)`, and the first point's `:y` should agree. The renderer and hit-testing read `:selrect` / `:points`, so a shape can render or select incorrectly even when `:x` / `:y` look right. - -## Helpers that preserve the invariant - -- `gsh/move`: translates by delta and updates geometry consistently. -- `gsh/absolute-move`: moves to an absolute position by computing a delta from the current selrect. -- `gsh/transform-shape`: applies a full transform. -- `cts/setup-shape`: initializes geometry for new shapes; variant test helpers such as `thv/add-variant-with-child` use it. - -## Edits that break the invariant - -- `(assoc shape :x ...)` or `(assoc shape :y ...)`: updates only one field and leaves `:selrect` / `:points` stale. -- `ths/update-shape file label :y val`: goes through `set-shape-attr`, but does not repair all position fields for `:y` alone. -- Direct `update-in` edits to `:selrect`, `:points`, or dimensions. - -## Test setup warning - -When positioning test shapes, use `gsh/absolute-move`, `gsh/move`, or production change helpers. Do not set only `:x` / `:y`. - -```clojure -(cls/generate-update-shapes - (pcb/empty-changes nil page-id) - #{(:id child)} - #(gsh/absolute-move % (gpt/point (:x %) 101)) - (:objects page) - {}) -``` - -Using `(ths/update-shape file label :y 101)` leaves `:selrect.y` stale. Downstream code that reads `:selrect` can then fail in ways that look like product bugs but are only invalid test setup. - -## :touched and geometry mutation - -When a copy shape changes geometry through the proper pipeline (`set-shape-attr` via `process-operation :set`), `:touched` gains `:geometry-group` unless ignored. Tests can either drive the production update with `cls/generate-update-shapes`, or inject `(assoc shape :touched #{:geometry-group})` when only touched state matters. - -If a test needs both a new position and touched state, move the shape first with geometry-preserving helpers, then inject or assert touched state. \ No newline at end of file diff --git a/.serena/memories/common/geometry.md b/.serena/memories/common/geometry.md new file mode 100644 index 0000000000..4f483b2943 --- /dev/null +++ b/.serena/memories/common/geometry.md @@ -0,0 +1,7 @@ +# Shape geometry and numeric precision + +- Shape geometry is redundant: `:x`, `:y`, `:width` and `:height` coexist with `:selrect`, corner `:points` and transform matrices. Keep them coherent after a mutation; rendering and hit testing can read a different representation from the one a caller changed. +- Use `gsh/move`, `gsh/absolute-move` or `gsh/transform-shape` for geometric edits and `cts/setup-shape` for initialization. An isolated `assoc` or test helper update of `:x` or `:y` can leave `:selrect` and `:points` stale. Prefer production-path change helpers in tests; see `mem:common/testing`. +- Sub-pixel drift can arise from WASM modifiers and composed transforms, even when JVM fixture setup produces clean values. Include drift in regression tests when the bug depends on it; do not assume exact equality after transformation. +- Use the appropriate tolerance helper: scalar `app.common.math/close?` defaults to `0.001`; point and matrix namespaces have their own `close?`. `gsh/close-attrs?` compares supported geometry attrs with tolerance, but its zipped `:points` and `:position-data` comparisons truncate to the shorter sequence and can treat nil as equal to a vector. Check lengths and use a suitable comparator for variable-length data. +- For copy geometry and touched state, move with geometry-preserving helpers first, then assert or set touched as appropriate. Component sync applies tolerance selectively, not to every attribute; see `mem:common/components`. diff --git a/.serena/memories/common/testing.md b/.serena/memories/common/testing.md index a5e6169cf3..052e0e7663 100644 --- a/.serena/memories/common/testing.md +++ b/.serena/memories/common/testing.md @@ -5,6 +5,6 @@ - JS tests compile before running. Register new namespaces in `common_tests/runner.cljc`; new vars in registered namespaces need no runner change. Direct `node target/tests/test.js` runs the existing build and does not rebuild after edits. - JVM coverage uses `clojure -M:dev:test:coverage`; it instruments all configured namespaces, even for a focused run, and is slower. Its lcov, HTML and EMMA form counts go under `target/coverage/`; see `tests.edn` for exclusions. - Helpers live under `common/src/app/common/test_helpers/`. Tests using label-to-UUID helpers need `(t/use-fixtures :each thi/test-fixture)` to reset labels between tests. Inspect the builders there rather than recreating common file, component or variant setup. -- Prefer production-path mutations such as `cls/generate-update-shapes` with `thf/apply-changes`; the latter validates by default. Disable validation only for intentionally malformed intermediate state. For swaps that keep touched state use `tho/swap-component-in-shape` with `{:keep-touched? true}`. `add-variant-with-copy` does not take child position params; move them after creation. For geometry-sensitive setup see `mem:common/geometry-invariants`. +- Prefer production-path mutations such as `cls/generate-update-shapes` with `thf/apply-changes`; the latter validates by default. Disable validation only for intentionally malformed intermediate state. For swaps that keep touched state use `tho/swap-component-in-shape` with `{:keep-touched? true}`. `add-variant-with-copy` does not take child position params; move them after creation. For geometry-sensitive setup see `mem:common/geometry`. - `sg/sample` is unseeded by default. If a test filters for a subtype, fix the seed and verify the subtype exists; otherwise `first` can yield nil and later produce a typeless shape. - For component and referential-integrity debugging see `mem:common/components`.