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(sync): stop reopened markdown docs from silently dropping edits (#9257)
teodaff429fafd2
diff --git a/apps/web/src/features/drive-view/components/FileDetail.tsx b/apps/web/src/features/drive-view/components/FileDetail.tsxindex 45ef9d698a..6d6ce651c5 100644--- a/apps/web/src/features/drive-view/components/FileDetail.tsx+++ b/apps/web/src/features/drive-view/components/FileDetail.tsx@@ -25,6 +25,7 @@ import {ErrorBoundary,type JSX,Match,+ onCleanup,type ParentProps,Suspense,Switch,@@ -104,12 +105,54 @@ export function FileDetailLoadGate<entityType: string;label: string;load: (documentId: string) => Promise<T>;+ /**+ * Releases what a load opened, such as a markdown document's sync source,+ * once its result can no longer be shown: its content replaced or removed,+ * superseded by a newer load, resolved after the gate unmounted, or on+ * unmount.+ */+ dispose?: (data: T) => void;children: (data: T) => JSX.Element;}) {const analytics = useAnalytics();+ // Shown results are released when their content is disposed (after the+ // editor it rendered); the rest here. Each result is released once.+ const released = new WeakSet<T>();+ const release = (data: T | undefined) => {+ if (data === undefined || released.has(data)) return;+ released.add(data);+ props.dispose?.(data);+ };+ let unmounted = false;+ let latestLoad = 0;+ let current: T | undefined;+ const load = async (documentId: string): Promise<T | undefined> => {+ const request = ++latestLoad;+ const data = await props.load(documentId);+ // The resource ignores a superseded result; one unloaded while pending+ // (access revoked) must not become current either, so a later grant finds+ // nothing loaded and reloads. Nothing will show it, so release it now.+ if (unmounted || request !== latestLoad) {+ release(data);+ return undefined;+ }+ current = data;+ return data;+ };+ const unload = () => {+ latestLoad++;+ const previous = current;+ current = undefined;+ mutate(undefined);+ release(previous);+ };+ onCleanup(() => {+ unmounted = true;+ release(current);+ });const [document, { refetch, mutate }] = createResource(() => props.documentId,- props.load+ load);// Counts the open once per document, as BlockLoader does for blocks; access// changes and retries reload the same document without reopening it.@@ -127,14 +170,14 @@ export function FileDetailLoadGate<});const reload = () => {// Dispose the old editor and its sync source before loading a new token.- mutate(undefined);+ unload();fireAndForget(refetch(), 'drive-view');};const sharedAccess = useSharedAccessUpdates(() => shareEntity('document', props.documentId),(level) => {if (level === null) {- mutate(undefined);+ unload();return;}const loaded = document.state === 'ready' ? document.latest : undefined;@@ -168,21 +211,25 @@ export function FileDetailLoadGate</>)}</Match>- <Match when={document()}>- {(data) => (- <ErrorBoundary- fallback={(error, reset) => (- <FileDetailBodyState- label={props.label}- error={error}- actionLabel="Reset"- onAction={reset}- />- )}- >- {props.children(data())}- </ErrorBoundary>- )}+ <Match when={document()} keyed>+ {(data) => {+ // Runs after this content, and the editor it renders, is disposed.+ onCleanup(() => release(data));+ return (+ <ErrorBoundary+ fallback={(error, reset) => (+ <FileDetailBodyState+ label={props.label}+ error={error}+ actionLabel="Reset"+ onAction={reset}+ />+ )}+ >+ {props.children(data)}+ </ErrorBoundary>+ );+ }}</Match></Switch></Suspense>diff --git a/apps/web/src/features/drive-view/views/MarkdownDetail.tsx b/apps/web/src/features/drive-view/views/MarkdownDetail.tsxindex ab3ff889a6..61f8b10cf7 100644--- a/apps/web/src/features/drive-view/views/MarkdownDetail.tsx+++ b/apps/web/src/features/drive-view/views/MarkdownDetail.tsx@@ -154,6 +154,7 @@ export function MarkdownDetail(props: MarkdownDetailProps) {entityType="md"label={props.kind === 'task' ? 'task' : 'document'}load={loadMarkdownDocument}+ dispose={(data) => data.source.cleanup()}>{(data) => (<MarkdownDetailContentdiff --git a/docs/architecture/sync-router.md b/docs/architecture/sync-router.mdindex ebf460ad9c..d5d4ab2e65 100644--- a/docs/architecture/sync-router.md+++ b/docs/architecture/sync-router.md@@ -29,6 +29,15 @@ id and forwards the inner bytes unchanged. Existing 900,000-byte chunking andper-document reassembly remain in place under the envelope. Text `ping`/`pong`heartbeats apply to the dedicated sync socket.+The router keeps one route per document per socket, but every caller of+`attach()` gets its own virtual socket. A source learns the document only from+`RemoteInitialSync`, which the Durable Object sends once per downstream+connection, so a caller joining an open route re-subscribes it. Callers already+on the route see a close, as on an upstream drop, then a reconnect, and+converge. The route unsubscribes when its last virtual socket closes. Callers+still close their source when their view goes away; a source that is never+closed keeps its route open.+Each client socket owns its routes and downstream tasks. Any DSS replica canaccept a connection; reconnecting to another replica creates new routes andre-subscribes with refreshed document tokens. Replicas share no routing state.diff --git a/packages/collaboration/src/sync-service/router.ts b/packages/collaboration/src/sync-service/router.tsindex 1809942632..ec1b1fc4dc 100644--- a/packages/collaboration/src/sync-service/router.ts+++ b/packages/collaboration/src/sync-service/router.ts@@ -31,8 +31,9 @@ import type { SyncSocket } from './socket';* DSS host routes inbound frames through its embedded router, which holds* one upstream connection per (socket, document).*- * Each document gets a {@link SyncSocket}-compatible virtual socket, so- * `SyncServiceSource` works unmodified on top of this transport.+ * Each caller attaching a document gets its own {@link SyncSocket}-compatible+ * virtual socket, so `SyncServiceSource` works unmodified on top of this+ * transport.*/export class SyncRouterTransport {private readonly getUrl: UrlResolver;@@ -45,8 +46,9 @@ export class SyncRouterTransport {}/**- * Attach a document to the shared socket: subscribes immediately and- * returns the virtual socket for it. `refreshToken` is consulted on every+ * Attach a document to the shared socket and return a socket for this+ * caller. Callers of one document share its route, but each gets its own+ * socket, closed independently. `refreshToken` is consulted on every* re-subscribe (upstream drop, router reconnect), mirroring how the direct* socket refetches a permission token per reconnect.*/@@ -56,22 +58,18 @@ export class SyncRouterTransport {refreshToken: () => Promise<string>): SyncSocket {const existing = this.docs.get(documentId);- if (existing) return existing;+ if (existing) return existing.addOwner(initialToken, refreshToken);- const virtual = new VirtualSyncSocket(- this,- documentId,- initialToken,- refreshToken- );+ const virtual = new VirtualSyncSocket(this, documentId, initialToken);this.docs.set(documentId, virtual);+ const socket = virtual.addOwner(undefined, refreshToken);virtual.subscribe();- return virtual;+ return socket;}- /** Called by a virtual socket when it is closed for good. */- public detach(documentId: string): void {- this.docs.delete(documentId);+ /** Called by a virtual socket when its last owner closes it. */+ public detach(documentId: string, virtual: VirtualSyncSocket): void {+ if (this.docs.get(documentId) === virtual) this.docs.delete(documentId);}/** The shared router websocket, built lazily on first use. */@@ -147,23 +145,26 @@ const RESUBSCRIBE_BASE_MS = 500;const RESUBSCRIBE_MAX_MS = 8_000;/**- * A {@link SyncSocket} for one document, riding the shared router socket.+ * One document's route on the shared router socket, shared by every caller+ * that attached the document. Each caller holds its own+ * {@link OwnedSyncSocket}; the route unsubscribes when the last one closes.** Lifecycle mapping (virtual event ← router/envelope cause):* - Open ← first `RouterSubscribed`* - Message ← `RouterDocFrame` (decoded to `FromRemote`)* - Close ← `RouterSubscribeFailed` / `RouterDocClosed` / router close- * - Reconnect ← any later `RouterSubscribed` (upstream or router recovery)+ * - Reconnect ← any later `RouterSubscribed` (upstream or router recovery,+ * or a new caller joining an open route)*/-class VirtualSyncSocket implements SyncSocket {+class VirtualSyncSocket {private readonly transport: SyncRouterTransport;private readonly documentId: string;- private readonly refreshToken: () => Promise<string>;private initialToken: string | undefined;private reassembler = new Reassembler();private subscriptionAttempt = 0;- private readonly listeners: ListenerSets = {};+ /** Callers sharing this route, in join order. */+ private readonly owners = new Set<OwnedSyncSocket>();private state: 'subscribing' | 'open' | 'dropped' | 'closed' = 'subscribing';private wasEverOpen = false;private resubscribeTimer: ReturnType<typeof setTimeout> | undefined;@@ -174,13 +175,67 @@ class VirtualSyncSocket implements SyncSocket {public constructor(transport: SyncRouterTransport,documentId: string,- initialToken: string | undefined,- refreshToken: () => Promise<string>+ initialToken: string | undefined) {this.transport = transport;this.documentId = documentId;this.initialToken = initialToken;- this.refreshToken = refreshToken;+ }++ /**+ * Give another caller its own socket on this route. A sync source learns+ * the document only from `RemoteInitialSync`, which the Durable Object sends+ * once per downstream connection, so a caller joining an open route+ * re-subscribes it; the callers already on it see a close, then a+ * reconnect.+ */+ public addOwner(+ initialToken: string | undefined,+ refreshToken: () => Promise<string>+ ): SyncSocket {+ const owner = new OwnedSyncSocket(this, refreshToken);+ const current = [...this.owners];+ // Registered first, so the route outlives any current owner that closes+ // during the close below.+ this.owners.add(owner);+ // A route still subscribing (or waiting to) delivers its initial sync to+ // every owner, including this one.+ if (current.length === 0 || this.state !== 'open') return owner;++ // Re-subscribing replaces the server's route, dropping its in-flight acks+ // and replies. Start the new subscription first, so anything sent from+ // here on waits for it, then close the current owners as an upstream drop+ // would, so their pending waits end now instead of timing out.+ this.initialToken = initialToken;+ this.resubscribe();+ const close = new CloseEvent(WebsocketEvent.Close, {+ code: 4000,+ reason: 'resubscribing for another caller',+ wasClean: false,+ });+ for (const other of current) other.dispatch(WebsocketEvent.Close, close);+ return owner;+ }++ /** Called by an owner's `close()`; the last one closes the route. */+ public release(owner: OwnedSyncSocket): void {+ if (!this.owners.delete(owner) || this.owners.size > 0) return;+ this.clearResubscribeTimer();+ // Even a dropped route may still be live server-side (a re-subscribe that+ // failed to authorize never replaced it); unsubscribing an unknown route+ // is a no-op for the router.+ if (+ this.state !== 'closed' &&+ this.transport.socket().connectionState === WebsocketConnectionState.Open+ ) {+ this.transport+ .socket()+ .send(ToRouter.fromRouterUnsubscribe({ docId: this.documentId }));+ }+ this.state = 'closed';+ this.subscriptionAttempt++;+ this.pending = [];+ this.transport.detach(this.documentId, this);}public get connectionState(): WebsocketConnectionState {@@ -214,50 +269,6 @@ class VirtualSyncSocket implements SyncSocket {if (this.state === 'dropped') this.resubscribe();}- public close(): void {- if (this.state === 'closed') return;- this.clearResubscribeTimer();- if (- this.state !== 'dropped' &&- this.transport.socket().connectionState === WebsocketConnectionState.Open- ) {- this.transport- .socket()- .send(ToRouter.fromRouterUnsubscribe({ docId: this.documentId }));- }- this.state = 'closed';- this.subscriptionAttempt++;- this.pending = [];- this.transport.detach(this.documentId);- }-- public addEventListener<K extends WebsocketEvent>(- type: K,- listener: WebsocketEventListener<K, IFromPeer, FromRemote>- ): void {- // The per-key generic can't be proven against the mapped type; the- // add/dispatch pair keeps K consistent.- let listeners = this.listeners[type] as unknown as- | Set<WebsocketEventListener<K, IFromPeer, FromRemote>>- | undefined;- if (!listeners) {- listeners = new Set();- (this.listeners as Record<K, unknown>)[type] = listeners;- }- listeners.add(listener);- }-- public removeEventListener<K extends WebsocketEvent>(- type: K,- listener: WebsocketEventListener<K, IFromPeer, FromRemote>- ): void {- (- this.listeners[type] as unknown as- | Set<WebsocketEventListener<K, IFromPeer, FromRemote>>- | undefined- )?.delete(listener);- }-/** Send `RouterSubscribe`, minting a fresh token when the initial is spent. */public subscribe(): void {if (this.state === 'closed') return;@@ -269,10 +280,13 @@ class VirtualSyncSocket implements SyncSocket {private async authorizeSubscription(attempt: number): Promise<void> {const initial = this.initialToken;this.initialToken = undefined;+ // The newest caller holds the most current authorization.+ const refreshToken = [...this.owners].at(-1)?.refreshToken;try {- const token = initial ?? (await this.refreshToken());+ const token = initial ?? (await refreshToken?.());if (this.state === 'closed' || attempt !== this.subscriptionAttempt)return;+ if (token === undefined) throw new Error('No caller to authorize');this.transport.socket().send(ToRouter.fromRouterSubscribe({ docId: this.documentId, token }));@@ -391,12 +405,87 @@ class VirtualSyncSocket implements SyncSocket {type: K,event: Parameters<WebsocketEventListener<K, IFromPeer, FromRemote>>[1]): void {+ for (const owner of [...this.owners]) owner.dispatch(type, event);+ }+}++/** One caller's socket on a shared document route. */+class OwnedSyncSocket implements SyncSocket {+ private readonly route: VirtualSyncSocket;+ public readonly refreshToken: () => Promise<string>;+ private readonly listeners: ListenerSets = {};+ private closed = false;++ public constructor(+ route: VirtualSyncSocket,+ refreshToken: () => Promise<string>+ ) {+ this.route = route;+ this.refreshToken = refreshToken;+ }++ public get connectionState(): WebsocketConnectionState {+ return this.closed+ ? WebsocketConnectionState.Closed+ : this.route.connectionState;+ }++ public send(message: IFromPeer): boolean {+ return !this.closed && this.route.send(message);+ }++ public startHeartbeat(): void {+ this.route.startHeartbeat();+ }++ public reconnectIfDisconnected(): void {+ if (!this.closed) this.route.reconnectIfDisconnected();+ }++ public close(): void {+ if (this.closed) return;+ this.closed = true;+ this.route.release(this);+ }++ public addEventListener<K extends WebsocketEvent>(+ type: K,+ listener: WebsocketEventListener<K, IFromPeer, FromRemote>+ ): void {+ // The per-key generic can't be proven against the mapped type; the+ // add/dispatch pair keeps K consistent.+ let listeners = this.listeners[type] as unknown as+ | Set<WebsocketEventListener<K, IFromPeer, FromRemote>>+ | undefined;+ if (!listeners) {+ listeners = new Set();+ (this.listeners as Record<K, unknown>)[type] = listeners;+ }+ listeners.add(listener);+ }++ public removeEventListener<K extends WebsocketEvent>(+ type: K,+ listener: WebsocketEventListener<K, IFromPeer, FromRemote>+ ): void {+ (+ this.listeners[type] as unknown as+ | Set<WebsocketEventListener<K, IFromPeer, FromRemote>>+ | undefined+ )?.delete(listener);+ }++ public dispatch<K extends WebsocketEvent>(+ type: K,+ event: Parameters<WebsocketEventListener<K, IFromPeer, FromRemote>>[1]+ ): void {+ if (this.closed) return;const listeners = this.listeners[type] as unknown as| Set<WebsocketEventListener<K, IFromPeer, FromRemote>>| undefined;if (!listeners) return;// The listener signature carries the concrete Websocket for convenience;- // sources only use the event, so the virtual socket passes itself.+ // sources only use the event, so the owned socket passes itself.const instance = this as unknown as Websocket<IFromPeer, FromRemote>;for (const listener of [...listeners]) listener(instance, event);}