From 44ca4e78fe30e0e80020d6f2b16dc61fac593b69 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:47:08 +0100 Subject: [PATCH] fix: /status shows too little context when a reply ends on a tool result (#34622) When a reply ended on a tool result, for example because it was stopped right after a tool returned or the model call after it failed, /status left that result out, since it came back after the model's last reported token count. The result is still sent with the next message and context compaction counts it, so compaction could run while /status showed the chat well under the threshold. /status now counts those results too, the same way the compaction check does. Fixes #34617 --- src/lib/utils/contextUsage.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/utils/contextUsage.ts b/src/lib/utils/contextUsage.ts index a612b0d031..dd884d924c 100644 --- a/src/lib/utils/contextUsage.ts +++ b/src/lib/utils/contextUsage.ts @@ -40,7 +40,10 @@ const usageCheckpoints = new WeakMap(); export const captureUsageCheckpoint = (message: ContextMessage): void => { const usage = message.usage ?? message.info?.usage; - if (usage) usageCheckpoints.set(usage, estimateMessagesTokens([message])); + // Tool results that end the reply came back after the report. + const output = [...(message.output ?? [])] as { type?: string }[]; + while (output.at(-1)?.type === 'function_call_output') output.pop(); + if (usage) usageCheckpoints.set(usage, estimateMessagesTokens([{ ...message, output }])); }; export const estimateUsageGrowth = (message: ContextMessage): number => { @@ -49,7 +52,7 @@ export const estimateUsageGrowth = (message: ContextMessage): number => { const tokens = estimateMessagesTokens([message]); if (!usageCheckpoints.has(usage)) { // shortcut: loaded chats anchor here; persist checkpoints if reloads must retain in-flight estimates. - usageCheckpoints.set(usage, tokens); + captureUsageCheckpoint(message); } return Math.max(0, tokens - usageCheckpoints.get(usage)!); };