feat(cli): add --worktree flag to create/reuse a git worktree for the TUI - #12809
Conversation
Code Review SummaryStatus: 6 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
SUGGESTION
Files Reviewed (5 files)Incremental review of commits since
Fix these issues in Kilo Cloud Previous Review Summaries (5 snapshots, latest commit 7c25f5b)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit 7c25f5b)Status: 3 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)SUGGESTION
Files Reviewed (3 files)Incremental review of commits since
Fix these issues in Kilo Cloud Previous review (commit 90989d4)Status: 3 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)SUGGESTION
Files Reviewed (7 files)
Fix these issues in Kilo Cloud Previous review (commit 627e24d)Status: 4 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
SUGGESTION
Files Reviewed (5 files)
Fix these issues in Kilo Cloud Previous review (commit 0d78791)Status: 5 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
SUGGESTION
Resolved since last review
Files Reviewed (5 files)
Fix these issues in Kilo Cloud Previous review (commit 907f7df)Status: 6 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
SUGGESTION
Files Reviewed (3 files)
Reviewed by kimi-k3 · Input: 67.5K · Output: 13.3K · Cached: 880.4K Review guidance: REVIEW.md from base branch |
marius-kilocode
left a comment
There was a problem hiding this comment.
We should rather focus on something that upstream already does to ensure no duplicate implementation.
|
@marius-kilocode Can you clarify what you have in mind regarding the reuse of upstream features/code? I looked around a bit and couldn't find a |
|
@marius-kilocode For more context, I checked kilocode-legacy. The worktree feature existed there in two generations, and neither depended on upstream opencode (legacy predates the opencode fork entirely, so there was no "upstream" to duplicate against at the time):
On upstream duplication specifically: upstream What I did about the real overlap (Agent Manager vs. this CLI flag): I made the CLI's I could not make them share the literal same code path in this PR: |
# Conflicts: # packages/opencode/src/worktree/index.ts
Manual Test Plan — PR #12809:
|
…into feat/worktree-for-cli
…into feat/worktree-for-cli
Agreed to merge this in as is, and have obtained approval from another colleague.
feat(cli): add --worktree flag to create/reuse a git worktree for the TUI
Issue
https://docs.google.com/document/d/1eXKc_A9f_mVuc3LiniP3gbLWm7Ic_cPe58z1Tq_ZlzY/edit?pli=1&tab=t.0#heading=h.t3eix4ujj88t
Context
kilohad no way to open the TUI directly inside an isolatedgit worktree, no way to manage those worktrees from the CLI or TUI once created, and resuming a session always assumed you were already in (or navigated back into) the directory it was created in. This addskilo --worktree <name>to spin up (or reuse) a worktree and launch straight into it,kilo worktree create/list/removeas non-interactive counterparts for scripting, a/worktreealias in the TUI to list/remove worktrees, and makes explicit--session <id>resume follow the session back into its original worktree when possible.Implementation
kilo --worktree <name>: added to the default TUI command ($0 [project]). Beforeprocess.chdir, the resolved directory now goes throughresolveTuiDirectory(args, root)in the new Kilo-ownedsrc/kilocode/cli/cmd/tui-worktree.ts, keeping the sharedtui.tsdiff to a single option registration, one lazy import, and one small integration block..kilo/worktrees/<slug>inside the primary checkout — matching where the VS Code extension's Agent Manager (WorktreeManager.ts) already places its own worktrees — with a plain<slug>branch name (noopencode/prefix), and a.git/info/excludeentry so the directory doesn't show up ingit status.Worktree.Service(src/worktree/index.ts) — no new git plumbing:git worktree prune). Reclaims a dead registration (and its branch) before recreating under the same name.git worktree prune, which drops the registration but never deletes the branch) usinggit branch -d— never-D— so it can never silently discard unmerged work from an interrupted worktree or an unrelated branch that happens to share the name; surfaces git's own refusal instead..kilo/worktrees/<name>inside it and branch off the wrongHEAD.worktree.setup.readyevent (packages/schema/src/worktree-event.ts, emitted fromworktree/index.ts) that fires once the project's start script finishes, not just checkout — the CLI waits for this before returning, with a 10-minute timeout that's cancelled if creation itself rejects, so a failed creation exits immediately instead of leaving a dangling timer.kilo worktree create/list/remove(newsrc/kilocode/cli/cmd/worktree.ts, registered via the existingKiloCli.register()hook — zero shared-file diff for this part):createreusesresolveWorktreedirectly, so it gets all of the above (idempotent reuse, stale-registration reclaim, safe branch handling, nested-worktree rejection) for free.list/removematch by name or directory basename againstWorktree.Service.list()./worktree//worktrees: added as slash aliases on the existing (experimental,KILO_EXPERIMENTAL_WORKSPACES-gated) workspaces dialog, since worktrees are already listed/removable there via the sameWorktree.Service.--session <id>is passed without--project, the session's storeddirectoryis looked up via a lightweight, disposableDatabase-only layer (not the fullAppRuntime/instance graph, since--sessionis a common flag and shouldn't pay for bootstrapping Plugin/LSP/MCP/Provider/etc just for this) and used instead of the cwd if it still exists on disk. Any lookup failure falls back silently to the current directory so normal session validation still reports the real error.@kilocode/cli, minor).Screenshots / Video
How to Test
Manual/local verification
bun run typecheck,bun run lint,bun test, andbun run script/check-opencode-annotations.ts— all clean (agent-executed).kilo --worktree feature-x: created.kilo/worktrees/feature-xon branchfeature-x, waited for the setup-ready event, and launched the TUI rooted there. Re-running reused the existing worktree.git worktree prune(leaving a stale, unmerged branch): re-running with the same name correctly refused (Branch "feature-x" already exists... not fully merged) instead of discarding the commit; after removing the branch normally, re-running recreated it under the same name.kilo --worktree "!!!"(name that slugifies to empty): failed fast withInvalid worktree name "!!!".--worktree <name>from inside another worktree: correctly refused with a message pointing at the primary checkout.kilo worktree create/list/remove: full cycle verified against the real binary, including reuse and post-remove git state (git worktree list/git branch --listshow no leftovers).kilo --session <id>from a different directory: correctly resumed in the session's original worktree.Reviewer test steps
kilo --worktree my-feature..kilo/worktrees/my-feature.kilo worktree listandkilo worktree remove my-feature; confirm it's gone fromgit worktree list.kilo --session <that-session-id>; confirm it resumes in the worktree directory rather than the main checkout.KILO_EXPERIMENTAL_WORKSPACES=1) In the TUI, type/worktreeand confirm it opens the workspaces dialog listing worktrees.