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

Swapping or switching a head for a component checked only the copies in
that component's main against the head's parents. A copy there could
have swapped away a component that its own main still holds, so the
swap passed, and a sync or reset that brought the held component back
nested a component inside itself.

The check now follows each component the new main holds into its main,
through every level, as the nest check does.

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

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  19.108 |  0.54 |
| edit-through-a-chain-of-dependents |   68.931 |  39.394 |  0.57 |
| library-update-into-copies         |   83.083 |  45.356 |  0.55 |
| main-edit-among-many-shapes        |    4.837 |   2.251 |  0.47 |
| main-edit-into-10-copies           |     1.79 |   0.974 |  0.54 |
| main-edit-into-100-copies          |   15.415 |   9.266 |  0.60 |
| main-edit-into-1000-copies         |  153.086 |  94.168 |  0.62 |
| variant-switch-into-copies         |   32.052 |  20.366 |  0.64 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-02 09:35:05 +01:00
parent a7ae3140c5
commit 6ebac310ba
2 changed files with 27 additions and 5 deletions

View File

@ -666,14 +666,16 @@
(defn swap-nesting-loop?
"Whether swapping, or switching, the copy head `shape` of `objects` for a
copy of the component `component-id` of library data `ldata` would nest a
component inside itself."
component inside itself, directly or through the mains of the components
that component holds in `ldata`."
[objects shape ldata component-id]
(let [component (ctkl/get-component ldata component-id true)
page (ctf/get-component-page ldata component)
root (ctf/get-component-root ldata component)]
root (ctf/get-component-root ldata component)
parents (into #{} (keep :component-id) (cfh/get-parents-with-self objects (:parent-id shape)))]
(boolean
(and page
root
(cfh/components-nesting-loop?
(cfh/get-children-with-self (:objects page) (:id root))
(cfh/get-parents-with-self objects (:parent-id shape)))))))
(some parents
(ctn/held-component-ids (cfh/get-children-with-self (:objects page) (:id root))
{(:id ldata) {:data ldata}}))))))

View File

@ -95,6 +95,26 @@
(t/is (true? (loop? :nested)))
(t/is (false? (loop? :copy01)))))
(t/deftest swap-through-a-held-main-is-a-nesting-loop
;; :cy holds a copy of :cz whose own copy of :cx was swapped away; the
;; main of :cz still holds :cx, which a reset brings back
(let [file (-> (thf/sample-file :file1)
(tho/add-simple-component :cw :mw :mw-child)
(tho/add-simple-component :cv :mv :mv-child)
(tho/add-frame :mx)
(thc/instantiate-component :cw :x-head :parent-label :mx)
(thc/make-component :cx :mx)
(tho/add-frame :mz)
(thc/instantiate-component :cx :z-x :parent-label :mz)
(thc/make-component :cz :mz)
(tho/add-frame :my)
(thc/instantiate-component :cz :y-z :parent-label :my :children-labels [:y-z-x])
(thc/make-component :cy :my)
(thc/component-swap :y-z-x :cv :y-z-v))
objects (:objects (thf/current-page file))]
(t/is (true? (clv/swap-nesting-loop? objects (ths/get-shape file :x-head) (:data file) (thi/id :cy))))
(t/is (false? (clv/swap-nesting-loop? objects (ths/get-shape file :x-head) (:data file) (thi/id :cv))))))
(t/deftest swap-in-place-keeps-index-and-layout-attrs
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)