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
feat(workspace): diagnostics name dangling calls and say which HTTP calls linked, not just which were read (#4551)
Raghav Chamadiyabdf00f1f673d
diff --git a/docs/scale/WORKSPACES.md b/docs/scale/WORKSPACES.mdindex 7d3567524..02962acb7 100644--- a/docs/scale/WORKSPACES.md+++ b/docs/scale/WORKSPACES.md@@ -332,14 +332,14 @@ repowise workspace diagnostics --repo api # limit to one repoThe report covers:- **Provider / consumer counts** per repo, broken down by contract type.-- **Unmatched consumers**, grouped by reason:- - `no_provider`, no provider anywhere declares a matching route/service/topic.- - `internal_only`, the only matching provider is in the same repo + service, so the call is intra-service and intentionally not surfaced as a cross-repo link.+- **Unmatched consumers**, grouped by reason. Each call is named (HTTP, gRPC and topics first); table reads collapse to one line per repo.+ - `no_provider`, a dangling call: no provider anywhere declares a matching route/service/topic/table.- `unlinked`, a cross-service provider with a matching id exists, but no link formed (a candidate worth inspecting).- `external_host`, the call targets a literal third-party host (Stripe, Formspree, ...) that is not a workspace service, so it is intentionally excluded from matching.-- **Orphan providers**, endpoints declared but never consumed by any repo.+- **In-service consumers** (`in_service_consumers`, reason `internal_only`): the only matching provider is in the same repo + service. They are served, so they are counted apart and never listed as unmatched.+- **Orphan providers**, endpoints declared but never consumed by any repo. One row per repo, service and id; a table declared in a model and a migration lists the second file under `other_files`.- **Weak links**, matched links below the confidence threshold.-- **Extraction coverage**: how many contracts came from the parsed symbol table (`index`) versus a text dialect (`regex`), and how many HTTP client calls were located but could not be resolved to an endpoint. The HTTP coverage percentage is calls resolved over calls located, plus one per file that makes a network call (a client library, channel or raw request) no dialect read, so an unsupported client lowers the figure instead of dropping out of it. Each such file counts once, so it is a floor on what was missed, not total recall. It is the figure that turns a large orphan-provider count from alarming into explained.+- **Extraction coverage**: how many contracts came from the parsed symbol table (`index`) versus a text dialect (`regex`), and how many HTTP client calls were located but could not be resolved to an endpoint. The HTTP coverage percentage is calls resolved over calls located, plus one per file that makes a network call (a client library, channel or raw request) no dialect read, so an unsupported client lowers the figure instead of dropping out of it. Each such file counts once, so it is a floor on what was missed, not total recall. It is the figure that turns a large orphan-provider count from alarming into explained. A call read is not a call linked, so the headline leads with the HTTP calls that linked to another service (`http_consumers_linked`, `http_link_coverage`, same denominator) and then says where the read but unlinked calls went.The same data is available over REST at `GET /api/workspace/diagnostics` and is embedded in the system graph artifact's `diagnostics` block.diff --git a/packages/cli/src/repowise/cli/commands/workspace_cmd/diagnostics.py b/packages/cli/src/repowise/cli/commands/workspace_cmd/diagnostics.pyindex 1cefa3be0..68e8b6a41 100644--- a/packages/cli/src/repowise/cli/commands/workspace_cmd/diagnostics.py+++ b/packages/cli/src/repowise/cli/commands/workspace_cmd/diagnostics.py@@ -54,10 +54,10 @@ def workspace_diagnostics()diag = graph.diagnostics- breakdown, unmatched, orphans = _filter_to_repo(diag, repo_alias)+ breakdown, unmatched, orphans, in_service = _filter_to_repo(diag, repo_alias)if fmt == "json":- _emit_diagnostics_json(graph, breakdown, unmatched, orphans)+ _emit_diagnostics_json(graph, breakdown, unmatched, orphans, in_service)return_report_artifact_provenance(@@ -72,6 +72,7 @@ def workspace_diagnostics(_print_link_totals(diag)_print_extraction_coverage(diag)_print_unmatched(unmatched)+ _print_in_service(in_service)_print_orphans(orphans)if not unmatched and not orphans:@@ -82,17 +83,21 @@ def workspace_diagnostics(def _filter_to_repo(diag: ExtractionDiagnostics, repo_alias: str | None-) -> tuple[list[RepoDiagnostics], list[UnmatchedConsumer], list[OrphanProvider]]:+) -> tuple[+ list[RepoDiagnostics], list[UnmatchedConsumer], list[OrphanProvider], list[UnmatchedConsumer]+]:"""The per-repo lists, narrowed to *repo_alias* when one is given."""breakdown = diag.repo_breakdownunmatched = diag.unmatched_consumersorphans = diag.orphan_providers+ in_service = diag.in_service_consumersif not repo_alias:- return breakdown, unmatched, orphans+ return breakdown, unmatched, orphans, in_servicereturn ([r for r in breakdown if r.repo == repo_alias],[u for u in unmatched if u.repo == repo_alias],[o for o in orphans if o.repo == repo_alias],+ [u for u in in_service if u.repo == repo_alias],)@@ -101,6 +106,7 @@ def _emit_diagnostics_json(breakdown: list[RepoDiagnostics],unmatched: list[UnmatchedConsumer],orphans: list[OrphanProvider],+ in_service: list[UnmatchedConsumer],) -> None:from repowise.core.workspace.graph_evidence import graph_evidence@@ -116,10 +122,13 @@ def _emit_diagnostics_json("unmatched_consumers": [u.to_dict() for u in unmatched],"unmatched_by_reason": diag.unmatched_by_reason,"orphan_providers": [o.to_dict() for o in orphans],+ "in_service_consumers": [u.to_dict() for u in in_service],"providers_by_layer": diag.providers_by_layer,"consumers_by_layer": diag.consumers_by_layer,"http_consumers_unresolved": diag.http_consumers_unresolved,"http_consumer_coverage": diag.http_consumer_coverage,+ "http_consumers_linked": diag.http_consumers_linked,+ "http_link_coverage": diag.http_link_coverage,"unrecognised_call_files": diag.unrecognised_call_files,"evidence": graph_evidence(graph).to_dict(),}@@ -180,31 +189,42 @@ def _print_extraction_coverage(diag: ExtractionDiagnostics) -> None:def _print_call_coverage(diag: ExtractionDiagnostics) -> None:- """Resolved calls over every call seen, misses named per repo.+ """Linked calls, then read calls, over every call seen; misses named.- The denominator carries one call per file the network probe found calling- out with no contract read from it, so an unknown client library lowers the- figure. An artifact older than the probe gets counts and no percentage.+ A call read but matched to nothing is not coverage, so the headline leads+ with the calls that linked to another service and says what became of the+ rest. The denominator carries one call per file the network probe found+ calling out with no contract read from it, so an unknown client library+ lowers both figures. An artifact older than the probe gets counts only."""- http_consumers = sum(r.consumers_by_type.get("http", 0) for r in diag.repo_breakdown)+ read = diag.http_consumers_readunresolved = diag.http_consumers_unresolvedmissed = diag.unrecognised_call_file_count- coverage = diag.http_consumer_coverageif missed is None:console.print(- f" HTTP calls {http_consumers} resolved, {unresolved} located but not "+ f" HTTP calls {read} read, {unresolved} located but not ""statically resolvable; calls no dialect recognises were not counted ""(this artifact predates that count).")return- if coverage is None:+ seen = diag.http_calls_seen+ if seen is None:console.print(" HTTP calls none located, and no file makes a recognisable network call.")return+ linked = diag.http_consumers_linked+ if linked is None:+ head = f"{read} of {seen} read ([bold]{read * 100 / seen:.0f}%[/bold]), links not counted"+ else:+ head = (+ f"{linked} of {seen} linked to another service "+ f"([bold]{linked * 100 / seen:.0f}%[/bold]); {read} read ({read * 100 / seen:.0f}%)"+ )console.print(- f" HTTP calls {http_consumers} of {http_consumers + unresolved + missed} resolved "- f"([bold]{coverage * 100:.0f}%[/bold]): {unresolved} located but not statically "+ f" HTTP calls {head}: {unresolved} located but not statically "f"resolvable, {missed} file(s) calling out through code no dialect reads.")+ if linked is not None and read > linked and (split := _unlinked_http_split(diag)):+ console.print(f" [dim]read, not linked: {split}[/dim]")by_repo = [(repo, n) for repo, n in (diag.unrecognised_call_files or {}).items() if n]if by_repo:console.print(@@ -214,32 +234,120 @@ def _print_call_coverage(diag: ExtractionDiagnostics) -> None:)+def _unlinked_http_split(diag: ExtractionDiagnostics) -> str:+ """Where the read but unlinked HTTP calls went, workspace wide."""+ counts = Counter(u.reason for u in diag.unmatched_consumers if u.contract_type == "http")+ in_service = sum(1 for u in diag.in_service_consumers if u.contract_type == "http")+ parts = [+ f"{n} {_HTTP_REASON_SHORT.get(reason, reason)}"+ for reason, n in sorted(counts.items(), key=lambda kv: _reason_rank(kv[0]))+ ]+ if in_service:+ parts.append(f"{in_service} served inside their own service")+ return ", ".join(parts)+++_HTTP_REASON_SHORT = {+ "no_provider": "match no declared route",+ "unlinked": "match a route that did not link",+ "external_host": "call a third-party host",+}+_REASON_LABELS = {- "no_provider": "no matching provider found",- "internal_only": "provider is same repo + service (intra-service)",+ "no_provider": "dangling: no repo declares the route, service, topic or table they use","unlinked": "matching provider exists but no link formed",+ "external_host": "call a third-party host, outside the workspace",}+#: Reasons in reading order: dangling calls first, any other reason next,+#: third-party hosts last.+_REASON_RANK = {"no_provider": 0, "unlinked": 1, "external_host": 3}++#: Call types listed first; tables collapse to one line per repo.+_TYPE_ORDER = ("http", "grpc", "topic")+_DATA_TYPE = "data"+_ROW_CAP = 20+_NAME_CAP = 12+++def _reason_rank(reason: str) -> tuple[int, str]:+ return _REASON_RANK.get(reason, 2), reason+++def _type_rank(contract_type: str) -> int:+ return _TYPE_ORDER.index(contract_type) if contract_type in _TYPE_ORDER else len(_TYPE_ORDER)+++def _print_rows(rows: list[UnmatchedConsumer] | list[OrphanProvider], indent: str) -> None:+ """Call-shaped rows, HTTP, gRPC and topics first, capped."""+ ordered = sorted(rows, key=lambda r: _type_rank(r.contract_type))+ for r in ordered[:_ROW_CAP]:+ others = getattr(r, "other_files", None)+ extra = f" +{len(others)} more file(s)" if others else ""+ console.print(+ f"{indent}[dim]{r.repo}[/dim] {r.contract_id} ([dim]{r.file_path}{extra}[/dim])"+ )+ if len(ordered) > _ROW_CAP:+ console.print(f"{indent}[dim]... and {len(ordered) - _ROW_CAP} more[/dim]")+++def _print_tables(+ rows: list[UnmatchedConsumer] | list[OrphanProvider], indent: str, verb: str, tail: str+) -> None:+ """Table rows as one line per repo: the distinct names, capped."""+ by_repo: dict[str, set[str]] = {}+ for r in rows:+ by_repo.setdefault(r.repo, set()).add(r.contract_id.split("::", 1)[-1])+ for repo, names in sorted(by_repo.items()):+ shown = ", ".join(sorted(names)[:_NAME_CAP])+ more = f", +{len(names) - _NAME_CAP} more" if len(names) > _NAME_CAP else ""+ console.print(+ f"{indent}[dim]{repo}[/dim] {verb} {len(names)} table(s){tail}: {shown}{more}"+ )+def _print_unmatched(unmatched: list[UnmatchedConsumer]) -> None:- """Unmatched consumers grouped by reason.+ """Unmatched consumers by reason, each call named and tables per repo.Counts come from the (possibly filtered) list, so they agree with the total.+ Calls to third-party hosts are counted, not listed: they are out of scope+ on purpose."""if not unmatched:return- by_reason = Counter(u.reason for u in unmatched)+ by_reason: dict[str, list[UnmatchedConsumer]] = {}+ for u in unmatched:+ by_reason.setdefault(u.reason, []).append(u)console.print(f"\n [bold]{len(unmatched)}[/bold] unmatched consumer(s):")- for reason, count in sorted(by_reason.items()):- label = _REASON_LABELS.get(reason, reason)- console.print(f" [yellow]{count}[/yellow] — {label}")+ for reason in sorted(by_reason, key=_reason_rank):+ rows = by_reason[reason]+ console.print(f" [yellow]{len(rows)}[/yellow] {_REASON_LABELS.get(reason, reason)}")+ if reason == "external_host":+ continue+ _print_rows([r for r in rows if r.contract_type != _DATA_TYPE], " ")+ _print_tables(+ [r for r in rows if r.contract_type == _DATA_TYPE], " ", "reads", ""+ )+++def _print_in_service(in_service: list[UnmatchedConsumer]) -> None:+ """Consumers served by their own service: counted apart, never unmatched."""+ if not in_service:+ return+ by_type = Counter(u.contract_type for u in in_service)+ split = ", ".join(f"{n} {t}" for t, n in by_type.most_common())+ console.print(+ f"\n [bold]{len(in_service)}[/bold] consumer(s) served inside their own "+ f"service, not cross-repo ({split})."+ )def _print_orphans(orphans: list[OrphanProvider]) -> None:+ """Providers no consumer reaches: endpoints named, tables one line per repo."""if not orphans:returnconsole.print(f"\n [bold]{len(orphans)}[/bold] orphan provider(s) (declared, never consumed):")- for o in orphans[:20]:- console.print(f" [dim]{o.repo}[/dim] {o.contract_id} ([dim]{o.file_path}[/dim])")- if len(orphans) > 20:- console.print(f" [dim]... and {len(orphans) - 20} more[/dim]")+ _print_rows([o for o in orphans if o.contract_type != _DATA_TYPE], " ")+ _print_tables(+ [o for o in orphans if o.contract_type == _DATA_TYPE], " ", "declares", " nothing reads"+ )diff --git a/packages/core/src/repowise/core/workspace/diagnostics.py b/packages/core/src/repowise/core/workspace/diagnostics.pyindex 65ac5bfb1..097ca6a96 100644--- a/packages/core/src/repowise/core/workspace/diagnostics.py+++ b/packages/core/src/repowise/core/workspace/diagnostics.py@@ -50,6 +50,8 @@ class UnmatchedReason:NO_PROVIDER = "no_provider"#: The only matching provider(s) live in the same repo + service, so the#: call is intra-service and intentionally not surfaced as a cross-repo link.+ #: Such a consumer is served, so it goes to ``in_service_consumers``, never+ #: to the unmatched list.INTERNAL_ONLY = "internal_only"#: A cross-service provider with a matching id exists, but no link was#: formed (e.g. an HTTP path that only the candidate pass could bridge and@@ -118,6 +120,9 @@ class OrphanProvider:file_path: strcontract_id: strcontract_type: str+ #: Other files of the same repo and service declaring the same id, such as+ #: a table's model and its migration. One orphan, not one per file.+ other_files: list[str] = field(default_factory=list)def to_dict(self) -> dict[str, Any]:return asdict(self)@@ -315,6 +320,10 @@ class ExtractionDiagnostics:unmatched_consumers: list[UnmatchedConsumer] = field(default_factory=list)unmatched_by_reason: dict[str, int] = field(default_factory=dict)orphan_providers: list[OrphanProvider] = field(default_factory=list)+ #: Consumers whose only provider is their own repo + service: served, not+ #: cross-repo, so kept out of ``unmatched_consumers``. Reason is always+ #: :attr:`UnmatchedReason.INTERNAL_ONLY`.+ in_service_consumers: list[UnmatchedConsumer] = field(default_factory=list)#: Workspace-wide rollups of the per-repo layer split.providers_by_layer: dict[str, int] = field(default_factory=dict)consumers_by_layer: dict[str, int] = field(default_factory=dict)@@ -335,6 +344,9 @@ class ExtractionDiagnostics:#: contract (:mod:`.network_probe`): calls no dialect recognised. None when#: no repo was probed (an artifact older than the probe).unrecognised_call_files: dict[str, int] | None = None+ #: HTTP consumers that formed a cross-repo link. None on an artifact+ #: written before this count, which only knew which calls were read.+ http_consumers_linked: int | None = None@propertydef unrecognised_call_file_count(self) -> int | None:@@ -354,16 +366,40 @@ class ExtractionDiagnostics:artifact predates the probe, since a ratio over located calls alonereads 100% whatever was missed."""+ seen = self.http_calls_seen+ if seen is None:+ return None+ return self.http_consumers_read / seen++ @property+ def http_consumers_read(self) -> int:+ """HTTP consumer contracts read, linked or not."""+ return sum(r.consumers_by_type.get("http", 0) for r in self.repo_breakdown)++ @property+ def http_calls_seen(self) -> int | None:+ """The coverage denominator: calls read, calls located, unread files.++ None when it is zero or the artifact predates the network probe.+ """missed = self.unrecognised_call_file_countif missed is None:return None- http_consumers = sum(- r.consumers_by_type.get("http", 0) for r in self.repo_breakdown- )- denominator = http_consumers + self.http_consumers_unresolved + missed- if denominator == 0:+ seen = self.http_consumers_read + self.http_consumers_unresolved + missed+ return seen or None++ @property+ def http_link_coverage(self) -> float | None:+ """Share of the calls seen that linked to another service's provider.++ Same denominator as :attr:`http_consumer_coverage`, so a call that was+ read but matched nothing counts against it. None where that one is+ None, or when the artifact predates the linked count.+ """+ seen = self.http_calls_seen+ if seen is None or self.http_consumers_linked is None:return None- return http_consumers / denominator+ return self.http_consumers_linked / seendef to_dict(self) -> dict[str, Any]:return {@@ -375,10 +411,13 @@ class ExtractionDiagnostics:"unmatched_consumers": [u.to_dict() for u in self.unmatched_consumers],"unmatched_by_reason": self.unmatched_by_reason,"orphan_providers": [o.to_dict() for o in self.orphan_providers],+ "in_service_consumers": [u.to_dict() for u in self.in_service_consumers],"providers_by_layer": self.providers_by_layer,"consumers_by_layer": self.consumers_by_layer,"http_consumers_unresolved": self.http_consumers_unresolved,"http_consumer_coverage": self.http_consumer_coverage,+ "http_consumers_linked": self.http_consumers_linked,+ "http_link_coverage": self.http_link_coverage,"symbol_identity": {r: v.to_dict() for r, v in sorted(self.symbol_identity.items())},"schema_coverage": self.schema_coverage.to_dict(),"code_api": self.code_api.to_dict(),@@ -392,6 +431,14 @@ class ExtractionDiagnostics:schema = data.get("schema_coverage") or {}code = data.get("code_api") or {}openapi = data.get("openapi") or {}+ unmatched = [_unmatched_from_dict(u) for u in data.get("unmatched_consumers", [])]+ in_service = [_unmatched_from_dict(u) for u in data.get("in_service_consumers", [])]+ by_reason = dict(data.get("unmatched_by_reason", {}))+ # An artifact written before in-service consumers were split out lists+ # them as unmatched; move them so every reader sees one shape.+ in_service += [u for u in unmatched if u.reason == UnmatchedReason.INTERNAL_ONLY]+ unmatched = [u for u in unmatched if u.reason != UnmatchedReason.INTERNAL_ONLY]+ by_reason.pop(UnmatchedReason.INTERNAL_ONLY, None)return cls(total_providers=data.get("total_providers", 0),total_consumers=data.get("total_consumers", 0),@@ -410,26 +457,19 @@ class ExtractionDiagnostics:)for r in data.get("repo_breakdown", [])],- unmatched_consumers=[- UnmatchedConsumer(- repo=u.get("repo", ""),- file_path=u.get("file_path", ""),- contract_id=u.get("contract_id", ""),- contract_type=u.get("contract_type", ""),- reason=u.get("reason", UnmatchedReason.NO_PROVIDER),- )- for u in data.get("unmatched_consumers", [])- ],- unmatched_by_reason=data.get("unmatched_by_reason", {}),+ unmatched_consumers=unmatched,+ unmatched_by_reason=by_reason,orphan_providers=[OrphanProvider(repo=o.get("repo", ""),file_path=o.get("file_path", ""),contract_id=o.get("contract_id", ""),contract_type=o.get("contract_type", ""),+ other_files=list(o.get("other_files", [])),)for o in data.get("orphan_providers", [])],+ in_service_consumers=in_service,providers_by_layer=data.get("providers_by_layer", {}),consumers_by_layer=data.get("consumers_by_layer", {}),http_consumers_unresolved=data.get("http_consumers_unresolved", 0),@@ -459,9 +499,20 @@ class ExtractionDiagnostics:),openapi=OpenApiCoverage.from_dict(openapi),unrecognised_call_files=data.get("unrecognised_call_files"),+ http_consumers_linked=data.get("http_consumers_linked"),)+def _unmatched_from_dict(u: dict[str, Any]) -> UnmatchedConsumer:+ return UnmatchedConsumer(+ repo=u.get("repo", ""),+ file_path=u.get("file_path", ""),+ contract_id=u.get("contract_id", ""),+ contract_type=u.get("contract_type", ""),+ reason=u.get("reason", UnmatchedReason.NO_PROVIDER),+ )++# ---------------------------------------------------------------------------# Builder# ---------------------------------------------------------------------------@@ -561,40 +612,13 @@ def build_diagnostics(for p in providers:providers_by_norm_id[normalize_contract_id(p.contract_id)].append(p)- # Unmatched consumers, grouped by reason --------------------------------- unmatched: list[UnmatchedConsumer] = []+ unmatched, in_service = _unmatched_consumers(+ consumers, matched_consumers, providers_by_norm_id+ )by_reason: dict[str, int] = defaultdict(int)- for c in consumers:- key = _contract_key(c.repo, c.file_path, c.contract_id)- # A binding is wiring its queue's consumers are matched through, not a call.- if key in matched_consumers or c.meta.get("kind") == TOPIC_KIND_BINDING:- continue- reason = _classify_unmatched(c, providers_by_norm_id)- by_reason[reason] += 1- unmatched.append(- UnmatchedConsumer(- repo=c.repo,- file_path=c.file_path,- contract_id=c.contract_id,- contract_type=c.contract_type,- reason=reason,- )- )-- # Orphan providers — declared but never consumed ------------------------- orphans: list[OrphanProvider] = []- for p in providers:- key = _contract_key(p.repo, p.file_path, p.contract_id)- if key in matched_providers:- continue- orphans.append(- OrphanProvider(- repo=p.repo,- file_path=p.file_path,- contract_id=p.contract_id,- contract_type=p.contract_type,- )- )+ for u in unmatched:+ by_reason[u.reason] += 1+ orphans = _orphan_providers(providers, matched_providers)identity = {role: SymbolIdentity(@@ -651,6 +675,7 @@ def build_diagnostics(unmatched_consumers=unmatched,unmatched_by_reason=dict(sorted(by_reason.items())),orphan_providers=orphans,+ in_service_consumers=in_service,providers_by_layer=dict(sorted(prov_by_layer_all.items())),consumers_by_layer=dict(sorted(cons_by_layer_all.items())),# Summed from the stats, not from the per-repo breakdown: a repo whose@@ -664,9 +689,73 @@ def build_diagnostics(code_api=code_api,openapi=OpenApiCoverage.from_stats(stats_by_repo),unrecognised_call_files=_unrecognised_call_files(stats_by_repo),+ http_consumers_linked=sum(+ 1+ for c in consumers+ if c.contract_type == "http"+ and _contract_key(c.repo, c.file_path, c.contract_id) in matched_consumers+ ),)+def _unmatched_consumers(+ consumers: list[Contract],+ matched_consumers: set[tuple[str, str, str]],+ providers_by_norm_id: dict[str, list[Contract]],+) -> tuple[list[UnmatchedConsumer], list[UnmatchedConsumer]]:+ """Consumers that formed no link: (unmatched, served in their own service)."""+ unmatched: list[UnmatchedConsumer] = []+ in_service: list[UnmatchedConsumer] = []+ for c in consumers:+ key = _contract_key(c.repo, c.file_path, c.contract_id)+ # A binding is wiring its queue's consumers are matched through, not a call.+ if key in matched_consumers or c.meta.get("kind") == TOPIC_KIND_BINDING:+ continue+ reason = _classify_unmatched(c, providers_by_norm_id)+ row = UnmatchedConsumer(+ repo=c.repo,+ file_path=c.file_path,+ contract_id=c.contract_id,+ contract_type=c.contract_type,+ reason=reason,+ )+ (in_service if reason == UnmatchedReason.INTERNAL_ONLY else unmatched).append(row)+ return unmatched, in_service+++def _orphan_providers(+ providers: list[Contract], matched_providers: set[tuple[str, str, str]]+) -> list[OrphanProvider]:+ """Providers no consumer reaches, one row per repo, service and id.++ A table declared in a model and again in its migration is one endpoint, so+ the copies fold into ``other_files``. A group any consumer reached is not+ an orphan, whichever copy the link landed on.+ """+ groups: dict[tuple[str, str | None, str], list[Contract]] = {}+ for p in providers:+ key = (p.repo, p.service, normalize_contract_id(p.contract_id))+ groups.setdefault(key, []).append(p)+ orphans: list[OrphanProvider] = []+ for members in groups.values():+ if any(+ _contract_key(p.repo, p.file_path, p.contract_id) in matched_providers+ for p in members+ ):+ continue+ first = members[0]+ orphans.append(+ OrphanProvider(+ repo=first.repo,+ file_path=first.file_path,+ contract_id=first.contract_id,+ contract_type=first.contract_type,+ other_files=sorted({p.file_path for p in members} - {first.file_path}),+ )+ )+ return orphans++def _unrecognised_call_files(stats_by_repo: dict[str, dict[str, int]],) -> dict[str, int] | None:diff --git a/packages/core/src/repowise/core/workspace/reads.py b/packages/core/src/repowise/core/workspace/reads.pyindex 7abbef09c..56a83cf60 100644--- a/packages/core/src/repowise/core/workspace/reads.py+++ b/packages/core/src/repowise/core/workspace/reads.py@@ -189,9 +189,15 @@ def unmatched_reason(The reasons live in ``system_graph.json``, not ``contracts.json``, keyed bythe same ``(repo, file_path, contract_id)`` triple. None when no graph is- built or the consumer did match.+ built or the consumer did match. A consumer served inside its own service+ is listed apart and reads ``internal_only``."""- for u in (diagnostics or {}).get("unmatched_consumers", []):+ diagnostics = diagnostics or {}+ rows = [+ *diagnostics.get("unmatched_consumers", []),+ *diagnostics.get("in_service_consumers", []),+ ]+ for u in rows:if (u.get("repo") == repoand u.get("file_path") == file_pathdiff --git a/packages/server/src/repowise/server/schemas/workspace.py b/packages/server/src/repowise/server/schemas/workspace.pyindex 48ebb25bf..e7bd7fac2 100644--- a/packages/server/src/repowise/server/schemas/workspace.py+++ b/packages/server/src/repowise/server/schemas/workspace.py@@ -302,6 +302,8 @@ class WorkspaceOrphanProvider(BaseModel):file_path: strcontract_id: strcontract_type: str+ #: Other files of the same repo and service declaring the same id.+ other_files: list[str] = []class WorkspaceSymbolIdentity(BaseModel):@@ -357,6 +359,8 @@ class WorkspaceExtractionDiagnostics(BaseModel):unmatched_consumers: list[WorkspaceUnmatchedConsumer] = []unmatched_by_reason: dict[str, int] = {}orphan_providers: list[WorkspaceOrphanProvider] = []+ #: Consumers served by their own repo + service, kept out of the unmatched list.+ in_service_consumers: list[WorkspaceUnmatchedConsumer] = []providers_by_layer: dict[str, int] = {}consumers_by_layer: dict[str, int] = {}http_consumers_unresolved: int = 0@@ -364,6 +368,10 @@ class WorkspaceExtractionDiagnostics(BaseModel):#: file that calls out through code no dialect reads. ``None`` when nothing#: was seen (0/0 is not 100%) or the artifact predates that count.http_consumer_coverage: float | None = None+ #: HTTP consumers that linked to another service, and their share of the+ #: same denominator. ``None`` on an artifact written before the count.+ http_consumers_linked: int | None = None+ http_link_coverage: float | None = None#: Per repo, files with a network-call token and no consumer contract.#: ``None`` on an artifact written before the count existed.unrecognised_call_files: dict[str, int] | None = Nonediff --git a/packages/types/src/generated/http.ts b/packages/types/src/generated/http.tsindex 3b05f4cba..23db72b7b 100644--- a/packages/types/src/generated/http.ts+++ b/packages/types/src/generated/http.ts@@ -4670,10 +4670,13 @@ export interface WorkspaceExtractionDiagnostics {unmatched_consumers?: WorkspaceUnmatchedConsumer[];unmatched_by_reason?: Record<string, number>;orphan_providers?: WorkspaceOrphanProvider[];+ in_service_consumers?: WorkspaceUnmatchedConsumer[];providers_by_layer?: Record<string, number>;consumers_by_layer?: Record<string, number>;http_consumers_unresolved?: number;http_consumer_coverage?: number | null;+ http_consumers_linked?: number | null;+ http_link_coverage?: number | null;unrecognised_call_files?: Record<string, number> | null;unrecognised_call_file_count?: number | null;symbol_identity?: Record<string, WorkspaceSymbolIdentity>;@@ -4772,6 +4775,7 @@ export interface WorkspaceOrphanProvider {file_path: string;contract_id: string;contract_type: string;+ other_files?: string[];}export interface WorkspaceRepoActions {diff --git a/packages/types/src/workspace.ts b/packages/types/src/workspace.tsindex b03d0046e..512efe77f 100644--- a/packages/types/src/workspace.ts+++ b/packages/types/src/workspace.ts@@ -197,6 +197,8 @@ export interface OrphanProvider {file_path: string;contract_id: string;contract_type: string;+ /** Other files of the same repo and service declaring the same id. */+ other_files?: string[];}export interface ExtractionDiagnostics {@@ -208,6 +210,8 @@ export interface ExtractionDiagnostics {unmatched_consumers: UnmatchedConsumer[];unmatched_by_reason: Record<string, number>;orphan_providers: OrphanProvider[];+ /** Consumers served by their own repo + service, kept out of `unmatched_consumers`. */+ in_service_consumers?: UnmatchedConsumer[];/** Workspace-wide rollup of the per-repo `index` / `regex` split. */providers_by_layer: Record<string, number>;consumers_by_layer: Record<string, number>;@@ -219,6 +223,9 @@ export interface ExtractionDiagnostics {* seen (0/0 is not 100%) or the artifact predates that count.*/http_consumer_coverage: number | null;+ /** HTTP consumers that linked to another service, and their share of the same denominator. */+ http_consumers_linked?: number | null;+ http_link_coverage?: number | null;/** Per repo, files with a network-call token and no consumer contract; `null` before the count existed. */unrecognised_call_files?: Record<string, number> | null;unrecognised_call_file_count?: number | null;diff --git a/tests/unit/cli/test_workspace_cmd.py b/tests/unit/cli/test_workspace_cmd.pyindex ba9237bb4..ea19b63fd 100644--- a/tests/unit/cli/test_workspace_cmd.py+++ b/tests/unit/cli/test_workspace_cmd.py@@ -582,6 +582,7 @@ class TestWorkspaceDiagnosticsReport:consumers_by_layer={} if clean else {"regex": 12},http_consumers_unresolved=3,unrecognised_call_files={"web": 0},+ http_consumers_linked=4,)if not clean:diag.unmatched_consumers = [@@ -589,6 +590,9 @@ class TestWorkspaceDiagnosticsReport:UnmatchedConsumer("web", "b.ts", "http::GET::/y", "http", "mystery"),]diag.unmatched_by_reason = {"no_provider": 1, "mystery": 1}+ diag.in_service_consumers = [+ UnmatchedConsumer("api", "s.py", "data::t", "data", "internal_only"),+ ]diag.orphan_providers = [OrphanProvider("api", f"r{i}.py", f"http::GET::/o{i}", "http")for i in range(orphans)@@ -608,11 +612,13 @@ class TestWorkspaceDiagnosticsReport:assert "Extraction coverage" in outassert "Providers 30 (20 index, 10 regex)" in outassert "Consumers 12 (12 regex)" in out- assert "HTTP calls 9 of 12 resolved" in out- assert "75%" in out+ assert "HTTP calls 4 of 12 linked to another service (33%); 9 read (75%)" in out+ assert "read, not linked: 1 match no declared route, 1 mystery" in outassert "2 unmatched consumer(s)" in out- assert "no matching provider found" in out+ assert "dangling: no repo declares" in out+ assert "web http::GET::/x (a.ts)" in outassert "mystery" in out+ assert "1 consumer(s) served inside their own service, not cross-repo (1 data)" in outassert "23 orphan provider(s)" in outassert "http::GET::/o19" in outassert "http::GET::/o20" not in out@@ -650,6 +656,9 @@ class TestWorkspaceDiagnosticsReport:assert [r["repo"] for r in data["repo_breakdown"]] == ["web"]assert len(data["unmatched_consumers"]) == 2assert data["orphan_providers"] == []+ assert data["in_service_consumers"] == []+ assert data["http_consumers_linked"] == 4+ assert data["http_link_coverage"] == pytest.approx(4 / 12)# Totals and roll-ups are workspace-wide, not filtered.assert data["total_providers"] == 30assert data["unmatched_by_reason"] == {"no_provider": 1, "mystery": 1}@@ -674,8 +683,70 @@ class TestWorkspaceDiagnosticsReport:)assert result.exit_code == 0, result.outputassert "2 unmatched consumer(s)" in result.output- # Reason counts follow the filtered list, not the workspace roll-up.- assert "intra-service" not in result.output+ # An old artifact's in-service row is read as in-service, and the+ # repo filter drops it with the rest of api's rows.+ assert "served inside their own service, not cross-repo" not in result.output++ def test_read_not_linked_split_adds_up(self, runner, tmp_path):+ """Read minus linked equals the split, built from real diagnostics."""+ from repowise.core.workspace.contracts import Contract, ContractLink+ from repowise.core.workspace.diagnostics import build_diagnostics+ from repowise.core.workspace.system_graph import SystemGraph, save_system_graph++ _write_workspace_config(+ tmp_path,+ repos=[{"path": "api", "alias": "api"}, {"path": "web", "alias": "web"}],+ )++ def c(repo, cid, role, file):+ return Contract(repo=repo, contract_id=cid, contract_type="http", role=role,+ file_path=file, symbol_name="f", confidence=0.8)++ contracts = [+ c("api", "http::GET::/u", "provider", "h.py"),+ c("api", "http::GET::/own", "provider", "h.py"),+ c("api", "http::GET::/own", "consumer", "self.py"),+ c("web", "http::GET::/u", "consumer", "a.ts"),+ c("web", "http::GET::/none", "consumer", "b.ts"),+ ]+ links = [+ ContractLink(contract_id="http::GET::/u", contract_type="http", match_type="exact",+ confidence=0.9, provider_repo="api", provider_file="h.py",+ provider_symbol="f", provider_service=None, consumer_repo="web",+ consumer_file="a.ts", consumer_symbol="f", consumer_service=None),+ ]+ diag = build_diagnostics(contracts, links, {"web": {"call_files_unrecognised": 1}})+ save_system_graph(SystemGraph(diagnostics=diag, generated_at="t"), tmp_path)++ out = runner.invoke(cli, ["workspace", "diagnostics", str(tmp_path)]).output+ assert "HTTP calls 1 of 4 linked to another service (25%); 3 read (75%)" in out+ assert "read, not linked: 1 match no declared route, 1 served inside their own service" in out+ assert "web http::GET::/none (b.ts)" in out++ def test_tables_collapse_to_one_line_per_repo(self, runner, tmp_path):+ from repowise.core.workspace.diagnostics import OrphanProvider, UnmatchedConsumer+ from repowise.core.workspace.system_graph import load_system_graph, save_system_graph++ self._save(tmp_path, orphans=1)+ graph = load_system_graph(tmp_path)+ diag = graph.diagnostics+ diag.unmatched_consumers = [+ UnmatchedConsumer("web", f"q{i}.ts", f"data::t{i % 2}", "data", "no_provider")+ for i in range(4)+ ] + [UnmatchedConsumer("web", "g.ts", "grpc::S/M", "grpc", "no_provider")]+ diag.orphan_providers += [+ OrphanProvider("api", "m.py", "data::users", "data", ["mig.py"]),+ OrphanProvider("api", "n.py", "data::zones", "data"),+ ]+ save_system_graph(graph, tmp_path)++ out = runner.invoke(cli, ["workspace", "diagnostics", str(tmp_path)]).output+ assert "5 unmatched consumer(s)" in out+ assert "web grpc::S/M (g.ts)" in out+ assert "web reads 2 table(s): t0, t1" in out+ assert "3 orphan provider(s)" in out+ assert "api http::GET::/o0 (r0.py)" in out+ assert "api declares 2 table(s) nothing reads: users, zones" in out# ---------------------------------------------------------------------------diff --git a/tests/unit/workspace/test_diagnostics.py b/tests/unit/workspace/test_diagnostics.pyindex e9db9c2e3..e9c0f33bd 100644--- a/tests/unit/workspace/test_diagnostics.py+++ b/tests/unit/workspace/test_diagnostics.py@@ -111,16 +111,35 @@ def test_unmatched_no_provider():assert diag.unmatched_by_reason == {UnmatchedReason.NO_PROVIDER: 1}-def test_unmatched_internal_only():- # Provider and consumer live in the same repo + service: intra-service call,- # intentionally not surfaced as a cross-repo link.+def test_in_service_consumer_is_counted_apart_not_unmatched():+ # Provider and consumer live in the same repo + service: the call is served,+ # so it is not unmatched, and it is not a cross-repo link either.contracts = [_provider("api", "http::GET::/users", service="services/x"),_consumer("api", "http::GET::/users", service="services/x"),]diag = build_diagnostics(contracts, [])- assert len(diag.unmatched_consumers) == 1- assert diag.unmatched_consumers[0].reason == UnmatchedReason.INTERNAL_ONLY+ assert diag.unmatched_consumers == []+ assert diag.unmatched_by_reason == {}+ assert [u.reason for u in diag.in_service_consumers] == [UnmatchedReason.INTERNAL_ONLY]+++def test_old_artifact_internal_only_rows_move_to_in_service():+ from repowise.core.workspace.diagnostics import ExtractionDiagnostics++ row = {+ "repo": "api",+ "file_path": "c.py",+ "contract_id": "data::t",+ "contract_type": "data",+ "reason": "internal_only",+ }+ restored = ExtractionDiagnostics.from_dict(+ {"unmatched_consumers": [row], "unmatched_by_reason": {"internal_only": 1}}+ )+ assert restored.unmatched_consumers == []+ assert restored.unmatched_by_reason == {}+ assert [u.contract_id for u in restored.in_service_consumers] == ["data::t"]def test_orphan_provider_detected():@@ -130,6 +149,69 @@ def test_orphan_provider_detected():assert diag.orphan_providers[0].contract_id == "http::GET::/unused"+def test_orphan_declared_in_two_files_is_one_orphan():+ """A table in its model and its migration is one endpoint nobody reads."""+ contracts = [+ _provider("api", "data::payouts", ctype="data", file="app/Models/Payout.php"),+ _provider("api", "data::payouts", ctype="data", file="db/migrations/1_create.php"),+ _provider("worker", "data::payouts", ctype="data", file="m.py"),+ ]+ diag = build_diagnostics(contracts, [])+ by_repo = {o.repo: o for o in diag.orphan_providers}+ assert len(diag.orphan_providers) == 2+ assert by_repo["api"].file_path == "app/Models/Payout.php"+ assert by_repo["api"].other_files == ["db/migrations/1_create.php"]+ assert by_repo["worker"].other_files == []+++def test_orphan_copies_split_by_service():+ contracts = [+ _provider("mono", "http::GET::/h", file="a/h.py", service="a"),+ _provider("mono", "http::GET::/h", file="b/h.py", service="b"),+ ]+ assert len(build_diagnostics(contracts, []).orphan_providers) == 2+++def test_an_endpoint_linked_through_one_copy_is_not_an_orphan():+ contracts = [+ _provider("api", "http::GET::/u", file="h.py"),+ _provider("api", "http::GET::/u", file="h2.py"),+ _consumer("web", "http::GET::/u"),+ ]+ links = [_link("http::GET::/u", "api", "web")]+ assert build_diagnostics(contracts, links).orphan_providers == []+++def test_link_coverage_counts_linked_calls_not_read_ones():+ """Read 3 calls of 4 seen, linked 1: 25% linked, 75% read."""+ contracts = [+ _provider("api", "http::GET::/u"),+ _consumer("web", "http::GET::/u"),+ _consumer("web", "http::GET::/nowhere", file="d.py"),+ _consumer("web", "http::GET::/gone", file="e.py"),+ ]+ links = [_link("http::GET::/u", "api", "web")]+ diag = build_diagnostics(contracts, links, {"web": {"call_files_unrecognised": 1}})+ assert diag.http_consumers_linked == 1+ assert diag.http_consumer_coverage == 0.75+ assert diag.http_link_coverage == 0.25+ from repowise.core.workspace.diagnostics import ExtractionDiagnostics++ restored = ExtractionDiagnostics.from_dict(diag.to_dict())+ assert restored.to_dict() == diag.to_dict()+++def test_link_coverage_unknown_on_an_artifact_without_the_count():+ from repowise.core.workspace.diagnostics import ExtractionDiagnostics++ diag = build_diagnostics([_consumer("web", "http::GET::/u")], [], {"web": {"call_files_unrecognised": 0}})+ data = diag.to_dict()+ del data["http_consumers_linked"]+ restored = ExtractionDiagnostics.from_dict(data)+ assert restored.http_consumer_coverage == 1.0+ assert restored.http_link_coverage is None++def test_weak_link_counted_at_threshold():contracts = [_provider("api", "http::GET::/u"),diff --git a/tests/unit/workspace/test_reads.py b/tests/unit/workspace/test_reads.pyindex 1fdf0b291..c65da63d8 100644--- a/tests/unit/workspace/test_reads.py+++ b/tests/unit/workspace/test_reads.py@@ -87,6 +87,13 @@ def test_an_unmatched_consumer_names_its_reason():assert detail["unmatched_reason"] == "no_provider"+def test_a_consumer_served_in_its_own_service_names_its_reason():+ row = {k: CONSUMER[k] for k in ("repo", "file_path", "contract_id")}+ diagnostics = {"in_service_consumers": [{**row, "reason": "internal_only"}]}+ detail = contract_detail([CONSUMER], [], diagnostics, **row)+ assert detail["unmatched_reason"] == "internal_only"++def test_linked_filter_counts_the_consumer_under_its_own_id():linked = list_contracts([PROVIDER, CONSUMER], [LINK], linked=True)assert linked["total_contracts"] == 2diff --git a/tests/unit/workspace/test_system_graph.py b/tests/unit/workspace/test_system_graph.pyindex 3f0c06b37..5bebefc1e 100644--- a/tests/unit/workspace/test_system_graph.py+++ b/tests/unit/workspace/test_system_graph.py@@ -344,10 +344,13 @@ def test_system_graph_json_shape_is_locked():"unmatched_consumers","unmatched_by_reason","orphan_providers",+ "in_service_consumers","providers_by_layer","consumers_by_layer","http_consumers_unresolved","http_consumer_coverage",+ "http_consumers_linked",+ "http_link_coverage","symbol_identity","schema_coverage","openapi",