From 9cf5af4aaf46443819f207ebf43a51d97227c46d Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Sun, 8 Nov 2026 01:22:28 +0100 Subject: [PATCH] :bug: Keep a component deleted, not gone, when its creation is undone Undoing the creation of a component removed it from the file. A copy another file had made of it in the meantime, as a consumer of a library can after a variant is added there, then pointed at a component that did not exist: the file failed validation, and the copy could not find its main shapes or be restored. Undoing the creation now marks the component deleted and keeps its main's shapes in it, as deleting it does; redoing it adds it again. The undo and redo checks of the component tests compare files without their deleted components, since an undone creation leaves one where there was none. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | world | baseline | now | ratio | |------------------------------------+----------+---------+-------| | deepest-edit-through-three-levels | 35.215 | 19.745 | 0.56 | | edit-through-a-chain-of-dependents | 68.931 | 37.914 | 0.55 | | library-update-into-copies | 83.083 | 48.026 | 0.58 | | main-edit-among-many-shapes | 4.837 | 1.944 | 0.40 | | main-edit-into-10-copies | 1.790 | 1.047 | 0.58 | | main-edit-into-100-copies | 15.415 | 10.179 | 0.66 | | main-edit-into-1000-copies | 153.086 | 91.854 | 0.60 | | variant-switch-into-copies | 32.052 | 21.546 | 0.67 | AI-assisted-by: claude-opus-5-5 --- .../src/app/common/files/changes_builder.cljc | 5 ++-- .../composable/comp/undo_check.cljc | 2 +- .../common/test_helpers/composable/gen.cljc | 30 +++++++++---------- .../test_helpers/composable/oracle.cljc | 13 ++++++-- .../app/common/test_helpers/normalize.cljc | 11 +++++-- .../common_tests/logic/variants_test.cljc | 28 +++++++++++++++++ 6 files changed, 66 insertions(+), 23 deletions(-) diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index 15d5f0baa2..1f92ad7646 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -943,9 +943,10 @@ (update :undo-changes (fn [undo-changes] (-> undo-changes + ;; marked deleted, keeping its main's shapes, so a + ;; copy another file made of it still finds it (conj {:type :del-component - :id id - :skip-undelete? true}) + :id id}) (into (comp (map :id) (map lookupf) (map mk-change)) diff --git a/common/src/app/common/test_helpers/composable/comp/undo_check.cljc b/common/src/app/common/test_helpers/composable/comp/undo_check.cljc index 510c4f2075..721b0a82ae 100644 --- a/common/src/app/common/test_helpers/composable/comp/undo_check.cljc +++ b/common/src/app/common/test_helpers/composable/comp/undo_check.cljc @@ -57,7 +57,7 @@ (defn snapshot "The normalized value of `file` that the round trip compares." ([file] (snapshot file {})) - ([file opts] (thn/normalize-file file opts))) + ([file opts] (thn/normalize-file file (merge {:deleted-components? false} opts)))) (defn snapshot-files "The normalized files affected by a step, including linked libraries." diff --git a/common/src/app/common/test_helpers/composable/gen.cljc b/common/src/app/common/test_helpers/composable/gen.cljc index 5155492259..a3e5459aac 100644 --- a/common/src/app/common/test_helpers/composable/gen.cljc +++ b/common/src/app/common/test_helpers/composable/gen.cljc @@ -724,9 +724,9 @@ [before after journal] (let [undone (r/step (restore-references after (:reference-before journal)) (n/undo)) redone (r/step (restore-references undone (:reference-after journal)) (n/redo))] - (when-not (= before (oracle/snapshot undone)) - (oracle/fail :I7 {:phase :undo :diff (thn/diff before (oracle/snapshot undone))})) - (when-not (= (oracle/snapshot after) (oracle/snapshot redone)) + (when-not (= before (oracle/history-snapshot undone)) + (oracle/fail :I7 {:phase :undo :diff (thn/diff before (oracle/history-snapshot undone))})) + (when-not (= (oracle/history-snapshot after) (oracle/history-snapshot redone)) (oracle/fail :I7 {:phase :redo})))) (defn- synchronizable-components @@ -804,7 +804,7 @@ [situation owner op kind] (let [before (source-context situation owner) history (get-in situation [:library-histories owner] - {:undo [] :redo [] :baseline (get (oracle/snapshot before) owner)}) + {:undo [] :redo [] :baseline (get (oracle/history-snapshot before) owner)}) undo? (= kind :library-undo) redo? (= kind :library-redo) journal (cond undo? (peek (:undo history)) redo? (peek (:redo history))) @@ -822,8 +822,8 @@ :else history) files (cf/all-files after) current-id (:id (tm/file situation))] - (when entry (assert-round-trip (oracle/snapshot before) after entry)) - (when-not (= (dissoc (oracle/snapshot before) owner) (dissoc (oracle/snapshot after) owner)) + (when entry (assert-round-trip (oracle/history-snapshot before) after entry)) + (when-not (= (dissoc (oracle/history-snapshot before) owner) (dissoc (oracle/history-snapshot after) owner)) (oracle/fail :I7 {:phase :source-editor-wrote-another-file})) (-> situation (tm/with-file (get files current-id)) @@ -974,8 +974,8 @@ after (if library-id after (reduce (fn [after owner] (cond-> after - (not= (get (oracle/snapshot situation) owner) - (get (oracle/snapshot after) owner)) + (not= (get (oracle/history-snapshot situation) owner) + (get (oracle/history-snapshot after) owner)) (assoc-in [:library-histories owner :foreign-write] true))) after (keys (:library-histories situation)))) _ (when (= kind :update-main) @@ -1039,14 +1039,14 @@ :undo (-> state (assoc :undo (pop history)) (update :redo conj (peek history))) :redo (-> state (assoc :redo (pop redo)) (update :undo conj (peek redo))) (cond-> state - changed (-> (assoc :checkpoint (oracle/snapshot situation) :redo []) + changed (-> (assoc :checkpoint (oracle/history-snapshot situation) :redo []) (update :undo conj {:before model :after next-model :reference-start (reference-snapshot situation)})))))))) (defn initial-state [world-spec] (let [situation (world world-spec)] - {:situation situation :baseline (oracle/snapshot situation) + {:situation situation :baseline (oracle/history-snapshot situation) :model {} :trace [] :undo [] :redo [] :executed {} :coverage {}})) (defn- check-sequence @@ -1062,10 +1062,10 @@ (oracle/fail :I7 {:phase :history-depth :runner depth :model (count undo)})) (when-not (if (:accepted-libraries situation) (= (get baseline (:id (tm/file situation))) - (get (oracle/snapshot undone) (:id (tm/file situation)))) - (= baseline (oracle/snapshot undone))) + (get (oracle/history-snapshot undone) (:id (tm/file situation)))) + (= baseline (oracle/history-snapshot undone))) (oracle/fail :I7 {:phase :sequence-undo})) - (when-not (= (oracle/snapshot situation) (oracle/snapshot redone)) + (when-not (= (oracle/history-snapshot situation) (oracle/history-snapshot redone)) (oracle/fail :I7 {:phase :sequence-redo})))) (defn- check-source-sequences @@ -1080,9 +1080,9 @@ undone undo)] (when-not (= (count undo) (r/undo-depth editor)) (oracle/fail :I7 {:phase :source-history-depth})) - (when (and (not foreign-write) (not= baseline (get (oracle/snapshot undone) owner))) + (when (and (not foreign-write) (not= baseline (get (oracle/history-snapshot undone) owner))) (oracle/fail :I7 {:phase :source-sequence-undo})) - (when-not (= (oracle/snapshot editor) (oracle/snapshot redone)) + (when-not (= (oracle/history-snapshot editor) (oracle/history-snapshot redone)) (oracle/fail :I7 {:phase :source-sequence-redo}))))) (defn run diff --git a/common/src/app/common/test_helpers/composable/oracle.cljc b/common/src/app/common/test_helpers/composable/oracle.cljc index 5063bd5690..59d333e7bf 100644 --- a/common/src/app/common/test_helpers/composable/oracle.cljc +++ b/common/src/app/common/test_helpers/composable/oracle.cljc @@ -54,10 +54,17 @@ (fail :I9 {:phase :requested-grid :expected expected :actual actual})))) (defn snapshot + ([situation] (snapshot situation {})) + ([situation opts] + (into {} (map (fn [[id file]] + [id (thn/normalize-file file (merge {:position-data? false} opts))])) + (assoc (tm/aux-files situation) (:id (tm/file situation)) (tm/file situation))))) + +(defn history-snapshot + "The `snapshot` undo and redo must give back: without deleted components, + since undoing a component's creation leaves it deleted." [situation] - (into {} (map (fn [[id file]] - [id (thn/normalize-file file {:position-data? false})])) - (assoc (tm/aux-files situation) (:id (tm/file situation)) (tm/file situation)))) + (snapshot situation {:deleted-components? false})) (defn derived-attributes [objects shape] diff --git a/common/src/app/common/test_helpers/normalize.cljc b/common/src/app/common/test_helpers/normalize.cljc index 580299d3e1..c96bf20d5c 100644 --- a/common/src/app/common/test_helpers/normalize.cljc +++ b/common/src/app/common/test_helpers/normalize.cljc @@ -15,7 +15,9 @@ - a linked file's `:synced-at`, the server sync status rather than authored design data; - with `{:position-data? false}`, the shapes' `:position-data`, the text - layout that WASM measurement regenerates. + layout that WASM measurement regenerates; + - with `{:deleted-components? false}`, the deleted components, which an + undo of a component's creation leaves where there was none. Shapes and the maps that hold them become plain maps, so a shape record compares equal to a map with the same attributes, and an objects map (see `app.common.types.objects-map`) to a plain map with the same entries. @@ -54,7 +56,7 @@ `:position-data?` (default true) keeps the shapes' `:position-data`." ([file] (normalize-file file {})) ([file opts] - (let [opts (merge {:position-data? true} opts)] + (let [opts (merge {:position-data? true :deleted-components? true} opts)] (-> file (dissoc :modified-at :synced-at) (update :data @@ -62,6 +64,11 @@ (-> data (d/update-when :pages-index (partial map-vals #(normalize-page % opts))) + (d/update-when :components + (fn [components] + (cond->> components + (not (:deleted-components? opts)) + (into {} (remove (comp :deleted val)))))) (d/update-when :components (partial map-vals #(normalize-component % opts)))))))))) diff --git a/common/test/common_tests/logic/variants_test.cljc b/common/test/common_tests/logic/variants_test.cljc index 989c937ad2..c615856ece 100644 --- a/common/test/common_tests/logic/variants_test.cljc +++ b/common/test/common_tests/logic/variants_test.cljc @@ -562,6 +562,34 @@ (t/is (= 3 (count values))) (t/is (apply distinct? values)))) +(t/deftest test-undo-add-new-variant-keeps-it-deleted + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02)) + page (thf/current-page file) + new-id (uuid/next) + main-id (uuid/next) + + ;; ==== Action + changes (-> (pcb/empty-changes nil (:id page)) + (pcb/with-library-data (:data file)) + (pcb/with-objects (:objects page)) + (clv/generate-add-new-variant (ths/get-shape file :m01) (thi/id :v01) new-id main-id 0)) + file' (thf/apply-changes file changes) + undone (thf/apply-undo-changes file' changes) + redone (thf/apply-changes undone changes) + + ;; ==== Get + deleted (get-in undone [:data :components new-id])] + + ;; ==== Check + ;; a copy another file made of it can still find it, and restore it + (t/is (true? (:deleted deleted))) + (t/is (= main-id (get-in deleted [:objects main-id :id]))) + (t/is (nil? (get-in (thf/current-page undone) [:objects main-id]))) + (t/is (not (:deleted (get-in redone [:data :components new-id])))) + (t/is (some? (get-in (thf/current-page redone) [:objects main-id]))))) + (t/deftest test-delete-variant ;; When a variant container becomes empty, it id automatically deleted (let [;; ==== Setup