@devries-cameron20 / t3code Mirror · read-only / commits / dff0b1159b
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 02:00 UTC

fix(connect): show why relay unlink failed (#25198)

Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

shivam committed Nov 8, 2026 at 13:56 UTC dff0b1159bf7aa3ad5e5598d6e1bb7d6243f51a2
7 files changed +136 -39
apps/web/src/cloud/linkEnvironment.test.ts
+46 -1
@@ -26,7 +26,7 @@ import { ManagedRelay } from "@t3tools/client-runtime/relay";
26 import { layerRemoteHttpClient } from "@t3tools/client-runtime/rpc";
27 import { __resetDesktopPrimaryAuthForTests } from "../environments/primary/desktopAuth";
28
29 -import { readCloudLinkState } from "./linkEnvironment";
29 +import { readCloudLinkState, unlinkEnvironmentFromCloud } from "./linkEnvironment";
30
31 const TARGET = {
32 environmentId: "environment-1",
@@ -239,4 +239,49 @@ describe("web cloud link environment client", () => {
239 }
240 }),
241 );
242 +
243 + it.effect("reports why the relay kept the environment after a local unlink", () =>
244 + Effect.gen(function* () {
245 + const fetchMock = vi.fn((input: RequestInfo | URL) =>
246 + Promise.resolve(
247 + String(input).startsWith("https://relay.example.test/")
248 + ? Response.json(
249 + {
250 + _tag: "RelayInternalError",
251 + code: "internal_error",
252 + reason: "upstream_unavailable",
253 + traceId: "trace-unlink",
254 + },
255 + { status: 500 },
256 + )
257 + : Response.json({ ok: true, endpointRuntimeStatus: null }),
258 + ),
259 + );
260 + vi.stubGlobal("fetch", fetchMock);
261 + vi.stubGlobal("window", {
262 + location: { origin: "t3code://app" },
263 + desktopBridge: {
264 + getLocalEnvironmentBearerToken: vi.fn().mockResolvedValue("desktop-bearer-token"),
265 + } as unknown as DesktopBridge,
266 + });
267 +
268 + const error = yield* withServices(
269 + unlinkEnvironmentFromCloud({
270 + target: { environmentId: EnvironmentId.make(TARGET.environmentId) },
271 + clerkToken: "clerk-token",
272 + }),
273 + ).pipe(Effect.flip);
274 +
275 + expect(fetchMock.mock.calls.map(([input]) => String(input))).toEqual([
276 + "http://127.0.0.1:3000/api/connect/unlink",
277 + "https://relay.example.test/v1/client/environment-links/environment-1",
278 + ]);
279 + expect(error).toMatchObject({
280 + _tag: "CloudEnvironmentLinkError",
281 + message:
282 + "Disconnected locally, but T3 Connect did not confirm it removed this environment: Relay encountered an internal error (upstream_unavailable).",
283 + traceId: "trace-unlink",
284 + });
285 + }),
286 + );
287 });
apps/web/src/cloud/linkEnvironment.ts
+6 -6
@@ -120,8 +120,8 @@ const isEnvironmentCloudApiError = Schema.is(
120 function decodedRelayClientError(message: string) {
121 return (cause: ManagedRelay.ManagedRelayClientError) => {
122 const failed = cause._tag === "ManagedRelayRequestFailedError" ? cause : undefined;
123 - const traceId = failed?.traceId;
124 - const detail = failed ? relayRequestFailureDetail(failed) : null;
123 + const traceId = "traceId" in cause ? cause.traceId : undefined;
124 + const detail = (failed && relayRequestFailureDetail(failed)) ?? cause.message;
125 return new CloudEnvironmentLinkError({
126 message: detail ? `${message}: ${detail}` : message,
127 cause,
@@ -309,10 +309,10 @@ export const unlinkEnvironmentFromCloud: (input: {
309 environmentId: input.target.environmentId,
310 })
311 .pipe(
312 - Effect.catch((cause) =>
313 - Effect.logWarning("Could not revoke cloud environment link after local unlink.", {
314 - cause,
315 - }),
312 + Effect.mapError(
313 + decodedRelayClientError(
314 + "Disconnected locally, but T3 Connect did not confirm it removed this environment",
315 + ),
316 ),
317 );
318 }
apps/web/src/cloud/relayFailureReason.ts new
+25
@@ -0,0 +1,25 @@
1 +import { ManagedRelay, relayRequestFailureDetail } from "@t3tools/client-runtime/relay";
2 +import * as Schema from "effect/Schema";
3 +
4 +const isRequestFailed = Schema.is(ManagedRelay.ManagedRelayRequestFailedError);
5 +const isRequestTimeout = Schema.is(ManagedRelay.ManagedRelayRequestTimeoutError);
6 +
7 +/** One short line for an error toast saying why a T3 Connect request failed. */
8 +export function relayFailureReason(cause: unknown): string | null {
9 + for (let current = cause, depth = 0; current instanceof Error && depth < 4; depth += 1) {
10 + if (isRequestTimeout(current)) return "T3 Connect timed out. Try again.";
11 + if (isRequestFailed(current)) {
12 + if (current.transportFailed) return "Could not reach T3 Connect. Check your network.";
13 + if (current.relayError) {
14 + return current.relayError._tag === "RelayInternalError"
15 + ? "T3 Connect is unavailable. Try again."
16 + : relayRequestFailureDetail(current);
17 + }
18 + if (current.status === 504) return "T3 Connect timed out. Try again.";
19 + if ((current.status ?? 0) >= 500) return "T3 Connect is unavailable. Try again.";
20 + return relayRequestFailureDetail(current);
21 + }
22 + current = current.cause;
23 + }
24 + return null;
25 +}
apps/web/src/cloud/useCloudLinkController.ts
+32 -15
@@ -18,6 +18,8 @@ import * as Schema from "effect/Schema";
18 import { useState } from "react";
19
20 import { toastManager } from "../components/ui/toast";
21 +import { stackedThreadToast } from "../components/ui/toastHelpers";
22 +import { relayFailureReason } from "./relayFailureReason";
23 import { relayEnvironmentDiscovery } from "../state/relay";
24 import { readEnvironmentScope } from "../state/session";
25 import { useAtomCommand } from "../state/use-atom-command";
@@ -51,6 +53,7 @@ function isManagedTunnelActive(
53 }
54
55 const isManagedRelayRequestFailedError = Schema.is(ManagedRelay.ManagedRelayRequestFailedError);
56 +const isManagedRelayClientError = Schema.is(ManagedRelay.ManagedRelayClientError);
57
58 /** The account's tunnel cap when the relay refused a new tunnel for being at it. */
59 function tunnelLimitFromLinkFailure(cause: unknown): number | null {
@@ -84,24 +87,28 @@ export function useCloudLinkController(environmentId: EnvironmentId | null) {
87 const [operationError, setOperationError] = useState<string | null>(null);
88 const [tunnelLimit, setTunnelLimit] = useState<number | null>(null);
89
87 - const reportUpdateFailure = (cause: unknown) => {
90 + const reportUpdateFailure = (cause: unknown, reason?: string) => {
91 const message = cause instanceof Error ? cause.message : "Could not update T3 Connect access.";
92 const traceId = findErrorTraceId(cause);
93 console.error("[t3-connect] Could not update T3 Connect", { message, traceId, cause });
94 setOperationError(traceId ? `${message} Trace ID: ${traceId}` : message);
92 - toastManager.add({
93 - type: "error",
94 - title: "Could not update T3 Connect",
95 - description: message,
96 - data: traceId
97 - ? {
98 - secondaryActionProps: {
99 - children: "Copy trace ID",
100 - onClick: () => void navigator.clipboard?.writeText(traceId),
101 - },
102 - }
103 - : undefined,
104 - });
95 + toastManager.add(
96 + stackedThreadToast({
97 + type: "error",
98 + title: "Could not update T3 Connect",
99 + description: reason ?? relayFailureReason(cause) ?? message,
100 + ...(traceId
101 + ? {
102 + actionProps: {
103 + children: "Copy trace ID",
104 + onClick: () => void navigator.clipboard?.writeText(traceId),
105 + },
106 + actionVariant: "outline",
107 + }
108 + : {}),
109 + data: { hideCopyButton: true },
110 + }),
111 + );
112 };
113
114 const linkData = cloudLinkState.data;
@@ -157,9 +164,19 @@ export function useCloudLinkController(environmentId: EnvironmentId | null) {
164 refreshManagedTunnelUsage();
165 if (unlinkResult._tag === "Failure") {
166 if (!isAtomCommandInterrupted(unlinkResult)) {
160 - reportUpdateFailure(squashAtomCommandFailure(unlinkResult));
167 + const cause = squashAtomCommandFailure(unlinkResult);
168 + // Only the relay delete fails with a relay error, and it runs after the local unlink.
169 + const relayKeptLink =
170 + isCloudEnvironmentLinkError(cause) && isManagedRelayClientError(cause.cause);
171 + reportUpdateFailure(
172 + cause,
173 + relayKeptLink
174 + ? "Disconnected here, but T3 Connect still lists it. Remove it in Account."
175 + : undefined,
176 + );
177 }
178 cloudLinkState.refresh();
179 + await refreshRelayEnvironments();
180 return false;
181 }
182 } else {
apps/web/src/components/clerk/T3ConnectUserProfilePage.tsx
+22 -14
@@ -13,6 +13,7 @@ import {
13 useManagedRelayEnvironments,
14 useManagedTunnelUsage,
15 } from "../../cloud/managedRelayState";
16 +import { relayFailureReason } from "../../cloud/relayFailureReason";
17 import { relayEnvironmentDiscovery } from "../../state/relay";
18 import { useAtomCommand } from "../../state/use-atom-command";
19 import { T3CHAT_SUBSCRIPTION_URL, TunnelUsageMeter, tunnelUpgradeCopy } from "../cloud/TunnelUsage";
@@ -20,6 +21,7 @@ import { Button, InlineButton } from "../ui/button";
21 import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
22 import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "../ui/empty";
23 import { toastManager } from "../ui/toast";
24 +import { stackedThreadToast } from "../ui/toastHelpers";
25 import {
26 ClerkUserProfilePage,
27 ClerkUserProfileRefreshButton,
@@ -192,9 +194,9 @@ export function T3ConnectUserProfilePage() {
194 });
195 mutationPendingRef.current = false;
196 setDeregisteringEnvironmentId(null);
197 + setConfirmingEnvironmentId(null);
198
199 if (result._tag === "Success") {
197 - setConfirmingEnvironmentId(null);
200 setRemovedEnvironments((current) => {
201 const linkedAtById = new Map(current.accountId === accountId ? current.linkedAtById : []);
202 linkedAtById.set(environment.environmentId, environment.linkedAt);
@@ -211,6 +213,8 @@ export function T3ConnectUserProfilePage() {
213 }
214 if (isAtomCommandInterrupted(result)) return;
215
216 + // The relay may have removed it before failing.
217 + environmentsState.refresh();
218 const cause = squashAtomCommandFailure(result);
219 const message = cause instanceof Error ? cause.message : "Could not deregister the server.";
220 const traceId = findErrorTraceId(cause);
@@ -220,19 +224,23 @@ export function T3ConnectUserProfilePage() {
224 traceId,
225 cause,
226 });
223 - toastManager.add({
224 - type: "error",
225 - title: "Could not deregister server",
226 - description: message,
227 - data: traceId
228 - ? {
229 - secondaryActionProps: {
230 - children: "Copy trace ID",
231 - onClick: () => void navigator.clipboard?.writeText(traceId),
232 - },
233 - }
234 - : undefined,
235 - });
227 + toastManager.add(
228 + stackedThreadToast({
229 + type: "error",
230 + title: "Could not deregister server",
231 + description: relayFailureReason(cause) ?? message,
232 + ...(traceId
233 + ? {
234 + actionProps: {
235 + children: "Copy trace ID",
236 + onClick: () => void navigator.clipboard?.writeText(traceId),
237 + },
238 + actionVariant: "outline",
239 + }
240 + : {}),
241 + data: { hideCopyButton: true },
242 + }),
243 + );
244 };
245
246 const removedEnvironmentLinkedAt =
apps/web/src/components/clerk/clerkAppearance.ts
+1 -1
@@ -29,7 +29,7 @@ export const clerkAppearance = {
29 colorRing: "var(--ring)",
30 },
31 elements: {
32 - modalBackdrop: "dialog-backdrop",
32 + modalBackdrop: "dialog-backdrop z-50",
33 cardBox: "dialog-glass rounded-2xl border",
34 card: "border-0 bg-transparent shadow-none",
35 footer: "bg-transparent bg-none",
packages/client-runtime/src/relay/managedRelay.ts
+4 -2
@@ -47,6 +47,7 @@ import * as HttpClientError from "effect/http/HttpClientError";
47 import type * as HttpMethod from "effect/http/HttpMethod";
48 import * as HttpApiClient from "effect/http-api/HttpApiClient";
49 import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
50 +import { relayRequestFailureDetail } from "./errorPresentation.ts";
51
52 export interface ManagedRelayDpopProofInput {
53 readonly method: HttpMethod.HttpMethod;
@@ -159,8 +160,9 @@ export class ManagedRelayRequestFailedError extends Schema.TaggedError<ManagedRe
160 },
161 ) {
162 override get message(): string {
162 - const message = `Could not ${this.action}.`;
163 - return this.transportFailed ? `${message} ${NETWORK_BLOCKING_HINT}` : message;
163 + if (this.transportFailed) return `Could not ${this.action}. ${NETWORK_BLOCKING_HINT}`;
164 + const detail = relayRequestFailureDetail(this);
165 + return detail ? `Could not ${this.action}: ${detail}` : `Could not ${this.action}.`;
166 }
167 }
168