fix(mobile): wait for fork shell before navigation

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-08-07 13:25:10 +02:00
1 parent 83c2c5de03
commit 6e887517c3
3 files changed
+85 -12

No files matched your search

+21 -12
View File
@@ -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<void> {
async function waitForThreadShell(
environmentId: EnvironmentId,
threadId: ThreadId,
): Promise<boolean> {
const atom = environmentThreadShells.threadShellAtom(scopeThreadRef(environmentId, threadId));
const deadline = Date.now() + 2_000;
while (appAtomRegistry.get(atom) === null && Date.now() < deadline) {
await new Promise<void>((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));
}}
@@ -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);
});
});
@@ -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<void>;
}): Promise<boolean> {
const now = input.now ?? Date.now;
const delay =
input.delay ??
((durationMs: number) =>
new Promise<void>((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();
}