Skip to content

fix(acp): run each session's tools in the cwd from session/new - #1731

Merged
addyCooks merged 3 commits into
Nano-Collective:mainfrom
diyaavirmani:fix/acp-session-cwd
Oct 13, 2026
Merged

addyCooks merged 3 commits into
Nano-Collective:mainfrom
diyaavirmani:fix/acp-session-cwd

Conversation

@diyaavirmani

Copy link
Copy Markdown
Contributor

Description

Closes #1707.

ACP sessions now work in the cwd the client sends in session/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 on session/new would let sessions overwrite each other. Instead, each AcpSession gets a SessionCwdScope, and a turn runs inside it via AsyncLocalStorage. This is the same pattern plugins/host.ts already uses to keep concurrent ACP workspaces isolated.

  • While a scope is active, getSessionCwd(), getProjectRoot(), getSafeSessionCwd() and setSessionCwd() read and write the scope. Everything built on them (file tools, execute_bash including its cd capture, path validators, search tools, hooks, ACP tool-call locations) follows the session without per-tool changes. A bash cd moves only that session, and it persists across its turns.
  • Outside a scope, nothing changes: the TUI, nanocoder run and the daemon behave exactly as before.
  • runAcpConversation runs each turn in the session's scope. The ACP system prompt is built in it too, so the reported working directory and AGENTS.md come from the session's project (prompt-builder now reads getProjectRoot(), which equals process.cwd() outside a scope).
  • The git tools (execProcess, shared with gh) now spawn in getProjectRoot(), 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

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Changeset

  • Added a changeset (pnpm changeset) describing this change for the changelog

Testing

Automated Tests

  • New features include passing tests in .spec.ts/tsx files
  • All existing tests pass (pnpm test:all completes successfully)
  • Tests cover both success and error scenarios

New tests:

  • source/acp/acp-conversation.spec.ts: real read_file / write_file / execute_bash through runAcpConversation, with the session pointed at a temp project while the process runs from the checkout. Covers:
    • relative read and write resolve in the session cwd
    • pwd prints it
    • an absolute path inside it passes the boundary
    • a bash cd persists for the session without moving the process cwd
    • two sessions mid-turn at once each see their own directory
  • source/services/session-cwd.spec.ts: scope semantics. Root and cwd come from the scope, a cd stays in its scope and persists, concurrent scopes are isolated, a removed cd target 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's AGENTS.md.
  • source/tools/git/utils.spec.ts: execGit runs 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/sdk client spawning dist/cli.js --acp in launch/ with session/new cwd = project/). The read, the relative and absolute writes, pwd and the system prompt all resolve to project/ now, matching the control.

Manual Testing

  • Tested with Ollama
  • Tested with OpenRouter
  • Tested with OpenAI-compatible API
  • Tested MCP integration (if applicable)

The end-to-end check used a local scripted OpenAI-compatible endpoint, not a hosted provider.

Checklist

  • If this was for an open issue, I was assigned to it
  • Code follows project style guidelines
  • Self-review completed
  • Documentation updated (if needed)
  • No breaking changes (or clearly documented)
  • Appropriate logging added using structured logging (no new runtime paths that need logging)

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
@github-actions github-actions Bot added area:tools Tool implementations and tool-calling area:docs Documentation labels Oct 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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 cwd the client sent in session/new (and load/resume) instead of the agent's spawn directory. The implementation is correct: an AsyncLocalStorage scope per AcpSession wraps the turn, getSessionCwd/getProjectRoot/setSessionCwd/setProjectRoot/getSafeSessionCwd consult the scope, and outside a scope the global behaviour is unchanged. The regression suite is thorough — ten new tests across session-cwd, acp-conversation, prompt-builder, and git/utils exercise the spec scenarios plus concurrent sessions. One minor doc/code inconsistency is worth flagging.

⚪ nit · completeness · docs/features/acp.md:81

The doc note says project-level configuration (agents.config.json, MCP servers, custom tools and skills) is read once from the directory the agent process starts in, and recommends editors spawn the agent in the project directory. But runAcpConversation now nests withTrustedProjectPlugins(options.session.cwd, …) inside the session's cwd scope, so plugins are loaded from <session.cwd>/.nanocoder/plugins/, not the agent's launch dir. Either drop "plugins" from the list of things read from the launch dir, or note that plugins follow the session cwd while everything else (config/MCP/custom tools/skills) follows the launch dir. The PR description acknowledges this is partial (process-level config loading is out of scope), so a clearer doc line would help users understand the asymmetry.


🔴 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 /re-review.

@github-actions github-actions Bot added the agent:comments nc-review left non-blocking findings label Oct 12, 2026
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.
@diyaavirmani

Copy link
Copy Markdown
Contributor Author

@addyCooks this is ready for review. It fixes #1707: ACP turns now run in the session's cwd from session/new, through a per-session scope, while the TUI and run are unchanged. It adds 10 regression tests, the nc-review docs nit is addressed in 029e25e, and all CI checks are green.

addyCooks
addyCooks previously approved these changes Oct 13, 2026

@addyCooks addyCooks left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @diyaavirmani, nice approach.
LGTM 🎉

@diyaavirmani diyaavirmani left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed !

@addyCooks addyCooks left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a merge of main into this branch to fix a conflict with #1678 in acp-conversation.ts (the cwd scope and the read-stub scope are now nested).
No other changes.

Approving 🎉

@addyCooks
addyCooks merged commit 6bb4699 into Nano-Collective:main Oct 13, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent:comments nc-review left non-blocking findings area:docs Documentation area:tools Tool implementations and tool-calling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] ACP ignores the cwd from session/new; tools, bash and path checks use the agent's spawn directory

2 participants