@devries-cameron20 / t3code Mirror · read-only / commits / c21700d412
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 23, 01:45 UTC

fix(mobile): app no longer stays on Loading environments after a locked background launch (#15894)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

nekohasekai committed Oct 20, 2026 at 04:24 UTC c21700d41266997c9622e7aaef99eba38df76578
6 files changed +220 -1
apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift
+6
@@ -167,6 +167,12 @@ public final class T3NativeControlsModule: Module {
167 return bounds.width > bounds.height ? "landscape" : "portrait"
168 }
169
170 + @JS
171 + @MainActor
172 + func isProtectedDataAvailable() async -> Bool {
173 + UIApplication.shared.isProtectedDataAvailable
174 + }
175 +
176 private func launchArgument(_ flag: String) -> String? {
177 let arguments = ProcessInfo.processInfo.arguments
178 guard
apps/mobile/src/features/connection/PermissionUpdateNotice.tsx
+3 -1
@@ -5,6 +5,7 @@ import { Atom } from "effect/reactivity";
5 import * as SecureStore from "expo-secure-store";
6 import { useEffect, useMemo, useRef, useState } from "react";
7
8 +import { whenProtectedDataAvailable } from "../../lib/protectedData";
9 import { useEnvironments } from "../../state/environments";
10 import { environmentSession } from "../../state/session";
11
@@ -35,7 +36,8 @@ export function PermissionUpdateNotice() {
36
37 useEffect(() => {
38 let active = true;
38 - void SecureStore.getItemAsync(storageKey)
39 + void whenProtectedDataAvailable()
40 + .then(() => SecureStore.getItemAsync(storageKey))
41 .then((raw) => {
42 const value: unknown = raw === null ? [] : JSON.parse(raw);
43 if (active)
apps/mobile/src/lib/protectedData.test.ts new
+165
@@ -0,0 +1,165 @@
1 +import * as Effect from "effect/Effect";
2 +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3 +
4 +const mocks = vi.hoisted(() => ({
5 + appState: "background" as string | null,
6 + nativeCheckSupported: true,
7 + platform: "ios",
8 + subscribed: Promise.withResolvers<void>(),
9 + listeners: new Set<(state: string) => void>(),
10 + isProtectedDataAvailable: vi.fn<() => Promise<boolean>>(),
11 + readStorage: vi.fn<() => Promise<void>>(),
12 +}));
13 +
14 +vi.mock("expo", () => ({
15 + requireOptionalNativeModule: () => ({
16 + ...(mocks.nativeCheckSupported && {
17 + isProtectedDataAvailable: mocks.isProtectedDataAvailable,
18 + }),
19 + }),
20 +}));
21 +vi.mock("react-native", () => ({
22 + Platform: {
23 + get OS() {
24 + return mocks.platform;
25 + },
26 + },
27 + AppState: {
28 + get currentState() {
29 + return mocks.appState;
30 + },
31 + addEventListener: (_event: string, listener: (state: string) => void) => {
32 + mocks.listeners.add(listener);
33 + mocks.subscribed.resolve();
34 + return { remove: () => mocks.listeners.delete(listener) };
35 + },
36 + },
37 +}));
38 +
39 +vi.mock("../persistence/layer", async () => {
40 + const Effect = await import("effect/Effect");
41 + const Layer = await import("effect/Layer");
42 + return { layer: Layer.effectDiscard(Effect.promise(mocks.readStorage)) };
43 +});
44 +vi.mock("../features/cloud/dpop", async () => ({
45 + layer: (await import("effect/Layer")).empty,
46 +}));
47 +vi.mock("../features/cloud/managedRelayLayer", async () => {
48 + const Layer = await import("effect/Layer");
49 + return { layer: () => Layer.empty };
50 +});
51 +vi.mock("../features/cloud/publicConfig", () => ({
52 + resolveCloudPublicConfig: () => ({ relay: { url: null } }),
53 +}));
54 +vi.mock("../features/observability/tracing", async () => ({
55 + layer: (await import("effect/Layer")).empty,
56 +}));
57 +vi.mock("@t3tools/client-runtime/rpc", async () => {
58 + const Layer = await import("effect/Layer");
59 + return { layerRemoteHttpClient: () => Layer.empty };
60 +});
61 +
62 +let runtime: typeof import("./runtime").runtime | undefined;
63 +
64 +beforeEach(() => {
65 + vi.resetModules();
66 + mocks.appState = "background";
67 + mocks.nativeCheckSupported = true;
68 + mocks.platform = "ios";
69 + mocks.subscribed = Promise.withResolvers<void>();
70 + mocks.listeners.clear();
71 + mocks.isProtectedDataAvailable.mockReset();
72 + mocks.readStorage.mockReset().mockResolvedValue(undefined);
73 +});
74 +
75 +afterEach(async () => {
76 + await runtime?.dispose();
77 + runtime = undefined;
78 +});
79 +
80 +describe("protected storage startup", () => {
81 + it.each([
82 + { launch: "background", state: "background", checkRejects: false },
83 + { launch: "inactive", state: "inactive", checkRejects: false },
84 + { launch: "unknown", state: "unknown", checkRejects: false },
85 + { launch: "unset", state: null, checkRejects: false },
86 + { launch: "background with a failed native check", state: "background", checkRejects: true },
87 + {
88 + launch: "background without a native check",
89 + state: "background",
90 + nativeCheckSupported: false,
91 + },
92 + { launch: "unknown without a native check", state: "unknown", nativeCheckSupported: false },
93 + { launch: "unset without a native check", state: null, nativeCheckSupported: false },
94 + ])(
95 + "keeps a locked $launch launch usable after activation without rebuilding its runtime",
96 + async (scenario) => {
97 + mocks.appState = scenario.state;
98 + mocks.nativeCheckSupported = scenario.nativeCheckSupported ?? true;
99 + mocks.isProtectedDataAvailable.mockImplementation(async () => {
100 + if (scenario.checkRejects) throw new Error("Protected data check unavailable.");
101 + return false;
102 + });
103 + mocks.readStorage.mockImplementation(async () => {
104 + if (mocks.appState !== "active") throw new Error("User interaction is not allowed.");
105 + });
106 + ({ runtime } = await import("./runtime"));
107 + const startup = runtime.runPromiseExit(Effect.succeed("ready"));
108 +
109 + // A missing gate finishes startup with a cached storage failure instead.
110 + await Promise.race([mocks.subscribed.promise, startup]);
111 + expect(mocks.readStorage).not.toHaveBeenCalled();
112 + mocks.appState = "inactive";
113 + for (const listener of mocks.listeners) listener("inactive");
114 + await Promise.resolve();
115 + expect(mocks.readStorage).not.toHaveBeenCalled();
116 +
117 + mocks.appState = "active";
118 + for (const listener of mocks.listeners) listener("active");
119 + expect(await startup).toMatchObject({ _tag: "Success", value: "ready" });
120 + expect(await runtime.runPromise(Effect.succeed("still ready"))).toBe("still ready");
121 + expect(mocks.readStorage).toHaveBeenCalledOnce();
122 + expect(mocks.listeners.size).toBe(0);
123 + mocks.appState = "background";
124 + await (await import("./protectedData")).whenProtectedDataAvailable();
125 + expect(mocks.isProtectedDataAvailable).toHaveBeenCalledTimes(
126 + mocks.nativeCheckSupported ? 1 : 0,
127 + );
128 + },
129 + );
130 +
131 + it.each([
132 + {
133 + launch: "unlocked background",
134 + state: "background",
135 + platform: "ios",
136 + nativeCheckSupported: true,
137 + },
138 + { launch: "active foreground", state: "active", platform: "ios", nativeCheckSupported: true },
139 + {
140 + launch: "Android background",
141 + state: "background",
142 + platform: "android",
143 + nativeCheckSupported: false,
144 + },
145 + {
146 + launch: "Android with a native check",
147 + state: "background",
148 + platform: "android",
149 + nativeCheckSupported: true,
150 + },
151 + ])("starts storage on $launch launch without waiting for activation", async (scenario) => {
152 + mocks.appState = scenario.state;
153 + mocks.platform = scenario.platform;
154 + mocks.nativeCheckSupported = scenario.nativeCheckSupported;
155 + mocks.isProtectedDataAvailable.mockResolvedValue(scenario.platform === "ios");
156 + ({ runtime } = await import("./runtime"));
157 +
158 + expect(await runtime.runPromise(Effect.succeed("ready"))).toBe("ready");
159 + expect(mocks.isProtectedDataAvailable).toHaveBeenCalledTimes(
160 + scenario.platform === "ios" && scenario.state === "background" ? 1 : 0,
161 + );
162 + expect(mocks.readStorage).toHaveBeenCalledOnce();
163 + expect(mocks.listeners.size).toBe(0);
164 + });
165 +});
apps/mobile/src/lib/protectedData.ts new
+36
@@ -0,0 +1,36 @@
1 +import { requireOptionalNativeModule } from "expo";
2 +import { AppState, Platform } from "react-native";
3 +
4 +const NativeControls = requireOptionalNativeModule<{
5 + readonly isProtectedDataAvailable?: () => Promise<boolean>;
6 +}>("T3NativeControls");
7 +
8 +let protectedDataAvailable: Promise<void> | undefined;
9 +
10 +// A locked iOS background launch cannot read startup storage. Cache readiness
11 +// for this launch only; later device locks do not pause an initialized runtime.
12 +export function whenProtectedDataAvailable(): Promise<void> {
13 + protectedDataAvailable ??= new Promise((resolve) => {
14 + const isAvailable = NativeControls?.isProtectedDataAvailable;
15 + if (Platform.OS !== "ios" || AppState.currentState === "active") {
16 + resolve();
17 + return;
18 + }
19 + const subscription = AppState.addEventListener("change", (state) => {
20 + if (state === "active") done();
21 + });
22 + function done() {
23 + subscription.remove();
24 + resolve();
25 + }
26 + isAvailable?.().then(
27 + (available) => {
28 + if (available) done();
29 + },
30 + () => {
31 + // A failed check does not establish readiness; activation still can.
32 + },
33 + );
34 + });
35 + return protectedDataAvailable;
36 +}
apps/mobile/src/lib/runtime.ts
+5
@@ -1,3 +1,4 @@
1 +import * as Effect from "effect/Effect";
2 import * as Layer from "effect/Layer";
3 import * as ManagedRuntime from "effect/ManagedRuntime";
4 import * as Socket from "effect/socket/Socket";
@@ -10,6 +11,7 @@ import { resolveCloudPublicConfig } from "../features/cloud/publicConfig";
11 import * as Tracing from "../features/observability/tracing";
12 import * as Persistence from "../persistence/layer";
13 import { disposeOnFoundationReplace, type FoundationHotModule } from "./foundation-fast-refresh";
14 +import { whenProtectedDataAvailable } from "./protectedData";
15
16 declare const module: { readonly hot?: FoundationHotModule } | undefined;
17
@@ -35,6 +37,9 @@ const layerRuntime = Layer.merge(
37 Layer.provideMerge(layerHttpClient),
38 Layer.provideMerge(Tracing.layer.pipe(Layer.provide(layerHttpClient))),
39 Layer.provideMerge(Persistence.layer),
40 + // These layers read the keychain and the database while they build, and a
41 + // failed build is kept for the life of the process.
42 + Layer.provide(Layer.effectDiscard(Effect.promise(whenProtectedDataAvailable))),
43 );
44
45 export const runtime: ManagedRuntime.ManagedRuntime<
apps/mobile/src/lib/storage.test.ts
+5
@@ -70,6 +70,10 @@ const mocks = vi.hoisted(() => {
70 };
71 });
72
73 +vi.mock("expo", () => ({
74 + requireOptionalNativeModule: () => null,
75 +}));
76 +
77 vi.mock("expo-secure-store", () => ({
78 deleteItemAsync: mocks.deleteItemAsync,
79 getItemAsync: mocks.getItemAsync,
@@ -89,6 +93,7 @@ vi.mock("expo-constants", () => ({
93 }));
94
95 vi.mock("react-native", () => ({
96 + AppState: { currentState: "active" },
97 Platform: {
98 OS: "ios",
99 },