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(chat): the context counter shows the real window from the first message
purple76aaa8379d29
diff --git a/src/components/chat/TokenCounter.tsx b/src/components/chat/TokenCounter.tsxindex 5ba2ca94..9386b460 100644--- a/src/components/chat/TokenCounter.tsx+++ b/src/components/chat/TokenCounter.tsx@@ -60,16 +60,19 @@ export function TokenCounter() {const rawUsed = fill.usedif (!activeConversationId || messages.length === 0) return null+ // No window known yet (the very first probe of this model in this session):+ // no number rather than a made-up one. A 16384 stand-in read as a real "16K"+ // (Gegenprobe 01.10.2026).+ if (ctx.sendWindow <= 0 && ctx.contextWindow <= 0) return null- // Resolved real context window; fall back to the VRAM-safe default only while- // the provider probe is still in flight (ctx not resolved yet). On a paid- // provider the denominator is the SEND window, not the model window: a+ // The resolved real context window (known here, see the return above). On a+ // paid provider the denominator is the SEND window, not the model window: a// 262k-context model whose steps are capped at 64k would otherwise sit quiet// at 25 percent forever, the red warning would never fire, and every support// case would arrive with a healthy meter next to a drained wallet (plan A2,// meter honesty).const window = ctx.sendWindow > 0 ? ctx.sendWindow : ctx.contextWindow- const maxTokens = window > 0 ? window : 16384+ const maxTokens = window// Cap the numerator at the active window: a long chat carried onto a// smaller-context model shows "8.0k/8.0k" (full), not "20k/8k".const usedTokens = Math.min(rawUsed, maxTokens)@@ -134,12 +137,9 @@ export function TokenCounter() {window: maxTokens,source: fill.source,real: fill.real,- // `maxTokens > 0` war immer wahr — auch fuer den 16384er-Notnagel- // weiter oben, den es nur gibt, solange die Anbieter-Abfrage laeuft.- // Der Zaehler behauptete damit sekundenlang ein bekanntes Fenster:- // 14k belegt auf einem 128k-Modell las sich als "triggers on the next- // message", bis die Antwort kam. `window` ist die Zahl VOR dem- // Notnagel und damit die ehrliche Auskunft.+ // Ein unbekanntes Fenster kommt hier nicht mehr an (frueher stand+ // dafuer ein 16384er-Notnagel, und 14k auf einem 128k-Modell las sich+ // sekundenlang als "triggers on the next message").windowIsTrue: window > 0,messageCount: sichtbareAnzahl,threshold: autoSchwelle,diff --git a/src/hooks/__tests__/the-context-window-is-known-on-remount.test.ts b/src/hooks/__tests__/the-context-window-is-known-on-remount.test.tsnew file mode 100644index 00000000..f2890b27--- /dev/null+++ b/src/hooks/__tests__/the-context-window-is-known-on-remount.test.ts@@ -0,0 +1,47 @@+/**+ * @vitest-environment jsdom+ *+ * Gegenprobe 01.10.2026: the context counter mounts with a conversation's+ * first message. A fresh mount started unresolved and divided by a stand-in+ * for the length of the provider probe: "18/16K", then "30/32K". A mount now+ * starts from the window this model last resolved to.+ */+import { describe, it, expect, vi, beforeEach } from 'vitest'+import { renderHook, waitFor } from '@testing-library/react'++const probe = vi.hoisted(() => ({ calls: 0, release: null as null | (() => void) }))++vi.mock('../../lib/context-compaction', async (importOriginal) => {+ const real = await importOriginal<typeof import('../../lib/context-compaction')>()+ return {+ ...real,+ getModelMaxTokens: async () => {+ probe.calls+++ if (probe.calls > 1) await new Promise<void>((r) => { probe.release = r })+ return 131072+ },+ }+})++import { useActiveContextWindow } from '../useActiveContextWindow'+import { useModelStore } from '../../stores/modelStore'++const MODEL = 'lu-cloud::zai-org/GLM-5.3'++beforeEach(() => {+ useModelStore.setState({ activeModel: MODEL })+})++describe('the counter after a remount', () => {+ it('knows the window at once, the probe only refreshes it', async () => {+ const first = renderHook(() => useActiveContextWindow())+ await waitFor(() => expect(first.result.current.sendWindow).toBeGreaterThan(0))+ const known = first.result.current.sendWindow+ first.unmount()++ // The second probe hangs: whatever shows now comes from before.+ const second = renderHook(() => useActiveContextWindow())+ expect(second.result.current.sendWindow).toBe(known)+ probe.release?.()+ })+})diff --git a/src/hooks/useActiveContextWindow.ts b/src/hooks/useActiveContextWindow.tsindex 0b392178..55187018 100644--- a/src/hooks/useActiveContextWindow.ts+++ b/src/hooks/useActiveContextWindow.ts@@ -107,6 +107,16 @@ export function remoteWindowSource(return providerId === 'lu-cloud' && max > 0 ? 'probe' : 'guess'}+/**+ * The last window each model resolved to, for the whole app session. The+ * counter mounts with a conversation's first message, and a fresh mount used+ * to start unresolved: for the length of the provider probe it divided by a+ * stand-in and read "18/16K", then "30/32K" (Gegenprobe 01.10.2026). A mount+ * now starts from what this model last resolved to and the probe refreshes+ * it, so the number only changes when the window really did.+ */+const lastResolved = new Map<string, ActiveContext>()+export function useActiveContextWindow(reloadTick = 0): ActiveContext {const activeModel = useModelStore((s) => s.activeModel)const override = useSettingsStore((s) => s.settings.contextWindowOverride)@@ -122,7 +132,10 @@ export function useActiveContextWindow(reloadTick = 0): ActiveContext {// about the counter never lying, and "62k of 262k" under a model that has// 8k is exactly the lie. Unresolved reads as unknown, which is the state// every consumer already handles on mount.- const [resolved, setResolved] = useState<{ model: string; ctx: ActiveContext } | null>(null)+ const [resolved, setResolved] = useState<{ model: string; ctx: ActiveContext } | null>(() => {+ const known = activeModel ? lastResolved.get(activeModel) : undefined+ return activeModel && known ? { model: activeModel, ctx: known } : null+ })// Re-read whenever a model reload finishes anywhere (the Context dropdown// fires this), so every consumer — counter AND dropdown — reflects the new@@ -138,7 +151,10 @@ export function useActiveContextWindow(reloadTick = 0): ActiveContext {if (!activeModel) returnlet cancelled = falseconst providerId = getProviderIdFromModel(activeModel)- const setState = (ctx: ActiveContext) => setResolved({ model: activeModel, ctx })+ const setState = (ctx: ActiveContext) => {+ lastResolved.set(activeModel, ctx)+ setResolved({ model: activeModel, ctx })+ };(async () => {// ── Ollama: num_ctx is per-request, so what we send == what runs. ──