Undo and redo history
Handle $ARGUMENTS. check, or no argument, runs the gate bound. letters runs --peer-letters and --peer-letters --yjs at their gate bound. deep runs --size=5, --blocks=2 --size=5 and --peer-inserts and compares their failures with Findings. lower runs --lower after a fix removes a known finding. case <identity> runs --case=<identity> to print one case's full detail. update brings the verification in line with changed code, as Coverage record says.
The property. A user runs a short list of edits, undos, redos and one change from another peer. Each undo gives back the document and caret from before its step, and each redo the document and caret after it, and neither drops nor copies another peer's paragraph that its step never touched. docs/vision/plite.md says transaction history controls grouping, skipping and restoration, and that ordinary input groups by its idle clock. The laws hold the history plugin to how it groups: an edit under history: 'merge' joins the step before it, an edit after the idle window or under history: 'new-batch' starts a step, typing right after the letter the plain way or the idle way typed before joins, and a change under history: 'skip', such as another peer's, adds none. A new step clears the redos, and hasUndo and hasRedo match the steps left. A history saved with History.toJSON and restored into a fresh editor replays the same documents. With --peer-letters, the other peer runs any of the user's letter commands, and after each undo and redo each letter shows as often as that command and the user's steps still done leave it.
The harm. An undo that gives back another document loses the user's work, and one that loses a step leaves an edit the user cannot take back. Three history fixes landed before this check: 94cf545da2, ab4b6576ee and t_d3072b82's fix in 128eb410c9. A seed or a tester found each one, one case at a time.
What it checks
packages/plitejs/scripts/undo-redo-history.ts runs the real history plugin on every list up to a bound. It checks the TypeScript code itself, not a model of it.
Documents. Every document of up to --blocks top-level blocks, each a paragraph or a quote of paragraphs, from the shapes builder in packages/plitejs/scripts/authored-documents.ts. Each line holds one letter. The schema adds an align property to paragraphs and quotes. With --peer-inserts it adds a box, which holds paragraphs and sits only in a quote, and an outer, which holds paragraphs and quotes and sits only at the root.
Edits. The steps and commands of packages/plitejs/scripts/letter-commands.ts, which the recording check also runs: typing, Enter, Backspace and Delete at every caret beside a letter, a backward selection delete between every two carets, paste of two lines, wrap and unwrap at every letter. The check adds an align change at every letter and, as a list's first move on a document of two or more top-level blocks, the removal of each one. With --peer-inserts it adds a lift of every paragraph in a quote, a wrap of a quote's line in a box and of a root block in an outer, each written as operations and as a value diff of the whole document. The first edit in a list types X, the second Y and the third Z, so a doubled or lost letter shows.
Modes. Once the history holds a step, each edit runs four ways: right after the move before it, after the idle window, under history: 'merge' and under history: 'new-batch'. The script fakes performance.now() to step the clock. The idle way runs only where the plain way joined the step before, because elsewhere it records what the plain way records. The new-batch way runs there too, and wherever another edit can follow it, because the plugin stops typing from joining a step that new-batch started.
Moves. A list holds up to --depth moves and up to --edits edits, and no more moves than --size less the document's letters. Undo and redo run where a step can replay. In the default gate, right after a letter typed the plain or the idle way while another edit can still follow, a move may be an announce, a bare update that emits a screen-reader announcement and changes nothing else. The edit after it never runs under merge, because an explicit merge ignores the idle clock and would only repeat the lists without the announcement. That skip is a deliberate coverage limit. With merge kept, the gate compared 18,150 cases and found 20 more history-caret findings, each a baseline entry with the announcement inserted, so the gate misses a plugin in which an announcement changes what an explicit merge does. One move per list may be another peer's change, under history: 'skip' with the collaboration tag: a paragraph R appended to the document, the deletion of one block or two sibling blocks at any level that leaves each parent a block, or a move of a top-level block or a quote's line to another top-level place or quote line. A move comes last in its list, alone, with the deletion of a top-level block beside the moved one, or with an R typed in a paragraph the move crosses, so one update holds a move among other edits. With --peer-inserts, that change is never a move, which the default gate covers, and may also insert R at every block boundary, at the root and inside each quote, box and outer, or box(R) inside a quote. Where it follows exactly one edit and no edit follows it, the same update may also type r into the block beside the insert, delete that block's last letter or set the quote's align, written as operations or as a value diff.
With --peer-letters, the other peer's change is one letter command instead: typing r, Enter, Backspace and Delete at every caret beside a letter, wrap and unwrap at every letter, setting a letter's block to align: 'left', inserting a paragraph R at every block boundary at the root and inside each quote, or a move of a top-level block or a quote's line. It runs once the history holds a step, and only undos and redos follow it. Without --yjs it is a skipped update in the user's editor, as above. With --yjs, the user's editor and a second editor each bind their own Yjs document through the yjs plugin. The user's list reaches the second editor, which runs the command as a plain update, and its Yjs update comes back to the user's editor as a remote import.
Laws. After each move the script compares what the history did with a model of the steps. A case records its first failure per rule:
history-undo: an undo that does not give back the document from before its step, or that changes how many copies of another peer's paragraph R the document holds when its step never changed that count.
history-redo: the same for a redo and the document after its step.
history-caret: an undo or redo whose selection is not the one from before or after its step.
history-failed: an undo or redo that reports failed or pending, throws, or reports through the lifecycle error sink.
history-group: an edit that joins the step before it after the idle window or under new-batch, an edit under merge that starts a step of its own while no redo waits and no other peer has changed the document, typing that starts a step of its own right after the letter typed before, even with an announce between them, or another peer's appended paragraph that changes the number of undo steps.
history-stack: hasUndo or hasRedo that differs from the steps left, a changed document with no step, an edit that leaves a redo, or an announce that changes the undo or redo steps.
history-restore: a history restored from its JSON whose undos and redos give other documents than the editor it came from. It compares whole documents, another peer's paragraph included.
history-throws: an edit, another peer's change or History.fromJSON that throws an error other than a refusal.
history-lines: after another peer's move, an undo or redo that does not give back the lines of the document from before or after its step, less the lines of each block that peer deleted, or one that leaves a different number of the letters that peer typed, or a move that changes the number of undo or redo steps. A deleted line that a step changed takes away only a line that shares one of its letters, and a step may go with a deleted block only when its before and after differ only in blocks that share a letter with that block, or in empty lines when the block was an empty line. Lines compare as a set with counts and ignore R and empty lines, so a line put back in the wrong place, a lost empty line, or a surviving line lost beside a related deleted one still passes.
With --peer-inserts, two more laws file under history-undo and history-redo: a letter another peer deleted, in a block or by a text delete, never comes back, and once it deleted something, an undo or redo keeps every paragraph, letter and alignment it wrote. An alignment it set is exempt when the step adds or removes a quote. A peer update that also types, deletes a letter or aligns, or that arrives as a value diff, must place R where the same list with only the plain insert written as operations places it, and the script replays that list in its own editor to compare them after every move. Carets compare in the document with the peer's paragraph, letter and alignment stripped, no caret law holds after a peer's value diff, because applying one can match a user's line to the peer's paragraph, and a caret that misses its law does not stop the list.
With --peer-letters, only a list that ends in the other peer's command is judged, and these laws judge its unwinding instead of history-undo, history-redo and the history-lines law above:
history-letters: an undo or redo after which a letter shows a number of times other than the count before the peer's command, plus what that command added or removed, plus what each step still done added or removed, floored at zero. When the peer's command leaves fewer lines, a letter that an undone step deleted may stay deleted, because the line it would land in may be gone.
history-lines: when the peer's command leaves the lines as they were, less the letters it removed, an undo or redo that does not give the lines from before or after its step. The law sets aside the letters a step deleted beside a line the peer removed.
history-group: a peer's command that changes the number of undo or redo steps, unless it removed something and each dropped step changed only letters the peer removed or letters in lines the peer removed.
history-retrace: a redo that does not give the document the undo before it started from.
history-converge: with --yjs, after the unwinding reaches the second editor, a document there other than the user's. When the second editor does not admit an update, the finding files under history-failed.
Unwinding. At the end of each list the script undoes every step and redoes every step through the same laws, then saves the history as JSON, restores it into a fresh editor and unwinds that the same way. After another peer deletes a block, the model no longer knows which documents the steps give, so the script only checks that each undo and redo replays and that the restored history gives the same documents. After another peer's move, each undo and redo goes through the history-lines law instead of history-undo and history-redo. When the move also deleted a block and exactly the steps that may go with it went, the rest still go through that law; when other steps went, unwinding only checks that each step replays and keeps the letters that peer typed. A move after an earlier deletion gets only those replay checks too.
A refusal the editor gives on purpose counts in the summary's refused and is not a failure.
It compares the findings with packages/plitejs/scripts/undo-redo-history-baseline.json through tooling/scripts/finding-baseline.mjs. Every finding is filed under packages/plitejs/src/history. An identity names the document and the moves, never the result.
Run it
Run every command from the repository root.
pnpm check:undo-redo-history checks the gate bound: one block, three moves, two edits, size four. It compares 18,017 cases on 3 documents in 60 s to 135 s, depending on the machine's load. A --depth above 3 exits 2, because each move types its own letter. It prints one JSON summary line, then each finding the baseline does not list. pnpm check undo-redo-history runs the same step through the check runner, and pnpm check --affected runs it for a change under packages/plitejs/src/, to the script, check-harness.ts, letter-commands.ts or authored-documents.ts, to a file under packages/plitejs/test/support/, to config/plite-source-aliases.ts or to tooling/scripts/finding-baseline.mjs.
--blocks, --depth, --edits and --size raise one part of the bound. --size=5 compares 67,456 cases in 222 s to 337 s, and --blocks=2 --size=5 compares 387,347 cases on 12 documents in 367 s to 1,006 s, depending on the machine's load. A bound other than the gate's or --case skips the baseline and exits 1 on any failure.
pnpm check:undo-redo-history --peer-inserts runs the widened check at its own gate bound: one block, three moves, two edits, size five, because undoing a lift and redoing it after another peer's change takes three moves on quote(paragraph("a") paragraph("b")). It compares 119,206 cases on 3 documents in about 4 minutes at a load average near 45 and 23 minutes near 250, against its own baseline, packages/plitejs/scripts/undo-redo-history-peer-inserts-baseline.json, and its own case count in GATES. --lower and --init with the flag write that baseline. pnpm check does not run it.
pnpm check:undo-redo-history --peer-letters runs the letter-command check at its own gate bound: two blocks, three moves, one edit, size five. It compares 21,738 cases on 12 documents in 23 s to 64 s at a load average near 40 and in 144 s near 150, against packages/plitejs/scripts/undo-redo-history-peer-letters-baseline.json. --peer-letters --yjs compares 21,333 cases in 72 s to 100 s near 45 and in 41 minutes near 150, against packages/plitejs/scripts/undo-redo-history-yjs-letters-baseline.json, and prints about 18,000 Yjs Invalid access warnings. The Yjs run compares 405 fewer cases because more of the peer's commands drop a step over Yjs, and a list whose peer command fails a law stops there, as in the other gates. --lower and --init with the flags write each baseline. pnpm check runs neither.
--empty-quotes lets a quote hold no line and adds an edit that splits each top-level quote before its first line or after its last, which leaves an empty quote. At the gate bound it compares 21,141 cases and reads no baseline. The gate runs without it, because the schema the gate shares with the recording check requires a line in every quote.
--case='<identity or part of one>' prints the full stack of each failure whose identity contains it, such as --size=5 '--case=Enter at b|0 ; undo ; remote delete block 0.1'.
--jobs=<n> sets the number of worker processes, the machine's cores up to 16 by default.
- History follows a skipped change by the facts each commit records, and five flags check that path.
--digest prints the count and the SHA-256 of every commit's public change JSON. --facts runs checkFacts on every commit and counts a commit as faulted when its facts are wrong, or as unexplained when it holds unknown facts that no raw change or paste entered. --admitted prints each list whose skipped change the verdict admits. --exact plays each list of the mode's frozen admitted file two ways: once reading the history right after the skipped update, and once reading only the stack depths. After each press it compares the whole document with a tree of labelled blocks and letters that the script updates on its own. --trace prints one digest per mode of every press's document and effects on the lists outside that file. The default mode's file is packages/plitejs/scripts/undo-redo-history-admitted.txt, with 216 lists, and the --peer-letters mode's is packages/plitejs/scripts/undo-redo-history-peer-letters-admitted.txt, with 1,160. The gate fails when the verdict admits a list that its mode's file does not hold, which it prints as UNFROZEN, and when its mode's baseline lists one that the file holds, which it prints as BASELINED.
- Exit 0 is a pass and exit 1 a finding. Exit 2 is a bad flag or a crashed worker. At the gate the run also fails when it compares other than 18,017 cases, the
cases of its entry in the GATES list, so a change that stops edits from applying cannot shrink the gate while the baseline still matches. Update that count only with the reason it moved.
- After a fix removes a known finding, the gate fails until you run
pnpm check:undo-redo-history --lower. The baseline never grows. --init seeds only a rule the baseline lacks, and --lower and --init write nothing when the case count misses its pin in GATES.
Read a failure
A finding prints its rule, identity and the first line of its detail:
history-caret paragraph("a") :: Enter at a|1 ; Delete at a|1 [merge]
undo selects {"anchor":{"offset":1,"path":[0,0]},...}, not {...}
history-stack paragraph("a") :: wrap a ; unwrap a [merge]
after undo, hasUndo is false and hasRedo false, with 0 undo and 1 redo steps left
Both are known findings. In the first, Delete takes back the Enter it merges into, and the undo of the empty step moves only the caret. Run --case on the identity for the full diff and stack.
Tell a real bug from a modeling error before you change code:
- Real bug. A user can reach the list, and an undo, redo or restore gives what the law says it must not. Fix the history plugin, then run
--lower.
- Modeling error. The law's oracle is ambiguous for that list, such as which side of another peer's paragraph an undone block lands on. Fix the script with the reason.
- Intended change. A change to how history groups steps changes the law too. Change the law and
docs/vision/plite.md together. Never weaken a law or add a baseline entry to get green.
Findings
The gate finds 1,172: 70 history-caret, 25 history-group and 2 history-stack, all under history: 'merge', and 1,075 history-lines. The first three classes have one cause:
- An explicit merge whose edit takes back its step's change keeps an empty step.
Enter at a|1 ; Delete at a|1 [merge] leaves the document as it was and one undo step. Its undo moves only the caret, a later appended paragraph from another peer drops the step, and after wrap a ; unwrap a [merge] the undo leaves no redo. 70 history-caret, 25 history-group and 2 history-stack entries.
The 1,075 history-lines entries follow another peer's move. Before history followed facts, next failed 1,096 such lists: these 1,075, 20 that the default mode's frozen file holds and history now gets right, and quote(paragraph("a")) :: Enter at a|1 ; unwrap a ; remote move 1 to 2 and delete block 0, which the structural merge by position fixes. By what the law found on those 1,096, 242 undo to the wrong lines, 103 undo to the wrong lines less the deleted ones, 275 change the number of steps, 472 drop a letter the peer typed, 2 redo to the wrong lines and 2 never reach a step the law expects:
- An undo after another peer's move keeps text its step added.
paragraph("a") :: Enter at a|1 ; paste two lines at a|1 [merge] ; remote move 1 to 0 undoes to the lines y and a, not a. Most come after a paste or an Enter.
- An undo after another peer moves a line out of its quote loses letters.
quote(paragraph("a") paragraph("b")) :: Backspace at b|0 ; remote move 0.0 to 0 undoes to the line a, and b is gone; paragraph("a") :: wrap a ; align a ; remote move 0.0 to 1 undoes to no line.
- Another peer's move changes the number of undo or redo steps.
paragraph("a") :: Enter at a|0 ; Enter at a|0 ; remote move 2 to 0 and type R in block 1 leaves one undo step of two.
- An undo after another peer moves a block and deletes another gives lines other than the snapshot's less the deleted ones.
paragraph("a") :: paste two lines at a|0 ; paste two lines at a|0 ; remote move 1 to 3 and delete block 2 undoes to X and x, not X and xa.
- A redo after another peer's move drops the line it should restore.
quote(paragraph("a")) :: Enter at a|0 ; undo ; remote move 0.0 to 0 redoes to no line.
- An undo after another peer's move drops a letter that peer typed.
paragraph("a") :: align a ; Enter at a|1 ; remote move 0 to 2 and type R in block 1 types R in the line the Enter made, and the undo removes that line with it.
- Another peer's move that deletes a line a paste or an Enter made drops the whole step.
paragraph("a") :: paste two lines at a|0 ; paste two lines at a|0 ; remote move 0 to 2 and delete block 1 leaves one undo step of two, although the first paste also changed a.
- An undo after another peer deletes a pasted line loses the line it changed.
paragraph("a") :: paste two lines at a|0 ; Backspace at a|1 ; remote move 0 to 2 and delete block 1 undoes to no line, not a.
--size=5 finds 3,065 failures and --blocks=2 --size=5 56,652. The deep runs read no baseline. Before the fix for t_9c9c168f, the two-block run also gave 37 history-group findings where undo dropped a removed paragraph beside another peer's appended one. 33 start by removing a block, such as paragraph("a") paragraph("b") :: remove block 1 ; align a ; remote append R, and 4 are merged steps whose paste and selection delete remove one, such as paste two lines at b|0 ; select X|0..b|1 Backspace [merge] ; remote append R. Most hold an explicit merge. These classes have none:
An undo, redo or restore beside an empty quote throws. With --empty-quotes at the gate bound, 57 history-throws cases fail that the baseline does not list, with 433 history-lines cases, and 41 history-lines entries of the baseline do not appear. 40 of the throws and 14 of the history-lines cases come from the schema alone, where another peer moves a quote's only line out and leaves the quote empty: quote(paragraph("a")) :: Enter at a|0 ; undo ; remote move 0.0 to 0 puts the selection outside the root. 16 throws follow a split that one step merges with an Enter or a paste. That step goes invalid, so the undo and History.toJSON both throw Editor element "quote" at [0] cannot contain "text", or at [1], and the run reports it from the restore in History.fromJSON, as the restored-history class below does, such as quote(paragraph("a")) :: Enter at a|0 ; split quote 0 at 2 [merge] ; remote move 0.0 to 0 and delete block 1. The last, quote(paragraph("a")) :: split quote 0 at 1 ; unwrap a [merge] ; remote move 1 to 0, throws Unbalanced JSON token slice in carryThroughMove when the other peer's move arrives. Each of the 419 history-lines cases with a split follows another peer's move, as the baseline's 1,095 entries do. Before t_b628752f's fix, rebasing the split's undo read the first child of the empty quote as a text and crashed a worker.
An undo or redo throws after another peer moves a line inside a quote. quote(paragraph("a") paragraph("b")) :: Enter at a|0 ; Backspace at b|0 ; remote move 0.1 to 0 throws, with errors such as A text node cannot contain another node and a selection outside the root. At --size=5, 145 history-throws cases have no explicit merge, and 60 more do.
Another peer's appended paragraph drops an undo step. On paragraph("a") paragraph("b"), wrap a ; Backspace at b|0 ; remote append R leaves one undo step of two. 215 history-group cases.
A restored history refuses a step after another peer deletes a block. quote(paragraph("a") paragraph("b")) :: Enter at b|0 ; undo ; remote delete block 0.1 keeps a redo of the split, and History.fromJSON throws Editor element "quote" at [0] cannot contain "text" when it validates it. 11 history-throws cases.
An undo puts the caret in another peer's appended paragraph. paragraph("a") paragraph("b") :: Enter at a|0 ; select a|0..b|1 Backspace ; remote append R undoes to a caret at [2,0], not [1,0]. 36 history-caret cases.
--peer-inserts at its gate bound fails 10,942 cases, which its baseline lists: 9,432 history-undo, 443 history-group, 326 history-redo, 286 history-stack, 226 history-caret, 217 history-throws and 12 history-failed. Next fails each of them under the same check. The class counts below were taken at 10,308 findings, before typeOver joined the edits, which added 706 findings whose list holds a type-over, and before upstream fixes dropped 74, so they no longer sum to the total. Each finding falls in one class:
- An undo of a split brings back the letters of a line another peer deleted.
paragraph("a") :: Enter at a|0 ; wrap a [merge] ; remote delete block 1. 3,191 cases, all from the law that a deleted letter never comes back.
- An undo of a paste inside a quote loses another peer's paragraph between the pasted lines.
quote(paragraph("a")) :: paste two lines at a|1 ; remote insert R at 0.1 undoes to quote(paragraph("aR")). Inside a quote the paste records its change as a replacement of a text's close token, not as a plain insert, so the undo does not read as a join of two lines. 2,343 cases, 2,303 of them in a quote.
- An undo of a box or outer merges another peer's paragraph inside it into the user's line.
quote(paragraph("a") paragraph("b")) :: box b by value ; remote insert R at 0.1.0 undoes to quote(paragraph("a") paragraph("bR")). 1,801 cases with the wrap written as a value diff, and 130 more where an operations wrap shares its step with a split or a join.
- The explicit merge class above. 732 cases.
- An undo or redo of a move brings back a block another peer deleted, when that block was the moved one or its old container.
quote(paragraph("a") paragraph("b")) :: lift b by value ; undo ; remote delete block 0.1 redoes with b back, and quote(paragraph("a") paragraph("b")) :: lift a ; lift b ; remote delete block 1 undoes with b back. 576 cases.
- A peer update that also types, deletes a letter, aligns, boxes its paragraph or arrives as a value diff loses the paragraph or letter on undo.
quote(paragraph("a")) :: Enter at a|1 ; remote insert R at 0.1 and type r at the end of 0.1. 305 cases after splits, 150 after lifts, boxes, outers and wraps, and 66 after joins, deletes and typing.
- An undo or redo brings back the letter another peer deleted in the update that inserted its paragraph.
paragraph("a") :: wrap a ; remote insert R at 0 and delete the last letter of 0.0 by value. 220 cases.
- An undo, redo or restored history moves another peer's box into a parent that refuses it, and the replay or
History.fromJSON throws. quote(paragraph("a") paragraph("b")) :: box b ; undo ; remote insert box(R) at 0.2 and type r at the end of 0.1 by value throws box at [0,1] cannot contain box. 214 cases.
- An undo of an Enter loses another peer's paragraph beside lines its step or the step before it split.
paragraph("a") :: Enter at a|0 ; Enter at a|1 [merge] ; remote insert R at 2. 163 cases.
- A peer update that also types or deletes a letter places its paragraph elsewhere than a plain insert. On a redo of a box or outer,
R moves into the new container, as in quote(paragraph("a") paragraph("b")) :: outer a by value ; undo ; remote insert R at 1 and type r at the end of 0.1, which redoes to outer(quote(a, br), R) where the plain insert gives outer(quote(a, br)), R (30). A redo of a lift puts R on the other side of the quote's edge (2). An undo of a lift or outer places R unlike the plain insert, where the compound result can be the right one (24). A redo of an Enter puts the new line after R once the peer deleted the split line's letter (10). An undo of a join drops the empty line it splits back out (11). A peer update written as a value diff places R elsewhere than the plain insert after a box, outer, lift, unwrap, Enter, join or typing (109 on undo, 80 on redo). In 109 of them the same update written as operations passes every law: the diff reuses the user's tokens for R, so the user's letter or line close becomes the peer's. Six of the undo findings, which next fails too, follow a lift written as a value diff: after quote(paragraph("a") paragraph("b")) :: lift a by value, a peer update that inserts R or box(R) at 1.0 and types or deletes a letter there undoes to quote(a, R, b) with the letter change, as a lift written as operations does, while the plain insert undoes to quote(R, a, b).
- Another peer's inserted paragraph drops an undo step, like the appended paragraph above. 66 cases.
- A peer update that types where an undone split was drops the redo.
paragraph("a") :: Enter at a|1 ; undo ; remote insert R at 0 and type r at the end of 0. 37 cases. Whether the redo should survive a conflicting edit is a call nobody has made yet.
- Smaller classes. An explicit merge right after a lift starts its own step (16). An undo of an outer drops the user's empty line (5). A peer insert inside an outer changes the number of undo steps (2). A redo of a lift written as a value diff merges the peer's paragraph into the lifted line, as in
quote(paragraph("a") paragraph("b")) :: lift b by value ; undo ; remote insert R at 0.2 (3). A stored step nests a paragraph in a paragraph after a peer value diff (7). The restored-history refusal above (8). A peer value diff typing into the step's line reorders the user's letters (7).
The flag cannot see a loss that leaves the document looking like the one before the step once the peer's letter is stripped, and it does not generate a peer update that inserts an empty paragraph. Two such cases fail on this tree and before it. After quote(paragraph("a") paragraph("")) :: Enter at a|1, a peer types z at the new line's start, and undo gives quote((a), (z)), not quote((az), ()), at the root too. After paragraph("a") :: Enter at a|1, a peer types z at the new line's start and inserts an empty paragraph after it, and undo gives (a), (z). When the peer edits inside the run the undo replaces, a root run is still found by value, so after quote(paragraph("a")) quote(paragraph("a") paragraph("")) :: Enter at the first a|1, a peer's z in the new line, then undo, gives quote((a), (z)), quote((a)) and drops the second quote's empty line. A peer update that aligns the new line and inserts R beside it loses R on undo, and the check aligns only quotes. A peer update whose deleted letter empties the stacks leaves the placement law nothing to compare, and where the list with only the plain insert breaks a law itself, the placement law skips the list, so only the count of R checks it. When the peer aligns or types in the line and inserts R, a redo of a line break in one of two identical quotes splits the other quote, as before this change. After quote(paragraph("a")) quote(paragraph("a")) :: Enter at the second a|0 ; undo, the peer aligns the second quote's line and inserts R first, and the redo gives (R), quote((), (a)), quote(<left>(a)). On root lines (), (a), (), a backward delete of the first two lines, then a peer update that types Z in the line left and inserts R first, undoes to (), (a), (R), (Z) where the plain insert puts R first. The check generates neither, because its quotes hold one letter each and its lines are never empty. A peer update that adds R and wraps the quote in an outer keeps R on undo only when it is written as operations and does not also align the quote, and the check generates no peer wrap. After another peer deletes a block, step 6 checks only that each step replays, so an undo that also deletes text that survived passes. The P0 suggested task for these classes owns them.
--peer-letters at its gate bound fails 656 times, and its baseline lists each: 332 history-letters, 135 history-group, 49 history-lines, 70 history-throws and 70 history-failed. --peer-letters --yjs finds 1,541: 491 history-letters, 542 history-group, 375 history-lines, 66 history-throws, 66 history-failed and 1 history-converge. 416 identities fail in both, 240 only as a skipped update and 1,125 only over Yjs. The classes, with counts grouped by shape from the skipped-update run:
- An undo of a paste inside a quote loses the peer's
r or R. quote(paragraph("a")) quote(paragraph("b")) :: paste two lines at b|1 ; remote type r at x|1 undoes to quote(a), quote(b). About 68, the paste-in-quote class of --peer-inserts.
- An undo or redo of a selection delete or type-over removes the peer's
r typed inside the range or at its edge. quote(paragraph("a")) paragraph("b") :: typeOver a..b ; undo ; remote type r at a|0 redoes to quote(X). About 60.
- An undo of a type-over leaves the user's
X after the peer moves or unwraps a line. paragraph("a") quote(paragraph("b") paragraph("c")) :: typeOver a..b ; remote move 0 to 1.1 undoes to a, quote(b, c, X). About 28.
- A redo of a delete keeps a letter the peer moved out of the range.
paragraph("a") quote(paragraph("b")) :: select a|0..b|1 Backspace ; undo ; remote move 0 to 1.1 redoes to an empty line and a. About 120. A redo that copies a letter, as after select a|1..b|0 Backspace ; undo ; remote move 1 to 0 on two quotes, and a redo of an Enter that deletes a after the peer moved it out of its quote, as in quote(paragraph("a")) :: Enter at a|0 ; undo ; remote move 0.0 to 0, are in the same family.
- Over Yjs only, an undo or redo puts letters in the wrong line where the skipped update puts them right.
quote(paragraph("a") paragraph("b")) :: paste two lines at a|0 ; remote move 0.0 to 0.3 undoes to quote(xab, X) over Yjs and to quote(a, b) as a skipped update. A Yjs import maps the history through other changes than the skipped update does.
- A redo of a block removal deletes the
r the peer typed into that block. paragraph("a") paragraph("b") :: remove block 0 ; undo ; remote type r at a|0. About 16. The law keeps r, and Yjs's own UndoManager would delete it. Nobody has decided which is right, so they stay in the baseline.
- An undo joins lines the user never joined.
paragraph("a") paragraph("b") :: Enter at b|0 ; remote Delete at a|1 undoes to ab, and after quote(paragraph("a") paragraph("b")) :: paste two lines at b|1 ; remote move 0.0 to 0.1 the undo joins the wrong line. A redo of a join that fills the peer's empty line instead, as after quote(paragraph("a")) paragraph("b") :: Delete at a|1 ; undo ; remote Enter at a|1, may be right.
- The peer's command drops a step. After
paragraph("a") quote(paragraph("b")) :: type X at b|1 ; remote Backspace at b|0 no undo step is left. Over Yjs a peer's move drops one too, as after paragraph("a") :: Enter at a|0 ; remote move 0 to 1. When the peer repeats an undone Enter or splits a line the user joined, as in paragraph("a") :: Enter at a|0 ; undo ; remote Enter at a|0, the redo goes, and whether it should is undecided.
- History keeps a step that nests a quote or puts text in a quote.
quote(paragraph("a") paragraph("b")) :: unwrap a ; remote wrap a throws in History.toJSON, and the undo fails.
- Over Yjs, the second editor keeps an empty line after a redo.
quote(paragraph("a") paragraph("b")) paragraph("c") :: select a|1..c|1 Backspace ; remote insert R at 1, the one history-converge finding.
The gone-letter law also finds 345 history-undo cases at the default gate bound, such as paragraph("a") :: wrap a ; paste two lines at a|0 ; remote delete block 0.1. It runs only under the flag, so the default gate keeps its baseline.
What a pass proves
Bugs planted on this tree showed where the gate holds:
- Mutant rec-undo-h2, where
addMapping in packages/plitejs/src/history/history-state.ts drops the mapping a history-skip commit queues, gives 3,964 history-throws, 16 history-failed, 94 history-caret and 3 history-stack findings. Undo after another peer appends a paragraph applies a change sized for the document without it.
- Mutant rec-undo-h5, where
writeHistory and replaceHistoryHead no longer clear the redos, gives 244 history-stack findings, such as align a ; undo ; align a leaving a redo.
- A mutant where
history-plugin.ts ignores the history-merge tag gives 1,262 history-group findings, each an explicit merge that starts its own step.
- With
history-plugin.ts back to ending the typing group on every commit it does not save, an announcement-only commit included, the gate gives 4 history-group findings, such as paragraph("a") :: type X at a|1 ; announce ; type Z at X|1. It also compares 17,998 cases, because the idle and new-batch ways run only where the plain way joined.
Planted bugs and reverted fixes show where the letter-command gates hold:
- Mutant rec-undo-h2 gives 589
history-letters, 434 history-lines, 18,529 history-failed and 18,553 history-throws findings in each of them. paragraph("a") :: paste two lines at a|0 ; remote move 0 to 1 then undoes to no a.
- With this change's scripts copied in,
--peer-letters fails at the parent of four history fixes and passes those cases at the fix. At c3f9077683's parent, 8 history-letters findings, such as paragraph("a") quote(paragraph("b")) :: Delete at a|1 ; remote wrap a, which undoes to no b. At 9e86aa451f's parent, 16 history-group findings with and without --yjs, such as paragraph("a") quote(paragraph("b") paragraph("c")) :: Backspace at b|0 ; remote insert R at 1.0, which leaves no undo step. At b52e002bb3's parent, 23 history-group findings and 1 history-letters finding, such as paragraph("a") quote(paragraph("b")) :: remove block 0 ; remote move 0.0 to 0. At 8d0516b055's parent, 40 history-throws and 40 history-failed findings, such as paragraph("a") quote(paragraph("b")) :: Enter at a|0 ; undo ; remote move 0 to 1, whose redo puts the selection outside the root. Each fix's own repro is not among these lists. 9e86aa451f's takes two peer updates, b52e002bb3's takes one peer update that adds a line and also types or aligns, and 8d0516b055's takes two user edits before the peer's move, one more than the gate bound. Whether --blocks=1 --edits=2 finds it is unproven, because a run at a load average near 150 was stopped unfinished after 47 minutes.
- It finds nothing that a7bd418e20, 79b0d3b2a1 or 4714093a3a fixed. a7bd418e20's bug needs a link. 79b0d3b2a1's needs the peer to wrap a line while the user removes its block, before either edit reaches the other, and 4714093a3a's needs an empty quote and a concurrent join. In this check, the peer always edits the user's latest document. t_862034d0's bug needs the user to paste whole paragraphs, which no letter command does.
The gate does not catch t_9c9c168f's bug. It runs one top-level block, which has no block to remove, so only --blocks=2 finds it, and only in its last-block form, because another peer's paragraph is always appended at the end. The unit tests in packages/plitejs/test/history/peer-block-undo.test.ts pin the middle and nested forms.
The gate does not catch t_d3072b82's bug, which 128eb410c9 fixed, and neither do the deep runs or the yjs-peer-order check: with deletesWholeSiblings in packages/plitejs/src/history/root-change.ts back to its one-node test, every count equals the clean run's. In one editor, another peer's deletion of the edit
…(truncated)
1---2description: Check that every undo gives back the document from before its step and every redo the one after, over every list of up to three short commands on every small document, under the grouping, skipping and restoration rules of Plite history. Use before and after changing packages/plitejs/src/history or another file in its coverage record, when the undo-redo-history step of pnpm check fails, or when fixing an undo or redo bug a seed or a tester found.3argument-hint: '[check | letters | deep | lower | case <identity> | update]'4name: undo-redo-history5metadata:6 skiller:7 source: https://skillmd.com/skills/udecode/undo-redo-history8 upstream: .agents/rules/undo-redo-history.mdc9---1011# Undo and redo history1213Handle $ARGUMENTS. `check`, or no argument, runs the gate bound. `letters` runs `--peer-letters` and `--peer-letters --yjs` at their gate bound. `deep` runs `--size=5`, `--blocks=2 --size=5` and `--peer-inserts` and compares their failures with Findings. `lower` runs `--lower` after a fix removes a known finding. `case <identity>` runs `--case=<identity>` to print one case's full detail. `update` brings the verification in line with changed code, as Coverage record says.1415**The property.** A user runs a short list of edits, undos, redos and one change from another peer. Each undo gives back the document and caret from before its step, and each redo the document and caret after it, and neither drops nor copies another peer's paragraph that its step never touched. `docs/vision/plite.md` says transaction history controls grouping, skipping and restoration, and that ordinary input groups by its idle clock. The laws hold the history plugin to how it groups: an edit under `history: 'merge'` joins the step before it, an edit after the idle window or under `history: 'new-batch'` starts a step, typing right after the letter the plain way or the idle way typed before joins, and a change under `history: 'skip'`, such as another peer's, adds none. A new step clears the redos, and `hasUndo` and `hasRedo` match the steps left. A history saved with `History.toJSON` and restored into a fresh editor replays the same documents. With `--peer-letters`, the other peer runs any of the user's letter commands, and after each undo and redo each letter shows as often as that command and the user's steps still done leave it.1617**The harm.** An undo that gives back another document loses the user's work, and one that loses a step leaves an edit the user cannot take back. Three history fixes landed before this check: 94cf545da2, ab4b6576ee and t_d3072b82's fix in 128eb410c9. A seed or a tester found each one, one case at a time.1819## What it checks2021`packages/plitejs/scripts/undo-redo-history.ts` runs the real history plugin on every list up to a bound. It checks the TypeScript code itself, not a model of it.22231. **Documents.** Every document of up to `--blocks` top-level blocks, each a paragraph or a quote of paragraphs, from the `shapes` builder in `packages/plitejs/scripts/authored-documents.ts`. Each line holds one letter. The schema adds an `align` property to paragraphs and quotes. With `--peer-inserts` it adds a `box`, which holds paragraphs and sits only in a quote, and an `outer`, which holds paragraphs and quotes and sits only at the root.242. **Edits.** The steps and commands of `packages/plitejs/scripts/letter-commands.ts`, which the recording check also runs: typing, Enter, Backspace and Delete at every caret beside a letter, a backward selection delete between every two carets, paste of two lines, `wrap` and `unwrap` at every letter. The check adds an `align` change at every letter and, as a list's first move on a document of two or more top-level blocks, the removal of each one. With `--peer-inserts` it adds a `lift` of every paragraph in a quote, a wrap of a quote's line in a box and of a root block in an outer, each written as operations and as a value diff of the whole document. The first edit in a list types `X`, the second `Y` and the third `Z`, so a doubled or lost letter shows.253. **Modes.** Once the history holds a step, each edit runs four ways: right after the move before it, after the idle window, under `history: 'merge'` and under `history: 'new-batch'`. The script fakes `performance.now()` to step the clock. The idle way runs only where the plain way joined the step before, because elsewhere it records what the plain way records. The new-batch way runs there too, and wherever another edit can follow it, because the plugin stops typing from joining a step that new-batch started.264. **Moves.** A list holds up to `--depth` moves and up to `--edits` edits, and no more moves than `--size` less the document's letters. Undo and redo run where a step can replay. In the default gate, right after a letter typed the plain or the idle way while another edit can still follow, a move may be an `announce`, a bare update that emits a screen-reader announcement and changes nothing else. The edit after it never runs under merge, because an explicit merge ignores the idle clock and would only repeat the lists without the announcement. That skip is a deliberate coverage limit. With merge kept, the gate compared 18,150 cases and found 20 more `history-caret` findings, each a baseline entry with the announcement inserted, so the gate misses a plugin in which an announcement changes what an explicit merge does. One move per list may be another peer's change, under `history: 'skip'` with the collaboration tag: a paragraph `R` appended to the document, the deletion of one block or two sibling blocks at any level that leaves each parent a block, or a move of a top-level block or a quote's line to another top-level place or quote line. A move comes last in its list, alone, with the deletion of a top-level block beside the moved one, or with an `R` typed in a paragraph the move crosses, so one update holds a move among other edits. With `--peer-inserts`, that change is never a move, which the default gate covers, and may also insert `R` at every block boundary, at the root and inside each quote, box and outer, or `box(R)` inside a quote. Where it follows exactly one edit and no edit follows it, the same update may also type `r` into the block beside the insert, delete that block's last letter or set the quote's `align`, written as operations or as a value diff.2728 With `--peer-letters`, the other peer's change is one letter command instead: typing `r`, Enter, Backspace and Delete at every caret beside a letter, `wrap` and `unwrap` at every letter, setting a letter's block to `align: 'left'`, inserting a paragraph `R` at every block boundary at the root and inside each quote, or a move of a top-level block or a quote's line. It runs once the history holds a step, and only undos and redos follow it. Without `--yjs` it is a skipped update in the user's editor, as above. With `--yjs`, the user's editor and a second editor each bind their own Yjs document through the `yjs` plugin. The user's list reaches the second editor, which runs the command as a plain update, and its Yjs update comes back to the user's editor as a remote import.295. **Laws.** After each move the script compares what the history did with a model of the steps. A case records its first failure per rule:30 - `history-undo`: an undo that does not give back the document from before its step, or that changes how many copies of another peer's paragraph `R` the document holds when its step never changed that count.31 - `history-redo`: the same for a redo and the document after its step.32 - `history-caret`: an undo or redo whose selection is not the one from before or after its step.33 - `history-failed`: an undo or redo that reports `failed` or `pending`, throws, or reports through the lifecycle error sink.34 - `history-group`: an edit that joins the step before it after the idle window or under new-batch, an edit under merge that starts a step of its own while no redo waits and no other peer has changed the document, typing that starts a step of its own right after the letter typed before, even with an `announce` between them, or another peer's appended paragraph that changes the number of undo steps.35 - `history-stack`: `hasUndo` or `hasRedo` that differs from the steps left, a changed document with no step, an edit that leaves a redo, or an `announce` that changes the undo or redo steps.36 - `history-restore`: a history restored from its JSON whose undos and redos give other documents than the editor it came from. It compares whole documents, another peer's paragraph included.37 - `history-throws`: an edit, another peer's change or `History.fromJSON` that throws an error other than a refusal.38 - `history-lines`: after another peer's move, an undo or redo that does not give back the lines of the document from before or after its step, less the lines of each block that peer deleted, or one that leaves a different number of the letters that peer typed, or a move that changes the number of undo or redo steps. A deleted line that a step changed takes away only a line that shares one of its letters, and a step may go with a deleted block only when its before and after differ only in blocks that share a letter with that block, or in empty lines when the block was an empty line. Lines compare as a set with counts and ignore `R` and empty lines, so a line put back in the wrong place, a lost empty line, or a surviving line lost beside a related deleted one still passes.3940 With `--peer-inserts`, two more laws file under `history-undo` and `history-redo`: a letter another peer deleted, in a block or by a text delete, never comes back, and once it deleted something, an undo or redo keeps every paragraph, letter and alignment it wrote. An alignment it set is exempt when the step adds or removes a quote. A peer update that also types, deletes a letter or aligns, or that arrives as a value diff, must place `R` where the same list with only the plain insert written as operations places it, and the script replays that list in its own editor to compare them after every move. Carets compare in the document with the peer's paragraph, letter and alignment stripped, no caret law holds after a peer's value diff, because applying one can match a user's line to the peer's paragraph, and a caret that misses its law does not stop the list.4142 With `--peer-letters`, only a list that ends in the other peer's command is judged, and these laws judge its unwinding instead of `history-undo`, `history-redo` and the `history-lines` law above:43 - `history-letters`: an undo or redo after which a letter shows a number of times other than the count before the peer's command, plus what that command added or removed, plus what each step still done added or removed, floored at zero. When the peer's command leaves fewer lines, a letter that an undone step deleted may stay deleted, because the line it would land in may be gone.44 - `history-lines`: when the peer's command leaves the lines as they were, less the letters it removed, an undo or redo that does not give the lines from before or after its step. The law sets aside the letters a step deleted beside a line the peer removed.45 - `history-group`: a peer's command that changes the number of undo or redo steps, unless it removed something and each dropped step changed only letters the peer removed or letters in lines the peer removed.46 - `history-retrace`: a redo that does not give the document the undo before it started from.47 - `history-converge`: with `--yjs`, after the unwinding reaches the second editor, a document there other than the user's. When the second editor does not admit an update, the finding files under `history-failed`.486. **Unwinding.** At the end of each list the script undoes every step and redoes every step through the same laws, then saves the history as JSON, restores it into a fresh editor and unwinds that the same way. After another peer deletes a block, the model no longer knows which documents the steps give, so the script only checks that each undo and redo replays and that the restored history gives the same documents. After another peer's move, each undo and redo goes through the `history-lines` law instead of `history-undo` and `history-redo`. When the move also deleted a block and exactly the steps that may go with it went, the rest still go through that law; when other steps went, unwinding only checks that each step replays and keeps the letters that peer typed. A move after an earlier deletion gets only those replay checks too.497. A refusal the editor gives on purpose counts in the summary's `refused` and is not a failure.508. It compares the findings with `packages/plitejs/scripts/undo-redo-history-baseline.json` through `tooling/scripts/finding-baseline.mjs`. Every finding is filed under `packages/plitejs/src/history`. An identity names the document and the moves, never the result.5152## Run it5354Run every command from the repository root.5556- `pnpm check:undo-redo-history` checks the gate bound: one block, three moves, two edits, size four. It compares 18,017 cases on 3 documents in 60 s to 135 s, depending on the machine's load. A `--depth` above 3 exits 2, because each move types its own letter. It prints one JSON summary line, then each finding the baseline does not list. `pnpm check undo-redo-history` runs the same step through the check runner, and `pnpm check --affected` runs it for a change under `packages/plitejs/src/`, to the script, `check-harness.ts`, `letter-commands.ts` or `authored-documents.ts`, to a file under `packages/plitejs/test/support/`, to `config/plite-source-aliases.ts` or to `tooling/scripts/finding-baseline.mjs`.57- `--blocks`, `--depth`, `--edits` and `--size` raise one part of the bound. `--size=5` compares 67,456 cases in 222 s to 337 s, and `--blocks=2 --size=5` compares 387,347 cases on 12 documents in 367 s to 1,006 s, depending on the machine's load. A bound other than the gate's or `--case` skips the baseline and exits 1 on any failure.58- `pnpm check:undo-redo-history --peer-inserts` runs the widened check at its own gate bound: one block, three moves, two edits, size five, because undoing a lift and redoing it after another peer's change takes three moves on `quote(paragraph("a") paragraph("b"))`. It compares 119,206 cases on 3 documents in about 4 minutes at a load average near 45 and 23 minutes near 250, against its own baseline, `packages/plitejs/scripts/undo-redo-history-peer-inserts-baseline.json`, and its own case count in `GATES`. `--lower` and `--init` with the flag write that baseline. `pnpm check` does not run it.59- `pnpm check:undo-redo-history --peer-letters` runs the letter-command check at its own gate bound: two blocks, three moves, one edit, size five. It compares 21,738 cases on 12 documents in 23 s to 64 s at a load average near 40 and in 144 s near 150, against `packages/plitejs/scripts/undo-redo-history-peer-letters-baseline.json`. `--peer-letters --yjs` compares 21,333 cases in 72 s to 100 s near 45 and in 41 minutes near 150, against `packages/plitejs/scripts/undo-redo-history-yjs-letters-baseline.json`, and prints about 18,000 Yjs `Invalid access` warnings. The Yjs run compares 405 fewer cases because more of the peer's commands drop a step over Yjs, and a list whose peer command fails a law stops there, as in the other gates. `--lower` and `--init` with the flags write each baseline. `pnpm check` runs neither.60- `--empty-quotes` lets a quote hold no line and adds an edit that splits each top-level quote before its first line or after its last, which leaves an empty quote. At the gate bound it compares 21,141 cases and reads no baseline. The gate runs without it, because the schema the gate shares with the recording check requires a line in every quote.61- `--case='<identity or part of one>'` prints the full stack of each failure whose identity contains it, such as `--size=5 '--case=Enter at b|0 ; undo ; remote delete block 0.1'`.62- `--jobs=<n>` sets the number of worker processes, the machine's cores up to 16 by default.63- History follows a skipped change by the facts each commit records, and five flags check that path. `--digest` prints the count and the SHA-256 of every commit's public change JSON. `--facts` runs `checkFacts` on every commit and counts a commit as faulted when its facts are wrong, or as unexplained when it holds unknown facts that no raw change or paste entered. `--admitted` prints each list whose skipped change the verdict admits. `--exact` plays each list of the mode's frozen admitted file two ways: once reading the history right after the skipped update, and once reading only the stack depths. After each press it compares the whole document with a tree of labelled blocks and letters that the script updates on its own. `--trace` prints one digest per mode of every press's document and effects on the lists outside that file. The default mode's file is `packages/plitejs/scripts/undo-redo-history-admitted.txt`, with 216 lists, and the `--peer-letters` mode's is `packages/plitejs/scripts/undo-redo-history-peer-letters-admitted.txt`, with 1,160. The gate fails when the verdict admits a list that its mode's file does not hold, which it prints as `UNFROZEN`, and when its mode's baseline lists one that the file holds, which it prints as `BASELINED`.64- Exit 0 is a pass and exit 1 a finding. Exit 2 is a bad flag or a crashed worker. At the gate the run also fails when it compares other than 18,017 cases, the `cases` of its entry in the `GATES` list, so a change that stops edits from applying cannot shrink the gate while the baseline still matches. Update that count only with the reason it moved.65- After a fix removes a known finding, the gate fails until you run `pnpm check:undo-redo-history --lower`. The baseline never grows. `--init` seeds only a rule the baseline lacks, and `--lower` and `--init` write nothing when the case count misses its pin in `GATES`.6667## Read a failure6869A finding prints its rule, identity and the first line of its detail:7071```text72history-caret paragraph("a") :: Enter at a|1 ; Delete at a|1 [merge]73 undo selects {"anchor":{"offset":1,"path":[0,0]},...}, not {...}7475history-stack paragraph("a") :: wrap a ; unwrap a [merge]76 after undo, hasUndo is false and hasRedo false, with 0 undo and 1 redo steps left77```7879Both are known findings. In the first, Delete takes back the Enter it merges into, and the undo of the empty step moves only the caret. Run `--case` on the identity for the full diff and stack.8081Tell a real bug from a modeling error before you change code:8283- **Real bug.** A user can reach the list, and an undo, redo or restore gives what the law says it must not. Fix the history plugin, then run `--lower`.84- **Modeling error.** The law's oracle is ambiguous for that list, such as which side of another peer's paragraph an undone block lands on. Fix the script with the reason.85- **Intended change.** A change to how history groups steps changes the law too. Change the law and `docs/vision/plite.md` together. Never weaken a law or add a baseline entry to get green.8687## Findings8889The gate finds 1,172: 70 `history-caret`, 25 `history-group` and 2 `history-stack`, all under `history: 'merge'`, and 1,075 `history-lines`. The first three classes have one cause:9091- **An explicit merge whose edit takes back its step's change keeps an empty step.** `Enter at a|1 ; Delete at a|1 [merge]` leaves the document as it was and one undo step. Its undo moves only the caret, a later appended paragraph from another peer drops the step, and after `wrap a ; unwrap a [merge]` the undo leaves no redo. 70 `history-caret`, 25 `history-group` and 2 `history-stack` entries.9293The 1,075 `history-lines` entries follow another peer's move. Before history followed facts, next failed 1,096 such lists: these 1,075, 20 that the default mode's frozen file holds and history now gets right, and `quote(paragraph("a")) :: Enter at a|1 ; unwrap a ; remote move 1 to 2 and delete block 0`, which the structural merge by position fixes. By what the law found on those 1,096, 242 undo to the wrong lines, 103 undo to the wrong lines less the deleted ones, 275 change the number of steps, 472 drop a letter the peer typed, 2 redo to the wrong lines and 2 never reach a step the law expects:9495- **An undo after another peer's move keeps text its step added.** `paragraph("a") :: Enter at a|1 ; paste two lines at a|1 [merge] ; remote move 1 to 0` undoes to the lines `y` and `a`, not `a`. Most come after a paste or an Enter.96- **An undo after another peer moves a line out of its quote loses letters.** `quote(paragraph("a") paragraph("b")) :: Backspace at b|0 ; remote move 0.0 to 0` undoes to the line `a`, and `b` is gone; `paragraph("a") :: wrap a ; align a ; remote move 0.0 to 1` undoes to no line.97- **Another peer's move changes the number of undo or redo steps.** `paragraph("a") :: Enter at a|0 ; Enter at a|0 ; remote move 2 to 0 and type R in block 1` leaves one undo step of two.98- **An undo after another peer moves a block and deletes another gives lines other than the snapshot's less the deleted ones.** `paragraph("a") :: paste two lines at a|0 ; paste two lines at a|0 ; remote move 1 to 3 and delete block 2` undoes to `X` and `x`, not `X` and `xa`.99- **A redo after another peer's move drops the line it should restore.** `quote(paragraph("a")) :: Enter at a|0 ; undo ; remote move 0.0 to 0` redoes to no line.100- **An undo after another peer's move drops a letter that peer typed.** `paragraph("a") :: align a ; Enter at a|1 ; remote move 0 to 2 and type R in block 1` types `R` in the line the Enter made, and the undo removes that line with it.101- **Another peer's move that deletes a line a paste or an Enter made drops the whole step.** `paragraph("a") :: paste two lines at a|0 ; paste two lines at a|0 ; remote move 0 to 2 and delete block 1` leaves one undo step of two, although the first paste also changed `a`.102- **An undo after another peer deletes a pasted line loses the line it changed.** `paragraph("a") :: paste two lines at a|0 ; Backspace at a|1 ; remote move 0 to 2 and delete block 1` undoes to no line, not `a`.103104`--size=5` finds 3,065 failures and `--blocks=2 --size=5` 56,652. The deep runs read no baseline. Before the fix for t_9c9c168f, the two-block run also gave 37 `history-group` findings where undo dropped a removed paragraph beside another peer's appended one. 33 start by removing a block, such as `paragraph("a") paragraph("b") :: remove block 1 ; align a ; remote append R`, and 4 are merged steps whose paste and selection delete remove one, such as `paste two lines at b|0 ; select X|0..b|1 Backspace [merge] ; remote append R`. Most hold an explicit merge. These classes have none:105106- **An undo, redo or restore beside an empty quote throws.** With `--empty-quotes` at the gate bound, 57 `history-throws` cases fail that the baseline does not list, with 433 `history-lines` cases, and 41 `history-lines` entries of the baseline do not appear. 40 of the throws and 14 of the `history-lines` cases come from the schema alone, where another peer moves a quote's only line out and leaves the quote empty: `quote(paragraph("a")) :: Enter at a|0 ; undo ; remote move 0.0 to 0` puts the selection outside the root. 16 throws follow a split that one step merges with an Enter or a paste. That step goes invalid, so the undo and `History.toJSON` both throw `Editor element "quote" at [0] cannot contain "text"`, or at `[1]`, and the run reports it from the restore in `History.fromJSON`, as the restored-history class below does, such as `quote(paragraph("a")) :: Enter at a|0 ; split quote 0 at 2 [merge] ; remote move 0.0 to 0 and delete block 1`. The last, `quote(paragraph("a")) :: split quote 0 at 1 ; unwrap a [merge] ; remote move 1 to 0`, throws `Unbalanced JSON token slice` in `carryThroughMove` when the other peer's move arrives. Each of the 419 `history-lines` cases with a split follows another peer's move, as the baseline's 1,095 entries do. Before t_b628752f's fix, rebasing the split's undo read the first child of the empty quote as a text and crashed a worker.107108- **An undo or redo throws after another peer moves a line inside a quote.** `quote(paragraph("a") paragraph("b")) :: Enter at a|0 ; Backspace at b|0 ; remote move 0.1 to 0` throws, with errors such as `A text node cannot contain another node` and a selection outside the root. At `--size=5`, 145 `history-throws` cases have no explicit merge, and 60 more do.109- **Another peer's appended paragraph drops an undo step.** On `paragraph("a") paragraph("b")`, `wrap a ; Backspace at b|0 ; remote append R` leaves one undo step of two. 215 `history-group` cases.110- **A restored history refuses a step after another peer deletes a block.** `quote(paragraph("a") paragraph("b")) :: Enter at b|0 ; undo ; remote delete block 0.1` keeps a redo of the split, and `History.fromJSON` throws `Editor element "quote" at [0] cannot contain "text"` when it validates it. 11 `history-throws` cases.111- **An undo puts the caret in another peer's appended paragraph.** `paragraph("a") paragraph("b") :: Enter at a|0 ; select a|0..b|1 Backspace ; remote append R` undoes to a caret at `[2,0]`, not `[1,0]`. 36 `history-caret` cases.112113`--peer-inserts` at its gate bound fails 10,942 cases, which its baseline lists: 9,432 `history-undo`, 443 `history-group`, 326 `history-redo`, 286 `history-stack`, 226 `history-caret`, 217 `history-throws` and 12 `history-failed`. Next fails each of them under the same check. The class counts below were taken at 10,308 findings, before `typeOver` joined the edits, which added 706 findings whose list holds a type-over, and before upstream fixes dropped 74, so they no longer sum to the total. Each finding falls in one class:114115- **An undo of a split brings back the letters of a line another peer deleted.** `paragraph("a") :: Enter at a|0 ; wrap a [merge] ; remote delete block 1`. 3,191 cases, all from the law that a deleted letter never comes back.116- **An undo of a paste inside a quote loses another peer's paragraph between the pasted lines.** `quote(paragraph("a")) :: paste two lines at a|1 ; remote insert R at 0.1` undoes to `quote(paragraph("aR"))`. Inside a quote the paste records its change as a replacement of a text's close token, not as a plain insert, so the undo does not read as a join of two lines. 2,343 cases, 2,303 of them in a quote.117- **An undo of a box or outer merges another peer's paragraph inside it into the user's line.** `quote(paragraph("a") paragraph("b")) :: box b by value ; remote insert R at 0.1.0` undoes to `quote(paragraph("a") paragraph("bR"))`. 1,801 cases with the wrap written as a value diff, and 130 more where an operations wrap shares its step with a split or a join.118- **The explicit merge class above.** 732 cases.119- **An undo or redo of a move brings back a block another peer deleted**, when that block was the moved one or its old container. `quote(paragraph("a") paragraph("b")) :: lift b by value ; undo ; remote delete block 0.1` redoes with `b` back, and `quote(paragraph("a") paragraph("b")) :: lift a ; lift b ; remote delete block 1` undoes with `b` back. 576 cases.120- **A peer update that also types, deletes a letter, aligns, boxes its paragraph or arrives as a value diff loses the paragraph or letter on undo.** `quote(paragraph("a")) :: Enter at a|1 ; remote insert R at 0.1 and type r at the end of 0.1`. 305 cases after splits, 150 after lifts, boxes, outers and wraps, and 66 after joins, deletes and typing.121- **An undo or redo brings back the letter another peer deleted in the update that inserted its paragraph.** `paragraph("a") :: wrap a ; remote insert R at 0 and delete the last letter of 0.0 by value`. 220 cases.122- **An undo, redo or restored history moves another peer's box into a parent that refuses it**, and the replay or `History.fromJSON` throws. `quote(paragraph("a") paragraph("b")) :: box b ; undo ; remote insert box(R) at 0.2 and type r at the end of 0.1 by value` throws `box at [0,1] cannot contain box`. 214 cases.123- **An undo of an Enter loses another peer's paragraph beside lines its step or the step before it split.** `paragraph("a") :: Enter at a|0 ; Enter at a|1 [merge] ; remote insert R at 2`. 163 cases.124- **A peer update that also types or deletes a letter places its paragraph elsewhere than a plain insert.** On a redo of a box or outer, `R` moves into the new container, as in `quote(paragraph("a") paragraph("b")) :: outer a by value ; undo ; remote insert R at 1 and type r at the end of 0.1`, which redoes to `outer(quote(a, br), R)` where the plain insert gives `outer(quote(a, br)), R` (30). A redo of a lift puts `R` on the other side of the quote's edge (2). An undo of a lift or outer places `R` unlike the plain insert, where the compound result can be the right one (24). A redo of an Enter puts the new line after `R` once the peer deleted the split line's letter (10). An undo of a join drops the empty line it splits back out (11). A peer update written as a value diff places `R` elsewhere than the plain insert after a box, outer, lift, unwrap, Enter, join or typing (109 on undo, 80 on redo). In 109 of them the same update written as operations passes every law: the diff reuses the user's tokens for `R`, so the user's letter or line close becomes the peer's. Six of the undo findings, which next fails too, follow a lift written as a value diff: after `quote(paragraph("a") paragraph("b")) :: lift a by value`, a peer update that inserts `R` or `box(R)` at `1.0` and types or deletes a letter there undoes to `quote(a, R, b)` with the letter change, as a lift written as operations does, while the plain insert undoes to `quote(R, a, b)`.125- **Another peer's inserted paragraph drops an undo step**, like the appended paragraph above. 66 cases.126- **A peer update that types where an undone split was drops the redo.** `paragraph("a") :: Enter at a|1 ; undo ; remote insert R at 0 and type r at the end of 0`. 37 cases. Whether the redo should survive a conflicting edit is a call nobody has made yet.127- **Smaller classes.** An explicit merge right after a lift starts its own step (16). An undo of an outer drops the user's empty line (5). A peer insert inside an outer changes the number of undo steps (2). A redo of a lift written as a value diff merges the peer's paragraph into the lifted line, as in `quote(paragraph("a") paragraph("b")) :: lift b by value ; undo ; remote insert R at 0.2` (3). A stored step nests a paragraph in a paragraph after a peer value diff (7). The restored-history refusal above (8). A peer value diff typing into the step's line reorders the user's letters (7).128129The flag cannot see a loss that leaves the document looking like the one before the step once the peer's letter is stripped, and it does not generate a peer update that inserts an empty paragraph. Two such cases fail on this tree and before it. After `quote(paragraph("a") paragraph("")) :: Enter at a|1`, a peer types `z` at the new line's start, and undo gives `quote((a), (z))`, not `quote((az), ())`, at the root too. After `paragraph("a") :: Enter at a|1`, a peer types `z` at the new line's start and inserts an empty paragraph after it, and undo gives `(a), (z)`. When the peer edits inside the run the undo replaces, a root run is still found by value, so after `quote(paragraph("a")) quote(paragraph("a") paragraph("")) :: Enter at the first a|1`, a peer's `z` in the new line, then undo, gives `quote((a), (z)), quote((a))` and drops the second quote's empty line. A peer update that aligns the new line and inserts `R` beside it loses `R` on undo, and the check aligns only quotes. A peer update whose deleted letter empties the stacks leaves the placement law nothing to compare, and where the list with only the plain insert breaks a law itself, the placement law skips the list, so only the count of `R` checks it. When the peer aligns or types in the line and inserts `R`, a redo of a line break in one of two identical quotes splits the other quote, as before this change. After `quote(paragraph("a")) quote(paragraph("a")) :: Enter at the second a|0 ; undo`, the peer aligns the second quote's line and inserts `R` first, and the redo gives `(R), quote((), (a)), quote(<left>(a))`. On root lines `(), (a), ()`, a backward delete of the first two lines, then a peer update that types `Z` in the line left and inserts `R` first, undoes to `(), (a), (R), (Z)` where the plain insert puts `R` first. The check generates neither, because its quotes hold one letter each and its lines are never empty. A peer update that adds `R` and wraps the quote in an outer keeps `R` on undo only when it is written as operations and does not also align the quote, and the check generates no peer wrap. After another peer deletes a block, step 6 checks only that each step replays, so an undo that also deletes text that survived passes. The P0 suggested task for these classes owns them.130131`--peer-letters` at its gate bound fails 656 times, and its baseline lists each: 332 `history-letters`, 135 `history-group`, 49 `history-lines`, 70 `history-throws` and 70 `history-failed`. `--peer-letters --yjs` finds 1,541: 491 `history-letters`, 542 `history-group`, 375 `history-lines`, 66 `history-throws`, 66 `history-failed` and 1 `history-converge`. 416 identities fail in both, 240 only as a skipped update and 1,125 only over Yjs. The classes, with counts grouped by shape from the skipped-update run:132133- **An undo of a paste inside a quote loses the peer's `r` or `R`.** `quote(paragraph("a")) quote(paragraph("b")) :: paste two lines at b|1 ; remote type r at x|1` undoes to `quote(a), quote(b)`. About 68, the paste-in-quote class of `--peer-inserts`.134- **An undo or redo of a selection delete or type-over removes the peer's `r` typed inside the range or at its edge.** `quote(paragraph("a")) paragraph("b") :: typeOver a..b ; undo ; remote type r at a|0` redoes to `quote(X)`. About 60.135- **An undo of a type-over leaves the user's `X` after the peer moves or unwraps a line.** `paragraph("a") quote(paragraph("b") paragraph("c")) :: typeOver a..b ; remote move 0 to 1.1` undoes to `a, quote(b, c, X)`. About 28.136- **A redo of a delete keeps a letter the peer moved out of the range.** `paragraph("a") quote(paragraph("b")) :: select a|0..b|1 Backspace ; undo ; remote move 0 to 1.1` redoes to an empty line and `a`. About 120. A redo that copies a letter, as after `select a|1..b|0 Backspace ; undo ; remote move 1 to 0` on two quotes, and a redo of an Enter that deletes `a` after the peer moved it out of its quote, as in `quote(paragraph("a")) :: Enter at a|0 ; undo ; remote move 0.0 to 0`, are in the same family.137- **Over Yjs only, an undo or redo puts letters in the wrong line where the skipped update puts them right.** `quote(paragraph("a") paragraph("b")) :: paste two lines at a|0 ; remote move 0.0 to 0.3` undoes to `quote(xab, X)` over Yjs and to `quote(a, b)` as a skipped update. A Yjs import maps the history through other changes than the skipped update does.138- **A redo of a block removal deletes the `r` the peer typed into that block.** `paragraph("a") paragraph("b") :: remove block 0 ; undo ; remote type r at a|0`. About 16. The law keeps `r`, and Yjs's own UndoManager would delete it. Nobody has decided which is right, so they stay in the baseline.139- **An undo joins lines the user never joined.** `paragraph("a") paragraph("b") :: Enter at b|0 ; remote Delete at a|1` undoes to `ab`, and after `quote(paragraph("a") paragraph("b")) :: paste two lines at b|1 ; remote move 0.0 to 0.1` the undo joins the wrong line. A redo of a join that fills the peer's empty line instead, as after `quote(paragraph("a")) paragraph("b") :: Delete at a|1 ; undo ; remote Enter at a|1`, may be right.140- **The peer's command drops a step.** After `paragraph("a") quote(paragraph("b")) :: type X at b|1 ; remote Backspace at b|0` no undo step is left. Over Yjs a peer's move drops one too, as after `paragraph("a") :: Enter at a|0 ; remote move 0 to 1`. When the peer repeats an undone Enter or splits a line the user joined, as in `paragraph("a") :: Enter at a|0 ; undo ; remote Enter at a|0`, the redo goes, and whether it should is undecided.141- **History keeps a step that nests a quote or puts text in a quote.** `quote(paragraph("a") paragraph("b")) :: unwrap a ; remote wrap a` throws in `History.toJSON`, and the undo fails.142- **Over Yjs, the second editor keeps an empty line after a redo.** `quote(paragraph("a") paragraph("b")) paragraph("c") :: select a|1..c|1 Backspace ; remote insert R at 1`, the one `history-converge` finding.143144The gone-letter law also finds 345 `history-undo` cases at the default gate bound, such as `paragraph("a") :: wrap a ; paste two lines at a|0 ; remote delete block 0.1`. It runs only under the flag, so the default gate keeps its baseline.145146## What a pass proves147148Bugs planted on this tree showed where the gate holds:149150- Mutant rec-undo-h2, where `addMapping` in `packages/plitejs/src/history/history-state.ts` drops the mapping a history-skip commit queues, gives 3,964 `history-throws`, 16 `history-failed`, 94 `history-caret` and 3 `history-stack` findings. Undo after another peer appends a paragraph applies a change sized for the document without it.151- Mutant rec-undo-h5, where `writeHistory` and `replaceHistoryHead` no longer clear the redos, gives 244 `history-stack` findings, such as `align a ; undo ; align a` leaving a redo.152- A mutant where `history-plugin.ts` ignores the `history-merge` tag gives 1,262 `history-group` findings, each an explicit merge that starts its own step.153- With `history-plugin.ts` back to ending the typing group on every commit it does not save, an announcement-only commit included, the gate gives 4 `history-group` findings, such as `paragraph("a") :: type X at a|1 ; announce ; type Z at X|1`. It also compares 17,998 cases, because the idle and new-batch ways run only where the plain way joined.154155Planted bugs and reverted fixes show where the letter-command gates hold:156157- Mutant rec-undo-h2 gives 589 `history-letters`, 434 `history-lines`, 18,529 `history-failed` and 18,553 `history-throws` findings in each of them. `paragraph("a") :: paste two lines at a|0 ; remote move 0 to 1` then undoes to no `a`.158- With this change's scripts copied in, `--peer-letters` fails at the parent of four history fixes and passes those cases at the fix. At c3f9077683's parent, 8 `history-letters` findings, such as `paragraph("a") quote(paragraph("b")) :: Delete at a|1 ; remote wrap a`, which undoes to no `b`. At 9e86aa451f's parent, 16 `history-group` findings with and without `--yjs`, such as `paragraph("a") quote(paragraph("b") paragraph("c")) :: Backspace at b|0 ; remote insert R at 1.0`, which leaves no undo step. At b52e002bb3's parent, 23 `history-group` findings and 1 `history-letters` finding, such as `paragraph("a") quote(paragraph("b")) :: remove block 0 ; remote move 0.0 to 0`. At 8d0516b055's parent, 40 `history-throws` and 40 `history-failed` findings, such as `paragraph("a") quote(paragraph("b")) :: Enter at a|0 ; undo ; remote move 0 to 1`, whose redo puts the selection outside the root. Each fix's own repro is not among these lists. 9e86aa451f's takes two peer updates, b52e002bb3's takes one peer update that adds a line and also types or aligns, and 8d0516b055's takes two user edits before the peer's move, one more than the gate bound. Whether `--blocks=1 --edits=2` finds it is unproven, because a run at a load average near 150 was stopped unfinished after 47 minutes.159- It finds nothing that a7bd418e20, 79b0d3b2a1 or 4714093a3a fixed. a7bd418e20's bug needs a link. 79b0d3b2a1's needs the peer to wrap a line while the user removes its block, before either edit reaches the other, and 4714093a3a's needs an empty quote and a concurrent join. In this check, the peer always edits the user's latest document. t_862034d0's bug needs the user to paste whole paragraphs, which no letter command does.160161The gate does not catch t_9c9c168f's bug. It runs one top-level block, which has no block to remove, so only `--blocks=2` finds it, and only in its last-block form, because another peer's paragraph is always appended at the end. The unit tests in `packages/plitejs/test/history/peer-block-undo.test.ts` pin the middle and nested forms.162163The gate does not catch t_d3072b82's bug, which 128eb410c9 fixed, and neither do the deep runs or the `yjs-peer-order` check: with `deletesWholeSiblings` in `packages/plitejs/src/history/root-change.ts` back to its one-node test, every count equals the clean run's. In one editor, another peer's deletion of the edit164165…(truncated)