@devries-cameron20 / t3code-1 / commits / 71b867080b
Fork 0

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

fix(mobile): chat feed no longer re-derives on every render (#22913)

Co-authored-by: flamboh <oliver.boorstein@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 21, 2026 at 14:08 UTC 71b867080b1dc5853b53417efe15b2443f33f904
2 files changed +85 -21
apps/mobile/src/features/threads/ThreadFeed.tsx
+28 -18
@@ -1875,6 +1875,33 @@ function ThreadFeedPlaceholder(props: {
1875 );
1876 }
1877
1878 +// A hook's return value is frozen to the compiler, so ThreadFeed's later
1879 +// hooks can't drag this derivation into an uncached scope.
1880 +function useThreadFeedPresentation(
1881 + props: ThreadFeedProps,
1882 + expandedTurnIds: ReadonlySet<RunId>,
1883 + expandedWorkGroups: Record<string, boolean>,
1884 +) {
1885 + const [feedCache] = useState(createThreadFeedCache);
1886 + return appendPendingThreadMessages(
1887 + deriveThreadFeedPresentation(
1888 + props.feed,
1889 + props.latestRun,
1890 + expandedTurnIds,
1891 + new Set(
1892 + Object.entries(expandedWorkGroups)
1893 + .filter(([, expanded]) => expanded)
1894 + .map(([groupId]) => groupId),
1895 + ),
1896 + props.activeWorkStartedAt,
1897 + props.runlessWorkActive ?? false,
1898 + feedCache,
1899 + ),
1900 + props.feed,
1901 + props.queuedMessages,
1902 + );
1903 +}
1904 +
1905 export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
1906 const { listRef, onEndFollowEnabledChange, onHeaderMaterialVisibilityChange } = props;
1907 const workflowOpening = useWorkflowOpening(props.environmentId, props.threadId);
@@ -1900,7 +1927,6 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
1927 const disclosureSettleFrameRef = useRef<number | null>(null);
1928 const disclosureSettleSecondFrameRef = useRef<number | null>(null);
1929 const disclosureAnchorKeyRef = useRef<string | null>(null);
1903 - const [feedCache] = useState(createThreadFeedCache);
1930 const previousLatestTurnRef = useRef(props.latestRun);
1931 const userScrollSettleTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
1932 const headerMaterialVisibleRef = useRef(false);
@@ -2456,23 +2482,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
2482 reportHeaderMaterialVisibility(false);
2483 }, [feedThreadKey, reportHeaderMaterialVisibility]);
2484
2459 - const presentedFeed = appendPendingThreadMessages(
2460 - deriveThreadFeedPresentation(
2461 - props.feed,
2462 - props.latestRun,
2463 - expandedTurnIds,
2464 - new Set(
2465 - Object.entries(expandedWorkGroups)
2466 - .filter(([, expanded]) => expanded)
2467 - .map(([groupId]) => groupId),
2468 - ),
2469 - props.activeWorkStartedAt,
2470 - props.runlessWorkActive ?? false,
2471 - feedCache,
2472 - ),
2473 - props.feed,
2474 - props.queuedMessages,
2475 - );
2485 + const presentedFeed = useThreadFeedPresentation(props, expandedTurnIds, expandedWorkGroups);
2486 const setupAnchorIndex = presentedFeed.findIndex(
2487 (entry) => entry.type === "message" && entry.message.role === "user",
2488 );
apps/mobile/src/reactCompilerCoverage.test.ts
+57 -3
@@ -66,8 +66,7 @@ interface CompilerEvent {
66
67 // babel.config.js is the file Metro loads; the logger rides on the preset's
68 // own react-compiler option so the plugin runs with Metro's exact settings.
69 -function compilerBailouts(file: string): string[] {
70 - const events: CompilerEvent[] = [];
69 +function compileLikeMetro(file: string, events: CompilerEvent[], output: Record<string, unknown>) {
70 const babelConfig = requireFromMobile("./babel.config.js")({ cache: () => {} });
71 const presets = babelConfig.presets.map(([name, options]: [string, object]) =>
72 name === "babel-preset-expo"
@@ -82,14 +81,20 @@ function compilerBailouts(file: string): string[] {
81 ]
82 : [name, options],
83 );
85 - babel.transformFileSync(NodePath.join(mobileRoot, file), {
84 + return babel.transformFileSync(NodePath.join(mobileRoot, file), {
85 babelrc: false,
86 configFile: false,
87 cwd: mobileRoot,
88 presets,
89 caller: metroCaller,
90 code: false,
91 + ...output,
92 });
93 +}
94 +
95 +function compilerBailouts(file: string): string[] {
96 + const events: CompilerEvent[] = [];
97 + compileLikeMetro(file, events, {});
98 return events
99 .filter((event) => event.kind === "CompileError" || event.kind === "CompileSkip")
100 .map(
@@ -98,6 +103,47 @@ function compilerBailouts(file: string): string[] {
103 );
104 }
105
106 +interface AstNode {
107 + readonly type: string;
108 + readonly [key: string]: unknown;
109 +}
110 +
111 +const isAstNode = (value: unknown): value is AstNode =>
112 + typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
113 +
114 +function walkAst(
115 + value: unknown,
116 + visit: (node: AstNode, ancestors: readonly AstNode[]) => void,
117 + ancestors: readonly AstNode[] = [],
118 +) {
119 + if (typeof value !== "object" || value === null) return;
120 + const node = isAstNode(value) ? value : null;
121 + if (node) visit(node, ancestors);
122 + const childAncestors = node ? [...ancestors, node] : ancestors;
123 + for (const child of Object.values(value)) walkAst(child, visit, childAncestors);
124 +}
125 +
126 +// For each call to `callee`, whether it runs only on the cache-miss branch of
127 +// an `if ($[n] !== dep)` block rather than on every render.
128 +function compilerCachedCalls(file: string, callee: string): boolean[] {
129 + const result = compileLikeMetro(file, [], { ast: true });
130 + const cached: boolean[] = [];
131 + if (typeof result !== "object" || result === null || !("ast" in result)) return cached;
132 + walkAst(result.ast, (node, ancestors) => {
133 + if (node.type !== "CallExpression" || !isAstNode(node.callee)) return;
134 + if (node.callee.type !== "Identifier" || node.callee.name !== callee) return;
135 + cached.push(
136 + ancestors.some(
137 + (ancestor, index) =>
138 + ancestor.type === "IfStatement" &&
139 + ancestors[index + 1] === ancestor.consequent &&
140 + JSON.stringify(ancestor.test).includes('"name":"$"'),
141 + ),
142 + );
143 + });
144 + return cached;
145 +}
146 +
147 describe("React Compiler coverage", () => {
148 it("Metro's transformer applies the compiler", async () => {
149 const { transformer } = await metroConfig;
@@ -137,6 +183,14 @@ describe("React Compiler coverage", () => {
183 }
184 });
185
186 + // The compiler once silently dropped this cache, so every
187 + // ThreadFeed render rebuilt the feed and handed LegendList a new data array.
188 + it("caches the thread feed derivation", () => {
189 + expect(
190 + compilerCachedCalls("src/features/threads/ThreadFeed.tsx", "deriveThreadFeedPresentation"),
191 + ).toEqual([true]);
192 + });
193 +
194 it("lists only files that exist", () => {
195 expect(Object.keys(KNOWN_BAILOUTS).filter((file) => !sourceFiles.includes(file))).toEqual([]);
196 });