Repository navigation
Description
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:
- Start a turn and let
_run_turnrun to completion without ever subscribing to it (simulates a client whose connection was already down when the turn finished;_run_turnruns as an independent task regardless of subscribers). - Confirm the user+assistant messages are correctly persisted with real ids, and that the turn's execution has been popped from
_executions. - Call
subscribe_turn(turn_id, after_seq=0)fresh, simulating the client's reconnect. - The single
doneevent yielded hasmetadata == {"status": "completed", "synthesized": True}— noassistant_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
fix: persist DONE event ids before a completed turn's execution is po…
fix: persist DONE event ids before a completed turn's execution is po…
fix: persist completed turn message IDs for reconnects (#705)
pancacake commentedon Aug 16, 2026
thanks for your comments!
AmirF194 commentedon Aug 16, 2026
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).