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.
This commit is contained in:
@@ -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)' });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user