Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(collab): stop a sync reset from erasing collaborators' edits (#9564)

teo3d7301ce600d
diff --git a/packages/collaboration/src/collab/engine.ts b/packages/collaboration/src/collab/engine.ts
index f57247eeb1..611a26a589 100644
--- a/packages/collaboration/src/collab/engine.ts
+++ b/packages/collaboration/src/collab/engine.ts
@@ -236,16 +236,23 @@ export class SyncEngine<S extends GenericRootSchema, D> {
}
const generation = this.lifecycleGeneration;
- await this.syncLock.runExclusive(async () => {
- if (generation !== this.lifecycleGeneration) return;
+ const replaced = await this.syncLock.runExclusive(async () => {
+ if (generation !== this.lifecycleGeneration) return false;
this.log('info', 'engine.reset: starting');
+ // The snapshot replaces this document, and this peer's unsent edits
+ // can't be merged into it locally afterwards; deliver them first so the
+ // server's snapshot holds them. Any the server doesn't acknowledge stay
+ // in the WAL for later delivery (the canvas replays them onto the
+ // replacement).
+ await this.flushUnsentEdits();
+ if (generation !== this.lifecycleGeneration) return false;
const snapshot = await (snapshotThunk ?? this.defaultSnapshotThunk)();
- if (generation !== this.lifecycleGeneration) return;
+ if (generation !== this.lifecycleGeneration) return false;
if (snapshot.isErr()) {
this.log('error', 'engine.reset: failed to get snapshot', {
err: String(snapshot.error),
});
- return;
+ return false;
}
const resetResult = await this.loroManager.reset(snapshot.value);
@@ -253,15 +260,33 @@ export class SyncEngine<S extends GenericRootSchema, D> {
this.log('error', 'engine.reset: loro manager reset failed', {
err: JSON.stringify(resetResult.error),
});
- return;
+ return false;
}
+ if (generation !== this.lifecycleGeneration) return false;
+ if (!wasRunning) return true;
+
+ // The binding still shows the replaced document, and local edits sync
+ // its whole state. Restart and hand it the new state under this lock,
+ // so nothing the restart triggers can write the stale one back first.
+ this.start();
+ const state = this.loroManager.state?.state;
+ if (state) this.bindings.onRemoteState(state);
+ return true;
});
- if (wasRunning && generation === this.lifecycleGeneration) {
+ if (!replaced && wasRunning && generation === this.lifecycleGeneration) {
this.start();
}
}
+ private async flushUnsentEdits(): Promise<void> {
+ try {
+ await this.syncs.wal.flushPending();
+ } catch {
+ // The WAL reports its own failures.
+ }
+ }
+
public onStateUpdate(stateUpdate: StateUpdate<S> | undefined) {
if (!this._isRunning || !stateUpdate) return;
diff --git a/packages/collaboration/src/collab/manager.ts b/packages/collaboration/src/collab/manager.ts
index 3162074231..987f8c9fbd 100644
--- a/packages/collaboration/src/collab/manager.ts
+++ b/packages/collaboration/src/collab/manager.ts
@@ -92,6 +92,8 @@ export interface SyncEngineManager<
readonly doc: LoroDoc;
readonly initialized: boolean;
readonly peerId: bigint;
+ /** The latest state the manager published, if any. */
+ readonly state: StateUpdate<S> | undefined;
importUpdate(
update: LoroRawUpdate
): Result<boolean, ResultError<LoroManagerError>[]>;
diff --git a/packages/collaboration/src/collab/testing.ts b/packages/collaboration/src/collab/testing.ts
index f0721ae5ad..c25f977000 100644
--- a/packages/collaboration/src/collab/testing.ts
+++ b/packages/collaboration/src/collab/testing.ts
@@ -186,6 +186,7 @@ export class MockLoroManager implements SyncEngineManager<GenericRootSchema> {
private _initialized: boolean;
public peerId = BigInt(1);
+ public state: StateUpdate<GenericRootSchema> | undefined = undefined;
public importUpdate = vi.fn(
(
_update: LoroRawUpdate
diff --git a/packages/collaboration/src/collab/wal.ts b/packages/collaboration/src/collab/wal.ts
index 7752d08813..155d23e54b 100644
--- a/packages/collaboration/src/collab/wal.ts
+++ b/packages/collaboration/src/collab/wal.ts
@@ -238,6 +238,8 @@ export class WALSyncer<T> {
* to re-run after completing so those entries aren't stranded. */
private hasNewPending = false;
private reportedFailures = new Map<'initialization' | 'flush', string>();
+ /** Appends still writing to the store; see {@link flushPending}. */
+ private readonly writes = new Set<Promise<void>>();
public pendingFlush: Promise<void> = Promise.resolve();
private cleanupFns: Array<() => void> = [];
private deliveryOutage:
@@ -297,6 +299,22 @@ export class WALSyncer<T> {
}
public async append(item: T): Promise<void> {
+ const appended = this.appendEntry(item);
+ this.writes.add(appended);
+ try {
+ await appended;
+ } finally {
+ this.writes.delete(appended);
+ }
+ }
+
+ /** Waits for appends still writing to the store, then flushes. */
+ public async flushPending(): Promise<void> {
+ await Promise.allSettled([...this.writes]);
+ await this.flush();
+ }
+
+ private async appendEntry(item: T): Promise<void> {
await this.ready();
await this.store.append(item);
if (this.label)