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