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(health): keep get_health's targeted analysis meta inside its session (#2670)

Raghav Chamadiya27045965c053
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/tool.py b/packages/server/src/repowise/server/mcp_server/tool_health/tool.py
index 958ad0c69..916c687be 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/tool.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/tool.py
@@ -211,26 +211,27 @@ async def get_health(
data = await load_health_data(
session, repository, reference_repository, ctx.path, req
)
+ # The rest stays inside the session: the analysis meta queries it, and a
+ # query on a closed session checks out a connection nothing returns.
+ if data.pop.scoped:
+ result, mode_totals = build_targeted(data, req, pager, ctx.path)
+ else:
+ result, mode_totals = build_dashboard(data, req, pager)
+ add_optional_blocks(result, data, req, pager, str(ctx.path))
+ result = _finish(result, data, req, pager, mode_totals)
- if data.pop.scoped:
- result, mode_totals = build_targeted(data, req, pager, ctx.path)
- else:
- result, mode_totals = build_dashboard(data, req, pager)
- add_optional_blocks(result, data, req, pager, str(ctx.path))
- result = _finish(result, data, req, pager, mode_totals)
-
- # Targeted mode scopes the stale signal to the asked-about files.
- result["_meta"] = _build_meta(repository=repository, targets=targets if targets else None)
- if data.pop.scoped:
- # Analysis freshness is repo-wide, so both modes report the same status.
- await _attach_repository_analysis_meta(session, repository, result["_meta"])
- else:
- _attach_health_analysis_meta(result["_meta"], data.pop.all_metrics)
- pager.report_omissions(result, omission_collector, reference_repository)
- omission_collector.attach(result)
- # Server-side wall clock, as ``get_context`` reports.
- result["_meta"]["timing_ms"] = round((perf_counter() - started) * 1000, 2)
- return result
+ # Targeted mode scopes the stale signal to the asked-about files.
+ result["_meta"] = _build_meta(repository=repository, targets=targets if targets else None)
+ if data.pop.scoped:
+ # Analysis freshness is repo-wide, so both modes report the same status.
+ await _attach_repository_analysis_meta(session, repository, result["_meta"])
+ else:
+ _attach_health_analysis_meta(result["_meta"], data.pop.all_metrics)
+ pager.report_omissions(result, omission_collector, reference_repository)
+ omission_collector.attach(result)
+ # Server-side wall clock, as ``get_context`` reports.
+ result["_meta"]["timing_ms"] = round((perf_counter() - started) * 1000, 2)
+ return result
def _finish(
diff --git a/tests/unit/server/mcp/test_health.py b/tests/unit/server/mcp/test_health.py
index bb8b8cf03..0f66d85ac 100644
--- a/tests/unit/server/mcp/test_health.py
+++ b/tests/unit/server/mcp/test_health.py
@@ -2622,3 +2622,21 @@ async def test_the_ranked_findings_leave_performance_out_but_asking_returns_it(
asked = await get_health(include=["performance"], only=["top_findings"])
assert any(f["dimension"] == "performance" for f in asked["top_findings"])
+
+
[email protected]("targets", [["does/not/exist.py"], ["src/db/models.py"]])
+async def test_a_targeted_read_returns_its_connection(setup_mcp, health_data, factory, targets):
+ """The analysis meta used to query a closed session, checking out a connection
+ nothing returned. A garbage collection then dropped it, and on the one-connection
+ test pool that dropped the whole in-memory database mid-test."""
+ import gc
+
+ from sqlalchemy import text
+
+ from repowise.server.mcp_server import get_health
+
+ await get_health(targets=targets, only=["metrics"])
+ gc.collect()
+ async with factory() as s:
+ assert (await s.execute(text("SELECT count(*) FROM repositories"))).scalar() == 1