Skip to content

Commit 31f4534

Browse files
fix(client-runtime): preserve enablement across compatibility blocks
1 parent 73e5f48 commit 31f4534

3 files changed

Lines changed: 45 additions & 74 deletions

File tree

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

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,11 @@ describe("EnvironmentRegistry", () => {
10201020
expect(
10211021
(yield* SubscriptionRef.get(registry.entries)).get(RELAY_TARGET.environmentId)?.enabled,
10221022
).toBe(false);
1023+
expect(yield* registry.state(RELAY_TARGET.environmentId)).toMatchObject({ desired: false });
1024+
expect(yield* Ref.get(harness.storedDisabled)).toEqual(
1025+
new Set([RELAY_TARGET.environmentId]),
1026+
);
1027+
expect(yield* Ref.get(harness.sessions)).toHaveLength(0);
10231028
yield* registry.setEnabled(RELAY_TARGET.environmentId, true);
10241029
yield* awaitConnectionState(
10251030
registry,
@@ -1030,13 +1035,16 @@ describe("EnvironmentRegistry", () => {
10301035
}),
10311036
);
10321037

1033-
it.effect("a socket preflight rejection pauses the connection without switching it off", () =>
1038+
it.effect("protocol rejection preserves enablement through recovery and relaunch", () =>
10341039
Effect.gen(function* () {
10351040
const error = new ConnectionBlockedError({
10361041
reason: "unsupported",
10371042
detail: "Use a compatible client.",
10381043
});
1039-
const harness = yield* makeHarness([RELAY_TARGET], [], [], { prepareError: error });
1044+
const rejection = yield* Ref.make<ConnectionBlockedError | undefined>(error);
1045+
const harness = yield* makeHarness([RELAY_TARGET], [], [], {
1046+
beforePrepareRoute: () => Ref.get(rejection),
1047+
});
10401048
yield* Effect.gen(function* () {
10411049
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
10421050
yield* registry.start;
@@ -1047,9 +1055,7 @@ describe("EnvironmentRegistry", () => {
10471055
Stream.take(1),
10481056
Stream.runDrain,
10491057
);
1050-
expect((yield* Ref.get(harness.storedDisabled)).has(RELAY_TARGET.environmentId)).toBe(
1051-
false,
1052-
);
1058+
expect(yield* Ref.get(harness.storedDisabled)).toEqual(new Set());
10531059
expect(
10541060
(yield* SubscriptionRef.get(registry.entries)).get(RELAY_TARGET.environmentId),
10551061
).toMatchObject({ enabled: true, unsupportedReason: error.message });
@@ -1059,60 +1065,22 @@ describe("EnvironmentRegistry", () => {
10591065
(state) => !state.desired && state.phase === "available",
10601066
);
10611067
expect(yield* Ref.get(harness.sessions)).toHaveLength(0);
1062-
}).pipe(Effect.provide(harness.layer));
1063-
}),
1064-
);
10651068

1066-
it.effect("an enabled environment reconnects once its compatibility block clears", () =>
1067-
Effect.gen(function* () {
1068-
const harness = yield* makeHarness([RELAY_TARGET]);
1069-
const error = new ConnectionBlockedError({
1070-
reason: "unsupported",
1071-
detail: "Use a compatible client.",
1072-
});
1073-
yield* Effect.gen(function* () {
1074-
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
1075-
yield* registry.start;
1069+
yield* Ref.set(rejection, undefined);
1070+
yield* registry.setCompatibility(RELAY_TARGET.environmentId, null);
10761071
yield* awaitConnectionState(
10771072
registry,
10781073
RELAY_TARGET.environmentId,
10791074
(state) => state.phase === "connected",
10801075
);
1081-
1076+
expect(yield* Ref.get(harness.sessions)).toHaveLength(1);
10821077
yield* registry.setCompatibility(RELAY_TARGET.environmentId, error);
10831078
yield* awaitConnectionState(
10841079
registry,
10851080
RELAY_TARGET.environmentId,
1086-
(state) => state.phase === "available",
1087-
);
1088-
expect(
1089-
(yield* SubscriptionRef.get(registry.entries)).get(RELAY_TARGET.environmentId),
1090-
).toMatchObject({ enabled: true, unsupportedReason: error.message });
1091-
expect((yield* Ref.get(harness.storedDisabled)).has(RELAY_TARGET.environmentId)).toBe(
1092-
false,
1093-
);
1094-
1095-
yield* registry.setCompatibility(RELAY_TARGET.environmentId, null);
1096-
yield* awaitConnectionState(
1097-
registry,
1098-
RELAY_TARGET.environmentId,
1099-
(state) => state.phase === "connected",
1100-
);
1101-
expect(yield* Ref.get(harness.sessions)).toHaveLength(2);
1102-
}).pipe(Effect.provide(harness.layer));
1103-
}),
1104-
);
1105-
1106-
it.effect("a blocked environment comes back enabled after a relaunch", () =>
1107-
Effect.gen(function* () {
1108-
const harness = yield* makeHarness([RELAY_TARGET]);
1109-
yield* Effect.gen(function* () {
1110-
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
1111-
yield* registry.start;
1112-
yield* registry.setCompatibility(
1113-
RELAY_TARGET.environmentId,
1114-
new ConnectionBlockedError({ reason: "unsupported", detail: "Use a compatible client." }),
1081+
(state) => !state.desired && state.phase === "available",
11151082
);
1083+
expect(yield* Ref.get(harness.releasedSessions)).toBe(1);
11161084
}).pipe(Effect.provide(harness.layer), Effect.scoped);
11171085

11181086
yield* Effect.gen(function* () {
@@ -1795,6 +1763,15 @@ describe("EnvironmentRegistry", () => {
17951763
expect(
17961764
(yield* SubscriptionRef.get(registry.entries)).get(TARGET.environmentId),
17971765
).toMatchObject({ enabled: true, unsupportedReason: error.message });
1766+
yield* registry.registerPlatform(
1767+
new PrimaryConnectionRegistration({
1768+
target: new PrimaryConnectionTarget({ ...TARGET, label: "Renamed" }),
1769+
}),
1770+
);
1771+
expect(yield* registry.state(TARGET.environmentId)).toMatchObject({
1772+
desired: false,
1773+
phase: "available",
1774+
});
17981775
expect(yield* Ref.get(harness.sessions)).toHaveLength(1);
17991776
yield* registry.registerPlatform(
18001777
new PrimaryConnectionRegistration({

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ function unsupportedState(
6969
};
7070
}
7171

72-
/** The user wants it on and its host is not known to be incompatible. */
73-
function shouldConnect(entry: ConnectionCatalogEntry): boolean {
74-
return entry.enabled && entry.unsupportedReason === undefined;
75-
}
76-
7772
export class EnvironmentNotRegisteredError extends Schema.TaggedError<EnvironmentNotRegisteredError>()(
7873
"EnvironmentNotRegisteredError",
7974
{
@@ -361,10 +356,9 @@ export const make = Effect.gen(function* () {
361356
const environmentId = entry.target.environmentId;
362357
const scope = yield* Scope.fork(registryScope);
363358
const supervisor = yield* EnvironmentSupervisor.make(entry, {
364-
// An enabled environment starts out connecting. Starting it idle and
365-
// then asking it to connect would first publish the state of an
366-
// environment the user switched off.
367-
initiallyDesired: shouldConnect(entry),
359+
// Seed the intent to avoid publishing a false switched-off state
360+
// before an enabled, compatible environment starts connecting.
361+
initiallyDesired: entry.enabled && entry.unsupportedReason === undefined,
368362
learnRoutes: (input) => learnRoutes({ environmentId, ...input }),
369363
}).pipe(
370364
Effect.provideService(Connectivity.Connectivity, connectivity),
@@ -381,14 +375,7 @@ export const make = Effect.gen(function* () {
381375
yield* SubscriptionRef.changes(supervisor.state).pipe(
382376
Stream.runForEach((state) =>
383377
state.phase === "blocked" && state.lastFailure?.reason === "unsupported"
384-
? setCompatibility(environmentId, state.lastFailure).pipe(
385-
Effect.catch((error) =>
386-
Effect.logWarning("Could not mark an environment unsupported.", {
387-
environmentId,
388-
error,
389-
}),
390-
),
391-
)
378+
? setCompatibility(environmentId, state.lastFailure)
392379
: Effect.void,
393380
),
394381
Effect.forkIn(scope),
@@ -1119,9 +1106,7 @@ export const make = Effect.gen(function* () {
11191106
serverUpdateRequired: _previousUpdateRequired,
11201107
...rest
11211108
} = entry;
1122-
// Compatibility is runtime state: it pauses the connection without
1123-
// touching the user's persisted on/off choice, so an environment the
1124-
// user left on reconnects once its host speaks our protocol again.
1109+
// Compatibility pauses connections without changing the user's saved on/off choice.
11251110
const next: ConnectionCatalogEntry =
11261111
error === null
11271112
? rest
@@ -1136,7 +1121,7 @@ export const make = Effect.gen(function* () {
11361121
new Map(current).set(environmentId, { ...lease, entry: next }),
11371122
);
11381123
if (error !== null) yield* lease.supervisor.disconnect;
1139-
else if (shouldConnect(next)) yield* lease.supervisor.connect;
1124+
else if (next.enabled) yield* lease.supervisor.connect;
11401125
}
11411126
yield* SubscriptionRef.update(entries, (current) =>
11421127
new Map(current).set(environmentId, next),

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,13 @@ describe("v2 thread shell lists", () => {
215215
registry.dispose();
216216
});
217217

218-
it("leaves switched-off environments out of navigation", () => {
218+
it.each([
219+
{ reason: "switched-off", state: { enabled: false } },
220+
{
221+
reason: "unsupported",
222+
state: { enabled: true, unsupportedReason: "Use a compatible client." },
223+
},
224+
])("leaves $reason environments out of navigation until restored", ({ state }) => {
219225
const { registry, threads, catalogValueAtom } = makeHarness([
220226
environmentId,
221227
remoteEnvironmentId,
@@ -225,15 +231,18 @@ describe("v2 thread shell lists", () => {
225231
new Set(registry.get(threads.navigationThreadShellsAtom).map((t) => t.environmentId));
226232
expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
227233

228-
const setRemoteEnabled = (enabled: boolean) => {
234+
const setRemotePaused = (paused: boolean) => {
229235
const catalog = registry.get(catalogValueAtom);
230236
const entries = new Map(catalog.entries);
231-
entries.set(remoteEnvironmentId, { ...entries.get(remoteEnvironmentId)!, enabled });
237+
entries.set(remoteEnvironmentId, {
238+
...entries.get(remoteEnvironmentId)!,
239+
...(paused ? state : { enabled: true, unsupportedReason: undefined }),
240+
});
232241
registry.set(catalogValueAtom, { ...catalog, entries });
233242
};
234-
setRemoteEnabled(false);
243+
setRemotePaused(true);
235244
expect(environmentsOf()).toEqual(new Set([environmentId]));
236-
setRemoteEnabled(true);
245+
setRemotePaused(false);
237246
expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
238247
dispose();
239248
registry.dispose();

0 commit comments

Comments
 (0)