MCPcopy Create free account
hub / github.com/Noumena-Network/code / cachedMicrocompactPath

Function cachedMicrocompactPath

src/services/compact/microCompact.ts:315–409  ·  view source on GitHub ↗

* Cached microcompact path - uses cache editing API to remove tool results * without invalidating the cached prefix. * * Key differences from regular microcompact: * - Does NOT modify local message content (cache_reference and cache_edits are added at API layer) * - Uses count-based trigger/kee

(
  messages: Message[],
  querySource: QuerySource | undefined,
)

Source from the content-addressed store, hash-verified

313 * - Tracks tool results and queues cache edits for the API layer
314 */
315async function cachedMicrocompactPath(
316 messages: Message[],
317 querySource: QuerySource | undefined,
318): Promise<MicrocompactResult> {
319 const mod = await getCachedMCModule()
320 const state = ensureCachedMCState()
321 const config = mod.getCachedMCConfig()
322
323 const compactableToolIds = new Set(collectCompactableToolIds(messages))
324 // Second pass: register tool results grouped by user message
325 for (const message of messages) {
326 if (message.type === 'user' && Array.isArray(message.message.content)) {
327 const groupIds: string[] = []
328 for (const block of message.message.content) {
329 if (
330 block.type === 'tool_result' &&
331 compactableToolIds.has(block.tool_use_id) &&
332 !state.registeredTools.has(block.tool_use_id)
333 ) {
334 mod.registerToolResult(state, block.tool_use_id)
335 groupIds.push(block.tool_use_id)
336 }
337 }
338 mod.registerToolMessage(state, groupIds)
339 }
340 }
341
342 const toolsToDelete = mod.getToolResultsToDelete(state)
343
344 if (toolsToDelete.length > 0) {
345 // Create and queue the cache_edits block for the API layer
346 const cacheEdits = mod.createCacheEditsBlock(state, toolsToDelete)
347 if (cacheEdits) {
348 pendingCacheEdits = cacheEdits
349 }
350
351 logForDebugging(
352 `Cached MC deleting ${toolsToDelete.length} tool(s): ${toolsToDelete.join(', ')}`,
353 )
354
355 // Log the event
356 logEvent('ncode_cached_microcompact', {
357 toolsDeleted: toolsToDelete.length,
358 deletedToolIds: toolsToDelete.join(
359 ',',
360 ) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
361 activeToolCount: state.toolOrder.length - state.deletedRefs.size,
362 triggerType:
363 'auto' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
364 threshold: config.triggerThreshold,
365 keepRecent: config.keepRecent,
366 })
367
368 // Suppress warning after successful compaction
369 suppressCompactWarning()
370
371 // Notify cache break detection that cache reads will legitimately drop
372 if (feature('PROMPT_CACHE_BREAK_DETECTION')) {

Callers 1

microcompactMessagesFunction · 0.85

Calls 8

getCachedMCModuleFunction · 0.85
ensureCachedMCStateFunction · 0.85
suppressCompactWarningFunction · 0.85
notifyCacheDeletionFunction · 0.85
logForDebuggingFunction · 0.50
logEventFunction · 0.50
hasMethod · 0.45

Tested by

no test coverage detected