From 91eb0cfc38f04a632cdb4f3cdaa5a8e8332e6661 Mon Sep 17 00:00:00 2001 From: uroma Date: Mon, 19 Jan 2026 16:57:45 +0000 Subject: [PATCH] fix: correct route path, fix race condition, add persistence for active sessions - Fix route path from /api/sessions/:id/move to /claude/api/claude/sessions/:id/move - Fix race condition by fetching session once and storing isActiveSession flag - Add database persistence for active session metadata changes - Ensures consistency between in-memory and database state Co-Authored-By: Claude Sonnet 4.5 --- server.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/server.js b/server.js index 17212dd2..c598941c 100644 --- a/server.js +++ b/server.js @@ -634,13 +634,15 @@ app.delete('/claude/api/claude/sessions/:id', requireAuth, (req, res) => { }); // Move session to different project -app.post('/api/sessions/:id/move', requireAuth, (req, res) => { +app.post('/claude/api/claude/sessions/:id/move', requireAuth, (req, res) => { try { const { id } = req.params; const { projectId } = req.body; - // Check if session exists (in-memory or historical) + // Check if session exists (in-memory or historical) - fetch once let session = claudeService.sessions.get(id); + let isActiveSession = !!session; + if (!session) { // Check historical sessions const historicalSessions = claudeService.loadHistoricalSessions(); @@ -668,10 +670,13 @@ app.post('/api/sessions/:id/move', requireAuth, (req, res) => { } // Update session's metadata with projectId - if (claudeService.sessions.get(id)) { - // Active session - update metadata - const activeSession = claudeService.sessions.get(id); - activeSession.metadata.projectId = validatedId; + if (isActiveSession) { + // Active session - update metadata and persist to database + session.metadata.projectId = validatedId; + db.prepare(` + INSERT OR REPLACE INTO sessions (id, projectId, deletedAt) + VALUES (?, ?, NULL) + `).run(id, validatedId); } else { // Historical session - update in database db.prepare(` @@ -681,10 +686,13 @@ app.post('/api/sessions/:id/move', requireAuth, (req, res) => { } } else { // Move to unassigned (projectId = null) - if (claudeService.sessions.get(id)) { - // Active session - remove projectId from metadata - const activeSession = claudeService.sessions.get(id); - delete activeSession.metadata.projectId; + if (isActiveSession) { + // Active session - remove projectId from metadata and persist to database + delete session.metadata.projectId; + db.prepare(` + INSERT OR REPLACE INTO sessions (id, projectId, deletedAt) + VALUES (?, NULL, NULL) + `).run(id); } else { // Historical session - update in database db.prepare(`