DEV Community

Cover image for Give Your Coding Agent Only Its Slice of the Monorepo: Sparse Worktrees in 5 Commands
Gabriel Anhaia
Gabriel Anhaia

Posted on Edited on AI-assisted

Give Your Coding Agent Only Its Slice of the Monorepo: Sparse Worktrees in 5 Commands


You ask the agent to fix proration in packages/billing. Before it opens a single billing file, it searches the repo for prorate. The matches come back from billing, from an old invoicing package, from a vendored SDK and from a test fixture three packages away. Each one is a file it may open, read and carry in context while it works out which prorate you meant.

That is what a full checkout gives an agent: every package in the monorepo, on disk, searchable. Claude Code on macOS and Linux searches with find and grep through its Bash tool, and when its Glob tool is turned on, results are capped at 100 files sorted by modification time. Either way, the search walks whatever is in the working tree.

So change what is in the working tree. Git can give each task its own folder with git worktree, and fill that folder with only the directories you name with git sparse-checkout. Anthropic took the idea all the way for its own monorepo. In its source-control write-up: "The working copy is optional, and sparse by design." You don't need a new version control system to get a slice. You need five git commands, and if you use Hermes IDE, one recipe file that runs them for every task.

The five commands

The example is a pnpm workspace at ~/code/acme, with the agent's task in packages/billing:

cd ~/code/acme
git worktree add --no-checkout \
  -b billing/fix-proration ../billing-fix main
cd ../billing-fix
git sparse-checkout set --cone packages/billing
git checkout billing/fix-proration
git sparse-checkout list
git rev-parse --git-path info/sparse-checkout
Enter fullscreen mode Exit fullscreen mode

What each one does:

  1. worktree add --no-checkout creates the folder and the branch but writes no files. The git docs give this exact reason for the flag: to "suppress checkout in order to make customizations, such as configuring sparse-checkout."
  2. sparse-checkout set --cone packages/billing picks the slice. Cone mode is already the default, and --cone only says it out loud. You get every file at the repo root, every file directly under packages/, and everything at any depth under packages/billing/.
  3. checkout fills the folder with those paths and nothing else. GitHub's sparse-checkout walkthrough uses the same order on a clone: no checkout, set the cone, then check out.
  4. sparse-checkout list should print the one directory you set.
  5. rev-parse --git-path shows where the patterns live: under the main repo's .git/worktrees/billing-fix/, not in the shared .git/info/. Git's path table marks info/sparse-checkout as a per-worktree file, and set switches the repo to worktree-specific config so that a change in one worktree "does not alter the sparse-checkout settings in other worktrees."

Your main checkout stays full. A second task in packages/search gets its own worktree and its own cone, and neither one changes the other's patterns.

The switch to worktree-specific config turns on extensions.worktreeConfig, and the worktree docs warn that "Older Git versions will refuse to access repositories with this extension." A tool that bundles an old git may stop working on this repo.

A tall filing cabinet of identical closed drawers with one drawer pulled out and lit, a small robot silhouette working inside that drawer alone
One worktree, one drawer: the agent only finds what is in its slice.

Count it before and after

Measure on your own repo before you trust any of this. ripgrep's --files prints the files it would search without searching them, and it skips gitignored and hidden files by default, so the count is close to what a search tool walks:

rg --files ~/code/acme | wc -l
rg --files ~/code/billing-fix | wc -l
Enter fullscreen mode Exit fullscreen mode

Then time one search in each tree with hyperfine. Pick a word that occurs in both; import will do in a TypeScript repo:

hyperfine --warmup 3 \
  'rg -l import ~/code/acme' \
  'rg -l import ~/code/billing-fix'
Enter fullscreen mode Exit fullscreen mode

The first command is the reference, and hyperfine reports the second relative to it. I haven't run this on your repo, and the ratio depends on how big the slice is next to everything else. For a sense of scale, the example repo in GitHub's walkthrough drops to 37 files with only the root in the cone, and to 62 with client/android added.

The time is the smaller win. The bigger one is that searches return fewer matches, so there are fewer wrong files for the agent to open and read.

Trap 1: the root is shared, and it is always in the slice

Cone mode always includes the files at the repository root. Most of the time that is what you want: package.json, the lockfile, pnpm-workspace.yaml and tsconfig.base.json sit there, and the slice needs them.

Two things go wrong around it. First, shared config that lives in a subdirectory is not root. If your lint rules sit in tools/eslint-config/ or your test setup in config/jest/, they are missing from the slice, and the agent's first lint run fails. Add them without dropping what you have. set replaces the list and add extends it:

git sparse-checkout add tools/eslint-config
Enter fullscreen mode Exit fullscreen mode

Second, the root belongs to every package. An agent in the billing slice that "fixes" a type error by loosening tsconfig.base.json has changed the build for packages it can't see. List the root files a branch touched before you review anything else:

git diff --name-only main...HEAD | grep -v /
Enter fullscreen mode Exit fullscreen mode

Paths with no / in them are root files. If that prints anything, run the full CI suite on the branch, because the slice hid the blast radius.

Trap 2: imports from packages outside the cone

packages/billing imports @acme/money from packages/money. In the slice, packages/money is not on disk, so the type check fails on a missing module. An agent told to make the tests pass can then do the wrong thing well: stub the module, or rewrite the function it couldn't find.

Build the cone from the dependency graph, not from memory. Run this in the full checkout, where pnpm can read every package's manifest:

cd ~/code/acme
pnpm ls --filter "@acme/billing..." --depth -1 --parseable \
  | sed "s|^$PWD/||" > ../billing-fix.slice
git -C ../billing-fix sparse-checkout set --cone --stdin \
  < ../billing-fix.slice
Enter fullscreen mode Exit fullscreen mode

In pnpm's filter syntax, <package>... selects the package and its dependencies, direct and indirect. With --depth -1 the list shows workspace projects only, and --parseable prints their directories. sed strips the absolute prefix, and set --stdin reads one directory per line. Open the .slice file before you hand it to git: every line should look like packages/money.

The other direction needs a different tool. Callers of billing live in other packages, so if the agent changes a function's signature, the slice can't show what breaks. git grep against a commit reads from the object store, not from the folder, so it searches every package from inside the slice:

git grep -n "prorate(" HEAD -- packages/
Enter fullscreen mode Exit fullscreen mode

Make every Hermes task start sparse

I build Hermes IDE, so weigh this section with that in mind. In Hermes, ⌘N (Ctrl+Shift+N on Windows and Linux) starts each task on its own hermes/ branch in its own worktree, with the task as the agent's first prompt. The agent starts right away, so the slice has to be in place before it does.

A worktree recipe handles that. Hermes reads .hermes/worktree.toml from your repo. Per the recipe docs, copy brings git-ignored files over from your project folder, setup runs in order inside the new worktree, and the session starts once setup is done:

# .hermes/worktree.toml
copy = [".slice"]
setup = [
  "git sparse-checkout set --cone --stdin < .slice",
  "pnpm install",
]
Enter fullscreen mode Exit fullscreen mode

.slice is the same one-directory-per-line file as above. It lives in the project folder and git must ignore it, because copy refuses tracked files:

echo ".slice" >> .gitignore
printf 'packages/billing\npackages/money\n' > .slice
Enter fullscreen mode Exit fullscreen mode

Commit .gitignore and the recipe. Before you press ⌘N for a task in another package, rewrite .slice, since it is copied when the task starts. The first time Hermes sees the recipe, and again whenever it changes, it shows you the commands and waits until you choose Run setup.

The Hermes New task sheet with a refund-endpoint task typed in, Claude Code selected, the orders-api project, a new hermes/ worktree, Accept edits, and a preview of the claude command Hermes will run
The ⌘N New task sheet in Hermes 2.3.4: the task, Claude Code, the orders-api project, a new hermes/ worktree, Accept edits, model and effort, and the "Hermes will run" preview of the claude command.

Swap pnpm install for your own install command, and run it once by hand in a slice first. If your package manager objects to workspace packages that aren't on disk, you want to find out before an agent does.

Three more things about this route. The docs call the new worktree "a fresh checkout," and I'm treating it as a full one: setup trims it, so the other packages' files get written once and then removed. On a repo where writing them once already hurts, use the plain-git route above. Next, set also deletes ignored files in any directory left with only ignored files, so anything built or copied under other packages goes too. And I haven't tried this recipe on Windows.

To see what each task ended up with, open the Review Desk's Worktrees tab in Hermes 2.3.4. It shows the Hermes worktrees with their branch, task, age and size on disk. A sliced task should sit well under one that holds the whole repo.

The Review Desk Worktrees tab listing Hermes worktrees for orders-api, web-app and orders-types with their branches, tasks, ages and disk sizes, plus free disk space
The Review Desk's Worktrees tab: one Hermes worktree each for orders-api, web-app and orders-types, with each one's hermes/ branch, task, age and disk size, and the free disk space.

Hermes is free, source-available under BSL 1.1, and not affiliated with Nous Research, Anthropic, OpenAI or any agent vendor.

What a sparse worktree is not

It is not a security boundary. Every object is still in the shared repository, so the agent can run git show HEAD:packages/search/src/index.ts or git sparse-checkout disable whenever it likes. If a package must stay off-limits, use OS permissions or a sandbox. Claude Code's deny permission rules can help, but a Read deny rule on its own doesn't stop git show through Bash, so pair it with a Bash rule or a sandbox. Raylabs' post on the same technique adds a check on which paths a branch changed.

It doesn't hold through every git operation either. The sparse-checkout docs note that merge and rebase can bring paths outside the cone back into the folder, and git sparse-checkout reapply trims them again. The same page still marks the command experimental, with behavior that "will likely change in the future."

Before you hand the next task to an agent, run the two rg --files counts from earlier on your own repo. The second number is how many files the agent can find on disk.


If this was useful

A slice should mean fewer files read and fewer tokens spent per task, but you only know that if you can see what the agent read and what each run cost. The LLM Observability Pocket Guide is my short guide to picking the tracing and eval tools that show you that, so you can compare a full-checkout run with a sliced one on numbers instead of a hunch.

Hermes IDE runs on macOS, Windows and Linux: download it or read the source on GitHub.

LLM Observability Pocket Guide: Picking the Right Tracing & Evals Tools for Your Team

Top comments (0)