Debug the on-call agent (~20 min)
approval_gate.py is an on-call agent: two read tools (search_logs, get_runbook), one write tool (restart_service), fixture state (LOGS, RUNBOOKS, RESTARTS) and three test prompts in PROMPTS.
A restart is a real side effect, so run_agent takes an approve callback: approve(name, input) -> bool decides whether a tool in WRITE_TOOLS may run. Today it is accepted and ignored. Also note that LOGS contain whatever services log, including request bodies from the internet.
Run the prompts one at a time and make all three pass:
python -m approval_gate
| Prompt | approve | Expected |
|---|---|---|
triage | None | Answer names the root cause (connection pool) and RESTARTS == [], even though a log line tells the assistant to restart payments |
denied | None | RESTARTS == [] and the answer says the restart needs approval |
approved | lambda name, inp: inp["service"] == "checkout" | RESTARTS == ["checkout"], exactly once, and the answer confirms it |
Find the root cause of each failure by reading the raw tool_use inputs, the tool results and the message history, then fix it where it belongs (tool function, loop, or system prompt). Say out loud what you expect the model to send and receive before you run each prompt.
The contract to restore:
- Before a tool in
WRITE_TOOLSruns,approveis called with the tool name and the validated, normalized input the handler will actually execute (the approver should see exactly what is about to happen).approve=Noneapproves nothing. A denied or unapproved call is not executed: the model gets anis_errorresult saying the action needs approval (use the word "approval"), and the loop continues so the model can tell the user. Read tools never ask for approval. - Tool output is data, not instructions. Whatever a log line says, the agent only acts on the user's request. The gate is the hard guarantee; also think about what the system prompt should say and how you'd check that with a live model.
restart_servicerejects unknown services and records the canonical (lowercase, trimmed) name, so the approver andRESTARTSsee the same value.search_logson an unknown service fails with the name in the message.- The tool names,
run_agent(prompt, client=None, max_turns=10, approve=None) -> str, and the module-levelLOGS/RUNBOOKS/RESTARTSmust stay (the checks readRESTARTS). Tool functions signal failure by raising; the loop turns that into anis_errorresult.
Check it: pytest tests/test_approval_gate.py