fix(cli): preserve intra-turn text/tool order for ACP sessions#505
Merged
tiann merged 1 commit intotiann:mainfrom Apr 21, 2026
Merged
fix(cli): preserve intra-turn text/tool order for ACP sessions#505tiann merged 1 commit intotiann:mainfrom
tiann merged 1 commit intotiann:mainfrom
Conversation
Flush buffered assistant text before emitting tool_call and plan updates so the CLI preserves the order in which an ACP agent produced them. tool_call_update events do not flush: they are lifecycle events on an already-open tool call, and flushing there would split a text segment that spans the tool's execution. Cumulative-delta dedup, rate-limit, and internal-event filters in AcpMessageHandler are untouched; only the previously implicit "text is emitted at turn boundary" contract is relaxed. Existing tests covering ordering are reworked to assert the new contract, and new cases cover interleaved text/tool sequences, dedup across tool boundaries, and dedup preservation when text arrives between tool_call and tool_call_update.
There was a problem hiding this comment.
Findings
- No actionable findings on the added/modified lines.
Summary
- Review mode: initial
- No diff-attributable correctness, security, or regression issues found in the latest PR diff.
- Residual risk/testing gap: not run in automation here; coverage is strong for
text -> tool_call -> tool_resultordering and dedup boundaries, but I did not verify against a live ACP provider stream.
Testing
- Not run (automation)
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.
Problem
When using
hapi gemini(ACP backend), text chunks that arrive before atool call are rendered after all tool blocks in the web UI. A session
producing
text → tool_call → tool_result → textarrives at the Hub andWeb in the order
tool_call, tool_result, text, so the assistant'sreasoning appears below the tool output rather than above it.
This is an intra-turn ordering issue — distinct from the turn-completion
timing addressed in #356.
Solution
AcpMessageHandler.handleUpdatenow callsflushText()immediatelybefore dispatching a
tool_callorplanupdate — a new toolinvocation or plan closes the preceding text segment.
tool_call_update(tool result / progress) does NOT flush: it is alifecycle event on an already-open tool call, not a boundary between
text segments. Flushing there would split a text segment that spans
the tool's execution and break the cumulative-delta dedup inside
appendTextChunk.The cumulative-delta dedup, audience filtering, rate-limit parsing,
and internal-event suppression in
AcpMessageHandlerare untouched.AcpSdkBackend'swaitForSessionUpdateQuiet/turn_completelogicis not modified.
Tests
['text', 'tool_call', 'tool_result'].text → toolandtext → tool → text → tool.segment separated by tool events.
tool_callandtool_call_updateis preserved and emitted after
tool_result, not duplicated orfragmented.
event, overlap dedup) are unchanged and pass.