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

Function compactConversation

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

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

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