xa.claude_env¶
The environment and flags a fresh claude process starts with: account and permissions.
No xa imports and no third-party ones: this is the lowest layer, so that any launcher (xa’s tmux panes, crowsnest’s spawner, a future terminal-tab provider) can import one copy of the rules instead of re-deriving them.
Account. Claude Code picks its account by CLAUDE_CONFIG_DIR (config,
credentials and transcripts all move with it), and reaches the default home
only by the variable’s absence. A command line handed to tmux runs in a shell
whose environment is the tmux server’s, which belongs to whoever started the
server: possibly another account, possibly a session long gone, whose identity
markers (CLAUDECODE, CLAUDE_CODE_SESSION_ID, its messaging socket, its
effort) would make the new process register as that dead session’s child.
env_prefix() therefore states every variable it controls either way round:
set when the child should have it, unset (env -u) when not. tmux -e can
only set, so it cannot express the default home.
Permissions. permission_args() turns a mode into flags,
bypass_refused() says where claude would refuse the bypass flags (root,
unless IS_SANDBOX=1; the refusal is invisible behind tmux), and
resume_permission() picks the mode a resumed session comes back in.
Moved here from crowsnest.spawn (thorwhalen/xa#23), whose tests and
derivation notes it keeps; crowsnest is to import it from here.
Module Attributes
Claude Code's account variable, and the home it means when unset. |
|
The variables that select which account a session runs under. |
|
they would override the account the home selects (an API key that bills elsewhere). |
|
The |
|
Under this variable ( |
|
Claude Code's name for "skip every permission check", and its aliases. |
|
The modes |
|
The variable that carries OTLP request headers (credentials). |
Functions
|
The |
|
Would claude refuse the bypass flags here? Root, unsandboxed, not vouched for. |
|
|
|
|
|
The ``env -u . |
|
The flags that start claude in permission |
|
|
Classes
|
An opt-in, per-launch OpenTelemetry trace export for one Claude Code session. |
- xa.claude_env.ACCOUNT_VARS = ('CLAUDE_CONFIG_DIR', 'CLAUDE_PROFILE')¶
The variables that select which account a session runs under. A shell profile scheme commonly pairs
CLAUDE_CONFIG_DIRwith aCLAUDE_PROFILElabel and re-derives one from the other, so the two travel together or not at all.
- xa.claude_env.BYPASS_MODE = 'bypassPermissions'¶
Claude Code’s name for “skip every permission check”, and its aliases.
- xa.claude_env.DROPPED_VARS = ('ANTHROPIC_API_KEY',)¶
they would override the account the home selects (an API key that bills elsewhere).
- Type:
Variables a new session must not inherit whatever they are set to
- xa.claude_env.HOME_ENV_VAR = 'CLAUDE_CONFIG_DIR'¶
Claude Code’s account variable, and the home it means when unset.
- xa.claude_env.KNOWN_MODES = frozenset({'acceptEdits', 'auto', 'bypassPermissions', 'default', 'dontAsk', 'manual', 'plan'})¶
The modes
claude --permission-modeaccepts (defaultis the older name ofmanual). A mode a newer claude adds goes here.
- xa.claude_env.OTLP_HEADERS_VAR = 'OTEL_EXPORTER_OTLP_HEADERS'¶
The variable that carries OTLP request headers (credentials). Never put on a command line.
- class xa.claude_env.OtlpTraces(endpoint, protocol='http/protobuf', headers=dict(), content=False, resource=dict(), export_interval_ms=None)[source]¶
Bases:
objectAn opt-in, per-launch OpenTelemetry trace export for one Claude Code session.
Pass it as
env_prefix(..., telemetry=OtlpTraces(endpoint=...)).headers(an OTLP backend’s credentials) are never part of the command lineenv_prefix()returns, and never in this object’srepr: they reach the process only throughenv(), for a launcher that starts claude with a process environment of its own (subprocess.run(..., env=...)). The prefix then leaves the variable to the shell, as it does every other token.>>> t = OtlpTraces('http://127.0.0.1:4318/v1/traces', headers={'Authorization': 'x'}) >>> 'Authorization' in repr(t) False >>> t.env(with_headers=False)['OTEL_TRACES_EXPORTER'] 'otlp'
- xa.claude_env.SANDBOX_VAR = 'IS_SANDBOX'¶
Under this variable (
"1") Claude Code accepts the bypass flag from root.env_prefix()hands it on and never unsets it: a launcher may set it.
- xa.claude_env.SESSION_VARS = ('CLAUDECODE', 'CLAUDE_AGENT_SDK_VERSION', 'CLAUDE_BG_AUTH_SNAPSHOT_PATH', 'CLAUDE_BG_BACKEND', 'CLAUDE_BG_CLAIM_AUTH', 'CLAUDE_BG_DISPATCHER_RATE_LIMIT_TIER', 'CLAUDE_BG_DISPATCHER_SUBSCRIPTION_TYPE', 'CLAUDE_BG_ISOLATION', 'CLAUDE_BG_MEMORY_TOGGLED_OFF', 'CLAUDE_BG_POST_CLEAR_RESPAWN', 'CLAUDE_BG_PTY_AUTH', 'CLAUDE_BG_RENDEZVOUS_SOCK', 'CLAUDE_BG_RV_AUTH', 'CLAUDE_BG_SESSION_PERMISSION_RULES', 'CLAUDE_BG_SOCKET_TOKENS_PATH', 'CLAUDE_BG_SOURCE', 'CLAUDE_BG_STARTUP_WEDGE_MS', 'CLAUDE_BG_TCC_DISCLAIMED', 'CLAUDE_CODE_BRIDGE_SESSION_ID', 'CLAUDE_CODE_CHILD_SESSION', 'CLAUDE_CODE_CLOUD_SESSION_ID', 'CLAUDE_CODE_ENTRYPOINT', 'CLAUDE_CODE_EXECPATH', 'CLAUDE_CODE_MESSAGING_SOCKET', 'CLAUDE_CODE_MESSAGING_TOKEN', 'CLAUDE_CODE_REMOTE_SESSION_ID', 'CLAUDE_CODE_REMOTE_SESSION_ORIGIN', 'CLAUDE_CODE_REMOTE_SESSION_UUID', 'CLAUDE_CODE_SESSION_ACCESS_TOKEN', 'CLAUDE_CODE_SESSION_ATTENDED', 'CLAUDE_CODE_SESSION_ID', 'CLAUDE_CODE_SESSION_KIND', 'CLAUDE_CODE_SESSION_LOG', 'CLAUDE_CODE_SESSION_NAME', 'CLAUDE_CODE_SESSION_ORIGIN', 'CLAUDE_CODE_SPAWN_TIMESTAMP_MS', 'CLAUDE_CODE_SSE_PORT', 'CLAUDE_CODE_TRIGGER_ID', 'CLAUDE_CODE_WORKER_EPOCH', 'CLAUDE_EFFORT', 'CLAUDE_JOB_DIR', 'CLAUDE_PID', 'CLAUDE_PROJECT_DIR', 'CLAUDE_PROJECT_UUID', 'CLAUDE_SESSION_ID')¶
The
CLAUDE*variables that identify one running session – who it is, what started it, how to talk to it – named so thatenv_prefix()can unset them whether or not this process has them (a tmux server keeps the markers of the session that started it and hands them to every pane it opens afterwards).Identity, not configuration. Claude Code reads hundreds of
CLAUDE*variables, nearly all settings a person may deliberately export; those stay. Derived by matching the identity families (session and background-job plumbing, messaging and bridge sockets, job and project directories) against the name table of theclaudebinary (strings $(command -v claude) | grep -oE 'CLAUDE[A-Z0-9_]*'), not typed out. Re-derive it when Claude Code grows a new family;test_session_vars_covers_every_marker_this_live_session_carriesis the tripwire. Account variables are deliberately absent: seeACCOUNT_VARS.
- xa.claude_env.account_env_tokens(home, *, environ=None, telemetry=None)[source]¶
The
envprefix that runs a command underhome’s account, markers stripped.>>> toks = account_env_tokens('/h/b', environ={'CLAUDECODE': '1'}) >>> toks[-1], ('-u', 'CLAUDECODE') in zip(toks, toks[1:]) ('CLAUDE_CONFIG_DIR=/h/b', True)
- xa.claude_env.bypass_refused(*, euid=None, environ=None, root_bypass_ok=False)[source]¶
Would claude refuse the bypass flags here? Root, unsandboxed, not vouched for.
root_bypass_okis for a launcher known to setIS_SANDBOXitself.>>> bypass_refused(euid=0, environ={}), bypass_refused(euid=0, environ={'IS_SANDBOX': '1'}) (True, False) >>> bypass_refused(euid=501, environ={}) False
- Return type:
- xa.claude_env.canonical_permission_mode(mode)[source]¶
modespelled as claude spells it;''stays''(claude’s own default).Case-insensitive; every bypass alias is
'bypassPermissions'. An unknown name raisesValueErrorlisting the known ones.>>> canonical_permission_mode('Bypass'), canonical_permission_mode('acceptedits') ('bypassPermissions', 'acceptEdits')
- Return type:
- xa.claude_env.child_env(environ=None, *, home=None, drop=('ANTHROPIC_API_KEY',))[source]¶
environ(defaultos.environ) as a new session should inherit it.The spawning session’s identity markers (
SESSION_VARS) go; the account and everyCLAUDE*setting (CLAUDE_CODE_OAUTH_TOKEN,CLAUDE_CODE_USE_BEDROCK, …) stay. Withhomethe child runs under that home instead:CLAUDE_CONFIG_DIRset to it, or unset when it is the default home. TheCLAUDE_PROFILElabel travels only with the account it labels.dropgoes too (pass()to keep it).>>> child_env({'CLAUDECODE': '1', 'CLAUDE_CONFIG_DIR': '/h/b', 'PATH': '/bin'}) {'CLAUDE_CONFIG_DIR': '/h/b', 'PATH': '/bin'} >>> child_env({'ANTHROPIC_API_KEY': 'sk-x', 'PATH': '/bin'}) {'PATH': '/bin'}
- Return type:
- xa.claude_env.env_prefix(env, *, environ=None, drop=('ANTHROPIC_API_KEY',), markers=('CLAUDECODE', 'CLAUDE_AGENT_SDK_VERSION', 'CLAUDE_BG_AUTH_SNAPSHOT_PATH', 'CLAUDE_BG_BACKEND', 'CLAUDE_BG_CLAIM_AUTH', 'CLAUDE_BG_DISPATCHER_RATE_LIMIT_TIER', 'CLAUDE_BG_DISPATCHER_SUBSCRIPTION_TYPE', 'CLAUDE_BG_ISOLATION', 'CLAUDE_BG_MEMORY_TOGGLED_OFF', 'CLAUDE_BG_POST_CLEAR_RESPAWN', 'CLAUDE_BG_PTY_AUTH', 'CLAUDE_BG_RENDEZVOUS_SOCK', 'CLAUDE_BG_RV_AUTH', 'CLAUDE_BG_SESSION_PERMISSION_RULES', 'CLAUDE_BG_SOCKET_TOKENS_PATH', 'CLAUDE_BG_SOURCE', 'CLAUDE_BG_STARTUP_WEDGE_MS', 'CLAUDE_BG_TCC_DISCLAIMED', 'CLAUDE_CODE_BRIDGE_SESSION_ID', 'CLAUDE_CODE_CHILD_SESSION', 'CLAUDE_CODE_CLOUD_SESSION_ID', 'CLAUDE_CODE_ENTRYPOINT', 'CLAUDE_CODE_EXECPATH', 'CLAUDE_CODE_MESSAGING_SOCKET', 'CLAUDE_CODE_MESSAGING_TOKEN', 'CLAUDE_CODE_REMOTE_SESSION_ID', 'CLAUDE_CODE_REMOTE_SESSION_ORIGIN', 'CLAUDE_CODE_REMOTE_SESSION_UUID', 'CLAUDE_CODE_SESSION_ACCESS_TOKEN', 'CLAUDE_CODE_SESSION_ATTENDED', 'CLAUDE_CODE_SESSION_ID', 'CLAUDE_CODE_SESSION_KIND', 'CLAUDE_CODE_SESSION_LOG', 'CLAUDE_CODE_SESSION_NAME', 'CLAUDE_CODE_SESSION_ORIGIN', 'CLAUDE_CODE_SPAWN_TIMESTAMP_MS', 'CLAUDE_CODE_SSE_PORT', 'CLAUDE_CODE_TRIGGER_ID', 'CLAUDE_CODE_WORKER_EPOCH', 'CLAUDE_EFFORT', 'CLAUDE_JOB_DIR', 'CLAUDE_PID', 'CLAUDE_PROJECT_DIR', 'CLAUDE_PROJECT_UUID', 'CLAUDE_SESSION_ID'), telemetry=None)[source]¶
The
env -u ... K=V ...tokens that make a fresh shell run a command underenv.Each name this controls –
markers, theCLAUDE*names ofenvironthatenvlacks,ACCOUNT_VARS,dropandDROPPED_VARS– is stated either way round: set whenenvhas it, unset when it does not, because the shell’s environment is not this process’s. Everything else (other settings, and so any token) is left to the shell rather than written on a command line, wherepsand tmux would show it.>>> env_prefix({'CLAUDE_CONFIG_DIR': '/h/b'}, environ={}, markers=('CLAUDECODE',)) ['env', '-u', 'CLAUDECODE', '-u', 'CLAUDE_PROFILE', '-u', 'ANTHROPIC_API_KEY', 'CLAUDE_CONFIG_DIR=/h/b'] >>> env_prefix({}, environ={}, markers=('CLAUDECODE',), drop=()) ['env', '-u', 'CLAUDECODE', '-u', 'CLAUDE_CONFIG_DIR', '-u', 'CLAUDE_PROFILE', '-u', 'ANTHROPIC_API_KEY']
telemetry(defaultNone: nothing changes) addsOtlpTraces’s variables, without its headers:>>> toks = env_prefix({}, environ={}, markers=(), drop=(), ... telemetry=OtlpTraces('http://h:4318/v1/traces', headers={'Authorization': 's3cret'})) >>> 'OTEL_TRACES_EXPORTER=otlp' in toks, any('s3cret' in t for t in toks) (True, False)
- xa.claude_env.permission_args(mode, *, allow_bypass=False)[source]¶
The flags that start claude in permission
mode(''/None: none).allow_bypasskeeps bypass reachable (shift-tab) for a session that starts in another mode.>>> permission_args('bypass') ['--dangerously-skip-permissions'] >>> permission_args('auto', allow_bypass=True) ['--allow-dangerously-skip-permissions', '--permission-mode', 'auto'] >>> permission_args('') []
- xa.claude_env.resume_permission(mode, *, bypass_seen, euid=None, environ=None)[source]¶
(mode, allow_bypass, warning)for resuming a session last seen inmode.The session comes back in the mode it was last in. If it ever reached bypass (
bypass_seen) it was started with the bypass flag, so bypass stays reachable even when it was last in another mode. Where claude would refuse the bypass flags (bypass_refused()) they are dropped with a warning: the machine’s own settings then decide, as they would for a person there. A mode this module does not know (renamed in a newer claude) is dropped with a warning too, since claude would refuse it at startup, invisibly, behind tmux.>>> resume_permission('auto', bypass_seen=True, euid=501) ('auto', True, None) >>> resume_permission('bypassPermissions', bypass_seen=True, euid=0, environ={})[:2] ('', False)