Claude Code chats survive server restarts in a detachable host

A pipelines-claude-host process owns each claude -p CLI and its pipes,
spools its stream-json output with offsets and serves a private Unix
socket. The server detaches on shutdown and reattaches on start,
replaying the spool to rebuild turns, approvals, questions, background
tasks and the live model. Explicit close, idle release and CLI exit stop
the host; a crashed host falls back to --resume with a notice.
This commit is contained in:
2026-11-04 16:09:56 +00:00
parent fa6c0864db
commit c810437aa5
19 changed files with 2396 additions and 37 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{}