Skip to content

Commit 722ce0d

Browse files
fix(mobile): a draft stamped to a slower environment waits for that environment
resolveRouteDraft treated any loaded project as readiness, so on a cold launch a draft stamped to environment B was reported missing as soon as environment A's projects arrived, and the route was replaced with the picker. It now stays pending while the stamped environment is still connecting or synchronizing its shell, and reports missing only once that environment has settled without the project. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1f49c48 commit 722ce0d

3 files changed

Lines changed: 101 additions & 3 deletions

File tree

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,11 @@ import {
100100
readNewTaskFlowState,
101101
resolveRouteDraft,
102102
type RouteDraftResolution,
103+
stampedEnvironmentProjectsLoading,
103104
updateNewTaskFlowState,
104105
} from "./new-task-flow-state";
106+
import { useEnvironmentShellReadiness } from "../../state/shell";
107+
import { useConnectionsReady, useWorkspaceEnvironments } from "../../state/workspace";
105108

106109
type WorkspaceMode = "local" | "worktree";
107110

@@ -242,6 +245,22 @@ export function NewTaskFlowProvider(
242245
const routeDraft = useComposerDraft(
243246
storedFlowState.draftKey === null ? (props.routeDraftId ?? null) : null,
244247
);
248+
// Readiness of the environment the route's draft is stamped to; another
249+
// environment's projects loading first says nothing about it.
250+
const stampedEnvironmentId = routeDraft.project?.environmentId ?? null;
251+
const connectionsReady = useConnectionsReady();
252+
const stampedEnvironmentConnection =
253+
useWorkspaceEnvironments().find(
254+
(environment) => environment.environmentId === stampedEnvironmentId,
255+
)?.connectionState ?? null;
256+
const stampedEnvironmentShell = useEnvironmentShellReadiness(stampedEnvironmentId);
257+
const stampedEnvironmentLoading = stampedEnvironmentProjectsLoading({
258+
connectionsReady,
259+
connectionState: stampedEnvironmentConnection,
260+
shellCacheRead: stampedEnvironmentShell.cacheRead,
261+
shellStatus: stampedEnvironmentShell.status,
262+
shellHasError: stampedEnvironmentShell.hasError,
263+
});
245264
const resolveRoute = (
246265
flow: NewTaskFlowState,
247266
loaded: boolean,
@@ -253,6 +272,7 @@ export function NewTaskFlowProvider(
253272
draftsLoaded: loaded,
254273
stamp,
255274
projects,
275+
stampedEnvironmentLoading,
256276
});
257277
const routeDraftResolution = resolveRoute(storedFlowState, draftsLoaded, routeDraft.project);
258278
const flowState =

‎apps/mobile/src/features/threads/new-task-flow-state.test.ts‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
pruneNewTaskFlowStates,
1010
readNewTaskFlowState,
1111
resolveRouteDraft,
12+
stampedEnvironmentProjectsLoading,
1213
updateNewTaskFlowState,
1314
} from "./new-task-flow-state";
1415

@@ -68,6 +69,7 @@ describe("resolveRouteDraft", () => {
6869
draftsLoaded: true,
6970
stamp,
7071
projects: [project],
72+
stampedEnvironmentLoading: false,
7173
};
7274

7375
it("binds the route's draft and its stamped project", () => {
@@ -87,11 +89,26 @@ describe("resolveRouteDraft", () => {
8789
expect(resolveRouteDraft({ ...base, flow })).toEqual({ kind: "none" });
8890
});
8991

90-
it("waits for hydration, and for projects while none have loaded", () => {
92+
it("waits for hydration, and for the stamped environment's projects", () => {
9193
expect(resolveRouteDraft({ ...base, draftsLoaded: false, stamp: undefined })).toEqual({
9294
kind: "pending",
9395
});
94-
expect(resolveRouteDraft({ ...base, projects: [] })).toEqual({ kind: "pending" });
96+
expect(resolveRouteDraft({ ...base, projects: [], stampedEnvironmentLoading: true })).toEqual({
97+
kind: "pending",
98+
});
99+
});
100+
101+
it("waits for a slower environment's projects while another environment's have loaded", () => {
102+
const environmentB = EnvironmentId.make("env-b");
103+
const projectB = { environmentId: environmentB, id: ProjectId.make("project-b") };
104+
const stampB = { environmentId: environmentB, projectId: projectB.id };
105+
106+
expect(resolveRouteDraft({ ...base, stamp: stampB, stampedEnvironmentLoading: true })).toEqual({
107+
kind: "pending",
108+
});
109+
expect(
110+
resolveRouteDraft({ ...base, stamp: stampB, projects: [project, projectB] }),
111+
).toMatchObject({ kind: "open", flow: { environmentId: environmentB } });
95112
});
96113

97114
it("reports a draft that is gone or whose project is gone", () => {
@@ -101,3 +118,37 @@ describe("resolveRouteDraft", () => {
101118
).toEqual({ kind: "missing" });
102119
});
103120
});
121+
122+
describe("stampedEnvironmentProjectsLoading", () => {
123+
const settled = {
124+
connectionsReady: true,
125+
connectionState: "connected" as const,
126+
shellCacheRead: true,
127+
shellStatus: "live" as const,
128+
shellHasError: false,
129+
};
130+
131+
it("waits while the environment is still delivering its first project list", () => {
132+
expect(stampedEnvironmentProjectsLoading({ ...settled, connectionsReady: false })).toBe(true);
133+
expect(stampedEnvironmentProjectsLoading({ ...settled, shellCacheRead: false })).toBe(true);
134+
expect(stampedEnvironmentProjectsLoading({ ...settled, connectionState: "connecting" })).toBe(
135+
true,
136+
);
137+
expect(stampedEnvironmentProjectsLoading({ ...settled, shellStatus: "synchronizing" })).toBe(
138+
true,
139+
);
140+
});
141+
142+
it("settles once the list is live, or when nothing more will arrive", () => {
143+
expect(stampedEnvironmentProjectsLoading(settled)).toBe(false);
144+
expect(stampedEnvironmentProjectsLoading({ ...settled, connectionState: null })).toBe(false);
145+
expect(
146+
stampedEnvironmentProjectsLoading({
147+
...settled,
148+
connectionState: "reconnecting",
149+
shellStatus: "synchronizing",
150+
}),
151+
).toBe(false);
152+
expect(stampedEnvironmentProjectsLoading({ ...settled, shellHasError: true })).toBe(false);
153+
});
154+
});

‎apps/mobile/src/features/threads/new-task-flow-state.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2+
import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
13
import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
24
import { Atom } from "effect/reactivity";
35

@@ -86,6 +88,8 @@ export function resolveRouteDraft(input: {
8688
readonly environmentId: EnvironmentId;
8789
readonly id: ProjectId;
8890
}>;
91+
/** See `stampedEnvironmentProjectsLoading`. */
92+
readonly stampedEnvironmentLoading: boolean;
8993
}): RouteDraftResolution {
9094
const { routeDraftId, stamp } = input;
9195
if (routeDraftId === undefined || input.flow.draftKey !== null) return { kind: "none" };
@@ -97,7 +101,7 @@ export function resolveRouteDraft(input: {
97101
const loaded = input.projects.some(
98102
(project) => project.environmentId === stamp.environmentId && project.id === stamp.projectId,
99103
);
100-
if (!loaded) return input.projects.length === 0 ? { kind: "pending" } : { kind: "missing" };
104+
if (!loaded) return input.stampedEnvironmentLoading ? { kind: "pending" } : { kind: "missing" };
101105
return {
102106
kind: "open",
103107
flow: {
@@ -109,6 +113,29 @@ export function resolveRouteDraft(input: {
109113
};
110114
}
111115

116+
/**
117+
* Whether the stamped environment may still deliver its project list. An
118+
* unknown, offline, or failed environment is settled, so a project it lacks
119+
* sends the route back to the picker. Mirrors `threadRouteIsHydrating`.
120+
*/
121+
export function stampedEnvironmentProjectsLoading(input: {
122+
readonly connectionsReady: boolean;
123+
readonly connectionState: EnvironmentConnectionPhase | null;
124+
readonly shellCacheRead: boolean;
125+
readonly shellStatus: EnvironmentShellStatus;
126+
readonly shellHasError: boolean;
127+
}): boolean {
128+
if (input.shellHasError) return false;
129+
if (!input.connectionsReady) return true;
130+
if (input.connectionState === null) return false;
131+
if (!input.shellCacheRead) return true;
132+
// Only a first attempt is worth waiting out; a reconnecting environment's
133+
// retries leave its shell synchronizing.
134+
if (input.connectionState === "connecting") return true;
135+
if (input.connectionState !== "connected") return false;
136+
return input.shellStatus === "synchronizing" || input.shellStatus === "empty";
137+
}
138+
112139
interface NavigationRoutes {
113140
readonly routes: ReadonlyArray<{
114141
readonly key: string;

0 commit comments

Comments
 (0)