Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(sessions): keep a matched rg path even at the repo root (#2639)

Younes Berianeeb29cdaef7d9
diff --git a/packages/core/src/repowise/core/sessions/adapters/codex.py b/packages/core/src/repowise/core/sessions/adapters/codex.py
index 385c8eb8e..efcffa669 100644
--- a/packages/core/src/repowise/core/sessions/adapters/codex.py
+++ b/packages/core/src/repowise/core/sessions/adapters/codex.py
@@ -394,6 +394,21 @@ def _mcp_search_results(output: str) -> list[dict[str, str]]:
return [r for r in results if isinstance(r, dict)] if isinstance(results, list) else []
+def _looks_like_a_bare_path(candidate: str) -> bool:
+ """Whether a bare (no ``:line:``) line from ``rg --files``/``rg -l`` is a path.
+
+ A real path never has whitespace in it, which stray prose like "No files
+ were searched" does. A path with no separator can still be a real,
+ root-level filename (``setup.py``), so a ``.`` after the first character
+ admits it too without opening the door to bare words like "TODO".
+ """
+ if not candidate or any(char.isspace() for char in candidate):
+ return False
+ if "/" in candidate or "\\" in candidate:
+ return True
+ return "." in candidate[1:]
+
+
def _rewrite_search_output(output: Any) -> dict | None:
"""``rg`` output as the MCP ``search_codebase`` result shape, or None.
@@ -404,7 +419,11 @@ def _rewrite_search_output(output: Any) -> dict | None:
Only the leading path of an ``rg`` line becomes a file. ``rg`` prints
``path:line:text`` for a content match, and taking the whole line would
- hand the decision miner a "file" that does not exist.
+ hand the decision miner a "file" that does not exist. A matched path is
+ kept unconditionally, even with no separator (a root-level match like
+ ``app.py:12:``): the ``:line:`` suffix already confirms it is a path, not
+ prose. Only a bare, unmatched line needs the separator-or-extension
+ filter, since that is the only case where prose could be mistaken for one.
"""
if not isinstance(output, list):
return None
@@ -421,9 +440,10 @@ def _rewrite_search_output(output: Any) -> dict | None:
if not line or line.startswith(_EXEC_NOISE):
continue
match = _RG_MATCH_RE.match(line)
- candidate = match.group("path") if match else line
- if "/" in candidate or "\\" in candidate:
- results.append({"file": candidate.replace("\\", "/")})
+ if match:
+ results.append({"file": match.group("path").replace("\\", "/")})
+ elif _looks_like_a_bare_path(line):
+ results.append({"file": line.replace("\\", "/")})
return {"result": {"results": results}}
diff --git a/tests/unit/sessions/test_codex_adapter.py b/tests/unit/sessions/test_codex_adapter.py
index 2f03ddee7..82e5c070a 100644
--- a/tests/unit/sessions/test_codex_adapter.py
+++ b/tests/unit/sessions/test_codex_adapter.py
@@ -313,6 +313,44 @@ def test_a_content_match_binds_the_path_not_the_matched_line(adapter: CodexAdapt
assert results == [{"file": "src/app.py"}]
+def test_a_root_level_content_match_still_binds_its_path(adapter: CodexAdapter) -> None:
+ """A ``path:line:`` match with no separator in the path is still a path."""
+ call_event = adapter.normalize(RG_CALL)
+ event = adapter.normalize(
+ _search_then_output(
+ [{"type": "input_text", "text": "Exit code: 0\nOutput:\napp.py:12: # TODO\n"}]
+ )
+ )
+
+ assert event is not None
+ results = json.loads(event.tool_results[0].content)["result"]["results"]
+ assert results == [{"file": "app.py"}]
+ assert call_event is not None
+ assert call_event.tool_uses[0].input["path"] == "app.py"
+
+
+def test_a_bare_root_level_filename_is_kept(adapter: CodexAdapter) -> None:
+ """``rg --files``/``rg -l`` output with no ``:line:`` still admits a root file."""
+ adapter.normalize(RG_CALL)
+ event = adapter.normalize(_search_then_output([{"type": "input_text", "text": "Output:\nsetup.py\n"}]))
+
+ assert event is not None
+ results = json.loads(event.tool_results[0].content)["result"]["results"]
+ assert results == [{"file": "setup.py"}]
+
+
+def test_a_bare_prose_line_is_still_dropped(adapter: CodexAdapter) -> None:
+ """Loosening the bare-line filter must not admit stray prose as a file."""
+ adapter.normalize(RG_CALL)
+ event = adapter.normalize(
+ _search_then_output([{"type": "input_text", "text": "Output:\nNo files were searched\n"}])
+ )
+
+ assert event is not None
+ results = json.loads(event.tool_results[0].content)["result"]["results"]
+ assert results == []
+
+
def test_unreadable_search_output_neither_raises_nor_erases(adapter: CodexAdapter) -> None:
"""``normalize`` may not raise on content, and must not drop a real result."""
adapter.normalize(RG_CALL)