Skip to content

Reconnect after a turn completes drops the DONE event's message ids (root cause of #698) #705

Closed

Description

@AmirF194

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

Root cause for #698 (assistant replies intermittently disappear; the next turn loses context).

When a turn finishes normally, TurnRuntimeManager._run_turn (deeptutor/services/session/turn_runtime.py) publishes the terminal done event (carrying user_message_id / assistant_message_id for the frontend's optimistic-id reconcile) through _publish_live_event, which only appends it to the in-memory _TurnExecution.events buffer and pushes it to whatever WS subscribers are connected right now. The one-shot _flush_buffered_events call that persists the buffer to the turn-events store runs before that done event is constructed, so the done event itself, and the id-bearing metadata on it, are never persisted. Once the turn finishes, _run_turn's finally block unconditionally pops the turn from _executions.

If the client's websocket connection drops during the turn (network blip, a backgrounded/throttled tab, the client's own 45s heartbeat timeout) and reconnects after the turn has already completed and been popped, subscribe_turn's resume_from path finds no live execution and no persisted done event, so it synthesizes a fallback one (_synthesize_done_event) that carries only {"status": "completed", "synthesized": True} — no ids. The frontend's RECONCILE_TURN dispatch never fires for that turn, so the correctly-persisted assistant message stays outside the client's optimistic-id-reconciled state: the reply appears to disappear, and because the tip id used for the next turn's parent_message_id never advances past the previous (already-reconciled) message, the following turn's context effectively resets to before that reply. A full session reload re-fetches the true state from the DB and makes the reply reappear, matching the "switching to another conversation and back" workaround in #698.

Steps to reproduce

Deterministic backend reproduction (no browser/live LLM needed) — drives TurnRuntimeManager.start_turn/_run_turn/subscribe_turn for real, with only the LLM orchestrator boundary stubbed:

  1. Start a turn and let _run_turn run to completion without ever subscribing to it (simulates a client whose connection was already down when the turn finished; _run_turn runs as an independent task regardless of subscribers).
  2. Confirm the user+assistant messages are correctly persisted with real ids, and that the turn's execution has been popped from _executions.
  3. Call subscribe_turn(turn_id, after_seq=0) fresh, simulating the client's reconnect.
  4. The single done event yielded has metadata == {"status": "completed", "synthesized": True} — no assistant_message_id.

Expected Behavior

A client reconnecting after a turn has already finished should still receive the persisted user_message_id / assistant_message_id on done, so it can run the same optimistic-id reconcile as a client that stayed connected throughout.

Related Module

API/Backend

Configuration Used

N/A — pure backend/session-runtime logic bug, reproduced directly against deeptutor.services.session.turn_runtime, unrelated to any specific LLM provider or deployment config.

Additional Context

Fix + a regression test driving the real TurnRuntimeManager (not a mock of the reconcile logic) incoming in a linked PR.

Activity

  1. added a commit that references this issue on Aug 3, 2026
    8670b10
  2. added 2 commits that reference this issue on Aug 16, 2026
    3339256
    a182ce7
  3. pancacake commented on Aug 16, 2026

    @pancacake
    Collaborator

    thanks for your comments!

  4. AmirF194 commented on Aug 16, 2026

    @AmirF194
    ContributorAuthor

    Fixed by #706, which merged into dev. Closing (the PR targeted dev per CONTRIBUTING's branch gate, so GitHub did not auto-close this one on merge).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions