Skip to content

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.

test(agent): keep-whole, the Code tab think gate and the cost-brake wiring are pinned (CA)

purpled5614959173f
diff --git a/src/lib/__tests__/a-result-fetched-again-stays-whole.test.ts b/src/lib/__tests__/a-result-fetched-again-stays-whole.test.ts
new file mode 100644
index 00000000..80a80ead
--- /dev/null
+++ b/src/lib/__tests__/a-result-fetched-again-stays-whole.test.ts
@@ -0,0 +1,59 @@
+/**
+ * CA (13./14.11.2026): a read the model fetched again after its result was
+ * capped keeps its NEWEST result whole (context-decay keepWhole). Capping it
+ * again only started the next read, cap, read lap, each a full paid request.
+ */
+import { describe, it, expect } from 'vitest'
+import { applyToolResultDecay, DECAY_RESULT_CHARS } from '../context-decay'
+
+type M = { role: string; content: string; key?: string; tool_call_id?: string; tool_calls?: unknown[] }
+
+const big = (tag: string) => tag + 'x'.repeat(DECAY_RESULT_CHARS * 3)
+
+/** Six iterations: the same file read in step 1 and step 4, other reads between. */
+function run(): M[] {
+ const out: M[] = [{ role: 'system', content: 's' }, { role: 'user', content: 'task' }]
+ const reads = ['a.ts', 'b.ts', 'c.ts', 'a.ts', 'd.ts', 'e.ts']
+ reads.forEach((f, i) => {
+ out.push({ role: 'assistant', content: '', tool_calls: [{ id: `c${i}` }] })
+ out.push({ role: 'tool', content: big(`${f}#${i}`), tool_call_id: `c${i}`, key: `file_read|${f}` })
+ })
+ return out
+}
+
+const keyOf = (m: M) => m.key
+
+describe('keepWhole', () => {
+ it('without it, the second read of a.ts ages out and is capped like the rest', () => {
+ const r = applyToolResultDecay(run(), { keyOf })
+ const second = r.messages.find((m) => m.content.startsWith('a.ts#3'))!
+ expect(second.content.length).toBeLessThan(DECAY_RESULT_CHARS * 2)
+ })
+
+ it('with it, the newest a.ts result stays whole while its older copy and the others are capped', () => {
+ const r = applyToolResultDecay(run(), { keyOf, keepWhole: new Set(['file_read|a.ts']) })
+ const first = r.messages.find((m) => m.content.startsWith('a.ts#0'))!
+ const second = r.messages.find((m) => m.content.startsWith('a.ts#3'))!
+ const other = r.messages.find((m) => m.content.startsWith('b.ts#1'))!
+ expect(second.content.length).toBe(big('a.ts#3').length)
+ expect(first.content.length).toBeLessThan(DECAY_RESULT_CHARS * 2)
+ expect(other.content.length).toBeLessThan(DECAY_RESULT_CHARS * 2)
+ })
+
+ it('needs keyOf to know which result is which; without it nothing is pinned', () => {
+ const r = applyToolResultDecay(run(), { keepWhole: new Set(['file_read|a.ts']) })
+ const second = r.messages.find((m) => m.content.startsWith('a.ts#3'))!
+ expect(second.content.length).toBeLessThan(DECAY_RESULT_CHARS * 2)
+ })
+})
+
+describe('the loops feed it', () => {
+ it('both agent loops pass the reads fetched again after a cap', async () => {
+ const { readFileSync } = await import('node:fs')
+ for (const f of ['../../hooks/useAgentChat.ts', '../../hooks/useCodex.ts']) {
+ const src = readFileSync(new URL(f, import.meta.url), 'utf8')
+ expect(src, f).toMatch(/keepWhole: neededAgain,/)
+ expect(src, f).toMatch(/if \(trimmedReadKeys\.has\(k\)\) neededAgain\.add\(k\)/)
+ }
+ })
+})
diff --git a/src/lib/__tests__/ca-night-wiring.test.ts b/src/lib/__tests__/ca-night-wiring.test.ts
new file mode 100644
index 00000000..59d0859c
--- /dev/null
+++ b/src/lib/__tests__/ca-night-wiring.test.ts
@@ -0,0 +1,56 @@
+/**
+ * CA (13./14.11.2026): the wiring of the night's fixes in the two agent loops,
+ * plus the small helpers they use. Behaviour of each piece has its own test;
+ * this file keeps the pieces connected.
+ */
+import { describe, it, expect, beforeEach } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { billingOf, creditRatesOf } from '../agent-core/credit-rates'
+import { useModelStore } from '../../stores/modelStore'
+
+const read = (p: string) => readFileSync(new URL(p, import.meta.url), 'utf8')
+const AGENT = read('../../hooks/useAgentChat.ts')
+const CODE = read('../../hooks/useCodex.ts')
+
+describe('Code tab: no <think> request on top of a catalogue reasoner', () => {
+ it('the tag request needs a model without its own thinkMode', () => {
+ const at = CODE.indexOf('reason through your thinking inside <think></think> tags')
+ const cond = CODE.slice(CODE.lastIndexOf('if (settings.thinkingEnabled', at), at)
+ expect(cond).toMatch(/codexThinkMode\(activeModel\) === undefined/)
+ })
+})
+
+describe('the cost brake in both loops', () => {
+ for (const [name, src] of [['Agent', AGENT], ['Code', CODE]] as const) {
+ it(`${name}: priced from the model, limits from settings, billing per step`, () => {
+ expect(src).toMatch(/rates: creditRatesOf\(activeModel\)/)
+ expect(src).toMatch(/runCreditLimit: useSettingsStore\.getState\(\)\.settings\.agentRunCreditLimit/)
+ expect(src).toMatch(/hourCreditLimit: useSettingsStore\.getState\(\)\.settings\.agentHourCreditLimit/)
+ expect(src).toMatch(/costBrake\.record\(turn\.promptEvalCount, \{ output: turn\.evalCount, billing: billingOf\(turn\) \}\)/)
+ })
+ it(`${name}: a warning and a pause also show above the chat`, () => {
+ expect((src.match(/show\('cost-brake', brake\.message, 'ruhig'\)/g) ?? []).length).toBe(2)
+ })
+ }
+})
+
+describe('creditRatesOf / billingOf', () => {
+ beforeEach(() => useModelStore.setState({ models: [] } as never))
+
+ it('reads the declared price, null without one', () => {
+ useModelStore.setState({ models: [
+ { name: 'lu-cloud::x', creditRates: { inputPerMillion: 20_000, outputPerMillion: 60_000 } },
+ { name: 'openai::local' },
+ ] } as never)
+ expect(creditRatesOf('lu-cloud::x')).toEqual({ inputPerMillion: 20_000, outputPerMillion: 60_000 })
+ expect(creditRatesOf('openai::local')).toBeNull()
+ expect(creditRatesOf(null)).toBeNull()
+ })
+
+ it('billing only as LU Cloud spells it', () => {
+ expect(billingOf({ billing: 'flash' })).toBe('flash')
+ expect(billingOf({ billing: 'credits' })).toBe('credits')
+ expect(billingOf({ billing: 'free' })).toBeUndefined()
+ expect(billingOf(null)).toBeUndefined()
+ })
+})