codebase-memory-mcp icon indicating copy to clipboard operation
codebase-memory-mcp copied to clipboard

MCP session serves a stale graph indefinitely after the background watcher re-indexes

Open melihemreguler opened this issue 4 months ago • 7 comments

Summary

The background watcher correctly detects a change and re-indexes, and the database is updated within ~15 s. But the live MCP session that registered the watcher keeps serving the pre-change graph indefinitely. Only an index_repository call issued from that same session refreshes its view.

Effect: in a long-lived agent session the knowledge graph silently freezes at session start while the on-disk graph moves on.

Repro

  1. Start the MCP server with cwd set to a git repo, so the session registers the project (auto_watch default true).
  2. Send initialize, then tools/call index_repository.
  3. Record search_graph(label=Function) — baseline.
  4. Append a new function to a source file. Do not call index_repository.
  5. Every 15 s, query search_graph over the live MCP session and run codebase-memory-mcp cli search_graph from a separate process.

Observed

+ 15s  MCP: stale     [func_alpha_renamed, func_epsilon, func_gamma, main]
       CLI: HAS ZETA  [func_alpha_renamed, func_epsilon, func_gamma, func_zeta, main]
...
+120s  MCP: stale     (unchanged for the full run)
       CLI: HAS ZETA  (correct since +15s)

Server stderr during the run confirms the watcher did its job:

level=info msg=watcher.changed project=… strategy=git
level=info msg=watcher.reindex project=… path=…

Then, in the same session:

tools/call index_repository  ->  search_graph immediately returns func_zeta

Diagnosis

The session's read view appears to be invalidated only by writes originating in that session. Writes committed by the daemon-owned watcher do not invalidate it.

Impact

This is a silent-staleness failure, the most damaging class for an agent-facing tool. Over a normal working day the agent answers "who calls X" from a morning snapshot with no indication the answer is stale, and the watcher's work is invisible to the very session that registered it.

Notes

  • Explicit re-index correctness is fine. Adding, renaming and deleting functions and files, committed and uncommitted, was verified across 8 scenarios with no ghost nodes and no drift.
  • Incremental re-index costs ~0.2 s on a 796-file repo, so calling index_repository after edits is a viable workaround.
  • Secondary observation, possibly by design but undocumented: the watcher registers only the session's cwd project. A repo indexed explicitly via index_repository from a session whose cwd is elsewhere is never watched — the log shows watcher.watch for the cwd project instead.

Environment

  • Version: codebase-memory-mcp 0.9.0 (release binary, darwin-arm64)
  • OS: macOS 26.5.2, Apple Silicon
  • Repo under test: DeusData/codebase-memory-mcp @ af3ffbd2 (21,143 nodes / 123,103 edges)
  • Verified identically through both the MCP server and cli mode (outputs byte-identical)

melihemreguler avatar Jul 27 '26 07:07 melihemreguler

Thanks for the clean watcher/session split. The code supports the reported stale-view mechanism: resolve_store caches the SQLite connection for the current project and returns it unchanged on every subsequent query (src/mcp/mcp.c:1970-1984). A watcher publishing a new database generation does not invalidate that per-session handle, while an index_repository call in the same session naturally crosses the mutation path and refreshes state.

That is a silent correctness failure for long-lived sessions, so I have routed it to 0.9.1-rc at priority/high. The proof that a same-session reindex refreshes the view is particularly useful for binding the fix. Thank you.

DeusData avatar Jul 28 '26 18:07 DeusData

Thank you, @melihemreguler! Separating the watcher from the session, and showing that a same-session reindex refreshes the view, made this unambiguous. It also explains why your 15-second polling never recovered.

A session drops its cached database handle only when it runs index_repository itself, or after 60 seconds without any query. So a session that keeps asking questions keeps reading the old graph. Until the fix lands, pausing queries for a minute or calling index_repository explicitly works around it.

The fix is to check, whenever the cached handle is reused, whether the project's database was republished, and to reopen it if so. It's a small change and high priority.

DeusData avatar Sep 26 '26 04:09 DeusData

A correction to my previous comment, @melihemreguler; I'm sorry for the confusion. I described old behaviour. Current releases no longer keep the database handle between queries. Since v0.9.1-rc.1 (0e00ef57), every tool call opens the currently published database and closes it when the call ends. A session therefore sees a watcher or CLI re-index on its very next query, without an idle period and without calling index_repository itself. The workaround I suggested isn't needed.

We reproduced your scenario on current main: one open session, a CLI re-index from another process that adds func_zeta, and the session's very next query returns it. We've also added a regression test that fails if the handle is ever cached across requests again. Your report was on 0.9.0. If you still see staleness on v0.11.0, please tell us the version and we'll dig in.

DeusData avatar Sep 26 '26 04:09 DeusData

Thanks for the report! To move this forward we need a bit more so we can reproduce it ourselves:

  • the codebase-memory-mcp --version you're on
  • the exact steps or command you ran
  • a public repo (or a small dummy snippet) that shows the problem — please don't paste proprietary code

Once that's here we'll pick it straight back up. Heads-up: issues left awaiting-reporter are automatically closed after a few weeks of silence, but a comment reopens the door anytime.

github-actions[bot] avatar Sep 27 '26 02:09 github-actions[bot]

Thank you again for the session-versus-watcher reproduction. The final September correction supersedes the earlier cached-handle explanation and workaround: the requested follow-up is whether you still see this on v0.11.0. I have no new contrary result or reason to ask you to repeat the original 0.9.0 run.

DeusData avatar Oct 19 '26 22:10 DeusData

Thank you again for the watcher/session comparison. The final September correction supersedes the old workaround; the remaining check is whether staleness persists on the requested newer version.

DeusData avatar Oct 25 '26 17:10 DeusData

This issue has been waiting on more information for 21 days (a version, exact steps, or a public repro), so it's now marked stale. It will not be closed automatically — a maintainer reviews stale issues by hand. Adding a comment with the details clears the label and puts it straight back in the queue. We're happy to pick it back up the moment we can reproduce it.

github-actions[bot] avatar Nov 17 '26 02:11 github-actions[bot]