@devries-cameron20 / t3code-1 / commits / f99b202753
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): T3 Connect settings can repair a link that drifted to publish-only (#19389)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 14, 2026 at 04:22 UTC f99b20275359ff7acecdb916a9fa25ac33102b12
5 files changed +89 -8
apps/web/src/cloud/useCloudLinkController.test.tsx
+42 -1
@@ -4,6 +4,7 @@ import {
4 EnvironmentId,
5 type EnvironmentCloudLinkStateResult,
6 } from "@t3tools/contracts";
7 +import type { RelayManagedEndpointProviderKind } from "@t3tools/contracts/relay";
8 import { AsyncResult } from "effect/reactivity";
9 import { act, useLayoutEffect } from "react";
10 import { create, type ReactTestRenderer } from "react-test-renderer";
@@ -13,6 +14,7 @@ const testState = vi.hoisted(() => ({
14 scopes: new Set<string>(),
15 snapshot: null as EnvironmentCloudLinkStateResult | null,
16 renderedSnapshot: null as EnvironmentCloudLinkStateResult | null,
17 + relayProviderKind: null as RelayManagedEndpointProviderKind | null,
18 getToken: vi.fn<() => Promise<string | null>>(),
19 link: vi.fn(),
20 unlink: vi.fn(),
@@ -22,12 +24,26 @@ const testState = vi.hoisted(() => ({
24 toast: vi.fn(),
25 }));
26
27 +vi.mock("@effect/atom-react", () => ({
28 + useAtomValue: () => ({
29 + environments: new Map(
30 + testState.relayProviderKind === null
31 + ? []
32 + : [
33 + [
34 + "primary",
35 + { environment: { endpoint: { providerKind: testState.relayProviderKind } } },
36 + ],
37 + ],
38 + ),
39 + }),
40 +}));
41 vi.mock("@clerk/react", () => ({
42 useAuth: () => ({ getToken: testState.getToken, isSignedIn: true }),
43 }));
44 vi.mock("../components/ui/toast", () => ({ toastManager: { add: testState.toast } }));
45 vi.mock("../state/relay", () => ({
30 - relayEnvironmentDiscovery: { refresh: testState.refreshDiscovery },
46 + relayEnvironmentDiscovery: { refresh: testState.refreshDiscovery, stateValueAtom: {} },
47 }));
48 vi.mock("../state/session", () => ({
49 readEnvironmentScope: (_environmentId: string, scope: string) => testState.scopes.has(scope),
@@ -105,6 +121,7 @@ beforeEach(() => {
121 testState.scopes = new Set([AuthRelayReadScope, AuthRelayWriteScope]);
122 testState.snapshot = linkedState;
123 testState.renderedSnapshot = linkedState;
124 + testState.relayProviderKind = null;
125 testState.getToken.mockReset().mockResolvedValue("clerk-token");
126 for (const command of [
127 testState.link,
@@ -191,6 +208,30 @@ describe("useCloudLinkController", () => {
208 });
209 });
210
211 + it("shows a link the relay holds as publish-only as off and relinks it as managed", async () => {
212 + testState.relayProviderKind = "manual";
213 + await mountController();
214 +
215 + expect(controller?.managedTunnelActive).toBe(false);
216 + expect(controller?.managedTunnelOutOfSync).toBe(true);
217 + expect(await reconcile({ managedTunnel: true, publish: false })).toBe(true);
218 + expect(testState.link).toHaveBeenCalledExactlyOnceWith({
219 + target,
220 + clerkToken: "clerk-token",
221 + mode: "managed",
222 + });
223 + });
224 +
225 + it("keeps a managed link that the relay also holds as managed", async () => {
226 + testState.relayProviderKind = "cloudflare_tunnel";
227 + await mountController();
228 +
229 + expect(controller?.managedTunnelActive).toBe(true);
230 + expect(controller?.managedTunnelOutOfSync).toBe(false);
231 + expect(await reconcile({ managedTunnel: true, publish: false })).toBe(true);
232 + expect(testState.link).not.toHaveBeenCalled();
233 + });
234 +
235 it("stops the preference write if management permission is lost after linking", async () => {
236 await mountController();
237 testState.link.mockImplementationOnce(async () => {
apps/web/src/cloud/useCloudLinkController.ts
+39 -6
@@ -1,6 +1,13 @@
1 +import { useAtomValue } from "@effect/atom-react";
2 import { useAuth } from "@clerk/react";
3 import { findErrorTraceId } from "@t3tools/client-runtime/errors";
3 -import { EnvironmentId, AuthRelayReadScope, AuthRelayWriteScope } from "@t3tools/contracts";
4 +import {
5 + EnvironmentId,
6 + AuthRelayReadScope,
7 + AuthRelayWriteScope,
8 + type EnvironmentCloudLinkStateResult,
9 +} from "@t3tools/contracts";
10 +import type { RelayManagedEndpointProviderKind } from "@t3tools/contracts/relay";
11 import {
12 isAtomCommandInterrupted,
13 settlePromise,
@@ -27,6 +34,24 @@ export interface CloudLinkDesiredState {
34 readonly holdWebhooksWhileOffline?: boolean;
35 }
36
37 +/**
38 + * Whether the link has a working managed tunnel. The local tunnel config can
39 + * outlive a relay link that drifted to publish-only, and clients then get
40 + * `endpoint_provider_not_managed`. So when relay discovery has this
41 + * environment's record, it must also say managed. Older environment servers
42 + * predate managedTunnelActive; for them a link always implies a tunnel.
43 + */
44 +function isManagedTunnelActive(
45 + linkState: EnvironmentCloudLinkStateResult,
46 + relayProviderKind: RelayManagedEndpointProviderKind | undefined,
47 +) {
48 + const localManagedTunnel = linkState.managedTunnelActive ?? linkState.linked;
49 + return (
50 + localManagedTunnel &&
51 + (relayProviderKind === undefined || relayProviderKind === "cloudflare_tunnel")
52 + );
53 +}
54 +
55 /**
56 * Drives the primary environment's T3 Connect link. T3 Connect (managed
57 * tunnel) and agent-activity publishing are independent capabilities backed by
@@ -52,6 +77,11 @@ export function useCloudLinkController() {
77 { reportFailure: false },
78 );
79 const primaryCloudLinkState = usePrimaryCloudLinkState();
80 + const relayEnvironments = useAtomValue(relayEnvironmentDiscovery.stateValueAtom).environments;
81 + const primaryEnvironmentId = primaryCloudLinkState.target?.environmentId;
82 + const relayProviderKind = primaryEnvironmentId
83 + ? relayEnvironments.get(primaryEnvironmentId)?.environment.endpoint.providerKind
84 + : undefined;
85 const [operationError, setOperationError] = useState<string | null>(null);
86
87 const reportUpdateFailure = (cause: unknown) => {
@@ -74,10 +104,12 @@ export function useCloudLinkController() {
104 });
105 };
106
77 - // Older environment servers predate the managedTunnelActive field; for them a
78 - // link always implies a managed tunnel, so fall back to `linked`.
79 - const managedTunnelActive =
80 - primaryCloudLinkState.data?.managedTunnelActive ?? primaryCloudLinkState.data?.linked ?? false;
107 + const linkData = primaryCloudLinkState.data;
108 + const managedTunnelActive = linkData ? isManagedTunnelActive(linkData, relayProviderKind) : false;
109 + // Local state says managed but the relay holds a publish-only link. Turning
110 + // T3 Connect on relinks as managed, which repairs it.
111 + const managedTunnelOutOfSync =
112 + linkData !== null && !managedTunnelActive && isManagedTunnelActive(linkData, undefined);
113 const publishAgentActivity = primaryCloudLinkState.data?.publishAgentActivity ?? false;
114 const holdWebhooksWhileOffline = primaryCloudLinkState.data?.holdWebhooksWhileOffline ?? false;
115 const linked = primaryCloudLinkState.data?.linked ?? false;
@@ -145,7 +177,7 @@ export function useCloudLinkController() {
177 reportUpdateFailure(new Error("Sign in to T3 Connect before enabling this."));
178 return false;
179 }
148 - const currentManagedTunnel = currentLinkState.managedTunnelActive ?? currentLinkState.linked;
180 + const currentManagedTunnel = isManagedTunnelActive(currentLinkState, relayProviderKind);
181 if (!currentLinkState.linked || currentManagedTunnel !== desired.managedTunnel) {
182 const linkResult = await linkPrimaryEnvironment({
183 target,
@@ -194,6 +226,7 @@ export function useCloudLinkController() {
226 linkState: primaryCloudLinkState,
227 linked,
228 managedTunnelActive,
229 + managedTunnelOutOfSync,
230 publishAgentActivity,
231 holdWebhooksWhileOffline,
232 operationError,
apps/web/src/components/settings/ConnectionsSettings.tsx
+4 -1
@@ -1829,6 +1829,7 @@ function ConfiguredCloudLinkRow({ canManageRelay }: { readonly canManageRelay: b
1829 isSignedIn,
1830 linkState: primaryCloudLinkState,
1831 managedTunnelActive,
1832 + managedTunnelOutOfSync,
1833 publishAgentActivity,
1834 holdWebhooksWhileOffline,
1835 operationError,
@@ -1919,7 +1920,9 @@ function ConfiguredCloudLinkRow({ canManageRelay }: { readonly canManageRelay: b
1920 description={
1921 managedTunnelActive
1922 ? "This environment is available to your other devices through T3 Connect."
1922 - : "Make this environment available to your other devices through T3 Connect."
1923 + : managedTunnelOutOfSync
1924 + ? "T3 Connect is out of sync. This environment is linked only to publish agent activity, so other devices cannot connect. Turn T3 Connect on to repair it."
1925 + : "Make this environment available to your other devices through T3 Connect."
1926 }
1927 status={operationError ?? primaryCloudLinkState.error}
1928 control={
packages/client-runtime/src/connection/errors.test.ts
+1
@@ -61,6 +61,7 @@ describe("mapManagedRelayError", () => {
61 expect(mapConnectRefusal("endpoint_provider_not_managed")).toMatchObject({
62 _tag: "ConnectionBlockedError",
63 reason: "permission",
64 + detail: expect.stringContaining("turn T3 Connect on"),
65 });
66 expect(mapConnectRefusal(undefined)).toMatchObject({
67 _tag: "ConnectionBlockedError",
packages/client-runtime/src/relay/errorPresentation.ts
+3
@@ -46,6 +46,9 @@ export function relayProtectedErrorMessage(error: RelayProtectedError): string {
46 if (error.reason === "managed_endpoint_allocation_not_ready") {
47 return "The environment's T3 Connect tunnel is still being set up.";
48 }
49 + if (error.reason === "endpoint_provider_not_managed") {
50 + return "This environment is linked only to publish agent activity, so it cannot accept connections. On that computer, open Settings > Connections and turn T3 Connect on.";
51 + }
52 return error.reason
53 ? `Relay rejected the environment connection request (${error.reason}).`
54 : "Relay rejected the environment connection request.";