The best way to control Claude Code, Codex and more from any computer or phone
fix(relay): T3 Connect repairs tunnel ingress that drifted from a matching origin (#27961)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 2, 2026 at 18:51 UTC
0c863d63ec8271ddae7e6729d30b07d228898233
3 files changed
+204
-24
infra/relay/src/environments/ManagedEndpointProvider.test.ts
+136
-2
@@ -4,6 +4,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
4
import { describe, expect, it } from "@effect/vitest";
5
import * as Alchemy from "alchemy";
6
import * as Cloudflare from "@alchemy.run/cloudflare";
7
+import * as ZeroTrust from "@distilled.cloud/cloudflare/zero-trust";
8
import * as Deferred from "effect/Deferred";
9
import * as Effect from "effect/Effect";
10
import * as Fiber from "effect/Fiber";
@@ -36,10 +37,25 @@ const config = RelayConfiguration.RelayConfiguration.of({
37
});
38
39
interface TunnelCall {
39
- readonly operation: "get" | "list" | "create" | "putConfiguration" | "getToken" | "delete";
40
+ readonly operation:
41
+ | "get"
42
+ | "list"
43
+ | "create"
44
+ | "putConfiguration"
45
+ | "getConfiguration"
46
+ | "getToken"
47
+ | "delete";
48
readonly input: unknown;
49
}
50
51
+interface TunnelConfiguration {
52
+ readonly ingress: ReadonlyArray<{
53
+ readonly hostname?: string;
54
+ readonly path?: string;
55
+ readonly service: string;
56
+ }>;
57
+}
58
+
59
interface DnsCall {
60
readonly operation: "listRecords" | "createRecord" | "updateRecord" | "deleteRecord";
61
readonly input: unknown;
@@ -88,6 +104,11 @@ function makeTunnelClient(calls: TunnelCall[] = []) {
104
Effect.sync(() => {
105
calls.push({ operation: "putConfiguration", input: { tunnelId, tunnelConfig } });
106
}),
107
+ getConfiguration: (tunnelId) =>
108
+ Effect.sync(() => {
109
+ calls.push({ operation: "getConfiguration", input: tunnelId });
110
+ return { ingress: [] };
111
+ }),
112
getToken: (tunnelId) =>
113
Effect.sync(() => {
114
calls.push({ operation: "getToken", input: tunnelId });
@@ -100,7 +121,10 @@ function makeTunnelClient(calls: TunnelCall[] = []) {
121
});
122
}
123
103
-function makePersistentTunnelClient(calls: TunnelCall[] = []) {
124
+function makePersistentTunnelClient(
125
+ calls: TunnelCall[] = [],
126
+ configuration: { current: TunnelConfiguration | null } = { current: null },
127
+) {
128
let tunnel: { readonly id: string; readonly name: string } | null = null;
129
return ManagedEndpointProvider.ManagedEndpointTunnelClient.of({
130
get: (tunnelId) =>
@@ -134,6 +158,12 @@ function makePersistentTunnelClient(calls: TunnelCall[] = []) {
158
putConfiguration: (tunnelId, tunnelConfig) =>
159
Effect.sync(() => {
160
calls.push({ operation: "putConfiguration", input: { tunnelId, tunnelConfig } });
161
+ configuration.current = tunnelConfig;
162
+ }),
163
+ getConfiguration: (tunnelId: string) =>
164
+ Effect.sync(() => {
165
+ calls.push({ operation: "getConfiguration", input: tunnelId });
166
+ return configuration.current ?? { ingress: [] };
167
}),
168
getToken: (tunnelId) =>
169
Effect.sync(() => {
@@ -787,6 +817,44 @@ describe("ManagedEndpointProvider", () => {
817
});
818
});
819
820
+ it.effect("reads a tunnel without remote configuration as having no ingress", () => {
821
+ const tunnelClient = {
822
+ getConfiguration: () =>
823
+ Effect.fail(
824
+ new ZeroTrust.TunnelConfigurationNotFound({
825
+ code: 1055,
826
+ message: "Tunnel configuration not found",
827
+ }),
828
+ ),
829
+ } as unknown as Cloudflare.Tunnel.ReadWriteTunnelClient;
830
+ const layer = ManagedEndpointProvider.layerCloudflareBindings(
831
+ tunnelClient,
832
+ {} as Cloudflare.DNS.ReadWriteDnsClient,
833
+ {} as Alchemy.BaseRuntimeContext,
834
+ ).pipe(
835
+ Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
836
+ Layer.provide(RelayConfiguration.layer(config)),
837
+ Layer.provide(
838
+ Layer.succeed(ManagedEndpointAllocations.ManagedEndpointAllocations, makeAllocations()),
839
+ ),
840
+ Layer.provide(Layer.succeed(ManagedTunnelLimits.ManagedTunnelLimits, makeTunnelLimits())),
841
+ Layer.provide(
842
+ Layer.succeed(
843
+ CloudflareApiPause.CloudflareApiPause,
844
+ CloudflareApiPause.CloudflareApiPause.of({
845
+ pausedUntilMillis: Effect.succeed(0),
846
+ pauseUntil: () => Effect.void,
847
+ }),
848
+ ),
849
+ ),
850
+ );
851
+
852
+ return Effect.gen(function* () {
853
+ const tunnels = yield* ManagedEndpointProvider.ManagedEndpointTunnelClient;
854
+ expect(yield* tunnels.getConfiguration("tunnel-id")).toEqual({ ingress: [] });
855
+ }).pipe(Effect.provide(layer));
856
+ });
857
+
858
it.effect("does not hide non-not-found checkpoint update failures", () => {
859
const dnsCalls: DnsCall[] = [];
860
const failure = new ManagedEndpointProvider.ManagedEndpointDnsClientError({
@@ -1139,6 +1207,72 @@ describe("ManagedEndpointProvider", () => {
1207
}).pipe(Effect.provide(layer));
1208
});
1209
1210
+ it.effect("restores Cloudflare ingress that drifted from the recorded origin", () => {
1211
+ const tunnelCalls: TunnelCall[] = [];
1212
+ const configuration: { current: TunnelConfiguration | null } = { current: null };
1213
+ const layer = layerProvider(makePersistentTunnelClient(tunnelCalls, configuration));
1214
+
1215
+ return Effect.gen(function* () {
1216
+ const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;
1217
+ const key = { userId: "user_ABC", environmentId: "env_ABC" } as const;
1218
+ const origin = { localHttpHost: "127.0.0.1", localHttpPort: 3773 } as const;
1219
+ const provisioned = yield* provider.provision({ ...key, origin });
1220
+ const reconcile = provider.reconcileOrigin({
1221
+ ...key,
1222
+ tunnelId: provisioned.runtime.tunnelId!,
1223
+ origin,
1224
+ endpoint: provisioned.endpoint,
1225
+ });
1226
+ tunnelCalls.length = 0;
1227
+
1228
+ expect(yield* reconcile).toBe("ready");
1229
+ expect(tunnelCalls.map((call) => call.operation)).toEqual(["get", "getConfiguration"]);
1230
+
1231
+ configuration.current = {
1232
+ ingress: [
1233
+ { hostname: expectedManagedHostname("env_ABC"), service: "http://127.0.0.1:4711" },
1234
+ { service: "http_status:404" },
1235
+ ],
1236
+ };
1237
+ expect(yield* reconcile).toBe("ready");
1238
+ expect(configuration.current).toEqual({
1239
+ ingress: [
1240
+ { hostname: expectedManagedHostname("env_ABC"), service: "http://127.0.0.1:3773" },
1241
+ { service: "http_status:404" },
1242
+ ],
1243
+ });
1244
+
1245
+ configuration.current = {
1246
+ ingress: [
1247
+ { hostname: "*.t3code.test", service: "http://127.0.0.1:4711" },
1248
+ { hostname: expectedManagedHostname("env_ABC"), service: "http://127.0.0.1:3773" },
1249
+ { service: "http_status:404" },
1250
+ ],
1251
+ };
1252
+ expect(yield* reconcile).toBe("ready");
1253
+ expect(configuration.current.ingress[0]).toEqual({
1254
+ hostname: expectedManagedHostname("env_ABC"),
1255
+ service: "http://127.0.0.1:3773",
1256
+ });
1257
+
1258
+ configuration.current = {
1259
+ ingress: [
1260
+ {
1261
+ hostname: expectedManagedHostname("env_ABC"),
1262
+ path: "/api",
1263
+ service: "http://127.0.0.1:3773",
1264
+ },
1265
+ { service: "http_status:404" },
1266
+ ],
1267
+ };
1268
+ expect(yield* reconcile).toBe("ready");
1269
+ expect(configuration.current.ingress[0]).toEqual({
1270
+ hostname: expectedManagedHostname("env_ABC"),
1271
+ service: "http://127.0.0.1:3773",
1272
+ });
1273
+ }).pipe(Effect.provide(layer));
1274
+ });
1275
+
1276
it.effect("rejects an active endpoint that does not match the allocation hostname", () => {
1277
const layer = layerProvider(makePersistentTunnelClient());
1278
infra/relay/src/environments/ManagedEndpointProvider.ts
+67
-22
@@ -216,6 +216,7 @@ const ManagedEndpointTunnelClientOperation = Schema.Literals([
216
"list",
217
"create",
218
"put-configuration",
219
+ "get-configuration",
220
"get-token",
221
"delete",
222
]);
@@ -265,6 +266,16 @@ export class ManagedEndpointTunnelClient extends Context.Service<
266
}>;
267
},
268
) => Effect.Effect<unknown, ManagedEndpointTunnelClientError>;
269
+ readonly getConfiguration: (tunnelId: string) => Effect.Effect<
270
+ {
271
+ readonly ingress: ReadonlyArray<{
272
+ readonly hostname?: string | null;
273
+ readonly path?: string | null;
274
+ readonly service: string;
275
+ }>;
276
+ },
277
+ ManagedEndpointTunnelClientError
278
+ >;
279
readonly getToken: (
280
tunnelId: string,
281
) => Effect.Effect<string, ManagedEndpointTunnelClientError>;
@@ -756,32 +767,53 @@ const make = Effect.gen(function* () {
767
hostname: allocation.hostname,
768
});
769
}
770
+ const verifyTunnel = <A>(read: Effect.Effect<A, ManagedEndpointTunnelClientError>) =>
771
+ read.pipe(
772
+ Effect.asSome,
773
+ Effect.catchTags({
774
+ ManagedEndpointTunnelClientError: (cause) =>
775
+ isManagedEndpointNotFound(cause.cause)
776
+ ? Effect.succeedNone
777
+ : Effect.fail(
778
+ new ManagedEndpointProvisioningFailed({
779
+ userId: input.userId,
780
+ environmentId: input.environmentId,
781
+ stage: "verify-tunnel",
782
+ tunnelId: input.tunnelId,
783
+ cause,
784
+ }),
785
+ ),
786
+ }),
787
+ );
788
// Released records retain tunnel ids; verify Cloudflare still has the tunnel before reconnecting.
760
- const recorded = yield* tunnels.get(input.tunnelId).pipe(
761
- Effect.asSome,
762
- Effect.catchTags({
763
- ManagedEndpointTunnelClientError: (cause) =>
764
- isManagedEndpointNotFound(cause.cause)
765
- ? Effect.succeedNone
766
- : Effect.fail(
767
- new ManagedEndpointProvisioningFailed({
768
- userId: input.userId,
769
- environmentId: input.environmentId,
770
- stage: "verify-tunnel",
771
- tunnelId: input.tunnelId,
772
- cause,
773
- }),
774
- ),
775
- }),
776
- );
789
+ const recorded = yield* verifyTunnel(tunnels.get(input.tunnelId));
790
if (Option.isNone(recorded) || recorded.value.deletedAt) {
791
return "recovery_required";
792
}
793
+ const originService = formatOriginService(input.origin);
794
if (
795
allocation.origin?.localHttpHost === input.origin.localHttpHost &&
796
allocation.origin.localHttpPort === input.origin.localHttpPort
797
) {
784
- return "ready";
798
+ // Live ingress can drift from the recorded origin; only a matching rule skips the rewrite.
799
+ const configuration = yield* verifyTunnel(tunnels.getConfiguration(input.tunnelId));
800
+ if (Option.isNone(configuration)) {
801
+ return "recovery_required";
802
+ }
803
+ const hostname = normalizeHostname(allocation.hostname);
804
+ // Cloudflare routes by the first matching rule, so an earlier wildcard or catch-all wins.
805
+ const rule = configuration.value.ingress.find((entry) => {
806
+ if (entry.hostname == null) return true;
807
+ const candidate = normalizeHostname(entry.hostname);
808
+ return (
809
+ candidate === hostname ||
810
+ (candidate.startsWith("*.") && hostname.endsWith(candidate.slice(1)))
811
+ );
812
+ });
813
+ // A path-restricted rule leaves other paths to later rules, so it cannot prove routing.
814
+ if (rule?.service === originService && !rule.path) {
815
+ return "ready";
816
+ }
817
}
818
819
const updated = yield* allocations
@@ -795,10 +827,7 @@ const make = Effect.gen(function* () {
827
tunnels
828
.putConfiguration(input.tunnelId, {
829
ingress: [
798
- {
799
- hostname: allocation.hostname,
800
- service: formatOriginService(input.origin),
801
- },
830
+ { hostname: allocation.hostname, service: originService },
831
{ service: "http_status:404" },
832
],
833
})
@@ -1606,6 +1635,22 @@ export const layerCloudflareBindings = (
1635
}),
1636
),
1637
),
1638
+ getConfiguration: (tunnelId) =>
1639
+ gate(tunnelClient.getConfiguration(tunnelId)).pipe(
1640
+ Effect.map(({ config }) => ({ ingress: config?.ingress ?? [] })),
1641
+ // Cloudflare reports a tunnel without remote configuration as code 1055, not 404.
1642
+ Effect.catchTags({
1643
+ TunnelConfigurationNotFound: () => Effect.succeed({ ingress: [] }),
1644
+ }),
1645
+ Effect.mapError(
1646
+ (cause) =>
1647
+ new ManagedEndpointTunnelClientError({
1648
+ operation: "get-configuration",
1649
+ tunnelId,
1650
+ cause,
1651
+ }),
1652
+ ),
1653
+ ),
1654
getToken: (tunnelId) =>
1655
gate(tunnelClient.getToken(tunnelId)).pipe(
1656
Effect.mapError(
infra/relay/src/environments/ManagedEndpointReaper.test.ts
+1
@@ -140,6 +140,7 @@ function harness(input?: {
140
}),
141
create: () => Effect.die("unused"),
142
putConfiguration: () => Effect.die("unused"),
143
+ getConfiguration: () => Effect.die("unused"),
144
getToken: () => Effect.die("unused"),
145
delete: (tunnelId) =>
146
input?.failAllDeletes === true ||