From a64deafb2576daa3614df5f40d0b0370eff67d33 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Fri, 23 Oct 2026 20:20:38 +0200 Subject: [PATCH] :bug: Remove invalid swaps when a main detaches a nested head A swapped copy must follow structural changes when its position ceases to be a component head. Walk that shape normally during sync, remove its component metadata and swap group, and replace its former component children with the detached main shape's children. The regression fails three assertions before the fix. The swap suite passes 44 tests and 177 assertions, including exact undo. The shrunk generated detach scenario also passes with its structural model update. Benchmark vs Phase 1e: main 10/100/1000 0.50/0.57/0.64; levels 0.52; chain 0.52; library 0.60; many shapes 0.55; switch 0.65. No flags. AI-assisted-by: GPT-6 --- common/src/app/common/logic/libraries.cljc | 7 +++++-- common/test/common_tests/logic/swap_group_test.cljc | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index be0c524ac2..67626edceb 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -550,7 +550,8 @@ children-main {:pair? ctk/is-main-of? :on-pair (fn [changes child-inst child-main] - (if (and (not reset?) (ctk/swapped? child-inst)) + (if (and (not reset?) (ctk/swapped? child-inst) + (ctk/instance-head? child-main)) (update-swapped-placement changes ctx child-inst child-main) (sync-copy-recursive changes ctx child-inst child-main))) :only-inst (fn [changes child-inst] @@ -1201,7 +1202,9 @@ (if (and (= (:shape-ref dest-shape) (:id origin-shape)) (ctk/subcopy-head? dest-shape) (not (ctk/instance-head? origin-shape))) - (pcb/update-shapes changes [(:id dest-shape)] ctk/unhead-shape {:ignore-touched true}) + (pcb/update-shapes changes [(:id dest-shape)] + #(-> % ctk/remove-swap-group ctk/unhead-shape) + {:ignore-touched true}) changes)) (defn- check-swapped-main diff --git a/common/test/common_tests/logic/swap_group_test.cljc b/common/test/common_tests/logic/swap_group_test.cljc index 0072848e98..5414bddf4e 100644 --- a/common/test/common_tests/logic/swap_group_test.cljc +++ b/common/test/common_tests/logic/swap_group_test.cljc @@ -785,3 +785,15 @@ (t/is (mth/close? 80 (:height head) 0.001)) (t/is (= (thn/normalize-file (tm/file edited)) (thn/normalize-file (tm/file (r/step updated (n/undo)))))))) + +(t/deftest detaching-a-main-child-removes-the-copys-now-invalid-swap + (let [initial (tm/make-situation (setup)) + detached (-> initial (r/step (n/detach :card-star)) r/settle) + head (tm/resolve-shape detached :copy-star) + main (tm/resolve-shape detached :card-star) + objects (:objects (thf/current-page (tm/file detached)))] + (t/is (not (ctk/instance-head? head))) + (t/is (not (ctk/swapped? head))) + (t/is (= (:shapes main) (mapv #(get-in objects [% :shape-ref]) (:shapes head)))) + (t/is (= (thn/normalize-file (tm/file initial)) + (thn/normalize-file (tm/file (r/step detached (n/undo))))))))