doer-agent 0.8.4 → 0.8.6

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.
@@ -1,6 +1,8 @@
1
1
  import { spawn, spawnSync } from "node:child_process";
2
2
  import { existsSync } from "node:fs";
3
+ import { createRequire } from "node:module";
3
4
  import path from "node:path";
5
+ const require = createRequire(import.meta.url);
4
6
  const ANSI_RE = /\u001b\[[0-9;]*m/g;
5
7
  function shellSingleQuote(value) {
6
8
  return `'${value.replace(/'/g, `'"'"'`)}'`;
@@ -32,6 +34,20 @@ function hasDirectCodexBinary() {
32
34
  });
33
35
  return result.status === 0;
34
36
  }
37
+ function resolveBundledCodexCliBinPath() {
38
+ try {
39
+ const packageJsonPath = require.resolve("@openai/codex/package.json");
40
+ const packageJson = require(packageJsonPath);
41
+ const codexBin = packageJson.bin?.codex;
42
+ if (!codexBin) {
43
+ return null;
44
+ }
45
+ return path.resolve(path.dirname(packageJsonPath), codexBin);
46
+ }
47
+ catch {
48
+ return null;
49
+ }
50
+ }
35
51
  export function stripAnsi(value) {
36
52
  return value.replace(ANSI_RE, "");
37
53
  }
@@ -144,6 +160,10 @@ function buildWorkspaceMcpConfigArgs(args) {
144
160
  }
145
161
  export function buildLocalCodexCliCommand(args) {
146
162
  const quotedArgs = args.map(shellSingleQuote).join(" ");
163
+ const bundledCodex = resolveBundledCodexCliBinPath();
164
+ if (bundledCodex) {
165
+ return `exec ${shellSingleQuote(process.execPath)} ${shellSingleQuote(bundledCodex)} ${quotedArgs}`;
166
+ }
147
167
  const direct = `exec codex ${quotedArgs}`;
148
168
  const fallback = `exec npm exec --yes --package doer-agent -- codex ${quotedArgs}`;
149
169
  const script = [
@@ -159,19 +179,27 @@ export function spawnManagedCodexCommand(args) {
159
179
  ...args.env,
160
180
  DOER_AGENT_TOKEN: args.agentToken,
161
181
  };
162
- const child = hasDirectCodexBinary()
163
- ? spawn("codex", args.codexArgs, {
182
+ const bundledCodex = resolveBundledCodexCliBinPath();
183
+ const child = bundledCodex
184
+ ? spawn(process.execPath, [bundledCodex, ...args.codexArgs], {
164
185
  cwd: args.taskWorkspace,
165
186
  detached: process.platform !== "win32",
166
187
  env,
167
188
  stdio: ["ignore", "pipe", "pipe"],
168
189
  })
169
- : spawn("npm", ["exec", "--yes", "--package", "doer-agent", "--", "codex", ...args.codexArgs], {
170
- cwd: args.taskWorkspace,
171
- detached: process.platform !== "win32",
172
- env,
173
- stdio: ["ignore", "pipe", "pipe"],
174
- });
190
+ : hasDirectCodexBinary()
191
+ ? spawn("codex", args.codexArgs, {
192
+ cwd: args.taskWorkspace,
193
+ detached: process.platform !== "win32",
194
+ env,
195
+ stdio: ["ignore", "pipe", "pipe"],
196
+ })
197
+ : spawn("npm", ["exec", "--yes", "--package", "doer-agent", "--", "codex", ...args.codexArgs], {
198
+ cwd: args.taskWorkspace,
199
+ detached: process.platform !== "win32",
200
+ env,
201
+ stdio: ["ignore", "pipe", "pipe"],
202
+ });
175
203
  child.stdout?.setEncoding("utf8");
176
204
  child.stderr?.setEncoding("utf8");
177
205
  return child;
@@ -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/dist/codex-cli.js CHANGED
@@ -1,7 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import { existsSync, realpathSync } from "node:fs";
3
- import { delimiter, join } from "node:path";
3
+ import { createRequire } from "node:module";
4
+ import { delimiter, dirname, join } from "node:path";
4
5
  import { spawn } from "node:child_process";
6
+ const require = createRequire(import.meta.url);
5
7
  function isWindows() {
6
8
  return process.platform === "win32";
7
9
  }
@@ -23,6 +25,10 @@ function resolveSelf() {
23
25
  }
24
26
  }
25
27
  function findCodexBinary() {
28
+ const localCodex = resolveBundledCodexBinary();
29
+ if (localCodex) {
30
+ return localCodex;
31
+ }
26
32
  const pathValue = process.env.PATH ?? "";
27
33
  const dirs = pathValue.split(delimiter).filter(Boolean);
28
34
  const self = resolveSelf();
@@ -43,6 +49,20 @@ function findCodexBinary() {
43
49
  }
44
50
  return null;
45
51
  }
52
+ function resolveBundledCodexBinary() {
53
+ try {
54
+ const packageJsonPath = require.resolve("@openai/codex/package.json");
55
+ const packageJson = require(packageJsonPath);
56
+ const codexBin = packageJson.bin?.codex;
57
+ if (!codexBin) {
58
+ return null;
59
+ }
60
+ return join(dirname(packageJsonPath), codexBin);
61
+ }
62
+ catch {
63
+ return null;
64
+ }
65
+ }
46
66
  const binary = findCodexBinary();
47
67
  if (!binary) {
48
68
  console.error("Unable to find a real 'codex' binary in PATH.");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doer-agent",
3
- "version": "0.8.4",
3
+ "version": "0.8.6",
4
4
  "description": "Reverse-polling agent runtime for doer",
5
5
  "type": "module",
6
6
  "main": "dist/agent.js",
@@ -25,15 +25,16 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@modelcontextprotocol/sdk": "^1.29.0",
28
- "@openai/codex": "^0.133.0",
29
- "@openai/codex-sdk": "^0.133.0",
28
+ "@openai/codex": "^0.141.0",
29
+ "@openai/codex-sdk": "^0.141.0",
30
30
  "diff": "^9.0.0",
31
31
  "nats": "^2.29.3",
32
32
  "tar": "^7.5.15"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/node": "^20.19.40",
36
- "tsx": "^4.21.0",
36
+ "esbuild": "^0.28.1",
37
+ "tsx": "^4.22.4",
37
38
  "typescript": "^5"
38
39
  }
39
40
  }