[FEATURE]: Expose session context to child processes via environment variables #6727

Open
opened 2026-02-16 18:05:04 -05:00 by yindo · 3 comments
Owner

Originally created by @klimkin on GitHub (Jan 18, 2026).

Originally assigned to: @thdxr on GitHub.

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

Problem

User scripts and tools that are executed via opencode (bash tool, shell commands) have no way to identify which opencode session they're running in. This makes it difficult to correlate logs, metrics, and outputs when running multiple opencode sessions, such as in iterative workflows (e.g., Ralph loops).

Use Case

A user running a Ralph loop that spawns a new opencode session per iteration needs to log which session each iteration corresponds to. Currently, child processes spawned by opencode have no access to session metadata.

Solution

Expose session context to child processes via environment variables:

  • OPENCODE_SESSION_ID: The unique identifier of the current session (e.g., ses_xyz123)
  • OPENCODE_SESSION_TITLE: The human-readable title of the current session (e.g., "Fix bug in auth flow")
    These variables are set in two user-facing command execution contexts:
  1. Bash tool: When users execute commands via the bash tool
  2. Prompt shell: When commands are executed via direct shell invocation

Implementation Details

  • Environment variables are set dynamically per spawn (not globally at startup like AGENT and OPENCODE)
  • This is necessary because users can switch between sessions during interactive work
  • Session lookup includes graceful error handling to avoid breaking existing tests and mock contexts

Example Usage

#!/bin/bash
# User script can now access session context
echo "Running in session: $OPENCODE_SESSION_TITLE ($OPENCODE_SESSION_ID)" >> /var/log/opencode-iterations.log

This enables proper tracking and correlation of work across multiple concurrent or sequential opencode sessions.

Originally created by @klimkin on GitHub (Jan 18, 2026). Originally assigned to: @thdxr on GitHub. ### Feature hasn't been suggested before. - [x] I have verified this feature I'm about to request hasn't been suggested before. ### Describe the enhancement you want to request ## Problem User scripts and tools that are executed via opencode (bash tool, shell commands) have no way to identify which opencode session they're running in. This makes it difficult to correlate logs, metrics, and outputs when running multiple opencode sessions, such as in iterative workflows (e.g., Ralph loops). ## Use Case A user running a Ralph loop that spawns a new opencode session per iteration needs to log which session each iteration corresponds to. Currently, child processes spawned by opencode have no access to session metadata. ## Solution Expose session context to child processes via environment variables: - `OPENCODE_SESSION_ID`: The unique identifier of the current session (e.g., `ses_xyz123`) - `OPENCODE_SESSION_TITLE`: The human-readable title of the current session (e.g., `"Fix bug in auth flow"`) These variables are set in two user-facing command execution contexts: 1. **Bash tool**: When users execute commands via the bash tool 2. **Prompt shell**: When commands are executed via direct shell invocation ## Implementation Details - Environment variables are set dynamically per spawn (not globally at startup like `AGENT` and `OPENCODE`) - This is necessary because users can switch between sessions during interactive work - Session lookup includes graceful error handling to avoid breaking existing tests and mock contexts ## Example Usage ```bash #!/bin/bash # User script can now access session context echo "Running in session: $OPENCODE_SESSION_TITLE ($OPENCODE_SESSION_ID)" >> /var/log/opencode-iterations.log ``` This enables proper tracking and correlation of work across multiple concurrent or sequential opencode sessions.
yindo added the discussion label 2026-02-16 18:05:04 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 18, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #6279: Pass session identifier to MCP servers - Similar request to expose session context, but specifically for MCP servers rather than all child processes
  • #9099: Export actual server URL as environment variable - Related feature request for exposing server context via environment variables to child processes
  • #6936: Pass agents bash env variables OR pass agents bashrc files - Addresses the related need for agents to access environment context from the parent shell

Feel free to ignore if this specific feature request (session context for bash tool and shell commands) addresses a different use case than these existing issues.

@github-actions[bot] commented on GitHub (Jan 18, 2026): This issue might be a duplicate of existing issues. Please check: - #6279: Pass session identifier to MCP servers - Similar request to expose session context, but specifically for MCP servers rather than all child processes - #9099: Export actual server URL as environment variable - Related feature request for exposing server context via environment variables to child processes - #6936: Pass agents bash env variables OR pass agents bashrc files - Addresses the related need for agents to access environment context from the parent shell Feel free to ignore if this specific feature request (session context for bash tool and shell commands) addresses a different use case than these existing issues.
Author
Owner

@smola commented on GitHub (Jan 31, 2026):

👍 I also have 2 strong use cases for OPENCODE_SESSION_ID:

  1. Improve telemetry of some tooling used by agents, by attaching the session ID to events.
  2. Commands whose output is optimized for use within agents, and can present more efficient output if they know what they have returned in previous invocations within the same session.
@smola commented on GitHub (Jan 31, 2026): 👍 I also have 2 strong use cases for `OPENCODE_SESSION_ID`: 1. Improve telemetry of some tooling used by agents, by attaching the session ID to events. 2. Commands whose output is optimized for use within agents, and can present more efficient output if they know what they have returned in previous invocations within the same session.
Author
Owner

@tesdal commented on GitHub (Feb 15, 2026):

With #13661 I guess it would be possible to enable this with your own plugin that populates env, if populating env is more controversial.

@tesdal commented on GitHub (Feb 15, 2026): With #13661 I guess it would be possible to enable this with your own plugin that populates env, if populating env is more controversial.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#6727