Merge: Claude Code chats survive server restarts via a detachable host

This commit is contained in:
2026-11-04 16:15:06 +00:00
21 changed files with 2400 additions and 40 deletions
+8
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"log"
"strings"
"git.a-wan.net/alan/pipelines/internal/pipeline"
@@ -58,6 +59,13 @@ func handleChatClose(ctx context.Context, request protocol.Message, broker chatB
}
// A closed chat takes no more turns: prompts it still held are dropped.
hub.clearQueue(ctx, sessionID)
// A harness that keeps the agent alive across restarts (Claude Code's
// host) stops it now; the transcript stays and a later prompt resumes it.
if releaser, ok := broker.(chatSessionReleaser); ok {
if err := releaser.ReleaseSession(ctx, sessionID); err != nil {
log.Printf("chat session %s: release after close: %v", sessionID, err)
}
}
session, found, err := registry.OperatorSession(ctx, sessionID)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
+33 -2
View File
@@ -166,14 +166,32 @@ func (m *chatManager) harness(backend string) chatBroker {
return client
}
// chatShutdownDetacher is implemented by a backend whose live sessions can
// outlive this server: on shutdown it lets them go instead of stopping them,
// and the next server reattaches (Claude Code's detachable hosts).
type chatShutdownDetacher interface {
Detach()
}
// chatSessionReleaser is implemented by a backend that keeps a session's agent
// running until it is explicitly released; chat_close releases it.
type chatSessionReleaser interface {
ReleaseSession(ctx context.Context, sessionID string) error
}
func closeHarness(client chatBroker) {
if detacher, ok := client.(chatShutdownDetacher); ok {
detacher.Detach()
return
}
if closer, ok := client.(interface{ Close() }); ok {
closer.Close()
}
}
// Close releases the owned Codex and Claude Code clients, if any, and stops
// later lazy attachment.
// Close lets go of the Codex and Claude Code clients, if any, and stops later
// lazy attachment. It runs at server shutdown: a backend whose sessions can
// survive it (chatShutdownDetacher) is detached, not stopped.
func (m *chatManager) Close() {
m.mu.Lock()
m.closed = true
@@ -183,6 +201,19 @@ func (m *chatManager) Close() {
closeHarness(claudeClient)
}
// ReleaseSession releases a session's agent on its own harness after an
// explicit close. A harness without that notion is left as is.
func (m *chatManager) ReleaseSession(ctx context.Context, sessionID string) error {
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
return nil
}
if releaser, ok := adapter.(chatSessionReleaser); ok {
return releaser.ReleaseSession(ctx, sessionID)
}
return nil
}
// Backends lists the harnesses available for a new session.
func (m *chatManager) Backends() []string {
backends := []string{}