Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
068251d
fix(relay): keep a tunnel whose host registered recovery after the cu…
only21mil Oct 22, 2026
17b659b
chore(relay): format the recovery allocation listing
only21mil Oct 22, 2026
a192d2b
docs(relay): say when a host registers recovery relative to cloudflared
only21mil Oct 22, 2026
e05942b
test(relay): give the API test allocations a recovery timestamp
only21mil Oct 22, 2026
9daded6
Merge main (51273020db) into r13/a199-keep-a-managed-tunnel-whose-all…
only21mil Oct 23, 2026
7b0de64
fix(relay): skip the recovery timestamp guard when recovery is unusable
only21mil Oct 24, 2026
783ea31
fix(relay): keep the recovery guard for a registration after the listing
only21mil Oct 24, 2026
3768a6a
fix(relay): release only the allocation generation the reaper listed
only21mil Oct 24, 2026
862be63
docs(relay): name only allocation writes in the release generation pin
only21mil Oct 24, 2026
fd1fe90
fix(relay): a relink that restores the recovery key moves the allocat…
only21mil Oct 24, 2026
794d5b8
fix(relay): write the link before the allocation bump
only21mil Oct 24, 2026
cfc68d7
chore(relay): merge main into reconnect cleanup fix
maria-rcks Oct 26, 2026
9918373
refactor(relay): shorten reconnect cleanup rationale
maria-rcks Oct 26, 2026
a03f622
fix(relay): the sweep skips a reconnecting host's tunnel before count…
only21mil Nov 18, 2026
5b55597
fix(relay): an unchanged relink no longer cancels a tunnel release
only21mil Nov 18, 2026
f8d0c85
test(relay): the release cutoff cases pass the reaper's listed recove…
only21mil Nov 18, 2026
bb008bb
fix(relay): a first link writes its row once
only21mil Nov 18, 2026
8013679
Merge branch 'main' into r13/a199-keep-a-managed-tunnel-whose-allocat…
only21mil Nov 18, 2026
2e44c17
Merge branch 'main' into r13/a199-keep-a-managed-tunnel-whose-allocat…
only21mil Nov 19, 2026
3b39010
Merge branch 'main' into r13/a199-keep-a-managed-tunnel-whose-allocat…
only21mil Nov 20, 2026
5509c27
test(relay): add recoveryEnabledAt to the failed-unlink linker fixture
only21mil Nov 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions infra/relay/src/environments/EnvironmentConnector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function makeAllocations(
updatedAt: "2026-05-25T00:00:00.000Z",
generation: 1,
tunnelReleasedAt: null,
recoveryEnabledAt: null,
},
): ManagedEndpointAllocations.ManagedEndpointAllocations["Service"] {
return {
Expand Down Expand Up @@ -428,6 +429,7 @@ describe("EnvironmentConnector", () => {
updatedAt: "2026-05-25T00:00:00.000Z",
generation: 1,
tunnelReleasedAt: null,
recoveryEnabledAt: null,
}),
}),
),
Expand Down Expand Up @@ -487,6 +489,7 @@ describe("EnvironmentConnector", () => {
updatedAt: "2026-05-25T00:00:00.000Z",
generation: 2,
tunnelReleasedAt: "2026-05-26T00:00:00.000Z",
recoveryEnabledAt: null,
});

return Effect.gen(function* () {
Expand Down
1 change: 1 addition & 0 deletions infra/relay/src/environments/EnvironmentLinker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ describe("EnvironmentLinker", () => {
updatedAt: "2026-01-01T00:00:00.000Z",
generation: 1,
tunnelReleasedAt: null,
recoveryEnabledAt: null,
}),
deprovision: () => Effect.sync(() => touched.push("deprovision") > 0 && tornDown),
upsert: () => Effect.sync(() => void touched.push("upsert")),
Expand Down
1 change: 1 addition & 0 deletions infra/relay/src/environments/EnvironmentLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ const make = Effect.gen(function* () {
? new EnvironmentLinks.EnvironmentLinkUpsertPersistenceError({
userId: input.userId,
environmentId: verified.environmentId,
stage: "transaction",
cause,
})
: cause,
Expand Down
173 changes: 168 additions & 5 deletions infra/relay/src/environments/EnvironmentLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import type { SQL } from "drizzle-orm";
import { PgDialect } from "drizzle-orm/pg-core";

import * as RelayDb from "../db.ts";
import { relayEnvironmentLinks } from "../persistence/schema.ts";
import { relayEnvironmentLinks, relayManagedEndpointAllocations } from "../persistence/schema.ts";
import * as EnvironmentLinks from "./EnvironmentLinks.ts";

describe("EnvironmentLinks", () => {
it.effect("carries the webhook-hold opt-in over only from links with the same key", () => {
const inserted: Array<Record<string, unknown>> = [];
const revoked: Array<{ values: Record<string, unknown>; condition: unknown }> = [];
const fakeDb = {
$client: {
withTransaction: (effect: Effect.Effect<unknown>) => effect,
},
$client: { withTransaction: <A, E, R>(effect: Effect.Effect<A, E, R>) => effect },
select: () => ({ from: () => ({ where: () => ({ for: () => Effect.succeed([]) }) }) }),
execute: () => Effect.void,
update: () => ({
set: (values: Record<string, unknown>) => ({
Expand All @@ -33,7 +32,10 @@ describe("EnvironmentLinks", () => {
insert: () => ({
values: (values: Record<string, unknown>) => {
inserted.push(values);
return { onConflictDoUpdate: () => Effect.void };
return {
onConflictDoNothing: () => ({ returning: () => Effect.succeed([{}]) }),
onConflictDoUpdate: () => Effect.void,
};
},
}),
} as unknown as RelayDb.RelayDb["Service"];
Expand Down Expand Up @@ -70,6 +72,167 @@ describe("EnvironmentLinks", () => {
);
});

interface LinkRow {
readonly environmentPublicKey: string;
readonly endpointProviderKind: string;
readonly revokedAt: string | null;
}
const tunnelLink: LinkRow = {
environmentPublicKey: "public-key-1",
endpointProviderKind: "cloudflare_tunnel",
revokedAt: null,
};
it.effect.each<{
readonly when: string;
readonly previous?: LinkRow;
// The link a concurrent first link inserted after this lock read found none.
readonly raced?: LinkRow;
readonly providerKind?: string;
readonly moved: boolean;
}>([
{ when: "a first link", moved: true },
{
when: "a concurrent first link",
raced: { ...tunnelLink, environmentPublicKey: "public-key-0" },
moved: true,
},
{
when: "a replaced key",
previous: { ...tunnelLink, environmentPublicKey: "public-key-0" },
moved: true,
},
{
when: "a revoked link",
previous: { ...tunnelLink, revokedAt: "2026-06-01T00:00:00.000Z" },
moved: true,
},
{
when: "a publish-only link",
previous: { ...tunnelLink, endpointProviderKind: "manual" },
moved: true,
},
{ when: "an unchanged tunnel link", previous: tunnelLink, moved: false },
{
when: "a relink to publish-only",
previous: { ...tunnelLink, environmentPublicKey: "public-key-0" },
providerKind: "manual",
moved: false,
},
])(
"moves the allocation generation only when a link makes recovery usable again ($when)",
({ previous, raced, providerKind = "cloudflare_tunnel", moved }) => {
const events: Array<string> = [];
const locks: Array<unknown> = [];
const allocationUpdates: Array<{
readonly set: Record<string, unknown>;
readonly where: unknown;
}> = [];
const fakeDb = {
$client: {
withTransaction: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
Effect.sync(() => events.push("begin")).pipe(
Effect.andThen(effect),
Effect.tap(() => Effect.sync(() => events.push("commit"))),
),
},
select: () => ({
from: (table: unknown) => {
expect(table).toBe(relayEnvironmentLinks);
return {
where: (where: unknown) => ({
for: (strength: string) =>
Effect.sync(() => {
expect(strength).toBe("update");
events.push("lock");
locks.push(where);
const row = locks.length === 1 ? previous : raced;
return row === undefined ? [] : [row];
}),
}),
};
},
}),
execute: () => Effect.sync(() => events.push("advisory-lock")),
update: (table: unknown) => {
if (table === relayEnvironmentLinks) {
return { set: () => ({ where: () => Effect.sync(() => events.push("revoke")) }) };
}
expect(table).toBe(relayManagedEndpointAllocations);
return {
set: (set: Record<string, unknown>) => ({
where: (where: unknown) =>
Effect.sync(() => {
events.push("allocation");
allocationUpdates.push({ set, where });
}),
}),
};
},
insert: (table: unknown) => {
expect(table).toBe(relayEnvironmentLinks);
return {
values: () => ({
onConflictDoNothing: () => ({
returning: () =>
Effect.sync(() => {
events.push("insert");
return raced === undefined ? [{ environmentId: "env-1" }] : [];
}),
}),
onConflictDoUpdate: () => Effect.sync(() => events.push("link")),
}),
};
},
} as unknown as RelayDb.RelayDb["Service"];

return Effect.gen(function* () {
const links = yield* EnvironmentLinks.EnvironmentLinks;
yield* links.upsert({
userId: "user-1",
request: {
notificationsEnabled: false,
liveActivitiesEnabled: false,
managedTunnelsEnabled: providerKind === "cloudflare_tunnel",
} as never,
proof: {
environmentId: "env-1",
environmentPublicKey: "public-key-1",
descriptor: { label: "Laptop" },
} as never,
endpoint: {
httpBaseUrl: "https://a.example",
wsBaseUrl: "wss://a.example",
providerKind,
} as never,
});

expect(events).toEqual([
"begin",
"advisory-lock",
"lock",
...(previous === undefined ? ["insert"] : []),
...(raced === undefined ? [] : ["lock"]),
...(previous === undefined && raced === undefined ? [] : ["link"]),
"revoke",
...(moved ? ["allocation"] : []),
"commit",
]);
const dialect = new PgDialect();
expect(dialect.sqlToQuery(locks[0] as never).params).toEqual(["user-1", "env-1"]);
for (const { set, where } of allocationUpdates) {
const query = dialect.sqlToQuery(where as never);
expect(query.sql).toContain('"recovery_environment_public_key" = $3');
expect(query.params).toEqual(["user-1", "env-1", "public-key-1"]);
expect(dialect.sqlToQuery(set.generation as never).sql).toContain('"generation" + 1');
}
}).pipe(
Effect.provide(
EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
),
);
},
);

it.effect("updates Live Activities only on the matching active link", () =>
Effect.gen(function* () {
const pg = yield* Effect.acquireRelease(
Expand Down
Loading
Loading