Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): keep submits alive across the full reconnect ladder (#1648)

XZH80921f24ae4d
diff --git a/web/app/(workspace)/learning/books/components/BookChatPanel.tsx b/web/app/(workspace)/learning/books/components/BookChatPanel.tsx
index 5451d799a..84a580180 100644
--- a/web/app/(workspace)/learning/books/components/BookChatPanel.tsx
+++ b/web/app/(workspace)/learning/books/components/BookChatPanel.tsx
@@ -1,6 +1,10 @@
"use client";
import { browserStorage } from "@/shared/storage";
+import {
+ SUBMIT_CONNECT_RETRY_INTERVAL_MS,
+ SUBMIT_CONNECT_RETRY_LIMIT,
+} from "@/lib/send-retry";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import type {
@@ -280,7 +284,7 @@ export default function BookChatPanel({
client.send(payload);
return;
}
- if (attempt >= 10) {
+ if (attempt >= SUBMIT_CONNECT_RETRY_LIMIT) {
setBusy(false);
setMessages((prev) => [
...prev,
@@ -294,7 +298,7 @@ export default function BookChatPanel({
const timer = setTimeout(() => {
retryTimersRef.current.delete(timer);
sendWithRetry(client, payload, attempt + 1);
- }, 200);
+ }, SUBMIT_CONNECT_RETRY_INTERVAL_MS);
retryTimersRef.current.add(timer);
}
diff --git a/web/app/(workspace)/whisper/page.tsx b/web/app/(workspace)/whisper/page.tsx
index 1a1718883..2eeb19230 100644
--- a/web/app/(workspace)/whisper/page.tsx
+++ b/web/app/(workspace)/whisper/page.tsx
@@ -7,6 +7,10 @@ import WhisperComposer from "@/components/whisper/WhisperComposer";
import WhisperMessageList from "@/components/whisper/WhisperMessageList";
import WhisperRoomChip from "@/components/whisper/WhisperRoomChip";
import { useCapabilityFilter } from "@/features/capabilities/useCapabilityCatalog";
+import {
+ SUBMIT_CONNECT_RETRY_INTERVAL_MS,
+ SUBMIT_CONNECT_RETRY_LIMIT,
+} from "@/lib/send-retry";
import type {
StartTurnMessage,
StreamEvent,
@@ -189,7 +193,7 @@ export default function WhisperPage() {
setEverConnected(true);
return;
}
- if (attempt >= 10) {
+ if (attempt >= SUBMIT_CONNECT_RETRY_LIMIT) {
setBusy(false);
setMessages((prev) => [
...prev,
@@ -204,7 +208,7 @@ export default function WhisperPage() {
const timer = setTimeout(() => {
retryTimersRef.current.delete(timer);
sendWithRetry(payload, attempt + 1);
- }, 200);
+ }, SUBMIT_CONNECT_RETRY_INTERVAL_MS);
retryTimersRef.current.add(timer);
}
diff --git a/web/context/QuizFollowupContext.tsx b/web/context/QuizFollowupContext.tsx
index b31cf3d73..8bce409dc 100644
--- a/web/context/QuizFollowupContext.tsx
+++ b/web/context/QuizFollowupContext.tsx
@@ -29,6 +29,10 @@ import {
import { updateNotebookEntry } from "@/lib/notebook-api";
import { shouldAppendEventContent } from "@/lib/stream";
import { hasPendingAskUser } from "@/lib/ask-user-state";
+import {
+ SUBMIT_CONNECT_RETRY_INTERVAL_MS,
+ SUBMIT_CONNECT_RETRY_LIMIT,
+} from "@/lib/send-retry";
import {
type ChatMessage,
type LLMSelection,
@@ -390,7 +394,7 @@ export function QuizFollowupProvider({ children }: ProviderProps) {
const attempt = options.attempt ?? 0;
const runner = ensureRunner(key);
if (!runner.client.connected) {
- if (attempt >= 10) {
+ if (attempt >= SUBMIT_CONNECT_RETRY_LIMIT) {
updateThread(key, (prev) => ({
...prev,
isStreaming: false,
@@ -402,7 +406,7 @@ export function QuizFollowupProvider({ children }: ProviderProps) {
return new Promise<boolean>((resolve) => {
window.setTimeout(
() => resolve(send(key, message, { ...options, attempt: attempt + 1 })),
- 200,
+ SUBMIT_CONNECT_RETRY_INTERVAL_MS,
);
});
}
diff --git a/web/features/chat/ChatStateAdapter.tsx b/web/features/chat/ChatStateAdapter.tsx
index 29b5a8f84..1d57c61a4 100644
--- a/web/features/chat/ChatStateAdapter.tsx
+++ b/web/features/chat/ChatStateAdapter.tsx
@@ -69,6 +69,10 @@ import {
tipMessageId,
} from "@/lib/message-branches";
import { nextOptimisticId, resolvePersistedMessage } from "@/lib/optimistic-id";
+import {
+ SUBMIT_CONNECT_RETRY_INTERVAL_MS,
+ SUBMIT_CONNECT_RETRY_LIMIT,
+} from "@/lib/send-retry";
import { reconcileTurnIds } from "@/lib/turn-reconcile";
import { decideFailedTurnReplay, isFailedTurnVisible } from "@/lib/chat-resend";
import {
@@ -2242,7 +2246,7 @@ export function ChatStateAdapterProvider({
}
const runner = ensureRunner(key);
if (!runner.client.connected) {
- if (attempt >= 10) {
+ if (attempt >= SUBMIT_CONNECT_RETRY_LIMIT) {
console.error("WebSocket failed to connect after retries");
dispatch({
type: "STREAM_END",
@@ -2276,7 +2280,7 @@ export function ChatStateAdapterProvider({
resolve(
dispatchToRunner(key, msg, { ...options, attempt: attempt + 1 }),
);
- }, 200);
+ }, SUBMIT_CONNECT_RETRY_INTERVAL_MS);
retryTimersRef.current.add(timerId);
});
}
diff --git a/web/lib/send-retry.ts b/web/lib/send-retry.ts
new file mode 100644
index 000000000..9f27f1584
--- /dev/null
+++ b/web/lib/send-retry.ts
@@ -0,0 +1,2 @@
+export const SUBMIT_CONNECT_RETRY_INTERVAL_MS = 200
+export const SUBMIT_CONNECT_RETRY_LIMIT = 60
diff --git a/web/tests/chat-failed-submission-recovery.spec.tsx b/web/tests/chat-failed-submission-recovery.spec.tsx
index cc359b3f0..6aaf2581b 100644
--- a/web/tests/chat-failed-submission-recovery.spec.tsx
+++ b/web/tests/chat-failed-submission-recovery.spec.tsx
@@ -20,9 +20,16 @@ import {
readFailedSubmissions,
storeFailedSubmission,
} from "@/lib/failed-submissions";
+import {
+ SUBMIT_CONNECT_RETRY_INTERVAL_MS,
+ SUBMIT_CONNECT_RETRY_LIMIT,
+} from "@/lib/send-retry";
initI18n("en");
+const GIVE_UP_MS =
+ SUBMIT_CONNECT_RETRY_LIMIT * SUBMIT_CONNECT_RETRY_INTERVAL_MS + 400;
+
const fixture = vi.hoisted(() => ({
connected: false,
sent: [] as Array<Record<string, unknown>>,
@@ -279,10 +286,10 @@ it("marks a submission the server never received as unsent, not a failed reply",
fireEvent.click(screen.getByText("Load"));
});
// Submit while the transport cannot connect. The retry schedule gives
- // up after ~2s of failed connection attempts.
+ // up once its connect budget is exhausted.
fireEvent.click(screen.getByText("Send"));
await act(async () => {
- await vi.advanceTimersByTimeAsync(2_400);
+ await vi.advanceTimersByTimeAsync(GIVE_UP_MS);
});
// The optimistic user row is flagged unsent and NO assistant row was
// left behind to masquerade as an errored reply.
@@ -430,7 +437,7 @@ it("keeps a new unsent submission when an older server turn has identical text",
});
fireEvent.click(screen.getByText("Send"));
await act(async () => {
- await vi.advanceTimersByTimeAsync(2_400);
+ await vi.advanceTimersByTimeAsync(GIVE_UP_MS);
});
const saved = readFailedSubmission("s1");
expect(saved?.submissionId).toBeTruthy();
@@ -507,7 +514,7 @@ it("restores a failed first message in a draft and can retry after reload", asyn
);
fireEvent.click(screen.getByText("Send"));
await act(async () => {
- await vi.advanceTimersByTimeAsync(2_400);
+ await vi.advanceTimersByTimeAsync(GIVE_UP_MS);
});
expect(readMessages()).toEqual([
{ role: "user", content: "Hello offline", failed: true },
@@ -552,7 +559,7 @@ it("retries a failed draft with its original settings after live settings change
try {
render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
fireEvent.click(screen.getByText("Send"));
- await act(async () => { await vi.advanceTimersByTimeAsync(2_400); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
fireEvent.click(screen.getByText("Change live settings"));
fixture.connected = true;
await act(async () => { fireEvent.click(screen.getByText("Resend")); });
@@ -575,7 +582,7 @@ it("retries the original reading viewport after the live document changes", asyn
render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
fireEvent.click(screen.getByText("Prepare reading"));
fireEvent.click(screen.getByText("Send"));
- await act(async () => { await vi.advanceTimersByTimeAsync(2_400); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
setReadingWorkspace("ws-other");
setReadingMaterial("feedface", 3);
setReadingViewport({ locator: 99, selection: "new passage", timeSeconds: 33 });
@@ -598,7 +605,7 @@ it("retries the original watching position after the live video changes", async
render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
fireEvent.click(screen.getByText("Prepare watching"));
fireEvent.click(screen.getByText("Send"));
- await act(async () => { await vi.advanceTimersByTimeAsync(2_400); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
setWatchingMaterial("video-b");
setWatchingViewport(99);
fixture.connected = true;
@@ -712,9 +719,9 @@ it("keeps both unsent messages when a second direct send fails", async () => {
const firstView = render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
await act(async () => { fireEvent.click(screen.getByText("Load")); });
fireEvent.click(screen.getByText("Send"));
- await act(async () => { await vi.advanceTimersByTimeAsync(2_400); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
fireEvent.click(screen.getByText("Send another"));
- await act(async () => { await vi.advanceTimersByTimeAsync(2_400); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
expect(readFailedSubmissions("s1").map((record) => record.content)).toEqual([
"Hello offline", "Second offline",
]);
@@ -738,7 +745,7 @@ it("a later successful send clears only its own record", async () => {
render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
await act(async () => { fireEvent.click(screen.getByText("Load")); });
fireEvent.click(screen.getByText("Send"));
- await act(async () => { await vi.advanceTimersByTimeAsync(2_400); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
fixture.connected = true;
fireEvent.click(screen.getByText("Send another"));
expect(readFailedSubmissions("s1")).toHaveLength(2);
@@ -762,7 +769,7 @@ it("moves an older failed draft message into the server session after a later se
try {
const firstView = render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
fireEvent.click(screen.getByText("Send"));
- await act(async () => { await vi.advanceTimersByTimeAsync(2_400); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
fixture.connected = true;
fireEvent.click(screen.getByText("Send another"));
const secondId = readFailedSubmissions("draft:general").at(-1)?.submissionId;
@@ -1031,7 +1038,7 @@ it("warns when browser storage cannot save even the unsent text", async () => {
});
render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
fireEvent.click(screen.getByText("Send"));
- await act(async () => { await vi.advanceTimersByTimeAsync(2_400); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
expect(screen.getByTestId("submissionNotSaved").textContent).toBe("true");
expect(screen.getByTestId("submissionFailed").textContent).toBe("true");
expect(readMessages().at(-1)?.content).toBe("Hello offline");
diff --git a/web/tests/submit-connect-retry-budget.test.ts b/web/tests/submit-connect-retry-budget.test.ts
new file mode 100644
index 000000000..92e58dbf5
--- /dev/null
+++ b/web/tests/submit-connect-retry-budget.test.ts
@@ -0,0 +1,18 @@
+import test from 'node:test'
+import assert from 'node:assert/strict'
+import { SUBMIT_CONNECT_RETRY_INTERVAL_MS, SUBMIT_CONNECT_RETRY_LIMIT } from '@/lib/send-retry'
+import { reconnectDelay } from '@/features/chat/transport/reconnect-policy'
+
+test('submit retry budget covers the worst-case reconnect rung (#1648)', () => {
+ const budgetMs = SUBMIT_CONNECT_RETRY_LIMIT * SUBMIT_CONNECT_RETRY_INTERVAL_MS
+ const maxRungMs = reconnectDelay(64, () => 1)
+ assert.ok(
+ budgetMs >= maxRungMs,
+ `submit budget ${budgetMs}ms must cover the reconnect rung cap ${maxRungMs}ms`
+ )
+})
+
+test('submit retry budget leaves headroom beyond one reconnect rung', () => {
+ const budgetMs = SUBMIT_CONNECT_RETRY_LIMIT * SUBMIT_CONNECT_RETRY_INTERVAL_MS
+ assert.ok(budgetMs > 8_000)
+})