TypeScript
refactor(client): cloud account transitions run in one service (#24974)
Julius Marminge60169698879d
diff --git a/apps/mobile/src/features/cloud/managedRelayState.ts b/apps/mobile/src/features/cloud/managedRelayState.tsindex 75b2658c51c..507b84629b0 100644--- a/apps/mobile/src/features/cloud/managedRelayState.ts+++ b/apps/mobile/src/features/cloud/managedRelayState.ts@@ -12,7 +12,7 @@ import {import type { EnvironmentId } from "@t3tools/contracts";import type { RelayClientEnvironmentRecord } from "@t3tools/contracts/relay";import { AsyncResult, Atom } from "effect/reactivity";-import { useCallback, useEffect } from "react";+import { useCallback } from "react";import * as Runtime from "../../lib/runtime";import { appAtomRegistry } from "../../state/atom-registry";@@ -53,14 +53,6 @@ export function useManagedRelayEnvironments() {: EMPTY_ENVIRONMENTS_ATOM;const result = useAtomValue(atom);const snapshot = readManagedRelaySnapshotState(result);- useEffect(() => {- if (snapshot.error) {- console.error("[t3-cloud] Relay environment listing failed", {- message: snapshot.error,- traceId: snapshot.errorTraceId,- });- }- }, [snapshot.error, snapshot.errorTraceId]);const refresh = useCallback(() => {if (accountId) {managedRelayQueryManager.refreshEnvironments(appAtomRegistry, accountId);diff --git a/apps/web/src/cloud/managedAuth.test.ts b/apps/web/src/cloud/managedAuth.test.tsindex 27f8a218790..095ad1f7e6d 100644--- a/apps/web/src/cloud/managedAuth.test.ts+++ b/apps/web/src/cloud/managedAuth.test.ts@@ -9,7 +9,7 @@ import {} from "./managedAuth";vi.mock("@clerk/react", () => ({- useAuth: vi.fn(),+ useClerk: vi.fn(),}));vi.mock("../lib/runtime", () => ({diff --git a/apps/web/src/cloud/managedAuth.tsx b/apps/web/src/cloud/managedAuth.tsxindex f9bf1c966f4..8c3c866f957 100644--- a/apps/web/src/cloud/managedAuth.tsx+++ b/apps/web/src/cloud/managedAuth.tsx@@ -1,17 +1,20 @@-import { useAuth } from "@clerk/react";-import { ManagedRelay, setManagedRelaySession } from "@t3tools/client-runtime/relay";+import { useClerk } from "@clerk/react";+import {+ createCloudAccountService,+ ManagedRelay,+ setManagedRelaySession,+} from "@t3tools/client-runtime/relay";import {reportAtomCommandResult,+ runAtomCommand,settleAsyncResult,- settlePromise,} from "@t3tools/client-runtime/state/runtime";import * as Effect from "effect/Effect";-import { useEffect, useRef, type ReactNode } from "react";+import { useEffect, type ReactNode } from "react";import { environmentCatalog } from "../connection/catalog";import { runtime } from "../lib/runtime";import { appAtomRegistry } from "../rpc/atomRegistry";-import { useAtomCommand } from "../state/use-atom-command";import { resolveRelayClerkTokenOptions } from "./publicConfig";export function deactivateManagedRelayAuthentication(): void {@@ -28,81 +31,40 @@ export function activateManagedRelayAuthentication(});}-export function ManagedRelayAuthProvider({ children }: { readonly children: ReactNode }) {- const { getToken, isLoaded, isSignedIn, userId } = useAuth({- treatPendingAsSignedOut: false,- });- const removeRelayEnvironments = useAtomCommand(environmentCatalog.removeRelayEnvironments, {- reportFailure: false,- reportDefect: false,- });- const observedAccountRef = useRef<string | null | undefined>(undefined);- const accountTransitionRef = useRef<Promise<void> | null>(null);-- useEffect(() => {- if (!isLoaded) {- return;- }-- let cancelled = false;- const previousAccount = observedAccountRef.current;- const nextAccount = isSignedIn && userId ? userId : null;- observedAccountRef.current = nextAccount;+type ClerkInstance = ReturnType<typeof useClerk>;- const queueAccountCleanup = () => {- const previousTransition = accountTransitionRef.current ?? Promise.resolve();- accountTransitionRef.current = previousTransition.then(async () => {- const results = await Promise.all([- removeRelayEnvironments(),- settleAsyncResult(() =>- runtime.runPromiseExit(- ManagedRelay.ManagedRelayClient.pipe(- Effect.flatMap((client) => client.resetTokenCache),- ),- ),- ),- ]);- for (const result of results) {- reportAtomCommandResult(result, { label: "cloud account cleanup" });- }- });- return accountTransitionRef.current;- };+function clerkTokenReader(clerk: ClerkInstance): () => Promise<string | null> {+ return async () =>+ clerk.session ? await clerk.session.getToken(resolveRelayClerkTokenOptions()) : null;+}- if (!isSignedIn || !userId) {- deactivateManagedRelayAuthentication();- if (previousAccount !== null) {- void queueAccountCleanup();- }- } else {- const tokenProvider = () => getToken(resolveRelayClerkTokenOptions());- const activateSession = () => {- if (!cancelled) {- activateManagedRelayAuthentication(userId, tokenProvider);- }- };- const activateAfterTransition = (transition: Promise<void>) => {- void (async () => {- const result = await settlePromise(async () => {- await transition;- activateSession();- });- reportAtomCommandResult(result, { label: "cloud account activation" });- })();- };- if (previousAccount !== undefined && previousAccount !== null && previousAccount !== userId) {- deactivateManagedRelayAuthentication();- activateAfterTransition(queueAccountCleanup());- } else {- activateAfterTransition(accountTransitionRef.current ?? Promise.resolve());- }+const managedRelayAccountService = createCloudAccountService<ClerkInstance>({+ deactivate: deactivateManagedRelayAuthentication,+ detach: deactivateManagedRelayAuthentication,+ cleanUp: async () => {+ const results = await Promise.all([+ runAtomCommand(appAtomRegistry, environmentCatalog.removeRelayEnvironments, undefined, {+ reportFailure: false,+ reportDefect: false,+ }),+ settleAsyncResult(() =>+ runtime.runPromiseExit(+ ManagedRelay.ManagedRelayClient.pipe(Effect.flatMap((client) => client.resetTokenCache)),+ ),+ ),+ ]);+ for (const result of results) {+ reportAtomCommandResult(result, { label: "cloud account cleanup" });}- return () => {- cancelled = true;- };- }, [getToken, isLoaded, isSignedIn, removeRelayEnvironments, userId]);-- useEffect(() => () => deactivateManagedRelayAuthentication(), []);+ },+ activate: ({ source, accountId }) =>+ activateManagedRelayAuthentication(accountId, clerkTokenReader(source)),+});+export function ManagedRelayAuthProvider({ children }: { readonly children: ReactNode }) {+ // ClerkProvider creates the Clerk instance and owns its lifetime, so the+ // account service observes it for as long as this provider is mounted.+ const clerk = useClerk();+ useEffect(() => managedRelayAccountService.attach(clerk), [clerk]);return children;}diff --git a/apps/web/src/cloud/managedRelayState.ts b/apps/web/src/cloud/managedRelayState.tsindex ecff5d2ae8f..16fec79749b 100644--- a/apps/web/src/cloud/managedRelayState.ts+++ b/apps/web/src/cloud/managedRelayState.ts@@ -19,7 +19,6 @@ import * as Context from "effect/Context";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import { AsyncResult, Atom } from "effect/reactivity";-import { useEffect } from "react";import { runtime } from "../lib/runtime";import { appAtomRegistry } from "../rpc/atomRegistry";@@ -67,14 +66,6 @@ export function useManagedRelayEnvironments() {: EMPTY_ENVIRONMENTS_ATOM;const result = useAtomValue(atom);const snapshot = readManagedRelaySnapshotState(result);- useEffect(() => {- if (snapshot.error) {- console.error("[t3-cloud] Relay environment listing failed", {- message: snapshot.error,- traceId: snapshot.errorTraceId,- });- }- }, [snapshot.error, snapshot.errorTraceId]);const refresh = () => {if (accountId) {managedRelayQueryManager.refreshEnvironments(appAtomRegistry, accountId);@@ -94,14 +85,6 @@ export function useManagedRelayDevices() {const atom = accountId ? managedRelayQueryManager.devicesAtom(accountId) : EMPTY_DEVICES_ATOM;const result = useAtomValue(atom);const snapshot = readManagedRelaySnapshotState(result);- useEffect(() => {- if (snapshot.error) {- console.error("[t3-cloud] Relay device listing failed", {- message: snapshot.error,- traceId: snapshot.errorTraceId,- });- }- }, [snapshot.error, snapshot.errorTraceId]);const refresh = () => {if (accountId) {managedRelayQueryManager.refreshDevices(appAtomRegistry, accountId);diff --git a/packages/client-runtime/src/relay/cloudAccountService.test.ts b/packages/client-runtime/src/relay/cloudAccountService.test.tsnew file mode 100644index 00000000000..f66d8a9423f--- /dev/null+++ b/packages/client-runtime/src/relay/cloudAccountService.test.ts@@ -0,0 +1,244 @@+import { describe, expect, it } from "vite-plus/test";++import {+ type CloudAccountSource,+ createCloudAccountService,+ planCloudAccountStep,+} from "./cloudAccountService.ts";++type Emit = (user: { readonly id: string } | null | undefined) => void;++function makeSource() {+ let listener: Parameters<CloudAccountSource["addListener"]>[0] | null = null;+ const source: CloudAccountSource = {+ addListener: (next) => {+ listener = next;+ return () => {+ listener = null;+ };+ },+ };+ const emit: Emit = (user) => listener?.({ user });+ return { source, emit, isSubscribed: () => listener !== null };+}++function makeService() {+ const log: string[] = [];+ const activated: Array<PromiseWithResolvers<void>> = [];+ const cleanedUp: Array<PromiseWithResolvers<void>> = [];+ const gates = new Map<+ string,+ { readonly started: PromiseWithResolvers<void>; readonly release: PromiseWithResolvers<void> }+ >();+ const service = createCloudAccountService<CloudAccountSource>({+ deactivate: () => log.push("deactivate"),+ detach: () => log.push("detach"),+ onSignOutTransition: () => log.push("signOutTransition"),+ cleanUp: async ({ accountId, wasActive }) => {+ log.push(`cleanUp:${accountId}:${wasActive}`);+ cleanedUp.shift()?.resolve();+ },+ prepare: async ({ accountId }) => {+ log.push(`prepare:${accountId}`);+ const gate = gates.get(accountId);+ gate?.started.resolve();+ await gate?.release.promise;+ },+ activate: ({ accountId, isAccountTransition }) => {+ log.push(`activate:${accountId}:${isAccountTransition}`);+ activated.shift()?.resolve();+ },+ });+ const nextActivation = () => {+ const deferred = Promise.withResolvers<void>();+ activated.push(deferred);+ return deferred.promise;+ };+ const nextCleanUp = () => {+ const deferred = Promise.withResolvers<void>();+ cleanedUp.push(deferred);+ return deferred.promise;+ };+ const gate = (accountId: string) => {+ const deferred = {+ started: Promise.withResolvers<void>(),+ release: Promise.withResolvers<void>(),+ };+ gates.set(accountId, deferred);+ return deferred;+ };+ return { service, log, nextActivation, nextCleanUp, gate };+}++describe("planCloudAccountStep", () => {+ it("treats the first observation as a non-transition", () => {+ expect(planCloudAccountStep(undefined, "a")).toEqual({+ _tag: "SignIn",+ accountId: "a",+ switchFromAccountId: null,+ isAccountTransition: false,+ });+ expect(planCloudAccountStep(undefined, null)).toEqual({+ _tag: "SignOut",+ previousAccountId: null,+ isAccountTransition: false,+ });+ });++ it("ignores repeats and switches from the previous account", () => {+ expect(planCloudAccountStep("a", "a")).toEqual({ _tag: "None" });+ expect(planCloudAccountStep(null, null)).toEqual({ _tag: "None" });+ expect(planCloudAccountStep("a", "b")).toEqual({+ _tag: "SignIn",+ accountId: "b",+ switchFromAccountId: "a",+ isAccountTransition: true,+ });+ });+});++describe("createCloudAccountService", () => {+ it("activates once per account however often Clerk emits", async () => {+ const { service, log, nextActivation } = makeService();+ const { source, emit } = makeSource();+ service.attach(source);++ emit(undefined);+ expect(service.getState()).toEqual({ _tag: "Unobserved" });+ const activation = nextActivation();+ emit({ id: "a" });+ emit({ id: "a" });+ expect(service.getState()).toEqual({ _tag: "Activating", accountId: "a" });+ await activation;++ expect(service.getState()).toEqual({ _tag: "Active", accountId: "a" });+ expect(log).toEqual(["prepare:a", "activate:a:false"]);+ });++ it("drops credentials synchronously and cleans up before activating the next account", async () => {+ const { service, log, nextActivation } = makeService();+ const { source, emit } = makeSource();+ service.attach(source);+ const first = nextActivation();+ emit({ id: "a" });+ await first;+ log.length = 0;++ const second = nextActivation();+ emit({ id: "b" });+ expect(log).toEqual(["deactivate"]);+ await second;++ expect(log).toEqual(["deactivate", "cleanUp:a:true", "prepare:b", "activate:b:true"]);+ });++ it("cleans up on sign-out, including a signed-out first observation", async () => {+ const { service, log, nextActivation, nextCleanUp } = makeService();+ const { source, emit } = makeSource();+ service.attach(source);++ const initialCleanUp = nextCleanUp();+ emit(null);+ await initialCleanUp;+ expect(log).toEqual(["deactivate", "cleanUp:null:false"]);++ const activation = nextActivation();+ emit({ id: "a" });+ await activation;+ log.length = 0;++ const cleanUp = nextCleanUp();+ emit(null);+ await cleanUp;+ expect(service.getState()).toEqual({ _tag: "SignedOut" });+ expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]);+ });++ it("never activates an account that signed out while it was activating", async () => {+ const { service, log, nextCleanUp, gate } = makeService();+ const { source, emit } = makeSource();+ service.attach(source);+ const prepared = gate("a");+ emit({ id: "a" });+ await prepared.started.promise;+ const cleanUp = nextCleanUp();+ emit(null);+ prepared.release.resolve();+ await cleanUp;++ expect(service.getState()).toEqual({ _tag: "SignedOut" });+ expect(log).toEqual(["prepare:a", "signOutTransition", "deactivate", "cleanUp:a:false"]);+ });++ it("retries a rejected activation on the next emission for the same account", async () => {+ const log: string[] = [];+ const failed = Promise.withResolvers<void>();+ const activated = Promise.withResolvers<void>();+ let attempts = 0;+ const service = createCloudAccountService<CloudAccountSource>({+ deactivate: () => log.push("deactivate"),+ detach: () => log.push("detach"),+ cleanUp: async ({ accountId, wasActive }) => {+ log.push(`cleanUp:${accountId}:${wasActive}`);+ },+ prepare: async ({ accountId }) => {+ attempts += 1;+ log.push(`prepare:${accountId}`);+ if (attempts === 1) {+ // Queued before the rejection, so the service's catch runs before+ // the test's `await failed.promise` continuation.+ queueMicrotask(failed.resolve);+ throw new Error("restore failed");+ }+ },+ activate: ({ accountId, isAccountTransition }) => {+ log.push(`activate:${accountId}:${isAccountTransition}`);+ activated.resolve();+ },+ });+ const { source, emit } = makeSource();+ service.attach(source);+ emit(null);+ emit({ id: "a" });+ await failed.promise;+ expect(service.getState()).toMatchObject({ _tag: "Failed", accountId: "a" });++ emit({ id: "a" });+ await activated.promise;++ expect(service.getState()).toEqual({ _tag: "Active", accountId: "a" });+ expect(log).toEqual([+ "deactivate",+ "cleanUp:null:false",+ "prepare:a",+ "prepare:a",+ "activate:a:true",+ ]);+ });++ it("detaches from a replaced Clerk instance", () => {+ const { service, log } = makeService();+ const first = makeSource();+ const second = makeSource();+ service.attach(first.source);+ service.attach(second.source);++ expect(first.isSubscribed()).toBe(false);+ expect(second.isSubscribed()).toBe(true);+ expect(log).toEqual(["detach"]);+ });++ it("releases credentials and stops observing when detached", async () => {+ const { service, log, nextActivation } = makeService();+ const { source, emit, isSubscribed } = makeSource();+ const detach = service.attach(source);+ const activation = nextActivation();+ emit({ id: "a" });+ await activation;++ detach();+ expect(isSubscribed()).toBe(false);+ expect(service.getState()).toEqual({ _tag: "Unobserved" });+ expect(log).toEqual(["prepare:a", "activate:a:false", "detach"]);+ });+});diff --git a/packages/client-runtime/src/relay/cloudAccountService.ts b/packages/client-runtime/src/relay/cloudAccountService.tsnew file mode 100644index 00000000000..6592fba687b--- /dev/null+++ b/packages/client-runtime/src/relay/cloudAccountService.ts@@ -0,0 +1,213 @@+import { reportAtomCommandResult, settlePromise } from "../state/runtime.ts";++/**+ * The slice of a Clerk instance the account service needs. Clerk emits on+ * every resource change (token refreshes, profile edits), so the service+ * dedupes emissions down to account changes.+ */+export interface CloudAccountSource {+ readonly addListener: (+ listener: (resources: { readonly user?: { readonly id: string } | null | undefined }) => void,+ ) => () => void;+}++interface CloudAccountSignIn {+ readonly _tag: "SignIn";+ readonly accountId: string;+ /** Signed-in account being replaced; its data is removed before activation. */+ readonly switchFromAccountId: string | null;+ readonly isAccountTransition: boolean;+}++export type CloudAccountStep =+ | { readonly _tag: "None" }+ | {+ readonly _tag: "SignOut";+ /** Account whose data is removed; null removes data of any account. */+ readonly previousAccountId: string | null;+ readonly isAccountTransition: boolean;+ }+ | CloudAccountSignIn;++/**+ * `previous` is undefined until Clerk's first emission. A first emission that+ * is signed out still cleans up, so data left by an account that signed out+ * while the app was closed does not linger.+ */+export function planCloudAccountStep(+ previous: string | null | undefined,+ next: string | null,+): CloudAccountStep {+ if (previous === next) return { _tag: "None" };+ const isAccountTransition = previous !== undefined;+ if (next === null) {+ return { _tag: "SignOut", previousAccountId: previous ?? null, isAccountTransition };+ }+ return {+ _tag: "SignIn",+ accountId: next,+ switchFromAccountId: previous ?? null,+ isAccountTransition,+ };+}++export interface CloudAccountHandlers<S extends CloudAccountSource> {+ /** Synchronously drops every credential so nothing reads the old account during cleanup. */+ readonly deactivate: () => void;+ readonly cleanUp: (input: {+ readonly source: S;+ readonly accountId: string | null;+ /** Whether `accountId` finished activating, so per-account registrations exist. */+ readonly wasActive: boolean;+ }) => Promise<void>;+ /** Async work that must finish before the session is visible (draft restore etc.). */+ readonly prepare?: (input: {+ readonly source: S;+ readonly accountId: string;+ readonly isCurrent: () => boolean;+ }) => Promise<void>;+ readonly activate: (input: {+ readonly source: S;+ readonly accountId: string;+ readonly isAccountTransition: boolean;+ }) => void;+ readonly onSignOutTransition?: () => void;+ /** Releases credentials when the service stops observing a Clerk instance. */+ readonly detach: () => void;+}++export type CloudAccountState =+ | { readonly _tag: "Unobserved" }+ | { readonly _tag: "SignedOut" }+ | { readonly _tag: "Activating"; readonly accountId: string }+ | { readonly _tag: "Active"; readonly accountId: string }+ /**+ * Activation rejected. The next emission for the same account retries the+ * original step, so a transient failure recovers on Clerk's next resource+ * change (token refreshes emit regularly) without re-requesting onboarding.+ */+ | {+ readonly _tag: "Failed";+ readonly accountId: string;+ readonly step: CloudAccountSignIn;+ readonly wasActive: boolean;+ };++export interface CloudAccountService<S extends CloudAccountSource> {+ /**+ * Observes `source` until the returned function runs. Attaching while another+ * source is attached detaches that one first.+ */+ readonly attach: (source: S) => () => void;+ readonly getState: () => CloudAccountState;+}++export function createCloudAccountService<S extends CloudAccountSource>(+ handlers: CloudAccountHandlers<S>,+): CloudAccountService<S> {+ let attached: { readonly source: S; readonly unsubscribe: () => void } | null = null;+ let state: CloudAccountState = { _tag: "Unobserved" };+ // Bumped on every account change so a superseded activation never applies.+ let generation = 0;+ let tail: Promise<void> = Promise.resolve();++ const enqueue = (label: string, operation: () => Promise<void>) => {+ const run = tail.then(operation, operation);+ tail = run.then(+ () => undefined,+ () => undefined,+ );+ void settlePromise(() => run).then((result) => reportAtomCommandResult(result, { label }));+ };++ const observedAccount = (): string | null | undefined => {+ switch (state._tag) {+ case "Unobserved":+ return undefined;+ case "SignedOut":+ return null;+ default:+ return state.accountId;+ }+ };++ const activate = (source: S, step: CloudAccountSignIn, wasActive: boolean) => {+ const activation = generation;+ const isCurrent = () => activation === generation;+ state = { _tag: "Activating", accountId: step.accountId };+ if (step.switchFromAccountId !== null) handlers.deactivate();+ enqueue("cloud account activation", async () => {+ try {+ if (step.switchFromAccountId !== null) {+ await handlers.cleanUp({ source, accountId: step.switchFromAccountId, wasActive });+ }+ if (!isCurrent()) return;+ await handlers.prepare?.({ source, accountId: step.accountId, isCurrent });+ } catch (error) {+ if (isCurrent()) state = { _tag: "Failed", accountId: step.accountId, step, wasActive };+ throw error;+ }+ if (!isCurrent()) return;+ state = { _tag: "Active", accountId: step.accountId };+ handlers.activate({+ source,+ accountId: step.accountId,+ isAccountTransition: step.isAccountTransition,+ });+ });+ };++ const observe = (source: S, next: string | null) => {+ const previousState = state;+ if (previousState._tag === "Failed" && previousState.accountId === next) {+ generation += 1;+ activate(source, previousState.step, previousState.wasActive);+ return;+ }+ const step = planCloudAccountStep(observedAccount(), next);+ if (step._tag === "None") return;+ generation += 1;+ const wasActive = previousState._tag === "Active";++ if (step._tag === "SignOut") {+ state = { _tag: "SignedOut" };+ if (step.isAccountTransition) handlers.onSignOutTransition?.();+ handlers.deactivate();+ enqueue("cloud account cleanup", () =>+ handlers.cleanUp({ source, accountId: step.previousAccountId, wasActive }),+ );+ return;+ }++ activate(source, step, wasActive);+ };++ const detach = () => {+ if (!attached) return;+ attached.unsubscribe();+ attached = null;+ generation += 1;+ state = { _tag: "Unobserved" };+ handlers.detach();+ };++ return {+ attach(source) {+ detach();+ const current = {+ source,+ unsubscribe: source.addListener((resources) => {+ // Clerk reports `user: undefined` until it loads; pending sessions+ // still carry a user and count as signed in.+ if (resources.user === undefined) return;+ observe(source, resources.user?.id ?? null);+ }),+ };+ attached = current;+ return () => {+ if (attached === current) detach();+ };+ },+ getState: () => state,+ };+}diff --git a/packages/client-runtime/src/relay/index.ts b/packages/client-runtime/src/relay/index.tsindex 4c8104eb44b..09f14436891 100644--- a/packages/client-runtime/src/relay/index.ts+++ b/packages/client-runtime/src/relay/index.ts@@ -1,4 +1,5 @@export * as Discovery from "./discovery.ts";+export * from "./cloudAccountService.ts";export * from "./errorPresentation.ts";export * as ManagedRelay from "./managedRelay.ts";export * from "./managedRelayState.ts";diff --git a/packages/client-runtime/src/relay/managedRelayState.test.ts b/packages/client-runtime/src/relay/managedRelayState.test.tsindex 3b2410f179a..eb7772b9746 100644--- a/packages/client-runtime/src/relay/managedRelayState.test.ts+++ b/packages/client-runtime/src/relay/managedRelayState.test.ts@@ -6,11 +6,14 @@ import {import { describe, expect, it } from "@effect/vitest";import * as Effect from "effect/Effect";import * as Fiber from "effect/Fiber";-import { AtomRegistry } from "effect/reactivity";+import * as Layer from "effect/Layer";+import * as Logger from "effect/Logger";+import { Atom, AtomRegistry } from "effect/reactivity";import { afterEach, vi } from "vite-plus/test";import * as ManagedRelay from "./managedRelay.ts";import {+ createManagedRelayQueryManager,createManagedRelaySession,deregisterManagedRelayEnvironment,ManagedRelaySessionError,@@ -96,6 +99,49 @@ function clerkToken(expiresAtSeconds: number): string {describe("createManagedRelayQueryManager", () => {afterEach(resetRegistry);+ it.effect("logs a failed device listing once however many readers mount it", () =>+ Effect.gen(function* () {+ const logs: Array<unknown> = [];+ const runtime = Atom.runtime(+ Layer.mergeAll(+ Layer.succeed(+ ManagedRelay.ManagedRelayClient,+ createClient({+ listDevices: () =>+ Effect.fail(+ new ManagedRelay.ManagedRelayRequestFailedError({+ action: "list relay client devices",+ cause: new Error("relay down"),+ }),+ ),+ }),+ ),+ Logger.layer([Logger.make(({ message }) => logs.push(message))], {+ mergeWithExisting: false,+ }),+ ),+ );+ const manager = createManagedRelayQueryManager(runtime);+ setSession();+ const atom = manager.devicesAtom("account-1");++ const failed = Promise.withResolvers<void>();+ const readers = [0, 1].map(() =>+ registry.subscribe(+ atom,+ (result) => {+ if (result._tag === "Failure") failed.resolve();+ },+ { immediate: true },+ ),+ );+ yield* Effect.promise(() => failed.promise);+ for (const unsubscribe of readers) unsubscribe();++ expect(logs).toEqual([["[t3-cloud] Relay device listing failed"]]);+ }),+ );+it.effect("rejects deregistration after the account changes", () =>Effect.gen(function* () {const unlinkEnvironment = vi.fn(() => Effect.succeed({ ok: true }));diff --git a/packages/client-runtime/src/relay/managedRelayState.ts b/packages/client-runtime/src/relay/managedRelayState.tsindex cfe99e8abce..9ee7eb8c3cb 100644--- a/packages/client-runtime/src/relay/managedRelayState.ts+++ b/packages/client-runtime/src/relay/managedRelayState.ts@@ -278,29 +278,49 @@ function validateEnvironmentStatus(return Effect.succeed(status);}+function describeManagedRelayFailure(cause: Cause.Cause<unknown>): {+ readonly message: string;+ readonly traceId: string | null;+} {+ const error = Cause.squash(cause);+ return {+ message:+ isManagedRelayRequestFailedError(error) && error.relayError+ ? relayProtectedErrorMessage(error.relayError)+ : error instanceof Error+ ? error.message+ : "Could not load T3 Connect data.",+ traceId: findErrorTraceId(error),+ };+}+export function readManagedRelaySnapshotState<A>(result: AsyncResult.AsyncResult<A, unknown>,): ManagedRelaySnapshotState<A> {- let error: string | null = null;- let errorTraceId: string | null = null;- if (result._tag === "Failure") {- const cause = Cause.squash(result.cause);- error =- isManagedRelayRequestFailedError(cause) && cause.relayError- ? relayProtectedErrorMessage(cause.relayError)- : cause instanceof Error- ? cause.message- : "Could not load T3 Connect data.";- errorTraceId = findErrorTraceId(cause);- }+ const failure = result._tag === "Failure" ? describeManagedRelayFailure(result.cause) : null;return {data: Option.getOrNull(AsyncResult.value(result)),- error,- errorTraceId,+ error: failure?.message ?? null,+ errorTraceId: failure?.traceId ?? null,isPending: result.waiting,};}+// Logged where the listing runs so each failed fetch reports once, however many+// components read the atom.+const logListingFailure =+ (label: string) =>+ <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>+ self.pipe(+ Effect.tapCause((cause) => {+ if (Cause.hasInterruptsOnly(cause)) return Effect.void;+ const { message, traceId } = describeManagedRelayFailure(cause);+ return Effect.logError(`[t3-cloud] Relay ${label} listing failed`).pipe(+ Effect.annotateLogs({ message, traceId }),+ );+ }),+ );+export function createManagedRelayQueryManager(runtime: Atom.AtomRuntime<ManagedRelay.ManagedRelayClient>,options?: {@@ -349,7 +369,7 @@ export function createManagedRelayQueryManager({ ...base, stage: "relay-request" },relay.listEnvironments({ clerkToken }),);- }),+ }).pipe(logListingFailure("environment")),).pipe(Atom.swr({ staleTime, revalidateOnMount: true }),@@ -372,7 +392,7 @@ export function createManagedRelayQueryManager({ ...base, stage: "relay-request" },relay.listDevices({ clerkToken }),);- }),+ }).pipe(logListingFailure("device")),).pipe(Atom.swr({ staleTime, revalidateOnMount: true }),