Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(answer): bound the greps a long question can trigger (#3016)
Raghav Chamadiya5638fb8b50ea
diff --git a/packages/core/src/repowise/core/analysis/decisions/accepter.py b/packages/core/src/repowise/core/analysis/decisions/accepter.pyindex 2e7f436d5..f49cc6bdb 100644--- a/packages/core/src/repowise/core/analysis/decisions/accepter.py+++ b/packages/core/src/repowise/core/analysis/decisions/accepter.py@@ -24,6 +24,7 @@ def _git_identity(repo_path: Path) -> str:["git", "config", "user.name"],cwd=repo_path,capture_output=True,+ stdin=subprocess.DEVNULL,text=True,timeout=5,check=False,@@ -65,6 +66,7 @@ def is_tracked(repo_path: Path | str, rel_path: str) -> bool:["git", "ls-files", "--error-unmatch", "--", rel_path],cwd=Path(repo_path),capture_output=True,+ stdin=subprocess.DEVNULL,text=True,timeout=10,check=False,diff --git a/packages/core/src/repowise/core/analysis/decisions/source_files.py b/packages/core/src/repowise/core/analysis/decisions/source_files.pyindex a9973d843..c12b8012c 100644--- a/packages/core/src/repowise/core/analysis/decisions/source_files.py+++ b/packages/core/src/repowise/core/analysis/decisions/source_files.py@@ -70,6 +70,7 @@ def tracked_files(repo_path: Path) -> set[Path] | None:proc = subprocess.run(["git", "-C", str(repo_path), "ls-files", "-z"],capture_output=True,+ stdin=subprocess.DEVNULL,timeout=30,check=False,)diff --git a/packages/core/src/repowise/core/analysis/history_scan.py b/packages/core/src/repowise/core/analysis/history_scan.pyindex d90e1137d..478cf56ba 100644--- a/packages/core/src/repowise/core/analysis/history_scan.py+++ b/packages/core/src/repowise/core/analysis/history_scan.py@@ -59,6 +59,7 @@ def _run_git(repo_path: Path, args: list[str], *, timeout: float = 30.0) -> str:["git", *args],cwd=str(repo_path),capture_output=True,+ stdin=subprocess.DEVNULL,text=True,# git writes utf-8. ``text=True`` alone decodes with the locale# codec, so on a default Windows install a commit subject holdingdiff --git a/packages/core/src/repowise/core/precedent/structural.py b/packages/core/src/repowise/core/precedent/structural.pyindex c708703ba..5cfeb1801 100644--- a/packages/core/src/repowise/core/precedent/structural.py+++ b/packages/core/src/repowise/core/precedent/structural.py@@ -397,6 +397,7 @@ def _formatter_drift(root: Path, _traverser: Any) -> list[Episode]:[executable, "format", "--check", "."],cwd=str(root),capture_output=True,+ stdin=subprocess.DEVNULL,text=True,timeout=FORMATTER_TIMEOUT_S,env={**os.environ, "NO_COLOR": "1"},@@ -480,6 +481,7 @@ def _head_commit(root: Path) -> str | None:["git", "rev-parse", "HEAD"],cwd=str(root),capture_output=True,+ stdin=subprocess.DEVNULL,text=True,timeout=_GIT_TIMEOUT_S,)diff --git a/packages/core/src/repowise/core/procutils.py b/packages/core/src/repowise/core/procutils.pyindex 4d3055048..d6193247e 100644--- a/packages/core/src/repowise/core/procutils.py+++ b/packages/core/src/repowise/core/procutils.py@@ -228,6 +228,7 @@ def _ps_field(pid: int, field: str) -> str | None:out = subprocess.run(["ps", "-p", str(pid), "-o", f"{field}="],capture_output=True,+ stdin=subprocess.DEVNULL,text=True,timeout=5,)diff --git a/packages/core/src/repowise/core/providers/llm/codex_cli.py b/packages/core/src/repowise/core/providers/llm/codex_cli.pyindex a04784709..4a2dd1999 100644--- a/packages/core/src/repowise/core/providers/llm/codex_cli.py+++ b/packages/core/src/repowise/core/providers/llm/codex_cli.py@@ -150,6 +150,8 @@ def _load_codex_model_catalog(codex_cmd: str) -> dict[str, CodexModelReasoning]capture_output=True,check=False,text=True,+ # Inside a stdio MCP server stdin is the JSON-RPC pipe; never share it.+ stdin=subprocess.DEVNULL,timeout=_CATALOG_TIMEOUT_SECONDS,)except (OSError, subprocess.SubprocessError):diff --git a/packages/core/src/repowise/core/providers/llm/opencode.py b/packages/core/src/repowise/core/providers/llm/opencode.pyindex 45bf2e8a6..8c571d5c1 100644--- a/packages/core/src/repowise/core/providers/llm/opencode.py+++ b/packages/core/src/repowise/core/providers/llm/opencode.py@@ -224,6 +224,8 @@ def _load_opencode_model_catalog(opencode_cmd: str) -> list[str] | None:capture_output=True,check=False,text=True,+ # Inside a stdio MCP server stdin is the JSON-RPC pipe; never share it.+ stdin=subprocess.DEVNULL,timeout=_CATALOG_TIMEOUT_SECONDS,)except (OSError, subprocess.SubprocessError):diff --git a/packages/server/src/repowise/server/mcp_server/_code_rationale.py b/packages/server/src/repowise/server/mcp_server/_code_rationale.pyindex a7dd28eb2..05364f974 100644--- a/packages/server/src/repowise/server/mcp_server/_code_rationale.py+++ b/packages/server/src/repowise/server/mcp_server/_code_rationale.py@@ -343,6 +343,12 @@ _CAP_FAMILY: tuple[str, ...] = ("cap", "limit", "max", "bound", "ceiling", "thre# Git-grep is bounded so a pathological pattern can't stall a tool response._GREP_TIMEOUT_S = 20+# A pasted issue body carries 100+ nouns and numbers; an uncapped alternation+# matches most comment lines in the repo. Longest nouns are the most specific:+# on sample issue bodies they ranked the target no worse than the uncapped or+# first-12 sets, and a minimum per-file count added nothing.+_MAX_GREP_NOUNS = 12+_MAX_GREP_NUMBERS = 4def grep_comment_candidates(@@ -372,9 +378,10 @@ def grep_comment_candidates(return []if not repo_root:return []- numbers = _salient_numbers(query)+ numbers = _salient_numbers(query)[:_MAX_GREP_NUMBERS]terms = _content_terms(query)- nouns = [t for t in terms if t not in _CAP_FAMILY]+ nouns = sorted((t for t in terms if t not in _CAP_FAMILY), key=lambda t: (-len(t), t))+ nouns = nouns[:_MAX_GREP_NOUNS]anchors = list(numbers) or list(_CAP_FAMILY)if not nouns or not anchors:return []@@ -405,7 +412,9 @@ def grep_comment_candidates(# ``color.ui=always`` git config must not wrap paths in ANSI# escapes (which would corrupt the path split below)."--no-color",- "-n",+ # -c: one ``path:count`` line per file, so the output is bounded+ # by the file count, not by how many lines a broad pattern hits.+ "-c","-I","-i","-E",@@ -432,9 +441,9 @@ def grep_comment_candidates(return []counts: Counter[str] = Counter()for line in proc.stdout.splitlines():- path = line.split(":", 1)[0]- if path:- counts[path] += 1+ path, _, n = line.rpartition(":")+ if path and n.isdigit():+ counts[path] += int(n)# ``git grep`` only scans tracked files, but a gitignored copy can still be# tracked (or land here via a future --no-index retry); filter the winners# through the repo's exclusion rules so an ignored path is never anchored on.diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/data_shape.py b/packages/server/src/repowise/server/mcp_server/tool_answer/data_shape.pyindex c39627192..2ea849500 100644--- a/packages/server/src/repowise/server/mcp_server/tool_answer/data_shape.py+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/data_shape.py@@ -121,6 +121,9 @@ def _neg_str(s: str) -> tuple[int, ...]:return tuple(-ord(c) for c in s)+_MAX_SHAPE_IDENTIFIERS = 4++def _specific_identifiers(question_ids: set[str]) -> list[str]:"""Keep only identifiers specific enough to ground a data shape on.@@ -134,7 +137,9 @@ def _specific_identifiers(question_ids: set[str]) -> list[str]:for q in question_idsif len(q) >= _DATA_SHAPE_MIN_IDENT_LEN and ("_" in q or any(c.isupper() for c in q))]- return sorted(kept, key=len, reverse=True)+ # Each one costs a whole-repo grep run one after another; a pasted issue+ # body names dozens. Name tie-break keeps the cut deterministic.+ return sorted(kept, key=lambda q: (-len(q), q))[:_MAX_SHAPE_IDENTIFIERS]# Pathspecs limiting the grep to source and excluding heavy/vendored dirs. Thediff --git a/tests/unit/server/mcp/test_answer_data_shape.py b/tests/unit/server/mcp/test_answer_data_shape.pyindex 9eb9aa741..3ae095e0e 100644--- a/tests/unit/server/mcp/test_answer_data_shape.py+++ b/tests/unit/server/mcp/test_answer_data_shape.py@@ -403,6 +403,22 @@ def enrich(rows_json, meta, src):assert "also_accessed" not in out+def test_identifier_fan_out_is_capped(tmp_path, monkeypatch):+ """A pasted body naming dozens of identifiers greps only the longest few."""+ from repowise.server.mcp_server.tool_answer import data_shape as ds++ grepped: list[str] = []++ def fake_grep(_root, identifier, _spec=None):+ grepped.append(identifier)+ return []++ monkeypatch.setattr(ds, "_grep_identifier_files", fake_grep)+ ids = {f"blob_{'x' * i}" for i in range(2, 30)}+ assert ds.mine_data_shape(tmp_path, ids) is None+ assert grepped == sorted(ids, key=len, reverse=True)[: ds._MAX_SHAPE_IDENTIFIERS]++def test_none_repo_root_is_safe():assert mine_data_shape(None, {"anything_json"}) is Nonediff --git a/tests/unit/server/mcp/test_code_rationale.py b/tests/unit/server/mcp/test_code_rationale.pyindex 11d170518..89828b914 100644--- a/tests/unit/server/mcp/test_code_rationale.py+++ b/tests/unit/server/mcp/test_code_rationale.py@@ -296,6 +296,47 @@ def test_grep_comment_candidates_skips_gitignored_file(tmp_path):assert "stale/leak.py" not in cands+def test_grep_comment_candidates_bounds_a_pasted_issue_body(tmp_path, monkeypatch):+ """A long issue body must not become an unbounded alternation or output."""+ from repowise.server.mcp_server import _code_rationale as cr++ repo = _git_repo(tmp_path, _CALLER_CAP_FILES)+ seen: list[list[str]] = []+ real_run = subprocess.run++ def spy(args, **kwargs):+ seen.append(list(args))+ return real_run(args, **kwargs)++ monkeypatch.setattr(cr.subprocess, "run", spy)+ filler = " ".join(f"longnoun{chr(97 + i % 26)}{chr(97 + i // 26)}" for i in range(60))+ numbers = " ".join(str(n) for n in range(100, 120))+ question = f"why is the caller list capped at 50 {filler} {numbers}"+ cands = grep_comment_candidates(str(repo), question)++ args = seen[0]+ pattern = args[args.index("-E") + 1]+ # Longest nouns are kept, so the 10-char filler crowds out "caller".+ assert pattern.count("longnoun") == 2 * cr._MAX_GREP_NOUNS+ assert "caller" not in pattern+ # The first numbers in question order: 50, 100, 101, 102.+ assert "102" in pattern and "103" not in pattern+ # Per-file counts, never one output line per matched source line.+ assert "-c" in args and "-n" not in args+ assert cands == []+++def test_grep_comment_candidates_ranks_by_per_file_count(tmp_path):+ repo = _git_repo(+ tmp_path,+ {+ "one.py": "# caller cap 50\n",+ "three.py": "# caller cap 50\n# caller cap 50 again\n# caller 50 here\n",+ },+ )+ assert grep_comment_candidates(str(repo), "caller capped at 50") == ["three.py", "one.py"]++def test_grep_comment_candidates_needs_a_noun(tmp_path):# Numbers but no content noun (all stopwords) -> nothing to anchor on.repo = _git_repo(tmp_path, _CALLER_CAP_FILES)diff --git a/tests/unit/server/mcp/test_subprocess_stdin.py b/tests/unit/server/mcp/test_subprocess_stdin.pynew file mode 100644index 000000000..5de2d6f81--- /dev/null+++ b/tests/unit/server/mcp/test_subprocess_stdin.py@@ -0,0 +1,43 @@+"""Every child process an MCP tool can spawn must not inherit stdin.++Inside a stdio MCP server stdin is the JSON-RPC pipe; a child that reads it+consumes the protocol stream.+"""++from __future__ import annotations++import ast+import importlib+from pathlib import Path++import pytest++_MODULES = (+ "repowise.server.mcp_server._code_rationale",+ "repowise.server.mcp_server.tool_answer.data_shape",+ "repowise.core.analysis.history_scan",+ "repowise.core.precedent.structural",+ "repowise.core.analysis.decisions.accepter",+ "repowise.core.analysis.decisions.source_files",+ "repowise.core.procutils",+ "repowise.core.providers.llm.codex_cli",+ "repowise.core.providers.llm.opencode",+)+_SPAWNERS = {"run", "Popen", "check_output", "check_call", "call"}++[email protected]("module", _MODULES)+def test_subprocess_calls_pass_stdin(module):+ path = Path(importlib.import_module(module).__file__)+ tree = ast.parse(path.read_text(encoding="utf-8"))+ missing = [+ node.lineno+ for node in ast.walk(tree)+ if isinstance(node, ast.Call)+ and isinstance(node.func, ast.Attribute)+ and node.func.attr in _SPAWNERS+ and isinstance(node.func.value, ast.Name)+ and node.func.value.id == "subprocess"+ and not {k.arg for k in node.keywords} & {"stdin", "input"}+ ]+ assert not missing, f"{path.name}: subprocess call without stdin at lines {missing}"diff --git a/tests/unit/test_providers/test_codex_cli_provider.py b/tests/unit/test_providers/test_codex_cli_provider.pyindex a71cc765f..8c267f873 100644--- a/tests/unit/test_providers/test_codex_cli_provider.py+++ b/tests/unit/test_providers/test_codex_cli_provider.py@@ -523,3 +523,21 @@ async def test_generate_rejects_unsupported_reasoning_off(monkeypatch, tmp_path)with pytest.raises(ProviderError, match="model_reasoning_effort='none'"):await CodexCliProvider(repo_path=tmp_path).generate("sys", "user", reasoning="off")+++def test_model_catalog_probe_never_inherits_stdin(monkeypatch):+ """Inside a stdio MCP server stdin is the JSON-RPC pipe."""+ import subprocess++ from repowise.core.providers.llm import codex_cli++ seen: dict[str, Any] = {}++ def fake_run(_args, **kwargs):+ seen.update(kwargs)+ return subprocess.CompletedProcess(_args, 1, "", "")++ monkeypatch.setattr(codex_cli.subprocess, "run", fake_run)+ codex_cli._load_codex_model_catalog.cache_clear()+ assert codex_cli._load_codex_model_catalog("codex-stdin-probe") is None+ assert seen["stdin"] is subprocess.DEVNULLdiff --git a/tests/unit/test_providers/test_opencode_provider.py b/tests/unit/test_providers/test_opencode_provider.pyindex ec98103dd..0fc4f25c5 100644--- a/tests/unit/test_providers/test_opencode_provider.py+++ b/tests/unit/test_providers/test_opencode_provider.py@@ -479,3 +479,21 @@ async def test_available_model_options_fallback(monkeypatch, tmp_path):assert options[0].model == "opencode/default"assert options[0].recommended is Trueassert options[0].source == "fallback"+++def test_model_catalog_probe_never_inherits_stdin(monkeypatch):+ """Inside a stdio MCP server stdin is the JSON-RPC pipe."""+ import subprocess++ from repowise.core.providers.llm import opencode++ seen: dict[str, Any] = {}++ def fake_run(_args, **kwargs):+ seen.update(kwargs)+ return subprocess.CompletedProcess(_args, 1, "", "")++ monkeypatch.setattr(opencode.subprocess, "run", fake_run)+ opencode._load_opencode_model_catalog.cache_clear()+ assert opencode._load_opencode_model_catalog("opencode-stdin-probe") is None+ assert seen["stdin"] is subprocess.DEVNULL