Add lazy-loading and LRU cache for context-based lookups#62
Merged
Conversation
Cross-URI LRU eviction in GetOrLoad could call ts_tree_delete on a tree another LSP handler was actively walking (RootNode, queries), causing a use-after-free on C-allocated memory that the Go race detector cannot observe. GetTree now returns a release closure; Set/Close/CloseAll/eviction mark the tree retired but defer the free until the last holder releases.
…-owned Add HasOpen(uri) to DocumentStore that returns true only for non-transient entries (i.e. those added via Set/didOpen). Transient entries loaded from disk via GetOrLoad now correctly return false from HasOpen. Update readFileText and getFileLine to use HasOpen instead of Get for the open-check, so rename operations branch correctly on the open flag — transient files are written directly to disk rather than sent as LSP text edits for buffers the editor never opened.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit e978adf. Configure here.
…FileLine
HasOpen + Get were two separate RLock acquisitions. If Close removed
the document between them, Get returned ("", false) which was
discarded via _, leaving callers with empty text and open=true.
GetIfOpen returns both text and editor-owned status under a single
RLock, matching the atomicity of the original single Get call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

"Headless" LSP clients such as Claude Code don't actually open files when doing lookups or changing them. This means that some of our tricks for looking things up via the context of the files breaks.
This PR adds lazy disk-fallback to the document store so LSP clients that don't drive the full
didOpen/didChange/didCloselifecycle (e.g. Claude Code) can still query definitions, hovers, references, and other text-dependent handlers. Use-injected lookups in particular:lookupThroughUse,lookupThroughUseOf. Alias merging also previously returnednilfor any file the editor hadn't explicitly opened.The goal of these changes is enablement work for a proper Claude Code plugin: #59.
What changed
DocumentStore.GetOrLoad(uri): lazy disk read for anyfile://URI that isn't already in the store. Disk-loaded entries are markedtransient: trueand tracked in a Least Recently Used (LRU) cache. Non-file://URIs (e.g.untitled:) return(false)without touching disk to avoid theuriToPathpanic.SetviadidOpen) are never counted, never reordered, never evicted.Setcleanly promotes a transient entry to editor-owned, dropping its LRU bookkeeping.maxTransientDocumentsinitialization option (default 50, accepts integer or JSON number, clamps negatives to 0). Documented inREADME.md.Definition,Hover,References,Completion,CodeAction,Declaration,DocumentHighlight,DocumentSymbol,FoldingRanges,Implementation,PrepareRename,Rename,SignatureHelp,TypeDefinition,PrepareCallHierarchy, …) now callGetOrLoad.Formattingdeliberately keepsGet— it only makes sense for in-memory editor buffers.Correctness notes
SetorGetOrLoadpopulated the URI first.bumpLRUis a no-op if the URI was promoted to editor-owned between the RLock and Lock, so the fast-path race is safe.evictTransientLockedcloses the cached tree-sitter tree before deletion - no leak.go test -race.Note
Medium Risk
Touches core document lifecycle, concurrent tree-sitter memory safety, and many LSP code paths; behavior change is intentional but wide in scope.
Overview
Adds lazy disk loading for LSP documents so clients that never send
didOpen(e.g. Claude Code) can still run definition, hover, references, completion, and related handlers.DocumentStore.GetOrLoadreadsfile://URIs from disk, marks them transient, and keeps them in an LRU (default cap 50, tunable viamaxTransientDocumentsininitializationOptions).didOpenbuffers stay editor-owned: not counted, not evicted;Setpromotes a transient entry and drops LRU tracking.GetTreenow returns areleasecallback and uses refcounted tree-sitter trees so LRU eviction cannot free C memory while another handler is walking the AST (fixes a use-after-free).GetIfOpendistinguishes real editor buffers from transient cache forreadFileText/getFileLine. Most read-only handlers switched fromGettoGetOrLoad; formatting still usesGetonly. README documents the new option; broad tests cover LRU, promotion, and concurrent eviction.Reviewed by Cursor Bugbot for commit 7cf2266. Bugbot is set up for automated code reviews on this repo. Configure here.