@useorgx/openclaw-plugin 0.4.8 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (284) hide show
  1. package/README.md +35 -0
  2. package/dashboard/dist/assets/BJgZIVUQ.js +53 -0
  3. package/dashboard/dist/assets/BJgZIVUQ.js.br +0 -0
  4. package/dashboard/dist/assets/BJgZIVUQ.js.gz +0 -0
  5. package/dashboard/dist/assets/BXWDRGm-.js +1 -0
  6. package/dashboard/dist/assets/BXWDRGm-.js.br +0 -0
  7. package/dashboard/dist/assets/BXWDRGm-.js.gz +0 -0
  8. package/dashboard/dist/assets/BgOYB78t.js +4 -0
  9. package/dashboard/dist/assets/BgOYB78t.js.br +0 -0
  10. package/dashboard/dist/assets/BgOYB78t.js.gz +0 -0
  11. package/dashboard/dist/assets/C-KIc3Wc.js.br +0 -0
  12. package/dashboard/dist/assets/C-KIc3Wc.js.gz +0 -0
  13. package/dashboard/dist/assets/CE38zU4U.js +1 -0
  14. package/dashboard/dist/assets/CE38zU4U.js.br +0 -0
  15. package/dashboard/dist/assets/CE38zU4U.js.gz +0 -0
  16. package/dashboard/dist/assets/CFGKRAzG.js +1 -0
  17. package/dashboard/dist/assets/CFGKRAzG.js.br +0 -0
  18. package/dashboard/dist/assets/CFGKRAzG.js.gz +0 -0
  19. package/dashboard/dist/assets/CGGR2GZh.js +1 -0
  20. package/dashboard/dist/assets/CGGR2GZh.js.br +0 -0
  21. package/dashboard/dist/assets/CGGR2GZh.js.gz +0 -0
  22. package/dashboard/dist/assets/CL_wXqR7.js +1 -0
  23. package/dashboard/dist/assets/CL_wXqR7.js.br +0 -0
  24. package/dashboard/dist/assets/CL_wXqR7.js.gz +0 -0
  25. package/dashboard/dist/assets/CPFiTmlw.js +8 -0
  26. package/dashboard/dist/assets/CPFiTmlw.js.br +0 -0
  27. package/dashboard/dist/assets/CPFiTmlw.js.gz +0 -0
  28. package/dashboard/dist/assets/CZZTvkQZ.js +1 -0
  29. package/dashboard/dist/assets/CZZTvkQZ.js.br +0 -0
  30. package/dashboard/dist/assets/CZZTvkQZ.js.gz +0 -0
  31. package/dashboard/dist/assets/{CpJsfbXo.js → CxQ08qFN.js} +2 -2
  32. package/dashboard/dist/assets/CxQ08qFN.js.br +0 -0
  33. package/dashboard/dist/assets/CxQ08qFN.js.gz +0 -0
  34. package/dashboard/dist/assets/D-bf6hEI.js +213 -0
  35. package/dashboard/dist/assets/D-bf6hEI.js.br +0 -0
  36. package/dashboard/dist/assets/D-bf6hEI.js.gz +0 -0
  37. package/dashboard/dist/assets/DG6y9wJI.js +2 -0
  38. package/dashboard/dist/assets/DG6y9wJI.js.br +0 -0
  39. package/dashboard/dist/assets/DG6y9wJI.js.gz +0 -0
  40. package/dashboard/dist/assets/DNxKz-GV.js +1 -0
  41. package/dashboard/dist/assets/DNxKz-GV.js.br +0 -0
  42. package/dashboard/dist/assets/DNxKz-GV.js.gz +0 -0
  43. package/dashboard/dist/assets/DW_rKUic.js +11 -0
  44. package/dashboard/dist/assets/DW_rKUic.js.br +0 -0
  45. package/dashboard/dist/assets/DW_rKUic.js.gz +0 -0
  46. package/dashboard/dist/assets/DbNoijHm.js +1 -0
  47. package/dashboard/dist/assets/DbNoijHm.js.br +0 -0
  48. package/dashboard/dist/assets/DbNoijHm.js.gz +0 -0
  49. package/dashboard/dist/assets/DjcdE6jC.js +2 -0
  50. package/dashboard/dist/assets/DjcdE6jC.js.br +0 -0
  51. package/dashboard/dist/assets/DjcdE6jC.js.gz +0 -0
  52. package/dashboard/dist/assets/FZYuCDnt.js +1 -0
  53. package/dashboard/dist/assets/FZYuCDnt.js.br +0 -0
  54. package/dashboard/dist/assets/FZYuCDnt.js.gz +0 -0
  55. package/dashboard/dist/assets/PAUiij_z.js +1 -0
  56. package/dashboard/dist/assets/PAUiij_z.js.br +0 -0
  57. package/dashboard/dist/assets/PAUiij_z.js.gz +0 -0
  58. package/dashboard/dist/assets/cNrhgGc1.js +8 -0
  59. package/dashboard/dist/assets/cNrhgGc1.js.br +0 -0
  60. package/dashboard/dist/assets/cNrhgGc1.js.gz +0 -0
  61. package/dashboard/dist/assets/h5biQs2I.css +1 -0
  62. package/dashboard/dist/assets/h5biQs2I.css.br +0 -0
  63. package/dashboard/dist/assets/h5biQs2I.css.gz +0 -0
  64. package/dashboard/dist/assets/ic2FaMnh.js +1 -0
  65. package/dashboard/dist/assets/ic2FaMnh.js.br +0 -0
  66. package/dashboard/dist/assets/ic2FaMnh.js.gz +0 -0
  67. package/dashboard/dist/assets/nByHNHoW.js +1 -0
  68. package/dashboard/dist/assets/nByHNHoW.js.br +0 -0
  69. package/dashboard/dist/assets/nByHNHoW.js.gz +0 -0
  70. package/dashboard/dist/assets/qm8xLgv-.css +1 -0
  71. package/dashboard/dist/assets/qm8xLgv-.css.br +0 -0
  72. package/dashboard/dist/assets/qm8xLgv-.css.gz +0 -0
  73. package/dashboard/dist/assets/tS9mbYZi.js +1 -0
  74. package/dashboard/dist/assets/tS9mbYZi.js.br +0 -0
  75. package/dashboard/dist/assets/tS9mbYZi.js.gz +0 -0
  76. package/dashboard/dist/brand/anthropic-mark.svg.br +0 -0
  77. package/dashboard/dist/brand/anthropic-mark.svg.gz +0 -0
  78. package/dashboard/dist/brand/openai-mark.svg.br +0 -0
  79. package/dashboard/dist/brand/openai-mark.svg.gz +0 -0
  80. package/dashboard/dist/brand/openclaw-mark.svg.br +0 -0
  81. package/dashboard/dist/brand/openclaw-mark.svg.gz +0 -0
  82. package/dashboard/dist/brand/xandy-orchestrator.png +0 -0
  83. package/dashboard/dist/index.html +7 -5
  84. package/dashboard/dist/index.html.br +0 -0
  85. package/dashboard/dist/index.html.gz +0 -0
  86. package/dist/activity-actor-fields.js +26 -4
  87. package/dist/activity-store.js +38 -26
  88. package/dist/agent-context-store.js +84 -42
  89. package/dist/agent-run-store.js +49 -28
  90. package/dist/agent-suite.d.ts +9 -0
  91. package/dist/agent-suite.js +150 -17
  92. package/dist/artifacts/artifact-domain-schemas.d.ts +66 -0
  93. package/dist/artifacts/artifact-domain-schemas.js +357 -0
  94. package/dist/artifacts/register-artifact.d.ts +4 -3
  95. package/dist/artifacts/register-artifact.js +170 -57
  96. package/dist/auth/flows.d.ts +47 -0
  97. package/dist/auth/flows.js +169 -0
  98. package/dist/auth-store.js +6 -26
  99. package/dist/byok-store.js +5 -19
  100. package/dist/chat-store.d.ts +157 -0
  101. package/dist/chat-store.js +586 -0
  102. package/dist/cli/orgx.d.ts +66 -0
  103. package/dist/cli/orgx.js +102 -0
  104. package/dist/config/refresh.d.ts +32 -0
  105. package/dist/config/refresh.js +55 -0
  106. package/dist/config/resolution.d.ts +37 -0
  107. package/dist/config/resolution.js +178 -0
  108. package/dist/contracts/client.d.ts +43 -3
  109. package/dist/contracts/client.js +159 -30
  110. package/dist/contracts/retro-schema.d.ts +81 -0
  111. package/dist/contracts/retro-schema.js +80 -0
  112. package/dist/contracts/shared-types.d.ts +306 -0
  113. package/dist/contracts/shared-types.js +179 -0
  114. package/dist/contracts/skill-pack-schema.d.ts +192 -0
  115. package/dist/contracts/skill-pack-schema.js +180 -0
  116. package/dist/contracts/types.d.ts +224 -132
  117. package/dist/contracts/types.js +5 -0
  118. package/dist/entities/auto-assignment.d.ts +36 -0
  119. package/dist/entities/auto-assignment.js +141 -0
  120. package/dist/entity-comment-store.js +5 -25
  121. package/dist/event-sanitization.d.ts +11 -0
  122. package/dist/event-sanitization.js +113 -0
  123. package/dist/fs-utils.js +13 -1
  124. package/dist/gateway-watchdog.d.ts +5 -0
  125. package/dist/gateway-watchdog.js +50 -0
  126. package/dist/hash-utils.d.ts +2 -0
  127. package/dist/hash-utils.js +12 -0
  128. package/dist/hooks/post-reporting-event.mjs +1 -5
  129. package/dist/http/helpers/activity-headline.d.ts +10 -0
  130. package/dist/http/helpers/activity-headline.js +73 -0
  131. package/dist/http/helpers/artifact-fallback.d.ts +13 -0
  132. package/dist/http/helpers/artifact-fallback.js +148 -0
  133. package/dist/http/helpers/auto-continue-engine.d.ts +486 -0
  134. package/dist/http/helpers/auto-continue-engine.js +3563 -0
  135. package/dist/http/helpers/autopilot-operations.d.ts +176 -0
  136. package/dist/http/helpers/autopilot-operations.js +554 -0
  137. package/dist/http/helpers/autopilot-runtime.d.ts +43 -0
  138. package/dist/http/helpers/autopilot-runtime.js +607 -0
  139. package/dist/http/helpers/autopilot-slice-utils.d.ts +56 -0
  140. package/dist/http/helpers/autopilot-slice-utils.js +899 -0
  141. package/dist/http/helpers/decision-mapper.d.ts +52 -0
  142. package/dist/http/helpers/decision-mapper.js +260 -0
  143. package/dist/http/helpers/dispatch-lifecycle.d.ts +119 -0
  144. package/dist/http/helpers/dispatch-lifecycle.js +809 -0
  145. package/dist/http/helpers/hash-utils.d.ts +1 -0
  146. package/dist/http/helpers/hash-utils.js +1 -0
  147. package/dist/http/helpers/kickoff-context.d.ts +12 -0
  148. package/dist/http/helpers/kickoff-context.js +228 -0
  149. package/dist/http/helpers/llm-client.d.ts +47 -0
  150. package/dist/http/helpers/llm-client.js +256 -0
  151. package/dist/http/helpers/mission-control.d.ts +193 -0
  152. package/dist/http/helpers/mission-control.js +1383 -0
  153. package/dist/http/helpers/openclaw-cli.d.ts +37 -0
  154. package/dist/http/helpers/openclaw-cli.js +283 -0
  155. package/dist/http/helpers/runtime-sse.d.ts +20 -0
  156. package/dist/http/helpers/runtime-sse.js +110 -0
  157. package/dist/http/helpers/sentinel-catalog.d.ts +23 -0
  158. package/dist/http/helpers/sentinel-catalog.js +193 -0
  159. package/dist/http/helpers/session-classification.d.ts +9 -0
  160. package/dist/http/helpers/session-classification.js +564 -0
  161. package/dist/http/helpers/slice-experience-v2.d.ts +137 -0
  162. package/dist/http/helpers/slice-experience-v2.js +677 -0
  163. package/dist/http/helpers/slice-run-projections.d.ts +72 -0
  164. package/dist/http/helpers/slice-run-projections.js +860 -0
  165. package/dist/http/helpers/triage-mapper.d.ts +43 -0
  166. package/dist/http/helpers/triage-mapper.js +549 -0
  167. package/dist/http/helpers/value-utils.d.ts +6 -0
  168. package/dist/http/helpers/value-utils.js +72 -0
  169. package/dist/http/helpers/workspace-scope.d.ts +15 -0
  170. package/dist/http/helpers/workspace-scope.js +170 -0
  171. package/dist/http/index.d.ts +88 -0
  172. package/dist/http/index.js +3610 -0
  173. package/dist/http/router.d.ts +23 -0
  174. package/dist/http/router.js +23 -0
  175. package/dist/http/routes/agent-control.d.ts +79 -0
  176. package/dist/http/routes/agent-control.js +684 -0
  177. package/dist/http/routes/agent-suite.d.ts +38 -0
  178. package/dist/http/routes/agent-suite.js +397 -0
  179. package/dist/http/routes/agents-catalog.d.ts +40 -0
  180. package/dist/http/routes/agents-catalog.js +128 -0
  181. package/dist/http/routes/billing.d.ts +23 -0
  182. package/dist/http/routes/billing.js +55 -0
  183. package/dist/http/routes/chat.d.ts +19 -0
  184. package/dist/http/routes/chat.js +522 -0
  185. package/dist/http/routes/debug.d.ts +14 -0
  186. package/dist/http/routes/debug.js +21 -0
  187. package/dist/http/routes/decision-actions.d.ts +20 -0
  188. package/dist/http/routes/decision-actions.js +103 -0
  189. package/dist/http/routes/delegation.d.ts +19 -0
  190. package/dist/http/routes/delegation.js +32 -0
  191. package/dist/http/routes/dispatch-gateway-envelope.d.ts +25 -0
  192. package/dist/http/routes/dispatch-gateway-envelope.js +26 -0
  193. package/dist/http/routes/entities.d.ts +63 -0
  194. package/dist/http/routes/entities.js +440 -0
  195. package/dist/http/routes/entity-dynamic.d.ts +25 -0
  196. package/dist/http/routes/entity-dynamic.js +191 -0
  197. package/dist/http/routes/health.d.ts +22 -0
  198. package/dist/http/routes/health.js +49 -0
  199. package/dist/http/routes/live-legacy.d.ts +115 -0
  200. package/dist/http/routes/live-legacy.js +112 -0
  201. package/dist/http/routes/live-misc.d.ts +81 -0
  202. package/dist/http/routes/live-misc.js +426 -0
  203. package/dist/http/routes/live-snapshot.d.ts +136 -0
  204. package/dist/http/routes/live-snapshot.js +916 -0
  205. package/dist/http/routes/live-terminal.d.ts +11 -0
  206. package/dist/http/routes/live-terminal.js +261 -0
  207. package/dist/http/routes/live-triage.d.ts +61 -0
  208. package/dist/http/routes/live-triage.js +248 -0
  209. package/dist/http/routes/mission-control-actions.d.ts +131 -0
  210. package/dist/http/routes/mission-control-actions.js +1791 -0
  211. package/dist/http/routes/mission-control-read.d.ts +73 -0
  212. package/dist/http/routes/mission-control-read.js +1640 -0
  213. package/dist/http/routes/onboarding.d.ts +34 -0
  214. package/dist/http/routes/onboarding.js +101 -0
  215. package/dist/http/routes/realtime-orchestrator.d.ts +10 -0
  216. package/dist/http/routes/realtime-orchestrator.js +74 -0
  217. package/dist/http/routes/run-control.d.ts +27 -0
  218. package/dist/http/routes/run-control.js +96 -0
  219. package/dist/http/routes/runtime-hooks.d.ts +69 -0
  220. package/dist/http/routes/runtime-hooks.js +437 -0
  221. package/dist/http/routes/sentinels-catalog.d.ts +7 -0
  222. package/dist/http/routes/sentinels-catalog.js +24 -0
  223. package/dist/http/routes/settings-byok.d.ts +23 -0
  224. package/dist/http/routes/settings-byok.js +163 -0
  225. package/dist/http/routes/summary.d.ts +18 -0
  226. package/dist/http/routes/summary.js +49 -0
  227. package/dist/http/routes/usage.d.ts +24 -0
  228. package/dist/http/routes/usage.js +362 -0
  229. package/dist/http/routes/work-artifacts.d.ts +9 -0
  230. package/dist/http/routes/work-artifacts.js +55 -0
  231. package/dist/http/shared-state.d.ts +16 -0
  232. package/dist/http/shared-state.js +1 -0
  233. package/dist/http-handler.d.ts +1 -88
  234. package/dist/http-handler.js +1 -10605
  235. package/dist/index.js +287 -2284
  236. package/dist/json-utils.d.ts +1 -0
  237. package/dist/json-utils.js +8 -0
  238. package/dist/local-openclaw.js +29 -6
  239. package/dist/mcp-client-setup.js +3 -3
  240. package/dist/mcp-http-handler.js +33 -59
  241. package/dist/next-up-queue-store.d.ts +16 -1
  242. package/dist/next-up-queue-store.js +93 -25
  243. package/dist/outbox.d.ts +5 -0
  244. package/dist/outbox.js +113 -9
  245. package/dist/paths.js +24 -5
  246. package/dist/reporting/rollups.d.ts +53 -0
  247. package/dist/reporting/rollups.js +148 -0
  248. package/dist/retro/domain-templates.d.ts +45 -0
  249. package/dist/retro/domain-templates.js +297 -0
  250. package/dist/retro/quality-rubric.d.ts +33 -0
  251. package/dist/retro/quality-rubric.js +213 -0
  252. package/dist/runtime-cleanup.d.ts +18 -0
  253. package/dist/runtime-cleanup.js +87 -0
  254. package/dist/runtime-instance-store.js +5 -31
  255. package/dist/services/background.d.ts +34 -0
  256. package/dist/services/background.js +45 -0
  257. package/dist/services/experiment-randomization.d.ts +21 -0
  258. package/dist/services/experiment-randomization.js +63 -0
  259. package/dist/services/instrumentation.d.ts +29 -0
  260. package/dist/services/instrumentation.js +136 -0
  261. package/dist/skill-pack-state.d.ts +36 -5
  262. package/dist/skill-pack-state.js +273 -29
  263. package/dist/snapshot-store.js +5 -25
  264. package/dist/stores/json-store.d.ts +11 -0
  265. package/dist/stores/json-store.js +42 -0
  266. package/dist/sync/local-agent-telemetry.d.ts +13 -0
  267. package/dist/sync/local-agent-telemetry.js +128 -0
  268. package/dist/sync/outbox-replay.d.ts +55 -0
  269. package/dist/sync/outbox-replay.js +621 -0
  270. package/dist/team-context-store.d.ts +23 -0
  271. package/dist/team-context-store.js +116 -0
  272. package/dist/telemetry/posthog.js +4 -2
  273. package/dist/tools/core-tools.d.ts +72 -0
  274. package/dist/tools/core-tools.js +2270 -0
  275. package/dist/types.d.ts +2 -0
  276. package/dist/types.js +2 -0
  277. package/dist/worker-supervisor.js +23 -0
  278. package/package.json +14 -4
  279. package/dashboard/dist/assets/B3ziCA02.js +0 -8
  280. package/dashboard/dist/assets/BNeJ0kpF.js +0 -1
  281. package/dashboard/dist/assets/BzkiMPmM.js +0 -215
  282. package/dashboard/dist/assets/CUV9IHHi.js +0 -1
  283. package/dashboard/dist/assets/Ie7d9Iq2.css +0 -1
  284. package/dashboard/dist/assets/sAhvFnpk.js +0 -4
@@ -0,0 +1,607 @@
1
+ import { chmodSync, createWriteStream, existsSync, mkdtempSync, mkdirSync, readFileSync, writeFileSync, } from "node:fs";
2
+ import { spawn } from "node:child_process";
3
+ import { homedir, tmpdir } from "node:os";
4
+ import { dirname, join, resolve, sep } from "node:path";
5
+ import { getOrgxPluginConfigDir } from "../../paths.js";
6
+ import { normalizeCodexArgs } from "./autopilot-slice-utils.js";
7
+ export function createAutopilotRuntime(deps) {
8
+ function hasExplicitCodexSubcommand(args) {
9
+ const first = (args[0] ?? "").trim();
10
+ if (!first || first.startsWith("-"))
11
+ return false;
12
+ return true;
13
+ }
14
+ function envFlag(name, defaultValue) {
15
+ const raw = (process.env[name] ?? "").trim().toLowerCase();
16
+ if (!raw)
17
+ return defaultValue;
18
+ return !(raw === "0" || raw === "false" || raw === "no" || raw === "off");
19
+ }
20
+ function readFirstExisting(paths) {
21
+ for (const pathname of paths) {
22
+ if (!pathname)
23
+ continue;
24
+ try {
25
+ if (!existsSync(pathname))
26
+ continue;
27
+ return readFileSync(pathname, "utf8");
28
+ }
29
+ catch {
30
+ // continue
31
+ }
32
+ }
33
+ return null;
34
+ }
35
+ function extractOrgxOpenclawUrl(configToml) {
36
+ if (!configToml)
37
+ return null;
38
+ const lines = configToml.split(/\r?\n/);
39
+ let inTargetSection = false;
40
+ for (const rawLine of lines) {
41
+ const line = rawLine.trim();
42
+ if (!line)
43
+ continue;
44
+ if (/^\[mcp_servers\.(?:"orgx-openclaw"|orgx-openclaw)\]\s*$/i.test(line)) {
45
+ inTargetSection = true;
46
+ continue;
47
+ }
48
+ if (inTargetSection && line.startsWith("[")) {
49
+ break;
50
+ }
51
+ if (!inTargetSection)
52
+ continue;
53
+ const urlMatch = line.match(/^url\s*=\s*"([^"]+)"\s*$/i);
54
+ if (!urlMatch)
55
+ continue;
56
+ const rawUrl = (urlMatch[1] ?? "").trim();
57
+ if (rawUrl.length > 0)
58
+ return rawUrl;
59
+ }
60
+ return null;
61
+ }
62
+ function prepareAutopilotCodexHome() {
63
+ const configuredHome = (process.env.ORGX_AUTOPILOT_CODEX_HOME ?? "").trim();
64
+ const preferredHome = configuredHome.length > 0
65
+ ? configuredHome
66
+ : join(getOrgxPluginConfigDir(), "codex-autopilot-home");
67
+ const sourceHome = (process.env.CODEX_HOME ?? "").trim() || join(homedir(), ".codex");
68
+ const sourceConfigCandidates = [
69
+ join(sourceHome, "config.toml"),
70
+ join(homedir(), ".codex", "config.toml"),
71
+ join(homedir(), ".config", "codex", "config.toml"),
72
+ ];
73
+ const mcpModeRaw = (process.env.ORGX_AUTOPILOT_CODEX_MCP_MODE ?? "").trim().toLowerCase();
74
+ const mcpMode = mcpModeRaw.length > 0 ? mcpModeRaw : "orgx-openclaw";
75
+ const sourceConfig = readFirstExisting(sourceConfigCandidates);
76
+ const extractedOrgxOpenclawUrl = extractOrgxOpenclawUrl(sourceConfig);
77
+ const defaultOrgxOpenclawUrl = (process.env.ORGX_AUTOPILOT_ORGX_MCP_URL ?? "").trim() ||
78
+ extractedOrgxOpenclawUrl ||
79
+ "http://127.0.0.1:18789/orgx/mcp";
80
+ const configLines = [
81
+ "# Autogenerated by OrgX autopilot runtime. This isolates codex slices",
82
+ "# from broken or noisy global MCP entries in ~/.codex/config.toml.",
83
+ "",
84
+ ];
85
+ if (mcpMode === "none") {
86
+ configLines.push("mcp_servers = {}", "");
87
+ }
88
+ else {
89
+ configLines.push('[mcp_servers."orgx-openclaw"]', `url = ${JSON.stringify(defaultOrgxOpenclawUrl)}`, "");
90
+ }
91
+ const writeAutopilotHome = (targetHome) => {
92
+ try {
93
+ mkdirSync(targetHome, { recursive: true, mode: 0o700 });
94
+ try {
95
+ chmodSync(targetHome, 0o700);
96
+ }
97
+ catch {
98
+ // best effort
99
+ }
100
+ }
101
+ catch {
102
+ return false;
103
+ }
104
+ const sourceAuthPath = join(sourceHome, "auth.json");
105
+ const targetAuthPath = join(targetHome, "auth.json");
106
+ try {
107
+ if (existsSync(sourceAuthPath)) {
108
+ const authRaw = readFileSync(sourceAuthPath, "utf8");
109
+ writeFileSync(targetAuthPath, authRaw, { encoding: "utf8", mode: 0o600 });
110
+ }
111
+ }
112
+ catch {
113
+ // best effort
114
+ }
115
+ try {
116
+ writeFileSync(join(targetHome, "config.toml"), `${configLines.join("\n")}\n`, {
117
+ encoding: "utf8",
118
+ mode: 0o600,
119
+ });
120
+ }
121
+ catch {
122
+ return false;
123
+ }
124
+ return true;
125
+ };
126
+ if (writeAutopilotHome(preferredHome))
127
+ return preferredHome;
128
+ // Hard fallback: use an ephemeral isolated CODEX_HOME so slices do not
129
+ // inherit potentially broken global MCP config.
130
+ try {
131
+ const fallbackHome = mkdtempSync(join(tmpdir(), "orgx-autopilot-codex-home-"));
132
+ if (writeAutopilotHome(fallbackHome))
133
+ return fallbackHome;
134
+ }
135
+ catch {
136
+ // best effort
137
+ }
138
+ return null;
139
+ }
140
+ function ensurePrivateDirForFile(pathname) {
141
+ const dir = dirname(pathname);
142
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
143
+ try {
144
+ chmodSync(dir, 0o700);
145
+ }
146
+ catch {
147
+ // best effort
148
+ }
149
+ }
150
+ function hasSliceOutput(pathname) {
151
+ try {
152
+ if (!existsSync(pathname))
153
+ return false;
154
+ const raw = readFileSync(pathname, "utf8");
155
+ return raw.trim().length > 0;
156
+ }
157
+ catch {
158
+ return false;
159
+ }
160
+ }
161
+ function writeFallbackSliceOutput(input) {
162
+ if (hasSliceOutput(input.outputPath))
163
+ return false;
164
+ try {
165
+ writeFileSync(input.outputPath, `${JSON.stringify({
166
+ status: "error",
167
+ summary: `Worker exited without structured output (${input.reason}; code=${String(input.code)}, signal=${String(input.signal)}).`,
168
+ workstream_id: input.workstreamId ?? "unknown",
169
+ workstream_title: input.workstreamTitle ?? null,
170
+ slice_id: input.runId,
171
+ }, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
172
+ return true;
173
+ }
174
+ catch {
175
+ return false;
176
+ }
177
+ }
178
+ function spawnCodexSliceWorker(input) {
179
+ ensurePrivateDirForFile(input.logPath);
180
+ ensurePrivateDirForFile(input.outputPath);
181
+ const workerKind = (process.env.ORGX_AUTOPILOT_WORKER_KIND ?? "").trim().toLowerCase();
182
+ if (workerKind === "mock") {
183
+ const scriptPath = resolve(dirname(deps.filename), "..", "..", "scripts", "mock-autopilot-slice-worker.mjs");
184
+ const logStream = createWriteStream(input.logPath, { flags: "a" });
185
+ const outStream = createWriteStream(input.outputPath, { flags: "a" });
186
+ logStream.write(`\n==== ${new Date().toISOString()} :: mock slice ${input.runId} ====\n`);
187
+ const child = spawn("node", [scriptPath], {
188
+ cwd: input.cwd,
189
+ env: {
190
+ ...process.env,
191
+ ...input.env,
192
+ },
193
+ stdio: ["ignore", "pipe", "pipe"],
194
+ // Keep the mock worker as a normal child so stdout/stderr capture is deterministic.
195
+ detached: false,
196
+ });
197
+ deps.autoContinueSliceChildren.set(input.runId, child);
198
+ try {
199
+ logStream.write(`spawned pid=${String(child.pid ?? "")} stdout=${String(Boolean(child.stdout))} stderr=${String(Boolean(child.stderr))}\n`);
200
+ }
201
+ catch {
202
+ // ignore
203
+ }
204
+ child.stdout?.on("data", (chunk) => {
205
+ try {
206
+ logStream.write(chunk);
207
+ }
208
+ catch {
209
+ // ignore
210
+ }
211
+ try {
212
+ outStream.write(chunk);
213
+ }
214
+ catch {
215
+ // ignore
216
+ }
217
+ });
218
+ child.stderr?.on("data", (chunk) => {
219
+ try {
220
+ logStream.write(chunk);
221
+ }
222
+ catch {
223
+ // ignore
224
+ }
225
+ });
226
+ child.on("close", (code, signal) => {
227
+ deps.autoContinueSliceChildren.delete(input.runId);
228
+ const stamp = new Date().toISOString();
229
+ const wroteFallback = writeFallbackSliceOutput({
230
+ outputPath: input.outputPath,
231
+ runId: input.runId,
232
+ workstreamId: input.env.ORGX_WORKSTREAM_ID,
233
+ workstreamTitle: input.env.ORGX_WORKSTREAM_TITLE,
234
+ code,
235
+ signal,
236
+ reason: "mock worker close",
237
+ });
238
+ try {
239
+ if (wroteFallback) {
240
+ logStream.write(`fallback output synthesized at ${input.outputPath}\n`);
241
+ }
242
+ logStream.write(`\n==== ${stamp} :: exit code=${String(code)} signal=${String(signal)} ====\n`);
243
+ }
244
+ catch {
245
+ // ignore
246
+ }
247
+ try {
248
+ logStream.end();
249
+ }
250
+ catch {
251
+ // ignore
252
+ }
253
+ try {
254
+ outStream.end();
255
+ }
256
+ catch {
257
+ // ignore
258
+ }
259
+ });
260
+ child.on("error", (error) => {
261
+ deps.autoContinueSliceChildren.delete(input.runId);
262
+ const msg = deps.safeErrorMessage(error);
263
+ try {
264
+ logStream.write(`\nworker error: ${msg}\n`);
265
+ }
266
+ catch {
267
+ // ignore
268
+ }
269
+ try {
270
+ outStream.write(`${JSON.stringify({
271
+ status: "error",
272
+ summary: `Worker spawn error: ${msg}`,
273
+ workstream_id: input.env.ORGX_WORKSTREAM_ID ?? "unknown",
274
+ workstream_title: input.env.ORGX_WORKSTREAM_TITLE ?? null,
275
+ slice_id: input.runId,
276
+ }, null, 2)}\n`);
277
+ }
278
+ catch {
279
+ // ignore
280
+ }
281
+ });
282
+ return { pid: child.pid ?? null };
283
+ }
284
+ if (workerKind === "claude-code" || workerKind === "claude_code") {
285
+ const hasArgFlag = (args, flag) => args.includes(flag) || args.some((arg) => typeof arg === "string" && arg.startsWith(`${flag}=`));
286
+ const readArgValue = (args, flag) => {
287
+ const inline = args.find((arg) => typeof arg === "string" && arg.startsWith(`${flag}=`));
288
+ if (inline)
289
+ return inline.slice(flag.length + 1).trim() || null;
290
+ const index = args.indexOf(flag);
291
+ const next = index >= 0 ? args[index + 1] : null;
292
+ return typeof next === "string" && next.trim().length > 0 ? next.trim() : null;
293
+ };
294
+ const claudeBin = (process.env.ORGX_CLAUDE_CODE_BIN ?? "").trim() || "claude";
295
+ const rawArgs = (process.env.ORGX_CLAUDE_CODE_ARGS ?? "").trim();
296
+ const args = rawArgs.length > 0 ? rawArgs.split(/\s+/).filter(Boolean) : [];
297
+ const hasPrint = hasArgFlag(args, "--print") || args.includes("-p");
298
+ const hasOutputFormat = hasArgFlag(args, "--output-format");
299
+ const hasJsonSchema = hasArgFlag(args, "--json-schema");
300
+ const hasNoSessionPersistence = hasArgFlag(args, "--no-session-persistence");
301
+ const hasPermissionMode = hasArgFlag(args, "--permission-mode");
302
+ const hasDangerousSkipPermissions = hasArgFlag(args, "--dangerously-skip-permissions");
303
+ const hasAllowDangerousSkipPermissions = hasArgFlag(args, "--allow-dangerously-skip-permissions");
304
+ const explicitOutputFormat = readArgValue(args, "--output-format");
305
+ const explicitJsonSchemaArg = readArgValue(args, "--json-schema");
306
+ const explicitPermissionMode = readArgValue(args, "--permission-mode");
307
+ let schemaArg = null;
308
+ if (input.outputSchemaPath && !hasJsonSchema) {
309
+ try {
310
+ const schemaRaw = readFileSync(input.outputSchemaPath, "utf8").trim();
311
+ if (schemaRaw.length > 0)
312
+ schemaArg = schemaRaw;
313
+ }
314
+ catch {
315
+ schemaArg = null;
316
+ }
317
+ }
318
+ const claudeExtraArgs = [];
319
+ if (!hasPrint)
320
+ claudeExtraArgs.push("--print");
321
+ if (!hasOutputFormat)
322
+ claudeExtraArgs.push("--output-format", "json");
323
+ if (!hasNoSessionPersistence)
324
+ claudeExtraArgs.push("--no-session-persistence");
325
+ if (!hasPermissionMode)
326
+ claudeExtraArgs.push("--permission-mode", "bypassPermissions");
327
+ if (!hasDangerousSkipPermissions && !hasAllowDangerousSkipPermissions) {
328
+ claudeExtraArgs.push("--dangerously-skip-permissions");
329
+ }
330
+ if (schemaArg)
331
+ claudeExtraArgs.push("--json-schema", schemaArg);
332
+ const logStream = createWriteStream(input.logPath, { flags: "a" });
333
+ const outStream = createWriteStream(input.outputPath, { flags: "a" });
334
+ logStream.write(`\n==== ${new Date().toISOString()} :: claude slice ${input.runId} ====\n`);
335
+ logStream.write(`claude_bin: ${claudeBin}\n`);
336
+ if (claudeExtraArgs.length > 0) {
337
+ const claudeArgsForLog = claudeExtraArgs.map((arg) => {
338
+ if (schemaArg && arg === schemaArg)
339
+ return "<json-schema>";
340
+ return arg.length > 160 ? `${arg.slice(0, 157)}...` : arg;
341
+ });
342
+ logStream.write(`claude_args_injected: ${claudeArgsForLog.join(" ")}\n`);
343
+ }
344
+ if (schemaArg && input.outputSchemaPath) {
345
+ logStream.write(`claude_json_schema: ${input.outputSchemaPath}\n`);
346
+ }
347
+ else if (hasJsonSchema) {
348
+ logStream.write(`claude_json_schema: ${explicitJsonSchemaArg ?? "provided-via-args"}\n`);
349
+ }
350
+ else if (input.outputSchemaPath && !hasJsonSchema) {
351
+ logStream.write(`claude_json_schema: unavailable (${input.outputSchemaPath})\n`);
352
+ }
353
+ logStream.write(`claude_output_format: ${hasOutputFormat ? explicitOutputFormat ?? "provided" : "json"}\n`);
354
+ logStream.write(`claude_permission_mode: ${hasPermissionMode ? explicitPermissionMode ?? "provided" : "bypassPermissions"}\n`);
355
+ logStream.write(`claude_skip_permissions: ${hasDangerousSkipPermissions || hasAllowDangerousSkipPermissions ? "provided" : "dangerously-skip-permissions"}\n`);
356
+ const childEnv = {
357
+ ...process.env,
358
+ ...deps.resolveByokEnvOverrides(),
359
+ ...input.env,
360
+ };
361
+ if (claudeBin.includes(sep)) {
362
+ const binDir = dirname(claudeBin);
363
+ childEnv.PATH = childEnv.PATH ? `${binDir}:${childEnv.PATH}` : binDir;
364
+ }
365
+ // Force non-interactive structured output by default for parity with codex output-schema mode.
366
+ // If callers already pass explicit print/output/schema flags, we preserve their args unchanged.
367
+ const child = spawn(claudeBin, [...args, ...claudeExtraArgs, input.prompt], {
368
+ cwd: input.cwd,
369
+ env: childEnv,
370
+ stdio: ["ignore", "pipe", "pipe"],
371
+ detached: true,
372
+ });
373
+ deps.autoContinueSliceChildren.set(input.runId, child);
374
+ child.stdout?.on("data", (chunk) => {
375
+ try {
376
+ logStream.write(chunk);
377
+ }
378
+ catch {
379
+ // ignore
380
+ }
381
+ try {
382
+ outStream.write(chunk);
383
+ }
384
+ catch {
385
+ // ignore
386
+ }
387
+ });
388
+ child.stderr?.on("data", (chunk) => {
389
+ try {
390
+ logStream.write(chunk);
391
+ }
392
+ catch {
393
+ // ignore
394
+ }
395
+ });
396
+ child.on("close", (code, signal) => {
397
+ deps.autoContinueSliceChildren.delete(input.runId);
398
+ const stamp = new Date().toISOString();
399
+ const wroteFallback = writeFallbackSliceOutput({
400
+ outputPath: input.outputPath,
401
+ runId: input.runId,
402
+ workstreamId: input.env.ORGX_WORKSTREAM_ID,
403
+ workstreamTitle: input.env.ORGX_WORKSTREAM_TITLE,
404
+ code,
405
+ signal,
406
+ reason: "claude worker close",
407
+ });
408
+ try {
409
+ if (wroteFallback) {
410
+ logStream.write(`fallback output synthesized at ${input.outputPath}\n`);
411
+ }
412
+ logStream.write(`\n==== ${stamp} :: exit code=${String(code)} signal=${String(signal)} ====\n`);
413
+ }
414
+ catch {
415
+ // ignore
416
+ }
417
+ try {
418
+ logStream.end();
419
+ }
420
+ catch {
421
+ // ignore
422
+ }
423
+ try {
424
+ outStream.end();
425
+ }
426
+ catch {
427
+ // ignore
428
+ }
429
+ });
430
+ child.on("error", (error) => {
431
+ deps.autoContinueSliceChildren.delete(input.runId);
432
+ const msg = deps.safeErrorMessage(error);
433
+ try {
434
+ logStream.write(`\nworker error: ${msg}\n`);
435
+ }
436
+ catch {
437
+ // ignore
438
+ }
439
+ try {
440
+ outStream.write(`${JSON.stringify({
441
+ status: "error",
442
+ summary: `Worker spawn error: ${msg}`,
443
+ workstream_id: input.env.ORGX_WORKSTREAM_ID ?? "unknown",
444
+ workstream_title: input.env.ORGX_WORKSTREAM_TITLE ?? null,
445
+ slice_id: input.runId,
446
+ }, null, 2)}\n`);
447
+ }
448
+ catch {
449
+ // ignore
450
+ }
451
+ });
452
+ child.unref();
453
+ return { pid: child.pid ?? null };
454
+ }
455
+ const codexInfo = deps.resolveCodexBinInfo();
456
+ const codexBin = codexInfo.bin;
457
+ const rawArgs = (process.env.ORGX_CODEX_ARGS ?? "").trim();
458
+ const normalizedArgs = normalizeCodexArgs(rawArgs.length > 0
459
+ ? rawArgs.split(/\s+/).filter(Boolean)
460
+ : ["exec", "--ephemeral", "--full-auto", "--skip-git-repo-check"]);
461
+ const args = hasExplicitCodexSubcommand(normalizedArgs)
462
+ ? normalizedArgs
463
+ : ["exec", ...normalizedArgs];
464
+ const extraArgs = [];
465
+ const logStream = createWriteStream(input.logPath, { flags: "a" });
466
+ logStream.write(`\n==== ${new Date().toISOString()} :: slice ${input.runId} ====\n`);
467
+ logStream.write(`codex_bin: ${codexBin}${codexInfo.versionString ? ` (${codexInfo.versionString})` : ""}\n`);
468
+ const childEnv = {
469
+ ...process.env,
470
+ ...deps.resolveByokEnvOverrides(),
471
+ ...input.env,
472
+ };
473
+ const shouldIsolateCodexHome = envFlag("ORGX_AUTOPILOT_ISOLATE_CODEX_HOME", true);
474
+ const autopilotCodexHome = prepareAutopilotCodexHome();
475
+ if (autopilotCodexHome) {
476
+ childEnv.CODEX_HOME = autopilotCodexHome;
477
+ logStream.write(shouldIsolateCodexHome
478
+ ? `codex_home: ${autopilotCodexHome}\n`
479
+ : `codex_home: ${autopilotCodexHome} (forced-safe override while ORGX_AUTOPILOT_ISOLATE_CODEX_HOME=false)\n`);
480
+ }
481
+ else if (shouldIsolateCodexHome) {
482
+ delete childEnv.CODEX_HOME;
483
+ logStream.write("codex_home: isolation unavailable (default lookup)\n");
484
+ }
485
+ else {
486
+ logStream.write("codex_home: forced-safe override unavailable (inheriting global CODEX_HOME)\n");
487
+ }
488
+ if (codexBin.includes(sep)) {
489
+ const binDir = dirname(codexBin);
490
+ childEnv.PATH = childEnv.PATH ? `${binDir}:${childEnv.PATH}` : binDir;
491
+ }
492
+ const hasOutputLastMessage = args.includes("--output-last-message") ||
493
+ args.some((arg) => typeof arg === "string" && arg.startsWith("--output-last-message="));
494
+ const outputArgs = hasOutputLastMessage
495
+ ? []
496
+ : ["--output-last-message", input.outputPath];
497
+ const hasOutputSchema = args.includes("--output-schema") ||
498
+ args.some((arg) => typeof arg === "string" && arg.startsWith("--output-schema="));
499
+ const outputSchemaArgs = input.outputSchemaPath && !hasOutputSchema
500
+ ? ["--output-schema", input.outputSchemaPath]
501
+ : [];
502
+ if (outputSchemaArgs.length > 0) {
503
+ logStream.write(`output_schema: ${input.outputSchemaPath}\n`);
504
+ }
505
+ const child = spawn(codexBin, [...args, ...extraArgs, ...outputArgs, ...outputSchemaArgs, input.prompt], {
506
+ cwd: input.cwd,
507
+ env: childEnv,
508
+ stdio: ["ignore", "pipe", "pipe"],
509
+ detached: true,
510
+ });
511
+ deps.autoContinueSliceChildren.set(input.runId, child);
512
+ child.stdout?.on("data", (chunk) => {
513
+ try {
514
+ logStream.write(chunk);
515
+ }
516
+ catch {
517
+ // ignore
518
+ }
519
+ });
520
+ child.stderr?.on("data", (chunk) => {
521
+ try {
522
+ logStream.write(chunk);
523
+ }
524
+ catch {
525
+ // ignore
526
+ }
527
+ });
528
+ child.on("close", (code, signal) => {
529
+ deps.autoContinueSliceChildren.delete(input.runId);
530
+ const stamp = new Date().toISOString();
531
+ const wroteFallback = writeFallbackSliceOutput({
532
+ outputPath: input.outputPath,
533
+ runId: input.runId,
534
+ workstreamId: input.env.ORGX_WORKSTREAM_ID,
535
+ workstreamTitle: input.env.ORGX_WORKSTREAM_TITLE,
536
+ code,
537
+ signal,
538
+ reason: "codex worker close",
539
+ });
540
+ try {
541
+ if (wroteFallback) {
542
+ logStream.write(`fallback output synthesized at ${input.outputPath}\n`);
543
+ }
544
+ logStream.write(`\n==== ${stamp} :: exit code=${String(code)} signal=${String(signal)} ====\n`);
545
+ }
546
+ catch {
547
+ // ignore
548
+ }
549
+ try {
550
+ logStream.end();
551
+ }
552
+ catch {
553
+ // ignore
554
+ }
555
+ });
556
+ child.on("error", (error) => {
557
+ deps.autoContinueSliceChildren.delete(input.runId);
558
+ const msg = deps.safeErrorMessage(error);
559
+ try {
560
+ logStream.write(`\nworker error: ${msg}\n`);
561
+ }
562
+ catch {
563
+ // ignore
564
+ }
565
+ try {
566
+ writeFileSync(input.outputPath, `${JSON.stringify({
567
+ status: "error",
568
+ summary: `Worker spawn error: ${msg}`,
569
+ workstream_id: input.env.ORGX_WORKSTREAM_ID ?? "unknown",
570
+ workstream_title: input.env.ORGX_WORKSTREAM_TITLE ?? null,
571
+ slice_id: input.runId,
572
+ }, null, 2)}\n`, { encoding: "utf8" });
573
+ }
574
+ catch {
575
+ // ignore
576
+ }
577
+ });
578
+ child.unref();
579
+ return { pid: child.pid ?? null };
580
+ }
581
+ function writeRuntimeEvent(input) {
582
+ const instance = deps.upsertRuntimeInstanceFromHook({
583
+ source_client: input.sourceClient,
584
+ event: input.event ?? null,
585
+ run_id: input.runId,
586
+ correlation_id: input.runId,
587
+ initiative_id: input.initiativeId,
588
+ workstream_id: input.workstreamId,
589
+ task_id: input.taskId,
590
+ agent_id: input.agentId,
591
+ agent_name: input.agentName,
592
+ phase: input.phase,
593
+ progress_pct: input.progressPct ?? null,
594
+ message: input.message ?? null,
595
+ metadata: input.metadata ?? null,
596
+ timestamp: input.timestamp ?? new Date().toISOString(),
597
+ });
598
+ // Make runtime updates feel instantaneous (don't wait for the 15s staleness timer).
599
+ deps.broadcastRuntimeSse("runtime.updated", instance);
600
+ deps.clearSnapshotResponseCache();
601
+ return instance;
602
+ }
603
+ return {
604
+ spawnCodexSliceWorker,
605
+ writeRuntimeEvent,
606
+ };
607
+ }
@@ -0,0 +1,56 @@
1
+ export declare function ensureAutopilotSliceSchemaPath(schemaFilename: string): string;
2
+ export declare function parseSliceResult<T extends object>(raw: string): T | null;
3
+ export declare function readSliceOutputFile(pathname: string): string | null;
4
+ export declare function readFileTailSafe(pathname: string, maxChars?: number): string;
5
+ export declare function fileUpdatedAtEpochMs(pathname: string, fallbackEpochMs: number): number;
6
+ export type CodexBinInfo = {
7
+ bin: string;
8
+ version: [number, number, number] | null;
9
+ versionString: string | null;
10
+ };
11
+ export declare function normalizeCodexArgs(args: string[]): string[];
12
+ export declare function createCodexBinResolver(): {
13
+ resolveCodexBinInfo: () => CodexBinInfo;
14
+ getCachedCodexProbeSummary: () => string | null;
15
+ };
16
+ export declare function buildSliceOutputInstructions(input: {
17
+ runId: string;
18
+ schemaPath: string;
19
+ requiredSkills: string[];
20
+ }): string;
21
+ export declare function buildWorkstreamSlicePrompt(input: {
22
+ initiativeTitle: string;
23
+ initiativeId: string;
24
+ workstreamId: string;
25
+ workstreamTitle: string;
26
+ milestoneSummaries: Array<{
27
+ id: string;
28
+ title: string;
29
+ status: string;
30
+ }>;
31
+ taskSummaries: Array<{
32
+ id: string;
33
+ title: string;
34
+ status: string;
35
+ milestoneId: string | null;
36
+ }>;
37
+ executionPolicy: {
38
+ domain: string;
39
+ requiredSkills: string[];
40
+ };
41
+ behaviorConfig?: {
42
+ configId?: string | null;
43
+ version?: string | null;
44
+ hash?: string | null;
45
+ policySource?: string | null;
46
+ context?: string | null;
47
+ } | null;
48
+ runId: string;
49
+ schemaPath: string;
50
+ }): string;
51
+ export type BuildScopeDirectiveScope = "task" | "milestone" | "workstream";
52
+ export declare function buildScopeDirective(scope: BuildScopeDirectiveScope, meta: {
53
+ milestoneTitles?: string[];
54
+ workstreamTitle?: string;
55
+ taskCount: number;
56
+ }): string;