Skip to content

fix(Chat): latest-value refs are written at commit - #10162

Merged
cixzhang merged 1 commit into
meta:mainfrom
vjeux:fix/chat-new-messages-committed-refs
Nov 6, 2026
Merged

cixzhang merged 1 commit into
meta:mainfrom
vjeux:fix/chat-new-messages-committed-refs

Conversation

@vjeux

@vjeux vjeux commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

User impact

useChatNewMessages (which ChatLayout uses), useSpeechRecognition and useChatDictation could act on values from a render React threw away, such as a suspended transition's. The next resize or recognition event then used that render's callbacks or audio context. After such a render, useChatNewMessages could also flag new messages while the list followed the bottom.

Problem and solution fit

  • Why it occurs: each hook copied its latest values into refs during render. React can discard a render, and the refs keep its values.
  • Why it harms the task: the observer and recognition callbacks read those refs, so they use values no committed render passed.
  • Why it matters: transitions and Suspense discard renders by design.

Each hook now writes them at commit, which only committed renders reach.

Expected behavior and authority

  • useChatNewMessages' documented option: "When locked, new messages don't flag".
  • architecture:react-component-runtime INV4: ref-backed coordination must preserve the current API contract.
  • Specs decision, Nov 5 2026:
    • "No ref writes during render except lazy one-time init."
    • "the latest-value ref is written in a named layout effect and read only in event handlers"
    • It allows "useInsertionEffect where it must run before layout effects".

Smallest restoration

The recognition hooks, read only by recognition events, write in one useIsomorphicLayoutEffect, as usePanGesture does. useChatNewMessages writes in one useInsertionEffect. Its observer's first, synchronous call comes as React attaches the content ref, before any layout Effect. A layout Effect would leave that call the previous commit's values. An insertion Effect runs only at commit, before refs attach. ComplexSelector and BaseTypeahead do the same. There is no API or visual change.

ChatLayout's other parts write no ref during render. ChatComposerInput follows separately; open pull requests change it.

Evidence

  • Reproduction before the change: the three discarded-render cases fail on main without the fix (312e311, on this fork's red-arm/chat-latest-value-refs).
  • Content swap: with a layout Effect, a commit that swapped the content element and locked the list ran the previous listener (6b214f4, on red-arm/chat-content-swap). The insertion Effect passes.
  • Result after the change: all four cases pass, using only committed values.
  • Representative unchanged path: the existing cases still pass.
  • Regression tests: these four cases, in useChatNewMessages.test.tsx and useChatDictation.test.tsx. Each fails if a value it covers is written during render or after attach.

Scope

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

Local gates at fb516c0, one commit on main 4789178.

  • npx vitest run --project ui packages/core/src/Chat/: 440 passed. The red arms above fail without the fix.
  • The full Node project (16,769 passed) and UI project (21,300 passed).
  • Every Chat browser spec against a Storybook built and stamped at this head: the accessibility contracts in Chromium (36 passed) and WebKit (3 passed), and the interaction spec in Chromium and WebKit.
  • tsc --project packages/core/tsconfig.json --noEmit, ESLint, Prettier and changeset coverage: clean.
  • pnpm review:pre: no findings.

@vercel

vercel Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 5, 2026 10:07pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 5, 2026
@astracat-bot

astracat-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 5, 2026, 10:14 PM UTC

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · fb516c0

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
ChatLayout 53.8 kB (+3 B) · 9.3 kB CSS (0) — ✗ · ✓ · —
ChatComposer 67.1 kB (0) · 10.6 kB CSS (0) — ✗ · ✓ · —
ChatComposerDrawer 17.7 kB (0) · 4.7 kB CSS (0) — ✗ · ✓ · —
15 more components
Component Size LoC Visual · A11y · RTL
ChatComposerInput 65.0 kB (0) · 10.1 kB CSS (0) — ✗ · ✓ · —
ChatComposerTokenElement 65.0 kB (0) · 10.1 kB CSS (0) — ✗ · ✓ · —
ChatDictationButton 51.1 kB (0) · 8.8 kB CSS (0) — ✗ · ✓ · —
ChatLayoutScrollButton 51.1 kB (0) · 8.9 kB CSS (0) — ✗ · ✓ · —
ChatMessage 16.8 kB (0) · 3.7 kB CSS (0) — ✗ · ✓ · ✓
ChatMessageBubble 7.2 kB (0) · 3.7 kB CSS (0) — ✗ · ✓ · ✓
ChatMessageList 23.0 kB (0) · 6.9 kB CSS (0) — ✗ · ✓ · —
ChatMessageMetadata 19.9 kB (0) · 4.1 kB CSS (0) — ✗ · ✓ · —
ChatSendButton 51.0 kB (0) · 8.7 kB CSS (0) — ✗ · ✓ · —
ChatSystemMessage 7.0 kB (0) · 3.8 kB CSS (0) — ✗ · ✓ · —
ChatTokenizedText 7.2 kB (0) · 4.0 kB CSS (0) — ✗ · ✓ · —
ChatToolCalls 26.1 kB (0) · 8.3 kB CSS (0) — ✗ · ✓ · —
Chat — — ✗ · ✓ · —
lab/Comments 279.2 kB (−2 B) · 2.5 kB CSS (0) — — · — · —
chat — — — · ✓ · —
Visual changes: 4 snapshots in 1 component

A change is a question for the reviewer, not a failure.

Component · story Before After Diff
Chat · Default (neutral, dark) before frame after frame diff frame
Chat · Default (neutral, light) before frame after frame diff frame
Chat · Clipping (neutral, dark) before frame after frame diff frame
Chat · Clipping (neutral, light) before frame after frame diff frame

Package size: @astryxdesign/core 6.62 MB packed (+521 B); no new dependencies.

API changes: none in this pull request

Committed API report behind this build: cli 19 rows, core 23 rows; run pnpm api:report.

Touched components checked; add ci:run-visual, ci:run-a11y or ci:run-rtl to run the full suites.


Generated by PR Enrichment workflow | View full report

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested: one fix is needed before this lands.

  • Writing the refs in a layout Effect (useChatNewMessages.ts:77-80) runs after the observer's first call on attach, because observeResize fires inside the callback ref, so a commit that mounts or swaps the content element and also changes isLocked or onResize uses the previous commit's values (the old listener runs, and going from unlocked to locked still flags new messages); writing both refs in useInsertionEffect, which runs only at commit and before refs attach, fixes it, with a test for that case.

The discarded-render fix itself holds: the new case fails against the previous hook and passes here. ChatLayout is not affected by the attach-path issue, because its list registers the content element from a passive Effect. CI has not run on this head yet.

[Automated review]

Comment on lines +77 to +80
useIsomorphicLayoutEffect(() => {
isLockedRef.current = isLocked;
onResizeRef.current = onResize;
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing the refs here runs after the observer's first call on attach. observeResize fires at registration inside contentRef (lines 87-100; sharedResizeObserver.ts:89-92), and React attaches that ref before this layout Effect runs. So a commit that mounts or swaps the content element and also changes isLocked or onResize uses the previous commit's values: the old listener runs, and going from unlocked to locked still flags new messages.

An insertion Effect runs only at commit and before refs attach, as ComplexSelector relies on:

useInsertionEffect(() => {
  isLockedRef.current = isLocked;
  onResizeRef.current = onResize;
});

Add a test that unlocks, then swaps the element and locks in one commit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 988600c: both refs are now written in a useInsertionEffect, which runs at commit and before refs attach, so the observer's first call on attach reads that commit's lock and listener. The new case "reads the lock and onResize of the commit that attaches a new content element" unlocks, then swaps the element and locks in one commit. It fails with the layout Effect (6b214f4) and passes here.

@vjeux vjeux changed the title fix(Chat): useChatNewMessages writes its latest-value refs at commit fix(Chat): latest-value refs are written at commit Nov 5, 2026
@vjeux
vjeux force-pushed the fix/chat-new-messages-committed-refs branch 2 times, most recently from 988600c to 22909de Compare November 5, 2026 21:39

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested: one wording fix is needed before this lands.

  • The changeset's last sentence says useChatNewMessages "no longer flags new messages while the list follows the bottom", but on main that only happens after a render React discards, so scope it to that case, for example "After such a render, useChatNewMessages also no longer flags new messages while the list follows the bottom." (the description's User impact line needs the same qualifier).

The code fix holds: each new case fails against the previous hooks and passes here, and the insertion Effect gives the observer's first call on attach that commit's lock and listener. CI has not run on this head yet.

[Automated review]

'@astryxdesign/core': patch
---

[fix] `useChatNewMessages` (which `ChatLayout` uses), `useSpeechRecognition` and `useChatDictation` read their callbacks and other latest values as last committed. A render that React discards, as a suspended transition does, no longer has its callbacks called on the next resize or recognition event. Its audio context is no longer used either. `useChatNewMessages` also no longer flags new messages while the list follows the bottom.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On main the hook flags new messages while locked only after a render React discards; with no such render it reads the committed isLocked. As written, the last sentence reads as a general lock bug. Scope it to that render:

Suggested change
[fix] `useChatNewMessages` (which `ChatLayout` uses), `useSpeechRecognition` and `useChatDictation` read their callbacks and other latest values as last committed. A render that React discards, as a suspended transition does, no longer has its callbacks called on the next resize or recognition event. Its audio context is no longer used either. `useChatNewMessages` also no longer flags new messages while the list follows the bottom.
[fix] `useChatNewMessages` (which `ChatLayout` uses), `useSpeechRecognition` and `useChatDictation` read their callbacks and other latest values as last committed. A render that React discards, as a suspended transition does, no longer has its callbacks called on the next resize or recognition event. Its audio context is no longer used either. After such a render, `useChatNewMessages` also no longer flags new messages while the list follows the bottom.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fb516c0: the changeset now takes this sentence as suggested ("After such a render, useChatNewMessages also no longer flags new messages while the list follows the bottom."), and the description's User impact line carries the same qualifier.

useChatNewMessages, useSpeechRecognition and useChatDictation wrote
their latest callbacks, lock and audio context into refs during
render. A render React discards, as a suspended transition does, left
those values behind: the next resize or recognition event called the
discarded callbacks or used the discarded audio context, and an
unlocked value that never committed flagged new messages while the
list followed the bottom.

The recognition hooks now write their refs in one layout effect, as
usePanGesture does. useChatNewMessages writes them in an insertion
effect, as ComplexSelector does: its observer first calls back as the
content element attaches, before any layout effect.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending CI: CI has not run on this head yet.

The changeset now limits the new-message flag to the render React discards, and the insertion Effect keeps the observer's first call on attach on that commit's lock and listener. Each new case fails against the previous hooks and passes here.

Next step: CI skips drafts and fork pull requests without the ci:run label, so once this is marked ready for review, a maintainer adds ci:run so CI runs.

[Automated review]

@vjeux
vjeux marked this pull request as ready for review November 5, 2026 22:25
@cixzhang cixzhang added the ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) label Nov 5, 2026
@cixzhang
cixzhang marked this pull request as draft November 5, 2026 22:35
@cixzhang
cixzhang marked this pull request as ready for review November 5, 2026 22:35
@cixzhang
cixzhang merged commit df0e60e into meta:main Nov 6, 2026
165 of 170 checks passed
@vjeux
vjeux deleted the fix/chat-new-messages-committed-refs branch November 6, 2026 00:07

This branch was successfully deployed

1 active deployment
Preview — fb516c06 Deployed Nov 5, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants