Skip to content

Commit bef45aa

Browse files
PR Batch Testerclaude
andcommitted
fix(server): a relink no longer shows the old tunnel's MCP URL
The reachability result is kept until the next check. A stopped connector sends no readiness event, so after relinking to another account the old result, with the old host, stayed visible until the new connector reported in. Each result now remembers the tunnel config it checked and is dropped once that config changes. Also limit the docs' `t3 connect status` claim to servers set up with `t3 connect`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f644e7f commit bef45aa

3 files changed

Lines changed: 97 additions & 12 deletions

File tree

‎apps/server/src/cloud/ConnectReachability.test.ts‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@ import * as NodeFS from "node:fs";
33

44
import { T3_CONNECT_TROUBLESHOOTING_SECTIONS, connectReachableMcpUrl } from "@t3tools/contracts";
55
import { describe, expect, it } from "@effect/vitest";
6+
import * as Effect from "effect/Effect";
7+
import * as Layer from "effect/Layer";
8+
import * as Option from "effect/Option";
9+
import * as Stream from "effect/Stream";
10+
import { HttpClient, HttpClientResponse } from "effect/http";
11+
import { ChildProcessSpawner } from "effect/process";
612

7-
import { classifyReachability, type ReachabilityFacts } from "./ConnectReachability.ts";
13+
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
14+
import { CLOUD_ENDPOINT_RUNTIME_CONFIG } from "./config.ts";
15+
import {
16+
ConnectReachability,
17+
classifyReachability,
18+
layer as connectReachabilityLayer,
19+
type ReachabilityFacts,
20+
} from "./ConnectReachability.ts";
21+
import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts";
822

923
const checkedAt = "2026-10-19T00:00:00.000Z";
1024
const connected = { readyConnections: 4, hostname: "env.example.test", protocol: "quic" };
@@ -78,6 +92,59 @@ describe("classifyReachability", () => {
7892
});
7993
});
8094

95+
describe("ConnectReachability", () => {
96+
it.effect("drops a result once the tunnel config it checked is replaced", () => {
97+
const secrets = new Map([[CLOUD_ENDPOINT_RUNTIME_CONFIG, new TextEncoder().encode("first")]]);
98+
const unused = Effect.die("unused");
99+
const dependencies = Layer.mergeAll(
100+
Layer.succeed(
101+
ServerSecretStore.ServerSecretStore,
102+
ServerSecretStore.ServerSecretStore.of({
103+
get: (name) => Effect.sync(() => Option.fromNullishOr(secrets.get(name))),
104+
set: () => unused,
105+
create: () => unused,
106+
getOrCreateRandom: () => unused,
107+
remove: () => unused,
108+
}),
109+
),
110+
Layer.succeed(
111+
ManagedEndpointRuntime.CloudManagedEndpointRuntime,
112+
ManagedEndpointRuntime.CloudManagedEndpointRuntime.of({
113+
applyConfig: () => unused,
114+
inspectConnector: Effect.succeed(connected),
115+
readinessChanged: Stream.empty,
116+
recoveryRequests: Stream.empty,
117+
requestRecovery: () => unused,
118+
tunnelConnected: Stream.empty,
119+
withLinkStateLock: (effect) => effect,
120+
}),
121+
),
122+
Layer.succeed(
123+
HttpClient.HttpClient,
124+
HttpClient.make((request) =>
125+
Effect.succeed(HttpClientResponse.fromWeb(request, new Response("{}"))),
126+
),
127+
),
128+
Layer.succeed(
129+
ChildProcessSpawner.ChildProcessSpawner,
130+
ChildProcessSpawner.make(() => unused),
131+
),
132+
);
133+
134+
return Effect.gen(function* () {
135+
const reachability = yield* ConnectReachability;
136+
yield* reachability.check;
137+
expect(Option.getOrNull(yield* reachability.latest)).toMatchObject({
138+
hostname: "env.example.test",
139+
});
140+
141+
// A relink stores a new tunnel before its connector reports in.
142+
secrets.set(CLOUD_ENDPOINT_RUNTIME_CONFIG, new TextEncoder().encode("second"));
143+
expect(yield* reachability.latest).toEqual(Option.none());
144+
}).pipe(Effect.provide(connectReachabilityLayer.pipe(Layer.provide(dependencies))));
145+
});
146+
});
147+
81148
describe("T3 Connect troubleshooting links", () => {
82149
it("each section names a heading in the user guide", () => {
83150
const guide = NodeFS.readFileSync(

‎apps/server/src/cloud/ConnectReachability.ts‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ function hasErrorCode(error: unknown, codes: ReadonlySet<string>): boolean {
175175
}
176176

177177
const DNS_ERROR_CODES = new Set(["ENOTFOUND", "EAI_AGAIN", "EAI_NONAME"]);
178+
const textDecoder = new TextDecoder();
178179

179180
const canOpenTcp = (host: string, port: number) =>
180181
Effect.callback<boolean>((resume) => {
@@ -203,8 +204,14 @@ const make = Effect.gen(function* () {
203204
const secrets = yield* ServerSecretStore.ServerSecretStore;
204205
const httpClient = yield* HttpClient.HttpClient;
205206
// `order` ranks checks by start, so a slow older one cannot replace a newer result.
207+
// `tunnelConfig` is the config a result was checked against: after a relink the old
208+
// result names the old tunnel's host until the new connector triggers a check.
206209
const latestRef = yield* Ref.make(
207-
Option.none<{ readonly order: number; readonly result: EnvironmentConnectReachability }>(),
210+
Option.none<{
211+
readonly order: number;
212+
readonly result: EnvironmentConnectReachability;
213+
readonly tunnelConfig: string | null;
214+
}>(),
208215
);
209216
const checkCounter = yield* Ref.make(0);
210217
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
@@ -226,10 +233,14 @@ const make = Effect.gen(function* () {
226233
),
227234
);
228235

229-
const gatherFacts = Effect.gen(function* () {
230-
const managedTunnelConfigured = Option.isSome(
231-
yield* secrets.get(CLOUD_ENDPOINT_RUNTIME_CONFIG).pipe(Effect.orElseSucceed(Option.none)),
232-
);
236+
const readTunnelConfig = secrets.get(CLOUD_ENDPOINT_RUNTIME_CONFIG).pipe(
237+
Effect.map(Option.match({ onNone: () => null, onSome: (bytes) => textDecoder.decode(bytes) })),
238+
Effect.orElseSucceed(() => null),
239+
);
240+
241+
// Runs only inside the `ConnectReachability.check` span.
242+
const gatherFacts = Effect.fnUntraced(function* (tunnelConfig: string | null) {
243+
const managedTunnelConfigured = tunnelConfig !== null;
233244
const connector = managedTunnelConfigured ? yield* runtime.inspectConnector : null;
234245
const endpoint =
235246
connector?.hostname && connector.readyConnections !== 0
@@ -259,12 +270,13 @@ const make = Effect.gen(function* () {
259270

260271
const check = Effect.gen(function* () {
261272
const order = yield* Ref.updateAndGet(checkCounter, (count) => count + 1);
262-
const facts = yield* gatherFacts;
273+
const tunnelConfig = yield* readTunnelConfig;
274+
const facts = yield* gatherFacts(tunnelConfig);
263275
const result = classifyReachability(facts, DateTime.formatIso(yield* DateTime.now));
264276
const previous = yield* Ref.modify(latestRef, (current) =>
265277
Option.isSome(current) && current.value.order > order
266278
? [null, current]
267-
: [current, Option.some({ order, result })],
279+
: [current, Option.some({ order, result, tunnelConfig })],
268280
);
269281
if (previous === null) return result;
270282
const changed = Option.match(previous, {
@@ -306,7 +318,13 @@ const make = Effect.gen(function* () {
306318

307319
return ConnectReachability.of({
308320
check,
309-
latest: Ref.get(latestRef).pipe(Effect.map(Option.map(({ result }) => result))),
321+
latest: Effect.gen(function* () {
322+
const latest = yield* Ref.get(latestRef);
323+
const tunnelConfig = yield* readTunnelConfig;
324+
return Option.flatMap(latest, (entry) =>
325+
entry.tunnelConfig === tunnelConfig ? Option.some(entry.result) : Option.none(),
326+
);
327+
}),
310328
});
311329
});
312330

‎docs/user/outside-agents.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ same device can reach it.
2222
For the machine you are on, open the menu next to its name at the top of
2323
**Settings → Connections** instead. Once T3 Connect has checked that other
2424
devices can reach it, **Copy MCP URL** there gives the T3 Connect address its
25-
tunnel is serving. `t3 connect status` prints the same URL. A saved environment
26-
with the same name is a different environment, often an older install whose
27-
tunnel is gone.
25+
tunnel is serving. On a server set up with `t3 connect`, `t3 connect status`
26+
prints the same URL. A saved environment with the same name is a different
27+
environment, often an older install whose tunnel is gone.
2828

2929
- **An agent on your own computers** can use `localhost` on the host, T3
3030
Connect, or any `https` address, such as

0 commit comments

Comments
 (0)