Repository navigation
Conversation
…ARS_PER_TOKEN Change TOOL_RESULT_CHARS_PER_TOKEN from 2 to 4 so that tool-result text is estimated at the same chars-per-token ratio as ordinary text. The old value of 2 caused the mid-turn precheck to over-estimate tool-result token pressure by ~2x, triggering destructive truncate_tool_results_only recovery on turns well within the real context window. Updated affected test thresholds to match the corrected estimates and added a regression test for the reporter's scenario: ~250k chars of tool-result text across ~20 calls on a 200k context window with 50k reserve tokens should no longer false-positive overflow. Co-Authored-By: Claude <[email protected]> Related to #101929
|
Thanks for the context here. I swept through the related work, and this is now duplicate or superseded. Close: this branch is a partial and currently dirty/conflicting landing candidate for the linked mid-turn precheck bug, and it is superseded by #101952, which changes the guard to estimate the provider-boundary projected tool-result view and includes live runtime proof. Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Canonical path: Close this branch and keep review focused on #101952; if maintainers want any narrow regression-test idea from this branch, fold it into that canonical PR. So I’m closing this here and keeping the remaining discussion on #101952. Review detailsBest possible solution: Close this branch and keep review focused on #101952; if maintainers want any narrow regression-test idea from this branch, fold it into that canonical PR. Do we have a high-confidence way to reproduce the issue? Yes, at source level: current main still has the mid-turn guard estimating contextMessages before the provider-boundary aggregate tool-result projection, and the linked issue supplies concrete logs and billed-usage comparisons. I did not rerun the live provider cron scenario in this read-only review. Is this the best way to solve the issue? No. This branch is a plausible partial fix to one ratio, but #101952 is the better landing path because it estimates the same projected prompt view the provider path sends and includes live runtime proof. Security review: Security review cleared: Security review cleared: the diff changes only TypeScript estimator/test code and does not touch dependencies, secrets, workflows, package metadata, downloads, or other supply-chain surfaces. AGENTS.md: found and applied where relevant. What I checked:
Likely related people:
Codex review notes: model internal, reasoning high; reviewed against 4115e832d833. |
Summary
The mid-turn precheck token estimator over-counted tool-result text by ~2× because
TOOL_RESULT_CHARS_PER_TOKEN=2treated tool results as twice as token-dense as ordinary text. Aligning it withESTIMATED_CHARS_PER_TOKEN=4eliminates the systematic over-estimate while retaining the 1.2× safety margin.TOOL_RESULT_CHARS_PER_TOKEN=2inestimateToolResultContentTokenPressure()causescontext-overflow-midturn-precheckto fire at ~2.3-2.6× above real provider-billed usage, triggering destructivetruncate_tool_results_onlyrecovery on turns well within the context windowTOOL_RESULT_CHARS_PER_TOKENfrom 2 to 4, matchingESTIMATED_CHARS_PER_TOKENused everywhere elsesrc/agents/embedded-agent-runner/run/preemptive-compaction.ts(one constant),src/agents/embedded-agent-runner/run/preemptive-compaction.test.ts(updated thresholds + regression test)SAFETY_MARGIN(still 1.2×),ESTIMATED_CHARS_PER_TOKEN(unchanged at 4),TOOL_RESULT_CHARS_PER_TOKEN_ESTIMATEintool-result-char-estimator.ts(separate char-estimation path, not the token estimator), aggregate truncation projection in the estimator (kept scope minimal)Change Type & Scope
Change Type
Scope
Linked Issue
Motivation
Users with
midTurnPrecheck.enabled: trueon tool-result-heavy agent turns (e.g. work-queue digest agents) experience false-positive overflow detection. The precheck firestruncate_tool_results_onlyrecovery that rewrites all prior tool results into tiny stubs, causing the model to re-fetch data it already obtained. This wastes tokens (~3× in the reporter's case), increases latency, and exposes long-running turns to transient provider errors — all while the turn is well within the real context window.The root cause is the conservative
TOOL_RESULT_CHARS_PER_TOKEN=2which was set when the mid-turn precheck was first introduced. Real tokenizers (GPT, Claude) encode English tool-result text at approximately the same 4 chars/token rate as ordinary text, making the 2 value a systematic 2× over-count.Review Contract
Real Behavior Proof
Behavior addressed: On a tool-result-heavy turn with ~250k chars of tool result text across ~20 calls, a 200k context window, and 50k reserve tokens, the old estimator produced
estimatedPromptTokens ≈ 162k(>150k prompt budget), triggering false overflow. The corrected estimator produces an estimate comfortably below 150k.Real environment tested: Ubuntu Linux, Node 22.19, branch
fix/midturn-precheck-estimator-101929, local checkoutExact steps or command run after this patch:
Evidence after fix:
Observed result after fix:
~250kchars of tool results + 200k context + 50k reserve →route=fits,shouldCompact=false,overflowTokens=0,estimatedPromptTokens < 150_000What was not tested:
estimateStringCharshandles encoding differences, but the chars-per-token ratio is encoding-agnostic)Verification Environment
Risks and Mitigations
ESTIMATED_CHARS_PER_TOKEN=4has been the system-wide default for all non-tool-result text without issues suggests this risk is low.Security Impact
Compatibility / Migration
Human Verification
AI Assistance 🤖
TOOL_RESULT_CHARS_PER_TOKEN=2provenance to commits2d033d2aa84andc08400ea7d5, proposed and implemented one-constant fix with test updates.Review Conversations
Related to #101929