forked from QwenLM/qwen-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecall.test.ts
More file actions
198 lines (178 loc) · 6.29 KB
/
Copy pathrecall.test.ts
File metadata and controls
198 lines (178 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
buildRelevantAutoMemoryPrompt,
resolveRelevantAutoMemoryPromptForQuery,
selectRelevantAutoMemoryDocuments,
} from './recall.js';
import type { ScannedAutoMemoryDocument } from './scan.js';
import type { Config } from '../config/config.js';
import { scanAutoMemoryTopicDocuments } from './scan.js';
import { selectRelevantAutoMemoryDocumentsByModel } from './relevanceSelector.js';
vi.mock('./scan.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('./scan.js')>();
return {
...actual,
scanAutoMemoryTopicDocuments: vi.fn(),
// Explicit mock — recall now unions user-level docs into the pool, so
// leaving this on the real implementation would silently fall through
// to the filesystem (only "works" because the path doesn't exist and
// listMarkdownFiles swallows ENOENT). Defaults to an empty pool.
scanUserAutoMemoryTopicDocuments: vi.fn().mockResolvedValue([]),
};
});
vi.mock('./relevanceSelector.js', () => ({
selectRelevantAutoMemoryDocumentsByModel: vi.fn(),
}));
const docs: ScannedAutoMemoryDocument[] = [
{
type: 'reference',
filePath: '/tmp/reference.md',
relativePath: 'reference.md',
filename: 'reference.md',
title: 'Reference Memory',
description: 'Dashboards and external docs',
body: '# Reference Memory\n\n- Grafana dashboard: grafana.internal/d/api-latency',
mtimeMs: 3,
},
{
type: 'project',
filePath: '/tmp/project.md',
relativePath: 'project.md',
filename: 'project.md',
title: 'Project Memory',
description: 'Project constraints and release context',
body: '# Project Memory\n\n- Release freeze starts Friday.',
mtimeMs: 2,
},
{
type: 'user',
filePath: '/tmp/user.md',
relativePath: 'user.md',
filename: 'user.md',
title: 'User Memory',
description: 'User preferences',
body: '# User Memory\n\n- User prefers terse responses.',
mtimeMs: 1,
},
];
const activeToolDocs: ScannedAutoMemoryDocument[] = [
{
type: 'reference',
filePath: '/tmp/ata-tool.md',
relativePath: 'ata-tool.md',
filename: 'ata-tool.md',
title: 'ATA tool schema notes',
description:
'article-list-query parameter schema and failed tool-call attempts',
body: '# ATA tool schema notes\n\n- ata::article-list-query failed with guessed field mappings.',
mtimeMs: 4,
},
{
type: 'reference',
filePath: '/tmp/ata-gotcha.md',
relativePath: 'ata-gotcha.md',
filename: 'ata-gotcha.md',
title: 'ATA tool gotcha',
description: 'article-list-query known workaround for transient failures',
body: '# ATA tool gotcha\n\n- mcp__ata__article-list-query can return systemError during index rotation; retry after checking the ATA oncall note.',
mtimeMs: 6,
},
{
type: 'reference',
filePath: '/tmp/ata-owner.md',
relativePath: 'ata-owner.md',
filename: 'ata-owner.md',
title: 'ATA escalation',
description: 'ATA service owner and escalation path',
body: '# ATA escalation\n\n- Ask the ATA oncall when the service returns systemError.',
mtimeMs: 5,
},
];
describe('auto-memory relevant recall', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('selects the most relevant documents for a query', () => {
const selected = selectRelevantAutoMemoryDocuments(
'check the dashboard reference for latency',
docs,
);
expect(selected[0]?.type).toBe('reference');
expect(selected.map((doc) => doc.type)).toContain('reference');
});
it('returns an empty list for an empty query', () => {
expect(selectRelevantAutoMemoryDocuments(' ', docs)).toEqual([]);
});
it('formats selected documents as a prompt block', () => {
const prompt = buildRelevantAutoMemoryPrompt([docs[0], docs[2]]);
expect(prompt).toContain('## Relevant memory');
expect(prompt).toContain('Reference Memory (reference.md)');
expect(prompt).toContain('User Memory (user.md)');
});
it('uses model-driven selection when config is provided', async () => {
vi.mocked(scanAutoMemoryTopicDocuments).mockResolvedValue(docs);
vi.mocked(selectRelevantAutoMemoryDocumentsByModel).mockResolvedValue([
docs[0],
]);
const result = await resolveRelevantAutoMemoryPromptForQuery(
'/tmp/project',
'check the dashboard reference for latency',
{
config: {} as Config,
},
);
expect(result.strategy).toBe('model');
expect(result.selectedDocs).toEqual([docs[0]]);
expect(result.prompt).toContain('Reference Memory (reference.md)');
});
it('falls back to heuristic selection when model-driven selection fails', async () => {
vi.mocked(scanAutoMemoryTopicDocuments).mockResolvedValue(docs);
vi.mocked(selectRelevantAutoMemoryDocumentsByModel).mockRejectedValue(
new Error('selector failed'),
);
const result = await resolveRelevantAutoMemoryPromptForQuery(
'/tmp/project',
'check the dashboard reference for latency',
{
config: {} as Config,
excludedFilePaths: ['/tmp/user.md'],
},
);
expect(result.strategy).toBe('heuristic');
expect(result.selectedDocs.map((doc) => doc.filePath)).toContain(
'/tmp/reference.md',
);
expect(result.selectedDocs.map((doc) => doc.filePath)).not.toContain(
'/tmp/user.md',
);
});
it('keeps active tool schemas out of heuristic fallback', async () => {
vi.mocked(scanAutoMemoryTopicDocuments).mockResolvedValue(activeToolDocs);
vi.mocked(selectRelevantAutoMemoryDocumentsByModel).mockRejectedValue(
new Error('selector failed'),
);
const result = await resolveRelevantAutoMemoryPromptForQuery(
'/tmp/project',
'read the ATA article with article-list-query',
{
config: {} as Config,
recentTools: ['mcp__ata__article-list-query'],
},
);
expect(result.strategy).toBe('heuristic');
expect(result.selectedDocs.map((doc) => doc.filePath)).not.toContain(
'/tmp/ata-tool.md',
);
expect(result.selectedDocs.map((doc) => doc.filePath)).toContain(
'/tmp/ata-gotcha.md',
);
expect(result.selectedDocs.map((doc) => doc.filePath)).toContain(
'/tmp/ata-owner.md',
);
});
});