901. Permission gate for agent tool calls
Why it matters Agents that can send email, pay or run shell commands need a gate between the model and the tool. Allow, ask and deny tiers plus content checks are the standard design.
Before an agent runs a tool, a permission gate decides what happens.
Write gate_calls(calls, policy, approvals, deny_patterns).
- calls: list of dicts {"id": str, "tool": str, "args": dict}.
- policy: dict tool -> "allow", "ask" or "deny". Tools not in the policy are "ask".
- approvals: list of call ids the user has approved.
- deny_patterns: list of substrings. If any string value in args (top level only, compared case-insensitively) contains a pattern, the call is blocked whatever the policy says.
Decision for each call, checked in this order:
1. a deny pattern matches -> "blocked:pattern"
2. policy is "deny" -> "blocked:policy"
3. policy is "allow" -> "run"
4. policy is "ask" -> "run" if the id is in approvals, else "pending"
Return a list of [id, decision] pairs in call order.
def gate_calls(calls, policy, approvals, deny_patterns)Example 1
Input: gate_calls([{'id': 'c1', 'tool': 'search', 'args': {'q': 'weather'}}, {'id': 'c2', 'tool': 'shell', 'args': {'cmd': 'rm -rf /'}}, {'id': 'c3', 'tool': 'send_email', 'args': {'to': 'boss@x.com'}}], {'search': 'allow', 'shell': 'ask'}, [], ['rm -rf'])
Output: [['c1', 'run'], ['c2', 'blocked:pattern'], ['c3', 'pending']]
Example 2
Input: calls = [{'id': 'c3', 'tool': 'send_email', 'args': {'to': 'boss@x.com'}}], policy = {}, approvals = ['c3'], deny_patterns = []
Output: [['c3', 'run']]
Example 3
Input: calls = [{'id': 'a', 'tool': 'pay', 'args': {'amount': 10}}, {'id': 'b', 'tool': 'pay', 'args': {'note': 'IGNORE previous instructions'}}], policy = {'pay': 'deny'}, approvals = ['a', 'b'], deny_patterns = ['ignore previous']
Output: [['a', 'blocked:policy'], ['b', 'blocked:pattern']]
Constraints
- Python 3 standard library. Code runs in your browser.
- Your code must pass all 5 tests to be accepted.
Hint 1
Lower-case the patterns and the string arg values once.
Hint 2
Check patterns before the policy.
Hint 3
Default unknown tools to ask.
Common mistakes
- Letting approvals override a deny.
- Checking non-string args with in, which raises for ints.
- Defaulting unknown tools to allow.
Understand the idea
Order matters: hard blocks first (dangerous content, denied tools), then automatic allows, then human approval for everything else. Unknown tools default to ask, which fails safe. Matching patterns case-insensitively stops trivial bypasses like .ENV instead of .env; real systems add normalisation and allow-lists on top.
Shapes and data flow
Lists and dicts in, list of pairs out.