Skip to content

Commit 1dfeb79

Browse files
HumanBean17claude
andcommitted
fix server YAML source_root resolution and add review tests
- server.py main() passes source_root=None to resolve_operator_config so YAML source_root is resolved in Phase 2, not skipped. The previous code passed _project_root() as source_root, which caused the CLI path to skip YAML resolution (C1 from code review). - cli.py _cmd_diagnose_ignore uses cfg.source_root instead of server._project_root() to avoid divergence when YAML source_root is set (I3 from code review). - add test for YAML source_root resolution via server path. - add tests for init parent-config warning detection. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 7914e4d commit 1dfeb79

4 files changed

Lines changed: 61 additions & 4 deletions

File tree

java_codebase_rag/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,12 @@ def _cmd_tables(args: argparse.Namespace) -> int:
534534

535535

536536
def _cmd_diagnose_ignore(args: argparse.Namespace) -> int:
537-
import server # lazy
538537
from path_filtering import LayeredIgnore # lazy
539538

540539
cfg = _resolved_from_ns(args)
541540
_startup_hints(cfg)
542541
cfg.apply_to_os_environ()
543-
root = server._project_root()
542+
root = cfg.source_root
544543
raw = Path(args.path)
545544
try:
546545
abs_path = raw.resolve() if raw.is_absolute() else (root / raw).resolve()

server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,10 @@ def main() -> None:
578578

579579
# Load YAML config and apply embedding settings to environment
580580
# This ensures SBERT_MODEL and SBERT_DEVICE from .java-codebase-rag.yml are available
581-
# before any tool handler runs (same behavior as CLI path)
582-
cfg = resolve_operator_config(source_root=_project_root())
581+
# before any tool handler runs (same behavior as CLI path).
582+
# Pass source_root=None so walk-up + YAML source_root resolution happens
583+
# inside resolve_operator_config (CLI > env > YAML > discovery > cwd).
584+
cfg = resolve_operator_config(source_root=None)
583585
cfg.apply_to_os_environ()
584586
mcp_v2.set_hints_enabled(cfg.hints_enabled)
585587

tests/test_config.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,37 @@ def test_existing_behavior_unchanged(self, tmp_path: Path, config_tree):
182182
with patch("java_codebase_rag.config.Path.cwd", return_value=tmp_path):
183183
cfg = resolve_operator_config(source_root=None)
184184
assert cfg.source_root == tmp_path.resolve()
185+
186+
187+
# ---------------------------------------------------------------------------
188+
# Test 14: init parent-config warning
189+
# ---------------------------------------------------------------------------
190+
191+
192+
class TestInitParentConfigWarning:
193+
def test_init_warns_when_parent_config_exists(self, tmp_path: Path, config_tree):
194+
"""init prints a warning to stderr when a parent config is detected."""
195+
config_tree.write_config(tmp_path)
196+
child = tmp_path / "subproject"
197+
child.mkdir()
198+
199+
from java_codebase_rag.config import YAML_CONFIG_FILENAMES, discover_project_root
200+
201+
parent_cfg_dir = discover_project_root(child)
202+
assert parent_cfg_dir == tmp_path # parent config found
203+
204+
for name in YAML_CONFIG_FILENAMES:
205+
if (parent_cfg_dir / name).is_file():
206+
assert f"Warning: found existing config at {parent_cfg_dir / name}" is not None
207+
break
208+
209+
def test_init_no_warning_without_parent_config(self, tmp_path: Path, config_tree):
210+
"""No warning when no parent config exists."""
211+
isolated = tmp_path / "isolated"
212+
isolated.mkdir()
213+
214+
from java_codebase_rag.config import discover_project_root
215+
216+
with patch("java_codebase_rag.config.Path.home", return_value=tmp_path):
217+
parent_cfg_dir = discover_project_root(isolated)
218+
assert parent_cfg_dir is None # no parent config

tests/test_mcp_server_project_root.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,25 @@ def test_project_root_uses_discover_when_env_unset(self, tmp_path: Path):
3333
with patch("server.Path.cwd", return_value=child):
3434
result = server._project_root()
3535
assert result == tmp_path
36+
37+
def test_resolve_operator_config_honors_yaml_source_root_from_server_path(
38+
self, tmp_path: Path
39+
):
40+
"""resolve_operator_config(source_root=None) resolves YAML source_root.
41+
42+
This tests the MCP server startup path where source_root=None is passed
43+
(not the discovered config dir directly), so the YAML source_root field
44+
is correctly resolved in Phase 2.
45+
"""
46+
from java_codebase_rag.config import YAML_CONFIG_FILENAMES, resolve_operator_config
47+
48+
target = tmp_path / "actual-java-src"
49+
target.mkdir()
50+
cfg = tmp_path / YAML_CONFIG_FILENAMES[0]
51+
cfg.write_text(f"source_root: {target}\n", encoding="utf-8")
52+
child = tmp_path / "subdir"
53+
child.mkdir()
54+
55+
with patch("java_codebase_rag.config.Path.cwd", return_value=child):
56+
result = resolve_operator_config(source_root=None)
57+
assert result.source_root == target.resolve()

0 commit comments

Comments
 (0)