Skip to content

fix: throw errors at idle session state#659

Open
qwertycxz wants to merge 3 commits into
agentclientprotocol:mainfrom
qwertycxz:master
Open

fix: throw errors at idle session state#659
qwertycxz wants to merge 3 commits into
agentclientprotocol:mainfrom
qwertycxz:master

Conversation

@qwertycxz
Copy link
Copy Markdown

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 labeled loop:. Except for fatal runtime exceptions, all returns and errors should be outside the loop: while (true) loop.

Error captures

All errors encountered from Claude Code no longer throw an Error. They are now stored in the errors array.

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 the while (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 a RequestError based on the lastAssistantError.

Modified tests

All sessions tested with messages will always ultimately switch to idle session state.

Copilot AI review requested due to automatic review settings May 14, 2026 08:10
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 RequestError only once the session is idle (or prompt handoff occurs).
  • Updates tests to consistently end mocked sessions with an idle session 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 thread src/acp-agent.ts
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[] = [];
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread src/acp-agent.ts
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,
},
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, there is no need to keep a helper function that is only used in one place. But what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Responses out-of-order when API error occurs

2 participants