---
title: "Agent lesson 4: Restricted folders"
description: "Keep an agent out of a folder, and see what it sees instead - nothing of the folder, not even its files' names; and a file it writes there left out of its change, named as refused."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.forge.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent lesson 4: Restricted folders

**Time:** 20 minutes. **Track:** [agent builder](https://docs.forge.workreef.dev/learn/#agent-builder). **Before this:** [agent lesson 1](https://docs.forge.workreef.dev/learn/agents/drive-the-forge/).

By the end of this lesson you will be able to:

- say what an agent outside a restricted folder is given of it: nothing, not even names;
- read the forge's answer that names a file the agent may not write;
- say why an agent's access is never more than that of the person it works for.

## A folder for one team

Start a forge, and land a service with a payroll folder from a person's workspace:

```console server
$ af-server --dev
...
```

```console check
$ source ~/af-dev/dev/env
$ af workspace add ~/all
Workspace at /home/you/all
Paths: **
main has no landing yet
$ cd ~/all
$ af new -m "Lay out the service"
Working on change soqmmuqwxmkvonywxysykznkmvlynxko at commit d0a67732f79be9f177fad73bc1a3514c2a1c8f0f50b11695e5842ee8cb2427e1
$ mkdir -p api payroll
$ printf 'def pay(who):\n    return rates[who]\n' > api/pay.py
$ printf 'name,salary\nada,100\n' > payroll/salaries.csv
$ af submit
Submitted commit f87f5ce1f37fabba80cd633726d6aaffea2dbc404f8b2429f20ffa849f853b2c, 2 edits in its last snapshot
```

```console eventually
$ af status
Change soqmmuqwxmkvonywxysykznkmvlynxko (landed) at commit f87f5ce1f37fabba80cd633726d6aaffea2dbc404f8b2429f20ffa849f853b2c
```

Restrict the folder to the payroll team, as the identity provider names its group. The development forge's person is in no group, so you are outside it, and an agent you start has no more access than you:

```console check
$ af folder restrict payroll --team payroll --reason "salary data"
done: it took effect at once, on your word as an administrator
$ af folder list
payroll (reads and writes): team payroll
```

The agent's part is played by the small MCP client of [lesson 1](https://docs.forge.workreef.dev/learn/agents/drive-the-forge/#the-forges-tools-over-mcp):

```console check
$ cat > ~/agent-session <<'END'
> #!/bin/bash
> coproc MCP { af "${@:2}" mcp "$1"; }
> say() { printf '%s\n' "$1" >&"${MCP[1]}"; }
> say '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"agent-session","version":"1"}}}'
> read -r _ <&"${MCP[0]}"
> say '{"jsonrpc":"2.0","method":"notifications/initialized"}'
> id=0
> while IFS= read -r step; do
>   case "$step" in
>     '! '*) eval "${step#! }" ;;
>     ?*) id=$((id + 1)); name=${step%% *}
>         say "{\"jsonrpc\":\"2.0\",\"id\":$id,\"method\":\"tools/call\",\"params\":{\"name\":\"$name\",\"arguments\":${step#* }}}"
>         read -r reply <&"${MCP[0]}"
>         case "$reply" in
>           *'"structuredContent":'*) reply=${reply#*\"structuredContent\":}; reply=${reply%,\"isError\":*} ;;
>           *'"error":'*) reply=${reply#*\"error\":}; reply=${reply%\}} ;;
>         esac
>         printf '%s -> %s\n' "$name" "$reply" ;;
>   esac
> done
> END
$ chmod +x ~/agent-session
```

## What the agent is given

The agent opens a workspace of every file. It is given the API's, and nothing of the payroll folder:

```console check
$ ~/agent-session ~/agent <<'END'
> open_workspace {"patterns":["**"]}
> status {}
> ! find ~/agent/files -type f | sort
> read {"path":"payroll/salaries.csv"}
> END
open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["**"],"written":1}
status -> {"change":null,"commit":null,"conflicts":[],"findings":0,"findings_partial":false,"landing":1,"refused":[],"state":null,"waiting_for":[],"withheld":[]}
/home/you/agent/files/api/pay.py
read -> {"code":-32602,"message":"payroll/salaries.csv is no file at f87f5ce1f37fabba80cd633726d6aaffea2dbc404f8b2429f20ffa849f853b2c"}
```

For the agent the folder does not exist: no file of it is on disk, `status` names none, and a `read` answers as for a path main does not hold. Not even the files' names reach it, since a name, `layoffs.csv` say, can tell as much as what is in the file. A search finds nothing there either, as the first exercise shows, and the third shows the file was there to find.

That is different from a file the agent may read whose contents the forge was made to purge: those it names as **withheld**, with why, since the agent may know the file is there.

## What the agent may not write

The agent starts a change and writes a file in the folder, and one outside it:

```console check
$ ~/agent-session ~/agent <<'END'
> open_workspace {}
> start_change {"description":"Pay by the hour","author_name":"Lesson Agent","author_email":"agent@localhost"}
> ! mkdir -p ~/agent/files/payroll && printf 'name,hourly\nada,1\n' > ~/agent/files/payroll/hourly.csv
> ! printf 'def pay(who, hours):\n    return rates[who] * hours\n' > ~/agent/files/api/pay.py
> status {}
> diff {}
> END
open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["**"],"written":0}
start_change -> {"change":"rmxvntszkltmyplrrmpxspomqvorqruv","commit":"31c5661752f46fa200b7fdfcc14742696cc6d111ed305074242ac760e057ccb8"}
Warning: Refused to snapshot some files:
  payroll/hourly.csv: this actor may not write it
status -> {"change":"rmxvntszkltmyplrrmpxspomqvorqruv","commit":"dae2ca5ce9b93ea29ea24cd081bc43c50ebf0fb07f9c6d1b75240b863b932d98","conflicts":[],"findings":0,"findings_partial":false,"landing":1,"refused":["payroll/hourly.csv"],"state":"open","waiting_for":["scan"],"withheld":[]}
Warning: Refused to snapshot some files:
  payroll/hourly.csv: this actor may not write it
diff -> {"base":"f87f5ce1f37fabba80cd633726d6aaffea2dbc404f8b2429f20ffa849f853b2c","commit":"dae2ca5ce9b93ea29ea24cd081bc43c50ebf0fb07f9c6d1b75240b863b932d98","files":[{"after":"file","before":"file","path":"api/pay.py","unified":"--- a/api/pay.py\n+++ b/api/pay.py\n@@ -1,2 +1,2 @@\n-def pay(who):\n-    return rates[who]\n+def pay(who, hours):\n+    return rates[who] * hours\n","withheld":null}]}
```

The file in the folder is left out of the change and named as **refused**, in `status` and in a warning on the server's standard error, which an agent's harness logs: the snapshot takes the agent's other edits and leaves that one on disk, as jj leaves out a file it may not write. The change holds only the API's edit, so nothing the agent may not write ever reaches the forge.

## Exercises

1. Have the agent search main for `salary`, the header of the file it may not read. What does it find?

   ```console check
   $ ~/agent-session ~/agent <<'END'
   > open_workspace {}
   > search {"pattern":"salary"}
   > END
   open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["**"],"written":0}
   search -> {"commit":"f87f5ce1f37fabba80cd633726d6aaffea2dbc404f8b2429f20ffa849f853b2c","files":[]}
   ```

   Nothing. The answer names the landing it searched, which is the one that holds `payroll/salaries.csv`, so the index held the file and the forge left it out of what this agent may see. Exercise 3 asks again once the folder is open.

2. Have the agent remove the file it may not write, and check that nothing is refused any more.

   ```console check
   $ ~/agent-session ~/agent <<'END'
   > open_workspace {}
   > ! rm -r ~/agent/files/payroll
   > status {}
   > END
   open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["**"],"written":0}
   status -> {"change":"rmxvntszkltmyplrrmpxspomqvorqruv","commit":"dae2ca5ce9b93ea29ea24cd081bc43c50ebf0fb07f9c6d1b75240b863b932d98","conflicts":[],"findings":0,"findings_partial":false,"landing":1,"refused":[],"state":"open","waiting_for":["scan"],"withheld":[]}
   ```

3. Lift the restriction. The agent's next sync brings the folder's files, as a sync brings main's: a workspace follows its actor's access as it follows main. The same search as in exercise 1 now finds the file.

   ```console check
   $ af folder unrestrict payroll --reason "the data moved"
   done: it took effect at once, on your word as an administrator
   $ ~/agent-session ~/agent <<'END'
   > open_workspace {}
   > ! find ~/agent/files -type f | sort
   > search {"pattern":"salary"}
   > END
   open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["**"],"written":1}
   /home/you/agent/files/api/pay.py
   /home/you/agent/files/payroll/salaries.csv
   search -> {"commit":"f87f5ce1f37fabba80cd633726d6aaffea2dbc404f8b2429f20ffa849f853b2c","files":[{"lines":[{"number":1,"text":"name,salary"}],"path":"payroll/salaries.csv"}]}
   ```

## Check your understanding

<details>
<summary>Why does an agent outside a restricted folder see nothing of it, not even names?</summary>

A file's name can tell as much as its contents, so the forge gives an agent outside the folder neither: a sync sends nothing of it, a read answers as for no file, and a search finds nothing there. A file the agent may read but whose contents were purged is different: it is named as withheld, since knowing it is there tells the agent nothing it may not know.

</details>

<details>
<summary>An agent writes a file in a folder it may not write. Is its change refused?</summary>

No: the file is left out of the change and named as refused, and the agent's other edits are taken as ever. The file stays on the agent's disk; nothing of it reaches the forge.

</details>

<details>
<summary>A person gains access to a folder, or loses it. What happens to their agent's workspace?</summary>

Its next sync follows: a folder newly readable comes onto its disk, and one newly closed leaves it, a file the agent edited there kept on disk, as a sync keeps an edit. The workspace knows the access its files were synced under and sends it with each sync; when it moved, the forge answers the sync whole.

</details>

<details>
<summary>Can an agent be given more access than the person who started it?</summary>

No. An agent's access is never more than that of the line it was born from, so restricting a folder for a person restricts it for every agent below them.

</details>

## Next

[Agent lesson 5: Keys for agents](https://docs.forge.workreef.dev/learn/agents/keys-for-agents/) gives an agent some of the organization's keys and not others. The [curriculum map](https://docs.forge.workreef.dev/learn/) lists the other tracks. [Permissions and approvals](https://docs.forge.workreef.dev/concepts/permissions/) explains restricted folders and grants in depth.

Source: https://docs.forge.workreef.dev/learn/agents/restricted-folders/index.mdx
