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.

Replay entity tracking on websocket reconnect (#4171)

teo5cfc76d29ab9
diff --git a/js/app/packages/app/component/Root.tsx b/js/app/packages/app/component/Root.tsx
index bccc3824d3..a714ff2e33 100644
--- a/js/app/packages/app/component/Root.tsx
+++ b/js/app/packages/app/component/Root.tsx
@@ -55,6 +55,7 @@ import { prefetchHistory } from '@queries/history/history';
import { invalidateUserNotifications } from '@queries/notification/user-notifications';
import { QuerySyncProvider } from '@queries/sync/SyncProvider';
import { MutationUndoProvider } from '@queries/undo';
+import { useReopenTrackedEntitiesOnReconnect } from '@service-connection/client';
import { ws as connectionGatewayWebsocket } from '@service-connection/websocket';
import { MetaProvider, Title } from '@solidjs/meta';
import {
@@ -370,6 +371,7 @@ function ConfiguredGlobalAppStateProvider(props: ParentProps) {
// Initialize global notification helpers
const notifInterface = usePlatformNotificationState();
useChatRenameWebsocketSync();
+ useReopenTrackedEntitiesOnReconnect();
const onNotification = (notification: UnifiedNotification) => {
if (notifInterface === 'not-supported') return;
diff --git a/js/app/packages/service-clients/service-connection/client.ts b/js/app/packages/service-clients/service-connection/client.ts
index 78a54fa951..d4a27c9132 100644
--- a/js/app/packages/service-clients/service-connection/client.ts
+++ b/js/app/packages/service-clients/service-connection/client.ts
@@ -1,4 +1,5 @@
import type { EntityId } from '@core/types';
+import { createReconnectEffect } from '@websocket/index';
import { ok } from 'neverthrow';
import type { TrackEntityMessage } from './generated/schemas/trackEntityMessage';
@@ -6,23 +7,31 @@ import { clearStream } from './stream';
import { ws } from './websocket';
// ref counting on connection_gateway open/close events is needed to avoid breaking
-// events if more than one incstance of a block is opened
-const trackedEntities: Map<EntityId, number> = new Map();
+// events if more than one incstance of a block is opened. We also retain the entity_type
+// so that `open` can be replayed on the new connection_id after a websocket reconnect.
+interface TrackedEntity {
+ entityType: TrackEntityMessage['entity_type'];
+ count: number;
+}
+const trackedEntities: Map<EntityId, TrackedEntity> = new Map();
export const connectionGatewayClient = {
async trackEntity(args: TrackEntityMessage) {
- const trackedCount = trackedEntities.get(args.entity_id);
+ const tracked = trackedEntities.get(args.entity_id);
if (args.action === 'open') {
- if (trackedCount) {
- trackedEntities.set(args.entity_id, trackedCount + 1);
+ if (tracked) {
+ tracked.count += 1;
return ok({});
} else {
- trackedEntities.set(args.entity_id, 1);
+ trackedEntities.set(args.entity_id, {
+ entityType: args.entity_type,
+ count: 1,
+ });
}
} else if (args.action === 'close') {
- if (!trackedCount) return ok({});
- else if (trackedCount > 1) {
- trackedEntities.set(args.entity_id, trackedCount - 1);
+ if (!tracked) return ok({});
+ else if (tracked.count > 1) {
+ tracked.count -= 1;
return ok({});
} else {
trackedEntities.delete(args.entity_id);
@@ -36,3 +45,21 @@ export const connectionGatewayClient = {
return ok({});
},
};
+
+/**
+ * Re-sends `open` for every tracked entity when the socket reconnects. A reconnect gets a new
+ * connection_id, but `open` is only sent once on mount (and is ref-count guarded), so without
+ * this the new connection has no presence row or stream subscription for still-open entities.
+ */
+export function useReopenTrackedEntitiesOnReconnect(): void {
+ createReconnectEffect(ws, () => {
+ for (const [entity_id, { entityType }] of trackedEntities) {
+ ws.send({
+ type: 'track_entity',
+ entity_id,
+ entity_type: entityType,
+ action: 'open',
+ });
+ }
+ });
+}