DEV Community

BuildItOmi
BuildItOmi

Posted on Fully Autonomous

How we made an agentic CLI with subagents that edit in parallel (git worktrees + a three-way merge)

Coding agents are good at one thing at a time. Ask for "an API route and its tests" and most agents write the route, then the tests, one after the other, in the same folder you're working in.

We wanted Omi CLI to hand independent pieces to helpers that work at the same time, without the helpers stepping on each other or on you. This post is how we did it in plain Node.js with nothing but git. It shipped in Omi CLI 0.5.0.

The problem with "just run two agents"

Running two agents in the same directory breaks quickly:

  • Both read package.json, both edit it, the second write wins.
  • One runs npm test while the other is halfway through an edit.
  • You are editing files too, and you don't want any of it landing without review.

So every helper needs its own copy of the project, and there needs to be a careful way back.

Step 1: a worktree that starts from now, not from HEAD

git worktree add gives you a second working directory on its own branch, sharing the same .git. Cheap, fast, built in.

The catch: a worktree starts from a commit. If you have uncommitted edits (you almost always do mid-task), a helper starting from HEAD sees a stale project. We needed "the project as it is right now" without touching your working tree or index.

git stash create does exactly that. It builds a stash commit from your uncommitted changes and prints its hash, but doesn't store it or reset anything:

const snap = git(top, ["stash", "create"]).stdout.trim() || "HEAD";
gitOk(top, ["worktree", "add", "--quiet", "-b", branch, dir, snap]);
Enter fullscreen mode Exit fullscreen mode

If there's nothing to stash it prints nothing, so we fall back to HEAD.

That still misses brand-new files you haven't git added. We copy those in, skipping anything ignored:

const untracked = git(top, ["ls-files", "--others", "--exclude-standard", "-z"])
  .stdout.split("\0")
  .filter((f) => f && !f.startsWith(".omi/worktrees/"));
Enter fullscreen mode Exit fullscreen mode

Each helper lives in .omi/worktrees/sub-<name>-<id> on a branch omi/sub-<name>-<id>. To keep those folders out of your git status, we append .omi/worktrees/ to .git/info/exclude (your .gitignore is never touched).

Two hard requirements fall out of this, and we say them in the error message instead of failing weirdly: the project must be a git repo, and it must have at least one commit.

Step 2: let the helpers work

A helper is the same agent loop as the main one, with a narrower system prompt (its task and type) and a restricted tool list. It edits and runs commands inside its own worktree, so npm test in one helper can't see half-written files from another.

Helper types are Markdown files with front matter: name, description, tools, model, and the body is its instructions. We read .omi/agents/*.md, ~/.omi/agents/*.md, and also .claude/agents/*.md, so if you've already written agent definitions for Claude Code they work unchanged.

When a helper finishes, it commits on its branch with a fixed identity (so it never needs your git config or GPG key):

const ID = ["-c", "user.name=Omi CLI", "-c", "user.email=omi-cli@users.noreply.builditomi.com",
            "-c", "commit.gpgsign=false"];
git(h.dir, ["add", "-A"]);
gitOk(h.dir, [...ID, "commit", "--quiet", "--no-verify", "-m", `Omi subagent: ${message}`]);
Enter fullscreen mode Exit fullscreen mode

Then git diff --name-status --no-renames <base> <head> tells us which files it added, changed or deleted.

Step 3: the way back is not git merge

This is the part we spent the most time on. The obvious move is git merge omi/sub-.... We don't, for two reasons:

  1. You haven't approved anything yet. In Omi, every edit goes through a diff you approve. A helper's work shouldn't skip that just because it came from a branch.
  2. Your working tree is dirty. You kept working while the helper ran. A branch merge wants a clean tree or a commit, and you don't want Omi committing for you.

So merging is file by file. For each changed file we have three versions: mine (the file on your disk now), base (what the helper started from) and theirs (what the helper wrote). Most cases need no merge at all:

export function mergeText(mine, base, theirs) {
  if (mine === base) return { text: theirs, conflict: false };            // you didn't touch it
  if (mine === theirs || base === theirs) return { text: mine, conflict: false };
  // both changed it: real three-way merge
  const r = spawnSync("git", ["merge-file", "-p", "-L", "yours", "-L", "start", "-L", "subagent",
                               mineFile, baseFile, theirsFile], { encoding: "utf8" });
  return { text: r.stdout, conflict: r.status > 0 };
}
Enter fullscreen mode Exit fullscreen mode

git merge-file -p does a three-way merge of three plain files and prints the result, with conflict markers labelled yours / start / subagent. The exit status is the number of conflicts. No branches, no index, no commit.

The merged text then goes through the same approveEdit path as any other edit, so you see a diff and say yes or no, and it's recorded as a checkpoint, which means /undo and /rewind cover it.

The edge cases we decided on, and report rather than hide:

  • Binary file changed on both sides: left as yours, reported as a conflict.
  • Helper deleted a file you changed: kept yours, reported.
  • Helper deleted a file you didn't touch: goes through the normal delete approval.
  • You decline a file: recorded as declined; the rest still merge.

At the end the helper's worktree and branch are removed (or kept, if you ask).

What it looks like

$ omi "add a billing API route and its tests, in parallel"
  ⇉ subagent api-route  own copy on omi/sub-api-route-…
  ⇉ subagent tests  own copy on omi/sub-tests-…
Enter fullscreen mode Exit fullscreen mode

/agents lists the helpers and their status.

What we'd tell you if you build this yourself

  • git stash create is the trick that makes worktrees usable for agents mid-task.
  • Don't merge branches into a dirty tree. Merge files, and only after someone approves.
  • git merge-file is underrated: a full three-way merge for any three files, with labels.
  • Write the failure modes into the error messages ("needs at least one commit") instead of the docs.

The whole module is about 300 lines with no dependencies beyond Node and git. You can read it in the npm package: npm pack @builditomi/cli and open src/subagents.js (the licence lets you read the source; it is not open source).

Try it

npm install -g @builditomi/cli
omi
Enter fullscreen mode Exit fullscreen mode

Local models (omi -m ollama:<model>) need no account. More in the docs: cli.builditomi.com/docs. If you try subagents on a real repo, we'd like to hear where the merge step surprised you.

Part 2 (coming next): how the same CLI drives a real Chrome with zero dependencies.

Top comments (0)