"""The Assessment intake (F02 ADR 0008): the Discovery package is checked in code before any Assessment agent reads it. `p1` (Parse Assessment Files over the selected artifacts) hands over each artifact's text, joined by the platform's file separator. A JSON artifact arrives as its raw text, so the package is found by parsing each document and looking, at any depth (KAB may wrap a step's output, or hold it as a JSON string), for the object Discovery's package step writes: `package_version` and `manifest` (F01 ADR 0020). Checks, all in code: - exactly one package among the selected artifacts; - a `package_version` this intake reads; - every part the manifest lists is present, and its `sha256` over the part's canonical JSON matches; - the manifest says `complete`, and the critic's verdict is `pass`; - the manifest's execution id and engagement match the run's `discovery_execution_id` and `engagement_id`. Any failure refuses the intake, so the step fails and no Assessment agent runs on an unchecked package. No model is involved. """ from __future__ import annotations import hashlib import json from typing import Any, Dict, Iterator, List, Optional FILE_SEPARATOR = "\n\n---\n\n" # the platform's join between parsed files (data_file_processing.py) READS = ("1.0",) class IntakeRefused(RuntimeError): """The Discovery package failed a check: the step fails, and Assessment does not start.""" def __init__(self, problems: List[str]): super().__init__("The Discovery package was refused: " + "; ".join(problems)) self.problems = problems def digest(part: Any) -> str: """The same canonical form the package step hashes (F01 ADR 0020).""" text = json.dumps(part, sort_keys=True, separators=(",", ":"), ensure_ascii=False) return "sha256:" + hashlib.sha256(text.encode("utf-8")).hexdigest() 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 find(documents: Optional[str]) -> List[Dict[str, Any]]: found = [] for document in (documents or "").split(FILE_SEPARATOR): found += list(_packages(document.strip())) return found def check(payload: Dict[str, Any]) -> Dict[str, Any]: packages = find(payload.get("discovery_documents")) if len(packages) != 1: raise IntakeRefused([f"{len(packages)} Discovery packages among the selected artifacts; select exactly one " "(the Discovery run's package step output)"]) package = packages[0] manifest = package["manifest"] problems = [] if package.get("package_version") not in READS: problems.append(f"package version {package.get('package_version')} is not one this intake reads ({', '.join(READS)})") verified = [] for name, entry in (manifest.get("parts") or {}).items(): if name not in package: problems.append(f"part {name} is listed in the manifest but missing") elif digest(package[name]) != entry.get("sha256"): problems.append(f"part {name} does not match its sha256: it changed after the package was made") else: verified.append(name) if not manifest.get("complete"): problems.append("the package is not complete: " + ("; ".join(manifest.get("problems") or []) or "no reason given")) if manifest.get("critic_verdict") != "pass": problems.append(f"the Discovery critic's verdict is {manifest.get('critic_verdict') or 'absent'}, not pass") expected = payload.get("discovery_execution_id") if expected and manifest.get("execution_id") != expected: problems.append(f"the package is from {manifest.get('execution_id') or 'an unknown run'}, not {expected}") if payload.get("engagement_id") and manifest.get("engagement_id") != payload["engagement_id"]: problems.append(f"the package is for engagement {manifest.get('engagement_id')}, not {payload['engagement_id']}") if problems: raise IntakeRefused(problems) grouped = package.get("applications") or {} return { "engagement_id": payload.get("engagement_id"), "run_id": payload.get("run_id"), "verified": True, "package_version": package["package_version"], "discovery_execution_id": manifest.get("execution_id"), "discovery_run_id": manifest.get("run_id"), "critic_verdict": manifest["critic_verdict"], "provider": manifest.get("provider"), "parts_verified": verified, "absent_parts": manifest.get("absent_parts") or [], "source_gaps": manifest.get("source_gaps") or [], "agent_versions": manifest.get("agent_versions") or {}, "applications": grouped.get("applications") or [], "ungrouped_assets": grouped.get("ungrouped_assets") or [], "scorecard": manifest.get("scorecard") or {}, "note": (f"Discovery package {manifest.get('execution_id')} verified: {len(verified)} parts match their " f"sha256, the critic passed, {len(grouped.get('applications') or [])} applications."), }