Skip to content

[FEATURE]: Propagate session ID to tool subprocesses via environment variable #15739

Closed

Description

@jlguerreiro

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

Feature Request

Problem

When OpenCode executes a tool (e.g., a bash command), it spawns a subprocess. That subprocess has no way to discover which OpenCode session triggered it. This makes it impossible for external systems — or even
the tool itself — to correlate their work back to a specific session.

Use Case

If you build tooling that wraps or extends OpenCode's tool execution (e.g., logging, metrics, multi-session orchestration), you need to know which session a tool invocation belongs to. Today, the only way to
attempt this is fragile heuristics like FIFO matching or content-based comparison, because the subprocess environment contains no session context.

Proposed Solution

Set environment variables on tool subprocesses before they are spawned:

  • OPENCODE_SESSION_ID — the session that triggered the tool call
  • OPENCODE_TOOL_PART_ID (optional) — the part ID of the tool invocation within the message, for finer-grained correlation

This is a minimal, non-breaking change. Tools that don't need the information simply ignore the env vars.

Precedent

PR #12932 already propagates session context (as HTTP headers) for LLM requests. This proposal extends the same pattern to tool subprocess execution.

Activity

  1. added
    discussionUsed for feature requests, proposals, ideas, etc. Open discussion
    on Mar 2, 2026
  2. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Mar 2, 2026
  3. github-actions commented on Mar 2, 2026

    @github-actions
    Contributor

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

    If your request is distinct from those, please clarify how it differs.

  4. added a commit that references this issue on Mar 2, 2026
    f109a1c
  5. taxilian commented on Apr 9, 2026

    @taxilian

    #21756 would make it possible to have a plugin do this

  6. assigned and unassigned on Apr 26, 2026
  7. removed
    discussionUsed for feature requests, proposals, ideas, etc. Open discussion
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  8. XJfyrh commented on Aug 3, 2026

    @XJfyrh

    This request would also directly prevent a class of correctness bugs I hit with concurrent sessions. Posting here rather than opening a duplicate issue, since this is the same root cause.

    Real-world failure caused by the missing env var
    I ran two opencode sessions in parallel, one per project directory (proj-a/ and proj-b/). Both agents created a temporary helper script with the same generic name (gen_appendix.py) in the shared pre-approved temp directory (%TEMP%\opencode).

    Because there is no session identifier available in the subprocess environment, the agent could not name its scratch file uniquely. Session B overwrote the file while session A was still using it; session A then executed session B's script content (different paths, different logic) and wrote its output into the wrong project directory. The corruption was only caught because the script happened to print the output path.

    Why this matters beyond external-tool correlation
    The issue body frames OPENCODE_SESSION_ID as a need for external tooling (logging, orchestration). My case shows the env var is also needed for agent-internal correctness: an agent cannot isolate its own scratch files across concurrent sessions without it. That makes this a reliability/feature-availability gap, not just an observability nicety.

    Concrete benefit if implemented
    Agents could name temp files as ${OPENCODE_SESSION_ID} (or use a session-scoped dir such as %TEMP%\opencode<session-id>), eliminating last-writer-wins collisions entirely. This matches the project's own pattern of unique names in the tool-output store (e.g. tool).

    Happy to provide more details or test once implemented.

  9. ebuildy commented on Sep 22, 2026

    @ebuildy

    I have just found this bug too, tmp files are shared across sessions !!! 1 session create a file, other session overrides it ..... this is so dangerous.

    Any news about it?

  10. rekram1-node commented on Sep 29, 2026

    @rekram1-node
    Collaborator

    Implemented for the shell-tool subprocesses in #51975: commands (including background commands) receive OPENCODE_SESSION_ID from the current tool session, replacing any stale inherited value. Child processes launched by those commands inherit it. This does not inject the ID into arbitrary plugin subprocesses or provide the optional OPENCODE_TOOL_PART_ID.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions