Skip to content

fix: persist DONE event ids before a completed turn's execution is popped - #706

Merged
pancacake merged 1 commit into
HKUDS:devfrom
AmirF194:fix/698-reconnect-loses-done-message-ids
Aug 16, 2026
Merged

pancacake merged 1 commit into
HKUDS:devfrom
AmirF194:fix/698-reconnect-loses-done-message-ids

Conversation

@AmirF194

Copy link
Copy Markdown
Contributor

Description

Root cause and fix for #698 (assistant replies intermittently disappear; the next turn loses context via a stale parent id). Companion issue: #705.

_run_turn's normal-completion path persisted the buffered turn events to the store before constructing and publishing the terminal done event. _publish_live_event only appends to the in-memory per-turn event buffer and pushes to whatever websocket subscribers are connected at that instant; it never persists anything itself. So the done event, carrying user_message_id/assistant_message_id, was only ever delivered live, never persisted.

Once the turn finishes, its execution is unconditionally popped from _executions. A client that reconnects after that (heartbeat timeout, a backgrounded tab, a network blip) calls subscribe_turn via the resume_from path, finds no live execution and no persisted done in the backlog, and gets a synthesized fallback done (_synthesize_done_event) with no ids. The frontend's RECONCILE_TURN dispatch never fires for that turn, so the assistant reply, correctly persisted in the database the whole time, stays outside the client's optimistic-id-reconciled local tree, and the following turn's parent_message_id never advances past it. A full session reload re-fetches from the database and the reply reappears, which matches the "switching to another conversation and back" workaround reported in #698.

Fix: move the flush to run once, after the done event (and the post-turn session_meta title update) have been published, matching the ordering already used on the cancelled and exception paths. events_flushed is a single-writer, lock-guarded flag with exactly one _flush_buffered_events call site per branch, so this changes what a branch's one flush captures, not how many times it runs.

Verification

Added test_reconnect_after_turn_completion_still_carries_message_ids (tests/services/session/test_turn_runtime_subscribe.py), driving the real TurnRuntimeManager.start_turn / _run_turn / subscribe_turn end to end (only the LLM orchestrator boundary is stubbed, same pattern as the existing test_regenerate.py::test_end_to_end_skips_memory_refresh_and_no_duplicate_user):

  1. Start a turn and let it run to completion with no subscriber ever attached (_run_turn is an independent task, so it runs regardless).
  2. Confirm both messages persisted with real ids and the execution was popped from _executions.
  3. Call subscribe_turn(turn_id, after_seq=0) fresh (the reconnect path) and assert the yielded done event's metadata carries the real assistant_message_id.

Confirmed this fails on dev HEAD ({"status": "completed", "synthesized": True}, no id) and passes on this branch, both in a clean python:3.13-slim Docker container with an editable install. Also ran the full tests/services/session/ suite (153 tests) and tests/api/test_unified_ws_turn_runtime.py in the same container: all pass, no regressions.

Updated a stale docstring in test_turn_event_flush.py that documented the old (buggy) "flush right before DONE" ordering.

ruff check and ruff format --check clean on the changed files (this repo's CI only runs Ruff for Python; I did not run mypy/bandit/interrogate, which are configured in pre-commit but are not invoked by any .github/workflows/*.yml job). Did not run the Web Node Tests job since no frontend files changed.

Invariant check (events_flushed / _executions, both shared mutable state): events_flushed has exactly one writer (_flush_buffered_events, under self._lock); _executions has exactly one insertion site (start_turn) and one removal site (_run_turn's finally), both under the same lock. No test in this PR mocks any of those three sites: the real lock, the real dict, and the real flag are exercised.

Related Issues

Module(s) Affected

  • services
  • tests

Checklist

  • I have read and followed the contribution guidelines.
  • My code follows the project's coding standards.
  • ruff check / ruff format --check clean (see Verification).
  • I have added relevant tests for my changes.
  • I have updated the documentation (if necessary): none needed, internal runtime behavior only.
  • My changes do not introduce any new security vulnerabilities.

Additional Notes

Targeting dev per CONTRIBUTING.md (general bug fix, not multi-user-specific).

@AmirF194

AmirF194 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor Author

No rush, just a check-in on this one. CI is green and it's a small, self-contained fix if that helps with prioritizing.

@AmirF194
AmirF194 force-pushed the fix/698-reconnect-loses-done-message-ids branch from 810fd5d to 8670b10 Compare August 3, 2026 18:13
…pped

Fixes HKUDS#705

_run_turn's normal-completion path called _flush_buffered_events (which
persists the buffered turn events to the store, once) before constructing
and publishing the terminal `done` event. That event, carrying the
user_message_id/assistant_message_id the frontend needs to reconcile its
optimistic ids, was therefore never persisted -- only pushed to whichever
websocket subscribers happened to be live at that instant.

A client that reconnects (resume_from) after the turn has already finished
and its execution was popped from _executions falls back entirely to the
persisted backlog, finds no done event there, and subscribe_turn
synthesizes an id-less one. The reconcile never fires for that turn, so a
correctly-persisted assistant reply stays outside the client's local tree
until a full session reload.

Move the flush to after the done event (and the post-turn session title
update) are published, matching the ordering the cancelled/exception paths
already use. events_flushed is a single-writer, lock-guarded flag with one
call site per branch, so this only changes what a given branch's one flush
captures, not how many times it runs.
@pancacake
pancacake force-pushed the fix/698-reconnect-loses-done-message-ids branch from 8670b10 to 3339256 Compare August 16, 2026 15:03
@pancacake
pancacake merged commit a182ce7 into HKUDS:dev Aug 16, 2026
12 checks passed
@AmirF194
AmirF194 deleted the fix/698-reconnect-loses-done-message-ids branch August 16, 2026 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants