Apply the chat directory's environment and account to Claude Code chats

Claude Code chats now start with the chat directory's own environment:
direnv (allowed .envrc only, via direnv export json) and mise (mise env
--json), cached per directory and invalidated when config, watched files or
allow records change. A blocked .envrc is logged and shown in the chat as a
notice and never applied. When the directory chooses the Claude account
(CLAUDE_CONFIG_DIR, API key or token variables), the server's service token
and login-file fallbacks are skipped and inherited credentials dropped.
Transcripts in a directory's CLAUDE_CONFIG_DIR are found for history,
resume and after a restart. PWD is set to the chat directory.
This commit is contained in:
2026-11-02 16:37:20 +00:00
parent d78cbe3f66
commit b4382b08ad
11 changed files with 1139 additions and 13 deletions
+58 -1
View File
@@ -199,7 +199,9 @@ $EDITOR ~/.config/pipelines/claude-token
`PIPELINES_CLAUDE_TOKEN_FILE` names another file. The file must be a regular,
owner-only file. The startup report shows `Claude service token present` when
it is read. An `ANTHROPIC_API_KEY`, `CLAUDE_CODE_OAUTH_TOKEN` or
`CLAUDE_CONFIG_DIR` in the server's environment takes precedence.
`CLAUDE_CONFIG_DIR` in the server's environment takes precedence, and a chat
directory that chooses its own account wins over all of these (see
[Per-directory accounts and environment](#per-directory-accounts-and-environment)).
**Models:** the catalog comes from the CLI; switching applies live and is kept
for the next turn.
@@ -228,6 +230,61 @@ approval cards.
**Mid-turn messages:** yes, with `turn/steer` pinned to the running turn. If
the turn already ended, the message is queued for the next turn instead.
## Per-directory accounts and environment
A chat runs in the directory you choose for it. For Claude Code the server
also applies that directory's own environment, so the chat behaves as if you
had opened a terminal there and started `claude`:
- **Working directory.** The `claude` process starts in the chat directory
with `PWD` set to it. Codex threads are started with that `cwd` and OpenCode
sessions with that location, so every harness runs tools there.
- **Git identity.** `includeIf "gitdir:..."` rules in your git config apply
automatically: git resolves them from the working directory of a repository
under the chosen directory.
- **direnv.** When `direnv` is installed (the service's `PATH`, then
`/opt/homebrew/bin`, `/usr/local/bin`, `~/.local/bin`, `~/.nix-profile/bin`)
the server runs `direnv export json` in the chat directory, so an `.envrc`
there or in a parent applies. Only an `.envrc` you already allowed is used;
the server never runs `direnv allow`. A blocked `.envrc` is logged and shown
in the chat as a "Directory environment" notice, and nothing from it is
applied.
- **mise.** When `mise` is installed the server merges `mise env --json`
(untrusted config is reported the same way). When both set a variable,
direnv wins.
- A plain `.env` file is not read on its own; use an `.envrc` (`dotenv` loads
one) so the allow check applies.
The result is merged onto the server's environment and cached per directory;
editing an `.envrc`, a mise config, a file direnv watches, or allowing or
denying an `.envrc` makes the next chat process read it again. Set
`PIPELINES_DIRECTORY_ENV=off` in the server's environment to turn this off.
**Choosing the Claude account per directory.** When the directory's
environment sets or unsets any of `CLAUDE_CONFIG_DIR`, `CLAUDE_CODE_OAUTH_TOKEN`,
`ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN`, `CLAUDE_SECURESTORAGE_CONFIG_DIR`,
`CLAUDE_CODE_USE_BEDROCK` or `CLAUDE_CODE_USE_VERTEX`, that directory owns the
account: the service token and the `~/.claude` login-file fallback are not
added, and inherited server credentials (`CLAUDE_CODE_OAUTH_TOKEN`,
`ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN`) are dropped unless the directory
sets them. Transcripts kept in a directory's `CLAUDE_CONFIG_DIR` are found for
history and resume, also after a restart. For example:
```sh
# ~/work/.envrc (then run `direnv allow ~/work`)
export CLAUDE_CONFIG_DIR="$HOME/work/.claude-profile"
export GH_CONFIG_DIR="$HOME/work/.gh"
```
Sign that profile in once from a terminal in that directory (`claude`, then
`/login`). Shell functions or aliases in `~/.zshrc` that switch accounts by
`$PWD` are not seen by the server, which does not run your shell; move them
into an `.envrc`.
Codex and OpenCode chats share one App Server or `opencode serve` process
across directories, so only the working directory applies to them; their
account is the one that process uses.
## Adopting existing sessions
```sh