Skip to content

feat(tool/bash): expose session context via environment variables#7067

Closed
oussamadouhou wants to merge 2 commits into
anomalyco:devfrom
oussamadouhou:feat/session-id-env-var
Closed

feat(tool/bash): expose session context via environment variables#7067
oussamadouhou wants to merge 2 commits into
anomalyco:devfrom
oussamadouhou:feat/session-id-env-var

Conversation

@oussamadouhou

Copy link
Copy Markdown

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)

Inject OPENCODE_SESSION_ID and OPENCODE_MESSAGE_ID into subprocess environment,
enabling spawned processes and scripts to identify their parent session context.

This enables:
- Session self-awareness for agents using bash commands
- Cross-session communication patterns
- Session history access from scripts
Verifies that OPENCODE_SESSION_ID and OPENCODE_MESSAGE_ID are correctly
passed to subprocess environment when executing bash commands.
@github-actions

github-actions Bot commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

@oussamadouhou
oussamadouhou force-pushed the feat/session-id-env-var branch from e8e8094 to 9d389d4 Compare January 6, 2026 09:10
@oussamadouhou

Copy link
Copy Markdown
Author

Closing - will resubmit with expanded scope after further review

@oussamadouhou
oussamadouhou deleted the feat/session-id-env-var branch January 6, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant