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
@@ -13,9 +13,11 @@
13
13
  // B. the EnterPlanMode tool (already hook-denied for every label);
14
14
  // C. resuming a session whose agent wire journal holds a durable
15
15
  // `plan_mode.enter` record (restore() folds ONLY agents/*/wire.jsonl).
16
- // This module closes A and C before any process is spawned, refuses the
17
- // v2-only experimental features (tower, subagent fork) that this plugin does
18
- // not certify, and requires local execution for configured and resumed sessions.
16
+ // This module closes A and C before any process is spawned and requires
17
+ // local execution for configured and resumed sessions. The v2-only
18
+ // experimental features (tower, subagent fork) are forced off for every
19
+ // child by per-flag env in cli-client.ts; only an uncertified release, whose
20
+ // flag names are unreviewed, still refuses an enabled selector here.
19
21
  // B stays in runtime/hooks/approval-policy.ts.
20
22
  //
21
23
  // Contract:
@@ -29,8 +31,11 @@
29
31
  import { constants as fsConstants } from "node:fs";
30
32
  import { open, readdir } from "node:fs/promises";
31
33
  import path from "node:path";
32
- import { createInterface } from "node:readline";
33
34
  import { RuntimeError } from "./errors.js";
35
+ import { kimiCodeConfigPath } from "./kimi-home.js";
36
+ import { kimiJournalRecords, UNPARSEABLE_JOURNAL_LINE } from "./kimi-journal.js";
37
+ import { kimiCertifiedReleaseHas } from "./kimi-releases.js";
38
+ import { tomlErrorLocation } from "./util.js";
34
39
  import { parse as parseToml } from "./vendor/smol-toml/parse.js";
35
40
  /** `<kimiHome>/config.toml` is small hand-edited config, never a data file. */
36
41
  const CONFIG_MAX_BYTES = 1 * 1024 * 1024;
@@ -91,7 +96,7 @@ async function readSmallFileSafe(filePath, maxBytes) {
91
96
  /** Read+parse `<kimiHome>/config.toml`. `config: null` means the file is
92
97
  * absent (upstream default applies); any read/parse failure fails closed. */
93
98
  async function readParsedConfig(kimiHome) {
94
- const configPath = path.join(kimiHome, "config.toml");
99
+ const configPath = kimiCodeConfigPath(kimiHome);
95
100
  const read = await readSmallFileSafe(configPath, CONFIG_MAX_BYTES);
96
101
  if (read.kind === "absent")
97
102
  return { kind: "ok", config: null };
@@ -104,7 +109,7 @@ async function readParsedConfig(kimiHome) {
104
109
  catch (error) {
105
110
  return {
106
111
  kind: "unreadable",
107
- reason: `${configPath} could not be parsed as TOML: ${error.message}`,
112
+ reason: `${configPath} could not be parsed as TOML: ${tomlErrorLocation(error)}`,
108
113
  };
109
114
  }
110
115
  }
@@ -149,14 +154,23 @@ export async function inspectPlanModeConfig(kimiHome) {
149
154
  const offending = matching.filter(([, value]) => value !== false).map(([rawKey]) => rawKey);
150
155
  if (offending.length === 0)
151
156
  return { kind: "ok", setting: "false" };
152
- const configPath = path.join(kimiHome, "config.toml");
157
+ const configPath = kimiCodeConfigPath(kimiHome);
153
158
  return {
154
159
  kind: "refuse",
155
160
  refusalKind: "v2-plan-mode-configured",
156
161
  reason: `\`${offending.join("`, `")}\` (upstream setting \`default_plan_mode\`) is set in ${configPath} and is not \`false\`.`,
157
162
  };
158
163
  }
159
- export async function inspectEnvironmentConfig(kimiHome) {
164
+ /**
165
+ * `allowDefaultEnvironment`: upstream reads `environments.default` only in
166
+ * sessionManagerService.openCreation, i.e. when a session is created. A fresh
167
+ * print on a certified row passes `--environment local`, which skips the
168
+ * declarations; a resume goes through doResume, which applies the session's
169
+ * stored binding (state.json) and never consults the default (2.5.0-2.7.3).
170
+ * The stored binding is checked separately by scanSessionJournalsForPlan, so
171
+ * that one key stops refusing. A non-table section still refuses.
172
+ */
173
+ export async function inspectEnvironmentConfig(kimiHome, options = {}) {
160
174
  const parsed = await readParsedConfig(kimiHome);
161
175
  if (parsed.kind === "unreadable") {
162
176
  return { kind: "refuse", refusalKind: "v2-config-unreadable", reason: parsed.reason };
@@ -170,7 +184,7 @@ export async function inspectEnvironmentConfig(kimiHome) {
170
184
  reason: "The [environments] section is not a table, so the execution environment cannot be verified as local.",
171
185
  };
172
186
  }
173
- if (section["default"] !== undefined) {
187
+ if (options.allowDefaultEnvironment !== true && section["default"] !== undefined) {
174
188
  return {
175
189
  kind: "refuse",
176
190
  refusalKind: "v2-environment-configured",
@@ -187,17 +201,24 @@ export async function inspectEnvironmentConfig(kimiHome) {
187
201
  reason: "The [workspace] section is not a table, so project trust requirements cannot be verified.",
188
202
  };
189
203
  }
190
- const trustDisabled = Object.entries(workspace).some(([key, value]) => upstreamSnakeToCamel(key) === "requireTrust" && value !== true);
191
- if (trustDisabled) {
204
+ const trustValues = Object.entries(workspace)
205
+ .filter(([key]) => upstreamSnakeToCamel(key) === "requireTrust")
206
+ .map(([, value]) => value);
207
+ // Literal false is the user's own choice and becomes a warning; any other
208
+ // non-true value is malformed and still refuses.
209
+ if (trustValues.some((value) => value !== true && value !== false)) {
192
210
  return {
193
211
  kind: "refuse",
194
212
  refusalKind: "v2-workspace-trust-disabled",
195
- reason: "Workspace trust is disabled in [workspace]; plugin-managed jobs require project trust enforcement to remain enabled.",
213
+ reason: "[workspace] require_trust is not a boolean, so project trust requirements cannot be verified.",
196
214
  };
197
215
  }
216
+ if (trustValues.includes(false))
217
+ return { kind: "ok", trustDisabled: true };
198
218
  }
199
219
  return { kind: "ok" };
200
220
  }
221
+ export const WORKSPACE_TRUST_DISABLED_WARNING = "Workspace trust is disabled in kimi-code's config: MCP servers configured by this repository can start outside the plugin's hook.";
201
222
  /**
202
223
  * Refuse v2-only experimental features this plugin does not certify.
203
224
  * Sources (per-flag precedence upstream is env → [experimental] config →
@@ -282,46 +303,80 @@ async function openJournalSafely(journalPath, maxBytes) {
282
303
  }
283
304
  return { kind: "ok", handle };
284
305
  }
285
- async function scanJournalLines(handle, expectedExecutionRoot) {
286
- const rl = createInterface({ input: handle.createReadStream() });
287
- try {
288
- for await (const rawLine of rl) {
289
- const line = rawLine.trim();
290
- if (line.length === 0)
291
- continue;
292
- let parsed;
293
- try {
294
- parsed = JSON.parse(line);
306
+ /** Records that mean the session already did work: a turn, prompt, tool, model or delegated task. */
307
+ const ACTIVITY_RECORD = /^(turn\.|prompt\.|tool\.|llm\.|shell\.|subagent\.|task\.|assistant\.|context\.append_message$|context\.append_loop_event$)/;
308
+ /** Records that make replay non-linear (branch switch or undo), so a fold could differ. */
309
+ const REWIND_RECORD = /^(agent\.switched$|context\.undo$|context\.undone$)/;
310
+ async function scanJournalLines(handle, expectedExecutionRoot, tolerateCancelledDefaultEnter = false) {
311
+ // G2: a print-created session's main journal holds the create-time
312
+ // `default_plan_mode` enter immediately followed by print's id-less cancel,
313
+ // before any work. On a linear journal that pair folds to inactive, so it
314
+ // alone is tolerated; every other plan record, or that pair anywhere else,
315
+ // still taints.
316
+ // Replay stops at the first corrupt line, so records after it may add
317
+ // taints (seeing more is conservative) but never start or complete a fold.
318
+ let sawActivity = false;
319
+ let sawRewind = false;
320
+ let sawCorrupt = false;
321
+ let pendingEnter = null;
322
+ let toleratedPair = false;
323
+ const taintEnter = { kind: "tainted", recordType: "plan_mode.enter" };
324
+ for await (const framed of kimiJournalRecords(handle.createReadStream())) {
325
+ const { line, parsed } = framed;
326
+ if (parsed === UNPARSEABLE_JOURNAL_LINE) {
327
+ if (framed.terminated)
328
+ sawCorrupt = true;
329
+ if (pendingEnter !== null)
330
+ return taintEnter;
331
+ if (line.includes('"plan_mode.') || line.includes('"plan.')) {
332
+ return { kind: "tainted", recordType: "<unparseable>" };
333
+ }
334
+ if (line.includes('"environment.set_binding')) {
335
+ return { kind: "environment", recordType: "<unparseable-environment-binding>" };
295
336
  }
296
- catch {
297
- if (line.includes('"plan_mode.') || line.includes('"plan.')) {
298
- return { kind: "tainted", recordType: "<unparseable>" };
299
- }
300
- if (line.includes('"environment.set_binding')) {
301
- return { kind: "environment", recordType: "<unparseable-environment-binding>" };
302
- }
337
+ continue;
338
+ }
339
+ const record = parsed;
340
+ const type = record?.["type"];
341
+ if (pendingEnter !== null) {
342
+ const cancelId = record?.["id"];
343
+ const cancels = type === "plan_mode.cancel" && record?.["agentId"] === "main" &&
344
+ (cancelId === undefined || cancelId === pendingEnter.id);
345
+ if (!cancels)
346
+ return taintEnter;
347
+ pendingEnter = null;
348
+ toleratedPair = true;
349
+ continue;
350
+ }
351
+ if (typeof type === "string" && REWIND_RECORD.test(type)) {
352
+ if (toleratedPair)
353
+ return taintEnter;
354
+ sawRewind = true;
355
+ }
356
+ if (typeof type === "string" && ACTIVITY_RECORD.test(type))
357
+ sawActivity = true;
358
+ if (typeof type === "string" && (type.startsWith("plan_mode.") || type.startsWith("plan."))) {
359
+ if (tolerateCancelledDefaultEnter && type === "plan_mode.enter" && !toleratedPair &&
360
+ !sawActivity && !sawRewind && !sawCorrupt && record?.["agentId"] === "main" && typeof record?.["id"] === "string") {
361
+ pendingEnter = { id: record["id"] };
303
362
  continue;
304
363
  }
305
- const record = parsed;
306
- const type = record?.["type"];
307
- if (typeof type === "string" && (type.startsWith("plan_mode.") || type.startsWith("plan."))) {
308
- return { kind: "tainted", recordType: type };
364
+ return { kind: "tainted", recordType: type };
365
+ }
366
+ if (type === "environment.set_binding") {
367
+ if (record?.["environmentId"] !== "local") {
368
+ return { kind: "environment", recordType: type };
309
369
  }
310
- if (type === "environment.set_binding") {
311
- if (record?.["environmentId"] !== "local") {
312
- return { kind: "environment", recordType: type };
313
- }
314
- const cwd = record?.["cwd"];
315
- if (expectedExecutionRoot !== undefined && cwd !== undefined && cwd !== expectedExecutionRoot) {
316
- return { kind: "environment", recordType: "environment.set_binding.cwd" };
317
- }
370
+ const cwd = record?.["cwd"];
371
+ if (expectedExecutionRoot !== undefined && cwd !== undefined && cwd !== expectedExecutionRoot) {
372
+ return { kind: "environment", recordType: "environment.set_binding.cwd" };
318
373
  }
319
374
  }
320
- return null;
321
- }
322
- finally {
323
- rl.close();
324
375
  }
376
+ // An enter as the last record was never cancelled.
377
+ if (pendingEnter !== null)
378
+ return taintEnter;
379
+ return null;
325
380
  }
326
381
  async function inspectSessionMetadataEnvironment(statePath, expectedExecutionRoot) {
327
382
  const read = await readSmallFileSafe(statePath, CONFIG_MAX_BYTES);
@@ -364,7 +419,7 @@ async function inspectSessionMetadataEnvironment(statePath, expectedExecutionRoo
364
419
  * name `environmentId:"local"`. No session dir, no journal, unreadable metadata,
365
420
  * or any single journal above the size cap → unavailable. Never modifies files.
366
421
  */
367
- export async function scanSessionJournalsForPlan(kimiHome, sessionId, expectedExecutionRoot) {
422
+ export async function scanSessionJournalsForPlan(kimiHome, sessionId, expectedExecutionRoot, options = {}) {
368
423
  const sessionsDir = path.join(kimiHome, "sessions");
369
424
  const workspaceEntries = await boundedReaddir(sessionsDir, DIR_ENUM_CAP);
370
425
  if (workspaceEntries.kind === "absent") {
@@ -413,19 +468,19 @@ export async function scanSessionJournalsForPlan(kimiHome, sessionId, expectedEx
413
468
  }
414
469
  if (!agentEntry.isDirectory())
415
470
  continue;
416
- journalPaths.push(path.join(agentsDir, agentEntry.name, "wire.jsonl"));
471
+ journalPaths.push({ path: path.join(agentsDir, agentEntry.name, "wire.jsonl"), main: agentEntry.name === "main" });
417
472
  }
418
473
  }
419
474
  if (journalPaths.length === 0) {
420
475
  return { kind: "unavailable", reason: `no agent wire journals found for session ${sessionId}` };
421
476
  }
422
- for (const journalPath of journalPaths) {
477
+ for (const { path: journalPath, main } of journalPaths) {
423
478
  const opened = await openJournalSafely(journalPath, JOURNAL_MAX_BYTES);
424
479
  if (opened.kind === "unavailable") {
425
480
  return { kind: "unavailable", reason: `${journalPath}: ${opened.reason}` };
426
481
  }
427
482
  try {
428
- const finding = await scanJournalLines(opened.handle, expectedExecutionRoot);
483
+ const finding = await scanJournalLines(opened.handle, expectedExecutionRoot, main && options.tolerateCancelledDefaultEnter === true);
429
484
  if (finding?.kind === "tainted") {
430
485
  return { kind: "tainted", journal: journalPath, recordType: finding.recordType };
431
486
  }
@@ -437,26 +492,32 @@ export async function scanSessionJournalsForPlan(kimiHome, sessionId, expectedEx
437
492
  await opened.handle.close().catch(() => undefined);
438
493
  }
439
494
  }
440
- return { kind: "clean", journals: journalPaths };
495
+ return { kind: "clean", journals: journalPaths.map((journal) => journal.path) };
496
+ }
497
+ /** A non-swarm launch of an exactly certified row that has the capability. */
498
+ function certifiedPrintLaunchHas(options, capability) {
499
+ const release = options.release;
500
+ return release !== undefined &&
501
+ release.certification === "certified" &&
502
+ release.operationKind !== "swarm" &&
503
+ release.operationKind !== "swarm-write" &&
504
+ kimiCertifiedReleaseHas(release.kimiVersion, capability);
441
505
  }
442
- /**
443
- * Run every inspection and throw a RuntimeError on the first refusal:
444
- * CLI_V2_PLAN_MODE_CONFIGURED details.refusal_kind = "v2-plan-mode-configured"
445
- * CLI_V2_CONFIG_UNREADABLE details.refusal_kind = "v2-config-unreadable"
446
- * CLI_V2_EXPERIMENTAL_UNSAFE details.refusal_kind = "v2-experimental-unsafe", details.selectors
447
- * CLI_V2_ENVIRONMENT_UNSAFE / CLI_V2_WORKSPACE_TRUST_UNSAFE for unsafe config
448
- * KIMI_SESSION_ENVIRONMENT_UNSAFE details.refusal_kind = "session-environment-unsafe"
449
- * KIMI_SESSION_PLAN_TAINTED details.refusal_kind = "session-plan-tainted", details.record_type
450
- * KIMI_SESSION_JOURNAL_UNAVAILABLE details.refusal_kind = "session-journal-unavailable"
451
- * Every message carries a truthful remedy (edit the named setting / start a
452
- * fresh session) and MUST NOT mention /kimi:setup or hook drift.
453
- * Every details object also carries `retryable_after_setup: false`.
454
- */
455
506
  export async function assertNativeV2Preflight(options) {
507
+ const warnings = [];
456
508
  const stage = options.stage ?? "native-v2.preflight";
457
- const configPath = path.join(options.kimiHome, "config.toml");
509
+ const configPath = kimiCodeConfigPath(options.kimiHome);
510
+ // On a print-cancels-plan row the setting is read only inside
511
+ // sessionLifecycleService.create(): a fresh print cancels that enter before
512
+ // any turn (run-v2-print.ts), and `-r` on an existing session goes through
513
+ // doResume, which never consults it (a missing id fails, never creates).
514
+ // So the setting no longer refuses there; for a resume the journal scan
515
+ // below remains the gate. An unreadable config still refuses.
458
516
  const planMode = await inspectPlanModeConfig(options.kimiHome);
459
- if (planMode.kind === "refuse") {
517
+ const printCancelsPlan = planMode.kind === "refuse" &&
518
+ planMode.refusalKind === "v2-plan-mode-configured" &&
519
+ certifiedPrintLaunchHas(options, "print-cancels-plan");
520
+ if (planMode.kind === "refuse" && !printCancelsPlan) {
460
521
  const code = planMode.refusalKind === "v2-plan-mode-configured"
461
522
  ? "CLI_V2_PLAN_MODE_CONFIGURED"
462
523
  : "CLI_V2_CONFIG_UNREADABLE";
@@ -467,7 +528,9 @@ export async function assertNativeV2Preflight(options) {
467
528
  details: { refusal_kind: planMode.refusalKind, retryable_after_setup: false },
468
529
  });
469
530
  }
470
- const environment = await inspectEnvironmentConfig(options.kimiHome);
531
+ const environment = await inspectEnvironmentConfig(options.kimiHome, {
532
+ allowDefaultEnvironment: certifiedPrintLaunchHas(options, "local-environment"),
533
+ });
471
534
  if (environment.kind === "refuse") {
472
535
  const code = environment.refusalKind === "v2-environment-configured"
473
536
  ? "CLI_V2_ENVIRONMENT_UNSAFE"
@@ -483,7 +546,14 @@ export async function assertNativeV2Preflight(options) {
483
546
  details: { refusal_kind: environment.refusalKind, retryable_after_setup: false },
484
547
  });
485
548
  }
486
- const experimental = await inspectExperimentalSelectors(options.kimiHome, options.env);
549
+ if (environment.trustDisabled === true)
550
+ warnings.push(WORKSPACE_TRUST_DISABLED_WARNING);
551
+ // Certified and test-bypass children run with tower and subagent fork forced
552
+ // off by per-flag env, which outranks config and the master flag on every
553
+ // reviewed release. An uncertified release's flag names are unreviewed.
554
+ const experimental = options.release === undefined || options.release.certification === "uncertified"
555
+ ? await inspectExperimentalSelectors(options.kimiHome, options.env)
556
+ : { kind: "ok" };
487
557
  if (experimental.kind === "refuse") {
488
558
  throw new RuntimeError("CLI_V2_EXPERIMENTAL_UNSAFE", `${experimental.reason} Unset the listed selector(s) before retrying.`, stage, {
489
559
  details: {
@@ -494,7 +564,7 @@ export async function assertNativeV2Preflight(options) {
494
564
  });
495
565
  }
496
566
  if (options.resumeSessionId !== undefined) {
497
- const journalScan = await scanSessionJournalsForPlan(options.kimiHome, options.resumeSessionId, options.expectedExecutionRoot);
567
+ const journalScan = await scanSessionJournalsForPlan(options.kimiHome, options.resumeSessionId, options.expectedExecutionRoot, { tolerateCancelledDefaultEnter: certifiedPrintLaunchHas(options, "print-cancels-plan") });
498
568
  if (journalScan.kind === "environment") {
499
569
  throw new RuntimeError("KIMI_SESSION_ENVIRONMENT_UNSAFE", `Saved session ${options.resumeSessionId} has an unsafe execution environment binding (${journalScan.recordType} in ${journalScan.journal}). Start a fresh local session; saved session state was not modified.`, stage, {
500
570
  details: {
@@ -522,4 +592,5 @@ export async function assertNativeV2Preflight(options) {
522
592
  });
523
593
  }
524
594
  }
595
+ return { warnings };
525
596
  }