Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
26b3d03
test(client-runtime): reproduce a failed direct route save
StiensWout Nov 29, 2026
42d9079
fix(client-runtime): retry saving repeated direct routes
StiensWout Nov 29, 2026
25ea5fe
fix(client-runtime): compare endpoint reports outside the save effect
StiensWout Nov 29, 2026
c862204
test(client-runtime): cover direct route save recovery
StiensWout Nov 29, 2026
312c3f7
test(client-runtime): assert recovery on the original relay session
StiensWout Nov 29, 2026
ba8d69a
fix(client-runtime): retry a failed route save while connected
StiensWout Nov 29, 2026
d1322c4
refactor(client-runtime): retry a failed route save with an uninterru…
StiensWout Nov 29, 2026
dd23166
test(client-runtime): cover repeated route save failures and teardown
StiensWout Nov 29, 2026
56291ee
fix(client-runtime): keep direct route save retries interruptible
StiensWout Nov 29, 2026
9e4333c
test(client-runtime): cover route saves during registry teardown
StiensWout Nov 29, 2026
c8aa9f4
fix(client-runtime): finish publishing an in-flight route save
StiensWout Nov 29, 2026
6eff803
test(client-runtime): type the route save lifecycle fixture
StiensWout Nov 29, 2026
e476625
test(client-runtime): move route save teardown cases onto the registr…
StiensWout Nov 29, 2026
31a571d
revert(client-runtime): keep route save teardown cases in their own file
StiensWout Nov 29, 2026
0a0ac27
Merge remote-tracking branch 'origin/main' into wf/find-3d2e2fa38c397…
juliusmarminge Nov 29, 2026
e65e5d3
test(client-runtime): run route save teardown cases on the registry t…
juliusmarminge Nov 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 186 additions & 9 deletions packages/client-runtime/src/connection/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ORCHESTRATION_PROTOCOL_VERSION,
type ExecutionEnvironmentDescriptor,
type ServerConfig,
type ServerConfigStreamEvent,
} from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as Context from "effect/Context";
Expand All @@ -21,6 +22,7 @@ import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import * as SubscriptionRef from "effect/SubscriptionRef";
import * as Tracer from "effect/Tracer";
import * as TestClock from "effect/testing/TestClock";

import * as ClientCapabilities from "../platform/capabilities.ts";
import * as TokenStore from "../authorization/tokenStore.ts";
Expand All @@ -42,7 +44,7 @@ import * as Connectivity from "./connectivity.ts";
import * as ConnectionCredentialStore from "./credentialStore.ts";
import * as ConnectionDriver from "./driver.ts";
import type { RouteCheck } from "./driver.ts";
import { connectionRouteId, connectionRouteKind } from "./routes.ts";
import { connectionRouteId, connectionRouteKind, connectionRoutes } from "./routes.ts";
import {
ConnectionTransientError,
ConnectionBlockedError,
Expand Down Expand Up @@ -168,6 +170,9 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
) => Effect.Effect<ConnectionBlockedError | undefined>;
readonly initialDisabled?: ReadonlyArray<EnvironmentId>;
readonly directEndpoints?: ServerConfig["directEndpoints"];
readonly configEvents?: Stream.Stream<ServerConfigStreamEvent>;
readonly beforeProfilePut?: Effect.Effect<void, ConnectionTransientError>;
readonly beforeSetRoutes?: Effect.Effect<void>;
},
) {
const storedTargets = yield* Ref.make<ReadonlyArray<ConnectionTarget>>(initialTargets);
Expand Down Expand Up @@ -235,7 +240,11 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
}
}),
setRoutes: (environmentId, routes) =>
Ref.update(storedTargets, (current) => replaceRoutes(current, environmentId, routes)),
(options?.beforeSetRoutes ?? Effect.void).pipe(
Effect.andThen(
Ref.update(storedTargets, (current) => replaceRoutes(current, environmentId, routes)),
),
),
remove: (environmentId) =>
Effect.gen(function* () {
yield* options?.beforeRegistrationRemove?.(environmentId) ?? Effect.void;
Expand Down Expand Up @@ -323,11 +332,15 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
Effect.map((current) => Option.fromUndefinedOr(current.get(connectionId))),
),
put: (profile) =>
Ref.update(storedProfiles, (current) => {
const next = new Map(current);
next.set(profile.connectionId, profile);
return next;
}),
(options?.beforeProfilePut ?? Effect.void).pipe(
Effect.andThen(
Ref.update(storedProfiles, (current) => {
const next = new Map(current);
next.set(profile.connectionId, profile);
return next;
}),
),
),
remove: (connectionId) =>
Ref.update(storedProfiles, (current) => {
const next = new Map(current);
Expand Down Expand Up @@ -419,13 +432,14 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
? Effect.die(new Error("Config is not used by registry tests."))
: Effect.succeed({ directEndpoints: options.directEndpoints } as ServerConfig),
subscribeServerConfig: () =>
options?.directEndpoints === undefined
options?.configEvents ??
(options?.directEndpoints === undefined
? Stream.never
: Stream.make({
version: 1 as const,
type: "snapshot" as const,
config: { directEndpoints: options.directEndpoints } as ServerConfig,
}),
})),
ready: Effect.void,
probe: Effect.void,
inboundFrames: Effect.succeed(0),
Expand Down Expand Up @@ -2406,6 +2420,169 @@ describe("EnvironmentRegistry routes", () => {
}),
);

it.effect(
"saves a direct route when a later snapshot repeats an endpoint after a write failure",
() =>
Effect.gen(function* () {
const storageAvailable = yield* Deferred.make<void>();
const firstSnapshotProcessed = yield* Deferred.make<void>();
const snapshotsProcessed = yield* Deferred.make<void>();
const httpBaseUrl = "https://devbox.tail1234.ts.net/";
const snapshot = {
version: 1,
type: "snapshot",
config: {
availableEditors: [],
directEndpoints: [{ kind: "tailnet", httpBaseUrl }],
} as unknown as ServerConfig,
} satisfies ServerConfigStreamEvent;
const harness = yield* makeHarness([RELAY_TARGET], [], [], {
beforeProfilePut: Deferred.isDone(storageAvailable).pipe(
Effect.flatMap((available) =>
available
? Effect.void
: Effect.fail(
new ConnectionTransientError({
reason: "remote-unavailable",
detail: "Temporary storage failure",
}),
),
),
),
configEvents: Stream.concat(
Stream.make(snapshot),
Stream.fromEffect(
Deferred.succeed(firstSnapshotProcessed, undefined).pipe(
Effect.andThen(Deferred.await(storageAvailable)),
Effect.as({
...snapshot,
config: { ...snapshot.config, availableEditors: ["file-manager"] },
} satisfies ServerConfigStreamEvent),
),
),
).pipe(Stream.ensuring(Deferred.succeed(snapshotsProcessed, undefined))),
});
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
yield* registry.start;
yield* Deferred.await(firstSnapshotProcessed);
expect(yield* Ref.get(harness.storedTargets)).toEqual([RELAY_TARGET]);
yield* Deferred.succeed(storageAvailable, undefined);
yield* Deferred.await(snapshotsProcessed);
const routes = routesOf(
yield* Ref.get(harness.storedTargets),
RELAY_TARGET.environmentId,
);
expect(routes.length).toBe(2);
const learned = routes.find((route) => route._tag === "BearerConnectionTarget");
expect(learned).toMatchObject({ environmentId: RELAY_TARGET.environmentId });
expect((yield* Ref.get(harness.storedProfiles)).get(learned!.connectionId)).toMatchObject(
{
httpBaseUrl,
learned: true,
network: "tailscale",
},
);
}).pipe(Effect.provide(harness.layer));
}),
);

it.effect.each(["disconnect", "remove"] as const)(
"%s during a learned route save retry",
(action) =>
Effect.gen(function* () {
const publish = yield* Deferred.make<void>();
const firstSaveFinished = yield* Deferred.make<void>();
const storageStarted = yield* Deferred.make<void>();
const finishStorage = yield* Deferred.make<void>();
const removalStarted = yield* Deferred.make<void>();
const finishRemoval = yield* Deferred.make<void>();
const attempts = yield* Ref.make(0);
const harness = yield* makeHarness([RELAY_TARGET], [], [], {
configEvents: Stream.fromEffect(
Deferred.await(publish).pipe(
Effect.as({
version: 1,
type: "snapshot",
config: {
directEndpoints: [
{ kind: "tailnet", httpBaseUrl: "https://devbox.tail1234.ts.net/" },
],
} as unknown as ServerConfig,
} satisfies ServerConfigStreamEvent),
),
).pipe(
Stream.concat(
Stream.fromEffect(Deferred.succeed(firstSaveFinished, undefined)).pipe(Stream.drain),
),
Stream.concat(Stream.never),
),
beforeProfilePut: Ref.updateAndGet(attempts, (count) => count + 1).pipe(
Effect.flatMap((count) =>
count === 1
? Effect.fail(
new ConnectionTransientError({
reason: "remote-unavailable",
detail: "Temporary storage failure",
}),
)
: Effect.void,
),
),
beforeSetRoutes: Deferred.succeed(storageStarted, undefined).pipe(
Effect.andThen(Deferred.await(finishStorage)),
),
beforeRegistrationRemove: () =>
Deferred.succeed(removalStarted, undefined).pipe(
Effect.andThen(Deferred.await(finishRemoval)),
),
});
yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
yield* registry.start;
if (action === "remove") {
yield* Deferred.succeed(publish, undefined);
yield* Deferred.await(firstSaveFinished);
const removal = yield* registry
.remove(RELAY_TARGET.environmentId)
.pipe(Effect.forkChild);
yield* Deferred.await(removalStarted);
yield* TestClock.adjust("2 seconds");
yield* Deferred.succeed(finishRemoval, undefined);
yield* Fiber.join(removal);
expect(
(yield* SubscriptionRef.get(registry.entries)).has(RELAY_TARGET.environmentId),
).toBe(false);
expect(yield* Ref.get(harness.storedTargets)).toEqual([]);
return;
}

yield* registry.run(
RELAY_TARGET.environmentId,
Effect.gen(function* () {
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
yield* Deferred.succeed(publish, undefined);
yield* Deferred.await(firstSaveFinished);
yield* TestClock.adjust("2 seconds");
yield* Deferred.await(storageStarted);
yield* supervisor.disconnect;
yield* TestClock.adjust(0);
yield* Deferred.succeed(finishStorage, undefined);
yield* TestClock.adjust(0);
}),
);
const entry = (yield* SubscriptionRef.get(registry.entries)).get(
RELAY_TARGET.environmentId,
);
expect(entry && connectionRoutes(entry).map((route) => route.target)).toEqual(
yield* Ref.get(harness.storedTargets),
);
expect(yield* Ref.get(harness.storedTargets)).toHaveLength(2);
expect(yield* Ref.get(harness.releasedSessions)).toBe(1);
}).pipe(Effect.provide(harness.layer));
}),
);

it.effect("saves the Tailscale mark when the server reports a learned address as tailnet", () =>
Effect.gen(function* () {
const httpBaseUrl = "http://100.101.102.103:3773/";
Expand Down
8 changes: 5 additions & 3 deletions packages/client-runtime/src/connection/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,8 @@ export const make = Effect.gen(function* () {
readonly activeRoute: ConnectionRoute;
readonly reported: ReadonlyArray<ReportedEndpoint>;
}) {
// Keep lock acquisition interruptible. Once locked, publish the result of a
// catalog write even if the session closes while desktop IPC is pending.
return yield* withLeaseLock(
input.environmentId,
Effect.gen(function* () {
Expand Down Expand Up @@ -997,13 +999,13 @@ export const make = Effect.gen(function* () {
new Map(current).set(input.environmentId, next),
);
return Option.some(next);
}),
}).pipe(Effect.uninterruptible),
).pipe(
Effect.catch((error) =>
Effect.tapError((error) =>
Effect.logWarning("Could not save routes learned from the environment.", {
environmentId: input.environmentId,
error,
}).pipe(Effect.as(Option.none<ConnectionCatalogEntry>())),
}),
),
);
});
Expand Down
47 changes: 45 additions & 2 deletions packages/client-runtime/src/connection/supervisor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2852,7 +2852,16 @@ describe("EnvironmentSupervisor routes", () => {
}),
);

it.effect.each(["initial", "late", "withdrawn", "withdrawn-upgrade", "omitted"] as const)(
const directEndpointReports = [
"initial",
"late",
"withdrawn",
"withdrawn-upgrade",
"omitted",
"retry",
"retry-disconnect",
] as const;
it.effect.each(directEndpointReports)(
"learns the %s direct endpoint over T3 Connect",
(discovery) =>
Effect.gen(function* () {
Expand Down Expand Up @@ -2885,6 +2894,10 @@ describe("EnvironmentSupervisor routes", () => {
const discoveryProcessed = yield* Deferred.make<void>();
const discoveryStarted = yield* Deferred.make<void>();
const finishDiscovery = yield* Deferred.make<void>();
const retrying = discovery === "retry" || discovery === "retry-disconnect";
const saveAttempts = yield* Ref.make(0);
const retryWaiting = yield* Deferred.make<void>();
const releaseSaveLock = yield* Deferred.make<void>();
const lateAddress = "https://fixture.tail1234.ts.net/";
const learned = yield* Ref.make<ReadonlyArray<string>>([]);
const harness = yield* makeHarness({
Expand All @@ -2909,7 +2922,10 @@ describe("EnvironmentSupervisor routes", () => {
}),
),
),
).pipe(Stream.ensuring(Deferred.succeed(discoveryProcessed, undefined)))
).pipe(
Stream.concat(retrying ? Stream.never : Stream.empty),
Stream.ensuring(Deferred.succeed(discoveryProcessed, undefined)),
)
: undefined,
checkRoute: (route) =>
route.target._tag === "BearerConnectionTarget"
Expand Down Expand Up @@ -2938,6 +2954,16 @@ describe("EnvironmentSupervisor routes", () => {
allowInsecure: true,
});
if (routes === null) return Option.none();
if (retrying) {
const attempt = yield* Ref.updateAndGet(saveAttempts, (count) => count + 1);
if (attempt === 1 || (attempt === 2 && discovery === "retry")) {
return yield* transient("Temporary storage failure.");
}
if (discovery === "retry-disconnect") {
yield* Deferred.succeed(retryWaiting, undefined);
yield* Deferred.await(releaseSaveLock);
}
}
const next = entryWithRoutes(entry, routes);
yield* Ref.set(current, next);
yield* Ref.update(learned, (all) => [
Expand All @@ -2956,6 +2982,23 @@ describe("EnvironmentSupervisor routes", () => {
yield* Deferred.await(discoveryStarted);
}
yield* Deferred.succeed(finishDiscovery, undefined);
if (discovery === "retry") {
yield* TestClock.adjust("5 seconds");
expect(yield* Ref.get(learned)).toContain(
`learned:${TARGET.environmentId}:https://fixture.tail1234.ts.net`,
);
return;
}
if (discovery === "retry-disconnect") {
yield* TestClock.adjust("2 seconds");
yield* Deferred.await(retryWaiting);
yield* supervisor.disconnect;
yield* TestClock.adjust(0);
const released = yield* Ref.get(harness.releaseCount);
yield* Deferred.succeed(releaseSaveLock, undefined);
expect(released).toBe(1);
return;
}
if (discovery === "withdrawn-upgrade") {
yield* Deferred.await(discoveryProcessed);
yield* TestClock.adjust("60 seconds");
Expand Down
Loading
Loading