fix: Backfill project field on SDK session creation to prevent race condition
PostToolUse hook can create the session before UserPromptSubmit's session-init sets the project, leaving it empty. Add an UPDATE after INSERT OR IGNORE to backfill the project when a later hook provides it. Closes #939 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
committed by
Alex Newman
parent
6382d6f9c7
commit
af308ea5c8
@@ -1165,7 +1165,7 @@ export class SessionStore {
|
|||||||
const now = new Date();
|
const now = new Date();
|
||||||
const nowEpoch = now.getTime();
|
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
|
// 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
|
// response and stored via updateMemorySessionId(). CRITICAL: memory_session_id must NEVER
|
||||||
// equal contentSessionId - that would inject memory messages into the user's transcript!
|
// equal contentSessionId - that would inject memory messages into the user's transcript!
|
||||||
@@ -1175,6 +1175,14 @@ export class SessionStore {
|
|||||||
VALUES (?, NULL, ?, ?, ?, ?, 'active')
|
VALUES (?, NULL, ?, ?, ?, ?, 'active')
|
||||||
`).run(contentSessionId, project, userPrompt, now.toISOString(), nowEpoch);
|
`).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
|
// Return existing or new ID
|
||||||
const row = this.db.prepare('SELECT id FROM sdk_sessions WHERE content_session_id = ?')
|
const row = this.db.prepare('SELECT id FROM sdk_sessions WHERE content_session_id = ?')
|
||||||
.get(contentSessionId) as { id: number };
|
.get(contentSessionId) as { id: number };
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export function createSDKSession(
|
|||||||
const now = new Date();
|
const now = new Date();
|
||||||
const nowEpoch = now.getTime();
|
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
|
// 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
|
// response and stored via updateMemorySessionId(). CRITICAL: memory_session_id must NEVER
|
||||||
// equal contentSessionId - that would inject memory messages into the user's transcript!
|
// equal contentSessionId - that would inject memory messages into the user's transcript!
|
||||||
@@ -40,6 +40,14 @@ export function createSDKSession(
|
|||||||
VALUES (?, NULL, ?, ?, ?, ?, 'active')
|
VALUES (?, NULL, ?, ?, ?, ?, 'active')
|
||||||
`).run(contentSessionId, project, userPrompt, now.toISOString(), nowEpoch);
|
`).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
|
// Return existing or new ID
|
||||||
const row = db.prepare('SELECT id FROM sdk_sessions WHERE content_session_id = ?')
|
const row = db.prepare('SELECT id FROM sdk_sessions WHERE content_session_id = ?')
|
||||||
.get(contentSessionId) as { id: number };
|
.get(contentSessionId) as { id: number };
|
||||||
|
|||||||
Reference in New Issue
Block a user