From 4c810cb24987d5deb06e722fbe7064ec31016f34 Mon Sep 17 00:00:00 2001 From: Studio Date: Tue, 1 Dec 2026 06:11:53 -0500 Subject: [PATCH] =?UTF-8?q?T-134:=20M2.1d=20=E2=80=94=20MCP=20copilot=20`s?= =?UTF-8?q?earch=5Fsymbols`=20tool:=20vault-safe=20security-master=20symbo?= =?UTF-8?q?l=20lookup=20via=20an=20injected=20`SymbolSearchPort`=20in=20`l?= =?UTF-8?q?ibs/research/src/mcp/tools/`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/research/src/mcp/tools/search-symbols.ts | 9 ++++-- libs/research/test/mcp-search-symbols.test.ts | 30 +++++++++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/libs/research/src/mcp/tools/search-symbols.ts b/libs/research/src/mcp/tools/search-symbols.ts index 9324ee16..a5bd5882 100644 --- a/libs/research/src/mcp/tools/search-symbols.ts +++ b/libs/research/src/mcp/tools/search-symbols.ts @@ -242,7 +242,7 @@ function fenceAndProjectRow( }; } -function matchRank(row: SymbolSearchRow, normalizedQuery: string): number { +function matchRank(row: SymbolSearchRow, normalizedQuery: string): number | null { const normalizedTicker = row.ticker.toLowerCase(); if (normalizedTicker === normalizedQuery) { return 0; @@ -253,7 +253,7 @@ function matchRank(row: SymbolSearchRow, normalizedQuery: string): number { if (row.name.toLowerCase().includes(normalizedQuery)) { return 2; } - return 3; + return null; } function validateFactoryOptions(options: SearchSymbolsToolOptions): number { @@ -322,7 +322,10 @@ export function createSearchSymbolsTool(options: SearchSymbolsToolOptions): McpT input.includeDelisted ); if (fenced !== null) { - eligible.push({ row: fenced, rank: matchRank(fenced, normalizedQuery) }); + const rank = matchRank(fenced, normalizedQuery); + if (rank !== null) { + eligible.push({ row: fenced, rank }); + } } } diff --git a/libs/research/test/mcp-search-symbols.test.ts b/libs/research/test/mcp-search-symbols.test.ts index 2e8e8b15..1db83564 100644 --- a/libs/research/test/mcp-search-symbols.test.ts +++ b/libs/research/test/mcp-search-symbols.test.ts @@ -337,11 +337,35 @@ describe('MCP search_symbols tool', () => { expect(resB.isError).toBe(false); expect(resA.payload).toEqual(resB.payload); - // Exact ticker (2, 10) -> prefix (5, 25) -> name substring (15; note 100 has 'Apple' not 'aapl') -> other (30, 100) - expect(resA.payload?.rows.map(r => r.listingId)).toEqual([2, 10, 5, 25, 15, 30, 100]); - expect(resA.payload?.rowCount).toBe(7); + // Exact ticker (2, 10) -> prefix (5, 25) -> name substring (15); unrelated candidates (30, 100) are excluded + expect(resA.payload?.rows.map(r => r.listingId)).toEqual([2, 10, 5, 25, 15]); + expect(resA.payload?.rowCount).toBe(5); expect(resA.payload?.truncated).toBe(false); + // Unrelated candidates must not trigger truncation when all actual matches fit within limit. + const exactFitRes = await callSearchSymbols(createFakePort({ rows: fixture }), { + query: 'AAPL', + limit: 5, + }); + expect(exactFitRes.isError).toBe(false); + expect(exactFitRes.payload?.rows.map(r => r.listingId)).toEqual([2, 10, 5, 25, 15]); + expect(exactFitRes.payload?.rowCount).toBe(5); + expect(exactFitRes.payload?.truncated).toBe(false); + + // When none of the port's returned candidates match, return an empty, non-truncated payload. + const noMatchRes = await callSearchSymbols(createFakePort({ rows: fixture }), { + query: 'UNMATCHED', + limit: 2, + }); + expect(noMatchRes.isError).toBe(false); + expect(noMatchRes.payload).toEqual({ + scope: 'vault_fenced', + vaultCutoffTs: VAULT_CUTOFF_TS, + rowCount: 0, + truncated: false, + rows: [], + }); + // Verify trimmed query, default limit, includeDelisted: true, and vaultCutoffTs were passed to port. expect(fakeA.calls).toHaveLength(1); expect(fakeA.calls[0]).toEqual({