Skip to content

fix(undo): keep newest entries when stack is full - #13462

Merged
tiensonqin merged 1 commit into
logseq:masterfrom
yuxi-liu-wired:fix/undo-stack-cap
Sep 30, 2026
Merged

tiensonqin merged 1 commit into
logseq:masterfrom
yuxi-liu-wired:fix/undo-stack-cap

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

What changes

When the worker's undo stack reaches max-stack-length (250), conj-op keeps the oldest half and drops the newest half, including the entry it was pushing. After the 250th undoable change, the stack holds changes 1 to 125. The next change is pushed on top. Undo then takes back that change, then jumps to change 125, while changes 126 to 250 stay applied. The redo stack has the same cap.

;; before
(subvec result 0 (/ max-stack-length 2))
;; after
(subvec result (- (count result) (quot max-stack-length 2)))

The fix keeps the newest half. Each undo then meets the state that the entry above it left.

Tests

  • New full-undo-stack-keeps-newest-entries-test in frontend.worker.undo-redo-test, with max-stack-length redefined to 10: 12 title edits, then undo 7 times. On master the titles read v11 v10 v4 v3 v2 v1 child: after 2 undos it jumps back 6 edits. With the fix they read v11 v10 v9 v8 v7 v6 v5, and the stack is empty after that.
  • frontend.worker.undo-redo-test with the fix: 43 tests, 221 assertions, 0 failures.

Devin Review

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Devin Review

(let [result (conj (if (empty? col) [] col) op)]
(if (>= (count result) max-stack-length)
(subvec result 0 (/ max-stack-length 2))
(subvec result (- (count result) (quot max-stack-length 2)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Capped history retains unbounded old entries

After the first truncation, conj-op appends to a suffix subvector while retaining its discarded backing entries. Each later truncation advances the suffix, so repeated edits grow worker memory without bound despite the capped stack.

Learn more

A ClojureScript subvector is a view over its backing vector. When a full history stack takes its newest half, the returned stack still references all older entries in that backing vector. The next conj appends at the subvector's end in that backing vector; the next truncation repeats the process. Both undo and redo use conj-op, so sustained editing or undoing retains discarded history payloads in the worker.

Example: With a cap of 10, the tenth edit keeps five visible entries backed by ten. Five more edits fill the stack again; the next suffix retains five visible entries backed by fifteen. Repeating this cycle keeps increasing the backing vector even though the visible stack has at most nine entries.

Recommended fix: Materialize the retained suffix into a fresh vector when truncating, and test repeated truncation rather than only the visible stack count.

Suggested change
(subvec result (- (count result) (quot max-stack-length 2)))
(vec (subvec result (- (count result) (quot max-stack-length 2))))

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Direction fix is correct — the old code kept the oldest half and dropped the most recent ops at the cap.

One nit (Devin Review flagged it too): (subvec result (- (count result) (quot max-stack-length 2))) keeps a reference to the full 250-entry backing vector, so the trimmed ops aren't GC'd while the subvec lives. (vec (subvec ...)) would detach it.

Written by Devin

@tiensonqin tiensonqin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! 🚢 👍

A full undo or redo stack kept its oldest half and dropped the newest
half, including the entry just pushed. After the 250th undoable change,
changes 126 to 250 had no entries: undo stepped back to change 251, then
jumped to change 125 while 126 to 250 stayed applied. Keep the newest
half instead.
@tiensonqin
tiensonqin merged commit 5803194 into logseq:master Sep 30, 2026
12 of 13 checks passed
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
@yuxi-liu-wired
yuxi-liu-wired deleted the fix/undo-stack-cap branch September 30, 2026 20:02
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 1, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 4, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 8, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 11, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants