MCPcopy
hub / github.com/codeaashu/claude-code / startRelevantMemoryPrefetch

Function startRelevantMemoryPrefetch

src/utils/attachments.ts:2361–2424  ·  view source on GitHub ↗
(
  messages: ReadonlyArray<Message>,
  toolUseContext: ToolUseContext,
)

Source from the content-addressed store, hash-verified

2359 * handle with settlement tracking. Bound with `using` in query.ts.
2360 */
2361export function startRelevantMemoryPrefetch(
2362 messages: ReadonlyArray<Message>,
2363 toolUseContext: ToolUseContext,
2364): MemoryPrefetch | undefined {
2365 if (
2366 !isAutoMemoryEnabled() ||
2367 !getFeatureValue_CACHED_MAY_BE_STALE('tengu_moth_copse', false)
2368 ) {
2369 return undefined
2370 }
2371
2372 const lastUserMessage = messages.findLast(m => m.type === 'user' && !m.isMeta)
2373 if (!lastUserMessage) {
2374 return undefined
2375 }
2376
2377 const input = getUserMessageText(lastUserMessage)
2378 // Single-word prompts lack enough context for meaningful term extraction
2379 if (!input || !/\s/.test(input.trim())) {
2380 return undefined
2381 }
2382
2383 const surfaced = collectSurfacedMemories(messages)
2384 if (surfaced.totalBytes >= RELEVANT_MEMORIES_CONFIG.MAX_SESSION_BYTES) {
2385 return undefined
2386 }
2387
2388 // Chained to the turn-level abort so user Escape cancels the sideQuery
2389 // immediately, not just on [Symbol.dispose] when queryLoop exits.
2390 const controller = createChildAbortController(toolUseContext.abortController)
2391 const firedAt = Date.now()
2392 const promise = getRelevantMemoryAttachments(
2393 input,
2394 toolUseContext.options.agentDefinitions.activeAgents,
2395 toolUseContext.readFileState,
2396 collectRecentSuccessfulTools(messages, lastUserMessage),
2397 controller.signal,
2398 surfaced.paths,
2399 ).catch(e => {
2400 if (!isAbortError(e)) {
2401 logError(e)
2402 }
2403 return []
2404 })
2405
2406 const handle: MemoryPrefetch = {
2407 promise,
2408 settledAt: null,
2409 consumedOnIteration: -1,
2410 [Symbol.dispose]() {
2411 controller.abort()
2412 logEvent('tengu_memdir_prefetch_collected', {
2413 hidden_by_first_iteration:
2414 handle.settledAt !== null && handle.consumedOnIteration === 0,
2415 consumed_on_iteration: handle.consumedOnIteration,
2416 latency_ms: (handle.settledAt ?? Date.now()) - firedAt,
2417 })
2418 },

Callers 1

queryLoopFunction · 0.85

Calls 9

isAutoMemoryEnabledFunction · 0.85
getUserMessageTextFunction · 0.85
collectSurfacedMemoriesFunction · 0.85
isAbortErrorFunction · 0.70
logErrorFunction · 0.70

Tested by

no test coverage detected