Repository navigation
RTC: Add revision when CRDT content is replaced - #85876
alecgeatches wants to merge 6 commits into
Conversation
… as a permanent revision
…aced by a document that shares no history (e.g. a CRDT document replacement)
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +37 B (0%) Total Size: 8.61 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome 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
|
| if ( $is_recovery_point ) { | ||
| $autosave_id = _wp_put_post_revision( $post_data ); |
There was a problem hiding this comment.
What are the benefits of this special autosave vs. triggering a normal save?
There was a problem hiding this comment.
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.
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_documentmeta 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 theblocksproperty, 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:
root.set('blocks', { ... })), which corresponds to a full replacement of content without a shared history. This calls into an optionalonRemoteReplacement()callback.editRecordis called with the new content, the editor fires off an autosave request with a specialcrdt_recovery_pointflag.This allows either user to recover overwritten changes:
autosave-test-fix.mov
Known limits for this solution:
Testing Instructions
See videos above. On
trunkcurrently (commit2481dec21c8bfdad3779585535527cb84094f16c), the reproduction steps for #85792 can be used to create an edited post without a saved CRDT document:Ensure real-time collaboration is enabled, and set
AUTOSAVE_INTERVALto10to speed up the initial autosave:As user A, create a draft post with a paragraph. Do not explicitly save, and wait for an
/autosavesREST request.After the save goes through, add additional a second paragraph to the post and wait for an auto save. Again, do not explicitly save.
As user B, in another browser or profile, open the same post.
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.