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.