Open
fix: keep one-shot CLI queries read-only #4
fix: keep one-shot CLI queries read-only #4
Open
AbssyLanAdd on Sep 1
Total: 3 files changed+91-3
@@ -103,7 +103,7 @@ class ProjectBrainServer:
103 self._atexit_registered = False103 self._atexit_registered = False
104 self._continuous_monitoring_active = False104 self._continuous_monitoring_active = False
105 self._capabilities = None105 self._capabilities = None
106- self._read_only = False106+ self._read_only: bool = False
107 self._closed = False107 self._closed = False
108 # Tool implementations: name -> sync callable. Populated by108 # Tool implementations: name -> sync callable. Populated by
109 # _register_tools. CLI/REPL calls these directly; MCP async wrappers109 # _register_tools. CLI/REPL calls these directly; MCP async wrappers
@@ -119,13 +119,21 @@ class ProjectBrainServer:
119 if self._initialized:119 if self._initialized:
120 # A read-only session must never start the watcher, even if a120 # A read-only session must never start the watcher, even if a
121 # tool function requests it with the default start_watcher=True.121 # tool function requests it with the default start_watcher=True.
122- if not self._read_only and start_watcher and not self._continuous_monitoring_active:122+ if (
123+ not read_only
124+ and not self._read_only
125+ and start_watcher
126+ and not self._continuous_monitoring_active
127+ ):
123 self._start_watcher(continuous=True)128 self._start_watcher(continuous=True)
124 return129 return
125- self._read_only = read_only
126 self._initialize(start_watcher, read_only=read_only)130 self._initialize(start_watcher, read_only=read_only)
127 131 
128 def _initialize(self, start_watcher: bool, *, read_only: bool = False) -> None:132 def _initialize(self, start_watcher: bool, *, read_only: bool = False) -> None:
133+ # Persist the mode on the server instance. Registered tool functions
134+ # call _ensure_initialized() with write-mode defaults, so the original
135+ # CLI/REPL read-only decision must survive those later fast-path calls.
136+ self._read_only = read_only
129 pb_dir = self.project_path / ".projectbrain" / "graph"137 pb_dir = self.project_path / ".projectbrain" / "graph"
130 pb_dir.mkdir(parents=True, exist_ok=True)138 pb_dir.mkdir(parents=True, exist_ok=True)
131 139 
@@ -133,6 +133,7 @@ def test_repl_recovers_from_unclosed_quote(monkeypatch, tmp_path, capsys):
133 133 
134 def _ensure_initialized(self, start_watcher=False, read_only=False):134 def _ensure_initialized(self, start_watcher=False, read_only=False):
135 assert start_watcher is False135 assert start_watcher is False
136+ assert read_only is True
136 137 
137 from project_brain.mcp import server as server_module138 from project_brain.mcp import server as server_module
138 139 
@@ -152,6 +153,41 @@ def test_repl_recovers_from_unclosed_quote(monkeypatch, tmp_path, capsys):
152 assert "Traceback" not in captured.out + captured.err153 assert "Traceback" not in captured.out + captured.err
153 154 
154 155 
156+def test_one_shot_query_initializes_server_in_read_only_mode(
157+ monkeypatch, tmp_path, capsys
158+):
159+ initialization_calls = []
160+ 
161+ class FakeServer:
162+ tool_names = {"locate_symbol"}
163+ 
164+ def __init__(self, _project_path):
165+ pass
166+ 
167+ def _ensure_initialized(self, start_watcher=True, read_only=False):
168+ initialization_calls.append((start_watcher, read_only))
169+ 
170+ def call_tool_sync(self, tool_name, kwargs):
171+ assert tool_name == "locate_symbol"
172+ assert kwargs == {"symbol_name": "OrderService"}
173+ return {"symbols": [], "freshness": {"status": "degraded"}}
174+ 
175+ from project_brain.mcp import server as server_module
176+ 
177+ monkeypatch.setattr(server_module, "ProjectBrainServer", FakeServer)
178+ args = SimpleNamespace(
179+ command="locate",
180+ symbol_name="OrderService",
181+ kind=None,
182+ json=True,
183+ )
184+ 
185+ cli._cmd_query(tmp_path, args)
186+ 
187+ assert initialization_calls == [(False, True)]
188+ assert '"symbols": []' in capsys.readouterr().out
189+ 
190+ 
155def test_human_diff_output_includes_payload_and_truncation():191def test_human_diff_output_includes_payload_and_truncation():
156 rendered = format_result(192 rendered = format_result(
157 "get_change_diff",193 "get_change_diff",
@@ -87,6 +87,50 @@ def test_server_tools_registered(sample_project):
87 assert server.mcp is not None87 assert server.mcp is not None
88 88 
89 89 
90+def test_read_only_initialization_survives_default_tool_initialization(
91+ sample_project, monkeypatch
92+):
93+ """CLI/REPL queries must not reconcile when a tool initializes again."""
94+ server = ProjectBrainServer(str(sample_project))
95+ watcher_starts = []
96+ monkeypatch.setattr(
97+ server,
98+ "_start_watcher",
99+ lambda *, continuous: watcher_starts.append(continuous),
100+ )
101+ 
102+ server._ensure_initialized(start_watcher=False, read_only=True)
103+ result = json.loads(
104+ server.call_tool_sync(
105+ "locate_symbol",
106+ {"symbol_name": "OrderService"},
107+ )
108+ )
109+ 
110+ assert server._read_only is True
111+ assert result["symbols"]
112+ assert watcher_starts == []
113+ 
114+ 
115+def test_explicit_read_only_fast_path_never_starts_watcher(
116+ sample_project, monkeypatch
117+):
118+ """A read-only request must be honored even on a write-mode instance."""
119+ server = ProjectBrainServer(str(sample_project))
120+ server._initialized = True
121+ server._read_only = False
122+ watcher_starts = []
123+ monkeypatch.setattr(
124+ server,
125+ "_start_watcher",
126+ lambda *, continuous: watcher_starts.append(continuous),
127+ )
128+ 
129+ server._ensure_initialized(start_watcher=True, read_only=True)
130+ 
131+ assert watcher_starts == []
132+ 
133+ 
90def test_keyword_search(sample_project):134def test_keyword_search(sample_project):
91 server = ProjectBrainServer(str(sample_project))135 server = ProjectBrainServer(str(sample_project))
92 server._ensure_initialized()136 server._ensure_initialized()