kimi-plugin-cc 3.0.0 → 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 -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 +277 -262
- 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 +179 -107
- package/dist/hooks/install.js +44 -166
- package/dist/hooks/managed-block.js +44 -95
- 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 +45 -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 +277 -262
- 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 +179 -107
- package/plugins/kimi-codex/dist/hooks/install.js +44 -166
- package/plugins/kimi-codex/dist/hooks/managed-block.js +44 -95
- 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,22 +0,0 @@
|
|
|
1
|
-
import { appendFile, mkdir } from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
export async function writeInvocationLogHeader(logPath, metadata) {
|
|
4
|
-
const line = `${JSON.stringify({
|
|
5
|
-
ts: new Date().toISOString(),
|
|
6
|
-
direction: "meta",
|
|
7
|
-
message: metadata,
|
|
8
|
-
})}\n`;
|
|
9
|
-
await mkdir(path.dirname(logPath), { recursive: true });
|
|
10
|
-
try {
|
|
11
|
-
await appendFile(logPath, line, "utf8");
|
|
12
|
-
}
|
|
13
|
-
catch (error) {
|
|
14
|
-
if (error.code === "ENOENT") {
|
|
15
|
-
// Directory vanished between mkdir and appendFile — recreate and retry once.
|
|
16
|
-
await mkdir(path.dirname(logPath), { recursive: true });
|
|
17
|
-
await appendFile(logPath, line, "utf8");
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
throw error;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { realpath } from "node:fs/promises";
|
|
2
|
-
import { RuntimeError } from "../errors.js";
|
|
3
|
-
export async function assertMcpJobBinding(task, job) {
|
|
4
|
-
if (!job)
|
|
5
|
-
throw bindingError(task, "The supervised job row does not exist.");
|
|
6
|
-
if (job.job_id !== task.task_id)
|
|
7
|
-
throw bindingError(task, "Task and job IDs differ.");
|
|
8
|
-
if (job.operation_kind !== task.operation_kind)
|
|
9
|
-
throw bindingError(task, "Task and job operation kinds differ.");
|
|
10
|
-
if (job.repo_id !== task.repo_id)
|
|
11
|
-
throw bindingError(task, "Task and job repositories differ.");
|
|
12
|
-
if (job.background)
|
|
13
|
-
throw bindingError(task, "Supervised MCP jobs must retain foreground companion ownership.");
|
|
14
|
-
if (job.status === "running" && (task.companion_pid === null || job.pid !== task.companion_pid)) {
|
|
15
|
-
throw bindingError(task, "Task and job companion process IDs differ or are missing.");
|
|
16
|
-
}
|
|
17
|
-
let jobCwd;
|
|
18
|
-
try {
|
|
19
|
-
jobCwd = await realpath(job.cwd);
|
|
20
|
-
}
|
|
21
|
-
catch (error) {
|
|
22
|
-
throw bindingError(task, "The job workspace cannot be resolved.", error);
|
|
23
|
-
}
|
|
24
|
-
if (jobCwd !== task.workspace_cwd)
|
|
25
|
-
throw bindingError(task, "Task and job workspaces differ.");
|
|
26
|
-
return job;
|
|
27
|
-
}
|
|
28
|
-
export function mcpCompanionIdentity(task) {
|
|
29
|
-
return parseIdentity(task.companion_identity, task.companion_pid, task, "companion");
|
|
30
|
-
}
|
|
31
|
-
export function mcpKimiIdentity(task, job) {
|
|
32
|
-
return parseIdentity(task.kimi_identity, job.kimi_pid, task, "Kimi");
|
|
33
|
-
}
|
|
34
|
-
function parseIdentity(serialized, expectedPid, task, label) {
|
|
35
|
-
if (serialized === null)
|
|
36
|
-
return null;
|
|
37
|
-
try {
|
|
38
|
-
const value = JSON.parse(serialized);
|
|
39
|
-
if (!Number.isInteger(value.pid) || (value.pid ?? 0) <= 0 ||
|
|
40
|
-
!Number.isInteger(value.processGroupId) ||
|
|
41
|
-
typeof value.startToken !== "string" || value.startToken.length === 0 ||
|
|
42
|
-
typeof value.state !== "string") {
|
|
43
|
-
throw new Error("invalid identity shape");
|
|
44
|
-
}
|
|
45
|
-
if (expectedPid !== null && expectedPid !== value.pid)
|
|
46
|
-
throw new Error("identity and recorded PID differ");
|
|
47
|
-
return value;
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
throw bindingError(task, `The ${label} process identity is invalid.`, error);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function bindingError(task, message, cause) {
|
|
54
|
-
return new RuntimeError("MCP_JOB_BINDING_FAILED", message, "mcp.binding", {
|
|
55
|
-
...(cause instanceof Error ? { cause } : {}),
|
|
56
|
-
details: { job_id: task.task_id, operation_kind: task.operation_kind },
|
|
57
|
-
});
|
|
58
|
-
}
|
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
import { formatError, RuntimeError } from "../errors.js";
|
|
2
|
-
import { markJobFailed, STALE_SWEEP_GRACE_MS } from "../jobs.js";
|
|
3
|
-
import { JobStore, parseHookEscapes, parseJobWarnings, renderBudgetSource, } from "../job-store.js";
|
|
4
|
-
import { ensurePluginPaths, resolvePluginPaths } from "../paths.js";
|
|
5
|
-
import { readProcessIdentity, revalidateProcessIdentities, revalidateProcessIdentity, } from "../process-tree.js";
|
|
6
|
-
import { readArtifact, renderTerminalJobArtifact } from "../render.js";
|
|
7
|
-
import { runCancel } from "../commands/cancel.js";
|
|
8
|
-
import { sleep } from "../util.js";
|
|
9
|
-
import { assertMcpJobBinding, mcpCompanionIdentity, mcpKimiIdentity } from "./binding.js";
|
|
10
|
-
import { readJobControlArguments } from "./operations.js";
|
|
11
|
-
import { textResult } from "./tool-result.js";
|
|
12
|
-
import { resolveMcpWorkspace } from "./workspace.js";
|
|
13
|
-
export async function getMcpJobStatus(env, input) {
|
|
14
|
-
const { task, job } = await loadMcpJob(env, input);
|
|
15
|
-
const metadata = mcpJobMetadata(task, job);
|
|
16
|
-
return textResult(`${JSON.stringify(metadata, null, 2)}\n`, metadata);
|
|
17
|
-
}
|
|
18
|
-
export async function getMcpJobResult(env, input) {
|
|
19
|
-
const { task, job } = await loadMcpJob(env, input);
|
|
20
|
-
if (!job) {
|
|
21
|
-
if (task.launch_error)
|
|
22
|
-
return launchFailureResult(task);
|
|
23
|
-
throw new RuntimeError("JOB_NOT_TERMINAL", `MCP job ${task.task_id} is still starting.`, "mcp.result");
|
|
24
|
-
}
|
|
25
|
-
if (job.status === "running") {
|
|
26
|
-
throw new RuntimeError("JOB_NOT_TERMINAL", `Job ${job.job_id} is still running.`, "mcp.result");
|
|
27
|
-
}
|
|
28
|
-
return terminalJobResult(task, job);
|
|
29
|
-
}
|
|
30
|
-
export async function cancelMcpJob(env, input) {
|
|
31
|
-
const { task, job } = await loadMcpJob(env, input);
|
|
32
|
-
if (!job) {
|
|
33
|
-
if (task.launch_error)
|
|
34
|
-
return launchFailureResult(task);
|
|
35
|
-
throw new RuntimeError("JOB_NOT_TERMINAL", `MCP job ${task.task_id} has not attached to a companion job.`, "mcp.cancel");
|
|
36
|
-
}
|
|
37
|
-
if (job.status !== "running") {
|
|
38
|
-
const metadata = mcpJobMetadata(task, job);
|
|
39
|
-
return textResult(`${JSON.stringify({ ...metadata, message: "cancel is a no-op for terminal jobs." }, null, 2)}\n`, metadata);
|
|
40
|
-
}
|
|
41
|
-
const output = await runCancel([task.task_id], {
|
|
42
|
-
cwd: task.workspace_cwd,
|
|
43
|
-
env,
|
|
44
|
-
stdout: process.stdout,
|
|
45
|
-
stderr: process.stderr,
|
|
46
|
-
mcpCompanionIdentity: mcpCompanionIdentity(task),
|
|
47
|
-
mcpKimiIdentity: mcpKimiIdentity(task, job),
|
|
48
|
-
});
|
|
49
|
-
const current = await loadMcpJob(env, input);
|
|
50
|
-
const metadata = mcpJobMetadata(current.task, current.job);
|
|
51
|
-
return textResult(output, { ...metadata, cancellation: JSON.parse(output) });
|
|
52
|
-
}
|
|
53
|
-
export async function getMcpTaskView(env, input) {
|
|
54
|
-
let boundTask;
|
|
55
|
-
try {
|
|
56
|
-
const { task, job } = await loadMcpJob(env, input, (found) => { boundTask = found; });
|
|
57
|
-
if (!job) {
|
|
58
|
-
if (task.launch_error) {
|
|
59
|
-
return taskView(task, null, "completed", launchFailureResult(task), task.launch_error);
|
|
60
|
-
}
|
|
61
|
-
return taskView(task, null, "working", undefined, "Starting the supervised companion.");
|
|
62
|
-
}
|
|
63
|
-
switch (job.status) {
|
|
64
|
-
case "running":
|
|
65
|
-
return taskView(task, job, "working", undefined, `${task.operation_kind}: ${job.phase ?? "running"}`);
|
|
66
|
-
case "completed":
|
|
67
|
-
case "failed":
|
|
68
|
-
return taskView(task, job, "completed", await terminalJobResult(task, job), job.summary);
|
|
69
|
-
case "cancelled":
|
|
70
|
-
return taskView(task, job, "cancelled", undefined, job.summary);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
catch (error) {
|
|
74
|
-
if (boundTask && error instanceof RuntimeError && error.code === "MCP_PROCESS_IDENTITY_UNAVAILABLE") {
|
|
75
|
-
return taskView(boundTask, null, "working", undefined, formatError(error));
|
|
76
|
-
}
|
|
77
|
-
if (boundTask && error instanceof RuntimeError && error.code !== "JOB_NOT_FOUND") {
|
|
78
|
-
return {
|
|
79
|
-
taskId: boundTask.task_id,
|
|
80
|
-
status: "failed",
|
|
81
|
-
createdAt: boundTask.created_at,
|
|
82
|
-
lastUpdatedAt: new Date().toISOString(),
|
|
83
|
-
ttlMs: null,
|
|
84
|
-
pollIntervalMs: 2_000,
|
|
85
|
-
error: {
|
|
86
|
-
code: -32_603,
|
|
87
|
-
message: formatError(error),
|
|
88
|
-
data: { job_id: boundTask.task_id, operation_kind: boundTask.operation_kind },
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
throw error;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
async function loadMcpJob(env, input, observeTask) {
|
|
96
|
-
const args = readJobControlArguments(input);
|
|
97
|
-
const paths = resolvePluginPaths(env);
|
|
98
|
-
await ensurePluginPaths(paths);
|
|
99
|
-
const store = new JobStore(paths);
|
|
100
|
-
try {
|
|
101
|
-
let task = store.getMcpTask(args.jobId);
|
|
102
|
-
if (!task) {
|
|
103
|
-
throw new RuntimeError("JOB_NOT_FOUND", `MCP job ${args.jobId} was not found.`, "mcp.lookup");
|
|
104
|
-
}
|
|
105
|
-
const workspace = await resolveMcpWorkspace(env, args.workspace ?? task.workspace_cwd);
|
|
106
|
-
if (task.workspace_cwd !== workspace.cwd || task.repo_id !== workspace.repo.repoId) {
|
|
107
|
-
throw new RuntimeError("JOB_NOT_FOUND", `MCP job ${args.jobId} was not found in this workspace.`, "mcp.lookup");
|
|
108
|
-
}
|
|
109
|
-
observeTask?.(task);
|
|
110
|
-
let job = store.getJob(task.task_id);
|
|
111
|
-
if (!job) {
|
|
112
|
-
task = await reconcileUnattachedTask(store, task);
|
|
113
|
-
return { task, job: null };
|
|
114
|
-
}
|
|
115
|
-
await assertMcpJobBinding(task, job);
|
|
116
|
-
if (task.launch_state === "starting") {
|
|
117
|
-
let identity;
|
|
118
|
-
if (job.status === "running") {
|
|
119
|
-
const expected = mcpCompanionIdentity(task);
|
|
120
|
-
if (expected) {
|
|
121
|
-
const validation = await revalidateProcessIdentities([expected]);
|
|
122
|
-
if (!validation.complete) {
|
|
123
|
-
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The companion identity probe was inconclusive; the starting task was not attached or changed.", "mcp.reconcile");
|
|
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);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
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
|
-
}
|
|
157
|
-
else if (task.launch_error) {
|
|
158
|
-
throw new RuntimeError("MCP_JOB_BINDING_FAILED", task.launch_error, "mcp.binding");
|
|
159
|
-
}
|
|
160
|
-
if (job.status === "running") {
|
|
161
|
-
task = await refreshCompanionIdentity(store, task, job);
|
|
162
|
-
task = await captureKimiIdentity(store, task, job);
|
|
163
|
-
task = await reconcileStaleCompanion(store, paths, task, job);
|
|
164
|
-
job = store.getJob(task.task_id);
|
|
165
|
-
}
|
|
166
|
-
return { task, job };
|
|
167
|
-
}
|
|
168
|
-
finally {
|
|
169
|
-
store.close();
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
async function reconcileUnattachedTask(store, task) {
|
|
173
|
-
if (task.launch_state !== "starting")
|
|
174
|
-
return task;
|
|
175
|
-
const updatedAt = Date.parse(task.updated_at);
|
|
176
|
-
const fresh = Number.isFinite(updatedAt) && Date.now() - updatedAt < STALE_SWEEP_GRACE_MS;
|
|
177
|
-
if (task.companion_identity === null) {
|
|
178
|
-
if (fresh) {
|
|
179
|
-
if (task.companion_pid === null)
|
|
180
|
-
return task;
|
|
181
|
-
const current = await readProcessIdentity(task.companion_pid);
|
|
182
|
-
return current
|
|
183
|
-
? store.updateMcpTask(task.task_id, { companion_identity: JSON.stringify(current) })
|
|
184
|
-
: task;
|
|
185
|
-
}
|
|
186
|
-
if (task.companion_pid !== null) {
|
|
187
|
-
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The unattached companion has no reusable identity; refusing to adopt a process by PID alone.", "mcp.reconcile");
|
|
188
|
-
}
|
|
189
|
-
return store.updateMcpTask(task.task_id, {
|
|
190
|
-
launch_state: "exited",
|
|
191
|
-
launch_error: `No companion process was recorded before creating job ${task.task_id}.`,
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
if (fresh)
|
|
195
|
-
return task;
|
|
196
|
-
const validation = await revalidateProcessIdentities([mcpCompanionIdentity(task)]);
|
|
197
|
-
if (!validation.complete) {
|
|
198
|
-
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The unattached companion identity probe was inconclusive; the task was not changed.", "mcp.reconcile");
|
|
199
|
-
}
|
|
200
|
-
if (validation.identities.length > 0)
|
|
201
|
-
return task;
|
|
202
|
-
return store.updateMcpTask(task.task_id, {
|
|
203
|
-
launch_state: "exited",
|
|
204
|
-
launch_error: `Companion pid ${task.companion_pid} disappeared before creating job ${task.task_id}.`,
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
async function refreshCompanionIdentity(store, task, job) {
|
|
208
|
-
if (task.companion_identity !== null || job.pid === null)
|
|
209
|
-
return task;
|
|
210
|
-
const updatedAt = Date.parse(task.updated_at);
|
|
211
|
-
if (!Number.isFinite(updatedAt) || Date.now() - updatedAt >= STALE_SWEEP_GRACE_MS) {
|
|
212
|
-
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The companion identity is missing after the attach grace period; refusing to adopt a process by PID alone.", "mcp.reconcile");
|
|
213
|
-
}
|
|
214
|
-
const identity = await readProcessIdentity(job.pid);
|
|
215
|
-
if (!identity)
|
|
216
|
-
return task;
|
|
217
|
-
return store.updateMcpTask(task.task_id, {
|
|
218
|
-
companion_pid: job.pid,
|
|
219
|
-
companion_identity: JSON.stringify(identity),
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
async function captureKimiIdentity(store, task, job) {
|
|
223
|
-
if (task.kimi_identity !== null)
|
|
224
|
-
return task;
|
|
225
|
-
const companion = mcpCompanionIdentity(task);
|
|
226
|
-
if (!companion || !(await revalidateProcessIdentity(companion)))
|
|
227
|
-
return task;
|
|
228
|
-
const identity = job.kimi_pid !== null
|
|
229
|
-
? await readProcessIdentity(job.kimi_pid)
|
|
230
|
-
: undefined;
|
|
231
|
-
if (!identity)
|
|
232
|
-
return task;
|
|
233
|
-
return store.updateMcpTask(task.task_id, { kimi_identity: JSON.stringify(identity) });
|
|
234
|
-
}
|
|
235
|
-
async function reconcileStaleCompanion(store, paths, task, job) {
|
|
236
|
-
const updatedAt = Date.parse(job.updated_at);
|
|
237
|
-
if (!Number.isFinite(updatedAt) || Date.now() - updatedAt < STALE_SWEEP_GRACE_MS)
|
|
238
|
-
return task;
|
|
239
|
-
const companion = mcpCompanionIdentity(task);
|
|
240
|
-
if (!companion) {
|
|
241
|
-
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The stale companion has no reusable process identity; refusing PID-only reconciliation.", "mcp.reconcile");
|
|
242
|
-
}
|
|
243
|
-
const validation = await revalidateProcessIdentities([companion]);
|
|
244
|
-
if (!validation.complete) {
|
|
245
|
-
throw new RuntimeError("MCP_PROCESS_IDENTITY_UNAVAILABLE", "The companion process identity probe was inconclusive; no process was signalled and the job was not changed.", "mcp.reconcile");
|
|
246
|
-
}
|
|
247
|
-
if (validation.identities.length > 0)
|
|
248
|
-
return task;
|
|
249
|
-
const orphanWarning = await terminateRecordedKimi(task, job);
|
|
250
|
-
await markJobFailed(store, paths, job, new RuntimeError("MCP_COMPANION_DISAPPEARED", `The supervised companion disappeared before settling the job.${orphanWarning ? ` ${orphanWarning}` : ""}`, "mcp.reconcile", orphanWarning ? { details: { orphan_warning: orphanWarning, kimi_pid: job.kimi_pid } } : undefined), "Supervised MCP companion disappeared before reporting a terminal state.", { phase: "failed" });
|
|
251
|
-
return store.getMcpTask(task.task_id);
|
|
252
|
-
}
|
|
253
|
-
async function terminateRecordedKimi(task, job) {
|
|
254
|
-
const identity = mcpKimiIdentity(task, job);
|
|
255
|
-
if (!identity) {
|
|
256
|
-
return job.kimi_pid === null
|
|
257
|
-
? null
|
|
258
|
-
: `Kimi pid ${job.kimi_pid} was not signalled because no verified identity had been captured.`;
|
|
259
|
-
}
|
|
260
|
-
if (!(await revalidateProcessIdentity(identity)))
|
|
261
|
-
return null;
|
|
262
|
-
signalVerifiedIdentity(identity, "SIGTERM");
|
|
263
|
-
await sleep(1_500);
|
|
264
|
-
if (await revalidateProcessIdentity(identity))
|
|
265
|
-
signalVerifiedIdentity(identity, "SIGKILL");
|
|
266
|
-
return null;
|
|
267
|
-
}
|
|
268
|
-
function signalVerifiedIdentity(identity, signal) {
|
|
269
|
-
try {
|
|
270
|
-
process.kill(identity.processGroupId === identity.pid ? -identity.processGroupId : identity.pid, signal);
|
|
271
|
-
}
|
|
272
|
-
catch (error) {
|
|
273
|
-
const code = error.code;
|
|
274
|
-
if (code !== "ESRCH" && code !== "EPERM")
|
|
275
|
-
throw error;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
async function terminalJobResult(task, job) {
|
|
279
|
-
const metadata = mcpJobMetadata(task, job);
|
|
280
|
-
const body = job.final_output_path
|
|
281
|
-
? await readArtifact(job.final_output_path)
|
|
282
|
-
: `${renderTerminalJobArtifact(job)}\n`;
|
|
283
|
-
return textResult(body, {
|
|
284
|
-
...metadata,
|
|
285
|
-
artifact_path: job.final_output_path,
|
|
286
|
-
partial_output: job.status === "failed" && Number(job.error?.details?.partial_output_chars ?? 0) > 0,
|
|
287
|
-
}, job.status !== "completed");
|
|
288
|
-
}
|
|
289
|
-
function launchFailureResult(task) {
|
|
290
|
-
const message = task.launch_error ?? "The companion failed before creating its job.";
|
|
291
|
-
return textResult(message, {
|
|
292
|
-
job_id: task.task_id,
|
|
293
|
-
operation_kind: task.operation_kind,
|
|
294
|
-
launch_state: task.launch_state,
|
|
295
|
-
error: { code: "MCP_CHILD_LAUNCH_FAILED", message },
|
|
296
|
-
}, true);
|
|
297
|
-
}
|
|
298
|
-
function mcpJobMetadata(task, job) {
|
|
299
|
-
if (!job) {
|
|
300
|
-
return {
|
|
301
|
-
job_id: task.task_id,
|
|
302
|
-
operation_kind: task.operation_kind,
|
|
303
|
-
workspace: task.workspace_cwd,
|
|
304
|
-
status: task.launch_error ? "failed" : "starting",
|
|
305
|
-
launch_state: task.launch_state,
|
|
306
|
-
created_at: task.created_at,
|
|
307
|
-
updated_at: task.updated_at,
|
|
308
|
-
error: task.launch_error,
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
return {
|
|
312
|
-
job_id: job.job_id,
|
|
313
|
-
operation_kind: job.operation_kind,
|
|
314
|
-
command_type: job.command_type,
|
|
315
|
-
workspace: task.workspace_cwd,
|
|
316
|
-
status: job.status,
|
|
317
|
-
phase: job.phase,
|
|
318
|
-
summary: job.summary,
|
|
319
|
-
created_at: job.created_at,
|
|
320
|
-
updated_at: job.updated_at,
|
|
321
|
-
budget_ms: job.budget_ms,
|
|
322
|
-
budget_source: renderBudgetSource(job),
|
|
323
|
-
model: job.model,
|
|
324
|
-
thinking_effort_requested: job.thinking_effort ?? null,
|
|
325
|
-
kimi_session_id: job.kimi_session_id,
|
|
326
|
-
kimi_session_id_source: job.kimi_session_id_source,
|
|
327
|
-
hook_escapes: parseHookEscapes(job.hook_escapes),
|
|
328
|
-
...(parseJobWarnings(job.warnings).length > 0 ? { warnings: parseJobWarnings(job.warnings) } : {}),
|
|
329
|
-
error: job.error,
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
export function taskView(task, job, status, result, statusMessage) {
|
|
333
|
-
return {
|
|
334
|
-
taskId: task.task_id,
|
|
335
|
-
status,
|
|
336
|
-
...(statusMessage ? { statusMessage } : {}),
|
|
337
|
-
createdAt: task.created_at,
|
|
338
|
-
lastUpdatedAt: job?.updated_at ?? task.updated_at,
|
|
339
|
-
ttlMs: null,
|
|
340
|
-
pollIntervalMs: 2_000,
|
|
341
|
-
...(result ? { result: { resultType: "complete", ...result } } : {}),
|
|
342
|
-
};
|
|
343
|
-
}
|