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
+35
View File
@@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"slices"
"strings"
"sync"
"sync/atomic"
@@ -375,6 +376,40 @@ func TestChatInterruptPostsInterruptEndpoint(t *testing.T) {
})
}
func TestChatReleaseSessionStopsRunningShells(t *testing.T) {
var requests []string
var mu sync.Mutex
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
requests = append(requests, r.Method+" "+r.URL.Path)
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/session/ses_1/interrupt":
_, _ = w.Write([]byte(`{}`))
case "/api/shell":
_, _ = w.Write([]byte(`{"data":[{"id":"sh_running","status":"running","metadata":{"sessionID":"ses_1"}},{"id":"sh_done","status":"exited","metadata":{"sessionID":"ses_1"}}]}`))
case "/api/shell/sh_running":
_, _ = w.Write([]byte(`{}`))
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
if err := chat.ReleaseSession(context.Background(), "ses_1"); err != nil {
t.Fatal(err)
}
mu.Lock()
got := append([]string(nil), requests...)
mu.Unlock()
want := []string{"POST /api/session/ses_1/interrupt", "GET /api/shell", "DELETE /api/shell/sh_running"}
if !slices.Equal(got, want) {
t.Fatalf("release requests = %v, want %v", got, want)
}
}
// TestDecodeChatMessages loads a seeded user + assistant session in both
// response envelopes the two surfaces return and asserts the turns come back
// with role, id, part order, part ids and the completed tool's input, content