@@ -45,6 +45,7 @@ type MemorySearchToolResult =
4545 | MemoryCorpusSearchResult ;
4646
4747const MEMORY_SEARCH_TOOL_TIMEOUT_MS = 15_000 ;
48+ const MEMORY_SEARCH_TOOL_QMD_TIMEOUT_OVERHEAD_MS = 5_000 ;
4849const MEMORY_SEARCH_TOOL_COOLDOWN_MS = 60_000 ;
4950
5051const memorySearchToolCooldowns = new Map < string , { until : number ; error : string } > ( ) ;
@@ -81,6 +82,24 @@ export const testing = {
8182 } ,
8283} as const ;
8384
85+ function resolveMemorySearchToolTimeoutMs ( params : {
86+ backend : string | undefined ;
87+ qmdTimeoutMs : number | undefined ;
88+ } ) : number {
89+ if (
90+ params . backend !== "qmd" ||
91+ typeof params . qmdTimeoutMs !== "number" ||
92+ ! Number . isFinite ( params . qmdTimeoutMs ) ||
93+ params . qmdTimeoutMs <= 0
94+ ) {
95+ return MEMORY_SEARCH_TOOL_TIMEOUT_MS ;
96+ }
97+ return Math . max (
98+ MEMORY_SEARCH_TOOL_TIMEOUT_MS ,
99+ Math . floor ( params . qmdTimeoutMs ) + MEMORY_SEARCH_TOOL_QMD_TIMEOUT_OVERHEAD_MS ,
100+ ) ;
101+ }
102+
84103async function runMemorySearchToolWithDeadline < T > ( params : {
85104 timeoutMs : number ;
86105 run : ( ) => Promise < T > ;
@@ -345,6 +364,8 @@ export function createMemorySearchTool(options: {
345364 execute :
346365 ( { cfg, agentId } ) =>
347366 async ( _toolCallId , params ) => {
367+ const { resolveMemoryBackendConfig } = await loadMemoryToolRuntime ( ) ;
368+ const resolvedMemoryBackend = resolveMemoryBackendConfig ( { cfg, agentId } ) ;
348369 const rawParams = asToolParamsRecord ( params ) ;
349370 const query = readStringParam ( rawParams , "query" , { required : true } ) ;
350371 const maxResults = readPositiveIntegerParam ( rawParams , "maxResults" ) ;
@@ -381,9 +402,11 @@ export function createMemorySearchTool(options: {
381402 } ;
382403
383404 const outcome = await runMemorySearchToolWithDeadline ( {
384- timeoutMs : MEMORY_SEARCH_TOOL_TIMEOUT_MS ,
405+ timeoutMs : resolveMemorySearchToolTimeoutMs ( {
406+ backend : resolvedMemoryBackend . backend ,
407+ qmdTimeoutMs : resolvedMemoryBackend . qmd ?. limits ?. timeoutMs ,
408+ } ) ,
385409 run : async ( ) => {
386- const { resolveMemoryBackendConfig } = await loadMemoryToolRuntime ( ) ;
387410 const shouldQuerySupplements = requestedCorpus === "wiki" || requestedCorpus === "all" ;
388411 const shouldQueryMemory = requestedCorpus !== "wiki" && ! cooldown ;
389412 if ( cooldown && ! shouldQuerySupplements ) {
@@ -502,10 +525,12 @@ export function createMemorySearchTool(options: {
502525 }
503526 const status = activeMemory . manager . status ( ) ;
504527 const decorated = decorateCitations ( rawResults , includeCitations ) ;
505- const resolved = resolveMemoryBackendConfig ( { cfg, agentId } ) ;
506528 const memoryResults =
507529 status . backend === "qmd"
508- ? clampResultsByInjectedChars ( decorated , resolved . qmd ?. limits . maxInjectedChars )
530+ ? clampResultsByInjectedChars (
531+ decorated ,
532+ resolvedMemoryBackend . qmd ?. limits ?. maxInjectedChars ,
533+ )
509534 : decorated ;
510535 surfacedMemoryResults = memoryResults . map ( ( result ) => ( {
511536 ...result ,
0 commit comments