( messages: Message[], toolUseContext?: ToolUseContext, querySource?: QuerySource, )
| 251 | } |
| 252 | |
| 253 | export async function microcompactMessages( |
| 254 | messages: Message[], |
| 255 | toolUseContext?: ToolUseContext, |
| 256 | querySource?: QuerySource, |
| 257 | ): Promise<MicrocompactResult> { |
| 258 | // Clear suppression flag at start of new microcompact attempt |
| 259 | clearCompactWarningSuppression() |
| 260 | |
| 261 | // Time-based trigger runs first and short-circuits. If the gap since the |
| 262 | // last assistant message exceeds the threshold, the server cache has expired |
| 263 | // and the full prefix will be rewritten regardless — so content-clear old |
| 264 | // tool results now, before the request, to shrink what gets rewritten. |
| 265 | // Cached MC (cache-editing) is skipped when this fires: editing assumes a |
| 266 | // warm cache, and we just established it's cold. |
| 267 | const timeBasedResult = maybeTimeBasedMicrocompact(messages, querySource) |
| 268 | if (timeBasedResult) { |
| 269 | return timeBasedResult |
| 270 | } |
| 271 | |
| 272 | // Only run cached MC for the main thread to prevent forked agents |
| 273 | // (session_memory, prompt_suggestion, etc.) from registering their |
| 274 | // tool_results in the global cachedMCState, which would cause the main |
| 275 | // thread to try deleting tools that don't exist in its own conversation. |
| 276 | if (feature('CACHED_MICROCOMPACT')) { |
| 277 | const mod = await getCachedMCModule() |
| 278 | const model = toolUseContext?.options.mainLoopModel ?? getMainLoopModel() |
| 279 | if ( |
| 280 | mod.isCachedMicrocompactEnabled() && |
| 281 | mod.isModelSupportedForCacheEditing(model) && |
| 282 | isMainThreadSource(querySource) |
| 283 | ) { |
| 284 | return await cachedMicrocompactPath(messages, querySource) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // Legacy microcompact path removed — tengu_cache_plum_violet is always true. |
| 289 | // For contexts where cached microcompact is not available (external builds, |
| 290 | // non-ant users, unsupported models, sub-agents), no compaction happens here; |
| 291 | // autocompact handles context pressure instead. |
| 292 | return { messages } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Cached microcompact path - uses cache editing API to remove tool results |
no test coverage detected