← Back to PRs

#13246: feat: inject agent/session context as environment variables in exec commands

by LePetitPince open 2026-02-10 07:21 View on GitHub →
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