Skip to content

Commit 9ad3369

Browse files
cvick32brandonkachencodebuff-team
authored
Brandon/benchify (#325)
Co-authored-by: brandonkachen <brandonchenjiacheng@gmail.com> Co-authored-by: Codebuff <noreply@codebuff.com>
1 parent dc9d8d9 commit 9ad3369

File tree

1 file changed

+8
-56
lines changed

1 file changed

+8
-56
lines changed

backend/src/tools/batch-str-replace.ts

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -584,10 +584,10 @@ async function callBenchifyWithResilience(
584584
userInputId: string
585585
userId: string | undefined
586586
},
587-
): Promise<ParsedDiff[]> {
587+
): Promise<string | null> {
588588
const client = getBenchifyClient()
589589
if (!client) {
590-
return []
590+
return null
591591
}
592592

593593
return await withRetry(
@@ -601,17 +601,11 @@ async function callBenchifyWithResilience(
601601
BENCHIFY_TIMEOUT_MS,
602602
`Benchify call timed out after ${BENCHIFY_TIMEOUT_MS}ms`,
603603
)
604-
605-
// Validate response
606604
if (diff_response) {
607-
return validateBenchifyResponse(
608-
diff_response,
609-
editedFiles,
610-
context.agentStepId,
611-
)
605+
return diff_response
612606
}
613607

614-
return []
608+
return null
615609
},
616610
{
617611
maxRetries: 2,
@@ -631,44 +625,6 @@ async function callBenchifyWithResilience(
631625
)
632626
}
633627

634-
/**
635-
* Validates Benchify API response using pattern matching
636-
*/
637-
function validateBenchifyResponse(
638-
response: string,
639-
originalFiles: { path: string; contents: string }[],
640-
agentStepId: string,
641-
): ParsedDiff[] {
642-
const originalPaths = new Set(originalFiles.map((f) => f.path))
643-
644-
const patches = parsePatch(response)
645-
return patches.flatMap((patch) =>
646-
match(patch)
647-
.with({ oldFileName: P.string }, (res) => {
648-
// drop prefix a/ adding by diff patch
649-
const actualFileName = res.oldFileName.replace('a/', '')
650-
if (!originalPaths.has(actualFileName)) {
651-
logger.warn(
652-
{ path: actualFileName, agentStepId },
653-
'Benchify returned result for unexpected path',
654-
)
655-
return []
656-
}
657-
return [patch]
658-
})
659-
.otherwise(() => {
660-
logger.warn(
661-
{
662-
result: JSON.stringify(patch).substring(0, 100),
663-
agentStepId,
664-
},
665-
'Invalid Benchify patch',
666-
)
667-
return []
668-
}),
669-
)
670-
}
671-
672628
/**
673629
* Determines if a Benchify error should trigger a retry
674630
*/
@@ -701,7 +657,7 @@ function shouldRetryBenchifyError(error: Error): boolean {
701657
*/
702658
async function applyBenchifyResultsGracefully(
703659
editedFiles: { path: string; contents: string }[],
704-
benchifyDiffs: ParsedDiff[],
660+
benchifyDiff: string,
705661
context: {
706662
ws: WebSocket
707663
onResponseChunk: (chunk: string | PrintModeEvent) => void
@@ -714,12 +670,8 @@ async function applyBenchifyResultsGracefully(
714670
) {
715671
const results = await Promise.allSettled(
716672
editedFiles.map((editedFile) => {
717-
// again, we have to replace the a/ that the ParsedDiff introduced
718-
const diff = benchifyDiffs.find(
719-
(v) => v.oldFileName?.replace('a/', '') == editedFile.path,
720-
)
721-
if (diff) {
722-
applyBenchifyResultSafely(editedFile, diff, context)
673+
if (benchifyDiff) {
674+
applyBenchifyResultSafely(editedFile, benchifyDiff, context)
723675
} else {
724676
logger.warn(
725677
{ file: editedFile.path },
@@ -748,7 +700,7 @@ async function applyBenchifyResultsGracefully(
748700
*/
749701
async function applyBenchifyResultSafely(
750702
benchifyFile: { path: string; contents: string },
751-
benchifyDiff: ParsedDiff,
703+
benchifyDiff: string,
752704
context: {
753705
ws: WebSocket
754706
onResponseChunk: (chunk: string | PrintModeEvent) => void

0 commit comments

Comments
 (0)