#1648·DeepTutor

[Bug]: ask_user Submit during a WebSocket reconnect gives up after 2s (10x200ms) while the reconnect ladder waits up to 8s — and reports a false "question is no longer active"

Author: sakuramgCreated Sep 29, 2026Updated Sep 29, 2026

Do you need to file an issue?

  • I have searched the existing issues and this bug is not already filed.
  • I believe this is a legitimate bug, not just a question or feature request.

Describe the bug

A Submit on an ask_user card fails whenever the WebSocket happens to be mid-reconnect, and then reports a false server-side reason.

The client gates every submit on the socket already being connected, polls every 200 ms, and gives up after 10 attempts — a 2 s budget:

typescript
// web/context/QuizFollowupContext.tsx:388-410
options: { awaitAck?: boolean; attempt?: number } = {},
const attempt = options.attempt ?? 0;
if (!runner.client.connected) {
  if (attempt >= 10) {            // <- 10 x 200ms = 2s, then give up
    ...
    return Promise.resolve(false);
  }
  return new Promise((resolve) =>
    window.setTimeout(
      () => resolve(send(key, message, { ...options, attempt: attempt + 1 })),
      200,
    ),
  );
}
if (options.awaitAck) {
  return runner.client.sendAwaitingAck(message);
}

The same client waits far longer than that to reconnect:

typescript
// web/features/chat/transport/reconnect-policy.ts:1-14
const BASE_DELAY_MS = 250;
const MAX_DELAY_MS = 8_000;
// reconnectDelay(attempt) = min(8000, 250 * 2 ** attempt) * jitter(0.8 .. 1.2)

with a cumulative ladder of ~250 / 750 / 1750 / 3750 / 7750 / 15750 ms. The two budgets belong to one component and are mutually inconsistent: the submit window (2 s) is shorter than a single reconnect rung (up to 8 s), so a Submit issued while the socket is reconnecting cannot be delivered inside its own window, even though the client would have been connected a few seconds later.

The gate is copy-pasted at four call sites (all share the same 10 x 200 ms shape):

file line
web/context/QuizFollowupContext.tsx 393
web/features/chat/ChatStateAdapter.tsx 2245
web/app/(workspace)/learning/books/components/BookChatPanel.tsx 283
web/app/(workspace)/whisper/page.tsx 192

The second half of the bug is the message. When the local send gives up, the card surfaces:

typescript
// web/lib/ask-user-state.ts:16-17
export const REPLY_NOT_DELIVERED =
  "This question is no longer active, so your answer could not be sent. Send a new message to continue.";

That string asserts server state ("no longer active") which the client never verified, and it is produced purely from a local condition (!connected after 2 s). The backend's own, accurate rejection signal — error_code="turn_not_waiting_input" (deeptutor/api/routers/unified_ws.py:312) — is never reached, because no frame was ever sent.

The advice is also a dead end. The parked turn still holds the session lease, so following it fails:

RuntimeError: Session already has an active or recovering turn
  deeptutor/services/session/turns/request_preparer.py:415

Observed on the backend while the banner was showing: the turn was still persisted as waiting_input, still lease-owned, and the question's ask_user box still unresolved (no ask_user_resolved event). In other words the client told the user the question had expired at the exact moment the server was still waiting for the answer.

Steps to reproduce

  1. Install locally (non-Docker) and reach the app over a link where the WebSocket can drop / needs to re-establish (a remote or throttled path reproduces this readily).
  2. Have the teaching/chat agent pose an interactive ask_user card.
  3. Interrupt the socket (drop connectivity briefly, or sleep the client host) and click Submit while the client is reconnecting.
  4. The card flips to the red banner This question is no longer active, so your answer could not be sent. Send a new message to continue. within ~2 s.
  5. Inspect the backend: the turn is still waiting_input, its lease is still held, the box is still unresolved — and no turn_not_waiting_input was ever produced, because no command reached the server.

Expected Behavior

  • A Submit should wait at least as long as its own reconnect ladder before giving up (2 s is shorter than a single rung). Even better: do not gate on client.connected at all — hand the command to the transport's outbound queue and let it flush on reconnect, which removes the race instead of widening it.
  • On give-up the message must not assert server state. It should report the local condition (could not reach the server). Only say the question is gone once the server has actually refused the command (turn_not_waiting_input).
  • Never advise "send a new message to continue" while a parked turn holds the lease — request_preparer refuses that message, so the advice cannot work.

Related Module

Frontend/Web (web/context/QuizFollowupContext.tsx, web/features/chat/ChatStateAdapter.tsx, web/features/chat/transport/reconnect-policy.ts, web/lib/ask-user-state.ts) — surface: Guided Learning / teaching-agent ask_user card.

Configuration Used

  • Local (non-Docker) install via the venv CLI, deeptutor start (backend 8001, frontend 3782).
  • Client and server on different machines over a private mesh VPN (WireGuard-based), so the WebSocket re-establishes periodically.
  • Default LLM/embedding/search configuration; nothing about this path is provider-specific.

Logs and screenshots

The quiz/teacher submit path logs nothing on the client when it gives up (only ChatStateAdapter logs WebSocket failed to connect after retries, ChatStateAdapter.tsx:2246), which is why this is easy to misread as a server-side expiry. Server side showed repeated WS connections being accepted (reconnect churn) and no turn_not_waiting_input — consistent with "the command was never sent".

Confirmed in the deployed (minified) bundle, so this is the shipped behaviour, not just source reading:

static/chunks/…:  …sendAwaitingAck(n):(i.client.send(n),Promise.resolve(!0)):a>=10?…
static/chunks/…:  …Math.min(8e3,250*2**Math.max(0,a))…

Timing model, both sides read out of that bundle:

            reconnect lands at |  old (10x200ms)  |  new (60x200ms)
  -----------------------------+------------------+-----------------
            attempt 0 (~250ms) |      PASS        |      PASS
            attempt 1 (~750ms) |      PASS        |      PASS
           attempt 2 (~1750ms) |      PASS        |      PASS
           attempt 3 (~3750ms) |      FAIL        |      PASS   <-- user-visible bug
           attempt 4 (~7750ms) |      FAIL        |      PASS   <-- user-visible bug

Additional Information

  • DeepTutor Version: 1.6.6 locally. Re-checked against current main (v1.6.12): the gate is still if (attempt >= 10) in both QuizFollowupContext.tsx:393 and ChatStateAdapter.tsx:2245, so this is present in the latest code.
  • Operating System: macOS 26.7 (Apple Silicon)
  • Python Version: 3.11
  • Node.js Version: 26.8.2
  • Browser (if applicable): Chrome (the retry gate lives in the client bundle, so this is browser-independent)
  • Related Issues: #1297, #1273, #1594

How this differs from #1297 (closed, "Submit Answer does nothing and turn remains waiting_input"). #1297 could only inspect persisted state — it explicitly notes "the WebSocket ACK was not captured, so this is a diagnosis based on state/code inspection rather than a confirmed ACK response", and lists three candidate failure modes, the first being "the card callback never sends a WebSocket frame". This report pins that first mode down and gives the mechanism: the client's own 2 s submit budget expires first because the reconnect ladder allows up to 8 s per rung, so no frame is sent, and the user is shown a false server-state message. The waiting_input + missing-event symptom in #1297 is exactly what this produces.

Suggested fix (web-only, no protocol change). Raise the threshold in the four call sites so the window covers the ladder — attempt >= 10 → e.g. attempt >= 60 (12 s > the 8 s rung cap). I have verified this locally end-to-end on the served bundle: a Submit issued at a 3.75 s / 7.75 s reconnect now succeeds where it previously failed, the bundle stays byte-valid, and the site keeps serving. It is a workaround, not the ideal fix; the connected gate itself is the thing worth removing. Separately, the missing client-side logging that #1297 asked for would have made this obvious immediately — logging the turn id, the send path taken, and the give-up reason on this branch is worth doing regardless.