fix(agent): stop resume hydration from overfilling the context window#3331
Merged
Conversation
Cold-resume JSONL hydration selected up to 800k estimated tokens of history at 4 chars/token, but JSON-heavy tool payloads tokenize at ~2.5-3 chars/token, so the rebuilt transcript could exceed the model's real context window before the run even started. Because the hydrated messages carry zero usage metadata, the SDK cannot auto-compact ahead of the first API call, and every request (including compaction itself) fails with "prompt is too long", permanently bricking the task on every subsequent resume. Estimate at 3 chars/token and target roughly half the window (400k estimated for 1M-context models, 80k for 200k), leaving room for the system prompt, tools, skills, and the resumed run's own work. Generated-By: PostHog Code Task-Id: c6065620-5694-4478-a20a-fec4ccde8f1a
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
|
React Doctor found no issues in the changed files. 🎉 Reviewed by React Doctor for commit |
Generated-By: PostHog Code Task-Id: c6065620-5694-4478-a20a-fec4ccde8f1a
Generated-By: PostHog Code Task-Id: c6065620-5694-4478-a20a-fec4ccde8f1a
Contributor
|
Reviews (1): Last reviewed commit: "chore(agent): state budget rationale ind..." | Re-trigger Greptile |
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
Resuming a long-lived cloud task can fail hard with
Internal error: Prompt is too long, and once it does, every subsequent resume of that task fails the same way so the task is permanently stuckLoosely:
~/.claude, so the native session transcript is never on disk after a resume (warm: false). Every resume goes throughhydrateSessionJsonl, which rebuilds a transcript from the task's ACP notification logs.selectRecentTurnsbudgets 800k estimated tokens for 1M-context models at 4 chars/token. JSON-heavy tool payloads actually tokenize at ~2.5–3 chars/token, so the "800k" selection can be well over 1M real tokens. In the failing run, hydration kept 66 turns (~2.9M chars, estimated 732k tokens) that the API counted as 1,155,179 tokens against a 1,000,000 window. The 200k-window budget (150k estimated) has the same overflow, just less headroom to hide it.usage: 0on every message, so the SDK's context accounting starts near zero: it can't auto-compact before the first API call (the run'scompact_boundaryreportedpreTokens: 16250whilegetContextUsage()said 1.15M). The first call 400s, the SDK's recovery compaction can't help, and the turn fails as a non-recoverableagent_error.Changes