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.

fix(erinnerungen): kein Denkblock als Erinnerung, 800 statt 500 Token

purple26efe2e2e487
diff --git a/src/hooks/__tests__/memory-silent-call-gate.test.ts b/src/hooks/__tests__/memory-silent-call-gate.test.ts
index 67a1f548..3df9523f 100644
--- a/src/hooks/__tests__/memory-silent-call-gate.test.ts
+++ b/src/hooks/__tests__/memory-silent-call-gate.test.ts
@@ -283,3 +283,48 @@ describe('the existing guards still hold', () => {
expect(chatStream).not.toHaveBeenCalled()
})
})
+
+/**
+ * R5-26. The extraction ran on a 500 token budget. A model that pads the JSON
+ * with prose or a short think block tore off mid-object, and the whole turn was
+ * lost without a word, which is the same failure R5-25 fixes from the parsing
+ * side. 800 is the web's number (apps/web/hooks/useMemory.ts:98-101).
+ */
+describe('the budget the extraction asks for', () => {
+ it('R5-26: asks for 800 tokens, so a padded answer still arrives whole', async () => {
+ activeModel = 'qwen3:8b'
+ await threeTurns()
+ expect(chatStream).toHaveBeenCalledTimes(1)
+ expect(chatStream.mock.calls[0][2]).toMatchObject({ maxTokens: 800 })
+ })
+
+ it('R5-26 NEGATIVKONTROLLE: temperature and the context window are untouched', async () => {
+ activeModel = 'qwen3:8b'
+ await threeTurns()
+ expect(chatStream.mock.calls[0][2]).toMatchObject({ temperature: 0.1, contextWindow: 8192 })
+ })
+
+ it('R5-26: a 700 token answer is still read in full', async () => {
+ // Four characters to the token, the rule of thumb this app sizes budgets
+ // with: 700 tokens is roughly 2800 characters. Under the old 500 the reply
+ // would have been cut before its closing brace and parsed to nothing.
+ activeModel = 'qwen3:8b'
+ const padding = 'a'.repeat(2600)
+ const lang = JSON.stringify({ shouldSave: true, memories: [{ type: 'user', title: 'Long fact', description: 'Long fact', content: padding, tags: [] }] })
+ expect(lang.length).toBeGreaterThan(4 * 500)
+ expect(lang.length).toBeLessThan(4 * 800)
+ chatStream.mockImplementation(() => (async function* () { yield { content: lang, done: true } })())
+ await threeTurns()
+ expect(addMemory).toHaveBeenCalledTimes(1)
+ expect(addMemory).toHaveBeenCalledWith(expect.objectContaining({ content: padding }))
+ })
+
+ it('R5-25: a think block around the answer is not stored as a memory', async () => {
+ activeModel = 'qwen3:8b'
+ chatStream.mockImplementation(() => (async function* () {
+ yield { content: '<think>Maybe {"shouldSave": true, "memories": [{"type": "user", "title": "Guess", "content": "A stray thought", "tags": []}]} would do.</think>{"shouldSave": false, "memories": []}', done: true }
+ })())
+ await threeTurns()
+ expect(addMemory).not.toHaveBeenCalled()
+ })
+})
diff --git a/src/hooks/useMemory.ts b/src/hooks/useMemory.ts
index db3a69b7..5a2c5fd6 100644
--- a/src/hooks/useMemory.ts
+++ b/src/hooks/useMemory.ts
@@ -159,7 +159,10 @@ export async function extractMemoriesFromPair(
let fullResponse = ''
const stream = provider.chatStream(modelId, messages, {
temperature: 0.1,
- maxTokens: 500,
+ // 800 tokens leaves headroom for models that pad the JSON with prose or a
+ // short think block. At 500 the extraction tore off mid-object and the
+ // whole turn was lost without a word. Same number as the web.
+ maxTokens: 800,
contextWindow: numCtx,
})
diff --git a/src/lib/__tests__/memory-extraction.test.ts b/src/lib/__tests__/memory-extraction.test.ts
index 2e98d038..2c0c2325 100644
--- a/src/lib/__tests__/memory-extraction.test.ts
+++ b/src/lib/__tests__/memory-extraction.test.ts
@@ -301,3 +301,50 @@ describe('parsers survive prose around the JSON', () => {
expect(parseResolutionResponse('no idea {really}').action).toBe('ADD')
})
})
+
+/**
+ * R5-25. `extractJsonObject` takes the FIRST balanced object that parses, and
+ * `useMemory` never ran the response through the think splitter, so a brace pair
+ * the model wrote while thinking out loud could be stored as the user's memory.
+ * The three cases are the web's word for word
+ * (apps/web/lib/__tests__/memory-extraction.test.ts:152-171).
+ */
+describe('parseExtractionResponse — reasoning models', () => {
+ it('strips a leading think block before the JSON', () => {
+ const response = '<think>The user mentioned they are a nurse. That is worth saving.</think>\n{"shouldSave": true, "memories": [{"type": "user", "title": "Role", "content": "Works as a nurse", "tags": []}]}'
+ const result = parseExtractionResponse(response)
+ expect(result.shouldSave).toBe(true)
+ expect(result.memories[0].title).toBe('Role')
+ })
+
+ it('rejects an unterminated think block (token budget died mid-think)', () => {
+ const response = '<think>Let me consider whether {"shouldSave": true} applies here because'
+ const result = parseExtractionResponse(response)
+ expect(result.shouldSave).toBe(false)
+ expect(result.memories).toEqual([])
+ })
+
+ it('ignores braces inside the think block', () => {
+ const response = '<think>maybe {"shouldSave": true, "memories": []} hmm</think>{"shouldSave": false, "memories": []}'
+ const result = parseExtractionResponse(response)
+ expect(result.shouldSave).toBe(false)
+ })
+
+ it('HAUPTFALL: a memory the model only WEIGHED inside the think block is not saved', () => {
+ // The discriminating case. The other three pass on the old code by luck:
+ // their think blocks hold no object that survives the shape check, so the
+ // first balanced object that parses happens to be the real answer anyway.
+ // Here the thought is a complete, well-formed extraction result, so without
+ // the strip the model's musing becomes the user's memory.
+ const response = '<think>Maybe {"shouldSave": true, "memories": [{"type": "user", "title": "Guess", "description": "Guess", "content": "A stray thought", "tags": []}]} would do.</think>{"shouldSave": false, "memories": []}'
+ const result = parseExtractionResponse(response)
+ expect(result.shouldSave).toBe(false)
+ expect(result.memories).toEqual([])
+ })
+
+ it('NEGATIVKONTROLLE: an answer without a think block behaves exactly as before', () => {
+ const result = parseExtractionResponse('{"shouldSave": true, "memories": [{"type": "user", "title": "Role", "content": "Works as a nurse", "tags": []}]}')
+ expect(result.shouldSave).toBe(true)
+ expect(result.memories[0].content).toBe('Works as a nurse')
+ })
+})
diff --git a/src/lib/memory-extraction.ts b/src/lib/memory-extraction.ts
index ce91e2f8..187d7f15 100644
--- a/src/lib/memory-extraction.ts
+++ b/src/lib/memory-extraction.ts
@@ -91,6 +91,14 @@ export function parseExtractionResponse(response: string): ExtractionResult {
// Try to extract JSON from the response
let jsonStr = response.trim()
+ // Reasoning models prepend a think block, and extractJsonObject takes the
+ // FIRST balanced object that parses, so a brace pair inside that block would
+ // be stored as the user's memory. Drop it (an unterminated one means the
+ // token budget died mid-think: nothing to parse). Word for word the web's
+ // apps/web/lib/memory-extraction.ts.
+ jsonStr = jsonStr.replace(/<think>[\s\S]*?<\/think>/gi, '').trim()
+ if (/<think>/i.test(jsonStr)) return fallback
+
// Strip markdown code blocks
const codeBlockMatch = jsonStr.match(/```(?:json)?\s*([\s\S]*?)```/)
if (codeBlockMatch) {