---
title: "Agent lesson 3: Narrow, fold and compare"
description: "An agent narrows its workspace to the paths it works on with sparse, folds a fix into the change it belongs to with absorb, and sees what changed between two operations with op_diff."
---

> 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 3: Narrow, fold and compare

**Time:** 25 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:

- narrow an agent's workspace to the paths it works on, and open it again as it is;
- fold a fix into the change below that it belongs to, without naming that change;
- see what changed between two operations, change by change.

## A repository and a client

Start a forge, and land a small service 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 znunmtrrkrlqvvuuvumpnqkoyymzzmvq at commit ab4393cb6b26af53761071d9e3f161a544ff6020bf3fa5058dbb59b9e59ff12d
$ mkdir -p api web docs
$ printf 'def greet(name):\n    return "Hello"\n' > api/routes.py
$ printf '<p>Hello</p>\n' > web/index.html
$ printf '# Guide\n' > docs/guide.md
$ af submit
Submitted commit b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252e, 3 edits in its last snapshot
```

```console eventually
$ af status
Change znunmtrrkrlqvvuuvumpnqkoyymzzmvq (landed) at commit b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252e
```

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): each line it reads is a call, `NAME ARGUMENTS`, or a command to run between calls, `! COMMAND`.

```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
```

## Narrow the workspace

An agent given a task in the API needs the API's files, not the whole repository: fewer files to read, to sync and to snapshot. `sparse` changes the paths its workspace holds, as `jj sparse set` does: here, from every file to `api/**` alone.

```console check
$ ~/agent-session ~/agent <<'END'
> open_workspace {"patterns":["**"]}
> sparse {"clear":true,"add":["api/**"]}
> ! find ~/agent/files -type f | sort
> END
open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["**"],"written":3}
sparse -> {"added":0,"kept":[],"patterns":["api/**"],"removed":2}
/home/you/agent/files/api/routes.py
```

The workspace keeps its paths. When the agent's harness starts `af mcp` again, the agent opens its workspace as it is, with no patterns to remember; naming other ones is refused, pointing to `sparse`:

```console check
$ ~/agent-session ~/agent <<'END'
> open_workspace {}
> open_workspace {"patterns":["**"]}
> END
open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["api/**"],"written":0}
open_workspace -> {"code":-32602,"message":"this workspace holds [\"api/**\"]: leave the patterns out to open it as it is, or change them with `sparse`"}
```

## A fix where it belongs

The agent builds a stack: a change that greets by name, and on top of it, one that adds a health route. Before the work, note where the log is, for the last section:

```console check
$ export BEFORE=$(af op log -n 1 | head -1 | cut -d' ' -f1)
$ ~/agent-session ~/agent <<'END'
> open_workspace {}
> start_change {"description":"Greet by name","author_name":"Lesson Agent","author_email":"agent@localhost"}
> ! printf 'def greet(name):\n    return "Hello, " + nmae\n' > ~/agent/files/api/routes.py
> start_change {"description":"Add a health route","author_name":"Lesson Agent","author_email":"agent@localhost"}
> ! printf 'def health():\n    return "ok"\n' > ~/agent/files/api/health.py
> END
open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["api/**"],"written":0}
start_change -> {"change":"upnnqxortvlqzntxpwtqnluqyrrklpps","commit":"8963fa057eba572a98e06d5257bb8442b15597560eff4b2c738270d39ed11025"}
start_change -> {"change":"kuusopppvqqqtmtrkyvlpmzpkrulsyqm","commit":"ece6c7395a1e32dd44ca92416b004f6533a7623913fd65db8bd144912220a23a"}
```

Working on the health route, the agent sees the typo it made in the change below, `nmae`, and fixes it where it is, in its files. The fix belongs in the change below, not in the health route. `absorb` puts each edit into the change below that last changed those lines, as `jj absorb` does, found by annotation, so the agent need not name that change:

```console check
$ ~/agent-session ~/agent <<'END'
> open_workspace {}
> ! printf 'def greet(name):\n    return "Hello, " + name\n' > ~/agent/files/api/routes.py
> absorb {}
> diff {}
> END
open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["api/**"],"written":0}
absorb -> {"into":[{"change":"upnnqxortvlqzntxpwtqnluqyrrklpps","commit":"376a67f3fcd7ce509fd93507139ce8bd00cbdddf21175738ad7a496ccbc78444"}],"rebased":1,"skipped":[],"source":{"change":"kuusopppvqqqtmtrkyvlpmzpkrulsyqm","commit":"5174fa4c9f1bee58ea265ae56bea44a7aebac3835f5db954599e2a9bb56e68ff"}}
diff -> {"base":"376a67f3fcd7ce509fd93507139ce8bd00cbdddf21175738ad7a496ccbc78444","commit":"5174fa4c9f1bee58ea265ae56bea44a7aebac3835f5db954599e2a9bb56e68ff","files":[{"after":"file","before":"none","path":"api/health.py","unified":"--- a/api/health.py\n+++ b/api/health.py\n@@ -0,0 +1,2 @@\n+def health():\n+    return \"ok\"\n","withheld":null}]}
```

The fix went into "Greet by name", which last changed that line, and the health route was rebased onto it; the top change's diff is the health route alone. An edit with no change below to go to stays where it is, and so does one in a path the agent may not write, named in `skipped`.

## What changed since

`op_diff` compares two operations, as `jj op diff` does: each change as it stood at the first and at the second, one row however many operations moved it. From where the log was before the work, to now:

```console check
$ ~/agent-session ~/agent <<END
> op_diff {"from":"$BEFORE"}
> END
op_diff -> {"changes":[{"after":{"latest":"376a67f3fcd7ce509fd93507139ce8bd00cbdddf21175738ad7a496ccbc78444","state":"open"},"before":null,"change":"upnnqxortvlqzntxpwtqnluqyrrklpps","ordinal":0},{"after":{"latest":"5174fa4c9f1bee58ea265ae56bea44a7aebac3835f5db954599e2a9bb56e68ff","state":"open"},"before":null,"change":"kuusopppvqqqtmtrkyvlpmzpkrulsyqm","ordinal":1}],"from":"d71501df97a2b585fdad2d13ad94ccb0c5dcc22e5c93ee9aad0000914b711761457ed6d2a6035b77f3d7eeca5c52a821ec4ee58b1512fc6ff38b282e885b8cdf","from_ended":"2026-11-25T14:22:52.645Z","main_from":"b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252e","main_to":"b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252e","next":null,"to":"232fbf65a4cc90a1c1916d671e8d82ec1789979986dd6e7171420ee9cebf27a4faa7196a09f421df8ab9dce747a1a4f5a4ed5dfbbd49f7a03506abb02a569238","to_ended":"2026-11-25T14:22:53.104Z"}
```

Both changes are new since then, so each has no `before`, and its `after` is its latest commit and state. Given nothing, `op_diff` compares the newest operation with its parent: what the last step did.

## Exercises

1. Have the agent hold the docs too, and say how many files arrived.

   ```console check
   $ ~/agent-session ~/agent <<'END'
   > open_workspace {}
   > sparse {"add":["docs/**"]}
   > END
   open_workspace -> {"directory":"/home/you/agent/files","landing":1,"patterns":["api/**"],"written":0}
   sparse -> {"added":1,"kept":[],"patterns":["api/**","docs/**"],"removed":0}
   ```

2. Ask what the last operation did. Adding a path published none, so it is the `absorb`: both changes moved, the first by the fix and the second rebased onto it.

   ```console check
   $ ~/agent-session ~/agent <<'END'
   > op_diff {}
   > END
   op_diff -> {"changes":[{"after":{"latest":"376a67f3fcd7ce509fd93507139ce8bd00cbdddf21175738ad7a496ccbc78444","state":"open"},"before":{"latest":"fce01c631c8e3376ae6784a3b582a8d831ebacbe39bb30b9a83818f8256b26a2","state":"open"},"change":"upnnqxortvlqzntxpwtqnluqyrrklpps","ordinal":0},{"after":{"latest":"5174fa4c9f1bee58ea265ae56bea44a7aebac3835f5db954599e2a9bb56e68ff","state":"open"},"before":{"latest":"84d2a323021ef7c4c20f01616b3962b3ef6857e1c64609d225b2a322967562bb","state":"open"},"change":"kuusopppvqqqtmtrkyvlpmzpkrulsyqm","ordinal":1}],"from":"9378e74a73d2aa7b125bd93ef3c0e7d93cb43af792c0f7215ff8e26590c77850e74a7e0aaadb8465d1d222a313cefb6859223c91e9b7455a09a2b74409d8d694","from_ended":"2026-11-25T14:23:33.568Z","main_from":"b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252e","main_to":"b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252e","next":null,"to":"232fbf65a4cc90a1c1916d671e8d82ec1789979986dd6e7171420ee9cebf27a4faa7196a09f421df8ab9dce747a1a4f5a4ed5dfbbd49f7a03506abb02a569238","to_ended":"2026-11-25T14:22:53.104Z"}
   ```

## Check your understanding

<details>
<summary>Why give an agent a narrow workspace?</summary>

It reads, syncs and snapshots only the files it holds, so a large repository costs it what its task needs. It can still read any file of main with `read` and `search`, and widen its paths with `sparse` when the task grows.

</details>

<details>

<summary>

The agent's harness restarts it. What does it pass to `open_workspace`?

</summary>

Nothing: a workspace that exists opens with its own paths, as `sparse` last left them. Naming other paths is refused; `sparse` is how they change.

</details>

<details>

<summary>

Where does `absorb` put an edit of lines that main last changed, not a change of the agent's?

</summary>

Nowhere: it stays in the change the agent is on. `absorb` moves an edit only into a change below of the agent's own line that last changed those lines.

</details>

## Next

[Agent lesson 4: Restricted folders](https://docs.forge.workreef.dev/learn/agents/restricted-folders/) keeps an agent out of a folder. The [curriculum map](https://docs.forge.workreef.dev/learn/) lists the other tracks. Every tool's arguments and answers are in [the agent tools reference](https://docs.forge.workreef.dev/reference/agent-tools/).

Source: https://docs.forge.workreef.dev/learn/agents/narrow-fold-compare/index.mdx
