Skip to main content

Module worktree

Module worktree 

Source
Expand description

Git worktree isolation for subagents (Claude-Code parity).

PMAT-CODE-WORKTREE-001: Claude Code’s Agent tool accepts an isolation: "worktree" flag. When set, the runtime creates a fresh git worktree checked out to a new branch, the agent runs against that working tree, and at completion:

  • if the worktree is clean (no changes), it is removed and the branch is deleted — the caller sees no artifact.
  • if the worktree is dirty, both the worktree path and the branch are returned so the caller can inspect or merge.

This module ships the primitives (WorktreeSession::create, .is_dirty(), .auto_close_if_clean(), .keep()) so spawn-tool call sites can opt in.

§Example

use aprender_orchestrate::agent::worktree::WorktreeSession;

let repo = std::path::Path::new(".");
let session = WorktreeSession::create(repo, "agent/xyz")?;
// ...agent runs, edits files under session.path()...
if session.is_dirty()? {
    let kept = session.keep()?;  // returns (path, branch)
    println!("worktree kept at {} on branch {}", kept.0.display(), kept.1);
} else {
    session.auto_close()?;  // removes worktree + deletes branch
}

Structs§

WorktreeSession
Active git worktree session scoped to a subagent.

Enums§

WorktreeError
Errors arising from worktree lifecycle operations.