Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: reconnect a chat reply whose connection died silently (#17416)

Nilaycc38d5829c95
diff --git a/studio/frontend/src/features/chat/api/chat-generation-api.ts b/studio/frontend/src/features/chat/api/chat-generation-api.ts
index 4e98831298e..467fead0eac 100644
--- a/studio/frontend/src/features/chat/api/chat-generation-api.ts
+++ b/studio/frontend/src/features/chat/api/chat-generation-api.ts
@@ -3,6 +3,7 @@
// eslint-disable-next-line no-restricted-imports -- Avoid the auth barrel's React login page.
import { authFetch } from "@/features/auth/api";
+import { watchStreamSilence } from "@/lib/stream-silence";
import type {
OpenAIChatChunk,
OpenAIChatCompletionsRequest,
@@ -380,6 +381,7 @@ async function* streamChatGenerationEvents(
signal?: AbortSignal,
/** A keep-alive is progress only if its stamp MOVED, judged by the caller across reconnects. */
onActivity?: (keepAliveStamp?: string) => void,
+ silenceTimeoutMs?: number,
): AsyncGenerator<ChatGenerationEvent> {
const response = await authFetch(
`/api/inference/chat-runs/${encodeURIComponent(id)}/events?after=${Math.max(0, after)}`,
@@ -391,9 +393,11 @@ async function* streamChatGenerationEvents(
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
+ const silence = watchStreamSilence(reader, silenceTimeoutMs);
try {
while (true) {
const { done, value } = await reader.read();
+ silence.touch();
buffer += decoder.decode(value, { stream: !done });
buffer = buffer.replace(/\r\n/g, "\n");
let boundary = buffer.indexOf("\n\n");
@@ -422,6 +426,7 @@ async function* streamChatGenerationEvents(
if (done) return;
}
} finally {
+ silence.stop();
await reader.cancel().catch(() => undefined);
}
}
@@ -438,6 +443,7 @@ export async function* followChatGenerationRun(
signal?: AbortSignal;
/** Overridable so a test can reach the deadline without waiting out the default. */
stallTimeoutMs?: number;
+ silenceTimeoutMs?: number;
} = {},
): AsyncGenerator<ChatGenerationRunUpdate> {
const { replayFrom } = options;
@@ -502,6 +508,7 @@ export async function* followChatGenerationRun(
cursor,
signal,
noteProgress,
+ options.silenceTimeoutMs,
)) {
if (event.seq <= cursor) continue;
cursor = event.seq;
diff --git a/studio/frontend/src/features/chat/api/research-api.ts b/studio/frontend/src/features/chat/api/research-api.ts
index 79f825f525b..7f7c2bbd4b4 100644
--- a/studio/frontend/src/features/chat/api/research-api.ts
+++ b/studio/frontend/src/features/chat/api/research-api.ts
@@ -2,6 +2,7 @@
// eslint-disable-next-line no-restricted-imports -- Avoid the auth barrel's React login page.
import { authFetch } from "@/features/auth/api";
+import { watchStreamSilence } from "@/lib/stream-silence";
import type {
CreateResearchRunInput,
ResearchEvent,
@@ -165,6 +166,7 @@ export async function* streamResearchEvents(
id: string,
after: number,
signal?: AbortSignal,
+ silenceTimeoutMs?: number,
): AsyncGenerator<StreamResearchEvent> {
// POST, not GET: proxies hold a streamed GET until it closes. The route accepts both.
const response = await authFetch(
@@ -180,9 +182,11 @@ export async function* streamResearchEvents(
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
+ const silence = watchStreamSilence(reader, silenceTimeoutMs);
try {
while (true) {
const { done, value } = await reader.read();
+ silence.touch();
buffer += decoder.decode(value, { stream: !done });
// Normalize on the whole buffer so a CRLF split across chunks still frames.
buffer = buffer.replace(/\r\n/g, "\n");
@@ -223,6 +227,7 @@ export async function* streamResearchEvents(
}
}
} finally {
+ silence.stop();
await reader.cancel().catch(() => undefined);
}
}
@@ -265,6 +270,7 @@ export async function* followResearchRun(
initialRun?: ResearchRun;
signal?: AbortSignal;
replayFrom?: number;
+ silenceTimeoutMs?: number;
} = {},
): AsyncGenerator<ResearchRunUpdate> {
const { signal, replayFrom } = options;
@@ -302,7 +308,12 @@ export async function* followResearchRun(
let cursor = replayFrom ?? run.lastEventSeq;
while (!signal?.aborted) {
try {
- for await (const event of streamResearchEvents(id, cursor, signal)) {
+ for await (const event of streamResearchEvents(
+ id,
+ cursor,
+ signal,
+ options.silenceTimeoutMs,
+ )) {
cursor = Math.max(cursor, event.id);
// Delta-only events carry no run; reuse ours so ~12 deltas/s do not re-render every subscriber.
const eventRun: ResearchRun = event.run ?? currentRun;
diff --git a/studio/frontend/src/lib/stream-silence.ts b/studio/frontend/src/lib/stream-silence.ts
new file mode 100644
index 00000000000..d799a2ba9ef
--- /dev/null
+++ b/studio/frontend/src/lib/stream-silence.ts
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+/** Run event routes send a keep-alive every 15s, so a stream this quiet died without the browser
+ * hearing of it (a phone back from sleep). Wall clock, so time spent asleep counts. */
+export const STREAM_SILENCE_TIMEOUT_MS = 45_000;
+
+export function watchStreamSilence(
+ reader: ReadableStreamDefaultReader<unknown>,
+ timeoutMs = STREAM_SILENCE_TIMEOUT_MS,
+): { touch: () => void; stop: () => void } {
+ let lastReadAt = Date.now();
+ const timer = globalThis.setInterval(
+ () => {
+ if (Date.now() - lastReadAt > timeoutMs)
+ reader.cancel().catch(() => undefined);
+ },
+ Math.min(5_000, timeoutMs),
+ );
+ return {
+ touch: () => {
+ lastReadAt = Date.now();
+ },
+ stop: () => globalThis.clearInterval(timer),
+ };
+}
diff --git a/studio/frontend/tests/chat-generation-reconnect.test.ts b/studio/frontend/tests/chat-generation-reconnect.test.ts
index 6c62df51965..9cbb99c28ab 100644
--- a/studio/frontend/tests/chat-generation-reconnect.test.ts
+++ b/studio/frontend/tests/chat-generation-reconnect.test.ts
@@ -156,6 +156,58 @@ test("reconnect resumes from the applied cursor without duplicate chunks", async
);
});
+test("a stream that goes silent without closing is dropped and resumed from the cursor", async () => {
+ const eventUrls: string[] = [];
+ const encoder = new TextEncoder();
+ let deadStreamCancelled = false;
+ globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
+ const url = String(input);
+ if (url.includes("/events")) {
+ eventUrls.push(url);
+ if (eventUrls.length > 1) {
+ return sse([
+ frame(2, "chunk", { choices: [{ delta: { content: "B" } }] }),
+ frame(3, "run.completed", { status: "completed" }, run("completed", 3)),
+ ]);
+ }
+ return new Response(
+ new ReadableStream({
+ start(controller) {
+ controller.enqueue(
+ encoder.encode(
+ frame(1, "chunk", { choices: [{ delta: { content: "A" } }] }),
+ ),
+ );
+ init?.signal?.addEventListener("abort", () =>
+ controller.error(init.signal?.reason),
+ );
+ },
+ cancel() {
+ deadStreamCancelled = true;
+ },
+ }),
+ { status: 200, headers: { "content-type": "text/event-stream" } },
+ );
+ }
+ return new Response(JSON.stringify(run("running", 1)), {
+ status: 200,
+ headers: { "content-type": "application/json" },
+ });
+ }) as typeof fetch;
+ const sequences: number[] = [];
+ for await (const update of followChatGenerationRun("run-1", {
+ initialRun: run("running", 0),
+ replayFrom: 0,
+ silenceTimeoutMs: 50,
+ signal: AbortSignal.timeout(2_000),
+ })) {
+ if (update.event) sequences.push(update.event.seq);
+ }
+ assert.deepEqual(sequences, [1, 2, 3]);
+ assert.equal(deadStreamCancelled, true);
+ assert.match(eventUrls[1], /after=1$/);
+});
+
test("durable replay normalizes reasoning summary control frames", async () => {
globalThis.fetch = (async (input: RequestInfo | URL) => {
if (String(input).includes("/events")) {
diff --git a/studio/frontend/tests/research-run-silence.test.ts b/studio/frontend/tests/research-run-silence.test.ts
new file mode 100644
index 00000000000..a308881025b
--- /dev/null
+++ b/studio/frontend/tests/research-run-silence.test.ts
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import test from "node:test";
+
+import {
+ installLocalStorageFake,
+ registerBundlerResolver,
+} from "./helpers/kit.ts";
+
+register("./helpers/settings-api-resolver.mjs", import.meta.url);
+registerBundlerResolver();
+installLocalStorageFake();
+Object.assign(globalThis.window, {
+ setTimeout: globalThis.setTimeout,
+ clearTimeout: globalThis.clearTimeout,
+});
+
+const { followResearchRun } = await import(
+ "../src/features/chat/api/research-api.ts"
+);
+
+const RUN_ID = "run-silence";
+
+function snapshot(overrides: Record<string, unknown> = {}) {
+ return {
+ id: RUN_ID,
+ threadId: "thread-1",
+ userMessageId: "user-1",
+ status: "running",
+ steps: [],
+ sources: [],
+ lastEventSeq: 0,
+ createdAt: 1,
+ updatedAt: 1,
+ ...overrides,
+ };
+}
+
+function frame(id: number, event: string, data: Record<string, unknown>) {
+ return `id: ${id}\nevent: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
+}
+
+test("a research stream that goes silent without closing is dropped and resumed", async () => {
+ const original = globalThis.fetch;
+ const encoder = new TextEncoder();
+ const eventUrls: string[] = [];
+ let deadStreamCancelled = false;
+ const terminal = snapshot({ status: "completed", lastEventSeq: 2 });
+ globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
+ const url = String(input);
+ if (url.includes("/events")) {
+ eventUrls.push(url);
+ const first = eventUrls.length === 1;
+ return new Response(
+ new ReadableStream<Uint8Array>({
+ start(controller) {
+ if (!first) {
+ controller.enqueue(
+ encoder.encode(
+ frame(2, "run.completed", { attempt: 0, run: terminal }),
+ ),
+ );
+ controller.close();
+ return;
+ }
+ controller.enqueue(
+ encoder.encode(frame(1, "report.updated", { delta: 1 })),
+ );
+ init?.signal?.addEventListener("abort", () =>
+ controller.error(init.signal?.reason),
+ );
+ },
+ cancel() {
+ if (first) deadStreamCancelled = true;
+ },
+ }),
+ { status: 200, headers: { "content-type": "text/event-stream" } },
+ );
+ }
+ return new Response(JSON.stringify(snapshot({ lastEventSeq: 1 })), {
+ status: 200,
+ headers: { "content-type": "application/json" },
+ });
+ }) as typeof fetch;
+ try {
+ const ids: number[] = [];
+ let last: unknown;
+ for await (const update of followResearchRun(RUN_ID, {
+ initialRun: snapshot() as never,
+ silenceTimeoutMs: 50,
+ signal: AbortSignal.timeout(2_000),
+ })) {
+ if (update.event) ids.push(update.event.id);
+ last = update.run;
+ }
+ assert.deepEqual(ids, [1, 2]);
+ assert.equal((last as { status: string }).status, "completed");
+ assert.equal(deadStreamCancelled, true);
+ assert.match(eventUrls[1], /after=1$/);
+ } finally {
+ globalThis.fetch = original;
+ }
+});