@maxiaochao/pi-worktree 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 2026-10-09 - v0.2.0
4
+
5
+ - add current-session worktree switching for users, Agents, and SDK Harnesses
6
+ - 新增 `/worktree start` 用户命令,在当前 Pi 界面中把持久会话 fork 并切换到 worktree。
7
+ - 新增 `enter_worktree` Agent Tool 和 `pi-worktree/switch-request` Harness 协议。
8
+ - 新增幂等的 `pi-worktree prepare --json` 机器接口,安全处理创建、挂载、复用、dirty 和 stale 状态。
9
+
3
10
  ## 2026-09-30 - v0.1.0
4
11
 
5
12
  - 将 pi-worktree CLI、bash completion 和 Pi skill 合并为可独立安装的子包。
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @maxiaochao/pi-worktree
2
2
 
3
- `pi-worktree` CLI 与配套 Pi skill。CLI 创建、检查和清理 Git worktree,并在隔离目录中启动 Pi。
3
+ `pi-worktree` CLI、Pi Extension 与配套 skill。它既能启动独立 Pi,也能让用户在当前界面切换 worktree,或让 Agent 向 SDK Harness 发出结构化切换请求。
4
4
 
5
5
  ![Worktree 在真实临时 Git 仓库中的运行截图](docs/screenshot.png)
6
6
 
@@ -14,16 +14,28 @@
14
14
 
15
15
  ```bash
16
16
  pi-worktree start feature -p "implement the feature"
17
+ pi-worktree prepare feature --json
17
18
  pi-worktree info feature
18
19
  pi-worktree out
19
20
  pi-worktree remove feature
20
21
  ```
21
22
 
22
- `start` 从当前分支创建或复用 `.worktrees/feature`,记录 base,并在该目录启动 Pi。
23
+ `start` 从当前分支创建或复用 `.worktrees/feature`,记录 base,并在该目录启动 Pi。`prepare` 复用同一创建逻辑,但只返回目标,不启动嵌套 Pi。
24
+
25
+ 在交互式 Pi 中,用户可以保留当前界面和完整对话:
26
+
27
+ ```text
28
+ /worktree start feature
29
+ /worktree start feature --base main
30
+ ```
31
+
32
+ 命令会把当前持久会话 fork 到目标 worktree,再通过 Pi 的公开 session replacement API 切换。临时的 `--no-session` 会话无法使用此入口。
33
+
34
+ Agent 使用 Extension 注册的 `enter_worktree` tool。它创建或复用目标后返回 `pi-worktree/switch-request` 结构化结果,并终止当前工具轮;兼容 Harness 应在 `agent_settled` 后读取 `sessionFile` 和 `path`,执行 `SessionManager.forkFrom(sessionFile, path)`,再把返回的新 session 文件交给 `runtime.switchSession()`,最后重新绑定 `runtime.session`。这样目标 cwd 会写入新 session header,重启后仍然有效。普通 Pi 没有宿主切换钩子时,Tool 只会生成请求;用户仍可使用 `/worktree start` 完成切换。
23
35
 
24
36
  ## 安装
25
37
 
26
- 完整工具包已经包含 CLI 和 skill:
38
+ 完整工具包已经包含 CLI、Extension 和 skill:
27
39
 
28
40
  ```bash
29
41
  pi install npm:@maxiaochao/pi-toolkit
@@ -50,6 +62,7 @@ pi update npm:@maxiaochao/pi-worktree # 更新独立子包
50
62
 
51
63
  ```bash
52
64
  pi-worktree start <name> [pi args...]
65
+ pi-worktree prepare [--base <ref>] [--json] <name>
53
66
  pi-worktree list
54
67
  pi-worktree info <name>
55
68
  pi-worktree out
@@ -57,4 +70,6 @@ pi-worktree remove <name>
57
70
  pi-worktree prune
58
71
  ```
59
72
 
73
+ `prepare --json` 是 Extension 和其他宿主使用的幂等机器接口,返回 `created`、`attached`、`reused` 或 `already-active`,以及绝对路径、分支和 dirty 状态。已有 worktree 会复用;已存在但未挂载的同名分支会挂载;stale 注册、非法名称和路径冲突会显式失败。
74
+
60
75
  Agent 用法见 `skills/pi-worktree/SKILL.md`。测试流程、夹具和结果见 [TESTING.md](TESTING.md)。
package/TESTING.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## 流程
4
4
 
5
- 1. `npm --prefix packages/worktree test`:在临时 Git 仓库中使用假的 `pi` 命令运行 CLI。
5
+ 1. `npm --prefix packages/worktree test`:在临时 Git 仓库中运行 CLI,并验证 Extension 注册和 session 切换协议。
6
6
  2. `npm pack ./packages/worktree --dry-run`:确认 CLI、completion、skill 和安装脚本进入 tarball。
7
7
  3. 手工验收发布包的 `pi-worktree start` 交互流程。
8
8
  4. 确认 README 示例、真实临时 Git 仓库输出 `docs/screenshot.png` 和 `docs/demo.gif` 进入 tarball。
@@ -11,6 +11,9 @@
11
11
 
12
12
  - `--help` 输出命令说明。
13
13
  - `start` 从当前分支创建 `.worktrees/<name>` 并记录 base。
14
+ - `prepare --json` 覆盖 created、reused、already-active、dirty 和非法名称安全失败。
15
+ - `/worktree start` 把历史 fork 到 cwd 正确的新 session,再调用 `switchSession`。
16
+ - `enter_worktree` 返回带 `pi-worktree/switch-request`、源 session、目标路径和 dirty 状态的 structured content,并请求终止当前工具轮。
14
17
  - 假 Pi 收到正确工作目录和参数。
15
18
  - `list` 标记主 checkout 和子 worktree。
16
19
  - `info` 报告 clean、branch、base 和 ahead。
@@ -18,4 +21,4 @@
18
21
 
19
22
  ## 最近验证结果
20
23
 
21
- 迁移基线要求全部 shell 断言通过,且 `npm pack --dry-run` 包含 `bin/`、`skills/` 和 `scripts/install.mjs`。
24
+ 验收要求全部 Node/shell 断言通过,Extension 能被 Pi 单独加载,且 `npm pack --dry-run` 包含 `bin/`、`extensions/`、`skills/` 和 `scripts/install.mjs`。
package/bin/pi-worktree CHANGED
@@ -4,6 +4,7 @@
4
4
  # Usage:
5
5
  # pi-worktree start <name> [pi args...] # enter worktree <name> and start pi
6
6
  # pi-worktree start --base <ref> <name> [pi args...] # branch from <ref> instead of current branch
7
+ # pi-worktree prepare [--base <ref>] [--json] <name> # create/reuse without launching pi
7
8
  # pi-worktree -l | --list # list worktrees (main checkout marked "main checkout")
8
9
  # pi-worktree -i <name> # show base commit, ahead/behind, unique commits
9
10
  # pi-worktree -o | --out # cd back to the main checkout
@@ -18,7 +19,7 @@ else
18
19
  C_BOLD='' C_DIM='' C_RED='' C_GREEN='' C_YELLOW='' C_CYAN='' C_RESET=''
19
20
  fi
20
21
 
21
- ok() { printf '%spi-worktree:%s %s\n' "$C_GREEN" "$C_RESET" "$*"; }
22
+ ok() { [[ "${QUIET:-0}" == 1 ]] || printf '%spi-worktree:%s %s\n' "$C_GREEN" "$C_RESET" "$*"; }
22
23
  warn() { printf '%s⚠️ %s%s\n' "$C_YELLOW" "$C_RESET" "$*"; }
23
24
  die() { printf '%spi-worktree: %s%s\n' "$C_RED" "$C_RESET" "$*" >&2; exit 1; }
24
25
 
@@ -40,6 +41,12 @@ in_linked_worktree() {
40
41
 
41
42
  wt_dir() { echo ".worktrees/$1"; }
42
43
 
44
+ validate_name() {
45
+ local name="$1"
46
+ [[ "$name" != */* ]] || die "worktree name must not contain '/': $name"
47
+ git check-ref-format --branch "$name" >/dev/null 2>&1 || die "invalid worktree/branch name: $name"
48
+ }
49
+
43
50
  # Best available base ref: current branch > HEAD (worktrees should branch
44
51
  # from where you actually are; a stale origin/main must not win)
45
52
  pick_base() {
@@ -63,12 +70,14 @@ resolve_base() {
63
70
  }
64
71
 
65
72
  ensure_gitignore() {
73
+ local root
74
+ root=$(main_checkout)
66
75
  # The trailing slash is load-bearing: `.worktrees/` is a directory-only
67
76
  # pattern, and git check-ignore cannot match it against a path that does not
68
77
  # exist yet — so `.worktrees` would look unignored on every first run and the
69
78
  # rule would be appended again.
70
- if ! git check-ignore -q .worktrees/ 2>/dev/null; then
71
- printf '.worktrees/\n' >> .gitignore
79
+ if ! git -C "$root" check-ignore -q .worktrees/ 2>/dev/null; then
80
+ printf '.worktrees/\n' >> "$root/.gitignore"
72
81
  ok "added .worktrees/ to .gitignore"
73
82
  fi
74
83
  }
@@ -226,55 +235,140 @@ cmd_remove() {
226
235
  fi
227
236
  }
228
237
 
229
- cd_wt() {
230
- local name="$1"; shift
238
+ prepare_wt() {
239
+ local name="$1"
231
240
  require_repo
241
+ validate_name "$name"
232
242
 
233
- # Already isolated? Just launch pi here (superpowers Step 0).
243
+ # A linked checkout cannot safely create another `.worktrees/` tree relative
244
+ # to itself. Re-entering the active branch is nevertheless idempotent.
234
245
  if in_linked_worktree; then
235
- ok "already in an isolated worktree ($(pwd)), launching pi here"
236
- exec pi "$@"
246
+ local current_branch
247
+ current_branch=$(git branch --show-current)
248
+ [[ "$current_branch" == "$name" ]] \
249
+ || die "already inside linked worktree '$current_branch'; return to the main checkout first"
250
+ PREPARE_STATUS="already-active"
251
+ PREPARE_DIR=$(pwd -P)
252
+ PREPARE_BRANCH="$current_branch"
253
+ PREPARE_DIRTY=false
254
+ if [[ -n "$(git status --porcelain)" ]]; then PREPARE_DIRTY=true; fi
255
+ return 0
237
256
  fi
238
257
 
239
- local dir; dir="$(wt_dir "$name")"
258
+ local root worktrees_dir dir
259
+ root=$(main_checkout)
260
+ worktrees_dir="$root/.worktrees"
261
+ dir="$worktrees_dir/$name"
262
+ [[ ! -L "$worktrees_dir" ]] || die "$worktrees_dir must be a real directory, not a symlink"
263
+ [[ ! -L "$dir" ]] || die "$dir must not be a symlink"
240
264
 
241
265
  if [[ -d "$dir" ]]; then
242
266
  # Reuse: must be a registered worktree for this repo.
243
267
  # Skip the dirty-workspace prompt — the worktree already exists, nothing new to create.
268
+ local registered_path
269
+ registered_path=$(git worktree list --porcelain | sed -n 's/^worktree //p' | grep -Fx "$dir" || true)
270
+ [[ -n "$registered_path" ]] || die "$dir exists but is not a registered git worktree"
244
271
  git -C "$dir" rev-parse --git-dir >/dev/null 2>&1 || die "$dir exists but is not a git worktree"
272
+ [[ "$(git -C "$dir" rev-parse --path-format=absolute --git-common-dir)" == "$(git rev-parse --path-format=absolute --git-common-dir)" ]] \
273
+ || die "$dir belongs to a different git repository"
274
+ [[ "$(git -C "$dir" branch --show-current)" == "$name" ]] \
275
+ || die "$dir is not checked out on expected branch '$name'"
245
276
  ensure_gitignore
246
277
  ok "reusing existing worktree $dir"
278
+ PREPARE_STATUS="reused"
247
279
  else
248
- # Dirty workspace: uncommitted changes do NOT follow into the new worktree.
249
- if ! git diff --quiet || ! git diff --cached --quiet || [[ -n "$(git ls-files --others --exclude-standard)" ]]; then
250
- warn "Uncommitted changes stay in THIS directory; they will NOT appear in the worktree:"
251
- git status --short | head -20
252
- read -rp "Continue? [y/N] " ans
253
- [[ "$ans" =~ ^[Yy]$ ]] || die "aborted"
254
- fi
255
-
256
- # After the check above, so the tool's own .gitignore edit is not reported
257
- # as (and does not prompt about) the user's uncommitted work.
280
+ local target_abs registered_path
281
+ target_abs="$dir"
282
+ registered_path=$(git worktree list --porcelain | sed -n 's/^worktree //p' | grep -Fx "$target_abs" || true)
283
+ [[ -z "$registered_path" ]] \
284
+ || die "stale worktree registration for $dir; run 'pi-worktree prune' first"
258
285
  ensure_gitignore
259
- mkdir -p .worktrees
286
+ mkdir -p "$worktrees_dir"
260
287
  # Reuse branch if it exists, else create it
261
288
  if git show-ref --verify --quiet "refs/heads/$name"; then
262
- git worktree add "$dir" "$name"
289
+ if [[ "${QUIET:-0}" == 1 ]]; then
290
+ git worktree add "$dir" "$name" >/dev/null
291
+ else
292
+ git worktree add "$dir" "$name"
293
+ fi
263
294
  ok "checked out existing branch '$name'"
295
+ PREPARE_STATUS="attached"
264
296
  # record a best-effort base for -i display
265
297
  local mb; mb=$(git merge-base main "$name" 2>/dev/null || true)
266
298
  [[ -n "$mb" ]] && git config "branch.$name.base" "$(git rev-parse --short "$mb")" \
267
299
  && git config "branch.$name.baseRef" main
268
300
  else
269
301
  local base; base=$(resolve_base)
270
- git worktree add "$dir" -b "$name" "$base"
302
+ if [[ "${QUIET:-0}" == 1 ]]; then
303
+ git worktree add "$dir" -b "$name" "$base" >/dev/null
304
+ else
305
+ git worktree add "$dir" -b "$name" "$base"
306
+ fi
271
307
  git config "branch.$name.base" "$(git rev-parse --short "$base")"
272
308
  git config "branch.$name.baseRef" "$base"
273
309
  ok "created branch '$name' from $base @ $(git rev-parse --short "$base")"
310
+ PREPARE_STATUS="created"
274
311
  fi
275
312
  fi
276
313
 
277
- cd "$dir"
314
+ PREPARE_DIR=$(cd "$dir" && pwd -P)
315
+ PREPARE_BRANCH=$(git -C "$dir" branch --show-current)
316
+ PREPARE_DIRTY=false
317
+ if [[ -n "$(git -C "$dir" status --porcelain)" ]]; then PREPARE_DIRTY=true; fi
318
+ }
319
+
320
+ print_prepare_json() {
321
+ PI_WORKTREE_STATUS="$PREPARE_STATUS" \
322
+ PI_WORKTREE_PATH="$PREPARE_DIR" \
323
+ PI_WORKTREE_BRANCH="$PREPARE_BRANCH" \
324
+ PI_WORKTREE_DIRTY="$PREPARE_DIRTY" \
325
+ node -e 'process.stdout.write(JSON.stringify({status:process.env.PI_WORKTREE_STATUS,path:process.env.PI_WORKTREE_PATH,branch:process.env.PI_WORKTREE_BRANCH,dirty:process.env.PI_WORKTREE_DIRTY==="true"})+"\n")'
326
+ }
327
+
328
+ cmd_prepare() {
329
+ BASE_OVERRIDE=""
330
+ local json=0
331
+ while [[ $# -gt 0 ]]; do
332
+ case "$1" in
333
+ --base)
334
+ shift; [[ $# -ge 1 ]] || die "usage: pi-worktree prepare [--base <ref>] [--json] <name>"
335
+ BASE_OVERRIDE="$1"; shift ;;
336
+ --json) json=1; QUIET=1; shift ;;
337
+ --*) die "unknown prepare option: $1" ;;
338
+ *) break ;;
339
+ esac
340
+ done
341
+ [[ $# -eq 1 ]] || die "usage: pi-worktree prepare [--base <ref>] [--json] <name>"
342
+ prepare_wt "$1"
343
+ if [[ "$json" == 1 ]]; then
344
+ print_prepare_json
345
+ else
346
+ ok "$PREPARE_STATUS worktree: $PREPARE_DIR ($PREPARE_BRANCH)"
347
+ fi
348
+ }
349
+
350
+ cd_wt() {
351
+ local name="$1"; shift
352
+ require_repo
353
+
354
+ # Already isolated? Preserve the original start behavior and launch Pi here.
355
+ if in_linked_worktree; then
356
+ ok "already in an isolated worktree ($(pwd)), launching pi here"
357
+ exec pi "$@"
358
+ fi
359
+
360
+ # Dirty workspace: uncommitted changes do NOT follow into the new worktree.
361
+ local dir; dir="$(main_checkout)/.worktrees/$name"
362
+ if [[ ! -d "$dir" ]] && { ! git diff --quiet || ! git diff --cached --quiet || [[ -n "$(git ls-files --others --exclude-standard)" ]]; }; then
363
+ warn "Uncommitted changes stay in THIS directory; they will NOT appear in the worktree:"
364
+ git status --short | head -20
365
+ read -rp "Continue? [y/N] " ans
366
+ [[ "$ans" =~ ^[Yy]$ ]] || die "aborted"
367
+ fi
368
+
369
+ prepare_wt "$name"
370
+
371
+ cd "$PREPARE_DIR"
278
372
  exec pi "$@"
279
373
  }
280
374
 
@@ -285,6 +379,7 @@ case "${1:-}" in
285
379
  -r|--remove|rm|remove) shift; cmd_remove "$@" ;;
286
380
  -p|--prune|prune) shift; subcmd_prune ;;
287
381
  -h|--help|help) sed -n '2,10p' "$0" | sed 's/^# \{0,1\}//' ;;
382
+ prepare) shift; cmd_prepare "$@" ;;
288
383
  start) shift
289
384
  BASE_OVERRIDE=""
290
385
  if [[ "${1:-}" == "--base" ]]; then
@@ -4,7 +4,7 @@ _pi_worktree() {
4
4
  cur="${COMP_WORDS[COMP_CWORD]}"
5
5
  first="${COMP_WORDS[1]}"
6
6
 
7
- local SUBS="start list ls info out remove rm prune -l --list -i --info -o --out -r --remove -p --prune -h --help"
7
+ local SUBS="start prepare list ls info out remove rm prune -l --list -i --info -o --out -r --remove -p --prune -h --help"
8
8
 
9
9
  # existing worktree names (only linked worktrees, i.e. under .worktrees/)
10
10
  local wts=""
@@ -23,7 +23,7 @@ _pi_worktree() {
23
23
  fi
24
24
 
25
25
  case "$first" in
26
- -i|--info|info|-r|--remove|remove|rm|start)
26
+ -i|--info|info|-r|--remove|remove|rm|start|prepare)
27
27
  # only the second word of info/remove completes worktree names;
28
28
  # later words offer nothing (info/remove take no further args)
29
29
  if (( COMP_CWORD == 2 )); then
@@ -0,0 +1,173 @@
1
+ import { fileURLToPath } from "node:url";
2
+ import { resolve } from "node:path";
3
+ import { unlinkSync } from "node:fs";
4
+ import { SessionManager, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
5
+ import { Type } from "typebox";
6
+
7
+ const CLI_PATH = fileURLToPath(new URL("../bin/pi-worktree", import.meta.url));
8
+
9
+ export const WORKTREE_SWITCH_REQUEST = "pi-worktree/switch-request";
10
+
11
+ export interface WorktreePrepareResult {
12
+ status: "created" | "attached" | "reused" | "already-active";
13
+ path: string;
14
+ branch: string;
15
+ dirty: boolean;
16
+ }
17
+
18
+ export interface WorktreeSwitchRequest extends WorktreePrepareResult {
19
+ kind: typeof WORKTREE_SWITCH_REQUEST;
20
+ version: 1;
21
+ action: "fork-and-switch";
22
+ sessionFile: string;
23
+ }
24
+
25
+ const SwitchRequestSchema = Type.Object({
26
+ kind: Type.Literal(WORKTREE_SWITCH_REQUEST),
27
+ version: Type.Literal(1),
28
+ action: Type.Literal("fork-and-switch"),
29
+ sessionFile: Type.String(),
30
+ status: Type.Union([
31
+ Type.Literal("created"),
32
+ Type.Literal("attached"),
33
+ Type.Literal("reused"),
34
+ Type.Literal("already-active"),
35
+ ]),
36
+ path: Type.String(),
37
+ branch: Type.String(),
38
+ dirty: Type.Boolean(),
39
+ });
40
+
41
+ function parseCommandArgs(input: string): { name: string; base?: string } {
42
+ const tokens = input.trim().split(/\s+/).filter(Boolean);
43
+ if (tokens[0] !== "start" || tokens.length < 2) {
44
+ throw new Error("用法:/worktree start <name> [--base <ref>]");
45
+ }
46
+
47
+ const name = tokens[1];
48
+ let base: string | undefined;
49
+ for (let index = 2; index < tokens.length; index += 1) {
50
+ if (tokens[index] !== "--base" || !tokens[index + 1] || base !== undefined) {
51
+ throw new Error("用法:/worktree start <name> [--base <ref>]");
52
+ }
53
+ base = tokens[index + 1];
54
+ index += 1;
55
+ }
56
+ return { name, base };
57
+ }
58
+
59
+ function parsePrepareOutput(stdout: string): WorktreePrepareResult {
60
+ const line = stdout.trim().split("\n").filter(Boolean).at(-1);
61
+ if (!line) throw new Error("pi-worktree prepare 没有返回结果");
62
+ const result = JSON.parse(line) as Partial<WorktreePrepareResult>;
63
+ if (
64
+ !["created", "attached", "reused", "already-active"].includes(String(result.status))
65
+ || typeof result.path !== "string"
66
+ || typeof result.branch !== "string"
67
+ || typeof result.dirty !== "boolean"
68
+ ) {
69
+ throw new Error("pi-worktree prepare 返回了无效结果");
70
+ }
71
+ return result as WorktreePrepareResult;
72
+ }
73
+
74
+ async function prepareWorktree(
75
+ pi: ExtensionAPI,
76
+ cwd: string,
77
+ name: string,
78
+ base?: string,
79
+ ): Promise<WorktreePrepareResult> {
80
+ const args = ["prepare", "--json"];
81
+ if (base) args.push("--base", base);
82
+ args.push(name);
83
+ const result = await pi.exec(CLI_PATH, args, { cwd });
84
+ if (result.code !== 0) {
85
+ throw new Error(result.stderr.trim() || result.stdout.trim() || `pi-worktree prepare 失败(退出码 ${result.code})`);
86
+ }
87
+ return parsePrepareOutput(result.stdout);
88
+ }
89
+
90
+ export default function worktreeExtension(pi: ExtensionAPI) {
91
+ pi.registerCommand("worktree", {
92
+ description: "创建或复用 worktree,并在当前界面切换过去",
93
+ handler: async (input, ctx) => {
94
+ let prepared: WorktreePrepareResult;
95
+ let targetSessionFile: string;
96
+ try {
97
+ const { name, base } = parseCommandArgs(input);
98
+ const sourceSessionFile = ctx.sessionManager.getSessionFile();
99
+ if (!sourceSessionFile) {
100
+ throw new Error("当前是临时会话;请启用 session 持久化后再切换 worktree");
101
+ }
102
+
103
+ await ctx.waitForIdle();
104
+ prepared = await prepareWorktree(pi, ctx.cwd, name, base);
105
+ if (resolve(ctx.cwd) === resolve(prepared.path)) {
106
+ ctx.ui.notify(`已经位于 worktree:${prepared.path}`, "info");
107
+ return;
108
+ }
109
+
110
+ const target = SessionManager.forkFrom(sourceSessionFile, prepared.path);
111
+ targetSessionFile = target.getSessionFile() ?? "";
112
+ if (!targetSessionFile) throw new Error("无法为 worktree 创建持久会话");
113
+ } catch (error) {
114
+ ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
115
+ return;
116
+ }
117
+
118
+ // Do not catch replacement errors here: switchSession may already have
119
+ // invalidated this command context, so Pi must report such failures.
120
+ const switched = await ctx.switchSession(targetSessionFile, {
121
+ withSession: async (next) => {
122
+ const dirtyNote = prepared.dirty ? "(已有未提交改动)" : "";
123
+ next.ui.notify(`已切换到 ${prepared.branch}:${prepared.path}${dirtyNote}`, "success");
124
+ },
125
+ });
126
+ if (switched.cancelled) {
127
+ try {
128
+ unlinkSync(targetSessionFile);
129
+ } catch (error) {
130
+ ctx.ui.notify(`worktree 会话切换已取消,但无法清理派生会话:${error instanceof Error ? error.message : String(error)}`, "warning");
131
+ return;
132
+ }
133
+ ctx.ui.notify("worktree 会话切换已取消", "warning");
134
+ }
135
+ },
136
+ });
137
+
138
+ pi.registerTool({
139
+ name: "enter_worktree",
140
+ label: "Enter Worktree",
141
+ exposure: "model-only",
142
+ description:
143
+ "Create or reuse an isolated git worktree and request that the host Harness move this current conversation into it. Call this tool alone, then stop; the Harness performs the session switch after the tool completes.",
144
+ parameters: Type.Object({
145
+ name: Type.String({ description: "Single-segment worktree and branch name" }),
146
+ base: Type.Optional(Type.String({ description: "Optional git ref used only when creating a new branch" })),
147
+ }),
148
+ outputSchema: SwitchRequestSchema,
149
+ async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
150
+ const sourceSessionFile = ctx.sessionManager.getSessionFile();
151
+ if (!sourceSessionFile) throw new Error("当前是临时会话,Harness 无法续接到 worktree");
152
+
153
+ const prepared = await prepareWorktree(pi, ctx.cwd, params.name, params.base);
154
+ const request: WorktreeSwitchRequest = {
155
+ kind: WORKTREE_SWITCH_REQUEST,
156
+ version: 1,
157
+ action: "fork-and-switch",
158
+ sessionFile: sourceSessionFile,
159
+ ...prepared,
160
+ };
161
+ const dirtyNote = prepared.dirty ? " 目标 worktree 已有未提交改动。" : "";
162
+ return {
163
+ content: [{
164
+ type: "text",
165
+ text: `Worktree ${prepared.status}: ${prepared.path}.${dirtyNote} 已请求 Harness 将当前会话切换到该目录;不要继续调用仓库工具。`,
166
+ }],
167
+ details: request,
168
+ structuredContent: request,
169
+ terminate: true,
170
+ };
171
+ },
172
+ });
173
+ }
package/package.json CHANGED
@@ -1,20 +1,49 @@
1
1
  {
2
2
  "name": "@maxiaochao/pi-worktree",
3
- "version": "0.1.0",
4
- "description": "Git worktree CLI and Pi skill for isolated coding tasks.",
3
+ "version": "0.2.0",
4
+ "description": "Git worktree CLI, Pi extension, and skill for isolated coding tasks.",
5
5
  "type": "module",
6
- "keywords": ["pi-package", "pi-skill", "git", "worktree"],
7
- "files": ["bin", "skills", "scripts", "docs", "README.md", "TESTING.md", "CHANGELOG.md"],
6
+ "keywords": [
7
+ "pi-package",
8
+ "pi-skill",
9
+ "git",
10
+ "worktree"
11
+ ],
12
+ "files": [
13
+ "bin",
14
+ "extensions",
15
+ "skills",
16
+ "scripts",
17
+ "docs",
18
+ "README.md",
19
+ "TESTING.md",
20
+ "CHANGELOG.md"
21
+ ],
8
22
  "repository": {
9
23
  "type": "git",
10
24
  "url": "git+https://github.com/chaochaoxiaochao/pi-toolkit.git",
11
25
  "directory": "packages/worktree"
12
26
  },
13
- "bin": { "pi-worktree": "bin/pi-worktree" },
14
- "pi": { "skills": ["./skills"] },
27
+ "bin": {
28
+ "pi-worktree": "bin/pi-worktree"
29
+ },
30
+ "pi": {
31
+ "extensions": [
32
+ "./extensions/worktree.ts"
33
+ ],
34
+ "skills": [
35
+ "./skills"
36
+ ]
37
+ },
38
+ "peerDependencies": {
39
+ "@earendil-works/pi-coding-agent": "*",
40
+ "typebox": "*"
41
+ },
15
42
  "scripts": {
16
- "test": "node --test tests/install.test.mjs && bash tests/run-tests.sh",
43
+ "test": "node --test tests/install.test.mjs && node --experimental-strip-types --test tests/extension.test.mjs && bash tests/run-tests.sh",
17
44
  "postinstall": "node scripts/install.mjs"
18
45
  },
19
- "publishConfig": { "access": "public" }
46
+ "publishConfig": {
47
+ "access": "public"
48
+ }
20
49
  }
@@ -1,11 +1,11 @@
1
1
  ---
2
2
  name: pi-worktree
3
- description: Create, inspect, and clean up git worktrees with the pi-worktree CLI (start/list/info/remove/prune), which also launches Pi inside the worktree. Use when a task warrants isolation in its own worktree, or when the user asks to handle something the worktree way.
3
+ description: Create, enter, inspect, and clean up git worktrees through the enter_worktree tool, /worktree command, or pi-worktree CLI. Use when a task warrants isolation in its own worktree, or when the user asks to handle something the worktree way.
4
4
  ---
5
5
 
6
6
  # pi-worktree
7
7
 
8
- `pi-worktree` wraps git worktree creation and Pi launch into one command. Worktrees live in `.worktrees/<name>` (added to `.gitignore`), each with a branch of the same name, branched from the branch you are on now (or `HEAD` when detached). The base commit is recorded in `branch.<name>.base` / `branch.<name>.baseRef` so `info` can always report where the branch came from.
8
+ Worktrees live in `.worktrees/<name>` (added to `.gitignore`), each with a branch of the same name, branched from the current branch (or `HEAD` when detached). The base commit is recorded in `branch.<name>.base` / `branch.<name>.baseRef` so `info` can report where the branch came from.
9
9
 
10
10
  Installed by the package postinstall hook to `~/.local/bin/pi-worktree`, with bash completion for subcommands and existing worktree names.
11
11
 
@@ -13,6 +13,9 @@ Installed by the package postinstall hook to `~/.local/bin/pi-worktree`, with ba
13
13
 
14
14
  | Command | Effect |
15
15
  |---|---|
16
+ | `enter_worktree({ name, base? })` | Agent entrypoint: create/reuse a worktree and ask a compatible Harness to move this conversation there. Call it alone and stop after it returns. |
17
+ | `/worktree start <name> [--base <ref>]` | User entrypoint: fork the current persistent session into the worktree and switch the current Pi UI. |
18
+ | `pi-worktree prepare [--base <ref>] [--json] <name>` | Create/reuse without launching Pi; `--json` is the machine interface. |
16
19
  | `pi-worktree start <name> [pi args...]` | Create or reuse `.worktrees/<name>`, then launch Pi inside it. Already inside a linked worktree, it just launches Pi there. |
17
20
  | `pi-worktree start --base <ref> <name> [pi args...]` | Same, but branch from `<ref>` instead of the current branch. |
18
21
  | `pi-worktree list` | All worktrees; the main checkout is marked `(main checkout)`, stale registrations are flagged. |
@@ -24,13 +27,15 @@ Installed by the package postinstall hook to `~/.local/bin/pi-worktree`, with ba
24
27
  ## Isolating a task
25
28
 
26
29
  1. **Survey** — `pi-worktree list`, plus `pi-worktree info <name>` when the name you want is already taken. Reuse is free; a removed worktree is gone for good, because `remove` deletes its branch.
27
- 2. **Start** — `pi-worktree start <name>` (with `--base <ref>` when the work must not start from the current branch). Done when `list` shows the new worktree on its own branch.
30
+ 2. **Enter** — as an Agent, call `enter_worktree` once and stop so the Harness can switch after settlement. A user can instead run `/worktree start <name>`. Use the CLI `start` only when a separate Pi process is wanted.
28
31
  3. **Work** — inside the worktree: commit there, and check `pi-worktree info <name>` before claiming progress. `ahead: N commits since base` is the evidence.
29
32
  4. **Land** — `pi-worktree out`, then `git merge <name>` in the main checkout. Done when the worktree's unique commits are in the target branch.
30
33
  5. **Clean up** — `pi-worktree remove <name>`. A merged branch goes without prompting; anything unmerged asks once.
31
34
 
32
- ## Driving it from an agent
35
+ ## Entry-point rules
33
36
 
34
- - `start` **execs `pi`** in the worktree, and `out` execs a shell. With no trailing Pi args both want an interactive terminal, so either run `start <name> -p "<task>"` for a headless nested run, or hand the bare `start <name>` to the user to run themselves.
37
+ - Prefer `enter_worktree` when it is available. Its `fork-and-switch` request requires Harness support: after `agent_settled`, the host forks the source session into the target cwd and switches to that persisted session. After the tool returns, do not read or modify repository files until the host has switched cwd.
38
+ - `/worktree` is user-facing and cannot be invoked by the model. It requires a persisted session because it forks history into a session whose header owns the worktree cwd.
39
+ - `pi-worktree start` **execs a new `pi`** in the worktree, and `out` execs a shell. Use this only for an intentionally separate process/session.
40
+ - `prepare` is idempotent. It reports whether the target was created, attached, reused, or already active, and reports dirty reuse rather than discarding anything.
35
41
  - `remove` asks before discarding dirty files or unmerged commits, and a non-interactive stdin (EOF) aborts safely. Answer explicitly when the removal is intended: `printf 'y\ny\n' | pi-worktree remove <name>`.
36
- - Your own working directory cannot change. "Isolate this in a worktree" therefore means launching a nested headless Pi in the worktree, or telling the user to run `pi-worktree start <name>`.