1- import { createHash } from 'crypto'
2-
31import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
42import { supportsCacheControl } from '@codebuff/common/old-constants'
53import { TOOLS_WHICH_WONT_FORCE_NEXT_STEP } from '@codebuff/common/tools/constants'
64import { buildArray } from '@codebuff/common/util/array'
75import { AbortError , getErrorObject , isAbortError } from '@codebuff/common/util/error'
6+ import { serializeCacheDebugCorrelation } from '@codebuff/common/util/cache-debug'
87import { systemMessage , userMessage } from '@codebuff/common/util/messages'
98import { APICallError , type ToolSet } from 'ai'
109import { cloneDeep , mapValues } from 'lodash'
@@ -21,7 +20,10 @@ import { getAgentPrompt } from './templates/strings'
2120import { getToolSet } from './tools/prompts'
2221import { processStream } from './tools/stream-parser'
2322import { getAgentOutput } from './util/agent-output'
24- import { writeCacheDebugSnapshot } from './util/cache-debug'
23+ import {
24+ createCacheDebugSnapshot ,
25+ enrichCacheDebugSnapshotWithProviderRequest ,
26+ } from './util/cache-debug'
2527import {
2628 withSystemInstructionTags ,
2729 withSystemTags as withSystemTags ,
@@ -259,6 +261,52 @@ export const runAgentStep = async (
259261 const iterationNum = agentState . messageHistory . length
260262 const systemTokens = countTokensJson ( system )
261263
264+ const cacheDebugCorrelation = CACHE_DEBUG_FULL_LOGGING
265+ ? createCacheDebugSnapshot ( {
266+ agentType : String ( agentType ) ,
267+ system,
268+ toolDefinitions : params . tools
269+ ? Object . fromEntries (
270+ Object . entries ( params . tools ) . map ( ( [ name , tool ] ) => [
271+ name ,
272+ {
273+ description : tool . description ,
274+ inputSchema : tool . inputSchema as { } ,
275+ } ,
276+ ] ) ,
277+ )
278+ : { } ,
279+ messages : [ systemMessage ( system ) , ...agentState . messageHistory ] ,
280+ logger,
281+ projectRoot : fileContext . projectRoot ,
282+ runId : agentState . runId ,
283+ userInputId,
284+ agentStepId,
285+ model,
286+ } )
287+ : undefined
288+
289+ const onCacheDebugProviderRequestBuilt =
290+ cacheDebugCorrelation
291+ ? ( {
292+ provider,
293+ rawBody,
294+ normalizedBody,
295+ } : {
296+ provider : string
297+ rawBody : unknown
298+ normalizedBody ?: unknown
299+ } ) => {
300+ enrichCacheDebugSnapshotWithProviderRequest ( {
301+ correlation : cacheDebugCorrelation ,
302+ provider,
303+ rawBody,
304+ normalized : normalizedBody ?? rawBody ,
305+ logger,
306+ } )
307+ }
308+ : undefined
309+
262310 logger . debug (
263311 {
264312 iteration : iterationNum ,
@@ -286,6 +334,10 @@ export const runAgentStep = async (
286334 model,
287335 n : params . n ,
288336 onCostCalculated,
337+ cacheDebugCorrelation : cacheDebugCorrelation
338+ ? serializeCacheDebugCorrelation ( cacheDebugCorrelation )
339+ : undefined ,
340+ onCacheDebugProviderRequestBuilt,
289341 } )
290342
291343 if ( result . aborted ) {
@@ -336,8 +388,12 @@ export const runAgentStep = async (
336388 ...params ,
337389 agentId : agentState . parentId ? agentState . agentId : undefined ,
338390 costMode : params . costMode ,
391+ cacheDebugCorrelation : cacheDebugCorrelation
392+ ? serializeCacheDebugCorrelation ( cacheDebugCorrelation )
393+ : undefined ,
339394 includeCacheControl : supportsCacheControl ( agentTemplate . model ) ,
340395 messages : [ systemMessage ( system ) , ...agentState . messageHistory ] ,
396+ onCacheDebugProviderRequestBuilt,
341397 template : agentTemplate ,
342398 onCostCalculated,
343399 } )
@@ -715,36 +771,6 @@ export async function loopAgentSteps(
715771 inputSchema : tool . inputSchema as { } ,
716772 } ) )
717773
718- if ( CACHE_DEBUG_FULL_LOGGING ) {
719- // Debug: hash the system prompt and tool definitions to detect prompt cache invalidation
720- const systemHash = createHash ( 'sha256' ) . update ( system ) . digest ( 'hex' ) . slice ( 0 , 8 )
721- const sortedToolDefs = Object . keys ( toolDefinitions ) . sort ( ) . reduce ( ( acc , key ) => {
722- acc [ key ] = toolDefinitions [ key ]
723- return acc
724- } , { } as Record < string , unknown > )
725- const toolsHash = createHash ( 'sha256' ) . update ( JSON . stringify ( sortedToolDefs ) ) . digest ( 'hex' ) . slice ( 0 , 8 )
726- logger . debug (
727- {
728- systemHash,
729- toolsHash,
730- systemLength : system . length ,
731- toolCount : Object . keys ( toolDefinitions ) . length ,
732- toolNames : Object . keys ( toolDefinitions ) . sort ( ) ,
733- agentType,
734- } ,
735- `[Cache Debug] System prompt hash: ${ systemHash } , Tools hash: ${ toolsHash } ` ,
736- )
737-
738- writeCacheDebugSnapshot ( {
739- agentType : String ( agentType ) ,
740- system,
741- toolDefinitions : sortedToolDefs ,
742- messages : initialMessages ,
743- logger,
744- projectRoot : fileContext . projectRoot ,
745- } )
746- }
747-
748774 const additionalToolDefinitionsWithCache = async ( ) => {
749775 if ( ! cachedAdditionalToolDefinitions ) {
750776 cachedAdditionalToolDefinitions = await additionalToolDefinitions ( {
0 commit comments