Skip to content

Commit 05241fe

Browse files
committed
fix(ai-chat): preserve SDK session across cancel and error
cancelQuery, the abort catch in the query loop, and the error catch all nulled currentSessionId, so the next prompt started a fresh SDK session and Claude saw none of the prior turn — looking like amnesia after a Stop. Aborts only leave an interrupt marker in the session log, and most errors are transient (network, rate limit), so keep the session ID and let the next prompt resume; a truly broken session will surface its own fresh error.
1 parent 2c1a17b commit 05241fe

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

src-node/claude-code-agent.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ exports.cancelQuery = async function () {
270270
if (currentAbortController) {
271271
currentAbortController.abort();
272272
currentAbortController = null;
273-
// Clear session so next query starts fresh instead of resuming a killed session
274-
currentSessionId = null;
273+
// Keep currentSessionId so the next prompt resumes the same SDK session.
274+
// Aborts leave an interrupt marker in the session log, not a corrupted state.
275275
// Clear any pending question or plan
276276
_questionResolve = null;
277277
_planResolve = null;
@@ -1256,13 +1256,11 @@ async function _runQuery(requestId, prompt, projectPath, model, signal, locale,
12561256
return;
12571257
}
12581258
_log("Cancelled");
1259-
// Send sessionId so browser side can save partial history for later resume
1260-
const cancelledSessionId = currentSessionId;
1261-
// Clear session so next query starts fresh
1262-
currentSessionId = null;
1259+
// Keep currentSessionId so the next prompt can resume the same SDK
1260+
// session — the abort just leaves an interrupt marker in the log.
12631261
nodeConnector.triggerPeer("aiComplete", {
12641262
requestId: requestId,
1265-
sessionId: cancelledSessionId
1263+
sessionId: currentSessionId
12661264
});
12671265
return;
12681266
}
@@ -1288,8 +1286,9 @@ async function _runQuery(requestId, prompt, projectPath, model, signal, locale,
12881286
}
12891287
}
12901288

1291-
// Clear session after error to prevent cascading failures from resuming a broken session
1292-
currentSessionId = null;
1289+
// Keep currentSessionId so the user can retry — errors are often
1290+
// transient (network, rate limit), and if the session really is broken
1291+
// the next attempt will surface a fresh error of its own.
12931292

12941293
nodeConnector.triggerPeer("aiError", {
12951294
requestId: requestId,
@@ -1299,7 +1298,7 @@ async function _runQuery(requestId, prompt, projectPath, model, signal, locale,
12991298
// Always send aiComplete after aiError so the UI exits streaming state
13001299
nodeConnector.triggerPeer("aiComplete", {
13011300
requestId: requestId,
1302-
sessionId: null
1301+
sessionId: currentSessionId
13031302
});
13041303
}
13051304
}

0 commit comments

Comments
 (0)