fix: reopen DB connection when its file is replaced at the same path (#925)#929
Closed
Sway-Chan wants to merge 1 commit into
Closed
fix: reopen DB connection when its file is replaced at the same path (#925)#929Sway-Chan wants to merge 1 commit into
Sway-Chan wants to merge 1 commit into
Conversation
…ry#925) After a project dir is removed and recreated at the same path (a `git worktree remove`+`add`, or a fresh `codegraph init`), a long-lived MCP server kept reading the old, now-unlinked inode while init/sync wrote to the new one — serving a stale snapshot for the life of the daemon, with `codegraph sync` unable to refresh it. DatabaseConnection records its DB file's (dev, ino) at open and exposes isFileReplaced(); CodeGraph surfaces it as isDbReplaced(). Detection runs at the per-tool-call chokepoint, ToolHandler.getCodeGraph: - cross-project (projectPath) cache hits evict + close a replaced entry so the next query reopens against the new inode; - the default project is reopened via a hook the MCPEngine registers (it owns the default instance's watcher + catch-up), driven from the default-serving path rather than the cold init methods, which a loaded daemon never re-enters. A missing file (mid-recreate) is ignored so a transient gap never churns the connection.
c0c6b43 to
4761346
Compare
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.
Problem
Fixes #925. After a project directory is removed and recreated at the same path — a
git worktree remove+git worktree add, orrm -rf .codegraph && codegraph init— a long-lived MCP server keeps serving a stale snapshot for the life of the daemon:…/.codegraph/codegraph.dband holds that file descriptor.init/syncwrites a new inode at the same path.codegraph syncwrites the new one — they never meet, so search returns deleted symbols and misses new ones until the process is restarted.Fix
Detect the inode swap and reopen, encapsulated at the connection layer and triggered at the per-tool-call chokepoint:
DatabaseConnectionrecords its DB file's(dev, ino)at open and exposesisFileReplaced()— true only when the path now resolves to a different inode. A missing file (mid-recreate) returns false, so a transient gap never churns the connection.CodeGraph.isDbReplaced()surfaces it.ToolHandler.getCodeGraph()is the one place every tool call resolves an instance, so the check lives there:projectPath) cache hits route throughliveCachedGraph(), which evicts + closes a replaced entry so the next query reopens against the new inode via the existing open path;MCPEngineregisters (setDefaultReloadHook). The engine owns the default instance's watcher + catch-up sync, so the reopen is delegated to it — reusing the existing close →openSync→ watch → catch-up recovery, opening the replacement before closing the stale handle so a failed reopen leaves the current connection in place.The earlier draft only checked on the cold init methods (
ensureInitialized/retryInitializeSync), but a loaded daemon never re-enters those —session.tsand the in-process proxy both short-circuit once a default is loaded — so the default-project case (the headline scenario) would not have fired at runtime. Driving it fromgetCodeGraphfixes that. The check is onefs.statSyncper tool call, between requests, so it never races an in-flight query.Testing
New
__tests__/mcp-db-reopen.test.ts:DatabaseConnection.isFileReplaced()— false when unchanged, true after a same-path inode swap, false while the file is missing.CodeGraph.isDbReplaced()— flips to true after the project DB is recreated at the same path.ToolHandler.liveCachedGraph()— a replaced cached (cross-project) project is evicted and closed; an unchanged one is returned as-is.ToolHandler.getCodeGraph()default path — a swapped default project fires the engine reload hook and serves the fresh instance.tsc --noEmitclean; full test suite green (1580 passed).