diff --git a/docs/cli.md b/docs/cli.md index b1e9c2ac..3b2c2563 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -349,6 +349,12 @@ sessions pipeline runs create. The protocol types are: value all resolve to the home directory). A relative or missing directory is rejected before any OpenCode call. The created session is recorded with the stored tag `operator` and returned as the response's `chat_session`. + An optional `ephemeral: true` creates the session for automated verification: + it is created, bootstrapped and usable exactly like any other chat and can still + be closed and reopened, but its registry row is hidden, so `chat_sessions` never + lists it and a check that drives the real UI cannot leave a stray "New chat" in + the operator's list. The flag only removes a chat from the shared list, so it + cannot reveal or alter another session. - `chat_directories` returns the operator's home first, then the directories of previous operator sessions most-recently-used first, deduplicated and bounded (currently 8). It is a dedicated request so the create dialog can load the diff --git a/internal/pipeline/operator_sessions.go b/internal/pipeline/operator_sessions.go index 4d1f8a9a..fedb4736 100644 --- a/internal/pipeline/operator_sessions.go +++ b/internal/pipeline/operator_sessions.go @@ -58,7 +58,7 @@ func (s *Store) initOperatorSessions() error { _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS operator_sessions ( session_id TEXT PRIMARY KEY, directory TEXT NOT NULL, title TEXT NOT NULL DEFAULT '', tag TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL, last_used_at INTEGER NOT NULL, -closed_at INTEGER NOT NULL DEFAULT 0); +closed_at INTEGER NOT NULL DEFAULT 0, ephemeral INTEGER NOT NULL DEFAULT 0); CREATE INDEX IF NOT EXISTS operator_sessions_recent ON operator_sessions(last_used_at DESC);`) if err != nil { return err @@ -131,7 +131,13 @@ func (s *Store) AdvanceChatCreate(ctx context.Context, requestID, from, to strin // row valid and open (0), so an upgrade is idempotent across repeated Open calls // and no session is silently reported as closed. func (s *Store) migrateOperatorSessions() error { - return s.addColumnIfMissing("operator_sessions", "closed_at", "INTEGER NOT NULL DEFAULT 0") + if err := s.addColumnIfMissing("operator_sessions", "closed_at", "INTEGER NOT NULL DEFAULT 0"); err != nil { + return err + } + // ephemeral marks a chat created for automated verification. It is hidden from + // OperatorSessions but still a real row, so the session stays usable and + // closable. Existing rows default to 0 (a normal, listed chat). + return s.addColumnIfMissing("operator_sessions", "ephemeral", "INTEGER NOT NULL DEFAULT 0") } // RecordOperatorSession upserts one operator chat session in a single statement: @@ -139,13 +145,32 @@ func (s *Store) migrateOperatorSessions() error { // repeated id updates directory/title/tag and moves last_used_at to now while // preserving the original created_at. CreatedAt is therefore stable across // re-records. Recording also clears any closed state: re-creating or adopting a -// session is use, so it is open again. +// session is use, so it is open again. The row is a normal listed operator chat +// (ephemeral = 0). func (s *Store) RecordOperatorSession(ctx context.Context, sessionID, directory, title, tag string) error { + return s.recordSession(ctx, sessionID, directory, title, tag, false) +} + +// RecordVerificationSession upserts one chat created for automated verification +// (chat_create with ephemeral set). It is a real registry row — so the session +// can still be closed, reopened and looked up by id — but OperatorSessions, and +// therefore chat_sessions, never lists it. It exists so a check that drives the +// real UI cannot leave a permanent "New chat" in the operator's list, and a +// re-record always keeps it hidden. +func (s *Store) RecordVerificationSession(ctx context.Context, sessionID, directory, title, tag string) error { + return s.recordSession(ctx, sessionID, directory, title, tag, true) +} + +func (s *Store) recordSession(ctx context.Context, sessionID, directory, title, tag string, ephemeral bool) error { now := time.Now().UnixMilli() - _, err := s.db.ExecContext(ctx, `INSERT INTO operator_sessions(session_id,directory,title,tag,created_at,last_used_at,closed_at) -VALUES(?,?,?,?,?,?,0) -ON CONFLICT(session_id) DO UPDATE SET directory=excluded.directory,title=excluded.title,tag=excluded.tag,last_used_at=excluded.last_used_at,closed_at=0`, - sessionID, directory, title, tag, now, now) + hidden := 0 + if ephemeral { + hidden = 1 + } + _, err := s.db.ExecContext(ctx, `INSERT INTO operator_sessions(session_id,directory,title,tag,created_at,last_used_at,closed_at,ephemeral) +VALUES(?,?,?,?,?,?,0,?) +ON CONFLICT(session_id) DO UPDATE SET directory=excluded.directory,title=excluded.title,tag=excluded.tag,last_used_at=excluded.last_used_at,closed_at=0,ephemeral=excluded.ephemeral`, + sessionID, directory, title, tag, now, now, hidden) return err } @@ -196,13 +221,15 @@ FROM operator_sessions WHERE session_id=? AND tag <> ''`, sessionID) // OperatorSessions reports the operator's chat sessions, most-recently-used // first (ties broken by session id for a deterministic order), capped at // operatorSessionListLimit. Rows with no tag are omitted: a session with no -// recorded tag is not an operator session and must never be listed. Closed -// sessions are included: the app groups or filters them as history, so the +// recorded tag is not an operator session and must never be listed. Ephemeral +// rows are omitted too: a chat created for automated verification is usable and +// closable but never listed here, so it cannot clutter the operator's list. +// Closed sessions are included: the app groups or filters them as history, so the // operator can still see and reopen what they finished. It is one bounded // statement; recency is never mutated here. func (s *Store) OperatorSessions(ctx context.Context) ([]OperatorSession, error) { rows, err := s.db.QueryContext(ctx, `SELECT session_id,directory,title,tag,created_at,last_used_at,closed_at -FROM operator_sessions WHERE tag <> '' ORDER BY last_used_at DESC, session_id LIMIT ?`, operatorSessionListLimit) +FROM operator_sessions WHERE tag <> '' AND ephemeral = 0 ORDER BY last_used_at DESC, session_id LIMIT ?`, operatorSessionListLimit) if err != nil { return nil, err } diff --git a/internal/pipeline/operator_sessions_test.go b/internal/pipeline/operator_sessions_test.go index e326350f..06140a79 100644 --- a/internal/pipeline/operator_sessions_test.go +++ b/internal/pipeline/operator_sessions_test.go @@ -232,3 +232,91 @@ func TestOperatorSessionsSurviveRestart(t *testing.T) { t.Fatalf("restarted row missing fields: %+v", after[0]) } } + +// TestRecordVerificationSessionIsRealButNeverListed proves the automated- +// verification contract: the row exists and is closable (so a check that drives +// the real page can still close its throwaway chat), but OperatorSessions — and +// therefore chat_sessions — never lists it. +func TestRecordVerificationSessionIsRealButNeverListed(t *testing.T) { + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "verification-session.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if err := store.RecordOperatorSession(ctx, "ses_mine", "/work/mine", "Mine", "operator"); err != nil { + t.Fatal(err) + } + if err := store.RecordVerificationSession(ctx, "ses_check", "/work/check", "New chat", "operator"); err != nil { + t.Fatal(err) + } + listed, err := store.OperatorSessions(ctx) + if err != nil { + t.Fatal(err) + } + if len(listed) != 1 || listed[0].ID != "ses_mine" { + t.Fatalf("verification session was listed: %+v", listed) + } + session, found, err := store.OperatorSession(ctx, "ses_check") + if err != nil || !found { + t.Fatalf("verification session is not looked up: found=%v err=%v", found, err) + } + if session.ID != "ses_check" || session.Directory != "/work/check" { + t.Fatalf("verification session row = %+v", session) + } + if err := store.CloseOperatorSession(ctx, "ses_check"); err != nil { + t.Fatalf("close verification session: %v", err) + } + // Re-recording keeps it hidden, and a normal record of the same id lists it + // again (the flag is replaced, not sticky). + if err := store.RecordVerificationSession(ctx, "ses_check", "/work/check", "New chat", "operator"); err != nil { + t.Fatal(err) + } + if listed, err = store.OperatorSessions(ctx); err != nil || len(listed) != 1 { + t.Fatalf("re-recorded verification session listed: %+v err=%v", listed, err) + } + if err := store.RecordOperatorSession(ctx, "ses_check", "/work/check", "Kept", "operator"); err != nil { + t.Fatal(err) + } + if listed, err = store.OperatorSessions(ctx); err != nil || len(listed) != 2 { + t.Fatalf("a normal re-record did not list the session: %+v err=%v", listed, err) + } +} + +// TestOperatorSessionsMigrationAddsEphemeral proves an existing database created +// before the ephemeral column opens cleanly and keeps every existing chat listed. +func TestOperatorSessionsMigrationAddsEphemeral(t *testing.T) { + ctx := context.Background() + path := filepath.Join(t.TempDir(), "migrate-ephemeral.db") + store, err := Open(path) + if err != nil { + t.Fatal(err) + } + if err := store.RecordOperatorSession(ctx, "ses_old", "/work/old", "Old", "operator"); err != nil { + t.Fatal(err) + } + if _, err := store.db.ExecContext(ctx, "ALTER TABLE operator_sessions DROP COLUMN ephemeral"); err != nil { + t.Fatalf("drop ephemeral: %v", err) + } + if err := store.Close(); err != nil { + t.Fatal(err) + } + store, err = Open(path) + if err != nil { + t.Fatalf("Open after drop: %v", err) + } + defer store.Close() + listed, err := store.OperatorSessions(ctx) + if err != nil { + t.Fatal(err) + } + if len(listed) != 1 || listed[0].ID != "ses_old" { + t.Fatalf("migrated session list = %+v", listed) + } + if err := store.RecordVerificationSession(ctx, "ses_new_check", "/work/new", "New chat", "operator"); err != nil { + t.Fatal(err) + } + if listed, err = store.OperatorSessions(ctx); err != nil || len(listed) != 1 { + t.Fatalf("verification session listed after migration: %+v err=%v", listed, err) + } +} diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index c9f9f4ed..cd6b87da 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -187,6 +187,14 @@ type ChatCreateRequest struct { Title string `json:"title,omitempty"` Directory string `json:"directory,omitempty"` OperationID string `json:"operation_id,omitempty"` + // Ephemeral creates a chat for automated verification: the session is created, + // bootstrapped and usable exactly like a normal one, but no operator-registry + // row is written, so it can never appear in chat_sessions. It exists because a + // verification client drives the real UI and would otherwise leave a permanent + // "New chat" in the operator's list for every run. The flag is client-supplied + // and only ever *removes* a chat from the shared list, so a buggy or hostile + // client can hide its own chat but cannot reveal or alter another one. + Ephemeral bool `json:"ephemeral,omitempty"` } // ChatAdoptRequest is the chat_adopt request body: the id of a session that diff --git a/internal/server/chat.go b/internal/server/chat.go index c0da3f30..8ae8bd3f 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -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) diff --git a/internal/server/chat_sessions.go b/internal/server/chat_sessions.go index a2213e94..5b22851f 100644 --- a/internal/server/chat_sessions.go +++ b/internal/server/chat_sessions.go @@ -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) } diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go index 312bc221..c4c567c7 100644 --- a/internal/server/chat_test.go +++ b/internal/server/chat_test.go @@ -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) diff --git a/web/src/app.js b/web/src/app.js index 96682034..ee4f0661 100644 --- a/web/src/app.js +++ b/web/src/app.js @@ -4582,7 +4582,12 @@ function applyChatCommandEffect(sessionId, session, effect) { async function startChatSession(directory) { const from = state.chatActiveId; try { - const created = await repo.chatCreate("New chat", directory); + // An automated check (the UI smoke) can mark its throwaway chat ephemeral, so + // creating one for verification never adds a stray "New chat" to the + // operator's list. Set only by test tooling; a normal page leaves it unset. + const created = await repo.chatCreate("New chat", directory, undefined, { + ephemeral: globalThis.__pipelinesEphemeralChat === true, + }); // The list is the server's; insert the created session so the row is present // before the conversation opens, then refresh from the server. state.chatSessions = [created, ...state.chatSessions.filter((s) => s.id !== created.id)]; diff --git a/web/src/repository.js b/web/src/repository.js index 62f112db..1126f410 100644 --- a/web/src/repository.js +++ b/web/src/repository.js @@ -389,11 +389,16 @@ export class PipelinesRepository { * never created in whatever directory the server happens to be running in. The * reply's `chat_session` is returned, or a `ProtocolError` when the server did * not return one. + * + * `ephemeral` creates a verification chat: the server makes and bootstraps the + * session but records no operator-registry row, so the chat never appears in + * `chat_sessions` for the operator. Automated checks that drive this real page + * set it, so a run can never leave a stray "New chat" in the operator's list. */ - async chatCreate(title, directory, operationId = newChatOperationId()) { + async chatCreate(title, directory, operationId = newChatOperationId(), { ephemeral = false } = {}) { const response = await this.#client.call({ type: Protocol.CHAT_CREATE, - [ChatKeys.CREATE]: { title, directory, operation_id: operationId }, + [ChatKeys.CREATE]: { title, directory, operation_id: operationId, ...(ephemeral ? { ephemeral: true } : {}) }, }); const session = response[ChatKeys.SESSION]; if (!session) { diff --git a/web/test/ui-smoke.mjs b/web/test/ui-smoke.mjs index 68eee472..474e4f06 100644 --- a/web/test/ui-smoke.mjs +++ b/web/test/ui-smoke.mjs @@ -3371,7 +3371,11 @@ try { await page.fill("#chatInput", ""); // (e) `/new` creates a fresh throwaway session (C2's chat_create, sent directly - // until C2 lands) and opens it. The operator's session is left untouched. + // until C2 lands) and opens it. The operator's session is left untouched. The + // throwaway is created EPHEMERAL: this check drives the real page, so without it + // every run would leave a permanent "New chat" in the operator's chat list (the + // server writes no operator-registry row for an ephemeral create). + await page.evaluate(`window.__pipelinesEphemeralChat = true`); await page.fill("#chatInput", "/new"); await page.evaluate( `document.querySelector("#chatInput").dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }))`,