From d4c0596204e7dfd028dd9414eecc189ce69fb059 Mon Sep 17 00:00:00 2001 From: ekko Date: Tue, 28 Apr 2026 20:32:40 +0800 Subject: [PATCH] fix(chat): remove upstream usage values and pre-send inputTokens overwrite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove all evt.usage/parsed.usage references, only use local countTokens - Remove pre-send inputTokens calculation that was overwriting resume value with compressed context, causing incorrect context drop (70k → 40k) - run.completed now recalculates inputTokens with current snapshot + full messages including new ones from this run Co-Authored-By: Claude Opus 4.6 --- packages/client/src/stores/hermes/chat.ts | 14 -------------- .../server/src/services/hermes/chat-run-socket.ts | 11 +---------- 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/packages/client/src/stores/hermes/chat.ts b/packages/client/src/stores/hermes/chat.ts index 524ab323..3a0d1c46 100644 --- a/packages/client/src/stores/hermes/chat.ts +++ b/packages/client/src/stores/hermes/chat.ts @@ -783,13 +783,6 @@ export const useChatStore = defineStore('chat', () => { if (lastMsg?.isStreaming) { updateMessage(sid, lastMsg.id, { isStreaming: false }) } - if (evt.usage) { - const target = sessions.value.find(s => s.id === sid) - if (target) { - target.inputTokens = evt.usage.input_tokens - target.outputTokens = evt.usage.output_tokens - } - } // Server-computed usage (local countTokens, snapshot-aware) if ((evt as any).inputTokens != null) { const target = sessions.value.find(s => s.id === sid) @@ -1101,13 +1094,6 @@ export const useChatStore = defineStore('chat', () => { if (lastMsg?.isStreaming) { updateMessage(sid, lastMsg.id, { isStreaming: false }) } - if (evt.usage) { - const target = sessions.value.find(s => s.id === sid) - if (target) { - target.inputTokens = evt.usage.input_tokens - target.outputTokens = evt.usage.output_tokens - } - } // Server-computed usage (local countTokens, snapshot-aware) if ((evt as any).inputTokens != null) { const target = sessions.value.find(s => s.id === sid) diff --git a/packages/server/src/services/hermes/chat-run-socket.ts b/packages/server/src/services/hermes/chat-run-socket.ts index 8973ecdc..c1a5bd1c 100644 --- a/packages/server/src/services/hermes/chat-run-socket.ts +++ b/packages/server/src/services/hermes/chat-run-socket.ts @@ -436,15 +436,6 @@ export class ChatRunSocket { const headers: Record = { 'Content-Type': 'application/json' } if (apiKey) headers['Authorization'] = `Bearer ${apiKey}` - // Save input token count after compression (the actual context sent to model) - if (session_id && body.conversation_history) { - const state = this.sessionMap.get(session_id) - if (state) { - state.inputTokens = (body.conversation_history as any[]).reduce( - (sum, m) => sum + countTokens(m.content || ''), 0) - } - } - const res = await fetch(`${upstream}/v1/runs`, { method: 'POST', headers, @@ -568,7 +559,7 @@ export class ChatRunSocket { } } - // Track usage — recalculate with current snapshot + full messages + // Track usage — self-calculate with countTokens + snapshot if (parsed.event === 'run.completed') { const sid = session_id if (sid) {