[Bug] New Chat can resurrect an abandoned session if clicked while session creation is pending
Description
When session creation is in flight (NanocoderAcpClient.getOrCreateSession() awaiting this.connection.newSession()), triggering "New Chat" clears this._sessionId = undefined. However, the pending promise is not cancelled or invalidated. When _createSession resolves, it writes the old session ID back into this._sessionId and returns it to any callers awaiting _pendingSession. The client then continues using the session the user intended to abandon.
Environment
- OS: Linux (reviewed); macOS / Windows potentially affected
- Node version: >= 22.0.0
- Nanocoder version: 1.31.0
- Provider: Any
- Model: Any
Steps to Reproduce
- Trigger session initialization on a slow or mocked ACP connection where
newSessiontakes several seconds. - While
newSessionis awaiting a response, triggernanocoder.newChat(via title bar button or Command Palette). - Wait for the background
newSessionnetwork call to resolve. - Inspect the session ID returned by
getOrCreateSession()in a mocked reproduction, or inspect the session ID sent with the next ACP prompt.
Expected Behavior
Triggering New Chat should invalidate both the cached session ID and any in-flight session creation promises. Responses from abandoned creation attempts should be discarded, and subsequent prompts should initialize a fresh session.
Actual Behavior
The pending promise completes and executes this._sessionId = result.sessionId, writing the abandoned session ID back into the client state.
Logs/Screenshots
(Illustrative trace from mocked connection reproduction)
[Extension] New chat started — session cleared.
[ACP] newSession resolved: session-8f3a9b
[Extension] getOrCreateSession returned stale session: session-8f3a9b
Additional Context
- [x] No matching open issue found in issue search performed on 2026-10-12
- [x] Reproduced using a mocked ACP connection
- [x] Relevant code paths inspected at commit
d5ff9c37
Implementation Notes (optional)
- File:
plugins/vscode/src/acp-client.ts:204-235 - Introduce a monotonic request/generation counter (
_sessionGeneration++). - Capture the current generation at the start of
_createSessionand only assignthis._sessionId = result.sessionIdif the generation counter has not changed upon resolution. - Clear
this._pendingSessionand bump the generation counter innewChat()andsetConnection(). - Caveat: An abandoned creation attempt's
finallymust not clear a newer_pendingSession; cleanup should check promise identity or generation.
hey @akramcodez @addyCooks , would like to work on this could you please assign this to me.
Go ahead @karthikponna !