kimi-plugin-cc 3.0.0 → 3.1.1
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 -14
- package/README.md +428 -572
- package/assets/logo.svg +6 -0
- package/cordis.patch.yml +18 -0
- package/dist/background-spawn.js +4 -2
- package/dist/cli-client.js +105 -66
- package/dist/commands/ask.js +27 -12
- package/dist/commands/cancel.js +4 -13
- package/dist/commands/cli-helpers.js +1 -1
- package/dist/commands/pursue.js +3 -2
- package/dist/commands/registry.js +7 -26
- package/dist/commands/repair-sessions.js +7 -4
- package/dist/commands/replay.js +4 -3
- package/dist/commands/rescue.js +17 -5
- package/dist/commands/review.js +18 -5
- package/dist/commands/setup.js +283 -265
- package/dist/commands/swarm.js +20 -7
- package/dist/companion.js +142 -18
- package/dist/help.js +11 -7
- package/dist/hook-escape.js +7 -16
- package/dist/hooks/approval-hook.js +1762 -124
- package/dist/hooks/approval-policy.js +15 -10
- package/dist/hooks/config-safety.js +15 -24
- package/dist/hooks/hook-timing.js +1 -1
- package/dist/hooks/install-paths.js +224 -112
- package/dist/hooks/install.js +64 -169
- package/dist/hooks/managed-block.js +52 -101
- package/dist/job-store.js +117 -49
- package/dist/jobs.js +53 -1
- package/dist/kimi-engine.js +68 -92
- package/dist/kimi-errors.js +1 -1
- package/dist/kimi-journal.js +3 -2
- package/dist/kimi-releases.js +28 -49
- package/dist/kimi-timeouts.js +1 -15
- package/dist/kimi-version-probe.js +24 -72
- package/dist/logging.js +12 -7
- package/dist/mcp/binding.js +13 -4
- package/dist/mcp/job-control.js +386 -89
- package/dist/mcp/launcher.js +1 -1
- package/dist/mcp/operations.js +387 -193
- package/dist/mcp/protocol.js +116 -27
- package/dist/mcp/skills.js +130 -0
- package/dist/mcp/stdio.js +15 -2
- package/dist/native-v2-preflight.js +127 -35
- package/dist/parsing.js +45 -11
- package/dist/paths.js +20 -10
- package/dist/plugin-data.js +28 -83
- package/dist/read-scope.js +2 -2
- package/dist/render.js +13 -38
- package/dist/session-references.js +216 -0
- package/dist/session-title.js +1 -1
- package/dist/surface-routing.js +92 -5
- package/dist/swarm-activation.js +26 -137
- package/dist/version.js +1 -1
- package/docs/migration.md +46 -227
- package/docs/models.md +24 -66
- package/docs/safety.md +195 -400
- package/docs/session-visibility.md +24 -14
- 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 +2 -3
- package/plugins/kimi-codex/dist/background-spawn.js +4 -2
- package/plugins/kimi-codex/dist/cli-client.js +105 -66
- package/plugins/kimi-codex/dist/commands/ask.js +27 -12
- package/plugins/kimi-codex/dist/commands/cancel.js +4 -13
- package/plugins/kimi-codex/dist/commands/cli-helpers.js +1 -1
- package/plugins/kimi-codex/dist/commands/pursue.js +3 -2
- package/plugins/kimi-codex/dist/commands/registry.js +7 -26
- package/plugins/kimi-codex/dist/commands/repair-sessions.js +7 -4
- package/plugins/kimi-codex/dist/commands/replay.js +4 -3
- package/plugins/kimi-codex/dist/commands/rescue.js +17 -5
- package/plugins/kimi-codex/dist/commands/review.js +18 -5
- package/plugins/kimi-codex/dist/commands/setup.js +283 -265
- package/plugins/kimi-codex/dist/commands/swarm.js +20 -7
- package/plugins/kimi-codex/dist/companion.js +142 -18
- package/plugins/kimi-codex/dist/help.js +11 -7
- package/plugins/kimi-codex/dist/hook-escape.js +7 -16
- package/plugins/kimi-codex/dist/hooks/approval-hook.js +1762 -124
- package/plugins/kimi-codex/dist/hooks/approval-policy.js +15 -10
- package/plugins/kimi-codex/dist/hooks/config-safety.js +15 -24
- package/plugins/kimi-codex/dist/hooks/hook-timing.js +1 -1
- package/plugins/kimi-codex/dist/hooks/install-paths.js +224 -112
- package/plugins/kimi-codex/dist/hooks/install.js +64 -169
- package/plugins/kimi-codex/dist/hooks/managed-block.js +52 -101
- package/plugins/kimi-codex/dist/job-store.js +117 -49
- package/plugins/kimi-codex/dist/jobs.js +53 -1
- package/plugins/kimi-codex/dist/kimi-engine.js +68 -92
- package/plugins/kimi-codex/dist/kimi-errors.js +1 -1
- package/plugins/kimi-codex/dist/kimi-journal.js +3 -2
- package/plugins/kimi-codex/dist/kimi-releases.js +28 -49
- package/plugins/kimi-codex/dist/kimi-timeouts.js +1 -15
- package/plugins/kimi-codex/dist/kimi-version-probe.js +24 -72
- 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 +386 -89
- package/plugins/kimi-codex/dist/mcp/launcher.js +1 -1
- package/plugins/kimi-codex/dist/mcp/operations.js +387 -193
- package/plugins/kimi-codex/dist/mcp/protocol.js +116 -27
- package/plugins/kimi-codex/dist/mcp/skills.js +130 -0
- package/plugins/kimi-codex/dist/mcp/stdio.js +15 -2
- package/plugins/kimi-codex/dist/native-v2-preflight.js +127 -35
- package/plugins/kimi-codex/dist/parsing.js +45 -11
- package/plugins/kimi-codex/dist/paths.js +20 -10
- package/plugins/kimi-codex/dist/plugin-data.js +28 -83
- package/plugins/kimi-codex/dist/read-scope.js +2 -2
- package/plugins/kimi-codex/dist/render.js +13 -38
- package/plugins/kimi-codex/dist/session-references.js +216 -0
- package/plugins/kimi-codex/dist/session-title.js +1 -1
- package/plugins/kimi-codex/dist/surface-routing.js +92 -5
- package/plugins/kimi-codex/dist/swarm-activation.js +26 -137
- 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 -1402
- package/agents/kimi-pursue.md +0 -93
- package/agents/kimi-review.md +0 -86
- package/agents/kimi-swarm.md +0 -88
- package/agents/kimi-task.md +0 -93
- package/commands/README.md +0 -14
- package/commands/cancel.md +0 -17
- package/commands/pursue.md +0 -41
- package/commands/replay.md +0 -13
- package/commands/result.md +0 -13
- package/commands/review.md +0 -40
- package/commands/setup.md +0 -25
- package/commands/status.md +0 -13
- package/commands/swarm.md +0 -45
- package/commands/task.md +0 -40
- package/dist/commands/review-gate.js +0 -391
- package/dist/config.js +0 -22
- package/dist/dsh/companion.js +0 -177
- package/dist/dsh/index.js +0 -167
- package/dist/hooks/review-gate-stop.js +0 -19
- package/dist/schemas/review-gate-output.js +0 -41
- 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 -95
- 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/review-gate.js +0 -391
- 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 -167
- package/plugins/kimi-codex/dist/hooks/review-gate-stop.js +0 -19
- package/plugins/kimi-codex/dist/schemas/review-gate-output.js +0 -41
- package/plugins/kimi-codex/scripts/review-gate-hook.sh +0 -32
- 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-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-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 -35
- package/plugins/kimi-codex/skills/kimi-review/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-setup/SKILL.md +0 -35
- 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 -37
- package/plugins/kimi-codex/skills/kimi-swarm/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-task/SKILL.md +0 -37
- package/plugins/kimi-codex/skills/kimi-task/agents/openai.yaml +0 -6
- package/plugins/kimi-dsh/LICENSE +0 -201
- package/plugins/kimi-dsh/README.md +0 -84
- package/plugins/kimi-dsh/cordis.patch.yml +0 -45
- package/plugins/kimi-dsh/dist/background-spawn.js +0 -209
- package/plugins/kimi-dsh/dist/cli-cancellation.js +0 -43
- package/plugins/kimi-dsh/dist/cli-client.js +0 -1097
- package/plugins/kimi-dsh/dist/commands/ask.js +0 -269
- package/plugins/kimi-dsh/dist/commands/cancel.js +0 -177
- package/plugins/kimi-dsh/dist/commands/cli-helpers.js +0 -302
- package/plugins/kimi-dsh/dist/commands/models.js +0 -139
- package/plugins/kimi-dsh/dist/commands/pursue.js +0 -293
- 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 -266
- package/plugins/kimi-dsh/dist/commands/replay.js +0 -124
- package/plugins/kimi-dsh/dist/commands/rescue.js +0 -277
- package/plugins/kimi-dsh/dist/commands/result.js +0 -72
- package/plugins/kimi-dsh/dist/commands/review-gate.js +0 -391
- package/plugins/kimi-dsh/dist/commands/review.js +0 -217
- package/plugins/kimi-dsh/dist/commands/setup.js +0 -1465
- package/plugins/kimi-dsh/dist/commands/status.js +0 -27
- package/plugins/kimi-dsh/dist/commands/swarm.js +0 -823
- package/plugins/kimi-dsh/dist/commands/worker.js +0 -24
- package/plugins/kimi-dsh/dist/companion.js +0 -99
- 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 -167
- package/plugins/kimi-dsh/dist/errors.js +0 -40
- package/plugins/kimi-dsh/dist/git.js +0 -216
- 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 -145
- package/plugins/kimi-dsh/dist/hooks/approval-policy.js +0 -295
- package/plugins/kimi-dsh/dist/hooks/config-safety.js +0 -641
- package/plugins/kimi-dsh/dist/hooks/hook-timing.js +0 -48
- package/plugins/kimi-dsh/dist/hooks/install-paths.js +0 -428
- package/plugins/kimi-dsh/dist/hooks/install.js +0 -405
- package/plugins/kimi-dsh/dist/hooks/managed-block.js +0 -729
- 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 -631
- package/plugins/kimi-dsh/dist/jobs.js +0 -212
- package/plugins/kimi-dsh/dist/kimi-command.js +0 -144
- package/plugins/kimi-dsh/dist/kimi-engine.js +0 -770
- package/plugins/kimi-dsh/dist/kimi-errors.js +0 -205
- package/plugins/kimi-dsh/dist/kimi-home.js +0 -15
- package/plugins/kimi-dsh/dist/kimi-journal.js +0 -41
- package/plugins/kimi-dsh/dist/kimi-releases.js +0 -144
- package/plugins/kimi-dsh/dist/kimi-timeouts.js +0 -52
- package/plugins/kimi-dsh/dist/kimi-version-probe.js +0 -198
- 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 -343
- package/plugins/kimi-dsh/dist/mcp/launcher.js +0 -263
- package/plugins/kimi-dsh/dist/mcp/operations.js +0 -333
- package/plugins/kimi-dsh/dist/mcp/protocol.js +0 -277
- package/plugins/kimi-dsh/dist/mcp/server.js +0 -8
- package/plugins/kimi-dsh/dist/mcp/stdio.js +0 -79
- 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 -34
- package/plugins/kimi-dsh/dist/native-v2-preflight.js +0 -596
- package/plugins/kimi-dsh/dist/package.json +0 -1
- package/plugins/kimi-dsh/dist/parsing.js +0 -554
- package/plugins/kimi-dsh/dist/paths.js +0 -35
- package/plugins/kimi-dsh/dist/plugin-data.js +0 -98
- package/plugins/kimi-dsh/dist/process-tree.js +0 -597
- package/plugins/kimi-dsh/dist/prompt-size.js +0 -18
- package/plugins/kimi-dsh/dist/read-scope.js +0 -435
- package/plugins/kimi-dsh/dist/render.js +0 -214
- package/plugins/kimi-dsh/dist/rescue-approval.js +0 -1242
- package/plugins/kimi-dsh/dist/schemas/review-gate-output.js +0 -41
- package/plugins/kimi-dsh/dist/session-recovery.js +0 -202
- package/plugins/kimi-dsh/dist/session-title.js +0 -313
- package/plugins/kimi-dsh/dist/stream-json.js +0 -344
- package/plugins/kimi-dsh/dist/surface-routing.js +0 -21
- package/plugins/kimi-dsh/dist/swarm-activation.js +0 -1194
- package/plugins/kimi-dsh/dist/swarm-policy.js +0 -74
- package/plugins/kimi-dsh/dist/swarm-recovery.js +0 -166
- 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/util.js +0 -28
- 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
|
@@ -3,35 +3,23 @@ import { SETUP_SUPPORTED_FLAGS } from "../parsing.js";
|
|
|
3
3
|
// Without it, `kimi -p` auto-approves tool calls, including from read-only
|
|
4
4
|
// commands.
|
|
5
5
|
//
|
|
6
|
-
// Setup
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
// absolute
|
|
10
|
-
// probes the hook directly with
|
|
11
|
-
// and through `/bin/sh -c` using the exact installed
|
|
12
|
-
// denies, repairs orphan marker comments, and refuses remaining
|
|
13
|
-
// duplicate, orphaned, or malformed managed blocks.
|
|
14
|
-
//
|
|
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.
|
|
19
|
-
//
|
|
20
|
-
// Managed-block splicing is line-based and preserves user bytes outside owned
|
|
21
|
-
// edits; whole-file validation and inline-hook normalization use the TOML parser.
|
|
6
|
+
// Setup atomically publishes the bundled self-contained approval hook to
|
|
7
|
+
// `<pluginRoot>/hook/approval-hook.js`, then writes or refreshes a single
|
|
8
|
+
// canonical BEGIN/END block in ~/.kimi-code/config.toml containing
|
|
9
|
+
// `'<absolute-node>' '<absolute-hook-script>'` from
|
|
10
|
+
// resolveNodeBinary/buildHookShellCommand. Setup probes the hook directly with
|
|
11
|
+
// the parsed Node binary and through `/bin/sh -c` using the exact installed bytes.
|
|
22
12
|
import { spawn } from "node:child_process";
|
|
23
|
-
import { access, mkdir, lstat, mkdtemp, open, readdir, readFile, readlink, realpath, rename, rm, symlink, unlink, writeFile, } from "node:fs/promises";
|
|
13
|
+
import { access, chmod, mkdir, lstat, mkdtemp, open, readdir, readFile, readlink, realpath, rename, rm, symlink, unlink, writeFile, } from "node:fs/promises";
|
|
24
14
|
import { constants as fsConstants, existsSync } from "node:fs";
|
|
25
15
|
import { randomBytes } from "node:crypto";
|
|
26
16
|
import path from "node:path";
|
|
27
|
-
import { readPluginConfig, writePluginConfig } from "../config.js";
|
|
28
17
|
import { RuntimeError } from "../errors.js";
|
|
29
18
|
import { HOOK_TIMEOUT_S } from "../hooks/hook-timing.js";
|
|
30
|
-
import { olderHostHookWarnings } from "../hooks/install.js";
|
|
31
19
|
import { normalizeTopLevelInlineHooks, validateKimiHookSetForEnvironment, withKimiConfigLock, } from "../hooks/config-safety.js";
|
|
32
|
-
import { buildHookShellCommand,
|
|
33
|
-
import {
|
|
34
|
-
import { formatVersionOutOfRangeWarning, probeKimiVersion, } from "../kimi-version-probe.js";
|
|
20
|
+
import { buildHookShellCommand, parseHookShellCommand, resolveBundledHookSourcePath, resolveHookScriptPath, resolveNodeBinary, verifyManagedHookArtifact, } from "../hooks/install-paths.js";
|
|
21
|
+
import { countCleanEntriesInFile, describeHookTimeout, evaluateInstalled, findShortHookTimeout, findUnmanagedApprovalHookBlocks, ORPHAN_MARKER_REMEDY, pairBlocks, parseManagedBlock, scanTomlLines, } from "../hooks/managed-block.js";
|
|
22
|
+
import { formatVersionOutOfRangeWarning, KIMI_MINIMUM_CERTIFIED_VERSION, probeKimiVersion, } from "../kimi-version-probe.js";
|
|
35
23
|
import { inspectPluginOwnedKimi, selectKimiBinary } from "../kimi-command.js";
|
|
36
24
|
import { isNativeV2CertifiedVersion, NATIVE_V2_ANY_OPERATION_VERSIONS, NATIVE_V2_CERTIFIED_VERSIONS, NATIVE_V2_NON_SWARM_VERSIONS, } from "../kimi-engine.js";
|
|
37
25
|
import { resolveKimiCodeConfigPath, resolveKimiHome } from "../kimi-home.js";
|
|
@@ -57,39 +45,21 @@ export async function runSetup(argv, context) {
|
|
|
57
45
|
}
|
|
58
46
|
const paths = resolvePluginPaths(context.env);
|
|
59
47
|
await ensurePluginPaths(paths);
|
|
60
|
-
// Reconcile the review-gate flag first — it's independent of the
|
|
61
|
-
// kimi-code hook and we want the answer reflected in the result even
|
|
62
|
-
// if the rest of setup fails.
|
|
63
|
-
const existingConfig = await readPluginConfig(paths);
|
|
64
|
-
const reviewGateEnabled = parsed.enableReviewGate
|
|
65
|
-
? true
|
|
66
|
-
: parsed.disableReviewGate
|
|
67
|
-
? false
|
|
68
|
-
: existingConfig.reviewGateEnabled;
|
|
69
|
-
if (reviewGateEnabled !== existingConfig.reviewGateEnabled) {
|
|
70
|
-
await writePluginConfig(paths, { reviewGateEnabled });
|
|
71
|
-
}
|
|
72
48
|
const configPath = resolveKimiCodeConfigPath(context.env);
|
|
73
49
|
const hookScriptPath = resolveHookScriptPath(context.env);
|
|
74
50
|
assertHookPathTomlSafe(hookScriptPath);
|
|
75
|
-
// Host id scopes each managed block so Claude Code and Codex can share one
|
|
76
|
-
// ~/.kimi-code/config.toml without clobbering each other.
|
|
77
|
-
const hostId = resolveHostId(context.env, hookScriptPath);
|
|
78
51
|
const warnings = [];
|
|
79
52
|
switch (parsed.mode) {
|
|
80
53
|
case "uninstall":
|
|
81
|
-
return await runUninstall(configPath, hookScriptPath,
|
|
54
|
+
return await runUninstall(configPath, hookScriptPath, warnings, context);
|
|
82
55
|
case "check":
|
|
83
|
-
return await runCheck(configPath, hookScriptPath,
|
|
56
|
+
return await runCheck(configPath, hookScriptPath, warnings, context);
|
|
84
57
|
case "install":
|
|
85
|
-
return await runInstall(configPath, hookScriptPath,
|
|
58
|
+
return await runInstall(paths, configPath, hookScriptPath, warnings, context);
|
|
86
59
|
}
|
|
87
60
|
}
|
|
88
61
|
function parseArgs(argv) {
|
|
89
62
|
let mode = "install";
|
|
90
|
-
let enableReviewGate = false;
|
|
91
|
-
let disableReviewGate = false;
|
|
92
|
-
let removeAllHosts = false;
|
|
93
63
|
let installKimi = false;
|
|
94
64
|
let json = false;
|
|
95
65
|
for (const token of argv) {
|
|
@@ -112,38 +82,149 @@ function parseArgs(argv) {
|
|
|
112
82
|
}
|
|
113
83
|
mode = "uninstall";
|
|
114
84
|
break;
|
|
115
|
-
case "--all":
|
|
116
|
-
removeAllHosts = true;
|
|
117
|
-
break;
|
|
118
|
-
case "--enable-review-gate":
|
|
119
|
-
enableReviewGate = true;
|
|
120
|
-
break;
|
|
121
|
-
case "--disable-review-gate":
|
|
122
|
-
disableReviewGate = true;
|
|
123
|
-
break;
|
|
124
85
|
default:
|
|
125
86
|
throw new RuntimeError("INVALID_ARGS", `Unknown setup flag ${token}. Supported flags: ${SETUP_SUPPORTED_FLAGS}.`, "setup.parse");
|
|
126
87
|
}
|
|
127
88
|
}
|
|
128
|
-
if (enableReviewGate && disableReviewGate) {
|
|
129
|
-
throw new RuntimeError("INVALID_ARGS", "setup accepts either --enable-review-gate or --disable-review-gate, not both.", "setup.parse");
|
|
130
|
-
}
|
|
131
|
-
if (removeAllHosts && mode !== "uninstall") {
|
|
132
|
-
throw new RuntimeError("INVALID_ARGS", "--all is only valid with --uninstall (it removes EVERY host's managed block).", "setup.parse");
|
|
133
|
-
}
|
|
134
89
|
if (installKimi) {
|
|
135
90
|
if (argv.some((token) => token !== "--install-kimi" && token !== "--json")) {
|
|
136
91
|
throw new RuntimeError("INVALID_ARGS", "--install-kimi does not combine with other setup flags except --json.", "setup.parse");
|
|
137
92
|
}
|
|
138
|
-
return { mode: "install-kimi", json
|
|
93
|
+
return { mode: "install-kimi", json };
|
|
139
94
|
}
|
|
140
95
|
if (json) {
|
|
141
96
|
throw new RuntimeError("INVALID_ARGS", "--json is only valid with --install-kimi or --models.", "setup.parse");
|
|
142
97
|
}
|
|
143
|
-
return { mode, json
|
|
98
|
+
return { mode, json };
|
|
144
99
|
}
|
|
145
|
-
async function
|
|
146
|
-
|
|
100
|
+
async function assertNonSymlinkDirectory(target, label) {
|
|
101
|
+
let info;
|
|
102
|
+
try {
|
|
103
|
+
info = await lstat(target);
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
throw new RuntimeError("SETUP_HOOK_PATH_UNSAFE", `Unable to inspect ${label} at ${target}: ${err.message}`, "setup.hook-script", err instanceof Error ? { cause: err, details: { target } } : { details: { target } });
|
|
107
|
+
}
|
|
108
|
+
if (info.isSymbolicLink() || !info.isDirectory()) {
|
|
109
|
+
throw new RuntimeError("SETUP_HOOK_PATH_UNSAFE", `Refusing to publish hook into ${label} at ${target}: path is a symlink or not a directory.`, "setup.hook-script", { details: { target } });
|
|
110
|
+
}
|
|
111
|
+
return await realpath(target);
|
|
112
|
+
}
|
|
113
|
+
async function assertAbsentOrRegularFile(target, label) {
|
|
114
|
+
let info;
|
|
115
|
+
try {
|
|
116
|
+
info = await lstat(target);
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
if (err.code === "ENOENT")
|
|
120
|
+
return;
|
|
121
|
+
throw new RuntimeError("SETUP_HOOK_PATH_UNSAFE", `Unable to inspect ${label} at ${target}: ${err.message}`, "setup.hook-script", err instanceof Error ? { cause: err, details: { target } } : { details: { target } });
|
|
122
|
+
}
|
|
123
|
+
if (info.isSymbolicLink() || !info.isFile()) {
|
|
124
|
+
throw new RuntimeError("SETUP_HOOK_PATH_UNSAFE", `Refusing to overwrite ${label} at ${target}: path is a symlink or not a regular file.`, "setup.hook-script", { details: { target } });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async function publishHookArtifactIfManaged(paths, hookScriptPath, env) {
|
|
128
|
+
if (env.KIMI_PLUGIN_CC_HOOK_SCRIPT && env.KIMI_PLUGIN_CC_HOOK_SCRIPT.length > 0) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const sourcePath = resolveBundledHookSourcePath();
|
|
132
|
+
let sourceBytes;
|
|
133
|
+
try {
|
|
134
|
+
sourceBytes = await readFile(sourcePath, "utf8");
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
throw new RuntimeError("SETUP_HOOK_SCRIPT_MISSING", `Bundled hook script ${sourcePath} is missing or unreadable. Run \`bun run build\` or reinstall the plugin.`, "setup.hook-script", err instanceof Error ? { cause: err, details: { sourcePath } } : { details: { sourcePath } });
|
|
138
|
+
}
|
|
139
|
+
if (sourceBytes.trim().length === 0) {
|
|
140
|
+
throw new RuntimeError("SETUP_HOOK_SCRIPT_MISSING", `Bundled hook script ${sourcePath} is empty. Run \`bun run build\` or reinstall the plugin.`, "setup.hook-script", { details: { sourcePath } });
|
|
141
|
+
}
|
|
142
|
+
// Validate pluginRoot before acquiring the hook lock or creating hookDir.
|
|
143
|
+
const pluginRootReal = await assertNonSymlinkDirectory(paths.pluginRoot, "plugin data directory");
|
|
144
|
+
await withKimiConfigLock(path.join(paths.pluginRoot, "hook"), async () => {
|
|
145
|
+
let hookDirExisted = true;
|
|
146
|
+
try {
|
|
147
|
+
const info = await lstat(paths.hookDir);
|
|
148
|
+
if (info.isSymbolicLink() || !info.isDirectory()) {
|
|
149
|
+
throw new RuntimeError("SETUP_HOOK_PATH_UNSAFE", `Refusing to publish hook into ${paths.hookDir}: path is a symlink or not a directory.`, "setup.hook-script", { details: { hookDir: paths.hookDir } });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
if (err instanceof RuntimeError)
|
|
154
|
+
throw err;
|
|
155
|
+
if (err.code === "ENOENT") {
|
|
156
|
+
hookDirExisted = false;
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
throw new RuntimeError("SETUP_HOOK_PATH_UNSAFE", `Unable to inspect hook directory ${paths.hookDir}: ${err.message}`, "setup.hook-script", err instanceof Error ? { cause: err, details: { hookDir: paths.hookDir } } : { details: { hookDir: paths.hookDir } });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (!hookDirExisted) {
|
|
163
|
+
await mkdir(paths.hookDir, { mode: 0o700 });
|
|
164
|
+
}
|
|
165
|
+
const hookDirReal = await assertNonSymlinkDirectory(paths.hookDir, "hook directory");
|
|
166
|
+
const expectedHookDirReal = path.join(pluginRootReal, "hook");
|
|
167
|
+
if (hookDirReal !== expectedHookDirReal) {
|
|
168
|
+
throw new RuntimeError("SETUP_HOOK_PATH_UNSAFE", `Hook directory ${paths.hookDir} resolves to ${hookDirReal}, outside ${expectedHookDirReal}.`, "setup.hook-script", { details: { hookDir: paths.hookDir, hookDirReal, expectedHookDirReal } });
|
|
169
|
+
}
|
|
170
|
+
if (process.platform !== "win32") {
|
|
171
|
+
await chmod(paths.hookDir, 0o700);
|
|
172
|
+
}
|
|
173
|
+
const livePackageJsonPath = path.join(paths.hookDir, "package.json");
|
|
174
|
+
await assertAbsentOrRegularFile(livePackageJsonPath, "hook package.json");
|
|
175
|
+
await assertAbsentOrRegularFile(hookScriptPath, "hook script");
|
|
176
|
+
const stagingDir = await mkdtemp(path.join(paths.hookDir, ".staging-"));
|
|
177
|
+
try {
|
|
178
|
+
if (process.platform !== "win32") {
|
|
179
|
+
await chmod(stagingDir, 0o700);
|
|
180
|
+
}
|
|
181
|
+
const stagedPackageJsonPath = path.join(stagingDir, "package.json");
|
|
182
|
+
const stagedHookScriptPath = path.join(stagingDir, "approval-hook.js");
|
|
183
|
+
await writeFileAtomic(stagedPackageJsonPath, '{"type":"module"}\n', 0o644);
|
|
184
|
+
await writeFileAtomic(stagedHookScriptPath, sourceBytes, 0o644);
|
|
185
|
+
const stagedCommand = buildHookShellCommand(stagedHookScriptPath, env);
|
|
186
|
+
const stagedProbe = await probeHook(stagedHookScriptPath, stagedCommand, env);
|
|
187
|
+
if (!stagedProbe.ok) {
|
|
188
|
+
throw new RuntimeError("SETUP_HOOK_PROBE_FAILED", `Staged hook probe failed before activation (${stagedProbe.reason}). Existing live hook and kimi-code config were left unchanged.`, "setup.hook-probe", { details: { sourcePath, hookScriptPath, reason: stagedProbe.reason } });
|
|
189
|
+
}
|
|
190
|
+
// Re-check targets immediately before atomic swap from the same directory.
|
|
191
|
+
await assertAbsentOrRegularFile(livePackageJsonPath, "hook package.json");
|
|
192
|
+
await assertAbsentOrRegularFile(hookScriptPath, "hook script");
|
|
193
|
+
await rename(stagedPackageJsonPath, livePackageJsonPath);
|
|
194
|
+
await rename(stagedHookScriptPath, hookScriptPath);
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
await rm(stagingDir, { recursive: true, force: true });
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
function assertManagedBlocksSafeToReplace(contents, blocks, configPath, stage) {
|
|
202
|
+
if (blocks.length === 0)
|
|
203
|
+
return;
|
|
204
|
+
const lines = splitPreservingEnding(contents);
|
|
205
|
+
const { tables } = scanTomlLines(contents);
|
|
206
|
+
for (const block of blocks) {
|
|
207
|
+
let hooksTables = 0;
|
|
208
|
+
for (let lineNo = block.beginLine + 1; lineNo < block.endLine; lineNo += 1) {
|
|
209
|
+
if (!tables.has(lineNo))
|
|
210
|
+
continue;
|
|
211
|
+
const trimmed = lines[lineNo].trim();
|
|
212
|
+
if (/^\[\[hooks\]\]\s*(?:#.*)?$/.test(trimmed)) {
|
|
213
|
+
hooksTables += 1;
|
|
214
|
+
if (hooksTables > 1) {
|
|
215
|
+
throw new RuntimeError("SETUP_INVALID_HOOKS_CONFIG", `kimi-code config at ${configPath} has a managed block (lines ${block.beginLine + 1}-${block.endLine + 1}) containing multiple [[hooks]] tables; refusing to overwrite or strip user content. Inspect and repair the block manually; the existing config was left unchanged.`, stage, { details: { configPath, beginLine: block.beginLine + 1, endLine: block.endLine + 1 } });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
throw new RuntimeError("SETUP_INVALID_HOOKS_CONFIG", `kimi-code config at ${configPath} has a managed block (lines ${block.beginLine + 1}-${block.endLine + 1}) containing a non-hooks TOML table at line ${lineNo + 1}; refusing to overwrite or strip user content. Inspect and repair the block manually; the existing config was left unchanged.`, stage, { details: { configPath, beginLine: block.beginLine + 1, endLine: block.endLine + 1, line: lineNo + 1 } });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async function runInstall(paths, configPath, hookScriptPath, warnings, context) {
|
|
225
|
+
if (context.env.KIMI_PLUGIN_CC_HOOK_SCRIPT && context.env.KIMI_PLUGIN_CC_HOOK_SCRIPT.length > 0) {
|
|
226
|
+
await assertHookScriptExists(hookScriptPath);
|
|
227
|
+
}
|
|
147
228
|
const mutation = await withKimiConfigLock(configPath, async () => {
|
|
148
229
|
const original = await readConfigSafe(configPath);
|
|
149
230
|
const lineEnding = detectLineEnding(original);
|
|
@@ -167,66 +248,66 @@ async function runInstall(configPath, hookScriptPath, hostId, reviewGateEnabled,
|
|
|
167
248
|
}
|
|
168
249
|
warnings.push(`Converted the top-level inline hooks array (${inlineHooks.entryCount} entr${inlineHooks.entryCount === 1 ? "y" : "ies"}) to canonical [[hooks]] tables before installing. Surrounding config bytes were preserved; formatting and comments inside that hooks assignment were normalized.`);
|
|
169
250
|
}
|
|
170
|
-
// Prune only this host's orphaned marker-less entries before computing
|
|
171
|
-
// splice line numbers. Another host's marker-less table may be its live hook
|
|
172
|
-
// after a config rewrite strips comments, so it must remain untouched.
|
|
173
251
|
const expectedCommand = buildHookShellCommand(hookScriptPath, context.env);
|
|
174
252
|
const shortBefore = findShortHookTimeout(inlineHooks.contents, expectedCommand);
|
|
175
|
-
const { pruned, count: prunedCount, commands: prunedCommands } = pruneOrphanApprovalHooks(inlineHooks.contents,
|
|
253
|
+
const { pruned, count: prunedCount, commands: prunedCommands } = pruneOrphanApprovalHooks(inlineHooks.contents, expectedCommand);
|
|
176
254
|
const existing = pruned;
|
|
177
255
|
if (prunedCount > 0) {
|
|
178
256
|
const readorned = prunedCommands.includes(expectedCommand);
|
|
179
257
|
warnings.push(readorned
|
|
180
258
|
? shortBefore === null
|
|
181
|
-
? `Re-adorned
|
|
182
|
-
: `Re-adorned
|
|
183
|
-
: `Pruned ${prunedCount} stale marker-less kimi-plugin-cc hook block(s) left by
|
|
259
|
+
? `Re-adorned the 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 ${prunedCount} bare hook table(s) with one freshly marked canonical block. Enforcement was never interrupted.`
|
|
260
|
+
: `Re-adorned the managed-block markers: replaced ${prunedCount} bare hook table(s) with one freshly marked canonical block.`
|
|
261
|
+
: `Pruned ${prunedCount} stale marker-less kimi-plugin-cc hook block(s) left by earlier installs. This is a cleanup — your active hook is unaffected.`);
|
|
184
262
|
}
|
|
185
|
-
const { state, blocks } = parseManagedBlock(existing
|
|
263
|
+
const { state, blocks } = parseManagedBlock(existing);
|
|
186
264
|
if (state.kind === "orphan") {
|
|
187
265
|
throw new RuntimeError("SETUP_ORPHAN_MARKERS", [
|
|
188
266
|
`kimi-code config at ${configPath} contains an orphaned ${state.detail} marker.`,
|
|
189
267
|
"Inspect the managed marker comments and re-run setup.",
|
|
190
268
|
].join(" "), "setup.install", { details: { configPath, orphan: state.detail } });
|
|
191
269
|
}
|
|
270
|
+
assertManagedBlocksSafeToReplace(existing, blocks, configPath, "setup.install");
|
|
271
|
+
const block = buildManagedBlock(expectedCommand, lineEnding);
|
|
272
|
+
let next;
|
|
192
273
|
if (state.kind === "duplicate") {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
.
|
|
197
|
-
|
|
198
|
-
|
|
274
|
+
const hasLegacyBlock = blocks.some((entry) => entry.hostId !== null);
|
|
275
|
+
if (!hasLegacyBlock) {
|
|
276
|
+
throw new RuntimeError("SETUP_DUPLICATE_BLOCKS", [
|
|
277
|
+
`kimi-code config at ${configPath} contains ${state.beginLines.length} kimi-plugin-cc managed blocks (lines ${state.beginLines
|
|
278
|
+
.map((line) => line + 1)
|
|
279
|
+
.join(", ")}).`,
|
|
280
|
+
"Run `scripts/companion.sh setup --uninstall` to clear them, then re-run setup.",
|
|
281
|
+
].join(" "), "setup.install", { details: { configPath, beginLines: state.beginLines } });
|
|
282
|
+
}
|
|
283
|
+
warnings.push(`Consolidated ${blocks.length} legacy host-scoped managed block(s) into one canonical block.`);
|
|
284
|
+
next = consolidateManagedBlocks(existing, blocks, block, lineEnding);
|
|
199
285
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
if (otherHosts.length > 0) {
|
|
206
|
-
warnings.push(`Other host(s) also manage a block in this shared config: ${otherHosts.join(", ")}. ` +
|
|
207
|
-
`They are left untouched — each host owns its own PreToolUse block.`);
|
|
286
|
+
else if (state.kind === "found") {
|
|
287
|
+
next = spliceBlock(existing, state.beginLine, state.endLine, block, lineEnding);
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
next = appendBlock(existing, block, lineEnding);
|
|
208
291
|
}
|
|
209
|
-
const block = buildManagedBlock(expectedCommand, lineEnding, hostId);
|
|
210
|
-
const next = state.kind === "found"
|
|
211
|
-
? spliceBlock(existing, state.beginLine, state.endLine, block, lineEnding)
|
|
212
|
-
: insertBlockNearManagedBlocks(existing, blocks, block, lineEnding);
|
|
213
292
|
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
293
|
const shortAfter = findShortHookTimeout(next, expectedCommand);
|
|
217
294
|
if (shortAfter !== null) {
|
|
218
|
-
throw new RuntimeError("SETUP_INVALID_HOOKS_CONFIG", `kimi-code config at ${configPath} has another [[hooks]] entry with
|
|
295
|
+
throw new RuntimeError("SETUP_INVALID_HOOKS_CONFIG", `kimi-code config at ${configPath} has another [[hooks]] entry with the 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 setup; the existing config was left unchanged.`, "setup.install", { details: { configPath, timeout: describeHookTimeout(shortAfter.timeout) } });
|
|
219
296
|
}
|
|
220
297
|
if (shortBefore !== null) {
|
|
221
|
-
warnings.push(`Rewrote
|
|
298
|
+
warnings.push(`Rewrote 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
299
|
}
|
|
300
|
+
// Stage, probe, and atomically publish the managed hook artifact under the
|
|
301
|
+
// config lock (and inner hook lock) before writing config.toml so a failed
|
|
302
|
+
// staged probe leaves both the live hook and config.toml untouched.
|
|
303
|
+
await publishHookArtifactIfManaged(paths, hookScriptPath, context.env);
|
|
304
|
+
await assertHookScriptExists(hookScriptPath);
|
|
223
305
|
const blockWritten = next !== original;
|
|
224
306
|
if (blockWritten)
|
|
225
307
|
await writeConfigAtomic(configPath, next);
|
|
226
308
|
return { blockWritten, next, expectedCommand };
|
|
227
309
|
});
|
|
228
310
|
const { blockWritten, next, expectedCommand } = mutation;
|
|
229
|
-
warnings.push(...await olderHostHookWarnings(next, hostId, expectedCommand));
|
|
230
311
|
collectPermissionRuleWarnings(next, warnings);
|
|
231
312
|
const kimiBinary = describeKimiBinary(context.env, warnings);
|
|
232
313
|
await collectKimiVersionWarnings(context.env, warnings);
|
|
@@ -247,22 +328,19 @@ async function runInstall(configPath, hookScriptPath, hostId, reviewGateEnabled,
|
|
|
247
328
|
probe: probe.ok ? "ok" : "failed",
|
|
248
329
|
probeError: probe.ok ? undefined : probe.reason,
|
|
249
330
|
warnings,
|
|
250
|
-
reviewGateEnabled,
|
|
251
331
|
nextStep: probe.ok
|
|
252
|
-
? "Run
|
|
253
|
-
: "Re-run
|
|
332
|
+
? "Run `kimi_read` / `kimi_write` via MCP or `scripts/companion.sh read` / `write`."
|
|
333
|
+
: "Re-run setup after installing kimi-code and Node >=22.5. If the probe keeps failing, run `scripts/companion.sh setup --uninstall` and inspect ~/.kimi-code/config.toml manually.",
|
|
254
334
|
details: buildDetails({
|
|
255
335
|
configPath,
|
|
256
336
|
hookScriptPath,
|
|
257
|
-
reviewGateEnabled,
|
|
258
337
|
probe,
|
|
259
338
|
warnings,
|
|
260
|
-
hostId,
|
|
261
339
|
kimiBinary,
|
|
262
340
|
}),
|
|
263
341
|
};
|
|
264
342
|
}
|
|
265
|
-
async function runCheck(configPath, hookScriptPath,
|
|
343
|
+
async function runCheck(configPath, hookScriptPath, warnings, context) {
|
|
266
344
|
const failedCheck = (result) => ({
|
|
267
345
|
action: "check",
|
|
268
346
|
summary: result.summary,
|
|
@@ -273,15 +351,12 @@ async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, w
|
|
|
273
351
|
probe: "failed",
|
|
274
352
|
probeError: result.reason,
|
|
275
353
|
warnings,
|
|
276
|
-
reviewGateEnabled,
|
|
277
354
|
nextStep: result.nextStep,
|
|
278
355
|
details: buildDetails({
|
|
279
356
|
configPath,
|
|
280
357
|
hookScriptPath,
|
|
281
|
-
reviewGateEnabled,
|
|
282
358
|
probe: { ok: false, reason: result.detailReason ?? result.reason },
|
|
283
359
|
warnings,
|
|
284
|
-
hostId: result.hostId,
|
|
285
360
|
kimiBinary,
|
|
286
361
|
}),
|
|
287
362
|
});
|
|
@@ -293,49 +368,18 @@ async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, w
|
|
|
293
368
|
return failedCheck({
|
|
294
369
|
summary: `kimi-code hook configuration is invalid; the configured hooks array will not load.`,
|
|
295
370
|
reason: hookSet.reason ?? "configured hooks failed validation",
|
|
296
|
-
nextStep: "Repair or remove the invalid [[hooks]] entry named above, then run
|
|
297
|
-
hostId,
|
|
371
|
+
nextStep: "Repair or remove the invalid [[hooks]] entry named above, then run setup --check again. No model call is required.",
|
|
298
372
|
});
|
|
299
373
|
}
|
|
300
374
|
const expectedCommand = buildHookShellCommand(hookScriptPath, context.env);
|
|
301
|
-
// nodeExists refines mismatch prose into drift classification; hostId checks
|
|
302
|
-
// only this host's block in the shared config.
|
|
303
375
|
const installedCheck = evaluateInstalled(existing, expectedCommand, {
|
|
304
|
-
hostId,
|
|
305
376
|
nodeExists: (binPath) => existsSync(binPath),
|
|
377
|
+
env: context.env,
|
|
306
378
|
});
|
|
307
|
-
// Non-blocking coexistence + cruft notices.
|
|
308
|
-
const { blocks: allBlocks, foreignOrphans } = parseManagedBlock(existing, hostId);
|
|
309
|
-
for (const orphan of foreignOrphans) {
|
|
310
|
-
warnings.push(`Host "${orphan.hostId}" has an orphaned ${orphan.detail} managed marker at line ${orphan.line + 1}; that host's commands refuse until it is repaired. ${ORPHAN_MARKER_REMEDY}`);
|
|
311
|
-
}
|
|
312
|
-
const otherHosts = [
|
|
313
|
-
...new Set(allBlocks.map((b) => effectiveHost(b, hostId)).filter((h) => h !== hostId)),
|
|
314
|
-
];
|
|
315
|
-
if (otherHosts.length > 0) {
|
|
316
|
-
warnings.push(`Other host(s) also manage a block in this shared config: ${otherHosts.join(", ")} (coexisting, untouched).`);
|
|
317
|
-
}
|
|
318
|
-
warnings.push(...await olderHostHookWarnings(existing, hostId, expectedCommand));
|
|
319
|
-
// Config rewrites can strip markers while leaving each host's table enforcing.
|
|
320
|
-
// Our exact-command table is live; our stale table is prunable; another host's
|
|
321
|
-
// table is reported but left strictly alone.
|
|
322
379
|
const bareTables = findUnmanagedApprovalHookBlocks(existing);
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
// Exact current command bytes identify this host even when a host-id
|
|
327
|
-
// override disagrees with the path-derived host.
|
|
328
|
-
.filter((t) => t.command !== expectedCommand)
|
|
329
|
-
.map((t) => hostIdFromHookCommand(t.command))
|
|
330
|
-
.filter((h) => h !== null && h !== hostId)),
|
|
331
|
-
];
|
|
332
|
-
if (ownStale.length > 0) {
|
|
333
|
-
warnings.push(`Found ${ownStale.length} stale marker-less kimi-plugin-cc hook block(s) from this host's earlier installs. ` +
|
|
334
|
-
`Run /kimi:setup to refresh, or /kimi:setup --uninstall --all to clear everything.`);
|
|
335
|
-
}
|
|
336
|
-
for (const host of foreignHosts) {
|
|
337
|
-
warnings.push(`Host "${host}" has a marker-less kimi-plugin-cc hook block (kimi-code config rewrites strip comments). ` +
|
|
338
|
-
`It still enforces and is left untouched; run that host's setup to re-adorn its markers.`);
|
|
380
|
+
const staleBare = bareTables.filter((t) => t.command !== expectedCommand);
|
|
381
|
+
if (staleBare.length > 0) {
|
|
382
|
+
warnings.push(`Found ${staleBare.length} stale marker-less kimi-plugin-cc hook block(s) from earlier installs. Run setup to clean them up.`);
|
|
339
383
|
}
|
|
340
384
|
if (installedCheck.installed && installedCheck.note !== undefined) {
|
|
341
385
|
warnings.push(`Note: ${installedCheck.note}`);
|
|
@@ -350,19 +394,40 @@ async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, w
|
|
|
350
394
|
reason: installedCheck.reason ?? "managed block missing or invalid",
|
|
351
395
|
detailReason: installedCheck.reason ?? "block missing/invalid",
|
|
352
396
|
nextStep: installedCheck.timeoutTooShort === true
|
|
353
|
-
? "Run
|
|
354
|
-
: installedCheck.
|
|
355
|
-
? "
|
|
356
|
-
: installedCheck.state.kind === "
|
|
357
|
-
?
|
|
358
|
-
:
|
|
397
|
+
? "Run setup (without --check) to rewrite the hook timeout."
|
|
398
|
+
: installedCheck.drift?.dataOverrideFromCanonical === true
|
|
399
|
+
? "Unset KIMI_PLUGIN_CC_DATA in your host environment (for example in ~/.claude/settings.json), restart the host, and run setup --check again."
|
|
400
|
+
: installedCheck.state.kind === "absent"
|
|
401
|
+
? "Run setup (without --check) to install the managed block."
|
|
402
|
+
: installedCheck.state.kind === "orphan"
|
|
403
|
+
? ORPHAN_MARKER_REMEDY
|
|
404
|
+
: installedCheck.state.kind === "duplicate" &&
|
|
405
|
+
parseManagedBlock(existing).blocks.some((entry) => entry.hostId !== null)
|
|
406
|
+
? 'Run `kimi_setup` (action "install") or `scripts/companion.sh setup` to consolidate legacy host-scoped blocks into one canonical block.'
|
|
407
|
+
: "Run setup --uninstall, then setup to repair.",
|
|
359
408
|
});
|
|
360
409
|
}
|
|
361
410
|
collectPermissionRuleWarnings(existing, warnings);
|
|
362
411
|
await collectKimiVersionWarnings(context.env, warnings);
|
|
363
412
|
await collectInstalledKimiPluginsNotice(context.env, warnings);
|
|
364
|
-
|
|
365
|
-
|
|
413
|
+
const artifact = await verifyManagedHookArtifact(context.env, hookScriptPath);
|
|
414
|
+
if (!artifact.ok) {
|
|
415
|
+
return failedCheck({
|
|
416
|
+
summary: artifact.kind === "sha256-mismatch"
|
|
417
|
+
? `Managed hook script ${hookScriptPath} does not match the bundled hook artifact (SHA-256 mismatch).`
|
|
418
|
+
: artifact.kind === "unsafe-path"
|
|
419
|
+
? `Managed hook path ${hookScriptPath} failed safety checks.`
|
|
420
|
+
: artifact.kind === "bundle-missing"
|
|
421
|
+
? `Bundled hook artifact is missing or unreadable.`
|
|
422
|
+
: `Managed block references ${hookScriptPath} but that path is unreadable.`,
|
|
423
|
+
reason: artifact.reason,
|
|
424
|
+
nextStep: artifact.kind === "unsafe-path"
|
|
425
|
+
? "Remove or replace the unsafe symlink/non-regular path under the plugin hook directory, then re-run setup."
|
|
426
|
+
: artifact.kind === "bundle-missing"
|
|
427
|
+
? "Run `bun run build` or reinstall the plugin, then re-run setup."
|
|
428
|
+
: "Run setup (without --check) to republish the hook script and refresh the managed block.",
|
|
429
|
+
});
|
|
430
|
+
}
|
|
366
431
|
try {
|
|
367
432
|
await assertHookScriptExists(hookScriptPath);
|
|
368
433
|
}
|
|
@@ -370,7 +435,7 @@ async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, w
|
|
|
370
435
|
return failedCheck({
|
|
371
436
|
summary: `Managed block references ${hookScriptPath} but that path is unreadable.`,
|
|
372
437
|
reason: err instanceof Error ? err.message : String(err),
|
|
373
|
-
nextStep: "
|
|
438
|
+
nextStep: "Run setup (without --check) to republish the hook script and refresh the managed block.",
|
|
374
439
|
});
|
|
375
440
|
}
|
|
376
441
|
const probe = await probeHook(hookScriptPath, expectedCommand, context.env);
|
|
@@ -386,26 +451,21 @@ async function runCheck(configPath, hookScriptPath, hostId, reviewGateEnabled, w
|
|
|
386
451
|
probe: probe.ok ? "ok" : "failed",
|
|
387
452
|
probeError: probe.ok ? undefined : probe.reason,
|
|
388
453
|
warnings,
|
|
389
|
-
reviewGateEnabled,
|
|
390
454
|
nextStep: !probe.ok
|
|
391
|
-
? "Run
|
|
455
|
+
? "Run setup to repair the managed block, or setup --uninstall to remove the integration."
|
|
392
456
|
: installedCheck.via === "bare-table"
|
|
393
|
-
|
|
394
|
-
// re-adorning rather than claiming no action is needed.
|
|
395
|
-
? "Enforcement is active. Run /kimi:setup to re-adorn this host's managed-block markers — kimi-code stripped them on its last config write (comments are not preserved). Your hook keeps working meanwhile."
|
|
457
|
+
? "Enforcement is active. Run setup to re-adorn the managed-block markers — kimi-code stripped comments on its last config write. Your hook keeps working meanwhile."
|
|
396
458
|
: "No action needed.",
|
|
397
459
|
details: buildDetails({
|
|
398
460
|
configPath,
|
|
399
461
|
hookScriptPath,
|
|
400
|
-
reviewGateEnabled,
|
|
401
462
|
probe,
|
|
402
463
|
warnings,
|
|
403
|
-
hostId,
|
|
404
464
|
kimiBinary,
|
|
405
465
|
}),
|
|
406
466
|
};
|
|
407
467
|
}
|
|
408
|
-
async function runUninstall(configPath, hookScriptPath,
|
|
468
|
+
async function runUninstall(configPath, hookScriptPath, warnings, context) {
|
|
409
469
|
return await withKimiConfigLock(configPath, async () => {
|
|
410
470
|
const existing = await readConfigSafe(configPath);
|
|
411
471
|
if (existing.length === 0) {
|
|
@@ -418,29 +478,38 @@ async function runUninstall(configPath, hookScriptPath, hostId, removeAllHosts,
|
|
|
418
478
|
blockRemoved: false,
|
|
419
479
|
probe: "skipped",
|
|
420
480
|
warnings,
|
|
421
|
-
|
|
422
|
-
nextStep: "Run /kimi:setup to install the managed block again.",
|
|
481
|
+
nextStep: "Run setup to install the managed block again.",
|
|
423
482
|
details: buildDetails({
|
|
424
483
|
configPath,
|
|
425
484
|
hookScriptPath,
|
|
426
|
-
reviewGateEnabled,
|
|
427
485
|
probe: { ok: true, reason: "no-op (config absent)" },
|
|
428
486
|
warnings,
|
|
429
487
|
}),
|
|
430
488
|
};
|
|
431
489
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
490
|
+
const lineEnding = detectLineEnding(existing);
|
|
491
|
+
const inlineHooks = normalizeTopLevelInlineHooks(existing, lineEnding);
|
|
492
|
+
if (inlineHooks.found && !inlineHooks.converted) {
|
|
493
|
+
throw new RuntimeError("SETUP_INVALID_HOOKS_CONFIG", `Could not safely convert the top-level inline hooks assignment in ${configPath} during uninstall; the existing config was left unchanged.`, "setup.uninstall", { details: { configPath } });
|
|
494
|
+
}
|
|
495
|
+
const working = inlineHooks.contents;
|
|
496
|
+
const { blocks } = pairBlocks(splitPreservingEnding(working));
|
|
497
|
+
assertManagedBlocksSafeToReplace(working, blocks, configPath, "setup.uninstall");
|
|
498
|
+
const { stripped: strippedMarkers, removedBlocks, orphansLeft } = stripManagedBlocks(working);
|
|
439
499
|
const expectedCommand = buildHookShellCommand(hookScriptPath, context.env);
|
|
440
|
-
const { pruned: stripped, count: prunedCount } = pruneOrphanApprovalHooks(strippedMarkers,
|
|
441
|
-
const
|
|
500
|
+
const { pruned: stripped, count: prunedCount } = pruneOrphanApprovalHooks(strippedMarkers, expectedCommand);
|
|
501
|
+
const removedAnything = removedBlocks > 0 || orphansLeft.length > 0 || prunedCount > 0;
|
|
502
|
+
const finalContents = removedAnything ? stripped : existing;
|
|
503
|
+
if (evaluateInstalled(finalContents, expectedCommand).installed ||
|
|
504
|
+
countCleanEntriesInFile(finalContents, expectedCommand) > 0) {
|
|
505
|
+
throw new RuntimeError("SETUP_UNINSTALL_INCOMPLETE", `Could not safely remove all kimi-plugin-cc hook entries from ${configPath}; an active hook entry for this plugin still remains. Inspect ${configPath} manually; the existing config was left unchanged.`, "setup.uninstall", { details: { configPath } });
|
|
506
|
+
}
|
|
507
|
+
const changed = removedAnything && finalContents !== existing;
|
|
442
508
|
if (changed) {
|
|
443
|
-
|
|
509
|
+
if (inlineHooks.found) {
|
|
510
|
+
warnings.push(`Converted the top-level inline hooks array (${inlineHooks.entryCount} entr${inlineHooks.entryCount === 1 ? "y" : "ies"}) to canonical [[hooks]] tables while removing kimi-plugin-cc entries and preserving foreign hooks.`);
|
|
511
|
+
}
|
|
512
|
+
await writeConfigAtomic(configPath, finalContents);
|
|
444
513
|
}
|
|
445
514
|
if (orphansLeft.length > 0) {
|
|
446
515
|
warnings.push(`Detected ${orphansLeft.length} orphan marker line(s) at ${orphansLeft
|
|
@@ -450,14 +519,6 @@ async function runUninstall(configPath, hookScriptPath, hostId, removeAllHosts,
|
|
|
450
519
|
if (prunedCount > 0) {
|
|
451
520
|
warnings.push(`Pruned ${prunedCount} orphaned kimi-plugin-cc hook block(s) with no managed marker.`);
|
|
452
521
|
}
|
|
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
|
-
}
|
|
461
522
|
return {
|
|
462
523
|
action: "uninstall",
|
|
463
524
|
summary: changed
|
|
@@ -469,14 +530,12 @@ async function runUninstall(configPath, hookScriptPath, hostId, removeAllHosts,
|
|
|
469
530
|
blockRemoved: changed,
|
|
470
531
|
probe: "skipped",
|
|
471
532
|
warnings,
|
|
472
|
-
reviewGateEnabled,
|
|
473
533
|
nextStep: changed
|
|
474
|
-
? "Run
|
|
475
|
-
: "Run
|
|
534
|
+
? "Run setup again to reinstall the hook, or leave the plugin uninstalled."
|
|
535
|
+
: "Run setup to install the hook.",
|
|
476
536
|
details: buildDetails({
|
|
477
537
|
configPath,
|
|
478
538
|
hookScriptPath,
|
|
479
|
-
reviewGateEnabled,
|
|
480
539
|
probe: { ok: true, reason: changed ? `removed ${removedBlocks} block(s)` : "no-op (no markers)" },
|
|
481
540
|
warnings,
|
|
482
541
|
}),
|
|
@@ -494,39 +553,28 @@ async function readConfigSafe(configPath) {
|
|
|
494
553
|
throw new RuntimeError("SETUP_CONFIG_READ_FAILED", `Failed to read kimi-code config at ${configPath}: ${err.message}`, "setup.read-config", err instanceof Error ? { cause: err, details: { configPath } } : { details: { configPath } });
|
|
495
554
|
}
|
|
496
555
|
}
|
|
497
|
-
async function
|
|
498
|
-
|
|
499
|
-
// concurrent operations from sharing an intermediate path.
|
|
500
|
-
//
|
|
501
|
-
// Pass 0o600 at creation, not in a later chmod: the config can contain
|
|
502
|
-
// credentials, so the temp file must never exist with a wider umask-derived
|
|
503
|
-
// mode.
|
|
504
|
-
await mkdir(path.dirname(configPath), { recursive: true });
|
|
556
|
+
async function writeFileAtomic(targetPath, contents, mode) {
|
|
557
|
+
await mkdir(path.dirname(targetPath), { recursive: true });
|
|
505
558
|
const suffix = randomBytes(8).toString("hex");
|
|
506
|
-
const tmpPath = `${
|
|
559
|
+
const tmpPath = `${targetPath}.kimi-plugin-cc.${process.pid}.${suffix}.tmp`;
|
|
507
560
|
const openFlags = fsConstants.O_WRONLY |
|
|
508
561
|
fsConstants.O_CREAT |
|
|
509
562
|
fsConstants.O_EXCL |
|
|
510
563
|
(fsConstants.O_NOFOLLOW ?? 0);
|
|
511
564
|
try {
|
|
512
|
-
const handle = await open(tmpPath, openFlags,
|
|
565
|
+
const handle = await open(tmpPath, openFlags, mode);
|
|
513
566
|
try {
|
|
514
567
|
if (process.platform !== "win32") {
|
|
515
|
-
|
|
516
|
-
// platform-specific noise. The threat model excludes Windows.
|
|
517
|
-
// Belt-and-suspenders alongside the create-time mode above,
|
|
518
|
-
// matching createPrivateLockFile's pattern.
|
|
519
|
-
await handle.chmod(0o600);
|
|
568
|
+
await handle.chmod(mode);
|
|
520
569
|
}
|
|
521
570
|
await handle.writeFile(contents, "utf8");
|
|
522
571
|
}
|
|
523
572
|
finally {
|
|
524
573
|
await handle.close();
|
|
525
574
|
}
|
|
526
|
-
await rename(tmpPath,
|
|
575
|
+
await rename(tmpPath, targetPath);
|
|
527
576
|
}
|
|
528
577
|
catch (err) {
|
|
529
|
-
// Best-effort cleanup so stale tmp files don't accumulate.
|
|
530
578
|
try {
|
|
531
579
|
await unlink(tmpPath);
|
|
532
580
|
}
|
|
@@ -536,11 +584,15 @@ async function writeConfigAtomic(configPath, contents) {
|
|
|
536
584
|
throw err;
|
|
537
585
|
}
|
|
538
586
|
}
|
|
587
|
+
async function writeConfigAtomic(configPath, contents) {
|
|
588
|
+
// Pass 0o600 at creation so credentials in config.toml never exist with wider mode.
|
|
589
|
+
await writeFileAtomic(configPath, contents, 0o600);
|
|
590
|
+
}
|
|
539
591
|
async function assertKimiHookSetValid(contents, configPath, stage, env) {
|
|
540
592
|
const validation = await validateKimiHookSetForEnvironment(contents, env);
|
|
541
593
|
if (validation.valid)
|
|
542
594
|
return;
|
|
543
|
-
throw new RuntimeError("SETUP_INVALID_HOOKS_CONFIG", `${validation.reason ?? "Configured hooks failed validation"} Repair or remove that [[hooks]] entry before rerunning
|
|
595
|
+
throw new RuntimeError("SETUP_INVALID_HOOKS_CONFIG", `${validation.reason ?? "Configured hooks failed validation"} Repair or remove that [[hooks]] entry before rerunning setup; the existing config was left unchanged.`, stage, {
|
|
544
596
|
details: {
|
|
545
597
|
configPath,
|
|
546
598
|
hookEntry: validation.entry,
|
|
@@ -631,44 +683,28 @@ function removeOrphanMarkers(contents) {
|
|
|
631
683
|
? { contents, count: 0 }
|
|
632
684
|
: { contents: removeLines(contents, remove), count: remove.size };
|
|
633
685
|
}
|
|
686
|
+
function consolidateManagedBlocks(contents, blocks, replacement, lineEnding) {
|
|
687
|
+
const [first, ...rest] = blocks;
|
|
688
|
+
const spliced = spliceBlock(contents, first.beginLine, first.endLine, replacement, lineEnding);
|
|
689
|
+
const deltaLines = splitPreservingEnding(replacement).length - (first.endLine - first.beginLine + 1);
|
|
690
|
+
const remove = new Set();
|
|
691
|
+
for (const extra of rest) {
|
|
692
|
+
for (let i = extra.beginLine + deltaLines; i <= extra.endLine + deltaLines; i += 1) {
|
|
693
|
+
remove.add(i);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return removeLines(spliced, remove);
|
|
697
|
+
}
|
|
634
698
|
/**
|
|
635
|
-
* Remove managed blocks
|
|
636
|
-
* Ownership comes from the marker suffix or a legacy command's derived host.
|
|
637
|
-
* Orphan markers are removed individually so surrounding user content survives.
|
|
699
|
+
* Remove all managed blocks and orphan markers while preserving surrounding content.
|
|
638
700
|
*/
|
|
639
|
-
function stripManagedBlocks(contents
|
|
701
|
+
function stripManagedBlocks(contents) {
|
|
640
702
|
const lines = splitPreservingEnding(contents);
|
|
641
703
|
const { blocks, orphans } = pairBlocks(lines);
|
|
642
|
-
|
|
643
|
-
// use their command-derived host; an unattributable non-canonical command is
|
|
644
|
-
// ambiguous and only --all removes it. Orphan marker lines have no command
|
|
645
|
-
// and can be swept individually by a scoped uninstall.
|
|
646
|
-
const shouldRemove = (markerHost, commandPath, isOrphanMarker) => {
|
|
647
|
-
if (removeAll)
|
|
648
|
-
return true;
|
|
649
|
-
if (markerHost !== null)
|
|
650
|
-
return markerHost === currentHost;
|
|
651
|
-
if (isOrphanMarker)
|
|
652
|
-
return true;
|
|
653
|
-
const derived = commandPath !== null ? hostIdFromHookCommand(commandPath) : null;
|
|
654
|
-
if (derived === null)
|
|
655
|
-
return false;
|
|
656
|
-
return derived === currentHost;
|
|
657
|
-
};
|
|
658
|
-
const orphansLeft = orphans
|
|
659
|
-
.filter((orphan) => shouldRemove(orphan.hostId, null, true))
|
|
660
|
-
.map((orphan) => orphan.line);
|
|
704
|
+
const orphansLeft = orphans.map((orphan) => orphan.line);
|
|
661
705
|
const remove = new Set(orphansLeft);
|
|
662
706
|
let removedBlocks = 0;
|
|
663
707
|
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;
|
|
669
|
-
}
|
|
670
|
-
if (!shouldRemove(block.hostId, blockCommand, false))
|
|
671
|
-
continue;
|
|
672
708
|
removedBlocks += 1;
|
|
673
709
|
for (let i = block.beginLine; i <= block.endLine; i += 1)
|
|
674
710
|
remove.add(i);
|
|
@@ -679,20 +715,12 @@ function removeLines(contents, remove) {
|
|
|
679
715
|
return contents.split(/(?<=\n)/).filter((_, index) => !remove.has(index)).join("");
|
|
680
716
|
}
|
|
681
717
|
/**
|
|
682
|
-
* Remove orphaned, marker-less `[[hooks]]` tables that
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
* tables (so install can distinguish "re-adorning this host's live hook after
|
|
686
|
-
* a kimi-code comment strip" from "sweeping stale cruft"). A no-op (returns
|
|
687
|
-
* the input unchanged) when there is nothing to prune, so callers can compare
|
|
688
|
-
* identity cheaply.
|
|
689
|
-
*
|
|
690
|
-
* `ownedBy` restricts the prune to tables whose command path derives to that
|
|
691
|
-
* host — REQUIRED for scoped install/uninstall so one host never disarms the
|
|
692
|
-
* other's live (marker-stripped) hook. Omit only for `--uninstall --all`.
|
|
718
|
+
* Remove orphaned, marker-less `[[hooks]]` tables that belong to kimi-plugin-cc
|
|
719
|
+
* (see `findUnmanagedApprovalHookBlocks`). Returns the cleaned text, the count
|
|
720
|
+
* removed, and decoded commands so install can distinguish re-adorning from cleanup.
|
|
693
721
|
*/
|
|
694
|
-
function pruneOrphanApprovalHooks(contents,
|
|
695
|
-
const ranges = findUnmanagedApprovalHookBlocks(contents,
|
|
722
|
+
function pruneOrphanApprovalHooks(contents, alsoMatchCommand) {
|
|
723
|
+
const ranges = findUnmanagedApprovalHookBlocks(contents, alsoMatchCommand);
|
|
696
724
|
if (ranges.length === 0)
|
|
697
725
|
return { pruned: contents, count: 0, commands: [] };
|
|
698
726
|
const remove = new Set();
|
|
@@ -709,15 +737,11 @@ function pruneOrphanApprovalHooks(contents, ownedBy, alsoMatchCommand) {
|
|
|
709
737
|
};
|
|
710
738
|
}
|
|
711
739
|
// ----- Block content -----------------------------------------------------
|
|
712
|
-
function buildManagedBlock(shellCommand, lineEnding = "\n"
|
|
740
|
+
function buildManagedBlock(shellCommand, lineEnding = "\n") {
|
|
713
741
|
const commandLine = `command = ${tomlBasicString(shellCommand)}`;
|
|
714
|
-
const suffix = hostId.length > 0 ? `:${hostId}` : "";
|
|
715
742
|
return [
|
|
716
|
-
`${BEGIN_MARKER_PREFIX}
|
|
717
|
-
`# DO NOT EDIT — managed by
|
|
718
|
-
`# Host: ${hostId.length > 0 ? hostId : "(legacy)"} — each host owns a separate block in this shared`,
|
|
719
|
-
`# ~/.kimi-code/config.toml. Scoped setup/uninstall preserves other hosts;`,
|
|
720
|
-
`# /kimi:setup --uninstall --all removes every host's block.`,
|
|
743
|
+
`${BEGIN_MARKER_PREFIX} (v${KIMI_PLUGIN_CC_VERSION}) ===`,
|
|
744
|
+
`# DO NOT EDIT — managed by kimi-plugin-cc setup. Run \`scripts/companion.sh setup --uninstall\` to remove.`,
|
|
721
745
|
`# Purpose:`,
|
|
722
746
|
`# kimi-code's \`kimi -p\` mode hard-codes permission='auto' and`,
|
|
723
747
|
`# auto-approves every tool call. This hook enforces each managed command's`,
|
|
@@ -734,7 +758,7 @@ function buildManagedBlock(shellCommand, lineEnding = "\n", hostId = "") {
|
|
|
734
758
|
`event = "PreToolUse"`,
|
|
735
759
|
commandLine,
|
|
736
760
|
`timeout = ${DEFAULT_HOOK_TIMEOUT_S}`,
|
|
737
|
-
`# === END kimi-plugin-cc-managed
|
|
761
|
+
`# === END kimi-plugin-cc-managed ===`,
|
|
738
762
|
].join(lineEnding);
|
|
739
763
|
}
|
|
740
764
|
/**
|
|
@@ -957,8 +981,8 @@ async function runInstallKimi(json, context) {
|
|
|
957
981
|
`Jobs: ${jobsLine}`,
|
|
958
982
|
],
|
|
959
983
|
nextStep: jobsUseIt
|
|
960
|
-
? "Run
|
|
961
|
-
: "Unset KIMI_PLUGIN_CC_KIMI_BIN to use the plugin-owned binary, then run
|
|
984
|
+
? "Run setup --check to confirm the hook and the selected binary."
|
|
985
|
+
: "Unset KIMI_PLUGIN_CC_KIMI_BIN to use the plugin-owned binary, then run setup --check.",
|
|
962
986
|
report: {
|
|
963
987
|
ok: true,
|
|
964
988
|
json,
|
|
@@ -1003,7 +1027,6 @@ function installKimiResult(args) {
|
|
|
1003
1027
|
probe: args.failed ? "failed" : "skipped",
|
|
1004
1028
|
probeError: args.probeError,
|
|
1005
1029
|
warnings: [],
|
|
1006
|
-
reviewGateEnabled: false,
|
|
1007
1030
|
nextStep: args.nextStep,
|
|
1008
1031
|
details: args.details,
|
|
1009
1032
|
kimiInstall: args.report,
|
|
@@ -1275,13 +1298,11 @@ async function collectKimiVersionWarnings(env, warnings) {
|
|
|
1275
1298
|
env,
|
|
1276
1299
|
});
|
|
1277
1300
|
if (probe.kind === "failed") {
|
|
1278
|
-
|
|
1279
|
-
// missing/broken, the hook probe path will catch it with a much
|
|
1280
|
-
// clearer message. Avoid double-noise.
|
|
1301
|
+
warnings.push(`WARNING: could not verify the selected kimi-code binary version (${probe.reason}). Model-spawning operations will refuse until a working kimi-code >= ${KIMI_MINIMUM_CERTIFIED_VERSION} binary is available; run \`kimi_setup\` (action "install_kimi") or \`scripts/companion.sh setup --install-kimi\`, or check KIMI_PLUGIN_CC_KIMI_BIN.`);
|
|
1281
1302
|
return;
|
|
1282
1303
|
}
|
|
1283
|
-
if (!
|
|
1284
|
-
warnings.push(formatVersionOutOfRangeWarning(probe, KIMI_PLUGIN_CC_VERSION, NATIVE_V2_ANY_OPERATION_VERSIONS));
|
|
1304
|
+
if (!isNativeV2CertifiedVersion(probe.version)) {
|
|
1305
|
+
warnings.push(formatVersionOutOfRangeWarning(probe, KIMI_PLUGIN_CC_VERSION, NATIVE_V2_ANY_OPERATION_VERSIONS, env));
|
|
1285
1306
|
return;
|
|
1286
1307
|
}
|
|
1287
1308
|
if (kimiReleaseHas(probe.version, "bounded-swarm")) {
|
|
@@ -1296,7 +1317,7 @@ async function collectKimiVersionWarnings(env, warnings) {
|
|
|
1296
1317
|
* Non-blocking notice for kimi-code's own enabled plugins from
|
|
1297
1318
|
* ~/.kimi-code/plugins/installed.json. Their tools can register on every
|
|
1298
1319
|
* session, but read-only commands deny everything outside READ_ONLY_TOOLS
|
|
1299
|
-
* (Read, Grep, Glob, ReadMediaFile, TaskList, TaskOutput), which can waste model
|
|
1320
|
+
* (Read, Grep, Glob, ReadMediaFile, TaskList, TaskOutput, WaitFor), which can waste model
|
|
1300
1321
|
* turns. Missing, unreadable, or malformed manifests produce no notice; setup
|
|
1301
1322
|
* never blocks on or mutates the plugin list.
|
|
1302
1323
|
*/
|
|
@@ -1332,10 +1353,10 @@ async function collectInstalledKimiPluginsNotice(env, warnings) {
|
|
|
1332
1353
|
warnings.push([
|
|
1333
1354
|
`NOTE: ${enabledIds.length} kimi-code plugin(s) installed and enabled: ${enabledIds.join(", ")}.`,
|
|
1334
1355
|
" kimi-code registers their tools (including MCP) on every session. Under kimi-plugin-cc's",
|
|
1335
|
-
" read-only
|
|
1336
|
-
" PreToolUse hook denies
|
|
1356
|
+
" read-only operations (kimi_read and companion.sh read) the",
|
|
1357
|
+
" PreToolUse hook denies every tool that is not on its read-only list, so calls to these plugins' tools",
|
|
1337
1358
|
" are blocked — safe, but they can waste model turns. This is expected; no action needed.",
|
|
1338
|
-
" (
|
|
1359
|
+
" (kimi_write, companion.sh write, and swarm modes apply their own allowlists.) To avoid the turn-waste, disable",
|
|
1339
1360
|
" kimi-code plugins you don't need for delegated work in your kimi-code config.",
|
|
1340
1361
|
].join("\n"));
|
|
1341
1362
|
}
|
|
@@ -1403,11 +1424,9 @@ function buildDetails(args) {
|
|
|
1403
1424
|
const details = [
|
|
1404
1425
|
`Companion runtime: Node ${process.version}`,
|
|
1405
1426
|
`Plugin version: ${KIMI_PLUGIN_CC_VERSION}`,
|
|
1406
|
-
...(args.hostId ? [`Host id: ${args.hostId}`] : []),
|
|
1407
1427
|
...(args.kimiBinary ? [`Kimi binary: ${args.kimiBinary}`] : []),
|
|
1408
1428
|
`Config file: ${args.configPath}`,
|
|
1409
1429
|
`Hook script: ${args.hookScriptPath}`,
|
|
1410
|
-
`Review gate: ${args.reviewGateEnabled ? "enabled" : "disabled"}`,
|
|
1411
1430
|
`Probe: ${args.probe.ok ? "ok" : "failed"} — ${args.probe.reason}`,
|
|
1412
1431
|
];
|
|
1413
1432
|
if (args.warnings.length > 0) {
|
|
@@ -1428,7 +1447,6 @@ export function renderSetupResult(result) {
|
|
|
1428
1447
|
`Block written: ${result.blockWritten ? "yes" : "no"}`,
|
|
1429
1448
|
`Block removed: ${result.blockRemoved ? "yes" : "no"}`,
|
|
1430
1449
|
`Probe: ${result.probe}${result.probeError ? ` (${result.probeError})` : ""}`,
|
|
1431
|
-
`Review gate: ${result.reviewGateEnabled ? "enabled" : "disabled"}`,
|
|
1432
1450
|
"",
|
|
1433
1451
|
"Details:",
|
|
1434
1452
|
...result.details.map((detail) => `- ${detail}`),
|