fix(claude): flush OutgoingMessageQueue before consuming next user turn#909
Open
swear01 wants to merge 1 commit into
Open
fix(claude): flush OutgoingMessageQueue before consuming next user turn#909swear01 wants to merge 1 commit into
swear01 wants to merge 1 commit into
Conversation
OutgoingMessageQueue.scheduleProcessing() defers socket.emit() via setTimeout(fn,0) — a macrotask. The Claude SDK's nextMessage() callback runs in a microtask chain, which executes before that macrotask fires. This means messages-consumed for turn N+1 can be sent to the hub before the queued agent messages from turn N have been emitted. The hub stamps invokedAt on the N+1 user message at receive time, and then stores the late-arriving agent messages with created_at > invokedAt_N+1. Since compareMessages sorts by invokedAt ?? createdAt ascending, those agent messages sort permanently below the N+1 user message. Fix: await messageQueue.flush() at the top of nextMessage() so all pending outgoing agent messages are sent through the socket before messages-consumed is dispatched. Closes tiann#908 via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
There was a problem hiding this comment.
Findings
No issues found in the modified lines.
Summary
Review mode: initial
Reviewed the full latest diff for cli/src/claude/claudeRemoteLauncher.ts. The new messageQueue.flush() before consuming the next user turn matches the existing queue semantics: it releases pending delayed assistant messages and preserves send order before messages-consumed can stamp the next user message. Residual risk: this turn-boundary ordering behavior is not covered by an integration/regression test.
Testing
Not run (automation): bun is not installed in this runner environment, so bun test cli/src/claude/utils/OutgoingMessageQueue.test.ts failed before execution.
HAPI Bot
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.
Summary
OutgoingMessageQueue.scheduleProcessing()deferssocket.emit()viasetTimeout(fn, 0)(macrotask)nextMessage()callback runs in a microtask chain — before thatsetTimeoutfiresmessages-consumedfor turn N+1 to reach the hub before the queued agent messages from turn N, causing the hub to stampinvokedAt_N+1earlier than those agent messages'created_atcompareMessagessorts ascending byinvokedAt ?? createdAt, the late-stamped agent messages sort permanently below the N+1 user messageFix:
await messageQueue.flush()at the top ofnextMessage()so all pending outgoing agent messages are sent through the socket beforemessages-consumedis dispatched.Test plan
Closes #908
🤖 Generated with Claude Code