Skip to content

Commit 5ed7909

Browse files
committed
fixup! fix(ingest): tie each code graph to its root and prune graphs of deleted roots (#711, slice 1)
Route the root marker through gated_files.write_json (ADR-1110). Check unit, run 41925545304: test_storage_redaction_gate found a raw write_text in infrastructure/code_graph_cache.py. A marker altered by redaction is removed, so the graph stays unattributed instead of being pruned. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018kyZYt8uiGG3xcmvaug2eG
1 parent 46eb2ad commit 5ed7909

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎mcp_server/infrastructure/code_graph_cache.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import shutil
2525
from pathlib import Path
2626

27+
from mcp_server.shared import gated_files
28+
2729
logger = logging.getLogger(__name__)
2830

2931
ROOT_MARKER = "cortex-root.json"
@@ -44,14 +46,18 @@ def default_cache_dir() -> Path:
4446

4547

4648
def write_root_marker(graph_dir: str | Path, project_root: str | Path) -> None:
47-
"""Record ``project_root`` (resolved) as the root ``graph_dir`` was built from."""
49+
"""Record ``project_root`` (resolved) as the root ``graph_dir`` was built from.
50+
51+
The write goes through the redaction gate (ADR-1110). If redaction altered
52+
the path, the marker would name a root that does not exist and the graph
53+
would be pruned; the marker is removed instead, leaving it unattributed."""
4854
target = Path(graph_dir).expanduser()
49-
target.mkdir(parents=True, exist_ok=True)
5055
root = str(Path(project_root).expanduser().resolve())
51-
payload = json.dumps({"project_root": root}, indent=2) + "\n"
52-
tmp = target / (ROOT_MARKER + ".tmp")
53-
tmp.write_text(payload, encoding="utf-8")
54-
tmp.replace(target / ROOT_MARKER)
56+
marker = target / ROOT_MARKER
57+
gated_files.write_json(marker, {"project_root": root}, indent=2)
58+
if read_root_marker(target) != root:
59+
marker.unlink(missing_ok=True)
60+
logger.warning("root marker for %s was altered by redaction; removed", target)
5561

5662

5763
def read_root_marker(graph_dir: Path) -> str | None:

‎tests_py/infrastructure/test_code_graph_cache.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,23 @@ def test_prefix_sharing_project_is_not_touched(tmp_path: Path) -> None:
9898
def test_missing_cache_dir_is_empty_report(tmp_path: Path) -> None:
9999
report = cache.prune_orphan_graphs(tmp_path / "absent")
100100
assert report == cache.PruneReport((), (), ())
101+
102+
103+
def test_marker_altered_by_redaction_is_removed(tmp_path: Path, monkeypatch) -> None:
104+
"""A redacted root would name a missing path and get the graph pruned."""
105+
root = tmp_path / "proj"
106+
root.mkdir()
107+
graph_dir = tmp_path / "cache" / "proj-6666aaaa"
108+
graph_dir.mkdir(parents=True)
109+
real_write = cache.gated_files.write_json
110+
111+
def _redacting_write(path, data, **kwargs):
112+
return real_write(path, {"project_root": "REDACTED"}, **kwargs)
113+
114+
monkeypatch.setattr(cache.gated_files, "write_json", _redacting_write)
115+
cache.write_root_marker(graph_dir, root)
116+
117+
assert not (graph_dir / cache.ROOT_MARKER).exists()
118+
assert cache.prune_orphan_graphs(graph_dir.parent).unattributed == (
119+
"proj-6666aaaa",
120+
)

0 commit comments

Comments
 (0)