diff --git a/src/services/sqlite/SessionStore.ts b/src/services/sqlite/SessionStore.ts index e536edec..b17a569c 100644 --- a/src/services/sqlite/SessionStore.ts +++ b/src/services/sqlite/SessionStore.ts @@ -1165,7 +1165,7 @@ export class SessionStore { const now = new Date(); const nowEpoch = now.getTime(); - // Pure INSERT OR IGNORE - no updates, no complexity + // INSERT OR IGNORE to create session, then backfill project if it was created empty // NOTE: memory_session_id starts as NULL. It is captured by SDKAgent from the first SDK // response and stored via updateMemorySessionId(). CRITICAL: memory_session_id must NEVER // equal contentSessionId - that would inject memory messages into the user's transcript! @@ -1175,6 +1175,14 @@ export class SessionStore { VALUES (?, NULL, ?, ?, ?, ?, 'active') `).run(contentSessionId, project, userPrompt, now.toISOString(), nowEpoch); + // Backfill project if session was created by another hook with empty project + if (project) { + this.db.prepare(` + UPDATE sdk_sessions SET project = ? + WHERE content_session_id = ? AND (project IS NULL OR project = '') + `).run(project, contentSessionId); + } + // Return existing or new ID const row = this.db.prepare('SELECT id FROM sdk_sessions WHERE content_session_id = ?') .get(contentSessionId) as { id: number }; diff --git a/src/services/sqlite/sessions/create.ts b/src/services/sqlite/sessions/create.ts index d2bf773e..ffc5cb13 100644 --- a/src/services/sqlite/sessions/create.ts +++ b/src/services/sqlite/sessions/create.ts @@ -30,7 +30,7 @@ export function createSDKSession( const now = new Date(); const nowEpoch = now.getTime(); - // Pure INSERT OR IGNORE - no updates, no complexity + // INSERT OR IGNORE to create session, then backfill project if it was created empty // NOTE: memory_session_id starts as NULL. It is captured by SDKAgent from the first SDK // response and stored via updateMemorySessionId(). CRITICAL: memory_session_id must NEVER // equal contentSessionId - that would inject memory messages into the user's transcript! @@ -40,6 +40,14 @@ export function createSDKSession( VALUES (?, NULL, ?, ?, ?, ?, 'active') `).run(contentSessionId, project, userPrompt, now.toISOString(), nowEpoch); + // Backfill project if session was created by another hook with empty project + if (project) { + db.prepare(` + UPDATE sdk_sessions SET project = ? + WHERE content_session_id = ? AND (project IS NULL OR project = '') + `).run(project, contentSessionId); + } + // Return existing or new ID const row = db.prepare('SELECT id FROM sdk_sessions WHERE content_session_id = ?') .get(contentSessionId) as { id: number };