From ddceee95d2278fcca4748edfa8e54bf204e4893a Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 10 Nov 2026 15:57:29 +0100 Subject: [PATCH 1/2] fix(sync): do not push or save the generated initial state on open 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 Assisted-by: ClaudeCode:claude-opus-5-5 --- .../e2e/synchronize-content/untouched.spec.ts | 28 +++++++ src/components/CollaborativeEditor.vue | 5 ++ src/helpers/setInitialYjsState.ts | 5 +- src/tests/helpers/setInitialYjsState.spec.ts | 73 +++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 playwright/e2e/synchronize-content/untouched.spec.ts create mode 100644 src/tests/helpers/setInitialYjsState.spec.ts diff --git a/playwright/e2e/synchronize-content/untouched.spec.ts b/playwright/e2e/synchronize-content/untouched.spec.ts new file mode 100644 index 00000000000..989e6030a72 --- /dev/null +++ b/playwright/e2e/synchronize-content/untouched.spec.ts @@ -0,0 +1,28 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { expect, mergeTests } from '@playwright/test' +import { test as editorTest } from '../../support/fixtures/editor.ts' +import { test as uploadFileTest } from '../../support/fixtures/upload-file.ts' + +const test = mergeTests(editorTest, uploadFileTest) + +test.use({ fileContent: '## Hello world\n' }) + +test('does not save an untouched file', async ({ close, editor, open, page }) => { + const saves: string[] = [] + page.on('request', (request) => { + if (/\/session\/.*\/save/.test(request.url())) { + saves.push(request.url()) + } + }) + const push = page.waitForResponse(/\/session\/.*\/push/) + await open() + await expect(editor.getHeading({ name: 'Hello world' })).toBeVisible() + await push + await expect(editor.saveIndicator).not.toHaveAccessibleName(/Unsaved changes/) + await close() + expect(saves).toEqual([]) +}) diff --git a/src/components/CollaborativeEditor.vue b/src/components/CollaborativeEditor.vue index 7937d5cdff5..f92f4db0467 100644 --- a/src/components/CollaborativeEditor.vue +++ b/src/components/CollaborativeEditor.vue @@ -651,8 +651,13 @@ export default defineComponent({ content, isRichEditor: this.isRichEditor, }) + // Every client generates the same initial state from the file content. + // Mark it as received from the server so it is neither pushed nor saved. + // The first local change pushes it along, as the provider sends + // everything the server does not have yet. setInitialYjsState(this.ydoc, content, { isRichEditor: this.isRichEditor, + origin: this.syncProvider, }) } }) diff --git a/src/helpers/setInitialYjsState.ts b/src/helpers/setInitialYjsState.ts index c9304a5bae8..67272300779 100644 --- a/src/helpers/setInitialYjsState.ts +++ b/src/helpers/setInitialYjsState.ts @@ -17,11 +17,12 @@ import markdownit from '../markdownit/index.ts' * @param content desired content of the final document * @param options options * @param options.isRichEditor use a rich editor for the content + * @param options.origin origin of the update, e.g. the sync provider */ export function setInitialYjsState( ydoc: Doc, content: string, - { isRichEditor }: { isRichEditor: boolean }, + { isRichEditor, origin }: { isRichEditor: boolean, origin?: unknown }, ) { const html = isRichEditor ? markdownit.render(content) + '

' @@ -54,5 +55,5 @@ export function setInitialYjsState( } const baseUpdate = encodeStateAsUpdate(getBaseDoc(node)) - applyUpdate(ydoc, baseUpdate) + applyUpdate(ydoc, baseUpdate, origin) } diff --git a/src/tests/helpers/setInitialYjsState.spec.ts b/src/tests/helpers/setInitialYjsState.spec.ts new file mode 100644 index 00000000000..dc731d29bce --- /dev/null +++ b/src/tests/helpers/setInitialYjsState.spec.ts @@ -0,0 +1,73 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import type { SyncService } from '../../services/SyncService.ts' + +import * as decoding from 'lib0/decoding' +import mitt from 'mitt' +import { describe, expect, it, vi } from 'vitest' +import { applyUpdate, Doc, XmlText } from 'yjs' +import { setInitialYjsState } from '../../helpers/setInitialYjsState.ts' +import initWebSocketPolyfill from '../../services/WebSocketPolyfill.ts' +import { WebsocketProvider } from '../../services/y-websocket.js' + +const content = '# Hello world\n\nold line :)\n' + +/** + * Create a provider connected to a sync service that records the sent steps. + */ +async function setupProvider() { + const ydoc = new Doc() + const syncService = { + bus: mitt(), + open: vi.fn(async () => {}), + hasActiveConnection: () => true, + sendStep: vi.fn(), + } + const WebSocketPolyfill = initWebSocketPolyfill(syncService as unknown as SyncService) + const provider = new WebsocketProvider('ws://localhost:1234', 'file:123', ydoc, { + WebSocketPolyfill: WebSocketPolyfill as unknown as typeof WebSocket, + disableBc: true, + }) + await vi.waitUntil(() => provider.wsconnected) + const sentUpdates = () => syncService.sendStep.mock.calls + .map(([step]) => decoding.createDecoder(step)) + // sync message containing a sync step 2 or update + .filter((decoder) => decoding.readVarUint(decoder) === 0 && decoding.readVarUint(decoder) > 0) + .map((decoder) => decoding.readVarUint8Array(decoder)) + return { ydoc, provider, sentUpdates } +} + +describe('setInitialYjsState', () => { + it('applies the content with the given origin', () => { + const ydoc = new Doc() + const origin = {} + const onUpdate = vi.fn() + ydoc.on('update', onUpdate) + setInitialYjsState(ydoc, content, { isRichEditor: true, origin }) + expect(onUpdate).toHaveBeenCalledOnce() + expect(onUpdate.mock.calls[0][1]).toBe(origin) + expect(ydoc.getXmlFragment('default').toString()).toContain('old line :)') + }) + + it('is not sent with the sync provider as origin', async () => { + const { ydoc, provider, sentUpdates } = await setupProvider() + setInitialYjsState(ydoc, content, { isRichEditor: true, origin: provider }) + expect(sentUpdates()).toHaveLength(0) + provider.destroy() + }) + + it('is sent along with the first local change', async () => { + const { ydoc, provider, sentUpdates } = await setupProvider() + setInitialYjsState(ydoc, content, { isRichEditor: true, origin: provider }) + ydoc.getXmlFragment('default').insert(0, [new XmlText('edit')]) + expect(sentUpdates()).toHaveLength(1) + const remote = new Doc() + applyUpdate(remote, sentUpdates()[0]) + expect(remote.getXmlFragment('default').toString()) + .toBe(ydoc.getXmlFragment('default').toString()) + provider.destroy() + }) +}) From ea2464548981eac1df64281344746f499b57023a Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 11 Nov 2026 13:07:49 +0100 Subject: [PATCH 2/2] fix(sync): treat unpushed local changes as unsaved 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 Assisted-by: ClaudeCode:claude-opus-5-5 --- src/composables/useSaveService.ts | 21 +++- src/services/Outbox.ts | 21 +++- src/services/SaveService.ts | 6 ++ src/services/SyncService.ts | 8 +- src/tests/composables/useSaveService.spec.ts | 105 +++++++++++++++++++ src/tests/services/Outbox.spec.ts | 48 +++++++++ src/tests/services/SaveService.spec.ts | 19 ++++ src/tests/services/SyncService.spec.ts | 29 +++++ 8 files changed, 252 insertions(+), 5 deletions(-) create mode 100644 src/tests/composables/useSaveService.spec.ts create mode 100644 src/tests/services/Outbox.spec.ts diff --git a/src/composables/useSaveService.ts b/src/composables/useSaveService.ts index 6106ae46984..61d6cd9e2f8 100644 --- a/src/composables/useSaveService.ts +++ b/src/composables/useSaveService.ts @@ -42,7 +42,8 @@ export function provideSaveService( */ function getSaveData(): SaveData { return { - version: syncService.version, + // No steps were received yet on a document without stored state + version: Math.max(syncService.version, 0), autosaveContent: serialize(), documentState: getDocumentState(ydoc), } @@ -51,6 +52,7 @@ export function provideSaveService( const saveService = new SaveService({ connection, getSaveData, + pushChanges: () => syncService.sendRemainingSteps(), }) const autosaveOnUpdate = ({ transaction }: { transaction: Transaction }) => { @@ -99,13 +101,28 @@ export function provideSaveService( */ function updateVersionWithChanges(event: { version: number }) { versionWithChanges.value = Math.max(event.version, versionWithChanges.value) + // Retried pushes are not preceded by an editor update that triggers the autosave + if ((document.value?.lastSavedVersion ?? 0) < event.version && !saveService.autosave.isPending) { + saveService.autosave() + } } syncService.bus.on('changesPushed', updateVersionWithChanges) onUnmounted(() => { syncService.bus.off('changesPushed', updateVersionWithChanges) }) - const dirty = computed(() => (document.value?.lastSavedVersion ?? 0) < versionWithChanges.value) + const hasPendingChanges = ref(false) + // Track if local changes are waiting to be pushed + function updatePendingChanges(event: { pending: boolean }) { + hasPendingChanges.value = event.pending + } + syncService.bus.on('pendingChanges', updatePendingChanges) + onUnmounted(() => { + syncService.bus.off('pendingChanges', updatePendingChanges) + }) + + const dirty = computed(() => hasPendingChanges.value + || (document.value?.lastSavedVersion ?? 0) < versionWithChanges.value) watch(dirty, setDirty) provide(saveServiceKey, saveService) diff --git a/src/services/Outbox.ts b/src/services/Outbox.ts index f08917faf2a..3bab6151a62 100644 --- a/src/services/Outbox.ts +++ b/src/services/Outbox.ts @@ -17,6 +17,15 @@ export default class Outbox { #syncQuery = '' #recoveryAttemptCounter = 0 #isRecoveringSync = false + #onChange + + /** + * @param options options + * @param options.onChange called when `hasUpdate` changes + */ + constructor({ onChange = () => { } }: { onChange?: (hasUpdate: boolean) => void } = {}) { + this.#onChange = onChange + } storeStep(step: Uint8Array) { const encoded = encodeArrayBuffer(step) @@ -29,7 +38,7 @@ export default class Outbox { return } if (encoded < 'AQ') { - this.#syncUpdate = encoded + this.#setSyncUpdate(encoded) return } this.#awarenessUpdate = encoded @@ -58,6 +67,14 @@ export default class Outbox { return !!this.#syncUpdate } + #setSyncUpdate(update: string) { + const hadUpdate = this.hasUpdate + this.#syncUpdate = update + if (this.hasUpdate !== hadUpdate) { + this.#onChange(this.hasUpdate) + } + } + /* * Clear data that has just been sent. * @@ -66,7 +83,7 @@ export default class Outbox { */ clearSentData({ steps, awareness }: Sendable) { if (steps.includes(this.#syncUpdate)) { - this.#syncUpdate = '' + this.#setSyncUpdate('') } if (steps.includes(this.#syncQuery)) { this.#syncQuery = '' diff --git a/src/services/SaveService.ts b/src/services/SaveService.ts index 95a87d922e5..08c17cde574 100644 --- a/src/services/SaveService.ts +++ b/src/services/SaveService.ts @@ -37,18 +37,22 @@ class SaveService { lastSaveAttempt = 0 pendingAutosave = 0 getSaveData + pushChanges autosave clear constructor({ connection, getSaveData, + pushChanges = async () => {}, }: { connection: ShallowRef getSaveData: () => SaveData + pushChanges?: () => Promise }) { this.connection = connection this.getSaveData = getSaveData + this.pushChanges = pushChanges this.autosave = debounce(this._autosave.bind(this), AUTOSAVE_DEBOUNCE * 1000) this.clear = this.clearAutosave.bind(this) } @@ -67,6 +71,8 @@ class SaveService { logger.warn('Could not save due to missing connection') return } + // Save the version that includes the local changes + await this.pushChanges() const data = this.getSaveData() try { this.lastSaveAttempt = Date.now() diff --git a/src/services/SyncService.ts b/src/services/SyncService.ts index 7cb75f45728..90d79288c0c 100644 --- a/src/services/SyncService.ts +++ b/src/services/SyncService.ts @@ -63,6 +63,9 @@ export declare type EventTypes = { /* local yjs changes have been pushed to the server */ changesPushed: { version: number } + /* local yjs changes are waiting to be pushed to the server */ + pendingChanges: { pending: boolean } + /* error */ error: { type: ErrorType, data?: object } @@ -85,7 +88,10 @@ class SyncService { pushError = 0 backend?: PollingBackend #sendIntervalId?: NodeJS.Timeout - #outbox = new Outbox() + #outbox = new Outbox({ + onChange: (hasUpdate) => this.bus.emit('pendingChanges', { pending: hasUpdate }), + }) + bus = mitt() #openConnection: () => Promise #lastStepPush = Date.now() diff --git a/src/tests/composables/useSaveService.spec.ts b/src/tests/composables/useSaveService.spec.ts new file mode 100644 index 00000000000..65c6e48cb95 --- /dev/null +++ b/src/tests/composables/useSaveService.spec.ts @@ -0,0 +1,105 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import type { Editor } from '@tiptap/core' +import type { SaveService } from '../../services/SaveService.ts' +import type { EventTypes, SyncService } from '../../services/SyncService.ts' +import type { Connection } from '../../types/Connection.ts' +import type { Document } from '../../types/Document.ts' + +import { mount } from '@vue/test-utils' +import mitt from 'mitt' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { defineComponent, nextTick, shallowRef } from 'vue' +import { Doc } from 'yjs' +import * as saveApi from '../../apis/save.ts' +import { provideSaveService } from '../../composables/useSaveService.ts' + +vi.mock('../../apis/save.ts') + +const connection = shallowRef({ + documentId: '123', + sessionId: 345, + sessionToken: 'sessionToken', + filePath: './', + baseVersionEtag: 'etag', +}) + +/** + * Mount a component providing the save service with a stubbed sync service. + * + * @param options options + * @param options.version version on the stupped sync service + */ +function setup({ version = 2 } = {}) { + const syncService = { + bus: mitt(), + version, + sendRemainingSteps: vi.fn(async () => {}), + } + const editor = { on: vi.fn(), off: vi.fn() } + const setDirty = vi.fn<(val: boolean) => Promise>(async () => {}) + let saveService: SaveService | undefined + mount(defineComponent({ + template: '

', + setup() { + ({ saveService } = provideSaveService( + connection, + editor as unknown as Editor, + syncService as unknown as SyncService, + () => 'content', + new Doc(), + setDirty as unknown as (val: boolean) => Promise, + )) + }, + })) + const document = { lastSavedVersion: 1, lastSavedVersionTime: 0 } as Document + syncService.bus.emit('change', { document, sessions: [] }) + return { syncService, setDirty, saveService: saveService! } +} + +describe('provideSaveService', () => { + beforeEach(() => { + vi.useFakeTimers() + vi.mocked(saveApi.save).mockResolvedValue({ + data: { document: { lastSavedVersion: 2, lastSavedVersionTime: 0 } }, + } as Awaited>) + }) + + afterEach(() => { + vi.useRealTimers() + vi.clearAllMocks() + }) + + it('is dirty while local changes are pending', async () => { + const { syncService, setDirty } = setup() + syncService.bus.emit('pendingChanges', { pending: true }) + await nextTick() + expect(setDirty.mock.lastCall?.[0]).toBe(true) + syncService.bus.emit('pendingChanges', { pending: false }) + await nextTick() + expect(setDirty.mock.lastCall?.[0]).toBe(false) + }) + + it('autosaves changes pushed without an editor update', async () => { + const { syncService } = setup() + syncService.bus.emit('changesPushed', { version: 2 }) + await vi.advanceTimersByTimeAsync(1000) + expect(saveApi.save).toHaveBeenCalledWith(connection.value, expect.objectContaining({ version: 2 })) + }) + + it('does not autosave pushed changes that are already saved', async () => { + const { syncService } = setup() + syncService.bus.emit('changesPushed', { version: 1 }) + await vi.advanceTimersByTimeAsync(1000) + expect(saveApi.save).not.toHaveBeenCalled() + }) + + it('saves version 0 before any steps were received', async () => { + const { saveService } = setup({ version: -1 }) + await saveService.save() + expect(saveApi.save).toHaveBeenCalledWith(connection.value, expect.objectContaining({ version: 0 })) + }) +}) diff --git a/src/tests/services/Outbox.spec.ts b/src/tests/services/Outbox.spec.ts new file mode 100644 index 00000000000..c5a654ee960 --- /dev/null +++ b/src/tests/services/Outbox.spec.ts @@ -0,0 +1,48 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { describe, expect, it, vi } from 'vitest' +import Outbox from '../../services/Outbox.ts' + +// sync messages with yjs updates +const update = new Uint8Array([0, 2, 2, 0, 0]) +const otherUpdate = new Uint8Array([0, 2, 2, 0, 1]) +// sync step 1 message +const query = new Uint8Array([0, 0, 1, 0]) +// awareness message +const awareness = new Uint8Array([1, 0]) + +describe('Outbox', () => { + it('notifies when it gets an update and when the update was sent', () => { + const onChange = vi.fn() + const outbox = new Outbox({ onChange }) + outbox.storeStep(update) + outbox.storeStep(otherUpdate) + expect(onChange).toHaveBeenCalledExactlyOnceWith(true) + outbox.clearSentData(outbox.getDataToSend()) + expect(onChange).toHaveBeenLastCalledWith(false) + expect(onChange).toHaveBeenCalledTimes(2) + }) + + it('does not notify for sync queries and awareness', () => { + const onChange = vi.fn() + const outbox = new Outbox({ onChange }) + outbox.storeStep(query) + outbox.storeStep(awareness) + outbox.clearSentData(outbox.getDataToSend()) + expect(onChange).not.toHaveBeenCalled() + }) + + it('keeps the update that arrived while sending', () => { + const onChange = vi.fn() + const outbox = new Outbox({ onChange }) + outbox.storeStep(update) + const sendable = outbox.getDataToSend() + outbox.storeStep(otherUpdate) + outbox.clearSentData(sendable) + expect(onChange).toHaveBeenCalledExactlyOnceWith(true) + expect(outbox.hasUpdate).toBe(true) + }) +}) diff --git a/src/tests/services/SaveService.spec.ts b/src/tests/services/SaveService.spec.ts index 6f77eb0b9ba..e3d56bacbf0 100644 --- a/src/tests/services/SaveService.spec.ts +++ b/src/tests/services/SaveService.spec.ts @@ -44,4 +44,23 @@ describe('SaveService', () => { data: { outsideChange: 'outside content' }, }) }) + + it('pushes changes before saving', async () => { + vi.mocked(saveApi.save).mockResolvedValue({ + data: { document: { lastSavedVersion: 2 } }, + } as Awaited>) + let version = 1 + const pushChanges = vi.fn(async () => { + version = 2 + }) + const saveService = new SaveService({ + connection, + getSaveData: () => ({ ...getSaveData(), version }), + pushChanges, + }) + + expect(await saveService.save()).toBe(true) + expect(pushChanges).toHaveBeenCalledOnce() + expect(saveApi.save).toHaveBeenCalledWith(connection.value, expect.objectContaining({ version: 2 })) + }) }) diff --git a/src/tests/services/SyncService.spec.ts b/src/tests/services/SyncService.spec.ts index 4b5a07f67c2..dc630ce9860 100644 --- a/src/tests/services/SyncService.spec.ts +++ b/src/tests/services/SyncService.spec.ts @@ -4,11 +4,14 @@ */ import { describe, expect, it, vi } from 'vitest' +import { shallowRef } from 'vue' import * as connect from '../../apis/connect.ts' +import * as sync from '../../apis/sync.ts' import { provideConnection } from '../../composables/useConnection.js' import { SyncService } from '../../services/SyncService.js' vi.mock('../../apis/connect') +vi.mock('../../apis/sync') const connection = { documentId: '123', @@ -64,4 +67,30 @@ describe('Sync service', () => { expect(openHandler).toHaveBeenCalledWith(expect.objectContaining({ session: initialData.session })) expect(openData.value?.canAttachFiles).toBe(true) }) + + describe('pending changes', () => { + // sync message with an empty yjs update + const update = new Uint8Array([0, 2, 2, 0, 0]) + + it('are pending until pushed', async () => { + const service = new SyncService({ connection: shallowRef(connection), openConnection: vi.fn() }) + const onPendingChanges = vi.fn() + service.bus.on('pendingChanges', onPendingChanges) + vi.mocked(sync.push).mockResolvedValue({ data: { steps: [], documentState: '', version: 1 } }) + service.sendStep(update) + expect(onPendingChanges).toHaveBeenCalledExactlyOnceWith({ pending: true }) + await service.sendStepsNow() + expect(onPendingChanges).toHaveBeenLastCalledWith({ pending: false }) + }) + + it('remain pending if the push fails', async () => { + const service = new SyncService({ connection: shallowRef(connection), openConnection: vi.fn() }) + const onPendingChanges = vi.fn() + service.bus.on('pendingChanges', onPendingChanges) + vi.mocked(sync.push).mockRejectedValue({ code: 'ECONNABORTED' }) + service.sendStep(update) + await expect(service.sendStepsNow()).rejects.toThrow() + expect(onPendingChanges).toHaveBeenCalledExactlyOnceWith({ pending: true }) + }) + }) })