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,405 +0,0 @@
1
- import { constants as fsConstants, existsSync } from "node:fs";
2
- import { access, open, readFile } from "node:fs/promises";
3
- import path from "node:path";
4
- import { validateKimiHookSetForEnvironment } from "./config-safety.js";
5
- import { countCleanEntriesInFile, effectiveHost, evaluateInstalled, findShortHookTimeout, findUnmanagedApprovalHookBlocks, parseManagedBlock, } from "./managed-block.js";
6
- import { hostIdFromHookCommand, parseHookShellCommand, resolveHostId, tryBuildExpectedHookCommand, } from "./install-paths.js";
7
- import { RuntimeError } from "../errors.js";
8
- import { resolveKimiCodeConfigPath } from "../kimi-home.js";
9
- import { KIMI_PLUGIN_CC_VERSION } from "../version.js";
10
- export async function verifyHookInstalled(env, options = {}) {
11
- const configPath = resolveKimiCodeConfigPath(env);
12
- if (env.KIMI_PLUGIN_CC_SKIP_HOOK_CHECK === "1") {
13
- return { installed: true, configPath };
14
- }
15
- // Canonical expected shell command for the current env. If this
16
- // can't be resolved (KIMI_PLUGIN_CC_NODE_BIN not absolute,
17
- // install-paths module can't infer plugin root, etc.) treat the hook
18
- // as un-verifiable — installed=false with a structured reason. The
19
- // caller's stderr warning surfaces the underlying error code.
20
- const expected = tryBuildExpectedHookCommand(env);
21
- if ("error" in expected) {
22
- return {
23
- installed: false,
24
- reason: `unable to resolve canonical hook command for this companion: ${expected.error.message}`,
25
- configPath,
26
- };
27
- }
28
- let raw;
29
- try {
30
- raw = await readFile(configPath, "utf8");
31
- }
32
- catch (err) {
33
- const code = err.code;
34
- if (code === "ENOENT") {
35
- return {
36
- installed: false,
37
- reason: "kimi-code config file does not exist",
38
- configPath,
39
- };
40
- }
41
- return {
42
- installed: false,
43
- reason: `failed to read kimi-code config: ${err.message}`,
44
- configPath,
45
- };
46
- }
47
- const hookSet = await validateKimiHookSetForEnvironment(raw, env, options.kimiVersion);
48
- if (!hookSet.valid) {
49
- return {
50
- installed: false,
51
- reason: hookSet.reason ?? "configured hooks failed whole-array validation",
52
- configPath,
53
- hookSetInvalid: true,
54
- };
55
- }
56
- // Classify command mismatches into actionable drift reasons without changing
57
- // the installed=false decision. hostId isolates this host's block in the
58
- // shared config.
59
- const hostId = resolveHostId(env, expected.hookScriptPath);
60
- const check = evaluateInstalled(raw, expected.command, {
61
- hostId,
62
- nodeExists: (binPath) => existsSync(binPath),
63
- });
64
- // Markers are bookkeeping, not the security boundary. At job time a
65
- // duplicate or orphan marker is tolerated when the real parser finds a clean,
66
- // unconditional PreToolUse entry with the byte-exact command (the whole hook
67
- // set already validated above), and the job records the setup remedy. setup
68
- // and setup --check use evaluateInstalled directly and keep failing.
69
- const warnings = [];
70
- if (!check.installed &&
71
- (check.state.kind === "duplicate" || check.state.kind === "orphan") &&
72
- countCleanEntriesInFile(raw, expected.command) >= 1 &&
73
- // A short-timeout copy of the command can be the one kimi-code runs.
74
- findShortHookTimeout(raw, expected.command) === null) {
75
- warnings.push(`The managed kimi-code PreToolUse hook is installed and enforcing, but its config markers need repair: ${check.reason}`);
76
- }
77
- else if (!check.installed) {
78
- return {
79
- installed: false,
80
- reason: check.reason,
81
- configPath,
82
- // Forwarded verbatim. Present only for a parseable path/binary mismatch;
83
- // the earlier short-circuits above (unresolvable command, unreadable
84
- // config, invalid hook set) return before this point and therefore never
85
- // carry drift — those are not re-pin-recoverable.
86
- ...(check.drift !== undefined ? { drift: check.drift } : {}),
87
- };
88
- }
89
- // A byte-exact command does not prove the hook can run. Confirm the script
90
- // remains readable and fail closed on every access error.
91
- try {
92
- await access(expected.hookScriptPath, fsConstants.R_OK);
93
- }
94
- catch {
95
- return {
96
- installed: false,
97
- reason: `hook script ${expected.hookScriptPath} is missing or unreadable — run /kimi:setup to reinstall`,
98
- configPath,
99
- };
100
- }
101
- // Confirm the pinned interpreter with a NARROW X_OK backstop. Interpreter
102
- // moves that change the recomputed command are already refused as drift; this
103
- // check covers only unchanged command bytes whose Node token fails X_OK now:
104
- //
105
- // 1. An absolute KIMI_PLUGIN_CC_NODE_BIN override names a dead interpreter
106
- // other than the running process, so launch never proved it executable.
107
- // 2. The interpreter is chmodded after launch. realpathSync still resolves
108
- // it, the preferred path and command bytes stay unchanged, but X_OK fails.
109
- // 3. The pathname is removed after launch. realpathSync throws, and the
110
- // execPath fallback can retain the same spelling, so equality still passes
111
- // while the pinned path is gone.
112
- //
113
- // Refusing these cases matters because a failed hook spawn is ALLOW upstream.
114
- // This per-spawn check also catches degradation since setup's last execution
115
- // probe. It proves only the exec bit, not a working Node, architecture, or
116
- // version; setup's shell probe executes the byte-identical command instead.
117
- //
118
- // Fail closed on EVERY access error. Do not harmonize this with spawning code
119
- // that ignores selected errnos: a security verifier cannot "try and see".
120
- //
121
- // This refusal carries no drift and therefore no retryable_after_setup field:
122
- // setup cannot repair a broken Node installation.
123
- try {
124
- await access(expected.nodeBin, fsConstants.X_OK);
125
- }
126
- catch {
127
- return {
128
- installed: false,
129
- reason: `hook interpreter ${expected.nodeBin} is missing or not executable, so the PreToolUse hook ` +
130
- `cannot spawn (kimi-code reads a failed hook as ALLOW). Repair the Node install, or set ` +
131
- `KIMI_PLUGIN_CC_NODE_BIN to a valid Node >=22.5 executable and run /kimi:setup.`,
132
- configPath,
133
- refusalKind: "node-bin-not-executable",
134
- nodeBin: expected.nodeBin,
135
- };
136
- }
137
- // Channel W notice: an older host hook still applies its own rules here.
138
- warnings.push(...await olderHostHookWarnings(raw, hostId, expected.command));
139
- return warnings.length > 0 ? { installed: true, configPath, warnings } : { installed: true, configPath };
140
- }
141
- const MANIFEST_WALK_LEVELS = 4;
142
- const MANIFEST_MAX_BYTES = 64 * 1024;
143
- const SEMVER_RE = /^(\d+)\.(\d+)\.(\d+)(-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;
144
- /**
145
- * One launch warning per other host's kimi-plugin-cc hook from an older plugin
146
- * version than this one (or whose version cannot be read). kimi-code runs every
147
- * host's hook for every tool call and any deny wins, so that hook's older rules
148
- * still apply to this job. Covers other hosts' managed blocks and their
149
- * marker-less tables left by a config rewrite. Reads manifests only; never
150
- * executes anything and never refuses.
151
- */
152
- export async function olderHostHookWarnings(contents, ownHostId, ownCommand) {
153
- const ownDerived = hostIdFromHookCommand(ownCommand);
154
- const ownIds = new Set([ownHostId, ...(ownDerived !== null ? [ownDerived] : [])]);
155
- const others = new Map();
156
- for (const block of parseManagedBlock(contents, ownHostId).blocks) {
157
- const host = effectiveHost(block, ownHostId);
158
- if (ownIds.has(host) || block.commandPath.length === 0 || block.commandPath === ownCommand)
159
- continue;
160
- if (!others.has(block.commandPath))
161
- others.set(block.commandPath, host);
162
- }
163
- for (const table of findUnmanagedApprovalHookBlocks(contents)) {
164
- const host = hostIdFromHookCommand(table.command);
165
- if (host === null || ownIds.has(host) || table.command === ownCommand)
166
- continue;
167
- if (!others.has(table.command))
168
- others.set(table.command, host);
169
- }
170
- const warnings = [];
171
- for (const [command, host] of others) {
172
- const version = await hookPluginVersion(command);
173
- if (version !== undefined && !pluginVersionIsOlder(version))
174
- continue;
175
- warnings.push(`The ${host} host's kimi-plugin-cc hook (${version === undefined ? "an unknown version" : `version ${version}`}) ` +
176
- `also checks this job with its older rules; it can deny what this version allows. ` +
177
- `Update the plugin in ${host} and run its setup.`);
178
- }
179
- return warnings;
180
- }
181
- /**
182
- * The plugin version behind a canonical hook command: walk up a few levels
183
- * from the hook script to the nearest named `package.json` (it must be
184
- * `kimi-plugin-cc`) or a Codex install's `.codex-plugin/plugin.json` (name
185
- * `kimi`; that root has no package.json). Nameless `{"type":"module"}`
186
- * markers under dist/ are skipped. Undefined when nothing trustworthy is found.
187
- */
188
- export async function hookPluginVersion(command) {
189
- const parsed = parseHookShellCommand(command);
190
- if (parsed === null || !path.isAbsolute(parsed.hookScript))
191
- return undefined;
192
- let dir = path.dirname(path.resolve(parsed.hookScript));
193
- for (let level = 0; level <= MANIFEST_WALK_LEVELS; level += 1) {
194
- const pkg = await readSmallJsonObject(path.join(dir, "package.json"));
195
- if (pkg !== undefined && typeof pkg.name === "string") {
196
- return pkg.name === "kimi-plugin-cc" ? semverOrUndefined(pkg.version) : undefined;
197
- }
198
- const codex = await readSmallJsonObject(path.join(dir, ".codex-plugin", "plugin.json"));
199
- if (codex !== undefined && typeof codex.name === "string") {
200
- return codex.name === "kimi" ? semverOrUndefined(codex.version) : undefined;
201
- }
202
- const parent = path.dirname(dir);
203
- if (parent === dir)
204
- break;
205
- dir = parent;
206
- }
207
- return undefined;
208
- }
209
- /**
210
- * True when `version` is older than `current` (this plugin by default). Any
211
- * prerelease other than `current` itself, and any unparseable value, counts as
212
- * older.
213
- */
214
- export function pluginVersionIsOlder(version, current = KIMI_PLUGIN_CC_VERSION) {
215
- if (version === current)
216
- return false;
217
- const have = SEMVER_RE.exec(version);
218
- const want = SEMVER_RE.exec(current);
219
- if (have === null || want === null || have[4] !== undefined)
220
- return true;
221
- for (let i = 1; i <= 3; i += 1) {
222
- const diff = Number(have[i]) - Number(want[i]);
223
- if (diff !== 0)
224
- return diff < 0;
225
- }
226
- return false;
227
- }
228
- function semverOrUndefined(value) {
229
- return typeof value === "string" && SEMVER_RE.test(value) ? value : undefined;
230
- }
231
- async function readSmallJsonObject(file) {
232
- let handle;
233
- try {
234
- handle = await open(file, fsConstants.O_RDONLY | fsConstants.O_NONBLOCK);
235
- const stat = await handle.stat();
236
- if (!stat.isFile() || stat.size > MANIFEST_MAX_BYTES)
237
- return undefined;
238
- const value = JSON.parse(await handle.readFile("utf8"));
239
- return typeof value === "object" && value !== null && !Array.isArray(value)
240
- ? value
241
- : undefined;
242
- }
243
- catch {
244
- return undefined;
245
- }
246
- finally {
247
- await handle?.close().catch(() => undefined);
248
- }
249
- }
250
- /**
251
- * Caller-facing repair protocol shared by every hook-refusal surface. Re-pin
252
- * through the resolved plugin root, retry ONCE only when
253
- * `retryable_after_setup` is literally true and setup exits zero, and never use
254
- * the skip-check environment variable as a repair.
255
- */
256
- export function hookRefusalRetryProtocol(env) {
257
- const root = env.CLAUDE_PLUGIN_ROOT ?? env.PLUGIN_ROOT ?? "${CLAUDE_PLUGIN_ROOT}";
258
- return (`If details shows retryable_after_setup:true (routine after a plugin update — the ` +
259
- `version-stamped hook path moved), run \`${root}/scripts/companion.sh setup\` and, only ` +
260
- `if it exits 0, retry the refused command once. Anything else — or a second refusal — ` +
261
- `surface the refusal instead of retrying.`);
262
- }
263
- /**
264
- * Structured details for a hook-refusal RuntimeError. `retryable_after_setup`
265
- * is present only for drift and is literally true only when setup can re-pin
266
- * every differing token: hook-script drift, or node-bin/both drift whose Node
267
- * tokens name the same file. A different interpreter is not automatically
268
- * repairable. Callers must also require a zero setup exit before one retry.
269
- */
270
- export function hookRefusalDetails(status) {
271
- return {
272
- config_path: status.configPath,
273
- // Routes the caller to "tell the human to repair Node" instead of the
274
- // default "run /kimi:setup and retry" reflex. Never co-occurs with `drift`:
275
- // drift requires the equality check to FAIL, this requires it to PASS.
276
- ...(status.refusalKind !== undefined
277
- ? { refusal_kind: status.refusalKind, node_bin: status.nodeBin }
278
- : {}),
279
- ...(status.drift !== undefined
280
- ? {
281
- drift_axis: status.drift.axis,
282
- drift_installed_command: status.drift.installedCommand,
283
- drift_expected_command: status.drift.expectedCommand,
284
- retryable_after_setup: status.drift.axis === "hook-script" || status.drift.nodeInterpreterUnchanged === true,
285
- }
286
- : {}),
287
- };
288
- }
289
- /** Repair sentences of the read-only (ask, review, challenge, review gate) hook refusal. */
290
- export const READ_ONLY_HOOK_REPAIR = [
291
- "Run /kimi:setup or $kimi-setup to install or repair this host's managed block.",
292
- "Set KIMI_PLUGIN_CC_SKIP_HOOK_CHECK=1 only if you intentionally accept un-enforced execution.",
293
- ];
294
- /** Repair sentences of the write-capable (rescue, pursue) hook refusal. */
295
- export const WRITE_HOOK_REPAIR = [
296
- "Repair by running Claude Code /kimi:setup or Codex $kimi-setup, then retry.",
297
- "KIMI_PLUGIN_CC_SKIP_HOOK_CHECK=1 is only for deliberate tests or diagnostics.",
298
- ];
299
- function readOnlyGate(name, code) {
300
- return {
301
- code,
302
- stage: `${name}.hook-check`,
303
- warnLabel: name,
304
- intro: [`${name} refuses to run without the canonical kimi-plugin-cc PreToolUse hook.`],
305
- repair: READ_ONLY_HOOK_REPAIR,
306
- };
307
- }
308
- const SWARM_GATE = {
309
- code: "SWARM_HOOK_NOT_INSTALLED",
310
- stage: "swarm.hook-check",
311
- warnLabel: "swarm",
312
- intro: [
313
- "/kimi:swarm refuses to run without the kimi-plugin-cc PreToolUse hook.",
314
- "The hook enforces each worker's tool permissions, write confinement, and delegation limits.",
315
- ],
316
- repair: ["Repair by running Claude Code /kimi:setup or Codex $kimi-setup, then retry."],
317
- };
318
- /**
319
- * The refusal each operation's hook gate raises. The command gates and the
320
- * job-time check in prepareKimiExecutionPlan share it, so a definite negative
321
- * reads the same whichever check sees it.
322
- */
323
- const HOOK_GATES = {
324
- ask: readOnlyGate("ask", "ASK_HOOK_NOT_INSTALLED"),
325
- review: readOnlyGate("review", "REVIEW_HOOK_NOT_INSTALLED"),
326
- challenge: readOnlyGate("challenge", "CHALLENGE_HOOK_NOT_INSTALLED"),
327
- review_gate: readOnlyGate("review_gate", "REVIEW_GATE_HOOK_NOT_INSTALLED"),
328
- rescue: {
329
- code: "RESCUE_HOOK_NOT_INSTALLED",
330
- stage: "rescue.hook-check",
331
- warnLabel: "rescue",
332
- intro: ["rescue refuses to run without the kimi-plugin-cc PreToolUse hook."],
333
- repair: WRITE_HOOK_REPAIR,
334
- },
335
- pursue: {
336
- code: "PURSUE_HOOK_NOT_INSTALLED",
337
- stage: "pursue.hook-check",
338
- warnLabel: "rescue",
339
- intro: ["/kimi:pursue refuses to run without the kimi-plugin-cc PreToolUse hook."],
340
- repair: WRITE_HOOK_REPAIR,
341
- },
342
- swarm: SWARM_GATE,
343
- "swarm-write": SWARM_GATE,
344
- };
345
- /**
346
- * Warn once on stderr and build `operation`'s hook refusal for a status whose
347
- * `installed` is false.
348
- */
349
- export function hookGateRefusal(operation, status, env, stderr = process.stderr) {
350
- const gate = HOOK_GATES[operation];
351
- maybeWarnHookMissing(status, gate.warnLabel, stderr);
352
- return new RuntimeError(gate.code, [
353
- ...gate.intro,
354
- `Hook check failed: ${status.reason ?? "unknown"}.`,
355
- ...gate.repair,
356
- hookRefusalRetryProtocol(env),
357
- ].join(" "), gate.stage, { details: hookRefusalDetails(status) });
358
- }
359
- export function formatHookMissingWarning(status, commandLabel) {
360
- return [
361
- "",
362
- "WARNING: kimi-plugin-cc safety hook is NOT installed (or is invalid).",
363
- ` Command: ${commandLabel}`,
364
- ` Config: ${status.configPath}`,
365
- ` Reason: ${status.reason ?? "unknown"}`,
366
- "",
367
- " Without a valid PreToolUse hook, kimi-code's `-p` mode auto-approves",
368
- " every tool call — including Bash, Write, Edit — even from commands",
369
- " documented as read-only.",
370
- "",
371
- " This command will not start a Kimi model run until enforcement is",
372
- " repaired (the review gate skips instead of blocking stop).",
373
- "",
374
- " Fix: run Claude Code `/kimi:setup` or Codex `$kimi-setup` to install",
375
- " or repair this host's managed block in",
376
- " ~/.kimi-code/config.toml. If you use nvm, asdf, mise, or fnm, you",
377
- " must re-run `/kimi:setup` after any Node version switch — the",
378
- " verifier pins the absolute Node binary path and a switch invalidates",
379
- " the previously-installed block by design. See docs/safety.md.",
380
- "",
381
- " KIMI_PLUGIN_CC_SKIP_HOOK_CHECK=1 explicitly bypasses hook-verification",
382
- " refusals and restores un-enforced `permission: auto` execution. It does",
383
- " NOT bypass the experimental-v2 safety refusal. Reserve it for tests or",
384
- " diagnostics where the hook risk is intentional.",
385
- "",
386
- ].join("\n");
387
- }
388
- /** Once-per-process latch for missing-hook warnings. */
389
- let warnedThisProcess = false;
390
- /**
391
- * Emit the warning to stderr at most once per process. Stderr is for humans;
392
- * stdout remains reserved for load-bearing command output.
393
- */
394
- export function maybeWarnHookMissing(status, commandLabel, stderr = process.stderr) {
395
- if (status.installed)
396
- return;
397
- if (warnedThisProcess)
398
- return;
399
- warnedThisProcess = true;
400
- stderr.write(formatHookMissingWarning(status, commandLabel));
401
- }
402
- /** Test hook — resets the once-per-process latch. */
403
- export function __resetHookMissingWarning() {
404
- warnedThisProcess = false;
405
- }