useExternalStoreRuntime: infinite re-render loop via unconditional _capabilities reassignment
Bug: useExternalStoreRuntime causes infinite re-render loop via unconditional _capabilities reassignment
Versions
@assistant-ui/react: 0.12.11@assistant-ui/core: 0.1.0- React: 19
Description
useExternalStoreRuntime has a useEffect without a dependency array (useExternalStoreRuntime.js:8-10):
useEffect(() => {
runtime.setAdapter(store);
});
This calls runtime.setAdapter(store) after every render. The first guard if (this._store === store) return works correctly when the consumer memoizes the store object.
However, even when the _store === store check is bypassed (e.g. when isRunning or messages change — legitimate reasons for a new store reference), __internal_setAdapter in ExternalStoreThreadRuntimeCore unconditionally creates a new _capabilities object:
// external-store-thread-runtime-core.ts, __internal_setAdapter()
this._capabilities = {
switchToBranch: this._store.setMessages !== undefined,
edit: this._store.onEdit !== undefined,
reload: this._store.onReload !== undefined,
cancel: this._store.onCancel !== undefined,
// ...
};
This new object reference (even with identical boolean values) propagates through _notifySubscribers() → useSyncExternalStore detects a snapshot change → React re-renders → useEffect fires again → setAdapter with a new store object → new _capabilities → infinite loop.
The loop manifests as React's "Maximum update depth exceeded" error, typically during streaming (where isRunning and messages change frequently).
Root cause
The _capabilities object is always replaced with a new reference, even when all capability values are identical. Since ShallowMemoizeSubject (used by useSyncExternalStore subscribers) compares state properties by reference, the new _capabilities object always triggers subscriber notifications.
Suggested fix
Only assign a new _capabilities object when at least one capability value actually changed:
const newCaps: RuntimeCapabilities = {
switchToBranch: this._store.setMessages !== undefined,
switchBranchDuringRun: false,
edit: this._store.onEdit !== undefined,
reload: this._store.onReload !== undefined,
cancel: this._store.onCancel !== undefined,
speech: this._store.adapters?.speech !== undefined,
dictation: this._store.adapters?.dictation !== undefined,
unstable_copy: this._store.unstable_capabilities?.copy !== false,
attachments: !!this._store.adapters?.attachments,
feedback: !!this._store.adapters?.feedback,
};
const oldCaps = this._capabilities;
if (
!oldCaps ||
oldCaps.switchToBranch !== newCaps.switchToBranch ||
oldCaps.edit !== newCaps.edit ||
oldCaps.reload !== newCaps.reload ||
oldCaps.cancel !== newCaps.cancel ||
oldCaps.speech !== newCaps.speech ||
oldCaps.dictation !== newCaps.dictation ||
oldCaps.unstable_copy !== newCaps.unstable_copy ||
oldCaps.attachments !== newCaps.attachments ||
oldCaps.feedback !== newCaps.feedback
) {
this._capabilities = newCaps;
}
Reproduction
- Use
useExternalStoreRuntimewithconvertMessage,onNew,onEdit,onCancelcallbacks - Memoize the store options via
useMemo(as documented) - Send a message that triggers streaming (
isRunningtoggles,messagesarray updates) - During streaming,
messageschanges → newruntimeOptionsobject →setAdapterruns → new_capabilities→ subscribers notified → re-render → loop
Even with proper useMemo on the consumer side, the loop occurs because _capabilities identity changes inside the library code.
Workaround
We are currently using a pnpm patch on @assistant-ui/[email protected] that adds the equality check described above. Full patch available on request.
Additional context
The useEffect without deps is intentional (to pick up store changes every render), but it makes the library extremely sensitive to any internal state that creates new object references. The _capabilities reassignment is the primary trigger, but any future code that unconditionally creates new objects inside __internal_setAdapter could reintroduce the same issue.
A more robust alternative might be to add a dependency array to the useEffect (e.g. [store]), which would eliminate the need for the _store === store guard entirely and make the behavior more predictable for React's rendering model.
Assisted by Claude (Opus 4.6)