[Feature] Keep model and provider selection per ACP session instead of per process
Description
In ACP mode every session in a Nanocoder process shares one LLM client, one provider and one model. Changing the model or provider through session/set_config_option in one session changes it for all of them, including a turn that is running in another session. A saved session also records whatever model the shared client has at that moment, and session/resume ignores the model the session was saved with.
I'd like model and provider selection to belong to the session, so each ACP session keeps its own, with single-session behavior unchanged. Clients should be able to tell that the agent supports this.
Tracked from Nano-Collective/nanocoder-desktop#12 (item 11). It is a "later" item there, because the desktop works around it by running one root turn per process.
Use Case
A client that shows several conversations at once, such as Nanocoder Desktop (one process per project, many threads), wants each thread to keep its own model, and wants to run more than one turn at the same time. Today it must refuse a second turn while one is running, label the model control "applies to the whole project", disable model changes while any turn runs, and accept that:
- a model change in one thread changes every thread;
- a resumed thread runs on the process's current model, not the one it last used;
- a session's stored model can be wrong.
The code already says this is a known limit: a comment in setSessionConfigOption notes that a multi-session client "would see one shared model".
This is a prerequisite for parallel root turns in the desktop, which is why it is not urgent. It also fixes the stale stored model for any client.
Proposed Solution
- State on the session.
AcpSessionholds its own provider and model. New sessions start from the process default (see the open question about what that is). - Client per session, created only when needed. A session uses the shared default client until it picks a different provider or model, then gets its own client from
createLLMClient(provider, model). Single-session use creates nothing extra. session/set_config_optionchanges only that session. It no longer mutates the shared client, and returns that session's config options.buildConfigOptionsis built from the session's own state, including the options returned bysession/new,session/loadandsession/resume.- Mid-turn changes apply from the next turn. The running turn keeps the model it started with.
- Persist correctly.
saveAcpSessionToDiskwrites the session's own provider and model. - Resume and load restore them. If the saved provider and model are still available, use them. Otherwise fall back to the process default and say so in the response.
- Rebuild the system prompt when the model or provider changes, since the prompt and tool mode depend on the model.
- Advertise it. Report per-session model state under
agentCapabilities._meta(for examplenanocoder.perSessionModel: true), so a client can enable per-thread model controls only when the agent supports them. This follows the advertising scheme in #1893.
Alternatives Considered
- Do nothing. Clients keep running one turn at a time and treat the model as project-wide, as the desktop does today.
- One process per session. Isolates everything, but it multiplies memory and start-up cost, and the process-wide MCP and tool setup is repeated.
- Always create a client per session. Simpler to reason about, but it adds cost for the common single-session case.
- No advertisement, clients check the version. Less to build, but a client cannot tell whether per-session models are in effect, and a wrong guess silently changes every session's model.
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-types.ts:AcpInitContexthas oneclient,providerandmodelfor the process.source/acp/acp-agent.ts(~L566-600): theproviderconfig option creates a new client, replacesinitContext.clientfor every session, callssetAutoCommitClient(newClient)(also process-wide), and callsupdateLastUsed(...).source/acp/acp-agent.ts(~L602-629): themodeloption callsclient.setModel(modelId)on the shared client, setsinitContext.model, and callsupdateLastUsed(...). A comment at ~L619-621 states the model is "effectively process-global".source/acp/acp-conversation.ts(~L365): each model call usesclient.chat, so a change applies to the next call even inside another session's running turn.source/acp/acp-agent.ts(~L990-1040):buildConfigOptionsreports the shared client's model and provider to every session.source/acp/acp-agent.ts(~L1118-1120, L1148-1149):saveAcpSessionToDiskwrites provider and model from the shared client.source/acp/acp-agent.ts(~L674-705):resumeSessionrestores messages and the working directory but not the saved provider or model.source/acp/acp-agent.ts(~L1042-1089):buildSystemPromptForSessionderives the prompt, tool mode and tuning from the process-wide model.setSessionConfigOptiondoes not call it after a change, whilesetSessionModedoes.source/client-factory.ts(~L32):createLLMClient(provider, model, ...)can build a client for a given provider and model.
Implementation Notes (optional)
AcpSession can carry provider, model and an optional client that defaults to the shared one. Pass session.client ?? initContext.client into the conversation runner and the system-prompt builder instead of reading initContext.client. Single-session behavior then stays the same.
Other state derived from the model looks process-wide and needs a check: the tuning and compaction settings applied in buildSystemPromptForSession, the last-built-prompt record, and the auto-commit client.
This pairs with other work on parallel turns: session-keyed subagent approvals (#1105) and per-session tool registries (#1889). It does not depend on them, but a client needs all of them before it can safely run turns in parallel.
Open questions for maintainers:
- Lazy or eager per-session client? I propose lazy, to keep the single-session path unchanged.
- What does a new session start with? The process default, or the most recent selection made in any session?
- What should
updateLastUsedrecord? Today every change rewrites the user's last-used provider and model. A per-session change might update only the default for new sessions, or nothing. - Auto-commit and other process-wide clients.
setAutoCommitClientis global. Should it follow the most recent change, stay on the default, or become per session? - Resume when the saved model is gone. Fall back silently, or report it in the response?
- Memory. How many distinct clients is acceptable per process?
Acceptance criteria:
- [ ] Two sessions can hold different models (and providers) at the same time.
- [ ] Changing the model or provider in one session does not change the other session's config options, client or running turn.
- [ ] The config options returned by
session/new,session/load,session/resumeandsession/set_config_optionare the session's own. - [ ] A change made during a running turn applies from the next turn.
- [ ]
saveAcpSessionToDiskrecords the session's own provider and model. - [ ]
session/resumeandsession/loadrestore the saved provider and model when available, and otherwise fall back to the process default. - [ ] The system prompt is rebuilt when the model or provider changes.
- [ ] The agent advertises per-session model state under
agentCapabilities._meta. - [ ] A client that uses one session sees the same behavior as today.
- [ ] Errors for unknown providers and models are unchanged.
- [ ] Docs describe per-session model selection.
Test plan:
- ACP spec: two sessions, change the model in one, and check the other's
buildConfigOptionsand client are unchanged. - ACP spec: a model change during a turn does not affect that turn and applies to the next.
- ACP spec: saved sessions record their own model, and resume restores it.
- ACP spec: resume with a model that is no longer available falls back to the default.
- ACP spec:
initializeadvertises per-session model state. - Regression: single-session flows and unknown-model errors behave as before.
Out of scope: running turns in parallel safely (approval routing, subagent events and tool registries are tracked in #1105, #1891 and #1889), changing how providers are configured, and the CLI's own model switching.