aft icon indicating copy to clipboard operation
aft copied to clipboard

bash: per-workdir environment provider for version managers (mise/direnv/asdf)

Open gaojunran opened this issue 2 weeks ago • 2 comments

Description

Let a user-tier configuration entry supply environment variables (including PATH) that follow the directory a bash call runs in. Version managers (mise, direnv, asdf, conda) select toolchain versions per directory; today AFT resolves them once, process-wide.

Current behaviour

effective_path.rs probes the login shell ($SHELL -lic) at first use, caches the result in <storage_dir>/effective-path.json (shell startup files are recorded as inputs and invalidate the cache on size/mtime change — documented in docs/config.md), and exposes it through initialize_process_path() -> &'static OsStr. That value is process-wide: the OnceLock is resolved once per AFT process, independent of the workdir of any individual call. In sandbox mode sandboxed_child_environment() even pins PATH to effective_path().

Consequences for a version manager:

  • The version selected by the login shell's initial directory (usually $HOME) wins for every call, regardless of workdir.
  • [env] blocks from the project's config (author identity, package registry, etc.) never apply.

Reproduction (mise, node pinned per project):

$ cat ~/Work/mise.toml
[tools]
node = "22"

$ mkdir -p /tmp/repro && printf '[tools]\nnode = "20"\n' > /tmp/repro/mise.toml
$ mise trust /tmp/repro
$ mise current          # in /tmp/repro
node 20.20.2
$ bash -c 'node -v && echo $PATH'   # AFT bash call with workdir=/tmp/repro
v22.23.0                             # global version; /tmp/repro's toolset is ignored

Why the existing escape hatches do not cover this

  • Per-call env and bash.env refuse PATH (bash_env.rs, BLOCKED_ENV_VARS), and a refused name fails the whole call (blocked_env_var). This is correct for the stated reason: a request-supplied environment reaches the outer launcher /bin/sh supervisor, which execs before Landlock/Seatbelt is installed.
  • Running the version manager inside the command (BASH_ENV hook that evals mise hook-env) works, but pays a full environment calculation on every call: measured 22.4 ms per command in a mise project (mise itself: 18.7–20 ms; AFT wrapper: ~2 ms), versus 2.1 ms outside a project.

Proposal

A user-level (never project-level) environment provider, run with the call's workdir and cached:

{
  "bash": {
    "env_provider": {
      "command": ["mise", "env", "--json"],
      "timeout_ms": 2000,
      "cache": { "inputs": ["mise.toml", ".mise.toml"], "ttl_ms": 60000 }
    }
  }
}
  • Output is a JSON object of name -> string | null; null removes an inherited name (version managers do unset JAVA_HOME, RUSTUP_HOME, …). mise env --json does not emit nulls today, so removals would need either a thin wrapper or a contract that also accepts an explicit unset list; the shape is yours to choose.
  • PATH is allowed here only: the value comes from the user's own configuration, the same trust level as the login-shell profile AFT already executes — not from a project file and not from a request.
  • Project-tier configuration and per-call env keep the current deny list. sandboxed_child_environment() keeps pinning PATH unless the design decides otherwise.
  • Cache key: (workdir, resolved provider command, inputs' size+mtime); without a cache this trades a per-call cost for a per-call cost.
  • Off by default.

Use case

Any agent session working in a repository that pins toolchain versions. Two concrete losses today:

  1. node -v / go version / cargo in a project run the global version, so the agent builds against a toolchain the project does not use.
  2. Project [env] values never apply. In my setup ~/Work/mise.toml sets JJ_USER/JJ_EMAIL and NPM_CONFIG_REGISTRY; AFT bash calls in that directory therefore commit with the wrong author identity and install from the wrong registry.

The workaround I ship locally is a BASH_ENV hook that evals mise hook-env on every command; it works, it is just 22 ms per call, and it cannot reuse anything AFT already knows about the directory.

Alternative considered: probe per workdir instead of per process

Re-running the -lic probe with the call's workdir would reuse all the existing machinery (inputs, cache file, timeout budget). Two problems:

  • Cost: fish -lic measured at ~95 ms on this machine, so a cold probe per new directory is ~4× the current per-call cost of the in-shell workaround, and it lands on the critical path of the first command in each directory.
  • Trust: an interactive login shell started inside a repository executes project-influenced startup (direnv's .envrc is the obvious case), so it would need the project-trust gate or an explicit opt-in anyway.

I am happy to implement whichever design you prefer, including the smaller one (env_provider) or a variant where the provider contract is a shell snippet AFT evals in the child rather than JSON.

gaojunran avatar Nov 15 '26 17:11 gaojunran

Thanks for the careful write-up. The diagnosis is right: AFT resolves PATH once per process from its login-shell probe (effective_path.rs, a process-wide OnceLock). So every bash call gets the toolchain of the directory that probe ran in, and a project's [env] never applies.

We agree with the direction: a provider that lives in user configuration only, is off by default, and follows the call's workdir. We want to settle a few design points first:

  • The cache key has to cover every config file the version manager reads up the directory tree (mise and asdf merge parent configs), not only files in the workdir.
  • How a provider removes an inherited name.
  • What a call reports when the provider fails or times out, so the agent never silently runs with the wrong toolchain.
  • How the result reaches a sandboxed shell, which pins PATH today.
  • A cloned project must not be able to run code through this. The provider command comes only from user configuration, and AFT keeps the version manager's own approval in force (mise trust, direnv allow); when it refuses, the shell starts without the project environment and says why. Variables such as LD_PRELOAD or GIT_SSH_COMMAND from a project environment are dropped.

We'll post the design here and build it ourselves. Thanks for offering to implement it; we'd value your review of the design once it's up.

aft-alfonso[bot] avatar Nov 15 '26 23:11 aft-alfonso[bot]

Shipped in v0.66.0 as an opt-in setting. In your user config (~/.config/cortexkit/aft.jsonc):

{ "shell": { "env_provider": { "kind": "mise" } } }

kind can be mise, direnv (not on Windows) or custom with a command. The provider runs in the command's working folder under AFT's sandbox, its output is cached, and project config files can't turn it on. If the provider fails, the command runs with the default environment and a one-line notice. Thanks for the proposal, @gaojunran.

aft-alfonso[bot] avatar Nov 17 '26 09:11 aft-alfonso[bot]