Skip to content

RTC: Add revision when CRDT content is replaced - #85876

Open
alecgeatches wants to merge 6 commits into
trunkfrom
add/rtc-recovery-points
Open

alecgeatches wants to merge 6 commits into
trunkfrom
add/rtc-recovery-points

Conversation

@alecgeatches

@alecgeatches alecgeatches commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What?

See #85792, which fixes an issue that could cause content loss when a post was saved without a CRDT document and another peer joins. Although that particular missing case has been fixed, it still shows a general RTC issue where a peer joining without a CRDT document can overwrite another user's live changes without providing either user a chance to recover the overwritten content:

autosave-test-trunk.mov

After user B joins the session and a round-trip of autosaves go through, the live document content added by user A after the first autosave is lost.

In this PR, we're adding a small backstop against content loss when a peer joins with an independent CRDT doc. Normally when two collaborative sessions build independent CRDT documents and then meet, Yjs keeps one document's content and deletes the other's. This PR stores the deleted content as a permanent revision of the post, so it can be restored from the Revisions panel. The outcome in the editor doesn't change, so new DB content without a CRDT doc still can overwrite changes in a live document, we just now save the replaced content beforehand.

Why?

A post without a persisted _crdt_document meta makes every session that opens it build its own new Yjs document from the database record. This can result in a live CRDT and joining user's CRDT share no history. When they join the same post, Yjs resolves concurrent map sets by client ID, which means the value from the higher client ID stays, and the other value is deleted together with everything nested inside it. For the blocks property, that is the whole post content in-editor.

Today, this can cause unsaved content loss. The deleted content is gone from the shared document, it is not in the losing peer's undo history, and the losing peer's next autosave overwrites their own autosave with the replaced content.

This is reproduced with the bug fixed in #85792, but the CRDT document could be missing for other reasons as mentioned above. Other triggers remain: a revision restore writes _crdt_document: null (#85509), an out-of-band save or a WP-CLI update, or deleting the meta as a repair step. This PR is a stopgap that prevents data loss for all of them without significant changes to the sync manager or save model.

How?

In short:

  1. Yjs reports when a top-level map replacement occurs with no origin (e.g. root.set('blocks', { ... })), which corresponds to a full replacement of content without a shared history. This calls into an optional onRemoteReplacement() callback.
  2. In the "losing" window (user A in the video above, or user B if their client ID is lower), before editRecord is called with the new content, the editor fires off an autosave request with a special crdt_recovery_point flag.
  3. The controller creates a revision for the specific content in the post before the content is overwritten.
  4. The editor continues as normal, replacing content with the winning map.

This allows either user to recover overwritten changes:

autosave-test-fix.mov

Known limits for this solution:

  • Recovery depends on the losing tab being available when the replacement arrives. If a user has a tab idled with unsaved changes and closes it, it will not have an opportunity to persist a safety revision.
  • The loser is not told that their content was saved in a revision. We can consider a UI notice, but as a first step having the content saved somewhere will at least make recovery possible.
  • No revision is written when the losing tab holds the same content as the database, because its content is already saved in the parent post. This allows users to "catch up" to live content without always creating a new revision.

Testing Instructions

See videos above. On trunk currently (commit 2481dec21c8bfdad3779585535527cb84094f16c), the reproduction steps for #85792 can be used to create an edited post without a saved CRDT document:

  1. Ensure real-time collaboration is enabled, and set AUTOSAVE_INTERVAL to 10 to speed up the initial autosave:

    /* .wp-env.override.json */
    {
        "config": {
            "AUTOSAVE_INTERVAL": 10
        }
    }
  2. As user A, create a draft post with a paragraph. Do not explicitly save, and wait for an /autosaves REST request.

  3. After the save goes through, add additional a second paragraph to the post and wait for an auto save. Again, do not explicitly save.

  4. As user B, in another browser or profile, open the same post.

  5. The two documents meet, and which one wins is a coin flip. In my local testing I forced user B's client ID to be the maximum value. When user A's second paragraph disappears, open the post's Revisions panel. A new revision holding user A's two paragraphs should exist, with the parent post and user A's autosave unchanged. Restore it to get the content back.

Use of AI Tools

Claude for the whole process.

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: alecgeatches <alecgeatches@git.wordpress.org>
Co-authored-by: chriszarate <czarate@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: +37 B (0%)

Total Size: 8.61 MB

📦 View Changed
Filename Size Change
build/scripts/core-data/index.min.js 77 kB +330 B (+0.43%)
build/scripts/editor/index.min.js 565 kB -293 B (-0.05%)

676c138 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 48b6c08 trunk % Change
timeToFirstByte 52.45 ms +9.72% -2.96% 53.25 ms +6.48% -6.85% -1.5%
largestContentfulPaint 90 ms +6.67% -6.67% 88 ms +4.55% -4.55% 2.27%
lcpMinusTtfb 35.95 ms +13.77% -7.23% 33.2 ms +25.45% -5.27% 8.28%
wpBeforeTemplate 27.58 ms +11.09% -5.33% 27.31 ms +13.91% -3.41% 0.99%
wpTemplate 21.14 ms +3.36% -3.88% 20.17 ms +7.29% -3.82% 4.81%
wpTotal 48.48 ms +9.32% -2.95% 49.36 ms +6.08% -7.17% -1.78%
wpMemoryUsage 7.72 MB +0% -0% 7.68 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 48b6c08 trunk % Change
timeToFirstByte 41.95 ms +8.22% -2.03% 41.9 ms +9.55% -3.22% 0.12%
largestContentfulPaint 92 ms +6.52% -0% 92 ms +6.52% -0% 0%
lcpMinusTtfb 51.45 ms +3.5% -2.62% 51 ms +2.45% -3.24% 0.88%
wpBeforeTemplate 22.66 ms +4.15% -1.32% 22.48 ms +5.29% -1.47% 0.8%
wpTemplate 15.71 ms +3.06% -2.61% 15.77 ms +6.02% -2.66% -0.38%
wpTotal 38.72 ms +8.08% -2.38% 38.62 ms +10.07% -2.82% 0.26%
wpMemoryUsage 6.41 MB +0% -0% 6.37 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 48b6c08 trunk % Change
mediaProcessingJpeg 379.06 ms +2.08% -0.65% 372.2 ms +2.75% -0.79% 1.84%
mediaProcessingAvif 5621.68 ms +1.87% -0.75% 5519.07 ms +0.96% -0.72% 1.86%
mediaProcessingJpegToAvif 3950.58 ms +0.78% -1.27% 3852.88 ms +0.92% -0.17% 2.54%

media-upload

Metric 48b6c08 trunk % Change
jpegUploadProcessing 1491.12 ms +32.47% -1.63% 1442.64 ms +0.96% -0.38% 3.36%
pngUploadProcessing 207.13 ms +15.16% -13.42% 231.79 ms +1.61% -1.98% -10.64%
largeJpegUploadProcessing 1473.94 ms +0.82% -1.25% 1436.93 ms +0.54% -0.48% 2.58%
multipleImageUploadProcessing 1600.49 ms +3.81% -0.38% 1988.8 ms +1.46% -21.27% -19.52%

post-editor

Metric 48b6c08 trunk % Change
serverResponse 414.77 ms +2.01% -1.99% 378.53 ms +7.98% -4.21% 9.57%
firstPaint 379.19 ms +10.79% -8.67% 352.18 ms +40.72% -8.76% 7.67%
domContentLoaded 853.66 ms +1.34% -1.63% 837.94 ms +3.18% -0.57% 1.88%
loaded 854.71 ms +1.35% -1.63% 838.96 ms +3.18% -0.56% 1.88%
firstContentfulPaint 501.03 ms +1.82% -1.08% 496.3 ms +0.69% -2.73% 0.95%
firstBlock 2122.66 ms +0.2% -0.67% 2125.55 ms +1.01% -1.67% -0.14%
type 18.22 ms +8.23% -5.16% 17.59 ms +6.03% -10.57% 3.58%
typeWithoutInspector 17.3 ms +4.28% -6.47% 17.43 ms +5.62% -7.11% -0.75%
typeWithTopToolbar 23.19 ms +2.41% -6.12% 22.52 ms +9.41% -5.86% 2.98%
typeContainer 8.31 ms +4.57% -4.57% 8.19 ms +1.47% -2.32% 1.47%
focus 119.79 ms +3.16% -4.26% 112.9 ms +12% -2.2% 6.1%
firstFocus 260.1 ms +0% -0% 243.52 ms +0% -0% 6.81%
selectAll 396.04 ms +1.65% -0.2% 376.27 ms +3.84% -1.74% 5.25%
undo 30.31 ms +2.84% -4.62% 29.75 ms +5.75% -4.67% 1.88%
listViewOpen 73.35 ms +10.09% -2.58% 73.47 ms +6.4% -9.54% -0.16%
inserterOpen 42.5 ms +11.06% -9.36% 39.7 ms +7.66% -6.42% 7.05%
inserterHover 11.41 ms +8.85% -8.33% 11.15 ms +4.39% -3.86% 2.33%
inserterSearch 8.82 ms +2.95% -8.28% 8.84 ms +14.14% -8.48% -0.23%
loadPatterns 620.31 ms +9.52% -3.77% 629.9 ms +5.24% -5.32% -1.52%
wpTotal 403.8 ms +1.73% -2.01% 367.67 ms +8.19% -4.39% 9.83%
wpMemoryUsage 14.76 MB +0% -0% 14.73 MB +0% -0% 0.24%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 48b6c08 trunk % Change
serverResponse 272.89 ms +0.97% -3.91% 267.22 ms +0.79% -7.71% 2.12%
firstPaint 270.03 ms +19.29% -8.39% 254.24 ms +9.55% -3.83% 6.21%
domContentLoaded 621.51 ms +6.75% -3.69% 616.04 ms +3.58% -4.78% 0.89%
loaded 622.44 ms +6.75% -3.7% 616.88 ms +3.59% -4.8% 0.9%
firstContentfulPaint 355.92 ms +9.25% -3.39% 365.29 ms +3.61% -4.45% -2.57%
firstBlock 1816.98 ms +5.14% -1.9% 1765.44 ms +1.44% -0.72% 2.92%
type 13.86 ms +4.04% -3.46% 15.35 ms +5.08% -8.08% -9.71%
navigate 79.46 ms +17.09% -6.17% 95.47 ms +10.15% -8.64% -16.77%
loadPatterns 1030.69 ms +7.48% -23.18% 1074.89 ms +6.94% -21.73% -4.11%
loadPages 967.37 ms +0.56% -2.34% 974.72 ms +1.92% -0.53% -0.75%
wpTotal 266.29 ms +0.92% -4.03% 260.63 ms +0.81% -7.89% 2.17%
wpMemoryUsage 12.78 MB +0% -0% 12.74 MB +0% -0% 0.28%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

676c138 Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 1,
+   "offset": 1,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
should keep correct selection when dragging outside block (-firefox) in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 1
+ Received  + 1

  Array [
    Object {
      "attributes": Object {
-       "content": "1",
+       "content": "23",
      },
      "name": "core/paragraph",
    },
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:787:41
should select by dragging into nested block in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 2 failed attempts.
Error: expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 0

  Array [
    1,
-   2,
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:569:5
Error: expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 0

  Array [
    1,
-   2,
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:569:5
should select by dragging into separator in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 1 failed attempt.
Error: expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 0

  Array [
    1,
-   2,
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:2182:5
should undo without broken temporary state in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:image -->
- <figure class="wp-block-image"><img alt=""/></figure>
+ <!-- wp:image {"id":20,"sizeSlug":"full","linkDestination":"none"} -->
+ <figure class="wp-block-image size-full"><img src="http://localhost:8891/wp-content/uploads/2026/11/4a6cc4c1-2c40-43cf-bdff-0778b800e4df.png" alt="" class="wp-image-20"/></figure>
  <!-- /wp:image -->
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/image.spec.js:432:49
three users concurrently edit a large post with diverse blocks in /test/e2e/specs/editor/collaboration/collaboration-stress.spec.ts, passed after 1 failed attempt.
TimeoutError: locator.waitFor: Timeout 17500ms exceeded.
Call log:
  - waiting for getByRole('button', { name: /Collaborators list/ }) to be visible

    at map (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/fixtures/collaboration-utils.ts:193:7)
    at CollaborationUtils.waitForMutualDiscovery (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/fixtures/collaboration-utils.ts:188:10)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-stress.spec.ts:416:28

676c138 Run

@github-actions github-actions Bot added [Package] Core data /packages/core-data [Package] Sync /packages/sync labels Oct 28, 2026
@alecgeatches alecgeatches added the [Feature] Real-time Collaboration Phase 3 of the Gutenberg roadmap around real-time collaboration label Oct 28, 2026
@alecgeatches alecgeatches added [Type] Bug An existing feature does not function as intended No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core and removed No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core labels Oct 28, 2026
Comment on lines +151 to +152
if ( $is_recovery_point ) {
$autosave_id = _wp_put_post_revision( $post_data );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What are the benefits of this special autosave vs. triggering a normal save?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I pushed up some new changes in 6aa70bb. We're still using _wp_put_post_revision(), so I'll answer that and explain the other changes a bit here.

What are the benefits of this special autosave vs. triggering a normal save?

We're using a revision save instead of a post save because we have potentially lost content, and we want to store it somewhere permanently so it can be restored if desired. Other save types won't work:

  • A regular save would write the "loser's" content back into the parent post. In this particular case we're defending against, we want to save a backup of the current post when another user's document replaces the whole CRDT. Saving the post directly would just overwrite the other user's work, and we're trying to do the opposite and keep any content that's been removed.
  • A regular autosave would overwrite the user's single per-user autosave. After some time, the loser's tab would autosave again with the winner's content and overwrite it, so we wouldn't get a backup.

We want a revision here because we want a checkpoint for potentially accidentally-removed content (like the accidental content loss in #85792), not because we want this user's version of content to win. A call to _wp_put_post_revision() for a permanent revision does this without otherwise changing the official post content after a new CRDT is created.


Additionally, I made a generalization change to match the discussion in #85792 (comment) about our overly RTC-specific changes to the autosave controller. Instead of the client passing the RTC-specific crdt_recovery_point parameter to ask for a special revision to be made, we pass a generalized as_revision instead, which does the same thing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Real-time Collaboration Phase 3 of the Gutenberg roadmap around real-time collaboration No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core [Package] Core data /packages/core-data [Package] Sync /packages/sync [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants