Files
pipelines/internal/opencode/chat_owned_test.go
T
alan aa8eddd686 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.
2026-11-04 16:17:08 +00:00

65 lines
2.1 KiB
Go

package opencode
import (
"context"
"errors"
"strings"
"syscall"
"testing"
)
// The server-spawned chat service is detached and registered: a second server
// process reuses the same running service (sessions survive the restart), and
// it is separate from the stage service.
func TestOwnedChatServiceIsReusedAcrossServerProcesses(t *testing.T) {
executable := fakeOwnedStageEnv(t)
t.Setenv(ChatConfigEnvKey, "")
t.Cleanup(func() {
path, _ := ChatServiceRegistrationPath()
if state, err := readOwnedStageState(path); err == nil {
_ = syscall.Kill(state.PID, syscall.SIGKILL)
}
})
first, err := EnsureOwnedChatService(context.Background(), executable, t.TempDir())
if err != nil {
t.Fatal(err)
}
path, _ := ChatServiceRegistrationPath()
stagePath, _ := StageServiceRegistrationPath()
if first.credentialFile != path || path == stagePath || !strings.HasPrefix(first.URL, "http://127.0.0.1:") {
t.Fatalf("chat connection = %+v", first)
}
pid := ownedPID(t, first)
first.Close() // the previous server process exits
second, err := EnsureOwnedChatService(context.Background(), executable, t.TempDir())
if err != nil || second.URL != first.URL || ownedPID(t, second) != pid {
t.Fatalf("chat service was not reused by the next server: %v", err)
}
}
// ReferenceAlive tells a live retained service from one whose process is gone,
// without reviving it.
func TestReferenceAliveDoesNotRevive(t *testing.T) {
executable := fakeOwnedStageEnv(t)
c, err := OpenStage(context.Background(), executable, t.TempDir(), "v2")
if err != nil {
t.Fatal(err)
}
reference, err := c.Reference()
if err != nil {
t.Fatal(err)
}
if err := ReferenceAlive(context.Background(), reference); err != nil {
t.Fatalf("live service reported gone: %v", err)
}
pid := ownedPID(t, c)
_ = syscall.Kill(pid, syscall.SIGKILL)
waitGone(t, pid)
if err := ReferenceAlive(context.Background(), reference); !errors.Is(err, ErrServiceNotRunning) {
t.Fatalf("gone service not reported: %v", err)
}
if _, healthy := c.ownedInfo(context.Background()); healthy {
t.Fatal("ReferenceAlive revived the service")
}
}