fix: stabilize opencode session startup

This commit is contained in:
Haileyesus
2026-05-13 18:44:07 +03:00
parent 421bdd2f0f
commit 57aece12e6
7 changed files with 97 additions and 61 deletions

View File

@@ -27,7 +27,6 @@ import { useFileMentions } from './useFileMentions';
import { type SlashCommand, useSlashCommands } from './useSlashCommands';
type PendingViewSession = {
sessionId: string | null;
startedAt: number;
};
@@ -566,13 +565,9 @@ export function useChatComposerState({
setTimeout(() => scrollToBottom(), 100);
if (!effectiveSessionId && !selectedSession?.id) {
if (typeof window !== 'undefined') {
// Reset stale pending IDs from previous interrupted runs before creating a new one.
sessionStorage.removeItem('pendingSessionId');
}
// For new sessions we intentionally keep this as `null` until the backend
// emits `session_created` with the canonical provider session id.
pendingViewSessionRef.current = { sessionId: null, startedAt: Date.now() };
// This tracks only that a request is in flight before the provider has
// emitted its real session id; routing still waits for session_created.
pendingViewSessionRef.current = { startedAt: Date.now() };
}
if (effectiveSessionId) {
onSessionActive?.(effectiveSessionId);
@@ -884,15 +879,11 @@ export function useChatComposerState({
return;
}
const pendingSessionId =
typeof window !== 'undefined' ? sessionStorage.getItem('pendingSessionId') : null;
const cursorSessionId =
typeof window !== 'undefined' ? sessionStorage.getItem('cursorSessionId') : null;
const candidateSessionIds = [
currentSessionId,
pendingViewSessionRef.current?.sessionId || null,
pendingSessionId,
provider === 'cursor' ? cursorSessionId : null,
selectedSession?.id || null,
];
@@ -910,7 +901,7 @@ export function useChatComposerState({
sessionId: targetSessionId,
provider,
});
}, [canAbortSession, currentSessionId, pendingViewSessionRef, provider, selectedSession?.id, sendMessage]);
}, [canAbortSession, currentSessionId, provider, selectedSession?.id, sendMessage]);
const handleGrantToolPermission = useCallback(
(suggestion: { entry: string; toolName: string }) => {

View File

@@ -7,7 +7,6 @@ import type { ProjectSession, LLMProvider } from '../../../types/app';
import type { SessionStore, NormalizedMessage } from '../../../stores/useSessionStore';
type PendingViewSession = {
sessionId: string | null;
startedAt: number;
};
@@ -63,6 +62,7 @@ interface UseChatRealtimeHandlersArgs {
streamTimerRef: MutableRefObject<number | null>;
accumulatedStreamRef: MutableRefObject<string>;
onSessionInactive?: (sessionId?: string | null) => void;
onSessionActive?: (sessionId?: string | null) => void;
onSessionProcessing?: (sessionId?: string | null) => void;
onSessionNotProcessing?: (sessionId?: string | null) => void;
onNavigateToSession?: (sessionId: string, options?: SessionNavigationOptions) => void;
@@ -89,6 +89,7 @@ export function useChatRealtimeHandlers({
streamTimerRef,
accumulatedStreamRef,
onSessionInactive,
onSessionActive,
onSessionProcessing,
onSessionNotProcessing,
onNavigateToSession,
@@ -104,7 +105,7 @@ export function useChatRealtimeHandlers({
lastProcessedMessageRef.current = latestMessage;
const activeViewSessionId =
selectedSession?.id || currentSessionId || pendingViewSessionRef.current?.sessionId || null;
selectedSession?.id || currentSessionId || null;
/* ---------------------------------------------------------------- */
/* Legacy messages (no `kind` field) — handle and return */
@@ -151,10 +152,12 @@ export function useChatRealtimeHandlers({
statusSessionId === currentSessionId || (selectedSession && statusSessionId === selectedSession.id);
if (msg.isProcessing) {
onSessionActive?.(statusSessionId);
onSessionProcessing?.(statusSessionId);
if (isCurrentSession) { setIsLoading(true); setCanAbortSession(true); }
return;
}
onSessionInactive?.(statusSessionId);
onSessionNotProcessing?.(statusSessionId);
if (isCurrentSession) {
@@ -235,15 +238,21 @@ export function useChatRealtimeHandlers({
if (!currentSessionId) {
console.log('Session created with ID:', newSessionId);
console.log('Existing session ID:', currentSessionId);
sessionStorage.setItem('pendingSessionId', newSessionId);
if (pendingViewSessionRef.current && !pendingViewSessionRef.current.sessionId) {
pendingViewSessionRef.current.sessionId = newSessionId;
}
setCurrentSessionId(newSessionId);
setPendingPermissionRequests((prev) =>
prev.map((r) => (r.sessionId ? r : { ...r, sessionId: newSessionId })),
);
}
pendingViewSessionRef.current = null;
onSessionActive?.(newSessionId);
onSessionProcessing?.(newSessionId);
setIsLoading(true);
setCanAbortSession(true);
setClaudeStatus({
text: 'Processing',
tokens: 0,
can_interrupt: true,
});
onNavigateToSession?.(newSessionId);
break;
}
@@ -266,6 +275,7 @@ export function useChatRealtimeHandlers({
setPendingPermissionRequests([]);
onSessionInactive?.(sid);
onSessionNotProcessing?.(sid);
pendingViewSessionRef.current = null;
// Handle aborted case
if (msg.aborted) {
@@ -279,16 +289,10 @@ export function useChatRealtimeHandlers({
typeof msg.actualSessionId === 'string' && msg.actualSessionId.trim().length > 0
? msg.actualSessionId
: null;
const pendingSessionId = sessionStorage.getItem('pendingSessionId');
const completedSuccessfully = msg.exitCode === undefined || msg.exitCode === 0;
const isVisibleSession =
Boolean(
sid
&& (
sid === activeViewSessionId
|| sid === pendingSessionId
|| pendingViewSessionRef.current?.sessionId === sid
),
&& sid === activeViewSessionId,
);
if (actualSessionId && sid && actualSessionId !== sid) {
@@ -296,17 +300,6 @@ export function useChatRealtimeHandlers({
if (isVisibleSession) {
setCurrentSessionId(actualSessionId);
if (pendingViewSessionRef.current) {
const pendingSession = pendingViewSessionRef.current.sessionId;
if (!pendingSession || pendingSession === sid) {
pendingViewSessionRef.current.sessionId = actualSessionId;
}
}
}
if (completedSuccessfully && pendingSessionId === sid) {
sessionStorage.removeItem('pendingSessionId');
}
if (isVisibleSession) {
@@ -316,16 +309,6 @@ export function useChatRealtimeHandlers({
break;
}
// Clear pending session
if (pendingSessionId && !currentSessionId && completedSuccessfully) {
const resolvedSessionId = actualSessionId || pendingSessionId;
setCurrentSessionId(resolvedSessionId);
if (actualSessionId) {
onNavigateToSession?.(resolvedSessionId, { replace: true });
}
sessionStorage.removeItem('pendingSessionId');
setTimeout(() => { void paletteOps.refreshProjects(); }, 500);
}
break;
}
@@ -335,6 +318,7 @@ export function useChatRealtimeHandlers({
setClaudeStatus(null);
onSessionInactive?.(sid);
onSessionNotProcessing?.(sid);
pendingViewSessionRef.current = null;
break;
}
@@ -399,6 +383,7 @@ export function useChatRealtimeHandlers({
streamTimerRef,
accumulatedStreamRef,
onSessionInactive,
onSessionActive,
onSessionProcessing,
onSessionNotProcessing,
onNavigateToSession,

View File

@@ -13,7 +13,6 @@ const MESSAGES_PER_PAGE = 20;
const INITIAL_VISIBLE_MESSAGES = 100;
type PendingViewSession = {
sessionId: string | null;
startedAt: number;
};
@@ -160,7 +159,7 @@ export function useChatSessionState({
* Why this is essential:
* - Chat keeps local state that is not fully derived from `selectedSession`:
* `currentSessionId`, `pendingUserMessage`, streaming/status flags, message
* pagination/scroll bookkeeping, and pending session IDs in sessionStorage.
* pagination/scroll bookkeeping, and provider-specific sessionStorage keys.
* - If the user clicks New Session while already on the same route with no
* selected session, parent state updates can be idempotent and this local
* state would otherwise persist, making the click appear to "do nothing".
@@ -177,7 +176,6 @@ export function useChatSessionState({
setIsLoading(false);
setCurrentSessionId(null);
setPendingUserMessage(null);
sessionStorage.removeItem('pendingSessionId');
sessionStorage.removeItem('cursorSessionId');
messagesOffsetRef.current = 0;
setHasMoreMessages(false);
@@ -396,6 +394,12 @@ export function useChatSessionState({
// Main session loading effect — store-based
useEffect(() => {
if (!selectedSession || !selectedProject) {
// A new provider run can be in flight before the router has a canonical
// selectedSession. Keep the processing banner alive until complete/error.
if (pendingViewSessionRef.current) {
return;
}
resetStreamingState();
pendingViewSessionRef.current = null;
setClaudeStatus(null);
@@ -537,10 +541,6 @@ export function useChatSessionState({
}
}, [selectedSession]);
useEffect(() => {
if (selectedSession?.id) pendingViewSessionRef.current = null;
}, [pendingViewSessionRef, selectedSession?.id]);
// Scroll to search target
useEffect(() => {
if (!searchTarget || chatMessages.length === 0 || isLoadingSessionMessages) return;

View File

@@ -17,7 +17,6 @@ import ChatComposer from './subcomponents/ChatComposer';
type PendingViewSession = {
sessionId: string | null;
startedAt: number;
};
@@ -240,6 +239,7 @@ function ChatInterface({
streamTimerRef,
accumulatedStreamRef,
onSessionInactive,
onSessionActive,
onSessionProcessing,
onSessionNotProcessing,
onNavigateToSession,