Ajay Krishnan 08ab6ea6fc Add focused smoke tests for the remaining 8 gem classes + adapter-raises edge
Sandi S2 (smoke coverage was 4/12) + Tobi T7 (adapter exception path
untested) consensus actions. 38 new tests across 9 files take total
from 15 -> 53 tests, 50 -> 134 assertions.

Per-class breakdown:

  session_test.rb                (2 tests)
    Contract on initialize parameters (positional record + keyword
    callables) and public surface (ensure!/recreate!/abort!/just_created?).
    AR fixtures stay in the host suite.

  turn_test.rb                   (4 tests)
    Required + optional keyword arg contracts (locks the 9 required
    + 8 optional keys against drift). Public surface = [:call] only.
    Result struct exercised as a value object with status predicates
    and cost/token delegation.

  message_artifacts_test.rb      (2 tests)
    Contract on initialize parameters; public surface = [:attach_from].

  impostor_test.rb               (3 tests)
    Initialize keyword contract; delegation to ActiveStorage attachment
    via a Struct double on #filename.

  sandbox_test.rb                (5 tests)
    Real tmpdir instantiation: #path / #exists? / #files (Enumerator
    when block-less, yields SandboxFile values when files present) /
    #file lookup-by-basename returns nil for missing.

  sandbox_file_test.rb           (4 tests)
    Real tmpdir + file: basic readers, marcel-backed content_type
    detection, #safe? size-cap rejection, #as_artifact identity
    conversion returns Opencode::Artifact.

  transform_test.rb              (8 tests)
    Documents the abstract contract (source_filename / destination_filename /
    render all raise NotImplementedError). A trivial concrete subclass
    inside the test exercises the default implementations of
    #applies_to?, #trusted?, #owned_filenames, and #purge_impostors?
    that delegate to the two abstract filename methods.

  tool_display_test.rb           (5 tests)
    Known tool canonicalization, status predicates (running/completed/
    errored/in_flight/terminal), unknown-tool fallback, nil-part
    tolerance (callers sometimes pass non-tool parts_json entries).

  uploaded_files_prompt_test.rb  (3 tests)
    Initialize keyword contract; #text returns raw content + empty
    sandbox_file_names map when no files attached; public surface check.

Plus 2 new tests in error_reporter_test.rb (C4):
  - test_adapter_exceptions_propagate: adapter that raises must
    propagate, not silently swallow — operators need to know the
    error tracker is broken.
  - test_report_returns_adapter_return_value: report passes through
    the adapter's return value verbatim (Rails.error.report returns
    the error itself; callers can chain).

Faithful to actual implementations: every test was first written from
the API I expected, then corrected against the class internals when
errors surfaced. The corrections themselves document the contract:
SandboxFile expects a String sandbox_prefix with trailing separator
(not a Pathname), Transform's filename methods are abstract not
nil-defaulting, etc.
2026-05-20 06:40:34 -07:00
2026-05-20 12:08:39 +00:00
2026-05-20 05:09:48 -07:00

opencode-rails

Production-grade OpenCode integration for Rails apps. Layers an ActiveRecord-aware session lifecycle, a turn orchestrator, an artifact pipeline, and a sandbox model on top of the wire-level client in opencode-ruby.

Alpha software. API will change before 1.0. Pin to a specific version.

Why this gem exists

opencode-ruby gives you a clean Net::HTTP + SSE client. To turn it into something a Rails app can ship, you need:

  1. Session lifecycle — idempotent create-or-resolve with row-level locking so concurrent jobs don't double-mint sessions; recreation for stale-session recovery; best-effort upstream abort on teardown.
  2. Turn orchestration — drive send_message_async, stream events into Opencode::Reply, recover from SessionNotFoundError / StaleSessionError by recreating + retrying, fetch the final exchange for cost/artifact extraction, finalize the persisted message via CAS against :pending.
  3. Mid-stream snapshotupdate_columns writes to bypass AR callbacks so app-level Turbo broadcasts fire on rate-limited intervals, not every SSE event.
  4. Artifact pipeline — extract files written by the agent (via the write tool's metadata, or by reading from a sandbox path), attach them to the assistant message via ActiveStorage, transform host-rendered artifacts (flight results HTML, etc.) before persisting.

opencode-rails ships these as named, single-responsibility objects so you wire them up instead of re-implementing them.

Install

# Gemfile
gem "opencode-ruby"   # wire client + Reply state machine
gem "opencode-rails"  # AR-coupled session/turn/artifact stack
bundle install

Runtime deps: activerecord, activestorage, activesupport (>= 7.1). Depends on opencode-ruby for the underlying HTTP/SSE primitives.

Quickstart

# config/initializers/opencode.rb
Opencode::Instrumentation.adapter = ->(name, payload, &blk) {
  ActiveSupport::Notifications.instrument(name, payload, &blk)
}
Opencode::ErrorReporter.adapter = ->(error, **opts) {
  Rails.error.report(error, **opts) if Rails.respond_to?(:error)
}
# app/jobs/generate_response_job.rb
class GenerateResponseJob < ApplicationJob
  def perform(assistant_message)
    conversation = assistant_message.conversation
    user_message = conversation.messages.where(role: :user).last
    client       = Opencode::Client.new(base_url: ENV["OPENCODE_URL"])

    session = Opencode::Session.new(
      conversation,
      permissions_for: ->(record) { permission_rules_for(record) },
      on_error:        ->(e, **opts) { Opencode::ErrorReporter.report(e, **opts) }
    )

    Opencode::Turn.new(
      message:        assistant_message,
      subject:        conversation,
      query_text:     user_message.content,
      client:         client,
      session:        session,
      on_turn_finished: ->(result) {
        # result.status   #=> :completed | :error | :cancelled
        # result.message  #=> the AR row (reloaded)
        # result.duration_ms
      }
    ).call
  end

  private

  def permission_rules_for(conversation)
    [
      { type: "edit", action: "allow", path: "data/sandbox/#{conversation.id}/" }
    ]
  end
end

The host's record (here conversation) must respond to #title, #opencode_session_id, #opencode_session_id=, #with_lock(&block), #update!, #reload, #id. The host's message record (here assistant_message) must respond to #error!(content), #update_columns(...), #with_lock(&block), #reload, #pending?.

What you get

Constant Role
Opencode::Session Idempotent ensure!/recreate!/abort! with row-level locking
Opencode::Turn End-to-end orchestrator: ensure session → send → stream → recover → finalize
Opencode::Exchange Domain object over a turn's message array; owns artifact extraction
Opencode::Artifact Value object: filename + bytes + content_type + trust metadata
Opencode::MessageArtifacts Idempotent attachment pipeline (ActiveStorage-aware, transform-applying)
Opencode::Sandbox Reads written files from disk, returns Artifact list
Opencode::SandboxFile Single-file value: pathname → bytes/content-type/identity Artifact
Opencode::Transform Base class for content-rewriting transforms (e.g. host-rendered HTML)
Opencode::Impostor ActiveStorage download/upload helper that round-trips bytes
Opencode::UploadedFilesPrompt Builds the user-prompt prefix listing uploaded files
Opencode::ToolDisplay View-model converting tool-call hashes to Turbo-friendly props
Opencode::ErrorReporter Pluggable adapter for routing swallowed errors

Plus everything from opencode-ruby: Client, Reply, ReplyObserver, Tracer, Prompts, ResponseParser, ToolPart, PartSource, Todo, Instrumentation, the error hierarchy.

Instrumentation + error reporting

The gem emits events through Opencode::Instrumentation.instrument(name, payload, &blk) and reports swallowed errors through Opencode::ErrorReporter.report(error, **opts). Both are no-ops by default. Wire your host's emitter / reporter in an initializer (see Quickstart above).

Events emitted (non-exhaustive):

  • opencode.turn.started, opencode.turn.finished
  • opencode.stream.completed, opencode.stream.interrupted
  • opencode.session.created, opencode.session.recreated
  • opencode.apply_patch.artifacts_dropped
  • opencode.response.upstream_error

Subscribe via ActiveSupport::Notifications.subscribe("opencode.*") once you've wired the adapter.

Position

opencode-rails is for Rails apps that want production-grade OpenCode streaming without reimplementing the orchestration boilerplate. If you want only the wire client, use opencode-ruby directly. If you want every endpoint with generated types, use opencode_client (Eric Guo's auto-generated gem).

License

MIT. See LICENSE.

Description
Production-grade Rails integration for OpenCode. ActiveRecord-aware session lifecycle, Turbo-friendly streaming, mid-stream snapshot pattern.
Readme MIT 141 KiB
Languages
Ruby 100%