Skip to content

Commit 02b794c

Browse files
vitalyiegorovclaudejuliusmarminge
authored
fix(client-runtime): incompatible hosts no longer switch environments off for good (pingdotgg#15052)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent a71a6c3 commit 02b794c

9 files changed

Lines changed: 154 additions & 59 deletions

File tree

‎apps/mobile/src/features/threads/NewTaskRouteScreen.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ function deriveProjectEmptyState(catalogState: WorkspaceState): {
7070
if (
7171
(catalogState.connectionState === "available" ||
7272
catalogState.connectionState === "offline" ||
73-
catalogState.connectionState === "error") &&
73+
catalogState.connectionState === "error" ||
74+
catalogState.connectionState === "unsupported") &&
7475
!catalogState.hasLoadedShellSnapshot
7576
) {
7677
return {
77-
title: "Environment unavailable",
78+
title:
79+
catalogState.connectionState === "unsupported"
80+
? "Client not supported"
81+
: "Environment unavailable",
7882
detail:
7983
catalogState.connectionError ??
8084
"The saved environment is offline. Check the URL or start the environment, then retry.",

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2075,7 +2075,7 @@ export function ConnectionsSettings() {
20752075
const loadBalancingEnvironments = [
20762076
...(primaryEnvironment ? [primaryEnvironment] : []),
20772077
...savedEnvironments.filter((environment) => environment.entry.enabled),
2078-
];
2078+
].filter((environment) => environment.connection.phase !== "unsupported");
20792079
const savedDesktopSshEnvironmentKeys = (() => {
20802080
const keys = new Set<string>();
20812081
for (const environment of savedEnvironments) {

‎packages/client-runtime/src/connection/onboarding.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,10 @@ describe("connection onboarding", () => {
368368
);
369369
yield* registry.setCompatibility(PAIRED_ENVIRONMENT_ID, UNSUPPORTED_ERROR);
370370
const before = yield* SubscriptionRef.get(registry.entries);
371-
expect(before.get(PAIRED_ENVIRONMENT_ID)?.enabled).toBe(false);
371+
expect(before.get(PAIRED_ENVIRONMENT_ID)).toMatchObject({
372+
enabled: true,
373+
unsupportedReason: UNSUPPORTED_ERROR.message,
374+
});
372375

373376
const environmentId = yield* onboarding.registerPairing(PAIRING_INPUT);
374377

‎packages/client-runtime/src/connection/registry.test.ts‎

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ describe("EnvironmentRegistry", () => {
987987
yield* Deferred.await(relayChecked);
988988

989989
const entries = yield* SubscriptionRef.get(registry.entries);
990-
expect(entries.get(RELAY_TARGET.environmentId)).toMatchObject({ enabled: false });
990+
expect(entries.get(RELAY_TARGET.environmentId)?.unsupportedReason).toBeDefined();
991991
expect(entries.get(TARGET.environmentId)).toMatchObject({ enabled: true });
992992
expect(entries.get(TARGET.environmentId)?.unsupportedReason).toBeUndefined();
993993
expect((yield* registry.state(TARGET.environmentId)).phase).toBe("connected");
@@ -996,7 +996,7 @@ describe("EnvironmentRegistry", () => {
996996
);
997997

998998
it.effect.each([false, true])(
999-
"discovery keeps unsupported environments off (learned route: %s)",
999+
"a switched-off environment stays off after compatibility clears (learned route: %s)",
10001000
(learned) =>
10011001
Effect.gen(function* () {
10021002
const environmentId = BEARER_TARGET.environmentId;
@@ -1030,6 +1030,9 @@ describe("EnvironmentRegistry", () => {
10301030
expect((yield* SubscriptionRef.get(registry.entries)).get(environmentId)?.enabled).toBe(
10311031
false,
10321032
);
1033+
expect(yield* registry.state(environmentId)).toMatchObject({ desired: false });
1034+
expect(yield* Ref.get(harness.storedDisabled)).toEqual(new Set([environmentId]));
1035+
expect(yield* Ref.get(harness.sessions)).toHaveLength(0);
10331036
yield* registry.setEnabled(environmentId, true);
10341037
yield* awaitConnectionState(
10351038
registry,
@@ -1040,28 +1043,68 @@ describe("EnvironmentRegistry", () => {
10401043
}),
10411044
);
10421045

1043-
it.effect("a socket preflight rejection persists the connection as switched off", () =>
1046+
it.effect("protocol rejection preserves enablement through recovery and relaunch", () =>
10441047
Effect.gen(function* () {
10451048
const error = new ConnectionBlockedError({
10461049
reason: "unsupported",
10471050
detail: "Use a compatible client.",
10481051
});
1049-
const harness = yield* makeHarness([RELAY_TARGET], [], [], { prepareError: error });
1052+
const rejection = yield* Ref.make<ConnectionBlockedError | undefined>(error);
1053+
const harness = yield* makeHarness([RELAY_TARGET], [], [], {
1054+
beforePrepareRoute: () => Ref.get(rejection),
1055+
});
10501056
yield* Effect.gen(function* () {
10511057
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
10521058
yield* registry.start;
10531059
yield* SubscriptionRef.changes(registry.entries).pipe(
1054-
Stream.filter((entries) => entries.get(RELAY_TARGET.environmentId)?.enabled === false),
1060+
Stream.filter(
1061+
(entries) => entries.get(RELAY_TARGET.environmentId)?.unsupportedReason !== undefined,
1062+
),
10551063
Stream.take(1),
10561064
Stream.runDrain,
10571065
);
1058-
expect((yield* Ref.get(harness.storedDisabled)).has(RELAY_TARGET.environmentId)).toBe(true);
1066+
expect(yield* Ref.get(harness.storedDisabled)).toEqual(new Set());
10591067
expect(
1060-
(yield* SubscriptionRef.get(registry.entries)).get(RELAY_TARGET.environmentId)
1061-
?.unsupportedReason,
1062-
).toBe(error.message);
1068+
(yield* SubscriptionRef.get(registry.entries)).get(RELAY_TARGET.environmentId),
1069+
).toMatchObject({ enabled: true, unsupportedReason: error.message });
1070+
yield* awaitConnectionState(
1071+
registry,
1072+
RELAY_TARGET.environmentId,
1073+
(state) => !state.desired && state.phase === "available",
1074+
);
10631075
expect(yield* Ref.get(harness.sessions)).toHaveLength(0);
1064-
}).pipe(Effect.provide(harness.layer));
1076+
1077+
yield* Ref.set(rejection, undefined);
1078+
yield* registry.setCompatibility(RELAY_TARGET.environmentId, null);
1079+
yield* awaitConnectionState(
1080+
registry,
1081+
RELAY_TARGET.environmentId,
1082+
(state) => state.phase === "connected",
1083+
);
1084+
expect(yield* Ref.get(harness.sessions)).toHaveLength(1);
1085+
yield* registry.setCompatibility(RELAY_TARGET.environmentId, error);
1086+
yield* awaitConnectionState(
1087+
registry,
1088+
RELAY_TARGET.environmentId,
1089+
(state) => !state.desired && state.phase === "available",
1090+
);
1091+
expect(yield* Ref.get(harness.releasedSessions)).toBe(1);
1092+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
1093+
1094+
yield* Effect.gen(function* () {
1095+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
1096+
const entry = (yield* SubscriptionRef.get(registry.entries)).get(
1097+
RELAY_TARGET.environmentId,
1098+
);
1099+
expect(entry?.enabled).toBe(true);
1100+
expect(entry?.unsupportedReason).toBeUndefined();
1101+
yield* registry.start;
1102+
yield* awaitConnectionState(
1103+
registry,
1104+
RELAY_TARGET.environmentId,
1105+
(state) => state.phase === "connected",
1106+
);
1107+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
10651108
}),
10661109
);
10671110

@@ -1100,16 +1143,16 @@ describe("EnvironmentRegistry", () => {
11001143
yield* Effect.gen(function* () {
11011144
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
11021145
yield* registry.start;
1103-
const awaitDisabled = SubscriptionRef.changes(registry.entries).pipe(
1104-
Stream.filter((entries) => entries.get(environmentId)?.enabled === false),
1146+
const awaitUnsupported = SubscriptionRef.changes(registry.entries).pipe(
1147+
Stream.filter((entries) => entries.get(environmentId)?.unsupportedReason !== undefined),
11051148
Stream.take(1),
11061149
Stream.runDrain,
11071150
);
1108-
yield* awaitDisabled;
1109-
expect((yield* Ref.get(harness.storedDisabled)).has(environmentId)).toBe(true);
1151+
yield* awaitUnsupported;
1152+
expect((yield* Ref.get(harness.storedDisabled)).has(environmentId)).toBe(false);
11101153
expect((yield* SubscriptionRef.get(registry.entries)).get(environmentId)).toMatchObject({
11111154
target: relayFirst ? relay : BEARER_TARGET,
1112-
enabled: false,
1155+
enabled: true,
11131156
unsupportedReason: error.message,
11141157
serverUpdateRequired: true,
11151158
});
@@ -1132,13 +1175,13 @@ describe("EnvironmentRegistry", () => {
11321175
);
11331176
expect(yield* Ref.get(harness.sessions)).toHaveLength(1);
11341177
} else {
1135-
yield* awaitDisabled;
1178+
yield* awaitUnsupported;
11361179
expect((yield* SubscriptionRef.get(registry.entries)).get(environmentId)).toMatchObject({
1137-
enabled: false,
1180+
enabled: true,
11381181
unsupportedReason: error.message,
11391182
serverUpdateRequired: true,
11401183
});
1141-
expect((yield* Ref.get(harness.storedDisabled)).has(environmentId)).toBe(true);
1184+
expect((yield* Ref.get(harness.storedDisabled)).has(environmentId)).toBe(false);
11421185
expect(yield* Ref.get(harness.sessions)).toHaveLength(0);
11431186
}
11441187
expect(yield* Ref.get(attempts)).toBe(2);
@@ -1808,7 +1851,16 @@ describe("EnvironmentRegistry", () => {
18081851
yield* registry.reconcilePlatform([registration]);
18091852
expect(
18101853
(yield* SubscriptionRef.get(registry.entries)).get(TARGET.environmentId),
1811-
).toMatchObject({ enabled: false, unsupportedReason: error.message });
1854+
).toMatchObject({ enabled: true, unsupportedReason: error.message });
1855+
yield* registry.registerPlatform(
1856+
new PrimaryConnectionRegistration({
1857+
target: new PrimaryConnectionTarget({ ...TARGET, label: "Renamed" }),
1858+
}),
1859+
);
1860+
expect(yield* registry.state(TARGET.environmentId)).toMatchObject({
1861+
desired: false,
1862+
phase: "available",
1863+
});
18121864
expect(yield* Ref.get(harness.sessions)).toHaveLength(1);
18131865
yield* registry.registerPlatform(
18141866
new PrimaryConnectionRegistration({

‎packages/client-runtime/src/connection/registry.ts‎

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,9 @@ export const make = Effect.gen(function* () {
357357
const environmentId = entry.target.environmentId;
358358
const scope = yield* Scope.fork(registryScope);
359359
const supervisor = yield* EnvironmentSupervisor.make(entry, {
360-
// An enabled environment starts out connecting. Starting it idle and
361-
// then asking it to connect would first publish the state of an
362-
// environment the user switched off.
363-
initiallyDesired: entry.enabled,
360+
// Seed the intent to avoid publishing a false switched-off state
361+
// before an enabled, compatible environment starts connecting.
362+
initiallyDesired: entry.enabled && entry.unsupportedReason === undefined,
364363
learnRoutes: (input) => learnRoutes({ environmentId, ...input }),
365364
}).pipe(
366365
Effect.provideService(Connectivity.Connectivity, connectivity),
@@ -377,14 +376,7 @@ export const make = Effect.gen(function* () {
377376
yield* SubscriptionRef.changes(supervisor.state).pipe(
378377
Stream.runForEach((state) =>
379378
state.phase === "blocked" && state.lastFailure?.reason === "unsupported"
380-
? setCompatibility(environmentId, state.lastFailure).pipe(
381-
Effect.catch((error) =>
382-
Effect.logWarning("Could not disable an unsupported environment.", {
383-
environmentId,
384-
error,
385-
}),
386-
),
387-
)
379+
? setCompatibility(environmentId, state.lastFailure)
388380
: Effect.void,
389381
),
390382
Effect.forkIn(scope),
@@ -613,7 +605,7 @@ export const make = Effect.gen(function* () {
613605
const entry: ConnectionCatalogEntry =
614606
previous?.unsupportedReason !== undefined &&
615607
gitHubRoutingConnectionKey(previous) === gitHubRoutingConnectionKey(registered)
616-
? { ...registered, enabled: false, ...unsupportedState(previous) }
608+
? { ...registered, ...unsupportedState(previous) }
617609
: registered;
618610
const persisted = (yield* Ref.get(persistedEnvironmentIds)).has(target.environmentId);
619611
if (
@@ -1118,28 +1110,22 @@ export const make = Effect.gen(function* () {
11181110
serverUpdateRequired: _previousUpdateRequired,
11191111
...rest
11201112
} = entry;
1113+
// Compatibility pauses connections without changing the user's saved on/off choice.
11211114
const next: ConnectionCatalogEntry =
11221115
error === null
11231116
? rest
11241117
: {
11251118
...rest,
1126-
enabled: false,
11271119
unsupportedReason: error.message,
11281120
...(error.serverUpdateRequired === true ? { serverUpdateRequired: true } : {}),
11291121
};
1130-
if (
1131-
error !== null &&
1132-
entry.enabled &&
1133-
!(yield* Ref.get(platformEnvironmentIds)).has(environmentId)
1134-
) {
1135-
yield* registrations.setEnabled(environmentId, false);
1136-
}
11371122
const lease = (yield* SubscriptionRef.get(serviceScopes)).get(environmentId);
11381123
if (lease !== undefined) {
11391124
yield* SubscriptionRef.update(serviceScopes, (current) =>
11401125
new Map(current).set(environmentId, { ...lease, entry: next }),
11411126
);
11421127
if (error !== null) yield* lease.supervisor.disconnect;
1128+
else if (next.enabled) yield* lease.supervisor.connect;
11431129
}
11441130
yield* SubscriptionRef.update(entries, (current) =>
11451131
new Map(current).set(environmentId, next),

‎packages/client-runtime/src/state/connections.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function* enabledEnvironmentIds(
2929
catalog: EnvironmentCatalogState,
3030
): Generator<EnvironmentIdType> {
3131
for (const [environmentId, entry] of catalog.entries) {
32-
if (entry.enabled) {
32+
if (entry.enabled && entry.unsupportedReason === undefined) {
3333
yield environmentId;
3434
}
3535
}

‎packages/client-runtime/src/state/pullRequestRouting.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,13 @@ const routingAllowed = Effect.fn("PullRequestRouting.allowed")(function* (
161161
const entries = yield* SubscriptionRef.get(registry.entries);
162162
const origin = entries.get(originId);
163163
const destination = entries.get(destinationId);
164-
if (!origin?.enabled || !destination?.enabled) return false;
164+
if (
165+
!origin?.enabled ||
166+
!destination?.enabled ||
167+
origin.unsupportedReason !== undefined ||
168+
destination.unsupportedReason !== undefined
169+
)
170+
return false;
165171
const permissions = yield* GitHubRoutingPermissions;
166172
const source = yield* permissions.get(origin);
167173
const target = yield* permissions.get(destination);

‎packages/client-runtime/src/state/pullRequests.test.ts‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,15 @@ const makeTestRuntime = Effect.fn("makeTestRuntime")(function* (
754754
return { runtime, atoms, registry, environmentRegistry, supervisor, localSupervisor };
755755
});
756756

757-
it.effect.each(["default", "origin-off", "destination-off", "read-only"] as const)(
758-
"does not probe another environment with %s routing permission",
757+
it.effect.each([
758+
"default",
759+
"origin-off",
760+
"destination-off",
761+
"read-only",
762+
"destination-disabled",
763+
"destination-unsupported",
764+
] as const)(
765+
"preserves source requests when routing to another environment is blocked by %s",
759766
(permission) =>
760767
Effect.scoped(
761768
Effect.gen(function* () {
@@ -765,9 +772,10 @@ it.effect.each(["default", "origin-off", "destination-off", "read-only"] as cons
765772
detail: "source failed",
766773
});
767774
const client = {
768-
[WS_METHODS.pullRequestsSummary]: () =>
775+
[WS_METHODS.pullRequestsSummary]: (input: { allowStale?: boolean }) =>
769776
Effect.suspend(() => {
770777
calls.push("source-read");
778+
expect(input.allowStale).toBeUndefined();
771779
return Effect.fail(failure);
772780
}),
773781
[WS_METHODS.pullRequestsRunAction]: () =>
@@ -779,7 +787,31 @@ it.effect.each(["default", "origin-off", "destination-off", "read-only"] as cons
779787
[WS_METHODS.pullRequestsRoutingIdentity]: () =>
780788
Effect.die("untrusted environment was contacted"),
781789
} as unknown as WsRpcProtocolClient;
782-
const { environmentRegistry, supervisor } = yield* makeTestRuntime(client, alternate);
790+
const { environmentRegistry, supervisor, localSupervisor } = yield* makeTestRuntime(
791+
client,
792+
alternate,
793+
);
794+
if (permission === "destination-disabled" || permission === "destination-unsupported") {
795+
yield* SubscriptionRef.update(
796+
environmentRegistry.entries,
797+
(entries) =>
798+
new Map(
799+
[...entries].map(([id, entry]) => [
800+
id,
801+
id === TARGET.environmentId
802+
? entry
803+
: {
804+
...entry,
805+
enabled: permission !== "destination-disabled",
806+
...(permission === "destination-unsupported"
807+
? { unsupportedReason: "Protocol version mismatch" }
808+
: {}),
809+
},
810+
]),
811+
),
812+
);
813+
yield* SubscriptionRef.set(localSupervisor.session, Option.none());
814+
}
783815
const ref = {
784816
projectId: ProjectId.make("project-1"),
785817
repository: "private/repo",
@@ -805,10 +837,13 @@ it.effect.each(["default", "origin-off", "destination-off", "read-only"] as cons
805837
Effect.succeed(
806838
permission === "read-only"
807839
? "read"
808-
: (entry.target.environmentId === TARGET.environmentId) ===
809-
(permission === "origin-off")
810-
? "off"
811-
: "read-write",
840+
: permission === "destination-disabled" ||
841+
permission === "destination-unsupported"
842+
? "read-write"
843+
: (entry.target.environmentId === TARGET.environmentId) ===
844+
(permission === "origin-off")
845+
? "off"
846+
: "read-write",
812847
),
813848
}),
814849
);

0 commit comments

Comments
 (0)