From 67f0d7bdd0c563e288fd88a769a8389ea7667416 Mon Sep 17 00:00:00 2001 From: AlexFucuson9 Date: Thu, 16 Jul 2026 19:20:08 +0700 Subject: [PATCH] fix: catch IntegrityError on sessions.title UNIQUE index creation When duplicate titles exist in the sessions table, CREATE UNIQUE INDEX throws sqlite3.IntegrityError instead of OperationalError. The previous code only caught OperationalError, causing Dashboard API crashes. Fix: catch IntegrityError, deduplicate by keeping newest row, then retry index creation. This handles: - Session imports with non-unique titles - Context compression creating duplicate continuation titles - Race conditions in concurrent session creation Fixes #65602 --- hermes_state.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hermes_state.py b/hermes_state.py index 0af1a0484711..8c3fde4070b1 100644 --- a/hermes_state.py +++ b/hermes_state.py @@ -1783,6 +1783,18 @@ def _init_schema(self): "CREATE UNIQUE INDEX IF NOT EXISTS idx_sessions_title_unique " "ON sessions(title) WHERE title IS NOT NULL" ) + except sqlite3.IntegrityError: + # Duplicate titles exist — deduplicate by keeping the newest row + cursor.execute( + "DELETE FROM sessions WHERE rowid NOT IN (" + " SELECT MAX(rowid) FROM sessions" + " WHERE title IS NOT NULL GROUP BY title" + ")" + ) + cursor.execute( + "CREATE UNIQUE INDEX IF NOT EXISTS idx_sessions_title_unique " + "ON sessions(title) WHERE title IS NOT NULL" + ) except sqlite3.OperationalError: pass # Index already exists