Skip to content

Worktree-aware agent execution: add cwd to run_agent() #53

@chanakyav

Description

@chanakyav

Parent Epic

Part of #52 — Parallel stacked workstreams

Summary

Add a cwd parameter to run_agent() so the copilot agent runs inside a git worktree directory instead of the repo root. Update all prompts to stop instructing the agent to create branches (the worktree already has the branch checked out).

Context

Today, run_agent() in agent.py does not pass cwd to subprocess.Popen(), so the agent inherits the orchestrator's working directory. With git worktrees, each task gets its own directory — the agent must run inside it.

Additionally, prompts currently tell the agent to git checkout -b <branch>, which is wrong in a worktree context (the branch already exists).

Changes

agent.py

  • Add cwd parameter to run_agent() signature
  • Pass cwd=cwd to subprocess.Popen()
  • Default to None (current behavior) for backward compatibility

orchestrator.py

  • BaseOrchestrator stores worktree path (from task record)
  • Pass cwd=self.worktree_path to run_agent() calls in _run_agent_with_retry()
  • _get_workspace_dirs() updated to scan from worktree path, not os.getcwd()
  • Fix os.getcwd() usage in _do_resolve_comments() for .autopilot-fix-summary.json path

prompts.py

  • implement_prompt(): Remove git checkout -b instruction when worktree is active; add You are already on branch <branch> context
  • implement_on_existing_branch_prompt(): Same adjustment
  • plan_and_implement_prompt(): Same adjustment
  • Replace hardcoded git diff main with git diff <base_branch> (uses get_default_branch())

persistence.py

  • Add worktree_path column to tasks table (nullable TEXT)
  • Schema version bump + migration

Depends On

Testing

  • run_agent() with cwd parameter passes it to subprocess
  • Prompts omit git checkout -b when worktree context is set
  • Orchestrator reads worktree path from task record
  • .autopilot-fix-summary.json read from worktree path, not os.getcwd()

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions