OpenCode
The OpenCode harness — hook adapter, module integrations, stats, and runtime footprint.
OpenCode is the default agent runtime DevBot plugs into. Each harness lives in src/harnesses/<name>/ and is self-contained — it owns its hooks, wiring and config templates, while business logic stays in the agentic modules’ tools/ and the harness only adapts events into commands.
Selection
harness: opencode (project or global config) selects this harness — and selects it alone: its init.sh/reset.sh, opencode.jsonc registration and plugin/skill wiring run, while the other harness is treated as disabled. The root harness key is the only switch; an opencode entry in the modules map is silently ignored.
The adapter
src/harnesses/opencode/hooks/on-hooks.ts is a single plugin: one event handler plus tool.execute.before / tool.execute.after cover all six semantic events declared by the agentic modules’ hooks.json manifests.
| Event | Meaning | OpenCode mapping |
|---|---|---|
file.edited |
a file was saved | event (type file.edited) |
command.before |
a shell command is about to run | tool.execute.before (blocking) |
command.after |
a shell command finished | tool.execute.after (success-gated) |
session.idle |
the session went quiet | event (type session.idle) |
session.created |
a session started | event (type session.created) |
session.error |
a transient provider error | event (type session.error) |
skipOnCreate: true is honoured by this adapter only — it skips dispatch when a file.edited is a file create, so a rewriting hook such as a formatter never normalizes a freshly-written file before the agent’s next edit. blocking: true blocks when the command prints {"blocked": true, "message": "…"}. See Hooks for the manifest schema, the run placeholders and the match filters.
Module integrations
- guards —
command.before, blocking. Registered as atool.execute.beforeplugin that intercepts every shell tool call: the bash tool, and the PTY tools that launch or feed a command (pty_spawn,pty_write). No manual configuration needed. - format-md / format-json / format-yml —
file.editedwithskipOnCreate: true; the formatter runs in the background on save. - graphify — exposes the CLI as an OpenCode custom tool (
src/agentic/graphify/tools/opencode/graphify.ts) with subcommandsquery,path,explain,update,graph-stats,god-nodes, plus aserve-backed MCP server.
Version support
dev-bot targets the opencode 1.x line and refuses a 2.x host. Its whole plugin surface — the server hooks (hooks/on-hooks.ts) and the TUI plugins — is the V1 plugin API, which v2 replaced, so on a v2 host none of it loads while the seeded config still applies: the install half-works with no console signal. v2 also moved terminal config from tui.json to a global cli.json and stopped running language servers, so the LSP override below is inert there.
_devbot_require_opencode_1x (functions.sh) reads ~/.opencode/bin/opencode --version and, when the major is ≥ 2, prints the breakages and fails. A leading v on the version is tolerated. up.sh surfaces it on devbot / devbot up / make up; start.sh re-runs it, and only there is the launch actually refused — _run_service_scripts warns about a module up.sh’s non-zero exit and returns 0. Anything but a confidently-detected 2.x is allowed: a missing binary is start.sh’s own error, and a version with no leading digit is warned about, never fatal — a source build reporting 0.0.0--<ts> parses to a major of 0 and is allowed silently.
Stats
src/harnesses/opencode/stats.py reads the OpenCode SQLite database read-only. Cost and tokens are recorded per assistant step, so they are split evenly across the tools that step invoked (cost_kind: estimated). MCP tools are recognised by the server names declared in the project’s OpenCode config (mcp block and .opencode/*.mcp.json), so native tools whose names contain underscores are not mistaken for MCP tools. It also aggregates bash/pty_spawn/skill/grep/glob arguments — shell invocations are normalised to program subcommand (first two tokens) after stripping a leading cd … &&, with pty_spawn’s executable and argument array re-joined first so both channels read alike.
The adapter contract is harness-agnostic — see the devbot stats reference.
Runtime footprint
Each harness instance starts its own helper processes. Two levers keep that cost down per project.
LSP servers
opencode auto-starts a language server for every language it detects in the project (the lsp field in opencode.jsonc). Each server is a separate process costing roughly 100–250 MB per instance — measured in this repo: pyright ~119 MB, bash-language-server ~107 MB; on PHP projects intelephense is the heaviest.
The shipped opencode.jsonc sets lsp to an object that overrides the built-in "php intelephense" server: it trims Intelephense’s index to skip vendor/**/tests, vendor/**/docs and nested vendor trees while keeping vendor library symbols resolvable, caps its heap via NODE_OPTIONS, and launches it through lsp/intelephense.sh — which prefers the Intelephense version devbot pins (versions.env, seeded from the committed versions.env.dist) over the older copy opencode bundles. Intelephense only reads these settings through LSP workspace/configuration, which opencode answers from the override’s initialization; a project-root intelephense.config.json is not read.
opencode starts one server per directory it walks into and never tears a stale one down, so a long session accumulates idle servers in directories it has since left. lsp/reap.sh reclaims them: it walks the session’s opencode process tree and kills only servers that are provably not working — no CPU tick between two samples, or a working directory that no longer exists (a removed worktree) — leaving an active index alone. devbot:reconcile-work runs it on every wrap-up (silently, decisions logged to .agents/logs/lsp-reap.log); a reaped server respawns on demand the next time its directory is visited. --dry-run prints the classification without killing anything.
The object form overrides named servers only, keeping every other built-in enabled, and the key must be the built-in id ("php intelephense") — a key like "intelephense" names a custom server and silently does nothing. To disable a language server:
// opencode.jsonc
"lsp": {
"php intelephense": { "disabled": true }
}
MCP servers
See MCP configuration — heavy servers are dropped by disabling their module.
What disabling may take back is decided by the ownership record, not by the module’s name: init.sh
records every MCP key and server-plugin spec it writes in .opencode/.devbot-managed-mcps, and
reset.sh removes exactly those, leaving a server or plugin the project registered under a module’s
name alone with a notice. The rule comes from _devbot_prune_managed_entry in
src/_shared/functions.sh. Only opencode.jsonc is dev-bot’s to edit: .opencode/opencode.json is
opencode’s own project config, so an entry there survives whatever its name, with a notice. TUI specs
in .opencode/tui.json are not recorded and no reset prunes that file. The retired-key sweeps
(RETIRED_MCP_KEYS, RETIRED_MCP_KEY_PREFIXES) follow the same rule: a recorded key is removed,
an unrecorded one — a project’s own qmd, or an install predating the record — is reported as
residue for the user to remove.
TUI plugins
opencode loads two separate plugin surfaces, and the distinction is enforced by their schemas:
| Surface | File | Takes |
|---|---|---|
| Server | opencode.jsonc → plugin |
plugins that hook events and register tools |
| TUI | .opencode/tui.json → plugin |
plugins that render into the terminal UI |
opencode.json’s schema declares no tui key and sets additionalProperties: false, so a TUI plugin listed there is schema-invalid and opencode refuses to start. Each surface therefore gets its own template: opencode.dist.jsonc and tui.dist.jsonc, both applied by _write_jsonc_from_dist on first init and user-owned afterwards.
Unlike hooks, TUI plugins are not auto-discovered — they only load if named in tui.json’s plugin array. dev-bot’s own TUI plugins are symlinked into .opencode/tui-plugins/, removed by reset.sh, and referenced from the template by a path relative to tui.json, so the shipped template carries no install path. The link and the entry are one invariant, so both are converged through _ensure_tui_plugin (in src/_shared/functions.sh): init.sh calls it for every name in its farm, and up.sh calls it for the herdr plugin on a launch.
tui.json also accepts plugin_enabled, which switches off opencode’s built-in TUI blocks by slot id. The shipped template disables sidebar-context only — the footer already shows context usage, so the sidebar copy is a duplicate. internal:sidebar-files is deliberately left enabled: it is the only file tree.
A dist-backed config is seed-once: init.sh writes it only when the file is absent, so from then on it is user-owned and a later change to a dist never reaches an existing project. Additions are reconciled, removals are not — every plugin the dists ship is re-added to an already-seeded config on every init/reinit, and the herdr plugin also on a devbot up (mirrored in required-plugins.jsonc, with a test asserting each dist entry appears there). Nothing is ever removed: “remove anything the dist does not list” would delete a project’s own plugins.
PTY monitor
src/harnesses/opencode/pty-monitor/ is dev-bot’s own TUI plugin: it lists opencode-pty sessions in the sidebar (collapsible, with a bullet tinted green while running, red on a non-zero exit, muted otherwise) and opens a session’s live output in a dialog on click. It also removes them on demand — a ✕ on each row and a (clear finished) action in the header. opencode-pty keeps a session in its in-memory map until it is cleaned up (a plain kill retains it for log access), so finished sessions otherwise accumulate for the life of the opencode process — every sleep-wait and make test run leaves one behind. Removing a running session kills a live process, so it asks first; clearing finished ones never touches a live session, so it does not.
opencode-pty is a hard dependency — its HTTP API is the only window onto PTY sessions. Its server binds a random port and publishes it only by posting a message into the session; dev-bot reads the port from /proc instead (its own listening sockets). Starting the server is never a side effect of load: opencode awaits every TUI plugin factory before the TUI is usable, and a start goes through one of opencode-pty’s commands, which runs server initialisation and needs a throwaway session. So loading resolves an already-running server by discovery alone, and a start happens only when asked — expanding the panel (collapsed by default) or running /pty-monitor starts it inside a throwaway session that is then deleted, so the URL message never litters your transcript. Off Linux there is no /proc, so that bootstrap is what resolves the origin.
Handing over from the v2 adapter
The v2 harness links its own plugins as directory packages under .opencode/plugins/
(<name>/index.ts), which this one never registers. init.sh retires them on the
way in (_sweep_v2_plugin_dirs, keyed on the shim marker) and drops bun-pty
from .opencode/package.json (_retire_v2_pty_dependency) — the v2 PTY
extension is the only thing that declared it, and a host that installs the config
directory’s dependencies on start would otherwise fetch it for a plugin nothing
loads.
Tool-details default
src/harnesses/opencode/tui-defaults/ seeds dev-bot’s opinion that tool details start hidden. “Hide tool details” is TUI runtime state — the tool_details_visibility boolean in opencode’s kv store — not a config key, so no dist template can set it and a fresh install opens with details shown. The plugin writes the value at startup through the same reactive kv store opencode’s own toggle uses.
The seed fires only while the key is absent: a value already in kv is an explicit choice and is never overridden, and the key being present makes every later start a no-op. Toggling the setting in the TUI is therefore the opt-out — an install or update never undoes it.
Provider TTFT
src/harnesses/opencode/provider-ttft/ shows the active session’s last request time-to-first-token as TTFT 1.2s in the sidebar, in place of opencode’s built-in context block: it registers sidebar_content at order 100 — the row internal:sidebar-context used — which stays disabled in plugin_enabled, as the dist template already ships it. Panels there are additive, so the built-in row can be occupied but never extended. The row shows - until that session’s first measurement.
opencode publishes no provider-latency metric, so the value is derived: an assistant message’s time.created starts the clock and the first message.part.delta for that message stops it (deltas carry no timestamp, so the handler stamps Date.now()). The first reading for a request is final, and the entry is dropped when the message completes. A request that fails or is aborted before its first token therefore leaves that session’s previous reading in place — the panel shows the last measurement, never a blank. Readings are kept per session, so switching sessions shows each session’s own value; they include any retry backoff before the first token, and assume the TUI and the opencode server share a clock. A remote server running behind the TUI inflates the reading, while one running ahead makes the delta look earlier than time.created — a negative duration, which the plugin discards, so that session keeps its previous value.
herdr pane title
src/harnesses/opencode/herdr-pane-title/ puts the pane’s root session title in the herdr pane border. herdr names a pane from a manual label, or the agent kind when none is set, and exposes no setting for the terminal title the pane’s own program emits — that OSC stops at the herdr boundary. Its CONTRIBUTING.md closes unsolicited pull requests automatically, so the gap is filled from this side instead: the plugin runs herdr pane rename <pane> <title> whenever the title of the session shown in the pane changes.
The label follows the root session. A pane showing a subagent walks parentID up and keeps the root’s title, so a border names the work rather than the delegated slice. An untitled session — or a route that is not a session at all — clears a label this plugin set, while a label set by hand is never touched, and the same clear runs on dispose so a pane reused for shell work does not keep describing a session nothing is showing. Titles come from session.created/session.updated rather than from state.session, whose cache lags both the title opencode generates shortly after a session is created and every later rename; a switch to another session emits no event at all, so a 500 ms poll catches that and nothing else. herdr is spawned only when the rendered title differs from the one already applied — the poll is otherwise a Map lookup.
The plugin is inert outside a herdr pane: without HERDR_ENV=1 and HERDR_PANE_ID the factory returns before polling, and on a 1.x older than the session cache it returns rather than throwing on the first sync — dev-bot’s own guard only refuses 2.x. The binary is HERDR_BIN_PATH when herdr exports one and herdr on PATH otherwise, so the PATH-installed case the wiring already advertises is not left inert. up.sh converges the link and the tui.json entry only where herdr is installed (HERDR_BIN_PATH counts as installed, since herdr sets it on hosts where the binary is not on PATH), because the wiring is useless without the terminal.
Configuration
opencode.jsonc and .opencode/tui.json are written once from the harness templates (opencode.dist.jsonc, tui.dist.jsonc), then treated as user-owned — this is why an lsp override survives devbot reinit. Config is read once at startup: restart opencode after editing.
See also
- Claude Code — the other harness
- Hooks — the manifest schema and semantic events
- MCP configuration — shared gateways and footprint