🐛 Refuse an update main that would nest a component inside itself

Nesting, duplicating and swapping refuse to put a component inside itself;
updating a main from a copy did not. A swap inside a variant's main reaches,
through sync, a copy of the swap's new component whose slot was switched to
that variant, so the copy holds its own component two levels down. Pushing
that copy into its main made each main hold the other, and the sync cascade
never settled.

Add update-main-nesting-loop?: the copy holds a copy of its component, or of
a component whose main holds one. An update main it matches changes
nothing, and the generator skips such targets as it skips looping swaps.

A fresh JVM library run (seed 20261101) found this: sync did not settle
after 200 passes. The regression fails before the fix (sync does not
settle) and passes after it: the update leaves the files unchanged and the
program runs. The full common JVM suite passes at seed 712: 2068 tests /
33874 assertions.

Compare lower-quantile milliseconds with the saved baseline; keep that
baseline unchanged. The machine/JVM warning makes ratios rough. No world
is flagged.

World                      Baseline       Now    Ratio
Main into 10 copies           1.790     0.898     0.50
Main into 100 copies         15.415     8.836     0.57
Main into 1000 copies       153.086    88.060     0.58
Three nesting levels         35.215    18.841     0.54
Dependency chain             68.931    37.993     0.55
Library update               83.083    43.555     0.52
Many plain shapes             4.837     2.206     0.46
Variant switch               32.052    20.015     0.62

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-10-31 08:18:11 +01:00
parent 81fc3008b3
commit a44321fc4d
2 changed files with 46 additions and 2 deletions

View File

@ -671,14 +671,36 @@
(pcb/concat-changes changes placement))
changes)))
(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."
[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)))))
(defn generate-sync-shape-inverse
"Generate changes to update the component a shape is linked to, from
the values in the shape and all its children."
the values in the shape and all its children. An update that would nest
the component inside itself changes nothing."
[changes libraries container shape-id]
(shape-log :debug shape-id container :msg "Sync shape inverse" :shape (str shape-id))
(let [shape-inst (ctn/get-shape container shape-id)
library (dm/get-in libraries [(:component-file shape-inst) :data])
component (ctkl/get-component library (:component-id shape-inst))
component (when-not (update-main-nesting-loop? libraries container shape-id)
(ctkl/get-component library (:component-id shape-inst)))
changes (cond-> changes
(and component (contains? (:touched shape-inst) :name-group))

View File

@ -1,6 +1,7 @@
(ns common-tests.logic.composable-gen-test
(:require
[app.common.files.changes-builder :as pcb]
[app.common.files.helpers :as cfh]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.logic.shapes :as cls]
@ -21,6 +22,7 @@
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.shapes :as ths]
[app.common.test-helpers.texts :as thtx]
[app.common.types.component :as ctk]
[app.common.uuid :as uuid]
[clojure.test :as t]
[clojure.walk :as walk]))
@ -477,6 +479,26 @@
(t/is (= 1 (get-in result [:coverage :random :swap :changed])))
(t/is (= 1 (get-in result [:coverage :random :library-sync :changed])))))
(t/deftest update-main-does-not-nest-a-component-inside-itself
;; A swap in a variant's main reaches a slot switched to that variant inside
;; a copy of the swap's new component, so the copy holds its own component.
(let [program {:world [2 2 {:variants true :rich-variants true :text true :text-styles true
:library true :layout :flex :layout-copies true}]
:ops [[:nest 9 0 0 0] [:switch 9 0 0 0] [:nest 2 0 0 0] [:delete 0 0 0 0]
[:swap 10 0 0 0] [:edit 3 0 1 1] [:update-main 3]]}
before (:situation (cg/settle-state (reduce cg/step (cg/initial-state (:world program))
(butlast (:ops program)))))
objects (:objects (thf/current-page (tm/file before)))
nests-itself? (fn [shape]
(some #(= (:component-id shape) (:component-id %))
(cfh/get-children objects (:id shape))))
copy (some #(when (and (ctk/instance-root? %) (not (ctk/main-instance? %)) (nests-itself? %)) %)
(vals objects))
updated (-> before (r/step (n/update-main (constantly (:id copy)))) r/settle)]
(t/is (some? copy))
(t/is (= (oracle/snapshot before) (oracle/snapshot updated)))
(t/is (map? (cg/run program)))))
(t/deftest pushing-a-local-override-does-not-accept-unrelated-source-edits
(let [spec [2 1 {:library true :stale-libraries true :library-variants true}]
initial (cg/initial-state spec)