fix: prevent fallback effect from immediately resetting sendPhase on follow-up messages

The fallback effect that catches missed 'running' phase transitions was
incorrectly firing on follow-up messages. When beginSendPhase was called,
latestTurnSettled was still true from the previous completed turn, causing
an immediate resetSendPhase before the server could create the new turn.

Fix: record the turnId at the time beginSendPhase is called (via a ref),
and only allow the fallback reset when the latest turn differs from the
one recorded — meaning a new turn was actually created and has settled.

Applied via @cursor push command
This commit is contained in:
Cursor Agent committed 2026-03-29 17:07:04 +00:00
1 parent 9a9be39751
commit c91e8b23e8
1 file changed
+19 -6
+19 -6
View File
@@ -2161,14 +2161,21 @@ export default function ChatView({ threadId }: ChatViewProps) {
};
}, [phase, sendPhase]);
const beginSendPhase = useCallback((nextPhase: Exclude<SendPhase, "idle">) => {
setSendStartedAt((current) => current ?? new Date().toISOString());
setSendPhase(nextPhase);
}, []);
const turnIdAtSendRef = useRef<string | null>(null);
const beginSendPhase = useCallback(
(nextPhase: Exclude<SendPhase, "idle">) => {
setSendStartedAt((current) => current ?? new Date().toISOString());
turnIdAtSendRef.current = activeLatestTurn?.turnId ?? null;
setSendPhase(nextPhase);
},
[activeLatestTurn?.turnId],
);
const resetSendPhase = useCallback(() => {
setSendPhase("idle");
setSendStartedAt(null);
turnIdAtSendRef.current = null;
}, []);
useEffect(() => {
@@ -2198,12 +2205,18 @@ export default function ChatView({ threadId }: ChatViewProps) {
// this by checking whether the turn has settled while sendPhase is still
// non-idle — the turn completing is a definitive signal that "sending" is
// over.
//
// Guard: only reset when the latest turn differs from the one recorded at
// send time. Without this, a follow-up message on a thread whose previous
// turn already settled would be immediately reset before the server creates
// the new turn.
useEffect(() => {
if (sendPhase === "idle") return;
if (phase !== "running" && phase !== "connecting" && latestTurnSettled) {
const turnChanged = activeLatestTurn?.turnId !== turnIdAtSendRef.current;
if (phase !== "running" && phase !== "connecting" && latestTurnSettled && turnChanged) {
resetSendPhase();
}
}, [latestTurnSettled, phase, resetSendPhase, sendPhase]);
}, [activeLatestTurn?.turnId, latestTurnSettled, phase, resetSendPhase, sendPhase]);
useEffect(() => {
if (!activeThreadId) return;