← Back to Pull requests

[lexical-y][lexical-playground][lexical-website] Feature: Merge a block both sides changed with diff3 when unsynced edits are replayed #9914

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 14, 2026 20:43 UTC
Author

Description

describeUnsyncedEdits (@lexical/y/headless) and the replay on top of it
(replayUnsyncedEdits, recoverYOfflineEdits on @lexical/y/offline)
compare three documents block by block: the base, the user's and the current
one. A block only the user changed is replayed whole. A block both sides
changed was always a conflict, and went to the app's Offline edits panel
whole, even when one person fixed a word at the start of a paragraph and the
other added a sentence at its end.

Such a block is now merged three ways inside the block with diff3, the merge
version control systems use, on the Lexical JSON the report already reads (no
layout code, nothing new stored). It stays a conflict, exactly as before,
when the changes overlap or the merge cannot be shown to be safe.

  • internal/Diff3.ts: node-diff3 4.0.0's LCS, diffIndices and

  • diff3MergeRegions, vendored unmodified under its MIT license (the
    package takes no third-party runtime dependency; listed in
    .prettierignore so it stays byte-identical to the published source).
    4.0.0 is a day old, inside the 7 days minimumReleaseAge keeps new
    releases out of dependencies; it is the release whose rewritten LCS stays
    fast on repeated text (3.2.1's scans every candidate). Diff3.test.ts
    checks its LCS against a dynamic-programming LCS over 3,000 random pairs,
    and D133 records it.
  • internal/BlockMerge.ts keeps only the Lexical-specific parts around it:

  • - Tokens. A text block becomes tokens (a character with its node's
    format, style and NodeState, and the link or mark around it; one token
    for a line break, a tab, a decorator or a token-mode text node).
    - Pass 1, content. diff3 runs over words (a run of letters and digits;
    any other token alone), compared by content only: a change one side made
    is taken, the same change on both sides once, and changes to one word,
    or that overlap or touch, are a conflict. Inside a word one side changed,
    a second diffIndices over characters finds the letters it kept. In a
    script written without spaces (Chinese, Japanese, Thai), each character
    is its own unit.
    - Pass 2, properties. Each token both sides kept merges its properties
    three ways (format per bit, style, NodeState per key, link or mark).
    Two bits of one exclusive group (subscript and superscript; lowercase,
    uppercase and capitalize) that no side held together are a conflict.
    Typed text between two tokens the other side changed alike takes that
    change, and text one side removed that the other reformatted is a
    conflict.
    - Containers. Lists and tables merge by the same diff3 over their
    children's block ids (rows and cells, which have none, by the first block
    with an id inside them).
    - Checks. The merge is made again by word read from the end and by
    character from both ends, and every reading must make the same block;
    merging the user's block into the result again must change nothing.
    - Cost. A block of more than 10,000 tokens, or a side adding or
    removing more than 5,000, is not merged (unsupported): they bound the
    LCS, whose cost grows with how far two sequences differ.
  • YUnsyncedBlock gains resolution ('whole', 'merged', 'conflict'),

  • merged, conflict and conflictDetail. replayUnsyncedEdits writes a
    merged block over the block it was merged from, checked as a whole block
    is. blockMerge: false on the describe, replay and recover calls turns the
    merge off. All additive; the merge is on by default.
  • The playground's Offline edits panel offers Reapply for merged blocks.

  • DECISIONS.md: D133 (D126 amended). y.md: "A block both sides changed" in

  • the Offline section, and two limits.

    BlockMerge.ts is 1,623 lines (1,309 without comments) holding the types,
    the property merge, tokenizing and rebuilding runs, the container walk and
    the report. A custom Myers-based merge with its own alignment widening and
    closeness rules took 1,888 (1,557), plus a Myers module. The vendored diff3
    is 434 lines, license header included. Inline.ts is unchanged.

    Changeset

    .changeset/lexical-y-offline-block-merge.md ([feature], with Verify:).

    Test plan

    A seeded corpus (BlockMergeCorpus.test.ts, utils/blockMergeCorpus.ts) of
    2,800 pairs of concurrent edits to one block (paragraphs, a heading, bold and
    italic runs, links, line breaks, a list, a table, repeated letters) is checked
    against a model in which every character has an identity. Of the 2,769 blocks
    that are conflicts without the merge:

  • 1,630 merge, and 176 more are found to hold the user's edits already;

  • no merged block differs from what both sides' edits make;

  • none of the 188 cases whose edits' order matters is merged.
  • 300,000 more seeds: 172,410 of 297,399 merge, 0 wrong, 0 of 20,605
    order-dependent cases merged. BlockMergeReplay.test.ts runs 217 of the
    cases, and every node shape of the catalog, through stored documents and a
    bound editor.

    BlockMerge.test.ts has 81 rule cases, including these:

  • comment marks: added beside an edit, both sides over the same text, a

  • reply added while text is typed inside, two side by side stay two;
  • code blocks with highlight tokens, line breaks and tabs;

  • links side by side;

  • mentions, decorators, NodeState on text;

  • bold plus text typed beside it, after it and inside it (properties merge

  • in a second pass, not as edits);
  • text without spaces (Chinese);

  • the cost bound;

  • the cases an independent review found and this change fixes:

  • - two links or marks the other side made around typed text stay two;
    - NodeState merges key by key, and its keys are plain keys whatever
    their names (format, text, valueOf);
    - a __proto__ key in a NodeState value;
    - an accent in a text node of its own.

    Before

    On main, the new tests of OfflineSession.test.ts:

    $ vitest run --project unit packages/lexical-y/src/__tests__/unit/OfflineSession.test.ts -t "a block both sides changed"
         × persist stage: the report says which blocks are merged, whole and in conflict; nothing is applied
         × replay stage: the merge is applied with the whole blocks, as one undo step; the conflict stays in the panel
         × blockMerge: false: the block stays in the panel, as before the merge
         × a capture whose blocks were merged is deleted once the server holds them
         × Reapply a merged block; a block that changed since the merge was made is refused
    AssertionError: expected { applied: [ 'b3' ], …(1) } to deeply equal { applied: [ 'b1', 'b3' ], …(1) }
          Tests  5 failed | 51 skipped (56)
    

    After

    $ vitest run --project unit packages/lexical-y/src/__tests__/unit/OfflineSession.test.ts -t "a block both sides changed"
          Tests  5 passed | 51 skipped (56)
    
    $ vitest run --project unit packages/lexical-y        (LEXICAL_Y_GC=1: the same)
     Test Files  492 passed | 4 skipped (496)
          Tests  25957 passed | 412 expected fail | 169 skipped (26538)
    
    $ BLOCK_MERGE_CORPUS_FROM=<2800 + 50000k> BLOCK_MERGE_CORPUS_CASES=50000 vitest run ... BlockMergeCorpus.test.ts   (k = 0..5)
          Tests  4 passed (4)   x6: 172,410 of 297,399 merged, 0 violations, 0 of 20,605 order-dependent merged
    
    $ vitest run --project browser                          (Chromium)
     Test Files  84 passed (84)
          Tests  1104 passed | 2 skipped (1106)
    
    $ E2E_EDITOR_MODE=rich-text-with-collab-y playwright test --project=chromium Collaboration CollabYOffline CollabYNodes CollabYStructure CollabYSync
      36 passed (1.5m)
    

    The full unit, unit-size and scripts-unit projects and the browser
    run above were on the branch before its last review fixes, which touch only
    internal/BlockMerge.ts and its tests; the lexical-y suites were rerun
    after them. pnpm run flow, tsc, eslint, prettier --list-different . and
    pnpm run check-changesets pass. Firefox and WebKit were not run.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: potatowagon Review duration: 20661m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #9914"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%
    Repository