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

Function compactConversation

src/services/compact/compact.ts:411–792  ·  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

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

Callers 5

runInProcessTeammateFunction · 0.85
callFunction · 0.85
tryReactiveCompactFunction · 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
jsonStringifyFunction · 0.85

Tested by

no test coverage detected