Repository navigation
Description
Before submitting
- I searched existing issues and did not find a duplicate.
- I included enough detail to reproduce or investigate the problem.
Area
apps/server
Summary
When switching providers or forking a thread to another provider (full_thread_summary / portable context handoff), any assistant_message larger than ~12–13 KB (roughly 3,000–3,500 tokens—e.g. a detailed design doc or implementation plan) is silently omitted from the handoff payload, while the remaining budget is filled with raw command_execution stdout from the same turn.
As a result, the target model receives the user's original prompt, raw shell dumps, and zero assistant replies, forcing it to notice the missing reply via the Omitted item IDs recovery note and spend extra tool calls (ToolSearch + t3_thread_read) just to read the message sitting directly above the fork/switch divider.
Root cause (main @ 5940d817a3)
In packages/provider-core/src/server/handoffBudget.ts:
-
selectHistory()charges 1 JSON-escaped UTF-8 byte per token against a 16,000 cap (lines 15–20, 210–222, 252–266):DEFAULT_HANDOFF_TOKEN_CAPis16_000(clamped byHANDOFF_BYTE_CAP = 64_000), andContextHandoffService.prepareProviderHandoffpassesbudget: tokenCap(16,000) toselectHistory().- Inside
selectHistory(), every candidate's cost is computed as:const cost = Math.max( Buffer.byteLength(JSON.stringify(historyResponseItems([message], "")[1])) + 1, Buffer.byteLength(JSON.stringify(renderHistoricalMessage(message))) + 4, ); if (cost > remaining) return;
- After fix(server): handoff budget estimates stored history in tokens, not bytes #34926 (fixing [Bug]: Handoff budget counts stored history as 1 token per byte, and ACP registry agents fall back to a 128k window #23142),
nativeContextEstimateswitched toestimateTextTokens()(ceil(UTF-8 bytes / 4)), butselectHistory()still charges 1 JSON-escaped byte per token. After subtracting the handoff header, reserved width forOmitted item IDs(up tobudget / 8= 2 KB, added in fix(server): context handoff names omitted items instead of a read from the thread's start #34847), the 256-byte padding, and the user's prompt, only ~12–13.7 KB remains for the latest assistant message—and a ~14.7 KB reply (~17.2 KB JSON-encoded with[Historical ...]wrapper) exceeds the entire16,000-byte cap on its own.
-
All-or-nothing ("intact items") selection drops the latest
assistant_messagewith no truncation fallback (lines 256–270):tryAdd(messages.findLastIndex((message) => message.role === "assistant"))returns immediately whencost > remaining.- Unlike
makeLegacyImportSummary()(which trims to fit the remaining budget),selectHistory()never truncates or summarizes an oversized reply—even when it is the only assistant reply in the handoff.
-
Unfiltered
command_executionoutputs backfill the dropped reply (lines 146–152, 168–170, 270):historicalMessage()includes fullcommand_executionoutput (up to ~8 KB each) withrole: "assistant".- After
tryAdd(lastAssistant)skips the oversizedassistant_message, the reverse fallback loop (for (let index = messages.length - 1; index >= 0; index--) tryAdd(index)) fills the budget with smallercommand_executionitems (e.g.Preparing workspace+ rawgit grepstdout). - Note that
GitManager.readThreadContextwraps items withlimitCommandOutput(item)before passing them toselectHistory(), whereasContextHandoffService.prepareProviderHandoffpasses rawcommand_executionitems as-is.
Steps to reproduce
- In any project thread on Provider A, ask for a detailed architecture or implementation plan so the turn runs a couple of shell commands and produces an
assistant_messageof ~14–15 KB (~3,500–4,000 tokens). - Fork the thread from that turn (or switch to Provider B in the same thread) and send
"review this plan". - Inspect the outgoing
prompt.offerin~/.t3/userdata/logs/provider/events.<threadId>.logor watch Provider B's first thought.
Expected behavior
- The latest
assistant_message(the plan directly above the fork/handoff divider) should be included in the handoff context—not dropped completely because a ~3.5k-token reply exceeds 16,000 JSON-escaped bytes. - At minimum:
selectHistory()'s budget should reflect actual tokens (e.g., usingestimateTextTokens()or scaling byte budget to ~4×DEFAULT_HANDOFF_TOKEN_CAP) rather than capping intact replies at ~12–13 KB.- When the latest
assistant_message(oruser_message) still exceedsremaining, truncate/excerpt it instead of silently omitting it and filling the budget with rawcommand_executionstdout. - Deprioritize or cap
command_executionoutput relative touser_message/assistant_messageinprepareProviderHandoff.
Actual behavior
Observed on two separate single-turn forks on the same day:
- Case 1 (14,737-char assistant plan, 15,433 UTF-8 bytes, 17,192 JSON-encoded bytes):
Selected 4 intact items; omitted 2 items(total handoff: 13,155 chars).- Selected:
user_message(536 B),command_execution(Preparing workspace, 552 B),command_execution(git grep ..., 9,024 B),command_execution(git grep ..., 2,118 B). - Omitted: one
command_executionitem and the onlyassistant_messagein the thread (turn:1:assistant:0). - Target model immediately thinks: "The user wants me to review a plan, but the earlier answer containing it is missing from what I can see. I'll pull that thread back up with
t3_thread_read..."
- Case 2 (13,049-char assistant reply):
Selected 2 intact items; omitted 1 items(total handoff: 2,383 chars).- Selected:
user_message+command_execution(Preparing workspace). - Omitted: the only
assistant_message(turn:1:assistant:0).
Impact
Major degradation or frequent failure
Version or commit
0.1.13-nightly.20261125.5258 (bf961f8121c6); still present on main @ 5940d817a3
Environment
Linux x64, T3 Code desktop nightly 0.1.13-nightly.20261125.5258, cross-provider fork/handoff (Antigravity ACP -> Claude Code)
Logs or stack traces
Context handoff (full_thread_summary):
Provider context handoff. Thread: <parent-thread-id>. Covered app runs: 1-1.
Source item range: ...
Recover an omitted item only when the request needs it, with t3_thread_read({threadId:"<parent-thread-id>",view:"activity",itemId:"<omitted item ID>",maxCharsPerItem:4000}); ...
Selected 4 intact items; omitted 2 items. Omitted item IDs, newest last: <tool-item-id>, <assistant-message-item-id>.
Workaround
Setting T3CODE_CONTEXT_HANDOFF_TOKEN_CAP=64000 (the hard HANDOFF_BYTE_CAP ceiling in handoffBudget.ts) raises the per-handoff byte ceiling 4× from ~13 KB to ~61 KB, which avoids dropping typical long responses until they exceed ~60 KB JSON-encoded.
Activity