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
|
@@ -5,7 +5,11 @@ import { BOUNDED_SWARM_POLICY, evaluateSwarmWaveInput } from "../swarm-policy.js
|
|
|
5
5
|
*
|
|
6
6
|
* Includes kimi-code's own local-read defaults (`Read`, `Grep`, `Glob`)
|
|
7
7
|
* plus a few additional read-safe tools the runtime auto-allows
|
|
8
|
-
* (`ReadMediaFile`, `TaskList`, `TaskOutput`).
|
|
8
|
+
* (`ReadMediaFile`, `TaskList`, `TaskOutput`, `WaitFor`). `WaitFor` waits,
|
|
9
|
+
* up to a schema cap, on background tasks that already exist and can neither
|
|
10
|
+
* start nor stop one; kimi-code's goal guidance tells the model to call it,
|
|
11
|
+
* so denying it cost pursue a wasted call. Only the write labels can have a
|
|
12
|
+
* background task to wait on (docs/invariants.md). Network tools
|
|
9
13
|
* (`WebSearch`, `FetchURL`) are excluded; read-only labels allow them only
|
|
10
14
|
* for a call started with --network, which also confines these reads to the
|
|
11
15
|
* plugin-exported read roots (runtime/read-scope.ts).
|
|
@@ -17,6 +21,7 @@ export const READ_ONLY_TOOLS = new Set([
|
|
|
17
21
|
"ReadMediaFile",
|
|
18
22
|
"TaskList",
|
|
19
23
|
"TaskOutput",
|
|
24
|
+
"WaitFor",
|
|
20
25
|
]);
|
|
21
26
|
/** Web tools: denied unless the plugin exported KIMI_PLUGIN_CC_NETWORK=1 with read roots. */
|
|
22
27
|
const WEB_TOOLS = new Set(["WebSearch", "FetchURL"]);
|
|
@@ -30,13 +35,15 @@ const WEB_TOOLS = new Set(["WebSearch", "FetchURL"]);
|
|
|
30
35
|
*/
|
|
31
36
|
const AGENT_SWARM_TOOL = "AgentSwarm";
|
|
32
37
|
/**
|
|
33
|
-
* agent-core-v2's ONLY chain-breaking final allow is the
|
|
34
|
-
* it fires only while plan mode is ACTIVE. Native v2 is
|
|
35
|
-
* the invariant that plan mode is never armed in a
|
|
36
|
-
* `default_plan_mode` is refused pre-spawn
|
|
37
|
-
* plan-tainted sessions are never resumed,
|
|
38
|
-
* is denied HERE for every label.
|
|
39
|
-
* is explicit rather than a side
|
|
38
|
+
* Through 2.7.3, agent-core-v2's ONLY chain-breaking final allow is the
|
|
39
|
+
* plan-file guard, and it fires only while plan mode is ACTIVE. Native v2 is
|
|
40
|
+
* therefore certified on the invariant that plan mode is never armed in a
|
|
41
|
+
* plugin-managed session: `default_plan_mode` is refused pre-spawn
|
|
42
|
+
* (runtime/native-v2-preflight.ts), plan-tainted sessions are never resumed,
|
|
43
|
+
* and the model-reachable entry tool is denied HERE for every label.
|
|
44
|
+
* ExitPlanMode is denied too so the deny set is explicit rather than a side
|
|
45
|
+
* effect of the read-only allowlists. 2.8.0 runs this hook before the plan
|
|
46
|
+
* guard and has no final allow; the same closures stay on for every release.
|
|
40
47
|
*/
|
|
41
48
|
const PLAN_MODE_TOOLS = new Set(["EnterPlanMode", "ExitPlanMode"]);
|
|
42
49
|
/**
|
|
@@ -48,7 +55,6 @@ const KNOWN_LABELS = new Set([
|
|
|
48
55
|
"ask",
|
|
49
56
|
"review",
|
|
50
57
|
"challenge",
|
|
51
|
-
"review_gate",
|
|
52
58
|
"rescue",
|
|
53
59
|
"swarm",
|
|
54
60
|
"swarm-write",
|
|
@@ -135,7 +141,6 @@ export function decideHookOutcomePure(input, ctx) {
|
|
|
135
141
|
case "ask":
|
|
136
142
|
case "review":
|
|
137
143
|
case "challenge":
|
|
138
|
-
case "review_gate":
|
|
139
144
|
// Read-only narrative surfaces share READ_ONLY_TOOLS. They run as
|
|
140
145
|
// non-interactive subprocesses, so interactive tool prompts cannot
|
|
141
146
|
// substitute for this allowlist.
|
|
@@ -5,7 +5,7 @@ import path from "node:path";
|
|
|
5
5
|
import { RuntimeError } from "../errors.js";
|
|
6
6
|
import { selectKimiBinary } from "../kimi-command.js";
|
|
7
7
|
import { kimiReleaseHas } from "../kimi-releases.js";
|
|
8
|
-
import { probeKimiVersion } from "../kimi-version-probe.js";
|
|
8
|
+
import { KIMI_MINIMUM_CERTIFIED_VERSION, probeKimiVersion, } from "../kimi-version-probe.js";
|
|
9
9
|
import { sleep, tomlErrorLocation } from "../util.js";
|
|
10
10
|
import { extractValue } from "../vendor/smol-toml/extract.js";
|
|
11
11
|
import { parse as parseToml } from "../vendor/smol-toml/parse.js";
|
|
@@ -17,8 +17,9 @@ const DEFAULT_LOCK_STALE_MS = 2_000;
|
|
|
17
17
|
const DEFAULT_LOCK_RETRY_MS = 25;
|
|
18
18
|
export const KIMI_CONFIG_LOCK_METADATA_MAX_BYTES = 4_096;
|
|
19
19
|
const HOOK_FIELDS = new Set(["event", "matcher", "command", "timeout"]);
|
|
20
|
-
//
|
|
21
|
-
//
|
|
20
|
+
// Supported hook events for >= 2.8.0. Additive events (`PermissionRequest`,
|
|
21
|
+
// `PermissionResult`, `Interrupt`) still require an exact certified `hook-schema`
|
|
22
|
+
// release row.
|
|
22
23
|
const BASE_HOOK_EVENTS = new Set([
|
|
23
24
|
"PreToolUse",
|
|
24
25
|
"PostToolUse",
|
|
@@ -34,10 +35,10 @@ const BASE_HOOK_EVENTS = new Set([
|
|
|
34
35
|
"PostCompact",
|
|
35
36
|
"Notification",
|
|
36
37
|
]);
|
|
37
|
-
const VERSION_SENSITIVE_HOOK_EVENTS = new
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
const VERSION_SENSITIVE_HOOK_EVENTS = new Set([
|
|
39
|
+
"PermissionRequest",
|
|
40
|
+
"PermissionResult",
|
|
41
|
+
"Interrupt",
|
|
41
42
|
]);
|
|
42
43
|
export function kimiConfigLockPath(configPath) {
|
|
43
44
|
return `${configPath}${LOCK_SUFFIX}`;
|
|
@@ -554,14 +555,13 @@ function validateHookEntry(entry, index, line, versionSensitiveEvents) {
|
|
|
554
555
|
return invalidHookSet("missing required field `event`", index, line);
|
|
555
556
|
if (typeof event !== "string")
|
|
556
557
|
return invalidHookSet("`event` must be a string", index, line);
|
|
557
|
-
const
|
|
558
|
-
if (!BASE_HOOK_EVENTS.has(event) &&
|
|
558
|
+
const isVersionSensitive = VERSION_SENSITIVE_HOOK_EVENTS.has(event);
|
|
559
|
+
if (!BASE_HOOK_EVENTS.has(event) && !isVersionSensitive) {
|
|
559
560
|
return invalidHookSet(`unsupported hook event ${JSON.stringify(event)}`, index, line);
|
|
560
561
|
}
|
|
561
|
-
if (
|
|
562
|
+
if (isVersionSensitive) {
|
|
562
563
|
versionSensitiveEvents.push({
|
|
563
564
|
event,
|
|
564
|
-
minimumMinor,
|
|
565
565
|
entry: index,
|
|
566
566
|
line,
|
|
567
567
|
});
|
|
@@ -590,24 +590,15 @@ function validateVersionSensitiveEvents(events, version) {
|
|
|
590
590
|
if (version === undefined) {
|
|
591
591
|
return invalidHookSet(`version-sensitive event ${JSON.stringify(first.event)} requires a verified kimi-code version`, first.entry, first.line);
|
|
592
592
|
}
|
|
593
|
-
//
|
|
594
|
-
//
|
|
595
|
-
// commit). Schema acceptance does not certify operations.
|
|
596
|
-
// Require the full version string AND matching components; no inferred 2.x compatibility.
|
|
593
|
+
// Require the full version string AND matching components against a certified
|
|
594
|
+
// >= 2.8.0 `hook-schema` release row; no inferred compatibility.
|
|
597
595
|
if (version.major === 2 &&
|
|
598
596
|
version.version === `${version.major}.${version.minor}.${version.patch}` &&
|
|
599
597
|
kimiReleaseHas(version.version, "hook-schema")) {
|
|
600
598
|
return { valid: true };
|
|
601
599
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
}
|
|
605
|
-
for (const event of events) {
|
|
606
|
-
if (version.minor < event.minimumMinor) {
|
|
607
|
-
return invalidHookSet(`event ${JSON.stringify(event.event)} requires kimi-code >= 0.${event.minimumMinor}, but the installed version is 0.${version.minor}`, event.entry, event.line);
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
return { valid: true };
|
|
600
|
+
const observed = version.version ?? `${version.major}.${version.minor}`;
|
|
601
|
+
return invalidHookSet(`version-sensitive event ${JSON.stringify(first.event)} requires a certified kimi-code >= ${KIMI_MINIMUM_CERTIFIED_VERSION} release (got ${observed})`, first.entry, first.line);
|
|
611
602
|
}
|
|
612
603
|
function invalidHookSet(reason, entry, line, failureKind = "schema") {
|
|
613
604
|
const location = entry > 0 ? `hooks[${entry - 1}] at line ${line}` : `hooks configuration at line ${line}`;
|
|
@@ -15,7 +15,7 @@ export const HOOK_DECISION_DEADLINE_MS = HOOK_TIMEOUT_S * 1000 - 5_000;
|
|
|
15
15
|
/**
|
|
16
16
|
* kimi-code's timeout (seconds) for a [[hooks]] entry with no `timeout` key:
|
|
17
17
|
* `DEFAULT_HOOK_TIMEOUT_SECONDS` in agent-core-v2 `externalHooks/internal/
|
|
18
|
-
* matchHooks.ts`, 30 in every certified source through 2.
|
|
18
|
+
* matchHooks.ts`, 30 in every certified source through 2.8.0. Each compat
|
|
19
19
|
* audit re-verifies it.
|
|
20
20
|
*/
|
|
21
21
|
export const UPSTREAM_DEFAULT_HOOK_TIMEOUT_S = 30;
|
|
@@ -7,9 +7,12 @@
|
|
|
7
7
|
// upstream. The probe executes the same bytes that setup writes.
|
|
8
8
|
import { createHash } from "node:crypto";
|
|
9
9
|
import { realpathSync } from "node:fs";
|
|
10
|
+
import { lstat, readFile, realpath } from "node:fs/promises";
|
|
10
11
|
import path from "node:path";
|
|
11
12
|
import { fileURLToPath } from "node:url";
|
|
12
13
|
import { RuntimeError } from "../errors.js";
|
|
14
|
+
import { resolvePluginPaths } from "../paths.js";
|
|
15
|
+
import { resolvePluginDataRoot } from "../plugin-data.js";
|
|
13
16
|
/**
|
|
14
17
|
* Prefer a stable, non-version-stamped path to the SAME Node binary.
|
|
15
18
|
* Version-stamped interpreter paths change on upgrade and invalidate the
|
|
@@ -61,8 +64,7 @@ export function resolveNodeBinary(env) {
|
|
|
61
64
|
* Build the exact shell command string that `/bin/sh -c "<command>"`
|
|
62
65
|
* needs to spawn the hook. Single source of truth for:
|
|
63
66
|
*
|
|
64
|
-
* - what
|
|
65
|
-
* (`command = "..."` inside [[hooks]])
|
|
67
|
+
* - what setup writes into kimi-code's config (`command = "..."` inside [[hooks]])
|
|
66
68
|
* - what the shell probe runs via `spawn("/bin/sh", ["-c", ...])`
|
|
67
69
|
* - what the verifier (`evaluateInstalled`) equality-checks the
|
|
68
70
|
* installed `command = "..."` against on every command spawn
|
|
@@ -84,24 +86,36 @@ export function shellSingleQuote(value) {
|
|
|
84
86
|
return `'${value.replaceAll("'", "'\\''")}'`;
|
|
85
87
|
}
|
|
86
88
|
/**
|
|
87
|
-
*
|
|
89
|
+
* Locate the compiled, self-contained ESM hook artifact bundled with this
|
|
90
|
+
* package (`<packageRoot>/dist/hooks/approval-hook.js`). Setup copies this
|
|
91
|
+
* file into `<pluginRoot>/hook/approval-hook.js`.
|
|
92
|
+
*/
|
|
93
|
+
export function resolveBundledHookSourcePath() {
|
|
94
|
+
const here = fileURLToPath(import.meta.url);
|
|
95
|
+
const parts = here.split(path.sep);
|
|
96
|
+
if (parts.length < 3) {
|
|
97
|
+
throw resolveHookFailure(here);
|
|
98
|
+
}
|
|
99
|
+
const tailParent = parts[parts.length - 2];
|
|
100
|
+
const tailGrandparent = parts[parts.length - 3];
|
|
101
|
+
if (tailParent !== "hooks" || (tailGrandparent !== "runtime" && tailGrandparent !== "dist")) {
|
|
102
|
+
throw resolveHookFailure(here);
|
|
103
|
+
}
|
|
104
|
+
const packageRoot = parts.slice(0, parts.length - 3).join(path.sep) || path.sep;
|
|
105
|
+
return path.join(packageRoot, "dist", "hooks", "approval-hook.js");
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Resolve the absolute path to the installed hook script.
|
|
88
109
|
*
|
|
89
110
|
* Resolution order:
|
|
90
|
-
*
|
|
91
111
|
* 1. `KIMI_PLUGIN_CC_HOOK_SCRIPT` override — tests / advanced users.
|
|
92
|
-
* 2.
|
|
93
|
-
*
|
|
94
|
-
* artifact lives at `<root>/dist/hooks/approval-hook.js`. Walk up
|
|
95
|
-
* to `<root>` and append the canonical hook artifact path.
|
|
112
|
+
* 2. Canonical shared plugin hook path: `<pluginRoot>/hook/approval-hook.js`
|
|
113
|
+
* (e.g. `~/.local/share/kimi-plugin-cc/hook/approval-hook.js`).
|
|
96
114
|
*/
|
|
97
115
|
export function resolveHookScriptPath(env) {
|
|
98
116
|
const override = env.KIMI_PLUGIN_CC_HOOK_SCRIPT;
|
|
99
117
|
if (override !== undefined && override.length > 0) {
|
|
100
118
|
if (!path.isAbsolute(override)) {
|
|
101
|
-
// The hook shell's cwd can differ from the companion's. Require an
|
|
102
|
-
// absolute override so installation, verification, and the runtime spawn
|
|
103
|
-
// all refer to the same file rather than blessing a relative path that
|
|
104
|
-
// resolves elsewhere when kimi-code executes it.
|
|
105
119
|
throw new RuntimeError("SETUP_HOOK_SCRIPT_NOT_ABSOLUTE", [
|
|
106
120
|
`KIMI_PLUGIN_CC_HOOK_SCRIPT must be an absolute path; got ${JSON.stringify(override)}.`,
|
|
107
121
|
`kimi-code spawns hooks via /bin/sh -c with a cwd that may differ from the companion's.`,
|
|
@@ -110,21 +124,7 @@ export function resolveHookScriptPath(env) {
|
|
|
110
124
|
}
|
|
111
125
|
return override;
|
|
112
126
|
}
|
|
113
|
-
|
|
114
|
-
const parts = here.split(path.sep);
|
|
115
|
-
// Pin to the canonical suffix `{runtime|dist}/hooks/install-paths.{ts,js}`
|
|
116
|
-
// — anchoring to a specific tail keeps ancestor directories named
|
|
117
|
-
// "runtime" or "dist" from confusing the lookup.
|
|
118
|
-
if (parts.length < 3) {
|
|
119
|
-
throw resolveHookFailure(here);
|
|
120
|
-
}
|
|
121
|
-
const tailParent = parts[parts.length - 2];
|
|
122
|
-
const tailGrandparent = parts[parts.length - 3];
|
|
123
|
-
if (tailParent !== "hooks" || (tailGrandparent !== "runtime" && tailGrandparent !== "dist")) {
|
|
124
|
-
throw resolveHookFailure(here);
|
|
125
|
-
}
|
|
126
|
-
const pluginRoot = parts.slice(0, parts.length - 3).join(path.sep) || path.sep;
|
|
127
|
-
return path.join(pluginRoot, "dist", "hooks", "approval-hook.js");
|
|
127
|
+
return resolvePluginPaths(env).hookScriptPath;
|
|
128
128
|
}
|
|
129
129
|
function resolveHookFailure(here) {
|
|
130
130
|
return new RuntimeError("SETUP_RESOLVE_HOOK_FAILED", `Could not infer plugin root from install-paths module path ${here}. Set KIMI_PLUGIN_CC_HOOK_SCRIPT to the absolute path of dist/hooks/approval-hook.js.`, "setup.resolve-hook", { details: { here } });
|
|
@@ -211,11 +211,35 @@ function defaultSameFile(a, b) {
|
|
|
211
211
|
return false;
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
|
+
export function resolveDefaultCanonicalHookScriptPath(env) {
|
|
215
|
+
const baseEnv = { ...env };
|
|
216
|
+
delete baseEnv.KIMI_PLUGIN_CC_DATA;
|
|
217
|
+
try {
|
|
218
|
+
return path.join(resolvePluginDataRoot(baseEnv), "kimi-plugin-cc", "hook", "approval-hook.js");
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
export function isDataOverrideFromCanonicalHook(installedHookScript, expectedHookScript, env, sameFile = defaultSameFile) {
|
|
225
|
+
if (env === undefined ||
|
|
226
|
+
env.KIMI_PLUGIN_CC_DATA === undefined ||
|
|
227
|
+
(env.KIMI_PLUGIN_CC_HOOK_SCRIPT && env.KIMI_PLUGIN_CC_HOOK_SCRIPT.length > 0) ||
|
|
228
|
+
installedHookScript === expectedHookScript) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
const canonicalHook = resolveDefaultCanonicalHookScriptPath(env);
|
|
232
|
+
if (canonicalHook === undefined)
|
|
233
|
+
return false;
|
|
234
|
+
return path.normalize(installedHookScript) === path.normalize(canonicalHook) || sameFile(installedHookScript, canonicalHook);
|
|
235
|
+
}
|
|
214
236
|
/**
|
|
215
237
|
* Machine-readable classification of a canonical-command mismatch. Returns
|
|
216
238
|
* which token moved and, for Node, whether both spellings name the same file.
|
|
217
239
|
*
|
|
218
|
-
* `"hook-script"` drift is recoverable by running setup on this host
|
|
240
|
+
* `"hook-script"` drift is recoverable by running setup on this host unless it
|
|
241
|
+
* stems from an active `KIMI_PLUGIN_CC_DATA` override while the shared block
|
|
242
|
+
* points at the canonical hook (`dataOverrideFromCanonical: true`).
|
|
219
243
|
* `"node-bin"`/`"both"` drift is also recoverable when
|
|
220
244
|
* `nodeInterpreterUnchanged === true`; a token naming a different interpreter
|
|
221
245
|
* is not automatically repairable. A hook that cannot spawn fails open upstream.
|
|
@@ -223,7 +247,7 @@ function defaultSameFile(a, b) {
|
|
|
223
247
|
* Returns `undefined` for non-canonical commands or equal tokens. Classification
|
|
224
248
|
* never changes the verifier's exact-equality decision.
|
|
225
249
|
*/
|
|
226
|
-
export function classifyHookCommandDrift(installedCommand, expectedCommand, sameFile = defaultSameFile) {
|
|
250
|
+
export function classifyHookCommandDrift(installedCommand, expectedCommand, sameFile = defaultSameFile, env) {
|
|
227
251
|
const installed = parseHookShellCommand(installedCommand);
|
|
228
252
|
const expected = parseHookShellCommand(expectedCommand);
|
|
229
253
|
if (installed === null || expected === null) {
|
|
@@ -235,6 +259,7 @@ export function classifyHookCommandDrift(installedCommand, expectedCommand, same
|
|
|
235
259
|
return undefined;
|
|
236
260
|
}
|
|
237
261
|
const axis = nodeDrift && hookDrift ? "both" : nodeDrift ? "node-bin" : "hook-script";
|
|
262
|
+
const dataOverride = hookDrift && isDataOverrideFromCanonicalHook(installed.hookScript, expected.hookScript, env, sameFile);
|
|
238
263
|
// A changed Node spelling that still names the same file is path drift, not
|
|
239
264
|
// an interpreter move; callers use this to decide whether setup may retry.
|
|
240
265
|
return {
|
|
@@ -242,6 +267,7 @@ export function classifyHookCommandDrift(installedCommand, expectedCommand, same
|
|
|
242
267
|
installedCommand,
|
|
243
268
|
expectedCommand,
|
|
244
269
|
...(nodeDrift ? { nodeInterpreterUnchanged: sameFile(installed.nodeBin, expected.nodeBin) } : {}),
|
|
270
|
+
...(dataOverride ? { dataOverrideFromCanonical: true } : {}),
|
|
245
271
|
};
|
|
246
272
|
}
|
|
247
273
|
/**
|
|
@@ -251,7 +277,7 @@ export function classifyHookCommandDrift(installedCommand, expectedCommand, same
|
|
|
251
277
|
* message in place. Pure except for the injected filesystem predicates; never
|
|
252
278
|
* changes the verifier's exact-equality decision.
|
|
253
279
|
*/
|
|
254
|
-
export function describeHookCommandDrift(installedCommand, expectedCommand, nodeExists, sameFile = defaultSameFile) {
|
|
280
|
+
export function describeHookCommandDrift(installedCommand, expectedCommand, nodeExists, sameFile = defaultSameFile, env) {
|
|
255
281
|
const installed = parseHookShellCommand(installedCommand);
|
|
256
282
|
const expected = parseHookShellCommand(expectedCommand);
|
|
257
283
|
if (installed === null || expected === null) {
|
|
@@ -262,6 +288,7 @@ export function describeHookCommandDrift(installedCommand, expectedCommand, node
|
|
|
262
288
|
if (!nodeDrift && !hookDrift) {
|
|
263
289
|
return undefined;
|
|
264
290
|
}
|
|
291
|
+
const dataOverride = hookDrift && isDataOverrideFromCanonicalHook(installed.hookScript, expected.hookScript, env, sameFile);
|
|
265
292
|
const parts = [];
|
|
266
293
|
if (nodeDrift) {
|
|
267
294
|
if (!nodeExists(installed.nodeBin)) {
|
|
@@ -284,10 +311,22 @@ export function describeHookCommandDrift(installedCommand, expectedCommand, node
|
|
|
284
311
|
}
|
|
285
312
|
}
|
|
286
313
|
if (hookDrift) {
|
|
287
|
-
|
|
288
|
-
`
|
|
314
|
+
if (dataOverride) {
|
|
315
|
+
parts.push(`Hook script path drift: the installed hook points at the shared canonical hook ${installed.hookScript}, ` +
|
|
316
|
+
`but KIMI_PLUGIN_CC_DATA=${JSON.stringify(env.KIMI_PLUGIN_CC_DATA)} directs this host to ` +
|
|
317
|
+
`${expected.hookScript} (likely a KIMI_PLUGIN_CC_DATA override left over from a 3.0 per-host setup).`);
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
parts.push(`Hook script path drift: the installed hook points at ${installed.hookScript}, but this companion's ` +
|
|
321
|
+
`hook is ${expected.hookScript} (likely an upgrade from an earlier hook path or a moved data directory).`);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (dataOverride) {
|
|
325
|
+
parts.push("Unset KIMI_PLUGIN_CC_DATA in your host environment (for example in ~/.claude/settings.json) and restart the host so it uses the shared canonical hook.");
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
parts.push("Run `kimi_setup` (action \"install\") or `scripts/companion.sh setup` to re-pin the managed block to this companion's current paths.");
|
|
289
329
|
}
|
|
290
|
-
parts.push("Run /kimi:setup to re-pin the managed block to this companion's current paths.");
|
|
291
330
|
return parts.join(" ");
|
|
292
331
|
}
|
|
293
332
|
/**
|
|
@@ -337,80 +376,17 @@ export function tryBuildExpectedHookCommand(env) {
|
|
|
337
376
|
};
|
|
338
377
|
}
|
|
339
378
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
// The managed block is host-scoped (marker suffix `:<host-id>`) so each host
|
|
349
|
-
// owns and verifies its OWN PreToolUse block without clobbering the other's.
|
|
350
|
-
// The host id must be VERSION-INDEPENDENT so a plugin upgrade REFRESHES the
|
|
351
|
-
// same host's block instead of accumulating one block per version.
|
|
352
|
-
/**
|
|
353
|
-
* Resolve a stable, version-independent host id for the managed-block marker.
|
|
354
|
-
*
|
|
355
|
-
* Order: an explicit `KIMI_PLUGIN_CC_HOST_ID` override (slugified) wins — used
|
|
356
|
-
* by tests and the live-repair path. Otherwise derive from the resolved hook
|
|
357
|
-
* script path. Pass the already-resolved `hookScriptPath` when you have it
|
|
358
|
-
* (the verifier + setup do) so we don't re-resolve and risk a second throw.
|
|
359
|
-
*/
|
|
360
|
-
export function resolveHostId(env, hookScriptPath) {
|
|
361
|
-
const override = env.KIMI_PLUGIN_CC_HOST_ID;
|
|
362
|
-
if (override !== undefined && override.trim().length > 0) {
|
|
363
|
-
return slugifyHostId(override);
|
|
364
|
-
}
|
|
365
|
-
const resolved = hookScriptPath ?? resolveHookScriptPath(env);
|
|
366
|
-
return hostIdFromHookScript(resolved);
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Derive a host id from a hook-script path. `~/.claude/...` → `claude-code`,
|
|
370
|
-
* `~/.codex/...` → `codex` (both literal + version-independent), else a stable
|
|
371
|
-
* `host-<sha1(hookDir)[:16]>` for dev checkouts / unrecognized layouts.
|
|
372
|
-
*/
|
|
373
|
-
export function hostIdFromHookScript(hookScriptPath) {
|
|
374
|
-
const norm = hookScriptPath.split(path.sep).join("/");
|
|
375
|
-
if (norm.includes("/.claude/"))
|
|
376
|
-
return "claude-code";
|
|
377
|
-
if (norm.includes("/.codex/"))
|
|
378
|
-
return "codex";
|
|
379
|
-
// Hashing the hook directory is stable for dev checkouts. Sixteen hex chars
|
|
380
|
-
// avoid the collision risk of a 32-bit prefix, which could make one root
|
|
381
|
-
// treat another root's managed block as its own.
|
|
382
|
-
const digest = createHash("sha1").update(path.dirname(hookScriptPath)).digest("hex");
|
|
383
|
-
return `host-${digest.slice(0, 16)}`;
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* Normalize an arbitrary host-id override into the `[a-z0-9-]+` slug the
|
|
387
|
-
* marker regex accepts. Empty results fall back to `host` so the marker is
|
|
388
|
-
* always well-formed.
|
|
389
|
-
*/
|
|
390
|
-
export function slugifyHostId(value) {
|
|
391
|
-
const slug = value
|
|
392
|
-
.trim()
|
|
393
|
-
.toLowerCase()
|
|
394
|
-
.replace(/[^a-z0-9]+/g, "-")
|
|
395
|
-
.replace(/^-+|-+$/g, "")
|
|
396
|
-
.slice(0, 40);
|
|
397
|
-
return slug.length > 0 ? slug : "host";
|
|
398
|
-
}
|
|
379
|
+
const OWNED_APPROVAL_HOOK_PATTERNS = [
|
|
380
|
+
/\/kimi-plugin-cc\/hook\/approval-hook\.js$/,
|
|
381
|
+
/\/kimi-plugin-cc\/(?:dist|runtime)\/hooks\/approval-hook\.js$/,
|
|
382
|
+
/\/plugins\/kimi-codex\/dist\/hooks\/approval-hook\.js$/,
|
|
383
|
+
/\/dsh-kimi-plugin-cc\/dist\/hooks\/approval-hook\.js$/,
|
|
384
|
+
/\/kimi-marketplace\/[^/]+\/[^/]+\/(?:dist|runtime)\/hooks\/approval-hook\.js$/,
|
|
385
|
+
/\/\.gemini\/config\/plugins\/kimi\/(?:dist|runtime)\/hooks\/approval-hook\.js$/,
|
|
386
|
+
];
|
|
399
387
|
/**
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
* scoped cleanup treats that as ambiguous rather than claiming another host's
|
|
403
|
-
* block.
|
|
404
|
-
*/
|
|
405
|
-
export function hostIdFromHookCommand(command) {
|
|
406
|
-
const parsed = parseHookShellCommand(command);
|
|
407
|
-
if (parsed === null)
|
|
408
|
-
return null;
|
|
409
|
-
return hostIdFromHookScript(parsed.hookScript);
|
|
410
|
-
}
|
|
411
|
-
/**
|
|
412
|
-
* True only for a canonical two-single-quoted-token command whose script is
|
|
413
|
-
* `approval-hook.js` under a kimi-plugin-cc/dsh-kimi-plugin-cc/kimi-marketplace install tree.
|
|
388
|
+
* True only for a canonical two-single-quoted-token command whose script
|
|
389
|
+
* matches an exact kimi-plugin-cc install/published approval-hook path.
|
|
414
390
|
* This strict ownership test protects hand-rolled and non-canonical user hooks
|
|
415
391
|
* from orphan pruning.
|
|
416
392
|
*/
|
|
@@ -418,11 +394,147 @@ export function isOurApprovalHookCommand(decodedCommand) {
|
|
|
418
394
|
const parsed = parseHookShellCommand(decodedCommand);
|
|
419
395
|
if (parsed === null)
|
|
420
396
|
return false;
|
|
421
|
-
const script = parsed.hookScript.split(/[\\/]/);
|
|
422
|
-
if (script[script.length - 1] !== "approval-hook.js")
|
|
423
|
-
return false;
|
|
424
397
|
const normalized = parsed.hookScript.replace(/\\/g, "/");
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
398
|
+
return OWNED_APPROVAL_HOOK_PATTERNS.some((pattern) => pattern.test(normalized));
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Verify the managed `<pluginRoot>/hook/approval-hook.js` artifact on disk:
|
|
402
|
+
* - Reject symlinks or non-directory/non-regular files along `pluginRoot`, `hookDir`,
|
|
403
|
+
* and `hookScriptPath`, and require canonical containment under `pluginRoot/hook`.
|
|
404
|
+
* - Require SHA-256 byte equality against the bundled `dist/hooks/approval-hook.js`.
|
|
405
|
+
* Skipped when `KIMI_PLUGIN_CC_HOOK_SCRIPT` explicitly overrides the script path.
|
|
406
|
+
*/
|
|
407
|
+
export async function verifyManagedHookArtifact(env, hookScriptPath) {
|
|
408
|
+
if (env.KIMI_PLUGIN_CC_HOOK_SCRIPT && env.KIMI_PLUGIN_CC_HOOK_SCRIPT.length > 0) {
|
|
409
|
+
return { ok: true };
|
|
410
|
+
}
|
|
411
|
+
const paths = resolvePluginPaths(env);
|
|
412
|
+
let pluginRootReal;
|
|
413
|
+
try {
|
|
414
|
+
const info = await lstat(paths.pluginRoot);
|
|
415
|
+
if (info.isSymbolicLink() || !info.isDirectory()) {
|
|
416
|
+
return {
|
|
417
|
+
ok: false,
|
|
418
|
+
kind: "unsafe-path",
|
|
419
|
+
reason: `plugin data directory ${paths.pluginRoot} is a symlink or not a directory; refusing to trust managed hook`,
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
pluginRootReal = await realpath(paths.pluginRoot);
|
|
423
|
+
}
|
|
424
|
+
catch (err) {
|
|
425
|
+
if (err.code === "ENOENT") {
|
|
426
|
+
return {
|
|
427
|
+
ok: false,
|
|
428
|
+
kind: "missing",
|
|
429
|
+
reason: `hook script ${hookScriptPath} is missing or unreadable — run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to reinstall`,
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
return {
|
|
433
|
+
ok: false,
|
|
434
|
+
kind: "unsafe-path",
|
|
435
|
+
reason: `unable to inspect plugin data directory ${paths.pluginRoot}: ${err.message}`,
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
let hookDirReal;
|
|
439
|
+
try {
|
|
440
|
+
const info = await lstat(paths.hookDir);
|
|
441
|
+
if (info.isSymbolicLink() || !info.isDirectory()) {
|
|
442
|
+
return {
|
|
443
|
+
ok: false,
|
|
444
|
+
kind: "unsafe-path",
|
|
445
|
+
reason: `hook directory ${paths.hookDir} is a symlink or not a directory; refusing to trust managed hook`,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
hookDirReal = await realpath(paths.hookDir);
|
|
449
|
+
}
|
|
450
|
+
catch (err) {
|
|
451
|
+
if (err.code === "ENOENT") {
|
|
452
|
+
return {
|
|
453
|
+
ok: false,
|
|
454
|
+
kind: "missing",
|
|
455
|
+
reason: `hook script ${hookScriptPath} is missing or unreadable — run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to reinstall`,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
return {
|
|
459
|
+
ok: false,
|
|
460
|
+
kind: "unsafe-path",
|
|
461
|
+
reason: `unable to inspect hook directory ${paths.hookDir}: ${err.message}`,
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
const expectedHookDirReal = path.join(pluginRootReal, "hook");
|
|
465
|
+
if (hookDirReal !== expectedHookDirReal) {
|
|
466
|
+
return {
|
|
467
|
+
ok: false,
|
|
468
|
+
kind: "unsafe-path",
|
|
469
|
+
reason: `hook directory ${paths.hookDir} resolves to ${hookDirReal}, outside ${expectedHookDirReal}; refusing to trust managed hook`,
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
try {
|
|
473
|
+
const info = await lstat(hookScriptPath);
|
|
474
|
+
if (info.isSymbolicLink() || !info.isFile()) {
|
|
475
|
+
return {
|
|
476
|
+
ok: false,
|
|
477
|
+
kind: "unsafe-path",
|
|
478
|
+
reason: `hook script ${hookScriptPath} is a symlink or not a regular file; refusing to execute managed hook`,
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
const scriptReal = await realpath(hookScriptPath);
|
|
482
|
+
const expectedScriptReal = path.join(hookDirReal, path.basename(hookScriptPath));
|
|
483
|
+
if (scriptReal !== expectedScriptReal) {
|
|
484
|
+
return {
|
|
485
|
+
ok: false,
|
|
486
|
+
kind: "unsafe-path",
|
|
487
|
+
reason: `hook script ${hookScriptPath} resolves to ${scriptReal}, outside ${hookDirReal}; refusing to execute managed hook`,
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
catch (err) {
|
|
492
|
+
if (err.code === "ENOENT") {
|
|
493
|
+
return {
|
|
494
|
+
ok: false,
|
|
495
|
+
kind: "missing",
|
|
496
|
+
reason: `hook script ${hookScriptPath} is missing or unreadable — run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to reinstall`,
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
return {
|
|
500
|
+
ok: false,
|
|
501
|
+
kind: "unsafe-path",
|
|
502
|
+
reason: `unable to inspect hook script ${hookScriptPath}: ${err.message}`,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
let installedBytes;
|
|
506
|
+
try {
|
|
507
|
+
installedBytes = await readFile(hookScriptPath);
|
|
508
|
+
}
|
|
509
|
+
catch {
|
|
510
|
+
return {
|
|
511
|
+
ok: false,
|
|
512
|
+
kind: "missing",
|
|
513
|
+
reason: `hook script ${hookScriptPath} is missing or unreadable — run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to reinstall`,
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
let sourcePath;
|
|
517
|
+
let bundledBytes;
|
|
518
|
+
try {
|
|
519
|
+
sourcePath = resolveBundledHookSourcePath();
|
|
520
|
+
bundledBytes = await readFile(sourcePath);
|
|
521
|
+
}
|
|
522
|
+
catch (err) {
|
|
523
|
+
return {
|
|
524
|
+
ok: false,
|
|
525
|
+
kind: "bundle-missing",
|
|
526
|
+
reason: `bundled hook source is missing or unreadable (${err.message}); run \`bun run build\` or reinstall the plugin`,
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
const installedHash = createHash("sha256").update(installedBytes).digest("hex");
|
|
530
|
+
const bundledHash = createHash("sha256").update(bundledBytes).digest("hex");
|
|
531
|
+
if (installedHash !== bundledHash) {
|
|
532
|
+
return {
|
|
533
|
+
ok: false,
|
|
534
|
+
kind: "sha256-mismatch",
|
|
535
|
+
reason: `installed hook script ${hookScriptPath} does not match the bundled hook artifact (${sourcePath}, SHA-256 mismatch — likely after a plugin update). ` +
|
|
536
|
+
`Run \`kimi_setup\` (action "install") or \`scripts/companion.sh setup\` to republish it.`,
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
return { ok: true };
|
|
428
540
|
}
|