chat: a verification chat can never appear in the operator's list

The operator's chat list showed ~99 stray "New chat" entries. The listing
was already fail-closed (registry-only); the fault was what got recorded: an
automated UI check drives the real page, so its throwaway chat was recorded as
an operator chat exactly like a manually created one and stayed in the list for
ever (each run added one).

Server: chat_create takes an optional ephemeral flag. The session is created,
bootstrapped, usable, closable and reopenable exactly like any other, but its
registry row is hidden, so chat_sessions never lists it. operator_sessions gains
an ephemeral column (idempotent migration, existing rows default to listed) and
the list query filters it; the single-row lookup does not, so close/reopen keep
working. RecordVerificationSession is the hidden recorder.

Web: the page marks a create ephemeral only when automation asks for it
(__pipelinesEphemeralChat, set by the UI smoke for its throwaway chat), so the
smoke's own runs stop polluting while a manual create is unchanged.

Tests: a hidden row is never listed yet is still looked up and closable; a
normal re-record of the same id lists it again; a pre-ephemeral database
migrates with every existing chat still listed. Live data pruned separately
(107 -> 8 registry rows, backed up).
This commit is contained in:
2026-10-19 13:15:08 +01:00
parent 94c802a147
commit 2fe63012f9
10 changed files with 233 additions and 18 deletions
+3
View File
@@ -68,6 +68,9 @@ type operatorRegistry interface {
SetChatCreateSession(ctx context.Context, requestID, sessionID string) error
AdvanceChatCreate(ctx context.Context, requestID, from, to string) error
RecordOperatorSession(ctx context.Context, sessionID, directory, title, tag string) error
// RecordVerificationSession records a chat created for automated verification:
// a real, closable row that OperatorSessions never lists.
RecordVerificationSession(ctx context.Context, sessionID, directory, title, tag string) error
TouchOperatorSession(ctx context.Context, sessionID string) error
OperatorSessions(ctx context.Context) ([]pipeline.OperatorSession, error)
OperatorDirectories(ctx context.Context, limit int) ([]string, error)
+19 -4
View File
@@ -90,7 +90,9 @@ func handleChatSessions(ctx context.Context, request protocol.Message, broker ch
// resolved directory, the title and the operator tag. A blank or omitted
// directory defaults to the operator's home, never the server's working
// directory. A registry write failure answers an error naming the created id, so
// an orphaned session is visible rather than silently reported as success.
// an orphaned session is visible rather than silently reported as success. A
// request with Ephemeral set creates the session without any registry row, for
// automated verification that must never add a chat to the operator's list.
func handleChatCreate(ctx context.Context, request protocol.Message, broker chatBroker, 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"})
@@ -103,10 +105,12 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
title := ""
directory := ""
operationID := ""
ephemeral := false
if request.ChatCreate != nil {
title = strings.TrimSpace(request.ChatCreate.Title)
directory = strings.TrimSpace(request.ChatCreate.Directory)
operationID = strings.TrimSpace(request.ChatCreate.OperationID)
ephemeral = request.ChatCreate.Ephemeral
}
// Legacy clients did not carry a durable create identity. Give each received
// request its own server-generated scope so their connection-local envelope
@@ -182,7 +186,14 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
chatCreateConflictError(request, err, write)
return
}
if err := registry.RecordOperatorSession(ctx, session.ID, resolved, title, protocol.ChatSessionTagOperator); err != nil {
// An ephemeral (verification) create records a real but hidden row: the chat
// stays usable and closable, and chat_sessions never lists it. Everything
// else about the session — its id, skill activation and use — is identical.
record := registry.RecordOperatorSession
if ephemeral {
record = registry.RecordVerificationSession
}
if err := record(ctx, session.ID, resolved, title, protocol.ChatSessionTagOperator); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but could not be recorded: " + err.Error()})
return
}
@@ -198,8 +209,12 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
session.Tag = protocol.ChatSessionTagOperator
response := protocol.Message{Type: protocol.ChatCreate, ID: request.ID, ChatSession: &session}
// The same registry row is echoed on the list field so a client can splice it
// into chat_sessions without a re-list.
response.ChatSessions = []protocol.ChatSession{session}
// into chat_sessions without a re-list. An ephemeral create has no listed row
// to splice, so that field stays empty and the client that asked for it opens
// the session from the echoed ChatSession instead.
if !ephemeral {
response.ChatSessions = []protocol.ChatSession{session}
}
_ = write(response)
}
+54
View File
@@ -1451,6 +1451,60 @@ func TestChatCreateRecordsOperatorSession(t *testing.T) {
}
}
func TestChatCreateEphemeralSessionIsNeverListed(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
broker.created = protocol.ChatSession{ID: "ses_verify", Title: "New chat"}
dir := t.TempDir()
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatCreate, ID: "c1", ChatCreate: &protocol.ChatCreateRequest{Title: "New chat", Directory: dir, Ephemeral: true}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
// The session is real and usable: it is created, bootstrapped and echoed.
if response.Type != protocol.ChatCreate || response.ChatSession == nil || response.ChatSession.ID != "ses_verify" {
t.Fatalf("ephemeral chat_create response = %+v", response)
}
if calls, _, _ := broker.createQuery(); calls != 1 {
t.Fatalf("broker.Create calls = %d, want 1", calls)
}
broker.mu.Lock()
activateCalls := broker.activateCalls
broker.mu.Unlock()
if activateCalls != 1 {
t.Fatalf("manager bootstrap calls = %d, want 1", activateCalls)
}
// No LISTED row was written, so no list row is echoed and chat_sessions cannot
// list the verification chat...
if len(response.ChatSessions) != 0 {
t.Fatalf("ephemeral chat echoed a registry row: %+v", response.ChatSessions)
}
recorded, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(recorded) != 0 {
t.Fatalf("ephemeral chat recorded %d listed operator session(s): %+v", len(recorded), recorded)
}
// ...but the row is real, so the session stays closable like any other chat.
if _, found, err := store.OperatorSession(ctx, "ses_verify"); err != nil || !found {
t.Fatalf("ephemeral chat is not closable: found=%v err=%v", found, err)
}
if err := store.CloseOperatorSession(ctx, "ses_verify"); err != nil {
t.Fatalf("close ephemeral chat: %v", err)
}
listed, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(listed) != 0 {
t.Fatalf("closing an ephemeral chat listed it: %+v", listed)
}
}
func TestChatCreateReportsManagerBootstrapFailure(t *testing.T) {
broker := &fakeChat{created: protocol.ChatSession{ID: "ses_new"}, activateErr: errors.New("skill not found")}
store := newOperatorStore(t)