pi-crew 0.6.0 → 0.6.3

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.
Files changed (135) hide show
  1. package/CHANGELOG.md +225 -0
  2. package/README.md +70 -31
  3. package/docs/issue-29-analysis.md +189 -0
  4. package/docs/superpowers/plans/2026-06-09-fallow-patterns-adoption.md +1268 -0
  5. package/package.json +2 -2
  6. package/src/agents/agent-config.ts +2 -1
  7. package/src/benchmark/feedback-loop.ts +4 -2
  8. package/src/config/config.ts +106 -15
  9. package/src/errors.ts +107 -0
  10. package/src/extension/async-notifier.ts +6 -2
  11. package/src/extension/crew-cleanup.ts +8 -5
  12. package/src/extension/cross-extension-rpc.ts +48 -0
  13. package/src/extension/management.ts +464 -109
  14. package/src/extension/register.ts +194 -34
  15. package/src/extension/registration/commands.ts +4 -3
  16. package/src/extension/registration/subagent-helpers.ts +2 -2
  17. package/src/extension/registration/subagent-tools.ts +3 -1
  18. package/src/extension/registration/team-tool.ts +3 -1
  19. package/src/extension/registration/viewers.ts +3 -2
  20. package/src/extension/run-export.ts +16 -1
  21. package/src/extension/run-import.ts +16 -0
  22. package/src/extension/team-tool/anchor.ts +5 -1
  23. package/src/extension/team-tool/api.ts +12 -7
  24. package/src/extension/team-tool/cancel.ts +3 -3
  25. package/src/extension/team-tool/config-patch.ts +15 -1
  26. package/src/extension/team-tool/explain.ts +1 -1
  27. package/src/extension/team-tool/handle-schedule.ts +40 -0
  28. package/src/extension/team-tool/inspect.ts +3 -3
  29. package/src/extension/team-tool/lifecycle-actions.ts +4 -4
  30. package/src/extension/team-tool/respond.ts +2 -2
  31. package/src/extension/team-tool/run.ts +60 -11
  32. package/src/extension/team-tool/status.ts +1 -1
  33. package/src/extension/team-tool.ts +175 -47
  34. package/src/hooks/registry.ts +84 -12
  35. package/src/hooks/types.ts +12 -3
  36. package/src/i18n.ts +15 -2
  37. package/src/observability/exporters/otlp-exporter.ts +73 -0
  38. package/src/plugins/plugin-define.ts +6 -0
  39. package/src/plugins/plugin-registry.ts +32 -0
  40. package/src/plugins/plugins/index.ts +3 -0
  41. package/src/plugins/plugins/nextjs.ts +19 -0
  42. package/src/plugins/plugins/vite.ts +14 -0
  43. package/src/plugins/plugins/vitest.ts +14 -0
  44. package/src/runtime/adaptive-plan.ts +24 -0
  45. package/src/runtime/agent-control.ts +6 -3
  46. package/src/runtime/async-runner.ts +86 -3
  47. package/src/runtime/background-runner.ts +529 -144
  48. package/src/runtime/chain-parser.ts +5 -1
  49. package/src/runtime/chain-runner.ts +67 -3
  50. package/src/runtime/checkpoint.ts +262 -180
  51. package/src/runtime/child-pi.ts +165 -38
  52. package/src/runtime/crash-recovery.ts +25 -14
  53. package/src/runtime/crew-agent-records.ts +6 -3
  54. package/src/runtime/cross-extension-rpc.ts +34 -8
  55. package/src/runtime/diagnostic-export.ts +4 -5
  56. package/src/runtime/dynamic-script-runner.ts +9 -6
  57. package/src/runtime/foreground-watchdog.ts +3 -3
  58. package/src/runtime/heartbeat-watcher.ts +19 -2
  59. package/src/runtime/intercom-bridge.ts +7 -0
  60. package/src/runtime/iteration-hooks.ts +1 -1
  61. package/src/runtime/live-agent-manager.ts +6 -3
  62. package/src/runtime/live-irc.ts +4 -2
  63. package/src/runtime/manifest-cache.ts +4 -0
  64. package/src/runtime/orphan-worker-registry.ts +444 -0
  65. package/src/runtime/parallel-utils.ts +2 -1
  66. package/src/runtime/parent-guard.ts +70 -16
  67. package/src/runtime/pi-args.ts +437 -14
  68. package/src/runtime/pi-spawn.ts +1 -0
  69. package/src/runtime/post-checks.ts +11 -4
  70. package/src/runtime/retry-runner.ts +9 -3
  71. package/src/runtime/{drift-detectors.ts → run-drift.ts} +1 -1
  72. package/src/runtime/run-tracker.ts +38 -10
  73. package/src/runtime/sandbox.ts +42 -21
  74. package/src/runtime/scheduler.ts +25 -2
  75. package/src/runtime/semaphore.ts +2 -1
  76. package/src/runtime/settings-store.ts +14 -2
  77. package/src/runtime/skill-effectiveness.ts +109 -64
  78. package/src/runtime/skill-instructions.ts +114 -33
  79. package/src/runtime/stale-reconciler.ts +310 -69
  80. package/src/runtime/subagent-manager.ts +265 -53
  81. package/src/runtime/subprocess-tool-registry.ts +2 -2
  82. package/src/runtime/task-health.ts +76 -0
  83. package/src/runtime/task-id.ts +20 -0
  84. package/src/runtime/task-packet.ts +13 -1
  85. package/src/runtime/task-runner/live-executor.ts +1 -1
  86. package/src/runtime/task-runner/progress.ts +1 -1
  87. package/src/runtime/task-runner/state-helpers.ts +110 -6
  88. package/src/runtime/task-runner.ts +98 -67
  89. package/src/runtime/team-runner.ts +186 -20
  90. package/src/runtime/usage-tracker.ts +4 -2
  91. package/src/runtime/verification-gates.ts +36 -9
  92. package/src/schema/team-tool-schema.ts +27 -9
  93. package/src/skills/discover-skills.ts +9 -3
  94. package/src/state/active-run-registry.ts +170 -38
  95. package/src/state/artifact-store.ts +25 -13
  96. package/src/state/atomic-write-v2.ts +86 -0
  97. package/src/state/atomic-write.ts +346 -55
  98. package/src/state/blob-store.ts +178 -10
  99. package/src/state/contracts.ts +2 -1
  100. package/src/state/crew-init.ts +161 -28
  101. package/src/state/decision-ledger.ts +172 -111
  102. package/src/state/event-log-rotation.ts +82 -52
  103. package/src/state/event-log.ts +270 -75
  104. package/src/state/health-store.ts +71 -0
  105. package/src/state/hook-instinct-bridge.ts +2 -1
  106. package/src/state/locks.ts +109 -20
  107. package/src/state/mailbox.ts +45 -7
  108. package/src/state/observation-store.ts +4 -1
  109. package/src/state/run-graph.ts +141 -130
  110. package/src/state/run-metrics.ts +24 -8
  111. package/src/state/state-store.ts +333 -44
  112. package/src/state/task-claims.ts +9 -2
  113. package/src/tools/safe-bash.ts +69 -20
  114. package/src/types/new-api-types.ts +10 -5
  115. package/src/ui/keybinding-map.ts +2 -1
  116. package/src/ui/live-run-sidebar.ts +1 -1
  117. package/src/ui/loaders.ts +4 -0
  118. package/src/ui/overlays/agent-picker-overlay.ts +1 -1
  119. package/src/ui/overlays/mailbox-detail-overlay.ts +1 -1
  120. package/src/ui/run-action-dispatcher.ts +4 -3
  121. package/src/ui/run-snapshot-cache.ts +1 -1
  122. package/src/ui/status-colors.ts +2 -1
  123. package/src/ui/syntax-highlight.ts +2 -1
  124. package/src/ui/tool-render.ts +13 -3
  125. package/src/utils/env-filter.ts +66 -0
  126. package/src/utils/file-coalescer.ts +9 -1
  127. package/src/utils/fs-watch.ts +4 -2
  128. package/src/utils/gh-protocol.ts +2 -1
  129. package/src/utils/paths.ts +80 -5
  130. package/src/utils/redaction.ts +6 -1
  131. package/src/utils/safe-paths.ts +287 -10
  132. package/src/worktree/cleanup.ts +117 -26
  133. package/src/worktree/worktree-manager.ts +128 -15
  134. package/test-bugs-all.mjs +10 -6
  135. package/test-integration-check.ts +4 -0
@@ -88,7 +88,7 @@ export class HeartbeatWatcher {
88
88
  // Bug #5 fix: if stateRoot doesn't exist, the run was pruned — skip it silently.
89
89
  // This prevents stale "heartbeat dead" notifications for runs that no longer exist.
90
90
  if (!fs.existsSync(run.stateRoot)) continue;
91
- const loaded = loadRunManifestById(this.opts.cwd, run.runId);
91
+ const loaded = loadRunManifestById(this.opts.cwd, run.runId); // NOTE: no withRunLock - best-effort only; concurrent writes may cause inconsistency;
92
92
  if (!loaded) continue;
93
93
  for (const task of loaded.tasks) {
94
94
  if (task.status !== "running") continue;
@@ -112,7 +112,24 @@ export class HeartbeatWatcher {
112
112
  }
113
113
  }
114
114
  }
115
- const level = elapsed > thresholds.deadMs ? "dead" : elapsed > thresholds.staleMs ? "stale" : elapsed > thresholds.warnMs ? "warn" : "healthy";
115
+ // PID liveness gate: if the worker process is still alive, downgrade
116
+ // "dead" to "stale". This prevents false positives when the LLM spends
117
+ // a long time generating a response (>5 min) without tool calls.
118
+ // A truly dead process will eventually be detected by exit handlers.
119
+ let isProcessAlive = false;
120
+ const workerPid = task.heartbeat?.pid ?? task.checkpoint?.childPid;
121
+ if (workerPid && workerPid > 0) {
122
+ try {
123
+ process.kill(workerPid, 0);
124
+ isProcessAlive = true;
125
+ } catch {
126
+ // Process is dead
127
+ }
128
+ }
129
+ let level: HeartbeatLevel = elapsed > thresholds.deadMs ? "dead" : elapsed > thresholds.staleMs ? "stale" : elapsed > thresholds.warnMs ? "warn" : "healthy";
130
+ if (level === "dead" && isProcessAlive) {
131
+ level = "stale";
132
+ }
116
133
  this.opts.registry.gauge("crew.heartbeat.staleness_ms", "Heartbeat elapsed since last seen, milliseconds").set({ runId: run.runId, taskId: task.id }, Number.isFinite(elapsed) ? elapsed : thresholds.deadMs);
117
134
  this.opts.registry.counter("crew.heartbeat.level_total", "Heartbeat classifications by level").inc({ runId: run.runId, level });
118
135
  const previous = this.lastLevel.get(key);
@@ -79,6 +79,13 @@ export class IntercomQueue {
79
79
  });
80
80
  this.pending.delete(id);
81
81
  }, message.timeout);
82
+ // Defense in depth: never let a pending-message timer block
83
+ // process exit. The timer is cleared via respond()/evict() in
84
+ // the normal case; .unref() ensures shutdown isn't blocked if
85
+ // the queue is abandoned.
86
+ if (entry.timer && typeof entry.timer.unref === "function") {
87
+ entry.timer.unref();
88
+ }
82
89
  }
83
90
 
84
91
  this.pending.set(id, entry);
@@ -171,7 +171,7 @@ export async function runIterationHook(
171
171
  const { command, args } = resolveShellForScript(resolvedScript);
172
172
  const child = spawn(command, args, {
173
173
  cwd: payload.cwd,
174
- env: { ...sanitizeEnvSecrets(process.env, { allowList: ["PATH", "HOME", "USER", "USERPROFILE", "TEMP", "TMP", "TMPDIR", "LANG", "LC_ALL", "ComSpec", "SystemRoot", "PI_*"] }), PI_CREW_HOOK: "1" },
174
+ env: { ...sanitizeEnvSecrets(process.env, { allowList: ["PATH", "HOME", "USER", "USERPROFILE", "TEMP", "TMP", "TMPDIR", "LANG", "LC_ALL", "ComSpec", "SystemRoot", "PI_CREW_*"] }), PI_CREW_HOOK: "1" },
175
175
  stdio: ["pipe", "pipe", "pipe"],
176
176
  });
177
177
 
@@ -81,7 +81,8 @@ export function listLiveAgentsByWorkspace(workspaceId: string): LiveAgentHandle[
81
81
  /**
82
82
  * List only active agents (running/queued/waiting) for a specific workspace.
83
83
  */
84
- export function listActiveLiveAgentsByWorkspace(workspaceId: string): LiveAgentHandle[] {
84
+ /** @internal */
85
+ function listActiveLiveAgentsByWorkspace(workspaceId: string): LiveAgentHandle[] {
85
86
  return listActiveLiveAgents().filter((a) => a.workspaceId === workspaceId);
86
87
  }
87
88
 
@@ -150,7 +151,8 @@ function safeDisposeLiveSession(handle: LiveAgentHandle): void {
150
151
  }
151
152
  }
152
153
 
153
- export function removeLiveAgentHandle(agentId: string): LiveAgentHandle | undefined {
154
+ /** @internal */
155
+ function removeLiveAgentHandle(agentId: string): LiveAgentHandle | undefined {
154
156
  const handle = liveAgents.get(agentId);
155
157
  if (!handle) return undefined;
156
158
  liveAgents.delete(agentId);
@@ -406,7 +408,8 @@ export function broadcastIrcMessage(fromAgentId: string, message: IrcMessage): s
406
408
  }
407
409
 
408
410
  /** Phase 7: Get pending IRC messages for an agent (and clear them). */
409
- export function drainIrcMessages(agentIdOrTaskId: string): IrcMessage[] {
411
+ /** @internal */
412
+ function drainIrcMessages(agentIdOrTaskId: string): IrcMessage[] {
410
413
  const handle = getLiveAgent(agentIdOrTaskId);
411
414
  if (!handle) return [];
412
415
  const messages = [...handle.pendingMessages];
@@ -51,7 +51,8 @@ export function renderIrcPeerRoster(selfId: string, peers: Array<{ agentId: stri
51
51
  /**
52
52
  * Build the IRC system prompt section for a live-session worker.
53
53
  */
54
- export function buildIrcSystemSection(selfId: string, peers: Array<{ agentId: string; status: string }>): string {
54
+ /** @internal */
55
+ function buildIrcSystemSection(selfId: string, peers: Array<{ agentId: string; status: string }>): string {
55
56
  const roster = renderIrcPeerRoster(selfId, peers);
56
57
  return [
57
58
  "## Inter-Agent Communication",
@@ -66,7 +67,8 @@ export function buildIrcSystemSection(selfId: string, peers: Array<{ agentId: st
66
67
  * Route an IRC message to the appropriate agent(s).
67
68
  * Returns the list of agent IDs that received the message.
68
69
  */
69
- export function routeIrcMessage(
70
+ /** @internal */
71
+ function routeIrcMessage(
70
72
  message: IrcSendMessage,
71
73
  selfId: string,
72
74
  routing: {
@@ -157,6 +157,10 @@ export function createManifestCache(cwd: string, options: ManifestCacheOptions =
157
157
  listCache.clear();
158
158
  timer?.unref();
159
159
  }, ttlMs);
160
+ // Unref immediately so the timer never blocks process exit (defense in
161
+ // depth: the in-callback unref above may not run if shutdown happens
162
+ // before the timer fires).
163
+ listTimer.unref();
160
164
  }
161
165
 
162
166
  function loadManifest(runId: string, rootsToCheck: string[]): CachedManifest | undefined {
@@ -0,0 +1,444 @@
1
+ /**
2
+ * Orphan background-worker registry.
3
+ *
4
+ * Tracks PIDs of background-runner.ts processes spawned via async-runner.
5
+ * Workers are detached, setsid'd, and unref'd, so they outlive the spawning
6
+ * pi session. If the parent pi process is killed (SIGKILL, crash), workers
7
+ * become orphans and keep running forever.
8
+ *
9
+ * This registry provides:
10
+ * 1. `registerWorker` — called from async-runner.ts after successful spawn.
11
+ * 2. `unregisterWorker` — called when a worker exits (via async-marker
12
+ * or heartbeat watcher).
13
+ * 3. `cleanupOrphanWorkers` — called on session_start; kills workers whose
14
+ * registration is older than STALE_REGISTRATION_MS (default 1h) and
15
+ * removes dead PIDs from the registry.
16
+ *
17
+ * Persistence: file-based JSON in `<userPiRoot>/state/orphan-workers.json`.
18
+ * File is rewritten on every operation to drop dead PIDs.
19
+ *
20
+ * Thread-safety: All mutating operations (registerWorker, unregisterWorker,
21
+ * cleanupOrphanWorkers) are protected by file locking to prevent concurrent
22
+ * writes from causing lost updates.
23
+ */
24
+ import * as fs from "node:fs";
25
+ import * as path from "node:path";
26
+ import { execSync } from "node:child_process";
27
+ import { userPiRoot } from "../utils/paths.ts";
28
+ import { logInternalError } from "../utils/internal-error.ts";
29
+ import { withFileLockSync } from "../state/locks.ts";
30
+ import { isSymlinkSafePath } from "../state/atomic-write.ts";
31
+
32
+ const STALE_REGISTRATION_MS = 60 * 60 * 1000; // 1 hour
33
+ // Grace period before a fresh worker can be cleaned up when parent is dead.
34
+ // Workers registered more recently than this are kept (monitored) even if
35
+ // parent is dead, to avoid killing a legitimate worker from a session that
36
+ // died recently. Only stale workers (> GRACE_PERIOD_MS) are SIGKILLed.
37
+ const GRACE_PERIOD_MS = 5 * 60 * 1000; // 5 minutes
38
+
39
+ export interface OrphanWorkerEntry {
40
+ pid: number;
41
+ sessionId: string;
42
+ runId: string;
43
+ /** Parent PID (the pi process that spawned this worker). Used to verify
44
+ * the owning session is actually dead before killing the worker. */
45
+ parentPid: number;
46
+ /** Parent process start time in milliseconds since boot (from /proc/<pid>/stat).
47
+ * Used to detect PID reuse of the parent process. If the OS recycles the
48
+ * parent PID for a new process, the start time will differ and we won't
49
+ * incorrectly believe the parent is still alive. */
50
+ parentPidStartTime: number;
51
+ registeredAt: number; // epoch ms
52
+ /** Process start time in milliseconds since boot (from /proc/<pid>/stat).
53
+ * Used to detect PID reuse: if the OS recycles this PID for a new process,
54
+ * the start time will differ and we won't kill the wrong process. */
55
+ startTime: number;
56
+ }
57
+
58
+ /**
59
+ * Get process start time in milliseconds since boot.
60
+ * Uses platform-specific APIs for cross-platform PID reuse detection:
61
+ * - Linux: reads /proc/<pid>/stat (field 22, starttime)
62
+ * - macOS: sysctl KERN_PROC_PID to get kinfo_proc and extract p_starttime
63
+ * - Windows: GetProcessTimes API to get creation time
64
+ *
65
+ * Returns undefined if the process is gone or the platform API is unavailable.
66
+ */
67
+ function getProcessStartTime(pid: number): number | undefined {
68
+ try {
69
+ // First check if process exists
70
+ process.kill(pid, 0);
71
+ } catch {
72
+ return undefined;
73
+ }
74
+
75
+ const platform = process.platform;
76
+ if (platform === "linux") {
77
+ return getProcessStartTimeLinux(pid);
78
+ } else if (platform === "darwin") {
79
+ return getProcessStartTimeMacOS(pid);
80
+ } else if (platform === "win32") {
81
+ return getProcessStartTimeWindows(pid);
82
+ }
83
+ return undefined;
84
+ }
85
+
86
+ function getProcessStartTimeLinux(pid: number): number | undefined {
87
+ try {
88
+ const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf-8");
89
+ // comm is wrapped in parentheses and may contain spaces/special chars.
90
+ // Find the last ')' which marks the end of comm.
91
+ const lastParen = stat.lastIndexOf(")");
92
+ if (lastParen === -1) return undefined;
93
+ // Fields after comm: state(1) ppid(2) pgrp(3) session(4) tty_nr(5)
94
+ // tpgid(6) flags(7) minflt(8) cminflt(9) majflt(10) cmajflt(11)
95
+ // utime(12) stime(13) cutime(14) cstime(15) priority(16) nice(17)
96
+ // num_threads(18) itrealvalue(19) starttime(20) vsize(21) rss(22)
97
+ // ...but we only need starttime which is field 22 (index 21 after comm)
98
+ const fieldsAfterComm = stat.slice(lastParen + 1).trim().split(/\s+/);
99
+ // starttime is at index 19 (the 20th field after comm)
100
+ const startTimeClockTicks = Number(fieldsAfterComm[19]);
101
+ if (!Number.isFinite(startTimeClockTicks)) return undefined;
102
+ // Convert clock ticks to milliseconds using CLK_TCK (usually 100)
103
+ // We use a conservative estimate; the absolute value matters less
104
+ // than the uniqueness per PID lifecycle.
105
+ return Math.floor(startTimeClockTicks * 100);
106
+ } catch {
107
+ return undefined;
108
+ }
109
+ }
110
+
111
+ function getProcessStartTimeMacOS(pid: number): number | undefined {
112
+ // Use sysctl to get process start time on macOS
113
+ // KERN_PROC_PID returns a kinfo_proc structure with p_starttime
114
+ try {
115
+ // Use ps to get process start time - format: Mon Day Time or Mon Day Year
116
+ // For cross-platform consistency, we use 'lstart' which gives full timestamp
117
+ const output = execSync(`ps -p ${pid} -o lstart=`, { encoding: "utf-8", timeout: 5000 }).trim();
118
+ if (!output) return undefined;
119
+ // Parse date string like "Mon Jan 15 10:30:45 2024"
120
+ const date = new Date(output);
121
+ if (Number.isNaN(date.getTime())) return undefined;
122
+ return date.getTime();
123
+ } catch {
124
+ return undefined;
125
+ }
126
+ }
127
+
128
+ function getProcessStartTimeWindows(pid: number): number | undefined {
129
+ // Use Windows API via JSDrive's winattr or native code
130
+ // For Node.js without native modules, use tasklist /v and parse output
131
+ try {
132
+ // /v verbose, /fo csv, /nh no header
133
+ const output = execSync(
134
+ `powershell -Command "Get-Process -Id ${pid} | Select-Object -ExpandProperty StartTime"`,
135
+ { encoding: "utf-8", timeout: 5000 },
136
+ ).trim();
137
+ if (!output) return undefined;
138
+ const date = new Date(output);
139
+ if (Number.isNaN(date.getTime())) return undefined;
140
+ return date.getTime();
141
+ } catch {
142
+ return undefined;
143
+ }
144
+ }
145
+
146
+
147
+ let REGISTRY_PATH = path.join(userPiRoot(), "state", "orphan-workers.json");
148
+
149
+ /** @internal Test-only: override the registry path. */
150
+ export function __test_setRegistryPath(p: string): void {
151
+ REGISTRY_PATH = p;
152
+ }
153
+
154
+ function getRegistryPath(): string {
155
+ return REGISTRY_PATH;
156
+ }
157
+
158
+ /** Issue 3 fix: Validate sessionId and runId to prevent path traversal attacks. */
159
+ function isValidId(id: string): boolean {
160
+ if (!id || typeof id !== "string") return false;
161
+ if (id.includes("..") || id.includes("/") || id.includes("\\") || id.includes("\0")) return false;
162
+ return true;
163
+ }
164
+
165
+ function readRegistry(): OrphanWorkerEntry[] {
166
+ const p = getRegistryPath();
167
+ try {
168
+ // Atomic read: if file doesn't exist, readFileSync throws ENOENT
169
+ // which we handle explicitly. This eliminates the TOCTOU window
170
+ // between existsSync and readFileSync.
171
+ const raw = fs.readFileSync(p, "utf-8");
172
+ const parsed = JSON.parse(raw);
173
+ if (!Array.isArray(parsed)) return [];
174
+ return parsed.filter(
175
+ (e): e is OrphanWorkerEntry =>
176
+ typeof e === "object" &&
177
+ e !== null &&
178
+ typeof e.pid === "number" &&
179
+ typeof e.sessionId === "string" &&
180
+ typeof e.runId === "string" &&
181
+ typeof e.registeredAt === "number" &&
182
+ typeof (e as OrphanWorkerEntry).parentPid === "number" &&
183
+ typeof (e as OrphanWorkerEntry).parentPidStartTime === "number" &&
184
+ typeof (e as OrphanWorkerEntry).startTime === "number",
185
+ );
186
+ } catch (error) {
187
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") {
188
+ return [];
189
+ }
190
+ // Silent failure is deliberate for robustness (registry read failures
191
+ // shouldn't crash the process), but log at warning level to aid troubleshooting.
192
+ console.warn(`[orphan-worker-registry] readRegistry failed: ${error}`);
193
+ return [];
194
+ }
195
+ }
196
+
197
+ function writeRegistry(entries: OrphanWorkerEntry[]): void {
198
+ const p = getRegistryPath();
199
+ const dir = path.dirname(p);
200
+ // Issue 2 fix: Defense-in-depth validation of IDs inside writeRegistry.
201
+ // Even if registerWorker is bypassed in the future, we still validate
202
+ // that sessionId/runId don't contain path traversal characters before
203
+ // writing to disk.
204
+ for (const entry of entries) {
205
+ if (!isValidId(entry.sessionId) || !isValidId(entry.runId)) {
206
+ logInternalError("orphan-worker-registry.write", new Error("Refusing to write: invalid sessionId or runId"), `sessionId=${entry.sessionId} runId=${entry.runId}`);
207
+ return;
208
+ }
209
+ }
210
+ // NOTE: No withFileLockSync here — all callers (registerWorker, unregisterWorker,
211
+ // cleanupOrphanWorkers) already hold the lock. Nesting withFileLockSync on the same
212
+ // path causes self-deadlock (the lock file is not reentrant).
213
+ // Guard against symlink attacks on the registry file.
214
+ // isSymlinkSafePath walks the ancestor chain to detect any symlinks,
215
+ // preventing attacks where an intermediate ancestor is a symlink.
216
+ if (!isSymlinkSafePath(p)) {
217
+ logInternalError("orphan-worker-registry.write", new Error("Refusing to write: target is a symlink or inside untrusted directory"), `path=${p}`);
218
+ return;
219
+ }
220
+ // Issue 2 fix: Check parent directory safety immediately before creating it.
221
+ if (!isSymlinkSafePath(dir)) {
222
+ logInternalError("orphan-worker-registry.write", new Error("Refusing to create: parent directory is a symlink or inside untrusted directory"), `dir=${dir}`);
223
+ return;
224
+ }
225
+ // Ensure parent directory exists to serialize directory
226
+ // creation with registry file writes and prevent TOCTOU races.
227
+ if (!fs.existsSync(dir)) {
228
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
229
+ }
230
+ try {
231
+ fs.writeFileSync(p, JSON.stringify(entries, null, 2), { mode: 0o600 });
232
+ } catch (error) {
233
+ logInternalError(
234
+ "orphan-worker-registry.write",
235
+ error,
236
+ `path=${p} entries=${entries.length}`,
237
+ );
238
+ }
239
+ }
240
+
241
+ /**
242
+ * Add a worker PID to the registry. Idempotent (replaces existing entry
243
+ * for the same PID).
244
+ *
245
+ * @param parentPid The PID of the spawning pi process. Used later to
246
+ * verify the owning session is actually dead before killing the worker.
247
+ */
248
+ export function registerWorker(
249
+ pid: number,
250
+ sessionId: string,
251
+ runId: string,
252
+ parentPid: number,
253
+ options?: { registeredAt?: number },
254
+ ): void {
255
+ if (!Number.isFinite(pid) || pid <= 0) return;
256
+ // Issue 3 fix: Validate sessionId and runId to prevent path traversal attacks.
257
+ if (!isValidId(sessionId) || !isValidId(runId)) return;
258
+ const startTime = getProcessStartTime(pid) ?? 0;
259
+ const parentPidStartTime = Number.isFinite(parentPid) && parentPid > 0
260
+ ? (getProcessStartTime(parentPid) ?? 0)
261
+ : 0;
262
+ withFileLockSync(getRegistryPath(), () => {
263
+ const entries = readRegistry();
264
+ // Dedupe by PID
265
+ const filtered = entries.filter((e) => e.pid !== pid);
266
+ filtered.push({
267
+ pid,
268
+ sessionId,
269
+ runId,
270
+ parentPid: Number.isFinite(parentPid) ? parentPid : 0,
271
+ parentPidStartTime,
272
+ registeredAt: options?.registeredAt ?? Date.now(),
273
+ startTime,
274
+ });
275
+ writeRegistry(filtered);
276
+ });
277
+ }
278
+
279
+ /**
280
+ * Remove a worker PID from the registry. Called when the worker is known
281
+ * to have exited (e.g. via async-marker poll or heartbeat watcher).
282
+ */
283
+ export function unregisterWorker(pid: number): void {
284
+ if (!Number.isFinite(pid) || pid <= 0) return;
285
+ withFileLockSync(getRegistryPath(), () => {
286
+ const entries = readRegistry();
287
+ const filtered = entries.filter((e) => e.pid !== pid);
288
+ if (filtered.length !== entries.length) {
289
+ writeRegistry(filtered);
290
+ }
291
+ });
292
+ }
293
+
294
+ export interface CleanupOrphanWorkersResult {
295
+ scanned: number;
296
+ killed: number;
297
+ pruned: number; // dead PIDs removed from registry without killing
298
+ kept: number; // alive and fresh
299
+ }
300
+
301
+ /**
302
+ * Kill stale orphan background workers and prune dead PIDs from the registry.
303
+ *
304
+ * Strategy:
305
+ * - For each entry in the registry, check if the PID is still alive.
306
+ * - If alive AND registered > STALE_REGISTRATION_MS ago: SIGTERM the PID
307
+ * (it's an orphan from a long-dead session).
308
+ * - If alive AND fresh: keep (concurrent session).
309
+ * - If dead: prune from registry.
310
+ *
311
+ * @param currentSessionId If provided, workers from this session are
312
+ * ALWAYS kept regardless of age. This protects concurrent sessions.
313
+ * Pass undefined for unconditional cleanup (e.g. from `pi-crew cleanup`).
314
+ */
315
+ export function cleanupOrphanWorkers(
316
+ currentSessionId?: string,
317
+ ): CleanupOrphanWorkersResult {
318
+ let result: CleanupOrphanWorkersResult = { scanned: 0, killed: 0, pruned: 0, kept: 0 };
319
+ withFileLockSync(getRegistryPath(), () => {
320
+ const entries = readRegistry();
321
+ const now = Date.now();
322
+ const kept: OrphanWorkerEntry[] = [];
323
+ let killed = 0;
324
+ let pruned = 0;
325
+ for (const entry of entries) {
326
+ try {
327
+ process.kill(entry.pid, 0);
328
+ // PID is alive
329
+ const isMine = currentSessionId && entry.sessionId === currentSessionId;
330
+ if (isMine) {
331
+ // My session's worker — keep regardless of age
332
+ kept.push(entry);
333
+ continue;
334
+ }
335
+ // Verify parent is actually dead before killing worker.
336
+ // If parent is alive, this is a concurrent session's worker
337
+ // (or the same session that was misidentified). Keep it.
338
+ if (entry.parentPid > 0) {
339
+ // Verify parent hasn't been recycled before checking liveness.
340
+ // If the parent PID was reused by a different process, the start
341
+ // time will differ and we shouldn't trust the parentPid liveness check.
342
+ const currentParentStartTime = getProcessStartTime(entry.parentPid);
343
+ if (
344
+ currentParentStartTime !== undefined &&
345
+ entry.parentPidStartTime !== 0 &&
346
+ currentParentStartTime !== entry.parentPidStartTime
347
+ ) {
348
+ // Parent PID was recycled — this is a different process,
349
+ // treat as if parent is dead so we may clean up the worker.
350
+ } else {
351
+ try {
352
+ process.kill(entry.parentPid, 0);
353
+ // Parent is alive — concurrent session, keep worker
354
+ kept.push(entry);
355
+ continue;
356
+ } catch (err) {
357
+ // Parent is dead — proceed to verify it's actually our worker
358
+ // However, EPERM means the process exists but we lack permission
359
+ // to signal it. Treat as 'unknown' state and keep the entry.
360
+ if ((err as NodeJS.ErrnoException).code === "EPERM") {
361
+ kept.push(entry);
362
+ continue;
363
+ }
364
+ }
365
+ }
366
+ }
367
+ // Verify PID hasn't been recycled by checking start time matches.
368
+ // Capture startTime for re-verification before kill to close TOCTOU window.
369
+ // Between the kill(0) check and the actual SIGKILL below, the OS
370
+ // may have reused this PID for a new process. If the start time
371
+ // has changed, this is a different process and we must not kill it.
372
+ const currentStartTime = getProcessStartTime(entry.pid);
373
+ if (currentStartTime === undefined || entry.startTime === 0) {
374
+ // Can't verify startTime (macOS/Windows or stale entry) — trust registry
375
+ } else if (currentStartTime !== entry.startTime) {
376
+ // PID was recycled — different process now, prune without killing
377
+ pruned++;
378
+ continue;
379
+ }
380
+ // Re-verify start time immediately before SIGKILL to close the
381
+ // TOCTOU window.
382
+ //
383
+ // KNOWN RESIDUAL RACE: Even with this re-check, a microsecond-level
384
+ // window exists between the currentStartTime read (line 366) and the
385
+ // actual process.kill(entry.pid, "SIGKILL") call (line 400). The OS
386
+ // could theoretically recycle the PID and allocate it to a new process
387
+ // within that window. This is an inherent limitation of userspace PID
388
+ // verification against kernel PID allocation — the race cannot be fully
389
+ // eliminated without kernel-level process naming or a process descriptor
390
+ // that we do not have. As alternative mitigations, consider using
391
+ // process groups (killpg) for worker identification instead of raw PIDs,
392
+ // or kernel-level process descriptors if available on the platform.
393
+ // The consequence of killing a wrong process is severe (SIGKILL of an
394
+ // unrelated process), so this re-check is the best possible mitigation
395
+ // given the kernel's PID allocation semantics.
396
+ if (currentStartTime !== undefined && entry.startTime !== 0 && currentStartTime !== entry.startTime) {
397
+ // PID was recycled — different process now, prune without killing
398
+ pruned++;
399
+ continue;
400
+ }
401
+ if (now - entry.registeredAt > STALE_REGISTRATION_MS) {
402
+ // Stale orphan — SIGKILL because background-runner
403
+ // intentionally ignores SIGTERM (BUG #17 fix).
404
+ try {
405
+ process.kill(entry.pid, "SIGKILL");
406
+ killed++;
407
+ } catch {
408
+ // Race: died between check and kill
409
+ pruned++;
410
+ }
411
+ } else if (now - entry.registeredAt > GRACE_PERIOD_MS) {
412
+ // Fresh but outside grace period — parent dead and worker
413
+ // is not doing useful work (same session died > 5 min ago).
414
+ // SIGKILL to avoid wasting resources.
415
+ try {
416
+ process.kill(entry.pid, "SIGKILL");
417
+ killed++;
418
+ } catch {
419
+ pruned++;
420
+ }
421
+ } else {
422
+ // Fresh and within grace period — keep worker even though
423
+ // parent is dead. Could be a legitimate worker from a session
424
+ // that died recently and may still be doing useful work.
425
+ // Will be cleaned up on next cycle if still orphan.
426
+ kept.push(entry);
427
+ }
428
+ } catch {
429
+ // PID is dead — prune from registry
430
+ pruned++;
431
+ }
432
+ }
433
+ if (kept.length !== entries.length) {
434
+ writeRegistry(kept);
435
+ }
436
+ result = {
437
+ scanned: entries.length,
438
+ killed,
439
+ pruned,
440
+ kept: kept.length,
441
+ };
442
+ });
443
+ return result;
444
+ }
@@ -63,7 +63,8 @@ export async function mapConcurrent<T, R>(items: T[], limit: number, fn: (item:
63
63
  * On abort: returns partial results (may contain undefined entries).
64
64
  * On error: throws immediately (fail-fast) and cancels remaining work.
65
65
  */
66
- export async function mapConcurrentWithSignal<T, R>(
66
+ /** @internal */
67
+ async function mapConcurrentWithSignal<T, R>(
67
68
  items: T[],
68
69
  limit: number,
69
70
  fn: (item: T, i: number, signal: AbortSignal) => Promise<R>,