---
description: Spawn a new tmux window with a Claude Code session in its own git worktree (via --worktree flag)
arguments:
  - name: task
    description: Brief description of the task for the new session (used for branch name and seed prompt)
    required: true
  - name: model
    description: "Model to use for the spawned session (e.g., 'haiku', 'opus', 'sonnet')"
    required: false
---

Spawn a new tmux **window** with a Claude Code session in an isolated git worktree using the native `--worktree` flag. The worktree is created at `<repo>/.claude/worktrees/<slug>` with branch `worktree-<slug>`.

On session exit, Claude handles worktree cleanup automatically:
- **No changes:** worktree + branch removed automatically
- **Changes exist:** user is prompted to keep or remove

## CRITICAL: Always use the -w flag

The `-w` (or `--worktree`) flag MUST be passed to `claude`. This is what makes Claude aware it's in a worktree and enables auto-cleanup on exit.

**DO NOT** pre-create worktrees with `git worktree add` and then `cd` into them. **DO NOT** run `claude` without `-w` inside a worktree directory. That causes "escaped worktree" — Claude doesn't know it's in a worktree, CWD drifts, and no auto-cleanup happens.

## Steps

1. **Capture context first** (before anything else):
   ```bash
   REPO_ROOT=$(git rev-parse --show-toplevel)
   ```

2. **Derive the slug** from the task argument:
   - Take the first 2-3 key words of the task
   - Lowercase, hyphens, no punctuation
   - Example: "fix authentication timeout bug" → `fix-auth-timeout`
   - Branch will be `worktree-fix-auth-timeout`

3. **Validate** the branch name doesn't already exist:
   ```bash
   git branch --list "worktree-<slug>"
   ```
   If it exists, ask the user whether to reuse or pick a different name.

4. **Spawn a tmux window with `claude -w`:**

   ```bash
   SLUG="<slug>"
   SEED="<seed-prompt-text>"
   tmux new-window -n "cc-${SLUG}" -c "$REPO_ROOT" \
     "SSH_AUTH_SOCK=$SSH_AUTH_SOCK SSH_AGENT_PID=$SSH_AGENT_PID claude -w ${SLUG} --permission-mode acceptEdits ${model:+--model ${model}} -- ${SEED@Q}"
   ```

   Use `${SEED@Q}` (bash quoting operator) to safely escape the seed prompt. The SSH env vars are forwarded inline for WSL2 compatibility. `--permission-mode acceptEdits` auto-approves file edits/writes so spawned sessions run without stalling on permission prompts.

5. **Seed prompt** (the `SEED` variable):
   ```
   You are working in a git worktree on branch worktree-<slug>.

   Task: <full task description>

   Start by understanding the codebase context relevant to this task.
   When finished:
   1. Run the code-simplifier:code-simplifier agent on your changes
   2. Commit your changes
   3. STOP. Your work is done.

   Do NOT merge to main — the parent session handles that.
   Do NOT manually delete your worktree — Claude handles cleanup on exit.
   ```

6. **Confirm to the user:**
   - Slug and branch name (`worktree-<slug>`)
   - Worktree location: `<repo>/.claude/worktrees/<slug>`
   - tmux window: `cc-<slug>` (switch with `Prefix + n` or `Prefix + <number>`)
   - Remind: to merge, use `/merge-cleanup <slug>` from the main session

## Example

`/tmux-spawn fix authentication timeout bug`:
- Slug: `fix-auth-timeout`
- Branch: `worktree-fix-auth-timeout`
- Worktree: `<repo>/.claude/worktrees/fix-auth-timeout`
- tmux window: `cc-fix-auth-timeout`

```bash
SLUG="fix-auth-timeout"
SEED="You are working in a git worktree on branch worktree-fix-auth-timeout.

Task: fix authentication timeout bug
..."
tmux new-window -n "cc-fix-auth-timeout" -c "/home/mrf/Projects/my-repo" \
  "SSH_AUTH_SOCK=$SSH_AUTH_SOCK SSH_AGENT_PID=$SSH_AGENT_PID claude -w fix-auth-timeout --permission-mode acceptEdits -- ${SEED@Q}"
```

## Notes

- Each session is fully isolated — own directory, own branch, no conflicts
- User handles all remote git operations (push, pull, fetch)
- Never run sessions in the background
- Native cleanup prompts on session exit (keep/remove) — "remove" discards commits, so merge first if needed
- To merge + clean up: `/merge-cleanup <slug>` from the main session
