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
+41 -7
View File
@@ -58,6 +58,12 @@ type Options struct {
// target-aware verification. Definitions can name only a registered ID; they
// cannot select an endpoint or supply provenance.
VerificationFixtures []VerificationFixture
// DetachSessions keeps harness sessions running across a server restart:
// a stopping server leaves stage and chat sessions running (DetachSessions
// on the handler records the chats) and the next start reattaches them.
// False keeps the previous behaviour (tests, embedded servers): a stopping
// server interrupts its runs and the next start marks them interrupted.
DetachSessions bool
}
// Handler serves the persistent connection with default options. Canceling ctx
@@ -77,7 +83,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
var runs *runner
var runErr error
if len(stores) > 0 {
runs, runErr = newRunnerAt(ctx, stores[0], options.ArtifactsRoot)
runs, runErr = newRunnerReattaching(ctx, stores[0], options.ArtifactsRoot, options.DetachSessions)
if runErr == nil {
runs.events = events
}
@@ -115,15 +121,11 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
settingsStore = stores[0]
}
hub.settings = settingsStore
if options.Chat != nil && registry != nil {
// Keep observing chats whose turn a previous process left running, so
// their reply or question still notifies with no client attached.
go hub.observeRunningTurns(ctx, options.Chat)
}
if len(stores) > 0 {
// Prompts queued behind a running turn are durable and owned by their
// session; resume delivering any a previous process left queued.
hub.queueStore = stores[0]
hub.liveStore = stores[0]
if options.Chat != nil {
go hub.resumeQueued(ctx, options.Chat)
}
@@ -449,12 +451,44 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
}
}
mux.HandleFunc("GET /connect", connect)
return &runHandler{Handler: mux, runner: runs}
if options.Chat != nil && registry != nil {
// Reattach the chats a previous process left live (and fold in turns
// that ended while it was down), then keep observing any other chat
// whose turn is still running, so a reply or question still notifies
// with no client attached.
go func() {
hub.reattachSessions(ctx, options.Chat)
hub.observeRunningTurns(ctx, options.Chat)
}()
}
if runs != nil {
// Continue the runs a previous process left in flight, now that the
// runner's collaborators are wired and before any request is served.
runs.reattachRuns()
}
return &runHandler{Handler: mux, runner: runs, hub: hub}
}
type runHandler struct {
http.Handler
runner *runner
hub *chatHub
}
// DetachSessions records the chats left live on their harness so the next
// start reattaches them. Call it when the server is stopping, before the
// handler's context is canceled (rooms are torn down after that) and before the
// store closes. It never interrupts a session.
func DetachSessions(handler http.Handler) {
h, ok := handler.(*runHandler)
if !ok || h.hub == nil {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := h.hub.detachSessions(ctx); err != nil {
log.Printf("record live chat sessions: %v", err)
}
}
// WaitRuns drains active execution before the database is closed on shutdown.