🐛 Refuse a nest that loops through the main of a held component

A copy whose nested head was switched to another variant was nested into
the main of the component that head's main still holds. The loop check
only compared the components in the moved subtree with the parent
chain, so it let the nest through; a reset then brought the held
component back, and the sync cascade grew the cycle until it gave up.

The check now follows each component the moved shapes hold into its
main, through every level, as sync and reset can bring any of them
back. Moving, pasting and dropping in the layers share it.

Benchmark (scripts/bench-components, lower quantile in ms):

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  19.778 |  0.56 |
| edit-through-a-chain-of-dependents |   68.931 |  39.309 |  0.57 |
| library-update-into-copies         |   83.083 |  49.107 |  0.59 |
| main-edit-among-many-shapes        |    4.837 |   2.271 |  0.47 |
| main-edit-into-10-copies           |     1.79 |   1.001 |  0.56 |
| main-edit-into-100-copies          |   15.415 |   10.29 |  0.67 |
| main-edit-into-1000-copies         |  153.086 | 101.796 |  0.66 |
| variant-switch-into-copies         |   32.052 |   20.72 |  0.65 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-02 03:23:15 +01:00
parent fcd9128575
commit 6e3c8b1cb2
3 changed files with 57 additions and 4 deletions

View File

@ -499,6 +499,45 @@
[shape libraries]
(get-in libraries [(:component-file shape) :data :components (:component-id shape)]))
(defn- main-shapes
"The shapes of the main of `component` in the file data `data`: its main
instance on a page, or the shapes a deleted component keeps."
[data component]
(if (:deleted component)
(vals (:objects component))
(cfh/get-children-with-self
(get-in data [:pages-index (:main-instance-page component) :objects])
(:main-instance-id component))))
(defn held-component-ids
"Ids of the components of the copies and mains among `shapes`, and of the
components the mains of these hold, through every level. A sync or a
reset brings back what a main holds, even where a copy swapped it."
[shapes libraries]
(loop [pending (filterv :component-id shapes)
ids #{}]
(if-let [shape (peek pending)]
(let [id (:component-id shape)]
(if (contains? ids id)
(recur (pop pending) ids)
(let [data (get-in libraries [(:component-file shape) :data])
component (get-in data [:components id])]
(recur (cond-> (pop pending)
(some? component)
(into (filter :component-id) (main-shapes data component)))
(conj ids id)))))
ids)))
(defn- nesting-loop?
"Whether nesting `children` below `parent` would hold a component inside
its own main, directly or through the mains of the components it holds."
[objects parent children libraries]
(let [parent-ids (into #{} (keep :component-id) (cfh/get-parents-with-self objects (:id parent)))]
(and (seq parent-ids)
(boolean (some parent-ids
(held-component-ids (mapcat #(cfh/get-children-with-self objects (:id %)) children)
libraries))))))
(defn invalid-structure-for-component?
"Check if the structure generated nesting children in parent is invalid in
terms of nested components: a change of a copy's structure, a main inside
@ -522,9 +561,7 @@
selected-main-instance? (seq selected-components)
parent-in-component? (in-any-component? objects parent)
comps-nesting-loop? (not (->> children
(map #(cfh/components-nesting-loop? objects (:id %) (:id parent)))
(every? nil?)))]
comps-nesting-loop? (nesting-loop? objects parent children libraries)]
(or
;;We don't want to change the structure of component copies
(ctk/in-component-copy? parent)
@ -532,7 +569,8 @@
;; If we are moving something containing a main instance the container can't be part of a component (neither main nor copy)
(and selected-main-instance? parent-in-component?)
;; Avoid a component nesting loop: a main inside itself, or a copy
;; inside its own main. A plain shape moved into its own subtree passes;
;; inside its own main or the main of a component it holds. A plain
;; shape moved into its own subtree passes;
;; the move itself (`relocate-shapes`) refuses it.
comps-nesting-loop?)))

View File

@ -965,3 +965,11 @@
(t/is (map? (cg/run {:world [2 1 {:text true :text-styles true :layout :flex :layout-children true}]
:ops [[:remove-child 0 0 0 0] [:nest 0 0 0 0] [:style 2 27 1]
[:edit 0 1 1 :copy-head] [:reset 0] [:nest 0 0 0 0]]}))))
(t/deftest generated-nest-refuses-a-loop-through-a-switched-copy
;; a copy whose nested head was switched away from Board is nested into
;; Board's main; the main of that head still holds a Board, which a reset
;; brings back, so the nest would loop
(t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :library true :rich-variants true
:layout-copies true :text true}]
:ops [[:switch 3 0 0 0] [:nest 9 0 0 0] [:edit 7 1 1 :copy-head] [:reset 7]]}))))

View File

@ -410,6 +410,13 @@
(t/testing "a copy does not go inside its own main"
(t/is (= [:root :root] (place file :main1 [:copy1] :pasting? true)))
(t/is (= [:main2 :main2] (place file :main2 [:copy1] :pasting? true))))
(t/testing "a copy does not go inside the main of a component its main holds, though it swapped it"
(let [file (-> file
(tho/add-nested-component :n1 :nmain1 :nmain1-child :n2 :nmain2 :nested-head)
(thc/instantiate-component :n2 :ncopy2 :children-labels [:ncopy2-head])
(thc/component-swap :ncopy2-head :c2 :ncopy2-swapped))]
(t/is (= [:root :root] (place file :nmain1 [:ncopy2])))
(t/is (= [:board :board] (place file :board [:ncopy2])))))
(t/testing "a main does not go inside itself, even pasted"
(t/is (= [:root :root] (place file :main1-child [:main1])))
(t/is (= [:root :root] (place file :main1-child [:main1] :pasting? true))))