Version: @convex-dev/agent@0.6.1
Reproduction
A streaming message that completes with zero deltas in its accumulated deltas array (e.g. a tool-only response, or a transient state where the stream is registered but no chunks have arrived yet) sends React into an infinite setState loop, throwing the classic Maximum update depth exceeded error.
Stack trace points to useStreamingUIMessages.js:65 → streams.map$argument_0 → setMessageState.
Root cause
src/react/useStreamingUIMessages.ts lines 68-78:
for (const stream of streams) {
const lastDelta = stream.deltas.at(-1);
const cursor = messageState[stream.streamMessage.streamId]?.cursor;
if (!cursor) { // <-- bug 1
noNewDeltas = false;
break;
}
if (lastDelta && lastDelta.start >= cursor) { // <-- bug 2
noNewDeltas = false;
break;
}
}
-
!cursor treats cursor === 0 as "not set yet". But getParts(emptyDeltas, 0) returns cursor: 0, so any stream with no deltas writes messageState[streamId].cursor = 0. Next render the guard re-fires → setMessageState again → infinite loop.
-
lastDelta.start >= cursor is also too loose: cursor is delta.end after getParts, so when lastDelta.end === lastDelta.start (zero-length delta, which deltas.js explicitly tolerates) the same loop triggers.
Suggested fix
- if (!cursor) {
+ if (cursor === undefined) {
noNewDeltas = false;
break;
}
- if (lastDelta && lastDelta.start >= cursor) {
+ if (lastDelta && lastDelta.end > cursor) {
noNewDeltas = false;
break;
}
cursor === undefined only treats truly-unseen streams as needing work, and lastDelta.end > cursor matches the semantics of getParts (cursor = end of last consumed delta).
Patched locally via pnpm patch and confirmed loop stops; happy to send a PR if useful.
Version:
@convex-dev/agent@0.6.1Reproduction
A streaming message that completes with zero deltas in its accumulated
deltasarray (e.g. a tool-only response, or a transient state where the stream is registered but no chunks have arrived yet) sends React into an infinitesetStateloop, throwing the classicMaximum update depth exceedederror.Stack trace points to
useStreamingUIMessages.js:65→streams.map$argument_0→setMessageState.Root cause
src/react/useStreamingUIMessages.tslines 68-78:!cursortreatscursor === 0as "not set yet". ButgetParts(emptyDeltas, 0)returnscursor: 0, so any stream with no deltas writesmessageState[streamId].cursor = 0. Next render the guard re-fires →setMessageStateagain → infinite loop.lastDelta.start >= cursoris also too loose:cursorisdelta.endaftergetParts, so whenlastDelta.end === lastDelta.start(zero-length delta, which deltas.js explicitly tolerates) the same loop triggers.Suggested fix
cursor === undefinedonly treats truly-unseen streams as needing work, andlastDelta.end > cursormatches the semantics ofgetParts(cursor = end of last consumed delta).Patched locally via
pnpm patchand confirmed loop stops; happy to send a PR if useful.