Skip to content

Commit a257077

Browse files
committed
fix(relay): order host labels by observation time
1 parent e442168 commit a257077

4 files changed

Lines changed: 116 additions & 38 deletions

File tree

‎apps/server/src/relay/EnvironmentLabelRelaySync.test.ts‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ it.effect("sends the current host name at startup and only changed names thereaf
2323
const changes = yield* PubSub.unbounded<ServerSettings>();
2424
const pushed = yield* Queue.unbounded<{ readonly url: string; readonly body: unknown }>();
2525
const failedAttempts = yield* Queue.unbounded<void>();
26+
const unlinkedReads = yield* Queue.unbounded<void>();
2627
let remainingFailures = 0;
2728
const fetch: typeof globalThis.fetch = Object.assign(
2829
(input: Parameters<typeof globalThis.fetch>[0], init?: RequestInit) => {
@@ -45,7 +46,14 @@ it.effect("sends the current host name at startup and only changed names thereaf
4546
]);
4647
const layerDependencies = Layer.mergeAll(
4748
Layer.mock(ServerSecretStore.ServerSecretStore)({
48-
get: (name) => Effect.succeed(Option.fromNullishOr(stored.get(name))),
49+
get: (name) =>
50+
Effect.sync(() => {
51+
const value = stored.get(name);
52+
if (name === RELAY_ENVIRONMENT_CREDENTIAL_SECRET && value === undefined) {
53+
Queue.offerUnsafe(unlinkedReads, undefined);
54+
}
55+
return Option.fromNullishOr(value);
56+
}),
4957
}),
5058
Layer.mock(ServerEnvironment.ServerEnvironment)({
5159
getDescriptorForSettings: ({ environmentLabel }) =>
@@ -77,19 +85,38 @@ it.effect("sends the current host name at startup and only changed names thereaf
7785
yield* PubSub.publish(changes, settingsWith("", false));
7886
assert.deepEqual((yield* Queue.take(pushed)).body, { label: "Detected Mac" });
7987

80-
remainingFailures = 4;
81-
yield* PubSub.publish(changes, settingsWith("Retry Mac"));
82-
yield* Queue.take(failedAttempts);
83-
for (const delay of ["2 seconds", "4 seconds", "8 seconds"] as const) {
84-
yield* TestClock.adjust(delay);
88+
const exhaustPush = Effect.fn("RelayLabelTest.exhaustPush")(function* (label: string) {
89+
remainingFailures = 4;
90+
yield* PubSub.publish(changes, settingsWith(label));
8591
yield* Queue.take(failedAttempts);
86-
}
87-
yield* PubSub.publish(changes, settingsWith("", true));
92+
for (const delay of ["2 seconds", "4 seconds", "8 seconds"] as const) {
93+
yield* TestClock.adjust(delay);
94+
yield* Queue.take(failedAttempts);
95+
}
96+
});
97+
yield* exhaustPush("Retry Mac");
8898
yield* PubSub.publish(changes, settingsWith("Retry Mac", false));
8999
yield* PubSub.publish(changes, settingsWith("Retry Mac", true));
90100
yield* PubSub.publish(changes, settingsWith("After Retry"));
91-
assert.deepEqual((yield* Queue.take(pushed)).body, { label: "Detected Mac" });
92101
assert.deepEqual((yield* Queue.take(pushed)).body, { label: "Retry Mac" });
93102
assert.deepEqual((yield* Queue.take(pushed)).body, { label: "After Retry" });
103+
104+
yield* exhaustPush("Unconfirmed name");
105+
yield* PubSub.publish(changes, settingsWith("After Retry"));
106+
yield* PubSub.publish(changes, settingsWith("Recovered"));
107+
assert.deepEqual((yield* Queue.take(pushed)).body, { label: "After Retry" });
108+
assert.deepEqual((yield* Queue.take(pushed)).body, { label: "Recovered" });
109+
110+
stored.delete(RELAY_ENVIRONMENT_CREDENTIAL_SECRET);
111+
yield* PubSub.publish(changes, settingsWith("Unlinked name"));
112+
yield* Queue.take(unlinkedReads);
113+
stored.set(
114+
RELAY_ENVIRONMENT_CREDENTIAL_SECRET,
115+
new TextEncoder().encode("relinked-credential"),
116+
);
117+
yield* PubSub.publish(changes, settingsWith("Recovered"));
118+
yield* PubSub.publish(changes, settingsWith("Relinked"));
119+
assert.deepEqual((yield* Queue.take(pushed)).body, { label: "Recovered" });
120+
assert.deepEqual((yield* Queue.take(pushed)).body, { label: "Relinked" });
94121
}).pipe(Effect.scoped),
95122
);

‎apps/server/src/relay/EnvironmentLabelRelaySync.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const layer = Layer.effectDiscard(
4040
yield* pushEnvironmentLabel(environmentLabel).pipe(
4141
Effect.timeout("10 seconds"),
4242
Effect.retry({ schedule: Schedule.exponential("2 seconds"), times: 3 }),
43-
Effect.tap((sent) => (sent ? Ref.set(lastPushedLabel, environmentLabel) : Effect.void)),
43+
Effect.tap((sent) => Ref.set(lastPushedLabel, sent ? environmentLabel : null)),
4444
Effect.catchCause((cause) =>
4545
Ref.set(lastPushedLabel, null).pipe(
4646
Effect.andThen(

‎infra/relay/src/environments/EnvironmentLinks.test.ts‎

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, expect, it } from "@effect/vitest";
22
import * as Effect from "effect/Effect";
3+
import * as Deferred from "effect/Deferred";
4+
import * as Fiber from "effect/Fiber";
35
import * as FileSystem from "effect/FileSystem";
46
import * as NodeServices from "@effect/platform-node/NodeServices";
57
import { PGlite } from "@electric-sql/pglite";
@@ -56,14 +58,14 @@ describe("EnvironmentLinks", () => {
5658
expect(query.sql).toContain('"relay_environment_links"."environment_id" = $2');
5759
expect(query.sql).toContain('"relay_environment_links"."environment_public_key" = $3');
5860
expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
59-
expect(query.sql).toContain('"relay_environment_links"."updated_at" <= $4');
60-
expect(query.sql).toContain('"relay_environment_links"."label_updated_at" <= $5');
61+
expect(query.sql).toContain(
62+
'coalesce("relay_environment_links"."label_updated_at", "relay_environment_links"."updated_at") <= $4',
63+
);
6164
expect(query.params).toEqual([
6265
"user-1",
6366
"env-1",
6467
"verified-public-key",
6568
"2026-08-20T12:00:00.000Z",
66-
"2026-08-20T12:00:00.000Z",
6769
]);
6870
}).pipe(
6971
Effect.provide(
@@ -97,13 +99,10 @@ describe("EnvironmentLinks", () => {
9799
const query = new PgDialect().sqlToQuery(whereConditions[0] as never);
98100
expect(query.sql).not.toContain("user_id");
99101
expect(query.sql).toContain('"relay_environment_links"."environment_public_key" = $2');
100-
expect(query.sql).toContain('"relay_environment_links"."updated_at" <= $3');
101-
expect(query.params).toEqual([
102-
"env-1",
103-
"verified-public-key",
104-
"1970-01-01T00:00:00.000Z",
105-
"1970-01-01T00:00:00.000Z",
106-
]);
102+
expect(query.sql).toContain(
103+
'coalesce("relay_environment_links"."label_updated_at", "relay_environment_links"."updated_at") <= $3',
104+
);
105+
expect(query.params).toEqual(["env-1", "verified-public-key", "1970-01-01T00:00:00.000Z"]);
107106
}).pipe(
108107
Effect.provide(
109108
EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
@@ -148,6 +147,9 @@ describe("EnvironmentLinks", () => {
148147
);
149148
const database = drizzle({ client: pg });
150149
const insertion = database.insert(relayEnvironmentLinks);
150+
const updating = database.update(relayEnvironmentLinks);
151+
const pushStarted = yield* Deferred.make<void>();
152+
const releasePush = yield* Deferred.make<void>();
151153
const db = {
152154
insert: () => ({
153155
values: (values: Parameters<typeof insertion.values>[0]) => ({
@@ -163,15 +165,17 @@ describe("EnvironmentLinks", () => {
163165
}),
164166
}),
165167
update: () => ({
166-
set: (values: Parameters<ReturnType<typeof database.update>["set"]>[0]) => ({
167-
where: (
168-
condition: Parameters<
169-
ReturnType<ReturnType<typeof database.update>["set"]>["where"]
170-
>[0],
171-
) =>
172-
Effect.promise(() =>
173-
database.update(relayEnvironmentLinks).set(values).where(condition),
174-
),
168+
set: (values: Parameters<typeof updating.set>[0]) => ({
169+
where: (condition: Parameters<ReturnType<typeof updating.set>["where"]>[0]) =>
170+
Effect.gen(function* () {
171+
if (values.environmentLabel === "In-flight name") {
172+
yield* Deferred.succeed(pushStarted, undefined);
173+
yield* Deferred.await(releasePush);
174+
}
175+
return yield* Effect.promise(() =>
176+
database.update(relayEnvironmentLinks).set(values).where(condition),
177+
);
178+
}),
175179
}),
176180
}),
177181
} as unknown as RelayDb.RelayDb["Service"];
@@ -216,8 +220,8 @@ describe("EnvironmentLinks", () => {
216220
yield* TestClock.setTime(Date.parse(at(40)));
217221
for (const [challengeAt, key, proofLabel, expectedLabel, expectedWatermark] of [
218222
[20, "key", "Stale link proof", "Latest name", 30],
219-
[35, "key", "Fresh link proof", "Fresh link proof", 40],
220-
[15, "replacement-key", "Replacement host", "Replacement host", 40],
223+
[35, "key", "Fresh link proof", "Fresh link proof", 35],
224+
[15, "replacement-key", "Replacement host", "Replacement host", 15],
221225
] as const) {
222226
yield* links.upsert({
223227
userId: "user-1",
@@ -249,6 +253,53 @@ describe("EnvironmentLinks", () => {
249253
updatedAt: at(40),
250254
});
251255
}
256+
yield* Effect.promise(() =>
257+
database.update(relayEnvironmentLinks).set({
258+
environmentPublicKey: "key",
259+
environmentLabel: "Before rename",
260+
labelUpdatedAt: at(41),
261+
updatedAt: at(41),
262+
}),
263+
);
264+
yield* TestClock.setTime(Date.parse(at(45)));
265+
const inFlightPush = yield* links
266+
.recordEnvironmentLabel({
267+
environmentId: "env-1",
268+
environmentPublicKey: "key",
269+
label: "In-flight name",
270+
})
271+
.pipe(Effect.forkChild);
272+
yield* Deferred.await(pushStarted);
273+
yield* TestClock.setTime(Date.parse(at(50)));
274+
yield* links.upsert({
275+
userId: "user-1",
276+
challengeIssuedAt: at(42),
277+
request: {
278+
notificationsEnabled: false,
279+
liveActivitiesEnabled: false,
280+
managedTunnelsEnabled: false,
281+
} as never,
282+
proof: {
283+
environmentId: "env-1",
284+
environmentPublicKey: "key",
285+
descriptor: { label: "Old proof" },
286+
} as never,
287+
endpoint: {
288+
httpBaseUrl: "https://e",
289+
wsBaseUrl: "wss://e",
290+
providerKind: "direct",
291+
} as never,
292+
});
293+
yield* Deferred.succeed(releasePush, undefined);
294+
yield* Fiber.join(inFlightPush);
295+
const [afterRace] = yield* Effect.promise(() =>
296+
database.select().from(relayEnvironmentLinks),
297+
);
298+
expect(afterRace).toMatchObject({
299+
environmentLabel: "In-flight name",
300+
labelUpdatedAt: at(45),
301+
updatedAt: at(50),
302+
});
252303
}).pipe(Effect.provide(NodeServices.layer)),
253304
);
254305

‎infra/relay/src/environments/EnvironmentLinks.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,9 @@ const make = Effect.gen(function* () {
223223
eq(relayEnvironmentLinks.environmentId, input.environmentId),
224224
eq(relayEnvironmentLinks.environmentPublicKey, input.environmentPublicKey),
225225
isNull(relayEnvironmentLinks.revokedAt),
226-
lte(relayEnvironmentLinks.updatedAt, input.checkedAt),
227-
or(
228-
isNull(relayEnvironmentLinks.labelUpdatedAt),
229-
lte(relayEnvironmentLinks.labelUpdatedAt, input.checkedAt),
226+
lte(
227+
sql<string>`coalesce(${relayEnvironmentLinks.labelUpdatedAt}, ${relayEnvironmentLinks.updatedAt})`,
228+
input.checkedAt,
230229
),
231230
),
232231
)
@@ -249,7 +248,8 @@ const make = Effect.gen(function* () {
249248
const now = DateTime.formatIso(yield* DateTime.now);
250249
const { request, proof } = input;
251250
const environmentId = proof.environmentId;
252-
// The relay-issued challenge fences stale proofs without depending on the host clock.
251+
// Use the relay challenge as the proof's label watermark: completing a relink must
252+
// not outrank a newer push that is already in flight, or depend on the host clock.
253253
const keepPushedLabel = sql<boolean>`${relayEnvironmentLinks.environmentPublicKey} = ${proof.environmentPublicKey}
254254
AND ${relayEnvironmentLinks.labelUpdatedAt} >= ${input.challengeIssuedAt}`;
255255
// Inherit opt-in only from links proven by the same key; anyone can claim the public environment id.
@@ -268,7 +268,7 @@ const make = Effect.gen(function* () {
268268
environmentId,
269269
holdWebhooksWhileOffline: inheritedHoldWebhooks,
270270
environmentLabel: proof.descriptor.label,
271-
labelUpdatedAt: now,
271+
labelUpdatedAt: input.challengeIssuedAt,
272272
environmentPublicKey: proof.environmentPublicKey,
273273
endpointHttpBaseUrl: endpoint.httpBaseUrl,
274274
endpointWsBaseUrl: endpoint.wsBaseUrl,
@@ -286,7 +286,7 @@ const make = Effect.gen(function* () {
286286
set: {
287287
environmentPublicKey: proof.environmentPublicKey,
288288
environmentLabel: sql`CASE WHEN ${keepPushedLabel} THEN ${relayEnvironmentLinks.environmentLabel} ELSE ${proof.descriptor.label} END`,
289-
labelUpdatedAt: sql`CASE WHEN ${keepPushedLabel} THEN ${relayEnvironmentLinks.labelUpdatedAt} ELSE ${now} END`,
289+
labelUpdatedAt: sql`CASE WHEN ${keepPushedLabel} THEN ${relayEnvironmentLinks.labelUpdatedAt} ELSE ${input.challengeIssuedAt} END`,
290290
endpointHttpBaseUrl: endpoint.httpBaseUrl,
291291
endpointWsBaseUrl: endpoint.wsBaseUrl,
292292
endpointProviderKind: endpoint.providerKind,

0 commit comments

Comments
 (0)