Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(speech): point Metal at the unpacked shader source in the packaged app

Dev Goswami7adc7fb039a5
diff --git a/CLAUDE.md b/CLAUDE.md
index fc24cfa36..ec03ac3c2 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -9898,6 +9898,15 @@ Voice-to-text input captured via microphone, turned into terminal text via on-de
SIMD flags in ggml), a GPU backend (this smart-whisper has no Vulkan build option), cancelling a
running transcription, and the Server Edition (installed with `--ignore-scripts`, so it keeps the
baseline; the startup log line `[speech] whisper binary: …` says which binary loaded).
+- **macOS: smart-whisper's Metal shader path is patched to the unpacked copy** (second patch in
+ `scripts/patch-smart-whisper.mjs`, marker `NODETERM-PATCH(smart-whisper-metal-asar)`). smart-whisper
+ sets `GGML_METAL_PATH_RESOURCES` from its own `__dirname`, which in the packaged app is inside
+ `app.asar`; native code cannot read inside an asar ("Not a directory"), so Metal init failed and
+ whisper.cpp ran on the CPU with no error shown. The shader file is already unpacked (asarUnpack), so
+ the patch rewrites `app.asar` to `app.asar.unpacked` in that path. MEASURED with the app's own binary
+ on an M5 Pro, large-v3-turbo, 2.7 s clip: 5.97 s on the CPU, 0.64 s on Metal; in the app, dictation
+ went from several seconds to under one. A dev install and an explicit `GGML_METAL_PATH_RESOURCES`
+ are unchanged.
## Packaging & auto-update
diff --git a/scripts/patch-smart-whisper.d.mts b/scripts/patch-smart-whisper.d.mts
index 25b15f9b2..6b33e2f9c 100644
--- a/scripts/patch-smart-whisper.d.mts
+++ b/scripts/patch-smart-whisper.d.mts
@@ -4,3 +4,4 @@ export declare const PATCH_MARKER: string
export declare const VARIANT_TARGET: string
export declare const SIMD_FLAGS: readonly string[]
export declare function patchSmartWhisper(dir: string): 'patched' | 'already-patched'
+export declare const METAL_MARKER: string
diff --git a/scripts/patch-smart-whisper.mjs b/scripts/patch-smart-whisper.mjs
index dbf65eaca..4984a5bb0 100644
--- a/scripts/patch-smart-whisper.mjs
+++ b/scripts/patch-smart-whisper.mjs
@@ -28,6 +28,15 @@
* no-op when nothing sets the global.
* - Runs before electron-rebuild in `postinstall` / `rebuild`, so the native build sees it.
*
+ * SECOND PATCH: Metal shaders in a packaged app (macOS)
+ * smart-whisper tells whisper.cpp where its Metal shader source is through
+ * GGML_METAL_PATH_RESOURCES, built from its own __dirname. In the packaged app dist/ is inside
+ * app.asar, and native code cannot read inside an asar ("Not a directory"), so Metal init fails
+ * and whisper.cpp silently runs on the CPU. The shader file is already unpacked beside it
+ * (asarUnpack), so the patch points at app.asar.unpacked. Measured on an M5 Pro, Electron 42,
+ * large-v3-turbo, 2.7 s clip, the app's own binary: 5.97 s on the CPU vs 0.64 s on Metal. An
+ * explicit GGML_METAL_PATH_RESOURCES and a dev install are left unchanged.
+ *
* GUARD: src/core/speech/smart-whisper-patch.test.ts asserts the marker is present in the
* installed sources. If it is red, your node_modules is unpatched — run `npm run rebuild`.
*/
@@ -65,6 +74,14 @@ var module2 = (() => {
const GYP_FIND = ' }\n ],\n}'
+export const METAL_MARKER = 'NODETERM-PATCH(smart-whisper-metal-asar)'
+
+const METAL_FIND =
+ 'process.env.GGML_METAL_PATH_RESOURCES = process.env.GGML_METAL_PATH_RESOURCES || import_node_path.default.join(__dirname, "../whisper.cpp/ggml/src");'
+
+const METAL_REPLACE = `// ${METAL_MARKER}: native code cannot read inside app.asar; use the unpacked copy.
+process.env.GGML_METAL_PATH_RESOURCES = process.env.GGML_METAL_PATH_RESOURCES || import_node_path.default.join(__dirname, "../whisper.cpp/ggml/src").replace(/([\\\\/])app\\.asar(?=[\\\\/])/, "$1app.asar.unpacked");`
+
/** Patches the smart-whisper package in `dir`. Returns 'patched' | 'already-patched'. */
export function patchSmartWhisper(dir) {
const gypPath = path.join(dir, 'binding.gyp')
@@ -73,11 +90,16 @@ export function patchSmartWhisper(dir) {
const js = fs.readFileSync(jsPath, 'utf8')
const gypDone = gyp.includes(PATCH_MARKER)
const jsDone = js.includes(PATCH_MARKER)
- if (gypDone && jsDone) return 'already-patched'
+ const metalDone = js.includes(METAL_MARKER)
+ if (gypDone && jsDone && metalDone) return 'already-patched'
- if (!jsDone) {
- expectOnce(js, LOADER_FIND, 'dist/index.js binary require')
- fs.writeFileSync(jsPath, js.replace(LOADER_FIND, LOADER_REPLACE))
+ if (!jsDone || !metalDone) {
+ if (!jsDone) expectOnce(js, LOADER_FIND, 'dist/index.js binary require')
+ if (!metalDone) expectOnce(js, METAL_FIND, 'dist/index.js Metal path')
+ let out = js
+ if (!jsDone) out = out.replace(LOADER_FIND, LOADER_REPLACE)
+ if (!metalDone) out = out.replace(METAL_FIND, METAL_REPLACE)
+ fs.writeFileSync(jsPath, out)
}
if (!gypDone) {
diff --git a/src/core/speech/__fixtures__/smart-whisper-0.8.1/dist-index.js b/src/core/speech/__fixtures__/smart-whisper-0.8.1/dist-index.js
index 35d54e11c..eb48259b2 100644
--- a/src/core/speech/__fixtures__/smart-whisper-0.8.1/dist-index.js
+++ b/src/core/speech/__fixtures__/smart-whisper-0.8.1/dist-index.js
@@ -1,5 +1,6 @@
// The loader lines of smart-whisper 0.8.1's dist/index.js, verbatim, for patch-smart-whisper tests.
var import_node_path = { default: require("path") };
+process.env.GGML_METAL_PATH_RESOURCES = process.env.GGML_METAL_PATH_RESOURCES || import_node_path.default.join(__dirname, "../whisper.cpp/ggml/src");
var module2 = require(import_node_path.default.join(__dirname, "../build/Release/smart-whisper"));
var binding = module2;
module.exports = { binding };
diff --git a/src/core/speech/smart-whisper-patch.test.ts b/src/core/speech/smart-whisper-patch.test.ts
index 478142637..5b0af9045 100644
--- a/src/core/speech/smart-whisper-patch.test.ts
+++ b/src/core/speech/smart-whisper-patch.test.ts
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
import fs from 'node:fs'
import path from 'node:path'
import { createRequire } from 'node:module'
-import { PATCH_MARKER, SIMD_FLAGS, VARIANT_TARGET, patchSmartWhisper } from '../../../scripts/patch-smart-whisper.mjs'
+import { METAL_MARKER, PATCH_MARKER, SIMD_FLAGS, VARIANT_TARGET, patchSmartWhisper } from '../../../scripts/patch-smart-whisper.mjs'
import { WHISPER_SIMD_FLAGS } from './speech-service'
import { testTmpDir } from '../test-tmp'
@@ -17,8 +17,7 @@ const installed = fs.existsSync(path.join(PKG, 'binding.gyp'))
/** Pristine smart-whisper 0.8.1 binding.gyp + the loader lines of its dist/index.js (dist-index.js). */
const FIXTURE = path.resolve(__dirname, '__fixtures__/smart-whisper-0.8.1')
-function copyPackage(): string {
- const dir = testTmpDir('sw-patch-')
+function copyPackage(dir = testTmpDir('sw-patch-')): string {
fs.mkdirSync(path.join(dir, 'dist'), { recursive: true })
fs.copyFileSync(path.join(FIXTURE, 'binding.gyp'), path.join(dir, 'binding.gyp'))
// Stored flat: the repo's .gitignore drops every dist/ directory.
@@ -31,6 +30,7 @@ describe('patch-smart-whisper', () => {
const howto = 'node_modules is unpatched — run `npm run rebuild`.'
expect(fs.readFileSync(path.join(PKG, 'binding.gyp'), 'utf8'), howto).toContain(PATCH_MARKER)
expect(fs.readFileSync(path.join(PKG, 'dist', 'index.js'), 'utf8'), howto).toContain(PATCH_MARKER)
+ expect(fs.readFileSync(path.join(PKG, 'dist', 'index.js'), 'utf8'), howto).toContain(METAL_MARKER)
})
it('builds for exactly the CPU flags the runtime checks', () => {
@@ -59,6 +59,52 @@ describe('patch-smart-whisper', () => {
expect(() => patchSmartWhisper(dir)).toThrow(/anchor/)
})
+ it('refuses a dist/index.js whose Metal path line moved', () => {
+ const dir = copyPackage()
+ const js = path.join(dir, 'dist', 'index.js')
+ fs.writeFileSync(js, fs.readFileSync(js, 'utf8').replace(/^process\.env\.GGML_METAL.*\n/m, ''))
+ expect(() => patchSmartWhisper(dir)).toThrow(/Metal/)
+ })
+
+ describe('Metal shader path', () => {
+ const KEY = 'GGML_METAL_PATH_RESOURCES'
+
+ /** Loads a patched package from `pkgDir` and returns the shader dir it hands whisper.cpp. */
+ function metalPath(pkgDir: string, preset?: string): string | undefined {
+ copyPackage(pkgDir)
+ patchSmartWhisper(pkgDir)
+ const rel = path.join(pkgDir, 'build', 'Release')
+ fs.mkdirSync(rel, { recursive: true })
+ fs.writeFileSync(path.join(rel, 'smart-whisper.js'), 'module.exports = {}')
+ const saved = process.env[KEY]
+ if (preset === undefined) delete process.env[KEY]
+ else process.env[KEY] = preset
+ try {
+ createRequire(path.join(pkgDir, 'dist', 'index.js'))('./index.js')
+ return process.env[KEY]
+ } finally {
+ if (saved === undefined) delete process.env[KEY]
+ else process.env[KEY] = saved
+ }
+ }
+
+ it('points a packaged app at the unpacked copy, which native code can read', () => {
+ // Inside app.asar the path is "Not a directory" to Metal, so whisper.cpp ran on the CPU.
+ const res = path.join(testTmpDir('sw-metal-'), 'Resources')
+ const pkg = path.join(res, 'app.asar', 'node_modules', 'smart-whisper')
+ expect(metalPath(pkg)).toBe(path.join(res, 'app.asar.unpacked', 'node_modules', 'smart-whisper', 'whisper.cpp', 'ggml', 'src'))
+ })
+
+ it('leaves a dev install, an unpacked path and an explicit setting alone', () => {
+ const dev = path.join(testTmpDir('sw-metal-dev-'), 'node_modules', 'smart-whisper')
+ expect(metalPath(dev)).toBe(path.join(dev, 'whisper.cpp', 'ggml', 'src'))
+ const unpacked = path.join(testTmpDir('sw-metal-unp-'), 'app.asar.unpacked', 'node_modules', 'smart-whisper')
+ expect(metalPath(unpacked)).toBe(path.join(unpacked, 'whisper.cpp', 'ggml', 'src'))
+ const asar = path.join(testTmpDir('sw-metal-set-'), 'app.asar', 'node_modules', 'smart-whisper')
+ expect(metalPath(asar, '/custom/metal')).toBe('/custom/metal')
+ })
+ })
+
describe('patched loader', () => {
type G = { __NODETERM_WHISPER_BINARY__?: string; __NODETERM_WHISPER_LOADED__?: string }
const g = globalThis as G