DEV Community

Cover image for Run AI coding agents in parallel with Jujutsu (jj) workspaces
Roi Dayan
Roi Dayan

Posted on Originally published at moontors.com Fully Autonomous

Run AI coding agents in parallel with Jujutsu (jj) workspaces

Two agents in one working copy step on each other: one runs the tests while the other is halfway through a refactor, and both commit each other's files. With Git the answer is a worktree per agent. With Jujutsu it's a workspace per agent, and since jj 0.46 those workspaces work for agents far better than they used to. Here's the whole job, from the first workspace to cleaning up, plus a hook pair that makes Claude Code create jj workspaces by itself. Every command here was run on jj 0.47.1 and Git 2.54 on November 30, 2026.

Why workspaces suit agents

A jj workspace is a second working copy of the same repository, with its own .jj folder linked to the main one, and its own change checked out (Working copy). For agents that gives you three things worktrees don't:

  • One graph for everyone. Every workspace's change shows in one jj log, marked with the workspace's name, so you see what each agent has done from wherever you are.
  • No branch to babysit. An agent's work is a jj change from the start. You name it with a bookmark only when it's ready to push.
  • Undo stays local. Since jj 0.46, jj undo refuses to undo an operation made in another workspace, so one agent's undo can't take back another's work (changelog).

The catch used to be Git. Agents know Git far better than jj, and they run git status and git diff all day. A second jj workspace had no .git, so those commands failed and the agent got lost. jj 0.46 fixed that: in a colocated repository, jj workspace add now creates a Git worktree alongside the workspace, so Git works in it too.

Before you start

  • A colocated repository, with .jj and .git side by side. jj git init and jj git clone colocate by default since jj 0.34. jj git colocation status tells you, and jj git colocation enable converts one that isn't.
  • jj 0.46 or later (jj --version), for colocated workspaces.
  • Git 2.42 or later (git --version), as jj workspace add uses git worktree add --orphan.

One workspace per agent

From your repository, make a workspace beside it, starting from your main line:

jj workspace add ../app-agent-a --name agent-a -r 'trunk()'
Enter fullscreen mode Exit fullscreen mode
Created Git worktree for the new workspace.
Created workspace in "../app-agent-a"
Working copy  (@) now at: mszxxpvo c64b18d1 (empty) (no description set)
Parent commit (@-)      : sskonkup 7cbbea2b main | First commit
Enter fullscreen mode Exit fullscreen mode

trunk() is your main bookmark on the remote, main@origin here; with no remote, use -r main. The workspace starts as a new, empty change on top of it. Then open the agent in that folder:

cd ../app-agent-a
claude        # or codex, gemini, opencode
Enter fullscreen mode Exit fullscreen mode

With its sandbox on, Codex lets the agent write only inside its own folder, and a jj command run in a workspace writes to the repository's .jj in your main checkout. So each jj command Codex runs there waits until you let it run outside the sandbox.

Ignored files don't come along: no .env, no node_modules, no build output. Copy what the agent needs to run your app, and install dependencies before you hand it the task:

cp ../app/.env .
Enter fullscreen mode Exit fullscreen mode

Repeat with agent-b, agent-c and so on, one folder each. Keep the folders outside your repository: a workspace inside it shows up as untracked files in the main one, unless its folder is ignored.

Watching them work

From any workspace, one command shows everyone's progress:

jj log
Enter fullscreen mode Exit fullscreen mode

Each agent's change carries its workspace's name, agent-a@, agent-b@, beside your own default@. jj workspace list names every workspace with its folder and change.

What you see is each agent's change as jj last recorded it. jj records a workspace's files only when a jj command runs in that folder, so an agent that edits files and runs nothing else shows an empty change from outside. To record its latest files now, from anywhere:

jj -R ../app-agent-a status
Enter fullscreen mode Exit fullscreen mode

When you change files in an agent's change from another workspace, say by squashing a fix into it, that agent's working copy goes stale, and its next jj command stops:

Error: The working copy is stale (not updated since operation f43af447b123).
Hint: Run `jj workspace update-stale` to update it.
Enter fullscreen mode Exit fullscreen mode

Run jj workspace update-stale in that workspace to bring its files up to date (Stale working copy). A reword alone doesn't make it stale. If you'd rather not interrupt an agent, leave its change alone until its turn ends.

Keeping the agent on jj

With colocated workspaces, the agent's Git reads just work: git status and git diff show what it changed, and git log shows its parent (HEAD is detached there, which is normal in a colocated repository). Even a git commit isn't lost. jj picks it up on its next command:

Reset the working copy parent to the new Git HEAD.
Enter fullscreen mode Exit fullscreen mode

The agent's commit becomes a jj change, with a fresh empty change on top. Still, it's cleaner when the agent uses jj for anything that writes. Tell it so in AGENTS.md, or CLAUDE.md for Claude Code:

## Version control

This repository uses Jujutsu (jj), colocated with Git. Use jj for anything that writes: `jj describe -m` to describe your change, `jj new` to start the next one, `jj bookmark create` to name one. Never run `git commit`, `git checkout`, `git stash`, `git reset` or `git rebase`. Reading with `git status`, `git diff` and `git log` is fine.
Enter fullscreen mode Exit fullscreen mode

For a hard stop, a Claude Code PreToolUse hook on Bash can block the Git commands that write and tell the agent the jj command instead (hooks).

Landing an agent's work

Once the agent's turn is done, land its change from any workspace by its name, agent-a@. Record its latest files first, describe it if the agent didn't, then name it and push:

jj -R ../app-agent-a status
jj describe agent-a@ -m "Add the login form"
jj bookmark create feat-a -r agent-a@
jj git push --bookmark feat-a
Enter fullscreen mode Exit fullscreen mode

Skip the first line and you can push the change as jj last saw it, which may be empty. Skip the description and jj refuses: Won't push commit … since it has no description. Or skip naming it: jj git push -c agent-a@ creates a bookmark called push- plus the change id, and pushes it. Open the pull request from that bookmark as usual.

A bookmark follows its change, so if the agent keeps editing, the bookmark moves with it. Before the agent's next task, give it a new change with jj -R ../app-agent-a new, so the one you pushed stays as it is.

Cleaning up

Two ways, both from your main workspace:

  • jj workspace forget agent-a stops tracking the workspace and, since jj 0.46, removes its Git worktree. The folder stays on disk, to look through or delete yourself.
  • jj workspace remove agent-a records any files jj hadn't yet in a change of its own on top, described "SNAPSHOT OF REMOVED WORKSPACE agent-a", then removes the Git worktree and the folder. So nothing the agent left behind is lost. Ignored files in the folder are deleted.

Let Claude Code make the workspaces

Claude Code can start a session in a worktree of its own with claude --worktree, and gives subagents with isolation: "worktree" and background sessions one too. By default that's a git worktree. A WorktreeCreate hook replaces it entirely, and a WorktreeRemove hook handles the cleanup (WorktreeCreate). This pair makes them jj workspaces.

.claude/hooks/jj-worktree-create.sh:

#!/usr/bin/env bash
# a jj workspace beside the repository, in place of a git worktree; reused when the name comes again
set -euo pipefail
name=$(jq -r .name)
root=$(jj workspace root)
dir="$root-${name//\//-}"
if [ ! -d "$dir" ]; then
  jj workspace add "$dir" -r 'coalesce(trunk() ~ root(), @-)' >&2
  if [ -f "$root/.env" ]; then cp "$root/.env" "$dir/"; fi
fi
echo "$dir"
Enter fullscreen mode Exit fullscreen mode

.claude/hooks/jj-worktree-remove.sh:

#!/usr/bin/env bash
# remove the workspace, run from the main one, so jj keeps its unrecorded files and deletes its folder
set -euo pipefail
path=$(jq -r .worktree_path)
main=$(jj -R "$path" workspace root --name default)
jj -R "$main" workspace remove "$(basename "$path")" >&2
Enter fullscreen mode Exit fullscreen mode

.claude/settings.json:

{
  "hooks": {
    "WorktreeCreate": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "bash \"${CLAUDE_PROJECT_DIR}/.claude/hooks/jj-worktree-create.sh\""
          }
        ]
      }
    ],
    "WorktreeRemove": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "bash \"${CLAUDE_PROJECT_DIR}/.claude/hooks/jj-worktree-remove.sh\""
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

They need jq. Commit the two scripts and the settings to your main line and push it, so every workspace made from trunk() has them; with no remote, a workspace starts from your current change's parent instead.

Claude Code sends the worktree's name as JSON, and reads the folder from the create hook's last line of output, so jj's own messages go to stderr. The workspace is named after its folder: your repository's folder name and the worktree's name, joined by -, with any / turned into -, so in app, feat/login becomes app-feat-login. Claude Code calls the create hook again when you reuse a name, so the hook hands back the folder it already made. It copies .env itself, as Claude Code's .worktreeinclude doesn't apply once a hook takes over. Fed Claude Code's input, with a space in the path, with a remote and without one, the pair made each workspace with its Git worktree, reused one when its name came again, and removed it, the agent's unsaved file kept in a "SNAPSHOT OF REMOVED WORKSPACE" change of its own.

Three things to know, all from Claude Code's hooks reference:

  • When a subagent or a workflow agent finishes, Claude Code keeps its worktree and doesn't run the remove hook. jj workspace list shows them, each named after its folder; remove those with jj workspace remove.
  • Keep the hooks in a settings file, not a plugin: a plugin's create hook doesn't run for claude --worktree --tmux.
  • Claude Code checks for changes with Git before removing a worktree. In a colocated workspace Git sees the agent's edits, so it asks you first, as it does for a git worktree.

Doing this in Moontors

Moontors is the app I build: one window for Claude Code, Codex, Gemini CLI, Grok Build and OpenCode, every session a live card on one board. Since 0.9.3 it makes, pushes and cleans up the workspaces for you in a jj repository colocated with Git. Keeping each agent on jj is still the AGENTS.md block's job.

  • A workspace per session. Switch on Run in a new worktree in Create session, and the session gets a jj workspace, made with jj workspace add under ~/.moontors/worktrees/ as a new change. A branch name you give becomes a bookmark on it. See In a Jujutsu repository.
  • Cleaning up asks first. Archiving the session asks what goes with the workspace and its bookmark. Moontors has jj take the workspace in first, so a commit made there with Git becomes a jj change too, then has jj forget it.
  • A Git view that runs jj. Commit is jj commit, Fetch all is jj git fetch, and Push pushes the bookmark with jj git push. A commit's menu adds Push as a new bookmark and Move main here. See Git and Files.

It needs your own jj 0.46 or later and Git 2.42 or later. It's free, with no Moontors account, on macOS, Windows and Linux.

Cheat sheet

You want to Run
Check the repository is colocated jj git colocation status
Give an agent a workspace jj workspace add ../app-agent-a --name agent-a -r 'trunk()'
See every agent's change jj log
List the workspaces jj workspace list
Fix a stale working copy jj workspace update-stale
Record an agent's latest files jj -R ../app-agent-a status
Describe an agent's change jj describe agent-a@ -m "…"
Name and push it jj bookmark create feat-a -r agent-a@, then jj git push --bookmark feat-a
Push without naming it jj git push -c agent-a@
Start the agent's next change jj -R ../app-agent-a new
Stop tracking, keep the folder jj workspace forget agent-a
Remove the workspace and its folder jj workspace remove agent-a

AI did the writing, I did the reading, and we both took credit.

Top comments (1)

Collapse
 
crisstiank profile image
Cristian K •

Useful walkthrough, especially the colocated Git worktree part, since agents reach for git status all day. One thing separate workspaces don't isolate is runtime state: two agents in different folders still share localhost ports, the same dev database and any shared cache directory. If each agent runs the app or the tests, it helps to give every workspace its own port and database name through its copied .env, otherwise you get the same kind of collision as a shared working copy, just one layer down.