codex-supervisor-mcp 0.10.3 → 0.11.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.
@@ -0,0 +1,100 @@
1
+ import { execFileSync, spawn } from "node:child_process";
2
+ import { defaultBinDirs, findBinaryPath, resolveCommand, shimMessage } from "./bin-resolver.js";
3
+ import { applyCodexEvent } from "./event-parser.js";
4
+
5
+ const CODEX_NPM_ENTRY = { pkg: "@openai/codex", bin: "bin/codex.js" };
6
+
7
+ export class CodexRunnerAdapter {
8
+ constructor({ env = process.env } = {}) {
9
+ this.env = env;
10
+ this.name = "codex";
11
+ }
12
+
13
+ resolveTarget() {
14
+ const configured = this.env.CODEX_BIN?.trim();
15
+ const located = configured ? configured : (findBinaryPath("codex", { extraDirs: defaultBinDirs() }) ?? "codex");
16
+ const target = resolveCommand(located, { npmEntry: CODEX_NPM_ENTRY });
17
+ if (target.shim) return { error: shimMessage("codex", target.cmd, "CODEX_BIN") };
18
+ return { cmd: target.cmd, prefixArgs: target.prefixArgs };
19
+ }
20
+
21
+ capabilityPreflight(target = this.resolveTarget()) {
22
+ if (target.error) return { ok: false, error: target.error };
23
+ try {
24
+ const help = execFileSync(target.cmd, [...target.prefixArgs, "exec", "--help"], {
25
+ encoding: "utf8",
26
+ timeout: 15000,
27
+ windowsHide: true,
28
+ stdio: ["ignore", "pipe", "pipe"]
29
+ });
30
+ if (!help.includes("--dangerously-bypass-hook-trust") || !help.includes("--output-schema")) {
31
+ return { ok: false, error: "Codex CLI lacks required per-run hooks/output-schema; install Codex 0.162 or newer before dispatch" };
32
+ }
33
+ return { ok: true, target };
34
+ } catch (error) {
35
+ return { ok: false, error: error.code === "ENOENT"
36
+ ? `failed to spawn ${target.cmd} for Codex capability preflight: ${error.message}`
37
+ : `Codex capability preflight failed: ${error.message}` };
38
+ }
39
+ }
40
+
41
+ startArgs({ cwd, sandbox, model, reasoningEffort, skipGitRepoCheck }) {
42
+ const args = ["exec", "--json", "--cd", cwd, "--sandbox", sandbox];
43
+ if (skipGitRepoCheck) args.push("--skip-git-repo-check");
44
+ if (model) args.push("--model", model);
45
+ args.push("-c", `model_reasoning_effort="${reasoningEffort}"`, "-");
46
+ return args;
47
+ }
48
+
49
+ resumeArgs(task) {
50
+ const args = ["exec", "resume", "--json", "-c", `sandbox_mode="${task.sandbox}"`];
51
+ if (task.model) args.push("--model", task.model);
52
+ args.push("-c", `model_reasoning_effort="${task.reasoning_effort ?? "high"}"`);
53
+ if (task.skip_git_repo_check) args.push("--skip-git-repo-check");
54
+ args.push(task.thread_id, "-");
55
+ return args;
56
+ }
57
+
58
+ forkSideArgs({ forkThreadId, workerThreadId, model }) {
59
+ const args = forkThreadId
60
+ ? ["exec", "resume", "--json", "--skip-git-repo-check", "-c", 'sandbox_mode="read-only"']
61
+ : ["exec", "fork", "--json", "--skip-git-repo-check", "-c", 'sandbox_mode="read-only"'];
62
+ if (model) args.push("-m", model);
63
+ args.push(forkThreadId ?? workerThreadId, "-");
64
+ return args;
65
+ }
66
+
67
+ start(target, args, options) {
68
+ return spawn(target.cmd, [...target.prefixArgs, ...args], options);
69
+ }
70
+
71
+ parseEvent(task, event) {
72
+ return applyCodexEvent(task, event);
73
+ }
74
+
75
+ cancelChild(child) {
76
+ if (process.platform === "win32" && child.pid) {
77
+ try {
78
+ execFileSync("taskkill", ["/PID", String(child.pid), "/T", "/F"], { stdio: "ignore", timeout: 5000, windowsHide: true });
79
+ return;
80
+ } catch {
81
+ // fall through to the process handle
82
+ }
83
+ }
84
+ child.kill("SIGTERM");
85
+ }
86
+
87
+ cancelPid(pid) {
88
+ if (process.platform === "win32") {
89
+ execFileSync("taskkill", ["/PID", String(pid), "/T", "/F"], { stdio: "ignore", timeout: 5000, windowsHide: true });
90
+ return;
91
+ }
92
+ process.kill(pid, "SIGTERM");
93
+ }
94
+
95
+ ownsPid(pid, describeProcess) {
96
+ return describeProcess(pid).includes("codex");
97
+ }
98
+ }
99
+
100
+ export const codexRunner = new CodexRunnerAdapter();
package/src/side-chat.js CHANGED
@@ -24,8 +24,8 @@
24
24
  // session ended ................... fork deleted, history cleared
25
25
  import { execFileSync, spawn } from "node:child_process";
26
26
  import { randomUUID } from "node:crypto";
27
- import { defaultBinDirs, findBinaryPath, resolveCommand, shimMessage } from "./bin-resolver.js";
28
27
  import { codexConfigText, disableMcpServerArgs } from "./mcp-config.js";
28
+ import { codexRunner } from "./runner-adapter.js";
29
29
  import {
30
30
  closeSideSession,
31
31
  getSideSession,
@@ -37,7 +37,6 @@ import {
37
37
  updateSideTurn
38
38
  } from "./task-store.js";
39
39
 
40
- const CODEX_NPM_ENTRY = { pkg: "@openai/codex", bin: "bin/codex.js" };
41
40
  const TURN_TIMEOUT_MS = 10 * 60 * 1000;
42
41
 
43
42
  // What a side question is, said to the fork every time. Without it the fork
@@ -104,29 +103,6 @@ async function sweepOnce() {
104
103
  await interruptRunningSideTurns();
105
104
  }
106
105
 
107
- function codexTarget() {
108
- const configured = process.env.CODEX_BIN?.trim();
109
- const located = configured ? configured : (findBinaryPath("codex", { extraDirs: defaultBinDirs() }) ?? "codex");
110
- const target = resolveCommand(located, { npmEntry: CODEX_NPM_ENTRY });
111
- if (target.shim) return { error: shimMessage("codex", target.cmd, "CODEX_BIN") };
112
- return { cmd: target.cmd, prefixArgs: target.prefixArgs };
113
- }
114
-
115
- // Stop a side turn and everything under it. On Windows the direct child is
116
- // the JS wrapper around the native Codex binary; killing the wrapper alone
117
- // leaves the real CLI running, so the whole tree goes through taskkill.
118
- function killTree(child) {
119
- if (process.platform === "win32" && child.pid) {
120
- try {
121
- execFileSync("taskkill", ["/PID", String(child.pid), "/T", "/F"], { stdio: "ignore", timeout: 5000, windowsHide: true });
122
- return;
123
- } catch {
124
- // fall back to the handle below
125
- }
126
- }
127
- child.kill("SIGTERM");
128
- }
129
-
130
106
  function clip(text, max = OUTPUT_CLIP) {
131
107
  if (typeof text !== "string") return text ?? null;
132
108
  return text.length <= max ? text : `${text.slice(0, max)}\n…(${text.length - max} more characters)`;
@@ -338,20 +314,16 @@ export async function runSideTurn({ task, text, res = null, fresh = false }) {
338
314
  await sweepOnce();
339
315
  if (!task.thread_id) throw Object.assign(new Error("this worker recorded no thread_id, so there is no Codex thread to fork"), { code: "no_thread" });
340
316
  if (running.has(task.id)) throw Object.assign(new Error("a side turn is already running for this worker"), { code: "busy" });
341
- const target = codexTarget();
317
+ const target = codexRunner.resolveTarget();
342
318
  if (target.error) throw Object.assign(new Error(target.error), { code: "codex_missing" });
343
319
 
344
320
  const { session, refreshed } = await prepareSideSession(task, { fresh });
345
321
 
346
- const args = session.fork_thread_id
347
- ? ["exec", "resume", "--json", "--skip-git-repo-check", "-c", 'sandbox_mode="read-only"']
348
- : ["exec", "fork", "--json", "--skip-git-repo-check", "-c", 'sandbox_mode="read-only"'];
349
- args.push(...sideTurnConfigArgs(codexConfigText()));
350
- if (task.model) args.push("-m", task.model);
351
- args.push(session.fork_thread_id ?? task.thread_id, "-");
322
+ const args = codexRunner.forkSideArgs({ forkThreadId: session.fork_thread_id, workerThreadId: task.thread_id, model: task.model });
323
+ args.splice(-2, 0, ...sideTurnConfigArgs(codexConfigText()));
352
324
 
353
325
  const cwd = task.worktree_path || task.project_root || process.cwd();
354
- const child = spawn(target.cmd, [...target.prefixArgs, ...args], {
326
+ const child = codexRunner.start(target, args, {
355
327
  cwd,
356
328
  stdio: ["pipe", "pipe", "pipe"],
357
329
  env: { ...process.env, NO_COLOR: "1" },
@@ -390,11 +362,11 @@ export async function runSideTurn({ task, text, res = null, fresh = false }) {
390
362
  let stoppedBy = null;
391
363
  const timer = setTimeout(() => {
392
364
  stoppedBy = "timeout";
393
- killTree(child);
365
+ codexRunner.cancelChild(child);
394
366
  }, TURN_TIMEOUT_MS);
395
367
  live.stop = (reason) => {
396
368
  stoppedBy = reason;
397
- killTree(child);
369
+ codexRunner.cancelChild(child);
398
370
  };
399
371
 
400
372
  child.stdout.setEncoding("utf8");
@@ -546,12 +518,12 @@ export async function endSideSession(taskId) {
546
518
  if (!row) return { ended: false };
547
519
  let deleted = false;
548
520
  if (row.fork_thread_id) {
549
- const target = codexTarget();
521
+ const target = codexRunner.resolveTarget();
550
522
  if (!target.error) {
551
523
  deleted = await new Promise((resolve) => {
552
524
  // --force: without a terminal codex asks for confirmation and exits.
553
- const child = spawn(target.cmd, [...target.prefixArgs, "delete", "--force", row.fork_thread_id], { stdio: "ignore", windowsHide: true, env: { ...process.env, NO_COLOR: "1" } });
554
- const t = setTimeout(() => { killTree(child); resolve(false); }, 15000);
525
+ const child = codexRunner.start(target, ["delete", "--force", row.fork_thread_id], { stdio: "ignore", windowsHide: true, env: { ...process.env, NO_COLOR: "1" } });
526
+ const t = setTimeout(() => { codexRunner.cancelChild(child); resolve(false); }, 15000);
555
527
  child.on("exit", (code) => { clearTimeout(t); resolve(code === 0); });
556
528
  child.on("error", () => { clearTimeout(t); resolve(false); });
557
529
  });