The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 25, 06:15 UTC
Debounce reconnect disconnect logging (#1862)
Julius Marminge committed
Apr 9, 2026 at 15:39 UTC
9bedd7140ce22f7f05d3990cdb50cafcceae37de
4 files changed
+123
-9
apps/web/src/environments/runtime/connection.test.ts
+74
-2
@@ -6,10 +6,12 @@ import type { WsRpcClient } from "~/rpc/wsRpcClient";
6
7
function createTestClient(options?: {
8
readonly getSnapshot?: () => Promise<{ readonly snapshotSequence: number }>;
9
+ readonly replayEvents?: () => Promise<ReadonlyArray<any>>;
10
}) {
11
const lifecycleListeners = new Set<(event: any) => void>();
12
const configListeners = new Set<(event: any) => void>();
13
const terminalListeners = new Set<(event: any) => void>();
14
+ let domainResubscribe: (() => void) | undefined;
15
16
const getSnapshot = vi.fn(
17
options?.getSnapshot ??
@@ -20,6 +22,7 @@ function createTestClient(options?: {
22
threads: [],
23
}) as any),
24
);
25
+ const replayEvents = vi.fn(options?.replayEvents ?? (async () => []));
26
27
const client = {
28
dispose: vi.fn(async () => undefined),
@@ -49,8 +52,15 @@ function createTestClient(options?: {
52
dispatchCommand: vi.fn(async () => undefined),
53
getTurnDiff: vi.fn(async () => undefined),
54
getFullThreadDiff: vi.fn(async () => undefined),
52
- replayEvents: vi.fn(async () => []),
53
- onDomainEvent: () => () => undefined,
55
+ replayEvents,
56
+ onDomainEvent: vi.fn((_: (event: any) => void, options?: { onResubscribe?: () => void }) => {
57
+ domainResubscribe = options?.onResubscribe;
58
+ return () => {
59
+ if (domainResubscribe === options?.onResubscribe) {
60
+ domainResubscribe = undefined;
61
+ }
62
+ };
63
+ }),
64
},
65
terminal: {
66
open: vi.fn(async () => undefined),
@@ -114,6 +124,9 @@ function createTestClient(options?: {
124
});
125
}
126
},
127
+ triggerDomainResubscribe: () => {
128
+ domainResubscribe?.();
129
+ },
130
};
131
}
132
@@ -213,4 +226,63 @@ describe("createEnvironmentConnection", () => {
226
227
await connection.dispose();
228
});
229
+
230
+ it("swallows replay recovery failures triggered by resubscribe", async () => {
231
+ const environmentId = EnvironmentId.makeUnsafe("env-1");
232
+ const snapshotError = new Error("snapshot failed");
233
+ let snapshotCalls = 0;
234
+ const { client, triggerDomainResubscribe } = createTestClient({
235
+ getSnapshot: async () => {
236
+ snapshotCalls += 1;
237
+ if (snapshotCalls === 1) {
238
+ return {
239
+ snapshotSequence: 1,
240
+ projects: [],
241
+ threads: [],
242
+ } as any;
243
+ }
244
+
245
+ throw snapshotError;
246
+ },
247
+ replayEvents: async () => {
248
+ throw new Error("SocketCloseError: 1006");
249
+ },
250
+ });
251
+
252
+ const connection = createEnvironmentConnection({
253
+ kind: "saved",
254
+ knownEnvironment: {
255
+ id: "env-1",
256
+ label: "Remote env",
257
+ source: "manual",
258
+ target: {
259
+ httpBaseUrl: "http://example.test",
260
+ wsBaseUrl: "ws://example.test",
261
+ },
262
+ environmentId,
263
+ },
264
+ client,
265
+ applyEventBatch: vi.fn(),
266
+ syncSnapshot: vi.fn(),
267
+ applyTerminalEvent: vi.fn(),
268
+ });
269
+
270
+ await Promise.resolve();
271
+ await Promise.resolve();
272
+
273
+ const onUnhandledRejection = vi.fn();
274
+ process.on("unhandledRejection", onUnhandledRejection);
275
+
276
+ try {
277
+ triggerDomainResubscribe();
278
+ await new Promise((resolve) => setTimeout(resolve, 0));
279
+ await new Promise((resolve) => setTimeout(resolve, 0));
280
+ } finally {
281
+ process.off("unhandledRejection", onUnhandledRejection);
282
+ }
283
+
284
+ expect(onUnhandledRejection).not.toHaveBeenCalled();
285
+
286
+ await connection.dispose();
287
+ });
288
});
apps/web/src/environments/runtime/connection.ts
+8
-4
@@ -128,6 +128,10 @@ export function createEnvironmentConnection(
128
queueMicrotask(flushPendingDomainEvents);
129
};
130
131
+ const scheduleReplayRecovery = (reason: "sequence-gap" | "resubscribe") => {
132
+ void runReplayRecovery(reason).catch(() => undefined);
133
+ };
134
+
135
const runReplayRecovery = async (reason: "sequence-gap" | "resubscribe"): Promise<void> => {
136
if (!recovery.beginReplayRecovery(reason)) {
137
return;
@@ -172,7 +176,7 @@ export function createEnvironmentConnection(
176
return;
177
}
178
}
175
- void runReplayRecovery(reason);
179
+ scheduleReplayRecovery(reason);
180
} else if (replayCompletion.shouldReplay && import.meta.env.MODE !== "test") {
181
console.warn(
182
"[orchestration-recovery]",
@@ -198,7 +202,7 @@ export function createEnvironmentConnection(
202
if (!disposed) {
203
input.syncSnapshot(snapshot, environmentId);
204
if (recovery.completeSnapshotRecovery(snapshot.snapshotSequence)) {
201
- void runReplayRecovery("sequence-gap");
205
+ scheduleReplayRecovery("sequence-gap");
206
}
207
}
208
} catch (error) {
@@ -245,7 +249,7 @@ export function createEnvironmentConnection(
249
}
250
if (action === "recover") {
251
flushPendingDomainEvents();
248
- void runReplayRecovery("sequence-gap");
252
+ scheduleReplayRecovery("sequence-gap");
253
}
254
},
255
{
@@ -254,7 +258,7 @@ export function createEnvironmentConnection(
258
return;
259
}
260
flushPendingDomainEvents();
257
- void runReplayRecovery("resubscribe");
261
+ scheduleReplayRecovery("resubscribe");
262
},
263
},
264
);
apps/web/src/rpc/wsTransport.test.ts
+33
@@ -707,6 +707,39 @@ describe("WsTransport", () => {
707
await transport.dispose();
708
});
709
710
+ it("logs a transport disconnect once even when multiple subscriptions fail together", async () => {
711
+ const transport = new WsTransport("ws://localhost:3020");
712
+ const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => undefined);
713
+
714
+ const unsubscribeA = transport.subscribe(
715
+ () => Stream.fail(new Error("SocketCloseError: 1006")),
716
+ vi.fn(),
717
+ { retryDelay: 10 },
718
+ );
719
+ const unsubscribeB = transport.subscribe(
720
+ () => Stream.fail(new Error("SocketCloseError: 1006")),
721
+ vi.fn(),
722
+ { retryDelay: 10 },
723
+ );
724
+
725
+ await waitFor(() => {
726
+ expect(sockets).toHaveLength(1);
727
+ });
728
+
729
+ getSocket().open();
730
+
731
+ await waitFor(() => {
732
+ expect(warnSpy).toHaveBeenCalledTimes(1);
733
+ });
734
+ expect(warnSpy).toHaveBeenCalledWith("WebSocket RPC subscription disconnected", {
735
+ error: "SocketCloseError: 1006",
736
+ });
737
+
738
+ unsubscribeA();
739
+ unsubscribeB();
740
+ await transport.dispose();
741
+ });
742
+
743
it("streams finite request events without re-subscribing", async () => {
744
const transport = new WsTransport("ws://localhost:3020");
745
const listener = vi.fn();
apps/web/src/rpc/wsTransport.ts
+8
-3
@@ -50,6 +50,7 @@ export class WsTransport {
50
private readonly url: WsRpcProtocolSocketUrlProvider;
51
private readonly lifecycleHandlers: WsProtocolLifecycleHandlers | undefined;
52
private disposed = false;
53
+ private hasReportedTransportDisconnect = false;
54
private reconnectChain: Promise<void> = Promise.resolve();
55
private session: TransportSession;
56
@@ -136,6 +137,7 @@ export class WsTransport {
137
listener,
138
() => active,
139
() => {
140
+ this.hasReportedTransportDisconnect = false;
141
hasReceivedValue = true;
142
},
143
);
@@ -156,9 +158,12 @@ export class WsTransport {
158
return;
159
}
160
159
- console.warn("WebSocket RPC subscription disconnected", {
160
- error: formattedError,
161
- });
161
+ if (!this.hasReportedTransportDisconnect) {
162
+ console.warn("WebSocket RPC subscription disconnected", {
163
+ error: formattedError,
164
+ });
165
+ }
166
+ this.hasReportedTransportDisconnect = true;
167
await sleep(retryDelayMs);
168
}
169
}