fix: throw errors at idle session state#659
Open
qwertycxz wants to merge 3 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adjusts the ACP agent’s prompt-processing control flow to ensure Claude Code errors are surfaced only after the session reaches an idle state (or when the next queued prompt replay is observed), addressing the out-of-order response behavior described in #654.
Changes:
- Refactors the
prompt()message-consumption loop to use a labeled loop with a single “break out” path, deferring returns/throws until after the loop completes. - Changes error handling to accumulate Claude Code errors during streaming and throw a
RequestErroronly once the session is idle (or prompt handoff occurs). - Updates tests to consistently end mocked sessions with an
idlesession state message via a shared injection helper.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/acp-agent.ts | Refactors prompt loop exit/error behavior; defers throwing until idle/handoff; accumulates encountered errors. |
| src/tests/acp-agent.test.ts | Introduces injectSessionWithMessages() to standardize mocked session message streams ending in idle; updates affected test cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
717
to
+724
| // When the Claude SDK classifies a turn as failed (e.g. rate limit, auth | ||
| // problem, billing), it sets a categorical `error` field on the | ||
| // `SDKAssistantMessage` that precedes the final `result` message. We | ||
| // capture it here so the subsequent `RequestError.internalError` can | ||
| // forward it to clients as structured `data`, sparing them from | ||
| // pattern-matching on the human-readable message text. | ||
| let lastAssistantError: SDKAssistantMessageError | undefined; | ||
| const errors: string[] = []; |
Comment on lines
+1161
to
+1173
| return { | ||
| stopReason, | ||
| usage: { | ||
| cachedReadTokens: session.accumulatedUsage.cachedReadTokens, | ||
| cachedWriteTokens: session.accumulatedUsage.cachedWriteTokens, | ||
| inputTokens: session.accumulatedUsage.inputTokens, | ||
| outputTokens: session.accumulatedUsage.outputTokens, | ||
| totalTokens: | ||
| session.accumulatedUsage.cachedReadTokens + | ||
| session.accumulatedUsage.cachedWriteTokens + | ||
| session.accumulatedUsage.inputTokens + | ||
| session.accumulatedUsage.outputTokens, | ||
| }, |
Author
There was a problem hiding this comment.
IMO, there is no need to keep a helper function that is only used in one place. But what do you think?
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.
Fix #654.
A cleanup of #480, and a cleanup of the cleanup of #463.
#463 used idle session state as end of turn on success but not on failure, which caused #654. This PR fixes it.
Modified control flow
The
while (true)loop is labeledloop:. Except for fatal runtime exceptions, all returns and errors should be outside theloop: while (true)loop.Error captures
All errors encountered from Claude Code no longer throw an
Error. They are now stored in theerrorsarray.Return and throw
Only when the session is idle, or when the next prompt message is reached (in which case the session will not become idle, according to the leaked Claude Code source),
break loop;is executed. This is the only exit point from thewhile (true)loop (aside from fatal runtime errors).Outside the
loop: while (true)loop, we return the JSON RPC result if no error occurred, or throw aRequestErrorbased on thelastAssistantError.Modified tests
All sessions tested with messages will always ultimately switch to idle session state.