Repository navigation
[lexical][lexical-y] Bug Fix: Blocks and containers past a call's argument limit: no content spread into a call, and a main-thread size gate - #9883
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
|
AI review status for this pull request.
|
86e0713 to
048db84
Compare
048db84 to
37bb501
Compare
## Description #9880 sized a layout 1 document from the runtime's argument limit and migrated it through 1to2 and 2to3. This extends it to the layout 4 floor: the existing cells now run the whole chain to layout 4, and new cells take layout 3 documents of more than max(limit, 130,000) units in one container (one long line, and lines of 99 characters), with and without flat NodeState over them (rewritten, and skipped by step 0), through 3to4, the layout 4 reader and the writer (an edit at the end of the last line), gc off and on. An audit of content-sized spreads in the binding and the frozen steps finds one left, the writer's `$indexHolders` (`stack.push(...node.getChildren())`), which #9883 already changes to `pushAll`; the 3to4 step has none. ## Changeset None: tests. ## Test plan ### Before #9880's cells, which stop at layout 3, below the new floor (the 3to4 cells did not exist): ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks × lines of 99 characters, over 131072 units: migrates to layout 3 and reads the same (gc off) × one line of 131072 characters: migrates to layout 3 and reads the same (gc on) × in its read: a YMigrationError whose cause is the error ... YMigrationError: @lexical/y: no chain of steps from layout 1 to 4: no step from layout 3 Tests 6 failed | 2 passed (8) ``` ### After ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks ✓ a layout 1 root container with more units than a call takes as arguments (6) ✓ one line of 131072 characters: migrates to layout 4 and reads the same (gc on) ✓ 3to4 on a layout 3 root container with more units than a call takes (8) ✓ lines of 99 characters, over 131072 units, flat state: migrates to layout 4, reads the same, and edits (gc off) ✓ one line of 131072 characters, flat state: migrates to layout 4, reads the same, and edits (gc on) ... Tests 16 passed (16) ``` In the default pool (a worker, whose limit is 524,288) the same 16 size cells pass on documents of 524,288 units and more.
There was a problem hiding this comment.
Thanks, this looks good. $appendAll keeps the existing single append(...nodes) call for up to 8,192 nodes, and past that each chunk goes through ElementNode.splice one node at a time, so sibling links, sizes and the selection steps that follow match what the old spread produced. The new LargeBlock.test.ts cases fail with RangeError on main and pass here, and they assert the resulting blocks' child counts and text. The @lexical/y pushAll/spliceAll rewrites store the same items in the same order.
A few optional, non-blocking suggestions:
- A reproducer for the
@lexical/ypart.LargeContainer.test.tspasses onmainas well (as the description says), so for@lexical/yonly the source scan tells the two apart.packages/lexical-y/AGENTS.mdasks every bug fix to land with a regression test insrc/__tests__/unit/regressions/. One test that reaches a rewritten call past the limit (for example the holder index ininternal/Write.ts:4107, which walks the children of an inline element in a line) would back the changeset's[fix]. Otherwise the changeset could describe these edits as preventive. - Seed assertion (
packages/lexical-y/src/__tests__/size/LargeContainer.test.ts:255-262): "headless seed reads back the same" only checksblocks > 0and a total over 130,000. Comparing the read summary with the shape's own summary would catch a seed that drops content. - Document the new tier. The Testing Strategy section of
AGENTS.mdlists unit, browser and E2E tests. A bullet for__tests__/size/would help later contributors: what goes there, that it runs in theforkspool, and thatpnpm run test-unitincludes it. - CI time and watch mode.
LargeContainer.test.tsis one file of 20 serial tests (266 s on Node 22). It ran last in the Node 22 unit job, whosepnpm run test-unitstep grew from 799-932 s onmainto 1,245 s here. Splitting it by shape, so the parts run in parallel, or runningunit-sizeas its own CI job would recover most of that. Separately, withunit-sizeintest-unit-watch(package.json:50), every edit tolexicalor@lexical/yre-runs the file; keeping it intest-unitonly would keep the watch loop fast. - Changeset wording (
.changeset/lexical-large-block-append.md): "more than about 130,000 children" is V8's main-thread limit, but JavaScriptCore (Safari) already throws past 65,536 arguments. Naming the range per engine, or saying "tens of thousands", would help readers match their crash. TheVerify:steps work as written.
37bb501 to
af8514e
Compare
There was a problem hiding this comment.
Thanks, this looks good. $appendAll keeps the existing single append(...nodes) call for up to 8,192 nodes. Past that it appends chunks through the class's own append, and ElementNode.splice handles each chunk one node at a time, so the resulting siblings, sizes and the selection steps that follow match what the old spread produced. The new LargeBlock.test.ts cases throw RangeError on main and pass here, and they assert each block's child count and text. SpreadArguments.test.ts flags exactly the eight @lexical/y spreads this change replaces. The earlier suggestions are all addressed: the seed assertion, the AGENTS.md tier, keeping size tests out of watch mode, the JavaScriptCore range, and [chore] for the preventive @lexical/y part.
A few non-blocking suggestions, the first being the one I would do before merging:
- Keep the size tier from competing with the unit suite. CI's
core-tests / unit (22.x)job failed on this head. An unrelated test,BoundaryNormalization.test.ts› "inline nested between texts", hit the 5,000 ms default timeout at 6,036 ms; it takes about 2.5 s onmain. That runner was slow throughout, and in both CI runs the size files started last, right beside the slowest jsdom suites. Addingsequence: {groupOrder: 1}to theunit-sizeproject invitest.config.mts(supported by the installed Vitest) makes theunitandscripts-unitprojects finish before the size files start. Runningunit-sizeas its own CI step would also work. - The V8 threshold in the changesets (
.changeset/lexical-large-block-append.md:5, and the same number in.changeset/lexical-y-large-container-spreads.md:5andAGENTS.md:370). 131,072 is the smallest power of two thatargumentLimit()finds failing, not where the error starts. On Node 24, a spread intopushon the main thread fails past about 125,000 items at the top of the stack and about 100,000 items 2,000 frames deep, and calls inside an editor update run deeper.CONTRIBUTING.mdasks changeset entries to be written for someone who uses Lexical, so something like "past roughly 100,000 to 125,000 on V8's main thread" would help readers match their crash. - A source scan for core.
packages/lexical/srcnow has only bounded call spreads left (class names, listener and command arguments, keyboard shortcut matches,getComposedRanges.apply,registerEventListeners). Nothing keeps it that way, asSpreadArguments.test.tsdoes for@lexical/y. Pointing that scan (or a copy) atpackages/lexical/srcwith those allowances would. - A limit check in the migration size files.
describeMigration(packages/lexical-y/src/__tests__/utils/sizeShapes.ts:311) has noLIMIT <= 131_072case, unlikedescribeShape. So the twoMigrateLargeContainer.*files do not check that they run on a main thread, as the description says each file does. The same one-line test would cover them. - A follow-up idea, outside this pull request. Other packages still spread content lists into calls. Two examples:
currentLexicalNode.append(...childLexicalNodes)inpackages/lexical-html/src/index.ts:601, which an HTML paste of one paragraph with that many<br>lines reaches, andnode.append(...nextChildren.slice(nextIndex))inpackages/lexical-yjs/src/SyncV2.ts:357. Since$appendAllis internal tolexical, a follow-up would need to export it or add a package-local helper.
…ument limit: no content spread into a call, and a main-thread size gate ## Description The number of arguments a call can take is bounded by the stack it runs on: Node's main thread throws `RangeError: Maximum call stack size exceeded` past 131,072, a Vitest worker thread only past 524,288. #9880 fixed one such call (`perUnit.push(...strings)` in the migration runner), which production hit on large layout-1 documents while the unit tests, running in worker threads, did not. This change looks for the rest and adds a gate that runs at production's limit. **`lexical`: four selection operations threw on a block with more children than that.** A paragraph pasted from a text with 70,000 line feeds holds 140,000 children (a text node and a line break per line). `RangeSelection.insertParagraph` after its first child, `$insertNodes` at its end (merging an inserted paragraph's children in) or inside it (joining its tail to the last inserted block), and `$splitNode` each moved the children with `append(...children)` and threw. They now append through `$appendAll` (internal): one `append(...nodes)` call as before for up to 8,192 nodes (none included: an empty `append()` still marks the parent writable), and chunks of 8,192 through the class's own `append` past that, so overrides still apply. Two more spreads of block children in `LexicalSelection.ts` (`$extractInlineFromBlocks`) and `LexicalStructure.ts` (structure repair) become loops. The remaining spreads in `packages/lexical/src` are bounded: class names, listener and command arguments, keyboard shortcut matches. **`@lexical/y`: eight call spreads of lists that can grow with the document** (an inline element's children in a line the writer indexes, the runs of text an edit spans, a split's parts, a run's ends, queued updates and warnings) now use the package's existing `pushAll` / `spliceAll` (internal/Arrays.ts). A new source test (`SpreadArguments.test.ts`) parses every source file of the package and fails on any call that passes a list as its arguments (a spread into a call or `new`, `fn.apply(thisArg, list)`, `fn['apply'](…)`, `Reflect.apply(…)`, `Reflect.construct(…)`) unless it is listed with the reason its list is bounded: the three chunked calls (at most `ARGUMENTS_CHUNK` or 4,096 items), event argument tuples, forwarded parameters, and `spliceAll`'s small branch. An alias of `Function.prototype.apply` or of `Reflect` is not recognized. **A main-thread size gate.** A new Vitest project, `unit-size` (part of `pnpm run test-unit`, after the unit projects (`sequence.groupOrder`) so it never competes with their 5 s timeouts, and not part of the watch script), runs `packages/**/__tests__/size/**` in the `forks` pool, so each file runs on a child process's main thread with production's argument limit (roughly 100,000 to 125,000 arguments on Node, depending on stack depth); each file checks that limit. The `@lexical/y` files (one case each, so they run in parallel; shared code in `__tests__/utils/sizeShapes.ts`) build documents past both 130,000 units and that limit in one container (one line of 135,168 characters; 45,056 lines of 2 characters; 135,168 lines; one line of 67,584 texts and line breaks; one line holding a link of as many) and take each through the binding's first write, a fresh binding's load, an edit each way, the headless read, seed (compared with the shape built in a plain editor) and edit, with Yjs garbage collection off and on, and migrate the first two shapes from layout 1. `LargeBlock.test.ts` runs the four selection operations above on a block of 135,168 children. AGENTS.md's Testing Strategy describes the new tier. The `@lexical/y` changes are preventive (its changeset is a `[chore]`): the size gate already passes on main for these shapes, since #9880 fixed the one path they reach, and it guards against a regression at production's limit. ## Test plan ### Before On main (e2836f0), with this change's tests and Vitest config: ``` $ pnpm exec vitest run --project unit-size packages/lexical/src/__tests__/size ✓ runs on a main thread × insertParagraph after its first child moves the rest into a new block × inserting it at the end of a paragraph merges its children in × inserting blocks inside it joins its tail to the last inserted block × $splitNode after its first child moves the rest into a copy ✓ its text and line breaks read back RangeError: Maximum call stack size exceeded (x4) Tests 4 failed | 2 passed (6) $ pnpm exec vitest run --project unit SpreadArguments × no list of content is spread into a call + "internal/Binding.ts:2218: ends.push(...endsOf(s))", + "internal/Binding.ts:3867: this.replies.push(...replies)", + "internal/Binding.ts:3895: updates.splice(at, 0, ...this.replies.splice(0))", + "internal/Binding.ts:3967: this.pendingWarnings.splice(0, Infinity, ...rest, ...refusals)", + "internal/Binding.ts:5152: this.replies.unshift(...editor._updates.splice(0))", + "internal/Read.ts:3141: pieces.splice(k, 1, ...parts)", + "internal/TextEdits.ts:1645: next.push(...toLeft([filled[i]], runs))", + "internal/Write.ts:4107: stack.push(...node.getChildren())", Tests 1 failed | 1 passed (2) $ pnpm exec vitest run --project unit-size packages/lexical-y Tests 17 passed (17) (the earlier single-file version; see above) ``` ### After ``` $ pnpm exec vitest run --project unit-size ✓ packages/lexical-y/src/__tests__/size/LargeContainer.LongLine.test.ts (4 tests) ✓ packages/lexical-y/src/__tests__/size/MigrateLargeContainer.LongLine.test.ts (2 tests) ✓ packages/lexical/src/__tests__/size/LargeBlock.test.ts (6 tests) ✓ packages/lexical-y/src/__tests__/size/LargeContainer.ShortLines.test.ts (4 tests) ✓ packages/lexical-y/src/__tests__/size/LargeContainer.InlineChildren.test.ts (4 tests) ✓ packages/lexical-y/src/__tests__/size/LargeContainer.LinkChildren.test.ts (4 tests) ✓ packages/lexical-y/src/__tests__/size/LargeContainer.ManyLines.test.ts (4 tests) ✓ packages/lexical-y/src/__tests__/size/MigrateLargeContainer.ShortLines.test.ts (2 tests) Test Files 8 passed (8) Tests 30 passed (30) (2:47 wall on a 16-core laptop) $ pnpm exec vitest run --project unit SpreadArguments Tests 2 passed (2) ```
af8514e to
c2dd841
Compare
## Description #9880 sized a layout 1 document from the runtime's argument limit and migrated it through 1to2 and 2to3. This extends it to the layout 4 floor: the existing cells now run the whole chain to layout 4, and new cells take layout 3 documents of more than max(limit, 130,000) units in one container (one long line, and lines of 99 characters), with and without flat NodeState over them (rewritten, and skipped by step 0), through 3to4, the layout 4 reader and the writer (an edit at the end of the last line), gc off and on. An audit of content-sized spreads in the binding and the frozen steps finds one left, the writer's `$indexHolders` (`stack.push(...node.getChildren())`), which #9883 already changes to `pushAll`; the 3to4 step has none. ## Changeset None: tests. ## Test plan ### Before #9880's cells, which stop at layout 3, below the new floor (the 3to4 cells did not exist): ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks × lines of 99 characters, over 131072 units: migrates to layout 3 and reads the same (gc off) × one line of 131072 characters: migrates to layout 3 and reads the same (gc on) × in its read: a YMigrationError whose cause is the error ... YMigrationError: @lexical/y: no chain of steps from layout 1 to 4: no step from layout 3 Tests 6 failed | 2 passed (8) ``` ### After ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks ✓ a layout 1 root container with more units than a call takes as arguments (6) ✓ one line of 131072 characters: migrates to layout 4 and reads the same (gc on) ✓ 3to4 on a layout 3 root container with more units than a call takes (8) ✓ lines of 99 characters, over 131072 units, flat state: migrates to layout 4, reads the same, and edits (gc off) ✓ one line of 131072 characters, flat state: migrates to layout 4, reads the same, and edits (gc on) ... Tests 16 passed (16) ``` In the default pool (a worker, whose limit is 524,288) the same 16 size cells pass on documents of 524,288 units and more.
There was a problem hiding this comment.
This looks good to me. The four lexical operations that moved a block's children with append(...children) (Enter via insertParagraph, both $insertNodes merges, and $splitNode) now go through $appendAll, which keeps the single call for up to 8,192 nodes and chunks past that through the class's own append, so overrides such as ListItemNode.append still apply node by node. LargeBlock.test.ts reproduces the RangeError on main in 4 of its 6 cases and checks the resulting blocks' children and text, and the same assertions at 20,000 children give identical results with one call and with chunks. The eight @lexical/y rewrites keep the order and in-place behaviour of the calls they replace, and the size files check both peers' documents.
Optional, non-blocking follow-up:
- The HTML version of the same content still throws.
$generateNodesFromDOMon a<p>holdingd<br>70,000 times (140,000 children) hits the sameRangeErroratpackages/lexical-html/src/index.ts:633(wrapper.append(...continuousInlines)), and line 601 spreads the same list. The defaulttext/htmlclipboard handler (packages/lexical-clipboard/src/ClipboardImportExtension.ts:228-233) catches the error and falls back to the next handler, so such a paste loses its formatting with a console error, and direct callers of$generateNodesFromDOMget the exception.ListItemNode.appendre-spreads a merged item's children too (packages/lexical-list/src/LexicalListItemNode.ts:360). A later pull request applying the same chunked append there, with a size test likeLargeBlock.test.ts, would cover the HTML paste path. The changeset describes this pull request's scope accurately as written.
## Description #9880 sized a layout 1 document from the runtime's argument limit and migrated it through 1to2 and 2to3. This extends it to the layout 4 floor: the existing cells now run the whole chain to layout 4, and new cells take layout 3 documents of more than max(limit, 130,000) units in one container (one long line, and lines of 99 characters), with and without flat NodeState over them (rewritten, and skipped by step 0), through 3to4, the layout 4 reader and the writer (an edit at the end of the last line), gc off and on. An audit of content-sized spreads in the binding and the frozen steps finds one left, the writer's `$indexHolders` (`stack.push(...node.getChildren())`), which #9883 already changes to `pushAll`; the 3to4 step has none. ## Changeset None: tests. ## Test plan ### Before #9880's cells, which stop at layout 3, below the new floor (the 3to4 cells did not exist): ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks × lines of 99 characters, over 131072 units: migrates to layout 3 and reads the same (gc off) × one line of 131072 characters: migrates to layout 3 and reads the same (gc on) × in its read: a YMigrationError whose cause is the error ... YMigrationError: @lexical/y: no chain of steps from layout 1 to 4: no step from layout 3 Tests 6 failed | 2 passed (8) ``` ### After ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks ✓ a layout 1 root container with more units than a call takes as arguments (6) ✓ one line of 131072 characters: migrates to layout 4 and reads the same (gc on) ✓ 3to4 on a layout 3 root container with more units than a call takes (8) ✓ lines of 99 characters, over 131072 units, flat state: migrates to layout 4, reads the same, and edits (gc off) ✓ one line of 131072 characters, flat state: migrates to layout 4, reads the same, and edits (gc on) ... Tests 16 passed (16) ``` In the default pool (a worker, whose limit is 524,288) the same 16 size cells pass on documents of 524,288 units and more.
With #9883's `unit-size` project (each file on a child process's main thread), its layout 1 migration cells now run the chain to layout 4, and `describeMigration3to4` adds two size files: a layout 3 document past the limit, as one long line and as short lines, with flat NodeState over it, through 3to4 on the main thread, gc off and on. An audit of content-sized spreads in the binding and the frozen steps finds one left, the writer's `$indexHolders` (`stack.push(...node.getChildren())`), which #9883 changed to `pushAll`; the 3to4 step has none. None: tests. ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks × lines of 99 characters, over 131072 units: migrates to layout 3 and reads the same (gc off) × one line of 131072 characters: migrates to layout 3 and reads the same (gc on) × in its read: a YMigrationError whose cause is the error ... YMigrationError: @lexical/y: no chain of steps from layout 1 to 4: no step from layout 3 Tests 6 failed | 2 passed (8) ``` ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks ✓ a layout 1 root container with more units than a call takes as arguments (6) ✓ one line of 131072 characters: migrates to layout 4 and reads the same (gc on) ✓ 3to4 on a layout 3 root container with more units than a call takes (8) ✓ lines of 99 characters, over 131072 units, flat state: migrates to layout 4, reads the same, and edits (gc off) ✓ one line of 131072 characters, flat state: migrates to layout 4, reads the same, and edits (gc on) ... Tests 16 passed (16) ``` In the default pool (a worker, whose limit is 524,288) the same 16 size cells pass on documents of 524,288 units and more. ``` $ pnpm exec vitest run --project unit-size packages/lexical-y/src/__tests__/size/MigrateLargeContainer ✓ |unit-size| .../size/MigrateLargeContainer3to4.LongLine.test.ts (3 tests) ✓ |unit-size| .../size/MigrateLargeContainer.LongLine.test.ts (3 tests) ✓ |unit-size| .../size/MigrateLargeContainer3to4.ShortLines.test.ts (3 tests) ✓ |unit-size| .../size/MigrateLargeContainer.ShortLines.test.ts (3 tests) Tests 12 passed (12) ```
## Description Yjs does not collect a deleted item whose `keep` flag is set. The package set and cleared that flag in two places that did not know of each other: the undo registry in `Binding.ts` (D88: the format items of the changes on the undo stacks, kept so an undo can read them after a collaborator deleted them) and `Hidden.ts` (the format items a held change deleted, which the line being composed goes on showing). This moves every write of `Item.keep` into `src/internal/Keeps.ts`: - `UndoKeeps` is one listening lifetime's keeps: `keep`, `release`, `releaseRekept` and `keepAgain`, with the three module-level maps the registry used. The closures in `YBinding#startInner` call it; when a release runs (queued, between transactions, on dispose) is unchanged and stays in the binding. - `keepShown` and `unkeepShown` are the hold's two writes. Each rule is as it was. The move makes one known gap a one-file question without closing it: an item the hold kept first is "kept already" to the undo registry, which then never releases it, and the hold's release clears the flag whether or not an undo step has come to rely on it since. `ARCHITECTURE.md` marks this step (step 2) done. This is stacked on the shown-items filter (`Shown.ts`) and its harness. The stack was rebased onto main 8111f33 (#9883) after these numbers were taken on e2836f0 (8 KeepsOwner cases then, 9 now, with the hand-off case); the byte-identity harness and the gates were run again on the rebased heads. ## Test plan `KeepsOwner.test.ts` pins the rules (an item and the items of the nodes it is in; two lifetimes; an item kept before any lifetime kept it; a keep set again after a stack is cleared; the hold's flag). Its guard reads `src/internal` with the TypeScript compiler (`utils/keepGuard.ts`, on the source reader of the filter's guard) and fails on a write of a Yjs item's `keep` outside `Keeps.ts`: an assignment, a compound assignment or a `delete`, through an alias of the item, an element access or a line break, and not in a comment, a string, a read, or a `keep` of another type (a second test feeds it a source with each of those). Node 24.21.0, `--pool=forks --maxWorkers=4`. ### Before On the commit before this one (the filter, its harness and the architecture document, on main `e2836f099`): ``` $ tsc -p tsconfig.json --noEmit # exit 0 $ eslint packages/lexical-y # exit 0 $ vitest --project unit --no-watch --pool=forks --maxWorkers=4 packages/lexical-y/ # gc off Test Files 5 failed | 442 passed | 4 skipped (451) Tests 6 failed | 23872 passed | 384 expected fail | 156 skipped (24418) $ LEXICAL_Y_GC=1 FUZZ_GC=1 vitest --project unit --no-watch --pool=forks --maxWorkers=4 packages/lexical-y/ Test Files 1 failed | 446 passed | 4 skipped (451) Tests 1 failed | 23877 passed | 384 expected fail | 156 skipped (24418) ``` The six gc-off failures are "Test timed out in 5000ms" under load and pass alone; the gc-on failure is `regressions/LoadAcrossGap.test.ts > a node-valued entry 10,000 levels deep, overwritten concurrently`, which fails on main in some runs and not in others (that commit's message has both controls). On main itself the same two runs end with 4 and 5 failures, eight of them load and the ninth that test. ### After ``` $ tsc -p tsconfig.json --noEmit # exit 0 $ eslint packages/lexical-y # exit 0 $ prettier --list-different packages/lexical-y # exit 0, nothing listed $ vitest --project unit --no-watch --pool=forks packages/lexical-y/src/__tests__/unit/KeepsOwner.test.ts Test Files 1 passed (1) Tests 9 passed (9) $ vitest --project unit --no-watch --pool=forks --maxWorkers=4 packages/lexical-y/ # gc off Test Files 448 passed | 4 skipped (452) Tests 23886 passed | 384 expected fail | 156 skipped (24426) $ LEXICAL_Y_GC=1 FUZZ_GC=1 vitest --project unit --no-watch --pool=forks --maxWorkers=4 packages/lexical-y/ Test Files 448 passed | 4 skipped (452) Tests 23886 passed | 384 expected fail | 156 skipped (24426) ``` No test fails in either run. Of the 24,346 tests that exist on main, every one passes or is skipped here as it is on main once main's load failures are run alone (they pass), except the `LoadAcrossGap` test above, which main fails with gc on and this run passed. The 18 added tests are `ShownFilter.test.ts` (10) and `KeepsOwner.test.ts` (8). Stored bytes, editor states and warnings, with the committed harness (`src/__tests__/trace/`), main `e2836f099` against this commit: ``` $ node compareByteTraces.mjs trace-base-gc0 trace-keeps-gc0 # gc off test files traced in both runs: 446; identical: 445; different: 1; different and allowed: 0 in the identical files: {"docs":209126,"finalBytes":187921645,"states":549394,"transactions":783014,"updateBytes":67957712,"updates":305763,"warnings":8780} different: packages/lexical-y/src/__tests__/unit/regressions/LoadAcrossGap.test.ts: finalHash one run only: packages/lexical-y/src/__tests__/unit/KeepsOwner.test.ts (after only) one run only: packages/lexical-y/src/__tests__/unit/ShownFilter.test.ts (after only) exit 1 $ node compareByteTraces.mjs trace-base-gc1 trace-keeps-gc1 # gc on test files traced in both runs: 446; identical: 445; different: 1; different and allowed: 0 in the identical files: {"docs":209126,"finalBytes":185985746,"states":549393,"transactions":783191,"updateBytes":67062002,"updates":305763,"warnings":8780} different: packages/lexical-y/src/__tests__/unit/regressions/LoadAcrossGap.test.ts: finalHash one run only: packages/lexical-y/src/__tests__/unit/KeepsOwner.test.ts (after only) one run only: packages/lexical-y/src/__tests__/unit/ShownFilter.test.ts (after only) exit 1 $ node compareByteTraces.mjs trace-base-gc0 trace-basectl-gc0 # main against a second run of main, LoadAcrossGap alone test files traced in both runs: 1; identical: 0; different: 1; different and allowed: 0 different: packages/lexical-y/src/__tests__/unit/regressions/LoadAcrossGap.test.ts: finalHash ``` `LoadAcrossGap.test.ts` differs in the hash of its final states only, and main differs from a second run of main in the same hash (the last command); its updates, state vectors, editor states and warnings are identical in every run. One worker failed to start in the gc-off run (`BoundaryNormalization.test.ts`); that file was traced again alone and is in the numbers above. LaneFuzz (`blockconv`, `carry`, `nohold`), gc off and on, traced the same way, main against this commit: ``` LaneFuzz, all three lanes, seeds 100001..103000 in 6 slices of 500 (18,000 histories per tree), gc off and on: tests, each slice main: 126 passed (126) branch: 126 passed (126) histories with a finding (all of known-open classes, none over its ceiling) main: 182 branch: 182 findings.jsonl identical in 6 of 6 slices byte traces identical in 6 of 6 slices: {"docs":120078,"finalBytes":161532192,"states":276203,"transactions":331262,"updateBytes":126405348,"updates":190076,"warnings":12278} no-hold lane alone, seeds 300001..305000 in 5 slices of 1,000 (10,000 histories per tree), gc off and on: tests, each slice main: 82 passed (82) branch: 82 passed (82) histories with a finding (all of known-open classes, none over its ceiling) main: 0 branch: 0 findings.jsonl identical in 5 of 5 slices byte traces identical in 5 of 5 slices: {"docs":50065,"finalBytes":67328636,"states":102011,"transactions":95701,"updateBytes":47329207,"updates":59860,"warnings":0} ``` Not run here: the maintainers' generated-history gate of record (its generator is not in the repository), browser and E2E tests.
## Description Yjs does not collect a deleted item whose `keep` flag is set. The package set and cleared that flag in two places that did not know of each other: the undo registry in `Binding.ts` (D88: the format items of the changes on the undo stacks, kept so an undo can read them after a collaborator deleted them) and `Hidden.ts` (the format items a held change deleted, which the line being composed goes on showing). This moves every write of `Item.keep` into `src/internal/Keeps.ts`: - `UndoKeeps` is one listening lifetime's keeps: `keep`, `release`, `releaseRekept` and `keepAgain`, with the three module-level maps the registry used. The closures in `YBinding#startInner` call it; when a release runs (queued, between transactions, on dispose) is unchanged and stays in the binding. - `keepShown` and `unkeepShown` are the hold's two writes. Each rule is as it was. The move makes one known gap a one-file question without closing it: an item the hold kept first is "kept already" to the undo registry, which then never releases it, and the hold's release clears the flag whether or not an undo step has come to rely on it since. `ARCHITECTURE.md` marks this step (step 2) done. This is stacked on the shown-items filter (`Shown.ts`) and its harness. The stack was rebased onto main 8111f33 (#9883) after these numbers were taken on e2836f0 (8 KeepsOwner cases then, 9 now, with the hand-off case); the byte-identity harness and the gates were run again on the rebased heads. ## Test plan `KeepsOwner.test.ts` pins the rules (an item and the items of the nodes it is in; two lifetimes; an item kept before any lifetime kept it; a keep set again after a stack is cleared; the hold's flag). Its guard reads `src/internal` with the TypeScript compiler (`utils/keepGuard.ts`, on the source reader of the filter's guard) and fails on a write of a Yjs item's `keep` outside `Keeps.ts`: an assignment, a compound assignment or a `delete`, through an alias of the item, an element access or a line break, and not in a comment, a string, a read, or a `keep` of another type (a second test feeds it a source with each of those). Node 24.21.0, `--pool=forks --maxWorkers=4`. ### Before On the commit before this one (the filter, its harness and the architecture document, on main `e2836f099`): ``` $ tsc -p tsconfig.json --noEmit # exit 0 $ eslint packages/lexical-y # exit 0 $ vitest --project unit --no-watch --pool=forks --maxWorkers=4 packages/lexical-y/ # gc off Test Files 5 failed | 442 passed | 4 skipped (451) Tests 6 failed | 23872 passed | 384 expected fail | 156 skipped (24418) $ LEXICAL_Y_GC=1 FUZZ_GC=1 vitest --project unit --no-watch --pool=forks --maxWorkers=4 packages/lexical-y/ Test Files 1 failed | 446 passed | 4 skipped (451) Tests 1 failed | 23877 passed | 384 expected fail | 156 skipped (24418) ``` The six gc-off failures are "Test timed out in 5000ms" under load and pass alone; the gc-on failure is `regressions/LoadAcrossGap.test.ts > a node-valued entry 10,000 levels deep, overwritten concurrently`, which fails on main in some runs and not in others (that commit's message has both controls). On main itself the same two runs end with 4 and 5 failures, eight of them load and the ninth that test. ### After ``` $ tsc -p tsconfig.json --noEmit # exit 0 $ eslint packages/lexical-y # exit 0 $ prettier --list-different packages/lexical-y # exit 0, nothing listed $ vitest --project unit --no-watch --pool=forks packages/lexical-y/src/__tests__/unit/KeepsOwner.test.ts Test Files 1 passed (1) Tests 9 passed (9) $ vitest --project unit --no-watch --pool=forks --maxWorkers=4 packages/lexical-y/ # gc off Test Files 448 passed | 4 skipped (452) Tests 23886 passed | 384 expected fail | 156 skipped (24426) $ LEXICAL_Y_GC=1 FUZZ_GC=1 vitest --project unit --no-watch --pool=forks --maxWorkers=4 packages/lexical-y/ Test Files 448 passed | 4 skipped (452) Tests 23886 passed | 384 expected fail | 156 skipped (24426) ``` No test fails in either run. Of the 24,346 tests that exist on main, every one passes or is skipped here as it is on main once main's load failures are run alone (they pass), except the `LoadAcrossGap` test above, which main fails with gc on and this run passed. The 18 added tests are `ShownFilter.test.ts` (10) and `KeepsOwner.test.ts` (8). Stored bytes, editor states and warnings, with the committed harness (`src/__tests__/trace/`), main `e2836f099` against this commit: ``` $ node compareByteTraces.mjs trace-base-gc0 trace-keeps-gc0 # gc off test files traced in both runs: 446; identical: 445; different: 1; different and allowed: 0 in the identical files: {"docs":209126,"finalBytes":187921645,"states":549394,"transactions":783014,"updateBytes":67957712,"updates":305763,"warnings":8780} different: packages/lexical-y/src/__tests__/unit/regressions/LoadAcrossGap.test.ts: finalHash one run only: packages/lexical-y/src/__tests__/unit/KeepsOwner.test.ts (after only) one run only: packages/lexical-y/src/__tests__/unit/ShownFilter.test.ts (after only) exit 1 $ node compareByteTraces.mjs trace-base-gc1 trace-keeps-gc1 # gc on test files traced in both runs: 446; identical: 445; different: 1; different and allowed: 0 in the identical files: {"docs":209126,"finalBytes":185985746,"states":549393,"transactions":783191,"updateBytes":67062002,"updates":305763,"warnings":8780} different: packages/lexical-y/src/__tests__/unit/regressions/LoadAcrossGap.test.ts: finalHash one run only: packages/lexical-y/src/__tests__/unit/KeepsOwner.test.ts (after only) one run only: packages/lexical-y/src/__tests__/unit/ShownFilter.test.ts (after only) exit 1 $ node compareByteTraces.mjs trace-base-gc0 trace-basectl-gc0 # main against a second run of main, LoadAcrossGap alone test files traced in both runs: 1; identical: 0; different: 1; different and allowed: 0 different: packages/lexical-y/src/__tests__/unit/regressions/LoadAcrossGap.test.ts: finalHash ``` `LoadAcrossGap.test.ts` differs in the hash of its final states only, and main differs from a second run of main in the same hash (the last command); its updates, state vectors, editor states and warnings are identical in every run. One worker failed to start in the gc-off run (`BoundaryNormalization.test.ts`); that file was traced again alone and is in the numbers above. LaneFuzz (`blockconv`, `carry`, `nohold`), gc off and on, traced the same way, main against this commit: ``` LaneFuzz, all three lanes, seeds 100001..103000 in 6 slices of 500 (18,000 histories per tree), gc off and on: tests, each slice main: 126 passed (126) branch: 126 passed (126) histories with a finding (all of known-open classes, none over its ceiling) main: 182 branch: 182 findings.jsonl identical in 6 of 6 slices byte traces identical in 6 of 6 slices: {"docs":120078,"finalBytes":161532192,"states":276203,"transactions":331262,"updateBytes":126405348,"updates":190076,"warnings":12278} no-hold lane alone, seeds 300001..305000 in 5 slices of 1,000 (10,000 histories per tree), gc off and on: tests, each slice main: 82 passed (82) branch: 82 passed (82) histories with a finding (all of known-open classes, none over its ceiling) main: 0 branch: 0 findings.jsonl identical in 5 of 5 slices byte traces identical in 5 of 5 slices: {"docs":50065,"finalBytes":67328636,"states":102011,"transactions":95701,"updateBytes":47329207,"updates":59860,"warnings":0} ``` Not run here: the maintainers' generated-history gate of record (its generator is not in the repository), browser and E2E tests.
With #9883's `unit-size` project (each file on a child process's main thread), its layout 1 migration cells now run the chain to layout 4, and `describeMigration3to4` adds two size files: a layout 3 document past the limit, as one long line and as short lines, with flat NodeState over it, through 3to4 on the main thread, gc off and on. An audit of content-sized spreads in the binding and the frozen steps finds one left, the writer's `$indexHolders` (`stack.push(...node.getChildren())`), which #9883 changed to `pushAll`; the 3to4 step has none. None: tests. ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks × lines of 99 characters, over 131072 units: migrates to layout 3 and reads the same (gc off) × one line of 131072 characters: migrates to layout 3 and reads the same (gc on) × in its read: a YMigrationError whose cause is the error ... YMigrationError: @lexical/y: no chain of steps from layout 1 to 4: no step from layout 3 Tests 6 failed | 2 passed (8) ``` ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/MigrateLargeContainer.test.ts --pool=forks ✓ a layout 1 root container with more units than a call takes as arguments (6) ✓ one line of 131072 characters: migrates to layout 4 and reads the same (gc on) ✓ 3to4 on a layout 3 root container with more units than a call takes (8) ✓ lines of 99 characters, over 131072 units, flat state: migrates to layout 4, reads the same, and edits (gc off) ✓ one line of 131072 characters, flat state: migrates to layout 4, reads the same, and edits (gc on) ... Tests 16 passed (16) ``` In the default pool (a worker, whose limit is 524,288) the same 16 size cells pass on documents of 524,288 units and more. ``` $ pnpm exec vitest run --project unit-size packages/lexical-y/src/__tests__/size/MigrateLargeContainer ✓ |unit-size| .../size/MigrateLargeContainer3to4.LongLine.test.ts (3 tests) ✓ |unit-size| .../size/MigrateLargeContainer.LongLine.test.ts (3 tests) ✓ |unit-size| .../size/MigrateLargeContainer3to4.ShortLines.test.ts (3 tests) ✓ |unit-size| .../size/MigrateLargeContainer.ShortLines.test.ts (3 tests) Tests 12 passed (12) ```
…stence and edits that did not sync (@lexical/y/offline) ## Description `@lexical/y` gives an app `freeze()` and `describeUnsyncedEdits` for a client whose room moved on, but nothing stores a Yjs 14 document on the device: `y-indexeddb` imports Yjs 13, and none keys by epoch or reports quota or damage. Edits that never reached a server live only in the tab. This adds `@lexical/y/offline`, an experimental entry with two layers: - **A local store** on IndexedDB alone, with no new dependency (`openYOfflineStore`). A slot per document key and epoch holds a base (what the server is known to hold) and the updates since. Writes are incremental and compacted. Two tabs on one key keep each other's rows: each writing handle holds a Web Lock (`navigator.locks` by default) and writes the whole document as its first row, so its later rows build only on its own. A handle replaces or removes only the rows it wrote, or rows it loaded from a writer that is gone (a closed tab, an earlier page load). Storage failures come back as typed results, never throws: `unavailable` (also an open that does not answer within `openTimeout`), `blocked`, `quota`, `corrupt` (with the readable rows as salvage), `closed`, `limit` and `failed`. Optional `limits` cap keys per group and bytes per key by refusing writes, never by evicting. `store.keys()` lists keys, so an app can clear by owner (another viewer at sign-in). - **An offline session** over any binding with `binding`, `status` and `freeze()` (`attachYOfflineSession`; a `YCollabBinding` is one, and an app with its own `YBinding` passes a `freeze()` over `quiesce()`). Stages are `'off'`, `'persist'` and `'replay'`. At `'persist'` every edit is stored as it is made (`persistEdits`, default true). `keep: 'unsynced'` stores only while the server lacks edits and deletes them on `markSynced()`. `writeDelay` waits for updates to pause. One path serves every return: - a reconnect at the same epoch syncs; - a replaced or upgraded room (a new epoch) freezes the client's copy, captures it into the same store, and describes it block by block; - so does a copy that a closed tab left at another epoch, or that never synced. `recoverYOfflineEdits` feeds an Offline edits panel and, at `'replay'`, reapplies the blocks nobody else touched as one undo step, at most once across tabs. It deletes a capture with nothing left only once the fresh document's `session` reports no unsynced edits, so a replay that has not synced keeps its capture. A replay or a read that fails comes back as that capture's typed error, never a rejection. The playground gets a "Collab Y Offline" mode with the panel and a "Simulate replaced" button, plus an e2e spec. y.md documents the layer. DECISIONS.md records D124 to D126. ## Backport This is #9899 on the pre-layout-4 pin (#9895: e2836f0 plus #9883, #9887, #9888 and the #9891 setup API), for apps still on layout 3. The source of the offline layer is the same as in #9899. The differences are: - the entry is registered without the `3to4` lines this base lacks (package.json `exports`, tsconfig paths, `.flowconfig`, `Source.test.ts`); - `OfflineLayout3.test.ts` is left out: it imports the `3to4` step. On this base the session tests already bind, edit, persist, capture and replay layout 3 documents, and "below the floor" means layout 2. ## Changeset `.changeset/lexical-y-offline.md` (`@lexical/y` patch, `[feature]`). ## Test plan ### Before ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/Offline No test files found, exiting with code 1 ``` ### After On this base, with gc off and on (`LEXICAL_Y_GC=1`): ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/Offline packages/lexical-y/src/__tests__/unit/regressions/OfflineReviewFindings.test.ts Tests 90 passed (90) $ pnpm exec vitest run --project unit packages/lexical-y Tests 24502 passed | 388 expected fail | 156 skipped (25046) $ pnpm exec vitest run --project unit-size Tests 32 passed (32) $ pnpm exec vitest run --project scripts-unit Tests 3677 passed (3677) $ pnpm exec vitest run --project unit packages/lexical-playground Tests 396 passed (396) $ pnpm run tsc && pnpm run flow Found 0 errors ``` The 90 tests break down as 30 store, 47 session, 4 fault and 9 regression tests. The browser tests and the collab Y e2e ran on #9899 (Chromium: browser 1099 passed, e2e 36/36). They were not rerun on this base.
Description
The number of arguments a call can take is bounded by the stack it runs on: Node's main thread throws
RangeError: Maximum call stack size exceededpast 131,072, a Vitest worker thread only past 524,288. #9880 fixed one such call (perUnit.push(...strings)in the migration runner), which production hit on large layout-1 documents while the unit tests, running in worker threads, did not. This change looks for the rest and adds a gate that runs at production's limit.lexical: four selection operations threw on a block with more children than that. A paragraph pasted from a text with 70,000 line feeds holds 140,000 children (a text node and a line break per line).RangeSelection.insertParagraphafter its first child,$insertNodesat its end (merging an inserted paragraph's children in) or inside it (joining its tail to the last inserted block), and$splitNodeeach moved the children withappend(...children)and threw. They now append through$appendAll(internal): oneappend(...nodes)call as before for up to 8,192 nodes (none included: an emptyappend()still marks the parent writable), and chunks of 8,192 through the class's ownappendpast that, so overrides still apply. Two more spreads of block children inLexicalSelection.ts($extractInlineFromBlocks) andLexicalStructure.ts(structure repair) become loops. The remaining spreads inpackages/lexical/srcare bounded: class names, listener and command arguments, keyboard shortcut matches.@lexical/y: eight call spreads of lists that can grow with the document (an inline element's children in a line the writer indexes, the runs of text an edit spans, a split's parts, a run's ends, queued updates and warnings) now use the package's existingpushAll/spliceAll(internal/Arrays.ts). A new source test (SpreadArguments.test.ts) parses every source file of the package and fails on any call that passes a list as its arguments (a spread into a call ornew,fn.apply(thisArg, list),fn['apply'](…),Reflect.apply(…),Reflect.construct(…)) unless it is listed with the reason its list is bounded: the three chunked calls (at mostARGUMENTS_CHUNKor 4,096 items), event argument tuples, forwarded parameters, andspliceAll's small branch. An alias ofFunction.prototype.applyor ofReflectis not recognized.A main-thread size gate. A new Vitest project,
unit-size(part ofpnpm run test-unit, after the unit projects (sequence.groupOrder) so it never competes with their 5 s timeouts, and not part of the watch script), runspackages/**/__tests__/size/**in theforkspool, so each file runs on a child process's main thread with production's argument limit (roughly 100,000 to 125,000 arguments on Node, depending on stack depth); each file checks that limit. The@lexical/yfiles (one case each, so they run in parallel; shared code in__tests__/utils/sizeShapes.ts) build documents past both 130,000 units and that limit in one container (one line of 135,168 characters; 45,056 lines of 2 characters; 135,168 lines; one line of 67,584 texts and line breaks; one line holding a link of as many) and take each through the binding's first write, a fresh binding's load, an edit each way, the headless read, seed (compared with the shape built in a plain editor) and edit, with Yjs garbage collection off and on, and migrate the first two shapes from layout 1.LargeBlock.test.tsruns the four selection operations above on a block of 135,168 children. AGENTS.md's Testing Strategy describes the new tier.The
@lexical/ychanges are preventive (its changeset is a[chore]): the size gate already passes on main for these shapes, since #9880 fixed the one path they reach, and it guards against a regression at production's limit.Test plan
Before
On main (e2836f0), with this change's tests and Vitest config:
After