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,293 +0,0 @@
|
|
|
1
|
-
import { allocateJobId } from "../job-id.js";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { createCliCancellationHandlers } from "../cli-cancellation.js";
|
|
4
|
-
import { runCliPromptWithBudget } from "../cli-client.js";
|
|
5
|
-
import { executionPlanFromPersisted, observedExecutionFields, persistedExecutionPlanFields, prepareKimiExecutionPlan, } from "../kimi-engine.js";
|
|
6
|
-
import { RuntimeError } from "../errors.js";
|
|
7
|
-
import { resolveRepoIdentity } from "../git.js";
|
|
8
|
-
import { digestPrompt, markJobCancelled, markJobFailed, sweepStaleJobs } from "../jobs.js";
|
|
9
|
-
import { JobStore } from "../job-store.js";
|
|
10
|
-
import { classifyManagedCommandFailure } from "../kimi-errors.js";
|
|
11
|
-
import { KIMI_PURSUE_DEFAULT_BUDGET_MS } from "../kimi-timeouts.js";
|
|
12
|
-
import { writeInvocationLogHeader } from "../logging.js";
|
|
13
|
-
import { ensurePluginPaths, resolvePluginPaths } from "../paths.js";
|
|
14
|
-
import { parsePursueArgs } from "../parsing.js";
|
|
15
|
-
import { readArtifact, renderManagedJobOutput, writeArtifact } from "../render.js";
|
|
16
|
-
import { assertCliResultSuccess, reassembleProseFromRecords, warnIfSessionIdMissing, warnHookEscapes, hookRefusal } from "./cli-helpers.js";
|
|
17
|
-
import { buildKimiSessionTitle, syncKimiSessionTitle } from "../session-title.js";
|
|
18
|
-
import { shorten } from "../util.js";
|
|
19
|
-
// /kimi:pursue — experimental autonomous goal mode (kimi-code 0.8.0+).
|
|
20
|
-
// Runtime-foreground-only, with a mandatory finite wall-clock budget. No resume:
|
|
21
|
-
// goalId is distinct from sessionId. Rescue lineage and the "rescue" hook label
|
|
22
|
-
// apply the workspace write allowlist on every continuation turn, and the hook
|
|
23
|
-
// is mandatory. Status, result, and cancellation use the same rescue lineage.
|
|
24
|
-
const PURSUE_SUMMARY_MAX = 120;
|
|
25
|
-
const PURSUE_AGENT_PROFILE = "<goal-mode>";
|
|
26
|
-
/**
|
|
27
|
-
* Goal-terminal exits (3 = blocked, 6 = paused) are NOT failures, so
|
|
28
|
-
* `assertCliResultSuccess` never runs for them — which means a native-v2 plan
|
|
29
|
-
* whose child exited 3/6 emitting nothing would otherwise resolve with an
|
|
30
|
-
* empty artifact and unproven provenance (cli-client enforces marker-first on
|
|
31
|
-
* every emitted line and on a clean exit, but not on a silent non-zero exit).
|
|
32
|
-
* Require the marker on every non-aborted goal-terminal result of a v2 plan.
|
|
33
|
-
* (Kimi second-round finding 2.)
|
|
34
|
-
*/
|
|
35
|
-
export function assertGoalRunProvenance(plan, result) {
|
|
36
|
-
if (plan.intendedEngine !== "native-v2" || result.aborted || result.systemVersion !== undefined) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
throw new RuntimeError("CLI_ENGINE_PROVENANCE_MISMATCH", `Refusing pursue result: the execution plan required native-v2, but the child exited ${String(result.exitCode)} without the system.version marker, so the engine that ran cannot be established.`, "pursue.provenance", {
|
|
40
|
-
details: {
|
|
41
|
-
operation_kind: "pursue",
|
|
42
|
-
intended_engine: "native-v2",
|
|
43
|
-
observed_engine: result.observedEngine ?? null,
|
|
44
|
-
exit_code: result.exitCode,
|
|
45
|
-
retryable_after_setup: false,
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Map a headless goal-mode exit code to a terminal status. complete=0 is
|
|
51
|
-
* success; blocked=3 and paused=6 are terminal non-failures; anything else is
|
|
52
|
-
* a genuine process failure. Source:
|
|
53
|
-
* apps/kimi-code/src/cli/goal-prompt.ts::GOAL_EXIT_CODES.
|
|
54
|
-
*/
|
|
55
|
-
export function classifyGoalExit(exitCode) {
|
|
56
|
-
switch (exitCode) {
|
|
57
|
-
case 0:
|
|
58
|
-
return "complete";
|
|
59
|
-
case 3:
|
|
60
|
-
return "blocked";
|
|
61
|
-
case 6:
|
|
62
|
-
return "paused";
|
|
63
|
-
default:
|
|
64
|
-
return "unknown";
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Build the headless goal prompt. The objective becomes the goal; an optional
|
|
69
|
-
* turn cap is appended as a model INSTRUCTION (soft — headless create has no
|
|
70
|
-
* argv/env to set a hard turn budget). The hard bound is always the
|
|
71
|
-
* wall-clock AbortController; goal budget mutation is not authorized.
|
|
72
|
-
*/
|
|
73
|
-
export function buildGoalPrompt(objective, turns) {
|
|
74
|
-
const trimmed = objective.trim().split(/\s+/).join(" ");
|
|
75
|
-
const turnHint = turns !== undefined
|
|
76
|
-
? ` The plugin owns the outer wall-clock budget; do not call SetGoalBudget. Stop after at most ${turns} turns (soft instruction). Unfinished work at the soft stop is not completion.`
|
|
77
|
-
: "";
|
|
78
|
-
if (trimmed.length + turnHint.length > 4_000) {
|
|
79
|
-
throw new RuntimeError("INVALID_ARGS", turns === undefined
|
|
80
|
-
? "pursue objective exceeds Kimi's 4000-character goal limit; shorten the objective."
|
|
81
|
-
: "pursue objective plus --turns guidance exceeds Kimi's 4000-character goal limit; shorten the objective or omit --turns.", "pursue.parse");
|
|
82
|
-
}
|
|
83
|
-
return `/goal -- ${trimmed}${turnHint}`;
|
|
84
|
-
}
|
|
85
|
-
function renderGoalHeader(summary, exitStatus) {
|
|
86
|
-
const status = summary?.status ?? exitStatus;
|
|
87
|
-
const lines = [`**Goal status:** ${status}`];
|
|
88
|
-
if (summary?.reason) {
|
|
89
|
-
lines.push(`**Reason:** ${summary.reason}`);
|
|
90
|
-
}
|
|
91
|
-
const metrics = [];
|
|
92
|
-
if (summary?.turnsUsed != null)
|
|
93
|
-
metrics.push(`turns: ${summary.turnsUsed}`);
|
|
94
|
-
if (summary?.tokensUsed != null)
|
|
95
|
-
metrics.push(`tokens: ${summary.tokensUsed}`);
|
|
96
|
-
if (summary?.wallClockMs != null) {
|
|
97
|
-
metrics.push(`wall-clock: ${Math.round(summary.wallClockMs / 1000)}s`);
|
|
98
|
-
}
|
|
99
|
-
if (metrics.length > 0) {
|
|
100
|
-
lines.push(`**Usage:** ${metrics.join(", ")}`);
|
|
101
|
-
}
|
|
102
|
-
if (summary?.goalId) {
|
|
103
|
-
lines.push(`**Goal id:** \`${summary.goalId}\` (resume is not yet exposed for /kimi:pursue — see docs/safety.md)`);
|
|
104
|
-
}
|
|
105
|
-
return lines.join("\n");
|
|
106
|
-
}
|
|
107
|
-
export async function runPursue(argv, context) {
|
|
108
|
-
const parsed = parsePursueArgs(argv);
|
|
109
|
-
const objective = parsed.objective?.trim();
|
|
110
|
-
if (!objective) {
|
|
111
|
-
throw new RuntimeError("INVALID_ARGS", "/kimi:pursue requires an objective. Usage: /kimi:pursue [--budget 30m] [--turns N] [-m model] <objective>", "pursue.parse");
|
|
112
|
-
}
|
|
113
|
-
const paths = resolvePluginPaths(context.env);
|
|
114
|
-
await ensurePluginPaths(paths);
|
|
115
|
-
const repoIdentity = await resolveRepoIdentity(context.cwd);
|
|
116
|
-
const store = new JobStore(paths);
|
|
117
|
-
try {
|
|
118
|
-
await sweepStaleJobs(store, paths);
|
|
119
|
-
const executionPlan = await prepareKimiExecutionPlan({
|
|
120
|
-
operationKind: "pursue",
|
|
121
|
-
cwd: context.cwd,
|
|
122
|
-
env: context.env,
|
|
123
|
-
stderr: context.stderr,
|
|
124
|
-
});
|
|
125
|
-
const prompt = buildGoalPrompt(objective, parsed.turns);
|
|
126
|
-
const jobId = allocateJobId(context);
|
|
127
|
-
const logPath = path.join(paths.logsDir, `pursue-${jobId}.jsonl`);
|
|
128
|
-
const job = store.createJob({
|
|
129
|
-
job_id: jobId,
|
|
130
|
-
repo_id: repoIdentity.repoId,
|
|
131
|
-
// Reuse the rescue lineage: same write-trust boundary + allowlist.
|
|
132
|
-
command_type: "rescue",
|
|
133
|
-
cwd: context.cwd,
|
|
134
|
-
model: parsed.model ?? null,
|
|
135
|
-
thinking: null,
|
|
136
|
-
thinking_effort: parsed.thinkingEffort ?? null,
|
|
137
|
-
background: false,
|
|
138
|
-
budget_ms: parsed.budgetMs ?? KIMI_PURSUE_DEFAULT_BUDGET_MS,
|
|
139
|
-
budget_source: parsed.budgetMs !== undefined ? "explicit" : "default",
|
|
140
|
-
pid: process.pid,
|
|
141
|
-
kimi_pid: null,
|
|
142
|
-
status: "running",
|
|
143
|
-
kimi_session_id: null,
|
|
144
|
-
...persistedExecutionPlanFields(executionPlan),
|
|
145
|
-
agent_profile: PURSUE_AGENT_PROFILE,
|
|
146
|
-
prompt_digest: digestPrompt(prompt),
|
|
147
|
-
summary: `[pursue] ${shorten(objective, PURSUE_SUMMARY_MAX)}`,
|
|
148
|
-
phase: "starting",
|
|
149
|
-
final_output_path: null,
|
|
150
|
-
stream_log_path: logPath,
|
|
151
|
-
error: null,
|
|
152
|
-
});
|
|
153
|
-
try {
|
|
154
|
-
await writeInvocationLogHeader(logPath, {
|
|
155
|
-
commandType: "pursue",
|
|
156
|
-
kimiSessionId: "(pending)",
|
|
157
|
-
cwd: context.cwd,
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
catch (error) {
|
|
161
|
-
const classified = new RuntimeError("PURSUE_LOG_HEADER_FAILED", `Failed to write pursue invocation log header: ${error.message ?? String(error)}`, "pursue.log-header", error instanceof Error ? { cause: error } : undefined);
|
|
162
|
-
await markJobFailed(store, paths, job, classified, "Pursue failed.", { phase: "failed" });
|
|
163
|
-
throw classified;
|
|
164
|
-
}
|
|
165
|
-
const completed = await executePursueJob(job.job_id, prompt, objective, job.budget_ms, context);
|
|
166
|
-
if (!completed.final_output_path) {
|
|
167
|
-
throw new RuntimeError("PURSUE_RESULT_MISSING", "Pursue finished without a rendered result.", "pursue.result");
|
|
168
|
-
}
|
|
169
|
-
return readArtifact(completed.final_output_path);
|
|
170
|
-
}
|
|
171
|
-
finally {
|
|
172
|
-
store.close();
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
async function executePursueJob(jobId, prompt, objective, budgetMs, context) {
|
|
176
|
-
const paths = resolvePluginPaths(context.env);
|
|
177
|
-
await ensurePluginPaths(paths);
|
|
178
|
-
const store = new JobStore(paths);
|
|
179
|
-
const job = store.getJob(jobId);
|
|
180
|
-
if (!job) {
|
|
181
|
-
store.close();
|
|
182
|
-
throw new RuntimeError("JOB_NOT_FOUND", `Pursue job ${jobId} was not found.`, "pursue.worker");
|
|
183
|
-
}
|
|
184
|
-
// Pursue is write-capable AND autonomous — refuse without the PreToolUse hook,
|
|
185
|
-
// exactly like rescue. The hook gates every tool call in every goal
|
|
186
|
-
// continuation turn; without it, kimi -p auto-approves destructive ops.
|
|
187
|
-
if (context.env.KIMI_PLUGIN_CC_SKIP_HOOK_CHECK !== "1") {
|
|
188
|
-
const classified = await hookRefusal(context, "pursue");
|
|
189
|
-
if (classified) {
|
|
190
|
-
try {
|
|
191
|
-
return await markJobFailed(store, paths, job, classified, "Pursue failed.", { phase: "failed" });
|
|
192
|
-
}
|
|
193
|
-
finally {
|
|
194
|
-
store.close();
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
const handlers = createCliCancellationHandlers();
|
|
199
|
-
try {
|
|
200
|
-
const executionPlan = executionPlanFromPersisted(job);
|
|
201
|
-
store.updateRunningJob(job.job_id, { phase: "turn-running" });
|
|
202
|
-
const result = await runCliPromptWithBudget({
|
|
203
|
-
cwd: job.cwd,
|
|
204
|
-
// Enable headless goal mode for THIS spawn only (per-job env block —
|
|
205
|
-
// never exported to a shell). KIMI_PLUGIN_CC_CMD=rescue is overlaid by
|
|
206
|
-
// cli-client from commandLabel below so the write allowlist applies.
|
|
207
|
-
// The env flag gates goal mode on kimi-code 0.8–0.11; 0.12.0 removed the
|
|
208
|
-
// gate (PR #569) so it is now redundant there but harmless — unknown
|
|
209
|
-
// experimental flag ids resolve to undefined and never error. Kept for
|
|
210
|
-
// 0.8–0.11 compat. The /goal prompt prefix (buildGoalPrompt) is the
|
|
211
|
-
// actual trigger on every supported version.
|
|
212
|
-
env: { ...context.env, KIMI_CODE_EXPERIMENTAL_GOAL_COMMAND: "1" },
|
|
213
|
-
command: executionPlan.command,
|
|
214
|
-
prefixArgs: [...executionPlan.prefixArgs],
|
|
215
|
-
executionPlan,
|
|
216
|
-
prompt,
|
|
217
|
-
commandLabel: "rescue",
|
|
218
|
-
trustedWorkspaceRoot: job.cwd,
|
|
219
|
-
model: job.model ?? undefined,
|
|
220
|
-
thinkingEffort: job.thinking_effort ?? undefined,
|
|
221
|
-
logPath: job.stream_log_path,
|
|
222
|
-
onSpawn: (pid) => { store.updateRunningJob(job.job_id, { kimi_pid: pid }); },
|
|
223
|
-
signal: handlers.signal,
|
|
224
|
-
}, budgetMs, "pursue.prompt");
|
|
225
|
-
store.updateRunningJob(job.job_id, observedExecutionFields(result));
|
|
226
|
-
warnHookEscapes(result, "pursue", context.stderr);
|
|
227
|
-
if (handlers.cancelling) {
|
|
228
|
-
throw new RuntimeError("PURSUE_CANCELLED", "Pursue cancelled by user request.", "pursue.runtime");
|
|
229
|
-
}
|
|
230
|
-
const goalStatus = classifyGoalExit(result.exitCode);
|
|
231
|
-
// 0/3/6 are terminal goal states (complete/blocked/paused) — NOT failures.
|
|
232
|
-
// Any other exit code is a real process failure; assertCliResultSuccess
|
|
233
|
-
// raises the canonical aborted / CLI_NONZERO_EXIT error for it.
|
|
234
|
-
if (goalStatus === "unknown") {
|
|
235
|
-
assertCliResultSuccess(result, "pursue.runtime");
|
|
236
|
-
}
|
|
237
|
-
assertGoalRunProvenance(executionPlan, result);
|
|
238
|
-
if (result.sessionId !== undefined &&
|
|
239
|
-
result.sessionId.length > 0 &&
|
|
240
|
-
result.sessionId !== job.kimi_session_id) {
|
|
241
|
-
store.updateRunningJob(job.job_id, { kimi_session_id_source: "announced", kimi_session_id: result.sessionId });
|
|
242
|
-
}
|
|
243
|
-
await syncKimiSessionTitle({
|
|
244
|
-
promptText: objective,
|
|
245
|
-
env: context.env,
|
|
246
|
-
cwd: job.cwd,
|
|
247
|
-
sessionId: result.sessionId ?? job.kimi_session_id,
|
|
248
|
-
title: buildKimiSessionTitle("pursue", objective),
|
|
249
|
-
stderr: context.stderr,
|
|
250
|
-
});
|
|
251
|
-
warnIfSessionIdMissing(result, "pursue", job.job_id, context.stderr);
|
|
252
|
-
const header = renderGoalHeader(result.goalSummary, goalStatus);
|
|
253
|
-
const prose = reassembleProseFromRecords(result.records);
|
|
254
|
-
const finalText = `${header}\n\n---\n\n${prose}`.trimEnd();
|
|
255
|
-
const rendered = renderManagedJobOutput(job, finalText);
|
|
256
|
-
let artifactPath;
|
|
257
|
-
try {
|
|
258
|
-
artifactPath = await writeArtifact(paths, job, rendered.rendered);
|
|
259
|
-
}
|
|
260
|
-
catch (writeError) {
|
|
261
|
-
context.stderr.write(`[kimi-plugin-cc] pursue artifact write failed for job ${job.job_id}; raw output preserved in error details.\n`);
|
|
262
|
-
const classified = new RuntimeError("PURSUE_ARTIFACT_WRITE_FAILED", `Failed to write pursue artifact: ${writeError.message ?? String(writeError)}`, "pursue.artifact", {
|
|
263
|
-
...(writeError instanceof Error ? { cause: writeError } : {}),
|
|
264
|
-
details: { rawOutput: finalText },
|
|
265
|
-
});
|
|
266
|
-
return await markJobFailed(store, paths, job, classified, "Pursue failed.", { phase: "failed" });
|
|
267
|
-
}
|
|
268
|
-
if (handlers.cancelling) {
|
|
269
|
-
throw new RuntimeError("PURSUE_CANCELLED", "Pursue cancelled after artifact write.", "pursue.runtime");
|
|
270
|
-
}
|
|
271
|
-
const goalSummaryStatus = result.goalSummary?.status ?? goalStatus;
|
|
272
|
-
return (store.markCompleted(job.job_id, {
|
|
273
|
-
summary: `[pursue:${goalSummaryStatus}] ${shorten(objective, PURSUE_SUMMARY_MAX)}`,
|
|
274
|
-
phase: "done",
|
|
275
|
-
final_output_path: artifactPath,
|
|
276
|
-
error: null,
|
|
277
|
-
}) ?? job);
|
|
278
|
-
}
|
|
279
|
-
catch (error) {
|
|
280
|
-
if (handlers.cancelling) {
|
|
281
|
-
const cancelledError = new RuntimeError("PURSUE_CANCELLED", "Pursue cancelled by user request.", "pursue.runtime", error instanceof Error ? { cause: error } : undefined);
|
|
282
|
-
return await markJobCancelled(store, paths, job, "Pursue cancelled by user request.", cancelledError, {
|
|
283
|
-
phase: "cancelled",
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
const classified = classifyManagedCommandFailure(error, "rescue", job.job_id, { preserveStage: true });
|
|
287
|
-
return await markJobFailed(store, paths, job, classified, "Pursue failed.", { phase: "failed" });
|
|
288
|
-
}
|
|
289
|
-
finally {
|
|
290
|
-
handlers.dispose();
|
|
291
|
-
store.close();
|
|
292
|
-
}
|
|
293
|
-
}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { RECOVER_SWARM_SUPPORTED_FLAGS } from "../parsing.js";
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
3
|
-
import { lstat, realpath, writeFile } from "node:fs/promises";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { createCliCancellationHandlers } from "../cli-cancellation.js";
|
|
6
|
-
import { RuntimeError } from "../errors.js";
|
|
7
|
-
import { captureWorktreePatch, resolveRepoIdentity } from "../git.js";
|
|
8
|
-
import { verifyHookInstalled } from "../hooks/install.js";
|
|
9
|
-
import { markJobFailed } from "../jobs.js";
|
|
10
|
-
import { JobStore } from "../job-store.js";
|
|
11
|
-
import { executionPlanFromPersisted, prepareKimiExecutionPlan } from "../kimi-engine.js";
|
|
12
|
-
import { resolveKimiHome } from "../kimi-home.js";
|
|
13
|
-
import { ensurePluginPaths, resolvePluginPaths } from "../paths.js";
|
|
14
|
-
import { probeProcessIdentity } from "../process-tree.js";
|
|
15
|
-
import { deactivateSwarmSession } from "../swarm-activation.js";
|
|
16
|
-
import { assertRecoveryOwnerGone, finishRecoveryRecord, recordRecoveryEvent, recoverLaunch, recoveryError } from "../swarm-recovery.js";
|
|
17
|
-
export async function runRecoverSwarm(argv, context) {
|
|
18
|
-
const positional = argv.filter((arg) => !arg.startsWith("-"));
|
|
19
|
-
if (positional.length !== 1 || argv.some((arg) => arg.startsWith("-") && arg !== RECOVER_SWARM_SUPPORTED_FLAGS) || argv.filter((arg) => arg === RECOVER_SWARM_SUPPORTED_FLAGS).length > 1) {
|
|
20
|
-
throw new RuntimeError("INVALID_ARGS", `Usage: companion.sh recover-swarm <job-id> [${RECOVER_SWARM_SUPPORTED_FLAGS}]. Defaults to preview.`, "swarm.recovery.args");
|
|
21
|
-
}
|
|
22
|
-
const jobId = positional[0];
|
|
23
|
-
const apply = argv.includes(RECOVER_SWARM_SUPPORTED_FLAGS);
|
|
24
|
-
const paths = resolvePluginPaths(context.env);
|
|
25
|
-
await ensurePluginPaths(paths);
|
|
26
|
-
const repo = await resolveRepoIdentity(context.cwd);
|
|
27
|
-
const store = new JobStore(paths);
|
|
28
|
-
let claim;
|
|
29
|
-
const handlers = createCliCancellationHandlers();
|
|
30
|
-
const signal = AbortSignal.any([handlers.signal, AbortSignal.timeout(30_000)]);
|
|
31
|
-
try {
|
|
32
|
-
const job = store.getJob(jobId);
|
|
33
|
-
const saved = store.getSwarmRecovery(jobId);
|
|
34
|
-
if (!job || job.repo_id !== repo.repoId || !saved || saved.record.schema !== 1 || saved.record.jobId !== jobId) {
|
|
35
|
-
throw recoveryError("No recovery record owned by this repository. Older runs require manual inspection.");
|
|
36
|
-
}
|
|
37
|
-
let record = saved.record;
|
|
38
|
-
const summary = () => ({ job_id: jobId, mode: apply ? "apply" : "preview", recovery_complete: record.complete,
|
|
39
|
-
session_id: record.session?.sessionId ?? null, session_cleanup_pending: record.sessionDirty,
|
|
40
|
-
launches: record.launches, worktree: record.workspaceRoot, patch_path: record.patchPath,
|
|
41
|
-
next_step: record.complete ? "No recovery is needed. Retained worktrees may be inspected and removed manually."
|
|
42
|
-
: `Inspect this record; use companion.sh recover-swarm ${jobId} --apply only after the original job has stopped.` });
|
|
43
|
-
const settleAbandonedJob = async () => {
|
|
44
|
-
if (job.status === "running")
|
|
45
|
-
await markJobFailed(store, paths, job, new RuntimeError("SWARM_OWNER_DISAPPEARED", "The original swarm companion stopped without reporting a result; resource recovery completed, not the task.", "swarm.recovery"), "Abandoned swarm resources recovered; task did not complete.", { phase: "failed" });
|
|
46
|
-
};
|
|
47
|
-
if (!apply)
|
|
48
|
-
return `${JSON.stringify(summary(), null, 2)}\n`;
|
|
49
|
-
if (record.complete) {
|
|
50
|
-
if (job.status === "running") {
|
|
51
|
-
await assertRecoveryOwnerGone(record);
|
|
52
|
-
await settleAbandonedJob();
|
|
53
|
-
}
|
|
54
|
-
return `${JSON.stringify(summary(), null, 2)}\n`;
|
|
55
|
-
}
|
|
56
|
-
await assertRecoveryOwnerGone(record);
|
|
57
|
-
if (saved.owner !== null) {
|
|
58
|
-
const owner = JSON.parse(saved.owner);
|
|
59
|
-
const live = await probeProcessIdentity(owner.pid);
|
|
60
|
-
if (!live.complete || live.identity?.startToken === owner.startToken)
|
|
61
|
-
throw recoveryError("Another recovery owner is alive or cannot be verified.");
|
|
62
|
-
}
|
|
63
|
-
const self = await probeProcessIdentity(process.pid);
|
|
64
|
-
if (!self.complete || !self.identity)
|
|
65
|
-
throw recoveryError("Cannot establish recovery ownership on this host.");
|
|
66
|
-
claim = JSON.stringify(self.identity);
|
|
67
|
-
if (!store.claimSwarmRecovery(jobId, saved.owner, claim)) {
|
|
68
|
-
claim = undefined;
|
|
69
|
-
throw recoveryError("Another recovery acquired this job.");
|
|
70
|
-
}
|
|
71
|
-
// Another owner may have completed and released the claim between our first read
|
|
72
|
-
// and the CAS (null -> owner -> null). Never overwrite that newer journal.
|
|
73
|
-
record = store.getSwarmRecovery(jobId).record;
|
|
74
|
-
if (record.complete) {
|
|
75
|
-
await settleAbandonedJob();
|
|
76
|
-
return `${JSON.stringify(summary(), null, 2)}\n`;
|
|
77
|
-
}
|
|
78
|
-
const expectedRoot = job.operation_kind === "swarm-write"
|
|
79
|
-
? path.join(paths.worktreesDir, job.repo_id, `swarm-write-${job.job_id}`) : null;
|
|
80
|
-
if (record.workspaceRoot !== expectedRoot || record.cwd !== (expectedRoot ?? job.cwd)
|
|
81
|
-
|| record.kimiHome !== resolveKimiHome(context.env, record.cwd)) {
|
|
82
|
-
throw recoveryError("Recovery workspace or Kimi home differs from the recorded job. Use its original home and repository; do not guess a session.");
|
|
83
|
-
}
|
|
84
|
-
if (expectedRoot) {
|
|
85
|
-
const st = await lstat(expectedRoot);
|
|
86
|
-
if (!st.isDirectory() || st.isSymbolicLink())
|
|
87
|
-
throw recoveryError("Recovery worktree must be the original non-symlink directory.");
|
|
88
|
-
const parent = await realpath(path.dirname(expectedRoot));
|
|
89
|
-
if (await realpath(expectedRoot) !== path.join(parent, path.basename(expectedRoot)))
|
|
90
|
-
throw recoveryError("Recovery worktree path is redirected.");
|
|
91
|
-
}
|
|
92
|
-
const persist = () => store.saveSwarmRecovery(jobId, record);
|
|
93
|
-
for (const launch of record.launches)
|
|
94
|
-
await recoverLaunch(launch, persist, signal);
|
|
95
|
-
if (record.sessionDirty) {
|
|
96
|
-
if (!record.session || !/^[\w-]+$/.test(record.session.sessionId) || !/^[\w-]+$/.test(record.session.workspaceId))
|
|
97
|
-
throw recoveryError("Recovery session identity is missing or invalid.");
|
|
98
|
-
const installed = await verifyHookInstalled(context.env);
|
|
99
|
-
if (!installed.installed)
|
|
100
|
-
throw recoveryError(`Managed hook verification failed: ${installed.reason ?? "unknown"}. Recovery does not install or relax hooks.`);
|
|
101
|
-
const plan = executionPlanFromPersisted(job);
|
|
102
|
-
const current = await prepareKimiExecutionPlan({ operationKind: plan.operationKind, cwd: record.cwd, env: context.env });
|
|
103
|
-
if (current.command !== plan.command || current.kimiVersion !== plan.kimiVersion || JSON.stringify(current.prefixArgs) !== JSON.stringify(plan.prefixArgs))
|
|
104
|
-
throw recoveryError("The exact recorded CLI is not selected. Restore that certified binary before session cleanup.");
|
|
105
|
-
signal.throwIfAborted();
|
|
106
|
-
const recoveryExecutionCwd = await realpath(record.cwd);
|
|
107
|
-
await deactivateSwarmSession({ cwd: recoveryExecutionCwd, env: context.env, command: current.command,
|
|
108
|
-
prefixArgs: current.prefixArgs, executionPlan: current, commandLabel: expectedRoot ? "swarm-write" : "swarm",
|
|
109
|
-
// Recovery only deactivates the stopped session; it never starts a model
|
|
110
|
-
// turn. Use the strictest admission policy rather than reconstructing a
|
|
111
|
-
// prior wave limit that recovery records do not need to retain.
|
|
112
|
-
swarmMaxConcurrency: 1,
|
|
113
|
-
trustedWorkspaceRoot: expectedRoot ?? undefined, ...record.session, signal,
|
|
114
|
-
onRecoveryEvent: (event) => recordRecoveryEvent(store, record, event) });
|
|
115
|
-
}
|
|
116
|
-
signal.throwIfAborted();
|
|
117
|
-
if (expectedRoot) {
|
|
118
|
-
// Capture without applying or deleting. A crash after the write leaves a recoverable artifact.
|
|
119
|
-
const patch = await captureWorktreePatch(expectedRoot, signal);
|
|
120
|
-
const patchPath = path.join(paths.artifactsDir, `swarm-recovered-${jobId}-${randomUUID()}.patch`);
|
|
121
|
-
await writeFile(patchPath, patch, { flag: "wx", mode: 0o600 });
|
|
122
|
-
record.patchPath = patchPath;
|
|
123
|
-
}
|
|
124
|
-
finishRecoveryRecord(store, record);
|
|
125
|
-
if (!record.complete)
|
|
126
|
-
throw recoveryError("Recovery remains incomplete; evidence and worktree were retained.");
|
|
127
|
-
await settleAbandonedJob();
|
|
128
|
-
return `${JSON.stringify(summary(), null, 2)}\n`;
|
|
129
|
-
}
|
|
130
|
-
finally {
|
|
131
|
-
handlers.dispose();
|
|
132
|
-
try {
|
|
133
|
-
if (claim !== undefined)
|
|
134
|
-
store.claimSwarmRecovery(jobId, claim, null);
|
|
135
|
-
}
|
|
136
|
-
finally {
|
|
137
|
-
store.close();
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { RuntimeError } from "../errors.js";
|
|
2
|
-
const passthroughOutput = "passthrough";
|
|
3
|
-
const parsedOutput = "parsed";
|
|
4
|
-
const DEFAULT_ESCALATION_MS = 1_500;
|
|
5
|
-
/**
|
|
6
|
-
* Build cancellation config from the display name used verbatim in messages
|
|
7
|
-
* and the uppercase stem used in error codes.
|
|
8
|
-
*/
|
|
9
|
-
function makeCancellationConfig(prefix) {
|
|
10
|
-
const { displayName, errorCodeStem } = prefix;
|
|
11
|
-
return {
|
|
12
|
-
escalationMs: DEFAULT_ESCALATION_MS,
|
|
13
|
-
errorCodes: {
|
|
14
|
-
cancelledDuringStart: `${errorCodeStem}_CANCELLED_DURING_START`,
|
|
15
|
-
cancelled: `${errorCodeStem}_CANCELLED`,
|
|
16
|
-
},
|
|
17
|
-
cancelMessages: {
|
|
18
|
-
duringStart: `${displayName} cancelled during startup.`,
|
|
19
|
-
afterPrompt: `${displayName} cancelled by user request after prompt completion.`,
|
|
20
|
-
afterArtifact: `${displayName} cancelled by user request after artifact write.`,
|
|
21
|
-
default: `${displayName} cancelled by user request.`,
|
|
22
|
-
},
|
|
23
|
-
cancelledSummary: `${displayName} cancelled by user request.`,
|
|
24
|
-
failedSummary: `${displayName} failed.`,
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Single source of truth for per-command behavior. Only commands using the
|
|
29
|
-
* managed-cancellation flow participate; `setup` and `task` do not.
|
|
30
|
-
*
|
|
31
|
-
* Display-name casing is part of the user-visible contract: `Ask` and `Rescue`
|
|
32
|
-
* are title-cased, while `review`, `challenge`, and `review_gate` are lowercase.
|
|
33
|
-
*/
|
|
34
|
-
export const COMMAND_REGISTRY = {
|
|
35
|
-
ask: {
|
|
36
|
-
outputMode: passthroughOutput,
|
|
37
|
-
cancellation: makeCancellationConfig({ displayName: "Ask", errorCodeStem: "ASK" }),
|
|
38
|
-
},
|
|
39
|
-
review: {
|
|
40
|
-
outputMode: passthroughOutput,
|
|
41
|
-
cancellation: makeCancellationConfig({ displayName: "review", errorCodeStem: "REVIEW" }),
|
|
42
|
-
},
|
|
43
|
-
challenge: {
|
|
44
|
-
outputMode: passthroughOutput,
|
|
45
|
-
cancellation: makeCancellationConfig({ displayName: "challenge", errorCodeStem: "CHALLENGE" }),
|
|
46
|
-
},
|
|
47
|
-
rescue: {
|
|
48
|
-
outputMode: passthroughOutput,
|
|
49
|
-
cancellation: makeCancellationConfig({ displayName: "Rescue", errorCodeStem: "RESCUE" }),
|
|
50
|
-
},
|
|
51
|
-
review_gate: {
|
|
52
|
-
outputMode: parsedOutput,
|
|
53
|
-
cancellation: makeCancellationConfig({ displayName: "review_gate", errorCodeStem: "REVIEW_GATE" }),
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
/**
|
|
57
|
-
* Convenience accessor for managed commands (review/challenge/rescue/
|
|
58
|
-
* review_gate/ask). Identical to direct lookup, with a defensive
|
|
59
|
-
* runtime guard: TypeScript narrows `commandType` to `ManagedCommandType`,
|
|
60
|
-
* but a stale SQLite row, corrupted job artifact, or future command
|
|
61
|
-
* type added without a registry entry could still reach here at
|
|
62
|
-
* runtime. A bare `COMMAND_REGISTRY[commandType]` would return
|
|
63
|
-
* `undefined` and surface as an unclassified `TypeError` at the
|
|
64
|
-
* destructure call site. Throwing a classified `RuntimeError` here
|
|
65
|
-
* keeps the failure path within the command-layer error model so
|
|
66
|
-
* /kimi:status and /kimi:result render it correctly.
|
|
67
|
-
*/
|
|
68
|
-
export function getManagedCommandConfig(commandType) {
|
|
69
|
-
const config = COMMAND_REGISTRY[commandType];
|
|
70
|
-
if (!config) {
|
|
71
|
-
throw new RuntimeError("UNKNOWN_COMMAND_TYPE", `No registry entry for command type "${String(commandType)}". This indicates a stale job record or an unregistered command.`, "commands.registry");
|
|
72
|
-
}
|
|
73
|
-
return config;
|
|
74
|
-
}
|