fix: robust MCP startup root resolution and deferred scan fallback#500
fix: robust MCP startup root resolution and deferred scan fallback#500idea404 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 265de894e3
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Fix codedb mcp startup fragility when launched by MCP clients (opencode, Claude Code, etc.) from an unexpected cwd or without usable roots. The server entered scan=loading_snapshot with files=0 and never recovered. Root resolution order: 1. Explicit path: codedb mcp /path or codedb /path mcp 2. CODEDB_ROOT env var 3. Git root from cwd (new: git rev-parse --show-toplevel) 4. MCP client roots via roots/list handshake 5. cwd fallback (after 3s timeout) Changes: - src/main.zig: parse mcp [path], --help/-h, reject unknown flags (--snapshot etc), git root detection, flush before exit, terminal ready state when no indexable root available (with race guard: check ds.triggered before marking ready to avoid premature completion when a roots response fires the scan just before timeout) - src/mcp.zig: trigger cwd fallback on roots/list error, missing result, or malformed roots array - src/git.zig: add getGitRoot() for git repo root detection Tests: 7 new unit tests, 4 new E2E scenarios (13 tests). All 30/30 E2E tests pass. 626/626 unit tests pass. Closes justrach#384, justrach#371, justrach#278
265de89 to
35cc057
Compare
|
Good catch — fixed in 35cc057. The issue was that
The original code treated both the same way, prematurely marking Fix: Check
If Added a regression test ( All 30/30 E2E tests and 626/626 unit tests pass. |
|
Hey @idea404, thanks for the very thorough PR — fully verified locally and security-clean. Wanted to flag something + ask how you'd like to proceed. StatusMost of what this PR fixes shipped in v0.2.5821 (merged via #509 / #510). The 2026-05-28 release closed #502 along with six other issues triaged the same day. The MCP arg-parser overhaul, What's genuinely additive in this PRYour
My version only handled the post-timeout cwd fallback in How would you like to proceed?
Either way, thanks for the careful test coverage — the new e2e scenarios are nice; we may want to keep S6 ("empty roots/list falls back to cwd") and S7 ("stdout stays clean") in some form. |
Linked Issues
Closes #502
Related (already closed): #384, #371, #278
Summary
codedb mcpwas fragile when launched by MCP clients (opencode, Claude Code, etc.) from an unexpected cwd or without usable roots. The server enteredscan=loading_snapshotwithfiles=0and never recovered, producing a functionally empty MCP server.codedb mcp --helpalso started the server instead of printing help, and unknown flags like--snapshotwere silently ignored.Root Resolution Order (after fix)
codedb mcp /path/to/repoorcodedb /path mcpCODEDB_ROOTenv vargit rev-parse --show-toplevel)roots/listhandshakeFiles Changed
src/git.ziggetGitRoot()function (+24 lines)src/main.zig--help,[path], flag rejection), git root detection, flush before exit, terminal ready state when no indexable root (+65 lines)src/mcp.zigroots/listerror, missing result, or malformed roots array (+40 lines)src/test_mcp.zigscripts/e2e_mcp_test.pyTotal: 506 lines (over 500-line guideline due to comprehensive test coverage)
Tests Run
Before (failing behavior)
After (fixed behavior)
Non-Regression
codedb <root> mcp(positional root before command) still works as beforeRebase
Rebased onto current
upstream/main(commit6317fe8).Generated Files
No generated files committed.
codedb.snapshotwas restored to its original state.Confirmation
This submission matches
CONTRIBUTING.mdrequirements:main