fix(mobile): wait for fork shell before navigation

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-07-24 17:45:09 +02:00
1 parent c841e2a160
commit bccaea5fcd
3 files changed
+81 -6

No files matched your search

@@ -35,6 +35,7 @@ import {
} from "react-native-nitro-markdown";
import {
ActivityIndicator,
Alert,
Image,
Linking,
Platform,
@@ -108,6 +109,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",
@@ -158,12 +160,14 @@ export interface ThreadFeedProps {
readonly skills?: ReadonlyArray<SelectableMarkdownSkill>;
}
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: {
@@ -208,7 +212,14 @@ function AssistantForkButton(props: {
})
.then(async (result) => {
if (result._tag !== "Success") return;
await waitForThreadShell(props.environmentId, 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,
@@ -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();
}