Files
orca/src/main/ipc/worktree-folder-rename-target.ts
T
Trevin ChowandJinjing 7361bff698 feat: rename worktree folder to match branch on first work (#4743)
* feat: rename worktree folder to match branch on first work

When the first agent message auto-renames a freshly created creature branch to a
short, work-derived name, also align the on-disk worktree folder and the sidebar
display name with it. Re-key every worktree-scoped slice of state — renderer
store maps plus the persisted main-process state — through the resulting id
change so the live worktree survives the rename instead of being treated as a
deletion (its tabs, terminals, browser panes, and git status all follow).

The rename is best-effort and local-only: a skip or failure (remote runtime,
Windows lock, destination taken) leaves the folder as-is and never undoes the
branch/display rename that already landed.

Squashed from the original PR #4743 commits, rebased onto upstream/main to drop
accumulated merge commits and i18n formatting churn so the branch carries only
the feature diff:
- Rename worktree folder to match branch on first work
- migrate renamed worktree session ids
- keep the live worktree alive through a folder rename
- address PR review feedback

* Address PR review feedback (#4743)

- orca-runtime: emit in-process worktreesChanged client event on folder rename, mirroring notifyBranchRenamed so onClientEvent listeners aren't left stale
- worktrees: re-key rightSidebarExplorerViewByWorktree and activeWorkspaceKey through a worktree-identity rename (both were worktree-scoped but missed by buildWorktreeRenameState)
- branch-name-from-work: treat prefix-only model output as an empty slug so the caller skips the rename instead of producing a doubled prefix
- worktree-folder-rename-target: document why posix.dirname is safe (Windows filtered out earlier)
- tests for each of the above

* Extract branch rename test helpers to a separate harness file

Move git responders, mock builders, and test event fixtures out of
first-work-branch-rename.test.ts into a new test harness file. This
reduces file length and removes the max-lines ESLint disable directive,
complying with project style guidelines.

* Wrap entire OnboardingFlow in TooltipProvider

Enable the use of tooltips anywhere within the onboarding flow, rather
than restricting them to the step indicators.

---------

Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
2026-06-15 19:58:55 -07:00

55 lines
2.1 KiB
TypeScript

import { posix } from 'path'
import type { GlobalSettings } from '../../shared/types'
import { WORKTREE_ID_SEPARATOR } from '../../shared/worktree-id'
import { computeWorktreePath } from './worktree-logic'
type WorktreePathSettings = Pick<GlobalSettings, 'nestWorkspaces' | 'workspaceDir'>
export type WorktreeFolderRenamePlan = {
oldPath: string
newPath: string
/** `${repoId}::${newPath}` — what a worktree refresh reports post-move. */
newWorktreeId: string
}
/**
* Decide whether (and where) to rename a worktree's on-disk folder so it matches
* the work-derived branch leaf. Returns null to skip — degrading to "branch +
* display renamed, folder kept" — when the move would be unsafe or pointless:
* remote (SSH folder moves aren't mirrored), Windows (the OS locks a directory
* that is the running agent's cwd), the name already matches, or current path
* settings would relocate the worktree to a different parent rather than rename
* it in place. newWorktreeId is built from the same path passed to the move, so
* it matches the id git reports back after `git worktree move`.
*/
export function planWorktreeFolderRename(args: {
repoId: string
repoPath: string
oldWorktreePath: string
newLeaf: string
settings: WorktreePathSettings
platform: NodeJS.Platform
isRemote: boolean
}): WorktreeFolderRenamePlan | null {
if (args.isRemote || args.platform === 'win32') {
return null
}
const newPath = computeWorktreePath(args.newLeaf, args.repoPath, args.settings)
if (!newPath || newPath === args.oldWorktreePath) {
return null
}
// Why: keep this a pure rename. If path settings changed since creation the
// computed target could sit under a different parent — moving there would
// relocate, not rename, so skip rather than surprise the user.
// posix.dirname is safe here: win32 is filtered out above, so every remaining
// path (remote/Linux/Mac) uses forward slashes.
if (posix.dirname(newPath) !== posix.dirname(args.oldWorktreePath)) {
return null
}
return {
oldPath: args.oldWorktreePath,
newPath,
newWorktreeId: `${args.repoId}${WORKTREE_ID_SEPARATOR}${newPath}`
}
}