MCPcopy Index your code
hub / github.com/codeaashu/claude-code / cachedMicrocompactPath

Function cachedMicrocompactPath

src/services/compact/microCompact.ts:305–399  ·  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

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

Callers 1

microcompactMessagesFunction · 0.85

Calls 10

getCachedMCModuleFunction · 0.85
ensureCachedMCStateFunction · 0.85
logForDebuggingFunction · 0.85
logEventFunction · 0.85
suppressCompactWarningFunction · 0.85
featureFunction · 0.85
notifyCacheDeletionFunction · 0.85
hasMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected