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(answer): extract a value only for the constant the question names (#3700)

Swati Ahuja6f5ab2ccd6eb
diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/answer.py b/packages/server/src/repowise/server/mcp_server/tool_answer/answer.py
index 0586d7e56..a287295b6 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_answer/answer.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/answer.py
@@ -606,7 +606,7 @@ async def get_answer(
# Value-shaped question + a question-matched constant in the top hits →
# the verbatim assignment line (read live by the hydrator) IS the answer.
if _is_value_question(question) and question_ids:
- extraction = _extract_value_answer(hits, question_ids)
+ extraction = _extract_value_answer(hits, question_ids, question)
if extraction is not None:
return _with_candidates(
build_value_payload(
diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/payload.py b/packages/server/src/repowise/server/mcp_server/tool_answer/payload.py
index bdeb963ea..345b3b2c6 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_answer/payload.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/payload.py
@@ -346,15 +346,19 @@ def build_value_payload(
The verbatim assignment line (read live by the hydrator) IS the answer.
Not cached: extraction is cheap and must reflect the current source.
+ Only a constant the question names exactly is ``high``: one whose name the
+ question only spells out may not be the one meant.
"""
top_score_fp = hits[0].get("score", 0.0) if hits else 0.0
answer_text = extraction["answer"]
if extraction.get("value_source"):
answer_text += "\n\n" + extraction["value_source"]
+ exact = bool(extraction.get("exact_name"))
+ confidence = "high" if exact else "medium"
return {
"answer": answer_text,
"citations": [extraction["file"]],
- "confidence": "high",
+ "confidence": confidence,
"retrieval_quality": (
"high" if top_score_fp >= _HIGH_CONFIDENCE_SCORE_FLOOR else "partial"
),
@@ -363,13 +367,18 @@ def build_value_payload(
"retrieval": [],
"note": (
"Extracted verbatim from the live source line — no LLM "
- "synthesis involved. Cite directly; no verification "
- "Read needed. candidate_files holds the files retrieval ranked, "
+ "synthesis involved. "
+ + (
+ "Cite directly; no verification Read needed. "
+ if exact
+ else "The question does not name it exactly; confirm it is the one meant. "
+ )
+ + "candidate_files holds the files retrieval ranked, "
"for the wider question the value sits inside."
),
"_meta": _build_meta(
timing_ms=(time.perf_counter() - t0) * 1000,
- hint=_answer_hint("high"),
+ hint=_answer_hint(confidence),
repository=repository,
targets=[extraction["file"], *fallback_targets],
),
diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/symbols.py b/packages/server/src/repowise/server/mcp_server/tool_answer/symbols.py
index 22b36e6a5..072e73ac1 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_answer/symbols.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/symbols.py
@@ -17,6 +17,7 @@ from sqlalchemy import select
from repowise.core.persistence.models import WikiSymbol
from repowise.server.mcp_server._query_shape import _WORD_CHAIN_RE, _identifier_shaped
+from repowise.server.mcp_server._query_terms import split_humps
from repowise.server.mcp_server._verify import verify_and_heal
from repowise.server.mcp_server.tool_answer.config import (
_ENRICH_TOP_N_HITS,
@@ -127,40 +128,69 @@ def is_symbol_lookup_question(question: str, question_ids: set[str]) -> bool:
return not _prose_dominates(question, list(question_ids))
-def _extract_value_answer(hits: list[dict], question_ids: set[str]) -> dict | None:
+def _value_entry(s: dict, path: str) -> dict | None:
+ """The extractable assignment for one hydrated symbol, or None."""
+ if not s.get("_matched") or s.get("kind") not in ("constant", "variable"):
+ return None
+ sig = s.get("signature") or ""
+ if "=" not in sig:
+ return None
+ entry = {
+ "name": s.get("name"),
+ "signature": sig,
+ "file": path,
+ "line": s.get("start_line"),
+ "answer": f"{sig} ({path}:{s.get('start_line')})",
+ }
+ # Multi-line values: include the live body the hydrator attached.
+ excerpt = s.get("source_excerpt")
+ if excerpt and excerpt.strip() != sig.strip():
+ entry["value_source"] = excerpt
+ return entry
+
+
+def _extract_value_answer(
+ hits: list[dict], question_ids: set[str], question: str
+) -> dict | None:
"""Verbatim-assignment answer for value-shaped questions (the C1 fast path).
A matched constant/variable's signature is the verbatim assignment from
- live source, so it is the answer, with no LLM call. Exact name matches win
- over substring matches.
+ live source, so it is the answer, with no LLM call. An exact name match
+ wins. A name that only contains a question identifier (``Symbol`` inside
+ ``SymbolKind``) is a coincidence unless the question's words spell every
+ part of it; otherwise None, so synthesis and its gates answer instead.
"""
qids_lower = {q.lower() for q in question_ids}
- candidates: list[dict] = []
+ question_stems = _text_stems(question)
+ question_parts = _name_parts(question)
+ spelled: dict | None = None
for h in hits[:_ENRICH_TOP_N_HITS]:
path = h.get("target_path")
- if not path:
- continue
- for s in h.get("symbols") or []:
- if not s.get("_matched") or s.get("kind") not in ("constant", "variable"):
- continue
- sig = s.get("signature") or ""
- if "=" not in sig:
+ for s in (h.get("symbols") or []) if path else []:
+ entry = _value_entry(s, path)
+ if entry is None:
continue
- entry = {
- "name": s.get("name"),
- "signature": sig,
- "file": path,
- "line": s.get("start_line"),
- "answer": f"{sig} ({path}:{s.get('start_line')})",
- }
- # Multi-line values: include the live body the hydrator attached.
- excerpt = s.get("source_excerpt")
- if excerpt and excerpt.strip() != sig.strip():
- entry["value_source"] = excerpt
- if (s.get("name") or "").lower() in qids_lower:
+ name = entry["name"] or ""
+ if name.lower() in qids_lower:
+ entry["exact_name"] = True
return entry
- candidates.append(entry)
- return candidates[0] if candidates else None
+ if spelled is None and _question_spells(name, question_stems, question_parts):
+ spelled = entry
+ return spelled
+
+
+def _name_parts(text: str) -> set[str]:
+ """Lowercased hump- and separator-split parts of *text*, short ones included."""
+ return {p.lower() for p in re.split(r"[^A-Za-z0-9]+", split_humps(text)) if p}
+
+
+def _question_spells(name: str, question_stems: set[str], question_parts: set[str]) -> bool:
+ """Whether the question's words cover every part of *name*."""
+ name_stems = _text_stems(name)
+ if not name_stems or not all(_stem_hit(t, question_stems) for t in name_stems):
+ return False
+ # _text_stems drops parts under 3 chars, so `V2` in `MAX_V2` must appear as is.
+ return all(p in question_parts for p in _name_parts(name) if len(p) < 3)
def _symbol_def_dict(sym) -> dict:
diff --git a/tests/unit/server/mcp/test_answer_calibration.py b/tests/unit/server/mcp/test_answer_calibration.py
index 4d03a1914..2d69e7bf4 100644
--- a/tests/unit/server/mcp/test_answer_calibration.py
+++ b/tests/unit/server/mcp/test_answer_calibration.py
@@ -464,6 +464,72 @@ async def test_mechanism_question_skips_fast_path(setup_mcp, monkeypatch):
assert "grounding" not in result
+def _value_hits(name: str, signature: str) -> list[dict]:
+ sym = {"name": name, "kind": "constant", "signature": signature, "start_line": 7}
+ return [{"target_path": "pkg/models.py", "symbols": [dict(sym, _matched=True)]}]
+
+
+class TestValueExtractionRelevance:
+ """A constant that only contains a question identifier is not the answer."""
+
+ def _extract(self, question: str, name: str, signature: str):
+ from repowise.server.mcp_server.tool_answer.symbols import (
+ _extract_question_identifiers,
+ _extract_value_answer,
+ )
+
+ ids = _extract_question_identifiers(question)
+ return _extract_value_answer(_value_hits(name, signature), ids, question)
+
+ def test_exact_name_is_extracted(self) -> None:
+ out = self._extract(
+ "What is the default of _DOMINANCE_RATIO?", "_DOMINANCE_RATIO", "_DOMINANCE_RATIO = 1.2"
+ )
+ assert out is not None and out["exact_name"]
+
+ def test_identifier_inside_an_unrelated_name_is_not_extracted(self) -> None:
+ # `Symbol` sits inside `SymbolKind`; nothing in the question asks about a kind.
+ question = (
+ "How are has_method edges built from Symbol.parent_name; how does a "
+ "method's parent_name map to its class (e.g. enum constant parent 'E::A')?"
+ )
+ assert self._extract(question, "SymbolKind", "SymbolKind = Literal[") is None
+
+ def test_alias_of_the_named_function_is_not_extracted(self) -> None:
+ question = "What is the timeout for the FTS search leg in get_answer retrieval?"
+ assert (
+ self._extract(question, "_get_answer_raw", "_get_answer_raw = get_answer") is None
+ )
+
+ def test_name_the_question_spells_out_is_extracted_but_not_exact(self) -> None:
+ out = self._extract(
+ "What is the DEFAULT_TIMEOUT config value?",
+ "DEFAULT_TIMEOUT_CONFIG",
+ "DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=5.0)",
+ )
+ assert out is not None and not out.get("exact_name")
+
+ def test_short_name_part_must_appear_in_the_question(self) -> None:
+ # The stemmer drops parts under 3 chars, so `V2` is checked as a token.
+ assert self._extract("What is the MAX limit?", "MAX_V2", "MAX_V2 = 8") is None
+ out = self._extract("What is the MAX v2 limit?", "MAX_V2", "MAX_V2 = 8")
+ assert out is not None and not out.get("exact_name")
+
+
+async def test_spelled_out_value_is_medium_not_high(setup_mcp, monkeypatch):
+ import repowise.server.mcp_server.tool_answer.answer as answer_mod
+ from repowise.server.mcp_server import get_answer
+
+ symbol = dict(_SYMBOL, name="MIN_COUNT_FLOOR", signature="MIN_COUNT_FLOOR = 2")
+ _patch_pipeline(monkeypatch, answer_mod, with_symbols=True, symbol=symbol)
+
+ result = await get_answer("What is the floor value of MIN_COUNT?")
+ assert result["grounding"] == "extracted"
+ assert result["confidence"] == "medium"
+ assert "no verification Read needed" not in result["note"]
+
+
@pytest.mark.asyncio
async def test_synthesized_answer_carries_grounded_quotes(setup_mcp, monkeypatch):
"""C2: symbols named in the answer attach {path, lines, quote}."""