fix: pin native exports to stage execution service

This commit is contained in:
2026-10-20 21:46:42 +01:00
parent 1f867d4827
commit bb98bde4fc
20 changed files with 420 additions and 74 deletions
@@ -0,0 +1,23 @@
# Stage export uses the execution service
V2 stage admission reads the existing local CLI service registration and pins
every `opencode api` subprocess with `--server` and private inherited auth.
Missing registration fails closed; stage execution does not provision a service.
The chat broker's `PIPELINES_OPENCODE_ENDPOINT` and password-file overrides do
not select stage execution or export.
Both agent and tracked generation ownership callbacks retain a private reference
containing the endpoint identity, execution directory, and credential-file path,
not a password. Export and restart interruption resolve that exact file and
refuse an endpoint identity change. Export also checks the recorded directory.
Credentials remain in OpenCode's owner-only service registration, not the run
database or protocol evidence. No export uses the size-truncating V2 CLI fallback.
Historical owners without a reference can read only the existing local service
registration, never the chat override. Their original service identity cannot be
reconstructed if that registration has since changed; an unavailable session
remains failed/pending evidence rather than being recreated or replayed.
Isolated HTTP A/B tests cover endpoint separation, authenticated export,
reference recovery, wrong credentials and changed endpoints. Fake-CLI tests
cover matching endpoint/auth/directory and ownership before prompt admission.
+14
View File
@@ -11,6 +11,7 @@ import (
)
func TestProviderCheckExecutesGenerationAndReportsFailures(t *testing.T) {
fakeNativeService(t)
// Isolate harness settings so an unrelated developer profile cannot route the
// native check through Docker.
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
@@ -43,6 +44,19 @@ esac
}
}
func fakeNativeService(t *testing.T) {
t.Helper()
state := t.TempDir()
t.Setenv("XDG_STATE_HOME", state)
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := os.Mkdir(filepath.Join(state, "opencode"), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(state, "opencode", "service.json"), []byte(`{"url":"http://127.0.0.1:1","password":"fixture-password"}`), 0600); err != nil {
t.Fatal(err)
}
}
func TestProviderCheckDeclineDoesNotExecute(t *testing.T) {
t.Setenv("PIPELINES_OPENCODE", "/does-not-exist")
var out bytes.Buffer
+1
View File
@@ -114,6 +114,7 @@ func TestProviderCheckDockerTextModeOmitsJSON(t *testing.T) {
}
func TestProviderCheckNativeProfileKeepsHostGenerator(t *testing.T) {
fakeNativeService(t)
root := t.TempDir()
marker := filepath.Join(root, "opencode-called")
opencode := filepath.Join(root, "opencode")
+60 -9
View File
@@ -28,6 +28,7 @@ type Connection struct {
Backend, Executable, Directory string
URL string
password string
credentialFile string // V2 auth stays in the existing private service file
// username overrides the backend-default basic-auth user for an
// operator-supplied endpoint (PIPELINES_OPENCODE_ENDPOINT_USERNAME). It is
// empty for the owned serve connection, which keeps the detected default.
@@ -49,8 +50,9 @@ type Connection struct {
unauthorized bool
}
// 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.
// Reference retains the execution service, never provider auth. V1 retains its
// ephemeral loopback password privately; V2 records only the existing credential
// file's path and endpoint identity, so rotation can never redirect recovery.
func (c *Connection) Reference() (string, error) {
cache, err := os.UserCacheDir()
if err != nil {
@@ -64,7 +66,16 @@ func (c *Connection) Reference() (string, error) {
if err != nil {
return "", err
}
data, _ := json.Marshal(map[string]string{"url": c.URL, "password": c.password, "directory": c.Directory})
reference := map[string]string{"url": c.URL, "password": c.password, "directory": c.Directory}
if c.Backend == "v2" {
if c.credentialFile == "" || c.URL == "" {
_ = f.Close()
_ = os.Remove(f.Name())
return "", fmt.Errorf("V2 stage requires an existing service credential reference")
}
reference = map[string]string{"backend": "v2", "url": c.URL, "directory": c.Directory, "credentialFile": c.credentialFile}
}
data, _ := json.Marshal(reference)
_, writeErr := f.Write(data)
closeErr := f.Close()
if writeErr != nil {
@@ -91,10 +102,23 @@ func FromReference(path string) (*Connection, error) {
if err != nil {
return nil, err
}
var reference struct{ URL, Password, Directory string }
var reference struct{ Backend, URL, Password, Directory, CredentialFile string }
if err = json.Unmarshal(data, &reference); err != nil {
return nil, err
}
if reference.Backend == "v2" {
if !filepath.IsAbs(reference.CredentialFile) {
return nil, fmt.Errorf("invalid V2 service credential reference")
}
c, err := serviceConnection(reference.CredentialFile, reference.Directory)
if err != nil {
return nil, err
}
if c.URL != reference.URL {
return nil, fmt.Errorf("retained OpenCode service endpoint changed")
}
return c, nil
}
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")
@@ -152,11 +176,15 @@ func Open(ctx context.Context, executable, directory, backend string) (*Connecti
if err != nil {
return nil, err
}
// The V2 CLI talks to its own managed service, so stages reach it through the
// `api` subcommand and Open returns no owned process for them. Only
// OpenStream (brokered chat) opens the owned V2 endpoint.
// V2 stages use the existing CLI-managed service, pinned before admission.
// Missing registration fails closed: never discover/start another service.
if c.Backend == "v2" {
return c, nil
managed, err := sessionExportConnection(c.Directory)
if err != nil {
return nil, err
}
managed.Executable = c.Executable
return managed, nil
}
if err := c.startServe(ctx); err != nil {
return nil, err
@@ -164,12 +192,35 @@ func Open(ctx context.Context, executable, directory, backend string) (*Connecti
return c, nil
}
// PinCLI binds an api subprocess to this exact existing service, bypassing CLI
// discovery (which could otherwise start or select a different service). Auth is
// inherited privately, never passed in argv or persisted in ownership records.
func (c *Connection) PinCLI(cmd *exec.Cmd) {
if c.Backend != "v2" || c.URL == "" {
return
}
cmd.Args = append(cmd.Args, "--server", c.URL)
if cmd.Env == nil {
cmd.Env = os.Environ()
}
for _, key := range []string{"OPENCODE_PASSWORD", "OPENCODE_SERVER_PASSWORD"} {
filtered := cmd.Env[:0]
for _, entry := range cmd.Env {
if !strings.HasPrefix(entry, key+"=") {
filtered = append(filtered, entry)
}
}
cmd.Env = filtered
}
cmd.Env = append(cmd.Env, "OPENCODE_PASSWORD="+c.password)
}
// OpenStream opens the single owned `opencode serve` endpoint for either backend
// so a caller that needs a URL-bearing, SSE-capable connection (brokered chat)
// gets one on the installed V2 CLI too. The V2 server exposes the authenticated
// SSE event firehose at /api/event and the /api HTTP surface; the native serve
// exposes /event and the translated /api surface. Stage generation keeps using
// Open (the unstarted V2 connection plus its `api` subcommand), so this starts
// Open (the pinned V2 connection plus its `api` subcommand), so this starts
// no process for it and there is never a second transport.
func OpenStream(ctx context.Context, executable, directory, backend string) (*Connection, error) {
c, err := newConnection(ctx, executable, directory, backend)
+110
View File
@@ -0,0 +1,110 @@
package opencode
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestExecutionServiceReferencePinsIdentityAndAuth(t *testing.T) {
c, calls := exportAPI(t, `{"data":{"info":{"id":"ses_ok"},"messages":[{"type":"user","text":"execution A"}]}}`, 200)
chatCalls := 0
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
chatCalls++
w.WriteHeader(http.StatusNotFound)
}))
defer chat.Close()
state := t.TempDir()
t.Setenv(OpenCodeServiceStateEnvKey, state)
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv(EndpointEnvKey, chat.URL)
t.Setenv(EndpointUsernameEnvKey, "wrong-chat-user")
chatPassword := filepath.Join(t.TempDir(), "chat-password")
if err := os.WriteFile(chatPassword, []byte("chat-password"), 0600); err != nil {
t.Fatal(err)
}
t.Setenv(EndpointPasswordFileEnvKey, chatPassword)
if err := os.Mkdir(filepath.Join(state, "opencode"), 0700); err != nil {
t.Fatal(err)
}
path := filepath.Join(state, "opencode", "service.json")
writeService := func(endpoint, password string) {
t.Helper()
data, _ := json.Marshal(map[string]string{"url": endpoint, "password": password})
if err := os.WriteFile(path, data, 0600); err != nil {
t.Fatal(err)
}
}
writeService(c.URL, c.password)
owned, err := Open(context.Background(), "/must-not-run-cli", "/workspace", "v2")
if err != nil {
t.Fatal(err)
}
ref, err := owned.Reference()
if err != nil {
t.Fatal(err)
}
data, _ := os.ReadFile(ref)
if strings.Contains(string(data), c.password) || strings.Contains(string(data), `"password"`) {
t.Fatal("credential copied into reference")
}
// Restart recovery remains on A, even when the chat endpoint and the state
// discovery directory point elsewhere. Only the pinned private file is read.
t.Setenv(OpenCodeServiceStateEnvKey, t.TempDir())
recovered, err := FromReference(ref)
if err != nil {
t.Fatal(err)
}
s, err := ExportSession(context.Background(), "/must-not-run-cli", "/workspace", "v2", "ses_ok", recovered)
if err != nil || len(s.Messages) != 1 || *calls != 1 || chatCalls != 0 {
t.Fatalf("export did not stay on execution A: %v", err)
}
t.Setenv("OPENCODE_PASSWORD", "foreign-password")
t.Setenv("OPENCODE_SERVER_PASSWORD", "foreign-server-password")
cmd := exec.Command("/must-not-run-cli", "api", "get", "/api/info")
recovered.PinCLI(cmd)
if strings.Join(cmd.Args[len(cmd.Args)-2:], " ") != "--server "+c.URL {
t.Fatal("CLI discovery was not bypassed")
}
passwords := 0
for _, entry := range cmd.Env {
if strings.HasPrefix(entry, "OPENCODE_SERVER_PASSWORD=") {
t.Fatal("foreign server auth survived")
}
if strings.HasPrefix(entry, "OPENCODE_PASSWORD=") {
passwords++
if entry != "OPENCODE_PASSWORD="+c.password {
t.Fatal("CLI authentication does not match export")
}
}
}
if passwords != 1 {
t.Fatal("CLI credential missing or duplicated")
}
writeService(c.URL, "wrong-password")
wrong, err := FromReference(ref)
if err != nil {
t.Fatal(err)
}
if _, err := ExportSession(context.Background(), "/must-not-run-cli", "/workspace", "v2", "ses_ok", wrong); err == nil {
t.Fatal("wrong execution credential accepted")
}
writeService(chat.URL, "chat-password")
if _, err := FromReference(ref); err == nil || chatCalls != 0 {
t.Fatal("changed service identity followed instead of failing closed")
}
}
func TestExecutionServiceMissingDoesNotUseChatOrStartCLI(t *testing.T) {
t.Setenv(OpenCodeServiceStateEnvKey, t.TempDir())
t.Setenv(EndpointEnvKey, "http://127.0.0.1:1")
if _, err := Open(context.Background(), "/must-not-run-cli", "/workspace", "v2"); err == nil {
t.Fatal("missing execution service accepted")
}
}
+3 -4
View File
@@ -19,7 +19,6 @@ func exportAPI(t *testing.T, body string, status int) (*Connection, *int) {
*calls++
user, pass, ok := r.BasicAuth()
if !ok || user != "opencode" || pass != "test-export-password" {
t.Error("missing endpoint authentication")
w.WriteHeader(401)
return
}
@@ -119,14 +118,14 @@ func TestV2ExportExistingServiceDiscoveryAndEndpointIsolation(t *testing.T) {
t.Fatalf("existing service read: %v", err)
}
t.Setenv(EndpointEnvKey, "http://127.0.0.1:1")
if _, err := ExportSession(context.Background(), "/never-start-cli", "/workspace", "v2", "ses_ok"); err == nil || *calls != 1 {
t.Fatal("credential crossed endpoint boundary")
if _, err := ExportSession(context.Background(), "/never-start-cli", "/workspace", "v2", "ses_ok"); err != nil || *calls != 2 {
t.Fatal("chat endpoint redirected execution export")
}
t.Setenv(EndpointEnvKey, "")
if err := os.Chmod(path, 0644); err != nil {
t.Fatal(err)
}
if _, err := ExportSession(context.Background(), "/never-start-cli", "/workspace", "v2", "ses_ok"); err == nil || *calls != 1 {
if _, err := ExportSession(context.Background(), "/never-start-cli", "/workspace", "v2", "ses_ok"); err == nil || *calls != 2 {
t.Fatal("public service credential accepted")
}
}
+26 -41
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)
@@ -11,52 +12,36 @@ import (
// never discovers by starting a CLI, serves a new backend, or pairs credentials.
// Callers already holding the exact connection should pass it to ExportSession.
func sessionExportConnection(directory string) (*Connection, error) {
endpoint := strings.TrimSpace(os.Getenv(EndpointEnvKey))
password, err := ReadEndpointCredentialFile(os.Getenv(EndpointPasswordFileEnvKey))
path, err := ServiceCredentialPath()
if err != nil {
return nil, err
}
if endpoint == "" || password == "" {
path, err := ServiceCredentialPath()
if err != nil {
return nil, err
}
credential, err := ReadServiceCredential(path)
if err != nil {
return nil, err
}
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot read the local OpenCode endpoint")
}
var state struct {
URL string `json:"url"`
}
if json.Unmarshal(data, &state) != nil {
return nil, fmt.Errorf("invalid local OpenCode endpoint")
}
base, err := NormalizeEndpointURL(state.URL)
if err != nil {
return nil, fmt.Errorf("invalid local OpenCode endpoint")
}
if endpoint == "" {
endpoint = base
} else {
configured, err := NormalizeEndpointURL(endpoint)
if err != nil || configured != base {
return nil, fmt.Errorf("session export endpoint requires its own credential")
}
}
if password == "" {
password = credential
}
}
base, err := NormalizeEndpointURL(endpoint)
return serviceConnection(path, directory)
}
// serviceConnection reads only the existing CLI-managed service. Chat endpoint
// overrides belong to a different transport and must never redirect stage work.
func serviceConnection(path, directory string) (*Connection, error) {
path, err := filepath.Abs(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("invalid local OpenCode credential path")
}
return &Connection{Backend: "v2", URL: base, Directory: directory, password: password,
username: strings.TrimSpace(os.Getenv(EndpointUsernameEnvKey))}, nil
if err := checkPrivateFile(path, serviceCredentialLimit); err != nil {
return nil, fmt.Errorf("cannot read the local OpenCode service credential")
}
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot read the local OpenCode endpoint")
}
var state struct{ URL, Password string }
if json.Unmarshal(data, &state) != nil || strings.TrimSpace(state.Password) == "" {
return nil, fmt.Errorf("invalid local OpenCode endpoint")
}
base, err := NormalizeEndpointURL(state.URL)
if err != nil {
return nil, fmt.Errorf("invalid local OpenCode endpoint")
}
return &Connection{Backend: "v2", URL: base, Directory: directory, password: strings.TrimSpace(state.Password), credentialFile: path}, nil
}
// UnmarshalJSON retains the complete stored turn, including unknown metadata
+3
View File
@@ -81,6 +81,7 @@ esac
t.Fatal(err)
}
t.Setenv("PIPELINES_OPENCODE", executable)
localExportService(t, "http://127.0.0.1:1")
store, err := pipeline.Open(filepath.Join(dir, "runs.db"))
if err != nil {
t.Fatal(err)
@@ -446,6 +447,7 @@ esac
t.Fatal(err)
}
t.Setenv("PIPELINES_OPENCODE", executable)
localExportService(t, "http://127.0.0.1:1")
store, err := pipeline.Open(filepath.Join(dir, "runs.db"))
if err != nil {
t.Fatal(err)
@@ -533,6 +535,7 @@ func TestReadOnlyStageRetriesPostAdmissionTransportButMutatingStageDoesNot(t *te
dir := t.TempDir()
executable := filepath.Join(dir, "opencode")
t.Setenv("PIPELINES_OPENCODE", executable)
localExportService(t, "http://127.0.0.1:1")
scriptTemplate := `#!/bin/sh
if [ "$1" = "--help" ]; then echo ' api'; exit 0; fi
printf '%s %s\n' "$2" "$3" >> "@STATE@/calls"
+1 -8
View File
@@ -13,7 +13,6 @@ import (
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/stage"
)
@@ -78,7 +77,6 @@ func TestAutomaticSessionExportRecoveryTransientThenComplete(t *testing.T) {
func TestAutomaticSessionExportRecoveryWrongSessionExhaustsNeverClean(t *testing.T) {
h, s, id := recoveryRunner(t)
ctx := context.Background()
dir := t.TempDir()
var calls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, password, ok := r.BasicAuth()
@@ -95,12 +93,7 @@ func TestAutomaticSessionExportRecoveryWrongSessionExhaustsNeverClean(t *testing
w.Write([]byte(`{"info":{"id":"ses_wrong"},"messages":[{"info":{"role":"assistant"},"parts":[{"type":"text","text":"wrong"}]}]}`))
}))
t.Cleanup(srv.Close)
passwordFile := filepath.Join(dir, "password")
if err := os.WriteFile(passwordFile, []byte("fixture-password"), 0600); err != nil {
t.Fatal(err)
}
t.Setenv(opencode.EndpointEnvKey, srv.URL)
t.Setenv(opencode.EndpointPasswordFileEnvKey, passwordFile)
localExportService(t, srv.URL)
h.runner.exportSession = stage.ExportSession
for range 3 {
job, _ := s.SessionExportJob(ctx, id, 1)
+17 -5
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
@@ -54,13 +55,24 @@ func sessionExportCLI(t *testing.T) {
fmt.Fprint(w, `{"data":`+sessionExportBody+`}`)
}))
t.Cleanup(srv.Close)
passwordPath := filepath.Join(t.TempDir(), "password")
if err := os.WriteFile(passwordPath, []byte("fixture-password"), 0600); err != nil {
localExportService(t, srv.URL)
t.Setenv(opencode.EndpointEnvKey, "http://127.0.0.1:1")
t.Setenv(opencode.EndpointPasswordFileEnvKey, "")
t.Setenv(opencode.EndpointUsernameEnvKey, "")
}
func localExportService(t *testing.T, endpoint string) {
t.Helper()
t.Setenv("XDG_CACHE_HOME", t.TempDir())
state := t.TempDir()
t.Setenv(opencode.OpenCodeServiceStateEnvKey, state)
if err := os.Mkdir(filepath.Join(state, "opencode"), 0700); err != nil {
t.Fatal(err)
}
data, _ := json.Marshal(map[string]string{"url": endpoint, "password": "fixture-password"})
if err := os.WriteFile(filepath.Join(state, "opencode", "service.json"), data, 0600); err != nil {
t.Fatal(err)
}
t.Setenv(opencode.EndpointEnvKey, srv.URL)
t.Setenv(opencode.EndpointPasswordFileEnvKey, passwordPath)
t.Setenv(opencode.EndpointUsernameEnvKey, "")
}
// sessionExportRunner runs one stage that records a session for the run, so the
+1
View File
@@ -56,6 +56,7 @@ esac
t.Fatal(err)
}
t.Setenv("PIPELINES_OPENCODE", executable)
localExportService(t, "http://127.0.0.1:1")
store, err := pipeline.Open(filepath.Join(dir, "runs.db"))
if err != nil {
t.Fatal(err)
+1
View File
@@ -49,6 +49,7 @@ esac
t.Fatal(err)
}
t.Setenv("PIPELINES_OPENCODE", executable)
localExportService(t, "http://127.0.0.1:1")
store, err := pipeline.Open(filepath.Join(dir, "runs.db"))
if err != nil {
t.Fatal(err)
+1
View File
@@ -86,6 +86,7 @@ esac
t.Fatal(err)
}
t.Setenv("PIPELINES_OPENCODE", exe)
localExportService(t, "http://127.0.0.1:1")
}
func budgetPipeline(t *testing.T, ctx context.Context, store *pipeline.Store, names []string, configs string) {
+10 -1
View File
@@ -42,7 +42,7 @@ func (c OpenCodeClient) interrupt(ctx context.Context, id string, confirm bool)
if c.command == nil && c.connection == nil {
var connection *opencode.Connection
var err error
if c.Backend == "v1" && c.Reference != "" {
if c.Reference != "" {
connection, err = opencode.FromReference(c.Reference)
} else {
connection, err = opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend)
@@ -50,6 +50,9 @@ func (c OpenCodeClient) interrupt(ctx context.Context, id string, confirm bool)
if err != nil {
return err
}
if c.Reference != "" && (connection.Backend != c.Backend || connection.Directory != c.Directory) {
return fmt.Errorf("retained OpenCode execution context mismatch")
}
defer connection.Close()
c.connection = connection
c.Backend = connection.Backend
@@ -86,6 +89,12 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
defer connection.Close()
c.connection = connection
c.Backend = connection.Backend
if c.Backend == "v2" {
c.Reference, err = connection.Reference()
if err != nil {
return "", err
}
}
}
rules := c.permissionRules()
data, err := c.call(ctx, "post", "/api/session", map[string]any{"title": "Pipeline audited agent stage", "agent": "build", "location": map[string]string{"directory": c.Directory}, "model": map[string]string{"providerID": config.Provider, "id": config.Model}, "permissions": rules})
+1
View File
@@ -134,6 +134,7 @@ func TestSuccessfulFormatProbeIsRetainedWithoutBeingCalledAFailure(t *testing.T)
// read-only requests (never wait/generate/interrupt/delete or the recent-20
// all-message fetch).
func TestSessionEvidenceUsesBoundedLatestAssistantReadback(t *testing.T) {
fakeExecutionService(t)
var calls [][2]string
sessionBody := `{"data":{"id":"ses_evidence","cost":0.5,"note":"api_key=private-value"}}`
client := OpenCodeClient{Backend: "v2", Directory: t.TempDir(), command: func(_ context.Context, _ string, args ...string) ([]byte, error) {
+91
View File
@@ -0,0 +1,91 @@
package stage
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
"git.a-wan.net/alan/pipelines/internal/opencode"
)
// fakeExecutionService supplies discovery metadata only; subprocess fixtures
// implement the API and must never consult an operator's service registration.
func fakeExecutionService(t *testing.T) {
t.Helper()
state := t.TempDir()
t.Setenv(opencode.OpenCodeServiceStateEnvKey, state)
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := os.Mkdir(filepath.Join(state, "opencode"), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(state, "opencode", "service.json"), []byte(`{"url":"http://127.0.0.1:1","password":"fixture-password"}`), 0600); err != nil {
t.Fatal(err)
}
}
func TestV2StagePersistsExecutionReferenceBeforePrompt(t *testing.T) {
for _, agent := range []bool{false, true} {
t.Run(map[bool]string{false: "generation", true: "agent"}[agent], func(t *testing.T) {
state := t.TempDir()
t.Setenv(opencode.OpenCodeServiceStateEnvKey, state)
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv(opencode.EndpointEnvKey, "http://127.0.0.1:2")
t.Setenv("OPENCODE_PASSWORD", "foreign")
directory := t.TempDir()
if err := os.Mkdir(filepath.Join(state, "opencode"), 0700); err != nil {
t.Fatal(err)
}
data, _ := json.Marshal(map[string]string{"url": "http://127.0.0.1:1", "password": "execution-password"})
if err := os.WriteFile(filepath.Join(state, "opencode", "service.json"), data, 0600); err != nil {
t.Fatal(err)
}
cli := filepath.Join(t.TempDir(), "opencode")
// No HTTP or provider call: reject any discovery/unpinned invocation,
// wrong auth, wrong cwd, or prompt admitted before ownership succeeded.
script := `#!/bin/sh
[ "$(pwd -P)" = "$(cd "$EXPECTED_DIRECTORY" && pwd -P)" ] || exit 21
[ "$OPENCODE_PASSWORD" = execution-password ] || exit 22
case "$*" in *'--server http://127.0.0.1:1'*) ;; *) exit 23 ;; esac
case "$3" in
/api/session) echo '{"data":{"id":"ses_owned"}}' ;;
/api/session/ses_owned/interrupt) echo '{"data":{}}' ;;
/api/session/active) echo '{"data":{}}' ;;
*) exit 24 ;;
esac
`
if err := os.WriteFile(cli, []byte(script), 0700); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_OPENCODE", cli)
t.Setenv("EXPECTED_DIRECTORY", directory)
stop := errors.New("ownership test stops before prompt")
called := false
check := func(id, backend, reference string, active bool) error {
if !active {
return nil
}
called = true
c, err := opencode.FromReference(reference)
if err != nil || id != "ses_owned" || backend != "v2" || c.URL != "http://127.0.0.1:1" || c.Directory != directory {
t.Fatalf("ownership did not retain execution context: %v", err)
}
return stop
}
client := OpenCodeClient{Directory: directory, Backend: "v2", OnGenerationSession: check}
var err error
if agent {
c := AgentClient{OpenCodeClient: client}
c.OnSession = func(id string, active bool) error { return check(id, c.Backend, c.Reference, active) }
_, err = c.Generate(context.Background(), ModelConfig{}, "unused")
} else {
_, err = client.Generate(context.Background(), ModelConfig{}, "unused")
}
if !called || !errors.Is(err, stop) {
t.Fatalf("ownership callback not reached through pinned CLI: %v", err)
}
})
}
}
+9
View File
@@ -135,6 +135,9 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any)
if c.ArtifactsDir != "" {
cmd.Env = append(os.Environ(), ArtifactsEnv+"="+c.ArtifactsDir)
}
if c.connection != nil {
c.connection.PinCLI(cmd)
}
stdout := &boundedBuffer{limit: maxModelResponse}
stderr := &boundedBuffer{limit: 8 << 10}
cmd.Stdout = stdout
@@ -279,6 +282,12 @@ func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, promp
defer connection.Close()
c.connection = connection
c.Backend = connection.Backend
if c.Backend == "v2" {
c.Reference, err = connection.Reference()
if err != nil {
return "", err
}
}
}
directory := c.Directory
if directory == "" {
@@ -15,6 +15,7 @@ import (
// so the real os/exec path (not the command seam) is exercised.
func writeOpencodeFixture(t *testing.T, script string) (dir, marker string) {
t.Helper()
fakeExecutionService(t)
dir = t.TempDir()
executable := filepath.Join(dir, "opencode")
if err := os.WriteFile(executable, []byte(script), 0700); err != nil {
+11 -2
View File
@@ -51,7 +51,8 @@ type SessionExportRequest struct {
Stage string
Provider string
Model string
// Reference is the private retained native session directory (Pi only).
// Reference is the private native session directory (Pi), or the retained
// OpenCode execution-service context. It never contains credentials itself.
Reference string
}
@@ -102,7 +103,15 @@ func ExportSession(ctx context.Context, request SessionExportRequest) (SessionEx
if request.Backend == "pi" {
session, err = exportPiSession(ctx, request)
} else {
session, err = opencode.ExportSession(ctx, opencode.Executable(), request.Directory, request.Backend, request.SessionID)
var connections []*opencode.Connection
if request.Backend == "v2" && request.Reference != "" {
connection, refErr := opencode.FromReference(request.Reference)
if refErr != nil || connection.Backend != "v2" || connection.Directory != request.Directory {
return SessionExportResult{Source: sessionExportSource, Status: "failed"}, fmt.Errorf("session export: retained execution service is unavailable or mismatched")
}
connections = append(connections, connection)
}
session, err = opencode.ExportSession(ctx, opencode.Executable(), request.Directory, request.Backend, request.SessionID, connections...)
}
if err != nil {
if errors.Is(err, opencode.ErrEmptySessionExport) {
+36 -4
View File
@@ -2,6 +2,7 @@ package stage
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
@@ -42,12 +43,17 @@ func sessionCLI(t *testing.T, id, body string) {
fmt.Fprint(w, `{"data":`+body+`}`)
}))
t.Cleanup(srv.Close)
passwordPath := filepath.Join(t.TempDir(), "password")
if err := os.WriteFile(passwordPath, []byte("fixture-password"), 0600); err != nil {
state := t.TempDir()
t.Setenv(opencode.OpenCodeServiceStateEnvKey, state)
if err := os.Mkdir(filepath.Join(state, "opencode"), 0700); err != nil {
t.Fatal(err)
}
t.Setenv(opencode.EndpointEnvKey, srv.URL)
t.Setenv(opencode.EndpointPasswordFileEnvKey, passwordPath)
credential, _ := json.Marshal(map[string]string{"url": srv.URL, "password": "fixture-password"})
if err := os.WriteFile(filepath.Join(state, "opencode", "service.json"), credential, 0600); err != nil {
t.Fatal(err)
}
t.Setenv(opencode.EndpointEnvKey, "http://127.0.0.1:1")
t.Setenv(opencode.EndpointPasswordFileEnvKey, "")
t.Setenv(opencode.EndpointUsernameEnvKey, "")
}
@@ -61,6 +67,32 @@ func sessionBody() string {
`{"type":"file","uri":"file:///tmp/x"}]}]}`
}
func TestExportSessionUsesRetainedExecutionReference(t *testing.T) {
sessionCLI(t, "ses_ok", sessionBody())
dir := t.TempDir()
t.Setenv("XDG_CACHE_HOME", t.TempDir())
c, err := opencode.Open(context.Background(), "/must-not-run", dir, "v2")
if err != nil {
t.Fatal(err)
}
reference, err := c.Reference()
if err != nil {
t.Fatal(err)
}
request := sessionRequest(dir)
request.Reference = reference
// Discovery no longer finds A, but the persisted reference must still do so.
t.Setenv(opencode.OpenCodeServiceStateEnvKey, t.TempDir())
result, err := ExportSession(context.Background(), request)
if err != nil || result.Messages != 2 {
t.Fatalf("retained execution reference ignored: %+v %v", result, err)
}
request.Reference = filepath.Join(t.TempDir(), "missing-reference")
if _, err := ExportSession(context.Background(), request); err == nil {
t.Fatal("missing execution reference fell through to discovery")
}
}
func TestExportSessionRetainsNativeToolImagesAndUnknownMetadata(t *testing.T) {
dir := t.TempDir()
body := `{"info":{"id":"ses_ok"},"messages":[{"id":"m1","type":"user","content":[{"type":"text","text":"inspect screenshot"}]},{"id":"m2","type":"assistant","providerState":{"custom":"preserved"},"content":[{"type":"tool","name":"read","state":{"status":"completed","content":[{"type":"image","mimeType":"image/png","data":"iVBORfixturebytes"}]}}]}]}`