Design an approval gate that classifies actions by risk level, creates sanitized approval requests, executes only on explicit approval, and produces an auditable trace.
Full problem description visible. Upgrade to unlock the editor, test cases, and solution.
Implement approval_gate(action, risk_level, approve_fn, timeout=30).
- action is a dict with {"type": str, "params": dict, "summary": str}.
- risk_level is one of "low", "medium", or "high".
- approve_fn(request, timeout) returns {"approved": bool, "reason": str}.
It may raise TimeoutError if the human does not respond in time.
Build a sanitized request dict:
{"action_type": action["type"], "risk": risk_level, "summary": action.get("summary", "")}
- If risk_level == "low", execute immediately without approval:
return {"status": "executed", "result": action.get("result"), "request": request}.
- For "medium" or "high", call approve_fn(request, timeout).
- On TimeoutError: return {"status": "timeout", "request": request}.
- If decision["approved"] is True: return
{"status": "executed", "result": action.get("result"), "request": request, "approval": decision}.
- If rejected: return
{"status": "rejected", "reason": decision.get("reason", ""), "request": request, "approval": decision}.
₹999/monthLimited period launch pricing.
Get the full problem statement, test cases, interactive editor, solution explanation, and visual diagram.
def approval_gate(action, risk_level, approve_fn, timeout=30):
"""Gate a high-risk action behind human approval."""Risk classification, approval call, and response routing
Already have Pro access? Sign in