@mclawnet/agent 0.6.35 → 0.6.36

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 (69) hide show
  1. package/cli.js +6 -6
  2. package/dist/__tests__/collect-manifest.test.d.ts +2 -0
  3. package/dist/__tests__/collect-manifest.test.d.ts.map +1 -0
  4. package/dist/__tests__/hub-connection-on-activity.test.d.ts +2 -0
  5. package/dist/__tests__/hub-connection-on-activity.test.d.ts.map +1 -0
  6. package/dist/__tests__/hub-connection-wake-watch.test.d.ts +2 -0
  7. package/dist/__tests__/hub-connection-wake-watch.test.d.ts.map +1 -0
  8. package/dist/__tests__/ideas-rest-client.test.d.ts +2 -0
  9. package/dist/__tests__/ideas-rest-client.test.d.ts.map +1 -0
  10. package/dist/__tests__/legacy-claude-execute-compat.test.d.ts +2 -0
  11. package/dist/__tests__/legacy-claude-execute-compat.test.d.ts.map +1 -0
  12. package/dist/__tests__/no-adapter-cycle.test.d.ts +2 -0
  13. package/dist/__tests__/no-adapter-cycle.test.d.ts.map +1 -0
  14. package/dist/__tests__/session-manager-merge.test.d.ts +2 -0
  15. package/dist/__tests__/session-manager-merge.test.d.ts.map +1 -0
  16. package/dist/__tests__/session-manager-sticky.test.d.ts +2 -0
  17. package/dist/__tests__/session-manager-sticky.test.d.ts.map +1 -0
  18. package/dist/__tests__/session-protocol-dispatch.test.d.ts +2 -0
  19. package/dist/__tests__/session-protocol-dispatch.test.d.ts.map +1 -0
  20. package/dist/__tests__/worktree-bridge.test.d.ts +2 -0
  21. package/dist/__tests__/worktree-bridge.test.d.ts.map +1 -0
  22. package/dist/backend-adapter.d.ts +6 -232
  23. package/dist/backend-adapter.d.ts.map +1 -1
  24. package/dist/backend-factory-AFF6I7YF.js +11 -0
  25. package/dist/backend-factory.d.ts +23 -1
  26. package/dist/backend-factory.d.ts.map +1 -1
  27. package/dist/bootstrap-deps.d.ts +22 -0
  28. package/dist/bootstrap-deps.d.ts.map +1 -1
  29. package/dist/bootstrap-deps.js +23 -4
  30. package/dist/bootstrap-deps.js.map +1 -1
  31. package/dist/{chunk-PJ5M6Q36.js → chunk-376QZ7JB.js} +2 -2
  32. package/dist/chunk-376QZ7JB.js.map +1 -0
  33. package/dist/{chunk-B733MQCA.js → chunk-GOCWMRBB.js} +1772 -284
  34. package/dist/chunk-GOCWMRBB.js.map +1 -0
  35. package/dist/{chunk-M2CDVPQF.js → chunk-JH6RGJBQ.js} +2 -2
  36. package/dist/{chunk-FYM7CXUI.js → chunk-VAEFJLPL.js} +25 -3
  37. package/dist/chunk-VAEFJLPL.js.map +1 -0
  38. package/dist/{dist-EGT2NQEW.js → dist-NWVHAP5R.js} +155 -13
  39. package/dist/dist-NWVHAP5R.js.map +1 -0
  40. package/dist/errors.d.ts +20 -0
  41. package/dist/errors.d.ts.map +1 -1
  42. package/dist/hub-connection.d.ts +25 -1
  43. package/dist/hub-connection.d.ts.map +1 -1
  44. package/dist/ideas-rest-client.d.ts +25 -0
  45. package/dist/ideas-rest-client.d.ts.map +1 -0
  46. package/dist/index.js +3 -3
  47. package/dist/{linux-IHA4O633.js → linux-MBU6ERXL.js} +3 -3
  48. package/dist/{macos-G4VK2253.js → macos-I2DUWFUH.js} +3 -3
  49. package/dist/projects-handler.d.ts +146 -1
  50. package/dist/projects-handler.d.ts.map +1 -1
  51. package/dist/service/index.js +5 -5
  52. package/dist/session-manager.d.ts +58 -0
  53. package/dist/session-manager.d.ts.map +1 -1
  54. package/dist/start.d.ts.map +1 -1
  55. package/dist/start.js +3 -2
  56. package/dist/{windows-P6U3JLUZ.js → windows-PEJ3KOLC.js} +3 -3
  57. package/dist/worktree-bridge.d.ts +51 -0
  58. package/dist/worktree-bridge.d.ts.map +1 -0
  59. package/package.json +9 -8
  60. package/dist/backend-factory-VRPU3534.js +0 -9
  61. package/dist/chunk-B733MQCA.js.map +0 -1
  62. package/dist/chunk-FYM7CXUI.js.map +0 -1
  63. package/dist/chunk-PJ5M6Q36.js.map +0 -1
  64. package/dist/dist-EGT2NQEW.js.map +0 -1
  65. /package/dist/{backend-factory-VRPU3534.js.map → backend-factory-AFF6I7YF.js.map} +0 -0
  66. /package/dist/{chunk-M2CDVPQF.js.map → chunk-JH6RGJBQ.js.map} +0 -0
  67. /package/dist/{linux-IHA4O633.js.map → linux-MBU6ERXL.js.map} +0 -0
  68. /package/dist/{macos-G4VK2253.js.map → macos-I2DUWFUH.js.map} +0 -0
  69. /package/dist/{windows-P6U3JLUZ.js.map → windows-PEJ3KOLC.js.map} +0 -0
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  loadConfig,
3
3
  saveConfig
4
- } from "./chunk-PJ5M6Q36.js";
4
+ } from "./chunk-376QZ7JB.js";
5
5
 
6
6
  // src/service/config.ts
7
7
  import { existsSync, readFileSync } from "fs";
@@ -85,4 +85,4 @@ export {
85
85
  validateServiceConfig,
86
86
  mergeServiceConfig
87
87
  };
88
- //# sourceMappingURL=chunk-M2CDVPQF.js.map
88
+ //# sourceMappingURL=chunk-JH6RGJBQ.js.map
@@ -21,7 +21,7 @@ async function createBackendAdapter(kind, ctx = {}) {
21
21
  }
22
22
  } else if (resolved === "codex") {
23
23
  try {
24
- const mod = await import("./dist-EGT2NQEW.js");
24
+ const mod = await import("./dist-NWVHAP5R.js");
25
25
  adapter = new mod.CodexAdapter();
26
26
  } catch (err) {
27
27
  throw rethrowWithRole(
@@ -41,9 +41,31 @@ function rethrowWithRole(err, roleName, prefix) {
41
41
  const tag = roleName ? ` [role=${roleName}]` : "";
42
42
  return new Error(`${prefix}${tag}: ${base}`);
43
43
  }
44
+ var ALL_KINDS_MAP = { claude: true, codex: true };
45
+ var ALL_KINDS = Object.keys(ALL_KINDS_MAP);
46
+ async function collectAgentManifest() {
47
+ const entries = await Promise.all(
48
+ ALL_KINDS.map(async (kind) => {
49
+ try {
50
+ const adapter = await createBackendAdapter(kind);
51
+ return await adapter.getManifest();
52
+ } catch (err) {
53
+ return {
54
+ kind,
55
+ installed: false,
56
+ unavailableReason: err instanceof Error ? err.message : String(err),
57
+ models: [],
58
+ modes: []
59
+ };
60
+ }
61
+ })
62
+ );
63
+ return { manifestVersion: 1, backends: entries };
64
+ }
44
65
 
45
66
  export {
46
67
  __resetBackendFactoryCache,
47
- createBackendAdapter
68
+ createBackendAdapter,
69
+ collectAgentManifest
48
70
  };
49
- //# sourceMappingURL=chunk-FYM7CXUI.js.map
71
+ //# sourceMappingURL=chunk-VAEFJLPL.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/backend-factory.ts"],"sourcesContent":["import type { BackendAdapter } from \"./backend-adapter.js\";\nimport type { AgentManifestPayload, BackendKind } from \"@mclawnet/shared\";\n\n// Re-export for back-compat with existing callers that import BackendKind from\n// this module rather than @mclawnet/shared.\nexport type { BackendKind };\n\nconst cache = new Map<BackendKind, BackendAdapter>();\n\nexport function __resetBackendFactoryCache(): void {\n cache.clear();\n}\n\nexport interface BackendFactoryContext {\n /** Role instance / template name — surfaces in error messages so users know\n * which role is misconfigured when its backend package fails to load. */\n roleName?: string;\n}\n\n/**\n * Resolve a BackendAdapter for the given backend kind.\n *\n * - \"claude\" / undefined → @mclawnet/claude-adapter (back-compat default).\n * - \"codex\" → @mclawnet/codex-adapter via dynamic import so the codex bundle\n * isn't pulled into claude-only deployments.\n *\n * Why dynamic import (and why codex-adapter is `optionalDependencies` in\n * agent's package.json, not `dependencies`):\n * - agent owns the runtime relationship \"I load adapters on demand\".\n * - Adapters only depend on @mclawnet/backend-types (the interface package),\n * so there is no build-time cycle.\n * - `optionalDependencies` keeps the \"user installs just @mclawnet/agent\n * and codex backend works out of the box\" UX: npm/pnpm will try to\n * install codex-adapter; if that fails (network, platform issue, user\n * opted out via --no-optional), the dynamic import below throws and\n * the agent surfaces a clear error instead of crashing at startup.\n * - Promoting codex-adapter to `dependencies` would force install, which\n * is fine for monorepo but unfriendly to claude-only deployments.\n *\n * Results are cached per kind; subsequent calls return the same instance.\n */\nexport async function createBackendAdapter(\n kind: BackendKind | undefined,\n ctx: BackendFactoryContext = {},\n): Promise<BackendAdapter> {\n const resolved: BackendKind = kind ?? \"claude\";\n const cached = cache.get(resolved);\n if (cached) return cached;\n\n let adapter: BackendAdapter;\n if (resolved === \"claude\") {\n try {\n const mod = (await import(\"@mclawnet/claude-adapter\" as string)) as {\n ClaudeCodeAdapter: new () => BackendAdapter;\n };\n adapter = new mod.ClaudeCodeAdapter();\n } catch (err) {\n throw rethrowWithRole(\n err,\n ctx.roleName,\n \"failed to load @mclawnet/claude-adapter\",\n );\n }\n } else if (resolved === \"codex\") {\n try {\n const mod = (await import(\"@mclawnet/codex-adapter\" as string)) as {\n CodexAdapter: new () => BackendAdapter;\n };\n adapter = new mod.CodexAdapter();\n } catch (err) {\n throw rethrowWithRole(\n err,\n ctx.roleName,\n \"failed to load @mclawnet/codex-adapter (is codex CLI installed?)\",\n );\n }\n } else {\n throw new Error(`unknown backend kind: ${resolved}`);\n }\n\n cache.set(resolved, adapter);\n return adapter;\n}\n\nfunction rethrowWithRole(err: unknown, roleName: string | undefined, prefix: string): Error {\n const base = err instanceof Error ? err.message : String(err);\n const tag = roleName ? ` [role=${roleName}]` : \"\";\n return new Error(`${prefix}${tag}: ${base}`);\n}\n\n/**\n * All backend kinds the agent should advertise in its manifest. Mirrors the\n * `BackendKind` union exposed by @mclawnet/shared. The `satisfies` clause\n * forces a tsc error if `BackendKind` grows without a matching map entry.\n */\nconst ALL_KINDS_MAP = { claude: true, codex: true } as const satisfies Record<BackendKind, true>;\nconst ALL_KINDS = Object.keys(ALL_KINDS_MAP) as BackendKind[];\n\n/**\n * Probe every known backend adapter and aggregate their `getManifest()` output\n * into a single payload the agent can ship to the hub. Failures (binary not on\n * PATH, adapter package broken, etc.) become `installed: false` entries rather\n * than rejecting the whole collection — the hub still wants to know \"we tried\n * but it isn't available\" for each kind.\n */\nexport async function collectAgentManifest(): Promise<AgentManifestPayload> {\n const entries = await Promise.all(\n ALL_KINDS.map(async (kind) => {\n try {\n const adapter = await createBackendAdapter(kind);\n return await adapter.getManifest();\n } catch (err) {\n return {\n kind,\n installed: false,\n unavailableReason: err instanceof Error ? err.message : String(err),\n models: [],\n modes: [],\n };\n }\n }),\n );\n return { manifestVersion: 1, backends: entries };\n}\n"],"mappings":";AAOA,IAAM,QAAQ,oBAAI,IAAiC;AAE5C,SAAS,6BAAmC;AACjD,QAAM,MAAM;AACd;AA8BA,eAAsB,qBACpB,MACA,MAA6B,CAAC,GACL;AACzB,QAAM,WAAwB,QAAQ;AACtC,QAAM,SAAS,MAAM,IAAI,QAAQ;AACjC,MAAI,OAAQ,QAAO;AAEnB,MAAI;AACJ,MAAI,aAAa,UAAU;AACzB,QAAI;AACF,YAAM,MAAO,MAAM,OAAO,0BAAoC;AAG9D,gBAAU,IAAI,IAAI,kBAAkB;AAAA,IACtC,SAAS,KAAK;AACZ,YAAM;AAAA,QACJ;AAAA,QACA,IAAI;AAAA,QACJ;AAAA,MACF;AAAA,IACF;AAAA,EACF,WAAW,aAAa,SAAS;AAC/B,QAAI;AACF,YAAM,MAAO,MAAM,OAAO,oBAAmC;AAG7D,gBAAU,IAAI,IAAI,aAAa;AAAA,IACjC,SAAS,KAAK;AACZ,YAAM;AAAA,QACJ;AAAA,QACA,IAAI;AAAA,QACJ;AAAA,MACF;AAAA,IACF;AAAA,EACF,OAAO;AACL,UAAM,IAAI,MAAM,yBAAyB,QAAQ,EAAE;AAAA,EACrD;AAEA,QAAM,IAAI,UAAU,OAAO;AAC3B,SAAO;AACT;AAEA,SAAS,gBAAgB,KAAc,UAA8B,QAAuB;AAC1F,QAAM,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAC5D,QAAM,MAAM,WAAW,UAAU,QAAQ,MAAM;AAC/C,SAAO,IAAI,MAAM,GAAG,MAAM,GAAG,GAAG,KAAK,IAAI,EAAE;AAC7C;AAOA,IAAM,gBAAgB,EAAE,QAAQ,MAAM,OAAO,KAAK;AAClD,IAAM,YAAY,OAAO,KAAK,aAAa;AAS3C,eAAsB,uBAAsD;AAC1E,QAAM,UAAU,MAAM,QAAQ;AAAA,IAC5B,UAAU,IAAI,OAAO,SAAS;AAC5B,UAAI;AACF,cAAM,UAAU,MAAM,qBAAqB,IAAI;AAC/C,eAAO,MAAM,QAAQ,YAAY;AAAA,MACnC,SAAS,KAAK;AACZ,eAAO;AAAA,UACL;AAAA,UACA,WAAW;AAAA,UACX,mBAAmB,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAAA,UAClE,QAAQ,CAAC;AAAA,UACT,OAAO,CAAC;AAAA,QACV;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO,EAAE,iBAAiB,GAAG,UAAU,QAAQ;AACjD;","names":[]}
@@ -3,7 +3,7 @@ import { execSync, spawn } from "child_process";
3
3
  import { EventEmitter } from "events";
4
4
  import { existsSync, readFileSync } from "fs";
5
5
  import { createRequire } from "module";
6
- import { homedir, platform } from "os";
6
+ import { homedir, platform as platform2 } from "os";
7
7
  import { dirname, join as join2 } from "path";
8
8
 
9
9
  // ../codex-adapter/dist/codex-spawn-args.js
@@ -11,18 +11,33 @@ import { writeFileSync, unlinkSync } from "fs";
11
11
  import { join } from "path";
12
12
  import { tmpdir } from "os";
13
13
  import { randomUUID } from "crypto";
14
- import { DEFAULT_SANDBOX } from "@mclawnet/shared";
14
+ import { DEFAULT_SANDBOX, SANDBOX_LEVELS } from "@mclawnet/shared";
15
15
  function escapeToml(value) {
16
16
  return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
17
17
  }
18
+ var VALID_CODEX_SANDBOX = new Set(SANDBOX_LEVELS);
18
19
  function buildCodexSpawnArgs(options) {
19
20
  const args = ["app-server"];
20
21
  let briefingFile;
21
22
  void options.resumeId;
22
23
  args.push("-c", 'approval_policy="never"');
23
- const sandboxLevel = options.sandbox ?? DEFAULT_SANDBOX;
24
+ let sandboxLevel;
25
+ let modeSource;
26
+ if (options.mode && VALID_CODEX_SANDBOX.has(options.mode)) {
27
+ sandboxLevel = options.mode;
28
+ modeSource = "options.mode";
29
+ } else if (options.sandbox) {
30
+ sandboxLevel = options.sandbox;
31
+ modeSource = "options.sandbox";
32
+ } else {
33
+ sandboxLevel = DEFAULT_SANDBOX;
34
+ modeSource = "default";
35
+ }
24
36
  const codexSandboxMode = sandboxLevel === "read-only" ? "read-only" : sandboxLevel === "full-access" ? "danger-full-access" : "workspace-write";
25
37
  args.push("-c", `sandbox_mode="${codexSandboxMode}"`);
38
+ if (options.model) {
39
+ args.push("-c", `model="${escapeToml(options.model)}"`);
40
+ }
26
41
  if (options.mcpServer) {
27
42
  const { command, args: serverArgs, env } = options.mcpServer;
28
43
  args.push("-c", `mcp_servers.clawnet-mcp.command="${escapeToml(command)}"`);
@@ -43,7 +58,7 @@ function buildCodexSpawnArgs(options) {
43
58
  writeFileSync(briefingFile, options.systemPrompt, { encoding: "utf-8", mode: 384 });
44
59
  args.push("-c", `model_instructions_file="${escapeToml(briefingFile)}"`);
45
60
  }
46
- return { args, briefingFile };
61
+ return { args, briefingFile, modeSource };
47
62
  }
48
63
  function cleanupBriefingFile(filePath) {
49
64
  try {
@@ -295,9 +310,6 @@ function mapCodexFrame(frame) {
295
310
  }
296
311
  case "item/completed": {
297
312
  const item = params.item;
298
- if (item?.type === "agentMessage" && typeof item.text === "string") {
299
- return { kind: "assistant_text", text: item.text };
300
- }
301
313
  if (item?.type === "commandExecution") {
302
314
  const failed = item.status === "failed" || item.status === "declined";
303
315
  const badExit = typeof item.exitCode === "number" && item.exitCode !== 0;
@@ -376,9 +388,57 @@ function mapCodexFrame(frame) {
376
388
  return { kind: "raw", backend: "codex", payload: frame };
377
389
  }
378
390
 
391
+ // ../codex-adapter/dist/catalog.js
392
+ var CODEX_MODELS = [
393
+ {
394
+ code: "gpt-4o",
395
+ label: "GPT-4o",
396
+ tier: "premium",
397
+ contextWindow: 128e3,
398
+ supportsVision: true,
399
+ supportsToolUse: true,
400
+ default: true
401
+ },
402
+ {
403
+ code: "gpt-4o-mini",
404
+ label: "GPT-4o mini",
405
+ tier: "standard",
406
+ contextWindow: 128e3,
407
+ supportsVision: true,
408
+ supportsToolUse: true
409
+ }
410
+ ];
411
+ var CODEX_MODES = [
412
+ { code: "read-only", label: "Read Only", description: "No FS writes" },
413
+ { code: "workspace-write", label: "Workspace Write", description: "Write inside cwd only", default: true },
414
+ { code: "full-access", label: "Full Access", description: "No sandbox" }
415
+ ];
416
+
417
+ // ../codex-adapter/dist/detect.js
418
+ import { execFile } from "child_process";
419
+ import { promisify } from "util";
420
+ import { platform } from "os";
421
+ var exec = promisify(execFile);
422
+ async function detectCodexInstall() {
423
+ try {
424
+ const isWin2 = platform() === "win32";
425
+ const lookup = isWin2 ? "where" : "which";
426
+ const { stdout: lookupOut } = await exec(lookup, ["codex"], { timeout: 5e3 });
427
+ const binaryPath = lookupOut.trim().split(/\r?\n/)[0]?.trim();
428
+ if (!binaryPath) {
429
+ return { installed: false, reason: `codex CLI not detected: empty ${lookup} output` };
430
+ }
431
+ const { stdout: verOut } = await exec(binaryPath, ["--version"], { timeout: 5e3 });
432
+ return { installed: true, binaryPath, version: verOut.trim() };
433
+ } catch (err) {
434
+ const message = err instanceof Error ? err.message : String(err);
435
+ return { installed: false, reason: `codex CLI not detected: ${message}` };
436
+ }
437
+ }
438
+
379
439
  // ../codex-adapter/dist/codex-adapter.js
380
440
  var log = createLogger({ module: "codex-adapter" });
381
- var isWin = platform() === "win32";
441
+ var isWin = platform2() === "win32";
382
442
  function resolveCodexBin(explicit) {
383
443
  if (explicit && explicit !== "codex")
384
444
  return explicit;
@@ -430,7 +490,7 @@ function getResolvedCodexBin(explicit) {
430
490
  return resolved;
431
491
  }
432
492
  function resolveSpawnTarget(codexBin) {
433
- const isWindows = platform() === "win32";
493
+ const isWindows = platform2() === "win32";
434
494
  if (!isWindows || !codexBin.toLowerCase().endsWith(".cmd")) {
435
495
  return { command: codexBin, prefixArgs: [] };
436
496
  }
@@ -460,6 +520,7 @@ var CLAWNET_CLIENT_INFO = {
460
520
  version: "0.1.0",
461
521
  title: "ClawNet Agent"
462
522
  };
523
+ var ASSISTANT_TEXT_SEEN_MAX = 64;
463
524
  var CodexProcess = class extends EventEmitter {
464
525
  id;
465
526
  workDir;
@@ -487,6 +548,24 @@ var CodexProcess = class extends EventEmitter {
487
548
  handshakeComplete = false;
488
549
  /** Inputs queued by send() while the handshake is still in flight. */
489
550
  pendingInputs = [];
551
+ /**
552
+ * Per-itemId accumulator of assistant text already emitted via
553
+ * `item/agentMessage/delta` chunks. Consulted on `item/completed` with
554
+ * `item.type === "agentMessage"` to decide what (if anything) of the
555
+ * completed payload still needs to be emitted. Without this, codex
556
+ * delivers the same assistant text twice (deltas + final) and the UI
557
+ * renders "Hello worldHello world".
558
+ *
559
+ * Entries are cleared per item when the matching `item/completed` fires.
560
+ * We do NOT clear the whole map on `turn/completed`: codex's wire
561
+ * doesn't strictly guarantee that every item/completed precedes its
562
+ * parent turn/completed, and a trailing item/completed against an empty
563
+ * accumulator would re-emit the full text (reactivating the bug). To
564
+ * cap unbounded growth from interrupted turns that never fire
565
+ * completed, the map is size-bounded with FIFO eviction; in practice a
566
+ * single turn produces 1-2 agentMessage items so the cap is generous.
567
+ */
568
+ assistantTextSeen = /* @__PURE__ */ new Map();
490
569
  /** Temp file for briefing injection; cleaned up on kill(). */
491
570
  briefingFile;
492
571
  /**
@@ -524,6 +603,7 @@ var CodexProcess = class extends EventEmitter {
524
603
  this.approvalMethods.clear();
525
604
  this.approvalResolvers.clear();
526
605
  this.pendingInputs.length = 0;
606
+ this.assistantTextSeen.clear();
527
607
  if (this.briefingFile) {
528
608
  cleanupBriefingFile(this.briefingFile);
529
609
  this.briefingFile = void 0;
@@ -547,23 +627,30 @@ var CodexAdapter = class {
547
627
  type = "codex";
548
628
  codexBin;
549
629
  handshakeTimeoutMs;
630
+ detect;
550
631
  constructor(options) {
551
632
  this.codexBin = getResolvedCodexBin(options?.codexBin);
552
633
  const envTimeout = Number(process.env.CLAWNET_CODEX_HANDSHAKE_TIMEOUT_MS);
553
634
  this.handshakeTimeoutMs = options?.handshakeTimeoutMs ?? (Number.isFinite(envTimeout) && envTimeout > 0 ? envTimeout : 15e3);
635
+ this.detect = options?.detect ?? detectCodexInstall;
554
636
  }
555
637
  async spawn(options) {
556
638
  let cwd = options.workDir || process.cwd();
557
639
  if (!existsSync(cwd))
558
640
  cwd = homedir();
559
641
  const mcpServer = this.buildMcpServerConfig(options);
560
- const { args, briefingFile } = buildCodexSpawnArgs({
642
+ const { args, briefingFile, modeSource } = buildCodexSpawnArgs({
561
643
  sessionId: options.sessionId,
562
644
  resumeId: options.resumeId,
563
645
  systemPrompt: options.systemPrompt,
564
646
  mcpServer,
565
- sandbox: options.sandbox
647
+ sandbox: options.sandbox,
648
+ model: options.model,
649
+ mode: options.mode
566
650
  });
651
+ if (options.mode && modeSource !== "options.mode") {
652
+ log.warn({ sessionId: options.sessionId, requestedMode: options.mode, modeSource }, "codex: invalid options.mode ignored, falling back");
653
+ }
567
654
  log.info({ sessionId: options.sessionId, bin: this.codexBin, args, cwd, resumeId: options.resumeId }, "codex spawn: forking app-server");
568
655
  const { command, prefixArgs } = resolveSpawnTarget(this.codexBin);
569
656
  if (command !== this.codexBin) {
@@ -572,7 +659,7 @@ var CodexAdapter = class {
572
659
  const proc = spawn(command, [...prefixArgs, ...args], {
573
660
  cwd,
574
661
  stdio: ["pipe", "pipe", "pipe"],
575
- env: process.env,
662
+ env: { ...process.env, ...options.env ?? {} },
576
663
  windowsHide: true
577
664
  });
578
665
  const stderrChunks = [];
@@ -824,6 +911,49 @@ var CodexAdapter = class {
824
911
  log.info({ sessionId: cp.id }, "codex turn started");
825
912
  return;
826
913
  }
914
+ if (method === "item/agentMessage/delta") {
915
+ const p = params ?? {};
916
+ if (typeof p.itemId === "string" && typeof p.delta === "string") {
917
+ const prev = cp.assistantTextSeen.get(p.itemId) ?? "";
918
+ if (!cp.assistantTextSeen.has(p.itemId) && cp.assistantTextSeen.size >= ASSISTANT_TEXT_SEEN_MAX) {
919
+ const oldest = cp.assistantTextSeen.keys().next().value;
920
+ if (oldest !== void 0)
921
+ cp.assistantTextSeen.delete(oldest);
922
+ }
923
+ cp.assistantTextSeen.set(p.itemId, prev + p.delta);
924
+ } else {
925
+ log.warn({ sessionId: cp.id, params: p }, "codex agentMessage delta: missing itemId/delta \u2014 dedupe will not apply to this item");
926
+ }
927
+ } else if (method === "item/completed") {
928
+ const p = params ?? {};
929
+ const item = p.item;
930
+ if (item?.type === "agentMessage") {
931
+ if (typeof item.id !== "string" || typeof item.text !== "string") {
932
+ log.warn({ sessionId: cp.id, item }, "codex agentMessage completed: malformed item.id/text \u2014 dropping to avoid duplicate emission");
933
+ return;
934
+ }
935
+ const seen = cp.assistantTextSeen.get(item.id) ?? "";
936
+ cp.assistantTextSeen.delete(item.id);
937
+ const full = item.text;
938
+ let suffix;
939
+ if (full === seen) {
940
+ return;
941
+ } else if (seen.length > 0 && full.startsWith(seen)) {
942
+ suffix = full.slice(seen.length);
943
+ } else {
944
+ suffix = full;
945
+ if (seen.length > 0) {
946
+ log.warn({ sessionId: cp.id, itemId: item.id, seenLen: seen.length, fullLen: full.length }, "codex agentMessage: completed text diverges from accumulated deltas \u2014 emitting full");
947
+ }
948
+ }
949
+ if (suffix.length === 0)
950
+ return;
951
+ const out2 = { kind: "assistant_text", text: suffix };
952
+ this.logBackendOutput(cp.id, method, out2);
953
+ cp.emit("output", out2);
954
+ return;
955
+ }
956
+ }
827
957
  const out = mapCodexFrame({ method, params });
828
958
  this.logBackendOutput(cp.id, method, out);
829
959
  cp.emit("output", out);
@@ -928,6 +1058,18 @@ var CodexAdapter = class {
928
1058
  if (process2 instanceof CodexProcess)
929
1059
  process2.on("exit", handler);
930
1060
  }
1061
+ async getManifest() {
1062
+ const det = await this.detect();
1063
+ return {
1064
+ kind: "codex",
1065
+ installed: det.installed,
1066
+ binaryPath: det.binaryPath,
1067
+ version: det.version,
1068
+ unavailableReason: det.reason,
1069
+ models: det.installed ? CODEX_MODELS : [],
1070
+ modes: det.installed ? CODEX_MODES : []
1071
+ };
1072
+ }
931
1073
  };
932
1074
  export {
933
1075
  CodexAdapter,
@@ -937,4 +1079,4 @@ export {
937
1079
  mapCodexFrame,
938
1080
  parseApprovalRequest
939
1081
  };
940
- //# sourceMappingURL=dist-EGT2NQEW.js.map
1082
+ //# sourceMappingURL=dist-NWVHAP5R.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../codex-adapter/src/codex-adapter.ts","../../codex-adapter/src/codex-spawn-args.ts","../../codex-adapter/src/json-rpc-client.ts","../../codex-adapter/src/permission-mapper.ts","../../codex-adapter/src/output-mapper.ts","../../codex-adapter/src/catalog.ts","../../codex-adapter/src/detect.ts"],"sourcesContent":["import { execSync, spawn, type ChildProcess } from \"node:child_process\";\nimport { EventEmitter } from \"node:events\";\nimport { existsSync, readFileSync } from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport { homedir, platform } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport { buildCodexSpawnArgs, cleanupBriefingFile, type McpServerConfig } from \"./codex-spawn-args.js\";\nimport type {\n BackendAdapter,\n BackendProcess,\n BackendOutput,\n PermissionDecision,\n PermissionRequest,\n SpawnOptions,\n} from \"@mclawnet/backend-types\";\nimport type { BackendManifestEntry } from \"@mclawnet/shared\";\nimport { createLogger, preview } from \"@mclawnet/logger\";\nimport { JsonRpcClient } from \"./json-rpc-client.js\";\nimport {\n buildApprovalReply,\n parseApprovalRequest,\n type ApprovalWireFamily,\n} from \"./permission-mapper.js\";\nimport { mapCodexFrame } from \"./output-mapper.js\";\nimport { CODEX_MODELS, CODEX_MODES } from \"./catalog.js\";\nimport { detectCodexInstall, type DetectResult } from \"./detect.js\";\n\nconst log = createLogger({ module: \"codex-adapter\" });\n\nconst isWin = platform() === \"win32\";\n\n/**\n * Resolve the codex CLI binary the same way claude-adapter resolves claude:\n * use the OS lookup tool (where/which), fall back to known install paths.\n *\n * Without this, Windows users hit two failure modes:\n * 1. `spawn(\"codex\", ...)` fails because Windows `CreateProcess` does NOT\n * consult PATHEXT — only cmd.exe does — so a bare \"codex\" never finds\n * the npm-installed `codex.cmd` / `codex.exe` shim.\n * 2. Even if cmd.exe is on the way, post-CVE-2024-27980 Node refuses to\n * spawn `.cmd` / `.bat` without `shell: true`.\n */\nfunction resolveCodexBin(explicit?: string): string {\n if (explicit && explicit !== \"codex\") return explicit;\n\n const names = isWin ? [\"codex.exe\", \"codex.cmd\", \"codex.bat\"] : [\"codex\"];\n const whichCmd = isWin ? \"where\" : \"which\";\n for (const name of names) {\n try {\n const found = execSync(`${whichCmd} ${name}`, {\n encoding: \"utf-8\",\n // 1.5s is plenty for `where`/`which` on a healthy machine; the old\n // 5s × 3 worst-case (15s blocking event loop) showed up on slow\n // domain-joined Windows hosts / network-PATH setups.\n timeout: 1500,\n stdio: [\"ignore\", \"pipe\", \"ignore\"],\n }).trim().split(/\\r?\\n/)[0];\n if (found && existsSync(found)) return found;\n } catch {\n /* not in PATH */\n }\n }\n\n const home = homedir();\n const fallbacks = isWin\n ? [\n join(home, \"AppData\", \"Roaming\", \"npm\", \"codex.cmd\"),\n join(home, \"AppData\", \"Roaming\", \"npm\", \"codex.exe\"),\n join(home, \".cargo\", \"bin\", \"codex.exe\"),\n join(home, \".local\", \"bin\", \"codex.exe\"),\n ]\n : [\n join(home, \".cargo\", \"bin\", \"codex\"),\n join(home, \".local\", \"bin\", \"codex\"),\n \"/opt/homebrew/bin/codex\",\n \"/usr/local/bin/codex\",\n ];\n for (const p of fallbacks) {\n if (existsSync(p)) return p;\n }\n\n return isWin ? \"codex.cmd\" : \"codex\";\n}\n\n/**\n * Module-level cache for the resolved codex binary. Without this, every\n * CodexAdapter instance (created per-session by the agent's adapter factory)\n * pays the `where`/`which` cost again. On a Windows host where `where`\n * times out (slow PATH, network drive, AV scan), 3 lookups × 1.5s would\n * stall agent startup for every spawn.\n *\n * Cache is keyed on the override string so explicit per-instance overrides\n * (e.g. tests) bypass the shared cache.\n */\nconst resolvedCodexBinCache = new Map<string, string>();\nlet resolveCodexBinCallCount = 0;\nfunction getResolvedCodexBin(explicit?: string): string {\n const key = explicit ?? \"\";\n const hit = resolvedCodexBinCache.get(key);\n if (hit) return hit;\n resolveCodexBinCallCount++;\n const resolved = resolveCodexBin(explicit);\n resolvedCodexBinCache.set(key, resolved);\n return resolved;\n}\n\n/** Test-only: clear the resolved-bin cache between cases. */\nexport function __resetResolvedCodexBinCache(): void {\n resolvedCodexBinCache.clear();\n resolveCodexBinCallCount = 0;\n}\n\n/** Test-only: how many times resolveCodexBin actually ran (i.e. cache misses). */\nexport function __getResolveCodexBinCallCount(): number {\n return resolveCodexBinCallCount;\n}\n\n/**\n * Resolve how to spawn codex without invoking cmd.exe on Windows.\n *\n * codex.cmd is an npm shim that dispatches to the real Rust .exe (or in\n * some installs, a node-shimmed cli.js). Spawning the .cmd directly:\n * - requires `shell: true` post-CVE-2024-27980, which would also have to\n * deal with cmd.exe's 8191-char command-line cap.\n * - drops the child's stdio attachment in subtle ways through cmd.exe.\n *\n * So we parse the shim and spawn the underlying .exe (or `node cli.js`)\n * directly. Mirrors claude-adapter's resolveSpawnTarget pattern.\n */\nexport function resolveSpawnTarget(codexBin: string): {\n command: string;\n prefixArgs: string[];\n} {\n // Re-read platform per call (instead of using the module-level `isWin`\n // constant) so tests can flip `process.platform` to exercise Windows\n // paths from a macOS / Linux CI. Mirrors claude-adapter's pattern.\n const isWindows = platform() === \"win32\";\n if (!isWindows || !codexBin.toLowerCase().endsWith(\".cmd\")) {\n return { command: codexBin, prefixArgs: [] };\n }\n try {\n const cmdContent = readFileSync(codexBin, \"utf-8\");\n // codex shim typically points at a sibling .exe: e.g.\n // \"%~dp0\\node_modules\\@openai\\codex\\bin\\codex.exe\" %*\n const exeMatch = cmdContent.match(/(?:%~?dp0%?)[\\\\/](.+?\\.exe)/i);\n if (exeMatch) {\n const relParts = exeMatch[1].split(/[\\\\/]/);\n const exePath = join(dirname(codexBin), ...relParts);\n if (existsSync(exePath)) return { command: exePath, prefixArgs: [] };\n }\n // Fallback: npm-style `node cli.js` shim.\n const jsMatch = cmdContent.match(/(?:%~?dp0%?)[\\\\/](.+?\\.js)/i);\n if (jsMatch) {\n const relParts = jsMatch[1].split(/[\\\\/]/);\n const cliJsPath = join(dirname(codexBin), ...relParts);\n if (existsSync(cliJsPath)) {\n return { command: process.execPath, prefixArgs: [cliJsPath] };\n }\n }\n } catch {\n /* fall through */\n }\n // Last resort: invoke through cmd.exe. Loses the 8191-char cap protection\n // but at least the process gets spawned. Args are short for codex so this\n // is unlikely to bite.\n return { command: codexBin, prefixArgs: [] };\n}\n\nconst CLAWNET_CLIENT_INFO = {\n name: \"clawnet\",\n version: \"0.1.0\",\n title: \"ClawNet Agent\",\n};\n\n/**\n * Cap on the per-process `assistantTextSeen` map. Bounds memory growth\n * from interrupted agentMessage items that never fire `item/completed`\n * (a single very long session would otherwise leak forever now that\n * `turn/completed` no longer wholesale-clears — see the comment on that\n * branch in `handleNotification`).\n *\n * A single codex turn typically produces 1-2 agentMessage items; 64\n * comfortably covers extreme cases without making eviction reachable\n * during normal use. If the cap is exceeded, the OLDEST untouched entry\n * is dropped (FIFO); a subsequent matching `item/completed` for that\n * itemId would then see an empty accumulator and emit the full text,\n * producing at most one duplicated message instead of an unbounded leak.\n */\nconst ASSISTANT_TEXT_SEEN_MAX = 64;\n\n/**\n * Per-process state wrapper. Holds the JSON-RPC client + pending approval\n * resolvers so `respondToPermission` can find the right server-initiated\n * request id to reply to.\n */\nexport class CodexProcess extends EventEmitter implements BackendProcess {\n readonly id: string;\n readonly workDir: string;\n pid?: number;\n proc?: ChildProcess;\n private killed = false;\n /** callId → JSON-RPC server-request id we still owe a result to. */\n pendingApprovals = new Map<string, number>();\n /** callId → originating wire family (drives v2/legacy/mcp reply shape). */\n approvalMethods = new Map<string, ApprovalWireFamily>();\n /** Monotonic counter for approval resolver keys. */\n private nextResolverKey = 1;\n /** Resolver registered by the rpc client for each pending approval. */\n approvalResolvers = new Map<number, (reply: Record<string, string>) => void>();\n rpc?: JsonRpcClient;\n backendSessionId?: string;\n /**\n * Thread id to resume on agent restart. When set, handshake() sends\n * `thread/resume { threadId }` instead of `thread/start { cwd }`.\n * Set from SpawnOptions.resumeId in CodexAdapter.spawn(). Must NOT be\n * passed as `--resume` CLI flag — app-server rejects it.\n */\n resumeId?: string;\n /** True once `initialize` round-trip + `thread/start` (or `thread/resume`) have completed. */\n handshakeComplete = false;\n /** Inputs queued by send() while the handshake is still in flight. */\n pendingInputs: string[] = [];\n /**\n * Per-itemId accumulator of assistant text already emitted via\n * `item/agentMessage/delta` chunks. Consulted on `item/completed` with\n * `item.type === \"agentMessage\"` to decide what (if anything) of the\n * completed payload still needs to be emitted. Without this, codex\n * delivers the same assistant text twice (deltas + final) and the UI\n * renders \"Hello worldHello world\".\n *\n * Entries are cleared per item when the matching `item/completed` fires.\n * We do NOT clear the whole map on `turn/completed`: codex's wire\n * doesn't strictly guarantee that every item/completed precedes its\n * parent turn/completed, and a trailing item/completed against an empty\n * accumulator would re-emit the full text (reactivating the bug). To\n * cap unbounded growth from interrupted turns that never fire\n * completed, the map is size-bounded with FIFO eviction; in practice a\n * single turn produces 1-2 agentMessage items so the cap is generous.\n */\n assistantTextSeen = new Map<string, string>();\n /** Temp file for briefing injection; cleaned up on kill(). */\n briefingFile?: string;\n /**\n * Set by CodexAdapter.spawn() so handshake() can surface real process-exit\n * info if the codex CLI died (e.g. arg parse error). Without this, a dead\n * process leads to a generic \"handshake timeout\" instead of the actual\n * \"error: unexpected argument …\" stderr message.\n *\n * Default returns (null, \"\") for tests that construct CodexProcess\n * directly without going through spawn() (e.g. attachRpc mock pipes).\n * spawn() overrides these with real getters wired to the child process.\n */\n getExitInfo: () => { code: number | null; signal: string | null } | null = () => null;\n getStderr: () => string = () => \"\";\n\n /** Allocate a resolver key. Monotonic; never reused. */\n allocResolverKey(): number {\n return this.nextResolverKey++;\n }\n\n constructor(sessionId: string, workDir: string, proc?: ChildProcess) {\n super();\n this.id = sessionId;\n this.workDir = workDir;\n this.proc = proc;\n this.pid = proc?.pid;\n }\n\n async kill(): Promise<void> {\n this.killed = true;\n // Drain any pending approval bookkeeping so long-running hubs don't leak\n // entries on aborted sessions. Resolve any outstanding RPC promise with a\n // \"cancel\"-equivalent so callers awaiting `respondToPermission` never hang.\n for (const [, resolver] of this.approvalResolvers) {\n try {\n resolver({ decision: \"abort\" });\n } catch {\n // ignore\n }\n }\n this.pendingApprovals.clear();\n this.approvalMethods.clear();\n this.approvalResolvers.clear();\n this.pendingInputs.length = 0;\n this.assistantTextSeen.clear();\n if (this.briefingFile) {\n cleanupBriefingFile(this.briefingFile);\n this.briefingFile = undefined;\n }\n if (this.proc && !this.proc.killed) {\n try {\n this.proc.kill(\"SIGTERM\");\n } catch {\n // ignore\n }\n }\n }\n\n isAlive(): boolean {\n if (this.killed) return false;\n if (!this.proc) return true;\n return !this.proc.killed && this.proc.exitCode === null;\n }\n}\n\nexport interface CodexAdapterOptions {\n /** Override path to the codex binary. Default: lookup in PATH. */\n codexBin?: string;\n /**\n * Max time to wait for codex handshake (initialize + thread/start|resume)\n * to complete before emitting an error. Default: 15s. Used to detect the\n * case where the codex process dies mid-handshake (CLI parse error,\n * crash) — without this, pendingInputs stack up forever and the swarm\n * silently stalls.\n */\n handshakeTimeoutMs?: number;\n /**\n * Override the CLI detection probe. Defaults to `detectCodexInstall`.\n * Exposed for tests (ESM module exports aren't configurable, so we\n * inject the dependency instead of spying on the module).\n */\n detect?: () => Promise<DetectResult>;\n}\n\n/**\n * CodexAdapter — BackendAdapter wrapping `codex app-server --listen stdio://`.\n *\n * M3.S3 scope: spawn/stop/send/onOutput plus the permission flow\n * (`onPermissionRequest` / `respondToPermission`). Resume + token-budget +\n * MCP plumbing land in follow-up slices.\n */\nexport class CodexAdapter implements BackendAdapter {\n readonly type = \"codex\";\n private codexBin: string;\n private handshakeTimeoutMs: number;\n private detect: () => Promise<DetectResult>;\n\n constructor(options?: CodexAdapterOptions) {\n this.codexBin = getResolvedCodexBin(options?.codexBin);\n // Precedence: explicit constructor option > env var > 15s default. The\n // env var (CLAWNET_CODEX_HANDSHAKE_TIMEOUT_MS) is the escape hatch for\n // slow CI / cold-start machines where 15s isn't enough and the user\n // can't easily inject a CodexAdapterOptions override.\n const envTimeout = Number(process.env.CLAWNET_CODEX_HANDSHAKE_TIMEOUT_MS);\n this.handshakeTimeoutMs =\n options?.handshakeTimeoutMs ??\n (Number.isFinite(envTimeout) && envTimeout > 0 ? envTimeout : 15000);\n this.detect = options?.detect ?? detectCodexInstall;\n }\n\n async spawn(options: SpawnOptions): Promise<CodexProcess> {\n let cwd = options.workDir || process.cwd();\n if (!existsSync(cwd)) cwd = homedir();\n\n // Build args with MCP + briefing injection\n const mcpServer = this.buildMcpServerConfig(options);\n const { args, briefingFile, modeSource } = buildCodexSpawnArgs({\n sessionId: options.sessionId,\n resumeId: options.resumeId,\n systemPrompt: options.systemPrompt,\n mcpServer,\n sandbox: options.sandbox,\n model: options.model,\n mode: options.mode,\n });\n\n if (options.mode && modeSource !== \"options.mode\") {\n log.warn(\n { sessionId: options.sessionId, requestedMode: options.mode, modeSource },\n \"codex: invalid options.mode ignored, falling back\",\n );\n }\n\n log.info(\n { sessionId: options.sessionId, bin: this.codexBin, args, cwd, resumeId: options.resumeId },\n \"codex spawn: forking app-server\",\n );\n const { command, prefixArgs } = resolveSpawnTarget(this.codexBin);\n if (command !== this.codexBin) {\n log.info(\n { sessionId: options.sessionId, bin: this.codexBin, spawnCmd: command, prefixArgs },\n \"codex spawn: using resolved shim target (windows .cmd bypass)\",\n );\n }\n const proc = spawn(command, [...prefixArgs, ...args], {\n cwd,\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n env: { ...process.env, ...(options.env ?? {}) },\n windowsHide: true,\n });\n // Capture exit state into a closure so handshake() can surface it if\n // initialize / thread RPC times out. Without this guard a dead codex\n // (e.g. CLI arg parse error → ~600ms after spawn) would leave the\n // adapter waiting handshakeTimeoutMs (default 15s) with a generic\n // \"handshake timeout\" message. With it, we report exit code + stderr.\n const stderrChunks: string[] = [];\n proc.stderr?.on(\"data\", (chunk: Buffer) => {\n const text = chunk.toString(\"utf8\").trim();\n if (text) {\n stderrChunks.push(text);\n log.warn({ sessionId: options.sessionId, stderr: text }, \"codex stderr\");\n }\n });\n let exitInfo: { code: number | null; signal: string | null } | null = null;\n // cp is built AFTER this Promise resolves, so capture a thunk and resolve\n // it lazily. proc.on(\"exit\") needs to re-emit on cp so the BackendAdapter\n // contract's onExit/onError subscribers (session-manager) actually fire —\n // without this re-emit, codex crashes silently never reach the swarm\n // coordinator's handleRoleCrashed path and the queen stays unaware.\n let createdCp: CodexProcess | undefined;\n // Shared emit helper: invoked both from the proc.on(\"exit\") handler and\n // the late-fire safety branch below. Centralizes the \"emit exit + maybe\n // emit error with stderr\" contract so future changes can't drift.\n const emitProcessExit = (cp: CodexProcess, code: number | null, signal: string | null) => {\n try { cp.emit(\"exit\", code); } catch { /* listener threw */ }\n const isClean = code === 0 || signal === \"SIGTERM\";\n if (isClean) return;\n // Surface stderr via \"error\" so the agent's onSessionError handler\n // can include the real failure cause (e.g. \"error: unexpected\n // argument\", codex auth missing) instead of a bare \"exit code=1\".\n const stderr = stderrChunks.join(\"\\n\").trim().slice(0, 800);\n const detail = stderr ? ` — stderr: ${stderr}` : \"\";\n try {\n cp.emit(\n \"error\",\n new Error(`codex process exited (code=${code ?? \"null\"}, signal=${signal ?? \"null\"})${detail}`),\n );\n } catch { /* listener threw */ }\n };\n proc.on(\"exit\", (code, signal) => {\n const isClean = code === 0 || signal === \"SIGTERM\";\n const level = isClean ? \"debug\" : \"warn\";\n log[level]({ sessionId: options.sessionId, code, signal }, \"codex process exited\");\n if (!exitInfo) exitInfo = { code, signal };\n const cpRef = createdCp;\n if (!cpRef) return;\n emitProcessExit(cpRef, code, signal);\n });\n\n await new Promise<void>((resolve, reject) => {\n const t = setTimeout(() => resolve(), 2000);\n proc.on(\"error\", (err) => {\n clearTimeout(t);\n reject(new Error(`Failed to spawn codex CLI: ${err.message}`));\n });\n proc.on(\"spawn\", () => {\n clearTimeout(t);\n log.debug({ sessionId: options.sessionId, pid: proc.pid }, \"codex spawn: process started\");\n resolve();\n });\n });\n\n const cp = new CodexProcess(options.sessionId, cwd, proc);\n cp.briefingFile = briefingFile;\n cp.resumeId = options.resumeId;\n // Expose process-exit signal to handshake so a timeout can report the\n // real cause instead of a generic \"timeout\".\n cp.getExitInfo = () => exitInfo;\n cp.getStderr = () => stderrChunks.join(\"\\n\").slice(0, 800);\n createdCp = cp;\n // Late-fire safety: if the process exited BEFORE we got here (cp wasn't\n // assigned yet so the proc.on(\"exit\") path skipped the re-emit), surface\n // it now via the same channel. setImmediate defers until session-manager\n // has had a chance to subscribe (which happens synchronously after our\n // returned cp lands).\n if (exitInfo) {\n const { code, signal } = exitInfo;\n setImmediate(() => emitProcessExit(cp, code, signal));\n }\n this.wireRpc(cp);\n return cp;\n }\n\n private buildMcpServerConfig(options: SpawnOptions): McpServerConfig | undefined {\n // Resolve clawnet-mcp-server path. The package's \"exports\" map is a\n // closed whitelist (only \".\" and \"./package.json\" are exported, plus\n // \"./server\"). Resolving `@mclawnet/mcp-server/dist/server.js` directly\n // throws ERR_PACKAGE_PATH_NOT_EXPORTED on modern Node, which is why\n // packaged-release Windows hosts saw \"not resolvable\" while the dev\n // monorepo path silently worked.\n //\n // Strategy: try the supported subpath export first (\"./server\"), then\n // resolve via package.json + join, then fall back to the legacy direct\n // path (still useful when \"./server\" isn't published yet but a hub\n // installs an older mcp-server release), then dev-monorepo fallback.\n let serverPath: string | undefined;\n const require = createRequire(import.meta.url);\n const tryResolve = (specifier: string): string | undefined => {\n try {\n const p = require.resolve(specifier);\n return p && existsSync(p) ? p : undefined;\n } catch {\n return undefined;\n }\n };\n serverPath = tryResolve(\"@mclawnet/mcp-server/server\");\n if (!serverPath) {\n try {\n const pkgPath = require.resolve(\"@mclawnet/mcp-server/package.json\");\n const candidate = join(dirname(pkgPath), \"dist\", \"server.js\");\n if (existsSync(candidate)) serverPath = candidate;\n } catch {\n /* not installed */\n }\n }\n if (!serverPath) {\n serverPath = tryResolve(\"@mclawnet/mcp-server/dist/server.js\");\n }\n if (!serverPath) {\n const devPath = join(\n import.meta.dirname ?? __dirname,\n \"../../mcp-server/dist/server.js\",\n );\n if (existsSync(devPath)) serverPath = devPath;\n }\n if (!serverPath) {\n log.warn(\n { pkg: \"@mclawnet/mcp-server\" },\n \"codex: clawnet-mcp-server not resolvable, codex role will lack MCP tools\",\n );\n return undefined;\n }\n log.debug({ serverPath }, \"codex: resolved clawnet-mcp-server path\");\n const env: Record<string, string> = {};\n if (options.workDir) env.CLAWNET_WORK_DIR = options.workDir;\n // mcp-server's server.ts:28 explicitly documents: CLAWNET_HOME is the\n // user home (NO `.clawnet` suffix) — the server appends `.clawnet`\n // itself via projectRoot(). Passing `~/.clawnet` here would yield\n // `~/.clawnet/.clawnet/...` (double append) and break inbox/task layout.\n const home = process.env.CLAWNET_HOME ?? homedir();\n env.CLAWNET_HOME = home;\n return { command: \"node\", args: [serverPath], env };\n }\n\n /**\n * Bind a CodexProcess to a JSON-RPC duplex. Public so tests can inject a\n * mocked stdin/stdout pair without spawning a real subprocess.\n *\n * Drives the v2 handshake automatically: `initialize` → `thread/start`.\n * Once `thread/start` resolves, `cp.backendSessionId` is set and a\n * `session_started` event fires so the agent's hub bridge can persist it.\n */\n attachRpc(\n cp: CodexProcess,\n stdin: NodeJS.WritableStream,\n stdout: NodeJS.ReadableStream,\n ): void {\n const rpc = new JsonRpcClient({\n stdin: stdin as never,\n stdout: stdout as never,\n onRequest: (method, params) => this.handleServerRequest(cp, method, params),\n onNotification: (method, params) => this.handleNotification(cp, method, params),\n onMalformedLine: (line, err) =>\n log.warn(\n { sessionId: cp.id, err: err.message, linePreview: line.slice(0, 200) },\n \"codex stdout: non-JSON line ignored\",\n ),\n });\n cp.rpc = rpc;\n void this.handshake(cp);\n }\n\n private async handshake(cp: CodexProcess): Promise<void> {\n if (!cp.rpc) return;\n const rpc = cp.rpc;\n const resumeId = cp.resumeId;\n log.info(\n { sessionId: cp.id, workDir: cp.workDir, resumeId },\n \"codex handshake: start\",\n );\n\n // Wrap the whole handshake in a timeout so a dead codex process (e.g.\n // killed by `--resume` CLI parse error, segfault, etc.) doesn't leave\n // pendingInputs queued forever. Without this guard the swarm silently\n // stalls — the user sees no error, just nothing happens.\n //\n // raceSettled is declared up here (before the IIFE) so the handshake\n // success path can also check it. If timeout/death wins the race,\n // handshakeAttempt may still complete later — without this guard it\n // would mutate cp.backendSessionId, emit session_started, and flush\n // pendingInputs against a process the caller already considers dead.\n let raceSettled = false;\n const handshakeAttempt = (async () => {\n await rpc.request(\"initialize\", { clientInfo: CLAWNET_CLIENT_INFO });\n log.info({ sessionId: cp.id }, \"codex handshake: initialize ok\");\n\n // Branch on resumeId — `codex app-server` rejects `--resume` CLI flag,\n // so resume MUST happen at the JSON-RPC layer via `thread/resume`.\n // `thread/start` on a resumed codex returns a NEW threadId (losing\n // conversation continuity), which is why we have to branch here.\n let threadId: string | undefined;\n if (resumeId) {\n const resumed = (await rpc.request(\"thread/resume\", { threadId: resumeId })) as\n | { thread?: { id?: string }; threadId?: string }\n | undefined;\n // Don't silently fall back to the input resumeId — if codex ever\n // migrates the thread id (the protocol allows it) we'd persist a\n // stale id and break the next restart. Treat absent thread id as a\n // protocol error so the caller sees it and can re-spawn fresh.\n threadId = resumed?.thread?.id ?? resumed?.threadId;\n } else {\n // v2 `thread/start` returns { thread: { id, sessionId, ... }, ... }.\n // The resumable identifier is `thread.id` — `sessionId` here is the\n // tree-id shared by forked threads, not what `thread/resume` wants.\n const started = (await rpc.request(\"thread/start\", { cwd: cp.workDir })) as\n | { thread?: { id?: string }; threadId?: string }\n | undefined;\n threadId = started?.thread?.id ?? started?.threadId;\n }\n\n if (!threadId) {\n throw new Error(\n `codex handshake: ${resumeId ? \"thread/resume\" : \"thread/start\"} returned no threadId`,\n );\n }\n // Race lost (timeout/death already won) → don't mutate state, don't\n // emit session_started, don't flush queued inputs. The handshake\n // error has already been reported via cp.emit(\"error\") in the\n // catch-block below.\n if (raceSettled) {\n log.warn(\n { sessionId: cp.id, threadId },\n \"codex handshake resolved AFTER race already lost — discarding result\",\n );\n return;\n }\n cp.backendSessionId = threadId;\n cp.handshakeComplete = true;\n log.info(\n { sessionId: cp.id, threadId, queuedInputs: cp.pendingInputs.length },\n \"codex handshake: complete, flushing queued inputs\",\n );\n cp.emit(\"session_started\", { backendSessionId: threadId });\n // Flush any inputs that arrived while the handshake was in flight.\n const queued = cp.pendingInputs.splice(0);\n for (const input of queued) {\n this.dispatchTurn(cp, input);\n }\n })();\n // Attach a no-op catch so handshakeAttempt rejecting AFTER losing the\n // race (timeout or death already won) doesn't bubble as an unhandled\n // promise rejection — node --unhandled-rejections=strict would crash.\n // The real error already surfaced via cp.emit(\"error\") above.\n handshakeAttempt.catch(() => {});\n\n let timeoutHandle: NodeJS.Timeout | undefined;\n let exitPollHandle: NodeJS.Timeout | undefined;\n // raceSettled is checked by the death-poll loop and the handshake\n // success path. clearTimeout in finally{} alone is insufficient:\n // poll() may be mid-execution when finally runs, scheduling the next\n // setTimeout before clear; the success path could likewise resolve a\n // late RPC after the race has been decided.\n const timeoutPromise = new Promise<never>((_, reject) => {\n timeoutHandle = setTimeout(() => {\n reject(\n new Error(\n `codex handshake timeout after ${this.handshakeTimeoutMs}ms (process may have died)`,\n ),\n );\n }, this.handshakeTimeoutMs);\n });\n\n // Process-death detector: poll exitInfo every 100ms; if codex exits\n // mid-handshake (e.g. CLI parse error), reject early with the real\n // stderr instead of waiting handshakeTimeoutMs (15s default) for the\n // generic timeout message.\n const deathPromise = new Promise<never>((_, reject) => {\n const poll = () => {\n if (raceSettled) return; // race already over — stop recursing\n const exit = cp.getExitInfo();\n if (exit) {\n const stderr = cp.getStderr();\n reject(\n new Error(\n `codex process died during handshake ` +\n `(code=${exit.code}, signal=${exit.signal}). ` +\n `stderr: ${stderr || \"(empty)\"}`,\n ),\n );\n return;\n }\n exitPollHandle = setTimeout(poll, 100);\n };\n poll();\n });\n\n try {\n await Promise.race([handshakeAttempt, timeoutPromise, deathPromise]);\n } catch (err) {\n log.error({ err, sessionId: cp.id }, \"codex v2 handshake failed\");\n cp.emit(\"error\", err);\n } finally {\n raceSettled = true;\n if (timeoutHandle) clearTimeout(timeoutHandle);\n if (exitPollHandle) clearTimeout(exitPollHandle);\n }\n }\n\n private wireRpc(cp: CodexProcess) {\n const proc = cp.proc;\n if (proc?.stdin && proc?.stdout) {\n this.attachRpc(cp, proc.stdin, proc.stdout);\n }\n }\n\n private handleServerRequest(\n cp: CodexProcess,\n method: string,\n params: unknown,\n ): Promise<unknown> {\n const parsed = parseApprovalRequest(method, params);\n if (!parsed) {\n log.warn({ method }, \"codex server-request: unrecognized method\");\n return Promise.resolve({ decision: \"denied\" });\n }\n const { req, wireFamily } = parsed;\n return new Promise((resolve) => {\n const resolverKey = cp.allocResolverKey();\n cp.pendingApprovals.set(req.callId, resolverKey);\n cp.approvalMethods.set(req.callId, wireFamily);\n cp.approvalResolvers.set(resolverKey, (reply) => {\n resolve(reply);\n });\n cp.emit(\"permission_request\", req);\n });\n }\n\n private handleNotification(cp: CodexProcess, method: string, params: unknown) {\n // v2 thread lifecycle\n if (method === \"thread/started\") {\n // codex emits this as a *notification* during normal turns too; the\n // primary session_started signal comes from thread/start's RPC result\n // (see handshake()). Suppress the duplicate here so onSessionStarted\n // listeners only fire once.\n return;\n }\n if (method === \"error\") {\n // codex `error` notifications carry the real failure cause (e.g.\n // \"Missing environment variable: COPILOT_API_KEY\", upstream provider\n // failure). Before this branch existed they degraded to {kind:\"raw\"} →\n // normalize-backend-output dropped them silently → swarm coordinator\n // never learned the role had failed → queen kept nudging a dead\n // worker. Promote to cp.emit(\"error\") so session-manager.onError →\n // onSessionError fires.\n const p = (params ?? {}) as {\n error?: { message?: string; codexErrorInfo?: string };\n willRetry?: boolean;\n };\n const message = p.error?.message ?? \"codex emitted unspecified error\";\n log.warn({ sessionId: cp.id, params }, \"codex error notification\");\n cp.emit(\"error\", new Error(`codex: ${message}`));\n // willRetry:false is codex saying \"this turn is dead, no retry\". Kill\n // the process so proc.on(\"exit\") fires → session-manager.onSessionExit\n // → swarmCoordinator.handleRoleCrashed flips the role to crashed and\n // wakes the queen. Leaving it alive would burn an idle process and\n // continue queen nudge loops.\n if (p.willRetry === false) {\n void cp.kill().catch((err) => {\n log.warn({ err, sessionId: cp.id }, \"kill after fatal codex error failed\");\n });\n }\n return;\n }\n if (method === \"thread/status/changed\") {\n // Codex pairs systemError status with an `error` notification that\n // carries willRetry. We forward the status as onError so the swarm\n // sees the failure signal immediately, but defer the kill decision to\n // the paired `error` frame (it has the willRetry bit). Other status\n // transitions (active, idle, running, …) are benign lifecycle events\n // — silently consume them, otherwise mapCodexFrame would degrade them\n // to {kind:\"raw\"} and spam log.warn for every normal turn.\n const p = (params ?? {}) as { status?: { type?: string } };\n if (p.status?.type === \"systemError\") {\n log.warn({ sessionId: cp.id, params }, \"codex thread systemError\");\n cp.emit(\"error\", new Error(\"codex: thread entered systemError\"));\n }\n return;\n }\n if (method === \"turn/completed\" || method === \"thread/turnComplete\" || method === \"turn/complete\") {\n log.info({ sessionId: cp.id, method }, \"codex turn complete\");\n // Token usage is delivered via the separate\n // `thread/tokenUsageUpdated` notification — the `turn` payload here\n // does NOT carry a `usage` field per the v2 schema.\n cp.emit(\"turn_complete\", {\n backendSessionId: cp.backendSessionId,\n });\n // Intentionally do NOT clear cp.assistantTextSeen here. A trailing\n // item/completed (channel reorder, codex internal buffering) against\n // an empty accumulator would re-emit the full agentMessage text and\n // bring back the original duplication bug. Unbounded growth from\n // interrupted turns that never fire item/completed is bounded by\n // ASSISTANT_TEXT_SEEN_MAX + FIFO eviction in the delta branch below.\n return;\n }\n if (method === \"turn/started\") {\n log.info({ sessionId: cp.id }, \"codex turn started\");\n return;\n }\n // Dedupe assistant text: codex streams chunks via\n // `item/agentMessage/delta` AND then re-sends the same full text via\n // `item/completed{ item.type: \"agentMessage\" }`. Without dedupe the UI\n // events-reducer (which appends every `text` event to the open message)\n // renders the message twice.\n //\n // Strategy: accumulate per-itemId delta text; on completed-agentMessage\n // emit only the suffix the deltas didn't already cover. delta is treated\n // as an optional streaming hint; completed is the source of truth.\n if (method === \"item/agentMessage/delta\") {\n const p = (params ?? {}) as { itemId?: unknown; delta?: unknown };\n if (typeof p.itemId === \"string\" && typeof p.delta === \"string\") {\n const prev = cp.assistantTextSeen.get(p.itemId) ?? \"\";\n // FIFO eviction guard: cap the map so an interrupted turn that\n // never fires item/completed for an open agentMessage can't leak\n // unbounded entries over a long-lived session. Map iteration is\n // insertion order, so deleting the first key drops the oldest.\n // Only check size when inserting a NEW key — updates of an\n // existing key don't grow the map.\n if (\n !cp.assistantTextSeen.has(p.itemId) &&\n cp.assistantTextSeen.size >= ASSISTANT_TEXT_SEEN_MAX\n ) {\n const oldest = cp.assistantTextSeen.keys().next().value;\n if (oldest !== undefined) cp.assistantTextSeen.delete(oldest);\n }\n cp.assistantTextSeen.set(p.itemId, prev + p.delta);\n } else {\n // Telemetry: surface protocol drift instead of silently degrading\n // to \"no dedupe → full text duplicate\" on the matching completed.\n log.warn(\n { sessionId: cp.id, params: p },\n \"codex agentMessage delta: missing itemId/delta — dedupe will not apply to this item\",\n );\n }\n // Fall through to mapper, which emits {kind:\"assistant_text\", text:delta}.\n } else if (method === \"item/completed\") {\n const p = (params ?? {}) as { item?: { id?: unknown; type?: unknown; text?: unknown } };\n const item = p.item;\n if (item?.type === \"agentMessage\") {\n // Always short-circuit for agentMessage — never fall through to\n // mapper. Mapper has no per-itemId delta history and would re-emit\n // the full text against an empty accumulator, reactivating the\n // duplication bug under protocol drift (e.g. codex changes itemId\n // type).\n if (typeof item.id !== \"string\" || typeof item.text !== \"string\") {\n log.warn(\n { sessionId: cp.id, item },\n \"codex agentMessage completed: malformed item.id/text — dropping to avoid duplicate emission\",\n );\n return;\n }\n const seen = cp.assistantTextSeen.get(item.id) ?? \"\";\n cp.assistantTextSeen.delete(item.id);\n const full = item.text;\n let suffix: string;\n if (full === seen) {\n // Deltas already covered the full text — drop the redundant frame.\n return;\n } else if (seen.length > 0 && full.startsWith(seen)) {\n // Deltas covered a prefix; emit only what's left.\n suffix = full.slice(seen.length);\n } else {\n // No deltas seen (resume / cached / short-circuit) OR the final\n // text diverges from the streamed prefix (codex revised the\n // response). In both cases the full completed text is the source\n // of truth; emitting it preserves correctness even if the UI ends\n // up showing both the partial draft and the final answer.\n suffix = full;\n if (seen.length > 0) {\n log.warn(\n { sessionId: cp.id, itemId: item.id, seenLen: seen.length, fullLen: full.length },\n \"codex agentMessage: completed text diverges from accumulated deltas — emitting full\",\n );\n }\n }\n if (suffix.length === 0) return;\n const out: BackendOutput = { kind: \"assistant_text\", text: suffix };\n this.logBackendOutput(cp.id, method, out);\n cp.emit(\"output\", out);\n return;\n }\n // Non-agentMessage item/completed (commandExecution, fileChange,\n // mcpToolCall, …) — fall through to mapper.\n }\n const out: BackendOutput = mapCodexFrame({ method, params });\n this.logBackendOutput(cp.id, method, out);\n cp.emit(\"output\", out);\n }\n\n /**\n * Selective notification logging. The previous catch-all `log.debug(\"codex\n * notification\")` fired for every streaming `item/agentMessage/delta` chunk\n * (many per turn), drowning the actually useful events. Instead, log only\n * structural events at INFO and surface unrecognised methods as WARN so the\n * `{kind:\"raw\"}` degradation isn't silent.\n */\n private logBackendOutput(sessionId: string, method: string, out: BackendOutput): void {\n // Streaming text deltas — too high-frequency to log per-chunk.\n if (method === \"item/agentMessage/delta\") return;\n // Other token-usage / progress notifications without a structural meaning.\n if (method === \"thread/tokenUsageUpdated\") return;\n\n switch (out.kind) {\n case \"tool_use\":\n log.info(\n { sessionId, callId: out.callId, tool: out.toolName, input: preview(out.input, 120) },\n \"codex tool_use\",\n );\n return;\n case \"tool_result\":\n log.info(\n { sessionId, callId: out.callId, isError: out.isError, output: preview(out.output, 120) },\n \"codex tool_result\",\n );\n return;\n case \"assistant_text\":\n log.debug(\n { sessionId, len: out.text.length, text: preview(out.text, 120) },\n \"codex assistant_text\",\n );\n return;\n case \"raw\":\n log.warn({ sessionId, method, payload: preview(out.payload, 200) }, \"codex unhandled frame\");\n return;\n }\n }\n\n async stop(process: BackendProcess): Promise<void> {\n await process.kill();\n }\n\n send(process: BackendProcess, input: string): void {\n if (!(process instanceof CodexProcess)) {\n log.warn(\n { sessionId: (process as { id?: string })?.id, len: input.length },\n \"codex send: not a CodexProcess — input dropped\",\n );\n return;\n }\n if (!process.rpc) {\n log.warn({ sessionId: process.id, len: input.length }, \"codex send: no rpc — input dropped\");\n return;\n }\n if (!process.backendSessionId) {\n // Handshake still in flight: queue the input and let `handshake()`\n // flush it once `thread/start` resolves. This avoids silent drops when\n // a hub author moves from claude-adapter (no handshake) to codex.\n process.pendingInputs.push(input);\n log.info(\n { sessionId: process.id, queueDepth: process.pendingInputs.length, len: input.length },\n \"codex send: handshake pending, queued\",\n );\n return;\n }\n log.info(\n { sessionId: process.id, threadId: process.backendSessionId, len: input.length },\n \"codex send: dispatching turn/start\",\n );\n this.dispatchTurn(process, input);\n }\n\n private dispatchTurn(process: CodexProcess, input: string): void {\n if (!process.rpc || !process.backendSessionId) return;\n // codex v2: turn/start is a *request* (server returns a result when the\n // turn is queued). We fire-and-forget here because the agent loop reacts\n // to async `turn/completed` / `item/...` notifications, not to the\n // request's result envelope.\n void process.rpc\n .request(\"turn/start\", {\n threadId: process.backendSessionId,\n input: [{ type: \"text\", text: input }],\n })\n .then(() => {\n log.info(\n { sessionId: process.id, threadId: process.backendSessionId },\n \"codex turn/start: accepted by server\",\n );\n })\n .catch((err: Error) => {\n log.warn({ err, sessionId: process.id }, \"turn/start failed\");\n process.emit(\"error\", err);\n });\n }\n\n onOutput(process: BackendProcess, handler: (msg: unknown) => void): void {\n if (process instanceof CodexProcess) process.on(\"output\", handler);\n }\n\n onPermissionRequest(\n process: BackendProcess,\n handler: (req: PermissionRequest) => void,\n ): void {\n if (process instanceof CodexProcess) process.on(\"permission_request\", handler);\n }\n\n async respondToPermission(\n process: BackendProcess,\n decision: PermissionDecision,\n ): Promise<void> {\n if (!(process instanceof CodexProcess)) return;\n const key = process.pendingApprovals.get(decision.callId);\n if (key === undefined) {\n log.warn({ callId: decision.callId }, \"respondToPermission: no pending approval\");\n return;\n }\n const family = process.approvalMethods.get(decision.callId) ?? \"legacy\";\n const resolver = process.approvalResolvers.get(key);\n process.pendingApprovals.delete(decision.callId);\n process.approvalMethods.delete(decision.callId);\n process.approvalResolvers.delete(key);\n const reply = buildApprovalReply(family, decision.decision);\n resolver?.(reply);\n }\n\n onTurnComplete(\n process: BackendProcess,\n handler: (info: { backendSessionId?: string }) => void,\n ): void {\n if (process instanceof CodexProcess) process.on(\"turn_complete\", handler);\n }\n\n onSessionStarted(\n process: BackendProcess,\n handler: (info: { backendSessionId: string }) => void,\n ): void {\n if (process instanceof CodexProcess) process.on(\"session_started\", handler);\n }\n\n onError(process: BackendProcess, handler: (err: Error) => void): void {\n if (process instanceof CodexProcess) process.on(\"error\", handler);\n }\n\n onExit(process: BackendProcess, handler: (code: number | null) => void): void {\n if (process instanceof CodexProcess) process.on(\"exit\", handler);\n }\n\n async getManifest(): Promise<BackendManifestEntry> {\n const det = await this.detect();\n return {\n kind: \"codex\",\n installed: det.installed,\n binaryPath: det.binaryPath,\n version: det.version,\n unavailableReason: det.reason,\n models: det.installed ? CODEX_MODELS : [],\n modes: det.installed ? CODEX_MODES : [],\n };\n }\n}\n","/**\n * codex-spawn-args — construct CLI args for `codex app-server` with\n * per-spawn MCP server + briefing injection.\n *\n * Extracted from CodexAdapter.spawn() so it can be unit-tested without\n * spawning a real child process.\n */\nimport { writeFileSync, unlinkSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { tmpdir } from \"node:os\";\nimport { randomUUID } from \"node:crypto\";\nimport { DEFAULT_SANDBOX, SANDBOX_LEVELS, type SandboxLevel } from \"@mclawnet/shared\";\n\nexport interface McpServerConfig {\n command: string;\n args?: string[];\n env?: Record<string, string>;\n}\n\nexport interface CodexSpawnArgsOptions {\n sessionId: string;\n resumeId?: string;\n systemPrompt?: string;\n mcpServer?: McpServerConfig;\n /**\n * Per-role sandbox level. Maps to codex's `sandbox_mode` config key:\n * - \"read-only\" → `read-only`\n * - \"workspace-write\" → `workspace-write` (default)\n * - \"full-access\" → `danger-full-access` (codex's internal name; we\n * expose the friendlier label upstream)\n * Undefined falls back to \"workspace-write\" so existing callers see no\n * behavior change.\n */\n sandbox?: SandboxLevel;\n /**\n * (N3) Sticky model code from SessionManager. Mapped to codex's\n * `-c model=\"...\"`. Codex passes this directly to OpenAI's API.\n * Omitted = codex CLI uses its own default.\n */\n model?: string;\n /**\n * (N3) Sticky mode code from SessionManager. For codex this is alias for\n * sandbox (catalog uses same axis). Resolution: options.mode wins over\n * options.sandbox only if it's a valid codex sandbox value; otherwise\n * options.sandbox path is used.\n */\n mode?: string;\n}\n\nexport interface CodexSpawnArgsResult {\n args: string[];\n briefingFile?: string;\n /** (N3) Which precedence layer supplied the final sandbox_mode. */\n modeSource?: \"options.mode\" | \"options.sandbox\" | \"default\";\n}\n\nfunction escapeToml(value: string): string {\n return value.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, '\\\\\"');\n}\n\n// N3: module-level Set reusing shared SANDBOX_LEVELS so adding a new\n// sandbox level there auto-propagates here. Allocated once per process,\n// not per buildCodexSpawnArgs call.\nconst VALID_CODEX_SANDBOX: ReadonlySet<SandboxLevel> = new Set(SANDBOX_LEVELS);\n\nexport function buildCodexSpawnArgs(options: CodexSpawnArgsOptions): CodexSpawnArgsResult {\n const args: string[] = [\"app-server\"];\n let briefingFile: string | undefined;\n\n // NOTE: `--resume <id>` is NOT a valid `codex app-server` flag. It only\n // exists on `codex` interactive / `codex exec`. Passing it to app-server\n // makes the CLI exit with \"error: unexpected argument '--resume' found\",\n // and the calling spawn() doesn't notice the immediate exit, so all\n // JSON-RPC traffic hangs. Resume is implemented at the protocol layer\n // by sending `thread/resume { threadId }` from CodexAdapter.handshake()\n // when CodexProcess.resumeId is set. `options.resumeId` is kept on the\n // CodexProcess for the handshake to read; we do NOT add it to args.\n void options.resumeId;\n\n // Non-interactive runtime policy: swarm roles run unattended, so MCP tool\n // calls must not be gated on a user-facing approval prompt. Without\n // `approval_policy=\"never\"`, codex defaults to `on-request` and rejects\n // every MCP tool call with \"user rejected MCP tool call\" because there's\n // no elicitation handler wired on the client side. `workspace-write`\n // matches the role's workDir as the writable root (analogous to claude-\n // adapter's `--permission-mode bypassPermissions` + workspace boundary).\n args.push(\"-c\", 'approval_policy=\"never\"');\n // Map ClawNet's abstract sandbox level to codex's mode name. We expose\n // \"full-access\" to users for readability; codex's internal mode is\n // named \"danger-full-access\". Default (undefined) → DEFAULT_SANDBOX\n // from @mclawnet/shared so existing swarms keep current behavior.\n // N3: Resolve sandbox from options.mode > options.sandbox > DEFAULT_SANDBOX.\n let sandboxLevel: SandboxLevel;\n let modeSource: \"options.mode\" | \"options.sandbox\" | \"default\";\n if (options.mode && VALID_CODEX_SANDBOX.has(options.mode as SandboxLevel)) {\n sandboxLevel = options.mode as SandboxLevel;\n modeSource = \"options.mode\";\n } else if (options.sandbox) {\n sandboxLevel = options.sandbox;\n modeSource = \"options.sandbox\";\n } else {\n sandboxLevel = DEFAULT_SANDBOX;\n modeSource = \"default\";\n }\n const codexSandboxMode =\n sandboxLevel === \"read-only\"\n ? \"read-only\"\n : sandboxLevel === \"full-access\"\n ? \"danger-full-access\"\n : \"workspace-write\";\n args.push(\"-c\", `sandbox_mode=\"${codexSandboxMode}\"`);\n\n // N3: model\n if (options.model) {\n args.push(\"-c\", `model=\"${escapeToml(options.model)}\"`);\n }\n\n // Layer 1: MCP server injection via -c flags\n if (options.mcpServer) {\n const { command, args: serverArgs, env } = options.mcpServer;\n args.push(\"-c\", `mcp_servers.clawnet-mcp.command=\"${escapeToml(command)}\"`);\n if (serverArgs && serverArgs.length > 0) {\n const argsToml = \"[\" + serverArgs.map((a) => `\"${escapeToml(a)}\"`).join(\", \") + \"]\";\n args.push(\"-c\", `mcp_servers.clawnet-mcp.args=${argsToml}`);\n }\n if (env) {\n for (const [key, value] of Object.entries(env)) {\n args.push(\"-c\", `mcp_servers.clawnet-mcp.env.${key}=\"${escapeToml(value)}\"`);\n }\n }\n // CRITICAL: per-server tool approval mode. Without this codex still\n // elicits each MCP tool call (even with global approval_policy=\"never\"\n // which only covers shell exec). The elicitation has no client-side\n // handler, so every MCP tool call returns \"user rejected MCP tool call\"\n // in 0s. \"approve\" silently approves all tool calls — required for\n // unattended swarm-role execution. (Valid values are \"prompt\" and\n // \"approve\"; \"auto\" is NOT accepted by codex 0.134.)\n args.push(\"-c\", 'mcp_servers.clawnet-mcp.default_tools_approval_mode=\"approve\"');\n }\n\n // Layer 2: Briefing injection via temp file + model_instructions_file\n if (options.systemPrompt) {\n // Sanitize sessionId for use in a filename: swarm role sessionIds look like\n // \"<swarmId>::<instanceId>\" (e.g. \"abc123::dev-0\"). Windows forbids `:`\n // (and `\\\\/*?\"<>|`, control chars 0x00-0x1F, plus trailing `.`/space) in\n // filenames, so writeFileSync blows up with ENOENT and the entire codex\n // role fails to spawn. Replace all illegal chars with `_` — the file is\n // throwaway, only the .md suffix matters.\n const safeId = options.sessionId\n // eslint-disable-next-line no-control-regex\n .replace(/[\\\\/:*?\"<>|\\x00-\\x1F]/g, \"_\")\n .replace(/[. ]+$/g, \"_\");\n briefingFile = join(tmpdir(), `clawnet-briefing-${safeId}-${randomUUID().slice(0, 8)}.md`);\n writeFileSync(briefingFile, options.systemPrompt, { encoding: \"utf-8\", mode: 0o600 });\n args.push(\"-c\", `model_instructions_file=\"${escapeToml(briefingFile)}\"`);\n }\n\n return { args, briefingFile, modeSource };\n}\n\nexport function cleanupBriefingFile(filePath: string): void {\n try {\n unlinkSync(filePath);\n } catch {\n // ignore — file may already be gone\n }\n}\n","import type { Writable, Readable } from \"node:stream\";\n\nexport interface JsonRpcClientOptions {\n stdin: Writable;\n stdout: Readable;\n onRequest?: (method: string, params: unknown) => Promise<unknown> | unknown;\n onNotification?: (method: string, params: unknown) => void;\n onMalformedLine?: (line: string, err: Error) => void;\n}\n\ninterface JsonRpcPending {\n resolve: (v: unknown) => void;\n reject: (e: Error) => void;\n}\n\n/**\n * Minimal JSON-RPC 2.0 client over NDJSON stdio.\n *\n * Half of the duplex: client-initiated `request()` returns a Promise tied to\n * the response by id. Server-initiated requests (codex approval callbacks)\n * are dispatched to `onRequest` and the resolved value is written back as a\n * `result` envelope.\n */\nexport class JsonRpcClient {\n private stdin: Writable;\n private buffer = \"\";\n private nextId = 1;\n private pending = new Map<number, JsonRpcPending>();\n private onRequest?: JsonRpcClientOptions[\"onRequest\"];\n private onNotification?: JsonRpcClientOptions[\"onNotification\"];\n private onMalformedLine?: JsonRpcClientOptions[\"onMalformedLine\"];\n\n constructor(opts: JsonRpcClientOptions) {\n this.stdin = opts.stdin;\n this.onRequest = opts.onRequest;\n this.onNotification = opts.onNotification;\n this.onMalformedLine = opts.onMalformedLine;\n opts.stdout.on(\"data\", (chunk: Buffer) => this.feed(chunk.toString(\"utf-8\")));\n // Without this listener, an EPIPE on stdin (codex dies mid-handshake,\n // user closes the pipe, OS sends SIGPIPE) bubbles as an unhandled\n // 'error' event and crashes the host process. We surface it as a\n // structured rejection on every in-flight request instead.\n this.stdin.on(\"error\", (err) => this.failAllPending(err));\n }\n\n /**\n * Reject every in-flight request with the given error. Called when stdin\n * dies so callers get a clean rejection instead of hanging until the\n * handshake death-poll catches up.\n */\n private failAllPending(err: Error): void {\n for (const [, p] of this.pending) {\n try { p.reject(err); } catch { /* ignore listener throw */ }\n }\n this.pending.clear();\n }\n\n request(method: string, params: unknown): Promise<unknown> {\n const id = this.nextId++;\n const frame = { jsonrpc: \"2.0\", id, method, params };\n return new Promise((resolve, reject) => {\n this.pending.set(id, { resolve, reject });\n // Pass an error callback so a write failure (EPIPE on dead child)\n // rejects this specific request instead of triggering an unhandled\n // 'error' on the stream. The stdin 'error' handler above is the\n // backstop for failures that arrive out-of-band of any write.\n this.stdin.write(JSON.stringify(frame) + \"\\n\", (err) => {\n if (err) {\n this.pending.delete(id);\n reject(err);\n }\n });\n });\n }\n\n notify(method: string, params: unknown): void {\n // Same swallow-rather-than-crash policy for notifications. There's no\n // pending promise to reject; the caller fired-and-forgot.\n this.stdin.write(JSON.stringify({ jsonrpc: \"2.0\", method, params }) + \"\\n\", () => {\n /* errors absorbed by the 'error' handler in the constructor */\n });\n }\n\n private feed(s: string) {\n this.buffer += s;\n let nl: number;\n while ((nl = this.buffer.indexOf(\"\\n\")) >= 0) {\n const line = this.buffer.slice(0, nl).trim();\n this.buffer = this.buffer.slice(nl + 1);\n if (!line) continue;\n this.dispatch(line);\n }\n }\n\n private dispatch(line: string) {\n let msg: {\n id?: number;\n method?: string;\n params?: unknown;\n result?: unknown;\n error?: { code: number; message: string };\n };\n try {\n msg = JSON.parse(line);\n } catch (err) {\n this.onMalformedLine?.(line, err as Error);\n return;\n }\n if (typeof msg.id === \"number\" && (msg.result !== undefined || msg.error !== undefined)) {\n const pending = this.pending.get(msg.id);\n if (!pending) return;\n this.pending.delete(msg.id);\n if (msg.error) pending.reject(new Error(msg.error.message));\n else pending.resolve(msg.result);\n return;\n }\n if (msg.method && typeof msg.id === \"number\") {\n // server-initiated request\n const id = msg.id;\n const method = msg.method;\n const params = msg.params;\n void Promise.resolve(this.onRequest?.(method, params))\n .then((result) => {\n this.stdin.write(JSON.stringify({ jsonrpc: \"2.0\", id, result }) + \"\\n\");\n })\n .catch((err: Error) => {\n this.stdin.write(\n JSON.stringify({\n jsonrpc: \"2.0\",\n id,\n error: { code: -32000, message: err.message },\n }) + \"\\n\",\n );\n });\n return;\n }\n if (msg.method) {\n this.onNotification?.(msg.method, msg.params);\n }\n }\n}\n","import type { PermissionDecision, PermissionRequest } from \"@mclawnet/backend-types\";\n\n/**\n * Three coexisting decision-wire families codex app-server speaks:\n * - \"legacy\" → ReviewDecision (approved/approved_for_session/denied/abort)\n * - \"v2\" → CommandExecutionApprovalDecision / FileChangeApprovalDecision\n * (accept/acceptForSession/decline/cancel)\n * - \"mcp\" → McpServerElicitationAction\n * ({ action: \"accept\" | \"decline\" | \"cancel\" })\n *\n * `parseApprovalRequest` returns the family so `respondToPermission` can pick\n * the right wire without re-deriving it from a brittle method-name prefix.\n */\nexport type ApprovalWireFamily = \"legacy\" | \"v2\" | \"mcp\";\n\nexport type ReviewDecisionWire =\n | \"approved\"\n | \"approved_for_session\"\n | \"denied\"\n | \"abort\";\n\nexport function decisionToWire(d: PermissionDecision[\"decision\"]): ReviewDecisionWire {\n switch (d) {\n case \"allow\":\n return \"approved\";\n case \"allow_session\":\n return \"approved_for_session\";\n case \"deny\":\n return \"denied\";\n case \"abort\":\n return \"abort\";\n default: {\n const _exhaustive: never = d;\n return _exhaustive;\n }\n }\n}\n\n/**\n * v2 CommandExecutionApprovalDecision / FileChangeApprovalDecision wire enum.\n * Used by codex app-server >= 0.134 for `item/.../requestApproval` flows.\n */\nexport type V2DecisionWire = \"accept\" | \"acceptForSession\" | \"decline\" | \"cancel\";\n\nexport function decisionToV2Wire(d: PermissionDecision[\"decision\"]): V2DecisionWire {\n switch (d) {\n case \"allow\":\n return \"accept\";\n case \"allow_session\":\n return \"acceptForSession\";\n case \"deny\":\n return \"decline\";\n case \"abort\":\n return \"cancel\";\n default: {\n const _exhaustive: never = d;\n return _exhaustive;\n }\n }\n}\n\n/**\n * MCP elicitation action enum (RMCP CreateElicitationResult). Note: \"session\"\n * scope isn't part of the MCP spec — `allow_session` degrades to \"accept\".\n */\nexport type McpElicitationAction = \"accept\" | \"decline\" | \"cancel\";\n\nexport function decisionToMcpAction(d: PermissionDecision[\"decision\"]): McpElicitationAction {\n switch (d) {\n case \"allow\":\n case \"allow_session\":\n return \"accept\";\n case \"deny\":\n return \"decline\";\n case \"abort\":\n return \"cancel\";\n default: {\n const _exhaustive: never = d;\n return _exhaustive;\n }\n }\n}\n\n/** @deprecated kept only for back-compat with M3.S5 test seam; prefer the\n * wire family returned by `parseApprovalRequest`. */\nexport function isV2ApprovalMethod(method: string): boolean {\n return method.startsWith(\"item/\");\n}\n\n/**\n * Build the wire-shaped reply body for a given decision + family.\n * - legacy → `{ decision: \"approved\" | ... }`\n * - v2 → `{ decision: \"accept\" | ... }`\n * - mcp → `{ action: \"accept\" | ... }`\n */\nexport function buildApprovalReply(\n family: ApprovalWireFamily,\n decision: PermissionDecision[\"decision\"],\n): Record<string, string> {\n switch (family) {\n case \"legacy\":\n return { decision: decisionToWire(decision) };\n case \"v2\":\n return { decision: decisionToV2Wire(decision) };\n case \"mcp\":\n return { action: decisionToMcpAction(decision) };\n default: {\n const _exhaustive: never = family;\n return _exhaustive;\n }\n }\n}\n\nexport interface ParsedApprovalRequest {\n req: PermissionRequest;\n wireFamily: ApprovalWireFamily;\n}\n\n/**\n * Map a codex JSON-RPC approval request to our backend-neutral\n * PermissionRequest. Covers both legacy (`execCommandApproval`,\n * `applyPatchApproval`) and v2 (`item/commandExecution/requestApproval`,\n * `item/fileChange/requestApproval`) shapes, plus MCP elicitation.\n *\n * Returns null when the method is not a recognized approval channel — the\n * adapter forwards such frames as raw output instead.\n */\nexport function parseApprovalRequest(\n method: string,\n params: unknown,\n): ParsedApprovalRequest | null {\n const p = (params ?? {}) as Record<string, unknown>;\n const callId = (p.callId as string | undefined) ?? (p.itemId as string | undefined);\n if (!callId) return null;\n const reason = p.reason as string | undefined;\n const cwd = p.cwd as string | undefined;\n\n if (method === \"execCommandApproval\") {\n return {\n req: {\n callId,\n toolName: \"shell\",\n input: { command: p.command, cwd },\n meta: { backend: \"codex\", reason, cwd },\n },\n wireFamily: \"legacy\",\n };\n }\n if (method === \"item/commandExecution/requestApproval\") {\n return {\n req: {\n callId,\n toolName: \"shell\",\n input: { command: p.command, cwd },\n meta: { backend: \"codex\", reason, cwd },\n },\n wireFamily: \"v2\",\n };\n }\n if (method === \"applyPatchApproval\") {\n return {\n req: {\n callId,\n toolName: \"apply_patch\",\n input: { fileChanges: p.fileChanges },\n meta: { backend: \"codex\", reason, cwd },\n },\n wireFamily: \"legacy\",\n };\n }\n if (method === \"item/fileChange/requestApproval\") {\n return {\n req: {\n callId,\n toolName: \"apply_patch\",\n input: { fileChanges: p.fileChanges },\n meta: { backend: \"codex\", reason, cwd },\n },\n wireFamily: \"v2\",\n };\n }\n if (method === \"mcpServer/elicitation/request\") {\n return {\n req: {\n callId,\n toolName: (p.serverName as string | undefined) ?? \"mcp\",\n input: { message: p.message, meta: p._meta },\n meta: { backend: \"codex\", reason },\n },\n wireFamily: \"mcp\",\n };\n }\n return null;\n}\n","import type { BackendOutput } from \"@mclawnet/backend-types\";\n\n/**\n * Map a single codex JSON-RPC notification frame to a backend-neutral\n * BackendOutput envelope. Unknown methods degrade to `{ kind: \"raw\" }` so\n * the agent main loop can still see them (e.g. for debug logging) without\n * the adapter pretending to understand every codex frame.\n *\n * Covers both v2 (`item/agentMessage/delta`, `item/started`, `item/completed`)\n * and legacy (`thread/message`, `thread/toolUse`) frame names.\n */\nexport function mapCodexFrame(frame: {\n method: string;\n params?: unknown;\n}): BackendOutput {\n const params = (frame.params ?? {}) as Record<string, unknown>;\n switch (frame.method) {\n // ── v2 frames (real codex app-server v2 protocol) ─────────────────\n case \"item/agentMessage/delta\": {\n const delta = params.delta;\n if (typeof delta === \"string\") {\n return { kind: \"assistant_text\", text: delta };\n }\n break;\n }\n case \"item/completed\": {\n const item = params.item as\n | {\n type?: string;\n text?: string;\n id?: string;\n // commandExecution\n command?: string;\n aggregatedOutput?: string | null;\n exitCode?: number | null;\n status?: string;\n // fileChange\n changes?: unknown;\n // mcpToolCall\n server?: string;\n tool?: string;\n arguments?: unknown;\n result?: unknown;\n error?: unknown;\n }\n | undefined;\n // NOTE: `item.type === \"agentMessage\"` is intentionally NOT handled\n // here. It's intercepted earlier in `CodexAdapter.handleNotification`,\n // which needs per-itemId delta state to avoid double-emitting (deltas\n // already streamed the same text). Re-adding a branch here would\n // bypass that dedupe and reintroduce the \"Hello worldHello world\" bug.\n if (item?.type === \"commandExecution\") {\n // CommandExecutionStatus = \"inProgress\" | \"completed\" | \"failed\" | \"declined\"\n const failed = item.status === \"failed\" || item.status === \"declined\";\n const badExit = typeof item.exitCode === \"number\" && item.exitCode !== 0;\n return {\n kind: \"tool_result\",\n callId: String(item.id ?? \"\"),\n output: item.aggregatedOutput ?? null,\n isError: failed || badExit,\n };\n }\n if (item?.type === \"fileChange\") {\n // PatchApplyStatus shares the CommandExecutionStatus enum shape.\n const failed = item.status === \"failed\" || item.status === \"declined\";\n return {\n kind: \"tool_result\",\n callId: String(item.id ?? \"\"),\n output: { changes: item.changes },\n isError: failed,\n };\n }\n if (item?.type === \"mcpToolCall\") {\n // McpToolCallStatus = \"inProgress\" | \"completed\" | \"failed\"\n const failed = item.status === \"failed\" || item.error != null;\n return {\n kind: \"tool_result\",\n callId: String(item.id ?? \"\"),\n output: failed ? item.error : item.result,\n isError: failed,\n };\n }\n break;\n }\n case \"item/started\": {\n const item = params.item as\n | { type?: string; id?: string; command?: string; name?: string; input?: unknown }\n | undefined;\n if (item?.type === \"commandExecution\") {\n return {\n kind: \"tool_use\",\n callId: String(item.id ?? \"\"),\n toolName: \"shell\",\n input: { command: item.command },\n };\n }\n if (item?.type === \"mcpToolCall\") {\n return {\n kind: \"tool_use\",\n callId: String(item.id ?? \"\"),\n toolName: String(item.name ?? \"mcp\"),\n input: item.input,\n };\n }\n break;\n }\n\n // ── legacy frame names (kept for back-compat with older codex / tests) ──\n case \"thread/message\": {\n const role = params.role as string | undefined;\n const content = params.content as Array<{ type: string; text?: string }> | undefined;\n if (role === \"assistant\" && content) {\n const text = content\n .filter((c) => c.type === \"text\" && typeof c.text === \"string\")\n .map((c) => c.text!)\n .join(\"\");\n return { kind: \"assistant_text\", text };\n }\n break;\n }\n case \"thread/toolUse\":\n return {\n kind: \"tool_use\",\n callId: String(params.callId ?? \"\"),\n toolName: String(params.name ?? \"\"),\n input: params.input,\n };\n case \"thread/toolResult\":\n return {\n kind: \"tool_result\",\n callId: String(params.callId ?? \"\"),\n output: params.output,\n isError: Boolean(params.isError),\n };\n }\n return { kind: \"raw\", backend: \"codex\", payload: frame };\n}\n","import type { ModelDescriptor, ModeDescriptor } from \"@mclawnet/shared\";\n\n// PROVISIONAL — not verified against `codex --help` on a real install.\n// Update once we have a way to query codex's accepted model list at runtime.\n// `code` values must be ones OpenAI's API accepts (codex CLI passes via -c model=\"...\").\nexport const CODEX_MODELS: ModelDescriptor[] = [\n {\n code: \"gpt-4o\",\n label: \"GPT-4o\",\n tier: \"premium\",\n contextWindow: 128_000,\n supportsVision: true,\n supportsToolUse: true,\n default: true,\n },\n {\n code: \"gpt-4o-mini\",\n label: \"GPT-4o mini\",\n tier: \"standard\",\n contextWindow: 128_000,\n supportsVision: true,\n supportsToolUse: true,\n },\n];\n\n// codex 的 mode = sandbox_mode\nexport const CODEX_MODES: ModeDescriptor[] = [\n { code: \"read-only\", label: \"Read Only\", description: \"No FS writes\" },\n { code: \"workspace-write\", label: \"Workspace Write\", description: \"Write inside cwd only\", default: true },\n { code: \"full-access\", label: \"Full Access\", description: \"No sandbox\" },\n];\n","import { execFile } from \"node:child_process\";\nimport { promisify } from \"node:util\";\nimport { platform } from \"node:os\";\n\nconst exec = promisify(execFile);\n\n// TODO: extract `detectCliBinary(name)` into a shared backend-utils package\n// (NOT into @mclawnet/agent — that would re-introduce the build-time cycle\n// the @mclawnet/backend-types extraction was created to break).\n// Removes ~90% duplication between this file and the sibling adapter.\n\nexport interface DetectResult {\n installed: boolean;\n binaryPath?: string;\n version?: string;\n reason?: string;\n}\n\n/**\n * Detect a codex CLI install on this machine. Must never throw — all\n * errors (missing binary, EACCES, timeout, hung --version) are absorbed\n * and surfaced via `reason` with `installed: false`.\n *\n * NOTE: Keep the entire body inside the single try block. Any code path\n * added outside the try would break the no-throw contract that\n * collectAgentManifest() relies on.\n */\nexport async function detectCodexInstall(): Promise<DetectResult> {\n try {\n const isWin = platform() === \"win32\";\n const lookup = isWin ? \"where\" : \"which\";\n const { stdout: lookupOut } = await exec(lookup, [\"codex\"], { timeout: 5000 });\n // `where` / `which` may return multiple shims (homebrew + nvm-global, etc.) — take first line.\n const binaryPath = lookupOut.trim().split(/\\r?\\n/)[0]?.trim();\n if (!binaryPath) {\n return { installed: false, reason: `codex CLI not detected: empty ${lookup} output` };\n }\n const { stdout: verOut } = await exec(binaryPath, [\"--version\"], { timeout: 5000 });\n return { installed: true, binaryPath, version: verOut.trim() };\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n return { installed: false, reason: `codex CLI not detected: ${message}` };\n }\n}\n"],"mappings":";AAAA,SAAS,UAAU,aAAgC;AACnD,SAAS,oBAAoB;AAC7B,SAAS,YAAY,oBAAoB;AACzC,SAAS,qBAAqB;AAC9B,SAAS,SAAS,YAAAA,iBAAgB;AAClC,SAAS,SAAS,QAAAC,aAAY;;;ACE9B,SAAS,eAAe,kBAAkB;AAC1C,SAAS,YAAY;AACrB,SAAS,cAAc;AACvB,SAAS,kBAAkB;AAC3B,SAAS,iBAAiB,sBAAyC;AA6CnE,SAAS,WAAW,OAAa;AAC/B,SAAO,MAAM,QAAQ,OAAO,MAAM,EAAE,QAAQ,MAAM,KAAK;AACzD;AAKA,IAAM,sBAAiD,IAAI,IAAI,cAAc;AAEvE,SAAU,oBAAoB,SAA8B;AAChE,QAAM,OAAiB,CAAC,YAAY;AACpC,MAAI;AAUJ,OAAK,QAAQ;AASb,OAAK,KAAK,MAAM,yBAAyB;AAMzC,MAAI;AACJ,MAAI;AACJ,MAAI,QAAQ,QAAQ,oBAAoB,IAAI,QAAQ,IAAoB,GAAG;AACzE,mBAAe,QAAQ;AACvB,iBAAa;EACf,WAAW,QAAQ,SAAS;AAC1B,mBAAe,QAAQ;AACvB,iBAAa;EACf,OAAO;AACL,mBAAe;AACf,iBAAa;EACf;AACA,QAAM,mBACJ,iBAAiB,cACb,cACA,iBAAiB,gBACf,uBACA;AACR,OAAK,KAAK,MAAM,iBAAiB,gBAAgB,GAAG;AAGp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mBAAmB,QAAQ,SAAS,QAAQ;AAC1D,eAAW,KAAK;EAClB;EAEA,eACEA,UACA,SAAsD;AAEtD,QAAIA,oBAAmB;AAAc,MAAAA,SAAQ,GAAG,iBAAiB,OAAO;EAC1E;EAEA,iBACEA,UACA,SAAqD;AAErD,QAAIA,oBAAmB;AAAc,MAAAA,SAAQ,GAAG,mBAAmB,OAAO;EAC5E;EAEA,QAAQA,UAAyB,SAA6B;AAC5D,QAAIA,oBAAmB;AAAc,MAAAA,SAAQ,GAAG,SAAS,OAAO;EAClE;EAEA,OAAOA,UAAyB,SAAsC;AACpE,QAAIA,oBAAmB;AAAc,MAAAA,SAAQ,GAAG,QAAQ,OAAO;EACjE;EAEA,MAAM,cAAW;AACf,UAAM,MAAM,MAAM,KAAK,OAAM;AAC7B,WAAO;MACL,MAAM;MACN,WAAW,IAAI;MACf,YAAY,IAAI;MAChB,SAAS,IAAI;MACb,mBAAmB,IAAI;MACvB,QAAQ,IAAI,YAAY,eAAe,CAAA;MACvC,OAAO,IAAI,YAAY,cAAc,CAAA;;EAEzC;;","names":["platform","join","isWin","platform","join","platform","join","cp","require","out","process"]}
package/dist/errors.d.ts CHANGED
@@ -37,4 +37,24 @@ export declare class SessionLimitReachedError extends Error {
37
37
  readonly code = "SESSION_LIMIT_REACHED";
38
38
  constructor(details: SessionPoolSnapshot);
39
39
  }
40
+ /**
41
+ * (N3) Thrown by SessionManager.sendUserInput when the pending per-turn
42
+ * model override differs from the model the current child process was
43
+ * spawned with. The caller (hub-connection.ts session.input handler) is
44
+ * expected to catch this, emit session.died, then re-spawn via
45
+ * session.create with --resume.
46
+ */
47
+ export declare class RespawnRequiredError extends Error {
48
+ readonly code: "RESPAWN_REQUIRED";
49
+ readonly sessionId: string;
50
+ readonly newModel: string;
51
+ readonly currentModel: string | undefined;
52
+ readonly backendSessionId: string | undefined;
53
+ constructor(args: {
54
+ sessionId: string;
55
+ newModel: string;
56
+ currentModel: string | undefined;
57
+ backendSessionId: string | undefined;
58
+ });
59
+ }
40
60
  //# sourceMappingURL=errors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,YAAY,CAAC;AAEhD;;;;;;;;GAQG;AACH,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IACpC,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,cAAc,EAAE,WAAW,GAAG,IAAI,CAAC;CACpC;AAED;;;;;;GAMG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;aAGrB,OAAO,EAAE,mBAAmB;IAFxD,QAAQ,CAAC,IAAI,2BAA2B;gBAEZ,OAAO,EAAE,mBAAmB;CAIzD"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,YAAY,CAAC;AAEhD;;;;;;;;GAQG;AACH,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IACpC,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,cAAc,EAAE,WAAW,GAAG,IAAI,CAAC;CACpC;AAED;;;;;;GAMG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;aAGrB,OAAO,EAAE,mBAAmB;IAFxD,QAAQ,CAAC,IAAI,2BAA2B;gBAEZ,OAAO,EAAE,m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