From d65f8ded0fc1a479d21c83bdaa35d9cebbb28879 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 d5567e9c07..ce1460d547 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -511,7 +511,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] @@ -1118,7 +1119,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))))))))