Skip to content

fix(realtime): never hand a message to the handlers twice while its first copy is running - #11

Closed
sanctrl wants to merge 6 commits into
mainfrom
fix/engine-inflight-dedup
Closed

sanctrl wants to merge 6 commits into
mainfrom
fix/engine-inflight-dedup

Conversation

@sanctrl

@sanctrl sanctrl commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #10. Merge #10 first. This branch is #10's head (bf24ad3) plus 3be8f04, b140d1e, 801ce7d and 5e7b5f0; review only those four. The base is main, not #10's branch. If #10 is squash-merged, run git rebase --onto origin/main bf24ad3 first; that replays the four commits with no conflicts.

What

1. A message could reach the handlers twice while its first copy's handler was still running. This happens with the server's reconnect backlog plus the REST drain of the same rows, or with an in-order copy whose cursor moves only after its handler returns.

A copy on the same connection as a running first copy is not dispatched again, and nothing ever waits for the first copy (_adopt_in_flight):

  • Live copy: acked when the first copy finished cleanly, never if it failed.
  • Drain row: the cursor reads the first copy's outcome when it is computed, and stops if the first copy is not done. The next drain covers the row as a dedup hit.
  • Failed first copy: in the default mode the server re-offers the message. Under ack_on_receipt the live copy was already acked at receipt, so there is no re-offer, as for any failed handler.
  • Later connection: a copy there is dispatched afresh, because adoption is per connection generation.
  • Dedup off: with dedup_cache_size=0, every copy is dispatched.

2. A frame that parked while a gap was being resolved was cleared unseen. Under ack_on_receipt (the Hermes setting) that frame was acked and never handled.

  • _resolve_gap and the shutdown flush now work on the frames parked when they start. Each frame leaves the buffer, and the cursor moves past it, before its handler is awaited.
  • In-order arrivals during that time go to the receive loop at once.
  • A frame behind a new hole gets its own gap window and its own report.
  • A state that is being resolved is never dropped as idle.
  • disconnect() stops taking frames in before it flushes. An arriving frame is neither acked nor dispatched, and the server keeps it. A frame acked at receipt is never dropped.

3. Two races closed. The per-conversation cursor never moves backwards, and cleanup only removes the state it was handed.

Re-review items (5e7b5f0)

  • Gap fill during shutdown: a gap fill whose fetch returns after disconnect() began acks nothing. Under ack_on_receipt, its rows used to be acked and then lost when the shutdown cancelled the task mid-handover. TS was not affected: it re-reads the state after the fetch, and disconnect() and a close clear that state synchronously.
  • Spurious gap: a live repeat that adopts a running drain row now moves the cursor on, like a dispatch does. Before, the next frame parked and opened a gap that did not exist: a REST fill and a report 2 s later.
  • Docs: README and CHANGELOG now say that handlers in one conversation start in seq order but can overlap, as in TS.

Tests that fail on 801ce7d:

  • test_a_gap_fill_that_returns_during_disconnect_acks_nothing
  • test_an_adopted_in_order_repeat_moves_the_cursor_on

Checks: pytest 360 passed on 3.9, 3.11 and 3.13; 15 repeats pass; ruff and mypy are clean.

Review items (all reproduced, then fixed)

Tests that fail on the previous head (b140d1e)

  • test_a_drain_repeat_of_a_running_message_is_not_dispatched_and_never_waited_for (both modes)
  • test_a_copy_on_a_later_connection_is_dispatched_afresh_while_the_first_runs (hung on the old head)
  • test_a_cancelled_first_copy_counts_as_not_handled
  • test_disconnect_takes_no_frame_in_once_it_began
  • test_disconnect_returns_promptly_while_frames_keep_arriving (both modes)
  • test_a_handler_that_disconnects_while_a_repeat_of_its_message_arrives_returns
  • test_a_frame_behind_a_new_hole_gets_its_own_gap_window
  • 5 shared-scenario runs

test_a_live_repeat_of_a_first_copy_that_failed_is_never_acked passes on the previous head. It is the guard for R4: with the reviewer's mutation applied, it fails.

Checks

  • pytest on 3.9, 3.11 and 3.13: 358 passed.
  • 15 repeats pass.
  • ruff and mypy are clean.
  • The fixture is byte-identical to agentchat-typescript.

…gine conformance scenarios

Default mode dropped a live message.new below the conversation's seq anchor
without an ack, even when this session had already dispatched that message
id. The delivery-ack protocol acknowledges every dedup hit (the earlier
dispatch is the proof), and the TypeScript SDK does. It is now acked again,
still without re-dispatch. A below-anchor frame no handler has seen is
unchanged: dropped unacked, the server re-offers it.

Adds conformance/v1/engine-scenarios.json, run in both modes by a new
harness (identical bytes ship in the TypeScript SDK): drain gap + close
during an earlier handler or inside the gap window, a raising handler during
a drain, and an already-handled repeat below the live anchor as a frame and
as a drain row. Also pins the drain-cursor rule for a socket drop during an
earlier handler with a regression test.
…handler

Shared scenario (identical bytes in agentchat-typescript): page [g1, g3, h1],
h1's handler held, the socket drops, h1 released; the default-mode cursor
stops at g1. This drain awaits each handler while routing, so the drop lands
during a LATER row's handler and clears the parked g3 before the cursor is
computed. Copying a cursor that stops only while a row is still buffered
into _drain_row_ackable now fails this scenario and the reworked unit test
(the earlier-handler shape did not catch it).

CHANGELOG: say where the SDKs still differ on a below-anchor frame no
handler has seen.
…irst copy is running

After a reconnect the server pushes its stored backlog on the socket and the
REST drain brings the same rows. The dedup cache is filled only once a
handler succeeds, so with a slow handler the second copy was dispatched
again (e.g. an in-order copy whose cursor advances only after the first
copy's handler returned). _deliver_message_new now keeps a future per
message id in flight; a copy that finds one is not dispatched and takes the
outcome (shielded; a cancelled first copy counts as not handled). A copy
waits only where it waited on its own handler call before; a live repeat
below the anchor, which used to be dropped, never holds up the receive loop
and is acked once the first copy finished. Off with dedup_cache_size=0.

Also closes two races between concurrent copies: the per-conversation
cursor never moves backwards (_advance_past), and _cleanup_if_idle removes
only the state it was handed, never a newer connection's.

Shared scenarios repeat-while-handler-runs-in-order / -below-anchor
(identical bytes in agentchat-typescript).
…ver, never cleared unseen

_resolve_gap and _flush_parked_frames snapshotted the buffer, awaited each
handler, then cleared it. The receive loop keeps running during those
awaits, so a frame that parked meanwhile was cleared without dispatch:
under ack_on_receipt (acked at receipt) it was never handled; in the
default mode it waited unacked for the next reconnect. A concurrent
in-order arrival could also pop entries the snapshot still expected
(KeyError in the gap task) or hand them over twice.

Both now pop each frame before handing it over, lowest seq first, and keep
going until nothing is parked (the flush across every conversation, since
disconnect() flushes before it closes the socket). The cursor advances past
each frame after its handler, never backwards.

Shared scenario frame-arrives-while-a-lapsed-gap-is-handled, with a new op
gap_window_lapses (identical bytes in agentchat-typescript).
…aps resolve on a snapshot

Review of #11. An in-flight repeat no longer waits for the first copy
anywhere: _adopt_in_flight records the outcome and returns. A live repeat
is acked when the first copy finished cleanly, never if it failed; a drain
row's cursor reads the outcome when computed and stops if the first copy is
not done (the next drain covers it as a dedup hit), so a slow or hung live
handler no longer holds up later drain rows, and a handler that awaits
disconnect() while a repeat of its own message arrives no longer
deadlocks. Adoption is per connection generation: a re-offer on a later
connection is dispatched afresh even if the first copy still runs.

_resolve_gap and the shutdown flush work on the frames parked when they
start; each frame leaves the buffer and the cursor moves past it before its
handler is awaited, so in-order arrivals go to the receive loop, a frame
behind a new hole gets its own gap window and report, and nothing is
cleared unseen (a state being resolved is never dropped as idle).
disconnect() stops taking frames in before it flushes, so it returns
promptly while frames keep arriving; a frame acked at receipt is never
dropped by the flush.
…opted in-order repeat moves the cursor on

A gap fill whose fetch returned while disconnect() was flushing acked its
rows at receipt (ack_on_receipt), and the shutdown then cancelled the gap
task, possibly mid-handover: acked rows never handled. Once disconnect() has
begun, the fill now returns before acking (a gap fill marks nothing on the
server, which keeps the rows).

A live repeat that adopted a drain row still running its handler left the
cursor where it was, so the next live frame parked and opened a spurious gap
(a REST gap fill and a report 2 s later, though nothing was missing). The
in-order branch now moves the cursor on for an adopted copy exactly as for
a dispatch.

Docs: handlers in one conversation can overlap (they start in seq order),
as in the TypeScript SDK.
@sanctrl

sanctrl commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Folded into #12 (same reviewed head, merged conflict-free with the other two) so main gets one merge instead of three. Closing in favour of #12.

@sanctrl sanctrl closed this Nov 8, 2026
sanctrl added a commit that referenced this pull request Nov 8, 2026
…erved gate question fixtures (folds #9, #10, #11) (#12)

* conformance(v1): the served gate question records reply after the send

tray.sample.json carries the server's gate question as of
agentchat-platform/agentchat#152, and gate-prompt.expected.txt is its
rendering. Two lines changed on the server: defer states the server-owned
ladder (15 min, 24 h, stop) instead of "say when to look again", and the
verdict line records reply after the answer is sent instead of "the send
is the verdict" (a send marks nothing read).

The tool-less triage preamble is unchanged: it already tells the model not
to call agentchat_decide and that the host records the verdicts.

Byte-identical to agentchat-typescript's conformance/v1.

* fix(realtime): ack an already-handled repeat below the seq anchor; engine conformance scenarios

Default mode dropped a live message.new below the conversation's seq anchor
without an ack, even when this session had already dispatched that message
id. The delivery-ack protocol acknowledges every dedup hit (the earlier
dispatch is the proof), and the TypeScript SDK does. It is now acked again,
still without re-dispatch. A below-anchor frame no handler has seen is
unchanged: dropped unacked, the server re-offers it.

Adds conformance/v1/engine-scenarios.json, run in both modes by a new
harness (identical bytes ship in the TypeScript SDK): drain gap + close
during an earlier handler or inside the gap window, a raising handler during
a drain, and an already-handled repeat below the live anchor as a frame and
as a drain row. Also pins the drain-cursor rule for a socket drop during an
earlier handler with a regression test.

* test(conformance): drain gap with a socket drop during a later row's handler

Shared scenario (identical bytes in agentchat-typescript): page [g1, g3, h1],
h1's handler held, the socket drops, h1 released; the default-mode cursor
stops at g1. This drain awaits each handler while routing, so the drop lands
during a LATER row's handler and clears the parked g3 before the cursor is
computed. Copying a cursor that stops only while a row is still buffered
into _drain_row_ackable now fails this scenario and the reworked unit test
(the earlier-handler shape did not catch it).

CHANGELOG: say where the SDKs still differ on a below-anchor frame no
handler has seen.

* fix(realtime): never hand a message to the handlers twice while its first copy is running

After a reconnect the server pushes its stored backlog on the socket and the
REST drain brings the same rows. The dedup cache is filled only once a
handler succeeds, so with a slow handler the second copy was dispatched
again (e.g. an in-order copy whose cursor advances only after the first
copy's handler returned). _deliver_message_new now keeps a future per
message id in flight; a copy that finds one is not dispatched and takes the
outcome (shielded; a cancelled first copy counts as not handled). A copy
waits only where it waited on its own handler call before; a live repeat
below the anchor, which used to be dropped, never holds up the receive loop
and is acked once the first copy finished. Off with dedup_cache_size=0.

Also closes two races between concurrent copies: the per-conversation
cursor never moves backwards (_advance_past), and _cleanup_if_idle removes
only the state it was handed, never a newer connection's.

Shared scenarios repeat-while-handler-runs-in-order / -below-anchor
(identical bytes in agentchat-typescript).

* fix(realtime): a frame that parks while a gap is resolved is handed over, never cleared unseen

_resolve_gap and _flush_parked_frames snapshotted the buffer, awaited each
handler, then cleared it. The receive loop keeps running during those
awaits, so a frame that parked meanwhile was cleared without dispatch:
under ack_on_receipt (acked at receipt) it was never handled; in the
default mode it waited unacked for the next reconnect. A concurrent
in-order arrival could also pop entries the snapshot still expected
(KeyError in the gap task) or hand them over twice.

Both now pop each frame before handing it over, lowest seq first, and keep
going until nothing is parked (the flush across every conversation, since
disconnect() flushes before it closes the socket). The cursor advances past
each frame after its handler, never backwards.

Shared scenario frame-arrives-while-a-lapsed-gap-is-handled, with a new op
gap_window_lapses (identical bytes in agentchat-typescript).

* fix(realtime): progress beats dedup: nothing waits on another copy, gaps resolve on a snapshot

Review of #11. An in-flight repeat no longer waits for the first copy
anywhere: _adopt_in_flight records the outcome and returns. A live repeat
is acked when the first copy finished cleanly, never if it failed; a drain
row's cursor reads the outcome when computed and stops if the first copy is
not done (the next drain covers it as a dedup hit), so a slow or hung live
handler no longer holds up later drain rows, and a handler that awaits
disconnect() while a repeat of its own message arrives no longer
deadlocks. Adoption is per connection generation: a re-offer on a later
connection is dispatched afresh even if the first copy still runs.

_resolve_gap and the shutdown flush work on the frames parked when they
start; each frame leaves the buffer and the cursor moves past it before its
handler is awaited, so in-order arrivals go to the receive loop, a frame
behind a new hole gets its own gap window and report, and nothing is
cleared unseen (a state being resolved is never dropped as idle).
disconnect() stops taking frames in before it flushes, so it returns
promptly while frames keep arriving; a frame acked at receipt is never
dropped by the flush.

* fix(realtime): a gap fill acks nothing once disconnect() began; an adopted in-order repeat moves the cursor on

A gap fill whose fetch returned while disconnect() was flushing acked its
rows at receipt (ack_on_receipt), and the shutdown then cancelled the gap
task, possibly mid-handover: acked rows never handled. Once disconnect() has
begun, the fill now returns before acking (a gap fill marks nothing on the
server, which keeps the rows).

A live repeat that adopted a drain row still running its handler left the
cursor where it was, so the next live frame parked and opened a spurious gap
(a REST gap fill and a report 2 s later, though nothing was missing). The
in-order branch now moves the cursor on for an adopted copy exactly as for
a dispatch.

Docs: handlers in one conversation can overlap (they start in seq order),
as in the TypeScript SDK.
@sanctrl
sanctrl deleted the fix/engine-inflight-dedup branch November 11, 2026 12:24
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.

1 participant