opencode-copilot-delegate 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/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # opencode-copilot-delegate
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 7dfb029: Eliminate head-of-line blocking and halve fork/exec cost in the orphan reaper
8
+
9
+ Two performance improvements to the plugin-init orphan-subprocess reaper, both surfaced in the `v0.1.x / v0.2.x runtime hardening follow-ups` tracking issue:
10
+
11
+ - **Streaming worker pool replaces chunked `Promise.all`**: under loaded conditions a single slow `ps` invocation (300-500ms is realistic) used to idle four sibling workers in the same chunk, blowing the soft init budget by 5-10x. The reaper now spawns up to `MAX_CONCURRENT_PROBES = 5` workers that drain a shared queue independently — a slow probe blocks only its own worker.
12
+ - **Combined `ps -p <pid> -o comm=,lstart=` query**: the two separate `comm` and `lstart` lookups (per entry, plus per task spawn) consolidate into a single `getPidIdentity(pid)` call. Halves fork/exec cost on the reap hot path, makes the concurrency-cap math match reality (5 workers ⇒ at most 5 concurrent `ps` subprocesses, not 10), and gets an atomic kernel snapshot of both identity legs.
13
+
14
+ Net wall-clock impact for a 10-entry orphan file with one slow `ps` probe: the chunked-of-5 worst case (~210ms) drops to roughly 200ms, but the streaming pool eliminates the artificial wave-boundary stall — the 9 fast probes complete in ~30ms instead of being trapped behind the slow one.
15
+
16
+ No public API changes. All callers and tests updated to use the combined `getPidIdentity` dependency.
17
+
3
18
  ## 0.2.0
4
19
 
5
20
  ### Minor Changes
package/dist/index.js CHANGED
@@ -9381,9 +9381,9 @@ function defaultIsPluginAlive(pid) {
9381
9381
  return isErrnoException(e) && e.code === "EPERM";
9382
9382
  }
9383
9383
  }
9384
- function psField(pid, field) {
9384
+ function runPs(args) {
9385
9385
  return new Promise((resolve) => {
9386
- const child = spawn2("ps", ["-p", String(pid), "-o", `${field}=`]);
9386
+ const child = spawn2("ps", args);
9387
9387
  let stdout = "";
9388
9388
  let timedOut = false;
9389
9389
  const timeout = setTimeout(() => {
@@ -9409,11 +9409,18 @@ function psField(pid, field) {
9409
9409
  });
9410
9410
  });
9411
9411
  }
9412
- async function getPidComm(pid) {
9413
- return psField(pid, "comm");
9414
- }
9415
- async function getPidStartTime(pid) {
9416
- return psField(pid, "lstart");
9412
+ async function getPidIdentity(pid) {
9413
+ const raw = await runPs(["-p", String(pid), "-o", "comm=,lstart="]);
9414
+ if (raw === null)
9415
+ return null;
9416
+ const firstWs = raw.search(/\s/);
9417
+ if (firstWs === -1)
9418
+ return null;
9419
+ const comm = raw.slice(0, firstWs);
9420
+ const lstart = raw.slice(firstWs).trim();
9421
+ if (!comm || !lstart)
9422
+ return null;
9423
+ return { comm, lstart };
9417
9424
  }
9418
9425
  function parseLine(line) {
9419
9426
  const parts = line.split("\t");
@@ -9425,38 +9432,42 @@ function parseLine(line) {
9425
9432
  const pid = Number(pidStr);
9426
9433
  return { pid, comm: parts[1], lstart: parts[2] };
9427
9434
  }
9428
- async function processEntries(entries, killProcessTree2, getPidComm2, getPidStartTime2) {
9435
+ var MAX_CONCURRENT_PROBES = 5;
9436
+ async function processEntry(entry, killProcessTree2, getPidIdentity2) {
9437
+ try {
9438
+ process.kill(entry.pid, 0);
9439
+ } catch {
9440
+ return { reaped: false, skipped: true };
9441
+ }
9442
+ const live = await getPidIdentity2(entry.pid);
9443
+ if (live === null || live.comm !== entry.comm || live.lstart !== entry.lstart) {
9444
+ return { reaped: false, skipped: true };
9445
+ }
9446
+ try {
9447
+ await killProcessTree2(entry.pid);
9448
+ return { reaped: true, skipped: false };
9449
+ } catch {
9450
+ return { reaped: false, skipped: true };
9451
+ }
9452
+ }
9453
+ async function processEntries(entries, killProcessTree2, getPidIdentity2) {
9429
9454
  let reaped = 0;
9430
9455
  let skipped = 0;
9431
- for (let i2 = 0;i2 < entries.length; i2 += 5) {
9432
- const chunk = entries.slice(i2, i2 + 5);
9433
- const results = await Promise.all(chunk.map(async (entry) => {
9434
- try {
9435
- process.kill(entry.pid, 0);
9436
- } catch {
9437
- return { reaped: false, skipped: true };
9438
- }
9439
- const [liveComm, liveLstart] = await Promise.all([
9440
- getPidComm2(entry.pid),
9441
- getPidStartTime2(entry.pid)
9442
- ]);
9443
- if (liveComm !== entry.comm || liveLstart !== entry.lstart) {
9444
- return { reaped: false, skipped: true };
9445
- }
9446
- try {
9447
- await killProcessTree2(entry.pid);
9448
- return { reaped: true, skipped: false };
9449
- } catch {
9450
- return { reaped: false, skipped: true };
9451
- }
9452
- }));
9453
- for (const r of results) {
9454
- if (r.reaped)
9456
+ const queue = [...entries];
9457
+ const workerCount = Math.min(MAX_CONCURRENT_PROBES, entries.length);
9458
+ const workers = Array.from({ length: workerCount }, async () => {
9459
+ while (true) {
9460
+ const entry = queue.shift();
9461
+ if (!entry)
9462
+ return;
9463
+ const result = await processEntry(entry, killProcessTree2, getPidIdentity2);
9464
+ if (result.reaped)
9455
9465
  reaped++;
9456
- else if (r.skipped)
9466
+ else if (result.skipped)
9457
9467
  skipped++;
9458
9468
  }
9459
- }
9469
+ });
9470
+ await Promise.all(workers);
9460
9471
  return { reaped, skipped };
9461
9472
  }
9462
9473
  async function readPidFileEntries(filePath) {
@@ -9489,7 +9500,7 @@ async function cleanupAfterReap(filePath, isCurrent, currentInstancePath) {
9489
9500
  return false;
9490
9501
  }
9491
9502
  }
9492
- async function reapOneFile(filePath, filePid, isCurrent, currentInstancePath, isPluginAlive, killProcessTree2, getPidComm2, getPidStartTime2) {
9503
+ async function reapOneFile(filePath, filePid, isCurrent, currentInstancePath, isPluginAlive, killProcessTree2, getPidIdentity2) {
9493
9504
  const empty = {
9494
9505
  reaped: 0,
9495
9506
  skipped: 0,
@@ -9503,7 +9514,7 @@ async function reapOneFile(filePath, filePid, isCurrent, currentInstancePath, is
9503
9514
  if (entries === null) {
9504
9515
  return empty;
9505
9516
  }
9506
- const { reaped, skipped } = await processEntries(entries, killProcessTree2, getPidComm2, getPidStartTime2);
9517
+ const { reaped, skipped } = await processEntries(entries, killProcessTree2, getPidIdentity2);
9507
9518
  const deleted = await cleanupAfterReap(filePath, isCurrent, currentInstancePath);
9508
9519
  return { reaped, skipped, scanned: true, deleted };
9509
9520
  }
@@ -9512,8 +9523,7 @@ async function reapOrphans(opts) {
9512
9523
  pidFileDir,
9513
9524
  currentInstancePath,
9514
9525
  killProcessTree: killProcessTree2,
9515
- getPidComm: getPidComm2,
9516
- getPidStartTime: getPidStartTime2,
9526
+ getPidIdentity: getPidIdentity2,
9517
9527
  isPluginAlive = defaultIsPluginAlive
9518
9528
  } = opts;
9519
9529
  let files;
@@ -9536,7 +9546,7 @@ async function reapOrphans(opts) {
9536
9546
  continue;
9537
9547
  }
9538
9548
  const filePid = Number(stem);
9539
- const outcome = await reapOneFile(filePath, filePid, filePid === process.pid, currentInstancePath, isPluginAlive, killProcessTree2, getPidComm2, getPidStartTime2);
9549
+ const outcome = await reapOneFile(filePath, filePid, filePid === process.pid, currentInstancePath, isPluginAlive, killProcessTree2, getPidIdentity2);
9540
9550
  reaped += outcome.reaped;
9541
9551
  skipped += outcome.skipped;
9542
9552
  if (outcome.scanned)
@@ -9648,9 +9658,9 @@ function createTask(input, taskId = `cpl_${randomUUID()}`) {
9648
9658
  taskId
9649
9659
  };
9650
9660
  if (task.pid > 0 && pidFilePath) {
9651
- Promise.all([getPidComm(task.pid), getPidStartTime(task.pid)]).then(([comm, lstart]) => {
9652
- if (comm && lstart) {
9653
- appendPidEntry(pidFilePath, task.pid, comm, lstart).catch(() => {});
9661
+ getPidIdentity(task.pid).then((identity3) => {
9662
+ if (identity3) {
9663
+ appendPidEntry(pidFilePath, task.pid, identity3.comm, identity3.lstart).catch(() => {});
9654
9664
  } else {
9655
9665
  console.warn(`[task-registry] ps lookup returned null for pid ${task.pid}; subprocess will not be tracked for orphan reaping`);
9656
9666
  }
@@ -10219,8 +10229,7 @@ var CopilotDelegate = async ({ client, directory }) => {
10219
10229
  pidFileDir,
10220
10230
  currentInstancePath,
10221
10231
  killProcessTree,
10222
- getPidComm,
10223
- getPidStartTime
10232
+ getPidIdentity
10224
10233
  });
10225
10234
  } catch {}
10226
10235
  return {
@@ -1,7 +1,9 @@
1
1
  export interface ReapDeps {
2
2
  killProcessTree: (pid: number) => Promise<void>;
3
- getPidComm: (pid: number) => Promise<string | null>;
4
- getPidStartTime: (pid: number) => Promise<string | null>;
3
+ getPidIdentity: (pid: number) => Promise<{
4
+ comm: string;
5
+ lstart: string;
6
+ } | null>;
5
7
  isPluginAlive: (pid: number) => boolean;
6
8
  }
7
9
  export interface ReapResult {
@@ -11,13 +13,27 @@ export interface ReapResult {
11
13
  deletedFiles: number;
12
14
  }
13
15
  export declare function defaultIsPluginAlive(pid: number): boolean;
14
- export declare function getPidComm(pid: number): Promise<string | null>;
15
- export declare function getPidStartTime(pid: number): Promise<string | null>;
16
+ /**
17
+ * Read both comm (kernel-tracked executable name) and lstart (process
18
+ * start time) for a PID via a single ps invocation.
19
+ *
20
+ * Halves the fork/exec cost of identity verification compared with two
21
+ * separate `ps -o comm=` and `ps -o lstart=` calls. The combined output
22
+ * format is `<comm> <lstart>` where comm is a single token and lstart is
23
+ * a multi-word date string (e.g., `Tue Apr 28 23:45:30 2026`).
24
+ *
25
+ * Returns null on any failure mode (timeout, spawn error, non-zero exit,
26
+ * empty output, malformed output). Callers must treat null as "identity
27
+ * unverifiable; do not trust this PID."
28
+ */
29
+ export declare function getPidIdentity(pid: number): Promise<{
30
+ comm: string;
31
+ lstart: string;
32
+ } | null>;
16
33
  export declare function reapOrphans(opts: {
17
34
  pidFileDir: string;
18
35
  currentInstancePath: string;
19
36
  killProcessTree: ReapDeps['killProcessTree'];
20
- getPidComm: ReapDeps['getPidComm'];
21
- getPidStartTime: ReapDeps['getPidStartTime'];
37
+ getPidIdentity: ReapDeps['getPidIdentity'];
22
38
  isPluginAlive?: ReapDeps['isPluginAlive'];
23
39
  }): Promise<ReapResult>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-copilot-delegate",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "OpenCode plugin that delegates tasks to GitHub Copilot CLI as background subprocesses",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",