Keep OpenCode/Codex chat and stage sessions running across server restarts

A stopping server no longer interrupts harness sessions. Runs are canceled
with a shutdown cause, so stage clients leave admitted sessions running and
owned; start-up recovery re-adopts live OpenCode V2 stage sessions (wait,
read back the result, continue the graph), restarts unadmitted visits,
continues runs between stages, and falls back to interrupted with a reason
when the harness is gone. Chats with live work are recorded on shutdown and
reattached on start; turns that ended during the downtime are folded in
from history. The server-spawned V2 chat serve is now a detached owned
service. PIPELINES_SHUTDOWN_SESSIONS=stop restores the old behaviour.
This commit is contained in:
2026-11-04 16:17:08 +00:00
parent 9351cd7d74
commit aa8eddd686
26 changed files with 2318 additions and 99 deletions
+50 -7
View File
@@ -8,6 +8,7 @@ import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@@ -90,23 +91,65 @@ func (c *Connection) Reference() (string, error) {
return f.Name(), nil
}
func FromReference(path string) (*Connection, error) {
type connectionReference struct{ Backend, URL, Password, Directory, CredentialFile string }
func readReference(path string) (connectionReference, error) {
var reference connectionReference
info, err := os.Lstat(path)
if err != nil {
return nil, err
return reference, err
}
if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() > MaxRequest {
return nil, fmt.Errorf("invalid private OpenCode connection reference")
return reference, 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")
return reference, fmt.Errorf("OpenCode reference belongs to another user")
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
return reference, err
}
var reference struct{ Backend, URL, Password, Directory, CredentialFile string }
if err = json.Unmarshal(data, &reference); err != nil {
err = json.Unmarshal(data, &reference)
return reference, err
}
// ErrServiceNotRunning reports that a retained OpenCode service no longer
// answers: its process is gone, and with it any session turn it was running.
var ErrServiceNotRunning = errors.New("the OpenCode service is not running")
// ReferenceAlive reports whether the execution service a retained reference
// names is running right now, without starting or reviving it. Restart recovery
// uses it to tell a session that kept running across a Pipelines restart from
// one whose harness process is gone.
func ReferenceAlive(ctx context.Context, path string) error {
reference, err := readReference(path)
if err != nil {
return fmt.Errorf("retained OpenCode reference unreadable: %w", err)
}
if reference.Backend != "v2" {
return fmt.Errorf("the V1 OpenCode stage service belonged to the stopped server process")
}
if !filepath.IsAbs(reference.CredentialFile) {
return fmt.Errorf("invalid V2 service credential reference")
}
c, err := serviceConnection(reference.CredentialFile, reference.Directory)
if err != nil {
return err
}
if c.URL != reference.URL {
return fmt.Errorf("retained OpenCode service endpoint changed")
}
probe, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if err := c.probeHealth(probe, "v2"); err != nil {
return fmt.Errorf("%w at %s", ErrServiceNotRunning, c.URL)
}
return nil
}
func FromReference(path string) (*Connection, error) {
reference, err := readReference(path)
if err != nil {
return nil, err
}
if reference.Backend == "v2" {