🐛 Keep delayed copy measurements with their undo group

Route non-stacked derived commits to their applied group when a later
edit or transaction intervenes. Copy remeasurement must undo with the
font action rather than becoming a separate step after an unrelated edit.
Keep adjacent non-stacked entries and exclude the redo tail from routing.

Extend the font-boundary regression to component mains and copies, and
cover both stacking modes during a later transaction. The regression
fails before the fix; all 1,563 frontend tests and 10,880 assertions pass.
Live WASM history passes exact undo/redo and full-sync consistency.

The sync core is unchanged by this undo fix. Its preceding benchmark
ratios remain 0.48-0.76 of the unchanged baseline, with no regression flag.

AI-assisted-by: gpt-6
This commit is contained in:
alonso.torres 2026-10-27 10:16:15 +01:00
parent 503330d178
commit e5dc24ed83
3 changed files with 95 additions and 83 deletions

View File

@ -171,9 +171,9 @@
(assoc-in [:workspace-undo :transaction :tags] tags)))
(defn append-undo
"Add changes to undo history. Stacked changes extend their applied undo
group when present, otherwise the current entry. An open transaction
accumulates changes unless they stack into another applied group."
"Add changes to undo history. Delayed changes extend their applied undo
group across later edits or transactions, even without stacking.
Stacked changes without a matching group extend the current entry."
[entry stack?]
(assert (check-undo-entry entry))
@ -191,7 +191,7 @@
in-group? (fn [item group]
(or (= group (:undo-group item))
(contains? (:undo-groups item) group)))
group-index (when (and stack? (:undo-group entry)
group-index (when (and (:undo-group entry)
(not (in-group? transaction (:undo-group entry))))
(last (keep-indexed
(fn [index item]
@ -200,7 +200,8 @@
(cond
;; A delayed derived change still belongs to its primary action,
;; even after another edit or transaction has started.
(some? group-index)
(and (some? group-index)
(or stack? (< group-index index) transaction))
(stack-undo-entry state entry group-index)
(and (get-in state [:workspace-undo :transaction])

View File

@ -182,57 +182,67 @@
(st/emit! #(assoc % :features features))))))
(t/deftest ^:async a-delayed-font-resize-stays-with-its-font-edit
(let [features (:features @st/state)
ready (atom true)
face {:font-id "gfont-roboto" :weight 400 :style 0 :emoji? false}]
(thw/setup-wasm-mocks!)
(try
(await
(mock/with-mocks*
{wasm/text-editor-has-focus? (mock/stub (constantly false))
wasm.fonts/get-content-fonts (mock/stub (constantly [face]))
wasm.fonts/make-font-data (mock/stub identity)
wasm.fonts/font-ready? (mock/stub (fn [_] @ready))
wasm/get-text-dimensions (mock/stub (fn [] {:x 0 :y 0 :width 100 :max-width 100
:height (if @ready 40 20)}))
dwthw/render-thumbnail (mock/stub (fn [& _] (ptk/data-event ::thumbnail-skipped)))}
(let [file (-> (thf/sample-file :file1)
(doseq [component? [false true]]
(let [features (:features @st/state)
ready (atom true)
face {:font-id "gfont-roboto" :weight 400 :style 0 :emoji? false}
measuring (atom nil)]
(thw/setup-wasm-mocks!)
(try
(await
(mock/with-mocks*
{wasm/text-editor-has-focus? (mock/stub (constantly false))
wasm.fonts/get-content-fonts (mock/stub (constantly [face]))
wasm.fonts/make-font-data (mock/stub identity)
wasm.fonts/font-ready? (mock/stub (fn [_] @ready))
wasm/set-shape-text-content (mock/stub (fn [id _] (reset! measuring id)))
wasm/get-text-dimensions (mock/stub (fn [] {:x 0 :y 0 :width 100 :max-width 100
:height (if @ready
(if (= @measuring (thi/id :copy-child)) 50 40)
20)}))
dwthw/render-thumbnail (mock/stub (fn [& _] (ptk/data-event ::thumbnail-skipped)))}
(let [file (cond-> (thf/sample-file :file1)
component?
(thtx/add-text-component-with-copy [[["Read the guide"]]])
(not component?)
(ths/add-sample-shape :text :type :text :width 100 :height 20
:grow-type :auto-height
:content (thtx/content [[["Read the guide"]]]))
(ths/add-sample-shape :rect :type :rect :opacity 1))
_ (ftm/install! (tm/make-situation file))
before (ftm/snapshot)]
(st/emit! #(update % :features (fnil conj #{}) "render-wasm/v1"))
(reset! ready false)
(st/emit! (dwt/update-all-attrs [(thi/id :text)]
{:font-id "gfont-roboto" :font-family "Roboto"
:font-variant-id "regular"}))
(await (ha/wait-for #(some (comp seq :font) (vals (wrf/pending)))
"font edit is pending"))
(st/emit! (dwsh/update-shapes [(thi/id :rect)] #(assoc % :opacity 0.4)))
(await (ha/wait-for #(= 0.4 (:opacity (ths/get-shape (ftm/current-file) :rect)))
"unrelated edit commits before the font arrives"))
(let [text (ths/get-shape (ftm/current-file) :text)
faces (map (comp wasm.fonts/font-data-key wasm.fonts/make-font-data)
(wasm.fonts/get-content-fonts (:content text)))]
(t/is (seq faces) "the font wait has a face to release")
(reset! ready true)
(doseq [face faces] (rx/push! wasm.fonts/font-stored-stream face)))
(t/is (= :settled (await (ftm/await-step []))))
(t/is (mth/close? 40 (:height (ths/get-shape (ftm/current-file) :text))))
(let [after (ftm/snapshot)]
(t/is (= :settled (await (ftm/await-step [dwu/undo]))))
(t/is (= 1 (:opacity (ths/get-shape (ftm/current-file) :rect))))
(t/is (mth/close? 40 (:height (ths/get-shape (ftm/current-file) :text)))
"undoing the unrelated edit keeps the font's measured height")
(t/is (= :settled (await (ftm/await-step [dwu/undo]))))
(t/is (uc/same-state? before (ftm/snapshot)))
(t/is (= :settled (await (ftm/await-step [dwu/redo dwu/redo]))))
(t/is (uc/same-state? after (ftm/snapshot)))))))
(finally
(thw/teardown-wasm-mocks!)
(st/emit! #(assoc % :features features))))))
:content (thtx/content [[["Read the guide"]]])))
text-id (thi/id (if component? :main-child :text))
file (-> file
(ths/add-sample-shape :rect :type :rect :opacity 1))
_ (ftm/install! (tm/make-situation file))
before (ftm/snapshot)]
(st/emit! #(update % :features (fnil conj #{}) "render-wasm/v1"))
(reset! ready false)
(st/emit! (dwt/update-all-attrs [text-id]
{:font-id "gfont-roboto" :font-family "Roboto"
:font-variant-id "regular"}))
(await (ha/wait-for #(some (comp seq :font) (vals (wrf/pending)))
"font edit is pending"))
(st/emit! (dwsh/update-shapes [(thi/id :rect)] #(assoc % :opacity 0.4)))
(await (ha/wait-for #(= 0.4 (:opacity (ths/get-shape (ftm/current-file) :rect)))
"unrelated edit commits before the font arrives"))
(let [text (ths/get-shape-by-id (ftm/current-file) text-id)
faces (map (comp wasm.fonts/font-data-key wasm.fonts/make-font-data)
(wasm.fonts/get-content-fonts (:content text)))]
(t/is (seq faces) "the font wait has a face to release")
(reset! ready true)
(doseq [face faces] (rx/push! wasm.fonts/font-stored-stream face)))
(t/is (= :settled (await (ftm/await-step []))))
(t/is (mth/close? 40 (:height (ths/get-shape-by-id (ftm/current-file) text-id))))
(let [after (ftm/snapshot)]
(t/is (= :settled (await (ftm/await-step [dwu/undo]))))
(t/is (= 1 (:opacity (ths/get-shape (ftm/current-file) :rect))))
(t/is (mth/close? 40 (:height (ths/get-shape-by-id (ftm/current-file) text-id)))
"undoing the unrelated edit keeps the font's measured height")
(t/is (= :settled (await (ftm/await-step [dwu/undo]))))
(t/is (uc/same-state? before (ftm/snapshot)))
(t/is (= :settled (await (ftm/await-step [dwu/redo dwu/redo]))))
(t/is (uc/same-state? after (ftm/snapshot)))))))
(finally
(thw/teardown-wasm-mocks!)
(st/emit! #(assoc % :features features)))))))
(t/deftest ^:async hiding-a-grid-main-child-then-moving-its-main-stays-synced
(loop [live (ftm/start-generated (cg/world [2 1 {:text true :text-styles true :layout :grid}]))

View File

@ -60,35 +60,36 @@
(get-in @st/state [:workspace-undo :index]))
(t/deftest ^:async delayed-derived-changes-do-not-join-an-unrelated-transaction
(let [file (-> (thf/sample-file :file)
(ths/add-sample-shape :first :type :rect :opacity 1)
(ths/add-sample-shape :second :type :rect :opacity 1))
_ (ftm/install! (tm/make-situation file))
before (ftm/snapshot)
first-group (uuid/next)
second-group (uuid/next)
transaction (js/Symbol)
edit (fn [label attr value opts]
(let [page (thf/current-page (ftm/current-file))
changes (cls/generate-update-shapes
(pcb/empty-changes nil (:id page)) [(thi/id label)]
#(assoc % attr value) (:objects page) {})]
(dch/commit-changes (merge changes opts))))]
(t/is (= :settled (await (ftm/await-step [(edit :first :opacity 0.4 {:undo-group first-group})]))))
(t/is (= :settled
(await (ftm/await-step [(dwu/start-undo-transaction transaction)
(edit :second :opacity 0.5 {:undo-group second-group})
(edit :first :r1 8 {:undo-group first-group :stack-undo? true})
(dwu/commit-undo-transaction transaction)]))))
(let [after (ftm/snapshot)]
(t/is (= [first-group second-group] (mapv :undo-group (get-in @st/state [:workspace-undo :items]))))
(t/is (= :settled (await (ftm/await-step [dwu/undo]))))
(t/is (= 1 (:opacity (ths/get-shape (ftm/current-file) :second))))
(t/is (= 8 (:r1 (ths/get-shape (ftm/current-file) :first))))
(t/is (= :settled (await (ftm/await-step [dwu/undo]))))
(t/is (uc/same-state? before (ftm/snapshot)))
(t/is (= :settled (await (ftm/await-step [dwu/redo dwu/redo]))))
(t/is (uc/same-state? after (ftm/snapshot))))))
(doseq [stack? [true false]]
(let [file (-> (thf/sample-file :file)
(ths/add-sample-shape :first :type :rect :opacity 1)
(ths/add-sample-shape :second :type :rect :opacity 1))
_ (ftm/install! (tm/make-situation file))
before (ftm/snapshot)
first-group (uuid/next)
second-group (uuid/next)
transaction (js/Symbol)
edit (fn [label attr value opts]
(let [page (thf/current-page (ftm/current-file))
changes (cls/generate-update-shapes
(pcb/empty-changes nil (:id page)) [(thi/id label)]
#(assoc % attr value) (:objects page) {})]
(dch/commit-changes (merge changes opts))))]
(t/is (= :settled (await (ftm/await-step [(edit :first :opacity 0.4 {:undo-group first-group})]))))
(t/is (= :settled
(await (ftm/await-step [(dwu/start-undo-transaction transaction)
(edit :second :opacity 0.5 {:undo-group second-group})
(edit :first :r1 8 {:undo-group first-group :stack-undo? stack?})
(dwu/commit-undo-transaction transaction)]))))
(let [after (ftm/snapshot)]
(t/is (= [first-group second-group] (mapv :undo-group (get-in @st/state [:workspace-undo :items]))))
(t/is (= :settled (await (ftm/await-step [dwu/undo]))))
(t/is (= 1 (:opacity (ths/get-shape (ftm/current-file) :second))))
(t/is (= 8 (:r1 (ths/get-shape (ftm/current-file) :first))))
(t/is (= :settled (await (ftm/await-step [dwu/undo]))))
(t/is (uc/same-state? before (ftm/snapshot)))
(t/is (= :settled (await (ftm/await-step [dwu/redo dwu/redo]))))
(t/is (uc/same-state? after (ftm/snapshot)))))))
(t/deftest ^:async undo-and-redo-an-entry-without-undo-group
(ftm/install! (setup/simple-component-with-labeled-copy))