@narumitw/pi-worktree 0.35.0 → 0.39.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/README.md CHANGED
@@ -56,7 +56,12 @@ Choose one action:
56
56
  - **Prune stale metadata** — inspect Git's dry-run output, then optionally run the matching prune.
57
57
  - **Configure worktree root** — set a machine-local default root or submit a blank value to restore `~/.worktrees`.
58
58
 
59
- The menu title shows the effective worktree root and whether it comes from the built-in default or the user settings file. `/worktree` intentionally does not accept text subcommands or expose argument autocomplete. Every change is initiated and confirmed through the interactive UI.
59
+ The standard root menu shows the registered count, current path, effective worktree root, its source,
60
+ and any settings warning. Escape closes it. `/worktree` intentionally does not accept text
61
+ subcommands or expose argument autocomplete. Every change is initiated and confirmed through TUI or
62
+ RPC dialogs; print and JSON modes reject the command observably. Operation-specific branch/path
63
+ inputs, worktree identity selectors, preflight previews, and destructive confirmations remain
64
+ extension-owned because they carry Git safety and commit-aware revalidation.
60
65
 
61
66
  ## 🌿 Add defaults
62
67
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-worktree",
3
- "version": "0.35.0",
3
+ "version": "0.39.0",
4
4
  "description": "Pi extension for safe interactive Git worktree management and workspace switching.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -32,14 +32,17 @@
32
32
  "@earendil-works/pi-coding-agent": "*"
33
33
  },
34
34
  "devDependencies": {
35
- "@biomejs/biome": "2.5.5",
36
- "@earendil-works/pi-coding-agent": "0.82.1",
37
- "@types/node": "26.1.1",
35
+ "@biomejs/biome": "2.5.6",
36
+ "@earendil-works/pi-coding-agent": "0.83.0",
37
+ "@types/node": "26.1.2",
38
38
  "typescript": "7.0.2"
39
39
  },
40
40
  "repository": {
41
41
  "type": "git",
42
42
  "url": "https://github.com/narumiruna/pi-extensions",
43
43
  "directory": "extensions/pi-worktree"
44
+ },
45
+ "dependencies": {
46
+ "@narumitw/pi-tui-kit": "<1"
44
47
  }
45
48
  }
package/src/command.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import { resolve } from "node:path";
3
3
  import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
4
+ import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
4
5
  import {
5
6
  addWorktree,
6
7
  administrativeHistoryOids,
@@ -37,14 +38,29 @@ const ACTION_SWITCH = "Switch worktree";
37
38
  const ACTION_REMOVE = "Remove worktree";
38
39
  const ACTION_PRUNE = "Prune stale metadata";
39
40
  const ACTION_CONFIGURE_ROOT = "Configure worktree root";
40
- const ACTIONS = [ACTION_ADD, ACTION_SWITCH, ACTION_REMOVE, ACTION_PRUNE, ACTION_CONFIGURE_ROOT];
41
+ const ACTIONS = {
42
+ add: ACTION_ADD,
43
+ switch: ACTION_SWITCH,
44
+ remove: ACTION_REMOVE,
45
+ prune: ACTION_PRUNE,
46
+ configure: ACTION_CONFIGURE_ROOT,
47
+ } as const;
48
+
49
+ interface WorktreeMenuOwner {
50
+ signal: AbortSignal;
51
+ isCurrent(): boolean;
52
+ }
41
53
 
42
54
  interface AdministrativeHistoryRisk {
43
55
  label: string;
44
56
  oids: string[];
45
57
  }
46
58
 
47
- export function registerWorktreeCommand(pi: ExtensionAPI, settings: WorktreeSettingsRuntime): void {
59
+ export function registerWorktreeCommand(
60
+ pi: ExtensionAPI,
61
+ settings: WorktreeSettingsRuntime,
62
+ getMenuOwner: () => WorktreeMenuOwner,
63
+ ): void {
48
64
  pi.registerCommand("worktree", {
49
65
  description: "Interactively manage Git worktrees and their default root",
50
66
  handler: async (args, ctx) => {
@@ -57,7 +73,7 @@ export function registerWorktreeCommand(pi: ExtensionAPI, settings: WorktreeSett
57
73
  return;
58
74
  }
59
75
  if (!ctx.hasUI) {
60
- safeNotify(ctx, "/worktree requires an interactive Pi UI.", "error");
76
+ safeNotify(ctx, "/worktree requires TUI or RPC mode.", "error");
61
77
  return;
62
78
  }
63
79
 
@@ -67,29 +83,51 @@ export function registerWorktreeCommand(pi: ExtensionAPI, settings: WorktreeSett
67
83
  const currentPath = await currentWorktreePath(pi, ctx.cwd, ctx.signal);
68
84
  const root = settings.get();
69
85
  const warning = root.warning ? " — settings warning" : "";
70
- const action = await ctx.ui.select(
71
- stripTerminalControls(
72
- `Git worktrees (${records.length}) — current: ${currentPath}\nWorktree root: ${root.effectiveRoot} (${root.source})${warning}`,
73
- ),
74
- ACTIONS,
75
- );
76
- switch (action) {
77
- case ACTION_ADD:
78
- await addFlow(pi, ctx, records, root.effectiveRoot);
79
- return;
80
- case ACTION_SWITCH:
81
- await switchFlow(pi, ctx, records, currentPath);
82
- return;
83
- case ACTION_REMOVE:
84
- await removeFlow(pi, ctx, records, currentPath);
85
- return;
86
- case ACTION_PRUNE:
87
- await pruneFlow(pi, ctx, records);
88
- return;
89
- case ACTION_CONFIGURE_ROOT:
90
- await configureRootFlow(ctx, settings);
91
- return;
92
- }
86
+ const owner = getMenuOwner();
87
+ const runFlow = async (flow: () => Promise<void>) => {
88
+ try {
89
+ await flow();
90
+ } catch (error) {
91
+ safeNotify(ctx, formatError(error), "error");
92
+ }
93
+ return { kind: "close" } as const;
94
+ };
95
+ type Screen = "main";
96
+ type Action = keyof typeof ACTIONS;
97
+ const menu = defineMenu<undefined, Screen, Action, ExtensionCommandContext>({
98
+ start: "main",
99
+ screens: {
100
+ main: () => ({
101
+ kind: "actions",
102
+ title: "Git worktrees",
103
+ lines: [
104
+ `Registered: ${records.length}`,
105
+ `Current: ${currentPath}`,
106
+ `Worktree root: ${root.effectiveRoot} (${root.source})${warning}`,
107
+ ],
108
+ items: Object.entries(ACTIONS).map(([id, label]) => ({
109
+ id,
110
+ label,
111
+ action: id as Action,
112
+ })),
113
+ hint: "close",
114
+ }),
115
+ },
116
+ actions: {
117
+ add: async () => runFlow(() => addFlow(pi, ctx, records, root.effectiveRoot)),
118
+ switch: async ({ signal }) =>
119
+ runFlow(() => switchFlow(pi, ctx, records, currentPath, signal)),
120
+ remove: async ({ signal }) =>
121
+ runFlow(() => removeFlow(pi, ctx, records, currentPath, signal)),
122
+ prune: async () => runFlow(() => pruneFlow(pi, ctx, records)),
123
+ configure: async () => runFlow(() => configureRootFlow(ctx, settings)),
124
+ },
125
+ });
126
+ await runMenu(ctx, menu, {
127
+ getState: () => undefined,
128
+ signal: owner.signal,
129
+ isCurrent: owner.isCurrent,
130
+ });
93
131
  } catch (error) {
94
132
  safeNotify(ctx, formatError(error), "error");
95
133
  }
@@ -237,6 +275,7 @@ async function switchFlow(
237
275
  ctx: ExtensionCommandContext,
238
276
  records: readonly WorktreeRecord[],
239
277
  currentPath: string,
278
+ signal?: AbortSignal,
240
279
  ): Promise<void> {
241
280
  const candidates = records.filter(
242
281
  (record) =>
@@ -245,7 +284,7 @@ async function switchFlow(
245
284
  existsSync(record.path) &&
246
285
  !pathsEqual(record.path, currentPath),
247
286
  );
248
- const selected = await selectWorktree(ctx, "Switch to worktree", candidates, currentPath);
287
+ const selected = await selectWorktree(ctx, "Switch to worktree", candidates, currentPath, signal);
249
288
  if (!selected) return;
250
289
  const latest = await revalidateWorktreeIdentity(pi, ctx, selected);
251
290
  if (
@@ -264,11 +303,18 @@ async function removeFlow(
264
303
  ctx: ExtensionCommandContext,
265
304
  records: readonly WorktreeRecord[],
266
305
  currentPath: string,
306
+ signal?: AbortSignal,
267
307
  ): Promise<void> {
268
308
  const candidates = records.filter(
269
309
  (record) => !record.isMain && !record.bare && !pathsEqual(record.path, currentPath),
270
310
  );
271
- const selected = await selectWorktree(ctx, "Remove linked worktree", candidates, currentPath);
311
+ const selected = await selectWorktree(
312
+ ctx,
313
+ "Remove linked worktree",
314
+ candidates,
315
+ currentPath,
316
+ signal,
317
+ );
272
318
  if (!selected) return;
273
319
  if (selected.lockedReason !== undefined) {
274
320
  throw new Error(
@@ -594,17 +640,40 @@ async function selectWorktree(
594
640
  title: string,
595
641
  records: readonly WorktreeRecord[],
596
642
  currentPath: string,
643
+ signal?: AbortSignal,
597
644
  ): Promise<WorktreeRecord | undefined> {
598
645
  if (records.length === 0) {
599
646
  ctx.ui.notify("No eligible worktrees are available for this action.", "info");
600
647
  return undefined;
601
648
  }
602
- const labels = records.map(
603
- (record, index) => `${index + 1}. ${formatWorktree(record, currentPath)}`,
604
- );
605
- const selected = await ctx.ui.select(title, labels);
606
- const index = selected === undefined ? -1 : labels.indexOf(selected);
607
- return index < 0 ? undefined : records[index];
649
+ let selected: WorktreeRecord | undefined;
650
+ const menu = defineMenu<undefined, "worktrees", "choose", ExtensionCommandContext>({
651
+ start: "worktrees",
652
+ screens: {
653
+ worktrees: () => ({
654
+ kind: "choice",
655
+ title,
656
+ items: records.map((record, index) => ({
657
+ id: record.path,
658
+ label: `${index + 1}. ${formatWorktree(record, currentPath)}`,
659
+ })),
660
+ action: "choose",
661
+ hint: "close",
662
+ }),
663
+ },
664
+ actions: {
665
+ choose: async ({ itemId }) => {
666
+ selected = records.find((record) => record.path === itemId);
667
+ return selected ? { kind: "close" } : { kind: "rejected" };
668
+ },
669
+ },
670
+ });
671
+ await runMenu(ctx, menu, {
672
+ getState: () => undefined,
673
+ signal,
674
+ isCurrent: () => !signal?.aborted,
675
+ });
676
+ return selected;
608
677
  }
609
678
 
610
679
  function safeNotify(
package/src/worktree.ts CHANGED
@@ -16,16 +16,26 @@ export default function worktreeExtension(
16
16
  ): void {
17
17
  const settings = options.settings ?? createWorktreeSettingsRuntime({ path: settingsFilePath });
18
18
  let sessionGeneration = 0;
19
- registerWorktreeCommand(pi, settings);
19
+ let menuController = new AbortController();
20
+ registerWorktreeCommand(pi, settings, () => {
21
+ const generation = sessionGeneration;
22
+ return {
23
+ signal: menuController.signal,
24
+ isCurrent: () => generation === sessionGeneration && !menuController.signal.aborted,
25
+ };
26
+ });
20
27
 
21
28
  pi.on("session_start", async (_event, ctx) => {
22
29
  const generation = ++sessionGeneration;
30
+ menuController.abort(new DOMException("Worktree session replaced", "AbortError"));
31
+ menuController = new AbortController();
23
32
  const loaded = await settings.reload();
24
33
  if (generation !== sessionGeneration || !loaded.warning || !ctx.hasUI) return;
25
34
  ctx.ui.notify(loaded.warning, "warning");
26
35
  });
27
36
  pi.on("session_shutdown", async () => {
28
37
  sessionGeneration += 1;
38
+ menuController.abort(new DOMException("Worktree session shut down", "AbortError"));
29
39
  await settings.flush?.();
30
40
  });
31
41
  }