@henryqw/pi-herdr-done 0.2.0 → 0.3.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
@@ -12,16 +12,20 @@ pi install npm:@henryqw/pi-herdr-done
12
12
 
13
13
  | Surface | Type | Purpose |
14
14
  | --- | --- | --- |
15
- | `/done` | command | Close current Herdr worktree workspace and remove its clean checkout. |
16
- | `/done --force` | command | Close workspace and remove checkout even when worktree is dirty. |
15
+ | `/done` | command | Remove the current worktree checkout, fast-forward the parent workspace, and close only this session's tab. |
16
+ | `/done --force` | command | Same, even when the worktree is dirty. |
17
17
 
18
18
  Both forms wait for Pi to become idle. `/done` asks for confirmation first; `/done --force` skips it because the flag already states intent. Normal removal runs:
19
19
 
20
20
  ```bash
21
- herdr worktree remove --workspace "$HERDR_WORKSPACE_ID"
21
+ git worktree remove .
22
+ git -C <parent> pull --ff-only
23
+ herdr tab close "$HERDR_TAB_ID"
22
24
  ```
23
25
 
24
- Herdr closes the worktree workspace and removes its checkout. Command requires Pi running inside Herdr with `HERDR_ENV=1` and `HERDR_WORKSPACE_ID` set.
26
+ The parent pull runs only when this session ran in a linked worktree with a non-bare primary; it is skipped when the parent has diverged (`--ff-only` fails safely) or when another Herdr tab is working in the parent. Concurrent completions serialize on a lock around the parent checkout.
27
+
28
+ Only the current session's tab closes, so sibling tabs in the same workspace survive. Command requires Pi running inside Herdr with `HERDR_ENV=1` and `HERDR_TAB_ID` set.
25
29
 
26
30
  Dirty worktrees make `/done` fail. Commit or discard changes, or use `/done --force` to explicitly delete them.
27
31
 
@@ -1,20 +1,34 @@
1
+ import { tmpdir } from "node:os";
1
2
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
3
  import { createHerdrClient } from "@henryqw/pi-herdr";
4
+ import { lock } from "proper-lockfile";
5
+
6
+ type ExecResult = { stdout: string; stderr: string; code: number; killed?: boolean };
7
+
8
+ type SnapshotPane = { tab_id?: unknown; cwd?: unknown };
3
9
 
4
10
  export default function herdrDoneExtension(pi: ExtensionAPI): void {
5
11
  const herdr = createHerdrClient<{ cwd: string }>((command, args, options) =>
6
12
  pi.exec(command, [...args], options));
7
13
 
14
+ const execOrThrow = async (command: string, args: string[], cwd: string): Promise<string> => {
15
+ const result = await pi.exec(command, args, { cwd }) as unknown as ExecResult;
16
+ if (result.code !== 0 || result.killed) {
17
+ throw new Error(`${command} ${args[0]} failed: ${result.stderr.trim() || "killed"}`);
18
+ }
19
+ return result.stdout;
20
+ };
21
+
8
22
  pi.registerCommand("done", {
9
- description: "Close and remove the current Herdr worktree",
23
+ description: "Remove the current Herdr worktree, then fast-forward its parent workspace",
10
24
  handler: async (args, ctx) => {
11
25
  const option = args.trim();
12
26
  if (option && option !== "--force") throw new Error("Usage: /done [--force]");
13
27
  if (process.env.HERDR_ENV !== "1") {
14
28
  throw new Error("/done requires the current Pi session inside Herdr (HERDR_ENV=1).");
15
29
  }
16
- const workspaceId = process.env.HERDR_WORKSPACE_ID?.trim();
17
- if (!workspaceId) throw new Error("HERDR_WORKSPACE_ID is missing.");
30
+ const tabId = process.env.HERDR_TAB_ID?.trim();
31
+ if (!tabId) throw new Error("HERDR_TAB_ID is missing.");
18
32
 
19
33
  if (option !== "--force") {
20
34
  const confirmed = await ctx.ui.confirm("Done", "Close and remove the current Herdr worktree?");
@@ -22,10 +36,52 @@ export default function herdrDoneExtension(pi: ExtensionAPI): void {
22
36
  }
23
37
 
24
38
  await ctx.waitForIdle();
25
- await herdr.run([
26
- "worktree", "remove", "--workspace", workspaceId,
27
- ...(option === "--force" ? ["--force"] : []),
28
- ], { cwd: ctx.cwd });
39
+ const checkout = (await execOrThrow("git", ["rev-parse", "--show-toplevel"], ctx.cwd)).trim();
40
+ // First record of the NUL-delimited list is always the main worktree;
41
+ // -z keeps paths containing newlines parseable.
42
+ const worktreeFields = (await execOrThrow("git", ["worktree", "list", "--porcelain", "-z"], checkout)).split("\0");
43
+ const mainCheckout = worktreeFields[0]?.startsWith("worktree ") ? worktreeFields[0].slice("worktree ".length) : undefined;
44
+ if (!mainCheckout) throw new Error("git worktree list returned no main worktree.");
45
+ // A bare primary has no working tree to pull into.
46
+ const parentIsBare = mainCheckout !== checkout &&
47
+ worktreeFields.slice(1, worktreeFields.indexOf("")).includes("bare");
48
+
49
+ const release = await lock(checkout);
50
+ try {
51
+ const snapshot = await herdr.json(["api", "snapshot"], { cwd: ctx.cwd });
52
+ const panes = (snapshot.result as { snapshot?: { panes?: SnapshotPane[] } } | undefined)?.snapshot?.panes;
53
+ if (!Array.isArray(panes)) throw new Error("herdr api snapshot returned no panes.");
54
+ const guarded = mainCheckout === checkout ? [checkout] : [checkout, mainCheckout];
55
+ const isGuarded = (cwd: string) =>
56
+ guarded.some((root) => cwd === root || cwd.startsWith(`${root}/`));
57
+ const dependents = [...new Set(panes
58
+ .filter((pane): pane is SnapshotPane & { tab_id: string; cwd: string } =>
59
+ typeof pane.tab_id === "string" && pane.tab_id !== tabId &&
60
+ typeof pane.cwd === "string" && isGuarded(pane.cwd))
61
+ .map((pane) => pane.tab_id))];
62
+ if (dependents.length > 0) {
63
+ throw new Error(`Worktree still used by Herdr tabs ${dependents.join(", ")}; close them first.`);
64
+ }
65
+
66
+ await execOrThrow("git", [
67
+ "worktree", "remove", ...(option === "--force" ? ["--force"] : []), checkout,
68
+ ], ctx.cwd);
69
+ } finally {
70
+ await release();
71
+ }
72
+ if (!parentIsBare && mainCheckout !== checkout) {
73
+ // Serialize concurrent completions pulling the same parent checkout.
74
+ // Run outside the removed checkout because its directory no longer exists.
75
+ const releasePull = await lock(mainCheckout);
76
+ try {
77
+ await execOrThrow("git", ["pull", "--ff-only"], mainCheckout);
78
+ } finally {
79
+ await releasePull();
80
+ }
81
+ }
82
+ // Close only this session's tab so unrelated tabs survive.
83
+ // Run outside the removed checkout because its directory no longer exists.
84
+ await herdr.run(["tab", "close", tabId], { cwd: tmpdir() });
29
85
  },
30
86
  });
31
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@henryqw/pi-herdr-done",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Close and remove the current Herdr worktree from Pi.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -27,7 +27,8 @@
27
27
  "@earendil-works/pi-coding-agent": "^0.84.2"
28
28
  },
29
29
  "dependencies": {
30
- "@henryqw/pi-herdr": "^0.1.2"
30
+ "@henryqw/pi-herdr": "^0.1.2",
31
+ "proper-lockfile": "^4.1.2"
31
32
  },
32
33
  "repository": {
33
34
  "type": "git",