-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Skip large diffs when generating context for the LLM #2217
Open
dividedmind
wants to merge
1
commit into
main
Choose a base branch
from
fix/skip-large-diffs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Examine all the diffs in a git diff output and replace long ones with a simple | ||
* message indicating that there is a change and its size. | ||
* @param patchset - The patchset to process. | ||
* @param maxDiffLength - The maximum length of a diff to include in the patchset. | ||
* @returns The processed patchset. | ||
*/ | ||
|
||
export default function processPatchset(patchset: string, maxDiffLength = 5000): string { | ||
const parts = patchset.split(/^(?=diff|commit)/m); | ||
return parts | ||
.map((part) => { | ||
if (part.startsWith('diff')) { | ||
const lines = part.split('\n'); | ||
const header = lines[0]; | ||
const body = lines.slice(1).join('\n'); | ||
if (body.length > maxDiffLength) { | ||
return `${header}\n[Change of size ${body.trimEnd().length}]\n`; | ||
} | ||
} | ||
return part; | ||
}) | ||
.join(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import processPatchset from '../../../src/lib/processPatchset'; | ||
|
||
describe('processPatchset', () => { | ||
it('should handle patchset with small diffs correctly', () => { | ||
const patchset = `diff --git a/file1.txt b/file1.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file1.txt | ||
+++ b/file1.txt | ||
@@ -1,3 +1,3 @@ | ||
-Hello World | ||
+Hello AppMap | ||
This is a test file.`; | ||
|
||
const result = processPatchset(patchset, 1000); | ||
expect(result).toContain('Hello AppMap'); | ||
}); | ||
|
||
it('should have the correct output format', () => { | ||
const largeDiff = 'a'.repeat(2000); | ||
const patchset = `Here's some header. | ||
diff --git a/file1.txt b/file1.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file1.txt | ||
+++ b/file1.txt | ||
@@ -1,3 +1,3 @@ | ||
-Hello World | ||
+Hello AppMap | ||
This is a test file. | ||
diff --git a/file2.txt b/file2.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file2.txt | ||
+++ b/file2.txt | ||
@@ -1,3 +1,3 @@ | ||
${largeDiff} | ||
diff --git a/file3.txt b/file3.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file3.txt | ||
+++ b/file3.txt | ||
@@ -1,3 +1,3 @@ | ||
-Hello World | ||
+Hello AppMap | ||
This is another test file.`; | ||
|
||
const result = processPatchset(patchset, 1000); | ||
expect(result).toMatchInlineSnapshot(` | ||
"Here's some header. | ||
diff --git a/file1.txt b/file1.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file1.txt | ||
+++ b/file1.txt | ||
@@ -1,3 +1,3 @@ | ||
-Hello World | ||
+Hello AppMap | ||
This is a test file. | ||
diff --git a/file2.txt b/file2.txt | ||
[Change of size 2078] | ||
diff --git a/file3.txt b/file3.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file3.txt | ||
+++ b/file3.txt | ||
@@ -1,3 +1,3 @@ | ||
-Hello World | ||
+Hello AppMap | ||
This is another test file." | ||
`); | ||
}); | ||
|
||
it('should handle patchsets with multiple commit headers like from git log', () => { | ||
const patchset = `commit 1 | ||
Author: John Doe <[email protected]> | ||
Date: Mon Jan 1 12:00:00 2023 +0000 | ||
|
||
Initial commit | ||
|
||
diff --git a/file1.txt b/file1.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file1.txt | ||
+++ b/file1.txt | ||
@@ -1,3 +1,3 @@ | ||
-Hello World | ||
+Hello AppMap | ||
This is a test file. | ||
|
||
commit 2 | ||
Author: Jane Doe <[email protected]> | ||
Date: Tue Jan 2 12:00:00 2023 +0000 | ||
|
||
Second commit | ||
|
||
diff --git a/file2.txt b/file2.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file2.txt | ||
+++ b/file2.txt | ||
@@ -1,3 +1,3 @@ | ||
-Hello World | ||
+Hello AppMap | ||
This is another test file.`; | ||
|
||
const result = processPatchset(patchset, 10); | ||
expect(result).toMatchInlineSnapshot(` | ||
"commit 1 | ||
Author: John Doe <[email protected]> | ||
Date: Mon Jan 1 12:00:00 2023 +0000 | ||
|
||
Initial commit | ||
|
||
diff --git a/file1.txt b/file1.txt | ||
[Change of size 126] | ||
commit 2 | ||
Author: Jane Doe <[email protected]> | ||
Date: Tue Jan 2 12:00:00 2023 +0000 | ||
|
||
Second commit | ||
|
||
diff --git a/file2.txt b/file2.txt | ||
[Change of size 132] | ||
" | ||
`); | ||
}); | ||
|
||
it('should handle patchset with large diffs correctly', () => { | ||
const largeDiff = 'a'.repeat(2000); | ||
const patchset = `diff --git a/file2.txt b/file2.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file2.txt | ||
+++ b/file2.txt | ||
@@ -1,3 +1,3 @@ | ||
${largeDiff}`; | ||
|
||
const result = processPatchset(patchset, 1000); | ||
expect(result).toContain('[Change of size 2078]'); | ||
}); | ||
|
||
it('should handle patchset with mixed diff sizes correctly', () => { | ||
const largeDiff = 'a'.repeat(2000); | ||
const smallDiff = 'b'.repeat(500); | ||
const patchset = `diff --git a/file3.txt b/file3.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file3.txt | ||
+++ b/file3.txt | ||
@@ -1,3 +1,3 @@ | ||
${largeDiff} | ||
diff --git a/file4.txt b/file4.txt | ||
index 83db48f..f735c4e 100644 | ||
--- a/file4.txt | ||
+++ b/file4.txt | ||
@@ -1,3 +1,3 @@ | ||
${smallDiff}`; | ||
|
||
const result = processPatchset(patchset, 1000); | ||
expect(result).toContain('[Change of size 2078]'); | ||
expect(result).toContain(smallDiff); | ||
}); | ||
|
||
it('should return other text unchanged', () => { | ||
const diffs = ['This is some text with no diffs.', '', '- this\n+ that']; | ||
const result = diffs.map((diff) => processPatchset(diff)); | ||
expect(result).toEqual(diffs); | ||
}); | ||
}); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been looking into this type of limiting as well, and one of the issues I see is that
getDiffLog
returns, as the name suggests,git log
output notgit diff
output. So, splitting this output would seem to be more naturally done on thecommit <sha>
(author) rather than on thediff --git
line.WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words, maybe it would be better / easier if
getDiffLog
andgetWorkingDiff
returned an output that had some structure (at least an array of chunks?) rather than just a string that the caller then has to figure out how to split.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I've actually noticed this too and fixed it in the amend below :)