Repository navigation
fix(Chat): latest-value refs are written at commit - #10162
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report🔎 Preview ·
|
| 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) | ![]() |
![]() |
![]() |
| Chat · Default (neutral, light) | ![]() |
![]() |
![]() |
| Chat · Clipping (neutral, dark) | ![]() |
![]() |
![]() |
| Chat · Clipping (neutral, light) | ![]() |
![]() |
![]() |
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
There was a problem hiding this comment.
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, becauseobserveResizefires inside the callback ref, so a commit that mounts or swaps the content element and also changesisLockedoronResizeuses the previous commit's values (the old listener runs, and going from unlocked to locked still flags new messages); writing both refs inuseInsertionEffect, 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]
| useIsomorphicLayoutEffect(() => { | ||
| isLockedRef.current = isLocked; | ||
| onResizeRef.current = onResize; | ||
| }); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
988600c to
22909de
Compare
There was a problem hiding this comment.
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 onmainthat only happens after a render React discards, so scope it to that case, for example "After such a render,useChatNewMessagesalso 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. |
There was a problem hiding this comment.
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:
| [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. |
There was a problem hiding this comment.
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.
22909de to
fb516c0
Compare
There was a problem hiding this comment.
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]












User impact
useChatNewMessages(whichChatLayoutuses),useSpeechRecognitionanduseChatDictationcould 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,useChatNewMessagescould also flag new messages while the list followed the bottom.Problem and solution fit
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-runtimeINV4: ref-backed coordination must preserve the current API contract.Smallest restoration
The recognition hooks, read only by recognition events, write in one
useIsomorphicLayoutEffect, asusePanGesturedoes.useChatNewMessageswrites in oneuseInsertionEffect. 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.ComplexSelectorandBaseTypeaheaddo the same. There is no API or visual change.ChatLayout's other parts write no ref during render.ChatComposerInputfollows separately; open pull requests change it.Evidence
mainwithout the fix (312e311, on this fork'sred-arm/chat-latest-value-refs).red-arm/chat-content-swap). The insertion Effect passes.useChatNewMessages.test.tsxanduseChatDictation.test.tsx. Each fails if a value it covers is written during render or after attach.Scope
Testing
Local gates at fb516c0, one commit on
main4789178.npx vitest run --project ui packages/core/src/Chat/: 440 passed. The red arms above fail without the fix.tsc --project packages/core/tsconfig.json --noEmit, ESLint, Prettier and changeset coverage: clean.pnpm review:pre: no findings.