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(actions): add tests only where no test reaches; stand history rules down on a short history (#3266)
Raghav Chamadiya669cbbd52e62
diff --git a/docs/layers/CODE_HEALTH.md b/docs/layers/CODE_HEALTH.mdindex fd4461071..9fa76b8b7 100644--- a/docs/layers/CODE_HEALTH.md+++ b/docs/layers/CODE_HEALTH.md@@ -226,6 +226,12 @@ Rows are grouped as **Now**, **Worth planning** and **Improve what Repowise cansee** (steps such as adding a coverage report that make other answers sharper).Run `repowise update` to refresh the list after you change code.+A fragile file asks for tests only when it has no measured coverage and no test+reaches it in the code graph. When tests do reach it, the row asks you to+simplify its lead function instead. An index with fewer than five commits leaves+out the rows ranked by history: fragile files, bug-fix concentration and+knowledge loss.+## Performance findingsPerformance risk flags structure that wastes work. It does not measure runtime.diff --git a/docs/reference/CLI_REFERENCE.md b/docs/reference/CLI_REFERENCE.mdindex 1ba73301e..374a8d2ec 100644--- a/docs/reference/CLI_REFERENCE.md+++ b/docs/reference/CLI_REFERENCE.md@@ -729,7 +729,7 @@ repowise next --all # up to 20 rows instead of 5repowise next --format json # the whole stored view (--json also works)```-Rows are grouped as **Now**, **Worth planning** and **Improve what Repowise can see**, each with its impact, the facts behind it, when it counts as done, and a command when one applies. An index built before a store existed names that store and suggests `repowise update` rather than reporting it as empty.+Rows are grouped as **Now**, **Worth planning** and **Improve what Repowise can see**, each with its impact, the facts behind it, when it counts as done, and a command when one applies. An index built before a store existed names that store and suggests `repowise update` rather than reporting it as empty. With fewer than five commits, the rows ranked by history are left out and the JSON sets `context.history_too_short`.---diff --git a/packages/core/src/repowise/core/analysis/actions/build.py b/packages/core/src/repowise/core/analysis/actions/build.pyindex 756d4fd4f..1aa896a5b 100644--- a/packages/core/src/repowise/core/analysis/actions/build.py+++ b/packages/core/src/repowise/core/analysis/actions/build.py@@ -48,6 +48,10 @@ Row shapes (the field names are the SQL columns):``coverage``One mapping: ``files_measured``, ``ingested_at``, ``ingested_commit_sha``,``partial``.+``test_map``+ One mapping of path to ``total`` (test files that reach it in the code+ graph) and ``via`` (the walk), for :func:`reach_paths`; see+ ``repowise.core.analysis.test_reachability.any_tests_reaching``."""from __future__ import annotations@@ -67,7 +71,7 @@ from repowise.core.analysis.health.rows import fieldfrom repowise.core.analysis.health.scoring import HISTORY_CATEGORY, biomarker_categoryfrom repowise.core.author_identity import author_identity_key-from .context import WEEK+from .context import WEEK, build_contextfrom .facts import (CoverageState,DeadFacts,@@ -79,7 +83,7 @@ from .facts import (RepoFacts,SecretFacts,)-from .rules.code import FIX_FIRST_ACTIONS+from .rules.code import FIX_FIRST_ACTIONS, is_fragilefrom .rules.hygiene import PUBLIC_ENV_KIND, SECRET_KINDSlogger = logging.getLogger(__name__)@@ -222,6 +226,39 @@ def with_leads(files: Mapping[str, FileFacts], findings: Rows) -> dict[str, Filereturn out+def reach_paths(files: Mapping[str, FileFacts]) -> list[str]:+ """The files whose test reach is worth a lookup: fragile, with no measured coverage.++ Only these can become "add tests", so the graph walk stays keyed to a few.+ The busy and fix bars depend on the files alone.+ """+ ctx = build_context(RepoFacts(anchor=None, files=files))+ return [+ p+ for p, f in files.items()+ if f.line_coverage_pct is None and is_fragile(f, ctx)+ ]+++def with_test_reach(+ files: Mapping[str, FileFacts], reaching: Mapping[str, Any]+) -> dict[str, FileFacts]:+ """``files`` with each :func:`reach_paths` file's reaching test files.++ ``reaching`` maps a path to a row with ``total`` and ``via``; a looked-up+ path it does not name has none.+ """+ out = dict(files)+ for path in reach_paths(files):+ row = reaching.get(path)+ out[path] = replace(+ out[path],+ tests_reaching=int(field(row, "total") or 0) if row is not None else 0,+ tests_reaching_via=field(row, "via") if row is not None else None,+ )+ return out++# --- the other stores ---------------------------------------------------------@@ -429,12 +466,15 @@ def build_repo_facts(dead_code: Rows | None = None,decisions: Mapping[str, Any] | None = None,coverage: Mapping[str, Any] | None = None,+ test_map: Mapping[str, Any] | None = None,+ history_commits: int | None = None,unavailable: Mapping[str, str] | None = None,absent_reason: str = ABSENT,) -> RepoFacts:"""`RepoFacts` from rows (shapes in the module docstring).- ``anchor`` is the newest commit's time and ``head_sha`` its sha. A store+ ``anchor`` is the newest commit's time and ``head_sha`` its sha;+ ``history_commits`` is how many commits the index holds. A storepassed as ``None`` is reported unavailable with ``absent_reason``; one thatfails to build is reported the same way, so it costs only its own rules.``unavailable`` names stores the caller withholds, with the reason; they@@ -443,7 +483,7 @@ def build_repo_facts(anchor = _when(anchor)since = (anchor - QUARTER) if anchor else Noneweek = (anchor - WEEK) if anchor else None- values: dict[str, Any] = {"anchor": anchor}+ values: dict[str, Any] = {"anchor": anchor, "history_commits": history_commits}missing: dict[str, str] = {}health_findings = list(health_findings)@@ -463,6 +503,8 @@ def build_repo_facts(return outbuild("files", files, file_store)+ if "files" in values:+ build("test_map", test_map, lambda m: {"files": with_test_reach(values["files"], m)})known_files: Mapping[str, FileFacts] = values.get("files") or {}build("authors", authors, lambda rows: build_authors(rows, since=since))build(@@ -499,5 +541,7 @@ __all__ = ["build_repo_facts","build_secrets","lead_paths",+ "reach_paths","with_leads",+ "with_test_reach",]diff --git a/packages/core/src/repowise/core/analysis/actions/context.py b/packages/core/src/repowise/core/analysis/actions/context.pyindex 2cff5e430..b30a0437b 100644--- a/packages/core/src/repowise/core/analysis/actions/context.py+++ b/packages/core/src/repowise/core/analysis/actions/context.py@@ -32,6 +32,14 @@ BUSY_PERCENTILE = 0.9FIX_FLOOR = 3FIX_PERCENTILE = 0.95+#: Said by every rule that ranks files by their history when the index holds+#: fewer commits than it takes to call one file busy: a one-commit import or a+#: shallow clone has churn and fix counts, but they describe the clone.+HISTORY_TOO_SHORT = (+ "The index holds too few commits to rank files by their history; "+ "fetch the full history and run `repowise update`."+)+@dataclass(frozen=True, slots=True)class RepoContext:@@ -42,11 +50,16 @@ class RepoContext:busy_threshold: intfix_threshold: intfix_commits_90d: int+ history_commits: int | None = None@propertydef is_team(self) -> bool:return self.active_authors_90d >= TEAM_MIN_AUTHORS+ @property+ def history_too_short(self) -> bool:+ return self.history_commits is not None and self.history_commits < BUSY_FLOOR+def in_week(self, when: datetime | None) -> bool:if when is None or self.week_start is None:return False@@ -81,4 +94,5 @@ def build_context(facts: RepoFacts) -> RepoContext:busy_threshold=max(BUSY_FLOOR, busy),fix_threshold=max(FIX_FLOOR, fixes),fix_commits_90d=facts.fix_commits_90d,+ history_commits=facts.history_commits,)diff --git a/packages/core/src/repowise/core/analysis/actions/engine.py b/packages/core/src/repowise/core/analysis/actions/engine.pyindex 054772cd9..f5c759052 100644--- a/packages/core/src/repowise/core/analysis/actions/engine.py+++ b/packages/core/src/repowise/core/analysis/actions/engine.py@@ -134,6 +134,7 @@ def compose_actions("fix_commits_90d": ctx.fix_commits_90d,"busy_threshold": ctx.busy_threshold,"coverage": facts.coverage.status,+ "history_too_short": ctx.history_too_short,},"horizons": horizons,"rules": [o.as_dict() for o in outcomes],diff --git a/packages/core/src/repowise/core/analysis/actions/facts.py b/packages/core/src/repowise/core/analysis/actions/facts.pyindex 4741c058b..5dbd81468 100644--- a/packages/core/src/repowise/core/analysis/actions/facts.py+++ b/packages/core/src/repowise/core/analysis/actions/facts.py@@ -47,6 +47,11 @@ class FileFacts:owner_pct: float | Nonedependents: int | Nonelead: LeadFinding | None = None+ #: Test files the code graph says reach this file; ``None`` when not looked+ #: up (only fragile files with no measured coverage are) or the map failed.+ tests_reaching: int | None = None+ #: Which walk found them (``call-graph`` or ``import-graph``).+ tests_reaching_via: str | None = None@dataclass(frozen=True, slots=True)@@ -139,5 +144,7 @@ class RepoFacts:author_last_commit: Mapping[str, datetime] = field(default_factory=dict)active_authors_90d: int = 0coverage: CoverageState = CoverageState("unknown")+ #: Commits the index holds; ``None`` when not counted.+ history_commits: int | None = None#: Store name -> why it could not be read.unavailable: Mapping[str, str] = field(default_factory=dict)diff --git a/packages/core/src/repowise/core/analysis/actions/rules/code.py b/packages/core/src/repowise/core/analysis/actions/rules/code.pyindex 2eae69b68..22f9eb8f1 100644--- a/packages/core/src/repowise/core/analysis/actions/rules/code.py+++ b/packages/core/src/repowise/core/analysis/actions/rules/code.py@@ -6,7 +6,7 @@ import posixpathfrom collections import defaultdictfrom datetime import datetime-from ..context import RepoContext+from ..context import HISTORY_TOO_SHORT, RepoContextfrom ..facts import FileFacts, RepoFactsfrom ..model import Action, ActionCommand, ActionDetail, RuleOutcome, WhyFact, fingerprintfrom ._text import code, plural@@ -219,7 +219,58 @@ def _coverage_fact(f: FileFacts, facts: RepoFacts) -> WhyFact:return WhyFact("line coverage", "Unknown, not in the report", "unknown")-def _is_fragile(f: FileFacts, ctx: RepoContext) -> bool:+def _reach_fact(f: FileFacts) -> WhyFact:+ if not f.tests_reaching:+ return WhyFact("tests that reach it", "None in the code graph", "inferred")+ via = (f.tests_reaching_via or "code graph").replace("-", " ")+ return WhyFact(+ "tests that reach it", f"{plural(f.tests_reaching, 'test file')}, {via}", "inferred"+ )+++def _lead_name(f: FileFacts) -> str | None:+ """The lead function's name, unless the parser could only call it anonymous."""+ name = f.lead.function if f.lead else None+ return None if not name or name.startswith("<anonymous") else name+++def _fragile_step(f: FileFacts) -> tuple[str, str, str] | None:+ """Title, done-when and variant for a fragile file, or ``None`` to stay quiet.++ Measured coverage decides first. Without it, "add tests" holds only when no+ test reaches the file in the code graph; a file tests do reach gets the+ simplify step instead, as an inferred claim.+ """+ cov = f.line_coverage_pct+ if cov is not None and cov < COVERED_PCT:+ return (+ f"Raise test coverage on {code(f.path)} from {cov:.0f}%",+ "Line coverage on this file reaches 80%.",+ "raise",+ )+ if cov is None and f.tests_reaching is None:+ # Unknown is not zero: with no coverage and no test map, neither "add+ # tests" nor "tests reach it" is a claim the facts support.+ return None+ if cov is None and f.tests_reaching == 0:+ return (+ f"Add tests around {code(f.path)} before its next change",+ "A coverage report includes it at 80% or more.",+ "unknown",+ )+ if f.lead is None:+ return None+ name = _lead_name(f)+ where = code(name) + " in " if name else ""+ tested = "it is tested" if cov is not None else "tests reach it"+ return (+ f"Simplify {where}{code(f.path)}: {tested}, and fixes keep landing",+ "Its lead finding closes and bug-fix commits slow down.",+ "simplify" if cov is not None else "reached",+ )+++def is_fragile(f: FileFacts, ctx: RepoContext) -> bool:return (not f.is_testand f.bug_magnet@@ -237,39 +288,31 @@ def fragile_file(facts: RepoFacts, ctx: RepoContext) -> RuleOutcome:rule = "fragile_file"if "files" in facts.unavailable:return RuleOutcome(rule, "unavailable", facts.unavailable["files"])+ if ctx.history_too_short:+ return RuleOutcome(rule, "not_applicable", HISTORY_TOO_SHORT)if ctx.fix_commits_90d == 0:return RuleOutcome(rule, "not_applicable", "No bug-fix commits in the last 90 days.")# A file Fix first already names carries one action, not two.named = fix_first_paths(facts)fragile = sorted(- (f for f in facts.files.values() if _is_fragile(f, ctx) and f.path not in named),+ (f for f in facts.files.values() if is_fragile(f, ctx) and f.path not in named),key=lambda f: -(f.fix_commits_90d * f.commits_90d),)touched = [f for f in fragile if ctx.in_week(f.last_commit_at)]week_rows = {f.path for f in touched[:FRAGILE_WEEK_ROWS]}actions = []for f in fragile:- cov = f.line_coverage_pct- if cov is None:- title = f"Add tests around {code(f.path)} before its next change"- done = "A coverage report includes it at 80% or more."- variant = "unknown"- elif cov < COVERED_PCT:- title = f"Raise test coverage on {code(f.path)} from {cov:.0f}%"- done = "Line coverage on this file reaches 80%."- variant = "raise"- elif f.lead is not None:- where = code(f.lead.function) + " in " if f.lead.function else ""- title = f"Simplify {where}{code(f.path)}: it is tested, and fixes keep landing"- done = "Its lead finding closes and bug-fix commits slow down."- variant = "simplify"- else:+ step = _fragile_step(f)+ if step is None:continue+ title, done, variant = stepwhy = [WhyFact("commits in 90 days", str(f.commits_90d)),WhyFact("bug-fix commits", str(f.fix_commits_90d)),_coverage_fact(f, facts),]+ if f.line_coverage_pct is None:+ why.append(_reach_fact(f))impact = (f"Changed {f.commits_90d} times in 90 days, and {f.fix_commits_90d} ""of those commits were bug fixes."@@ -288,11 +331,11 @@ def fragile_file(facts: RepoFacts, ctx: RepoContext) -> RuleOutcome:why=tuple(why),target_kind="file",target_path=f.path,- target_symbol=f.lead.function if variant == "simplify" and f.lead else None,+ target_symbol=_lead_name(f) if variant in ("simplify", "reached") else None,identity=f.path,surface="file",effort="M" if (f.nloc or 0) < 600 else "L",- confidence="high" if variant != "unknown" else "medium",+ confidence="high" if variant in ("raise", "simplify") else "medium",done_when=done,marker=f.lead.biomarker if f.lead else None,weight=float(f.fix_commits_90d * f.commits_90d),@@ -340,6 +383,8 @@ def fix_concentration(facts: RepoFacts, ctx: RepoContext) -> RuleOutcome:rule = "fix_concentration"if "files" in facts.unavailable:return RuleOutcome(rule, "unavailable", facts.unavailable["files"])+ if ctx.history_too_short:+ return RuleOutcome(rule, "not_applicable", HISTORY_TOO_SHORT)total = ctx.fix_commits_90dif total < CONCENTRATION_MIN_FIXES:return RuleOutcome(@@ -391,7 +436,7 @@ def fix_concentration(facts: RepoFacts, ctx: RepoContext) -> RuleOutcome:fixed_files[folder], key=lambda p: -production[p].fix_commits_90d)includes = tuple(- p for p in members if _is_fragile(production[p], ctx) and p not in named+ p for p in members if is_fragile(production[p], ctx) and p not in named)actions.append(Action(diff --git a/packages/core/src/repowise/core/analysis/actions/rules/hygiene.py b/packages/core/src/repowise/core/analysis/actions/rules/hygiene.pyindex a1350cc88..9ff5ab23d 100644--- a/packages/core/src/repowise/core/analysis/actions/rules/hygiene.py+++ b/packages/core/src/repowise/core/analysis/actions/rules/hygiene.py@@ -8,7 +8,7 @@ from datetime import timedeltafrom repowise.core.support_paths import DOC_EXTENSIONS-from ..context import RepoContext+from ..context import HISTORY_TOO_SHORT, RepoContextfrom ..facts import RepoFactsfrom ..model import Action, ActionCommand, ActionDetail, RuleOutcome, WhyFact, fingerprintfrom ._text import code, plural@@ -340,6 +340,8 @@ def knowledge_loss(facts: RepoFacts, ctx: RepoContext) -> RuleOutcome:rule = "knowledge_loss"if "files" in facts.unavailable:return RuleOutcome(rule, "unavailable", facts.unavailable["files"])+ if ctx.history_too_short:+ return RuleOutcome(rule, "not_applicable", HISTORY_TOO_SHORT)if not ctx.is_team:return RuleOutcome(rule,diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.pyindex ab93be8b3..eee8e2a02 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py@@ -25,8 +25,8 @@ from repowise.core.analysis.test_reachability import (MAX_TESTS_PER_TARGET,ReachDistance,ReachedBy,+ cached_test_files,imported_names_by_test,- load_test_files,rank_tests,reach_into_symbols,tests_matching_by_name,@@ -1038,7 +1038,7 @@ async def _validation_inputs(for file_path, lines in _line_ranges(suggestion).items():if not _measured_labels(measured.get(file_path, []), lines):unanswered.add(file_path)- test_files = await load_test_files(session, repository_id)+ test_files = await cached_test_files(session, repository_id)inferred = (await tests_reaching_by_tier(session, repository_id, sorted(unanswered), test_files=test_filesdiff --git a/packages/core/src/repowise/core/analysis/test_reachability.py b/packages/core/src/repowise/core/analysis/test_reachability.pyindex 492819541..4845cd421 100644--- a/packages/core/src/repowise/core/analysis/test_reachability.py+++ b/packages/core/src/repowise/core/analysis/test_reachability.py@@ -148,12 +148,13 @@ over data already in the database.from __future__ import annotationsimport json+from collections import OrderedDictfrom collections.abc import Collection, Mappingfrom dataclasses import dataclassfrom pathlib import PurePosixPathfrom typing import Any, Literal, NamedTuple, TypeAlias-from sqlalchemy import select, text+from sqlalchemy import func, select, textfrom sqlalchemy.ext.asyncio import AsyncSessionfrom repowise.core.analysis.execution_graph import (@@ -162,7 +163,7 @@ from repowise.core.analysis.execution_graph import (file_of_symbol,)from repowise.core.ingestion.models import EXECUTION_EDGE_TYPES, FILE_DEPENDENCY_EDGE_TYPES-from repowise.core.persistence.models import GraphNode+from repowise.core.persistence.models import GraphNode, HealthFileMetric, Repositoryfrom repowise.core.test_paths import paired_test_namesfrom .dead_code.file_reachability import BARREL_FILENAMES@@ -196,8 +197,11 @@ __all__ = ["CallGraphView","ReachDistance","ReachedBy",+ "any_tests_reaching",+ "cached_test_files","call_graph_from_db","call_graph_from_graph",+ "clear_test_map_cache","direct_dependents","files_reached_by_tests","files_with_paired_tests",@@ -208,6 +212,7 @@ __all__ = ["tests_matching_by_name","tests_reaching","tests_reaching_by_tier",+ "tests_reaching_through_helpers",]@@ -419,6 +424,114 @@ async def load_test_files(session: AsyncSession, repo_id: str) -> set[str]:return {row[0] for row in res.all()}+# Answers kept in process per (database, repository, index stamp), so one+# request reads the test files once whoever asks: Fix first's validation and+# Do next both walk from them. Ceiling: Do next's per-file reach is still+# walked once per index; persisting it at index time (the engine's+# ``_files_reached_by_tests`` set, stored as a per-file bool) is the upgrade path.+_CACHE_SIZE = 32+_cache: OrderedDict[tuple[Any, ...], Any] = OrderedDict()+++async def _index_stamp(session: AsyncSession, repo_id: str) -> tuple[Any, ...]:+ """What moves when an index run rewrites the graph: the repository row and+ the file metrics written beside the graph rows (the stamp Fix first uses).+ One statement, so a cached answer costs one read."""+ repo = Repository.id == repo_id+ metrics = HealthFileMetric.repository_id == repo_id+ columns = (+ select(Repository.head_commit).where(repo),+ select(Repository.updated_at).where(repo),+ select(Repository.graph_edges_parser_fingerprint).where(repo),+ select(func.max(HealthFileMetric.updated_at)).where(metrics),+ select(func.count()).select_from(HealthFileMetric).where(metrics),+ )+ row = (await session.execute(select(*(c.scalar_subquery() for c in columns)))).one()+ return tuple(row)+++async def _cache_key(session: AsyncSession, repo_id: str, *parts: Any) -> tuple[Any, ...]:+ bind = str(session.bind.url) if session.bind is not None else None+ return (bind, repo_id, await _index_stamp(session, repo_id), *parts)+++def _remember(key: tuple[Any, ...], value: Any) -> Any:+ _cache[key] = value+ while len(_cache) > _CACHE_SIZE:+ _cache.popitem(last=False)+ return value+++def clear_test_map_cache() -> None:+ _cache.clear()+++async def cached_test_files(session: AsyncSession, repo_id: str) -> frozenset[str]:+ """:func:`load_test_files`, read once per index state; frozen because it is shared."""+ key = await _cache_key(session, repo_id, "test_files")+ if key in _cache:+ _cache.move_to_end(key)+ return _cache[key]+ return _remember(key, frozenset(await load_test_files(session, repo_id)))+++async def any_tests_reaching(+ session: AsyncSession, repo_id: str, targets: Collection[str]+) -> dict[str, ReachedBy]:+ """Whether any test reaches each target: the default tiers, then a test-only helper.++ The first walk is :func:`tests_reaching_by_tier` at its defaults, the one+ impacted-tests runs, so the two never disagree about a file it answers.+ For the rest, :func:`tests_reaching_through_helpers`. Cached per index state.+ """+ seeds = tuple(sorted(set(targets)))+ key = await _cache_key(session, repo_id, "any_reach", seeds)+ if key in _cache:+ _cache.move_to_end(key)+ return _cache[key]+ test_files = set(await cached_test_files(session, repo_id))+ found = await tests_reaching_by_tier(session, repo_id, list(seeds), test_files=test_files)+ rest = [s for s in seeds if s not in found]+ if rest:+ found |= await tests_reaching_through_helpers(session, repo_id, rest, test_files)+ return _remember(key, found)+++async def tests_reaching_through_helpers(+ session: AsyncSession, repo_id: str, targets: list[str], test_files: set[str]+) -> dict[str, ReachedBy]:+ """Tests that import a file only tests import, which imports the target.++ A second import hop, kept off hubs: a blanket second hop was measured and+ rejected (see Depth), but a module nothing outside the test suite imports+ is test support whatever its name (``attempt.spawn-workspace.test-support.ts``+ loading ``attempt.ts`` for 30-odd tests), so walking through it claims+ nothing a hub would.+ """+ edge_types = sorted(FILE_DEPENDENCY_EDGE_TYPES)+ middles: dict[str, set[str]] = {}+ for dependent, target in await _edges_into(session, repo_id, targets, edge_types, frozenset()):+ if dependent not in test_files:+ middles.setdefault(dependent, set()).add(target)+ importers: dict[str, set[str]] = {}+ for dependent, middle in await _edges_into(+ session, repo_id, sorted(middles), edge_types, frozenset()+ ):+ importers.setdefault(middle, set()).add(dependent)+ reached: dict[str, set[str]] = {}+ for middle, who in importers.items():+ if who <= test_files:+ for target in middles[middle]:+ reached.setdefault(target, set()).update(who)+ out: dict[str, ReachedBy] = {}+ for target, tests in reached.items():+ ordered = tuple(rank_tests(target, tests))+ out[target] = ReachedBy(+ list(ordered[:MAX_TESTS_PER_TARGET]), "import-graph", len(ordered), ordered+ )+ return out++async def placed_test_files(session: AsyncSession, repo_id: str) -> set[str]:"""Test files with a resolved dependency edge into a non-test file of the repository.diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/actions.py b/packages/core/src/repowise/core/persistence/crud/analysis/actions.pyindex abc51874e..39ce9dd6c 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/actions.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/actions.py@@ -39,13 +39,16 @@ from repowise.core.analysis.actions.build import (build_recent,build_secrets,lead_paths,+ reach_paths,with_leads,+ with_test_reach,)from repowise.core.analysis.actions.facts import FileFactsfrom repowise.core.analysis.actions.rules.code import FIX_FIRST_ACTIONSfrom repowise.core.analysis.actions.rules.hygiene import PUBLIC_ENV_KIND, SECRET_KINDSfrom repowise.core.analysis.dead_code.risk_factors import REVIEW_ONLY_KINDSfrom repowise.core.analysis.finding_registry import excluded_types+from repowise.core.analysis.test_reachability import any_tests_reachingfrom ...models import (ActionState,@@ -224,6 +227,26 @@ async def _recent(return build_recent(rows, week=since, open_findings=open_findings, files=files)+async def _history(session: AsyncSession, repo_id: str) -> dict[str, Any]:+ count = select(func.count()).select_from(GitCommit).where(GitCommit.repository_id == repo_id)+ commits = (await session.execute(count)).scalar_one()+ if not commits:+ # No commit rows and no per-file history: git was never indexed, which+ # is unknown, not a short history.+ indexed = select(GitMetadata.file_path).where(GitMetadata.repository_id == repo_id)+ if (await session.execute(indexed.limit(1))).first() is None:+ return {"history_commits": None}+ return {"history_commits": commits}+++async def _test_map(session: AsyncSession, repo_id: str, files: dict[str, FileFacts]) -> dict:+ """Only a file no test reaches may be told to add tests, so only those few are walked."""+ paths = reach_paths(files)+ if not paths:+ return {}+ return {"files": with_test_reach(files, await any_tests_reaching(session, repo_id, paths))}++async def _fix_first(session: AsyncSession, repo_id: str) -> dict[str, Any]:return {"fix_first": (await load_fix_first(session, repo_id, limit=FIX_FIRST_ACTIONS)).items}@@ -389,7 +412,10 @@ async def load_repo_facts(session: AsyncSession, repo_id: str) -> RepoFacts:logger.warning("actions: %s unavailable: %s", store, exc)unavailable[store] = ABSENT+ await read("history", lambda: _history(session, repo_id))await read("files", lambda: _files(session, repo_id, since))+ if "files" in values:+ await read("test_map", lambda: _test_map(session, repo_id, values["files"]))files: dict[str, FileFacts] = values.get("files") or {}await read("authors", lambda: _authors(session, repo_id, since))await read("commit_health", lambda: _recent(session, repo_id, week, files))diff --git a/packages/server/src/repowise/server/schemas/actions.py b/packages/server/src/repowise/server/schemas/actions.pyindex a1d98980b..b5efc8919 100644--- a/packages/server/src/repowise/server/schemas/actions.py+++ b/packages/server/src/repowise/server/schemas/actions.py@@ -77,6 +77,10 @@ class ActionContext(BaseModel):fix_commits_90d: intbusy_threshold: intcoverage: Literal["measured", "stale", "unknown"]+ history_too_short: bool = Field(+ False,+ description="The index holds too few commits for history-driven rules to rank by.",+ )class ActionsResponse(BaseModel):diff --git a/packages/types/src/actions.ts b/packages/types/src/actions.tsindex ad62dc6db..72bd9bc1d 100644--- a/packages/types/src/actions.ts+++ b/packages/types/src/actions.ts@@ -135,6 +135,8 @@ export interface ActionsResponse {fix_commits_90d: number;busy_threshold: number;coverage: "measured" | "stale" | "unknown";+ /** Too few commits indexed for history-driven rules to rank by. */+ history_too_short: boolean;};horizons: Record<ActionHorizonKey, ActionHorizon>;rules: ActionRuleReport[];diff --git a/packages/types/src/generated/http.ts b/packages/types/src/generated/http.tsindex 25359f4bd..6c144cefc 100644--- a/packages/types/src/generated/http.ts+++ b/packages/types/src/generated/http.ts@@ -23,6 +23,8 @@ export interface ActionContext {fix_commits_90d: number;busy_threshold: number;coverage: "measured" | "stale" | "unknown";+ /** The index holds too few commits for history-driven rules to rank by. */+ history_too_short?: boolean;}export interface ActionDetail {diff --git a/packages/ui/__tests__/overview/next-actions.test.tsx b/packages/ui/__tests__/overview/next-actions.test.tsxindex c58d6e45f..047b467a7 100644--- a/packages/ui/__tests__/overview/next-actions.test.tsx+++ b/packages/ui/__tests__/overview/next-actions.test.tsx@@ -56,6 +56,7 @@ function response(week: NextAction[], quarter: NextAction[]): ActionsResponse {fix_commits_90d: 40,busy_threshold: 8,coverage: "unknown",+ history_too_short: false,},horizons: { week: horizon(week), quarter: horizon(quarter) },rules: [diff --git a/tests/unit/analysis/test_actions_rules.py b/tests/unit/analysis/test_actions_rules.pyindex 0f2da235b..7e65d8b77 100644--- a/tests/unit/analysis/test_actions_rules.py+++ b/tests/unit/analysis/test_actions_rules.py@@ -45,6 +45,7 @@ def _file(path: str, **kw) -> FileFacts:owner_name=None,owner_pct=None,dependents=None,+ tests_reaching=0,)base.update(kw)return FileFacts(**base)@@ -599,3 +600,95 @@ def test_rollup_carries_evidence_starting_with_the_worst_file_and_commands() ->mcp = [c.mcp for c in a.commands]assert any(m and m.startswith('get_health(targets=["src/worst.py"') for m in mcp)assert 'get_change_risk(revspec="w1")' in mcp+++# ---------------------------------------------------------------------------+# Self-checks: "add tests" needs no test reaching the file; short history+# ---------------------------------------------------------------------------++_LEAD = LeadFinding("complex_method", "high", "runEmbeddedAttempt", 40, "CCN 90")+++def test_fragile_reached_by_tests_says_simplify_not_add_tests() -> None:+ """openclaw's attempt.ts: no coverage report, but tests reach it in the graph."""+ path = "src/agents/embedded-agent-runner/run/attempt.ts"+ f = _file(path, lead=_LEAD, tests_reaching=36, tests_reaching_via="import-graph")+ (a,) = _run(code.fragile_file, _facts([f])).actions+ assert not a.title.startswith("Add tests")+ assert a.title == (+ f"Simplify `runEmbeddedAttempt` in `{path}`: tests reach it, and fixes keep landing"+ )+ reach = next(w for w in a.why if w.label == "tests that reach it")+ assert (reach.value, reach.basis) == ("36 test files, import graph", "inferred")+ assert a.confidence == "medium"+ assert a.target_symbol == "runEmbeddedAttempt"+++def test_fragile_reached_by_tests_without_a_lead_is_silent() -> None:+ out = _run(code.fragile_file, _facts([_file("src/a.py", tests_reaching=2)]))+ assert out.actions == ()+++def test_fragile_no_test_reaches_says_add_tests() -> None:+ (a,) = _run(code.fragile_file, _facts([_file("src/a.py", tests_reaching=0)])).actions+ assert a.title.startswith("Add tests around `src/a.py`")+ reach = next(w for w in a.why if w.label == "tests that reach it")+ assert (reach.value, reach.basis) == ("None in the code graph", "inferred")+++def test_fragile_unknown_reach_is_not_zero() -> None:+ """A failed test-map walk claims neither "add tests" nor "tests reach it"."""+ lead = LeadFinding("complex_method", "high", "run", 10, "")+ unknown = _file("src/a.py", lead=lead, tests_reaching=None)+ facts = _facts([unknown], unavailable={"test_map": "Not in this index yet."})+ assert _run(code.fragile_file, facts).actions == ()+ (zero,) = _run(code.fragile_file, _facts([replace(unknown, tests_reaching=0)])).actions+ assert zero.title.startswith("Add tests around")+++def test_an_anonymous_lead_function_is_left_out_of_the_title() -> None:+ lead = LeadFinding("complex_method", "high", "<anonymous@3167>", 3167, "")+ f = _file("src/gateway/chat.ts", lead=lead, tests_reaching=3)+ (a,) = _run(code.fragile_file, _facts([f])).actions+ assert a.title == "Simplify `src/gateway/chat.ts`: tests reach it, and fixes keep landing"+ assert a.target_symbol is None+++def test_measured_coverage_wins_over_the_test_map() -> None:+ """Ours: update_cmd/command.py (68 changes, 27 fixes, 70%) keeps "raise coverage",+ and call_resolver.py keeps "simplify _csharp_type_id", whatever reaches them."""+ raise_path = "packages/cli/src/repowise/cli/commands/update_cmd/command.py"+ tested_path = "packages/core/src/repowise/core/ingestion/call_resolver.py"+ lead = LeadFinding("complex_method", "high", "_csharp_type_id", 10, "")+ files = [+ _file(raise_path, commits_90d=68, fix_commits_90d=27, line_coverage_pct=70.0,+ tests_reaching=40),+ _file(tested_path, commits_90d=68, fix_commits_90d=27, line_coverage_pct=91.0,+ lead=lead, tests_reaching=40),+ ]+ titles = {a.target_path: a.title for a in _run(code.fragile_file, _facts(files)).actions}+ assert titles[raise_path] == f"Raise test coverage on `{raise_path}` from 70%"+ assert titles[tested_path] == (+ f"Simplify `_csharp_type_id` in `{tested_path}`: it is tested, and fixes keep landing"+ )+++def test_one_commit_history_stands_history_rules_down_and_flags_the_view() -> None:+ """hermes-dogfood: a one-commit copy has churn and fix counts that describe the copy."""+ files = [_file(f"src/m{i}/a{j}.py", commits_90d=30, fix_commits_90d=10)+ for i in range(2) for j in range(4)]+ facts = _facts(files, history_commits=1, active_authors_90d=5)+ for rule in (code.fragile_file, code.fix_concentration, hygiene.knowledge_loss):+ out = _run(rule, facts)+ assert (out.status, out.actions) == ("not_applicable", ())+ assert "too few commits" in out.reason+ view = compose_actions(facts, now=NOW)+ assert view["context"]["history_too_short"] is True+++def test_history_at_the_busy_floor_or_uncounted_is_not_flagged() -> None:+ files = [_file("src/a.py")]+ for commits in (5, None):+ facts = _facts(files, history_commits=commits)+ assert build_context(facts).history_too_short is False+ assert _run(code.fragile_file, facts).status == "evaluated"diff --git a/tests/unit/conftest.py b/tests/unit/conftest.pyindex d602bdafd..d7aa82357 100644--- a/tests/unit/conftest.py+++ b/tests/unit/conftest.py@@ -18,3 +18,14 @@ def cursor_hooks_json(tmp_path_factory, monkeypatch):monkeypatch.setattr(cursor, "user_hooks_path", lambda: hooks)monkeypatch.setattr(cursor.CursorAdapter, "detect", lambda self: False)return hooks++[email protected](autouse=True)+def fresh_test_map_cache():+ """In-memory stores reuse one URL and seed graph rows without moving the+ index stamp, so the process-wide test-map cache starts empty per test."""+ from repowise.core.analysis.test_reachability import clear_test_map_cache++ clear_test_map_cache()+ yield+ clear_test_map_cache()diff --git a/tests/unit/health/test_recommendations.py b/tests/unit/health/test_recommendations.pyindex 88044c801..40db16ea0 100644--- a/tests/unit/health/test_recommendations.py+++ b/tests/unit/health/test_recommendations.py@@ -20,7 +20,7 @@ from repowise.core.analysis.health.refactoring.recommendations import (rehydrate_suggestion,target_symbol_ids,)-from repowise.core.analysis.test_reachability import ReachDistance, ReachedBy+from repowise.core.analysis.test_reachability import ReachDistance, ReachedBy, clear_test_map_cachefrom repowise.core.persistence.crud import save_test_coveragefrom repowise.core.persistence.database import init_dbfrom tests.unit.persistence.helpers import insert_repo@@ -287,11 +287,15 @@ async def test_query_count_is_constant_as_plan_and_test_counts_grow() -> None:event.listen(engine.sync_engine, "before_cursor_execute", record)try:+ # Both calls start from a cold test-map cache, so the count measures the+ # payload, not whether the first call warmed it.+ clear_test_map_cache()statements.clear()await hydrate_recommendations(async_session, repo.id, [_plan("one", file_path="src/f0.py")])small = len(statements)await save_test_coverage(async_session, repo.id, records, source_format="coverage.py")await async_session.commit()+ clear_test_map_cache()statements.clear()await hydrate_recommendations(async_session,diff --git a/tests/unit/persistence/test_actions_build.py b/tests/unit/persistence/test_actions_build.pyindex a03bc1a90..2a3c1e75e 100644--- a/tests/unit/persistence/test_actions_build.py+++ b/tests/unit/persistence/test_actions_build.py@@ -129,6 +129,8 @@ def _rows(dead_ids: list[str], **overrides):],"decisions": {"stale_decisions": [], "summary": {}},"coverage": {"files_measured": 0},+ "test_map": {},+ "history_commits": 5,}return {**rows, **overrides}diff --git a/tests/unit/persistence/test_actions_loader.py b/tests/unit/persistence/test_actions_loader.pyindex 9bb6539a1..5371cc7b6 100644--- a/tests/unit/persistence/test_actions_loader.py+++ b/tests/unit/persistence/test_actions_loader.py@@ -53,6 +53,13 @@ async def _seed(session) -> str:committed_at=ANCHOR - timedelta(days=2), subject="feat: build step"),GitCommit(repository_id=rid, sha="older", author_name="Cy", author_email="[email protected]",committed_at=ANCHOR - timedelta(days=20), subject="fix: things"),+ # Older history, outside every window: enough commits to rank by.+ *(+ GitCommit(repository_id=rid, sha=f"old{i}", author_name="Ada",+ author_email="[email protected]", committed_at=ANCHOR - timedelta(days=200 + i),+ subject="chore: old")+ for i in range(2)+ ),])add([GitCommitFile(repository_id=rid, sha="older", file_path="src/old.py")])@@ -292,3 +299,93 @@ async def test_coverage_state_reads_the_ingest_record(async_session) -> None:# The row's sha sorts after "head", which `max()` over rows would have picked.assert (state.status, state.partial, state.files_measured) == ("measured", True, 1)assert (await _coverage(async_session, rid, "newer"))["coverage"].status == "stale"+++async def test_a_test_reaching_through_a_helper_turns_add_tests_into_simplify(async_session) -> None:+ """openclaw's attempt.ts: tests load it through a support module two imports away."""+ from repowise.core.analysis.test_reachability import clear_test_map_cache+ from repowise.core.persistence.crud.analysis.actions import load_repo_facts+ from repowise.core.persistence.models import GraphEdge, GraphNode++ rid = await _seed(async_session)+ facts = await load_repo_facts(async_session, rid)+ assert facts.history_commits == 5+ assert facts.files["src/core.py"].tests_reaching == 0++ for path, is_test in (+ ("src/core.py", False),+ ("src/core.test-support.py", False),+ ("tests/test_core.py", True),+ ):+ async_session.add(+ GraphNode(repository_id=rid, node_id=path, node_type="file", is_test=is_test)+ )+ for src, dst in (+ ("tests/test_core.py", "src/core.test-support.py"),+ ("src/core.test-support.py", "src/core.py"),+ ):+ async_session.add(+ GraphEdge(repository_id=rid, source_node_id=src, target_node_id=dst, edge_type="imports")+ )+ await async_session.commit()+ # The graph changed without an index run, which is what moves the cache stamp.+ clear_test_map_cache()++ facts = await load_repo_facts(async_session, rid)+ core = facts.files["src/core.py"]+ assert (core.tests_reaching, core.tests_reaching_via) == (1, "import-graph")++ # Once production code imports the middle file it is a hub, not test+ # support, and the second hop no longer counts.+ async_session.add(GraphNode(repository_id=rid, node_id="src/app.py", node_type="file"))+ async_session.add(+ GraphEdge(repository_id=rid, source_node_id="src/app.py",+ target_node_id="src/core.test-support.py", edge_type="imports")+ )+ await async_session.commit()+ clear_test_map_cache()+ facts = await load_repo_facts(async_session, rid)+ assert facts.files["src/core.py"].tests_reaching == 0+++async def test_a_failed_test_map_walk_is_unknown_not_zero(async_session, monkeypatch) -> None:+ from repowise.core.persistence.crud.analysis import actions as loader++ rid = await _seed(async_session)++ async def broken(*_a, **_k):+ raise RuntimeError("graph_edges unreadable")++ monkeypatch.setattr(loader, "any_tests_reaching", broken)+ facts = await loader.load_repo_facts(async_session, rid)+ assert "test_map" in facts.unavailable+ assert facts.files["src/core.py"].tests_reaching is None+++async def test_an_index_without_git_has_no_history_count(async_session) -> None:+ from repowise.core.persistence.crud.analysis.actions import load_repo_facts++ rid = await _seed(async_session)+ await async_session.execute(text("DELETE FROM git_commits"))+ await async_session.commit()+ assert (await load_repo_facts(async_session, rid)).history_commits == 0++ await async_session.execute(text("DELETE FROM git_metadata"))+ await async_session.commit()+ facts = await load_repo_facts(async_session, rid)+ assert facts.history_commits is None+++async def test_a_one_commit_index_is_flagged_and_history_rules_stand_down(async_session) -> None:+ rid = await _seed(async_session)+ await async_session.execute(text("DELETE FROM git_commits WHERE sha != 'head'"))+ await async_session.commit()++ view = await load_actions_view(async_session, rid, now=NOW)+ assert view["context"]["history_too_short"] is True+ status = {r["rule"]: (r["status"], r["reason"]) for r in view["rules"]}+ for rule in ("fragile_file", "fix_concentration", "knowledge_loss"):+ assert status[rule][0] == "not_applicable"+ assert "too few commits" in status[rule][1]+ # Rules that do not rank by history still run.+ assert status["live_secret"][0] == "evaluated"diff --git a/tests/unit/server/test_refactoring.py b/tests/unit/server/test_refactoring.pyindex 91461b875..75b9092d0 100644--- a/tests/unit/server/test_refactoring.py+++ b/tests/unit/server/test_refactoring.py@@ -9,6 +9,7 @@ import pytestfrom httpx import AsyncClientfrom sqlalchemy import event+from repowise.core.analysis.test_reachability import clear_test_map_cachefrom repowise.core.persistence import (batch_upsert_graph_edges,batch_upsert_graph_nodes,@@ -273,6 +274,9 @@ async def test_paged_targets_query_count_is_constant_as_payload_grows(repo_id = await _seed(client, app)async def request_count(limit: int) -> int:+ # Each request starts from a cold test-map cache, so the count measures+ # the payload, not whether the previous request warmed it.+ clear_test_map_cache()statements: list[str] = []def record(*args) -> None: