Skip to content

Commit aedb555

Browse files
fix: limit LSP diagnostics to prevent context window waste (#5480)
Co-authored-by: Aiden Cline <[email protected]>
1 parent 1638ffd commit aedb555

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

‎packages/opencode/src/tool/edit.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { Instance } from "../project/instance"
1818
import { Agent } from "../agent/agent"
1919
import { Snapshot } from "@/snapshot"
2020

21+
const MAX_DIAGNOSTICS_PER_FILE = 20
22+
2123
function normalizeLineEndings(text: string): string {
2224
return text.replaceAll("\r\n", "\n")
2325
}
@@ -141,10 +143,11 @@ export const EditTool = Tool.define("edit", {
141143
for (const [file, issues] of Object.entries(diagnostics)) {
142144
if (issues.length === 0) continue
143145
if (file === filePath) {
144-
output += `\nThis file has errors, please fix\n<file_diagnostics>\n${issues
145-
.filter((item) => item.severity === 1)
146-
.map(LSP.Diagnostic.pretty)
147-
.join("\n")}\n</file_diagnostics>\n`
146+
const errors = issues.filter((item) => item.severity === 1)
147+
const limited = errors.slice(0, MAX_DIAGNOSTICS_PER_FILE)
148+
const suffix =
149+
errors.length > MAX_DIAGNOSTICS_PER_FILE ? `\n... and ${errors.length - MAX_DIAGNOSTICS_PER_FILE} more` : ""
150+
output += `\nThis file has errors, please fix\n<file_diagnostics>\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</file_diagnostics>\n`
148151
continue
149152
}
150153
}

‎packages/opencode/src/tool/write.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import { Filesystem } from "../util/filesystem"
1111
import { Instance } from "../project/instance"
1212
import { Agent } from "../agent/agent"
1313

14+
const MAX_DIAGNOSTICS_PER_FILE = 20
15+
const MAX_PROJECT_DIAGNOSTICS_FILES = 5
16+
1417
export const WriteTool = Tool.define("write", {
1518
description: DESCRIPTION,
1619
parameters: z.object({
@@ -77,13 +80,20 @@ export const WriteTool = Tool.define("write", {
7780
let output = ""
7881
await LSP.touchFile(filepath, true)
7982
const diagnostics = await LSP.diagnostics()
83+
let projectDiagnosticsCount = 0
8084
for (const [file, issues] of Object.entries(diagnostics)) {
8185
if (issues.length === 0) continue
86+
const sorted = issues.toSorted((a, b) => (a.severity ?? 4) - (b.severity ?? 4))
87+
const limited = sorted.slice(0, MAX_DIAGNOSTICS_PER_FILE)
88+
const suffix =
89+
issues.length > MAX_DIAGNOSTICS_PER_FILE ? `\n... and ${issues.length - MAX_DIAGNOSTICS_PER_FILE} more` : ""
8290
if (file === filepath) {
83-
output += `\nThis file has errors, please fix\n<file_diagnostics>\n${issues.map(LSP.Diagnostic.pretty).join("\n")}\n</file_diagnostics>\n`
91+
output += `\nThis file has errors, please fix\n<file_diagnostics>\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</file_diagnostics>\n`
8492
continue
8593
}
86-
output += `\n<project_diagnostics>\n${file}\n${issues.map(LSP.Diagnostic.pretty).join("\n")}\n</project_diagnostics>\n`
94+
if (projectDiagnosticsCount >= MAX_PROJECT_DIAGNOSTICS_FILES) continue
95+
projectDiagnosticsCount++
96+
output += `\n<project_diagnostics>\n${file}\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</project_diagnostics>\n`
8797
}
8898

8999
return {

0 commit comments

Comments
 (0)