Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Preserve live stream subscriptions across explicit reconnects (#1972)

Julius Marminge94d13a2b754f
diff --git a/apps/web/src/rpc/wsTransport.test.ts b/apps/web/src/rpc/wsTransport.test.ts
index 2863bb022..12c3d4528 100644
--- a/apps/web/src/rpc/wsTransport.test.ts
+++ b/apps/web/src/rpc/wsTransport.test.ts
@@ -639,6 +639,115 @@ describe("WsTransport", () => {
await transport.dispose();
});
+ it("re-subscribes live stream listeners after an explicit transport reconnect", async () => {
+ const transport = createTransport("ws://localhost:3020");
+ const listener = vi.fn();
+ const onResubscribe = vi.fn();
+
+ const unsubscribe = transport.subscribe(
+ (client) => client[WS_METHODS.subscribeServerLifecycle]({}),
+ listener,
+ { onResubscribe },
+ );
+
+ await waitFor(() => {
+ expect(sockets).toHaveLength(1);
+ });
+
+ const firstSocket = getSocket();
+ firstSocket.open();
+
+ await waitFor(() => {
+ expect(firstSocket.sent).toHaveLength(1);
+ });
+
+ const firstRequest = JSON.parse(firstSocket.sent[0] ?? "{}") as { id: string };
+ const firstEvent = {
+ version: 1,
+ sequence: 1,
+ type: "welcome",
+ payload: {
+ environment: {
+ environmentId: "environment-local",
+ label: "Local environment",
+ platform: { os: "darwin", arch: "arm64" },
+ serverVersion: "0.0.0-test",
+ capabilities: { repositoryIdentity: true },
+ },
+ cwd: "/tmp/one",
+ projectName: "one",
+ },
+ };
+
+ firstSocket.serverMessage(
+ JSON.stringify({
+ _tag: "Chunk",
+ requestId: firstRequest.id,
+ values: [firstEvent],
+ }),
+ );
+
+ await waitFor(() => {
+ expect(listener).toHaveBeenLastCalledWith(firstEvent);
+ });
+
+ await transport.reconnect();
+
+ await waitFor(() => {
+ expect(sockets).toHaveLength(2);
+ });
+
+ const secondSocket = getSocket();
+ expect(secondSocket).not.toBe(firstSocket);
+ expect(firstSocket.readyState).toBe(MockWebSocket.CLOSED);
+
+ secondSocket.open();
+
+ await waitFor(() => {
+ expect(secondSocket.sent).toHaveLength(1);
+ });
+
+ const secondRequest = JSON.parse(secondSocket.sent[0] ?? "{}") as {
+ id: string;
+ tag: string;
+ };
+ expect(secondRequest.tag).toBe(WS_METHODS.subscribeServerLifecycle);
+ expect(secondRequest.id).not.toBe(firstRequest.id);
+ expect(onResubscribe).toHaveBeenCalledOnce();
+
+ const secondEvent = {
+ version: 1,
+ sequence: 2,
+ type: "welcome",
+ payload: {
+ environment: {
+ environmentId: "environment-local",
+ label: "Local environment",
+ platform: { os: "darwin", arch: "arm64" },
+ serverVersion: "0.0.0-test",
+ capabilities: { repositoryIdentity: true },
+ },
+ cwd: "/tmp/two",
+ projectName: "two",
+ },
+ };
+
+ secondSocket.serverMessage(
+ JSON.stringify({
+ _tag: "Chunk",
+ requestId: secondRequest.id,
+ values: [secondEvent],
+ }),
+ );
+
+ await waitFor(() => {
+ expect(listener).toHaveBeenLastCalledWith(secondEvent);
+ });
+
+ unsubscribe();
+ await transport.dispose();
+ });
+
it("does not fire onResubscribe when the first stream attempt exits before any value", async () => {
const transport = createTransport("ws://localhost:3020");
const listener = vi.fn();
diff --git a/apps/web/src/rpc/wsTransport.ts b/apps/web/src/rpc/wsTransport.ts
index 851cc5046..8c86625dc 100644
--- a/apps/web/src/rpc/wsTransport.ts
+++ b/apps/web/src/rpc/wsTransport.ts
@@ -121,6 +121,7 @@ export class WsTransport {
return;
}
+ const session = this.session;
try {
if (hasReceivedValue) {
try {
@@ -130,7 +131,6 @@ export class WsTransport {
}
}
- const session = this.session;
const runningStream = this.runStreamOnSession(
session,
connect,
@@ -150,6 +150,10 @@ export class WsTransport {
return;
}
+ if (session !== this.session) {
+ continue;
+ }
+
const formattedError = formatErrorMessage(error);
if (!isTransportConnectionErrorMessage(formattedError)) {
console.warn("WebSocket RPC subscription failed", {