forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.ts
More file actions
524 lines (499 loc) · 19.2 KB
/
Copy pathsession.ts
File metadata and controls
524 lines (499 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import {
type ServerConfig,
type ServerConfigStreamEvent,
WsSubscribeServerConfigRpc,
WS_METHODS,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as Deferred from "effect/Deferred";
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/unstable/rpc/Rpc";
import * as RpcClient from "effect/unstable/rpc/RpcClient";
import * as RpcClientError from "effect/unstable/rpc/RpcClientError";
import * as RpcSerialization from "effect/unstable/rpc/RpcSerialization";
import * as Socket from "effect/unstable/socket/Socket";
import { makeWsRpcProtocolClient, type WsRpcProtocolClient } from "./protocol.ts";
import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
import type {
ConnectionAttemptError,
ConnectionTransientError,
ConnectionTransientReason,
PreparedConnection,
} from "../connection/model.ts";
import {
ConnectionBlockedError,
ConnectionTransientError as ConnectionTransientErrorClass,
} from "../connection/model.ts";
import { formatDisconnectDetail, type SocketCloseCapture } from "../connection/disconnectDetail.ts";
import * as ConnectionDiagnosticsLog from "../connection/diagnosticsLog.ts";
import {
applyServerConfigProjection,
type ServerConfigProjection,
withoutEnvironmentThemes,
} from "../state/serverConfigProjection.ts";
const SOCKET_OPEN_TIMEOUT = "15 seconds";
/** Mutable sink filled before onDisconnect so we never emit a bare "disconnected." */
type DisconnectCauseSink = {
causeMessage?: string;
reason?: ConnectionTransientReason;
close?: SocketCloseCapture;
};
function socketHostFromUrl(socketUrl: string): string | undefined {
try {
return new URL(socketUrl).host;
} catch {
return undefined;
}
}
function captureSocketClose(
webSocketConstructor: (url: string, protocols?: string | string[]) => globalThis.WebSocket,
sink: { current: SocketCloseCapture },
): (url: string, protocols?: string | string[]) => globalThis.WebSocket {
return (url, protocols) => {
const socket = webSocketConstructor(url, protocols);
socket.addEventListener(
"close",
(event) => {
const closeEvent = event as CloseEvent;
sink.current = {
code: typeof closeEvent.code === "number" ? closeEvent.code : undefined,
reason: typeof closeEvent.reason === "string" ? closeEvent.reason : undefined,
};
},
{ once: true },
);
return socket;
};
}
function causeTextOf(cause: unknown, fallback: string): string {
if (cause instanceof Error) return cause.message;
if (typeof cause === "string") return cause;
return fallback;
}
function noteSocketError(sink: DisconnectCauseSink, error: Socket.SocketError): void {
const reason = error.reason;
switch (reason._tag) {
case "SocketCloseError": {
sink.close = {
code: reason.code,
reason: reason.closeReason,
};
// Prefer close-code formatting over a generic SocketCloseError string.
sink.reason ??= "transport";
return;
}
case "SocketOpenError": {
const causeText = causeTextOf(reason.cause, reason.kind);
const lower = causeText.toLowerCase();
if (lower.includes("ping timeout")) {
sink.causeMessage = "ping timeout";
sink.reason = "timeout";
return;
}
// WebSocket openTimeout (not keepalive) — leave cause empty so formatters use open wording.
if (reason.kind === "Timeout" && (lower.includes("open") || lower.includes("waiting"))) {
sink.reason ??= "timeout";
return;
}
sink.causeMessage ??= causeText;
sink.reason ??= lower.includes("timeout") ? "timeout" : "transport";
return;
}
case "SocketReadError":
case "SocketWriteError": {
sink.causeMessage ??= causeTextOf(reason.cause, reason._tag);
sink.reason ??= "transport";
return;
}
}
}
function mergeCloseCapture(
fromEvent: SocketCloseCapture,
fromError: SocketCloseCapture | undefined,
): SocketCloseCapture {
return {
code: fromEvent.code ?? fromError?.code,
reason: fromEvent.reason ?? fromError?.reason,
};
}
/**
* Wrap a Socket so transport failures are recorded before ConnectionHooks.onDisconnect.
* onDisconnect alone only sees an empty close capture when the failure is a ping timeout
* (socket still open; browser close event fires later/async).
*/
function captureSocketFailures(socket: Socket.Socket, sink: DisconnectCauseSink): Socket.Socket {
return Socket.make({
runRaw: (handler, options) =>
socket.runRaw(handler, options).pipe(
Effect.tapError((error) =>
Effect.sync(() => {
if (Socket.SocketError.is(error)) {
noteSocketError(sink, error);
}
}),
),
),
writer: socket.writer,
});
}
export interface RpcSession {
readonly client: WsRpcProtocolClient;
readonly initialConfig: Effect.Effect<ServerConfig, ConnectionAttemptError>;
readonly subscribeServerConfig: (
input: ServerConfigSubscriptionInput,
) => ServerConfigSubscription;
readonly ready: Effect.Effect<void, ConnectionAttemptError>;
readonly probe: Effect.Effect<void, ConnectionAttemptError>;
readonly closed: Effect.Effect<never, ConnectionAttemptError>;
}
export interface RpcSessionOptions {
readonly environmentThemes?: boolean;
readonly usageLimitSources?: boolean;
/** This client answers /usage-limits itself, so the server may advertise it. */
readonly usageLimitsCommand?: boolean;
}
export class RpcSessionFactory extends Context.Service<
RpcSessionFactory,
{
readonly connect: (
connection: PreparedConnection,
) => Effect.Effect<RpcSession, ConnectionAttemptError, Scope.Scope>;
}
>()("@t3tools/client-runtime/rpc/session/RpcSessionFactory") {}
type InitialConfigError = Effect.Error<
ReturnType<WsRpcProtocolClient[typeof WS_METHODS.serverGetConfig]>
>;
type ProbeError = Effect.Error<ReturnType<WsRpcProtocolClient[typeof WS_METHODS.serverProbe]>>;
type ServerConfigSubscriptionError =
| Rpc.ErrorExit<typeof WsSubscribeServerConfigRpc>
| 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<ServerConfigStreamEvent> {
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);
function mapSessionRpcError(
error: InitialConfigError | ProbeError | ServerConfigSubscriptionError,
networkHint: string,
): ConnectionAttemptError {
switch (error._tag) {
case "EnvironmentAuthorizationError":
return new ConnectionBlockedError({
reason: "permission",
detail: error.message,
});
case "KeybindingsConfigParseError":
case "ServerSettingsError":
return new ConnectionTransientErrorClass({
reason: "remote-unavailable",
detail: error.message,
});
case "RpcClientError": {
const lower = error.message.toLowerCase();
if (lower.includes("ping timeout")) {
return new ConnectionTransientErrorClass({
reason: "timeout",
detail: "ping timeout",
});
}
return new ConnectionTransientErrorClass({
reason: "transport",
detail: `${error.message}${isSocketErrorReason(error.reason) ? networkHint : ""}`,
});
}
}
}
/** @public Service construction is part of the canonical Effect module API. */
export const make = Effect.fn("RpcSessionFactory.make")(function* (
options: RpcSessionOptions = {},
) {
const webSocketConstructor = yield* Socket.WebSocketConstructor;
const diagnosticsLog = yield* Effect.serviceOption(
ConnectionDiagnosticsLog.ConnectionDiagnosticsLog,
);
const serverConfigInput: ServerConfigSubscriptionInput = {
...(options.environmentThemes === true ? { environmentThemes: true } : {}),
...(options.usageLimitSources === true ? { usageLimitSources: true } : {}),
...(options.usageLimitsCommand === true ? { usageLimitsCommand: true } : {}),
};
const connect = Effect.fnUntraced(function* (connection: PreparedConnection) {
const networkHint =
connection.target._tag === "RelayConnectionTarget" ? ` ${NETWORK_BLOCKING_HINT}` : "";
const mapRpcError = (error: Parameters<typeof mapSessionRpcError>[0]) =>
mapSessionRpcError(error, networkHint);
yield* Effect.annotateCurrentSpan({
"connection.environment.id": connection.environmentId,
});
const connected = yield* Deferred.make<void>();
const disconnected = yield* Deferred.make<never, ConnectionTransientError>();
const closeCapture: { current: SocketCloseCapture } = { current: {} };
const causeSink: DisconnectCauseSink = {};
const trackedConstructor = captureSocketClose(webSocketConstructor, closeCapture);
const hooks = RpcClient.ConnectionHooks.of({
onConnect: Deferred.succeed(connected, undefined).pipe(Effect.asVoid),
// Fork patch: runs before the protocol fails the socket with SocketOpenError(ping timeout).
onPingTimeout: Effect.sync(() => {
causeSink.causeMessage = "ping timeout";
causeSink.reason = "timeout";
}),
onDisconnect: Deferred.isDone(connected).pipe(
Effect.flatMap((wasConnected) => {
const close = mergeCloseCapture(closeCapture.current, causeSink.close);
const detail = `${formatDisconnectDetail({
label: connection.label,
wasConnected,
close,
causeMessage: causeSink.causeMessage,
})}${networkHint}`;
const error = new ConnectionTransientErrorClass({
reason: causeSink.reason ?? "transport",
detail,
});
const record = Option.match(diagnosticsLog, {
onNone: () => Effect.void,
onSome: (log) =>
log.record({
environmentId: connection.environmentId,
label: connection.label,
kind: wasConnected ? "disconnect" : "connect_failed",
reason: error.reason,
detail: error.detail,
closeCode: close.code,
closeReason: close.reason,
socketHost: socketHostFromUrl(connection.socketUrl),
}),
});
return record.pipe(Effect.andThen(Deferred.fail(disconnected, error)), Effect.asVoid);
}),
),
});
// Build socket, wrap to capture SocketError (close codes / open errors), then protocol.
const protocolLayer = Layer.effect(
RpcClient.Protocol,
Effect.gen(function* () {
const rawSocket = yield* Socket.makeWebSocket(connection.socketUrl, {
openTimeout: SOCKET_OPEN_TIMEOUT,
}).pipe(Effect.provideService(Socket.WebSocketConstructor, trackedConstructor));
const socket = captureSocketFailures(rawSocket, causeSink);
return yield* RpcClient.makeProtocolSocket({
retryTransientErrors: false,
retryPolicy: Schedule.recurs(0),
}).pipe(
Effect.provideService(Socket.Socket, socket),
Effect.provide(RpcSerialization.layerJson),
Effect.provideService(RpcClient.ConnectionHooks, hooks),
);
}),
);
const protocolContext = yield* Layer.build(protocolLayer).pipe(
Effect.withSpan("environment.websocket.connect"),
);
const protocolClient = yield* makeWsRpcProtocolClient.pipe(Effect.provide(protocolContext));
const initialConfigDeferred = yield* Deferred.make<ServerConfig>();
const serverConfigExit = yield* Deferred.make<void, ServerConfigSubscriptionError>();
const configSubscriptionClosed = yield* Deferred.make<never, ConnectionAttemptError>();
const serverConfigState = yield* Ref.make(Option.none<ServerConfigReplayState>());
const serverConfigUpdates = yield* PubSub.sliding<BufferedServerConfigEvent>(64);
const configSubscriptionEndedError = new ConnectionTransientErrorClass({
reason: "remote-unavailable",
detail: `${connection.label} config subscription ended.`,
});
const serverConfigSource = protocolClient[WS_METHODS.subscribeServerConfig](
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<BufferedServerConfigEvent>(), current] as const;
}
const next = {
projection: projection.value,
revision: Option.match(current, {
onNone: () => 1,
onSome: (state) => state.revision + 1,
}),
themesEvent:
event.type === "environmentThemesUpdated"
? event
: event.type === "snapshot" &&
event.config.environment.capabilities.environmentThemes !== true
? undefined
: Option.getOrUndefined(current)?.themesEvent,
sourcesEvent:
event.type === "usageLimitSourcesUpdated"
? event
: event.type === "snapshot" &&
event.config.environment.capabilities.usageLimitSources !== true
? undefined
: 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") {
yield* Deferred.succeed(initialConfigDeferred, event.config);
}
}),
),
Effect.onExit((exit) => {
if (Exit.isSuccess(exit)) {
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);
const initialConfig = Effect.raceFirst(
Deferred.await(initialConfigDeferred),
Deferred.await(serverConfigExit).pipe(
Effect.mapError(mapRpcError),
Effect.flatMap(() => Effect.fail(configSubscriptionEndedError)),
),
).pipe(Effect.withSpan("environment.initialSync"));
const serverConfigEvents = Stream.unwrap(
Effect.gen(function* () {
const subscription = yield* PubSub.subscribe(serverConfigUpdates);
yield* Effect.raceFirst(
Deferred.await(initialConfigDeferred).pipe(Effect.asVoid),
Deferred.await(serverConfigExit),
);
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);
}
// 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 subscribeServerConfig = (input: ServerConfigSubscriptionInput) =>
Equal.equals(input, serverConfigInput)
? serverConfigEvents
: protocolClient[WS_METHODS.subscribeServerConfig](input);
const probe = initialConfig.pipe(
Effect.flatMap((config) =>
(config.environment.capabilities.connectionProbe === true
? protocolClient[WS_METHODS.serverProbe]({})
: protocolClient[WS_METHODS.serverGetConfig]({})
).pipe(Effect.mapError(mapRpcError)),
),
Effect.asVoid,
Effect.withSpan("clientRuntime.connection.rpcSession.probe"),
);
return {
client: protocolClient,
initialConfig,
subscribeServerConfig,
ready: Deferred.await(connected).pipe(
Effect.andThen(initialConfig),
Effect.asVoid,
Effect.raceFirst(Deferred.await(disconnected)),
),
probe,
closed: Effect.raceFirst(
Deferred.await(disconnected),
Deferred.await(configSubscriptionClosed),
),
} satisfies RpcSession;
});
return RpcSessionFactory.of({ connect });
});
export const layerWithOptions = (options: RpcSessionOptions) =>
Layer.effect(RpcSessionFactory, make(options));
export const layer = layerWithOptions({});