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

Function startRelevantMemoryPrefetch

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

Source from the content-addressed store, hash-verified

2417 * handle with settlement tracking. Bound with `using` in query.ts.
2418 */
2419export function startRelevantMemoryPrefetch(
2420 messages: ReadonlyArray<Message>,
2421 toolUseContext: ToolUseContext,
2422): MemoryPrefetch | undefined {
2423 if (
2424 !isAutoMemoryEnabled() ||
2425 !getFeatureValue_CACHED_MAY_BE_STALE('tengu_moth_copse', false)
2426 ) {
2427 return undefined
2428 }
2429
2430 // Poor mode: skip the side-query to save tokens
2431 const { isPoorModeActive } =
2432 require('../commands/poor/poorMode.js') as typeof import('../commands/poor/poorMode.js')
2433 if (isPoorModeActive()) {
2434 return undefined
2435 }
2436
2437 const lastUserMessage = messages.findLast(m => m.type === 'user' && !m.isMeta)
2438 if (!lastUserMessage) {
2439 return undefined
2440 }
2441
2442 const input = getUserMessageText(lastUserMessage)
2443 // Single-word prompts lack enough context for meaningful term extraction
2444 if (!input || !/\s/.test(input.trim())) {
2445 return undefined
2446 }
2447
2448 const surfaced = collectSurfacedMemories(messages)
2449 if (surfaced.totalBytes >= RELEVANT_MEMORIES_CONFIG.MAX_SESSION_BYTES) {
2450 return undefined
2451 }
2452
2453 // Chained to the turn-level abort so user Escape cancels the sideQuery
2454 // immediately, not just on [Symbol.dispose] when queryLoop exits.
2455 const controller = createChildAbortController(toolUseContext.abortController)
2456 const firedAt = Date.now()
2457 const promise = getRelevantMemoryAttachments(
2458 input,
2459 toolUseContext.options.agentDefinitions.activeAgents,
2460 toolUseContext.readFileState,
2461 collectRecentSuccessfulTools(messages, lastUserMessage),
2462 controller.signal,
2463 surfaced.paths,
2464 toolUseContext.langfuseTrace,
2465 ).catch(e => {
2466 if (!isAbortError(e)) {
2467 logError(e)
2468 }
2469 return []
2470 })
2471
2472 const handle: MemoryPrefetch = {
2473 promise,
2474 settledAt: null,
2475 consumedOnIteration: -1,
2476 [Symbol.dispose]() {

Callers 1

queryLoopFunction · 0.85

Calls 11

isAutoMemoryEnabledFunction · 0.85
isPoorModeActiveFunction · 0.85
getUserMessageTextFunction · 0.85
collectSurfacedMemoriesFunction · 0.85
isAbortErrorFunction · 0.85
nowMethod · 0.80
logErrorFunction · 0.70

Tested by

no test coverage detected