Repository navigation
fix(qqbot): queued and in-flight messages are lost when the gateway resumes after reconnect or restart - #109997
YangManBOBO wants to merge 1 commit into
Conversation
|
Codex review: needs changes before merge. Reviewed July 19, 2026, 10:13 PM ET / July 20, 2026, 02:13 UTC. Summary PR surface: Source -64, Tests +188. Total +124 across 5 files. Reproducibility: yes. at source level: a protocol-compatible gateway can send a message frame, keep its handler queued or in flight, drop the socket, and compare the subsequent RESUME sequence with the replay boundary. The PR body supplies this exact real-connection scenario and before/after output. Review metrics: none identified. Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Risk before merge
Maintainer options:
Copy recommended automerge instructionNext step before merge
Security Review findings
Review detailsBest possible solution: Rebase the narrow settlement-aware sequence change onto current Do we have a high-confidence way to reproduce the issue? Yes, at source level: a protocol-compatible gateway can send a message frame, keep its handler queued or in flight, drop the socket, and compare the subsequent RESUME sequence with the replay boundary. The PR body supplies this exact real-connection scenario and before/after output. Is this the best way to solve the issue? No. A settlement-aware RESUME cursor is the right repair direction, but this branch is not the best implementation while it removes the QQ Bot ingress lifecycle instead of composing with it. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against c5bc6c3d2dd2. Label changesLabel changes:
Label justifications:
Evidence reviewedPR surface: Source -64, Tests +188. Total +124 across 5 files. View PR surface stats
Acceptance criteria:
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
Review history (4 earlier review cycles)
|
|
Addressed the P1 review finding in 1c1923a13c: heartbeat and replay cursors are now separate. |
|
Both round-2 items addressed in e74c2d88aa: Handler-failure cursor policy — adopted the recommended option (preserve terminal failure semantics): a logged handler failure now settles in a check-test-types — the failure was this head's own |
|
@clawsweeper review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
e74c2d8 to
64d627c
Compare
|
Closing in favor of #111859 which applies the same SeqWatermark fix while preserving the QQ Bot ingress lifecycle, turn-adoption lifecycle, shutdown handling, and admission control intact. This replacement branch:
See #111859 for the full implementation. |
Closes #109996
What Problem This Solves
Fixes an issue where QQ Bot users' messages would silently vanish — no reply, no error — whenever the gateway connection dropped, the server requested a reconnect, or the gateway process restarted while their message was still queued or being processed.
The QQ gateway replays only events with
seqgreater than the RESUME seq, but the channel committed (and persisted to the SQLite session store) each frame's seq at receipt, before the queued handler ran. Any message in flight or in the in-memory per-peer queue at disconnect/restart time was skipped by the resume replay and permanently lost. Handler failures had the same outcome: the message was logged-and-dropped locally while the already-advanced seq defeated the protocol's replay. This is the same defect family as the ClickClack reconnect-cursor race fixed in #108577, with the added severity that the premature cursor is persisted across process restarts (5-minute resume window).Why This Change Was Made
Message-frame seqs now commit to the RESUME watermark only when the message settles. A new
SeqWatermarktracks in-flight message seqs: the resumable value stays just below the oldest unsettled message, so RESUME replays anything still queued, in flight, or failed; frames without queued work (READY, RESUMED, interactions, acks) commit immediately, exactly as before. Settling covers every terminal state: handler completed, handler failed with a logged drop (the existing failure contract), or intentional discard (queue-full eviction, urgent queue clear). Failed handlers settle deliberately: the watermark is connection-wide and the queue keeps processing later messages after a failure, so holding the cursor behind one poison message would replay already-successful traffic for unrelated peers on every reconnect. The watermark therefore stays behind only messages that are genuinely still queued or in flight — exactly the restart/reconnect loss window this PR closes. The previously fire-and-forget message dispatch also gets a rejection handler so routing failures are logged and left pending for replay. Heartbeats intentionally do NOT use the watermark: they report the latest received frame seq (receive cursor) so the client never looks behind while a handler is running; only RESUME and persisted session state use the settlement watermark.Delivery semantics move from at-most-once (silent loss) to at-least-once across reconnects: a crash after a handler completes but before the throttled session save can redeliver an already-handled message. That matches the durable-before-ack convention used by the Telegram/WhatsApp/Slack/Signal channels.
User Impact
QQ Bot messages received while an agent turn is running — or queued behind one — now survive reconnects, server-driven RECONNECTs, and gateway restarts within the resume window: the gateway replays them and the bot answers. Previously they disappeared with no trace. No config, protocol, or persisted-state shape changes.
Evidence
Live behavior proof (real GatewayConnection, real
wsclient, protocol-compatible local gateway)Driver: the production
GatewayConnection(real websocket client, real reconnect/backoff, real event dispatcher and per-peer message queue) connected to a local WebSocket server implementing the QQ gateway protocol: HELLO → IDENTIFY → READY(s=1), and RESUME → replay events withseq > resume seq. A message events=2is delivered while its handler is still running, then the server drops the socket.Before (current
main) — the client resumes at the already-committed seq and the message is lost:After (this patch) — the client resumes below the unprocessed message, the server replays it, and it is handled:
Regression coverage
seq-watermark.test.ts(new): commit/register/settle semantics, out-of-order settles, replay boundary, session reset.gateway-connection.test.ts(4 new integration tests): message seq is not committed until the handler completes; with a message in flight, heartbeats report the latest received seq (2) while reconnect RESUME asks for replay below the unprocessed message (1); a failed handler settles and a failure-then-later-success reconnect resumes past both (no cross-peer replay amplification); queue-full eviction settles so the watermark cannot wedge.main, all 4 new integration tests fail (RESUME seq advances past the in-flight message); the 7 pre-existing disconnect-status tests pass unchanged in both states.node scripts/run-vitest.mjsonseq-watermark.test.ts+gateway-connection.test.ts+message-queue.test.ts→ 26 passed.outbound-dispatch.test.tsfailures on this machine reproduce identically without this change (unrelated outbound media-path surface).oxlint,oxfmt, andgit diff --checkpass on the five changed files.Sibling-surface context
The receive-path audit that found this also checked the other channels for the same premature-cursor family: Telegram's default isolated ingress advances its offset only after durable spooling, WhatsApp journals inbound before ack, Slack and Signal are durable-before-ack, and iMessage clamps its catchup cursor below failed rows — qqbot was the remaining gateway-resume surface committing before processing.