diff --git a/config/main-sync-fs-baseline.txt b/config/main-sync-fs-baseline.txt index e106b4f0b3b..e2c5517cf6e 100644 --- a/config/main-sync-fs-baseline.txt +++ b/config/main-sync-fs-baseline.txt @@ -497,8 +497,8 @@ 2 src/main/speech/speech-model-download-cleanup.ts 1 src/main/speech/speech-model-download-transport.ts 2 src/main/speech/speech-model-http-download.ts +2 src/main/speech/stt-gpu-failure-marker.ts 1 src/main/speech/stt-worker-model-config.ts -2 src/main/speech/stt-worker-paths.ts 2 src/main/sqlite/harden-database-files.ts 1 src/main/sqlite/row-cache/keyed-json-row-cache.ts 2 src/main/sqlite/row-cache/legacy-json-source.ts @@ -609,6 +609,7 @@ 7 src/shared/serve-bundle-park.ts 1 src/shared/serve-user-data-path.ts 3 src/shared/source-scan/source-tree-scan.ts +3 src/shared/speech-recognizer-package.ts 5 src/shared/startup-command-staging.ts 1 src/shared/terminal/pty/native-process/pty-process-ceiling-state.ts 8 src/shared/windows-breakaway-launcher.ts diff --git a/src/main/speech/stt-gpu-failure-marker.ts b/src/main/speech/stt-gpu-failure-marker.ts index 2f04cc6e6d6..5a79cd52f22 100644 --- a/src/main/speech/stt-gpu-failure-marker.ts +++ b/src/main/speech/stt-gpu-failure-marker.ts @@ -1,4 +1,5 @@ -import { readFileSync, writeFileSync } from 'node:fs' +import { readFileSync } from 'node:fs' +import { writeFile } from 'node:fs/promises' import { release } from 'node:os' import { join } from 'node:path' import type { SttGpuPolicy } from './stt-worker-cpu-fallback' @@ -39,20 +40,23 @@ function readMarkerKey(file: string): string | null { /** `gpuEnabled` is the user's Voice setting; the marker records a GPU that failed on this machine. */ export function createSttGpuPolicy(modelsDir: string, gpuEnabled: () => boolean): SttGpuPolicy { const file = join(modelsDir, GPU_FAILURE_MARKER_FILE) + // Read once per process; this process's own failures update it in memory. + let failedKey: string | null | undefined return { allowGpu: () => { if (!gpuEnabled()) { return false } - const marker = readMarkerKey(file) - return marker === null || marker !== gpuFailureKey() + if (failedKey === undefined) { + failedKey = readMarkerKey(file) + } + return failedKey !== gpuFailureKey() }, markGpuFailed: () => { - try { - writeFileSync(file, JSON.stringify({ key: gpuFailureKey() })) - } catch (error) { + failedKey = gpuFailureKey() + writeFile(file, JSON.stringify({ key: failedKey })).catch((error: unknown) => { console.warn('[speech] Could not record the GPU failure:', error) - } + }) } } } diff --git a/src/main/speech/stt-transcribe-cpp-recognizer.ts b/src/main/speech/stt-transcribe-cpp-recognizer.ts index c04b205f962..5230e3a3446 100644 --- a/src/main/speech/stt-transcribe-cpp-recognizer.ts +++ b/src/main/speech/stt-transcribe-cpp-recognizer.ts @@ -1,4 +1,4 @@ -import { existsSync, readFileSync } from 'node:fs' +import { readFile } from 'node:fs/promises' import { availableParallelism } from 'node:os' import { join } from 'node:path' import { pathToFileURL } from 'node:url' @@ -17,11 +17,9 @@ function performanceCoreThreads(): number { // The hotwords file is written in sherpa's format, one `term :score` per line. The host deletes it // once dictation starts, so a CPU fallback's reload finds it gone and runs without. -function readVocabulary(path: string | undefined): string[] { - if (!path || !existsSync(path)) { - return [] - } - return readFileSync(path, 'utf8') +async function readVocabulary(path: string | undefined): Promise { + const text = path ? await readFile(path, 'utf8').catch(() => '') : '' + return text .split('\n') .map((line) => line.replace(/\s+:[\d.]+\s*$/, '').trim()) .filter(Boolean) @@ -46,7 +44,7 @@ export async function createTranscribeCppRecognizer( ) // The child runs one call at a time, so every stream can share one session. const session = model.createSession({ nThreads: performanceCoreThreads() }) - const vocabulary = readVocabulary(init.hotwordsFilePath) + const vocabulary = await readVocabulary(init.hotwordsFilePath) const options: TranscribeCpp.TranscribeOptions = { timestamps: 'none', ...(init.language && model.capabilities.languages.includes(init.language) diff --git a/src/main/speech/stt-worker-paths.ts b/src/main/speech/stt-worker-paths.ts index 6b7b3a4fd1b..f8e3edf8ede 100644 --- a/src/main/speech/stt-worker-paths.ts +++ b/src/main/speech/stt-worker-paths.ts @@ -36,9 +36,20 @@ function speechNodeModulesDirCandidates(): string[] { return require.resolve.paths('transcribe-cpp') ?? [] } +// The app's engine packages don't change while it runs, so each engine is looked up once. +const speechEngineModulesDirs = new Map() + /** The node_modules directory holding `engine`, or null on hosts without it. */ export function getSpeechEngineModulesDir(engine: SpeechEngine): string | null { - return speechNodeModulesDirCandidates().find((dir) => hasSpeechEnginePackage(dir, engine)) ?? null + let dir = speechEngineModulesDirs.get(engine) + if (dir === undefined) { + dir = + speechNodeModulesDirCandidates().find((candidate) => + hasSpeechEnginePackage(candidate, engine) + ) ?? null + speechEngineModulesDirs.set(engine, dir) + } + return dir } /** Whether this host ships the engine `model` runs on. */