跳到正文
DEV Community· Mac·· 57 分鐘前AI 評分62

並行編碼智能體如何避免程式碼庫衝突

Parallel Coding Agents Without Repository Collisions

AI 導讀

這篇指南提出一套並行編碼智能體協作流程,以獨立工作區、明確任務所有權和有序合併減少程式碼庫衝突。建議每個智能體使用獨立 Git worktree 和分支,預先劃分檔案所有權,並分開設定連接埠、快取和資料庫。合併時先整合共享介面等變更,再更新其他分支並重跑測試;清理前須保存或檢查未提交的工作。

正文

Three coding agents can finish three tasks at once. They can also overwrite the same file, reuse one port, or merge against old code. Speed only helps when each agent has a clear place to work and a safe path back to the main branch.

The goal is not “more agents.” The goal is useful work that can merge with less waiting and less repair.

Give every agent an isolated workspace

A branch is not enough when several processes use the same folder. One agent may change files while another is testing them. Build output, lock files, caches, and local databases can also collide.

Use one Git worktree per coding agent. A worktree gives each agent its own folder and branch while sharing the same Git history.

# Create three isolated workspaces from the current repository
git worktree add ../repo-api -b agent/api
git worktree add ../repo-ui -b agent/ui
git worktree add ../repo-tests -b agent/tests

# Check every active workspace before starting agents
git worktree list

The VibeFlow CLI can create this shape with vibeflow launch --worktree --new-branch --provider codex. Its official README also provides commands to list sessions, inspect worktrees, and check for conflicts.

Claim work before changing code

Isolation stops folder collisions. It does not stop two agents from solving the same ticket or editing the same shared file.

Use an atomic task claim. The first agent that claims a work item owns it until completion, release, or timeout. Record the session, branch, working directory, commit, and last heartbeat.

VibeFlow agent sessions follow this pattern: initialize, poll, claim, execute, commit, and complete. Code-changing personas also use one live session per branch. A second session gets a branch_locked result instead of racing the first one.

Control What it prevents Evidence to keep
Worktree per agent Files changing under another process Worktree path and branch
Atomic task claim Duplicate work Work item and session ID
Branch lock Two writers on one branch Lock owner and heartbeat
File ownership note Shared-file surprises Planned file list
Ordered merge queue Stale merges Tested commit and merge order

Plan file ownership before agents start

Ask each agent to list the files it expects to change. Reject or split plans that overlap on high-risk files such as dependency locks, database migrations, generated clients, root configuration, and shared types.

A simple plan might be:

  1. API agent owns services/api/**.
  2. UI agent owns apps/web/**.
  3. Test agent owns tests/e2e/**.
  4. One named integrator owns package-lock.json, migrations, and shared schemas.

File ownership is a planning rule, not a security boundary. Agents still need tests and review.

Separate ports, caches, and generated output

Worktrees can still collide through services outside Git. Give each workspace its own port range, cache path, temporary directory, database name, and generated-output folder.

export AGENT_SLOT=2
export PORT=$((3000 + AGENT_SLOT))
export TEST_DB="app_test_${AGENT_SLOT}"
export CACHE_DIR=".cache/agent-${AGENT_SLOT}"
npm test

Do not share a writable local database between agents. Do not let two agents regenerate the same checked-in file. If generation is required, assign it to the integrator after upstream branches merge.

Merge in dependency order

Agents should not all merge when they finish. Merge the branch that changes shared contracts first. Rebase or update the remaining branches, then rerun their checks.

Use this order:

  1. Merge schema, interface, or shared-library changes.
  2. Update dependent branches to the tested main commit.
  3. Run focused tests in each branch.
  4. Merge application changes.
  5. Run the full integration suite on the combined commit.
  6. Keep one rollback point and one owner for the release.

The commit that passed should be the commit that ships. If the merge creates a new commit, run the required checks again.

Stop stale or unsafe sessions

A heartbeat shows that a process is alive. It does not prove useful progress. Set limits for task time, retry count, tool calls, and cost. Release a claim when the heartbeat expires. Let a human terminate a live but stuck session after checking its logs and uncommitted diff.

Before cleanup, save or review uncommitted work. Automatic worktree removal can delete changes that were never committed.

A safe three-agent flow

Start with three tasks that touch different areas. Create three worktrees and branches. Claim each task. Review planned files. Run agents with separate ports and caches. Merge the shared contract first. Update the other branches. Run focused and full tests. Then release the exact tested artifact.

VibeFlow coordinates agent roles, work claims, security review, QA, and Git records. Its agent-session controls add branch locks and worktree-aware ownership. Teams can also use Vera as an independent review step before merge.

Parallel work becomes reliable when ownership is visible. Give every agent one task, one workspace, one branch, and one merge path.

來源:DEV Community · dev.to