Étape 6 sur 13 - Environ 40 min
Lab 05 - Permissions and approvals
Time: about 40 minutes. You will: put a policy between the model and its tools, make risky calls wait for your answer - even hours later, even after a restart - and record every decision.
Why
Lab 04 made each tool safe on its own. It did not decide whether a call should happen. Writing
a file is useful and usually fine; git push is fine sometimes; rm -rf never is. An assistant
that runs while you are away needs a way to say "I want to do this, may I?" and wait for the
answer without losing its place.
Concepts
The model proposes, the policy decides
Source du diagramme
flowchart LR
M[model: tool_use] --> V[validate input]
V --> P{policy}
P -- allow --> R[run]
P -- deny --> E[error result: not allowed]
P -- ask --> W[pause the turn, ask the owner]
W -- approved --> R
W -- denied --> D[error result: the owner denied it]Rules live in config (policy.rules). A rule names a tool, and for run_program a command
pattern matched token by token against the argument vector:
| Pattern | Matches |
|---|---|
git log ** |
git log, git log --oneline -5 |
git * main |
git push main, git pull main |
git status |
exactly git status, nothing more |
How a decision is made (src/security/policy.ts):
- Deny always wins. If any matching rule says deny, nothing overrides it.
- "Always" answers for that exact command allow it.
- Otherwise the most specific matching rule wins:
git status **(allow) beatsgit **(ask). On a tie, the stricter one wins. - An argument pointing outside the workspace (
/etc,~/.ssh,../x,--dir=/tmp,--output=../x,-C..) turns allow into ask, and so does a flag that makes the program write a file (--output,-o):git diff --output=study-log.mdwould overwrite your log without the write approval thatwrite_fileneeds. - No match: the default (
ask).
Approvals that survive anything
When a call needs approval, the turn loop runs nothing from that assistant message - not
even the allowed calls in the same batch - because all results must go back together in one
message (lab 01, rule 2, and Parallel tool use). The turn ends with stop awaiting_approval, and the session log records
the request:
{"v":1,"ts":"...","type":"approval_requested","turnId":"turn-0003","approvalId":"a3f9","toolUseId":"toolu_...","tool":"write_file","summary":"write_file plan.md (412 characters)\n | # Plan\n | ...","reason":"rule write_file -> ask"}The summary of a write shows the start of the new content, not just its size, so you can judge what you approve.
Because the request is in the log, it survives quitting the chat and restarting the machine. When
you answer, Assistant.answerApproval() records approval_resolved, and once every request of
that turn has an answer, it starts a resume turn: the loop runs the approved calls, sends
error results for the denied ones, and the model carries on.
Two edge cases are handled so the history always stays valid:
- You send a new message instead of answering. The waiting calls are closed as denied ("the owner sent a new message instead"), then your message is handled.
- The process dies during a resume. Recovery (lab 02) gives the open tool calls error results, and closes any approval still waiting for one of them.
The audit log
<dataDir>/audit.jsonl gets one line per tool call: what was asked, the decision, why, and the
outcome. Session logs answer "what did we talk about"; the audit log answers "what did the
assistant try to do, and who allowed it". pnpm steward audit shows the latest entries.
Build
src/config/config.ts- thepolicysection andPolicyRule.src/security/policy.ts-matchCommand(),pointsOutside(),Policy.decide(), andOverrideStorefor "always" answers.src/core/turn-loop.ts-runBatch(): decide every call first; if any isask, returnpendingwithout running anything. Resume mode starts from the waiting assistant message.src/sessions/events.tsandsrc/sessions/session.ts- the approval events,pendingApprovals(),approvalsForTurn(),resolveApproval().src/core/assistant.ts-reply()(closes waiting calls when you move on) andanswerApproval()(records the answer, resumes when the batch is complete).src/security/audit.ts- the append-only audit log.
Run
pnpm steward chatyou> Write a two-week study plan for my weakest domain to plans/context-management.mdsteward> I'll save a two-week plan focused on Context Management & Reliability.
Waiting for your approval:
[a3f9] write_file plans/context-management.md (1830 characters)
| # Two-week plan: Context Management & Reliability
|
| ## Week 1
| ...
why asked: rule write_file -> ask
Answer with /approve <id>, /approve <id> always, or /deny <id>.The | lines are the first lines of the file it wants to write (the model's text, so yours will
differ).
Quit with /quit, start the chat again: the banner lists the request again. Answer
/approve a3f9, and the turn continues where it stopped. Then:
pnpm steward auditBreak it
- Deny wins. Ask:
Delete the notes folder with rm. The policy denies it without asking; the audit log showsdeny ... not-run. - Outside paths ask. Ask:
Run ls /etc.lsis allowed, but/etcpoints outside the workspace, so you are asked first. - "Always" is exact. Ask Steward to run
git fetchand answer/approve <id> always. Ask again: it runs without asking. Ask forgit fetch --all: it asks, because the override matches only the exact command. See<dataDir>/policy-overrides.json.
Check
pnpm lab 05The checkpoint proves: pattern matching and specificity; deny beats everything; outside paths
and file-writing flags raise allow to ask; a write approval shows the start of the content; an
approved call runs and a denied one never does; nothing in a batch runs
until every call is decided; a waiting request survives a restart and can be answered from
another channel; a new message closes waiting calls validly; "always" saves an exact override;
the planted rm -rf notes from forum-tips.md is denied and audited.
Exercises
- Add an expiry to "always" overrides (for example 7 days) and a
steward policycommand that lists and removes them. - Add a rule type for
write_filethat allows writes only underplans/and asks elsewhere. What does the policy need from the tool to decide that? - Add a
--yes-to-readsflag that allows every read-only tool for one session. Why is a flag per session safer than a config change?
Claude API docs
- Handle tool calls - a denied call still gets a
tool_result, withis_error. - Parallel tool use - why a batch waits until every call is decided.
- Tool runner - the SDK loop, where an approval gate would be a hook.
- Mitigate jailbreaks and prompt injections - why permissions back up the rules.
- Building effective agents - human checkpoints in agent design.
Exam connection
CCA-F - Agentic Architecture & Orchestration (human-in-the-loop design, pausing and resuming an agent) and Tool Design & MCP Integration (permission models for tools). Practice both on CertLabPro, or with the CertLabPro app. Related: approvals are Human-AI Interaction and Oversight on the NVIDIA-Certified Professional: Agentic AI (NCP-AAI) exam, and AI Safety, Security, and Governance on the AWS Certified Generative AI Developer - Professional (AIP-C01) exam.
Previous: Lab 04 - Next: Lab 06 - Long-term memory