fix(server): reset session ID on query restart to prevent overwrite crash (#201)
* fix(server): reset session ID on query restart to prevent overwrite crash When ensureQuery() restarts a query, the old claudeSessionId was retained. If the new query landed on a different Claude session (e.g. after a hook or reconnect), captureSessionIdFromMessage() would detect the mismatch and throw a fatal "session ID overwrite" error, killing the agent mid-turn. Reset claudeSessionId and persistence during query restart so the new session can establish its own identity cleanly. Closes #200 * fix(server): recover from session ID overwrite and load projects instantly - Reset claudeSessionId unconditionally before every new query creation, not just on explicit restarts. Fixes unrecoverable crash loop where a pump failure left stale session ID, causing every subsequent query to immediately throw the overwrite error again. - Decouple fetch_workspaces_request from reconciliation: serve registry snapshot immediately instead of awaiting git operations on all workspaces. Background reconciliation runs after response is sent and pushes workspace_update events for any changed workspaces (e.g. stale worktrees getting archived). * fix(server): auto-resume Claude session after overwrite error to preserve history When the query pump dies (e.g. after a session ID overwrite error), preserve claudeSessionId so the next query automatically passes resume: sessionId to the Claude SDK. Claude responds with the same session ID, the overwrite check passes, and the conversation history is intact. Previously claudeSessionId was reset unconditionally before every new query, causing the next query to start a fresh session and lose the conversation. Now it is only reset for explicit restarts (queryRestartNeeded), where a fresh session is the intended behavior. * fix(server): reset session ID before throwing overwrite error to break retry loop When the overwrite error fired, claudeSessionId was left set to the old value. Our auto-resume fix then passed resume: oldSessionId on every retry, Claude returned yet another new session ID each time, and the overwrite error looped indefinitely (same "Existing" UUID across all failures in the log). Fix: reset claudeSessionId = null in both captureSessionIdFromMessage and handleSystemMessage before throwing, so the next query starts a fresh session instead of looping on a dead resume target. Auto-resume still works for legitimate pump failures (network drops etc.) since those exit without throwing, leaving claudeSessionId intact. * fix(server): accept session ID change mid-stream instead of failing the turn Hooks can cause Claude to restart with a new session ID mid-turn. Previously both captureSessionIdFromMessage and handleSystemMessage threw a CRITICAL error when the session ID changed, failing the turn and leaving the user with an error message on what should have been a transparent hook execution. Now both paths log a warning and accept the new session ID, allowing the turn to continue uninterrupted through hook-triggered subprocess restarts.
This commit is contained in:
@@ -1848,6 +1848,9 @@ class ClaudeAgentSession implements AgentSession {
|
||||
this.input = null;
|
||||
this.queryPumpPromise = null;
|
||||
this.queryRestartNeeded = false;
|
||||
// Reset session identity for explicit restarts so the new query starts
|
||||
// a fresh session rather than resuming the previous one.
|
||||
this.claudeSessionId = null;
|
||||
oldInput?.end();
|
||||
oldQuery.close?.();
|
||||
try {
|
||||
@@ -1857,6 +1860,12 @@ class ClaudeAgentSession implements AgentSession {
|
||||
}
|
||||
}
|
||||
|
||||
// When the pump died unexpectedly (query became null, e.g. after a session
|
||||
// ID overwrite error), preserve claudeSessionId so buildOptions() passes
|
||||
// resume: sessionId and the new query auto-resumes the previous session.
|
||||
// For explicit restarts above, claudeSessionId was already cleared.
|
||||
this.persistence = null;
|
||||
|
||||
const input = createAsyncMessageInput<SDKUserMessage>();
|
||||
const options = this.buildOptions();
|
||||
this.logger.debug({ options: summarizeClaudeOptionsForLog(options) }, "claude query");
|
||||
@@ -2689,11 +2698,16 @@ class ClaudeAgentSession implements AgentSession {
|
||||
if (this.claudeSessionId === sessionId) {
|
||||
return null;
|
||||
}
|
||||
throw new Error(
|
||||
`CRITICAL: Claude session ID overwrite detected! ` +
|
||||
`Existing: ${this.claudeSessionId}, New: ${sessionId}. ` +
|
||||
`This indicates a session identity corruption bug.`,
|
||||
// Session ID changed mid-stream (e.g. a hook caused Claude to restart
|
||||
// with a new session). Accept the new ID and continue — the turn should
|
||||
// not be failed just because the underlying subprocess cycled.
|
||||
this.logger.warn(
|
||||
{ existingSessionId: this.claudeSessionId, newSessionId: sessionId },
|
||||
"Claude session ID changed in message; accepting new session",
|
||||
);
|
||||
this.claudeSessionId = sessionId;
|
||||
this.persistence = null;
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
private handleSystemMessage(message: SDKSystemMessage): string | null {
|
||||
@@ -2728,11 +2742,14 @@ class ClaudeAgentSession implements AgentSession {
|
||||
} else if (existingSessionId === newSessionId) {
|
||||
this.logger.debug({ sessionId: newSessionId }, "Claude session ID unchanged (same value)");
|
||||
} else {
|
||||
throw new Error(
|
||||
`CRITICAL: Claude session ID overwrite detected! ` +
|
||||
`Existing: ${existingSessionId}, New: ${newSessionId}. ` +
|
||||
`This indicates a session identity corruption bug.`,
|
||||
// Session ID changed in an init message (e.g. a hook restarted Claude
|
||||
// with a new session mid-turn). Accept the new ID and continue.
|
||||
this.logger.warn(
|
||||
{ existingSessionId, newSessionId },
|
||||
"Claude session ID changed in init message; accepting new session",
|
||||
);
|
||||
this.claudeSessionId = newSessionId;
|
||||
threadStartedSessionId = newSessionId;
|
||||
}
|
||||
this.availableModes = DEFAULT_MODES;
|
||||
this.currentMode = message.permissionMode;
|
||||
|
||||
Reference in New Issue
Block a user