Skip to content

LSP diagnostics from external projects leak into tool results and context window #16353

Closed as not planned

Description

@HALDRO

Description

LSP.diagnostics() in packages/opencode/src/lsp/index.ts:291 aggregates diagnostics from ALL active LSP clients into a single Record<string, Diagnostic[]> with no workspace/project path filtering. This causes diagnostics from files outside the current project to appear in tool results.

Impact

  1. Context window pollution — write.ts appends up to 5 "other files" diagnostics to the text output (what the model sees). These can come from completely unrelated projects, wasting context tokens and potentially confusing the model into "fixing" errors in the wrong codebase.

  2. metadata.diagnostics — all three tools (write, edit, apply_patch) pass the entire unfiltered diagnostics map into metadata, which surfaces in UI clients.

Discovered while building a VS Code extension that consumes OpenCode CLI via SSE/SDK. When the agent reads files from an external project and then edits files in the current workspace, diagnostics from the external project leak into metadata.diagnostics and pollute the context window.

Plugins

No response

OpenCode version

checked 1.1.60 & v1.2.20

Steps to reproduce

  1. Open a project (Project A)
  2. Ask the agent to read/analyze files from a different project on disk (Project B)
  3. Then ask the agent to edit a file in Project A
  4. The tool result will contain LSP diagnostics from Project B files

This happens because TypeScript language server follows imports across project boundaries, and once it starts tracking external files, their diagnostics persist in the client's diagnostics map.

Root cause

// packages/opencode/src/lsp/index.ts:291
export async function diagnostics() {
  const results: Record<string, LSPClient.Diagnostic[]> = {}
  for (const result of await runAll(async (client) => client.diagnostics)) {
    for (const [path, diagnostics] of result.entries()) {
      // No filtering — paths from any project end up here
      const arr = results[path] || []
      arr.push(...diagnostics)
      results[path] = arr
    }
  }
  return results
}

Activity

  1. added
    bug
    on Mar 6, 2026
  2. added
    core
    on Mar 6, 2026
  3. added a commit that references this issue on Mar 7, 2026
    7599193
  4. assigned and unassigned on Apr 26, 2026
  5. removed
    bug
    core
    on May 3, 2026
  6. github-actions commented on Jul 3, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

  7. one-pyy commented on Oct 5, 2026

    @one-pyy

    Same root cause as here: even though the UI only renders the edited file's diagnostics, all three tools (write, edit, apply_patch) still write the entire unfiltered diagnostics map into metadata.diagnostics, which is exactly where external-project entries leak in and get persisted.

    Workaround while this is unaddressed: opencode-edit-diagnostics-pruner — https://github.com/one-pyy/opencode-edit-diagnostics-pruner — a tool.execute.after hook that trims metadata.diagnostics to only the files touched by the current call and leaves everything else (tool output, input, other metadata) intact. It resolves relative paths against the project directory and preserves the full map whenever a path cannot be resolved, so it fails safe. The same repo also ships a CLI to prune already-stored sessions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions