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
|
@@ -1,729 +0,0 @@
|
|
|
1
|
-
// Shared parser for the kimi-plugin-cc managed block in
|
|
2
|
-
// ~/.kimi-code/config.toml. Setup and the verifier use this one grammar;
|
|
3
|
-
// install-paths.ts separately owns the canonical command bytes and drift
|
|
4
|
-
// classification used here after a mismatch.
|
|
5
|
-
//
|
|
6
|
-
// Claude Code and Codex share the config but install to different paths. The
|
|
7
|
-
// marker's `:<host-id>` suffix makes each host own and verify only its block.
|
|
8
|
-
// An un-suffixed legacy block can be adopted by the current host on install.
|
|
9
|
-
//
|
|
10
|
-
// A valid block has this line-oriented shape:
|
|
11
|
-
//
|
|
12
|
-
// # === BEGIN kimi-plugin-cc-managed:<host-id> (vX.Y.Z) ===
|
|
13
|
-
// ... optional comments ...
|
|
14
|
-
// [[hooks]]
|
|
15
|
-
// event = "PreToolUse"
|
|
16
|
-
// command = "'/abs/path/to/node' '/abs/path/to/approval-hook.js'"
|
|
17
|
-
// timeout = <integer>
|
|
18
|
-
// # === END kimi-plugin-cc-managed:<host-id> ===
|
|
19
|
-
//
|
|
20
|
-
// Critical rules:
|
|
21
|
-
//
|
|
22
|
-
// - No `matcher` in any TOML spelling: it can disable or narrow the hook.
|
|
23
|
-
// - `event = "PreToolUse"` is required.
|
|
24
|
-
// - Exactly one `[[hooks]]` table appears inside the markers.
|
|
25
|
-
// - The decoded command must equal the canonical expected command byte-for-byte.
|
|
26
|
-
// - Every PreToolUse entry with that command needs an effective timeout of at
|
|
27
|
-
// least HOOK_TIMEOUT_S (checked on the whole-file parse).
|
|
28
|
-
import { parse as parseToml } from "../vendor/smol-toml/parse.js";
|
|
29
|
-
import { classifyHookCommandDrift, describeHookCommandDrift, hostIdFromHookCommand, isOurApprovalHookCommand, } from "./install-paths.js";
|
|
30
|
-
import { HOOK_TIMEOUT_S, hookTimeoutEnforces } from "./hook-timing.js";
|
|
31
|
-
const MARKER_TAG = "kimi-plugin-cc-managed";
|
|
32
|
-
/**
|
|
33
|
-
* Plain install removes unpaired marker comments for every host and leaves each
|
|
34
|
-
* host's hook table in place. A scoped uninstall removes only this host's
|
|
35
|
-
* markers, so it would disarm a healthy block and keep another host's orphan.
|
|
36
|
-
*/
|
|
37
|
-
export const ORPHAN_MARKER_REMEDY = "Run /kimi:setup (without --uninstall or --check) to repair: install removes orphaned managed marker comments for every host and keeps each host's hook table. A scoped --uninstall does not remove another host's orphan marker. Another tool rewriting ~/.kimi-code/config.toml usually causes this.";
|
|
38
|
-
/**
|
|
39
|
-
* Strict BEGIN matcher. Accepts `# === BEGIN kimi-plugin-cc-managed`
|
|
40
|
-
* with an optional `:<host-id>` suffix, an optional ` (vX.Y.Z)` suffix,
|
|
41
|
-
* and trailing ` ===`. Group 1 captures the host id (undefined = legacy).
|
|
42
|
-
* Anything else (random comment containing the tag, mid-line embedded
|
|
43
|
-
* marker) is not a managed-block marker.
|
|
44
|
-
*/
|
|
45
|
-
const BEGIN_LINE_RE = /^#\s*===\s*BEGIN\s+kimi-plugin-cc-managed(?::([A-Za-z0-9._-]+))?(?:\s+\([^)]+\))?\s*===\s*$/;
|
|
46
|
-
const END_LINE_RE = /^#\s*===\s*END\s+kimi-plugin-cc-managed(?::([A-Za-z0-9._-]+))?\s*===\s*$/;
|
|
47
|
-
/** TOML basic string for `command = "..."`. Captures the inner value. */
|
|
48
|
-
const COMMAND_LINE_RE = /^command\s*=\s*"((?:[^"\\]|\\.)*)"\s*$/;
|
|
49
|
-
/** TOML literal string for `command = '...'` (no escapes inside). */
|
|
50
|
-
const COMMAND_LITERAL_LINE_RE = /^command\s*=\s*'([^']*)'\s*$/;
|
|
51
|
-
const EVENT_LINE_RE = /^event\s*=\s*"PreToolUse"\s*$/;
|
|
52
|
-
/** Anything matcher-shaped is a critical safety failure — block disabled. */
|
|
53
|
-
const MATCHER_LINE_RE = /^matcher\s*=/;
|
|
54
|
-
/** `timeout = <int>` — the only other key our managed [[hooks]] table emits. */
|
|
55
|
-
const TIMEOUT_LINE_RE = /^timeout\s*=\s*\d+\s*$/;
|
|
56
|
-
const HOOKS_TABLE_RE = /^\[\[hooks\]\]\s*$/;
|
|
57
|
-
// Recognize comment lines lexically, not text inside TOML string values.
|
|
58
|
-
// Whole-document parsing remains the authority for TOML validity.
|
|
59
|
-
function scanTomlLines(contents) {
|
|
60
|
-
const markers = new Set();
|
|
61
|
-
const tables = new Set();
|
|
62
|
-
let state = "plain";
|
|
63
|
-
let line = 0;
|
|
64
|
-
let blankPrefix = true;
|
|
65
|
-
for (let i = 0; i < contents.length; i += 1) {
|
|
66
|
-
const c = contents[i];
|
|
67
|
-
if (c === "\n") {
|
|
68
|
-
line += 1;
|
|
69
|
-
blankPrefix = true;
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
const onlyWhitespaceBefore = blankPrefix;
|
|
73
|
-
if (c !== " " && c !== "\t" && c !== "\r")
|
|
74
|
-
blankPrefix = false;
|
|
75
|
-
if (state === "plain") {
|
|
76
|
-
if (c === "[" && onlyWhitespaceBefore)
|
|
77
|
-
tables.add(line);
|
|
78
|
-
if (c === "#") {
|
|
79
|
-
if (onlyWhitespaceBefore) {
|
|
80
|
-
const end = contents.indexOf("\n", i);
|
|
81
|
-
const comment = contents.slice(i, end < 0 ? contents.length : end).trim();
|
|
82
|
-
if (BEGIN_LINE_RE.test(comment) || END_LINE_RE.test(comment))
|
|
83
|
-
markers.add(line);
|
|
84
|
-
}
|
|
85
|
-
const end = contents.indexOf("\n", i);
|
|
86
|
-
if (end < 0)
|
|
87
|
-
break;
|
|
88
|
-
i = end - 1;
|
|
89
|
-
}
|
|
90
|
-
else if (c === '"' || c === "'") {
|
|
91
|
-
const triple = contents.slice(i, i + 3) === c.repeat(3);
|
|
92
|
-
state = c === '"' ? (triple ? "multibasic" : "basic") : (triple ? "multiliteral" : "literal");
|
|
93
|
-
if (triple)
|
|
94
|
-
i += 2;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
const basic = state === "basic" || state === "multibasic";
|
|
99
|
-
const quote = basic ? '"' : "'";
|
|
100
|
-
if (basic && c === "\\") {
|
|
101
|
-
if (contents[i + 1] === "\n") {
|
|
102
|
-
line += 1;
|
|
103
|
-
blankPrefix = true;
|
|
104
|
-
}
|
|
105
|
-
i += 1;
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
if (c !== quote)
|
|
109
|
-
continue;
|
|
110
|
-
if (state === "basic" || state === "literal")
|
|
111
|
-
state = "plain";
|
|
112
|
-
else if (contents.slice(i, i + 3) === quote.repeat(3)) {
|
|
113
|
-
while (contents[i + 1] === quote)
|
|
114
|
-
i += 1;
|
|
115
|
-
state = "plain";
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return { markers, tables };
|
|
120
|
-
}
|
|
121
|
-
export function markerCommentLines(contents) {
|
|
122
|
-
return scanTomlLines(contents).markers;
|
|
123
|
-
}
|
|
124
|
-
/** Locate every BEGIN/END marker line, in order, with its parsed host id. */
|
|
125
|
-
function findMarkers(lines) {
|
|
126
|
-
const hits = [];
|
|
127
|
-
const comments = markerCommentLines(lines.join("\n"));
|
|
128
|
-
for (let i = 0; i < lines.length; i += 1) {
|
|
129
|
-
if (!comments.has(i))
|
|
130
|
-
continue;
|
|
131
|
-
const trimmed = lines[i].trim();
|
|
132
|
-
const begin = BEGIN_LINE_RE.exec(trimmed);
|
|
133
|
-
if (begin !== null) {
|
|
134
|
-
// Normalize marker host ids so a hand-edited casing difference cannot
|
|
135
|
-
// create a distinct host and confusing duplicate blocks.
|
|
136
|
-
hits.push({ index: i, kind: "begin", hostId: begin[1]?.toLowerCase() ?? null });
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
const end = END_LINE_RE.exec(trimmed);
|
|
140
|
-
if (end !== null) {
|
|
141
|
-
hits.push({ index: i, kind: "end", hostId: end[1]?.toLowerCase() ?? null });
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return hits;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Pair BEGIN/END markers into well-formed blocks. A block is a BEGIN followed
|
|
148
|
-
* immediately (in marker order) by an END with the same host suffix; every
|
|
149
|
-
* other marker is an orphan. Nested or interleaved markers therefore orphan
|
|
150
|
-
* both sides rather than being guessed into a block. Callers decide which
|
|
151
|
-
* orphans fail closed (see `parseManagedBlock`).
|
|
152
|
-
*/
|
|
153
|
-
export function pairBlocks(lines) {
|
|
154
|
-
const markers = findMarkers(lines);
|
|
155
|
-
const blocks = [];
|
|
156
|
-
const orphans = [];
|
|
157
|
-
let i = 0;
|
|
158
|
-
while (i < markers.length) {
|
|
159
|
-
const marker = markers[i];
|
|
160
|
-
const next = markers[i + 1];
|
|
161
|
-
if (marker.kind === "begin" && next !== undefined && next.kind === "end" && next.hostId === marker.hostId) {
|
|
162
|
-
const body = validateBlockBody(lines.slice(marker.index + 1, next.index));
|
|
163
|
-
blocks.push({
|
|
164
|
-
hostId: marker.hostId,
|
|
165
|
-
beginLine: marker.index,
|
|
166
|
-
endLine: next.index,
|
|
167
|
-
commandPath: body.commandPath,
|
|
168
|
-
valid: body.valid,
|
|
169
|
-
invalidReason: body.invalidReason,
|
|
170
|
-
});
|
|
171
|
-
i += 2;
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
orphans.push({
|
|
175
|
-
line: marker.index,
|
|
176
|
-
detail: marker.kind === "begin" ? "BEGIN-without-END" : "END-without-BEGIN",
|
|
177
|
-
hostId: marker.hostId,
|
|
178
|
-
});
|
|
179
|
-
i += 1;
|
|
180
|
-
}
|
|
181
|
-
return { blocks, orphans };
|
|
182
|
-
}
|
|
183
|
-
/** Validate the lines BETWEEN a BEGIN/END pair against the safety grammar. */
|
|
184
|
-
function validateBlockBody(rawBody) {
|
|
185
|
-
const blockLines = rawBody.map((line) => line.trim());
|
|
186
|
-
let foundHooksTable = false;
|
|
187
|
-
let foundEvent = false;
|
|
188
|
-
let commandPath = "";
|
|
189
|
-
let invalidReason;
|
|
190
|
-
for (const line of blockLines) {
|
|
191
|
-
if (line.length === 0 || line.startsWith("#"))
|
|
192
|
-
continue;
|
|
193
|
-
if (HOOKS_TABLE_RE.test(line)) {
|
|
194
|
-
if (foundHooksTable) {
|
|
195
|
-
invalidReason = "block contains more than one [[hooks]] table";
|
|
196
|
-
break;
|
|
197
|
-
}
|
|
198
|
-
foundHooksTable = true;
|
|
199
|
-
continue;
|
|
200
|
-
}
|
|
201
|
-
if (line.startsWith("[")) {
|
|
202
|
-
// Any TOML table header other than the single [[hooks]] table means the
|
|
203
|
-
// `event`/`command` lines below could belong to a DIFFERENT table (e.g.
|
|
204
|
-
// `[[hooks]]` with only an event, then `[not_hooks]` carrying the
|
|
205
|
-
// canonical command). Binding the block to exactly one [[hooks]] table
|
|
206
|
-
// keeps the verifier from blessing a command-less hook.
|
|
207
|
-
invalidReason =
|
|
208
|
-
"block contains an unexpected TOML table — only a single [[hooks]] table is allowed. Reinstall with /kimi:setup to repair.";
|
|
209
|
-
break;
|
|
210
|
-
}
|
|
211
|
-
if (MATCHER_LINE_RE.test(line)) {
|
|
212
|
-
invalidReason =
|
|
213
|
-
'block contains a `matcher = ...` line — kimi-code compiles matchers as JS regex (`new RegExp("*")` throws and disables the hook). Reinstall with /kimi:setup to repair.';
|
|
214
|
-
break;
|
|
215
|
-
}
|
|
216
|
-
if (EVENT_LINE_RE.test(line)) {
|
|
217
|
-
foundEvent = true;
|
|
218
|
-
continue;
|
|
219
|
-
}
|
|
220
|
-
// Decode TOML basic-string escapes before comparing with the canonical JS
|
|
221
|
-
// string; literal strings have no escapes. Otherwise shell quoting and
|
|
222
|
-
// TOML escaping can make an equivalent command compare unequal.
|
|
223
|
-
const decoded = decodeManagedCommandLine(line);
|
|
224
|
-
if (decoded !== null) {
|
|
225
|
-
commandPath = decoded;
|
|
226
|
-
continue;
|
|
227
|
-
}
|
|
228
|
-
// Other TOML lines are ignored only by this local grammar. Whole-file schema
|
|
229
|
-
// validation and foundHookEntryIsClean reject foreign keys before an
|
|
230
|
-
// installed verdict; the shell probe is not the validation backstop.
|
|
231
|
-
}
|
|
232
|
-
if (invalidReason === undefined) {
|
|
233
|
-
if (!foundHooksTable) {
|
|
234
|
-
invalidReason = "block is missing a `[[hooks]]` table";
|
|
235
|
-
}
|
|
236
|
-
else if (!foundEvent) {
|
|
237
|
-
invalidReason = 'block is missing `event = "PreToolUse"`';
|
|
238
|
-
}
|
|
239
|
-
else if (commandPath.length === 0) {
|
|
240
|
-
invalidReason = "block is missing a `command = \"...\"` line";
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
// Parser-based matcher rejection backs up the bare-line regex: smol-toml
|
|
244
|
-
// normalizes quoted, literal, and dotted matcher keys that the regex misses,
|
|
245
|
-
// while kimi-code honors them and can disable or narrow the hook. Both this
|
|
246
|
-
// marked-block path and the absent-state fallback must parse the real TOML.
|
|
247
|
-
// Fail closed when the body cannot be parsed in isolation.
|
|
248
|
-
if (invalidReason === undefined) {
|
|
249
|
-
try {
|
|
250
|
-
const parsed = parseToml(rawBody.join("\n"));
|
|
251
|
-
const hooks = parsed !== null && typeof parsed === "object"
|
|
252
|
-
? parsed.hooks
|
|
253
|
-
: undefined;
|
|
254
|
-
const hasMatcher = Array.isArray(hooks) &&
|
|
255
|
-
hooks.some((entry) => entry !== null &&
|
|
256
|
-
typeof entry === "object" &&
|
|
257
|
-
Object.prototype.hasOwnProperty.call(entry, "matcher"));
|
|
258
|
-
if (hasMatcher) {
|
|
259
|
-
invalidReason =
|
|
260
|
-
"block contains a `matcher` key (in a quoted or dotted spelling the line scanner misses) — kimi-code compiles matchers as JS regex and disables the hook. Reinstall with /kimi:setup to repair.";
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
catch {
|
|
264
|
-
invalidReason =
|
|
265
|
-
"block body is not valid TOML in isolation — cannot confirm it carries no hook-disabling matcher. Reinstall with /kimi:setup to repair.";
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
return { valid: invalidReason === undefined, invalidReason, commandPath };
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Parse the supplied kimi-code config text and resolve the managed-block
|
|
272
|
-
* state FOR A SPECIFIC HOST.
|
|
273
|
-
*
|
|
274
|
-
* Host resolution: the current host's own suffixed block is authoritative if
|
|
275
|
-
* present; otherwise a lone LEGACY (un-suffixed) block is adoptable and is
|
|
276
|
-
* returned as the found block (the current host will convert it on install).
|
|
277
|
-
* Other hosts' suffixed blocks are ignored for this host's state (that is the
|
|
278
|
-
* whole point of host scoping — they coexist).
|
|
279
|
-
*
|
|
280
|
-
* Only a trailing `\r` is removed from each line for CRLF safety; other line
|
|
281
|
-
* whitespace is preserved here and trimmed only by the relevant matchers.
|
|
282
|
-
*/
|
|
283
|
-
export function parseManagedBlock(contents, hostId) {
|
|
284
|
-
const rawLines = contents.split("\n");
|
|
285
|
-
const lines = rawLines.map((line) => line.replace(/\r$/, ""));
|
|
286
|
-
const { blocks, orphans } = pairBlocks(lines);
|
|
287
|
-
// An orphan suffixed for ANOTHER host cannot be part of this host's block:
|
|
288
|
-
// this host's verdict still rests on its own well-formed pair, the whole-file
|
|
289
|
-
// hook-set validation, and the real-parser cleanliness check. Legacy and own
|
|
290
|
-
// orphans stay fail-closed because they may be this host's damaged block.
|
|
291
|
-
const ownOrphan = orphans.find((o) => o.hostId === null || o.hostId === hostId);
|
|
292
|
-
const foreignOrphans = orphans.filter((o) => o.hostId !== null && o.hostId !== hostId);
|
|
293
|
-
if (ownOrphan !== undefined) {
|
|
294
|
-
return { state: { kind: "orphan", detail: ownOrphan.detail }, blocks: [], lines, foreignOrphans };
|
|
295
|
-
}
|
|
296
|
-
// A block belongs to this host when its marker suffix matches or, for a
|
|
297
|
-
// legacy un-suffixed block, its command derives to this host. An unattributable
|
|
298
|
-
// legacy command is claimable by the next installer; a command attributed to
|
|
299
|
-
// another host is ignored so its block remains untouched.
|
|
300
|
-
const relevant = blocks.filter((b) => effectiveHost(b, hostId) === hostId);
|
|
301
|
-
if (relevant.length === 0) {
|
|
302
|
-
return { state: { kind: "absent" }, blocks, lines, foreignOrphans };
|
|
303
|
-
}
|
|
304
|
-
if (relevant.length > 1) {
|
|
305
|
-
return {
|
|
306
|
-
state: { kind: "duplicate", beginLines: relevant.map((b) => b.beginLine) },
|
|
307
|
-
blocks,
|
|
308
|
-
lines,
|
|
309
|
-
foreignOrphans,
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
const block = relevant[0];
|
|
313
|
-
return {
|
|
314
|
-
state: {
|
|
315
|
-
kind: "found",
|
|
316
|
-
beginLine: block.beginLine,
|
|
317
|
-
endLine: block.endLine,
|
|
318
|
-
hostId: block.hostId,
|
|
319
|
-
commandPath: block.commandPath,
|
|
320
|
-
valid: block.valid,
|
|
321
|
-
invalidReason: block.invalidReason,
|
|
322
|
-
},
|
|
323
|
-
blocks,
|
|
324
|
-
lines,
|
|
325
|
-
foreignOrphans,
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* The host a managed block belongs to: its marker suffix when present,
|
|
330
|
-
* otherwise the host derived from its command path (a legacy un-suffixed
|
|
331
|
-
* block), falling back to `currentHost` when the command can't be attributed
|
|
332
|
-
* (a stale/bare legacy command — claimable by whoever installs next).
|
|
333
|
-
*/
|
|
334
|
-
export function effectiveHost(block, currentHost) {
|
|
335
|
-
if (block.hostId !== null)
|
|
336
|
-
return block.hostId;
|
|
337
|
-
return hostIdFromHookCommand(block.commandPath) ?? currentHost;
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Decode the `command = "..."` (or `'...'`) value from a single trimmed TOML
|
|
341
|
-
* line, or `null` if the line isn't a command assignment. Exported so the
|
|
342
|
-
* uninstall path can attribute a legacy block to its host without re-deriving
|
|
343
|
-
* the TOML decode.
|
|
344
|
-
*/
|
|
345
|
-
export function decodeManagedCommandLine(trimmedLine) {
|
|
346
|
-
const basic = COMMAND_LINE_RE.exec(trimmedLine);
|
|
347
|
-
if (basic !== null)
|
|
348
|
-
return decodeTomlBasicString(basic[1]);
|
|
349
|
-
const literal = COMMAND_LITERAL_LINE_RE.exec(trimmedLine);
|
|
350
|
-
if (literal !== null)
|
|
351
|
-
return literal[1];
|
|
352
|
-
return null;
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* True if a parsed `hooks[]` array contains a clean enforcing entry for
|
|
356
|
-
* `expectedCommand`: `event="PreToolUse"`, byte-exact `command`, NO `matcher`
|
|
357
|
-
* (any spelling the parser normalized to the `matcher` key can disable/narrow
|
|
358
|
-
* the hook), no keys beyond {event,command,timeout} (a stray key is fatal to
|
|
359
|
-
* kimi-code's strict schema), and a timeout kimi-code waits out. Each host's
|
|
360
|
-
* hook path is distinct, so the command uniquely identifies this host's entry.
|
|
361
|
-
*/
|
|
362
|
-
function countCleanEntries(hooks, expectedCommand) {
|
|
363
|
-
if (!Array.isArray(hooks))
|
|
364
|
-
return 0;
|
|
365
|
-
const allowedKeys = new Set(["event", "command", "timeout"]);
|
|
366
|
-
let count = 0;
|
|
367
|
-
for (const entry of hooks) {
|
|
368
|
-
if (entry === null || typeof entry !== "object")
|
|
369
|
-
continue;
|
|
370
|
-
const rec = entry;
|
|
371
|
-
if (rec.event !== "PreToolUse")
|
|
372
|
-
continue;
|
|
373
|
-
if (rec.command !== expectedCommand)
|
|
374
|
-
continue;
|
|
375
|
-
if (Object.prototype.hasOwnProperty.call(rec, "matcher"))
|
|
376
|
-
continue;
|
|
377
|
-
if (!Object.keys(rec).every((k) => allowedKeys.has(k)))
|
|
378
|
-
continue;
|
|
379
|
-
if (!hookTimeoutEnforces(rec.timeout))
|
|
380
|
-
continue;
|
|
381
|
-
count += 1;
|
|
382
|
-
}
|
|
383
|
-
return count;
|
|
384
|
-
}
|
|
385
|
-
/** Clean entries in the whole file's parsed `hooks[]`; 0 when it does not parse. */
|
|
386
|
-
export function countCleanEntriesInFile(contents, expectedCommand) {
|
|
387
|
-
let parsed;
|
|
388
|
-
try {
|
|
389
|
-
parsed = parseToml(contents);
|
|
390
|
-
}
|
|
391
|
-
catch {
|
|
392
|
-
return 0;
|
|
393
|
-
}
|
|
394
|
-
if (parsed === null || typeof parsed !== "object")
|
|
395
|
-
return 0;
|
|
396
|
-
return countCleanEntries(parsed.hooks, expectedCommand);
|
|
397
|
-
}
|
|
398
|
-
/**
|
|
399
|
-
* The first PreToolUse entry with this exact command whose timeout kimi-code
|
|
400
|
-
* would not wait out, in whole-file parse order (a `timeout` after the END
|
|
401
|
-
* marker binds to the same table). kimi-code runs only the first entry per
|
|
402
|
-
* command, after matcher filtering, so a short copy anywhere can be the one
|
|
403
|
-
* that runs and its timeout is read as allow. `null` when every copy enforces
|
|
404
|
-
* or the file does not parse; parse failures already fail closed elsewhere.
|
|
405
|
-
*/
|
|
406
|
-
export function findShortHookTimeout(contents, expectedCommand) {
|
|
407
|
-
let parsed;
|
|
408
|
-
try {
|
|
409
|
-
parsed = parseToml(contents);
|
|
410
|
-
}
|
|
411
|
-
catch {
|
|
412
|
-
return null;
|
|
413
|
-
}
|
|
414
|
-
const hooks = parsed?.hooks;
|
|
415
|
-
if (!Array.isArray(hooks))
|
|
416
|
-
return null;
|
|
417
|
-
for (const entry of hooks) {
|
|
418
|
-
if (entry === null || typeof entry !== "object")
|
|
419
|
-
continue;
|
|
420
|
-
const rec = entry;
|
|
421
|
-
if (rec.event !== "PreToolUse" || rec.command !== expectedCommand)
|
|
422
|
-
continue;
|
|
423
|
-
if (!hookTimeoutEnforces(rec.timeout))
|
|
424
|
-
return { timeout: rec.timeout };
|
|
425
|
-
}
|
|
426
|
-
return null;
|
|
427
|
-
}
|
|
428
|
-
/** `timeout = <value>` as written, for diagnoses. */
|
|
429
|
-
export function describeHookTimeout(timeout) {
|
|
430
|
-
const shown = typeof timeout === "number" || typeof timeout === "bigint" ? String(timeout) : JSON.stringify(timeout);
|
|
431
|
-
return `timeout = ${shown}`;
|
|
432
|
-
}
|
|
433
|
-
export function hookTimeoutReason(timeout) {
|
|
434
|
-
return `this host's hook entry has ${describeHookTimeout(timeout)}; kimi-code reads a hook that runs past its timeout as allow, so the entry needs a timeout of at least ${HOOK_TIMEOUT_S} seconds. Run /kimi:setup to rewrite it; setup leaves the config unchanged and names any copy it cannot rewrite.`;
|
|
435
|
-
}
|
|
436
|
-
/**
|
|
437
|
-
* Parser-based fallback used only in the absent state. Parse the WHOLE file
|
|
438
|
-
* exactly as kimi-code does: deleting managed-block lines first would change
|
|
439
|
-
* which table their keys bind to (a `matcher` written before another host's
|
|
440
|
-
* `[[hooks]]` header belongs to the preceding table, possibly this host's bare
|
|
441
|
-
* one). Host isolation is kept by counting: every managed block whose body
|
|
442
|
-
* carries this exact command may account for one clean entry, so only a clean
|
|
443
|
-
* entry beyond those can be this host's marker-less table. Requires the exact
|
|
444
|
-
* event and command, no matcher in any spelling, and no foreign keys. Parse
|
|
445
|
-
* errors fail closed. Callers run whole-file schema validation first.
|
|
446
|
-
*/
|
|
447
|
-
export function hasCleanEnforcingHookEntry(contents, expectedCommand) {
|
|
448
|
-
const lines = contents.split("\n").map((line) => line.replace(/\r$/, ""));
|
|
449
|
-
const managedWithCommand = pairBlocks(lines).blocks.filter((block) => block.commandPath === expectedCommand).length;
|
|
450
|
-
return countCleanEntriesInFile(contents, expectedCommand) > managedWithCommand;
|
|
451
|
-
}
|
|
452
|
-
/**
|
|
453
|
-
* Whole-file parser check for the FOUND (marked-block present) path. The line
|
|
454
|
-
* grammar in `validateBlockBody` only inspects the lines BETWEEN the BEGIN/END
|
|
455
|
-
* markers — but a `matcher = "*"` (or a stray key) placed AFTER the END comment
|
|
456
|
-
* is still part of the SAME `[[hooks]]` TOML table (comments don't terminate a
|
|
457
|
-
* table), so kimi-code loads it and DISABLES the hook (`new RegExp("*")` throws)
|
|
458
|
-
* while the marked body looks clean → a fail-open `installed:true`. Parsing the
|
|
459
|
-
* whole file exactly as kimi-code does attributes that trailing key to our entry
|
|
460
|
-
* and rejects it. Fail-closed on any parse error.
|
|
461
|
-
*/
|
|
462
|
-
function foundHookEntryIsClean(contents, expectedCommand) {
|
|
463
|
-
return countCleanEntriesInFile(contents, expectedCommand) > 0;
|
|
464
|
-
}
|
|
465
|
-
export function evaluateInstalled(contents, expectedCommand, opts) {
|
|
466
|
-
const { state } = parseManagedBlock(contents, opts.hostId);
|
|
467
|
-
const shortTimeout = findShortHookTimeout(contents, expectedCommand);
|
|
468
|
-
const timeoutRefusal = () => ({
|
|
469
|
-
installed: false,
|
|
470
|
-
reason: hookTimeoutReason(shortTimeout.timeout),
|
|
471
|
-
state,
|
|
472
|
-
timeoutTooShort: true,
|
|
473
|
-
});
|
|
474
|
-
if (state.kind === "absent") {
|
|
475
|
-
if (shortTimeout !== null)
|
|
476
|
-
return timeoutRefusal();
|
|
477
|
-
// kimi-code config rewrites drop comments but leave hook tables intact. In
|
|
478
|
-
// the absent state only, accept a marker-less table that is exactly this
|
|
479
|
-
// host's clean enforcing hook: markers are not the security boundary, the
|
|
480
|
-
// byte-exact command is. The real TOML parser sees matchers and foreign keys
|
|
481
|
-
// across blank lines, quoted spellings, and multiline values.
|
|
482
|
-
if (hasCleanEnforcingHookEntry(contents, expectedCommand)) {
|
|
483
|
-
return {
|
|
484
|
-
installed: true,
|
|
485
|
-
via: "bare-table",
|
|
486
|
-
note: "this host's managed-block markers are missing (kimi-code rewrites its config on login/settings changes and strips all comments), but the hook table with the exact canonical command is present and enforcing. Run /kimi:setup to re-adorn the markers.",
|
|
487
|
-
state,
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
// If no exact bare table remains, classify drift only from this host's own
|
|
491
|
-
// stale table so the caller gets an actionable axis.
|
|
492
|
-
const staleOwn = findBareApprovalHookTables(contents).find((table) => table.command !== expectedCommand &&
|
|
493
|
-
isOurApprovalHookCommand(table.command) &&
|
|
494
|
-
hostIdFromHookCommand(table.command) === opts.hostId);
|
|
495
|
-
return {
|
|
496
|
-
installed: false,
|
|
497
|
-
reason: "managed block is not present",
|
|
498
|
-
state,
|
|
499
|
-
...(staleOwn !== undefined
|
|
500
|
-
? { drift: classifyHookCommandDrift(staleOwn.command, expectedCommand) }
|
|
501
|
-
: {}),
|
|
502
|
-
};
|
|
503
|
-
}
|
|
504
|
-
if (state.kind === "duplicate") {
|
|
505
|
-
return {
|
|
506
|
-
installed: false,
|
|
507
|
-
reason: `duplicate managed blocks detected at lines ${state.beginLines
|
|
508
|
-
.map((line) => line + 1)
|
|
509
|
-
.join(", ")}. Run /kimi:setup --uninstall, then /kimi:setup.`,
|
|
510
|
-
state,
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
if (state.kind === "orphan") {
|
|
514
|
-
return {
|
|
515
|
-
installed: false,
|
|
516
|
-
reason: `${state.detail} marker. ${ORPHAN_MARKER_REMEDY}`,
|
|
517
|
-
state,
|
|
518
|
-
};
|
|
519
|
-
}
|
|
520
|
-
if (!state.valid) {
|
|
521
|
-
return {
|
|
522
|
-
installed: false,
|
|
523
|
-
reason: state.invalidReason ?? "managed block failed validation",
|
|
524
|
-
state,
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
if (state.commandPath !== expectedCommand) {
|
|
528
|
-
const classified = opts.nodeExists !== undefined
|
|
529
|
-
? describeHookCommandDrift(state.commandPath, expectedCommand, opts.nodeExists)
|
|
530
|
-
: undefined;
|
|
531
|
-
return {
|
|
532
|
-
installed: false,
|
|
533
|
-
reason: classified ??
|
|
534
|
-
`installed block's command does not match the canonical command this companion would write. Run /kimi:setup to refresh. expected ${expectedCommand}; got ${state.commandPath}.`,
|
|
535
|
-
state,
|
|
536
|
-
drift: classifyHookCommandDrift(state.commandPath, expectedCommand),
|
|
537
|
-
};
|
|
538
|
-
}
|
|
539
|
-
if (shortTimeout !== null)
|
|
540
|
-
return timeoutRefusal();
|
|
541
|
-
if (!foundHookEntryIsClean(contents, expectedCommand)) {
|
|
542
|
-
return {
|
|
543
|
-
installed: false,
|
|
544
|
-
reason: "the managed block's hook table carries a matcher or an unexpected key OUTSIDE the marked body (e.g. after the END marker) — kimi-code would load it and disable the hook. Run /kimi:setup --uninstall, then /kimi:setup to repair.",
|
|
545
|
-
state,
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
return { installed: true, via: "managed-block", state };
|
|
549
|
-
}
|
|
550
|
-
/**
|
|
551
|
-
* Line-oriented PRUNE scanner, never the installed decision; that decision uses
|
|
552
|
-
* the real TOML parser through `hasCleanEnforcingHookEntry` and
|
|
553
|
-
* `foundHookEntryIsClean`.
|
|
554
|
-
*
|
|
555
|
-
* The deletion range `[start, end)` is conservative: only the contiguous
|
|
556
|
-
* non-blank body after `[[hooks]]`, stopping at a blank line, table header, or
|
|
557
|
-
* managed marker so adjacent user content is preserved. A separate full-span
|
|
558
|
-
* scan refuses any further TOML field before the next table, including quoted
|
|
559
|
-
* and dotted keys, so pruning cannot attach those fields to another table.
|
|
560
|
-
* `command` is decoded for the caller's ownership checks.
|
|
561
|
-
*/
|
|
562
|
-
export function findBareApprovalHookTables(contents) {
|
|
563
|
-
const lines = contents.split("\n").map((line) => line.replace(/\r$/, ""));
|
|
564
|
-
const tableLines = scanTomlLines(contents).tables;
|
|
565
|
-
const managed = new Set();
|
|
566
|
-
for (const block of pairBlocks(lines).blocks) {
|
|
567
|
-
for (let i = block.beginLine; i <= block.endLine; i += 1)
|
|
568
|
-
managed.add(i);
|
|
569
|
-
}
|
|
570
|
-
const ranges = [];
|
|
571
|
-
for (let i = 0; i < lines.length; i += 1) {
|
|
572
|
-
if (managed.has(i))
|
|
573
|
-
continue;
|
|
574
|
-
if (!tableLines.has(i) || !HOOKS_TABLE_RE.test(lines[i].trim()))
|
|
575
|
-
continue;
|
|
576
|
-
// Collect the contiguous table body.
|
|
577
|
-
let j = i + 1;
|
|
578
|
-
let commandDecoded = "";
|
|
579
|
-
let hasPreToolUseEvent = false;
|
|
580
|
-
let hasMatcher = false;
|
|
581
|
-
// Our tables contain only event/command/timeout and comments. Any other key,
|
|
582
|
-
// including a multiline array, makes the shape unrecognized so pruning never
|
|
583
|
-
// partially cuts a structure and leaves dangling TOML.
|
|
584
|
-
let simpleGrammar = true;
|
|
585
|
-
while (j < lines.length) {
|
|
586
|
-
const trimmed = lines[j].trim();
|
|
587
|
-
if (trimmed.length === 0)
|
|
588
|
-
break;
|
|
589
|
-
// Any TOML table header bounds the table — including one with a trailing
|
|
590
|
-
// comment (`[[permission.rules]] # note`). A bare leading `[` is a table
|
|
591
|
-
// header in TOML (value arrays are `key = [...]`), so stop there and
|
|
592
|
-
// never swallow the next table.
|
|
593
|
-
if (trimmed.startsWith("["))
|
|
594
|
-
break;
|
|
595
|
-
if (BEGIN_LINE_RE.test(trimmed) || END_LINE_RE.test(trimmed))
|
|
596
|
-
break;
|
|
597
|
-
if (trimmed.startsWith("#")) {
|
|
598
|
-
j += 1;
|
|
599
|
-
continue;
|
|
600
|
-
}
|
|
601
|
-
if (EVENT_LINE_RE.test(trimmed)) {
|
|
602
|
-
hasPreToolUseEvent = true;
|
|
603
|
-
}
|
|
604
|
-
else if (MATCHER_LINE_RE.test(trimmed)) {
|
|
605
|
-
hasMatcher = true;
|
|
606
|
-
}
|
|
607
|
-
else {
|
|
608
|
-
const decoded = decodeManagedCommandLine(trimmed);
|
|
609
|
-
if (decoded !== null) {
|
|
610
|
-
commandDecoded = decoded;
|
|
611
|
-
}
|
|
612
|
-
else if (!TIMEOUT_LINE_RE.test(trimmed)) {
|
|
613
|
-
// Some key other than event/command/timeout/matcher — not our shape.
|
|
614
|
-
simpleGrammar = false;
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
j += 1;
|
|
618
|
-
}
|
|
619
|
-
// Blank lines and marker comments do not end a TOML table. Never leave any
|
|
620
|
-
// field behind to be rebound to a preceding table after pruning, regardless
|
|
621
|
-
// of the field's spelling or whether it is valid for a hook.
|
|
622
|
-
let fieldBeyondBody = false;
|
|
623
|
-
for (let k = j; k < lines.length; k += 1) {
|
|
624
|
-
const t = lines[k].trim();
|
|
625
|
-
if (tableLines.has(k))
|
|
626
|
-
break;
|
|
627
|
-
if (t.length > 0 && !t.startsWith("#")) {
|
|
628
|
-
fieldBeyondBody = true;
|
|
629
|
-
break;
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
// Report only tables matching this conservative line grammar: PreToolUse,
|
|
633
|
-
// a decoded command, no recognized matcher in the TOML span, and no foreign
|
|
634
|
-
// keys in the contiguous body. This is not a full TOML-semantic acceptance
|
|
635
|
-
// check; command and host ownership are the caller's concern.
|
|
636
|
-
if (simpleGrammar &&
|
|
637
|
-
commandDecoded.length > 0 &&
|
|
638
|
-
hasPreToolUseEvent &&
|
|
639
|
-
!hasMatcher &&
|
|
640
|
-
!fieldBeyondBody) {
|
|
641
|
-
ranges.push({ start: i, end: j, command: commandDecoded });
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
return ranges;
|
|
645
|
-
}
|
|
646
|
-
/**
|
|
647
|
-
* Find marker-less canonical approval-hook tables outside managed blocks.
|
|
648
|
-
* `ownedBy` restricts results to the host derived from each command and is
|
|
649
|
-
* required for scoped pruning: another host's marker-less table may be its live
|
|
650
|
-
* hook after a config rewrite. Omit `ownedBy` only for the explicit every-host
|
|
651
|
-
* `/kimi:setup --uninstall --all` sweep. Non-canonical and hand-rolled hooks are
|
|
652
|
-
* not claimed.
|
|
653
|
-
*/
|
|
654
|
-
export function findUnmanagedApprovalHookBlocks(contents, ownedBy, alsoMatchCommand) {
|
|
655
|
-
return findBareApprovalHookTables(contents).filter((table) => {
|
|
656
|
-
// Exact current command bytes are the strongest ownership signal and win
|
|
657
|
-
// even when a host-id override disagrees with path derivation. This does
|
|
658
|
-
// not widen the prune: another host's or a hand-rolled hook has different
|
|
659
|
-
// command bytes.
|
|
660
|
-
if (alsoMatchCommand !== undefined && table.command === alsoMatchCommand)
|
|
661
|
-
return true;
|
|
662
|
-
if (!isOurApprovalHookCommand(table.command))
|
|
663
|
-
return false;
|
|
664
|
-
if (ownedBy === undefined)
|
|
665
|
-
return true;
|
|
666
|
-
return hostIdFromHookCommand(table.command) === ownedBy;
|
|
667
|
-
});
|
|
668
|
-
}
|
|
669
|
-
export const MARKERS = {
|
|
670
|
-
TAG: MARKER_TAG,
|
|
671
|
-
BEGIN_LINE_RE,
|
|
672
|
-
END_LINE_RE,
|
|
673
|
-
};
|
|
674
|
-
/**
|
|
675
|
-
* Decode a TOML 1.0 basic-string body (the bytes BETWEEN the quotes —
|
|
676
|
-
* the surrounding `"..."` is stripped by the capture group). Handles
|
|
677
|
-
* the six standard escapes plus `\\` and `\"`. Unknown escapes fall
|
|
678
|
-
* through as-is, mirroring permissive parsers; the canonical command
|
|
679
|
-
* we compare against never contains them, so a mismatch surfaces as
|
|
680
|
-
* "installed: false" rather than a parse error.
|
|
681
|
-
*
|
|
682
|
-
* Inverse of `tomlBasicString` in `runtime/commands/setup.ts`; decoding before
|
|
683
|
-
* comparison prevents shell/TOML escaping from causing a false mismatch.
|
|
684
|
-
*/
|
|
685
|
-
function decodeTomlBasicString(raw) {
|
|
686
|
-
let out = "";
|
|
687
|
-
for (let i = 0; i < raw.length; i += 1) {
|
|
688
|
-
const ch = raw[i];
|
|
689
|
-
if (ch !== "\\") {
|
|
690
|
-
out += ch;
|
|
691
|
-
continue;
|
|
692
|
-
}
|
|
693
|
-
const next = raw[i + 1];
|
|
694
|
-
if (next === undefined) {
|
|
695
|
-
out += ch;
|
|
696
|
-
continue;
|
|
697
|
-
}
|
|
698
|
-
i += 1;
|
|
699
|
-
switch (next) {
|
|
700
|
-
case "\\":
|
|
701
|
-
out += "\\";
|
|
702
|
-
break;
|
|
703
|
-
case "\"":
|
|
704
|
-
out += "\"";
|
|
705
|
-
break;
|
|
706
|
-
case "b":
|
|
707
|
-
out += "\b";
|
|
708
|
-
break;
|
|
709
|
-
case "t":
|
|
710
|
-
out += "\t";
|
|
711
|
-
break;
|
|
712
|
-
case "n":
|
|
713
|
-
out += "\n";
|
|
714
|
-
break;
|
|
715
|
-
case "f":
|
|
716
|
-
out += "\f";
|
|
717
|
-
break;
|
|
718
|
-
case "r":
|
|
719
|
-
out += "\r";
|
|
720
|
-
break;
|
|
721
|
-
default:
|
|
722
|
-
// Pass unknown escapes through verbatim. The canonical command
|
|
723
|
-
// never produces them, so an inequality surfaces as a benign
|
|
724
|
-
// "installed: false" rather than a parse exception.
|
|
725
|
-
out += `\\${next}`;
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
return out;
|
|
729
|
-
}
|