diff --git a/chat/server.js b/chat/server.js index 5f42b87..aaf3ff5 100644 --- a/chat/server.js +++ b/chat/server.js @@ -16906,7 +16906,15 @@ async function handleNewMessage(req, res, sessionId, userId) { if (body.originalMessageId) message.originalMessageId = body.originalMessageId; // Preserve opencodeSessionId for session continuity in retries/continuations // Also prioritize body.opencodeSessionId over session.opencodeSessionId for explicit continuation - if (body.opencodeSessionId) { + // CRITICAL: If session.opencodeSessionId is null (session was reset), ignore body.opencodeSessionId + // to prevent reusing a corrupted session that was previously reset + if (session.opencodeSessionId === null && body.opencodeSessionId) { + log('Ignoring body opencodeSessionId because session was reset', { + bodySessionId: body.opencodeSessionId, + reason: 'Session was reset due to corruption/undo failure - forcing new session creation' + }); + // Don't set message.opencodeSessionId - let ensureOpencodeSession create a new one + } else if (body.opencodeSessionId) { message.opencodeSessionId = body.opencodeSessionId; log('Using explicit opencodeSessionId from request body', { opencodeSessionId: body.opencodeSessionId }); } else if (session.opencodeSessionId) {