kimi-plugin-cc 2.7.3 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +3 -16
- package/README.md +428 -515
- package/assets/logo.svg +6 -0
- package/cordis.patch.yml +18 -0
- package/dist/background-spawn.js +14 -19
- package/dist/cli-client.js +163 -105
- package/dist/commands/ask.js +44 -96
- package/dist/commands/cancel.js +11 -59
- package/dist/commands/cli-helpers.js +102 -1
- package/dist/commands/models.js +2 -3
- package/dist/commands/pursue.js +8 -21
- package/dist/commands/registry.js +7 -26
- package/dist/commands/repair-sessions.js +121 -12
- package/dist/commands/replay.js +4 -3
- package/dist/commands/rescue.js +29 -87
- package/dist/commands/result.js +7 -1
- package/dist/commands/review.js +28 -18
- package/dist/commands/setup.js +813 -462
- package/dist/commands/status.js +2 -2
- package/dist/commands/swarm.js +112 -125
- package/dist/commands/worker.js +1 -2
- package/dist/companion.js +145 -19
- package/dist/git.js +1 -9
- package/dist/help.js +13 -9
- package/dist/hook-escape.js +7 -16
- package/dist/hooks/approval-hook.js +1766 -87
- package/dist/hooks/approval-policy.js +86 -19
- package/dist/hooks/config-safety.js +31 -64
- package/dist/hooks/hook-timing.js +48 -0
- package/dist/hooks/install-paths.js +179 -107
- package/dist/hooks/install.js +126 -53
- package/dist/hooks/managed-block.js +121 -127
- package/dist/job-store.js +206 -166
- package/dist/jobs.js +66 -19
- package/dist/kimi-command.js +74 -1
- package/dist/kimi-engine.js +198 -127
- package/dist/kimi-errors.js +15 -70
- package/dist/kimi-home.js +6 -0
- package/dist/kimi-journal.js +42 -0
- package/dist/kimi-releases.js +123 -0
- package/dist/kimi-timeouts.js +2 -16
- package/dist/kimi-version-probe.js +24 -108
- package/dist/logging.js +12 -7
- package/dist/mcp/binding.js +13 -4
- package/dist/mcp/job-control.js +390 -95
- package/dist/mcp/launcher.js +13 -7
- package/dist/mcp/operations.js +405 -179
- package/dist/mcp/protocol.js +124 -33
- package/dist/mcp/skills.js +130 -0
- package/dist/mcp/stdio.js +18 -4
- package/dist/model-command.js +5 -3
- package/dist/native-v2-preflight.js +243 -80
- package/dist/parsing.js +211 -243
- package/dist/paths.js +28 -10
- package/dist/plugin-data.js +28 -83
- package/dist/process-tree.js +35 -24
- package/dist/read-scope.js +435 -0
- package/dist/render.js +58 -46
- package/dist/rescue-approval.js +461 -84
- package/dist/session-references.js +216 -0
- package/dist/session-title.js +26 -6
- package/dist/stream-json.js +1 -3
- package/dist/surface-routing.js +108 -0
- package/dist/swarm-activation.js +74 -188
- package/dist/swarm-policy.js +2 -1
- package/dist/swarm-recovery.js +2 -1
- package/dist/util.js +28 -0
- package/dist/version.js +1 -1
- package/docs/migration.md +45 -206
- package/docs/models.md +24 -66
- package/docs/safety.md +198 -353
- package/docs/session-visibility.md +25 -13
- package/docs/swarm-recovery.md +26 -20
- package/dsh/index.json +54 -0
- package/mcp_config.json +10 -0
- package/package.json +23 -82
- package/plugin.json +19 -0
- package/plugins/kimi-codex/.codex-plugin/plugin.json +7 -7
- package/plugins/kimi-codex/.mcp.json +3 -3
- package/plugins/kimi-codex/dist/background-spawn.js +14 -19
- package/plugins/kimi-codex/dist/cli-client.js +163 -105
- package/plugins/kimi-codex/dist/commands/ask.js +44 -96
- package/plugins/kimi-codex/dist/commands/cancel.js +11 -59
- package/plugins/kimi-codex/dist/commands/cli-helpers.js +102 -1
- package/plugins/kimi-codex/dist/commands/models.js +2 -3
- package/plugins/kimi-codex/dist/commands/pursue.js +8 -21
- package/plugins/kimi-codex/dist/commands/registry.js +7 -26
- package/plugins/kimi-codex/dist/commands/repair-sessions.js +121 -12
- package/plugins/kimi-codex/dist/commands/replay.js +4 -3
- package/plugins/kimi-codex/dist/commands/rescue.js +29 -87
- package/plugins/kimi-codex/dist/commands/result.js +7 -1
- package/plugins/kimi-codex/dist/commands/review.js +28 -18
- package/plugins/kimi-codex/dist/commands/setup.js +813 -462
- package/plugins/kimi-codex/dist/commands/status.js +2 -2
- package/plugins/kimi-codex/dist/commands/swarm.js +112 -125
- package/plugins/kimi-codex/dist/commands/worker.js +1 -2
- package/plugins/kimi-codex/dist/companion.js +145 -19
- package/plugins/kimi-codex/dist/git.js +1 -9
- package/plugins/kimi-codex/dist/help.js +13 -9
- package/plugins/kimi-codex/dist/hook-escape.js +7 -16
- package/plugins/kimi-codex/dist/hooks/approval-hook.js +1766 -87
- package/plugins/kimi-codex/dist/hooks/approval-policy.js +86 -19
- package/plugins/kimi-codex/dist/hooks/config-safety.js +31 -64
- package/plugins/kimi-codex/dist/hooks/hook-timing.js +48 -0
- package/plugins/kimi-codex/dist/hooks/install-paths.js +179 -107
- package/plugins/kimi-codex/dist/hooks/install.js +126 -53
- package/plugins/kimi-codex/dist/hooks/managed-block.js +121 -127
- package/plugins/kimi-codex/dist/job-store.js +206 -166
- package/plugins/kimi-codex/dist/jobs.js +66 -19
- package/plugins/kimi-codex/dist/kimi-command.js +74 -1
- package/plugins/kimi-codex/dist/kimi-engine.js +198 -127
- package/plugins/kimi-codex/dist/kimi-errors.js +15 -70
- package/plugins/kimi-codex/dist/kimi-home.js +6 -0
- package/plugins/kimi-codex/dist/kimi-journal.js +42 -0
- package/plugins/kimi-codex/dist/kimi-releases.js +123 -0
- package/plugins/kimi-codex/dist/kimi-timeouts.js +2 -16
- package/plugins/kimi-codex/dist/kimi-version-probe.js +24 -108
- package/plugins/kimi-codex/dist/logging.js +12 -7
- package/plugins/kimi-codex/dist/mcp/binding.js +13 -4
- package/plugins/kimi-codex/dist/mcp/job-control.js +390 -95
- package/plugins/kimi-codex/dist/mcp/launcher.js +13 -7
- package/plugins/kimi-codex/dist/mcp/operations.js +405 -179
- package/plugins/kimi-codex/dist/mcp/protocol.js +124 -33
- package/plugins/kimi-codex/dist/mcp/skills.js +130 -0
- package/plugins/kimi-codex/dist/mcp/stdio.js +18 -4
- package/plugins/kimi-codex/dist/model-command.js +5 -3
- package/plugins/kimi-codex/dist/native-v2-preflight.js +243 -80
- package/plugins/kimi-codex/dist/parsing.js +211 -243
- package/plugins/kimi-codex/dist/paths.js +28 -10
- package/plugins/kimi-codex/dist/plugin-data.js +28 -83
- package/plugins/kimi-codex/dist/process-tree.js +35 -24
- package/plugins/kimi-codex/dist/read-scope.js +435 -0
- package/plugins/kimi-codex/dist/render.js +58 -46
- package/plugins/kimi-codex/dist/rescue-approval.js +461 -84
- package/plugins/kimi-codex/dist/session-references.js +216 -0
- package/plugins/kimi-codex/dist/session-title.js +26 -6
- package/plugins/kimi-codex/dist/stream-json.js +1 -3
- package/plugins/kimi-codex/dist/surface-routing.js +108 -0
- package/plugins/kimi-codex/dist/swarm-activation.js +74 -188
- package/plugins/kimi-codex/dist/swarm-policy.js +2 -1
- package/plugins/kimi-codex/dist/swarm-recovery.js +2 -1
- package/plugins/kimi-codex/dist/util.js +28 -0
- package/plugins/kimi-codex/dist/version.js +1 -1
- package/plugins/kimi-codex/scripts/companion.sh +4 -15
- package/plugins/kimi-codex/skills/kimi/SKILL.md +77 -0
- package/plugins/kimi-codex/skills/kimi/agents/openai.yaml +6 -0
- package/scripts/companion.sh +4 -15
- package/skills/kimi/SKILL.md +77 -0
- package/skills/kimi/agents/openai.yaml +6 -0
- package/CHANGELOG.md +0 -1333
- package/agents/kimi-ask.md +0 -89
- package/agents/kimi-challenge.md +0 -86
- package/agents/kimi-pursue.md +0 -93
- package/agents/kimi-rescue.md +0 -92
- package/agents/kimi-review.md +0 -86
- package/agents/kimi-swarm-write.md +0 -96
- package/agents/kimi-swarm.md +0 -86
- package/commands/README.md +0 -16
- package/commands/ask.md +0 -38
- package/commands/cancel.md +0 -17
- package/commands/challenge.md +0 -35
- package/commands/pursue.md +0 -41
- package/commands/replay.md +0 -13
- package/commands/rescue.md +0 -38
- package/commands/result.md +0 -13
- package/commands/review.md +0 -35
- package/commands/setup.md +0 -23
- package/commands/status.md +0 -13
- package/commands/swarm.md +0 -44
- package/dist/commands/not-implemented.js +0 -4
- package/dist/commands/review-gate.js +0 -390
- package/dist/config.js +0 -22
- package/dist/dsh/companion.js +0 -177
- package/dist/dsh/index.js +0 -169
- package/dist/hooks/review-gate-stop.js +0 -19
- package/dist/schemas/review-gate-output.js +0 -43
- package/docs/registry-releases.md +0 -228
- package/docs/upstream-2.4.1-certification.md +0 -54
- package/docs/upstream-2.5.0-certification.md +0 -52
- package/docs/upstream-2.5.1-certification.md +0 -47
- package/docs/upstream-2.5.2-certification.md +0 -40
- package/docs/upstream-2.5.3-certification.md +0 -73
- package/docs/upstream-2.6.0-certification.md +0 -65
- package/docs/upstream-2.6.2-certification.md +0 -144
- package/docs/upstream-2.6.3-certification.md +0 -100
- package/docs/upstream-2.7.2-certification.md +0 -113
- package/docs/upstream-2.7.3-certification.md +0 -83
- package/docs/windows-validation.md +0 -39
- package/hooks/README.md +0 -9
- package/hooks/hooks.json +0 -16
- package/plugins/kimi-codex/dist/commands/not-implemented.js +0 -4
- package/plugins/kimi-codex/dist/commands/review-gate.js +0 -390
- package/plugins/kimi-codex/dist/config.js +0 -22
- package/plugins/kimi-codex/dist/dsh/companion.js +0 -177
- package/plugins/kimi-codex/dist/dsh/index.js +0 -169
- package/plugins/kimi-codex/dist/hooks/review-gate-stop.js +0 -19
- package/plugins/kimi-codex/dist/schemas/review-gate-output.js +0 -43
- package/plugins/kimi-codex/scripts/review-gate-hook.sh +0 -32
- package/plugins/kimi-codex/skills/kimi-ask/SKILL.md +0 -35
- package/plugins/kimi-codex/skills/kimi-ask/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-cancel/SKILL.md +0 -25
- package/plugins/kimi-codex/skills/kimi-cancel/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-challenge/SKILL.md +0 -34
- package/plugins/kimi-codex/skills/kimi-challenge/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-pursue/SKILL.md +0 -32
- package/plugins/kimi-codex/skills/kimi-pursue/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-replay/SKILL.md +0 -25
- package/plugins/kimi-codex/skills/kimi-replay/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-rescue/SKILL.md +0 -34
- package/plugins/kimi-codex/skills/kimi-rescue/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-result/SKILL.md +0 -25
- package/plugins/kimi-codex/skills/kimi-result/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-review/SKILL.md +0 -34
- package/plugins/kimi-codex/skills/kimi-review/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-setup/SKILL.md +0 -34
- package/plugins/kimi-codex/skills/kimi-setup/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-status/SKILL.md +0 -25
- package/plugins/kimi-codex/skills/kimi-status/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-swarm/SKILL.md +0 -35
- package/plugins/kimi-codex/skills/kimi-swarm/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-swarm-write/SKILL.md +0 -36
- package/plugins/kimi-codex/skills/kimi-swarm-write/agents/openai.yaml +0 -6
- package/plugins/kimi-dsh/LICENSE +0 -201
- package/plugins/kimi-dsh/README.md +0 -83
- package/plugins/kimi-dsh/cordis.patch.yml +0 -45
- package/plugins/kimi-dsh/dist/background-spawn.js +0 -216
- package/plugins/kimi-dsh/dist/cli-cancellation.js +0 -43
- package/plugins/kimi-dsh/dist/cli-client.js +0 -1078
- package/plugins/kimi-dsh/dist/commands/ask.js +0 -336
- package/plugins/kimi-dsh/dist/commands/cancel.js +0 -216
- package/plugins/kimi-dsh/dist/commands/cli-helpers.js +0 -201
- package/plugins/kimi-dsh/dist/commands/models.js +0 -140
- package/plugins/kimi-dsh/dist/commands/not-implemented.js +0 -4
- package/plugins/kimi-dsh/dist/commands/pursue.js +0 -307
- package/plugins/kimi-dsh/dist/commands/recover-swarm.js +0 -140
- package/plugins/kimi-dsh/dist/commands/registry.js +0 -74
- package/plugins/kimi-dsh/dist/commands/repair-sessions.js +0 -160
- package/plugins/kimi-dsh/dist/commands/replay.js +0 -124
- package/plugins/kimi-dsh/dist/commands/rescue.js +0 -347
- package/plugins/kimi-dsh/dist/commands/result.js +0 -66
- package/plugins/kimi-dsh/dist/commands/review-gate.js +0 -390
- package/plugins/kimi-dsh/dist/commands/review.js +0 -220
- package/plugins/kimi-dsh/dist/commands/setup.js +0 -1129
- package/plugins/kimi-dsh/dist/commands/status.js +0 -27
- package/plugins/kimi-dsh/dist/commands/swarm.js +0 -849
- package/plugins/kimi-dsh/dist/commands/worker.js +0 -25
- package/plugins/kimi-dsh/dist/companion.js +0 -97
- package/plugins/kimi-dsh/dist/config.js +0 -22
- package/plugins/kimi-dsh/dist/dsh/companion.js +0 -177
- package/plugins/kimi-dsh/dist/dsh/index.js +0 -169
- package/plugins/kimi-dsh/dist/errors.js +0 -40
- package/plugins/kimi-dsh/dist/git.js +0 -224
- package/plugins/kimi-dsh/dist/help.js +0 -43
- package/plugins/kimi-dsh/dist/hook-escape.js +0 -71
- package/plugins/kimi-dsh/dist/hooks/approval-hook.js +0 -104
- package/plugins/kimi-dsh/dist/hooks/approval-policy.js +0 -233
- package/plugins/kimi-dsh/dist/hooks/config-safety.js +0 -665
- package/plugins/kimi-dsh/dist/hooks/install-paths.js +0 -428
- package/plugins/kimi-dsh/dist/hooks/install.js +0 -210
- package/plugins/kimi-dsh/dist/hooks/managed-block.js +0 -684
- package/plugins/kimi-dsh/dist/hooks/review-gate-stop.js +0 -19
- package/plugins/kimi-dsh/dist/job-id.js +0 -12
- package/plugins/kimi-dsh/dist/job-store.js +0 -659
- package/plugins/kimi-dsh/dist/jobs.js +0 -217
- package/plugins/kimi-dsh/dist/kimi-command.js +0 -71
- package/plugins/kimi-dsh/dist/kimi-engine.js +0 -675
- package/plugins/kimi-dsh/dist/kimi-errors.js +0 -260
- package/plugins/kimi-dsh/dist/kimi-home.js +0 -9
- package/plugins/kimi-dsh/dist/kimi-timeouts.js +0 -52
- package/plugins/kimi-dsh/dist/kimi-version-probe.js +0 -234
- package/plugins/kimi-dsh/dist/logging.js +0 -22
- package/plugins/kimi-dsh/dist/mcp/binding.js +0 -58
- package/plugins/kimi-dsh/dist/mcp/job-control.js +0 -345
- package/plugins/kimi-dsh/dist/mcp/launcher.js +0 -257
- package/plugins/kimi-dsh/dist/mcp/operations.js +0 -301
- package/plugins/kimi-dsh/dist/mcp/protocol.js +0 -275
- package/plugins/kimi-dsh/dist/mcp/server.js +0 -8
- package/plugins/kimi-dsh/dist/mcp/stdio.js +0 -78
- package/plugins/kimi-dsh/dist/mcp/supervised-child.js +0 -15
- package/plugins/kimi-dsh/dist/mcp/tool-result.js +0 -7
- package/plugins/kimi-dsh/dist/mcp/workspace.js +0 -34
- package/plugins/kimi-dsh/dist/model-command.js +0 -32
- package/plugins/kimi-dsh/dist/native-v2-preflight.js +0 -525
- package/plugins/kimi-dsh/dist/package.json +0 -1
- package/plugins/kimi-dsh/dist/parsing.js +0 -620
- package/plugins/kimi-dsh/dist/paths.js +0 -27
- package/plugins/kimi-dsh/dist/plugin-data.js +0 -98
- package/plugins/kimi-dsh/dist/process-tree.js +0 -586
- package/plugins/kimi-dsh/dist/prompt-size.js +0 -18
- package/plugins/kimi-dsh/dist/render.js +0 -177
- package/plugins/kimi-dsh/dist/rescue-approval.js +0 -865
- package/plugins/kimi-dsh/dist/schemas/review-gate-output.js +0 -43
- package/plugins/kimi-dsh/dist/session-recovery.js +0 -202
- package/plugins/kimi-dsh/dist/session-title.js +0 -293
- package/plugins/kimi-dsh/dist/stream-json.js +0 -346
- package/plugins/kimi-dsh/dist/swarm-activation.js +0 -1197
- package/plugins/kimi-dsh/dist/swarm-policy.js +0 -73
- package/plugins/kimi-dsh/dist/swarm-recovery.js +0 -165
- package/plugins/kimi-dsh/dist/swarm-tool-policy.js +0 -146
- package/plugins/kimi-dsh/dist/types.js +0 -1
- package/plugins/kimi-dsh/dist/vendor/shell-quote/LICENSE +0 -24
- package/plugins/kimi-dsh/dist/vendor/shell-quote/index.js +0 -1
- package/plugins/kimi-dsh/dist/vendor/shell-quote/parse.js +0 -229
- package/plugins/kimi-dsh/dist/vendor/smol-toml/LICENSE +0 -24
- package/plugins/kimi-dsh/dist/vendor/smol-toml/date.js +0 -156
- package/plugins/kimi-dsh/dist/vendor/smol-toml/error.js +0 -67
- package/plugins/kimi-dsh/dist/vendor/smol-toml/extract.js +0 -293
- package/plugins/kimi-dsh/dist/vendor/smol-toml/parse.js +0 -164
- package/plugins/kimi-dsh/dist/vendor/smol-toml/primitive.js +0 -151
- package/plugins/kimi-dsh/dist/vendor/smol-toml/struct.js +0 -156
- package/plugins/kimi-dsh/dist/vendor/smol-toml/util.js +0 -57
- package/plugins/kimi-dsh/dist/version.js +0 -7
- package/plugins/kimi-dsh/package.json +0 -78
- package/plugins/kimi-dsh/scripts/companion.sh +0 -37
- package/plugins/kimi-dsh/scripts/review-gate-hook.sh +0 -32
- package/scripts/review-gate-hook.sh +0 -32
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { realpath, stat } from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { RuntimeError } from "../errors.js";
|
|
4
|
-
import { resolveRepoIdentity } from "../git.js";
|
|
5
|
-
export async function resolveMcpWorkspace(env, requested) {
|
|
6
|
-
const defaultCandidate = env.KIMI_PLUGIN_CC_MCP_WORKSPACE;
|
|
7
|
-
const candidate = requested ?? defaultCandidate;
|
|
8
|
-
if (!candidate) {
|
|
9
|
-
throw new RuntimeError("MCP_WORKSPACE_REQUIRED", "This MCP server has no default workspace. Supply the absolute workspace directory explicitly; Codex calls normally require it.", "mcp.workspace");
|
|
10
|
-
}
|
|
11
|
-
const cwd = await canonicalDirectory(candidate, "workspace");
|
|
12
|
-
if (defaultCandidate) {
|
|
13
|
-
const defaultCwd = await canonicalDirectory(defaultCandidate, "host workspace");
|
|
14
|
-
const relative = path.relative(defaultCwd, cwd);
|
|
15
|
-
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
16
|
-
throw new RuntimeError("MCP_WORKSPACE_OUTSIDE_DEFAULT", `workspace must be the host workspace or one of its descendants, not ${cwd} (host: ${defaultCwd}).`, "mcp.workspace");
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return { cwd, repo: await resolveRepoIdentity(cwd) };
|
|
20
|
-
}
|
|
21
|
-
async function canonicalDirectory(candidate, label) {
|
|
22
|
-
if (!path.isAbsolute(candidate)) {
|
|
23
|
-
throw new RuntimeError("MCP_WORKSPACE_NOT_ABSOLUTE", `${label} must be an absolute existing directory.`, "mcp.workspace");
|
|
24
|
-
}
|
|
25
|
-
try {
|
|
26
|
-
const cwd = await realpath(candidate);
|
|
27
|
-
if (!(await stat(cwd)).isDirectory())
|
|
28
|
-
throw new Error("not a directory");
|
|
29
|
-
return cwd;
|
|
30
|
-
}
|
|
31
|
-
catch (error) {
|
|
32
|
-
throw new RuntimeError("MCP_WORKSPACE_INVALID", `${label} is not an accessible existing directory: ${candidate}`, "mcp.workspace", error instanceof Error ? { cause: error } : undefined);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { runAsk } from "./commands/ask.js";
|
|
2
|
-
import { notImplementedCompanionCommand } from "./commands/not-implemented.js";
|
|
3
|
-
import { runPursue } from "./commands/pursue.js";
|
|
4
|
-
import { runRescue } from "./commands/rescue.js";
|
|
5
|
-
import { runReview } from "./commands/review.js";
|
|
6
|
-
import { runSwarm } from "./commands/swarm.js";
|
|
7
|
-
export async function runModelCommand(argv, context) {
|
|
8
|
-
const [command, ...rest] = argv;
|
|
9
|
-
switch (command) {
|
|
10
|
-
case "review":
|
|
11
|
-
return `${await runReview(rest, context, "review")}\n`;
|
|
12
|
-
case "ask":
|
|
13
|
-
return `${await runAsk(rest, context)}\n`;
|
|
14
|
-
case "task": {
|
|
15
|
-
const [taskType, ...taskArgs] = rest;
|
|
16
|
-
switch (taskType) {
|
|
17
|
-
case "rescue":
|
|
18
|
-
return runRescue(taskArgs, context);
|
|
19
|
-
case "challenge":
|
|
20
|
-
return `${await runReview(taskArgs, context, "challenge")}\n`;
|
|
21
|
-
case "pursue":
|
|
22
|
-
return runPursue(taskArgs, context);
|
|
23
|
-
case "swarm":
|
|
24
|
-
return runSwarm(taskArgs, context);
|
|
25
|
-
default:
|
|
26
|
-
notImplementedCompanionCommand(taskType ? `task ${taskType}` : "task");
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
default:
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,525 +0,0 @@
|
|
|
1
|
-
// Native-v2 pre-spawn safety preflight.
|
|
2
|
-
//
|
|
3
|
-
// Why this module exists:
|
|
4
|
-
//
|
|
5
|
-
// kimi-code 0.42.0 removed the legacy-v1 engine. agent-core-v2's ONLY
|
|
6
|
-
// chain-breaking `event.allow()` is the plan-file guard
|
|
7
|
-
// (features/plan/planService.ts:110), and it fires only while plan mode is
|
|
8
|
-
// ACTIVE. Plan mode can arm in a plugin-spawned `kimi -p` session in exactly
|
|
9
|
-
// three ways:
|
|
10
|
-
// A. `default_plan_mode = true` in <KIMI_CODE_HOME>/config.toml, read ONCE
|
|
11
|
-
// inside sessions.create() (single user file; no env, argv or project
|
|
12
|
-
// overlay; invalid values fall back to false upstream);
|
|
13
|
-
// B. the EnterPlanMode tool (already hook-denied for every label);
|
|
14
|
-
// C. resuming a session whose agent wire journal holds a durable
|
|
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.
|
|
19
|
-
// B stays in runtime/hooks/approval-policy.ts.
|
|
20
|
-
//
|
|
21
|
-
// Contract:
|
|
22
|
-
// - Pure inspection: never writes, never rewrites operator config, never
|
|
23
|
-
// clears saved plan state. Refusals name the setting, never dump config.
|
|
24
|
-
// - Fail-closed: unparseable/unreadable/oversized inputs refuse.
|
|
25
|
-
// - Strictly more conservative than upstream (a non-boolean value that
|
|
26
|
-
// upstream would ignore still refuses here).
|
|
27
|
-
// - Refusals here are NOT /kimi:setup-recoverable and must never be
|
|
28
|
-
// described as hook drift.
|
|
29
|
-
import { constants as fsConstants } from "node:fs";
|
|
30
|
-
import { open, readdir } from "node:fs/promises";
|
|
31
|
-
import path from "node:path";
|
|
32
|
-
import { createInterface } from "node:readline";
|
|
33
|
-
import { RuntimeError } from "./errors.js";
|
|
34
|
-
import { parse as parseToml } from "./vendor/smol-toml/parse.js";
|
|
35
|
-
/** `<kimiHome>/config.toml` is small hand-edited config, never a data file. */
|
|
36
|
-
const CONFIG_MAX_BYTES = 1 * 1024 * 1024;
|
|
37
|
-
/** A single agent's wire journal; generous but bounded so a runaway session
|
|
38
|
-
* log can't turn a preflight check into an unbounded read. */
|
|
39
|
-
const JOURNAL_MAX_BYTES = 64 * 1024 * 1024;
|
|
40
|
-
/** Bounded directory enumeration so a pathological sessions/ tree refuses
|
|
41
|
-
* instead of scanning forever. */
|
|
42
|
-
const DIR_ENUM_CAP = 4096;
|
|
43
|
-
const TRUTHY_STRINGS = new Set(["1", "true", "yes", "on"]);
|
|
44
|
-
function isTruthySelectorValue(value) {
|
|
45
|
-
if (value === true)
|
|
46
|
-
return true;
|
|
47
|
-
if (typeof value === "string")
|
|
48
|
-
return TRUTHY_STRINGS.has(value.trim().toLowerCase());
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Read a small file with the same no-follow, size-capped posture as the
|
|
53
|
-
* config lock reader in runtime/hooks/config-safety.ts. Never partial-reads:
|
|
54
|
-
* an oversized file refuses rather than being silently truncated.
|
|
55
|
-
*/
|
|
56
|
-
async function readSmallFileSafe(filePath, maxBytes) {
|
|
57
|
-
let handle;
|
|
58
|
-
try {
|
|
59
|
-
handle = await open(filePath, fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0) | (fsConstants.O_NONBLOCK ?? 0));
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
const code = error.code;
|
|
63
|
-
if (code === "ENOENT")
|
|
64
|
-
return { kind: "absent" };
|
|
65
|
-
if (code === "ELOOP")
|
|
66
|
-
return { kind: "unreadable", reason: "path is a symbolic link" };
|
|
67
|
-
return { kind: "unreadable", reason: error.message };
|
|
68
|
-
}
|
|
69
|
-
try {
|
|
70
|
-
const stats = await handle.stat({ bigint: true });
|
|
71
|
-
if (!stats.isFile())
|
|
72
|
-
return { kind: "unreadable", reason: "not a regular file" };
|
|
73
|
-
if (stats.size > BigInt(maxBytes)) {
|
|
74
|
-
return { kind: "unreadable", reason: `exceeds ${maxBytes} byte cap` };
|
|
75
|
-
}
|
|
76
|
-
const size = Number(stats.size);
|
|
77
|
-
const buffer = Buffer.alloc(size);
|
|
78
|
-
let offset = 0;
|
|
79
|
-
while (offset < size) {
|
|
80
|
-
const { bytesRead } = await handle.read(buffer, offset, size - offset, offset);
|
|
81
|
-
if (bytesRead === 0)
|
|
82
|
-
break;
|
|
83
|
-
offset += bytesRead;
|
|
84
|
-
}
|
|
85
|
-
return { kind: "ok", contents: buffer.subarray(0, offset).toString("utf8") };
|
|
86
|
-
}
|
|
87
|
-
finally {
|
|
88
|
-
await handle.close().catch(() => undefined);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/** Read+parse `<kimiHome>/config.toml`. `config: null` means the file is
|
|
92
|
-
* absent (upstream default applies); any read/parse failure fails closed. */
|
|
93
|
-
async function readParsedConfig(kimiHome) {
|
|
94
|
-
const configPath = path.join(kimiHome, "config.toml");
|
|
95
|
-
const read = await readSmallFileSafe(configPath, CONFIG_MAX_BYTES);
|
|
96
|
-
if (read.kind === "absent")
|
|
97
|
-
return { kind: "ok", config: null };
|
|
98
|
-
if (read.kind === "unreadable") {
|
|
99
|
-
return { kind: "unreadable", reason: `${configPath} could not be read safely: ${read.reason}` };
|
|
100
|
-
}
|
|
101
|
-
try {
|
|
102
|
-
return { kind: "ok", config: parseToml(read.contents) };
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
return {
|
|
106
|
-
kind: "unreadable",
|
|
107
|
-
reason: `${configPath} could not be parsed as TOML: ${error.message}`,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Upstream's EXACT top-level key normalization
|
|
113
|
-
* (`packages/agent-core-v2/src/app/config/toml.ts::snakeToCamel`, applied to
|
|
114
|
-
* every top-level key by `transformTomlData` before the config registry sees
|
|
115
|
-
* it). The plan section is registered under the camelCase domain
|
|
116
|
-
* `defaultPlanMode`, so the operator can spell it `default_plan_mode`,
|
|
117
|
-
* `defaultPlanMode`, `default_planMode`, … and upstream reads the same
|
|
118
|
-
* setting. The plugin MUST inspect the same normalized view — a literal
|
|
119
|
-
* `default_plan_mode` check lets `defaultPlanMode = true` arm plan mode.
|
|
120
|
-
* Pinned by tests/audit/v2-tag-scan.test.ts (toml.ts hash + regex text).
|
|
121
|
-
* NOTE: the `[experimental]` section registers its own `fromToml` that keeps
|
|
122
|
-
* raw keys, and its flag ids are snake_case (`subagent_fork`), so that table
|
|
123
|
-
* is deliberately NOT normalized here.
|
|
124
|
-
*/
|
|
125
|
-
export function upstreamSnakeToCamel(key) {
|
|
126
|
-
return key.replace(/_([a-z])/g, (_match, ch) => ch.toUpperCase());
|
|
127
|
-
}
|
|
128
|
-
const DEFAULT_PLAN_MODE_DOMAIN = "defaultPlanMode";
|
|
129
|
-
/**
|
|
130
|
-
* Inspect `<kimiHome>/config.toml` for the upstream default-plan-mode setting
|
|
131
|
-
* under EVERY spelling upstream normalizes to the `defaultPlanMode` domain.
|
|
132
|
-
* ENOENT → ok:"absent" (upstream default is false; the missing hook is caught
|
|
133
|
-
* separately). Any other read error, a symlinked file (open with O_NOFOLLOW),
|
|
134
|
-
* an oversized file, or a TOML parse failure → refuse "v2-config-unreadable".
|
|
135
|
-
* Every matching key holding literal boolean `false` → ok:"false". ANY matching
|
|
136
|
-
* key with any other value (true, string, number, table, array) → refuse
|
|
137
|
-
* "v2-plan-mode-configured", naming the raw key(s) found.
|
|
138
|
-
*/
|
|
139
|
-
export async function inspectPlanModeConfig(kimiHome) {
|
|
140
|
-
const parsed = await readParsedConfig(kimiHome);
|
|
141
|
-
if (parsed.kind === "unreadable") {
|
|
142
|
-
return { kind: "refuse", refusalKind: "v2-config-unreadable", reason: parsed.reason };
|
|
143
|
-
}
|
|
144
|
-
if (parsed.config === null)
|
|
145
|
-
return { kind: "ok", setting: "absent" };
|
|
146
|
-
const matching = Object.entries(parsed.config).filter(([rawKey]) => upstreamSnakeToCamel(rawKey) === DEFAULT_PLAN_MODE_DOMAIN);
|
|
147
|
-
if (matching.length === 0)
|
|
148
|
-
return { kind: "ok", setting: "absent" };
|
|
149
|
-
const offending = matching.filter(([, value]) => value !== false).map(([rawKey]) => rawKey);
|
|
150
|
-
if (offending.length === 0)
|
|
151
|
-
return { kind: "ok", setting: "false" };
|
|
152
|
-
const configPath = path.join(kimiHome, "config.toml");
|
|
153
|
-
return {
|
|
154
|
-
kind: "refuse",
|
|
155
|
-
refusalKind: "v2-plan-mode-configured",
|
|
156
|
-
reason: `\`${offending.join("`, `")}\` (upstream setting \`default_plan_mode\`) is set in ${configPath} and is not \`false\`.`,
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
export async function inspectEnvironmentConfig(kimiHome) {
|
|
160
|
-
const parsed = await readParsedConfig(kimiHome);
|
|
161
|
-
if (parsed.kind === "unreadable") {
|
|
162
|
-
return { kind: "refuse", refusalKind: "v2-config-unreadable", reason: parsed.reason };
|
|
163
|
-
}
|
|
164
|
-
const section = parsed.config?.["environments"];
|
|
165
|
-
if (section !== undefined) {
|
|
166
|
-
if (section === null || typeof section !== "object" || Array.isArray(section)) {
|
|
167
|
-
return {
|
|
168
|
-
kind: "refuse",
|
|
169
|
-
refusalKind: "v2-environment-configured",
|
|
170
|
-
reason: "The [environments] section is not a table, so the execution environment cannot be verified as local.",
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
if (section["default"] !== undefined) {
|
|
174
|
-
return {
|
|
175
|
-
kind: "refuse",
|
|
176
|
-
refusalKind: "v2-environment-configured",
|
|
177
|
-
reason: "A default remote execution environment is configured in [environments]; plugin-managed jobs require the local environment.",
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
const workspace = parsed.config?.["workspace"];
|
|
182
|
-
if (workspace !== undefined) {
|
|
183
|
-
if (workspace === null || typeof workspace !== "object" || Array.isArray(workspace)) {
|
|
184
|
-
return {
|
|
185
|
-
kind: "refuse",
|
|
186
|
-
refusalKind: "v2-workspace-trust-disabled",
|
|
187
|
-
reason: "The [workspace] section is not a table, so project trust requirements cannot be verified.",
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
const trustDisabled = Object.entries(workspace).some(([key, value]) => upstreamSnakeToCamel(key) === "requireTrust" && value !== true);
|
|
191
|
-
if (trustDisabled) {
|
|
192
|
-
return {
|
|
193
|
-
kind: "refuse",
|
|
194
|
-
refusalKind: "v2-workspace-trust-disabled",
|
|
195
|
-
reason: "Workspace trust is disabled in [workspace]; plugin-managed jobs require project trust enforcement to remain enabled.",
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return { kind: "ok" };
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Refuse v2-only experimental features this plugin does not certify.
|
|
203
|
-
* Sources (per-flag precedence upstream is env → [experimental] config →
|
|
204
|
-
* master flag → default): the `[experimental]` table keys `tower` and
|
|
205
|
-
* `subagent_fork` in `<kimiHome>/config.toml`, and the per-flag env vars
|
|
206
|
-
* `KIMI_CODE_EXPERIMENTAL_TOWER` / `KIMI_CODE_EXPERIMENTAL_SUBAGENT_FORK`.
|
|
207
|
-
* Truthy = boolean true, or a string in {"1","true","yes","on"} (trimmed,
|
|
208
|
-
* case-insensitive). The master `KIMI_CODE_EXPERIMENTAL_FLAG` is handled by
|
|
209
|
-
* kimi-engine.ts::assertNoUnsafeExperimentalSelector, not here.
|
|
210
|
-
* Config read errors follow inspectPlanModeConfig's fail-closed rules.
|
|
211
|
-
*/
|
|
212
|
-
export async function inspectExperimentalSelectors(kimiHome, env) {
|
|
213
|
-
const parsed = await readParsedConfig(kimiHome);
|
|
214
|
-
if (parsed.kind === "unreadable") {
|
|
215
|
-
return { kind: "refuse", selectors: [], reason: parsed.reason };
|
|
216
|
-
}
|
|
217
|
-
const selectors = [];
|
|
218
|
-
const experimental = parsed.config?.["experimental"];
|
|
219
|
-
if (experimental !== null && typeof experimental === "object" && !Array.isArray(experimental)) {
|
|
220
|
-
const table = experimental;
|
|
221
|
-
if (isTruthySelectorValue(table["tower"]))
|
|
222
|
-
selectors.push("config:[experimental].tower");
|
|
223
|
-
if (isTruthySelectorValue(table["subagent_fork"])) {
|
|
224
|
-
selectors.push("config:[experimental].subagent_fork");
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (isTruthySelectorValue(env.KIMI_CODE_EXPERIMENTAL_TOWER)) {
|
|
228
|
-
selectors.push("env:KIMI_CODE_EXPERIMENTAL_TOWER");
|
|
229
|
-
}
|
|
230
|
-
if (isTruthySelectorValue(env.KIMI_CODE_EXPERIMENTAL_SUBAGENT_FORK)) {
|
|
231
|
-
selectors.push("env:KIMI_CODE_EXPERIMENTAL_SUBAGENT_FORK");
|
|
232
|
-
}
|
|
233
|
-
if (selectors.length === 0)
|
|
234
|
-
return { kind: "ok" };
|
|
235
|
-
return {
|
|
236
|
-
kind: "refuse",
|
|
237
|
-
selectors,
|
|
238
|
-
reason: `Refusing v2-only experimental feature(s) not certified by kimi-plugin-cc: ${selectors.join(", ")}.`,
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
async function boundedReaddir(dirPath, cap) {
|
|
242
|
-
let entries;
|
|
243
|
-
try {
|
|
244
|
-
entries = await readdir(dirPath, { withFileTypes: true });
|
|
245
|
-
}
|
|
246
|
-
catch (error) {
|
|
247
|
-
const code = error.code;
|
|
248
|
-
if (code === "ENOENT" || code === "ENOTDIR")
|
|
249
|
-
return { kind: "absent" };
|
|
250
|
-
// Any other enumeration failure (EACCES, EIO, ELOOP, …) must land in the
|
|
251
|
-
// classified `unavailable` refusal, not escape as an untyped fs error.
|
|
252
|
-
return { kind: "unreadable", reason: `${dirPath}: ${error.message}` };
|
|
253
|
-
}
|
|
254
|
-
if (entries.length > cap)
|
|
255
|
-
return { kind: "cap-exceeded" };
|
|
256
|
-
return { kind: "ok", entries };
|
|
257
|
-
}
|
|
258
|
-
async function openJournalSafely(journalPath, maxBytes) {
|
|
259
|
-
let handle;
|
|
260
|
-
try {
|
|
261
|
-
// O_NONBLOCK so a FIFO planted at the journal path (no writer) does not
|
|
262
|
-
// block the open() indefinitely — the regular-file check below then
|
|
263
|
-
// refuses it. O_NOFOLLOW refuses a symlinked final component.
|
|
264
|
-
handle = await open(journalPath, fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0) | (fsConstants.O_NONBLOCK ?? 0));
|
|
265
|
-
}
|
|
266
|
-
catch (error) {
|
|
267
|
-
const code = error.code;
|
|
268
|
-
if (code === "ENOENT")
|
|
269
|
-
return { kind: "unavailable", reason: "journal not found" };
|
|
270
|
-
if (code === "ELOOP")
|
|
271
|
-
return { kind: "unavailable", reason: "journal path is a symbolic link" };
|
|
272
|
-
return { kind: "unavailable", reason: error.message };
|
|
273
|
-
}
|
|
274
|
-
const stats = await handle.stat({ bigint: true });
|
|
275
|
-
if (!stats.isFile()) {
|
|
276
|
-
await handle.close().catch(() => undefined);
|
|
277
|
-
return { kind: "unavailable", reason: "journal is not a regular file" };
|
|
278
|
-
}
|
|
279
|
-
if (stats.size > BigInt(maxBytes)) {
|
|
280
|
-
await handle.close().catch(() => undefined);
|
|
281
|
-
return { kind: "unavailable", reason: `journal exceeds ${maxBytes} byte cap` };
|
|
282
|
-
}
|
|
283
|
-
return { kind: "ok", handle };
|
|
284
|
-
}
|
|
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);
|
|
295
|
-
}
|
|
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
|
-
}
|
|
303
|
-
continue;
|
|
304
|
-
}
|
|
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 };
|
|
309
|
-
}
|
|
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
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
return null;
|
|
321
|
-
}
|
|
322
|
-
finally {
|
|
323
|
-
rl.close();
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
async function inspectSessionMetadataEnvironment(statePath, expectedExecutionRoot) {
|
|
327
|
-
const read = await readSmallFileSafe(statePath, CONFIG_MAX_BYTES);
|
|
328
|
-
if (read.kind === "absent")
|
|
329
|
-
return null;
|
|
330
|
-
if (read.kind === "unreadable") {
|
|
331
|
-
return { kind: "unavailable", reason: `${statePath}: ${read.reason}` };
|
|
332
|
-
}
|
|
333
|
-
let metadata;
|
|
334
|
-
try {
|
|
335
|
-
metadata = JSON.parse(read.contents);
|
|
336
|
-
}
|
|
337
|
-
catch {
|
|
338
|
-
return { kind: "unavailable", reason: `${statePath}: malformed session metadata` };
|
|
339
|
-
}
|
|
340
|
-
if (metadata === null || typeof metadata !== "object" || Array.isArray(metadata)) {
|
|
341
|
-
return { kind: "unavailable", reason: `${statePath}: invalid session metadata` };
|
|
342
|
-
}
|
|
343
|
-
const record = metadata;
|
|
344
|
-
const environmentId = record["environmentId"];
|
|
345
|
-
if (environmentId !== undefined && environmentId !== "local") {
|
|
346
|
-
return { kind: "environment", journal: statePath, recordType: "session.metadata.environmentId" };
|
|
347
|
-
}
|
|
348
|
-
const environmentCwd = record["environmentCwd"];
|
|
349
|
-
if ((environmentId === undefined || environmentId === "local") &&
|
|
350
|
-
expectedExecutionRoot !== undefined &&
|
|
351
|
-
environmentCwd !== undefined &&
|
|
352
|
-
environmentCwd !== expectedExecutionRoot) {
|
|
353
|
-
return { kind: "environment", journal: statePath, recordType: "session.metadata.environmentCwd" };
|
|
354
|
-
}
|
|
355
|
-
return null;
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* Scan saved session metadata and every agent wire journal for plan-mode records
|
|
359
|
-
* and nonlocal environment bindings. Layout at 0.42.0:
|
|
360
|
-
* `<kimiHome>/sessions/<workspaceId>/<sessionId>/agents/<agentId>/wire.jsonl`
|
|
361
|
-
* (workspaceId is unknown to the plugin → enumerate `sessions/*` with bounded
|
|
362
|
-
* readdir). Raw-line scan of EVERY agent journal: a JSON line whose `type`
|
|
363
|
-
* starts with `plan_mode.` or `plan.` → tainted; `environment.set_binding` must
|
|
364
|
-
* name `environmentId:"local"`. No session dir, no journal, unreadable metadata,
|
|
365
|
-
* or any single journal above the size cap → unavailable. Never modifies files.
|
|
366
|
-
*/
|
|
367
|
-
export async function scanSessionJournalsForPlan(kimiHome, sessionId, expectedExecutionRoot) {
|
|
368
|
-
const sessionsDir = path.join(kimiHome, "sessions");
|
|
369
|
-
const workspaceEntries = await boundedReaddir(sessionsDir, DIR_ENUM_CAP);
|
|
370
|
-
if (workspaceEntries.kind === "absent") {
|
|
371
|
-
return { kind: "unavailable", reason: `no sessions directory at ${sessionsDir}` };
|
|
372
|
-
}
|
|
373
|
-
if (workspaceEntries.kind === "cap-exceeded") {
|
|
374
|
-
return { kind: "unavailable", reason: `too many entries under ${sessionsDir} to scan safely` };
|
|
375
|
-
}
|
|
376
|
-
if (workspaceEntries.kind === "unreadable") {
|
|
377
|
-
return { kind: "unavailable", reason: `could not enumerate the session tree: ${workspaceEntries.reason}` };
|
|
378
|
-
}
|
|
379
|
-
const journalPaths = [];
|
|
380
|
-
for (const workspaceEntry of workspaceEntries.entries) {
|
|
381
|
-
// A symlinked entry in the session tree is refused, not skipped: a
|
|
382
|
-
// symlinked agent (or workspace) directory has isDirectory() === false, so
|
|
383
|
-
// skipping it would drop its journal from the scan and let an active-plan
|
|
384
|
-
// journal hide behind the symlink while a sibling reads clean. Fail closed.
|
|
385
|
-
if (workspaceEntry.isSymbolicLink()) {
|
|
386
|
-
return {
|
|
387
|
-
kind: "unavailable",
|
|
388
|
-
reason: `symlinked entry in the session tree: ${path.join(sessionsDir, workspaceEntry.name)}`,
|
|
389
|
-
};
|
|
390
|
-
}
|
|
391
|
-
if (!workspaceEntry.isDirectory())
|
|
392
|
-
continue;
|
|
393
|
-
const sessionDir = path.join(sessionsDir, workspaceEntry.name, sessionId);
|
|
394
|
-
const metadataFinding = await inspectSessionMetadataEnvironment(path.join(sessionDir, "state.json"), expectedExecutionRoot);
|
|
395
|
-
if (metadataFinding !== null)
|
|
396
|
-
return metadataFinding;
|
|
397
|
-
const agentsDir = path.join(sessionDir, "agents");
|
|
398
|
-
const agentEntries = await boundedReaddir(agentsDir, DIR_ENUM_CAP);
|
|
399
|
-
if (agentEntries.kind === "absent")
|
|
400
|
-
continue;
|
|
401
|
-
if (agentEntries.kind === "cap-exceeded") {
|
|
402
|
-
return { kind: "unavailable", reason: `too many agent entries under ${agentsDir} to scan safely` };
|
|
403
|
-
}
|
|
404
|
-
if (agentEntries.kind === "unreadable") {
|
|
405
|
-
return { kind: "unavailable", reason: `could not enumerate agent journals: ${agentEntries.reason}` };
|
|
406
|
-
}
|
|
407
|
-
for (const agentEntry of agentEntries.entries) {
|
|
408
|
-
if (agentEntry.isSymbolicLink()) {
|
|
409
|
-
return {
|
|
410
|
-
kind: "unavailable",
|
|
411
|
-
reason: `symlinked agent entry in the session tree: ${path.join(agentsDir, agentEntry.name)}`,
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
if (!agentEntry.isDirectory())
|
|
415
|
-
continue;
|
|
416
|
-
journalPaths.push(path.join(agentsDir, agentEntry.name, "wire.jsonl"));
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
if (journalPaths.length === 0) {
|
|
420
|
-
return { kind: "unavailable", reason: `no agent wire journals found for session ${sessionId}` };
|
|
421
|
-
}
|
|
422
|
-
for (const journalPath of journalPaths) {
|
|
423
|
-
const opened = await openJournalSafely(journalPath, JOURNAL_MAX_BYTES);
|
|
424
|
-
if (opened.kind === "unavailable") {
|
|
425
|
-
return { kind: "unavailable", reason: `${journalPath}: ${opened.reason}` };
|
|
426
|
-
}
|
|
427
|
-
try {
|
|
428
|
-
const finding = await scanJournalLines(opened.handle, expectedExecutionRoot);
|
|
429
|
-
if (finding?.kind === "tainted") {
|
|
430
|
-
return { kind: "tainted", journal: journalPath, recordType: finding.recordType };
|
|
431
|
-
}
|
|
432
|
-
if (finding?.kind === "environment") {
|
|
433
|
-
return { kind: "environment", journal: journalPath, recordType: finding.recordType };
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
finally {
|
|
437
|
-
await opened.handle.close().catch(() => undefined);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
return { kind: "clean", journals: journalPaths };
|
|
441
|
-
}
|
|
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
|
-
export async function assertNativeV2Preflight(options) {
|
|
456
|
-
const stage = options.stage ?? "native-v2.preflight";
|
|
457
|
-
const configPath = path.join(options.kimiHome, "config.toml");
|
|
458
|
-
const planMode = await inspectPlanModeConfig(options.kimiHome);
|
|
459
|
-
if (planMode.kind === "refuse") {
|
|
460
|
-
const code = planMode.refusalKind === "v2-plan-mode-configured"
|
|
461
|
-
? "CLI_V2_PLAN_MODE_CONFIGURED"
|
|
462
|
-
: "CLI_V2_CONFIG_UNREADABLE";
|
|
463
|
-
const remedy = planMode.refusalKind === "v2-plan-mode-configured"
|
|
464
|
-
? `Set \`default_plan_mode = false\` (or remove it) in ${configPath}.`
|
|
465
|
-
: `Fix or remove the unreadable file at ${configPath} so plan mode can be verified off.`;
|
|
466
|
-
throw new RuntimeError(code, `${planMode.reason} ${remedy}`, stage, {
|
|
467
|
-
details: { refusal_kind: planMode.refusalKind, retryable_after_setup: false },
|
|
468
|
-
});
|
|
469
|
-
}
|
|
470
|
-
const environment = await inspectEnvironmentConfig(options.kimiHome);
|
|
471
|
-
if (environment.kind === "refuse") {
|
|
472
|
-
const code = environment.refusalKind === "v2-environment-configured"
|
|
473
|
-
? "CLI_V2_ENVIRONMENT_UNSAFE"
|
|
474
|
-
: environment.refusalKind === "v2-workspace-trust-disabled"
|
|
475
|
-
? "CLI_V2_WORKSPACE_TRUST_UNSAFE"
|
|
476
|
-
: "CLI_V2_CONFIG_UNREADABLE";
|
|
477
|
-
const remedy = environment.refusalKind === "v2-environment-configured"
|
|
478
|
-
? `Remove \`environments.default\` in ${configPath} so plugin-managed sessions stay local.`
|
|
479
|
-
: environment.refusalKind === "v2-workspace-trust-disabled"
|
|
480
|
-
? `Set \`workspace.require_trust = true\` (or remove it) in ${configPath}.`
|
|
481
|
-
: `Fix or remove the unreadable file at ${configPath} so the execution environment can be verified as local.`;
|
|
482
|
-
throw new RuntimeError(code, `${environment.reason} ${remedy}`, stage, {
|
|
483
|
-
details: { refusal_kind: environment.refusalKind, retryable_after_setup: false },
|
|
484
|
-
});
|
|
485
|
-
}
|
|
486
|
-
const experimental = await inspectExperimentalSelectors(options.kimiHome, options.env);
|
|
487
|
-
if (experimental.kind === "refuse") {
|
|
488
|
-
throw new RuntimeError("CLI_V2_EXPERIMENTAL_UNSAFE", `${experimental.reason} Unset the listed selector(s) before retrying.`, stage, {
|
|
489
|
-
details: {
|
|
490
|
-
refusal_kind: "v2-experimental-unsafe",
|
|
491
|
-
selectors: experimental.selectors,
|
|
492
|
-
retryable_after_setup: false,
|
|
493
|
-
},
|
|
494
|
-
});
|
|
495
|
-
}
|
|
496
|
-
if (options.resumeSessionId !== undefined) {
|
|
497
|
-
const journalScan = await scanSessionJournalsForPlan(options.kimiHome, options.resumeSessionId, options.expectedExecutionRoot);
|
|
498
|
-
if (journalScan.kind === "environment") {
|
|
499
|
-
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
|
-
details: {
|
|
501
|
-
refusal_kind: "session-environment-unsafe",
|
|
502
|
-
record_type: journalScan.recordType,
|
|
503
|
-
retryable_after_setup: false,
|
|
504
|
-
},
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
|
-
if (journalScan.kind === "tainted") {
|
|
508
|
-
throw new RuntimeError("KIMI_SESSION_PLAN_TAINTED", `Saved session ${options.resumeSessionId} previously entered plan mode (record type "${journalScan.recordType}" in ${journalScan.journal}). Start a fresh session; saved session state was not modified.`, stage, {
|
|
509
|
-
details: {
|
|
510
|
-
refusal_kind: "session-plan-tainted",
|
|
511
|
-
record_type: journalScan.recordType,
|
|
512
|
-
retryable_after_setup: false,
|
|
513
|
-
},
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
if (journalScan.kind === "unavailable") {
|
|
517
|
-
throw new RuntimeError("KIMI_SESSION_JOURNAL_UNAVAILABLE", `Could not verify saved session ${options.resumeSessionId} is free of plan-mode state (${journalScan.reason}). Start a fresh session; saved session state was not modified.`, stage, {
|
|
518
|
-
details: {
|
|
519
|
-
refusal_kind: "session-journal-unavailable",
|
|
520
|
-
retryable_after_setup: false,
|
|
521
|
-
},
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|