The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(ui): the meter counts the run's tool chain once, not twice
David27d58ffa5902
diff --git a/src/lib/__tests__/token-usage.test.ts b/src/lib/__tests__/token-usage.test.tsindex f4a6d438..61015160 100644--- a/src/lib/__tests__/token-usage.test.ts+++ b/src/lib/__tests__/token-usage.test.ts@@ -8,6 +8,8 @@* Run: npx vitest run src/lib/__tests__/token-usage.test.ts*/import { describe, it, expect } from 'vitest'+import { readFileSync } from 'node:fs'+import { resolve } from 'node:path'import { computeContextFill, type FillMessage } from '../token-usage'import { estimateTokens } from '../context-compaction'@@ -83,3 +85,58 @@ describe('computeContextFill', () => {expect(computeContextFill(msgs).used).toBeLessThan(1400)})})++describe('a brand new chat starts at nothing', () => {+ const COUNTER = readFileSync(+ resolve(__dirname, '..', '..', 'components', 'chat', 'TokenCounter.tsx'),+ 'utf8',+ )++ it('an empty conversation has no fill', () => {+ expect(computeContextFill([])).toEqual({ used: 0, real: false, source: 'estimate' })+ })++ it('the counter draws nothing at all while the chat is empty', () => {+ // No render harness in this repo, so the empty-state exit is guarded at+ // the source, the way send-window.test.ts guards the denominator.+ expect(COUNTER).toMatch(/if \(!activeConversationId \|\| messages\.length === 0\) return null/)+ })+})++describe('the tool chain a run writes back is not new context', () => {+ // A coding run reports the size of the request it built while the store+ // still holds [user, assistant placeholder]. The hidden tool chain is+ // written back at run END and spliced in BEFORE the assistant message+ // (useCodex insertMessagesBefore). Those results were inside the built+ // request already, so adding them again doubles the meter on the very+ // first run of a fresh chat: 15k of real prompt read as 30k.+ const toolResult = (content: string): FillMessage =>+ ({ role: 'tool', content, hidden: true })++ const built = { tokens: 15000, atMessageCount: 2 }++ it('counts the persisted chain once, not twice', () => {+ const afterRun: FillMessage[] = [+ user('refactor the parser'),+ toolResult('x'.repeat(40000)),+ toolResult('y'.repeat(40000)),+ assistant('done'),+ ]+ const fill = computeContextFill(afterRun, built)+ expect(fill.source).toBe('built')+ expect(fill.used).toBe(15000 + estimateTokens('done') + 4)+ })++ it('still adds what the user typed after the build', () => {+ const afterRun: FillMessage[] = [+ user('refactor the parser'),+ toolResult('x'.repeat(40000)),+ assistant('done'),+ user('now run the suite'),+ ]+ const fill = computeContextFill(afterRun, built)+ expect(fill.used).toBe(+ 15000 + estimateTokens('done') + 4 + estimateTokens('now run the suite') + 4,+ )+ })+})diff --git a/src/lib/token-usage.ts b/src/lib/token-usage.tsindex 753b1cec..a989ba3c 100644--- a/src/lib/token-usage.ts+++ b/src/lib/token-usage.ts@@ -27,6 +27,8 @@ export interface FillMessage {content: stringthinking?: stringtoolCallSummary?: string+ /** Written by an agent run, not by the user. See computeContextFill. */+ hidden?: booleanusage?: {promptTokens: numbercompletionTokens: number@@ -73,6 +75,14 @@ export function computeContextFill(if (built && built.tokens > 0 && built.atMessageCount <= messages.length) {let used = built.tokensfor (let i = built.atMessageCount; i < messages.length; i++) {+ // A hidden message that shows up behind the anchor is the tool chain the+ // finished run wrote back into the store (useCodex splices it in BEFORE+ // its assistant message), and every byte of it was inside the request+ // this anchor measured. Counting it again doubled the meter on the first+ // run of a fresh chat: 15k of real prompt read as 30k, and it stayed+ // wrong until the next turn built a new anchor. Only what the user adds+ // after the build is new context.+ if (messages[i].hidden) continueused += visibleTokens(messages[i])}return { used, real: false, source: 'built' }