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(get_answer): cap confidence when a cited body was truncated (#1451)

Raghav Chamadiyadbad6c0b3476
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 70884bef9..f98aa1434 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
@@ -120,6 +120,7 @@ from repowise.server.mcp_server.tool_answer.confidence import (
_has_unqualified_exclusivity_over_truncated,
_is_value_question,
_ungrounded_numbers,
+ implicated_withheld_symbols,
)
from repowise.server.mcp_server.tool_answer.config import (
_AGREEMENT_RANK_GAP,
@@ -169,6 +170,7 @@ from repowise.server.mcp_server.tool_answer.symbols import (
_read_symbol_source,
build_homonym_union_bodies,
union_defers_to_synthesis,
+ withheld_definitions,
)
from repowise.server.mcp_server.tool_answer.synthesis import (
_hash_question,
@@ -1194,11 +1196,38 @@ async def get_answer(
names = sorted(union_groups)
total = sum(len(v) for v in union_groups.values())
cited = sorted({b["path"] for b in union_bodies})
+ # This payload returns BEFORE synthesis, so none of the confidence
+ # gates below ever see it: it is served in no-LLM mode and it used
+ # to hardcode confidence="high" with "no verification Read" even
+ # when a body arrived truncated. "this is the complete set" is also
+ # an exclusivity claim, generated by us rather than by a model, and
+ # it is true of the DEFINITION SET while saying nothing about
+ # whether each body was served whole.
+ #
+ # The dependency test the synthesis gate uses CANNOT work here, and
+ # keying on it made this gate dead code. This path is reached only
+ # for naming/lookup questions about the homonym, so the question
+ # names the SERVED symbol by construction while the withheld symbols
+ # are its inner members, and there is no answer prose to inspect
+ # either. Here the bodies simply ARE the answer, so truncation alone
+ # is the right condition: a body the consumer cannot see is exactly
+ # what confidence should reflect. This is the one place the blunt cap
+ # the brief proposed is correct.
+ union_truncated = any(b.get("truncated") for b in union_bodies)
+ _union_confidence = "medium" if union_truncated else "high"
note = (
f"{total} definition(s) of {', '.join(names)} exist (exact-name "
- f"index scan; this is the complete set). {len(union_bodies)} "
- "inlined below in symbol_bodies as live source; use them "
- "directly, no verification Read."
+ f"index scan; this is the complete set of DEFINITIONS). "
+ f"{len(union_bodies)} inlined below in symbol_bodies as live "
+ "source"
+ )
+ note += (
+ "; use them directly, no verification Read."
+ if not union_truncated
+ else ". At least one body was truncated: see "
+ "symbol_bodies[].withheld_symbols for what was not served, "
+ "and call get_symbol with the continuation before relying "
+ "on behaviour you cannot see."
)
if more_defs:
note += (
@@ -1216,7 +1245,7 @@ async def get_answer(
"implementations, so pick the one for your context."
),
"citations": cited,
- "confidence": "high",
+ "confidence": _union_confidence,
"grounding": "exact_symbol",
"symbol_bodies": union_bodies,
"fallback_targets": [b["path"] for b in union_bodies],
@@ -1224,7 +1253,7 @@ async def get_answer(
"note": note,
"_meta": _build_meta(
timing_ms=(time.perf_counter() - t0) * 1000,
- hint=_answer_hint("high", len(union_bodies)),
+ hint=_answer_hint(_union_confidence, len(union_bodies)),
repository=repository,
targets=cited,
),
@@ -1600,6 +1629,14 @@ async def get_answer(
if sym_end > end_served:
entry["truncated"] = True
entry["continuation"] = f"{path}:{end_served + 1}-{sym_end}"
+ # Say WHAT was withheld, not just that something was. A bare
+ # truncated flag plus a get_symbol pointer was followed zero times
+ # across the agent runs measured, so the consumer needs the names in
+ # hand to decide whether it is missing anything it cares about, and
+ # to continue inside this tool rather than falling back to Read.
+ withheld = withheld_definitions(repo_root, entry["continuation"])
+ if withheld:
+ entry["withheld_symbols"] = withheld
symbol_bodies.append(entry)
_seen_bodies.add((path, name))
if _tier == 0:
@@ -1744,6 +1781,24 @@ async def get_answer(
if exclusivity_over_truncated:
confidence = "medium"
+ # Eighth gate — a WITHHELD symbol the response depends on. The seventh gate
+ # needs an exclusivity token in the prose as well as truncation, and measured
+ # across four independent runs of the reference defect the token appears in
+ # only one of them: the other three are equally incomplete, equally "high",
+ # and it stays silent. It is also inert by construction in no-LLM mode, where
+ # there is no prose to hold a token.
+ #
+ # This gate keys on the dependency instead. It fires when a symbol in the
+ # withheld range is named by the QUESTION (every mode) or referenced as code
+ # by the ANSWER (LLM mode). Truncation alone is deliberately NOT enough:
+ # 22% of truncations withhold nothing the response leans on, and `high` is
+ # worth keeping when it is earned.
+ withheld_implicated = implicated_withheld_symbols(
+ question, answer_text, symbol_bodies
+ )
+ if confidence == "high" and withheld_implicated:
+ confidence = "medium"
+
# Non-dominant ceiling: ambiguous retrieval is the calibration cost of
# always synthesizing — the answer may be right, but with no single dominant
# page it must never read "high" (cite without verifying). Cap at medium even
@@ -1904,13 +1959,46 @@ async def get_answer(
f"Read {fallback_targets[0]} to verify whether other functions "
"participate beyond what the truncated symbol body shows."
)
+ elif withheld_implicated:
+ _ids = [
+ s["symbol_id"]
+ for b in symbol_bodies
+ for s in (b.get("withheld_symbols") or [])
+ if s.get("name") in set(withheld_implicated)
+ ]
+ payload["note"] = (
+ "Part of the code this answer depends on was not served: "
+ f"{', '.join(withheld_implicated)}. Continue in this tool with "
+ f"get_symbol id='{_ids[0]}' before relying on the mechanism."
+ if _ids else
+ "Part of the code this answer depends on was not served: "
+ f"{', '.join(withheld_implicated)}."
+ )
+ payload["next_action_hint"] = (
+ f"call get_symbol id='{_ids[0]}' for the withheld body"
+ if _ids else "request the withheld body before citing"
+ )
elif confidence == "high":
+ # The rationale deliberately no longer cites "the answer is direct
+ # (no hedging)". _SYSTEM_PROMPT instructs the model not to hedge, so
+ # scoring the absence of hedging as evidence FOR confidence is
+ # circular: the pipeline mandates directness and then reads its own
+ # mandate back as a signal. Dominance is an independent measurement;
+ # directness is not.
+ _tail = (
+ "Cite this answer; do not re-read the source unless a specific "
+ "detail is missing."
+ if not any(b.get("truncated") for b in symbol_bodies)
+ # Never tell the consumer to skip re-reading when the payload
+ # itself admits it withheld part of a cited body. The withheld
+ # names are in `symbol_bodies[].withheld_symbols`.
+ else "Some cited bodies were truncated; see "
+ "symbol_bodies[].withheld_symbols for what was not served."
+ )
payload["note"] = (
"High confidence: top retrieval result clearly dominates "
- f"(dominance ratio {_ratio:.2f}x, top score {_top_score:.2f}) "
- "AND the synthesised answer is direct (no hedging). Cite this "
- "answer; do not re-read the source unless a specific detail "
- "is missing."
+ f"(dominance ratio {_ratio:.2f}x, top score {_top_score:.2f}). "
+ + _tail
)
# Concept anchoring put a comment-justified file at the top, so synthesis
diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/confidence.py b/packages/server/src/repowise/server/mcp_server/tool_answer/confidence.py
index b9fd5cfd5..54c889f51 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_answer/confidence.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/confidence.py
@@ -227,3 +227,160 @@ def _has_unqualified_exclusivity_over_truncated(
low = low.replace("\u2019", "'").replace("\u02bc", "'")
return any(tok in low for tok in _EXCLUSIVITY_TOKENS)
+
+
+# A backticked span ending in a source-file extension is a PATH, so its dotted
+# components are not symbol references: ``store.py`` says nothing about a symbol
+# named ``py`` (or one named ``store``).
+_PATHISH_SPAN_RE = re.compile(
+ r"\.(py|pyi|ts|tsx|js|jsx|mjs|cjs|go|java|rb|php|rs|c|h|cc|cpp|hpp|cs|kt|kts|"
+ r"swift|scala|sql|sh|md|json|ya?ml|toml|txt|cfg|ini|lock)$",
+ re.IGNORECASE,
+)
+
+
+def _code_reference(text: str, name: str) -> bool:
+ """Does *text* refer to *name* as CODE rather than as an English word?
+
+ A bare word-boundary match is not usable here. Withheld symbols include
+ ``on``, ``line``, ``input``, ``width`` and ``join``, all ordinary English,
+ so ``\bon\b`` matches "based on the excerpts" and scores a harmless
+ truncation as harmful. Measured across the transcripts on disk, the naive
+ matcher put the harm rate 4+ points high and its hits were visibly prose.
+
+ The call shape allows no space before the paren: ``the width (in pixels)``
+ is a prose parenthetical, not a call, and admitting the space made it one.
+ """
+ n = re.escape(name)
+ patterns = [
+ rf"\b{n}\(", # called
+ rf"\b{n}\s*=(?!=)", # assigned (never a comparison)
+ ]
+ # Attribute access, unless the name IS a file extension: ``store.py`` is a
+ # path, and reading it as an attribute access implicates a symbol ``py``.
+ if not _PATHISH_SPAN_RE.search(f".{name}"):
+ patterns.append(rf"\.{n}\b")
+ if any(re.search(p, text) for p in patterns):
+ return True
+ for span in re.findall(r"`([^`]+)`", text):
+ head = span.strip().split("(")[0].strip()
+ if head == name:
+ return True
+ if _PATHISH_SPAN_RE.search(head):
+ continue
+ if name in head.split("."): # `Store._validate`
+ return True
+ return False
+
+
+def _is_distinctive_name(name: str) -> bool:
+ """Is *name* code-shaped enough that a bare word match cannot be prose?
+
+ Internal capital, underscore or digit — the same shape rule
+ ``_distinctive_terms`` uses. ``_validate`` and ``TodoStore`` qualify;
+ ``write``, ``main``, ``line`` and ``on`` do not, and those are exactly the
+ names that collapse confidence on an ordinary English question.
+ """
+ return (
+ any(ch.isupper() for ch in name[1:])
+ or "_" in name
+ or any(ch.isdigit() for ch in name)
+ )
+
+
+# Nothing but whitespace since the start of the string or the last sentence
+# terminator: a capital here may be grammar rather than a symbol.
+_STARTS_SENTENCE_RE = re.compile(r"(?:^|[.!?])\s*$")
+
+# ...but only when a determiner follows, which is what makes it read as an
+# imperative verb taking an object ("Run the indexer", "Update the schema").
+# "Store keeps what?" is a sentence-initial capital too, and there it IS the
+# symbol, so the exclusion must not swallow it.
+_IMPERATIVE_OBJECT_RE = re.compile(
+ r"^\s+(the|a|an|all|any|my|our|its|this|that|these|those|every|each)\b",
+ re.IGNORECASE,
+)
+
+
+def _question_names(question: str, name: str) -> bool:
+ """Does *question* name the symbol *name*, as code rather than as English?
+
+ Three tiers, loosest first:
+
+ * distinctive names (``_validate``, ``TodoStore``) match case-insensitively
+ on a word boundary — no English word has that shape;
+ * a leading-capital name (``Store``) matches only with its own case, so
+ "the store where results land" does not implicate ``Store``, and not at
+ the start of a sentence, where the capital is grammar rather than a name
+ ("Run the indexer, then what?" must not implicate a symbol ``Run``);
+ * an all-lowercase name (``on``, ``line``, ``write``, ``main``) needs an
+ actual code context, because a bare word match on those demotes ordinary
+ questions like "what is the main entry point?".
+ """
+ if not question:
+ return False
+ if _is_distinctive_name(name):
+ return re.search(rf"\b{re.escape(name)}\b", question, re.IGNORECASE) is not None
+ if any(ch.isupper() for ch in name):
+ return any(
+ not (
+ _STARTS_SENTENCE_RE.search(question[: m.start()])
+ and _IMPERATIVE_OBJECT_RE.match(question[m.end() :])
+ )
+ for m in re.finditer(rf"\b{re.escape(name)}\b", question)
+ )
+ return _code_reference(question, name)
+
+
+def implicated_withheld_symbols(
+ question: str,
+ answer_text: str,
+ symbol_bodies: list[dict],
+) -> list[str]:
+ """Withheld symbols the response actually leans on, worst case first.
+
+ Truncation on its own is not a defect: measured on the transcripts on disk,
+ 31% of responses truncate something and in 22% of those the withheld range
+ holds nothing the response relies on. Capping confidence on the bare flag
+ would spend `high` on those for no gain.
+
+ What matters is whether a symbol the response DEPENDS ON is in the withheld
+ range. Two independent routes, and the split is deliberate:
+
+ * the **question** names it. Always present, so it fires on any answer,
+ including one whose prose never mentions the symbol at all.
+ * the **answer** names it in a code context. Only when synthesis ran.
+
+ The question route is what stops this becoming the mistake the previous gate
+ made: a check conditioned solely on prose stays silent on every answer whose
+ prose happens not to name the symbol, which was 3 responses in 4 measured.
+
+ It is NOT what protects the no-LLM modes, and an earlier version of this
+ docstring wrongly claimed it was. A keyless deployment returns
+ ``_degraded_payload`` before ``symbol_bodies`` is built, so there is nothing
+ here to gate; the homonym-union early return is the no-LLM path that does
+ serve bodies, and it caps on truncation alone rather than calling this.
+
+ ``body_continues`` entries sort first: a symbol whose body was cut by the
+ truncation boundary is the sharper failure, because the response has already
+ shown its signature and may reason about behaviour it never saw.
+ """
+ hits: list[tuple[int, str]] = []
+ for body in symbol_bodies or []:
+ if not isinstance(body, dict) or not body.get("truncated"):
+ continue
+ for sym in body.get("withheld_symbols") or []:
+ name = sym.get("name")
+ if not name:
+ continue
+ in_question = _question_names(question, name)
+ in_answer = bool(answer_text) and _code_reference(answer_text, name)
+ if in_question or in_answer:
+ hits.append((0 if sym.get("body_continues") else 1, name))
+ seen: set[str] = set()
+ out: list[str] = []
+ for _rank, name in sorted(hits, key=lambda t: t[0]):
+ if name not in seen:
+ seen.add(name)
+ out.append(name)
+ return out
diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/config.py b/packages/server/src/repowise/server/mcp_server/tool_answer/config.py
index e2d1c1011..037fb8eb0 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_answer/config.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/config.py
@@ -368,7 +368,14 @@ _HIGH_CONFIDENCE_SCORE_FLOOR = 1.5
# contains. Cached pre-v12 rows carry the bare {path, lines} shape, which is the
# named-but-not-carried payload this version exists to replace, so they must
# bypass rather than serve the old block back.
-_ANSWER_SCHEMA_VERSION = 12
+# v13: withheld-body calibration — a truncated symbol_bodies entry now carries
+# `withheld_symbols`, confidence is capped when the response depends on one of
+# them (and on the homonym-union path whenever any cited body was truncated),
+# and the high/union notes no longer tell the caller to skip re-reading a
+# payload that admits it withheld part of a cited body. Cached pre-v13 rows
+# carry both the old `high` and the old "do not re-read the source" note, which
+# is precisely what this version exists to stop serving, so they must bypass.
+_ANSWER_SCHEMA_VERSION = 13
# Hard TTL on answer-cache rows. Commit-based invalidation (the payload's
# stamped ``_indexed_commit`` vs the repo's current head) is the primary
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 c498eb4ed..1b455525f 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
@@ -7,6 +7,8 @@ promote question-matched symbols to the top of each hit's symbol list.
from __future__ import annotations
+import re
+from functools import lru_cache
from pathlib import Path
from typing import Any
@@ -156,6 +158,12 @@ def _read_symbol_source(
return body
+# Where a signature stops when it does not end in a Python colon: after the
+# closing paren of the parameter list (with an optional return annotation), at a
+# trailing brace, or at a semicolon (an abstract/interface member).
+_SIG_TERMINATOR_RE = re.compile(r"\)\s*(?:->[^:{]*)?\s*[:{]|\{\s*$|;\s*$")
+
+
def _read_signature_from_source(
repo_root: Path | None, file_path: str, start_line: int, *, text: str | None = None
) -> str | None:
@@ -185,7 +193,13 @@ def _read_signature_from_source(
line = lines[i]
sig_lines.append(line.strip())
paren_depth += line.count("(") - line.count(")")
- if line.rstrip().endswith(":") and paren_depth <= 0:
+ stripped = line.rstrip()
+ # "ends with a colon" alone leaves a one-line body (``def go(self): pass``)
+ # and every brace language (``func f() error {``, ``render() {``) with no
+ # terminator at all, so the signature absorbs the lines after it.
+ if paren_depth <= 0 and (
+ stripped.endswith(":") or _SIG_TERMINATOR_RE.search(stripped)
+ ):
break
if not sig_lines:
return None
@@ -463,6 +477,13 @@ def build_homonym_union_bodies(
if end and end > end_served:
entry["truncated"] = True
entry["continuation"] = f"{path}:{end_served + 1}-{end}"
+ # Same treatment as the main answer path: say WHAT was withheld.
+ # This one matters more, not less — the union payload returns
+ # BEFORE synthesis, so it is served in no-LLM mode and it never
+ # reaches any of the confidence gates.
+ w = withheld_definitions(repo_root, entry["continuation"])
+ if w:
+ entry["withheld_symbols"] = w
symbol_bodies.append(entry)
spent += len(body)
else:
@@ -845,3 +866,364 @@ async def _hydrate_symbols_for_hits(
# Sort final slice by start_line for natural reading order.
kept.sort(key=lambda s: s["start_line"])
h["symbols"] = kept
+
+
+# ---------------------------------------------------------------------------
+# What a truncated body withheld
+# ---------------------------------------------------------------------------
+
+# Leading keywords that decorate a declaration without being one. Shared by the
+# keyword and brace-method shapes below.
+_DECL_MODIFIERS = (
+ r"pub|public|private|protected|internal|open|final|static|abstract|override|"
+ r"virtual|sealed|export|default|async|suspend|inline|readonly|declare|"
+ r"unsafe|extern|partial|data|operator|const"
+)
+
+# Definition-line shapes, tried in order; each yields ``indent`` / ``kind`` /
+# ``name``. A Python-only regex was the first cut and it made this whole feature
+# inert on TS/Go/Java — which, at a 120-line body cap, is exactly where
+# truncation bites hardest, since a TS class or a React component is the shape
+# that overruns the cap in the first place.
+_WITHHELD_DEF_PATTERNS: tuple[re.Pattern[str], ...] = (
+ # Python: def / async def / class.
+ re.compile(
+ r"^(?P<indent>[ \t]*)(?:async[ \t]+)?(?P<kind>def|class)[ \t]+"
+ r"(?P<name>[A-Za-z_]\w*)"
+ ),
+ # Go methods, whose name follows the receiver: ``func (s *Store) Write(``.
+ re.compile(
+ r"^(?P<indent>[ \t]*)(?P<kind>func)[ \t]+\([^)]*\)[ \t]*"
+ r"(?P<name>[A-Za-z_]\w*)"
+ ),
+ # Declaration keyword + name: Go func/type, Rust fn/struct/trait/impl/enum,
+ # Java/C#/Kotlin class/interface/record, TS class/interface/type/enum.
+ re.compile(
+ rf"^(?P<indent>[ \t]*)(?:(?:{_DECL_MODIFIERS})[ \t]+)*"
+ r"(?P<kind>func|fn|class|struct|interface|enum|trait|impl|record|type)"
+ r"[ \t]+(?P<name>[A-Za-z_$][\w$]*)"
+ ),
+ # JS/TS function declarations, including ``export default`` and generators.
+ # The separator after ``function`` is REQUIRED (or a generator star). With
+ # ``[ \t]*`` the keyword matched as a mere prefix of a longer identifier, so
+ # ``function_name: Mapped[str] = ...`` reported a symbol called ``_name``:
+ # measured at 507 fabricated entries across this repo, the single largest
+ # source of ids that resolve to nothing.
+ re.compile(
+ r"^(?P<indent>[ \t]*)(?:export[ \t]+)?(?:default[ \t]+)?(?:async[ \t]+)?"
+ r"(?P<kind>function)(?:[ \t]*\*[ \t]*|[ \t]+)(?P<name>[A-Za-z_$][\w$]*)"
+ ),
+ # JS/TS arrow bindings: ``export const Panel = (props) => {``. The arrow has
+ # to belong to the binding itself, with only an optional return annotation
+ # between: allowing slack before it turns every local initialised from a
+ # callback-taking call (``const pages = all.filter((p) => ...)``) into a
+ # "definition" with a symbol_id that resolves to nothing.
+ re.compile(
+ r"^(?P<indent>[ \t]*)(?:export[ \t]+)?(?P<kind>const|let|var)[ \t]+"
+ r"(?P<name>[A-Za-z_$][\w$]*)[^=]*=[ \t]*(?:async[ \t]+)?"
+ r"(?:\([^)]*\)(?:[ \t]*:[^=]*?)?|[A-Za-z_$][\w$]*)[ \t]*=>"
+ ),
+ # Brace-language members: `` public void run(String a) {``, `` render() {``.
+ # Between the parameter list and the brace only a return type or a throws
+ # clause may appear -- no parens, or an assertion call in a test file
+ # (``expect(x).toMatchObject({``) reads as a method declaration. The brace
+ # itself is optional so Allman style (``void Beta()`` with ``{`` on the next
+ # line, the C# default) is reachable; the caller supplies the next line.
+ re.compile(
+ rf"^(?P<indent>[ \t]*)(?:(?:{_DECL_MODIFIERS})[ \t]+)*"
+ r"(?:[A-Za-z_$][\w$<>,.\[\]]*[ \t]+)?(?P<name>[A-Za-z_$][\w$]*)[ \t]*"
+ r"\([^;{]*\)(?P<tail>[^;{()]*)(?P<brace>\{)?[ \t]*$"
+ ),
+)
+_BRACE_MEMBER = len(_WITHHELD_DEF_PATTERNS) - 1
+
+# The brace-member shape above also matches control flow (``if (x) {``), a
+# statement whose keyword the optional type group swallows (``raise
+# ValueError(f"... {x}")`` reports ``ValueError``), and any call taking a
+# callback (``describe("x", () => {``, ``it("x", function () {``). Both the
+# matched NAME and the line's own first word are checked, because the type group
+# hides the keyword from a name-only guard.
+_NOT_A_DEFINITION = frozenset(
+ {
+ "if", "for", "while", "switch", "catch", "else", "do", "try", "return",
+ "with", "using", "lock", "foreach", "case", "synchronized", "await",
+ "yield", "new", "typeof", "in", "of", "when", "unless", "match",
+ "raise", "throw", "assert", "del", "delete", "print", "elif", "except",
+ "finally", "import", "from", "global", "nonlocal", "pass", "break",
+ "continue", "go", "defer", "select", "range", "constructor",
+ }
+)
+
+_FIRST_WORD_RE = re.compile(r"[A-Za-z_$][\w$]*")
+
+# Words no language lets you NAME a definition, so a match producing one is a
+# parse accident whatever shape it came from: ``fn is not None`` reads as Rust's
+# ``fn <name>`` and reported a symbol called ``is``. Kept strictly to reserved
+# words -- ``match``, ``range`` and ``print`` are all real function names.
+_RESERVED_NAMES = frozenset(
+ {
+ "is", "not", "and", "or", "in", "if", "else", "elif", "for", "while",
+ "return", "none", "true", "false", "null", "undefined", "class", "def",
+ "import", "from", "as", "with", "pass", "lambda", "del", "global",
+ "raise", "try", "except", "finally", "yield", "await", "assert",
+ "break", "continue", "nonlocal", "var", "let", "const", "function",
+ }
+)
+
+
+def _match_definition(raw: str, next_raw: str = "") -> re.Match[str] | None:
+ """First definition shape *raw* matches, or None.
+
+ ``next_raw`` is the following source line, consulted only for Allman-style
+ braces where the declaration and its ``{`` sit on separate lines.
+ """
+ for i, pattern in enumerate(_WITHHELD_DEF_PATTERNS):
+ m = pattern.match(raw)
+ if not m:
+ continue
+ if m.group("name").lower() in _RESERVED_NAMES:
+ continue
+ if i == _BRACE_MEMBER:
+ head = _FIRST_WORD_RE.match(raw.strip())
+ if m.group("name").lower() in _NOT_A_DEFINITION:
+ continue
+ if head and head.group(0).lower() in _NOT_A_DEFINITION:
+ continue
+ # An anonymous function passed as an argument, in either syntax.
+ if "=>" in raw[: m.end()] or "function" in raw[: m.end()]:
+ continue
+ # Allman: the brace is on the next line. A declaration never ends in
+ # a comma, but an argument on its own line inside a multi-line call
+ # does -- and when the following argument is a dict literal, the
+ # next line really is ``{`` (``bool(matched_nums),`` then ``{``).
+ if not m.group("brace") and (
+ next_raw.strip() != "{" or raw.rstrip().endswith(",")
+ ):
+ continue
+ return m
+ return None
+
+
+# Anything that could open a string or a comment. A line with none of these
+# cannot change the scanner's state, so it skips the character walk.
+_QUOTEISH_RE = re.compile(r"""["'#]|/[*/]""")
+
+
+@lru_cache(maxsize=8)
+def _string_masked_lines(lines: tuple[str, ...]) -> frozenset[int]:
+ """1-based line numbers that START inside a multi-line string or comment.
+
+ Repowise's own docstrings are full of indented ``def``/``class`` examples,
+ and without this every one of them becomes a ``symbol_id`` the note tells the
+ agent to fetch and that resolves to nothing.
+
+ Deliberately a lexer-lite: it tracks Python triple quotes and C-style
+ ``/* */`` blocks and stops at ``#`` / ``//``. It does not know about raw
+ strings, template literals or nested-language escapes. The ceiling is
+ acceptable because the cost of a miss is one spurious name in a list of at
+ most eight, not a wrong answer; upgrading means parsing, which this helper
+ exists to avoid.
+
+ Cached on the line tuple: this is a per-character Python loop over the whole
+ file (68 ms on a 424 KB one), and the homonym-union path calls it once per
+ truncated body, re-masking the same file each time.
+ """
+ masked: set[int] = set()
+ delim: str | None = None
+ in_block = False
+ for n, raw in enumerate(lines, 1):
+ if delim is not None or in_block:
+ masked.add(n)
+ elif not _QUOTEISH_RE.search(raw):
+ # Nothing on this line can open a string or comment, so the
+ # character walk below cannot change state. Most lines are this
+ # line, and skipping them is what keeps a whole-file scan cheap.
+ continue
+ i = 0
+ while i < len(raw):
+ if delim is not None:
+ if raw.startswith(delim, i):
+ delim, i = None, i + 3
+ else:
+ i += 1
+ continue
+ if in_block:
+ if raw.startswith("*/", i):
+ in_block, i = False, i + 2
+ else:
+ i += 1
+ continue
+ if raw.startswith('"""', i) or raw.startswith("'''", i):
+ delim, i = raw[i : i + 3], i + 3
+ continue
+ if raw.startswith("/*", i):
+ in_block, i = True, i + 2
+ continue
+ if raw[i] == "#" or raw.startswith("//", i):
+ break
+ if raw[i] in ('"', "'"):
+ quote, i = raw[i], i + 1
+ while i < len(raw):
+ if raw[i] == "\\":
+ i += 2
+ continue
+ if raw[i] == quote:
+ i += 1
+ break
+ i += 1
+ continue
+ i += 1
+ return frozenset(masked)
+
+
+def _indent_width(raw: str) -> int:
+ return len(raw) - len(raw.lstrip())
+
+
+# Stand-in indent for "the cut is inside something still open", used when every
+# withheld line is a string body or a bracket tail and none carries a real one.
+_UNBOUNDED_INDENT = 1 << 30
+
+
+# Cap on how many withheld definitions are surfaced. This block exists to let
+# the agent CONTINUE inside the tool rather than fall back to Read, so it has to
+# stay small enough that it never competes with the answer for the window: names
+# and signatures only, never bodies.
+_WITHHELD_MAX_SYMBOLS: int = 8
+
+
+def withheld_definitions(
+ repo_root: Path | None, continuation: str | None
+) -> list[dict]:
+ """Definitions that live in the range a truncated body did NOT serve.
+
+ ``continuation`` is the ``path:first-last`` pointer already attached to a
+ truncated ``symbol_bodies`` entry, so this reads exactly the lines the
+ payload admits it withheld.
+
+ Why this exists rather than just flagging the truncation: measured on the
+ transcripts on disk, when a body is truncated the withheld range contains a
+ symbol the answer goes on to talk about **78% of the time**, and the
+ responses are at ``confidence: high`` in most of those. A flag alone does
+ not help the consumer, and the ``get_symbol`` pointer the payload already
+ carries was followed ZERO times across the runs measured. Names and
+ signatures are cheap and keep the agent inside the tool.
+
+ Returns ``[{name, kind, line, symbol_id, signature}]``, the boundary-cut
+ symbol first, empty on any failure (a probe that cannot read must not
+ manufacture doubt).
+ """
+ if not continuation:
+ return []
+ path, _, span = continuation.rpartition(":")
+ first, _, last = span.partition("-")
+ if not path or not first.isdigit() or not last.isdigit():
+ return []
+ lo, hi = int(first), int(last)
+ text = _read_repo_text(repo_root, path)
+ if text is None:
+ return []
+ lines = text.splitlines()
+ if lo < 1 or lo > len(lines):
+ return []
+ masked = _string_masked_lines(tuple(lines))
+
+ def _entry(line_no: int, m: re.Match[str], *, cut: bool = False) -> dict:
+ name = m.group("name")
+ # The brace-member shape has no keyword to report, so it is named for
+ # what it is rather than mislabelled as a Python `def`.
+ kind = m.groupdict().get("kind") or "member"
+ sig = _read_signature_from_source(repo_root, path, line_no, text=text)
+ e = {
+ "name": name,
+ "kind": kind,
+ "line": line_no,
+ "symbol_id": f"{path}::{name}",
+ "signature": (sig or f"{kind} {name}").strip(),
+ }
+ if cut:
+ e["body_continues"] = True
+ return e
+
+ out: list[dict] = []
+
+ # The symbol whose body is CUT BY the boundary, which is the case that
+ # motivated this whole helper and the one a naive implementation misses.
+ # In the reference defect the served range ended at 166 and `_validate`
+ # starts at 164: its `def` line was served, so it does not appear anywhere
+ # in the withheld range, while the line that actually causes the bug (176)
+ # sits inside it. Reporting only defs that START after the cut would say
+ # nothing about the symbol the answer is about.
+ #
+ # Taking the nearest preceding definition unconditionally is wrong, though:
+ # a symbol that ENDED before the cut was served whole, and reporting it as
+ # continuing puts a fully-served name at the head of the note and into the
+ # get_symbol pointer. A definition at indent I reaches line ``lo`` only if
+ # every non-blank line from it up to the first non-blank withheld line is
+ # indented deeper than I, so walking backwards while tracking the running
+ # minimum indent decides it exactly, in one pass and with no re-scan.
+ #
+ # The anchor obeys the same two exclusions as the walk. Taking the first
+ # non-blank withheld line flatly is what put the walk one line short of
+ # reality: when the cut lands ON a multi-line signature's own ``) -> dict:``
+ # (or on a flush-left docstring line), the anchor reads as column 0, the
+ # walk dies at once, and the payload ships truncated with NO withheld
+ # symbols -- gate 8 inert on exactly the long entry points that truncate.
+ _end = min(hi, len(lines))
+ _usable = [
+ n
+ for n in range(lo, _end + 1)
+ if lines[n - 1].strip()
+ and n not in masked
+ and lines[n - 1].strip()[0] not in ")]}{"
+ ]
+ if _usable:
+ anchor = _indent_width(lines[_usable[0] - 1])
+ elif any(lines[n - 1].strip() for n in range(lo, _end + 1)):
+ # Every withheld line is a string body or a bracket tail, so whatever
+ # encloses the cut is certainly still open: let any preceding
+ # definition qualify.
+ anchor = _UNBOUNDED_INDENT
+ else:
+ anchor = None
+ if anchor is not None:
+ min_indent = anchor
+ for back in range(lo - 1, 0, -1):
+ if min_indent <= 0:
+ break # nothing can be shallower, so nothing can still be open
+ raw = lines[back - 1]
+ stripped = raw.strip()
+ if not stripped:
+ continue
+ # A line opening with a closing bracket is the tail of a multi-line
+ # construct, not a statement at its own indent. Folding it is what
+ # made this miss the live reference case: ``get_answer``'s signature
+ # spans lines and ends ``) -> dict:`` at column 0, so the running
+ # minimum hit zero on the signature's own closing paren and the walk
+ # gave up two lines short of the ``async def`` it was looking for.
+ # An Allman brace (``{`` alone) is likewise part of the declaration
+ # above it, not a statement.
+ if stripped[0] in ")]}{":
+ continue
+ ind = _indent_width(raw)
+ nxt = lines[back] if back < len(lines) else ""
+ m = None if back in masked else _match_definition(raw, nxt)
+ if m is not None and ind < min_indent:
+ out.append(_entry(back, m, cut=True))
+ break
+ min_indent = min(min_indent, ind)
+
+ seen = {d["name"] for d in out}
+ for offset, raw in enumerate(lines[lo - 1 : _end]):
+ line_no = lo + offset
+ if line_no in masked:
+ continue
+ nxt = lines[line_no] if line_no < len(lines) else ""
+ m = _match_definition(raw, nxt)
+ if m is None or m.group("name") in seen:
+ continue
+ seen.add(m.group("name"))
+ out.append(_entry(line_no, m))
+ if len(out) >= _WITHHELD_MAX_SYMBOLS:
+ break
+ return out
diff --git a/tests/unit/server/mcp/test_answer_withheld_symbols.py b/tests/unit/server/mcp/test_answer_withheld_symbols.py
new file mode 100644
index 000000000..90ff1080a
--- /dev/null
+++ b/tests/unit/server/mcp/test_answer_withheld_symbols.py
@@ -0,0 +1,1065 @@
+"""Withheld-body handling for get_answer (follow-up to #1444 / PR #1445).
+
+The gate PR #1445 added needs BOTH an exclusivity token in the prose AND a
+truncated body. Measured across four independent runs of the reference defect,
+the token appears in one of them; the other three are equally incomplete,
+equally `confidence: high`, and the gate stays silent. It is also inert by
+construction with no LLM, because there is no prose to hold a token.
+
+These tests pin the follow-up behaviour:
+
+ * a truncated body must say WHAT it withheld (`withheld_symbols`), so the
+ consumer can continue inside the tool instead of falling back to Read;
+ * confidence must drop when a withheld symbol is one the response depends on;
+ * confidence must SURVIVE when the truncation withheld nothing relevant
+ (22% of truncations measured, and `high` is worth keeping when earned);
+ * the note must never tell the consumer to skip re-reading a payload that
+ admits it withheld part of a cited body;
+ * the dependency check must fire with NO prose at all, which is the no-LLM
+ path and the one the previous gate cannot reach.
+
+NOTE: the new helpers are imported INSIDE test bodies, never at module level.
+A module-level import turns "this behaviour was wrong before" into a collection
+error on the parent commit, so the file errors out and the end-to-end
+assertions never run -- proving only that a function is new. With the imports
+deferred the file collects on bd577942 and the e2e tests fail on real
+assertions about confidence and the note.
+"""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+from typing import ClassVar
+
+import pytest
+
+_SRC = '''\
+class TodoStore:
+ def write(self, todos):
+ """Write todos."""
+ return self._dedupe_by_id(todos)
+
+ @staticmethod
+ def _validate(item):
+ """Validate and normalize."""
+ item_id = str(item.get("id", "")).strip()
+ if not item_id:
+ item_id = "?"
+ return {"id": item_id}
+
+ @staticmethod
+ def _dedupe_by_id(todos):
+ """Drop duplicates."""
+ return list({t["id"]: t for t in todos}.values())
+'''
+
+
+def repo(tmp_path):
+ (tmp_path / "todo_tool.py").write_text(_SRC, encoding="utf-8")
+ return tmp_path
+
+
+class TestWithheldDefinitions:
+ def test_reports_symbol_whose_body_is_cut_by_the_boundary(self, repo) -> None:
+ """The motivating case, and the one a naive implementation misses.
+
+ `_validate` STARTS at line 6, before the cut, so its `def` line was
+ served and it appears nowhere inside the withheld range. The line that
+ causes the defect (`item_id = "?"`) is inside it. Reporting only defs
+ that start after the cut would say nothing about the symbol the answer
+ is actually about.
+ """
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ got = withheld_definitions(repo, "todo_tool.py:9-20")
+ names = {d["name"] for d in got}
+ assert "_validate" in names, f"expected _validate, got {names}"
+ cut = next(d for d in got if d["name"] == "_validate")
+ assert cut["body_continues"] is True
+ assert cut["symbol_id"] == "todo_tool.py::_validate"
+
+ def test_reports_definitions_starting_inside_the_range(self, repo) -> None:
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ names = {d["name"] for d in withheld_definitions(repo, "todo_tool.py:9-20")}
+ assert "_dedupe_by_id" in names
+
+ @pytest.mark.parametrize(
+ "cont", [None, "", "garbage", "todo_tool.py:x-y", "missing.py:1-9"]
+ )
+ def test_unreadable_input_yields_nothing(self, repo, cont) -> None:
+ """A probe that cannot read must not manufacture doubt."""
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ assert withheld_definitions(repo, cont) == []
+
+ def test_no_repo_root_yields_nothing(self) -> None:
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ assert withheld_definitions(None, "todo_tool.py:9-20") == []
+
+
+class TestImplicatedWithheldSymbols:
+ BODIES: ClassVar[list[dict]] = [
+ {
+ "truncated": True,
+ "withheld_symbols": [
+ {"name": "_validate", "symbol_id": "p::_validate", "body_continues": True}
+ ],
+ }
+ ]
+
+ def test_question_route_fires_with_no_prose_at_all(self) -> None:
+ """The no-LLM path. PR #1445's gate cannot reach this case.
+
+ With no synthesised prose there is no exclusivity token to find, so a
+ prose-conditioned gate is silently inert exactly where a keyless
+ deployment lives. The question is always present, so it is the route
+ that has to carry both modes.
+ """
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ assert implicated_withheld_symbols(
+ "why does _validate stamp a placeholder id?", "", self.BODIES
+ ) == ["_validate"]
+
+ def test_answer_route_fires_on_code_reference(self) -> None:
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ assert implicated_withheld_symbols(
+ "unrelated question", "the `_validate` helper stamps it", self.BODIES
+ ) == ["_validate"]
+
+ def test_prose_mention_as_english_does_not_fire(self) -> None:
+ """Guards the false-positive that inflated the first measurement.
+
+ Withheld symbols include ordinary English words (`on`, `line`, `input`,
+ `width`). A bare word-boundary match scores "based on the excerpts" as a
+ dependency on a symbol named `on`.
+ """
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ bodies = [{"truncated": True, "withheld_symbols": [{"name": "on"}]}]
+ assert implicated_withheld_symbols(
+ "unrelated", "based on the excerpts", bodies
+ ) == []
+
+ def test_irrelevant_truncation_does_not_fire(self) -> None:
+ """22% of truncations withhold nothing relevant; `high` is worth keeping."""
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ assert implicated_withheld_symbols(
+ "how does write work?", "write delegates to a helper", self.BODIES
+ ) == []
+
+ def test_untruncated_body_never_fires(self) -> None:
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ bodies = [{"withheld_symbols": [{"name": "_validate"}]}] # no truncated flag
+ assert implicated_withheld_symbols("why does _validate", "x", bodies) == []
+
+ def test_body_continues_sorts_first(self) -> None:
+ """The sharper failure is a body cut mid-symbol, so it leads the note."""
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ bodies = [
+ {
+ "truncated": True,
+ "withheld_symbols": [
+ {"name": "later_fn"},
+ {"name": "_validate", "body_continues": True},
+ ],
+ }
+ ]
+ got = implicated_withheld_symbols("_validate and later_fn", "", bodies)
+ assert got[0] == "_validate", got
+
+
+# ---------------------------------------------------------------------------
+# End-to-end pipeline tests
+# ---------------------------------------------------------------------------
+
+
+def _build_e2e_src() -> str:
+ """A class long enough to actually cross the inline-body line cap.
+
+ The cap is 120 lines, so a short fixture is served whole and never
+ truncates -- the first version of this test asserted on a truncation that
+ never happened, and failed against the fix it was written for.
+
+ Shaped to reproduce the real defect: `_validate`'s `def` line lands BEFORE
+ the cut (so it is served and looks covered) while the line that matters,
+ `item_id = "?"`, lands after it.
+ """
+ lines = ["class Store:"]
+ for i in range(38): # 3 lines each -> fills lines 2..115
+ lines += [f" def pad_{i}(self):", f" return {i}", ""]
+ assert len(lines) == 115, len(lines)
+ lines += [
+ " @staticmethod", # 116
+ " def _validate(item):", # 117 served
+ ' """Validate."""', # 118
+ ' item_id = str(item.get("id", "")).strip()', # 119
+ " if not item_id:", # 120 boundary
+ ' item_id = "?"', # 121 WITHHELD
+ ' return {"id": item_id}',
+ "",
+ ]
+ for i in range(6):
+ lines += [f" def tail_{i}(self):", f" return {i}", ""]
+ return "\n".join(lines) + "\n"
+
+
+_E2E_SRC = _build_e2e_src()
+_E2E_TOTAL_LINES = _E2E_SRC.count("\n")
+
+# `Store` runs past the 120-line inline cap, so the served body stops at 120 and
+# the entry truncates. `_validate`'s def (117) is inside the served part; the
+# line that causes the defect (121) is not.
+_TRUNCATED_CLASS = {
+ "name": "Store",
+ "kind": "class",
+ "signature": "class Store",
+ "docstring": "A store.",
+ "start_line": 1,
+ "end_line": _E2E_TOTAL_LINES,
+ "_matched": True,
+ "source_excerpt": "class Store:\n def pad_0(self):\n return 0",
+}
+
+
+def _patch_pipeline(monkeypatch, answer_mod, *, symbol: dict):
+ async def _fake_retrieve(question, ctx):
+ return [
+ {"page_id": "file_page:store.py", "score": 5.0},
+ {"page_id": "file_page:other.py", "score": 4.0},
+ ]
+
+ async def _fake_hydrate(hits, ctx, *, scope=None):
+ for i, h in enumerate(hits):
+ h["target_path"] = h["page_id"].removeprefix("file_page:")
+ h["title"] = h["target_path"]
+ h["summary"] = "Store summary."
+ h["snippet"] = ""
+ h["page_type"] = "file_page"
+ if i == 0:
+ h["symbols"] = [dict(symbol)]
+ return hits
+
+ monkeypatch.setattr(answer_mod, "_hybrid_retrieve", _fake_retrieve)
+ monkeypatch.setattr(answer_mod, "_hydrate_hits", _fake_hydrate)
+
+
+def _patch_provider(monkeypatch, answer_mod, content: str):
+ class _Provider:
+ provider_name = "mock"
+ model_name = "mock-1"
+
+ async def generate(self, **kwargs):
+ return SimpleNamespace(content=content)
+
+ monkeypatch.setattr(answer_mod, "_resolve_provider_for_answer", lambda _p: _Provider())
+
+
+# The same file, but a symbol short enough to be served whole: the control for
+# every "confidence must survive when nothing was withheld" assertion.
+_WHOLE_SYMBOL = {
+ "name": "Store",
+ "kind": "class",
+ "signature": "class Store",
+ "docstring": "A store.",
+ "start_line": 1,
+ "end_line": 4,
+ "_matched": True,
+ "source_excerpt": "class Store:\n def pad_0(self):\n return 0",
+}
+
+
+def _patch_union(monkeypatch, answer_mod, *, path="store.py", end_line=None):
+ """Force the homonym-union early return.
+
+ Patching the anchor is the only practical way onto this path: the union
+ fires on an exact-name index scan, and the payload returns before the
+ provider is even resolved, so nothing downstream can steer it. The previous
+ version of this file never drove `get_answer` down here, which is why the
+ dead gate survived review of the unit tests.
+ """
+
+ async def _fake_anchor(session, repo_id, question_ids, hits, **_kw):
+ return hits, {
+ "union": {
+ "Store": [
+ {
+ "name": "Store",
+ "file_path": path,
+ "start_line": 1,
+ "end_line": end_line or _E2E_TOTAL_LINES,
+ }
+ ]
+ },
+ "qualified_miss": [],
+ }
+
+ monkeypatch.setattr(answer_mod, "_anchor_symbol_hits", _fake_anchor)
+
+
+def _setup_repo(monkeypatch, tmp_path):
+ import repowise.server.mcp_server as mcp_mod
+
+ (tmp_path / "store.py").write_text(_E2E_SRC, encoding="utf-8")
+ monkeypatch.setattr(mcp_mod, "_repo_path", str(tmp_path))
+ return tmp_path
+
+
+async def test_truncated_body_exposes_what_it_withheld(setup_mcp, monkeypatch, tmp_path):
+ """FAILS on bd577942: the payload flags truncation but never says what was cut."""
+ import repowise.server.mcp_server.tool_answer.answer as answer_mod
+ from repowise.server.mcp_server import get_answer
+
+ _setup_repo(monkeypatch, tmp_path)
+ _patch_pipeline(monkeypatch, answer_mod, symbol=_TRUNCATED_CLASS)
+ _patch_provider(monkeypatch, answer_mod, "Store.write delegates to a helper.")
+
+ result = await get_answer("How does Store.write handle ids?")
+ bodies = result.get("symbol_bodies") or []
+ truncated = [b for b in bodies if b.get("truncated")]
+ assert truncated, "expected a truncated body in this fixture"
+ assert any(b.get("withheld_symbols") for b in truncated), (
+ "a truncated body must name what it withheld so the consumer can "
+ "continue inside the tool; got no withheld_symbols"
+ )
+
+
+async def test_question_naming_withheld_symbol_drops_confidence(
+ setup_mcp, monkeypatch, tmp_path
+):
+ """FAILS on bd577942. No exclusivity token anywhere, so gate 7 stays silent.
+
+ This is the reproducible shape: the response depends on a symbol whose body
+ was withheld, the prose contains no exclusivity language, and confidence
+ reads `high` regardless.
+ """
+ import repowise.server.mcp_server.tool_answer.answer as answer_mod
+ from repowise.server.mcp_server import get_answer
+
+ _setup_repo(monkeypatch, tmp_path)
+ _patch_pipeline(monkeypatch, answer_mod, symbol=_TRUNCATED_CLASS)
+ # The answer must NAME the symbol: `_gather_body_candidates` only inlines a
+ # body when `name in answer_text`, so a mock answer that never says "Store"
+ # serves no symbol_bodies and the gate has nothing to inspect. The answer
+ # deliberately contains NO exclusivity token.
+ _patch_provider(monkeypatch, answer_mod, "Store normalises ids while writing todos.")
+
+ # Names `Store` STANDALONE as well as `_validate`: a dotted
+ # `Store._validate` resolves to `_validate`, which is not a hydrated symbol
+ # here, so the question would serve no body at all.
+ result = await get_answer("In Store, why does _validate stamp a placeholder id?")
+ assert result["confidence"] != "high", (
+ "confidence must not be high when the question names a symbol whose "
+ f"body the payload withheld; got {result['confidence']!r}"
+ )
+
+
+async def test_high_confidence_note_never_says_skip_rereading_when_truncated(
+ setup_mcp, monkeypatch, tmp_path
+):
+ """FAILS on bd577942: the high note says 'do not re-read the source'."""
+ import repowise.server.mcp_server.tool_answer.answer as answer_mod
+ from repowise.server.mcp_server import get_answer
+
+ _setup_repo(monkeypatch, tmp_path)
+ _patch_pipeline(monkeypatch, answer_mod, symbol=_TRUNCATED_CLASS)
+ _patch_provider(monkeypatch, answer_mod, "Store.write delegates to a helper.")
+
+ result = await get_answer("How does Store.write work?")
+ note = (result.get("note") or "").lower()
+ assert "do not re-read" not in note, (
+ "the payload withheld part of a cited body, so it must not tell the "
+ f"consumer to skip re-reading; got: {note!r}"
+ )
+
+
+# ---------------------------------------------------------------------------
+# The homonym-union path: the NO-LLM case.
+#
+# This payload is built and returned BEFORE the provider is resolved, so it is
+# what a keyless deployment gets, and it reaches none of the confidence gates.
+# It used to hardcode confidence="high" with "no verification Read" even when a
+# body arrived truncated, and its note asserts "this is the complete set" --
+# an exclusivity claim we generate ourselves rather than one a model wrote.
+# ---------------------------------------------------------------------------
+
+
+class TestUnionPathTruncation:
+ def test_union_bodies_carry_withheld_symbols(self, tmp_path) -> None:
+ """FAILS before: the union path flagged truncation without naming it."""
+ from repowise.server.mcp_server.tool_answer.symbols import (
+ build_homonym_union_bodies,
+ )
+
+ (tmp_path / "store.py").write_text(_E2E_SRC, encoding="utf-8")
+ groups = {
+ "Store": [
+ {
+ "name": "Store",
+ "file_path": "store.py",
+ "start_line": 1,
+ "end_line": _E2E_TOTAL_LINES,
+ }
+ ]
+ }
+ bodies, _more = build_homonym_union_bodies(tmp_path, groups)
+ truncated = [b for b in bodies if b.get("truncated")]
+ assert truncated, "fixture must truncate for this test to mean anything"
+ assert any(b.get("withheld_symbols") for b in truncated), (
+ "the no-LLM union path must name what it withheld; it reaches no "
+ "confidence gate, so the payload is the only place doubt can surface"
+ )
+
+ def test_confidence_helper_fires_on_union_bodies_without_prose(self) -> None:
+ """The union path has no synthesised prose at all, by construction.
+
+ A prose-conditioned check is inert here. The question route is the only
+ one that can carry this path, which is the whole reason the gate is not
+ keyed on the answer text alone.
+ """
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ bodies = [
+ {
+ "truncated": True,
+ "withheld_symbols": [
+ {"name": "_validate", "symbol_id": "store.py::_validate",
+ "body_continues": True}
+ ],
+ }
+ ]
+ assert implicated_withheld_symbols(
+ "why does _validate stamp a placeholder id?", "", bodies
+ ) == ["_validate"]
+
+ @pytest.mark.asyncio
+ async def test_union_answer_over_a_truncated_body_is_not_high(
+ self, setup_mcp, monkeypatch, tmp_path
+ ):
+ """FAILS on 767391dd, and this is the blocker that review found.
+
+ The union path is reached only for naming/lookup questions about the
+ homonym, so the question names the SERVED symbol while the withheld
+ symbols are its inner members, and there is no answer prose either. A
+ dependency-keyed gate is therefore dead code here -- the same
+ conjunction defect PR #1445 had, one layer down. The bodies ARE the
+ answer on this path, so truncation alone has to cap it.
+
+ The `grounding` assertion is deliberate: without it this test would keep
+ passing if the pipeline stopped taking the union path at all, which is
+ exactly how the previous version of this file missed the defect.
+ """
+ import repowise.server.mcp_server.tool_answer.answer as answer_mod
+ from repowise.server.mcp_server import get_answer
+
+ _setup_repo(monkeypatch, tmp_path)
+ _patch_pipeline(monkeypatch, answer_mod, symbol=_TRUNCATED_CLASS)
+ _patch_union(monkeypatch, answer_mod)
+
+ result = await get_answer("where is Store defined?")
+ assert result.get("grounding") == "exact_symbol", (
+ f"this test only means something on the union path; got {result!r}"
+ )
+ assert any(b.get("truncated") for b in result["symbol_bodies"]), (
+ "the fixture must truncate or there is nothing to gate on"
+ )
+ assert result["confidence"] != "high", (
+ "a union answer whose cited body was cut must not read high; got "
+ f"{result['confidence']!r}"
+ )
+
+ @pytest.mark.asyncio
+ async def test_union_note_never_says_no_verification_read_when_truncated(
+ self, setup_mcp, monkeypatch, tmp_path
+ ):
+ """FAILS on bd577942: the union note said 'no verification Read'."""
+ import repowise.server.mcp_server.tool_answer.answer as answer_mod
+ from repowise.server.mcp_server import get_answer
+
+ _setup_repo(monkeypatch, tmp_path)
+ _patch_pipeline(monkeypatch, answer_mod, symbol=_TRUNCATED_CLASS)
+ _patch_union(monkeypatch, answer_mod)
+
+ result = await get_answer("where is Store defined?")
+ assert result.get("grounding") == "exact_symbol"
+ assert "no verification read" not in (result.get("note") or "").lower()
+
+ @pytest.mark.asyncio
+ async def test_union_answer_over_whole_bodies_stays_high(
+ self, setup_mcp, monkeypatch, tmp_path
+ ):
+ """The other direction: nothing withheld, so `high` survives.
+
+ Without this the cap could be an unconditional demotion and every test
+ above would still pass.
+ """
+ import repowise.server.mcp_server.tool_answer.answer as answer_mod
+ from repowise.server.mcp_server import get_answer
+
+ _setup_repo(monkeypatch, tmp_path)
+ _patch_pipeline(monkeypatch, answer_mod, symbol=_TRUNCATED_CLASS)
+ # A short definition, served whole.
+ _patch_union(monkeypatch, answer_mod, end_line=4, path="small.py")
+ (tmp_path / "small.py").write_text(
+ "class Store:\n def go(self):\n return 1\n\n", encoding="utf-8"
+ )
+
+ result = await get_answer("where is Store defined?")
+ assert result.get("grounding") == "exact_symbol"
+ assert not any(b.get("truncated") for b in result["symbol_bodies"])
+ assert result["confidence"] == "high"
+ assert "no verification read" in (result.get("note") or "").lower()
+
+
+# ---------------------------------------------------------------------------
+# The question route must not collapse confidence on ordinary English.
+#
+# Symbols named `on`, `line`, `input`, `write`, `main`, `get` and `run` are
+# everywhere, and a bare `\bname\b` match against the question demotes every
+# question that happens to use the word. The route that carries the no-LLM mode
+# is the one that must not over-fire.
+# ---------------------------------------------------------------------------
+
+_ENGLISH_COLLISIONS = [
+ ("on", "which handler runs on startup?"),
+ ("line", "how is each line of the log parsed?"),
+ ("input", "where does user input get validated?"),
+ ("write", "how do I write a new provider?"),
+ ("main", "what is the main entry point?"),
+ ("get", "how do I get the repo id?"),
+ ("run", "how does a run get scheduled?"),
+ ("width", "what sets the width of the output?"),
+ ("join", "where do the two tables join?"),
+]
+
+
[email protected]("name,question", _ENGLISH_COLLISIONS)
+def test_english_word_symbol_does_not_collapse_confidence(name, question) -> None:
+ """FAILS on 767391dd for every row: the question route was a bare \\b match."""
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ bodies = [{"truncated": True, "withheld_symbols": [{"name": name}]}]
+ assert implicated_withheld_symbols(question, "", bodies) == [], (
+ f"a symbol named {name!r} must not be implicated by ordinary prose"
+ )
+
+
+ "name,question",
+ [
+ # Distinctive shapes: underscore, internal capital, digit.
+ ("_validate", "why does _validate stamp a placeholder id?"),
+ ("TodoStore", "what does TodoStore keep?"),
+ ("dedupe_by_id", "what does dedupe_by_id drop?"),
+ ("sha256", "where is sha256 computed?"),
+ # Leading capital matches only with its own case.
+ ("Store", "In Store, why is the id blank?"),
+ # An English-word symbol still fires when named as code.
+ ("write", "how does Store.write handle ids?"),
+ ("run", "what does run() return?"),
+ ("main", "what does `main` do first?"),
+ ],
+)
+def test_question_route_still_fires_when_the_question_means_the_symbol(
+ name, question
+) -> None:
+ """The other direction: the guard must not turn the route off entirely."""
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ bodies = [{"truncated": True, "withheld_symbols": [{"name": name}]}]
+ assert implicated_withheld_symbols(question, "", bodies) == [name]
+
+
+def test_leading_capital_name_is_not_matched_as_lowercase_english() -> None:
+ from repowise.server.mcp_server.tool_answer.confidence import (
+ implicated_withheld_symbols,
+ )
+
+ bodies = [{"truncated": True, "withheld_symbols": [{"name": "Store"}]}]
+ assert implicated_withheld_symbols("where do we store the ids?", "", bodies) == []
+
+
+def test_backticked_path_does_not_implicate_its_extension() -> None:
+ """`store.py` is a path, not an attribute access on a symbol named `py`."""
+ from repowise.server.mcp_server.tool_answer.confidence import _code_reference
+
+ assert _code_reference("see `store.py` for this", "py") is False
+ assert _code_reference("see `store.py` for this", "store") is False
+ assert _code_reference("`Store._validate` stamps it", "_validate") is True
+
+
+def test_prose_parenthetical_is_not_a_call() -> None:
+ """FAILS on 767391dd: `\\bwidth\\s*\\(` matched 'the width (in pixels)'."""
+ from repowise.server.mcp_server.tool_answer.confidence import _code_reference
+
+ assert _code_reference("the width (in pixels) is fixed", "width") is False
+ assert _code_reference("it calls width(px) first", "width") is True
+
+
+# ---------------------------------------------------------------------------
+# body_continues must be earned, not asserted.
+# ---------------------------------------------------------------------------
+
+
+def test_a_wholly_served_symbol_is_not_reported_as_continuing(tmp_path) -> None:
+ """FAILS on 767391dd: the backward scan took the nearest preceding def.
+
+ `alpha` ends at line 2 and the cut is at line 5, so `alpha` was served
+ whole. It sorts FIRST in the note, so a false positive here becomes the
+ headline name and the `get_symbol id=` the agent is told to call.
+ """
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "a.py").write_text(
+ "def alpha():\n return 1\n\n\ndef beta():\n return 2\n", encoding="utf-8"
+ )
+ got = withheld_definitions(tmp_path, "a.py:5-6")
+ assert [d["name"] for d in got] == ["beta"], got
+ assert not any(d.get("body_continues") for d in got)
+
+
+def test_an_enclosing_symbol_that_does_reach_the_cut_is_reported(tmp_path) -> None:
+ """The other direction: the class body genuinely spans the boundary."""
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "b.py").write_text(
+ "class Store:\n"
+ " def head(self):\n"
+ " return 1\n"
+ " def tail(self):\n"
+ " return 2\n",
+ encoding="utf-8",
+ )
+ got = withheld_definitions(tmp_path, "b.py:4-5")
+ cut = [d for d in got if d.get("body_continues")]
+ assert [d["name"] for d in cut] == ["Store"], got
+
+
+# ---------------------------------------------------------------------------
+# The scanner has to work on the languages the 120-line cap actually bites.
+# ---------------------------------------------------------------------------
+
+
+def test_typescript_definitions_are_found(tmp_path) -> None:
+ """FAILS on 767391dd: the regex was `def|class`, so TS yielded nothing."""
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "c.ts").write_text(
+ "export class Panel {\n"
+ " render(props: P): JSX.Element {\n"
+ " if (props.x) {\n"
+ " return null;\n"
+ " }\n"
+ " return null;\n"
+ " }\n"
+ "}\n"
+ "export function useThing(a: number) {\n"
+ " return a;\n"
+ "}\n"
+ "export const Widget = (props: P) => {\n"
+ " return null;\n"
+ "};\n"
+ "export interface Shape { x: number }\n",
+ encoding="utf-8",
+ )
+ got = withheld_definitions(tmp_path, "c.ts:3-15")
+ names = [d["name"] for d in got]
+ assert "useThing" in names and "Widget" in names and "Shape" in names, names
+ # `render`'s body is cut by the boundary, so it leads.
+ assert got[0]["name"] == "render" and got[0]["body_continues"] is True
+ # Control flow is not a definition.
+ assert "if" not in names
+
+
+def test_go_definitions_are_found(tmp_path) -> None:
+ """FAILS on 767391dd. A receiver method's name follows the receiver."""
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "d.go").write_text(
+ "package main\n"
+ "\n"
+ "func (s *Store) Write(x int) error {\n"
+ "\tfor i := 0; i < x; i++ {\n"
+ "\t\ts.n++\n"
+ "\t}\n"
+ "\treturn nil\n"
+ "}\n"
+ "\n"
+ "type Config struct {\n"
+ "\tN int\n"
+ "}\n",
+ encoding="utf-8",
+ )
+ got = withheld_definitions(tmp_path, "d.go:5-12")
+ names = [d["name"] for d in got]
+ assert "Config" in names, names
+ assert got[0]["name"] == "Write" and got[0]["body_continues"] is True
+ assert "for" not in names
+
+
+def test_java_members_are_found(tmp_path) -> None:
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "e.java").write_text(
+ "public class Runner {\n"
+ " public static void main(String[] args) {\n"
+ " System.out.println(1);\n"
+ " }\n"
+ " private int helper(int a) {\n"
+ " return a;\n"
+ " }\n"
+ "}\n",
+ encoding="utf-8",
+ )
+ names = [d["name"] for d in withheld_definitions(tmp_path, "e.java:3-8")]
+ assert "helper" in names, names
+
+
+def test_a_callback_argument_is_not_a_definition(tmp_path) -> None:
+ """`describe("x", () => {` matches the brace-member shape by accident."""
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "f.test.ts").write_text(
+ "describe('outer', () => {\n"
+ " describe('inner', () => {\n"
+ " it('works', () => {\n"
+ " expect(appended).toMatchObject({ kind: 'trail' });\n"
+ " const rows = all.filter((p) => p.ok);\n"
+ " });\n"
+ " });\n"
+ "});\n",
+ encoding="utf-8",
+ )
+ names = [d["name"] for d in withheld_definitions(tmp_path, "f.test.ts:2-8")]
+ assert names == [], names
+
+
+def test_a_multiline_signature_still_reaches_the_cut(tmp_path) -> None:
+ """The live miss: the closing paren of a multi-line def sits at column 0.
+
+ Found on repowise's own `get_answer`, not in a fixture. Its signature ends
+ `) -> dict:` at column 0, so a running-minimum-indent walk hit zero on the
+ signature's own tail and gave up two lines short of the `async def` -- the
+ payload went out with `truncated: true` and no `withheld_symbols` at all.
+ """
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "h.py").write_text(
+ "async def wide(\n"
+ " a: str,\n"
+ " b: int = 1,\n"
+ ") -> dict:\n"
+ " x = 1\n"
+ " y = 2\n"
+ " return {}\n",
+ encoding="utf-8",
+ )
+ got = withheld_definitions(tmp_path, "h.py:6-7")
+ assert [d["name"] for d in got] == ["wide"], got
+ assert got[0]["body_continues"] is True
+
+
+def test_definitions_inside_a_docstring_are_not_reported(tmp_path) -> None:
+ """FAILS on 767391dd: it matched raw text, so an example became a symbol_id.
+
+ The id it produced (`f.py::fake_one`) resolves to nothing, and repowise's
+ own docstrings are full of exactly this shape.
+ """
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "f.py").write_text(
+ "def real(x):\n"
+ ' """Example.\n'
+ "\n"
+ " def fake_one():\n"
+ " pass\n"
+ "\n"
+ " class FakeTwo:\n"
+ " pass\n"
+ ' """\n'
+ " return x\n"
+ "\n"
+ "def also_real():\n"
+ " return 2\n",
+ encoding="utf-8",
+ )
+ names = [d["name"] for d in withheld_definitions(tmp_path, "f.py:3-13")]
+ assert "fake_one" not in names and "FakeTwo" not in names, names
+ assert "also_real" in names, names
+
+
+def test_a_one_line_body_does_not_bleed_into_the_signature(tmp_path) -> None:
+ """FAILS on 767391dd: `def go(self): pass` never ends with ':'."""
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "g.py").write_text(
+ "class K:\n def go(self): pass\n def after(self): pass\n",
+ encoding="utf-8",
+ )
+ sigs = [d["signature"] for d in withheld_definitions(tmp_path, "g.py:2-3")]
+ assert sigs == ["class K:", "def go(self): pass", "def after(self): pass"], sigs
+
+
+# ---------------------------------------------------------------------------
+# Cache and rationale
+# ---------------------------------------------------------------------------
+
+
+# ---------------------------------------------------------------------------
+# Scanner PRECISION. Every case below was found by an adversarial sweep over
+# real source, not by writing a fixture: an entry that is not a definition hands
+# the agent a `symbol_id` that resolves to nothing, and if its name happens to
+# be code-shaped it also demotes a correct answer.
+# ---------------------------------------------------------------------------
+
+
+ "line",
+ [
+ " function_name: Mapped[str] = mapped_column(String)", # 507 of these
+ " functionCall(x)",
+ "export functionality = 1",
+ " functions are called in sequence", # docstring prose
+ ],
+)
+def test_the_function_keyword_is_not_matched_as_an_identifier_prefix(line) -> None:
+ """`function` needs a separator, or it splits any longer identifier."""
+ from repowise.server.mcp_server.tool_answer.symbols import _match_definition
+
+ m = _match_definition(line)
+ assert m is None, f"{line!r} -> {m.group('name') if m else None}"
+
+
+ "line,expected",
+ [
+ ("export function useThing(a) {", "useThing"),
+ ("export function* gen() {", "gen"),
+ ("export default async function run() {", "run"),
+ ],
+)
+def test_real_function_declarations_still_match(line, expected) -> None:
+ from repowise.server.mcp_server.tool_answer.symbols import _match_definition
+
+ m = _match_definition(line)
+ assert m is not None and m.group("name") == expected
+
+
+ "line",
+ [
+ ' raise ValueError(f"repo (no .git found): {resolved}")',
+ " return dict(a=(1), b={})",
+ ' it("works", function () {',
+ ' test("x", async function () {',
+ ],
+)
+def test_a_statement_is_not_a_member_declaration(line) -> None:
+ """The optional type group swallows the keyword, hiding it from a name guard.
+
+ `raise ValueError(...)` reported a symbol `ValueError`, which is distinctive
+ enough that a question saying "why does this raise a ValueError?" then
+ demoted a correct answer.
+ """
+ from repowise.server.mcp_server.tool_answer.symbols import _match_definition
+
+ m = _match_definition(line)
+ assert m is None, f"{line!r} -> {m.group('name') if m else None}"
+
+
+ "line,nxt",
+ [
+ # Rust's `fn <name>` shape, but this is Python comparing a variable.
+ (" fn is not None", " and fn.text is not None"),
+ # An argument on its own line whose successor happens to be a dict
+ # literal, which looks exactly like an Allman brace.
+ (" bool(matched_nums),", " {"),
+ (" text(sql),", " {"),
+ ],
+)
+def test_a_parse_accident_is_not_a_definition(line, nxt) -> None:
+ from repowise.server.mcp_server.tool_answer.symbols import _match_definition
+
+ m = _match_definition(line, nxt)
+ assert m is None, f"{line!r} -> {m.group('name') if m else None}"
+
+
+def test_a_cut_landing_on_a_multiline_signature_still_finds_the_symbol(
+ tmp_path,
+) -> None:
+ """The anchor has to obey the same exclusions as the walk.
+
+ Found on the real `get_risk` MCP entry point. When the cut lands ON the
+ `) -> dict:` line, an anchor that reads the first non-blank withheld line
+ flatly sees column 0, the walk dies immediately, and the payload ships
+ `truncated: true` with NO withheld_symbols -- gate 8 inert on exactly the
+ long multi-line-signature entry points that truncate in the first place.
+ """
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "r.py").write_text(
+ "async def wide(\n"
+ " a: str,\n"
+ ") -> dict:\n"
+ " x = 1\n"
+ " return {}\n",
+ encoding="utf-8",
+ )
+ got = withheld_definitions(tmp_path, "r.py:3-5") # cut ON the ') -> dict:' line
+ assert [d["name"] for d in got] == ["wide"], got
+ assert got[0]["body_continues"] is True
+
+
+def test_a_cut_inside_a_flush_left_string_still_finds_the_symbol(tmp_path) -> None:
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "s.py").write_text(
+ 'def f():\n sql = """\nSELECT 1\n"""\n', encoding="utf-8"
+ )
+ got = withheld_definitions(tmp_path, "s.py:3-4")
+ assert [d["name"] for d in got] == ["f"], got
+
+
+def test_allman_braces_are_found(tmp_path) -> None:
+ """The C# default, and it used to yield nothing in both directions."""
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "k.cs").write_text(
+ "class K\n"
+ "{\n"
+ " void Alpha()\n"
+ " {\n"
+ " X();\n"
+ " }\n"
+ " void Beta()\n"
+ " {\n"
+ " Y();\n"
+ " }\n"
+ "}\n",
+ encoding="utf-8",
+ )
+ got = withheld_definitions(tmp_path, "k.cs:5-11")
+ assert got[0]["name"] == "Alpha" and got[0]["body_continues"] is True
+ assert "Beta" in [d["name"] for d in got], got
+
+
+def test_a_top_level_c_function_is_found(tmp_path) -> None:
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "m.c").write_text(
+ "#include <stdio.h>\n"
+ 'int main(int argc, char **argv) {\n'
+ ' puts("x");\n'
+ " return 0;\n"
+ "}\n",
+ encoding="utf-8",
+ )
+ got = withheld_definitions(tmp_path, "m.c:3-5")
+ assert [d["name"] for d in got] == ["main"], got
+
+
+def test_repeated_names_do_not_eat_the_cap(tmp_path) -> None:
+ """A minified line reported the same name once per line and burned the cap."""
+ from repowise.server.mcp_server.tool_answer.symbols import withheld_definitions
+
+ (tmp_path / "z.js").write_text(
+ "function a(){}\nfunction a(){}\nfunction a(){}\n", encoding="utf-8"
+ )
+ assert [d["name"] for d in withheld_definitions(tmp_path, "z.js:1-3")] == ["a"]
+
+
+ "name,question,expected",
+ [
+ # A sentence-initial capital before a determiner is an imperative verb.
+ ("Run", "Run the indexer, then what?", False),
+ ("Update", "Update the schema how?", False),
+ ("Store", "Store the results where?", False),
+ # ...but a sentence-initial capital is still the symbol otherwise.
+ ("Store", "Store keeps what?", True),
+ ("Store", "In Store, why is the id blank?", True),
+ ],
+)
+def test_sentence_initial_capital_is_only_excluded_as_an_imperative(
+ name, question, expected
+) -> None:
+ from repowise.server.mcp_server.tool_answer.confidence import _question_names
+
+ assert _question_names(question, name) is expected
+
+
+def test_the_string_mask_is_cached(tmp_path) -> None:
+ """The union path masks the same file once per truncated body."""
+ from repowise.server.mcp_server.tool_answer.symbols import _string_masked_lines
+
+ lines = tuple(f"line {i}" for i in range(200))
+ _string_masked_lines.cache_clear()
+ _string_masked_lines(lines)
+ _string_masked_lines(lines)
+ assert _string_masked_lines.cache_info().hits >= 1
+
+
+def test_answer_schema_version_was_bumped_for_this_change() -> None:
+ """FAILS on 767391dd, and a stale row serves the old `high` for 14 days.
+
+ Cache replay returns before symbol_bodies is built, so without a bump every
+ previously-cached answer keeps both the old confidence and the old "do not
+ re-read the source" note -- the two things this change exists to alter.
+ """
+ from repowise.server.mcp_server.tool_answer.config import _ANSWER_SCHEMA_VERSION
+
+ assert _ANSWER_SCHEMA_VERSION >= 13
+
+
+async def test_high_note_no_longer_cites_the_answers_own_directness(
+ setup_mcp, monkeypatch, tmp_path
+):
+ """FAILS on bd577942, and the clause it removes had no coverage at all.
+
+ _SYSTEM_PROMPT instructs the model not to hedge, so citing "the answer is
+ direct (no hedging)" as evidence FOR confidence reads the pipeline's own
+ mandate back as a signal.
+ """
+ import repowise.server.mcp_server.tool_answer.answer as answer_mod
+ from repowise.server.mcp_server import get_answer
+
+ _setup_repo(monkeypatch, tmp_path)
+ _patch_pipeline(monkeypatch, answer_mod, symbol=_WHOLE_SYMBOL)
+ _patch_provider(monkeypatch, answer_mod, "Store keeps todos in a dict.")
+
+ result = await get_answer("what does Store keep?")
+ assert result["confidence"] == "high", (
+ f"this test only covers the high note; got {result['confidence']!r}"
+ )
+ note = (result.get("note") or "").lower()
+ assert "hedging" not in note and "is direct" not in note, note