Run OpenCode stages on a Pipelines-owned stage service with their own limits

Stages now run on a detached Pipelines-owned opencode serve started with
OPENCODE_CONFIG_CONTENT carrying stage tool_output (16384 bytes / 400 lines)
and compaction.auto defaults, configurable through PIPELINES_OPENCODE_STAGE_*.
Chats keep their existing service and configuration; a server-spawned chat
serve can take PIPELINES_OPENCODE_CHAT_CONFIG. The service shares the user's
OpenCode config, credentials and session database, keeps a stable private
registration so retained references (recovery, interrupt, export) stay valid,
revives on the same port after a crash, replaces itself when idle after a
settings change, and is stopped on shutdown only when idle.
PIPELINES_OPENCODE_STAGE_SERVICE=shared restores the previous behaviour.
This commit is contained in:
2026-11-04 14:04:35 +00:00
parent fa6c0864db
commit b655278674
12 changed files with 977 additions and 15 deletions
+30 -6
View File
@@ -48,6 +48,9 @@ type Connection struct {
// tell an authentication refusal (worth re-pairing) from any other refusal.
// It is set only by the request path and cleared by the attach probe.
unauthorized bool
// configContent is the OPENCODE_CONFIG_CONTENT an owned serve process is
// started with; empty keeps the inherited environment unchanged.
configContent string
}
// Reference retains the execution service, never provider auth. V1 retains its
@@ -110,6 +113,13 @@ func FromReference(path string) (*Connection, error) {
if !filepath.IsAbs(reference.CredentialFile) {
return nil, fmt.Errorf("invalid V2 service credential reference")
}
// A retained reference to the owned stage service revives it on its
// recorded URL and password if it exited; it never selects another one.
if isOwnedStageRegistration(reference.CredentialFile) {
if err := reviveOwnedStageService(reference.Directory); err != nil {
return nil, err
}
}
c, err := serviceConnection(reference.CredentialFile, reference.Directory)
if err != nil {
return nil, err
@@ -244,7 +254,25 @@ func OpenChat(ctx context.Context, executable, directory string) (*Connection, e
if err != nil {
return nil, err
}
return OpenStream(ctx, executable, directory, backend)
c, err := newConnection(ctx, executable, directory, backend)
if err != nil {
return nil, err
}
// Chat-only limits apply to this spawned V2 process only; the operator's
// shared service and any attached endpoint keep their own configuration.
overrides, err := chatConfigOverrides()
if err != nil {
return nil, err
}
if overrides != nil && backend == "v2" {
if c.configContent, err = configContent(os.Getenv("OPENCODE_CONFIG_CONTENT"), overrides); err != nil {
return nil, err
}
}
if err := c.startServe(ctx); err != nil {
return nil, err
}
return c, nil
}
// newConnection resolves the executable/directory defaults, detects the backend
@@ -304,11 +332,7 @@ func (c *Connection) startServe(ctx context.Context) error {
c.password = hex.EncodeToString(secret)
c.process = exec.Command(c.Executable, "serve", "--hostname", "127.0.0.1", "--port", "0")
c.process.Dir = c.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 = serviceEnvironment(c.configContent)
c.process.Env = append(c.process.Env, "OPENCODE_SERVER_PASSWORD="+c.password, "OPENCODE_SERVER_USERNAME="+c.serverUser())
stdout, err := c.process.StdoutPipe()
if err != nil {