OpenCode Context Sharing: Standard Environment vs. Memoreal MCP Integration
| Architecture Aspect | Standard OpenCode Setup | OpenCode + Memoreal MCP | Impact on Parallel Orchestration |
|---|---|---|---|
| Worktree Memory Scope | Fragmented per worktree folder path | Automatically stripped to root repo slug | Agents in .worktrees/feat-x access main repo knowledge |
| Multi-Agent Handoffs | Ephemeral text summaries in chat | Dedicated handoff_to_agent MCP tool | Architect passes high-salience invariants directly to QA |
| Belief Consistency | Prone to hallucination across forks | Consolidated graph with Supersedes links | Ensures all subagents operate on identical truth |
| Token Overhead | Pasting multi-KB markdown per worker | Deterministic token-budgeted build_context | Drastically lowers subagent context token consumption |
The Architecture of OpenCode Multi-Agent Subagent Workflows
OpenCode is engineered specifically for multi-agent software engineering, orchestrating complex development pipelines across specialized agent wings including architect, reviewer, qa, security, and ops.
To ensure complete isolation and prevent branch contamination, OpenCode executes autonomous workstreams within temporary Git worktrees located under .worktrees/feat-*. However, in naive setups, this file-level isolation isolates agent memory as well, preventing workers from accessing knowledge discovered by sister agents.
- OpenCode provisions ephemeral worktrees to allow parallel test execution and code changes.
- Isolated file leases prevent concurrent workers from colliding on source code edits.
- Without shared persistent memory, each worker is forced to rediscover project rules from scratch.
Automated Worktree Path Normalization and Canonical Namespaces
Memoreal solves the worktree isolation dilemma through automated path normalization. When an OpenCode subagent executes inside /Users/dev/repo/.worktrees/feat-billing/, Memoreal automatically strips the .worktrees/* segment from the path.
The memory engine resolves the underlying Git root and canonical manifest, binding the subagent directly to the project canonical namespace (e.g., repo-billing-service). Whether an agent is working in the root directory or in worktree number 42, it queries and updates the exact same consolidated cognitive memory graph.
- Path normalization strips /.worktrees/*, /.claude/worktrees/*, and /.opencode/worktrees/*.
- Canonical namespace binding eliminates fragmented, disconnected memory silos across branches.
- Subagents can write memories during feature development that immediately ground QA agents.
Executing Multi-Agent Handoffs with handoff_to_agent
In addition to shared repository memory, Memoreal provides a specialized handoff_to_agent tool designed for multi-agent workflows. When an architect agent designs an implementation specification, it invokes handoff_to_agent to deposit a 0.9 salience memory into the destination wing namespace (e.g., wing:qa).
When the QA worker initializes in its own worktree, it reads its dedicated wing namespace, immediately discovering verified test criteria, modified file paths, and potential risk areas without consuming thousands of chat history tokens.
- handoff_to_agent attaches file paths and prior memory UUIDs as searchable metadata context tags.
- Target wings (architect, reviewer, qa, security, ops) maintain isolated, priority handoff queues.
- High salience guarantees that critical handoff constraints are prioritized in downstream context.
Configuring Memoreal MCP in opencode.json for Parallel Subagents
json{
"mcp": {
"servers": {
"memoreal": {
"type": "command",
"command": "npx -y @kolivri/memoreal-mcp@latest",
"environment": {
"MEMOREAL_API_KEY": "{env:MEMOREAL_API_KEY}"
}
}
}
}
}
Frequently Asked Questions
How does Memoreal recognize that a process is running inside a Git worktree?
Memoreal inspects the current working directory for known worktree patterns like /.worktrees/* and reads the git common directory (.git/worktrees/*) to locate the canonical project root.
Can multiple parallel subagents write to Memoreal simultaneously without data races?
Yes, Memoreal is built with concurrent atomic operations and transactional belief revision, ensuring that parallel writes from multiple workers never corrupt the cognitive graph.
Does OpenCode require any special flags to pass environment variables to MCP servers?
In opencode.json, you can reference system environment variables using the {env:VARIABLE_NAME} interpolation syntax, which securely forwards your credentials to the MCP process.
Install Persistent Memory for Your Coding Agent
Sub-15ms deterministic recall, AST symbol navigation, and 70%+ token savings across Claude Code, Cursor, Codex CLI, and Windsurf.
npx -y @kolivri/memoreal-mcp setup