( query: string, memories: MemoryHeader[], signal: AbortSignal, recentTools: readonly string[], )
| 75 | } |
| 76 | |
| 77 | async function selectRelevantMemories( |
| 78 | query: string, |
| 79 | memories: MemoryHeader[], |
| 80 | signal: AbortSignal, |
| 81 | recentTools: readonly string[], |
| 82 | ): Promise<string[]> { |
| 83 | const validFilenames = new Set(memories.map(m => m.filename)) |
| 84 | |
| 85 | const manifest = formatMemoryManifest(memories) |
| 86 | |
| 87 | // When Claude Code is actively using a tool (e.g. mcp__X__spawn), |
| 88 | // surfacing that tool's reference docs is noise — the conversation |
| 89 | // already contains working usage. The selector otherwise matches |
| 90 | // on keyword overlap ("spawn" in query + "spawn" in a memory |
| 91 | // description → false positive). |
| 92 | const toolsSection = |
| 93 | recentTools.length > 0 |
| 94 | ? `\n\nRecently used tools: ${recentTools.join(', ')}` |
| 95 | : '' |
| 96 | |
| 97 | try { |
| 98 | const result = await sideQuery({ |
| 99 | model: getDefaultSonnetModel(), |
| 100 | system: SELECT_MEMORIES_SYSTEM_PROMPT, |
| 101 | skipSystemPromptPrefix: true, |
| 102 | messages: [ |
| 103 | { |
| 104 | role: 'user', |
| 105 | content: `Query: ${query}\n\nAvailable memories:\n${manifest}${toolsSection}`, |
| 106 | }, |
| 107 | ], |
| 108 | max_tokens: 256, |
| 109 | output_format: { |
| 110 | type: 'json_schema', |
| 111 | schema: { |
| 112 | type: 'object', |
| 113 | properties: { |
| 114 | selected_memories: { type: 'array', items: { type: 'string' } }, |
| 115 | }, |
| 116 | required: ['selected_memories'], |
| 117 | additionalProperties: false, |
| 118 | }, |
| 119 | }, |
| 120 | signal, |
| 121 | querySource: 'memdir_relevance', |
| 122 | }) |
| 123 | |
| 124 | const textBlock = result.content.find(block => block.type === 'text') |
| 125 | if (!textBlock || textBlock.type !== 'text') { |
| 126 | return [] |
| 127 | } |
| 128 | |
| 129 | const parsed: { selected_memories: string[] } = jsonParse(textBlock.text) |
| 130 | return parsed.selected_memories.filter(f => validFilenames.has(f)) |
| 131 | } catch (e) { |
| 132 | if (signal.aborted) { |
| 133 | return [] |
| 134 | } |
no test coverage detected