I’m not sure if this is a bug or not, or “works as intended” and is effectively a feature request, but the sessionStart hook supports an output object including env and the docs say that field is responsible for:
Environment variables to set for this session. Available to all subsequent hook executions
but this is true for all hooks which do NOT involve a subagent and BEFORE a sessionStop.
Stated more directly, subagents do NOT inherit the environment variables from its parent, and subsequently not from subagent to subagent either.
In the same vein, closing an agent’s tab, which triggers a sessionStop, then returning to that conversation does not call a corresponding sessionStart, and therefore doesn’t set the environment variables again. Though sessionStopwill be called again if you close its tab again.
I’ve been able to verify this through the use of recording all hooks to a .jsonl file, and correlating them to error logs I’m keeping for my running code that checks for the environment variables and fails when they are missing.
For some additional context, I want to share information from the “root” agent down to ALL subagents such that they can all reliably work with certain APIs. As an example, I have one CLI which automatically writes files to a shared directory utilizing an environment variable for the absolute path.
On the more feature request side of things, but in the same context. It’d be even better if we could modify the environment variables of subagents during subagentStart. This could enable much more seamless subagent delegations.
I could easily imagine “parent” agents providing scoped credentials to subagents for utilizing CLIs that participate with it. When combined with other allow|deny-style hooks, like beforeShellExecution/preToolUse, it can further be enforced that only such participating CLIs are utilized by the agents.
Today you need to correlate conversation_id, and parent_conversation_id for subagents, outside of hooks in order to facilitate this behavior.
Hey, thanks for the detailed report and for running the verification via JSONL.
This looks like two separate issues:
Env variables aren’t being passed to subagents. The docs say Available to all subsequent hook executions, so it’s reasonable to expect hooks inside subagent sessions to get those variables too. This looks like a bug or a gap in the current implementation.
sessionStart isn’t triggered when returning to a conversation after sessionStop. This also looks like a missing lifecycle event.
I’ve passed this to the team. No timeline yet, but your report will help with prioritization.
Your idea about setting env via subagentStart is a great use case too, especially for scoped credentials. I’ll log it as a feature request.
Let me know if you find any workaround using conversation_id and parent_conversation_id correlation that could help other users.