Skip to content

Commit 904096d

Browse files
Merge speech engine recovery fixes into opt-in dictation
2 parents 66cae1b + 0e3dcff commit 904096d

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

‎apps/server/src/speech/SpeechModelStore.test.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,24 @@ const harness = Effect.gen(function* () {
7474
);
7575
}),
7676
);
77-
const store = yield* SpeechModelStore.SpeechModelStore.pipe(
77+
const openStore = SpeechModelStore.SpeechModelStore.pipe(
7878
Effect.provide(
7979
SpeechModelStore.layer.pipe(Layer.provide(Layer.succeed(HttpClient.HttpClient, client))),
8080
),
8181
);
82-
return { model, directory, stale, fs, requests, bodies, transportFailure, bodyStarted, store };
82+
const store = yield* openStore;
83+
return {
84+
model,
85+
directory,
86+
stale,
87+
fs,
88+
requests,
89+
bodies,
90+
transportFailure,
91+
bodyStarted,
92+
store,
93+
openStore,
94+
};
8395
});
8496

8597
const dependencies = ServerConfig.layerTest("/tmp", { prefix: "speech-model-store-" }).pipe(
@@ -137,7 +149,7 @@ it.effect("rejects a same-size download whose SHA-256 differs from the catalog",
137149
}).pipe(Effect.provide(dependencies)),
138150
);
139151

140-
it.effect("cancelling a download removes the partial file and keeps the existing model", () =>
152+
it.effect("cleans only download partials on cancellation or restart", () =>
141153
Effect.gen(function* () {
142154
const h = yield* harness;
143155
h.bodies.current = () => Stream.concat(Stream.succeed(bytes.subarray(0, SPLIT)), Stream.never);
@@ -146,5 +158,13 @@ it.effect("cancelling a download removes the partial file and keeps the existing
146158
yield* Fiber.interrupt(fiber);
147159
expect(yield* h.fs.readDirectory(h.directory)).toEqual(["model.gguf"]);
148160
expect([...(yield* h.fs.readFile(h.stale))]).toEqual([...new Uint8Array(bytes.length)]);
161+
const orphan = `${h.stale}.f660de6c-e172-4146-8ffe-d3c32eb48cd4.part`;
162+
yield* h.fs.writeFile(orphan, bytes.subarray(0, SPLIT));
163+
yield* h.fs.writeFileString(`${h.stale}.notes.part`, "not a download");
164+
yield* h.openStore;
165+
expect((yield* h.fs.readDirectory(h.directory)).sort()).toEqual([
166+
"model.gguf",
167+
"model.gguf.notes.part",
168+
]);
149169
}).pipe(Effect.provide(dependencies)),
150170
);

‎apps/server/src/speech/SpeechModelStore.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export class SpeechModelStore extends Context.Service<
5656
>()("t3/speech/SpeechModelStore") {}
5757

5858
const DOWNLOAD_ATTEMPTS = 3;
59+
const PARTIAL_FILENAME =
60+
/\.gguf\.[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\.part$/i;
5961

6062
const make = Effect.gen(function* () {
6163
const fs = yield* FileSystem.FileSystem;
@@ -64,6 +66,12 @@ const make = Effect.gen(function* () {
6466
const random = yield* SecureRandom.SecureRandom;
6567
const config = yield* ServerConfig.ServerConfig;
6668
const directory = path.join(config.stateDir, "speech", "models");
69+
// The server already owns this state directory; no download can be active before the store opens.
70+
for (const name of yield* fs.readDirectory(directory).pipe(Effect.orElseSucceed(() => []))) {
71+
if (PARTIAL_FILENAME.test(name)) {
72+
yield* fs.remove(path.join(directory, name), { force: true }).pipe(Effect.ignore);
73+
}
74+
}
6775
const modelPath = (model: ModelFile) => path.join(directory, model.filename);
6876
// Files whose digest failed, by identity, so status polls stop calling them ready.
6977
const corrupt = new Map<string, string>();

‎apps/server/src/speech/SpeechService.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as PlatformError from "effect/PlatformError";
1313
import * as HostProcess from "@t3tools/shared/HostProcess";
1414
import { DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS } from "@t3tools/contracts";
1515
import * as ServerSettings from "../serverSettings.ts";
16+
import { listNativeSpeechGpuDevices } from "./native.ts";
1617
import * as SpeechModelStore from "./SpeechModelStore.ts";
1718
import * as SpeechService from "./SpeechService.ts";
1819
import { SILERO_VAD } from "./silero.ts";
@@ -558,6 +559,24 @@ it.effect("reports unsupported hosts without wrapping a synthetic cause", () =>
558559
}),
559560
);
560561

562+
it.effect.each([{ devices: [] }, { devices: [{ id: "gpu", name: "Test GPU" }] }])(
563+
"retries failed GPU discovery without repeatedly probing on status polls: $devices",
564+
({ devices }) =>
565+
Effect.gen(function* () {
566+
const speech = yield* SpeechService.SpeechService;
567+
const discover = vi.mocked(listNativeSpeechGpuDevices).mockReset();
568+
discover.mockRejectedValueOnce(new Error("driver not ready")).mockResolvedValueOnce(devices);
569+
expect(yield* speech.status).toMatchObject({ gpuDevices: [] });
570+
yield* speech.status;
571+
expect(discover).toHaveBeenCalledOnce();
572+
yield* TestClock.adjust("1 minute");
573+
expect(yield* speech.status).toMatchObject({ gpuDevices: devices });
574+
yield* TestClock.adjust("1 minute");
575+
expect(yield* speech.status).toMatchObject({ gpuDevices: devices });
576+
expect(discover).toHaveBeenCalledTimes(2);
577+
}).pipe(Effect.provide(layer)),
578+
);
579+
561580
it.effect(
562581
"retries Auto-placed inference with the dictionary and the CPU's vocabulary capability",
563582
() =>

‎apps/server/src/speech/SpeechService.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
1010
import * as Cause from "effect/Cause";
1111
import * as Clock from "effect/Clock";
1212
import * as Context from "effect/Context";
13+
import * as Duration from "effect/Duration";
1314
import * as Effect from "effect/Effect";
1415
import * as Exit from "effect/Exit";
1516
import * as Fiber from "effect/Fiber";
@@ -179,7 +180,9 @@ const make = Effect.gen(function* () {
179180
let loadedAcceleration: string | undefined;
180181
let autoCpuModel: string | undefined;
181182
let loading: Promise<LoadedModel> | undefined;
182-
let gpuDevices: Promise<{ id: string; name: string }[]> | undefined;
183+
const discoverGpuDevices = yield* Effect.tryPromise(() => listNativeSpeechGpuDevices()).pipe(
184+
Effect.cachedWithTTL((exit) => (Exit.isSuccess(exit) ? Duration.infinity : "1 minute")),
185+
);
183186
let downloading:
184187
| {
185188
modelId: string;
@@ -455,9 +458,7 @@ const make = Effect.gen(function* () {
455458
supportsLanguageDetection: definition.supportsLanguageDetection,
456459
acceleration: settings.speechAcceleration,
457460
modelUnloadTimeout: settings.speechModelUnloadTimeout,
458-
gpuDevices: yield* Effect.promise(
459-
() => (gpuDevices ??= listNativeSpeechGpuDevices().catch(() => [])),
460-
),
461+
gpuDevices: yield* discoverGpuDevices.pipe(Effect.orElseSucceed(() => [])),
461462
};
462463
});
463464

0 commit comments

Comments
 (0)