From 272e07795ef18b9cc30f9dff1631a1aaf02177d0 Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Fri, 10 Apr 2026 19:44:19 -0500 Subject: [PATCH] Soften tool output eviction: keep prefix and require 2+ assistant turns - Preserve the first 1 KiB of truncated tool output instead of replacing with a zero-information placeholder, so the model retains partial context and can decide whether to re-fetch. - Only evict tool outputs that are at least 2 assistant turns old, not everything before the most recent assistant message. This prevents truncating outputs the model may still need on the immediately following turn in multi-step agentic workflows. --- src/lib/SessionManager.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index dab7e136..9b0e5eb1 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -396,29 +396,38 @@ export class SessionManager { }); }; - // Replace large tool result content with a short summary for messages the - // model has already seen and responded to. Like evictSeenImages, this is a - // send-time transformation only — session.messages stays intact for the UI. + // Replace large tool result content with a truncated prefix for messages the + // model has already seen and responded to at least twice. Like evictSeenImages, + // this is a send-time transformation only — session.messages stays intact for + // the UI. We keep the first TOOL_OUTPUT_EVICTION_THRESHOLD bytes so the model + // retains partial context and can decide whether to re-fetch the full output. const TOOL_OUTPUT_EVICTION_THRESHOLD = 1024; // 1 KiB + const TOOL_OUTPUT_MIN_ASSISTANT_TURNS = 2; // Only evict after 2+ assistant responses const evictSeenToolOutputs = (msgs: OpenAI.Chat.Completions.ChatCompletionMessageParam[]): OpenAI.Chat.Completions.ChatCompletionMessageParam[] => { - // Find the index of the last assistant message — everything before it - // has already been seen and responded to by the model. - let lastAssistantIdx = -1; - for (let i = msgs.length - 1; i >= 0; i--) { + // Collect indices of all assistant messages. + const assistantIndices: number[] = []; + for (let i = 0; i < msgs.length; i++) { if (msgs[i].role === 'assistant') { - lastAssistantIdx = i; - break; + assistantIndices.push(i); } } - if (lastAssistantIdx === -1) return msgs; + + // Need at least TOOL_OUTPUT_MIN_ASSISTANT_TURNS + 1 assistant messages: + // the ones that have "seen" the tool output, plus the current pending turn. + if (assistantIndices.length < TOOL_OUTPUT_MIN_ASSISTANT_TURNS + 1) return msgs; + + // The eviction cutoff is the assistant message N turns back from the end. + // Tool results before this index have been processed by at least N assistant + // turns, so truncating them is safe. + const cutoffIdx = assistantIndices[assistantIndices.length - TOOL_OUTPUT_MIN_ASSISTANT_TURNS]; return msgs.map((msg, idx) => { - if (idx >= lastAssistantIdx) return msg; + if (idx >= cutoffIdx) return msg; if (msg.role !== 'tool') return msg; if (typeof msg.content !== 'string') return msg; if (msg.content.length <= TOOL_OUTPUT_EVICTION_THRESHOLD) return msg; - return { ...msg, content: '[Previous tool output truncated]' }; + return { ...msg, content: msg.content.slice(0, TOOL_OUTPUT_EVICTION_THRESHOLD) + '\n\n[… truncated]' }; }); };