@fitchmultz/pi-subagents 0.52.18 → 0.52.20

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 (57) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +4 -0
  3. package/dist/agent/host.js +12 -0
  4. package/dist/agent/index.js +70 -56
  5. package/dist/agent/launch.js +12 -3
  6. package/dist/agent/notifications.js +5 -5
  7. package/dist/agent/parking.js +26 -15
  8. package/dist/agent/runner.js +18 -16
  9. package/dist/agent/tool-schemas.js +2 -0
  10. package/dist/agent/usage.js +42 -3
  11. package/dist/agent/view.js +13 -13
  12. package/dist/agent/wake.js +17 -15
  13. package/dist/build-info.js +1 -1
  14. package/dist/pi-intercom/bridge-presence.js +30 -18
  15. package/dist/pi-intercom/bridge-wake.js +60 -37
  16. package/dist/pi-intercom/broker/broker.js +2 -2
  17. package/dist/pi-intercom/broker/client-last.js +6 -18
  18. package/dist/pi-intercom/broker/client.js +16 -50
  19. package/dist/pi-intercom/broker/pending-requests.d.ts +12 -0
  20. package/dist/pi-intercom/broker/pending-requests.js +26 -0
  21. package/dist/pi-intercom/broker/snapshot-reader.d.ts +3 -2
  22. package/dist/pi-intercom/broker/snapshot-reader.js +34 -15
  23. package/dist/pi-intercom/broker/snapshots.d.ts +25 -7
  24. package/dist/pi-intercom/broker/snapshots.js +56 -25
  25. package/dist/pi-intercom/broker/spawn-claim.js +89 -0
  26. package/dist/pi-intercom/broker/spawn.js +31 -78
  27. package/dist/pi-intercom/lifecycle.js +26 -26
  28. package/dist/shared/post-exit-stdio-guard.js +44 -37
  29. package/docs/development.md +25 -0
  30. package/docs/intercom.md +13 -0
  31. package/docs/reference.md +16 -5
  32. package/package.json +1 -1
  33. package/scripts/build.mjs +41 -14
  34. package/skills/pi-subagents/SKILL.md +8 -4
  35. package/src/agent/contracts.ts +17 -0
  36. package/src/agent/host.ts +19 -0
  37. package/src/agent/index.ts +91 -62
  38. package/src/agent/launch.ts +16 -3
  39. package/src/agent/notifications.ts +5 -5
  40. package/src/agent/parking.ts +39 -17
  41. package/src/agent/runner.ts +20 -21
  42. package/src/agent/tool-schemas.ts +2 -0
  43. package/src/agent/usage.ts +48 -3
  44. package/src/agent/view.ts +15 -13
  45. package/src/agent/wake.ts +19 -15
  46. package/src/pi-intercom/bridge-presence.ts +37 -18
  47. package/src/pi-intercom/bridge-wake.ts +70 -38
  48. package/src/pi-intercom/broker/broker.ts +2 -1
  49. package/src/pi-intercom/broker/client-last.ts +14 -25
  50. package/src/pi-intercom/broker/client.ts +36 -65
  51. package/src/pi-intercom/broker/pending-requests.ts +37 -0
  52. package/src/pi-intercom/broker/snapshot-reader.ts +44 -16
  53. package/src/pi-intercom/broker/snapshots.ts +82 -37
  54. package/src/pi-intercom/broker/spawn-claim.ts +98 -0
  55. package/src/pi-intercom/broker/spawn.ts +42 -82
  56. package/src/pi-intercom/lifecycle.ts +38 -27
  57. package/src/shared/post-exit-stdio-guard.ts +48 -40
@@ -19,6 +19,7 @@ interface ChildWithPipedStdio {
19
19
  readonly stdout: ChildProcess["stdout"];
20
20
  readonly stderr: ChildProcess["stderr"];
21
21
  readonly on: ChildProcess["on"];
22
+ readonly off: ChildProcess["off"];
22
23
  }
23
24
 
24
25
  interface ChildWithKill {
@@ -356,13 +357,17 @@ export function attachChildProcessLifecycle(
356
357
  }
357
358
 
358
359
  function attachPostExitStdioGuard(child: ChildWithPipedStdio): () => void {
359
- let exited = false;
360
+ let state:
361
+ | { readonly kind: "waiting" }
362
+ | { readonly kind: "draining"; idle: NodeJS.Timeout | undefined; readonly hard: NodeJS.Timeout }
363
+ | { readonly kind: "closed" } = { kind: "waiting" };
360
364
  let stdoutEnded = false;
361
365
  let stderrEnded = false;
362
- let idleTimer: NodeJS.Timeout | undefined;
363
- let hardTimer: NodeJS.Timeout | undefined;
364
366
 
365
367
  const destroyUnendedStdio = () => {
368
+ if (state.kind !== "draining") {
369
+ return;
370
+ }
366
371
  if (!stdoutEnded) {
367
372
  try {
368
373
  child.stdout?.destroy();
@@ -377,55 +382,58 @@ function attachPostExitStdioGuard(child: ChildWithPipedStdio): () => void {
377
382
  // The process has exited; failed pipe destruction must not prevent other cleanup.
378
383
  }
379
384
  }
380
- };
381
-
382
- const clearTimers = () => {
383
- if (idleTimer) {
384
- clearTimeout(idleTimer);
385
- idleTimer = undefined;
386
- }
387
- if (hardTimer) {
388
- clearTimeout(hardTimer);
389
- hardTimer = undefined;
390
- }
385
+ cleanup();
391
386
  };
392
387
 
393
388
  const armIdleTimer = () => {
394
- if (!exited) {
389
+ if (state.kind !== "draining") {
395
390
  return;
396
391
  }
397
- if (idleTimer) {
398
- clearTimeout(idleTimer);
399
- }
400
- idleTimer = setTimeout(destroyUnendedStdio, 2000);
401
- idleTimer.unref();
392
+ clearTimeout(state.idle);
393
+ state.idle = setTimeout(destroyUnendedStdio, 2000);
394
+ state.idle.unref();
402
395
  };
403
-
404
- child.stdout?.on("data", armIdleTimer);
405
- child.stderr?.on("data", armIdleTimer);
406
- child.stdout?.on("end", () => {
396
+ const stdoutEnd = () => {
407
397
  stdoutEnded = true;
408
398
  if (stderrEnded) {
409
- clearTimers();
399
+ cleanup();
410
400
  }
411
- });
412
- child.stderr?.on("end", () => {
401
+ };
402
+ const stderrEnd = () => {
413
403
  stderrEnded = true;
414
404
  if (stdoutEnded) {
415
- clearTimers();
405
+ cleanup();
416
406
  }
417
- });
418
- child.on("exit", () => {
419
- exited = true;
420
- armIdleTimer();
421
- if (hardTimer) {
407
+ };
408
+ const exited = () => {
409
+ if (state.kind !== "waiting") {
422
410
  return;
423
411
  }
424
- hardTimer = setTimeout(destroyUnendedStdio, 8000);
425
- hardTimer.unref();
426
- });
427
- child.on("close", clearTimers);
428
- child.on("error", clearTimers);
429
-
430
- return clearTimers;
412
+ const hard = setTimeout(destroyUnendedStdio, 8000);
413
+ hard.unref();
414
+ state = { kind: "draining", idle: undefined, hard };
415
+ armIdleTimer();
416
+ };
417
+ function cleanup(): void {
418
+ if (state.kind === "draining") {
419
+ clearTimeout(state.idle);
420
+ clearTimeout(state.hard);
421
+ }
422
+ state = { kind: "closed" };
423
+ child.stdout?.off("data", armIdleTimer);
424
+ child.stderr?.off("data", armIdleTimer);
425
+ child.stdout?.off("end", stdoutEnd);
426
+ child.stderr?.off("end", stderrEnd);
427
+ child.off("exit", exited);
428
+ child.off("close", cleanup);
429
+ child.off("error", cleanup);
430
+ }
431
+ child.stdout?.on("data", armIdleTimer);
432
+ child.stderr?.on("data", armIdleTimer);
433
+ child.stdout?.on("end", stdoutEnd);
434
+ child.stderr?.on("end", stderrEnd);
435
+ child.on("exit", exited);
436
+ child.on("close", cleanup);
437
+ child.on("error", cleanup);
438
+ return cleanup;
431
439
  }