class LittleGhost::Harnesses::OpenCode
Runs OpenCode with opencode run --format json --agent build.
In :isolated mode every tool permission is granted without prompting and the provider’s API key is the placeholder. In :native_guards mode OpenCode may read and edit files in the working directory, every other permission, including shell commands, web access, and directories outside the working directory, is denied, and OpenCode authenticates from the inherited environment. Project configuration in the working directory is ignored, automatic updates and sharing are disabled, the harness adds only the request’s mcp_servers, and OpenCode’s XDG directories live under home. OpenCode merges this configuration over its global configuration, so MCP servers or other settings in a persistent home still apply.
defaults accepts:
:provider-
OpenCodeprovider identifier, such as"openrouter". Requests name models asprovider/model. :base_url-
BaseURL for that provider, for example aNetwork::Facadeaddress. :api_key-
Placeholder API key for that provider in
:isolatedmode. A gateway replaces it. :model-
Modelused when the request does not name one. :config-
Additional
OpenCodeconfiguration merged into the generated configuration.
OpenCode has no reasoning effort option, so it ignores a request’s effort.
OpenCode reports cost and token usage after each step, so run stops it when cost exceeds budget_usd. Each finished step counts toward max_turns.
Public Instance Methods
Source
# File lib/little_ghost/harnesses/open_code.rb, line 61 def command(request) arguments = [executable, "run", "--format", "json", "--agent", "build"] model = request.model || defaults[:model] arguments.push("--model", model.to_s) if model arguments.push("--session", request.session_id) if request.session_id arguments.concat(positional(prompt_with_instructions(request))) end
Returns the OpenCode command line for request.
Source
# File lib/little_ghost/harnesses/open_code.rb, line 70 def environment(request) { "OPENCODE_PERMISSION" => JSON.generate(isolated? ? {"*" => "allow"} : NATIVE_PERMISSIONS), "OPENCODE_DISABLE_PROJECT_CONFIG" => "1", "OPENCODE_DISABLE_AUTOUPDATE" => "1", "OPENCODE_CONFIG_CONTENT" => JSON.generate(configuration(request)), "XDG_CONFIG_HOME" => File.join(home, ".config"), "XDG_DATA_HOME" => File.join(home, ".local", "share"), "XDG_CACHE_HOME" => File.join(home, ".cache"), "XDG_STATE_HOME" => File.join(home, ".local", "state") } end
Returns OpenCode’s permission, configuration, and directory settings.
Source
# File lib/little_ghost/harnesses/open_code.rb, line 84 def parse_line(line, state) data = JSON.parse(line) return [] unless data.is_a?(Hash) state.session_id = data["sessionID"] if data["sessionID"].is_a?(String) part = data["part"].is_a?(Hash) ? data["part"] : {} case data["type"] when "step_start" then step_start(state) when "text" state.text = part["text"].to_s [{kind: :text, text: state.text}] when "tool_use" then tool_events(part) when "step_finish" then step_finish(part, state) when "error" state.status = :failed error = data["error"] state.stop_reason = (error.is_a?(Hash) && error["name"].is_a?(String)) ? error["name"] : "error" [] else [] end end
Parses one run --format json event.