Skip to content
mentoor

Topic 3 of 5 · 6 min read

Preventing conflicting edits between parallel agents

Goal: Split a change across parallel agents into non-overlapping file areas with separate worktrees, and find the overlap in a flawed split.

Parallel agents stay out of each other's way when every file has exactly one owner and every agent works in its own worktree on its own branch. Prompts that say "be careful" do not prevent conflicts. Structure does.

Check yourself · 1 of 3

What is the main benefit of giving each parallel agent its own git worktree?

Choose one answer

Why it matters

Two agents in one checkout see each other's half-written files, and a typecheck then fails on code that is not yours. Two agents on different branches that edit the same file produce a merge conflict, which costs more to resolve than the parallel work saved.

How to split work

  1. List the files the change touches. Read the code or ask an agent to map it first.
  2. Cut along file boundaries. Group by package, folder or layer, such as api/, web/, docs/. One owner per file.
  3. Put shared files in one hand. Lock files, config, shared types, changelog and index files are conflict magnets. Assign each to a single agent, or leave them to the final integration step.
  4. Give each agent a worktree. git worktree add ../feat-api -b feat/api creates a second checkout of the same repository with its own branch and working files, so agents do not share uncommitted changes. The worktree documentation covers the details.
  5. State the boundary in the brief. "You own api/**. Do not edit anything else. If you need a change elsewhere, write it in your handoff."
  6. Decide merge order. Merge the layer others depend on first (shared types, then API, then web), and rebase the rest on it.
  7. Integrate once. One session merges, resolves conflicts and runs the full checks.

Before and after

Weak split: agent A "does the backend", agent B "does the frontend". Both need types/order.ts, both edit it, and the branches conflict.

Better:

Agent A: owns api/**, types/order.ts. Merges first.
Agent B: owns web/**. Reads types/order.ts, never edits it.
Agent C: owns docs/**.
Integration (lead): package.json, CHANGELOG.md, final checks.

What goes wrong

  • Hidden shared files. The lockfile, a barrel index.ts or a generated file is touched by everyone.
  • Same checkout. Agents run builds that read each other's unfinished edits.
  • Scope creep. An agent "tidies" a file it does not own.
  • Late integration. Merging everything at the end finds all conflicts at once. Merge early and in order.

Examples

Creating the worktrees

git worktree add ../shop-api -b feat/api
git worktree add ../shop-web -b feat/web
git worktree list

Each agent starts in its own directory. Both are checkouts of the same repository, so commits are shared, but working files and branches are not.

Finding the overlap

A plan gives agent A src/orders/** and agent B src/checkout/**. Both tasks add a field to src/orders/types.ts. The overlap is one file, and the fix is to give types.ts to agent A and tell agent B to wait for that commit or report what it needs.

Key points

  • Give each file one owner; cut the work along folder or layer boundaries.
  • Use a separate worktree and branch per agent so uncommitted edits stay private.
  • Shared files such as lock files, changelogs, shared types and index files need a single owner or a final integration step.
  • Put the file boundary in each agent's brief, with a rule to report needs outside it.
  • Decide a merge order up front: dependencies first, then dependents.

Common mistakes

  • Splitting by task name instead of by file, so two tasks touch the same files.
  • Forgetting generated files, lock files and index files in the ownership list.
  • Letting agents share one working directory and typecheck against each other's unfinished edits.
  • Merging all branches at the end instead of in dependency order.

Check yourself · 2 of 3

Two agents are split as "backend" and "frontend". Both must add a field to shared/order-types.ts. What is the best fix?

Choose one answer

Check yourself · 3 of 3

Three branches are ready: shared types, the API that uses them, and the web app that calls the API. In what order should they merge?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic