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): change heartbeat config to be more lenient (#1190)
teo940dd2fe9cfb
diff --git a/js/app/packages/service-clients/service-sync/source.ts b/js/app/packages/service-clients/service-sync/source.tsindex d961a57ece..5b4e34d0e2 100644--- a/js/app/packages/service-clients/service-sync/source.ts+++ b/js/app/packages/service-clients/service-sync/source.ts@@ -80,11 +80,12 @@ function createSyncServiceSocket(documentId: string, token: string) {.withBackoff(new ConstantBackoff(500)).withMaxRetries(20).withHeartbeat({- interval: 1_000,- timeout: 1_000,+ interval: 3_000,+ timeout: 2_000,pingMessage: 'ping',pongMessage: 'pong',- maxMissedHeartbeats: 1,+ maxMissedHeartbeats: 2,+ autoStart: false, // Start heartbeat manually after initial sync completes}).build();}@@ -127,6 +128,10 @@ export const createSyncServiceSource = async (const initialSync = initialSyncResult.value;+ // Start heartbeat only after initial sync completes successfully+ // This prevents the heartbeat from closing the connection during slow initial syncs+ ws.startHeartbeat();+const eventBus = createEventBus<SyncSourceEvent>();const status = createWebsocketStateSignal(ws);@@ -183,6 +188,11 @@ export const createSyncServiceSource = async (});createReconnectEffect(ws, async () => {+ // Always restart heartbeat after reconnect, regardless of sync success/failure.+ // This ensures the connection is monitored even if sync fails.+ // startHeartbeat() is safe to call - it will no-op if connection closed.+ ws.startHeartbeat();+let reconnectSyncResult: Result<InitialSync, TimeoutError> =await ResultAsync.fromPromise(raceTimeout(@@ -198,6 +208,8 @@ export const createSyncServiceSource = async ('Failed to reconnect to sync service',reconnectSyncResult.error);+ // Heartbeat is already running from above, so connection remains monitored+ // even though sync failed. The connection will eventually timeout and retry.return;}diff --git a/js/app/packages/websocket/core/websocket-heartbeat-options.ts b/js/app/packages/websocket/core/websocket-heartbeat-options.tsindex 2c7c177215..7cb78a05c6 100644--- a/js/app/packages/websocket/core/websocket-heartbeat-options.ts+++ b/js/app/packages/websocket/core/websocket-heartbeat-options.ts@@ -16,6 +16,13 @@ export interface WebsocketHeartbeatOptions {/** The maximum number of missed heartbeats before the connection is considered dead. */readonly maxMissedHeartbeats?: number;++ /**+ * Whether to automatically start the heartbeat when the websocket opens.+ * If false, you must call `startHeartbeat()` manually.+ * Defaults to true.+ */+ readonly autoStart?: boolean;}/**diff --git a/js/app/packages/websocket/core/websocket.ts b/js/app/packages/websocket/core/websocket.tsindex b9bd30c05c..1f45695885 100644--- a/js/app/packages/websocket/core/websocket.ts+++ b/js/app/packages/websocket/core/websocket.ts@@ -511,6 +511,11 @@ export class Websocket<Send = WebsocketData, Receive = WebsocketData> {this.scheduleConnectionRetryIfNeeded();}).with({ type: WebsocketEvent.Open }, () => {+ // Set connection state to Open first, before any other actions that depend on it.+ // This ensures startHeartbeat() can check state correctly.+ this.connectionState = WebsocketConnectionState.Open;++ // Check if this is a reconnection (we had connected before)if (this.backoff !== undefined && this._lastConnection !== undefined) {const detail: ReconnectEventDetail = {retries: this.backoff.retries,@@ -525,11 +530,14 @@ export class Websocket<Send = WebsocketData, Receive = WebsocketData> {this.backoff.reset();}- if (this._options.heartbeat) {+ if (+ this._options.heartbeat &&+ this._options.heartbeat.autoStart !== false+ ) {this.startHeartbeat();}- this.connectionState = WebsocketConnectionState.Open;+ // Update lastConnection AFTER reconnect check, so we can detect first connect vs reconnectthis._lastConnection = new Date();this.dispatchEvent(type, event);this.sendBufferedData();@@ -739,14 +747,24 @@ export class Websocket<Send = WebsocketData, Receive = WebsocketData> {/*** Starts a new heartbeat interval.+ * Call this manually if you set `autoStart: false` in heartbeat options.+ *+ * Safe to call multiple times - will stop any existing heartbeat first.+ * Safe to call on closed connections - will no-op if connection isn't open.*/- private startHeartbeat() {+ public startHeartbeat() {this.stopHeartbeat();this.missedHeartbeats = 0;+if (!isRequiredHeartbeatOptions(this._options?.heartbeat)) {return;}+ // Don't start heartbeat if connection isn't open - avoids orphan timers+ if (this.connectionState !== WebsocketConnectionState.Open) {+ return;+ }+this.heartbeatInterval = setInterval(() => {if (this.connectionState === WebsocketConnectionState.Open) {this.sendHeartbeat();