Skip to content

Agent worktree isolation uses origin/main instead of current HEAD #1586

Description

@fipnooone

Summary

When spawning an agent with isolation: "worktree", OpenClaude may create the agent worktree from origin/<defaultBranch> instead of the parent session’s current branch or HEAD.

This is surprising because an isolated agent is expected to see the same committed project state as the parent session. Instead, the agent may see an older tree and miss files that exist on the active branch.

Steps to Reproduce

  1. Open OpenClaude in a git repository on a non-main branch.

  2. Make sure the current branch has committed files that do not exist on main / origin/main.

  3. Make sure origin/main exists locally, for example:

    git fetch origin main
  4. Spawn an agent with worktree isolation:

    Agent({
      isolation: "worktree",
      subagent_type: "general-purpose",
      prompt: "Report pwd, git HEAD, origin/main HEAD, and whether package.json exists."
    })
  5. Inspect the agent output or the created worktree.

Expected Behavior

The isolated agent worktree should be based on the parent session’s current HEAD.

If using origin/<defaultBranch> is intentional, that behavior should be documented and exposed clearly in the agent/worktree metadata.

Actual Behavior

The agent worktree is valid, but it is based on origin/main instead of the parent session’s current branch or HEAD.

Example evidence from a reproduction:

Parent session:

branch: feature-agent-worktree
HEAD:        7b414e28b2b9f1b2bda77eca913ebb0df27b47ce
origin/main: 536144ab143a3c6a0ef4b5facbb573e6b41fa45d

package.json exists in HEAD: yes
package.json exists in origin/main: no

Isolated agent:

pwd: /Users/fipnooone/empty/.claude/worktrees/agent-afae7144
git branch --show-current: worktree-agent-afae7144
git rev-parse HEAD: 536144ab143a3c6a0ef4b5facbb573e6b41fa45d
git rev-parse origin/main: 536144ab143a3c6a0ef4b5facbb573e6b41fa45d
git rev-parse main: 536144ab143a3c6a0ef4b5facbb573e6b41fa45d

package.json exists: no

As a result, the agent missed files that existed on the active parent branch.

Root Cause

The agent path calls createAgentWorktree(slug), which currently delegates to the shared getOrCreateWorktree(...) helper.

That helper prefers origin/<defaultBranch> when available:

const originRef = `origin/${defaultBranch}`
const originSha = gitDir
  ? await resolveRef(gitDir, `refs/remotes/origin/${defaultBranch}`)
  : null

if (originSha) {
  baseBranch = originRef
  baseSha = originSha
}

Then it creates the worktree from the selected base ref:

addArgs.push('-B', worktreeBranch, worktreePath, baseBranch)

This means agent worktree isolation inherits the default-branch worktree behavior instead of using the parent session’s current HEAD.

Environment

  • OpenClaude version: 0.18.0
  • OS: macOS
  • Shell: zsh

Suggested Fix

For Agent({ isolation: "worktree" }), create the isolated worktree from the parent session’s current HEAD by default.

Alternatively, if using origin/<defaultBranch> is intentional, document this behavior and expose the selected base ref in the agent/worktree metadata so users can understand why the agent is not seeing their active branch state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions