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
@@ -7,9 +7,11 @@
7
7
  // upstream. The probe executes the same bytes that setup writes.
8
8
  import { createHash } from "node:crypto";
9
9
  import { realpathSync } from "node:fs";
10
+ import { lstat, readFile, realpath } from "node:fs/promises";
10
11
  import path from "node:path";
11
12
  import { fileURLToPath } from "node:url";
12
13
  import { RuntimeError } from "../errors.js";
14
+ import { resolvePluginPaths } from "../paths.js";
13
15
  /**
14
16
  * Prefer a stable, non-version-stamped path to the SAME Node binary.
15
17
  * Version-stamped interpreter paths change on upgrade and invalidate the
@@ -61,8 +63,7 @@ export function resolveNodeBinary(env) {
61
63
  * Build the exact shell command string that `/bin/sh -c "<command>"`
62
64
  * needs to spawn the hook. Single source of truth for:
63
65
  *
64
- * - what `/kimi:setup` writes into kimi-code's config
65
- * (`command = "..."` inside [[hooks]])
66
+ * - what setup writes into kimi-code's config (`command = "..."` inside [[hooks]])
66
67
  * - what the shell probe runs via `spawn("/bin/sh", ["-c", ...])`
67
68
  * - what the verifier (`evaluateInstalled`) equality-checks the
68
69
  * installed `command = "..."` against on every command spawn
@@ -84,24 +85,36 @@ export function shellSingleQuote(value) {
84
85
  return `'${value.replaceAll("'", "'\\''")}'`;
85
86
  }
86
87
  /**
87
- * Resolve the absolute path to the compiled hook script.
88
+ * Locate the compiled, self-contained ESM hook artifact bundled with this
89
+ * package (`<packageRoot>/dist/hooks/approval-hook.js`). Setup copies this
90
+ * file into `<pluginRoot>/hook/approval-hook.js`.
91
+ */
92
+ export function resolveBundledHookSourcePath() {
93
+ const here = fileURLToPath(import.meta.url);
94
+ const parts = here.split(path.sep);
95
+ if (parts.length < 3) {
96
+ throw resolveHookFailure(here);
97
+ }
98
+ const tailParent = parts[parts.length - 2];
99
+ const tailGrandparent = parts[parts.length - 3];
100
+ if (tailParent !== "hooks" || (tailGrandparent !== "runtime" && tailGrandparent !== "dist")) {
101
+ throw resolveHookFailure(here);
102
+ }
103
+ const packageRoot = parts.slice(0, parts.length - 3).join(path.sep) || path.sep;
104
+ return path.join(packageRoot, "dist", "hooks", "approval-hook.js");
105
+ }
106
+ /**
107
+ * Resolve the absolute path to the installed hook script.
88
108
  *
89
109
  * Resolution order:
90
- *
91
110
  * 1. `KIMI_PLUGIN_CC_HOOK_SCRIPT` override — tests / advanced users.
92
- * 2. Sibling resolution from this file's URL. This module lives at
93
- * `<root>/{runtime,dist}/hooks/install-paths.{ts,js}`. The hook
94
- * artifact lives at `<root>/dist/hooks/approval-hook.js`. Walk up
95
- * to `<root>` and append the canonical hook artifact path.
111
+ * 2. Canonical shared plugin hook path: `<pluginRoot>/hook/approval-hook.js`
112
+ * (e.g. `~/.local/share/kimi-plugin-cc/hook/approval-hook.js`).
96
113
  */
97
114
  export function resolveHookScriptPath(env) {
98
115
  const override = env.KIMI_PLUGIN_CC_HOOK_SCRIPT;
99
116
  if (override !== undefined && override.length > 0) {
100
117
  if (!path.isAbsolute(override)) {
101
- // The hook shell's cwd can differ from the companion's. Require an
102
- // absolute override so installation, verification, and the runtime spawn
103
- // all refer to the same file rather than blessing a relative path that
104
- // resolves elsewhere when kimi-code executes it.
105
118
  throw new RuntimeError("SETUP_HOOK_SCRIPT_NOT_ABSOLUTE", [
106
119
  `KIMI_PLUGIN_CC_HOOK_SCRIPT must be an absolute path; got ${JSON.stringify(override)}.`,
107
120
  `kimi-code spawns hooks via /bin/sh -c with a cwd that may differ from the companion's.`,
@@ -110,21 +123,7 @@ export function resolveHookScriptPath(env) {
110
123
  }
111
124
  return override;
112
125
  }
113
- const here = fileURLToPath(import.meta.url);
114
- const parts = here.split(path.sep);
115
- // Pin to the canonical suffix `{runtime|dist}/hooks/install-paths.{ts,js}`
116
- // — anchoring to a specific tail keeps ancestor directories named
117
- // "runtime" or "dist" from confusing the lookup.
118
- if (parts.length < 3) {
119
- throw resolveHookFailure(here);
120
- }
121
- const tailParent = parts[parts.length - 2];
122
- const tailGrandparent = parts[parts.length - 3];
123
- if (tailParent !== "hooks" || (tailGrandparent !== "runtime" && tailGrandparent !== "dist")) {
124
- throw resolveHookFailure(here);
125
- }
126
- const pluginRoot = parts.slice(0, parts.length - 3).join(path.sep) || path.sep;
127
- return path.join(pluginRoot, "dist", "hooks", "approval-hook.js");
126
+ return resolvePluginPaths(env).hookScriptPath;
128
127
  }
129
128
  function resolveHookFailure(here) {
130
129
  return new RuntimeError("SETUP_RESOLVE_HOOK_FAILED", `Could not infer plugin root from install-paths module path ${here}. Set KIMI_PLUGIN_CC_HOOK_SCRIPT to the absolute path of dist/hooks/approval-hook.js.`, "setup.resolve-hook", { details: { here } });
@@ -287,7 +286,7 @@ export function describeHookCommandDrift(installedCommand, expectedCommand, node
287
286
  parts.push(`Hook script path drift: the installed hook points at ${installed.hookScript}, but this companion's ` +
288
287
  `hook is ${expected.hookScript} (likely a plugin update or move — the install path is version-stamped).`);
289
288
  }
290
- parts.push("Run /kimi:setup to re-pin the managed block to this companion's current paths.");
289
+ parts.push("Run `kimi_setup` (action \"install\") or `scripts/companion.sh setup` to re-pin the managed block to this companion's current paths.");
291
290
  return parts.join(" ");
292
291
  }
293
292
  /**
@@ -337,80 +336,17 @@ export function tryBuildExpectedHookCommand(env) {
337
336
  };
338
337
  }
339
338
  }
340
- // ----- Host identity -----------------------------------------------------
341
- //
342
- // Claude Code and Codex install this plugin to DIFFERENT, version-stamped,
343
- // host-specific paths but SHARE one `~/.kimi-code/config.toml`:
344
- //
345
- // Claude: ~/.claude/plugins/cache/kimi-marketplace/kimi/<ver>/dist/hooks/approval-hook.js
346
- // Codex: ~/.codex/plugins/cache/kimi-marketplace/kimi/<ver>/dist/hooks/approval-hook.js
347
- //
348
- // The managed block is host-scoped (marker suffix `:<host-id>`) so each host
349
- // owns and verifies its OWN PreToolUse block without clobbering the other's.
350
- // The host id must be VERSION-INDEPENDENT so a plugin upgrade REFRESHES the
351
- // same host's block instead of accumulating one block per version.
352
- /**
353
- * Resolve a stable, version-independent host id for the managed-block marker.
354
- *
355
- * Order: an explicit `KIMI_PLUGIN_CC_HOST_ID` override (slugified) wins — used
356
- * by tests and the live-repair path. Otherwise derive from the resolved hook
357
- * script path. Pass the already-resolved `hookScriptPath` when you have it
358
- * (the verifier + setup do) so we don't re-resolve and risk a second throw.
359
- */
360
- export function resolveHostId(env, hookScriptPath) {
361
- const override = env.KIMI_PLUGIN_CC_HOST_ID;
362
- if (override !== undefined && override.trim().length > 0) {
363
- return slugifyHostId(override);
364
- }
365
- const resolved = hookScriptPath ?? resolveHookScriptPath(env);
366
- return hostIdFromHookScript(resolved);
367
- }
368
- /**
369
- * Derive a host id from a hook-script path. `~/.claude/...` → `claude-code`,
370
- * `~/.codex/...` → `codex` (both literal + version-independent), else a stable
371
- * `host-<sha1(hookDir)[:16]>` for dev checkouts / unrecognized layouts.
372
- */
373
- export function hostIdFromHookScript(hookScriptPath) {
374
- const norm = hookScriptPath.split(path.sep).join("/");
375
- if (norm.includes("/.claude/"))
376
- return "claude-code";
377
- if (norm.includes("/.codex/"))
378
- return "codex";
379
- // Hashing the hook directory is stable for dev checkouts. Sixteen hex chars
380
- // avoid the collision risk of a 32-bit prefix, which could make one root
381
- // treat another root's managed block as its own.
382
- const digest = createHash("sha1").update(path.dirname(hookScriptPath)).digest("hex");
383
- return `host-${digest.slice(0, 16)}`;
384
- }
339
+ const OWNED_APPROVAL_HOOK_PATTERNS = [
340
+ /\/kimi-plugin-cc\/hook\/approval-hook\.js$/,
341
+ /\/kimi-plugin-cc\/(?:dist|runtime)\/hooks\/approval-hook\.js$/,
342
+ /\/plugins\/kimi-codex\/dist\/hooks\/approval-hook\.js$/,
343
+ /\/dsh-kimi-plugin-cc\/dist\/hooks\/approval-hook\.js$/,
344
+ /\/kimi-marketplace\/[^/]+\/[^/]+\/(?:dist|runtime)\/hooks\/approval-hook\.js$/,
345
+ /\/\.gemini\/config\/plugins\/kimi\/(?:dist|runtime)\/hooks\/approval-hook\.js$/,
346
+ ];
385
347
  /**
386
- * Normalize an arbitrary host-id override into the `[a-z0-9-]+` slug the
387
- * marker regex accepts. Empty results fall back to `host` so the marker is
388
- * always well-formed.
389
- */
390
- export function slugifyHostId(value) {
391
- const slug = value
392
- .trim()
393
- .toLowerCase()
394
- .replace(/[^a-z0-9]+/g, "-")
395
- .replace(/^-+|-+$/g, "")
396
- .slice(0, 40);
397
- return slug.length > 0 ? slug : "host";
398
- }
399
- /**
400
- * Derive the owning host id from a canonical hook command's script path.
401
- * Returns `null` for a non-canonical command whose owner cannot be determined;
402
- * scoped cleanup treats that as ambiguous rather than claiming another host's
403
- * block.
404
- */
405
- export function hostIdFromHookCommand(command) {
406
- const parsed = parseHookShellCommand(command);
407
- if (parsed === null)
408
- return null;
409
- return hostIdFromHookScript(parsed.hookScript);
410
- }
411
- /**
412
- * True only for a canonical two-single-quoted-token command whose script is
413
- * `approval-hook.js` under a kimi-plugin-cc/dsh-kimi-plugin-cc/kimi-marketplace install tree.
348
+ * True only for a canonical two-single-quoted-token command whose script
349
+ * matches an exact kimi-plugin-cc install/published approval-hook path.
414
350
  * This strict ownership test protects hand-rolled and non-canonical user hooks
415
351
  * from orphan pruning.
416
352
  */
@@ -418,11 +354,147 @@ export function isOurApprovalHookCommand(decodedCommand) {
418
354
  const parsed = parseHookShellCommand(decodedCommand);
419
355
  if (parsed === null)
420
356
  return false;
421
- const script = parsed.hookScript.split(/[\\/]/);
422
- if (script[script.length - 1] !== "approval-hook.js")
423
- return false;
424
357
  const normalized = parsed.hookScript.replace(/\\/g, "/");
425
- // Require a real path segment, not an arbitrary substring, so a user hook in
426
- // a similarly named wrapper directory is never misclassified and pruned.
427
- return normalized.includes("/kimi-plugin-cc/") || normalized.includes("/dsh-kimi-plugin-cc/") || normalized.includes("/kimi-marketplace/");
358
+ return OWNED_APPROVAL_HOOK_PATTERNS.some((pattern) => pattern.test(normalized));
359
+ }
360
+ /**
361
+ * Verify the managed `<pluginRoot>/hook/approval-hook.js` artifact on disk:
362
+ * - Reject symlinks or non-directory/non-regular files along `pluginRoot`, `hookDir`,
363
+ * and `hookScriptPath`, and require canonical containment under `pluginRoot/hook`.
364
+ * - Require SHA-256 byte equality against the bundled `dist/hooks/approval-hook.js`.
365
+ * Skipped when `KIMI_PLUGIN_CC_HOOK_SCRIPT` explicitly overrides the script path.
366
+ */
367
+ export async function verifyManagedHookArtifact(env, hookScriptPath) {
368
+ if (env.KIMI_PLUGIN_CC_HOOK_SCRIPT && env.KIMI_PLUGIN_CC_HOOK_SCRIPT.length > 0) {
369
+ return { ok: true };
370
+ }
371
+ const paths = resolvePluginPaths(env);
372
+ let pluginRootReal;
373
+ try {
374
+ const info = await lstat(paths.pluginRoot);
375
+ if (info.isSymbolicLink() || !info.isDirectory()) {
376
+ return {
377
+ ok: false,
378
+ kind: "unsafe-path",
379
+ reason: `plugin data directory ${paths.pluginRoot} is a symlink or not a directory; refusing to trust managed hook`,
380
+ };
381
+ }
382
+ pluginRootReal = await realpath(paths.pluginRoot);
383
+ }
384
+ catch (err) {
385
+ if (err.code === "ENOENT") {
386
+ return {
387
+ ok: false,
388
+ kind: "missing",
389
+ reason: `hook script ${hookScriptPath} is missing or unreadable — run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to reinstall`,
390
+ };
391
+ }
392
+ return {
393
+ ok: false,
394
+ kind: "unsafe-path",
395
+ reason: `unable to inspect plugin data directory ${paths.pluginRoot}: ${err.message}`,
396
+ };
397
+ }
398
+ let hookDirReal;
399
+ try {
400
+ const info = await lstat(paths.hookDir);
401
+ if (info.isSymbolicLink() || !info.isDirectory()) {
402
+ return {
403
+ ok: false,
404
+ kind: "unsafe-path",
405
+ reason: `hook directory ${paths.hookDir} is a symlink or not a directory; refusing to trust managed hook`,
406
+ };
407
+ }
408
+ hookDirReal = await realpath(paths.hookDir);
409
+ }
410
+ catch (err) {
411
+ if (err.code === "ENOENT") {
412
+ return {
413
+ ok: false,
414
+ kind: "missing",
415
+ reason: `hook script ${hookScriptPath} is missing or unreadable — run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to reinstall`,
416
+ };
417
+ }
418
+ return {
419
+ ok: false,
420
+ kind: "unsafe-path",
421
+ reason: `unable to inspect hook directory ${paths.hookDir}: ${err.message}`,
422
+ };
423
+ }
424
+ const expectedHookDirReal = path.join(pluginRootReal, "hook");
425
+ if (hookDirReal !== expectedHookDirReal) {
426
+ return {
427
+ ok: false,
428
+ kind: "unsafe-path",
429
+ reason: `hook directory ${paths.hookDir} resolves to ${hookDirReal}, outside ${expectedHookDirReal}; refusing to trust managed hook`,
430
+ };
431
+ }
432
+ try {
433
+ const info = await lstat(hookScriptPath);
434
+ if (info.isSymbolicLink() || !info.isFile()) {
435
+ return {
436
+ ok: false,
437
+ kind: "unsafe-path",
438
+ reason: `hook script ${hookScriptPath} is a symlink or not a regular file; refusing to execute managed hook`,
439
+ };
440
+ }
441
+ const scriptReal = await realpath(hookScriptPath);
442
+ const expectedScriptReal = path.join(hookDirReal, path.basename(hookScriptPath));
443
+ if (scriptReal !== expectedScriptReal) {
444
+ return {
445
+ ok: false,
446
+ kind: "unsafe-path",
447
+ reason: `hook script ${hookScriptPath} resolves to ${scriptReal}, outside ${hookDirReal}; refusing to execute managed hook`,
448
+ };
449
+ }
450
+ }
451
+ catch (err) {
452
+ if (err.code === "ENOENT") {
453
+ return {
454
+ ok: false,
455
+ kind: "missing",
456
+ reason: `hook script ${hookScriptPath} is missing or unreadable — run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to reinstall`,
457
+ };
458
+ }
459
+ return {
460
+ ok: false,
461
+ kind: "unsafe-path",
462
+ reason: `unable to inspect hook script ${hookScriptPath}: ${err.message}`,
463
+ };
464
+ }
465
+ let installedBytes;
466
+ try {
467
+ installedBytes = await readFile(hookScriptPath);
468
+ }
469
+ catch {
470
+ return {
471
+ ok: false,
472
+ kind: "missing",
473
+ reason: `hook script ${hookScriptPath} is missing or unreadable — run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to reinstall`,
474
+ };
475
+ }
476
+ let sourcePath;
477
+ let bundledBytes;
478
+ try {
479
+ sourcePath = resolveBundledHookSourcePath();
480
+ bundledBytes = await readFile(sourcePath);
481
+ }
482
+ catch (err) {
483
+ return {
484
+ ok: false,
485
+ kind: "bundle-missing",
486
+ reason: `bundled hook source is missing or unreadable (${err.message}); run \`bun run build\` or reinstall the plugin`,
487
+ };
488
+ }
489
+ const installedHash = createHash("sha256").update(installedBytes).digest("hex");
490
+ const bundledHash = createHash("sha256").update(bundledBytes).digest("hex");
491
+ if (installedHash !== bundledHash) {
492
+ return {
493
+ ok: false,
494
+ kind: "sha256-mismatch",
495
+ reason: `installed hook script ${hookScriptPath} does not match the bundled hook artifact (${sourcePath}, SHA-256 mismatch — likely after a plugin update). ` +
496
+ `Run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to republish it.`,
497
+ };
498
+ }
499
+ return { ok: true };
428
500
  }
@@ -1,11 +1,13 @@
1
1
  import { constants as fsConstants, existsSync } from "node:fs";
2
2
  import { access, readFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
4
5
  import { validateKimiHookSetForEnvironment } from "./config-safety.js";
5
- import { evaluateInstalled } from "./managed-block.js";
6
- import { resolveHostId, tryBuildExpectedHookCommand } from "./install-paths.js";
7
- import { resolveKimiHome } from "../kimi-home.js";
8
- export async function verifyHookInstalled(env) {
6
+ import { countCleanEntriesInFile, evaluateInstalled, findShortHookTimeout, } from "./managed-block.js";
7
+ import { tryBuildExpectedHookCommand, verifyManagedHookArtifact, } from "./install-paths.js";
8
+ import { RuntimeError } from "../errors.js";
9
+ import { resolveKimiCodeConfigPath } from "../kimi-home.js";
10
+ export async function verifyHookInstalled(env, options = {}) {
9
11
  const configPath = resolveKimiCodeConfigPath(env);
10
12
  if (env.KIMI_PLUGIN_CC_SKIP_HOOK_CHECK === "1") {
11
13
  return { installed: true, configPath };
@@ -42,22 +44,32 @@ export async function verifyHookInstalled(env) {
42
44
  configPath,
43
45
  };
44
46
  }
45
- const hookSet = await validateKimiHookSetForEnvironment(raw, env);
47
+ const hookSet = await validateKimiHookSetForEnvironment(raw, env, options.kimiVersion);
46
48
  if (!hookSet.valid) {
47
49
  return {
48
50
  installed: false,
49
51
  reason: hookSet.reason ?? "configured hooks failed whole-array validation",
50
52
  configPath,
53
+ hookSetInvalid: true,
51
54
  };
52
55
  }
53
- // Classify command mismatches into actionable drift reasons without changing
54
- // the installed=false decision. hostId isolates this host's block in the
55
- // shared config.
56
56
  const check = evaluateInstalled(raw, expected.command, {
57
- hostId: resolveHostId(env, expected.hookScriptPath),
58
57
  nodeExists: (binPath) => existsSync(binPath),
59
58
  });
60
- if (!check.installed) {
59
+ // Markers are bookkeeping, not the security boundary. At job time a
60
+ // duplicate or orphan marker is tolerated when the real parser finds a clean,
61
+ // unconditional PreToolUse entry with the byte-exact command (the whole hook
62
+ // set already validated above), and the job records the setup remedy. setup
63
+ // and setup --check use evaluateInstalled directly and keep failing.
64
+ const warnings = [];
65
+ if (!check.installed &&
66
+ (check.state.kind === "duplicate" || check.state.kind === "orphan") &&
67
+ countCleanEntriesInFile(raw, expected.command) >= 1 &&
68
+ // A short-timeout copy of the command can be the one kimi-code runs.
69
+ findShortHookTimeout(raw, expected.command) === null) {
70
+ warnings.push(`The managed kimi-code PreToolUse hook is installed and enforcing, but its config markers need repair: ${check.reason}`);
71
+ }
72
+ else if (!check.installed) {
61
73
  return {
62
74
  installed: false,
63
75
  reason: check.reason,
@@ -69,40 +81,37 @@ export async function verifyHookInstalled(env) {
69
81
  ...(check.drift !== undefined ? { drift: check.drift } : {}),
70
82
  };
71
83
  }
72
- // A byte-exact command does not prove the hook can run. Confirm the script
73
- // remains readable and fail closed on every access error.
84
+ const artifact = await verifyManagedHookArtifact(env, expected.hookScriptPath);
85
+ if (!artifact.ok) {
86
+ if (artifact.kind === "sha256-mismatch") {
87
+ return {
88
+ installed: false,
89
+ reason: artifact.reason,
90
+ configPath,
91
+ drift: {
92
+ axis: "hook-script",
93
+ installedCommand: expected.command,
94
+ expectedCommand: expected.command,
95
+ nodeInterpreterUnchanged: true,
96
+ },
97
+ };
98
+ }
99
+ return {
100
+ installed: false,
101
+ reason: artifact.reason,
102
+ configPath,
103
+ };
104
+ }
74
105
  try {
75
106
  await access(expected.hookScriptPath, fsConstants.R_OK);
76
107
  }
77
108
  catch {
78
109
  return {
79
110
  installed: false,
80
- reason: `hook script ${expected.hookScriptPath} is missing or unreadable — run /kimi:setup to reinstall`,
111
+ reason: `hook script ${expected.hookScriptPath} is missing or unreadable — run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to reinstall`,
81
112
  configPath,
82
113
  };
83
114
  }
84
- // Confirm the pinned interpreter with a NARROW X_OK backstop. Interpreter
85
- // moves that change the recomputed command are already refused as drift; this
86
- // check covers only unchanged command bytes whose Node token fails X_OK now:
87
- //
88
- // 1. An absolute KIMI_PLUGIN_CC_NODE_BIN override names a dead interpreter
89
- // other than the running process, so launch never proved it executable.
90
- // 2. The interpreter is chmodded after launch. realpathSync still resolves
91
- // it, the preferred path and command bytes stay unchanged, but X_OK fails.
92
- // 3. The pathname is removed after launch. realpathSync throws, and the
93
- // execPath fallback can retain the same spelling, so equality still passes
94
- // while the pinned path is gone.
95
- //
96
- // Refusing these cases matters because a failed hook spawn is ALLOW upstream.
97
- // This per-spawn check also catches degradation since setup's last execution
98
- // probe. It proves only the exec bit, not a working Node, architecture, or
99
- // version; setup's shell probe executes the byte-identical command instead.
100
- //
101
- // Fail closed on EVERY access error. Do not harmonize this with spawning code
102
- // that ignores selected errnos: a security verifier cannot "try and see".
103
- //
104
- // This refusal carries no drift and therefore no retryable_after_setup field:
105
- // setup cannot repair a broken Node installation.
106
115
  try {
107
116
  await access(expected.nodeBin, fsConstants.X_OK);
108
117
  }
@@ -111,27 +120,23 @@ export async function verifyHookInstalled(env) {
111
120
  installed: false,
112
121
  reason: `hook interpreter ${expected.nodeBin} is missing or not executable, so the PreToolUse hook ` +
113
122
  `cannot spawn (kimi-code reads a failed hook as ALLOW). Repair the Node install, or set ` +
114
- `KIMI_PLUGIN_CC_NODE_BIN to a valid Node >=22.5 executable and run /kimi:setup.`,
123
+ `KIMI_PLUGIN_CC_NODE_BIN to a valid Node >=22.5 executable and run \`kimi_setup\` (action "install").`,
115
124
  configPath,
116
125
  refusalKind: "node-bin-not-executable",
117
126
  nodeBin: expected.nodeBin,
118
127
  };
119
128
  }
120
- return { installed: true, configPath };
121
- }
122
- function resolveKimiCodeConfigPath(env) {
123
- return path.join(resolveKimiHome(env), "config.toml");
129
+ return warnings.length > 0 ? { installed: true, configPath, warnings } : { installed: true, configPath };
124
130
  }
125
131
  /**
126
132
  * Caller-facing repair protocol shared by every hook-refusal surface. Re-pin
127
- * through the resolved plugin root, retry ONCE only when
128
- * `retryable_after_setup` is literally true and setup exits zero, and never use
129
- * the skip-check environment variable as a repair.
133
+ * cleanly, retry ONCE only when `retryable_after_setup` is literally true and
134
+ * setup exits zero, and never use the skip-check environment variable as a repair.
130
135
  */
131
- export function hookRefusalRetryProtocol(env) {
132
- const root = env.CLAUDE_PLUGIN_ROOT ?? env.PLUGIN_ROOT ?? "${CLAUDE_PLUGIN_ROOT}";
136
+ export function hookRefusalRetryProtocol(_env) {
137
+ const hereRoot = path.resolve(fileURLToPath(new URL("../..", import.meta.url)));
133
138
  return (`If details shows retryable_after_setup:true (routine after a plugin update — the ` +
134
- `version-stamped hook path moved), run \`${root}/scripts/companion.sh setup\` and, only ` +
139
+ `hook script or node path moved), run \`${hereRoot}/scripts/companion.sh setup\` and, only ` +
135
140
  `if it exits 0, retry the refused command once. Anything else — or a second refusal — ` +
136
141
  `surface the refusal instead of retrying.`);
137
142
  }
@@ -161,6 +166,75 @@ export function hookRefusalDetails(status) {
161
166
  : {}),
162
167
  };
163
168
  }
169
+ /** Repair sentences of the read-only (ask, review, challenge) hook refusal. */
170
+ export const READ_ONLY_HOOK_REPAIR = [
171
+ "Run `kimi_setup` (action \"install\") or `scripts/companion.sh setup` to install or repair the canonical managed block.",
172
+ "Set KIMI_PLUGIN_CC_SKIP_HOOK_CHECK=1 only if you intentionally accept un-enforced execution.",
173
+ ];
174
+ /** Repair sentences of the write-capable (rescue, pursue) hook refusal. */
175
+ export const WRITE_HOOK_REPAIR = [
176
+ "Repair by running `kimi_setup` (action \"install\") or `scripts/companion.sh setup`, then retry.",
177
+ "KIMI_PLUGIN_CC_SKIP_HOOK_CHECK=1 is only for deliberate tests or diagnostics.",
178
+ ];
179
+ function readOnlyGate(name, code) {
180
+ return {
181
+ code,
182
+ stage: `${name}.hook-check`,
183
+ warnLabel: name,
184
+ intro: [`${name} refuses to run without the canonical kimi-plugin-cc PreToolUse hook.`],
185
+ repair: READ_ONLY_HOOK_REPAIR,
186
+ };
187
+ }
188
+ const SWARM_GATE = {
189
+ code: "SWARM_HOOK_NOT_INSTALLED",
190
+ stage: "swarm.hook-check",
191
+ warnLabel: "swarm",
192
+ intro: [
193
+ "swarm refuses to run without the kimi-plugin-cc PreToolUse hook.",
194
+ "The hook enforces each worker's tool permissions, write confinement, and delegation limits.",
195
+ ],
196
+ repair: ["Repair by running `kimi_setup` (action \"install\") or `scripts/companion.sh setup`, then retry."],
197
+ };
198
+ /**
199
+ * The refusal each operation's hook gate raises. The command gates and the
200
+ * job-time check in prepareKimiExecutionPlan share it, so a definite negative
201
+ * reads the same whichever check sees it.
202
+ */
203
+ const HOOK_GATES = {
204
+ ask: readOnlyGate("ask", "ASK_HOOK_NOT_INSTALLED"),
205
+ review: readOnlyGate("review", "REVIEW_HOOK_NOT_INSTALLED"),
206
+ challenge: readOnlyGate("challenge", "CHALLENGE_HOOK_NOT_INSTALLED"),
207
+ rescue: {
208
+ code: "RESCUE_HOOK_NOT_INSTALLED",
209
+ stage: "rescue.hook-check",
210
+ warnLabel: "rescue",
211
+ intro: ["rescue refuses to run without the kimi-plugin-cc PreToolUse hook."],
212
+ repair: WRITE_HOOK_REPAIR,
213
+ },
214
+ pursue: {
215
+ code: "PURSUE_HOOK_NOT_INSTALLED",
216
+ stage: "pursue.hook-check",
217
+ warnLabel: "rescue",
218
+ intro: ["pursue refuses to run without the kimi-plugin-cc PreToolUse hook."],
219
+ repair: WRITE_HOOK_REPAIR,
220
+ },
221
+ swarm: SWARM_GATE,
222
+ "swarm-write": SWARM_GATE,
223
+ };
224
+ /**
225
+ * Warn once on stderr and build `operation`'s hook refusal for a status whose
226
+ * `installed` is false.
227
+ */
228
+ export function hookGateRefusal(operation, status, env, stderr = process.stderr) {
229
+ const gate = HOOK_GATES[operation];
230
+ maybeWarnHookMissing(status, gate.warnLabel, stderr);
231
+ return new RuntimeError(gate.code, [
232
+ ...gate.intro,
233
+ `Hook check failed: ${status.reason ?? "unknown"}.`,
234
+ ...gate.repair,
235
+ hookRefusalRetryProtocol(env),
236
+ ].join(" "), gate.stage, { details: hookRefusalDetails(status) });
237
+ }
164
238
  export function formatHookMissingWarning(status, commandLabel) {
165
239
  return [
166
240
  "",
@@ -174,14 +248,13 @@ export function formatHookMissingWarning(status, commandLabel) {
174
248
  " documented as read-only.",
175
249
  "",
176
250
  " This command will not start a Kimi model run until enforcement is",
177
- " repaired (the review gate skips instead of blocking stop).",
251
+ " repaired.",
178
252
  "",
179
- " Fix: run Claude Code `/kimi:setup` or Codex `$kimi-setup` to install",
180
- " or repair this host's managed block in",
181
- " ~/.kimi-code/config.toml. If you use nvm, asdf, mise, or fnm, you",
182
- " must re-run `/kimi:setup` after any Node version switch — the",
183
- " verifier pins the absolute Node binary path and a switch invalidates",
184
- " the previously-installed block by design. See docs/safety.md.",
253
+ " Fix: run `kimi_setup` (action \"install\") or `scripts/companion.sh setup`",
254
+ " to install or repair the canonical managed block in ~/.kimi-code/config.toml.",
255
+ " If you use nvm, asdf, mise, or fnm, re-run setup after any Node version",
256
+ " switch — the verifier pins the absolute Node binary path and a switch",
257
+ " invalidates the previously-installed block by design. See docs/safety.md.",
185
258
  "",
186
259
  " KIMI_PLUGIN_CC_SKIP_HOOK_CHECK=1 explicitly bypasses hook-verification",
187
260
  " refusals and restores un-enforced `permission: auto` execution. It does",