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.
This commit is contained in:
shumeiko.it
2026-10-27 12:54:51 +01:00
parent 06d267dec8
commit b3b61e14c9
3 changed files with 125 additions and 1 deletions

View File

@@ -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 });
}
}

View File

@@ -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<typeof import('@waggle/agent')>();
return { ...actual, runAgentLoop: testState.runAgentLoop };
});
vi.mock('../../src/local/model-availability.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../src/local/model-availability.js')>();
return {
...actual,
resolveUsableModel: async (_server: FastifyInstance, requestedModel: string) => requestedModel,
};
});
function parseSse(raw: string): Array<{ event: string; data: Record<string, unknown> }> {
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<string, unknown> };
});
}
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<AgentResponse> => {
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);
});
});