Skip to content

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

Closed as not planned

Description

@klimkin

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.

Activity

  1. added
    discussionUsed for feature requests, proposals, ideas, etc. Open discussion
    on Jan 18, 2026
  2. github-actions commented on Jan 18, 2026

    @github-actions
    Contributor

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

    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.

  3. smola commented on Jan 31, 2026

    @smola

    👍 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.
  4. tesdal commented on Feb 15, 2026

    @tesdal
    Contributor

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

  5. jrswab commented on Feb 27, 2026

    @jrswab

    +1 on this. I'm hitting the same limitation in practice.

    I regularly run multiple concurrent OpenCode sessions in the same project directory. My use case is linking work artifacts (commits, branches, logs) back to the specific session that produced them. Today, the only option from within a session is opencode session list -n 1, which returns the most recently updated session for the working directory. That's unreliable when multiple sessions are active simultaneously.

    Having OPENCODE_SESSION_ID (and OPENCODE_SESSION_TITLE) injected into Bash tool subprocesses would make this deterministic. It would also let the LLM itself programmatically interact with its own session via the server API (sharing, exporting, or renaming the session) without guessing which session it belongs to.

  6. github-actions commented on Apr 29, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

  7. wylswz commented on May 25, 2026

    @wylswz

    +1. I'm working on an agent that creates pr to my repo and respond to discussions in the same session. I'm trying to wrap the gh cli that builds mapping between pr id and session id, however, it is impossible to do this currently. Having session id propagated would be really helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

discussionUsed for feature requests, proposals, ideas, etc. Open discussion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions