Skip to content

fix(history): let goto reach steps restored or recorded out of id order - #378

Open
Exeloo wants to merge 2 commits into
mainfrom
fix/history-restore-order
Open

Exeloo wants to merge 2 commits into
mainfrom
fix/history-restore-order

Conversation

@Exeloo

@Exeloo Exeloo commented Nov 29, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Closes #265

HistoryStack.goTo compared step ids to decide how far to undo, but steps restored under the steps made while loading (and remote changes recorded before a redo) are numbered out of stack order. Clicking such a step in the History panel did nothing, or undid the wrong steps.

  • goTo now works on positions in the stack (packages/history/src/stack/history-stack.ts): it finds the clicked step's index and undoes the undoable steps above it. goTo(0) still undoes every step. An id the stack no longer has (trimmed or invalidated, for example a click racing a trim) now does nothing, where it used to undo an arbitrary set of steps.
  • After a restore under new steps, the state at load (savepoint 0) moves to the last restored step, so returning to it reads as saved.
  • HistoryEntry.id gets a doc comment: unique in its stack, not in stack order.
-      for (;;) {
-        const last = this._past.findLast((entry) => entry.undoable);
-        if (!last || last.id <= entryId) return;
+      const target =
+        entryId === SAVED_AT_START ? -1 : this._past.findIndex((entry) => entry.id === entryId);
+      if (target < 0 && entryId !== SAVED_AT_START) return;
+      while (this._past.findLastIndex((entry) => entry.undoable) > target) {

Public signatures are unchanged.

How do you test this PR?

New tests in packages/history/test/stack/history-stack.spec.ts:

  • goTo reaches steps restored under the steps made while loading (the History goTo does nothing on steps restored under newer ones #265 repro: push, restore two entries, go to the first restored step);
  • an id read before a queued restore still points at its step;
  • goTo reaches a change of another origin recorded before a redo;
  • the state at load stays the savepoint when steps are restored under new ones.
Command Result
npx vitest run in packages/history (reviewer, on 04f66b2) 4 files, 26 tests passed
pnpm lint (reviewer, on 04f66b2) 29/29

The producer reported the new tests failing on main and passing with the fix; those outputs were not kept, and the reviewer did not reproduce the red run. Not run on this branch: the root build, typecheck and the browser tests.

Integration: a scratch branch with every editor branch of this sweep merged passed pnpm build, lint, typecheck, pnpm test --force (1014 passed, 1 skipped) and the full e2e suite with the engine (141/141, Chromium only).

Merge danger

  • Two-way door. Internal to the history package, with no API change. Runtime dependents: the editor app and the history-panel plugin. Merging to main deploys the editor app.
  • Behaviour change: goTo with an unknown id does nothing. Its callers (HistoryService.goTo, the History panel) only pass 0 or an id from the rendered steps.
  • Open question for the team: a restore while the stack has no steps yet still sets the savepoint to null (so the state at load reads as unsaved), while a restore under new steps now moves it. Today nothing reads dirty in production (see HistoryStack dirty tracking is never wired #352), so no user sees the difference.

Follow-up issues

Quality gate

Small gate: one reviewer covering every lane.

Lane Result HEAD covered
Docs Pass: the goTo docstring still holds, HistoryEntry.id documented 04f66b2
Correctness Pass (Minor: the two restore paths disagree on the savepoint, question above) 04f66b2
Performance Pass: same order as the old loop, bounded by the stack limit 04f66b2
Accessibility N/A: no UI change 04f66b2
Security N/A: in-memory stack logic, no trust boundary 04f66b2
Style Pass 04f66b2

@Exeloo
Exeloo requested a review from Tchips46 as a code owner November 29, 2026 11:15
@github-actions github-actions Bot added the packages:history Related to History package label Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

packages:history Related to History package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

History goTo does nothing on steps restored under newer ones

1 participant