@drakulavich/kesha-voice-kit 1.31.0 → 2.0.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/README.md CHANGED
@@ -19,7 +19,7 @@
19
19
  - **Small Rust engine** — single ~65MB binary, no ffmpeg, no Python, no native Node addons
20
20
 
21
21
  <p align="center">
22
- <img src="https://github.com/drakulavich/kesha-voice-kit/raw/main/demo.gif" alt="kesha demo — English + Russian transcription with automatic language detection" width="800">
22
+ <img src="https://github.com/drakulavich/kesha-voice-kit/raw/main/docs/assets/demo.webp" alt="kesha demo: an English voice message transcribed, a Russian one as JSON with automatic language detection, then text-to-speech into a wav" width="800">
23
23
  </p>
24
24
 
25
25
  ## Quick Start
@@ -138,10 +138,10 @@ Full per-file breakdown (Russian + English): [BENCHMARK.md](BENCHMARK.md). The C
138
138
 
139
139
  All of these install the Bun CLI wrapper; engine + models still download explicitly via `kesha install`. (Nix is the exception — it currently builds only the engine from source; see below.)
140
140
 
141
- - **Homebrew** — `brew install drakulavich/tap/kesha-voice-kit` · [docs/homebrew.md](docs/homebrew.md)
142
- - **Linux packages** (`.deb`/`.rpm`, x64) — published on CLI releases, see [docs/linux-packages.md](docs/linux-packages.md)
141
+ - **Homebrew** — `brew install drakulavich/tap/kesha-voice-kit` · [docs/distribution.md](docs/distribution.md#homebrew)
142
+ - **Linux packages** (`.deb`/`.rpm`, x64) — published on stable releases, see [docs/distribution.md](docs/distribution.md#linux-packages)
143
143
  - **Docker** (GHCR image) — [docs/docker.md](docs/docker.md)
144
- - **Nix** (`aarch64-darwin` / `x86_64-linux`) — builds the engine from source (`nix build github:drakulavich/kesha-voice-kit#kesha-engine`). The full `kesha` CLI via `nix run` / `nix profile install` is **not yet available** — it needs a maintainer with Nix to populate a build hash ([#946](https://github.com/drakulavich/kesha-voice-kit/issues/946)). · [docs/nix-install.md](docs/nix-install.md)
144
+ - **Nix** (`aarch64-darwin` / `x86_64-linux`) — builds the engine from source (`nix build github:drakulavich/kesha-voice-kit#kesha-engine`). The full `kesha` CLI via `nix run` / `nix profile install` is **not yet available** — it needs a maintainer with Nix to populate a build hash ([#946](https://github.com/drakulavich/kesha-voice-kit/issues/946)). · [docs/distribution.md](docs/distribution.md#nix)
145
145
  - **Shell completions + manpage** — `kesha completions bash|zsh|fish` and `kesha manpage` print the packaged files to install wherever your shell expects them.
146
146
 
147
147
  ## Integrations
package/SKILL.md CHANGED
@@ -236,7 +236,7 @@ No system deps — English G2P is embedded (`misaki-rs`); Russian G2P is bundled
236
236
 
237
237
  **Speech-to-text (25):** Bulgarian, Croatian, Czech, Danish, Dutch, English, Estonian, Finnish, French, German, Greek, Hungarian, Italian, Latvian, Lithuanian, Maltese, Polish, Portuguese, Romanian, Russian, Slovak, Slovenian, Spanish, Swedish, Ukrainian.
238
238
 
239
- **Text-to-speech (9):** English (Kokoro-82M; FluidAudio CoreML on darwin-arm64, ONNX elsewhere), Russian (Vosk-TTS, 5 baked-in speakers — default `ru-vosk-m02`), Spanish, French, Italian, Portuguese (Kokoro — CharsiuG2P on ONNX builds, FluidAudio G2P on darwin-arm64), plus Hindi, Japanese, Chinese (Kokoro, darwin-arm64 only). `hi`/`ja` accept romanized Latin input only; native Devanagari and kana/kanji are rejected with `E_SCRIPT_UNSUPPORTED` — see [#492](https://github.com/drakulavich/kesha-voice-kit/issues/492). Any macOS system voice is also available via `--voice macos-*`.
239
+ **Text-to-speech (9):** English (Kokoro-82M; FluidAudio CoreML on darwin-arm64, ONNX elsewhere), Russian (Vosk-TTS, 5 baked-in speakers — default `ru-vosk-m02`), Spanish, French, Italian, Portuguese (Kokoro — CharsiuG2P on ONNX builds, FluidAudio G2P on darwin-arm64), plus Hindi, Japanese, Chinese (Kokoro, darwin-arm64 only). The `hi`/`ja` Kokoro voices read romanized Latin input only; on macOS, text that is mostly Devanagari or kana/kanji with no `--voice` routes to an installed AVSpeech voice (Lekha for `hi-IN`, Otoya for `ja-JP`), while an explicit `--voice hi-*`/`ja-*` refuses them with `E_SCRIPT_UNSUPPORTED`; non-macOS builds have no `hi`/`ja` voices, so the text is refused by the default English voice (`E_SCRIPT_UNSUPPORTED`) and `--voice hi-*`/`ja-*` is `E_VOICE_UNKNOWN` — see [#492](https://github.com/drakulavich/kesha-voice-kit/issues/492). Any macOS system voice is also available via `--voice macos-*`.
240
240
 
241
241
  ## Performance
242
242
 
@@ -27,7 +27,7 @@ The product promise is not "one model for every audio problem." The promise is a
27
27
  | Integrate with OpenClaw as a local voice model | Stable integration surface, user-configured route | `docs/openclaw.md` |
28
28
  | Integrate with Hermes Agent as local STT/TTS commands | Stable CLI surface, user-configured route | `docs/hermes.md` |
29
29
  | Use Raycast actions for selected-file transcription or clipboard speech | Beta, macOS only | `raycast/` extension |
30
- | Install through Nix instead of Bun/npm | Beta, selected systems | `docs/nix-install.md` |
30
+ | Install through Nix instead of Bun/npm | Beta, selected systems | `docs/distribution.md#nix` |
31
31
 
32
32
  ## Non-goals
33
33
 
@@ -63,8 +63,8 @@ The product promise is not "one model for every audio problem." The promise is a
63
63
  | STT | Supported, CoreML path | Not shipped | Supported, ONNX CPU path | Supported, ONNX CPU path | `aarch64-darwin`, `x86_64-linux` |
64
64
  | Audio language detection | Supported | Not shipped | Supported | Supported | Supported where the flake builds |
65
65
  | VAD | Supported with `kesha install --vad` | Not shipped | Supported with `kesha install --vad` | Supported with `kesha install --vad` | Supported where the engine path includes VAD assets |
66
- | TTS: Kokoro (`en` `es` `fr` `it` `pt`) | Supported, FluidAudio/CoreML in release builds | Not shipped | Supported, ONNX path | Supported, ONNX path | Supported except where noted in `docs/nix-install.md` |
67
- | TTS: Russian Vosk-TTS | Supported | Not shipped | Supported | Supported | Supported except where noted in `docs/nix-install.md` |
66
+ | TTS: Kokoro (`en` `es` `fr` `it` `pt`) | Supported, FluidAudio/CoreML in release builds | Not shipped | Supported, ONNX path | Supported, ONNX path | Supported except where noted in `docs/distribution.md#nix` |
67
+ | TTS: Russian Vosk-TTS | Supported | Not shipped | Supported | Supported | Supported except where noted in `docs/distribution.md#nix` |
68
68
  | TTS: Kokoro (`hi` `ja` `zh`) | Supported, darwin-arm64 only | Not shipped | Not supported | Not supported | `aarch64-darwin` only |
69
69
  | macOS system voices | Supported | Not shipped | Not applicable | Not applicable | Supported on `aarch64-darwin` |
70
70
  | Speaker diarization | Preview, darwin-arm64 only | Not supported | Not supported | Not supported | Not wired into the Nix build yet |
package/model-plan.json CHANGED
@@ -62,14 +62,14 @@
62
62
  { "relPath": "KokoroNoise_v2.mlmodelc/weights/weight.bin", "sizeBytes": 4580160 },
63
63
  { "relPath": "KokoroVocoder.mlmodelc/analytics/coremldata.bin", "sizeBytes": 243 },
64
64
  { "relPath": "KokoroVocoder.mlmodelc/coremldata.bin", "sizeBytes": 626 },
65
- { "relPath": "KokoroVocoder.mlmodelc/metadata.json", "sizeBytes": 4392 },
66
- { "relPath": "KokoroVocoder.mlmodelc/model.mil", "sizeBytes": 309130 },
65
+ { "relPath": "KokoroVocoder.mlmodelc/metadata.json", "sizeBytes": 4331 },
66
+ { "relPath": "KokoroVocoder.mlmodelc/model.mil", "sizeBytes": 309000 },
67
67
  { "relPath": "KokoroVocoder.mlmodelc/weights/weight.bin", "sizeBytes": 48889920 },
68
- { "relPath": "KokoroTail.mlmodelc/analytics/coremldata.bin", "sizeBytes": 243 },
69
- { "relPath": "KokoroTail.mlmodelc/coremldata.bin", "sizeBytes": 392 },
70
- { "relPath": "KokoroTail.mlmodelc/metadata.json", "sizeBytes": 1872 },
71
- { "relPath": "KokoroTail.mlmodelc/model.mil", "sizeBytes": 7014 },
72
- { "relPath": "KokoroTail.mlmodelc/weights/weight.bin", "sizeBytes": 81088 },
68
+ { "relPath": "KokoroTail_v2.mlmodelc/analytics/coremldata.bin", "sizeBytes": 243 },
69
+ { "relPath": "KokoroTail_v2.mlmodelc/coremldata.bin", "sizeBytes": 392 },
70
+ { "relPath": "KokoroTail_v2.mlmodelc/metadata.json", "sizeBytes": 1872 },
71
+ { "relPath": "KokoroTail_v2.mlmodelc/model.mil", "sizeBytes": 7014 },
72
+ { "relPath": "KokoroTail_v2.mlmodelc/weights/weight.bin", "sizeBytes": 81088 },
73
73
  { "relPath": "vocab.json", "sizeBytes": 1416 },
74
74
  { "relPath": "af_heart.bin", "sizeBytes": 522240 }
75
75
  ],
@@ -143,14 +143,14 @@
143
143
  { "relPath": "KokoroNoise_v2.mlmodelc/weights/weight.bin", "sizeBytes": 4580160 },
144
144
  { "relPath": "KokoroVocoder.mlmodelc/analytics/coremldata.bin", "sizeBytes": 243 },
145
145
  { "relPath": "KokoroVocoder.mlmodelc/coremldata.bin", "sizeBytes": 627 },
146
- { "relPath": "KokoroVocoder.mlmodelc/metadata.json", "sizeBytes": 4392 },
147
- { "relPath": "KokoroVocoder.mlmodelc/model.mil", "sizeBytes": 309127 },
146
+ { "relPath": "KokoroVocoder.mlmodelc/metadata.json", "sizeBytes": 4331 },
147
+ { "relPath": "KokoroVocoder.mlmodelc/model.mil", "sizeBytes": 308997 },
148
148
  { "relPath": "KokoroVocoder.mlmodelc/weights/weight.bin", "sizeBytes": 48889920 },
149
- { "relPath": "KokoroTail.mlmodelc/analytics/coremldata.bin", "sizeBytes": 243 },
150
- { "relPath": "KokoroTail.mlmodelc/coremldata.bin", "sizeBytes": 392 },
151
- { "relPath": "KokoroTail.mlmodelc/metadata.json", "sizeBytes": 1872 },
152
- { "relPath": "KokoroTail.mlmodelc/model.mil", "sizeBytes": 7014 },
153
- { "relPath": "KokoroTail.mlmodelc/weights/weight.bin", "sizeBytes": 81088 },
149
+ { "relPath": "KokoroTail_v2.mlmodelc/analytics/coremldata.bin", "sizeBytes": 243 },
150
+ { "relPath": "KokoroTail_v2.mlmodelc/coremldata.bin", "sizeBytes": 392 },
151
+ { "relPath": "KokoroTail_v2.mlmodelc/metadata.json", "sizeBytes": 1872 },
152
+ { "relPath": "KokoroTail_v2.mlmodelc/model.mil", "sizeBytes": 7014 },
153
+ { "relPath": "KokoroTail_v2.mlmodelc/weights/weight.bin", "sizeBytes": 81088 },
154
154
  { "relPath": "vocab.json", "sizeBytes": 1796 },
155
155
  { "relPath": "voices/zf_001.bin", "sizeBytes": 522240 },
156
156
  { "relPath": "voices/zm_050.bin", "sizeBytes": 522240 },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drakulavich/kesha-voice-kit",
3
- "version": "1.31.0",
3
+ "version": "2.0.0",
4
4
  "description": "Give your tools a voice — speech to text and back, 25 languages, up to ~19× faster than Whisper. On your machine.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -39,9 +39,6 @@
39
39
  "openclawVersion": "2026.5.27"
40
40
  }
41
41
  },
42
- "keshaEngine": {
43
- "version": "1.26.0"
44
- },
45
42
  "scripts": {
46
43
  "test": "bun test --timeout 30000",
47
44
  "test:watch": "bun test --timeout 30000 --watch",
@@ -61,7 +58,7 @@
61
58
  "check:release-manifest": "bun .github/scripts/release-manifest.mjs --check",
62
59
  "check:versions": "bun .github/scripts/check-versions.ts",
63
60
  "check:engine-targets": "bun .github/scripts/check-engine-targets.ts",
64
- "mutants:ts": "bun scripts/mutants-ts.ts"
61
+ "check:file-sizes": "bun .github/scripts/check-file-sizes.ts"
65
62
  },
66
63
  "keywords": [
67
64
  "asr",
@@ -89,8 +86,6 @@
89
86
  "bun": ">=1.3.0"
90
87
  },
91
88
  "devDependencies": {
92
- "@hughescr/stryker-bun-runner": "^1.3.8",
93
- "@stryker-mutator/core": "^9.6.1",
94
89
  "@types/bun": "latest",
95
90
  "fast-xml-parser": "^5.8.0",
96
91
  "typescript": "^7.0.2",
@@ -111,5 +106,24 @@
111
106
  "fast-uri": "^3.1.5",
112
107
  "ip-address": "^10.3.1",
113
108
  "smol-toml": "1.7.2"
109
+ },
110
+ "kesha": {
111
+ "engine": {
112
+ "version": "2.0.0",
113
+ "sha256": {
114
+ "kesha-engine-darwin-arm64": "2d75bc773fb81917a0dd996124c0e18eea3b15ecc8f04fe274d41c68bda9276c",
115
+ "kesha-engine-linux-x64": "09082a1f3eb07d8f8051806a057669f098856d84541f087a1e614ca80d8235b2",
116
+ "kesha-engine-windows-x64.exe": "de86f350bfd3d076cf339d99d892605b1a37a99085e8ff4c8ef87057618193f3",
117
+ "say-avspeech-darwin-arm64": "a3aa5b75ddff68a48f1158e838310b37cac72ef86841d9cfcdf1ee233cb7451a",
118
+ "kesha-textlang-darwin-arm64": "e45e2822248b14dd72aa79e2def8fa6b9c07c97423f11c841508769225d72da4"
119
+ },
120
+ "size": {
121
+ "kesha-engine-darwin-arm64": 73623152,
122
+ "kesha-engine-linux-x64": 67392296,
123
+ "kesha-engine-windows-x64.exe": 66550272,
124
+ "say-avspeech-darwin-arm64": 64208,
125
+ "kesha-textlang-darwin-arm64": 57648
126
+ }
127
+ }
114
128
  }
115
129
  }
@@ -1,8 +1,21 @@
1
- import { join, resolve, sep } from "path";
1
+ import { existsSync, realpathSync, statSync } from "fs";
2
+ import { dirname, join, resolve, sep } from "path";
3
+ import { dirSizeBytes } from "./diagnostic-paths";
2
4
 
3
- export interface CachePathEntry {
5
+ /** The sidecar executables the engine spawns from its own directory. */
6
+ const SIDECAR_FILES = ["say-avspeech", "kesha-textlang"] as const;
7
+
8
+ export interface CacheComponentSize {
4
9
  label: string;
5
10
  path: string;
11
+ exists: boolean;
12
+ sizeBytes: number;
13
+ }
14
+
15
+ interface EngineFootprint {
16
+ path: string;
17
+ members: string[];
18
+ sizeOf: (path: string) => number;
6
19
  }
7
20
 
8
21
  /**
@@ -18,6 +31,56 @@ export function isInsideDir(child: string, parent: string): boolean {
18
31
  return resolvedChild === resolvedParent || resolvedChild.startsWith(`${resolvedParent}${sep}`);
19
32
  }
20
33
 
34
+ /**
35
+ * What the Engine row owns (#1313). A managed install owns all of `<cache>/engine`. An
36
+ * overridden binary (`KESHA_ENGINE_BIN`, a Nix store path) shares its directory with software
37
+ * Kesha does not own, so only the binary and the sidecars beside it are counted — its
38
+ * grandparent may be `/usr/local`.
39
+ */
40
+ function engineFootprint(binPath: string, cacheRoot: string): EngineFootprint {
41
+ const managedRoot = join(cacheRoot, "engine");
42
+ if (resolve(dirname(binPath)) === resolve(managedRoot, "bin")) {
43
+ return { path: managedRoot, members: [managedRoot], sizeOf: dirSizeBytes };
44
+ }
45
+ const binDir = dirname(binPath);
46
+ return {
47
+ path: binPath,
48
+ members: [binPath, ...SIDECAR_FILES.map((f) => join(binDir, f))],
49
+ sizeOf: regularFileBytes,
50
+ };
51
+ }
52
+
53
+ /** A sidecar name that is (or links to) a directory is not ours to walk. */
54
+ function regularFileBytes(path: string): number {
55
+ try {
56
+ const st = statSync(path);
57
+ return st.isFile() ? st.size : 0;
58
+ } catch {
59
+ return 0;
60
+ }
61
+ }
62
+
63
+ function footprintBytes(engine: EngineFootprint, members: string[]): number {
64
+ return members.reduce((n, p) => n + engine.sizeOf(p), 0);
65
+ }
66
+
67
+ /** The cache plus whatever engine bytes live outside it, each counted once (#790). */
68
+ export function cacheTotalBytes(cacheRoot: string, binPath: string): number {
69
+ const engine = engineFootprint(binPath, cacheRoot);
70
+ const realCache = existingPath(cacheRoot);
71
+ const outside = engine.members.filter((m) => !isInsideDir(existingPath(m), realCache));
72
+ return dirSizeBytes(cacheRoot) + footprintBytes(engine, outside);
73
+ }
74
+
75
+ /** Compared by target, so an outside symlink into the cache is not counted twice. */
76
+ function existingPath(path: string): string {
77
+ try {
78
+ return realpathSync(path);
79
+ } catch {
80
+ return resolve(path);
81
+ }
82
+ }
83
+
21
84
  /**
22
85
  * The Kesha-managed directories both `kesha status --disk` and `kesha doctor` report, in one
23
86
  * place so a new model dir cannot be added to only one of them. The two ASR rows are mutually
@@ -26,13 +89,19 @@ export function isInsideDir(child: string, parent: string): boolean {
26
89
  * Whatever stayed in FluidAudio's own trees is outside this cache and is reported separately
27
90
  * by `fluidExternalRoots`.
28
91
  */
29
- export function cacheComponentPaths(
92
+ export function cacheComponents(
30
93
  cacheRoot: string,
31
- engineDir: string,
94
+ binPath: string,
32
95
  coreml: boolean,
33
- ): CachePathEntry[] {
34
- return [
35
- { label: "Engine", path: engineDir },
96
+ ): CacheComponentSize[] {
97
+ const engine = engineFootprint(binPath, cacheRoot);
98
+ const engineRow = {
99
+ label: "Engine",
100
+ path: engine.path,
101
+ exists: existsSync(engine.path),
102
+ sizeBytes: footprintBytes(engine, engine.members),
103
+ };
104
+ const modelRows = [
36
105
  ...(coreml
37
106
  ? [{ label: "FluidAudio (in cache)", path: join(cacheRoot, "fluidaudio") }]
38
107
  : [{ label: "ASR (Parakeet)", path: join(cacheRoot, "models/parakeet-tdt-v3") }]),
@@ -40,5 +109,6 @@ export function cacheComponentPaths(
40
109
  { label: "VAD (Silero)", path: join(cacheRoot, "models/silero-vad") },
41
110
  { label: "TTS (Kokoro)", path: join(cacheRoot, "models/kokoro-82m") },
42
111
  { label: "TTS (Vosk)", path: join(cacheRoot, "models/vosk-ru") },
43
- ];
112
+ ].map((row) => ({ ...row, exists: existsSync(row.path), sizeBytes: dirSizeBytes(row.path) }));
113
+ return [engineRow, ...modelRows];
44
114
  }
@@ -86,10 +86,12 @@ export function resolveEngineVersionFlag(raw: unknown): string | undefined {
86
86
  const value = String(raw).trim();
87
87
  if (isSemver(value)) return value;
88
88
  const hint = /^v\d/.test(value)
89
- ? ' Drop the leading "v" — that belongs to the release tag, not the version.'
90
- : "";
91
- throw new Error(
92
- `--engine-version needs an exact SemVer 2.0 version like 1.24.8 or 1.24.8-alpha.1, got "${value}".${hint}`,
89
+ ? 'drop the leading "v" — that belongs to the release tag, not the version.'
90
+ : undefined;
91
+ throw new KeshaError(
92
+ "E_INVALID_ARG",
93
+ `--engine-version needs an exact SemVer 2.0 version like 1.24.8 or 1.24.8-alpha.1, got "${value}".`,
94
+ hint === undefined ? {} : { hint },
93
95
  );
94
96
  }
95
97
 
package/src/cli/main.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { defineCommand } from "citty";
2
2
  import { errorMessage } from "../error-utils";
3
3
  import { existsSync } from "fs";
4
- import { detectAll } from "tinyld";
5
4
  import { isDirectoryPath, transcribeWithSegments, validateTranscribeRequest } from "../transcribe";
6
5
 
7
6
  export { isDirectoryPath };
@@ -26,7 +25,7 @@ import { runCommandSession, type CommandSession } from "./command-session";
26
25
  import { USAGE_MESSAGE } from "./dispatch";
27
26
  import type { CliContext } from "./context";
28
27
  import { exitCodeFor, KeshaError } from "../engine/events";
29
- import { routeLanguage } from "../language-routing";
28
+ import { detectTextLanguageFallback, routeLanguage } from "../language-routing";
30
29
  import { renderInvalidArg } from "./options";
31
30
 
32
31
  interface MainCommandArgs {
@@ -47,18 +46,7 @@ interface MainCommandArgs {
47
46
  "no-color": boolean;
48
47
  }
49
48
 
50
- /**
51
- * The CLI-side `tinyld` fallback, tagged `source: "tinyld"` so a consumer can
52
- * tell it apart from an Engine detection instead of reading the placeholder
53
- * `confidence: 0` it used to carry as a genuine zero (#941). The score is
54
- * tinyld's own and is not on the Engine's scale.
55
- */
56
- export function detectTextLanguageFallback(text: string): TextLangDetectResult | undefined {
57
- if (!text) return undefined;
58
- const [best] = detectAll(text);
59
- if (!best) return undefined;
60
- return { code: best.lang, confidence: best.accuracy, source: "tinyld" };
61
- }
49
+ export { detectTextLanguageFallback };
62
50
 
63
51
  export function detectLanguage(text: string): string {
64
52
  return detectTextLanguageFallback(text)?.code ?? "";
package/src/doctor.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { existsSync } from "fs";
2
- import { cacheComponentPaths, isInsideDir } from "./cache-layout";
2
+ import { cacheComponents, cacheTotalBytes } from "./cache-layout";
3
3
  import { errorMessage } from "./error-utils";
4
4
  import { humanBytes } from "./format";
5
5
  import { dirname, join } from "path";
@@ -270,14 +270,8 @@ function collectCache(
270
270
  ): DoctorReport["cache"] {
271
271
  const cache = keshaCacheDir();
272
272
  const binPath = getEngineBinPath();
273
- const engineDir = dirname(dirname(binPath));
274
- const components: CacheComponent[] = cacheComponentPaths(
275
- cache,
276
- engineDir,
277
- isCoremlBackend(backend),
278
- ).map((component) => ({ label: component.label, ...pathSummary(component.path) }));
279
- const engineOutsideCache = isInsideDir(engineDir, cache) ? 0 : dirSizeBytes(engineDir);
280
- const totalBytes = dirSizeBytes(cache) + engineOutsideCache;
273
+ const components: CacheComponent[] = cacheComponents(cache, binPath, isCoremlBackend(backend));
274
+ const totalBytes = cacheTotalBytes(cache, binPath);
281
275
  const externalRoots = fluidExternalRoots({ homeDir, cacheRoot: cache });
282
276
  const externalTotalBytes = fluidExternalTotalBytes(externalRoots);
283
277
 
@@ -233,6 +233,11 @@ function quoteInvalidLine(line: string): string {
233
233
  return chars.length <= MAX_QUOTED_LINE_CHARS ? line : `${chars.slice(0, MAX_QUOTED_LINE_CHARS).join("")}…`;
234
234
  }
235
235
 
236
+ /** One wording for an off-protocol line, whether it fails the run or only warns. */
237
+ export function offProtocolLine(command: string, line: string): string {
238
+ return `kesha-engine ${command} wrote a line that is not a protocol event: "${quoteInvalidLine(line)}"`;
239
+ }
240
+
236
241
  /** The KeshaError for a run that wrote a non-event line, reported an error event, or exited non-zero in silence; `stderr` is the transcript unless the caller substitutes one. */
237
242
  export function engineFailure(command: string, outcome: StderrOutcome, exitCode: number | undefined, stderr = outcome.stderr.trim()): KeshaError {
238
243
  const extra = { exitCode, stderr, origin: "engine" as const };
@@ -240,7 +245,7 @@ export function engineFailure(command: string, outcome: StderrOutcome, exitCode:
240
245
  const rest = stderr.split("\n").filter((line) => !outcome.invalid.includes(line)).join("\n").trim();
241
246
  return new KeshaError(
242
247
  "E_INTERNAL",
243
- `kesha-engine ${command} wrote a line that is not a protocol event: "${quoteInvalidLine(outcome.invalid[0]!)}"`,
248
+ offProtocolLine(command, outcome.invalid[0]!),
244
249
  { ...extra, stderr: rest },
245
250
  );
246
251
  }
@@ -0,0 +1,114 @@
1
+ import { errorMessage } from "../error-utils";
2
+ import { log } from "../log";
3
+ import { abortOnSignal, engineAbortError, pendingInterruption, registerProcessTree } from "../process-tree";
4
+ import { resolveStatePaths } from "../state-paths";
5
+ import { PROTOCOL_VERSION } from "./describe";
6
+ import { KeshaError, readEvents, type EventSinks, type StderrOutcome } from "./events";
7
+
8
+ type SpawnStdioEntry = "inherit" | "pipe" | "ignore";
9
+ type SpawnStdio = [SpawnStdioEntry, SpawnStdioEntry, SpawnStdioEntry];
10
+ type EngineProcess = ReturnType<typeof Bun.spawn>;
11
+
12
+ /** The env for a spawn whose stderr is parsed as protocol 4 events. */
13
+ function protocolEnv(): Record<string, string | undefined> {
14
+ return { ...process.env, KESHA_PROTOCOL: String(PROTOCOL_VERSION) };
15
+ }
16
+
17
+ export function spawnHint(): string {
18
+ return process.env.KESHA_ENGINE_BIN
19
+ ? "KESHA_ENGINE_BIN points at it; fix the path or unset it and run `kesha install`"
20
+ : "run `kesha install`";
21
+ }
22
+
23
+ /** The engine reads only `KESHA_CACHE_DIR`; it gets the root the CLI resolved whenever that differs from the raw value. */
24
+ function withResolvedCacheDir(env: Record<string, string | undefined>): Record<string, string | undefined> {
25
+ const cache = resolveStatePaths(env).cacheDir;
26
+ const raw = env.KESHA_CACHE_DIR;
27
+ if (cache.source === "default") {
28
+ if (raw === undefined) return env;
29
+ const { KESHA_CACHE_DIR: _empty, ...rest } = env;
30
+ return rest;
31
+ }
32
+ return raw === cache.path ? env : { ...env, KESHA_CACHE_DIR: cache.path };
33
+ }
34
+
35
+ /** `Bun.spawn` throws synchronously on ENOENT/EACCES; every launch failure becomes `E_ENGINE_SPAWN`. */
36
+ function spawnEngineProcess(
37
+ binPath: string,
38
+ args: string[],
39
+ stdio: SpawnStdio,
40
+ env: Record<string, string | undefined> = process.env,
41
+ ): EngineProcess {
42
+ const interrupted = pendingInterruption();
43
+ if (interrupted) throw interrupted;
44
+ try {
45
+ // `env` is passed explicitly: Bun snapshots process.env at startup otherwise (#874).
46
+ return Bun.spawn([binPath, ...args], { detached: true, stdio, env: withResolvedCacheDir(env) });
47
+ } catch (err) {
48
+ throw new KeshaError("E_ENGINE_SPAWN", `failed to launch kesha-engine at ${binPath}: ${errorMessage(err)}`, {
49
+ hint: spawnHint(),
50
+ });
51
+ }
52
+ }
53
+
54
+ /** An engine that refuses the run before reading closes the pipe; that EPIPE is its answer, reported by the exit status and stderr. */
55
+ async function writeEngineStdin(proc: EngineProcess, text: string): Promise<void> {
56
+ const sink = proc.stdin as Bun.FileSink;
57
+ try {
58
+ sink.write(text);
59
+ await sink.end();
60
+ } catch (err) {
61
+ log.debug(`stdin write failed: ${errorMessage(err)}`);
62
+ }
63
+ }
64
+
65
+ export interface EngineProcessOptions<T> {
66
+ /** `[stdin, stdout, stderr]`. A piped stderr is read as protocol 4 events and the engine is told so through its env. */
67
+ stdio: SpawnStdio;
68
+ /** Written to a piped stdin, which is then closed. */
69
+ stdin?: string;
70
+ /** Consumes a piped stdout; `proc` lets a relay stop the engine once nobody reads it. */
71
+ readStdout?: (stream: ReadableStream<Uint8Array>, proc: EngineProcess) => Promise<T>;
72
+ sinks?: EventSinks;
73
+ /** Aborting terminates the process tree, SIGKILL after a grace; an already-aborted signal is `E_INTERRUPTED` before any spawn. */
74
+ signal?: AbortSignal;
75
+ }
76
+
77
+ export interface EngineProcessRun<T> {
78
+ exitCode: number;
79
+ signalCode: string | null;
80
+ /** The signal fired while the engine ran: a caller's cancel, or a deadline it set with `AbortSignal.timeout`. */
81
+ aborted: boolean;
82
+ stdout: T;
83
+ events: StderrOutcome;
84
+ }
85
+
86
+ /** Spawns the engine (or a sidecar) as its own process tree and waits for it; the tree is unregistered however the run ends. */
87
+ export async function runEngineProcess<T = undefined>(
88
+ binPath: string,
89
+ args: string[],
90
+ opts: EngineProcessOptions<T>,
91
+ ): Promise<EngineProcessRun<T>> {
92
+ if (opts.signal?.aborted) throw engineAbortError();
93
+ const startedAt = performance.now();
94
+ log.debug(`spawn ${binPath} ${args.join(" ")}`);
95
+ const eventsOnStderr = opts.stdio[2] === "pipe";
96
+ const proc = spawnEngineProcess(binPath, args, opts.stdio, eventsOnStderr ? protocolEnv() : process.env);
97
+ const tree = registerProcessTree(proc);
98
+ const cancel = abortOnSignal(tree, opts.signal);
99
+ try {
100
+ const [stdout, events, exitCode] = await Promise.all([
101
+ opts.readStdout?.(proc.stdout as ReadableStream<Uint8Array>, proc),
102
+ eventsOnStderr
103
+ ? readEvents(proc.stderr as ReadableStream<Uint8Array>, opts.sinks)
104
+ : { stderr: "", error: null, invalid: [] },
105
+ proc.exited,
106
+ opts.stdin === undefined ? undefined : writeEngineStdin(proc, opts.stdin),
107
+ ]);
108
+ log.debug(`exit=${exitCode} dt=${Math.round(performance.now() - startedAt)}ms args=${JSON.stringify(args)}`);
109
+ return { exitCode, signalCode: proc.signalCode, aborted: cancel.aborted, stdout: stdout as T, events };
110
+ } finally {
111
+ cancel.dispose();
112
+ tree.dispose();
113
+ }
114
+ }
@@ -1,22 +1,14 @@
1
1
  import { existsSync } from "fs";
2
- import { errorMessage } from "./error-utils";
3
- import {
4
- getDescribe,
5
- getEngineBinPath,
6
- spawnEngineProcess,
7
- type EngineCapabilities,
8
- } from "./engine";
2
+ import { ENGINE_PROBE_TIMEOUT_MS, getDescribe, getEngineBinPath, type EngineCapabilities } from "./engine";
9
3
  import { describeToCapabilities } from "./engine/describe";
10
4
  import { KeshaError } from "./engine/events";
11
- import { registerProcessTree } from "./process-tree";
5
+ import { runEngineProcess } from "./engine/spawn";
12
6
 
13
7
  export type ExecutableHealth =
14
8
  | { status: "ok" }
15
9
  | { status: "missing" }
16
10
  | { status: "unusable"; detail: string };
17
11
 
18
- const PROBE_TIMEOUT_MS = 15_000;
19
-
20
12
  /**
21
13
  * Proves a downloaded binary actually executes — existence is not health (#770).
22
14
  *
@@ -28,39 +20,32 @@ const PROBE_TIMEOUT_MS = 15_000;
28
20
  * The timeout guards against a binary that starts but never exits; macOS Gatekeeper can take
29
21
  * several seconds to scan a freshly written binary on its first run, so it is generous.
30
22
  */
23
+ /** Message plus hint, not the rendered error: a caller wrapping it would otherwise nest a second code line. */
24
+ function spawnFailureDetail(err: unknown): string {
25
+ if (!(err instanceof Error)) return String(err);
26
+ return err instanceof KeshaError && err.hint ? `${err.message}; ${err.hint}` : err.message;
27
+ }
28
+
31
29
  export async function probeExecutable(
32
30
  binPath: string,
33
31
  args: string[] = [],
34
32
  ): Promise<ExecutableHealth> {
35
33
  if (!existsSync(binPath)) return { status: "missing" };
36
34
 
37
- let proc: ReturnType<typeof Bun.spawn>;
35
+ let run: Awaited<ReturnType<typeof runEngineProcess>>;
38
36
  try {
39
- proc = spawnEngineProcess(binPath, args, ["ignore", "ignore", "ignore"]);
37
+ run = await runEngineProcess(binPath, args, {
38
+ stdio: ["ignore", "ignore", "ignore"],
39
+ signal: AbortSignal.timeout(ENGINE_PROBE_TIMEOUT_MS),
40
+ });
40
41
  } catch (err) {
41
- return { status: "unusable", detail: errorMessage(err) };
42
- }
43
- const tree = registerProcessTree(proc);
44
-
45
- let timedOut = false;
46
- let forceKillTimer: Timer | undefined;
47
- const timer = setTimeout(() => {
48
- timedOut = true;
49
- tree.terminate();
50
- forceKillTimer = tree.forceKillAfterGrace();
51
- }, PROBE_TIMEOUT_MS);
52
- try {
53
- await proc.exited;
54
- } finally {
55
- clearTimeout(timer);
56
- tree.dispose();
57
- if (!timedOut && forceKillTimer) clearTimeout(forceKillTimer);
42
+ return { status: "unusable", detail: spawnFailureDetail(err) };
58
43
  }
59
44
 
60
- if (timedOut) {
61
- return { status: "unusable", detail: `no exit within ${PROBE_TIMEOUT_MS / 1000}s` };
45
+ if (run.aborted) {
46
+ return { status: "unusable", detail: `no exit within ${ENGINE_PROBE_TIMEOUT_MS / 1000}s` };
62
47
  }
63
- if (proc.signalCode) return { status: "unusable", detail: `killed by ${proc.signalCode}` };
48
+ if (run.signalCode) return { status: "unusable", detail: `killed by ${run.signalCode}` };
64
49
  return { status: "ok" };
65
50
  }
66
51
 
@@ -73,36 +58,20 @@ export async function probeExecutable(
73
58
  */
74
59
  export async function readExecutableVersion(
75
60
  binPath: string,
76
- timeoutMs = PROBE_TIMEOUT_MS,
61
+ timeoutMs = ENGINE_PROBE_TIMEOUT_MS,
77
62
  ): Promise<string | null> {
78
63
  if (!existsSync(binPath)) return null;
79
64
 
80
- let proc: ReturnType<typeof Bun.spawn>;
65
+ let stdout: string;
81
66
  try {
82
- proc = spawnEngineProcess(binPath, ["--version"], ["ignore", "pipe", "ignore"]);
67
+ ({ stdout } = await runEngineProcess(binPath, ["--version"], {
68
+ stdio: ["ignore", "pipe", "ignore"],
69
+ readStdout: (stream) => new Response(stream).text(),
70
+ signal: AbortSignal.timeout(timeoutMs),
71
+ }));
83
72
  } catch {
84
73
  return null;
85
74
  }
86
- const tree = registerProcessTree(proc);
87
-
88
- let timedOut = false;
89
- let forceKillTimer: Timer | undefined;
90
- const timer = setTimeout(() => {
91
- timedOut = true;
92
- tree.terminate();
93
- forceKillTimer = tree.forceKillAfterGrace();
94
- }, timeoutMs);
95
- let stdout: string;
96
- try {
97
- [stdout] = await Promise.all([
98
- new Response(proc.stdout as ReadableStream<Uint8Array>).text(),
99
- proc.exited,
100
- ]);
101
- } finally {
102
- clearTimeout(timer);
103
- tree.dispose();
104
- if (!timedOut && forceKillTimer) clearTimeout(forceKillTimer);
105
- }
106
75
  return /(\d+\.\d+\.\d+[0-9A-Za-z.+-]*)/.exec(stdout)?.[1] ?? null;
107
76
  }
108
77
 
@@ -132,7 +101,7 @@ const MUTE_DETAIL = "reports no capabilities";
132
101
  * that could not be made to answer (`unusable`), not one that answered nothing (`mute`).
133
102
  */
134
103
  export async function engineFunctionalHealth(
135
- timeoutMs = PROBE_TIMEOUT_MS,
104
+ timeoutMs = ENGINE_PROBE_TIMEOUT_MS,
136
105
  ): Promise<EngineFunctionalHealth> {
137
106
  const controller = new AbortController();
138
107
  const timer = setTimeout(() => controller.abort(), timeoutMs);