feat(worktree): add git worktree pool management for parallel agents#289
Open
whitelonng wants to merge 2 commits into
Open
feat(worktree): add git worktree pool management for parallel agents#289whitelonng wants to merge 2 commits into
whitelonng wants to merge 2 commits into
Conversation
Adds a Git worktree pool (max 3) so multiple agents can work on the same repository on isolated branches without interfering with each other. This is the infrastructure layer: pool management + Settings UI. Agent-runtime integration (auto acquire/release on task lifecycle) is left for a follow-up. Main process (src/main/services/worktree-service.ts): - acquireWorktree: create or reset (git reset --hard + git clean -ffd) a pool worktree; emits WORKTREE_HAS_CHANGES error if dirty and not forced (matches TalkCody's protocol) - listWorktrees / removeWorktree / getWorktreeChanges / commitWorktree - mergeWorktreeToMain (ff-first, then merge commit, conflict detection) - syncWorktreeFromMain (rebase with auto-stash) - abortMerge / continueMerge / abortRebase / cleanupWorktrees - findAvailablePoolIndex - Reuses Kun's runGit() (now exported) for all git invocation; no git2/nodegit dependency, pure git CLI IPC layer: 12 ipcMain.handle channels (worktree:acquire/release/list/ remove/changes/commit/merge/abort-merge/continue-merge/sync/abort-rebase/ cleanup/find-available) with zod-validated payloads. Preload + kun-gui-api: full type-safe bridge. Renderer: - Zustand store (worktree-store.ts) - Settings section with pool overview, per-pool cards (create/release/ merge/sync/remove), conflict file listing, and cleanup-all - 21 i18n keys (en/zh)
…tash warnings Three safety fixes from code review: 1. mergeWorktreeToMain: refuse to checkout main in the user's main repo. Previously it silently switched the user's current branch to main and never switched back. Now it checks if the user is already on main; if not, returns an error instructing them to switch first. 2. acquireWorktree force path: clean -ffd → clean -fd. The double -f would delete nested git repos (submodules, CMake deps), which is destructive and surprising. Single -f removes untracked files/dirs only. 3. syncWorktreeFromMain: stash pop conflicts and non-conflict failures now return informative messages telling the user the stash is still on the stack and they need to run `git stash pop` manually.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a Git worktree pool (max 3) so multiple agents can work on the same repository on isolated branches without interfering with each other. This PR delivers the infrastructure + management UI; agent-runtime auto-integration (acquire on task start, release on task end, conflict dialog) is intentionally left for a follow-up to keep this PR reviewable.
Architecture
All git operations use the git CLI (no
git2/nodegitdependency) via Kun's existingrunGit()helper, which is now exported fromgit-service.ts.What's included
Main process (
src/main/services/worktree-service.ts— new):acquireWorktree— create or force-reset (git reset --hard+git clean -ffd) a pool worktree. Emits aWORKTREE_HAS_CHANGES:{poolIndex}:{count}error if the worktree is dirty andforceis not set — the renderer parses this to show a confirmation dialog.listWorktrees/removeWorktree/getWorktreeChanges/commitWorktreemergeWorktreeToMain— fast-forward first, then merge commit, with conflict detection (git diff --name-only --diff-filter=U)syncWorktreeFromMain— rebase from main with auto-stash (git stash push→git rebase→git stash pop), conflict detectionabortMerge/continueMerge/abortRebase/cleanupWorktrees/findAvailablePoolIndexWORKTREE_TASK_MAP)IPC layer: 12
ipcMain.handlechannels with zod-validated payloads (worktree:acquire,worktree:release,worktree:list,worktree:remove,worktree:changes,worktree:commit,worktree:merge,worktree:abort-merge,worktree:continue-merge,worktree:sync,worktree:abort-rebase,worktree:cleanup,worktree:find-available)Preload +
kun-gui-api.ts: full type-safe bridge with shared types insrc/shared/worktree.ts.Renderer:
worktree-store.ts)Key design decisions
runGitexported, not duplicated — Kun's existingrunGit()already handles C-locale forcing (critical for parsing git output) and timeout/buffer limits. Worktree reuses it directly.reset --hardbeforeclean -ffd— order matters: reset turns staged-new files back into untracked, then clean removes them. Reversed order leaves staged files behind.WORKTREE_HAS_CHANGESas a string protocol — matches the source project's approach; the error message is parsed by the renderer to show a "N uncommitted changes, force reset?" confirmation.~/.kun/{project-basename}/pool-{i}by default, configurable viaworktreeRoot.Verification
npm run typecheckpassesnpm run lint— 0 errors (9 pre-existing warnings unchanged)npx vitest run src/renderer/src/components— 262/262 passedNotes
findNearestGitRoot(which already handles.gitfiles for worktrees/submodules).