Skip to content

Commit 8781cc8

Browse files
fix(clients): save the profile of a learned route
A learned route was persisted as a target without its profile, so after a reload it had no address. The registry loaded it as a broken route, and the next learn added it again, duplicating the target. Learned routes now save their profile with the route list, and on load a learned route without one, or a duplicate, is dropped to be learned again on the next connection. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent e3a4e88 commit 8781cc8

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,4 +1818,23 @@ describe("EnvironmentRegistry routes", () => {
18181818
}).pipe(Effect.provide(harness.layer), Effect.scoped);
18191819
}),
18201820
);
1821+
1822+
it.effect("drops a learned route whose profile was not saved instead of duplicating it", () =>
1823+
Effect.gen(function* () {
1824+
const learnedId = `learned:${LAN_TARGET.environmentId}:100.64.0.9:3773@${LAN_TARGET.connectionId}`;
1825+
const learned = new BearerConnectionTarget({ ...LAN_TARGET, connectionId: learnedId });
1826+
// An earlier build saved the learned target twice and never its profile.
1827+
const harness = yield* makeHarness(
1828+
[LAN_TARGET, learned, learned],
1829+
[LAN_PROFILE],
1830+
[[LAN_TARGET.connectionId, BEARER_CREDENTIAL]],
1831+
);
1832+
yield* Effect.gen(function* () {
1833+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
1834+
const entry = (yield* SubscriptionRef.get(registry.entries)).get(LAN_TARGET.environmentId);
1835+
expect(entry?.target).toEqual(LAN_TARGET);
1836+
expect(entry?.alternateRoutes ?? []).toEqual([]);
1837+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
1838+
}),
1839+
);
18211840
});

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
connectionRoutes,
4949
entryWithRoutes,
5050
findBearerRouteByUrl,
51+
isLearned,
5152
mergeLearnedRoutes,
5253
routesAfterRemoving,
5354
upsertRoute,
@@ -236,7 +237,18 @@ export const make = Effect.gen(function* () {
236237
yield* Effect.forEach(
237238
persistedRoutesByEnvironment,
238239
Effect.fn("EnvironmentRegistry.loadCatalogEntry")(function* ([environmentId, targets]) {
239-
const routes = yield* Effect.forEach(targets, loadRoute, { concurrency: "unbounded" });
240+
const loaded = yield* Effect.forEach(targets, loadRoute, { concurrency: "unbounded" });
241+
// A learned route without its profile has no address to reach; it is
242+
// learned again on the next connection. A paired route keeps its slot
243+
// so its missing profile still surfaces as a connection error.
244+
const seen = new Set<string>();
245+
const usable = loaded.filter((route) => {
246+
const id = connectionRouteId(route.target);
247+
if (seen.has(id)) return false;
248+
seen.add(id);
249+
return !(id.startsWith("learned:") && Option.isNone(route.profile));
250+
});
251+
const routes = usable.length > 0 ? usable : loaded.slice(0, 1);
240252
const first = routes[0]!;
241253
return [
242254
environmentId,
@@ -862,6 +874,16 @@ export const make = Effect.gen(function* () {
862874
});
863875
if (routes === null) return Option.none<ConnectionCatalogEntry>();
864876
const next = entryWithRoutes(entry, routes);
877+
// A learned route owns its profile (address and authorization); the
878+
// credential stays with the route it borrows from.
879+
const previousIds = new Set(
880+
connectionRoutes(entry).map((route) => connectionRouteId(route.target)),
881+
);
882+
for (const route of routes) {
883+
if (!isLearned(route) || previousIds.has(connectionRouteId(route.target))) continue;
884+
const profile = Option.getOrNull(route.profile);
885+
if (profile !== null) yield* profiles.put(profile);
886+
}
865887
yield* registrations.setRoutes(input.environmentId, persistedRoutes(next));
866888
// Update the lease in place: the live session already works, and
867889
// `installEntryLocked` would replace it for a route list change.

0 commit comments

Comments
 (0)