Concurrency & isolation workflow Fact-checked · 2026-11-23

Parallel Coding Agent Stack: Git Worktrees, Memory Layers and Guardrails

When running 2–4 coding agents (Claude Code, Codex CLI, OpenCode) at once, the biggest failure mode is sessions overwriting the same workspace or colliding on ports and databases. Combine isolated Git worktrees, a session board, permission deny-lists and scoped memory to keep parallel work safe.

Setup time
~15 minutes on an existing Git repo
Risk posture
Medium-high autonomy · Contained via per-task worktrees & host approval gates
Cost profile
Model usage billed via your coding host plan/keys; worktree & memory layers are open source
Use this stack when
  • Advancing independent tracks simultaneously—such as UI polish, API fixes and test coverage—without waiting serially on one agent loop.
  • Reviewing clean per-task Git diffs and test logs side by side before anything lands on main.
Skip this stack if
  • Tightly coupled refactors touching the same shared files, where parallel branches mostly create merge conflicts.
  • Tiny one-off scripts without version control or automated tests.

Architecture & flow

Main repo & task splitWorktree A · Coding hostWorktree B · Coding hostHuman diff review & merge
Each parallel coding session gets its own Git worktree and permission gate before merging back

Stack layers & catalog tools

Layer 1 · Primary coding host

Source: Codex approvals & security docs ↗

Reads the codebase, edits files and runs tests while enforcing command approvals and OS/container sandboxing.

Wiring & protocols: Configured via AGENTS.md / CLAUDE.md, lifecycle hooks (PreToolUse), and CLI permission flags.

Pick one primary host or pair a read-only Codex review session with OpenCode/Claude Code implementation.

Layer 3 · Cross-session memory

Source: Agent memory layers ranking

Prevents re-explaining architecture conventions in every fresh worktree without bloating the prompt window.

Wiring & protocols: Captures observations via hooks and retrieves snippets on demand via MCP tools.

Keeping concise build/test instructions in AGENTS.md already covers most single-repo needs.

Find companion SKILL.md packs on FollowSkills.com → Discover compatible MCP servers on FollowMCP.com →

Wiring & hardening steps

1. Isolate every session in its own Git worktree before launching agents

Never point two active coding sessions at the same working tree. Give each task a clean branch and directory.

git worktree add ../repo-auth -b feat/auth
git worktree add ../repo-tests -b test/coverage

2. Lock down secret reads and destructive commands in host config

Deny .env reads and destructive shell commands in .claude/settings.json, or switch OpenCode from default allow to ask via opencode.json.

{
  "permissions": {
    "deny": ["Read(./.env)", "Read(./.env.*)", "Bash(rm -rf *)"]
  }
}

3. Isolate test ports and temporary databases across worktrees

Parallel test runs fail flakily when they share a fixed port or SQLite file. Document per-worktree env overrides in AGENTS.md.

Common traps & fixes

Trap Two worktrees running test suites against the same local port or SQLite file.
Fix Use ephemeral ports and per-worktree temp paths in tests, or isolate the whole runtime in containers (container-use).
Trap Turning on --dangerously-skip-permissions across all concurrent tabs to save clicks.
Fix Split attention raises mistake rates—keep approval gates on host runs and reserve auto-approve for disposable containers.
Trap Stacking multiple memory plugins that dump raw session transcripts into every prompt.
Fix Run one on-demand retrieval layer (MCP search/fetch) and exclude secrets from capture.

Pre-flight safety checklist

  • Clean main branch; every agent runs inside its own worktree and feature branch.
  • Secret files (.env*) and destructive shell commands explicitly denied in host settings.
  • Test commands use isolated ports and temporary databases per worktree.
  • Verification commands (pytest, npm test, linters) documented in AGENTS.md / CLAUDE.md.
  • Every merged branch passes a human git diff review first.

FAQ

How many parallel sessions are practical at once?

Most developers find 2–3 independent tasks optimal. Beyond 4 concurrent sessions, human code-review bandwidth and merge conflicts usually become the bottleneck.

Does a Git worktree provide OS-level sandboxing?

No. A worktree only separates Git working directories; processes still share your user account, home folder and network unless wrapped in an OS sandbox or Docker container.

Can Claude Code and Codex share one project rules file?

Yes—keep canonical build and safety rules in AGENTS.md and reference it from CLAUDE.md so instructions stay in sync.

Agent Workflow Stacks

Was this page helpful?