kimi-plugin-cc 2.7.3 → 3.1.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 (317) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +3 -16
  3. package/README.md +428 -515
  4. package/assets/logo.svg +6 -0
  5. package/cordis.patch.yml +18 -0
  6. package/dist/background-spawn.js +14 -19
  7. package/dist/cli-client.js +163 -105
  8. package/dist/commands/ask.js +44 -96
  9. package/dist/commands/cancel.js +11 -59
  10. package/dist/commands/cli-helpers.js +102 -1
  11. package/dist/commands/models.js +2 -3
  12. package/dist/commands/pursue.js +8 -21
  13. package/dist/commands/registry.js +7 -26
  14. package/dist/commands/repair-sessions.js +121 -12
  15. package/dist/commands/replay.js +4 -3
  16. package/dist/commands/rescue.js +29 -87
  17. package/dist/commands/result.js +7 -1
  18. package/dist/commands/review.js +28 -18
  19. package/dist/commands/setup.js +813 -462
  20. package/dist/commands/status.js +2 -2
  21. package/dist/commands/swarm.js +112 -125
  22. package/dist/commands/worker.js +1 -2
  23. package/dist/companion.js +145 -19
  24. package/dist/git.js +1 -9
  25. package/dist/help.js +13 -9
  26. package/dist/hook-escape.js +7 -16
  27. package/dist/hooks/approval-hook.js +1766 -87
  28. package/dist/hooks/approval-policy.js +86 -19
  29. package/dist/hooks/config-safety.js +31 -64
  30. package/dist/hooks/hook-timing.js +48 -0
  31. package/dist/hooks/install-paths.js +179 -107
  32. package/dist/hooks/install.js +126 -53
  33. package/dist/hooks/managed-block.js +121 -127
  34. package/dist/job-store.js +206 -166
  35. package/dist/jobs.js +66 -19
  36. package/dist/kimi-command.js +74 -1
  37. package/dist/kimi-engine.js +198 -127
  38. package/dist/kimi-errors.js +15 -70
  39. package/dist/kimi-home.js +6 -0
  40. package/dist/kimi-journal.js +42 -0
  41. package/dist/kimi-releases.js +123 -0
  42. package/dist/kimi-timeouts.js +2 -16
  43. package/dist/kimi-version-probe.js +24 -108
  44. package/dist/logging.js +12 -7
  45. package/dist/mcp/binding.js +13 -4
  46. package/dist/mcp/job-control.js +390 -95
  47. package/dist/mcp/launcher.js +13 -7
  48. package/dist/mcp/operations.js +405 -179
  49. package/dist/mcp/protocol.js +124 -33
  50. package/dist/mcp/skills.js +130 -0
  51. package/dist/mcp/stdio.js +18 -4
  52. package/dist/model-command.js +5 -3
  53. package/dist/native-v2-preflight.js +243 -80
  54. package/dist/parsing.js +211 -243
  55. package/dist/paths.js +28 -10
  56. package/dist/plugin-data.js +28 -83
  57. package/dist/process-tree.js +35 -24
  58. package/dist/read-scope.js +435 -0
  59. package/dist/render.js +58 -46
  60. package/dist/rescue-approval.js +461 -84
  61. package/dist/session-references.js +216 -0
  62. package/dist/session-title.js +26 -6
  63. package/dist/stream-json.js +1 -3
  64. package/dist/surface-routing.js +108 -0
  65. package/dist/swarm-activation.js +74 -188
  66. package/dist/swarm-policy.js +2 -1
  67. package/dist/swarm-recovery.js +2 -1
  68. package/dist/util.js +28 -0
  69. package/dist/version.js +1 -1
  70. package/docs/migration.md +45 -206
  71. package/docs/models.md +24 -66
  72. package/docs/safety.md +198 -353
  73. package/docs/session-visibility.md +25 -13
  74. package/docs/swarm-recovery.md +26 -20
  75. package/dsh/index.json +54 -0
  76. package/mcp_config.json +10 -0
  77. package/package.json +23 -82
  78. package/plugin.json +19 -0
  79. package/plugins/kimi-codex/.codex-plugin/plugin.json +7 -7
  80. package/plugins/kimi-codex/.mcp.json +3 -3
  81. package/plugins/kimi-codex/dist/background-spawn.js +14 -19
  82. package/plugins/kimi-codex/dist/cli-client.js +163 -105
  83. package/plugins/kimi-codex/dist/commands/ask.js +44 -96
  84. package/plugins/kimi-codex/dist/commands/cancel.js +11 -59
  85. package/plugins/kimi-codex/dist/commands/cli-helpers.js +102 -1
  86. package/plugins/kimi-codex/dist/commands/models.js +2 -3
  87. package/plugins/kimi-codex/dist/commands/pursue.js +8 -21
  88. package/plugins/kimi-codex/dist/commands/registry.js +7 -26
  89. package/plugins/kimi-codex/dist/commands/repair-sessions.js +121 -12
  90. package/plugins/kimi-codex/dist/commands/replay.js +4 -3
  91. package/plugins/kimi-codex/dist/commands/rescue.js +29 -87
  92. package/plugins/kimi-codex/dist/commands/result.js +7 -1
  93. package/plugins/kimi-codex/dist/commands/review.js +28 -18
  94. package/plugins/kimi-codex/dist/commands/setup.js +813 -462
  95. package/plugins/kimi-codex/dist/commands/status.js +2 -2
  96. package/plugins/kimi-codex/dist/commands/swarm.js +112 -125
  97. package/plugins/kimi-codex/dist/commands/worker.js +1 -2
  98. package/plugins/kimi-codex/dist/companion.js +145 -19
  99. package/plugins/kimi-codex/dist/git.js +1 -9
  100. package/plugins/kimi-codex/dist/help.js +13 -9
  101. package/plugins/kimi-codex/dist/hook-escape.js +7 -16
  102. package/plugins/kimi-codex/dist/hooks/approval-hook.js +1766 -87
  103. package/plugins/kimi-codex/dist/hooks/approval-policy.js +86 -19
  104. package/plugins/kimi-codex/dist/hooks/config-safety.js +31 -64
  105. package/plugins/kimi-codex/dist/hooks/hook-timing.js +48 -0
  106. package/plugins/kimi-codex/dist/hooks/install-paths.js +179 -107
  107. package/plugins/kimi-codex/dist/hooks/install.js +126 -53
  108. package/plugins/kimi-codex/dist/hooks/managed-block.js +121 -127
  109. package/plugins/kimi-codex/dist/job-store.js +206 -166
  110. package/plugins/kimi-codex/dist/jobs.js +66 -19
  111. package/plugins/kimi-codex/dist/kimi-command.js +74 -1
  112. package/plugins/kimi-codex/dist/kimi-engine.js +198 -127
  113. package/plugins/kimi-codex/dist/kimi-errors.js +15 -70
  114. package/plugins/kimi-codex/dist/kimi-home.js +6 -0
  115. package/plugins/kimi-codex/dist/kimi-journal.js +42 -0
  116. package/plugins/kimi-codex/dist/kimi-releases.js +123 -0
  117. package/plugins/kimi-codex/dist/kimi-timeouts.js +2 -16
  118. package/plugins/kimi-codex/dist/kimi-version-probe.js +24 -108
  119. package/plugins/kimi-codex/dist/logging.js +12 -7
  120. package/plugins/kimi-codex/dist/mcp/binding.js +13 -4
  121. package/plugins/kimi-codex/dist/mcp/job-control.js +390 -95
  122. package/plugins/kimi-codex/dist/mcp/launcher.js +13 -7
  123. package/plugins/kimi-codex/dist/mcp/operations.js +405 -179
  124. package/plugins/kimi-codex/dist/mcp/protocol.js +124 -33
  125. package/plugins/kimi-codex/dist/mcp/skills.js +130 -0
  126. package/plugins/kimi-codex/dist/mcp/stdio.js +18 -4
  127. package/plugins/kimi-codex/dist/model-command.js +5 -3
  128. package/plugins/kimi-codex/dist/native-v2-preflight.js +243 -80
  129. package/plugins/kimi-codex/dist/parsing.js +211 -243
  130. package/plugins/kimi-codex/dist/paths.js +28 -10
  131. package/plugins/kimi-codex/dist/plugin-data.js +28 -83
  132. package/plugins/kimi-codex/dist/process-tree.js +35 -24
  133. package/plugins/kimi-codex/dist/read-scope.js +435 -0
  134. package/plugins/kimi-codex/dist/render.js +58 -46
  135. package/plugins/kimi-codex/dist/rescue-approval.js +461 -84
  136. package/plugins/kimi-codex/dist/session-references.js +216 -0
  137. package/plugins/kimi-codex/dist/session-title.js +26 -6
  138. package/plugins/kimi-codex/dist/stream-json.js +1 -3
  139. package/plugins/kimi-codex/dist/surface-routing.js +108 -0
  140. package/plugins/kimi-codex/dist/swarm-activation.js +74 -188
  141. package/plugins/kimi-codex/dist/swarm-policy.js +2 -1
  142. package/plugins/kimi-codex/dist/swarm-recovery.js +2 -1
  143. package/plugins/kimi-codex/dist/util.js +28 -0
  144. package/plugins/kimi-codex/dist/version.js +1 -1
  145. package/plugins/kimi-codex/scripts/companion.sh +4 -15
  146. package/plugins/kimi-codex/skills/kimi/SKILL.md +77 -0
  147. package/plugins/kimi-codex/skills/kimi/agents/openai.yaml +6 -0
  148. package/scripts/companion.sh +4 -15
  149. package/skills/kimi/SKILL.md +77 -0
  150. package/skills/kimi/agents/openai.yaml +6 -0
  151. package/CHANGELOG.md +0 -1333
  152. package/agents/kimi-ask.md +0 -89
  153. package/agents/kimi-challenge.md +0 -86
  154. package/agents/kimi-pursue.md +0 -93
  155. package/agents/kimi-rescue.md +0 -92
  156. package/agents/kimi-review.md +0 -86
  157. package/agents/kimi-swarm-write.md +0 -96
  158. package/agents/kimi-swarm.md +0 -86
  159. package/commands/README.md +0 -16
  160. package/commands/ask.md +0 -38
  161. package/commands/cancel.md +0 -17
  162. package/commands/challenge.md +0 -35
  163. package/commands/pursue.md +0 -41
  164. package/commands/replay.md +0 -13
  165. package/commands/rescue.md +0 -38
  166. package/commands/result.md +0 -13
  167. package/commands/review.md +0 -35
  168. package/commands/setup.md +0 -23
  169. package/commands/status.md +0 -13
  170. package/commands/swarm.md +0 -44
  171. package/dist/commands/not-implemented.js +0 -4
  172. package/dist/commands/review-gate.js +0 -390
  173. package/dist/config.js +0 -22
  174. package/dist/dsh/companion.js +0 -177
  175. package/dist/dsh/index.js +0 -169
  176. package/dist/hooks/review-gate-stop.js +0 -19
  177. package/dist/schemas/review-gate-output.js +0 -43
  178. package/docs/registry-releases.md +0 -228
  179. package/docs/upstream-2.4.1-certification.md +0 -54
  180. package/docs/upstream-2.5.0-certification.md +0 -52
  181. package/docs/upstream-2.5.1-certification.md +0 -47
  182. package/docs/upstream-2.5.2-certification.md +0 -40
  183. package/docs/upstream-2.5.3-certification.md +0 -73
  184. package/docs/upstream-2.6.0-certification.md +0 -65
  185. package/docs/upstream-2.6.2-certification.md +0 -144
  186. package/docs/upstream-2.6.3-certification.md +0 -100
  187. package/docs/upstream-2.7.2-certification.md +0 -113
  188. package/docs/upstream-2.7.3-certification.md +0 -83
  189. package/docs/windows-validation.md +0 -39
  190. package/hooks/README.md +0 -9
  191. package/hooks/hooks.json +0 -16
  192. package/plugins/kimi-codex/dist/commands/not-implemented.js +0 -4
  193. package/plugins/kimi-codex/dist/commands/review-gate.js +0 -390
  194. package/plugins/kimi-codex/dist/config.js +0 -22
  195. package/plugins/kimi-codex/dist/dsh/companion.js +0 -177
  196. package/plugins/kimi-codex/dist/dsh/index.js +0 -169
  197. package/plugins/kimi-codex/dist/hooks/review-gate-stop.js +0 -19
  198. package/plugins/kimi-codex/dist/schemas/review-gate-output.js +0 -43
  199. package/plugins/kimi-codex/scripts/review-gate-hook.sh +0 -32
  200. package/plugins/kimi-codex/skills/kimi-ask/SKILL.md +0 -35
  201. package/plugins/kimi-codex/skills/kimi-ask/agents/openai.yaml +0 -6
  202. package/plugins/kimi-codex/skills/kimi-cancel/SKILL.md +0 -25
  203. package/plugins/kimi-codex/skills/kimi-cancel/agents/openai.yaml +0 -6
  204. package/plugins/kimi-codex/skills/kimi-challenge/SKILL.md +0 -34
  205. package/plugins/kimi-codex/skills/kimi-challenge/agents/openai.yaml +0 -6
  206. package/plugins/kimi-codex/skills/kimi-pursue/SKILL.md +0 -32
  207. package/plugins/kimi-codex/skills/kimi-pursue/agents/openai.yaml +0 -6
  208. package/plugins/kimi-codex/skills/kimi-replay/SKILL.md +0 -25
  209. package/plugins/kimi-codex/skills/kimi-replay/agents/openai.yaml +0 -6
  210. package/plugins/kimi-codex/skills/kimi-rescue/SKILL.md +0 -34
  211. package/plugins/kimi-codex/skills/kimi-rescue/agents/openai.yaml +0 -6
  212. package/plugins/kimi-codex/skills/kimi-result/SKILL.md +0 -25
  213. package/plugins/kimi-codex/skills/kimi-result/agents/openai.yaml +0 -6
  214. package/plugins/kimi-codex/skills/kimi-review/SKILL.md +0 -34
  215. package/plugins/kimi-codex/skills/kimi-review/agents/openai.yaml +0 -6
  216. package/plugins/kimi-codex/skills/kimi-setup/SKILL.md +0 -34
  217. package/plugins/kimi-codex/skills/kimi-setup/agents/openai.yaml +0 -6
  218. package/plugins/kimi-codex/skills/kimi-status/SKILL.md +0 -25
  219. package/plugins/kimi-codex/skills/kimi-status/agents/openai.yaml +0 -6
  220. package/plugins/kimi-codex/skills/kimi-swarm/SKILL.md +0 -35
  221. package/plugins/kimi-codex/skills/kimi-swarm/agents/openai.yaml +0 -6
  222. package/plugins/kimi-codex/skills/kimi-swarm-write/SKILL.md +0 -36
  223. package/plugins/kimi-codex/skills/kimi-swarm-write/agents/openai.yaml +0 -6
  224. package/plugins/kimi-dsh/LICENSE +0 -201
  225. package/plugins/kimi-dsh/README.md +0 -83
  226. package/plugins/kimi-dsh/cordis.patch.yml +0 -45
  227. package/plugins/kimi-dsh/dist/background-spawn.js +0 -216
  228. package/plugins/kimi-dsh/dist/cli-cancellation.js +0 -43
  229. package/plugins/kimi-dsh/dist/cli-client.js +0 -1078
  230. package/plugins/kimi-dsh/dist/commands/ask.js +0 -336
  231. package/plugins/kimi-dsh/dist/commands/cancel.js +0 -216
  232. package/plugins/kimi-dsh/dist/commands/cli-helpers.js +0 -201
  233. package/plugins/kimi-dsh/dist/commands/models.js +0 -140
  234. package/plugins/kimi-dsh/dist/commands/not-implemented.js +0 -4
  235. package/plugins/kimi-dsh/dist/commands/pursue.js +0 -307
  236. package/plugins/kimi-dsh/dist/commands/recover-swarm.js +0 -140
  237. package/plugins/kimi-dsh/dist/commands/registry.js +0 -74
  238. package/plugins/kimi-dsh/dist/commands/repair-sessions.js +0 -160
  239. package/plugins/kimi-dsh/dist/commands/replay.js +0 -124
  240. package/plugins/kimi-dsh/dist/commands/rescue.js +0 -347
  241. package/plugins/kimi-dsh/dist/commands/result.js +0 -66
  242. package/plugins/kimi-dsh/dist/commands/review-gate.js +0 -390
  243. package/plugins/kimi-dsh/dist/commands/review.js +0 -220
  244. package/plugins/kimi-dsh/dist/commands/setup.js +0 -1129
  245. package/plugins/kimi-dsh/dist/commands/status.js +0 -27
  246. package/plugins/kimi-dsh/dist/commands/swarm.js +0 -849
  247. package/plugins/kimi-dsh/dist/commands/worker.js +0 -25
  248. package/plugins/kimi-dsh/dist/companion.js +0 -97
  249. package/plugins/kimi-dsh/dist/config.js +0 -22
  250. package/plugins/kimi-dsh/dist/dsh/companion.js +0 -177
  251. package/plugins/kimi-dsh/dist/dsh/index.js +0 -169
  252. package/plugins/kimi-dsh/dist/errors.js +0 -40
  253. package/plugins/kimi-dsh/dist/git.js +0 -224
  254. package/plugins/kimi-dsh/dist/help.js +0 -43
  255. package/plugins/kimi-dsh/dist/hook-escape.js +0 -71
  256. package/plugins/kimi-dsh/dist/hooks/approval-hook.js +0 -104
  257. package/plugins/kimi-dsh/dist/hooks/approval-policy.js +0 -233
  258. package/plugins/kimi-dsh/dist/hooks/config-safety.js +0 -665
  259. package/plugins/kimi-dsh/dist/hooks/install-paths.js +0 -428
  260. package/plugins/kimi-dsh/dist/hooks/install.js +0 -210
  261. package/plugins/kimi-dsh/dist/hooks/managed-block.js +0 -684
  262. package/plugins/kimi-dsh/dist/hooks/review-gate-stop.js +0 -19
  263. package/plugins/kimi-dsh/dist/job-id.js +0 -12
  264. package/plugins/kimi-dsh/dist/job-store.js +0 -659
  265. package/plugins/kimi-dsh/dist/jobs.js +0 -217
  266. package/plugins/kimi-dsh/dist/kimi-command.js +0 -71
  267. package/plugins/kimi-dsh/dist/kimi-engine.js +0 -675
  268. package/plugins/kimi-dsh/dist/kimi-errors.js +0 -260
  269. package/plugins/kimi-dsh/dist/kimi-home.js +0 -9
  270. package/plugins/kimi-dsh/dist/kimi-timeouts.js +0 -52
  271. package/plugins/kimi-dsh/dist/kimi-version-probe.js +0 -234
  272. package/plugins/kimi-dsh/dist/logging.js +0 -22
  273. package/plugins/kimi-dsh/dist/mcp/binding.js +0 -58
  274. package/plugins/kimi-dsh/dist/mcp/job-control.js +0 -345
  275. package/plugins/kimi-dsh/dist/mcp/launcher.js +0 -257
  276. package/plugins/kimi-dsh/dist/mcp/operations.js +0 -301
  277. package/plugins/kimi-dsh/dist/mcp/protocol.js +0 -275
  278. package/plugins/kimi-dsh/dist/mcp/server.js +0 -8
  279. package/plugins/kimi-dsh/dist/mcp/stdio.js +0 -78
  280. package/plugins/kimi-dsh/dist/mcp/supervised-child.js +0 -15
  281. package/plugins/kimi-dsh/dist/mcp/tool-result.js +0 -7
  282. package/plugins/kimi-dsh/dist/mcp/workspace.js +0 -34
  283. package/plugins/kimi-dsh/dist/model-command.js +0 -32
  284. package/plugins/kimi-dsh/dist/native-v2-preflight.js +0 -525
  285. package/plugins/kimi-dsh/dist/package.json +0 -1
  286. package/plugins/kimi-dsh/dist/parsing.js +0 -620
  287. package/plugins/kimi-dsh/dist/paths.js +0 -27
  288. package/plugins/kimi-dsh/dist/plugin-data.js +0 -98
  289. package/plugins/kimi-dsh/dist/process-tree.js +0 -586
  290. package/plugins/kimi-dsh/dist/prompt-size.js +0 -18
  291. package/plugins/kimi-dsh/dist/render.js +0 -177
  292. package/plugins/kimi-dsh/dist/rescue-approval.js +0 -865
  293. package/plugins/kimi-dsh/dist/schemas/review-gate-output.js +0 -43
  294. package/plugins/kimi-dsh/dist/session-recovery.js +0 -202
  295. package/plugins/kimi-dsh/dist/session-title.js +0 -293
  296. package/plugins/kimi-dsh/dist/stream-json.js +0 -346
  297. package/plugins/kimi-dsh/dist/swarm-activation.js +0 -1197
  298. package/plugins/kimi-dsh/dist/swarm-policy.js +0 -73
  299. package/plugins/kimi-dsh/dist/swarm-recovery.js +0 -165
  300. package/plugins/kimi-dsh/dist/swarm-tool-policy.js +0 -146
  301. package/plugins/kimi-dsh/dist/types.js +0 -1
  302. package/plugins/kimi-dsh/dist/vendor/shell-quote/LICENSE +0 -24
  303. package/plugins/kimi-dsh/dist/vendor/shell-quote/index.js +0 -1
  304. package/plugins/kimi-dsh/dist/vendor/shell-quote/parse.js +0 -229
  305. package/plugins/kimi-dsh/dist/vendor/smol-toml/LICENSE +0 -24
  306. package/plugins/kimi-dsh/dist/vendor/smol-toml/date.js +0 -156
  307. package/plugins/kimi-dsh/dist/vendor/smol-toml/error.js +0 -67
  308. package/plugins/kimi-dsh/dist/vendor/smol-toml/extract.js +0 -293
  309. package/plugins/kimi-dsh/dist/vendor/smol-toml/parse.js +0 -164
  310. package/plugins/kimi-dsh/dist/vendor/smol-toml/primitive.js +0 -151
  311. package/plugins/kimi-dsh/dist/vendor/smol-toml/struct.js +0 -156
  312. package/plugins/kimi-dsh/dist/vendor/smol-toml/util.js +0 -57
  313. package/plugins/kimi-dsh/dist/version.js +0 -7
  314. package/plugins/kimi-dsh/package.json +0 -78
  315. package/plugins/kimi-dsh/scripts/companion.sh +0 -37
  316. package/plugins/kimi-dsh/scripts/review-gate-hook.sh +0 -32
  317. package/scripts/review-gate-hook.sh +0 -32
@@ -1,345 +0,0 @@
1
- import { formatError, RuntimeError } from "../errors.js";
2
- import { markJobFailed, STALE_SWEEP_GRACE_MS } from "../jobs.js";
3
- import { JobStore, parseHookEscapes, renderBudgetSource, } from "../job-store.js";
4
- import { ensurePluginPaths, resolvePluginPaths } from "../paths.js";
5
- import { readProcessIdentity, revalidateProcessIdentities, revalidateProcessIdentity, } from "../process-tree.js";
6
- import { readArtifact, renderTerminalJobArtifact } from "../render.js";
7
- import { runCancel } from "../commands/cancel.js";
8
- import { assertMcpJobBinding, mcpCompanionIdentity, mcpKimiIdentity } from "./binding.js";
9
- import { readJobControlArguments } from "./operations.js";
10
- import { textResult } from "./tool-result.js";
11
- import { resolveMcpWorkspace } from "./workspace.js";
12
- export async function getMcpJobStatus(env, input) {
13
- const { task, job } = await loadMcpJob(env, input);
14
- const metadata = mcpJobMetadata(task, job);
15
- return textResult(`${JSON.stringify(metadata, null, 2)}\n`, metadata);
16
- }
17
- export async function getMcpJobResult(env, input) {
18
- const { task, job } = await loadMcpJob(env, input);
19
- if (!job) {
20
- if (task.launch_error)
21
- return launchFailureResult(task);
22
- throw new RuntimeError("JOB_NOT_TERMINAL", `MCP job ${task.task_id} is still starting.`, "mcp.result");
23
- }
24
- if (job.status === "running") {
25
- throw new RuntimeError("JOB_NOT_TERMINAL", `Job ${job.job_id} is still running.`, "mcp.result");
26
- }
27
- return terminalJobResult(task, job);
28
- }
29
- export async function cancelMcpJob(env, input) {
30
- const { task, job } = await loadMcpJob(env, input);
31
- if (!job) {
32
- if (task.launch_error)
33
- return launchFailureResult(task);
34
- throw new RuntimeError("JOB_NOT_TERMINAL", `MCP job ${task.task_id} has not attached to a companion job.`, "mcp.cancel");
35
- }
36
- if (job.status !== "running") {
37
- const metadata = mcpJobMetadata(task, job);
38
- return textResult(`${JSON.stringify({ ...metadata, message: "cancel is a no-op for terminal jobs." }, null, 2)}\n`, metadata);
39
- }
40
- const output = await runCancel([task.task_id], {
41
- cwd: task.workspace_cwd,
42
- env,
43
- stdout: process.stdout,
44
- stderr: process.stderr,
45
- mcpCompanionIdentity: mcpCompanionIdentity(task),
46
- mcpKimiIdentity: mcpKimiIdentity(task, job),
47
- });
48
- const current = await loadMcpJob(env, input);
49
- const metadata = mcpJobMetadata(current.task, current.job);
50
- return textResult(output, { ...metadata, cancellation: JSON.parse(output) });
51
- }
52
- export async function getMcpTaskView(env, input) {
53
- let boundTask;
54
- try {
55
- const { task, job } = await loadMcpJob(env, input, (found) => { boundTask = found; });
56
- if (!job) {
57
- if (task.launch_error) {
58
- return taskView(task, null, "completed", launchFailureResult(task), task.launch_error);
59
- }
60
- return taskView(task, null, "working", undefined, "Starting the supervised companion.");
61
- }
62
- switch (job.status) {
63
- case "running":
64
- return taskView(task, job, "working", undefined, `${task.operation_kind}: ${job.phase ?? "running"}`);
65
- case "completed":
66
- return taskView(task, job, "completed", await terminalJobResult(task, job), job.summary);
67
- case "failed":
68
- return taskView(task, job, "completed", await terminalJobResult(task, job), job.summary);
69
- case "cancelled":
70
- return taskView(task, job, "cancelled", undefined, job.summary);
71
- }
72
- }
73
- catch (error) {
74
- if (boundTask && error instanceof RuntimeError && error.code === "MCP_PROCESS_IDENTITY_UNAVAILABLE") {
75
- return taskView(boundTask, null, "working", undefined, formatError(error));
76
- }
77
- if (boundTask && error instanceof RuntimeError && error.code !== "JOB_NOT_FOUND") {
78
- return {
79
- taskId: boundTask.task_id,
80
- status: "failed",
81
- createdAt: boundTask.created_at,
82
- lastUpdatedAt: new Date().toISOString(),
83
- ttlMs: null,
84
- pollIntervalMs: 2_000,
85
- error: {
86
- code: -32_603,
87
- message: formatError(error),
88
- data: { job_id: boundTask.task_id, operation_kind: boundTask.operation_kind },
89
- },
90
- };
91
- }
92
- throw error;
93
- }
94
- }
95
- async function loadMcpJob(env, input, observeTask) {
96
- const args = readJobControlArguments(input);
97
- const paths = resolvePluginPaths(env);
98
- await ensurePluginPaths(paths);
99
- const store = new JobStore(paths);
100
- try {
101
- let task = store.getMcpTask(args.jobId);
102
- if (!task) {
103
- throw new RuntimeError("JOB_NOT_FOUND", `MCP job ${args.jobId} was not found.`, "mcp.lookup");
104
- }
105
- const workspace = await resolveMcpWorkspace(env, args.workspace ?? task.workspace_cwd);
106
- if (task.workspace_cwd !== workspace.cwd || task.repo_id !== workspace.repo.repoId) {
107
- throw new RuntimeError("JOB_NOT_FOUND", `MCP job ${args.jobId} was not found in this workspace.`, "mcp.lookup");
108
- }
109
- observeTask?.(task);
110
- let job = store.getJob(task.task_id);
111
- if (!job) {
112
- task = await reconcileUnattachedTask(store, task);
113
- return { task, job: null };
114
- }
115
- await assertMcpJobBinding(task, job);
116
- if (task.launch_state === "starting") {
117
- let identity;
118
- if (job.status === "running") {
119
- const expected = mcpCompanionIdentity(task);
120
- if (expected) {
121
- const validation = await revalidateProcessIdentities([expected]);
122
- if (!validation.complete) {
123
- throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The companion identity probe was inconclusive; the starting task was not attached or changed.", "mcp.reconcile");
124
- }
125
- if (validation.identities.length === 0) {
126
- const taskUpdatedAt = Date.parse(task.updated_at);
127
- const jobUpdatedAt = Date.parse(job.updated_at);
128
- const stale = Number.isFinite(taskUpdatedAt) && Number.isFinite(jobUpdatedAt) &&
129
- Date.now() - taskUpdatedAt >= STALE_SWEEP_GRACE_MS &&
130
- Date.now() - jobUpdatedAt >= STALE_SWEEP_GRACE_MS;
131
- if (!stale) {
132
- throw new RuntimeError("MCP_PROCESS_IDENTITY_MISMATCH", "The recorded companion process identity no longer matches; the starting task was not attached and no process was adopted.", "mcp.reconcile");
133
- }
134
- task = await reconcileStaleCompanion(store, paths, task, job);
135
- job = store.getJob(task.task_id);
136
- return { task, job };
137
- }
138
- identity = validation.identities[0];
139
- }
140
- else if (job.pid !== null) {
141
- const updatedAt = Date.parse(task.updated_at);
142
- if (!Number.isFinite(updatedAt) || Date.now() - updatedAt >= STALE_SWEEP_GRACE_MS) {
143
- throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The starting task has no reusable identity after the attach grace period; refusing to adopt a process by PID alone.", "mcp.reconcile");
144
- }
145
- identity = await readProcessIdentity(job.pid);
146
- }
147
- }
148
- if (job.status !== "running" || identity) {
149
- task = store.updateMcpTask(task.task_id, {
150
- launch_state: "attached",
151
- attached_at: new Date().toISOString(),
152
- companion_pid: job.pid ?? task.companion_pid,
153
- ...(identity ? { companion_identity: JSON.stringify(identity) } : {}),
154
- });
155
- }
156
- }
157
- else if (task.launch_error) {
158
- throw new RuntimeError("MCP_JOB_BINDING_FAILED", task.launch_error, "mcp.binding");
159
- }
160
- if (job.status === "running") {
161
- task = await refreshCompanionIdentity(store, task, job);
162
- task = await captureKimiIdentity(store, task, job);
163
- task = await reconcileStaleCompanion(store, paths, task, job);
164
- job = store.getJob(task.task_id);
165
- }
166
- return { task, job };
167
- }
168
- finally {
169
- store.close();
170
- }
171
- }
172
- async function reconcileUnattachedTask(store, task) {
173
- if (task.launch_state !== "starting")
174
- return task;
175
- const updatedAt = Date.parse(task.updated_at);
176
- const fresh = Number.isFinite(updatedAt) && Date.now() - updatedAt < STALE_SWEEP_GRACE_MS;
177
- if (task.companion_identity === null) {
178
- if (fresh) {
179
- if (task.companion_pid === null)
180
- return task;
181
- const current = await readProcessIdentity(task.companion_pid);
182
- return current
183
- ? store.updateMcpTask(task.task_id, { companion_identity: JSON.stringify(current) })
184
- : task;
185
- }
186
- if (task.companion_pid !== null) {
187
- throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The unattached companion has no reusable identity; refusing to adopt a process by PID alone.", "mcp.reconcile");
188
- }
189
- return store.updateMcpTask(task.task_id, {
190
- launch_state: "exited",
191
- launch_error: `No companion process was recorded before creating job ${task.task_id}.`,
192
- });
193
- }
194
- if (fresh)
195
- return task;
196
- const validation = await revalidateProcessIdentities([mcpCompanionIdentity(task)]);
197
- if (!validation.complete) {
198
- throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The unattached companion identity probe was inconclusive; the task was not changed.", "mcp.reconcile");
199
- }
200
- if (validation.identities.length > 0)
201
- return task;
202
- return store.updateMcpTask(task.task_id, {
203
- launch_state: "exited",
204
- launch_error: `Companion pid ${task.companion_pid} disappeared before creating job ${task.task_id}.`,
205
- });
206
- }
207
- async function refreshCompanionIdentity(store, task, job) {
208
- if (task.companion_identity !== null || job.pid === null)
209
- return task;
210
- const updatedAt = Date.parse(task.updated_at);
211
- if (!Number.isFinite(updatedAt) || Date.now() - updatedAt >= STALE_SWEEP_GRACE_MS) {
212
- throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The companion identity is missing after the attach grace period; refusing to adopt a process by PID alone.", "mcp.reconcile");
213
- }
214
- const identity = await readProcessIdentity(job.pid);
215
- if (!identity)
216
- return task;
217
- return store.updateMcpTask(task.task_id, {
218
- companion_pid: job.pid,
219
- companion_identity: JSON.stringify(identity),
220
- });
221
- }
222
- async function captureKimiIdentity(store, task, job) {
223
- if (task.kimi_identity !== null)
224
- return task;
225
- const companion = mcpCompanionIdentity(task);
226
- if (!companion || !(await revalidateProcessIdentity(companion)))
227
- return task;
228
- const identity = job.kimi_pid !== null
229
- ? await readProcessIdentity(job.kimi_pid)
230
- : undefined;
231
- if (!identity)
232
- return task;
233
- return store.updateMcpTask(task.task_id, { kimi_identity: JSON.stringify(identity) });
234
- }
235
- async function reconcileStaleCompanion(store, paths, task, job) {
236
- const updatedAt = Date.parse(job.updated_at);
237
- if (!Number.isFinite(updatedAt) || Date.now() - updatedAt < STALE_SWEEP_GRACE_MS)
238
- return task;
239
- const companion = mcpCompanionIdentity(task);
240
- if (!companion) {
241
- throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The stale companion has no reusable process identity; refusing PID-only reconciliation.", "mcp.reconcile");
242
- }
243
- const validation = await revalidateProcessIdentities([companion]);
244
- if (!validation.complete) {
245
- throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The companion process identity probe was inconclusive; no process was signalled and the job was not changed.", "mcp.reconcile");
246
- }
247
- if (validation.identities.length > 0)
248
- return task;
249
- const orphanWarning = await terminateRecordedKimi(task, job);
250
- await markJobFailed(store, paths, job, new RuntimeError("MCP_COMPANION_DISAPPEARED", `The supervised companion disappeared before settling the job.${orphanWarning ? ` ${orphanWarning}` : ""}`, "mcp.reconcile", orphanWarning ? { details: { orphan_warning: orphanWarning, kimi_pid: job.kimi_pid } } : undefined), "Supervised MCP companion disappeared before reporting a terminal state.", { phase: "failed" });
251
- return store.getMcpTask(task.task_id);
252
- }
253
- async function terminateRecordedKimi(task, job) {
254
- const identity = mcpKimiIdentity(task, job);
255
- if (!identity) {
256
- return job.kimi_pid === null
257
- ? null
258
- : `Kimi pid ${job.kimi_pid} was not signalled because no verified identity had been captured.`;
259
- }
260
- if (!(await revalidateProcessIdentity(identity)))
261
- return null;
262
- signalVerifiedIdentity(identity, "SIGTERM");
263
- await sleep(1_500);
264
- if (await revalidateProcessIdentity(identity))
265
- signalVerifiedIdentity(identity, "SIGKILL");
266
- return null;
267
- }
268
- function signalVerifiedIdentity(identity, signal) {
269
- try {
270
- process.kill(identity.processGroupId === identity.pid ? -identity.processGroupId : identity.pid, signal);
271
- }
272
- catch (error) {
273
- const code = error.code;
274
- if (code !== "ESRCH" && code !== "EPERM")
275
- throw error;
276
- }
277
- }
278
- async function terminalJobResult(task, job) {
279
- const metadata = mcpJobMetadata(task, job);
280
- const body = job.final_output_path
281
- ? await readArtifact(job.final_output_path)
282
- : `${renderTerminalJobArtifact(job)}\n`;
283
- return textResult(body, {
284
- ...metadata,
285
- artifact_path: job.final_output_path,
286
- partial_output: job.status === "failed" && Number(job.error?.details?.partial_output_chars ?? 0) > 0,
287
- }, job.status !== "completed");
288
- }
289
- function launchFailureResult(task) {
290
- const message = task.launch_error ?? "The companion failed before creating its job.";
291
- return textResult(message, {
292
- job_id: task.task_id,
293
- operation_kind: task.operation_kind,
294
- launch_state: task.launch_state,
295
- error: { code: "MCP_CHILD_LAUNCH_FAILED", message },
296
- }, true);
297
- }
298
- function mcpJobMetadata(task, job) {
299
- if (!job) {
300
- return {
301
- job_id: task.task_id,
302
- operation_kind: task.operation_kind,
303
- workspace: task.workspace_cwd,
304
- status: task.launch_error ? "failed" : "starting",
305
- launch_state: task.launch_state,
306
- created_at: task.created_at,
307
- updated_at: task.updated_at,
308
- error: task.launch_error,
309
- };
310
- }
311
- return {
312
- job_id: job.job_id,
313
- operation_kind: job.operation_kind,
314
- command_type: job.command_type,
315
- workspace: task.workspace_cwd,
316
- status: job.status,
317
- phase: job.phase,
318
- summary: job.summary,
319
- created_at: job.created_at,
320
- updated_at: job.updated_at,
321
- budget_ms: job.budget_ms,
322
- budget_source: renderBudgetSource(job),
323
- model: job.model,
324
- thinking_effort_requested: job.thinking_effort ?? null,
325
- kimi_session_id: job.kimi_session_id,
326
- kimi_session_id_source: job.kimi_session_id_source,
327
- hook_escapes: parseHookEscapes(job.hook_escapes),
328
- error: job.error,
329
- };
330
- }
331
- function taskView(task, job, status, result, statusMessage) {
332
- return {
333
- taskId: task.task_id,
334
- status,
335
- ...(statusMessage ? { statusMessage } : {}),
336
- createdAt: task.created_at,
337
- lastUpdatedAt: job?.updated_at ?? task.updated_at,
338
- ttlMs: null,
339
- pollIntervalMs: 2_000,
340
- ...(result ? { result: { resultType: "complete", ...result } } : {}),
341
- };
342
- }
343
- function sleep(ms) {
344
- return new Promise((resolve) => setTimeout(resolve, ms));
345
- }
@@ -1,257 +0,0 @@
1
- import { spawn } from "node:child_process";
2
- import { randomUUID } from "node:crypto";
3
- import { access, open, stat } from "node:fs/promises";
4
- import { constants as fsConstants } from "node:fs";
5
- import path from "node:path";
6
- import { fileURLToPath } from "node:url";
7
- import { RuntimeError } from "../errors.js";
8
- import { preferStableNodePath } from "../hooks/install-paths.js";
9
- import { JobStore } from "../job-store.js";
10
- import { markJobFailed } from "../jobs.js";
11
- import { resolveKimiHome } from "../kimi-home.js";
12
- import { ensurePluginPaths, resolvePluginPaths } from "../paths.js";
13
- import { readProcessIdentity, revalidateProcessIdentities, } from "../process-tree.js";
14
- import { assertMcpJobBinding, mcpCompanionIdentity } from "./binding.js";
15
- import { resolveMcpWorkspace } from "./workspace.js";
16
- const compiled = import.meta.url.endsWith(".js");
17
- const companionEntrypoint = fileURLToPath(new URL(compiled ? "../companion.js" : "../companion.ts", import.meta.url));
18
- const companionProjectRoot = path.resolve(path.dirname(companionEntrypoint), "..");
19
- export async function launchMcpOperation(invocation, env, options) {
20
- const attachTimeoutMs = options?.attachTimeoutMs ?? 60_000;
21
- const readIdentity = options?.identityReader ?? readProcessIdentity;
22
- let preserveStartingTask = false;
23
- const attachDeadline = Date.now() + attachTimeoutMs;
24
- const paths = resolvePluginPaths(env);
25
- await ensurePluginPaths(paths);
26
- const workspace = await resolveMcpWorkspace(env, invocation.workspace);
27
- const taskId = randomUUID();
28
- const stdoutPath = path.join(paths.logsDir, `mcp-${taskId}.stdout.log`);
29
- const stderrPath = path.join(paths.logsDir, `mcp-${taskId}.stderr.log`);
30
- const store = new JobStore(paths);
31
- let child;
32
- let stdoutHandle;
33
- let stderrHandle;
34
- let exited;
35
- let spawnError;
36
- try {
37
- const task = store.createMcpTask({
38
- task_id: taskId,
39
- operation_kind: invocation.operationKind,
40
- repo_id: workspace.repo.repoId,
41
- workspace_cwd: workspace.cwd,
42
- stdout_path: stdoutPath,
43
- stderr_path: stderrPath,
44
- });
45
- stdoutHandle = await open(stdoutPath, "wx", 0o600);
46
- stderrHandle = await open(stderrPath, "wx", 0o600);
47
- const nodeBinary = await resolveNodeBinary(env);
48
- const args = compiled
49
- ? [companionEntrypoint, "mcp-child", taskId, ...invocation.argv]
50
- : ["--import", "tsx", companionEntrypoint, "mcp-child", taskId, ...invocation.argv];
51
- const childEnv = sanitizedChildEnv(env, paths, workspace.cwd);
52
- child = spawn(nodeBinary, args, {
53
- cwd: companionProjectRoot,
54
- detached: process.platform !== "win32",
55
- stdio: ["ignore", stdoutHandle.fd, stderrHandle.fd],
56
- env: childEnv,
57
- });
58
- child.once("error", (error) => { spawnError = error; });
59
- child.once("close", (code, signal) => { exited = { code, signal }; });
60
- if (child.pid !== undefined) {
61
- const identity = await readIdentity(child.pid);
62
- store.updateMcpTask(taskId, {
63
- companion_pid: child.pid,
64
- ...(identity ? { companion_identity: JSON.stringify(identity) } : {}),
65
- });
66
- }
67
- await stdoutHandle.close();
68
- await stderrHandle.close();
69
- stdoutHandle = undefined;
70
- stderrHandle = undefined;
71
- for (;;) {
72
- if (spawnError)
73
- throw await markLaunchFailure(store, task, spawnError);
74
- const job = store.getJob(taskId);
75
- if (job && (job.status !== "running" || job.phase === "turn-running" || job.phase === "worker-running")) {
76
- const currentTask = store.getMcpTask(taskId) ?? task;
77
- await assertMcpJobBinding(currentTask, job);
78
- let identity;
79
- if (job.status === "running") {
80
- const expected = mcpCompanionIdentity(currentTask);
81
- if (expected) {
82
- const validation = await revalidateProcessIdentities([expected], {
83
- identityReader: options?.identityReader,
84
- });
85
- if (!validation.complete) {
86
- preserveStartingTask = true;
87
- throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The attachment identity probe was inconclusive; the launch record was preserved.", "mcp.launch", { details: { job_id: taskId, operation_kind: invocation.operationKind } });
88
- }
89
- if (validation.identities.length === 0) {
90
- preserveStartingTask = true;
91
- throw new RuntimeError("MCP_PROCESS_IDENTITY_MISMATCH", "The recorded companion process identity changed before attachment; no process was adopted and the launch record was preserved.", "mcp.launch", { details: { job_id: taskId, operation_kind: invocation.operationKind } });
92
- }
93
- identity = validation.identities[0];
94
- }
95
- else if (child.pid !== undefined) {
96
- identity = await readIdentity(child.pid);
97
- }
98
- }
99
- const attached = store.updateMcpTask(taskId, {
100
- launch_state: "attached",
101
- attached_at: new Date().toISOString(),
102
- ...(identity ? { companion_identity: JSON.stringify(identity) } : {}),
103
- });
104
- child.unref();
105
- return { task: attached, job };
106
- }
107
- if (exited) {
108
- const diagnostic = await readTail(stderrPath);
109
- const error = new RuntimeError("MCP_CHILD_LAUNCH_FAILED", `The companion exited before creating its job (code ${exited.code ?? "unknown"}${exited.signal ? `, signal ${exited.signal}` : ""}).${diagnostic ? `\n${diagnostic}` : ""}`, "mcp.launch", { details: { job_id: taskId, operation_kind: invocation.operationKind, exit_code: exited.code, signal: exited.signal } });
110
- throw await markLaunchFailure(store, task, error, diagnostic);
111
- }
112
- if (Date.now() >= attachDeadline) {
113
- const aborted = await abortMcpLaunch(store, paths, task, job, attachTimeoutMs, options?.identityReader);
114
- if ("result" in aborted) {
115
- child.unref();
116
- return aborted.result;
117
- }
118
- preserveStartingTask = aborted.preserveStarting;
119
- throw aborted.error;
120
- }
121
- await sleep(25);
122
- }
123
- }
124
- catch (error) {
125
- if (!preserveStartingTask && store.getMcpTask(taskId)?.launch_state === "starting") {
126
- store.updateMcpTask(taskId, {
127
- launch_state: "exited",
128
- launch_error: (error instanceof Error ? error.message : String(error)).slice(-8_000),
129
- });
130
- }
131
- child?.unref();
132
- throw error;
133
- }
134
- finally {
135
- store.close();
136
- await stdoutHandle?.close().catch(() => undefined);
137
- await stderrHandle?.close().catch(() => undefined);
138
- }
139
- }
140
- async function abortMcpLaunch(store, paths, task, job, timeoutMs, identityReader) {
141
- let signalDelivered = false;
142
- let preserveStarting = true;
143
- try {
144
- const current = store.getMcpTask(task.task_id) ?? task;
145
- const identity = mcpCompanionIdentity(current);
146
- if (identity) {
147
- const validation = await revalidateProcessIdentities([identity], { identityReader });
148
- if (!validation.complete) {
149
- throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The companion identity probe was inconclusive; no signal was sent and the launch record was preserved.", "mcp.launch", { details: { job_id: task.task_id, operation_kind: task.operation_kind } });
150
- }
151
- if (validation.identities.length === 0) {
152
- throw new RuntimeError("MCP_PROCESS_IDENTITY_MISMATCH", "The recorded companion process identity no longer matches; no signal was sent and the launch record was preserved.", "mcp.launch", { details: { job_id: task.task_id, operation_kind: task.operation_kind } });
153
- }
154
- signalVerifiedIdentity(identity, "SIGTERM");
155
- signalDelivered = true;
156
- await sleep(1_500);
157
- const remaining = await revalidateProcessIdentities([identity], { identityReader });
158
- if (!remaining.complete) {
159
- const error = new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The post-signal companion identity probe was inconclusive; the launch record was preserved.", "mcp.launch", { details: { job_id: task.task_id, operation_kind: task.operation_kind } });
160
- if (job) {
161
- await assertMcpJobBinding(current, job);
162
- return { result: { task: current, job } };
163
- }
164
- return { error, preserveStarting };
165
- }
166
- if (remaining.identities.length > 0) {
167
- signalVerifiedIdentity(identity, "SIGKILL");
168
- }
169
- preserveStarting = false;
170
- }
171
- const error = new RuntimeError("MCP_LAUNCH_TIMEOUT", `The companion did not become cancellation-ready within ${timeoutMs}ms.${signalDelivered ? " The companion was signalled." : " No verified process identity was available, so no signal was sent."}`, "mcp.launch", { details: { job_id: task.task_id, operation_kind: task.operation_kind, timeout_ms: timeoutMs, signal_delivered: signalDelivered } });
172
- if (signalDelivered) {
173
- if (job) {
174
- await markJobFailed(store, paths, job, error, "MCP launch timed out before cancellation was ready.", { phase: "failed" });
175
- }
176
- return { error: await markLaunchFailure(store, current, error), preserveStarting };
177
- }
178
- if (job) {
179
- await assertMcpJobBinding(current, job);
180
- return { result: { task: current, job } };
181
- }
182
- return { error, preserveStarting };
183
- }
184
- catch (error) {
185
- return { error: error instanceof Error ? error : new Error(String(error)), preserveStarting };
186
- }
187
- }
188
- function signalVerifiedIdentity(identity, signal) {
189
- try {
190
- process.kill(identity.processGroupId === identity.pid ? -identity.processGroupId : identity.pid, signal);
191
- }
192
- catch (error) {
193
- const code = error.code;
194
- if (code !== "ESRCH")
195
- throw error;
196
- }
197
- }
198
- async function resolveNodeBinary(env) {
199
- const override = env.KIMI_PLUGIN_CC_NODE_BIN;
200
- const binary = override ? override : preferStableNodePath(process.execPath);
201
- if (binary.includes("/") || binary.includes("\\")) {
202
- try {
203
- await access(binary, fsConstants.X_OK);
204
- }
205
- catch (error) {
206
- throw new RuntimeError("MCP_NODE_BIN_INVALID", `Configured Node binary is not executable: ${binary}`, "mcp.launch", {
207
- cause: error instanceof Error ? error : undefined,
208
- });
209
- }
210
- }
211
- return binary;
212
- }
213
- function sanitizedChildEnv(env, paths, workspace) {
214
- return {
215
- ...env,
216
- KIMI_PLUGIN_CC_WORKSPACE_CWD: workspace,
217
- KIMI_PLUGIN_CC_MCP_WORKSPACE: workspace,
218
- KIMI_PLUGIN_CC_DATA: path.resolve(paths.claudePluginData),
219
- KIMI_CODE_HOME: resolveKimiHome(env, workspace),
220
- KIMI_PLUGIN_CC_WORKSPACE_ROOT: undefined,
221
- KIMI_PLUGIN_CC_CMD: undefined,
222
- KIMI_PLUGIN_CC_OPERATION: undefined,
223
- KIMI_PLUGIN_CC_SKIP_HOOK_CHECK: undefined,
224
- KIMI_PLUGIN_CC_SKIP_VERSION_PROBE: undefined,
225
- KIMI_CODE_TRUST_WORKSPACE: undefined,
226
- KIMI_CODE_EXPERIMENTAL_FLAG: undefined,
227
- };
228
- }
229
- async function markLaunchFailure(store, task, error, diagnostic) {
230
- const failure = error instanceof Error ? error : new Error(String(error));
231
- store.updateMcpTask(task.task_id, {
232
- launch_state: "exited",
233
- launch_error: (diagnostic || failure.message).slice(-8_000),
234
- });
235
- return failure;
236
- }
237
- async function readTail(filePath, maximum = 8_000) {
238
- try {
239
- const info = await stat(filePath);
240
- const handle = await open(filePath, "r");
241
- try {
242
- const length = Math.min(maximum, info.size);
243
- const buffer = Buffer.alloc(length);
244
- await handle.read(buffer, 0, length, Math.max(0, info.size - length));
245
- return buffer.toString("utf8").trim();
246
- }
247
- finally {
248
- await handle.close();
249
- }
250
- }
251
- catch {
252
- return "";
253
- }
254
- }
255
- function sleep(ms) {
256
- return new Promise((resolve) => setTimeout(resolve, ms));
257
- }