nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] Make `session/load` fail for an unknown session ID, like `session/resume`

Open addyCooks opened this issue 1 month ago • 0 comments

Description

session/load with a session ID that doesn't exist creates an empty in-memory session, replays nothing and succeeds. session/resume with the same ID fails with Session not found on disk. A client therefore can't use session/load to find out that a session is missing.

I'd like session/load to fail for an unknown ID the same way session/resume does, or, if the maintainers prefer to keep the current behavior, to tell the client that a new session was created.

Tracked from Nano-Collective/nanocoder-desktop#12 (item 8). It is a nice-to-have there, because the desktop works around it by always using session/resume. This issue is also a question about whether the current behavior is intended.

Use Case

A client that has stored a session ID (an editor thread, or Nanocoder Desktop's durable threads) needs to know when that session no longer exists. For example, Nanocoder deletes sessions after 30 idle days or beyond 100 sessions by default, and a user can delete a session by hand.

With session/load, a missing session looks like success: the client gets an empty conversation and carries on as if history had been lost. Worse, if the client then sends a prompt, the turn is saved to disk under that ID, so a stored session now exists under an ID the client invented.

session/resume already reports this correctly. Nanocoder Desktop relies on that error: on Session not found on disk it marks the thread view-only and offers recovery actions. That is why the message (or a stable code) matters, and why it should be identical for load.

The difference between the two methods is easy to miss, and the ACP spec doesn't say which behavior is right.

Proposed Solution

In loadSession, when the ID is neither in memory nor on disk, throw the same error as resumeSession instead of registering a new session. Keep resumeSession exactly as it is, including its message, because clients already rely on it.

Behavior to keep:

  • A session that is in memory still loads and replays its history.
  • A session on disk still loads, replays and restores its messages.
  • newSession, resumeSession and closeSession behave as before.

Alternatives Considered

  • Keep creating the session, but say so. Return _meta: {"nanocoder/created": true} (or similar) from session/load when the ID was unknown. It's non-breaking for any client that relies on the current behavior, but it leaves a way to create a session with an arbitrary ID.
  • Do nothing. Clients that need to detect a missing session use session/resume, as Nanocoder Desktop does today.
  • Make both methods return a stable error code (for example the SDK's RequestError.resourceNotFound, -32002) instead of a plain Error. This is a separate improvement that helps both methods, see the questions below.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] This feature aligns with the project's goals (local-first AI assistance)

Current behavior, checked against main at fc01e23:

  • source/acp/acp-agent.ts (~L167-196): loadSession reuses an in-memory session, or loads from disk (sessionManager.loadSession) and calls registerSession(...) whether or not the file exists. It restores messages only when a persisted session was found, then replays history and returns modes and config options.
  • source/acp/acp-agent.ts (~L674-705): resumeSession throws Error('Session not found on disk: <id>') when sessionManager.loadSession returns nothing, and otherwise evicts any stale in-memory session, registers it from the persisted working directory, and replays.
  • source/acp/acp-agent.ts (~L471): after a turn, saveAcpSessionToDisk(session) runs in finally, so a prompt to an unknown loaded ID is saved to disk.
  • source/acp/acp-agent.spec.ts (~L226): the test "AcpAgent.loadSession - creates a usable session for an unknown id" asserts the current behavior and sends a prompt to the new session.
  • plugins/vscode/src/acp-client.ts: the VS Code extension calls only resumeSession, never session/load.
  • The ACP session-setup docs describe replay for session/load but don't say what either method should do for an unknown ID. Only session/close says an agent MAY return an error when the session doesn't exist.
  • Both "not found" errors are plain Errors, so a client can only recognize them by their message text.

Implementation Notes (optional)

The change is small: after sessionManager.loadSession(params.sessionId), if there is no persisted session and none in memory, throw before registerSession. The error text can reuse resume's Session not found on disk: <id> so clients that already recognize it keep working. #1889 will also touch loadSession, so the two changes should be coordinated.

Open questions for maintainers:

  1. Is the current behavior intended? The existing test suggests it was deliberate. If some client relies on session/load creating a session, the _meta alternative above avoids breaking it.
  2. Error shape. Keep the plain Error message, or move both load and resume to a stable error code so clients don't match message text?
  3. Should session/load ever create a session? ACP says only session/new creates one.

Acceptance criteria:

  • [ ] session/load with an ID that is neither in memory nor on disk fails and creates no session, in memory or on disk.
  • [ ] session/load for a session on disk still replays history and restores messages.
  • [ ] session/load for a session in memory still replays its history.
  • [ ] The failure carries the same message as session/resume's, so a client can treat them alike.
  • [ ] session/resume behavior and its error are unchanged.
  • [ ] The existing test that expects a usable session for an unknown ID is changed to expect the failure.
  • [ ] docs/features/acp.md describes what load and resume do for an unknown ID.

Test plan:

  • ACP spec: loadSession with an unknown ID rejects, and a following prompt for that ID also fails with "Session not found".
  • ACP spec: no file is written for the unknown ID.
  • ACP spec: loading a persisted session and an in-memory session still replay.
  • ACP spec: both loadSession and resumeSession reject an unknown ID with the same message, checked side by side so the two stay consistent.

Out of scope: how cwd is handled on load versus resume, whether resume should replay history, honoring mcpServers (#1889), and any change to session retention.

addyCooks avatar Oct 26 '26 17:10 addyCooks