Skip to content

fix(sync): do not push or save the generated initial state on open - #9589

Merged
mejo- merged 2 commits into
mainfrom
fix/dont_push_initial_state
Nov 17, 2026
Merged

mejo- merged 2 commits into
mainfrom
fix/dont_push_initial_state

Conversation

@mejo-

@mejo- mejo- commented Nov 10, 2026

Copy link
Copy Markdown
Member

Opening a file without a stored Yjs state applied the state generated from the markdown as a local change. It was pushed like an edit, which marked the document dirty, so closing the viewer or leaving the page saved the re-serialized markdown even though nothing was edited. This dropped trailing newlines and normalized the syntax of untouched files.

Apply the initial state with the sync provider as origin. Every client generates the same state from the file content, and the provider sends it along with the first local change, as it sends everything the server does not have yet.

Fixes: #9077

Assisted-by: ClaudeCode:claude-opus-5-5

🏁 Checklist

  • Code is properly formatted (npm run lint / npm run stylelint / composer run cs:check)
  • Sign-off message is added to all commits
  • Tests (unit, integration and/or end-to-end) passing and the changes are covered with tests

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI tools
  • The AI-generated content was reviewed, comprehended and tested by a human

@mejo- mejo- self-assigned this Nov 10, 2026
@mejo- mejo- added bug Something isn't working 3. to review labels Nov 10, 2026
@github-project-automation github-project-automation Bot moved this to 🧭 Planning evaluation (don't pick) in 📝 Productivity team Nov 10, 2026
@nextcloud-command nextcloud-command added the AI assisted This PR contains AI-assisted commits label Nov 10, 2026
@mejo- mejo- moved this from 🧭 Planning evaluation (don't pick) to 👀 In review in 📝 Productivity team Nov 10, 2026

@blizzz blizzz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With a by-catch might be worth a follow up, as it was not introduced (though affected) here: typing after a document reset is silently lost

  1. Create a.md with at least two versions. Delete its Text document state, for example with occ text:reset --full <fileId>.
  2. User A opens a.md with one editor only, which means a 5 s poll interval, or 20 s if the tab is hidden. A does not type.
  3. User B restores an older version in the Versions sidebar.
  4. Before A's next poll, A types "LOST".
  5. The push returns 412, an "Editing session has expired" toast appears and the page reloads.
    • "LOST" is gone.
    • localStorage['text-indexeddb-conflict-file-<id>'] is not set.
  6. Control: on main, step 3 leaves the document untouched. hasUnsavedChanges is true because of the initial step, so the restore does not reset it.

Opening a file without a stored Yjs state applied the state generated
from the markdown as a local change. It was pushed like an edit, which
marked the document dirty, so closing the viewer or leaving the page
saved the re-serialized markdown even though nothing was edited. This
dropped trailing newlines and normalized the syntax of untouched files.

Apply the initial state with the sync provider as origin. Every client
generates the same state from the file content, and the provider sends
it along with the first local change, as it sends everything the server
does not have yet.

Fixes: #9077

Signed-off-by: Jonas <jonas@freesources.org>
Assisted-by: ClaudeCode:claude-opus-5-5
Only versions that were pushed but not saved yet marked the document as
dirty. Changes made while offline were therefore not marked dirty: they
were not kept in IndexedDB for recovery, and no conflict resolution was
offered when the file changed meanwhile.

Track whether the outbox holds a local update that was not pushed yet
and include it in the dirty state. Push pending changes before saving,
so the save includes them, and autosave versions that were pushed by a
retry without an editor update.

Signed-off-by: Jonas <jonas@freesources.org>
Assisted-by: ClaudeCode:claude-opus-5-5
@mejo-
mejo- force-pushed the fix/dont_push_initial_state branch from f3308c0 to ea24645 Compare November 11, 2026 13:25
@max-nextcloud

Copy link
Copy Markdown
Contributor

I assumed I had fixed this in 6be8cd3

Did not know about the linked issue - so I did not test it. Will check if this can still be reproduced.

@max-nextcloud

Copy link
Copy Markdown
Contributor

Ah... I see. This is about the dirty tracking not autosave.

@max-nextcloud max-nextcloud left a comment

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.

First commit makes sense to me either way.

Second commit I would prefer to set the dirty state on updates that are added to history just like we trigger the autosave. That would make sure the two behave consistently and seems simpler from my point of view.

@mejo-

mejo- commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

Second commit I would prefer to set the dirty state on updates that are added to history just like we trigger the autosave. That would make sure the two behave consistently and seems simpler from my point of view.

That way, Undo/Redo wouldn't trigger a push/save request after going online again as they're addToHistory: false.

I'm too tired now to put it into my own words, so I'll let claudes findings here:

  1. Undo/redo isn't an addToHistory update

Text uses the undo/redo from Yjs (via tiptap's Collaboration extension). An undo changes the Yjs document, and y-prosemirror then applies that change to the editor with addToHistory: false (node_modules/y-prosemirror/src/plugins/sync-plugin.js:355, used at :635). So autosaveOnUpdate ignores undo and redo, and a dirty flag based on the same check would too. I tested this with a scratch spec on nextcloud.local:

Scenario main (= autosave trigger from 6be8cd3) your two commits
Type, wait for the save, Ctrl+Z → is the undo saved? ✘ no save request within 15 s ✓
Type, wait for the save, go offline, Ctrl+Z → "Unsaved changes"? ✘ still "Last saved" ✓

With Max's approach, the second row stays as on main. An undo made offline is never marked dirty, so there's no IndexedDB recovery and no conflict dialog. Your second commit gets the first row right as a side effect: once the undo is pushed, the new autosave on unsaved pushed versions picks it up.

  1. An editor-update flag has no reliable point to reset it

The flag would also need to be reset at some point.

  • Resetting it on changesPushed is too early: if the user types while a push is still in flight, that push's response clears the flag, but the new edit hasn't been pushed. Going offline at that moment loses the dirty state. The Outbox already handles this case; Outbox.spec.ts tests it ("keeps the update that arrived while sending").
  • Resetting it on save needs the version of the latest edit. Getting that means tracking pushes again.

The outbox is where "this local change hasn't reached the server" is actually known, so dirty follows it exactly.

  1. Some local changes don't come from the editor

On reopen, sendUpdateFromDoc('offline', …) sends the changes recovered from IndexedDB directly to the provider. That isn't an editor transaction at all. The outbox sees these changes, while an editor listener would not.

On consistency

Your second commit already brings autosave closer to dirty. Any change that reaches the server unsaved now gets autosaved, whatever caused it, and that includes undo and retried pushes. Making dirty as narrow as the autosave trigger would bring the two back in line by reintroducing these gaps.

@mejo-

mejo- commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

/backport to stable35

@mejo-
mejo- merged commit 512b224 into main Nov 17, 2026
76 of 78 checks passed
@mejo-
mejo- deleted the fix/dont_push_initial_state branch November 17, 2026 10:38
@github-project-automation github-project-automation Bot moved this from 👀 In review to ☑️ Done in 📝 Productivity team Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review AI assisted This PR contains AI-assisted commits bug Something isn't working

Projects

Status: ☑️ Done

Development

Successfully merging this pull request may close these issues.

Text overrides restored file due to trailing newline

4 participants