c810437aa5
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.
162 lines
6.7 KiB
Go
162 lines
6.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// handleChatClose finishes an operator chat session and records it as closed on
|
|
// the server. It stops a turn that is running by reusing the existing interrupt
|
|
// (so a stream is never left running into nothing, and the result records that a
|
|
// turn was stopped), then sets the closed state in the operator-session
|
|
// registry. Nothing is deleted: the session, its transcript and its history
|
|
// survive, and every client sees the closed state because it lives in the
|
|
// registry, not on one device. A session id that is not a recorded operator
|
|
// session is refused before anything is written, so a pipeline-owned run session
|
|
// is never touched.
|
|
func handleChatClose(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, write func(protocol.Message) error) {
|
|
if broker == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
|
|
return
|
|
}
|
|
if registry == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
|
|
return
|
|
}
|
|
query := request.ChatQuery
|
|
if query == nil || !validChatSessionID(query.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
|
return
|
|
}
|
|
sessionID := query.SessionID
|
|
if _, found, err := registry.OperatorSession(ctx, sessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
} else if !found {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is not an operator chat session"})
|
|
return
|
|
}
|
|
// Stop a running turn first. A failed interrupt is surfaced and the close is
|
|
// not recorded, so the operator is never told a live turn was stopped when it
|
|
// was not. The flag comes from the session's room (a turn in flight), so an
|
|
// idle session is never interrupted.
|
|
interrupted := hub.turnInFlight(sessionID)
|
|
if interrupted {
|
|
resetCompaction := hub.compactionReset(sessionID)
|
|
if err := broker.Interrupt(ctx, sessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " could not be stopped: " + err.Error()})
|
|
return
|
|
}
|
|
resetCompaction()
|
|
}
|
|
if err := registry.CloseOperatorSession(ctx, sessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
// 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()})
|
|
return
|
|
}
|
|
if !found {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " was closed but is no longer an operator session"})
|
|
return
|
|
}
|
|
wire := chatSessionFromOperator(session)
|
|
response := protocol.Message{
|
|
Type: protocol.ChatClose,
|
|
ID: request.ID,
|
|
ChatSession: &wire,
|
|
ChatClose: &protocol.ChatCloseResult{SessionID: sessionID, ClosedAt: session.ClosedAt, Interrupted: interrupted},
|
|
}
|
|
// Echo the registry row on the list field so a client can splice it into
|
|
// chat_sessions without a re-list.
|
|
response.ChatSessions = []protocol.ChatSession{wire}
|
|
_ = write(response)
|
|
}
|
|
|
|
// handleChatReopen clears the closed state of an operator chat session so it
|
|
// returns to the active list. Nothing is created or deleted; it is the inverse of
|
|
// chat_close. Prompting a closed session also reopens it (the store clears the
|
|
// closed state on touch), so both an explicit reopen and simply sending another
|
|
// message work. A session id that is not a recorded operator session is refused.
|
|
func handleChatReopen(ctx context.Context, request protocol.Message, registry operatorRegistry, write func(protocol.Message) error) {
|
|
if registry == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
|
|
return
|
|
}
|
|
query := request.ChatQuery
|
|
if query == nil || !validChatSessionID(query.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
|
return
|
|
}
|
|
sessionID := query.SessionID
|
|
if _, found, err := registry.OperatorSession(ctx, sessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
} else if !found {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is not an operator chat session"})
|
|
return
|
|
}
|
|
if err := registry.ReopenOperatorSession(ctx, sessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
session, found, err := registry.OperatorSession(ctx, sessionID)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
if !found {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is no longer an operator session"})
|
|
return
|
|
}
|
|
wire := chatSessionFromOperator(session)
|
|
response := protocol.Message{
|
|
Type: protocol.ChatReopen,
|
|
ID: request.ID,
|
|
ChatSession: &wire,
|
|
}
|
|
response.ChatSessions = []protocol.ChatSession{wire}
|
|
_ = write(response)
|
|
}
|
|
|
|
// chatSessionFromOperator projects one registry row to the wire ChatSession the
|
|
// app lists, so close/reopen and chat_sessions agree on the closed fields. The
|
|
// id is always set; title/directory/tag are copied verbatim, and the closed
|
|
// state comes from closed_at.
|
|
func chatSessionFromOperator(session pipeline.OperatorSession) protocol.ChatSession {
|
|
out := protocol.ChatSession{
|
|
ID: session.ID,
|
|
Title: strings.TrimSpace(session.Title),
|
|
Directory: session.Directory,
|
|
Tag: session.Tag,
|
|
CreatedAt: session.CreatedAt,
|
|
UpdatedAt: session.UpdatedAt,
|
|
Backend: session.Backend,
|
|
}
|
|
if session.ClosedAt > 0 {
|
|
out.Closed = true
|
|
out.ClosedAt = session.ClosedAt
|
|
}
|
|
if session.StarredAt > 0 {
|
|
out.Starred = true
|
|
out.StarredAt = session.StarredAt
|
|
}
|
|
out.Color = session.Color
|
|
return withChatHandoff(out)
|
|
}
|