/** * @file Run AI agents in parallel, each in its own git worktree, and merge * results back to the base branch. Socket's CLAUDE.md "Parallel Claude * sessions" rule mandates worktree isolation when multiple agents touch the * same checkout. This helper enforces that contract: each item gets a fresh * worktree branched from the base, the per-item function runs inside it, then * the helper either fast-forward-merges the worktree branch back into the * base once changes were committed, or removes the worktree silently when no * changes happened. Concurrency: caller-controlled with a default cap of * 4. Per the "Programmatic Claude calls" CLAUDE.md rule, very high * concurrency saturates the API rate limits; 4 is safe headroom for most * accounts. The hard cap of 8 prevents accidental flag-finger disasters. * Cleanup policy: see `WorktreeCleanup` in types.mts. Default 'always' — * fail-or-pass, the worktree is removed. */ import type { WorktreeCleanup } from './types.js'; export interface WorktreeRunOptions { /** * Base repo path of the primary checkout. */ readonly baseRepo: string; /** * Branch to merge results into. Default: current branch of baseRepo. */ readonly branch?: string | undefined; /** * Cleanup policy. Default 'always'. */ readonly cleanup?: WorktreeCleanup | undefined; /** * Parallel cap. Default 4, max 8. */ readonly concurrency?: number | undefined; /** * Prefix for worktree branch + dir names. Default 'agent-task'. */ readonly namePrefix?: string | undefined; /** * Where worktrees live on disk. Default: `${tmpdir}/${prefix}-`. */ readonly worktreeRoot?: string | undefined; } export interface WorktreeRunContext { /** * Worktree dir on disk. */ readonly cwd: string; /** * Branch name created for this worktree. */ readonly branch: string; /** * 0-indexed item position. */ readonly index: number; } export interface WorktreeRunSettled { readonly cleanup: 'removed' | 'kept'; readonly error?: unknown | undefined; readonly merged: boolean; readonly status: 'fulfilled' | 'rejected'; readonly value?: T | undefined; readonly worktreePath: string; } export declare function currentBranch(repo: string): string; export declare function git(cwd: string, ...args: string[]): string; export declare function hasCommittedChanges(worktree: string, baseBranch: string): boolean; export declare function hasStagedOrUnstaged(worktree: string): boolean; export declare function runOne(item: I, index: number, worktreeBranch: string, worktreePath: string, baseRepo: string, branch: string, cleanup: WorktreeCleanup, fn: (item: I, ctx: WorktreeRunContext) => Promise): Promise>; /** * Run `fn` for each item in parallel, each invocation isolated in its own git * worktree. Results are merged back into the base branch with `git merge * --ff-only`; non-FF merges are reported as failures and the worktree is * preserved regardless of cleanup policy. * * @example * ;```ts * import { spawnAiAgentsInWorktrees } from '@socketsecurity/lib/ai/worktree' * import { spawnAiAgent } from '@socketsecurity/lib/ai/spawn' * import { AI_PROFILE } from '@socketsecurity/lib/ai/profiles' * * const repos = ['socket-addon', 'socket-btm', 'socket-lib'] * const settled = await spawnAiAgentsInWorktrees( * repos, * async ({ cwd }) => { * return await spawnAiAgent({ * ...AI_PROFILE.create, * prompt: 'Run the cleanup task', * cwd, * }) * }, * { * baseRepo: '/Users//projects/socket-wheelhouse', * concurrency: 3, * cleanup: 'on-empty', * }, * ) * ``` */ export declare function spawnAiAgentsInWorktrees(items: readonly I[], fn: (item: I, ctx: WorktreeRunContext) => Promise, options: WorktreeRunOptions): Promise>>; export declare function tryGit(cwd: string, ...args: string[]): { ok: boolean; output: string; };