fix(core): balance truncated patch summaries

This commit is contained in:
Kit Langton 2026-07-20 15:27:40 -04:00
commit a9bc065996
3 changed files with 11 additions and 4 deletions

View file

@ -44,10 +44,17 @@ export function patch(input: string, maximumBytes: number) {
const parsed = parsePatch(input)[0]
const hunk = parsed?.hunks[0]
if (!parsed || !hunk) return truncate(input, maximumBytes)
const changed = [hunk.lines.find((line) => line.startsWith("-")), hunk.lines.find((line) => line.startsWith("+"))]
const changedLines = parsed.hunks.flatMap((item) => item.lines).filter((line) => /^[+-]/.test(line))
const removed = changedLines.filter((line) => line.startsWith("-"))
const added = changedLines.filter((line) => line.startsWith("+"))
const changed = [removed[0], added[0]]
.filter((line) => line !== undefined)
.map((line) => line[0] + truncate(line.slice(1), Math.max(0, Math.floor(maximumBytes / 2) - 1)))
const lines = [...changed, " ... diff truncated ..."]
const lines = [
...changed,
...(removed.length > 1 ? [`-... ${removed.length - 1} removed lines omitted ...`] : []),
...(added.length > 1 ? [`+... ${added.length - 1} added lines omitted ...`] : []),
]
return formatPatch({
...parsed,
hunks: [

View file

@ -198,7 +198,7 @@ describe("EditTool", () => {
const hunk = parsePatch(structured.files[0]?.patch ?? "")[0]?.hunks[0]
expect(hunk?.lines.some((line) => line.startsWith("-"))).toBe(true)
expect(hunk?.lines.some((line) => line.startsWith("+"))).toBe(true)
expect(hunk).toMatchObject({ oldLines: 2, newLines: 2 })
expect(hunk).toMatchObject({ oldLines: 1, newLines: 1 })
expect(structured).toMatchObject({
replacements: 1,
files: [

View file

@ -241,7 +241,7 @@ describe("PatchTool", () => {
const hunk = parsePatch(structured.files[0]?.patch ?? "")[0]?.hunks[0]
expect(hunk?.lines.some((line) => line.startsWith("-"))).toBe(true)
expect(hunk?.lines.some((line) => line.startsWith("+"))).toBe(true)
expect(hunk).toMatchObject({ oldLines: 2, newLines: 2 })
expect(hunk).toMatchObject({ oldLines: 1, newLines: 1 })
expect(structured).toMatchObject({
applied: [{ type: "update", resource: "large.txt" }],
files: [{ file: "large.txt", patch: expect.stringContaining("... truncated ...") }],