fix: recover Claude Code sessions after truncation#1444
Open
contsun wants to merge 1 commit into
Open
Conversation
Detect max_tokens truncation, unparsed tool inputs, input validation errors, and zero-token empty results from Claude Code streams. Auto-continue ordinary max_tokens completions in the same session, reset poisoned sessions, and save a recovery handoff that is injected once into the next fresh session. Add user-facing i18n notices and prompt guidance to reduce oversized outputs and long heredocs.
484d190 to
c5f1d33
Compare
Author
|
Update: added detection for Claude Code's no-visible-output meta prompt (). This path was not a normal terminal result event, so the previous recovery logic would not trigger. Added and force-pushed the branch. |
Author
|
Update: added detection for Claude Code's no-visible-output meta prompt: This path is emitted as an Added regression test: Force-pushed the branch with commit |
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.
Summary
stop_reason=max_tokensand automatically continue ordinary truncations in the same session__unparsedToolInput,InputValidationError) and zero-token empty resultsWhy
When Claude Code hits
max_tokenswhile emitting a large tool call, the tool-use JSON can be truncated. That leaves the session in a poisoned state: cache reads drop to zero, large prompt prefixes are billed as fresh input, and subsequent turns may return zero-token empty results. This patch separates ordinary truncation (same-session auto-continue) from corrupted sessions (fresh session + recovery handoff).Test plan
git diff --checkgo test ./agent/claudecode -run 'TestHandleResultPropagatesMaxTokensStopReason|TestHandleAssistantUnparsedToolInputMarksNextResultUnhealthy|TestHandleUserInputValidationErrorMarksNextResultUnhealthy|TestHandleResultZeroTokenEmptyMarksUnhealthy' -vgo test ./core -run 'TestRecoveryHandoffInjectedOnceAfterUnhealthyReset|TestProcessInteractiveEvents_AutoContinuesSameSessionOnMaxTokens|TestProcessInteractiveEvents_UnhealthyResultResetsSessionInsteadOfEmptyResponse|TestUnsolicitedReader_UnhealthyResultResetsSession|TestI18n_AllKeysHaveEnglish' -vNote: a full
go test ./corerun hit an existing TempDir cleanup flaky inTestCUJ_A3_ImageReachesAgent; rerunning that test alone passed.