Repository navigation
fix: persist DONE event ids before a completed turn's execution is popped - #706
Merged
pancacake merged 1 commit intoAug 16, 2026
Merged
Conversation
2 tasks done
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
force-pushed
the
fix/698-reconnect-loses-done-message-ids
branch
from
August 3, 2026 18:13
810fd5d to
8670b10
Compare
…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
force-pushed
the
fix/698-reconnect-loses-done-message-ids
branch
from
August 16, 2026 15:03
8670b10 to
3339256
Compare
Closed
2 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 terminaldoneevent._publish_live_eventonly 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 thedoneevent, carryinguser_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) callssubscribe_turnvia theresume_frompath, finds no live execution and no persisteddonein the backlog, and gets a synthesized fallbackdone(_synthesize_done_event) with no ids. The frontend'sRECONCILE_TURNdispatch 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'sparent_message_idnever 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
doneevent (and the post-turnsession_metatitle update) have been published, matching the ordering already used on the cancelled and exception paths.events_flushedis a single-writer, lock-guarded flag with exactly one_flush_buffered_eventscall 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 realTurnRuntimeManager.start_turn/_run_turn/subscribe_turnend to end (only the LLM orchestrator boundary is stubbed, same pattern as the existingtest_regenerate.py::test_end_to_end_skips_memory_refresh_and_no_duplicate_user):_run_turnis an independent task, so it runs regardless)._executions.subscribe_turn(turn_id, after_seq=0)fresh (the reconnect path) and assert the yieldeddoneevent's metadata carries the realassistant_message_id.Confirmed this fails on
devHEAD ({"status": "completed", "synthesized": True}, no id) and passes on this branch, both in a cleanpython:3.13-slimDocker container with an editable install. Also ran the fulltests/services/session/suite (153 tests) andtests/api/test_unified_ws_turn_runtime.pyin the same container: all pass, no regressions.Updated a stale docstring in
test_turn_event_flush.pythat documented the old (buggy) "flush right before DONE" ordering.ruff checkandruff format --checkclean on the changed files (this repo's CI only runs Ruff for Python; I did not runmypy/bandit/interrogate, which are configured inpre-commitbut are not invoked by any.github/workflows/*.ymljob). Did not run the Web Node Tests job since no frontend files changed.Invariant check (
events_flushed/_executions, both shared mutable state):events_flushedhas exactly one writer (_flush_buffered_events, underself._lock);_executionshas exactly one insertion site (start_turn) and one removal site (_run_turn'sfinally), 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
servicestestsChecklist
ruff check/ruff format --checkclean (see Verification).Additional Notes
Targeting
devper CONTRIBUTING.md (general bug fix, not multi-user-specific).