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): make three performance findings say only what the code shows (#4773)

Raghav Chamadiya69a9d54b9346
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/hot_path_sync_io.py b/packages/core/src/repowise/core/analysis/health/biomarkers/hot_path_sync_io.py
index c3eb29280..c5ef9958c 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/hot_path_sync_io.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/hot_path_sync_io.py
@@ -41,10 +41,15 @@ class HotPathSyncIoDetector:
continue
phrasing = _BOUNDARY_PHRASING.get(hit.detail, "a blocking I/O call")
details: dict[str, object] = {"boundary_kind": hit.detail}
- callers = ""
+ # The gate is "callers >= p80 of called functions" (perf.ranking), so
+ # the text states that, not "most-called": the bar can be 2 callers.
+ where = "a widely called function"
if hit.callers:
details["callers"] = hit.callers
- callers = f" ({hit.callers} direct callers)"
+ where = (
+ f"a function called from {hit.callers} places, at least as many "
+ "callers as 80% of called functions in this repo"
+ )
out.append(
BiomarkerResult(
biomarker_type=self.name,
@@ -54,8 +59,7 @@ class HotPathSyncIoDetector:
line_end=hit.line,
details=details,
reason=(
- f"{phrasing} in one of the most-called functions in "
- f"this repo{callers}; every call through it waits for the I/O"
+ f"{phrasing} in {where}; every call through it waits for the I/O"
),
)
)
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py
index 4ec8689ff..d7458271f 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py
@@ -192,6 +192,13 @@ _LOOP_KINDS: frozenset[str] = frozenset({"for_statement", "while_statement", "do
_LOCK_SCOPE_METHODS: frozenset[str] = frozenset({"synchronized", "withLock"})
+def _is_constant_string(node: Node | None) -> bool:
+ """A string literal with no template placeholder (``"^a$"``, not ``"$it.*"``)."""
+ if node is None or node.type not in _STRING_KINDS or node.text is None:
+ return False
+ return not _TEMPLATE_RE.search(node.text.decode("utf-8", "replace"))
+
+
def _decode(node: Node | None) -> str | None:
if node is None or node.text is None:
return None
@@ -578,9 +585,7 @@ class KotlinPerfDialect(IterableSources):
if fn is not None and fn.type == "navigation_expression"
else None
)
- return (
- "regex_compile_in_loop" if recv is not None and recv.type in _STRING_KINDS else None
- )
+ return "regex_compile_in_loop" if _is_constant_string(recv) else None
# Heavy clients to hoist, not rebuild per iteration. Kotlin has no
# ``new``, so a constructor is a bare call whose method IS the type name.
if method in JAVA_RESOURCE_CTORS and not self.callee_is_attribute(node):
@@ -613,10 +618,7 @@ class KotlinPerfDialect(IterableSources):
first = next((c for c in args.children if c.is_named), None)
if first is None:
return False
- inner = next((c for c in first.children if c.is_named), None)
- if inner is None or inner.type not in _STRING_KINDS or inner.text is None:
- return False
- return not _TEMPLATE_RE.search(inner.text.decode("utf-8", "replace"))
+ return _is_constant_string(next((c for c in first.children if c.is_named), None))
# -- what a loop walks (perf/dialects/iterables.py) ---------------------------
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/rust.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/rust.py
index 7a308abe3..3a0b2ad45 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/rust.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/rust.py
@@ -312,9 +312,11 @@ class RustPerfDialect(IterableSources):
# ``futures::executor::block_on`` / ``Handle::block_on`` inside async
# blocks the executor thread — the canonical sync-in-async smell.
return "block_on"
- if root in ("std", "thread") and method == "sleep":
+ # A method call's root is its receiver's leftmost name, so ``std`` would
+ # match ``std::io::stdin().read(..)`` (console IO, not ``fs::read``).
+ if root == "thread" and method == "sleep":
return "thread::sleep"
- if root in ("std", "fs") and method in RUST_SYNC_FS_METHODS:
+ if root == "fs" and method in RUST_SYNC_FS_METHODS:
return f"fs::{method}"
return None
diff --git a/packages/core/src/repowise/core/analysis/health/perf/gated.py b/packages/core/src/repowise/core/analysis/health/perf/gated.py
index bd68832b7..167f44a8f 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/gated.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/gated.py
@@ -85,8 +85,8 @@ def collect_centrality_gated(
Keyed by file path. For every function carrying the corresponding fact, a hit
is produced ONLY when ``ranker.is_hot(path, func_start)``, so a quadratic
- loop or a blocking sync sink ships only in one of the repo's most-called
- functions. Pure when no graph is available (nothing is hot, so no hits),
+ loop or a blocking sync sink ships only in a function with at least as many
+ callers as 80% of the repo's called functions. Pure when no graph is available (nothing is hot, so no hits),
which is the precision-first default. A fact whose function
sits inside Rust inline test code (``_in_rust_test_range``) is skipped
before the hotness check even runs — test code doing blocking I/O is
diff --git a/packages/ui/src/health/biomarker-glossary.ts b/packages/ui/src/health/biomarker-glossary.ts
index 942940fcb..68a46cb29 100644
--- a/packages/ui/src/health/biomarker-glossary.ts
+++ b/packages/ui/src/health/biomarker-glossary.ts
@@ -338,7 +338,7 @@ export const BIOMARKER_GLOSSARY: Record<string, BiomarkerInfo> = {
label: BIOMARKER_LABELS.hot_path_sync_io,
category: "performance",
description:
- "A blocking subprocess or filesystem call in one of the repo's most-called functions, even outside a loop. Every call through the function waits for it. Advisory: a latency signal ranked by call-graph centrality, not proof of a request path, and not always a defect. Tests, tooling, examples, and generated or vendored code are not flagged.",
+ "A blocking subprocess or filesystem call in a function with at least as many callers as 80% of the repo's called functions, even outside a loop. Every call through the function waits for it. Advisory: a latency signal ranked by call-graph centrality, not proof of a request path, and not always a defect. Tests, tooling, examples, and generated or vendored code are not flagged.",
},
blocking_io_under_lock: {
label: BIOMARKER_LABELS.blocking_io_under_lock,
diff --git a/tests/unit/health/test_perf_dialects.py b/tests/unit/health/test_perf_dialects.py
index 37da7ddb3..399deb1e3 100644
--- a/tests/unit/health/test_perf_dialects.py
+++ b/tests/unit/health/test_perf_dialects.py
@@ -445,6 +445,16 @@ def test_rust_fs_in_async_blocks():
assert not any(k == "blocking_sync_in_async" for k, _ in _hits("rust", awaited))
+def test_rust_console_io_in_async_is_not_fs():
+ """``std::io::stdin().read(..)`` roots at ``std`` but is console IO, not ``fs::read``."""
+ stdin = "async fn f(){ let _ = std::io::stdin().read(&mut []); }"
+ stderr = 'async fn f(){ let _ = std::io::stderr().write(b"x"); }'
+ assert not any(k == "blocking_sync_in_async" for k, _ in _hits("rust", stdin))
+ assert not any(k == "blocking_sync_in_async" for k, _ in _hits("rust", stderr))
+ sleep = "async fn f(d: Duration){ std::thread::sleep(d); }"
+ assert ("blocking_sync_in_async", "thread::sleep") in _hits("rust", sleep)
+
+
# ---------------------------------------------------------------------------
# Phase-7c precision fixes (multi-language corpus FP classes)
# ---------------------------------------------------------------------------
@@ -1512,6 +1522,22 @@ _KOTLIN_CASES = [
[],
"a dynamic pattern may legitimately vary - not hoistable",
),
+ (
+ 'fun m(xs: List<String>) {\n for (x in xs) {\n val r = "^a".toRegex()\n }\n}\n',
+ [("regex_compile_in_loop", "")],
+ "literal.toRegex() recompiled per iteration",
+ ),
+ (
+ "fun m(xs: List<P>) {\n"
+ ' for (x in xs) {\n val r = "${x.id}.*".toRegex()\n }\n}\n',
+ [],
+ "an interpolated toRegex() receiver varies per iteration - not hoistable",
+ ),
+ (
+ 'fun m(xs: List<String>) {\n for (x in xs) {\n val r = "$x.*".toRegex()\n }\n}\n',
+ [],
+ "a bare $name template receiver is not hoistable either",
+ ),
(
"fun m(xs: List<String>) {\n for (x in xs) {\n lock.lock()\n }\n}\n",
[("lock_in_loop", "")],
diff --git a/tests/unit/health/test_perf_phase7b.py b/tests/unit/health/test_perf_phase7b.py
index 3001e17f9..cfaca50bf 100644
--- a/tests/unit/health/test_perf_phase7b.py
+++ b/tests/unit/health/test_perf_phase7b.py
@@ -372,7 +372,11 @@ def test_hot_path_sync_io_reason_claims_centrality_not_a_request_path():
hit = PerfHit("hot_path_sync_io", 3, "f", "filesystem", callers=4)
(finding,) = HotPathSyncIoDetector().detect(_ctx([hit]))
assert "request" not in finding.reason
- assert "most-called functions in this repo (4 direct callers)" in finding.reason
+ assert "most-called" not in finding.reason
+ assert (
+ "called from 4 places, at least as many callers as 80% of called functions"
+ in finding.reason
+ )
assert finding.details == {"boundary_kind": "filesystem", "callers": 4}