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:
109
packages/server/tests/local/chat-recall-step.test.ts
Normal file
109
packages/server/tests/local/chat-recall-step.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user