Skip to content

fix(acp): serialize session updates to prevent out-of-order messages#21314

Closed
LydiaCai1203 wants to merge 2 commits intoanomalyco:devfrom
LydiaCai1203:fix-err-msg-order
Closed

fix(acp): serialize session updates to prevent out-of-order messages#21314
LydiaCai1203 wants to merge 2 commits intoanomalyco:devfrom
LydiaCai1203:fix-err-msg-order

Conversation

@LydiaCai1203
Copy link
Copy Markdown


Type of change

  • Bug fix

What does this PR do?

Problem: ACP session updates (sessionUpdate calls) can arrive at the client out of order because they are fired
concurrently from different event handlers. This causes garbled streaming output — for example, tool results
appearing before the tool call, or text chunks arriving in the wrong sequence. Additionally, there's a race
condition where SSE delta events haven't all arrived by the time end_turn fires, causing the final assistant message
to be truncated.

Fix:

  1. Serialized session updates — All sessionUpdate calls are now routed through a single promise chain (sendUpdate /
    sendChain), guaranteeing they are delivered in the order they were enqueued. Individual .catch() handlers are
    consolidated into the chain.
  2. Post-prompt reconciliation — After prompt() returns, reconcileAssistantMessage() fetches the complete assistant
    message from the API and delivers any text/reasoning content that the SSE delta events missed (comparing characters
    sent via deltas vs the final message length). A reconciledParts set prevents the delta handler from sending
    duplicates for parts already reconciled.
  3. question.asked event handling — Adds a new event handler that maps AskUserQuestion tool calls to the ACP
    permission flow, with support for multi-question/multi-select responses and "Other" custom text answers.
  4. Minor: adds debug logging for session processor stop reasons; updates the question tool to a two-step async
    pattern; adds missing question method to test context.

How did you verify your code works?

  • Tested ACP streaming locally — verified message chunks arrive in correct order
  • Verified truncated messages no longer occur after reconciliation
  • Tested question flow end-to-end through ACP permission UI

All ACP sessionUpdate calls are now routed through a promise chain
(`sendUpdate`) that guarantees ordering. After each prompt completes,
a reconciliation step fetches the final assistant message and delivers
any text/reasoning content that was missed by SSE delta events — fixing
a race where streaming events arrive after end_turn.

Also adds handling for the `question.asked` event via the ACP permission
flow, and updates the question tool to use a two-step async pattern.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions bot added needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Apr 7, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@LydiaCai1203 LydiaCai1203 mentioned this pull request Apr 8, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs:compliance This means the issue will auto-close after 2 hours. needs:issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant