# maxigp0/opencode-session-bridge architecture

> Architecture diagram of the GitHub repository maxigp0/opencode-session-bridge, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/maxigp0/opencode-session-bridge
- Repository: https://github.com/maxigp0/opencode-session-bridge

## Overview

opencode-session-bridge is an OpenCode v2 plugin for managing independent sessions and orchestrating work across them. The main workflow enters through the registered session tools, validates and authorizes requests, invokes OpenCode session APIs, then waits for the requested turn and formats bounded results. Separate capabilities cover transcript and permission management, ownership tracking, worktrees, and race candidates. The excerpts show selected calls; broader tool capabilities and some subsystem links are grounded in the README, so unsampled internal call paths are not inferred.

Main entry points: [`src/tools.ts`](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/tools.ts) (Session tools), [`src/orchestration.ts`](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/orchestration.ts) (Session orchestration), [`src/worktrees.ts`](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/worktrees.ts) (Worktree management), [`src/wait.ts`](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/wait.ts) (Turn waiting)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Plugin entry"]
  node_entrypoint["Plugin setup<br/>[index.ts]"]
  node_tools["Session tools<br/>[tools.ts]"]
  node_options["Options and policy<br/>[options.ts]"]
  node_validation["Input validation<br/>[validation.ts]"]
end

subgraph group_sessions["Session workflows"]
  node_wait["Turn waiting<br/>[wait.ts]"]
  node_runtime["Request runtime<br/>[runtime.ts]"]
  node_orchestration["Session orchestration<br/>[orchestration.ts]"]
  node_queue["Background queue<br/>[queue.ts]"]
  node_live["Live session state<br/>[live.ts]"]
  node_retry["Retry state<br/>[retry.ts]"]
end

subgraph group_state["State and output"]
  node_output["Structured output<br/>[output.ts]"]
  node_format["Transcript formatting<br/>[format.ts]"]
  node_i18n["Language strings<br/>[i18n.ts]"]
  node_registry[("Ownership registry<br/>[registry.ts]")]
end

subgraph group_projects["Project orchestration"]
  node_directories["Directory safety<br/>[directories.ts]"]
  node_worktrees["Worktree management<br/>[worktrees.ts]"]
  node_git["Git operations<br/>[git.ts]"]
  node_races["Race candidates<br/>[races.ts]"]
end

node_agent(("OpenCode agent"))
node_host["OpenCode host"]

node_agent -->|"invokes tools"| node_entrypoint
node_entrypoint -->|"registers"| node_tools
node_entrypoint -->|"configures"| node_options
node_tools -->|"validates input"| node_validation
node_tools -->|"guards requests"| node_runtime
node_tools -->|"calls session APIs"| node_host
node_tools -->|"waits for turns"| node_wait
node_tools -->|"delegates workflows"| node_orchestration
node_tools -->|"builds results"| node_output
node_wait -->|"reads events and sessions"| node_host
node_wait -->|"reads retry state"| node_retry
node_orchestration -->|"waits for turns"| node_wait
node_orchestration -->|"records ownership"| node_registry
node_orchestration -->|"analyzes candidates"| node_races
node_orchestration -->|"cleans worktrees"| node_worktrees
node_tools -->|"tracks queued prompts"| node_queue
node_tools -->|"reports live state"| node_live
node_output -->|"renders transcript"| node_format
node_format -->|"translates labels"| node_i18n
node_registry -->|"keys by directory"| node_directories
node_worktrees -->|"validates paths"| node_directories
node_worktrees -->|"runs Git helpers"| node_git
node_tools -->|"manages worktrees"| node_worktrees
node_tools -->|"runs race tools"| node_races
node_races -->|"uses patch helpers"| node_git
node_races -->|"uses candidate worktrees"| node_worktrees

click node_entrypoint "https://github.com/maxigp0/opencode-session-bridge/blob/main/index.ts"
click node_tools "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/tools.ts"
click node_options "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/options.ts"
click node_validation "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/validation.ts"
click node_wait "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/wait.ts"
click node_runtime "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/runtime.ts"
click node_orchestration "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/orchestration.ts"
click node_queue "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/queue.ts"
click node_live "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/live.ts"
click node_retry "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/retry.ts"
click node_output "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/output.ts"
click node_format "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/format.ts"
click node_i18n "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/i18n.ts"
click node_registry "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/registry.ts"
click node_directories "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/directories.ts"
click node_worktrees "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/worktrees.ts"
click node_git "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/git.ts"
click node_races "https://github.com/maxigp0/opencode-session-bridge/blob/main/src/races.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entrypoint,node_tools,node_options,node_validation toneBlue
class node_wait,node_runtime,node_orchestration,node_queue,node_live,node_retry toneAmber
class node_output,node_format,node_i18n,node_registry toneMint
class node_directories,node_worktrees,node_git,node_races toneRose
class node_agent,node_host toneIndigo
```

## Components

### Plugin entry

- [Plugin setup](https://github.com/maxigp0/opencode-session-bridge/blob/main/index.ts): `index.ts`
- [Session tools](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/tools.ts): `src/tools.ts`
- [Options and policy](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/options.ts): `src/options.ts`
- [Input validation](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/validation.ts): `src/validation.ts`

### Session workflows

- [Turn waiting](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/wait.ts): `src/wait.ts`
- [Request runtime](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/runtime.ts): `src/runtime.ts`
- [Session orchestration](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/orchestration.ts): `src/orchestration.ts`
- [Background queue](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/queue.ts): `src/queue.ts`
- [Live session state](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/live.ts): `src/live.ts`
- [Retry state](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/retry.ts): `src/retry.ts`

### State and output

- [Structured output](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/output.ts): `src/output.ts`
- [Transcript formatting](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/format.ts): `src/format.ts`
- [Language strings](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/i18n.ts): `src/i18n.ts`
- [Ownership registry](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/registry.ts): `src/registry.ts`

### Project orchestration

- [Directory safety](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/directories.ts): `src/directories.ts`
- [Worktree management](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/worktrees.ts): `src/worktrees.ts`
- [Git operations](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/git.ts): `src/git.ts`
- [Race candidates](https://github.com/maxigp0/opencode-session-bridge/blob/main/src/races.ts): `src/races.ts`

### Other components

- **OpenCode agent**
- **OpenCode host**

## Connections

- OpenCode agent → Plugin setup: invokes tools (evidence: `README.md`)
- Plugin setup → Session tools: registers (evidence: `index.ts`)
- Plugin setup → Options and policy: configures (evidence: `index.ts`)
- Session tools → Input validation: validates input (evidence: `src/tools.ts`)
- Session tools → Request runtime: guards requests (evidence: `src/tools.ts`)
- Session tools → OpenCode host: calls session APIs (evidence: `README.md`)
- Session tools → Turn waiting: waits for turns (evidence: `src/tools.ts`)
- Session tools → Session orchestration: delegates workflows (evidence: `src/orchestration.ts`)
- Session tools → Structured output: builds results (evidence: `src/tools.ts`)
- Turn waiting → OpenCode host: reads events and sessions (evidence: `src/wait.ts`)
- Turn waiting → Retry state: reads retry state (evidence: `src/wait.ts`)
- Session orchestration → Turn waiting: waits for turns (evidence: `src/orchestration.ts`)
- Session orchestration → Ownership registry: records ownership (evidence: `src/orchestration.ts`)
- Session orchestration → Race candidates: analyzes candidates (evidence: `src/orchestration.ts`)
- Session orchestration → Worktree management: cleans worktrees (evidence: `src/orchestration.ts`)
- Session tools → Background queue: tracks queued prompts (evidence: `README.md`)
- Session tools → Live session state: reports live state (evidence: `README.md`)
- Structured output → Transcript formatting: renders transcript (evidence: `src/output.ts`)
- Transcript formatting → Language strings: translates labels (evidence: `src/format.ts`)
- Ownership registry → Directory safety: keys by directory (evidence: `src/registry.ts`)
- Worktree management → Directory safety: validates paths (evidence: `src/worktrees.ts`)
- Worktree management → Git operations: runs Git helpers (evidence: `src/worktrees.ts`)
- Session tools → Worktree management: manages worktrees (evidence: `README.md`)
- Session tools → Race candidates: runs race tools (evidence: `README.md`)
- Race candidates → Git operations: uses patch helpers (evidence: `src/races.ts`)
- Race candidates → Worktree management: uses candidate worktrees (evidence: `README.md`)
