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
+8
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"log"
"strings"
"git.a-wan.net/alan/pipelines/internal/pipeline"
@@ -58,6 +59,13 @@ func handleChatClose(ctx context.Context, request protocol.Message, broker chatB
}
// A closed chat takes no more turns: prompts it still held are dropped.
hub.clearQueue(ctx, sessionID)
// A harness that keeps the agent alive across restarts (Claude Code's
// host) stops it now; the transcript stays and a later prompt resumes it.
if releaser, ok := broker.(chatSessionReleaser); ok {
if err := releaser.ReleaseSession(ctx, sessionID); err != nil {
log.Printf("chat session %s: release after close: %v", sessionID, err)
}
}
session, found, err := registry.OperatorSession(ctx, sessionID)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
+9 -12
View File
@@ -13,23 +13,20 @@ import (
// Chat sessions survive a Pipelines restart. Only an operator action (closing
// the chat, or Stop on its turn) stops one.
//
// Per-backend contract. A chat backend whose sessions can outlive the server
// process implements chatSessionDetacher. On shutdown the server calls
// DetachSessions instead of Close: the backend releases its own connection or
// process handles but must not interrupt, abort or kill any session, turn,
// Per-backend contract (chatShutdownDetacher and chatSessionReleaser in
// chat_manager.go). On shutdown chatManager.Close calls Detach on a backend
// that implements it instead of Close: the backend releases its own connection
// or process handles but must not interrupt, abort or kill any session, turn,
// question or background task. On the next start the server reattaches through
// the ordinary broker surface: chatTurnProber.ProbeTurn (is the turn still
// running?), Stream (re-subscribe), Permissions/Forms (pending questions and
// approvals), Shells (background tasks) and Messages (read back a turn that
// ended while the server was down). A backend without DetachSessions is closed
// as before, and its sessions end with the server.
// ended while the server was down). chat_close calls ReleaseSession on a
// backend that keeps an agent alive until released. A backend without Detach
// is closed as before, and its sessions end with the server.
//
// OpenCode (an attached or detached owned service) and Codex (its App Server)
// implement it; a Claude Code host that keeps its CLI processes alive can
// implement the same method.
type chatSessionDetacher interface {
DetachSessions()
}
// OpenCode (an attached or detached owned service), Codex (its App Server) and
// Claude Code (its detachable hosts) implement Detach.
// chatLiveStore records, on shutdown, the chats left live on their harness so
// the next start reattaches exactly those; *pipeline.Store implements it.
+29 -20
View File
@@ -166,14 +166,32 @@ func (m *chatManager) harness(backend string) chatBroker {
return client
}
// chatShutdownDetacher is implemented by a backend whose live sessions can
// outlive this server: on shutdown it lets them go instead of stopping them,
// and the next server reattaches (Claude Code's detachable hosts).
type chatShutdownDetacher interface {
Detach()
}
// chatSessionReleaser is implemented by a backend that keeps a session's agent
// running until it is explicitly released; chat_close releases it.
type chatSessionReleaser interface {
ReleaseSession(ctx context.Context, sessionID string) error
}
func closeHarness(client chatBroker) {
if detacher, ok := client.(chatShutdownDetacher); ok {
detacher.Detach()
return
}
if closer, ok := client.(interface{ Close() }); ok {
closer.Close()
}
}
// Close releases the owned Codex and Claude Code clients, if any, and stops
// later lazy attachment.
// Close lets go of the Codex and Claude Code clients, if any, and stops later
// lazy attachment. It runs at server shutdown: a backend whose sessions can
// survive it (chatShutdownDetacher) is detached, not stopped.
func (m *chatManager) Close() {
m.mu.Lock()
m.closed = true
@@ -183,26 +201,17 @@ func (m *chatManager) Close() {
closeHarness(claudeClient)
}
// DetachSessions is the shutdown counterpart of Close for a server that keeps
// chat sessions running across a restart (see chat_detach.go). Each harness
// that implements chatSessionDetacher releases its handles without stopping a
// session; one that does not is closed as before. The OpenCode connection's
// lifetime belongs to the caller (cmd/pipelines-server), so it is only detached.
func (m *chatManager) DetachSessions() {
m.mu.Lock()
m.closed = true
codexClient, claudeClient := m.codex, m.claude
m.mu.Unlock()
if detacher, ok := m.opencode.(chatSessionDetacher); ok {
detacher.DetachSessions()
// ReleaseSession releases a session's agent on its own harness after an
// explicit close. A harness without that notion is left as is.
func (m *chatManager) ReleaseSession(ctx context.Context, sessionID string) error {
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
return nil
}
for _, client := range []chatBroker{codexClient, claudeClient} {
if detacher, ok := client.(chatSessionDetacher); ok {
detacher.DetachSessions()
continue
}
closeHarness(client)
if releaser, ok := adapter.(chatSessionReleaser); ok {
return releaser.ReleaseSession(ctx, sessionID)
}
return nil
}
// Backends lists the harnesses available for a new session.
+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