Repository navigation
fix(mobile): app no longer stays on Loading environments after a locked background launch #15894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
juliusmarminge
merged 8 commits into
pingdotgg:main
from
nekohasekai:agent/fix-15718-ios-loading-before-unlock
Oct 19, 2026
+220
−1
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
45aff1c
fix(mobile): app no longer stays on Loading environments after a lock…
nekohasekai 195f7d4
fix(mobile): permission notice waits for protected data
nekohasekai 57b150d
Merge remote-tracking branch 'origin/main' into takeover/ssh-relay-re…
juliusmarminge 1a0665f
test(mobile): exercise protected storage through startup runtime
juliusmarminge 271c98b
fix(mobile): wait for confirmed protected storage at startup
juliusmarminge 7b3b865
refactor(mobile): rely on Clerk protected storage recovery
juliusmarminge aa3340e
docs(mobile): remove redundant startup gate guidance
juliusmarminge 1598f41
fix(mobile): wait for activation on older iOS binaries
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import * as Effect from "effect/Effect"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| appState: "background" as string | null, | ||
| nativeCheckSupported: true, | ||
| platform: "ios", | ||
| subscribed: Promise.withResolvers<void>(), | ||
| listeners: new Set<(state: string) => void>(), | ||
| isProtectedDataAvailable: vi.fn<() => Promise<boolean>>(), | ||
| readStorage: vi.fn<() => Promise<void>>(), | ||
| })); | ||
|
|
||
| vi.mock("expo", () => ({ | ||
| requireOptionalNativeModule: () => ({ | ||
| ...(mocks.nativeCheckSupported && { | ||
| isProtectedDataAvailable: mocks.isProtectedDataAvailable, | ||
| }), | ||
| }), | ||
| })); | ||
| vi.mock("react-native", () => ({ | ||
| Platform: { | ||
| get OS() { | ||
| return mocks.platform; | ||
| }, | ||
| }, | ||
| AppState: { | ||
| get currentState() { | ||
| return mocks.appState; | ||
| }, | ||
| addEventListener: (_event: string, listener: (state: string) => void) => { | ||
| mocks.listeners.add(listener); | ||
| mocks.subscribed.resolve(); | ||
| return { remove: () => mocks.listeners.delete(listener) }; | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("../persistence/layer", async () => { | ||
| const Effect = await import("effect/Effect"); | ||
| const Layer = await import("effect/Layer"); | ||
| return { layer: Layer.effectDiscard(Effect.promise(mocks.readStorage)) }; | ||
| }); | ||
| vi.mock("../features/cloud/dpop", async () => ({ | ||
| layer: (await import("effect/Layer")).empty, | ||
| })); | ||
| vi.mock("../features/cloud/managedRelayLayer", async () => { | ||
| const Layer = await import("effect/Layer"); | ||
| return { layer: () => Layer.empty }; | ||
| }); | ||
| vi.mock("../features/cloud/publicConfig", () => ({ | ||
| resolveCloudPublicConfig: () => ({ relay: { url: null } }), | ||
| })); | ||
| vi.mock("../features/observability/tracing", async () => ({ | ||
| layer: (await import("effect/Layer")).empty, | ||
| })); | ||
| vi.mock("@t3tools/client-runtime/rpc", async () => { | ||
| const Layer = await import("effect/Layer"); | ||
| return { layerRemoteHttpClient: () => Layer.empty }; | ||
| }); | ||
|
|
||
| let runtime: typeof import("./runtime").runtime | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| mocks.appState = "background"; | ||
| mocks.nativeCheckSupported = true; | ||
| mocks.platform = "ios"; | ||
| mocks.subscribed = Promise.withResolvers<void>(); | ||
| mocks.listeners.clear(); | ||
| mocks.isProtectedDataAvailable.mockReset(); | ||
| mocks.readStorage.mockReset().mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await runtime?.dispose(); | ||
| runtime = undefined; | ||
| }); | ||
|
|
||
| describe("protected storage startup", () => { | ||
| it.each([ | ||
| { launch: "background", state: "background", checkRejects: false }, | ||
| { launch: "inactive", state: "inactive", checkRejects: false }, | ||
| { launch: "unknown", state: "unknown", checkRejects: false }, | ||
| { launch: "unset", state: null, checkRejects: false }, | ||
| { launch: "background with a failed native check", state: "background", checkRejects: true }, | ||
| { | ||
| launch: "background without a native check", | ||
| state: "background", | ||
| nativeCheckSupported: false, | ||
| }, | ||
| { launch: "unknown without a native check", state: "unknown", nativeCheckSupported: false }, | ||
| { launch: "unset without a native check", state: null, nativeCheckSupported: false }, | ||
| ])( | ||
| "keeps a locked $launch launch usable after activation without rebuilding its runtime", | ||
| async (scenario) => { | ||
| mocks.appState = scenario.state; | ||
| mocks.nativeCheckSupported = scenario.nativeCheckSupported ?? true; | ||
| mocks.isProtectedDataAvailable.mockImplementation(async () => { | ||
| if (scenario.checkRejects) throw new Error("Protected data check unavailable."); | ||
| return false; | ||
| }); | ||
| mocks.readStorage.mockImplementation(async () => { | ||
| if (mocks.appState !== "active") throw new Error("User interaction is not allowed."); | ||
| }); | ||
| ({ runtime } = await import("./runtime")); | ||
| const startup = runtime.runPromiseExit(Effect.succeed("ready")); | ||
|
|
||
| // A missing gate finishes startup with a cached storage failure instead. | ||
| await Promise.race([mocks.subscribed.promise, startup]); | ||
| expect(mocks.readStorage).not.toHaveBeenCalled(); | ||
| mocks.appState = "inactive"; | ||
| for (const listener of mocks.listeners) listener("inactive"); | ||
| await Promise.resolve(); | ||
| expect(mocks.readStorage).not.toHaveBeenCalled(); | ||
|
|
||
| mocks.appState = "active"; | ||
| for (const listener of mocks.listeners) listener("active"); | ||
| expect(await startup).toMatchObject({ _tag: "Success", value: "ready" }); | ||
| expect(await runtime.runPromise(Effect.succeed("still ready"))).toBe("still ready"); | ||
| expect(mocks.readStorage).toHaveBeenCalledOnce(); | ||
| expect(mocks.listeners.size).toBe(0); | ||
| mocks.appState = "background"; | ||
| await (await import("./protectedData")).whenProtectedDataAvailable(); | ||
| expect(mocks.isProtectedDataAvailable).toHaveBeenCalledTimes( | ||
| mocks.nativeCheckSupported ? 1 : 0, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| it.each([ | ||
| { | ||
| launch: "unlocked background", | ||
| state: "background", | ||
| platform: "ios", | ||
| nativeCheckSupported: true, | ||
| }, | ||
| { launch: "active foreground", state: "active", platform: "ios", nativeCheckSupported: true }, | ||
| { | ||
| launch: "Android background", | ||
| state: "background", | ||
| platform: "android", | ||
| nativeCheckSupported: false, | ||
| }, | ||
| { | ||
| launch: "Android with a native check", | ||
| state: "background", | ||
| platform: "android", | ||
| nativeCheckSupported: true, | ||
| }, | ||
| ])("starts storage on $launch launch without waiting for activation", async (scenario) => { | ||
| mocks.appState = scenario.state; | ||
| mocks.platform = scenario.platform; | ||
| mocks.nativeCheckSupported = scenario.nativeCheckSupported; | ||
| mocks.isProtectedDataAvailable.mockResolvedValue(scenario.platform === "ios"); | ||
| ({ runtime } = await import("./runtime")); | ||
|
|
||
| expect(await runtime.runPromise(Effect.succeed("ready"))).toBe("ready"); | ||
| expect(mocks.isProtectedDataAvailable).toHaveBeenCalledTimes( | ||
| scenario.platform === "ios" && scenario.state === "background" ? 1 : 0, | ||
| ); | ||
| expect(mocks.readStorage).toHaveBeenCalledOnce(); | ||
| expect(mocks.listeners.size).toBe(0); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { requireOptionalNativeModule } from "expo"; | ||
| import { AppState, Platform } from "react-native"; | ||
|
|
||
| const NativeControls = requireOptionalNativeModule<{ | ||
| readonly isProtectedDataAvailable?: () => Promise<boolean>; | ||
| }>("T3NativeControls"); | ||
|
|
||
| let protectedDataAvailable: Promise<void> | undefined; | ||
|
|
||
| // A locked iOS background launch cannot read startup storage. Cache readiness | ||
| // for this launch only; later device locks do not pause an initialized runtime. | ||
| export function whenProtectedDataAvailable(): Promise<void> { | ||
| protectedDataAvailable ??= new Promise((resolve) => { | ||
| const isAvailable = NativeControls?.isProtectedDataAvailable; | ||
| if (Platform.OS !== "ios" || AppState.currentState === "active") { | ||
| resolve(); | ||
| return; | ||
| } | ||
| const subscription = AppState.addEventListener("change", (state) => { | ||
| if (state === "active") done(); | ||
| }); | ||
| function done() { | ||
| subscription.remove(); | ||
| resolve(); | ||
| } | ||
| isAvailable?.().then( | ||
| (available) => { | ||
| if (available) done(); | ||
| }, | ||
| () => { | ||
| // A failed check does not establish readiness; activation still can. | ||
| }, | ||
| ); | ||
| }); | ||
| return protectedDataAvailable; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.