Skip to content

fix(mcp): harden code-rationale git-grep for macOS and color configs#825

Open
austintraver wants to merge 1 commit into
repowise-dev:mainfrom
austintraver:fix/git-grep-posix-whitespace
Open

fix(mcp): harden code-rationale git-grep for macOS and color configs#825
austintraver wants to merge 1 commit into
repowise-dev:mainfrom
austintraver:fix/git-grep-posix-whitespace

Conversation

@austintraver

Copy link
Copy Markdown
Contributor

Summary

grep_comment_candidates() in mcp_server/_code_rationale.py shells out to git grep -E and parses the result. The invocation was fragile in two environment-dependent ways, both invisible on Linux CI:

  1. \s is not portable in git grep -E. \s is a GNU regex extension; macOS/BSD system git (e.g. Apple Git) does not honor it in ERE mode, so ^\s*(#|//|--|\*)... never matches comment-leading lines with leading indentation. The function returns [], silently disabling MCP concept-anchoring / in-code rationale on macOS.
  2. git grep output was not forced to --no-color. With color.ui=always in a user's git config, git wraps each path in ANSI escapes (\x1b[35m...enrichment.py\x1b[m), which the line.split(":")[0] path parse captures verbatim, corrupting every returned candidate path.

Both are invisible in CI (Linux, default git config), so the two tests that cover this pass in CI while the feature is silently broken for macOS users and anyone running color.ui=always.

Fix: use the POSIX class [[:space:]] (portable across BSD/macOS and GNU/Linux git) and pass --no-color so the parsed output is stable regardless of git config. No behavior change on Linux.

Related Issues

None filed.

Test Plan

  • uv run pytest tests/unit/server/mcp/test_code_rationale.py -> 19 passed on macOS. Before this change, test_grep_comment_candidates_ranks_number_bearing_file and test_concept_anchor_injects_winner_as_dominant fail on macOS (they already pass on Linux/CI).
  • ruff check clean on the changed file.

Reproduced on macOS (Apple Git 2.50.1):

git grep -E '^\s*#.*caller'            # no match (\s unsupported in BSD git ERE)
git grep -E '^[[:space:]]*#.*caller'   # matches
# and with color.ui=always, paths return as \x1b[35m...\x1b[m unless --no-color is passed

Checklist

  • Code follows the project's style
  • Existing tests still pass (and now also pass on macOS / with color.ui=always)
  • No new dependencies
  • No docs change needed

grep_comment_candidates() shells out to `git grep -E` and parses the output.
The invocation was fragile in two environment-dependent ways, both invisible
on Linux CI:

1. The pattern anchored on `\s`, a GNU regex extension. macOS/BSD git's ERE
   engine does not honor it, so `^\s*(#|//|--|\*)...` matched nothing and the
   function returned an empty list, silently disabling concept-anchoring on
   macOS.

2. The command did not pass `--no-color`, so a user with `color.ui=always`
   got ANSI escapes wrapping every path (`\x1b[35m...enrichment.py\x1b[m`),
   which the `line.split(":")[0]` path parse then captured verbatim.

Use the POSIX class `[[:space:]]` (portable across BSD/macOS and GNU/Linux)
and pass `--no-color` so parsed output is stable regardless of git config.
The two existing tests already covered this behavior but failed only off
Linux; both now pass on macOS.
@repowise-bot

repowise-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

✅ Health: 7.6 (unchanged)

📋 At a glance
1 file changed health · 1 hotspot touched · 2 new findings introduced · 1 co-change pair left out. Scoped to packages.

File Score Δ Why
.../mcp_server/_code_rationale.py 5.1 → 4.7 ▼ -0.4 🔻 introduced brain method, large method · ✅ resolved error handling

💡 .../mcp_server/_code_rationale.py: Split this function. It carries high cyclomatic complexity and many dependents — extract cohesive responsibilities into helpers so each call site sees a smaller surface area.

🔎 More signals (2)

🔥 Hotspot touched (1)

  • .../mcp_server/_code_rationale.py — 3 commits/90d, 4 dependents · primary owner: Raghav Chamadiya (100%)

🔗 Hidden coupling (1 file)

  • .../mcp_server/_code_rationale.py co-changes with .../tool_answer/answer.py (2× — 🟢 routine) — not in this PR.

📊 Full report · ⭐ Star Repowise · 📥 Install bot · Last updated 2026-07-14 04:48 UTC
Silence on a single PR with [skip repowise] in the title · Per-repo toggle on repowise.dev/settings?tab=bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant