Skip to content

Commit e9a292a

Browse files
liamwarfieldclaude
andcommitted
fix(mobile): reconnecting threads no longer open on "Failed to connect"
Opening a thread on a remote environment showed "Failed to connect. Retrying <env>..." right away, because the connection keeps its last failure across attempts, including one left over from the background. The thread pill now says "Reconnecting to <env>..." for 5 seconds before it reports the failure. Blocked connections still show their error immediately. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9826dbb commit e9a292a

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ const threadVisitTracker = createThreadVisitTracker({
340340
// Past the composer's 180 ms height animation.
341341
const COMPOSER_HEIGHT_SETTLE_MS = 220;
342342

343+
// A reconnect often fails once before it lands (a resumed app's first attempt,
344+
// a failure left over from the background), so it reads as Reconnecting first.
345+
const RECONNECT_ERROR_DELAY_MS = 5_000;
346+
343347
// Anchors, the submitted message, follow state and pending scrolls belong to
344348
// one thread, so the body remounts when the thread changes.
345349
export function ThreadDetailScreen(props: ThreadDetailScreenProps) {
@@ -521,6 +525,13 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
521525
}
522526
})();
523527
const threadSyncLabel = useDelayedStatus(selectedThreadKey, realThreadSyncLabel);
528+
const connectionRetrying =
529+
props.connectionStateLabel === "connecting" || props.connectionStateLabel === "reconnecting";
530+
const retryConnectionError = useDelayedStatus(
531+
props.environmentId,
532+
connectionRetrying ? props.connectionError : null,
533+
RECONNECT_ERROR_DELAY_MS,
534+
);
524535
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, {
525536
reportFailure: false,
526537
});
@@ -611,7 +622,7 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
611622
);
612623
const floatingStatus = ((): FloatingWorkingStatus | null => {
613624
const connectionStatus = connectionFloatingStatus({
614-
connectionError: props.connectionError,
625+
connectionError: connectionRetrying ? retryConnectionError : props.connectionError,
615626
connectionState: props.connectionStateLabel,
616627
environmentLabel: props.environmentLabel,
617628
onReconnect: props.onReconnectEnvironment,

‎apps/mobile/src/lib/useDelayedStatus.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { useState } from "react";
77

88
import { useDeadlinePassed } from "./clock";
99

10-
export function useDelayedStatus<A>(key: string, value: A | null): A | null {
10+
export function useDelayedStatus<A>(key: string, value: A | null, showDelayMs?: number): A | null {
1111
const [state, setState] = useState(() =>
12-
stepDelayedStatus<A>(null, { key, value, deadlinePassed: false }, Date.now),
12+
stepDelayedStatus<A>(null, { key, value, deadlinePassed: false, showDelayMs }, Date.now),
1313
);
1414
const deadlinePassed = useDeadlinePassed(delayedStatusDeadline(state));
15-
const next = stepDelayedStatus(state, { key, value, deadlinePassed }, Date.now);
15+
const next = stepDelayedStatus(state, { key, value, deadlinePassed, showDelayMs }, Date.now);
1616
if (next !== state) setState(next);
1717
return shownDelayedStatus(next, key);
1818
}

‎packages/client-runtime/src/delayedStatus.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ describe("stepDelayedStatus", () => {
2727
expect(step(pending, "syncing", 399)).toBe(pending);
2828
const shown = step(pending, "syncing", 400);
2929
expect(shownDelayedStatus(shown, "thread-1")).toBe("syncing");
30+
const slow = stepDelayedStatus(
31+
null,
32+
{ key: "thread-1", value: "failed", deadlinePassed: false, showDelayMs: 5_000 },
33+
() => 0,
34+
);
35+
expect(delayedStatusDeadline(slow)).toBe(5_000);
3036
});
3137

3238
it("never shows a status that clears before the show delay", () => {

‎packages/client-runtime/src/delayedStatus.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface DelayedStatusInput<A> {
2121
/** Compared by identity, so use strings or other stable values. */
2222
readonly value: A | null;
2323
readonly deadlinePassed: boolean;
24+
readonly showDelayMs?: number | undefined;
2425
}
2526

2627
/**
@@ -30,14 +31,14 @@ export interface DelayedStatusInput<A> {
3031
*/
3132
export function stepDelayedStatus<A>(
3233
state: DelayedStatusState<A> | null,
33-
{ key, value, deadlinePassed }: DelayedStatusInput<A>,
34+
{ key, value, deadlinePassed, showDelayMs = STATUS_SHOW_DELAY_MS }: DelayedStatusInput<A>,
3435
now: () => number,
3536
): DelayedStatusState<A> {
3637
const idle = (): DelayedStatusState<A> => ({ phase: "idle", key });
3738
const pending = (): DelayedStatusState<A> => ({
3839
phase: "pending",
3940
key,
40-
showAt: now() + STATUS_SHOW_DELAY_MS,
41+
showAt: now() + showDelayMs,
4142
});
4243
const shown = (shownValue: A): DelayedStatusState<A> => ({
4344
phase: "shown",

0 commit comments

Comments
 (0)