import { type ServerConfig, type ServerConfigStreamEvent, WsSubscribeServerConfigRpc, WS_METHODS, } from "@t3tools/contracts"; import { joinSocketFrameParts, SOCKET_FRAME_PARTS_QUERY_PARAM, } from "@t3tools/shared/socketFrameParts"; import * as Cause from "effect/Cause"; import * as Clock from "effect/Clock"; import * as Context from "effect/Context"; import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Equal from "effect/Equal"; import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as PubSub from "effect/PubSub"; import * as Ref from "effect/Ref"; import * as Schedule from "effect/Schedule"; import * as Schema from "effect/Schema"; import type * as Scope from "effect/Scope"; import * as Stream from "effect/Stream"; import type * as Rpc from "effect/rpc/Rpc"; import * as RpcClient from "effect/rpc/RpcClient"; import * as RpcClientError from "effect/rpc/RpcClientError"; import * as RpcSerialization from "effect/rpc/RpcSerialization"; import * as Socket from "effect/socket/Socket"; import { makeWsRpcProtocolClient, PING_TIMEOUT, type WsRpcProtocolClient } from "./protocol.ts"; import { NETWORK_BLOCKING_HINT, UNRESPONSIVE_HOST_HINT } from "../errors/network.ts"; import type { ConnectionAttemptError, ConnectionTransientError, PreparedConnection, } from "../connection/model.ts"; import { ConnectionBlockedError, ConnectionTransientError as ConnectionTransientErrorClass, } from "../connection/model.ts"; import { applyServerConfigProjection, type ServerConfigProjection, withoutEnvironmentThemes, } from "../state/serverConfigProjection.ts"; import { environmentMismatchError } from "../connection/errors.ts"; import { recordServerClock } from "../clock.ts"; const SOCKET_OPEN_TIMEOUT = "15 seconds"; export interface RpcSession { readonly client: WsRpcProtocolClient; readonly initialConfig: Effect.Effect; readonly subscribeServerConfig: ( input?: ServerConfigSubscriptionInput, ) => ServerConfigSubscription; readonly ready: Effect.Effect; readonly probe: Effect.Effect; /** Frames received so far: proof the socket is alive that needs no reply. */ readonly inboundFrames: Effect.Effect; readonly closed: Effect.Effect; } export interface RpcSessionOptions { readonly environmentThemes?: boolean; readonly usageLimitSources?: boolean; readonly usageLimitsCommand?: boolean; readonly providerCatalogsOnDemand?: boolean; readonly providerCatalogDigests?: boolean; readonly providerStatusDeltas?: boolean; } export class RpcSessionFactory extends Context.Service< RpcSessionFactory, { readonly connect: ( connection: PreparedConnection, ) => Effect.Effect; } >()("@t3tools/client-runtime/rpc/session/RpcSessionFactory") {} type InitialConfigError = Effect.Error< ReturnType >; type ProbeError = Effect.Error>; type ServerConfigSubscriptionError = | Rpc.ErrorExit | RpcClientError.RpcClientError; type ServerConfigSubscription = Stream.Stream< ServerConfigStreamEvent, ServerConfigSubscriptionError >; type ServerConfigSubscriptionInput = Parameters< WsRpcProtocolClient[typeof WS_METHODS.subscribeServerConfig] >[0]; type EnvironmentThemesUpdatedEvent = Extract< ServerConfigStreamEvent, { readonly type: "environmentThemesUpdated" } >; type UsageLimitSourcesUpdatedEvent = Extract< ServerConfigStreamEvent, { readonly type: "usageLimitSourcesUpdated" } >; interface ServerConfigReplayState { readonly projection: ServerConfigProjection; readonly revision: number; readonly themesEvent: EnvironmentThemesUpdatedEvent | undefined; readonly sourcesEvent: UsageLimitSourcesUpdatedEvent | undefined; } interface BufferedServerConfigEvent { readonly event: ServerConfigStreamEvent; readonly replay: ServerConfigReplayState; readonly revision: number; } function serverConfigReplayEvents( state: ServerConfigReplayState, ): ReadonlyArray { const snapshot = { version: 1 as const, type: "snapshot" as const, config: withoutEnvironmentThemes(state.projection.config), }; return [ snapshot, ...(state.themesEvent === undefined ? [] : [state.themesEvent]), ...(state.sourcesEvent === undefined ? [] : [state.sourcesEvent]), ]; } const isSocketErrorReason = Schema.is(Socket.SocketErrorReason); const PING_TIMEOUT_SECONDS = Duration.toSeconds(PING_TIMEOUT); function mapSessionRpcError( error: InitialConfigError | ProbeError | ServerConfigSubscriptionError | ConnectionAttemptError, via: "relay" | "direct", socketFailure: () => ConnectionTransientError, ): ConnectionAttemptError { switch (error._tag) { case "ConnectionBlockedError": case "ConnectionTransientError": return error; case "EnvironmentAuthorizationError": return new ConnectionBlockedError({ reason: "permission", detail: error.message, hop: "server", via, }); case "KeybindingsConfigParseError": case "ServerSettingsError": return new ConnectionTransientErrorClass({ reason: "remote-unavailable", detail: error.message, hop: "server", via, }); case "RpcClientError": return isSocketErrorReason(error.reason) ? socketFailure() : new ConnectionTransientErrorClass({ reason: "transport", detail: error.message, ...socketFailureLocation(via), }); } } /** Over T3 Connect the relay already reached the host, so a socket that drops failed between this device and the tunnel. */ function socketFailureLocation(via: "relay" | "direct") { return { hop: via === "relay" ? "tunnel" : "server", via } as const; } /** @public */ export const make = Effect.fn("RpcSessionFactory.make")(function* ( options: RpcSessionOptions = {}, ) { const webSocketConstructor = yield* Socket.WebSocketConstructor; const serverConfigInput: ServerConfigSubscriptionInput = { ...(options.environmentThemes === true ? { environmentThemes: true } : {}), ...(options.usageLimitSources === true ? { usageLimitSources: true } : {}), ...(options.usageLimitsCommand === true ? { usageLimitsCommand: true } : {}), ...(options.providerCatalogsOnDemand === true ? { providerCatalogsOnDemand: true } : {}), ...(options.providerCatalogDigests === true ? { providerCatalogDigests: true } : {}), ...(options.providerStatusDeltas === true ? { providerStatusDeltas: true } : {}), }; // Config state merges the snapshots it fetches back in, so no subscriber needs them broadcast. const onDemandSnapshots = (input: ServerConfigSubscriptionInput) => ({ ...input, workspaceSnapshotsOnDemand: true, }); const connect = Effect.fnUntraced(function* (connection: PreparedConnection) { const via = connection.target._tag === "RelayConnectionTarget" ? "relay" : "direct"; const networkHint = via === "relay" ? ` ${NETWORK_BLOCKING_HINT}` : ""; yield* Effect.annotateCurrentSpan({ "connection.environment.id": connection.environmentId, }); const connected = yield* Deferred.make(); const disconnected = yield* Deferred.make(); let pingTimedOut = false; const socketFailure = () => new ConnectionTransientErrorClass({ reason: "transport", // The network hint only fits a socket that never opened. Once it // opened, the path worked, and a host that goes quiet is more // often overloaded or asleep than blocked. detail: !Deferred.isDoneUnsafe(connected) ? `${connection.label} could not establish a WebSocket connection.${networkHint}` : pingTimedOut ? `${connection.label} stopped responding for ${PING_TIMEOUT_SECONDS}s. ${UNRESPONSIVE_HOST_HINT}` : `${connection.label} disconnected.`, ...socketFailureLocation(via), }); const mapRpcError = (error: Parameters[0]) => mapSessionRpcError(error, via, socketFailure); const hooks = RpcClient.ConnectionHooks.of({ onConnect: Deferred.succeed(connected, undefined).pipe(Effect.asVoid), onDisconnect: Effect.suspend(() => Deferred.fail(disconnected, socketFailure())).pipe( Effect.asVoid, ), }); // The protocol never reopens its socket, so every frame counted here came // from this session's one socket. let inboundFrames = 0; const countingWebSocketConstructor: typeof webSocketConstructor = (url, options) => { const socket = webSocketConstructor(url, options); socket.addEventListener("message", () => { inboundFrames += 1; }); return socket; }; const socketUrl = new URL(connection.socketUrl); socketUrl.searchParams.set(SOCKET_FRAME_PARTS_QUERY_PARAM, "1"); const layerSocket = Layer.effect( Socket.Socket, Effect.map(Effect.service(Socket.Socket), joinSocketFrameParts), ).pipe( Layer.provide( Socket.layerWebSocket(socketUrl.toString(), { openTimeout: SOCKET_OPEN_TIMEOUT, }), ), Layer.provide(Layer.succeed(Socket.WebSocketConstructor, countingWebSocketConstructor)), ); const layerProtocol = Layer.effect( RpcClient.Protocol, RpcClient.makeProtocolSocket({ pingTimeout: PING_TIMEOUT, retryTransientErrors: false, retryPolicy: Schedule.recurs(0), // RpcClient runs this before it fails the open requests. onPingTimeout: Effect.sync(() => { pingTimedOut = true; }), }).pipe( // Teardown sends an interrupt per open stream, and a write to a closed // socket waits out the client's 1s interrupt timeout before reconnecting. // Requests still go through, so they fail with the protocol error. Effect.map((protocol) => ({ ...protocol, send: (clientId, request, transferables) => request._tag !== "Request" && Deferred.isDoneUnsafe(disconnected) ? Effect.void : protocol.send(clientId, request, transferables), })), ), ).pipe( Layer.provide( Layer.mergeAll( layerSocket, RpcSerialization.layerJson, Layer.succeed(RpcClient.ConnectionHooks, hooks), ), ), ); const protocolContext = yield* Layer.build(layerProtocol).pipe( Effect.withSpan("environment.websocket.connect"), ); // Building the protocol only starts the socket; this span times the // handshake up to the first open, without making `connect` wait for it. yield* Deferred.await(connected).pipe( Effect.raceFirst(Deferred.await(disconnected)), Effect.withSpan("environment.websocket.open"), Effect.ignore, Effect.forkScoped, ); const protocolClient = yield* makeWsRpcProtocolClient.pipe(Effect.provide(protocolContext)); const initialConfigDeferred = yield* Deferred.make(); const serverConfigExit = yield* Deferred.make< void, ServerConfigSubscriptionError | ConnectionAttemptError >(); const configSubscriptionClosed = yield* Deferred.make(); const serverConfigState = yield* Ref.make(Option.none()); const serverConfigUpdates = yield* PubSub.sliding(64); const configSubscriptionEndedError = new ConnectionTransientErrorClass({ reason: "remote-unavailable", detail: `${connection.label} config subscription ended.`, }); // An interrupt while the session is not closing came from the server; it ends the subscription. let closingSession = false; const configRequestedAtMs = yield* Clock.currentTimeMillis; const serverConfigSource = protocolClient[WS_METHODS.subscribeServerConfig]( onDemandSnapshots(serverConfigInput), ).pipe( Stream.runForEach((event) => Effect.gen(function* () { const buffered = yield* Ref.modify(serverConfigState, (current) => { const projection = applyServerConfigProjection( Option.map(current, (state) => state.projection), event, ); if (Option.isNone(projection)) { return [Option.none(), current] as const; } const next = { projection: projection.value, revision: Option.match(current, { onNone: () => 1, onSome: (state) => state.revision + 1, }), themesEvent: event.type === "environmentThemesUpdated" ? event : Option.getOrUndefined(current)?.themesEvent, sourcesEvent: event.type === "usageLimitSourcesUpdated" ? event : Option.getOrUndefined(current)?.sourcesEvent, } satisfies ServerConfigReplayState; return [ Option.some({ event, replay: next, revision: next.revision }), Option.some(next), ] as const; }); if (Option.isSome(buffered)) { yield* PubSub.publish(serverConfigUpdates, buffered.value); } if (event.type === "snapshot") { // Later snapshots may repeat the first one's serverTime. Record // before readiness so the first timers already use its clock. if (!(yield* Deferred.isDone(initialConfigDeferred))) { const { serverTime } = event.config; recordServerClock(connection.environmentId, { serverTimeMs: serverTime === undefined ? undefined : Date.parse(serverTime), requestedAtMs: configRequestedAtMs, receivedAtMs: yield* Clock.currentTimeMillis, }); } yield* Deferred.succeed(initialConfigDeferred, event.config); } }), ), // An unreadable first snapshot means the versions are incompatible. A later // unreadable update ends the session, and the reconnect's snapshot decides. Effect.catchDefect((defect) => Schema.isSchemaError(defect) ? Effect.logWarning("Server configuration failed to decode.", { environmentId: connection.environmentId, issue: defect.message, }).pipe( Effect.andThen( Effect.fail( Deferred.isDoneUnsafe(initialConfigDeferred) ? new ConnectionTransientErrorClass({ reason: "remote-unavailable", detail: `${connection.label} sent a server configuration update this client could not read.`, hop: "server", via, }) : new ConnectionBlockedError({ reason: "unsupported", detail: `${connection.label} sent an incompatible server configuration. Update the client and server to compatible versions.`, hop: "server", via, }), ), ), ) : Effect.die(defect), ), Effect.onExit((exit) => { if (Exit.isSuccess(exit) || (Cause.hasInterruptsOnly(exit.cause) && !closingSession)) { return Effect.all([ Deferred.succeed(serverConfigExit, undefined), Deferred.fail(configSubscriptionClosed, configSubscriptionEndedError), ]).pipe(Effect.asVoid); } if (Cause.hasInterruptsOnly(exit.cause)) { return Effect.void; } return Effect.all([ Deferred.failCause(serverConfigExit, exit.cause), Deferred.failCause(configSubscriptionClosed, Cause.map(exit.cause, mapRpcError)), ]).pipe(Effect.asVoid); }), ); yield* serverConfigSource.pipe(Effect.forkScoped); // Finalizers run in reverse, so this is set before the fork above is interrupted. yield* Effect.addFinalizer(() => Effect.sync(() => { closingSession = true; }), ); const initialConfig = Effect.raceFirst( Deferred.await(initialConfigDeferred), Deferred.await(serverConfigExit).pipe( Effect.mapError(mapRpcError), Effect.flatMap(() => Effect.fail(configSubscriptionEndedError)), ), ).pipe( Effect.filterOrElse( (config) => config.environment.environmentId === connection.environmentId, (config) => environmentMismatchError({ expected: connection.environmentId, actual: config.environment.environmentId, }), ), Effect.withSpan("environment.initialSync"), ); const serverConfigEvents = Stream.unwrap( Effect.gen(function* () { const subscription = yield* PubSub.subscribe(serverConfigUpdates); const snapshot = yield* Ref.get(serverConfigState); if (Option.isNone(snapshot)) { return Stream.empty; } const updates = Stream.fromSubscription(subscription).pipe( Stream.filter((buffered) => buffered.revision > snapshot.value.revision), Stream.mapAccum( () => snapshot.value.revision, (revision, buffered) => [ buffered.revision, buffered.revision === revision + 1 ? [buffered.event] : serverConfigReplayEvents(buffered.replay), ], ), ); const terminal = Stream.fromEffect(Deferred.await(serverConfigExit)).pipe(Stream.drain); return Stream.concat( Stream.fromIterable(serverConfigReplayEvents(snapshot.value)), Stream.merge(updates, terminal, { haltStrategy: "either" }), ); }), ).pipe( Stream.catchCause((cause) => { if (Cause.hasInterruptsOnly(cause)) { return Stream.failCause( Cause.fromReasons(cause.reasons.filter(Cause.isInterruptReason)), ); } // The supervisor keeps the original cause. Shared durable consumers // need a transport-shaped failure so they wait for its replacement. return Stream.fail( new RpcClientError.RpcClientError({ reason: new RpcClientError.RpcClientDefect({ message: `${connection.label} config subscription failed.`, cause, }), }), ); }), ); const validatedInitialConfig = initialConfig.pipe( Effect.mapError( (cause) => new RpcClientError.RpcClientError({ reason: new RpcClientError.RpcClientDefect({ message: `${connection.label} config subscription failed.`, cause, }), }), ), ); const subscribeServerConfig = (input: ServerConfigSubscriptionInput = serverConfigInput) => Stream.unwrap( validatedInitialConfig.pipe( Effect.as( Equal.equals(input, serverConfigInput) ? serverConfigEvents : protocolClient[WS_METHODS.subscribeServerConfig](onDemandSnapshots(input)), ), ), ); const probe = initialConfig.pipe( Effect.flatMap(() => protocolClient[WS_METHODS.serverProbe]({}).pipe(Effect.mapError(mapRpcError)), ), Effect.asVoid, Effect.withSpan("clientRuntime.connection.rpcSession.probe"), ); return { client: protocolClient, initialConfig, subscribeServerConfig, ready: initialConfig.pipe(Effect.asVoid), probe, inboundFrames: Effect.sync(() => inboundFrames), // The config stream also fails on disconnect, and only it can report a decode failure. closed: Deferred.await(configSubscriptionClosed), } satisfies RpcSession; }); return RpcSessionFactory.of({ connect }); }); export const layer = (options: RpcSessionOptions) => Layer.effect(RpcSessionFactory, make(options));