🐛 Preserve file destinations through undo and redo

Keep a namespaced file destination on each history change. Replay changes
in order in their target files, including stacked entries and transactions.
Keep payload fields named file-id intact, such as set-tokens-source (F111).

Snapshot the current file and every library in both composable runners.
Name the file in failure signatures; normalize the server synced-at status
stamp alongside modified-at. Update golden cases H and AP for this exclusion.
The library update regression checks both files through undo and redo and
failed against the old code.

Common JVM: 1705 tests; JS: 1633; frontend: 1495; backend: 1610; exporter: 14.
All pass, with lint and formatting checks for both changed modules.

Benchmark vs Phase 1e (lower quantile, ms):
world                 baseline     now   ratio
main-10                  1.790   0.993    0.55
main-100                15.415   9.038    0.59
main-1000              153.086  90.145    0.59
three-levels            35.215  18.989    0.54
chain                   68.931  35.045    0.51
library-500             83.083  85.830    1.03
many-shapes              4.837   3.344    0.69
variant-100             32.052  33.586    1.05
All worlds pass the 10% regression threshold.

AI-assisted-by: GPT-6
This commit is contained in:
alonso.torres 2026-10-22 14:37:48 +02:00
parent 5b4049b5da
commit d077a5ec8e
6 changed files with 107 additions and 56 deletions

View File

@ -363,10 +363,10 @@
:operation (->WrongUndoEdit)})]
;; the redo leaves the wrong name in place, and so does the variant undo
(t/is (= [:undo :redo :variant-undo] (phases situation)))
(t/is (= (repeat 3 #{[:object :copy-root :name]})
(t/is (= (repeat 3 #{[:file :file1 :object :copy-root :name]})
(map :signature (uc/failures situation))))
(t/is (= {:name "Wrong"}
(-> (uc/failures situation) first :diff second :data :pages-index
(-> (uc/failures situation) first :diff second :files :file1 :data :pages-index
vals first :objects (get :copy-root))))))
(t/deftest round-trip-reports-a-change-with-no-undo-entry
@ -410,7 +410,7 @@
(let [edit (tm/assign-id (->WrongUndoEdit))
situation (r/run-variant {:setup setup/simple-component-with-copy
:operation edit})
pin #{[:object :copy-root :name]}
pin #{[:file :file1 :object :copy-root :name]}
known {:bug "F0" :fails {:undo pin :redo pin :variant-undo pin} :op edit}
verdict #(uc/verdict situation {:undo-check {:known-failures %}})]
(t/is (= {:unexpected [] :resolved []} (verdict [known])))
@ -457,7 +457,7 @@
(let [edit (tm/assign-id (->WrongUndoEdit))
situation (r/run-variant {:setup setup/simple-component-with-copy
:operation edit})
pin #{[:object :copy-root :name]}
pin #{[:file :file1 :object :copy-root :name]}
known {:bug "F0" :fails {:undo pin :redo pin :variant-undo pin} :op edit}
check #(report-types (fn [] (uc/check! situation {:undo-check %})))]
(t/is (= [:fail :fail :fail] (check {})))
@ -470,7 +470,7 @@
right (n/change-property :main-child :fills red)
situation (r/run-variant {:setup setup/simple-component-with-copy
:operation (tm/in-sequence [wrong right])})
pin #{[:object :copy-root :name]}
pin #{[:file :file1 :object :copy-root :name]}
known {:bug "F0" :fails {:undo pin :redo pin :variant-undo pin} :op wrong}
verdict #(uc/verdict situation {:undo-check {:known-failures [%]}})
phases (fn [k mark] (mapv :phase (k (verdict mark))))]
@ -488,7 +488,7 @@
(t/deftest a-mark-that-applies-to-no-variant-fails
(let [edit (tm/assign-id (->WrongUndoEdit))
pin #{[:object :copy-root :name]}
pin #{[:file :file1 :object :copy-root :name]}
mark {:bug "F0" :fails {:undo pin} :op edit}
case-map {:undo-check {:known-failures [mark]}}]
(t/is (= [:fail] (report-types #(uc/check-marks-applied! case-map :pure []))))

View File

@ -502,10 +502,10 @@
(rx/filter dch/commit?)
(rx/map deref)
(rx/mapcat
(fn [{:keys [save-undo? undo-changes redo-changes undo-group tags stack-undo? selected-before translation?]}]
(fn [{:keys [file-id save-undo? undo-changes redo-changes undo-group tags stack-undo? selected-before translation?]}]
(if (and save-undo? (seq undo-changes))
(let [entry {:undo-changes undo-changes
:redo-changes redo-changes
(let [entry {:undo-changes (mapv #(assoc % ::dwu/file-id file-id) undo-changes)
:redo-changes (mapv #(assoc % ::dwu/file-id file-id) redo-changes)
:undo-group undo-group
:tags tags
:selected-before selected-before

View File

@ -327,6 +327,47 @@
(assoc-in state [:workspace-local :selected] selected-ids)
state))))
(defn- history-runs
"The runs of `pairs` (`[applied inverse]` per history entry, in the order
they apply) that share a destination file, as `[file-id applied inverse]`.
The inverse of a run undoes it; within an entry, the last run of a file
carries the entry's inverse for that file."
[pairs]
(->> pairs
(mapcat (fn [[applied inverse]]
(let [runs (partition-by ::file-id applied)
last-run (into {} (map-indexed (fn [i run] [(::file-id (first run)) i])) runs)]
(map-indexed (fn [i run]
(let [file-id (::file-id (first run))]
[file-id
(vec run)
(if (= i (get last-run file-id))
(filterv #(= file-id (::file-id %)) inverse)
[])]))
runs))))
(partition-by first)
(map (fn [segments]
[(ffirst segments)
(into [] (mapcat second) segments)
(into [] (mapcat #(nth % 2)) (reverse segments))]))))
(defn- commit-history-changes
"Replay the history `pairs` (see `history-runs`) in order, keeping each run
in its destination file. Older entries without a destination use the
current file. Each commit is undoable by a rollback, which also puts the
history back to `undo-index`. `translation` marks them as moves only."
[origin undo-index pairs translation]
(->> (history-runs pairs)
(map (fn [[file-id applied inverse]]
(dch/commit-changes {:file-id file-id
:redo-changes (mapv #(dissoc % ::file-id) applied)
:undo-changes (mapv #(dissoc % ::file-id) inverse)
:undo-index undo-index
:origin origin
:save-undo? false
:translation? translation})))
(rx/from)))
(defn undo-to-index
"Repeat undoing or redoing until dest-index is reached."
[dest-index]
@ -346,34 +387,25 @@
index (or (:index undo) (dec (count items)))]
(when (and (some? items)
(<= -1 dest-index (dec (count items))))
(let [[redo-changes undo-changes]
(cond
(< dest-index index)
(let [entries (subvec items (inc dest-index) (inc index))]
[(into [] (mapcat :undo-changes) (reverse entries))
(into [] (mapcat :redo-changes) entries)])
(> dest-index index)
(let [entries (subvec items (inc index) (inc dest-index))]
[(into [] (mapcat :redo-changes) entries)
(into [] (mapcat :undo-changes) (reverse entries))])
:else
[[] []])
(let [pairs (cond
(< dest-index index)
(->> (subvec items (inc dest-index) (inc index))
(reverse)
(map (juxt :undo-changes :redo-changes)))
(> dest-index index)
(->> (subvec items (inc index) (inc dest-index))
(map (juxt :redo-changes :undo-changes)))
:else [])
changes (into [] (mapcat first) pairs)
;; a replay of moves only is itself a move
translation (->> (subvec items
(inc (min dest-index index))
(inc (max dest-index index)))
(every? :translation))]
(when (seq redo-changes)
(rx/of (materialize-undo redo-changes dest-index)
;; Undoable by a rollback, which also puts the index back
(dch/commit-changes {:redo-changes redo-changes
:undo-changes undo-changes
:undo-index index
:origin it
:save-undo? false
:translation? translation})))))))))))
(when (seq changes)
(rx/concat
(rx/of (materialize-undo changes dest-index))
(commit-history-changes it index pairs translation)))))))))))
(declare ^:private assure-valid-current-page)
@ -414,15 +446,11 @@
(let [first-item (get items undo-group-index)]
(rx/of (undo-to-index (dec undo-group-index))
(restore-selection (:selected-before first-item))))
(rx/of (materialize-undo changes (dec index))
(dch/commit-changes {:redo-changes changes
:undo-changes (:redo-changes item)
:undo-index index
:save-undo? false
:translation? (:translation item)
:origin it})
(restore-selection (:selected-before item))
(assure-valid-current-page)))))))))))
(rx/concat
(rx/of (materialize-undo changes (dec index)))
(commit-history-changes it index [[changes (:redo-changes item)]] (:translation item))
(rx/of (restore-selection (:selected-before item))
(assure-valid-current-page))))))))))))
(def redo
(ptk/reify ::redo
@ -455,14 +483,10 @@
(let [last-item (get items redo-group-index)]
(rx/of (undo-to-index redo-group-index)
(restore-selection (:selected-after last-item))))
(rx/of (materialize-undo changes (inc index))
(dch/commit-changes {:redo-changes changes
:undo-changes (:undo-changes item)
:undo-index index
:origin it
:save-undo? false
:translation? (:translation item)})
(restore-selection (:selected-after item))))))))))))
(rx/concat
(rx/of (materialize-undo changes (inc index)))
(commit-history-changes it index [[changes (:undo-changes item)]] (:translation item))
(rx/of (restore-selection (:selected-after item)))))))))))))
(defn- assure-valid-current-page
[]

View File

@ -46,7 +46,7 @@
:case-ao-copy-dropped-into-a-grid-cell-reaches-copies
[{:transcript " {:copy :loose, :into :main-root, :cell [1 3]}", :states ["abc6e41a291516a2" "571d314ba8917a08"]}]
:case-ap-library-main-child-removal-reaches-copy
[{:transcript "", :states ["f53d220c42298128" "7c2dfde6478d399c"]}]
[{:transcript "", :states ["dd877acf0d7f120c" "0a472792b8a73e33"]}]
:case-aq-main-change-reaches-a-copy-nested-in-a-sub-board
[{:transcript " {:target #fn, :property :fills, :value \"#00ff00\"}\n {:target :main-instance, :property :fills, :value \"#ff0000\"}", :states ["d458024081bee6ec" "df29aaf3b63bece6" "0aef0de70c1e2e98"]}
{:transcript "one-of -> \n {:target :main-instance, :property :fills, :value \"#ff0000\"}", :states ["c9ecea6283cef90c" "be12b667808778f3"]}]
@ -109,7 +109,7 @@
:case-f-move-shape-in-main-auto-propagates-order-to-clean-copy
[{:transcript " {:target :main-child1, :parent :main-root, :to-index 2}", :states ["1994876f6bbe7d2e" "de144b280bc4fc82"]}]
:case-h-library-change-propagates-across-file-boundary-on-sync
[{:transcript "", :states ["d8c5e8bda3728127" "6ee281995d7d8f65"]}]
[{:transcript "", :states ["46c4b8337914be2d" "d95e20909221223c"]}]
:case-i-undo-reverts-edit-and-its-auto-propagation
[{:transcript " {:target :main-child, :property :fills, :value \"#ff0000\"}\n \n {:times 1}", :states ["386e940ac63af4b1" "734ec524d88eb6bb" "ff1fa863b88027e5"]}]
:case-k-synchronisation-scenarios

View File

@ -480,12 +480,12 @@
(let [{:keys [index items]} (:workspace-undo @st/state)]
(or index (dec (count items)))))
(defn- snapshot
"The normalized live file the round trip compares. WASM regenerates
(defn snapshot
"The normalized live files the round trip compares. WASM regenerates
`:position-data` by measuring, so it is left out there."
[]
(uc/snapshot (current-file)
{:position-data? (not (features/active-feature? @st/state "render-wasm/v1"))}))
(uc/snapshot-files (:files @st/state)
{:position-data? (not (features/active-feature? @st/state "render-wasm/v1"))}))
(defn- mark-baseline
[situation]

View File

@ -16,6 +16,7 @@
[app.common.test-helpers.ids-map :as cthi]
[app.common.test-helpers.shapes :as cths]
[app.main.data.workspace.libraries :as dwl]
[app.main.data.workspace.undo :as dwu]
[app.main.repo :as rp]
[app.main.store :as st]
[beicon.v2.core :as rx]
@ -72,3 +73,29 @@
(t/is (= new-color (nested-child-fill library'))))
(t/is (= new-color (fill-of (ftm/current-file) :copy-a-child)))
(t/is (= new-color (fill-of (ftm/current-file) :copy-b-nested-child)))))))
(t/deftest ^:async undo-and-redo-update-main-restore-both-files
(await
(mock/with-mocks*
{rp/cmd! (mock/stub (fn [& _] (rx/of nil)))
rx/timer mock/timer-mock}
(let [library (install!)]
(await (ftm/await-step [(ftm/fill-commit :copy-a-child new-color {})]))
(let [before (ftm/snapshot)]
(await (ftm/await-step [(dwl/update-component-sync (cthi/id :copy-a) (:id library))]))
(let [after (ftm/snapshot)]
(await (ftm/await-step [dwu/undo]))
(t/is (= before (ftm/snapshot)))
(await (ftm/await-step [dwu/redo]))
(t/is (= after (ftm/snapshot)))))
(await (ftm/await-step [dwu/undo]))
(let [restored (get-in @st/state [:files (:id library)])]
(t/is (= old-color (fill-of restored :main-a-child)))
(t/is (= old-color (nested-child-fill restored))))
(t/is (= old-color (fill-of (ftm/current-file) :copy-b-nested-child)))
(t/is (= new-color (fill-of (ftm/current-file) :copy-a-child)))
(await (ftm/await-step [dwu/redo]))
(let [redone (get-in @st/state [:files (:id library)])]
(t/is (= new-color (fill-of redone :main-a-child)))
(t/is (= new-color (nested-child-fill redone))))
(t/is (= new-color (fill-of (ftm/current-file) :copy-b-nested-child)))))))