MCPcopy Index your code
hub / github.com/Noumena-Network/code / startRelevantMemoryPrefetch

Function startRelevantMemoryPrefetch

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

Source from the content-addressed store, hash-verified

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

Callers 1

queryLoopFunction · 0.85

Calls 9

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

Tested by

no test coverage detected