kimi-plugin-cc 2.7.3 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +3 -16
- package/README.md +428 -515
- package/assets/logo.svg +6 -0
- package/cordis.patch.yml +18 -0
- package/dist/background-spawn.js +14 -19
- package/dist/cli-client.js +163 -105
- package/dist/commands/ask.js +44 -96
- package/dist/commands/cancel.js +11 -59
- package/dist/commands/cli-helpers.js +102 -1
- package/dist/commands/models.js +2 -3
- package/dist/commands/pursue.js +8 -21
- package/dist/commands/registry.js +7 -26
- package/dist/commands/repair-sessions.js +121 -12
- package/dist/commands/replay.js +4 -3
- package/dist/commands/rescue.js +29 -87
- package/dist/commands/result.js +7 -1
- package/dist/commands/review.js +28 -18
- package/dist/commands/setup.js +813 -462
- package/dist/commands/status.js +2 -2
- package/dist/commands/swarm.js +112 -125
- package/dist/commands/worker.js +1 -2
- package/dist/companion.js +145 -19
- package/dist/git.js +1 -9
- package/dist/help.js +13 -9
- package/dist/hook-escape.js +7 -16
- package/dist/hooks/approval-hook.js +1766 -87
- package/dist/hooks/approval-policy.js +86 -19
- package/dist/hooks/config-safety.js +31 -64
- package/dist/hooks/hook-timing.js +48 -0
- package/dist/hooks/install-paths.js +179 -107
- package/dist/hooks/install.js +126 -53
- package/dist/hooks/managed-block.js +121 -127
- package/dist/job-store.js +206 -166
- package/dist/jobs.js +66 -19
- package/dist/kimi-command.js +74 -1
- package/dist/kimi-engine.js +198 -127
- package/dist/kimi-errors.js +15 -70
- package/dist/kimi-home.js +6 -0
- package/dist/kimi-journal.js +42 -0
- package/dist/kimi-releases.js +123 -0
- package/dist/kimi-timeouts.js +2 -16
- package/dist/kimi-version-probe.js +24 -108
- package/dist/logging.js +12 -7
- package/dist/mcp/binding.js +13 -4
- package/dist/mcp/job-control.js +390 -95
- package/dist/mcp/launcher.js +13 -7
- package/dist/mcp/operations.js +405 -179
- package/dist/mcp/protocol.js +124 -33
- package/dist/mcp/skills.js +130 -0
- package/dist/mcp/stdio.js +18 -4
- package/dist/model-command.js +5 -3
- package/dist/native-v2-preflight.js +243 -80
- package/dist/parsing.js +211 -243
- package/dist/paths.js +28 -10
- package/dist/plugin-data.js +28 -83
- package/dist/process-tree.js +35 -24
- package/dist/read-scope.js +435 -0
- package/dist/render.js +58 -46
- package/dist/rescue-approval.js +461 -84
- package/dist/session-references.js +216 -0
- package/dist/session-title.js +26 -6
- package/dist/stream-json.js +1 -3
- package/dist/surface-routing.js +108 -0
- package/dist/swarm-activation.js +74 -188
- package/dist/swarm-policy.js +2 -1
- package/dist/swarm-recovery.js +2 -1
- package/dist/util.js +28 -0
- package/dist/version.js +1 -1
- package/docs/migration.md +45 -206
- package/docs/models.md +24 -66
- package/docs/safety.md +198 -353
- package/docs/session-visibility.md +25 -13
- package/docs/swarm-recovery.md +26 -20
- package/dsh/index.json +54 -0
- package/mcp_config.json +10 -0
- package/package.json +23 -82
- package/plugin.json +19 -0
- package/plugins/kimi-codex/.codex-plugin/plugin.json +7 -7
- package/plugins/kimi-codex/.mcp.json +3 -3
- package/plugins/kimi-codex/dist/background-spawn.js +14 -19
- package/plugins/kimi-codex/dist/cli-client.js +163 -105
- package/plugins/kimi-codex/dist/commands/ask.js +44 -96
- package/plugins/kimi-codex/dist/commands/cancel.js +11 -59
- package/plugins/kimi-codex/dist/commands/cli-helpers.js +102 -1
- package/plugins/kimi-codex/dist/commands/models.js +2 -3
- package/plugins/kimi-codex/dist/commands/pursue.js +8 -21
- package/plugins/kimi-codex/dist/commands/registry.js +7 -26
- package/plugins/kimi-codex/dist/commands/repair-sessions.js +121 -12
- package/plugins/kimi-codex/dist/commands/replay.js +4 -3
- package/plugins/kimi-codex/dist/commands/rescue.js +29 -87
- package/plugins/kimi-codex/dist/commands/result.js +7 -1
- package/plugins/kimi-codex/dist/commands/review.js +28 -18
- package/plugins/kimi-codex/dist/commands/setup.js +813 -462
- package/plugins/kimi-codex/dist/commands/status.js +2 -2
- package/plugins/kimi-codex/dist/commands/swarm.js +112 -125
- package/plugins/kimi-codex/dist/commands/worker.js +1 -2
- package/plugins/kimi-codex/dist/companion.js +145 -19
- package/plugins/kimi-codex/dist/git.js +1 -9
- package/plugins/kimi-codex/dist/help.js +13 -9
- package/plugins/kimi-codex/dist/hook-escape.js +7 -16
- package/plugins/kimi-codex/dist/hooks/approval-hook.js +1766 -87
- package/plugins/kimi-codex/dist/hooks/approval-policy.js +86 -19
- package/plugins/kimi-codex/dist/hooks/config-safety.js +31 -64
- package/plugins/kimi-codex/dist/hooks/hook-timing.js +48 -0
- package/plugins/kimi-codex/dist/hooks/install-paths.js +179 -107
- package/plugins/kimi-codex/dist/hooks/install.js +126 -53
- package/plugins/kimi-codex/dist/hooks/managed-block.js +121 -127
- package/plugins/kimi-codex/dist/job-store.js +206 -166
- package/plugins/kimi-codex/dist/jobs.js +66 -19
- package/plugins/kimi-codex/dist/kimi-command.js +74 -1
- package/plugins/kimi-codex/dist/kimi-engine.js +198 -127
- package/plugins/kimi-codex/dist/kimi-errors.js +15 -70
- package/plugins/kimi-codex/dist/kimi-home.js +6 -0
- package/plugins/kimi-codex/dist/kimi-journal.js +42 -0
- package/plugins/kimi-codex/dist/kimi-releases.js +123 -0
- package/plugins/kimi-codex/dist/kimi-timeouts.js +2 -16
- package/plugins/kimi-codex/dist/kimi-version-probe.js +24 -108
- package/plugins/kimi-codex/dist/logging.js +12 -7
- package/plugins/kimi-codex/dist/mcp/binding.js +13 -4
- package/plugins/kimi-codex/dist/mcp/job-control.js +390 -95
- package/plugins/kimi-codex/dist/mcp/launcher.js +13 -7
- package/plugins/kimi-codex/dist/mcp/operations.js +405 -179
- package/plugins/kimi-codex/dist/mcp/protocol.js +124 -33
- package/plugins/kimi-codex/dist/mcp/skills.js +130 -0
- package/plugins/kimi-codex/dist/mcp/stdio.js +18 -4
- package/plugins/kimi-codex/dist/model-command.js +5 -3
- package/plugins/kimi-codex/dist/native-v2-preflight.js +243 -80
- package/plugins/kimi-codex/dist/parsing.js +211 -243
- package/plugins/kimi-codex/dist/paths.js +28 -10
- package/plugins/kimi-codex/dist/plugin-data.js +28 -83
- package/plugins/kimi-codex/dist/process-tree.js +35 -24
- package/plugins/kimi-codex/dist/read-scope.js +435 -0
- package/plugins/kimi-codex/dist/render.js +58 -46
- package/plugins/kimi-codex/dist/rescue-approval.js +461 -84
- package/plugins/kimi-codex/dist/session-references.js +216 -0
- package/plugins/kimi-codex/dist/session-title.js +26 -6
- package/plugins/kimi-codex/dist/stream-json.js +1 -3
- package/plugins/kimi-codex/dist/surface-routing.js +108 -0
- package/plugins/kimi-codex/dist/swarm-activation.js +74 -188
- package/plugins/kimi-codex/dist/swarm-policy.js +2 -1
- package/plugins/kimi-codex/dist/swarm-recovery.js +2 -1
- package/plugins/kimi-codex/dist/util.js +28 -0
- package/plugins/kimi-codex/dist/version.js +1 -1
- package/plugins/kimi-codex/scripts/companion.sh +4 -15
- package/plugins/kimi-codex/skills/kimi/SKILL.md +77 -0
- package/plugins/kimi-codex/skills/kimi/agents/openai.yaml +6 -0
- package/scripts/companion.sh +4 -15
- package/skills/kimi/SKILL.md +77 -0
- package/skills/kimi/agents/openai.yaml +6 -0
- package/CHANGELOG.md +0 -1333
- package/agents/kimi-ask.md +0 -89
- package/agents/kimi-challenge.md +0 -86
- package/agents/kimi-pursue.md +0 -93
- package/agents/kimi-rescue.md +0 -92
- package/agents/kimi-review.md +0 -86
- package/agents/kimi-swarm-write.md +0 -96
- package/agents/kimi-swarm.md +0 -86
- package/commands/README.md +0 -16
- package/commands/ask.md +0 -38
- package/commands/cancel.md +0 -17
- package/commands/challenge.md +0 -35
- package/commands/pursue.md +0 -41
- package/commands/replay.md +0 -13
- package/commands/rescue.md +0 -38
- package/commands/result.md +0 -13
- package/commands/review.md +0 -35
- package/commands/setup.md +0 -23
- package/commands/status.md +0 -13
- package/commands/swarm.md +0 -44
- package/dist/commands/not-implemented.js +0 -4
- package/dist/commands/review-gate.js +0 -390
- package/dist/config.js +0 -22
- package/dist/dsh/companion.js +0 -177
- package/dist/dsh/index.js +0 -169
- package/dist/hooks/review-gate-stop.js +0 -19
- package/dist/schemas/review-gate-output.js +0 -43
- package/docs/registry-releases.md +0 -228
- package/docs/upstream-2.4.1-certification.md +0 -54
- package/docs/upstream-2.5.0-certification.md +0 -52
- package/docs/upstream-2.5.1-certification.md +0 -47
- package/docs/upstream-2.5.2-certification.md +0 -40
- package/docs/upstream-2.5.3-certification.md +0 -73
- package/docs/upstream-2.6.0-certification.md +0 -65
- package/docs/upstream-2.6.2-certification.md +0 -144
- package/docs/upstream-2.6.3-certification.md +0 -100
- package/docs/upstream-2.7.2-certification.md +0 -113
- package/docs/upstream-2.7.3-certification.md +0 -83
- package/docs/windows-validation.md +0 -39
- package/hooks/README.md +0 -9
- package/hooks/hooks.json +0 -16
- package/plugins/kimi-codex/dist/commands/not-implemented.js +0 -4
- package/plugins/kimi-codex/dist/commands/review-gate.js +0 -390
- package/plugins/kimi-codex/dist/config.js +0 -22
- package/plugins/kimi-codex/dist/dsh/companion.js +0 -177
- package/plugins/kimi-codex/dist/dsh/index.js +0 -169
- package/plugins/kimi-codex/dist/hooks/review-gate-stop.js +0 -19
- package/plugins/kimi-codex/dist/schemas/review-gate-output.js +0 -43
- package/plugins/kimi-codex/scripts/review-gate-hook.sh +0 -32
- package/plugins/kimi-codex/skills/kimi-ask/SKILL.md +0 -35
- package/plugins/kimi-codex/skills/kimi-ask/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-cancel/SKILL.md +0 -25
- package/plugins/kimi-codex/skills/kimi-cancel/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-challenge/SKILL.md +0 -34
- package/plugins/kimi-codex/skills/kimi-challenge/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-pursue/SKILL.md +0 -32
- package/plugins/kimi-codex/skills/kimi-pursue/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-replay/SKILL.md +0 -25
- package/plugins/kimi-codex/skills/kimi-replay/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-rescue/SKILL.md +0 -34
- package/plugins/kimi-codex/skills/kimi-rescue/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-result/SKILL.md +0 -25
- package/plugins/kimi-codex/skills/kimi-result/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-review/SKILL.md +0 -34
- package/plugins/kimi-codex/skills/kimi-review/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-setup/SKILL.md +0 -34
- package/plugins/kimi-codex/skills/kimi-setup/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-status/SKILL.md +0 -25
- package/plugins/kimi-codex/skills/kimi-status/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-swarm/SKILL.md +0 -35
- package/plugins/kimi-codex/skills/kimi-swarm/agents/openai.yaml +0 -6
- package/plugins/kimi-codex/skills/kimi-swarm-write/SKILL.md +0 -36
- package/plugins/kimi-codex/skills/kimi-swarm-write/agents/openai.yaml +0 -6
- package/plugins/kimi-dsh/LICENSE +0 -201
- package/plugins/kimi-dsh/README.md +0 -83
- package/plugins/kimi-dsh/cordis.patch.yml +0 -45
- package/plugins/kimi-dsh/dist/background-spawn.js +0 -216
- package/plugins/kimi-dsh/dist/cli-cancellation.js +0 -43
- package/plugins/kimi-dsh/dist/cli-client.js +0 -1078
- package/plugins/kimi-dsh/dist/commands/ask.js +0 -336
- package/plugins/kimi-dsh/dist/commands/cancel.js +0 -216
- package/plugins/kimi-dsh/dist/commands/cli-helpers.js +0 -201
- package/plugins/kimi-dsh/dist/commands/models.js +0 -140
- package/plugins/kimi-dsh/dist/commands/not-implemented.js +0 -4
- package/plugins/kimi-dsh/dist/commands/pursue.js +0 -307
- package/plugins/kimi-dsh/dist/commands/recover-swarm.js +0 -140
- package/plugins/kimi-dsh/dist/commands/registry.js +0 -74
- package/plugins/kimi-dsh/dist/commands/repair-sessions.js +0 -160
- package/plugins/kimi-dsh/dist/commands/replay.js +0 -124
- package/plugins/kimi-dsh/dist/commands/rescue.js +0 -347
- package/plugins/kimi-dsh/dist/commands/result.js +0 -66
- package/plugins/kimi-dsh/dist/commands/review-gate.js +0 -390
- package/plugins/kimi-dsh/dist/commands/review.js +0 -220
- package/plugins/kimi-dsh/dist/commands/setup.js +0 -1129
- package/plugins/kimi-dsh/dist/commands/status.js +0 -27
- package/plugins/kimi-dsh/dist/commands/swarm.js +0 -849
- package/plugins/kimi-dsh/dist/commands/worker.js +0 -25
- package/plugins/kimi-dsh/dist/companion.js +0 -97
- package/plugins/kimi-dsh/dist/config.js +0 -22
- package/plugins/kimi-dsh/dist/dsh/companion.js +0 -177
- package/plugins/kimi-dsh/dist/dsh/index.js +0 -169
- package/plugins/kimi-dsh/dist/errors.js +0 -40
- package/plugins/kimi-dsh/dist/git.js +0 -224
- package/plugins/kimi-dsh/dist/help.js +0 -43
- package/plugins/kimi-dsh/dist/hook-escape.js +0 -71
- package/plugins/kimi-dsh/dist/hooks/approval-hook.js +0 -104
- package/plugins/kimi-dsh/dist/hooks/approval-policy.js +0 -233
- package/plugins/kimi-dsh/dist/hooks/config-safety.js +0 -665
- package/plugins/kimi-dsh/dist/hooks/install-paths.js +0 -428
- package/plugins/kimi-dsh/dist/hooks/install.js +0 -210
- package/plugins/kimi-dsh/dist/hooks/managed-block.js +0 -684
- package/plugins/kimi-dsh/dist/hooks/review-gate-stop.js +0 -19
- package/plugins/kimi-dsh/dist/job-id.js +0 -12
- package/plugins/kimi-dsh/dist/job-store.js +0 -659
- package/plugins/kimi-dsh/dist/jobs.js +0 -217
- package/plugins/kimi-dsh/dist/kimi-command.js +0 -71
- package/plugins/kimi-dsh/dist/kimi-engine.js +0 -675
- package/plugins/kimi-dsh/dist/kimi-errors.js +0 -260
- package/plugins/kimi-dsh/dist/kimi-home.js +0 -9
- package/plugins/kimi-dsh/dist/kimi-timeouts.js +0 -52
- package/plugins/kimi-dsh/dist/kimi-version-probe.js +0 -234
- package/plugins/kimi-dsh/dist/logging.js +0 -22
- package/plugins/kimi-dsh/dist/mcp/binding.js +0 -58
- package/plugins/kimi-dsh/dist/mcp/job-control.js +0 -345
- package/plugins/kimi-dsh/dist/mcp/launcher.js +0 -257
- package/plugins/kimi-dsh/dist/mcp/operations.js +0 -301
- package/plugins/kimi-dsh/dist/mcp/protocol.js +0 -275
- package/plugins/kimi-dsh/dist/mcp/server.js +0 -8
- package/plugins/kimi-dsh/dist/mcp/stdio.js +0 -78
- package/plugins/kimi-dsh/dist/mcp/supervised-child.js +0 -15
- package/plugins/kimi-dsh/dist/mcp/tool-result.js +0 -7
- package/plugins/kimi-dsh/dist/mcp/workspace.js +0 -34
- package/plugins/kimi-dsh/dist/model-command.js +0 -32
- package/plugins/kimi-dsh/dist/native-v2-preflight.js +0 -525
- package/plugins/kimi-dsh/dist/package.json +0 -1
- package/plugins/kimi-dsh/dist/parsing.js +0 -620
- package/plugins/kimi-dsh/dist/paths.js +0 -27
- package/plugins/kimi-dsh/dist/plugin-data.js +0 -98
- package/plugins/kimi-dsh/dist/process-tree.js +0 -586
- package/plugins/kimi-dsh/dist/prompt-size.js +0 -18
- package/plugins/kimi-dsh/dist/render.js +0 -177
- package/plugins/kimi-dsh/dist/rescue-approval.js +0 -865
- package/plugins/kimi-dsh/dist/schemas/review-gate-output.js +0 -43
- package/plugins/kimi-dsh/dist/session-recovery.js +0 -202
- package/plugins/kimi-dsh/dist/session-title.js +0 -293
- package/plugins/kimi-dsh/dist/stream-json.js +0 -346
- package/plugins/kimi-dsh/dist/swarm-activation.js +0 -1197
- package/plugins/kimi-dsh/dist/swarm-policy.js +0 -73
- package/plugins/kimi-dsh/dist/swarm-recovery.js +0 -165
- package/plugins/kimi-dsh/dist/swarm-tool-policy.js +0 -146
- package/plugins/kimi-dsh/dist/types.js +0 -1
- package/plugins/kimi-dsh/dist/vendor/shell-quote/LICENSE +0 -24
- package/plugins/kimi-dsh/dist/vendor/shell-quote/index.js +0 -1
- package/plugins/kimi-dsh/dist/vendor/shell-quote/parse.js +0 -229
- package/plugins/kimi-dsh/dist/vendor/smol-toml/LICENSE +0 -24
- package/plugins/kimi-dsh/dist/vendor/smol-toml/date.js +0 -156
- package/plugins/kimi-dsh/dist/vendor/smol-toml/error.js +0 -67
- package/plugins/kimi-dsh/dist/vendor/smol-toml/extract.js +0 -293
- package/plugins/kimi-dsh/dist/vendor/smol-toml/parse.js +0 -164
- package/plugins/kimi-dsh/dist/vendor/smol-toml/primitive.js +0 -151
- package/plugins/kimi-dsh/dist/vendor/smol-toml/struct.js +0 -156
- package/plugins/kimi-dsh/dist/vendor/smol-toml/util.js +0 -57
- package/plugins/kimi-dsh/dist/version.js +0 -7
- package/plugins/kimi-dsh/package.json +0 -78
- package/plugins/kimi-dsh/scripts/companion.sh +0 -37
- package/plugins/kimi-dsh/scripts/review-gate-hook.sh +0 -32
- package/scripts/review-gate-hook.sh +0 -32
|
@@ -1,58 +1,299 @@
|
|
|
1
|
+
import { runCancel } from "../commands/cancel.js";
|
|
2
|
+
import { runModelInventory } from "../commands/models.js";
|
|
3
|
+
import { runRecoverSwarm } from "../commands/recover-swarm.js";
|
|
4
|
+
import { runRepairSessions } from "../commands/repair-sessions.js";
|
|
5
|
+
import { replayJob } from "../commands/replay.js";
|
|
6
|
+
import { renderSetupResult, runSetup } from "../commands/setup.js";
|
|
1
7
|
import { formatError, RuntimeError } from "../errors.js";
|
|
2
|
-
import { markJobFailed, STALE_SWEEP_GRACE_MS } from "../jobs.js";
|
|
3
|
-
import { JobStore, parseHookEscapes, renderBudgetSource, } from "../job-store.js";
|
|
8
|
+
import { isCwdWithinWorkspace, markJobFailed, STALE_SWEEP_GRACE_MS, sweepStaleJobs, } from "../jobs.js";
|
|
9
|
+
import { JobStore, parseHookEscapes, parseJobWarnings, renderBudgetSource, } from "../job-store.js";
|
|
10
|
+
import { resolveKimiHome } from "../kimi-home.js";
|
|
4
11
|
import { ensurePluginPaths, resolvePluginPaths } from "../paths.js";
|
|
5
12
|
import { readProcessIdentity, revalidateProcessIdentities, revalidateProcessIdentity, } from "../process-tree.js";
|
|
6
13
|
import { readArtifact, renderTerminalJobArtifact } from "../render.js";
|
|
7
|
-
import {
|
|
8
|
-
import { assertMcpJobBinding, mcpCompanionIdentity, mcpKimiIdentity } from "./binding.js";
|
|
9
|
-
import { readJobControlArguments } from "./operations.js";
|
|
14
|
+
import { sleep } from "../util.js";
|
|
15
|
+
import { assertMcpJobBinding, jobCompanionIdentity, jobKimiIdentity, mcpCompanionIdentity, mcpKimiIdentity, } from "./binding.js";
|
|
16
|
+
import { readJobControlArguments, readSetupArguments, } from "./operations.js";
|
|
10
17
|
import { textResult } from "./tool-result.js";
|
|
11
18
|
import { resolveMcpWorkspace } from "./workspace.js";
|
|
19
|
+
const DEFAULT_WAIT_MS = 30_000;
|
|
20
|
+
const DEFAULT_SYNC_WAIT_TIMEOUT_MS = 50_000;
|
|
21
|
+
function pinnedKimiHomeEnv(env, cwd) {
|
|
22
|
+
return {
|
|
23
|
+
...env,
|
|
24
|
+
KIMI_CODE_HOME: resolveKimiHome(env, cwd),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function sleepAbortable(ms, signal) {
|
|
28
|
+
if (!signal)
|
|
29
|
+
return sleep(ms);
|
|
30
|
+
if (signal.aborted)
|
|
31
|
+
return Promise.resolve();
|
|
32
|
+
return new Promise((resolve) => {
|
|
33
|
+
const onAbort = () => {
|
|
34
|
+
clearTimeout(timer);
|
|
35
|
+
resolve();
|
|
36
|
+
};
|
|
37
|
+
const timer = setTimeout(() => {
|
|
38
|
+
signal.removeEventListener("abort", onAbort);
|
|
39
|
+
resolve();
|
|
40
|
+
}, ms);
|
|
41
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
export async function handleMcpJobTool(env, input, signal) {
|
|
45
|
+
const args = readJobControlArguments(input);
|
|
46
|
+
switch (args.action) {
|
|
47
|
+
case "list":
|
|
48
|
+
return await listMcpJobs(env, args);
|
|
49
|
+
case "status":
|
|
50
|
+
return await getMcpJobStatus(env, input);
|
|
51
|
+
case "result":
|
|
52
|
+
return await getMcpJobResult(env, input);
|
|
53
|
+
case "wait":
|
|
54
|
+
return await awaitMcpJob(env, {
|
|
55
|
+
jobId: args.jobId,
|
|
56
|
+
workspace: args.workspace,
|
|
57
|
+
waitTimeoutMs: args.waitMs ?? DEFAULT_WAIT_MS,
|
|
58
|
+
signal,
|
|
59
|
+
});
|
|
60
|
+
case "cancel":
|
|
61
|
+
return await cancelMcpJob(env, input);
|
|
62
|
+
case "replay":
|
|
63
|
+
return await replayMcpJob(env, args);
|
|
64
|
+
case "recover_swarm":
|
|
65
|
+
return await recoverMcpSwarm(env, args);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export async function handleMcpSetupTool(env, input) {
|
|
69
|
+
const args = readSetupArguments(input);
|
|
70
|
+
const paths = resolvePluginPaths(env);
|
|
71
|
+
await ensurePluginPaths(paths);
|
|
72
|
+
const workspace = args.workspace !== undefined || env.KIMI_PLUGIN_CC_MCP_WORKSPACE !== undefined
|
|
73
|
+
? await resolveMcpWorkspace(env, args.workspace)
|
|
74
|
+
: undefined;
|
|
75
|
+
const resolvedCwd = workspace?.cwd ?? env.KIMI_PLUGIN_CC_WORKSPACE_CWD ?? process.cwd();
|
|
76
|
+
const context = {
|
|
77
|
+
cwd: resolvedCwd,
|
|
78
|
+
env: pinnedKimiHomeEnv(env, resolvedCwd),
|
|
79
|
+
stdout: process.stdout,
|
|
80
|
+
stderr: process.stderr,
|
|
81
|
+
};
|
|
82
|
+
switch (args.action) {
|
|
83
|
+
case "models": {
|
|
84
|
+
const jsonText = await runModelInventory(["--json"], context);
|
|
85
|
+
return textResult(jsonText, JSON.parse(jsonText), false);
|
|
86
|
+
}
|
|
87
|
+
case "repair_sessions": {
|
|
88
|
+
const repairArgs = [];
|
|
89
|
+
if (args.apply)
|
|
90
|
+
repairArgs.push("--apply");
|
|
91
|
+
if (args.all)
|
|
92
|
+
repairArgs.push("--all");
|
|
93
|
+
const output = await runRepairSessions(repairArgs, context);
|
|
94
|
+
return textResult(output, JSON.parse(output), false);
|
|
95
|
+
}
|
|
96
|
+
case "check":
|
|
97
|
+
case "install":
|
|
98
|
+
case "install_kimi": {
|
|
99
|
+
const setupArgs = args.action === "check"
|
|
100
|
+
? ["--check"]
|
|
101
|
+
: args.action === "install_kimi"
|
|
102
|
+
? ["--install-kimi", "--json"]
|
|
103
|
+
: [];
|
|
104
|
+
const result = await runSetup(setupArgs, context);
|
|
105
|
+
const rendered = renderSetupResult(result);
|
|
106
|
+
const structured = result.kimiInstall
|
|
107
|
+
? { ...result.kimiInstall }
|
|
108
|
+
: {
|
|
109
|
+
action: result.action,
|
|
110
|
+
summary: result.summary,
|
|
111
|
+
config_path: result.configPath,
|
|
112
|
+
hook_script_path: result.hookScriptPath,
|
|
113
|
+
block_written: result.blockWritten,
|
|
114
|
+
block_removed: result.blockRemoved,
|
|
115
|
+
probe: result.probe,
|
|
116
|
+
...(result.probeError ? { probe_error: result.probeError } : {}),
|
|
117
|
+
warnings: result.warnings,
|
|
118
|
+
next_step: result.nextStep,
|
|
119
|
+
};
|
|
120
|
+
return textResult(rendered.endsWith("\n") ? rendered : `${rendered}\n`, structured, result.probe === "failed");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
export async function awaitMcpJob(env, options) {
|
|
125
|
+
const timeoutMs = options.waitTimeoutMs ?? DEFAULT_SYNC_WAIT_TIMEOUT_MS;
|
|
126
|
+
const deadline = Date.now() + timeoutMs;
|
|
127
|
+
let delayMs = 50;
|
|
128
|
+
let pinnedJobId = options.jobId;
|
|
129
|
+
let lastMetadata;
|
|
130
|
+
for (;;) {
|
|
131
|
+
if (options.signal?.aborted) {
|
|
132
|
+
const payload = {
|
|
133
|
+
...(lastMetadata ?? { ...(pinnedJobId ? { job_id: pinnedJobId } : {}), status: "running" }),
|
|
134
|
+
wait_aborted: true,
|
|
135
|
+
};
|
|
136
|
+
return textResult(`${JSON.stringify(payload, null, 2)}\n`, payload, false);
|
|
137
|
+
}
|
|
138
|
+
const { task, job, workspace } = await loadJobSnapshot(env, {
|
|
139
|
+
action: "wait",
|
|
140
|
+
jobId: pinnedJobId,
|
|
141
|
+
workspace: options.workspace,
|
|
142
|
+
});
|
|
143
|
+
const resolvedId = task?.task_id ?? job?.job_id;
|
|
144
|
+
if (pinnedJobId === undefined && resolvedId !== undefined) {
|
|
145
|
+
pinnedJobId = resolvedId;
|
|
146
|
+
}
|
|
147
|
+
lastMetadata = jobMetadata(task, job, workspace.cwd);
|
|
148
|
+
if (!job) {
|
|
149
|
+
if (task?.launch_error)
|
|
150
|
+
return launchFailureResult(task);
|
|
151
|
+
}
|
|
152
|
+
else if (job.status !== "running") {
|
|
153
|
+
return await terminalJobResult(task, job, workspace.cwd);
|
|
154
|
+
}
|
|
155
|
+
const remaining = deadline - Date.now();
|
|
156
|
+
if (remaining <= 0) {
|
|
157
|
+
return textResult(`${JSON.stringify({
|
|
158
|
+
...lastMetadata,
|
|
159
|
+
timed_out_waiting: true,
|
|
160
|
+
message: `Job ${resolvedId ?? "<unknown>"} is still running after waiting ${timeoutMs}ms. Call kimi_job({ action: "wait", job_id: ${JSON.stringify(resolvedId)} }) to continue waiting, or action="cancel" to stop it.`,
|
|
161
|
+
}, null, 2)}\n`, { ...lastMetadata, timed_out_waiting: true }, false);
|
|
162
|
+
}
|
|
163
|
+
await sleepAbortable(Math.min(delayMs, remaining), options.signal);
|
|
164
|
+
delayMs = Math.min(250, delayMs * 2);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async function listMcpJobs(env, args) {
|
|
168
|
+
const workspace = await resolveMcpWorkspace(env, args.workspace);
|
|
169
|
+
const paths = resolvePluginPaths(env);
|
|
170
|
+
await ensurePluginPaths(paths);
|
|
171
|
+
const store = new JobStore(paths);
|
|
172
|
+
try {
|
|
173
|
+
await sweepStaleJobs(store, paths, {
|
|
174
|
+
repoId: workspace.repo.repoId,
|
|
175
|
+
workspaceCwd: workspace.cwd,
|
|
176
|
+
});
|
|
177
|
+
const candidates = store.listJobs({
|
|
178
|
+
repoId: workspace.repo.repoId,
|
|
179
|
+
status: args.statusFilter,
|
|
180
|
+
limit: null,
|
|
181
|
+
});
|
|
182
|
+
const limit = args.limit ?? 20;
|
|
183
|
+
const jobs = [];
|
|
184
|
+
for (const candidate of candidates) {
|
|
185
|
+
if (await isCwdWithinWorkspace(candidate.cwd, workspace.cwd)) {
|
|
186
|
+
jobs.push(candidate);
|
|
187
|
+
if (jobs.length >= limit)
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const projected = jobs.map((job) => ({
|
|
192
|
+
job_id: job.job_id,
|
|
193
|
+
operation_kind: job.operation_kind,
|
|
194
|
+
command_type: job.command_type,
|
|
195
|
+
status: job.status,
|
|
196
|
+
phase: job.phase,
|
|
197
|
+
summary: job.summary,
|
|
198
|
+
created_at: job.created_at,
|
|
199
|
+
updated_at: job.updated_at,
|
|
200
|
+
kimi_session_id: job.kimi_session_id,
|
|
201
|
+
}));
|
|
202
|
+
const payload = {
|
|
203
|
+
workspace: workspace.cwd,
|
|
204
|
+
count: projected.length,
|
|
205
|
+
jobs: projected,
|
|
206
|
+
};
|
|
207
|
+
return textResult(`${JSON.stringify(payload, null, 2)}\n`, payload, false);
|
|
208
|
+
}
|
|
209
|
+
finally {
|
|
210
|
+
store.close();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
12
213
|
export async function getMcpJobStatus(env, input) {
|
|
13
|
-
const
|
|
14
|
-
const
|
|
214
|
+
const args = normalizeControlArgs(input, "status");
|
|
215
|
+
const { task, job, workspace } = await loadJobSnapshot(env, args);
|
|
216
|
+
const metadata = jobMetadata(task, job, workspace.cwd);
|
|
15
217
|
return textResult(`${JSON.stringify(metadata, null, 2)}\n`, metadata);
|
|
16
218
|
}
|
|
17
219
|
export async function getMcpJobResult(env, input) {
|
|
18
|
-
const
|
|
220
|
+
const args = normalizeControlArgs(input, "result");
|
|
221
|
+
const { task, job, workspace } = await loadJobSnapshot(env, args);
|
|
19
222
|
if (!job) {
|
|
20
|
-
if (task
|
|
223
|
+
if (task?.launch_error)
|
|
21
224
|
return launchFailureResult(task);
|
|
22
|
-
throw new RuntimeError("JOB_NOT_TERMINAL", `MCP job ${task.
|
|
225
|
+
throw new RuntimeError("JOB_NOT_TERMINAL", `MCP job ${task?.task_id ?? args.jobId ?? "<unknown>"} is still starting.`, "mcp.result");
|
|
23
226
|
}
|
|
24
227
|
if (job.status === "running") {
|
|
25
228
|
throw new RuntimeError("JOB_NOT_TERMINAL", `Job ${job.job_id} is still running.`, "mcp.result");
|
|
26
229
|
}
|
|
27
|
-
return terminalJobResult(task, job);
|
|
230
|
+
return terminalJobResult(task, job, workspace.cwd);
|
|
28
231
|
}
|
|
29
|
-
export async function cancelMcpJob(env, input) {
|
|
30
|
-
const
|
|
232
|
+
export async function cancelMcpJob(env, input, options) {
|
|
233
|
+
const args = normalizeControlArgs(input, "cancel");
|
|
234
|
+
const { task, job, workspace } = await loadJobSnapshot(env, args, undefined, options);
|
|
235
|
+
const targetId = task?.task_id ?? job?.job_id;
|
|
31
236
|
if (!job) {
|
|
32
|
-
if (task
|
|
237
|
+
if (task?.launch_error)
|
|
33
238
|
return launchFailureResult(task);
|
|
34
|
-
throw new RuntimeError("JOB_NOT_TERMINAL", `MCP job ${
|
|
239
|
+
throw new RuntimeError("JOB_NOT_TERMINAL", `MCP job ${targetId ?? "<unknown>"} has not attached to a companion job.`, "mcp.cancel");
|
|
35
240
|
}
|
|
36
241
|
if (job.status !== "running") {
|
|
37
|
-
const metadata =
|
|
242
|
+
const metadata = jobMetadata(task, job, workspace.cwd);
|
|
38
243
|
return textResult(`${JSON.stringify({ ...metadata, message: "cancel is a no-op for terminal jobs." }, null, 2)}\n`, metadata);
|
|
39
244
|
}
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
245
|
+
const companionIdentity = task ? mcpCompanionIdentity(task) : jobCompanionIdentity(job);
|
|
246
|
+
const kimiIdentity = task ? mcpKimiIdentity(task, job) : jobKimiIdentity(job);
|
|
247
|
+
const output = await runCancel([job.job_id], {
|
|
248
|
+
cwd: workspace.cwd,
|
|
249
|
+
env: pinnedKimiHomeEnv(env, workspace.cwd),
|
|
43
250
|
stdout: process.stdout,
|
|
44
251
|
stderr: process.stderr,
|
|
45
|
-
mcpCompanionIdentity:
|
|
46
|
-
mcpKimiIdentity:
|
|
252
|
+
mcpCompanionIdentity: companionIdentity,
|
|
253
|
+
mcpKimiIdentity: kimiIdentity,
|
|
47
254
|
});
|
|
48
|
-
const current = await
|
|
49
|
-
const metadata =
|
|
255
|
+
const current = await loadJobSnapshot(env, { action: "status", jobId: job.job_id, workspace: workspace.cwd });
|
|
256
|
+
const metadata = jobMetadata(current.task, current.job, workspace.cwd);
|
|
50
257
|
return textResult(output, { ...metadata, cancellation: JSON.parse(output) });
|
|
51
258
|
}
|
|
259
|
+
async function replayMcpJob(env, args) {
|
|
260
|
+
const { job, workspace } = await loadJobSnapshot(env, args);
|
|
261
|
+
if (!job) {
|
|
262
|
+
throw new RuntimeError("JOB_NOT_FOUND", `Job ${args.jobId ?? "<unknown>"} has no recorded job log.`, "mcp.replay");
|
|
263
|
+
}
|
|
264
|
+
const replayed = await replayJob(job);
|
|
265
|
+
const output = `${replayed.rendered}${replayed.rendered.endsWith("\n") ? "" : "\n"}`;
|
|
266
|
+
return textResult(output, {
|
|
267
|
+
job_id: job.job_id,
|
|
268
|
+
workspace: workspace.cwd,
|
|
269
|
+
action: "replay",
|
|
270
|
+
status: job.status,
|
|
271
|
+
operation_kind: job.operation_kind ?? null,
|
|
272
|
+
partial: replayed.partial,
|
|
273
|
+
}, job.status === "failed");
|
|
274
|
+
}
|
|
275
|
+
async function recoverMcpSwarm(env, args) {
|
|
276
|
+
const { job, workspace } = await loadJobSnapshot(env, args);
|
|
277
|
+
if (!job) {
|
|
278
|
+
throw new RuntimeError("JOB_NOT_FOUND", `Job ${args.jobId ?? "<unknown>"} was not found.`, "mcp.recover_swarm");
|
|
279
|
+
}
|
|
280
|
+
const recoverArgs = args.apply ? [job.job_id, "--apply"] : [job.job_id];
|
|
281
|
+
const output = await runRecoverSwarm(recoverArgs, {
|
|
282
|
+
cwd: workspace.cwd,
|
|
283
|
+
env: pinnedKimiHomeEnv(env, workspace.cwd),
|
|
284
|
+
stdout: process.stdout,
|
|
285
|
+
stderr: process.stderr,
|
|
286
|
+
});
|
|
287
|
+
return textResult(output, JSON.parse(output), false);
|
|
288
|
+
}
|
|
52
289
|
export async function getMcpTaskView(env, input) {
|
|
53
290
|
let boundTask;
|
|
54
291
|
try {
|
|
55
|
-
const
|
|
292
|
+
const args = normalizeControlArgs(input, "status");
|
|
293
|
+
const { task, job, workspace } = await loadJobSnapshot(env, args, (found) => { boundTask = found; });
|
|
294
|
+
if (!task) {
|
|
295
|
+
throw new RuntimeError("JOB_NOT_FOUND", `MCP job ${args.jobId ?? "<unknown>"} was not found.`, "mcp.lookup");
|
|
296
|
+
}
|
|
56
297
|
if (!job) {
|
|
57
298
|
if (task.launch_error) {
|
|
58
299
|
return taskView(task, null, "completed", launchFailureResult(task), task.launch_error);
|
|
@@ -63,9 +304,8 @@ export async function getMcpTaskView(env, input) {
|
|
|
63
304
|
case "running":
|
|
64
305
|
return taskView(task, job, "working", undefined, `${task.operation_kind}: ${job.phase ?? "running"}`);
|
|
65
306
|
case "completed":
|
|
66
|
-
return taskView(task, job, "completed", await terminalJobResult(task, job), job.summary);
|
|
67
307
|
case "failed":
|
|
68
|
-
return taskView(task, job, "completed", await terminalJobResult(task, job), job.summary);
|
|
308
|
+
return taskView(task, job, "completed", await terminalJobResult(task, job, workspace.cwd), job.summary);
|
|
69
309
|
case "cancelled":
|
|
70
310
|
return taskView(task, job, "cancelled", undefined, job.summary);
|
|
71
311
|
}
|
|
@@ -92,78 +332,128 @@ export async function getMcpTaskView(env, input) {
|
|
|
92
332
|
throw error;
|
|
93
333
|
}
|
|
94
334
|
}
|
|
95
|
-
|
|
96
|
-
|
|
335
|
+
function normalizeControlArgs(input, defaultAction) {
|
|
336
|
+
if (input && typeof input === "object" && !Array.isArray(input) && "action" in input) {
|
|
337
|
+
return readJobControlArguments(input);
|
|
338
|
+
}
|
|
339
|
+
const raw = (input && typeof input === "object" && !Array.isArray(input) ? input : {});
|
|
340
|
+
return readJobControlArguments({ ...raw, action: defaultAction });
|
|
341
|
+
}
|
|
342
|
+
async function loadBoundTaskSnapshot(store, paths, initialTask, workspace, observeTask) {
|
|
343
|
+
let task = initialTask;
|
|
344
|
+
if (task.workspace_cwd !== workspace.cwd || task.repo_id !== workspace.repo.repoId) {
|
|
345
|
+
throw new RuntimeError("JOB_NOT_FOUND", `MCP job ${task.task_id} was not found in this workspace.`, "mcp.lookup");
|
|
346
|
+
}
|
|
347
|
+
observeTask?.(task);
|
|
348
|
+
let job = store.getJob(task.task_id);
|
|
349
|
+
if (!job) {
|
|
350
|
+
task = await reconcileUnattachedTask(store, task);
|
|
351
|
+
return { task, job: null, workspace };
|
|
352
|
+
}
|
|
353
|
+
await assertMcpJobBinding(task, job);
|
|
354
|
+
if (task.launch_state === "starting") {
|
|
355
|
+
let identity;
|
|
356
|
+
if (job.status === "running") {
|
|
357
|
+
const expected = mcpCompanionIdentity(task);
|
|
358
|
+
if (expected) {
|
|
359
|
+
const validation = await revalidateProcessIdentities([expected]);
|
|
360
|
+
if (!validation.complete) {
|
|
361
|
+
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The companion identity probe was inconclusive; the starting task was not attached or changed.", "mcp.reconcile");
|
|
362
|
+
}
|
|
363
|
+
if (validation.identities.length === 0) {
|
|
364
|
+
const taskUpdatedAt = Date.parse(task.updated_at);
|
|
365
|
+
const jobUpdatedAt = Date.parse(job.updated_at);
|
|
366
|
+
const stale = Number.isFinite(taskUpdatedAt) && Number.isFinite(jobUpdatedAt) &&
|
|
367
|
+
Date.now() - taskUpdatedAt >= STALE_SWEEP_GRACE_MS &&
|
|
368
|
+
Date.now() - jobUpdatedAt >= STALE_SWEEP_GRACE_MS;
|
|
369
|
+
if (!stale) {
|
|
370
|
+
throw new RuntimeError("MCP_PROCESS_IDENTITY_MISMATCH", "The recorded companion process identity no longer matches; the starting task was not attached and no process was adopted.", "mcp.reconcile");
|
|
371
|
+
}
|
|
372
|
+
task = await reconcileStaleCompanion(store, paths, task, job);
|
|
373
|
+
job = store.getJob(task.task_id);
|
|
374
|
+
return { task, job, workspace };
|
|
375
|
+
}
|
|
376
|
+
identity = validation.identities[0];
|
|
377
|
+
}
|
|
378
|
+
else if (job.pid !== null) {
|
|
379
|
+
const updatedAt = Date.parse(task.updated_at);
|
|
380
|
+
if (!Number.isFinite(updatedAt) || Date.now() - updatedAt >= STALE_SWEEP_GRACE_MS) {
|
|
381
|
+
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The starting task has no reusable identity after the attach grace period; refusing to adopt a process by PID alone.", "mcp.reconcile");
|
|
382
|
+
}
|
|
383
|
+
identity = await readProcessIdentity(job.pid);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (job.status !== "running" || identity) {
|
|
387
|
+
task = store.updateMcpTask(task.task_id, {
|
|
388
|
+
launch_state: "attached",
|
|
389
|
+
attached_at: new Date().toISOString(),
|
|
390
|
+
companion_pid: job.pid ?? task.companion_pid,
|
|
391
|
+
...(identity ? { companion_identity: JSON.stringify(identity) } : {}),
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
else if (task.launch_error) {
|
|
396
|
+
throw new RuntimeError("MCP_JOB_BINDING_FAILED", task.launch_error, "mcp.binding");
|
|
397
|
+
}
|
|
398
|
+
if (job.status === "running") {
|
|
399
|
+
task = await refreshCompanionIdentity(store, task, job);
|
|
400
|
+
task = await captureKimiIdentity(store, task, job);
|
|
401
|
+
task = await reconcileStaleCompanion(store, paths, task, job);
|
|
402
|
+
job = store.getJob(task.task_id);
|
|
403
|
+
}
|
|
404
|
+
return { task, job, workspace };
|
|
405
|
+
}
|
|
406
|
+
async function loadJobSnapshot(env, args, observeTask, options) {
|
|
97
407
|
const paths = resolvePluginPaths(env);
|
|
98
408
|
await ensurePluginPaths(paths);
|
|
99
409
|
const store = new JobStore(paths);
|
|
100
410
|
try {
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
|
|
411
|
+
const task = args.jobId ? store.getMcpTask(args.jobId) : null;
|
|
412
|
+
if (task) {
|
|
413
|
+
const workspace = await resolveMcpWorkspace(env, args.workspace ?? task.workspace_cwd);
|
|
414
|
+
return await loadBoundTaskSnapshot(store, paths, task, workspace, observeTask);
|
|
104
415
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
throw new RuntimeError("JOB_NOT_FOUND", `MCP job ${args.jobId} was not found in this workspace.`, "mcp.lookup");
|
|
416
|
+
if (options?.requireMcpTask) {
|
|
417
|
+
throw new RuntimeError("JOB_NOT_FOUND", `MCP task ${args.jobId ?? "<unknown>"} was not found.`, "mcp.lookup");
|
|
108
418
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
419
|
+
// Fallback: inspect CLI-launched or latest repo job contained in the validated workspace.
|
|
420
|
+
const workspace = await resolveMcpWorkspace(env, args.workspace);
|
|
421
|
+
await sweepStaleJobs(store, paths, {
|
|
422
|
+
repoId: workspace.repo.repoId,
|
|
423
|
+
workspaceCwd: workspace.cwd,
|
|
424
|
+
});
|
|
425
|
+
let foundJob = null;
|
|
426
|
+
if (args.jobId) {
|
|
427
|
+
const candidate = store.getJob(args.jobId);
|
|
428
|
+
if (candidate &&
|
|
429
|
+
candidate.repo_id === workspace.repo.repoId &&
|
|
430
|
+
(await isCwdWithinWorkspace(candidate.cwd, workspace.cwd))) {
|
|
431
|
+
foundJob = candidate;
|
|
432
|
+
}
|
|
114
433
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (validation.identities.length === 0) {
|
|
126
|
-
const taskUpdatedAt = Date.parse(task.updated_at);
|
|
127
|
-
const jobUpdatedAt = Date.parse(job.updated_at);
|
|
128
|
-
const stale = Number.isFinite(taskUpdatedAt) && Number.isFinite(jobUpdatedAt) &&
|
|
129
|
-
Date.now() - taskUpdatedAt >= STALE_SWEEP_GRACE_MS &&
|
|
130
|
-
Date.now() - jobUpdatedAt >= STALE_SWEEP_GRACE_MS;
|
|
131
|
-
if (!stale) {
|
|
132
|
-
throw new RuntimeError("MCP_PROCESS_IDENTITY_MISMATCH", "The recorded companion process identity no longer matches; the starting task was not attached and no process was adopted.", "mcp.reconcile");
|
|
133
|
-
}
|
|
134
|
-
task = await reconcileStaleCompanion(store, paths, task, job);
|
|
135
|
-
job = store.getJob(task.task_id);
|
|
136
|
-
return { task, job };
|
|
137
|
-
}
|
|
138
|
-
identity = validation.identities[0];
|
|
139
|
-
}
|
|
140
|
-
else if (job.pid !== null) {
|
|
141
|
-
const updatedAt = Date.parse(task.updated_at);
|
|
142
|
-
if (!Number.isFinite(updatedAt) || Date.now() - updatedAt >= STALE_SWEEP_GRACE_MS) {
|
|
143
|
-
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The starting task has no reusable identity after the attach grace period; refusing to adopt a process by PID alone.", "mcp.reconcile");
|
|
144
|
-
}
|
|
145
|
-
identity = await readProcessIdentity(job.pid);
|
|
434
|
+
else {
|
|
435
|
+
const candidates = store.listJobsByUpdated({
|
|
436
|
+
repoId: workspace.repo.repoId,
|
|
437
|
+
terminalOnly: args.action === "result",
|
|
438
|
+
runningOnly: args.action === "cancel",
|
|
439
|
+
});
|
|
440
|
+
for (const candidate of candidates) {
|
|
441
|
+
if (await isCwdWithinWorkspace(candidate.cwd, workspace.cwd)) {
|
|
442
|
+
foundJob = candidate;
|
|
443
|
+
break;
|
|
146
444
|
}
|
|
147
445
|
}
|
|
148
|
-
if (job.status !== "running" || identity) {
|
|
149
|
-
task = store.updateMcpTask(task.task_id, {
|
|
150
|
-
launch_state: "attached",
|
|
151
|
-
attached_at: new Date().toISOString(),
|
|
152
|
-
companion_pid: job.pid ?? task.companion_pid,
|
|
153
|
-
...(identity ? { companion_identity: JSON.stringify(identity) } : {}),
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
446
|
}
|
|
157
|
-
|
|
158
|
-
throw new RuntimeError("
|
|
447
|
+
if (!foundJob) {
|
|
448
|
+
throw new RuntimeError("JOB_NOT_FOUND", args.jobId
|
|
449
|
+
? `Job ${args.jobId} was not found in this workspace.`
|
|
450
|
+
: "No matching job was found in this workspace.", "mcp.lookup");
|
|
159
451
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
task = await reconcileStaleCompanion(store, paths, task, job);
|
|
164
|
-
job = store.getJob(task.task_id);
|
|
452
|
+
const attachedTask = store.getMcpTask(foundJob.job_id);
|
|
453
|
+
if (attachedTask) {
|
|
454
|
+
return await loadBoundTaskSnapshot(store, paths, attachedTask, workspace, observeTask);
|
|
165
455
|
}
|
|
166
|
-
return { task, job };
|
|
456
|
+
return { task: null, job: foundJob, workspace };
|
|
167
457
|
}
|
|
168
458
|
finally {
|
|
169
459
|
store.close();
|
|
@@ -275,8 +565,8 @@ function signalVerifiedIdentity(identity, signal) {
|
|
|
275
565
|
throw error;
|
|
276
566
|
}
|
|
277
567
|
}
|
|
278
|
-
async function terminalJobResult(task, job) {
|
|
279
|
-
const metadata =
|
|
568
|
+
async function terminalJobResult(task, job, workspaceCwd) {
|
|
569
|
+
const metadata = jobMetadata(task, job, workspaceCwd);
|
|
280
570
|
const body = job.final_output_path
|
|
281
571
|
? await readArtifact(job.final_output_path)
|
|
282
572
|
: `${renderTerminalJobArtifact(job)}\n`;
|
|
@@ -295,8 +585,15 @@ function launchFailureResult(task) {
|
|
|
295
585
|
error: { code: "MCP_CHILD_LAUNCH_FAILED", message },
|
|
296
586
|
}, true);
|
|
297
587
|
}
|
|
298
|
-
function
|
|
588
|
+
function jobMetadata(task, job, workspaceCwd) {
|
|
299
589
|
if (!job) {
|
|
590
|
+
if (!task) {
|
|
591
|
+
return {
|
|
592
|
+
workspace: workspaceCwd,
|
|
593
|
+
status: "failed",
|
|
594
|
+
error: "No job metadata available.",
|
|
595
|
+
};
|
|
596
|
+
}
|
|
300
597
|
return {
|
|
301
598
|
job_id: task.task_id,
|
|
302
599
|
operation_kind: task.operation_kind,
|
|
@@ -310,9 +607,9 @@ function mcpJobMetadata(task, job) {
|
|
|
310
607
|
}
|
|
311
608
|
return {
|
|
312
609
|
job_id: job.job_id,
|
|
313
|
-
operation_kind: job.operation_kind,
|
|
610
|
+
operation_kind: job.operation_kind ?? task?.operation_kind ?? null,
|
|
314
611
|
command_type: job.command_type,
|
|
315
|
-
workspace: task
|
|
612
|
+
workspace: task?.workspace_cwd ?? workspaceCwd,
|
|
316
613
|
status: job.status,
|
|
317
614
|
phase: job.phase,
|
|
318
615
|
summary: job.summary,
|
|
@@ -325,10 +622,11 @@ function mcpJobMetadata(task, job) {
|
|
|
325
622
|
kimi_session_id: job.kimi_session_id,
|
|
326
623
|
kimi_session_id_source: job.kimi_session_id_source,
|
|
327
624
|
hook_escapes: parseHookEscapes(job.hook_escapes),
|
|
625
|
+
...(parseJobWarnings(job.warnings).length > 0 ? { warnings: parseJobWarnings(job.warnings) } : {}),
|
|
328
626
|
error: job.error,
|
|
329
627
|
};
|
|
330
628
|
}
|
|
331
|
-
function taskView(task, job, status, result, statusMessage) {
|
|
629
|
+
export function taskView(task, job, status, result, statusMessage) {
|
|
332
630
|
return {
|
|
333
631
|
taskId: task.task_id,
|
|
334
632
|
status,
|
|
@@ -340,6 +638,3 @@ function taskView(task, job, status, result, statusMessage) {
|
|
|
340
638
|
...(result ? { result: { resultType: "complete", ...result } } : {}),
|
|
341
639
|
};
|
|
342
640
|
}
|
|
343
|
-
function sleep(ms) {
|
|
344
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
345
|
-
}
|
|
@@ -11,6 +11,7 @@ import { markJobFailed } from "../jobs.js";
|
|
|
11
11
|
import { resolveKimiHome } from "../kimi-home.js";
|
|
12
12
|
import { ensurePluginPaths, resolvePluginPaths } from "../paths.js";
|
|
13
13
|
import { readProcessIdentity, revalidateProcessIdentities, } from "../process-tree.js";
|
|
14
|
+
import { sleep } from "../util.js";
|
|
14
15
|
import { assertMcpJobBinding, mcpCompanionIdentity } from "./binding.js";
|
|
15
16
|
import { resolveMcpWorkspace } from "./workspace.js";
|
|
16
17
|
const compiled = import.meta.url.endsWith(".js");
|
|
@@ -71,7 +72,7 @@ export async function launchMcpOperation(invocation, env, options) {
|
|
|
71
72
|
for (;;) {
|
|
72
73
|
if (spawnError)
|
|
73
74
|
throw await markLaunchFailure(store, task, spawnError);
|
|
74
|
-
|
|
75
|
+
let job = store.getJob(taskId);
|
|
75
76
|
if (job && (job.status !== "running" || job.phase === "turn-running" || job.phase === "worker-running")) {
|
|
76
77
|
const currentTask = store.getMcpTask(taskId) ?? task;
|
|
77
78
|
await assertMcpJobBinding(currentTask, job);
|
|
@@ -86,11 +87,19 @@ export async function launchMcpOperation(invocation, env, options) {
|
|
|
86
87
|
preserveStartingTask = true;
|
|
87
88
|
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The attachment identity probe was inconclusive; the launch record was preserved.", "mcp.launch", { details: { job_id: taskId, operation_kind: invocation.operationKind } });
|
|
88
89
|
}
|
|
89
|
-
|
|
90
|
+
// A companion that finished between the job read and the probe
|
|
91
|
+
// has no process left; its terminal row explains the absence.
|
|
92
|
+
const settled = validation.identities.length === 0 ? store.getJob(taskId) : null;
|
|
93
|
+
if (settled && settled.status !== "running") {
|
|
94
|
+
job = settled;
|
|
95
|
+
}
|
|
96
|
+
else if (validation.identities.length === 0) {
|
|
90
97
|
preserveStartingTask = true;
|
|
91
98
|
throw new RuntimeError("MCP_PROCESS_IDENTITY_MISMATCH", "The recorded companion process identity changed before attachment; no process was adopted and the launch record was preserved.", "mcp.launch", { details: { job_id: taskId, operation_kind: invocation.operationKind } });
|
|
92
99
|
}
|
|
93
|
-
|
|
100
|
+
else {
|
|
101
|
+
identity = validation.identities[0];
|
|
102
|
+
}
|
|
94
103
|
}
|
|
95
104
|
else if (child.pid !== undefined) {
|
|
96
105
|
identity = await readIdentity(child.pid);
|
|
@@ -215,7 +224,7 @@ function sanitizedChildEnv(env, paths, workspace) {
|
|
|
215
224
|
...env,
|
|
216
225
|
KIMI_PLUGIN_CC_WORKSPACE_CWD: workspace,
|
|
217
226
|
KIMI_PLUGIN_CC_MCP_WORKSPACE: workspace,
|
|
218
|
-
KIMI_PLUGIN_CC_DATA: path.resolve(paths.
|
|
227
|
+
KIMI_PLUGIN_CC_DATA: path.resolve(paths.dataRoot),
|
|
219
228
|
KIMI_CODE_HOME: resolveKimiHome(env, workspace),
|
|
220
229
|
KIMI_PLUGIN_CC_WORKSPACE_ROOT: undefined,
|
|
221
230
|
KIMI_PLUGIN_CC_CMD: undefined,
|
|
@@ -252,6 +261,3 @@ async function readTail(filePath, maximum = 8_000) {
|
|
|
252
261
|
return "";
|
|
253
262
|
}
|
|
254
263
|
}
|
|
255
|
-
function sleep(ms) {
|
|
256
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
257
|
-
}
|