Skip to content

Core Data: Merge block metadata attribute structurally in RTC to prevent orphaned notes - #79956

Closed
adamsilverstein wants to merge 8 commits into
trunkfrom
fix/74751-concurrent-notes-rtc
Closed

adamsilverstein wants to merge 8 commits into
trunkfrom
fix/74751-concurrent-notes-rtc

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented Jul 7, 2026 •

Copy link
Copy Markdown
Member

Fixes #74751

What?

When two collaborators add a note to the same block at the same moment, one of the notes ends up orphaned and shows "Original block deleted" for its author, while the other collaborator cannot see it at all. This PR makes the block metadata attribute merge structurally in the real-time collaboration layer so concurrent note additions both survive.

Why?

#75147 made it possible for multiple note threads to co-exist on one block (metadata.noteId became an array), but the RTC layer still stored the whole metadata attribute as a plain value in the CRDT document. The generic metadata attribute is registered as { type: 'object' } with no query schema (packages/block-editor/src/hooks/metadata.js), and mergeCrdtBlocks only creates structured Yjs types for rich-text, array+query, and object+query attributes. Everything else is replaced wholesale via Y.Map.set().

Concurrent set() calls on the same key are resolved last-writer-wins by Yjs, so when peer A wrote { noteId: [ 101 ] } and peer B concurrently wrote { noteId: [ 102 ] }, one write was discarded entirely and that note lost its block reference.

As @Mamaduka noted on the issue, this is not specific to Notes: any feature using block metadata for storage hits the same race.

How?

All changes are in packages/core-data:

  • The generic metadata attribute is now stored as a Y.Map, and its noteId key as a Y.Array. Both containers are materialized for every block in the document (even when empty), so concurrent first writes from two peers insert into the same shared array - concurrent insertions commute in Yjs - instead of racing to create the container (container creation is itself last-writer-wins).
  • noteId is merged with set semantics (missing ids deleted, new ids appended); its container is cleared but never deleted, so a temporarily-empty incoming state can't destroy the shared array. Other metadata keys (name, bindings, ...) merge per-key instead of clobbering the whole object.
  • Empty containers are stripped whenever blocks are read out of the document (deserializeBlockAttributes, the persisted-content comparison, and the areBlocksEqual diff), so editor state, serialized post content, and dirty-state detection are unchanged.
  • Plain-value metadata from documents created before this change is upgraded to the structured form on the next merge.
  • Block types that register their own metadata attribute with a query schema keep the existing schema-driven merge behavior.

The set-mergeable key list is a constant (METADATA_SET_KEYS = [ 'noteId' ]) so other metadata-based features can opt in later.

Testing Instructions

Automated:

npm run test:unit -- packages/core-data/src/utils/test/crdt-blocks.ts
npm run test:e2e -- editor/collaboration/http-only/collaboration-concurrent-notes

The new e2e test holds all wp-sync requests on both collaborators' pages while each user adds a note to the same block, then releases them so the concurrent updates merge. It was verified to fail against a build without the fix (the block ends up with a single noteId and one note is discarded) and to pass with it. It lives under http-only/ because pausing replication via request interception only works with the HTTP polling provider.

Manual:

  1. Enable real-time collaboration (Settings > Writing).
  2. Open the same post as two different users in two browsers.
  3. Throttle or briefly disconnect the network in both, and add a note to the same block as each user at (nearly) the same moment; restore the connection.
  4. Both notes should appear attached to the block for both users; previously one showed "Original block deleted" for its author and was invisible to the other user.

Test in WP Playground

The generic block `metadata` attribute is registered as a plain
`{ type: 'object' }` attribute with no query schema, so the CRDT merge
layer stored it as a plain value and replaced it wholesale on every
change. When two collaborators wrote metadata concurrently (e.g. both
attaching a note to the same block at the same moment), Yjs resolved the
concurrent map set last-writer-wins and one peer's note id was
discarded, orphaning that note with 'Original block deleted'.

Store metadata as a Y.Map and its noteId key as a Y.Array that is
materialized for every block, so concurrent note additions insert into
the same shared array and commute instead of racing on container
creation. Set-type keys are diffed with set semantics and their
containers are cleared but never deleted; other metadata keys merge
per-key. Empty containers are stripped whenever blocks are read out of
the document so editor state and serialized content are unchanged.

Fixes #74751
Holds all wp-sync requests on both collaborators' pages while each adds
a note to the same block, then releases them so the concurrent updates
merge. Verifies the block references both notes on both pages, both
threads render for both users, and neither note is orphaned.

Lives under http-only/ because pausing replication through request
interception only works with the HTTP polling provider.

Verified to fail (one note id discarded) against a build without the
metadata merge fix and pass with it.

See #74751
@adamsilverstein
adamsilverstein requested a review from nerrad as a code owner July 7, 2026 17:56
@adamsilverstein adamsilverstein added [Type] Bug An existing feature does not function as intended [Package] Core data /packages/core-data [Feature] Real-time Collaboration Phase 3 of the Gutenberg roadmap around real-time collaboration [Feature] Notes Phase 3 of the Gutenberg roadmap around block commenting labels Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Size Change: +539 B (+0.01%)

Total Size: 7.74 MB

📦 View Changed
Filename Size Change
build/scripts/core-data/index.min.js 32.6 kB +539 B (+1.68%)

compressed-size-action

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

Flaky tests detected in 040c1f3.
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.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/28887425590
📝 Reported issues:

@Mamaduka

Mamaduka commented Jul 7, 2026

Copy link
Copy Markdown
Member

I think this is a more general issue with RTC and complex block attributes. This probably needs a general solution rather than a metadata-attribute-specific one.

@t-hamano

t-hamano commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

This probably needs a general solution rather than a metadata-attribute-specific one.

I'm not very familiar with this area and relied heavily on AI, but I tried it with #81046.

adamsilverstein and others added 2 commits September 30, 2026 22:51
…-notes-rtc

# Conflicts:
#	packages/core-data/src/utils/test/crdt-blocks.jsdom.test.ts
#	packages/core-data/src/utils/test/crdt.jsdom.test.ts
Trunk upgraded wp-prettier to 3.9.6 and dropped dependency group
comment blocks from import headers; reformat the branch's files so
the lint job passes after the trunk merge.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CdtuWidg1XBK8cpYLNSWxF
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

🎉 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: adamsilverstein <adamsilverstein@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: adrianduffell <adrianduffell@git.wordpress.org>
Co-authored-by: smithjw1 <smithjw1@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: +505 B (+0.01%)

Total Size: 8.61 MB

📦 View Changed
Filename Size Change
build/scripts/core-data/index.min.js 77.2 kB +505 B (+0.66%)

eeb7613 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 11a3404 trunk % Change
timeToFirstByte 44.35 ms +6.76% -6.99% 44.4 ms +13.29% -5.07% -0.11%
largestContentfulPaint 76 ms +5.26% -5.26% 76 ms +5.26% -5.26% 0%
lcpMinusTtfb 29.7 ms +17.51% -6.9% 28.5 ms +8.95% -4.21% 4.21%
wpBeforeTemplate 22.9 ms +15.11% -3.49% 22.46 ms +17.41% -3.52% 1.96%
wpTemplate 17.25 ms +6.26% -6.78% 17.72 ms +1.41% -5.87% -2.65%
wpTotal 41.05 ms +7.09% -7.19% 40.96 ms +12.65% -5.47% 0.22%
wpMemoryUsage 7.72 MB +0% -0% 7.69 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 11a3404 trunk % Change
timeToFirstByte 35.85 ms +3.77% -2.79% 35.3 ms +8.78% -1.27% 1.56%
largestContentfulPaint 80 ms +2.5% -0% 80 ms +5% -5% 0%
lcpMinusTtfb 44.35 ms +2.93% -3.83% 44.15 ms +3.4% -6.8% 0.45%
wpBeforeTemplate 19.42 ms +2.21% -1.08% 19.23 ms +13.94% -1.35% 0.99%
wpTemplate 13.14 ms +6.85% -3.65% 13.49 ms +2% -3.48% -2.59%
wpTotal 33.03 ms +4.3% -2.6% 32.64 ms +8.95% -1.62% 1.19%
wpMemoryUsage 6.41 MB +0% -0% 6.38 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 11a3404 trunk % Change
mediaProcessingJpeg 326.35 ms +0.43% -0.74% 325.87 ms +0.41% -0.36% 0.15%
mediaProcessingAvif 4857.63 ms +0.31% -0.33% 4830.52 ms +0.64% -0.18% 0.56%
mediaProcessingJpegToAvif 3411.55 ms +0.28% -0.51% 3381.8 ms +0.22% -0.16% 0.88%

media-upload

Metric 11a3404 trunk % Change
jpegUploadProcessing 1444.57 ms +2.94% -1.53% 1432.19 ms +3.54% -1.33% 0.86%
pngUploadProcessing 214.75 ms +8.64% -1.75% 220.36 ms +1.87% -0.43% -2.55%
largeJpegUploadProcessing 1415.35 ms +0.56% -0.26% 1414.37 ms +1.25% -0.46% 0.07%
multipleImageUploadProcessing 1478.99 ms +2.05% -0.21% 1497.47 ms +0.67% -1.17% -1.23%

post-editor

Metric 11a3404 trunk % Change
serverResponse 465.75 ms +2.98% -7.06% 452.68 ms +7.92% -3.7% 2.89%
firstPaint 355.06 ms +41.98% -22.03% 376.54 ms +31.66% -21.02% -5.7%
domContentLoaded 878.72 ms +0.47% -1.02% 873.24 ms +2.01% -1% 0.63%
loaded 880.06 ms +0.46% -1.02% 874.46 ms +2% -0.99% 0.64%
firstContentfulPaint 518.07 ms +2.48% -2.69% 499.29 ms +2.2% -2.24% 3.76%
firstBlock 2246.04 ms +3.12% -1.23% 2223.15 ms +0.84% -1.19% 1.03%
type 18.72 ms +7.43% -7.1% 17.5 ms +5.83% -10.74% 6.97%
typeWithoutInspector 16.62 ms +2.83% -14.38% 18.32 ms +3.88% -9.12% -9.28%
typeWithTopToolbar 22.39 ms +6.7% -9.16% 22.96 ms +20.38% -6.01% -2.48%
typeContainer 8.73 ms +6.53% -3.44% 8.73 ms +9.28% -7.9% 0%
focus 117.42 ms +3.56% -0.81% 120.98 ms +5.27% -5.38% -2.94%
firstFocus 244.41 ms +0% -0% 265.59 ms +0% -0% -7.97%
selectAll 408.47 ms +2.15% -2.09% 418.56 ms +3.5% -4.39% -2.41%
undo 29.97 ms +5.47% -7.14% 31.27 ms +7.42% -5.37% -4.16%
listViewOpen 75.89 ms +8.76% -6.43% 78.65 ms +11.1% -6.97% -3.51%
inserterOpen 43.35 ms +11.14% -8% 44.53 ms +8.02% -2.78% -2.65%
inserterHover 11.19 ms +4.11% -8.85% 11.01 ms +9.08% -12.17% 1.63%
inserterSearch 8.59 ms +4.54% -4.31% 8.34 ms +10.79% -4.92% 3%
loadPatterns 662.94 ms +3.77% -3.7% 650.38 ms +2.29% -3.32% 1.93%
wpTotal 455.26 ms +3.21% -7.05% 442.94 ms +8.12% -3.76% 2.78%
wpMemoryUsage 14.78 MB +0% -0% 14.74 MB +0% -0% 0.24%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 11a3404 trunk % Change
serverResponse 463.44 ms +0.74% -5.32% 463.71 ms +1.47% -8.51% -0.06%
firstPaint 322.63 ms +11.03% -3.52% 345.52 ms +5.9% -6.94% -6.62%
domContentLoaded 849.1 ms +1.57% -0.22% 851.96 ms +2.64% -0.88% -0.34%
loaded 850.02 ms +1.57% -0.23% 853.07 ms +2.66% -0.89% -0.36%
firstContentfulPaint 487.29 ms +2.71% -1.6% 496.92 ms +2.54% -2.1% -1.94%
firstBlock 2520.67 ms +2.28% -0.69% 2526.24 ms +0.58% -0.92% -0.22%
type 17.58 ms +5.52% -12.97% 17.49 ms +5.72% -4.52% 0.51%
navigate 114.89 ms +19.62% -5.98% 111.71 ms +8.58% -2.48% 2.85%
loadPatterns 1175.02 ms +20.46% -9.37% 1245.51 ms +5.43% -11.78% -5.66%
loadPages 956.22 ms +10.37% -1.7% 989.51 ms +0.73% -3.9% -3.36%
wpTotal 454.08 ms +0.71% -5.44% 454.37 ms +1.19% -8.7% -0.06%
wpMemoryUsage 12.79 MB +0% -0% 12.76 MB +0% -0% 0.27%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

eeb7613 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.

Page List converts to Navigation Links with entity bindings in /test/e2e/specs/editor/blocks/navigation.spec.js, passed after 1 failed attempt.
Error: expect(locator).toContainText(expected) failed

Locator: getByRole('region', { name: 'Editor settings' }).getByRole('tabpanel', { name: 'Content' }).getByRole('button', { name: /Link to:/ })
Expected substring: "test-page-1"
Received string:    "Link to:Cat/catPagePublished"
Timeout: 5000ms

Call log:
  - Expect "toContainText" getByRole('region', { name: 'Editor settings' }).getByRole('tabpanel', { name: 'Content' }).getByRole('button', { name: /Link to:/ }) with timeout 5000ms
  - waiting for getByRole('region', { name: 'Editor settings' }).getByRole('tabpanel', { name: 'Content' }).getByRole('button', { name: /Link to:/ })
    14 × locator resolved to <button type="button" aria-expanded="false" aria-haspopup="dialog" class="components-button link-preview-button is-next-40px-default-size is-secondary">…</button>
       - unexpected value "Link to:Cat/catPagePublished"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/navigation.spec.js:1949:31
a navigation superseded by the traversal discharges to idle in /test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts, passed after 1 failed attempt.
SyntaxError: Unexpected token 'o', "not hydrated" is not valid JSON
    at readLog (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts:28:14)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts:47:24
    at waitForLogLength (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts:46:2)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts:693:5

eeb7613 Run

@Mamaduka Mamaduka moved this from 🔎 Needs Review to 🦵 Punted to 7.3 in WordPress 7.2 Editor Tasks Nov 3, 2026
Trunk moved floating notes into a canvas margin (#83786), so the
note form is no longer inside the Editor settings region unless the
All notes sidebar is open. Open it on both pages, as the other
collaboration notes tests do.
@adamsilverstein

adamsilverstein commented Nov 10, 2026 •

Copy link
Copy Markdown
Member Author

Closing in favor of

@github-project-automation github-project-automation Bot moved this from 🦵 Punted to 7.3 to ✅ Done in WordPress 7.2 Editor Tasks Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Notes Phase 3 of the Gutenberg roadmap around block commenting [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

Development

Successfully merging this pull request may close these issues.

Note appears as being attached to a deleted block when two users add them at exactly the same moment using real-time collaboration

3 participants