Repository navigation
Studio: stop a chat switch mid-reply crashing the app on a stale message index - #13352
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Fixes #10288
Summary
Switching chats while a reply is still generating could replace the whole app with:
and, in the same window,
MessagePartText can only be used inside text or reasoning message parts.(both reported in #10288; the third error there,Duplicate key toolCallId, was fixed by #12442).When the chat view switches threads, assistant-ui swaps its store to the incoming thread before React unmounts the outgoing message rows. If a row that is still mounted re-renders in that window (a streaming text update, a chat store change), it resolves its old index against the incoming thread. The incoming thread is usually empty (still loading, or a new chat), so
thread.message({ index: 1 })throws insideuseSyncExternalStore, and the nearest boundary is the router's, which takes the whole page down. The same window can hand a text leaf a tool call part at its old index, which is theMessagePartTextthrow.Upstream assistant-ui fixed these after the version Studio pins (react 0.12.28, core 0.1.17, store 0.2.9): assistant-ui/assistant-ui#5119 (part hooks tolerate a mismatched part type), #5127 (part index guard) and #5948 / #5270 (message index guard, in the 0.15 / store 0.3 rewrite). Upgrading is a breaking migration, so this applies the same guards to the scopes Studio renders itself.
Changes
stale-safe-scopes.tsx: Studio's ownMessageByIndexProviderfor the message list. While a row's index is past the end of the thread it keeps the message it last resolved, and while a part index is past that message's parts it keeps the part it last resolved there. In-range indexes always read the current thread, so a normal switch shows the new chat. A row that never resolved still throws as before.useMessagePartTextSafereplacesuseMessagePartTextinMarkdownText: a mismatched part reads one frozen empty text part instead of throwing.progressive-messages.tsxuses the guarded provider;markdown-text.tsxuses the safe hook.@assistant-ui/store0.2.9 (already installed as a dependency of@assistant-ui/react) is now declared directly, since the provider importsDerivedfrom it. The lockfile change is the root entry only.Testing
Real Studio builds of main and this branch, Chromium,
unsloth/Qwen3-0.6B-GGUFQ4_K_M. Each trial opens a project, sends a long prompt from the project page, then clicks a saved chat in the sidebar while the reply is generating.tapClientLookup: Index 1 out of bounds (length: 0)Switching away and back mid-reply on this branch still shows the full reply once it finishes (4 of 4 trials, 1,890 to 2,568 characters).
A harness page renders Studio's real
Threadon the same runtime setup (remote thread list, local runtime per thread) and forces the race on every switch: a store subscriber flushes a chat store update synchronously, so the assistant message re-renders between the store switch and React's own update. Main threw in 20 of 20 trials (17Index 1 out of bounds (length: 0), 3Index 3 out of bounds (length: 2)); this branch threw in 0 of 19 trials across 492 forced re-renders.tests/stale-safe-scopes.test.ts: new. Covers the message and part guards, a never-resolved index still throwing, a same-length switch reading the new thread, the frozen empty text part, and reading through a client shaped like assistant-ui's own proxy (an earlier draft wrapped that proxy directly and tripped a Proxy invariant; the test fails on that draft).EMFILE, port in use), and each of those files passes when rerun alone. Typecheck clean; no new biome diagnostics in the changed files.Before / after
Real Studio builds of main (
caa9fbd0a) and this branch, same scenario: a long prompt sent from the project page, a saved chat opened from the sidebar 4 s into the reply, then the generating chat reopened. Main crashes on the switch; this branch shows the finished reply.Not covered