From 2e2f0192d9ad82e93203964f9db58ba26e7693f0 Mon Sep 17 00:00:00 2001 From: Christian Gick Date: Sun, 18 Jan 2026 10:33:59 +0200 Subject: [PATCH] fix: Validate session_id exists before memory insert (CF-194) - Added session existence check before INSERT - Graceful degradation: sets session_id to null if invalid - Prevents FK constraint violation - Memory still stored without session link --- src/tools/memories.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/tools/memories.ts b/src/tools/memories.ts index 84930d0..f1c069e 100644 --- a/src/tools/memories.ts +++ b/src/tools/memories.ts @@ -45,7 +45,18 @@ interface MemoryListArgs { * Add a new memory/learning (enhanced with session_id and task_id) */ export async function memoryAdd(args: MemoryAddArgs): Promise { - const { category, title, content, context, project, session_id, task_id } = args; + let { category, title, content, context, project, session_id, task_id } = args; + + // Validate session_id exists if provided (graceful degradation) + if (session_id) { + const sessionExists = await queryOne<{ exists: boolean }>( + 'SELECT EXISTS(SELECT 1 FROM sessions WHERE id = $1)', + [session_id] + ); + if (!sessionExists?.exists) { + session_id = undefined; // Set to null if session doesn't exist + } + } // Generate embedding for semantic search const embedText = `${title}. ${content}`;