MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / tryApplyPatchWithFallbacks

Function tryApplyPatchWithFallbacks

sdk/src/tools/apply-patch.ts:501–550  ·  view source on GitHub ↗
(params: {
  oldContent: string
  diff: string
})

Source from the content-addressed store, hash-verified

499}
500
501function tryApplyPatchWithFallbacks(params: {
502 oldContent: string
503 diff: string
504}): {
505 patched: string | null
506 attemptedStrategies: string[]
507 lastError?: string
508} {
509 const attempts = buildPatchAttempts(params.oldContent, params.diff)
510 const attemptedStrategies: string[] = []
511 let lastError: string | undefined
512
513 const seen = new Set<string>()
514
515 for (const attempt of attempts) {
516 const key = JSON.stringify({
517 source: attempt.source,
518 diff: attempt.diff,
519 })
520
521 if (seen.has(key)) {
522 continue
523 }
524
525 seen.add(key)
526 attemptedStrategies.push(attempt.name)
527
528 try {
529 const { result: patched } = applyDiff(attempt.source, attempt.diff, 'default')
530
531 if (patchHasIntendedChanges(attempt.diff) && patched === attempt.source) {
532 lastError = 'Patch produced no content changes'
533 continue
534 }
535
536 return {
537 patched,
538 attemptedStrategies,
539 }
540 } catch (error) {
541 lastError = error instanceof Error ? error.message : String(error)
542 }
543 }
544
545 return {
546 patched: null,
547 attemptedStrategies,
548 ...(lastError ? { lastError } : {}),
549 }
550}
551
552function formatPatchFailureMessage(params: {
553 path: string

Callers 1

applyPatchToolFunction · 0.85

Calls 3

buildPatchAttemptsFunction · 0.85
applyDiffFunction · 0.85
patchHasIntendedChangesFunction · 0.85

Tested by

no test coverage detected