Release native chat runtimes on lifecycle changes

Ensure /new and chat close stop native foreground and background work before changing registry state. Preserve transcripts while surfacing release and recovery cleanup failures instead of reporting false success, with focused adapter and server regressions.
This commit is contained in:
2026-11-11 17:11:40 +00:00
parent a5912d9a1e
commit 2307d45417
11 changed files with 197 additions and 14 deletions
+25
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"errors"
"strings"
"testing"
"time"
@@ -85,6 +86,9 @@ func TestChatNewSessionKeepsTheChatOnEveryHarness(t *testing.T) {
if calls, title, directory := owner.createQuery(); calls != 1 || title != "Feature" || directory != "/work/a" {
t.Fatalf("harness create = %d %q %q", calls, title, directory)
}
if released, _ := owner.releaseQuery(); len(released) != 1 || released[0] != tc.oldID {
t.Fatalf("released sessions = %v, want [%s]", released, tc.oldID)
}
pushed := readMessage(t, ctx, conns[1])
if pushed.Type != protocol.ChatSessionChanged || pushed.ChatSession == nil ||
pushed.ChatSession.ID != tc.newID || pushed.ChatSession.PreviousID != tc.oldID {
@@ -108,6 +112,27 @@ func TestChatNewSessionKeepsTheChatOnEveryHarness(t *testing.T) {
}
}
func TestChatNewSessionDoesNotMoveChatWhenReleaseFails(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
broker.created = protocol.ChatSession{ID: "ses_fresh"}
broker.releaseErr = errors.New("native runtime unavailable")
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := store.RecordOperatorSession(ctx, "ses_old", "/work", "A", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
if err := wsjson.Write(ctx, conn, chatNewSessionRequest("new", "ses_old")); err != nil {
t.Fatal(err)
}
reply := readMessage(t, ctx, conn)
if reply.Type != protocol.Error || !strings.Contains(reply.Error, "could not be released") {
t.Fatalf("release failure reply = %+v", reply)
}
if row, found, _ := store.OperatorSession(ctx, "ses_old"); !found || row.ID != "ses_old" {
t.Fatalf("old row moved after release failure: %+v found=%v", row, found)
}
}
// TestChatNewSessionRefusesWithoutCreating proves a refused /new never starts
// a native session: a foreign id, a running turn, and a second /new on a chat
// already moved on.