Skip to content
TypeScript

fix(mobile): reconnecting threads no longer open on "Failed to connect" (#26643)

Liam Warfield3e42e668c4cd
diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx
index 6695e7963ef..eab4d492766 100644
--- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx
+++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx
@@ -346,6 +346,9 @@ const NO_RUN_STOP_ATOM = Atom.make<RunStopPresentation | null>(null).pipe(
Atom.withLabel("mobile-thread-run-stop:empty"),
);
+// Connection failures survive retries and app suspension.
+const RECONNECT_ERROR_DELAY_MS = 5_000;
+
// Anchors, the submitted message, follow state and pending scrolls belong to
// one thread, so the body remounts when the thread changes.
export function ThreadDetailScreen(props: ThreadDetailScreenProps) {
@@ -527,6 +530,14 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
}
})();
const threadSyncLabel = useDelayedStatus(selectedThreadKey, realThreadSyncLabel);
+ const connectionRetrying =
+ props.connectionStateLabel === "connecting" || props.connectionStateLabel === "reconnecting";
+ const retryError = connectionRetrying ? props.connectionError : null;
+ const retryConnectionError = useDelayedStatus(
+ `${props.environmentId}:${retryError !== null}`,
+ retryError,
+ RECONNECT_ERROR_DELAY_MS,
+ );
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, {
reportFailure: false,
});
@@ -661,7 +672,7 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
);
const floatingStatus = ((): FloatingWorkingStatus | null => {
const connectionStatus = connectionFloatingStatus({
- connectionError: props.connectionError,
+ connectionError: connectionRetrying ? retryConnectionError : props.connectionError,
connectionState: props.connectionStateLabel,
environmentLabel: props.environmentLabel,
onReconnect: props.onReconnectEnvironment,
diff --git a/apps/mobile/src/lib/useDelayedStatus.ts b/apps/mobile/src/lib/useDelayedStatus.ts
index 2bf3aaa5538..305a375a1da 100644
--- a/apps/mobile/src/lib/useDelayedStatus.ts
+++ b/apps/mobile/src/lib/useDelayedStatus.ts
@@ -7,12 +7,12 @@ import { useState } from "react";
import { useDeadlinePassed } from "./clock";
-export function useDelayedStatus<A>(key: string, value: A | null): A | null {
+export function useDelayedStatus<A>(key: string, value: A | null, showDelayMs?: number): A | null {
const [state, setState] = useState(() =>
- stepDelayedStatus<A>(null, { key, value, deadlinePassed: false }, Date.now),
+ stepDelayedStatus<A>(null, { key, value, deadlinePassed: false, showDelayMs }, Date.now),
);
const deadlinePassed = useDeadlinePassed(delayedStatusDeadline(state));
- const next = stepDelayedStatus(state, { key, value, deadlinePassed }, Date.now);
+ const next = stepDelayedStatus(state, { key, value, deadlinePassed, showDelayMs }, Date.now);
if (next !== state) setState(next);
return shownDelayedStatus(next, key);
}
diff --git a/packages/client-runtime/src/delayedStatus.test.ts b/packages/client-runtime/src/delayedStatus.test.ts
index f49e1ad78a8..d4a860c32fc 100644
--- a/packages/client-runtime/src/delayedStatus.test.ts
+++ b/packages/client-runtime/src/delayedStatus.test.ts
@@ -27,6 +27,15 @@ describe("stepDelayedStatus", () => {
expect(step(pending, "syncing", 399)).toBe(pending);
const shown = step(pending, "syncing", 400);
expect(shownDelayedStatus(shown, "thread-1")).toBe("syncing");
+ const slow = stepDelayedStatus(
+ null,
+ { key: "thread-1", value: "failed", deadlinePassed: false, showDelayMs: 5_000 },
+ () => 0,
+ );
+ expect(delayedStatusDeadline(slow)).toBe(5_000);
+ expect(shownDelayedStatus(step(slow, "failed", 4_999), "thread-1")).toBeNull();
+ expect(shownDelayedStatus(step(slow, "failed", 5_000), "thread-1")).toBe("failed");
+ expect(step(slow, null, 200).phase).toBe("idle");
});
it("never shows a status that clears before the show delay", () => {
@@ -69,5 +78,14 @@ describe("stepDelayedStatus", () => {
const other = step(shown, "syncing", 500, "thread-2");
expect(shownDelayedStatus(other, "thread-2")).toBeNull();
expect(delayedStatusDeadline(other)).toBe(900);
+ const cleared = step(shown, null, 500, "cleared");
+ expect(shownDelayedStatus(cleared, "cleared")).toBeNull();
+ const retry = stepDelayedStatus(
+ cleared,
+ { key: "thread-1", value: "failed", deadlinePassed: false, showDelayMs: 5_000 },
+ () => 501,
+ );
+ expect(shownDelayedStatus(retry, "thread-1")).toBeNull();
+ expect(delayedStatusDeadline(retry)).toBe(5_501);
});
});
diff --git a/packages/client-runtime/src/delayedStatus.ts b/packages/client-runtime/src/delayedStatus.ts
index 7de7962ef64..ad07773a8d9 100644
--- a/packages/client-runtime/src/delayedStatus.ts
+++ b/packages/client-runtime/src/delayedStatus.ts
@@ -21,6 +21,7 @@ export interface DelayedStatusInput<A> {
/** Compared by identity, so use strings or other stable values. */
readonly value: A | null;
readonly deadlinePassed: boolean;
+ readonly showDelayMs?: number | undefined;
}
/**
@@ -30,14 +31,14 @@ export interface DelayedStatusInput<A> {
*/
export function stepDelayedStatus<A>(
state: DelayedStatusState<A> | null,
- { key, value, deadlinePassed }: DelayedStatusInput<A>,
+ { key, value, deadlinePassed, showDelayMs = STATUS_SHOW_DELAY_MS }: DelayedStatusInput<A>,
now: () => number,
): DelayedStatusState<A> {
const idle = (): DelayedStatusState<A> => ({ phase: "idle", key });
const pending = (): DelayedStatusState<A> => ({
phase: "pending",
key,
- showAt: now() + STATUS_SHOW_DELAY_MS,
+ showAt: now() + showDelayMs,
});
const shown = (shownValue: A): DelayedStatusState<A> => ({
phase: "shown",