Skip to content

Automatic git worktree management for agent tasks #100534

Description

@steipete

Problem

OpenClaw has no managed way to run agent tasks in isolated git checkouts. Agents work directly in the user's checkout, so parallel tasks on one repo collide, and any temporary checkouts an operator creates by hand are never cleaned up. Every major coding-agent product (Claude Code, OpenAI Codex, Cursor, Copilot, Hermes) has converged on automatic git-worktree management: per-task worktrees, declarative provisioning of gitignored files, and automatic loss-proof cleanup. Workboard already models workspace.kind: "worktree" on automation cards, but nothing materializes it.

Proposed solution

Core managed-worktree service with the industry-standard lifecycle, adapted to OpenClaw's architecture rules (SQLite-first state, additive gateway protocol, plugin-SDK seams):

  • Centralized layout: <state-dir>/worktrees/<repo-fingerprint>/<name>, branch openclaw/<name> per task, based off the fetched remote default branch (offline fallback to local HEAD).
  • Provisioning: .worktreeinclude (same semantics as Claude Code/Codex — copy selected gitignored+untracked files), plus an optional executable .openclaw/worktree-setup.sh repo hook.
  • Registry in the shared state DB; git-native activity locks (openclaw pid=<pid>) so GC can distinguish live, dead, and foreign owners.
  • Snapshot-before-delete: every removal first commits the full tree (untracked included, gitignored excluded) to refs/openclaw/snapshots/<id>; restore rebuilds the branch at the original commit with the snapshot content as uncommitted working state.
  • Cleanup: lossless auto-remove when a run ends; 7-day idle GC for run-owned worktrees (manual ones exempt); orphan reconciliation; 30-day snapshot retention.
  • Surfaces: worktrees.* gateway RPC, openclaw worktrees CLI, Control UI page, plugin-SDK facade, and Workboard materialization of kind: "worktree" cards.

Non-goals: managing foreign worktrees (~/.codex/worktrees, .claude/worktrees), new config keys, apply-to-main merge-back, port/env isolation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal backlog priority with limited blast radius.clawsweeper:linked-pr-openClawSweeper found an open linked pull request for this issue.clawsweeper:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:otherThis issue has meaningful maintainer-visible impact outside the owned taxonomy.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.maintainerMaintainer-authored PR

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions