Skip to content

Real-time collaboration: Save the selection from before a change when changes are batched - #85980

Draft
ellatrix wants to merge 2 commits into
trunkfrom
fix/rtc-undo-selection-history
Draft

ellatrix wants to merge 2 commits into
trunkfrom
fix/rtc-undo-selection-history

Conversation

@ellatrix

Copy link
Copy Markdown
Member

What?

With real-time collaboration, undo restores the caret from before the change even when several changes were applied to the shared document in one batch.

This fixes the most frequent CI flake right now, "Collaboration - Undo/Redo › Undo restores the post selection when another synced entity is loaded": it needed a retry in 8 of the last 10 trunk runs (2026-10-29).

Why?

In the test, the user types "abcdef", moves the caret to 3, presses Enter, then undoes. The caret should go back to 3 but lands at 6. A trace of a failing run showed:

  1. When editing alone, the sync manager queues local changes and applies them in a setTimeout. Playwright presses keys back to back and Chromium runs input before timers, so the typing, the caret moves and Enter were all applied in one batch after Enter.
  2. Each change records its selection in the selection history in another setTimeout (applyPostChangesToCRDTDoc), so none of them had been recorded when the batch reached Enter's change.
  3. Enter's undo level saved that stale history (the caret at 0 in the then-empty paragraph), and undo restored it as the end of "abcdef".

Locally it failed 6 of 10 runs on trunk. CI only started hitting it on Oct 23, when "Try: Run e2e tests in parallel, on one wp-env site per two cores" (#85420) put more load on each runner. A person typing doesn't batch changes like this, but a busy main thread can.

How?

Record the selection when the change's Yjs transaction ends (ydoc.once( 'afterTransaction' )) instead of in a timeout. The undo manager saves its undo meta in its own afterTransaction listener, which was added first, so the meta still holds the selection from before the change, as the timeout intended.

Testing Instructions

  1. New unit test, "saves the selection from before a change in its undo level": applies a caret move and then an edit with no timers in between. It fails on trunk (expected [ undefined ]) and passes here.
  2. WP_BASE_URL=<site> npm run test:e2e -- --project=chromium --retries=0 --repeat-each=10 test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts -g "Undo restores the post selection": 6 of 10 failed on trunk, 10 of 10 pass here.
  3. All collaboration e2e tests pass (69 of 69), as do the core-data and sync unit tests.

There is no Playground link or screenshot: the bug only shows when changes arrive faster than the sync manager's timeout, which can't be done by hand, so the tests above cover it.

Use of AI Tools

Investigated and written with Claude Code.

🤖 Generated with Claude Code

… changes are batched

The selection history was updated in a timeout, so when several queued
changes were applied in one go, an undo level saved the selection from
before all of them. Update it when the change's transaction ends, after
the undo manager has saved its meta.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ellatrix ellatrix added [Type] Bug An existing feature does not function as intended [Feature] Real-time Collaboration Phase 3 of the Gutenberg roadmap around real-time collaboration labels Oct 29, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the [Package] Core data /packages/core-data label Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

📦 Bundle size

Size Change: +15 B (0%)

Total Size: 8.56 MB

📦 View Changed
Filename Size Change
build/scripts/core-data/index.min.js 76.7 kB +15 B (+0.02%)

94de2f7 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 931a1d3 trunk % Change
timeToFirstByte 57.55 ms +3.3% -4.26% 58.75 ms +3.66% -6.55% -2.04%
largestContentfulPaint 96 ms +4.17% -4.17% 96 ms +4.17% -4.17% 0%
lcpMinusTtfb 37.25 ms +18.39% -7.92% 34.95 ms +13.88% -3.86% 6.58%
wpBeforeTemplate 29.48 ms +3.15% -1.09% 30.43 ms +5.69% -4.37% -3.12%
wpTemplate 23.12 ms +3.33% -5.32% 23.45 ms +7.04% -5.71% -1.41%
wpTotal 53.53 ms +3.87% -4.18% 54.76 ms +3.83% -6.78% -2.25%
wpMemoryUsage 7.70 MB +0% -0% 7.67 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 931a1d3 trunk % Change
timeToFirstByte 49.35 ms +5.37% -3.34% 48.1 ms +7.8% -1.87% 2.6%
largestContentfulPaint 108 ms +0% -3.7% 104 ms +3.85% -3.85% 3.85%
lcpMinusTtfb 56.8 ms +4.93% -1.41% 55.7 ms +3.05% -4.58% 1.97%
wpBeforeTemplate 27.57 ms +8.31% -1.31% 27.5 ms +5.02% -1.75% 0.25%
wpTemplate 17.74 ms +9.19% -4.45% 17.54 ms +4.05% -3.59% 1.14%
wpTotal 46.14 ms +5.68% -3.36% 44.79 ms +8.1% -1.83% 3.01%
wpMemoryUsage 6.40 MB +0% -0% 6.36 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 931a1d3 trunk % Change
mediaProcessingJpeg 406.65 ms +1.52% -0.42% 406.27 ms +0.72% -1.2% 0.09%
mediaProcessingAvif 6173.73 ms +0.36% -0.38% 6176.16 ms +0.41% -0.48% -0.04%
mediaProcessingJpegToAvif 4252.98 ms +0.21% -0.33% 4237.82 ms +0.71% -0.06% 0.36%

media-upload

Metric 931a1d3 trunk % Change
jpegUploadProcessing 1962.45 ms +0.72% -0.75% 1475.91 ms +4.58% -0.71% 32.97%
pngUploadProcessing 273.3 ms +8.36% -2.94% 174.91 ms +3.67% -4.32% 56.25%
largeJpegUploadProcessing 1950.24 ms +0.38% -0.71% 1947.37 ms +1.01% -0.79% 0.15%
multipleImageUploadProcessing 2059.44 ms +0.87% -0.36% 2036.3 ms +0.3% -0.54% 1.14%

post-editor

Metric 931a1d3 trunk % Change
serverResponse 332.88 ms +6.52% -5.47% 354.73 ms +2.02% -12.13% -6.16%
firstPaint 387.43 ms +3.04% -37.65% 347.84 ms +14.74% -52.76% 11.38%
domContentLoaded 682.91 ms +3.1% -2.27% 678.54 ms +2.86% -1.92% 0.64%
loaded 683.93 ms +3.1% -2.28% 679.56 ms +2.88% -1.92% 0.64%
firstContentfulPaint 398.83 ms +6.23% -1.51% 401.71 ms +3.64% -1.54% -0.72%
firstBlock 2224.01 ms +1.96% -1.31% 2205.52 ms +1.1% -2.82% 0.84%
type 14.88 ms +5.71% -8.8% 14.19 ms +5.57% -5.43% 4.86%
typeWithoutInspector 15.33 ms +3.72% -10.37% 13.93 ms +9.55% -7.75% 10.05%
typeWithTopToolbar 18.88 ms +2.17% -9.8% 17.78 ms +3.66% -1.91% 6.19%
typeContainer 6.26 ms +5.91% -6.71% 6.27 ms +5.1% -11.64% -0.16%
focus 94.71 ms +1.64% -3.93% 86.57 ms +11.71% -0.7% 9.4%
firstFocus 193.48 ms +0% -0% 172.69 ms +0% -0% 12.04%
selectAll 363.14 ms +5.39% -3.48% 344.03 ms +7.94% -3.7% 5.55%
undo 24.57 ms +5.45% -4.52% 24.41 ms +5.82% -2.74% 0.66%
listViewOpen 59.62 ms +1.58% -4.66% 56.58 ms +10.25% -8.87% 5.37%
inserterOpen 31.14 ms +4.85% -3.98% 32.07 ms +3.77% -8.33% -2.9%
inserterHover 9.1 ms +10.99% -1.43% 9.08 ms +5.51% -8.7% 0.22%
inserterSearch 6.76 ms +10.65% -5.77% 7.08 ms +3.67% -6.92% -4.52%
loadPatterns 489.82 ms +7.53% -1.98% 499.13 ms +3.06% -4.73% -1.87%
wpTotal 324.08 ms +6.33% -5.7% 345.88 ms +1.76% -12.48% -6.3%
wpMemoryUsage 14.73 MB +0% -0% 14.72 MB +0% -0% 0.1%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 931a1d3 trunk % Change
serverResponse 550.88 ms +2.94% -4.7% 555.39 ms +3.49% -3.8% -0.81%
firstPaint 284.11 ms +10.11% -11.28% 290.84 ms +20.11% -3.27% -2.31%
domContentLoaded 962.89 ms +2.32% -1.21% 1010.9 ms +1.19% -1.53% -4.75%
loaded 964.12 ms +2.31% -1.19% 1012.31 ms +1.21% -1.52% -4.76%
firstContentfulPaint 544.12 ms +1.05% -4.9% 569.92 ms +2.97% -5.36% -4.53%
firstBlock 4175.4 ms +0.56% -1.23% 4170.5 ms +0.74% -0.52% 0.12%
type 21.86 ms +7.73% -2.2% 22.65 ms +4.59% -11.92% -3.49%
navigate 140.6 ms +9.29% -2.3% 137.46 ms +0.8% -2.07% 2.28%
loadPatterns 1357.7 ms +28.44% -7.43% 1355.17 ms +4.06% -3.13% 0.19%
loadPages 1081.09 ms +2.1% -1.33% 1051.8 ms +2.23% -5.35% 2.78%
wpTotal 540.63 ms +2.84% -4.7% 544.84 ms +3.58% -3.81% -0.77%
wpMemoryUsage 12.77 MB +0% -0% 12.73 MB +0% -0% 0.27%
wpDbQueries 44 +2.27% -0% 44 +2.27% -0% 0%

94de2f7 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.

Custom Completer: should allow option selection via keypress event in /test/e2e/specs/editor/various/autocomplete-and-mentions.spec.js, passed after 2 failed attempts.
Error: apiRequestContext.fetch: socket hang up
Call log:
  - → DELETE http://localhost:8891/wp-json/wp/v2/users/30?force=true&reassign=1
    - user-agent: Playwright/1.63.0 (x64; ubuntu 24.04) node/24.20 CI/1
    - accept: */*
    - accept-encoding: gzip,deflate,br
    - X-WP-Nonce: 08d731d0b6
    - cookie: wordpress_test_cookie=WP%20Cookie%20check; wordpress_logged_in_da433b7faafd8180ebeb1624ceeeab7e=admin%7C1793484492%7CAEafp5BmdefcJADbsHp6WhhdjihkXHmMHJzpJO2wFov%7C0b47d85994307d86c61809dedb85d72b3bf56165fd7cdc352b907d38b20d4cc7; wp-settings-time-1=1793312366

    at RequestUtils.rest (/home/runner/work/gutenberg/gutenberg/packages/e2e-test-utils-playwright/src/request-utils/rest.ts:111:39)
    at RequestUtils.deleteUser (/home/runner/work/gutenberg/gutenberg/packages/e2e-test-utils-playwright/src/request-utils/users.ts:91:30)
    at map (/home/runner/work/gutenberg/gutenberg/packages/e2e-test-utils-playwright/src/request-utils/users.ts:116:51)
    at RequestUtils.deleteAllUsers (/home/runner/work/gutenberg/gutenberg/packages/e2e-test-utils-playwright/src/request-utils/users.ts:116:5)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/autocomplete-and-mentions.spec.js:73:3

can be created by selecting text and using keyboard shortcuts in /test/e2e/specs/editor/blocks/links.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 1
+ Received  + 1

  Array [
    Object {
      "attributes": Object {
-       "content": "This is Gutenberg",
+       "content": "This is ht",
      },
      "name": "core/paragraph",
    },
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/links.spec.js:563:41
does not select blocks in the canvas preview when editing from the sidebar in /test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeVisible() failed

Locator: getByRole('listbox', { name: 'Search results' })
Expected: visible
Timeout: 5000ms
Error: element(s) not found

Call log:
  - Expect "toBeVisible" getByRole('listbox', { name: 'Search results' }) with timeout 5000ms
  - waiting for getByRole('listbox', { name: 'Search results' })

    at LinkControl.useLinkControlSearch (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js:423:33)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js:179:3
region-scoped focus that refrains on traversals in /test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts, passed after 1 failed attempt.
Error: expect(locator).toHaveCount(expected) failed

Locator:  locator('[data-wp-router-region="lifecycle-a"] :focus')
Expected: 0
Received: 1
Timeout:  5000ms

Call log:
  - Expect "toHaveCount" locator('[data-wp-router-region="lifecycle-a"] :focus') with timeout 5000ms
  - waiting for locator('[data-wp-router-region="lifecycle-a"] :focus')
    14 × locator resolved to 1 element
       - unexpected value "1"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts:1652:37
should gradually multi-select in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 3
+ Received  + 7

  Array [
    Object {
-     "name": "core/column",
+     "attributes": Object {
+       "content": "1",
+       "dropCap": false,
      },
-   Object {
-     "name": "core/column",
+     "clientId": "c61c1271-b85f-4f4b-a6fa-60ebe2f1b816",
+     "innerBlocks": Array [],
+     "isValid": true,
+     "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:1200: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":22,"sizeSlug":"full","linkDestination":"none"} -->
+ <figure class="wp-block-image size-full"><img src="http://localhost:8889/wp-content/uploads/2026/10/afc1e8cb-69e3-4f7e-be51-3f09f403a1c2.png" alt="" class="wp-image-22"/></figure>
  <!-- /wp:image -->
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/image.spec.js:432:49

94de2f7 Run

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 [Package] Core data /packages/core-data [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant