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(agent): the budget respects what LM Studio actually loaded
purple71d3dd8775bd
diff --git a/src/lib/__tests__/agent-num-ctx.test.ts b/src/lib/__tests__/agent-num-ctx.test.tsindex c7e975a4..6ab0aa4a 100644--- a/src/lib/__tests__/agent-num-ctx.test.ts+++ b/src/lib/__tests__/agent-num-ctx.test.ts@@ -1,24 +1,42 @@import { describe, it, expect, vi, beforeEach } from 'vitest'-// The Ollama probe and the catalog lookup are the only I/O in the resolver.+// The Ollama probe, the catalog lookup and the provider's loaded-context+// answer are the only I/O in the resolver.vi.mock('../../api/ollama', () => ({getModelContextCached: vi.fn(),}))vi.mock('../context-compaction', () => ({getModelMaxTokens: vi.fn(),}))+vi.mock('../../api/providers', () => ({+ getProviderForModel: vi.fn(),+}))import { resolveAgentNumCtx } from '../agent-num-ctx'+import { AGENT_CONTEXT_CAP, DEFAULT_CONTEXT_CAP } from '../context-window'import { getModelContextCached } from '../../api/ollama'import { getModelMaxTokens } from '../context-compaction'+import { getProviderForModel } from '../../api/providers'const probe = getModelContextCached as unknown as ReturnType<typeof vi.fn>const catalog = getModelMaxTokens as unknown as ReturnType<typeof vi.fn>+const providerFor = getProviderForModel as unknown as ReturnType<typeof vi.fn>+// R19: what the provider reports as actually loaded. null = server did not+// say; 'absent' = a provider without the method at all (Anthropic etc.).+let loadedCtx: number | null | 'absent'describe('resolveAgentNumCtx — one num_ctx per model, per turn', () => {beforeEach(() => {probe.mockReset()catalog.mockReset()+ loadedCtx = 'absent'+ providerFor.mockReset()+ providerFor.mockImplementation((name: string) => ({+ provider: loadedCtx === 'absent'+ ? {}+ : { loadedContextLength: vi.fn(async () => loadedCtx) },+ modelId: name.includes('::') ? name.split('::').slice(1).join('::') : name,+ }))})it('lets an explicit override win without probing the model', async () => {@@ -27,10 +45,39 @@ describe('resolveAgentNumCtx — one num_ctx per model, per turn', () => {expect(catalog).not.toHaveBeenCalled()})- it('uses the model context capped for VRAM when there is no override', async () => {+ // 2026-08-06 — this used to assert 16384, the chat cap, and that number is+ // what made a 20 minute Coding run restart its own plan. The tool catalogue+ // alone measured 8488 tokens of the 16384 on the installed build, so the run+ // reached step 18 of 30, compacted, and began again at step 1. An agent turn+ // is the only thing this resolver serves, and it gets the agent ceiling.+ it('gives an agent turn the agent ceiling, not the chat one', async () => {+ probe.mockResolvedValue(131072)+ expect(await resolveAgentNumCtx('qwen2.5-coder:14b', 'ollama', 0)).toBe(AGENT_CONTEXT_CAP)+ expect(AGENT_CONTEXT_CAP).toBeGreaterThan(DEFAULT_CONTEXT_CAP)+ })++ it('leaves room for the work after the catalogue is paid for', async () => {+ // The point of the change, stated as the thing that has to stay true. The+ // measured catalogue cost has to leave the majority of the window free, or+ // compaction starts eating the run's own progress.+ const CATALOGUE_TOKENS = 8488 // measured, Ollama prompt_eval_count, 30 toolsprobe.mockResolvedValue(131072)- // DEFAULT_CONTEXT_CAP keeps the KV cache off a consumer GPU's back.- expect(await resolveAgentNumCtx('qwen2.5-coder:14b', 'ollama', 0)).toBe(16384)+ const ctx = await resolveAgentNumCtx('qwen2.5-coder:14b', 'ollama', 0)+ expect(ctx - CATALOGUE_TOKENS).toBeGreaterThan(ctx / 2)+ })++ it('NEGATIVE CONTROL: a model that cannot take the bigger window keeps its own', async () => {+ // The ceiling is a ceiling, never a request. Asking a model for more room+ // than it was trained on makes Ollama extrapolate RoPE and costs quality.+ probe.mockResolvedValue(8192)+ expect(await resolveAgentNumCtx('small:3b', 'ollama', 0)).toBe(8192)+ })++ it('NEGATIVE CONTROL: an unknown real maximum stays at the conservative cap', async () => {+ // 0 means the probe could not tell us. That is not permission to ask for+ // 32k, so the fallback is the chat cap, exactly as before this change.+ probe.mockResolvedValue(0)+ expect(await resolveAgentNumCtx('mystery:latest', 'ollama', 0)).toBe(DEFAULT_CONTEXT_CAP)})it('floors at 8192 so vision feedback never overflows a 4096-default model', async () => {@@ -81,4 +128,52 @@ describe('resolveAgentNumCtx — one num_ctx per model, per turn', () => {const extraction = await resolveAgentNumCtx('qwen2.5-coder:14b', 'ollama', 0)expect(extraction).toBe(chat)})++ // R19 (LM Studio, 2026-08-07, twice the cause of a run death): the model+ // CAN do 32k, LM Studio JIT-loaded it at 16k, the budget said 32k, and the+ // server hard-truncated the prompt — tool contract included — mid-plan.+ describe('R19: the budget respects what the server actually loaded', () => {+ it('clamps to the loaded context when it is below the model maximum', async () => {+ catalog.mockResolvedValue(32768)+ loadedCtx = 16384+ expect(await resolveAgentNumCtx('qwen3-4b', 'openai', 0, 'openai::qwen3-4b')).toBe(16384)+ })++ it('takes the loaded number even below the 8192 floor, because the floor cannot grow the allocation', async () => {+ catalog.mockResolvedValue(32768)+ loadedCtx = 4096+ expect(await resolveAgentNumCtx('qwen3-4b', 'openai', 0, 'openai::qwen3-4b')).toBe(4096)+ })++ it('NEGATIVE CONTROL: a server that does not say keeps the catalog budget', async () => {+ catalog.mockResolvedValue(32768)+ loadedCtx = null+ expect(await resolveAgentNumCtx('vllm-model', 'openai', 0, 'openai::vllm-model')).toBe(32768)+ })++ it('NEGATIVE CONTROL: a loaded context ABOVE the maximum never raises the budget', async () => {+ catalog.mockResolvedValue(8192)+ loadedCtx = 32768+ expect(await resolveAgentNumCtx('small-model', 'openai', 0, 'openai::small-model')).toBe(8192)+ })++ it('NEGATIVE CONTROL: a provider without the probe is untouched', async () => {+ catalog.mockResolvedValue(200000)+ loadedCtx = 'absent'+ expect(await resolveAgentNumCtx('claude-opus-4', 'anthropic', 0, 'anthropic::claude-opus-4')).toBe(200000)+ })++ it('NEGATIVE CONTROL: the user override still wins over everything', async () => {+ loadedCtx = 4096+ expect(await resolveAgentNumCtx('qwen3-4b', 'openai', 24576, 'openai::qwen3-4b')).toBe(24576)+ expect(providerFor).not.toHaveBeenCalled()+ })++ it('NEGATIVE CONTROL: Ollama never asks the openai probe', async () => {+ probe.mockResolvedValue(32768)+ loadedCtx = 4096+ await resolveAgentNumCtx('qwen2.5-coder:14b', 'ollama', 0)+ expect(providerFor).not.toHaveBeenCalled()+ })+ })})diff --git a/src/lib/agent-num-ctx.ts b/src/lib/agent-num-ctx.tsindex 7777dc8b..870cdfeb 100644--- a/src/lib/agent-num-ctx.ts+++ b/src/lib/agent-num-ctx.ts@@ -15,7 +15,8 @@*/import { getModelContextCached } from '../api/ollama'-import { effectiveContextWindow } from './context-window'+import { getProviderForModel } from '../api/providers'+import { AGENT_CONTEXT_CAP, effectiveContextWindow } from './context-window'import { getModelMaxTokens } from './context-compaction'/**@@ -26,8 +27,16 @@ import { getModelMaxTokens } from './context-compaction'* to resolve a cloud model's real window from the catalog** The override wins outright. Without one, Ollama models get their REAL context- * capped for VRAM, floored at 8192 so feeding a generated image back for vision- * feedback never overflows a 4096-default model.+ * capped at AGENT_CONTEXT_CAP, floored at 8192 so feeding a generated image back+ * for vision feedback never overflows a 4096-default model.+ *+ * The agent ceiling, not the chat one: this resolver only ever runs for a turn+ * that carries the tool catalogue, and that catalogue measured 8488 tokens on+ * the installed build, which is 52% of the chat cap before the work starts. A+ * run under the chat cap reached step 18 of 30, compacted, and restarted its+ * own plan (David, 2026-08-06: "Du kannst nicht eine Riesencoding Aufgabe in+ * 'nem sechzehn k Kontext rausschicken"). See context-window.ts for the VRAM+ * measurement that says the larger ceiling offloads rather than OOMs.** Cloud models resolve their REAL window from the model catalog. They used to* fall through to the flat 8192 — there is no KV-cache/VRAM cost on our side,@@ -46,7 +55,10 @@ export async function resolveAgentNumCtx(if (!override) {if (providerId === 'ollama') {try {- ctx = Math.max(effectiveContextWindow(await getModelContextCached(modelId), 0), 8192)+ ctx = Math.max(+ effectiveContextWindow(await getModelContextCached(modelId), 0, AGENT_CONTEXT_CAP),+ 8192,+ )} catch { /* keep the 8192 floor on failure */ }} else {try {@@ -54,6 +66,17 @@ export async function resolveAgentNumCtx(if (typeof real === 'number' && Number.isFinite(real) && real > 0) {ctx = Math.max(real, 8192)}+ // R19 (LM Studio, twice the cause of a run death): the KV cache is+ // allocated at JIT-load time and a prompt beyond it is hard-truncated+ // server-side, tool contract included. When the server says what it+ // actually loaded, that number IS the budget — even below the 8192+ // floor, because the floor cannot grow an allocation we do not+ // control. Ollama is different: there num_ctx sets the allocation.+ if (providerId === 'openai') {+ const { provider, modelId: rawId } = getProviderForModel(fullModelName ?? modelId)+ const loaded = await provider.loadedContextLength?.(rawId)+ if (typeof loaded === 'number' && loaded > 0 && loaded < ctx) ctx = loaded+ }} catch { /* keep the 8192 floor on failure */ }}}