MingaClaudeBridge.Env (minga_claude_bridge v0.1.0)

Copy Markdown View Source

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.

Summary

Types

An environment as a map of variable names to values.

The :env option of Port.open/2.

Functions

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

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

The variables step 3 forces.

The credential keys the bridge rejects.

Returns true when step 1 removes the inherited variable name.

Types

env_map()

@type env_map() :: %{optional(String.t()) => String.t()}

An environment as a map of variable names to values.

error()

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

port_env()

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

The :env option of Port.open/2.

Functions

child_env(parent, credential)

@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(parent, credential)

@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()

@spec forced() :: env_map()

The variables step 3 forces.

rejected_credential_keys()

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

The credential keys the bridge rejects.

stripped?(name)

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

Returns true when step 1 removes the inherited variable name.