Skip to content
TypeScript

fix(server): linked environments pick up the other machine's new name (#29779)

Alex Southwellddaf9996a180
diff --git a/apps/server/src/peer/PeerLinks.test.ts b/apps/server/src/peer/PeerLinks.test.ts
index a99cb7f9051..36c9b09fb44 100644
--- a/apps/server/src/peer/PeerLinks.test.ts
+++ b/apps/server/src/peer/PeerLinks.test.ts
@@ -453,6 +453,27 @@ it.effect("reads one stored link without asking the peer", () =>
).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
+it.effect("a linked peer that is renamed shows its new name", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const b = yield* serveB(box);
+ const a = yield* makeA(laptop);
+ const pairing = yield* b.auth.issuePairingCredential();
+ yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" });
+
+ yield* Ref.set(b.descriptor, descriptorOf(box.environmentId, "Box Mini"));
+ expect((yield* a.links.resolve(box.environmentId)).link.label).toBe("Box Mini");
+ const stored = yield* a.links.get(box.environmentId);
+ expect(Option.map(stored, (link) => link.label)).toEqual(Option.some("Box Mini"));
+
+ yield* Ref.set(b.descriptor, descriptorOf(box.environmentId, "Box Studio"));
+ expect((yield* a.links.list).map((link) => link.label)).toEqual(["Box Studio"]);
+ const storedAfterList = yield* a.links.get(box.environmentId);
+ expect(Option.map(storedAfterList, (link) => link.label)).toEqual(Option.some("Box Studio"));
+ }),
+ ).pipe(Effect.provide(NodeHttpServer.layerTest)),
+);
+
it.effect("tells the agent to link again once the peer revokes the link or it expires", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/apps/server/src/peer/PeerLinks.ts b/apps/server/src/peer/PeerLinks.ts
index 70aa68d25cc..dfcab9f11f2 100644
--- a/apps/server/src/peer/PeerLinks.ts
+++ b/apps/server/src/peer/PeerLinks.ts
@@ -9,7 +9,6 @@ import {
type PeerLink,
type PeerLinkCreateInput,
PeerLinkError,
- type PeerLinkStatus,
type PeerLinkSummary,
} from "@t3tools/contracts";
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
@@ -496,29 +495,42 @@ const make = Effect.gen(function* () {
Effect.orElseSucceed(() => Option.none()),
);
- const statusOf = (link: PeerLink, now: DateTime.Utc) =>
- DateTime.isLessThanOrEqualTo(link.expiresAt, now)
- ? Effect.succeed<PeerLinkStatus>("expired")
- : Effect.findFirst(link.urls, (url) =>
- describe(url).pipe(
- Effect.map(
- (found) => Option.isSome(found) && found.value.environmentId === link.environmentId,
- ),
- ),
- ).pipe(
- Effect.map((answered): PeerLinkStatus =>
- Option.isSome(answered) ? "reachable" : "unreachable",
- ),
- );
+ /** A peer's label follows its machine name, so the link takes the one its own address answers with. */
+ const withLabel = (link: PeerLink, label: string) =>
+ label === link.label
+ ? Effect.succeed(link)
+ : sql`
+ UPDATE peer_environment_links SET label = ${label}
+ WHERE environment_id = ${link.environmentId}
+ `.pipe(Effect.ignoreCause({ log: true }), Effect.as({ ...link, label }));
+
+ const statusOf = Effect.fn("PeerLinks.statusOf")(function* (
+ link: PeerLink,
+ now: DateTime.Utc,
+ ): Effect.fn.Return<PeerLinkSummary> {
+ if (DateTime.isLessThanOrEqualTo(link.expiresAt, now)) return { ...link, status: "expired" };
+ let label = link.label;
+ const answered = yield* Effect.findFirst(link.urls, (url) =>
+ describe(url).pipe(
+ Effect.map((found) => {
+ if (Option.isNone(found) || found.value.environmentId !== link.environmentId) {
+ return false;
+ }
+ label = found.value.label;
+ return true;
+ }),
+ ),
+ );
+ if (Option.isNone(answered)) return { ...link, status: "unreachable" };
+ return { ...(yield* withLabel(link, label)), status: "reachable" };
+ });
const list: PeerLinks["Service"]["list"] = Effect.gen(function* () {
const links = yield* Effect.forEach(yield* readRows, toLink);
const now = yield* DateTime.now;
- return yield* Effect.forEach(
- links,
- (link) => statusOf(link, now).pipe(Effect.map((status) => ({ ...link, status }))),
- { concurrency: "unbounded" },
- );
+ return yield* Effect.forEach(links, (link) => statusOf(link, now), {
+ concurrency: "unbounded",
+ });
});
const get: PeerLinks["Service"]["get"] = (environmentId) =>
@@ -570,11 +582,13 @@ const make = Effect.gen(function* () {
// The token goes only to an address that proves it is this peer, so a
// reused LAN address or a moved hostname never receives it.
let fenced = true;
+ let label = link.label;
const url = yield* Effect.findFirst(link.urls, (candidate) =>
describe(candidate).pipe(
Effect.map((found) => {
if (Option.isNone(found) || found.value.environmentId !== environmentId) return false;
fenced = linkLimitsHold(found.value);
+ label = found.value.label;
return true;
}),
),
@@ -593,7 +607,11 @@ const make = Effect.gen(function* () {
message: `${link.label} runs a T3 Code version that cannot keep a linked agent's limits. Update it, then link again.`,
});
}
- return { link, url: url.value, token: new TextDecoder().decode(stored.value) };
+ return {
+ link: yield* withLabel(link, label),
+ url: url.value,
+ token: new TextDecoder().decode(stored.value),
+ };
},
);