[PR #7067] feat(tool/bash): expose session context via environment variables #12236

Closed
opened 2026-02-16 18:17:09 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7067

State: closed
Merged: No


Summary

Injects OPENCODE_SESSION_ID and OPENCODE_MESSAGE_ID into subprocess environment when executing bash commands, enabling spawned processes to identify their parent session context.

Motivation

Currently, when OpenCode spawns bash subprocesses, those processes have no way to identify which session they belong to. This limits:

  • Session self-awareness: Agents cannot query their own session history
  • Cross-session communication: Scripts cannot interact with session APIs
  • Context propagation: Tooling cannot correlate subprocess activity with sessions

Changes

  • packages/opencode/src/tool/bash.ts: Added OPENCODE_SESSION_ID and OPENCODE_MESSAGE_ID to the subprocess environment
  • packages/opencode/test/tool/bash.test.ts: Added test verifying environment variable injection

Use Cases

# Agent can now read its own session history
echo "Current session: $OPENCODE_SESSION_ID"

# Scripts can interact with session APIs
curl "http://localhost:4096/session/$OPENCODE_SESSION_ID/messages"

# Cross-session communication patterns
opencode-session-id info $OPENCODE_SESSION_ID

Testing

  • Added unit test that verifies both environment variables are correctly passed to subprocess
  • All existing bash tool tests continue to pass (9/9)
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7067 **State:** closed **Merged:** No --- ## Summary Injects `OPENCODE_SESSION_ID` and `OPENCODE_MESSAGE_ID` into subprocess environment when executing bash commands, enabling spawned processes to identify their parent session context. ## Motivation Currently, when OpenCode spawns bash subprocesses, those processes have no way to identify which session they belong to. This limits: - **Session self-awareness**: Agents cannot query their own session history - **Cross-session communication**: Scripts cannot interact with session APIs - **Context propagation**: Tooling cannot correlate subprocess activity with sessions ## Changes - `packages/opencode/src/tool/bash.ts`: Added `OPENCODE_SESSION_ID` and `OPENCODE_MESSAGE_ID` to the subprocess environment - `packages/opencode/test/tool/bash.test.ts`: Added test verifying environment variable injection ## Use Cases ```bash # Agent can now read its own session history echo "Current session: $OPENCODE_SESSION_ID" # Scripts can interact with session APIs curl "http://localhost:4096/session/$OPENCODE_SESSION_ID/messages" # Cross-session communication patterns opencode-session-id info $OPENCODE_SESSION_ID ``` ## Testing - Added unit test that verifies both environment variables are correctly passed to subprocess - All existing bash tool tests continue to pass (9/9)
yindo added the pull-request label 2026-02-16 18:17:09 -05:00
yindo closed this issue 2026-02-16 18:17:09 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12236