kimi-plugin-cc 2.7.2 → 3.0.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 (224) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +3 -5
  3. package/CHANGELOG.md +78 -1
  4. package/README.md +505 -448
  5. package/agents/kimi-pursue.md +6 -6
  6. package/agents/kimi-review.md +22 -22
  7. package/agents/kimi-swarm.md +25 -23
  8. package/agents/{kimi-ask.md → kimi-task.md} +34 -30
  9. package/commands/README.md +4 -6
  10. package/commands/pursue.md +1 -1
  11. package/commands/review.md +8 -3
  12. package/commands/setup.md +4 -2
  13. package/commands/swarm.md +6 -5
  14. package/commands/{rescue.md → task.md} +13 -11
  15. package/dist/background-spawn.js +10 -17
  16. package/dist/cli-client.js +65 -46
  17. package/dist/commands/ask.js +22 -89
  18. package/dist/commands/cancel.js +7 -46
  19. package/dist/commands/cli-helpers.js +102 -1
  20. package/dist/commands/models.js +2 -3
  21. package/dist/commands/pursue.js +5 -19
  22. package/dist/commands/repair-sessions.js +118 -12
  23. package/dist/commands/rescue.js +12 -82
  24. package/dist/commands/result.js +7 -1
  25. package/dist/commands/review-gate.js +1 -0
  26. package/dist/commands/review.js +12 -15
  27. package/dist/commands/setup.js +606 -270
  28. package/dist/commands/status.js +2 -2
  29. package/dist/commands/swarm.js +93 -119
  30. package/dist/commands/worker.js +1 -2
  31. package/dist/companion.js +3 -1
  32. package/dist/dsh/index.js +15 -17
  33. package/dist/git.js +1 -9
  34. package/dist/help.js +3 -3
  35. package/dist/hooks/approval-hook.js +41 -0
  36. package/dist/hooks/approval-policy.js +72 -10
  37. package/dist/hooks/config-safety.js +19 -42
  38. package/dist/hooks/hook-timing.js +48 -0
  39. package/dist/hooks/install.js +206 -11
  40. package/dist/hooks/managed-block.js +78 -33
  41. package/dist/job-store.js +100 -128
  42. package/dist/jobs.js +14 -19
  43. package/dist/kimi-command.js +74 -1
  44. package/dist/kimi-engine.js +170 -74
  45. package/dist/kimi-errors.js +14 -69
  46. package/dist/kimi-home.js +6 -0
  47. package/dist/kimi-journal.js +41 -0
  48. package/dist/kimi-releases.js +144 -0
  49. package/dist/kimi-timeouts.js +1 -1
  50. package/dist/kimi-version-probe.js +12 -48
  51. package/dist/mcp/job-control.js +4 -6
  52. package/dist/mcp/launcher.js +12 -6
  53. package/dist/mcp/operations.js +50 -18
  54. package/dist/mcp/protocol.js +16 -14
  55. package/dist/mcp/stdio.js +3 -2
  56. package/dist/model-command.js +5 -3
  57. package/dist/native-v2-preflight.js +139 -68
  58. package/dist/parsing.js +171 -237
  59. package/dist/paths.js +8 -0
  60. package/dist/process-tree.js +35 -24
  61. package/dist/read-scope.js +435 -0
  62. package/dist/render.js +46 -9
  63. package/dist/rescue-approval.js +461 -84
  64. package/dist/schemas/review-gate-output.js +3 -5
  65. package/dist/session-title.js +25 -5
  66. package/dist/stream-json.js +1 -3
  67. package/dist/surface-routing.js +21 -0
  68. package/dist/swarm-activation.js +74 -77
  69. package/dist/swarm-policy.js +2 -1
  70. package/dist/swarm-recovery.js +2 -1
  71. package/dist/util.js +28 -0
  72. package/dist/version.js +1 -1
  73. package/docs/migration.md +27 -6
  74. package/docs/models.md +2 -2
  75. package/docs/registry-releases.md +3 -1
  76. package/docs/safety.md +131 -81
  77. package/docs/session-visibility.md +5 -3
  78. package/docs/swarm-recovery.md +4 -4
  79. package/docs/upstream-2.7.2-certification.md +16 -2
  80. package/docs/upstream-2.7.3-certification.md +95 -0
  81. package/package.json +2 -1
  82. package/plugins/kimi-codex/.codex-plugin/plugin.json +3 -3
  83. package/plugins/kimi-codex/.mcp.json +1 -0
  84. package/plugins/kimi-codex/dist/background-spawn.js +10 -17
  85. package/plugins/kimi-codex/dist/cli-client.js +65 -46
  86. package/plugins/kimi-codex/dist/commands/ask.js +22 -89
  87. package/plugins/kimi-codex/dist/commands/cancel.js +7 -46
  88. package/plugins/kimi-codex/dist/commands/cli-helpers.js +102 -1
  89. package/plugins/kimi-codex/dist/commands/models.js +2 -3
  90. package/plugins/kimi-codex/dist/commands/pursue.js +5 -19
  91. package/plugins/kimi-codex/dist/commands/repair-sessions.js +118 -12
  92. package/plugins/kimi-codex/dist/commands/rescue.js +12 -82
  93. package/plugins/kimi-codex/dist/commands/result.js +7 -1
  94. package/plugins/kimi-codex/dist/commands/review-gate.js +1 -0
  95. package/plugins/kimi-codex/dist/commands/review.js +12 -15
  96. package/plugins/kimi-codex/dist/commands/setup.js +606 -270
  97. package/plugins/kimi-codex/dist/commands/status.js +2 -2
  98. package/plugins/kimi-codex/dist/commands/swarm.js +93 -119
  99. package/plugins/kimi-codex/dist/commands/worker.js +1 -2
  100. package/plugins/kimi-codex/dist/companion.js +3 -1
  101. package/plugins/kimi-codex/dist/dsh/index.js +15 -17
  102. package/plugins/kimi-codex/dist/git.js +1 -9
  103. package/plugins/kimi-codex/dist/help.js +3 -3
  104. package/plugins/kimi-codex/dist/hooks/approval-hook.js +41 -0
  105. package/plugins/kimi-codex/dist/hooks/approval-policy.js +72 -10
  106. package/plugins/kimi-codex/dist/hooks/config-safety.js +19 -42
  107. package/plugins/kimi-codex/dist/hooks/hook-timing.js +48 -0
  108. package/plugins/kimi-codex/dist/hooks/install.js +206 -11
  109. package/plugins/kimi-codex/dist/hooks/managed-block.js +78 -33
  110. package/plugins/kimi-codex/dist/job-store.js +100 -128
  111. package/plugins/kimi-codex/dist/jobs.js +14 -19
  112. package/plugins/kimi-codex/dist/kimi-command.js +74 -1
  113. package/plugins/kimi-codex/dist/kimi-engine.js +170 -74
  114. package/plugins/kimi-codex/dist/kimi-errors.js +14 -69
  115. package/plugins/kimi-codex/dist/kimi-home.js +6 -0
  116. package/plugins/kimi-codex/dist/kimi-journal.js +41 -0
  117. package/plugins/kimi-codex/dist/kimi-releases.js +144 -0
  118. package/plugins/kimi-codex/dist/kimi-timeouts.js +1 -1
  119. package/plugins/kimi-codex/dist/kimi-version-probe.js +12 -48
  120. package/plugins/kimi-codex/dist/mcp/job-control.js +4 -6
  121. package/plugins/kimi-codex/dist/mcp/launcher.js +12 -6
  122. package/plugins/kimi-codex/dist/mcp/operations.js +50 -18
  123. package/plugins/kimi-codex/dist/mcp/protocol.js +16 -14
  124. package/plugins/kimi-codex/dist/mcp/stdio.js +3 -2
  125. package/plugins/kimi-codex/dist/model-command.js +5 -3
  126. package/plugins/kimi-codex/dist/native-v2-preflight.js +139 -68
  127. package/plugins/kimi-codex/dist/parsing.js +171 -237
  128. package/plugins/kimi-codex/dist/paths.js +8 -0
  129. package/plugins/kimi-codex/dist/process-tree.js +35 -24
  130. package/plugins/kimi-codex/dist/read-scope.js +435 -0
  131. package/plugins/kimi-codex/dist/render.js +46 -9
  132. package/plugins/kimi-codex/dist/rescue-approval.js +461 -84
  133. package/plugins/kimi-codex/dist/schemas/review-gate-output.js +3 -5
  134. package/plugins/kimi-codex/dist/session-title.js +25 -5
  135. package/plugins/kimi-codex/dist/stream-json.js +1 -3
  136. package/plugins/kimi-codex/dist/surface-routing.js +21 -0
  137. package/plugins/kimi-codex/dist/swarm-activation.js +74 -77
  138. package/plugins/kimi-codex/dist/swarm-policy.js +2 -1
  139. package/plugins/kimi-codex/dist/swarm-recovery.js +2 -1
  140. package/plugins/kimi-codex/dist/util.js +28 -0
  141. package/plugins/kimi-codex/dist/version.js +1 -1
  142. package/plugins/kimi-codex/skills/kimi-pursue/SKILL.md +1 -1
  143. package/plugins/kimi-codex/skills/kimi-review/SKILL.md +8 -7
  144. package/plugins/kimi-codex/skills/kimi-review/agents/openai.yaml +1 -1
  145. package/plugins/kimi-codex/skills/kimi-setup/SKILL.md +2 -1
  146. package/plugins/kimi-codex/skills/kimi-swarm/SKILL.md +8 -6
  147. package/plugins/kimi-codex/skills/kimi-swarm/agents/openai.yaml +1 -1
  148. package/plugins/kimi-codex/skills/{kimi-ask → kimi-task}/SKILL.md +11 -9
  149. package/plugins/kimi-codex/skills/kimi-task/agents/openai.yaml +6 -0
  150. package/plugins/kimi-dsh/README.md +3 -2
  151. package/plugins/kimi-dsh/dist/background-spawn.js +10 -17
  152. package/plugins/kimi-dsh/dist/cli-client.js +65 -46
  153. package/plugins/kimi-dsh/dist/commands/ask.js +22 -89
  154. package/plugins/kimi-dsh/dist/commands/cancel.js +7 -46
  155. package/plugins/kimi-dsh/dist/commands/cli-helpers.js +102 -1
  156. package/plugins/kimi-dsh/dist/commands/models.js +2 -3
  157. package/plugins/kimi-dsh/dist/commands/pursue.js +5 -19
  158. package/plugins/kimi-dsh/dist/commands/repair-sessions.js +118 -12
  159. package/plugins/kimi-dsh/dist/commands/rescue.js +12 -82
  160. package/plugins/kimi-dsh/dist/commands/result.js +7 -1
  161. package/plugins/kimi-dsh/dist/commands/review-gate.js +1 -0
  162. package/plugins/kimi-dsh/dist/commands/review.js +12 -15
  163. package/plugins/kimi-dsh/dist/commands/setup.js +606 -270
  164. package/plugins/kimi-dsh/dist/commands/status.js +2 -2
  165. package/plugins/kimi-dsh/dist/commands/swarm.js +93 -119
  166. package/plugins/kimi-dsh/dist/commands/worker.js +1 -2
  167. package/plugins/kimi-dsh/dist/companion.js +3 -1
  168. package/plugins/kimi-dsh/dist/dsh/index.js +15 -17
  169. package/plugins/kimi-dsh/dist/git.js +1 -9
  170. package/plugins/kimi-dsh/dist/help.js +3 -3
  171. package/plugins/kimi-dsh/dist/hooks/approval-hook.js +41 -0
  172. package/plugins/kimi-dsh/dist/hooks/approval-policy.js +72 -10
  173. package/plugins/kimi-dsh/dist/hooks/config-safety.js +19 -42
  174. package/plugins/kimi-dsh/dist/hooks/hook-timing.js +48 -0
  175. package/plugins/kimi-dsh/dist/hooks/install.js +206 -11
  176. package/plugins/kimi-dsh/dist/hooks/managed-block.js +78 -33
  177. package/plugins/kimi-dsh/dist/job-store.js +100 -128
  178. package/plugins/kimi-dsh/dist/jobs.js +14 -19
  179. package/plugins/kimi-dsh/dist/kimi-command.js +74 -1
  180. package/plugins/kimi-dsh/dist/kimi-engine.js +170 -74
  181. package/plugins/kimi-dsh/dist/kimi-errors.js +14 -69
  182. package/plugins/kimi-dsh/dist/kimi-home.js +6 -0
  183. package/plugins/kimi-dsh/dist/kimi-journal.js +41 -0
  184. package/plugins/kimi-dsh/dist/kimi-releases.js +144 -0
  185. package/plugins/kimi-dsh/dist/kimi-timeouts.js +1 -1
  186. package/plugins/kimi-dsh/dist/kimi-version-probe.js +12 -48
  187. package/plugins/kimi-dsh/dist/mcp/job-control.js +4 -6
  188. package/plugins/kimi-dsh/dist/mcp/launcher.js +12 -6
  189. package/plugins/kimi-dsh/dist/mcp/operations.js +50 -18
  190. package/plugins/kimi-dsh/dist/mcp/protocol.js +16 -14
  191. package/plugins/kimi-dsh/dist/mcp/stdio.js +3 -2
  192. package/plugins/kimi-dsh/dist/model-command.js +5 -3
  193. package/plugins/kimi-dsh/dist/native-v2-preflight.js +139 -68
  194. package/plugins/kimi-dsh/dist/parsing.js +171 -237
  195. package/plugins/kimi-dsh/dist/paths.js +8 -0
  196. package/plugins/kimi-dsh/dist/process-tree.js +35 -24
  197. package/plugins/kimi-dsh/dist/read-scope.js +435 -0
  198. package/plugins/kimi-dsh/dist/render.js +46 -9
  199. package/plugins/kimi-dsh/dist/rescue-approval.js +461 -84
  200. package/plugins/kimi-dsh/dist/schemas/review-gate-output.js +3 -5
  201. package/plugins/kimi-dsh/dist/session-title.js +25 -5
  202. package/plugins/kimi-dsh/dist/stream-json.js +1 -3
  203. package/plugins/kimi-dsh/dist/surface-routing.js +21 -0
  204. package/plugins/kimi-dsh/dist/swarm-activation.js +74 -77
  205. package/plugins/kimi-dsh/dist/swarm-policy.js +2 -1
  206. package/plugins/kimi-dsh/dist/swarm-recovery.js +2 -1
  207. package/plugins/kimi-dsh/dist/util.js +28 -0
  208. package/plugins/kimi-dsh/dist/version.js +1 -1
  209. package/plugins/kimi-dsh/package.json +1 -1
  210. package/agents/kimi-challenge.md +0 -86
  211. package/agents/kimi-rescue.md +0 -92
  212. package/agents/kimi-swarm-write.md +0 -96
  213. package/commands/ask.md +0 -38
  214. package/commands/challenge.md +0 -35
  215. package/dist/commands/not-implemented.js +0 -4
  216. package/plugins/kimi-codex/dist/commands/not-implemented.js +0 -4
  217. package/plugins/kimi-codex/skills/kimi-ask/agents/openai.yaml +0 -6
  218. package/plugins/kimi-codex/skills/kimi-challenge/SKILL.md +0 -34
  219. package/plugins/kimi-codex/skills/kimi-challenge/agents/openai.yaml +0 -6
  220. package/plugins/kimi-codex/skills/kimi-rescue/SKILL.md +0 -34
  221. package/plugins/kimi-codex/skills/kimi-rescue/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/dist/commands/not-implemented.js +0 -4
@@ -1,13 +1,14 @@
1
+ import { evaluateScopedRead, isAllowedFetchUrl, parseScopeEnvironment, SCOPED_READ_TOOLS, } from "../read-scope.js";
1
2
  import { BOUNDED_SWARM_POLICY, evaluateSwarmWaveInput } from "../swarm-policy.js";
2
3
  /**
3
4
  * Tools considered read-only across review/challenge/review_gate/rescue.
4
5
  *
5
6
  * Includes kimi-code's own local-read defaults (`Read`, `Grep`, `Glob`)
6
7
  * plus a few additional read-safe tools the runtime auto-allows
7
- * (`ReadMediaFile`, `TaskList`, `TaskOutput`). Excludes network
8
- * operations (`WebSearch`, `FetchURL`) since review-style commands
9
- * should stay on the local repo — adding them would silently broaden
10
- * the safety contract for what is documented as a local-only review.
8
+ * (`ReadMediaFile`, `TaskList`, `TaskOutput`). Network tools
9
+ * (`WebSearch`, `FetchURL`) are excluded; read-only labels allow them only
10
+ * for a call started with --network, which also confines these reads to the
11
+ * plugin-exported read roots (runtime/read-scope.ts).
11
12
  */
12
13
  export const READ_ONLY_TOOLS = new Set([
13
14
  "Read",
@@ -17,6 +18,8 @@ export const READ_ONLY_TOOLS = new Set([
17
18
  "TaskList",
18
19
  "TaskOutput",
19
20
  ]);
21
+ /** Web tools: denied unless the plugin exported KIMI_PLUGIN_CC_NETWORK=1 with read roots. */
22
+ const WEB_TOOLS = new Set(["WebSearch", "FetchURL"]);
20
23
  /**
21
24
  * Exact tool name for kimi-code's parallel fan-out. Every spawned subagent
22
25
  * inherits KIMI_PLUGIN_CC_CMD. On native v2, each agent has its own eager
@@ -55,6 +58,9 @@ export async function decideHookOutcome(input, ctx) {
55
58
  if (!("evaluator" in result))
56
59
  return result;
57
60
  const toolName = typeof input.tool_name === "string" ? input.tool_name : "";
61
+ if (result.evaluator === "read-scope") {
62
+ return await evaluateScopedRead(toolName, input.tool_input, result.roots);
63
+ }
58
64
  if (ctx.rescueEvaluator !== undefined) {
59
65
  return await ctx.rescueEvaluator(result.workspaceRoot, toolName, input.tool_input);
60
66
  }
@@ -134,8 +140,10 @@ export function decideHookOutcomePure(input, ctx) {
134
140
  // non-interactive subprocesses, so interactive tool prompts cannot
135
141
  // substitute for this allowlist.
136
142
  if (READ_ONLY_TOOLS.has(toolName)) {
137
- return { decision: "allow" };
143
+ return readToolDecision(toolName, scopeOf(ctx));
138
144
  }
145
+ if (WEB_TOOLS.has(toolName))
146
+ return webToolDecision(label, toolName, input.tool_input, scopeOf(ctx));
139
147
  return {
140
148
  decision: "deny",
141
149
  reason: denyReadOnlyMessage(label, toolName),
@@ -146,12 +154,15 @@ export function decideHookOutcomePure(input, ctx) {
146
154
  // AgentSwarm are allowed and the singular Agent stays denied. This adds
147
155
  // no direct tool-write permission within the repository/Git trust
148
156
  // boundary: read-only Git context commands still trust repository Git
149
- // configuration and can execute configured helpers. Exact 2.6.2/2.6.3/2.7.2 waves
157
+ // configuration and can execute configured helpers. Bounded-swarm waves
150
158
  // pass the admission guard above; fresh children cannot activate swarm
151
159
  // mode. Older paths retain their inherited scheduler limitations.
152
- if (READ_ONLY_TOOLS.has(toolName) || toolName === AGENT_SWARM_TOOL) {
160
+ if (toolName === AGENT_SWARM_TOOL)
153
161
  return { decision: "allow" };
154
- }
162
+ if (READ_ONLY_TOOLS.has(toolName))
163
+ return readToolDecision(toolName, scopeOf(ctx));
164
+ if (WEB_TOOLS.has(toolName))
165
+ return webToolDecision(label, toolName, input.tool_input, scopeOf(ctx));
155
166
  return {
156
167
  decision: "deny",
157
168
  reason: denySwarmMessage(toolName, ctx.swarmPolicy === BOUNDED_SWARM_POLICY && ctx.swarmWaveSize === "1"),
@@ -163,6 +174,9 @@ export function decideHookOutcomePure(input, ctx) {
163
174
  // KIMI_PLUGIN_CC_WORKSPACE_ROOT, never the payload cwd, so an in-process
164
175
  // subagent cannot move the trust boundary or forge its root. A missing
165
176
  // root fails closed for writes.
177
+ const slashed = backslashRootDenial(ctx.trustedWorkspaceRoot, toolName);
178
+ if (slashed !== undefined)
179
+ return slashed;
166
180
  if (READ_ONLY_TOOLS.has(toolName) || toolName === AGENT_SWARM_TOOL) {
167
181
  return { decision: "allow" };
168
182
  }
@@ -177,6 +191,9 @@ export function decideHookOutcomePure(input, ctx) {
177
191
  return { evaluator: "rescue", workspaceRoot };
178
192
  }
179
193
  case "rescue": {
194
+ const slashed = backslashRootDenial(ctx.trustedWorkspaceRoot, toolName);
195
+ if (slashed !== undefined)
196
+ return slashed;
180
197
  const workspaceRoot = typeof ctx.trustedWorkspaceRoot === "string" ? ctx.trustedWorkspaceRoot : "";
181
198
  if (workspaceRoot.length === 0) {
182
199
  if (READ_ONLY_TOOLS.has(toolName))
@@ -198,7 +215,7 @@ export function decideHookOutcomePure(input, ctx) {
198
215
  }
199
216
  // Unknown label: fail closed to READ_ONLY_TOOLS only.
200
217
  if (READ_ONLY_TOOLS.has(toolName)) {
201
- return { decision: "allow" };
218
+ return readToolDecision(toolName, scopeOf(ctx));
202
219
  }
203
220
  return {
204
221
  decision: "deny",
@@ -206,12 +223,57 @@ export function decideHookOutcomePure(input, ctx) {
206
223
  };
207
224
  }
208
225
  }
226
+ /**
227
+ * kimi-code resolves its work dir with `pathe`, which reads a literal `\` as a
228
+ * separator, so a write-label root holding one names a different directory to
229
+ * kimi than to this hook. Every tool is denied; the launcher refuses such a
230
+ * root before spawn, so reaching here means the environment was altered.
231
+ */
232
+ function backslashRootDenial(root, toolName) {
233
+ if (process.platform === "win32" || typeof root !== "string" || !root.includes("\\"))
234
+ return undefined;
235
+ return {
236
+ decision: "deny",
237
+ reason: `kimi-plugin-cc safety hook: writes cannot be confined to ${root}: it contains a backslash, ` +
238
+ `which kimi-code reads as a path separator; tool "${toolName}" denied.`,
239
+ };
240
+ }
241
+ function scopeOf(ctx) {
242
+ return parseScopeEnvironment(ctx.readRoots, ctx.network);
243
+ }
244
+ function readToolDecision(toolName, scope) {
245
+ if (scope.confined && SCOPED_READ_TOOLS.has(toolName))
246
+ return { evaluator: "read-scope", roots: scope.roots };
247
+ return { decision: "allow" };
248
+ }
249
+ function webToolDecision(label, toolName, toolInput, scope) {
250
+ if (!scope.network) {
251
+ return {
252
+ decision: "deny",
253
+ reason: `kimi-plugin-cc safety hook: ${label} has no network access; tool "${toolName}" is denied. ` +
254
+ "The caller can allow WebSearch and FetchURL for one call with --network, which also confines reads to the workspace and any --read-root.",
255
+ };
256
+ }
257
+ if (toolName === "WebSearch")
258
+ return { decision: "allow" };
259
+ const url = typeof toolInput === "object" && toolInput !== null && !Array.isArray(toolInput)
260
+ ? toolInput.url
261
+ : undefined;
262
+ if (isAllowedFetchUrl(url))
263
+ return { decision: "allow" };
264
+ return {
265
+ decision: "deny",
266
+ reason: "kimi-plugin-cc safety hook: FetchURL accepts only http:// and https:// URLs without embedded credentials " +
267
+ "whose host is a public name or a canonical global unicast IP address; localhost, .local and .internal names, " +
268
+ "numerically encoded hosts, and private, loopback, link-local, documentation, multicast, reserved, 6to4 and Teredo addresses are denied.",
269
+ };
270
+ }
209
271
  function denyReadOnlyMessage(label, toolName) {
210
272
  const tool = toolName.length > 0 ? toolName : "<unspecified>";
211
273
  return [
212
274
  `kimi-plugin-cc safety hook: ${label} is read-only.`,
213
275
  `Tool "${tool}" is denied — use Read, Grep, or Glob to inspect the workspace instead.`,
214
- "If you need to mutate state, the user must invoke /kimi:rescue (write-capable) rather than this command.",
276
+ "If you need to mutate state, the user must invoke /kimi:task --write (write-capable) rather than this command.",
215
277
  ].join(" ");
216
278
  }
217
279
  function denySwarmMessage(toolName, singleWorker) {
@@ -3,7 +3,10 @@ import { constants as fsConstants } from "node:fs";
3
3
  import { link, lstat, mkdir, open, rename, unlink } from "node:fs/promises";
4
4
  import path from "node:path";
5
5
  import { RuntimeError } from "../errors.js";
6
+ import { selectKimiBinary } from "../kimi-command.js";
7
+ import { kimiReleaseHas } from "../kimi-releases.js";
6
8
  import { probeKimiVersion } from "../kimi-version-probe.js";
9
+ import { sleep, tomlErrorLocation } from "../util.js";
7
10
  import { extractValue } from "../vendor/smol-toml/extract.js";
8
11
  import { parse as parseToml } from "../vendor/smol-toml/parse.js";
9
12
  import { parseKey } from "../vendor/smol-toml/struct.js";
@@ -122,9 +125,6 @@ async function createPrivateLockFile(lockPath, owner) {
122
125
  const stats = await handle.stat({ bigint: true });
123
126
  candidateIdentity = lockIdentity(stats.dev, stats.ino);
124
127
  }
125
- catch (error) {
126
- throw error;
127
- }
128
128
  finally {
129
129
  await handle.close().catch(() => undefined);
130
130
  }
@@ -271,10 +271,7 @@ async function tryRecoverStaleLock(lockPath, observed, staleMs) {
271
271
  try {
272
272
  await assertOwnedLock(recoveryPath, recoverySnapshot, recoveryOwner.token);
273
273
  const current = await readLockSnapshot(lockPath);
274
- if (current !== null &&
275
- current.identity === observed.identity &&
276
- current.owner?.token === observed.owner?.token &&
277
- isStaleLock(current, staleMs)) {
274
+ if (current !== null && sameObservedLock(current, observed) && isStaleLock(current, staleMs)) {
278
275
  // The per-identity recovery lock above ensures only one observer of this
279
276
  // inode can claim it. Rename is the atomic removal from the public path;
280
277
  // verification happens on the claimed inode before its unique name is
@@ -370,9 +367,6 @@ async function releaseOwnedLock(lockPath, acquired, token) {
370
367
  }
371
368
  await unlink(releasePath);
372
369
  }
373
- function sleep(ms) {
374
- return new Promise((resolve) => setTimeout(resolve, ms));
375
- }
376
370
  /**
377
371
  * Convert a validated top-level inline `hooks = [...]` assignment to canonical
378
372
  * `[[hooks]]` tables so setup can append its own array-table entry without
@@ -490,14 +484,18 @@ export function validateKimiHookSet(contents, version) {
490
484
  return inspection.validation;
491
485
  return validateVersionSensitiveEvents(inspection.versionSensitiveEvents, version);
492
486
  }
493
- /** Probe `kimi --version` only when the config uses an additive hook event. */
494
- export async function validateKimiHookSetForEnvironment(contents, env) {
487
+ /**
488
+ * Probe `kimi --version` only when the config uses an additive hook event.
489
+ * A job's execution plan passes its own exact probe as `knownVersion`, so the
490
+ * hook set is judged against the release the job runs and nothing is spawned.
491
+ */
492
+ export async function validateKimiHookSetForEnvironment(contents, env, knownVersion) {
495
493
  const inspection = inspectKimiHookSet(contents);
496
494
  if (!inspection.validation.valid || inspection.versionSensitiveEvents.length === 0) {
497
495
  return inspection.validation;
498
496
  }
499
- const probe = await probeKimiVersion({
500
- kimiBin: env.KIMI_PLUGIN_CC_KIMI_BIN || undefined,
497
+ const probe = knownVersion ?? await probeKimiVersion({
498
+ kimiBin: selectKimiBinary(env).command,
501
499
  env,
502
500
  });
503
501
  const first = inspection.versionSensitiveEvents[0];
@@ -514,7 +512,7 @@ function inspectKimiHookSet(contents) {
514
512
  catch (error) {
515
513
  const line = tomlErrorLine(error);
516
514
  return {
517
- validation: invalidHookSet(`TOML parse failed: ${error instanceof Error ? error.message : String(error)}`, 0, line, "parse"),
515
+ validation: invalidHookSet(`TOML parse failed: ${tomlErrorLocation(error)}`, 0, line, "parse"),
518
516
  versionSensitiveEvents: [],
519
517
  };
520
518
  }
@@ -592,34 +590,13 @@ function validateVersionSensitiveEvents(events, version) {
592
590
  if (version === undefined) {
593
591
  return invalidHookSet(`version-sensitive event ${JSON.stringify(first.event)} requires a verified kimi-code version`, first.entry, first.line);
594
592
  }
595
- // Exact schemas reviewed at 2.0.0 (1b89e4b0), 2.0.1 (caf7d4e2),
596
- // 2.0.2 (9d07f634), 2.1.0 (52437299), 2.1.1 (f67e6398), 2.2.0 (472192c3),
597
- // 2.3.0 (b5e28fd6), 2.3.1 (684bae9e), 2.3.2 (b8dd3a13), 2.3.3 (ec495f0c),
598
- // 2.4.0 (f26e4d91), 2.4.1 (2c73f3f5), 2.5.0 (41c34143), 2.5.1 (1b02030d),
599
- // 2.5.2 (1c142a00), 2.5.3 (5af9942e), 2.6.0 (740d6c0b), 2.6.2
600
- // (6a8493df), 2.6.3 (b0de2fbd), and 2.7.2 (56145f84): the additive events
601
- // and HookDefSchema are unchanged. Schema acceptance does not certify operations.
593
+ // The additive events and HookDefSchema are unchanged at every release whose
594
+ // row in kimi-releases.ts carries "hook-schema" (each row names the reviewed
595
+ // commit). Schema acceptance does not certify operations.
602
596
  // Require the full version string AND matching components; no inferred 2.x compatibility.
603
- if (version.major === 2 && ((version.minor === 0 && ((version.version === "2.0.0" && version.patch === 0) ||
604
- (version.version === "2.0.1" && version.patch === 1) ||
605
- (version.version === "2.0.2" && version.patch === 2))) ||
606
- (version.minor === 1 && ((version.version === "2.1.0" && version.patch === 0) ||
607
- (version.version === "2.1.1" && version.patch === 1))) ||
608
- (version.minor === 2 && ((version.version === "2.2.0" && version.patch === 0))) ||
609
- (version.minor === 3 && ((version.version === "2.3.0" && version.patch === 0) ||
610
- (version.version === "2.3.1" && version.patch === 1) ||
611
- (version.version === "2.3.2" && version.patch === 2) ||
612
- (version.version === "2.3.3" && version.patch === 3))) ||
613
- (version.minor === 4 && ((version.version === "2.4.0" && version.patch === 0) ||
614
- (version.version === "2.4.1" && version.patch === 1))) ||
615
- (version.minor === 5 && ((version.version === "2.5.0" && version.patch === 0) ||
616
- (version.version === "2.5.1" && version.patch === 1) ||
617
- (version.version === "2.5.2" && version.patch === 2) ||
618
- (version.version === "2.5.3" && version.patch === 3))) ||
619
- (version.minor === 6 && ((version.version === "2.6.0" && version.patch === 0) ||
620
- (version.version === "2.6.2" && version.patch === 2) ||
621
- (version.version === "2.6.3" && version.patch === 3))) ||
622
- (version.minor === 7 && version.version === "2.7.2" && version.patch === 2))) {
597
+ if (version.major === 2 &&
598
+ version.version === `${version.major}.${version.minor}.${version.patch}` &&
599
+ kimiReleaseHas(version.version, "hook-schema")) {
623
600
  return { valid: true };
624
601
  }
625
602
  if (version.major !== 0) {
@@ -0,0 +1,48 @@
1
+ // Timing shared by the installed managed block and the hook entry script.
2
+ //
3
+ // kimi-code reads a hook that times out as ALLOW, so the hook must decide (or
4
+ // deny) before kimi-code's timeout expires. The installed timeout, the shortest
5
+ // timeout an entry may have and still count as enforcing, and the decision
6
+ // deadline all derive from HOOK_TIMEOUT_S so they cannot drift.
7
+ /** `timeout` (seconds) written into every managed PreToolUse block. */
8
+ export const HOOK_TIMEOUT_S = 15;
9
+ /**
10
+ * Fail-closed deadline for a labelled call, counted from hook start and
11
+ * covering stdin read plus policy evaluation. The margin absorbs Node startup
12
+ * and module load, which kimi-code's timeout counts but this timer does not.
13
+ */
14
+ export const HOOK_DECISION_DEADLINE_MS = HOOK_TIMEOUT_S * 1000 - 5_000;
15
+ /**
16
+ * kimi-code's timeout (seconds) for a [[hooks]] entry with no `timeout` key:
17
+ * `DEFAULT_HOOK_TIMEOUT_SECONDS` in agent-core-v2 `externalHooks/internal/
18
+ * matchHooks.ts`, 30 in every certified source through 2.7.3. Each compat
19
+ * audit re-verifies it.
20
+ */
21
+ export const UPSTREAM_DEFAULT_HOOK_TIMEOUT_S = 30;
22
+ /**
23
+ * The timeout kimi-code applies to an entry's raw `timeout` value: the
24
+ * upstream default when absent, the integer itself within upstream's 1-600
25
+ * schema, otherwise `null` (not provably enforcing).
26
+ */
27
+ export function effectiveHookTimeoutS(value) {
28
+ if (value === undefined)
29
+ return UPSTREAM_DEFAULT_HOOK_TIMEOUT_S;
30
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 600)
31
+ return null;
32
+ return value;
33
+ }
34
+ /**
35
+ * True when kimi-code waits at least `HOOK_TIMEOUT_S` for the hook, so the
36
+ * fail-closed deadline below fires before upstream's timeout-as-allow.
37
+ */
38
+ export function hookTimeoutEnforces(value) {
39
+ const effective = effectiveHookTimeoutS(value);
40
+ return effective !== null && effective >= HOOK_TIMEOUT_S;
41
+ }
42
+ /**
43
+ * Test-only delay before the policy decision (milliseconds, or `hang` for a
44
+ * promise that never settles). It can only push a decision into the
45
+ * fail-closed deadline, and buildKimiCliEnvironment deletes it on every spawn,
46
+ * so it never reaches a job's hook.
47
+ */
48
+ export const HOOK_TEST_DELAY_ENV = "KIMI_PLUGIN_CC_HOOK_TEST_DELAY_MS";
@@ -1,11 +1,13 @@
1
1
  import { constants as fsConstants, existsSync } from "node:fs";
2
- import { access, readFile } from "node:fs/promises";
2
+ import { access, open, readFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  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) {
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 = {}) {
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,37 @@ 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
56
  // Classify command mismatches into actionable drift reasons without changing
54
57
  // the installed=false decision. hostId isolates this host's block in the
55
58
  // shared config.
59
+ const hostId = resolveHostId(env, expected.hookScriptPath);
56
60
  const check = evaluateInstalled(raw, expected.command, {
57
- hostId: resolveHostId(env, expected.hookScriptPath),
61
+ hostId,
58
62
  nodeExists: (binPath) => existsSync(binPath),
59
63
  });
60
- if (!check.installed) {
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) {
61
78
  return {
62
79
  installed: false,
63
80
  reason: check.reason,
@@ -117,10 +134,118 @@ export async function verifyHookInstalled(env) {
117
134
  nodeBin: expected.nodeBin,
118
135
  };
119
136
  }
120
- return { installed: true, configPath };
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;
121
180
  }
122
- function resolveKimiCodeConfigPath(env) {
123
- return path.join(resolveKimiHome(env), "config.toml");
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
+ }
124
249
  }
125
250
  /**
126
251
  * Caller-facing repair protocol shared by every hook-refusal surface. Re-pin
@@ -161,6 +286,76 @@ export function hookRefusalDetails(status) {
161
286
  : {}),
162
287
  };
163
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
+ }
164
359
  export function formatHookMissingWarning(status, commandLabel) {
165
360
  return [
166
361
  "",