Skip to content

Agent worktrees pile up inside the repo and choke the dev server's file watcher: relocate them with a WorktreeCreate hook, prune merged ones on SessionStart #1

Description

@kaiomp

Version: 0.6.0 (latest 0.7.0)
Command: none (Agent tool dispatch with isolation: "worktree")

What happened:
Over ~15 sessions, background agents dispatched with isolation: "worktree"
left their worktrees under .claude/worktrees/. Auto-clean only removes an
UNCHANGED worktree, so every agent that did real work left one behind, each
with its own node_modules. The framework dev server (Next.js) watches the
repo root and has no ignore setting: after every edit it rescanned all
15 copies, sat at 100% CPU and stopped answering. It took profiling the
server process to find the cause. The worktrees are git-ignored, so nothing
surfaced them.

What I expected:
flow keeps the repo free of agent worktrees:

  1. A WorktreeCreate hook that places agent worktrees OUTSIDE the repository
    (e.g. ~/.claude/worktrees//), so no in-repo watcher ever
    sees them. Claude Code documents this hook as the way to replace
    worktree creation; there is no setting to relocate .claude/worktrees.
  2. A SessionStart (or SessionEnd) hook that runs git worktree prune and
    removes agent worktrees whose branch is already merged into the default
    branch. Unmerged work is never touched. There is no per-subagent hook,
    so session-level is the only place.
  3. A line in the /flow:continue recap when leftover agent worktrees exist.
    ^ assumed — correct me on where these belong in flow

Repro:
A monorepo with a large node_modules. Dispatch two agents with
isolation: "worktree" that commit; merge their branches; start the
framework dev server; edit a file. The watcher rescans every copy.
^ assumed — the profile showed the watcher thread busy and the hang
disappeared once the copies were removed; the exact trigger is inferred.

Workaround used:
git worktree remove --force && git branch -D for each
merged agent worktree, then git worktree prune. Documented in the
project's CLAUDE.md until flow ships the hooks.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions