nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] New Chat can resurrect an abandoned session if clicked while session creation is pending

Open akramcodez opened this issue 1 month ago • 2 comments

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

  1. Trigger session initialization on a slow or mocked ACP connection where newSession takes several seconds.
  2. While newSession is awaiting a response, trigger nanocoder.newChat (via title bar button or Command Palette).
  3. Wait for the background newSession network call to resolve.
  4. 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 _createSession and only assign this._sessionId = result.sessionId if the generation counter has not changed upon resolution.
  • Clear this._pendingSession and bump the generation counter in newChat() and setConnection().
  • Caveat: An abandoned creation attempt's finally must not clear a newer _pendingSession; cleanup should check promise identity or generation.

akramcodez avatar Oct 12 '26 11:10 akramcodez

hey @akramcodez @addyCooks , would like to work on this could you please assign this to me.

karthikponna avatar Oct 12 '26 12:10 karthikponna

Go ahead @karthikponna !

addyCooks avatar Oct 13 '26 06:10 addyCooks