Simplify Go installation and support standard OpenCode releases

This commit is contained in:
2026-10-08 23:17:38 +01:00
parent 0f30a001c2
commit accbe63e39
101 changed files with 1331 additions and 682 deletions
+261
View File
@@ -0,0 +1,261 @@
// Package opencode adapts the two published OpenCode API families without installing a CLI.
package opencode
import (
"bufio"
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
"time"
)
const MaxRequest = 64 << 10
const MaxResponse = 1 << 20
type Connection struct {
Backend, Executable, Directory string
URL string
password string
process *exec.Cmd
done chan error
models map[string]map[string]string
responses map[string][]byte
}
// Reference records only our ephemeral loopback service credentials, never provider auth.
// The private file lets crash recovery abort the same owned service, not a new instance.
func (c *Connection) Reference() (string, error) {
cache, err := os.UserCacheDir()
if err != nil {
return "", err
}
dir := filepath.Join(cache, "pipelines", "private")
if err = os.MkdirAll(dir, 0700); err != nil {
return "", err
}
f, err := os.CreateTemp(dir, "opencode-connection-*.json")
if err != nil {
return "", err
}
data, _ := json.Marshal(map[string]string{"url": c.URL, "password": c.password, "directory": c.Directory})
_, writeErr := f.Write(data)
closeErr := f.Close()
if writeErr != nil {
return "", writeErr
}
if closeErr != nil {
return "", closeErr
}
return f.Name(), nil
}
func FromReference(path string) (*Connection, error) {
info, err := os.Lstat(path)
if err != nil {
return nil, err
}
if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size()>MaxRequest {
return nil, fmt.Errorf("invalid private OpenCode connection reference")
}
if stat, ok := info.Sys().(*syscall.Stat_t); !ok || stat.Uid != uint32(os.Getuid()) {
return nil, fmt.Errorf("OpenCode reference belongs to another user")
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var reference struct{ URL, Password, Directory string }
if err = json.Unmarshal(data, &reference); err != nil {
return nil, err
}
u, err := url.Parse(reference.URL)
if err != nil || u.Scheme != "http" || u.Hostname() != "127.0.0.1" || u.Port() == "" || u.Path != "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || len(reference.Password) != 64 {
return nil, fmt.Errorf("invalid owned OpenCode service endpoint")
}
return &Connection{Backend: "v1", URL: reference.URL, password: reference.Password, Directory: reference.Directory}, nil
}
func Executable() string {
if path := os.Getenv("PIPELINES_OPENCODE"); path != "" {
return path
}
return "opencode"
}
// Detect checks the advertised command interface, not an assumed release number.
func Detect(ctx context.Context, executable string) (string, error) {
cmd := exec.CommandContext(ctx, executable, "--help")
data, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("cannot run the selected OpenCode CLI: %w", err)
}
if regexp.MustCompile(`(?m)^\s*(?:opencode\s+)?api\s`).Match(data) {
return "v2", nil
}
if regexp.MustCompile(`(?m)^\s*(?:opencode\s+)?serve\s`).Match(data) {
return "v1", nil
}
return "", fmt.Errorf("selected OpenCode CLI advertises neither api nor serve")
}
func Open(ctx context.Context, executable, directory, backend string) (*Connection, error) {
if executable == "" {
executable = Executable()
}
if directory == "" {
var err error
directory, err = os.Getwd()
if err != nil {
return nil, err
}
}
if backend == "" {
var err error
backend, err = Detect(ctx, executable)
if err != nil {
return nil, err
}
}
c := &Connection{Backend: backend, Executable: executable, Directory: directory, models: map[string]map[string]string{}, responses: map[string][]byte{}}
if backend == "v2" {
return c, nil
}
if backend != "v1" {
return nil, fmt.Errorf("unknown OpenCode backend %q", backend)
}
secret := make([]byte, 32)
if _, err := rand.Read(secret); err != nil {
return nil, err
}
c.password = hex.EncodeToString(secret)
c.process = exec.Command(executable, "serve", "--hostname", "127.0.0.1", "--port", "0")
c.process.Dir = directory
for _, entry := range os.Environ() {
if !strings.HasPrefix(entry, "OPENCODE_SERVER_PASSWORD=") && !strings.HasPrefix(entry, "OPENCODE_SERVER_USERNAME=") {
c.process.Env = append(c.process.Env, entry)
}
}
c.process.Env = append(c.process.Env, "OPENCODE_SERVER_PASSWORD="+c.password, "OPENCODE_SERVER_USERNAME=pipelines")
stdout, err := c.process.StdoutPipe()
if err != nil {
return nil, err
}
c.process.Stderr = io.Discard
if err = c.process.Start(); err != nil {
return nil, err
}
c.done = make(chan error, 1)
go func() { c.done <- c.process.Wait() }()
address := make(chan string, 1)
go func() {
scanner := bufio.NewScanner(stdout)
pattern := regexp.MustCompile(`http://127\.0\.0\.1:[0-9]+`)
sent := false
for scanner.Scan() {
if !sent {
if found := pattern.FindString(scanner.Text()); found != "" {
address <- found
sent = true
}
}
}
}()
startup, stop := context.WithTimeout(ctx, 20*time.Second)
defer stop()
select {
case c.URL = <-address:
case <-c.done:
c.process = nil
return nil, fmt.Errorf("OpenCode serve exited before becoming ready")
case <-startup.Done():
c.Close()
return nil, fmt.Errorf("OpenCode serve startup: %w", startup.Err())
}
health, err := c.Native(startup, "get", "/global/health", nil)
var result struct {
Healthy bool `json:"healthy"`
Version string `json:"version"`
}
if err != nil || json.Unmarshal(health, &result) != nil || !result.Healthy || result.Version == "" {
c.Close()
return nil, fmt.Errorf("OpenCode serve did not return the expected native health contract")
}
return c, nil
}
func (c *Connection) Close() {
if c.process == nil {
return
}
_ = c.process.Process.Signal(syscall.SIGTERM)
select {
case <-c.done:
case <-time.After(5 * time.Second):
_ = c.process.Process.Kill()
<-c.done
}
c.process = nil
}
func (c *Connection) Native(ctx context.Context, method, path string, body any) ([]byte, error) {
u, err := url.Parse(c.URL + path)
if err != nil {
return nil, err
}
query := u.Query()
query.Set("directory", c.Directory)
u.RawQuery = query.Encode()
var data []byte
if body != nil {
data, err = json.Marshal(body)
if err != nil {
return nil, err
}
if len(data) > MaxRequest {
return nil, fmt.Errorf("OpenCode request exceeds 64 KiB")
}
}
request, err := http.NewRequestWithContext(ctx, strings.ToUpper(method), u.String(), bytes.NewReader(data))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/json")
request.SetBasicAuth("pipelines", c.password)
response, err := http.DefaultClient.Do(request)
if err != nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
return nil, fmt.Errorf("OpenCode native API request failed")
}
defer response.Body.Close()
if response.StatusCode < 200 || response.StatusCode >= 300 {
return nil, fmt.Errorf("OpenCode %s %s failed (HTTP %d)", method, path, response.StatusCode)
}
if !strings.HasPrefix(response.Header.Get("Content-Type"), "application/json") {
return nil, fmt.Errorf("OpenCode %s %s returned a non-JSON API response", method, path)
}
data, err = io.ReadAll(io.LimitReader(response.Body, MaxResponse+1))
if err != nil {
return nil, err
}
if len(data) > MaxResponse {
return nil, fmt.Errorf("OpenCode response exceeds 1 MiB")
}
if !json.Valid(data) {
return nil, fmt.Errorf("OpenCode returned invalid JSON")
}
return data, nil
}
+67
View File
@@ -0,0 +1,67 @@
package opencode
import (
"context"
"encoding/json"
"os"
"testing"
"time"
)
// This test does no inference and never uses or copies the user's login.
func TestStableNativeSessionContractLive(t *testing.T) {
executable := os.Getenv("PIPELINES_TEST_OPENCODE_V1")
if executable == "" {
t.Skip("set PIPELINES_TEST_OPENCODE_V1 for no-inference stable CLI contract test")
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", home+"/config")
t.Setenv("XDG_DATA_HOME", home+"/data")
t.Setenv("XDG_CACHE_HOME", home+"/cache")
t.Setenv("XDG_STATE_HOME", home+"/state")
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
defer cancel()
c, err := Open(ctx, executable, home, "")
if err != nil {
t.Fatal(err)
}
defer c.Close()
if c.Backend != "v1" {
t.Fatal("expected native serve backend")
}
data, err := c.Call(ctx, "post", "/api/session", map[string]any{"title": "Pipelines contract test", "model": map[string]string{"providerID": "opencode-go", "id": "deepseek-v4.1-flash"}, "permissions": []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}, {"action": "read", "resource": "*", "effect": "allow"}, {"action": "shell", "resource": "go test ./...", "effect": "allow"}}})
if err != nil {
t.Fatal(err)
}
var result struct {
Data struct {
ID string `json:"id"`
} `json:"data"`
}
json.Unmarshal(data, &result)
defer c.Call(context.Background(), "delete", "/api/session/"+result.Data.ID, nil)
data, err = c.Native(ctx, "get", "/session/"+result.Data.ID, nil)
if err != nil {
t.Fatal(err)
}
var session struct {
Permission []map[string]string `json:"permission"`
}
json.Unmarshal(data, &session)
if len(session.Permission) != 3 || session.Permission[2]["permission"] != "bash" {
t.Fatal("scoped native permission contract not retained", string(data))
}
ref, err := c.Reference()
if err != nil {
t.Fatal(err)
}
defer os.Remove(ref)
recovered, err := FromReference(ref)
if err != nil {
t.Fatal(err)
}
if _, err = recovered.Call(ctx, "post", "/api/session/"+result.Data.ID+"/interrupt", nil); err != nil {
t.Fatal("cannot abort same owned service via durable reference", err)
}
}
+183
View File
@@ -0,0 +1,183 @@
package opencode
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"path/filepath"
)
var sessionID = regexp.MustCompile(`^ses[A-Za-z0-9_-]+$`)
func Permissions(rules []map[string]string) []map[string]string {
result := make([]map[string]string, 0, len(rules))
for _, rule := range rules {
permission := rule["action"]
if permission == "shell" {
permission = "bash"
}
result = append(result, map[string]string{"permission": permission, "pattern": rule["resource"], "action": rule["effect"]})
}
return result
}
func messageText(data []byte) (string, error) {
var message struct {
Info struct {
Role string `json:"role"`
Finish string `json:"finish"`
Error json.RawMessage `json:"error"`
Time struct {
Completed int64 `json:"completed"`
} `json:"time"`
} `json:"info"`
Parts []struct{ Type, Text string } `json:"parts"`
}
if json.Unmarshal(data, &message) != nil || message.Info.Role != "assistant" || message.Info.Finish != "stop" || message.Info.Time.Completed == 0 {
return "", fmt.Errorf("OpenCode returned no completed assistant result")
}
if len(message.Info.Error) > 0 && string(message.Info.Error) != "null" {
return "", fmt.Errorf("OpenCode agent reported an execution error")
}
var text string
for _, part := range message.Parts {
if part.Type == "text" {
text += part.Text
}
}
if text == "" {
return "", fmt.Errorf("OpenCode returned no assistant text")
}
return text, nil
}
// Call adapts the pipeline's existing V2 command contract to the native stable API.
// Sessions still use the same account/database; only our loopback serve process is owned.
func (c *Connection) Call(ctx context.Context, method, path string, body any) ([]byte, error) {
if c.Backend != "v1" {
return nil, fmt.Errorf("native adapter requires the serve backend")
}
if path == "/api/session" && method == "post" {
data, _ := json.Marshal(body)
var request struct {
Title, Agent string
Model map[string]string
Permissions []map[string]string
}
if err := json.Unmarshal(data, &request); err != nil {
return nil, err
}
rules := request.Permissions
if rules == nil {
rules = []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}}
}
nativeRules:=Permissions(rules)
// V1 tool implementations can match absolute file paths; preserve the exact
// approved scope in both forms, never grant a workspace-wide edit wildcard.
var scoped []map[string]string
for _,rule:=range nativeRules{scoped=append(scoped,rule);if rule["permission"]=="edit"&&rule["action"]=="allow"&&!filepath.IsAbs(rule["pattern"]){scoped=append(scoped,map[string]string{"permission":"edit","action":"allow","pattern":filepath.Join(c.Directory,rule["pattern"])})}}
payload := map[string]any{"title": request.Title, "permission": scoped, "model": request.Model}
if request.Agent != "" {
payload["agent"] = request.Agent
}
created, err := c.Native(ctx, "post", "/session", payload)
if err != nil {
return nil, err
}
var session struct {
ID string `json:"id"`
}
if json.Unmarshal(created, &session) != nil || !sessionID.MatchString(session.ID) {
return nil, fmt.Errorf("OpenCode returned an invalid native session")
}
c.models[session.ID] = map[string]string{"providerID": request.Model["providerID"], "modelID": request.Model["id"]}
return json.Marshal(map[string]any{"data": map[string]string{"id": session.ID}})
}
clean := strings.SplitN(path, "?", 2)[0]
parts := strings.Split(strings.TrimPrefix(clean, "/api/session/"), "/")
if strings.HasPrefix(clean, "/api/experimental/session/") {
parts = strings.Split(strings.TrimPrefix(clean, "/api/experimental/session/"), "/")
}
if len(parts) == 0 || !sessionID.MatchString(parts[0]) {
return nil, fmt.Errorf("unsupported native OpenCode operation")
}
id := parts[0]
if len(parts) == 1 && method == "delete" {
data, err := c.Native(ctx, "delete", "/session/"+id, nil)
if err != nil {
return nil, err
}
var confirmed bool
if json.Unmarshal(data,&confirmed)!=nil||!confirmed {
return nil, fmt.Errorf("OpenCode session deletion was not confirmed")
}
delete(c.models, id)
delete(c.responses, id)
return nil, nil
}
if len(parts) != 2 {
return nil, fmt.Errorf("unsupported native OpenCode operation")
}
switch parts[1] {
case "interrupt":
data, err := c.Native(ctx, "post", "/session/"+id+"/abort", nil)
if err != nil {
return nil, err
}
var confirmed bool
if json.Unmarshal(data,&confirmed)!=nil||!confirmed {
return nil, fmt.Errorf("OpenCode abort was not confirmed")
}
return nil, nil
case "generate", "prompt":
data, _ := json.Marshal(body)
var request map[string]string
if err := json.Unmarshal(data, &request); err != nil {
return nil, err
}
text := request["prompt"]
if text == "" {
text = request["text"]
}
response, err := c.Native(ctx, "post", "/session/"+id+"/message", map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}})
if err != nil {
return nil, err
}
c.responses[id] = response
if parts[1] == "prompt" {
return nil, nil
}
result, err := messageText(response)
if err != nil {
return nil, err
}
return json.Marshal(map[string]any{"data": map[string]string{"text": result}})
case "wait":
_, err := messageText(c.responses[id])
return nil, err
case "message":
data, err := c.Native(ctx, "get", "/session/"+id+"/message", nil)
if err != nil {
return nil, err
}
var messages []struct {
Info map[string]any `json:"info"`
Parts []map[string]any `json:"parts"`
}
if json.Unmarshal(data, &messages) != nil {
return nil, fmt.Errorf("invalid native messages")
}
var converted []map[string]any
for i := len(messages) - 1; i >= 0; i-- {
m := messages[i]
if m.Info["role"] != "assistant" {
continue
}
converted = append(converted, map[string]any{"finish": m.Info["finish"], "error": m.Info["error"], "content": m.Parts, "native_info": m.Info})
}
return json.Marshal(map[string]any{"data": converted})
}
return nil, fmt.Errorf("unsupported native OpenCode operation")
}
+101
View File
@@ -0,0 +1,101 @@
package opencode
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestNativeSessionModelPermissionsGenerationAndAbort(t *testing.T) {
var created, prompted, deleted, aborted bool
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.URL.Query().Get("directory") != "/repo" {
t.Error("workspace lost")
}
switch r.Method + " " + r.URL.Path {
case "POST /session":
var body struct {
Model map[string]string
Permission []map[string]string
}
json.NewDecoder(r.Body).Decode(&body)
if body.Model["id"] != "model" || body.Model["providerID"] != "provider" || body.Permission[0]["action"] != "deny" || body.Permission[1]["permission"] != "bash" {
t.Error("model or scoped permissions lost", body)
}
created = true
w.Write([]byte(`{"id":"ses_native"}`))
case "POST /session/ses_native/message":
var body struct {
Model map[string]string
Parts []map[string]string
}
json.NewDecoder(r.Body).Decode(&body)
if body.Model["modelID"] != "model" || body.Parts[0]["text"] != "exact prompt" {
t.Error("prompt or message model changed", body)
}
prompted = true
w.Write([]byte(`{"info":{"role":"assistant","finish":"stop","time":{"completed":1}},"parts":[{"type":"text","text":"result"}]}`))
case "GET /session/ses_native/message":
w.Write([]byte(`[{"info":{"role":"assistant","finish":"stop","time":{"completed":1}},"parts":[{"type":"text","text":"result"}]}]`))
case "POST /session/ses_native/abort":
aborted = true
w.Write([]byte("true"))
case "DELETE /session/ses_native":
deleted = true
w.Write([]byte("true"))
default:
t.Error("unexpected native operation", r.Method, r.URL.Path)
}
}))
defer server.Close()
c := &Connection{Backend: "v1", URL: server.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}}
ctx := context.Background()
_, err := c.Call(ctx, "post", "/api/session", map[string]any{"model": map[string]string{"providerID": "provider", "id": "model"}, "permissions": []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}, {"action": "shell", "resource": "go test ./...", "effect": "allow"}}})
if err != nil {
t.Fatal(err)
}
data, err := c.Call(ctx, "post", "/api/session/ses_native/generate", map[string]string{"prompt": "exact prompt"})
if err != nil || !strings.Contains(string(data), "result") {
t.Fatal(string(data), err)
}
if _, err = c.Call(ctx, "post", "/api/experimental/session/ses_native/wait", nil); err != nil {
t.Fatal(err)
}
if _, err = c.Call(ctx, "get", "/api/session/ses_native/message?order=desc", nil); err != nil {
t.Fatal(err)
}
if _, err = c.Call(ctx, "post", "/api/session/ses_native/interrupt", nil); err != nil {
t.Fatal(err)
}
if _, err = c.Call(ctx, "delete", "/api/session/ses_native", nil); err != nil {
t.Fatal(err)
}
if !created || !prompted || !deleted || !aborted {
t.Fatal("native lifecycle incomplete")
}
}
func TestHTMLBridgeIsNotAnAPIAndErrorsAreRedacted(t *testing.T) {
for _, status := range []int{200, 500} {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(status)
w.Write([]byte("secret-token HTML"))
}))
c := &Connection{URL: server.URL}
_, err := c.Native(context.Background(), "get", "/api/session", nil)
server.Close()
if err == nil || strings.Contains(err.Error(), "secret-token") {
t.Fatal(err)
}
}
for _, body := range []string{`{"info":{"role":"assistant","finish":"tool-calls","time":{"completed":1}},"parts":[]}`, `{"info":{"role":"assistant","finish":"stop"},"parts":[{"type":"text","text":"unfinished"}]}`, `{"info":{"role":"assistant","finish":"stop","time":{"completed":1},"error":{"message":"bad"}},"parts":[{"type":"text","text":"failed"}]}`} {
if _, err := messageText([]byte(body)); err == nil {
t.Fatal("unfinished/error response accepted")
}
}
}