🐛 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
This commit is contained in:
alonso.torres 2026-10-23 20:20:38 +02:00
parent 8014df768d
commit d65f8ded0f
2 changed files with 17 additions and 2 deletions

View File

@ -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

View File

@ -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))))))))