#13246: feat: inject agent/session context as environment variables in exec commands
agents
stale
# Inject Agent/Session Context as Environment Variables in Exec Commands
## Summary
This PR enables downstream CLIs and scripts to detect which agent and session invoked them by automatically injecting four environment variables into every `exec` command:
- `OPENCLAW_AGENT_ID` — The agent identifier (e.g., `lepetitpince`)
- `OPENCLAW_SESSION_KEY` — Full session key (e.g., `agent:lepetitpince:main`)
- `OPENCLAW_SESSION_LABEL` — Human-readable label (e.g., `main`, `subagent:abc123`)
- `OPENCLAW_SESSION_KIND` — Session type (`main`, `subagent`, or `custom`)
**Use case:** Multi-agent task management systems (like [clawdo](https://github.com/LePetitPince/clawdo)) need to know *which* agent invoked a command to properly track task ownership and maintain audit trails.
## Changes
- **`bash-tools.exec.ts`** (+66 lines): `injectAgentContext()` helper + unconditional call after env preparation
- **`bash-tools.exec.context-injection.test.ts`** (+266 lines): 8 tests covering happy path, edge cases, and graceful degradation
**Backward compatible:** Variables are additive. Existing scripts ignore them; new tools can depend on them.
## Design Decisions
### Always-on, no opt-in flag
Agent identity is a fact, not a preference. Making injection optional creates ecosystem fragmentation where tools cannot reliably depend on these variables being present. Like `PATH`, `HOME`, and `USER` — always there.
### Graceful degradation
If `agentId` or `sessionKey` is missing or malformed, injection is skipped silently. No crashes, no warnings. This ensures compatibility with non-agent contexts.
### Minimal variable set
Only 4 variables, all non-sensitive metadata. No credentials, no secrets — just identity and context.
## Testing
All 8 tests passing:
- ✅ Main agent session injection
- ✅ Subagent session injection
- ✅ Custom session injection
- ✅ Empty agentId (graceful skip)
- ✅ Empty sessionKey (graceful skip)
- ✅ Malformed sessionKey (graceful skip)
- ✅ Non-agent session format (graceful skip)
- ✅ Both missing (no injection, no crash)
## Usage Example
```bash
# Downstream CLIs automatically know their caller:
$ clawdo propose "Fix bug"
# clawdo reads OPENCLAW_AGENT_ID=elite-coder → task tagged automatically
# In any script:
agent_id = os.environ.get("OPENCLAW_AGENT_ID", "unknown")
```
---
Closes #11477
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds always-on injection of agent/session context into exec subprocess environments via four new `OPENCLAW_*` variables, plus a new Vitest suite that exercises main/subagent/custom session keys and several malformed/missing cases.
The injection is implemented inside `createExecTool()` after env preparation, using `parseAgentSessionKey()` to derive a human-readable label and kind from agent-formatted session keys. Tests validate expected values by running `echo` commands through the exec tool.
<h3>Confidence Score: 3/5</h3>
- This PR has a couple of correctness issues that likely need fixes before merge.
- Core implementation is small and well-contained, but the new env injection is currently inconsistent across exec hosts (node path doesn’t receive injected vars), and the JSDoc and tests don’t accurately reflect/verify the intended behavior (doc mismatch and tests can’t distinguish unset vs empty).
- src/agents/bash-tools.exec.ts, src/agents/bash-tools.exec.context-injection.test.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#11497: feat(exec): inject agent identity env vars for subagents
by NOVA-Openclaw · 2026-02-07
86.2%
#20072: feat(sessions_spawn): add sessionKey param to reuse sub-agent sessions
by Be1Human · 2026-02-18
77.1%
#14734: test(agents): guard against stale allowAgents in existing sessions
by davidahmann · 2026-02-12
76.4%
#11854: fix: resolve per-agent tools.exec config in pi-tools
by Yida-Dev · 2026-02-08
76.3%
#7516: feat(sessions): Auto-inject From:/To: identity headers in agent-to-...
by RusDyn · 2026-02-02
75.6%
#11300: feat(exec): make shell configurable via tools.exec.shell
by imjszhang · 2026-02-07
75.6%
#9200: Fix: Strip dangerous env vars from baseEnv in host execution
by vishaltandale00 · 2026-02-05
75.3%
#15852: fix: pass agentId when resolving IRC session paths
by MisterGuy420 · 2026-02-14
75.0%
#12871: fix: use bash and warn about shell injection (issue #12836)
by ambicuity · 2026-02-09
74.7%
#11788: feat: inter-agent communication via CLI scripts
by jingkang0822 · 2026-02-08
74.5%