diff --git a/src/mcp/memorySearch.test.ts b/src/mcp/memorySearch.test.ts new file mode 100644 index 00000000..c1120a23 --- /dev/null +++ b/src/mcp/memorySearch.test.ts @@ -0,0 +1,103 @@ +// AGT-4875 — the memory MCP server's search handler caps concurrent searches and +// returns the slot even when a search hits its deadline. + +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { createMemorySearchHandler } from './memorySearch.js'; + +interface Pending { + resolve: (text: string) => void; +} + +function controllableSearch() { + const pending: Pending[] = []; + let inFlight = 0; + let peak = 0; + const search = vi.fn((_cwd: string, query: string) => { + inFlight += 1; + peak = Math.max(peak, inFlight); + return new Promise((resolve) => { + pending.push({ + resolve: (text) => { + inFlight -= 1; + resolve(text || query); + }, + }); + }); + }); + return { search, pending, peak: () => peak }; +} + +afterEach(() => { + vi.useRealTimers(); +}); + +describe('createMemorySearchHandler concurrency', () => { + it('never runs more searches than the limit and rejects the excess as busy', async () => { + const { search, pending, peak } = controllableSearch(); + const handler = createMemorySearchHandler({ search, cwd: () => '/repo', maxConcurrent: 2, timeoutMs: 10_000 }); + + const calls = Array.from({ length: 5 }, (_, i) => handler({ query: `q${i}` })); + await Promise.resolve(); + + expect(search).toHaveBeenCalledTimes(2); + expect(handler.running).toBe(2); + expect(peak()).toBe(2); + + for (const p of pending) p.resolve(''); + const results = await Promise.all(calls); + + expect(results.filter((r) => !r.isError).map((r) => r.content[0].text)).toEqual(['q0', 'q1']); + const busy = results.filter((r) => r.isError); + expect(busy).toHaveLength(3); + expect(busy.every((r) => r.content[0].text.includes('busy'))).toBe(true); + expect(handler.running).toBe(0); + + // Slots are free again: a new call runs. + const again = handler({ query: 'later' }); + await Promise.resolve(); + expect(search).toHaveBeenCalledTimes(3); + pending[2].resolve(''); + expect((await again).isError).toBeUndefined(); + }); + + it('returns the slot when a search hits its deadline', async () => { + vi.useFakeTimers(); + const { search } = controllableSearch(); // never resolved: a hung embedding + const handler = createMemorySearchHandler({ search, cwd: () => '/repo', maxConcurrent: 1, timeoutMs: 1_000 }); + + const hung = handler({ query: 'hung' }); + await Promise.resolve(); + expect(handler.running).toBe(1); + expect((await handler({ query: 'blocked' })).content[0].text).toContain('busy'); + + await vi.advanceTimersByTimeAsync(1_000); + const timedOut = await hung; + expect(timedOut.isError).toBe(true); + expect(timedOut.content[0].text).toContain('timed out after 1000ms'); + expect(handler.running).toBe(0); + + const next = handler({ query: 'next' }); + await Promise.resolve(); + expect(search).toHaveBeenCalledTimes(2); + expect(handler.running).toBe(1); + await vi.advanceTimersByTimeAsync(1_000); + await next; + expect(handler.running).toBe(0); + }); + + it('releases the slot when the search throws, and rejects bad arguments without taking one', async () => { + const search = vi.fn(async () => { + throw new Error('index missing'); + }); + const handler = createMemorySearchHandler({ search, cwd: () => '/repo', maxConcurrent: 1 }); + + const bad = await handler({ query: '' }); + expect(bad.isError).toBe(true); + expect(search).not.toHaveBeenCalled(); + + const failed = await handler({ query: 'x' }); + expect(failed.content[0].text).toContain('index missing'); + expect(handler.running).toBe(0); + expect((await handler({ query: 'y' })).content[0].text).toContain('index missing'); + }); +}); diff --git a/src/mcp/memorySearch.ts b/src/mcp/memorySearch.ts new file mode 100644 index 00000000..f48734e4 --- /dev/null +++ b/src/mcp/memorySearch.ts @@ -0,0 +1,101 @@ +// Created: 2026-11-15 +// Purpose: search_memory request handling for the memory MCP server, with a deadline and a concurrency cap (AGT-4875) +// Dependencies: zod +// Test Status: memorySearch.test.ts + +import { z } from 'zod'; + +export const SearchArgumentsSchema = z.object({ + query: z.string().trim().min(1).max(2_000), + limit: z.number().int().min(1).max(10).default(5), +}).strict(); + +/** Wall-clock ceiling for a single search — hung embeddings must not pin the MCP process. */ +export const SEARCH_TIMEOUT_MS = 60_000; + +/** + * At most this many searches run at once. Each one loads embeddings and scans the + * repo's memory, so a burst of calls from one agent used to fan out without bound. + */ +export const MAX_CONCURRENT_SEARCHES = 2; + +// A type alias (not an interface) so it satisfies the MCP SDK result index signature. +export type MemorySearchResult = { + content: Array<{ type: 'text'; text: string }>; + isError?: true; +}; + +export interface MemorySearchHandlerOptions { + search: (cwd: string, query: string, limit: number) => Promise; + cwd: () => string; + timeoutMs?: number; + maxConcurrent?: number; +} + +/** + * Build the search_memory handler. + * + * Over-limit policy: a call that finds every slot taken is rejected at once with a + * "busy" tool error instead of waiting. Waiting would need its own queue bound and + * timeout, and a CLI agent handles a tool error by retrying or moving on, which is + * the cheaper outcome for a recall hint. + * + * A slot is returned when the call finishes, fails or hits its deadline. After a + * deadline the underlying search cannot be cancelled and may still finish in the + * background; the slot is returned anyway so a hung embedding cannot hold the + * server busy forever (same rule as AGT-3495). + */ +export function createMemorySearchHandler(options: MemorySearchHandlerOptions) { + const timeoutMs = options.timeoutMs ?? SEARCH_TIMEOUT_MS; + const maxConcurrent = Math.max(1, options.maxConcurrent ?? MAX_CONCURRENT_SEARCHES); + let active = 0; + + const handler = async (rawArgs: unknown): Promise => { + let args: z.infer; + try { + args = SearchArgumentsSchema.parse(rawArgs ?? {}); + } catch (err) { + return failure(err); + } + if (active >= maxConcurrent) { + return { + content: [{ + type: 'text', + text: `search_memory busy: ${active}/${maxConcurrent} searches already running — retry shortly`, + }], + isError: true, + }; + } + active += 1; + let deadlineTimer: ReturnType | null = null; + try { + const deadline = new Promise((_, reject) => { + deadlineTimer = setTimeout( + () => reject(new Error(`search_memory timed out after ${timeoutMs}ms`)), + timeoutMs, + ); + }); + const text = await Promise.race([options.search(options.cwd(), args.query, args.limit), deadline]); + return { content: [{ type: 'text', text }] }; + } catch (err) { + return failure(err); + } finally { + if (deadlineTimer) clearTimeout(deadlineTimer); + active -= 1; + } + }; + + // A getter, not Object.assign: assign would copy the value once. + return Object.defineProperty(handler, 'running', { + /** Searches currently holding a slot. */ + get: () => active, + enumerable: true, + }) as typeof handler & { readonly running: number }; +} + +function failure(err: unknown): MemorySearchResult { + return { + content: [{ type: 'text', text: `search_memory failed: ${err instanceof Error ? err.message : String(err)}` }], + isError: true, + }; +} diff --git a/src/mcp/memoryServer.ts b/src/mcp/memoryServer.ts index 1eeceaf2..bad91a3b 100644 --- a/src/mcp/memoryServer.ts +++ b/src/mcp/memoryServer.ts @@ -16,15 +16,7 @@ import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { ListToolsRequestSchema, CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js'; import { searchRepoMemoryText } from '../memory/repoKnowledge.js'; -import { z } from 'zod'; - -const SearchArgumentsSchema = z.object({ - query: z.string().trim().min(1).max(2_000), - limit: z.number().int().min(1).max(10).default(5), -}).strict(); - -/** Wall-clock ceiling for a single search — hung embeddings must not pin the MCP process. */ -const SEARCH_TIMEOUT_MS = 60_000; +import { createMemorySearchHandler } from './memorySearch.js'; // MCP stdio reserves stdout for protocol frames. Keep all console.log output on // stderr for this process lifetime instead of patching/restoring it per request. @@ -52,37 +44,16 @@ async function main(): Promise { { capabilities: { tools: {} } }, ); + // Deadline and concurrency cap live in memorySearch.ts (AGT-4875). + const searchMemory = createMemorySearchHandler({ search: searchRepoMemoryText, cwd: () => process.cwd() }); + server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [SEARCH_TOOL] })); server.setRequestHandler(CallToolRequestSchema, async (req) => { if (req.params.name !== 'search_memory') { return { content: [{ type: 'text', text: `Unknown tool: ${req.params.name}` }], isError: true }; } - let deadlineTimer: ReturnType | null = null; - try { - const args = SearchArgumentsSchema.parse(req.params.arguments ?? {}); - const controller = new AbortController(); - deadlineTimer = setTimeout( - () => controller.abort(new Error(`search_memory timed out after ${SEARCH_TIMEOUT_MS}ms`)), - SEARCH_TIMEOUT_MS, - ); - const text = await Promise.race([ - searchRepoMemoryText(process.cwd(), args.query, args.limit), - new Promise((_, reject) => { - const onAbort = () => reject(controller.signal.reason ?? new Error('search_memory aborted')); - if (controller.signal.aborted) onAbort(); - else controller.signal.addEventListener('abort', onAbort, { once: true }); - }), - ]); - return { content: [{ type: 'text', text }] }; - } catch (err) { - return { - content: [{ type: 'text', text: `search_memory failed: ${err instanceof Error ? err.message : String(err)}` }], - isError: true, - }; - } finally { - if (deadlineTimer) clearTimeout(deadlineTimer); - } + return searchMemory(req.params.arguments); }); await server.connect(new StdioServerTransport());