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
+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