# `MingaClaudeBridge.Env`
[🔗](https://github.com/jsmestad/minga-claude-bridge/blob/main/lib/minga_claude_bridge/env.ex#L1)

Computes the environment of a Claude Code child process.

The child must use Claude Code's own login and settings, never Minga's provider keys or the configuration of a parent Claude Code session. `child_env/2` builds the environment in three steps:

  1. Start from the parent environment and remove every variable in `stripped?/1`: `ANTHROPIC_*`, `AWS_*`, `NODE_*`, the Bedrock, Vertex, and Foundry switches, the variables a parent Claude Code session sets on its children, and Minga's provider key variables.
  2. Apply the route credential's `env` entry. An empty value removes the variable. `HOME`, `CLAUDE_CONFIG_DIR`, and `USERPROFILE` are rejected, because the bridge reads Claude Code's session files through the parent's values.
  3. Force the variables in `forced/0`, which keep Claude Code from compacting on its own, loading claude.ai MCP servers, sending non-essential traffic, cutting MCP tool descriptions, deferring tools behind tool search, or timing out a tool call Minga is still running.

The result is in the format of the `:env` option of `Port.open/2`: charlist pairs, with `false` for a variable to remove. A Port applies `:env` on top of the inherited environment, so every removal is explicit.

Values are secrets. This module never logs them, and callers must not log the result.

# `env_map`

```elixir
@type env_map() :: %{optional(String.t()) =&gt; String.t()}
```

An environment as a map of variable names to values.

# `error`

```elixir
@type error() :: {:forbidden_env, [String.t()]} | {:invalid_env, [String.t()]}
```

# `port_env`

```elixir
@type port_env() :: [{charlist(), charlist() | false}]
```

The `:env` option of `Port.open/2`.

# `child_env`

```elixir
@spec child_env(env_map(), env_map()) :: {:ok, port_env()} | {:error, error()}
```

Builds the child environment from the parent environment and the credential's `env` entry.

Returns `{:error, {:forbidden_env, keys}}` when the credential sets `HOME`, `CLAUDE_CONFIG_DIR`, or `USERPROFILE`, and `{:error, {:invalid_env, keys}}` for a credential key that is empty or contains `=` or a NUL byte, or a value that contains a NUL byte. Error reasons name keys, never values.

# `final_env`

```elixir
@spec final_env(env_map(), env_map()) :: env_map()
```

Returns the final environment as a map, after the same three steps as `child_env/2`.

# `forced`

```elixir
@spec forced() :: env_map()
```

The variables step 3 forces.

# `rejected_credential_keys`

```elixir
@spec rejected_credential_keys() :: [String.t()]
```

The credential keys the bridge rejects.

# `stripped?`

```elixir
@spec stripped?(String.t()) :: boolean()
```

Returns true when step 1 removes the inherited variable `name`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
