@cydm/pie 2.8.1 → 4.0.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.
Files changed (42) hide show
  1. package/dist/builtin/extensions/ask-user/index.js +8 -4
  2. package/dist/builtin/extensions/changelog/index.js +2 -2
  3. package/dist/builtin/extensions/cron/index.js +295 -33
  4. package/dist/builtin/extensions/document-attachments/index.js +1 -1
  5. package/dist/builtin/extensions/herdr-agent-state/index.js +2 -2
  6. package/dist/builtin/extensions/im-forward/README.md +20 -5
  7. package/dist/builtin/extensions/im-forward/index.js +415 -142
  8. package/dist/builtin/extensions/memory/index.js +83 -30
  9. package/dist/builtin/extensions/pie-ipc/README.md +2 -2
  10. package/dist/builtin/extensions/pie-ipc/index.js +269 -30
  11. package/dist/builtin/extensions/pie-proc/README.md +93 -0
  12. package/dist/builtin/extensions/pie-proc/index.js +630 -0
  13. package/dist/builtin/extensions/pie-proc/package.json +11 -0
  14. package/dist/builtin/extensions/plan-mode/index.js +4 -4
  15. package/dist/builtin/extensions/subagent/index.js +6 -6
  16. package/dist/builtin/extensions/todo/index.js +4 -4
  17. package/dist/builtin/skills/chrome-devtools-axi/chrome-devtools-axi-bridge.js +106 -2
  18. package/dist/builtin/skills/chrome-devtools-axi/chrome-devtools-axi-cli.js +1 -0
  19. package/dist/builtin/skills/chrome-devtools-axi/chrome-devtools-axi-runtime.js +1 -1
  20. package/dist/builtin/skills/chrome-devtools-axi/{chunk-BNSAEVRT.js → chunk-WOGAQ4P5.js} +2 -2
  21. package/dist/builtin/skills/chrome-devtools-axi/{cli-AO2ZTPRM.js → cli-CHC7XYFU.js} +27 -3
  22. package/dist/builtin/skills/computer-use-axi/computer-use-axi-broker.js +2 -2
  23. package/dist/builtin/skills/computer-use-axi/computer-use-axi-cli.js +1 -0
  24. package/dist/builtin/skills/computer-use-axi/scripts/setup-helper.mjs +6 -2
  25. package/dist/builtin/skills/tui-use/tui-use-daemon.cjs +179 -9
  26. package/dist/chunks/{chunk-PK72LVWQ.js → chunk-FDLLNPIE.js} +143 -53
  27. package/dist/chunks/{chunk-3BC5KGDQ.js → chunk-JQRZLVPY.js} +12 -1
  28. package/dist/chunks/{chunk-GH4VW2OT.js → chunk-NCP264ZE.js} +8 -2
  29. package/dist/chunks/{chunk-YRR6C43W.js → chunk-QX6WS73C.js} +1 -1
  30. package/dist/chunks/{chunk-3NHP7HK5.js → chunk-TDKNKNPL.js} +6 -5
  31. package/dist/chunks/{chunk-GDIYSMTS.js → chunk-UOZHQ73X.js} +25 -17
  32. package/dist/chunks/{chunk-RSL7E4R7.js → chunk-WK4N5HT5.js} +66 -23
  33. package/dist/chunks/chunk-X6LBWHK3.js +37 -0
  34. package/dist/chunks/concierge-cli-PIOGXKDR.js +939 -0
  35. package/dist/chunks/{model-registry-LI5THXV4.js → model-registry-YA6U366L.js} +2 -2
  36. package/dist/chunks/{src-BPSQMSSC.js → src-AB7PLORG.js} +6 -4
  37. package/dist/chunks/{src-YG5M2H44.js → src-ODLT57PQ.js} +2 -2
  38. package/dist/chunks/{src-66XSMIAM.js → src-XSLD2IQ2.js} +1 -1
  39. package/dist/chunks/{src-OPPYTPWG.js → src-ZZZO4HSR.js} +11 -3
  40. package/dist/chunks/{test-stream-JBWXKOQA.js → test-stream-GIUD5JWS.js} +1 -1
  41. package/dist/cli.js +1433 -235
  42. package/package.json +105 -105
@@ -5,7 +5,7 @@ import {
5
5
  createCliRetryPolicy,
6
6
  formatTransportLifecycle,
7
7
  logTransportLifecycle
8
- } from "../../../chunks/chunk-GDIYSMTS.js";
8
+ } from "../../../chunks/chunk-UOZHQ73X.js";
9
9
  import {
10
10
  getConfigDir
11
11
  } from "../../../chunks/chunk-ILUHTKF3.js";
@@ -14,10 +14,10 @@ import {
14
14
  createPolicyEnforcedTools,
15
15
  createSharedFileSystemTools,
16
16
  createSubagentCapability
17
- } from "../../../chunks/chunk-PK72LVWQ.js";
18
- import "../../../chunks/chunk-RSL7E4R7.js";
19
- import "../../../chunks/chunk-GH4VW2OT.js";
20
- import "../../../chunks/chunk-3BC5KGDQ.js";
17
+ } from "../../../chunks/chunk-FDLLNPIE.js";
18
+ import "../../../chunks/chunk-WK4N5HT5.js";
19
+ import "../../../chunks/chunk-NCP264ZE.js";
20
+ import "../../../chunks/chunk-JQRZLVPY.js";
21
21
  import "../../../chunks/chunk-XXGYUYXI.js";
22
22
  import "../../../chunks/chunk-LZQXQOPJ.js";
23
23
 
@@ -43,7 +43,7 @@ function defaultReadProcessStartTime(pid) {
43
43
  "-Command",
44
44
  `$p = Get-CimInstance Win32_Process -Filter "ProcessId=${pid}"; if ($p) { $p.CreationDate.ToString('o') }`
45
45
  ],
46
- { encoding: "utf-8", timeout: 4e3 }
46
+ { encoding: "utf-8", timeout: 4e3, windowsHide: true }
47
47
  );
48
48
  const line = output.split(/\r?\n/).find((entry) => entry.trim().length > 0);
49
49
  return line?.trim();
@@ -8,10 +8,10 @@ import {
8
8
  executeManageTodoList,
9
9
  executionStateToTodos,
10
10
  restoreExecutionState
11
- } from "../../../chunks/chunk-PK72LVWQ.js";
12
- import "../../../chunks/chunk-RSL7E4R7.js";
13
- import "../../../chunks/chunk-GH4VW2OT.js";
14
- import "../../../chunks/chunk-3BC5KGDQ.js";
11
+ } from "../../../chunks/chunk-FDLLNPIE.js";
12
+ import "../../../chunks/chunk-WK4N5HT5.js";
13
+ import "../../../chunks/chunk-NCP264ZE.js";
14
+ import "../../../chunks/chunk-JQRZLVPY.js";
15
15
  import "../../../chunks/chunk-XXGYUYXI.js";
16
16
  import "../../../chunks/chunk-LZQXQOPJ.js";
17
17
 
@@ -4,11 +4,12 @@ import {
4
4
  BRIDGE_PORT_IN_USE_EXIT_CODE,
5
5
  PAGE_IDENTITY_CHANGED_ERROR,
6
6
  clearSelectedPageId,
7
+ inspectProcessIdentity,
7
8
  readSelfStartTime,
8
9
  resolveSessionName,
9
10
  resolveSessionPidFile,
10
11
  resolveSessionPort
11
- } from "./chunk-BNSAEVRT.js";
12
+ } from "./chunk-WOGAQ4P5.js";
12
13
  import {
13
14
  __commonJS,
14
15
  __export,
@@ -19571,6 +19572,36 @@ import {
19571
19572
  } from "node:fs";
19572
19573
  import { basename, dirname, isAbsolute, join } from "node:path";
19573
19574
  import { pathToFileURL } from "node:url";
19575
+
19576
+ // vendor/chrome-devtools-axi/src/supervisor-anchor-shared.ts
19577
+ var SUPERVISOR_PID_ENV = "PIE_SUPERVISOR_PID";
19578
+ var SUPERVISOR_START_TIME_ENV = "PIE_SUPERVISOR_START_TIME";
19579
+ function parseSupervisorAnchor(env = process.env) {
19580
+ const pidText = env[SUPERVISOR_PID_ENV]?.trim();
19581
+ if (!pidText || !/^\d+$/.test(pidText)) return void 0;
19582
+ const pid = Number(pidText);
19583
+ if (!Number.isSafeInteger(pid) || pid <= 0) return void 0;
19584
+ const startTime = env[SUPERVISOR_START_TIME_ENV]?.trim();
19585
+ return startTime ? { pid, startTime } : { pid };
19586
+ }
19587
+ function isSupervisorGone(anchor2, readIdentity) {
19588
+ try {
19589
+ process.kill(anchor2.pid, 0);
19590
+ } catch (error2) {
19591
+ return error2.code === "ESRCH";
19592
+ }
19593
+ let identity;
19594
+ try {
19595
+ identity = readIdentity(anchor2.pid);
19596
+ } catch {
19597
+ return false;
19598
+ }
19599
+ if (identity.startTime === void 0) return false;
19600
+ if (anchor2.startTime === void 0) return false;
19601
+ return identity.startTime !== anchor2.startTime;
19602
+ }
19603
+
19604
+ // vendor/chrome-devtools-axi/src/bridge.ts
19574
19605
  async function isBridgeClientConnected(client) {
19575
19606
  try {
19576
19607
  await client.listTools();
@@ -19849,6 +19880,72 @@ function handleBridgeServerError(error2, port, exit = process.exit) {
19849
19880
  function writeReadySignal() {
19850
19881
  process.stdout.write("READY\n");
19851
19882
  }
19883
+ var LAUNCHER_HEADER = "x-axi-launcher";
19884
+ var DEFAULT_HEARTBEAT_MS = 3e4;
19885
+ var DEFAULT_IDLE_SHUTDOWN_MS = 30 * 6e4;
19886
+ var parseSupervisorAnchor2 = parseSupervisorAnchor;
19887
+ var isSupervisorGone2 = (supervisor) => isSupervisorGone(supervisor, inspectProcessIdentity);
19888
+ function parseLauncherHeader(value) {
19889
+ const raw = Array.isArray(value) ? value[0] : value;
19890
+ if (!raw) return void 0;
19891
+ const colon = raw.indexOf(":");
19892
+ const pidText = colon === -1 ? raw : raw.slice(0, colon);
19893
+ if (!/^\d+$/.test(pidText)) return void 0;
19894
+ const pid = Number(pidText);
19895
+ if (!Number.isSafeInteger(pid) || pid <= 0) return void 0;
19896
+ const startTime = colon === -1 ? void 0 : raw.slice(colon + 1);
19897
+ return startTime ? { pid, startTime } : { pid };
19898
+ }
19899
+ function createBridgeLifecycle(shutdown, opts = {}) {
19900
+ const heartbeatMs = opts.heartbeatMs ?? DEFAULT_HEARTBEAT_MS;
19901
+ const idleShutdownMs = opts.idleShutdownMs ?? DEFAULT_IDLE_SHUTDOWN_MS;
19902
+ const now = opts.now ?? Date.now;
19903
+ let supervisor;
19904
+ let supervisorFromEnv = false;
19905
+ if (opts.anchor) {
19906
+ supervisor = opts.anchor;
19907
+ supervisorFromEnv = true;
19908
+ }
19909
+ let lastRequestAt = now();
19910
+ let disposed = false;
19911
+ const fire = (reason) => {
19912
+ disposed = true;
19913
+ clearInterval(timer);
19914
+ logBridgeMessage(reason);
19915
+ try {
19916
+ void shutdown();
19917
+ } catch {
19918
+ }
19919
+ };
19920
+ const tick = () => {
19921
+ if (disposed) return;
19922
+ const t = now();
19923
+ if (t - lastRequestAt >= idleShutdownMs) {
19924
+ fire(`idle ${Math.round((t - lastRequestAt) / 1e3)}s \u2265 ${Math.round(idleShutdownMs / 1e3)}s with no requests; shutting down`);
19925
+ return;
19926
+ }
19927
+ if (supervisor && isSupervisorGone2(supervisor)) {
19928
+ fire(`supervisor pid ${supervisor.pid} is gone and no requests since; shutting down`);
19929
+ return;
19930
+ }
19931
+ };
19932
+ const timer = setInterval(tick, heartbeatMs);
19933
+ timer.unref?.();
19934
+ return {
19935
+ observeRequest(req) {
19936
+ lastRequestAt = now();
19937
+ if (supervisorFromEnv) return;
19938
+ const next = parseLauncherHeader(req.headers[LAUNCHER_HEADER]);
19939
+ if (!next) return;
19940
+ if (supervisor?.startTime && !next.startTime) return;
19941
+ supervisor = next;
19942
+ },
19943
+ dispose() {
19944
+ disposed = true;
19945
+ clearInterval(timer);
19946
+ }
19947
+ };
19948
+ }
19852
19949
  var KEYCHAIN_ISOLATION_CHROME_ARGS = [
19853
19950
  "--use-mock-keychain",
19854
19951
  "--password-store=basic"
@@ -19913,7 +20010,8 @@ var DEFAULT_MCP_PATH_PROBE = {
19913
20010
  try {
19914
20011
  return execSync("npm prefix -g", {
19915
20012
  encoding: "utf8",
19916
- stdio: ["ignore", "pipe", "ignore"]
20013
+ stdio: ["ignore", "pipe", "ignore"],
20014
+ windowsHide: true
19917
20015
  }).trim();
19918
20016
  } catch {
19919
20017
  return null;
@@ -19954,6 +20052,7 @@ function resolveTransportSpec(probe = DEFAULT_MCP_PATH_PROBE) {
19954
20052
  help = execFileSync(process.execPath, [explicitPath, "--help"], {
19955
20053
  encoding: "utf8",
19956
20054
  stdio: ["ignore", "pipe", "pipe"],
20055
+ windowsHide: true,
19957
20056
  timeout: 5e3,
19958
20057
  killSignal: "SIGKILL"
19959
20058
  });
@@ -20152,6 +20251,10 @@ async function runBridge(port = resolveSessionPort()) {
20152
20251
  logBridgeMessage("Connected to chrome-devtools-mcp");
20153
20252
  const sessionName = resolveSessionName();
20154
20253
  const server = createBridgeServer(bridgeClient, sessionName);
20254
+ const lifecycle = createBridgeLifecycle(() => shutdown(), {
20255
+ anchor: parseSupervisorAnchor2()
20256
+ });
20257
+ server.on("request", (req) => lifecycle.observeRequest(req));
20155
20258
  server.on("error", (error2) => {
20156
20259
  handleBridgeServerError(error2, port);
20157
20260
  });
@@ -20164,6 +20267,7 @@ async function runBridge(port = resolveSessionPort()) {
20164
20267
  const shutdown = async () => {
20165
20268
  if (shuttingDown) return;
20166
20269
  shuttingDown = true;
20270
+ lifecycle.dispose();
20167
20271
  removePidFile();
20168
20272
  await closeServer(server);
20169
20273
  await closeBridgeTransport(bridgeTransport);
@@ -39,6 +39,7 @@ export function resolveChromeDevtoolsMcpScript() {
39
39
  export function runChromeDevtoolsAxiCli(args = process.argv.slice(2), options = {}) {
40
40
  const bin = resolveChromeDevtoolsAxiCli();
41
41
  return spawnSync(process.execPath, [bin, ...args], {
42
+ windowsHide: true,
42
43
  stdio: "inherit",
43
44
  shell: false,
44
45
  ...options,
@@ -21,6 +21,6 @@ function tryFastPath(argv, options) {
21
21
 
22
22
  // vendor/chrome-devtools-axi/bin/chrome-devtools-axi.ts
23
23
  if (!tryFastPath(process.argv.slice(2), { version: VERSION })) {
24
- const { main } = await import("./cli-AO2ZTPRM.js");
24
+ const { main } = await import("./cli-CHC7XYFU.js");
25
25
  await main(process.argv.slice(2));
26
26
  }
@@ -334,7 +334,7 @@ function inspectProcessIdentity(pid) {
334
334
  "-Command",
335
335
  `$p = Get-CimInstance Win32_Process -Filter "ProcessId=<pid>"; if ($p) { $p.CommandLine; $p.CreationDate.ToString('o') }`.replace("<pid>", String(pid))
336
336
  ],
337
- { encoding: "utf-8", timeout: 4e3 }
337
+ { encoding: "utf-8", timeout: 4e3, windowsHide: true }
338
338
  );
339
339
  const lines = output.split(/\r?\n/).filter((line) => line.length > 0);
340
340
  if (lines.length === 0) return {};
@@ -345,7 +345,7 @@ function inspectProcessIdentity(pid) {
345
345
  }
346
346
  }
347
347
  try {
348
- const psEnv = { ...process.env, LC_ALL: "C", LANG: "C" };
348
+ const psEnv = { ...process.env, LC_ALL: "C", LANG: "C", TZ: "UTC0" };
349
349
  const output = execFileSync(
350
350
  "ps",
351
351
  ["-p", String(pid), "-o", "lstart=", "-o", "command="],
@@ -12,6 +12,7 @@ import {
12
12
  needsPageId,
13
13
  overlaySessionSelected,
14
14
  parsePagesList,
15
+ readSelfStartTime,
15
16
  rememberToolRouting,
16
17
  resolveBridgeScript,
17
18
  resolveSessionName,
@@ -19,7 +20,7 @@ import {
19
20
  resolveSessionPort,
20
21
  resolveSessionStateDir,
21
22
  setSelectedPageId
22
- } from "./chunk-BNSAEVRT.js";
23
+ } from "./chunk-WOGAQ4P5.js";
23
24
  import "./chunk-OJ6N23UZ.js";
24
25
 
25
26
  // ../../node_modules/chrome-devtools-axi/node_modules/@toon-format/toon/dist/index.mjs
@@ -1661,10 +1662,31 @@ function isProcessAlive(pid) {
1661
1662
  return error?.code !== "ESRCH";
1662
1663
  }
1663
1664
  }
1665
+ var cachedLauncherHeader;
1666
+ function launcherHeader() {
1667
+ cachedLauncherHeader ??= (() => {
1668
+ const pid = process.env.PIE_SUPERVISOR_PID?.trim();
1669
+ if (pid && Number.isInteger(Number(pid)) && Number(pid) > 0) {
1670
+ const startTime = process.env.PIE_SUPERVISOR_START_TIME?.trim() ?? "";
1671
+ return `${pid}:${startTime}`;
1672
+ }
1673
+ return `${process.pid}:${readSelfStartTime() ?? ""}`;
1674
+ })();
1675
+ return cachedLauncherHeader;
1676
+ }
1664
1677
  function httpGet(port, path, timeoutMs = 2e3) {
1665
1678
  return new Promise((resolve3, reject) => {
1666
1679
  const req = request(
1667
- { hostname: "127.0.0.1", port, path, method: "GET", timeout: timeoutMs },
1680
+ {
1681
+ hostname: "127.0.0.1",
1682
+ port,
1683
+ path,
1684
+ method: "GET",
1685
+ timeout: timeoutMs,
1686
+ // Identify this launcher to the bridge so it can supervise our
1687
+ // liveness and reap its detached tree when we die (leak fix).
1688
+ headers: { "x-axi-launcher": launcherHeader() }
1689
+ },
1668
1690
  (res) => {
1669
1691
  res.setEncoding("utf8");
1670
1692
  let data = "";
@@ -1829,7 +1851,7 @@ function isBridgeProcess(pid, expectedStartTime) {
1829
1851
  "-Command",
1830
1852
  `(Get-CimInstance Win32_Process -Filter "ProcessId=${pid}").CommandLine`
1831
1853
  ],
1832
- { encoding: "utf-8", timeout: 4e3 }
1854
+ { encoding: "utf-8", timeout: 4e3, windowsHide: true }
1833
1855
  );
1834
1856
  if (!commandLineLooksLikeBridge(command)) return false;
1835
1857
  } catch {
@@ -1905,6 +1927,7 @@ function taskkillTree(pid) {
1905
1927
  try {
1906
1928
  execFileSync("taskkill", ["/PID", String(pid), "/T", "/F"], {
1907
1929
  stdio: "ignore",
1930
+ windowsHide: true,
1908
1931
  timeout: 1e4
1909
1932
  });
1910
1933
  return true;
@@ -1964,6 +1987,7 @@ function spawnBridgeProcess(port, sessionName) {
1964
1987
  runner === "tsx" ? ["tsx", script] : [script],
1965
1988
  {
1966
1989
  stdio: "ignore",
1990
+ windowsHide: true,
1967
1991
  env: {
1968
1992
  ...process.env,
1969
1993
  CHROME_DEVTOOLS_AXI_PORT: String(port),
@@ -7573,7 +7573,7 @@ async function runProcess3(command, args, timeoutMs, signal, env) {
7573
7573
  if (signal?.aborted) throw new Error("Operation aborted.");
7574
7574
  await new Promise((resolve2, reject) => {
7575
7575
  assertNativeBackendAllowed();
7576
- const child = spawn3(command, args, { stdio: ["ignore", "pipe", "pipe"], env });
7576
+ const child = spawn3(command, args, { stdio: ["ignore", "pipe", "pipe"], env, windowsHide: true });
7577
7577
  let stdout = "";
7578
7578
  let stderr = "";
7579
7579
  const cleanup = () => {
@@ -7643,7 +7643,7 @@ var WindowsHelperClient = class {
7643
7643
  await this.ensureInstalled(signal);
7644
7644
  if (this.child && this.child.exitCode === null && !this.child.killed) return this.child;
7645
7645
  assertNativeBackendAllowed();
7646
- const child = spawn3(WINDOWS_HELPER_PATH, [], { stdio: ["pipe", "pipe", "pipe"] });
7646
+ const child = spawn3(WINDOWS_HELPER_PATH, [], { stdio: ["pipe", "pipe", "pipe"], windowsHide: true });
7647
7647
  child.stdout.setEncoding("utf8");
7648
7648
  child.stderr.setEncoding("utf8");
7649
7649
  child.stdin.setDefaultEncoding("utf8");
@@ -27,6 +27,7 @@ export function resolveComputerUseAxiCli() {
27
27
  function main() {
28
28
  const runtime = resolveComputerUseAxiCli();
29
29
  const result = spawnSync(process.execPath, [runtime, ...process.argv.slice(2)], {
30
+ windowsHide: true,
30
31
  stdio: "inherit",
31
32
  shell: false,
32
33
  env: process.env,
@@ -123,7 +123,11 @@ async function copyIfChanged(sourcePath, destinationPath) {
123
123
 
124
124
  async function run(command, commandArgs) {
125
125
  await new Promise((resolve, reject) => {
126
- const child = spawn(command, commandArgs, { stdio: "inherit" });
126
+ // windowsHide: console-less hosts (Node spawning this script on Windows,
127
+ // e.g. the helper auto-install path) must not flash conhost windows at
128
+ // the user; when run interactively from a real terminal the flag is a
129
+ // no-op (the child still inherits stdio below).
130
+ const child = spawn(command, commandArgs, { stdio: "inherit", windowsHide: true });
127
131
  child.on("error", reject);
128
132
  child.on("close", (code) => {
129
133
  if (code === 0) {
@@ -140,7 +144,7 @@ function moduleCachePath(arch) {
140
144
  }
141
145
 
142
146
  async function commandOutput(command, commandArgs) {
143
- const { stdout } = await execFile(command, commandArgs, { encoding: "utf8" });
147
+ const { stdout } = await execFile(command, commandArgs, { encoding: "utf8", windowsHide: true });
144
148
  return stdout;
145
149
  }
146
150
 
@@ -7071,6 +7071,51 @@ function resolveExecutable(file, cwd = process.cwd(), env = process.env) {
7071
7071
  }
7072
7072
 
7073
7073
  // vendor/tui-use/src/session.ts
7074
+ var import_node_child_process = require("node:child_process");
7075
+ async function listDescendants(rootPid) {
7076
+ if (process.platform === "win32") return [];
7077
+ const out = [];
7078
+ const frontier = [rootPid];
7079
+ const seen = /* @__PURE__ */ new Set([rootPid]);
7080
+ while (frontier.length > 0) {
7081
+ const parent = frontier.shift();
7082
+ const children = await new Promise((resolve) => {
7083
+ (0, import_node_child_process.execFile)("pgrep", ["-P", String(parent)], { timeout: 2e3 }, (error, stdout) => {
7084
+ resolve(error ? [] : stdout.split("\n").map((l) => parseInt(l, 10)).filter((n) => Number.isInteger(n) && n > 0));
7085
+ });
7086
+ });
7087
+ for (const child of children) {
7088
+ if (!seen.has(child)) {
7089
+ seen.add(child);
7090
+ out.push(child);
7091
+ frontier.push(child);
7092
+ }
7093
+ }
7094
+ }
7095
+ return out;
7096
+ }
7097
+ async function sweepDescendants(pids) {
7098
+ const alive = (pid) => {
7099
+ try {
7100
+ process.kill(pid, 0);
7101
+ return true;
7102
+ } catch {
7103
+ return false;
7104
+ }
7105
+ };
7106
+ for (const pid of pids) {
7107
+ if (alive(pid)) {
7108
+ try {
7109
+ process.kill(pid, "SIGKILL");
7110
+ } catch {
7111
+ }
7112
+ }
7113
+ }
7114
+ const deadline = Date.now() + 2e3;
7115
+ while (Date.now() < deadline && pids.some(alive)) {
7116
+ await new Promise((resolve) => setTimeout(resolve, 25));
7117
+ }
7118
+ }
7074
7119
  function getCellStyle(cell) {
7075
7120
  return {
7076
7121
  fg: cell.getFgColor(),
@@ -7475,14 +7520,28 @@ Supported keys: ${supported}`
7475
7520
  }
7476
7521
  async killProcess() {
7477
7522
  if (this._status === "running") {
7523
+ const descendants = await listDescendants(this.ptyProcess.pid);
7478
7524
  this.ptyProcess.kill();
7479
- const deadline = Date.now() + 3e3;
7525
+ let deadline = Date.now() + 1e3;
7480
7526
  while (this._status === "running" && Date.now() < deadline) {
7481
7527
  await new Promise((resolve) => setTimeout(resolve, 25));
7482
7528
  }
7529
+ if (this._status === "running") {
7530
+ try {
7531
+ this.ptyProcess.kill("SIGKILL");
7532
+ } catch {
7533
+ }
7534
+ deadline = Date.now() + 2e3;
7535
+ while (this._status === "running" && Date.now() < deadline) {
7536
+ await new Promise((resolve) => setTimeout(resolve, 25));
7537
+ }
7538
+ }
7483
7539
  if (this._status === "running") {
7484
7540
  throw new Error(`Session ${this.id} did not exit after kill.`);
7485
7541
  }
7542
+ if (process.platform !== "win32" && descendants.length > 0) {
7543
+ await sweepDescendants(descendants);
7544
+ }
7486
7545
  }
7487
7546
  }
7488
7547
  dispose() {
@@ -7674,6 +7733,59 @@ var PID_PATH = import_node_path3.default.join(STATE_DIR, "daemon.pid");
7674
7733
  var LOCK_PATH = import_node_path3.default.join(STATE_DIR, "startup.lock");
7675
7734
  var SOCKET_PATH = process.platform === "win32" ? `\\\\.\\pipe\\pie-tui-${(0, import_node_crypto.createHash)("sha256").update(STATE_DIR.toLowerCase()).digest("hex").slice(0, 24)}` : import_node_path3.default.join(STATE_DIR, "daemon.sock");
7676
7735
 
7736
+ // vendor/tui-use/src/supervisor-anchor-shared.ts
7737
+ var SUPERVISOR_PID_ENV = "PIE_SUPERVISOR_PID";
7738
+ var SUPERVISOR_START_TIME_ENV = "PIE_SUPERVISOR_START_TIME";
7739
+ function parseSupervisorAnchor(env = process.env) {
7740
+ const pidText = env[SUPERVISOR_PID_ENV]?.trim();
7741
+ if (!pidText || !/^\d+$/.test(pidText)) return void 0;
7742
+ const pid = Number(pidText);
7743
+ if (!Number.isSafeInteger(pid) || pid <= 0) return void 0;
7744
+ const startTime = env[SUPERVISOR_START_TIME_ENV]?.trim();
7745
+ return startTime ? { pid, startTime } : { pid };
7746
+ }
7747
+ function readStartTime(pid) {
7748
+ if (!Number.isInteger(pid) || pid <= 0) return void 0;
7749
+ if (process.platform === "win32") {
7750
+ try {
7751
+ const { execFileSync } = require("node:child_process");
7752
+ return execFileSync(
7753
+ "powershell",
7754
+ [
7755
+ "-NoProfile",
7756
+ "-NonInteractive",
7757
+ "-Command",
7758
+ `$p = Get-CimInstance Win32_Process -Filter "ProcessId=${pid}"; if ($p) { $p.CreationDate.ToString('o') }`
7759
+ ],
7760
+ { timeout: 5e3, encoding: "utf8", windowsHide: true }
7761
+ ).trim() || void 0;
7762
+ } catch {
7763
+ return void 0;
7764
+ }
7765
+ }
7766
+ try {
7767
+ const { execFileSync } = require("node:child_process");
7768
+ return execFileSync("ps", ["-p", String(pid), "-o", "lstart="], {
7769
+ timeout: 5e3,
7770
+ env: { ...process.env, LC_ALL: "C", LANG: "C", TZ: "UTC0" },
7771
+ encoding: "utf8"
7772
+ }).trim() || void 0;
7773
+ } catch {
7774
+ return void 0;
7775
+ }
7776
+ }
7777
+ function isSupervisorGone(anchor) {
7778
+ try {
7779
+ process.kill(anchor.pid, 0);
7780
+ } catch (error) {
7781
+ return error.code === "ESRCH";
7782
+ }
7783
+ const identity = { startTime: readStartTime(anchor.pid) };
7784
+ if (identity.startTime === void 0) return false;
7785
+ if (anchor.startTime === void 0) return false;
7786
+ return identity.startTime !== anchor.startTime;
7787
+ }
7788
+
7677
7789
  // vendor/tui-use/package.json
7678
7790
  var version = "0.1.18-pie.3";
7679
7791
 
@@ -7681,6 +7793,9 @@ var version = "0.1.18-pie.3";
7681
7793
  var sessions = /* @__PURE__ */ new Map();
7682
7794
  var exitedAt = /* @__PURE__ */ new Map();
7683
7795
  var busy = /* @__PURE__ */ new Map();
7796
+ var sessionAnchors = /* @__PURE__ */ new Map();
7797
+ var lastSessionActivity = /* @__PURE__ */ new Map();
7798
+ var SESSION_MAX_IDLE_AGE_MS = 12 * 60 * 60 * 1e3;
7684
7799
  var instanceId = (0, import_node_crypto2.randomUUID)();
7685
7800
  var idleSetting = Number(process.env.PIE_TUI_USE_IDLE_TIMEOUT_MS);
7686
7801
  var idleTimeout = Number.isInteger(idleSetting) && idleSetting >= 100 && idleSetting <= 3e5 ? idleSetting : 3e5;
@@ -7694,9 +7809,53 @@ function pruneExited() {
7694
7809
  sessions.get(id)?.dispose();
7695
7810
  sessions.delete(id);
7696
7811
  exitedAt.delete(id);
7812
+ sessionAnchors.delete(id);
7813
+ lastSessionActivity.delete(id);
7697
7814
  if (currentSession === id) currentSession = null;
7698
7815
  }
7699
7816
  }
7817
+ function reapOrphanedSessions() {
7818
+ if (shuttingDown) return;
7819
+ for (const id of [...sessionAnchors.keys()]) {
7820
+ if (!sessions.has(id)) sessionAnchors.delete(id);
7821
+ }
7822
+ for (const [id, anchor] of sessionAnchors) {
7823
+ if (!anchor || busy.get(id)) continue;
7824
+ const session = sessions.get(id);
7825
+ if (!session || session.status !== "running") continue;
7826
+ if (!isSupervisorGone(anchor)) continue;
7827
+ try {
7828
+ import_node_fs2.default.appendFileSync(LOG_PATH, `[reap] session ${id} creator pid ${anchor.pid} is gone; killing PTY tree
7829
+ `);
7830
+ } catch {
7831
+ }
7832
+ void session.kill().then(() => {
7833
+ exitedAt.set(id, Date.now());
7834
+ sessionAnchors.delete(id);
7835
+ pruneExited();
7836
+ updateIdleTimer();
7837
+ }).catch(() => {
7838
+ });
7839
+ }
7840
+ for (const [id, session] of sessions) {
7841
+ if (busy.get(id) || session.status !== "running") continue;
7842
+ const anchor = sessionAnchors.get(id);
7843
+ if (anchor && !isSupervisorGone(anchor)) continue;
7844
+ const last = lastSessionActivity.get(id) ?? session.startTime;
7845
+ if (Date.now() - last < SESSION_MAX_IDLE_AGE_MS) continue;
7846
+ try {
7847
+ import_node_fs2.default.appendFileSync(LOG_PATH, `[reap] session ${id} exceeded max idle age (${Math.round((Date.now() - last) / 6e4)}min, no live anchor); killing PTY tree
7848
+ `);
7849
+ } catch {
7850
+ }
7851
+ void session.kill().then(() => {
7852
+ exitedAt.set(id, Date.now());
7853
+ pruneExited();
7854
+ updateIdleTimer();
7855
+ }).catch(() => {
7856
+ });
7857
+ }
7858
+ }
7700
7859
  function updateIdleTimer() {
7701
7860
  clearTimeout(idleTimer);
7702
7861
  if (shuttingDown || [...sessions.values()].some((session) => session.status === "running") || busy.size) return;
@@ -7705,6 +7864,8 @@ function updateIdleTimer() {
7705
7864
  }, idleTimeout);
7706
7865
  idleTimer.unref();
7707
7866
  }
7867
+ var reapTimer = setInterval(() => reapOrphanedSessions(), 3e4);
7868
+ reapTimer.unref?.();
7708
7869
  async function shutdown() {
7709
7870
  if (shuttingDown) throw new Error("Daemon is already stopping.");
7710
7871
  shuttingDown = true;
@@ -7734,14 +7895,22 @@ async function handleRequest(req, signal) {
7734
7895
  do {
7735
7896
  id2 = (0, import_node_crypto2.randomUUID)();
7736
7897
  } while (sessions.has(id2));
7737
- const session2 = new Session(id2, req.command, {
7738
- ...req,
7739
- onExit: () => {
7740
- exitedAt.set(id2, Date.now());
7741
- pruneExited();
7742
- updateIdleTimer();
7743
- }
7744
- });
7898
+ sessionAnchors.set(id2, parseSupervisorAnchor(req.env ?? process.env));
7899
+ let session2;
7900
+ try {
7901
+ session2 = new Session(id2, req.command, {
7902
+ ...req,
7903
+ onExit: () => {
7904
+ exitedAt.set(id2, Date.now());
7905
+ sessionAnchors.delete(id2);
7906
+ pruneExited();
7907
+ updateIdleTimer();
7908
+ }
7909
+ });
7910
+ } catch (error) {
7911
+ sessionAnchors.delete(id2);
7912
+ throw error;
7913
+ }
7745
7914
  sessions.set(id2, session2);
7746
7915
  currentSession = id2;
7747
7916
  updateIdleTimer();
@@ -7765,6 +7934,7 @@ async function handleRequest(req, signal) {
7765
7934
  const session = sessions.get(id);
7766
7935
  if (!session) throw new Error(`Session not found: ${id}`);
7767
7936
  busy.set(id, (busy.get(id) ?? 0) + 1);
7937
+ lastSessionActivity.set(id, Date.now());
7768
7938
  updateIdleTimer();
7769
7939
  try {
7770
7940
  switch (req.type) {