fix: lazy SQLite corruption recovery in MCP ToolHandler (Issue #679)#683
Merged
Conversation
When CLI `zcodegraph index` rebuilds the database file, the MCP server's long-lived CodeGraph instance holds a stale SQLite handle. Subsequent tool calls fail with 'database disk image is malformed'. Fix: detect corruption errors in ToolHandler.execute()'s catch block, reopen the database connection via CodeGraph.reopen(), and retry the tool call exactly once. TDD vertical slices: - isSqliteCorruptionError(): match malformed/SQLITE_CORRUPT/file is not a database - CodeGraph.reopen(): public method, reuses existing reopenDatabaseAfterExternalIndex() - ToolHandler.execute(): refactored to executeOnce() + retry wrapper - 9 tests covering detection, reopen, retry success, non-corruption skip, and no-double-retry guard Existing MCP tests: 23/23 pass (no regression).
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.
Issue
Fixes #679 — MCP server holds stale SQLite connection after CLI
zcodegraph indexrebuilds the database, causingdatabase disk image is malformederrors.Root Cause
CLI
zcodegraph indexrebuilds the.codegraph/codegraph.dbfile. The MCP server's long-livedCodeGraphinstance still holds the old SQLite handle → stale → corruption error on next tool call.Fix: Lazy Detection + Reopen + Retry (Option A)
isSqliteCorruptionError()matchesmalformed/SQLITE_CORRUPT/file is not a databaseCodeGraph.reopen()— public method, reuses existingreopenDatabaseAfterExternalIndex()logicToolHandler.execute()refactored toexecuteOnce()+ retry wrapper; reopens DB and retries exactly once on corruptionTDD Vertical Slices
isSqliteCorruptionErrormatches 3 patterns, rejects non-corruption, handles non-Errorsrc/db/error-detection.tsCodeGraph.reopen()serves queries after reopensrc/index.ts— publicreopen()src/mcp/tools.ts—execute()+executeOnce()Files Changed
src/db/error-detection.ts— new:isSqliteCorruptionError(err: unknown): booleansrc/index.ts— new:CodeGraph.reopen()public methodsrc/mcp/tools.ts— refactored:execute()→executeOnce()+ retry wrapper__tests__/malformed-recovery.test.ts— 9 TDD testsTests
npx vitest run __tests__/malformed-recovery.test.ts— 9/9 passnpx vitest run __tests__/mcp-*.test.ts— 23/23 pass (no regression)