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(mcp): answer 'what happens when' questions from the entry's call order (#2737)
Raghav Chamadiya3cf18874b020
diff --git a/packages/server/src/repowise/server/mcp_server/_answer_context.py b/packages/server/src/repowise/server/mcp_server/_answer_context.pyindex 1a4641e2b..aac4b1e7c 100644--- a/packages/server/src/repowise/server/mcp_server/_answer_context.py+++ b/packages/server/src/repowise/server/mcp_server/_answer_context.py@@ -215,6 +215,7 @@ async def build_structured_prelude(section headers):Top symbols by relevance: ...+ Calls made by <entry>, in source order: ...Recent significant commits: ...Decision records: ...@@ -228,6 +229,11 @@ async def build_structured_prelude(if top_symbols:sections.append(f"Top symbols by relevance: {top_symbols}")+ # Read from the call graph, so the order is a fact, not a guess.+ sequence = next((h["_call_sequence"] for h in hits if h.get("_call_sequence")), "")+ if sequence:+ sections.append(sequence)+commits_line = await _recent_commits_summary(hits, ctx, repo_id)if commits_line:sections.append(f"Recent significant commits: {commits_line}")diff --git a/packages/server/src/repowise/server/mcp_server/_entry_trace.py b/packages/server/src/repowise/server/mcp_server/_entry_trace.pynew file mode 100644index 000000000..aa27211ec--- /dev/null+++ b/packages/server/src/repowise/server/mcp_server/_entry_trace.py@@ -0,0 +1,327 @@+"""Call order for "what happens when" questions in ``get_answer``.++A sequence question ("what happens when a user runs X", "walk me through Y",+"step by step") is answered by one entry function and the calls it makes, in+order. Retrieval lands near that function but ranks files by vocabulary, so a+file that merely says "step" or "order" can take the top slot and the answer+guesses the sequence.++This stage picks the entry among the functions defined in the retrieved files:+the one whose name carries the question's rarest words (a word most candidates+share says little), preferring the one that calls the other matches (it is+upstream of them) and then the one with the most calls. Its callees come from the call graph in call-site order, one level deep+plus the steps of its heaviest callees, and reach synthesis as a call-order+line. The entry's file leads the hits and the files of its heaviest callees+join them, so the prose and the bodies behind each step are both served.+"""++from __future__ import annotations++import json+import re+from typing import Any++from sqlalchemy import select++from repowise.core.analysis.execution_graph import (+ is_excluded_execution_path,+ is_walkable_execution_edge,+)+from repowise.core.ingestion.models import EXECUTION_EDGE_TYPES+from repowise.core.persistence.models import GraphEdge, GraphNode, Page+from repowise.server.mcp_server._answer_pipeline import is_subsystem_query+from repowise.server.mcp_server._graph_files import node_to_file+from repowise.server.mcp_server._helpers import is_excluded+from repowise.server.mcp_server._query_terms import content_terms+from repowise.server.mcp_server.tool_search_symbols import _tokens++_SEQUENCE_RE = re.compile(+ r"\bwhat happens\b|\bstep[- ]by[- ]step\b|\bwalk (?:me )?through\b"+ r"|\bin (?:what|which) order\b|\bsequence of\b|\bcall (?:chain|path|sequence|order)\b"+ r"|\bend[- ]to[- ]end\b|\blifecycle of\b",+ re.IGNORECASE,+)++# Words that describe the question's shape, never the code it asks about.+_SHAPE_WORDS = frozenset(+ {+ "happen",+ "happens",+ "step",+ "steps",+ "walk",+ "order",+ "sequence",+ "call",+ "calls",+ "called",+ "function",+ "functions",+ "flow",+ "lifecycle",+ "end",+ "user",+ "users",+ }+)+# A span the question quotes as code (`tool serve`) names the thing asked about.+_QUOTED_RE = re.compile("`([^`]+)`")++_ENTRY_KINDS = ("function", "method", "constructor")+_SEED_HITS = 8 # retrieved files the entry is looked for in+_MIN_CALLS = 2 # an entry that calls fewer has no sequence to tell+_MAX_STEPS = 10 # direct callees named, heaviest kept, then shown in call order+_EXPAND_STEPS = 2 # heaviest callees whose own steps are named too+_MAX_SUBSTEPS = 6+_MAX_INJECT = 2 # callee files placed right under the entry+_RANK_STEP = 0.01 # keeps the placed files in order without opening a dominance gap++_WALK_RE = re.compile(r"\bwalk (?:me )?through\b", re.IGNORECASE)++# "What happens if X fails" asks about a failure path, which the happy-path+# call order would answer wrongly.+_FAILURE_RE = re.compile(+ r"\bwhat happens (?:if|when)\b.*\b(?:fails?|failed|errors?|raises?|throws?|crash(?:es)?"+ r"|times? out|timeouts?|is missing|missing|goes down|is down|breaks?)\b",+ re.IGNORECASE,+)+++def is_sequence_question(question: str) -> bool:+ """True when the question asks what happens, in order, on the normal path.++ A subsystem overview ("walk me through the architecture of X") belongs to+ the parent-page stage, and a failure question to the failure path.+ """+ if not question or not _SEQUENCE_RE.search(question):+ return False+ # "Walk me through" alone reads as an overview to the parent-page stage; a+ # question is an overview here only when something else says so too.+ overview = is_subsystem_query(_WALK_RE.sub(" ", question))+ return not _FAILURE_RE.search(question) and not overview+++def _question_words(question: str) -> dict[str, float]:+ """Content words of the question, those it quotes as code counting double."""+ quoted = {w for span in _QUOTED_RE.findall(question) for w in _tokens(span)}+ words: dict[str, float] = {}+ for term in content_terms(question, max_terms=16):+ for word in _tokens(term):+ if word not in _SHAPE_WORDS and len(word) >= 3:+ words[word] = 2.0 if word in quoted else 1.0+ return words+++def _word_weights(names: list[str], words: dict[str, float]) -> dict[str, float]:+ """A question word's weight, divided by how many candidate names carry it.++ A word many names share says little about which one the question means;+ the named thing is usually rare among them.+ """+ counts = {w: sum(1 for name in names if w in _tokens(name)) for w in words}+ return {w: words[w] / n for w, n in counts.items() if n}+++def _name_score(name: str, weights: dict[str, float]) -> float:+ tokens = _tokens(name)+ return sum(weight for word, weight in weights.items() if word in tokens)+++async def _callees(+ session: Any, repo_id: str, sources: list[str]+) -> dict[str, list[tuple[int, str]]]:+ """``{source: [(first call line, target), ...]}`` over walkable execution edges."""+ if not sources:+ return {}+ res = await session.execute(+ select(+ GraphEdge.source_node_id,+ GraphEdge.target_node_id,+ GraphEdge.edge_type,+ GraphEdge.resolution_origin,+ GraphEdge.confidence,+ GraphEdge.call_lines_json,+ ).where(+ GraphEdge.repository_id == repo_id,+ GraphEdge.source_node_id.in_(sources),+ GraphEdge.edge_type.in_(sorted(EXECUTION_EDGE_TYPES)),+ )+ )+ out: dict[str, dict[str, int]] = {}+ for src, tgt, etype, origin, conf, lines_json in res.all():+ if not tgt or tgt == src or is_excluded_execution_path(node_to_file(tgt)):+ continue+ if not is_walkable_execution_edge(etype, origin, conf):+ continue+ try:+ lines = [int(n) for n in json.loads(lines_json or "[]")]+ except (TypeError, ValueError):+ lines = []+ first = min(lines) if lines else 1 << 30+ seen = out.setdefault(src, {})+ seen[tgt] = min(first, seen.get(tgt, first))+ return {+ src: sorted((line, tgt) for tgt, line in targets.items()) for src, targets in out.items()+ }+++async def _fanout(session: Any, repo_id: str, node_ids: list[str]) -> dict[str, int]:+ """Walkable calls per node: the same edges the trace would show."""+ calls = await _callees(session, repo_id, node_ids)+ return {node: len(targets) for node, targets in calls.items()}+++def _leaf(node_id: str) -> str:+ return node_id.rsplit("::", 1)[-1]+++def _heaviest_in_order(calls: list[tuple[int, str]], fanout: dict[str, int], cap: int) -> list[str]:+ """The *cap* callees with the most calls of their own, returned in call order."""+ ranked = sorted(calls, key=lambda c: (-fanout.get(c[1], 0), c[0]))[:cap]+ return [tgt for _line, tgt in sorted(ranked)]+++async def _choose_entry(+ session: Any, repo_id: str, seed_files: list[str], words: dict[str, float]+) -> tuple[str, dict[str, list[tuple[int, str]]]] | None:+ res = await session.execute(+ select(GraphNode.node_id, GraphNode.name, GraphNode.file_path).where(+ GraphNode.repository_id == repo_id,+ GraphNode.file_path.in_(seed_files),+ GraphNode.node_type == "symbol",+ GraphNode.kind.in_(_ENTRY_KINDS),+ GraphNode.is_test.is_(False),+ )+ )+ rows = [(node_id, name or "", path) for node_id, name, path in res.all()]+ weights = _word_weights([name for _id, name, _path in rows], words)+ file_rank = {path: i for i, path in enumerate(seed_files)}+ matches = {+ node_id: (score, file_rank.get(path, len(seed_files)))+ for node_id, name, path in rows+ if (score := _name_score(name, weights))+ }+ if not matches:+ return None+ calls = await _callees(session, repo_id, sorted(matches))++ def rank(node_id: str) -> tuple:+ score, file_pos = matches[node_id]+ own = calls.get(node_id, [])+ upstream = sum(1 for _line, tgt in own if tgt in matches)+ return (-score, -upstream, -len(own), file_pos, node_id)++ entry = min(matches, key=rank)+ if len(calls.get(entry, [])) < _MIN_CALLS:+ return None+ return entry, calls+++def _label(node_id: str) -> str:+ return f"{_leaf(node_id)} ({node_to_file(node_id)})"+++async def expand_via_entry_trace(+ session: Any, repo_id: str, hits: list[dict], question: str, exclude_spec: Any = None+) -> tuple[list[dict], list[list[str]]]:+ """Lead with the entry a sequence question names and serve its call order.++ No-op (``hits`` unchanged, ``[]``) unless the question is sequence-shaped+ and an entry with at least ``_MIN_CALLS`` calls is found in the retrieved+ files. Returns the hits and ``[[entry, step, ...]]`` for ``flow_path``.+ """+ if not hits or not is_sequence_question(question):+ return hits, []+ words = _question_words(question)+ seed_files: list[str] = []+ for h in hits[:_SEED_HITS]:+ path = node_to_file(h.get("target_path") or "")+ if path and path not in seed_files:+ seed_files.append(path)+ if not words or not seed_files:+ return hits, []++ chosen = await _choose_entry(session, repo_id, seed_files, words)+ if chosen is None:+ return hits, []+ entry, calls = chosen++ direct = calls[entry]+ fanout = await _fanout(session, repo_id, [tgt for _line, tgt in direct])+ steps = _heaviest_in_order(direct, fanout, _MAX_STEPS)+ heavy = sorted(steps, key=lambda s: -fanout.get(s, 0))[:_EXPAND_STEPS]+ heavy = [s for s in heavy if fanout.get(s, 0) >= _MIN_CALLS]+ sub_calls = await _callees(session, repo_id, heavy)+ sub_fanout = await _fanout(+ session, repo_id, [tgt for s in heavy for _line, tgt in sub_calls.get(s, [])]+ )++ parts = []+ for step in steps:+ label = _label(step)+ subs = _heaviest_in_order(sub_calls.get(step, []), sub_fanout, _MAX_SUBSTEPS)+ if subs:+ label += " [calls, in order: " + ", ".join(_leaf(s) for s in subs) + "]"+ parts.append(label)+ sequence = (+ f"Calls made by {_label(entry)}, in source order (a call may sit on a branch "+ "that does not always run): " + " -> ".join(parts)+ )++ entry_file = node_to_file(entry)+ inject = []+ for step in heavy:+ path = node_to_file(step)+ if path != entry_file and path not in inject and not is_excluded(path, exclude_spec):+ inject.append(path)+ inject = inject[:_MAX_INJECT]++ hits = await _place_files(session, repo_id, hits, entry_file, inject)+ entry_hit = next((h for h in hits if h.get("target_path") == entry_file), None)+ if entry_hit is None:+ # No readable page for the entry: nothing to carry the order to synthesis.+ return hits, []+ entry_hit["_call_sequence"] = sequence+ return hits, [[entry, *steps]]+++async def _place_files(+ session: Any, repo_id: str, hits: list[dict], entry_file: str, inject: list[str]+) -> list[dict]:+ """The entry's file first, then the callee files, then everything else."""+ top = max((h.get("score", 0.0) for h in hits), default=0.0)+ wanted: dict[str, float] = {}+ for i, path in enumerate([entry_file, *inject]):+ wanted.setdefault(path, top + _RANK_STEP * (len(inject) + 1 - i))+ by_path = {h.get("target_path"): h for h in hits if h.get("page_type") == "file_page"}+ absent = [p for p in wanted if p not in by_path]+ if absent:+ res = await session.execute(+ select(Page.target_path, Page.summary).where(+ Page.repository_id == repo_id,+ Page.target_path.in_(absent),+ Page.page_type == "file_page",+ Page.freshness_status != "tombstone",+ )+ )+ for path, summary in res.all():+ hit = {+ "page_id": f"file_page:{path}",+ "target_path": path,+ "title": f"File: {path}",+ "summary": summary or "",+ "snippet": (summary or "")[:200],+ "page_type": "file_page",+ "score": 0.0,+ "_sources": {"call_trace"},+ "_expanded_from": "call_trace",+ }+ hits.append(hit)+ by_path[path] = hit+ for path, score in wanted.items():+ hit = by_path.get(path)+ if hit is not None and hit.get("score", 0.0) < score:+ hit["score"] = score+ hit.setdefault("_sources", set()).add("call_trace")+ hits.sort(key=lambda h: h.get("score", 0.0), reverse=True)+ return hitsdiff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/answer.py b/packages/server/src/repowise/server/mcp_server/tool_answer/answer.pyindex 7b077c411..04f3a9cb8 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@@ -94,6 +94,9 @@ from repowise.server.mcp_server._answer_pipeline import hydrate_hits as _hydratefrom repowise.server.mcp_server._answer_pipeline import (retrieval_legs as _retrieval_legs,)+from repowise.server.mcp_server._entry_trace import (+ expand_via_entry_trace as _expand_via_entry_trace,+)from repowise.server.mcp_server._flow_path import expand_via_flow_path as _expand_via_flow_pathfrom repowise.server.mcp_server._helpers import (_get_exclude_spec,@@ -271,20 +274,31 @@ async def _run_retrieval_pipeline(# cannot. This and the stages below run before the cap so an injected file# can take a top-5 slot.flow_paths: list[list[str]] = []+ # A sequence question ("what happens when ...") is answered by the call+ # order of the entry it names; when that resolves, it is the flow served.with contextlib.suppress(Exception):async with get_session(ctx.session_factory) as session:- hits, flow_paths = await _expand_via_flow_path(- session, repo_id, hits, question, question_ids+ hits, flow_paths = await _expand_via_entry_trace(+ session, repo_id, hits, question, exclude_spec)+ traced = bool(flow_paths)+ if not traced:+ with contextlib.suppress(Exception):+ async with get_session(ctx.session_factory) as session:+ hits, flow_paths = await _expand_via_flow_path(+ session, repo_id, hits, question, question_ids+ )# Neighborhood re-rank: for flow questions whose target file is never named,# re-rank the 1-2 hop neighborhood of the top hits. No-op on other shapes.with contextlib.suppress(Exception):async with get_session(ctx.session_factory) as session:hits = await _expand_via_neighbor_rerank(session, repo_id, hits, question, ctx)# Parent-concept surfacing: a subsystem-shaped question leads with the- # rollup page for the subsystem. No-op on other shapes.- with contextlib.suppress(Exception):- hits = await _expand_via_parent_page(hits, question, ctx)+ # rollup page for the subsystem. No-op on other shapes, and skipped once a+ # call trace answers the question: its entry file must keep the lead.+ if not traced:+ with contextlib.suppress(Exception):+ hits = await _expand_via_parent_page(hits, question, ctx)# Demote noise (decisions on non-why, test pages on non-test questions)# below real pages. Non-dropping; after anchoring, which never injects noise.hits = _demote_noise_hits(hits, question, is_why=_is_why_question(question))diff --git a/tests/unit/server/mcp/test_entry_trace.py b/tests/unit/server/mcp/test_entry_trace.pynew file mode 100644index 000000000..643f13827--- /dev/null+++ b/tests/unit/server/mcp/test_entry_trace.py@@ -0,0 +1,200 @@+"""Sequence questions lead with the entry they name and serve its call order.++A small generic graph: a command handler ``serve_command`` calls, in source+order, ``load_settings``, ``build_app`` and ``start_server``; ``build_app``+(another file) calls three helpers of its own. A second ``serve``-named+function, ``serve_static``, is called by the handler's callee and so is+downstream of it. Retrieval ranked an unrelated file first.+"""++from __future__ import annotations++import json+from datetime import UTC, datetime++import pytest++from repowise.core.persistence.models import GraphEdge, GraphNode, Page+from repowise.server.mcp_server._entry_trace import (+ expand_via_entry_trace,+ is_sequence_question,+)++_NOW = datetime(2026, 3, 19, 12, 0, 0, tzinfo=UTC)+_CMD = "app/cli/serve.py"+_BUILD = "app/core/build.py"+_OTHER = "app/util/order_steps.py"+_Q = "What happens step by step when a user runs `tool serve`?"+++def _node(rid: str, node_id: str, *, kind: str = "function") -> GraphNode:+ path, name = node_id.split("::", 1)+ return GraphNode(+ id=f"n:{node_id}",+ repository_id=rid,+ node_id=node_id,+ node_type="symbol",+ kind=kind,+ name=name,+ file_path=path,+ language="python",+ symbol_count=0,+ is_test=False,+ is_entry_point=False,+ pagerank=0.0,+ betweenness=0.0,+ community_id=0,+ created_at=_NOW,+ )+++def _call(rid: str, src: str, tgt: str, line: int) -> GraphEdge:+ return GraphEdge(+ id=f"{src}->{tgt}",+ repository_id=rid,+ source_node_id=src,+ target_node_id=tgt,+ edge_type="calls",+ confidence=0.9,+ resolution_origin="import_scoped",+ call_lines_json=json.dumps([line]),+ created_at=_NOW,+ )+++def _page(rid: str, path: str) -> Page:+ return Page(+ id=f"file_page:{path}",+ repository_id=rid,+ page_type="file_page",+ title=f"File: {path}",+ content=f"# {path}",+ summary=f"summary of {path}",+ target_path=path,+ source_hash=path,+ model_name="mock",+ provider_name="mock",+ generation_level=2,+ created_at=_NOW,+ updated_at=_NOW,+ )+++async def _seed(session, rid: str) -> None:+ entry = f"{_CMD}::serve_command"+ build = f"{_BUILD}::build_app"+ nodes = [+ entry,+ f"{_CMD}::load_settings",+ f"{_CMD}::start_server",+ build,+ f"{_BUILD}::serve_static",+ f"{_BUILD}::mount_routes",+ f"{_BUILD}::add_middleware",+ f"{_OTHER}::order_steps",+ f"{_OTHER}::run_user_task",+ ]+ session.add_all([_node(rid, n) for n in nodes])+ session.add_all(+ [+ # Stored out of call order on purpose: the served order is by call site.+ _call(rid, entry, f"{_CMD}::start_server", 30),+ _call(rid, entry, f"{_CMD}::load_settings", 10),+ _call(rid, entry, build, 20),+ _call(rid, build, f"{_BUILD}::mount_routes", 50),+ _call(rid, build, f"{_BUILD}::serve_static", 40),+ _call(rid, build, f"{_BUILD}::add_middleware", 60),+ _call(rid, f"{_OTHER}::order_steps", f"{_CMD}::load_settings", 5),+ # Carries the question's framing words (``user``, ``runs``), not its subject.+ _call(rid, f"{_OTHER}::run_user_task", f"{_CMD}::load_settings", 5),+ _call(rid, f"{_OTHER}::run_user_task", f"{_CMD}::start_server", 6),+ _call(rid, f"{_OTHER}::run_user_task", f"{_OTHER}::order_steps", 7),+ ]+ )+ session.add_all([_page(rid, p) for p in (_CMD, _BUILD, _OTHER)])+ await session.flush()+++def _hits() -> list[dict]:+ return [+ {"target_path": _OTHER, "score": 3.0, "page_type": "file_page", "_sources": {"fts"}},+ {"target_path": _CMD, "score": 2.0, "page_type": "file_page", "_sources": {"vector"}},+ ]+++def test_the_gate_reads_sequence_shapes_only():+ assert is_sequence_question("What happens when a request comes in?")+ assert is_sequence_question("Walk me through how a job is scheduled")+ assert is_sequence_question("In what order are plugins loaded?")+ assert not is_sequence_question("Where is the retry limit configured?")+ assert not is_sequence_question("How does the cache key work?")+++def test_failure_and_subsystem_questions_are_not_call_order_questions():+ assert not is_sequence_question("What happens if the embedding call times out?")+ assert not is_sequence_question("What happens when the upload fails?")+ assert not is_sequence_question("Walk me through the architecture of the ingestion subsystem")+++async def test_the_named_entry_leads_and_its_calls_come_in_call_site_order(session, repo_id):+ await _seed(session, repo_id)++ hits, flows = await expand_via_entry_trace(session, repo_id, _hits(), _Q)++ assert flows == [+ [+ f"{_CMD}::serve_command",+ f"{_CMD}::load_settings",+ f"{_BUILD}::build_app",+ f"{_CMD}::start_server",+ ]+ ]+ # The entry's file first, then the file of its heaviest callee.+ assert [h["target_path"] for h in hits][:2] == [_CMD, _BUILD]+ sequence = hits[0]["_call_sequence"]+ assert sequence.index("load_settings") < sequence.index("build_app")+ assert (+ "build_app (app/core/build.py) [calls, in order: serve_static, mount_routes, add_middleware]"+ in sequence+ )+++async def test_a_question_of_another_shape_is_left_alone(session, repo_id):+ await _seed(session, repo_id)+ before = _hits()++ hits, flows = await expand_via_entry_trace(+ session, repo_id, _hits(), "Where is the serve command defined?"+ )++ assert flows == []+ assert [h["target_path"] for h in hits] == [h["target_path"] for h in before]+++async def test_no_entry_is_claimed_when_no_retrieved_function_carries_a_question_word(+ session, repo_id+):+ await _seed(session, repo_id)++ hits, flows = await expand_via_entry_trace(+ session, repo_id, _hits(), "What happens when a user uploads an avatar?"+ )++ assert flows == []+ assert hits[0]["target_path"] == _OTHER+++async def test_a_file_the_caller_excludes_is_not_injected(session, repo_id):+ import pathspec++ await _seed(session, repo_id)+ spec = pathspec.PathSpec.from_lines("gitwildmatch", ["app/core/"])++ hits, flows = await expand_via_entry_trace(session, repo_id, _hits(), _Q, spec)++ assert flows+ assert _BUILD not in [h["target_path"] for h in hits]