v1.12.3: stale-stream banner with Retry/Discard
When an assistant message sits status='streaming' with no token activity for 60+ seconds, the chat shows a banner above the input offering Retry or Discard. Both clear the stale row via a new backend endpoint POST /api/chats/:id/discard_stale that updates status='failed' and publishes chat_status='idle'. Closes the UX gap that caused the 2026-05-21 debugging spiral — slow streams and dead streams now look different to the user. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import { api } from '@/api/client';
|
||||
import { useSessionStream } from '@/hooks/useSessionStream';
|
||||
import { MessageList } from '@/components/MessageList';
|
||||
import { ChatInput } from '@/components/ChatInput';
|
||||
import { StaleStreamBanner } from '@/components/StaleStreamBanner';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
@@ -44,6 +45,38 @@ export function ChatPane({ sessionId, chatId, projectId, agentId, onAgentChange,
|
||||
|
||||
const chatMessages = stream.messages.filter((m) => m.chat_id === chatId);
|
||||
const streaming = chatMessages.some((m) => m.status === 'streaming');
|
||||
|
||||
// v1.12.3: stale-stream detection. Watches the (at most one) streaming
|
||||
// assistant row. If its content length doesn't grow for STALE_THRESHOLD_MS,
|
||||
// assume the upstream call is dead and surface the recovery banner. We use
|
||||
// content length as the activity signal because every token delta extends
|
||||
// it; last_seq isn't currently bumped per delta.
|
||||
const STALE_THRESHOLD_MS = 60_000;
|
||||
const streamingMsg = chatMessages.find((m) => m.status === 'streaming' && m.role === 'assistant');
|
||||
const streamingId = streamingMsg?.id ?? null;
|
||||
const streamingLen = streamingMsg?.content.length ?? 0;
|
||||
const lastActivityRef = useRef<{ id: string; len: number; at: number } | null>(null);
|
||||
const [stale, setStale] = useState(false);
|
||||
useEffect(() => {
|
||||
if (!streamingId) {
|
||||
lastActivityRef.current = null;
|
||||
setStale(false);
|
||||
return;
|
||||
}
|
||||
const prev = lastActivityRef.current;
|
||||
if (!prev || prev.id !== streamingId || prev.len !== streamingLen) {
|
||||
lastActivityRef.current = { id: streamingId, len: streamingLen, at: Date.now() };
|
||||
setStale(false);
|
||||
}
|
||||
const interval = setInterval(() => {
|
||||
const a = lastActivityRef.current;
|
||||
if (!a) return;
|
||||
if (Date.now() - a.at >= STALE_THRESHOLD_MS) {
|
||||
setStale(true);
|
||||
}
|
||||
}, 5_000);
|
||||
return () => clearInterval(interval);
|
||||
}, [streamingId, streamingLen]);
|
||||
// v1.11.5: per-chat model context limit comes from chat.model_context_limit
|
||||
// populated by GET /api/sessions/:id/chats. Threaded into ChatInput so
|
||||
// ContextBar can render a zero-state before the first assistant message.
|
||||
@@ -87,6 +120,45 @@ export function ChatPane({ sessionId, chatId, projectId, agentId, onAgentChange,
|
||||
}
|
||||
}
|
||||
|
||||
const handleDiscardStale = useCallback(async () => {
|
||||
if (!streamingId) return;
|
||||
try {
|
||||
await api.chats.discardStale(chatId, streamingId);
|
||||
setStale(false);
|
||||
lastActivityRef.current = null;
|
||||
} catch (err) {
|
||||
// 409 (race) is benign — the row already terminated some other way.
|
||||
const msg = err instanceof Error ? err.message : 'discard failed';
|
||||
if (!msg.includes('409')) toast.error(msg);
|
||||
setStale(false);
|
||||
}
|
||||
}, [chatId, streamingId]);
|
||||
|
||||
const handleRetryStale = useCallback(async () => {
|
||||
if (!streamingId) return;
|
||||
const lastUser = [...chatMessages].reverse().find((m) => m.role === 'user' && m.kind === 'message');
|
||||
if (!lastUser) {
|
||||
toast.error('no prior user message to retry');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await api.chats.discardStale(chatId, streamingId);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : 'discard failed';
|
||||
if (!msg.includes('409')) {
|
||||
toast.error(msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
setStale(false);
|
||||
lastActivityRef.current = null;
|
||||
try {
|
||||
await api.messages.send(chatId, lastUser.content);
|
||||
} catch (err) {
|
||||
toast.error(err instanceof Error ? err.message : 'retry send failed');
|
||||
}
|
||||
}, [chatId, streamingId, chatMessages]);
|
||||
|
||||
const handleForceSend = useCallback(async (content: string) => {
|
||||
const trimmed = content.trim();
|
||||
if (!trimmed) return;
|
||||
@@ -187,6 +259,13 @@ export function ChatPane({ sessionId, chatId, projectId, agentId, onAgentChange,
|
||||
</div>
|
||||
)}
|
||||
|
||||
{stale && streamingId && (
|
||||
<StaleStreamBanner
|
||||
onRetry={() => void handleRetryStale()}
|
||||
onDiscard={() => void handleDiscardStale()}
|
||||
/>
|
||||
)}
|
||||
|
||||
<ChatInput
|
||||
disabled={false}
|
||||
projectId={projectId}
|
||||
|
||||
Reference in New Issue
Block a user