Skip to content

Commit b5ed9e2

Browse files
committed
fix: bound edit mismatch diagnostics before normalization
1 parent 475a968 commit b5ed9e2

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

src/agents/sessions/tools/edit.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ describe("edit tool", () => {
154154
expect(message).not.toContain(targetTail);
155155
});
156156

157-
it("bounds many-line mismatch candidate diagnostics before collecting lines", async () => {
157+
it("bounds CRLF mismatch candidate diagnostics before collecting lines", async () => {
158158
const scannedCandidate = "inside-scan-candidate";
159159
const unscannedCandidate = "outside-scan-candidate";
160160
const filler = Array.from({ length: 999 }, (_, index) => `filler ${index + 1}`);
161161
const filePath = await createTempFile(
162-
`${filler.join("\n")}\nalpha ${scannedCandidate}\nalpha ${unscannedCandidate}\n`,
162+
`${filler.join("\r\n")}\r\nalpha ${scannedCandidate}\r\nalpha ${unscannedCandidate}\r\n`,
163163
);
164164
const tool = createEditTool(tmpDir);
165165

src/agents/sessions/tools/edit.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,39 @@ function splitBoundedDiagnosticLines(text: string): string[] {
214214
}
215215

216216
function collectBoundedDiagnosticLines(text: string): string[] {
217-
const normalized = normalizeToLF(text);
218217
const lines: string[] = [];
219-
let start = 0;
220-
while (lines.length < EDIT_MISMATCH_SCAN_LINE_LIMIT) {
221-
const end = normalized.indexOf("\n", start);
222-
const rawLine = end === -1 ? normalized.slice(start) : normalized.slice(start, end);
223-
lines.push(truncateDiagnosticLine(rawLine));
224-
if (end === -1) {
225-
break;
218+
let line = "";
219+
let lineTruncated = false;
220+
221+
const pushLine = () => {
222+
lines.push(lineTruncated ? `${line}... (line truncated)` : line);
223+
line = "";
224+
lineTruncated = false;
225+
};
226+
227+
for (
228+
let index = 0;
229+
index < text.length && lines.length < EDIT_MISMATCH_SCAN_LINE_LIMIT;
230+
index++
231+
) {
232+
const char = text[index];
233+
if (char === "\r" || char === "\n") {
234+
pushLine();
235+
if (char === "\r" && text[index + 1] === "\n") {
236+
index++;
237+
}
238+
continue;
226239
}
227-
start = end + 1;
240+
241+
if (line.length < EDIT_MISMATCH_LINE_TEXT_LIMIT) {
242+
line += char;
243+
} else {
244+
lineTruncated = true;
245+
}
246+
}
247+
248+
if (lines.length < EDIT_MISMATCH_SCAN_LINE_LIMIT) {
249+
pushLine();
228250
}
229251
if (lines.length > 1 && lines[lines.length - 1] === "") {
230252
lines.pop();

0 commit comments

Comments
 (0)