2fe63012f9
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).
323 lines
11 KiB
Go
323 lines
11 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestChatCreateBootstrapTransitionsAreOwnedAndDurable(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "operator.db")
|
|
store, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bootstrap, err := store.BeginChatCreate(ctx, "request", "title", "/work")
|
|
if err != nil || bootstrap.State != "created" {
|
|
t.Fatalf("BeginChatCreate = %+v, %v", bootstrap, err)
|
|
}
|
|
if err := store.AdvanceChatCreate(ctx, "request", "created", "creating"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.AdvanceChatCreate(ctx, "request", "created", "creating"); !errors.Is(err, ErrChatCreateConflict) {
|
|
t.Fatalf("second reservation error = %v, want ownership conflict", err)
|
|
}
|
|
if err := store.SetChatCreateSession(ctx, "request", "ses_one"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.SetChatCreateSession(ctx, "request", "ses_two"); !errors.Is(err, ErrChatCreateConflict) {
|
|
t.Fatalf("second session owner error = %v, want ownership conflict", err)
|
|
}
|
|
if err := store.AdvanceChatCreate(ctx, "request", "creating", "activated"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reopened, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer reopened.Close()
|
|
bootstrap, err = reopened.BeginChatCreate(ctx, "request", "title", "/work")
|
|
if err != nil || bootstrap.SessionID != "ses_one" || bootstrap.State != "activated" {
|
|
t.Fatalf("reopened bootstrap = %+v, %v", bootstrap, err)
|
|
}
|
|
}
|
|
|
|
// TestOperatorSessionsRecordListAndUpsert proves the operator-session registry
|
|
// contract: rows persist the id, directory, title and tag; the list is
|
|
// most-recently-used first; and a second RecordOperatorSession for the same id
|
|
// updates directory/title/tag/last_used_at while preserving the original
|
|
// created_at.
|
|
func TestOperatorSessionsRecordListAndUpsert(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "operator.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
|
|
if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a", "A", "operator"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
time.Sleep(3 * time.Millisecond)
|
|
if err := store.RecordOperatorSession(ctx, "ses_b", "/work/b", "B", "operator"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sessions, err := store.OperatorSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(sessions) != 2 {
|
|
t.Fatalf("sessions = %+v, want 2", sessions)
|
|
}
|
|
// Most-recently-used first: ses_b was recorded after ses_a.
|
|
if sessions[0].ID != "ses_b" || sessions[1].ID != "ses_a" {
|
|
t.Fatalf("not ordered most-recently-used first: %+v", sessions)
|
|
}
|
|
if sessions[1].Directory != "/work/a" || sessions[1].Title != "A" || sessions[1].Tag != "operator" || sessions[1].CreatedAt == 0 || sessions[1].UpdatedAt == 0 {
|
|
t.Fatalf("recorded row missing fields: %+v", sessions[1])
|
|
}
|
|
originalCreated := sessions[1].CreatedAt
|
|
|
|
time.Sleep(3 * time.Millisecond)
|
|
if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a2", "A2", "operator"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sessions, err = store.OperatorSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sessions[0].ID != "ses_a" {
|
|
t.Fatalf("upsert did not move the session to the front: %+v", sessions)
|
|
}
|
|
updated := sessions[0]
|
|
if updated.Directory != "/work/a2" || updated.Title != "A2" || updated.Tag != "operator" {
|
|
t.Fatalf("upsert did not update directory/title/tag: %+v", updated)
|
|
}
|
|
if updated.CreatedAt != originalCreated {
|
|
t.Fatalf("upsert changed created_at: got %d want %d", updated.CreatedAt, originalCreated)
|
|
}
|
|
if updated.UpdatedAt <= originalCreated {
|
|
t.Fatalf("upsert did not move last_used_at: %+v", updated)
|
|
}
|
|
}
|
|
|
|
// TestOperatorSessionsIgnoresUntagged proves an untagged row is not an operator
|
|
// session: it is never listed and never contributes a directory choice.
|
|
func TestOperatorSessionsIgnoresUntagged(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "untagged.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if err := store.RecordOperatorSession(ctx, "ses_untagged", "/work/untagged", "no tag", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sessions, err := store.OperatorSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(sessions) != 0 {
|
|
t.Fatalf("untagged session was listed: %+v", sessions)
|
|
}
|
|
directories, err := store.OperatorDirectories(ctx, 8)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(directories) != 0 {
|
|
t.Fatalf("untagged session contributed a directory: %+v", directories)
|
|
}
|
|
}
|
|
|
|
// TestOperatorDirectoriesDedupeBoundAndOrder proves OperatorDirectories returns
|
|
// distinct directories most-recently-used first, deduplicated and bounded: more
|
|
// directories are seeded than the requested limit.
|
|
func TestOperatorDirectoriesDedupeBoundAndOrder(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "directories.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
|
|
record := func(id, directory string) {
|
|
t.Helper()
|
|
if err := store.RecordOperatorSession(ctx, id, directory, id, "operator"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
record("ses_1", "/work/one")
|
|
record("ses_2", "/work/one") // duplicate directory for a newer session
|
|
record("ses_3", "/work/two")
|
|
record("ses_4", "/work/three")
|
|
record("ses_5", "/work/four")
|
|
// Set the recency explicitly so the ordering assertion is deterministic.
|
|
set := func(id string, at int64) {
|
|
t.Helper()
|
|
if _, err := store.db.ExecContext(ctx, "UPDATE operator_sessions SET last_used_at=? WHERE session_id=?", at, id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
set("ses_1", 10)
|
|
set("ses_2", 50) // the duplicate dir's newest use wins the ordering
|
|
set("ses_3", 40)
|
|
set("ses_4", 30)
|
|
set("ses_5", 20)
|
|
|
|
directories, err := store.OperatorDirectories(ctx, 2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(directories) != 2 {
|
|
t.Fatalf("directories = %+v, want the requested bound of 2", directories)
|
|
}
|
|
if directories[0] != "/work/one" || directories[1] != "/work/two" {
|
|
t.Fatalf("directories = %+v, want the two most recently used distinct dirs", directories)
|
|
}
|
|
all, err := store.OperatorDirectories(ctx, 8)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(all) != 4 {
|
|
t.Fatalf("directories = %+v, want 4 deduplicated", all)
|
|
}
|
|
if all[0] != "/work/one" || all[1] != "/work/two" || all[2] != "/work/three" || all[3] != "/work/four" {
|
|
t.Fatalf("directories not MRU-ordered: %+v", all)
|
|
}
|
|
}
|
|
|
|
// TestOperatorSessionsSurviveRestart proves a recorded operator session is read
|
|
// back after Close and re-Open of the same SQLite path, with its
|
|
// directory/tag/recency intact and no re-registration.
|
|
func TestOperatorSessionsSurviveRestart(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "restart.db")
|
|
store, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.RecordOperatorSession(ctx, "ses_restart", "/work/restart", "Restart", "operator"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, err := store.OperatorSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
store, err = Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
after, err := store.OperatorSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(after) != 1 || len(before) != 1 {
|
|
t.Fatalf("sessions before/after restart = %+v / %+v", before, after)
|
|
}
|
|
if after[0] != before[0] {
|
|
t.Fatalf("session changed across restart:\n got %+v\nwant %+v", after[0], before[0])
|
|
}
|
|
if after[0].Directory != "/work/restart" || after[0].Tag != "operator" || after[0].UpdatedAt == 0 {
|
|
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)
|
|
}
|
|
}
|