Commit 75d911fff for llama.cpp
commit 75d911fff3e880fda4d5eadb3bba22bcebbb7293
Author: Aleksander Grygier <aleksander.grygier@gmail.com>
Date: Thu Dec 3 17:45:02 2026 +0100
ui: rejoin a dropped reply once the page is back, retrying a few times (#33615)
A phone that locks freezes the page and drops its connection, and for the first seconds
after it wakes it has no network. The stream reader tried to rejoin once, often then, and
closed the reply as interrupted when that failed. It now waits until the page is in front
and online, retries up to six times with growing pauses, and stops early when the server
says the session is gone.
Assisted-by: Claude (Opus 5.5 Medium)
diff --git a/ui/apps/chat/src/lib/constants/api-endpoints.constants.ts b/ui/apps/chat/src/lib/constants/api-endpoints.constants.ts
index 532fa147e..9b79752f1 100644
--- a/ui/apps/chat/src/lib/constants/api-endpoints.constants.ts
+++ b/ui/apps/chat/src/lib/constants/api-endpoints.constants.ts
@@ -37,6 +37,14 @@ export const API_TOOLS_TASKS = {
// resume retry cadence while the owning model is still loading (server answers 503)
export const STREAM_RESUME_RETRY_MS = 2000;
+// how often a stream that dropped mid-reply is rejoined before the reply is given up on. A
+// phone that wakes from a locked screen has no network for its first seconds, so one try is
+// not enough; each wait is STREAM_RESUME_RETRY_MS longer than the one before
+export const STREAM_RESUME_MAX_ATTEMPTS = 6;
+
+// the answers to a resume that mean the session is gone for good, so no retry helps
+export const STREAM_RESUME_GONE_STATUSES: readonly number[] = [404, 410];
+
export const API_STREAM = {
BASE: './v1/stream',
LOOKUP: './v1/streams/lookup'
diff --git a/ui/apps/chat/src/lib/services/chat.service.ts b/ui/apps/chat/src/lib/services/chat.service.ts
index fccf270fa..bfb528051 100644
--- a/ui/apps/chat/src/lib/services/chat.service.ts
+++ b/ui/apps/chat/src/lib/services/chat.service.ts
@@ -19,7 +19,10 @@ import {
SSE_DONE_MARKER,
SSE_LINE_SEPARATOR,
STREAM_QUERY_PARAMS,
+ STREAM_RESUME_GONE_STATUSES,
STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX,
+ STREAM_RESUME_MAX_ATTEMPTS,
+ STREAM_RESUME_RETRY_MS,
STREAM_VISIBILITY_KICK_MS
} from '#lib/constants/index.ts';
import {
@@ -500,6 +503,8 @@ export class ChatService {
// if a resume returns 200 but yields nothing, we abandon
// since the session has a bounded size, the total number of retries is bounded by construction
let madeProgress = true;
+ // resumes tried since the stream last made progress
+ let resumeAttempts = 0;
const encoder = new TextEncoder();
@@ -731,12 +736,9 @@ export class ChatService {
if (!conversationId) break;
- if (!madeProgress) {
- onConnectionState?.(StreamConnectionState.LOST);
- onError?.(new Error('Stream resume produced no new bytes, giving up'));
-
- break;
- }
+ // a resume that brought nothing new counts against the same budget as one that
+ // failed outright; any byte of progress earns a fresh one
+ if (madeProgress) resumeAttempts = 0;
onConnectionState?.(StreamConnectionState.RESUMING);
madeProgress = false;
@@ -747,18 +749,61 @@ export class ChatService {
// resumeStream reads the offset from localStorage, so persist the
// freshest bytesParsed before asking the server to replay from it
ChatService.flushStreamState(conversationId);
- const resumeResp = await ChatService.resumeStream(
- conversationId,
- abortSignal,
- streamModel
- ).catch(() => null);
- // an abort landing during the resume request is intentional, not a lost connection
+ let resumeResp: Response | null = null;
+ let isGone = false;
+
+ // A locked phone freezes the page and drops its sockets, and for the first seconds
+ // after it wakes the network is not back yet. So the stream is rejoined only once the
+ // page is in front and online again, and a failed try is repeated a few times before
+ // the reply is given up on; the server keeps the session meanwhile
+ while (resumeAttempts < STREAM_RESUME_MAX_ATTEMPTS) {
+ await ChatService.waitUntilReachable(abortSignal);
+
+ if (abortSignal?.aborted) break;
+
+ if (resumeAttempts > 0) {
+ await ChatService.sleep(resumeAttempts * STREAM_RESUME_RETRY_MS, abortSignal);
+
+ if (abortSignal?.aborted) break;
+ }
+
+ resumeAttempts++;
+
+ const response = await ChatService.resumeStream(
+ conversationId,
+ abortSignal,
+ streamModel
+ ).catch(() => null);
+
+ // an abort landing during the resume request is intentional, not a lost connection
+ if (abortSignal?.aborted) break;
+
+ if (response?.status === 200) {
+ resumeResp = response;
+
+ break;
+ }
+
+ // the session is gone: the server restarted or it expired, no retry brings it back
+ if (response && STREAM_RESUME_GONE_STATUSES.includes(response.status)) {
+ isGone = true;
+
+ break;
+ }
+ }
+
if (abortSignal?.aborted) break;
- if (!resumeResp || resumeResp.status !== 200) {
+ if (!resumeResp) {
onConnectionState?.(StreamConnectionState.LOST);
- onError?.(new Error('Stream connection lost and could not be resumed'));
+ onError?.(
+ new Error(
+ isGone
+ ? 'Stream connection lost and its session is gone'
+ : 'Stream connection lost and could not be resumed'
+ )
+ );
break;
}
@@ -1709,6 +1754,21 @@ export class ChatService {
}
}
+ /** Resolves after `ms`, or at once when the signal aborts. */
+ private static sleep(ms: number, signal?: AbortSignal): Promise<void> {
+ return new Promise((resolve) => {
+ const timer = setTimeout(done, ms);
+
+ function done() {
+ clearTimeout(timer);
+ signal?.removeEventListener('abort', done);
+ resolve();
+ }
+
+ signal?.addEventListener('abort', done, { once: true });
+ });
+ }
+
/**
* Strips legacy inline reasoning content tags from message content.
* Handles both plain string content and multipart content arrays.
@@ -1732,6 +1792,34 @@ export class ChatService {
});
}
+ /**
+ * Resolves once the page can reach the server again: in front and online. A hidden page is
+ * throttled or frozen and an offline one cannot fetch, so a request made then only burns a try.
+ * Resolves at once where there is no document, and when the signal aborts.
+ */
+ private static waitUntilReachable(signal?: AbortSignal): Promise<void> {
+ const isReachable = () =>
+ typeof document === 'undefined' ||
+ (document.visibilityState === 'visible' && navigator.onLine !== false);
+
+ if (isReachable() || signal?.aborted) return Promise.resolve();
+
+ return new Promise((resolve) => {
+ function check() {
+ if (!isReachable() && !signal?.aborted) return;
+
+ document.removeEventListener('visibilitychange', check);
+ window.removeEventListener('online', check);
+ signal?.removeEventListener('abort', check);
+ resolve();
+ }
+
+ document.addEventListener('visibilitychange', check);
+ window.addEventListener('online', check);
+ signal?.addEventListener('abort', check, { once: true });
+ });
+ }
+
// write the resume state straight to localStorage, bypassing the throttle
private static writeStreamState(
conversationId: string,