Files
Jonathan Boniface 3b3ca64de0
Some checks failed
check agents / check (push) Failing after 14s
F02: K-02 lookup in code (ADR 0013), represents rule in the profiler; first passing run
- Critic 0.3.0: the hooks look up every disposition citation in the verified
  Discovery package (app/evidence.py). An unresolved citation fails K-02 and
  rejects the disposition; whether the records support the basis stays the
  model's judgement.
- Critic 0.3.1: K-02 judges a disposition's citations together (evidence_ref
  is the primary record), ADR 0013 addendum.
- Profiler 0.1.1: represents edges are not dependencies (ADR 0012, now covering
  ALZ-ASM-02).
- Scaffold: builds every prompt before deleting anything.
- Tests on Discovery run exec_58ba6a71d00b's package and runs 007 and 009 (15).
- Run exec_49e355141382 (RUN-ASM-AZURE-012) passed on crucible-kab-dev.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 15:59:01 +00:00

109 lines
4.9 KiB
Python

"""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:<discovery_execution_id>/<agent_id>/<record_key>`. 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:<execution_id>/<agent_id>/<record_key>"
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