Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
Fix relay credential lookup for unlinked environments (#4692)
Julius Marmingee00781a66191
diff --git a/infra/relay/src/db.ts b/infra/relay/src/db.tsindex 194ffb6ea..efb0df567 100644--- a/infra/relay/src/db.ts+++ b/infra/relay/src/db.ts@@ -7,6 +7,7 @@ import * as RemovalPolicy from "alchemy/RemovalPolicy";import type { EffectPgDatabase } from "drizzle-orm/effect-postgres";import * as Context from "effect/Context";import * as Effect from "effect/Effect";+import * as Layer from "effect/Layer";import { relayDatabaseMode } from "./dbConfig.ts";@@ -17,6 +18,23 @@ export class RelayDb extends Context.Service<}>()("t3code-relay/db/RelayDb") {}+export class RelayTransactions extends Context.Service<+ RelayTransactions,+ {+ readonly withTransaction: RelayDb["Service"]["$client"]["withTransaction"];+ }+>()("t3code-relay/db/RelayTransactions") {+ static readonly layer = Layer.effect(+ RelayTransactions,+ Effect.gen(function* () {+ const db = yield* RelayDb;+ return RelayTransactions.of({+ withTransaction: db.$client.withTransaction,+ });+ }),+ );+}+export const PlanetscaleDatabase = Effect.gen(function* () {const { stage } = yield* Alchemy.Stack;const schema = yield* Drizzle.Schema("RelaySchema", {diff --git a/infra/relay/src/environments/EnvironmentConnector.test.ts b/infra/relay/src/environments/EnvironmentConnector.test.tsindex 6e3388e39..7f536bafb 100644--- a/infra/relay/src/environments/EnvironmentConnector.test.ts+++ b/infra/relay/src/environments/EnvironmentConnector.test.ts@@ -198,7 +198,9 @@ function makeAllocations(recordDns: () => Effect.die("unused"),markReady: () => Effect.die("unused"),claimRelease: () => Effect.die("unused"),+ claimDeprovision: () => Effect.die("unused"),remove: () => Effect.die("unused"),+ removeClaimed: () => Effect.die("unused"),};}diff --git a/infra/relay/src/environments/EnvironmentCredentials.test.ts b/infra/relay/src/environments/EnvironmentCredentials.test.tsindex 4e12dabe8..4a2e07c6b 100644--- a/infra/relay/src/environments/EnvironmentCredentials.test.ts+++ b/infra/relay/src/environments/EnvironmentCredentials.test.ts@@ -1,6 +1,6 @@import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto";import { describe, expect, it } from "@effect/vitest";-import { PgDialect, QueryBuilder } from "drizzle-orm/pg-core";+import { PgDialect } from "drizzle-orm/pg-core";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";@@ -58,13 +58,17 @@ describe("EnvironmentCredentials", () => {it.effect("does not retain credential tokens when lookup persistence fails", () => {const cause = new Error("database unavailable");const token = "t3env_sensitive-credential-token";+ const whereConditions: Array<unknown> = [];const fakeDb = {select: () => ({from: (table: unknown) => {expect(table).toBe(relayEnvironmentCredentials);return {- where: () => ({- limit: () => Effect.fail(cause),+ where: (condition: unknown) => ({+ limit: () => {+ whereConditions.push(condition);+ return Effect.fail(cause);+ },}),};},@@ -81,6 +85,13 @@ describe("EnvironmentCredentials", () => {});expect(error.cause).toBe(cause);expect(error).not.toHaveProperty("token");+ expect(whereConditions).toHaveLength(1);++ const query = new PgDialect().sqlToQuery(whereConditions[0] as never);+ expect(query.sql).toContain("exists");+ expect(query.sql).toContain('"relay_environment_links"."environment_id"');+ expect(query.sql).toContain('"relay_environment_links"."environment_public_key"');+ expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');}).pipe(Effect.provide(EnvironmentCredentials.layer.pipe(@@ -180,7 +191,6 @@ describe("EnvironmentCredentials", () => {const updateValues: Array<Record<string, unknown>> = [];const whereConditions: Array<unknown> = [];const fakeDb = {- select: (fields: Parameters<QueryBuilder["select"]>[0]) => new QueryBuilder().select(fields),update: (table: unknown) => {expect(table).toBe(relayEnvironmentCredentials);return {diff --git a/infra/relay/src/environments/EnvironmentCredentials.ts b/infra/relay/src/environments/EnvironmentCredentials.tsindex 39f40d941..373f894ee 100644--- a/infra/relay/src/environments/EnvironmentCredentials.ts+++ b/infra/relay/src/environments/EnvironmentCredentials.ts@@ -6,7 +6,8 @@ import * as Encoding from "effect/Encoding";import * as Layer from "effect/Layer";import * as Option from "effect/Option";import * as Schema from "effect/Schema";-import { and, eq, isNull, ne, notExists } from "drizzle-orm";+import { and, eq, exists, isNull, ne, notExists } from "drizzle-orm";+import { QueryBuilder } from "drizzle-orm/pg-core";import * as RelayDb from "../db.ts";import { relayEnvironmentCredentials, relayEnvironmentLinks } from "../persistence/schema.ts";@@ -196,6 +197,24 @@ const make = Effect.gen(function* () {and(eq(relayEnvironmentCredentials.credentialHash, credentialHash),isNull(relayEnvironmentCredentials.revokedAt),+ exists(+ new QueryBuilder()+ .select({ userId: relayEnvironmentLinks.userId })+ .from(relayEnvironmentLinks)+ .where(+ and(+ eq(+ relayEnvironmentLinks.environmentId,+ relayEnvironmentCredentials.environmentId,+ ),+ eq(+ relayEnvironmentLinks.environmentPublicKey,+ relayEnvironmentCredentials.environmentPublicKey,+ ),+ isNull(relayEnvironmentLinks.revokedAt),+ ),+ ),+ ),),).limit(1)@@ -238,7 +257,7 @@ const make = Effect.gen(function* () {eq(relayEnvironmentCredentials.environmentPublicKey, input.environmentPublicKey),isNull(relayEnvironmentCredentials.revokedAt),notExists(- db+ new QueryBuilder().select({ userId: relayEnvironmentLinks.userId }).from(relayEnvironmentLinks).where(diff --git a/infra/relay/src/environments/EnvironmentLinker.test.ts b/infra/relay/src/environments/EnvironmentLinker.test.tsindex 7488939f0..c0811e82d 100644--- a/infra/relay/src/environments/EnvironmentLinker.test.ts+++ b/infra/relay/src/environments/EnvironmentLinker.test.ts@@ -136,6 +136,7 @@ function testLayer(input?: {revokeForEnvironmentPublicKey: () => Effect.succeed(false),}),Layer.succeed(ManagedEndpointProvider.ManagedEndpointProvider, {+ prepareDeprovision: () => Effect.succeed(null),deprovision: input?.deprovision ?? (() => Effect.void),release: () => Effect.succeed(true),provision: () =>diff --git a/infra/relay/src/environments/ManagedEndpointAllocations.test.ts b/infra/relay/src/environments/ManagedEndpointAllocations.test.tsindex 1a3c01d1e..ebf51de10 100644--- a/infra/relay/src/environments/ManagedEndpointAllocations.test.ts+++ b/infra/relay/src/environments/ManagedEndpointAllocations.test.ts@@ -10,6 +10,61 @@ const layerWithDb = (db: RelayDb.RelayDb["Service"]) =>ManagedEndpointAllocations.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, db)));describe("ManagedEndpointAllocations", () => {+ it.effect("returns a claim generation only when deprovision wins the allocation CAS", () => {+ let claimedAt: string | undefined;+ const fakeDb = {+ update: (table: unknown) => {+ expect(table).toBe(relayManagedEndpointAllocations);+ return {+ set: (values: { readonly updatedAt: string }) => {+ claimedAt = values.updatedAt;+ return {+ where: () => ({+ returning: () => Effect.succeed([{ userId: "user-1" }]),+ }),+ };+ },+ };+ },+ } as unknown as RelayDb.RelayDb["Service"];++ return Effect.gen(function* () {+ const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;+ const generation = yield* allocations.claimDeprovision({+ userId: "user-1",+ environmentId: "environment-1",+ updatedAt: "captured-generation",+ });++ expect(generation).toBe(claimedAt);+ expect(generation).not.toBeNull();+ }).pipe(Effect.provide(layerWithDb(fakeDb)));+ });++ it.effect("does not remove an allocation superseded after a deprovision claim", () => {+ const fakeDb = {+ delete: (table: unknown) => {+ expect(table).toBe(relayManagedEndpointAllocations);+ return {+ where: () => ({+ returning: () => Effect.succeed([]),+ }),+ };+ },+ } as unknown as RelayDb.RelayDb["Service"];++ return Effect.gen(function* () {+ const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;+ expect(+ yield* allocations.removeClaimed({+ userId: "user-1",+ environmentId: "environment-1",+ updatedAt: "outdated-claim-generation",+ }),+ ).toBe(false);+ }).pipe(Effect.provide(layerWithDb(fakeDb)));+ });+it.effect("retains database failures with allocation operation and identity", () => {const cause = new Error("database unavailable");const fakeDb = {diff --git a/infra/relay/src/environments/ManagedEndpointAllocations.ts b/infra/relay/src/environments/ManagedEndpointAllocations.tsindex f0349de99..4320eeea3 100644--- a/infra/relay/src/environments/ManagedEndpointAllocations.ts+++ b/infra/relay/src/environments/ManagedEndpointAllocations.ts@@ -51,7 +51,9 @@ export class ManagedEndpointAllocationPersistenceError extends Schema.TaggedErro"record-dns","mark-ready","claim-release",+ "claim-deprovision","remove",+ "remove-claimed",]),stage: Schema.Literals(["database-request", "resolve-reservation"]),userId: Schema.String,@@ -91,6 +93,14 @@ interface ClaimManagedEndpointReleaseInput extends ManagedEndpointAllocationKeyreadonly updatedAt: string;}+interface ClaimManagedEndpointDeprovisionInput extends ManagedEndpointAllocationKey {+ readonly updatedAt: string;+}++interface RemoveClaimedManagedEndpointAllocationInput extends ManagedEndpointAllocationKey {+ readonly updatedAt: string;+}+export class ManagedEndpointAllocations extends Context.Service<ManagedEndpointAllocations,{@@ -119,9 +129,22 @@ export class ManagedEndpointAllocations extends Context.Service<readonly claimRelease: (input: ClaimManagedEndpointReleaseInput,) => Effect.Effect<boolean, ManagedEndpointAllocationPersistenceError>;+ /**+ * Claims the complete allocation for teardown only if its generation still+ * matches the snapshot captured by the unlink operation.+ *+ * Returns the claim generation used by `removeClaimed`, or null when a+ * concurrent provision has already superseded the snapshot.+ */+ readonly claimDeprovision: (+ input: ClaimManagedEndpointDeprovisionInput,+ ) => Effect.Effect<string | null, ManagedEndpointAllocationPersistenceError>;readonly remove: (input: ManagedEndpointAllocationKey,) => Effect.Effect<void, ManagedEndpointAllocationPersistenceError>;+ readonly removeClaimed: (+ input: RemoveClaimedManagedEndpointAllocationInput,+ ) => Effect.Effect<boolean, ManagedEndpointAllocationPersistenceError>;}>()("t3code-relay/environments/ManagedEndpointAllocations") {}@@ -321,6 +344,35 @@ export const make = Effect.gen(function* () {);return claimed;}),+ claimDeprovision: Effect.fn("relay.managed_endpoint_allocations.claim_deprovision")(function* (+ input: ClaimManagedEndpointDeprovisionInput,+ ) {+ const claimedAt = DateTime.formatIso(yield* DateTime.now);+ const claimed = yield* db+ .update(relayManagedEndpointAllocations)+ .set({ updatedAt: claimedAt })+ .where(+ and(+ whereAllocation(input),+ eq(relayManagedEndpointAllocations.updatedAt, input.updatedAt),+ ),+ )+ .returning({ userId: relayManagedEndpointAllocations.userId })+ .pipe(+ Effect.map((rows) => rows.length > 0),+ Effect.mapError(+ (cause) =>+ new ManagedEndpointAllocationPersistenceError({+ operation: "claim-deprovision",+ stage: "database-request",+ userId: input.userId,+ environmentId: input.environmentId,+ cause,+ }),+ ),+ );+ return claimed ? claimedAt : null;+ }),remove: Effect.fn("relay.managed_endpoint_allocations.remove")(function* (input: ManagedEndpointAllocationKey,) {@@ -339,6 +391,32 @@ export const make = Effect.gen(function* () {),);}),+ removeClaimed: Effect.fn("relay.managed_endpoint_allocations.remove_claimed")(function* (+ input: RemoveClaimedManagedEndpointAllocationInput,+ ) {+ return yield* db+ .delete(relayManagedEndpointAllocations)+ .where(+ and(+ whereAllocation(input),+ eq(relayManagedEndpointAllocations.updatedAt, input.updatedAt),+ ),+ )+ .returning({ userId: relayManagedEndpointAllocations.userId })+ .pipe(+ Effect.map((rows) => rows.length > 0),+ Effect.mapError(+ (cause) =>+ new ManagedEndpointAllocationPersistenceError({+ operation: "remove-claimed",+ stage: "database-request",+ userId: input.userId,+ environmentId: input.environmentId,+ cause,+ }),+ ),+ );+ }),});});diff --git a/infra/relay/src/environments/ManagedEndpointProvider.test.ts b/infra/relay/src/environments/ManagedEndpointProvider.test.tsindex a26cd3042..4d136658c 100644--- a/infra/relay/src/environments/ManagedEndpointProvider.test.ts+++ b/infra/relay/src/environments/ManagedEndpointProvider.test.ts@@ -50,7 +50,9 @@ interface AllocationCall {| "recordDns"| "markReady"| "claimRelease"- | "remove";+ | "claimDeprovision"+ | "remove"+ | "removeClaimed";readonly input: unknown;}@@ -226,11 +228,31 @@ function makeAllocations(calls: AllocationCall[] = []) {mutate(allocationKey(input), (current) => current);return true;}),+ claimDeprovision: (input) =>+ Effect.sync(() => {+ calls.push({ operation: "claimDeprovision", input });+ const allocation = allocations.get(allocationKey(input));+ if (allocation === undefined || allocation.updatedAt !== input.updatedAt) {+ return null;+ }+ mutate(allocationKey(input), (current) => current);+ return allocations.get(allocationKey(input))?.updatedAt ?? null;+ }),remove: (input) =>Effect.sync(() => {calls.push({ operation: "remove", input });allocations.delete(allocationKey(input));}),+ removeClaimed: (input) =>+ Effect.sync(() => {+ calls.push({ operation: "removeClaimed", input });+ const allocation = allocations.get(allocationKey(input));+ if (allocation === undefined || allocation.updatedAt !== input.updatedAt) {+ return false;+ }+ allocations.delete(allocationKey(input));+ return true;+ }),});}@@ -774,12 +796,54 @@ describe("ManagedEndpointProvider", () => {"recordDns","markReady","get",- "remove",+ "claimDeprovision",+ "removeClaimed",]);}).pipe(Effect.provide(layer));},);+ it.effect("does not deprovision an allocation superseded by a concurrent relink", () => {+ const tunnelCalls: TunnelCall[] = [];+ const dnsCalls: DnsCall[] = [];+ const allocationCalls: AllocationCall[] = [];+ const layer = providerLayer(+ makePersistentTunnelClient(tunnelCalls),+ makeDnsClient(dnsCalls),+ makeAllocations(allocationCalls),+ );++ return Effect.gen(function* () {+ const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;+ const key = { userId: "user_ABC", environmentId: "env_ABC" } as const;+ const request = {+ ...key,+ origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },+ } as const;+ yield* provider.provision(request);+ const unlinkTarget = yield* provider.prepareDeprovision(key);+ expect(unlinkTarget).not.toBeNull();+ if (unlinkTarget === null) {+ return;+ }++ // A relink refreshes the allocation generation after unlink captured its+ // target but before unlink begins external teardown.+ yield* provider.provision(request);+ const tunnelCallCount = tunnelCalls.length;+ const dnsCallCount = dnsCalls.length;+ const allocationCallCount = allocationCalls.length;++ yield* provider.deprovision({ ...key, target: unlinkTarget });++ expect(tunnelCalls).toHaveLength(tunnelCallCount);+ expect(dnsCalls).toHaveLength(dnsCallCount);+ expect(allocationCalls.slice(allocationCallCount).map((call) => call.operation)).toEqual([+ "claimDeprovision",+ ]);+ }).pipe(Effect.provide(layer));+ });+it.effect("releases the tunnel while keeping the allocation, DNS record, and hostname", () => {const tunnelCalls: TunnelCall[] = [];const dnsCalls: DnsCall[] = [];@@ -1004,8 +1068,10 @@ describe("ManagedEndpointProvider", () => {"recordDns","markReady","get",+ "claimDeprovision","get",- "remove",+ "claimDeprovision",+ "removeClaimed",]);}).pipe(Effect.provide(layer));});@@ -1046,7 +1112,7 @@ describe("ManagedEndpointProvider", () => {});yield* provider.deprovision(key);- expect(allocationCalls.map((call) => call.operation)).toContain("remove");+ expect(allocationCalls.map((call) => call.operation)).toContain("removeClaimed");}).pipe(Effect.provide(layer));});diff --git a/infra/relay/src/environments/ManagedEndpointProvider.ts b/infra/relay/src/environments/ManagedEndpointProvider.tsindex 46875b121..9a5788748 100644--- a/infra/relay/src/environments/ManagedEndpointProvider.ts+++ b/infra/relay/src/environments/ManagedEndpointProvider.ts@@ -77,6 +77,7 @@ export class ManagedEndpointProvisioningFailed extends Schema.TaggedErrorClass<Mconst ManagedEndpointDeprovisioningStage = Schema.Literals(["load-allocation","claim-release",+ "claim-deprovision","delete-dns-record","delete-tunnel","remove-allocation",@@ -123,6 +124,8 @@ export interface ManagedEndpointProvisioningResult {readonly runtime: RelayManagedEndpointRuntimeConfig;}+export type ManagedEndpointDeprovisionTarget = ManagedEndpointAllocations.ManagedEndpointAllocation;+export class ManagedEndpointProvider extends Context.Service<ManagedEndpointProvider,{@@ -131,9 +134,22 @@ export class ManagedEndpointProvider extends Context.Service<readonly environmentId: string;readonly origin: RelayManagedEndpointOrigin;}) => Effect.Effect<ManagedEndpointProvisioningResult, ManagedEndpointProviderError>;+ /**+ * Captures the allocation generation owned by an unlink before its link+ * revocation commits. Passing this target to `deprovision` prevents a+ * concurrent relink from having its newer allocation torn down.+ */+ readonly prepareDeprovision: (input: {+ readonly userId: string;+ readonly environmentId: string;+ }) => Effect.Effect<+ ManagedEndpointDeprovisionTarget | null,+ ManagedEndpointDeprovisioningFailed+ >;readonly deprovision: (input: {readonly userId: string;readonly environmentId: string;+ readonly target?: ManagedEndpointDeprovisionTarget | null;}) => Effect.Effect<void, ManagedEndpointDeprovisioningFailed>;/*** Deletes the provisioned Cloudflare tunnel while keeping the allocation@@ -424,13 +440,9 @@ export const make = Effect.gen(function* () {);});- return ManagedEndpointProvider.of({- deprovision: Effect.fn("relay.managed_endpoint_provider.deprovision")(function* (input) {- yield* Effect.annotateCurrentSpan({- "relay.user_id": input.userId,- "relay.environment_id": input.environmentId,- });- const allocation = yield* allocations.get(input).pipe(+ const prepareDeprovision = Effect.fn("relay.managed_endpoint_provider.prepare_deprovision")(+ function* (input: { readonly userId: string; readonly environmentId: string }) {+ return yield* allocations.get(input).pipe(Effect.mapError((cause) =>new ManagedEndpointDeprovisioningFailed({@@ -440,9 +452,42 @@ export const make = Effect.gen(function* () {}),),);+ },+ );++ return ManagedEndpointProvider.of({+ prepareDeprovision,+ deprovision: Effect.fn("relay.managed_endpoint_provider.deprovision")(function* (input) {+ yield* Effect.annotateCurrentSpan({+ "relay.user_id": input.userId,+ "relay.environment_id": input.environmentId,+ });+ const allocation =+ input.target === undefined ? yield* prepareDeprovision(input) : input.target;if (allocation === null) {return;}+ const claimedAt = yield* allocations+ .claimDeprovision({+ userId: input.userId,+ environmentId: input.environmentId,+ updatedAt: allocation.updatedAt,+ })+ .pipe(+ Effect.mapError(+ (cause) =>+ new ManagedEndpointDeprovisioningFailed({+ ...input,+ stage: "claim-deprovision",+ ...(allocation.tunnelId === null ? {} : { tunnelId: allocation.tunnelId }),+ ...(allocation.dnsRecordId === null ? {} : { dnsRecordId: allocation.dnsRecordId }),+ cause,+ }),+ ),+ );+ if (claimedAt === null) {+ return;+ }const dnsRecordId = allocation.dnsRecordId;if (dnsRecordId !== null) {yield* ignoreNotFound(dns.deleteRecord(dnsRecordId)).pipe(@@ -471,18 +516,24 @@ export const make = Effect.gen(function* () {),);}- yield* allocations.remove(input).pipe(- Effect.mapError(- (cause) =>- new ManagedEndpointDeprovisioningFailed({- ...input,- stage: "remove-allocation",- ...(allocation.tunnelId === null ? {} : { tunnelId: allocation.tunnelId }),- ...(allocation.dnsRecordId === null ? {} : { dnsRecordId: allocation.dnsRecordId }),- cause,- }),- ),- );+ yield* allocations+ .removeClaimed({+ userId: input.userId,+ environmentId: input.environmentId,+ updatedAt: claimedAt,+ })+ .pipe(+ Effect.mapError(+ (cause) =>+ new ManagedEndpointDeprovisioningFailed({+ ...input,+ stage: "remove-allocation",+ ...(allocation.tunnelId === null ? {} : { tunnelId: allocation.tunnelId }),+ ...(allocation.dnsRecordId === null ? {} : { dnsRecordId: allocation.dnsRecordId }),+ cause,+ }),+ ),+ );}),release: Effect.fn("relay.managed_endpoint_provider.release")(function* (input) {yield* Effect.annotateCurrentSpan({diff --git a/infra/relay/src/environments/ManagedTunnelLimits.test.ts b/infra/relay/src/environments/ManagedTunnelLimits.test.tsindex 45d4db8fe..b53a487ee 100644--- a/infra/relay/src/environments/ManagedTunnelLimits.test.ts+++ b/infra/relay/src/environments/ManagedTunnelLimits.test.ts@@ -4,6 +4,7 @@ import * as Layer from "effect/Layer";import * as RelayDb from "../db.ts";import {+ relayEnvironmentLinks,relayManagedEndpointAllocations,relayManagedTunnelLimits,} from "../persistence/schema.ts";@@ -28,7 +29,16 @@ function makeFakeDb(input: {}expect(table).toBe(relayManagedEndpointAllocations);return {- where: () => input.countRows ?? Effect.succeed([{ activeTunnels: 0 }]),+ innerJoin: (table: unknown, condition: unknown) => {+ expect(table).toBe(relayEnvironmentLinks);+ expect(condition).toBeDefined();+ return {+ where: (where: unknown) => {+ expect(where).toBeDefined();+ return input.countRows ?? Effect.succeed([{ activeTunnels: 0 }]);+ },+ };+ },};},}),diff --git a/infra/relay/src/environments/ManagedTunnelLimits.ts b/infra/relay/src/environments/ManagedTunnelLimits.tsindex bab4d882e..9d8b9c796 100644--- a/infra/relay/src/environments/ManagedTunnelLimits.ts+++ b/infra/relay/src/environments/ManagedTunnelLimits.ts@@ -1,4 +1,4 @@-import { and, count, eq, ne } from "drizzle-orm";+import { and, count, eq, isNull, ne } from "drizzle-orm";import * as Context from "effect/Context";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";@@ -6,6 +6,7 @@ import * as Schema from "effect/Schema";import * as RelayDb from "../db.ts";import {+ relayEnvironmentLinks,relayManagedEndpointAllocations,relayManagedTunnelLimits,} from "../persistence/schema.ts";@@ -81,10 +82,19 @@ export const make = Effect.gen(function* () {const counted = yield* db.select({ activeTunnels: count() }).from(relayManagedEndpointAllocations)+ .innerJoin(+ relayEnvironmentLinks,+ and(+ eq(relayEnvironmentLinks.userId, relayManagedEndpointAllocations.userId),+ eq(relayEnvironmentLinks.environmentId, relayManagedEndpointAllocations.environmentId),+ ),+ ).where(and(eq(relayManagedEndpointAllocations.userId, input.userId),ne(relayManagedEndpointAllocations.environmentId, input.environmentId),+ isNull(relayEnvironmentLinks.revokedAt),+ eq(relayEnvironmentLinks.managedTunnelsEnabled, true),),).pipe(diff --git a/infra/relay/src/http/Api.test.ts b/infra/relay/src/http/Api.test.tsindex b43a28375..daf756a2b 100644--- a/infra/relay/src/http/Api.test.ts+++ b/infra/relay/src/http/Api.test.ts@@ -14,6 +14,7 @@ import * as Tracer from "effect/Tracer";import * as HttpRouter from "effect/unstable/http/HttpRouter";import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";+import { EnvironmentId } from "@t3tools/contracts";import { RelayEnvironmentAuth } from "@t3tools/contracts/relay";import {@@ -22,12 +23,17 @@ import {relayDocsRedirectRoute,relayEnvironmentAuthLayer,relayNotFoundRoute,+ revokeEnvironmentLinkRecord,traceRelayHttpRequestWith,+ unlinkEnvironmentRecord,verifyRelayClientBearerToken,withoutCapturedParentSpan,} from "./Api.ts";import * as RelayConfiguration from "../Config.ts";+import * as RelayDb from "../db.ts";import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts";+import * as EnvironmentLinks from "../environments/EnvironmentLinks.ts";+import * as ManagedEndpointProvider from "../environments/ManagedEndpointProvider.ts";vi.mock("@clerk/backend", () => ({createClerkClient: vi.fn(),@@ -155,6 +161,240 @@ describe("relay environment authentication", () => {});});+function relayUnlinkTestLayer(input?: {+ readonly withTransaction?: RelayDb.RelayTransactions["Service"]["withTransaction"];+ readonly getForUser?: EnvironmentLinks.EnvironmentLinks["Service"]["getForUser"];+ readonly revokeForUser?: EnvironmentLinks.EnvironmentLinks["Service"]["revokeForUser"];+ readonly revokeCredential?: EnvironmentCredentials.EnvironmentCredentials["Service"]["revokeForEnvironmentPublicKey"];+ readonly prepareDeprovision?: ManagedEndpointProvider.ManagedEndpointProvider["Service"]["prepareDeprovision"];+ readonly deprovision?: ManagedEndpointProvider.ManagedEndpointProvider["Service"]["deprovision"];+}) {+ return Layer.mergeAll(+ Layer.succeed(+ RelayDb.RelayTransactions,+ RelayDb.RelayTransactions.of({+ withTransaction: input?.withTransaction ?? ((effect) => effect),+ }),+ ),+ Layer.succeed(+ EnvironmentLinks.EnvironmentLinks,+ EnvironmentLinks.EnvironmentLinks.of({+ upsert: () => Effect.die("unused upsert"),+ listUsersForEnvironment: () => Effect.die("unused listUsersForEnvironment"),+ listDeliveryUsersForEnvironment: () => Effect.die("unused listDeliveryUsersForEnvironment"),+ listPublicKeysForEnvironment: () => Effect.die("unused listPublicKeysForEnvironment"),+ listForUser: () => Effect.die("unused listForUser"),+ getForUser: input?.getForUser ?? (() => Effect.succeed(null)),+ revokeForUser: input?.revokeForUser ?? (() => Effect.succeed(false)),+ }),+ ),+ Layer.succeed(+ EnvironmentCredentials.EnvironmentCredentials,+ EnvironmentCredentials.EnvironmentCredentials.of({+ create: () => Effect.die("unused create"),+ authenticate: () => Effect.die("unused authenticate"),+ revokeForEnvironmentPublicKey: input?.revokeCredential ?? (() => Effect.succeed(false)),+ }),+ ),+ Layer.succeed(+ ManagedEndpointProvider.ManagedEndpointProvider,+ ManagedEndpointProvider.ManagedEndpointProvider.of({+ provision: () => Effect.die("unused provision"),+ prepareDeprovision: input?.prepareDeprovision ?? (() => Effect.succeed(null)),+ deprovision: input?.deprovision ?? (() => Effect.void),+ release: () => Effect.die("unused release"),+ }),+ ),+ );+}++const linkedEnvironmentRecord = {+ environmentId: EnvironmentId.make("environment-1"),+ label: "Environment 1",+ endpoint: {+ httpBaseUrl: "https://environment-1.example.test/",+ wsBaseUrl: "wss://environment-1.example.test/ws",+ providerKind: "cloudflare_tunnel",+ },+ environmentPublicKey: "public-key",+ linkedAt: "2026-07-28T00:00:00.000Z",+} as const;++describe("relay environment unlink", () => {+ it.effect("revokes the link and its credentials in one database transaction", () => {+ const calls: Array<string> = [];+ return Effect.gen(function* () {+ expect(+ yield* revokeEnvironmentLinkRecord({+ userId: "user-1",+ environmentId: "environment-1",+ environmentPublicKey: "public-key",+ }),+ ).toBe(true);+ expect(calls).toEqual(["transaction", "link", "credential"]);+ }).pipe(+ Effect.provide(+ relayUnlinkTestLayer({+ withTransaction: (effect) => {+ calls.push("transaction");+ return effect;+ },+ revokeForUser: () =>+ Effect.sync(() => {+ calls.push("link");+ return true;+ }),+ revokeCredential: () =>+ Effect.sync(() => {+ calls.push("credential");+ return true;+ }),+ }),+ ),+ );+ });++ it.effect("commits database revocation before deprovisioning the managed endpoint", () => {+ const calls: Array<string> = [];+ const deprovisionTarget = {+ userId: "user-1",+ environmentId: "environment-1",+ hostname: "environment-1.example.test",+ tunnelId: "tunnel-1",+ tunnelName: "environment-1-tunnel",+ dnsRecordId: "dns-1",+ readyAt: "2026-07-28T00:00:00.000Z",+ updatedAt: "generation-before-unlink",+ } satisfies ManagedEndpointProvider.ManagedEndpointDeprovisionTarget;++ return Effect.gen(function* () {+ expect(+ yield* unlinkEnvironmentRecord({+ userId: "user-1",+ environmentId: "environment-1",+ }),+ ).toBe(true);+ expect(calls).toEqual([+ "prepare",+ "lookup",+ "transaction",+ "link",+ "credential",+ "deprovision",+ ]);+ }).pipe(+ Effect.provide(+ relayUnlinkTestLayer({+ withTransaction: (effect) => {+ calls.push("transaction");+ return effect;+ },+ getForUser: () =>+ Effect.sync(() => {+ calls.push("lookup");+ return linkedEnvironmentRecord;+ }),+ revokeForUser: () =>+ Effect.sync(() => {+ calls.push("link");+ return true;+ }),+ revokeCredential: () =>+ Effect.sync(() => {+ calls.push("credential");+ return true;+ }),+ prepareDeprovision: () =>+ Effect.sync(() => {+ calls.push("prepare");+ return deprovisionTarget;+ }),+ deprovision: (request) =>+ Effect.sync(() => {+ expect(request.target).toBe(deprovisionTarget);+ calls.push("deprovision");+ }),+ }),+ ),+ );+ });++ it.effect("does not deprovision when database revocation fails", () => {+ const calls: Array<string> = [];+ const failure = new EnvironmentCredentials.EnvironmentCredentialRevokePersistenceError({+ environmentId: "environment-1",+ cause: "database unavailable",+ });++ return Effect.gen(function* () {+ expect(+ yield* Effect.flip(+ unlinkEnvironmentRecord({+ userId: "user-1",+ environmentId: "environment-1",+ }),+ ),+ ).toBe(failure);+ expect(calls).toEqual(["prepare", "transaction", "link", "credential"]);+ }).pipe(+ Effect.provide(+ relayUnlinkTestLayer({+ withTransaction: (effect) => {+ calls.push("transaction");+ return effect;+ },+ getForUser: () => Effect.succeed(linkedEnvironmentRecord),+ revokeForUser: () =>+ Effect.sync(() => {+ calls.push("link");+ return true;+ }),+ revokeCredential: () =>+ Effect.sync(() => {+ calls.push("credential");+ }).pipe(Effect.andThen(Effect.fail(failure))),+ prepareDeprovision: () =>+ Effect.sync(() => {+ calls.push("prepare");+ return null;+ }),+ deprovision: () =>+ Effect.sync(() => {+ calls.push("deprovision");+ }),+ }),+ ),+ );+ });++ it.effect("retries deprovisioning after the link is already revoked", () => {+ const calls: Array<string> = [];+ return Effect.gen(function* () {+ expect(+ yield* unlinkEnvironmentRecord({+ userId: "user-1",+ environmentId: "environment-1",+ }),+ ).toBe(false);+ expect(calls).toEqual(["prepare", "deprovision"]);+ }).pipe(+ Effect.provide(+ relayUnlinkTestLayer({+ prepareDeprovision: () =>+ Effect.sync(() => {+ calls.push("prepare");+ return null;+ }),+ deprovision: () =>+ Effect.sync(() => {+ calls.push("deprovision");+ }),+ }),+ ),+ );+ });+});+describe("relay request tracing", () => {it.effect("does not parent endpoint spans to an ambient parent captured while building handlers",diff --git a/infra/relay/src/http/Api.ts b/infra/relay/src/http/Api.tsindex 0d5813ab7..7510b4a8d 100644--- a/infra/relay/src/http/Api.ts+++ b/infra/relay/src/http/Api.ts@@ -403,6 +403,67 @@ export const healthApi = HttpApiBuilder.group(}),);+export const revokeEnvironmentLinkRecord = Effect.fn(+ "relay.api.client.revokeEnvironmentLinkRecord",+)(function* (input: {+ readonly userId: string;+ readonly environmentId: string;+ readonly environmentPublicKey: string;+}) {+ const transactions = yield* RelayDb.RelayTransactions;+ const links = yield* EnvironmentLinks.EnvironmentLinks;+ const credentials = yield* EnvironmentCredentials.EnvironmentCredentials;+ return yield* transactions.withTransaction(+ Effect.gen(function* () {+ const revoked = yield* links.revokeForUser({+ userId: input.userId,+ environmentId: input.environmentId,+ });+ if (revoked) {+ yield* credentials.revokeForEnvironmentPublicKey({+ environmentId: input.environmentId,+ environmentPublicKey: input.environmentPublicKey,+ });+ }+ return revoked;+ }),+ );+});++export const unlinkEnvironmentRecord = Effect.fn("relay.api.client.unlinkEnvironmentRecord")(+ function* (input: { readonly userId: string; readonly environmentId: string }) {+ const links = yield* EnvironmentLinks.EnvironmentLinks;+ const managedEndpointProvider = yield* ManagedEndpointProvider.ManagedEndpointProvider;+ const deprovisionTarget = yield* managedEndpointProvider.prepareDeprovision({+ userId: input.userId,+ environmentId: input.environmentId,+ });+ const link = yield* links.getForUser({+ userId: input.userId,+ environmentId: input.environmentId,+ });+ const unlinked =+ link === null+ ? false+ : yield* revokeEnvironmentLinkRecord({+ userId: input.userId,+ environmentId: link.environmentId,+ environmentPublicKey: link.environmentPublicKey,+ });++ // External teardown cannot share the SQL transaction. Run it only after+ // revocation commits so a database failure leaves a fully usable active+ // link. Still run teardown when the link is already revoked, allowing a+ // retry to finish cleanup after an earlier Cloudflare failure.+ yield* managedEndpointProvider.deprovision({+ userId: input.userId,+ environmentId: input.environmentId,+ target: deprovisionTarget,+ });+ return unlinked;+ },+);+export const mobileApi = HttpApiBuilder.group(RelayApi,"mobile",@@ -470,7 +531,6 @@ export const clientApi = HttpApiBuilder.group(const linker = yield* EnvironmentLinker.EnvironmentLinker;const links = yield* EnvironmentLinks.EnvironmentLinks;const managedEndpointProvider = yield* ManagedEndpointProvider.ManagedEndpointProvider;- const credentials = yield* EnvironmentCredentials.EnvironmentCredentials;const devices = yield* Devices.Devices;return handlers.handle(@@ -592,29 +652,16 @@ export const clientApi = HttpApiBuilder.group(Effect.fn("relay.api.client.unlinkEnvironment")(function* (args) {const { params } = args;const { userId } = yield* RelayClientPrincipal;- yield* managedEndpointProvider- .deprovision({- userId,- environmentId: params.environmentId,- })- .pipe(Effect.catch(() => relayInternalErrorResponse("upstream_unavailable")));- const link = yield* links.getForUser({- userId,- environmentId: params.environmentId,- });- if (link === null) {- return { ok: false };- }- const unlinked = yield* links.revokeForUser({+ const unlinked = yield* unlinkEnvironmentRecord({userId,environmentId: params.environmentId,- });- if (unlinked) {- yield* credentials.revokeForEnvironmentPublicKey({- environmentId: link.environmentId,- environmentPublicKey: link.environmentPublicKey,- });- }+ }).pipe(+ Effect.catchTags({+ SqlError: () => relayInternalErrorResponse("internal_error"),+ ManagedEndpointDeprovisioningFailed: () =>+ relayInternalErrorResponse("upstream_unavailable"),+ }),+ );return { ok: unlinked };}, mapRelayCommonApiErrors("not_authorized")),)diff --git a/infra/relay/src/worker.ts b/infra/relay/src/worker.tsindex d0fabe8de..bcc53f3ff 100644--- a/infra/relay/src/worker.ts+++ b/infra/relay/src/worker.ts@@ -217,7 +217,11 @@ export const ApiLive = Api.make(Layer.provideMerge(LiveActivities.layer),Layer.provideMerge(DeliveryAttempts.layer),Layer.provideMerge(RelayTokens.layer),- Layer.provideMerge(Layer.succeed(RelayDb.RelayDb, db)),+ Layer.provideMerge(+ RelayDb.RelayTransactions.layer.pipe(+ Layer.provideMerge(Layer.succeed(RelayDb.RelayDb, db)),+ ),+ ),Layer.provideMerge(Layer.effect(RelayConfiguration.RelayConfiguration, loadSettings)),Layer.provideMerge(webcryptoLayer),);