Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(collab): convergance + preserve params (#5117)
teo60c2cfced7f5
diff --git a/apps/web/src/features/command/Launcher.tsx b/apps/web/src/features/command/Launcher.tsxindex 9cce4310ff..232b5e5d84 100644--- a/apps/web/src/features/command/Launcher.tsx+++ b/apps/web/src/features/command/Launcher.tsx@@ -113,7 +113,6 @@ const createBlock = async (spec: {type: blockName,id,params: createMdParams,- preserveParams: true,},mergeHistory: true,referredFrom: 'launcher',@@ -123,9 +122,7 @@ const createBlock = async (spec: {{type: blockName,id,- ...(createMdParams- ? { params: createMdParams, preserveParams: true }- : {}),+ ...(createMdParams ? { params: createMdParams } : {}),},{referredFrom: 'launcher',diff --git a/packages/collaboration/src/collab/engine.test.ts b/packages/collaboration/src/collab/engine.test.tsindex cae92914bb..a0ec0f5520 100644--- a/packages/collaboration/src/collab/engine.test.ts+++ b/packages/collaboration/src/collab/engine.test.ts@@ -1,12 +1,18 @@import { LoroDoc } from 'loro-crdt';+import { err, ResultAsync } from 'neverthrow';import { describe, expect, it, vi } from 'vitest';import { SyncEngine } from './engine';+import { LoroManagerError } from './manager';+import type { RawUpdate } from './shared';+import { createNoopLiveSyncSource } from './source';import {MockChatter,MockLiveSyncSource,MockLoroManager,+ MockWALStore,makeTestWAL,} from './testing';+import { WALSyncer } from './wal';const emptySnapshot = () => new LoroDoc().export({ mode: 'snapshot' });@@ -200,4 +206,120 @@ describe('SyncEngine', () => {expect(started).toBe(false);expect(source.registerPeerId).not.toHaveBeenCalled();});++ it('skips convergence payloads with zero bytes (noop live source)', async () => {+ // Non-propagating AI edit sessions run the engine over a noop live+ // source whose requestUpdatesSince/requestSnapshot answer with an empty+ // Uint8Array — not a valid Loro payload. Startup convergence must skip+ // it rather than import → throw → reset in a loop.+ const live = createNoopLiveSyncSource('doc-1');+ const wal = new WALSyncer<RawUpdate>(+ new MockWALStore<RawUpdate>(),+ (updates) => live.pushUpdate(updates)+ );+ const manager = new MockLoroManager();+ const engine = new SyncEngine({+ loroManager: manager,+ awareness: makeAwareness(),+ syncs: { wal, live },+ bindings: { onRemoteState: vi.fn() },+ });++ engine.start();+ await new Promise((resolve) => setTimeout(resolve));++ expect(manager.importUpdate).not.toHaveBeenCalled();+ expect(manager.reset).not.toHaveBeenCalled();+ expect(engine.isRunning).toBe(true);+ engine.stop();+ wal.destroy();+ });++ it('converges instead of resetting when a remote update is causally pending', async () => {+ const source = new MockLiveSyncSource();+ const { wal } = makeTestWAL(source);+ const manager = new MockLoroManager();+ const engine = new SyncEngine({+ loroManager: manager,+ awareness: makeAwareness(),+ syncs: { wal, live: source },+ bindings: { onRemoteState: vi.fn() },+ });++ engine.start();+ // Let the startup convergence fully settle, so the pending-triggered+ // convergence below isn't coalesced onto it.+ await new Promise((resolve) => setTimeout(resolve));++ manager.importUpdate.mockReturnValueOnce(+ err([{ code: LoroManagerError.ImportPending, message: 'pending' }])+ );+ source.emit({ type: 'update', update: new Uint8Array([9]) });++ // A pending import triggers a fresh convergence, not a reset.+ await vi.waitFor(() =>+ expect(source.requestUpdatesSince).toHaveBeenCalledTimes(2)+ );+ expect(manager.reset).not.toHaveBeenCalled();+ expect(source.requestSnapshot).not.toHaveBeenCalled();+ });++ it('runs a fresh convergence for a reconnect that arrives mid-flight', async () => {+ const source = new MockLiveSyncSource();+ const firstRequest = Promise.withResolvers<Uint8Array<ArrayBuffer>>();+ source.requestUpdatesSince.mockReturnValueOnce(+ ResultAsync.fromSafePromise(firstRequest.promise)+ );+ const { wal } = makeTestWAL(source);+ const manager = new MockLoroManager();+ const engine = new SyncEngine({+ loroManager: manager,+ awareness: makeAwareness(),+ syncs: { wal, live: source },+ bindings: { onRemoteState: vi.fn() },+ });++ engine.start();+ expect(source.requestUpdatesSince).toHaveBeenCalledOnce();++ // Reconnect while the startup convergence is still in flight: coalesced+ // for now, but a fresh pass must follow once the in-flight one settles.+ source.emit({+ type: 'reconnect',+ snapshot: emptySnapshot(),+ awareness: new Uint8Array(),+ });+ expect(source.requestUpdatesSince).toHaveBeenCalledOnce();++ firstRequest.resolve(new Uint8Array());+ await vi.waitFor(() =>+ expect(source.requestUpdatesSince).toHaveBeenCalledTimes(2)+ );+ });++ it('does not apply a late startup convergence response after stop', async () => {+ const source = new MockLiveSyncSource();+ const pendingUpdate = Promise.withResolvers<Uint8Array<ArrayBuffer>>();+ source.requestUpdatesSince.mockReturnValueOnce(+ ResultAsync.fromSafePromise(pendingUpdate.promise)+ );+ const { wal } = makeTestWAL(source);+ const manager = new MockLoroManager();+ const engine = new SyncEngine({+ loroManager: manager,+ awareness: makeAwareness(),+ syncs: { wal, live: source },+ bindings: { onRemoteState: vi.fn() },+ });++ engine.start();+ expect(source.requestUpdatesSince).toHaveBeenCalledOnce();+ engine.stop();++ pendingUpdate.resolve(new Uint8Array([1, 2, 3]));+ await pendingUpdate.promise;+ await Promise.resolve();++ expect(manager.importUpdate).not.toHaveBeenCalled();+ });});diff --git a/packages/collaboration/src/collab/engine.ts b/packages/collaboration/src/collab/engine.tsindex 24848c6914..bba110415f 100644--- a/packages/collaboration/src/collab/engine.ts+++ b/packages/collaboration/src/collab/engine.ts@@ -14,6 +14,7 @@ import type { Awareness } from './awareness';import { BroadcastChannelChatter, type Chatter, noopChatter } from './chatter';import { logSyncService } from './logger';import {+ LoroManagerError,LoroStateTag,type StateUpdate,type SyncEngineManager,@@ -68,6 +69,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {private readonly readonly: () => boolean;private readonly syncLock = new Mutex();private unsubscribe?: () => void;+ private liveUnsubscribe?: () => void;private snapshotInterval?: ReturnType<typeof setInterval>;private readonly snapshotStore?: LoroSnapshotStore;private readonly defaultSnapshotThunk: SnapshotThunk;@@ -75,6 +77,12 @@ export class SyncEngine<S extends GenericRootSchema, D> {private readonly makeChatter: (documentId: string) => Chatter;private chatter?: Chatter;private chatterUnsub?: () => void;+ private lifecycleGeneration = 0;+ private convergence?: {+ generation: number;+ promise: Promise<void>;+ rerunRequested: boolean;+ };constructor({loroManager,@@ -133,7 +141,13 @@ export class SyncEngine<S extends GenericRootSchema, D> {.exhaustive());- this.syncs.live.listen((event) => this.handleSourceEvent(event));+ this._isRunning = true;+ this.lifecycleGeneration++;++ this.liveUnsubscribe?.();+ this.liveUnsubscribe = this.syncs.live.listen((event) =>+ this.handleSourceEvent(event)+ );this.syncs.live.registerPeerId(this.loroManager.peerId);if (this.snapshotStore && this.snapshotInterval === undefined) {@@ -143,16 +157,22 @@ export class SyncEngine<S extends GenericRootSchema, D> {);}- this._isRunning = true;this.onRunningChange(true);this.log('info', 'engine.start: ok');+ void this.convergeFromServer();return true;}public stop() {+ this._isRunning = false;+ this.lifecycleGeneration++;+this.unsubscribe?.();this.unsubscribe = undefined;+ this.liveUnsubscribe?.();+ this.liveUnsubscribe = undefined;+this.chatterUnsub?.();this.chatter?.close();this.chatterUnsub = undefined;@@ -165,7 +185,6 @@ export class SyncEngine<S extends GenericRootSchema, D> {this.awareness.updateLocalAwareness(undefined);this.syncs.live.pushAwareness(this.awareness.getEncodedLocalAwareness());- this._isRunning = false;this.onRunningChange(false);this.log('info', 'engine.stop: ok');}@@ -280,6 +299,20 @@ export class SyncEngine<S extends GenericRootSchema, D> {const importResult = this.loroManager.importUpdate(update);await Promise.resolve();if (importResult.isErr()) {+ const pendingOnly = importResult.error.every(+ (e) => e.code === LoroManagerError.ImportPending+ );+ if (pendingOnly) {+ // The update arrived ahead of its causal deps; Loro holds it and+ // applies it once the gap fills. Pull the gap from the server+ // instead of resetting. No rerun: if the gap's ops haven't reached+ // the server yet (e.g. another tab's unflushed WAL), rerunning+ // would poll in a tight loop; the held ops apply whenever the deps+ // arrive through any channel.+ this.log('info', 'engine: remote update pending on missing ops');+ void this.convergeFromServer();+ return;+ }this.log('error', 'engine: failed to import remote update, resetting', {err: importResult,});@@ -308,17 +341,63 @@ export class SyncEngine<S extends GenericRootSchema, D> {'info','engine: reconnect, requesting updates since current version');- this.requestAndHandleUpdatesSince(this.loroManager.doc.version());+ void this.convergeFromServer({ rerunIfInFlight: true });break;}}+ /**+ * Pull every server operation missing from the current local seed.+ *+ * Snapshot sources are intentionally transport-agnostic and first-wins, so+ * this anti-entropy step is what makes optimistic, IDB, and S3 seeds converge+ * to server truth. Calls within one engine lifecycle are coalesced;+ * `rerunIfInFlight` additionally queues one fresh pass after the in-flight+ * one settles, so a reconnect is never absorbed into an attempt that goes+ * on to exhaust its retries.+ */+ private convergeFromServer({+ rerunIfInFlight = false,+ }: {+ rerunIfInFlight?: boolean;+ } = {}): Promise<void> {+ const generation = this.lifecycleGeneration;+ if (!this.isActiveGeneration(generation)) return Promise.resolve();++ const inFlight = this.convergence;+ if (inFlight?.generation === generation) {+ if (rerunIfInFlight) inFlight.rerunRequested = true;+ return inFlight.promise;+ }++ const since = this.loroManager.doc.version();+ const promise = this.requestAndHandleUpdatesSince(since, 1, generation);+ const record = { generation, promise, rerunRequested: false };+ this.convergence = record;+ const settle = () => {+ if (this.convergence !== record) return;+ this.convergence = undefined;+ if (record.rerunRequested) void this.convergeFromServer();+ };+ void promise.then(settle, settle);+ return promise;+ }++ private isActiveGeneration(generation: number): boolean {+ return this._isRunning && this.lifecycleGeneration === generation;+ }+private async requestAndHandleUpdatesSince(since: VersionVector,- attempt = 1+ attempt: number,+ generation: number) {+ if (!this.isActiveGeneration(generation)) return;+this.log('debug', `engine: requestUpdatesSince (attempt ${attempt})`);const updates = await this.syncs.live.requestUpdatesSince(since);+ if (!this.isActiveGeneration(generation)) return;+if (updates.isErr() || !updates.value) {this.log('error', 'engine: requestUpdatesSince failed', {err: 'error' in updates ? updates.error : 'update is undefined',@@ -327,13 +406,22 @@ export class SyncEngine<S extends GenericRootSchema, D> {await new Promise((resolve) =>setTimeout(resolve, REQUEST_UPDATES_RETRY_DELAY_MS));- void this.requestAndHandleUpdatesSince(since, attempt + 1);+ await this.requestAndHandleUpdatesSince(since, attempt + 1, generation);}return;}+ if (updates.value.length === 0) {+ // Nothing to converge. Zero bytes are not a valid Loro payload (real+ // sources encode "no new ops" as a non-empty update), so importing them+ // would throw and trigger a reset — the noop live source used by+ // non-propagating AI edit sessions answers with exactly this.+ this.log('debug', 'engine: requestUpdatesSince ok, empty update');+ return;+ }+this.log('debug', 'engine: requestUpdatesSince ok, applying update');- this.handleRemoteUpdate(updates.value);+ await this.handleRemoteUpdate(updates.value);}}diff --git a/packages/collaboration/src/collab/manager-integration.test.ts b/packages/collaboration/src/collab/manager-integration.test.tsindex f13ccc54f4..12c25b7592 100644--- a/packages/collaboration/src/collab/manager-integration.test.ts+++ b/packages/collaboration/src/collab/manager-integration.test.ts@@ -1,9 +1,12 @@import { Mirror, schema } from '@loro-mirror/core';-import { LoroDoc } from 'loro-crdt';+import { LoroDoc, type VersionVector } from 'loro-crdt';import { createRoot } from 'solid-js';-import { describe, expect, it } from 'vitest';+import { describe, expect, it, vi } from 'vitest';+import { noopChatter } from './chatter';+import { createSyncEngine } from './engine';import { createLoroManager, type LoroManager } from './manager';import { TestServer } from './test-utils/test-server';+import { InMemoryWALStore, WALSyncer } from './wal';// mostly dummy schema that's flat so it's easy for testsconst TEST_SCHEMA = schema({@@ -43,6 +46,104 @@ function pushToServer(}describe('LoroManager seed + converge — two-client merge', () => {+ it('converges a stale local seed on engine startup without losing offline edits', async () => {+ const initialSnapshotX = await buildSnapshot([{ id: 'p1', text: 'X ' }]);+ const server = new TestServer();+ server.applyUpdate(initialSnapshotX);+ const live = server.asLiveSyncSource();+ const wal = new WALSyncer(new InMemoryWALStore<Uint8Array>(), (updates) =>+ live.pushUpdate(updates)+ );+ const onRemoteState = vi.fn();+ const { clientA, offlineClientB, reconnectedB, syncEngine, dispose } =+ createRoot((dispose) => {+ const reconnectedB = createLoroManager(TEST_SCHEMA, {+ documentId: 'test-doc-b-reconnected',+ });+ return {+ clientA: createLoroManager(TEST_SCHEMA, {+ documentId: 'test-doc-a',+ }),+ offlineClientB: createLoroManager(TEST_SCHEMA, {+ documentId: 'test-doc-b-offline',+ }),+ reconnectedB,+ syncEngine: createSyncEngine({+ loroManager: reconnectedB,+ awareness: {+ local: () => undefined,+ updateLocalAwareness: vi.fn(),+ getEncodedLocalAwareness: vi.fn(() => new Uint8Array()),+ importRemoteAwareness: vi.fn(),+ } as any,+ syncs: { wal, live },+ bindings: { onRemoteState },+ makeChatter: () => noopChatter(),+ }),+ dispose,+ };+ });++ await clientA.ingest({ kind: 'dss', snapshot: initialSnapshotX });+ await offlineClientB.ingest({+ kind: 'dss',+ snapshot: initialSnapshotX,+ });++ await offlineClientB.syncToLoro({+ paragraphs: [{ id: 'p1', text: 'X online-B ' }],+ });+ pushToServer(offlineClientB, server);++ await offlineClientB.syncToLoro({+ paragraphs: [{ id: 'p1', text: 'X online-B offline-B ' }],+ });+ const staleLocalSnapshot = offlineClientB.doc.export({+ mode: 'snapshot',+ });+ const versionAfterOfflineEdit = offlineClientB.doc.version();++ clientA.importUpdate(server.doc.export({ mode: 'update' }));+ await Promise.resolve();+ await clientA.syncToLoro({+ paragraphs: [{ id: 'p1', text: 'X online-B online-A ' }],+ });+ pushToServer(clientA, server);++ await reconnectedB.ingest({+ kind: 'local',+ snapshot: staleLocalSnapshot,+ });++ syncEngine.start();++ await vi.waitFor(() => {+ const finalText = paragraphTexts(reconnectedB)[0]!;+ expect(finalText).toContain('X');+ expect(finalText).toContain('online-B');+ expect(finalText).toContain('online-A');+ expect(finalText).toContain('offline-B');+ });+ expect(live.requestUpdatesSince).toHaveBeenCalledOnce();+ const requestedVersion = (+ live.requestUpdatesSince as unknown as {+ mock: { calls: [[VersionVector]] };+ }+ ).mock.calls[0][0];+ expect(requestedVersion.toJSON()).toEqual(versionAfterOfflineEdit.toJSON());++ await vi.waitFor(() => {+ const remoteText = onRemoteState.mock.lastCall?.[0].paragraphs[0].text;+ expect(remoteText).toContain('online-B');+ expect(remoteText).toContain('online-A');+ expect(remoteText).toContain('offline-B');+ });++ syncEngine.stop();+ wal.destroy();+ dispose();+ });+it('two clients seeded from the same snapshot converge after concurrent edits', async () => {await createRoot(async (dispose) => {const initialSnapshotX = await buildSnapshot([{ id: 'p1', text: 'X ' }]);@@ -84,4 +185,35 @@ describe('LoroManager seed + converge — two-client merge', () => {dispose();});});++ it('reports a causally pending update instead of treating it as a no-op', async () => {+ await createRoot(async (dispose) => {+ const source = new LoroDoc();+ source.setPeerId(7n);+ const text = source.getText('pending-test');+ text.insert(0, 'first');+ source.commit();+ const afterFirst = source.version();++ text.insert(text.length, ' second');+ source.commit();+ const updateMissingItsPredecessor = source.export({+ mode: 'update',+ from: afterFirst,+ });++ const manager = createLoroManager(TEST_SCHEMA, {+ documentId: 'test-doc-pending',+ });+ await manager.ingest({+ kind: 'dss',+ snapshot: await buildSnapshot([]),+ });++ const result = manager.importUpdate(updateMissingItsPredecessor);++ expect(result.isErr()).toBe(true);+ dispose();+ });+ });});diff --git a/packages/collaboration/src/collab/manager.ts b/packages/collaboration/src/collab/manager.tsindex 744ed6edcd..34f81f4240 100644--- a/packages/collaboration/src/collab/manager.ts+++ b/packages/collaboration/src/collab/manager.ts@@ -22,6 +22,9 @@ import type { GenericRootSchema, LoroRawUpdate, RawUpdate } from './shared';export enum LoroManagerError {ImportFailed = 'IMPORT_FAILED',+ /** The update arrived ahead of its causal dependencies. Loro holds it and+ * applies it automatically once the gap fills — not a failure. */+ ImportPending = 'IMPORT_PENDING',NotInitialized = 'NOT_INITIALIZED',InitializeFailed = 'INITIALIZE_FAILED',SyncFailed = 'SYNC_FAILED',@@ -56,6 +59,24 @@ export type LoroManagerOptions = {documentId: string;};+/** Map Loro's {@link ImportStatus} onto our Result: ok(didChange), or+ * {@link LoroManagerError.ImportPending} when ops were held back waiting on+ * missing causal dependencies. */+function importStatusToResult(+ importStatus: ImportStatus+): Result<boolean, ResultError<LoroManagerError>[]> {+ if ((importStatus.pending?.size ?? 0) > 0) {+ return err([+ {+ code: LoroManagerError.ImportPending,+ message: 'Import held back pending missing causal dependencies',+ },+ ]);+ }++ return ok(importStatus.success.size > 0);+}+export interface SyncEngineManager<S extends GenericRootSchema = GenericRootSchema,> {@@ -227,15 +248,7 @@ export class LoroManager<S extends GenericRootSchema = GenericRootSchema>]);}- const didChange = Object.keys(importStatus.success).length > 0;-- if (Object.keys(importStatus.pending ?? {}).length > 0) {- return err([- { code: LoroManagerError.ImportFailed, message: 'Import failed' },- ]);- }-- return ok(didChange);+ return importStatusToResult(importStatus);}importBatchUpdates(@@ -260,15 +273,7 @@ export class LoroManager<S extends GenericRootSchema = GenericRootSchema>]);}- const didChange = Object.keys(importStatus.success).length > 0;-- if (Object.keys(importStatus.pending ?? {}).length > 0) {- return err([- { code: LoroManagerError.ImportFailed, message: 'Import failed' },- ]);- }-- return ok(didChange);+ return importStatusToResult(importStatus);}async initializeFromSnapshot(@@ -439,13 +444,10 @@ export class LoroManager<S extends GenericRootSchema = GenericRootSchema>]);}- if (Object.keys(importStatus.pending ?? {}).length > 0) {- return err([- {- code: LoroManagerError.ImportFailed,- message: 'Snapshot import has pending updates',- },- ]);+ const statusResult = importStatusToResult(importStatus);+ if (statusResult.isErr()) {+ // A reset snapshot must be self-contained; pending deps mean a bad seed.+ return err(statusResult.error);}const newMirror = createMirror(newDoc, this.schema);diff --git a/packages/collaboration/src/collab/snapshot-store.ts b/packages/collaboration/src/collab/snapshot-store.tsindex 34a3781050..b315a39081 100644--- a/packages/collaboration/src/collab/snapshot-store.ts+++ b/packages/collaboration/src/collab/snapshot-store.ts@@ -1,6 +1,6 @@import { type DBSchema, type IDBPDatabase, openDB as idbOpen } from 'idb';import { logSyncService } from './logger';-import type { LoroManager } from './manager';+import { type LoroManager, LoroManagerError } from './manager';import type { GenericRootSchema, RawUpdate } from './shared';import type { WALStore } from './wal';@@ -100,6 +100,20 @@ export async function loadCachedState<S extends GenericRootSchema>(for (const entry of pending) {const importResult = loroManager.importUpdate(entry.update);if (importResult.isErr()) {+ const pendingOnly = importResult.error.every(+ (e) => e.code === LoroManagerError.ImportPending+ );+ if (pendingOnly) {+ // Loro holds the entry until its causal gap fills, so keep replaying:+ // a later WAL entry may fill the gap, and held ops apply on their own.+ logSyncService({+ documentId: 'unknown',+ level: 'warn',+ context: { misc: { entryId: entry.id } },+ message: 'snapshot-store: WAL entry pending during cold load',+ });+ continue;+ }// Stop replaying. Skipped entries are safe: delivered ones are on the// server (server sync will bring them back) and undelivered ones are// still in the WAL (next edit or reconnect will flush them).diff --git a/packages/collaboration/src/collab/testing.ts b/packages/collaboration/src/collab/testing.tsindex 405baf7691..f0721ae5ad 100644--- a/packages/collaboration/src/collab/testing.ts+++ b/packages/collaboration/src/collab/testing.ts@@ -1,7 +1,12 @@-import { ok, okAsync } from 'neverthrow';+import { ok, okAsync, type Result } from 'neverthrow';import { vi } from 'vitest';+import type { ResultError } from '../internal/result';import type { Chatter, ChatterMessage } from './chatter';-import type { StateUpdate, SyncEngineManager } from './manager';+import type {+ LoroManagerError,+ StateUpdate,+ SyncEngineManager,+} from './manager';import type { GenericRootSchema, LoroRawUpdate, RawUpdate } from './shared';import type { SnapshotStore } from './snapshot-store';import type { SyncSourceEvent } from './source';@@ -181,7 +186,11 @@ export class MockLoroManager implements SyncEngineManager<GenericRootSchema> {private _initialized: boolean;public peerId = BigInt(1);- public importUpdate = vi.fn(() => ok(true));+ public importUpdate = vi.fn(+ (+ _update: LoroRawUpdate+ ): Result<boolean, ResultError<LoroManagerError>[]> => ok(true)+ );public syncToLoro = vi.fn(async () => ok(undefined as void));public reset = vi.fn(async () => ok(undefined as void));diff --git a/packages/collaboration/src/sync-service/source.test.ts b/packages/collaboration/src/sync-service/source.test.tsindex 3b724fd576..f77b02a1ed 100644--- a/packages/collaboration/src/sync-service/source.test.ts+++ b/packages/collaboration/src/sync-service/source.test.ts@@ -145,6 +145,21 @@ describe('SyncServiceSource', () => {expect(events).toEqual([{ type: 'update', update: bytes }]);});+ it('drops events arriving after the last listener detaches instead of buffering', () => {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1');+ const events: SyncSourceEvent[] = [];+ const unsubscribe = src.listen((e) => events.push(e));+ unsubscribe();++ ws.deliver(remote.update(new Uint8Array([7]))); // engine stopped — dropped++ const eventsAfterRestart: SyncSourceEvent[] = [];+ src.listen((e) => eventsAfterRestart.push(e));+ expect(events).toEqual([]);+ expect(eventsAfterRestart).toEqual([]);+ });+it('pushUpdate resolves false before initial sync', async () => {const ws = new FakeSocket();const src = new SyncServiceSource(ws, 'doc1');diff --git a/packages/collaboration/src/sync-service/source.ts b/packages/collaboration/src/sync-service/source.tsindex 874df83d71..209e21f8a5 100644--- a/packages/collaboration/src/sync-service/source.ts+++ b/packages/collaboration/src/sync-service/source.ts@@ -87,8 +87,11 @@ export class SyncServiceSource implements LiveSyncSource {private readonly ws: SyncSocket;private readonly newId: () => string;private readonly listeners = new Set<(event: SyncSourceEvent) => void>();- /** Events that arrive before any listener attaches; flushed on `listen()`. */+ /** Events that arrive before the first listener attaches; flushed on+ * `listen()`. Once a listener has attached, listener-less windows (e.g. a+ * stopped engine) drop events instead — see `emit`. */private readonly buffered: SyncSourceEvent[] = [];+ private hasEverHadListener = false;/** Pending request/response waiters, matched against each incoming message. */private readonly waiters = new Set<MessageWaiter>();private initialSyncReceived = false;@@ -150,6 +153,7 @@ export class SyncServiceSource implements LiveSyncSource {this.initialSyncPromise;public listen: LiveSyncSource['listen'] = (listener) => {+ this.hasEverHadListener = true;this.listeners.add(listener);// Flush anything that arrived before the first listener attached.if (this.buffered.length > 0) {@@ -245,7 +249,11 @@ export class SyncServiceSource implements LiveSyncSource {private emit(event: SyncSourceEvent) {if (this.listeners.size === 0) {- this.buffered.push(event);+ // Buffer only the pre-first-listen window (source construction → engine+ // start). After a listener has detached (engine stopped), drop events —+ // buffering here is unbounded, and a restarted engine converges from+ // the server instead of replaying a stale backlog.+ if (!this.hasEverHadListener) this.buffered.push(event);return;}for (const listener of this.listeners) listener(event);