fix(web): chat view no longer renders twice per update #20847
main fromrefs/pull/20847/head·merged by juliusmarminge View on GitHubProblem#
The render audit found that every ChatView render was followed by a second one. In send, stream, and typing scenarios each ChatView commit was followed by another ~61-render commit (ThreadDetails, BranchToolbar, dialogs, popovers). The only cause was ChatView#499 useSyncExternalStore arr0 -> arr0 (same content), and it happened 30 times in a 14s streaming turn.
useAutoBalanceUpdateBanner called Atom.make inside the hook. When the draft is not auto-balanced, ChatView passed a fresh [] literal, so the compiler cache always missed and a new atom was built every render:
// before, compiled
if ($[0] !== environments) {
t0 = Atom.make((get) => environments.map(...));
Each new atom gave useAtomValue a new store, and that store's snapshot was a new []. useSyncExternalStore saw a changed snapshot and scheduled another ChatView render.
Fix#
- Added
state/environmentAtomList.ts. It builds anAtom.familykeyed by the joined environment ids, which reads one per-environment atom for each id, so an equal id list always gets the same atom and snapshot.state/server.tsexportsenvironmentUpdateStatesAtomandstate/session.tsexportsenvironmentSessionStatesAtom. - ChatView now passes a module-level
EMPTY_AUTO_UPDATE_ENVIRONMENTSinstead of[]. - Other hooks and components built atoms in render the same way, and those are fixed here too:
useEnvironmentsWithScope: atom family keyed by scope and ids; it returns a stableSet, whichBrowserProfileReporteruses as an effect dependency.useSharedSettingsSyncTargetIdsuseUpdateEverythingSidebarUpdateCapsuleConnectionsSettings- Scheduled tasks, storage cleanup, and device host lists
useProjectScans- Settings-scope project files: keyed by member fields
usePaginatedBranches: keyed by target and cursorsuseReviewFilePatches: keyed by theme and patch targets
Compiled output after the fix: the atom comes from a family lookup on a string key, so the store and snapshot stay the same, and with the module-level empty list the hook's cache hits:
if ($[0] !== environments) {
t0 = environmentUpdateStatesAtom(environmentIdsKey(environments.map(_temp)));
...
const updateStates = useAtomValue(t0);
environmentAtomList.test.ts checks that an equal id list returns the same atom and the same snapshot.
What still renders#
ChatView still renders when a value it subscribes to really changes, such as the thread shell's updatedAt during streaming (a separate audit finding). The banner hook re-runs its derivations when the environments list identity changes, but the atom and its snapshot stay the same, so no follow-up commit gets scheduled.
After:
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
juliusmarminge added labelsmacroscope-review
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Team
- Run ID:
d3b1f035-ce2d-470b-9077-bf1379db25f2
📥 Commits
-
Reviewing files that changed from the base of the PR and between 2fbd4318fc5b610c3e7fcfcbd5bbd80b73521d8f and a514a2e84754e9dabb5e81b6f4185e0d2d849b3b.
📒 Files selected for processing: 17
-
apps/web/src/components/ChatView.tsxapps/web/src/components/chat/useAutoBalanceUpdateBanner.tsxapps/web/src/components/diffs/useReviewFilePatches.tsapps/web/src/components/settings/ConnectionsSettings.tsxapps/web/src/components/settings/DeviceHostsSettings.tsxapps/web/src/components/settings/ScheduledTasksSettings.tsxapps/web/src/components/settings/SettingsScopeContext.tsxapps/web/src/components/settings/StorageSettings.tsxapps/web/src/components/sidebar/SidebarUpdateCapsule.tsxapps/web/src/components/sidebar/useUpdateEverything.tsapps/web/src/hooks/useSettings.tsapps/web/src/onboarding/useProjectScans.tsapps/web/src/state/environmentAtomList.test.tsapps/web/src/state/environmentAtomList.tsapps/web/src/state/queries.tsapps/web/src/state/server.tsapps/web/src/state/session.ts
Limit details: You’ve used all 10 included reviews currently available.
-
📝 Walkthrough
-
The web app adds keyed atom-list helpers and aggregate environment state atoms. Settings, sidebar, and onboarding components use keyed atoms for environment data. Review patch parsing and branch pagination also use keyed atom families.
Changes: Web atom state and query updates
Layer / File(s) Summary Keyed environment atom foundation
apps/web/src/state/environmentAtomList.ts,apps/web/src/state/environmentAtomList.test.ts,apps/web/src/state/server.ts,apps/web/src/state/session.tsAdds helpers that key atom lists by environment IDs. Adds aggregate update and session state atoms, with tests for atom and snapshot identity and empty lists. Environment state consumers
apps/web/src/components/ChatView.tsx,apps/web/src/components/chat/useAutoBalanceUpdateBanner.tsx,apps/web/src/components/settings/ConnectionsSettings.tsx,apps/web/src/components/sidebar/SidebarUpdateCapsule.tsx,apps/web/src/components/sidebar/useUpdateEverything.ts,apps/web/src/hooks/useSettings.tsUpdate-state and session-state consumers read keyed atom values and pair results with environments by index. ChatViewuses a typed empty environment list when automatic selection is inactive.Environment query consumers
apps/web/src/components/settings/DeviceHostsSettings.tsx,apps/web/src/components/settings/ScheduledTasksSettings.tsx,apps/web/src/components/settings/StorageSettings.tsx,apps/web/src/onboarding/useProjectScans.tsDevice-host, scheduled-task, storage-cleanup, and project-scan data use keyed environment atom lists. Keyed project and review data
apps/web/src/components/settings/SettingsScopeContext.tsx,apps/web/src/components/diffs/useReviewFilePatches.tsProject-file atoms use serialized member identity tuples as keys. Review patch parsing and aggregation use atoms keyed by target data and theme. Keyed branch pagination
apps/web/src/state/queries.tsVCS ref page atoms use target-and-cursor keys. Refresh resets pagination and refreshes the first-page atom for the current target. Priority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix
Suggested reviewers:
maria-rcks
Merge Risk: ⚪ Minimal · up to
a514a-
No merge-blocking issue is established. Branch pagination resets when the target changes; the review-patch theme concern remains unverified.
Pre-merge checks:
3 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Description check The description explains the problem and implementation clearly, but it does not provide the required scope and approval information. Verification is also incomplete because post-fix results are marke… Add the triaged issue or maintainer approval details, or explain why this focused fix qualifies for an exemption. Complete verification with observed results, state any checks not performed, and include before/after screenshots or a recordi… Passed checks: 3 passed-
Check name Status Explanation Title check The title clearly identifies the primary fix: preventing duplicate ChatView renders during updates. Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request.
Full details: Description check
-
Explanation
The description explains the problem and implementation clearly, but it does not provide the required scope and approval information. Verification is also incomplete because post-fix results are marked as “to be measured,” and required UI evidence is missing.
Resolution
Add the triaged issue or maintainer approval details, or explain why this focused fix qualifies for an exemption. Complete verification with observed results, state any checks not performed, and include before/after screenshots or a recording if needed to demonstrate the UI behavior.
-
- Fix all pre-merge checks with AI
✨ Finishing Touches
-
📝 Generate docstrings
-
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Usage-based review receipt#
- Mode: Continue automatically
- Reviewed files: 17
- Charged: $4.25
- View usage details
Comment `@coderabbitai help` to get the list of available commands.Note
This review was completed with usage-based billing: files reviewed beyond your plan's included limits are billed at $0.25/file. View usage-based billing.
-
Re-measured in the app (seeded data, same scenario, median of 3):
Measured against main at 489d08a054, with this PR's commit cherry-picked onto that same commit (it applies cleanly). The server ran without
--watch, and no window had a reconnect. One PR window that caught aproviderStatusespush was dropped and replaced with a fourth run.Scenario: open a Claude Haiku thread, type 10 characters in the composer, then send a short one-line turn. The window covers typing plus the whole turn until the stop button goes away.
scenario main this PR type 10 chars + send a short Haiku turn: commits / renders 88 / 4887 85 / 4384 ChatViewrenders28 16 renders outside the timeline (chrome, composer, sidebar) 4246 3549 ChatView#499 useSyncExternalStore(13 per window on main) is gone. That is the second render after each update, and its ~60-render tail (details panel, toolbar, dialogs) goes with it. On this scenario ChatView renders drop 43%. That's not quite half, because some of the remaining ChatView renders never had a duplicate.What still renders, and it's legitimate:
ChatView state#58(8) andstate#56(5): the thread projection, as items and run state stream in.- The composer: about 1950 renders. Most are the 10 keystrokes plus the send transition.
- The timeline count is a bit higher on the PR side (836 vs 668). The PR runs went last, so the thread had more rows. Every send adds a turn.
Behaviour check: the sidebar update capsule still shows "Update 1 provider". The update-available toast still appears. Settings > Connections still lists the environment, its clients and pairing links. Turns send and complete normally.
juliusmarminge merged this52d593c
juliusmarminge closed this
Sign in to join this conversation.
Approvability#
Verdict: Needs Human Approval
Macroscope's review found this PR requires human approval — The change extends beyond the ChatView render fix into a cross-cutting atom identity and caching refactor spanning settings, onboarding, update flows, source-control queries, and diff rendering. Because it changes runtime subscription and refresh behavior across 17 production files, the broader impact should receive human review.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).