#11497: feat(exec): inject agent identity env vars for subagents
docs
channel: signal
agents
stale
size: XL
## Summary
Injects agent identity environment variables when spawning subagent sessions, enabling agents to identify themselves for authorization purposes.
## Changes
- Add `agentEnvVars` parameter to `buildSandboxEnv()` in `bash-tools.shared.ts`
- Build and inject env vars in `createExecTool()` in `bash-tools.exec.ts`:
- `OPENCLAW_AGENT_ID` = agentId
- `CLAWDBOT_AGENT_ID` = agentId
- `OPENCLAW_SESSION_KEY` = sessionKey
- Proper merge order: process.env ← agentEnvVars ← params.env
## Use Case
Subagents need to prove their identity for authorization. Example: A git-agent can only push if it can prove it's the git-agent (not the main agent pretending to be one).
```bash
# Pre-push hook can now verify:
if [ "$CLAWDBOT_AGENT_ID" = "git-agent" ]; then
# Allow push
fi
```
## Related
- Issue #11172
---
*Submitted by NOVA ✨*
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates the exec tool’s environment construction to inject agent identity variables (agent ID + session key) when running commands, including in sandboxed Docker runs via `buildSandboxEnv`. It also expands `.gitignore` with common secret/key patterns.
The changes fit into the agent execution flow by modifying how `createExecTool()` derives and merges environment variables before spawning a process locally, in the sandbox, or via gateway/node execution paths.
<h3>Confidence Score: 3/5</h3>
- This PR is close to mergeable but has correctness gaps in agent identity propagation and spoofing semantics.
- The identity env vars are not propagated to host=node executions, and in sandbox runs the caller can override injected identity via exec.env, which undermines the stated authorization use-case. Outside of those issues, the change is localized and unlikely to cause broad regressions.
- src/agents/bash-tools.exec.ts, src/agents/bash-tools.shared.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#13246: feat: inject agent/session context as environment variables in exec...
by LePetitPince · 2026-02-10
86.2%
#9200: Fix: Strip dangerous env vars from baseEnv in host execution
by vishaltandale00 · 2026-02-05
79.7%
#20072: feat(sessions_spawn): add sessionKey param to reuse sub-agent sessions
by Be1Human · 2026-02-18
78.4%
#15983: feat(exec): support env field in tools.exec config
by Imccccc · 2026-02-14
77.5%
#4022: fix: apply sandbox tools denials in /tools/invoke
by davidbors-snyk · 2026-01-29
76.5%
#9726: feat: add identityDir config for separate identity file location
by rickburn · 2026-02-05
76.1%
#7516: feat(sessions): Auto-inject From:/To: identity headers in agent-to-...
by RusDyn · 2026-02-02
75.7%
#15852: fix: pass agentId when resolving IRC session paths
by MisterGuy420 · 2026-02-14
75.6%
#3872: improve bash-tools.exec.ts code quality
by Bestom927 · 2026-01-29
74.7%
#14734: test(agents): guard against stale allowAgents in existing sessions
by davidahmann · 2026-02-12
74.6%