Skip to content

Studio: stop a chat switch mid-reply crashing the app on a stale message index - #13352

Merged
danielhanchen merged 2 commits into
mainfrom
studio-thread-switch-stale-index
Oct 12, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-thread-switch-stale-index

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Fixes #10288

Summary

Switching chats while a reply is still generating could replace the whole app with:

Something went wrong!
tapClientLookup: Index 1 out of bounds (length: 0)

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 inside useSyncExternalStore, 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 the MessagePartText throw.

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 own MessageByIndexProvider for 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.
  • useMessagePartTextSafe replaces useMessagePartText in MarkdownText: a mismatched part reads one frozen empty text part instead of throwing.
  • progressive-messages.tsx uses the guarded provider; markdown-text.tsx uses the safe hook.
  • @assistant-ui/store 0.2.9 (already installed as a dependency of @assistant-ui/react) is now declared directly, since the provider imports Derived from it. The lockfile change is the root entry only.

Testing

Real Studio builds of main and this branch, Chromium, unsloth/Qwen3-0.6B-GGUF Q4_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.

Build Trials App crashed Error
main, run 1 21 6 tapClientLookup: Index 1 out of bounds (length: 0)
main, run 2 (after the branch run) 15 4 same, all on switching away from the project chat
this branch 30 0 none, no console errors

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 Thread on 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 (17 Index 1 out of bounds (length: 0), 3 Index 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).
  • Frontend tests: 11,313 of 11,327 passed in a full run on a heavily loaded host; the 14 failures were inotify / port exhaustion (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.

Before: Something went wrong. After: the reply renders in full after switching away and back

Not covered

  • Windows, macOS and the desktop app were not tested live; the change is frontend only.
  • Other part leaves (tool cards) still read their part directly; only the text part had a throwing read.

@danielhanchen danielhanchen self-assigned this Oct 12, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-12T02:38:22.604377Z d7eaf1f New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-12T02:24:48.856722Z 95b463c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 12, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 12, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 95b463c369

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

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

Labels

None yet

Projects

None yet

1 participant