kimi-plugin-cc 3.0.0 → 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 (281) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +3 -14
  3. package/README.md +428 -572
  4. package/assets/logo.svg +6 -0
  5. package/cordis.patch.yml +18 -0
  6. package/dist/background-spawn.js +4 -2
  7. package/dist/cli-client.js +105 -66
  8. package/dist/commands/ask.js +27 -12
  9. package/dist/commands/cancel.js +4 -13
  10. package/dist/commands/cli-helpers.js +1 -1
  11. package/dist/commands/pursue.js +3 -2
  12. package/dist/commands/registry.js +7 -26
  13. package/dist/commands/repair-sessions.js +7 -4
  14. package/dist/commands/replay.js +4 -3
  15. package/dist/commands/rescue.js +17 -5
  16. package/dist/commands/review.js +18 -5
  17. package/dist/commands/setup.js +277 -262
  18. package/dist/commands/swarm.js +20 -7
  19. package/dist/companion.js +142 -18
  20. package/dist/help.js +11 -7
  21. package/dist/hook-escape.js +7 -16
  22. package/dist/hooks/approval-hook.js +1762 -124
  23. package/dist/hooks/approval-policy.js +15 -10
  24. package/dist/hooks/config-safety.js +15 -24
  25. package/dist/hooks/hook-timing.js +1 -1
  26. package/dist/hooks/install-paths.js +179 -107
  27. package/dist/hooks/install.js +44 -166
  28. package/dist/hooks/managed-block.js +44 -95
  29. package/dist/job-store.js +117 -49
  30. package/dist/jobs.js +53 -1
  31. package/dist/kimi-engine.js +68 -92
  32. package/dist/kimi-errors.js +1 -1
  33. package/dist/kimi-journal.js +3 -2
  34. package/dist/kimi-releases.js +28 -49
  35. package/dist/kimi-timeouts.js +1 -15
  36. package/dist/kimi-version-probe.js +24 -72
  37. package/dist/logging.js +12 -7
  38. package/dist/mcp/binding.js +13 -4
  39. package/dist/mcp/job-control.js +386 -89
  40. package/dist/mcp/launcher.js +1 -1
  41. package/dist/mcp/operations.js +387 -193
  42. package/dist/mcp/protocol.js +116 -27
  43. package/dist/mcp/skills.js +130 -0
  44. package/dist/mcp/stdio.js +15 -2
  45. package/dist/native-v2-preflight.js +127 -35
  46. package/dist/parsing.js +45 -11
  47. package/dist/paths.js +20 -10
  48. package/dist/plugin-data.js +28 -83
  49. package/dist/read-scope.js +2 -2
  50. package/dist/render.js +13 -38
  51. package/dist/session-references.js +216 -0
  52. package/dist/session-title.js +1 -1
  53. package/dist/surface-routing.js +92 -5
  54. package/dist/swarm-activation.js +26 -137
  55. package/dist/version.js +1 -1
  56. package/docs/migration.md +45 -227
  57. package/docs/models.md +24 -66
  58. package/docs/safety.md +195 -400
  59. package/docs/session-visibility.md +24 -14
  60. package/docs/swarm-recovery.md +26 -20
  61. package/dsh/index.json +54 -0
  62. package/mcp_config.json +10 -0
  63. package/package.json +23 -82
  64. package/plugin.json +19 -0
  65. package/plugins/kimi-codex/.codex-plugin/plugin.json +7 -7
  66. package/plugins/kimi-codex/.mcp.json +2 -3
  67. package/plugins/kimi-codex/dist/background-spawn.js +4 -2
  68. package/plugins/kimi-codex/dist/cli-client.js +105 -66
  69. package/plugins/kimi-codex/dist/commands/ask.js +27 -12
  70. package/plugins/kimi-codex/dist/commands/cancel.js +4 -13
  71. package/plugins/kimi-codex/dist/commands/cli-helpers.js +1 -1
  72. package/plugins/kimi-codex/dist/commands/pursue.js +3 -2
  73. package/plugins/kimi-codex/dist/commands/registry.js +7 -26
  74. package/plugins/kimi-codex/dist/commands/repair-sessions.js +7 -4
  75. package/plugins/kimi-codex/dist/commands/replay.js +4 -3
  76. package/plugins/kimi-codex/dist/commands/rescue.js +17 -5
  77. package/plugins/kimi-codex/dist/commands/review.js +18 -5
  78. package/plugins/kimi-codex/dist/commands/setup.js +277 -262
  79. package/plugins/kimi-codex/dist/commands/swarm.js +20 -7
  80. package/plugins/kimi-codex/dist/companion.js +142 -18
  81. package/plugins/kimi-codex/dist/help.js +11 -7
  82. package/plugins/kimi-codex/dist/hook-escape.js +7 -16
  83. package/plugins/kimi-codex/dist/hooks/approval-hook.js +1762 -124
  84. package/plugins/kimi-codex/dist/hooks/approval-policy.js +15 -10
  85. package/plugins/kimi-codex/dist/hooks/config-safety.js +15 -24
  86. package/plugins/kimi-codex/dist/hooks/hook-timing.js +1 -1
  87. package/plugins/kimi-codex/dist/hooks/install-paths.js +179 -107
  88. package/plugins/kimi-codex/dist/hooks/install.js +44 -166
  89. package/plugins/kimi-codex/dist/hooks/managed-block.js +44 -95
  90. package/plugins/kimi-codex/dist/job-store.js +117 -49
  91. package/plugins/kimi-codex/dist/jobs.js +53 -1
  92. package/plugins/kimi-codex/dist/kimi-engine.js +68 -92
  93. package/plugins/kimi-codex/dist/kimi-errors.js +1 -1
  94. package/plugins/kimi-codex/dist/kimi-journal.js +3 -2
  95. package/plugins/kimi-codex/dist/kimi-releases.js +28 -49
  96. package/plugins/kimi-codex/dist/kimi-timeouts.js +1 -15
  97. package/plugins/kimi-codex/dist/kimi-version-probe.js +24 -72
  98. package/plugins/kimi-codex/dist/logging.js +12 -7
  99. package/plugins/kimi-codex/dist/mcp/binding.js +13 -4
  100. package/plugins/kimi-codex/dist/mcp/job-control.js +386 -89
  101. package/plugins/kimi-codex/dist/mcp/launcher.js +1 -1
  102. package/plugins/kimi-codex/dist/mcp/operations.js +387 -193
  103. package/plugins/kimi-codex/dist/mcp/protocol.js +116 -27
  104. package/plugins/kimi-codex/dist/mcp/skills.js +130 -0
  105. package/plugins/kimi-codex/dist/mcp/stdio.js +15 -2
  106. package/plugins/kimi-codex/dist/native-v2-preflight.js +127 -35
  107. package/plugins/kimi-codex/dist/parsing.js +45 -11
  108. package/plugins/kimi-codex/dist/paths.js +20 -10
  109. package/plugins/kimi-codex/dist/plugin-data.js +28 -83
  110. package/plugins/kimi-codex/dist/read-scope.js +2 -2
  111. package/plugins/kimi-codex/dist/render.js +13 -38
  112. package/plugins/kimi-codex/dist/session-references.js +216 -0
  113. package/plugins/kimi-codex/dist/session-title.js +1 -1
  114. package/plugins/kimi-codex/dist/surface-routing.js +92 -5
  115. package/plugins/kimi-codex/dist/swarm-activation.js +26 -137
  116. package/plugins/kimi-codex/dist/version.js +1 -1
  117. package/plugins/kimi-codex/scripts/companion.sh +4 -15
  118. package/plugins/kimi-codex/skills/kimi/SKILL.md +77 -0
  119. package/plugins/kimi-codex/skills/kimi/agents/openai.yaml +6 -0
  120. package/scripts/companion.sh +4 -15
  121. package/skills/kimi/SKILL.md +77 -0
  122. package/skills/kimi/agents/openai.yaml +6 -0
  123. package/CHANGELOG.md +0 -1402
  124. package/agents/kimi-pursue.md +0 -93
  125. package/agents/kimi-review.md +0 -86
  126. package/agents/kimi-swarm.md +0 -88
  127. package/agents/kimi-task.md +0 -93
  128. package/commands/README.md +0 -14
  129. package/commands/cancel.md +0 -17
  130. package/commands/pursue.md +0 -41
  131. package/commands/replay.md +0 -13
  132. package/commands/result.md +0 -13
  133. package/commands/review.md +0 -40
  134. package/commands/setup.md +0 -25
  135. package/commands/status.md +0 -13
  136. package/commands/swarm.md +0 -45
  137. package/commands/task.md +0 -40
  138. package/dist/commands/review-gate.js +0 -391
  139. package/dist/config.js +0 -22
  140. package/dist/dsh/companion.js +0 -177
  141. package/dist/dsh/index.js +0 -167
  142. package/dist/hooks/review-gate-stop.js +0 -19
  143. package/dist/schemas/review-gate-output.js +0 -41
  144. package/docs/registry-releases.md +0 -228
  145. package/docs/upstream-2.4.1-certification.md +0 -54
  146. package/docs/upstream-2.5.0-certification.md +0 -52
  147. package/docs/upstream-2.5.1-certification.md +0 -47
  148. package/docs/upstream-2.5.2-certification.md +0 -40
  149. package/docs/upstream-2.5.3-certification.md +0 -73
  150. package/docs/upstream-2.6.0-certification.md +0 -65
  151. package/docs/upstream-2.6.2-certification.md +0 -144
  152. package/docs/upstream-2.6.3-certification.md +0 -100
  153. package/docs/upstream-2.7.2-certification.md +0 -113
  154. package/docs/upstream-2.7.3-certification.md +0 -95
  155. package/docs/windows-validation.md +0 -39
  156. package/hooks/README.md +0 -9
  157. package/hooks/hooks.json +0 -16
  158. package/plugins/kimi-codex/dist/commands/review-gate.js +0 -391
  159. package/plugins/kimi-codex/dist/config.js +0 -22
  160. package/plugins/kimi-codex/dist/dsh/companion.js +0 -177
  161. package/plugins/kimi-codex/dist/dsh/index.js +0 -167
  162. package/plugins/kimi-codex/dist/hooks/review-gate-stop.js +0 -19
  163. package/plugins/kimi-codex/dist/schemas/review-gate-output.js +0 -41
  164. package/plugins/kimi-codex/scripts/review-gate-hook.sh +0 -32
  165. package/plugins/kimi-codex/skills/kimi-cancel/SKILL.md +0 -25
  166. package/plugins/kimi-codex/skills/kimi-cancel/agents/openai.yaml +0 -6
  167. package/plugins/kimi-codex/skills/kimi-pursue/SKILL.md +0 -32
  168. package/plugins/kimi-codex/skills/kimi-pursue/agents/openai.yaml +0 -6
  169. package/plugins/kimi-codex/skills/kimi-replay/SKILL.md +0 -25
  170. package/plugins/kimi-codex/skills/kimi-replay/agents/openai.yaml +0 -6
  171. package/plugins/kimi-codex/skills/kimi-result/SKILL.md +0 -25
  172. package/plugins/kimi-codex/skills/kimi-result/agents/openai.yaml +0 -6
  173. package/plugins/kimi-codex/skills/kimi-review/SKILL.md +0 -35
  174. package/plugins/kimi-codex/skills/kimi-review/agents/openai.yaml +0 -6
  175. package/plugins/kimi-codex/skills/kimi-setup/SKILL.md +0 -35
  176. package/plugins/kimi-codex/skills/kimi-setup/agents/openai.yaml +0 -6
  177. package/plugins/kimi-codex/skills/kimi-status/SKILL.md +0 -25
  178. package/plugins/kimi-codex/skills/kimi-status/agents/openai.yaml +0 -6
  179. package/plugins/kimi-codex/skills/kimi-swarm/SKILL.md +0 -37
  180. package/plugins/kimi-codex/skills/kimi-swarm/agents/openai.yaml +0 -6
  181. package/plugins/kimi-codex/skills/kimi-task/SKILL.md +0 -37
  182. package/plugins/kimi-codex/skills/kimi-task/agents/openai.yaml +0 -6
  183. package/plugins/kimi-dsh/LICENSE +0 -201
  184. package/plugins/kimi-dsh/README.md +0 -84
  185. package/plugins/kimi-dsh/cordis.patch.yml +0 -45
  186. package/plugins/kimi-dsh/dist/background-spawn.js +0 -209
  187. package/plugins/kimi-dsh/dist/cli-cancellation.js +0 -43
  188. package/plugins/kimi-dsh/dist/cli-client.js +0 -1097
  189. package/plugins/kimi-dsh/dist/commands/ask.js +0 -269
  190. package/plugins/kimi-dsh/dist/commands/cancel.js +0 -177
  191. package/plugins/kimi-dsh/dist/commands/cli-helpers.js +0 -302
  192. package/plugins/kimi-dsh/dist/commands/models.js +0 -139
  193. package/plugins/kimi-dsh/dist/commands/pursue.js +0 -293
  194. package/plugins/kimi-dsh/dist/commands/recover-swarm.js +0 -140
  195. package/plugins/kimi-dsh/dist/commands/registry.js +0 -74
  196. package/plugins/kimi-dsh/dist/commands/repair-sessions.js +0 -266
  197. package/plugins/kimi-dsh/dist/commands/replay.js +0 -124
  198. package/plugins/kimi-dsh/dist/commands/rescue.js +0 -277
  199. package/plugins/kimi-dsh/dist/commands/result.js +0 -72
  200. package/plugins/kimi-dsh/dist/commands/review-gate.js +0 -391
  201. package/plugins/kimi-dsh/dist/commands/review.js +0 -217
  202. package/plugins/kimi-dsh/dist/commands/setup.js +0 -1465
  203. package/plugins/kimi-dsh/dist/commands/status.js +0 -27
  204. package/plugins/kimi-dsh/dist/commands/swarm.js +0 -823
  205. package/plugins/kimi-dsh/dist/commands/worker.js +0 -24
  206. package/plugins/kimi-dsh/dist/companion.js +0 -99
  207. package/plugins/kimi-dsh/dist/config.js +0 -22
  208. package/plugins/kimi-dsh/dist/dsh/companion.js +0 -177
  209. package/plugins/kimi-dsh/dist/dsh/index.js +0 -167
  210. package/plugins/kimi-dsh/dist/errors.js +0 -40
  211. package/plugins/kimi-dsh/dist/git.js +0 -216
  212. package/plugins/kimi-dsh/dist/help.js +0 -43
  213. package/plugins/kimi-dsh/dist/hook-escape.js +0 -71
  214. package/plugins/kimi-dsh/dist/hooks/approval-hook.js +0 -145
  215. package/plugins/kimi-dsh/dist/hooks/approval-policy.js +0 -295
  216. package/plugins/kimi-dsh/dist/hooks/config-safety.js +0 -641
  217. package/plugins/kimi-dsh/dist/hooks/hook-timing.js +0 -48
  218. package/plugins/kimi-dsh/dist/hooks/install-paths.js +0 -428
  219. package/plugins/kimi-dsh/dist/hooks/install.js +0 -405
  220. package/plugins/kimi-dsh/dist/hooks/managed-block.js +0 -729
  221. package/plugins/kimi-dsh/dist/hooks/review-gate-stop.js +0 -19
  222. package/plugins/kimi-dsh/dist/job-id.js +0 -12
  223. package/plugins/kimi-dsh/dist/job-store.js +0 -631
  224. package/plugins/kimi-dsh/dist/jobs.js +0 -212
  225. package/plugins/kimi-dsh/dist/kimi-command.js +0 -144
  226. package/plugins/kimi-dsh/dist/kimi-engine.js +0 -770
  227. package/plugins/kimi-dsh/dist/kimi-errors.js +0 -205
  228. package/plugins/kimi-dsh/dist/kimi-home.js +0 -15
  229. package/plugins/kimi-dsh/dist/kimi-journal.js +0 -41
  230. package/plugins/kimi-dsh/dist/kimi-releases.js +0 -144
  231. package/plugins/kimi-dsh/dist/kimi-timeouts.js +0 -52
  232. package/plugins/kimi-dsh/dist/kimi-version-probe.js +0 -198
  233. package/plugins/kimi-dsh/dist/logging.js +0 -22
  234. package/plugins/kimi-dsh/dist/mcp/binding.js +0 -58
  235. package/plugins/kimi-dsh/dist/mcp/job-control.js +0 -343
  236. package/plugins/kimi-dsh/dist/mcp/launcher.js +0 -263
  237. package/plugins/kimi-dsh/dist/mcp/operations.js +0 -333
  238. package/plugins/kimi-dsh/dist/mcp/protocol.js +0 -277
  239. package/plugins/kimi-dsh/dist/mcp/server.js +0 -8
  240. package/plugins/kimi-dsh/dist/mcp/stdio.js +0 -79
  241. package/plugins/kimi-dsh/dist/mcp/supervised-child.js +0 -15
  242. package/plugins/kimi-dsh/dist/mcp/tool-result.js +0 -7
  243. package/plugins/kimi-dsh/dist/mcp/workspace.js +0 -34
  244. package/plugins/kimi-dsh/dist/model-command.js +0 -34
  245. package/plugins/kimi-dsh/dist/native-v2-preflight.js +0 -596
  246. package/plugins/kimi-dsh/dist/package.json +0 -1
  247. package/plugins/kimi-dsh/dist/parsing.js +0 -554
  248. package/plugins/kimi-dsh/dist/paths.js +0 -35
  249. package/plugins/kimi-dsh/dist/plugin-data.js +0 -98
  250. package/plugins/kimi-dsh/dist/process-tree.js +0 -597
  251. package/plugins/kimi-dsh/dist/prompt-size.js +0 -18
  252. package/plugins/kimi-dsh/dist/read-scope.js +0 -435
  253. package/plugins/kimi-dsh/dist/render.js +0 -214
  254. package/plugins/kimi-dsh/dist/rescue-approval.js +0 -1242
  255. package/plugins/kimi-dsh/dist/schemas/review-gate-output.js +0 -41
  256. package/plugins/kimi-dsh/dist/session-recovery.js +0 -202
  257. package/plugins/kimi-dsh/dist/session-title.js +0 -313
  258. package/plugins/kimi-dsh/dist/stream-json.js +0 -344
  259. package/plugins/kimi-dsh/dist/surface-routing.js +0 -21
  260. package/plugins/kimi-dsh/dist/swarm-activation.js +0 -1194
  261. package/plugins/kimi-dsh/dist/swarm-policy.js +0 -74
  262. package/plugins/kimi-dsh/dist/swarm-recovery.js +0 -166
  263. package/plugins/kimi-dsh/dist/swarm-tool-policy.js +0 -146
  264. package/plugins/kimi-dsh/dist/types.js +0 -1
  265. package/plugins/kimi-dsh/dist/util.js +0 -28
  266. package/plugins/kimi-dsh/dist/vendor/shell-quote/LICENSE +0 -24
  267. package/plugins/kimi-dsh/dist/vendor/shell-quote/index.js +0 -1
  268. package/plugins/kimi-dsh/dist/vendor/shell-quote/parse.js +0 -229
  269. package/plugins/kimi-dsh/dist/vendor/smol-toml/LICENSE +0 -24
  270. package/plugins/kimi-dsh/dist/vendor/smol-toml/date.js +0 -156
  271. package/plugins/kimi-dsh/dist/vendor/smol-toml/error.js +0 -67
  272. package/plugins/kimi-dsh/dist/vendor/smol-toml/extract.js +0 -293
  273. package/plugins/kimi-dsh/dist/vendor/smol-toml/parse.js +0 -164
  274. package/plugins/kimi-dsh/dist/vendor/smol-toml/primitive.js +0 -151
  275. package/plugins/kimi-dsh/dist/vendor/smol-toml/struct.js +0 -156
  276. package/plugins/kimi-dsh/dist/vendor/smol-toml/util.js +0 -57
  277. package/plugins/kimi-dsh/dist/version.js +0 -7
  278. package/plugins/kimi-dsh/package.json +0 -78
  279. package/plugins/kimi-dsh/scripts/companion.sh +0 -37
  280. package/plugins/kimi-dsh/scripts/review-gate-hook.sh +0 -32
  281. package/scripts/review-gate-hook.sh +0 -32
@@ -1,770 +0,0 @@
1
- import { constants as fsConstants } from "node:fs";
2
- import { access, readFile, realpath, stat } from "node:fs/promises";
3
- import path from "node:path";
4
- import { RuntimeError } from "./errors.js";
5
- import { resolveKimiCliCommand } from "./kimi-command.js";
6
- import { resolveKimiHome } from "./kimi-home.js";
7
- import { formatVersionChoice, kimiVersionsInScope, kimiVersionsWith, resolveKimiRelease } from "./kimi-releases.js";
8
- import { hookGateRefusal, verifyHookInstalled } from "./hooks/install.js";
9
- import { assertNativeV2Preflight } from "./native-v2-preflight.js";
10
- import { isInTestedRange, parseVersionLine, probeKimiVersion, } from "./kimi-version-probe.js";
11
- import { isRecord } from "./util.js";
12
- /**
13
- * The user-visible operation actually being performed. This is deliberately
14
- * separate from JobRecord.command_type: pursue and swarm-write reuse the
15
- * rescue job lineage, while read-only swarm reuses review.
16
- */
17
- const NON_SWARM_OPERATION_KINDS = ["review", "challenge", "ask", "rescue", "review_gate", "pursue"];
18
- const OPERATION_KINDS = [...NON_SWARM_OPERATION_KINDS, "swarm", "swarm-write"];
19
- /** Operations that may run on an uncertified release. Every write or swarm operation stays exact. */
20
- const UNCERTIFIED_READ_ONLY_OPERATIONS = new Set([
21
- "ask",
22
- "review",
23
- "challenge",
24
- "review_gate",
25
- ]);
26
- /** Restores exact-version refusal for every operation. */
27
- export const STRICT_VERSIONS_ENV = "KIMI_PLUGIN_CC_STRICT_VERSIONS";
28
- function strictVersions(env) {
29
- return env[STRICT_VERSIONS_ENV] === "1";
30
- }
31
- /**
32
- * Native-v2 certification of an exact version for one operation: "certified"
33
- * from the per-operation table, "uncertified" for a read-only operation on an
34
- * unlisted plain 2.x that borrows a row's mechanics, otherwise null (refuse).
35
- */
36
- export function resolveNativeV2Certification(operationKind, version, env) {
37
- if (isNativeV2Certified(operationKind, version))
38
- return "certified";
39
- if (!UNCERTIFIED_READ_ONLY_OPERATIONS.has(operationKind) || strictVersions(env))
40
- return null;
41
- const normalized = parseVersionLine(version)?.raw;
42
- if (normalized === undefined || isNativeV2CertifiedVersion(normalized))
43
- return null;
44
- const resolved = resolveKimiRelease(normalized);
45
- return resolved !== null && !resolved.certified ? "uncertified" : null;
46
- }
47
- export const NATIVE_V2_SAFETY_PROFILE = "native-v2-no-plan/1";
48
- /**
49
- * Minimum legacy-v1 version that actually contains each operation. The upper
50
- * boundary is KIMI_TESTED_MINORS, enforced by probe.inTestedRange.
51
- */
52
- const LEGACY_V1_MINIMUMS = {
53
- review: { major: 0, minor: 1 },
54
- challenge: { major: 0, minor: 1 },
55
- ask: { major: 0, minor: 1 },
56
- rescue: { major: 0, minor: 1 },
57
- review_gate: { major: 0, minor: 1 },
58
- pursue: { major: 0, minor: 8 },
59
- swarm: { major: 0, minor: 12 },
60
- "swarm-write": { major: 0, minor: 18 },
61
- };
62
- /**
63
- * Exact kimi-code releases certified for native v2 under
64
- * NATIVE_V2_SAFETY_PROFILE. Exact, not minor-ranged: upstream ships
65
- * behavioural changes in patch releases, and the no-plan construction must be
66
- * re-proven (mechanized tag scan + real-binary smoke) per tag before a row is
67
- * added to KIMI_V2_RELEASES in kimi-releases.ts with scope "all". Each entry
68
- * has its own row in tests/audit/v2-tag-scan.test.ts and a dated entry in
69
- * ROADMAP-TO-GA.md § Post-GA audit log.
70
- */
71
- export const NATIVE_V2_CERTIFIED_VERSIONS = Object.freeze([...kimiVersionsInScope("all")]);
72
- /**
73
- * Exact releases certified only for review, challenge, ask, rescue, review_gate
74
- * and pursue. Their upstream rate-limit recovery can exceed AgentSwarm's
75
- * configured maximum, and these releases lack a certified plugin admission
76
- * construction, so both swarm operations refuse before model spawn.
77
- */
78
- export const NATIVE_V2_NON_SWARM_VERSIONS = Object.freeze([...kimiVersionsInScope("non-swarm")]);
79
- /**
80
- * Any-operation union used by setup and engine selection. The older
81
- * NATIVE_V2_CERTIFIED_VERSIONS export stays all-operation-only for consumers
82
- * that choose a full-swarm pin automatically.
83
- */
84
- export const NATIVE_V2_ANY_OPERATION_VERSIONS = Object.freeze([
85
- ...NATIVE_V2_CERTIFIED_VERSIONS,
86
- ...NATIVE_V2_NON_SWARM_VERSIONS,
87
- ].sort((left, right) => left.localeCompare(right, "en", { numeric: true })));
88
- /** Per-operation native-v2 certification. One green operation never certifies another. */
89
- export const NATIVE_V2_CERTIFIED = new Map([
90
- ["review", NATIVE_V2_ANY_OPERATION_VERSIONS],
91
- ["challenge", NATIVE_V2_ANY_OPERATION_VERSIONS],
92
- ["ask", NATIVE_V2_ANY_OPERATION_VERSIONS],
93
- ["rescue", NATIVE_V2_ANY_OPERATION_VERSIONS],
94
- ["review_gate", NATIVE_V2_ANY_OPERATION_VERSIONS],
95
- ["pursue", NATIVE_V2_ANY_OPERATION_VERSIONS],
96
- ["swarm", NATIVE_V2_CERTIFIED_VERSIONS],
97
- ["swarm-write", NATIVE_V2_CERTIFIED_VERSIONS],
98
- ]);
99
- export function isNativeV2Certified(operationKind, version) {
100
- const normalized = parseVersionLine(version)?.raw;
101
- if (normalized === undefined)
102
- return false;
103
- return NATIVE_V2_CERTIFIED.get(operationKind)?.includes(normalized) ?? false;
104
- }
105
- /**
106
- * True when this exact version is certified for native v2 for ANY operation.
107
- * Used by setup to phrase its version notice: a v2-certified version is NOT an
108
- * "out of tested range, will refuse" case even though it is outside the legacy
109
- * `KIMI_TESTED_MINORS` table (that table intentionally never gains 0.42).
110
- */
111
- export function isNativeV2CertifiedVersion(version) {
112
- const normalized = parseVersionLine(version)?.raw;
113
- if (normalized === undefined)
114
- return false;
115
- return NATIVE_V2_ANY_OPERATION_VERSIONS.includes(normalized);
116
- }
117
- /**
118
- * Select native v2 for a certified version, a test-only operation admission,
119
- * or any major-2 release, then enforce scope in assertCertifiedCapability. A
120
- * narrowed or uncertified 2.x never falls back to legacy-v1 (deleted upstream
121
- * at 0.42.0), so its refusal names the native-v2 set.
122
- */
123
- export function selectIntendedEngine(operationKind, probe) {
124
- if (isNativeV2CertifiedVersion(probe.version) || isNativeV2Certified(operationKind, probe.version)) {
125
- return "native-v2";
126
- }
127
- const parsed = parseVersionLine(probe.version);
128
- return parsed !== undefined && parsed.major >= 2 ? "native-v2" : "legacy-v1";
129
- }
130
- const UNSAFE_EXPERIMENTAL_VALUES = new Set(["1", "true", "yes", "on"]);
131
- /**
132
- * Refuse the ambient experimental-feature selector for legacy-v1, uncertified
133
- * and plan-less launches. A certified or test-bypass native-v2 plan is exempt:
134
- * upstream reads the master flag only from the child's own environment
135
- * (flagService via bootstrap.getEnv, 0.42.0-2.7.3), and buildKimiCliEnvironment
136
- * deletes it from every native-v2 child. cli-client and swarm activation repeat
137
- * this at the final spawn boundary in case the environment changed after plan
138
- * creation.
139
- */
140
- export function assertNoUnsafeExperimentalSelector(env, stage = "kimi-engine.plan", plan) {
141
- const enabled = UNSAFE_EXPERIMENTAL_VALUES.has((env.KIMI_CODE_EXPERIMENTAL_FLAG ?? "").trim().toLowerCase());
142
- if (!enabled)
143
- return;
144
- if (plan?.intendedEngine === "native-v2" && plan.certification !== "uncertified")
145
- return;
146
- throw new RuntimeError("CLI_V2_HOOK_ORDER_UNSAFE", "Refusing kimi-code experimental features: the master KIMI_CODE_EXPERIMENTAL_FLAG enables unreviewed agent-core-v2 features (tower, subagent fork) that kimi-plugin-cc has not certified under its no-plan safety profile. Unset KIMI_CODE_EXPERIMENTAL_FLAG and retry.", stage, {
147
- details: {
148
- refusal_kind: "v2-hook-order-unsafe",
149
- retryable_after_setup: false,
150
- experimental_v2: true,
151
- },
152
- });
153
- }
154
- /**
155
- * Resolve and certify the exact command tuple before a job row is created.
156
- * The same tuple is persisted and later supplied to cli-client, including by a
157
- * detached worker, so ambient PATH/config changes cannot silently choose an
158
- * alternate route between dispatch and execution.
159
- */
160
- export async function prepareKimiExecutionPlan(options) {
161
- // An explicit legacy-v1 plan refuses before any subprocess; otherwise the
162
- // engine and certification decide below, after the master-flag-free probe.
163
- if (options.intendedEngine === "legacy-v1")
164
- assertNoUnsafeExperimentalSelector(options.env);
165
- if (options.intendedEngine === "native-v2" &&
166
- !NATIVE_V2_CERTIFIED.has(options.operationKind)) {
167
- throw nativeV2NotCertified(options.operationKind, null);
168
- }
169
- const kimi = resolveKimiCliCommand(options.env);
170
- const exactCommand = await resolveExactExecutable(kimi.command, options.cwd, options.env);
171
- const warnings = [];
172
- const runPreflight = async (intendedEngine, release) => {
173
- if (options.resumeSource) {
174
- assertResumeEngineCompatible(options.resumeSource, intendedEngine, options.operationKind);
175
- }
176
- if (intendedEngine !== "native-v2")
177
- return;
178
- // Safety, not certification: runs for test-bypass plans too.
179
- const preflight = await assertNativeV2Preflight({
180
- kimiHome: resolveKimiHome(options.env, options.cwd),
181
- env: options.env,
182
- resumeSessionId: options.resumeSessionId,
183
- expectedExecutionRoot: options.cwd,
184
- stage: "kimi-engine.plan",
185
- release: { operationKind: options.operationKind, ...release },
186
- });
187
- warnings.push(...preflight.warnings);
188
- };
189
- if (options.env.KIMI_PLUGIN_CC_SKIP_VERSION_PROBE === "1") {
190
- const intendedEngine = options.intendedEngine ?? "legacy-v1";
191
- const safetyProfile = intendedEngine === "native-v2" ? NATIVE_V2_SAFETY_PROFILE : null;
192
- assertNoUnsafeExperimentalSelector(options.env, "kimi-engine.plan", { intendedEngine, certification: "test-bypass" });
193
- await runPreflight(intendedEngine, { kimiVersion: null, certification: "test-bypass" });
194
- warnings.push(...await planHookCheck(options.operationKind, options.env, options.stderr));
195
- return finishPlan({
196
- schemaVersion: 1,
197
- operationKind: options.operationKind,
198
- intendedEngine,
199
- command: exactCommand,
200
- prefixArgs: Object.freeze([...kimi.prefixArgs]),
201
- kimiVersion: null,
202
- certification: "test-bypass",
203
- resumedFromJobId: options.resumedFromJobId ?? null,
204
- safetyProfile,
205
- }, warnings, options.stderr);
206
- }
207
- const probe = await probeKimiVersion({
208
- kimiBin: exactCommand,
209
- prefixArgs: kimi.prefixArgs,
210
- cwd: options.cwd,
211
- env: withoutExperimentalMasterFlag(options.env),
212
- });
213
- if (probe.kind !== "ok") {
214
- throw new RuntimeError("KIMI_EXECUTION_PLAN_UNRESOLVED", `Refusing to spawn kimi because the exact command/version plan could not be established: ${probe.reason}. Run Claude Code \`/kimi:setup\` or Codex \`$kimi-setup\` to verify the active binary, or correct KIMI_PLUGIN_CC_KIMI_BIN / KIMI_PLUGIN_CC_KIMI_PREFIX_ARGS and retry.`, "kimi-engine.plan", {
215
- details: {
216
- operation_kind: options.operationKind,
217
- intended_engine: options.intendedEngine ?? null,
218
- command: exactCommand,
219
- prefix_args: kimi.prefixArgs,
220
- },
221
- });
222
- }
223
- const intendedEngine = options.intendedEngine ?? selectIntendedEngine(options.operationKind, probe);
224
- const safetyProfile = intendedEngine === "native-v2" ? NATIVE_V2_SAFETY_PROFILE : null;
225
- const certification = assertCertifiedCapability(intendedEngine, options.operationKind, probe, options.env);
226
- assertNoUnsafeExperimentalSelector(options.env, "kimi-engine.plan", { intendedEngine, certification });
227
- if (certification === "uncertified") {
228
- const borrowed = resolveKimiRelease(probe.version).release.version;
229
- warnings.push(`kimi-code ${probe.version} is not certified by this plugin release; running ${options.operationKind} read-only with ${borrowed} mechanics.`);
230
- }
231
- await runPreflight(intendedEngine, { kimiVersion: probe.version, certification });
232
- warnings.push(...await planHookCheck(options.operationKind, options.env, options.stderr, probe));
233
- return finishPlan({
234
- schemaVersion: 1,
235
- operationKind: options.operationKind,
236
- intendedEngine,
237
- command: exactCommand,
238
- prefixArgs: Object.freeze([...kimi.prefixArgs]),
239
- kimiVersion: probe.version,
240
- certification,
241
- resumedFromJobId: options.resumedFromJobId ?? null,
242
- safetyProfile,
243
- }, warnings, options.stderr);
244
- }
245
- function withoutExperimentalMasterFlag(env) {
246
- const copy = { ...env };
247
- delete copy.KIMI_CODE_EXPERIMENTAL_FLAG;
248
- return copy;
249
- }
250
- /**
251
- * Job-time hook check against the release this plan probed (`probe`, absent
252
- * only on the test-bypass path). It refuses when the configured hooks array is
253
- * invalid for that release, because kimi-code would then load no hook at all;
254
- * the command gate's earlier check may have probed a different release.
255
- * Any other definite negative (`installed === false`) refuses with the
256
- * operation's command-gate error: the hook may have gone between that gate and
257
- * this plan. KIMI_PLUGIN_CC_SKIP_HOOK_CHECK=1 makes verifyHookInstalled report
258
- * installed, exactly as at the gates. Otherwise it returns warnings: marker
259
- * repair (G6) and other hosts' older hooks that still apply their own rules to
260
- * this job (H2). A thrown, unverifiable result adds nothing and never refuses.
261
- */
262
- async function planHookCheck(operation, env, stderr, probe) {
263
- let status;
264
- try {
265
- status = await verifyHookInstalled(env, { kimiVersion: probe });
266
- }
267
- catch {
268
- return [];
269
- }
270
- if (status.hookSetInvalid) {
271
- throw new RuntimeError("KIMI_HOOK_SET_INVALID", `Refusing to spawn kimi: the configured kimi-code hooks are invalid for the release this job runs (${probe?.version ?? "version not probed"}): ${status.reason}. kimi-code drops the whole hooks array, so the managed PreToolUse hook would not run. Repair or remove that [[hooks]] entry in ${status.configPath} and retry.`, "kimi-engine.plan", {
272
- details: {
273
- config_path: status.configPath,
274
- kimi_version: probe?.version ?? null,
275
- retryable_after_setup: false,
276
- },
277
- });
278
- }
279
- if (status.installed)
280
- return status.warnings ?? [];
281
- throw hookGateRefusal(operation, status, env, stderr);
282
- }
283
- /** Freeze the plan; a nonempty warning list is attached and announced once on stderr. */
284
- function finishPlan(plan, warnings, stderr = process.stderr) {
285
- if (warnings.length === 0)
286
- return Object.freeze(plan);
287
- for (const warning of warnings)
288
- stderr.write(`[kimi-plugin-cc] warning: ${warning}\n`);
289
- return Object.freeze({ ...plan, warnings: Object.freeze([...warnings]) });
290
- }
291
- export function assertExecutionPlanMatchesSpawn(plan, command, prefixArgs, env = {}) {
292
- assertExecutionPlanShape(plan);
293
- assertPlanCertification(plan, env);
294
- if (plan.command !== command || !sameStrings(plan.prefixArgs, prefixArgs)) {
295
- throw new RuntimeError("KIMI_EXECUTION_PLAN_MISMATCH", "Refusing to spawn kimi because the command tuple differs from the certified execution plan.", "cli-client.pre-spawn", {
296
- details: {
297
- operation_kind: plan.operationKind,
298
- intended_engine: plan.intendedEngine,
299
- planned_command: plan.command,
300
- planned_prefix_args: plan.prefixArgs,
301
- requested_command: command,
302
- requested_prefix_args: prefixArgs,
303
- },
304
- });
305
- }
306
- }
307
- export function persistedExecutionPlanFields(plan) {
308
- return {
309
- operation_kind: plan.operationKind,
310
- intended_engine: plan.intendedEngine,
311
- observed_engine: null,
312
- kimi_version: plan.kimiVersion,
313
- system_version: null,
314
- kimi_command: plan.command,
315
- kimi_prefix_args: JSON.stringify(plan.prefixArgs),
316
- plan_certification: plan.certification,
317
- resumed_from_job_id: plan.resumedFromJobId,
318
- safety_profile: plan.safetyProfile,
319
- warnings: plan.warnings !== undefined && plan.warnings.length > 0 ? JSON.stringify(plan.warnings) : null,
320
- };
321
- }
322
- export function observedExecutionFields(result) {
323
- return {
324
- observed_engine: result.observedEngine,
325
- system_version: result.systemVersion ?? null,
326
- // Detection evidence from runtime/hook-escape.ts, JSON array or NULL.
327
- hook_escapes: result.hookEscapes && result.hookEscapes.length > 0 ? JSON.stringify(result.hookEscapes) : null,
328
- };
329
- }
330
- export function executionPlanFromPersisted(fields) {
331
- if (fields.operation_kind === null ||
332
- fields.intended_engine === null ||
333
- fields.kimi_command === null ||
334
- fields.kimi_prefix_args === null ||
335
- fields.plan_certification === null) {
336
- throw new RuntimeError("KIMI_EXECUTION_PLAN_MISSING", "This job predates durable engine provenance and cannot be used as a subprocess execution plan.", "kimi-engine.persisted-plan");
337
- }
338
- if (!isOperationKind(fields.operation_kind)) {
339
- throw invalidPersistedPlan(`unknown operation_kind ${JSON.stringify(fields.operation_kind)}`);
340
- }
341
- if (!isKimiEngine(fields.intended_engine)) {
342
- throw invalidPersistedPlan(`unknown intended_engine ${JSON.stringify(fields.intended_engine)}`);
343
- }
344
- if (!isPlanCertification(fields.plan_certification)) {
345
- throw invalidPersistedPlan(`unknown plan_certification ${JSON.stringify(fields.plan_certification)}`);
346
- }
347
- if (fields.kimi_command.length === 0) {
348
- throw invalidPersistedPlan("kimi_command is empty");
349
- }
350
- let prefixArgs;
351
- try {
352
- prefixArgs = JSON.parse(fields.kimi_prefix_args);
353
- }
354
- catch (error) {
355
- throw invalidPersistedPlan("kimi_prefix_args is not valid JSON", error);
356
- }
357
- if (!Array.isArray(prefixArgs) || !prefixArgs.every((entry) => typeof entry === "string")) {
358
- throw invalidPersistedPlan("kimi_prefix_args is not a string array");
359
- }
360
- const plan = Object.freeze({
361
- schemaVersion: 1,
362
- operationKind: fields.operation_kind,
363
- intendedEngine: fields.intended_engine,
364
- command: fields.kimi_command,
365
- prefixArgs: Object.freeze([...prefixArgs]),
366
- kimiVersion: fields.kimi_version,
367
- certification: fields.plan_certification,
368
- resumedFromJobId: fields.resumed_from_job_id,
369
- safetyProfile: fields.safety_profile ?? null,
370
- });
371
- assertExecutionPlanShape(plan);
372
- return plan;
373
- }
374
- const MAX_FORENSIC_LOG_BYTES = 32 * 1024 * 1024;
375
- /**
376
- * Best-effort, evidence-only persistence for a historical row. Missing,
377
- * oversized, malformed, or conflicting logs leave the row unknown. This is
378
- * deliberately non-blocking: provenance inspection must not break status or
379
- * result retrieval for an old job.
380
- */
381
- export async function reconcileHistoricalJobProvenance(store, job) {
382
- if (job.observed_engine !== null && job.operation_kind !== null)
383
- return job;
384
- if (!job.stream_log_path)
385
- return job;
386
- try {
387
- const info = await stat(job.stream_log_path);
388
- if (!info.isFile() || info.size > MAX_FORENSIC_LOG_BYTES)
389
- return job;
390
- const evidence = classifyHistoricalEngineProvenance(await readFile(job.stream_log_path, "utf8"));
391
- if (evidence.conflict)
392
- return job;
393
- return (store.backfillHistoricalProvenance(job.job_id, {
394
- operation_kind: evidence.operationKind,
395
- observed_engine: evidence.observedEngine,
396
- kimi_version: evidence.kimiVersion,
397
- system_version: evidence.observedEngine === "native-v2" ? evidence.kimiVersion : null,
398
- }) ?? job);
399
- }
400
- catch {
401
- return job;
402
- }
403
- }
404
- /**
405
- * Known cross-engine resumes are forbidden. Unknown historical rows keep the
406
- * pre-provenance legacy-v1 behavior, but can never be continued on native v2:
407
- * a v2 resume replays the saved journal, so only proven, plugin-managed v2
408
- * lineage is accepted there.
409
- */
410
- export function assertResumeEngineCompatible(source, targetEngine, operationKind) {
411
- if (source.observed_engine === null && targetEngine === "native-v2") {
412
- throw new RuntimeError("KIMI_SESSION_LINEAGE_UNKNOWN", `Refusing to resume session ${source.kimi_session_id ?? "<unknown>"}: job ${source.job_id} has no proven engine provenance, and native-v2 continues only plugin-managed native-v2 sessions. Start a fresh ${operationKind} session instead; the old job's status/result/replay remain available.`, `${operationKind}.resume`, {
413
- details: {
414
- refusal_kind: "session-lineage-unknown",
415
- retryable_after_setup: false,
416
- source_job_id: source.job_id,
417
- source_engine: null,
418
- target_engine: targetEngine,
419
- operation_kind: operationKind,
420
- },
421
- });
422
- }
423
- if (source.observed_engine === null || source.observed_engine === targetEngine)
424
- return;
425
- throw new RuntimeError("KIMI_SESSION_ENGINE_MISMATCH", `Refusing to resume session ${source.kimi_session_id ?? "<unknown>"}: job ${source.job_id} is proven ${source.observed_engine}, while the new ${operationKind} plan is ${targetEngine}. Start a fresh session instead.`, `${operationKind}.resume`, {
426
- details: {
427
- source_job_id: source.job_id,
428
- source_engine: source.observed_engine,
429
- target_engine: targetEngine,
430
- operation_kind: operationKind,
431
- },
432
- });
433
- }
434
- /**
435
- * Re-validate a PERSISTED resume before a detached worker spawns it. The
436
- * dispatch path checks resume lineage against `resumeSource`, but a worker (or
437
- * a forged/corrupt queued row) reloads the plan and hands `kimi_session_id`
438
- * straight to the spawn — so the binding and lineage must be re-established
439
- * from the store here, not trusted from the row. Callers invoke this ONLY when
440
- * the row carries a session id to resume; a genuinely fresh session has none
441
- * yet (kimi assigns it after the run), so it never reaches this check.
442
- *
443
- * Refuses when: the row carries a session id but the plan records no source
444
- * job (a session with no plugin lineage can never be a plugin-managed resume);
445
- * the recorded source job is missing; the source it names does not actually
446
- * own the session being resumed (binding forgery); or the source's proven
447
- * engine is incompatible with the plan's engine (incl. unknown lineage on a
448
- * native-v2 target). The v2 preflight separately re-scans that session's
449
- * journal for plan taint at the spawn boundary.
450
- */
451
- export async function assertPersistedResumeLineage(store, plan, resumeSessionId, operationKind) {
452
- if (plan.resumedFromJobId === null) {
453
- throw new RuntimeError("KIMI_SESSION_LINEAGE_UNKNOWN", `Refusing to resume session ${resumeSessionId}: the persisted job carries a session id but records no source job, so its lineage cannot be established. Start a fresh session.`, `${operationKind}.resume`, {
454
- details: {
455
- refusal_kind: "session-lineage-unknown",
456
- retryable_after_setup: false,
457
- source_job_id: null,
458
- },
459
- });
460
- }
461
- const source = store.getJob(plan.resumedFromJobId);
462
- if (!source) {
463
- throw new RuntimeError("KIMI_SESSION_LINEAGE_UNKNOWN", `Refusing to resume session ${resumeSessionId}: the persisted source job ${plan.resumedFromJobId} no longer exists, so its engine lineage cannot be established. Start a fresh session.`, `${operationKind}.resume`, {
464
- details: {
465
- refusal_kind: "session-lineage-unknown",
466
- retryable_after_setup: false,
467
- source_job_id: plan.resumedFromJobId,
468
- },
469
- });
470
- }
471
- const reconciled = await reconcileHistoricalJobProvenance(store, source);
472
- if (reconciled.kimi_session_id !== resumeSessionId) {
473
- throw new RuntimeError("KIMI_SESSION_LINEAGE_UNKNOWN", `Refusing to resume session ${resumeSessionId}: the persisted source job ${plan.resumedFromJobId} owns a different session (${reconciled.kimi_session_id ?? "<none>"}). Start a fresh session.`, `${operationKind}.resume`, {
474
- details: {
475
- refusal_kind: "session-lineage-unknown",
476
- retryable_after_setup: false,
477
- source_job_id: plan.resumedFromJobId,
478
- },
479
- });
480
- }
481
- assertResumeEngineCompatible(reconciled, plan.intendedEngine, operationKind);
482
- }
483
- /**
484
- * Classify saved plugin logs without guessing. `system.version` and wire
485
- * protocol 1.5 are positive v2 evidence; a spawn that explicitly records the
486
- * forced legacy flag, or wire protocol <=1.4, is positive v1 evidence. Absence
487
- * of either remains unknown, covering the historical v1.9.5 routing window.
488
- */
489
- export function classifyHistoricalEngineProvenance(contents) {
490
- let sawLegacy = false;
491
- let sawNative = false;
492
- let sawLegacyForcedSpawn = false;
493
- let sawRunEvidence = false;
494
- let kimiVersion = null;
495
- let operationKind = null;
496
- const evidence = [];
497
- for (const rawLine of contents.split(/\r?\n/)) {
498
- const line = rawLine.trim();
499
- if (line.length === 0)
500
- continue;
501
- let entry;
502
- try {
503
- entry = JSON.parse(line);
504
- }
505
- catch {
506
- continue;
507
- }
508
- if (!isRecord(entry))
509
- continue;
510
- if (entry.event === "system_version" && typeof entry.version === "string") {
511
- sawNative = true;
512
- kimiVersion = parseVersionLine(entry.version)?.raw ?? entry.version;
513
- evidence.push("stream-json:system.version");
514
- }
515
- if (entry.event === "spawn" && entry.legacy_v1_forced === true) {
516
- sawLegacyForcedSpawn = true;
517
- }
518
- if (entry.event === "record" ||
519
- entry.event === "session_announce" ||
520
- entry.event === "goal_summary" ||
521
- (entry.event === "exit" && entry.exit_code === 0)) {
522
- sawRunEvidence = true;
523
- }
524
- const protocolVersions = entry.type === "metadata" && typeof entry.protocol_version === "string"
525
- ? [entry.protocol_version]
526
- : [];
527
- for (const protocolVersion of protocolVersions) {
528
- const parsed = parseProtocolVersion(protocolVersion);
529
- if (parsed === null)
530
- continue;
531
- if (parsed.major > 1 || (parsed.major === 1 && parsed.minor >= 5)) {
532
- sawNative = true;
533
- evidence.push(`session-wire:protocol-${protocolVersion}`);
534
- }
535
- else {
536
- sawLegacy = true;
537
- evidence.push(`session-wire:protocol-${protocolVersion}`);
538
- }
539
- }
540
- operationKind ??= operationKindFromLogEntry(entry);
541
- }
542
- if (sawLegacyForcedSpawn && sawRunEvidence) {
543
- sawLegacy = true;
544
- evidence.push("spawn:legacy_v1_forced+run-evidence");
545
- }
546
- const conflict = sawLegacy && sawNative;
547
- return {
548
- observedEngine: conflict ? null : sawNative ? "native-v2" : sawLegacy ? "legacy-v1" : null,
549
- kimiVersion,
550
- operationKind,
551
- evidence: Object.freeze([...new Set(evidence)]),
552
- conflict,
553
- };
554
- }
555
- function assertCertifiedCapability(engine, operationKind, probe, env) {
556
- if (engine === "native-v2") {
557
- const certification = resolveNativeV2Certification(operationKind, probe.version, env);
558
- if (certification === null)
559
- throw nativeV2NotCertified(operationKind, probe.version, env);
560
- return certification;
561
- }
562
- const minimum = LEGACY_V1_MINIMUMS[operationKind];
563
- const meetsMinimum = probe.major > minimum.major ||
564
- (probe.major === minimum.major && probe.minor >= minimum.minor);
565
- if (!probe.inTestedRange || !meetsMinimum) {
566
- const remedy = !probe.inTestedRange
567
- ? "Update kimi-plugin-cc to a release that certifies this kimi-code minor, or point KIMI_PLUGIN_CC_KIMI_BIN at a certified binary."
568
- : `Upgrade kimi-code to at least ${minimum.major}.${minimum.minor}.0 within a certified minor.`;
569
- throw new RuntimeError("KIMI_CAPABILITY_NOT_CERTIFIED", `Refusing ${operationKind}: kimi-code ${probe.version} is not in this operation's certified legacy-v1 range. ${remedy} KIMI_PLUGIN_CC_SKIP_VERSION_PROBE is a test/smoke seam, not a production repair path.`, "kimi-engine.capability", {
570
- details: {
571
- refusal_kind: "v1-version-not-certified",
572
- retryable_after_setup: false,
573
- operation_kind: operationKind,
574
- intended_engine: engine,
575
- kimi_version: probe.version,
576
- minimum_version: `${minimum.major}.${minimum.minor}.0`,
577
- tested_minor: probe.inTestedRange,
578
- },
579
- });
580
- }
581
- return "certified";
582
- }
583
- /**
584
- * Revalidate the persisted certification at the final subprocess boundary.
585
- * This keeps a corrupt/forged SQLite row from converting a once-certified plan
586
- * into an untested-version or native-v2 spawn.
587
- */
588
- function assertPlanCertification(plan, env) {
589
- if (plan.intendedEngine === "native-v2" && plan.safetyProfile !== NATIVE_V2_SAFETY_PROFILE) {
590
- // A forged/deserialized v2 row cannot reach spawn without the profile it
591
- // was certified under.
592
- throw invalidPersistedPlan("native-v2 plans require the native-v2-no-plan safety profile");
593
- }
594
- if (plan.intendedEngine === "legacy-v1" && plan.safetyProfile !== null) {
595
- throw invalidPersistedPlan("legacy-v1 plans must not carry a safety profile");
596
- }
597
- if (plan.certification === "test-bypass") {
598
- if (plan.kimiVersion !== null) {
599
- throw invalidPersistedPlan("test-bypass plans must have kimi_version=null");
600
- }
601
- if (env.KIMI_PLUGIN_CC_SKIP_VERSION_PROBE !== "1") {
602
- throw invalidPersistedPlan("test-bypass plans require KIMI_PLUGIN_CC_SKIP_VERSION_PROBE=1 at the final spawn boundary");
603
- }
604
- return;
605
- }
606
- if (plan.kimiVersion === null) {
607
- throw invalidPersistedPlan(`${plan.certification} plans require kimi_version`);
608
- }
609
- const parsed = parseVersionLine(plan.kimiVersion);
610
- if (parsed === undefined) {
611
- throw invalidPersistedPlan("kimi_version is not a parseable semantic version");
612
- }
613
- if (plan.certification === "uncertified" && plan.intendedEngine !== "native-v2") {
614
- throw invalidPersistedPlan("uncertified plans must use native-v2");
615
- }
616
- // Recompute from the release table and the current environment: a strict
617
- // environment, a write operation, or a row that has since been added all
618
- // refuse a plan whose recorded certification no longer matches.
619
- const certification = assertCertifiedCapability(plan.intendedEngine, plan.operationKind, {
620
- kind: "ok",
621
- version: parsed.raw,
622
- major: parsed.major,
623
- minor: parsed.minor,
624
- patch: parsed.patch,
625
- inTestedRange: isInTestedRange(parsed.major, parsed.minor),
626
- }, env);
627
- if (certification !== plan.certification) {
628
- // A plan claiming exact certification for an uncertified release keeps the
629
- // historical capability refusal; any other mismatch is a stale/forged row.
630
- if (plan.certification === "certified")
631
- throw nativeV2NotCertified(plan.operationKind, parsed.raw, env);
632
- throw invalidPersistedPlan(`plan_certification ${JSON.stringify(plan.certification)} does not match ${JSON.stringify(certification)} for kimi-code ${parsed.raw}`);
633
- }
634
- }
635
- function assertExecutionPlanShape(plan) {
636
- const candidate = plan;
637
- if (candidate.schemaVersion !== 1) {
638
- throw invalidPersistedPlan("unsupported schemaVersion");
639
- }
640
- if (typeof candidate.operationKind !== "string" || !isOperationKind(candidate.operationKind)) {
641
- throw invalidPersistedPlan("operationKind is invalid");
642
- }
643
- if (typeof candidate.intendedEngine !== "string" || !isKimiEngine(candidate.intendedEngine)) {
644
- throw invalidPersistedPlan("intendedEngine is invalid");
645
- }
646
- if (typeof candidate.certification !== "string" ||
647
- !isPlanCertification(candidate.certification)) {
648
- throw invalidPersistedPlan("certification is invalid");
649
- }
650
- if (typeof candidate.command !== "string" || candidate.command.length === 0) {
651
- throw invalidPersistedPlan("command is empty or invalid");
652
- }
653
- if (!Array.isArray(candidate.prefixArgs) ||
654
- !candidate.prefixArgs.every((entry) => typeof entry === "string")) {
655
- throw invalidPersistedPlan("prefixArgs is not a string array");
656
- }
657
- if (candidate.kimiVersion !== null && typeof candidate.kimiVersion !== "string") {
658
- throw invalidPersistedPlan("kimiVersion is invalid");
659
- }
660
- if (candidate.resumedFromJobId !== null &&
661
- typeof candidate.resumedFromJobId !== "string") {
662
- throw invalidPersistedPlan("resumedFromJobId is invalid");
663
- }
664
- if (candidate.safetyProfile !== null && candidate.safetyProfile !== NATIVE_V2_SAFETY_PROFILE) {
665
- throw invalidPersistedPlan("safetyProfile is invalid");
666
- }
667
- }
668
- function nativeV2NotCertified(operationKind, version, env = {}) {
669
- const certified = NATIVE_V2_CERTIFIED.get(operationKind) ?? [];
670
- const normalized = version === null ? undefined : parseVersionLine(version)?.raw;
671
- if (normalized !== undefined &&
672
- NATIVE_V2_NON_SWARM_VERSIONS.includes(normalized) &&
673
- (operationKind === "swarm" || operationKind === "swarm-write")) {
674
- return new RuntimeError("KIMI_CAPABILITY_NOT_CERTIFIED", `Refusing ${operationKind}: kimi-code ${normalized} is certified only for non-swarm operations (review, challenge, ask, rescue, review_gate, pursue). Its native rate-limit recovery can exceed AgentSwarm's configured maximum, so this exact release refuses swarm before model spawn. Select exact kimi-code ${formatVersionChoice(kimiVersionsWith("bounded-swarm"))} for this plugin's certified bounded waves and single-worker path, or use a non-swarm operation on ${normalized}.`, "kimi-engine.capability", {
675
- details: {
676
- refusal_kind: "v2-swarm-not-certified",
677
- certification_scope: "non-swarm",
678
- retryable_after_setup: false,
679
- operation_kind: operationKind,
680
- intended_engine: "native-v2",
681
- kimi_version: normalized,
682
- certified_versions: [...certified],
683
- certified_operations: [...NON_SWARM_OPERATION_KINDS],
684
- },
685
- });
686
- }
687
- const observed = version === null ? "an unprobed version" : `kimi-code ${version}`;
688
- const borrowable = normalized !== undefined && resolveKimiRelease(normalized)?.certified === false;
689
- const readOnlyNote = !borrowable
690
- ? ""
691
- : strictVersions(env)
692
- ? ` ${STRICT_VERSIONS_ENV}=1 is set, so read-only operations on this uncertified release also refuse.`
693
- : " Read-only operations (ask, review, challenge, review gate) still run on this uncertified release with a warning; write and swarm operations need an exact certified release.";
694
- return new RuntimeError("KIMI_CAPABILITY_NOT_CERTIFIED", `Refusing ${operationKind} on native v2: ${observed} is not in this operation's exactly certified set [${certified.join(", ")}].${readOnlyNote} Run setup --install-kimi to give the plugin its own copy of the newest certified kimi-code, update kimi-plugin-cc to a release that certifies this version, or point KIMI_PLUGIN_CC_KIMI_BIN at a certified binary. KIMI_PLUGIN_CC_SKIP_VERSION_PROBE is a test/smoke seam, not a production repair path.`, "kimi-engine.capability", {
695
- details: {
696
- refusal_kind: "v2-version-not-certified",
697
- retryable_after_setup: false,
698
- operation_kind: operationKind,
699
- intended_engine: "native-v2",
700
- kimi_version: version,
701
- certified_versions: [...certified],
702
- },
703
- });
704
- }
705
- function invalidPersistedPlan(message, cause) {
706
- return new RuntimeError("KIMI_EXECUTION_PLAN_INVALID", `Persisted kimi execution plan is invalid: ${message}.`, "kimi-engine.persisted-plan", cause instanceof Error ? { cause } : undefined);
707
- }
708
- function sameStrings(left, right) {
709
- return left.length === right.length && left.every((value, index) => value === right[index]);
710
- }
711
- async function resolveExactExecutable(command, cwd, env) {
712
- const candidates = [];
713
- if (command.includes("/") || command.includes("\\")) {
714
- candidates.push(path.isAbsolute(command) ? command : path.resolve(cwd, command));
715
- }
716
- else {
717
- const searchPath = env.PATH ?? process.env.PATH ?? "/usr/local/bin:/usr/bin:/bin";
718
- const extensions = process.platform === "win32"
719
- ? (env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";")
720
- : [""];
721
- for (const entry of searchPath.split(path.delimiter)) {
722
- const directory = entry.length === 0 ? cwd : path.isAbsolute(entry) ? entry : path.resolve(cwd, entry);
723
- for (const extension of extensions) {
724
- candidates.push(path.join(directory, `${command}${extension}`));
725
- }
726
- }
727
- }
728
- for (const candidate of candidates) {
729
- try {
730
- await access(candidate, process.platform === "win32" ? fsConstants.F_OK : fsConstants.X_OK);
731
- return await realpath(candidate);
732
- }
733
- catch {
734
- }
735
- }
736
- throw new RuntimeError("KIMI_EXECUTION_PLAN_UNRESOLVED", `Refusing to spawn kimi because executable ${JSON.stringify(command)} could not be resolved to an exact runnable path. Run Claude Code \`/kimi:setup\` or Codex \`$kimi-setup\` to verify the active binary, or correct KIMI_PLUGIN_CC_KIMI_BIN and retry.`, "kimi-engine.plan", { details: { command, cwd } });
737
- }
738
- function parseProtocolVersion(value) {
739
- const match = value.match(/^(\d+)\.(\d+)(?:\.\d+)?$/);
740
- if (match === null)
741
- return null;
742
- return { major: Number(match[1]), minor: Number(match[2]) };
743
- }
744
- function operationKindFromLogEntry(entry) {
745
- if (typeof entry.operation_kind === "string" && isOperationKind(entry.operation_kind)) {
746
- return entry.operation_kind;
747
- }
748
- const candidate = entry.event === "spawn"
749
- ? entry.command_label
750
- : entry.direction === "meta" && isRecord(entry.message)
751
- ? entry.message.commandType
752
- : null;
753
- if (typeof candidate !== "string")
754
- return null;
755
- // Historical `rescue` labels cover both ordinary rescue and pursue, whose
756
- // write-policy label intentionally reuses rescue. Without the newer explicit
757
- // operation_kind field, choosing either would be a guess.
758
- if (candidate === "rescue")
759
- return null;
760
- return isOperationKind(candidate) ? candidate : null;
761
- }
762
- function isOperationKind(value) {
763
- return OPERATION_KINDS.includes(value);
764
- }
765
- function isKimiEngine(value) {
766
- return value === "legacy-v1" || value === "native-v2";
767
- }
768
- function isPlanCertification(value) {
769
- return value === "certified" || value === "uncertified" || value === "test-bypass";
770
- }