Skip to content

refactor(acp): load project config per session folder via SessionContext - #2100

Merged
will-lamerton merged 2 commits into
mainfrom
refactor/session-context-config
Nov 11, 2026
Merged

will-lamerton merged 2 commits into
mainfrom
refactor/session-context-config

Conversation

@will-lamerton

Copy link
Copy Markdown
Member

First step of moving per-session state out of process globals so one ACP process can serve sessions in different folders correctly.

What was wrong

nanocoder --acp is one process hosting many sessions, and each session names its own folder (cwd in session/new). Since #1707 its tools, bash, path boundary, system prompt and AGENTS.md follow that folder, but every config loader read process.cwd(). So a session opened in project B, from an agent spawned in project A, ran B's tools under A's alwaysAllow, disabledTools, lifecycle hooks, formatters, sandbox, firewall, autoCommit, auto-compact / retry limits, systemPrompt, tune and provider list.

Concrete case: A has alwaysAllow: ["write_file"], B has alwaysAllow: ["read_file"]. A session in B advertised and applied ["write_file"].

The change

  • services/session-context.ts (new): SessionContext held in AsyncLocalStorage. This is the scope that session-cwd.ts already had (projectRoot, sessionCwd), moved into its own module so later steps can add more per-session state to it. session-cwd.ts keeps its getters and setters and reads the context. Renames: SessionCwdScope → SessionContext, createSessionCwdScope → createSessionContext, runInSessionCwdScope → runInSessionContext, AcpSession.cwdScope → AcpSession.context.
  • Config loaders take an explicit cwd: loadHierarchicalConfig and all the loaders behind loadAppConfig, plus loadAllProviderConfigs, loadAllMCPConfigs, loadProjectMCPConfig, getClosestConfigFile and loadDefaultMode. The public ones default to getConfigRoot(), which is the active session's folder, or process.cwd() outside a session.
  • getAppConfig() keeps the launch-directory config exactly as before. Inside a session context it returns that folder's config, cached per canonical root (realpath, so /var/... and /private/var/... are the same folder). A session in the launch folder shares the launch config object. reloadAppConfig / clearAppConfig drop the per-root entries too.
  • ACP: every request that acts on one session (session/new, load, resume, prompt, set_mode, set_config_option) now runs inside that session's context. Each public method is a thin wrapper around the old body, which moved into a private method. The trust check (fix(acp): check directory trust for each session's cwd #2071) still runs before the context is entered.
  • acp-conversation.ts changes only at its one call site (import plus runInSessionContext(options.session.context, …)). It does not conflict with the loop-engine refactor.

Behaviour

  • TUI, --plain, daemon: no change. They never enter a context, so every loader still reads process.cwd(), and the launch config is cached and reloaded as before.
  • ACP: changed (changeset, minor). A session in another folder reads that project's agents.config.json. Still set up once from the launch folder: the starting provider/model, .mcp.json server connections, custom tools, commands and skills. These are documented in docs/features/acp.md and are follow-ups.

Most of the config/index.ts line count is biome re-wrapping the loadHierarchicalConfig(cwd, …) calls. git diff -w shows 177+/79-.

Tests

  • New config/session-root-config.spec.ts: a session reads its own folder's providers / MCP / alwaysAllow / defaultMode / getClosestConfigFile; the launch config is unchanged outside a session; a session in the launch folder (including through a symlink) shares the object; concurrent sessions in two folders keep their own; clearing drops session roots.
  • New ACP spec: two sessions in one process, one in another project, each advertise their own alwaysAllow.
  • Existing specs rewritten to keep the same assertions through the new seam:
    • Several acp-agent.spec.ts tests mutated the launch config but opened sessions at the placeholder cwd: '/tmp'. They now open sessions in the launch dir (testConfigDir). These are the withProviders tests, the approval-policy test, the disabled-tools test and SESSION_CWD.
    • The acp-conversation.spec.ts auto-commit test now enables autoCommit in the session repo's own agents.config.json, not on the launch config. It asserts the same commit message.
    • config-schema.spec.ts: the loader scraper now matches the leading cwd, argument. The sanity assertions are unchanged.
  • Ran: tsc --noEmit, biome, knip (both passes), AVA for source/acp, source/config, source/services, source/plain, source/models, tools/git, utils/prompt-builder, plus the full AVA suite. The only failures were the CLI integration specs (need dist/) and app-container "narrow boot summary places branch on its own line". That one fails on origin/main too when the branch name is this long, so it is unrelated.

Next in the sequence

  1. Move the remaining process-global per-session state into the context: session-override, tuneAggressiveCompact / compressionBackup, lastBuiltPrompt, the subagent executor's cwd, NANOCODER_SESSION_ID.
  2. Replace the import-time .env load in config/index.ts with an explicit bootstrap.
  3. A separate small fix: session/resume right after session/new returns "not found", because a new session isn't written to disk until its first turn ends.

🤖 Generated with Claude Code

Generalise the AsyncLocalStorage scope in session-cwd.ts into a
SessionContext module (services/session-context.ts). Config loaders
(loadHierarchicalConfig and every loader behind loadAppConfig,
loadAllProviderConfigs, loadAllMCPConfigs, getClosestConfigFile,
loadDefaultMode) now take an explicit cwd, defaulting to the active
session's folder or process.cwd() outside one. getAppConfig caches
config per canonical root; a session in the launch folder shares the
launch config object.

ACP requests that act on a session (new, load, resume, prompt, set
mode, set config option) now run inside the session's context, so a
session in another folder reads that project's agents.config.json.
TUI and --plain run outside any context and behave as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions github-actions Bot added area:tools Tool implementations and tool-calling area:docs Documentation labels 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.

The roots are the launch folder or a session folder that already passed
the trust check, as for the existing project-config joins.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@will-lamerton
will-lamerton merged commit 07b88e9 into main Nov 11, 2026
32 of 33 checks passed
@will-lamerton
will-lamerton deleted the refactor/session-context-config branch November 11, 2026 16:36
jay23606 added a commit to jay23606/nanocoder that referenced this pull request Nov 12, 2026
…ext (Nano-Collective#2100)

* refactor(acp): load project config per session folder via SessionContext

Generalise the AsyncLocalStorage scope in session-cwd.ts into a
SessionContext module (services/session-context.ts). Config loaders
(loadHierarchicalConfig and every loader behind loadAppConfig,
loadAllProviderConfigs, loadAllMCPConfigs, getClosestConfigFile,
loadDefaultMode) now take an explicit cwd, defaulting to the active
session's folder or process.cwd() outside one. getAppConfig caches
config per canonical root; a session in the launch folder shares the
launch config object.

ACP requests that act on a session (new, load, resume, prompt, set
mode, set config option) now run inside the session's context, so a
session in another folder reads that project's agents.config.json.
TUI and --plain run outside any context and behave as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* chore(config): mark session-root config joins for semgrep

The roots are the launch folder or a session folder that already passed
the trust check, as for the existing project-config joins.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

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 area:tools Tool implementations and tool-calling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant