From 65ba0de0b812637cd4feab1ceb2a05ae1772f6d4 Mon Sep 17 00:00:00 2001 From: Jenner Torrence Date: Sat, 21 Nov 2026 21:18:37 -0500 Subject: [PATCH] fix(sync): watcher sweep reconciles a new committed change again (#163) #155 bound sweep drift to the commit it was computed against and refused it as `sweep_drift_stale` when the swept commit differed from either HEAD or the indexed commit. A commit the sweep finds at HEAD that nothing has indexed yet differs from the indexed commit by definition, so it was refused on every sweep and never reached the ordinary incremental sync the repair falls through to when HEAD has moved. Drop the swept-vs-indexed staleness condition. A sweep that saw the current HEAD now reaches the existing "not a targeted repair" branch, which runs the ordinary sync (indexed..HEAD change set) and never applies the sweep's path list to the older generation. HEAD moving after the sweep, or the published generation changing, is still refused. Adds a regression test for the branch and puts tests/test_v13_data_reconciliation.py in the protected agent gate. Fixes Consiliency/Code-Index-MCP#163 Co-Authored-By: Claude Opus 5.5 --- Makefile | 1 + mcp_server/storage/git_index_manager.py | 10 ++--- tests/test_issue_145_rebuild_decisions.py | 50 +++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 6987cda0..b1956c41 100644 --- a/Makefile +++ b/Makefile @@ -357,6 +357,7 @@ agent-gate-local: agent-fast-local tests/test_ref_poller_edges.py \ tests/test_rename_atomicity.py \ tests/test_artifact_download.py \ + tests/test_v13_data_reconciliation.py \ -q --no-cov agent-full-local: agent-gate-local coverage release-smoke-container diff --git a/mcp_server/storage/git_index_manager.py b/mcp_server/storage/git_index_manager.py index 378258c6..287b4a0e 100644 --- a/mcp_server/storage/git_index_manager.py +++ b/mcp_server/storage/git_index_manager.py @@ -671,11 +671,11 @@ def _repair_repository_drift_locked( stale.append( f"HEAD moved from {observed_commit[:12]} to {(current_commit or '?')[:12]}" ) - if observed_commit is not None and observed_commit != last_indexed_commit: - stale.append( - f"indexed commit is {(last_indexed_commit or 'none')[:12]}, " - f"not the swept {observed_commit[:12]}" - ) + # A swept commit that is the current HEAD but not the indexed commit + # is not stale: the sweep found a committed change that nothing has + # indexed yet. The drift paths are not applied to the older + # generation; the ordinary incremental sync below computes the change + # set from the indexed commit to HEAD (#163). if observed_index_path is not None and ( not repo_info.index_path or Path(repo_info.index_path).resolve(strict=False) diff --git a/tests/test_issue_145_rebuild_decisions.py b/tests/test_issue_145_rebuild_decisions.py index 9e173837..305db3d6 100644 --- a/tests/test_issue_145_rebuild_decisions.py +++ b/tests/test_issue_145_rebuild_decisions.py @@ -404,6 +404,56 @@ def spy(repo_id_arg, ctx, changes, *args, **kwargs): assert holder["sweeper"].sweep_once() == [] +def test_sweep_of_an_unindexed_head_commit_runs_the_incremental_sync( + runtime, monkeypatch, caplog # noqa: F811 +): + """A committed change the sweep finds at HEAD is synced, not refused (#163). + + The sweep lists HEAD B while the published generation is still labelled + A. That drift is not stale: nothing has indexed B yet. The ordinary + incremental sync must publish B from the A..B change set, without a full + rebuild and without applying the sweep's path list to the A generation. + """ + repo, registry, repo_id, _store, manager = runtime + for index in range(6): + (repo / f"stable{index}.py").write_text(f"stable_value_{index} = {index}\n") + (repo / "changed.py").write_text("changed_old = 1\n") + commit_a = _commit_all(repo, "A") + assert manager.rebuild_repository_index(repo_id).action == "full_index" + generation_before = registry.get(repo_id).index_generation + + (repo / "changed.py").write_text("changed_new = 2\n") + commit_b = _commit_all(repo, "B edits changed.py") + assert registry.get(repo_id).last_indexed_commit == commit_a + + synced_paths = [] + real_incremental = manager._incremental_index_update + + def spy(repo_id_arg, ctx, changes, *args, **kwargs): + synced_paths.extend(changes.added + changes.modified + changes.deleted) + return real_incremental(repo_id_arg, ctx, changes, *args, **kwargs) + + monkeypatch.setattr(manager, "_incremental_index_update", spy) + _no_full_index(manager, monkeypatch) + store = manager._resolve_ctx(repo_id).sqlite_store + sweeper, marked = _sweeper_for(manager, registry, repo_id, store) + + with caplog.at_level(logging.INFO): + assert sweeper.sweep_once() == [repo_id] + + info = registry.get(repo_id) + assert info.last_indexed_commit == commit_b + assert info.index_generation != generation_before + assert synced_paths == ["changed.py"] + assert marked == [repo_id] + assert manager._resolve_ctx(repo_id).sqlite_store.search_code_fts("changed_new") + messages = [record.getMessage() for record in caplog.records] + assert not any("Not applying sweep drift" in m for m in messages) + assert any("running an ordinary sync" in m for m in messages) + # The published generation agrees with B: the next sweep is quiet. + assert sweeper.sweep_once() == [] + + def test_memory_release_is_best_effort(monkeypatch): assert isinstance(release_rebuild_memory("repo"), dict) monkeypatch.setenv("MCP_INDEX_MALLOC_TRIM", "false")