@devries-cameron20 / t3code Mirror · read-only / commits / 9ec71438cd
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 11:00 UTC

fix(provider-core): handoff work reserve fits small context windows (#32598)

Co-authored-by: Zuri <zurih@rtx.internal> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Mia's AI Lab committed Nov 15, 2026 at 08:03 UTC 9ec71438cd43b0f3179ab5ca0c0a2a783fb8280a
3 files changed +20 -5
apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts
+16 -3
@@ -280,6 +280,17 @@ describe("handoff budget", () => {
280 );
281 }
282 assert.equal(handoffBudget({ ...base, tokenCap: 2_000 }), 2_000);
283 + // The work reserve never takes more than half of a small local window.
284 + for (const [modelContextWindow, nativeContextEstimate, expected] of [
285 + [8_192, 0, 4_086],
286 + [32_768, 0, 16_758],
287 + [131_072, 50_000, 48_294],
288 + ] as const) {
289 + assert.equal(
290 + handoffBudget({ ...base, tokenCap: 64_000, modelContextWindow, nativeContextEstimate }),
291 + expected,
292 + );
293 + }
294 });
295
296 it("keeps measured occupancy across a model change", () => {
@@ -408,13 +419,15 @@ describe("handoff budget", () => {
419 assert.isAtMost(historyCost(selected.messages, selected.context), budget);
420 }
421 assert.equal(handoffBudget({ ...input, modelContextWindow: 2_000_000 }), 16_000);
411 - assert.equal(handoffBudget({ ...input, modelContextWindow: 20_000 }), 0);
422 + // 20k window: half is reserved for work, then 27 prompt bytes and 8,192 per image.
423 + const smallWindowBudget = count === 1 ? 1_781 : 0;
424 + assert.equal(handoffBudget({ ...input, modelContextWindow: 20_000 }), smallWindowBudget);
425 assert.equal(
426 handoffBudget({
427 ...input,
428 providerThread: { ...providerThread, contextUsage: { usedTokens: 0, maxTokens: 20_000 } },
429 }),
417 - 0,
430 + smallWindowBudget,
431 );
432 assert.equal(
433 handoffBudget({
@@ -425,7 +438,7 @@ describe("handoff budget", () => {
438 contextUsage: { usedTokens: 0, maxTokens: 1_000_000, autoCompactThreshold: 20_000 },
439 },
440 }),
428 - 0,
441 + smallWindowBudget,
442 );
443 }
444 });
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+1 -1
@@ -603,7 +603,7 @@ describe("orchestration v2 provider switching", () => {
603 : scenario === "large-current-input" || priorImages || exhaustedTarget
604 ? {
605 getModelContextWindow: () =>
606 - scenario.includes("target-tight") ? 25_121 : 32_000,
606 + scenario.includes("target-tight") ? 18_241 : 32_000,
607 }
608 : {}),
609 ...(scenario.endsWith("-native") || scenario === "large-current-input"
packages/provider-core/src/server/handoffBudget.ts
+3 -1
@@ -118,13 +118,15 @@ export function handoffBudget(input: {
118 const native = usage?.usedTokens ?? input.nativeContextEstimate;
119 const current =
120 Buffer.byteLength(JSON.stringify(input.userText)) + attachmentTokenAllowance(input.attachments);
121 + // Small windows would otherwise reserve all of themselves for work.
122 + const workReserve = Math.min(Math.max(16_000, Math.ceil(window / 4)), Math.ceil(window / 2));
123 return Math.max(
124 0,
125 Math.min(
126 input.tokenCap,
127 // Cap imported history only; adapters own attachment transport limits.
128 HANDOFF_BYTE_CAP,
127 - window - native - current - Math.max(16_000, Math.ceil(window / 4)),
129 + window - native - current - workReserve,
130 ),
131 );
132 }