chat: close and reopen an operator session on the server, keeping its history

Add a server-side close for operator chats, so finishing with a chat takes it out
of the active list for every client without deleting anything. The brokered
protocol gains chat_close and chat_reopen; chat_close stops a running turn by
reusing the existing broker interrupt (its result carries an interrupted flag, so
a stream is never left running into nothing) and then sets closed_at on the
session's operator_sessions row. The session, its transcript and its history
survive, and because the closed state lives in the registry chat_sessions reports
it to every client, not just the one that asked. An id that is not a recorded
operator session, including a pipeline-owned run session, is refused before
anything is written. chat_reopen clears the state again, and simply prompting a
closed session also clears it (the store clears closed_at on touch).

The registry gains closed_at for a fresh database and an idempotent migration for
one created before it existed. The app renders closed sessions under a labelled
Closed section with a Closed chip, offers a Close action with a confirmation that
says it stops a running turn, marks the chat closed on every device and deletes
nothing, and a Reopen action on each closed row. The device-local hide is renamed
Hide on this device, with a snackbar saying the chat stays on the server, so it is
never confused with the real close. No new dependency, the installer and Docker
files are untouched, and no run is started from the app.

Checked before committing: the packaged templates carry no chat surface and need
no update, and internal/author/install.go's managed inventory needs none either
(this adds no reference file). The packaged authoring skill reference
internal/author/skill/references/operations.md and the shared docs/cli.md and
android/README.md are stale because of this change, but an item no longer edits a
shared document: what is stale and what should change there is recorded in
docs/notes/chat-close.md for the docs-fold writer, which is committed here with
the change.

Checks: gofmt clean, go build ./... and go vet ./... clean, and
go test ./internal/protocol/... ./internal/pipeline/... ./internal/server/...
./internal/author/... pass. The app side is verified with :app:assembleDebug and
:app:testDebugUnitTest (--offline --no-daemon, BUILD SUCCESSFUL); the on-device
close, second-client and reopen check is deferred to visual review.

Not deployed; deploying is the operator's step.
This commit is contained in:
2026-10-18 00:10:47 +01:00
parent 73284ff68c
commit 971e555ceb
21 changed files with 1293 additions and 44 deletions
+27
View File
@@ -26,6 +26,16 @@ const (
ChatMessages = "chat_messages"
ChatInterrupt = "chat_interrupt"
ChatReply = "chat_reply"
// ChatClose finishes an operator session and records it as closed on the
// server: any in-flight turn is interrupted through the existing interrupt,
// nothing is deleted (the session, its transcript and its history survive) and
// the closed state is shared by every client because it lives in the
// operator-session registry. ChatReopen clears the closed state again, so a
// finished chat can be resumed. Both take a ChatQuery (the session id) and
// echo the updated session like chat_create/chat_adopt. Neither ever touches
// the pipeline-owned run sessions.
ChatClose = "chat_close"
ChatReopen = "chat_reopen"
// ChatDirectories is the request/response type for the operator's directory
// choices: the home directory plus the directories of previous operator
// sessions, most recently used first, deduplicated and bounded. It is a
@@ -116,6 +126,23 @@ type ChatSession struct {
// of letting two clients silently write the same conversation. It is
// additive and optional: older peers ignore it.
Busy bool `json:"busy,omitempty"`
// Closed records that the operator finished with this session: the server
// keeps the session, its transcript and its history, but the closed state is
// shared by every client (it lives in the operator-session registry, not on
// one device). ClosedAt is the epoch-millisecond close time, 0 while open.
// Additive/omitempty so older peers ignore them.
Closed bool `json:"closed,omitempty"`
ClosedAt int64 `json:"closed_at,omitempty"`
}
// ChatCloseResult reports the outcome of a chat_close: the session id, the
// epoch-millisecond time the closed state was recorded, and whether an
// in-flight turn was interrupted to stop it. It is the record that a running
// turn was stopped, so a stream is never left running into nothing.
type ChatCloseResult struct {
SessionID string `json:"session_id"`
ClosedAt int64 `json:"closed_at"`
Interrupted bool `json:"interrupted,omitempty"`
}
// ChatCreateRequest is the chat_create request body: a title and an optional
+6 -2
View File
@@ -22,6 +22,8 @@ func chatSamples() []struct {
Directory: "/work", ParentID: "ses_0", Tag: ChatSessionTagOperator, CreatedAt: 1792112366711, UpdatedAt: 1792112367999,
}},
{"session_busy", ChatSession{ID: "ses_2", Title: "held", Busy: true}},
{"session_closed", ChatSession{ID: "ses_3", Title: "done", Closed: true, ClosedAt: 1792112367999}},
{"close_result", ChatCloseResult{SessionID: "ses_3", ClosedAt: 1792112367999, Interrupted: true}},
{"create", ChatCreateRequest{Title: "demo", Directory: "/work"}},
{"adopt", ChatAdoptRequest{SessionID: "ses_1"}},
{"query", ChatQuery{SessionID: "ses_1", Limit: 50}},
@@ -166,6 +168,7 @@ func TestChatEnvelopeRoundTrip(t *testing.T) {
},
ChatReply: &ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionReject},
ChatDirectories: []string{"/home/operator", "/work/project"},
ChatClose: &ChatCloseResult{SessionID: "ses_1", ClosedAt: 1792112367999, Interrupted: true},
}
encoded, err := json.Marshal(message)
if err != nil {
@@ -188,7 +191,8 @@ func TestChatEnvelopeRoundTrip(t *testing.T) {
!reflect.DeepEqual(decoded.ChatEvent, message.ChatEvent) ||
!reflect.DeepEqual(decoded.ChatPermission, message.ChatPermission) ||
!reflect.DeepEqual(decoded.ChatReply, message.ChatReply) ||
!reflect.DeepEqual(decoded.ChatDirectories, message.ChatDirectories) {
!reflect.DeepEqual(decoded.ChatDirectories, message.ChatDirectories) ||
!reflect.DeepEqual(decoded.ChatClose, message.ChatClose) {
t.Fatalf("chat payloads lost: %+v", decoded)
}
}
@@ -198,7 +202,7 @@ func TestChatFieldsOmittedWhenUnused(t *testing.T) {
if err != nil {
t.Fatal(err)
}
for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_adopt", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply", "chat_directories"} {
for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_adopt", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply", "chat_directories", "chat_close"} {
if strings.Contains(string(minimal), absent) {
t.Fatalf("absent %q must be omitted: %s", absent, minimal)
}
+4
View File
@@ -166,6 +166,10 @@ type Message struct {
ChatEvent *ChatEventFrame `json:"chat_event,omitempty"`
ChatPermission *ChatPermissionFrame `json:"chat_permission,omitempty"`
ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"`
// ChatClose carries the outcome of a chat_close; the close and reopen
// requests themselves ride on ChatQuery (the session id), matching
// chat_messages/chat_interrupt.
ChatClose *ChatCloseResult `json:"chat_close,omitempty"`
// ChatDirectories carries the operator's directory choices on a
// chat_directories response (home first, then recent operator-session
// directories, deduplicated and bounded). Additive/omitempty so older peers