fix: drain background task turns before processing prompt response#359
Closed
ElleNajt wants to merge 2 commits intoagentclientprotocol:mainfrom
Closed
fix: drain background task turns before processing prompt response#359ElleNajt wants to merge 2 commits intoagentclientprotocol:mainfrom
ElleNajt wants to merge 2 commits intoagentclientprotocol:mainfrom
Conversation
When background tasks (e.g. `sleep` with `run_in_background`) complete between prompts, their completion messages are buffered in the SDK's async generator. On the next prompt() call, these buffered messages are processed first and sent as ACP notifications, causing responses to appear shifted to later prompts. Fix: detect background task turns by their message sequence (task_notification → init → ... → result) and skip them before processing the real prompt response. An `init` preceded by `task_notification` starts a background turn; an `init` NOT preceded starts the real response. Also remove noisy "No onPostToolUseHook found" error log in tools.ts, which fires benignly when background task tool_use hooks aren't registered (because their stream events are now skipped). Fixes: xenodim/agent-shell#201 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
Related: #353 |
The "No onPostToolUseHook found for tool use ID" message was logged via logger.error(), which goes to stderr. acp.el treats every non-empty stderr line as an error and agent-shell displays it in the "Notices" drawer — making an expected condition (subagent tools, server_tool_use) appear as a user-visible error. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
can't reopen this PR bc of a force push, it's now here: #388 |
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.
I've run into a problem in agent-shell where if tasks return in the background, Claude's responses to them are returned instead of it's responses to my messages, and messages can get out of sync: xenodium/agent-shell#201
I put Claude in a vm with a test of the bug and it solved it; it produced a patch that has fixed it locally for me. I haven't had time to think about what this code is doing (and it's not a priority).
All I can claim is that it seems reasonable and works, so here is a draft PR with the fix and tests in case it's useful to anyone else.
Here is Claude's description of the problem and solution: