ce-worktree
everyinc/compound-engineering-plugin
设置隔离的 git 工作树。在开始独立工作时使用,或当 ce-work/ce-code-review 提供工作树选项时使用;首先检测现有的隔离状态。
...展开全部关于 ce-worktree
工作树隔离(Worktree Isolation)负责设置隔离的 Git 工作树,以便在不干扰用户主签出(main checkout)的情况下继续工作。由于大多数编码环境(coding harnesses)在会话开始时已经创建了工作树,该技能的主要任务是检测现有隔离状态,从而避免创建冗余的工作树。它遵循严格的操作顺序:检测现有隔离状态,优先使用原生工作树工具,仅在两者均不适用时才回退到纯 Git 命令。
检测机制通过比较解析后的绝对 Git 目录与解析后的绝对公共 Git 目录来实现。由于 Git 会根据当前目录混合使用绝对路径和相对路径形式,该技能会先将两者解析为绝对路径,而不是进行原始字符串比较,否则会导致错误的“已隔离”结果。当这两个路径不同时,它通过检查超级项目的工作树来区分链接的工作树与子模块,并在已处于隔离工作树内部时就地工作。当存在原生工作树原语(例如 EnterWorktree 工具、/worktree 命令或 --worktree 标志)时,它会使用这些原语并停止操作,因为后台静默执行的 git worktree add 会创建编码环境无法看到或清理的幻影状态。
Git 回退机制从仓库根目录运行,选择基于工作描述生成的有意义分支名称,确保 .worktrees/ 被 gitignore(通过检查带有尾部斜杠的路径以遵守仅针对目录的规则),尽力执行非致命的基线分支 fetch 操作,在 .worktrees/
常见问题解答
为什么该技能在创建工作树之前要检查现有隔离状态?
大多数编码环境默认在会话开始时创建工作树,因此常见情况是隔离状态已经存在。从现有工作树内部创建另一个工作树会导致进入错误的工作树,且对创建当前工作树的编码环境不可见。
它如何在检测过程中避免错误的“已隔离”结果?
它首先将 git 目录和公共 git 目录解析为绝对路径,然后进行比较,而不是进行原始字符串比较。Git 会根据当前目录返回混合的绝对和相对形式,否则会导致假阳性结果。
何时应使用原生工作树工具而不是纯 Git?
每当编码环境提供原生原语(如 EnterWorktree/WorktreeCreate 工具、/worktree 命令或 --worktree 标志)时,该技能会使用它并停止操作。原生工具负责放置、跟踪和清理工作树,以便编码环境可以管理它;后台静默执行的 git worktree add 会创建编码环境无法看到的幻影状态。
如果 git worktree add 因权限或沙箱错误而失败,会发生什么?
此失败要求在接触当前签出之前进行阻塞性的用户决策。该技能会报告错误并通过平台的问题工具(例如 Claude Code 中的 AskUserQuestion)询问用户,提供诸如在当前签出中工作或停止以解决问题的选项,仅在获得明确确认后才会继续在主签出中工作。
该技能如何确保工作树内容不会被提交?
在创建任何内容之前,它会通过运行带有尾部斜杠的 git check-ignore 来验证 .worktrees/ 是否被 gitignore,因此即使目录尚不存在,现有的仅针对目录的规则也会被遵守;如果需要,它还会将 .worktrees/ 行添加到 .gitignore 中。
Ensure the current work happens in an isolated workspace, without disturbing the user's main checkout. Most coding harnesses now create a worktree by default at session start, so the common case is that isolation already exists.
Done when: the caller is working in an isolated tree — existing or newly created — and its path and branch have been reported, or a blocker has been reported instead.
Order of operations: detect existing isolation -> prefer a native worktree tool -> fall back to plain git. Never create a worktree the harness cannot see.
Two modes, set by the caller's need:
- New work (default). No ref named — create a fresh branch from a base (trunk). This is what
ce-workandce-code-reviewuse when the user picks the worktree option. - Isolate an existing ref. The caller names a PR head, branch, or commit — attach the worktree to that ref instead of creating a new branch. A branch can be checked out in only one worktree at a time. If the named ref is already checked out anywhere (most commonly as the primary checkout's current branch), do not create a second worktree — report that it is already checked out at
<path>and let the caller act (work there in place; or, only if a clean separate tree is essential, create a detached worktree at the same commit).
Step 0: Detect existing isolation
Compare the resolved absolute git dir against the resolved absolute common git dir. Git mixes absolute and relative forms depending on the current directory (from a subdirectory of a normal checkout, --git-dir comes back absolute while --git-common-dir may be relative), so a raw string compare yields a false "already isolated":
git rev-parse --absolute-git-dir # absolute git dir for this worktree(cd "$(git rev-parse --git-common-dir)" && pwd -P) # absolute shared (common) git dir
Equal -> normal checkout; continue to Step 1.
Different -> a linked worktree or a submodule. Distinguish with git rev-parse --show-superproject-working-tree:
- Non-empty -> submodule; treat it as a normal checkout and continue to Step 1.
- Empty -> already isolated. Report the worktree path (
git rev-parse --show-toplevel) and current branch, then work in place — a worktree-from-worktree lands in the wrong tree and is invisible to the harness that made the current one. In isolate-an-existing-ref mode, check that ref out here (unless it is already current) rather than nesting a worktree.
Step 1: Prefer the harness's native worktree tool
If the harness provides a native worktree primitive — for example an EnterWorktree / WorktreeCreate tool, a /worktree command, or a --worktree flag — use it and stop. Native tools place, track, and clean up the worktree so the harness can manage it. A behind-the-back git worktree add creates phantom state the harness cannot see, navigate to, or clean up.
Step 2: Git fallback
Only when there is no native tool and Step 0 found no existing isolation.
- Run from the repo root:
cd "$(git rev-parse --show-toplevel)". The paths below are repo-root-relative, but the skill runs from the user's current directory — without this,.worktrees/<branch>and the.gitignoreedit land in a subdirectory (e.g.src/.worktrees/...). - Choose a meaningful branch name from the work description (e.g.
feat/login,fix/email-validation) — never an opaque auto-generated one. Base: origin's default branch, elsemain. - Ensure
.worktrees/is gitignored before creating anything:git check-ignore -q .worktrees/— with the trailing slash, so an existing directory-only.worktrees/rule is honored even before the directory exists (without the slash the probe misses it and dirties a correctly-configured repo). Not ignored -> add a.worktrees/line to.gitignore. - Refresh the base with
git fetch origin <from-branch>. This is non-fatal — nooriginremote, a differently-named remote, or a local-only branch is not an abort; continue with the local ref. - Create the worktree, per mode:
- New work:
git worktree add -b <branch-name> .worktrees/<branch-name> origin/<from-branch>(use the local<from-branch>ref iforigin/<from-branch>does not exist). - Existing branch or tag:
git worktree add .worktrees/<slug> <target-ref>. - PR: check it out on a local branch —
git fetch origin pull/<n>/head:pr-<n>thengit worktree add .worktrees/pr-<n> pr-<n>. Never a detachedFETCH_HEAD: that orphans the fix loop's commits instead of updating the PR. (For push-tracking back to the PR, create it detached —git worktree add --detach .worktrees/pr-<n>— thencdin and rungh pr checkout <n>, which is fork-safe.) - If git reports the ref is already checked out elsewhere, apply the one-branch-one-worktree rule above — do not force a second worktree.
- New work:
cdinto it, then report the path and branch.
If git worktree add fails with a sandbox or permission error, the requested isolation does not exist. Do not proceed in the current checkout — the user chose isolation specifically to avoid it. Report the failure and ask, offering options such as "work in the current checkout" vs "stop and resolve the permission issue", using the platform's blocking question tool: AskUserQuestion in Claude Code (call ToolSearch with select:AskUserQuestion first if its schema isn't loaded), request_user_input in Codex, ask_question in Antigravity CLI (agy), ask_user in Pi (via the pi-ask-user extension). Only when no blocking tool exists in the harness or the call errors, present the numbered options in chat and wait for the reply. Never skip the confirmation, and do not retry alternative paths automatically.





首页
