MCPcopy
hub / github.com/claude-code-best/claude-code / microcompactMessages

Function microcompactMessages

src/services/compact/microCompact.ts:257–297  ·  view source on GitHub ↗
(
  messages: Message[],
  toolUseContext?: ToolUseContext,
  querySource?: QuerySource,
)

Source from the content-addressed store, hash-verified

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

Callers 4

approximateMessageTokensFunction · 0.85
callFunction · 0.85
collectContextDataFunction · 0.85
callFunction · 0.85

Calls 6

getCachedMCModuleFunction · 0.85
getMainLoopModelFunction · 0.85
isMainThreadSourceFunction · 0.85
cachedMicrocompactPathFunction · 0.85

Tested by

no test coverage detected