@devries-cameron20 / t3code Mirror · read-only / commits / 0ad1e9d9e4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 25, 03:45 UTC

[codex] Refactor client-runtime Effect services (#3198)

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Jun 19, 2026 at 22:31 UTC 0ad1e9d9e4bf691ad1dcfb5af69de11b0b48047f
44 files changed +1281 -1186
apps/mobile/src/connection/catalog-store.ts
+1 -1
@@ -18,7 +18,7 @@ export const LEGACY_CONNECTIONS_KEY = "t3code.connections";
18 function catalogError(operation: string, cause: unknown) {
19 return new ConnectionTransientError({
20 reason: "remote-unavailable",
21 - message: `Could not ${operation} the local connection catalog: ${String(cause)}`,
21 + detail: `Could not ${operation} the local connection catalog: ${String(cause)}`,
22 });
23 }
24
apps/mobile/src/connection/platform.ts
+53 -47
@@ -10,8 +10,8 @@ import {
10 import {
11 ConnectionBlockedError,
12 ConnectionTransientError,
13 - ConnectionWakeups,
13 Connectivity,
14 + Wakeups,
15 } from "@t3tools/client-runtime/connection";
16 import { managedRelayAccountChanges, managedRelaySessionAtom } from "@t3tools/client-runtime/relay";
17 import { AuthStandardClientScopes } from "@t3tools/contracts";
@@ -41,53 +41,47 @@ function networkStatus(state: Network.NetworkState): "unknown" | "offline" | "on
41 return "unknown";
42 }
43
44 -const connectivityLayer = Layer.succeed(
45 - Connectivity,
46 - Connectivity.of({
47 - status: Effect.tryPromise({
48 - try: () => Network.getNetworkStateAsync(),
49 - catch: () => undefined,
50 - }).pipe(
51 - Effect.match({
52 - onFailure: () => "unknown" as const,
53 - onSuccess: networkStatus,
54 - }),
55 - ),
56 - changes: Stream.callback((queue) =>
44 +const connectivityLayer = Connectivity.layer({
45 + status: Effect.tryPromise({
46 + try: () => Network.getNetworkStateAsync(),
47 + catch: () => undefined,
48 + }).pipe(
49 + Effect.match({
50 + onFailure: () => "unknown" as const,
51 + onSuccess: networkStatus,
52 + }),
53 + ),
54 + changes: Stream.callback((queue) =>
55 + Effect.acquireRelease(
56 + Effect.sync(() =>
57 + Network.addNetworkStateListener((state) => {
58 + Queue.offerUnsafe(queue, networkStatus(state));
59 + }),
60 + ),
61 + (subscription) => Effect.sync(() => subscription.remove()),
62 + ).pipe(Effect.asVoid),
63 + ),
64 +});
65 +
66 +const wakeupsLayer = Wakeups.layer({
67 + changes: Stream.merge(
68 + Stream.callback<"application-active">((queue) =>
69 Effect.acquireRelease(
70 Effect.sync(() =>
59 - Network.addNetworkStateListener((state) => {
60 - Queue.offerUnsafe(queue, networkStatus(state));
71 + AppState.addEventListener("change", (state) => {
72 + if (state === "active") {
73 + Queue.offerUnsafe(queue, "application-active");
74 + }
75 }),
76 ),
77 (subscription) => Effect.sync(() => subscription.remove()),
78 ).pipe(Effect.asVoid),
79 ),
66 - }),
67 -);
68 -
69 -const wakeupsLayer = Layer.succeed(
70 - ConnectionWakeups,
71 - ConnectionWakeups.of({
72 - changes: Stream.merge(
73 - Stream.callback<"application-active">((queue) =>
74 - Effect.acquireRelease(
75 - Effect.sync(() =>
76 - AppState.addEventListener("change", (state) => {
77 - if (state === "active") {
78 - Queue.offerUnsafe(queue, "application-active");
79 - }
80 - }),
81 - ),
82 - (subscription) => Effect.sync(() => subscription.remove()),
83 - ).pipe(Effect.asVoid),
84 - ),
85 - managedRelayAccountChanges(appAtomRegistry).pipe(
86 - Stream.map(() => "credentials-changed" as const),
87 - ),
80 + managedRelayAccountChanges(appAtomRegistry).pipe(
81 + Stream.map(() => "credentials-changed" as const),
82 ),
89 - }),
90 -);
83 + ),
84 +});
85
86 const capabilitiesLayer = Layer.succeedContext(
87 Context.make(
@@ -98,7 +92,7 @@ const capabilitiesLayer = Layer.succeedContext(
92 if (session === null) {
93 return yield* new ConnectionBlockedError({
94 reason: "authentication",
101 - message: "Sign in to T3 Cloud to connect this environment.",
95 + detail: "Sign in to T3 Cloud to connect this environment.",
96 });
97 }
98 const token = yield* session.readClerkToken().pipe(
@@ -106,14 +100,14 @@ const capabilitiesLayer = Layer.succeedContext(
100 (error) =>
101 new ConnectionTransientError({
102 reason: "network",
109 - message: error.message,
103 + detail: error.message,
104 }),
105 ),
106 );
107 if (token === null) {
108 return yield* new ConnectionBlockedError({
109 reason: "authentication",
116 - message: "The T3 Cloud session is unavailable.",
110 + detail: "The T3 Cloud session is unavailable.",
111 });
112 }
113 return token;
@@ -132,7 +126,7 @@ const capabilitiesLayer = Layer.succeedContext(
126 catch: (cause) =>
127 new ConnectionTransientError({
128 reason: "remote-unavailable",
135 - message: `Could not load the mobile device identity: ${String(cause)}`,
129 + detail: `Could not load the mobile device identity: ${String(cause)}`,
130 }),
131 }).pipe(Effect.map(Option.some)),
132 }),
@@ -151,14 +145,14 @@ const capabilitiesLayer = Layer.succeedContext(
145 Effect.fail(
146 new ConnectionBlockedError({
147 reason: "unsupported",
154 - message: "SSH environments are only available in the desktop app.",
148 + detail: "SSH environments are only available in the desktop app.",
149 }),
150 ),
151 prepare: () =>
152 Effect.fail(
153 new ConnectionBlockedError({
154 reason: "unsupported",
161 - message: "SSH environments are only available in the desktop app.",
155 + detail: "SSH environments are only available in the desktop app.",
156 }),
157 ),
158 disconnect: () => Effect.void,
@@ -195,7 +189,19 @@ const environmentOwnedDataCleanupLayer = Layer.succeed(
189 }),
190 );
191
198 -export const connectionPlatformLayer = Layer.mergeAll(
192 +type ConnectionPlatformLayerSource =
193 + | typeof connectionStorageLayer
194 + | typeof connectivityLayer
195 + | typeof wakeupsLayer
196 + | typeof capabilitiesLayer
197 + | typeof platformConnectionSourceLayer
198 + | typeof environmentOwnedDataCleanupLayer;
199 +
200 +export const connectionPlatformLayer: Layer.Layer<
201 + Layer.Success<ConnectionPlatformLayerSource>,
202 + Layer.Error<ConnectionPlatformLayerSource>,
203 + Layer.Services<ConnectionPlatformLayerSource>
204 +> = Layer.mergeAll(
205 connectionStorageLayer,
206 connectivityLayer,
207 wakeupsLayer,
apps/mobile/src/connection/runtime.ts
+18 -6
@@ -1,16 +1,28 @@
1 -import { connectionLayer as clientConnectionLayer } from "@t3tools/client-runtime/connection";
1 +import { Connection } from "@t3tools/client-runtime/connection";
2 import * as Layer from "effect/Layer";
3 import { Atom } from "effect/unstable/reactivity";
4
5 import { runtimeContextLayer } from "../lib/runtime";
6 import { connectionPlatformLayer } from "./platform";
7
8 -const providedConnectionPlatformLayer = connectionPlatformLayer.pipe(
9 - Layer.provide(runtimeContextLayer),
10 -);
8 +const providedConnectionPlatformLayer: Layer.Layer<
9 + Layer.Success<typeof connectionPlatformLayer>,
10 + Layer.Error<typeof connectionPlatformLayer>
11 +> = connectionPlatformLayer.pipe(Layer.provide(runtimeContextLayer));
12 +
13 +type ConnectionLayerSource =
14 + | typeof Connection.layer
15 + | typeof runtimeContextLayer
16 + | typeof providedConnectionPlatformLayer;
17
12 -export const connectionLayer = clientConnectionLayer.pipe(
18 +export const connectionLayer: Layer.Layer<
19 + Layer.Success<ConnectionLayerSource>,
20 + Layer.Error<ConnectionLayerSource>
21 +> = Connection.layer.pipe(
22 Layer.provideMerge(Layer.mergeAll(runtimeContextLayer, providedConnectionPlatformLayer)),
23 );
24
16 -export const connectionAtomRuntime = Atom.runtime(connectionLayer);
25 +export const connectionAtomRuntime: Atom.AtomRuntime<
26 + Layer.Success<typeof connectionLayer>,
27 + Layer.Error<typeof connectionLayer>
28 +> = Atom.runtime(connectionLayer);
apps/mobile/src/connection/storage.ts
+10 -10
@@ -8,11 +8,11 @@ import {
8 removeCatalogValue,
9 replaceCatalogValue,
10 } from "@t3tools/client-runtime/platform";
11 -import { RemoteDpopAccessTokenStore } from "@t3tools/client-runtime/authorization";
11 +import { TokenStore } from "@t3tools/client-runtime/authorization";
12 import {
13 - ConnectionCredentialStore,
14 - ConnectionProfileStore,
13 ConnectionTransientError,
14 + CredentialStore,
15 + ProfileStore,
16 } from "@t3tools/client-runtime/connection";
17 import {
18 EnvironmentId,
@@ -58,7 +58,7 @@ const LegacyStoredShellSnapshot = Schema.Struct({
58 function catalogError(operation: string, cause: unknown) {
59 return new ConnectionTransientError({
60 reason: "remote-unavailable",
61 - message: `Could not ${operation} the local connection catalog: ${String(cause)}`,
61 + detail: `Could not ${operation} the local connection catalog: ${String(cause)}`,
62 });
63 }
64
@@ -197,7 +197,7 @@ export const connectionStorageLayer = Layer.effectContext(
197 .update((document) => removeConnectionFromCatalog(document, target))
198 .pipe(Effect.mapError((error) => targetPersistenceError("remove-connection", error))),
199 });
200 - const profileStore = ConnectionProfileStore.of({
200 + const profileStore = ProfileStore.make({
201 get: (connectionId) =>
202 catalog.read.pipe(
203 Effect.map((document) =>
@@ -221,7 +221,7 @@ export const connectionStorageLayer = Layer.effectContext(
221 ),
222 })),
223 });
224 - const credentialStore = ConnectionCredentialStore.of({
224 + const credentialStore = CredentialStore.make({
225 get: (connectionId) =>
226 catalog.read.pipe(
227 Effect.map((document) =>
@@ -248,7 +248,7 @@ export const connectionStorageLayer = Layer.effectContext(
248 ),
249 })),
250 });
251 - const remoteTokenStore = RemoteDpopAccessTokenStore.of({
251 + const remoteTokenStore = TokenStore.make({
252 get: (environmentId) =>
253 catalog.read.pipe(
254 Effect.map((document) =>
@@ -423,9 +423,9 @@ export const connectionStorageLayer = Layer.effectContext(
423
424 return Context.make(ConnectionTargetStore, targetStore).pipe(
425 Context.add(ConnectionRegistrationStore, registrationStore),
426 - Context.add(ConnectionProfileStore, profileStore),
427 - Context.add(ConnectionCredentialStore, credentialStore),
428 - Context.add(RemoteDpopAccessTokenStore, remoteTokenStore),
426 + Context.add(ProfileStore.ConnectionProfileStore, profileStore),
427 + Context.add(CredentialStore.ConnectionCredentialStore, credentialStore),
428 + Context.add(TokenStore.RemoteDpopAccessTokenStore, remoteTokenStore),
429 Context.add(EnvironmentCacheStore, cacheStore),
430 );
431 }),
apps/web/src/cloud/linkEnvironment.test.ts
+2 -4
@@ -15,9 +15,7 @@ import { HttpClient } from "effect/unstable/http";
15 import { afterEach, beforeEach, vi } from "vite-plus/test";
16 import {
17 AVAILABLE_CONNECTION_STATE,
18 - type EnvironmentRegistryService,
18 EnvironmentSupervisor,
20 - type EnvironmentSupervisorService,
19 type PreparedConnection,
20 PrimaryConnectionTarget,
21 } from "@t3tools/client-runtime/connection";
@@ -114,13 +112,13 @@ function registryLayer(options?: {
112 connect: Effect.void,
113 disconnect: Effect.void,
114 retryNow: Effect.void,
117 - } satisfies EnvironmentSupervisorService);
115 + } satisfies EnvironmentSupervisor["Service"]);
116 const registry = {
117 run: <A, E, R>(_environmentId: EnvironmentId, effect: Effect.Effect<A, E, R>) =>
118 Effect.provideService(effect, EnvironmentSupervisor, supervisor),
119 runStream: <A, E, R>(_environmentId: EnvironmentId, stream: Stream.Stream<A, E, R>) =>
120 Stream.provideService(stream, EnvironmentSupervisor, supervisor),
123 - } as unknown as EnvironmentRegistryService;
121 + } as unknown as EnvironmentRegistry["Service"];
122 return EnvironmentRegistry.of(registry);
123 }),
124 );
apps/web/src/connection/platform.ts
+63 -56
@@ -10,11 +10,11 @@ import {
10 import {
11 ConnectionBlockedError,
12 ConnectionTransientError,
13 - ConnectionWakeups,
13 Connectivity,
14 mapRemoteEnvironmentError,
15 PrimaryConnectionRegistration,
16 PrimaryConnectionTarget,
17 + Wakeups,
18 } from "@t3tools/client-runtime/connection";
19 import { fetchRemoteEnvironmentDescriptor } from "@t3tools/client-runtime/environment";
20 import { managedRelayAccountChanges, managedRelaySessionAtom } from "@t3tools/client-runtime/relay";
@@ -50,56 +50,50 @@ function currentNetworkStatus(): "unknown" | "offline" | "online" {
50 return navigator.onLine ? "online" : "offline";
51 }
52
53 -const connectivityLayer = Layer.succeed(
54 - Connectivity,
55 - Connectivity.of({
56 - status: Effect.sync(currentNetworkStatus),
57 - changes: Stream.callback((queue) =>
53 +const connectivityLayer = Connectivity.layer({
54 + status: Effect.sync(currentNetworkStatus),
55 + changes: Stream.callback((queue) =>
56 + Effect.acquireRelease(
57 + Effect.sync(() => {
58 + const online = () => Queue.offerUnsafe(queue, "online");
59 + const offline = () => Queue.offerUnsafe(queue, "offline");
60 + window.addEventListener("online", online);
61 + window.addEventListener("offline", offline);
62 + return { online, offline };
63 + }),
64 + ({ online, offline }) =>
65 + Effect.sync(() => {
66 + window.removeEventListener("online", online);
67 + window.removeEventListener("offline", offline);
68 + }),
69 + ).pipe(Effect.asVoid),
70 + ),
71 +});
72 +
73 +const wakeupsLayer = Wakeups.layer({
74 + changes: Stream.merge(
75 + Stream.callback<"application-active">((queue) =>
76 Effect.acquireRelease(
77 Effect.sync(() => {
60 - const online = () => Queue.offerUnsafe(queue, "online");
61 - const offline = () => Queue.offerUnsafe(queue, "offline");
62 - window.addEventListener("online", online);
63 - window.addEventListener("offline", offline);
64 - return { online, offline };
78 + const listener = () => {
79 + if (document.visibilityState === "visible") {
80 + Queue.offerUnsafe(queue, "application-active");
81 + }
82 + };
83 + document.addEventListener("visibilitychange", listener);
84 + return listener;
85 }),
66 - ({ online, offline }) =>
86 + (listener) =>
87 Effect.sync(() => {
68 - window.removeEventListener("online", online);
69 - window.removeEventListener("offline", offline);
88 + document.removeEventListener("visibilitychange", listener);
89 }),
90 ).pipe(Effect.asVoid),
91 ),
73 - }),
74 -);
75 -
76 -const wakeupsLayer = Layer.succeed(
77 - ConnectionWakeups,
78 - ConnectionWakeups.of({
79 - changes: Stream.merge(
80 - Stream.callback<"application-active">((queue) =>
81 - Effect.acquireRelease(
82 - Effect.sync(() => {
83 - const listener = () => {
84 - if (document.visibilityState === "visible") {
85 - Queue.offerUnsafe(queue, "application-active");
86 - }
87 - };
88 - document.addEventListener("visibilitychange", listener);
89 - return listener;
90 - }),
91 - (listener) =>
92 - Effect.sync(() => {
93 - document.removeEventListener("visibilitychange", listener);
94 - }),
95 - ).pipe(Effect.asVoid),
96 - ),
97 - managedRelayAccountChanges(appAtomRegistry).pipe(
98 - Stream.map(() => "credentials-changed" as const),
99 - ),
92 + managedRelayAccountChanges(appAtomRegistry).pipe(
93 + Stream.map(() => "credentials-changed" as const),
94 ),
101 - }),
102 -);
95 + ),
96 +});
97
98 function clientMetadata() {
99 const desktop = window.desktopBridge !== undefined;
@@ -116,12 +110,12 @@ function sshPreparationError(cause: unknown) {
110 if (message.toLowerCase().includes("cancel")) {
111 return new ConnectionBlockedError({
112 reason: "authentication",
119 - message,
113 + detail: message,
114 });
115 }
116 return new ConnectionTransientError({
117 reason: "remote-unavailable",
124 - message: `Could not prepare the SSH environment: ${message}`,
118 + detail: `Could not prepare the SSH environment: ${message}`,
119 });
120 }
121
@@ -139,7 +133,7 @@ export const provisionDesktopSshEnvironment = Effect.fn(
133 if (pairingToken === null) {
134 return yield* new ConnectionBlockedError({
135 reason: "authentication",
142 - message: "The SSH environment did not issue a pairing credential.",
136 + detail: "The SSH environment did not issue a pairing credential.",
137 });
138 }
139 const descriptor = yield* Effect.tryPromise({
@@ -170,7 +164,7 @@ const capabilitiesLayer = Layer.effectContext(
164 if (session === null) {
165 return yield* new ConnectionBlockedError({
166 reason: "authentication",
173 - message: "Sign in to T3 Cloud to connect this environment.",
167 + detail: "Sign in to T3 Cloud to connect this environment.",
168 });
169 }
170 const token = yield* session.readClerkToken().pipe(
@@ -178,14 +172,14 @@ const capabilitiesLayer = Layer.effectContext(
172 (error) =>
173 new ConnectionTransientError({
174 reason: "network",
181 - message: error.message,
175 + detail: error.message,
176 }),
177 ),
178 );
179 if (token === null) {
180 return yield* new ConnectionBlockedError({
181 reason: "authentication",
188 - message: "The T3 Cloud session is unavailable.",
182 + detail: "The T3 Cloud session is unavailable.",
183 });
184 }
185 return token;
@@ -200,7 +194,7 @@ const capabilitiesLayer = Layer.effectContext(
194 catch: (cause) =>
195 new ConnectionTransientError({
196 reason: "remote-unavailable",
203 - message: `Could not load the desktop primary credential: ${String(cause)}`,
197 + detail: `Could not load the desktop primary credential: ${String(cause)}`,
198 }),
199 }).pipe(Effect.map(Option.fromNullishOr)),
200 });
@@ -210,7 +204,7 @@ const capabilitiesLayer = Layer.effectContext(
204 if (bridge === undefined) {
205 return yield* new ConnectionBlockedError({
206 reason: "unsupported",
213 - message: "SSH environments are only available in the desktop app.",
207 + detail: "SSH environments are only available in the desktop app.",
208 });
209 }
210 return yield* provisionDesktopSshEnvironment(bridge, target);
@@ -220,7 +214,7 @@ const capabilitiesLayer = Layer.effectContext(
214 if (bridge === undefined) {
215 return yield* new ConnectionBlockedError({
216 reason: "unsupported",
223 - message: "SSH environments are only available in the desktop app.",
217 + detail: "SSH environments are only available in the desktop app.",
218 });
219 }
220 const bootstrap = yield* Effect.tryPromise({
@@ -233,7 +227,7 @@ const capabilitiesLayer = Layer.effectContext(
227 if (bootstrap.pairingToken === null) {
228 return yield* new ConnectionBlockedError({
229 reason: "authentication",
236 - message: "The SSH environment did not issue a pairing credential.",
230 + detail: "The SSH environment did not issue a pairing credential.",
231 });
232 }
233 const access = yield* Effect.tryPromise({
@@ -256,7 +250,7 @@ const capabilitiesLayer = Layer.effectContext(
250 catch: (cause) =>
251 new ConnectionTransientError({
252 reason: "remote-unavailable",
259 - message: `Could not disconnect the SSH environment: ${String(cause)}`,
253 + detail: `Could not disconnect the SSH environment: ${String(cause)}`,
254 }),
255 });
256 }),
@@ -278,7 +272,7 @@ const loadPrimaryConnectionRegistration = Effect.fn(
272 if (resolved === null) {
273 return yield* new ConnectionBlockedError({
274 reason: "configuration",
281 - message: "Unable to resolve the primary environment endpoint.",
275 + detail: "Unable to resolve the primary environment endpoint.",
276 });
277 }
278 const descriptor = yield* fetchRemoteEnvironmentDescriptor({
@@ -342,7 +336,20 @@ const rpcRequestObserverLayer = Layer.succeed(
336 }),
337 );
338
345 -export const connectionPlatformLayer = Layer.mergeAll(
339 +type ConnectionPlatformLayerSource =
340 + | typeof connectionStorageLayer
341 + | typeof connectivityLayer
342 + | typeof wakeupsLayer
343 + | typeof capabilitiesLayer
344 + | typeof platformConnectionSourceLayer
345 + | typeof environmentOwnedDataCleanupLayer
346 + | typeof rpcRequestObserverLayer;
347 +
348 +export const connectionPlatformLayer: Layer.Layer<
349 + Layer.Success<ConnectionPlatformLayerSource>,
350 + Layer.Error<ConnectionPlatformLayerSource>,
351 + Layer.Services<ConnectionPlatformLayerSource>
352 +> = Layer.mergeAll(
353 connectionStorageLayer,
354 connectivityLayer,
355 wakeupsLayer,
apps/web/src/connection/runtime.ts
+18 -6
@@ -1,16 +1,28 @@
1 -import { connectionLayer as clientConnectionLayer } from "@t3tools/client-runtime/connection";
1 +import { Connection } from "@t3tools/client-runtime/connection";
2 import * as Layer from "effect/Layer";
3 import { Atom } from "effect/unstable/reactivity";
4
5 import { runtimeContextLayer } from "../lib/runtime";
6 import { connectionPlatformLayer } from "./platform";
7
8 -const providedConnectionPlatformLayer = connectionPlatformLayer.pipe(
9 - Layer.provide(runtimeContextLayer),
10 -);
8 +const providedConnectionPlatformLayer: Layer.Layer<
9 + Layer.Success<typeof connectionPlatformLayer>,
10 + Layer.Error<typeof connectionPlatformLayer>
11 +> = connectionPlatformLayer.pipe(Layer.provide(runtimeContextLayer));
12 +
13 +type ConnectionLayerSource =
14 + | typeof Connection.layer
15 + | typeof runtimeContextLayer
16 + | typeof providedConnectionPlatformLayer;
17
12 -export const connectionLayer = clientConnectionLayer.pipe(
18 +export const connectionLayer: Layer.Layer<
19 + Layer.Success<ConnectionLayerSource>,
20 + Layer.Error<ConnectionLayerSource>
21 +> = Connection.layer.pipe(
22 Layer.provideMerge(Layer.mergeAll(runtimeContextLayer, providedConnectionPlatformLayer)),
23 );
24
16 -export const connectionAtomRuntime = Atom.runtime(connectionLayer);
25 +export const connectionAtomRuntime: Atom.AtomRuntime<
26 + Layer.Success<typeof connectionLayer>,
27 + Layer.Error<typeof connectionLayer>
28 +> = Atom.runtime(connectionLayer);
apps/web/src/connection/storage.test.ts
+1 -1
@@ -43,7 +43,7 @@ describe("makeCatalogStore", () => {
43 Effect.gen(function* () {
44 const failure = new ConnectionTransientError({
45 reason: "remote-unavailable",
46 - message: "permission denied",
46 + detail: "permission denied",
47 });
48 const store = yield* makeCatalogStore({
49 read: Effect.fail(failure),
apps/web/src/connection/storage.ts
+10 -10
@@ -11,11 +11,11 @@ import {
11 removeConnectionFromCatalog,
12 replaceCatalogValue,
13 } from "@t3tools/client-runtime/platform";
14 -import { RemoteDpopAccessTokenStore } from "@t3tools/client-runtime/authorization";
14 +import { TokenStore } from "@t3tools/client-runtime/authorization";
15 import {
16 - ConnectionCredentialStore,
17 - ConnectionProfileStore,
16 ConnectionTransientError,
17 + CredentialStore,
18 + ProfileStore,
19 } from "@t3tools/client-runtime/connection";
20 import {
21 EnvironmentId,
@@ -63,7 +63,7 @@ const encodeStoredThreadSnapshot = Schema.encodeEffect(StoredThreadSnapshotJson)
63 function catalogError(operation: string, cause: unknown) {
64 return new ConnectionTransientError({
65 reason: "remote-unavailable",
66 - message: `Could not ${operation} the local connection catalog: ${String(cause)}`,
66 + detail: `Could not ${operation} the local connection catalog: ${String(cause)}`,
67 });
68 }
69
@@ -343,7 +343,7 @@ export const connectionStorageLayer = Layer.effectContext(
343 .update((document) => removeConnectionFromCatalog(document, target))
344 .pipe(Effect.mapError((cause) => persistenceError("remove-connection", cause))),
345 });
346 - const profileStore = ConnectionProfileStore.of({
346 + const profileStore = ProfileStore.make({
347 get: (connectionId) =>
348 catalog.read.pipe(
349 Effect.map((document) =>
@@ -367,7 +367,7 @@ export const connectionStorageLayer = Layer.effectContext(
367 ),
368 })),
369 });
370 - const credentialStore = ConnectionCredentialStore.of({
370 + const credentialStore = CredentialStore.make({
371 get: (connectionId) =>
372 catalog.read.pipe(
373 Effect.map((document) =>
@@ -394,7 +394,7 @@ export const connectionStorageLayer = Layer.effectContext(
394 ),
395 })),
396 });
397 - const remoteTokenStore = RemoteDpopAccessTokenStore.of({
397 + const remoteTokenStore = TokenStore.make({
398 get: (environmentId) =>
399 catalog.read.pipe(
400 Effect.map((document) =>
@@ -527,9 +527,9 @@ export const connectionStorageLayer = Layer.effectContext(
527
528 return Context.make(ConnectionTargetStore, targetStore).pipe(
529 Context.add(ConnectionRegistrationStore, registrationStore),
530 - Context.add(ConnectionProfileStore, profileStore),
531 - Context.add(ConnectionCredentialStore, credentialStore),
532 - Context.add(RemoteDpopAccessTokenStore, remoteTokenStore),
530 + Context.add(ProfileStore.ConnectionProfileStore, profileStore),
531 + Context.add(CredentialStore.ConnectionCredentialStore, credentialStore),
532 + Context.add(TokenStore.RemoteDpopAccessTokenStore, remoteTokenStore),
533 Context.add(EnvironmentCacheStore, cacheStore),
534 );
535 }),
packages/client-runtime/src/authorization/index.ts
+6 -3
@@ -1,4 +1,7 @@
1 -export * from "./layer.ts";
1 export * from "./remote.ts";
3 -export * from "./service.ts";
4 -export * from "./tokenStore.ts";
2 +export {
3 + type AuthorizedRemoteEnvironment,
4 + type RelayEnvironmentAuthorization,
5 + RemoteEnvironmentAuthorization,
6 +} from "./service.ts";
7 +export * as TokenStore from "./tokenStore.ts";
packages/client-runtime/src/authorization/layer.test.ts
+23 -24
@@ -5,12 +5,11 @@ import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
6 import * as Ref from "effect/Ref";
7
8 -import { ManagedRelayDpopSigner, ManagedRelayDpopSignerError } from "../relay/managedRelay.ts";
8 +import * as ManagedRelay from "../relay/managedRelay.ts";
9 import { remoteHttpClientLayer } from "../rpc/http.ts";
10 -import { ClientPresentation } from "../platform/capabilities.ts";
11 -import { RemoteEnvironmentAuthorization, type RelayEnvironmentAuthorization } from "./service.ts";
12 -import { RemoteDpopAccessToken, RemoteDpopAccessTokenStore } from "./tokenStore.ts";
13 -import { remoteEnvironmentAuthorizationLayer } from "./layer.ts";
10 +import * as ClientCapabilities from "../platform/capabilities.ts";
11 +import * as RemoteEnvironmentAuthorization from "./service.ts";
12 +import * as TokenStore from "./tokenStore.ts";
13
14 const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
15 const ENDPOINT = {
@@ -30,7 +29,7 @@ const DESCRIPTOR = {
29 repositoryIdentity: true,
30 },
31 };
33 -const BOOTSTRAP: RelayEnvironmentAuthorization = {
32 +const BOOTSTRAP: RemoteEnvironmentAuthorization.RelayEnvironmentAuthorization = {
33 environmentId: ENVIRONMENT_ID,
34 endpoint: ENDPOINT,
35 credential: "relay-bootstrap",
@@ -76,7 +75,7 @@ const authInvalid = () =>
75 );
76
77 const makeHarness = Effect.fn("TestRemoteAuthorization.makeHarness")(function* (input: {
79 - readonly initialToken?: RemoteDpopAccessToken;
78 + readonly initialToken?: TokenStore.RemoteDpopAccessToken;
79 readonly responses: ReadonlyArray<Response>;
80 }) {
81 const tokens = yield* Ref.make(
@@ -96,7 +95,7 @@ const makeHarness = Effect.fn("TestRemoteAuthorization.makeHarness")(function* (
95 >([]);
96 const fetch = recordedFetch(input.responses);
97
99 - const tokenStore = RemoteDpopAccessTokenStore.of({
98 + const tokenStore = TokenStore.RemoteDpopAccessTokenStore.of({
99 get: (environmentId) =>
100 Ref.get(tokens).pipe(
101 Effect.map((current) => Option.fromUndefinedOr(current.get(environmentId))),
@@ -114,23 +113,23 @@ const makeHarness = Effect.fn("TestRemoteAuthorization.makeHarness")(function* (
113 return next;
114 }),
115 });
117 - const signer = ManagedRelayDpopSigner.of({
116 + const signer = ManagedRelay.ManagedRelayDpopSigner.of({
117 thumbprint: Effect.succeed("thumbprint-1"),
118 createProof: (proofInput) =>
119 Ref.update(proofInputs, (current) => [...current, proofInput]).pipe(
120 Effect.as(`proof:${proofInput.url}`),
122 - Effect.mapError((cause) => new ManagedRelayDpopSignerError({ cause })),
121 + Effect.mapError((cause) => new ManagedRelay.ManagedRelayDpopSignerError({ cause })),
122 ),
123 });
125 - const layer = remoteEnvironmentAuthorizationLayer.pipe(
124 + const layer = RemoteEnvironmentAuthorization.layer.pipe(
125 Layer.provide(
126 Layer.mergeAll(
127 remoteHttpClientLayer(fetch.fetchFn),
129 - Layer.succeed(ManagedRelayDpopSigner, signer),
130 - Layer.succeed(RemoteDpopAccessTokenStore, tokenStore),
128 + Layer.succeed(ManagedRelay.ManagedRelayDpopSigner, signer),
129 + Layer.succeed(TokenStore.RemoteDpopAccessTokenStore, tokenStore),
130 Layer.succeed(
132 - ClientPresentation,
133 - ClientPresentation.of({
131 + ClientCapabilities.ClientPresentation,
132 + ClientCapabilities.ClientPresentation.of({
133 metadata: {
134 label: "T3 Code Test",
135 deviceType: "mobile",
@@ -159,7 +158,7 @@ const makeHarness = Effect.fn("TestRemoteAuthorization.makeHarness")(function* (
158 describe("RemoteEnvironmentAuthorization", () => {
159 it.effect("reuses a valid persisted environment token without contacting the relay", () =>
160 Effect.gen(function* () {
162 - const cached = new RemoteDpopAccessToken({
161 + const cached = new TokenStore.RemoteDpopAccessToken({
162 environmentId: ENVIRONMENT_ID,
163 label: DESCRIPTOR.label,
164 endpoint: ENDPOINT,
@@ -173,7 +172,7 @@ describe("RemoteEnvironmentAuthorization", () => {
172 });
173
174 const authorized = yield* Effect.gen(function* () {
176 - const remote = yield* RemoteEnvironmentAuthorization;
175 + const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
176 return yield* remote.authorizeDpop({
177 expectedEnvironmentId: ENVIRONMENT_ID,
178 obtainBootstrap: harness.obtainBootstrap,
@@ -191,7 +190,7 @@ describe("RemoteEnvironmentAuthorization", () => {
190
191 it.effect("refreshes and persists an expired environment token", () =>
192 Effect.gen(function* () {
194 - const expired = new RemoteDpopAccessToken({
193 + const expired = new TokenStore.RemoteDpopAccessToken({
194 environmentId: ENVIRONMENT_ID,
195 label: DESCRIPTOR.label,
196 endpoint: ENDPOINT,
@@ -209,7 +208,7 @@ describe("RemoteEnvironmentAuthorization", () => {
208 });
209
210 const authorized = yield* Effect.gen(function* () {
212 - const remote = yield* RemoteEnvironmentAuthorization;
211 + const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
212 return yield* remote.authorizeDpop({
213 expectedEnvironmentId: ENVIRONMENT_ID,
214 obtainBootstrap: harness.obtainBootstrap,
@@ -230,7 +229,7 @@ describe("RemoteEnvironmentAuthorization", () => {
229
230 it.effect("evicts an auth-invalid cached token and obtains a fresh bootstrap", () =>
231 Effect.gen(function* () {
233 - const cached = new RemoteDpopAccessToken({
232 + const cached = new TokenStore.RemoteDpopAccessToken({
233 environmentId: ENVIRONMENT_ID,
234 label: DESCRIPTOR.label,
235 endpoint: ENDPOINT,
@@ -249,7 +248,7 @@ describe("RemoteEnvironmentAuthorization", () => {
248 });
249
250 const authorized = yield* Effect.gen(function* () {
252 - const remote = yield* RemoteEnvironmentAuthorization;
251 + const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
252 return yield* remote.authorizeDpop({
253 expectedEnvironmentId: ENVIRONMENT_ID,
254 obtainBootstrap: harness.obtainBootstrap,
@@ -269,7 +268,7 @@ describe("RemoteEnvironmentAuthorization", () => {
268
269 it.effect("refreshes a cached endpoint after consecutive transient failures", () =>
270 Effect.gen(function* () {
272 - const cached = new RemoteDpopAccessToken({
271 + const cached = new TokenStore.RemoteDpopAccessToken({
272 environmentId: ENVIRONMENT_ID,
273 label: DESCRIPTOR.label,
274 endpoint: ENDPOINT,
@@ -289,7 +288,7 @@ describe("RemoteEnvironmentAuthorization", () => {
288 });
289
290 const authorized = yield* Effect.gen(function* () {
292 - const remote = yield* RemoteEnvironmentAuthorization;
291 + const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
292 const firstFailure = yield* remote
293 .authorizeDpop({
294 expectedEnvironmentId: ENVIRONMENT_ID,
@@ -329,7 +328,7 @@ describe("RemoteEnvironmentAuthorization", () => {
328 });
329
330 yield* Effect.gen(function* () {
332 - const remote = yield* RemoteEnvironmentAuthorization;
331 + const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
332 return yield* remote.authorizeDpop({
333 expectedEnvironmentId: ENVIRONMENT_ID,
334 obtainBootstrap: harness.obtainBootstrap,
packages/client-runtime/src/authorization/layer.ts deleted
-268
@@ -1,268 +0,0 @@
1 -import {
2 - exchangeRemoteDpopAccessToken,
3 - type RemoteEnvironmentAuthError,
4 - resolveRemoteDpopWebSocketConnectionUrl,
5 - resolveRemoteWebSocketConnectionUrl,
6 -} from "./remote.ts";
7 -import { environmentMismatchError, mapRemoteEnvironmentError } from "../connection/errors.ts";
8 -import { ConnectionBlockedError, type ConnectionAttemptError } from "../connection/model.ts";
9 -import { fetchRemoteEnvironmentDescriptor } from "../environment/descriptor.ts";
10 -import { environmentEndpointUrl } from "../environment/endpoint.ts";
11 -import { ClientPresentation } from "../platform/capabilities.ts";
12 -import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
13 -import { RemoteEnvironmentAuthorization } from "./service.ts";
14 -import { RemoteDpopAccessToken, RemoteDpopAccessTokenStore } from "./tokenStore.ts";
15 -import * as Clock from "effect/Clock";
16 -import * as Effect from "effect/Effect";
17 -import * as Layer from "effect/Layer";
18 -import * as Option from "effect/Option";
19 -import * as Ref from "effect/Ref";
20 -import * as Result from "effect/Result";
21 -import { HttpClient } from "effect/unstable/http";
22 -
23 -const TOKEN_EXPIRY_SAFETY_MARGIN_MS = 60_000;
24 -const CACHED_ENDPOINT_FAILURE_THRESHOLD = 2;
25 -
26 -function mapDpopSocketError(error: RemoteEnvironmentAuthError | ConnectionAttemptError) {
27 - return error._tag === "ConnectionTransientError" || error._tag === "ConnectionBlockedError"
28 - ? error
29 - : mapRemoteEnvironmentError(error);
30 -}
31 -
32 -const fetchDescriptor = Effect.fn("clientRuntime.connection.remote.fetchDescriptor")(function* (
33 - httpBaseUrl: string,
34 -) {
35 - return yield* fetchRemoteEnvironmentDescriptor({ httpBaseUrl }).pipe(
36 - Effect.mapError(mapRemoteEnvironmentError),
37 - );
38 -});
39 -
40 -export const remoteEnvironmentAuthorizationLayer = Layer.effect(
41 - RemoteEnvironmentAuthorization,
42 - Effect.gen(function* () {
43 - const signer = yield* ManagedRelayDpopSigner;
44 - const presentation = yield* ClientPresentation;
45 - const tokenStore = yield* RemoteDpopAccessTokenStore;
46 - const httpClient = yield* HttpClient.HttpClient;
47 - const cachedEndpointFailures = yield* Ref.make<ReadonlyMap<string, number>>(new Map());
48 -
49 - const resetCachedEndpointFailures = (environmentId: string) =>
50 - Ref.update(cachedEndpointFailures, (current) => {
51 - if (!current.has(environmentId)) {
52 - return current;
53 - }
54 - const next = new Map(current);
55 - next.delete(environmentId);
56 - return next;
57 - });
58 -
59 - const recordCachedEndpointFailure = (environmentId: string) =>
60 - Ref.modify(cachedEndpointFailures, (current) => {
61 - const failureCount = (current.get(environmentId) ?? 0) + 1;
62 - const next = new Map(current);
63 - next.set(environmentId, failureCount);
64 - return [failureCount, next] as const;
65 - });
66 -
67 - const authorizeBearer = Effect.fn("clientRuntime.connection.remote.authorizeBearer")(
68 - function* (input: {
69 - readonly expectedEnvironmentId: Parameters<
70 - RemoteEnvironmentAuthorization["Service"]["authorizeBearer"]
71 - >[0]["expectedEnvironmentId"];
72 - readonly httpBaseUrl: string;
73 - readonly wsBaseUrl: string;
74 - readonly bearerToken: string;
75 - }) {
76 - const descriptor = yield* fetchDescriptor(input.httpBaseUrl).pipe(
77 - Effect.provideService(HttpClient.HttpClient, httpClient),
78 - );
79 - if (descriptor.environmentId !== input.expectedEnvironmentId) {
80 - return yield* environmentMismatchError({
81 - expected: input.expectedEnvironmentId,
82 - actual: descriptor.environmentId,
83 - });
84 - }
85 - const socketUrl = yield* resolveRemoteWebSocketConnectionUrl({
86 - wsBaseUrl: input.wsBaseUrl,
87 - httpBaseUrl: input.httpBaseUrl,
88 - bearerToken: input.bearerToken,
89 - }).pipe(
90 - Effect.mapError(mapRemoteEnvironmentError),
91 - Effect.provideService(HttpClient.HttpClient, httpClient),
92 - );
93 - return {
94 - environmentId: descriptor.environmentId,
95 - label: descriptor.label,
96 - httpBaseUrl: input.httpBaseUrl,
97 - socketUrl,
98 - httpAuthorization: {
99 - _tag: "Bearer" as const,
100 - token: input.bearerToken,
101 - },
102 - };
103 - },
104 - );
105 -
106 - const createDpopSocketUrl = Effect.fn("clientRuntime.connection.remote.createDpopSocketUrl")(
107 - function* (token: RemoteDpopAccessToken) {
108 - const ticketProof = yield* signer
109 - .createProof({
110 - method: "POST",
111 - url: environmentEndpointUrl(token.endpoint.httpBaseUrl, "/api/auth/websocket-ticket"),
112 - accessToken: token.accessToken,
113 - })
114 - .pipe(
115 - Effect.mapError(
116 - () =>
117 - new ConnectionBlockedError({
118 - reason: "configuration",
119 - message: "Could not create the websocket authorization proof.",
120 - }),
121 - ),
122 - );
123 - return yield* resolveRemoteDpopWebSocketConnectionUrl({
124 - wsBaseUrl: token.endpoint.wsBaseUrl,
125 - httpBaseUrl: token.endpoint.httpBaseUrl,
126 - accessToken: token.accessToken,
127 - dpopProof: ticketProof,
128 - }).pipe(Effect.provideService(HttpClient.HttpClient, httpClient));
129 - },
130 - );
131 -
132 - const authorizeDpop = Effect.fn("clientRuntime.connection.remote.authorizeDpop")(
133 - function* (input: {
134 - readonly expectedEnvironmentId: Parameters<
135 - RemoteEnvironmentAuthorization["Service"]["authorizeDpop"]
136 - >[0]["expectedEnvironmentId"];
137 - readonly obtainBootstrap: Parameters<
138 - RemoteEnvironmentAuthorization["Service"]["authorizeDpop"]
139 - >[0]["obtainBootstrap"];
140 - }) {
141 - const thumbprint = yield* signer.thumbprint.pipe(
142 - Effect.mapError(
143 - () =>
144 - new ConnectionBlockedError({
145 - reason: "configuration",
146 - message: "Could not load the environment authorization key.",
147 - }),
148 - ),
149 - Effect.withSpan("environment.authorization.dpopKey.resolve"),
150 - );
151 - const now = yield* Clock.currentTimeMillis;
152 - const cached = yield* tokenStore
153 - .get(input.expectedEnvironmentId)
154 - .pipe(Effect.withSpan("environment.authorization.accessToken.cache"));
155 - if (
156 - Option.isSome(cached) &&
157 - cached.value.environmentId === input.expectedEnvironmentId &&
158 - cached.value.dpopThumbprint === thumbprint &&
159 - cached.value.expiresAtEpochMs > now + TOKEN_EXPIRY_SAFETY_MARGIN_MS
160 - ) {
161 - yield* Effect.annotateCurrentSpan({
162 - "connection.remote_token_cache": "hit",
163 - });
164 - const cachedSocket = yield* createDpopSocketUrl(cached.value).pipe(Effect.result);
165 - if (Result.isSuccess(cachedSocket)) {
166 - yield* resetCachedEndpointFailures(input.expectedEnvironmentId);
167 - return {
168 - environmentId: cached.value.environmentId,
169 - label: cached.value.label,
170 - httpBaseUrl: cached.value.endpoint.httpBaseUrl,
171 - socketUrl: cachedSocket.success,
172 - httpAuthorization: {
173 - _tag: "Dpop" as const,
174 - accessToken: cached.value.accessToken,
175 - },
176 - };
177 - }
178 - if (cachedSocket.failure._tag === "ConnectionBlockedError") {
179 - return yield* mapDpopSocketError(cachedSocket.failure);
180 - }
181 - const mappedFailure = mapDpopSocketError(cachedSocket.failure);
182 - if (mappedFailure._tag === "ConnectionTransientError") {
183 - const failureCount = yield* recordCachedEndpointFailure(input.expectedEnvironmentId);
184 - if (failureCount < CACHED_ENDPOINT_FAILURE_THRESHOLD) {
185 - return yield* mappedFailure;
186 - }
187 - }
188 - yield* tokenStore
189 - .remove(input.expectedEnvironmentId)
190 - .pipe(Effect.withSpan("environment.authorization.accessToken.remove"));
191 - yield* resetCachedEndpointFailures(input.expectedEnvironmentId);
192 - }
193 -
194 - yield* resetCachedEndpointFailures(input.expectedEnvironmentId);
195 - yield* Effect.annotateCurrentSpan({
196 - "connection.remote_token_cache": "miss",
197 - });
198 - const bootstrap = yield* input.obtainBootstrap;
199 - const descriptor = yield* fetchDescriptor(bootstrap.endpoint.httpBaseUrl).pipe(
200 - Effect.provideService(HttpClient.HttpClient, httpClient),
201 - Effect.withSpan("environment.authorization.descriptor"),
202 - );
203 - if (descriptor.environmentId !== input.expectedEnvironmentId) {
204 - return yield* environmentMismatchError({
205 - expected: input.expectedEnvironmentId,
206 - actual: descriptor.environmentId,
207 - });
208 - }
209 - const bootstrapProof = yield* signer
210 - .createProof({
211 - method: "POST",
212 - url: environmentEndpointUrl(bootstrap.endpoint.httpBaseUrl, "/oauth/token"),
213 - })
214 - .pipe(
215 - Effect.mapError(
216 - () =>
217 - new ConnectionBlockedError({
218 - reason: "configuration",
219 - message: "Could not create the environment authorization proof.",
220 - }),
221 - ),
222 - );
223 - const access = yield* exchangeRemoteDpopAccessToken({
224 - httpBaseUrl: bootstrap.endpoint.httpBaseUrl,
225 - credential: bootstrap.credential,
226 - dpopProof: bootstrapProof,
227 - scopes: presentation.scopes,
228 - clientMetadata: presentation.metadata,
229 - }).pipe(
230 - Effect.mapError(mapRemoteEnvironmentError),
231 - Effect.provideService(HttpClient.HttpClient, httpClient),
232 - Effect.withSpan("environment.authorization.accessToken.exchange"),
233 - );
234 - const issuedAt = yield* Clock.currentTimeMillis;
235 - const token = new RemoteDpopAccessToken({
236 - environmentId: descriptor.environmentId,
237 - label: descriptor.label,
238 - endpoint: bootstrap.endpoint,
239 - accessToken: access.access_token,
240 - expiresAtEpochMs: issuedAt + access.expires_in * 1_000,
241 - dpopThumbprint: thumbprint,
242 - });
243 - const socketUrl = yield* createDpopSocketUrl(token).pipe(
244 - Effect.mapError(mapDpopSocketError),
245 - );
246 - yield* tokenStore
247 - .put(token)
248 - .pipe(Effect.withSpan("environment.authorization.accessToken.persist"));
249 - return {
250 - environmentId: descriptor.environmentId,
251 - label: descriptor.label,
252 - httpBaseUrl: bootstrap.endpoint.httpBaseUrl,
253 - socketUrl,
254 - httpAuthorization: {
255 - _tag: "Dpop" as const,
256 - accessToken: token.accessToken,
257 - },
258 - };
259 - },
260 - );
261 -
262 - return RemoteEnvironmentAuthorization.of({
263 - authorizeBearer,
264 - authorizeDpop: (input) =>
265 - authorizeDpop(input).pipe(Effect.withSpan("environment.authorization")),
266 - });
267 - }),
268 -);
packages/client-runtime/src/authorization/service.ts
+265 -2
@@ -1,9 +1,28 @@
1 import { EnvironmentId } from "@t3tools/contracts";
2 import type { RelayManagedEndpoint } from "@t3tools/contracts/relay";
3 +import {
4 + exchangeRemoteDpopAccessToken,
5 + type RemoteEnvironmentAuthError,
6 + resolveRemoteDpopWebSocketConnectionUrl,
7 + resolveRemoteWebSocketConnectionUrl,
8 +} from "./remote.ts";
9 +import { environmentMismatchError, mapRemoteEnvironmentError } from "../connection/errors.ts";
10 +import { ConnectionBlockedError, type ConnectionAttemptError } from "../connection/model.ts";
11 +import { fetchRemoteEnvironmentDescriptor } from "../environment/descriptor.ts";
12 +import { environmentEndpointUrl } from "../environment/endpoint.ts";
13 +import * as ClientCapabilities from "../platform/capabilities.ts";
14 +import * as ManagedRelay from "../relay/managedRelay.ts";
15 +import * as TokenStore from "./tokenStore.ts";
16 +import * as Clock from "effect/Clock";
17 import * as Context from "effect/Context";
4 -import type * as Effect from "effect/Effect";
18 +import * as Effect from "effect/Effect";
19 +import * as Layer from "effect/Layer";
20 +import * as Option from "effect/Option";
21 +import * as Ref from "effect/Ref";
22 +import * as Result from "effect/Result";
23 +import * as HttpClient from "effect/unstable/http/HttpClient";
24
6 -import type { ConnectionAttemptError, PreparedHttpAuthorization } from "../connection/model.ts";
25 +import type { PreparedHttpAuthorization } from "../connection/model.ts";
26
27 export interface RelayEnvironmentAuthorization {
28 readonly environmentId: EnvironmentId;
@@ -37,3 +56,247 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
56 }) => Effect.Effect<AuthorizedRemoteEnvironment, ConnectionAttemptError>;
57 }
58 >()("@t3tools/client-runtime/authorization/service/RemoteEnvironmentAuthorization") {}
59 +
60 +const TOKEN_EXPIRY_SAFETY_MARGIN_MS = 60_000;
61 +const CACHED_ENDPOINT_FAILURE_THRESHOLD = 2;
62 +
63 +function mapDpopSocketError(error: RemoteEnvironmentAuthError | ConnectionAttemptError) {
64 + return error._tag === "ConnectionTransientError" || error._tag === "ConnectionBlockedError"
65 + ? error
66 + : mapRemoteEnvironmentError(error);
67 +}
68 +
69 +const fetchDescriptor = Effect.fn("clientRuntime.connection.remote.fetchDescriptor")(function* (
70 + httpBaseUrl: string,
71 +) {
72 + return yield* fetchRemoteEnvironmentDescriptor({ httpBaseUrl }).pipe(
73 + Effect.mapError(mapRemoteEnvironmentError),
74 + );
75 +});
76 +
77 +export const make = Effect.gen(function* () {
78 + const signer = yield* ManagedRelay.ManagedRelayDpopSigner;
79 + const presentation = yield* ClientCapabilities.ClientPresentation;
80 + const tokenStore = yield* TokenStore.RemoteDpopAccessTokenStore;
81 + const httpClient = yield* HttpClient.HttpClient;
82 + const cachedEndpointFailures = yield* Ref.make<ReadonlyMap<string, number>>(new Map());
83 +
84 + const resetCachedEndpointFailures = (environmentId: string) =>
85 + Ref.update(cachedEndpointFailures, (current) => {
86 + if (!current.has(environmentId)) {
87 + return current;
88 + }
89 + const next = new Map(current);
90 + next.delete(environmentId);
91 + return next;
92 + });
93 +
94 + const recordCachedEndpointFailure = (environmentId: string) =>
95 + Ref.modify(cachedEndpointFailures, (current) => {
96 + const failureCount = (current.get(environmentId) ?? 0) + 1;
97 + const next = new Map(current);
98 + next.set(environmentId, failureCount);
99 + return [failureCount, next] as const;
100 + });
101 +
102 + const authorizeBearer = Effect.fn("clientRuntime.connection.remote.authorizeBearer")(
103 + function* (input: {
104 + readonly expectedEnvironmentId: Parameters<
105 + RemoteEnvironmentAuthorization["Service"]["authorizeBearer"]
106 + >[0]["expectedEnvironmentId"];
107 + readonly httpBaseUrl: string;
108 + readonly wsBaseUrl: string;
109 + readonly bearerToken: string;
110 + }) {
111 + const descriptor = yield* fetchDescriptor(input.httpBaseUrl).pipe(
112 + Effect.provideService(HttpClient.HttpClient, httpClient),
113 + );
114 + if (descriptor.environmentId !== input.expectedEnvironmentId) {
115 + return yield* environmentMismatchError({
116 + expected: input.expectedEnvironmentId,
117 + actual: descriptor.environmentId,
118 + });
119 + }
120 + const socketUrl = yield* resolveRemoteWebSocketConnectionUrl({
121 + wsBaseUrl: input.wsBaseUrl,
122 + httpBaseUrl: input.httpBaseUrl,
123 + bearerToken: input.bearerToken,
124 + }).pipe(
125 + Effect.mapError(mapRemoteEnvironmentError),
126 + Effect.provideService(HttpClient.HttpClient, httpClient),
127 + );
128 + return {
129 + environmentId: descriptor.environmentId,
130 + label: descriptor.label,
131 + httpBaseUrl: input.httpBaseUrl,
132 + socketUrl,
133 + httpAuthorization: {
134 + _tag: "Bearer" as const,
135 + token: input.bearerToken,
136 + },
137 + };
138 + },
139 + );
140 +
141 + const createDpopSocketUrl = Effect.fn("clientRuntime.connection.remote.createDpopSocketUrl")(
142 + function* (token: TokenStore.RemoteDpopAccessToken) {
143 + const ticketProof = yield* signer
144 + .createProof({
145 + method: "POST",
146 + url: environmentEndpointUrl(token.endpoint.httpBaseUrl, "/api/auth/websocket-ticket"),
147 + accessToken: token.accessToken,
148 + })
149 + .pipe(
150 + Effect.mapError(
151 + () =>
152 + new ConnectionBlockedError({
153 + reason: "configuration",
154 + detail: "Could not create the websocket authorization proof.",
155 + }),
156 + ),
157 + );
158 + return yield* resolveRemoteDpopWebSocketConnectionUrl({
159 + wsBaseUrl: token.endpoint.wsBaseUrl,
160 + httpBaseUrl: token.endpoint.httpBaseUrl,
161 + accessToken: token.accessToken,
162 + dpopProof: ticketProof,
163 + }).pipe(Effect.provideService(HttpClient.HttpClient, httpClient));
164 + },
165 + );
166 +
167 + const authorizeDpop = Effect.fn("clientRuntime.connection.remote.authorizeDpop")(
168 + function* (input: {
169 + readonly expectedEnvironmentId: Parameters<
170 + RemoteEnvironmentAuthorization["Service"]["authorizeDpop"]
171 + >[0]["expectedEnvironmentId"];
172 + readonly obtainBootstrap: Parameters<
173 + RemoteEnvironmentAuthorization["Service"]["authorizeDpop"]
174 + >[0]["obtainBootstrap"];
175 + }) {
176 + const thumbprint = yield* signer.thumbprint.pipe(
177 + Effect.mapError(
178 + () =>
179 + new ConnectionBlockedError({
180 + reason: "configuration",
181 + detail: "Could not load the environment authorization key.",
182 + }),
183 + ),
184 + Effect.withSpan("environment.authorization.dpopKey.resolve"),
185 + );
186 + const now = yield* Clock.currentTimeMillis;
187 + const cached = yield* tokenStore
188 + .get(input.expectedEnvironmentId)
189 + .pipe(Effect.withSpan("environment.authorization.accessToken.cache"));
190 + if (
191 + Option.isSome(cached) &&
192 + cached.value.environmentId === input.expectedEnvironmentId &&
193 + cached.value.dpopThumbprint === thumbprint &&
194 + cached.value.expiresAtEpochMs > now + TOKEN_EXPIRY_SAFETY_MARGIN_MS
195 + ) {
196 + yield* Effect.annotateCurrentSpan({
197 + "connection.remote_token_cache": "hit",
198 + });
199 + const cachedSocket = yield* createDpopSocketUrl(cached.value).pipe(Effect.result);
200 + if (Result.isSuccess(cachedSocket)) {
201 + yield* resetCachedEndpointFailures(input.expectedEnvironmentId);
202 + return {
203 + environmentId: cached.value.environmentId,
204 + label: cached.value.label,
205 + httpBaseUrl: cached.value.endpoint.httpBaseUrl,
206 + socketUrl: cachedSocket.success,
207 + httpAuthorization: {
208 + _tag: "Dpop" as const,
209 + accessToken: cached.value.accessToken,
210 + },
211 + };
212 + }
213 + if (cachedSocket.failure._tag === "ConnectionBlockedError") {
214 + return yield* mapDpopSocketError(cachedSocket.failure);
215 + }
216 + const mappedFailure = mapDpopSocketError(cachedSocket.failure);
217 + if (mappedFailure._tag === "ConnectionTransientError") {
218 + const failureCount = yield* recordCachedEndpointFailure(input.expectedEnvironmentId);
219 + if (failureCount < CACHED_ENDPOINT_FAILURE_THRESHOLD) {
220 + return yield* mappedFailure;
221 + }
222 + }
223 + yield* tokenStore
224 + .remove(input.expectedEnvironmentId)
225 + .pipe(Effect.withSpan("environment.authorization.accessToken.remove"));
226 + yield* resetCachedEndpointFailures(input.expectedEnvironmentId);
227 + }
228 +
229 + yield* resetCachedEndpointFailures(input.expectedEnvironmentId);
230 + yield* Effect.annotateCurrentSpan({
231 + "connection.remote_token_cache": "miss",
232 + });
233 + const bootstrap = yield* input.obtainBootstrap;
234 + const descriptor = yield* fetchDescriptor(bootstrap.endpoint.httpBaseUrl).pipe(
235 + Effect.provideService(HttpClient.HttpClient, httpClient),
236 + Effect.withSpan("environment.authorization.descriptor"),
237 + );
238 + if (descriptor.environmentId !== input.expectedEnvironmentId) {
239 + return yield* environmentMismatchError({
240 + expected: input.expectedEnvironmentId,
241 + actual: descriptor.environmentId,
242 + });
243 + }
244 + const bootstrapProof = yield* signer
245 + .createProof({
246 + method: "POST",
247 + url: environmentEndpointUrl(bootstrap.endpoint.httpBaseUrl, "/oauth/token"),
248 + })
249 + .pipe(
250 + Effect.mapError(
251 + () =>
252 + new ConnectionBlockedError({
253 + reason: "configuration",
254 + detail: "Could not create the environment authorization proof.",
255 + }),
256 + ),
257 + );
258 + const access = yield* exchangeRemoteDpopAccessToken({
259 + httpBaseUrl: bootstrap.endpoint.httpBaseUrl,
260 + credential: bootstrap.credential,
261 + dpopProof: bootstrapProof,
262 + scopes: presentation.scopes,
263 + clientMetadata: presentation.metadata,
264 + }).pipe(
265 + Effect.mapError(mapRemoteEnvironmentError),
266 + Effect.provideService(HttpClient.HttpClient, httpClient),
267 + Effect.withSpan("environment.authorization.accessToken.exchange"),
268 + );
269 + const issuedAt = yield* Clock.currentTimeMillis;
270 + const token = new TokenStore.RemoteDpopAccessToken({
271 + environmentId: descriptor.environmentId,
272 + label: descriptor.label,
273 + endpoint: bootstrap.endpoint,
274 + accessToken: access.access_token,
275 + expiresAtEpochMs: issuedAt + access.expires_in * 1_000,
276 + dpopThumbprint: thumbprint,
277 + });
278 + const socketUrl = yield* createDpopSocketUrl(token).pipe(Effect.mapError(mapDpopSocketError));
279 + yield* tokenStore
280 + .put(token)
281 + .pipe(Effect.withSpan("environment.authorization.accessToken.persist"));
282 + return {
283 + environmentId: descriptor.environmentId,
284 + label: descriptor.label,
285 + httpBaseUrl: bootstrap.endpoint.httpBaseUrl,
286 + socketUrl,
287 + httpAuthorization: {
288 + _tag: "Dpop" as const,
289 + accessToken: token.accessToken,
290 + },
291 + };
292 + },
293 + );
294 +
295 + return RemoteEnvironmentAuthorization.of({
296 + authorizeBearer,
297 + authorizeDpop: (input) =>
298 + authorizeDpop(input).pipe(Effect.withSpan("environment.authorization")),
299 + });
300 +});
301 +
302 +export const layer = Layer.effect(RemoteEnvironmentAuthorization, make);
packages/client-runtime/src/authorization/tokenStore.ts
+7
@@ -2,6 +2,7 @@ import { EnvironmentId } from "@t3tools/contracts";
2 import { RelayManagedEndpoint } from "@t3tools/contracts/relay";
3 import * as Context from "effect/Context";
4 import type * as Effect from "effect/Effect";
5 +import * as Layer from "effect/Layer";
6 import type * as Option from "effect/Option";
7 import * as Schema from "effect/Schema";
8
@@ -28,3 +29,9 @@ export class RemoteDpopAccessTokenStore extends Context.Service<
29 readonly remove: (environmentId: EnvironmentId) => Effect.Effect<void, ConnectionAttemptError>;
30 }
31 >()("@t3tools/client-runtime/authorization/tokenStore/RemoteDpopAccessTokenStore") {}
32 +
33 +export const make = (service: RemoteDpopAccessTokenStore["Service"]) =>
34 + RemoteDpopAccessTokenStore.of(service);
35 +
36 +export const layer = (service: RemoteDpopAccessTokenStore["Service"]) =>
37 + Layer.succeed(RemoteDpopAccessTokenStore, make(service));
packages/client-runtime/src/connection/catalog.ts
-28
@@ -1,10 +1,7 @@
1 import { DesktopSshEnvironmentTargetSchema, EnvironmentId } from "@t3tools/contracts";
2 -import * as Context from "effect/Context";
3 -import type * as Effect from "effect/Effect";
2 import * as Option from "effect/Option";
3 import * as Schema from "effect/Schema";
4
7 -import type { ConnectionAttemptError } from "./model.ts";
5 import {
6 BearerConnectionTarget,
7 PrimaryConnectionTarget,
@@ -116,28 +113,3 @@ export function connectionRegistrationCatalogEntry(
113 };
114 }
115 }
119 -
120 -export class ConnectionProfileStore extends Context.Service<
121 - ConnectionProfileStore,
122 - {
123 - readonly get: (
124 - connectionId: string,
125 - ) => Effect.Effect<Option.Option<ConnectionProfile>, ConnectionAttemptError>;
126 - readonly put: (profile: ConnectionProfile) => Effect.Effect<void, ConnectionAttemptError>;
127 - readonly remove: (connectionId: string) => Effect.Effect<void, ConnectionAttemptError>;
128 - }
129 ->()("@t3tools/client-runtime/connection/catalog/ConnectionProfileStore") {}
130 -
131 -export class ConnectionCredentialStore extends Context.Service<
132 - ConnectionCredentialStore,
133 - {
134 - readonly get: (
135 - connectionId: string,
136 - ) => Effect.Effect<Option.Option<ConnectionCredential>, ConnectionAttemptError>;
137 - readonly put: (
138 - connectionId: string,
139 - credential: ConnectionCredential,
140 - ) => Effect.Effect<void, ConnectionAttemptError>;
141 - readonly remove: (connectionId: string) => Effect.Effect<void, ConnectionAttemptError>;
142 - }
143 ->()("@t3tools/client-runtime/connection/catalog/ConnectionCredentialStore") {}
packages/client-runtime/src/connection/connectivity.ts
+6
@@ -1,5 +1,6 @@
1 import * as Context from "effect/Context";
2 import type * as Effect from "effect/Effect";
3 +import * as Layer from "effect/Layer";
4 import type * as Stream from "effect/Stream";
5
6 import type { NetworkStatus } from "./model.ts";
@@ -11,3 +12,8 @@ export class Connectivity extends Context.Service<
12 readonly changes: Stream.Stream<NetworkStatus>;
13 }
14 >()("@t3tools/client-runtime/connection/connectivity") {}
15 +
16 +export const make = (service: Connectivity["Service"]) => Connectivity.of(service);
17 +
18 +export const layer = (service: Connectivity["Service"]) =>
19 + Layer.succeed(Connectivity, make(service));
packages/client-runtime/src/connection/credentialStore.ts new
+27
@@ -0,0 +1,27 @@
1 +import * as Context from "effect/Context";
2 +import type * as Effect from "effect/Effect";
3 +import * as Layer from "effect/Layer";
4 +import type * as Option from "effect/Option";
5 +
6 +import type { ConnectionCredential } from "./catalog.ts";
7 +import type { ConnectionAttemptError } from "./model.ts";
8 +
9 +export class ConnectionCredentialStore extends Context.Service<
10 + ConnectionCredentialStore,
11 + {
12 + readonly get: (
13 + connectionId: string,
14 + ) => Effect.Effect<Option.Option<ConnectionCredential>, ConnectionAttemptError>;
15 + readonly put: (
16 + connectionId: string,
17 + credential: ConnectionCredential,
18 + ) => Effect.Effect<void, ConnectionAttemptError>;
19 + readonly remove: (connectionId: string) => Effect.Effect<void, ConnectionAttemptError>;
20 + }
21 +>()("@t3tools/client-runtime/connection/credentialStore/ConnectionCredentialStore") {}
22 +
23 +export const make = (service: ConnectionCredentialStore["Service"]) =>
24 + ConnectionCredentialStore.of(service);
25 +
26 +export const layer = (service: ConnectionCredentialStore["Service"]) =>
27 + Layer.succeed(ConnectionCredentialStore, make(service));
packages/client-runtime/src/connection/driver.ts
+34 -36
@@ -9,8 +9,8 @@ import type {
9 ConnectionAttemptStage,
10 PreparedConnection,
11 } from "./model.ts";
12 -import { ConnectionResolver } from "./resolver.ts";
13 -import { RpcSessionFactory, type RpcSession } from "../rpc/session.ts";
12 +import * as ConnectionResolver from "./resolver.ts";
13 +import * as RpcSession from "../rpc/session.ts";
14
15 export type ConnectionDriverProgress =
16 | {
@@ -23,44 +23,42 @@ export type ConnectionDriverProgress =
23
24 export interface EnvironmentConnectionLease {
25 readonly prepared: PreparedConnection;
26 - readonly session: RpcSession;
26 + readonly session: RpcSession.RpcSession;
27 }
28
29 -export interface ConnectionDriverService {
30 - readonly connect: (
31 - entry: ConnectionCatalogEntry,
32 - reportProgress: (progress: ConnectionDriverProgress) => Effect.Effect<void>,
33 - ) => Effect.Effect<EnvironmentConnectionLease, ConnectionAttemptError, Scope.Scope>;
34 -}
35 -
36 -export class ConnectionDriver extends Context.Service<ConnectionDriver, ConnectionDriverService>()(
37 - "@t3tools/client-runtime/connection/driver/ConnectionDriver",
38 -) {}
39 -
40 -export const connectionDriverLayer = Layer.effect(
29 +export class ConnectionDriver extends Context.Service<
30 ConnectionDriver,
42 - Effect.gen(function* () {
43 - const resolver = yield* ConnectionResolver;
44 - const sessions = yield* RpcSessionFactory;
45 -
46 - const connect = Effect.fn("ConnectionDriver.connect")(function* (
31 + {
32 + readonly connect: (
33 entry: ConnectionCatalogEntry,
34 reportProgress: (progress: ConnectionDriverProgress) => Effect.Effect<void>,
49 - ) {
50 - const target = entry.target;
51 - yield* Effect.annotateCurrentSpan({
52 - "connection.environment.id": target.environmentId,
53 - "connection.target.kind": target._tag,
54 - });
55 - yield* reportProgress({ stage: "preparing" });
56 - const prepared = yield* resolver.prepare(entry);
57 - yield* reportProgress({ stage: "opening", prepared });
58 - const session = yield* sessions.connect(prepared);
59 - yield* reportProgress({ stage: "synchronizing", prepared });
60 - yield* session.ready;
61 - return { prepared, session } satisfies EnvironmentConnectionLease;
35 + ) => Effect.Effect<EnvironmentConnectionLease, ConnectionAttemptError, Scope.Scope>;
36 + }
37 +>()("@t3tools/client-runtime/connection/driver/ConnectionDriver") {}
38 +
39 +export const make = Effect.gen(function* () {
40 + const resolver = yield* ConnectionResolver.ConnectionResolver;
41 + const sessions = yield* RpcSession.RpcSessionFactory;
42 +
43 + const connect = Effect.fn("ConnectionDriver.connect")(function* (
44 + entry: ConnectionCatalogEntry,
45 + reportProgress: (progress: ConnectionDriverProgress) => Effect.Effect<void>,
46 + ) {
47 + const target = entry.target;
48 + yield* Effect.annotateCurrentSpan({
49 + "connection.environment.id": target.environmentId,
50 + "connection.target.kind": target._tag,
51 });
52 + yield* reportProgress({ stage: "preparing" });
53 + const prepared = yield* resolver.prepare(entry);
54 + yield* reportProgress({ stage: "opening", prepared });
55 + const session = yield* sessions.connect(prepared);
56 + yield* reportProgress({ stage: "synchronizing", prepared });
57 + yield* session.ready;
58 + return { prepared, session } satisfies EnvironmentConnectionLease;
59 + });
60 +
61 + return ConnectionDriver.of({ connect });
62 +});
63
64 - return ConnectionDriver.of({ connect });
65 - }),
66 -);
64 +export const layer = Layer.effect(ConnectionDriver, make);
packages/client-runtime/src/connection/errors.ts
+17 -17
@@ -11,14 +11,14 @@ import {
11 export function profileMissingError(connectionId: string): ConnectionBlockedError {
12 return new ConnectionBlockedError({
13 reason: "configuration",
14 - message: `Connection profile ${connectionId} is unavailable.`,
14 + detail: `Connection profile ${connectionId} is unavailable.`,
15 });
16 }
17
18 export function credentialMissingError(connectionId: string): ConnectionBlockedError {
19 return new ConnectionBlockedError({
20 reason: "authentication",
21 - message: `Connection credential ${connectionId} is unavailable.`,
21 + detail: `Connection credential ${connectionId} is unavailable.`,
22 });
23 }
24
@@ -28,7 +28,7 @@ export function environmentMismatchError(input: {
28 }): ConnectionBlockedError {
29 return new ConnectionBlockedError({
30 reason: "configuration",
31 - message: `Connected environment ${input.actual} does not match ${input.expected}.`,
31 + detail: `Connected environment ${input.actual} does not match ${input.expected}.`,
32 });
33 }
34
@@ -40,34 +40,34 @@ function relayProtectedError(error: RelayProtectedError): ConnectionAttemptError
40 case "RelayAgentActivityPublishProofInvalidError":
41 return new ConnectionBlockedError({
42 reason: "authentication",
43 - message: error.message,
43 + detail: error.message,
44 traceId: error.traceId,
45 });
46 case "RelayEnvironmentConnectNotAuthorizedError":
47 case "RelayEnvironmentLinkProofInvalidError":
48 return new ConnectionBlockedError({
49 reason: "permission",
50 - message: error.message,
50 + detail: error.message,
51 traceId: error.traceId,
52 });
53 case "RelayEnvironmentEndpointTimedOutError":
54 return new ConnectionTransientError({
55 reason: "timeout",
56 - message: error.message,
56 + detail: error.message,
57 traceId: error.traceId,
58 });
59 case "RelayEnvironmentEndpointUnavailableError":
60 case "RelayEnvironmentLinkUnavailableError":
61 return new ConnectionTransientError({
62 reason: "endpoint-unavailable",
63 - message: error.message,
63 + detail: error.message,
64 traceId: error.traceId,
65 });
66 case "RelayEnvironmentLinkFailedError":
67 case "RelayInternalError":
68 return new ConnectionTransientError({
69 reason: "relay-unavailable",
70 - message: error.message,
70 + detail: error.message,
71 traceId: error.traceId,
72 });
73 }
@@ -80,13 +80,13 @@ export function mapManagedRelayError(error: ManagedRelayClientError): Connection
80 if (error.cause?._tag === "ManagedRelayRequestTimeoutError") {
81 return new ConnectionTransientError({
82 reason: "timeout",
83 - message: error.message,
83 + detail: error.message,
84 ...(error.traceId ? { traceId: error.traceId } : {}),
85 });
86 }
87 return new ConnectionTransientError({
88 reason: "relay-unavailable",
89 - message: error.message,
89 + detail: error.message,
90 ...(error.traceId ? { traceId: error.traceId } : {}),
91 });
92 }
@@ -98,43 +98,43 @@ export function mapRemoteEnvironmentError(
98 case "EnvironmentAuthInvalidError":
99 return new ConnectionBlockedError({
100 reason: "authentication",
101 - message: "The environment credential is invalid.",
101 + detail: "The environment credential is invalid.",
102 traceId: error.traceId,
103 });
104 case "EnvironmentScopeRequiredError":
105 case "EnvironmentOperationForbiddenError":
106 return new ConnectionBlockedError({
107 reason: "permission",
108 - message: "The environment credential does not grant the required access.",
108 + detail: "The environment credential does not grant the required access.",
109 traceId: error.traceId,
110 });
111 case "EnvironmentRequestInvalidError":
112 return new ConnectionBlockedError({
113 reason: "configuration",
114 - message: "The environment rejected the authentication request.",
114 + detail: "The environment rejected the authentication request.",
115 traceId: error.traceId,
116 });
117 case "RemoteEnvironmentAuthTimeoutError":
118 return new ConnectionTransientError({
119 reason: "timeout",
120 - message: error.message,
120 + detail: error.message,
121 });
122 case "RemoteEnvironmentAuthFetchError":
123 return new ConnectionTransientError({
124 reason: "network",
125 - message: error.message,
125 + detail: error.message,
126 });
127 case "EnvironmentInternalError":
128 return new ConnectionTransientError({
129 reason: "remote-unavailable",
130 - message: "The environment could not authorize the connection.",
130 + detail: "The environment could not authorize the connection.",
131 traceId: error.traceId,
132 });
133 case "RemoteEnvironmentAuthInvalidJsonError":
134 case "RemoteEnvironmentAuthUndeclaredStatusError":
135 return new ConnectionTransientError({
136 reason: "remote-unavailable",
137 - message: error.message,
137 + detail: error.message,
138 });
139 }
140 }
packages/client-runtime/src/connection/index.ts
+29 -8
@@ -1,12 +1,33 @@
1 export * from "./catalog.ts";
2 -export * from "./connectivity.ts";
3 -export * from "./driver.ts";
2 +export * as Connectivity from "./connectivity.ts";
3 +export * as CredentialStore from "./credentialStore.ts";
4 +export {
5 + ConnectionDriver,
6 + type ConnectionDriverProgress,
7 + type EnvironmentConnectionLease,
8 +} from "./driver.ts";
9 export * from "./errors.ts";
5 -export * from "./layer.ts";
10 +export * as Connection from "./layer.ts";
11 export * from "./model.ts";
7 -export * from "./onboarding.ts";
12 +export {
13 + type BearerConnectionUpdateInput,
14 + ConnectionOnboarding,
15 + type PairingConnectionInput,
16 + type SshConnectionInput,
17 + prepareBearerConnectionUpdate,
18 + preparePairingRegistration,
19 + prepareSshRegistration,
20 + registerPairingConnection,
21 + registerSshConnection,
22 + updateBearerConnection,
23 +} from "./onboarding.ts";
24 export * from "./presentation.ts";
9 -export * from "./registry.ts";
10 -export * from "./resolver.ts";
11 -export * from "./supervisor.ts";
12 -export * from "./wakeups.ts";
25 +export * as ProfileStore from "./profileStore.ts";
26 +export {
27 + EnvironmentNotRegisteredError,
28 + EnvironmentRegistry,
29 + PlatformEnvironmentRemovalError,
30 +} from "./registry.ts";
31 +export { ConnectionResolver } from "./resolver.ts";
32 +export { EnvironmentSupervisor, type EnvironmentSupervisorOptions } from "./supervisor.ts";
33 +export * as Wakeups from "./wakeups.ts";
packages/client-runtime/src/connection/layer.ts
+19 -21
@@ -2,37 +2,37 @@ import * as Effect from "effect/Effect";
2 import * as Layer from "effect/Layer";
3 import * as Stream from "effect/Stream";
4
5 -import { connectionResolverLayer } from "./resolver.ts";
6 -import { connectionDriverLayer } from "./driver.ts";
7 -import { environmentRegistryLayer, EnvironmentRegistry } from "./registry.ts";
8 -import { connectionOnboardingLayer } from "./onboarding.ts";
9 -import { PlatformConnectionSource } from "../platform/source.ts";
10 -import { relayEnvironmentDiscoveryLayer } from "../relay/discovery.ts";
11 -import { remoteEnvironmentAuthorizationLayer } from "../authorization/layer.ts";
12 -import { rpcSessionFactoryLayer } from "../rpc/session.ts";
13 -
14 -const resolverLayer = connectionResolverLayer.pipe(
15 - Layer.provide(remoteEnvironmentAuthorizationLayer),
5 +import * as ConnectionResolver from "./resolver.ts";
6 +import * as ConnectionDriver from "./driver.ts";
7 +import * as EnvironmentRegistry from "./registry.ts";
8 +import * as ConnectionOnboarding from "./onboarding.ts";
9 +import * as PlatformConnectionSource from "../platform/source.ts";
10 +import * as RelayEnvironmentDiscovery from "../relay/discovery.ts";
11 +import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
12 +import * as RpcSession from "../rpc/session.ts";
13 +
14 +const resolverLayer = ConnectionResolver.layer.pipe(
15 + Layer.provide(RemoteEnvironmentAuthorization.layer),
16 );
17
18 -const driverLayer = connectionDriverLayer.pipe(
19 - Layer.provide(Layer.mergeAll(resolverLayer, rpcSessionFactoryLayer)),
18 +const driverLayer = ConnectionDriver.layer.pipe(
19 + Layer.provide(Layer.mergeAll(resolverLayer, RpcSession.layer)),
20 );
21
22 -const registryLayer = environmentRegistryLayer.pipe(Layer.provide(driverLayer));
22 +const registryLayer = EnvironmentRegistry.layer.pipe(Layer.provide(driverLayer));
23
24 -const onboardingLayer = connectionOnboardingLayer.pipe(Layer.provide(registryLayer));
24 +const onboardingLayer = ConnectionOnboarding.layer.pipe(Layer.provide(registryLayer));
25
26 const connectionServicesLayer = Layer.mergeAll(
27 registryLayer,
28 - relayEnvironmentDiscoveryLayer,
28 + RelayEnvironmentDiscovery.layer,
29 onboardingLayer,
30 );
31
32 const connectionStartupLayer = Layer.effectDiscard(
33 Effect.gen(function* () {
34 - const registry = yield* EnvironmentRegistry;
35 - const platformSource = yield* PlatformConnectionSource;
34 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
35 + const platformSource = yield* PlatformConnectionSource.PlatformConnectionSource;
36 yield* registry.start;
37 yield* platformSource.registrations.pipe(
38 Stream.runForEach(registry.registerPlatform),
@@ -41,6 +41,4 @@ const connectionStartupLayer = Layer.effectDiscard(
41 }).pipe(Effect.withSpan("clientRuntime.connection.application.start")),
42 );
43
44 -export const connectionLayer = connectionStartupLayer.pipe(
45 - Layer.provideMerge(connectionServicesLayer),
46 -);
44 +export const layer = connectionStartupLayer.pipe(Layer.provideMerge(connectionServicesLayer));
packages/client-runtime/src/connection/model.ts
+31 -26
@@ -57,44 +57,49 @@ export type ConnectionTargetKind = ConnectionTarget["_tag"];
57
58 export type NetworkStatus = "unknown" | "offline" | "online";
59
60 -export type ConnectionTransientReason =
61 - | "network"
62 - | "timeout"
63 - | "transport"
64 - | "endpoint-unavailable"
65 - | "relay-unavailable"
66 - | "remote-unavailable";
67 -
68 -export type ConnectionBlockedReason =
69 - | "authentication"
70 - | "configuration"
71 - | "permission"
72 - | "unsupported";
60 +export const ConnectionTransientReason = Schema.Literals([
61 + "network",
62 + "timeout",
63 + "transport",
64 + "endpoint-unavailable",
65 + "relay-unavailable",
66 + "remote-unavailable",
67 +]);
68 +export type ConnectionTransientReason = typeof ConnectionTransientReason.Type;
69 +
70 +export const ConnectionBlockedReason = Schema.Literals([
71 + "authentication",
72 + "configuration",
73 + "permission",
74 + "unsupported",
75 +]);
76 +export type ConnectionBlockedReason = typeof ConnectionBlockedReason.Type;
77
78 export class ConnectionTransientError extends Schema.TaggedErrorClass<ConnectionTransientError>()(
79 "ConnectionTransientError",
80 {
77 - reason: Schema.Literals([
78 - "network",
79 - "timeout",
80 - "transport",
81 - "endpoint-unavailable",
82 - "relay-unavailable",
83 - "remote-unavailable",
84 - ]),
85 - message: Schema.String,
81 + reason: ConnectionTransientReason,
82 + detail: Schema.String,
83 traceId: Schema.optionalKey(Schema.String),
84 },
88 -) {}
85 +) {
86 + override get message(): string {
87 + return this.detail;
88 + }
89 +}
90
91 export class ConnectionBlockedError extends Schema.TaggedErrorClass<ConnectionBlockedError>()(
92 "ConnectionBlockedError",
93 {
93 - reason: Schema.Literals(["authentication", "configuration", "permission", "unsupported"]),
94 - message: Schema.String,
94 + reason: ConnectionBlockedReason,
95 + detail: Schema.String,
96 traceId: Schema.optionalKey(Schema.String),
97 },
97 -) {}
98 +) {
99 + override get message(): string {
100 + return this.detail;
101 + }
102 +}
103
104 export type ConnectionAttemptError = ConnectionTransientError | ConnectionBlockedError;
105
packages/client-runtime/src/connection/onboarding.ts
+52 -47
@@ -6,22 +6,22 @@ import * as Layer from "effect/Layer";
6 import * as Option from "effect/Option";
7 import * as Schema from "effect/Schema";
8 import * as SubscriptionRef from "effect/SubscriptionRef";
9 -import { HttpClient } from "effect/unstable/http";
9 +import * as HttpClient from "effect/unstable/http/HttpClient";
10
11 import { bootstrapRemoteBearerSession } from "../authorization/remote.ts";
12 import { deriveWsBaseUrl, normalizeHttpBaseUrl } from "../environment/endpoint.ts";
13 import { fetchRemoteEnvironmentDescriptor } from "../environment/descriptor.ts";
14 -import { ClientPresentation, SshEnvironmentGateway } from "../platform/capabilities.ts";
14 +import * as ClientCapabilities from "../platform/capabilities.ts";
15 import {
16 BearerConnectionCredential,
17 BearerConnectionProfile,
18 BearerConnectionRegistration,
19 type ConnectionCatalogEntry,
20 type ConnectionCredential,
21 - ConnectionCredentialStore,
21 SshConnectionProfile,
22 SshConnectionRegistration,
23 } from "./catalog.ts";
24 +import * as ConnectionCredentialStore from "./credentialStore.ts";
25 import { mapRemoteEnvironmentError } from "./errors.ts";
26 import {
27 BearerConnectionTarget,
@@ -29,8 +29,8 @@ import {
29 SshConnectionTarget,
30 type ConnectionAttemptError,
31 } from "./model.ts";
32 -import type { ConnectionPersistenceError } from "../platform/persistence.ts";
33 -import { EnvironmentRegistry } from "./registry.ts";
32 +import * as Persistence from "../platform/persistence.ts";
33 +import * as EnvironmentRegistry from "./registry.ts";
34
35 export interface PairingConnectionInput {
36 readonly pairingUrl?: string;
@@ -54,13 +54,19 @@ export class ConnectionOnboarding extends Context.Service<
54 {
55 readonly registerPairing: (
56 input: PairingConnectionInput,
57 - ) => Effect.Effect<EnvironmentId, ConnectionAttemptError | ConnectionPersistenceError>;
57 + ) => Effect.Effect<
58 + EnvironmentId,
59 + ConnectionAttemptError | Persistence.ConnectionPersistenceError
60 + >;
61 readonly registerSsh: (
62 input: SshConnectionInput,
60 - ) => Effect.Effect<EnvironmentId, ConnectionAttemptError | ConnectionPersistenceError>;
63 + ) => Effect.Effect<
64 + EnvironmentId,
65 + ConnectionAttemptError | Persistence.ConnectionPersistenceError
66 + >;
67 readonly updateBearer: (
68 input: BearerConnectionUpdateInput,
63 - ) => Effect.Effect<void, ConnectionAttemptError | ConnectionPersistenceError>;
69 + ) => Effect.Effect<void, ConnectionAttemptError | Persistence.ConnectionPersistenceError>;
70 }
71 >()("@t3tools/client-runtime/connection/onboarding/ConnectionOnboarding") {}
72
@@ -71,7 +77,7 @@ const resolvePairingTarget = Effect.fn("clientRuntime.connection.onboarding.reso
77 catch: (cause) =>
78 new ConnectionBlockedError({
79 reason: "configuration",
74 - message: cause instanceof Error ? cause.message : "The pairing details are invalid.",
80 + detail: cause instanceof Error ? cause.message : "The pairing details are invalid.",
81 }),
82 });
83 },
@@ -81,7 +87,7 @@ export const preparePairingRegistration = Effect.fn(
87 "clientRuntime.connection.onboarding.preparePairingRegistration",
88 )(function* (input: PairingConnectionInput) {
89 const target = yield* resolvePairingTarget(input);
84 - const presentation = yield* ClientPresentation;
90 + const presentation = yield* ClientCapabilities.ClientPresentation;
91 const descriptor = yield* fetchRemoteEnvironmentDescriptor({
92 httpBaseUrl: target.httpBaseUrl,
93 }).pipe(Effect.mapError(mapRemoteEnvironmentError));
@@ -116,7 +122,7 @@ export const registerPairingConnection = Effect.fn(
122 "clientRuntime.connection.onboarding.registerPairingConnection",
123 )(function* (input: PairingConnectionInput) {
124 const registration = yield* preparePairingRegistration(input);
119 - const registry = yield* EnvironmentRegistry;
125 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
126 yield* registry.register(registration);
127 return registration.target.environmentId;
128 });
@@ -127,8 +133,8 @@ const isBearerProfile = Schema.is(BearerConnectionProfile);
133 export const updateBearerConnection = Effect.fn(
134 "clientRuntime.connection.onboarding.updateBearerConnection",
135 )(function* (input: BearerConnectionUpdateInput) {
130 - const registry = yield* EnvironmentRegistry;
131 - const credentials = yield* ConnectionCredentialStore;
136 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
137 + const credentials = yield* ConnectionCredentialStore.ConnectionCredentialStore;
138 const entry = (yield* SubscriptionRef.get(registry.entries)).get(input.environmentId);
139 const credential =
140 entry?.target._tag === "BearerConnectionTarget"
@@ -159,7 +165,7 @@ export const prepareBearerConnectionUpdate = Effect.fn(
165 ) {
166 return yield* new ConnectionBlockedError({
167 reason: "configuration",
162 - message: "Only saved bearer environments can be edited.",
168 + detail: "Only saved bearer environments can be edited.",
169 });
170 }
171
@@ -167,7 +173,7 @@ export const prepareBearerConnectionUpdate = Effect.fn(
173 if (Option.isNone(credential) || !isBearerCredential(credential.value)) {
174 return yield* new ConnectionBlockedError({
175 reason: "authentication",
170 - message: "The saved bearer credential is unavailable.",
176 + detail: "The saved bearer credential is unavailable.",
177 });
178 }
179
@@ -175,7 +181,7 @@ export const prepareBearerConnectionUpdate = Effect.fn(
181 if (label === "") {
182 return yield* new ConnectionBlockedError({
183 reason: "configuration",
178 - message: "Environment label cannot be empty.",
184 + detail: "Environment label cannot be empty.",
185 });
186 }
187 const httpBaseUrl = yield* Effect.try({
@@ -183,7 +189,7 @@ export const prepareBearerConnectionUpdate = Effect.fn(
189 catch: (cause) =>
190 new ConnectionBlockedError({
191 reason: "configuration",
186 - message: cause instanceof Error ? cause.message : "The environment URL is invalid.",
192 + detail: cause instanceof Error ? cause.message : "The environment URL is invalid.",
193 }),
194 });
195 const connectionId = entry.target.connectionId;
@@ -207,7 +213,7 @@ export const prepareBearerConnectionUpdate = Effect.fn(
213 export const prepareSshRegistration = Effect.fn(
214 "clientRuntime.connection.onboarding.prepareSshRegistration",
215 )(function* (input: SshConnectionInput) {
210 - const gateway = yield* SshEnvironmentGateway;
216 + const gateway = yield* ClientCapabilities.SshEnvironmentGateway;
217 const provisioned = yield* gateway.provision(input.target);
218 const connectionId = `ssh:${provisioned.environmentId}`;
219 const label = input.label?.trim() || provisioned.label || provisioned.bootstrap.target.alias;
@@ -231,37 +237,36 @@ export const registerSshConnection = Effect.fn(
237 "clientRuntime.connection.onboarding.registerSshConnection",
238 )(function* (input: SshConnectionInput) {
239 const registration = yield* prepareSshRegistration(input);
234 - const registry = yield* EnvironmentRegistry;
240 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
241 yield* registry.register(registration);
242 return registration.target.environmentId;
243 });
244
239 -export const connectionOnboardingLayer = Layer.effect(
240 - ConnectionOnboarding,
241 - Effect.gen(function* () {
242 - const registry = yield* EnvironmentRegistry;
243 - const presentation = yield* ClientPresentation;
244 - const httpClient = yield* HttpClient.HttpClient;
245 - const ssh = yield* SshEnvironmentGateway;
246 - const credentials = yield* ConnectionCredentialStore;
245 +export const make = Effect.gen(function* () {
246 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
247 + const presentation = yield* ClientCapabilities.ClientPresentation;
248 + const httpClient = yield* HttpClient.HttpClient;
249 + const ssh = yield* ClientCapabilities.SshEnvironmentGateway;
250 + const credentials = yield* ConnectionCredentialStore.ConnectionCredentialStore;
251
248 - return ConnectionOnboarding.of({
249 - registerPairing: (input) =>
250 - registerPairingConnection(input).pipe(
251 - Effect.provideService(EnvironmentRegistry, registry),
252 - Effect.provideService(ClientPresentation, presentation),
253 - Effect.provideService(HttpClient.HttpClient, httpClient),
254 - ),
255 - registerSsh: (input) =>
256 - registerSshConnection(input).pipe(
257 - Effect.provideService(EnvironmentRegistry, registry),
258 - Effect.provideService(SshEnvironmentGateway, ssh),
259 - ),
260 - updateBearer: (input) =>
261 - updateBearerConnection(input).pipe(
262 - Effect.provideService(EnvironmentRegistry, registry),
263 - Effect.provideService(ConnectionCredentialStore, credentials),
264 - ),
265 - });
266 - }),
267 -);
252 + return ConnectionOnboarding.of({
253 + registerPairing: (input) =>
254 + registerPairingConnection(input).pipe(
255 + Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, registry),
256 + Effect.provideService(ClientCapabilities.ClientPresentation, presentation),
257 + Effect.provideService(HttpClient.HttpClient, httpClient),
258 + ),
259 + registerSsh: (input) =>
260 + registerSshConnection(input).pipe(
261 + Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, registry),
262 + Effect.provideService(ClientCapabilities.SshEnvironmentGateway, ssh),
263 + ),
264 + updateBearer: (input) =>
265 + updateBearerConnection(input).pipe(
266 + Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, registry),
267 + Effect.provideService(ConnectionCredentialStore.ConnectionCredentialStore, credentials),
268 + ),
269 + });
270 +});
271 +
272 +export const layer = Layer.effect(ConnectionOnboarding, make);
packages/client-runtime/src/connection/presentation.test.ts
+3 -3
@@ -67,7 +67,7 @@ describe("connection presentation", () => {
67 attempt: 2,
68 lastFailure: new ConnectionTransientError({
69 reason: "transport",
70 - message: "Socket closed.",
70 + detail: "Socket closed.",
71 traceId: "trace-previous",
72 }),
73 }),
@@ -85,7 +85,7 @@ describe("connection presentation", () => {
85 retryAt: 1,
86 lastFailure: new ConnectionTransientError({
87 reason: "transport",
88 - message: "Disconnected.",
88 + detail: "Disconnected.",
89 traceId: "trace-1",
90 }),
91 }),
@@ -106,7 +106,7 @@ describe("connection presentation", () => {
106 attempt: 2,
107 lastFailure: new ConnectionTransientError({
108 reason: "transport",
109 - message: "Relay connection timed out.",
109 + detail: "Relay connection timed out.",
110 traceId: "trace-retry",
111 }),
112 }),
packages/client-runtime/src/connection/profileStore.ts new
+24
@@ -0,0 +1,24 @@
1 +import * as Context from "effect/Context";
2 +import type * as Effect from "effect/Effect";
3 +import * as Layer from "effect/Layer";
4 +import type * as Option from "effect/Option";
5 +
6 +import type { ConnectionProfile } from "./catalog.ts";
7 +import type { ConnectionAttemptError } from "./model.ts";
8 +
9 +export class ConnectionProfileStore extends Context.Service<
10 + ConnectionProfileStore,
11 + {
12 + readonly get: (
13 + connectionId: string,
14 + ) => Effect.Effect<Option.Option<ConnectionProfile>, ConnectionAttemptError>;
15 + readonly put: (profile: ConnectionProfile) => Effect.Effect<void, ConnectionAttemptError>;
16 + readonly remove: (connectionId: string) => Effect.Effect<void, ConnectionAttemptError>;
17 + }
18 +>()("@t3tools/client-runtime/connection/profileStore/ConnectionProfileStore") {}
19 +
20 +export const make = (service: ConnectionProfileStore["Service"]) =>
21 + ConnectionProfileStore.of(service);
22 +
23 +export const layer = (service: ConnectionProfileStore["Service"]) =>
24 + Layer.succeed(ConnectionProfileStore, make(service));
packages/client-runtime/src/connection/registry.test.ts
+63 -66
@@ -14,23 +14,22 @@ import * as Result from "effect/Result";
14 import * as Stream from "effect/Stream";
15 import * as SubscriptionRef from "effect/SubscriptionRef";
16
17 -import { SshEnvironmentGateway } from "../platform/capabilities.ts";
18 -import { RemoteDpopAccessToken, RemoteDpopAccessTokenStore } from "../authorization/tokenStore.ts";
17 +import * as ClientCapabilities from "../platform/capabilities.ts";
18 +import * as TokenStore from "../authorization/tokenStore.ts";
19 import {
20 BearerConnectionCredential,
21 BearerConnectionProfile,
22 BearerConnectionRegistration,
23 type ConnectionRegistration,
24 - ConnectionCredentialStore,
25 - ConnectionProfileStore,
24 PrimaryConnectionRegistration,
25 RelayConnectionRegistration,
26 SshConnectionProfile,
27 type ConnectionCredential,
28 type ConnectionProfile,
29 } from "./catalog.ts";
32 -import { Connectivity } from "./connectivity.ts";
33 -import { ConnectionDriver } from "./driver.ts";
30 +import * as Connectivity from "./connectivity.ts";
31 +import * as ConnectionCredentialStore from "./credentialStore.ts";
32 +import * as ConnectionDriver from "./driver.ts";
33 import {
34 ConnectionTransientError,
35 BearerConnectionTarget,
@@ -41,17 +40,12 @@ import {
40 type PreparedConnection,
41 type SupervisorConnectionState,
42 } from "./model.ts";
44 -import {
45 - ConnectionPersistenceError,
46 - ConnectionRegistrationStore,
47 - ConnectionTargetStore,
48 - EnvironmentCacheStore,
49 - EnvironmentOwnedDataCleanup,
50 -} from "../platform/persistence.ts";
51 -import { EnvironmentRegistry, environmentRegistryLayer } from "./registry.ts";
52 -import type { RpcSession } from "../rpc/session.ts";
53 -import { EnvironmentSupervisor } from "./supervisor.ts";
54 -import { ConnectionWakeups } from "./wakeups.ts";
43 +import * as Persistence from "../platform/persistence.ts";
44 +import * as ConnectionProfileStore from "./profileStore.ts";
45 +import * as EnvironmentRegistry from "./registry.ts";
46 +import * as RpcSession from "../rpc/session.ts";
47 +import * as EnvironmentSupervisor from "./supervisor.ts";
48 +import * as ConnectionWakeups from "./wakeups.ts";
49
50 const TARGET = new PrimaryConnectionTarget({
51 environmentId: EnvironmentId.make("environment-1"),
@@ -137,10 +131,10 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
131 readonly beforeSessionConnect?: (environmentId: EnvironmentId) => Effect.Effect<void>;
132 readonly beforeRegistrationRegister?: (
133 registration: ConnectionRegistration,
140 - ) => Effect.Effect<void, ConnectionPersistenceError>;
134 + ) => Effect.Effect<void, Persistence.ConnectionPersistenceError>;
135 readonly beforeRegistrationRemove?: (
136 target: ConnectionTarget,
143 - ) => Effect.Effect<void, ConnectionPersistenceError>;
137 + ) => Effect.Effect<void, Persistence.ConnectionPersistenceError>;
138 },
139 ) {
140 const storedTargets = yield* Ref.make(
@@ -160,7 +154,7 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
154 new Map([
155 [
156 SSH_CONNECTION.environmentId,
163 - new RemoteDpopAccessToken({
157 + new TokenStore.RemoteDpopAccessToken({
158 environmentId: SSH_CONNECTION.environmentId,
159 label: SSH_CONNECTION.label,
160 endpoint: {
@@ -177,10 +171,10 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
171 );
172 const disconnectedSshTargets = yield* Ref.make<ReadonlyArray<DesktopSshEnvironmentTarget>>([]);
173
180 - const targetStore = ConnectionTargetStore.of({
174 + const targetStore = Persistence.ConnectionTargetStore.of({
175 list: Ref.get(storedTargets).pipe(Effect.map((targets) => [...targets.values()])),
176 });
183 - const registrationStore = ConnectionRegistrationStore.of({
177 + const registrationStore = Persistence.ConnectionRegistrationStore.of({
178 register: (registration) =>
179 Effect.gen(function* () {
180 yield* options?.beforeRegistrationRegister?.(registration) ?? Effect.void;
@@ -239,7 +233,7 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
233 });
234 }),
235 });
242 - const cacheStore = EnvironmentCacheStore.of({
236 + const cacheStore = Persistence.EnvironmentCacheStore.of({
237 loadShell: (environmentId) =>
238 Ref.get(shellCache).pipe(
239 Effect.map((cache) => Option.fromUndefinedOr(cache.get(environmentId))),
@@ -264,16 +258,16 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
258 ),
259 ),
260 });
267 - const ownedDataCleanup = EnvironmentOwnedDataCleanup.of({
261 + const ownedDataCleanup = Persistence.EnvironmentOwnedDataCleanup.of({
262 clear: (environmentId) =>
263 Ref.update(ownedDataClears, (environmentIds) => [...environmentIds, environmentId]),
264 });
265 const networkStatus = yield* SubscriptionRef.make<"unknown" | "offline" | "online">("online");
272 - const connectivity = Connectivity.of({
266 + const connectivity = Connectivity.Connectivity.of({
267 status: SubscriptionRef.get(networkStatus),
268 changes: SubscriptionRef.changes(networkStatus),
269 });
276 - const profileStore = ConnectionProfileStore.of({
270 + const profileStore = ConnectionProfileStore.ConnectionProfileStore.of({
271 get: (connectionId) =>
272 Ref.update(profileReadCount, (count) => count + 1).pipe(
273 Effect.andThen(Ref.get(storedProfiles)),
@@ -292,7 +286,7 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
286 return next;
287 }),
288 });
295 - const credentialStore = ConnectionCredentialStore.of({
289 + const credentialStore = ConnectionCredentialStore.ConnectionCredentialStore.of({
290 get: (connectionId) =>
291 Ref.get(storedCredentials).pipe(
292 Effect.map((current) => Option.fromUndefinedOr(current.get(connectionId))),
@@ -310,7 +304,7 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
304 return next;
305 }),
306 });
313 - const tokenStore = RemoteDpopAccessTokenStore.of({
307 + const tokenStore = TokenStore.RemoteDpopAccessTokenStore.of({
308 get: (environmentId) =>
309 Ref.get(storedRemoteTokens).pipe(
310 Effect.map((current) => Option.fromUndefinedOr(current.get(environmentId))),
@@ -328,12 +322,12 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
322 return next;
323 }),
324 });
331 - const sshGateway = SshEnvironmentGateway.of({
325 + const sshGateway = ClientCapabilities.SshEnvironmentGateway.of({
326 provision: () => Effect.die(new Error("SSH provisioning is not used.")),
327 prepare: () => Effect.die(new Error("SSH preparation is not used.")),
328 disconnect: (target) => Ref.update(disconnectedSshTargets, (current) => [...current, target]),
329 });
336 - const driver = ConnectionDriver.of({
330 + const driver = ConnectionDriver.ConnectionDriver.of({
331 connect: (entry, reportProgress) =>
332 Effect.gen(function* () {
333 const target = entry.target;
@@ -350,12 +344,12 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
344 yield* Ref.update(sessions, (current) => [...current, { closed }]);
345 const session = yield* Effect.acquireRelease(
346 Effect.succeed({
353 - client: {} as RpcSession["client"],
347 + client: {} as RpcSession.RpcSession["client"],
348 initialConfig: Effect.die(new Error("Config is not used by registry tests.")),
349 ready: Effect.void,
350 probe: Effect.void,
351 closed: Deferred.await(closed),
358 - } satisfies RpcSession),
352 + } satisfies RpcSession.RpcSession),
353 () => Ref.update(releasedSessions, (count) => count + 1),
354 );
355 yield* reportProgress({ stage: "synchronizing", prepared });
@@ -364,21 +358,24 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
358 }),
359 });
360
367 - const cacheLayer = Layer.succeed(EnvironmentCacheStore, cacheStore);
368 - const layer = environmentRegistryLayer.pipe(
361 + const cacheLayer = Layer.succeed(Persistence.EnvironmentCacheStore, cacheStore);
362 + const layer = EnvironmentRegistry.layer.pipe(
363 Layer.provide(
364 Layer.mergeAll(
371 - Layer.succeed(ConnectionTargetStore, targetStore),
372 - Layer.succeed(ConnectionRegistrationStore, registrationStore),
373 - Layer.succeed(ConnectionProfileStore, profileStore),
374 - Layer.succeed(ConnectionCredentialStore, credentialStore),
375 - Layer.succeed(RemoteDpopAccessTokenStore, tokenStore),
376 - Layer.succeed(SshEnvironmentGateway, sshGateway),
377 - Layer.succeed(Connectivity, connectivity),
378 - Layer.succeed(ConnectionWakeups, ConnectionWakeups.of({ changes: Stream.never })),
379 - Layer.succeed(ConnectionDriver, driver),
365 + Layer.succeed(Persistence.ConnectionTargetStore, targetStore),
366 + Layer.succeed(Persistence.ConnectionRegistrationStore, registrationStore),
367 + Layer.succeed(ConnectionProfileStore.ConnectionProfileStore, profileStore),
368 + Layer.succeed(ConnectionCredentialStore.ConnectionCredentialStore, credentialStore),
369 + Layer.succeed(TokenStore.RemoteDpopAccessTokenStore, tokenStore),
370 + Layer.succeed(ClientCapabilities.SshEnvironmentGateway, sshGateway),
371 + Layer.succeed(Connectivity.Connectivity, connectivity),
372 + Layer.succeed(
373 + ConnectionWakeups.ConnectionWakeups,
374 + ConnectionWakeups.ConnectionWakeups.of({ changes: Stream.never }),
375 + ),
376 + Layer.succeed(ConnectionDriver.ConnectionDriver, driver),
377 cacheLayer,
381 - Layer.succeed(EnvironmentOwnedDataCleanup, ownedDataCleanup),
378 + Layer.succeed(Persistence.EnvironmentOwnedDataCleanup, ownedDataCleanup),
379 ),
380 ),
381 );
@@ -401,7 +398,7 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
398 });
399
400 function awaitConnectionState(
404 - registry: EnvironmentRegistry["Service"],
401 + registry: EnvironmentRegistry.EnvironmentRegistry["Service"],
402 environmentId: EnvironmentId,
403 predicate: (state: SupervisorConnectionState) => boolean,
404 ) {
@@ -422,7 +419,7 @@ describe("EnvironmentRegistry", () => {
419 const harness = yield* makeHarness([SSH_CONNECTION], [SSH_PROFILE]);
420
421 yield* Effect.gen(function* () {
425 - const registry = yield* EnvironmentRegistry;
422 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
423 const entry = (yield* SubscriptionRef.get(registry.entries)).get(
424 SSH_CONNECTION.environmentId,
425 );
@@ -438,7 +435,7 @@ describe("EnvironmentRegistry", () => {
435 const harness = yield* makeHarness([]);
436
437 yield* Effect.gen(function* () {
441 - const registry = yield* EnvironmentRegistry;
438 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
439 const offline = yield* Effect.forkChild(
440 SubscriptionRef.changes(registry.networkStatus).pipe(
441 Stream.filter((status) => status === "offline"),
@@ -471,7 +468,7 @@ describe("EnvironmentRegistry", () => {
468 });
469
470 yield* Effect.gen(function* () {
474 - const registry = yield* EnvironmentRegistry;
471 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
472 const start = yield* Effect.forkChild(registry.start);
473
474 yield* Deferred.await(bothLoadsStarted).pipe(Effect.timeout("1 second"));
@@ -487,7 +484,7 @@ describe("EnvironmentRegistry", () => {
484 Effect.gen(function* () {
485 const harness = yield* makeHarness([TARGET]);
486 yield* Effect.gen(function* () {
490 - const registry = yield* EnvironmentRegistry;
487 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
488 yield* registry.start;
489 yield* awaitConnectionState(
490 registry,
@@ -499,7 +496,7 @@ describe("EnvironmentRegistry", () => {
496 .runStream(
497 TARGET.environmentId,
498 Stream.unwrap(
502 - EnvironmentSupervisor.pipe(
499 + EnvironmentSupervisor.EnvironmentSupervisor.pipe(
500 Effect.map((supervisor) =>
501 Stream.concat(
502 Stream.fromEffect(SubscriptionRef.get(supervisor.state)),
@@ -527,7 +524,7 @@ describe("EnvironmentRegistry", () => {
524 Effect.gen(function* () {
525 const harness = yield* makeHarness([TARGET]);
526 yield* Effect.gen(function* () {
530 - const registry = yield* EnvironmentRegistry;
527 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
528 yield* registry.start;
529 yield* awaitConnectionState(
530 registry,
@@ -554,7 +551,7 @@ describe("EnvironmentRegistry", () => {
551 active!.closed,
552 new ConnectionTransientError({
553 reason: "transport",
557 - message: "Disconnected.",
554 + detail: "Disconnected.",
555 }),
556 );
557 yield* Fiber.join(retryFiber);
@@ -578,7 +575,7 @@ describe("EnvironmentRegistry", () => {
575 const harness = yield* makeHarness([]);
576
577 yield* Effect.gen(function* () {
581 - const registry = yield* EnvironmentRegistry;
578 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
579 yield* registry.register(new RelayConnectionRegistration({ target: RELAY_TARGET }));
580 yield* awaitConnectionState(
581 registry,
@@ -603,7 +600,7 @@ describe("EnvironmentRegistry", () => {
600 const harness = yield* makeHarness([RELAY_TARGET]);
601
602 yield* Effect.gen(function* () {
606 - const registry = yield* EnvironmentRegistry;
603 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
604 const firstObserved = yield* Deferred.make<void>();
605 const secondObserved = yield* Deferred.make<void>();
606 const labels = yield* Ref.make<ReadonlyArray<string>>([]);
@@ -619,7 +616,7 @@ describe("EnvironmentRegistry", () => {
616 .followStream(
617 RELAY_TARGET.environmentId,
618 Stream.unwrap(
622 - EnvironmentSupervisor.pipe(
619 + EnvironmentSupervisor.EnvironmentSupervisor.pipe(
620 Effect.map((supervisor) =>
621 Stream.concat(Stream.succeed(supervisor.target.label), Stream.never),
622 ),
@@ -655,7 +652,7 @@ describe("EnvironmentRegistry", () => {
652 const harness = yield* makeHarness([]);
653
654 yield* Effect.gen(function* () {
658 - const registry = yield* EnvironmentRegistry;
655 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
656 yield* registry.retryNow(EnvironmentId.make("removed-environment"));
657 }).pipe(Effect.provide(harness.layer), Effect.scoped);
658 }),
@@ -670,7 +667,7 @@ describe("EnvironmentRegistry", () => {
667 );
668
669 yield* Effect.gen(function* () {
673 - const registry = yield* EnvironmentRegistry;
670 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
671 yield* registry.removeRelayEnvironments();
672
673 const targets = yield* Ref.get(harness.storedTargets);
@@ -695,7 +692,7 @@ describe("EnvironmentRegistry", () => {
692 const harness = yield* makeHarness([RELAY_TARGET], [], [], {
693 beforeRegistrationRemove: () =>
694 Effect.fail(
698 - new ConnectionPersistenceError({
695 + new Persistence.ConnectionPersistenceError({
696 operation: "remove-connection",
697 message: "Storage is unavailable.",
698 }),
@@ -703,7 +700,7 @@ describe("EnvironmentRegistry", () => {
700 });
701
702 yield* Effect.gen(function* () {
706 - const registry = yield* EnvironmentRegistry;
703 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
704 yield* registry.start;
705 yield* awaitConnectionState(
706 registry,
@@ -730,7 +727,7 @@ describe("EnvironmentRegistry", () => {
727 const harness = yield* makeHarness([]);
728
729 yield* Effect.gen(function* () {
733 - const registry = yield* EnvironmentRegistry;
730 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
731 yield* registry.register(
732 new BearerConnectionRegistration({
733 target: BEARER_TARGET,
@@ -760,7 +757,7 @@ describe("EnvironmentRegistry", () => {
757 const harness = yield* makeHarness([]);
758
759 yield* Effect.gen(function* () {
763 - const registry = yield* EnvironmentRegistry;
760 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
761 yield* registry.registerPlatform(new PrimaryConnectionRegistration({ target: TARGET }));
762 yield* awaitConnectionState(
763 registry,
@@ -791,7 +788,7 @@ describe("EnvironmentRegistry", () => {
788 const harness = yield* makeHarness([shadowedTarget]);
789
790 yield* Effect.gen(function* () {
794 - const registry = yield* EnvironmentRegistry;
791 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
792 yield* registry.registerPlatform(new PrimaryConnectionRegistration({ target: TARGET }));
793
794 expect(
@@ -825,7 +822,7 @@ describe("EnvironmentRegistry", () => {
822 });
823
824 yield* Effect.gen(function* () {
828 - const registry = yield* EnvironmentRegistry;
825 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
826 const persistedRegistration = yield* registry
827 .register(new RelayConnectionRegistration({ target: shadowedTarget }))
828 .pipe(Effect.forkChild({ startImmediately: true }));
@@ -864,7 +861,7 @@ describe("EnvironmentRegistry", () => {
861 });
862
863 yield* Effect.gen(function* () {
867 - const registry = yield* EnvironmentRegistry;
864 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
865 yield* registry.start;
866 yield* awaitConnectionState(
867 registry,
@@ -894,7 +891,7 @@ describe("EnvironmentRegistry", () => {
891 const harness = yield* makeHarness([]);
892
893 yield* Effect.gen(function* () {
897 - const registry = yield* EnvironmentRegistry;
894 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
895 const registration = new PrimaryConnectionRegistration({ target: TARGET });
896 yield* registry.registerPlatform(registration);
897 yield* awaitConnectionState(
@@ -924,7 +921,7 @@ describe("EnvironmentRegistry", () => {
921 );
922
923 yield* Effect.gen(function* () {
927 - const registry = yield* EnvironmentRegistry;
924 + const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
925 yield* registry.start;
926 yield* registry.remove(SSH_CONNECTION.environmentId);
927
packages/client-runtime/src/connection/registry.ts
+107 -102
@@ -1,4 +1,4 @@
1 -import type { EnvironmentId } from "@t3tools/contracts";
1 +import { EnvironmentId } from "@t3tools/contracts";
2 import * as Context from "effect/Context";
3 import * as Effect from "effect/Effect";
4 import * as Equal from "effect/Equal";
@@ -12,120 +12,124 @@ import * as Semaphore from "effect/Semaphore";
12 import * as Stream from "effect/Stream";
13 import * as SubscriptionRef from "effect/SubscriptionRef";
14
15 -import { SshEnvironmentGateway } from "../platform/capabilities.ts";
15 +import * as ClientCapabilities from "../platform/capabilities.ts";
16 import {
17 type ConnectionCatalogEntry,
18 type ConnectionRegistration,
19 - ConnectionProfileStore,
19 type PrimaryConnectionRegistration,
20 SshConnectionProfile,
21 connectionRegistrationCatalogEntry,
22 } from "./catalog.ts";
24 -import { Connectivity } from "./connectivity.ts";
23 +import * as ConnectionProfileStore from "./profileStore.ts";
24 +import * as Connectivity from "./connectivity.ts";
25 import type {
26 ConnectionAttemptError,
27 ConnectionTarget,
28 NetworkStatus,
29 SupervisorConnectionState,
30 } from "./model.ts";
31 -import {
32 - type ConnectionPersistenceError,
33 - ConnectionRegistrationStore,
34 - ConnectionTargetStore,
35 - EnvironmentCacheStore,
36 - EnvironmentOwnedDataCleanup,
37 -} from "../platform/persistence.ts";
38 -import {
39 - EnvironmentSupervisor,
40 - type EnvironmentSupervisorService,
41 - makeEnvironmentSupervisor,
42 -} from "./supervisor.ts";
43 -import { ConnectionDriver } from "./driver.ts";
44 -import { ConnectionWakeups } from "./wakeups.ts";
31 +import * as Persistence from "../platform/persistence.ts";
32 +import * as EnvironmentSupervisor from "./supervisor.ts";
33 +import * as ConnectionDriver from "./driver.ts";
34 +import * as ConnectionWakeups from "./wakeups.ts";
35
36 const isSshConnectionProfile = Schema.is(SshConnectionProfile);
37
38 export class EnvironmentNotRegisteredError extends Schema.TaggedErrorClass<EnvironmentNotRegisteredError>()(
39 "EnvironmentNotRegisteredError",
40 {
51 - environmentId: Schema.String,
52 - message: Schema.String,
41 + environmentId: EnvironmentId,
42 },
54 -) {}
43 +) {
44 + override get message(): string {
45 + return `Environment ${this.environmentId} is not registered.`;
46 + }
47 +}
48
49 export class PlatformEnvironmentRemovalError extends Schema.TaggedErrorClass<PlatformEnvironmentRemovalError>()(
50 "PlatformEnvironmentRemovalError",
51 {
59 - environmentId: Schema.String,
60 - message: Schema.String,
61 - },
62 -) {}
63 -
64 -export interface EnvironmentRegistryService {
65 - readonly entries: SubscriptionRef.SubscriptionRef<
66 - ReadonlyMap<EnvironmentId, ConnectionCatalogEntry>
67 - >;
68 - readonly networkStatus: SubscriptionRef.SubscriptionRef<NetworkStatus>;
69 - readonly start: Effect.Effect<void>;
70 - readonly register: (
71 - registration: ConnectionRegistration,
72 - ) => Effect.Effect<void, ConnectionPersistenceError>;
73 - readonly registerPlatform: (registration: PrimaryConnectionRegistration) => Effect.Effect<void>;
74 - readonly remove: (
52 environmentId: EnvironmentId,
76 - ) => Effect.Effect<
77 - void,
78 - | ConnectionPersistenceError
79 - | ConnectionAttemptError
80 - | EnvironmentNotRegisteredError
81 - | PlatformEnvironmentRemovalError
82 - >;
83 - readonly removeRelayEnvironments: () => Effect.Effect<
84 - void,
85 - ConnectionPersistenceError | ConnectionAttemptError | PlatformEnvironmentRemovalError
86 - >;
87 - readonly retryNow: (environmentId: EnvironmentId) => Effect.Effect<void>;
88 - readonly state: (
89 - environmentId: EnvironmentId,
90 - ) => Effect.Effect<SupervisorConnectionState, EnvironmentNotRegisteredError>;
91 - readonly stateChanges: (
92 - environmentId: EnvironmentId,
93 - ) => Stream.Stream<SupervisorConnectionState, EnvironmentNotRegisteredError>;
94 - readonly run: <A, E, R>(
95 - environmentId: EnvironmentId,
96 - effect: Effect.Effect<A, E, R>,
97 - ) => Effect.Effect<A, E | EnvironmentNotRegisteredError, Exclude<R, EnvironmentSupervisor>>;
98 - readonly runStream: <A, E, R>(
99 - environmentId: EnvironmentId,
100 - stream: Stream.Stream<A, E, R>,
101 - ) => Stream.Stream<A, E | EnvironmentNotRegisteredError, Exclude<R, EnvironmentSupervisor>>;
102 - readonly followStream: <A, E, R>(
103 - environmentId: EnvironmentId,
104 - stream: Stream.Stream<A, E, R>,
105 - ) => Stream.Stream<A, E, Exclude<R, EnvironmentSupervisor>>;
53 + },
54 +) {
55 + override get message(): string {
56 + return `Platform-managed environment ${this.environmentId} cannot be removed.`;
57 + }
58 }
59
60 export class EnvironmentRegistry extends Context.Service<
61 EnvironmentRegistry,
110 - EnvironmentRegistryService
62 + {
63 + readonly entries: SubscriptionRef.SubscriptionRef<
64 + ReadonlyMap<EnvironmentId, ConnectionCatalogEntry>
65 + >;
66 + readonly networkStatus: SubscriptionRef.SubscriptionRef<NetworkStatus>;
67 + readonly start: Effect.Effect<void>;
68 + readonly register: (
69 + registration: ConnectionRegistration,
70 + ) => Effect.Effect<void, Persistence.ConnectionPersistenceError>;
71 + readonly registerPlatform: (registration: PrimaryConnectionRegistration) => Effect.Effect<void>;
72 + readonly remove: (
73 + environmentId: EnvironmentId,
74 + ) => Effect.Effect<
75 + void,
76 + | Persistence.ConnectionPersistenceError
77 + | ConnectionAttemptError
78 + | EnvironmentNotRegisteredError
79 + | PlatformEnvironmentRemovalError
80 + >;
81 + readonly removeRelayEnvironments: () => Effect.Effect<
82 + void,
83 + | Persistence.ConnectionPersistenceError
84 + | ConnectionAttemptError
85 + | PlatformEnvironmentRemovalError
86 + >;
87 + readonly retryNow: (environmentId: EnvironmentId) => Effect.Effect<void>;
88 + readonly state: (
89 + environmentId: EnvironmentId,
90 + ) => Effect.Effect<SupervisorConnectionState, EnvironmentNotRegisteredError>;
91 + readonly stateChanges: (
92 + environmentId: EnvironmentId,
93 + ) => Stream.Stream<SupervisorConnectionState, EnvironmentNotRegisteredError>;
94 + readonly run: <A, E, R>(
95 + environmentId: EnvironmentId,
96 + effect: Effect.Effect<A, E, R>,
97 + ) => Effect.Effect<
98 + A,
99 + E | EnvironmentNotRegisteredError,
100 + Exclude<R, EnvironmentSupervisor.EnvironmentSupervisor>
101 + >;
102 + readonly runStream: <A, E, R>(
103 + environmentId: EnvironmentId,
104 + stream: Stream.Stream<A, E, R>,
105 + ) => Stream.Stream<
106 + A,
107 + E | EnvironmentNotRegisteredError,
108 + Exclude<R, EnvironmentSupervisor.EnvironmentSupervisor>
109 + >;
110 + readonly followStream: <A, E, R>(
111 + environmentId: EnvironmentId,
112 + stream: Stream.Stream<A, E, R>,
113 + ) => Stream.Stream<A, E, Exclude<R, EnvironmentSupervisor.EnvironmentSupervisor>>;
114 + }
115 >()("@t3tools/client-runtime/connection/registry/EnvironmentRegistry") {}
116
117 interface EnvironmentServiceScope {
118 readonly entry: ConnectionCatalogEntry;
115 - readonly supervisor: EnvironmentSupervisorService;
119 + readonly supervisor: EnvironmentSupervisor.EnvironmentSupervisor["Service"];
120 readonly scope: Scope.Closeable;
121 }
122
119 -const makeEnvironmentRegistry = Effect.fn("EnvironmentRegistry.make")(function* () {
120 - const storage = yield* ConnectionTargetStore;
121 - const registrations = yield* ConnectionRegistrationStore;
122 - const cache = yield* EnvironmentCacheStore;
123 - const ownedDataCleanup = yield* EnvironmentOwnedDataCleanup;
124 - const profiles = yield* ConnectionProfileStore;
125 - const connectivity = yield* Connectivity;
126 - const driver = yield* ConnectionDriver;
127 - const wakeups = yield* ConnectionWakeups;
128 - const ssh = yield* SshEnvironmentGateway;
123 +export const make = Effect.gen(function* () {
124 + const storage = yield* Persistence.ConnectionTargetStore;
125 + const registrations = yield* Persistence.ConnectionRegistrationStore;
126 + const cache = yield* Persistence.EnvironmentCacheStore;
127 + const ownedDataCleanup = yield* Persistence.EnvironmentOwnedDataCleanup;
128 + const profiles = yield* ConnectionProfileStore.ConnectionProfileStore;
129 + const connectivity = yield* Connectivity.Connectivity;
130 + const driver = yield* ConnectionDriver.ConnectionDriver;
131 + const wakeups = yield* ConnectionWakeups.ConnectionWakeups;
132 + const ssh = yield* ClientCapabilities.SshEnvironmentGateway;
133 const persistedTargets = yield* storage.list;
134 const initialEntries = new Map(
135 yield* Effect.forEach(
@@ -215,7 +219,6 @@ const makeEnvironmentRegistry = Effect.fn("EnvironmentRegistry.make")(function*
219 if (entry === undefined) {
220 return yield* new EnvironmentNotRegisteredError({
221 environmentId,
218 - message: `Environment ${environmentId} is not registered.`,
222 });
223 }
224 return entry;
@@ -241,12 +244,12 @@ const makeEnvironmentRegistry = Effect.fn("EnvironmentRegistry.make")(function*
244 Effect.gen(function* () {
245 const environmentId = entry.target.environmentId;
246 const scope = yield* Scope.make();
244 - const supervisor = yield* makeEnvironmentSupervisor(entry, {
247 + const supervisor = yield* EnvironmentSupervisor.make(entry, {
248 initiallyDesired: false,
249 }).pipe(
247 - Effect.provideService(Connectivity, connectivity),
248 - Effect.provideService(ConnectionDriver, driver),
249 - Effect.provideService(ConnectionWakeups, wakeups),
250 + Effect.provideService(Connectivity.Connectivity, connectivity),
251 + Effect.provideService(ConnectionDriver.ConnectionDriver, driver),
252 + Effect.provideService(ConnectionWakeups.ConnectionWakeups, wakeups),
253 Scope.provide(scope),
254 Effect.onError(() => Scope.close(scope, Exit.void)),
255 );
@@ -280,28 +283,30 @@ const makeEnvironmentRegistry = Effect.fn("EnvironmentRegistry.make")(function*
283 );
284 });
285
283 - const run: EnvironmentRegistryService["run"] = Effect.fn("EnvironmentRegistry.run")(function* <
284 - A,
285 - E,
286 - R,
287 - >(environmentId: EnvironmentId, effect: Effect.Effect<A, E, R>) {
288 - const supervisor = yield* acquireSupervisor(environmentId);
289 - return yield* Effect.provideService(effect, EnvironmentSupervisor, supervisor);
290 - });
286 + const run: EnvironmentRegistry["Service"]["run"] = Effect.fn("EnvironmentRegistry.run")(
287 + function* <A, E, R>(environmentId: EnvironmentId, effect: Effect.Effect<A, E, R>) {
288 + const supervisor = yield* acquireSupervisor(environmentId);
289 + return yield* Effect.provideService(
290 + effect,
291 + EnvironmentSupervisor.EnvironmentSupervisor,
292 + supervisor,
293 + );
294 + },
295 + );
296
292 - const runStream: EnvironmentRegistryService["runStream"] = <A, E, R>(
297 + const runStream: EnvironmentRegistry["Service"]["runStream"] = <A, E, R>(
298 environmentId: EnvironmentId,
299 stream: Stream.Stream<A, E, R>,
300 ) =>
301 Stream.unwrap(
302 acquireSupervisor(environmentId).pipe(
303 Effect.map((supervisor) =>
299 - Stream.provideService(stream, EnvironmentSupervisor, supervisor),
304 + Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
305 ),
306 ),
307 );
308
304 - const followStream: EnvironmentRegistryService["followStream"] = <A, E, R>(
309 + const followStream: EnvironmentRegistry["Service"]["followStream"] = <A, E, R>(
310 environmentId: EnvironmentId,
311 stream: Stream.Stream<A, E, R>,
312 ) =>
@@ -320,7 +325,11 @@ const makeEnvironmentRegistry = Effect.fn("EnvironmentRegistry.make")(function*
325 Effect.match({
326 onFailure: () => Stream.empty,
327 onSuccess: (supervisor) =>
323 - Stream.provideService(stream, EnvironmentSupervisor, supervisor),
328 + Stream.provideService(
329 + stream,
330 + EnvironmentSupervisor.EnvironmentSupervisor,
331 + supervisor,
332 + ),
333 }),
334 ),
335 ),
@@ -443,7 +452,6 @@ const makeEnvironmentRegistry = Effect.fn("EnvironmentRegistry.make")(function*
452 if ((yield* Ref.get(platformEnvironmentIds)).has(environmentId)) {
453 return yield* new PlatformEnvironmentRemovalError({
454 environmentId,
446 - message: "Platform-managed environments cannot be removed.",
455 });
456 }
457 const target = (yield* getEntry(environmentId)).target;
@@ -532,7 +540,7 @@ const makeEnvironmentRegistry = Effect.fn("EnvironmentRegistry.make")(function*
540 followStream(
541 environmentId,
542 Stream.unwrap(
535 - EnvironmentSupervisor.pipe(
543 + EnvironmentSupervisor.EnvironmentSupervisor.pipe(
544 Effect.map((supervisor) => SubscriptionRef.changes(supervisor.state)),
545 ),
546 ),
@@ -570,7 +578,4 @@ const makeEnvironmentRegistry = Effect.fn("EnvironmentRegistry.make")(function*
578 });
579 });
580
573 -export const environmentRegistryLayer = Layer.effect(
574 - EnvironmentRegistry,
575 - makeEnvironmentRegistry(),
576 -);
581 +export const layer = Layer.effect(EnvironmentRegistry, make);
packages/client-runtime/src/connection/resolver.test.ts
+43 -46
@@ -8,30 +8,19 @@ import * as Option from "effect/Option";
8 import * as Ref from "effect/Ref";
9 import * as Tracer from "effect/Tracer";
10
11 -import {
12 - ManagedRelayClient,
13 - ManagedRelayClientError,
14 - ManagedRelayRequestTimeoutError,
15 -} from "../relay/managedRelay.ts";
16 -import { ConnectionResolver } from "./resolver.ts";
17 -import { connectionResolverLayer } from "./resolver.ts";
18 -import {
19 - CloudSession,
20 - PrimaryEnvironmentAuth,
21 - RelayDeviceIdentity,
22 - SshEnvironmentGateway,
23 -} from "../platform/capabilities.ts";
24 -import { RemoteEnvironmentAuthorization } from "../authorization/service.ts";
11 +import * as ManagedRelay from "../relay/managedRelay.ts";
12 +import * as ConnectionResolver from "./resolver.ts";
13 +import * as ClientCapabilities from "../platform/capabilities.ts";
14 +import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
15 import {
16 BearerConnectionCredential,
17 BearerConnectionProfile,
18 type ConnectionCatalogEntry,
29 - ConnectionCredentialStore,
30 - ConnectionProfileStore,
19 SshConnectionProfile,
20 type ConnectionCredential,
21 type ConnectionProfile,
22 } from "./catalog.ts";
23 +import * as ConnectionCredentialStore from "./credentialStore.ts";
24 import {
25 BearerConnectionTarget,
26 ConnectionTransientError,
@@ -40,6 +29,7 @@ import {
29 SshConnectionTarget,
30 type ConnectionTarget,
31 } from "./model.ts";
32 +import * as ConnectionProfileStore from "./profileStore.ts";
33
34 const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
35 const ENDPOINT = {
@@ -79,8 +69,10 @@ function collectingTracer(spans: Array<string>): Tracer.Tracer {
69 });
70 }
71
82 -function relayClient(connectEnvironment: ManagedRelayClient["Service"]["connectEnvironment"]) {
83 - return ManagedRelayClient.of({
72 +function relayClient(
73 + connectEnvironment: ManagedRelay.ManagedRelayClient["Service"]["connectEnvironment"],
74 +) {
75 + return ManagedRelay.ManagedRelayClient.of({
76 relayUrl: "https://relay.example.test",
77 listEnvironments: () => unsupported("listEnvironments"),
78 listDevices: () => unsupported("listDevices"),
@@ -99,29 +91,29 @@ function relayClient(connectEnvironment: ManagedRelayClient["Service"]["connectE
91 const makeDependencies = Effect.fn("TestConnectionResolver.makeDependencies")((options?: {
92 readonly profiles?: ReadonlyArray<ConnectionProfile>;
93 readonly credentials?: ReadonlyArray<readonly [string, ConnectionCredential]>;
102 - readonly connectEnvironment?: ManagedRelayClient["Service"]["connectEnvironment"];
103 - readonly authorizeBearer?: RemoteEnvironmentAuthorization["Service"]["authorizeBearer"];
104 - readonly authorizeDpop?: RemoteEnvironmentAuthorization["Service"]["authorizeDpop"];
94 + readonly connectEnvironment?: ManagedRelay.ManagedRelayClient["Service"]["connectEnvironment"];
95 + readonly authorizeBearer?: RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization["Service"]["authorizeBearer"];
96 + readonly authorizeDpop?: RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization["Service"]["authorizeDpop"];
97 readonly primaryBearerToken?: string;
106 - readonly prepareSsh?: SshEnvironmentGateway["Service"]["prepare"];
98 + readonly prepareSsh?: ClientCapabilities.SshEnvironmentGateway["Service"]["prepare"];
99 }) => {
100 const profiles = new Map(
101 (options?.profiles ?? []).map((profile) => [profile.connectionId, profile]),
102 );
103 const credentials = new Map(options?.credentials ?? []);
104
113 - const profileStore = ConnectionProfileStore.of({
105 + const profileStore = ConnectionProfileStore.ConnectionProfileStore.of({
106 get: (connectionId) => Effect.succeed(Option.fromNullishOr(profiles.get(connectionId))),
107 put: (profile) => Effect.sync(() => void profiles.set(profile.connectionId, profile)),
108 remove: (connectionId) => Effect.sync(() => void profiles.delete(connectionId)),
109 });
118 - const credentialStore = ConnectionCredentialStore.of({
110 + const credentialStore = ConnectionCredentialStore.ConnectionCredentialStore.of({
111 get: (connectionId) => Effect.succeed(Option.fromNullishOr(credentials.get(connectionId))),
112 put: (connectionId, credential) =>
113 Effect.sync(() => void credentials.set(connectionId, credential)),
114 remove: (connectionId) => Effect.sync(() => void credentials.delete(connectionId)),
115 });
124 - const remote = RemoteEnvironmentAuthorization.of({
116 + const remote = RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization.of({
117 authorizeBearer:
118 options?.authorizeBearer ??
119 ((input) =>
@@ -151,7 +143,7 @@ const makeDependencies = Effect.fn("TestConnectionResolver.makeDependencies")((o
143 }),
144 )),
145 });
154 - const ssh = SshEnvironmentGateway.of({
146 + const ssh = ClientCapabilities.SshEnvironmentGateway.of({
147 provision: () => Effect.die("unused"),
148 prepare:
149 options?.prepareSsh ??
@@ -169,23 +161,28 @@ const makeDependencies = Effect.fn("TestConnectionResolver.makeDependencies")((o
161 });
162
163 const dependencies = Layer.mergeAll(
172 - Layer.succeed(ConnectionProfileStore, profileStore),
173 - Layer.succeed(ConnectionCredentialStore, credentialStore),
174 - Layer.succeed(CloudSession, CloudSession.of({ clerkToken: Effect.succeed("clerk-session") })),
164 + Layer.succeed(ConnectionProfileStore.ConnectionProfileStore, profileStore),
165 + Layer.succeed(ConnectionCredentialStore.ConnectionCredentialStore, credentialStore),
166 Layer.succeed(
176 - PrimaryEnvironmentAuth,
177 - PrimaryEnvironmentAuth.of({
167 + ClientCapabilities.CloudSession,
168 + ClientCapabilities.CloudSession.of({ clerkToken: Effect.succeed("clerk-session") }),
169 + ),
170 + Layer.succeed(
171 + ClientCapabilities.PrimaryEnvironmentAuth,
172 + ClientCapabilities.PrimaryEnvironmentAuth.of({
173 bearerToken: Effect.succeed(Option.fromNullishOr(options?.primaryBearerToken)),
174 }),
175 ),
176 Layer.succeed(
182 - RelayDeviceIdentity,
183 - RelayDeviceIdentity.of({ deviceId: Effect.succeed(Option.some("device-1")) }),
177 + ClientCapabilities.RelayDeviceIdentity,
178 + ClientCapabilities.RelayDeviceIdentity.of({
179 + deviceId: Effect.succeed(Option.some("device-1")),
180 + }),
181 ),
185 - Layer.succeed(RemoteEnvironmentAuthorization, remote),
186 - Layer.succeed(SshEnvironmentGateway, ssh),
182 + Layer.succeed(RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization, remote),
183 + Layer.succeed(ClientCapabilities.SshEnvironmentGateway, ssh),
184 Layer.succeed(
188 - ManagedRelayClient,
185 + ManagedRelay.ManagedRelayClient,
186 relayClient(
187 options?.connectEnvironment ??
188 ((input) =>
@@ -199,14 +196,14 @@ const makeDependencies = Effect.fn("TestConnectionResolver.makeDependencies")((o
196 ),
197 );
198
202 - return Effect.succeed(connectionResolverLayer.pipe(Layer.provide(dependencies)));
199 + return Effect.succeed(ConnectionResolver.layer.pipe(Layer.provide(dependencies)));
200 });
201
202 describe("ConnectionResolver", () => {
203 it.effect("prepares a primary environment without remote capabilities", () =>
204 Effect.gen(function* () {
205 const brokerLayer = yield* makeDependencies();
209 - const broker = yield* ConnectionResolver.pipe(Effect.provide(brokerLayer));
206 + const broker = yield* ConnectionResolver.ConnectionResolver.pipe(Effect.provide(brokerLayer));
207 const target = new PrimaryConnectionTarget({
208 environmentId: ENVIRONMENT_ID,
209 label: "Primary",
@@ -244,7 +241,7 @@ describe("ConnectionResolver", () => {
241 }),
242 ),
243 });
247 - const broker = yield* ConnectionResolver.pipe(Effect.provide(brokerLayer));
244 + const broker = yield* ConnectionResolver.ConnectionResolver.pipe(Effect.provide(brokerLayer));
245 const target = new PrimaryConnectionTarget({
246 environmentId: ENVIRONMENT_ID,
247 label: "Primary",
@@ -292,7 +289,7 @@ describe("ConnectionResolver", () => {
289 }),
290 ),
291 });
295 - const broker = yield* ConnectionResolver.pipe(Effect.provide(brokerLayer));
292 + const broker = yield* ConnectionResolver.ConnectionResolver.pipe(Effect.provide(brokerLayer));
293
294 expect(
295 (yield* broker.prepare(catalogEntry(target, Option.some(profile)))).socketUrl,
@@ -349,7 +346,7 @@ describe("ConnectionResolver", () => {
346 }),
347 ),
348 });
352 - const broker = yield* ConnectionResolver.pipe(Effect.provide(brokerLayer));
349 + const broker = yield* ConnectionResolver.ConnectionResolver.pipe(Effect.provide(brokerLayer));
350
351 expect((yield* broker.prepare(catalogEntry(target))).socketUrl).toContain("wsTicket=dpop");
352 expect(yield* Ref.get(relayInputs)).toEqual([
@@ -387,7 +384,7 @@ describe("ConnectionResolver", () => {
384 Effect.withSpan("test.remote.authorizeDpop"),
385 ),
386 });
390 - const broker = yield* ConnectionResolver.pipe(Effect.provide(brokerLayer));
387 + const broker = yield* ConnectionResolver.ConnectionResolver.pipe(Effect.provide(brokerLayer));
388
389 yield* broker
390 .prepare(catalogEntry(target))
@@ -431,7 +428,7 @@ describe("ConnectionResolver", () => {
428 }),
429 ),
430 });
434 - const broker = yield* ConnectionResolver.pipe(Effect.provide(brokerLayer));
431 + const broker = yield* ConnectionResolver.ConnectionResolver.pipe(Effect.provide(brokerLayer));
432
433 expect(
434 (yield* broker.prepare(catalogEntry(target, Option.some(profile)))).socketUrl,
@@ -449,15 +446,15 @@ describe("ConnectionResolver", () => {
446 const brokerLayer = yield* makeDependencies({
447 connectEnvironment: () =>
448 Effect.fail(
452 - new ManagedRelayClientError({
449 + new ManagedRelay.ManagedRelayClientError({
450 message: "Relay timed out.",
454 - cause: new ManagedRelayRequestTimeoutError({
451 + cause: new ManagedRelay.ManagedRelayRequestTimeoutError({
452 message: "Relay timed out.",
453 }),
454 }),
455 ),
456 });
460 - const broker = yield* ConnectionResolver.pipe(Effect.provide(brokerLayer));
457 + const broker = yield* ConnectionResolver.ConnectionResolver.pipe(Effect.provide(brokerLayer));
458 const error = yield* Effect.flip(broker.prepare(catalogEntry(target)));
459
460 expect(error).toBeInstanceOf(ConnectionTransientError);
packages/client-runtime/src/connection/resolver.ts
+45 -51
@@ -6,22 +6,16 @@ import * as Layer from "effect/Layer";
6 import * as Option from "effect/Option";
7 import * as Schema from "effect/Schema";
8
9 -import { RemoteEnvironmentAuthorization } from "../authorization/service.ts";
10 -import { ManagedRelayClient } from "../relay/managedRelay.ts";
11 -import {
12 - CloudSession,
13 - PrimaryEnvironmentAuth,
14 - RelayDeviceIdentity,
15 - SshEnvironmentGateway,
16 -} from "../platform/capabilities.ts";
9 +import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
10 +import * as ManagedRelay from "../relay/managedRelay.ts";
11 +import * as ClientCapabilities from "../platform/capabilities.ts";
12 import {
13 BearerConnectionCredential,
14 BearerConnectionProfile,
15 type ConnectionCatalogEntry,
21 - ConnectionCredentialStore,
22 - ConnectionProfileStore,
16 SshConnectionProfile,
17 } from "./catalog.ts";
18 +import * as ConnectionCredentialStore from "./credentialStore.ts";
19 import {
20 credentialMissingError,
21 environmentMismatchError,
@@ -37,6 +31,7 @@ import type {
31 SshConnectionTarget,
32 } from "./model.ts";
33 import { ConnectionBlockedError, type ConnectionAttemptError } from "./model.ts";
34 +import * as ConnectionProfileStore from "./profileStore.ts";
35
36 export class ConnectionResolver extends Context.Service<
37 ConnectionResolver,
@@ -60,8 +55,8 @@ function primarySocketUrl(target: PrimaryConnectionTarget): string {
55 }
56
57 const makePrimaryBroker = Effect.fn("clientRuntime.connection.broker.makePrimary")(function* () {
63 - const auth = yield* PrimaryEnvironmentAuth;
64 - const remote = yield* RemoteEnvironmentAuthorization;
58 + const auth = yield* ClientCapabilities.PrimaryEnvironmentAuth;
59 + const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
60
61 return Effect.fn("clientRuntime.connection.broker.primary")(function* (
62 target: PrimaryConnectionTarget,
@@ -92,8 +87,8 @@ const makePrimaryBroker = Effect.fn("clientRuntime.connection.broker.makePrimary
87 });
88
89 const makeBearerBroker = Effect.fn("clientRuntime.connection.broker.makeBearer")(function* () {
95 - const credentials = yield* ConnectionCredentialStore;
96 - const remote = yield* RemoteEnvironmentAuthorization;
90 + const credentials = yield* ConnectionCredentialStore.ConnectionCredentialStore;
91 + const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
92
93 return Effect.fn("clientRuntime.connection.broker.bearer")(function* (
94 entry: ConnectionCatalogEntry & { readonly target: BearerConnectionTarget },
@@ -106,7 +101,7 @@ const makeBearerBroker = Effect.fn("clientRuntime.connection.broker.makeBearer")
101 if (!isBearerProfile(profile)) {
102 return yield* new ConnectionBlockedError({
103 reason: "configuration",
109 - message: `Connection profile ${target.connectionId} is not a bearer connection.`,
104 + detail: `Connection profile ${target.connectionId} is not a bearer connection.`,
105 });
106 }
107 if (profile.environmentId !== target.environmentId) {
@@ -144,10 +139,10 @@ const makeBearerBroker = Effect.fn("clientRuntime.connection.broker.makeBearer")
139 });
140
141 const makeRelayBroker = Effect.fn("clientRuntime.connection.broker.makeRelay")(function* () {
147 - const relay = yield* ManagedRelayClient;
148 - const session = yield* CloudSession;
149 - const identity = yield* RelayDeviceIdentity;
150 - const remote = yield* RemoteEnvironmentAuthorization;
142 + const relay = yield* ManagedRelay.ManagedRelayClient;
143 + const session = yield* ClientCapabilities.CloudSession;
144 + const identity = yield* ClientCapabilities.RelayDeviceIdentity;
145 + const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
146
147 return Effect.fnUntraced(
148 function* (target: RelayConnectionTarget) {
@@ -192,9 +187,9 @@ const makeRelayBroker = Effect.fn("clientRuntime.connection.broker.makeRelay")(f
187 });
188
189 const makeSshBroker = Effect.fn("clientRuntime.connection.broker.makeSsh")(function* () {
195 - const profiles = yield* ConnectionProfileStore;
196 - const ssh = yield* SshEnvironmentGateway;
197 - const remote = yield* RemoteEnvironmentAuthorization;
190 + const profiles = yield* ConnectionProfileStore.ConnectionProfileStore;
191 + const ssh = yield* ClientCapabilities.SshEnvironmentGateway;
192 + const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
193
194 return Effect.fn("clientRuntime.connection.broker.ssh")(function* (
195 entry: ConnectionCatalogEntry & { readonly target: SshConnectionTarget },
@@ -207,7 +202,7 @@ const makeSshBroker = Effect.fn("clientRuntime.connection.broker.makeSsh")(funct
202 if (!isSshProfile(profile)) {
203 return yield* new ConnectionBlockedError({
204 reason: "configuration",
210 - message: `Connection profile ${target.connectionId} is not an SSH connection.`,
205 + detail: `Connection profile ${target.connectionId} is not an SSH connection.`,
206 });
207 }
208 if (profile.environmentId !== target.environmentId) {
@@ -246,34 +241,33 @@ const makeSshBroker = Effect.fn("clientRuntime.connection.broker.makeSsh")(funct
241 });
242 });
243
249 -export const connectionResolverLayer = Layer.effect(
250 - ConnectionResolver,
251 - Effect.gen(function* () {
252 - const primary = yield* makePrimaryBroker();
253 - const bearer = yield* makeBearerBroker();
254 - const relay = yield* makeRelayBroker();
255 - const ssh = yield* makeSshBroker();
244 +export const make = Effect.gen(function* () {
245 + const primary = yield* makePrimaryBroker();
246 + const bearer = yield* makeBearerBroker();
247 + const relay = yield* makeRelayBroker();
248 + const ssh = yield* makeSshBroker();
249
257 - const prepare = Effect.fn("clientRuntime.connection.broker.prepare")(function* (
258 - entry: ConnectionCatalogEntry,
259 - ) {
260 - const target: ConnectionTarget = entry.target;
261 - yield* Effect.annotateCurrentSpan({
262 - "connection.environment.id": target.environmentId,
263 - "connection.target.kind": target._tag,
264 - });
265 - switch (target._tag) {
266 - case "PrimaryConnectionTarget":
267 - return yield* primary(target);
268 - case "BearerConnectionTarget":
269 - return yield* bearer({ ...entry, target });
270 - case "RelayConnectionTarget":
271 - return yield* relay(target);
272 - case "SshConnectionTarget":
273 - return yield* ssh({ ...entry, target });
274 - }
250 + const prepare = Effect.fn("clientRuntime.connection.broker.prepare")(function* (
251 + entry: ConnectionCatalogEntry,
252 + ) {
253 + const target: ConnectionTarget = entry.target;
254 + yield* Effect.annotateCurrentSpan({
255 + "connection.environment.id": target.environmentId,
256 + "connection.target.kind": target._tag,
257 });
258 + switch (target._tag) {
259 + case "PrimaryConnectionTarget":
260 + return yield* primary(target);
261 + case "BearerConnectionTarget":
262 + return yield* bearer({ ...entry, target });
263 + case "RelayConnectionTarget":
264 + return yield* relay(target);
265 + case "SshConnectionTarget":
266 + return yield* ssh({ ...entry, target });
267 + }
268 + });
269 +
270 + return ConnectionResolver.of({ prepare });
271 +});
272
277 - return ConnectionResolver.of({ prepare });
278 - }),
279 -);
273 +export const layer = Layer.effect(ConnectionResolver, make);
packages/client-runtime/src/connection/supervisor.test.ts
+43 -44
@@ -13,12 +13,8 @@ import * as Tracer from "effect/Tracer";
13
14 import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
15 import type { ConnectionCatalogEntry } from "./catalog.ts";
16 -import { Connectivity } from "./connectivity.ts";
17 -import {
18 - ConnectionDriver,
19 - type ConnectionDriverProgress,
20 - type EnvironmentConnectionLease,
21 -} from "./driver.ts";
16 +import * as Connectivity from "./connectivity.ts";
17 +import * as ConnectionDriver from "./driver.ts";
18 import {
19 ConnectionBlockedError,
20 ConnectionTransientError,
@@ -30,9 +26,9 @@ import {
26 type PreparedConnection,
27 type SupervisorConnectionState,
28 } from "./model.ts";
33 -import type { RpcSession } from "../rpc/session.ts";
34 -import { makeEnvironmentSupervisor } from "./supervisor.ts";
35 -import { ConnectionWakeups } from "./wakeups.ts";
29 +import * as RpcSession from "../rpc/session.ts";
30 +import * as EnvironmentSupervisor from "./supervisor.ts";
31 +import * as ConnectionWakeups from "./wakeups.ts";
32
33 const TARGET = new PrimaryConnectionTarget({
34 environmentId: EnvironmentId.make("environment-1"),
@@ -70,14 +66,14 @@ const TEST_RPC_CLIENT = {} as WsRpcProtocolClient;
66 function transient(message = "Connection failed.") {
67 return new ConnectionTransientError({
68 reason: "transport",
73 - message,
69 + detail: message,
70 });
71 }
72
73 function blocked(message = "Authentication required.") {
74 return new ConnectionBlockedError({
75 reason: "authentication",
80 - message,
76 + detail: message,
77 });
78 }
79
@@ -137,7 +133,7 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
133 ReadonlyArray<Deferred.Deferred<never, ConnectionTransientError>>
134 >([]);
135
140 - const connectivity = Connectivity.of({
136 + const connectivity = Connectivity.Connectivity.of({
137 status: SubscriptionRef.get(networkStatus),
138 changes: SubscriptionRef.changes(networkStatus),
139 });
@@ -152,7 +148,7 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
148
149 const connect = Effect.fn("TestConnectionDriver.connect")(function* (
150 entry: ConnectionCatalogEntry,
155 - reportProgress: (progress: ConnectionDriverProgress) => Effect.Effect<void>,
151 + reportProgress: (progress: ConnectionDriver.ConnectionDriverProgress) => Effect.Effect<void>,
152 ) {
153 const target = entry.target;
154 yield* reportProgress({ stage: "preparing" });
@@ -170,27 +166,30 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
166 ready: options?.ready?.(attempt) ?? Effect.void,
167 probe: options?.probe?.(attempt) ?? Effect.void,
168 closed: Deferred.await(closed),
173 - } satisfies RpcSession),
169 + } satisfies RpcSession.RpcSession),
170 () => Ref.update(releaseCount, (count) => count + 1),
171 );
172
173 yield* reportProgress({ stage: "synchronizing", prepared });
174 yield* session.ready;
179 - return { prepared, session } satisfies EnvironmentConnectionLease;
175 + return { prepared, session } satisfies ConnectionDriver.EnvironmentConnectionLease;
176 });
177
178 const dependencies = Layer.mergeAll(
183 - Layer.succeed(Connectivity, connectivity),
179 + Layer.succeed(Connectivity.Connectivity, connectivity),
180 Layer.succeed(
185 - ConnectionWakeups,
186 - ConnectionWakeups.of({
181 + ConnectionWakeups.ConnectionWakeups,
182 + ConnectionWakeups.ConnectionWakeups.of({
183 changes: SubscriptionRef.changes(wakeups).pipe(
184 Stream.drop(1),
185 Stream.map((event) => event.reason),
186 ),
187 }),
188 ),
193 - Layer.succeed(ConnectionDriver, ConnectionDriver.of({ connect })),
189 + Layer.succeed(
190 + ConnectionDriver.ConnectionDriver,
191 + ConnectionDriver.ConnectionDriver.of({ connect }),
192 + ),
193 );
194
195 return {
@@ -235,7 +234,7 @@ describe("EnvironmentSupervisor", () => {
234 prepare: (attempt) =>
235 attempt === 1 ? Effect.fail(transient()) : Effect.succeed(PREPARED_CONNECTION),
236 });
238 - const supervisor = yield* makeEnvironmentSupervisor(RELAY_ENTRY, {
237 + const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
238 initiallyDesired: true,
239 }).pipe(
240 Effect.provide(harness.dependencies),
@@ -263,7 +262,7 @@ describe("EnvironmentSupervisor", () => {
262 it.effect("does not attempt a connection until it is desired", () =>
263 Effect.gen(function* () {
264 const harness = yield* makeHarness();
266 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY).pipe(
265 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY).pipe(
266 Effect.provide(harness.dependencies),
267 );
268
@@ -275,7 +274,7 @@ describe("EnvironmentSupervisor", () => {
274 it.effect("does not let the initial connect signal cancel the first attempt", () =>
275 Effect.gen(function* () {
276 const harness = yield* makeHarness();
278 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY).pipe(
277 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY).pipe(
278 Effect.provide(harness.dependencies),
279 );
280
@@ -290,7 +289,7 @@ describe("EnvironmentSupervisor", () => {
289 it.effect("waits while offline and connects immediately when the network returns", () =>
290 Effect.gen(function* () {
291 const harness = yield* makeHarness({ networkStatus: "offline" });
293 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
292 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
293 initiallyDesired: true,
294 }).pipe(Effect.provide(harness.dependencies));
295
@@ -317,7 +316,7 @@ describe("EnvironmentSupervisor", () => {
316 const harness = yield* makeHarness({
317 prepare: () => Effect.fail(transient()),
318 });
320 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
319 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
320 initiallyDesired: true,
321 }).pipe(Effect.provide(harness.dependencies));
322
@@ -345,7 +344,7 @@ describe("EnvironmentSupervisor", () => {
344 prepare: (attempt) =>
345 attempt === 1 ? Effect.fail(transient("Relay connection timed out.")) : Effect.never,
346 });
348 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
347 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
348 initiallyDesired: true,
349 }).pipe(Effect.provide(harness.dependencies));
350
@@ -378,7 +377,7 @@ describe("EnvironmentSupervisor", () => {
377 const harness = yield* makeHarness({
378 ready: () => Effect.never,
379 });
381 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
380 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
381 initiallyDesired: true,
382 }).pipe(Effect.provide(harness.dependencies));
383
@@ -410,7 +409,7 @@ describe("EnvironmentSupervisor", () => {
409 const harness = yield* makeHarness({
410 prepare: () => Effect.never,
411 });
413 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
412 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
413 initiallyDesired: true,
414 }).pipe(Effect.provide(harness.dependencies));
415
@@ -442,7 +441,7 @@ describe("EnvironmentSupervisor", () => {
441 ? Effect.die(new Error("Native transport defect."))
442 : Effect.succeed(PREPARED_CONNECTION),
443 });
445 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
444 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
445 initiallyDesired: true,
446 }).pipe(Effect.provide(harness.dependencies));
447
@@ -470,7 +469,7 @@ describe("EnvironmentSupervisor", () => {
469 prepare: (attempt) =>
470 attempt === 1 ? Effect.fail(transient()) : Effect.succeed(PREPARED_CONNECTION),
471 });
473 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
472 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
473 initiallyDesired: true,
474 }).pipe(Effect.provide(harness.dependencies));
475
@@ -488,7 +487,7 @@ describe("EnvironmentSupervisor", () => {
487 prepare: (attempt) =>
488 attempt === 1 ? Effect.fail(blocked()) : Effect.succeed(PREPARED_CONNECTION),
489 });
491 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
490 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
491 initiallyDesired: true,
492 }).pipe(Effect.provide(harness.dependencies));
493
@@ -505,7 +504,7 @@ describe("EnvironmentSupervisor", () => {
504 it.effect("releases a live session while offline and starts a new generation when online", () =>
505 Effect.gen(function* () {
506 const harness = yield* makeHarness();
508 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
507 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
508 initiallyDesired: true,
509 }).pipe(Effect.provide(harness.dependencies));
510
@@ -534,7 +533,7 @@ describe("EnvironmentSupervisor", () => {
533 prepare: (attempt) =>
534 attempt === 1 ? Effect.fail(blocked()) : Effect.succeed(PREPARED_CONNECTION),
535 });
537 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
536 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
537 initiallyDesired: true,
538 }).pipe(Effect.provide(harness.dependencies));
539
@@ -553,7 +552,7 @@ describe("EnvironmentSupervisor", () => {
552 prepare: () =>
553 Deferred.succeed(firstAttemptStarted, undefined).pipe(Effect.andThen(Effect.never)),
554 });
556 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
555 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
556 initiallyDesired: true,
557 }).pipe(Effect.provide(harness.dependencies));
558
@@ -591,7 +590,7 @@ describe("EnvironmentSupervisor", () => {
590 it.effect("treats an involuntary session close as transient and reconnects", () =>
591 Effect.gen(function* () {
592 const harness = yield* makeHarness();
594 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
593 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
594 initiallyDesired: true,
595 }).pipe(Effect.provide(harness.dependencies));
596
@@ -617,7 +616,7 @@ describe("EnvironmentSupervisor", () => {
616 it.effect("keeps escalating backoff when a newly opened session flaps", () =>
617 Effect.gen(function* () {
618 const harness = yield* makeHarness();
620 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
619 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
620 initiallyDesired: true,
621 }).pipe(Effect.provide(harness.dependencies));
622
@@ -663,7 +662,7 @@ describe("EnvironmentSupervisor", () => {
662 Effect.andThen(Deferred.succeed(probeCalled, undefined)),
663 ),
664 });
666 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
665 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
666 initiallyDesired: true,
667 }).pipe(Effect.provide(harness.dependencies));
668
@@ -684,7 +683,7 @@ describe("EnvironmentSupervisor", () => {
683 probe: (attempt) =>
684 attempt === 1 ? Effect.fail(transient("The live session is stale.")) : Effect.void,
685 });
687 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
686 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
687 initiallyDesired: true,
688 }).pipe(Effect.provide(harness.dependencies));
689
@@ -707,7 +706,7 @@ describe("EnvironmentSupervisor", () => {
706 const harness = yield* makeHarness({
707 probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
708 });
710 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
709 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
710 initiallyDesired: true,
711 }).pipe(Effect.provide(harness.dependencies));
712
@@ -732,7 +731,7 @@ describe("EnvironmentSupervisor", () => {
731 const harness = yield* makeHarness({
732 probe: () => Deferred.succeed(probeStarted, undefined).pipe(Effect.andThen(Effect.never)),
733 });
735 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
734 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
735 initiallyDesired: true,
736 }).pipe(Effect.provide(harness.dependencies));
737
@@ -749,7 +748,7 @@ describe("EnvironmentSupervisor", () => {
748 it.effect("does not churn a healthy session when credentials change", () =>
749 Effect.gen(function* () {
750 const harness = yield* makeHarness();
752 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
751 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
752 initiallyDesired: true,
753 }).pipe(Effect.provide(harness.dependencies));
754
@@ -766,7 +765,7 @@ describe("EnvironmentSupervisor", () => {
765 it.effect("releases and reconnects a relay session when credentials change", () =>
766 Effect.gen(function* () {
767 const harness = yield* makeHarness();
769 - const supervisor = yield* makeEnvironmentSupervisor(RELAY_ENTRY, {
768 + const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
769 initiallyDesired: true,
770 }).pipe(Effect.provide(harness.dependencies));
771
@@ -791,7 +790,7 @@ describe("EnvironmentSupervisor", () => {
790 ? Deferred.succeed(firstAttemptStarted, undefined).pipe(Effect.andThen(Effect.never))
791 : Effect.succeed(PREPARED_CONNECTION),
792 });
794 - const supervisor = yield* makeEnvironmentSupervisor(RELAY_ENTRY, {
793 + const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
794 initiallyDesired: true,
795 }).pipe(Effect.provide(harness.dependencies));
796
@@ -807,7 +806,7 @@ describe("EnvironmentSupervisor", () => {
806 it.effect("explicit disconnect releases the session and returns to available", () =>
807 Effect.gen(function* () {
808 const harness = yield* makeHarness();
810 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
809 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
810 initiallyDesired: true,
811 }).pipe(Effect.provide(harness.dependencies));
812
@@ -824,7 +823,7 @@ describe("EnvironmentSupervisor", () => {
823 it.effect("does not lose an explicit disconnect among concurrent wakeup signals", () =>
824 Effect.gen(function* () {
825 const harness = yield* makeHarness();
827 - const supervisor = yield* makeEnvironmentSupervisor(TARGET_ENTRY, {
826 + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
827 initiallyDesired: true,
828 }).pipe(Effect.provide(harness.dependencies));
829
packages/client-runtime/src/connection/supervisor.ts
+48 -45
@@ -15,12 +15,8 @@ import * as SubscriptionRef from "effect/SubscriptionRef";
15 import * as Tracer from "effect/Tracer";
16
17 import type { ConnectionCatalogEntry } from "./catalog.ts";
18 -import { Connectivity } from "./connectivity.ts";
19 -import {
20 - ConnectionDriver,
21 - type ConnectionDriverProgress,
22 - type EnvironmentConnectionLease,
23 -} from "./driver.ts";
18 +import * as Connectivity from "./connectivity.ts";
19 +import * as ConnectionDriver from "./driver.ts";
20 import {
21 type ConnectionAttemptError,
22 type ConnectionTarget,
@@ -29,8 +25,8 @@ import {
25 type PreparedConnection,
26 type SupervisorConnectionState,
27 } from "./model.ts";
32 -import type { RpcSession } from "../rpc/session.ts";
33 -import { type ConnectionWakeup, ConnectionWakeups } from "./wakeups.ts";
28 +import * as RpcSession from "../rpc/session.ts";
29 +import * as ConnectionWakeups from "./wakeups.ts";
30
31 const RETRY_DELAYS_MS = [1_000, 2_000, 4_000, 8_000, 16_000] as const;
32 const CONNECTION_ESTABLISHMENT_TIMEOUT = "15 seconds";
@@ -47,7 +43,7 @@ type SupervisorSignal =
43 | { readonly _tag: "DisconnectRequested" }
44 | { readonly _tag: "RetryRequested" }
45 | { readonly _tag: "NetworkChanged"; readonly network: NetworkStatus }
50 - | { readonly _tag: "Wakeup"; readonly reason: ConnectionWakeup };
46 + | { readonly _tag: "Wakeup"; readonly reason: ConnectionWakeups.ConnectionWakeup };
47
48 interface PendingRetryTrace {
49 readonly previousAttempt: Tracer.Span;
@@ -80,7 +76,7 @@ type EstablishmentEvent =
76 readonly exit: Exit.Exit<
77 {
78 readonly attemptSpan: Option.Option<Tracer.Span>;
83 - readonly lease: EnvironmentConnectionLease;
79 + readonly lease: ConnectionDriver.EnvironmentConnectionLease;
80 },
81 TracedAttemptFailure
82 >;
@@ -102,16 +98,6 @@ export interface EnvironmentSupervisorOptions {
98 readonly initiallyDesired?: boolean;
99 }
100
105 -export interface EnvironmentSupervisorService {
106 - readonly target: ConnectionTarget;
107 - readonly state: SubscriptionRef.SubscriptionRef<SupervisorConnectionState>;
108 - readonly session: SubscriptionRef.SubscriptionRef<Option.Option<RpcSession>>;
109 - readonly prepared: SubscriptionRef.SubscriptionRef<Option.Option<PreparedConnection>>;
110 - readonly connect: Effect.Effect<void>;
111 - readonly disconnect: Effect.Effect<void>;
112 - readonly retryNow: Effect.Effect<void>;
113 -}
114 -
101 function retryDelayMs(failureCount: number): number {
102 return RETRY_DELAYS_MS[Math.min(failureCount, RETRY_DELAYS_MS.length - 1)] ?? 16_000;
103 }
@@ -199,7 +185,7 @@ function failureFromExit<A>(
185 failure: {
186 error: new ConnectionTransientError({
187 reason: "transport",
202 - message: `${target.label} connection failed unexpectedly.`,
188 + detail: `${target.label} connection failed unexpectedly.`,
189 }),
190 attemptSpan: Option.none(),
191 },
@@ -208,34 +194,34 @@ function failureFromExit<A>(
194
195 export class EnvironmentSupervisor extends Context.Service<
196 EnvironmentSupervisor,
211 - EnvironmentSupervisorService
212 ->()("@t3tools/client-runtime/connection/supervisor/EnvironmentSupervisor") {
213 - static layer(
214 - entry: ConnectionCatalogEntry,
215 - options?: EnvironmentSupervisorOptions,
216 - ): Layer.Layer<
217 - EnvironmentSupervisor,
218 - never,
219 - Connectivity | ConnectionDriver | ConnectionWakeups
220 - > {
221 - return Layer.effect(EnvironmentSupervisor, makeEnvironmentSupervisor(entry, options));
197 + {
198 + readonly target: ConnectionTarget;
199 + readonly state: SubscriptionRef.SubscriptionRef<SupervisorConnectionState>;
200 + readonly session: SubscriptionRef.SubscriptionRef<Option.Option<RpcSession.RpcSession>>;
201 + readonly prepared: SubscriptionRef.SubscriptionRef<Option.Option<PreparedConnection>>;
202 + readonly connect: Effect.Effect<void>;
203 + readonly disconnect: Effect.Effect<void>;
204 + readonly retryNow: Effect.Effect<void>;
205 }
223 -}
206 +>()("@t3tools/client-runtime/connection/supervisor/EnvironmentSupervisor") {}
207
225 -export const makeEnvironmentSupervisor = Effect.fn("EnvironmentSupervisor.make")(function* (
208 +export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
209 entry: ConnectionCatalogEntry,
210 options?: EnvironmentSupervisorOptions,
211 ): Effect.fn.Return<
229 - EnvironmentSupervisorService,
212 + EnvironmentSupervisor["Service"],
213 never,
231 - Connectivity | ConnectionDriver | Scope.Scope | ConnectionWakeups
214 + | Connectivity.Connectivity
215 + | ConnectionDriver.ConnectionDriver
216 + | Scope.Scope
217 + | ConnectionWakeups.ConnectionWakeups
218 > {
219 const target = entry.target;
220 yield* annotateTarget(target);
221
236 - const connectivity = yield* Connectivity;
237 - const driver = yield* ConnectionDriver;
238 - const wakeups = yield* ConnectionWakeups;
222 + const connectivity = yield* Connectivity.Connectivity;
223 + const driver = yield* ConnectionDriver.ConnectionDriver;
224 + const wakeups = yield* ConnectionWakeups.ConnectionWakeups;
225 const initialIntent: SupervisorIntent = {
226 desired: options?.initiallyDesired ?? false,
227 network: yield* connectivity.status,
@@ -249,7 +235,7 @@ export const makeEnvironmentSupervisor = Effect.fn("EnvironmentSupervisor.make")
235 ? offlineState(initialIntent, 0, 0, null)
236 : connectingState(initialIntent, 0, 1, null),
237 );
252 - const session = yield* SubscriptionRef.make<Option.Option<RpcSession>>(Option.none());
238 + const session = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(Option.none());
239 const prepared = yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(Option.none());
240
241 const clearLease = Effect.all(
@@ -280,7 +266,7 @@ export const makeEnvironmentSupervisor = Effect.fn("EnvironmentSupervisor.make")
266 attempt: number,
267 generation: number,
268 lastFailure: ConnectionAttemptError | null,
283 - progress: ConnectionDriverProgress,
269 + progress: ConnectionDriver.ConnectionDriverProgress,
270 ) {
271 if ("prepared" in progress) {
272 yield* SubscriptionRef.set(prepared, Option.some(progress.prepared));
@@ -301,7 +287,11 @@ export const makeEnvironmentSupervisor = Effect.fn("EnvironmentSupervisor.make")
287 });
288
289 const traceRelayEstablishment = (
304 - effect: Effect.Effect<EnvironmentConnectionLease, ConnectionAttemptError, Scope.Scope>,
290 + effect: Effect.Effect<
291 + ConnectionDriver.EnvironmentConnectionLease,
292 + ConnectionAttemptError,
293 + Scope.Scope
294 + >,
295 attempt: number,
296 generation: number,
297 pendingRetry: Option.Option<PendingRetryTrace>,
@@ -392,7 +382,9 @@ export const makeEnvironmentSupervisor = Effect.fn("EnvironmentSupervisor.make")
382 }
383 });
384
395 - const monitorConnectedLease = Effect.fnUntraced(function* (lease: EnvironmentConnectionLease) {
385 + const monitorConnectedLease = Effect.fnUntraced(function* (
386 + lease: ConnectionDriver.EnvironmentConnectionLease,
387 + ) {
388 for (;;) {
389 const next = yield* Queue.take(signals);
390 switch (next._tag) {
@@ -417,7 +409,7 @@ export const makeEnvironmentSupervisor = Effect.fn("EnvironmentSupervisor.make")
409 Effect.fail(
410 new ConnectionTransientError({
411 reason: "timeout",
420 - message: `${target.label} did not respond to a connection health check.`,
412 + detail: `${target.label} did not respond to a connection health check.`,
413 }),
414 ),
415 }),
@@ -499,7 +491,7 @@ export const makeEnvironmentSupervisor = Effect.fn("EnvironmentSupervisor.make")
491 failure: {
492 error: new ConnectionTransientError({
493 reason: "timeout",
502 - message: `${target.label} did not respond during connection setup.`,
494 + detail: `${target.label} did not respond during connection setup.`,
495 }),
496 attemptSpan: Option.none(),
497 },
@@ -722,3 +714,14 @@ export const makeEnvironmentSupervisor = Effect.fn("EnvironmentSupervisor.make")
714 retryNow,
715 });
716 });
717 +
718 +export const layer = (
719 + entry: ConnectionCatalogEntry,
720 + options?: EnvironmentSupervisorOptions,
721 +): Layer.Layer<
722 + EnvironmentSupervisor,
723 + never,
724 + | Connectivity.Connectivity
725 + | ConnectionDriver.ConnectionDriver
726 + | ConnectionWakeups.ConnectionWakeups
727 +> => Layer.effect(EnvironmentSupervisor, make(entry, options));
packages/client-runtime/src/connection/wakeups.ts
+6
@@ -1,4 +1,5 @@
1 import * as Context from "effect/Context";
2 +import * as Layer from "effect/Layer";
3 import type * as Stream from "effect/Stream";
4
5 export type ConnectionWakeup = "application-active" | "credentials-changed";
@@ -9,3 +10,8 @@ export class ConnectionWakeups extends Context.Service<
10 readonly changes: Stream.Stream<ConnectionWakeup>;
11 }
12 >()("@t3tools/client-runtime/connection/wakeups/ConnectionWakeups") {}
13 +
14 +export const make = (service: ConnectionWakeups["Service"]) => ConnectionWakeups.of(service);
15 +
16 +export const layer = (service: ConnectionWakeups["Service"]) =>
17 + Layer.succeed(ConnectionWakeups, make(service));
packages/client-runtime/src/operations/commands.test.ts
+8 -11
@@ -18,11 +18,8 @@ import {
18 PrimaryConnectionTarget,
19 type PreparedConnection,
20 } from "../connection/model.ts";
21 -import {
22 - EnvironmentSupervisor,
23 - type EnvironmentSupervisorService,
24 -} from "../connection/supervisor.ts";
25 -import type { RpcSession } from "../rpc/session.ts";
21 +import * as EnvironmentSupervisor from "../connection/supervisor.ts";
22 +import * as RpcSession from "../rpc/session.ts";
23 import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
24 import { archiveThread, createProject, stopThreadSession } from "./commands.ts";
25
@@ -51,14 +48,14 @@ const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(funct
48 return { sequence: dispatched.length };
49 }),
50 } as unknown as WsRpcProtocolClient;
54 - const session: RpcSession = {
51 + const session: RpcSession.RpcSession = {
52 client,
53 initialConfig: Effect.never,
54 ready: Effect.void,
55 probe: Effect.void,
56 closed: Effect.never,
57 };
61 - return EnvironmentSupervisor.of({
58 + return EnvironmentSupervisor.EnvironmentSupervisor.of({
59 target: TARGET,
60 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
61 session: yield* SubscriptionRef.make(Option.some(session)),
@@ -66,7 +63,7 @@ const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(funct
63 connect: Effect.void,
64 disconnect: Effect.void,
65 retryNow: Effect.void,
69 - } satisfies EnvironmentSupervisorService);
66 + } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
67 });
68
69 describe("environment commands", () => {
@@ -80,7 +77,7 @@ describe("environment commands", () => {
77 title: "Project",
78 workspaceRoot: "/workspace/project",
79 createdAt: "2026-06-06T00:00:00.000Z",
83 - }).pipe(Effect.provideService(EnvironmentSupervisor, supervisor));
80 + }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
81
82 expect(result).toEqual({ sequence: 1 });
83 expect(dispatched).toEqual([
@@ -105,7 +102,7 @@ describe("environment commands", () => {
102 commandId: CommandId.make("queued-command"),
103 threadId: ThreadId.make("thread-1"),
104 createdAt: "2026-06-06T00:01:00.000Z",
108 - }).pipe(Effect.provideService(EnvironmentSupervisor, supervisor));
105 + }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
106
107 expect(dispatched).toEqual([
108 {
@@ -126,7 +123,7 @@ describe("environment commands", () => {
123 yield* archiveThread({
124 commandId: CommandId.make("archive-command"),
125 threadId: ThreadId.make("thread-1"),
129 - }).pipe(Effect.provideService(EnvironmentSupervisor, supervisor));
126 + }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
127
128 expect(dispatched).toEqual([
129 {
packages/client-runtime/src/platform/storageDocument.test.ts
+2 -2
@@ -1,7 +1,7 @@
1 import { EnvironmentId } from "@t3tools/contracts";
2 import { describe, expect, it } from "@effect/vitest";
3
4 -import { RemoteDpopAccessToken } from "../authorization/tokenStore.ts";
4 +import * as TokenStore from "../authorization/tokenStore.ts";
5 import {
6 BearerConnectionCredential,
7 BearerConnectionProfile,
@@ -38,7 +38,7 @@ const BEARER_PROFILE = new BearerConnectionProfile({
38 const BEARER_CREDENTIAL = new BearerConnectionCredential({
39 token: "bearer-token",
40 });
41 -const REMOTE_TOKEN = new RemoteDpopAccessToken({
41 +const REMOTE_TOKEN = new TokenStore.RemoteDpopAccessToken({
42 environmentId: ENVIRONMENT_ID,
43 label: "Remote",
44 endpoint: {
packages/client-runtime/src/platform/storageDocument.ts
+2 -2
@@ -6,7 +6,7 @@ import {
6 ConnectionProfile,
7 } from "../connection/catalog.ts";
8 import { type ConnectionTarget, PersistedConnectionTarget } from "../connection/model.ts";
9 -import { RemoteDpopAccessToken } from "../authorization/tokenStore.ts";
9 +import * as TokenStore from "../authorization/tokenStore.ts";
10
11 export const StoredConnectionCredential = Schema.Struct({
12 connectionId: Schema.String,
@@ -19,7 +19,7 @@ export const ConnectionCatalogDocument = Schema.Struct({
19 targets: Schema.Array(PersistedConnectionTarget),
20 profiles: Schema.Array(ConnectionProfile),
21 credentials: Schema.Array(StoredConnectionCredential),
22 - remoteDpopTokens: Schema.Array(RemoteDpopAccessToken),
22 + remoteDpopTokens: Schema.Array(TokenStore.RemoteDpopAccessToken),
23 });
24 export type ConnectionCatalogDocument = typeof ConnectionCatalogDocument.Type;
25
packages/client-runtime/src/relay/discovery.test.ts
+43 -39
@@ -13,17 +13,12 @@ import * as Ref from "effect/Ref";
13 import * as Stream from "effect/Stream";
14 import * as SubscriptionRef from "effect/SubscriptionRef";
15
16 -import {
17 - ManagedRelayClient,
18 - ManagedRelayClientError,
19 - ManagedRelayRequestTimeoutError,
20 - type ManagedRelayClientShape,
21 -} from "./managedRelay.ts";
22 -import { CloudSession } from "../platform/capabilities.ts";
23 -import { Connectivity } from "../connection/connectivity.ts";
16 +import * as ManagedRelay from "./managedRelay.ts";
17 +import * as ClientCapabilities from "../platform/capabilities.ts";
18 +import * as Connectivity from "../connection/connectivity.ts";
19 import { ConnectionBlockedError, type NetworkStatus } from "../connection/model.ts";
25 -import { ConnectionWakeups } from "../connection/wakeups.ts";
26 -import { RelayEnvironmentDiscovery, relayEnvironmentDiscoveryLayer } from "./discovery.ts";
20 +import * as ConnectionWakeups from "../connection/wakeups.ts";
21 +import * as RelayEnvironmentDiscovery from "./discovery.ts";
22
23 const environments = [
24 {
@@ -63,7 +58,7 @@ function status(
58 const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
59 const networkStatus = yield* SubscriptionRef.make<NetworkStatus>("online");
60 const listCalls = yield* Ref.make(0);
66 - const listFailure = yield* Ref.make<ManagedRelayClientError | null>(null);
61 + const listFailure = yield* Ref.make<ManagedRelay.ManagedRelayClientError | null>(null);
62 const secondListCall = yield* Deferred.make<void>();
63 const clerkToken = yield* Ref.make<string | null>("clerk-token");
64 const wakeups = yield* SubscriptionRef.make<{
@@ -74,10 +69,16 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
69 reason: "application-active",
70 });
71 const statusRequests = yield* Ref.make(
77 - new Map<string, Deferred.Deferred<RelayEnvironmentStatusResponse, ManagedRelayClientError>>(),
72 + new Map<
73 + string,
74 + Deferred.Deferred<RelayEnvironmentStatusResponse, ManagedRelay.ManagedRelayClientError>
75 + >(),
76 );
77 for (const environment of environments) {
80 - const request = yield* Deferred.make<RelayEnvironmentStatusResponse, ManagedRelayClientError>();
78 + const request = yield* Deferred.make<
79 + RelayEnvironmentStatusResponse,
80 + ManagedRelay.ManagedRelayClientError
81 + >();
82 yield* Ref.update(statusRequests, (current) => {
83 const next = new Map(current);
84 next.set(environment.environmentId, request);
@@ -85,7 +86,7 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
86 });
87 }
88
88 - const client = ManagedRelayClient.of({
89 + const client = ManagedRelay.ManagedRelayClient.of({
90 relayUrl: "https://relay.example.test",
91 listEnvironments: () =>
92 Effect.gen(function* () {
@@ -112,25 +113,25 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
113 unregisterDevice: () => Effect.die("unused"),
114 registerLiveActivity: () => Effect.die("unused"),
115 resetTokenCache: Effect.void,
115 - } satisfies ManagedRelayClientShape);
116 - const connectivity = Connectivity.of({
116 + } satisfies ManagedRelay.ManagedRelayClient["Service"]);
117 + const connectivity = Connectivity.Connectivity.of({
118 status: SubscriptionRef.get(networkStatus),
119 changes: SubscriptionRef.changes(networkStatus),
120 });
120 - const layer = relayEnvironmentDiscoveryLayer.pipe(
121 + const layer = RelayEnvironmentDiscovery.layer.pipe(
122 Layer.provide(
123 Layer.mergeAll(
123 - Layer.succeed(ManagedRelayClient, client),
124 + Layer.succeed(ManagedRelay.ManagedRelayClient, client),
125 Layer.succeed(
125 - CloudSession,
126 - CloudSession.of({
126 + ClientCapabilities.CloudSession,
127 + ClientCapabilities.CloudSession.of({
128 clerkToken: Ref.get(clerkToken).pipe(
129 Effect.flatMap((token) =>
130 token === null
131 ? Effect.fail(
132 new ConnectionBlockedError({
133 reason: "authentication",
133 - message: "Signed out.",
134 + detail: "Signed out.",
135 }),
136 )
137 : Effect.succeed(token),
@@ -138,10 +139,10 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
139 ),
140 }),
141 ),
141 - Layer.succeed(Connectivity, connectivity),
142 + Layer.succeed(Connectivity.Connectivity, connectivity),
143 Layer.succeed(
143 - ConnectionWakeups,
144 - ConnectionWakeups.of({
144 + ConnectionWakeups.ConnectionWakeups,
145 + ConnectionWakeups.ConnectionWakeups.of({
146 changes: SubscriptionRef.changes(wakeups).pipe(
147 Stream.drop(1),
148 Stream.map((event) => event.reason),
@@ -173,7 +174,7 @@ describe("RelayEnvironmentDiscovery", () => {
174 Effect.gen(function* () {
175 const harness = yield* makeHarness();
176 yield* Effect.gen(function* () {
176 - const discovery = yield* RelayEnvironmentDiscovery;
177 + const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
178 const refreshFiber = yield* Effect.forkChild(discovery.refresh);
179
180 const checking = yield* SubscriptionRef.changes(discovery.state).pipe(
@@ -224,7 +225,7 @@ describe("RelayEnvironmentDiscovery", () => {
225 Effect.gen(function* () {
226 const harness = yield* makeHarness();
227 yield* Effect.gen(function* () {
227 - const discovery = yield* RelayEnvironmentDiscovery;
228 + const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
229 const requests = yield* Ref.get(harness.statusRequests);
230 for (const environment of environments) {
231 yield* Deferred.succeed(
@@ -253,13 +254,13 @@ describe("RelayEnvironmentDiscovery", () => {
254 it.effect("publishes listing failures without rejecting the refresh command", () =>
255 Effect.gen(function* () {
256 const networkStatus = yield* SubscriptionRef.make<NetworkStatus>("online");
256 - const client = ManagedRelayClient.of({
257 + const client = ManagedRelay.ManagedRelayClient.of({
258 relayUrl: "https://relay.example.test",
259 listEnvironments: () =>
260 Effect.fail(
260 - new ManagedRelayClientError({
261 + new ManagedRelay.ManagedRelayClientError({
262 message: "Relay environment listing timed out.",
262 - cause: new ManagedRelayRequestTimeoutError({
263 + cause: new ManagedRelay.ManagedRelayRequestTimeoutError({
264 message: "Relay environment listing timed out.",
265 }),
266 }),
@@ -274,25 +275,28 @@ describe("RelayEnvironmentDiscovery", () => {
275 unregisterDevice: () => Effect.die("unused"),
276 registerLiveActivity: () => Effect.die("unused"),
277 resetTokenCache: Effect.void,
277 - } satisfies ManagedRelayClientShape);
278 - const layer = relayEnvironmentDiscoveryLayer.pipe(
278 + } satisfies ManagedRelay.ManagedRelayClient["Service"]);
279 + const layer = RelayEnvironmentDiscovery.layer.pipe(
280 Layer.provide(
281 Layer.mergeAll(
281 - Layer.succeed(ManagedRelayClient, client),
282 - Layer.succeed(CloudSession, {
282 + Layer.succeed(ManagedRelay.ManagedRelayClient, client),
283 + Layer.succeed(ClientCapabilities.CloudSession, {
284 clerkToken: Effect.succeed("clerk-token"),
285 }),
285 - Layer.succeed(Connectivity, {
286 + Layer.succeed(Connectivity.Connectivity, {
287 status: SubscriptionRef.get(networkStatus),
288 changes: SubscriptionRef.changes(networkStatus),
289 }),
289 - Layer.succeed(ConnectionWakeups, ConnectionWakeups.of({ changes: Stream.never })),
290 + Layer.succeed(
291 + ConnectionWakeups.ConnectionWakeups,
292 + ConnectionWakeups.ConnectionWakeups.of({ changes: Stream.never }),
293 + ),
294 ),
295 ),
296 );
297
298 yield* Effect.gen(function* () {
295 - const discovery = yield* RelayEnvironmentDiscovery;
299 + const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
300 yield* discovery.refresh;
301
302 const state = yield* SubscriptionRef.get(discovery.state);
@@ -310,7 +314,7 @@ describe("RelayEnvironmentDiscovery", () => {
314 Effect.gen(function* () {
315 const harness = yield* makeHarness();
316 yield* Effect.gen(function* () {
313 - const discovery = yield* RelayEnvironmentDiscovery;
317 + const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
318 const requests = yield* Ref.get(harness.statusRequests);
319 for (const environment of environments) {
320 yield* Deferred.succeed(
@@ -323,7 +327,7 @@ describe("RelayEnvironmentDiscovery", () => {
327
328 yield* Ref.set(
329 harness.listFailure,
326 - new ManagedRelayClientError({
330 + new ManagedRelay.ManagedRelayClientError({
331 message: "Relay environment listing failed.",
332 }),
333 );
@@ -340,7 +344,7 @@ describe("RelayEnvironmentDiscovery", () => {
344 Effect.gen(function* () {
345 const harness = yield* makeHarness();
346 yield* Effect.gen(function* () {
343 - const discovery = yield* RelayEnvironmentDiscovery;
347 + const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
348 const refreshFiber = yield* Effect.forkChild(discovery.refresh);
349 yield* SubscriptionRef.changes(discovery.state).pipe(
350 Stream.filter((state) => state.environments.size === environments.length),
packages/client-runtime/src/relay/discovery.ts
+17 -22
@@ -16,12 +16,12 @@ import * as Semaphore from "effect/Semaphore";
16 import * as Stream from "effect/Stream";
17 import * as SubscriptionRef from "effect/SubscriptionRef";
18
19 -import { ManagedRelayClient } from "./managedRelay.ts";
20 -import { CloudSession } from "../platform/capabilities.ts";
21 -import { Connectivity } from "../connection/connectivity.ts";
19 +import * as ManagedRelay from "./managedRelay.ts";
20 +import * as ClientCapabilities from "../platform/capabilities.ts";
21 +import * as Connectivity from "../connection/connectivity.ts";
22 import { mapManagedRelayError } from "../connection/errors.ts";
23 import { ConnectionBlockedError, type ConnectionAttemptError } from "../connection/model.ts";
24 -import { ConnectionWakeups } from "../connection/wakeups.ts";
24 +import * as ConnectionWakeups from "../connection/wakeups.ts";
25
26 export type RelayEnvironmentAvailability = "checking" | "online" | "offline" | "error";
27
@@ -39,14 +39,12 @@ export interface RelayEnvironmentDiscoveryState {
39 readonly error: Option.Option<ConnectionAttemptError>;
40 }
41
42 -export interface RelayEnvironmentDiscoveryService {
43 - readonly state: SubscriptionRef.SubscriptionRef<RelayEnvironmentDiscoveryState>;
44 - readonly refresh: Effect.Effect<void>;
45 -}
46 -
42 export class RelayEnvironmentDiscovery extends Context.Service<
43 RelayEnvironmentDiscovery,
49 - RelayEnvironmentDiscoveryService
44 + {
45 + readonly state: SubscriptionRef.SubscriptionRef<RelayEnvironmentDiscoveryState>;
46 + readonly refresh: Effect.Effect<void>;
47 + }
48 >()("@t3tools/client-runtime/relay/discovery/RelayEnvironmentDiscovery") {}
49
50 export const EMPTY_RELAY_ENVIRONMENT_DISCOVERY_STATE: RelayEnvironmentDiscoveryState = {
@@ -64,7 +62,7 @@ function validateStatus(
62 return Effect.fail(
63 new ConnectionBlockedError({
64 reason: "configuration",
67 - message: "Relay returned status for a different environment.",
65 + detail: "Relay returned status for a different environment.",
66 }),
67 );
68 }
@@ -76,7 +74,7 @@ function validateStatus(
74 return Effect.fail(
75 new ConnectionBlockedError({
76 reason: "configuration",
79 - message: "Relay returned status for a different environment endpoint.",
77 + detail: "Relay returned status for a different environment endpoint.",
78 }),
79 );
80 }
@@ -87,7 +85,7 @@ function validateStatus(
85 return Effect.fail(
86 new ConnectionBlockedError({
87 reason: "configuration",
90 - message: "Relay returned a descriptor for a different environment.",
88 + detail: "Relay returned a descriptor for a different environment.",
89 }),
90 );
91 }
@@ -104,11 +102,11 @@ function relayAccountId(clerkToken: string): Option.Option<string> {
102 }
103 }
104
107 -const makeRelayEnvironmentDiscovery = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
108 - const relay = yield* ManagedRelayClient;
109 - const session = yield* CloudSession;
110 - const connectivity = yield* Connectivity;
111 - const wakeups = yield* ConnectionWakeups;
105 +export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
106 + const relay = yield* ManagedRelay.ManagedRelayClient;
107 + const session = yield* ClientCapabilities.CloudSession;
108 + const connectivity = yield* Connectivity.Connectivity;
109 + const wakeups = yield* ConnectionWakeups.ConnectionWakeups;
110 const state = yield* SubscriptionRef.make(EMPTY_RELAY_ENVIRONMENT_DISCOVERY_STATE);
111 const refreshLock = yield* Semaphore.make(1);
112 const hasRefreshed = yield* Ref.make(false);
@@ -327,7 +325,4 @@ const makeRelayEnvironmentDiscovery = Effect.fn("RelayEnvironmentDiscovery.make"
325 return RelayEnvironmentDiscovery.of({ state, refresh });
326 });
327
330 -export const relayEnvironmentDiscoveryLayer = Layer.effect(
331 - RelayEnvironmentDiscovery,
332 - makeRelayEnvironmentDiscovery(),
333 -);
328 +export const layer = Layer.effect(RelayEnvironmentDiscovery, make());
packages/client-runtime/src/rpc/client.test.ts
+16 -17
@@ -22,11 +22,8 @@ import {
22 type PreparedConnection,
23 type SupervisorConnectionState,
24 } from "../connection/model.ts";
25 -import {
26 - EnvironmentSupervisor,
27 - type EnvironmentSupervisorService,
28 -} from "../connection/supervisor.ts";
29 -import type { RpcSession } from "../rpc/session.ts";
25 +import * as EnvironmentSupervisor from "../connection/supervisor.ts";
26 +import * as RpcSession from "../rpc/session.ts";
27 import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
28 import { EnvironmentRpcRequestObserver, request, runStream, subscribe } from "./client.ts";
29
@@ -46,7 +43,7 @@ const INSTALL_DOWNLOADING: RelayClientInstallProgressEvent = {
43 stage: "downloading",
44 };
45
49 -function session(client: WsRpcProtocolClient): RpcSession {
46 +function session(client: WsRpcProtocolClient): RpcSession.RpcSession {
47 return {
48 client,
49 initialConfig: Effect.never,
@@ -58,10 +55,12 @@ function session(client: WsRpcProtocolClient): RpcSession {
55
56 const makeHarness = Effect.fn("TestEnvironmentRpc.makeHarness")(function* () {
57 const state = yield* SubscriptionRef.make<SupervisorConnectionState>(AVAILABLE_CONNECTION_STATE);
61 - const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession>>(Option.none());
58 + const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
59 + Option.none(),
60 + );
61 const prepared = yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(Option.none());
62 const retryCount = yield* Ref.make(0);
64 - const supervisor = EnvironmentSupervisor.of({
63 + const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
64 target: TARGET,
65 state,
66 session: activeSession,
@@ -69,7 +68,7 @@ const makeHarness = Effect.fn("TestEnvironmentRpc.makeHarness")(function* () {
68 connect: Effect.void,
69 disconnect: Effect.void,
70 retryNow: Ref.update(retryCount, (count) => count + 1),
72 - } satisfies EnvironmentSupervisorService);
71 + } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
72 return {
73 activeSession,
74 retryCount,
@@ -89,7 +88,7 @@ describe("environment RPC", () => {
88 yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
89
90 const result = yield* request(WS_METHODS.cloudGetRelayClientStatus, {}).pipe(
92 - Effect.provideService(EnvironmentSupervisor, supervisor),
91 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
92 Effect.provideService(
93 EnvironmentRpcRequestObserver,
94 EnvironmentRpcRequestObserver.of({
@@ -128,7 +127,7 @@ describe("environment RPC", () => {
127 const resultFiber = yield* runStream(WS_METHODS.cloudInstallRelayClient, {}).pipe(
128 Stream.take(2),
129 Stream.runCollect,
131 - Effect.provideService(EnvironmentSupervisor, supervisor),
130 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
131 Effect.forkChild,
132 );
133 yield* Effect.yieldNow;
@@ -172,7 +171,7 @@ describe("environment RPC", () => {
171
172 const subscriptionFiber = yield* subscribe(WS_METHODS.subscribeTerminalEvents, {}).pipe(
173 Stream.runDrain,
175 - Effect.provideService(EnvironmentSupervisor, supervisor),
174 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
175 Effect.forkChild,
176 );
177 yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient)));
@@ -212,7 +211,7 @@ describe("environment RPC", () => {
211
212 const subscriptionFiber = yield* subscribe(WS_METHODS.subscribeTerminalEvents, {}).pipe(
213 Stream.runDrain,
215 - Effect.provideService(EnvironmentSupervisor, supervisor),
214 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
215 Effect.forkChild,
216 );
217 yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient)));
@@ -243,7 +242,7 @@ describe("environment RPC", () => {
242 yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
243 const error = yield* subscribe(WS_METHODS.subscribeTerminalEvents, {}).pipe(
244 Stream.runDrain,
246 - Effect.provideService(EnvironmentSupervisor, supervisor),
245 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
246 Effect.flip,
247 );
248
@@ -283,7 +282,7 @@ describe("environment RPC", () => {
282 },
283 ).pipe(
284 Stream.runDrain,
286 - Effect.provideService(EnvironmentSupervisor, supervisor),
285 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
286 Effect.forkChild,
287 );
288 for (let attempt = 0; attempt < 100 && observedFailures.length < 1; attempt += 1) {
@@ -329,7 +328,7 @@ describe("environment RPC", () => {
328 },
329 ).pipe(
330 Stream.runDrain,
332 - Effect.provideService(EnvironmentSupervisor, supervisor),
331 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
332 Effect.forkChild,
333 );
334 for (let attempt = 0; attempt < 100; attempt += 1) {
@@ -377,7 +376,7 @@ describe("environment RPC", () => {
376 },
377 ).pipe(
378 Stream.runDrain,
380 - Effect.provideService(EnvironmentSupervisor, supervisor),
379 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
380 Effect.exit,
381 );
382
packages/client-runtime/src/rpc/index.ts
+1 -1
@@ -1,4 +1,4 @@
1 export * from "./client.ts";
2 export * from "./http.ts";
3 export * from "./protocol.ts";
4 -export * from "./session.ts";
4 +export { type RpcSession, RpcSessionFactory } from "./session.ts";
packages/client-runtime/src/rpc/session.test.ts
+3 -3
@@ -18,7 +18,7 @@ import {
18 PrimaryConnectionTarget,
19 type PreparedConnection,
20 } from "../connection/model.ts";
21 -import { RpcSessionFactory, rpcSessionFactoryLayer } from "./session.ts";
21 +import * as RpcSession from "./session.ts";
22
23 type SocketEventType = "open" | "message" | "close" | "error";
24 type SocketEvent = {
@@ -149,8 +149,8 @@ const makeFactory = Effect.fn("TestRpcSessionFactory.make")(function* () {
149 sockets.push(socket);
150 return socket as unknown as globalThis.WebSocket;
151 });
152 - const layer = rpcSessionFactoryLayer.pipe(Layer.provide(constructorLayer));
153 - const factory = yield* RpcSessionFactory.pipe(Effect.provide(layer));
152 + const layer = RpcSession.layer.pipe(Layer.provide(constructorLayer));
153 + const factory = yield* RpcSession.RpcSessionFactory.pipe(Effect.provide(layer));
154 return { factory, sockets };
155 });
156
packages/client-runtime/src/rpc/session.ts
+74 -74
@@ -5,7 +5,8 @@ import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Schedule from "effect/Schedule";
7 import type * as Scope from "effect/Scope";
8 -import { RpcClient, RpcSerialization } from "effect/unstable/rpc";
8 +import * as RpcClient from "effect/unstable/rpc/RpcClient";
9 +import * as RpcSerialization from "effect/unstable/rpc/RpcSerialization";
10 import * as Socket from "effect/unstable/socket/Socket";
11
12 import { makeWsRpcProtocolClient, type WsRpcProtocolClient } from "./protocol.ts";
@@ -47,98 +48,97 @@ function mapInitialConfigError(error: InitialConfigError): ConnectionAttemptErro
48 case "EnvironmentAuthorizationError":
49 return new ConnectionBlockedError({
50 reason: "permission",
50 - message: error.message,
51 + detail: error.message,
52 });
53 case "KeybindingsConfigParseError":
54 case "ServerSettingsError":
55 return new ConnectionTransientErrorClass({
56 reason: "remote-unavailable",
56 - message: error.message,
57 + detail: error.message,
58 });
59 case "RpcClientError":
60 return new ConnectionTransientErrorClass({
61 reason: "transport",
61 - message: error.message,
62 + detail: error.message,
63 });
64 }
65 }
66
66 -export const rpcSessionFactoryLayer = Layer.effect(
67 - RpcSessionFactory,
68 - Effect.gen(function* () {
69 - const webSocketConstructor = yield* Socket.WebSocketConstructor;
67 +export const make = Effect.gen(function* () {
68 + const webSocketConstructor = yield* Socket.WebSocketConstructor;
69
71 - const connect = Effect.fnUntraced(function* (connection: PreparedConnection) {
72 - yield* Effect.annotateCurrentSpan({
73 - "connection.environment.id": connection.environmentId,
74 - });
70 + const connect = Effect.fnUntraced(function* (connection: PreparedConnection) {
71 + yield* Effect.annotateCurrentSpan({
72 + "connection.environment.id": connection.environmentId,
73 + });
74
76 - const connected = yield* Deferred.make<void>();
77 - const disconnected = yield* Deferred.make<never, ConnectionTransientError>();
78 - const hooks = RpcClient.ConnectionHooks.of({
79 - onConnect: Deferred.succeed(connected, undefined).pipe(Effect.asVoid),
80 - onDisconnect: Deferred.isDone(connected).pipe(
81 - Effect.flatMap((wasConnected) =>
82 - Deferred.fail(
83 - disconnected,
84 - new ConnectionTransientErrorClass({
85 - reason: "transport",
86 - message: wasConnected
87 - ? `${connection.label} disconnected.`
88 - : `${connection.label} could not establish a WebSocket connection.`,
89 - }),
90 - ),
91 - ),
92 - Effect.asVoid,
93 - ),
94 - });
95 - const socketLayer = Socket.layerWebSocket(connection.socketUrl, {
96 - openTimeout: SOCKET_OPEN_TIMEOUT,
97 - }).pipe(Layer.provide(Layer.succeed(Socket.WebSocketConstructor, webSocketConstructor)));
98 - const protocolLayer = Layer.effect(
99 - RpcClient.Protocol,
100 - RpcClient.makeProtocolSocket({
101 - retryTransientErrors: false,
102 - retryPolicy: Schedule.recurs(0),
103 - }),
104 - ).pipe(
105 - Layer.provide(
106 - Layer.mergeAll(
107 - socketLayer,
108 - RpcSerialization.layerJson,
109 - Layer.succeed(RpcClient.ConnectionHooks, hooks),
75 + const connected = yield* Deferred.make<void>();
76 + const disconnected = yield* Deferred.make<never, ConnectionTransientError>();
77 + const hooks = RpcClient.ConnectionHooks.of({
78 + onConnect: Deferred.succeed(connected, undefined).pipe(Effect.asVoid),
79 + onDisconnect: Deferred.isDone(connected).pipe(
80 + Effect.flatMap((wasConnected) =>
81 + Deferred.fail(
82 + disconnected,
83 + new ConnectionTransientErrorClass({
84 + reason: "transport",
85 + detail: wasConnected
86 + ? `${connection.label} disconnected.`
87 + : `${connection.label} could not establish a WebSocket connection.`,
88 + }),
89 ),
90 ),
112 - );
113 - const protocolContext = yield* Layer.build(protocolLayer).pipe(
114 - Effect.withSpan("environment.websocket.connect"),
115 - );
116 - const client = yield* makeWsRpcProtocolClient.pipe(Effect.provide(protocolContext));
117 - const initialConfig = yield* Effect.cached(
118 - client[WS_METHODS.serverGetConfig]({}).pipe(
119 - Effect.mapError(mapInitialConfigError),
120 - Effect.withSpan("environment.initialSync"),
91 + Effect.asVoid,
92 + ),
93 + });
94 + const socketLayer = Socket.layerWebSocket(connection.socketUrl, {
95 + openTimeout: SOCKET_OPEN_TIMEOUT,
96 + }).pipe(Layer.provide(Layer.succeed(Socket.WebSocketConstructor, webSocketConstructor)));
97 + const protocolLayer = Layer.effect(
98 + RpcClient.Protocol,
99 + RpcClient.makeProtocolSocket({
100 + retryTransientErrors: false,
101 + retryPolicy: Schedule.recurs(0),
102 + }),
103 + ).pipe(
104 + Layer.provide(
105 + Layer.mergeAll(
106 + socketLayer,
107 + RpcSerialization.layerJson,
108 + Layer.succeed(RpcClient.ConnectionHooks, hooks),
109 ),
122 - );
123 - const probe = client[WS_METHODS.serverGetConfig]({}).pipe(
110 + ),
111 + );
112 + const protocolContext = yield* Layer.build(protocolLayer).pipe(
113 + Effect.withSpan("environment.websocket.connect"),
114 + );
115 + const client = yield* makeWsRpcProtocolClient.pipe(Effect.provide(protocolContext));
116 + const initialConfig = yield* Effect.cached(
117 + client[WS_METHODS.serverGetConfig]({}).pipe(
118 Effect.mapError(mapInitialConfigError),
119 + Effect.withSpan("environment.initialSync"),
120 + ),
121 + );
122 + const probe = client[WS_METHODS.serverGetConfig]({}).pipe(
123 + Effect.mapError(mapInitialConfigError),
124 + Effect.asVoid,
125 + Effect.withSpan("clientRuntime.connection.rpcSession.probe"),
126 + );
127 +
128 + return {
129 + client,
130 + initialConfig,
131 + ready: Deferred.await(connected).pipe(
132 + Effect.andThen(initialConfig),
133 Effect.asVoid,
126 - Effect.withSpan("clientRuntime.connection.rpcSession.probe"),
127 - );
134 + Effect.raceFirst(Deferred.await(disconnected)),
135 + ),
136 + probe,
137 + closed: Deferred.await(disconnected),
138 + } satisfies RpcSession;
139 + });
140
129 - return {
130 - client,
131 - initialConfig,
132 - ready: Deferred.await(connected).pipe(
133 - Effect.andThen(initialConfig),
134 - Effect.asVoid,
135 - Effect.raceFirst(Deferred.await(disconnected)),
136 - ),
137 - probe,
138 - closed: Deferred.await(disconnected),
139 - } satisfies RpcSession;
140 - });
141 + return RpcSessionFactory.of({ connect });
142 +});
143
142 - return RpcSessionFactory.of({ connect });
143 - }),
144 -);
144 +export const layer = Layer.effect(RpcSessionFactory, make);
packages/client-runtime/src/state/connections.ts
+21 -11
@@ -5,10 +5,10 @@ import * as Stream from "effect/Stream";
5 import * as SubscriptionRef from "effect/SubscriptionRef";
6 import { AsyncResult, Atom } from "effect/unstable/reactivity";
7
8 -import { EnvironmentRegistry, type EnvironmentRegistryService } from "../connection/registry.ts";
8 +import * as EnvironmentRegistry from "../connection/registry.ts";
9 import type { ConnectionCatalogEntry } from "../connection/catalog.ts";
10 import { AVAILABLE_CONNECTION_STATE } from "../connection/model.ts";
11 -import { EnvironmentSupervisor } from "../connection/supervisor.ts";
11 +import * as EnvironmentSupervisor from "../connection/supervisor.ts";
12 import {
13 createAtomCommandScheduler,
14 createRuntimeCommand,
@@ -26,13 +26,13 @@ export const EMPTY_ENVIRONMENT_CATALOG_STATE: EnvironmentCatalogState = Object.f
26 });
27
28 export function createEnvironmentCatalogAtoms<R, E>(
29 - runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
29 + runtime: Atom.AtomRuntime<EnvironmentRegistry.EnvironmentRegistry | R, E>,
30 ) {
31 const commandScheduler = createAtomCommandScheduler();
32 const serial = { mode: "serial" as const, key: () => "environment-catalog" };
33 const catalogAtom = runtime.atom(
34 Stream.unwrap(
35 - EnvironmentRegistry.pipe(
35 + EnvironmentRegistry.EnvironmentRegistry.pipe(
36 Effect.map((registry) =>
37 SubscriptionRef.changes(registry.entries).pipe(
38 Stream.map((entries) => ({
@@ -52,7 +52,7 @@ export function createEnvironmentCatalogAtoms<R, E>(
52
53 const networkStatusAtom = runtime.atom(
54 Stream.unwrap(
55 - EnvironmentRegistry.pipe(
55 + EnvironmentRegistry.EnvironmentRegistry.pipe(
56 Effect.map((registry) => SubscriptionRef.changes(registry.networkStatus)),
57 ),
58 ),
@@ -68,7 +68,7 @@ export function createEnvironmentCatalogAtoms<R, E>(
68 followStreamInEnvironment(
69 environmentId,
70 Stream.unwrap(
71 - EnvironmentSupervisor.pipe(
71 + EnvironmentSupervisor.EnvironmentSupervisor.pipe(
72 Effect.map((supervisor) => SubscriptionRef.changes(supervisor.state)),
73 ),
74 ),
@@ -81,29 +81,39 @@ export function createEnvironmentCatalogAtoms<R, E>(
81 label: "environment-catalog:register",
82 scheduler: commandScheduler,
83 concurrency: serial,
84 - execute: (target: Parameters<EnvironmentRegistryService["register"]>[0]) =>
85 - EnvironmentRegistry.pipe(Effect.flatMap((registry) => registry.register(target))),
84 + execute: (
85 + target: Parameters<EnvironmentRegistry.EnvironmentRegistry["Service"]["register"]>[0],
86 + ) =>
87 + EnvironmentRegistry.EnvironmentRegistry.pipe(
88 + Effect.flatMap((registry) => registry.register(target)),
89 + ),
90 });
91 const remove = createRuntimeCommand(runtime, {
92 label: "environment-catalog:remove",
93 scheduler: commandScheduler,
94 concurrency: serial,
95 execute: (environmentId: EnvironmentIdType) =>
92 - EnvironmentRegistry.pipe(Effect.flatMap((registry) => registry.remove(environmentId))),
96 + EnvironmentRegistry.EnvironmentRegistry.pipe(
97 + Effect.flatMap((registry) => registry.remove(environmentId)),
98 + ),
99 });
100 const removeRelayEnvironments = createRuntimeCommand(runtime, {
101 label: "environment-catalog:remove-relay-environments",
102 scheduler: commandScheduler,
103 concurrency: serial,
104 execute: (_input: void) =>
99 - EnvironmentRegistry.pipe(Effect.flatMap((registry) => registry.removeRelayEnvironments())),
105 + EnvironmentRegistry.EnvironmentRegistry.pipe(
106 + Effect.flatMap((registry) => registry.removeRelayEnvironments()),
107 + ),
108 });
109 const retryNow = createRuntimeCommand(runtime, {
110 label: "environment-catalog:retry-now",
111 scheduler: commandScheduler,
112 concurrency: serial,
113 execute: (environmentId: EnvironmentIdType) =>
106 - EnvironmentRegistry.pipe(Effect.flatMap((registry) => registry.retryNow(environmentId))),
114 + EnvironmentRegistry.EnvironmentRegistry.pipe(
115 + Effect.flatMap((registry) => registry.retryNow(environmentId)),
116 + ),
117 });
118
119 return {
packages/client-runtime/src/state/shell-sync.test.ts
+10 -13
@@ -16,12 +16,9 @@ import {
16 PrimaryConnectionTarget,
17 type PreparedConnection,
18 } from "../connection/model.ts";
19 -import {
20 - EnvironmentSupervisor,
21 - type EnvironmentSupervisorService,
22 -} from "../connection/supervisor.ts";
23 -import { EnvironmentCacheStore } from "../platform/persistence.ts";
24 -import type { RpcSession } from "../rpc/session.ts";
19 +import * as EnvironmentSupervisor from "../connection/supervisor.ts";
20 +import * as Persistence from "../platform/persistence.ts";
21 +import * as RpcSession from "../rpc/session.ts";
22 import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
23 import { makeEnvironmentShellState } from "./shell.ts";
24
@@ -39,7 +36,7 @@ const LIVE_SHELL_SNAPSHOT: OrchestrationShellSnapshot = {
36 updatedAt: "2026-06-06T00:00:00.000Z",
37 };
38
42 -function session(client: WsRpcProtocolClient): RpcSession {
39 +function session(client: WsRpcProtocolClient): RpcSession.RpcSession {
40 return {
41 client,
42 initialConfig: Effect.never,
@@ -57,10 +54,10 @@ describe("environment shell synchronization", () => {
54 [ORCHESTRATION_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
55 } as unknown as WsRpcProtocolClient;
56 const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
60 - const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession>>(
57 + const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
58 Option.some(session(client)),
59 );
63 - const supervisor = EnvironmentSupervisor.of({
60 + const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
61 target: TARGET,
62 state: supervisorState,
63 session: activeSession,
@@ -68,8 +65,8 @@ describe("environment shell synchronization", () => {
65 connect: Effect.void,
66 disconnect: Effect.void,
67 retryNow: Effect.void,
71 - } satisfies EnvironmentSupervisorService);
72 - const cache = EnvironmentCacheStore.of({
68 + } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
69 + const cache = Persistence.EnvironmentCacheStore.of({
70 loadShell: () => Effect.succeed(Option.none()),
71 saveShell: () => Effect.never,
72 loadThread: () => Effect.succeed(Option.none()),
@@ -78,8 +75,8 @@ describe("environment shell synchronization", () => {
75 clear: () => Effect.void,
76 });
77 const shellState = yield* makeEnvironmentShellState().pipe(
81 - Effect.provideService(EnvironmentSupervisor, supervisor),
82 - Effect.provideService(EnvironmentCacheStore, cache),
78 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
79 + Effect.provideService(Persistence.EnvironmentCacheStore, cache),
80 );
81
82 yield* SubscriptionRef.set(supervisorState, {
packages/client-runtime/src/state/threads-sync.test.ts
+10 -13
@@ -24,12 +24,9 @@ import {
24 type PreparedConnection,
25 type SupervisorConnectionState,
26 } from "../connection/model.ts";
27 -import {
28 - EnvironmentSupervisor,
29 - type EnvironmentSupervisorService,
30 -} from "../connection/supervisor.ts";
31 -import { EnvironmentCacheStore } from "../platform/persistence.ts";
32 -import type { RpcSession } from "../rpc/session.ts";
27 +import * as EnvironmentSupervisor from "../connection/supervisor.ts";
28 +import * as Persistence from "../platform/persistence.ts";
29 +import * as RpcSession from "../rpc/session.ts";
30 import {
31 EMPTY_ENVIRONMENT_THREAD_STATE,
32 makeEnvironmentThreadState,
@@ -69,7 +66,7 @@ const BASE_THREAD: OrchestrationThread = {
66
67 type TestThreadInput = OrchestrationThreadStreamItem | Error;
68
72 -function testSession(client: WsRpcProtocolClient): RpcSession {
69 +function testSession(client: WsRpcProtocolClient): RpcSession.RpcSession {
70 return {
71 client,
72 initialConfig: Effect.never,
@@ -117,11 +114,11 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
114 ),
115 ),
116 } as unknown as WsRpcProtocolClient;
120 - const supervisorSession = yield* SubscriptionRef.make<Option.Option<RpcSession>>(
117 + const supervisorSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
118 Option.some(testSession(client)),
119 );
120 const prepared = yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(Option.none());
124 - const supervisor = EnvironmentSupervisor.of({
121 + const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
122 target: TARGET,
123 state: supervisorState,
124 session: supervisorSession,
@@ -129,8 +126,8 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
126 connect: Effect.void,
127 disconnect: Effect.void,
128 retryNow: Ref.update(retryCount, (count) => count + 1),
132 - } satisfies EnvironmentSupervisorService);
133 - const cache = EnvironmentCacheStore.of({
129 + } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
130 + const cache = Persistence.EnvironmentCacheStore.of({
131 loadShell: () => Effect.succeed(Option.none()),
132 saveShell: () => Effect.void,
133 loadThread: (_environmentId, threadId) =>
@@ -146,8 +143,8 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
143 clear: () => Effect.void,
144 });
145 const threadState = yield* makeEnvironmentThreadState(THREAD_ID).pipe(
149 - Effect.provideService(EnvironmentSupervisor, supervisor),
150 - Effect.provideService(EnvironmentCacheStore, cache),
146 + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
147 + Effect.provideService(Persistence.EnvironmentCacheStore, cache),
148 );
149 yield* SubscriptionRef.changes(threadState).pipe(
150 Stream.runForEach((state) =>