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>
This commit is contained in:
2026-11-21 18:10:41 +00:00
parent cfebd3a05b
commit 01040a7b12
8 changed files with 183 additions and 15 deletions
+16 -4
View File
@@ -554,16 +554,28 @@ func (c *Chat) StopTasks(context.Context, string) (int, error) {
return 0, errors.New("Codex does not expose a separate background-task stop operation")
}
// ReleaseSession stops an active Codex turn while preserving the thread and
// its transcript. Codex has no separate per-thread process to reap.
// ReleaseSession stops a Codex thread for an explicit close or /new: it
// interrupts an active turn, then unsubscribes this connection so the App
// Server unloads the thread (and its live runtime) once no other client holds
// it. The persisted thread and transcript stay; a later prompt reloads it
// through the "thread not found" resume path in PromptWithMessageIDModel.
func (c *Chat) ReleaseSession(ctx context.Context, id string) error {
c.mu.Lock()
turn := c.turns[id]
c.mu.Unlock()
if turn == "" {
if turn != "" {
if err := c.Interrupt(ctx, id); err != nil {
return err
}
}
_, err := c.call(ctx, "thread/unsubscribe", map[string]any{"threadId": id})
var refused *rpcCallError
if errors.As(err, &refused) && refused.Code == -32601 {
// An App Server without thread/unsubscribe keeps the thread loaded;
// the interrupt above already stopped its work.
return nil
}
return c.Interrupt(ctx, id)
return err
}
// Create starts one Codex thread in directory.
+56
View File
@@ -46,3 +46,59 @@ func TestDetachLeavesTurnRunningForNextConnection(t *testing.T) {
}
}
}
// 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)
}
}