diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 0faaf72f28..a69e6c573f 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -35,6 +35,7 @@ import { } from "react-native-nitro-markdown"; import { ActivityIndicator, + Alert, Image, Linking, Platform, @@ -105,6 +106,7 @@ import { environmentThreadShells, threadEnvironment } from "../../state/threads" import { useAtomCommand } from "../../state/use-atom-command"; import { useV2ItemSupport } from "../../state/v2-item-support"; import { resolveWorkspaceRelativeFilePath } from "../files/filePath"; +import { waitForThreadShellReady } from "./threadForkNavigation"; const MESSAGE_TIME_FORMATTER = new Intl.DateTimeFormat(undefined, { hour: "numeric", @@ -164,12 +166,14 @@ export interface ThreadFeedProps { } | null; } -async function waitForThreadShell(environmentId: EnvironmentId, threadId: ThreadId): Promise { +async function waitForThreadShell( + environmentId: EnvironmentId, + threadId: ThreadId, +): Promise { const atom = environmentThreadShells.threadShellAtom(scopeThreadRef(environmentId, threadId)); - const deadline = Date.now() + 2_000; - while (appAtomRegistry.get(atom) === null && Date.now() < deadline) { - await new Promise((resolve) => setTimeout(resolve, 40)); - } + return waitForThreadShellReady({ + read: () => appAtomRegistry.get(atom) !== null, + }); } function AssistantForkButton(props: { @@ -214,13 +218,18 @@ function AssistantForkButton(props: { }) .then(async (result) => { if (result._tag !== "Success") return; - await waitForThreadShell(props.environmentId, targetThreadId); - router.push( - buildThreadRoutePath({ - environmentId: props.environmentId, - threadId: targetThreadId, - }), - ); + const targetThreadReady = await waitForThreadShell(props.environmentId, targetThreadId); + if (!targetThreadReady) { + Alert.alert( + "Fork created", + "Its thread data did not reach this client. Reconnect and try opening it from the thread list.", + ); + return; + } + navigation.navigate("Thread", { + environmentId: props.environmentId, + threadId: targetThreadId, + }); }) .finally(() => setBusy(false)); }} diff --git a/apps/mobile/src/features/threads/threadForkNavigation.test.ts b/apps/mobile/src/features/threads/threadForkNavigation.test.ts new file mode 100644 index 0000000000..a56cb3da2e --- /dev/null +++ b/apps/mobile/src/features/threads/threadForkNavigation.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { waitForThreadShellReady } from "./threadForkNavigation"; + +describe("waitForThreadShellReady", () => { + it("returns true when the forked thread shell arrives before the deadline", async () => { + let elapsedMs = 0; + + const ready = await waitForThreadShellReady({ + read: () => elapsedMs >= 80, + timeoutMs: 120, + pollIntervalMs: 40, + now: () => elapsedMs, + delay: async (durationMs) => { + elapsedMs += durationMs; + }, + }); + + expect(ready).toBe(true); + }); + + it("returns false instead of navigating when the shell never arrives", async () => { + let elapsedMs = 0; + + const ready = await waitForThreadShellReady({ + read: () => false, + timeoutMs: 80, + pollIntervalMs: 40, + now: () => elapsedMs, + delay: async (durationMs) => { + elapsedMs += durationMs; + }, + }); + + expect(ready).toBe(false); + expect(elapsedMs).toBe(80); + }); +}); diff --git a/apps/mobile/src/features/threads/threadForkNavigation.ts b/apps/mobile/src/features/threads/threadForkNavigation.ts new file mode 100644 index 0000000000..97ebf9c97b --- /dev/null +++ b/apps/mobile/src/features/threads/threadForkNavigation.ts @@ -0,0 +1,26 @@ +const DEFAULT_TIMEOUT_MS = 2_000; +const DEFAULT_POLL_INTERVAL_MS = 40; + +export async function waitForThreadShellReady(input: { + readonly read: () => boolean; + readonly timeoutMs?: number; + readonly pollIntervalMs?: number; + readonly now?: () => number; + readonly delay?: (durationMs: number) => Promise; +}): Promise { + const now = input.now ?? Date.now; + const delay = + input.delay ?? + ((durationMs: number) => + new Promise((resolve) => { + setTimeout(resolve, durationMs); + })); + const deadline = now() + (input.timeoutMs ?? DEFAULT_TIMEOUT_MS); + const pollIntervalMs = input.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS; + + while (!input.read() && now() < deadline) { + await delay(Math.min(pollIntervalMs, deadline - now())); + } + + return input.read(); +}