MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / compactConversation

Function compactConversation

source code/services/compact/compact.ts:389–765  ·  view source on GitHub ↗
(
  messages: Message[],
  context: ToolUseContext,
  cacheSafeParams: CacheSafeParams,
  suppressFollowUpQuestions: boolean,
  customInstructions?: string,
  isAutoCompact: boolean = false,
  recompactionInfo?: RecompactionInfo,
)

Source from the content-addressed store, hash-verified

387 * and preserving recent conversation history.
388 */
389export async function compactConversation(
390 messages: Message[],
391 context: ToolUseContext,
392 cacheSafeParams: CacheSafeParams,
393 suppressFollowUpQuestions: boolean,
394 customInstructions?: string,
395 isAutoCompact: boolean = false,
396 recompactionInfo?: RecompactionInfo,
397): Promise<CompactionResult> {
398 try {
399 if (messages.length === 0) {
400 throw new Error(ERROR_MESSAGE_NOT_ENOUGH_MESSAGES)
401 }
402
403 const preCompactTokenCount = tokenCountWithEstimation(messages)
404
405 const appState = context.getAppState()
406 void logPermissionContextForAnts(appState.toolPermissionContext, 'summary')
407
408 context.onCompactProgress?.({
409 type: 'hooks_start',
410 hookType: 'pre_compact',
411 })
412
413 // Execute PreCompact hooks
414 context.setSDKStatus?.('compacting')
415 const hookResult = await executePreCompactHooks(
416 {
417 trigger: isAutoCompact ? 'auto' : 'manual',
418 customInstructions: customInstructions ?? null,
419 },
420 context.abortController.signal,
421 )
422 customInstructions = mergeHookInstructions(
423 customInstructions,
424 hookResult.newCustomInstructions,
425 )
426 const userDisplayMessage = hookResult.userDisplayMessage
427
428 // Show requesting mode with up arrow and custom message
429 context.setStreamMode?.('requesting')
430 context.setResponseLength?.(() => 0)
431 context.onCompactProgress?.({ type: 'compact_start' })
432
433 // 3P default: true — forked-agent path reuses main conversation's prompt cache.
434 // Experiment (Jan 2026) confirmed: false path is 98% cache miss, costs ~0.76% of
435 // fleet cache_creation (~38B tok/day), concentrated in ephemeral envs (CCR/GHA/SDK)
436 // with cold GB cache and 3P providers where GB is disabled. GB gate kept as kill-switch.
437 const promptCacheSharingEnabled = getFeatureValue_CACHED_MAY_BE_STALE(
438 'tengu_compact_cache_prefix',
439 true,
440 )
441
442 const compactPrompt = getCompactPrompt(customInstructions)
443 const summaryRequest = createUserMessage({
444 content: compactPrompt,
445 })
446

Callers 3

runInProcessTeammateFunction · 0.85
callFunction · 0.85
autoCompactIfNeededFunction · 0.85

Calls 15

tokenCountWithEstimationFunction · 0.85
executePreCompactHooksFunction · 0.85
mergeHookInstructionsFunction · 0.85
getCompactPromptFunction · 0.85
createUserMessageFunction · 0.85
streamCompactSummaryFunction · 0.85
getAssistantMessageTextFunction · 0.85
truncateHeadForPTLRetryFunction · 0.85
logEventFunction · 0.85
logForDebuggingFunction · 0.85

Tested by

no test coverage detected