diff --git a/apps/server/src/speech/stream.test.ts b/apps/server/src/speech/stream.test.ts index 7ce8b76dd15d2..18f6e86e7613f 100644 --- a/apps/server/src/speech/stream.test.ts +++ b/apps/server/src/speech/stream.test.ts @@ -8,9 +8,16 @@ import * as Schema from "effect/Schema"; import * as Socket from "effect/socket/Socket"; import * as TestClock from "effect/testing/TestClock"; import { HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http"; -import { SpeechStreamEvent, DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS } from "@t3tools/contracts"; -import type { SpeechService, SpeechStream } from "./SpeechService.ts"; -import { runSpeechSocket } from "./stream.ts"; +import { + AuthOrchestrationOperateScope, + AuthSessionId, + SpeechStreamEvent, + DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, +} from "@t3tools/contracts"; +import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts"; +import * as SessionStore from "../auth/SessionStore.ts"; +import { SpeechService, type SpeechStream } from "./SpeechService.ts"; +import { runSpeechSocket, speechStreamRouteLayer } from "./stream.ts"; import type * as NetAddress from "effect/net/NetAddress"; const decodeEvent = Schema.decodeUnknownSync(Schema.fromJsonString(SpeechStreamEvent)); @@ -171,6 +178,76 @@ it.live("processes pipelined audio before the finish command", () => { ).pipe(Effect.scoped, Effect.provide(NodeHttpServer.layerTest)); }); +it.live("stops in-flight work, closes the socket, and releases the stream on invalidation", () => { + const feeding = Promise.withResolvers(); + const released = Promise.withResolvers(); + let feedInterrupted = false; + const sessionId = AuthSessionId.make("speech-test"); + const stream: SpeechStream = { + feed: () => + Effect.sync(() => feeding.resolve()).pipe( + Effect.andThen(Effect.never), + Effect.onInterrupt(() => Effect.sync(() => (feedInterrupted = true))), + ), + finish: Effect.never, + }; + return Effect.gen(function* () { + const invalidated = yield* Deferred.make(); + yield* Layer.build( + HttpRouter.serve(speechStreamRouteLayer, { + disableLogger: true, + disableListenLog: true, + }).pipe( + Layer.provide( + Layer.mock(EnvironmentAuth.EnvironmentAuth, { + authenticateWebSocketUpgrade: () => + Effect.succeed({ + sessionId, + subject: "speech-test", + method: "bearer-access-token", + scopes: [AuthOrchestrationOperateScope], + proofKeyThumbprint: "thumbprint", + }), + }), + ), + Layer.provide( + Layer.mock(SessionStore.SessionStore, { + cookieName: "test", + legacyCookieName: undefined, + awaitInvalidation: (id, proofKeyThumbprint) => + id === sessionId && proofKeyThumbprint === "thumbprint" + ? Deferred.await(invalidated) + : Effect.never, + }), + ), + Layer.provide( + Layer.mock(SpeechService, { + startStream: () => + Effect.acquireRelease(Effect.succeed(stream), () => + Effect.sync(() => released.resolve(feedInterrupted)), + ), + }), + ), + ), + ); + const server = yield* HttpServer.HttpServer; + const origin = HttpServer.formatAddress(server.address).replace(/^http/, "ws"); + const connection = client(`${origin}/ws/voice`); + yield* Effect.addFinalizer(() => Effect.sync(() => connection.socket.close())); + const closed = new Promise((resolve) => + connection.socket.addEventListener("close", () => resolve(), { once: true }), + ); + expect(yield* Effect.promise(() => connection.next())).toEqual({ type: "ready" }); + connection.socket.send(new Float32Array([0.25])); + yield* Effect.promise(() => feeding.promise); + yield* Deferred.succeed(invalidated, undefined); + const [, interruptedBeforeRelease] = yield* Effect.promise(() => + Promise.all([closed, released.promise]), + ); + expect(interruptedBeforeRelease).toBe(true); + }).pipe(Effect.scoped, Effect.provide(NodeHttpServer.layerTest)); +}); + it.effect("releases the stream when a started client goes silent", () => Effect.gen(function* () { const released = yield* Deferred.make(); diff --git a/apps/server/src/speech/stream.ts b/apps/server/src/speech/stream.ts index 8eb9b9626010b..7c432d30f8b9f 100644 --- a/apps/server/src/speech/stream.ts +++ b/apps/server/src/speech/stream.ts @@ -15,6 +15,7 @@ import * as Scope from "effect/Scope"; import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http"; import * as Socket from "effect/socket/Socket"; import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts"; +import * as SessionStore from "../auth/SessionStore.ts"; import * as SpeechService from "./SpeechService.ts"; // A recording client streams audio continuously; behind a proxy a vanished @@ -105,6 +106,8 @@ export const runSpeechSocket = Effect.fn("speech.runSocket")(function* ( } }); yield* Effect.raceFirst(read, idle).pipe( + // Stops the worker before the stream's slow cleanup, so it cannot send in the meantime. + Effect.scoped, Effect.timeoutOrElse({ duration: "10 minutes", // The socket read loop is already interrupted here; its writer can no longer send. @@ -118,6 +121,7 @@ export const speechStreamRouteLayer = Layer.unwrap( Effect.gen(function* () { const auth = yield* EnvironmentAuth.EnvironmentAuth; const speech = yield* SpeechService.SpeechService; + const sessions = yield* SessionStore.SessionStore; return HttpRouter.add( "GET", SPEECH_STREAM_PATH, @@ -130,7 +134,10 @@ export const speechStreamRouteLayer = Layer.unwrap( if (!session.success.scopes.includes(AuthOrchestrationOperateScope)) return HttpServerResponse.empty({ status: 403 }); const socket = yield* request.upgrade; - yield* runSpeechSocket(socket, speech).pipe(Effect.scoped); + yield* Effect.raceFirst( + runSpeechSocket(socket, speech).pipe(Effect.scoped), + sessions.awaitInvalidation(session.success.sessionId, session.success.proofKeyThumbprint), + ).pipe(Effect.ignore({ log: true })); return HttpServerResponse.empty(); }), );