Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cyclops-cs): persist chat conversations (#7170)

r33drichards8cdce459f8e2
diff --git a/libs/fleet/backend/chat/postgres_store.go b/libs/fleet/backend/chat/postgres_store.go
new file mode 100644
index 0000000000..e8a49bbc0d
--- /dev/null
+++ b/libs/fleet/backend/chat/postgres_store.go
@@ -0,0 +1,213 @@
+package chat
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "time"
+
+ "github.com/jackc/pgx/v5"
+ "github.com/jackc/pgx/v5/pgxpool"
+)
+
+const postgresConversationColumns = "id, title, messages, created_at, updated_at, archived_at"
+
+type PostgresConversationStore struct {
+ pool *pgxpool.Pool
+ now func() time.Time
+}
+
+func NewPostgresConversationStore(ctx context.Context, databaseURL string) (*PostgresConversationStore, error) {
+ config, err := pgxpool.ParseConfig(databaseURL)
+ if err != nil {
+ return nil, fmt.Errorf("parse chat database URL: %w", err)
+ }
+ pool, err := pgxpool.NewWithConfig(ctx, config)
+ if err != nil {
+ return nil, fmt.Errorf("create chat database pool: %w", err)
+ }
+ return &PostgresConversationStore{pool: pool, now: func() time.Time { return time.Now().UTC() }}, nil
+}
+
+func (store *PostgresConversationStore) Close() {
+ store.pool.Close()
+}
+
+func (store *PostgresConversationStore) Create(ctx context.Context, ownerID string) (*Conversation, error) {
+ tx, err := store.pool.Begin(ctx)
+ if err != nil {
+ return nil, fmt.Errorf("begin create conversation: %w", err)
+ }
+ defer tx.Rollback(ctx)
+
+ if _, err := tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1, 0))`, ownerID); err != nil {
+ return nil, fmt.Errorf("lock conversation owner: %w", err)
+ }
+ var count int
+ if err := tx.QueryRow(ctx, `SELECT count(*) FROM chat_conversations WHERE owner_sub = $1`, ownerID).Scan(&count); err != nil {
+ return nil, fmt.Errorf("count owner conversations: %w", err)
+ }
+ if count >= memoryConversationMaxPerOwner {
+ return nil, ErrConversationLimit
+ }
+
+ id, err := newUUID()
+ if err != nil {
+ return nil, fmt.Errorf("generate conversation ID: %w", err)
+ }
+ now := store.now()
+ conversation := &Conversation{ID: id, CreatedAt: now, UpdatedAt: now}
+ if _, err := tx.Exec(ctx, `
+ INSERT INTO chat_conversations (id, owner_sub, title, messages, message_bytes, created_at, updated_at)
+ VALUES ($1, $2, '', '[]'::jsonb, 0, $3, $3)`, id, ownerID, now); err != nil {
+ return nil, fmt.Errorf("insert conversation: %w", err)
+ }
+ if err := tx.Commit(ctx); err != nil {
+ return nil, fmt.Errorf("commit create conversation: %w", err)
+ }
+ return conversation, nil
+}
+
+func (store *PostgresConversationStore) List(ctx context.Context, ownerID string, archived bool) ([]ConversationSummary, error) {
+ rows, err := store.pool.Query(ctx, `
+ SELECT id, title, created_at, updated_at, archived_at
+ FROM chat_conversations
+ WHERE owner_sub = $1 AND (archived_at IS NOT NULL) = $2
+ ORDER BY CASE WHEN $2 THEN archived_at ELSE updated_at END DESC, id`, ownerID, archived)
+ if err != nil {
+ return nil, fmt.Errorf("list conversations: %w", err)
+ }
+ defer rows.Close()
+
+ summaries := make([]ConversationSummary, 0)
+ for rows.Next() {
+ var summary ConversationSummary
+ if err := rows.Scan(&summary.ID, &summary.Title, &summary.CreatedAt, &summary.UpdatedAt, &summary.ArchivedAt); err != nil {
+ return nil, fmt.Errorf("scan conversation summary: %w", err)
+ }
+ summaries = append(summaries, summary)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, fmt.Errorf("iterate conversation summaries: %w", err)
+ }
+ return summaries, nil
+}
+
+func (store *PostgresConversationStore) Get(ctx context.Context, ownerID, conversationID string) (*Conversation, error) {
+ conversation, err := scanPostgresConversation(store.pool.QueryRow(ctx, `
+ SELECT `+postgresConversationColumns+`
+ FROM chat_conversations
+ WHERE id = $1 AND owner_sub = $2`, conversationID, ownerID))
+ if errors.Is(err, pgx.ErrNoRows) {
+ return nil, ErrConversationNotFound
+ }
+ if err != nil {
+ return nil, fmt.Errorf("get conversation: %w", err)
+ }
+ return conversation, nil
+}
+
+func (store *PostgresConversationStore) SetArchived(ctx context.Context, ownerID, conversationID string, archived bool) (*Conversation, error) {
+ now := store.now()
+ conversation, err := scanPostgresConversation(store.pool.QueryRow(ctx, `
+ UPDATE chat_conversations
+ SET archived_at = CASE WHEN $3 THEN $4 ELSE NULL END, updated_at = $4
+ WHERE id = $1 AND owner_sub = $2
+ RETURNING `+postgresConversationColumns, conversationID, ownerID, archived, now))
+ if errors.Is(err, pgx.ErrNoRows) {
+ return nil, ErrConversationNotFound
+ }
+ if err != nil {
+ return nil, fmt.Errorf("archive conversation: %w", err)
+ }
+ return conversation, nil
+}
+
+func (store *PostgresConversationStore) Append(ctx context.Context, ownerID, conversationID string, messages ...Message) error {
+ if len(messages) == 0 {
+ return nil
+ }
+
+ now := store.now()
+ pending := make([]Message, len(messages))
+ addedBytes := 0
+ for index, message := range messages {
+ if message.ID == "" {
+ id, err := newUUID()
+ if err != nil {
+ return fmt.Errorf("generate message ID: %w", err)
+ }
+ message.ID = id
+ }
+ if message.CreatedAt.IsZero() {
+ message.CreatedAt = now
+ }
+ pending[index] = cloneMessage(message)
+ addedBytes += messageSize(message)
+ }
+ encoded, err := json.Marshal(pending)
+ if err != nil {
+ return fmt.Errorf("encode chat messages: %w", err)
+ }
+
+ tx, err := store.pool.Begin(ctx)
+ if err != nil {
+ return fmt.Errorf("begin append conversation: %w", err)
+ }
+ defer tx.Rollback(ctx)
+
+ var archivedAt *time.Time
+ var currentBytes int
+ var title string
+ if err := tx.QueryRow(ctx, `
+ SELECT archived_at, message_bytes, title
+ FROM chat_conversations
+ WHERE id = $1 AND owner_sub = $2
+ FOR UPDATE`, conversationID, ownerID).Scan(&archivedAt, &currentBytes, &title); errors.Is(err, pgx.ErrNoRows) {
+ return ErrConversationNotFound
+ } else if err != nil {
+ return fmt.Errorf("lock conversation: %w", err)
+ }
+ if archivedAt != nil {
+ return ErrConversationArchived
+ }
+ if currentBytes+addedBytes > memoryConversationMaxBytes {
+ return ErrConversationLimit
+ }
+ if title == "" {
+ title = titleFromFirstUserMessage(pending)
+ }
+ if _, err := tx.Exec(ctx, `
+ UPDATE chat_conversations
+ SET messages = messages || $3::jsonb,
+ message_bytes = $4,
+ title = $5,
+ updated_at = $6
+ WHERE id = $1 AND owner_sub = $2`, conversationID, ownerID, string(encoded), currentBytes+addedBytes, title, now); err != nil {
+ return fmt.Errorf("append conversation messages: %w", err)
+ }
+ if err := tx.Commit(ctx); err != nil {
+ return fmt.Errorf("commit append conversation: %w", err)
+ }
+ return nil
+}
+
+func scanPostgresConversation(row pgx.Row) (*Conversation, error) {
+ var conversation Conversation
+ var encoded []byte
+ if err := row.Scan(
+ &conversation.ID,
+ &conversation.Title,
+ &encoded,
+ &conversation.CreatedAt,
+ &conversation.UpdatedAt,
+ &conversation.ArchivedAt,
+ ); err != nil {
+ return nil, err
+ }
+ if err := json.Unmarshal(encoded, &conversation.Messages); err != nil {
+ return nil, fmt.Errorf("decode conversation messages: %w", err)
+ }
+ return &conversation, nil
+}
diff --git a/libs/fleet/backend/database/migrations/000006_chat_conversations.sql b/libs/fleet/backend/database/migrations/000006_chat_conversations.sql
new file mode 100644
index 0000000000..4308b1f60d
--- /dev/null
+++ b/libs/fleet/backend/database/migrations/000006_chat_conversations.sql
@@ -0,0 +1,19 @@
+CREATE TABLE public.chat_conversations (
+ id text PRIMARY KEY,
+ owner_sub text NOT NULL,
+ title text NOT NULL DEFAULT '',
+ messages jsonb NOT NULL DEFAULT '[]'::jsonb,
+ message_bytes bigint NOT NULL DEFAULT 0 CHECK (message_bytes >= 0),
+ created_at timestamptz NOT NULL,
+ updated_at timestamptz NOT NULL,
+ archived_at timestamptz,
+ CONSTRAINT chat_conversations_messages_array CHECK (jsonb_typeof(messages) = 'array')
+);
+CREATE INDEX chat_conversations_owner_active_idx
+ ON public.chat_conversations (owner_sub, updated_at DESC, id)
+ WHERE archived_at IS NULL;
+CREATE INDEX chat_conversations_owner_archived_idx
+ ON public.chat_conversations (owner_sub, archived_at DESC, id)
+ WHERE archived_at IS NOT NULL;
+REVOKE ALL ON TABLE public.chat_conversations FROM PUBLIC;
+GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.chat_conversations TO cyclops_app;
diff --git a/libs/fleet/backend/database/migrator_pg_test.go b/libs/fleet/backend/database/migrator_pg_test.go
index c64e686eb4..cf013fcdfc 100644
--- a/libs/fleet/backend/database/migrator_pg_test.go
+++ b/libs/fleet/backend/database/migrator_pg_test.go
@@ -14,6 +14,7 @@ import (
"testing"
"time"
+ "cyclops-cs-backend/chat"
"github.com/jackc/pgx/v5"
)
@@ -123,6 +124,46 @@ func TestInitialMigrationBuildsCompleteDatabase(t *testing.T) {
assertMetabaseBoundary(t, ctx, inspectionURL, credentials.Metabase)
assertUsageReaderBoundary(t, ctx, inspectionURL, credentials.Usage)
assertApplicationBoundary(t, ctx, credentials.Application)
+ assertChatConversationStore(t, ctx, credentials.Application)
+}
+
+func assertChatConversationStore(t *testing.T, ctx context.Context, applicationURL string) {
+ t.Helper()
+ first, err := chat.NewPostgresConversationStore(ctx, applicationURL)
+ if err != nil {
+ t.Fatalf("create first chat store: %v", err)
+ }
+ defer first.Close()
+ second, err := chat.NewPostgresConversationStore(ctx, applicationURL)
+ if err != nil {
+ t.Fatalf("create second chat store: %v", err)
+ }
+ defer second.Close()
+
+ conversation, err := first.Create(ctx, "owner-1")
+ if err != nil {
+ t.Fatalf("create conversation: %v", err)
+ }
+ if err := first.Append(ctx, "owner-1", conversation.ID, chat.Message{Role: chat.RoleUser, Content: "list pools"}); err != nil {
+ t.Fatalf("append through first store: %v", err)
+ }
+ loaded, err := second.Get(ctx, "owner-1", conversation.ID)
+ if err != nil {
+ t.Fatalf("load through second store: %v", err)
+ }
+ if loaded.Title != "list pools" || len(loaded.Messages) != 1 {
+ t.Fatalf("second store loaded conversation = %+v", loaded)
+ }
+ if err := second.Append(ctx, "owner-1", conversation.ID, chat.Message{Role: chat.RoleAssistant, Content: "ready"}); err != nil {
+ t.Fatalf("append through second store: %v", err)
+ }
+ loaded, err = first.Get(ctx, "owner-1", conversation.ID)
+ if err != nil {
+ t.Fatalf("reload through first store: %v", err)
+ }
+ if len(loaded.Messages) != 2 || loaded.Messages[1].Content != "ready" {
+ t.Fatalf("first store did not observe second store append: %+v", loaded.Messages)
+ }
}
func TestRunUpgradesVersionOneAndThenNoOps(t *testing.T) {
@@ -134,14 +175,14 @@ func TestRunUpgradesVersionOneAndThenNoOps(t *testing.T) {
upgrade := captureRunSummary(t, func() error {
return Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials})
})
- if upgrade.Current != 1 || upgrade.Target != 5 || upgrade.Pending != 4 || upgrade.Applied != 4 || upgrade.Skipped != 1 || upgrade.Result != "success" {
+ if upgrade.Current != 1 || upgrade.Target != 6 || upgrade.Pending != 5 || upgrade.Applied != 5 || upgrade.Skipped != 1 || upgrade.Result != "success" {
t.Fatalf("version-one upgrade summary = %+v", upgrade)
}
noOp := captureRunSummary(t, func() error {
return Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials})
})
- if noOp.Current != 5 || noOp.Target != 5 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 5 || noOp.Result != "success" {
+ if noOp.Current != 6 || noOp.Target != 6 || noOp.Pending != 0 || noOp.Applied != 0 || noOp.Skipped != 6 || noOp.Result != "success" {
t.Fatalf("post-upgrade no-op summary = %+v", noOp)
}
}
@@ -1537,8 +1578,8 @@ func migrationLedgerRows(t *testing.T, ctx context.Context, adminURL string) []l
if err := rows.Err(); err != nil {
t.Fatal("iterate migration ledger")
}
- if len(ledger) != 5 {
- t.Fatalf("migration ledger row count = %d, want 5", len(ledger))
+ if len(ledger) != 6 {
+ t.Fatalf("migration ledger row count = %d, want 6", len(ledger))
}
for index, want := range []struct {
version int64
@@ -1549,6 +1590,7 @@ func migrationLedgerRows(t *testing.T, ctx context.Context, adminURL string) []l
{3, "000003_usage_claimed_sandbox_pool.sql"},
{4, "000004_filter_invalid_usage_sandbox_events.sql"},
{5, "000005_hourly_reservation_meter.sql"},
+ {6, "000006_chat_conversations.sql"},
} {
if ledger[index].Version != want.version || ledger[index].ApplicationOrder != int64(index+1) || ledger[index].Filename != want.filename {
t.Fatalf("migration ledger row %d = version:%d order:%d filename:%q", index, ledger[index].Version, ledger[index].ApplicationOrder, ledger[index].Filename)
@@ -1787,6 +1829,7 @@ func assertOwnershipAndPublicACLs(t *testing.T, ctx context.Context, adminURL, m
defer connection.Close(ctx)
for _, want := range []struct{ schema, name, owner string }{
{"public", "github_trust_policies", migrationOwner},
+ {"public", "chat_conversations", migrationOwner},
{"k8s_state", "resource_state", "k8s_state_owner"},
{"k8s_state", "resource_event_outbox", "k8s_state_owner"},
{"k8s_state", "resource_event_outbox_usage_lookup_idx", "k8s_state_owner"},
@@ -1819,7 +1862,7 @@ func assertOwnershipAndPublicACLs(t *testing.T, ctx context.Context, adminURL, m
for _, schema := range []string{"k8s_state", "k8s_api", "k8s_reporting", "cyclops_migrations"} {
assertNoPublicSchemaPrivilege(t, ctx, connection, schema, "USAGE")
}
- for _, relation := range []string{"public.github_trust_policies", "k8s_state.resource_state", "k8s_state.resource_event_outbox", "k8s_api.current_resources", "k8s_reporting.current_resources", "cyclops_migrations.applied_migrations"} {
+ for _, relation := range []string{"public.github_trust_policies", "public.chat_conversations", "k8s_state.resource_state", "k8s_state.resource_event_outbox", "k8s_api.current_resources", "k8s_reporting.current_resources", "cyclops_migrations.applied_migrations"} {
assertNoPublicTablePrivilege(t, ctx, connection, relation)
}
}
@@ -1880,7 +1923,7 @@ func assertRuntimeLedgerAccess(t *testing.T, ctx context.Context, credentials Cr
var count int
err := connection.QueryRow(ctx, `select count(*) from cyclops_migrations.applied_migrations`).Scan(&count)
connection.Close(ctx)
- if err != nil || count != 5 {
+ if err != nil || count != 6 {
t.Errorf("%s ledger select = count:%d err:%v", role, count, err)
}
assertStatementFails(t, ctx, databaseURL, `insert into cyclops_migrations.applied_migrations (version, filename, sha256) values (99, 'invalid.sql', 'invalid')`)
diff --git a/libs/fleet/backend/database/migrator_test.go b/libs/fleet/backend/database/migrator_test.go
index ec7822dd98..cabd3d9ae3 100644
--- a/libs/fleet/backend/database/migrator_test.go
+++ b/libs/fleet/backend/database/migrator_test.go
@@ -520,8 +520,8 @@ func TestEmbeddedMigrationsAreOrderedAndImmutable(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if len(files) != 5 {
- t.Fatalf("expected exactly five migrations, got %d", len(files))
+ if len(files) != 6 {
+ t.Fatalf("expected exactly six migrations, got %d", len(files))
}
initial := files[0]
if initial.Version != 1 || initial.Name != "000001_initial_schema.sql" {
@@ -629,6 +629,22 @@ func TestEmbeddedMigrationsAreOrderedAndImmutable(t *testing.T) {
t.Errorf("reservation meter migration is missing contract %q", expected)
}
}
+
+ chatConversations := files[5]
+ if chatConversations.Version != 6 || chatConversations.Name != "000006_chat_conversations.sql" {
+ t.Fatalf("expected version 6 chat migration, got version=%d name=%q", chatConversations.Version, chatConversations.Name)
+ }
+ for _, expected := range []string{
+ "CREATE TABLE public.chat_conversations",
+ "messages jsonb NOT NULL DEFAULT '[]'::jsonb",
+ "CREATE INDEX chat_conversations_owner_active_idx",
+ "CREATE INDEX chat_conversations_owner_archived_idx",
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.chat_conversations TO cyclops_app",
+ } {
+ if !strings.Contains(chatConversations.SQL, expected) {
+ t.Errorf("chat conversation migration is missing contract %q", expected)
+ }
+ }
}
func TestReservationMeterMigrationRetainsRecordedChecksum(t *testing.T) {
diff --git a/libs/fleet/backend/main.go b/libs/fleet/backend/main.go
index a416d8bbb5..3df596cff4 100644
--- a/libs/fleet/backend/main.go
+++ b/libs/fleet/backend/main.go
@@ -358,6 +358,25 @@ func run() error {
} else {
slog.Info("usage: enabled")
}
+ if cfg.Chat.BaseURL != "" {
+ chatStorage := "memory"
+ if cfg.Database.URL != "" {
+ conversationStore, conversationStoreErr := chat.NewPostgresConversationStore(ctx, cfg.Database.URL)
+ if conversationStoreErr != nil {
+ return errors.Join(fmt.Errorf("initialize chat conversation store: %w", conversationStoreErr), startupErrors, telemetryErr)
+ }
+ defer conversationStore.Close()
+ h.Conversations = conversationStore
+ chatStorage = "postgres"
+ } else {
+ h.Conversations = chat.NewMemoryConversationStore()
+ }
+ h.Model = chat.NewLiteLLMClient(cfg.Chat.BaseURL, cfg.Chat.APIKey, cfg.Chat.Model)
+ slog.Info("chat: configured", "base_url", cfg.Chat.BaseURL, "model", cfg.Chat.Model, "storage", chatStorage)
+ } else {
+ slog.Info("chat: not configured (LITELLM_BASE_URL and LITELLM_API_KEY unset)")
+ }
+
managementStore, processLocalManagement, managementStoreErr := featureFlagManagementStoreFromEnv(os.Getenv)
if managementStoreErr != nil {
startupErrors = errors.Join(startupErrors, managementStoreErr)
@@ -390,14 +409,6 @@ func run() error {
progress := initializeDatabaseFeatures(ctx, cfg.Database, &h, databaseFeatures)
go retryDatabaseFeatures(ctx, cfg.Database, &h, databaseFeatures, progress, databaseRetryInterval)
- if cfg.Chat.BaseURL != "" {
- h.Conversations = chat.NewMemoryConversationStore()
- h.Model = chat.NewLiteLLMClient(cfg.Chat.BaseURL, cfg.Chat.APIKey, cfg.Chat.Model)
- slog.Info("chat: configured", "base_url", cfg.Chat.BaseURL, "model", cfg.Chat.Model)
- } else {
- slog.Info("chat: not configured (LITELLM_BASE_URL and LITELLM_API_KEY unset)")
- }
-
router := setupRouter(h)
srv := &http.Server{Addr: cfg.WebServer.Addr, Handler: router}
diff --git a/libs/fleet/docs/browser-agent.md b/libs/fleet/docs/browser-agent.md
index 8e16dada77..ae7f83a20a 100644
--- a/libs/fleet/docs/browser-agent.md
+++ b/libs/fleet/docs/browser-agent.md
@@ -82,9 +82,10 @@ middleware:
- `POST /api/chat/conversations/{id}/turns` appends user or tool messages and
streams the assistant response as server-sent events.
-The backend stores user, assistant, tool-call, and tool-result messages. The
-React UI keeps only transient rendering and run state; it reloads canonical
-conversation history from the backend. If a browser run is stopped or rejects a
+In production, the backend stores user, assistant, tool-call, and tool-result
+messages in the shared Cyclops Postgres database, so conversations survive
+replica changes and pod restarts. The React UI keeps only transient rendering
+and run state; it reloads canonical conversation history from the backend. If a browser run is stopped or rejects a
tool call after the assistant request is stored, the next retry or user prompt
records synthetic failed tool results before continuing, so the conversation is
not stranded.