@mozhi0012/pi-terminal-worker 0.2.0 → 0.4.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 (50) hide show
  1. package/README.md +220 -169
  2. package/dist/controller.d.ts +9 -0
  3. package/dist/controller.d.ts.map +1 -1
  4. package/dist/controller.js +106 -53
  5. package/dist/controller.js.map +1 -1
  6. package/dist/launcher.d.ts +9 -1
  7. package/dist/launcher.d.ts.map +1 -1
  8. package/dist/launcher.js +95 -22
  9. package/dist/launcher.js.map +1 -1
  10. package/dist/lifecycle.d.ts +7 -0
  11. package/dist/lifecycle.d.ts.map +1 -1
  12. package/dist/lifecycle.js +4 -0
  13. package/dist/lifecycle.js.map +1 -1
  14. package/dist/multi-worker.d.ts +20 -0
  15. package/dist/multi-worker.d.ts.map +1 -1
  16. package/dist/multi-worker.js +53 -10
  17. package/dist/multi-worker.js.map +1 -1
  18. package/dist/protocol.d.ts +2 -0
  19. package/dist/protocol.d.ts.map +1 -1
  20. package/dist/protocol.js +2 -0
  21. package/dist/protocol.js.map +1 -1
  22. package/dist/team-config.d.ts +61 -0
  23. package/dist/team-config.d.ts.map +1 -0
  24. package/dist/team-config.js +195 -0
  25. package/dist/team-config.js.map +1 -0
  26. package/dist/terminal-multiplexer.d.ts +25 -0
  27. package/dist/terminal-multiplexer.d.ts.map +1 -0
  28. package/dist/terminal-multiplexer.js +102 -0
  29. package/dist/terminal-multiplexer.js.map +1 -0
  30. package/dist/tool-policy.d.ts +36 -0
  31. package/dist/tool-policy.d.ts.map +1 -0
  32. package/dist/tool-policy.js +199 -0
  33. package/dist/tool-policy.js.map +1 -0
  34. package/dist/worker.d.ts +1 -0
  35. package/dist/worker.d.ts.map +1 -1
  36. package/dist/worker.js +9 -1
  37. package/dist/worker.js.map +1 -1
  38. package/package.json +78 -72
  39. package/runtime/bootstrap-support.mjs +68 -49
  40. package/runtime/bootstrap.mjs +328 -304
  41. package/runtime/terminal-support.mjs +63 -0
  42. package/src/controller.ts +2761 -2724
  43. package/src/launcher.ts +592 -517
  44. package/src/lifecycle.ts +289 -284
  45. package/src/multi-worker.ts +69 -9
  46. package/src/protocol.ts +5 -0
  47. package/src/team-config.ts +255 -0
  48. package/src/terminal-multiplexer.ts +108 -0
  49. package/src/tool-policy.ts +213 -0
  50. package/src/worker.ts +9 -1
@@ -0,0 +1,63 @@
1
+ /** Terminal/process cleanup shared by supervisor and focused tests. */
2
+ import * as fs from "node:fs";
3
+ import { spawnSync } from "node:child_process";
4
+
5
+ export function closeHerdrPane(env = process.env, run = spawnSync) {
6
+ // Only the bootstrap launched by our herdr backend gets this marker.
7
+ if (!env.PITW_BOOTSTRAP_HERDR || !env.HERDR_PANE_ID) return;
8
+ try {
9
+ run(env.PITW_BOOTSTRAP_HERDR, ["pane", "close", env.HERDR_PANE_ID], {
10
+ env, stdio: "ignore", windowsHide: true, shell: false, timeout: 3000,
11
+ });
12
+ } catch { /* Already closed is success; Controller also has a cleanup handle. */ }
13
+ }
14
+
15
+ export function listProcessParents(platform = process.platform) {
16
+ if (platform === "linux") {
17
+ const result = [];
18
+ for (const entry of fs.readdirSync("/proc")) {
19
+ if (!/^\d+$/.test(entry)) continue;
20
+ try {
21
+ const stat = fs.readFileSync(`/proc/${entry}/stat`, "utf8");
22
+ // comm may contain spaces and parentheses; fields after its final ')' are stable.
23
+ const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
24
+ result.push({ pid: Number(entry), ppid: Number(fields[1]) });
25
+ } catch { /* Process exited during enumeration. */ }
26
+ }
27
+ return result;
28
+ }
29
+ const result = spawnSync("ps", ["-eo", "pid=,ppid="], { encoding: "utf8", timeout: 3000 });
30
+ if (result.error || result.status !== 0) throw new Error("无法枚举进程树");
31
+ return result.stdout.trim().split("\n").map((line) => {
32
+ const [pid, ppid] = line.trim().split(/\s+/).map(Number);
33
+ return { pid, ppid };
34
+ });
35
+ }
36
+
37
+ /** Freeze parents before rescanning, then kill descendants bottom-up without detaching from the TTY. */
38
+ export function killProcessTree(pid, { list = listProcessParents, signal = process.kill } = {}) {
39
+ if (!Number.isSafeInteger(pid) || pid <= 0) return;
40
+ const ordered = [pid];
41
+ const known = new Set(ordered);
42
+ const send = (target, name) => { try { signal(target, name); } catch { /* exited or inaccessible */ } };
43
+ send(pid, "SIGSTOP");
44
+ try {
45
+ // Once a discovered parent is frozen it cannot fork new descendants.
46
+ for (let pass = 0; pass < 64; pass++) {
47
+ const snapshot = list();
48
+ let added = false;
49
+ for (let index = 0; index < ordered.length; index++) {
50
+ for (const process of snapshot) {
51
+ if (process.ppid !== ordered[index] || known.has(process.pid)) continue;
52
+ known.add(process.pid);
53
+ ordered.push(process.pid);
54
+ send(process.pid, "SIGSTOP");
55
+ added = true;
56
+ }
57
+ }
58
+ if (!added) break;
59
+ }
60
+ } finally {
61
+ for (const target of ordered.reverse()) send(target, "SIGKILL");
62
+ }
63
+ }