Files
cp1-shakedown/shakedown_lib/gate.py
Jonathan Boniface 099f9a87e8 Runner: run a voyage remotely as the kab-automation identity
- shakedown_lib: runs F01 -> F02 -> F03 for one engagement on KAB with no
  personal token. Signs in as the kab-automation client (client credentials,
  provisioned by Stellwerk from crucible-platform-deploy) and renews the token
  itself.
- Each stage checks its workflow id against the registered sub-agent name,
  builds inputs from the case and upstream stages, starts the run, follows it
  and judges it with cp1-agents evaluate_case.py.
- Gates (plan 5b): in config/runner.yaml gate_projects only, continue on a
  critic pass with no failed step and a note saying the system decided.
  Never on a fail. Elsewhere, wait for a person and record who decided.
  gate_projects is empty for now.
- Ledger per voyage run, with --resume; nothing secret is written.
- Cymbal voyage ENG-CYMBAL-001; workflow registry; Dockerfile and an
  unapplied Job template; 15 tests against a fake KAB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 05:08:31 +00:00

98 lines
3.6 KiB
Python

"""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)