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(test): stop two wall-clock assertions flaking on shared runners (#2385)

Nehal kanak kuniyil thottathilb409a5976bd0
diff --git a/packages/server/src/repowise/server/mcp_server/_meta.py b/packages/server/src/repowise/server/mcp_server/_meta.py
index 8621cf027..7edef142a 100644
--- a/packages/server/src/repowise/server/mcp_server/_meta.py
+++ b/packages/server/src/repowise/server/mcp_server/_meta.py
@@ -30,6 +30,7 @@ from repowise.core.index_scope import (
index_scope_fingerprint,
load_index_scope,
)
+from repowise.server.mcp_server._rounding import round_float
# 2: index_scope carries the compact projection on routine responses. The key
# and its version field are unchanged, so a consumer reading the old shape has
@@ -403,7 +404,17 @@ def build_meta(
"""
out: dict[str, Any] = {"contract_version": MCP_CONTRACT_VERSION}
if timing_ms is not None:
- out["timing_ms"] = round(float(timing_ms), 2)
+ # Through the shared quantizer, not ``round(..., 2)``. A wall-clock
+ # duration is a float like any other on this wire, and two decimal
+ # places is not the same rule the rest of the payload follows: a
+ # 1077.85 ms measurement survives ``round`` but the significant-digit
+ # rule wants 1078.0, so the field tripped the no-raw-doubles guard
+ # whenever the run happened to land off an integer. ``round_float``
+ # returns None for a non-finite measurement, which is not valid JSON,
+ # so the key is omitted rather than emitted as null.
+ rounded_ms = round_float(float(timing_ms))
+ if rounded_ms is not None:
+ out["timing_ms"] = rounded_ms
if hint:
out["hint"] = hint
if cached:
diff --git a/tests/unit/distill/test_rewrite_perf.py b/tests/unit/distill/test_rewrite_perf.py
index 8aac1e881..ecec54f86 100644
--- a/tests/unit/distill/test_rewrite_perf.py
+++ b/tests/unit/distill/test_rewrite_perf.py
@@ -97,6 +97,23 @@ def _p95(cmd: list[str], payload: str) -> tuple[float, list[str], list[float]]:
return median, outputs, timings
+def _median_best_of_two(cmd: list[str], payload: str) -> tuple[float, list[str], list[float]]:
+ """The ledgered median, re-measured once when the first run goes over.
+
+ A shared runner can lift a whole batch of subprocess spawns at once, which
+ is the runner rather than the hook getting slower. A real regression
+ repeats; a scheduling blip does not, so the better of two runs is the
+ honest number. The budget is unchanged and the failure message still shows
+ the full distribution from the run that is judged.
+ """
+ first = _p95(cmd, payload)
+ if first[0] >= _LEDGERED_BUDGET_MS:
+ second = _p95(cmd, payload)
+ if second[0] < first[0]:
+ return second
+ return first
+
+
#: Command shapes the hook must answer within budget. The pipeline and
#: quoted-operator rows are the ones that exercise the shell lexer end to
#: end: they are the shapes that used to short-circuit on a character scan
@@ -158,7 +175,7 @@ def test_a_ledgered_invocation_stays_under_budget(tmp_path: Path) -> None:
)
subprocess.run(cmd, input=payload, capture_output=True, text=True)
- median, outputs, timings = _p95(cmd, payload)
+ median, outputs, timings = _median_best_of_two(cmd, payload)
assert all("repowise distill --source hook-bash pytest -x" in out for out in outputs)
# Guard the guard: a budget met by not writing the row would pass forever.
db = tmp_path / ".repowise" / "sessions" / "sessions.db"