Skip to content

fix(acp): check directory trust for each session's cwd - #2071

Merged
will-lamerton merged 1 commit into
mainfrom
fix/acp-session-cwd-trust
Nov 11, 2026
Merged

will-lamerton merged 1 commit into
mainfrom
fix/acp-session-cwd-trust

Conversation

@will-lamerton

Copy link
Copy Markdown
Member

What was wrong

acp-server.ts checks directory trust for process.cwd(), the folder nanocoder --acp starts in. After that, newSession(params.cwd), loadSession(params.cwd) and resumeSession (the saved workingDirectory) accepted any folder the editor sent. A session in an untrusted folder then:

  • ran tools and execute_bash there (turns run inside runInSessionCwdScope(session.cwd));
  • put that folder's AGENTS.md into the system prompt.

None of that went through the trust prompt. (Project plugins were already gated per folder by withTrustedProjectPlugins.)

Fix (this PR)

  • AcpInitContext.launchDirectory is set by the server once its own trust check has passed.
  • newSession, loadSession (when it registers from disk) and resumeSession call assertDirectoryTrusted(cwd) before registering a session:
    • the launch directory passes on the server's own check, --trust-directory included;
    • any other folder must pass ensureDirectoryTrust, so NANOCODER_TRUST_DIRECTORY=1 works as it does for the server.
  • A refusal is a RequestError with code -32003 (ACP_TRUST_REQUIRED_ERROR_CODE, the per-session form of the server's exit code 3). Its data is {"nanocoder/trustRequired": {cwd, status}}, with status worded as nanocoder trust --status words it. A client can run nanocoder trust --status --json / --grant <fingerprint> in that folder and retry, the same flow it uses for exit code 3.
  • docs/features/acp.md documents the error.

Not fixed here (phase 2)

Project-level config is still read from the launch directory, not the session's cwd. getAppConfig() is a process-wide singleton built from process.cwd(). That covers:

  • loadHierarchicalConfig in config/index.ts;
  • mcp-config-loader.ts (.mcp.json and agents.config.json);
  • getClosestConfigFile in config/paths.ts;
  • the project .env loaded at import time.

So a session in folder B still gets folder A's providers, hooks, MCP servers, alwaysAllow, sandbox and retry limits, while its prompt and AGENTS.md come from B. A real fix needs:

  1. The loaders take a cwd, and loadAppConfig(cwd) is cached per project root.
  2. getAppConfig() resolves through the session-cwd AsyncLocalStorage scope that ACP turns already run in, falling back to the process config outside it.
  3. Per-session config MCP servers are connected (today only the launch directory's are, at init), and per-session .env is handled.
  4. prompt() reads alwaysAllow inside the session scope.

That reaches into every getAppConfig() caller and MCP lifecycle, so I left it out of this PR. Trust-checking the session folder first is the prerequisite: once folder B is required to be trusted, loading B's config is safe.

Testing

New acp-agent.spec.ts tests:

  • An untrusted folder is refused with code -32003 and the trustRequired data, while the launch folder starts without a trust entry. Fails on main.
  • A trusted folder starts.
  • load and resume refuse a saved session whose folder is no longer trusted, and register nothing. Fails on main.

The full acp-agent.spec.ts and acp-capabilities.spec.ts pass (137 tests). tsc --noEmit and biome check pass. Existing tests build the agent without launchDirectory, which skips the check.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the area:docs Documentation label Nov 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

nc-review could not produce a verdict this run (agent exit 1). This is a problem with the review agent, not with your pull request. A maintainer can retry with /re-review.

@will-lamerton
will-lamerton force-pushed the fix/acp-session-cwd-trust branch from 64c8f48 to 457fb77 Compare November 11, 2026 15:07
acp-server.ts checks trust only for the directory the server starts in.
newSession, loadSession and resumeSession then accepted any cwd an editor
sent, so a session in an untrusted folder ran tools and bash there and
put its AGENTS.md in the prompt.

Sessions in any folder other than the launch directory are now refused
unless that folder is trusted. The refusal is a RequestError with code
-32003 and nanocoder/trustRequired {cwd, status} data, so a client can
run `nanocoder trust --status/--grant` there and retry. The launch
directory keeps the server's own check, --trust-directory included.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@will-lamerton
will-lamerton force-pushed the fix/acp-session-cwd-trust branch from 457fb77 to 367f347 Compare November 11, 2026 15:07
@will-lamerton
will-lamerton merged commit eef140a into main Nov 11, 2026
16 of 17 checks passed
@will-lamerton
will-lamerton deleted the fix/acp-session-cwd-trust branch November 11, 2026 15:08
jay23606 added a commit to jay23606/nanocoder that referenced this pull request Nov 12, 2026
…ve#2071)

acp-server.ts checks trust only for the directory the server starts in.
newSession, loadSession and resumeSession then accepted any cwd an editor
sent, so a session in an untrusted folder ran tools and bash there and
put its AGENTS.md in the prompt.

Sessions in any folder other than the launch directory are now refused
unless that folder is trusted. The refusal is a RequestError with code
-32003 and nanocoder/trustRequired {cwd, status} data, so a client can
run `nanocoder trust --status/--grant` there and retry. The launch
directory keeps the server's own check, --trust-directory included.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs Documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant