F02: Assessment report and package (ADR 0014, 0015); register agents in KAB
Some checks failed
check agents / check (push) Failing after 14s
Some checks failed
check agents / check (push) Failing after 14s
- Report (cp1-assessment-report-agent 0.2.0, step s7): a model writes the Assessment report from facts worked out in code (register, gaps, scorecard, rule traces, checks, and what each application is from the Discovery package); code sets the verdict and renders the pages. The disposition_signoff review point moves from the critic to the report. - Package (cp1-assessment-package-agent 0.1.1, step s8): after sign-off, every step's output in one package, with a register, a gaps register, the sign-off record, lineage and a sha256 per part, in code. Page step s9. - scripts/kab_register_agents.py: registers deployed agents in KAB's agent catalog (an in-place workflow update doesn't). - Runs exec_1b1257380df7 (014) and exec_374d5d577077 (015) completed on crucible-kab-dev: critic pass, report figures all in its facts, package complete with every digest recomputing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
"""The model writes the Assessment report; code supplies its facts and renders its pages (F02 ADR 0015).
|
||||
|
||||
`pre_process` replaces the step outputs with the facts worked out in code (`app/facts.py`), so the model writes from
|
||||
the same register, gaps and scorecard the package holds. `post_process` sets the verdict and ids from those facts,
|
||||
never from the model, and renders the HTML in code: `report_html` (marked as a draft, shown at the
|
||||
`disposition_signoff` review point) and `final_html` (for the page step after approval).
|
||||
|
||||
The two hooks run in the same request, so the facts pass between them in a ContextVar.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import contextvars
|
||||
from typing import Any, Optional
|
||||
|
||||
from app import facts, markdown
|
||||
|
||||
DRAFT = "Draft for sign-off. Not for release until the Engagement Lead continues the run at disposition_signoff."
|
||||
_facts: contextvars.ContextVar[Optional[dict]] = contextvars.ContextVar("report_facts", default=None)
|
||||
|
||||
|
||||
def pre_process(payload: dict) -> Optional[Any]:
|
||||
found = facts.build(payload)
|
||||
_facts.set(found)
|
||||
return {"engagement_id": payload.get("engagement_id"), "run_id": payload.get("run_id"),
|
||||
"discovery_execution_id": payload.get("discovery_execution_id"), "facts": found}
|
||||
|
||||
|
||||
def post_process(result: dict) -> Optional[dict]:
|
||||
found = _facts.get()
|
||||
_facts.set(None)
|
||||
if found is None:
|
||||
return None
|
||||
text = result.get("report_markdown") or ""
|
||||
title = f"Assessment report: {found.get('engagement_id') or ''} {found.get('run_id') or ''}".strip()
|
||||
return {**result, "engagement_id": found.get("engagement_id"), "run_id": found.get("run_id"),
|
||||
"verdict": found.get("verdict") or "not supplied",
|
||||
"signoff_queue": found["critic"]["signoff_queue"],
|
||||
"report_html": markdown.page(text, title, DRAFT), "final_html": markdown.page(text, title)}
|
||||
@@ -0,0 +1,114 @@
|
||||
"""What each application is, from the Discovery package (F02 ADR 0015, addendum of 2026-11-08), worked out in code.
|
||||
|
||||
The Assessment steps' outputs name an application's assets by id only, so a report written from them can say what was
|
||||
decided but not what the application is. This reads the Discovery package the intake verified (`discovery_documents`)
|
||||
and gives, per application: how Discovery identified it, its owner and business service, each asset (name, type,
|
||||
environment, location, criticality, CMDB record, data class, support and cost facts), its data stores, and its
|
||||
dependencies (a `represents` edge, a CMDB record matched to its own asset, is not one: ADR 0012).
|
||||
|
||||
The package is found as the intake finds it (`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
|
||||
|
||||
FILE_SEPARATOR = "\n\n---\n\n" # the platform's join between parsed files, as in the intake
|
||||
|
||||
|
||||
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 _part(pkg: dict, part: str, field: str) -> List[dict]:
|
||||
return [x for x in ((pkg.get(part) or {}).get(field) or []) if isinstance(x, dict)]
|
||||
|
||||
|
||||
def _asset(record: dict, ci: dict, node: dict, store: dict) -> dict:
|
||||
attrs = record.get("provider_attributes") or {}
|
||||
facts = record.get("facts") or {}
|
||||
sku = attrs.get("sku") if isinstance(attrs.get("sku"), dict) else {}
|
||||
return {
|
||||
"canonical_id": record.get("canonical_id"), "name": record.get("hostname") or ci.get("name") or node.get("name"),
|
||||
"resource_type": attrs.get("type"), "asset_class": record.get("asset_class"),
|
||||
"environment": record.get("environment"), "location": attrs.get("location") or facts.get("site"),
|
||||
"criticality": facts.get("criticality"), "services": record.get("services") or [],
|
||||
"os": (record.get("os") or {}).get("family") or None,
|
||||
"size": attrs.get("machineSize") or attrs.get("vmSize") or sku.get("name"), "tier": sku.get("tier"),
|
||||
"state": attrs.get("state") or attrs.get("provisioningState"), "public_address": attrs.get("publicAddress"),
|
||||
"support_status": facts.get("support_status"), "end_of_support": facts.get("end_of_support"),
|
||||
"annual_cost": facts.get("annual_cost"), "compliance_scope": facts.get("compliance_scope"),
|
||||
"inventory_confidence": record.get("confidence"), "conflicts": len(record.get("conflicts") or []),
|
||||
"cmdb": {"ci_ref": ci.get("ci_ref"), "ci_class": ci.get("ci_class"), "lifecycle": ci.get("lifecycle"),
|
||||
"support_group": ci.get("support_group"), "owner": ci.get("owner")} if ci else None,
|
||||
"retire_constraint": node.get("retire_constraint"),
|
||||
"data_class": store.get("highest_class") if store else None,
|
||||
"data_class_reason": store.get("reason") if store else None,
|
||||
}
|
||||
|
||||
|
||||
def applications(documents: Optional[str]) -> Dict[str, Any]:
|
||||
"""Per application id, what Discovery recorded about it; `found` says whether the package was read."""
|
||||
pkg = package(documents)
|
||||
if pkg is None:
|
||||
return {"found": False, "note": "The Discovery package was not among discovery_documents, so the application's "
|
||||
"make-up isn't known to this report.", "applications": {}}
|
||||
records = {r.get("canonical_id"): r for r in _part(pkg, "inventory", "records")}
|
||||
cis = {c.get("matched_canonical_id"): c for c in _part(pkg, "cmdb", "cis") if c.get("matched_canonical_id")}
|
||||
nodes = {n.get("id"): n for n in _part(pkg, "dependencies", "nodes")}
|
||||
stores = {s.get("canonical_id"): s for s in _part(pkg, "classification", "stores")}
|
||||
edges = [e for e in _part(pkg, "dependencies", "edges") if e.get("type") != "represents"]
|
||||
queue = set((pkg.get("dependencies") or {}).get("validation_queue") or [])
|
||||
out: Dict[str, Any] = {}
|
||||
for app in _part(pkg, "applications", "applications"):
|
||||
members = list(app.get("members") or [])
|
||||
own = set(members)
|
||||
name = lambda ref: (nodes.get(ref) or {}).get("name") or (records.get(ref) or {}).get("hostname") or ref
|
||||
links = []
|
||||
for e in edges:
|
||||
src, dst = e.get("source"), e.get("target")
|
||||
if src not in own and dst not in own:
|
||||
continue
|
||||
links.append({"edge_id": e.get("edge_id"), "type": e.get("type"),
|
||||
"direction": "internal" if src in own and dst in own else "outbound" if src in own else "inbound",
|
||||
"from": name(src), "to": name(dst), "confidence": e.get("confidence"),
|
||||
"validation": "awaiting validation" if e.get("edge_id") in queue else e.get("validation")})
|
||||
out[app.get("application_id")] = {
|
||||
"application_id": app.get("application_id"), "name": app.get("name"),
|
||||
"identified_by": app.get("basis"), "business_service": app.get("business_service"),
|
||||
"owner": app.get("owner"), "owners_disagree": app.get("owners_disagree"),
|
||||
"review_required": app.get("review_required"), "review_reasons": app.get("review_reasons") or [],
|
||||
"assets": [_asset(records.get(m) or {"canonical_id": m}, cis.get(m) or {}, nodes.get(m) or {},
|
||||
stores.get(m) or {}) for m in members],
|
||||
"data_stores": [{"canonical_id": s.get("canonical_id"), "name": s.get("store_name"),
|
||||
"highest_class": s.get("highest_class"), "reason": s.get("reason")}
|
||||
for m, s in stores.items() if m in own],
|
||||
"dependencies": links,
|
||||
}
|
||||
return {"found": True, "discovery_run_id": pkg.get("run_id"), "applications": out}
|
||||
@@ -0,0 +1,83 @@
|
||||
"""The facts the report is written from (F02 ADR 0015), worked out in code from the Assessment step outputs.
|
||||
|
||||
The register, gaps and scorecard come from `app/package.py`, a copy of the package agent's own module (the two images
|
||||
are separate; tests/test_report.py keeps the copies identical), so the report and the package can't disagree. The model
|
||||
receives these facts instead of the raw step outputs and writes the report from them.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from app import discovery as discovery_records, package
|
||||
|
||||
TOP_FINDINGS = 10
|
||||
SHORT = 400 # characters kept of a long basis or description
|
||||
|
||||
|
||||
def _short(text: Any) -> Any:
|
||||
return text if not isinstance(text, str) or len(text) <= SHORT else text[:SHORT].rstrip() + " …"
|
||||
|
||||
|
||||
def build(payload: dict) -> Dict[str, Any]:
|
||||
parts = package.parts_from(payload)
|
||||
reg = package.register(parts)
|
||||
gap = package.gaps(parts)
|
||||
critic = parts["critic"] or {}
|
||||
security = parts["security"] or {}
|
||||
plan = parts["plan"] or {}
|
||||
discovery = parts["discovery"] or {}
|
||||
dispositions: List[dict] = []
|
||||
for d in (parts["dispositions"] or {}).get("dispositions") or []:
|
||||
tco = d.get("tco_model") or {}
|
||||
dispositions.append({
|
||||
"application_id": d.get("application_id"), "disposition": d.get("disposition"),
|
||||
"confidence": d.get("confidence"), "interim_rule": d.get("interim_rule"),
|
||||
"rule_trace": [{"category": t.get("category"), "matched": t.get("matched"), "basis": _short(t.get("basis"))}
|
||||
for t in d.get("rule_trace") or []],
|
||||
"confidence_workings": d.get("confidence_workings"),
|
||||
"tco": {k: tco.get(k) for k in ("currency", "current_annual", "target_annual", "annual_delta", "indicative")},
|
||||
"tco_line_items": tco.get("line_items") or [],
|
||||
"licensing_note": _short(d.get("licensing_note")), "scoring_note": _short(d.get("scoring_note")),
|
||||
})
|
||||
findings = sorted((f for f in security.get("findings") or [] if isinstance(f, dict)),
|
||||
key=lambda f: -(f.get("rank_score") or 0))
|
||||
controls: Dict[str, int] = {}
|
||||
for c in security.get("controls") or []:
|
||||
if isinstance(c, dict):
|
||||
controls[c.get("result") or "unknown"] = controls.get(c.get("result") or "unknown", 0) + 1
|
||||
return {
|
||||
"engagement_id": payload.get("engagement_id"), "run_id": payload.get("run_id"),
|
||||
"lineage": {"discovery_execution_id": payload.get("discovery_execution_id"),
|
||||
"discovery_run_id": discovery.get("discovery_run_id"),
|
||||
"discovery_package_verified": discovery.get("verified"),
|
||||
"discovery_approval_ref": payload.get("discovery_approval_ref"),
|
||||
"discovery_source_gaps": discovery.get("source_gaps") or [],
|
||||
"rate_card_id": plan.get("rate_card_id"),
|
||||
"critic_model": critic.get("critic_model"), "executor_models": critic.get("executor_models"),
|
||||
"agent_versions": payload.get("agent_versions") or {}},
|
||||
"verdict": critic.get("verdict"),
|
||||
"critic": {"summary": critic.get("summary"),
|
||||
"checks": [{"check_id": c.get("check_id"), "result": c.get("result"),
|
||||
"details": [_short(x.get("reason")) for x in c.get("details") or [] if isinstance(x, dict)]}
|
||||
for c in critic.get("checks") or [] if isinstance(c, dict)],
|
||||
"rejected_dispositions": critic.get("rejected_dispositions") or [],
|
||||
"signoff_queue": critic.get("signoff_queue") or [],
|
||||
"unverified_claims": critic.get("unverified_claims") or [],
|
||||
"checked_in_code": ["K-02 citation lookup", "K-04", "K-05", "K-06", "K-07", "K-11", "the verdict"]},
|
||||
# What each application is, from the Discovery package (ADR 0015 addendum): its assets, owner and links.
|
||||
"applications": discovery_records.applications(payload.get("discovery_documents")),
|
||||
"plan": {"preconditions": plan.get("preconditions"), "summary": plan.get("summary"),
|
||||
"coverage_gaps": plan.get("coverage_gaps") or [], "in_cloud_estate": plan.get("in_cloud_estate")},
|
||||
"register": reg["applications"],
|
||||
"dispositions": dispositions,
|
||||
"risk": {"high_risk_tail": (parts["risk"] or {}).get("high_risk_tail") or [],
|
||||
"note": _short((parts["risk"] or {}).get("findings_note"))},
|
||||
"security": {"status": security.get("status"), "posture_score": security.get("posture_score"),
|
||||
"findings_total": len(findings), "controls_by_result": controls,
|
||||
"top_findings": [{"finding_id": f.get("finding_id"), "application_id": f.get("application_id"),
|
||||
"control_id": f.get("control_id"), "rank_score": f.get("rank_score"),
|
||||
"description": _short(f.get("description"))} for f in findings[:TOP_FINDINGS]],
|
||||
"advisory": reg["security_note"]},
|
||||
"gaps": gap,
|
||||
"scorecard": package.scorecard(parts, reg, gap),
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
"""The report's HTML, rendered in code from the model's Markdown (F02 ADR 0015). Standard library only.
|
||||
|
||||
Only the Markdown the prompt asks for is understood: headings, paragraphs, bullet and numbered lists, tables,
|
||||
blockquotes, **bold**, *italic* and `code`. Every character of the model's text is HTML-escaped before any markup is
|
||||
added, so nothing in the report (or in the estate data it quotes) can inject HTML or script into the page.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
import re
|
||||
from typing import List
|
||||
|
||||
STYLE = """
|
||||
:root { --fg: #1f2328; --muted: #59636e; --line: #d1d9e0; --head: #f6f8fa; --bg: #ffffff; --warn: #9a6700; --warnbg: #fff8c5; }
|
||||
@media (prefers-color-scheme: dark) { :root { --fg: #e6edf3; --muted: #9198a1; --line: #3d444d; --head: #151b23; --bg: #0d1117; --warn: #d29922; --warnbg: #2e2a1f; } }
|
||||
body { background: var(--bg); color: var(--fg); font: 15px/1.55 -apple-system, "Segoe UI", Roboto, sans-serif; margin: 0; }
|
||||
main { max-width: 960px; margin: 0 auto; padding: 24px 16px 48px; }
|
||||
h1 { font-size: 1.7em; margin: 0 0 .4em; } h2 { font-size: 1.3em; margin: 1.6em 0 .5em; border-bottom: 1px solid var(--line); padding-bottom: .2em; }
|
||||
h3 { font-size: 1.1em; margin: 1.2em 0 .4em; } p, ul, ol, blockquote { margin: 0 0 .9em; }
|
||||
.table { overflow-x: auto; margin: 0 0 1em; } table { border-collapse: collapse; min-width: 100%; font-size: .93em; }
|
||||
th, td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; } th { background: var(--head); }
|
||||
code { background: var(--head); padding: 1px 4px; border-radius: 4px; font-size: .92em; }
|
||||
blockquote { border-left: 3px solid var(--line); padding: 0 12px; color: var(--muted); }
|
||||
.draft { background: var(--warnbg); color: var(--warn); border: 1px solid var(--warn); border-radius: 6px; padding: 8px 12px; margin-bottom: 18px; font-weight: 600; }
|
||||
"""
|
||||
|
||||
|
||||
def _inline(text: str) -> str:
|
||||
out = html.escape(text, quote=True)
|
||||
out = re.sub(r"`([^`]+)`", r"<code>\1</code>", out)
|
||||
out = re.sub(r"\*\*([^*]+)\*\*", r"<strong>\1</strong>", out)
|
||||
out = re.sub(r"(?<![\w*])\*([^*\s][^*]*)\*(?![\w*])", r"<em>\1</em>", out)
|
||||
return out
|
||||
|
||||
|
||||
def _cells(line: str) -> List[str]:
|
||||
return [c.strip() for c in line.strip().strip("|").split("|")]
|
||||
|
||||
|
||||
def _is_rule(line: str) -> bool:
|
||||
return bool(re.fullmatch(r"\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)*\|?", line.strip()))
|
||||
|
||||
|
||||
def to_html(markdown: str) -> str:
|
||||
lines = (markdown or "").replace("\r\n", "\n").split("\n")
|
||||
out: List[str] = []
|
||||
i = 0
|
||||
while i < len(lines):
|
||||
line = lines[i]
|
||||
if not line.strip():
|
||||
i += 1
|
||||
continue
|
||||
heading = re.match(r"(#{1,4})\s+(.*)", line)
|
||||
if heading:
|
||||
level = len(heading.group(1))
|
||||
out.append(f"<h{level}>{_inline(heading.group(2).strip())}</h{level}>")
|
||||
i += 1
|
||||
elif line.lstrip().startswith("|") and i + 1 < len(lines) and _is_rule(lines[i + 1]):
|
||||
head = "".join(f"<th>{_inline(c)}</th>" for c in _cells(line))
|
||||
i += 2
|
||||
rows = []
|
||||
while i < len(lines) and lines[i].lstrip().startswith("|"):
|
||||
rows.append("<tr>" + "".join(f"<td>{_inline(c)}</td>" for c in _cells(lines[i])) + "</tr>")
|
||||
i += 1
|
||||
out.append(f'<div class="table"><table><thead><tr>{head}</tr></thead><tbody>{"".join(rows)}</tbody></table></div>')
|
||||
elif re.match(r"\s*[-*]\s+", line) or re.match(r"\s*\d+[.)]\s+", line):
|
||||
ordered = bool(re.match(r"\s*\d+[.)]\s+", line))
|
||||
pattern = r"\s*\d+[.)]\s+(.*)" if ordered else r"\s*[-*]\s+(.*)"
|
||||
items = []
|
||||
while i < len(lines) and re.match(pattern, lines[i]):
|
||||
items.append(f"<li>{_inline(re.match(pattern, lines[i]).group(1))}</li>")
|
||||
i += 1
|
||||
tag = "ol" if ordered else "ul"
|
||||
out.append(f"<{tag}>{''.join(items)}</{tag}>")
|
||||
elif line.lstrip().startswith(">"):
|
||||
quote = []
|
||||
while i < len(lines) and lines[i].lstrip().startswith(">"):
|
||||
quote.append(lines[i].lstrip()[1:].strip())
|
||||
i += 1
|
||||
out.append(f"<blockquote><p>{_inline(' '.join(quote))}</p></blockquote>")
|
||||
else:
|
||||
para = []
|
||||
while i < len(lines) and lines[i].strip() and not re.match(r"(#{1,4})\s|\s*[-*]\s|\s*\d+[.)]\s|\s*[|>]", lines[i]):
|
||||
para.append(lines[i].strip())
|
||||
i += 1
|
||||
if not para: # a line no rule above took: keep it as text rather than loop on it
|
||||
para, i = [line.strip()], i + 1
|
||||
out.append(f"<p>{_inline(' '.join(para))}</p>")
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def page(markdown: str, title: str, banner: str = "") -> str:
|
||||
note = f'<div class="draft">{html.escape(banner)}</div>' if banner else ""
|
||||
return ("<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\">"
|
||||
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
|
||||
f"<title>{html.escape(title)}</title><style>{STYLE}</style></head>"
|
||||
f"<body><main>{note}{to_html(markdown)}</main></body></html>")
|
||||
@@ -0,0 +1,304 @@
|
||||
"""The Assessment package (F02 ADR 0014): every Assessment step's output in one document, for Strategy and the later
|
||||
Features, with a manifest they can check in code.
|
||||
|
||||
The step runs only after the Engagement Lead continued the run at `disposition_signoff`, so a package exists only for
|
||||
an assessment a person let through, and continuing signs off the queue as it stands. Parts are named by meaning, not
|
||||
step id (KAB renames step ids on import), and copied unchanged so their evidence references still resolve. Two parts
|
||||
are worked out here, never by a model: `register` (one row per application) and `gaps` (what Discovery data would
|
||||
raise each application's confidence). Each part's `sha256` is over its canonical JSON, exactly as Discovery's package
|
||||
step computes it (`cp1-discovery-package-agent/app/package.py`). Standard library only.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
PACKAGE_VERSION = "1.0"
|
||||
SIGNOFF_BELOW = 0.80 # ALZ-ASM-04: signoff_required = confidence < 0.80
|
||||
|
||||
# The step's input name -> (part, the producing agent's own field name). scripts/build_workflow.py wires each input
|
||||
# from the step that produces it (PACKAGE_INPUTS there); tests/test_package.py checks the two agree.
|
||||
INPUTS = {
|
||||
# s0, the intake: the Discovery package as it verified it.
|
||||
"intake_verified": ("discovery", "verified"), "discovery_run_id": ("discovery", "discovery_run_id"),
|
||||
"discovery_package_version": ("discovery", "package_version"),
|
||||
"discovery_critic_verdict": ("discovery", "critic_verdict"), "discovery_provider": ("discovery", "provider"),
|
||||
"discovery_parts_verified": ("discovery", "parts_verified"), "discovery_absent_parts": ("discovery", "absent_parts"),
|
||||
"discovery_source_gaps": ("discovery", "source_gaps"), "discovery_agent_versions": ("discovery", "agent_versions"),
|
||||
# s1, the planner.
|
||||
"plan_status": ("plan", "status"), "preconditions": ("plan", "preconditions"), "rate_card_id": ("plan", "rate_card_id"),
|
||||
"in_cloud_estate": ("plan", "in_cloud_estate"), "application_source": ("plan", "application_source"),
|
||||
"applications": ("plan", "applications"), "tasks": ("plan", "tasks"), "coverage_gaps": ("plan", "coverage_gaps"),
|
||||
"plan_summary": ("plan", "summary"), "plan_note": ("plan", "findings_note"),
|
||||
# s2, the readiness profiler.
|
||||
"profiles": ("profiles", "profiles"), "profile_gaps": ("profiles", "gaps"), "profile_note": ("profiles", "findings_note"),
|
||||
# s3, the risk scorer.
|
||||
"scores": ("risk", "scores"), "high_risk_tail": ("risk", "high_risk_tail"), "risk_note": ("risk", "findings_note"),
|
||||
# s4, security posture (always advisory).
|
||||
"posture_status": ("security", "status"), "security_provider": ("security", "provider"),
|
||||
"posture_score": ("security", "posture_score"), "controls": ("security", "controls"),
|
||||
"security_findings": ("security", "findings"), "pillars": ("security", "pillars"),
|
||||
"injection_flags": ("security", "injection_flags"), "security_note": ("security", "findings_note"),
|
||||
# s5, dispositions and TCO.
|
||||
"dispositions": ("dispositions", "dispositions"), "producer_signoff_queue": ("dispositions", "signoff_queue"),
|
||||
"scoring_note": ("dispositions", "scoring_note"),
|
||||
# s6, the critic.
|
||||
"verdict": ("critic", "verdict"), "checks": ("critic", "checks"),
|
||||
"rejected_dispositions": ("critic", "rejected_dispositions"), "signoff_queue": ("critic", "signoff_queue"),
|
||||
"unverified_claims": ("critic", "unverified_claims"), "critic_model": ("critic", "critic_model"),
|
||||
"executor_models": ("critic", "executor_models"), "critic_summary": ("critic", "summary"),
|
||||
# s7, the report the Engagement Lead signed off from (its Markdown; the HTML is the page step's).
|
||||
"report_markdown": ("report", "report_markdown"),
|
||||
}
|
||||
COPIED = ("discovery", "plan", "profiles", "risk", "security", "dispositions", "critic", "report")
|
||||
PARTS = COPIED + ("register", "gaps")
|
||||
REQUIRED = COPIED # security is required too: its `status` says `not_applicable` when there is no cloud estate
|
||||
COUNTED = {"plan": "applications", "profiles": "profiles", "risk": "scores", "security": "findings",
|
||||
"dispositions": "dispositions", "critic": "checks", "register": "applications", "gaps": "items"}
|
||||
|
||||
# Where the data that would fill a gap comes from: the Discovery agent and field, in the producers' own terms.
|
||||
RISK_FACTOR_SOURCE = {
|
||||
"eol": "support_status or end_of_support on the application's inventory records (ALZ-DSC-03)",
|
||||
"fan_in": "a dependency map covering the application's assets (ALZ-DSC-06)",
|
||||
"data": "a classification of every store above 'unclassified' (ALZ-DSC-05, needs schema access), or "
|
||||
"compliance_scope on the assets (ALZ-DSC-03)",
|
||||
"change": "change records for the application's CIs from the ITSM (ALZ-DSC-04 itsm_signals)",
|
||||
}
|
||||
SIGNAL_SOURCE = {
|
||||
"platform_support": "support_status on the inventory records (ALZ-DSC-03)",
|
||||
"hardware_coupling": "the asset class of each inventory record (ALZ-DSC-03)",
|
||||
"data_constraints": "store classification (ALZ-DSC-05) or compliance_scope (ALZ-DSC-03)",
|
||||
"dependency_exposure": "the dependency map (ALZ-DSC-06)",
|
||||
"hosting_portability": "hosting on the inventory records (ALZ-DSC-03)",
|
||||
"sizing": "runtime telemetry: none is collected by Discovery today",
|
||||
"performance_baseline": "runtime telemetry: none is collected by Discovery today",
|
||||
}
|
||||
CONFIDENCE_SOURCE = {
|
||||
"P1": "runtime telemetry for sizing: none is collected by Discovery today",
|
||||
"P2": "the facts or signals named in the basis",
|
||||
"P3": "the risk factors missing from the score (see the risk_factor items)",
|
||||
"P4": "a like-for-like replacement for the appliance on the rate card",
|
||||
"P5": "stronger inventory records (confidence of 0.80 or more, no conflicts)",
|
||||
"P6": "evidence that the application is no longer used",
|
||||
}
|
||||
|
||||
|
||||
def digest(part: Any) -> str:
|
||||
text = json.dumps(part, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
|
||||
return "sha256:" + hashlib.sha256(text.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _decode(value: Any) -> Any:
|
||||
"""A step output KAB delivered as a JSON string, decoded; anything else as it came."""
|
||||
for _ in range(3):
|
||||
if not isinstance(value, str):
|
||||
break
|
||||
try:
|
||||
value = json.loads(value)
|
||||
except ValueError:
|
||||
break
|
||||
return value
|
||||
|
||||
|
||||
def parts_from(payload: dict) -> Dict[str, Optional[dict]]:
|
||||
"""The step's inputs, grouped into named parts. A part none of whose inputs arrived is absent (None)."""
|
||||
parts: Dict[str, dict] = {}
|
||||
for name, (part, field) in INPUTS.items():
|
||||
if payload.get(name) is not None:
|
||||
parts.setdefault(part, {})[field] = _decode(payload[name])
|
||||
return {part: parts.get(part) for part in COPIED}
|
||||
|
||||
|
||||
def _by_app(items: Any) -> Dict[str, dict]:
|
||||
return {i["application_id"]: i for i in items or [] if isinstance(i, dict) and i.get("application_id")}
|
||||
|
||||
|
||||
def _rate_card(document: Any) -> dict:
|
||||
"""The rate card's id and sha256 (canonical JSON when it parses, else the text as given)."""
|
||||
if not document:
|
||||
return {"rate_card_id": None, "sha256": None}
|
||||
parsed = _decode(document)
|
||||
if isinstance(parsed, (dict, list)):
|
||||
return {"rate_card_id": parsed.get("rate_card_id") if isinstance(parsed, dict) else None, "sha256": digest(parsed)}
|
||||
return {"rate_card_id": None, "sha256": "sha256:" + hashlib.sha256(str(document).encode("utf-8")).hexdigest()}
|
||||
|
||||
|
||||
def signoff_status(app: str, verdict: Optional[str], disposed: set, rejected: set, queue: set) -> str:
|
||||
"""Each application's sign-off, as continuing at disposition_signoff left it (ADR 0014)."""
|
||||
if verdict != "pass":
|
||||
return "not_signed_off" # a fail verdict is never approved; the package says so and is incomplete
|
||||
if app in rejected:
|
||||
return "rejected"
|
||||
if app not in disposed:
|
||||
return "not_disposed"
|
||||
return "signed_off" if app in queue else "not_required"
|
||||
|
||||
|
||||
def register(parts: Dict[str, Optional[dict]]) -> dict:
|
||||
"""One row per application: what Strategy, the Business Case, Target Architecture and Wave Planning read."""
|
||||
plan, critic = parts["plan"] or {}, parts["critic"] or {}
|
||||
profiles = _by_app((parts["profiles"] or {}).get("profiles"))
|
||||
scores = _by_app((parts["risk"] or {}).get("scores"))
|
||||
dispositions = _by_app((parts["dispositions"] or {}).get("dispositions"))
|
||||
security = parts["security"] or {}
|
||||
findings: Dict[str, List[dict]] = {}
|
||||
for f in security.get("findings") or []:
|
||||
if isinstance(f, dict):
|
||||
findings.setdefault(f.get("application_id") or "", []).append(f)
|
||||
rejected: Dict[str, List[str]] = {}
|
||||
for r in critic.get("rejected_dispositions") or []:
|
||||
if isinstance(r, dict):
|
||||
rejected.setdefault(r.get("application_id"), []).append(r.get("check_id"))
|
||||
verdict, queue = critic.get("verdict"), set(critic.get("signoff_queue") or [])
|
||||
gaps = {g.get("application_id"): g.get("reason") for g in plan.get("coverage_gaps") or [] if isinstance(g, dict)}
|
||||
rows = []
|
||||
for a in plan.get("applications") or []:
|
||||
app = a.get("application_id")
|
||||
d, s, p = dispositions.get(app) or {}, scores.get(app) or {}, profiles.get(app) or {}
|
||||
tco = d.get("tco_model") or {}
|
||||
own = findings.get(app, [])
|
||||
rows.append({
|
||||
"application_id": app, "name": a.get("name"), "assets": len(a.get("assets") or []),
|
||||
"coverage_gap": gaps.get(app),
|
||||
"disposition": d.get("disposition"), "confidence": d.get("confidence"),
|
||||
"interim_rule": d.get("interim_rule"), "signoff_required": d.get("signoff_required"),
|
||||
"signoff": signoff_status(app, verdict, set(dispositions), set(rejected), queue),
|
||||
"rejected_by": rejected.get(app, []),
|
||||
"matched_rule": next((t.get("category") for t in d.get("rule_trace") or [] if t.get("matched")), None),
|
||||
"risk_score": s.get("score"), "risk_partial": s.get("partial"),
|
||||
"risk_missing_factors": s.get("missing_factors") or [], "high_risk": s.get("high_risk"),
|
||||
"telemetry": p.get("telemetry"),
|
||||
"signals_unavailable": [x.get("signal") for x in p.get("signals") or [] if x.get("available") is False],
|
||||
"depends_on": len(p.get("depends_on") or []), "dependents": len(p.get("dependents") or []),
|
||||
"currency": tco.get("currency"), "current_annual": tco.get("current_annual"),
|
||||
"target_annual": tco.get("target_annual"), "annual_delta": tco.get("annual_delta"),
|
||||
"tco_indicative": tco.get("indicative"), "tco_missing_inputs": len(tco.get("missing_inputs") or []),
|
||||
"security_findings": len(own),
|
||||
"top_finding": max(own, key=lambda f: f.get("rank_score") or 0).get("description") if own else None,
|
||||
"evidence_ref": d.get("evidence_ref"),
|
||||
})
|
||||
return {"applications": rows, "posture_status": security.get("status"), "posture_score": security.get("posture_score"),
|
||||
"security_note": "Security findings are advisory (ALZ-ASM-05): they changed no disposition or confidence."}
|
||||
|
||||
|
||||
def gaps(parts: Dict[str, Optional[dict]]) -> dict:
|
||||
"""What Assessment couldn't establish, per application, with where the missing data would come from."""
|
||||
items: List[dict] = []
|
||||
plan = parts["plan"] or {}
|
||||
for g in plan.get("coverage_gaps") or []:
|
||||
if isinstance(g, dict):
|
||||
items.append({"application_id": g.get("application_id"), "kind": "coverage", "item": "not assessed",
|
||||
"effect": "no disposition", "reason": g.get("reason"), "fill_from": None})
|
||||
for s in (parts["risk"] or {}).get("scores") or []:
|
||||
for factor in s.get("missing_factors") or []:
|
||||
items.append({"application_id": s.get("application_id"), "kind": "risk_factor", "item": factor,
|
||||
"effect": "risk score is partial", "reason": next(
|
||||
(f.get("basis") for f in s.get("factors") or [] if f.get("factor") == factor), None),
|
||||
"fill_from": RISK_FACTOR_SOURCE.get(factor)})
|
||||
for p in (parts["profiles"] or {}).get("profiles") or []:
|
||||
for x in p.get("signals") or []:
|
||||
if x.get("available") is False:
|
||||
items.append({"application_id": p.get("application_id"), "kind": "readiness_signal",
|
||||
"item": x.get("signal"), "effect": "signal unknown", "reason": x.get("basis"),
|
||||
"fill_from": SIGNAL_SOURCE.get(x.get("signal"))})
|
||||
for g in (parts["profiles"] or {}).get("gaps") or []:
|
||||
if isinstance(g, dict):
|
||||
items.append({"application_id": g.get("application_id"), "kind": "profile", "item": "no profile",
|
||||
"effect": "readiness unknown", "reason": g.get("reason"), "fill_from": None})
|
||||
for d in (parts["dispositions"] or {}).get("dispositions") or []:
|
||||
app = d.get("application_id")
|
||||
for adj in (d.get("confidence_workings") or {}).get("adjustments") or []:
|
||||
rule = str(adj.get("rule") or "")
|
||||
items.append({"application_id": app, "kind": "confidence", "item": rule,
|
||||
"effect": f"confidence {adj.get('delta'):+.2f}" if isinstance(adj.get("delta"), (int, float))
|
||||
else "confidence lowered", "reason": adj.get("basis"),
|
||||
"fill_from": CONFIDENCE_SOURCE.get(rule.split(" ")[0])})
|
||||
missing = (d.get("tco_model") or {}).get("missing_inputs") or []
|
||||
if missing:
|
||||
fields = sorted({m.split(" ", 1)[1] if " " in m else m for m in missing})
|
||||
items.append({"application_id": app, "kind": "tco_input", "item": ", ".join(fields),
|
||||
"effect": f"{len(missing)} TCO inputs missing; the model is indicative",
|
||||
"reason": "; ".join(missing[:5]) + (" …" if len(missing) > 5 else ""),
|
||||
"fill_from": "annual_cost and a cost breakdown on the inventory records (a billing or "
|
||||
"cost-management source for Discovery)"})
|
||||
source_gaps = (parts["discovery"] or {}).get("source_gaps") or []
|
||||
return {"items": items, "discovery_source_gaps": source_gaps,
|
||||
"note": "Each item is something Assessment had to do without. Filling it in Discovery and running both "
|
||||
"workflows again is how confidence rises; nothing here is a defect of the run."}
|
||||
|
||||
|
||||
def scorecard(parts: Dict[str, Optional[dict]], reg: dict, gap: dict) -> dict:
|
||||
rows = reg["applications"]
|
||||
disposed = [r for r in rows if r["disposition"]]
|
||||
mix: Dict[str, int] = {}
|
||||
for r in disposed:
|
||||
mix[r["disposition"]] = mix.get(r["disposition"], 0) + 1
|
||||
kinds: Dict[str, int] = {}
|
||||
for i in gap["items"]:
|
||||
kinds[i["kind"]] = kinds.get(i["kind"], 0) + 1
|
||||
return {
|
||||
"version": "1",
|
||||
"applications": len(rows), "disposed": len(disposed), "dispositions": mix,
|
||||
"below_threshold": sum(1 for r in disposed if isinstance(r["confidence"], (int, float))
|
||||
and r["confidence"] < SIGNOFF_BELOW),
|
||||
"signed_off": sum(1 for r in rows if r["signoff"] == "signed_off"),
|
||||
"rejected": sum(1 for r in rows if r["signoff"] == "rejected"),
|
||||
"risk_partial": sum(1 for r in rows if r["risk_partial"]), "high_risk": sum(1 for r in rows if r["high_risk"]),
|
||||
"tco_with_target": sum(1 for r in disposed if r["target_annual"] is not None),
|
||||
"tco_with_current": sum(1 for r in disposed if r["current_annual"] is not None),
|
||||
"security_findings": len((parts["security"] or {}).get("findings") or []),
|
||||
"gaps": kinds,
|
||||
}
|
||||
|
||||
|
||||
def build(payload: dict, execution_id: str = "", now: Optional[datetime] = None) -> dict:
|
||||
parts: Dict[str, Any] = parts_from(payload)
|
||||
reg = register(parts)
|
||||
gap = gaps(parts)
|
||||
parts["register"], parts["gaps"] = reg, gap
|
||||
critic = parts["critic"] or {}
|
||||
verdict = critic.get("verdict")
|
||||
problems = [f"part missing: {p}" for p in REQUIRED if parts[p] is None]
|
||||
if verdict != "pass":
|
||||
problems.append(f"critic verdict is {verdict or 'absent'}, not pass")
|
||||
if (parts["discovery"] or {}).get("verified") is not True:
|
||||
problems.append("the intake did not verify the Discovery package")
|
||||
signed = [r["application_id"] for r in reg["applications"] if r["signoff"] == "signed_off"]
|
||||
manifest = {
|
||||
"package_version": PACKAGE_VERSION,
|
||||
"engagement_id": payload.get("engagement_id"), "run_id": payload.get("run_id"),
|
||||
"execution_id": execution_id or None,
|
||||
"created_at": (now or datetime.now(timezone.utc)).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"critic_verdict": verdict,
|
||||
"discovery": {"execution_id": payload.get("discovery_execution_id"),
|
||||
"run_id": (parts["discovery"] or {}).get("discovery_run_id"),
|
||||
"artifact_ids": _decode(payload.get("artifact_ids")) or [],
|
||||
"package_version": (parts["discovery"] or {}).get("package_version"),
|
||||
"verified": (parts["discovery"] or {}).get("verified"),
|
||||
"approval_ref": payload.get("discovery_approval_ref")},
|
||||
"rate_card": {**_rate_card(payload.get("rate_card_document")),
|
||||
**({"rate_card_id": (parts["plan"] or {}).get("rate_card_id")}
|
||||
if (parts["plan"] or {}).get("rate_card_id") else {})},
|
||||
"models": {"critic_model": critic.get("critic_model"), "executor_models": critic.get("executor_models")},
|
||||
"agent_versions": payload.get("agent_versions") or {},
|
||||
"signoff": {
|
||||
"gate": "disposition_signoff",
|
||||
"decision": "continued" if verdict == "pass" else "continued with a fail verdict: not signed off",
|
||||
"signed_off": signed if verdict == "pass" else [],
|
||||
"basis": "This step runs only after the Engagement Lead continued the run at disposition_signoff. "
|
||||
"Continuing signs off the sign-off queue as it stands (ADR 0014); the next workflow records "
|
||||
"that approval as its run input assessment_approval_ref.",
|
||||
},
|
||||
"parts": {p: {"sha256": digest(parts[p]),
|
||||
**({"items": len((parts[p] or {}).get(COUNTED[p]) or [])} if p in COUNTED else {})}
|
||||
for p in PARTS if parts[p] is not None},
|
||||
"absent_parts": [p for p in PARTS if parts[p] is None],
|
||||
"complete": not problems,
|
||||
"problems": problems,
|
||||
"scorecard": scorecard(parts, reg, gap),
|
||||
}
|
||||
return {"engagement_id": payload.get("engagement_id"), "run_id": payload.get("run_id"),
|
||||
"package_version": PACKAGE_VERSION, "manifest": manifest,
|
||||
**{p: parts[p] for p in PARTS if parts[p] is not None}}
|
||||
Reference in New Issue
Block a user