Files
pipelines/internal/server/chat_close.go
T
alan 4f8e99f316 Serialize open-chat sync writes by server revision and fan out prunes
Server: chat_open_workspace is a compare-and-swap on base_revision; a stale
write is refused and answered with the current snapshot (rejected: true).
Each stored change increments the revision; identical writes store nothing.
A request with no body reads the set. Seeds apply only to a never-stored set.
Validation uses the operator registry (recorded, not closed). chat_close and
connection-time pruning store a new revision and push it to every
connection; otherwise a new connection gets the current set to catch up.

App: ChatOpenWorkspaceController keeps one write in flight, coalesces later
changes, sends from the last replied revision and ignores lower revisions;
pushes and rejected replies win over unsent changes. It starts after the
local layout restores (seed or read), keeps a deep-linked chat, and is
disposed on server/gateway rebind. Remote membership is applied by
reconcileChatWorkspaceTree, keeping pane ids and split sizes. Federated
lists keep their local layout.

Tests: Go store/handler fan-out tests; app controller, reconcile and screen
sync tests. Notes record a live two-client scratch check.
2026-11-12 03:23:22 +00:00

171 lines
7.2 KiB
Go

package server
import (
"context"
"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()
}
// Drop queued work before releasing the native runtime so a queued prompt
// cannot restart background work after the release succeeds.
hub.clearQueue(ctx, sessionID)
hub.invalidateRecovery(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 {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " could not be released: " + err.Error()})
return
}
}
if err := hub.clearRecovery(ctx, sessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " recovery state could not be cleared: " + err.Error()})
return
}
if err := registry.CloseOperatorSession(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 + " 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)
// A closed chat leaves every device's open-chat set, so no client can keep
// or restore it.
dropFromChatWorkspace(ctx, registry, hub.changes(), sessionID)
}
// 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)
}