worktree.ts 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. import { feature } from 'bun:bundle'
  2. import chalk from 'chalk'
  3. import { spawnSync } from 'child_process'
  4. import {
  5. copyFile,
  6. mkdir,
  7. readdir,
  8. readFile,
  9. stat,
  10. symlink,
  11. utimes,
  12. } from 'fs/promises'
  13. import ignore from 'ignore'
  14. import { basename, dirname, join } from 'path'
  15. import { saveCurrentProjectConfig } from './config.js'
  16. import { getCwd } from './cwd.js'
  17. import { logForDebugging } from './debug.js'
  18. import { errorMessage, getErrnoCode } from './errors.js'
  19. import { execFileNoThrow, execFileNoThrowWithCwd } from './execFileNoThrow.js'
  20. import { parseGitConfigValue } from './git/gitConfigParser.js'
  21. import {
  22. getCommonDir,
  23. readWorktreeHeadSha,
  24. resolveGitDir,
  25. resolveRef,
  26. } from './git/gitFilesystem.js'
  27. import {
  28. findCanonicalGitRoot,
  29. findGitRoot,
  30. getBranch,
  31. getDefaultBranch,
  32. gitExe,
  33. } from './git.js'
  34. import {
  35. executeWorktreeCreateHook,
  36. executeWorktreeRemoveHook,
  37. hasWorktreeCreateHook,
  38. } from './hooks.js'
  39. import { containsPathTraversal } from './path.js'
  40. import { getPlatform } from './platform.js'
  41. import {
  42. getInitialSettings,
  43. getRelativeSettingsFilePathForSource,
  44. } from './settings/settings.js'
  45. import { sleep } from './sleep.js'
  46. import { isInITerm2 } from './swarm/backends/detection.js'
  47. const VALID_WORKTREE_SLUG_SEGMENT = /^[a-zA-Z0-9._-]+$/
  48. const MAX_WORKTREE_SLUG_LENGTH = 64
  49. /**
  50. * Validates a worktree slug to prevent path traversal and directory escape.
  51. *
  52. * The slug is joined into `.claude/worktrees/<slug>` via path.join, which
  53. * normalizes `..` segments — so `../../../target` would escape the worktrees
  54. * directory. Similarly, an absolute path (leading `/` or `C:\`) would discard
  55. * the prefix entirely.
  56. *
  57. * Forward slashes are allowed for nesting (e.g. `asm/feature-foo`); each
  58. * segment is validated independently against the allowlist, so `.` / `..`
  59. * segments and drive-spec characters are still rejected.
  60. *
  61. * Throws synchronously — callers rely on this running before any side effects
  62. * (git commands, hook execution, chdir).
  63. */
  64. export function validateWorktreeSlug(slug: string): void {
  65. if (slug.length > MAX_WORKTREE_SLUG_LENGTH) {
  66. throw new Error(
  67. `Invalid worktree name: must be ${MAX_WORKTREE_SLUG_LENGTH} characters or fewer (got ${slug.length})`,
  68. )
  69. }
  70. // Leading or trailing `/` would make path.join produce an absolute path
  71. // or a dangling segment. Splitting and validating each segment rejects
  72. // both (empty segments fail the regex) while allowing `user/feature`.
  73. for (const segment of slug.split('/')) {
  74. if (segment === '.' || segment === '..') {
  75. throw new Error(
  76. `Invalid worktree name "${slug}": must not contain "." or ".." path segments`,
  77. )
  78. }
  79. if (!VALID_WORKTREE_SLUG_SEGMENT.test(segment)) {
  80. throw new Error(
  81. `Invalid worktree name "${slug}": each "/"-separated segment must be non-empty and contain only letters, digits, dots, underscores, and dashes`,
  82. )
  83. }
  84. }
  85. }
  86. // Helper function to create directories recursively
  87. async function mkdirRecursive(dirPath: string): Promise<void> {
  88. await mkdir(dirPath, { recursive: true })
  89. }
  90. /**
  91. * Symlinks directories from the main repository to avoid duplication.
  92. * This prevents disk bloat from duplicating node_modules and other large directories.
  93. *
  94. * @param repoRootPath - Path to the main repository root
  95. * @param worktreePath - Path to the worktree directory
  96. * @param dirsToSymlink - Array of directory names to symlink (e.g., ['node_modules'])
  97. */
  98. async function symlinkDirectories(
  99. repoRootPath: string,
  100. worktreePath: string,
  101. dirsToSymlink: string[],
  102. ): Promise<void> {
  103. for (const dir of dirsToSymlink) {
  104. // Validate directory doesn't escape repository boundaries
  105. if (containsPathTraversal(dir)) {
  106. logForDebugging(
  107. `Skipping symlink for "${dir}": path traversal detected`,
  108. { level: 'warn' },
  109. )
  110. continue
  111. }
  112. const sourcePath = join(repoRootPath, dir)
  113. const destPath = join(worktreePath, dir)
  114. try {
  115. await symlink(sourcePath, destPath, 'dir')
  116. logForDebugging(
  117. `Symlinked ${dir} from main repository to worktree to avoid disk bloat`,
  118. )
  119. } catch (error) {
  120. const code = getErrnoCode(error)
  121. // ENOENT: source doesn't exist yet (expected - skip silently)
  122. // EEXIST: destination already exists (expected - skip silently)
  123. if (code !== 'ENOENT' && code !== 'EEXIST') {
  124. // Unexpected error (e.g., permission denied, unsupported platform)
  125. logForDebugging(
  126. `Failed to symlink ${dir} (${code ?? 'unknown'}): ${errorMessage(error)}`,
  127. { level: 'warn' },
  128. )
  129. }
  130. }
  131. }
  132. }
  133. export type WorktreeSession = {
  134. originalCwd: string
  135. worktreePath: string
  136. worktreeName: string
  137. worktreeBranch?: string
  138. originalBranch?: string
  139. originalHeadCommit?: string
  140. sessionId: string
  141. tmuxSessionName?: string
  142. hookBased?: boolean
  143. /** How long worktree creation took (unset when resuming an existing worktree). */
  144. creationDurationMs?: number
  145. /** True if git sparse-checkout was applied via settings.worktree.sparsePaths. */
  146. usedSparsePaths?: boolean
  147. }
  148. let currentWorktreeSession: WorktreeSession | null = null
  149. export function getCurrentWorktreeSession(): WorktreeSession | null {
  150. return currentWorktreeSession
  151. }
  152. /**
  153. * Restore the worktree session on --resume. The caller must have already
  154. * verified the directory exists (via process.chdir) and set the bootstrap
  155. * state (cwd, originalCwd).
  156. */
  157. export function restoreWorktreeSession(session: WorktreeSession | null): void {
  158. currentWorktreeSession = session
  159. }
  160. export function generateTmuxSessionName(
  161. repoPath: string,
  162. branch: string,
  163. ): string {
  164. const repoName = basename(repoPath)
  165. const combined = `${repoName}_${branch}`
  166. return combined.replace(/[/.]/g, '_')
  167. }
  168. type WorktreeCreateResult =
  169. | {
  170. worktreePath: string
  171. worktreeBranch: string
  172. headCommit: string
  173. existed: true
  174. }
  175. | {
  176. worktreePath: string
  177. worktreeBranch: string
  178. headCommit: string
  179. baseBranch: string
  180. existed: false
  181. }
  182. // Env vars to prevent git/SSH from prompting for credentials (which hangs the CLI).
  183. // GIT_TERMINAL_PROMPT=0 prevents git from opening /dev/tty for credential prompts.
  184. // GIT_ASKPASS='' disables askpass GUI programs.
  185. // stdin: 'ignore' closes stdin so interactive prompts can't block.
  186. const GIT_NO_PROMPT_ENV = {
  187. GIT_TERMINAL_PROMPT: '0',
  188. GIT_ASKPASS: '',
  189. }
  190. function worktreesDir(repoRoot: string): string {
  191. return join(repoRoot, '.claude', 'worktrees')
  192. }
  193. // Flatten nested slugs (`user/feature` → `user+feature`) for both the branch
  194. // name and the directory path. Nesting in either location is unsafe:
  195. // - git refs: `worktree-user` (file) vs `worktree-user/feature` (needs dir)
  196. // is a D/F conflict that git rejects.
  197. // - directory: `.claude/worktrees/user/feature/` lives inside the `user`
  198. // worktree; `git worktree remove` on the parent deletes children with
  199. // uncommitted work.
  200. // `+` is valid in git branch names and filesystem paths but NOT in the
  201. // slug-segment allowlist ([a-zA-Z0-9._-]), so the mapping is injective.
  202. function flattenSlug(slug: string): string {
  203. return slug.replaceAll('/', '+')
  204. }
  205. export function worktreeBranchName(slug: string): string {
  206. return `worktree-${flattenSlug(slug)}`
  207. }
  208. function worktreePathFor(repoRoot: string, slug: string): string {
  209. return join(worktreesDir(repoRoot), flattenSlug(slug))
  210. }
  211. /**
  212. * Creates a new git worktree for the given slug, or resumes it if it already exists.
  213. * Named worktrees reuse the same path across invocations, so the existence check
  214. * prevents unconditionally running `git fetch` (which can hang waiting for credentials)
  215. * on every resume.
  216. */
  217. async function getOrCreateWorktree(
  218. repoRoot: string,
  219. slug: string,
  220. options?: { prNumber?: number },
  221. ): Promise<WorktreeCreateResult> {
  222. const worktreePath = worktreePathFor(repoRoot, slug)
  223. const worktreeBranch = worktreeBranchName(slug)
  224. // Fast resume path: if the worktree already exists skip fetch and creation.
  225. // Read the .git pointer file directly (no subprocess, no upward walk) — a
  226. // subprocess `rev-parse HEAD` burns ~15ms on spawn overhead even for a 2ms
  227. // task, and the await yield lets background spawnSyncs pile on (seen at 55ms).
  228. const existingHead = await readWorktreeHeadSha(worktreePath)
  229. if (existingHead) {
  230. return {
  231. worktreePath,
  232. worktreeBranch,
  233. headCommit: existingHead,
  234. existed: true,
  235. }
  236. }
  237. // New worktree: fetch base branch then add
  238. await mkdir(worktreesDir(repoRoot), { recursive: true })
  239. const fetchEnv = { ...process.env, ...GIT_NO_PROMPT_ENV }
  240. let baseBranch: string
  241. let baseSha: string | null = null
  242. if (options?.prNumber) {
  243. const { code: prFetchCode, stderr: prFetchStderr } =
  244. await execFileNoThrowWithCwd(
  245. gitExe(),
  246. ['fetch', 'origin', `pull/${options.prNumber}/head`],
  247. { cwd: repoRoot, stdin: 'ignore', env: fetchEnv },
  248. )
  249. if (prFetchCode !== 0) {
  250. throw new Error(
  251. `Failed to fetch PR #${options.prNumber}: ${prFetchStderr.trim() || 'PR may not exist or the repository may not have a remote named "origin"'}`,
  252. )
  253. }
  254. baseBranch = 'FETCH_HEAD'
  255. } else {
  256. // If origin/<branch> already exists locally, skip fetch. In large repos
  257. // (210k files, 16M objects) fetch burns ~6-8s on a local commit-graph
  258. // scan before even hitting the network. A slightly stale base is fine —
  259. // the user can pull in the worktree if they want latest.
  260. // resolveRef reads the loose/packed ref directly; when it succeeds we
  261. // already have the SHA, so the later rev-parse is skipped entirely.
  262. const [defaultBranch, gitDir] = await Promise.all([
  263. getDefaultBranch(),
  264. resolveGitDir(repoRoot),
  265. ])
  266. const originRef = `origin/${defaultBranch}`
  267. const originSha = gitDir
  268. ? await resolveRef(gitDir, `refs/remotes/origin/${defaultBranch}`)
  269. : null
  270. if (originSha) {
  271. baseBranch = originRef
  272. baseSha = originSha
  273. } else {
  274. const { code: fetchCode } = await execFileNoThrowWithCwd(
  275. gitExe(),
  276. ['fetch', 'origin', defaultBranch],
  277. { cwd: repoRoot, stdin: 'ignore', env: fetchEnv },
  278. )
  279. baseBranch = fetchCode === 0 ? originRef : 'HEAD'
  280. }
  281. }
  282. // For the fetch/PR-fetch paths we still need the SHA — the fs-only resolveRef
  283. // above only covers the "origin/<branch> already exists locally" case.
  284. if (!baseSha) {
  285. const { stdout, code: shaCode } = await execFileNoThrowWithCwd(
  286. gitExe(),
  287. ['rev-parse', baseBranch],
  288. { cwd: repoRoot },
  289. )
  290. if (shaCode !== 0) {
  291. throw new Error(
  292. `Failed to resolve base branch "${baseBranch}": git rev-parse failed`,
  293. )
  294. }
  295. baseSha = stdout.trim()
  296. }
  297. const sparsePaths = getInitialSettings().worktree?.sparsePaths
  298. const addArgs = ['worktree', 'add']
  299. if (sparsePaths?.length) {
  300. addArgs.push('--no-checkout')
  301. }
  302. // -B (not -b): reset any orphan branch left behind by a removed worktree dir.
  303. // Saves a `git branch -D` subprocess (~15ms spawn overhead) on every create.
  304. addArgs.push('-B', worktreeBranch, worktreePath, baseBranch)
  305. const { code: createCode, stderr: createStderr } =
  306. await execFileNoThrowWithCwd(gitExe(), addArgs, { cwd: repoRoot })
  307. if (createCode !== 0) {
  308. throw new Error(`Failed to create worktree: ${createStderr}`)
  309. }
  310. if (sparsePaths?.length) {
  311. // If sparse-checkout or checkout fail after --no-checkout, the worktree
  312. // is registered and HEAD is set but the working tree is empty. Next run's
  313. // fast-resume (rev-parse HEAD) would succeed and present a broken worktree
  314. // as "resumed". Tear it down before propagating the error.
  315. const tearDown = async (msg: string): Promise<never> => {
  316. await execFileNoThrowWithCwd(
  317. gitExe(),
  318. ['worktree', 'remove', '--force', worktreePath],
  319. { cwd: repoRoot },
  320. )
  321. throw new Error(msg)
  322. }
  323. const { code: sparseCode, stderr: sparseErr } =
  324. await execFileNoThrowWithCwd(
  325. gitExe(),
  326. ['sparse-checkout', 'set', '--cone', '--', ...sparsePaths],
  327. { cwd: worktreePath },
  328. )
  329. if (sparseCode !== 0) {
  330. await tearDown(`Failed to configure sparse-checkout: ${sparseErr}`)
  331. }
  332. const { code: coCode, stderr: coErr } = await execFileNoThrowWithCwd(
  333. gitExe(),
  334. ['checkout', 'HEAD'],
  335. { cwd: worktreePath },
  336. )
  337. if (coCode !== 0) {
  338. await tearDown(`Failed to checkout sparse worktree: ${coErr}`)
  339. }
  340. }
  341. return {
  342. worktreePath,
  343. worktreeBranch,
  344. headCommit: baseSha,
  345. baseBranch,
  346. existed: false,
  347. }
  348. }
  349. /**
  350. * Copy gitignored files specified in .worktreeinclude from base repo to worktree.
  351. *
  352. * Only copies files that are BOTH:
  353. * 1. Matched by patterns in .worktreeinclude (uses .gitignore syntax)
  354. * 2. Gitignored (not tracked by git)
  355. *
  356. * Uses `git ls-files --others --ignored --exclude-standard --directory` to list
  357. * gitignored entries with fully-ignored dirs collapsed to single entries (so large
  358. * build outputs like node_modules/ don't force a full tree walk), then filters
  359. * against .worktreeinclude patterns in-process using the `ignore` library. If a
  360. * .worktreeinclude pattern explicitly targets a path inside a collapsed directory,
  361. * that directory is expanded with a second scoped `ls-files` call.
  362. */
  363. export async function copyWorktreeIncludeFiles(
  364. repoRoot: string,
  365. worktreePath: string,
  366. ): Promise<string[]> {
  367. let includeContent: string
  368. try {
  369. includeContent = await readFile(join(repoRoot, '.worktreeinclude'), 'utf-8')
  370. } catch {
  371. return []
  372. }
  373. const patterns = includeContent
  374. .split(/\r?\n/)
  375. .map(line => line.trim())
  376. .filter(line => line.length > 0 && !line.startsWith('#'))
  377. if (patterns.length === 0) {
  378. return []
  379. }
  380. // Single pass with --directory: collapses fully-gitignored dirs (node_modules/,
  381. // .turbo/, etc.) into single entries instead of listing every file inside.
  382. // In a large repo this cuts ~500k entries/~7s down to ~hundreds of entries/~100ms.
  383. const gitignored = await execFileNoThrowWithCwd(
  384. gitExe(),
  385. ['ls-files', '--others', '--ignored', '--exclude-standard', '--directory'],
  386. { cwd: repoRoot },
  387. )
  388. if (gitignored.code !== 0 || !gitignored.stdout.trim()) {
  389. return []
  390. }
  391. const entries = gitignored.stdout.trim().split('\n').filter(Boolean)
  392. const matcher = ignore().add(includeContent)
  393. // --directory emits collapsed dirs with a trailing slash; everything else is
  394. // an individual file.
  395. const collapsedDirs = entries.filter(e => e.endsWith('/'))
  396. const files = entries.filter(e => !e.endsWith('/') && matcher.ignores(e))
  397. // Edge case: a .worktreeinclude pattern targets a path inside a collapsed dir
  398. // (e.g. pattern `config/secrets/api.key` when all of `config/secrets/` is
  399. // gitignored with no tracked siblings). Expand only dirs where a pattern has
  400. // that dir as its explicit path prefix (stripping redundant leading `/`), the
  401. // dir falls under an anchored glob's literal prefix (e.g. `config/**/*.key`
  402. // expands `config/secrets/`), or the dir itself matches a pattern. We don't
  403. // expand for `**/` or anchorless patterns -- those match files in tracked dirs
  404. // (already listed individually) and expanding every collapsed dir for them
  405. // would defeat the perf win.
  406. const dirsToExpand = collapsedDirs.filter(dir => {
  407. if (
  408. patterns.some(p => {
  409. const normalized = p.startsWith('/') ? p.slice(1) : p
  410. // Literal prefix match: pattern starts with the collapsed dir path
  411. if (normalized.startsWith(dir)) return true
  412. // Anchored glob: dir falls under the pattern's literal (non-glob) prefix
  413. // e.g. `config/**/*.key` has literal prefix `config/` → expand `config/secrets/`
  414. const globIdx = normalized.search(/[*?[]/)
  415. if (globIdx > 0) {
  416. const literalPrefix = normalized.slice(0, globIdx)
  417. if (dir.startsWith(literalPrefix)) return true
  418. }
  419. return false
  420. })
  421. )
  422. return true
  423. if (matcher.ignores(dir.slice(0, -1))) return true
  424. return false
  425. })
  426. if (dirsToExpand.length > 0) {
  427. const expanded = await execFileNoThrowWithCwd(
  428. gitExe(),
  429. [
  430. 'ls-files',
  431. '--others',
  432. '--ignored',
  433. '--exclude-standard',
  434. '--',
  435. ...dirsToExpand,
  436. ],
  437. { cwd: repoRoot },
  438. )
  439. if (expanded.code === 0 && expanded.stdout.trim()) {
  440. for (const f of expanded.stdout.trim().split('\n').filter(Boolean)) {
  441. if (matcher.ignores(f)) {
  442. files.push(f)
  443. }
  444. }
  445. }
  446. }
  447. const copied: string[] = []
  448. for (const relativePath of files) {
  449. const srcPath = join(repoRoot, relativePath)
  450. const destPath = join(worktreePath, relativePath)
  451. try {
  452. await mkdir(dirname(destPath), { recursive: true })
  453. await copyFile(srcPath, destPath)
  454. copied.push(relativePath)
  455. } catch (e: unknown) {
  456. logForDebugging(
  457. `Failed to copy ${relativePath} to worktree: ${(e as Error).message}`,
  458. { level: 'warn' },
  459. )
  460. }
  461. }
  462. if (copied.length > 0) {
  463. logForDebugging(
  464. `Copied ${copied.length} files from .worktreeinclude: ${copied.join(', ')}`,
  465. )
  466. }
  467. return copied
  468. }
  469. /**
  470. * Post-creation setup for a newly created worktree.
  471. * Propagates settings.local.json, configures git hooks, and symlinks directories.
  472. */
  473. async function performPostCreationSetup(
  474. repoRoot: string,
  475. worktreePath: string,
  476. ): Promise<void> {
  477. // Copy settings.local.json to the worktree's .claude directory
  478. // This propagates local settings (which may contain secrets) to the worktree
  479. const localSettingsRelativePath =
  480. getRelativeSettingsFilePathForSource('localSettings')
  481. const sourceSettingsLocal = join(repoRoot, localSettingsRelativePath)
  482. try {
  483. const destSettingsLocal = join(worktreePath, localSettingsRelativePath)
  484. await mkdirRecursive(dirname(destSettingsLocal))
  485. await copyFile(sourceSettingsLocal, destSettingsLocal)
  486. logForDebugging(
  487. `Copied settings.local.json to worktree: ${destSettingsLocal}`,
  488. )
  489. } catch (e: unknown) {
  490. const code = getErrnoCode(e)
  491. if (code !== 'ENOENT') {
  492. logForDebugging(
  493. `Failed to copy settings.local.json: ${(e as Error).message}`,
  494. { level: 'warn' },
  495. )
  496. }
  497. }
  498. // Configure the worktree to use hooks from the main repository
  499. // This solves issues with .husky and other git hooks that use relative paths
  500. const huskyPath = join(repoRoot, '.husky')
  501. const gitHooksPath = join(repoRoot, '.git', 'hooks')
  502. let hooksPath: string | null = null
  503. for (const candidatePath of [huskyPath, gitHooksPath]) {
  504. try {
  505. const s = await stat(candidatePath)
  506. if (s.isDirectory()) {
  507. hooksPath = candidatePath
  508. break
  509. }
  510. } catch {
  511. // Path doesn't exist or can't be accessed
  512. }
  513. }
  514. if (hooksPath) {
  515. // `git config` (no --worktree flag) writes to the main repo's .git/config,
  516. // shared by all worktrees. Once set, every subsequent worktree create is a
  517. // no-op — skip the subprocess (~14ms spawn) when the value already matches.
  518. const gitDir = await resolveGitDir(repoRoot)
  519. const configDir = gitDir ? ((await getCommonDir(gitDir)) ?? gitDir) : null
  520. const existing = configDir
  521. ? await parseGitConfigValue(configDir, 'core', null, 'hooksPath')
  522. : null
  523. if (existing !== hooksPath) {
  524. const { code: configCode, stderr: configError } =
  525. await execFileNoThrowWithCwd(
  526. gitExe(),
  527. ['config', 'core.hooksPath', hooksPath],
  528. { cwd: worktreePath },
  529. )
  530. if (configCode === 0) {
  531. logForDebugging(
  532. `Configured worktree to use hooks from main repository: ${hooksPath}`,
  533. )
  534. } else {
  535. logForDebugging(`Failed to configure hooks path: ${configError}`, {
  536. level: 'error',
  537. })
  538. }
  539. }
  540. }
  541. // Symlink directories to avoid disk bloat (opt-in via settings)
  542. const settings = getInitialSettings()
  543. const dirsToSymlink = settings.worktree?.symlinkDirectories ?? []
  544. if (dirsToSymlink.length > 0) {
  545. await symlinkDirectories(repoRoot, worktreePath, dirsToSymlink)
  546. }
  547. // Copy gitignored files specified in .worktreeinclude (best-effort)
  548. await copyWorktreeIncludeFiles(repoRoot, worktreePath)
  549. // The core.hooksPath config-set above is fragile: husky's prepare script
  550. // (`git config core.hooksPath .husky`) runs on every `bun install` and
  551. // resets the SHARED .git/config value back to relative, causing each
  552. // worktree to resolve to its OWN .husky/ again. The attribution hook
  553. // file isn't tracked (it's in .git/info/exclude), so fresh worktrees
  554. // don't have it. Install it directly into the worktree's .husky/ —
  555. // husky won't delete it (husky install is additive-only), and for
  556. // non-husky repos this resolves to the shared .git/hooks/ (idempotent).
  557. //
  558. // Pass the worktree-local .husky explicitly: getHooksDir would return
  559. // the absolute core.hooksPath we just set above (main repo's .husky),
  560. // not the worktree's — `git rev-parse --git-path hooks` echoes the config
  561. // value verbatim when it's absolute.
  562. if (feature('COMMIT_ATTRIBUTION')) {
  563. const worktreeHooksDir =
  564. hooksPath === huskyPath ? join(worktreePath, '.husky') : undefined
  565. void import('./postCommitAttribution.js')
  566. .then(m =>
  567. m
  568. .installPrepareCommitMsgHook(worktreePath, worktreeHooksDir)
  569. .catch(error => {
  570. logForDebugging(
  571. `Failed to install attribution hook in worktree: ${error}`,
  572. )
  573. }),
  574. )
  575. .catch(error => {
  576. // Dynamic import() itself rejected (module load failure). The inner
  577. // .catch above only handles installPrepareCommitMsgHook rejection —
  578. // without this outer handler an import failure would surface as an
  579. // unhandled promise rejection.
  580. logForDebugging(`Failed to load postCommitAttribution module: ${error}`)
  581. })
  582. }
  583. }
  584. /**
  585. * Parses a PR reference from a string.
  586. * Accepts GitHub-style PR URLs (e.g., https://github.com/owner/repo/pull/123,
  587. * or GHE equivalents like https://ghe.example.com/owner/repo/pull/123)
  588. * or `#N` format (e.g., #123).
  589. * Returns the PR number or null if the string is not a recognized PR reference.
  590. */
  591. export function parsePRReference(input: string): number | null {
  592. // GitHub-style PR URL: https://<host>/owner/repo/pull/123 (with optional trailing slash, query, hash)
  593. // The /pull/N path shape is specific to GitHub — GitLab uses /-/merge_requests/N,
  594. // Bitbucket uses /pull-requests/N — so matching any host here is safe.
  595. const urlMatch = input.match(
  596. /^https?:\/\/[^/]+\/[^/]+\/[^/]+\/pull\/(\d+)\/?(?:[?#].*)?$/i,
  597. )
  598. if (urlMatch?.[1]) {
  599. return parseInt(urlMatch[1], 10)
  600. }
  601. // #N format
  602. const hashMatch = input.match(/^#(\d+)$/)
  603. if (hashMatch?.[1]) {
  604. return parseInt(hashMatch[1], 10)
  605. }
  606. return null
  607. }
  608. export async function isTmuxAvailable(): Promise<boolean> {
  609. const { code } = await execFileNoThrow('tmux', ['-V'])
  610. return code === 0
  611. }
  612. export function getTmuxInstallInstructions(): string {
  613. const platform = getPlatform()
  614. switch (platform) {
  615. case 'macos':
  616. return 'Install tmux with: brew install tmux'
  617. case 'linux':
  618. case 'wsl':
  619. return 'Install tmux with: sudo apt install tmux (Debian/Ubuntu) or sudo dnf install tmux (Fedora/RHEL)'
  620. case 'windows':
  621. return 'tmux is not natively available on Windows. Consider using WSL or Cygwin.'
  622. default:
  623. return 'Install tmux using your system package manager.'
  624. }
  625. }
  626. export async function createTmuxSessionForWorktree(
  627. sessionName: string,
  628. worktreePath: string,
  629. ): Promise<{ created: boolean; error?: string }> {
  630. const { code, stderr } = await execFileNoThrow('tmux', [
  631. 'new-session',
  632. '-d',
  633. '-s',
  634. sessionName,
  635. '-c',
  636. worktreePath,
  637. ])
  638. if (code !== 0) {
  639. return { created: false, error: stderr }
  640. }
  641. return { created: true }
  642. }
  643. export async function killTmuxSession(sessionName: string): Promise<boolean> {
  644. const { code } = await execFileNoThrow('tmux', [
  645. 'kill-session',
  646. '-t',
  647. sessionName,
  648. ])
  649. return code === 0
  650. }
  651. export async function createWorktreeForSession(
  652. sessionId: string,
  653. slug: string,
  654. tmuxSessionName?: string,
  655. options?: { prNumber?: number },
  656. ): Promise<WorktreeSession> {
  657. // Must run before the hook branch below — hooks receive the raw slug as an
  658. // argument, and the git branch builds a path from it via path.join.
  659. validateWorktreeSlug(slug)
  660. const originalCwd = getCwd()
  661. // Try hook-based worktree creation first (allows user-configured VCS)
  662. if (hasWorktreeCreateHook()) {
  663. const hookResult = await executeWorktreeCreateHook(slug)
  664. logForDebugging(
  665. `Created hook-based worktree at: ${hookResult.worktreePath}`,
  666. )
  667. currentWorktreeSession = {
  668. originalCwd,
  669. worktreePath: hookResult.worktreePath,
  670. worktreeName: slug,
  671. sessionId,
  672. tmuxSessionName,
  673. hookBased: true,
  674. }
  675. } else {
  676. // Fall back to git worktree
  677. const gitRoot = findGitRoot(getCwd())
  678. if (!gitRoot) {
  679. throw new Error(
  680. 'Cannot create a worktree: not in a git repository and no WorktreeCreate hooks are configured. ' +
  681. 'Configure WorktreeCreate/WorktreeRemove hooks in settings.json to use worktree isolation with other VCS systems.',
  682. )
  683. }
  684. const originalBranch = await getBranch()
  685. const createStart = Date.now()
  686. const { worktreePath, worktreeBranch, headCommit, existed } =
  687. await getOrCreateWorktree(gitRoot, slug, options)
  688. let creationDurationMs: number | undefined
  689. if (existed) {
  690. logForDebugging(`Resuming existing worktree at: ${worktreePath}`)
  691. } else {
  692. logForDebugging(
  693. `Created worktree at: ${worktreePath} on branch: ${worktreeBranch}`,
  694. )
  695. await performPostCreationSetup(gitRoot, worktreePath)
  696. creationDurationMs = Date.now() - createStart
  697. }
  698. currentWorktreeSession = {
  699. originalCwd,
  700. worktreePath,
  701. worktreeName: slug,
  702. worktreeBranch,
  703. originalBranch,
  704. originalHeadCommit: headCommit,
  705. sessionId,
  706. tmuxSessionName,
  707. creationDurationMs,
  708. usedSparsePaths:
  709. (getInitialSettings().worktree?.sparsePaths?.length ?? 0) > 0,
  710. }
  711. }
  712. // Save to project config for persistence
  713. saveCurrentProjectConfig(current => ({
  714. ...current,
  715. activeWorktreeSession: currentWorktreeSession ?? undefined,
  716. }))
  717. return currentWorktreeSession
  718. }
  719. export async function keepWorktree(): Promise<void> {
  720. if (!currentWorktreeSession) {
  721. return
  722. }
  723. try {
  724. const { worktreePath, originalCwd, worktreeBranch } = currentWorktreeSession
  725. // Change back to original directory first
  726. process.chdir(originalCwd)
  727. // Clear the session but keep the worktree intact
  728. currentWorktreeSession = null
  729. // Update config
  730. saveCurrentProjectConfig(current => ({
  731. ...current,
  732. activeWorktreeSession: undefined,
  733. }))
  734. logForDebugging(
  735. `Linked worktree preserved at: ${worktreePath}${worktreeBranch ? ` on branch: ${worktreeBranch}` : ''}`,
  736. )
  737. logForDebugging(
  738. `You can continue working there by running: cd ${worktreePath}`,
  739. )
  740. } catch (error) {
  741. logForDebugging(`Error keeping worktree: ${error}`, {
  742. level: 'error',
  743. })
  744. }
  745. }
  746. export async function cleanupWorktree(): Promise<void> {
  747. if (!currentWorktreeSession) {
  748. return
  749. }
  750. try {
  751. const { worktreePath, originalCwd, worktreeBranch, hookBased } =
  752. currentWorktreeSession
  753. // Change back to original directory first
  754. process.chdir(originalCwd)
  755. if (hookBased) {
  756. // Hook-based worktree: delegate cleanup to WorktreeRemove hook
  757. const hookRan = await executeWorktreeRemoveHook(worktreePath)
  758. if (hookRan) {
  759. logForDebugging(`Removed hook-based worktree at: ${worktreePath}`)
  760. } else {
  761. logForDebugging(
  762. `No WorktreeRemove hook configured, hook-based worktree left at: ${worktreePath}`,
  763. { level: 'warn' },
  764. )
  765. }
  766. } else {
  767. // Git-based worktree: use git worktree remove.
  768. // Explicit cwd: process.chdir above does NOT update getCwd() (the state
  769. // CWD that execFileNoThrow defaults to). If the model cd'd to a non-repo
  770. // dir, the bare execFileNoThrow variant would fail silently here.
  771. const { code: removeCode, stderr: removeError } =
  772. await execFileNoThrowWithCwd(
  773. gitExe(),
  774. ['worktree', 'remove', '--force', worktreePath],
  775. { cwd: originalCwd },
  776. )
  777. if (removeCode !== 0) {
  778. logForDebugging(`Failed to remove linked worktree: ${removeError}`, {
  779. level: 'error',
  780. })
  781. } else {
  782. logForDebugging(`Removed linked worktree at: ${worktreePath}`)
  783. }
  784. }
  785. // Clear the session
  786. currentWorktreeSession = null
  787. // Update config
  788. saveCurrentProjectConfig(current => ({
  789. ...current,
  790. activeWorktreeSession: undefined,
  791. }))
  792. // Delete the temporary worktree branch (git-based only)
  793. if (!hookBased && worktreeBranch) {
  794. // Wait a bit to ensure git has released all locks
  795. await sleep(100)
  796. const { code: deleteBranchCode, stderr: deleteBranchError } =
  797. await execFileNoThrowWithCwd(
  798. gitExe(),
  799. ['branch', '-D', worktreeBranch],
  800. { cwd: originalCwd },
  801. )
  802. if (deleteBranchCode !== 0) {
  803. logForDebugging(
  804. `Could not delete worktree branch: ${deleteBranchError}`,
  805. { level: 'error' },
  806. )
  807. } else {
  808. logForDebugging(`Deleted worktree branch: ${worktreeBranch}`)
  809. }
  810. }
  811. logForDebugging('Linked worktree cleaned up completely')
  812. } catch (error) {
  813. logForDebugging(`Error cleaning up worktree: ${error}`, {
  814. level: 'error',
  815. })
  816. }
  817. }
  818. /**
  819. * Create a lightweight worktree for a subagent.
  820. * Reuses getOrCreateWorktree/performPostCreationSetup but does NOT touch
  821. * global session state (currentWorktreeSession, process.chdir, project config).
  822. * Falls back to hook-based creation if not in a git repository.
  823. */
  824. export async function createAgentWorktree(slug: string): Promise<{
  825. worktreePath: string
  826. worktreeBranch?: string
  827. headCommit?: string
  828. gitRoot?: string
  829. hookBased?: boolean
  830. }> {
  831. validateWorktreeSlug(slug)
  832. // Try hook-based worktree creation first (allows user-configured VCS)
  833. if (hasWorktreeCreateHook()) {
  834. const hookResult = await executeWorktreeCreateHook(slug)
  835. logForDebugging(
  836. `Created hook-based agent worktree at: ${hookResult.worktreePath}`,
  837. )
  838. return { worktreePath: hookResult.worktreePath, hookBased: true }
  839. }
  840. // Fall back to git worktree
  841. // findCanonicalGitRoot (not findGitRoot) so agent worktrees always land in
  842. // the main repo's .claude/worktrees/ even when spawned from inside a session
  843. // worktree — otherwise they nest at <worktree>/.claude/worktrees/ and the
  844. // periodic cleanup (which scans the canonical root) never finds them.
  845. const gitRoot = findCanonicalGitRoot(getCwd())
  846. if (!gitRoot) {
  847. throw new Error(
  848. 'Cannot create agent worktree: not in a git repository and no WorktreeCreate hooks are configured. ' +
  849. 'Configure WorktreeCreate/WorktreeRemove hooks in settings.json to use worktree isolation with other VCS systems.',
  850. )
  851. }
  852. const { worktreePath, worktreeBranch, headCommit, existed } =
  853. await getOrCreateWorktree(gitRoot, slug)
  854. if (!existed) {
  855. logForDebugging(
  856. `Created agent worktree at: ${worktreePath} on branch: ${worktreeBranch}`,
  857. )
  858. await performPostCreationSetup(gitRoot, worktreePath)
  859. } else {
  860. // Bump mtime so the periodic stale-worktree cleanup doesn't consider this
  861. // worktree stale — the fast-resume path is read-only and leaves the original
  862. // creation-time mtime intact, which can be past the 30-day cutoff.
  863. const now = new Date()
  864. await utimes(worktreePath, now, now)
  865. logForDebugging(`Resuming existing agent worktree at: ${worktreePath}`)
  866. }
  867. return { worktreePath, worktreeBranch, headCommit, gitRoot }
  868. }
  869. /**
  870. * Remove a worktree created by createAgentWorktree.
  871. * For git-based worktrees, removes the worktree directory and deletes the temporary branch.
  872. * For hook-based worktrees, delegates to the WorktreeRemove hook.
  873. * Must be called with the main repo's git root (for git worktrees), not the worktree path,
  874. * since the worktree directory is deleted during this operation.
  875. */
  876. export async function removeAgentWorktree(
  877. worktreePath: string,
  878. worktreeBranch?: string,
  879. gitRoot?: string,
  880. hookBased?: boolean,
  881. ): Promise<boolean> {
  882. if (hookBased) {
  883. const hookRan = await executeWorktreeRemoveHook(worktreePath)
  884. if (hookRan) {
  885. logForDebugging(`Removed hook-based agent worktree at: ${worktreePath}`)
  886. } else {
  887. logForDebugging(
  888. `No WorktreeRemove hook configured, hook-based agent worktree left at: ${worktreePath}`,
  889. { level: 'warn' },
  890. )
  891. }
  892. return hookRan
  893. }
  894. if (!gitRoot) {
  895. logForDebugging('Cannot remove agent worktree: no git root provided', {
  896. level: 'error',
  897. })
  898. return false
  899. }
  900. // Run from the main repo root, not the worktree (which we're about to delete)
  901. const { code: removeCode, stderr: removeError } =
  902. await execFileNoThrowWithCwd(
  903. gitExe(),
  904. ['worktree', 'remove', '--force', worktreePath],
  905. { cwd: gitRoot },
  906. )
  907. if (removeCode !== 0) {
  908. logForDebugging(`Failed to remove agent worktree: ${removeError}`, {
  909. level: 'error',
  910. })
  911. return false
  912. }
  913. logForDebugging(`Removed agent worktree at: ${worktreePath}`)
  914. if (!worktreeBranch) {
  915. return true
  916. }
  917. // Delete the temporary worktree branch from the main repo
  918. const { code: deleteBranchCode, stderr: deleteBranchError } =
  919. await execFileNoThrowWithCwd(gitExe(), ['branch', '-D', worktreeBranch], {
  920. cwd: gitRoot,
  921. })
  922. if (deleteBranchCode !== 0) {
  923. logForDebugging(
  924. `Could not delete agent worktree branch: ${deleteBranchError}`,
  925. { level: 'error' },
  926. )
  927. }
  928. return true
  929. }
  930. /**
  931. * Slug patterns for throwaway worktrees created by AgentTool (`agent-a<7hex>`,
  932. * from earlyAgentId.slice(0,8)), WorkflowTool (`wf_<runId>-<idx>` where runId
  933. * is randomUUID().slice(0,12) = 8 hex + `-` + 3 hex), and bridgeMain
  934. * (`bridge-<safeFilenameId>`). These leak when the parent process is killed
  935. * (Ctrl+C, ESC, crash) before their in-process cleanup runs. Exact-shape
  936. * patterns avoid sweeping user-named EnterWorktree slugs like `wf-myfeature`.
  937. */
  938. const EPHEMERAL_WORKTREE_PATTERNS = [
  939. /^agent-a[0-9a-f]{7}$/,
  940. /^wf_[0-9a-f]{8}-[0-9a-f]{3}-\d+$/,
  941. // Legacy wf-<idx> slugs from before workflowRunId disambiguation — kept so
  942. // the 30-day sweep still cleans up worktrees leaked by older builds.
  943. /^wf-\d+$/,
  944. // Real bridge slugs are `bridge-${safeFilenameId(sessionId)}`.
  945. /^bridge-[A-Za-z0-9_]+(-[A-Za-z0-9_]+)*$/,
  946. // Template job worktrees: job-<templateName>-<8hex>. Prefix distinguishes
  947. // from user-named EnterWorktree slugs that happen to end in 8 hex.
  948. /^job-[a-zA-Z0-9._-]{1,55}-[0-9a-f]{8}$/,
  949. ]
  950. /**
  951. * Remove stale agent/workflow worktrees older than cutoffDate.
  952. *
  953. * Safety:
  954. * - Only touches slugs matching ephemeral patterns (never user-named worktrees)
  955. * - Skips the current session's worktree
  956. * - Fail-closed: skips if git status fails or shows tracked changes
  957. * (-uno: untracked files in a 30-day-old crashed agent worktree are build
  958. * artifacts; skipping the untracked scan is 5-10× faster on large repos)
  959. * - Fail-closed: skips if any commits aren't reachable from a remote
  960. *
  961. * `git worktree remove --force` handles both the directory and git's internal
  962. * worktree tracking. If git doesn't recognize the path as a worktree (orphaned
  963. * dir), it's left in place — a later readdir finding it stale again is harmless.
  964. */
  965. export async function cleanupStaleAgentWorktrees(
  966. cutoffDate: Date,
  967. ): Promise<number> {
  968. const gitRoot = findCanonicalGitRoot(getCwd())
  969. if (!gitRoot) {
  970. return 0
  971. }
  972. const dir = worktreesDir(gitRoot)
  973. let entries: string[]
  974. try {
  975. entries = await readdir(dir)
  976. } catch {
  977. return 0
  978. }
  979. const cutoffMs = cutoffDate.getTime()
  980. const currentPath = currentWorktreeSession?.worktreePath
  981. let removed = 0
  982. for (const slug of entries) {
  983. if (!EPHEMERAL_WORKTREE_PATTERNS.some(p => p.test(slug))) {
  984. continue
  985. }
  986. const worktreePath = join(dir, slug)
  987. if (currentPath === worktreePath) {
  988. continue
  989. }
  990. let mtimeMs: number
  991. try {
  992. mtimeMs = (await stat(worktreePath)).mtimeMs
  993. } catch {
  994. continue
  995. }
  996. if (mtimeMs >= cutoffMs) {
  997. continue
  998. }
  999. // Both checks must succeed with empty output. Non-zero exit (corrupted
  1000. // worktree, git not recognizing it, etc.) means skip — we don't know
  1001. // what's in there.
  1002. const [status, unpushed] = await Promise.all([
  1003. execFileNoThrowWithCwd(
  1004. gitExe(),
  1005. ['--no-optional-locks', 'status', '--porcelain', '-uno'],
  1006. { cwd: worktreePath },
  1007. ),
  1008. execFileNoThrowWithCwd(
  1009. gitExe(),
  1010. ['rev-list', '--max-count=1', 'HEAD', '--not', '--remotes'],
  1011. { cwd: worktreePath },
  1012. ),
  1013. ])
  1014. if (status.code !== 0 || status.stdout.trim().length > 0) {
  1015. continue
  1016. }
  1017. if (unpushed.code !== 0 || unpushed.stdout.trim().length > 0) {
  1018. continue
  1019. }
  1020. if (
  1021. await removeAgentWorktree(worktreePath, worktreeBranchName(slug), gitRoot)
  1022. ) {
  1023. removed++
  1024. }
  1025. }
  1026. if (removed > 0) {
  1027. await execFileNoThrowWithCwd(gitExe(), ['worktree', 'prune'], {
  1028. cwd: gitRoot,
  1029. })
  1030. logForDebugging(
  1031. `cleanupStaleAgentWorktrees: removed ${removed} stale worktree(s)`,
  1032. )
  1033. }
  1034. return removed
  1035. }
  1036. /**
  1037. * Check whether a worktree has uncommitted changes or new commits since creation.
  1038. * Returns true if there are uncommitted changes (dirty working tree), if commits
  1039. * were made on the worktree branch since `headCommit`, or if git commands fail
  1040. * — callers use this to decide whether to remove a worktree, so fail-closed.
  1041. */
  1042. export async function hasWorktreeChanges(
  1043. worktreePath: string,
  1044. headCommit: string,
  1045. ): Promise<boolean> {
  1046. const { code: statusCode, stdout: statusOutput } =
  1047. await execFileNoThrowWithCwd(gitExe(), ['status', '--porcelain'], {
  1048. cwd: worktreePath,
  1049. })
  1050. if (statusCode !== 0) {
  1051. return true
  1052. }
  1053. if (statusOutput.trim().length > 0) {
  1054. return true
  1055. }
  1056. const { code: revListCode, stdout: revListOutput } =
  1057. await execFileNoThrowWithCwd(
  1058. gitExe(),
  1059. ['rev-list', '--count', `${headCommit}..HEAD`],
  1060. { cwd: worktreePath },
  1061. )
  1062. if (revListCode !== 0) {
  1063. return true
  1064. }
  1065. if (parseInt(revListOutput.trim(), 10) > 0) {
  1066. return true
  1067. }
  1068. return false
  1069. }
  1070. /**
  1071. * Fast-path handler for --worktree --tmux.
  1072. * Creates the worktree and execs into tmux running Claude inside.
  1073. * This is called early in cli.tsx before loading the full CLI.
  1074. */
  1075. export async function execIntoTmuxWorktree(args: string[]): Promise<{
  1076. handled: boolean
  1077. error?: string
  1078. }> {
  1079. // Check platform - tmux doesn't work on Windows
  1080. if (process.platform === 'win32') {
  1081. return {
  1082. handled: false,
  1083. error: 'Error: --tmux is not supported on Windows',
  1084. }
  1085. }
  1086. // Check if tmux is available
  1087. const tmuxCheck = spawnSync('tmux', ['-V'], { encoding: 'utf-8' })
  1088. if (tmuxCheck.status !== 0) {
  1089. const installHint =
  1090. process.platform === 'darwin'
  1091. ? 'Install tmux with: brew install tmux'
  1092. : 'Install tmux with: sudo apt install tmux'
  1093. return {
  1094. handled: false,
  1095. error: `Error: tmux is not installed. ${installHint}`,
  1096. }
  1097. }
  1098. // Parse worktree name and tmux mode from args
  1099. let worktreeName: string | undefined
  1100. let forceClassicTmux = false
  1101. for (let i = 0; i < args.length; i++) {
  1102. const arg = args[i]
  1103. if (!arg) continue
  1104. if (arg === '-w' || arg === '--worktree') {
  1105. // Check if next arg exists and isn't another flag
  1106. const next = args[i + 1]
  1107. if (next && !next.startsWith('-')) {
  1108. worktreeName = next
  1109. }
  1110. } else if (arg.startsWith('--worktree=')) {
  1111. worktreeName = arg.slice('--worktree='.length)
  1112. } else if (arg === '--tmux=classic') {
  1113. forceClassicTmux = true
  1114. }
  1115. }
  1116. // Check if worktree name is a PR reference
  1117. let prNumber: number | null = null
  1118. if (worktreeName) {
  1119. prNumber = parsePRReference(worktreeName)
  1120. if (prNumber !== null) {
  1121. worktreeName = `pr-${prNumber}`
  1122. }
  1123. }
  1124. // Generate a slug if no name provided
  1125. if (!worktreeName) {
  1126. const adjectives = ['swift', 'bright', 'calm', 'keen', 'bold']
  1127. const nouns = ['fox', 'owl', 'elm', 'oak', 'ray']
  1128. const adj = adjectives[Math.floor(Math.random() * adjectives.length)]
  1129. const noun = nouns[Math.floor(Math.random() * nouns.length)]
  1130. const suffix = Math.random().toString(36).slice(2, 6)
  1131. worktreeName = `${adj}-${noun}-${suffix}`
  1132. }
  1133. // worktreeName is joined into worktreeDir via path.join below; apply the
  1134. // same allowlist used by the in-session worktree tool so the constraint
  1135. // holds uniformly regardless of entry point.
  1136. try {
  1137. validateWorktreeSlug(worktreeName)
  1138. } catch (e) {
  1139. return {
  1140. handled: false,
  1141. error: `Error: ${(e as Error).message}`,
  1142. }
  1143. }
  1144. // Mirror createWorktreeForSession(): hook takes precedence over git so the
  1145. // WorktreeCreate hook substitutes the VCS backend for this fast-path too
  1146. // (anthropics/claude-code#39281). Git path below runs only when no hook.
  1147. let worktreeDir: string
  1148. let repoName: string
  1149. if (hasWorktreeCreateHook()) {
  1150. try {
  1151. const hookResult = await executeWorktreeCreateHook(worktreeName)
  1152. worktreeDir = hookResult.worktreePath
  1153. } catch (error) {
  1154. return {
  1155. handled: false,
  1156. error: `Error: ${errorMessage(error)}`,
  1157. }
  1158. }
  1159. repoName = basename(findCanonicalGitRoot(getCwd()) ?? getCwd())
  1160. // biome-ignore lint/suspicious/noConsole: intentional console output
  1161. console.log(`Using worktree via hook: ${worktreeDir}`)
  1162. } else {
  1163. // Get main git repo root (resolves through worktrees)
  1164. const repoRoot = findCanonicalGitRoot(getCwd())
  1165. if (!repoRoot) {
  1166. return {
  1167. handled: false,
  1168. error: 'Error: --worktree requires a git repository',
  1169. }
  1170. }
  1171. repoName = basename(repoRoot)
  1172. worktreeDir = worktreePathFor(repoRoot, worktreeName)
  1173. // Create or resume worktree
  1174. try {
  1175. const result = await getOrCreateWorktree(
  1176. repoRoot,
  1177. worktreeName,
  1178. prNumber !== null ? { prNumber } : undefined,
  1179. )
  1180. if (!result.existed) {
  1181. // biome-ignore lint/suspicious/noConsole: intentional console output
  1182. console.log(
  1183. `Created worktree: ${worktreeDir} (based on ${result.baseBranch})`,
  1184. )
  1185. await performPostCreationSetup(repoRoot, worktreeDir)
  1186. }
  1187. } catch (error) {
  1188. return {
  1189. handled: false,
  1190. error: `Error: ${errorMessage(error)}`,
  1191. }
  1192. }
  1193. }
  1194. // Sanitize for tmux session name (replace / and . with _)
  1195. const tmuxSessionName =
  1196. `${repoName}_${worktreeBranchName(worktreeName)}`.replace(/[/.]/g, '_')
  1197. // Build new args without --tmux and --worktree (we're already in the worktree)
  1198. const newArgs: string[] = []
  1199. for (let i = 0; i < args.length; i++) {
  1200. const arg = args[i]
  1201. if (!arg) continue
  1202. if (arg === '--tmux' || arg === '--tmux=classic') continue
  1203. if (arg === '-w' || arg === '--worktree') {
  1204. // Skip the flag and its value if present
  1205. const next = args[i + 1]
  1206. if (next && !next.startsWith('-')) {
  1207. i++ // Skip the value too
  1208. }
  1209. continue
  1210. }
  1211. if (arg.startsWith('--worktree=')) continue
  1212. newArgs.push(arg)
  1213. }
  1214. // Get tmux prefix for user guidance
  1215. let tmuxPrefix = 'C-b' // default
  1216. const prefixResult = spawnSync('tmux', ['show-options', '-g', 'prefix'], {
  1217. encoding: 'utf-8',
  1218. })
  1219. if (prefixResult.status === 0 && prefixResult.stdout) {
  1220. const match = prefixResult.stdout.match(/prefix\s+(\S+)/)
  1221. if (match?.[1]) {
  1222. tmuxPrefix = match[1]
  1223. }
  1224. }
  1225. // Check if tmux prefix conflicts with Claude keybindings
  1226. // Claude binds: ctrl+b (task:background), ctrl+c, ctrl+d, ctrl+t, ctrl+o, ctrl+r, ctrl+s, ctrl+g, ctrl+e
  1227. const claudeBindings = [
  1228. 'C-b',
  1229. 'C-c',
  1230. 'C-d',
  1231. 'C-t',
  1232. 'C-o',
  1233. 'C-r',
  1234. 'C-s',
  1235. 'C-g',
  1236. 'C-e',
  1237. ]
  1238. const prefixConflicts = claudeBindings.includes(tmuxPrefix)
  1239. // Set env vars for the inner Claude to display tmux info in welcome message
  1240. const tmuxEnv = {
  1241. ...process.env,
  1242. CLAUDE_CODE_TMUX_SESSION: tmuxSessionName,
  1243. CLAUDE_CODE_TMUX_PREFIX: tmuxPrefix,
  1244. CLAUDE_CODE_TMUX_PREFIX_CONFLICTS: prefixConflicts ? '1' : '',
  1245. }
  1246. // Check if session already exists
  1247. const hasSessionResult = spawnSync(
  1248. 'tmux',
  1249. ['has-session', '-t', tmuxSessionName],
  1250. { encoding: 'utf-8' },
  1251. )
  1252. const sessionExists = hasSessionResult.status === 0
  1253. // Check if we're already inside a tmux session
  1254. const isAlreadyInTmux = Boolean(process.env.TMUX)
  1255. // Use tmux control mode (-CC) for native iTerm2 tab/pane integration
  1256. // This lets users use iTerm2's UI instead of learning tmux keybindings
  1257. // Use --tmux=classic to force traditional tmux even in iTerm2
  1258. // Control mode doesn't make sense when already in tmux (would need to switch-client)
  1259. const useControlMode = isInITerm2() && !forceClassicTmux && !isAlreadyInTmux
  1260. const tmuxGlobalArgs = useControlMode ? ['-CC'] : []
  1261. // Print hint about iTerm2 preferences when using control mode
  1262. if (useControlMode && !sessionExists) {
  1263. const y = chalk.yellow
  1264. // biome-ignore lint/suspicious/noConsole: intentional user guidance
  1265. console.log(
  1266. `\n${y('╭─ iTerm2 Tip ────────────────────────────────────────────────────────╮')}\n` +
  1267. `${y('│')} To open as a tab instead of a new window: ${y('│')}\n` +
  1268. `${y('│')} iTerm2 > Settings > General > tmux > "Tabs in attaching window" ${y('│')}\n` +
  1269. `${y('╰─────────────────────────────────────────────────────────────────────╯')}\n`,
  1270. )
  1271. }
  1272. // For ants in claude-cli-internal, set up dev panes (watch + start)
  1273. const isAnt = process.env.USER_TYPE === 'ant'
  1274. const isClaudeCliInternal = repoName === 'claude-cli-internal'
  1275. const shouldSetupDevPanes = isAnt && isClaudeCliInternal && !sessionExists
  1276. if (shouldSetupDevPanes) {
  1277. // Create detached session with Claude in first pane
  1278. spawnSync(
  1279. 'tmux',
  1280. [
  1281. 'new-session',
  1282. '-d', // detached
  1283. '-s',
  1284. tmuxSessionName,
  1285. '-c',
  1286. worktreeDir,
  1287. '--',
  1288. process.execPath,
  1289. ...newArgs,
  1290. ],
  1291. { cwd: worktreeDir, env: tmuxEnv },
  1292. )
  1293. // Split horizontally and run watch
  1294. spawnSync(
  1295. 'tmux',
  1296. ['split-window', '-h', '-t', tmuxSessionName, '-c', worktreeDir],
  1297. { cwd: worktreeDir },
  1298. )
  1299. spawnSync(
  1300. 'tmux',
  1301. ['send-keys', '-t', tmuxSessionName, 'bun run watch', 'Enter'],
  1302. { cwd: worktreeDir },
  1303. )
  1304. // Split vertically and run start
  1305. spawnSync(
  1306. 'tmux',
  1307. ['split-window', '-v', '-t', tmuxSessionName, '-c', worktreeDir],
  1308. { cwd: worktreeDir },
  1309. )
  1310. spawnSync('tmux', ['send-keys', '-t', tmuxSessionName, 'bun run start'], {
  1311. cwd: worktreeDir,
  1312. })
  1313. // Select the first pane (Claude)
  1314. spawnSync('tmux', ['select-pane', '-t', `${tmuxSessionName}:0.0`], {
  1315. cwd: worktreeDir,
  1316. })
  1317. // Attach or switch to the session
  1318. if (isAlreadyInTmux) {
  1319. // Switch to sibling session (avoid nesting)
  1320. spawnSync('tmux', ['switch-client', '-t', tmuxSessionName], {
  1321. stdio: 'inherit',
  1322. })
  1323. } else {
  1324. // Attach to the session
  1325. spawnSync(
  1326. 'tmux',
  1327. [...tmuxGlobalArgs, 'attach-session', '-t', tmuxSessionName],
  1328. {
  1329. stdio: 'inherit',
  1330. cwd: worktreeDir,
  1331. },
  1332. )
  1333. }
  1334. } else {
  1335. // Standard behavior: create or attach
  1336. if (isAlreadyInTmux) {
  1337. // Already in tmux - create detached session, then switch to it (sibling)
  1338. // Check if session already exists first
  1339. if (sessionExists) {
  1340. // Just switch to existing session
  1341. spawnSync('tmux', ['switch-client', '-t', tmuxSessionName], {
  1342. stdio: 'inherit',
  1343. })
  1344. } else {
  1345. // Create new detached session
  1346. spawnSync(
  1347. 'tmux',
  1348. [
  1349. 'new-session',
  1350. '-d', // detached
  1351. '-s',
  1352. tmuxSessionName,
  1353. '-c',
  1354. worktreeDir,
  1355. '--',
  1356. process.execPath,
  1357. ...newArgs,
  1358. ],
  1359. { cwd: worktreeDir, env: tmuxEnv },
  1360. )
  1361. // Switch to the new session
  1362. spawnSync('tmux', ['switch-client', '-t', tmuxSessionName], {
  1363. stdio: 'inherit',
  1364. })
  1365. }
  1366. } else {
  1367. // Not in tmux - create and attach (original behavior)
  1368. const tmuxArgs = [
  1369. ...tmuxGlobalArgs,
  1370. 'new-session',
  1371. '-A', // Attach if exists, create if not
  1372. '-s',
  1373. tmuxSessionName,
  1374. '-c',
  1375. worktreeDir,
  1376. '--', // Separator before command
  1377. process.execPath,
  1378. ...newArgs,
  1379. ]
  1380. spawnSync('tmux', tmuxArgs, {
  1381. stdio: 'inherit',
  1382. cwd: worktreeDir,
  1383. env: tmuxEnv,
  1384. })
  1385. }
  1386. }
  1387. return { handled: true }
  1388. }