ALZ-F01 Discovery sub-agent pattern: report phase and fetch in code
Orchestrator report phase (ADR 0013) and fetching in code (ADR 0014), with the report renderer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,13 +16,21 @@ from typing import Any, Dict, Iterable, List, Optional, Set
|
||||
|
||||
from app import workspace
|
||||
|
||||
SCAN_LIKE = {"scan_target", "classify_data", "trace_dependencies", "collect_network"}
|
||||
SCAN_LIKE = {"scan_target"}
|
||||
INGEST = {"ingest_cmdb", "ingest_itsm"}
|
||||
AFTER_INGEST = {"collect_network", "trace_dependencies", "reconcile_cmdb"}
|
||||
|
||||
|
||||
def _result(stored: Any) -> Any:
|
||||
"""An MCP result's own data: results are stored as they came back, in the {"success", "data"} wrapper."""
|
||||
if isinstance(stored, dict) and "success" in stored and isinstance(stored.get("data"), dict):
|
||||
return stored["data"]
|
||||
return stored
|
||||
|
||||
|
||||
def scope_ids(scope: Any) -> List[str]:
|
||||
"""The scope_entry_ids in an alz-scope.get_scope result, whichever shape it came back in."""
|
||||
scope = _result(scope)
|
||||
if isinstance(scope, dict):
|
||||
for key in ("entries", "scope_entries", "scope", "items"):
|
||||
if isinstance(scope.get(key), list):
|
||||
@@ -33,7 +41,17 @@ def scope_ids(scope: Any) -> List[str]:
|
||||
return [str(e["scope_entry_id"]) for e in scope if isinstance(e, dict) and e.get("scope_entry_id")]
|
||||
|
||||
|
||||
def _plan_issues(plan: Dict[str, Any], scope: List[str], scan_rps: Optional[float]) -> List[str]:
|
||||
def scope_sites(scope: Any) -> List[str]:
|
||||
"""The sites in an alz-scope.get_scope result: its `sites`, or the entries' own when it lists none."""
|
||||
scope = _result(scope)
|
||||
if isinstance(scope, dict) and isinstance(scope.get("sites"), list):
|
||||
return [str(s) for s in scope["sites"]]
|
||||
entries = scope.get("entries") if isinstance(scope, dict) else scope
|
||||
return sorted({str(e["site"]) for e in entries or [] if isinstance(e, dict) and e.get("site")})
|
||||
|
||||
|
||||
def _plan_issues(plan: Dict[str, Any], scope: List[str], scan_rps: Optional[float],
|
||||
sites: Optional[List[str]] = None) -> List[str]:
|
||||
issues: List[str] = []
|
||||
tasks = plan.get("tasks") or []
|
||||
gaps = plan.get("coverage_gaps") or []
|
||||
@@ -44,6 +62,14 @@ def _plan_issues(plan: Dict[str, Any], scope: List[str], scan_rps: Optional[floa
|
||||
if t.get("type") in SCAN_LIKE and str(t.get("target_ref")) not in in_scope:
|
||||
issues.append(f"scope_lock: task {t.get('task_id')} targets {t.get('target_ref')!r}, not in scope")
|
||||
|
||||
# Network coverage: every site has a collect_network task, and no task names another site.
|
||||
if sites is not None:
|
||||
network = {str(t.get("target_ref")) for t in tasks if t.get("type") == "collect_network"}
|
||||
for site in sorted(network - set(sites)):
|
||||
issues.append(f"scope_lock: collect_network targets {site!r}, not a site in scope")
|
||||
for site in sorted(set(sites) - network):
|
||||
issues.append(f"network_coverage: no collect_network task for site {site!r}")
|
||||
|
||||
# Source sequencing: everything after ingest depends on every ingest task.
|
||||
ingest_ids = {t.get("task_id") for t in tasks if t.get("type") in INGEST}
|
||||
for t in tasks:
|
||||
@@ -78,6 +104,7 @@ def _plan_issues(plan: Dict[str, Any], scope: List[str], scan_rps: Optional[floa
|
||||
|
||||
|
||||
def scan_rps_budget(budgets: Any) -> Optional[float]:
|
||||
budgets = _result(budgets)
|
||||
if isinstance(budgets, dict):
|
||||
for key in ("scan_rps", "scan_rps_budget"):
|
||||
if isinstance(budgets.get(key), (int, float)):
|
||||
@@ -106,13 +133,14 @@ def check_plan(plan_ref: str = "plan/task-graph", scope_ref: str = "plan/scope",
|
||||
try:
|
||||
run = workspace.current_run()
|
||||
plan = workspace.get(run, plan_ref)["content"]
|
||||
scope = scope_ids(workspace.get(run, scope_ref)["content"])
|
||||
scope_record = workspace.get(run, scope_ref)["content"]
|
||||
scope = scope_ids(scope_record)
|
||||
budgets = workspace.get(run, budgets_ref)["content"]
|
||||
except workspace.WorkspaceError as exc:
|
||||
return {"error": str(exc)}
|
||||
if not isinstance(plan, dict):
|
||||
return {"error": f"{plan_ref} is not a task graph object"}
|
||||
issues = _plan_issues(plan, scope, scan_rps_budget(budgets))
|
||||
issues = _plan_issues(plan, scope, scan_rps_budget(budgets), scope_sites(scope_record))
|
||||
planned = {str(t.get("target_ref")) for t in plan.get("tasks") or [] if t.get("type") in SCAN_LIKE}
|
||||
return {"ok": not issues, "issues": issues[:50], "scope_entries": len(scope),
|
||||
"planned": len(planned), "gaps": len(plan.get("coverage_gaps") or [])}
|
||||
|
||||
Reference in New Issue
Block a user