doer-agent 0.8.4 → 0.8.5

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.
@@ -18,6 +18,7 @@ export class CodexAppServerClient {
18
18
  stdoutLines = null;
19
19
  nextRequestId = 1;
20
20
  startPromise = null;
21
+ stopPromise = null;
21
22
  pending = new Map();
22
23
  constructor(options) {
23
24
  this.options = options;
@@ -37,10 +38,19 @@ export class CodexAppServerClient {
37
38
  }
38
39
  async stop() {
39
40
  const child = this.child;
40
- if (!child || child.killed) {
41
+ if (!child) {
41
42
  return;
42
43
  }
43
- child.kill("SIGTERM");
44
+ if (this.stopPromise) {
45
+ return await this.stopPromise;
46
+ }
47
+ this.stopPromise = this.stopChild(child);
48
+ try {
49
+ await this.stopPromise;
50
+ }
51
+ finally {
52
+ this.stopPromise = null;
53
+ }
44
54
  }
45
55
  async start() {
46
56
  if (this.child && !this.child.killed) {
@@ -60,9 +70,12 @@ export class CodexAppServerClient {
60
70
  async startInner() {
61
71
  this.child = spawn(process.execPath, [resolveCodexCliBinPath(), ...this.options.args], {
62
72
  cwd: this.options.cwd,
73
+ detached: process.platform !== "win32",
63
74
  env: this.options.env,
64
75
  stdio: ["pipe", "pipe", "pipe"],
65
76
  });
77
+ const childPid = this.child.pid;
78
+ const removeExitHooks = this.registerProcessExitHooks(childPid);
66
79
  this.child.stdout.setEncoding("utf8");
67
80
  this.child.stderr.setEncoding("utf8");
68
81
  this.stdoutLines = createInterface({ input: this.child.stdout });
@@ -75,6 +88,8 @@ export class CodexAppServerClient {
75
88
  });
76
89
  this.child.once("exit", (code, signal) => {
77
90
  this.options.onLog?.(`[codex-app-server] exited code=${code ?? "null"} signal=${signal ?? "null"}`);
91
+ removeExitHooks();
92
+ this.signalProcessGroup(childPid, "SIGTERM");
78
93
  this.rejectPending(new Error("Codex app-server exited"));
79
94
  this.stdoutLines?.close();
80
95
  this.stdoutLines = null;
@@ -157,4 +172,95 @@ export class CodexAppServerClient {
157
172
  pending.reject(error);
158
173
  }
159
174
  }
175
+ async stopChild(child) {
176
+ const pid = child.pid;
177
+ if (!pid) {
178
+ child.kill("SIGTERM");
179
+ return;
180
+ }
181
+ this.signalProcessGroup(pid, "SIGTERM") || child.kill("SIGTERM");
182
+ const exited = await this.waitForExit(child, 5_000);
183
+ if (!exited) {
184
+ this.options.onLog?.("[codex-app-server] forcing process group shutdown after timeout");
185
+ this.signalProcessGroup(pid, "SIGKILL") || child.kill("SIGKILL");
186
+ await this.waitForExit(child, 1_000);
187
+ }
188
+ else {
189
+ this.signalProcessGroup(pid, "SIGTERM");
190
+ }
191
+ }
192
+ registerProcessExitHooks(pid) {
193
+ if (!pid || process.platform === "win32") {
194
+ return () => { };
195
+ }
196
+ let removed = false;
197
+ const cleanup = () => {
198
+ this.signalProcessGroup(pid, "SIGTERM");
199
+ };
200
+ const signalHandlers = new Map();
201
+ const remove = () => {
202
+ if (removed) {
203
+ return;
204
+ }
205
+ removed = true;
206
+ process.off("exit", cleanup);
207
+ for (const [signal, handler] of signalHandlers) {
208
+ process.off(signal, handler);
209
+ }
210
+ };
211
+ process.once("exit", cleanup);
212
+ for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
213
+ const handler = () => {
214
+ cleanup();
215
+ remove();
216
+ try {
217
+ process.kill(process.pid, signal);
218
+ }
219
+ catch {
220
+ process.exitCode = 1;
221
+ }
222
+ };
223
+ signalHandlers.set(signal, handler);
224
+ process.once(signal, handler);
225
+ }
226
+ return remove;
227
+ }
228
+ signalProcessGroup(pid, signal) {
229
+ if (!pid || process.platform === "win32") {
230
+ return false;
231
+ }
232
+ try {
233
+ process.kill(-pid, signal);
234
+ return true;
235
+ }
236
+ catch (error) {
237
+ const code = typeof error === "object" && error !== null && "code" in error
238
+ ? String(error.code)
239
+ : "";
240
+ if (code && code !== "ESRCH") {
241
+ this.options.onLog?.(`[codex-app-server] failed to signal process group pid=${pid} signal=${signal} code=${code}`);
242
+ }
243
+ return false;
244
+ }
245
+ }
246
+ async waitForExit(child, timeoutMs) {
247
+ if (child.exitCode !== null || child.signalCode !== null) {
248
+ return true;
249
+ }
250
+ return await new Promise((resolve) => {
251
+ const timer = setTimeout(() => {
252
+ cleanup();
253
+ resolve(false);
254
+ }, timeoutMs);
255
+ const onExit = () => {
256
+ cleanup();
257
+ resolve(true);
258
+ };
259
+ const cleanup = () => {
260
+ clearTimeout(timer);
261
+ child.off("exit", onExit);
262
+ };
263
+ child.once("exit", onExit);
264
+ });
265
+ }
160
266
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doer-agent",
3
- "version": "0.8.4",
3
+ "version": "0.8.5",
4
4
  "description": "Reverse-polling agent runtime for doer",
5
5
  "type": "module",
6
6
  "main": "dist/agent.js",