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:
@@ -176,6 +176,14 @@ func (c *Chat) Close() {
|
||||
_ = c.conn.Close(websocket.StatusNormalClosure, "")
|
||||
}
|
||||
|
||||
// DetachSessions is the server-restart shutdown: threads and their turns live
|
||||
// in the Codex App Server, not in this client, so closing the control
|
||||
// connection leaves them running; no turn is interrupted. The next server
|
||||
// process reconnects and reconciles each thread through thread/read. An
|
||||
// approval or question the App Server sent to this connection is bound to it,
|
||||
// so one still pending at shutdown is not answerable after the restart.
|
||||
func (c *Chat) DetachSessions() { c.Close() }
|
||||
|
||||
func (c *Chat) readLoop() {
|
||||
for {
|
||||
_, data, err := c.conn.Read(c.ctx)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package codex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A server restart detaches from the App Server without interrupting the
|
||||
// thread's turn; the next server's fresh connection still sees the turn in
|
||||
// progress, then completed, through the read-only turn list.
|
||||
func TestDetachSessionsLeavesTurnRunningForNextConnection(t *testing.T) {
|
||||
var completed atomic.Bool
|
||||
var mu sync.Mutex
|
||||
var methods []string
|
||||
appServer := func(method string, _ json.RawMessage) any {
|
||||
mu.Lock()
|
||||
methods = append(methods, method)
|
||||
mu.Unlock()
|
||||
status := "inProgress"
|
||||
if completed.Load() {
|
||||
status = "completed"
|
||||
}
|
||||
return json.RawMessage(`{"data":[{"id":"turn","status":"` + status + `","items":[]}],"nextCursor":null}`)
|
||||
}
|
||||
before := modelTestChat(t, appServer)
|
||||
if running, err := before.ProbeTurn(context.Background(), fixtureThread); err != nil || !running {
|
||||
t.Fatalf("turn not running before the restart: %v %v", running, err)
|
||||
}
|
||||
before.DetachSessions()
|
||||
after := modelTestChat(t, appServer)
|
||||
if running, err := after.ProbeTurn(context.Background(), fixtureThread); err != nil || !running {
|
||||
t.Fatalf("turn did not survive the restart: %v %v", running, err)
|
||||
}
|
||||
completed.Store(true) // finished while nobody was watching
|
||||
if running, err := after.ProbeTurn(context.Background(), fixtureThread); err != nil || running {
|
||||
t.Fatalf("completed turn still reported running: %v %v", running, err)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
for _, method := range methods {
|
||||
if method == "turn/interrupt" {
|
||||
t.Fatalf("detaching interrupted the turn: %v", methods)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user