TypeScript
fix(client-runtime): incompatible hosts no longer switch environments off for good (#15052)
Vitaly Iegorov02b794c78211
diff --git a/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx b/apps/mobile/src/features/threads/NewTaskRouteScreen.tsxindex acbb805c5be..701d641b79f 100644--- a/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx+++ b/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx@@ -70,11 +70,15 @@ function deriveProjectEmptyState(catalogState: WorkspaceState): {if ((catalogState.connectionState === "available" ||catalogState.connectionState === "offline" ||- catalogState.connectionState === "error") &&+ catalogState.connectionState === "error" ||+ catalogState.connectionState === "unsupported") &&!catalogState.hasLoadedShellSnapshot) {return {- title: "Environment unavailable",+ title:+ catalogState.connectionState === "unsupported"+ ? "Client not supported"+ : "Environment unavailable",detail:catalogState.connectionError ??"The saved environment is offline. Check the URL or start the environment, then retry.",diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsxindex 364475abc10..fe02e5be7b4 100644--- a/apps/web/src/components/settings/ConnectionsSettings.tsx+++ b/apps/web/src/components/settings/ConnectionsSettings.tsx@@ -2075,7 +2075,7 @@ export function ConnectionsSettings() {const loadBalancingEnvironments = [...(primaryEnvironment ? [primaryEnvironment] : []),...savedEnvironments.filter((environment) => environment.entry.enabled),- ];+ ].filter((environment) => environment.connection.phase !== "unsupported");const savedDesktopSshEnvironmentKeys = (() => {const keys = new Set<string>();for (const environment of savedEnvironments) {diff --git a/packages/client-runtime/src/connection/onboarding.test.ts b/packages/client-runtime/src/connection/onboarding.test.tsindex 5e1dcd72222..ce1c63ed71a 100644--- a/packages/client-runtime/src/connection/onboarding.test.ts+++ b/packages/client-runtime/src/connection/onboarding.test.ts@@ -368,7 +368,10 @@ describe("connection onboarding", () => {);yield* registry.setCompatibility(PAIRED_ENVIRONMENT_ID, UNSUPPORTED_ERROR);const before = yield* SubscriptionRef.get(registry.entries);- expect(before.get(PAIRED_ENVIRONMENT_ID)?.enabled).toBe(false);+ expect(before.get(PAIRED_ENVIRONMENT_ID)).toMatchObject({+ enabled: true,+ unsupportedReason: UNSUPPORTED_ERROR.message,+ });const environmentId = yield* onboarding.registerPairing(PAIRING_INPUT);diff --git a/packages/client-runtime/src/connection/registry.test.ts b/packages/client-runtime/src/connection/registry.test.tsindex 35705a58188..b4fd5df7c36 100644--- a/packages/client-runtime/src/connection/registry.test.ts+++ b/packages/client-runtime/src/connection/registry.test.ts@@ -987,7 +987,7 @@ describe("EnvironmentRegistry", () => {yield* Deferred.await(relayChecked);const entries = yield* SubscriptionRef.get(registry.entries);- expect(entries.get(RELAY_TARGET.environmentId)).toMatchObject({ enabled: false });+ expect(entries.get(RELAY_TARGET.environmentId)?.unsupportedReason).toBeDefined();expect(entries.get(TARGET.environmentId)).toMatchObject({ enabled: true });expect(entries.get(TARGET.environmentId)?.unsupportedReason).toBeUndefined();expect((yield* registry.state(TARGET.environmentId)).phase).toBe("connected");@@ -996,7 +996,7 @@ describe("EnvironmentRegistry", () => {);it.effect.each([false, true])(- "discovery keeps unsupported environments off (learned route: %s)",+ "a switched-off environment stays off after compatibility clears (learned route: %s)",(learned) =>Effect.gen(function* () {const environmentId = BEARER_TARGET.environmentId;@@ -1030,6 +1030,9 @@ describe("EnvironmentRegistry", () => {expect((yield* SubscriptionRef.get(registry.entries)).get(environmentId)?.enabled).toBe(false,);+ expect(yield* registry.state(environmentId)).toMatchObject({ desired: false });+ expect(yield* Ref.get(harness.storedDisabled)).toEqual(new Set([environmentId]));+ expect(yield* Ref.get(harness.sessions)).toHaveLength(0);yield* registry.setEnabled(environmentId, true);yield* awaitConnectionState(registry,@@ -1040,28 +1043,68 @@ describe("EnvironmentRegistry", () => {}),);- it.effect("a socket preflight rejection persists the connection as switched off", () =>+ it.effect("protocol rejection preserves enablement through recovery and relaunch", () =>Effect.gen(function* () {const error = new ConnectionBlockedError({reason: "unsupported",detail: "Use a compatible client.",});- const harness = yield* makeHarness([RELAY_TARGET], [], [], { prepareError: error });+ const rejection = yield* Ref.make<ConnectionBlockedError | undefined>(error);+ const harness = yield* makeHarness([RELAY_TARGET], [], [], {+ beforePrepareRoute: () => Ref.get(rejection),+ });yield* Effect.gen(function* () {const registry = yield* EnvironmentRegistry.EnvironmentRegistry;yield* registry.start;yield* SubscriptionRef.changes(registry.entries).pipe(- Stream.filter((entries) => entries.get(RELAY_TARGET.environmentId)?.enabled === false),+ Stream.filter(+ (entries) => entries.get(RELAY_TARGET.environmentId)?.unsupportedReason !== undefined,+ ),Stream.take(1),Stream.runDrain,);- expect((yield* Ref.get(harness.storedDisabled)).has(RELAY_TARGET.environmentId)).toBe(true);+ expect(yield* Ref.get(harness.storedDisabled)).toEqual(new Set());expect(- (yield* SubscriptionRef.get(registry.entries)).get(RELAY_TARGET.environmentId)- ?.unsupportedReason,- ).toBe(error.message);+ (yield* SubscriptionRef.get(registry.entries)).get(RELAY_TARGET.environmentId),+ ).toMatchObject({ enabled: true, unsupportedReason: error.message });+ yield* awaitConnectionState(+ registry,+ RELAY_TARGET.environmentId,+ (state) => !state.desired && state.phase === "available",+ );expect(yield* Ref.get(harness.sessions)).toHaveLength(0);- }).pipe(Effect.provide(harness.layer));++ yield* Ref.set(rejection, undefined);+ yield* registry.setCompatibility(RELAY_TARGET.environmentId, null);+ yield* awaitConnectionState(+ registry,+ RELAY_TARGET.environmentId,+ (state) => state.phase === "connected",+ );+ expect(yield* Ref.get(harness.sessions)).toHaveLength(1);+ yield* registry.setCompatibility(RELAY_TARGET.environmentId, error);+ yield* awaitConnectionState(+ registry,+ RELAY_TARGET.environmentId,+ (state) => !state.desired && state.phase === "available",+ );+ expect(yield* Ref.get(harness.releasedSessions)).toBe(1);+ }).pipe(Effect.provide(harness.layer), Effect.scoped);++ yield* Effect.gen(function* () {+ const registry = yield* EnvironmentRegistry.EnvironmentRegistry;+ const entry = (yield* SubscriptionRef.get(registry.entries)).get(+ RELAY_TARGET.environmentId,+ );+ expect(entry?.enabled).toBe(true);+ expect(entry?.unsupportedReason).toBeUndefined();+ yield* registry.start;+ yield* awaitConnectionState(+ registry,+ RELAY_TARGET.environmentId,+ (state) => state.phase === "connected",+ );+ }).pipe(Effect.provide(harness.layer), Effect.scoped);}),);@@ -1100,16 +1143,16 @@ describe("EnvironmentRegistry", () => {yield* Effect.gen(function* () {const registry = yield* EnvironmentRegistry.EnvironmentRegistry;yield* registry.start;- const awaitDisabled = SubscriptionRef.changes(registry.entries).pipe(- Stream.filter((entries) => entries.get(environmentId)?.enabled === false),+ const awaitUnsupported = SubscriptionRef.changes(registry.entries).pipe(+ Stream.filter((entries) => entries.get(environmentId)?.unsupportedReason !== undefined),Stream.take(1),Stream.runDrain,);- yield* awaitDisabled;- expect((yield* Ref.get(harness.storedDisabled)).has(environmentId)).toBe(true);+ yield* awaitUnsupported;+ expect((yield* Ref.get(harness.storedDisabled)).has(environmentId)).toBe(false);expect((yield* SubscriptionRef.get(registry.entries)).get(environmentId)).toMatchObject({target: relayFirst ? relay : BEARER_TARGET,- enabled: false,+ enabled: true,unsupportedReason: error.message,serverUpdateRequired: true,});@@ -1132,13 +1175,13 @@ describe("EnvironmentRegistry", () => {);expect(yield* Ref.get(harness.sessions)).toHaveLength(1);} else {- yield* awaitDisabled;+ yield* awaitUnsupported;expect((yield* SubscriptionRef.get(registry.entries)).get(environmentId)).toMatchObject({- enabled: false,+ enabled: true,unsupportedReason: error.message,serverUpdateRequired: true,});- expect((yield* Ref.get(harness.storedDisabled)).has(environmentId)).toBe(true);+ expect((yield* Ref.get(harness.storedDisabled)).has(environmentId)).toBe(false);expect(yield* Ref.get(harness.sessions)).toHaveLength(0);}expect(yield* Ref.get(attempts)).toBe(2);@@ -1808,7 +1851,16 @@ describe("EnvironmentRegistry", () => {yield* registry.reconcilePlatform([registration]);expect((yield* SubscriptionRef.get(registry.entries)).get(TARGET.environmentId),- ).toMatchObject({ enabled: false, unsupportedReason: error.message });+ ).toMatchObject({ enabled: true, unsupportedReason: error.message });+ yield* registry.registerPlatform(+ new PrimaryConnectionRegistration({+ target: new PrimaryConnectionTarget({ ...TARGET, label: "Renamed" }),+ }),+ );+ expect(yield* registry.state(TARGET.environmentId)).toMatchObject({+ desired: false,+ phase: "available",+ });expect(yield* Ref.get(harness.sessions)).toHaveLength(1);yield* registry.registerPlatform(new PrimaryConnectionRegistration({diff --git a/packages/client-runtime/src/connection/registry.ts b/packages/client-runtime/src/connection/registry.tsindex 32ad38d1f08..76c49bc86ea 100644--- a/packages/client-runtime/src/connection/registry.ts+++ b/packages/client-runtime/src/connection/registry.ts@@ -357,10 +357,9 @@ export const make = Effect.gen(function* () {const environmentId = entry.target.environmentId;const scope = yield* Scope.fork(registryScope);const supervisor = yield* EnvironmentSupervisor.make(entry, {- // An enabled environment starts out connecting. Starting it idle and- // then asking it to connect would first publish the state of an- // environment the user switched off.- initiallyDesired: entry.enabled,+ // Seed the intent to avoid publishing a false switched-off state+ // before an enabled, compatible environment starts connecting.+ initiallyDesired: entry.enabled && entry.unsupportedReason === undefined,learnRoutes: (input) => learnRoutes({ environmentId, ...input }),}).pipe(Effect.provideService(Connectivity.Connectivity, connectivity),@@ -377,14 +376,7 @@ export const make = Effect.gen(function* () {yield* SubscriptionRef.changes(supervisor.state).pipe(Stream.runForEach((state) =>state.phase === "blocked" && state.lastFailure?.reason === "unsupported"- ? setCompatibility(environmentId, state.lastFailure).pipe(- Effect.catch((error) =>- Effect.logWarning("Could not disable an unsupported environment.", {- environmentId,- error,- }),- ),- )+ ? setCompatibility(environmentId, state.lastFailure): Effect.void,),Effect.forkIn(scope),@@ -613,7 +605,7 @@ export const make = Effect.gen(function* () {const entry: ConnectionCatalogEntry =previous?.unsupportedReason !== undefined &&gitHubRoutingConnectionKey(previous) === gitHubRoutingConnectionKey(registered)- ? { ...registered, enabled: false, ...unsupportedState(previous) }+ ? { ...registered, ...unsupportedState(previous) }: registered;const persisted = (yield* Ref.get(persistedEnvironmentIds)).has(target.environmentId);if (@@ -1118,28 +1110,22 @@ export const make = Effect.gen(function* () {serverUpdateRequired: _previousUpdateRequired,...rest} = entry;+ // Compatibility pauses connections without changing the user's saved on/off choice.const next: ConnectionCatalogEntry =error === null? rest: {...rest,- enabled: false,unsupportedReason: error.message,...(error.serverUpdateRequired === true ? { serverUpdateRequired: true } : {}),};- if (- error !== null &&- entry.enabled &&- !(yield* Ref.get(platformEnvironmentIds)).has(environmentId)- ) {- yield* registrations.setEnabled(environmentId, false);- }const lease = (yield* SubscriptionRef.get(serviceScopes)).get(environmentId);if (lease !== undefined) {yield* SubscriptionRef.update(serviceScopes, (current) =>new Map(current).set(environmentId, { ...lease, entry: next }),);if (error !== null) yield* lease.supervisor.disconnect;+ else if (next.enabled) yield* lease.supervisor.connect;}yield* SubscriptionRef.update(entries, (current) =>new Map(current).set(environmentId, next),diff --git a/packages/client-runtime/src/state/connections.ts b/packages/client-runtime/src/state/connections.tsindex d2117122c6a..686bcfd8315 100644--- a/packages/client-runtime/src/state/connections.ts+++ b/packages/client-runtime/src/state/connections.ts@@ -29,7 +29,7 @@ export function* enabledEnvironmentIds(catalog: EnvironmentCatalogState,): Generator<EnvironmentIdType> {for (const [environmentId, entry] of catalog.entries) {- if (entry.enabled) {+ if (entry.enabled && entry.unsupportedReason === undefined) {yield environmentId;}}diff --git a/packages/client-runtime/src/state/pullRequestRouting.ts b/packages/client-runtime/src/state/pullRequestRouting.tsindex 594e56760a7..8bdcb481c5f 100644--- a/packages/client-runtime/src/state/pullRequestRouting.ts+++ b/packages/client-runtime/src/state/pullRequestRouting.ts@@ -161,7 +161,13 @@ const routingAllowed = Effect.fn("PullRequestRouting.allowed")(function* (const entries = yield* SubscriptionRef.get(registry.entries);const origin = entries.get(originId);const destination = entries.get(destinationId);- if (!origin?.enabled || !destination?.enabled) return false;+ if (+ !origin?.enabled ||+ !destination?.enabled ||+ origin.unsupportedReason !== undefined ||+ destination.unsupportedReason !== undefined+ )+ return false;const permissions = yield* GitHubRoutingPermissions;const source = yield* permissions.get(origin);const target = yield* permissions.get(destination);diff --git a/packages/client-runtime/src/state/pullRequests.test.ts b/packages/client-runtime/src/state/pullRequests.test.tsindex 0ceaef7b94e..142847881b5 100644--- a/packages/client-runtime/src/state/pullRequests.test.ts+++ b/packages/client-runtime/src/state/pullRequests.test.ts@@ -754,8 +754,15 @@ const makeTestRuntime = Effect.fn("makeTestRuntime")(function* (return { runtime, atoms, registry, environmentRegistry, supervisor, localSupervisor };});-it.effect.each(["default", "origin-off", "destination-off", "read-only"] as const)(- "does not probe another environment with %s routing permission",+it.effect.each([+ "default",+ "origin-off",+ "destination-off",+ "read-only",+ "destination-disabled",+ "destination-unsupported",+] as const)(+ "preserves source requests when routing to another environment is blocked by %s",(permission) =>Effect.scoped(Effect.gen(function* () {@@ -765,9 +772,10 @@ it.effect.each(["default", "origin-off", "destination-off", "read-only"] as consdetail: "source failed",});const client = {- [WS_METHODS.pullRequestsSummary]: () =>+ [WS_METHODS.pullRequestsSummary]: (input: { allowStale?: boolean }) =>Effect.suspend(() => {calls.push("source-read");+ expect(input.allowStale).toBeUndefined();return Effect.fail(failure);}),[WS_METHODS.pullRequestsRunAction]: () =>@@ -779,7 +787,31 @@ it.effect.each(["default", "origin-off", "destination-off", "read-only"] as cons[WS_METHODS.pullRequestsRoutingIdentity]: () =>Effect.die("untrusted environment was contacted"),} as unknown as WsRpcProtocolClient;- const { environmentRegistry, supervisor } = yield* makeTestRuntime(client, alternate);+ const { environmentRegistry, supervisor, localSupervisor } = yield* makeTestRuntime(+ client,+ alternate,+ );+ if (permission === "destination-disabled" || permission === "destination-unsupported") {+ yield* SubscriptionRef.update(+ environmentRegistry.entries,+ (entries) =>+ new Map(+ [...entries].map(([id, entry]) => [+ id,+ id === TARGET.environmentId+ ? entry+ : {+ ...entry,+ enabled: permission !== "destination-disabled",+ ...(permission === "destination-unsupported"+ ? { unsupportedReason: "Protocol version mismatch" }+ : {}),+ },+ ]),+ ),+ );+ yield* SubscriptionRef.set(localSupervisor.session, Option.none());+ }const ref = {projectId: ProjectId.make("project-1"),repository: "private/repo",@@ -805,10 +837,13 @@ it.effect.each(["default", "origin-off", "destination-off", "read-only"] as consEffect.succeed(permission === "read-only"? "read"- : (entry.target.environmentId === TARGET.environmentId) ===- (permission === "origin-off")- ? "off"- : "read-write",+ : permission === "destination-disabled" ||+ permission === "destination-unsupported"+ ? "read-write"+ : (entry.target.environmentId === TARGET.environmentId) ===+ (permission === "origin-off")+ ? "off"+ : "read-write",),}),);diff --git a/packages/client-runtime/src/state/threadShell.test.ts b/packages/client-runtime/src/state/threadShell.test.tsindex 413a5c2b3f5..3739e494318 100644--- a/packages/client-runtime/src/state/threadShell.test.ts+++ b/packages/client-runtime/src/state/threadShell.test.ts@@ -215,7 +215,13 @@ describe("v2 thread shell lists", () => {registry.dispose();});- it("leaves switched-off environments out of navigation", () => {+ it.each([+ { reason: "switched-off", state: { enabled: false } },+ {+ reason: "unsupported",+ state: { enabled: true, unsupportedReason: "Use a compatible client." },+ },+ ])("leaves $reason environments out of navigation until restored", ({ state }) => {const { registry, threads, catalogValueAtom } = makeHarness([environmentId,remoteEnvironmentId,@@ -225,15 +231,18 @@ describe("v2 thread shell lists", () => {new Set(registry.get(threads.navigationThreadShellsAtom).map((t) => t.environmentId));expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));- const setRemoteEnabled = (enabled: boolean) => {+ const setRemotePaused = (paused: boolean) => {const catalog = registry.get(catalogValueAtom);const entries = new Map(catalog.entries);- entries.set(remoteEnvironmentId, { ...entries.get(remoteEnvironmentId)!, enabled });+ entries.set(remoteEnvironmentId, {+ ...entries.get(remoteEnvironmentId)!,+ ...(paused ? state : { enabled: true, unsupportedReason: undefined }),+ });registry.set(catalogValueAtom, { ...catalog, entries });};- setRemoteEnabled(false);+ setRemotePaused(true);expect(environmentsOf()).toEqual(new Set([environmentId]));- setRemoteEnabled(true);+ setRemotePaused(false);expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));dispose();registry.dispose();