@devries-cameron20 / t3code-1 / commits / 2b77a05cdb
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): new-task context no longer changes every render (#22134)

Co-authored-by: maria-rcks <maria@kuuro.net>

oliver committed Oct 30, 2026 at 13:37 UTC 2b77a05cdb7a8fc11767219638f60c65bcc707f5
4 files changed +196 -84
apps/mobile/src/features/threads/new-task-flow-provider.tsx
+63 -63
@@ -327,72 +327,13 @@ export function NewTaskFlowProvider(
327 null);
328
329 const linkedEnvironments = useLinkedEnvironments(selectedProject?.environmentId ?? null);
330 + const storedStickyModelSelection = useStickyComposerModelSelection();
331 + const modelSelectionNow = useNowMinuteMs();
332 const projectDefaults = useNewTaskProjectDefaults(selectedProject);
333 const selectedEnvironmentServerConfig = projectDefaults.serverConfig;
334 const isScratchDraft = projectDefaults.isScratch;
335 const serverConfigs = useServerConfigs();
336 const { connectedEnvironments } = useRemoteConnectionStatus();
335 - const scratchEnvironments = connectedEnvironments.filter(
336 - (environment) =>
337 - availableScratchWorkspaceRoot(
338 - environment.connectionState,
339 - serverConfigs.get(environment.environmentId),
340 - ) !== null,
341 - );
342 -
343 - const selectedRepositoryKey = selectedProject?.repositoryIdentity
344 - ? repositoryGroupingKeyOf(selectedProject.repositoryIdentity)
345 - : null;
346 - // Use || null: placeholder workspaceRoot can be empty, and an empty basename rejects real hosts.
347 - const selectedWorkspaceBasename = selectedProject?.workspaceRoot.split("/").at(-1) || null;
348 - const selectedProjectTitle = selectedProject?.title ?? null;
349 - const environments = (() => {
350 - if (isScratchDraft) {
351 - return scratchEnvironments.map((environment) => ({
352 - environmentId: environment.environmentId,
353 - environmentLabel: environment.environmentLabel,
354 - }));
355 - }
356 - const seen = new Set<EnvironmentId>();
357 - const result: Array<{
358 - readonly environmentId: EnvironmentId;
359 - readonly environmentLabel: string;
360 - }> = [];
361 - const hostsSelectedRepository = (project: EnvironmentProject) => {
362 - if (selectedRepositoryKey === null && selectedWorkspaceBasename === null) {
363 - return true;
364 - }
365 - const projectKey = project.repositoryIdentity
366 - ? repositoryGroupingKeyOf(project.repositoryIdentity)
367 - : null;
368 - if (selectedRepositoryKey !== null && projectKey !== null) {
369 - return projectKey === selectedRepositoryKey;
370 - }
371 - return (
372 - project.workspaceRoot.split("/").at(-1) === selectedWorkspaceBasename ||
373 - (selectedProjectTitle !== null && project.title === selectedProjectTitle)
374 - );
375 - };
376 - for (const project of projects) {
377 - if (!hostsSelectedRepository(project)) {
378 - continue;
379 - }
380 - if (seen.has(project.environmentId)) {
381 - continue;
382 - }
383 - const environment = savedConnectionsById[project.environmentId];
384 - if (!environment) {
385 - continue;
386 - }
387 - seen.add(project.environmentId);
388 - result.push({
389 - environmentId: project.environmentId,
390 - environmentLabel: environment.environmentLabel,
391 - });
392 - }
393 - return result;
394 - })();
395 -
337 const selectedProjectDraftKey = selectedProject ? activeDraftKey : null;
338 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- creates the persistent draft in the new-task flow store once a project is selected, before the composer mounts
339 useEffect(() => {
@@ -485,13 +426,11 @@ export function NewTaskFlowProvider(
426 selectedProjectDraft.modelSelection ?? null,
427 selectedProject?.id ?? null,
428 );
488 - const modelSelectionNow = useNowMinuteMs();
429 const projectDefaultModelSelection = resolveDefaultableModelSelection(
430 selectedEnvironmentServerConfig,
431 projectSettings.settings.defaultModelSelection,
432 selectedProject?.id ?? null,
433 );
494 - const storedStickyModelSelection = useStickyComposerModelSelection();
434 const stickyModelSelection = resolveDefaultableModelSelection(
435 selectedEnvironmentServerConfig,
436 storedStickyModelSelection,
@@ -842,6 +781,67 @@ export function NewTaskFlowProvider(
781 };
782 };
783
784 + const scratchEnvironments = connectedEnvironments.filter(
785 + (environment) =>
786 + availableScratchWorkspaceRoot(
787 + environment.connectionState,
788 + serverConfigs.get(environment.environmentId),
789 + ) !== null,
790 + );
791 +
792 + const selectedRepositoryKey = selectedProject?.repositoryIdentity
793 + ? repositoryGroupingKeyOf(selectedProject.repositoryIdentity)
794 + : null;
795 + // Use || null: placeholder workspaceRoot can be empty, and an empty basename rejects real hosts.
796 + const selectedWorkspaceBasename = selectedProject?.workspaceRoot.split("/").at(-1) || null;
797 + const selectedProjectTitle = selectedProject?.title ?? null;
798 + const environments = (() => {
799 + if (isScratchDraft) {
800 + return scratchEnvironments.map((environment) => ({
801 + environmentId: environment.environmentId,
802 + environmentLabel: environment.environmentLabel,
803 + }));
804 + }
805 + const seen = new Set<EnvironmentId>();
806 + const result: Array<{
807 + readonly environmentId: EnvironmentId;
808 + readonly environmentLabel: string;
809 + }> = [];
810 + const hostsSelectedRepository = (project: EnvironmentProject) => {
811 + if (selectedRepositoryKey === null && selectedWorkspaceBasename === null) {
812 + return true;
813 + }
814 + const projectKey = project.repositoryIdentity
815 + ? repositoryGroupingKeyOf(project.repositoryIdentity)
816 + : null;
817 + if (selectedRepositoryKey !== null && projectKey !== null) {
818 + return projectKey === selectedRepositoryKey;
819 + }
820 + return (
821 + project.workspaceRoot.split("/").at(-1) === selectedWorkspaceBasename ||
822 + (selectedProjectTitle !== null && project.title === selectedProjectTitle)
823 + );
824 + };
825 + for (const project of projects) {
826 + if (!hostsSelectedRepository(project)) {
827 + continue;
828 + }
829 + if (seen.has(project.environmentId)) {
830 + continue;
831 + }
832 + const environment = savedConnectionsById[project.environmentId];
833 + if (!environment) {
834 + continue;
835 + }
836 + seen.add(project.environmentId);
837 + result.push({
838 + environmentId: project.environmentId,
839 + environmentLabel: environment.environmentLabel,
840 + });
841 + }
842 + return result;
843 + })();
844 +
845 const value = {
846 activeNewTask,
847 projectScopes,
apps/mobile/src/reactCompilerCoverage.test.ts
+8
@@ -182,6 +182,14 @@ describe("React Compiler coverage", () => {
182 ).toEqual([true]);
183 });
184
185 + // The compiler once silently dropped these caches, so the new-task context
186 + // value changed on every provider render and re-rendered every consumer.
187 + it("caches the new-task model options and host environments", () => {
188 + const file = "src/features/threads/new-task-flow-provider.tsx";
189 + expect(compilerCachedCalls(file, "buildModelOptions")).toEqual([true]);
190 + expect(compilerCachedCalls(file, "repositoryGroupingKeyOf")).toEqual([true, true]);
191 + });
192 +
193 // An uncached registration would re-register the workspace inspector on every render.
194 it("registers the workspace inspector only when its renderer changes", () => {
195 expect(
apps/mobile/src/state/queries.debounce.test.tsx
+101 -4
@@ -1,11 +1,16 @@
1 import { AuthFilesystemReadScope, EnvironmentId } from "@t3tools/contracts";
2 -import { act } from "react";
2 +import { RegistryContext } from "@effect/atom-react";
3 +import type { VcsRefTarget } from "@t3tools/client-runtime/state/vcs";
4 +import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
5 +import { act, useLayoutEffect } from "react";
6 import { createRoot } from "react-dom/client";
7 import { expect, it, vi } from "vite-plus/test";
8
9 const mocks = vi.hoisted(() => ({
10 sessionAtom: {},
11 searchEntries: vi.fn(() => null),
12 + listRefs: vi.fn(),
13 + refresh: vi.fn(),
14 }));
15
16 vi.mock("./session", () => ({
@@ -21,9 +26,9 @@ vi.mock("./pull-requests", () => ({ composerPullRequests: {} }));
26 vi.mock("./projects", () => ({
27 projectEnvironment: { searchEntries: mocks.searchEntries },
28 }));
24 -vi.mock("./atom-registry", () => ({ appAtomRegistry: {} }));
29 +vi.mock("./atom-registry", () => ({ appAtomRegistry: { refresh: mocks.refresh } }));
30 vi.mock("./orchestration", () => ({ orchestrationEnvironment: {} }));
26 -vi.mock("./vcs", () => ({ vcsEnvironment: {} }));
31 +vi.mock("./vcs", () => ({ vcsEnvironment: { listRefs: mocks.listRefs } }));
32 vi.mock("./threads", () => ({ olderSettledThreads: {}, threadFamilies: {} }));
33 vi.mock("./query", () => ({
34 useEnvironmentQuery: (atom: unknown) => ({
@@ -37,7 +42,11 @@ vi.mock("./query", () => ({
42 }),
43 }));
44
40 -import { useComposerPathSearch, type ComposerPathSearchTarget } from "./queries";
45 +import {
46 + useComposerPathSearch,
47 + usePaginatedBranches,
48 + type ComposerPathSearchTarget,
49 +} from "./queries";
50
51 function setup() {
52 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -104,3 +113,91 @@ it.each([
113 input: { cwd: changed.cwd, query: changed.query, limit: 20 },
114 });
115 });
116 +
117 +it("keeps compiled branch results stable and resets pagination for a new target or refresh", () => {
118 + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
119 + const registry = AtomRegistry.make();
120 + const pages = Atom.family((key: string) => {
121 + const [, cwd, cursor] = JSON.parse(key) as [string, string, number | null];
122 + return Atom.make(
123 + AsyncResult.success({
124 + refs: [
125 + { name: `${cwd}-${cursor ?? 0}`, current: false, isDefault: false, worktreePath: null },
126 + ],
127 + isRepo: true,
128 + hasPrimaryRemote: true,
129 + nextCursor: cursor === null ? 100 : null,
130 + totalCount: 2,
131 + }),
132 + );
133 + });
134 + mocks.listRefs.mockImplementation(
135 + ({
136 + environmentId,
137 + input,
138 + }: {
139 + environmentId: EnvironmentId;
140 + input: { cwd: string; cursor?: number };
141 + }) => pages(JSON.stringify([environmentId, input.cwd, input.cursor ?? null])),
142 + );
143 + mocks.refresh.mockClear();
144 + const root = createRoot(document.createElement("div"));
145 + using _cleanup = {
146 + [Symbol.dispose]() {
147 + act(() => root.unmount());
148 + registry.dispose();
149 + },
150 + };
151 + let current: ReturnType<typeof usePaginatedBranches>;
152 + function Probe({ target }: { target: VcsRefTarget }) {
153 + const result = usePaginatedBranches(target);
154 + useLayoutEffect(() => {
155 + current = result;
156 + });
157 + return null;
158 + }
159 + const render = (cwd: string | null, options: Partial<VcsRefTarget> = {}) =>
160 + act(() =>
161 + root.render(
162 + <RegistryContext.Provider value={registry}>
163 + <Probe target={{ environmentId: target.environmentId, cwd, ...options }} />
164 + </RegistryContext.Provider>,
165 + ),
166 + );
167 + render("/repo");
168 + const first = current!;
169 + render("/repo");
170 + expect(current!.data).toBe(first.data);
171 + expect(current!.refs).toBe(first.refs);
172 + expect(current!.refresh).toBe(first.refresh);
173 + expect(current!.loadNext).toBe(first.loadNext);
174 + act(() =>
175 + registry.set(
176 + pages(JSON.stringify([target.environmentId, "/repo", null])),
177 + AsyncResult.success({
178 + ...first.data!,
179 + refs: [{ name: "/repo-0", current: true, isDefault: false, worktreePath: null }],
180 + }),
181 + ),
182 + );
183 + expect(current!.refs[0]?.current).toBe(true);
184 + act(() => current!.loadNext());
185 + expect(current!.refs.map((ref) => ref.name)).toEqual(["/repo-0", "/repo-100"]);
186 + act(() => current!.refresh());
187 + expect(current!.refs.map((ref) => ref.name)).toEqual(["/repo-0"]);
188 + expect(mocks.refresh).toHaveBeenCalledExactlyOnceWith(
189 + pages(JSON.stringify([target.environmentId, "/repo", null])),
190 + );
191 + act(() => current!.loadNext());
192 + render("/other");
193 + expect(current!.refs.map((ref) => ref.name)).toEqual(["/other-0"]);
194 + act(() => current!.loadNext());
195 + render("/other", { query: "feature" });
196 + expect(current!.refs).toHaveLength(1);
197 + act(() => current!.loadNext());
198 + render("/other", { environmentId: EnvironmentId.make("other-environment"), query: "feature" });
199 + expect(current!.refs).toHaveLength(1);
200 + render(null);
201 + expect(current!.data).toBeNull();
202 + expect(current!.refs).toEqual([]);
203 +});
apps/mobile/src/state/queries.ts
+24 -17
@@ -31,7 +31,7 @@ import * as Cause from "effect/Cause";
31 import { turnItemDetailRevision } from "@t3tools/client-runtime/work-log/item-detail";
32 import * as Option from "effect/Option";
33 import { AsyncResult, Atom } from "effect/reactivity";
34 -import { useEffect, useState } from "react";
34 +import { useEffect, useMemo, useState } from "react";
35
36 import { appAtomRegistry } from "./atom-registry";
37 import { orchestrationEnvironment } from "./orchestration";
@@ -299,22 +299,29 @@ export function usePaginatedBranches(target: VcsRefTarget) {
299 cursors: INITIAL_BRANCH_CURSORS,
300 });
301 const cursors = pagination.targetKey === targetKey ? pagination.cursors : INITIAL_BRANCH_CURSORS;
302 - const pageAtoms =
303 - target.environmentId !== null && target.cwd !== null
304 - ? cursors.map((cursor) =>
305 - vcsEnvironment.listRefs({
306 - environmentId: target.environmentId!,
307 - input: {
308 - cwd: target.cwd!,
309 - ...(query.length > 0 ? { query } : {}),
310 - ...(cursor === undefined ? {} : { cursor }),
311 - limit: VCS_REF_LIST_LIMIT,
312 - },
313 - }),
314 - )
315 - : [];
316 - const pagesAtom = Atom.make((get) => pageAtoms.map((atom) => get(atom))).pipe(
317 - Atom.withLabel(`mobile:vcs-ref-pages:${targetKey ?? "empty"}`),
302 + const pageAtoms = useMemo(
303 + () =>
304 + target.environmentId !== null && target.cwd !== null
305 + ? cursors.map((cursor) =>
306 + vcsEnvironment.listRefs({
307 + environmentId: target.environmentId!,
308 + input: {
309 + cwd: target.cwd!,
310 + ...(query.length > 0 ? { query } : {}),
311 + ...(cursor === undefined ? {} : { cursor }),
312 + limit: VCS_REF_LIST_LIMIT,
313 + },
314 + }),
315 + )
316 + : [],
317 + [target.environmentId, target.cwd, query, cursors],
318 + );
319 + const pagesAtom = useMemo(
320 + () =>
321 + Atom.make((get) => pageAtoms.map((atom) => get(atom))).pipe(
322 + Atom.withLabel(`mobile:vcs-ref-pages:${targetKey ?? "empty"}`),
323 + ),
324 + [pageAtoms, targetKey],
325 );
326 const results = useAtomValue(pagesAtom);
327 const values = results.flatMap((result) => {