@devries-cameron20 / t3code-1 / commits / 8657eecb28
Fork 0

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

fix(server): defer Tailscale endpoint discovery during startup (#29432)

Yash Singh committed Nov 2, 2026 at 11:05 UTC 8657eecb28bc8c767510cc6b9d852b628047c604
21 files changed +419 -142
apps/server/src/environment/DirectEndpoints.test.ts
+75 -13
@@ -2,6 +2,9 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { it as effectIt } from "@effect/vitest";
3 import type * as NodeOS from "node:os";
4 import * as Effect from "effect/Effect";
5 +import * as Deferred from "effect/Deferred";
6 +import * as Fiber from "effect/Fiber";
7 +import * as Ref from "effect/Ref";
8 import * as Layer from "effect/Layer";
9 import * as Sink from "effect/Sink";
10 import * as Stream from "effect/Stream";
@@ -255,25 +258,27 @@ const layerServeProbe = (status: number) =>
258 ),
259 );
260
258 -const layerServeConfig = Layer.effect(
259 - ServerConfig.ServerConfig,
260 - Effect.map(ServerConfig.ServerConfig, (config) => ({
261 - ...config,
262 - host: "127.0.0.1",
263 - tailscaleServeEnabled: true,
264 - tailscaleServePort: 443,
265 - })),
266 -).pipe(
267 - Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-direct-" })),
268 - Layer.provide(NodeServices.layer),
269 -);
261 +const layerServeConfig = (host = "127.0.0.1") =>
262 + Layer.effect(
263 + ServerConfig.ServerConfig,
264 + Effect.map(ServerConfig.ServerConfig, (config) => ({
265 + ...config,
266 + host,
267 + port: 3773,
268 + tailscaleServeEnabled: true,
269 + tailscaleServePort: 443,
270 + })),
271 + ).pipe(
272 + Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-direct-" })),
273 + Layer.provide(NodeServices.layer),
274 + );
275
276 const resolveWithServe = (probeStatus: number) =>
277 Effect.flatMap(DirectEndpoints.DirectEndpoints, (service) => service.resolve()).pipe(
278 Effect.provide(
279 DirectEndpoints.layer.pipe(
280 Layer.provide(
276 - Layer.mergeAll(layerServeConfig, layerTailscaleUp, layerServeProbe(probeStatus)),
281 + Layer.mergeAll(layerServeConfig(), layerTailscaleUp, layerServeProbe(probeStatus)),
282 ),
283 ),
284 ),
@@ -288,6 +293,63 @@ describe("DirectEndpoints Tailscale Serve", () => {
293 }),
294 );
295
296 + effectIt.effect(
297 + "returns bound endpoints during validation and reuses only a completed Serve result",
298 + () =>
299 + Effect.gen(function* () {
300 + const status = yield* Ref.make(200);
301 + const finishProbe = yield* Deferred.make<void>();
302 + const probeStarted = yield* Deferred.make<void>();
303 + const revalidationStarted = yield* Deferred.make<void>();
304 + const finishRevalidation = yield* Deferred.make<void>();
305 + const service = yield* DirectEndpoints.make.pipe(
306 + Effect.provide(
307 + Layer.mergeAll(
308 + layerServeConfig("192.168.1.10"),
309 + layerTailscaleUp,
310 + Layer.succeed(
311 + HttpClient.HttpClient,
312 + HttpClient.make((request) =>
313 + Deferred.succeed(probeStarted, undefined).pipe(
314 + Effect.andThen(Deferred.await(finishProbe)),
315 + Effect.andThen(Ref.get(status)),
316 + Effect.tap((status) =>
317 + status === 502
318 + ? Deferred.succeed(revalidationStarted, undefined).pipe(
319 + Effect.andThen(Deferred.await(finishRevalidation)),
320 + )
321 + : Effect.void,
322 + ),
323 + Effect.map((status) =>
324 + HttpClientResponse.fromWeb(request, new Response(null, { status })),
325 + ),
326 + ),
327 + ),
328 + ),
329 + ),
330 + ),
331 + );
332 + const resolving = yield* Effect.forkScoped(service.resolve());
333 + yield* Deferred.await(probeStarted);
334 + const bound = { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" };
335 + expect(yield* service.resolveAvailable()).toEqual([bound]);
336 + yield* Deferred.succeed(finishProbe, undefined);
337 + const validated = yield* Fiber.join(resolving);
338 + expect(validated).toEqual([
339 + bound,
340 + { kind: "tailnet", httpBaseUrl: "https://bb-1.tail1234.ts.net/" },
341 + ]);
342 + expect(yield* service.resolveAvailable()).toEqual(validated);
343 + yield* Ref.set(status, 502);
344 + const revalidating = yield* Effect.forkScoped(service.resolve());
345 + yield* Deferred.await(revalidationStarted);
346 + expect(yield* service.resolveAvailable()).toEqual(validated);
347 + yield* Deferred.succeed(finishRevalidation, undefined);
348 + expect(yield* Fiber.join(revalidating)).toEqual([bound]);
349 + expect(yield* service.resolveAvailable()).toEqual([bound]);
350 + }).pipe(Effect.scoped),
351 + );
352 +
353 effectIt.effect("leaves the tailnet name out when Serve is not forwarding", () =>
354 Effect.gen(function* () {
355 expect(yield* resolveWithServe(502)).toEqual([]);
apps/server/src/environment/DirectEndpoints.ts
+13 -5
@@ -9,6 +9,7 @@ import * as Duration from "effect/Duration";
9 import * as Effect from "effect/Effect";
10 import * as Layer from "effect/Layer";
11 import * as Option from "effect/Option";
12 +import * as Ref from "effect/Ref";
13 import { HttpClient } from "effect/http";
14 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
15 import * as NodeNet from "node:net";
@@ -34,6 +35,7 @@ const TAILSCALE_ENDPOINT_TIMEOUT = Duration.seconds(3);
35 export class DirectEndpoints extends Context.Service<
36 DirectEndpoints,
37 {
38 + readonly resolveAvailable: () => Effect.Effect<ReadonlyArray<ServerDirectEndpoint>>;
39 readonly resolve: () => Effect.Effect<ReadonlyArray<ServerDirectEndpoint>>;
40 }
41 >()("t3/environment/DirectEndpoints") {}
@@ -128,7 +130,8 @@ export const make = Effect.gen(function* () {
130 : Effect.succeed(false);
131 yield* Effect.forkDetach(onCloudVm);
132
131 - const resolve = Effect.gen(function* () {
133 + const servedEndpoint = yield* Ref.make<Option.Option<string>>(Option.none());
134 + const resolveAvailable = Effect.fn("DirectEndpoints.resolveAvailable")(function* () {
135 const endpoints = [
136 ...resolveBoundEndpoints({
137 platform,
@@ -138,6 +141,12 @@ export const make = Effect.gen(function* () {
141 onCloudVm: yield* onCloudVm,
142 }),
143 ];
144 + const servedUrl = yield* Ref.get(servedEndpoint);
145 + if (Option.isSome(servedUrl)) endpoints.push({ kind: "tailnet", httpBaseUrl: servedUrl.value });
146 + return endpoints;
147 + });
148 +
149 + const resolve = Effect.gen(function* () {
150 // Serve setup can fail without stopping startup; advertise only after it answers as this server.
151 if (config.tailscaleServeEnabled) {
152 const servedUrl = yield* readTailscaleStatus.pipe(
@@ -158,14 +167,13 @@ export const make = Effect.gen(function* () {
167 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
168 Effect.provideService(HttpClient.HttpClient, httpClient),
169 );
161 - if (Option.isSome(servedUrl)) {
162 - endpoints.push({ kind: "tailnet", httpBaseUrl: servedUrl.value });
163 - }
170 + yield* Ref.set(servedEndpoint, servedUrl);
171 }
165 - return endpoints;
172 + return yield* resolveAvailable();
173 });
174
175 return DirectEndpoints.of({
176 + resolveAvailable,
177 resolve: Effect.fn("DirectEndpoints.resolve")(function () {
178 return resolve;
179 }),
apps/server/src/ws.test.ts
+76 -6
@@ -154,6 +154,7 @@ it.effect("resends late editors without rolling back updates already sent", () =
154 Deferred.await(settingsSent).pipe(Effect.as("file-explorer" as const)),
155 },
156 { resolveTargets: () => Effect.succeed([]) },
157 + { resolve: () => Effect.succeed([]) },
158 ).pipe(
159 Stream.tap((event) =>
160 event.type === "settingsUpdated" ? Deferred.succeed(settingsSent, undefined) : Effect.void,
@@ -185,6 +186,7 @@ it.effect("resends a reveal kind and remote targets that outlast the snapshot ti
186 resolveFileManagerRevealKind: () => Effect.succeed("file-explorer"),
187 },
188 { resolveTargets: () => Deferred.await(finishDiscovery) },
189 + { resolve: () => Effect.succeed([]) },
190 ).pipe(Stream.runCollect, Effect.forkScoped({ startImmediately: true }));
191 yield* TestClock.adjust("6 seconds");
192 yield* Deferred.succeed(finishDiscovery, targets);
@@ -618,8 +620,15 @@ const layerConfigSources = Layer.mergeAll(
620 Layer.provideMerge(NodeServices.layer),
621 );
622
621 -it.effect.each(["pending", "cached"] as const)(
622 - "%s remote discovery preserves initial config and updates through discovery and ACK",
623 +it.effect.each([
624 + "pending",
625 + "cached",
626 + "direct-pending",
627 + "direct-cached",
628 + "direct-failed",
629 + "direct-interrupted",
630 +] as const)(
631 + "%s endpoint discovery preserves initial config and updates through discovery and ACK",
632 (discovery) =>
633 Effect.gen(function* () {
634 const keybindings = yield* Keybindings.Keybindings;
@@ -638,6 +647,15 @@ it.effect.each(["pending", "cached"] as const)(
647 const remoteDiscoveryStarted = yield* Deferred.make<void>();
648 const finishRemoteDiscovery = yield* Deferred.make<void>();
649 const targets = [{ kind: "mdns" as const, host: "test.local" }];
650 + const direct = discovery.startsWith("direct-");
651 + const bound = [{ kind: "lan" as const, httpBaseUrl: "http://192.168.1.10:3773/" }];
652 + const endpoints = [
653 + ...bound,
654 + { kind: "tailnet" as const, httpBaseUrl: "https://test.ts.net/" },
655 + ];
656 + const directStarted = yield* Deferred.make<void>();
657 + const finishDirect = yield* Deferred.make<void>();
658 + const directStopped = yield* Deferred.make<void>();
659 const responses = yield* Queue.unbounded<ServerConfigStreamEvent>();
660 const configRpcServices = Layer.mergeAll(
661 layerUnusedConfigRpcServices,
@@ -659,12 +677,30 @@ it.effect.each(["pending", "cached"] as const)(
677 resolveTargets: () =>
678 Deferred.succeed(remoteDiscoveryStarted, undefined).pipe(
679 Effect.andThen(
662 - discovery === "cached" ? Effect.void : Deferred.await(finishRemoteDiscovery),
680 + discovery === "cached" || direct
681 + ? Effect.void
682 + : Deferred.await(finishRemoteDiscovery),
683 ),
684 Effect.as(targets),
685 ),
686 }),
667 - Layer.mock(DirectEndpoints.DirectEndpoints)({ resolve: () => Effect.succeed([]) }),
687 + Layer.mock(DirectEndpoints.DirectEndpoints)({
688 + resolveAvailable: () => Effect.succeed(discovery === "direct-cached" ? endpoints : bound),
689 + resolve: () =>
690 + direct
691 + ? Deferred.succeed(directStarted, undefined).pipe(
692 + Effect.andThen(
693 + discovery === "direct-cached" ? Effect.void : Deferred.await(finishDirect),
694 + ),
695 + Effect.andThen(
696 + discovery === "direct-failed"
697 + ? Effect.die("probe failed")
698 + : Effect.succeed(endpoints),
699 + ),
700 + Effect.onExit(() => Deferred.succeed(directStopped, undefined)),
701 + )
702 + : Effect.succeed(bound),
703 + }),
704 Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
705 scratchRoot: Effect.succeedNone,
706 namedProjectsRoot: process.cwd(),
@@ -744,7 +780,14 @@ it.effect.each(["pending", "cached"] as const)(
780 const snapshot = yield* Queue.take(responses);
781 assert.equal(snapshot.type, "snapshot");
782 if (snapshot.type !== "snapshot") return yield* Effect.die("Missing initial snapshot");
747 - assert.deepEqual(snapshot.config.remoteOpenTargets, discovery === "cached" ? targets : []);
783 + assert.deepEqual(
784 + snapshot.config.remoteOpenTargets,
785 + discovery === "cached" || direct ? targets : [],
786 + );
787 + assert.deepEqual(
788 + snapshot.config.directEndpoints,
789 + discovery === "direct-cached" ? endpoints : bound,
790 + );
791 assert.isFalse(snapshot.config.settings.defaultAutoPull);
792 assert.deepEqual(snapshot.config.keybindings, initialKeybindings);
793 const duringAck = yield* keybindings.upsertKeybindingRule({
@@ -795,7 +838,31 @@ it.effect.each(["pending", "cached"] as const)(
838 assert.deepEqual(resolved.config.keybindings, latestKeybindings.keybindings);
839 yield* acknowledge;
840 }
841 + if (direct) {
842 + yield* Deferred.await(directStarted);
843 + if (discovery === "direct-pending") {
844 + yield* Deferred.succeed(finishDirect, undefined);
845 + const resolved = yield* Queue.take(responses);
846 + assert.equal(resolved.type, "snapshot");
847 + if (resolved.type !== "snapshot") return yield* Effect.die("Missing direct endpoints");
848 + assert.deepEqual(resolved.config.directEndpoints, endpoints);
849 + assert.deepEqual(
850 + resolved.config.settings,
851 + ServerSettings.redactServerSettingsForClient(latestSettings),
852 + );
853 + assert.deepEqual(resolved.config.keybindings, latestKeybindings.keybindings);
854 + yield* acknowledge;
855 + } else if (discovery === "direct-failed") {
856 + yield* Deferred.succeed(finishDirect, undefined);
857 + yield* Deferred.await(directStopped);
858 + yield* settings.updateSettings({ defaultAutoPull: false });
859 + const event = yield* Queue.take(responses);
860 + assert.equal(event.type, "settingsUpdated");
861 + yield* acknowledge;
862 + }
863 + }
864 yield* server.write(0, { _tag: "Interrupt", requestId, interruptors: [] });
865 + if (direct) yield* Deferred.await(directStopped);
866 }).pipe(
867 Effect.provide(
868 Layer.mergeAll(
@@ -858,7 +925,10 @@ it.effect("on-demand clients get provider config without workspace snapshots", (
925 Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({
926 resolveTargets: () => Effect.succeed([]),
927 }),
861 - Layer.mock(DirectEndpoints.DirectEndpoints)({ resolve: () => Effect.succeed([]) }),
928 + Layer.mock(DirectEndpoints.DirectEndpoints)({
929 + resolve: () => Effect.succeed([]),
930 + resolveAvailable: () => Effect.succeed([]),
931 + }),
932 Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
933 scratchRoot: Effect.succeedNone,
934 namedProjectsRoot: process.cwd(),
apps/server/src/ws.ts
+39 -9
@@ -320,6 +320,7 @@ export const withLateConfigDiscovery = <E, R>(
320 liveUpdates: Stream.Stream<ServerConfigStreamEvent, E, R>,
321 launcher: EditorDiscovery,
322 remoteOpenTargets: RemoteOpenTargets.RemoteOpenTargets["Service"],
323 + directEndpoints: Pick<DirectEndpoints.DirectEndpoints["Service"], "resolve">,
324 ) => {
325 const lateEditorConfig = Stream.fromEffect(launcher.resolveAvailableEditors()).pipe(
326 Stream.filter(
@@ -347,7 +348,15 @@ export const withLateConfigDiscovery = <E, R>(
348 Stream.map((targets) => ({ type: "remoteOpenTargetsResolved" as const, targets })),
349 );
350
350 - return Stream.merge(liveUpdates, Stream.merge(lateEditorConfig, lateRemoteOpenTargets)).pipe(
351 + const lateDirectEndpoints = Stream.fromEffect(directEndpoints.resolve()).pipe(
352 + Stream.catchCause(() => Stream.empty),
353 + Stream.map((endpoints) => ({ type: "directEndpointsResolved" as const, endpoints })),
354 + );
355 +
356 + return Stream.merge(
357 + liveUpdates,
358 + Stream.merge(lateEditorConfig, Stream.merge(lateRemoteOpenTargets, lateDirectEndpoints)),
359 + ).pipe(
360 Stream.mapAccum(
361 (): ClientServerConfig => config,
362 (current, event): readonly [ClientServerConfig, ReadonlyArray<ServerConfigStreamEvent>] => {
@@ -366,6 +375,13 @@ export const withLateConfigDiscovery = <E, R>(
375 const next = { ...current, remoteOpenTargets: event.targets };
376 return [next, [{ version: 1, type: "snapshot", config: next }]];
377 }
378 + case "directEndpointsResolved": {
379 + if (JSON.stringify(current.directEndpoints ?? []) === JSON.stringify(event.endpoints)) {
380 + return [current, []];
381 + }
382 + const next = { ...current, directEndpoints: event.endpoints };
383 + return [next, [{ version: 1, type: "snapshot", config: next }]];
384 + }
385 case "keybindingsUpdated":
386 return [{ ...current, ...event.payload }, [event]];
387 case "providerStatuses":
@@ -1769,7 +1785,7 @@ export const layerWsRpc = (
1785 const loadServerConfig = Effect.fn("ws.loadServerConfig")(function* (options: {
1786 readonly usageLimitsCommand: boolean;
1787 readonly workspaceSnapshotsOnDemand: boolean;
1772 - readonly deferRemoteOpenTargets: boolean;
1788 + readonly deferNetworkDiscovery: boolean;
1789 }) {
1790 const keybindingsConfig = yield* keybindings.loadConfigState;
1791 const currentProviders = yield* providerRegistry.getProviders;
@@ -1789,10 +1805,12 @@ export const layerWsRpc = (
1805 const discovery = yield* resolveOpenDiscoveryForConfig({
1806 editors: externalLauncher.resolveAvailableEditors(),
1807 fileManagerRevealKind: externalLauncher.resolveFileManagerRevealKind(),
1792 - remoteOpenTargets: options.deferRemoteOpenTargets
1808 + remoteOpenTargets: options.deferNetworkDiscovery
1809 ? Effect.succeed([])
1810 : remoteOpenTargets.resolveTargets(),
1795 - directEndpoints: directEndpoints.resolve(),
1811 + directEndpoints: options.deferNetworkDiscovery
1812 + ? directEndpoints.resolveAvailable()
1813 + : directEndpoints.resolve(),
1814 });
1815
1816 return {
@@ -2229,7 +2247,7 @@ export const layerWsRpc = (
2247 loadServerConfig({
2248 usageLimitsCommand: false,
2249 workspaceSnapshotsOnDemand: false,
2232 - deferRemoteOpenTargets: false,
2250 + deferNetworkDiscovery: false,
2251 }),
2252 [WS_METHODS.serverSearchAcpRegistry]: (input) =>
2253 acpRegistryCatalog
@@ -3154,10 +3172,16 @@ export const layerWsRpc = (
3172 const remoteTargets = yield* Effect.forkScoped(remoteOpenTargets.resolveTargets(), {
3173 startImmediately: true,
3174 });
3175 + const directResult = yield* Effect.forkScoped(
3176 + directEndpoints
3177 + .resolve()
3178 + .pipe(Effect.catchCause(() => directEndpoints.resolveAvailable())),
3179 + { startImmediately: true },
3180 + );
3181 const config = yield* loadServerConfig({
3182 usageLimitsCommand,
3183 workspaceSnapshotsOnDemand,
3160 - deferRemoteOpenTargets: true,
3184 + deferNetworkDiscovery: true,
3185 });
3186 const keybindingsUpdates = keybindingsChanges.pipe(
3187 Stream.map((event) => ({
@@ -3253,9 +3277,15 @@ export const layerWsRpc = (
3277 rpcInitialItems([
3278 { version: 1 as const, type: "snapshot" as const, config: initialConfig },
3279 ]),
3256 - withLateConfigDiscovery(initialConfig, liveUpdates, externalLauncher, {
3257 - resolveTargets: () => Fiber.join(remoteTargets),
3258 - }),
3280 + withLateConfigDiscovery(
3281 + initialConfig,
3282 + liveUpdates,
3283 + externalLauncher,
3284 + {
3285 + resolveTargets: () => Fiber.join(remoteTargets),
3286 + },
3287 + { resolve: () => Fiber.join(directResult) },
3288 + ),
3289 );
3290 }),
3291 ),
apps/web/src/cloud/linkEnvironment.test.ts
+1 -1
@@ -82,7 +82,7 @@ function layerRegistry(options?: {
82 const session: RpcSession = {
83 client,
84 initialConfig: Effect.never,
85 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
85 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
86 ready: Effect.void,
87 probe: Effect.void,
88 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/connection/registry.test.ts
+7 -1
@@ -418,7 +418,13 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
418 ? Effect.die(new Error("Config is not used by registry tests."))
419 : Effect.succeed({ directEndpoints: options.directEndpoints } as ServerConfig),
420 subscribeServerConfig: () =>
421 - Stream.die(new Error("Config is not used by registry tests.")),
421 + options?.directEndpoints === undefined
422 + ? Stream.never
423 + : Stream.make({
424 + version: 1 as const,
425 + type: "snapshot" as const,
426 + config: { directEndpoints: options.directEndpoints } as ServerConfig,
427 + }),
428 ready: Effect.void,
429 probe: Effect.void,
430 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/connection/supervisor.test.ts
+144 -60
@@ -1,4 +1,9 @@
1 -import { AuthStandardClientScopes, EnvironmentId, type ServerConfig } from "@t3tools/contracts";
1 +import {
2 + AuthStandardClientScopes,
3 + EnvironmentId,
4 + type ServerConfig,
5 + type ServerConfigStreamEvent,
6 +} from "@t3tools/contracts";
7 import { describe, expect, it } from "@effect/vitest";
8 import * as Deferred from "effect/Deferred";
9 import * as Effect from "effect/Effect";
@@ -79,7 +84,7 @@ const LAN_TARGET = new BearerConnectionTarget({
84 label: TARGET.label,
85 connectionId: "bearer:lan",
86 });
82 -const LAN_ROUTE: ConnectionRoute = {
87 +const LAN_ROUTE = {
88 target: LAN_TARGET,
89 profile: Option.some(
90 new BearerConnectionProfile({
@@ -90,7 +95,7 @@ const LAN_ROUTE: ConnectionRoute = {
95 wsBaseUrl: "ws://192.168.1.10:3773/",
96 }),
97 ),
93 -};
98 +} satisfies ConnectionRoute;
99 const LAN_THEN_RELAY_ENTRY: ConnectionCatalogEntry = {
100 target: LAN_ROUTE.target,
101 profile: LAN_ROUTE.profile,
@@ -145,6 +150,7 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
150 ) => Effect.Effect<PreparedConnection, ConnectionAttemptError>;
151 readonly ready?: (attempt: number) => Effect.Effect<void, ConnectionAttemptError>;
152 readonly probe?: (attempt: number) => Effect.Effect<void, ConnectionAttemptError>;
153 + readonly configEvents?: Stream.Stream<ServerConfigStreamEvent> | undefined;
154 readonly initialConfig?: (attempt: number) => Effect.Effect<ServerConfig, ConnectionAttemptError>;
155 readonly checkRoute?: (route: ConnectionRoute) => Effect.Effect<ConnectionDriver.RouteCheck>;
156 readonly checkHost?: (prepared: PreparedConnection) => Effect.Effect<ConnectionDriver.RouteCheck>;
@@ -201,7 +207,13 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
207 initialConfig:
208 options?.initialConfig?.(attempt) ??
209 Effect.die(new Error("Initial config is not used by supervisor tests.")),
204 - subscribeServerConfig: (input) => TEST_RPC_CLIENT.subscribeServerConfig(input),
210 + subscribeServerConfig: () =>
211 + options?.configEvents ??
212 + Stream.fromEffect(
213 + (options?.initialConfig?.(attempt) ?? Effect.never).pipe(Effect.orDie),
214 + ).pipe(
215 + Stream.map((config) => ({ version: 1 as const, type: "snapshot" as const, config })),
216 + ),
217 ready: options?.ready?.(attempt) ?? Effect.void,
218 probe: options?.probe?.(attempt) ?? Effect.void,
219 inboundFrames: options?.inboundFrames ?? Effect.succeed(0),
@@ -1789,62 +1801,134 @@ describe("EnvironmentSupervisor routes", () => {
1801 }),
1802 );
1803
1792 - it.effect("learns the LAN address over T3 Connect and moves to it", () =>
1793 - Effect.gen(function* () {
1794 - const relayEntry: ConnectionCatalogEntry = {
1795 - target: RELAY_TARGET,
1796 - profile: Option.none(),
1797 - enabled: true,
1798 - };
1799 - const lanAddress = yield* Ref.make("http://192.168.1.10:3773/");
1800 - const learned = yield* Ref.make<ReadonlyArray<string>>([]);
1801 - const harness = yield* makeHarness({
1802 - checkRoute: (route) =>
1803 - route.target._tag === "BearerConnectionTarget"
1804 - ? Effect.succeed("answered")
1805 - : Effect.succeed("unchecked"),
1806 - prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1807 - initialConfig: () =>
1808 - Ref.get(lanAddress).pipe(
1809 - Effect.map((httpBaseUrl): Pick<ServerConfig, "directEndpoints"> => ({
1810 - directEndpoints: [{ kind: "lan", httpBaseUrl }],
1811 - })),
1812 - Effect.map((config) => config as ServerConfig),
1813 - ),
1814 - });
1815 - const current = yield* Ref.make(relayEntry);
1816 - const supervisor = yield* EnvironmentSupervisor.make(relayEntry, {
1817 - initiallyDesired: true,
1818 - learnRoutes: ({ activeRoute, reported }) =>
1819 - Effect.gen(function* () {
1820 - const entry = yield* Ref.get(current);
1821 - const routes = mergeLearnedRoutes({
1822 - entry,
1823 - activeRoute,
1824 - reported,
1825 - allowInsecure: true,
1826 - });
1827 - if (routes === null) return Option.none();
1828 - const next = entryWithRoutes(entry, routes);
1829 - yield* Ref.set(current, next);
1830 - yield* Ref.update(learned, (all) => [
1831 - ...all,
1832 - ...routes.filter(isLearned).map((route) => connectionRouteId(route.target)),
1833 - ]);
1834 - return Option.some(next);
1835 - }),
1836 - }).pipe(Effect.provide(harness.dependencies));
1804 + it.effect.each(["initial", "late", "withdrawn", "withdrawn-upgrade", "omitted"] as const)(
1805 + "learns the %s direct endpoint over T3 Connect",
1806 + (discovery) =>
1807 + Effect.gen(function* () {
1808 + const relayEntry: ConnectionCatalogEntry =
1809 + discovery.startsWith("withdrawn") || discovery === "omitted"
1810 + ? {
1811 + target: LAN_ROUTE.target,
1812 + profile: Option.some(
1813 + new BearerConnectionProfile({
1814 + ...Option.getOrThrow(LAN_ROUTE.profile),
1815 + ...(discovery === "withdrawn-upgrade"
1816 + ? {
1817 + httpBaseUrl: "https://old.example.test/",
1818 + wsBaseUrl: "wss://old.example.test/",
1819 + }
1820 + : {}),
1821 + learned: true,
1822 + authorization: "t3-connect",
1823 + }),
1824 + ),
1825 + alternateRoutes: [{ target: RELAY_TARGET, profile: Option.none() }],
1826 + enabled: true,
1827 + }
1828 + : {
1829 + target: RELAY_TARGET,
1830 + profile: Option.none(),
1831 + enabled: true,
1832 + };
1833 + const initialProcessed = yield* Deferred.make<void>();
1834 + const discoveryProcessed = yield* Deferred.make<void>();
1835 + const discoveryStarted = yield* Deferred.make<void>();
1836 + const finishDiscovery = yield* Deferred.make<void>();
1837 + const lateAddress = "https://fixture.tail1234.ts.net/";
1838 + const learned = yield* Ref.make<ReadonlyArray<string>>([]);
1839 + const harness = yield* makeHarness({
1840 + configEvents:
1841 + discovery !== "initial"
1842 + ? Stream.concat(
1843 + Stream.make({
1844 + version: 1 as const,
1845 + type: "snapshot" as const,
1846 + config: (discovery === "omitted"
1847 + ? {}
1848 + : { directEndpoints: [] }) as unknown as ServerConfig,
1849 + }).pipe(Stream.ensuring(Deferred.succeed(initialProcessed, undefined))),
1850 + Stream.fromEffect(
1851 + Deferred.await(finishDiscovery).pipe(
1852 + Effect.as({
1853 + version: 1 as const,
1854 + type: "snapshot" as const,
1855 + config: {
1856 + directEndpoints: [{ kind: "tailnet", httpBaseUrl: lateAddress }],
1857 + } as unknown as ServerConfig,
1858 + }),
1859 + ),
1860 + ),
1861 + ).pipe(Stream.ensuring(Deferred.succeed(discoveryProcessed, undefined)))
1862 + : undefined,
1863 + checkRoute: (route) =>
1864 + route.target._tag === "BearerConnectionTarget"
1865 + ? Effect.succeed("answered")
1866 + : Effect.succeed("unchecked"),
1867 + prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1868 + initialConfig: () =>
1869 + Effect.succeed({
1870 + directEndpoints:
1871 + discovery !== "initial"
1872 + ? []
1873 + : [{ kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }],
1874 + } as unknown as ServerConfig),
1875 + });
1876 + const current = yield* Ref.make(relayEntry);
1877 + const supervisor = yield* EnvironmentSupervisor.make(relayEntry, {
1878 + initiallyDesired: true,
1879 + learnRoutes: ({ activeRoute, reported }) =>
1880 + Effect.gen(function* () {
1881 + yield* Deferred.succeed(discoveryStarted, undefined);
1882 + const entry = yield* Ref.get(current);
1883 + const routes = mergeLearnedRoutes({
1884 + entry,
1885 + activeRoute,
1886 + reported,
1887 + allowInsecure: true,
1888 + });
1889 + if (routes === null) return Option.none();
1890 + const next = entryWithRoutes(entry, routes);
1891 + yield* Ref.set(current, next);
1892 + yield* Ref.update(learned, (all) => [
1893 + ...all,
1894 + ...routes.filter(isLearned).map((route) => connectionRouteId(route.target)),
1895 + ]);
1896 + return Option.some(next);
1897 + }),
1898 + }).pipe(Effect.provide(harness.dependencies));
1899
1838 - yield* awaitState(
1839 - supervisor.state,
1840 - (state) => state.phase === "connected" && state.generation === 2,
1841 - );
1842 - expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
1843 - "BearerConnectionTarget",
1844 - );
1845 - expect(yield* Ref.get(learned)).toEqual([
1846 - `learned:${TARGET.environmentId}:http://192.168.1.10:3773`,
1847 - ]);
1848 - }),
1900 + if (discovery === "omitted") {
1901 + yield* Deferred.await(initialProcessed);
1902 + expect(yield* Ref.get(current)).toEqual(relayEntry);
1903 + expect(yield* Ref.get(learned)).toEqual([]);
1904 + } else {
1905 + yield* Deferred.await(discoveryStarted);
1906 + }
1907 + yield* Deferred.succeed(finishDiscovery, undefined);
1908 + if (discovery === "withdrawn-upgrade") {
1909 + yield* Deferred.await(discoveryProcessed);
1910 + yield* TestClock.adjust("60 seconds");
1911 + expect((yield* SubscriptionRef.get(supervisor.state)).generation).toBe(2);
1912 + return;
1913 + }
1914 + if (discovery === "withdrawn" || discovery === "omitted") {
1915 + yield* Deferred.await(discoveryProcessed);
1916 + expect(yield* Ref.get(learned)).toContain(
1917 + `learned:${TARGET.environmentId}:https://fixture.tail1234.ts.net`,
1918 + );
1919 + expect((yield* SubscriptionRef.get(supervisor.state)).generation).toBe(1);
1920 + return;
1921 + }
1922 + yield* awaitState(
1923 + supervisor.state,
1924 + (state) => state.phase === "connected" && state.generation === 2,
1925 + );
1926 + expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared)).target._tag).toBe(
1927 + "BearerConnectionTarget",
1928 + );
1929 + expect(yield* Ref.get(learned)).toEqual([
1930 + `learned:${TARGET.environmentId}:${discovery !== "initial" ? "https://fixture.tail1234.ts.net" : "http://192.168.1.10:3773"}`,
1931 + ]);
1932 + }),
1933 );
1934 });
packages/client-runtime/src/connection/supervisor.ts
+50 -33
@@ -36,6 +36,7 @@ import {
36 connectionRoutes,
37 type ConnectionRouteKind,
38 entryWithRoutes,
39 + insertRoute,
40 type ReportedEndpoint,
41 } from "./routes.ts";
42
@@ -87,6 +88,10 @@ interface PendingRetryTrace {
88 readonly reason: ConnectionAttemptError["reason"];
89 }
90
91 +type ConnectedLease = ConnectionDriver.EnvironmentConnectionLease & {
92 + readonly activeRoute: ConnectionRoute | undefined;
93 +};
94 +
95 interface TracedAttemptFailure {
96 readonly error: ConnectionAttemptError;
97 readonly attemptSpan: Option.Option<Tracer.Span>;
@@ -332,35 +337,42 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
337
338 const learnRoutesFrom = Effect.fnUntraced(function* (
339 learn: NonNullable<EnvironmentSupervisorOptions["learnRoutes"]>,
335 - lease: ConnectionDriver.EnvironmentConnectionLease,
340 + lease: ConnectedLease,
341 ) {
337 - const config = yield* lease.session.initialConfig.pipe(Effect.option);
338 - const reported = Option.getOrUndefined(config)?.directEndpoints;
339 - if (reported === undefined) return;
340 - const activeId = connectionRouteId(lease.prepared.target);
341 - const activeRoute = (yield* currentRoutes).find(
342 - (route) => connectionRouteId(route.target) === activeId,
343 - );
342 + const activeRoute = lease.activeRoute;
343 if (activeRoute === undefined) return;
345 - const updated = yield* learn({ activeRoute, reported });
346 - if (Option.isSome(updated)) {
347 - yield* Ref.set(currentEntry, updated.value);
348 - yield* requestBetterRouteCheck(lease);
349 - }
344 + yield* lease.session.subscribeServerConfig().pipe(
345 + Stream.filter((event) => event.type === "snapshot"),
346 + Stream.map((event) => event.config.directEndpoints),
347 + Stream.filter((reported) => reported !== undefined),
348 + Stream.changesWith((previous, next) => JSON.stringify(previous) === JSON.stringify(next)),
349 + Stream.runForEach((reported) =>
350 + Effect.gen(function* () {
351 + const updated = yield* learn({ activeRoute, reported });
352 + if (Option.isSome(updated)) {
353 + yield* Ref.set(currentEntry, updated.value);
354 + yield* requestBetterRouteCheck(lease);
355 + }
356 + }),
357 + ),
358 + Effect.ignoreCause({ log: true }),
359 + );
360 });
361
352 - const routeIndex = (lease: ConnectionDriver.EnvironmentConnectionLease) =>
362 + const routeIndex = (lease: ConnectedLease) =>
363 currentRoutes.pipe(
354 - Effect.map((routes) =>
355 - routes.findIndex(
356 - (route) => connectionRouteId(route.target) === connectionRouteId(lease.prepared.target),
357 - ),
358 - ),
364 + Effect.map((routes) => {
365 + const activeId = connectionRouteId(lease.prepared.target);
366 + const index = routes.findIndex((route) => connectionRouteId(route.target) === activeId);
367 + return index === -1 && lease.activeRoute !== undefined
368 + ? insertRoute(routes, lease.activeRoute).findIndex(
369 + (route) => connectionRouteId(route.target) === activeId,
370 + )
371 + : index;
372 + }),
373 );
374
361 - const checkBetterRoutes = Effect.fnUntraced(function* (
362 - lease: ConnectionDriver.EnvironmentConnectionLease,
363 - ) {
375 + const checkBetterRoutes = Effect.fnUntraced(function* (lease: ConnectedLease) {
376 const latest = yield* Ref.get(currentEntry);
377 const current = yield* routeIndex(lease);
378 if (current <= 0) return;
@@ -398,8 +410,8 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
410 });
411 });
412
401 - const betterRouteChecks = yield* Queue.dropping<ConnectionDriver.EnvironmentConnectionLease>(1);
402 - const requestBetterRouteCheck = (lease: ConnectionDriver.EnvironmentConnectionLease) =>
413 + const betterRouteChecks = yield* Queue.dropping<ConnectedLease>(1);
414 + const requestBetterRouteCheck = (lease: ConnectedLease) =>
415 routeIndex(lease).pipe(
416 Effect.flatMap((index) =>
417 index > 0 ? Queue.offer(betterRouteChecks, lease).pipe(Effect.asVoid) : Effect.void,
@@ -665,7 +677,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
677 // event, and "alive" once the socket delivers a frame. A slow or
678 // unreachable host never ends it.
679 const awaitSilentSocket = Effect.fn("EnvironmentSupervisor.awaitSilentSocket")(function* (
668 - lease: ConnectionDriver.EnvironmentConnectionLease,
680 + lease: ConnectedLease,
681 routeKind: ConnectionRouteKind | undefined,
682 ) {
683 const startedAt = yield* Clock.monotonicTimeNanos;
@@ -698,9 +710,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
710 // all probe the live session instead of replacing it, so a healthy socket is
711 // not torn down (the offline report is often wrong, for example for a
712 // loopback server).
701 - const monitorConnectedLease = Effect.fnUntraced(function* (
702 - lease: ConnectionDriver.EnvironmentConnectionLease,
703 - ) {
713 + const monitorConnectedLease = Effect.fnUntraced(function* (lease: ConnectedLease) {
714 // A probe answers an explicit retry here, so the retry must not also reset
715 // the backoff of a later, unrelated failure.
716 const takeSignal = Queue.take(signals).pipe(
@@ -938,11 +948,18 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
948 } satisfies AttemptOutcome;
949 }
950
951 + const connectedLease: ConnectedLease = {
952 + ...active.lease,
953 + activeRoute: (yield* currentRoutes).find(
954 + (route) =>
955 + connectionRouteId(route.target) === connectionRouteId(active.lease.prepared.target),
956 + ),
957 + };
958 const connectedAt = yield* Clock.currentTimeMillis;
942 - yield* SubscriptionRef.set(prepared, Option.some(active.lease.prepared));
943 - yield* SubscriptionRef.set(session, Option.some(active.lease.session));
959 + yield* SubscriptionRef.set(prepared, Option.some(connectedLease.prepared));
960 + yield* SubscriptionRef.set(session, Option.some(connectedLease.session));
961 if (options?.learnRoutes !== undefined) {
945 - yield* learnRoutesFrom(options.learnRoutes, active.lease).pipe(Effect.forkScoped);
962 + yield* learnRoutesFrom(options.learnRoutes, connectedLease).pipe(Effect.forkScoped);
963 }
964 yield* setState({
965 desired: true,
@@ -956,13 +973,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
973 });
974
975 const connectedExit = yield* Effect.raceFirst(
959 - active.lease.session.closed.pipe(
976 + connectedLease.session.closed.pipe(
977 Effect.mapError((error): TracedAttemptFailure => ({
978 error,
979 attemptSpan: active.attemptSpan,
980 })),
981 ),
965 - monitorConnectedLease(active.lease).pipe(
982 + monitorConnectedLease(connectedLease).pipe(
983 Effect.mapError((error): TracedAttemptFailure => ({
984 error,
985 attemptSpan: active.attemptSpan,
packages/client-runtime/src/operations/commands.performance.test.ts
+1 -1
@@ -174,7 +174,7 @@ const makeSupervisor = Effect.fn("CommandPerformance.makeSupervisor")(function*
174 },
175 },
176 } as never),
177 - subscribeServerConfig: (requestInput) => client.subscribeServerConfig(requestInput),
177 + subscribeServerConfig: (requestInput = {}) => client.subscribeServerConfig(requestInput),
178 ready: Effect.void,
179 probe: Effect.void,
180 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/operations/commands.test.ts
+1 -1
@@ -116,7 +116,7 @@ const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(funct
116 },
117 },
118 } as never),
119 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
119 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
120 ready: Effect.void,
121 probe: Effect.void,
122 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/rpc/client.test.ts
+1 -1
@@ -48,7 +48,7 @@ function session(client: WsRpcProtocolClient): RpcSession.RpcSession {
48 return {
49 client,
50 initialConfig: Effect.never,
51 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
51 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
52 ready: Effect.void,
53 probe: Effect.void,
54 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/rpc/session.test.ts
+1 -1
@@ -447,7 +447,7 @@ describe("RpcSessionFactory", () => {
447 yield* completeInitialConfig(socket, encodeServerConfig(SOURCE_SERVER_CONFIG), options);
448 yield* Fiber.join(ready);
449 const observed = yield* Queue.unbounded<ServerConfigStreamEventType>();
450 - yield* session.subscribeServerConfig(options).pipe(
450 + yield* session.subscribeServerConfig().pipe(
451 Stream.runForEach((event) => Queue.offer(observed, event)),
452 Effect.forkChild,
453 );
packages/client-runtime/src/rpc/session.ts
+2 -2
@@ -49,7 +49,7 @@ export interface RpcSession {
49 readonly client: WsRpcProtocolClient;
50 readonly initialConfig: Effect.Effect<ServerConfig, ConnectionAttemptError>;
51 readonly subscribeServerConfig: (
52 - input: ServerConfigSubscriptionInput,
52 + input?: ServerConfigSubscriptionInput,
53 ) => ServerConfigSubscription;
54 readonly ready: Effect.Effect<void, ConnectionAttemptError>;
55 readonly probe: Effect.Effect<void, ConnectionAttemptError>;
@@ -394,7 +394,7 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
394 }),
395 ),
396 );
397 - const subscribeServerConfig = (input: ServerConfigSubscriptionInput) =>
397 + const subscribeServerConfig = (input: ServerConfigSubscriptionInput = serverConfigInput) =>
398 Stream.unwrap(
399 validatedInitialConfig.pipe(
400 Effect.as(
packages/client-runtime/src/state/pullRequests.test.ts
+1 -1
@@ -510,7 +510,7 @@ function session(client: WsRpcProtocolClient): RpcSession {
510 client[WS_METHODS.pullRequestsInvalidate] ?? (() => Effect.void),
511 },
512 initialConfig: client[WS_METHODS.serverGetConfig]?.({}).pipe(Effect.orDie) ?? Effect.never,
513 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
513 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
514 ready: Effect.void,
515 probe: Effect.void,
516 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/state/server.test.ts
+1 -1
@@ -95,7 +95,7 @@ function session(client: WsRpcProtocolClient): RpcSession {
95 return {
96 client,
97 initialConfig: Effect.succeed(CONFIG),
98 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
98 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
99 ready: Effect.void,
100 probe: Effect.void,
101 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/state/serverUsage.test.ts
+1 -1
@@ -132,7 +132,7 @@ const makeHarness = Effect.fn("ServerUsageTest.makeHarness")(function* (
132 const session: RpcSession = {
133 client,
134 initialConfig: Effect.succeed(CONFIG),
135 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
135 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
136 ready: Effect.void,
137 probe: Effect.void,
138 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/state/shell-sync.test.ts
+1 -1
@@ -57,7 +57,7 @@ function session(client: WsRpcProtocolClient): RpcSession.RpcSession {
57 return {
58 client,
59 initialConfig: Effect.succeed({ shellResumeCompletionMarker: true } as never),
60 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
60 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
61 ready: Effect.void,
62 probe: Effect.void,
63 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/state/threads-atoms.test.ts
+1 -1
@@ -97,7 +97,7 @@ const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options?
97 threadResumeCompletionMarker: true,
98 threadSnapshotPagination: true,
99 } as never),
100 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
100 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
101 ready: Effect.void,
102 probe: Effect.void,
103 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/state/threads-sync.test.ts
+1 -1
@@ -85,7 +85,7 @@ function testSession(client: WsRpcProtocolClient): RpcSession.RpcSession {
85 return {
86 client,
87 initialConfig: Effect.succeed({} as never),
88 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
88 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
89 ready: Effect.void,
90 probe: Effect.void,
91 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/state/vcs.test.ts
+1 -1
@@ -93,7 +93,7 @@ function session(client: WsRpcProtocolClient): RpcSession {
93 return {
94 client,
95 initialConfig: Effect.never,
96 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
96 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
97 ready: Effect.void,
98 probe: Effect.void,
99 inboundFrames: Effect.succeed(0),
packages/client-runtime/src/state/vcsAction.test.ts
+1 -1
@@ -86,7 +86,7 @@ function session(client: WsRpcProtocolClient): RpcSession {
86 return {
87 client,
88 initialConfig: Effect.never,
89 - subscribeServerConfig: (input) => client.subscribeServerConfig(input),
89 + subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
90 ready: Effect.void,
91 probe: Effect.void,
92 inboundFrames: Effect.succeed(0),