"""App logic with no gradio / spaces / transformers imports, so it is unit-testable. Streams are lists/generators of plain-dict events. Nothing hidden (fault plan, database rows, committed flags) appears in any event before the final `end` event; the reveal is built from the scored record afterwards. """ from __future__ import annotations import copy import datetime as dt import html import json import os import re import time import uuid from pathlib import Path from typing import Any, Callable, Iterator from actionproof_min.agents import naive, oracle from actionproof_min.env import STEP_READ, STEP_TOOL, WRITE_TOOLS from actionproof_min.env_s3 import make_env_s3 from actionproof_min.episode import MAX_NEW_TOKENS, MAX_TURNS, stream_episode from actionproof_min.gate_env import SPACE_RULES, gate_env, gate_record from actionproof_min.runtime import episode_record_proc from actionproof_min.tasks import ProcTask, load_tasks REPLAY_URL = "https://huggingface.co/spaces/atharvgaur18/actionproof" BASE_REPO = "CohereLabs/tiny-aya-global" TRAINED_REPO = "atharvgaur18/actionproof-agent-d" MODELS = {"base": BASE_REPO, "trained": TRAINED_REPO} SESSION_CAP = 10 DATA = Path(__file__).resolve().parent / "data" / "tasks.json" RECORDED = Path(__file__).resolve().parent / "data" / "recorded.json" TRAINED_REVISION = "5418df563fc3" # atharvgaur18/actionproof-agent-d on the Hub (release/ARTIFACT_MANIFEST.json) # Fixed, deliberately not in the order a reader would guess; the key is shown only after a run. REALITIES = { "Reality A": {"kind": "lost_ack", "name": "lost acknowledgement", "what": "The payment was committed, but the reply was a timeout."}, "Reality B": {"kind": None, "name": "committed", "what": "The payment was committed and the reply said ok."}, "Reality C": {"kind": "phantom", "name": "phantom", "what": "The reply said ok, but nothing was committed."}, "Reality D": {"kind": "partial", "name": "partial (ratio 0.5)", "what": "The reply said ok, but only half the amount was committed."}, } SCRIPTED = {"scripted_naive": ("SCRIPTED: acknowledgement-trusting", naive), "scripted_naive_gate": ("SCRIPTED: acknowledgement-trusting, behind the RECOVERY GATE", naive), "scripted_oracle": ("SCRIPTED: transaction-aware oracle", oracle)} RECORDED_KEYS = {"recorded_base": "base", "recorded_trained": "trained"} # The Recovery Gate (RG1; actionproof_min/gate.py = actionproof/recovery_gate/gate.py). Gated live agents use the # model of MODEL_OF; gated agents of either kind are listed in GATED. MODEL_OF = {"trained_gate": "trained"} GATED = {"scripted_naive_gate", "trained_gate"} GATE_TAGS = {"gate_read": "read the record before a repeated write", "gate_verify": "read the record before accepting a completed report", "gate_escalate": "handed the purchase order to a human", "gate_finalize": "finished the episode after verifying every step"} # The base model is gated: without an HF_TOKEN secret its live runs always fall back to the recording, so say so. _BASE_LIVE = "live" if os.environ.get("HF_TOKEN") else "live if the Space has a token; else the labelled recording" AGENTS = {"judge": "Judge mode: acknowledgement-trusting script vs trained agent (live)", "base": f"Base model: tiny-aya-global ({_BASE_LIVE})", "trained": "Trained agent (live)", "gate_demo": "Gate demo: the acknowledgement-trusting script without and with the Recovery Gate (no GPU)", "trained_gate": "Trained agent behind the Recovery Gate (live)", "gate_compare": "Trained agent alone vs behind the Recovery Gate (2 live runs)", "both": f"Compare: base and trained ({_BASE_LIVE})", **{k: v[0] for k, v in SCRIPTED.items()}, "recorded_trained": "RECORDED: trained agent (no GPU)", "recorded_base": "RECORDED: base model (no GPU)"} SIDES = {"judge": ["scripted_naive", "trained"], "both": ["base", "trained"], "gate_demo": ["scripted_naive", "scripted_naive_gate"], "gate_compare": ["trained", "trained_gate"]} def load_recorded() -> dict: """Real model runs recorded with space_live/record_runs.py (meta + runs keyed model|task_id|reality).""" if not RECORDED.exists(): return {"meta": None, "runs": {}} return json.loads(RECORDED.read_text()) def has_recording(model_key: str, task_id: str, reality: str) -> bool: return f"{model_key}|{task_id}|{reality}" in load_recorded()["runs"] def recorded_events(model_key: str, task_id: str, reality: str) -> Iterator[dict]: yield from copy.deepcopy(load_recorded()["runs"][f"{model_key}|{task_id}|{reality}"]) # ------------------------------------------------------------------ tasks and realities def load_curated() -> list[ProcTask]: return load_tasks(DATA) def task_choices() -> dict[str, str]: """Label -> task_id. The label is the task's own request text, which is agent-visible.""" out = {} for t in load_curated(): out[f"{t.task_id.replace('p1_test_full-', 'Task ')} · {t.workflow.replace('_', ' ')}"] = t.task_id return out def get_task(task_id: str) -> ProcTask: return next(t for t in load_curated() if t.task_id == task_id) def apply_reality(task: ProcTask, reality: str) -> ProcTask: """Same request and world; `meta.faults` replaced by the reality's fault on the pay step's first call.""" spec = REALITIES[reality] t = copy.deepcopy(task) f: list[dict[str, Any]] = [] if spec["kind"] is not None: f = [{"tool": STEP_TOOL["pay"], "k": 0, "kind": spec["kind"]}] if spec["kind"] == "partial": f[0]["ratio"] = 0.5 t.meta["faults"], t.meta["never_commit"] = f, None t.meta["fault_class"] = spec["kind"] or "clean" return t # ------------------------------------------------------------------ event sources def utc_now() -> str: return dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%d %H:%M:%SZ") def scripted_events(policy_key: str, task: ProcTask, reality: str) -> Iterator[dict]: """Run a scripted policy to completion, then replay its agent-visible calls as events.""" label, policy = SCRIPTED[policy_key] t = apply_reality(task, reality) env = make_env_s3(t, f"live-{policy_key}-{task.task_id}") yield {"type": "start", "system": None, "prompt": t.prompt} if policy_key in GATED: # the policy's calls as it saw them (blocked ones included), with the gate's own calls after each evs: list[dict] = [] gate_env(env, t, SPACE_RULES, evs) gated = env.call def logged(tool: str, args: dict | None = None, by: str = "agent") -> dict: pos = len(evs) obs = gated(tool, args, by) evs.insert(pos, {"type": "call", "tool": tool, "args": args, "reply": obs}) return obs env.call = logged policy(env, t) env.gate_finalize() n = 0 for ev in evs: n += ev["type"] == "call" yield {**ev, "turn": max(n, 1)} rec = episode_record_proc(t, env) rec["gate"] = gate_record(env) yield {"type": "end", "ended_by": "report", "rec": rec, "turns": n, "invalid": 0, "n0": 0} return policy(env, t) for s in env.trace: if s.get("by", "agent") == "agent": yield {"type": "call", "turn": s["idx"] + 1, "tool": s["tool"], "args": s["args"], "reply": s["obs"]} rec = episode_record_proc(t, env) yield {"type": "end", "ended_by": "report", "rec": rec, "turns": len(env.trace), "invalid": 0, "n0": 0} def model_events(generate: Callable[[list[dict]], str], task: ProcTask, reality: str, system_fn: Callable | None = None, gate: bool = False) -> Iterator[dict]: """The p1_eval episode loop on a reality of the task. `generate` is injectable. `gate` puts the Recovery Gate in front of the tools (SPACE_RULES).""" t = apply_reality(task, reality) kw: dict[str, Any] = {"system_fn": system_fn} if system_fn else {} if gate: kw["gate_rules"] = SPACE_RULES yield from stream_episode(t, generate, f"live-{task.task_id}", **kw) # ------------------------------------------------------------------ rendering (agent-visible only until `end`) _ACK = re.compile(r'("status":\s*"ok")') def _esc(s: Any) -> str: return html.escape(str(s)) def render_reply(obs: dict) -> str: out = html.escape(json.dumps(obs), quote=False) return out.replace('"status": "ok"', '"status": "ok"') GATE_ERRORS = {"blocked_by_gate": "blocked, nothing was sent", "not_sent_by_gate": "not sent; the gate read the record instead", "report_rejected_by_gate": "report not accepted"} def render_event(ev: dict) -> str: t = ev["type"] if t == "call": args = json.dumps(ev["args"]) if ev["args"] is not None else "(invalid JSON)" is_report = ev["tool"] == "submit_report" gerr = GATE_ERRORS.get((ev["reply"] or {}).get("error")) cls = "step report" if is_report else "step" cls += " blocked" if gerr else "" head = "Agent report" if is_report else f"Turn {ev['turn']}" note = (f'
RECOVERY GATE · {_esc(gerr)}: {_esc(ev["reply"].get("detail", ""))}
' if gerr else "") return (f'
{head}
' f'
call {_esc(ev["tool"])} ' f'{html.escape(args, quote=False)}
{note}' f'
reply {render_reply(ev["reply"])}
') if t == "gate_call": args = json.dumps(ev["args"]) return (f'
Recovery Gate: {_esc(GATE_TAGS.get(ev["tag"], ev["tag"]))}
' f'
call {_esc(ev["tool"])} ' f'{html.escape(args, quote=False)}
' f'
reply {render_reply(ev["reply"])}
') if t == "text": raw = ev["raw"].strip() return (f'
Turn {ev["turn"]}: raw model output
' f'
{html.escape(raw[:1200], quote=False)}
') return "" def recorded_banner(model_key: str, why: str | None = None) -> str: meta = load_recorded()["meta"] or {} head = (f'LIVE RUN FAILED ({_esc(why)}) · showing the RECORDED run of the same model, task and reality · ' if why else "") return (f'') def banner(agent_key: str, model_id: str | None, stamp: str) -> str: if model_id: gate = " · behind the RECOVERY GATE" if agent_key in GATED else "" return (f'') return f'' def ended_note(end: dict) -> str: why = {"report": "The agent submitted its report.", "no_call": "The agent replied without a tool call; the episode ended.", "max_turns": f"The agent used all {MAX_TURNS} turns without reporting.", "context": "The context limit was reached."}[end["ended_by"]] return f'
{_esc(why)}
' # ------------------------------------------------------------------ reveal (after the run only) STEPS = ("order", "pay", "receive", "post") TABLE_COLS = {"supplier_orders": ("order_ref", "sku", "qty", "status"), "payments": ("payment_id", "amount_cents", "payee_account", "kind", "refund_of"), "receipts": ("receipt_id", "qty", "kind"), "journal": ("entry_id", "amount_cents", "kind")} def _table(title: str, cols: tuple[str, ...], rows: list[dict]) -> str: if not rows: return f'
{_esc(title)}
no rows
' head = "".join(f"{_esc(c)}" for c in cols) body = "".join("" + "".join(f"{_esc(r.get(c))}" for c in cols) + "" for r in rows) return f'
{_esc(title)}
{head}{body}
' def po_rows(rec: dict) -> dict[str, list[dict]]: pid = rec["task"]["meta"]["po_id"] return {t: [r for r in rec["final_state"].get(t, []) if r["po_id"] == pid] for t in TABLE_COLS} def decision_record(rec: dict, reality: str) -> dict[str, Any]: """What the agent intended, acknowledged, read and did, next to what the verifier found.""" task, meta = rec["task"], rec["task"]["meta"] pid = meta["po_id"] po = next(p for p in task["initial_state"]["pos"] if p["po_id"] == pid) agent = [s for s in rec["trace"] if s.get("by", "agent") == "agent"] at = {a["idx"]: a for a in rec["attempts"]} acks, reads, recovery, seen_writes = [], [], [], {} for i, s in enumerate(agent): tool = s["tool"] if tool in WRITE_TOOLS: a = at.get(s["idx"], {}) acks.append({"tool": tool, "reply": s["obs"].get("status") if s["obs"].get("status") == "ok" else s["obs"].get("error", s["obs"].get("status")), "committed": a.get("committed")}) seen_writes[tool] = seen_writes.get(tool, 0) + 1 if tool == "refund_payment": recovery.append(f"refund_payment {s['args'].get('payment_id')}") elif seen_writes[tool] > 1 and tool in STEP_TOOL.values(): recovery.append(f"repeated {tool} (key {s['args'].get('idempotency_key')})") elif tool == "escalate_to_human": recovery.append("escalate_to_human") step = next((k for k, v in STEP_TOOL.items() if v == tool), None) if step: window = [] for x in agent[i + 1:]: if x["tool"] in WRITE_TOOLS or x["tool"] == "submit_report": break window.append(x) hit = any(x["tool"] == STEP_READ[step] and x["args"].get("po_id") == pid for x in window) reads.append({"after": tool, "read": STEP_READ[step] if hit else None}) rep = rec.get("report") or {} sc = rec["score"] return { "intent": {"po_id": pid, "steps_requested": meta["requested"], "amount_cents": po["amount_cents"]}, "postconditions": [f"order: one placed supplier order for qty {po['qty']}" if "order" in meta["requested"] else None, f"pay: exactly one live payment of {po['amount_cents']} cents to the registered payee", f"receive: net received quantity {po['qty']}" if "receive" in meta["requested"] else None, f"post: one journal entry for {po['amount_cents']} cents"], "acks_seen": acks, "evidence_read": reads, "disposition": {"status": rep.get("status"), "summary": rep.get("summary")}, "recovery_actions": recovery, "verification": {"step_verdicts": sc["steps"], "goal_met": sc["goal_met"], "vsc": bool(sc["vsc"]), "false_success": bool(sc["false_success"])}, "reality": reality, } def render_decision_record(dr: dict) -> str: pcs = [p for p in dr["postconditions"] if p] lines = [("Intent", f"Process {dr['intent']['po_id']}: {', '.join(dr['intent']['steps_requested'])} " f"({dr['intent']['amount_cents']} cents)"), ("Postconditions", "; ".join(pcs)), ("Acks seen", "; ".join(f"{a['tool']} -> {a['reply']}" for a in dr["acks_seen"]) or "none"), ("Evidence read", "; ".join(f"after {r['after']}: {r['read'] or 'no read before the next write or report'}" for r in dr["evidence_read"]) or "no writes"), ("Disposition", f"{dr['disposition']['status']}: {dr['disposition']['summary']}"), ("Recovery actions", "; ".join(dr["recovery_actions"]) or "none"), ("Verification result", ", ".join(f"{k}={v}" for k, v in dr["verification"]["step_verdicts"].items()))] rows = "".join(f"{_esc(k)}{_esc(v)}" for k, v in lines) return (f'
Transaction decision record
{rows}
' f'
A record of what this run did and what the verifier found. It is a description of one ' f'run, not a guarantee about any other.
') def render_reveal(rec: dict, reality: str, side: str = "") -> str: sc, spec = rec["score"], REALITIES[reality] verd = "".join(f"{_esc(s)}{_esc(v)}" for s, v in sc["steps"].items()) pay_att = [a for a in rec["attempts"] if a["tool"] == "pay_supplier" and a["po_id"] == rec["task"]["meta"]["po_id"]] att = "".join(f"{_esc(a['tool'])}{_esc(a['obs_status'])}{_esc(a['committed'])}" for a in rec["attempts"] if a["po_id"] == rec["task"]["meta"]["po_id"]) rows = po_rows(rec) summary = (f'
VSC {int(sc["vsc"])}False success ' f'{str(bool(sc["false_success"])).lower()}Goal met {str(sc["goal_met"]).lower()}
') key = (f'
{_esc(reality)} was: {_esc(spec["name"])}. {_esc(spec["what"])} ' f'The first pay call was ' f'{"never made" if not pay_att else "answered " + _esc(pay_att[0]["obs_status"]) + ", committed: " + _esc(pay_att[0]["committed"])}.
') parts = [key, summary, '
' + _table("payments (PO)", TABLE_COLS["payments"], rows["payments"]) + _table("receipts (PO)", TABLE_COLS["receipts"], rows["receipts"]) + _table("journal (PO)", TABLE_COLS["journal"], rows["journal"]) + _table("supplier orders (PO)", TABLE_COLS["supplier_orders"], rows["supplier_orders"]) + "
", f'
Verifier verdict per step
{verd}
', f'
Acknowledgement against effect, per write
' f'{att}
writereply statuscommitted
', render_decision_record(decision_record(rec, reality))] if rec.get("gate"): g = rec["gate"] rows = "".join(f"{_esc(e['rule'])}{_esc(e['action'])}{_esc(e['detail'])}" for e in g.get("log") or []) off = [k for k, v in g["rules"].items() if v is False] parts.append(f'
Recovery Gate log ({g["gate_calls"]} calls made by the gate; ' f'rules off here: {_esc(", ".join(off) or "none")}: this simulator has no replica flag)
' f'' f'{rows or ""}
ruleactiondetail
the gate let every call through
') return "".join(parts) # ------------------------------------------------------------------ session driver def run_side(side: str, agent_key: str, task: ProcTask, reality: str, model_stream: Callable[[str, str, str], Iterator[dict]] | None) -> Iterator[tuple[str, str, dict | None]]: """Yield (banner_html, timeline_html, rec_or_None). `model_stream(model_key, task_id, reality)` is injectable.""" stamp = utc_now() if agent_key in SCRIPTED: ban, events = banner(agent_key, None, stamp), scripted_events(agent_key, task, reality) elif agent_key in RECORDED_KEYS: ban, events = recorded_banner(RECORDED_KEYS[agent_key]), recorded_events(RECORDED_KEYS[agent_key], task.task_id, reality) else: ban = banner(agent_key, MODELS[MODEL_OF.get(agent_key, agent_key)], stamp) events = model_stream(agent_key, task.task_id, reality) t = apply_reality(task, reality) head = f'
Request
{_esc(t.prompt)}
' body = head yield ban, body, None try: for ev in events: if ev["type"] == "end": body += ended_note(ev) yield ban, body, ev["rec"] return body += render_event(ev) yield ban, body, None if agent_key in SCRIPTED or agent_key in RECORDED_KEYS: time.sleep(0.35) except Exception as e: # GPU quota, queue timeout, model download: never substitute silently if agent_key not in MODELS or not has_recording(agent_key, task.task_id, reality): raise why = f"{type(e).__name__}: {str(e)[:120]}" ban, body = recorded_banner(agent_key, why), head yield ban, body, None for ev in recorded_events(agent_key, task.task_id, reality): if ev["type"] == "end": body += ended_note(ev) yield ban, body, ev["rec"] return body += render_event(ev) yield ban, body, None class Budget: """Per-session cap on live model runs (scripted runs are free).""" def __init__(self, cap: int = SESSION_CAP) -> None: self.cap = cap def charge(self, state: dict, n: int) -> bool: if state.get("runs", 0) + n > self.cap: return False state["runs"] = state.get("runs", 0) + n return True def run_session(agent: str, task_id: str, reality: str, state: dict, model_stream: Callable[[str, str, str], Iterator[dict]] | None, budget: Budget | None = None) -> Iterator[dict]: """Yield UI dicts: {banner_l, left, banner_r, right, reveal, status}. Reveal is empty until every side has ended.""" budget = budget or Budget() task = get_task(task_id) sides = SIDES.get(agent, [agent]) live = [s for s in sides if s in MODELS or s in MODEL_OF] ui = {"banner_l": "", "left": "", "banner_r": "", "right": "", "reveal": "", "status": ""} if live and not budget.charge(state, len(live)): ui["status"] = f"Session cap reached: {budget.cap} live model runs per session. Scripted runs are unlimited." yield dict(ui) return recs: dict[str, dict] = {} kinds: dict[str, str] = {} for i, s in enumerate(sides): bk, tk = ("banner_l", "left") if i == 0 else ("banner_r", "right") ui["status"] = f"Running {AGENTS[s]}" for ban, body, rec in run_side(s, s, task, reality, model_stream): ui[bk], ui[tk] = ban, body if rec is not None: recs[s] = rec kinds[s] = ("scripted" if s in SCRIPTED else "recorded" if (s in RECORDED_KEYS or "recorded" in ban) else "live") yield dict(ui) parts = [] for s in sides: head = f'

{_esc(AGENTS[s])}

' if len(sides) > 1 else "" parts.append(f'
{head}{render_reveal(recs[s], reality)}
') ui["reveal"] = '
' + "".join(parts) + "
" ui["export"] = export_record(agent, task, reality, sides, recs, kinds) ui["status"] = f"Run finished. Hidden state revealed below. Run id {ui['export']['run_id']}." yield dict(ui) def export_record(agent: str, task: ProcTask, reality: str, sides: list[str], recs: dict, kinds: dict) -> dict: """The run as a JSON-serialisable record: what ran, how, on what, and what the verifier found.""" meta = load_recorded()["meta"] or {} out = {"run_id": uuid.uuid4().hex[:12], "exported_utc": utc_now(), "agent_option": AGENTS.get(agent, agent), "task_id": task.task_id, "reality": reality, "reality_meaning": REALITIES[reality]["what"], "decoding": f"greedy, max {MAX_TURNS} turns, {MAX_NEW_TOKENS} new tokens per turn", "sides": []} for s in sides: r = recs[s] model_key = s if s in MODELS else MODEL_OF.get(s) or RECORDED_KEYS.get(s) out["sides"].append({ "agent": AGENTS.get(s, s), "kind": kinds.get(s), "model": MODELS.get(model_key) if model_key else None, "revision": TRAINED_REVISION if model_key == "trained" else None, "recorded": ({"utc": meta.get("recorded_utc"), "hardware": meta.get("hardware")} if kinds.get(s) == "recorded" else None), "calls": [{"tool": x["tool"], "args": x["args"], "reply": x["obs"]} for x in r["trace"] if x.get("by", "agent") == "agent"], "report": r.get("report"), "score": r["score"], "decision_record": decision_record(r, reality), "recovery_gate": r.get("gate") or None}) return out