Files
pipelines/internal/codex/detach_test.go
T
alan 01040a7b12 Stop the replaced session on /new for every harness
/new already released the old native session through the chat-close
release path, but two gaps let the replaced session stay alive, and the
client told the operator it could still be resumed in a terminal:

- Claude Code: ReleaseSession only closed a host this server was
  attached to. A host registered for the session but not attached (a
  failed reattach, or one left by an earlier server) kept running. It is
  now asked to close over its socket and killed as a fallback.
- Codex: ReleaseSession only interrupted an active turn. It now also
  sends thread/unsubscribe so the App Server unloads the thread; a later
  prompt reloads it through the existing "thread not found" resume path.
- The /new notice now says the previous session was stopped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 18:10:41 +00:00

105 lines
3.4 KiB
Go

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 TestDetachLeavesTurnRunningForNextConnection(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.Detach()
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)
}
}
}
// Releasing a thread (chat close or /new) interrupts its turn and then
// unsubscribes, so the App Server unloads the replaced thread instead of
// keeping it loaded indefinitely.
func TestReleaseSessionInterruptsAndUnloadsThread(t *testing.T) {
var mu sync.Mutex
var methods []string
var unsubscribed string
appServer := func(method string, params json.RawMessage) any {
mu.Lock()
defer mu.Unlock()
methods = append(methods, method)
if method == "thread/unsubscribe" {
var body struct {
ThreadID string `json:"threadId"`
}
_ = json.Unmarshal(params, &body)
unsubscribed = body.ThreadID
return json.RawMessage(`{"status":"unsubscribed"}`)
}
return json.RawMessage(`{}`)
}
c := modelTestChat(t, appServer)
c.mu.Lock()
c.turns[fixtureThread] = "turn"
c.mu.Unlock()
if err := c.ReleaseSession(context.Background(), fixtureThread); err != nil {
t.Fatal(err)
}
mu.Lock()
defer mu.Unlock()
if len(methods) != 2 || methods[0] != "turn/interrupt" || methods[1] != "thread/unsubscribe" || unsubscribed != fixtureThread {
t.Fatalf("release must interrupt then unsubscribe the thread: %v (%q)", methods, unsubscribed)
}
}
// An idle thread is still unloaded, and an App Server that predates
// thread/unsubscribe does not make the release fail.
func TestReleaseSessionUnloadsIdleThreadAndToleratesOldServers(t *testing.T) {
var mu sync.Mutex
var methods []string
c := modelTestChat(t, func(method string, _ json.RawMessage) any {
mu.Lock()
defer mu.Unlock()
methods = append(methods, method)
return &rpcError{Code: -32601, Message: "method not found"}
})
if err := c.ReleaseSession(context.Background(), fixtureThread); err != nil {
t.Fatalf("an unsupported unsubscribe must not fail the release: %v", err)
}
mu.Lock()
defer mu.Unlock()
if len(methods) != 1 || methods[0] != "thread/unsubscribe" {
t.Fatalf("an idle release must only unsubscribe: %v", methods)
}
}