From 227794d4632bc788ad733d62ce36e2450b2e7cdc Mon Sep 17 00:00:00 2001 From: southseact-3d Date: Tue, 17 Feb 2026 19:24:12 +0000 Subject: [PATCH] fix: handle undo failures gracefully by resetting session When opencode undo command fails: 1. Reset the opencode session (both IDs set to null) 2. Still remove the message from history 3. Return success instead of error This prevents corrupted sessions from blocking subsequent operations. --- chat/server.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/chat/server.js b/chat/server.js index 1e12232..5f42b87 100644 --- a/chat/server.js +++ b/chat/server.js @@ -17390,7 +17390,33 @@ async function handleUndoMessage(req, res, sessionId, messageId, userId) { sendJson(res, 200, { ok: true, message: 'Undo command sent successfully' }); } catch (error) { log('Undo command failed', { sessionId, messageId, error: String(error) }); - sendJson(res, 500, { error: `Undo failed: ${error.message}` }); + + // Reset opencode session since it's likely corrupted + log('Resetting opencode session due to undo failure', { + sessionId, + messageId, + previousSessionId: session.opencodeSessionId + }); + session.opencodeSessionId = null; + session.initialOpencodeSessionId = null; + + // Still try to remove the message and clear todos even if opencode undo failed + try { + if (message.todos) { + message.todos = []; + } + const messageIndex = session.messages.findIndex(m => m.id === messageId); + if (messageIndex !== -1) { + session.messages.splice(messageIndex, 1); + } + await persistState(); + log('Message removed despite undo command failure', { sessionId, messageId }); + } catch (cleanupErr) { + log('Failed to cleanup message after undo failure', { sessionId, messageId, error: String(cleanupErr) }); + } + + // Return success since the message is removed from history + sendJson(res, 200, { ok: true, message: 'Undo completed (session reset)' }); } }