Skip to content

[FEATURE]: Per-agent isolation: worktree config field for automatic subagent worktree provisioning #34216

Closed as not planned

Description

@Ryan-AI-Studios
  • I have verified this feature I'm about to request hasn't been suggested before.

Summary

Add a per-agent isolation config field (mirroring Claude Code's isolation: worktree subagent frontmatter) that makes child sessions spawned by that agent automatically provision a git worktree and run inside it, with automatic cleanup on session end.

This is the narrow, surgical version of the broader ask tracked in #17994 ("multi-agent orchestration in isolated workspaces"). #17994 covers the full conductor/orchestration surface; this issue is specifically about the one primitive that makes conductor-style workflows safe by default: a single config field that flips a subagent from "runs in parent's directory" to "runs in its own worktree."

Problem

Today, Session.create with parentID inherits the parent's directory from InstanceState.context. Child sessions run in the parent's working directory. The AGENTS.md guidance ("Parallel code edits conflict (no native worktree isolation). Sequence them, or git worktree add per worker.") is a workaround for exactly this gap.

OpenCode already ships a built-in Worktree.Service (packages/opencode/src/worktree/index.ts) with create / remove / list / reset, including Windows-specific retry handling. The plumbing exists; it's just not wired to the subagent spawn path.

Proposed

Add an isolation field to the agent config schema (both opencode.json agent block and .opencode/agents/*.md frontmatter):

{
  "agent": {
    "core-engineer": {
      "mode": "subagent",
      "isolation": "worktree"
    }
  }
}
# .opencode/agents/core-engineer.md
---
mode: subagent
isolation: worktree
---

Behavior when isolation: "worktree" is set on an agent and that agent is spawned as a child via the task tool:

  1. Session.create detects parentID + child agent's isolation === "worktree".
  2. Calls Worktree.create({ name: <slug from session/agent/track> }) to provision a worktree.
  3. Sets the child session's directory to the worktree's directory (instead of inheriting the parent's).
  4. On child session end/idle with no changes, calls Worktree.remove for auto-cleanup (Claude Code's "auto-removed if the subagent makes no changes" behavior).
  5. On child session end with changes, leaves the worktree in place for the parent to merge/inspect.

Default: isolation unset -> current behavior (child inherits parent's directory). No breaking change.

Storage location

Two options, both consistent with existing OpenCode conventions:

  1. Default (matches Worktree.Service today): ~/.local/share/opencode/worktree/<project-id>/<name>/ -- outside the repo, already the path the built-in service uses.
  2. Optional repo-local mode: .agents/worktrees/<name>/ at the project root. This mirrors Claude Code's .claude/worktrees/<name>/ location, and OpenCode already recognizes .agents/ as a cross-tool-compatible directory -- packages/opencode/src/skill/index.ts scans .agents/skills/**/SKILL.md (and ~/.agents/skills/ globally) as an external skill source, alongside .claude/. Extending the same .agents/ convention to worktrees keeps worktree state out of the OpenCode-specific .opencode/ namespace and aligns with the cross-tool agent ecosystem (Codex, Cursor, Copilot, etc.) that already use .agents/ for shared agent configuration.

A config flag could select between them, e.g. worktree.location: "data" | "project", defaulting to "data". This is secondary to the isolation field itself but worth deciding in the same pass.

Note: .agents/ is not currently in the agent-config resolution path (agents load from .opencode/agents/ and ~/.config/opencode/agents/), and the existing Worktree.Service stores outside the repo. Using .agents/worktrees/ for the repo-local mode would be a new use of an already-recognized directory, not a change to how agents or worktrees are loaded today.

Why this shape

What this does not do

Cross-references

Activity

  1. abiezerm commented on Jul 1, 2026

    @abiezerm

    This is a MUST nowadays.

  2. github-actions commented on Aug 31, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions