@@ -33,24 +33,54 @@ function getBenchifyClient(): Benchify | null {
3333 if ( ! benchifyClient ) {
3434 let benchifyApiKey : string | undefined
3535 try {
36+ // Log available environment variables (partial for debugging)
37+ const envKeys = Object . keys ( process . env )
38+ . filter ( ( key ) => key . includes ( 'BENCHIFY' ) || key . includes ( 'API' ) )
39+ . slice ( 0 , 10 ) // Limit to first 10 for safety
40+
3641 benchifyApiKey = env . BENCHIFY_API_KEY
42+ logger . info (
43+ {
44+ hasApiKey : ! ! benchifyApiKey ,
45+ apiKeyLength : benchifyApiKey ?. length || 0 ,
46+ apiKeyPrefix : benchifyApiKey ?. substring ( 0 , 8 ) || 'none' ,
47+ availableEnvKeys : envKeys ,
48+ nodeEnv : process . env . NODE_ENV ,
49+ } ,
50+ 'getBenchifyClient: Attempting to access BENCHIFY_API_KEY from environment' ,
51+ )
3752 } catch ( error ) {
3853 logger . warn (
3954 {
4055 error : error instanceof Error ? error . message : String ( error ) ,
56+ nodeEnv : process . env . NODE_ENV ,
4157 } ,
42- 'Failed to access BENCHIFY_API_KEY from environment' ,
58+ 'getBenchifyClient: Failed to access BENCHIFY_API_KEY from environment' ,
4359 )
4460 return null
4561 }
4662
4763 if ( ! benchifyApiKey ) {
64+ logger . warn (
65+ 'getBenchifyClient: No BENCHIFY_API_KEY found, returning null' ,
66+ )
4867 return null
4968 }
5069
51- benchifyClient = new Benchify ( {
52- apiKey : benchifyApiKey ,
53- } )
70+ try {
71+ benchifyClient = new Benchify ( {
72+ apiKey : benchifyApiKey ,
73+ } )
74+ logger . info ( 'getBenchifyClient: Successfully created Benchify client' )
75+ } catch ( error ) {
76+ logger . error (
77+ {
78+ error : error instanceof Error ? error . message : String ( error ) ,
79+ } ,
80+ 'getBenchifyClient: Failed to create Benchify client' ,
81+ )
82+ return null
83+ }
5484 }
5585 return benchifyClient
5686}
@@ -84,7 +114,20 @@ export async function executeBatchStrReplaces({
84114 state : Record < string , any >
85115 userId : string | undefined
86116} ) {
117+ logger . info (
118+ {
119+ deferredCount : deferredStrReplaces . length ,
120+ agentStepId,
121+ userInputId,
122+ userId,
123+ } ,
124+ 'executeBatchStrReplaces: Starting batch execution' ,
125+ )
126+
87127 if ( deferredStrReplaces . length === 0 ) {
128+ logger . info (
129+ 'executeBatchStrReplaces: No deferred str_replace operations, returning early' ,
130+ )
88131 return
89132 }
90133
@@ -281,8 +324,30 @@ export async function executeBatchStrReplaces({
281324 await Promise . all ( batchPromises )
282325
283326 // Call benchify with intended changes (even if str_replace operations failed)
327+ logger . info (
328+ {
329+ intendedChangesCount : intendedChanges . length ,
330+ editedFilesCount : editedFiles . length ,
331+ intendedChangeFiles : intendedChanges . map ( ( f ) => f . path ) ,
332+ editedFilesList : editedFiles . map ( ( f ) => f . path ) ,
333+ agentStepId,
334+ userInputId,
335+ } ,
336+ 'executeBatchStrReplaces: Preparing to call benchify' ,
337+ )
338+
284339 const client = getBenchifyClient ( )
285- if ( ! client || intendedChanges . length === 0 ) {
340+ if ( ! client ) {
341+ logger . warn (
342+ 'executeBatchStrReplaces: No benchify client available, skipping benchify call' ,
343+ )
344+ return
345+ }
346+
347+ if ( intendedChanges . length === 0 ) {
348+ logger . warn (
349+ 'executeBatchStrReplaces: No intended changes for benchify, skipping benchify call' ,
350+ )
286351 return
287352 }
288353
@@ -340,26 +405,68 @@ async function callBenchify(
340405 userId : string | undefined
341406 } ,
342407) : Promise < { path : string ; contents : string } [ ] | null > {
408+ logger . info (
409+ {
410+ editedFilesCount : editedFiles . length ,
411+ editedFilesList : editedFiles . map ( ( f ) => f . path ) ,
412+ totalContentLength : editedFiles . reduce (
413+ ( sum , f ) => sum + f . contents . length ,
414+ 0 ,
415+ ) ,
416+ ...context ,
417+ } ,
418+ 'callBenchify: Starting benchify API call' ,
419+ )
420+
343421 const client = getBenchifyClient ( )
344422 if ( ! client ) {
423+ logger . error ( 'callBenchify: No benchify client available' )
345424 return null
346425 }
347426
348- const response = await client . runFixer ( editedFiles , {
349- fix_types : [ 'string_literals' ] ,
350- } )
427+ try {
428+ logger . info (
429+ {
430+ fixTypes : [ 'string_literals' ] ,
431+ ...context ,
432+ } ,
433+ 'callBenchify: Calling client.runFixer' ,
434+ )
351435
352- logger . info (
353- {
354- responseReceived : ! ! response ,
355- responseLength : response ?. length || 0 ,
356- responseFiles : response ?. map ( ( r ) => r . path ) || [ ] ,
357- ...context ,
358- } ,
359- 'Benchify runFixer API response received' ,
360- )
436+ const response = await client . runFixer ( editedFiles , {
437+ fix_types : [ 'string_literals' ] ,
438+ } )
361439
362- return response
440+ logger . info (
441+ {
442+ responseReceived : ! ! response ,
443+ responseLength : response ?. length || 0 ,
444+ responseFiles : response ?. map ( ( r ) => r . path ) || [ ] ,
445+ responseContentLengths : response ?. map ( ( r ) => r . contents . length ) || [ ] ,
446+ ...context ,
447+ } ,
448+ 'callBenchify: Benchify runFixer API response received successfully' ,
449+ )
450+
451+ return response
452+ } catch ( error ) {
453+ logger . error (
454+ {
455+ error :
456+ error instanceof Error
457+ ? {
458+ message : error . message ,
459+ stack : error . stack ,
460+ name : error . name ,
461+ }
462+ : String ( error ) ,
463+ editedFilesCount : editedFiles . length ,
464+ ...context ,
465+ } ,
466+ 'callBenchify: Failed to call benchify API' ,
467+ )
468+ throw error
469+ }
363470}
364471
365472/**
@@ -376,7 +483,25 @@ async function applyBenchifyResults(
376483 userInputId : string
377484 } ,
378485) {
486+ logger . info (
487+ {
488+ benchifyFilesCount : benchifyFiles . length ,
489+ benchifyFilesList : benchifyFiles . map ( ( f ) => f . path ) ,
490+ toolCallsCount : context . toolCalls . length ,
491+ userInputId : context . userInputId ,
492+ } ,
493+ 'applyBenchifyResults: Starting to apply benchify results' ,
494+ )
495+
379496 for ( const benchifyFile of benchifyFiles ) {
497+ logger . debug (
498+ {
499+ fileName : benchifyFile . path ,
500+ contentLength : benchifyFile . contents . length ,
501+ userInputId : context . userInputId ,
502+ } ,
503+ 'applyBenchifyResults: Processing benchify file' ,
504+ )
380505 try {
381506 // Find the corresponding tool call for this file
382507 const relatedToolCall = context . toolCalls . find (
@@ -385,12 +510,27 @@ async function applyBenchifyResults(
385510
386511 if ( ! relatedToolCall ) {
387512 logger . warn (
388- { fileName : benchifyFile . path } ,
389- 'No matching tool call found for benchify result' ,
513+ {
514+ fileName : benchifyFile . path ,
515+ availableToolCallPaths : context . toolCalls . map (
516+ ( tc ) => tc . input . path ,
517+ ) ,
518+ userInputId : context . userInputId ,
519+ } ,
520+ 'applyBenchifyResults: No matching tool call found for benchify result' ,
390521 )
391522 continue
392523 }
393524
525+ logger . debug (
526+ {
527+ fileName : benchifyFile . path ,
528+ relatedToolCallId : relatedToolCall . toolCallId ,
529+ userInputId : context . userInputId ,
530+ } ,
531+ 'applyBenchifyResults: Found matching tool call for benchify result' ,
532+ )
533+
394534 // Get the original file content from our stored contents
395535 const originalContent =
396536 context . state . originalContents ?. [ benchifyFile . path ]
0 commit comments