fix session switch race on message send

This commit is contained in:
shiyue
2026-06-24 09:54:11 +08:00
parent 01c7fdd27a
commit 2f02270edc
4 changed files with 176 additions and 119 deletions

View File

@@ -5038,7 +5038,7 @@ wss.on('connection', (ws, req) => {
handleNewSession(ws, msg);
break;
case 'load_session':
handleLoadSession(ws, msg.sessionId);
handleLoadSession(ws, msg);
break;
case 'load_history_page':
handleLoadHistoryPage(ws, msg);
@@ -5602,7 +5602,7 @@ function handleSlashCommand(ws, text, sessionId, fallbackAgent) {
clearRuntimeSessionId(session);
session.updated = new Date().toISOString();
saveSession(session);
wsSend(ws, {
wsSend(ws, attachClientRequestId({
type: 'session_info',
sessionId: session.id,
messages: [],
@@ -5614,7 +5614,7 @@ function handleSlashCommand(ws, text, sessionId, fallbackAgent) {
cwd: session.cwd || null,
totalCost: session.totalCost || 0,
totalUsage: session.totalUsage || null,
});
}, { sessionId }));
}
wsSend(ws, { type: 'system_message', message: '会话已清除,上下文已重置。' });
break;
@@ -5885,6 +5885,11 @@ function buildSessionInfoPayload(session) {
};
}
function attachClientRequestId(payload, source = {}) {
const requestId = String(source?.requestId || '').trim();
return requestId ? { ...payload, requestId } : payload;
}
function handleNewSession(ws, msg) {
const result = createPersistentConversationSession(msg || {}, {
defaultAgent: normalizeAgent(msg?.agent),
@@ -5902,7 +5907,7 @@ function handleNewSession(ws, msg) {
const { session } = result;
detachWsFromActiveRuntimes(ws);
wsSessionMap.set(ws, session.id);
wsSend(ws, buildSessionInfoPayload(session));
wsSend(ws, attachClientRequestId(buildSessionInfoPayload(session), msg));
sendSessionList(ws);
}
@@ -5929,7 +5934,8 @@ function handleLoadHistoryPage(ws, msg = {}) {
});
}
function handleLoadSession(ws, sessionId) {
function handleLoadSession(ws, msg) {
const sessionId = sanitizeId(typeof msg === 'string' ? msg : msg?.sessionId);
reconcilePendingCrossConversationReplies();
const session = loadSession(sessionId);
if (!session) {
@@ -5961,7 +5967,7 @@ function handleLoadSession(ws, sessionId) {
saveSession(refreshedSession);
}
wsSend(ws, {
wsSend(ws, attachClientRequestId({
type: 'session_info',
sessionId: refreshedSession.id,
messages: recentMessages,
@@ -5987,7 +5993,7 @@ function handleLoadSession(ws, sessionId) {
waitingReplyCount: waitState.waitingReplyCount,
failedReplyCount: waitState.failedReplyCount,
pendingReplies: waitState.pendingReplies,
});
}, msg));
if (olderChunks.length > 0) {
olderChunks.forEach((chunk, index) => {
@@ -8331,7 +8337,7 @@ function handleImportNativeSession(ws, msg) {
};
saveSession(session);
wsSessionMap.set(ws, id);
wsSend(ws, {
wsSend(ws, attachClientRequestId({
type: 'session_info',
sessionId: id,
messages: session.messages,
@@ -8347,7 +8353,7 @@ function handleImportNativeSession(ws, msg) {
hasUnread: false,
historyPending: false,
isRunning: false,
});
}, msg));
sendSessionList(ws);
}
@@ -8432,7 +8438,7 @@ function handleImportCodexSession(ws, msg) {
saveSession(session);
wsSessionMap.set(ws, id);
wsSend(ws, {
wsSend(ws, attachClientRequestId({
type: 'session_info',
sessionId: id,
messages: session.messages,
@@ -8448,7 +8454,7 @@ function handleImportCodexSession(ws, msg) {
hasUnread: false,
historyPending: false,
isRunning: false,
});
}, msg));
sendSessionList(ws);
}