Sub topic 3
Allow, ask, or deny: the permission decision
This chapter assumes you know the agent loop from earlier in the tutorial. The model proposes a tool call, the harness (the program that runs the model and its tools) executes it, and the result goes back to the model. It also assumes you can read short Python and use a shell. Here we look at the step between "the model proposes" and "the harness executes".
The decision point
A coding agent does not run tools directly from model output. The harness intercepts each proposed tool call and produces one of three decisions before anything touches your system:
- Allow: the call runs immediately, with no prompt.
- Ask: the harness pauses and shows the call to you. You approve or reject it.
- Deny: the call never runs, and no prompt is shown.
This check lives in the harness, not in the model. The model can be told "do not delete files", but that is only a request. The harness decision is code that runs whatever the model says, so it is the part you can reason about and configure.
What each outcome means in practice
| Decision | Who decides | Human involved | What the model sees afterward |
|---|---|---|---|
| Allow | A rule or the current mode | No | The tool's real output |
| Ask | No rule matched, or a rule says to ask | Yes, per call | Real output if approved, a refusal if rejected |
| Deny | A deny rule or a policy | No | A refusal message instead of output |
Two details matter here.
First, a refusal is itself a tool result. The model reads it and continues. A well-behaved agent will report that it was blocked and pick another approach. A less careful one may try a different route to the same goal, such as using a Python one-liner to delete files after rm was denied. A deny rule on one command string cannot stop the agent from reaching the same outcome through another tool. This is the main reason the tutorial later covers sandboxing, which limits what any process can do regardless of the command used.
Second, "ask" is the default for anything the rules did not anticipate. That default is a safety property. A decision system that allowed unmatched calls would fail open, meaning any gap in your rules becomes a gap in your protection. Systems that ask on unmatched calls fail closed, at the cost of more interruptions.
Why deny is checked first
When both a deny rule and an allow rule match the same call, the harness needs a tiebreaker. The common and safest choice is that deny wins. Evaluating deny rules first has a useful consequence. You can write broad allow rules for convenience and carve out exceptions with narrow deny rules, and the exceptions hold.
Here is a minimal implementation. It returns a reason along with the outcome, which is useful for logging and for showing you why a prompt appeared.
from dataclasses import dataclass
from fnmatch import fnmatchcase
@dataclass(frozen=True)
class Decision:
outcome: str # "deny", "allow", or "ask"
reason: str
DENY = ["rm -rf *", "git push --force*", "curl *"]
ALLOW = ["git status", "git diff*", "pytest*", "git push*"]
def decide(command: str) -> Decision:
"""Evaluate deny rules first, then allow rules, then fall back to ask."""
for pattern in DENY:
if fnmatchcase(command, pattern):
return Decision("deny", f"matched deny rule {pattern!r}")
for pattern in ALLOW:
if fnmatchcase(command, pattern):
return Decision("allow", f"matched allow rule {pattern!r}")
return Decision("ask", "no rule matched")
for cmd in [
"git status",
"git push origin main",
"git push --force origin main",
"rm -rf build",
"npm install",
]:
d = decide(cmd)
print(f"{cmd!r:40} -> {d.outcome:5} ({d.reason})")Running it prints:
'git status' -> allow ('matched allow rule 'git status'')
'git push origin main' -> allow ('matched allow rule 'git push*'')
'git push --force origin main' -> deny ('matched deny rule 'git push --force*'')
'rm -rf build' -> deny ('matched deny rule 'rm -rf *'')
'npm install' -> ask ('no rule matched')Look at the third line. git push* is a broad allow rule that also matches the forced push, but the deny rule is checked first, so the forced push is blocked. If the order were reversed, the allow rule would win and the deny rule would be useless.
The code uses fnmatchcase rather than fnmatch. The plain fnmatch normalizes case and path separators according to the operating system, so its results can differ between Windows and Linux. A permission check should behave the same everywhere.
Edge cases in the decision
A deny rule can match more than you intended
Pattern git push --force* also matches git push --force-with-lease, which is a safer variant many teams want to permit. Deny rules are blunt. If you need to allow the lease form, you must write the deny pattern more narrowly or accept the extra prompts. How to write patterns well, and where they fail, is the subject of a later chapter.
"Ask" is not the same as "safe because a human looked"
An ask decision only helps if the person reads the prompt. A prompt that shows a truncated command, or that appears fifty times an hour, gets approved by reflex. Treat ask as a control with a quality that depends on how often it fires and how clearly it presents the call. Approval fatigue gets its own chapter.
Deny reasons should be visible to you and to the model
When a call is denied, say so plainly in the refusal. If the model only sees "error", it may retry the same call or try odd workarounds. If it sees "denied by policy: network commands are not permitted", it can explain the limit to you and propose something else. For you, the same reason string answers the question "why did it stop?" without a debugging session.
The decision is per call, not per task
The harness decides each tool call separately. Allowing git diff says nothing about the next call. Nor does it say anything about a call that looks similar but differs in one argument. The model can build a harmful action out of several individually allowed calls: read a secret file (allowed), then put its content in a command that sends it elsewhere. Per-call decisions are necessary, but they do not see the whole plan.
Unmatched calls need a policy when nobody is there
"Ask" assumes a person is at the terminal. In unattended runs, such as a scheduled script, an ask has no one to answer it. A harness must then choose a fallback, usually deny, or it hangs forever. Never let an unattended run quietly convert ask into allow. That is covered in the chapter on scripts and CI.
Decisions depend on the action category
Rules rarely apply to all tools equally. A typical default mapping follows the risk categories from the previous chapter:
- Reading files inside the project: often allowed by default.
- Writing or editing files: often ask, or allowed within the workspace.
- Running shell commands: ask unless a rule matches.
- Network access and external tool servers: ask or deny by default.
So the full decision combines the kind of tool, the specific arguments, any rules that match, and the current mode. The mode is the subject of the next chapter. For now, think of a mode as a bundle of defaults that shifts the fallback for unmatched calls, for example from "ask" to "allow file edits".
Checking your understanding of the model
Before moving on, you should be able to predict the outcome of a call by tracing the flow above. Take git push --force-with-lease, using the rule lists in the code example, and trace it. It matches the deny pattern git push --force*, so it is denied, even though git push* would also have allowed it. Now take pytest -x tests/. It matches no deny rule and matches pytest*, so it is allowed. Finally, pip install requests matches nothing and falls through to ask.
Exercise
Using the DENY and ALLOW lists from the chapter's code (DENY = ["rm -rf ", "git push --force", "curl "], ALLOW = ["git status", "git diff", "pytest", "git push"]), give the decision for each command and say which rule or fallback produced it: (1) git diff HEAD~1, (2) curl https://example.com, (3) git push --force-with-lease, (4) make test. Then explain why swapping the order of the two checks would be a security bug.
Show answer
(1) allow, from git diff*. (2) deny, from curl *. (3) deny, because the deny pattern git push --force* matches and deny is checked first, even though git push* would also match. (4) ask, because no rule matches and unmatched calls fall back to asking. If allow rules were checked first, any call matched by a broad allow rule would run even when a narrower deny rule was meant to block it. For example, git push* would approve a force push. Deny rules would then be unable to carve out exceptions, and a convenience rule could silently defeat a safety rule.
Exercise
An unattended nightly job runs a coding agent, and the agent proposes a command that matches no rule. The harness has no one to answer a prompt. What should the fallback decision be, and why is "allow" the wrong choice?
Show answer
The fallback should be deny, or the run should stop with an error. Allowing unmatched calls makes the system fail open. Any command your rules did not anticipate would run with no human review, so the gaps in your rules become gaps in your protection. Denying keeps the system fail closed. The cost is a failed run, which you can fix by adding a specific allow rule after review.