Skip to content

Commit c2c187d

Browse files
fix(server): handoff budget estimates stored history in tokens, not bytes
When a provider reports no usage, T3 estimated the native session's occupancy by counting every stored history byte as one token. About 100 KB of history (around 30k real tokens) then looked full against a 128k window, and switching back replaced the agent's native session with a fresh one carrying only a bounded handoff. Estimate that occupancy at ceil(UTF-8 bytes / 4), the same rule Codex uses for its own count. The new turn and imported history keep their pessimistic byte counts. Fixes #23142 AI-assisted; verified with the tests and tools listed above.
1 parent 3273abd commit c2c187d

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
handoffTokenCapConfig,
4646
handoffBudget,
4747
attachmentTokenAllowance,
48+
estimateTextTokens,
4849
contextUsageForHandoff,
4950
historicalMessage,
5051
latestNativeContextUsage,
@@ -998,7 +999,7 @@ export const layer: Layer.Layer<
998999
: 0;
9991000
return (
10001001
sum +
1001-
(historical === null ? 0 : Buffer.byteLength(historical.text)) +
1002+
(historical === null ? 0 : estimateTextTokens(historical.text)) +
10021003
nativeAttachments
10031004
);
10041005
}, 0)

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ describe("orchestration v2 provider switching", () => {
510510
capabilities: ClaudeProviderCapabilitiesV2,
511511
modelSelection: CLAUDE_MODEL_SELECTION,
512512
responseByRunOrdinal: scenario.includes("target-estimate")
513-
? { 2: "x".repeat(10_000) }
513+
? { 2: "x".repeat(64_000) }
514514
: {},
515515
capturedTurns,
516516
...(scenario.includes("retry") || scenario.includes("unsent") ? { failStartOnce } : {}),

‎packages/provider-core/src/server/handoffBudget.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ export function attachmentTokenAllowance(attachments: ReadonlyArray<ChatAttachme
9595
);
9696
}
9797

98-
// One UTF-8 byte per token is a pessimistic estimate, not a guarantee for custom models.
98+
// Stored history is mostly prose and tool output, about four UTF-8 bytes per token.
99+
export function estimateTextTokens(text: string): number {
100+
return Math.ceil(Buffer.byteLength(text) / 4);
101+
}
102+
103+
// One UTF-8 byte per token keeps the new turn and imported history pessimistic.
99104
export function handoffBudget(input: {
100105
readonly tokenCap: number;
101106
readonly userText: string;

0 commit comments

Comments
 (0)