@devries-cameron20 / t3code Mirror · read-only / commits / 314a11498e
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 21, 06:00 UTC

fix(web): preserve Connect environments on unreachable auth at launch (#24642)

Co-authored-by: Otavio Salvador <otavio@ossystems.com.br>

Julius Marminge committed Nov 19, 2026 at 12:12 UTC 314a11498e1ac1c29a553bef7fe6032146d51287
2 files changed +55 -5
packages/client-runtime/src/relay/cloudAccountService.test.ts
+29 -2
@@ -6,7 +6,7 @@ import {
6 planCloudAccountStep,
7 } from "./cloudAccountService.ts";
8
9 -type Emit = (user: { readonly id: string } | null | undefined) => void;
9 +type Emit = (user: { readonly id: string } | null | undefined, clientId?: string) => void;
10
11 function makeSource() {
12 let listener: Parameters<CloudAccountSource["addListener"]>[0] | null = null;
@@ -18,7 +18,8 @@ function makeSource() {
18 };
19 },
20 };
21 - const emit: Emit = (user) => listener?.({ user });
21 + const emit: Emit = (user, clientId = "client_1") =>
22 + listener?.({ user, client: { id: clientId } });
23 return { source, emit, isSubscribed: () => listener !== null };
24 }
25
@@ -154,6 +155,32 @@ describe("createCloudAccountService", () => {
155 expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]);
156 });
157
158 + it("waits for Clerk's API to confirm a signed-out launch before cleaning up", async () => {
159 + const { service, log, nextActivation, nextCleanUp } = makeService();
160 + const { source, emit } = makeSource();
161 + service.attach(source);
162 +
163 + // Offline: an id-less client, or the placeholder built from the session cookie.
164 + emit(null, "");
165 + emit(null, "client_init");
166 + expect(service.getState()).toEqual({ _tag: "Unobserved" });
167 + expect(log).toEqual([]);
168 +
169 + const cleanUp = nextCleanUp();
170 + emit(null);
171 + await cleanUp;
172 + expect(log).toEqual(["deactivate", "cleanUp:null:false"]);
173 +
174 + const activation = nextActivation();
175 + emit({ id: "a" });
176 + await activation;
177 + log.length = 0;
178 + const signOut = nextCleanUp();
179 + emit(null, "client_init");
180 + await signOut;
181 + expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]);
182 + });
183 +
184 it("never activates an account that signed out while it was activating", async () => {
185 const { service, log, nextCleanUp, gate } = makeService();
186 const { source, emit } = makeSource();
packages/client-runtime/src/relay/cloudAccountService.ts
+26 -3
@@ -7,10 +7,22 @@ import { reportAtomCommandResult, settlePromise } from "../state/runtime.ts";
7 */
8 export interface CloudAccountSource {
9 readonly addListener: (
10 - listener: (resources: { readonly user?: { readonly id: string } | null | undefined }) => void,
10 + listener: (resources: {
11 + readonly client?: { readonly id?: string | null | undefined } | null | undefined;
12 + readonly user?: { readonly id: string } | null | undefined;
13 + }) => void,
14 ) => () => void;
15 }
16
17 +/**
18 + * Clerk's load status cannot prove its API answered: offline it emits an
19 + * id-less client, and a failed launch fetch emits a "client_init" placeholder
20 + * built from the session cookie. Only a real /v1/client response has an id.
21 + */
22 +const isClientFromClerkApi = (
23 + client: { readonly id?: string | null | undefined } | null | undefined,
24 +) => Boolean(client?.id) && client?.id !== "client_init";
25 +
26 interface CloudAccountSignIn {
27 readonly _tag: "SignIn";
28 readonly accountId: string;
@@ -32,7 +44,8 @@ export type CloudAccountStep =
44 /**
45 * `previous` is undefined until Clerk's first emission. A first emission that
46 * is signed out still cleans up, so data left by an account that signed out
35 - * while the app was closed does not linger.
47 + * while the app was closed does not linger. The service only plans that step
48 + * once Clerk's API has confirmed the sign-out.
49 */
50 export function planCloudAccountStep(
51 previous: string | null | undefined,
@@ -200,7 +213,17 @@ export function createCloudAccountService<S extends CloudAccountSource>(
213 // Clerk reports `user: undefined` until it loads; pending sessions
214 // still carry a user and count as signed in.
215 if (resources.user === undefined) return;
203 - observe(source, resources.user?.id ?? null);
216 + const next = resources.user?.id ?? null;
217 + // An unreachable Clerk API at launch looks signed out; stay unobserved
218 + // until Clerk confirms it, so saved environments survive offline starts.
219 + if (
220 + next === null &&
221 + state._tag === "Unobserved" &&
222 + !isClientFromClerkApi(resources.client)
223 + ) {
224 + return;
225 + }
226 + observe(source, next);
227 }),
228 };
229 attached = current;