🐛 Refuse an update of a main that loops through several held mains

Updating a main from its copy checked the copies the copy holds and the
mains of those, but no deeper. A chain of mains, each holding the next
through a copy that swapped its head away, could still end in the
component being updated, which then nested inside itself.

The check now follows the held components through every level.

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

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |   18.93 |  0.54 |
| edit-through-a-chain-of-dependents |   68.931 |  38.249 |  0.55 |
| library-update-into-copies         |   83.083 |  44.715 |  0.54 |
| main-edit-among-many-shapes        |    4.837 |   2.166 |  0.45 |
| main-edit-into-10-copies           |     1.79 |   0.949 |  0.53 |
| main-edit-into-100-copies          |   15.415 |   9.513 |  0.62 |
| main-edit-into-1000-copies         |  153.086 | 102.177 |  0.67 |
| variant-switch-into-copies         |   32.052 |  20.994 |  0.65 |

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

View File

@ -736,22 +736,12 @@
(defn update-main-nesting-loop?
"Whether updating the main of the copy head `shape-id` of `container` from
the copy would nest its component inside itself: the copy holds a copy of
its component, or of a component whose main holds one."
its component, or of a component whose main holds one, through every
level."
[libraries container shape-id]
(let [component-id (:component-id (ctn/get-shape container shape-id))
holds? (fn [{:keys [component-file] :as head}]
(let [library (dm/get-in libraries [component-file :data])
component (ctkl/get-component library (:component-id head) true)
root (when component (ctf/get-component-root library component))]
(and root
(some #(= component-id (:component-id %))
(cfh/get-children (:objects (ctf/get-component-container library component))
(:id root))))))]
(boolean
(some (fn [shape]
(and (:component-id shape)
(or (= component-id (:component-id shape)) (holds? shape))))
(cfh/get-children (:objects container) shape-id)))))
(let [component-id (:component-id (ctn/get-shape container shape-id))]
(contains? (ctn/held-component-ids (cfh/get-children (:objects container) shape-id) libraries)
component-id)))
(defn generate-sync-shape-inverse
"Generate changes to update the component a shape is linked to, from

View File

@ -8,6 +8,7 @@
(:require
[app.common.data :as d]
[app.common.files.changes-builder :as pcb]
[app.common.logic.libraries :as cll]
[app.common.logic.variants :as clv]
[app.common.test-helpers.components :as thc]
[app.common.test-helpers.compositions :as tho]
@ -115,6 +116,33 @@
(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 update-main-through-a-held-main-is-a-nesting-loop
;; a copy of :ck holds :ca; :ca's copy of :cb and :cb's copy of :cc each
;; swapped their head away, but the main of :cc still holds :ck, so
;; updating :ck from the copy would loop
(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 :mk)
(thc/instantiate-component :cw :k-head :parent-label :mk)
(thc/make-component :ck :mk)
(tho/add-frame :mc)
(thc/instantiate-component :ck :c-k :parent-label :mc)
(thc/make-component :cc :mc)
(tho/add-frame :mb)
(thc/instantiate-component :cc :b-c :parent-label :mb :children-labels [:b-c-k])
(thc/make-component :cb :mb)
(thc/component-swap :b-c-k :cv :b-c-v)
(tho/add-frame :ma)
(thc/instantiate-component :cb :a-b :parent-label :ma :children-labels [:a-b-c])
(thc/make-component :ca :ma)
(thc/component-swap :a-b-c :cv :a-b-v)
(thc/instantiate-component :ck :copyk :children-labels [:copyk-head])
(thc/component-swap :copyk-head :ca :copyk-a))
objects (:objects (thf/current-page file))]
(t/is (true? (cll/update-main-nesting-loop? (libraries file) {:objects objects} (thi/id :copyk))))
(t/is (false? (cll/update-main-nesting-loop? (libraries file) {:objects objects} (thi/id :a-b))))))
(t/deftest swap-in-place-keeps-index-and-layout-attrs
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)