@lioneltay/worker-manager 0.0.3 → 0.0.4

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.
@@ -6,6 +6,25 @@ import { randomUUID } from "node:crypto";
6
6
  import { create } from "@lioneltay/worktree-manager";
7
7
  import { getGitRoot, getStateDir, readRegistry, writeRegistry, readAndFlushMail, writeOrchestratorMapping, removeOrchestratorMapping, } from "./state.js";
8
8
  import { spawnWorker } from "./spawn.js";
9
+ /**
10
+ * Get Claude Code's PID. The MCP server is spawned via:
11
+ * Claude Code → npx (npm exec) → node
12
+ * So process.ppid = npx's PID. We need Claude Code's PID (npx's parent)
13
+ * to match the hook's $PPID which is also Claude Code's PID.
14
+ */
15
+ function getClaudeCodePid() {
16
+ try {
17
+ const ppidStr = execFileSync("ps", ["-p", String(process.ppid), "-o", "ppid="], { encoding: "utf-8" }).trim();
18
+ const grandparentPid = parseInt(ppidStr, 10);
19
+ if (!isNaN(grandparentPid) && grandparentPid > 1) {
20
+ return grandparentPid;
21
+ }
22
+ }
23
+ catch {
24
+ // Fall through to process.ppid
25
+ }
26
+ return process.ppid;
27
+ }
9
28
  function generateShortId() {
10
29
  return randomUUID().slice(0, 8);
11
30
  }
@@ -37,7 +56,7 @@ function killTmuxSession(session) {
37
56
  // Session may already be dead
38
57
  }
39
58
  }
40
- function cleanupWorkers(stateDir) {
59
+ function cleanupWorkers(stateDir, pid) {
41
60
  const registry = readRegistry(stateDir);
42
61
  let changed = false;
43
62
  for (const worker of Object.values(registry.workers)) {
@@ -50,7 +69,7 @@ function cleanupWorkers(stateDir) {
50
69
  if (changed) {
51
70
  writeRegistry(stateDir, registry);
52
71
  }
53
- removeOrchestratorMapping(stateDir, process.ppid);
72
+ removeOrchestratorMapping(stateDir, pid);
54
73
  }
55
74
  function getCurrentBranch() {
56
75
  return execFileSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
@@ -62,8 +81,9 @@ export async function startOrchestratorServer() {
62
81
  const orchestratorId = generateShortId();
63
82
  const gitRoot = getGitRoot();
64
83
  const stateDir = getStateDir(gitRoot);
84
+ const claudePid = getClaudeCodePid();
65
85
  // Write mapping so hooks (which share the same parent PID) can find our mailbox.
66
- writeOrchestratorMapping(stateDir, process.ppid, orchestratorId);
86
+ writeOrchestratorMapping(stateDir, claudePid, orchestratorId);
67
87
  const server = new McpServer({
68
88
  name: "worker-manager",
69
89
  version: "0.0.1",
@@ -355,14 +375,14 @@ export async function startOrchestratorServer() {
355
375
  const transport = new StdioServerTransport();
356
376
  await server.connect(transport);
357
377
  transport.onclose = () => {
358
- cleanupWorkers(stateDir);
378
+ cleanupWorkers(stateDir, claudePid);
359
379
  };
360
380
  process.on("SIGTERM", () => {
361
- cleanupWorkers(stateDir);
381
+ cleanupWorkers(stateDir, claudePid);
362
382
  process.exit(0);
363
383
  });
364
384
  process.on("SIGINT", () => {
365
- cleanupWorkers(stateDir);
385
+ cleanupWorkers(stateDir, claudePid);
366
386
  process.exit(0);
367
387
  });
368
388
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lioneltay/worker-manager",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Claude Code plugin for spawning and managing worker agents in isolated worktrees",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,11 +19,6 @@
19
19
  "hooks",
20
20
  "README.md"
21
21
  ],
22
- "scripts": {
23
- "build": "tsc",
24
- "dev": "tsc --watch",
25
- "test": "vitest run --exclude dist"
26
- },
27
22
  "dependencies": {
28
23
  "@lioneltay/worktree-manager": "^0.0.1",
29
24
  "@modelcontextprotocol/sdk": "^1.12.1",
@@ -33,5 +28,10 @@
33
28
  "@types/node": "^22.13.1",
34
29
  "typescript": "^5.7.3",
35
30
  "vitest": "^4.0.18"
31
+ },
32
+ "scripts": {
33
+ "build": "tsc",
34
+ "dev": "tsc --watch",
35
+ "test": "vitest run --exclude dist"
36
36
  }
37
- }
37
+ }