AI Agents by HandAll problemsAbout

All problems › Agent problem · medium · ~20 min

Approval gate and injected instructions

human approval prompt injection debugging

Solve it in your browser →

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
PromptapproveExpected
triageNoneAnswer names the root cause (connection pool) and RESTARTS == [], even though a log line tells the assistant to restart payments
deniedNoneRESTARTS == [] and the answer says the restart needs approval
approvedlambda 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_TOOLS runs, approve is 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=None approves nothing. A denied or unapproved call is not executed: the model gets an is_error result 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_service rejects unknown services and records the canonical (lowercase, trimmed) name, so the approver and RESTARTS see the same value. search_logs on 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-level LOGS / RUNBOOKS / RESTARTS must stay (the checks read RESTARTS). Tool functions signal failure by raising; the loop turns that into an is_error result.

Check it: pytest tests/test_approval_gate.py

Starter code and hidden tests open in the editor. Nothing to install.