The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 15:45 UTC
fix(mobile): open threads from the local cache instead of "Thread unavailable" (#25627)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 26, 2026 at 15:03 UTC
a54ba53a5c9d76c599e613ca4ae8a8149709a24d
7 files changed
+112
-10
apps/mobile/src/features/threads/ThreadRouteScreen.tsx
+39
-1
@@ -5,7 +5,12 @@ import { buildProjectThreadStartTurnInput } from "../../lib/projectThreadStartTu
5
import { useWorktreeSetup } from "./use-worktree-setup";
6
import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup";
7
import { AppBar } from "../../components/ui/AppBar";
8
-import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
8
+import {
9
+ StackActions,
10
+ useNavigation,
11
+ useRoute,
12
+ type StaticScreenProps,
13
+} from "@react-navigation/native";
14
import { useEffect, useEffectEvent, useRef, useState, type ReactNode } from "react";
15
import * as Option from "effect/Option";
16
import {
@@ -74,6 +79,9 @@ import { useThreadComposerState } from "../../state/use-thread-composer-state";
79
import { resolveMergeBackTargetThreadId } from "@t3tools/client-runtime/state/thread-relationships";
80
import { resolveLatestMergeBackRun } from "@t3tools/client-runtime/state/thread-workflows";
81
import { threadEnvironment, useThreadFamily } from "../../state/threads";
82
+import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads";
83
+import { useProjects } from "../../state/entities";
84
+import { newTaskDestination } from "./new-task-project-selection";
85
import { projectThreadContentPresentation } from "./threadContentPresentation";
86
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
87
import {
@@ -191,6 +199,27 @@ function ThreadUnavailableScreen(props: {
199
);
200
}
201
202
+/** A thread that can't load opens a new task in its environment instead of a dead end. */
203
+function UnavailableThreadDraftRedirect(props: { readonly environmentId: EnvironmentId }) {
204
+ const navigation = useNavigation();
205
+ const projects = useProjects();
206
+ const { layout } = useAdaptiveWorkspaceLayout();
207
+ const openDraft = useEffectEvent(() => {
208
+ const project = projects.find((candidate) => candidate.environmentId === props.environmentId);
209
+ navigation.dispatch(
210
+ StackActions.replace(
211
+ "Thread",
212
+ newTaskDestination(layout.usesSplitView, projects, project, uuidv4()),
213
+ ),
214
+ );
215
+ });
216
+ // oxlint-disable-next-line t3code/react-effect-budget -- replaces this route in the navigator
217
+ useEffect(() => {
218
+ openDraft();
219
+ }, []);
220
+ return <OpeningThreadLoadingScreen />;
221
+}
222
+
223
export function ThreadRouteScreen(props: ThreadRouteScreenProps) {
224
return (
225
<ThreadScreenHost>
@@ -225,6 +254,7 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {
254
const routeEnvironmentShellState = useEnvironmentShellReadiness(environmentId);
255
const { onReconnectEnvironment } = useRemoteConnections();
256
const navigation = useNavigation();
257
+ const routeName = useRoute().name;
258
const routeConnectionState =
259
routeEnvironmentRuntime?.connectionState ?? (environmentId ? "available" : connectionState);
260
const routeThreadKey =
@@ -261,6 +291,10 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {
291
292
const stillHydrating = threadRouteIsHydrating({
293
isLoadingConnections: !connectionsReady,
294
+ environmentRegistered: routeEnvironmentRuntime !== null,
295
+ localCacheRead:
296
+ routeEnvironmentShellState.cacheRead &&
297
+ selectedThreadDetailState !== EMPTY_ENVIRONMENT_THREAD_STATE,
298
connectionState: routeConnectionState,
299
shellStatus: routeEnvironmentShellState.status,
300
shellHasError: routeEnvironmentShellState.hasError,
@@ -272,6 +306,10 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {
306
return withInspectorHeader(<OpeningThreadLoadingScreen />);
307
}
308
309
+ if (routeName === "Thread" && !props.inspectorPresentation) {
310
+ return <UnavailableThreadDraftRedirect environmentId={environmentId} />;
311
+ }
312
+
313
return withInspectorHeader(
314
<ThreadUnavailableScreen
315
actionLabel={
apps/mobile/src/features/threads/thread-route-hydration.test.ts
+17
@@ -4,6 +4,8 @@ import { threadRouteIsHydrating } from "./thread-route-hydration";
4
5
const settled = {
6
isLoadingConnections: false,
7
+ environmentRegistered: true,
8
+ localCacheRead: true,
9
connectionState: "connected" as const,
10
shellStatus: "live" as const,
11
shellHasError: false,
@@ -19,6 +21,21 @@ describe("threadRouteIsHydrating", () => {
21
expect(threadRouteIsHydrating({ ...settled, detailStatus: "empty" })).toBe(true);
22
});
23
24
+ it("waits for the local cache before trusting an offline connection", () => {
25
+ const offline = {
26
+ ...settled,
27
+ connectionState: "offline" as const,
28
+ detailStatus: "empty" as const,
29
+ };
30
+ expect(threadRouteIsHydrating({ ...offline, localCacheRead: false })).toBe(true);
31
+ expect(threadRouteIsHydrating(offline)).toBe(false);
32
+ expect(threadRouteIsHydrating({ ...offline, connectionState: "reconnecting" })).toBe(false);
33
+ expect(threadRouteIsHydrating({ ...offline, connectionState: "connecting" })).toBe(true);
34
+ expect(
35
+ threadRouteIsHydrating({ ...offline, localCacheRead: false, environmentRegistered: false }),
36
+ ).toBe(false);
37
+ });
38
+
39
it("stops waiting once an empty detail has an actionable outcome", () => {
40
expect(
41
threadRouteIsHydrating({
apps/mobile/src/features/threads/thread-route-hydration.ts
+10
-1
@@ -4,6 +4,8 @@ import type { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/thre
4
5
export function threadRouteIsHydrating(input: {
6
readonly isLoadingConnections: boolean;
7
+ readonly environmentRegistered: boolean;
8
+ readonly localCacheRead: boolean;
9
readonly connectionState: EnvironmentConnectionPhase;
10
readonly shellStatus: EnvironmentShellStatus;
11
readonly shellHasError: boolean;
@@ -16,16 +18,23 @@ export function threadRouteIsHydrating(input: {
18
if (input.isLoadingConnections) {
19
return true;
20
}
21
+ if (!input.environmentRegistered) {
22
+ return false;
23
+ }
24
+ if (!input.localCacheRead) {
25
+ return true;
26
+ }
27
+ // Only a first attempt is worth waiting out; a reconnecting environment has already failed.
28
if (
29
input.connectionState === "available" ||
30
input.connectionState === "offline" ||
31
+ input.connectionState === "reconnecting" ||
32
input.connectionState === "error"
33
) {
34
return false;
35
}
36
return (
37
input.connectionState === "connecting" ||
28
- input.connectionState === "reconnecting" ||
38
input.shellStatus === "synchronizing" ||
39
(input.connectionState === "connected" && input.shellStatus === "empty") ||
40
input.detailStatus === "synchronizing" ||
apps/mobile/src/features/workspace/workspace-selection-store.ts
+15
-5
@@ -19,7 +19,11 @@ import { workspaceRoutesOf } from "../layout/workspace-overlay-routes";
19
import type { WorkspaceSelections } from "../../persistence/mobile-preferences";
20
import { appAtomRegistry } from "../../state/atom-registry";
21
import { environmentPresentations } from "../../state/presentation";
22
-import { environmentThreadShells, threadEnvironment } from "../../state/threads";
22
+import {
23
+ environmentThreadShells,
24
+ startOpeningEnvironmentThread,
25
+ threadEnvironment,
26
+} from "../../state/threads";
27
import {
28
rememberedSettingsPage,
29
workspaceSelectionsIn,
@@ -137,11 +141,17 @@ export function settingsLinkPage() {
141
*/
142
export async function launchWorkspaceURL(): Promise<string | null> {
143
if (!REMEMBERS) return null;
140
- await Promise.all([
141
- hydrateWorkspaceSelections(),
142
- firstNativeLayoutMetrics(NATIVE_METRICS_TIMEOUT_MS),
143
- ]);
144
+ const metrics = firstNativeLayoutMetrics(NATIVE_METRICS_TIMEOUT_MS);
145
+ await hydrateWorkspaceSelections();
146
const thread = selections.threads;
147
+ // Read the chat's cached copy while the layout metrics are still on their way.
148
+ if (thread) {
149
+ startOpeningEnvironmentThread(
150
+ thread.environmentId as EnvironmentId,
151
+ thread.threadId as ThreadId,
152
+ );
153
+ }
154
+ await metrics;
155
if (!thread || !workspaceHasColumns() || !workspaceSelectionCheck.thread(thread)) return null;
156
return Linking.createURL(
157
`threads/${encodeURIComponent(thread.environmentId)}/${encodeURIComponent(thread.threadId)}`,
apps/mobile/src/state/shell.ts
+3
@@ -26,6 +26,8 @@ const EMPTY_ENVIRONMENT_SHELL_STATE_ATOM = Atom.make<EnvironmentShellState>(EMPT
26
27
const shellStatus = (state: EnvironmentShellState) => state.status;
28
const shellHasError = (state: EnvironmentShellState) => Option.isSome(state.error);
29
+// The empty placeholder stands until the environment has read its cached shell.
30
+const shellCacheRead = (state: EnvironmentShellState) => state !== EMPTY_SHELL_STATE;
31
32
export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null) {
33
const atom =
@@ -35,5 +37,6 @@ export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null
37
return {
38
status: useAtomValue(atom, shellStatus),
39
hasError: useAtomValue(atom, shellHasError),
40
+ cacheRead: useAtomValue(atom, shellCacheRead),
41
};
42
}
packages/client-runtime/src/state/presentation.test.ts
+22
@@ -135,3 +135,25 @@ describe("presentations with providers", () => {
135
}
136
});
137
});
138
+
139
+describe("connection presentation", () => {
140
+ it("presents an enabled environment as connecting until its supervisor reports", () => {
141
+ const h = harness();
142
+ const phase = () => h.registry.get(h.full.presentationAtom(FIRST))?.connection.phase;
143
+ const stop = h.registry.subscribe(h.full.presentationAtom(FIRST), () => {});
144
+ try {
145
+ expect(phase()).toBe("connecting");
146
+ h.registry.set(h.catalog, {
147
+ isReady: true,
148
+ entries: new Map([[FIRST, { ...entry(FIRST), enabled: false }]]),
149
+ });
150
+ expect(phase()).toBe("available");
151
+ h.registry.set(h.catalog, { isReady: true, entries: new Map([[FIRST, entry(FIRST)]]) });
152
+ h.registry.set(h.state, AsyncResult.success({ ...AVAILABLE_CONNECTION_STATE }));
153
+ expect(phase()).toBe("available");
154
+ } finally {
155
+ stop();
156
+ h.registry.dispose();
157
+ }
158
+ });
159
+});
packages/client-runtime/src/state/presentation.ts
+6
-3
@@ -82,10 +82,13 @@ export function createEnvironmentPresentationAtoms<E>(input: {
82
() => AVAILABLE_CONNECTION_STATE,
83
);
84
const previous = Option.getOrNull(get.self<EnvironmentPresentation | null>());
85
+ // The supervisor never publishes this placeholder; an enabled one starts out connecting.
86
const connection: EnvironmentConnectionPresentation =
86
- entry.unsupportedReason === undefined
87
- ? presentEnvironmentConnection(state)
88
- : { phase: "unsupported", error: entry.unsupportedReason, traceId: null };
87
+ entry.unsupportedReason !== undefined
88
+ ? { phase: "unsupported", error: entry.unsupportedReason, traceId: null }
89
+ : state === AVAILABLE_CONNECTION_STATE && entry.enabled
90
+ ? { phase: "connecting", error: null, traceId: null }
91
+ : presentEnvironmentConnection(state);
92
const next = {
93
entry,
94
connection: