@botiverse/oar 0.79.0 → 0.80.1

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 (147) hide show
  1. package/README.md +8 -3
  2. package/dist/contracts/install.d.ts +16 -10
  3. package/dist/contracts/install.d.ts.map +1 -1
  4. package/dist/contracts/records.d.ts +2 -2
  5. package/dist/contracts/records.d.ts.map +1 -1
  6. package/dist/contracts/session-unavailable-error.d.ts +6 -4
  7. package/dist/contracts/session-unavailable-error.d.ts.map +1 -1
  8. package/dist/contracts/session-unavailable-error.js +4 -2
  9. package/dist/contracts/session-unavailable-error.js.map +1 -1
  10. package/dist/runtimes/claude/context-breakdown.js +2 -2
  11. package/dist/runtimes/claude/context-breakdown.js.map +1 -1
  12. package/dist/runtimes/codex/app-server-client.d.ts +1 -1
  13. package/dist/runtimes/codex/app-server-client.d.ts.map +1 -1
  14. package/dist/runtimes/codex/session.d.ts.map +1 -1
  15. package/dist/runtimes/codex/session.js +11 -10
  16. package/dist/runtimes/codex/session.js.map +1 -1
  17. package/dist/runtimes/pi/rpc/open.d.ts.map +1 -1
  18. package/dist/runtimes/pi/rpc/open.js +3 -1
  19. package/dist/runtimes/pi/rpc/open.js.map +1 -1
  20. package/dist/runtimes/pi/rpc/session.js +2 -2
  21. package/dist/runtimes/pi/rpc/session.js.map +1 -1
  22. package/dist/runtimes/qoder/daemon.js +1 -1
  23. package/dist/runtimes/qoder/daemon.js.map +1 -1
  24. package/dist/runtimes/qoder/session.d.ts.map +1 -1
  25. package/dist/runtimes/qoder/session.js +2 -1
  26. package/dist/runtimes/qoder/session.js.map +1 -1
  27. package/dist/runtimes/qwen/session.d.ts.map +1 -1
  28. package/dist/runtimes/qwen/session.js +3 -1
  29. package/dist/runtimes/qwen/session.js.map +1 -1
  30. package/dist/shared/acp/client-app.d.ts +17 -4
  31. package/dist/shared/acp/client-app.d.ts.map +1 -1
  32. package/dist/shared/acp/client-app.js +35 -12
  33. package/dist/shared/acp/client-app.js.map +1 -1
  34. package/dist/shared/acp/initialize.d.ts +6 -0
  35. package/dist/shared/acp/initialize.d.ts.map +1 -0
  36. package/dist/shared/acp/initialize.js +30 -0
  37. package/dist/shared/acp/initialize.js.map +1 -0
  38. package/dist/shared/acp/input.d.ts +8 -0
  39. package/dist/shared/acp/input.d.ts.map +1 -0
  40. package/dist/shared/acp/input.js +76 -0
  41. package/dist/shared/acp/input.js.map +1 -0
  42. package/dist/shared/acp/process.d.ts.map +1 -1
  43. package/dist/shared/acp/process.js +19 -2
  44. package/dist/shared/acp/process.js.map +1 -1
  45. package/dist/shared/acp/profile.d.ts +2 -0
  46. package/dist/shared/acp/profile.d.ts.map +1 -1
  47. package/dist/shared/acp/profile.js +8 -24
  48. package/dist/shared/acp/profile.js.map +1 -1
  49. package/dist/shared/acp/protocol-error.d.ts +5 -0
  50. package/dist/shared/acp/protocol-error.d.ts.map +1 -0
  51. package/dist/shared/acp/protocol-error.js +6 -0
  52. package/dist/shared/acp/protocol-error.js.map +1 -0
  53. package/dist/shared/acp/records.d.ts +10 -8
  54. package/dist/shared/acp/records.d.ts.map +1 -1
  55. package/dist/shared/acp/records.js +24 -11
  56. package/dist/shared/acp/records.js.map +1 -1
  57. package/dist/shared/acp/routing.d.ts +59 -0
  58. package/dist/shared/acp/routing.d.ts.map +1 -0
  59. package/dist/shared/acp/routing.js +374 -0
  60. package/dist/shared/acp/routing.js.map +1 -0
  61. package/dist/shared/acp/session.d.ts +28 -2
  62. package/dist/shared/acp/session.d.ts.map +1 -1
  63. package/dist/shared/acp/session.js +163 -106
  64. package/dist/shared/acp/session.js.map +1 -1
  65. package/dist/shared/acp/shared-session.d.ts +27 -0
  66. package/dist/shared/acp/shared-session.d.ts.map +1 -0
  67. package/dist/shared/acp/shared-session.js +171 -0
  68. package/dist/shared/acp/shared-session.js.map +1 -0
  69. package/dist/shared/acp/shared-wire.d.ts +9 -0
  70. package/dist/shared/acp/shared-wire.d.ts.map +1 -0
  71. package/dist/shared/acp/shared-wire.js +113 -0
  72. package/dist/shared/acp/shared-wire.js.map +1 -0
  73. package/dist/shared/acp/turns.d.ts +4 -3
  74. package/dist/shared/acp/turns.d.ts.map +1 -1
  75. package/dist/shared/acp/turns.js +11 -4
  76. package/dist/shared/acp/turns.js.map +1 -1
  77. package/dist/shared/credential-redactor.d.ts +2 -0
  78. package/dist/shared/credential-redactor.d.ts.map +1 -1
  79. package/dist/shared/credential-redactor.js +1 -0
  80. package/dist/shared/credential-redactor.js.map +1 -1
  81. package/dist/shared/executable/diagnostics.d.ts +17 -0
  82. package/dist/shared/executable/diagnostics.d.ts.map +1 -1
  83. package/dist/shared/executable/diagnostics.js +11 -0
  84. package/dist/shared/executable/diagnostics.js.map +1 -1
  85. package/dist/shared/executable/ownership.d.ts +4 -0
  86. package/dist/shared/executable/ownership.d.ts.map +1 -1
  87. package/dist/shared/executable/ownership.js +44 -5
  88. package/dist/shared/executable/ownership.js.map +1 -1
  89. package/dist/shared/executable/process-cleanup-diagnostics.d.ts +21 -0
  90. package/dist/shared/executable/process-cleanup-diagnostics.d.ts.map +1 -0
  91. package/dist/shared/executable/process-cleanup-diagnostics.js +22 -0
  92. package/dist/shared/executable/process-cleanup-diagnostics.js.map +1 -0
  93. package/dist/shared/executable/process-cleanup.d.ts +13 -0
  94. package/dist/shared/executable/process-cleanup.d.ts.map +1 -0
  95. package/dist/shared/executable/process-cleanup.js +103 -0
  96. package/dist/shared/executable/process-cleanup.js.map +1 -0
  97. package/dist/shared/executable/process-shutdown.d.ts +17 -0
  98. package/dist/shared/executable/process-shutdown.d.ts.map +1 -0
  99. package/dist/shared/executable/process-shutdown.js +115 -0
  100. package/dist/shared/executable/process-shutdown.js.map +1 -0
  101. package/dist/shared/executable/process-signals.d.ts +43 -0
  102. package/dist/shared/executable/process-signals.d.ts.map +1 -0
  103. package/dist/shared/executable/process-signals.js +74 -0
  104. package/dist/shared/executable/process-signals.js.map +1 -0
  105. package/dist/shared/executable/process-table-async.d.ts +10 -0
  106. package/dist/shared/executable/process-table-async.d.ts.map +1 -0
  107. package/dist/shared/executable/process-table-async.js +104 -0
  108. package/dist/shared/executable/process-table-async.js.map +1 -0
  109. package/dist/shared/executable/process-table-command.d.ts +4 -0
  110. package/dist/shared/executable/process-table-command.d.ts.map +1 -0
  111. package/dist/shared/executable/process-table-command.js +69 -0
  112. package/dist/shared/executable/process-table-command.js.map +1 -0
  113. package/dist/shared/executable/process-tree.d.ts +37 -2
  114. package/dist/shared/executable/process-tree.d.ts.map +1 -1
  115. package/dist/shared/executable/process-tree.js +123 -21
  116. package/dist/shared/executable/process-tree.js.map +1 -1
  117. package/dist/shared/executable/process.d.ts +7 -45
  118. package/dist/shared/executable/process.d.ts.map +1 -1
  119. package/dist/shared/executable/process.js +27 -143
  120. package/dist/shared/executable/process.js.map +1 -1
  121. package/dist/shared/executable/run.d.ts.map +1 -1
  122. package/dist/shared/executable/run.js +16 -2
  123. package/dist/shared/executable/run.js.map +1 -1
  124. package/dist/shared/install-npm.d.ts.map +1 -1
  125. package/dist/shared/install-npm.js +31 -4
  126. package/dist/shared/install-npm.js.map +1 -1
  127. package/dist/shared/install.d.ts +1 -1
  128. package/dist/shared/install.js +1 -1
  129. package/dist/shared/process-pool.d.ts +5 -1
  130. package/dist/shared/process-pool.d.ts.map +1 -1
  131. package/dist/shared/process-pool.js +11 -8
  132. package/dist/shared/process-pool.js.map +1 -1
  133. package/dist/shared/process-session.d.ts.map +1 -1
  134. package/dist/shared/process-session.js +2 -1
  135. package/dist/shared/process-session.js.map +1 -1
  136. package/dist/shared/session-credentials.d.ts +2 -2
  137. package/dist/shared/session-credentials.d.ts.map +1 -1
  138. package/dist/shared/session-credentials.js +21 -18
  139. package/dist/shared/session-credentials.js.map +1 -1
  140. package/dist/shared/session-kernel.d.ts +2 -0
  141. package/dist/shared/session-kernel.d.ts.map +1 -1
  142. package/dist/shared/session-kernel.js +1 -1
  143. package/dist/shared/session-kernel.js.map +1 -1
  144. package/dist/shared/stream-json/session.d.ts.map +1 -1
  145. package/dist/shared/stream-json/session.js +10 -3
  146. package/dist/shared/stream-json/session.js.map +1 -1
  147. package/package.json +1 -1
@@ -7,11 +7,46 @@ export interface ProcessEntry {
7
7
  }
8
8
  /** The process table at one moment, by pid. */
9
9
  export type ProcessTable = ReadonlyMap<number, ProcessEntry>;
10
+ export interface ProcessTableRead {
11
+ readonly table: ProcessTable;
12
+ /** Entries omitted because access or a start identity was unavailable. */
13
+ readonly skipped: number;
14
+ }
15
+ export type ProcessTableSource = "procfs" | "ps" | "windows";
16
+ export type ProcessTableFailureReason = "timeout" | "command_failed" | "spawn_failed" | "read_failed" | "invalid_output" | "missing_host" | "missing_root";
17
+ /** No command output, process names, arguments or environment values are retained. */
18
+ export interface ProcessTableFailure {
19
+ readonly ok: false;
20
+ readonly source: ProcessTableSource;
21
+ readonly elapsedMs: number;
22
+ readonly reason: ProcessTableFailureReason;
23
+ readonly code?: string;
24
+ readonly exitCode?: number;
25
+ readonly signal?: string;
26
+ readonly timeoutMs?: number;
27
+ }
28
+ export type ProcessTableObservation = ProcessTableFailure | (ProcessTableRead & {
29
+ readonly ok: true;
30
+ readonly source: ProcessTableSource;
31
+ readonly elapsedMs: number;
32
+ });
33
+ export declare class InvalidProcessTableError extends Error {
34
+ readonly reason: "invalid_output" | "missing_host";
35
+ constructor(reason?: "invalid_output" | "missing_host");
36
+ }
37
+ export declare function parseProcessStat(pid: number, stat: string): ProcessEntry;
38
+ export declare function parseProcessPs(output: string): ProcessTableRead;
39
+ export declare function parseProcessWindows(output: string): ProcessTableRead;
10
40
  /**
11
41
  * The process table now (synchronous: the host's `exit` hook reads it too).
12
- * Empty when it cannot be read; the caller then reaches the process group only.
42
+ * A failed observation is distinct from a valid table with no descendants.
43
+ * The host must appear in a usable table, including its group on POSIX.
13
44
  */
14
- export declare function readProcessTable(source?: "procfs" | "ps" | "windows"): ProcessTable;
45
+ export declare function observeProcessTable(source?: ProcessTableSource): ProcessTableObservation;
46
+ /** Only bounded metadata leaves a failed process-table observation. */
47
+ export declare function processTableFailure(error: unknown, source: ProcessTableSource, elapsedMs: number): ProcessTableFailure;
48
+ /** Convenience for probes that require a usable observation. Never returns an empty fallback. */
49
+ export declare function readProcessTable(source?: ProcessTableSource): ProcessTable;
15
50
  /** Every process below `pid` in `table`: its children, theirs, and so on. */
16
51
  export declare function descendantsOf(table: ProcessTable, pid: number): ProcessEntry[];
17
52
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"process-tree.d.ts","sources":["../../../src/shared/executable/process-tree.ts"],"names":[],"mappings":"AAeA,0FAA0F;AAC1F,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED,+CAA+C;AAC/C,MAAM,MAAM,YAAY,GAAG,WAAW,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAoE7D;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,GAAE,QAAQ,GAAG,IAAI,GAAG,SAAuG,GAAG,YAAY,CAMhL;AAED,6EAA6E;AAC7E,wBAAgB,aAAa,CAAC,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,GAAG,YAAY,EAAE,CAuB9E;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,EAAE,KAAK,EAAE,YAAY,GAAG,IAAI,CA4BtF"}
1
+ {"version":3,"file":"process-tree.d.ts","sources":["../../../src/shared/executable/process-tree.ts"],"names":[],"mappings":"AAeA,0FAA0F;AAC1F,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAA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BAAgB,GAAG,cAAiC,EAA4D;CAC9I;AAgBD,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAOxE;AAiCD,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,gBAAgB,CAW/D;AAWD,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,gBAAgB,CAgBpE;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,GAAE,kBAAgH,GAAG,uBAAuB,CASrL;AAED,uEAAuE;AACvE,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,GAAG,mBAAmB,CAkBtH;AAED,iGAAiG;AACjG,wBAAgB,gBAAgB,CAAC,MAAM,CAAC,EAAE,kBAAkB,GAAG,YAAY,CAI1E;AAED,6EAA6E;AAC7E,wBAAgB,aAAa,CAAC,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,GAAG,YAAY,EAAE,CAuB9E;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,CAAC,EAAE,KAAK,EAAE,YAAY,GAAG,IAAI,CA6BtF"}
@@ -1,31 +1,64 @@
1
1
  import { execFileSync } from "node:child_process";
2
2
  import { readdirSync, readFileSync } from "node:fs";
3
3
  import { asRecord } from "../json.js";
4
+ export class InvalidProcessTableError extends Error {
5
+ reason;
6
+ constructor(reason = "invalid_output") {
7
+ super(reason);
8
+ this.reason = reason;
9
+ this.name = "InvalidProcessTableError";
10
+ }
11
+ }
4
12
  /** `/proc/<pid>/stat`'s ppid, pgrp and starttime (clock ticks since boot); null once the process is gone. */
5
13
  function readStat(pid) {
6
14
  let stat = "";
7
15
  try {
8
16
  stat = readFileSync(`/proc/${String(pid)}/stat`, "utf8");
9
17
  }
10
- catch {
11
- return null;
18
+ catch (error) {
19
+ // Processes can disappear during enumeration. The caller separately
20
+ // counts permission-denied entries; other read errors fail the table.
21
+ if (error instanceof Error && "code" in error && (error.code === "ENOENT" || error.code === "ESRCH")) {
22
+ return null;
23
+ }
24
+ throw error;
12
25
  }
26
+ return parseProcessStat(pid, stat);
27
+ }
28
+ export function parseProcessStat(pid, stat) {
13
29
  // Field 2, the command name, is parenthesized and may itself hold spaces
14
30
  // and parentheses; the fields after its last ")" start at field 3 (state).
15
31
  const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
16
32
  const [ppid, pgid, start] = [Number(fields[1]), Number(fields[2]), fields[19]];
17
- return Number.isInteger(ppid) && Number.isInteger(pgid) && start !== undefined ? { pid, ppid, pgid, start } : null;
33
+ if (!Number.isInteger(ppid) || !Number.isInteger(pgid) || start === undefined || !/^\d+$/u.test(start)) {
34
+ throw new InvalidProcessTableError();
35
+ }
36
+ return { pid, ppid, pgid, start };
18
37
  }
19
38
  /** Linux: every process's stat. */
20
39
  function readProcfs() {
21
40
  const table = new Map();
41
+ let skipped = 0;
22
42
  for (const name of readdirSync("/proc")) {
23
- const entry = /^\d+$/u.test(name) ? readStat(Number(name)) : null;
24
- if (entry !== null) {
25
- table.set(entry.pid, entry);
43
+ try {
44
+ const entry = /^\d+$/u.test(name) ? readStat(Number(name)) : null;
45
+ if (entry !== null) {
46
+ table.set(entry.pid, entry);
47
+ }
48
+ }
49
+ catch (error) {
50
+ // hidepid can deny other users' stat files without hiding our tree.
51
+ // Never signal these entries, but keep the count visible to cleanup.
52
+ const code = asRecord(error)?.code;
53
+ if (code === "EACCES" || code === "EPERM") {
54
+ skipped += 1;
55
+ }
56
+ else {
57
+ throw error;
58
+ }
26
59
  }
27
60
  }
28
- return table;
61
+ return { table, skipped };
29
62
  }
30
63
  /** Other POSIX systems (macOS): one `ps` run; `lstart` is the start time to the second. */
31
64
  function readPs() {
@@ -36,50 +69,116 @@ function readPs() {
36
69
  timeout: 5000,
37
70
  maxBuffer: 16 * 1024 * 1024,
38
71
  });
72
+ return parseProcessPs(output);
73
+ }
74
+ export function parseProcessPs(output) {
39
75
  const table = new Map();
40
76
  for (const line of output.split("\n")) {
77
+ if (line.trim() === "") {
78
+ continue;
79
+ }
41
80
  const match = /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\S.*?)\s*$/u.exec(line);
42
81
  if (match !== null) {
43
82
  const pid = Number(match[1]);
44
83
  table.set(pid, { pid, ppid: Number(match[2]), pgid: Number(match[3]), start: match[4] ?? "" });
45
84
  }
85
+ else {
86
+ throw new InvalidProcessTableError();
87
+ }
46
88
  }
47
- return table;
89
+ return { table, skipped: 0 };
48
90
  }
49
91
  /** Windows has no process groups; creation time distinguishes reused PIDs. */
50
92
  function readWindows() {
51
93
  const output = execFileSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command",
52
- "Get-CimInstance Win32_Process | Select-Object ProcessId,ParentProcessId,@{Name='Started';Expression={$_.CreationDate.ToUniversalTime().ToString('o')}} | ConvertTo-Json -Compress"], {
94
+ "$ErrorActionPreference='Stop'; Get-CimInstance Win32_Process | Select-Object ProcessId,ParentProcessId,@{Name='Started';Expression={if ($null -ne $_.CreationDate) { $_.CreationDate.ToUniversalTime().ToString('o') }}} | ConvertTo-Json -Compress"], {
53
95
  encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 5000, maxBuffer: 16 * 1024 * 1024,
54
96
  });
97
+ return parseProcessWindows(output);
98
+ }
99
+ export function parseProcessWindows(output) {
55
100
  const values = JSON.parse(output);
56
101
  const table = new Map();
102
+ let skipped = 0;
57
103
  for (const value of Array.isArray(values) ? values : [values]) {
58
- if (typeof value !== "object" || value === null) {
59
- continue;
60
- }
61
104
  const entry = asRecord(value);
62
- if (entry === null) {
105
+ if (entry === null || typeof entry.ProcessId !== "number" || !Number.isInteger(entry.ProcessId) || entry.ProcessId < 0
106
+ || typeof entry.ParentProcessId !== "number" || !Number.isInteger(entry.ParentProcessId) || entry.ParentProcessId < 0) {
107
+ throw new InvalidProcessTableError();
108
+ }
109
+ // Non-admin hosts may not see protected processes' creation times.
110
+ // Omit identities we cannot revalidate, without rejecting readable peers.
111
+ if (entry.Started === undefined || entry.Started === null || entry.Started === "") {
112
+ skipped += 1;
63
113
  continue;
64
114
  }
65
- if (typeof entry.ProcessId === "number" && typeof entry.ParentProcessId === "number" && typeof entry.Started === "string" && entry.Started.length > 0) {
66
- table.set(entry.ProcessId, { pid: entry.ProcessId, ppid: entry.ParentProcessId, pgid: 0, start: entry.Started });
115
+ if (typeof entry.Started !== "string") {
116
+ throw new InvalidProcessTableError();
117
+ }
118
+ if (entry.ProcessId === 0 || entry.ProcessId === 4) {
119
+ skipped += 1;
120
+ continue;
67
121
  }
122
+ table.set(entry.ProcessId, { pid: entry.ProcessId, ppid: entry.ParentProcessId, pgid: 0, start: entry.Started });
68
123
  }
69
- return table;
124
+ return { table, skipped };
70
125
  }
71
126
  /**
72
127
  * The process table now (synchronous: the host's `exit` hook reads it too).
73
- * Empty when it cannot be read; the caller then reaches the process group only.
128
+ * A failed observation is distinct from a valid table with no descendants.
129
+ * The host must appear in a usable table, including its group on POSIX.
74
130
  */
75
- export function readProcessTable(source = process.platform === "linux" ? "procfs" : (process.platform === "win32" ? "windows" : "ps")) {
131
+ export function observeProcessTable(source = process.platform === "linux" ? "procfs" : (process.platform === "win32" ? "windows" : "ps")) {
132
+ const started = performance.now();
76
133
  try {
77
- return source === "procfs" ? readProcfs() : (source === "windows" ? readWindows() : readPs());
134
+ const result = source === "procfs" ? readProcfs() : (source === "windows" ? readWindows() : readPs());
135
+ if (!result.table.has(process.pid)) {
136
+ throw new InvalidProcessTableError("missing_host");
137
+ }
138
+ return { ok: true, source, elapsedMs: performance.now() - started, ...result };
78
139
  }
79
- catch {
80
- return new Map();
140
+ catch (error) {
141
+ return processTableFailure(error, source, performance.now() - started);
81
142
  }
82
143
  }
144
+ /** Only bounded metadata leaves a failed process-table observation. */
145
+ export function processTableFailure(error, source, elapsedMs) {
146
+ const native = asRecord(error);
147
+ // Node's execFileSync error can embed stdout and the whole command.
148
+ // Preserve its identifying fields, never its message or output.
149
+ const code = typeof native?.code === "string" && /^[A-Z][A-Z0-9_]{0,63}$/u.test(native.code) ? native.code : undefined;
150
+ const exitCode = typeof native?.status === "number" ? native.status : (typeof native?.code === "number" ? native.code : undefined);
151
+ const signal = typeof native?.signal === "string" && /^SIG[A-Z0-9]{1,16}$/u.test(native.signal) ? native.signal : undefined;
152
+ let reason = "read_failed";
153
+ if (code === "ETIMEDOUT") {
154
+ reason = "timeout";
155
+ }
156
+ else if (error instanceof InvalidProcessTableError) {
157
+ reason = error.reason;
158
+ }
159
+ else if (error instanceof SyntaxError) {
160
+ reason = "invalid_output";
161
+ }
162
+ else if (exitCode !== undefined) {
163
+ reason = "command_failed";
164
+ }
165
+ else if (code === "ENOENT" || code === "EACCES" && source !== "procfs") {
166
+ reason = "spawn_failed";
167
+ }
168
+ return {
169
+ ok: false, source, elapsedMs, reason,
170
+ ...(code === undefined ? {} : { code }), ...(exitCode === undefined ? {} : { exitCode }),
171
+ ...(signal === undefined ? {} : { signal }), ...(reason === "timeout" ? { timeoutMs: 5000 } : {}),
172
+ };
173
+ }
174
+ /** Convenience for probes that require a usable observation. Never returns an empty fallback. */
175
+ export function readProcessTable(source) {
176
+ const result = observeProcessTable(source);
177
+ if (result.ok) {
178
+ return result.table;
179
+ }
180
+ throw new Error(`Process table ${result.source}: ${result.reason}${result.code === undefined ? "" : ` (${result.code})`} after ${result.elapsedMs.toFixed(0)} ms`, { cause: result });
181
+ }
83
182
  /** Every process below `pid` in `table`: its children, theirs, and so on. */
84
183
  export function descendantsOf(table, pid) {
85
184
  const children = new Map();
@@ -114,6 +213,9 @@ export function descendantsOf(table, pid) {
114
213
  */
115
214
  export function killEntries(entries, table) {
116
215
  const hostGroup = table.get(process.pid)?.pgid;
216
+ if (hostGroup === undefined) {
217
+ return;
218
+ }
117
219
  const groups = new Set();
118
220
  const pids = new Set();
119
221
  for (const entry of entries) {
@@ -1 +1 @@
1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AAO,EAAE,CAAC;QAC5B,MAAM,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACrC,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC,KAAK,IAAI,OAAO,CAAC,GAAG,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC;YAC1F,SAAS;QACX,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACtB,IAAI,OAAO,CAAC,IAAI,GAAG,CAAC,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YACnD,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IACD,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,IAAI,CAAC;YACH,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;QAClC,CAAC;QAAC,MAAM,CAAC;YACP,+BAA+B;QACjC,CAAC;IACH,CAAC;IACD,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,CAAC;YACH,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACP,kBAAkB;QACpB,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -1,8 +1,8 @@
1
- import { type ChildProcess } from "node:child_process";
2
1
  import { Duplex, type Readable, type Writable } from "node:stream";
3
2
  import { type ProcessDiagnostics } from "./diagnostics.js";
4
3
  import { treeResourcesReader } from "./process-resources.js";
5
4
  export { trackOwnedProcess } from "./ownership.js";
5
+ export { killGraceMs, KILL_GRACE_MS, killProcessTree, signalProcessGroup, OWN_PROCESS_GROUP } from "./process-signals.js";
6
6
  export interface LineProcessOptions {
7
7
  readonly cwd?: string;
8
8
  readonly env?: NodeJS.ProcessEnv;
@@ -14,7 +14,7 @@ export interface LineProcessOptions {
14
14
  readonly inheritStderr?: boolean;
15
15
  /**
16
16
  * `kill()` ends the child's whole process tree. On Windows
17
- * ({@link killProcessTree}) that is more than the direct child, which for an
17
+ * (killProcessTree) that is more than the direct child, which for an
18
18
  * npm `.cmd` shim is only `cmd.exe`. On POSIX, where the child's process
19
19
  * group is always signalled, it adds the descendants that left the group
20
20
  * (process-tree.ts): SIGTERM still goes to the group alone, so the child
@@ -28,49 +28,14 @@ export interface LineProcessOptions {
28
28
  /** Open a private bidirectional pipe at child fd 3, separate from stdout. */
29
29
  readonly extraPipe?: boolean;
30
30
  }
31
- /**
32
- * How long a process asked to stop (SIGTERM) gets before it is killed outright
33
- * (SIGKILL): the bound that keeps `kill()` followed by `await exited` from
34
- * hanging on a process that ignores the request. Generous on purpose: claude
35
- * answers SIGTERM with a shutdown of its own that runs its SessionEnd hooks
36
- * and force-exits after max(5 s, hook budget + 5 s), 15 s while writes are
37
- * still pending (2.1.283; the hook budget defaults to 1.5 s), and cutting that
38
- * short kills the hooks with it. Observed exits take 0.6-2.3 s.
39
- */
40
- export declare const KILL_GRACE_MS = 10000;
41
- /**
42
- * The grace period in force: `OAR_KILL_GRACE_MS` when it holds a nonnegative
43
- * number of milliseconds (a host whose runtimes need longer, e.g. claude with
44
- * slow SessionEnd hooks, or that wants a faster teardown), else `KILL_GRACE_MS`.
45
- */
46
- export declare function killGraceMs(env?: NodeJS.ProcessEnv): number;
47
- /**
48
- * POSIX children are spawned detached, which makes each the leader of its own
49
- * process group, so one signal to the negative pid reaches every process it
50
- * started (shells, dev servers, MCP servers), not only the child itself.
51
- * Detaching changes neither stdio nor exit tracking: the pipes stay connected
52
- * and the child is never unref'd, so the host still observes its exit. It does
53
- * take the child out of the terminal's job control: Ctrl-C (SIGINT) and SIGHUP
54
- * reach the host only, so a host that wants them to stop its runtimes
55
- * disposes its sessions on them. Windows has no process groups, and a
56
- * detached child there gets its own console, so it stays attached.
57
- */
58
- export declare const OWN_PROCESS_GROUP: boolean;
59
- /**
60
- * Send `signal` to a child spawned with `detached: OWN_PROCESS_GROUP` and to
61
- * everything in its process group. Falls back to the child alone where there
62
- * are no groups (Windows) or the group is gone (the child left it; ESRCH).
63
- * A reaped child's pid can be reused, so call this only while the child, or a
64
- * process it started, can still be alive.
65
- */
66
- export declare function signalProcessGroup(child: ChildProcess, signal: NodeJS.Signals): void;
67
31
  /** A long-lived child whose raw streams can also be observed line-by-line. */
68
32
  export interface LineProcess {
69
33
  /** Resolves once the OS process exists; rejects when it cannot be spawned. */
70
34
  readonly spawned: Promise<void>;
71
35
  /**
72
36
  * Resolves when the process is truly gone and its state has been released.
73
- * After `kill()`, it settles by the SIGKILL escalation at the latest.
37
+ * After `kill()`, it includes bounded observations around the SIGKILL
38
+ * escalation and any asynchronous descendant cleanup after native exit.
74
39
  */
75
40
  readonly exited: Promise<number | null>;
76
41
  readonly stdin: Writable;
@@ -79,11 +44,13 @@ export interface LineProcess {
79
44
  readonly extraPipe?: Duplex;
80
45
  /** Native status and the last 8 KiB of stderr observed so far. */
81
46
  diagnostics(): ProcessDiagnostics;
47
+ /** True immediately when kill begins, before asynchronous capture or EOF. */
48
+ stopping(): boolean;
82
49
  write(text: string): void;
83
50
  /** Complete UTF-8 lines, independent of stdout byte-chunk boundaries. */
84
51
  onLine(handler: (line: string) => void): void;
85
52
  /** Fires exactly once, for exit or spawn-level error alike. */
86
- onExit(handler: (code: number | null) => void): void;
53
+ onExit(handler: (code: number | null, reason?: string) => void): void;
87
54
  /**
88
55
  * Stop the process and everything it started: close stdin, SIGTERM its
89
56
  * process group (unless shutdown is stdin), and SIGKILL the group if the child is still running once
@@ -96,11 +63,6 @@ export interface LineProcess {
96
63
  /** The child's memory with its group and descendants (process-resources.ts); null once it has exited. */
97
64
  readonly resources: ReturnType<typeof treeResourcesReader>;
98
65
  }
99
- /**
100
- * Windows taskkill /T /F ends the child and its descendants, falling back to
101
- * the child alone when the walk fails. An exited child's pid may already be reused.
102
- */
103
- export declare function killProcessTree(child: ChildProcess): void;
104
66
  /**
105
67
  * Spawn through cross-spawn. On Windows a `.cmd`/`.bat` shim is first
106
68
  * resolved to its native target or an explicitly escaped cmd.exe invocation.
@@ -1 +1 @@
1
- {"version":3,"file":"process.d.ts","sources":["../../../src/shared/executable/process.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEjE,OAAO,EAAE,MAAM,EAAE,KAAK,QAAQ,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEnE,OAAO,EAAuD,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEhH,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAI7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;IACjC;;;;OAIG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;IACjC;;;;;;;;;OASG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,uEAAuE;IACvE,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,6EAA6E;IAC7E,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;CAC9B;AACD;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,QAAS,CAAC;AACpC;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,GAAE,MAAM,CAAC,UAAwB,GAAG,MAAM,CAIxE;AACD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,iBAAiB,SAA+B,CAAC;AAC9D;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,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E,MAAM,KAAK,IAAI,GAAG,IAAI,CAAC;IAC9C,+DAA+D;IAC/D,MAAM,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAAC;IACtE;;;;;;;OAOG;IACH,IAAI,IAAI,IAAI,CAAC;IACb,yGAAyG;IACzG,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC;CAC5D;AACD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,OAAO,GAAE,kBAAuB,GAC/B,WAAW,CAgHb"}
@@ -1,84 +1,14 @@
1
- import { execFile } from "node:child_process";
2
1
  import spawn from "cross-spawn";
3
2
  import { Duplex } from "node:stream";
4
3
  import { StringDecoder } from "node:string_decoder";
5
4
  import { nativeError, spawnChecked, spawnFailure, StderrTail } from "./diagnostics.js";
6
5
  import { trackOwnedProcess } from "./ownership.js";
7
6
  import { treeResourcesReader } from "./process-resources.js";
8
- import { descendantsOf, killEntries, readProcessTable } from "./process-tree.js";
7
+ import { processShutdown } from "./process-shutdown.js";
8
+ import { OWN_PROCESS_GROUP } from "./process-signals.js";
9
9
  import { windowsSpawnTarget } from "./windows-shim.js";
10
10
  export { trackOwnedProcess } from "./ownership.js";
11
- /**
12
- * How long a process asked to stop (SIGTERM) gets before it is killed outright
13
- * (SIGKILL): the bound that keeps `kill()` followed by `await exited` from
14
- * hanging on a process that ignores the request. Generous on purpose: claude
15
- * answers SIGTERM with a shutdown of its own that runs its SessionEnd hooks
16
- * and force-exits after max(5 s, hook budget + 5 s), 15 s while writes are
17
- * still pending (2.1.283; the hook budget defaults to 1.5 s), and cutting that
18
- * short kills the hooks with it. Observed exits take 0.6-2.3 s.
19
- */
20
- export const KILL_GRACE_MS = 10_000;
21
- /**
22
- * The grace period in force: `OAR_KILL_GRACE_MS` when it holds a nonnegative
23
- * number of milliseconds (a host whose runtimes need longer, e.g. claude with
24
- * slow SessionEnd hooks, or that wants a faster teardown), else `KILL_GRACE_MS`.
25
- */
26
- export function killGraceMs(env = process.env) {
27
- const configured = env.OAR_KILL_GRACE_MS?.trim() ?? "";
28
- const value = Number(configured);
29
- return configured.length > 0 && Number.isFinite(value) && value >= 0 ? value : KILL_GRACE_MS;
30
- }
31
- /**
32
- * POSIX children are spawned detached, which makes each the leader of its own
33
- * process group, so one signal to the negative pid reaches every process it
34
- * started (shells, dev servers, MCP servers), not only the child itself.
35
- * Detaching changes neither stdio nor exit tracking: the pipes stay connected
36
- * and the child is never unref'd, so the host still observes its exit. It does
37
- * take the child out of the terminal's job control: Ctrl-C (SIGINT) and SIGHUP
38
- * reach the host only, so a host that wants them to stop its runtimes
39
- * disposes its sessions on them. Windows has no process groups, and a
40
- * detached child there gets its own console, so it stays attached.
41
- */
42
- export const OWN_PROCESS_GROUP = process.platform !== "win32";
43
- /**
44
- * Send `signal` to a child spawned with `detached: OWN_PROCESS_GROUP` and to
45
- * everything in its process group. Falls back to the child alone where there
46
- * are no groups (Windows) or the group is gone (the child left it; ESRCH).
47
- * A reaped child's pid can be reused, so call this only while the child, or a
48
- * process it started, can still be alive.
49
- */
50
- export function signalProcessGroup(child, signal) {
51
- if (OWN_PROCESS_GROUP && child.pid !== undefined) {
52
- try {
53
- process.kill(-child.pid, signal);
54
- return;
55
- }
56
- catch {
57
- // Nobody is left in the group; the child itself may still be alive in
58
- // a group of its own making, so it is signalled directly below.
59
- }
60
- }
61
- child.kill(signal);
62
- }
63
- /**
64
- * Windows taskkill /T /F ends the child and its descendants, falling back to
65
- * the child alone when the walk fails. An exited child's pid may already be reused.
66
- */
67
- export function killProcessTree(child) {
68
- if (child.exitCode !== null || child.signalCode !== null) {
69
- return;
70
- }
71
- if (child.pid === undefined) {
72
- child.kill();
73
- return;
74
- }
75
- const killer = execFile("taskkill", ["/pid", String(child.pid), "/T", "/F"], { windowsHide: true }, (error) => {
76
- if (error !== null) {
77
- child.kill();
78
- }
79
- });
80
- trackOwnedProcess(killer);
81
- }
11
+ export { killGraceMs, KILL_GRACE_MS, killProcessTree, signalProcessGroup, OWN_PROCESS_GROUP } from "./process-signals.js";
82
12
  /**
83
13
  * Spawn through cross-spawn. On Windows a `.cmd`/`.bat` shim is first
84
14
  * resolved to its native target or an explicitly escaped cmd.exe invocation.
@@ -114,15 +44,20 @@ export function spawnLineProcess(command, args, options = {}) {
114
44
  let buffer = "";
115
45
  let readingLines = false;
116
46
  let ended = false;
47
+ let finalized = false;
117
48
  let exitCode = null;
118
49
  let exitSignal = null;
119
50
  let spawnError = undefined;
120
- let escalation = null;
121
- // `killTree`: the child's descendants when kill() began, until they
122
- // have had their SIGKILL.
123
- let descendants = [];
51
+ const shutdown = processShutdown(child, options);
124
52
  const { promise: spawned, resolve: spawnOk, reject: spawnFailed } = Promise.withResolvers();
125
- const { promise: exited, resolve: exitDone } = Promise.withResolvers();
53
+ const exited = (async () => {
54
+ await shutdown.finished;
55
+ finalized = true;
56
+ for (const handler of exitHandlers) {
57
+ handler(exitCode, shutdown.reason());
58
+ }
59
+ return exitCode;
60
+ })();
126
61
  child.once("spawn", spawnOk);
127
62
  const end = (code, signal = null) => {
128
63
  if (ended) {
@@ -131,19 +66,7 @@ export function spawnLineProcess(command, args, options = {}) {
131
66
  ended = true;
132
67
  exitCode = code;
133
68
  exitSignal = signal;
134
- if (escalation !== null) {
135
- clearTimeout(escalation);
136
- }
137
- if (descendants.length > 0) {
138
- // The child stopped before its SIGKILL: what it left running of its
139
- // tree now belongs to nobody, and goes before the exit is reported.
140
- killEntries(descendants, readProcessTable());
141
- descendants = [];
142
- }
143
- for (const handler of exitHandlers) {
144
- handler(code);
145
- }
146
- exitDone(code);
69
+ shutdown.nativeExited();
147
70
  };
148
71
  child.on("exit", end);
149
72
  child.on("error", (error) => {
@@ -174,13 +97,17 @@ export function spawnLineProcess(command, args, options = {}) {
174
97
  exited,
175
98
  stdin,
176
99
  stdout,
100
+ stopping: () => shutdown.stopping(),
177
101
  ...(child.stdio[3] instanceof Duplex ? { extraPipe: child.stdio[3] } : {}),
178
- diagnostics: () => ({
179
- exitCode, signal: exitSignal, stderr: stderr.text(),
180
- ...(spawnError === undefined ? {} : { error: spawnError }),
181
- }),
182
- // Never once kill() ended stdin: a write after end is an unhandled 'error'.
183
- write: (text) => { if (!stdin.writableEnded && !stdin.destroyed) {
102
+ diagnostics: () => {
103
+ const cleanup = shutdown.diagnostics();
104
+ return { exitCode, signal: exitSignal, stderr: stderr.text(),
105
+ ...(spawnError === undefined ? {} : { error: spawnError }),
106
+ ...(cleanup === undefined ? {} : { cleanup }),
107
+ };
108
+ },
109
+ // Stop accepting writes immediately, including while capture awaits EOF.
110
+ write: (text) => { if (!shutdown.stopping() && !ended && !stdin.writableEnded && !stdin.destroyed) {
184
111
  stdin.write(text);
185
112
  } },
186
113
  onLine(handler) {
@@ -191,9 +118,9 @@ export function spawnLineProcess(command, args, options = {}) {
191
118
  }
192
119
  },
193
120
  onExit(handler) {
194
- if (ended) {
121
+ if (finalized) {
195
122
  queueMicrotask(() => {
196
- handler(exitCode);
123
+ handler(exitCode, shutdown.reason());
197
124
  });
198
125
  }
199
126
  else {
@@ -201,50 +128,7 @@ export function spawnLineProcess(command, args, options = {}) {
201
128
  }
202
129
  },
203
130
  resources: treeResourcesReader(child, () => ended),
204
- kill() {
205
- if (ended || escalation !== null) {
206
- return;
207
- }
208
- if (options.shutdown !== "stdin" && options.killTree === true && process.platform === "win32") {
209
- stdin.end();
210
- killProcessTree(child);
211
- escalation = setTimeout(() => {
212
- child.kill("SIGKILL");
213
- }, killGraceMs());
214
- return;
215
- }
216
- const { pid } = child;
217
- const tree = options.killTree === true && pid !== undefined;
218
- if (tree) {
219
- // Read while the child runs: once it is gone, what left its group is
220
- // re-parented and no longer found below it.
221
- descendants = descendantsOf(readProcessTable(), pid);
222
- }
223
- stdin.end();
224
- // Pi RPC reenters shutdown on EOF plus SIGTERM and skips its hooks.
225
- // Its caller selects EOF alone, with the same bounded escalation.
226
- if (options.shutdown !== "stdin") {
227
- signalProcessGroup(child, "SIGTERM");
228
- }
229
- // Exit clears this deadline; otherwise force-end the remaining tree.
230
- escalation = setTimeout(() => {
231
- if (!tree) {
232
- signalProcessGroup(child, "SIGKILL");
233
- return;
234
- }
235
- // Read again first: the child may have started more since.
236
- const table = readProcessTable();
237
- const now = descendantsOf(table, pid);
238
- if (process.platform === "win32") {
239
- killProcessTree(child);
240
- }
241
- else {
242
- signalProcessGroup(child, "SIGKILL");
243
- }
244
- killEntries([...descendants, ...now], table);
245
- descendants = [];
246
- }, killGraceMs());
247
- },
131
+ kill: () => { shutdown.stop(); },
248
132
  };
249
133
  }
250
134
  //# sourceMappingURL=process.js.map