Sub topic 6
Scope and Duration: Once, This Session, or Always
This chapter assumes you know the allow, ask, and deny decisions and have seen how allow rules use patterns. Here you will learn how long and how widely an approval applies.
Two dials on every approval
When an agent asks permission and you say yes, you set two things, whether or not the prompt shows them.
- Scope is what the approval covers. It might be one exact command, a pattern such as
git diff*, every use of a tool, or a whole directory. - Duration is how long the approval lasts. It might be this single call, the current session, or every future session.
The two combine. "Always allow pytest*" is a narrow scope with a long duration. "Allow all shell commands for this session" is a wide scope with a short duration. Wide scope and long duration together are the riskiest pair.
The three durations
Once covers one call and is then forgotten. Use it for anything unusual, such as a one-off install or a command that touches files outside the project.
This session is held in memory and disappears when the agent exits. It suits repeated, low-risk work. For example, you might approve test runs while iterating on a bug.
Always is written to a settings file and applies in every later session. It is convenient, but a grant you made months ago still applies today, even if you no longer remember making it.
A small grant store
This sketch shows the mechanics. Once-grants are consumed on use. Session grants live in memory. Saved grants stand in for a settings file.
from dataclasses import dataclass
from fnmatch import fnmatch
@dataclass
class Grant:
pattern: str
duration: str # "once", "session", or "always"
session_grants = []
saved_grants = [Grant("git status", "always")]
def check(command):
"""Return True if a stored grant covers this command."""
for grant in session_grants + saved_grants:
if fnmatch(command, grant.pattern):
if grant.duration == "once":
session_grants.remove(grant) # consumed on first use
return True
return False
session_grants.append(Grant("pytest*", "session"))
session_grants.append(Grant("npm install", "once"))
print(check("pytest -q")) # True
print(check("npm install")) # True
print(check("npm install")) # False, the once-grant was used up
print(check("git status")) # True, from the saved grant
print(check("git push")) # False, never grantedChoosing scope carefully
Prompts often offer a pattern rather than the exact command you saw. If the proposed pattern is git *, you would be approving git push --force as well as git status. Edit the pattern down to the narrowest form that still saves you effort, such as git diff*.
The pattern problems from the previous chapter still apply. A prefix grant can cover a chained command, so a wide durable grant multiplies that risk.
Practical habits
- Default to once. Promote a grant to session only after you have seen the same safe request several times.
- Make "always" grants for read-only commands only, such as
git statusorls*. - Never give a durable grant to a command that deletes, publishes, or reaches the network.
- Keep durable grants in project settings that you can inspect and commit, not hidden in a personal file.
Exercise
You approve "git *" as an always-grant because the agent keeps running git diff. Name two problems with this choice and a better grant.
Show answer
The scope is too wide, because it also approves git push --force and git reset --hard. The duration is too long, because it stays active in every future session. A better choice is a session grant for git diff*, or an always-grant limited to read-only commands such as git status and git diff*.