🐛 Fit the cells a copy takes from its main within its own tracks

A copy that authors its track lists keeps them when its main adds or
removes tracks, but sync gave it its main's cells as they were: after
the main added a column, a copy with two columns of its own held a cell
in a third. A copy whose track counts differ from its main's now fits
the main's cells within its own tracks as the contract keeps cells when
tracks shrink (move, shorten or join), and the oracle expects the same.

Test: a-copy-with-its-own-columns-takes-its-main-cells-within-them (the
grid gate program of seed 175377838).
Common suite: as the previous commit.

Benchmark (scripts/bench-components, ms, lower 2.5% quantile), measured
with the previous commit's change in the tree as well:
|                             :world | :baseline |    :now |   :mean | :ratio |
|------------------------------------+-----------+---------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |   22.04 |   22.52 |   0.63 |
| edit-through-a-chain-of-dependents |    68.931 |  43.463 |   44.28 |   0.63 |
|         library-update-into-copies |    83.083 |  48.024 |  50.138 |   0.58 |
|        main-edit-among-many-shapes |     4.837 |   2.247 |   2.418 |   0.46 |
|           main-edit-into-10-copies |      1.79 |    1.06 |   1.078 |   0.59 |
|          main-edit-into-100-copies |    15.415 |  10.242 |  11.303 |   0.66 |
|         main-edit-into-1000-copies |   153.086 | 110.068 | 112.273 |   0.72 |
|         variant-switch-into-copies |    32.052 |  22.921 |  23.391 |   0.72 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-12 05:33:31 +01:00
parent 9e2a50da3c
commit d5cdebc478
4 changed files with 20 additions and 3 deletions

View File

@ -2242,6 +2242,10 @@
(:shapes shape-copy))]
(-> shape-copy
(assoc :layout-grid-cells (-> shape-main (ctsl/remap-grid-cells ids-map) :layout-grid-cells))
;; a copy with its own track lists keeps its main's cells within them
(cond-> (or (not= (count (:layout-grid-rows shape-copy)) (count (:layout-grid-rows shape-main)))
(not= (count (:layout-grid-columns shape-copy)) (count (:layout-grid-columns shape-main))))
(ctsl/fit-cells-to-tracks))
(ctsl/assign-cells objects))))
(defn grid-direction-cells

View File

@ -163,10 +163,10 @@
(def ^:private default-cell
{:row-span 1 :column-span 1 :position :auto :align-self :auto :justify-self :auto :shapes []})
(defn- fit-cells
(defn fit-cells
"`cells` placed within a grid of `rows` by `columns` that flows in
`direction`, as the contract keeps a copy's cells when its inherited
tracks shrink: cells with children first, then authored empty ones, then
tracks shrink, or the cells it takes from a main with other tracks: cells with children first, then authored empty ones, then
default ones, each where it fits, else in the first free slot, else in
one track; children left without a slot join the cell where theirs
would start. Free slots take default cells."

View File

@ -661,7 +661,14 @@
(and manual (= :auto (:position cell)))
(assoc :position :manual)))]))
[false []] cells)))
cells (->> (grid-cells main)
rows (count (:layout-grid-rows shape))
columns (count (:layout-grid-columns shape))
;; a copy with its own track lists keeps its main's cells within them
fitted (if (and (= rows (count (:layout-grid-rows main)))
(= columns (count (:layout-grid-columns main))))
(grid-cells main)
(grid/fit-cells (grid-cells main) rows columns (or (:layout-grid-dir shape) :row)))
cells (->> fitted
(sort-by flow-order)
(mapv #(update % :shapes (fn [ids] (filterv participating ids))))
end-auto-prefix)

View File

@ -2381,6 +2381,12 @@
:layout-copies true :text true}]
:ops [[:nest 6 0 0 0] [:nest 9 0 0 0] [:style 2 29 1] [:detach 5 0 0 0]]}))))
(t/deftest a-copy-with-its-own-columns-takes-its-main-cells-within-them
(t/is (map? (cg/run {:world [1 0 {:variants false :layout :grid :layout-item-margins false :grid-square false
:rich-variants false :layout-children false :grid-empty false
:layout-copies false :text false}]
:ops [[:grid-track 10 5 0 11] [:grid-track 0 1] [:grid-cell 0 0 0 1]]}))))
(t/deftest random-shape-structure-compositions
(let [coverage (atom {})
failure (atom nil)]