Skip to content

[Bug]: Mobile thread screen re-renders wholesale on every connection and sync change #40791

Open

Description

@danielphan2003

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/mobile

Steps to reproduce

  1. Open a thread whose agent is producing tool calls (for example "make 200 separate shell calls, each echo step N; sleep 2").
  2. Background the app for 45 s, then return.
  3. Log renders of ThreadRouteContent, ThreadDetailBody, ThreadFeed, ThreadComposer and AssistantMarkdownContent (or profile the resume).

Expected behavior

Connection-state, sync-status and unrelated store changes re-render only what displays them. Catch-up events re-render the feed rows that changed.

Actual behavior

Every render of the route rebuilds every prop of the thread screen, so the whole screen re-renders on each connection, sync and status change. During one resume, every visible assistant message re-rendered with identical props on each connection change: 378 AssistantMarkdownContent renders in a 45 s capture.

The React Compiler output shows why. Its memo scopes are dropped (pruned) in these places:

  • useThreadComposerState: derived values are computed between hook calls (useAtomValue, useNowMinuteMs, useComposerAttachmentActions, ...). Values such as (map[key] ?? []).filter(...) are then passed on to other calls, so their scopes span hooks and are pruned. selectedThreadFeed, selectedThreadActivityRun, onSendMessage and the returned object are rebuilt on every render, even when the detail is unchanged.
  • ThreadRouteContent:
    • Option.getOrNull(selectedThreadDetailState.data) produces a value the compiler treats as mutable. Its later method calls (.messages.find, resolveMergeBackTargetThreadId(...)) merge most of the component into one scope that spans hooks, which gets pruned.
    • renderThreadRouteBody() is a local function called during render, which keeps everything it captures unmemoized.
    • contentPresentation is passed on to threadSyncStatusLabel after a useDelayedStatus call.
  • ThreadDetailBody:
    • scopedThreadKey(...) (an opaque call) has the same effect for every callback that captures selectedThreadKey, including onJumpedToMessage and onFreshSession.
    • props.onSendMessage(...) and props.onChangeDraftMessage(...) make handleSendMessage and handleUseArtifactTemplate depend on all of props.
  • useLinkedEnvironments(null): called by every message without thread links, it still subscribes to the whole environment presentation map through useEnvironments(), so every connection change re-renders all visible markdown.

Fixes that restore memoization, with behavior unchanged:

  • Call hooks before deriving values in useThreadComposerState.
  • Read Option.isSome(x) ? x.value : null instead of calling getOrNull.
  • Inline the body JSX.
  • Move the one hook above contentPresentation.
  • Pass selectedThreadKey as a prop.
  • Destructure the two prop callbacks.
  • In useLinkedEnvironments, read presentations from an empty atom when environmentId is null.

I checked each one against [email protected] output. With render tracing on, route renders stop cascading into the thread screen, and assistant messages render only on mount.

Two GT costs ran on every one of those renders. LocaleConfig.requiresTranslation validates both locales through Intl on each call. gt-i18n's hashMessage computes SHA-256 per msg() call and per lookup. Together they took ~350 ms per resume in a production-mode Hermes profile. A cache on each (pnpm patches) removed them, and JS work from foreground to thread live dropped from 2238 ms to 1589 to 1717 ms.

Impact

Major degradation or frequent failure

Version or commit

Files as of c3a330d (ThreadRouteScreen.tsx, ThreadDetailScreen.tsx, use-thread-composer-state.ts, state/peerLinks.ts)

Environment

Android 17 emulator (production and dev bundles) and Pixel 9 Pro (release build)

Logs or stack traces

Render trace excerpt, before (each line is one render, with the props that changed):

RouteContent ["composer"]          composer ["selectedThreadFeed","selectedThreadActivityRun","onSendMessage", ...]   detailState []
DetailBody.props ["header","contentPresentation","screenTone","selectedThreadFeed","onSendMessage","onReconnectEnvironment"]
AMC [] (x25 on each connection change)

Upstream

The two GT costs are reported as generaltranslation/gt#2992.

Activity

  1. joei4cm commented on Nov 22, 2026

    @joei4cm
    Contributor

    Taking this — the app-side memoization set from the list above: hook order in useThreadComposerState, the Option.getOrNull / renderThreadRouteBody / contentPresentation scopes in ThreadRouteScreen, the prop captures in ThreadDetailBody, and useLinkedEnvironments(null) reading an empty atom. All behavior-preserving, as analyzed.

    Out of scope here: the two GT i18n costs. Those are generaltranslation/gt#2992 and would be a dependency patch rather than an app fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions