"""A live run's coverage, computed in code from the critic's own listing of every resource in scope (ADR 0017). The critic doesn't trust the planner's list: it lists the scope root again, with the same rules (app/scope.py), and compares that with the findings and the planner's declared gaps. Only counts and the uncovered ids go to the model, so a large estate doesn't fill its prompt. """ from __future__ import annotations from typing import Any, Dict from app import scope as sc MAX_IDS = 200 # uncovered ids passed to the model; the count is always exact def live_scope(payload: Dict[str, Any], call: sc.Call) -> Dict[str, Any]: provider = payload["provider"] items, refs, issues = sc.list_all(provider, call) accounting = sc.account(provider, items) key = (lambda i: i.lower()) if provider == "azure" else (lambda i: i) # ARM ids are case-insensitive found = {key(str(f.get("scope_entry_id") or "")) for f in payload.get("findings") or []} declared = {key(str(g.get("scope_entry_id") or "")) for g in payload.get("coverage_gaps") or []} uncovered = [e["id"] for e in accounting["entries"] if key(e["id"]) not in found | declared] return { "listed": accounting["listed"], "scope_entries": len(accounting["entries"]), "folded": len(accounting["folded"]), "structure": len(accounting["structure"]), "covered": len(accounting["entries"]) - len(uncovered), "coverage_gaps": uncovered[:MAX_IDS], "coverage_gap_count": len(uncovered), "listing_issues": issues, "evidence_refs": refs, } def live_dependencies(payload: Dict[str, Any]) -> Dict[str, Any]: """Every dependency a finding records, against the graph (ADR 0019): an edge from the finding's record to the target's record (or to the target itself, an external node), or a folded dependency. Computed in code, so the model never re-derives it.""" provider = payload["provider"] key = (lambda i: str(i or "").strip().rstrip("/").lower()) if provider == "azure" else \ (lambda i: str(i or "").strip().rstrip("/")) records = {key(r.get("scope_entry_id")): r.get("canonical_id") for r in payload.get("records") or []} edges = {(key(e.get("source")), key(e.get("target"))) for e in payload.get("edges") or []} folded = {(key(f.get("source")), key(f.get("target"))) for f in payload.get("folded_dependencies") or []} external = {key(n.get("id")) for n in payload.get("nodes") or [] if n.get("kind") == "external"} recorded, as_edges, to_external, as_folded, missing = 0, 0, 0, 0, [] for finding in payload.get("findings") or []: source = records.get(key(finding.get("scope_entry_id"))) for dep in finding.get("dependencies") or []: rid = dep.get("id") if isinstance(dep, dict) else dep if not rid or key(rid) == key(finding.get("scope_entry_id")): continue recorded += 1 target = records.get(key(rid)) or rid if source and (key(source), key(target)) in edges: as_edges += 1 to_external += key(target) in external elif source and (key(source), key(rid)) in folded: as_folded += 1 else: missing.append(f"{finding.get('scope_entry_id')} -> {rid}") return {"recorded": recorded, "edges": as_edges, "to_external_nodes": to_external, "folded": as_folded, "missing": missing[:MAX_IDS], "missing_count": len(missing)}