Skip to content

Diagnosing stale LSP state in long-running sessions #28

@jessealama

Description

@jessealama

Problem

In long-running sessions (e.g., when using lean-lsp-mcp with Claude Code), the diagnostics returned by the LSP can become stale. Specifically, diagnostics may differ from what a fresh lake build <file> produces on the same file.

This issue is hard to reproduce reliably, but seems to occur when:

  1. Files are edited externally (e.g., by an AI coding assistant)
  2. leanclient syncs the new content via textDocument/didChange
  3. Diagnostics are retrieved, but they don't reflect the true compilation state

Running lake build externally and then querying diagnostics can produce different (often more accurate) results.

Where does the problem originate?

This may be a layered issue:

Lean LSP server (incremental elaboration)
        ↑
   leanclient (Python LSP client)
        ↑
  lean-lsp-mcp (MCP wrapper)
        ↑
   Claude Code (edits files)

Lean LSP server: Uses incremental elaboration for performance. When textDocument/didChange is received, it re-elaborates the file but may use cached/stale internal state for imports and dependencies. This is likely the root cause.

leanclient: Correctly syncs file content to the LSP server. However, there may be limited options for forcing a full re-elaboration vs. incremental.

lean-lsp-mcp: Could detect staleness and trigger rebuilds, but this is a workaround rather than a fix.

Questions

  1. Is there an LSP protocol mechanism to force the Lean LSP server to fully re-elaborate a file (not incrementally)?

  2. Does closing and reopening a file (force_reopen=True) help clear the LSP server's internal state?

  3. Should this be raised as an issue with the Lean project if the root cause is in the LSP server's incremental elaboration?

Potential mitigations

In leanclient

  • Track file modification times to detect when external edits occurred
  • Expose staleness detection so consumers can decide how to respond
  • Test whether force_reopen=True helps vs. just didChange

In lean-lsp-mcp

  • Detect staleness before returning diagnostics
  • Auto-trigger lean_build when stale state is detected (slow but reliable)

Notes

  • The issue primarily manifests in long-running agentic sessions with many edits
  • Current workaround is periodic lean_build calls, but this is slow
  • Understanding whether this is fixable at the client level or requires Lean changes would help prioritize the right solution

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions