opencode
opencode copied to clipboard
feat: add gRPC transport alongside HTTP server
This PR introduces an opt-in gRPC transport to the OpenCode server, running alongside the existing HTTP interface. It provides a strongly-typed, backpressure-aware API surface suitable for headless clients, remote tooling, and multi-client workflows.
Scope
The gRPC transport is additive and optional. No existing HTTP behavior or APIs are changed.
What’s included
-
New gRPC server transport
- Enabled via
opencode serve --grpc [--grpc-port <N>] - Runs concurrently with the HTTP server
- Uses shared service logic to preserve parity
- Enabled via
-
Services exposed (
opencode.v1)ProjectService– list/get projectsSessionService– create/get/list sessionsEventService– server-streaming event subscription with cursor resumptionPtyService– bidirectional PTY streaming with strict lifecycle enforcementHealthService– standard gRPC health checks
-
Authentication parity
- Mirrors HTTP auth via
OPENCODE_TOKEN - Bearer token passed through gRPC metadata
UNAUTHENTICATED/PERMISSION_DENIEDmapped consistently
- Mirrors HTTP auth via
-
Workspace scoping
- Per-RPC workspace selection via
x-opencode-directorymetadata - Defaults to process CWD when omitted
- Per-RPC workspace selection via
-
Event persistence & resumption
- Disk-backed event log (
events.jsonl) - Configurable retention (count / bytes)
- Cursor-based resume with explicit reset contract when pruned
EventService.Infoexposes cursor bounds and retention limits
- Disk-backed event log (
-
PTY protocol guarantees
- Explicit state machine (
IDLE → OPEN → ACTIVE → CLOSED) - Strict validation of invalid transitions
- Ordered output delivery before exit
- Backpressure enforcement with deterministic termination
- Explicit state machine (
-
Operational hardening
- Message size limits
- Structured metrics in logs
- Optional gRPC reflection (env-gated)
- Health checks for readiness
-
Tests & verification
- gRPC integration tests
- HTTP ↔ gRPC conformance tests for shared functionality
- PTY protocol violation tests
- Typechecking verified
Non-goals
- No removal or deprecation of HTTP APIs
- No UI changes
- No breaking protocol changes (all protos are additive under
opencode.v1)
Why this belongs in core (not a plugin)
This defines a stable transport boundary, not an internal feature:
- Required for headless and multi-client usage
- Enforces backpressure, ordering, and lifecycle semantics
- Participates in core auth, workspace scoping, and persistence
- Versioned and testable as part of the public API surface
Docs
- Added
GRPC_TRANSPORT.mdas the implementation and usage reference - Includes configuration, limits, protocol semantics, and client guidance
Follow-ups
- Monitor production metrics post-release
- Collect client feedback during v1 stabilization window
- Defer any breaking changes to a future
opencode.v2