"""K-02's lookup, in code (F02 ADR 0013): does every disposition's evidence_ref resolve to a record in the package? A citation is `discovery://`. It resolves when the execution id is the run's, the agent is one whose records Assessment cites, and that agent's part of the Discovery package holds a record with that key in one of its id fields. A part's own field name (`itsm_signals`, say) also resolves: it cites the list, for example to show that it is empty. Whether the cited records *support* the rule-trace basis is the critic model's judgement, not this module's. The package is found the way the intake finds it (F02 ADR 0008, `cp1-assessment-intake-agent/app/intake.py`): the intake has already verified it, so it is read here, not checked again. Standard library only. """ from __future__ import annotations import json from typing import Any, Dict, Iterator, List, Optional, Set FILE_SEPARATOR = "\n\n---\n\n" # the platform's join between parsed files, as in the intake # The Discovery agents Assessment cites, and their part of the package (F01 ADR 0020). PART_OF = {"ALZ-DSC-03": "inventory", "ALZ-DSC-04": "cmdb", "ALZ-DSC-05": "classification", "ALZ-DSC-06": "dependencies", "ALZ-DSC-07": "network"} ID_FIELDS = ("canonical_id", "drift_id", "ci_ref", "edge_id", "id", "finding_id", "rule_id", "range_id", "flow_id") def _decode(value: Any, depth: int = 0) -> Any: while isinstance(value, str) and depth < 3: try: value, depth = json.loads(value), depth + 1 except ValueError: return value return value def _packages(value: Any, depth: int = 0) -> Iterator[Dict[str, Any]]: value = _decode(value) if depth > 6: return if isinstance(value, dict): if "package_version" in value and isinstance(value.get("manifest"), dict): yield value return for item in value.values(): yield from _packages(item, depth + 1) elif isinstance(value, list): for item in value: yield from _packages(item, depth + 1) def package(documents: Optional[str]) -> Optional[Dict[str, Any]]: found = [p for doc in (documents or "").split(FILE_SEPARATOR) for p in _packages(doc.strip())] return found[0] if len(found) == 1 else None def index(pkg: Dict[str, Any]) -> Dict[str, Set[str]]: """Each cited agent's record keys: the id fields of every record in its part, and the part's own field names.""" keys: Dict[str, Set[str]] = {} for agent, part in PART_OF.items(): found: Set[str] = set() body = pkg.get(part) or {} for field, value in (body.items() if isinstance(body, dict) else []): found.add(field) for record in value if isinstance(value, list) else []: if isinstance(record, dict): found.update(str(record[f]) for f in ID_FIELDS if record.get(f) not in (None, "")) keys[agent] = found return keys def unresolved(ref: Any, execution_id: str, keys: Dict[str, Set[str]]) -> Optional[str]: """Why `ref` doesn't resolve, or None when it does.""" text = str(ref or "") if not text.startswith("discovery:"): return "not a discovery: citation" parts = text[len("discovery:"):].split("/", 2) if len(parts) != 3: return "not discovery://" run, agent, key = parts if run != execution_id: return f"from {run}, not this Discovery run {execution_id}" if agent not in keys: return f"{agent} is not a Discovery agent whose records Assessment cites ({', '.join(PART_OF)})" if key not in keys[agent]: return f"no record {key} in {agent}'s part ({PART_OF[agent]}) of the package" return None def resolve(payload: Dict[str, Any]) -> Dict[str, List[dict]]: """K-02 lookup details per application: empty when every disposition's citations resolve.""" execution_id = payload.get("discovery_execution_id") or "" pkg = package(payload.get("discovery_documents")) out: Dict[str, List[dict]] = {} for d in payload.get("dispositions") or []: app = d.get("application_id", "?") refs = [d.get("evidence_ref")] + list(d.get("evidence_refs") or []) if pkg is None: out[app] = [{"record": app, "expected": "one Discovery package among discovery_documents", "recorded": "none, or more than one", "reason": "the citations can't be looked up"}] continue keys = index(pkg) problems = [] for ref in dict.fromkeys(r for r in refs if r): why = unresolved(ref, execution_id, keys) if why: problems.append({"record": app, "expected": "a citation that resolves to a record in the package", "recorded": str(ref), "reason": why}) if problems: out[app] = problems return out