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
+24
View File
@@ -322,6 +322,30 @@ func (c *Chat) Interrupt(ctx context.Context, id string) error {
return nil
}
// ReleaseSession stops active and background work for an explicit operator
// close or /new without deleting the persisted native conversation. OpenCode
// interrupt only stops the foreground drain; running shells are separate native
// operations and must be terminated individually.
func (c *Chat) ReleaseSession(ctx context.Context, id string) error {
if err := c.Interrupt(ctx, id); err != nil {
return err
}
shells, err := c.Shells(ctx, id)
if err != nil {
return err
}
for _, shell := range shells {
if shell.Status != protocol.ChatShellRunning {
continue
}
if _, err := c.call(ctx, "delete", "/api/shell/"+url.PathEscape(shell.ID), nil); err != nil {
return fmt.Errorf("stop shell %s: %w", shell.ID, err)
}
}
c.ReleaseShells(id)
return nil
}
// Detach is the server-restart shutdown (chatShutdownDetacher). Sessions, turns, questions,
// permissions and shells live in the OpenCode service, not in this adapter, so
// there is nothing to stop: the connection's owner decides whether the service
+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