Skip to content

feat(terminal): create worktree from agent terminal and transfer session#4767

Merged
gregpriday merged 2 commits intodevelopfrom
feature/issue-4756-create-worktree-agent-terminal
Apr 1, 2026
Merged

feat(terminal): create worktree from agent terminal and transfer session#4767
gregpriday merged 2 commits intodevelopfrom
feature/issue-4756-create-worktree-agent-terminal

Conversation

@gregpriday
Copy link
Copy Markdown
Collaborator

Summary

  • Adds a Move to New Worktree action for agent panels that opens the full NewWorktreeDialog flow, then gracefully shuts down the current agent session and restarts it in the new worktree with session resume args
  • Agents without resume support (e.g. Cursor) get a degraded-but-functional path: the worktree is created and a fresh agent session opens there, with a visual indicator that the session won't carry over
  • The shutdown-to-resume pipeline reuses the existing gracefulShutdown / agentSessionId / buildResumeCommand logic already present in the agent registry; no new shutdown mechanics needed

Resolves #4756

Changes

  • electron/ipc/channels.ts + handlers/terminal/lifecycle.ts + preload.cts: new terminal.getWorkingDirectory IPC call so the renderer can read a PTY's cwd before shutdown
  • src/store/slices/terminalRegistry/restart.ts: moveToNewWorktree thunk that sequences graceful shutdown, session ID capture, worktree creation trigger, and agent restart in the target directory
  • shared/types/actions.ts + src/services/actions/definitions/terminalSpawnActions.ts: terminal.moveToNewWorktree action wired up
  • src/components/Panel/PanelHeader.tsx + src/components/Terminal/TerminalContextMenu.tsx: menu entry exposed in both the three-dots dropdown and the right-click context menu, only shown for agent panels
  • src/components/Worktree/NewWorktreeDialog.tsx: minor prop additions to support being triggered programmatically with a callback
  • src/store/worktreeStore.ts: onWorktreeCreated callback slot so the terminal store can hook into post-creation
  • src/store/slices/__tests__/worktreeStore.onCreated.test.ts: unit tests covering the callback registration and invocation

Testing

Unit tests added for the onWorktreeCreated callback path. The existing restart and graceful-shutdown unit tests cover the underlying mechanics. Ran npm run check clean (zero errors, warnings are pre-existing).

- Add TERMINAL_GRACEFUL_KILL IPC channel exposing PtyClient.gracefulKill to renderer
- Add gracefulKill to preload, ElectronAPI types, and terminalClient
- Add terminal.moveToNewWorktree ActionId and action definition
- Add onCreated callback to CreateDialogState in worktreeStore
- Update NewWorktreeDialog to pass worktreeId to onWorktreeCreated callback
- Wire onCreated callback through App.tsx NewWorktreeDialog integration
- Add moveToNewWorktreeAndTransfer store action in restart.ts
- Add "Move to New Worktree…" menu item in PanelHeader dropdown (agent panels)
- Add "Move to New Worktree…" item in TerminalContextMenu (agent panels)
- Add unit tests for worktreeStore onCreated callback lifecycle
- Fix test type annotation (remove invalid ReturnType wrapper)
- Fix stale closure in error handler (use newCwd instead of terminal.cwd)
- Add .catch() to dynamic import chain for error visibility
- Fix test GitHubPR mock to match interface (OPEN state, GitHubUser author)
- Fix indentation in moveToNewWorktreeAndTransfer
@gregpriday gregpriday force-pushed the feature/issue-4756-create-worktree-agent-terminal branch from dbea737 to dbe9376 Compare April 1, 2026 12:24
@gregpriday gregpriday merged commit 7c66753 into develop Apr 1, 2026
@gregpriday gregpriday deleted the feature/issue-4756-create-worktree-agent-terminal branch April 1, 2026 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create worktree from agent terminal and transfer session

1 participant