fix: pin native exports to stage execution service
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user