MCPcopy Create free account
hub / github.com/Noumena-Network/code / compactConversation

Function compactConversation

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

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

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
streamCompactSummaryFunction · 0.85
getAssistantMessageTextFunction · 0.85
truncateHeadForPTLRetryFunction · 0.85
jsonStringifyFunction · 0.85
startsWithApiErrorPrefixFunction · 0.85
cacheToObjectFunction · 0.85

Tested by

no test coverage detected