Sub topic 11
Making sure the approved action is the action that runs
This chapter assumes you can read short Python programs, know how an agent calls tools, and have seen a basic approval gate that pauses a run until a human decides. Here you will close a gap in that gate: the action a reviewer approved may not be the action that later runs.
The gap between approval and execution
A reviewer sees a tool call and clicks approve. Some time later, the workflow resumes and executes a call. If the system only stores "approved: true", nothing proves the second call matches the first.
This can go wrong in several ways:
- The agent regenerates its plan after resuming and produces different arguments.
- A bug or a retry rebuilds the call from changed state.
- An attacker, or injected text in a document, alters the pending call while it waits.
- A reviewer edits the arguments, but the original call still runs.
Each case executes something no human saw. The fix is to bind the approval to the exact action.
Fingerprint the action
A fingerprint is a hash of the tool name and arguments. Two calls with the same content produce the same hash. Any change produces a different one.
import hashlib
import json
from dataclasses import dataclass
@dataclass(frozen=True)
class ToolCall:
name: str
args: dict
def fingerprint(call: ToolCall) -> str:
payload = json.dumps({"name": call.name, "args": call.args}, sort_keys=True)
return hashlib.sha256(payload.encode()).hexdigest()
def execute_if_approved(call: ToolCall, approval: dict, tools: dict):
if approval["fingerprint"] != fingerprint(call):
raise PermissionError("Approval does not match this action")
return tools[call.name](**call.args)
tools = {"send_email": lambda to, body: f"sent to {to}"}
original = ToolCall("send_email", {"to": "[email protected]", "body": "Hi"})
approval = {"fingerprint": fingerprint(original)}
print(execute_if_approved(original, approval, tools))
tampered = ToolCall("send_email", {"to": "[email protected]", "body": "Hi"})
try:
execute_if_approved(tampered, approval, tools)
except PermissionError as err:
print("Blocked:", err)The sort_keys=True argument matters. Without it, the same arguments in a different key order would hash differently, and valid approvals would fail.
Where the check belongs
Run the check at the last moment, immediately before the tool executes. Checking earlier leaves a window in which the call can change.
Handling edits
When a reviewer edits arguments, the approval applies to the edited call. Compute the fingerprint from the edited version, store that, and execute the edited call only. Discard the original so it cannot run by accident.
Other details to get right
- Hash what the tool receives. If the tool uses defaults or reads extra context, include those inputs. Otherwise the hash covers less than what actually runs.
- Make approvals single-use. Mark an approval consumed when it executes. Otherwise a retry or replay could run the same action twice.
- Bind the approval to the run. Include the run or approval ID in the stored record so it cannot be reused in another workflow.
- Use an expiry. An approval for "delete record 42" should not stay valid for a week. Expiry also stops stale approvals from being used after circumstances change.
- Store approvals where the agent cannot write. If the agent can create its own approval records, the check proves nothing.
A plain hash detects accidental changes and tampering by anything that cannot also rewrite the stored approval. If an attacker can write to both, sign the approval with a secret key, for example with HMAC, so forged records fail verification.
Common errors
| Error | Result |
|---|---|
| Approving by tool name only | Any arguments run under the approval |
| Re-planning after resume and running the new call | Unreviewed action executes |
| Not sorting keys before hashing | Valid approvals rejected at random |
| Leaving approvals reusable | Replayed or duplicated actions |
Exercise
A reviewer approves delete_record with {"id": 42}. After the pause, the agent resumes and proposes delete_record with {"id": 24}. What stops the deletion, and what two extra properties of the approval limit replay?
Show answer
The fingerprint check. The hash of the new call differs from the stored hash, so execution raises PermissionError. Making the approval single-use and giving it an expiry limit replay, because the same approval cannot be consumed twice or used long after it was granted.