From b3b61e14c9b10e5a84ceb39a23c4456eb2e493da Mon Sep 17 00:00:00 2001 From: "shumeiko.it" Date: Tue, 27 Oct 2026 12:54:51 +0100 Subject: [PATCH] fix(server): close the memory recall step when nothing is found or recall fails (UF-051) Only a successful recall reported its outcome, so an empty or failed recall left "Recalling relevant memories..." as the last step after the answer. Both cases now send a closing step and the turn goes on. --- docs/oss/upstream-fixes.md | 10 ++ packages/server/src/local/routes/chat.ts | 7 +- .../tests/local/chat-recall-step.test.ts | 109 ++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 packages/server/tests/local/chat-recall-step.test.ts diff --git a/docs/oss/upstream-fixes.md b/docs/oss/upstream-fixes.md index c9a9756..c6f9634 100644 --- a/docs/oss/upstream-fixes.md +++ b/docs/oss/upstream-fixes.md @@ -79,6 +79,7 @@ Baseline: commit `b20b138` of `git.os-dev.tech/oleg.m/waggle-os`. | UF-048 | The sidebar account row sits off-center at the bottom | Fixed | Web UI | | UF-049 | The workspace title stretches when switching tabs | Fixed | Web UI | | UF-050 | The chat activity card always claims memory, web and files | Fixed | Web UI, chat | +| UF-051 | The memory recall step looks unfinished after the answer | Fixed | Sidecar, chat, memory | ## Entries @@ -505,3 +506,12 @@ Entries UF-010 to UF-019 come from the UI audit `anti-slop/audit-001-2026-10-14. - Cause: the headline is a fixed string; only the step count beside it comes from the turn. - What we did: the headline is built from the turn itself: memory when a memory tool ran or the server reported recall or saving, the web for search, fetch and browser tools, files for file tools. A turn that touched none of them reads "Working" or "Done". Tests cover a plain turn and a web-and-files turn. - Recommendation: apply the same change in the main project. + +### UF-051. The memory recall step looks unfinished after the answer +- Status: Fixed +- Found: 2026-10-26 +- Where: at commit `b20b138`, `packages/server/src/local/routes/chat.ts:1923` (the "Recalling relevant memories..." step), `:1962` (no memories found) and the empty `catch` after it +- Symptom: when memory has nothing relevant, or the recall fails, the last line of the activity card is "Recalling relevant memories...", so the search looks like it is still running after the answer has arrived. +- Cause: only the branch that finds memories sends a closing step ("Recalled N relevant memories."). The empty branch sends just a tool result, and a failed recall sends nothing. +- What we did: the empty branch sends "No relevant memories found." and a failed recall sends "Could not search memory. Continuing without it." with an error tool result, and the turn goes on. A server test covers both cases and fails without the change. +- Recommendation: apply the same change in the main project. diff --git a/packages/server/src/local/routes/chat.ts b/packages/server/src/local/routes/chat.ts index 1ad6ff6..c105bd9 100644 --- a/packages/server/src/local/routes/chat.ts +++ b/packages/server/src/local/routes/chat.ts @@ -1892,11 +1892,16 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo }); sendEvent('tool_result', { name: 'auto_recall', result: resultText, duration: recallDuration, isError: false }); } else { + // UF-051: close the "Recalling…" step with its outcome, or it + // reads as still running after the answer lands. + sendEvent('step', { content: 'No relevant memories found.' }); sendEvent('tool_result', { name: 'auto_recall', result: 'No relevant memories found', duration: recallDuration, isError: false }); } } catch { throwIfTurnAborted(); - // Non-blocking — if recall fails, continue without it + // Non-blocking — if recall fails, continue without it, and say so. + sendEvent('step', { content: 'Could not search memory. Continuing without it.' }); + sendEvent('tool_result', { name: 'auto_recall', result: 'Memory search failed', isError: true }); } } diff --git a/packages/server/tests/local/chat-recall-step.test.ts b/packages/server/tests/local/chat-recall-step.test.ts new file mode 100644 index 0000000..427630e --- /dev/null +++ b/packages/server/tests/local/chat-recall-step.test.ts @@ -0,0 +1,109 @@ +/** + * UF-051: the automatic memory recall opens a "Recalling relevant memories..." + * step. It must always be followed by a step that reports the outcome, or the + * chat shows it as still in progress after the answer has arrived. + */ +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import type { FastifyInstance } from 'fastify'; +import { Orchestrator, type AgentLoopConfig, type AgentResponse } from '@waggle/agent'; +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; +import { buildLocalServer } from '../../src/local/index.js'; +import { closeAuditDb } from '../../src/local/routes/events.js'; +import { injectWithAuth, resetRateLimiter } from '../test-utils.js'; + +const testState = vi.hoisted(() => ({ runAgentLoop: vi.fn() })); + +vi.mock('@waggle/agent', async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, runAgentLoop: testState.runAgentLoop }; +}); + +vi.mock('../../src/local/model-availability.js', async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + resolveUsableModel: async (_server: FastifyInstance, requestedModel: string) => requestedModel, + }; +}); + +function parseSse(raw: string): Array<{ event: string; data: Record }> { + return raw.split(/\n\n/) + .filter(Boolean) + .map((block) => { + const lines = block.split('\n'); + const event = lines.find(line => line.startsWith('event: '))?.slice(7) ?? ''; + const data = lines.find(line => line.startsWith('data: '))?.slice(6) ?? '{}'; + return { event, data: JSON.parse(data) as Record }; + }); +} + +function stepsAfterRecall(body: string): string[] { + const steps = parseSse(body) + .filter(e => e.event === 'step') + .map(e => String(e.data.content)); + const start = steps.indexOf('Recalling relevant memories...'); + expect(start).toBeGreaterThanOrEqual(0); + return steps.slice(start + 1); +} + +describe('automatic recall step (UF-051)', () => { + let server: FastifyInstance; + let tmpDir: string; + + beforeAll(async () => { + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-recall-step-')); + testState.runAgentLoop.mockImplementation(async (config: AgentLoopConfig): Promise => { + config.onToken?.('Here is the answer.'); + return { content: 'Here is the answer.', toolsUsed: [], usage: { inputTokens: 10, outputTokens: 5 } }; + }); + server = await buildLocalServer({ dataDir: tmpDir }); + server.agentState.llmProvider = { + provider: 'litellm', + health: 'healthy', + detail: 'non-paid test double', + checkedAt: new Date().toISOString(), + }; + }); + + beforeEach(() => { + resetRateLimiter(server); + }); + + afterAll(async () => { + await server.close(); + closeAuditDb(); + await new Promise(resolve => setTimeout(resolve, 100)); + fs.rmSync(tmpDir, { recursive: true, force: true }); + }); + + async function ask(session: string) { + const response = await injectWithAuth(server, { + method: 'POST', + url: '/api/chat', + payload: { + message: 'What did we decide about the launch date?', + model: 'openrouter/anthropic/claude-sonnet-5', + session, + workspace: 'default', + }, + }); + expect(response.statusCode).toBe(200); + return response.body; + } + + it('reports that nothing was found when memory is empty', async () => { + const body = await ask('recall-empty'); + expect(stepsAfterRecall(body)[0]).toBe('No relevant memories found.'); + }); + + it('reports a failed recall and lets the turn finish', async () => { + const recall = vi.spyOn(Orchestrator.prototype, 'recallMemory').mockRejectedValue(new Error('index unavailable')); + const body = await ask('recall-failed').finally(() => recall.mockRestore()); + expect(stepsAfterRecall(body)[0]).toBe('Could not search memory. Continuing without it.'); + const recallResult = parseSse(body).find(e => e.event === 'tool_result' && e.data.name === 'auto_recall'); + expect(recallResult?.data.isError).toBe(true); + expect(parseSse(body).some(e => e.event === 'done')).toBe(true); + }); +});