@lioneltay/worker-manager 0.0.2 → 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.
package/dist/index.js CHANGED
@@ -2,20 +2,22 @@
2
2
  import * as fs from "node:fs";
3
3
  import * as path from "node:path";
4
4
  import * as os from "node:os";
5
- import { getStateDir, getGitRoot, readAndFlushMail, readRegistry, readOrchestratorId, } from "./state.js";
5
+ import { getStateDir, getGitRoot, readOrchestratorId, readAndFlushMail, readRegistry, } from "./state.js";
6
6
  import { startOrchestratorServer } from "./orchestrator.js";
7
7
  import { startWorkerServer } from "./worker.js";
8
8
  function runHook() {
9
9
  try {
10
+ const claudePid = parseInt(process.env.CLAUDE_PID, 10);
11
+ if (!claudePid)
12
+ return;
10
13
  const gitRoot = getGitRoot();
11
14
  const stateDir = getStateDir(gitRoot);
12
- const orchestratorId = readOrchestratorId(stateDir, process.ppid);
15
+ const orchestratorId = readOrchestratorId(stateDir, claudePid);
13
16
  if (!orchestratorId)
14
17
  return;
15
18
  const messages = readAndFlushMail(stateDir, orchestratorId);
16
- if (messages.length === 0) {
19
+ if (messages.length === 0)
17
20
  return;
18
- }
19
21
  for (const msg of messages) {
20
22
  const prefix = msg.type === "completion" ? "COMPLETED" : "QUESTION";
21
23
  process.stdout.write(`[Worker ${msg.workerName} (${msg.workerId})] ${prefix}: ${msg.content}\n`);
@@ -31,15 +33,17 @@ function runStopHook() {
31
33
  // Orchestrator mode: check for pending mail
32
34
  if (!workerId || !stateDir) {
33
35
  try {
36
+ const claudePid = parseInt(process.env.CLAUDE_PID, 10);
37
+ if (!claudePid)
38
+ return;
34
39
  const gitRoot = getGitRoot();
35
40
  const orchestratorStateDir = getStateDir(gitRoot);
36
- const orchestratorId = readOrchestratorId(orchestratorStateDir, process.ppid);
41
+ const orchestratorId = readOrchestratorId(orchestratorStateDir, claudePid);
37
42
  if (!orchestratorId)
38
43
  return;
39
44
  const messages = readAndFlushMail(orchestratorStateDir, orchestratorId);
40
- if (messages.length === 0) {
45
+ if (messages.length === 0)
41
46
  return;
42
- }
43
47
  const formatted = messages
44
48
  .map((m) => {
45
49
  const prefix = m.type === "completion" ? "COMPLETED" : "QUESTION";
@@ -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/hooks/hooks.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "hooks": [
6
6
  {
7
7
  "type": "command",
8
- "command": "npx -y @lioneltay/worker-manager --hook",
8
+ "command": "CLAUDE_PID=$PPID npx -y @lioneltay/worker-manager --hook",
9
9
  "timeout": 5
10
10
  }
11
11
  ]
@@ -16,7 +16,7 @@
16
16
  "hooks": [
17
17
  {
18
18
  "type": "command",
19
- "command": "npx -y @lioneltay/worker-manager --stop-hook",
19
+ "command": "CLAUDE_PID=$PPID npx -y @lioneltay/worker-manager --stop-hook",
20
20
  "timeout": 5
21
21
  }
22
22
  ]
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@lioneltay/worker-manager",
3
- "version": "0.0.2",
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",
7
- "url": "https://github.com/lioneltay/agent-forge",
7
+ "url": "git+https://github.com/lioneltay/agent-forge.git",
8
8
  "directory": "packages/claude-plugins/worker-manager"
9
9
  },
10
10
  "type": "module",
@@ -19,17 +19,19 @@
19
19
  "hooks",
20
20
  "README.md"
21
21
  ],
22
- "scripts": {
23
- "build": "tsc",
24
- "dev": "tsc --watch"
25
- },
26
22
  "dependencies": {
27
- "@modelcontextprotocol/sdk": "^1.12.1",
28
23
  "@lioneltay/worktree-manager": "^0.0.1",
24
+ "@modelcontextprotocol/sdk": "^1.12.1",
29
25
  "zod": "^3.24.2"
30
26
  },
31
27
  "devDependencies": {
32
28
  "@types/node": "^22.13.1",
33
- "typescript": "^5.7.3"
29
+ "typescript": "^5.7.3",
30
+ "vitest": "^4.0.18"
31
+ },
32
+ "scripts": {
33
+ "build": "tsc",
34
+ "dev": "tsc --watch",
35
+ "test": "vitest run --exclude dist"
34
36
  }
35
- }
37
+ }