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.

refactor(risk): stage the cross-repo enrichment of risk cards (#4070)

Raghav Chamadiya5dd0f51a9668
diff --git a/packages/server/src/repowise/server/mcp_server/tool_risk/enrichment.py b/packages/server/src/repowise/server/mcp_server/tool_risk/enrichment.py
index abc9dd059..20714bc60 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_risk/enrichment.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_risk/enrichment.py
@@ -119,6 +119,201 @@ def _combined_analysis(cross_state: dict, contract_state: dict, rows: list[dict]
}
+def _set_capped(
+ container: dict,
+ key: str,
+ population: list[dict],
+ collector: OmissionCollector | None,
+ target: str,
+ extra: dict | None = None,
+) -> None:
+ """Write *key* capped at the relationship limit, then its counts.
+
+ *extra* fields land between the list and its counts, where the wire
+ format puts them.
+ """
+ shown = population[:_RELATIONSHIP_LIMIT]
+ container[key] = shown
+ container.update(extra or {})
+ container[f"{key}_total"] = len(population)
+ container[f"{key}_emitted"] = len(shown)
+ container[f"{key}_truncated"] = len(shown) < len(population)
+ cap_collection(
+ container,
+ key,
+ population,
+ _RELATIONSHIP_LIMIT,
+ collector,
+ label=f"{target} :: {key} beyond cap={_RELATIONSHIP_LIMIT}",
+ preserve_counts=True,
+ )
+
+
+_EVIDENCE_PRIORITY = {"contract": 0, "structural": 1, "historical": 2}
+
+
+def _cross_repo_links(
+ alias: str,
+ target: str,
+ cross_partners: list[dict],
+ provider_links: list[dict],
+ consumer_links: list[dict],
+ package_consumers: list[dict],
+) -> list[dict]:
+ """Every cross-repo row for *target*, contract evidence first."""
+ links = [
+ *(
+ _contract_consumer(link, alias, target)
+ for link in provider_links
+ if link.get("consumer_repo") != alias
+ ),
+ *(
+ _contract_provider(link, alias, target)
+ for link in consumer_links
+ if link.get("provider_repo") != alias
+ ),
+ *(_package_consumer(link, target) for link in package_consumers),
+ *(_cross_repo_co_change(alias, target, partner) for partner in cross_partners),
+ ]
+ links.sort(
+ key=lambda row: (
+ _EVIDENCE_PRIORITY.get(row["evidence_kind"], 9),
+ row.get("target_repository", row.get("consumer_repository", "")),
+ row.get("target_file", row.get("consumer_file", "")),
+ row.get("source_repository", row.get("provider_repository", "")),
+ )
+ )
+ return links
+
+
+def _affected_repos(alias: str, links: list[dict]) -> list[str]:
+ return sorted(
+ {
+ repo
+ for row in links
+ for repo in (
+ row.get("source_repository"),
+ row.get("target_repository"),
+ row.get("provider_repository"),
+ row.get("consumer_repository"),
+ )
+ if repo and repo != alias
+ }
+ )
+
+
+def _legacy_contract_row(link: dict, side: str, relationship_type: str) -> dict:
+ """A ``cross_repo_impact`` contract row naming the *side* (consumer or provider)."""
+ return {
+ f"{side}_repo": link[f"{side}_repo"],
+ f"{side}_file": link[f"{side}_file"],
+ "contract_id": link["contract_id"],
+ "type": link["contract_type"],
+ "relationship_type": relationship_type,
+ "direction": "provider_to_consumer",
+ "evidence_kind": "contract",
+ }
+
+
+def _attach_impact(
+ r: dict,
+ alias: str,
+ cross_state: dict,
+ cross_partners: list[dict],
+ links: dict[str, list[dict]],
+ collector: OmissionCollector | None,
+) -> None:
+ """The legacy ``cross_repo_impact`` block, for a target with cross-repo rows.
+
+ ``cross_repo_consumers`` keeps its old name; each row and the sibling
+ marker say it is historical co-change.
+ """
+ target = r["target"]
+ impact = r.setdefault("cross_repo_impact", {})
+ legacy_co_changes = [
+ {
+ "repo": p["repo"],
+ "file": p["file"],
+ "strength": p["strength"],
+ "relationship_type": "co_change",
+ "direction": "undirected",
+ "evidence_kind": "historical",
+ "provenance": "workspace_git_history",
+ }
+ for p in cross_partners
+ ]
+ _set_capped(
+ impact,
+ "cross_repo_consumers",
+ legacy_co_changes,
+ collector,
+ target,
+ extra={
+ "cross_repo_consumers_semantics": "historical_co_change_partners",
+ "cross_repo_consumers_deprecated": True,
+ },
+ )
+ affected = _affected_repos(alias, links["all"])
+ impact["cross_repo_consumers_analysis"] = cross_state
+ impact["affected_repos"] = affected
+ impact["affected_repos_total"] = len(affected)
+ impact["affected_repos_emitted"] = len(affected)
+ impact["affected_repos_truncated"] = False
+ provider_side = [lk for lk in links["provider"] if lk.get("consumer_repo") != alias]
+ consumer_side = [lk for lk in links["consumer"] if lk.get("provider_repo") != alias]
+ if provider_side:
+ rows = [_legacy_contract_row(lk, "consumer", "contract_consumer") for lk in provider_side]
+ _set_capped(impact, "contract_consumers", rows, collector, target)
+ if consumer_side:
+ rows = [_legacy_contract_row(lk, "provider", "contract_provider") for lk in consumer_side]
+ _set_capped(impact, "contract_providers", rows, collector, target)
+
+
+def _enrich_target(
+ r: dict,
+ enricher: Any,
+ states: tuple[dict, dict],
+ alias: str,
+ collector: OmissionCollector | None,
+ include_graph: bool,
+) -> None:
+ """Typed workspace relationships for one target card.
+
+ *states* is the enricher's ``(cross_repo_analysis, contract_analysis)``.
+ """
+ target = r["target"]
+ cross_state, contract_state = states
+ cross_partners = enricher.get_cross_repo_partners(alias, target)
+ provider_links = enricher.get_contract_links_as_provider(alias, target)
+ consumer_links = enricher.get_contract_links_as_consumer(alias, target)
+ package_consumers = enricher.get_package_consumers(alias)
+ graph_collector = collector if include_graph else None
+
+ consumers = [_contract_consumer(link, alias, target) for link in provider_links]
+ _set_capped(r, "consumers", consumers, graph_collector, target)
+ r["relationship_analysis"]["consumers"] = {
+ **contract_state,
+ "scope": "workspace_contract_links",
+ "evidence_kind": "contract",
+ }
+ links = _cross_repo_links(
+ alias, target, cross_partners, provider_links, consumer_links, package_consumers
+ )
+ _set_capped(r, "cross_repo_links", links, graph_collector, target)
+ r["relationship_analysis"]["cross_repo"] = _combined_analysis(
+ cross_state, contract_state, links
+ )
+ if links:
+ _attach_impact(
+ r,
+ alias,
+ cross_state,
+ cross_partners,
+ {"all": links, "provider": provider_links, "consumer": consumer_links},
+ collector,
+ )
+
+
async def _enrich_cross_repo(
results: list[dict],
alias: str,
@@ -135,191 +330,9 @@ async def _enrich_cross_repo(
enricher = _state._cross_repo_enricher
if enricher is None or not _is_workspace_mode():
return
- cross_state = enricher.cross_repo_analysis
- contract_state = enricher.contract_analysis
+ states = (enricher.cross_repo_analysis, enricher.contract_analysis)
for r in results:
- target = r["target"]
- cross_partners = enricher.get_cross_repo_partners(alias, target)
- provider_links = enricher.get_contract_links_as_provider(alias, target)
- consumer_links = enricher.get_contract_links_as_consumer(alias, target)
- package_consumers = enricher.get_package_consumers(alias)
-
- consumers = [_contract_consumer(link, alias, target) for link in provider_links]
- r["consumers"] = consumers[:_RELATIONSHIP_LIMIT]
- r["consumers_total"] = len(consumers)
- r["consumers_emitted"] = len(r["consumers"])
- r["consumers_truncated"] = len(r["consumers"]) < len(consumers)
- cap_collection(
- r,
- "consumers",
- consumers,
- _RELATIONSHIP_LIMIT,
- collector if include_graph else None,
- label=f"{target} :: consumers beyond cap={_RELATIONSHIP_LIMIT}",
- preserve_counts=True,
- )
- r["relationship_analysis"]["consumers"] = {
- **contract_state,
- "scope": "workspace_contract_links",
- "evidence_kind": "contract",
- }
-
- co_change_links = [
- _cross_repo_co_change(alias, target, partner) for partner in cross_partners
- ]
- provider_rows = [
- _contract_consumer(link, alias, target)
- for link in provider_links
- if link.get("consumer_repo") != alias
- ]
- consumer_rows = [
- _contract_provider(link, alias, target)
- for link in consumer_links
- if link.get("provider_repo") != alias
- ]
- package_rows = [_package_consumer(link, target) for link in package_consumers]
- cross_repo_links = [*provider_rows, *consumer_rows, *package_rows, *co_change_links]
- priority = {"contract": 0, "structural": 1, "historical": 2}
- cross_repo_links.sort(
- key=lambda row: (
- priority.get(row["evidence_kind"], 9),
- row.get("target_repository", row.get("consumer_repository", "")),
- row.get("target_file", row.get("consumer_file", "")),
- row.get("source_repository", row.get("provider_repository", "")),
- )
- )
- r["cross_repo_links"] = cross_repo_links[:_RELATIONSHIP_LIMIT]
- r["cross_repo_links_total"] = len(cross_repo_links)
- r["cross_repo_links_emitted"] = len(r["cross_repo_links"])
- r["cross_repo_links_truncated"] = len(r["cross_repo_links"]) < len(cross_repo_links)
- cap_collection(
- r,
- "cross_repo_links",
- cross_repo_links,
- _RELATIONSHIP_LIMIT,
- collector if include_graph else None,
- label=f"{target} :: cross_repo_links beyond cap={_RELATIONSHIP_LIMIT}",
- preserve_counts=True,
- )
- r["relationship_analysis"]["cross_repo"] = _combined_analysis(
- cross_state, contract_state, cross_repo_links
- )
-
- if not cross_repo_links:
- continue
-
- affected_repos = sorted(
- {
- repo
- for row in cross_repo_links
- for repo in (
- row.get("source_repository"),
- row.get("target_repository"),
- row.get("provider_repository"),
- row.get("consumer_repository"),
- )
- if repo and repo != alias
- }
- )
- impact = r.setdefault("cross_repo_impact", {})
- legacy_co_changes = [
- {
- "repo": p["repo"],
- "file": p["file"],
- "strength": p["strength"],
- "relationship_type": "co_change",
- "direction": "undirected",
- "evidence_kind": "historical",
- "provenance": "workspace_git_history",
- }
- for p in cross_partners
- ]
- # Compatibility window: the old name remains, but every row and the
- # sibling marker state precisely that it is historical co-change.
- impact["cross_repo_consumers"] = legacy_co_changes[:_RELATIONSHIP_LIMIT]
- impact["cross_repo_consumers_semantics"] = "historical_co_change_partners"
- impact["cross_repo_consumers_deprecated"] = True
- impact["cross_repo_consumers_total"] = len(legacy_co_changes)
- impact["cross_repo_consumers_emitted"] = len(impact["cross_repo_consumers"])
- impact["cross_repo_consumers_truncated"] = len(impact["cross_repo_consumers"]) < len(
- legacy_co_changes
- )
- cap_collection(
- impact,
- "cross_repo_consumers",
- legacy_co_changes,
- _RELATIONSHIP_LIMIT,
- collector,
- label=f"{target} :: cross_repo_consumers beyond cap={_RELATIONSHIP_LIMIT}",
- preserve_counts=True,
- )
- impact["cross_repo_consumers_analysis"] = cross_state
- impact["affected_repos"] = affected_repos
- impact["affected_repos_total"] = len(affected_repos)
- impact["affected_repos_emitted"] = len(affected_repos)
- impact["affected_repos_truncated"] = False
- cross_provider_links = [
- link for link in provider_links if link.get("consumer_repo") != alias
- ]
- cross_consumer_links = [
- link for link in consumer_links if link.get("provider_repo") != alias
- ]
- if cross_provider_links:
- contract_consumers = [
- {
- "consumer_repo": lk["consumer_repo"],
- "consumer_file": lk["consumer_file"],
- "contract_id": lk["contract_id"],
- "type": lk["contract_type"],
- "relationship_type": "contract_consumer",
- "direction": "provider_to_consumer",
- "evidence_kind": "contract",
- }
- for lk in cross_provider_links
- ]
- impact["contract_consumers"] = contract_consumers[:_RELATIONSHIP_LIMIT]
- impact["contract_consumers_total"] = len(cross_provider_links)
- impact["contract_consumers_emitted"] = len(impact["contract_consumers"])
- impact["contract_consumers_truncated"] = len(impact["contract_consumers"]) < len(
- cross_provider_links
- )
- cap_collection(
- impact,
- "contract_consumers",
- contract_consumers,
- _RELATIONSHIP_LIMIT,
- collector,
- label=f"{target} :: contract_consumers beyond cap={_RELATIONSHIP_LIMIT}",
- preserve_counts=True,
- )
- if cross_consumer_links:
- contract_providers = [
- {
- "provider_repo": lk["provider_repo"],
- "provider_file": lk["provider_file"],
- "contract_id": lk["contract_id"],
- "type": lk["contract_type"],
- "relationship_type": "contract_provider",
- "direction": "provider_to_consumer",
- "evidence_kind": "contract",
- }
- for lk in cross_consumer_links
- ]
- impact["contract_providers"] = contract_providers[:_RELATIONSHIP_LIMIT]
- impact["contract_providers_total"] = len(cross_consumer_links)
- impact["contract_providers_emitted"] = len(impact["contract_providers"])
- impact["contract_providers_truncated"] = len(impact["contract_providers"]) < len(
- cross_consumer_links
- )
- cap_collection(
- impact,
- "contract_providers",
- contract_providers,
- _RELATIONSHIP_LIMIT,
- collector,
- label=f"{target} :: contract_providers beyond cap={_RELATIONSHIP_LIMIT}",
- preserve_counts=True,
- )
+ _enrich_target(r, enricher, states, alias, collector, include_graph)
async def _enrich_health(results: list[dict], ctx: Any, repo_id: str) -> None: