( messages: Message[], context: ToolUseContext, cacheSafeParams: CacheSafeParams, suppressFollowUpQuestions: boolean, customInstructions?: string, isAutoCompact: boolean = false, recompactionInfo?: RecompactionInfo, )
| 385 | * and preserving recent conversation history. |
| 386 | */ |
| 387 | export 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 |
no test coverage detected