"""The gate rule for end-to-end use cases (plan ยง5b), decided in code from what the run recorded. At a review pause the system may continue only when, for the steps up to the pause: - no step reported `failed` or `error`, and - the stage's latest critic verdict is exactly `pass`. Anything else (a `fail`, an unparseable or missing verdict, a failed step) is never continued: the pause is left for a person and the use case stops for a fix. Whether the system may decide at all depends on the project (the runner's `gate_projects`), not on this module. """ from __future__ import annotations import json from dataclasses import dataclass from typing import Any, Dict, List, Optional FAILED = {"failed", "error"} def parsed(value: Any) -> Any: if isinstance(value, str) and value[:1] in "{[": try: return json.loads(value) except ValueError: return value return value def unwrap(value: Any) -> Any: """A step's own output, out of KAB's wrappers (`response`, `output`, `result`, `data`), JSON strings decoded.""" value = parsed(value) for _ in range(4): if isinstance(value, dict) and "phase" not in value and "manifest" not in value: inner = next((value[k] for k in ("response", "output", "result", "data") if k in value), None) if inner is None: break value = parsed(inner) else: break return value def step_outputs(steps: Any) -> Dict[str, Any]: """step id -> its output, from `/executions/{id}/steps` (a mapping by step id, or a list of step records).""" body = steps.get("steps", steps) if isinstance(steps, dict) else steps if isinstance(body, dict): return {str(k): unwrap(v) for k, v in body.items() if k != "execution_id"} out: Dict[str, Any] = {} for item in body or []: if isinstance(item, dict): key = item.get("step_id") or item.get("id") if key is not None: out[str(key)] = unwrap(item.get("output", item.get("result", item))) return out def template_order(status: dict) -> List[str]: return [str(s.get("id")) for s in ((status.get("template") or {}).get("steps") or [])] def _verdict(value: Any) -> Optional[str]: if isinstance(value, dict): value = value.get("verdict") return value if isinstance(value, str) else None @dataclass class Decision: proceed: bool reason: str verdict: Optional[str] verdict_step: Optional[str] def decide(status: dict, steps: Any, paused_step: Optional[str]) -> Decision: if str(status.get("status", "")).lower() in FAILED: return Decision(False, f"run status is {status.get('status')}", None, None) outputs = step_outputs(steps) order = template_order(status) or list(outputs) if paused_step in order: order = order[: order.index(paused_step) + 1] verdict, verdict_step = None, None for step_id in order: out = outputs.get(step_id) if not isinstance(out, dict): continue if str(out.get("status", "")).lower() in FAILED: return Decision(False, f"step {step_id} reported {out.get('status')}", verdict, verdict_step) found = _verdict(out.get("verdict")) if found is not None: verdict, verdict_step = found, step_id if verdict is None: return Decision(False, "no critic verdict before the review point", None, None) if verdict != "pass": return Decision(False, f"critic verdict is {verdict!r} (step {verdict_step})", verdict, verdict_step) return Decision(True, f"critic verdict pass (step {verdict_step}), no failed step", verdict, verdict_step)