kimi-plugin-cc 2.7.3 → 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.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +3 -5
- package/CHANGELOG.md +69 -0
- package/README.md +505 -448
- package/agents/kimi-pursue.md +6 -6
- package/agents/kimi-review.md +22 -22
- package/agents/kimi-swarm.md +25 -23
- package/agents/{kimi-ask.md → kimi-task.md} +34 -30
- package/commands/README.md +4 -6
- package/commands/pursue.md +1 -1
- package/commands/review.md +8 -3
- package/commands/setup.md +4 -2
- package/commands/swarm.md +3 -2
- package/commands/{rescue.md → task.md} +13 -11
- package/dist/background-spawn.js +10 -17
- package/dist/cli-client.js +65 -46
- package/dist/commands/ask.js +22 -89
- package/dist/commands/cancel.js +7 -46
- package/dist/commands/cli-helpers.js +102 -1
- package/dist/commands/models.js +2 -3
- package/dist/commands/pursue.js +5 -19
- package/dist/commands/repair-sessions.js +118 -12
- package/dist/commands/rescue.js +12 -82
- package/dist/commands/result.js +7 -1
- package/dist/commands/review-gate.js +1 -0
- package/dist/commands/review.js +12 -15
- package/dist/commands/setup.js +606 -270
- package/dist/commands/status.js +2 -2
- package/dist/commands/swarm.js +93 -119
- package/dist/commands/worker.js +1 -2
- package/dist/companion.js +3 -1
- package/dist/dsh/index.js +15 -17
- package/dist/git.js +1 -9
- package/dist/help.js +3 -3
- package/dist/hooks/approval-hook.js +41 -0
- package/dist/hooks/approval-policy.js +72 -10
- package/dist/hooks/config-safety.js +19 -43
- package/dist/hooks/hook-timing.js +48 -0
- package/dist/hooks/install.js +206 -11
- package/dist/hooks/managed-block.js +78 -33
- package/dist/job-store.js +100 -128
- package/dist/jobs.js +14 -19
- package/dist/kimi-command.js +74 -1
- package/dist/kimi-engine.js +170 -75
- package/dist/kimi-errors.js +14 -69
- package/dist/kimi-home.js +6 -0
- package/dist/kimi-journal.js +41 -0
- package/dist/kimi-releases.js +144 -0
- package/dist/kimi-timeouts.js +1 -1
- package/dist/kimi-version-probe.js +12 -48
- package/dist/mcp/job-control.js +4 -6
- package/dist/mcp/launcher.js +12 -6
- package/dist/mcp/operations.js +50 -18
- package/dist/mcp/protocol.js +16 -14
- package/dist/mcp/stdio.js +3 -2
- package/dist/model-command.js +5 -3
- package/dist/native-v2-preflight.js +139 -68
- package/dist/parsing.js +171 -237
- package/dist/paths.js +8 -0
- package/dist/process-tree.js +35 -24
- package/dist/read-scope.js +435 -0
- package/dist/render.js +46 -9
- package/dist/rescue-approval.js +461 -84
- package/dist/schemas/review-gate-output.js +3 -5
- package/dist/session-title.js +25 -5
- package/dist/stream-json.js +1 -3
- package/dist/surface-routing.js +21 -0
- package/dist/swarm-activation.js +72 -75
- 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 +24 -3
- package/docs/models.md +2 -2
- package/docs/registry-releases.md +1 -1
- package/docs/safety.md +120 -70
- package/docs/session-visibility.md +5 -3
- package/docs/swarm-recovery.md +1 -1
- package/docs/upstream-2.7.3-certification.md +14 -2
- package/package.json +1 -1
- package/plugins/kimi-codex/.codex-plugin/plugin.json +3 -3
- package/plugins/kimi-codex/.mcp.json +1 -0
- package/plugins/kimi-codex/dist/background-spawn.js +10 -17
- package/plugins/kimi-codex/dist/cli-client.js +65 -46
- package/plugins/kimi-codex/dist/commands/ask.js +22 -89
- package/plugins/kimi-codex/dist/commands/cancel.js +7 -46
- 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 +5 -19
- package/plugins/kimi-codex/dist/commands/repair-sessions.js +118 -12
- package/plugins/kimi-codex/dist/commands/rescue.js +12 -82
- package/plugins/kimi-codex/dist/commands/result.js +7 -1
- package/plugins/kimi-codex/dist/commands/review-gate.js +1 -0
- package/plugins/kimi-codex/dist/commands/review.js +12 -15
- package/plugins/kimi-codex/dist/commands/setup.js +606 -270
- package/plugins/kimi-codex/dist/commands/status.js +2 -2
- package/plugins/kimi-codex/dist/commands/swarm.js +93 -119
- package/plugins/kimi-codex/dist/commands/worker.js +1 -2
- package/plugins/kimi-codex/dist/companion.js +3 -1
- package/plugins/kimi-codex/dist/dsh/index.js +15 -17
- package/plugins/kimi-codex/dist/git.js +1 -9
- package/plugins/kimi-codex/dist/help.js +3 -3
- package/plugins/kimi-codex/dist/hooks/approval-hook.js +41 -0
- package/plugins/kimi-codex/dist/hooks/approval-policy.js +72 -10
- package/plugins/kimi-codex/dist/hooks/config-safety.js +19 -43
- package/plugins/kimi-codex/dist/hooks/hook-timing.js +48 -0
- package/plugins/kimi-codex/dist/hooks/install.js +206 -11
- package/plugins/kimi-codex/dist/hooks/managed-block.js +78 -33
- package/plugins/kimi-codex/dist/job-store.js +100 -128
- package/plugins/kimi-codex/dist/jobs.js +14 -19
- package/plugins/kimi-codex/dist/kimi-command.js +74 -1
- package/plugins/kimi-codex/dist/kimi-engine.js +170 -75
- package/plugins/kimi-codex/dist/kimi-errors.js +14 -69
- package/plugins/kimi-codex/dist/kimi-home.js +6 -0
- package/plugins/kimi-codex/dist/kimi-journal.js +41 -0
- package/plugins/kimi-codex/dist/kimi-releases.js +144 -0
- package/plugins/kimi-codex/dist/kimi-timeouts.js +1 -1
- package/plugins/kimi-codex/dist/kimi-version-probe.js +12 -48
- package/plugins/kimi-codex/dist/mcp/job-control.js +4 -6
- package/plugins/kimi-codex/dist/mcp/launcher.js +12 -6
- package/plugins/kimi-codex/dist/mcp/operations.js +50 -18
- package/plugins/kimi-codex/dist/mcp/protocol.js +16 -14
- package/plugins/kimi-codex/dist/mcp/stdio.js +3 -2
- package/plugins/kimi-codex/dist/model-command.js +5 -3
- package/plugins/kimi-codex/dist/native-v2-preflight.js +139 -68
- package/plugins/kimi-codex/dist/parsing.js +171 -237
- package/plugins/kimi-codex/dist/paths.js +8 -0
- 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 +46 -9
- package/plugins/kimi-codex/dist/rescue-approval.js +461 -84
- package/plugins/kimi-codex/dist/schemas/review-gate-output.js +3 -5
- package/plugins/kimi-codex/dist/session-title.js +25 -5
- package/plugins/kimi-codex/dist/stream-json.js +1 -3
- package/plugins/kimi-codex/dist/surface-routing.js +21 -0
- package/plugins/kimi-codex/dist/swarm-activation.js +72 -75
- 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/skills/kimi-pursue/SKILL.md +1 -1
- package/plugins/kimi-codex/skills/kimi-review/SKILL.md +8 -7
- package/plugins/kimi-codex/skills/kimi-review/agents/openai.yaml +1 -1
- package/plugins/kimi-codex/skills/kimi-setup/SKILL.md +2 -1
- package/plugins/kimi-codex/skills/kimi-swarm/SKILL.md +8 -6
- package/plugins/kimi-codex/skills/kimi-swarm/agents/openai.yaml +1 -1
- package/plugins/kimi-codex/skills/{kimi-ask → kimi-task}/SKILL.md +11 -9
- package/plugins/kimi-codex/skills/kimi-task/agents/openai.yaml +6 -0
- package/plugins/kimi-dsh/README.md +3 -2
- package/plugins/kimi-dsh/dist/background-spawn.js +10 -17
- package/plugins/kimi-dsh/dist/cli-client.js +65 -46
- package/plugins/kimi-dsh/dist/commands/ask.js +22 -89
- package/plugins/kimi-dsh/dist/commands/cancel.js +7 -46
- package/plugins/kimi-dsh/dist/commands/cli-helpers.js +102 -1
- package/plugins/kimi-dsh/dist/commands/models.js +2 -3
- package/plugins/kimi-dsh/dist/commands/pursue.js +5 -19
- package/plugins/kimi-dsh/dist/commands/repair-sessions.js +118 -12
- package/plugins/kimi-dsh/dist/commands/rescue.js +12 -82
- package/plugins/kimi-dsh/dist/commands/result.js +7 -1
- package/plugins/kimi-dsh/dist/commands/review-gate.js +1 -0
- package/plugins/kimi-dsh/dist/commands/review.js +12 -15
- package/plugins/kimi-dsh/dist/commands/setup.js +606 -270
- package/plugins/kimi-dsh/dist/commands/status.js +2 -2
- package/plugins/kimi-dsh/dist/commands/swarm.js +93 -119
- package/plugins/kimi-dsh/dist/commands/worker.js +1 -2
- package/plugins/kimi-dsh/dist/companion.js +3 -1
- package/plugins/kimi-dsh/dist/dsh/index.js +15 -17
- package/plugins/kimi-dsh/dist/git.js +1 -9
- package/plugins/kimi-dsh/dist/help.js +3 -3
- package/plugins/kimi-dsh/dist/hooks/approval-hook.js +41 -0
- package/plugins/kimi-dsh/dist/hooks/approval-policy.js +72 -10
- package/plugins/kimi-dsh/dist/hooks/config-safety.js +19 -43
- package/plugins/kimi-dsh/dist/hooks/hook-timing.js +48 -0
- package/plugins/kimi-dsh/dist/hooks/install.js +206 -11
- package/plugins/kimi-dsh/dist/hooks/managed-block.js +78 -33
- package/plugins/kimi-dsh/dist/job-store.js +100 -128
- package/plugins/kimi-dsh/dist/jobs.js +14 -19
- package/plugins/kimi-dsh/dist/kimi-command.js +74 -1
- package/plugins/kimi-dsh/dist/kimi-engine.js +170 -75
- package/plugins/kimi-dsh/dist/kimi-errors.js +14 -69
- package/plugins/kimi-dsh/dist/kimi-home.js +6 -0
- package/plugins/kimi-dsh/dist/kimi-journal.js +41 -0
- package/plugins/kimi-dsh/dist/kimi-releases.js +144 -0
- package/plugins/kimi-dsh/dist/kimi-timeouts.js +1 -1
- package/plugins/kimi-dsh/dist/kimi-version-probe.js +12 -48
- package/plugins/kimi-dsh/dist/mcp/job-control.js +4 -6
- package/plugins/kimi-dsh/dist/mcp/launcher.js +12 -6
- package/plugins/kimi-dsh/dist/mcp/operations.js +50 -18
- package/plugins/kimi-dsh/dist/mcp/protocol.js +16 -14
- package/plugins/kimi-dsh/dist/mcp/stdio.js +3 -2
- package/plugins/kimi-dsh/dist/model-command.js +5 -3
- package/plugins/kimi-dsh/dist/native-v2-preflight.js +139 -68
- package/plugins/kimi-dsh/dist/parsing.js +171 -237
- package/plugins/kimi-dsh/dist/paths.js +8 -0
- package/plugins/kimi-dsh/dist/process-tree.js +35 -24
- package/plugins/kimi-dsh/dist/read-scope.js +435 -0
- package/plugins/kimi-dsh/dist/render.js +46 -9
- package/plugins/kimi-dsh/dist/rescue-approval.js +461 -84
- package/plugins/kimi-dsh/dist/schemas/review-gate-output.js +3 -5
- package/plugins/kimi-dsh/dist/session-title.js +25 -5
- package/plugins/kimi-dsh/dist/stream-json.js +1 -3
- package/plugins/kimi-dsh/dist/surface-routing.js +21 -0
- package/plugins/kimi-dsh/dist/swarm-activation.js +72 -75
- package/plugins/kimi-dsh/dist/swarm-policy.js +2 -1
- package/plugins/kimi-dsh/dist/swarm-recovery.js +2 -1
- package/plugins/kimi-dsh/dist/util.js +28 -0
- package/plugins/kimi-dsh/dist/version.js +1 -1
- package/plugins/kimi-dsh/package.json +1 -1
- package/agents/kimi-challenge.md +0 -86
- package/agents/kimi-rescue.md +0 -92
- package/agents/kimi-swarm-write.md +0 -96
- package/commands/ask.md +0 -38
- package/commands/challenge.md +0 -35
- package/dist/commands/not-implemented.js +0 -4
- package/plugins/kimi-codex/dist/commands/not-implemented.js +0 -4
- package/plugins/kimi-codex/skills/kimi-ask/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-rescue/SKILL.md +0 -34
- package/plugins/kimi-codex/skills/kimi-rescue/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/dist/commands/not-implemented.js +0 -4
package/dist/commands/setup.js
CHANGED
|
@@ -7,34 +7,40 @@ import { SETUP_SUPPORTED_FLAGS } from "../parsing.js";
|
|
|
7
7
|
// canonical `'<absolute-node>' '<absolute-hook-script>'` command from
|
|
8
8
|
// resolveNodeBinary/buildHookShellCommand. Both tokens are shell-quoted and the
|
|
9
9
|
// absolute Node path avoids PATH dependence under sanitized launches. Setup
|
|
10
|
-
// probes the hook directly with
|
|
11
|
-
// using the exact installed command bytes. It also warns about broad permission
|
|
10
|
+
// probes the hook directly with the Node binary parsed back from that command
|
|
11
|
+
// and through `/bin/sh -c` using the exact installed command bytes. It also warns about broad permission
|
|
12
12
|
// denies, repairs orphan marker comments, and refuses remaining
|
|
13
13
|
// duplicate, orphaned, or malformed managed blocks.
|
|
14
14
|
//
|
|
15
|
-
// Subcommands: install (default), --check, --uninstall [--all],
|
|
16
|
-
// --enable-review-gate/--disable-review-gate
|
|
15
|
+
// Subcommands: install (default), --check, --uninstall [--all],
|
|
16
|
+
// --enable-review-gate/--disable-review-gate, and the explicit, network-using
|
|
17
|
+
// --install-kimi [--json], which installs the highest certified kimi-code into
|
|
18
|
+
// plugin data. Nothing else in setup or any job installs or downloads.
|
|
17
19
|
//
|
|
18
20
|
// Managed-block splicing is line-based and preserves user bytes outside owned
|
|
19
21
|
// edits; whole-file validation and inline-hook normalization use the TOML parser.
|
|
20
22
|
import { spawn } from "node:child_process";
|
|
21
|
-
import { access, mkdir, open, readFile, rename, unlink, } from "node:fs/promises";
|
|
23
|
+
import { access, mkdir, lstat, mkdtemp, open, readdir, readFile, readlink, realpath, rename, rm, symlink, unlink, writeFile, } from "node:fs/promises";
|
|
22
24
|
import { constants as fsConstants, existsSync } from "node:fs";
|
|
23
25
|
import { randomBytes } from "node:crypto";
|
|
24
26
|
import path from "node:path";
|
|
25
27
|
import { readPluginConfig, writePluginConfig } from "../config.js";
|
|
26
28
|
import { RuntimeError } from "../errors.js";
|
|
29
|
+
import { HOOK_TIMEOUT_S } from "../hooks/hook-timing.js";
|
|
30
|
+
import { olderHostHookWarnings } from "../hooks/install.js";
|
|
27
31
|
import { normalizeTopLevelInlineHooks, validateKimiHookSetForEnvironment, withKimiConfigLock, } from "../hooks/config-safety.js";
|
|
28
|
-
import { buildHookShellCommand, hostIdFromHookCommand, resolveHookScriptPath, resolveHostId, resolveNodeBinary, } from "../hooks/install-paths.js";
|
|
29
|
-
import { decodeManagedCommandLine, effectiveHost, evaluateInstalled,
|
|
32
|
+
import { buildHookShellCommand, hostIdFromHookCommand, parseHookShellCommand, resolveHookScriptPath, resolveHostId, resolveNodeBinary, } from "../hooks/install-paths.js";
|
|
33
|
+
import { decodeManagedCommandLine, describeHookTimeout, effectiveHost, evaluateInstalled, findShortHookTimeout, findUnmanagedApprovalHookBlocks, ORPHAN_MARKER_REMEDY, pairBlocks, parseManagedBlock, } from "../hooks/managed-block.js";
|
|
30
34
|
import { formatVersionOutOfRangeWarning, probeKimiVersion, } from "../kimi-version-probe.js";
|
|
31
|
-
import {
|
|
32
|
-
import {
|
|
33
|
-
import {
|
|
35
|
+
import { inspectPluginOwnedKimi, selectKimiBinary } from "../kimi-command.js";
|
|
36
|
+
import { isNativeV2CertifiedVersion, NATIVE_V2_ANY_OPERATION_VERSIONS, NATIVE_V2_CERTIFIED_VERSIONS, NATIVE_V2_NON_SWARM_VERSIONS, } from "../kimi-engine.js";
|
|
37
|
+
import { resolveKimiCodeConfigPath, resolveKimiHome } from "../kimi-home.js";
|
|
38
|
+
import { formatVersionChoice, kimiReleaseHas, kimiVersionsWith } from "../kimi-releases.js";
|
|
39
|
+
import { ensurePluginPaths, resolveKimiBinDir, resolvePluginPaths } from "../paths.js";
|
|
34
40
|
import { KIMI_PLUGIN_CC_VERSION } from "../version.js";
|
|
35
41
|
import { parse as parseToml } from "../vendor/smol-toml/parse.js";
|
|
36
42
|
const BEGIN_MARKER_PREFIX = "# === BEGIN kimi-plugin-cc-managed";
|
|
37
|
-
const DEFAULT_HOOK_TIMEOUT_S =
|
|
43
|
+
const DEFAULT_HOOK_TIMEOUT_S = HOOK_TIMEOUT_S;
|
|
38
44
|
// Probe timing. The direct script probe is local and fast; allow 5s for
|
|
39
45
|
// node startup on cold CI runners. The shell probe adds /bin/sh fork
|
|
40
46
|
// overhead but should still complete well within the same budget.
|
|
@@ -46,6 +52,9 @@ const PROBE_TIMEOUT_MS = 5_000;
|
|
|
46
52
|
const PATH_FORBIDDEN_RE = /[\x00-\x1f"\\\n\r\t]/;
|
|
47
53
|
export async function runSetup(argv, context) {
|
|
48
54
|
const parsed = parseArgs(argv);
|
|
55
|
+
if (parsed.mode === "install-kimi") {
|
|
56
|
+
return await runInstallKimi(parsed.json, context);
|
|
57
|
+
}
|
|
49
58
|
const paths = resolvePluginPaths(context.env);
|
|
50
59
|
await ensurePluginPaths(paths);
|
|
51
60
|
// Reconcile the review-gate flag first — it's independent of the
|
|
@@ -81,8 +90,16 @@ function parseArgs(argv) {
|
|
|
81
90
|
let enableReviewGate = false;
|
|
82
91
|
let disableReviewGate = false;
|
|
83
92
|
let removeAllHosts = false;
|
|
93
|
+
let installKimi = false;
|
|
94
|
+
let json = false;
|
|
84
95
|
for (const token of argv) {
|
|
85
96
|
switch (token) {
|
|
97
|
+
case "--install-kimi":
|
|
98
|
+
installKimi = true;
|
|
99
|
+
break;
|
|
100
|
+
case "--json":
|
|
101
|
+
json = true;
|
|
102
|
+
break;
|
|
86
103
|
case "--check":
|
|
87
104
|
if (mode === "uninstall") {
|
|
88
105
|
throw new RuntimeError("INVALID_ARGS", "setup accepts at most one of --check, --uninstall.", "setup.parse");
|
|
@@ -114,7 +131,16 @@ function parseArgs(argv) {
|
|
|
114
131
|
if (removeAllHosts && mode !== "uninstall") {
|
|
115
132
|
throw new RuntimeError("INVALID_ARGS", "--all is only valid with --uninstall (it removes EVERY host's managed block).", "setup.parse");
|
|
116
133
|
}
|
|
117
|
-
|
|
134
|
+
if (installKimi) {
|
|
135
|
+
if (argv.some((token) => token !== "--install-kimi" && token !== "--json")) {
|
|
136
|
+
throw new RuntimeError("INVALID_ARGS", "--install-kimi does not combine with other setup flags except --json.", "setup.parse");
|
|
137
|
+
}
|
|
138
|
+
return { mode: "install-kimi", json, enableReviewGate, disableReviewGate, removeAllHosts };
|
|
139
|
+
}
|
|
140
|
+
if (json) {
|
|
141
|
+
throw new RuntimeError("INVALID_ARGS", "--json is only valid with --install-kimi or --models.", "setup.parse");
|
|
142
|
+
}
|
|
143
|
+
return { mode, json, enableReviewGate, disableReviewGate, removeAllHosts };
|
|
118
144
|
}
|
|
119
145
|
async function runInstall(configPath, hookScriptPath, hostId, reviewGateEnabled, warnings, context) {
|
|
120
146
|
await assertHookScriptExists(hookScriptPath);
|
|
@@ -145,12 +171,15 @@ async function runInstall(configPath, hookScriptPath, hostId, reviewGateEnabled,
|
|
|
145
171
|
// splice line numbers. Another host's marker-less table may be its live hook
|
|
146
172
|
// after a config rewrite strips comments, so it must remain untouched.
|
|
147
173
|
const expectedCommand = buildHookShellCommand(hookScriptPath, context.env);
|
|
174
|
+
const shortBefore = findShortHookTimeout(inlineHooks.contents, expectedCommand);
|
|
148
175
|
const { pruned, count: prunedCount, commands: prunedCommands } = pruneOrphanApprovalHooks(inlineHooks.contents, hostId, expectedCommand);
|
|
149
176
|
const existing = pruned;
|
|
150
177
|
if (prunedCount > 0) {
|
|
151
178
|
const readorned = prunedCommands.includes(expectedCommand);
|
|
152
179
|
warnings.push(readorned
|
|
153
|
-
?
|
|
180
|
+
? shortBefore === null
|
|
181
|
+
? `Re-adorned this host's managed-block markers: kimi-code rewrites its config on login/settings changes and strips all comments (markers included), leaving the hook enforcing but unmarked. Replaced the bare hook table with a freshly marked block (${prunedCount} table(s) refreshed). Enforcement was never interrupted.`
|
|
182
|
+
: `Re-adorned this host's managed-block markers: replaced the bare hook table with a freshly marked block (${prunedCount} table(s) refreshed).`
|
|
154
183
|
: `Pruned ${prunedCount} stale marker-less kimi-plugin-cc hook block(s) left by this host's earlier installs. This is a cleanup — your active hook is unaffected.`);
|
|
155
184
|
}
|
|
156
185
|
const { state, blocks } = parseManagedBlock(existing, hostId);
|
|
@@ -177,21 +206,32 @@ async function runInstall(configPath, hookScriptPath, hostId, reviewGateEnabled,
|
|
|
177
206
|
warnings.push(`Other host(s) also manage a block in this shared config: ${otherHosts.join(", ")}. ` +
|
|
178
207
|
`They are left untouched — each host owns its own PreToolUse block.`);
|
|
179
208
|
}
|
|
180
|
-
const block = buildManagedBlock(
|
|
209
|
+
const block = buildManagedBlock(expectedCommand, lineEnding, hostId);
|
|
181
210
|
const next = state.kind === "found"
|
|
182
211
|
? spliceBlock(existing, state.beginLine, state.endLine, block, lineEnding)
|
|
183
212
|
: insertBlockNearManagedBlocks(existing, blocks, block, lineEnding);
|
|
184
213
|
await assertKimiHookSetValid(next, configPath, "setup.install", context.env);
|
|
214
|
+
// Setup rewrites only its own block and prunable tables. Any other copy of
|
|
215
|
+
// this command (e.g. one with a matcher) can be the entry kimi-code runs.
|
|
216
|
+
const shortAfter = findShortHookTimeout(next, expectedCommand);
|
|
217
|
+
if (shortAfter !== null) {
|
|
218
|
+
throw new RuntimeError("SETUP_INVALID_HOOKS_CONFIG", `kimi-code config at ${configPath} has another [[hooks]] entry with this host's hook command and ${describeHookTimeout(shortAfter.timeout)}; kimi-code runs only one entry per command and reads a hook that runs past its timeout as allow. Remove that entry before rerunning /kimi:setup; the existing config was left unchanged.`, "setup.install", { details: { configPath, timeout: describeHookTimeout(shortAfter.timeout) } });
|
|
219
|
+
}
|
|
220
|
+
if (shortBefore !== null) {
|
|
221
|
+
warnings.push(`Rewrote this host's hook entry from ${describeHookTimeout(shortBefore.timeout)} to timeout = ${HOOK_TIMEOUT_S}: kimi-code reads a hook that runs past its timeout as allow, so the earlier entry did not reliably enforce.`);
|
|
222
|
+
}
|
|
185
223
|
const blockWritten = next !== original;
|
|
186
224
|
if (blockWritten)
|
|
187
225
|
await writeConfigAtomic(configPath, next);
|
|
188
|
-
return { blockWritten, next };
|
|
226
|
+
return { blockWritten, next, expectedCommand };
|
|
189
227
|
});
|
|
190
|
-
const { blockWritten, next } = mutation;
|
|
228
|
+
const { blockWritten, next, expectedCommand } = mutation;
|
|
229
|
+
warnings.push(...await olderHostHookWarnings(next, hostId, expectedCommand));
|
|
191
230
|
collectPermissionRuleWarnings(next, warnings);
|
|
231
|
+
const kimiBinary = describeKimiBinary(context.env, warnings);
|
|
192
232
|
await collectKimiVersionWarnings(context.env, warnings);
|
|
193
233
|
await collectInstalledKimiPluginsNotice(context.env, warnings);
|
|
194
|
-
const probe = await probeHook(hookScriptPath, context.env);
|
|
234
|
+
const probe = await probeHook(hookScriptPath, expectedCommand, context.env);
|
|
195
235
|
const summary = probe.ok
|
|
196
236
|
? blockWritten
|
|
197
237
|
? `Installed kimi-plugin-cc PreToolUse hook in ${configPath}.`
|
|
@@ -209,7 +249,7 @@ async function runInstall(configPath, hookScriptPath, hostId, reviewGateEnabled,
|
|
|
209
249
|
warnings,
|
|
210
250
|
reviewGateEnabled,
|
|
211
251
|
nextStep: probe.ok
|
|
212
|
-
? "Run /kimi:
|
|
252
|
+
? "Run /kimi:task, /kimi:review, /kimi:pursue or /kimi:swarm. Codex users can invoke the matching $kimi-* skills."
|
|
213
253
|
: "Re-run /kimi:setup after installing kimi-code and Node. If the probe keeps failing, run /kimi:setup --uninstall and inspect ~/.kimi-code/config.toml manually.",
|
|
214
254
|
details: buildDetails({
|
|
215
255
|
configPath,
|
|
@@ -218,35 +258,44 @@ async function runInstall(configPath, hookScriptPath, hostId, reviewGateEnabled,
|
|
|
218
258
|
probe,
|
|
219
259
|
warnings,
|
|
220
260
|
hostId,
|
|
261
|
+
kimiBinary,
|
|
221
262
|
}),
|
|
222
263
|
};
|
|
223
264
|
}
|
|
224
265
|
async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, warnings, context) {
|
|
266
|
+
const failedCheck = (result) => ({
|
|
267
|
+
action: "check",
|
|
268
|
+
summary: result.summary,
|
|
269
|
+
configPath,
|
|
270
|
+
hookScriptPath,
|
|
271
|
+
blockWritten: false,
|
|
272
|
+
blockRemoved: false,
|
|
273
|
+
probe: "failed",
|
|
274
|
+
probeError: result.reason,
|
|
275
|
+
warnings,
|
|
276
|
+
reviewGateEnabled,
|
|
277
|
+
nextStep: result.nextStep,
|
|
278
|
+
details: buildDetails({
|
|
279
|
+
configPath,
|
|
280
|
+
hookScriptPath,
|
|
281
|
+
reviewGateEnabled,
|
|
282
|
+
probe: { ok: false, reason: result.detailReason ?? result.reason },
|
|
283
|
+
warnings,
|
|
284
|
+
hostId: result.hostId,
|
|
285
|
+
kimiBinary,
|
|
286
|
+
}),
|
|
287
|
+
});
|
|
288
|
+
// Reported on every check path, including early failures.
|
|
289
|
+
const kimiBinary = describeKimiBinary(context.env, warnings);
|
|
225
290
|
const existing = await readConfigSafe(configPath);
|
|
226
291
|
const hookSet = await validateKimiHookSetForEnvironment(existing, context.env);
|
|
227
292
|
if (!hookSet.valid) {
|
|
228
|
-
|
|
229
|
-
return {
|
|
230
|
-
action: "check",
|
|
293
|
+
return failedCheck({
|
|
231
294
|
summary: `kimi-code hook configuration is invalid; the configured hooks array will not load.`,
|
|
232
|
-
|
|
233
|
-
hookScriptPath,
|
|
234
|
-
blockWritten: false,
|
|
235
|
-
blockRemoved: false,
|
|
236
|
-
probe: "failed",
|
|
237
|
-
probeError: reason,
|
|
238
|
-
warnings,
|
|
239
|
-
reviewGateEnabled,
|
|
295
|
+
reason: hookSet.reason ?? "configured hooks failed validation",
|
|
240
296
|
nextStep: "Repair or remove the invalid [[hooks]] entry named above, then run /kimi:setup --check again. No model call is required.",
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
hookScriptPath,
|
|
244
|
-
reviewGateEnabled,
|
|
245
|
-
probe: { ok: false, reason },
|
|
246
|
-
warnings,
|
|
247
|
-
hostId,
|
|
248
|
-
}),
|
|
249
|
-
};
|
|
297
|
+
hostId,
|
|
298
|
+
});
|
|
250
299
|
}
|
|
251
300
|
const expectedCommand = buildHookShellCommand(hookScriptPath, context.env);
|
|
252
301
|
// nodeExists refines mismatch prose into drift classification; hostId checks
|
|
@@ -266,6 +315,7 @@ async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, w
|
|
|
266
315
|
if (otherHosts.length > 0) {
|
|
267
316
|
warnings.push(`Other host(s) also manage a block in this shared config: ${otherHosts.join(", ")} (coexisting, untouched).`);
|
|
268
317
|
}
|
|
318
|
+
warnings.push(...await olderHostHookWarnings(existing, hostId, expectedCommand));
|
|
269
319
|
// Config rewrites can strip markers while leaving each host's table enforcing.
|
|
270
320
|
// Our exact-command table is live; our stale table is prunable; another host's
|
|
271
321
|
// table is reported but left strictly alone.
|
|
@@ -291,32 +341,22 @@ async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, w
|
|
|
291
341
|
warnings.push(`Note: ${installedCheck.note}`);
|
|
292
342
|
}
|
|
293
343
|
if (!installedCheck.installed) {
|
|
294
|
-
return {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
? ORPHAN_MARKER_REMEDY
|
|
311
|
-
: "Run /kimi:setup --uninstall, then /kimi:setup to repair.",
|
|
312
|
-
details: buildDetails({
|
|
313
|
-
configPath,
|
|
314
|
-
hookScriptPath,
|
|
315
|
-
reviewGateEnabled,
|
|
316
|
-
probe: { ok: false, reason: installedCheck.reason ?? "block missing/invalid" },
|
|
317
|
-
warnings,
|
|
318
|
-
}),
|
|
319
|
-
};
|
|
344
|
+
return failedCheck({
|
|
345
|
+
summary: installedCheck.timeoutTooShort === true
|
|
346
|
+
? `kimi-plugin-cc PreToolUse hook in ${configPath} does not enforce: ${installedCheck.reason}`
|
|
347
|
+
: installedCheck.state.kind === "absent"
|
|
348
|
+
? `kimi-plugin-cc managed block is NOT installed in ${configPath}.`
|
|
349
|
+
: `kimi-plugin-cc managed block is present but invalid: ${installedCheck.reason}.`,
|
|
350
|
+
reason: installedCheck.reason ?? "managed block missing or invalid",
|
|
351
|
+
detailReason: installedCheck.reason ?? "block missing/invalid",
|
|
352
|
+
nextStep: installedCheck.timeoutTooShort === true
|
|
353
|
+
? "Run /kimi:setup (without --check) to rewrite the hook timeout."
|
|
354
|
+
: installedCheck.state.kind === "absent"
|
|
355
|
+
? "Run /kimi:setup (without --check) to install the managed block."
|
|
356
|
+
: installedCheck.state.kind === "orphan"
|
|
357
|
+
? ORPHAN_MARKER_REMEDY
|
|
358
|
+
: "Run /kimi:setup --uninstall, then /kimi:setup to repair.",
|
|
359
|
+
});
|
|
320
360
|
}
|
|
321
361
|
collectPermissionRuleWarnings(existing, warnings);
|
|
322
362
|
await collectKimiVersionWarnings(context.env, warnings);
|
|
@@ -327,29 +367,13 @@ async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, w
|
|
|
327
367
|
await assertHookScriptExists(hookScriptPath);
|
|
328
368
|
}
|
|
329
369
|
catch (err) {
|
|
330
|
-
|
|
331
|
-
return {
|
|
332
|
-
action: "check",
|
|
370
|
+
return failedCheck({
|
|
333
371
|
summary: `Managed block references ${hookScriptPath} but that path is unreadable.`,
|
|
334
|
-
|
|
335
|
-
hookScriptPath,
|
|
336
|
-
blockWritten: false,
|
|
337
|
-
blockRemoved: false,
|
|
338
|
-
probe: "failed",
|
|
339
|
-
probeError: message,
|
|
340
|
-
warnings,
|
|
341
|
-
reviewGateEnabled,
|
|
372
|
+
reason: err instanceof Error ? err.message : String(err),
|
|
342
373
|
nextStep: "Reinstall the plugin so dist/hooks/approval-hook.js is present, or run /kimi:setup to refresh the managed block.",
|
|
343
|
-
|
|
344
|
-
configPath,
|
|
345
|
-
hookScriptPath,
|
|
346
|
-
reviewGateEnabled,
|
|
347
|
-
probe: { ok: false, reason: message },
|
|
348
|
-
warnings,
|
|
349
|
-
}),
|
|
350
|
-
};
|
|
374
|
+
});
|
|
351
375
|
}
|
|
352
|
-
const probe = await probeHook(hookScriptPath, context.env);
|
|
376
|
+
const probe = await probeHook(hookScriptPath, expectedCommand, context.env);
|
|
353
377
|
return {
|
|
354
378
|
action: "check",
|
|
355
379
|
summary: probe.ok
|
|
@@ -377,87 +401,87 @@ async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, w
|
|
|
377
401
|
probe,
|
|
378
402
|
warnings,
|
|
379
403
|
hostId,
|
|
404
|
+
kimiBinary,
|
|
380
405
|
}),
|
|
381
406
|
};
|
|
382
407
|
}
|
|
383
408
|
async function runUninstall(configPath, hookScriptPath, hostId, removeAllHosts, reviewGateEnabled, warnings, context) {
|
|
384
|
-
return await withKimiConfigLock(configPath, () =>
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
409
|
+
return await withKimiConfigLock(configPath, async () => {
|
|
410
|
+
const existing = await readConfigSafe(configPath);
|
|
411
|
+
if (existing.length === 0) {
|
|
412
|
+
return {
|
|
413
|
+
action: "uninstall",
|
|
414
|
+
summary: `Nothing to remove — ${configPath} does not exist or is empty.`,
|
|
415
|
+
configPath,
|
|
416
|
+
hookScriptPath,
|
|
417
|
+
blockWritten: false,
|
|
418
|
+
blockRemoved: false,
|
|
419
|
+
probe: "skipped",
|
|
420
|
+
warnings,
|
|
421
|
+
reviewGateEnabled,
|
|
422
|
+
nextStep: "Run /kimi:setup to install the managed block again.",
|
|
423
|
+
details: buildDetails({
|
|
424
|
+
configPath,
|
|
425
|
+
hookScriptPath,
|
|
426
|
+
reviewGateEnabled,
|
|
427
|
+
probe: { ok: true, reason: "no-op (config absent)" },
|
|
428
|
+
warnings,
|
|
429
|
+
}),
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
// Scoped uninstall removes only this host's marked and marker-less blocks;
|
|
433
|
+
// other hosts' blocks and live marker-less tables remain intact. `--all` is
|
|
434
|
+
// the explicit every-host sweep for all of this plugin's hook tables.
|
|
435
|
+
const { stripped: strippedMarkers, removedBlocks, orphansLeft } = stripManagedBlocks(existing, hostId, removeAllHosts);
|
|
436
|
+
// Exact current command bytes also identify this host's table when a host-id
|
|
437
|
+
// override disagrees with path derivation or a dev path is outside the strict
|
|
438
|
+
// ownership pattern.
|
|
439
|
+
const expectedCommand = buildHookShellCommand(hookScriptPath, context.env);
|
|
440
|
+
const { pruned: stripped, count: prunedCount } = pruneOrphanApprovalHooks(strippedMarkers, removeAllHosts ? undefined : hostId, expectedCommand);
|
|
441
|
+
const changed = stripped !== existing;
|
|
442
|
+
if (changed) {
|
|
443
|
+
await writeConfigAtomic(configPath, stripped);
|
|
444
|
+
}
|
|
445
|
+
if (orphansLeft.length > 0) {
|
|
446
|
+
warnings.push(`Detected ${orphansLeft.length} orphan marker line(s) at ${orphansLeft
|
|
447
|
+
.map((line) => `line ${line + 1}`)
|
|
448
|
+
.join(", ")}. Removed the marker line(s) but preserved surrounding user content — verify the config visually.`);
|
|
449
|
+
}
|
|
450
|
+
if (prunedCount > 0) {
|
|
451
|
+
warnings.push(`Pruned ${prunedCount} orphaned kimi-plugin-cc hook block(s) with no managed marker.`);
|
|
452
|
+
}
|
|
453
|
+
const { blocks: remainingBlocks } = parseManagedBlock(stripped, hostId);
|
|
454
|
+
const remainingHosts = [
|
|
455
|
+
...new Set(remainingBlocks.map((b) => effectiveHost(b, hostId))),
|
|
456
|
+
];
|
|
457
|
+
if (!removeAllHosts && remainingHosts.length > 0) {
|
|
458
|
+
warnings.push(`Left ${remainingHosts.length} other host block(s) in place: ${remainingHosts.join(", ")}. ` +
|
|
459
|
+
`Use \`/kimi:setup --uninstall --all\` to remove every host's block.`);
|
|
460
|
+
}
|
|
389
461
|
return {
|
|
390
462
|
action: "uninstall",
|
|
391
|
-
summary:
|
|
463
|
+
summary: changed
|
|
464
|
+
? `Removed ${removedBlocks} managed block(s)${prunedCount > 0 ? ` + ${prunedCount} orphan hook block(s)` : ""} from ${configPath}.`
|
|
465
|
+
: `No kimi-plugin-cc managed block to remove from ${configPath}.`,
|
|
392
466
|
configPath,
|
|
393
467
|
hookScriptPath,
|
|
394
468
|
blockWritten: false,
|
|
395
|
-
blockRemoved:
|
|
469
|
+
blockRemoved: changed,
|
|
396
470
|
probe: "skipped",
|
|
397
471
|
warnings,
|
|
398
472
|
reviewGateEnabled,
|
|
399
|
-
nextStep:
|
|
473
|
+
nextStep: changed
|
|
474
|
+
? "Run /kimi:setup again to reinstall the hook, or leave the plugin uninstalled."
|
|
475
|
+
: "Run /kimi:setup to install the hook.",
|
|
400
476
|
details: buildDetails({
|
|
401
477
|
configPath,
|
|
402
478
|
hookScriptPath,
|
|
403
479
|
reviewGateEnabled,
|
|
404
|
-
probe: { ok: true, reason: "no-op (
|
|
480
|
+
probe: { ok: true, reason: changed ? `removed ${removedBlocks} block(s)` : "no-op (no markers)" },
|
|
405
481
|
warnings,
|
|
406
482
|
}),
|
|
407
483
|
};
|
|
408
|
-
}
|
|
409
|
-
// Scoped uninstall removes only this host's marked and marker-less blocks;
|
|
410
|
-
// other hosts' blocks and live marker-less tables remain intact. `--all` is
|
|
411
|
-
// the explicit every-host sweep for all of this plugin's hook tables.
|
|
412
|
-
const { stripped: strippedMarkers, removedBlocks, orphansLeft } = stripManagedBlocks(existing, hostId, removeAllHosts);
|
|
413
|
-
// Exact current command bytes also identify this host's table when a host-id
|
|
414
|
-
// override disagrees with path derivation or a dev path is outside the strict
|
|
415
|
-
// ownership pattern.
|
|
416
|
-
const expectedCommand = buildHookShellCommand(hookScriptPath, context.env);
|
|
417
|
-
const { pruned: stripped, count: prunedCount } = pruneOrphanApprovalHooks(strippedMarkers, removeAllHosts ? undefined : hostId, expectedCommand);
|
|
418
|
-
const changed = stripped !== existing;
|
|
419
|
-
if (changed) {
|
|
420
|
-
await writeConfigAtomic(configPath, stripped);
|
|
421
|
-
}
|
|
422
|
-
if (orphansLeft.length > 0) {
|
|
423
|
-
warnings.push(`Detected ${orphansLeft.length} orphan marker line(s) at ${orphansLeft
|
|
424
|
-
.map((line) => `line ${line + 1}`)
|
|
425
|
-
.join(", ")}. Removed the marker line(s) but preserved surrounding user content — verify the config visually.`);
|
|
426
|
-
}
|
|
427
|
-
if (prunedCount > 0) {
|
|
428
|
-
warnings.push(`Pruned ${prunedCount} orphaned kimi-plugin-cc hook block(s) with no managed marker.`);
|
|
429
|
-
}
|
|
430
|
-
const { blocks: remainingBlocks } = parseManagedBlock(stripped, hostId);
|
|
431
|
-
const remainingHosts = [
|
|
432
|
-
...new Set(remainingBlocks.map((b) => effectiveHost(b, hostId))),
|
|
433
|
-
];
|
|
434
|
-
if (!removeAllHosts && remainingHosts.length > 0) {
|
|
435
|
-
warnings.push(`Left ${remainingHosts.length} other host block(s) in place: ${remainingHosts.join(", ")}. ` +
|
|
436
|
-
`Use \`/kimi:setup --uninstall --all\` to remove every host's block.`);
|
|
437
|
-
}
|
|
438
|
-
return {
|
|
439
|
-
action: "uninstall",
|
|
440
|
-
summary: changed
|
|
441
|
-
? `Removed ${removedBlocks} managed block(s)${prunedCount > 0 ? ` + ${prunedCount} orphan hook block(s)` : ""} from ${configPath}.`
|
|
442
|
-
: `No kimi-plugin-cc managed block to remove from ${configPath}.`,
|
|
443
|
-
configPath,
|
|
444
|
-
hookScriptPath,
|
|
445
|
-
blockWritten: false,
|
|
446
|
-
blockRemoved: changed,
|
|
447
|
-
probe: "skipped",
|
|
448
|
-
warnings,
|
|
449
|
-
reviewGateEnabled,
|
|
450
|
-
nextStep: changed
|
|
451
|
-
? "Run /kimi:setup again to reinstall the hook, or leave the plugin uninstalled."
|
|
452
|
-
: "Run /kimi:setup to install the hook.",
|
|
453
|
-
details: buildDetails({
|
|
454
|
-
configPath,
|
|
455
|
-
hookScriptPath,
|
|
456
|
-
reviewGateEnabled,
|
|
457
|
-
probe: { ok: true, reason: changed ? `removed ${removedBlocks} block(s)` : "no-op (no markers)" },
|
|
458
|
-
warnings,
|
|
459
|
-
}),
|
|
460
|
-
};
|
|
484
|
+
});
|
|
461
485
|
}
|
|
462
486
|
// ----- Config IO ---------------------------------------------------------
|
|
463
487
|
async function readConfigSafe(configPath) {
|
|
@@ -602,40 +626,10 @@ function splitPreservingEnding(contents) {
|
|
|
602
626
|
// Remove only unpaired marker comments, never the intervening content. The
|
|
603
627
|
// normal full-file validation still gates writes.
|
|
604
628
|
function removeOrphanMarkers(contents) {
|
|
605
|
-
const
|
|
606
|
-
const orphans = [];
|
|
607
|
-
const comments = markerCommentLines(contents);
|
|
608
|
-
let open = null;
|
|
609
|
-
for (let i = 0; i < lines.length; i += 1) {
|
|
610
|
-
if (!comments.has(i))
|
|
611
|
-
continue;
|
|
612
|
-
const line = lines[i].trim();
|
|
613
|
-
const begin = MARKERS.BEGIN_LINE_RE.exec(line);
|
|
614
|
-
if (begin !== null) {
|
|
615
|
-
if (open !== null)
|
|
616
|
-
orphans.push(open);
|
|
617
|
-
open = { index: i, host: begin[1]?.toLowerCase() ?? null };
|
|
618
|
-
}
|
|
619
|
-
else {
|
|
620
|
-
const end = MARKERS.END_LINE_RE.exec(line);
|
|
621
|
-
if (end === null)
|
|
622
|
-
continue;
|
|
623
|
-
const endMarker = { index: i, host: end[1]?.toLowerCase() ?? null };
|
|
624
|
-
if (open === null)
|
|
625
|
-
orphans.push(endMarker);
|
|
626
|
-
else {
|
|
627
|
-
if (open.host !== endMarker.host)
|
|
628
|
-
orphans.push(open, endMarker);
|
|
629
|
-
open = null;
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
if (open !== null)
|
|
634
|
-
orphans.push(open);
|
|
635
|
-
const remove = new Set(orphans.map((marker) => marker.index));
|
|
629
|
+
const remove = new Set(pairBlocks(splitPreservingEnding(contents)).orphans.map((marker) => marker.line));
|
|
636
630
|
return remove.size === 0
|
|
637
631
|
? { contents, count: 0 }
|
|
638
|
-
: { contents: contents
|
|
632
|
+
: { contents: removeLines(contents, remove), count: remove.size };
|
|
639
633
|
}
|
|
640
634
|
/**
|
|
641
635
|
* Remove managed blocks owned by `currentHost`, or every host with `removeAll`.
|
|
@@ -644,11 +638,7 @@ function removeOrphanMarkers(contents) {
|
|
|
644
638
|
*/
|
|
645
639
|
function stripManagedBlocks(contents, currentHost, removeAll) {
|
|
646
640
|
const lines = splitPreservingEnding(contents);
|
|
647
|
-
const
|
|
648
|
-
const markerLines = markerCommentLines(contents);
|
|
649
|
-
const result = [];
|
|
650
|
-
const orphansLeft = [];
|
|
651
|
-
let removedBlocks = 0;
|
|
641
|
+
const { blocks, orphans } = pairBlocks(lines);
|
|
652
642
|
// Suffixed blocks belong only to the matching host. Complete legacy blocks
|
|
653
643
|
// use their command-derived host; an unattributable non-canonical command is
|
|
654
644
|
// ambiguous and only --all removes it. Orphan marker lines have no command
|
|
@@ -665,72 +655,28 @@ function stripManagedBlocks(contents, currentHost, removeAll) {
|
|
|
665
655
|
return false;
|
|
666
656
|
return derived === currentHost;
|
|
667
657
|
};
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
const nextTrimmed = lines[j].trim();
|
|
680
|
-
if (markerLines.has(j) && MARKERS.BEGIN_LINE_RE.test(nextTrimmed)) {
|
|
681
|
-
// Two BEGINs in a row — the outer one is an orphan.
|
|
682
|
-
break;
|
|
683
|
-
}
|
|
684
|
-
const end = markerLines.has(j) ? MARKERS.END_LINE_RE.exec(nextTrimmed) : null;
|
|
685
|
-
if (end !== null) {
|
|
686
|
-
if ((end[1]?.toLowerCase() ?? null) === beginHost)
|
|
687
|
-
endIdx = j;
|
|
688
|
-
break;
|
|
689
|
-
}
|
|
690
|
-
const decoded = decodeManagedCommandLine(nextTrimmed);
|
|
691
|
-
if (decoded !== null)
|
|
692
|
-
blockCommand = decoded;
|
|
693
|
-
}
|
|
694
|
-
if (endIdx === -1) {
|
|
695
|
-
// Orphan BEGIN: drop only this line (if it's ours to remove).
|
|
696
|
-
if (shouldRemove(beginHost, null, true)) {
|
|
697
|
-
orphansLeft.push(i);
|
|
698
|
-
i += 1;
|
|
699
|
-
continue;
|
|
700
|
-
}
|
|
701
|
-
result.push(rawLines[i]);
|
|
702
|
-
i += 1;
|
|
703
|
-
continue;
|
|
704
|
-
}
|
|
705
|
-
if (shouldRemove(beginHost, blockCommand, false)) {
|
|
706
|
-
// Complete pair we're removing — drop BEGIN..END inclusive.
|
|
707
|
-
removedBlocks += 1;
|
|
708
|
-
i = endIdx + 1;
|
|
709
|
-
continue;
|
|
710
|
-
}
|
|
711
|
-
// Another host's block — keep it verbatim.
|
|
712
|
-
for (let k = i; k <= endIdx; k += 1)
|
|
713
|
-
result.push(rawLines[k]);
|
|
714
|
-
i = endIdx + 1;
|
|
715
|
-
continue;
|
|
658
|
+
const orphansLeft = orphans
|
|
659
|
+
.filter((orphan) => shouldRemove(orphan.hostId, null, true))
|
|
660
|
+
.map((orphan) => orphan.line);
|
|
661
|
+
const remove = new Set(orphansLeft);
|
|
662
|
+
let removedBlocks = 0;
|
|
663
|
+
for (const block of blocks) {
|
|
664
|
+
// Attribute by the LAST command line in the body, even one after a line
|
|
665
|
+
// that makes the block invalid (unlike the validator's commandPath).
|
|
666
|
+
let blockCommand = null;
|
|
667
|
+
for (const line of lines.slice(block.beginLine + 1, block.endLine)) {
|
|
668
|
+
blockCommand = decodeManagedCommandLine(line.trim()) ?? blockCommand;
|
|
716
669
|
}
|
|
717
|
-
|
|
718
|
-
if (end !== null) {
|
|
719
|
-
const endHost = end[1]?.toLowerCase() ?? null;
|
|
720
|
-
// Orphan END with no preceding BEGIN: drop only this line (if ours).
|
|
721
|
-
if (shouldRemove(endHost, null, true)) {
|
|
722
|
-
orphansLeft.push(i);
|
|
723
|
-
i += 1;
|
|
724
|
-
continue;
|
|
725
|
-
}
|
|
726
|
-
result.push(rawLines[i]);
|
|
727
|
-
i += 1;
|
|
670
|
+
if (!shouldRemove(block.hostId, blockCommand, false))
|
|
728
671
|
continue;
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
672
|
+
removedBlocks += 1;
|
|
673
|
+
for (let i = block.beginLine; i <= block.endLine; i += 1)
|
|
674
|
+
remove.add(i);
|
|
732
675
|
}
|
|
733
|
-
return { stripped:
|
|
676
|
+
return { stripped: removeLines(contents, remove), removedBlocks, orphansLeft };
|
|
677
|
+
}
|
|
678
|
+
function removeLines(contents, remove) {
|
|
679
|
+
return contents.split(/(?<=\n)/).filter((_, index) => !remove.has(index)).join("");
|
|
734
680
|
}
|
|
735
681
|
/**
|
|
736
682
|
* Remove orphaned, marker-less `[[hooks]]` tables that are unambiguously this
|
|
@@ -763,8 +709,7 @@ function pruneOrphanApprovalHooks(contents, ownedBy, alsoMatchCommand) {
|
|
|
763
709
|
};
|
|
764
710
|
}
|
|
765
711
|
// ----- Block content -----------------------------------------------------
|
|
766
|
-
function buildManagedBlock(
|
|
767
|
-
const shellCommand = buildHookShellCommand(hookScriptPath, env);
|
|
712
|
+
function buildManagedBlock(shellCommand, lineEnding = "\n", hostId = "") {
|
|
768
713
|
const commandLine = `command = ${tomlBasicString(shellCommand)}`;
|
|
769
714
|
const suffix = hostId.length > 0 ? `:${hostId}` : "";
|
|
770
715
|
return [
|
|
@@ -820,9 +765,6 @@ function assertHookPathTomlSafe(hookScriptPath) {
|
|
|
820
765
|
].join(" "), "setup.hook-path", { details: { hookScriptPath } });
|
|
821
766
|
}
|
|
822
767
|
// ----- Path resolution ---------------------------------------------------
|
|
823
|
-
function resolveKimiCodeConfigPath(env) {
|
|
824
|
-
return path.join(resolveKimiHome(env), "config.toml");
|
|
825
|
-
}
|
|
826
768
|
async function assertHookScriptExists(hookScriptPath) {
|
|
827
769
|
try {
|
|
828
770
|
await access(hookScriptPath, fsConstants.R_OK);
|
|
@@ -834,22 +776,23 @@ async function assertHookScriptExists(hookScriptPath) {
|
|
|
834
776
|
/**
|
|
835
777
|
* Two-layer probe; both layers require exit 2 and a non-empty denial reason.
|
|
836
778
|
*
|
|
837
|
-
* 1. Directly spawn the hook with the absolute path from
|
|
838
|
-
* catching missing/broken scripts and incorrect hook exit
|
|
839
|
-
* relying on PATH.
|
|
840
|
-
* 2. Execute the exact canonical
|
|
841
|
-
* `/bin/sh -c`: two shell-quoted absolute tokens for Node and the
|
|
779
|
+
* 1. Directly spawn the hook with the absolute Node path parsed back from
|
|
780
|
+
* `command`, catching missing/broken scripts and incorrect hook exit
|
|
781
|
+
* behavior without relying on PATH.
|
|
782
|
+
* 2. Execute `command`, the exact canonical bytes from buildHookShellCommand,
|
|
783
|
+
* through `/bin/sh -c`: two shell-quoted absolute tokens for Node and the
|
|
784
|
+
* script.
|
|
842
785
|
* This catches pinned-interpreter execution and quoting failures. It does
|
|
843
786
|
* NOT test PATH discovery or compatibility of some other `node` on PATH.
|
|
844
787
|
*
|
|
845
788
|
* The shell layer is skipped where `/bin/sh` is unsupported; the runtime
|
|
846
789
|
* otherwise assumes POSIX.
|
|
847
790
|
*/
|
|
848
|
-
async function probeHook(hookScriptPath, env) {
|
|
849
|
-
const directResult = await probeHookDirect(hookScriptPath, env);
|
|
791
|
+
async function probeHook(hookScriptPath, command, env) {
|
|
792
|
+
const directResult = await probeHookDirect(hookScriptPath, command, env);
|
|
850
793
|
if (!directResult.ok)
|
|
851
794
|
return directResult;
|
|
852
|
-
const shellResult = await probeHookViaShell(
|
|
795
|
+
const shellResult = await probeHookViaShell(command, env);
|
|
853
796
|
if (!shellResult.ok)
|
|
854
797
|
return shellResult;
|
|
855
798
|
return {
|
|
@@ -857,20 +800,20 @@ async function probeHook(hookScriptPath, env) {
|
|
|
857
800
|
reason: `${directResult.reason}; shell probe also ok`,
|
|
858
801
|
};
|
|
859
802
|
}
|
|
860
|
-
async function probeHookDirect(hookScriptPath, env) {
|
|
861
|
-
|
|
803
|
+
async function probeHookDirect(hookScriptPath, command, env) {
|
|
804
|
+
// Parsed back from the command, never re-resolved; the fallback is unreachable.
|
|
805
|
+
const nodeBin = parseHookShellCommand(command)?.nodeBin ?? resolveNodeBinary(env);
|
|
862
806
|
return await spawnProbe(nodeBin, [hookScriptPath], env, `direct probe via ${nodeBin}`);
|
|
863
807
|
}
|
|
864
|
-
async function probeHookViaShell(
|
|
808
|
+
async function probeHookViaShell(shellCommand, env) {
|
|
865
809
|
if (process.platform === "win32") {
|
|
866
810
|
// The hook runner shells out via `/bin/sh -c` per agent-core; on
|
|
867
811
|
// Windows the entire kimi-code launcher is unsupported. Skip the
|
|
868
812
|
// shell probe so we don't false-fail on a platform we don't run on.
|
|
869
813
|
return { ok: true, reason: "shell probe skipped (Windows)" };
|
|
870
814
|
}
|
|
871
|
-
//
|
|
872
|
-
//
|
|
873
|
-
const shellCommand = buildHookShellCommand(hookScriptPath, env);
|
|
815
|
+
// The exact command string the managed block wrote into kimi-code's config,
|
|
816
|
+
// so probe and runtime cannot drift.
|
|
874
817
|
return await spawnProbe("/bin/sh", ["-c", shellCommand], env, `shell probe via /bin/sh -c`);
|
|
875
818
|
}
|
|
876
819
|
function spawnProbe(command, args, env, label) {
|
|
@@ -952,6 +895,369 @@ function truncate(value, max) {
|
|
|
952
895
|
return value;
|
|
953
896
|
return `${value.slice(0, max)}…`;
|
|
954
897
|
}
|
|
898
|
+
// ----- Plugin-owned kimi-code (--install-kimi) ---------------------------
|
|
899
|
+
const KIMI_PACKAGE = "@moonshot-ai/kimi-code";
|
|
900
|
+
const KIMI_INSTALL_TIMEOUT_MS = 300_000;
|
|
901
|
+
const KIMI_INSTALL_KILL_GRACE_MS = 2_000;
|
|
902
|
+
// First start of a fresh install loads a large bundle; allow more than the
|
|
903
|
+
// 5s job-time probe.
|
|
904
|
+
const KIMI_INSTALL_PROBE_TIMEOUT_MS = 30_000;
|
|
905
|
+
const STDERR_TAIL_BYTES = 2_000;
|
|
906
|
+
const STAGING_PREFIX = ".staging-";
|
|
907
|
+
// A staging directory younger than this may belong to a concurrent install
|
|
908
|
+
// still inside its npm timeout and probe window, so it is not stale yet.
|
|
909
|
+
const STALE_STAGING_AGE_MS = 2 * (KIMI_INSTALL_TIMEOUT_MS + KIMI_INSTALL_PROBE_TIMEOUT_MS);
|
|
910
|
+
/** Numeric, not lexical: "2.10.0" is above "2.9.0". */
|
|
911
|
+
export function highestCertifiedKimiVersion(versions = NATIVE_V2_CERTIFIED_VERSIONS) {
|
|
912
|
+
const parse = (v) => v.split(".").map(Number);
|
|
913
|
+
const sorted = [...versions].sort((a, b) => {
|
|
914
|
+
const [x, y] = [parse(a), parse(b)];
|
|
915
|
+
for (let i = 0; i < 3; i += 1) {
|
|
916
|
+
if (x[i] !== y[i])
|
|
917
|
+
return (x[i] ?? 0) - (y[i] ?? 0);
|
|
918
|
+
}
|
|
919
|
+
return 0;
|
|
920
|
+
});
|
|
921
|
+
const highest = sorted[sorted.length - 1];
|
|
922
|
+
if (highest === undefined || !/^\d+\.\d+\.\d+$/.test(highest)) {
|
|
923
|
+
throw new RuntimeError("KIMI_INSTALL_FAILED", "No exact certified kimi-code version is available to install.", "setup.install-kimi");
|
|
924
|
+
}
|
|
925
|
+
return highest;
|
|
926
|
+
}
|
|
927
|
+
function compareExactVersions(a, b) {
|
|
928
|
+
const [x, y] = [a.split(".").map(Number), b.split(".").map(Number)];
|
|
929
|
+
for (let i = 0; i < 3; i += 1) {
|
|
930
|
+
if (x[i] !== y[i])
|
|
931
|
+
return (x[i] ?? 0) - (y[i] ?? 0);
|
|
932
|
+
}
|
|
933
|
+
return 0;
|
|
934
|
+
}
|
|
935
|
+
async function runInstallKimi(json, context) {
|
|
936
|
+
let version = "";
|
|
937
|
+
try {
|
|
938
|
+
version = highestCertifiedKimiVersion();
|
|
939
|
+
const outcome = await installPluginOwnedKimi(version, context.env);
|
|
940
|
+
const selection = selectKimiBinary(context.env);
|
|
941
|
+
const jobsUseIt = selection.source === "plugin-owned" && selection.command === outcome.binary;
|
|
942
|
+
const summary = outcome.downloaded
|
|
943
|
+
? `Installed kimi-code ${version} for kimi-plugin-cc.`
|
|
944
|
+
: `kimi-code ${version} is already installed for kimi-plugin-cc; nothing was downloaded.`;
|
|
945
|
+
const jobsLine = jobsUseIt
|
|
946
|
+
? "Jobs will now use this binary."
|
|
947
|
+
: selection.source === "override"
|
|
948
|
+
? `KIMI_PLUGIN_CC_KIMI_BIN is set, so jobs keep using ${selection.command} until it is unset.`
|
|
949
|
+
: `Jobs do not use this binary yet: ${selection.pluginOwned.reason ?? "selection did not pick it"}.`;
|
|
950
|
+
return installKimiResult({
|
|
951
|
+
summary,
|
|
952
|
+
failed: false,
|
|
953
|
+
details: [
|
|
954
|
+
`Version: ${version}`,
|
|
955
|
+
`Binary: ${outcome.binary}`,
|
|
956
|
+
`Current link: ${outcome.current} -> ${path.join(version, "node_modules", ".bin", "kimi")}`,
|
|
957
|
+
`Jobs: ${jobsLine}`,
|
|
958
|
+
],
|
|
959
|
+
nextStep: jobsUseIt
|
|
960
|
+
? "Run /kimi:setup --check to confirm the hook and the selected binary."
|
|
961
|
+
: "Unset KIMI_PLUGIN_CC_KIMI_BIN to use the plugin-owned binary, then run /kimi:setup --check.",
|
|
962
|
+
report: {
|
|
963
|
+
ok: true,
|
|
964
|
+
json,
|
|
965
|
+
version,
|
|
966
|
+
downloaded: outcome.downloaded,
|
|
967
|
+
binary: outcome.binary,
|
|
968
|
+
current: outcome.current,
|
|
969
|
+
selectedBinary: selection.command,
|
|
970
|
+
selectedSource: selection.source,
|
|
971
|
+
},
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
catch (error) {
|
|
975
|
+
const runtimeError = error instanceof RuntimeError
|
|
976
|
+
? error
|
|
977
|
+
: new RuntimeError("KIMI_INSTALL_FAILED", error instanceof Error ? error.message : String(error), "setup.install-kimi");
|
|
978
|
+
return installKimiResult({
|
|
979
|
+
summary: `Failed to install kimi-code${version ? ` ${version}` : ""} for kimi-plugin-cc; the selected binary is unchanged.`,
|
|
980
|
+
failed: true,
|
|
981
|
+
probeError: `[${runtimeError.code}] ${runtimeError.message}`,
|
|
982
|
+
details: [`Error: [${runtimeError.code}] ${runtimeError.message}`],
|
|
983
|
+
nextStep: "Fix the cause above and re-run setup --install-kimi. Jobs keep using the previously selected binary.",
|
|
984
|
+
report: {
|
|
985
|
+
ok: false,
|
|
986
|
+
json,
|
|
987
|
+
version,
|
|
988
|
+
downloaded: false,
|
|
989
|
+
error: { code: runtimeError.code, message: runtimeError.message, details: runtimeError.details },
|
|
990
|
+
},
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
function installKimiResult(args) {
|
|
995
|
+
return {
|
|
996
|
+
action: "install-kimi",
|
|
997
|
+
summary: args.summary,
|
|
998
|
+
configPath: "",
|
|
999
|
+
hookScriptPath: "",
|
|
1000
|
+
blockWritten: false,
|
|
1001
|
+
blockRemoved: false,
|
|
1002
|
+
// "failed" makes the companion exit 1; the hook itself is not probed here.
|
|
1003
|
+
probe: args.failed ? "failed" : "skipped",
|
|
1004
|
+
probeError: args.probeError,
|
|
1005
|
+
warnings: [],
|
|
1006
|
+
reviewGateEnabled: false,
|
|
1007
|
+
nextStep: args.nextStep,
|
|
1008
|
+
details: args.details,
|
|
1009
|
+
kimiInstall: args.report,
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Install exactly `version` into `<data root>/kimi-plugin-cc/kimi-bin/<version>/` and point
|
|
1014
|
+
* `current` at it. A verified existing install is reused without network
|
|
1015
|
+
* access. Downloads stage in a sibling directory that is verified with the
|
|
1016
|
+
* exact-version probe before it is renamed into place; any failure removes
|
|
1017
|
+
* the staging directory and leaves `current` untouched.
|
|
1018
|
+
*/
|
|
1019
|
+
async function installPluginOwnedKimi(version, env) {
|
|
1020
|
+
if (process.platform === "win32") {
|
|
1021
|
+
throw new RuntimeError("KIMI_INSTALL_UNSUPPORTED_PLATFORM", "setup --install-kimi is not supported on Windows. Install kimi-code yourself and set KIMI_PLUGIN_CC_KIMI_BIN to its path.", "setup.install-kimi");
|
|
1022
|
+
}
|
|
1023
|
+
const binDir = resolveKimiBinDir(env);
|
|
1024
|
+
await mkdir(binDir, { recursive: true });
|
|
1025
|
+
await removeStaleStaging(binDir);
|
|
1026
|
+
const versionDir = path.join(binDir, version);
|
|
1027
|
+
const binRelative = path.join(version, "node_modules", ".bin", "kimi");
|
|
1028
|
+
const binPath = path.join(binDir, binRelative);
|
|
1029
|
+
const currentPath = path.join(binDir, "current");
|
|
1030
|
+
const existing = await verifyKimiBinary(binPath, binDir, version, version, env);
|
|
1031
|
+
if (existing.ok) {
|
|
1032
|
+
await pointCurrentAt(binDir, binRelative, currentPath, env);
|
|
1033
|
+
return { version, downloaded: false, binary: await realpath(binPath), current: currentPath };
|
|
1034
|
+
}
|
|
1035
|
+
const staging = await mkdtemp(path.join(binDir, `${STAGING_PREFIX}${version}-`));
|
|
1036
|
+
let trash;
|
|
1037
|
+
try {
|
|
1038
|
+
await writeFile(path.join(staging, "package.json"), `${JSON.stringify({ name: "kimi-plugin-cc-kimi-bin", private: true })}\n`, "utf8");
|
|
1039
|
+
await runNpmInstall(staging, version, env);
|
|
1040
|
+
const staged = await verifyKimiBinary(path.join(staging, "node_modules", ".bin", "kimi"), binDir, path.basename(staging), version, env);
|
|
1041
|
+
if (!staged.ok) {
|
|
1042
|
+
throw new RuntimeError("KIMI_INSTALL_VERIFY_FAILED", `The installed kimi-code did not verify as exactly ${version}: ${staged.reason}.`, "setup.install-kimi", { details: { expected_version: version } });
|
|
1043
|
+
}
|
|
1044
|
+
if (await pathExists(versionDir)) {
|
|
1045
|
+
// A present but unverifiable version directory is set aside, not reused.
|
|
1046
|
+
trash = path.join(binDir, `.trash-${version}-${randomBytes(4).toString("hex")}`);
|
|
1047
|
+
await rename(versionDir, trash);
|
|
1048
|
+
}
|
|
1049
|
+
try {
|
|
1050
|
+
await rename(staging, versionDir);
|
|
1051
|
+
}
|
|
1052
|
+
catch (error) {
|
|
1053
|
+
if (trash) {
|
|
1054
|
+
await rename(trash, versionDir).catch(() => undefined);
|
|
1055
|
+
trash = undefined;
|
|
1056
|
+
}
|
|
1057
|
+
throw error;
|
|
1058
|
+
}
|
|
1059
|
+
await pointCurrentAt(binDir, binRelative, currentPath, env);
|
|
1060
|
+
return { version, downloaded: true, binary: await realpath(binPath), current: currentPath };
|
|
1061
|
+
}
|
|
1062
|
+
finally {
|
|
1063
|
+
await rm(staging, { recursive: true, force: true });
|
|
1064
|
+
if (trash)
|
|
1065
|
+
await rm(trash, { recursive: true, force: true });
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
/** Remove staging directories an interrupted install left directly in kimi-bin/. */
|
|
1069
|
+
async function removeStaleStaging(binDir, now = Date.now()) {
|
|
1070
|
+
let entries;
|
|
1071
|
+
try {
|
|
1072
|
+
entries = await readdir(binDir);
|
|
1073
|
+
}
|
|
1074
|
+
catch {
|
|
1075
|
+
return;
|
|
1076
|
+
}
|
|
1077
|
+
for (const name of entries) {
|
|
1078
|
+
if (!name.startsWith(STAGING_PREFIX))
|
|
1079
|
+
continue;
|
|
1080
|
+
const entry = path.join(binDir, name);
|
|
1081
|
+
try {
|
|
1082
|
+
const info = await lstat(entry);
|
|
1083
|
+
if (!info.isDirectory() || now - info.mtimeMs < STALE_STAGING_AGE_MS)
|
|
1084
|
+
continue;
|
|
1085
|
+
await rm(entry, { recursive: true, force: true });
|
|
1086
|
+
}
|
|
1087
|
+
catch {
|
|
1088
|
+
// Best effort: a leftover never affects binary resolution.
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
* Containment comes before execution: `binary` must resolve to an executable
|
|
1094
|
+
* regular file physically inside `<realpath(binDir)>/<dirName>/`, and only
|
|
1095
|
+
* that resolved path is probed. Anchoring at the real kimi-bin plus the
|
|
1096
|
+
* expected name rejects a version or staging directory that is itself a link.
|
|
1097
|
+
*/
|
|
1098
|
+
async function verifyKimiBinary(binary, binDir, dirName, version, env) {
|
|
1099
|
+
let resolved;
|
|
1100
|
+
let root;
|
|
1101
|
+
try {
|
|
1102
|
+
resolved = await realpath(binary);
|
|
1103
|
+
root = path.join(await realpath(binDir), dirName);
|
|
1104
|
+
}
|
|
1105
|
+
catch {
|
|
1106
|
+
return { ok: false, reason: `${binary} is missing` };
|
|
1107
|
+
}
|
|
1108
|
+
if (!resolved.startsWith(`${root}${path.sep}`)) {
|
|
1109
|
+
return { ok: false, reason: `${binary} resolves to ${resolved}, outside ${root}${path.sep}` };
|
|
1110
|
+
}
|
|
1111
|
+
try {
|
|
1112
|
+
const info = await lstat(resolved);
|
|
1113
|
+
if (!info.isFile())
|
|
1114
|
+
return { ok: false, reason: `${resolved} is not a regular file` };
|
|
1115
|
+
await access(resolved, fsConstants.X_OK);
|
|
1116
|
+
}
|
|
1117
|
+
catch {
|
|
1118
|
+
return { ok: false, reason: `${resolved} is not executable` };
|
|
1119
|
+
}
|
|
1120
|
+
const probe = await probeKimiVersion({ kimiBin: resolved, env, timeoutMs: KIMI_INSTALL_PROBE_TIMEOUT_MS });
|
|
1121
|
+
if (probe.kind !== "ok")
|
|
1122
|
+
return { ok: false, reason: probe.reason };
|
|
1123
|
+
const observed = probe.version.replace(/^v/, "");
|
|
1124
|
+
return observed === version ? { ok: true } : { ok: false, reason: `\`--version\` reported ${probe.version}` };
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Atomically replace `current` (POSIX rename over the old link), then confirm
|
|
1128
|
+
* binary resolution accepts the new link; otherwise restore the old one.
|
|
1129
|
+
*/
|
|
1130
|
+
async function pointCurrentAt(binDir, binRelative, currentPath, env) {
|
|
1131
|
+
let previous;
|
|
1132
|
+
try {
|
|
1133
|
+
previous = await readlink(currentPath);
|
|
1134
|
+
}
|
|
1135
|
+
catch {
|
|
1136
|
+
previous = undefined;
|
|
1137
|
+
}
|
|
1138
|
+
if (previous === binRelative && inspectPluginOwnedKimi(env).status === "active")
|
|
1139
|
+
return;
|
|
1140
|
+
const swap = async (target) => {
|
|
1141
|
+
const temp = path.join(binDir, `.current-${randomBytes(4).toString("hex")}`);
|
|
1142
|
+
await symlink(target, temp);
|
|
1143
|
+
try {
|
|
1144
|
+
await rename(temp, currentPath);
|
|
1145
|
+
}
|
|
1146
|
+
catch (error) {
|
|
1147
|
+
await rm(temp, { force: true });
|
|
1148
|
+
throw error;
|
|
1149
|
+
}
|
|
1150
|
+
};
|
|
1151
|
+
await swap(binRelative);
|
|
1152
|
+
const state = inspectPluginOwnedKimi(env);
|
|
1153
|
+
if (state.status !== "active") {
|
|
1154
|
+
if (previous !== undefined)
|
|
1155
|
+
await swap(previous);
|
|
1156
|
+
else
|
|
1157
|
+
await rm(currentPath, { force: true });
|
|
1158
|
+
throw new RuntimeError("KIMI_INSTALL_VERIFY_FAILED", `The new ${currentPath} link was not accepted by binary resolution: ${state.reason ?? state.status}. The previous link was restored.`, "setup.install-kimi");
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
async function runNpmInstall(prefix, version, env) {
|
|
1162
|
+
// Never a global install, whatever the caller's npm environment says.
|
|
1163
|
+
const npmEnv = { ...env };
|
|
1164
|
+
for (const key of Object.keys(npmEnv)) {
|
|
1165
|
+
if (/^npm_config_(global|location|prefix)$/i.test(key))
|
|
1166
|
+
delete npmEnv[key];
|
|
1167
|
+
}
|
|
1168
|
+
const args = [
|
|
1169
|
+
"install",
|
|
1170
|
+
"--prefix", prefix,
|
|
1171
|
+
"--no-global",
|
|
1172
|
+
// kimi-code's postinstall only acts on global installs and no runtime
|
|
1173
|
+
// dependency declares an install script.
|
|
1174
|
+
"--ignore-scripts",
|
|
1175
|
+
"--no-audit",
|
|
1176
|
+
"--no-fund",
|
|
1177
|
+
"--no-update-notifier",
|
|
1178
|
+
"--save-exact",
|
|
1179
|
+
`${KIMI_PACKAGE}@${version}`,
|
|
1180
|
+
];
|
|
1181
|
+
await new Promise((resolve, reject) => {
|
|
1182
|
+
let child;
|
|
1183
|
+
try {
|
|
1184
|
+
child = spawn("npm", args, { cwd: prefix, env: npmEnv, stdio: ["ignore", "ignore", "pipe"], detached: true });
|
|
1185
|
+
}
|
|
1186
|
+
catch (error) {
|
|
1187
|
+
reject(npmFailure(`could not start npm: ${error.message}`, version));
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
let stderr = "";
|
|
1191
|
+
child.stderr.setEncoding("utf8");
|
|
1192
|
+
child.stderr.on("data", (chunk) => {
|
|
1193
|
+
stderr = (stderr + chunk).slice(-STDERR_TAIL_BYTES);
|
|
1194
|
+
});
|
|
1195
|
+
let timedOut = false;
|
|
1196
|
+
const killGroup = (signal) => {
|
|
1197
|
+
try {
|
|
1198
|
+
if (child.pid !== undefined)
|
|
1199
|
+
process.kill(-child.pid, signal);
|
|
1200
|
+
}
|
|
1201
|
+
catch {
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
const timer = setTimeout(() => {
|
|
1205
|
+
timedOut = true;
|
|
1206
|
+
killGroup("SIGTERM");
|
|
1207
|
+
setTimeout(() => killGroup("SIGKILL"), KIMI_INSTALL_KILL_GRACE_MS).unref();
|
|
1208
|
+
}, KIMI_INSTALL_TIMEOUT_MS);
|
|
1209
|
+
child.on("error", (error) => {
|
|
1210
|
+
clearTimeout(timer);
|
|
1211
|
+
reject(npmFailure(`could not start npm: ${error.message}`, version));
|
|
1212
|
+
});
|
|
1213
|
+
child.on("close", (code, signal) => {
|
|
1214
|
+
clearTimeout(timer);
|
|
1215
|
+
if (timedOut) {
|
|
1216
|
+
reject(npmFailure(`npm install timed out after ${KIMI_INSTALL_TIMEOUT_MS / 1000}s`, version, stderr));
|
|
1217
|
+
}
|
|
1218
|
+
else if (code !== 0) {
|
|
1219
|
+
reject(npmFailure(`npm install exited ${code ?? signal}`, version, stderr));
|
|
1220
|
+
}
|
|
1221
|
+
else {
|
|
1222
|
+
resolve();
|
|
1223
|
+
}
|
|
1224
|
+
});
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
function npmFailure(reason, version, stderr = "") {
|
|
1228
|
+
return new RuntimeError("KIMI_INSTALL_FAILED", `Could not install ${KIMI_PACKAGE}@${version}: ${reason}.`, "setup.install-kimi", { details: { package: `${KIMI_PACKAGE}@${version}`, ...(stderr.trim() ? { npm_stderr_tail: stderr.trim() } : {}) } });
|
|
1229
|
+
}
|
|
1230
|
+
async function pathExists(target) {
|
|
1231
|
+
try {
|
|
1232
|
+
await access(target);
|
|
1233
|
+
return true;
|
|
1234
|
+
}
|
|
1235
|
+
catch {
|
|
1236
|
+
return false;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
/** Which binary jobs will use and why, plus a note when the plugin-owned one is stale or unusable. */
|
|
1240
|
+
function describeKimiBinary(env, warnings) {
|
|
1241
|
+
const selection = selectKimiBinary(env);
|
|
1242
|
+
const owned = selection.pluginOwned;
|
|
1243
|
+
const highest = highestCertifiedKimiVersion();
|
|
1244
|
+
if (owned.status === "active" && owned.version && compareExactVersions(owned.version, highest) < 0) {
|
|
1245
|
+
warnings.push(`NOTE: the plugin-owned kimi-code ${owned.version} is behind the highest certified version ${highest}. Run setup --install-kimi to install ${highest}.`);
|
|
1246
|
+
}
|
|
1247
|
+
if (owned.status === "rejected") {
|
|
1248
|
+
warnings.push(`The plugin-owned kimi-code install is not used: ${owned.reason}. Run setup --install-kimi to repair it.`);
|
|
1249
|
+
}
|
|
1250
|
+
switch (selection.source) {
|
|
1251
|
+
case "override":
|
|
1252
|
+
return `${selection.command} (KIMI_PLUGIN_CC_KIMI_BIN override)`;
|
|
1253
|
+
case "plugin-owned":
|
|
1254
|
+
return `${selection.command} (plugin-owned kimi-code ${owned.version} from setup --install-kimi)`;
|
|
1255
|
+
case "path":
|
|
1256
|
+
return owned.status === "absent"
|
|
1257
|
+
? `kimi on PATH (no plugin-owned install; setup --install-kimi installs ${highest})`
|
|
1258
|
+
: `kimi on PATH (plugin-owned install ${owned.status}${owned.reason ? `: ${owned.reason}` : ""})`;
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
955
1261
|
// ----- Permission rules scan --------------------------------------------
|
|
956
1262
|
/**
|
|
957
1263
|
* Warn only when the installed kimi is demonstrably outside the certified
|
|
@@ -963,8 +1269,9 @@ function truncate(value, max) {
|
|
|
963
1269
|
async function collectKimiVersionWarnings(env, warnings) {
|
|
964
1270
|
if (env.KIMI_PLUGIN_CC_SKIP_VERSION_PROBE === "1")
|
|
965
1271
|
return;
|
|
1272
|
+
// The same binary jobs select (override, plugin-owned, then PATH).
|
|
966
1273
|
const probe = await probeKimiVersion({
|
|
967
|
-
kimiBin: env.
|
|
1274
|
+
kimiBin: selectKimiBinary(env).command,
|
|
968
1275
|
env,
|
|
969
1276
|
});
|
|
970
1277
|
if (probe.kind === "failed") {
|
|
@@ -977,12 +1284,12 @@ async function collectKimiVersionWarnings(env, warnings) {
|
|
|
977
1284
|
warnings.push(formatVersionOutOfRangeWarning(probe, KIMI_PLUGIN_CC_VERSION, NATIVE_V2_ANY_OPERATION_VERSIONS));
|
|
978
1285
|
return;
|
|
979
1286
|
}
|
|
980
|
-
if (probe.version
|
|
1287
|
+
if (kimiReleaseHas(probe.version, "bounded-swarm")) {
|
|
981
1288
|
warnings.push(`NOTE: kimi-code ${probe.version} is certified for all operations. Swarm uses bounded waves with concurrency 2 or more, and one direct worker at concurrency 1. The finite job budget stops the whole run.`);
|
|
982
1289
|
return;
|
|
983
1290
|
}
|
|
984
1291
|
if (NATIVE_V2_NON_SWARM_VERSIONS.includes(probe.version)) {
|
|
985
|
-
warnings.push(`NOTE: kimi-code ${probe.version} is certified for review, challenge, ask, rescue, review gate, and pursue only. Read-only swarm and write swarm refuse before model spawn on this exact release. Select exact kimi-code
|
|
1292
|
+
warnings.push(`NOTE: kimi-code ${probe.version} is certified for review, challenge, ask, rescue, review gate, and pursue only. Read-only swarm and write swarm refuse before model spawn on this exact release. Select exact kimi-code ${formatVersionChoice(kimiVersionsWith("bounded-swarm"))} for this plugin's certified bounded waves and single-worker path.`);
|
|
986
1293
|
}
|
|
987
1294
|
}
|
|
988
1295
|
/**
|
|
@@ -1025,10 +1332,10 @@ async function collectInstalledKimiPluginsNotice(env, warnings) {
|
|
|
1025
1332
|
warnings.push([
|
|
1026
1333
|
`NOTE: ${enabledIds.length} kimi-code plugin(s) installed and enabled: ${enabledIds.join(", ")}.`,
|
|
1027
1334
|
" kimi-code registers their tools (including MCP) on every session. Under kimi-plugin-cc's",
|
|
1028
|
-
" read-only commands (/kimi:
|
|
1335
|
+
" read-only commands (/kimi:task, /kimi:review and the review gate) the",
|
|
1029
1336
|
" PreToolUse hook denies any tool outside Read/Grep/Glob, so calls to these plugins' tools",
|
|
1030
1337
|
" are blocked — safe, but they can waste model turns. This is expected; no action needed.",
|
|
1031
|
-
" (/kimi:
|
|
1338
|
+
" (/kimi:task --write and /kimi:swarm apply their own allowlists.) To avoid the turn-waste, disable",
|
|
1032
1339
|
" kimi-code plugins you don't need for delegated work in your kimi-code config.",
|
|
1033
1340
|
].join("\n"));
|
|
1034
1341
|
}
|
|
@@ -1097,6 +1404,7 @@ function buildDetails(args) {
|
|
|
1097
1404
|
`Companion runtime: Node ${process.version}`,
|
|
1098
1405
|
`Plugin version: ${KIMI_PLUGIN_CC_VERSION}`,
|
|
1099
1406
|
...(args.hostId ? [`Host id: ${args.hostId}`] : []),
|
|
1407
|
+
...(args.kimiBinary ? [`Kimi binary: ${args.kimiBinary}`] : []),
|
|
1100
1408
|
`Config file: ${args.configPath}`,
|
|
1101
1409
|
`Hook script: ${args.hookScriptPath}`,
|
|
1102
1410
|
`Review gate: ${args.reviewGateEnabled ? "enabled" : "disabled"}`,
|
|
@@ -1111,6 +1419,8 @@ function buildDetails(args) {
|
|
|
1111
1419
|
return details;
|
|
1112
1420
|
}
|
|
1113
1421
|
export function renderSetupResult(result) {
|
|
1422
|
+
if (result.kimiInstall)
|
|
1423
|
+
return renderInstallKimiResult(result, result.kimiInstall);
|
|
1114
1424
|
return [
|
|
1115
1425
|
result.summary,
|
|
1116
1426
|
"",
|
|
@@ -1127,3 +1437,29 @@ export function renderSetupResult(result) {
|
|
|
1127
1437
|
"Models: run setup --models to list configured aliases and provider setup guidance without making a model call.",
|
|
1128
1438
|
].join("\n");
|
|
1129
1439
|
}
|
|
1440
|
+
function renderInstallKimiResult(result, report) {
|
|
1441
|
+
if (report.json) {
|
|
1442
|
+
return JSON.stringify({
|
|
1443
|
+
action: "install-kimi",
|
|
1444
|
+
ok: report.ok,
|
|
1445
|
+
version: report.version,
|
|
1446
|
+
downloaded: report.downloaded,
|
|
1447
|
+
binary: report.binary ?? null,
|
|
1448
|
+
current: report.current ?? null,
|
|
1449
|
+
selected_binary: report.selectedBinary ?? null,
|
|
1450
|
+
selected_source: report.selectedSource ?? null,
|
|
1451
|
+
error: report.error ?? null,
|
|
1452
|
+
next_step: result.nextStep,
|
|
1453
|
+
}, null, 2);
|
|
1454
|
+
}
|
|
1455
|
+
return [
|
|
1456
|
+
result.summary,
|
|
1457
|
+
"",
|
|
1458
|
+
`Action: ${result.action}`,
|
|
1459
|
+
"",
|
|
1460
|
+
"Details:",
|
|
1461
|
+
...result.details.map((detail) => `- ${detail}`),
|
|
1462
|
+
"",
|
|
1463
|
+
`Next step: ${result.nextStep}`,
|
|
1464
|
+
].join("\n");
|
|
1465
|
+
}
|