Skip to content

Create worktrees in parallel to reduce startup latency for large -n values #76

Description

@that-github-user

Problem

In src/commands/run.ts, worktrees are created sequentially in a for loop:

for (let i = 1; i <= opts.attempts; i++) {
  const worktree = await git.createWorktree(repoRoot, `thinktank-${i}-...`);
  worktrees.push(worktree);
}

Each git worktree add call takes ~200-500ms. With -n 10, that's up to 5 seconds of setup before the first agent even starts.

Fix

Create all worktrees in parallel:

const worktrees = await Promise.all(
  Array.from({ length: opts.attempts }, (_, i) =>
    git.createWorktree(repoRoot, `thinktank-${i + 1}-${randomUUID().slice(0, 8)}`)
  )
);

Considerations

  • Git itself serializes some worktree operations internally; parallelism gains are real but bounded
  • Error handling: if one worktree creation fails, clean up all successfully created worktrees before throwing
  • The existing cleanup path already handles partial lists

Benchmark target

With -n 5 on a mid-range machine, worktree setup time should drop from ~2s to ~600ms.

Acceptance criteria

  • Worktrees are created with Promise.all in run.ts
  • If any worktree creation fails, all already-created worktrees are cleaned up before the error is shown
  • thinktank run -n 10 startup is measurably faster (documented in PR with before/after timing)
  • Existing tests still pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions