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,597 +0,0 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
|
-
import { readdir, readFile } from "node:fs/promises";
|
|
3
|
-
import { promisify } from "node:util";
|
|
4
|
-
import { sleep } from "./util.js";
|
|
5
|
-
const execFileAsync = promisify(execFile);
|
|
6
|
-
const MAX_DESCENDANT_DEPTH = 8;
|
|
7
|
-
const MAX_DESCENDANT_PIDS = 512;
|
|
8
|
-
const PROCESS_EXIT_POLL_MS = 10;
|
|
9
|
-
const PROCESS_SNAPSHOT_TIMEOUT_MS = 1_000;
|
|
10
|
-
const PROCESS_IDENTITY_PROBE_TIMEOUT_MS = 1_000;
|
|
11
|
-
const PROBE_TIMEOUT = Symbol("process-probe-timeout");
|
|
12
|
-
/**
|
|
13
|
-
* Capture descendant ancestry and reusable identities in the same bounded
|
|
14
|
-
* process-table snapshot. There is no PID-only handoff: on Linux, PPID, PGID,
|
|
15
|
-
* state, and start time come from one /proc/<pid>/stat read per row; other
|
|
16
|
-
* POSIX systems use one ps invocation for the whole table. Retained anchors
|
|
17
|
-
* supply expectedRoot so a reused PID cannot introduce unrelated descendants.
|
|
18
|
-
*/
|
|
19
|
-
export async function collectDescendantIdentities(rootPid, expectedRoot) {
|
|
20
|
-
if (!Number.isInteger(rootPid) || rootPid <= 0 || process.platform === "win32")
|
|
21
|
-
return [];
|
|
22
|
-
return snapshotWithinTimeout(collectVerifiedDescendantIdentities(rootPid, expectedRoot));
|
|
23
|
-
}
|
|
24
|
-
/** Collect owned roots and descendants with one shared table and targeted verification. */
|
|
25
|
-
export async function collectDescendantIdentitiesForRoots(expectedRoots) {
|
|
26
|
-
if (expectedRoots.length === 0 || process.platform === "win32")
|
|
27
|
-
return [];
|
|
28
|
-
return snapshotWithinTimeout(collectVerifiedDescendantIdentitiesForRoots(expectedRoots));
|
|
29
|
-
}
|
|
30
|
-
async function snapshotWithinTimeout(collect) {
|
|
31
|
-
const snapshot = await raceValueWithTimeout(collect, PROCESS_SNAPSHOT_TIMEOUT_MS, PROBE_TIMEOUT);
|
|
32
|
-
if (snapshot === PROBE_TIMEOUT) {
|
|
33
|
-
throw new Error(`process snapshot exceeded ${PROCESS_SNAPSHOT_TIMEOUT_MS}ms`);
|
|
34
|
-
}
|
|
35
|
-
return snapshot;
|
|
36
|
-
}
|
|
37
|
-
const INCOMPLETE_CAPTURE_RETRY_DELAYS_MS = [5, 20];
|
|
38
|
-
/**
|
|
39
|
-
* A capture that overlaps a process exit can read a row in mid-release and
|
|
40
|
-
* come back incomplete, and one incomplete capture makes a whole teardown
|
|
41
|
-
* unverifiable. Look again at once: a root that has gone then reads as absent,
|
|
42
|
-
* which is a complete observation, while a table that stays unreadable still
|
|
43
|
-
* fails. A capture that timed out is not retried.
|
|
44
|
-
*/
|
|
45
|
-
export async function retryIncompleteCapture(capture, retryDelaysMs = INCOMPLETE_CAPTURE_RETRY_DELAYS_MS) {
|
|
46
|
-
let snapshot = await capture();
|
|
47
|
-
for (const delayMs of retryDelaysMs) {
|
|
48
|
-
if (snapshot.complete || snapshot.timedOut === true)
|
|
49
|
-
break;
|
|
50
|
-
await sleep(delayMs);
|
|
51
|
-
snapshot = await capture();
|
|
52
|
-
}
|
|
53
|
-
return snapshot;
|
|
54
|
-
}
|
|
55
|
-
/** Return current members of the selected process groups, or undefined when unreadable. */
|
|
56
|
-
export async function collectProcessGroupsIdentities(processGroupIds) {
|
|
57
|
-
if (process.platform === "win32")
|
|
58
|
-
return undefined;
|
|
59
|
-
const groups = new Set(processGroupIds.filter((id) => Number.isInteger(id) && id > 0));
|
|
60
|
-
if (groups.size === 0)
|
|
61
|
-
return [];
|
|
62
|
-
const rows = await raceValueWithTimeout(collectProcessSnapshotRows(), PROCESS_SNAPSHOT_TIMEOUT_MS, PROBE_TIMEOUT);
|
|
63
|
-
if (rows === PROBE_TIMEOUT || rows === undefined)
|
|
64
|
-
return undefined;
|
|
65
|
-
return rows.filter((row) => groups.has(row.processGroupId)).map(toProcessIdentity);
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Compatibility surface for callers that only need numeric process listings.
|
|
69
|
-
* Cancellation must use collectDescendantIdentities so ancestry and identity
|
|
70
|
-
* cannot be separated by a PID-reuse window.
|
|
71
|
-
*/
|
|
72
|
-
export async function collectDescendants(rootPid) {
|
|
73
|
-
try {
|
|
74
|
-
return (await collectDescendantIdentities(rootPid)).map((identity) => identity.pid);
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
return [];
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
/** Distinguish a missing process from a failed or timed-out identity probe. */
|
|
81
|
-
export async function probeProcessIdentity(pid) {
|
|
82
|
-
if (!Number.isInteger(pid) || pid <= 0 || process.platform === "win32")
|
|
83
|
-
return { complete: true };
|
|
84
|
-
const read = process.platform === "linux"
|
|
85
|
-
? readProcessIdentityBatchFromProc([pid])
|
|
86
|
-
: readProcessIdentityBatchWithPs([pid]);
|
|
87
|
-
const batch = await raceValueWithTimeout(read, PROCESS_IDENTITY_PROBE_TIMEOUT_MS, PROBE_TIMEOUT);
|
|
88
|
-
if (batch === PROBE_TIMEOUT)
|
|
89
|
-
return { complete: false };
|
|
90
|
-
return { identity: batch.identities.get(pid), complete: batch.complete };
|
|
91
|
-
}
|
|
92
|
-
/** Read one reusable process identity. PID alone is not stable across teardown. */
|
|
93
|
-
export async function readProcessIdentity(pid) {
|
|
94
|
-
return (await probeProcessIdentity(pid)).identity;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Revalidate all captured identities concurrently under one deadline. The
|
|
98
|
-
* default path batches ps fallback reads; an injected per-pid reader is fanned
|
|
99
|
-
* out with Promise.all rather than multiplied by descendant count.
|
|
100
|
-
*/
|
|
101
|
-
export async function revalidateProcessIdentities(expectedIdentities, options = {}) {
|
|
102
|
-
const expected = dedupeIdentities(expectedIdentities);
|
|
103
|
-
if (expected.length === 0)
|
|
104
|
-
return { identities: [], complete: true };
|
|
105
|
-
const timeoutMs = Math.max(0, options.timeoutMs ?? PROCESS_IDENTITY_PROBE_TIMEOUT_MS);
|
|
106
|
-
const read = options.identityReader === undefined
|
|
107
|
-
? readCurrentIdentityBatch(expected)
|
|
108
|
-
: readIdentityBatchWithReader(expected, options.identityReader);
|
|
109
|
-
const batch = await raceValueWithTimeout(read, timeoutMs, PROBE_TIMEOUT);
|
|
110
|
-
if (batch === PROBE_TIMEOUT)
|
|
111
|
-
return { identities: [], complete: false };
|
|
112
|
-
const identities = [];
|
|
113
|
-
const groupChanged = [];
|
|
114
|
-
for (const expectedIdentity of expected) {
|
|
115
|
-
const current = batch.identities.get(expectedIdentity.pid);
|
|
116
|
-
if (current === undefined ||
|
|
117
|
-
current.startToken !== expectedIdentity.startToken ||
|
|
118
|
-
isQuiescentState(current.state)) {
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
if (current.processGroupId !== expectedIdentity.processGroupId) {
|
|
122
|
-
groupChanged.push(current);
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
identities.push(current);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return {
|
|
129
|
-
identities,
|
|
130
|
-
complete: batch.complete,
|
|
131
|
-
...(groupChanged.length > 0 ? { groupChanged } : {}),
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
/** Return the current identity only when pid, start token, and PGID still match. */
|
|
135
|
-
export async function revalidateProcessIdentity(expected, identityReader) {
|
|
136
|
-
const validation = await revalidateProcessIdentities([expected], { identityReader });
|
|
137
|
-
return validation.identities[0];
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Wait until the direct child and every captured identity are quiescent.
|
|
141
|
-
* Every probe is bounded even when the overall wait is infinite, and an
|
|
142
|
-
* incomplete probe is conservatively treated as still active.
|
|
143
|
-
*/
|
|
144
|
-
export async function waitForProcessTreeExit(identities, timeoutMs, options = {}) {
|
|
145
|
-
const targets = dedupeIdentities(identities);
|
|
146
|
-
const finiteTimeout = Number.isFinite(timeoutMs);
|
|
147
|
-
const deadline = finiteTimeout ? Date.now() + Math.max(0, timeoutMs) : Number.POSITIVE_INFINITY;
|
|
148
|
-
while (true) {
|
|
149
|
-
const rootAlive = options.isRootAlive?.() === true;
|
|
150
|
-
if (!rootAlive && targets.length === 0)
|
|
151
|
-
return true;
|
|
152
|
-
const remainingMs = deadline - Date.now();
|
|
153
|
-
if (finiteTimeout && remainingMs <= 0)
|
|
154
|
-
return false;
|
|
155
|
-
let active = rootAlive;
|
|
156
|
-
if (!active) {
|
|
157
|
-
const probeTimeoutMs = finiteTimeout
|
|
158
|
-
? Math.min(PROCESS_IDENTITY_PROBE_TIMEOUT_MS, remainingMs)
|
|
159
|
-
: PROCESS_IDENTITY_PROBE_TIMEOUT_MS;
|
|
160
|
-
const validation = await revalidateProcessIdentities(targets, {
|
|
161
|
-
identityReader: options.identityReader,
|
|
162
|
-
timeoutMs: probeTimeoutMs,
|
|
163
|
-
});
|
|
164
|
-
active = !validation.complete || validation.identities.length > 0 || (validation.groupChanged?.length ?? 0) > 0;
|
|
165
|
-
}
|
|
166
|
-
if (!active)
|
|
167
|
-
return true;
|
|
168
|
-
const afterProbeRemainingMs = deadline - Date.now();
|
|
169
|
-
if (finiteTimeout && afterProbeRemainingMs <= 0)
|
|
170
|
-
return false;
|
|
171
|
-
await sleep(Math.min(PROCESS_EXIT_POLL_MS, afterProbeRemainingMs));
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
async function collectVerifiedDescendantIdentities(rootPid, expectedRoot) {
|
|
175
|
-
if (expectedRoot !== undefined && expectedRoot.pid !== rootPid) {
|
|
176
|
-
return [];
|
|
177
|
-
}
|
|
178
|
-
const initialRows = await collectProcessSnapshotRows();
|
|
179
|
-
if (initialRows === undefined)
|
|
180
|
-
throw new Error("process snapshot unavailable");
|
|
181
|
-
const initialRoot = initialRows.find((row) => row.pid === rootPid);
|
|
182
|
-
if (initialRoot === undefined)
|
|
183
|
-
return [];
|
|
184
|
-
assertSignalableSnapshotRows([initialRoot]);
|
|
185
|
-
if (expectedRoot !== undefined &&
|
|
186
|
-
(initialRoot.startToken !== expectedRoot.startToken ||
|
|
187
|
-
initialRoot.processGroupId !== expectedRoot.processGroupId)) {
|
|
188
|
-
return [];
|
|
189
|
-
}
|
|
190
|
-
const candidateRows = descendantRowsFromSnapshot(rootPid, initialRows);
|
|
191
|
-
assertSignalableSnapshotRows(candidateRows);
|
|
192
|
-
if (candidateRows.length === 0)
|
|
193
|
-
return [];
|
|
194
|
-
// A second, targeted snapshot closes the cross-row /proc race: each child
|
|
195
|
-
// must retain its PID and start token. Reparenting and PGID changes are not exit.
|
|
196
|
-
const expectedRows = [initialRoot, ...candidateRows];
|
|
197
|
-
const expectedByPid = new Map(expectedRows.map((row) => [row.pid, row]));
|
|
198
|
-
const verificationRows = await readCurrentSnapshotRows(expectedRows);
|
|
199
|
-
const verifiedRoot = verificationRows.find((row) => row.pid === rootPid);
|
|
200
|
-
if (expectedRoot !== undefined && verifiedRoot !== undefined &&
|
|
201
|
-
(verifiedRoot.startToken !== expectedRoot.startToken ||
|
|
202
|
-
verifiedRoot.processGroupId !== expectedRoot.processGroupId)) {
|
|
203
|
-
return [];
|
|
204
|
-
}
|
|
205
|
-
const stableRows = verificationRows.filter((current) => {
|
|
206
|
-
const expected = expectedByPid.get(current.pid);
|
|
207
|
-
return (expected !== undefined &&
|
|
208
|
-
current.startToken === expected.startToken &&
|
|
209
|
-
!isQuiescentState(current.state));
|
|
210
|
-
});
|
|
211
|
-
return stableRows.map(toProcessIdentity);
|
|
212
|
-
}
|
|
213
|
-
async function collectVerifiedDescendantIdentitiesForRoots(expectedRoots) {
|
|
214
|
-
const roots = expectedRoots.filter((root) => Number.isInteger(root.pid) && root.pid > 0);
|
|
215
|
-
if (roots.length === 0)
|
|
216
|
-
return [];
|
|
217
|
-
if (new Set(roots.map((root) => root.pid)).size > MAX_DESCENDANT_PIDS) {
|
|
218
|
-
throw new Error(`process descendant snapshot exceeded ${MAX_DESCENDANT_PIDS} identities`);
|
|
219
|
-
}
|
|
220
|
-
const initialRows = await collectProcessSnapshotRows();
|
|
221
|
-
if (initialRows === undefined)
|
|
222
|
-
throw new Error("process snapshot unavailable");
|
|
223
|
-
const initialByPid = new Map(initialRows.map((row) => [row.pid, row]));
|
|
224
|
-
const childrenByParent = indexChildrenByParent(initialRows);
|
|
225
|
-
const candidatesByRoot = new Map();
|
|
226
|
-
const expectedByPid = new Map();
|
|
227
|
-
for (const expected of roots) {
|
|
228
|
-
const root = initialByPid.get(expected.pid);
|
|
229
|
-
if (root !== undefined)
|
|
230
|
-
assertSignalableSnapshotRows([root]);
|
|
231
|
-
if (root === undefined || isQuiescentState(root.state) ||
|
|
232
|
-
root.startToken !== expected.startToken || root.processGroupId !== expected.processGroupId)
|
|
233
|
-
continue;
|
|
234
|
-
if (candidatesByRoot.has(root.pid))
|
|
235
|
-
continue;
|
|
236
|
-
const candidates = [root, ...descendantRowsFromSnapshot(root.pid, initialRows, childrenByParent)];
|
|
237
|
-
assertSignalableSnapshotRows(candidates);
|
|
238
|
-
candidatesByRoot.set(root.pid, candidates);
|
|
239
|
-
for (const row of candidates) {
|
|
240
|
-
expectedByPid.set(row.pid, row);
|
|
241
|
-
if (expectedByPid.size > MAX_DESCENDANT_PIDS) {
|
|
242
|
-
throw new Error(`process descendant snapshot exceeded ${MAX_DESCENDANT_PIDS} identities`);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
if (expectedByPid.size === 0)
|
|
247
|
-
return [];
|
|
248
|
-
const verificationRows = await readCurrentSnapshotRows([...expectedByPid.values()]);
|
|
249
|
-
const verifiedByPid = new Map(verificationRows.map((row) => [row.pid, row]));
|
|
250
|
-
const ownedPids = new Set();
|
|
251
|
-
for (const [rootPid, candidates] of candidatesByRoot) {
|
|
252
|
-
const expected = expectedByPid.get(rootPid);
|
|
253
|
-
const current = verifiedByPid.get(rootPid);
|
|
254
|
-
if (current !== undefined &&
|
|
255
|
-
(current.startToken !== expected.startToken || current.processGroupId !== expected.processGroupId))
|
|
256
|
-
continue;
|
|
257
|
-
for (const candidate of candidates)
|
|
258
|
-
ownedPids.add(candidate.pid);
|
|
259
|
-
}
|
|
260
|
-
return verificationRows.filter((current) => {
|
|
261
|
-
const expected = expectedByPid.get(current.pid);
|
|
262
|
-
return ownedPids.has(current.pid) && expected !== undefined &&
|
|
263
|
-
current.startToken === expected.startToken && !isQuiescentState(current.state);
|
|
264
|
-
}).map(toProcessIdentity);
|
|
265
|
-
}
|
|
266
|
-
function assertSignalableSnapshotRows(rows) {
|
|
267
|
-
if (rows.some((row) => row.processGroupId <= 0)) {
|
|
268
|
-
throw new Error("process snapshot contains a nonsignalable owned process");
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
function descendantRowsFromSnapshot(rootPid, rows, indexedChildren) {
|
|
272
|
-
const root = rows.find((row) => row.pid === rootPid);
|
|
273
|
-
if (root === undefined || isQuiescentState(root.state))
|
|
274
|
-
return [];
|
|
275
|
-
const childrenByParent = indexedChildren ?? indexChildrenByParent(rows);
|
|
276
|
-
const descendants = [];
|
|
277
|
-
const seen = new Set([rootPid]);
|
|
278
|
-
let frontier = [rootPid];
|
|
279
|
-
for (let depth = 0; depth < MAX_DESCENDANT_DEPTH && frontier.length > 0; depth += 1) {
|
|
280
|
-
const next = [];
|
|
281
|
-
for (const parentPid of frontier) {
|
|
282
|
-
for (const child of childrenByParent.get(parentPid) ?? []) {
|
|
283
|
-
if (seen.has(child.pid))
|
|
284
|
-
continue;
|
|
285
|
-
seen.add(child.pid);
|
|
286
|
-
descendants.push(child);
|
|
287
|
-
if (descendants.length >= MAX_DESCENDANT_PIDS)
|
|
288
|
-
return descendants;
|
|
289
|
-
next.push(child.pid);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
frontier = next;
|
|
293
|
-
}
|
|
294
|
-
return descendants;
|
|
295
|
-
}
|
|
296
|
-
function indexChildrenByParent(rows) {
|
|
297
|
-
const childrenByParent = new Map();
|
|
298
|
-
for (const row of rows) {
|
|
299
|
-
if (row.pid <= 0 || row.parentPid <= 0 || row.pid === row.parentPid)
|
|
300
|
-
continue;
|
|
301
|
-
const children = childrenByParent.get(row.parentPid);
|
|
302
|
-
if (children === undefined)
|
|
303
|
-
childrenByParent.set(row.parentPid, [row]);
|
|
304
|
-
else
|
|
305
|
-
children.push(row);
|
|
306
|
-
}
|
|
307
|
-
return childrenByParent;
|
|
308
|
-
}
|
|
309
|
-
async function readCurrentSnapshotRows(expectedRows) {
|
|
310
|
-
const procPids = [];
|
|
311
|
-
const psPids = [];
|
|
312
|
-
for (const row of expectedRows) {
|
|
313
|
-
if (process.platform === "linux" && row.startToken.startsWith("proc:")) {
|
|
314
|
-
procPids.push(row.pid);
|
|
315
|
-
}
|
|
316
|
-
else {
|
|
317
|
-
psPids.push(row.pid);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
const [procOutcomes, psRows] = await Promise.all([
|
|
321
|
-
Promise.all(procPids.map((pid) => readProcRow(pid))),
|
|
322
|
-
collectProcessSnapshotRowsFromPs(psPids),
|
|
323
|
-
]);
|
|
324
|
-
if (procOutcomes.some((outcome) => outcome.status === "error" || outcome.status === "kernel" || outcome.status === "nonsignalable") || (psPids.length > 0 && psRows === undefined)) {
|
|
325
|
-
throw new Error("process identity verification failed");
|
|
326
|
-
}
|
|
327
|
-
return [
|
|
328
|
-
...procOutcomes.flatMap((outcome) => outcome.status === "found" ? [outcome.row] : []),
|
|
329
|
-
...(psRows ?? []),
|
|
330
|
-
];
|
|
331
|
-
}
|
|
332
|
-
async function collectProcessSnapshotRows() {
|
|
333
|
-
// Linux probes use start ticks. Switching a failed table read to ps would
|
|
334
|
-
// give the same process a different identity, so genuine proc errors fail closed.
|
|
335
|
-
return process.platform === "linux"
|
|
336
|
-
? collectProcessSnapshotRowsFromProc()
|
|
337
|
-
: collectProcessSnapshotRowsFromPs();
|
|
338
|
-
}
|
|
339
|
-
async function collectProcessSnapshotRowsFromProc() {
|
|
340
|
-
let entries;
|
|
341
|
-
try {
|
|
342
|
-
entries = await readdir("/proc");
|
|
343
|
-
}
|
|
344
|
-
catch {
|
|
345
|
-
return undefined;
|
|
346
|
-
}
|
|
347
|
-
const outcomes = await Promise.all(entries
|
|
348
|
-
.filter((entry) => /^\d+$/.test(entry))
|
|
349
|
-
.map((entry) => readProcRow(Number.parseInt(entry, 10))));
|
|
350
|
-
if (outcomes.some((outcome) => outcome.status === "error"))
|
|
351
|
-
return undefined;
|
|
352
|
-
// Retain valid hidden/released groups for ancestry. Only a requested subtree
|
|
353
|
-
// that depends on such a row must fail; unrelated rows cannot disable capture.
|
|
354
|
-
return outcomes.flatMap((outcome) => outcome.status === "found" || outcome.status === "nonsignalable" ? [outcome.row] : []);
|
|
355
|
-
}
|
|
356
|
-
async function collectProcessSnapshotRowsFromPs(pids) {
|
|
357
|
-
const uniquePids = pids === undefined ? undefined : uniqueValidPids(pids);
|
|
358
|
-
if (uniquePids !== undefined && uniquePids.length === 0)
|
|
359
|
-
return [];
|
|
360
|
-
let stdout;
|
|
361
|
-
try {
|
|
362
|
-
const result = await execFileAsync("ps", uniquePids === undefined
|
|
363
|
-
? ["-axo", "pid=,ppid=,pgid=,state=,lstart="]
|
|
364
|
-
: ["-o", "pid=,ppid=,pgid=,state=,lstart=", "-p", uniquePids.join(",")], {
|
|
365
|
-
timeout: uniquePids === undefined
|
|
366
|
-
? PROCESS_SNAPSHOT_TIMEOUT_MS
|
|
367
|
-
: PROCESS_IDENTITY_PROBE_TIMEOUT_MS,
|
|
368
|
-
maxBuffer: uniquePids === undefined ? 2 * 1024 * 1024 : 256 * 1024,
|
|
369
|
-
});
|
|
370
|
-
stdout = result.stdout;
|
|
371
|
-
}
|
|
372
|
-
catch (err) {
|
|
373
|
-
if (uniquePids !== undefined && hasErrorCode(err, 1))
|
|
374
|
-
return [];
|
|
375
|
-
return undefined;
|
|
376
|
-
}
|
|
377
|
-
return parsePsSnapshotRows(stdout);
|
|
378
|
-
}
|
|
379
|
-
async function readCurrentIdentityBatch(expected) {
|
|
380
|
-
if (process.platform !== "linux") {
|
|
381
|
-
return readProcessIdentityBatchWithPs(expected.map((identity) => identity.pid));
|
|
382
|
-
}
|
|
383
|
-
const procPids = [];
|
|
384
|
-
const psPids = [];
|
|
385
|
-
for (const identity of expected) {
|
|
386
|
-
if (identity.startToken.startsWith("proc:"))
|
|
387
|
-
procPids.push(identity.pid);
|
|
388
|
-
else
|
|
389
|
-
psPids.push(identity.pid);
|
|
390
|
-
}
|
|
391
|
-
const [procBatch, psBatch] = await Promise.all([
|
|
392
|
-
readProcessIdentityBatchFromProc(procPids),
|
|
393
|
-
readProcessIdentityBatchWithPs(psPids),
|
|
394
|
-
]);
|
|
395
|
-
return mergeIdentityBatches(procBatch, psBatch);
|
|
396
|
-
}
|
|
397
|
-
async function readProcessIdentityBatchFromProc(pids) {
|
|
398
|
-
if (pids.length === 0)
|
|
399
|
-
return { identities: new Map(), complete: true };
|
|
400
|
-
const outcomes = await Promise.all(pids.map((pid) => readProcRow(pid)));
|
|
401
|
-
const identities = new Map();
|
|
402
|
-
let complete = true;
|
|
403
|
-
for (const outcome of outcomes) {
|
|
404
|
-
if (outcome.status === "found") {
|
|
405
|
-
identities.set(outcome.row.pid, toProcessIdentity(outcome.row));
|
|
406
|
-
}
|
|
407
|
-
else if (outcome.status === "error" || outcome.status === "kernel" || outcome.status === "nonsignalable") {
|
|
408
|
-
complete = false;
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
return { identities, complete };
|
|
412
|
-
}
|
|
413
|
-
async function readProcessIdentityBatchWithPs(pids) {
|
|
414
|
-
const uniquePids = uniqueValidPids(pids);
|
|
415
|
-
if (uniquePids.length === 0)
|
|
416
|
-
return { identities: new Map(), complete: true };
|
|
417
|
-
let stdout;
|
|
418
|
-
try {
|
|
419
|
-
const result = await execFileAsync("ps", ["-o", "pid=,pgid=,state=,lstart=", "-p", uniquePids.join(",")], { timeout: PROCESS_IDENTITY_PROBE_TIMEOUT_MS, maxBuffer: 256 * 1024 });
|
|
420
|
-
stdout = result.stdout;
|
|
421
|
-
}
|
|
422
|
-
catch (err) {
|
|
423
|
-
if (hasErrorCode(err, 1))
|
|
424
|
-
return { identities: new Map(), complete: true };
|
|
425
|
-
return { identities: new Map(), complete: false };
|
|
426
|
-
}
|
|
427
|
-
const identities = new Map();
|
|
428
|
-
for (const line of stdout.split("\n")) {
|
|
429
|
-
const match = line.trim().match(/^(\d+)\s+(\d+)\s+(\S+)\s+(.+)$/);
|
|
430
|
-
if (match === null)
|
|
431
|
-
continue;
|
|
432
|
-
const pid = Number.parseInt(match[1], 10);
|
|
433
|
-
const processGroupId = Number.parseInt(match[2], 10);
|
|
434
|
-
if (!Number.isInteger(pid) || !Number.isInteger(processGroupId) || processGroupId <= 0) {
|
|
435
|
-
continue;
|
|
436
|
-
}
|
|
437
|
-
identities.set(pid, {
|
|
438
|
-
pid,
|
|
439
|
-
processGroupId,
|
|
440
|
-
state: match[3],
|
|
441
|
-
startToken: psStartToken(match[4]),
|
|
442
|
-
});
|
|
443
|
-
}
|
|
444
|
-
return { identities, complete: true };
|
|
445
|
-
}
|
|
446
|
-
async function readIdentityBatchWithReader(expected, identityReader) {
|
|
447
|
-
const outcomes = await Promise.all(expected.map(async (identity) => {
|
|
448
|
-
try {
|
|
449
|
-
return { identity: await identityReader(identity.pid), failed: false };
|
|
450
|
-
}
|
|
451
|
-
catch {
|
|
452
|
-
return { identity: undefined, failed: true };
|
|
453
|
-
}
|
|
454
|
-
}));
|
|
455
|
-
const identities = new Map();
|
|
456
|
-
let complete = true;
|
|
457
|
-
for (const outcome of outcomes) {
|
|
458
|
-
if (outcome.failed)
|
|
459
|
-
complete = false;
|
|
460
|
-
if (outcome.identity !== undefined)
|
|
461
|
-
identities.set(outcome.identity.pid, outcome.identity);
|
|
462
|
-
}
|
|
463
|
-
return { identities, complete };
|
|
464
|
-
}
|
|
465
|
-
async function readProcRow(pid) {
|
|
466
|
-
let stat;
|
|
467
|
-
try {
|
|
468
|
-
stat = await readFile(`/proc/${pid}/stat`, "utf8");
|
|
469
|
-
}
|
|
470
|
-
catch (err) {
|
|
471
|
-
if (hasErrorCode(err, "ENOENT") || hasErrorCode(err, "ESRCH")) {
|
|
472
|
-
return { status: "missing" };
|
|
473
|
-
}
|
|
474
|
-
return { status: "error" };
|
|
475
|
-
}
|
|
476
|
-
const closeParen = stat.lastIndexOf(")");
|
|
477
|
-
const openParen = stat.indexOf("(");
|
|
478
|
-
if (openParen < 1 || closeParen < openParen)
|
|
479
|
-
return { status: "error" };
|
|
480
|
-
const rawPid = stat.slice(0, openParen).trim();
|
|
481
|
-
const parsedPid = Number(rawPid);
|
|
482
|
-
const fields = stat.slice(closeParen + 1).trim().split(/\s+/);
|
|
483
|
-
const state = fields[0];
|
|
484
|
-
const parentPid = Number(fields[1]);
|
|
485
|
-
const processGroupId = Number(fields[2]);
|
|
486
|
-
const startTimeTicks = fields[19];
|
|
487
|
-
if (!/^\d+$/.test(rawPid) || !Number.isSafeInteger(parsedPid) || parsedPid !== pid ||
|
|
488
|
-
state === undefined || !/^[a-z]$/i.test(state) ||
|
|
489
|
-
startTimeTicks === undefined || !/^\d+$/.test(startTimeTicks) ||
|
|
490
|
-
!/^\d+$/.test(fields[1] ?? "") || !Number.isSafeInteger(parentPid) ||
|
|
491
|
-
!/^(?:\d+|-1)$/.test(fields[2] ?? "") || !Number.isSafeInteger(processGroupId)) {
|
|
492
|
-
return { status: "error" };
|
|
493
|
-
}
|
|
494
|
-
const row = {
|
|
495
|
-
pid,
|
|
496
|
-
parentPid,
|
|
497
|
-
processGroupId,
|
|
498
|
-
startToken: `proc:${startTimeTicks}`,
|
|
499
|
-
state,
|
|
500
|
-
};
|
|
501
|
-
if (processGroupId <= 0) {
|
|
502
|
-
// /proc stat field 9 carries PF_KTHREAD (Linux include/linux/sched.h).
|
|
503
|
-
// Only proven kernel threads are outside the user-process ancestry;
|
|
504
|
-
// a namespace-hidden user PGID of zero must remain an incomplete probe.
|
|
505
|
-
const rawFlags = fields[6];
|
|
506
|
-
const flags = rawFlags === undefined || !/^\d+$/.test(rawFlags) ? NaN : Number(rawFlags);
|
|
507
|
-
if (!Number.isSafeInteger(flags) || flags < 0 || flags > 0xffff_ffff)
|
|
508
|
-
return { status: "error" };
|
|
509
|
-
if (fields[2] === "0" && (flags & 0x0020_0000) !== 0)
|
|
510
|
-
return { status: "kernel" };
|
|
511
|
-
return { status: "nonsignalable", row };
|
|
512
|
-
}
|
|
513
|
-
return {
|
|
514
|
-
status: "found",
|
|
515
|
-
row,
|
|
516
|
-
};
|
|
517
|
-
}
|
|
518
|
-
function parsePsSnapshotRows(stdout) {
|
|
519
|
-
const rows = [];
|
|
520
|
-
for (const line of stdout.split("\n")) {
|
|
521
|
-
const match = line.trim().match(/^(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.+)$/);
|
|
522
|
-
if (match === null)
|
|
523
|
-
continue;
|
|
524
|
-
const pid = Number.parseInt(match[1], 10);
|
|
525
|
-
const parentPid = Number.parseInt(match[2], 10);
|
|
526
|
-
const processGroupId = Number.parseInt(match[3], 10);
|
|
527
|
-
if (!Number.isInteger(pid) ||
|
|
528
|
-
!Number.isInteger(parentPid) ||
|
|
529
|
-
!Number.isInteger(processGroupId) ||
|
|
530
|
-
processGroupId <= 0) {
|
|
531
|
-
continue;
|
|
532
|
-
}
|
|
533
|
-
rows.push({
|
|
534
|
-
pid,
|
|
535
|
-
parentPid,
|
|
536
|
-
processGroupId,
|
|
537
|
-
state: match[4],
|
|
538
|
-
startToken: psStartToken(match[5]),
|
|
539
|
-
});
|
|
540
|
-
}
|
|
541
|
-
return rows;
|
|
542
|
-
}
|
|
543
|
-
function mergeIdentityBatches(first, second) {
|
|
544
|
-
return {
|
|
545
|
-
identities: new Map([...first.identities, ...second.identities]),
|
|
546
|
-
complete: first.complete && second.complete,
|
|
547
|
-
};
|
|
548
|
-
}
|
|
549
|
-
function toProcessIdentity(row) {
|
|
550
|
-
return {
|
|
551
|
-
pid: row.pid,
|
|
552
|
-
processGroupId: row.processGroupId,
|
|
553
|
-
startToken: row.startToken,
|
|
554
|
-
state: row.state,
|
|
555
|
-
};
|
|
556
|
-
}
|
|
557
|
-
function dedupeIdentities(identities) {
|
|
558
|
-
const seen = new Set();
|
|
559
|
-
const deduped = [];
|
|
560
|
-
for (const identity of identities) {
|
|
561
|
-
const key = `${identity.pid}:${identity.startToken}:${identity.processGroupId}`;
|
|
562
|
-
if (seen.has(key))
|
|
563
|
-
continue;
|
|
564
|
-
seen.add(key);
|
|
565
|
-
deduped.push(identity);
|
|
566
|
-
}
|
|
567
|
-
return deduped;
|
|
568
|
-
}
|
|
569
|
-
function uniqueValidPids(pids) {
|
|
570
|
-
return [...new Set(pids.filter((pid) => Number.isInteger(pid) && pid > 0))];
|
|
571
|
-
}
|
|
572
|
-
function psStartToken(value) {
|
|
573
|
-
return `ps:${value.replace(/\s+/g, " ").trim()}`;
|
|
574
|
-
}
|
|
575
|
-
function isQuiescentState(state) {
|
|
576
|
-
const normalized = state.trim().toUpperCase();
|
|
577
|
-
return normalized.startsWith("Z") || normalized.startsWith("X");
|
|
578
|
-
}
|
|
579
|
-
export async function raceValueWithTimeout(promise, timeoutMs, timeoutValue) {
|
|
580
|
-
let timer;
|
|
581
|
-
try {
|
|
582
|
-
return await Promise.race([
|
|
583
|
-
promise,
|
|
584
|
-
new Promise((resolve) => {
|
|
585
|
-
timer = setTimeout(() => resolve(timeoutValue), Math.max(0, timeoutMs));
|
|
586
|
-
}),
|
|
587
|
-
]);
|
|
588
|
-
}
|
|
589
|
-
finally {
|
|
590
|
-
if (timer !== undefined)
|
|
591
|
-
clearTimeout(timer);
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
/** Matches an errno string (ENOENT) or a numeric child exit code on a thrown value. */
|
|
595
|
-
function hasErrorCode(err, code) {
|
|
596
|
-
return typeof err === "object" && err !== null && "code" in err && err.code === code;
|
|
597
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { RuntimeError } from "./errors.js";
|
|
2
|
-
// Node/libuv's default CreateProcess argument quoting (UTF-16 code units).
|
|
3
|
-
export function windowsCommandLineLength(command, args) {
|
|
4
|
-
const quotedLength = (value) => {
|
|
5
|
-
if (value.length > 0 && !/[\s"]/u.test(value))
|
|
6
|
-
return value.length;
|
|
7
|
-
return 2 + value.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/, '$1$1').length;
|
|
8
|
-
};
|
|
9
|
-
return [command, ...args].reduce((size, value) => size + quotedLength(value) + 1, 0);
|
|
10
|
-
}
|
|
11
|
-
export function assertPromptFitsCommandLine(command, args, platform = process.platform) {
|
|
12
|
-
if (platform !== "win32")
|
|
13
|
-
return;
|
|
14
|
-
const size = windowsCommandLineLength(command, args);
|
|
15
|
-
if (size <= 32_767)
|
|
16
|
-
return;
|
|
17
|
-
throw new RuntimeError("CLI_COMMAND_LINE_TOO_LONG", "The Kimi prompt exceeds the Windows command-line limit; the CLI was not started. Narrow the review or split it into smaller scopes. A short ask prompt can reference a saved diff inside the workspace. Reinstalling Kimi or running setup will not fix this. The current print transport does not support stdin prompts.", "cli-client.pre-spawn", { details: { command_line_utf16_units: size, limit_utf16_units: 32_767, platform } });
|
|
18
|
-
}
|