Skip to content

Give agents the user's login-shell env and the worktree's direnv env (#320) - #1693

Merged
currentbits merged 3 commits into
mainfrom
coder/propagate-the-user-s-real-shell-environm-750222
Oct 19, 2026
Merged

currentbits merged 3 commits into
mainfrom
coder/propagate-the-user-s-real-shell-environm-750222

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Agents now start with the same environment a terminal opened in the worktree would have. That covers vars from ~/.zshrc, SSH/GPG agent sockets, LANG, version managers, and the worktree's direnv / use flake env. Before this, only PATH came from the login shell, so a GUI launch dropped everything else. Closes #320.

Changes

  • Login env: Solenta already runs the user's login shell at boot to capture PATH. That same run now also captures env -0, and any var the rc files added or changed lands in process.env. Every spawn builds on process.env, so agents, git and terminals all get it. Shell bookkeeping (PWD, SHLVL, …) and a GPG_TTY of "not a tty" are skipped. These vars are not written to the PATH cache, because rc files often export tokens.
  • direnv per worktree: before each run starts (after worktree setup), the runner runs direnv export json in the worktree. withProjectEnv then layers the result under project.env and the provider overlay, for all providers and workflow agents. Stop cancels the wait, remote/WSL projects are skipped, and a missing direnv or .envrc adds nothing.
  • Worktree trust: a new worktree path is always blocked by direnv. If the main checkout's .envrc is already allowed and the worktree's copy is byte-identical, Solenta runs direnv allow for the worktree. An .envrc the branch changed stays blocked.

Verification

  • New unit tests: electron/test/pathenv.test.js (env parsing, the diff/skip rules, no tokens in the cache) and electron/test/direnvEnv.test.js (a fake direnv: layering, null unsets, remote skip, allow inheritance for identical vs edited .envrc). Both pass.
  • On this Mac, the real login shell capture (zsh, ~2 s) applied 11 vars (LANG, NVM_, HOMEBREW_, …).
  • Checked against a real direnv 2.38.2 binary: the allow-hash check matches direnv's own (after realpath, e.g. /tmp → /private/tmp), and a worktree with an identical .envrc loads FOO=bar end to end.
  • tsc --noEmit: no new errors. The only remaining errors come from deps missing in my local node_modules.
  • Local test:electron: first run cut off partway. 4 files failed in it; 3 pass when rerun alone (load), and the 4th (attachments PDF) needs pdfjs-dist, which my local node_modules lacks (file untouched here). A full rerun is in progress; CI is the gate.

Notes for reviewers

  • Login values win over launch values for keys the rc changed (e.g. a 1Password SSH_AUTH_SOCK). That's intended: it's what a terminal sees.
  • A cold use flake can hold run start for up to 60 s with no work-log step. Later loads hit nix-direnv's cache.
  • Auto-allow does not check for a direnv deny on the worktree path itself; Solenta creates those paths, so I expect that to be rare.
  • GPG: gpg on PATH, GNUPGHOME and the agent socket now come through. Pinentry still needs a GUI pinentry (pinentry-mac) since agents have no TTY. Seatbelt-sandboxed threads still can't write ~/.gnupg; I left that alone on purpose.
  • A gitignored .envrc never reaches the worktree, so it isn't loaded.

🤖 Generated with Claude Code

Solenta-Message-Id: 1e89f910-54d3-4b87-b9ad-bf283d1588f3
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T08:56:34.735832Z 7151f19 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7151f199c9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread electron/direnvEnv.js
try {
// ENOENT (direnv not installed) lands in the r.err branch below.
let r = await direnv(cwd, ["export", "json"]);
if (/is blocked/.test(r.stderr) && inheritsAllow(cwd, opts.mainPath || "")) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Detect blocked envrcs without parsing configurable logs

When a user customizes DIRENV_LOG_FORMAT or sets [global] log_format = "-", the error output may omit the literal is blocked, so an identical worktree .envrc never inherits the main checkout's authorization and every agent starts without its direnv environment. Direnv explicitly allows its output format to be changed or disabled in the direnv configuration documentation; use the failed exit status plus the independently verified inheritsAllow result rather than matching human-readable stderr.

Useful? React with 👍 / 👎.

Comment thread electron/direnvEnv.js
for (const [k, v] of Object.entries(JSON.parse(r.stdout))) {
env[k] = typeof v === "string" ? v : undefined;
}
loaded.set(cwd, env);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Prevent cancelled loads from overwriting newer environments

If a user stops a run while a slow direnv evaluation is in progress and immediately starts another run in the same worktree, the stopped run's execFile continues in the background. If it completes after the newer load, this unconditional write replaces the fresh cache entry with the old environment; subsequent awaits before provider dispatch can then cause the new agent to receive stale variables. Associate each load with a generation or actually terminate/ignore cancelled evaluations before updating loaded.

Useful? React with 👍 / 👎.

Comment thread electron/runner.js
Comment on lines +2457 to +2459
if (wfProject && !isRemoteProject(wfProject)) {
await loadDirenv(wfThread.worktreePath || wfProject.path, { mainPath: wfProject.path });
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Load direnv before retrying workflow agents

The new direnv load is limited to startWorkflowRun, while retryWorkflowAgent invokes the workflow engine through a separate path. After an application restart the persisted workflow can still be retried, but the in-memory direnv cache is empty, so every withProjectEnv(..., cwd) call in the retried phase contributes no direnv variables; even without a restart, edits to .envrc since the original workflow remain stale. Apply the same load step to the retry entry point.

Useful? React with 👍 / 👎.

@currentbits
currentbits merged commit 56aebe5 into main Oct 19, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Propagate the user's real shell environment into agent sessions

1 participant