Repository navigation
fix(acp): run each session's tools in the cwd from session/new - #1731
Conversation
ACP stored the cwd a client sends in session/new but never used it: file tools, execute_bash, the project path boundary, the git tools and the system prompt all resolved against the directory the agent process was spawned in. When an editor started the agent elsewhere, relative paths read and wrote the wrong files and absolute paths inside the real project were rejected, although the ACP spec requires the session cwd to be used regardless of where the agent was spawned. The working directory was process-global, and one ACP process can serve several sessions, so each AcpSession now owns a SessionCwdScope and its turns run inside it through AsyncLocalStorage (the pattern plugins/host already uses for ACP workspaces). While a scope is active the session-cwd getters and setters read and write it, so every tool built on them follows the session, and a bash cd moves only that session. Outside a scope nothing changes, so the TUI, `run` and the daemon behave as before. The ACP system prompt is built inside the scope as well, so the reported working directory and AGENTS.md come from the session's project, and the git tools spawn in the project root so they act on the session's repository. Closes Nano-Collective#1707
nc-review: comments — 1 nit@diyaavirmani — a few things worth a look, none blocking. The PR fixes a real ACP spec violation: tools, bash, the path boundary, the git tools, and the system prompt now follow the ⚪ nit · The doc note says project-level configuration ( 🔴 blocking · 🟠 a reviewer would ask for a change · ⚪ optional Automated code review — correctness, security, design, tests, plus duplicates and scope. A human still decides; this is not a substitute for review and is not exhaustive. The required status checks separately cover lint, formatting, types, unused dependencies, the test suite and the build. This bot never merges. Maintainers can rerun with |
Address review: project config, MCP servers, custom tools and skills come from the agent's start directory, while .nanocoder/plugins/ already loads per session from its cwd.
|
@addyCooks this is ready for review. It fixes #1707: ACP turns now run in the session's |
addyCooks
left a comment
There was a problem hiding this comment.
Thanks @diyaavirmani, nice approach.
LGTM 🎉
Description
Closes #1707.
ACP sessions now work in the
cwdthe client sends insession/new(and on load/resume) instead of the directory the agent process was spawned in. This is what the ACP spec requires ("MUST be used for the session regardless of where the Agent subprocess was spawned", "MUST remain the base for relative-path resolution").Approach. The working directory was process-global (
services/session-cwd.ts), and one ACP process can serve several sessions. Pinning it onsession/newwould let sessions overwrite each other. Instead, eachAcpSessiongets aSessionCwdScope, and a turn runs inside it viaAsyncLocalStorage. This is the same patternplugins/host.tsalready uses to keep concurrent ACP workspaces isolated.getSessionCwd(),getProjectRoot(),getSafeSessionCwd()andsetSessionCwd()read and write the scope. Everything built on them (file tools,execute_bashincluding itscdcapture, path validators, search tools, hooks, ACP tool-call locations) follows the session without per-tool changes. A bashcdmoves only that session, and it persists across its turns.nanocoder runand the daemon behave exactly as before.runAcpConversationruns each turn in the session's scope. The ACP system prompt is built in it too, so the reported working directory andAGENTS.mdcome from the session's project (prompt-buildernow readsgetProjectRoot(), which equalsprocess.cwd()outside a scope).execProcess, shared withgh) now spawn ingetProjectRoot(), so they act on the session's repository. Unchanged for the TUI.docs/features/acp.md: replaced the "editor spawns the agent in your project directory" assumption with what now holds, and noted that project-level configuration (agents.config.json, MCP servers, custom tools, skills) is still read once from the directory the agent starts in.Out of scope: the extension-side handling of
nanocoder.cwd(#1720) and process-level config loading.Type of Change
Changeset
pnpm changeset) describing this change for the changelogTesting
Automated Tests
.spec.ts/tsxfilespnpm test:allcompletes successfully)New tests:
source/acp/acp-conversation.spec.ts: realread_file/write_file/execute_bashthroughrunAcpConversation, with the session pointed at a temp project while the process runs from the checkout. Covers:cwdpwdprints itcdpersists for the session without moving the process cwdsource/services/session-cwd.spec.ts: scope semantics. Root and cwd come from the scope, acdstays in its scope and persists, concurrent scopes are isolated, a removedcdtarget falls back to the scope root, and nothing leaks outside.source/utils/prompt-builder.spec.ts: inside a scope, the prompt reports the session directory and appends that project'sAGENTS.md.source/tools/git/utils.spec.ts:execGitruns in the session's repository inside a scope.Reverting only the production changes makes all 10 new tests fail. The ACP ones fail with the exact symptom from #1707 (
File "marker.txt" does not exist).I also re-ran the end-to-end repro from #1707 (the locked
@agentclientprotocol/sdkclient spawningdist/cli.js --acpinlaunch/withsession/newcwd = project/). The read, the relative and absolute writes,pwdand the system prompt all resolve toproject/now, matching the control.Manual Testing
The end-to-end check used a local scripted OpenAI-compatible endpoint, not a hosted provider.
Checklist