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) } } // Unloading is cleanup: a thread the App Server no longer holds must not make // a close or /new fail. func TestReleaseSessionToleratesAnUnsubscribeRefusal(t *testing.T) { c := modelTestChat(t, func(string, json.RawMessage) any { return &rpcError{Code: -32600, Message: "thread not found"} }) if err := c.ReleaseSession(context.Background(), fixtureThread); err != nil { t.Fatalf("a refused unsubscribe must not fail the release: %v", err) } }