Merge main; use the shared chatShutdownDetacher for OpenCode and Codex

OpenCode and Codex chats implement Detach (called by chatManager.Close)
instead of a parallel DetachSessions interface. Docs follow the Claude Code
host and the systemd KillMode=process unit. The opt-in live restart check
copies only the stage provider's login row into its scratch OpenCode
database, builds the CLI for skill install and removes its scratch tree.
This commit is contained in:
2026-11-04 16:24:17 +00:00
26 changed files with 2463 additions and 101 deletions
+39 -11
View File
@@ -29,8 +29,8 @@ import (
// PIPELINES_RESTART_LIVE=1 go test ./internal/server -run TestRestartKeepsRealOpenCodeSessions -v -timeout 15m
//
// It builds pipelines-server, runs it on a scratch HOME/XDG tree, a scratch
// database and a free loopback port (with only the operator's OpenCode
// provider login copied in), starts a chat turn and a one-stage run that each
// database and a free loopback port (with only the operator's opencode-go
// provider login row copied into the scratch OpenCode database), starts a chat turn and a one-stage run that each
// run `sleep 30`, restarts the server mid-turn, and checks both finish
// instead of being aborted. Every scratch process is killed afterwards.
func TestRestartKeepsRealOpenCodeSessions(t *testing.T) {
@@ -46,6 +46,9 @@ func TestRestartKeepsRealOpenCodeSessions(t *testing.T) {
t.Fatal(err)
}
t.Logf("scratch root %s", root)
// Registered first, so it runs last: after the scratch processes are
// stopped, remove the tree (it holds a copied provider login row).
t.Cleanup(func() { _ = os.RemoveAll(root) })
home := filepath.Join(root, "home")
env := scratchEnvironment(t, realHome, home)
binary := filepath.Join(root, "pipelines-server")
@@ -56,8 +59,14 @@ func TestRestartKeepsRealOpenCodeSessions(t *testing.T) {
}
// Chat creation activates the manager skill, so install the current
// source's skills into the scratch harness roots.
skills := exec.Command("go", "run", "./cmd/pipelines", "skill", "install")
skills.Dir, skills.Env = filepath.Join("..", ".."), env
cli := filepath.Join(root, "pipelines")
buildCLI := exec.Command("go", "build", "-o", cli, "./cmd/pipelines")
buildCLI.Dir = filepath.Join("..", "..")
if out, err := buildCLI.CombinedOutput(); err != nil {
t.Fatalf("build CLI: %v\n%s", err, out)
}
skills := exec.Command(cli, "skill", "install")
skills.Env = env
if out, err := skills.CombinedOutput(); err != nil {
t.Fatalf("skill install: %v\n%s", err, out)
}
@@ -81,6 +90,10 @@ func TestRestartKeepsRealOpenCodeSessions(t *testing.T) {
server.stop()
killScratchServices(t, home)
})
// OpenCode 2.x keeps provider logins in its own database. The chat service
// has just created the scratch one; copy only the stage provider's login
// row into it before the stage service starts (scratch, owner-only).
copyProviderLogin(t, filepath.Join(realHome, ".local", "share", "opencode", "opencode.db"), filepath.Join(home, ".local", "share", "opencode", "opencode.db"), "opencode-go")
ctx, cancel := context.WithTimeout(context.Background(), 12*time.Minute)
defer cancel()
@@ -163,13 +176,6 @@ func scratchEnvironment(t *testing.T, realHome, home string) []string {
t.Fatal(err)
}
}
auth, err := os.ReadFile(filepath.Join(realHome, ".local", "share", "opencode", "auth.json"))
if err != nil {
t.Skipf("no OpenCode login to copy: %v", err)
}
if err := os.WriteFile(filepath.Join(data, "opencode", "auth.json"), auth, 0600); err != nil {
t.Fatal(err)
}
// A cold model catalog can leave the first service unable to resolve the
// model; seed the public catalog cache (not a credential).
if catalog, err := os.ReadFile(filepath.Join(realHome, ".cache", "opencode", "models.json")); err == nil {
@@ -197,6 +203,28 @@ func scratchEnvironment(t *testing.T, realHome, home string) []string {
"PIPELINES_OPENCODE="+opencodePath(), "PIPELINES_NTFY_URL=", "PIPELINES_MDNS=off")
}
func copyProviderLogin(t *testing.T, real, scratch, provider string) {
t.Helper()
if _, err := os.Stat(real); err != nil {
t.Skipf("no OpenCode login database: %v", err)
}
deadline := time.Now().Add(30 * time.Second)
for {
if _, err := os.Stat(scratch); err == nil {
break
}
if time.Now().After(deadline) {
t.Fatal("scratch OpenCode database was not created")
}
time.Sleep(200 * time.Millisecond)
}
script := "ATTACH '" + real + "' AS real; INSERT OR REPLACE INTO credential SELECT * FROM real.credential WHERE integration_id='" + provider + "'; SELECT changes();"
out, err := exec.Command("sqlite3", "-bail", scratch, script).CombinedOutput()
if err != nil || strings.TrimSpace(string(out)) == "0" {
t.Skipf("could not copy the %s login into the scratch OpenCode database: %v %s", provider, err, out)
}
}
func opencodePath() string {
if path, err := exec.LookPath("opencode"); err == nil {
return path