Agent Executor (AX) is Google's open agentic orchestration runtime
Go
feat: Support Postgres as database on substrate. (#163)
Junjie Wang22a9daeddf3e
diff --git a/cmd/ax/internal/cliutil/cliutil_harness.go b/cmd/ax/internal/cliutil/cliutil_harness.goindex 6a02e3f..03814d1 100644--- a/cmd/ax/internal/cliutil/cliutil_harness.go+++ b/cmd/ax/internal/cliutil/cliutil_harness.go@@ -22,8 +22,8 @@ import ("os""github.com/google/ax/internal/config2"- "github.com/google/ax/internal/controller2/eventlog""github.com/google/ax/internal/controller2"+ "github.com/google/ax/internal/controller2/eventlog""github.com/google/ax/internal/harness")@@ -110,6 +110,13 @@ func NewControllerFromConfig(ctx context.Context, cfg *Config) (*controller2.Conreturn controller2.New(ctx, controller2.Config{Registry: reg,EventLogBuilder: func() (eventlog.EventLog, error) {+ if cfg.EventLog.PostgresConfig.DSN != "" {+ dsn := os.ExpandEnv(cfg.EventLog.PostgresConfig.DSN)+ if dsn == "" {+ return nil, fmt.Errorf("eventlog: postgres dsn %q expanded to empty", cfg.EventLog.PostgresConfig.DSN)+ }+ return eventlog.OpenPostgresEventLog(dsn)+ }return eventlog.OpenSQLiteEventLog(cfg.EventLog.SQLiteConfig.Filename)},})diff --git a/go.mod b/go.modindex 03f135e..ae743eb 100644--- a/go.mod+++ b/go.mod@@ -8,6 +8,7 @@ require (github.com/agent-substrate/substrate v0.0.0-20260622205654-fe93d160a1dfgithub.com/envoyproxy/go-control-plane/envoy v1.37.0github.com/google/uuid v1.6.0+ github.com/jackc/pgx/v5 v5.10.0github.com/spf13/cobra v1.10.2golang.org/x/sync v0.20.0google.golang.org/genai v1.43.0@@ -49,6 +50,9 @@ require (github.com/googleapis/gax-go/v2 v2.21.0 // indirectgithub.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // indirectgithub.com/inconshreveable/mousetrap v1.1.0 // indirect+ github.com/jackc/pgpassfile v1.0.0 // indirect+ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect+ github.com/jackc/puddle/v2 v2.2.2 // indirectgithub.com/lucasb-eyer/go-colorful v1.4.0 // indirectgithub.com/mattn/go-isatty v0.0.20 // indirectgithub.com/mattn/go-runewidth v0.0.23 // indirectdiff --git a/go.sum b/go.sumindex cd73761..1ecff24 100644--- a/go.sum+++ b/go.sum@@ -57,6 +57,7 @@ github.com/cncf/xds/go v0.0.0-20260202195803-dba9d589def2/go.mod h1:qwXFYgsP6T7Xgithub.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=@@ -92,6 +93,14 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFsgithub.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=+github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=+github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=+github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=+github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=+github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=+github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=+github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=+github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=@@ -110,6 +119,7 @@ github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFgithub.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo=github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8=+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=@@ -124,6 +134,9 @@ github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTgithub.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=@@ -179,6 +192,7 @@ google.golang.org/protobuf v1.36.12-0.20260120151049-f2248ac996af/go.mod h1:HTf+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis=diff --git a/internal/config2/config.go b/internal/config2/config.goindex 12eb5ab..753727c 100644--- a/internal/config2/config.go+++ b/internal/config2/config.go@@ -48,9 +48,15 @@ type SQLiteConfig struct {Filename string `yaml:"filename"` // SQLite file for event log storage}+// PostgresConfig configures the Postgres event log.+type PostgresConfig struct {+ DSN string `yaml:"dsn"` // Postgres connection DSN+}+// EventLogConfig configures the event log storage.type EventLogConfig struct {- SQLiteConfig SQLiteConfig `yaml:"sqlite"`+ SQLiteConfig SQLiteConfig `yaml:"sqlite,omitempty"`+ PostgresConfig PostgresConfig `yaml:"postgres,omitempty"`}// HarnessesConfig groups harnesses to serve by type. There are two categories:@@ -137,8 +143,8 @@ func (c *Config) Validate() error {if c.Server.Address == "" {return fmt.Errorf("server.address is required")}- if c.EventLog.SQLiteConfig.Filename == "" {- return fmt.Errorf("eventlog.sqlite.filename is required")+ if c.EventLog.PostgresConfig.DSN == "" && c.EventLog.SQLiteConfig.Filename == "" {+ return fmt.Errorf("eventlog requires either postgres.dsn or sqlite.filename")}var defaultCount intdiff --git a/internal/controller2/controller.go b/internal/controller2/controller.goindex cd92ac9..1e1b095 100644--- a/internal/controller2/controller.go+++ b/internal/controller2/controller.go@@ -118,7 +118,8 @@ type harnessHandler struct {func (a *harnessHandler) OnMessage(ctx context.Context, execID string, msg *proto.Message) error {// Log every response received from the harness// TODO(anj): The harness should send the full input sent to get this particular response.- if _, err := a.logger.LogOutputs(ctx, []*proto.Message{msg}, proto.State_STATE_PENDING); err != nil {+ seq, err := a.logger.LogOutputs(ctx, []*proto.Message{msg}, proto.State_STATE_PENDING)+ if err != nil {slog.WarnContext(ctx, "Failed to log streamed message to event log",slog.String("conversation_id", a.logger.conversationID),slog.Any("error", err),@@ -130,6 +131,7 @@ func (a *harnessHandler) OnMessage(ctx context.Context, execID string, msg *prot}return a.execHandler(&proto.ExecResponse{Outputs: []*proto.Message{msg},+ Seq: seq,})}diff --git a/internal/controller2/eventlog/postgres.go b/internal/controller2/eventlog/postgres.gonew file mode 100644index 0000000..2a93158--- /dev/null+++ b/internal/controller2/eventlog/postgres.go@@ -0,0 +1,72 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package eventlog++import (+ "context"+ "database/sql"+ "fmt"+ "time"++ _ "github.com/jackc/pgx/v5/stdlib"+)++// OpenPostgresEventLog connects to the PostgreSQL database described by dsn and+// initializes the event log schema. Caller is responsible to ensure it is safe+// for concurrent use.+func OpenPostgresEventLog(dsn string) (EventLog, error) {+ db, err := sql.Open("pgx", dsn)+ if err != nil {+ return nil, fmt.Errorf("postgres_eventlog: open: %w", err)+ }++ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)+ defer cancel()++ if err := db.PingContext(ctx); err != nil {+ db.Close()+ return nil, fmt.Errorf("postgres_eventlog: ping: %w", err)+ }++ // Create tables if they don't exist.+ if _, err := db.ExecContext(ctx, `+ CREATE TABLE IF NOT EXISTS conversation_log (+ conversation_id TEXT NOT NULL,+ seq INTEGER NOT NULL,+ payload TEXT NOT NULL,+ PRIMARY KEY (conversation_id, seq)+ )`); err != nil {+ db.Close()+ return nil, fmt.Errorf("postgres_eventlog: create conversation_log table: %w", err)+ }++ if _, err := db.ExecContext(ctx, `+ CREATE TABLE IF NOT EXISTS execution_log (+ exec_id TEXT NOT NULL,+ payload TEXT NOT NULL,+ timestamp TIMESTAMPTZ NOT NULL+ )`); err != nil {+ db.Close()+ return nil, fmt.Errorf("postgres_eventlog: create execution_log table: %w", err)+ }++ // Create indexes if they don't exist.+ if _, err := db.ExecContext(ctx, `CREATE INDEX IF NOT EXISTS idx_execution_log_exec_id ON execution_log(exec_id)`); err != nil {+ db.Close()+ return nil, fmt.Errorf("postgres_eventlog: create index exec_id: %w", err)+ }++ return &sqlEventLog{db: db}, nil+}diff --git a/internal/controller2/eventlog/sql.go b/internal/controller2/eventlog/sql.gonew file mode 100644index 0000000..88d3727--- /dev/null+++ b/internal/controller2/eventlog/sql.go@@ -0,0 +1,208 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package eventlog++import (+ "context"+ "database/sql"+ "fmt"+ "time"++ "github.com/google/ax/proto"+)++// sqlEventLog is a database backed EventLog shared by the SQLite and+// PostgreSQL implementations.+type sqlEventLog struct {+ db *sql.DB+}++// Append serializes the event to JSON and inserts it into the database.+func (l *sqlEventLog) Append(ctx context.Context, event *proto.ConversationEvent) (int32, error) {+ tx, err := l.db.BeginTx(ctx, nil)+ if err != nil {+ return 0, fmt.Errorf("eventlog: begin tx: %w", err)+ }+ defer tx.Rollback()++ seq := event.Seq+ if seq == 0 {+ if err := tx.QueryRowContext(ctx, "SELECT COALESCE(MAX(seq), 0) + 1 FROM conversation_log WHERE conversation_id = $1", event.ConversationId).Scan(&seq); err != nil {+ return 0, fmt.Errorf("eventlog: compute seq: %w", err)+ }+ event.Seq = seq+ }++ payload, err := marshalOpts.Marshal(event)+ if err != nil {+ return 0, fmt.Errorf("eventlog: marshal event: %w", err)+ }++ if _, err := tx.ExecContext(ctx,+ "INSERT INTO conversation_log (conversation_id, seq, payload) VALUES ($1, $2, $3)",+ event.ConversationId, event.Seq, string(payload)); err != nil {+ return 0, fmt.Errorf("eventlog: insert conversation: %w", err)+ }++ if err := tx.Commit(); err != nil {+ return 0, fmt.Errorf("eventlog: commit: %w", err)+ }++ return seq, nil+}++// AppendExec inserts an execution event into the database.+// TODO(anj): Remove execution_log table and AppendExec when legacy controller is removed.+func (l *sqlEventLog) AppendExec(ctx context.Context, event *proto.ExecutionEvent) error {+ payload, err := marshalOpts.Marshal(event)+ if err != nil {+ return fmt.Errorf("eventlog: marshal exec: %w", err)+ }++ var timestamp time.Time+ if event.Timestamp != nil {+ timestamp = event.Timestamp.AsTime()+ } else {+ timestamp = time.Now()+ }++ if _, err := l.db.ExecContext(ctx,+ "INSERT INTO execution_log (exec_id, payload, timestamp) VALUES ($1, $2, $3)",+ event.ExecId, string(payload), timestamp); err != nil {+ return fmt.Errorf("eventlog: insert exec: %w", err)+ }++ return nil+}++// Events retrieves all events from the database for a conversation, ordered by seq.+func (l *sqlEventLog) Events(ctx context.Context, conversationID string) ([]*proto.ConversationEvent, error) {+ rows, err := l.db.QueryContext(ctx, "SELECT payload FROM conversation_log WHERE conversation_id = $1 ORDER BY seq", conversationID)+ if err != nil {+ return nil, fmt.Errorf("eventlog: query conversation: %w", err)+ }+ defer rows.Close()++ var events []*proto.ConversationEvent+ for rows.Next() {+ var payload string+ if err := rows.Scan(&payload); err != nil {+ return nil, fmt.Errorf("eventlog: scan conversation: %w", err)+ }++ ev := &proto.ConversationEvent{}+ if err := unmarshalOpts.Unmarshal([]byte(payload), ev); err != nil {+ return nil, fmt.Errorf("eventlog: unmarshal event: %w", err)+ }+ events = append(events, ev)+ }++ if err := rows.Err(); err != nil {+ return nil, fmt.Errorf("eventlog: iterate conversation: %w", err)+ }++ return events, nil+}++// ExecEvents retrieves all events from the database for a specific execution ID.+func (l *sqlEventLog) ExecEvents(ctx context.Context, execID string) ([]*proto.ExecutionEvent, error) {+ rows, err := l.db.QueryContext(ctx, "SELECT payload FROM execution_log WHERE exec_id = $1 ORDER BY timestamp", execID)+ if err != nil {+ return nil, fmt.Errorf("eventlog: query exec: %w", err)+ }+ defer rows.Close()++ var events []*proto.ExecutionEvent+ for rows.Next() {+ var payload string+ if err := rows.Scan(&payload); err != nil {+ return nil, fmt.Errorf("eventlog: scan exec: %w", err)+ }++ ev := &proto.ExecutionEvent{}+ if err := unmarshalOpts.Unmarshal([]byte(payload), ev); err != nil {+ continue+ }+ events = append(events, ev)+ }++ if err := rows.Err(); err != nil {+ return nil, fmt.Errorf("eventlog: iterate exec: %w", err)+ }++ return events, nil+}++// DeleteAll deletes all events for a specific conversation ID and its child executions.+func (l *sqlEventLog) DeleteAll(ctx context.Context, conversationID string) error {+ tx, err := l.db.BeginTx(ctx, nil)+ if err != nil {+ return fmt.Errorf("eventlog: begin tx: %w", err)+ }+ defer tx.Rollback()++ // TODO(jbd): Update the schema to include conversation_id at every execution event.++ // Get all exec_ids for this conversation.+ rows, err := tx.QueryContext(ctx, "SELECT payload FROM conversation_log WHERE conversation_id = $1", conversationID)+ if err != nil {+ return fmt.Errorf("eventlog: query conversation: %w", err)+ }+ defer rows.Close()++ var execIDs []string+ for rows.Next() {+ var payload string+ if err := rows.Scan(&payload); err != nil {+ return fmt.Errorf("eventlog: scan conversation: %w", err)+ }++ ev := &proto.ConversationEvent{}+ if err := unmarshalOpts.Unmarshal([]byte(payload), ev); err != nil {+ return fmt.Errorf("eventlog: unmarshal event: %w", err)+ }+ if ev.ExecId != "" {+ execIDs = append(execIDs, ev.ExecId)+ }+ }++ if err := rows.Err(); err != nil {+ return fmt.Errorf("eventlog: iterate conversation: %w", err)+ }+ rows.Close()++ // Delete from execution_log.+ for _, execID := range execIDs {+ if _, err := tx.ExecContext(ctx, "DELETE FROM execution_log WHERE exec_id = $1", execID); err != nil {+ return fmt.Errorf("eventlog: delete exec %s: %w", execID, err)+ }+ }++ // Delete from conversation_log.+ if _, err := tx.ExecContext(ctx, "DELETE FROM conversation_log WHERE conversation_id = $1", conversationID); err != nil {+ return fmt.Errorf("eventlog: delete conversation: %w", err)+ }++ if err := tx.Commit(); err != nil {+ return fmt.Errorf("eventlog: commit tx: %w", err)+ }++ return nil+}++// Close releases the database connection.+func (l *sqlEventLog) Close() error {+ return l.db.Close()+}diff --git a/internal/controller2/eventlog/sql_test.go b/internal/controller2/eventlog/sql_test.gonew file mode 100644index 0000000..6717f74--- /dev/null+++ b/internal/controller2/eventlog/sql_test.go@@ -0,0 +1,238 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package eventlog++import (+ "context"+ "os"+ "path/filepath"+ "testing"++ "github.com/google/ax/proto"+ "google.golang.org/protobuf/types/known/timestamppb"+)++// testEventLog runs the EventLog contract against a backend. newLog returns a+// fresh, empty event log for each subtest, using the subtest's *testing.T for+// temp dirs and cleanup.+//+// Subtests derive their IDs from t.Name() (which includes the backend and+// subtest name) and clear them with DeleteAll before and after running, so they+// are safe against the shared Postgres database and harmless against SQLite's+// per-test file.+func testEventLog(t *testing.T, newLog func(t *testing.T) EventLog) {+ t.Run("AppendAndEvents", func(t *testing.T) {+ ctx := context.Background()+ log := newLog(t)++ conv := t.Name() + "-conv-1"+ task1 := t.Name() + "-task-1"+ task2 := t.Name() + "-task-2"+ _ = log.DeleteAll(ctx, conv)+ t.Cleanup(func() { _ = log.DeleteAll(ctx, conv) })++ // 1. Conversation log.+ cev1 := &proto.ConversationEvent{ConversationId: conv, Seq: 1, ExecId: task1}+ cev2 := &proto.ConversationEvent{ConversationId: conv, Seq: 2, ExecId: task2}+ if _, err := log.Append(ctx, cev1); err != nil {+ t.Fatalf("failed to append cev1: %v", err)+ }+ if _, err := log.Append(ctx, cev2); err != nil {+ t.Fatalf("failed to append cev2: %v", err)+ }++ cEvents, err := log.Events(ctx, conv)+ if err != nil {+ t.Fatalf("failed to read conversation events: %v", err)+ }+ if len(cEvents) != 2 {+ t.Fatalf("expected 2 conversation events, got %d", len(cEvents))+ }+ if cEvents[0].Seq != 1 || cEvents[1].Seq != 2 {+ t.Errorf("conversation events out of order: %d, %d", cEvents[0].Seq, cEvents[1].Seq)+ }+ if cEvents[0].ExecId != task1 || cEvents[1].ExecId != task2 {+ t.Errorf("conversation events mismatch: %q, %q", cEvents[0].ExecId, cEvents[1].ExecId)+ }++ // 2. Execution log.+ ee1 := &proto.ExecutionEvent{+ ExecId: task1,+ State: proto.State_STATE_PENDING,+ Timestamp: timestamppb.Now(),+ Inputs: []*proto.Message{+ {Role: "user", Content: &proto.Content{Type: &proto.Content_Text{Text: &proto.TextContent{Text: "hello"}}}},+ },+ }+ if err := log.AppendExec(ctx, ee1); err != nil {+ t.Fatalf("failed to append ee1: %v", err)+ }++ eEvents, err := log.ExecEvents(ctx, task1)+ if err != nil {+ t.Fatalf("failed to read execution events: %v", err)+ }+ if len(eEvents) != 1 {+ t.Fatalf("expected 1 execution event, got %d", len(eEvents))+ }+ if eEvents[0].ExecId != task1 || eEvents[0].State != proto.State_STATE_PENDING {+ t.Errorf("execution event mismatch: %+v", eEvents[0])+ }+ })++ t.Run("Empty", func(t *testing.T) {+ ctx := context.Background()+ log := newLog(t)++ events, err := log.Events(ctx, t.Name()+"-none")+ if err != nil {+ t.Fatalf("failed to read events: %v", err)+ }+ if len(events) != 0 {+ t.Fatalf("expected 0 events, got %d", len(events))+ }+ })++ t.Run("DeleteAll", func(t *testing.T) {+ ctx := context.Background()+ log := newLog(t)++ conv1 := t.Name() + "-conv-1"+ conv2 := t.Name() + "-conv-2"+ task1 := t.Name() + "-task-1"+ task3 := t.Name() + "-task-3"+ _ = log.DeleteAll(ctx, conv1)+ _ = log.DeleteAll(ctx, conv2)+ t.Cleanup(func() {+ _ = log.DeleteAll(ctx, conv1)+ _ = log.DeleteAll(ctx, conv2)+ })++ if _, err := log.Append(ctx, &proto.ConversationEvent{ConversationId: conv1, Seq: 1, ExecId: task1}); err != nil {+ t.Fatalf("append: %v", err)+ }+ if _, err := log.Append(ctx, &proto.ConversationEvent{ConversationId: conv2, Seq: 1, ExecId: task3}); err != nil {+ t.Fatalf("append: %v", err)+ }+ if err := log.AppendExec(ctx, &proto.ExecutionEvent{ExecId: task1, State: proto.State_STATE_PENDING}); err != nil {+ t.Fatalf("append exec: %v", err)+ }+ if err := log.AppendExec(ctx, &proto.ExecutionEvent{ExecId: task3, State: proto.State_STATE_PENDING}); err != nil {+ t.Fatalf("append exec: %v", err)+ }++ if err := log.DeleteAll(ctx, conv1); err != nil {+ t.Fatalf("failed to delete events: %v", err)+ }++ if ev, _ := log.Events(ctx, conv1); len(ev) != 0 {+ t.Errorf("expected 0 events for conv1, got %d", len(ev))+ }+ if ev, _ := log.Events(ctx, conv2); len(ev) != 1 {+ t.Errorf("expected 1 event for conv2, got %d", len(ev))+ }+ if ee, _ := log.ExecEvents(ctx, task1); len(ee) != 0 {+ t.Errorf("expected 0 exec events for task1, got %d", len(ee))+ }+ if ee, _ := log.ExecEvents(ctx, task3); len(ee) != 1 {+ t.Errorf("expected 1 exec event for task3, got %d", len(ee))+ }+ })++ // AutoSeq exercises the seq==0 auto-assignment path: appends with Seq unset+ // receive sequential numbers starting at 1.+ t.Run("AutoSeq", func(t *testing.T) {+ ctx := context.Background()+ log := newLog(t)++ conv := t.Name() + "-conv"+ _ = log.DeleteAll(ctx, conv)+ t.Cleanup(func() { _ = log.DeleteAll(ctx, conv) })++ const n = 3+ for i := int32(1); i <= n; i++ {+ seq, err := log.Append(ctx, &proto.ConversationEvent{ConversationId: conv, ExecId: "t"})+ if err != nil {+ t.Fatalf("auto-seq append failed: %v", err)+ }+ if seq != i {+ t.Errorf("expected seq %d, got %d", i, seq)+ }+ }++ events, err := log.Events(ctx, conv)+ if err != nil {+ t.Fatalf("failed to read events: %v", err)+ }+ if len(events) != n {+ t.Fatalf("expected %d events, got %d", n, len(events))+ }+ for i, e := range events {+ if e.Seq != int32(i+1) {+ t.Errorf("event %d: expected seq %d, got %d", i, i+1, e.Seq)+ }+ }+ })+}++// TestSQLiteEventLog runs the EventLog contract against the SQLite backend.+func TestSQLiteEventLog(t *testing.T) {+ testEventLog(t, func(t *testing.T) EventLog {+ t.Helper()+ log, err := OpenSQLiteEventLog(filepath.Join(t.TempDir(), "test.db"))+ if err != nil {+ t.Fatalf("failed to open sqlite event log: %v", err)+ }+ t.Cleanup(func() { log.Close() })+ return log+ })+}++// TestPostgresEventLog runs the EventLog contract against the Postgres backend+// described by PG_TEST_DSN, skipping when that variable is not set.+func TestPostgresEventLog(t *testing.T) {+ dsn := os.Getenv("PG_TEST_DSN")+ if dsn == "" {+ t.Skip("PG_TEST_DSN not set; skipping Postgres event log tests")+ }+ testEventLog(t, func(t *testing.T) EventLog {+ t.Helper()+ log, err := OpenPostgresEventLog(dsn)+ if err != nil {+ t.Fatalf("failed to open postgres event log: %v", err)+ }+ t.Cleanup(func() { log.Close() })+ return log+ })+}++// TestSQLiteEventLog_CreatesParentDirectory is SQLite-specific: OpenSQLiteEventLog+// must create the database file's parent directory if it does not exist.+func TestSQLiteEventLog_CreatesParentDirectory(t *testing.T) {+ dbPath := filepath.Join(t.TempDir(), "newdir", "test.db")++ log, err := OpenSQLiteEventLog(dbPath)+ if err != nil {+ t.Fatalf("failed to open sqlite event log and create directory: %v", err)+ }+ defer log.Close()++ if _, err := os.Stat(filepath.Dir(dbPath)); os.IsNotExist(err) {+ t.Fatalf("expected parent directory to be created, but it does not exist")+ }+ if _, err := os.Stat(dbPath); os.IsNotExist(err) {+ t.Fatalf("expected database file to be created, but it does not exist")+ }+}diff --git a/internal/controller2/eventlog/sqlite.go b/internal/controller2/eventlog/sqlite.goindex 2ac4b97..cefe92d 100644--- a/internal/controller2/eventlog/sqlite.go+++ b/internal/controller2/eventlog/sqlite.go@@ -15,39 +15,31 @@package eventlogimport (- "context""database/sql""fmt""os""path/filepath""time"- "github.com/google/ax/proto"_ "modernc.org/sqlite")-// SQLiteEventLog is a durable EventLog that persists events in a SQLite database.-// It is safe for concurrent use.-type SQLiteEventLog struct {- db *sql.DB-}-const sqliteBusyTimeout = 10 * time.Second// OpenSQLiteEventLog opens (or creates) a SQLite database at path and initializes the event log schema.-func OpenSQLiteEventLog(path string) (*SQLiteEventLog, error) {+func OpenSQLiteEventLog(path string) (EventLog, error) {dir := filepath.Dir(path)if err := os.MkdirAll(dir, 0755); err != nil {return nil, fmt.Errorf("sqlite_eventlog: mkdir %s: %w", dir, err)}- dsn := fmt.Sprintf("%s?_pragma=busy_timeout(%d)", path, sqliteBusyTimeout.Milliseconds())+ dsn := fmt.Sprintf("%s?_pragma=busy_timeout(%d)&_txlock=immediate", path, sqliteBusyTimeout.Milliseconds())db, err := sql.Open("sqlite", dsn)if err != nil {return nil, fmt.Errorf("sqlite_eventlog: open %s: %w", dsn, err)}- // Create tables if they don't exist+ // Create tables if they don't exist.if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS conversation_log (conversation_id TEXT NOT NULL,@@ -69,183 +61,11 @@ func OpenSQLiteEventLog(path string) (*SQLiteEventLog, error) {return nil, fmt.Errorf("sqlite_eventlog: create execution_log table: %w", err)}- // Create indexes if they don't exist+ // Create indexes if they don't exist.if _, err := db.Exec(`CREATE INDEX IF NOT EXISTS idx_execution_log_exec_id ON execution_log(exec_id)`); err != nil {db.Close()return nil, fmt.Errorf("sqlite_eventlog: create index exec_id: %w", err)}- return &SQLiteEventLog{db: db}, nil-}--// Append serializes the event to JSON and inserts it into the database.-func (l *SQLiteEventLog) Append(ctx context.Context, event *proto.ConversationEvent) (int32, error) {- seq := event.Seq- if seq == 0 {- err := l.db.QueryRowContext(ctx, "SELECT COALESCE(MAX(seq), 0) + 1 FROM conversation_log WHERE conversation_id = ?", event.ConversationId).Scan(&seq)- if err != nil {- return 0, fmt.Errorf("sqlite_eventlog: compute seq: %w", err)- }- event.Seq = seq- }-- payload, err := marshalOpts.Marshal(event)- if err != nil {- return 0, fmt.Errorf("sqlite_eventlog: marshal event: %w", err)- }-- _, err = l.db.ExecContext(ctx,- "INSERT INTO conversation_log (conversation_id, seq, payload) VALUES (?, ?, ?)",- event.ConversationId, event.Seq, string(payload))-- if err != nil {- return 0, fmt.Errorf("sqlite_eventlog: insert conversation: %w", err)- }-- return seq, nil-}--// AppendExec inserts an execution event into the database.-// TODO(anj): Remove execution_log table and AppendExec when legacy controller is removed.-func (l *SQLiteEventLog) AppendExec(ctx context.Context, event *proto.ExecutionEvent) error {- payload, err := marshalOpts.Marshal(event)- if err != nil {- return fmt.Errorf("sqlite_eventlog: marshal exec: %w", err)- }-- var timestamp time.Time- if event.Timestamp != nil {- timestamp = event.Timestamp.AsTime()- } else {- timestamp = time.Now()- }-- _, err = l.db.ExecContext(ctx,- "INSERT INTO execution_log (exec_id, payload, timestamp) VALUES (?, ?, ?)",- event.ExecId, string(payload), timestamp)-- if err != nil {- return fmt.Errorf("sqlite_eventlog: insert exec: %w", err)- }-- return nil-}--// Events retrieves all events from the database for a conversation, ordered by seq and execution order.-func (l *SQLiteEventLog) Events(ctx context.Context, conversationID string) ([]*proto.ConversationEvent, error) {- rows, err := l.db.QueryContext(ctx, "SELECT payload FROM conversation_log WHERE conversation_id = ? ORDER BY seq", conversationID)- if err != nil {- return nil, fmt.Errorf("sqlite_eventlog: query conversation: %w", err)- }- defer rows.Close()-- var events []*proto.ConversationEvent- for rows.Next() {- var payload string- if err := rows.Scan(&payload); err != nil {- return nil, fmt.Errorf("sqlite_eventlog: scan conversation: %w", err)- }-- ev := &proto.ConversationEvent{}- if err := unmarshalOpts.Unmarshal([]byte(payload), ev); err != nil {- return nil, fmt.Errorf("sqlite_eventlog: unmarshal event: %w", err)- }- events = append(events, ev)- }-- if err := rows.Err(); err != nil {- return nil, fmt.Errorf("sqlite_eventlog: iterate conversation: %w", err)- }-- return events, nil-}--// ExecEvents retrieves all events from the database for a specific execution ID.-func (l *SQLiteEventLog) ExecEvents(ctx context.Context, execID string) ([]*proto.ExecutionEvent, error) {- rows, err := l.db.QueryContext(ctx, "SELECT payload FROM execution_log WHERE exec_id = ? ORDER BY timestamp", execID)- if err != nil {- return nil, fmt.Errorf("sqlite_eventlog: query exec: %w", err)- }- defer rows.Close()-- var events []*proto.ExecutionEvent- for rows.Next() {- var payload string- if err := rows.Scan(&payload); err != nil {- return nil, fmt.Errorf("sqlite_eventlog: scan exec: %w", err)- }-- ev := &proto.ExecutionEvent{}- if err := unmarshalOpts.Unmarshal([]byte(payload), ev); err != nil {- continue- }- events = append(events, ev)- }-- if err := rows.Err(); err != nil {- return nil, fmt.Errorf("sqlite_eventlog: iterate exec: %w", err)- }-- return events, nil-}--// DeleteAll deletes all events for a specific conversation ID and its child executions.-func (l *SQLiteEventLog) DeleteAll(ctx context.Context, conversationID string) error {- tx, err := l.db.BeginTx(ctx, nil)- if err != nil {- return fmt.Errorf("sqlite_eventlog: begin tx: %w", err)- }- defer tx.Rollback()-- // TODO(jbd): Update the schema to include conversation_id at every execution event.-- // Get all exec_ids for this conversation- rows, err := tx.QueryContext(ctx, "SELECT payload FROM conversation_log WHERE conversation_id = ?", conversationID)- if err != nil {- return fmt.Errorf("sqlite_eventlog: query conversation: %w", err)- }- defer rows.Close()-- var execIDs []string- for rows.Next() {- var payload string- if err := rows.Scan(&payload); err != nil {- return fmt.Errorf("sqlite_eventlog: scan conversation: %w", err)- }-- ev := &proto.ConversationEvent{}- if err := unmarshalOpts.Unmarshal([]byte(payload), ev); err != nil {- return fmt.Errorf("sqlite_eventlog: unmarshal event: %w", err)- }- if ev.ExecId != "" {- execIDs = append(execIDs, ev.ExecId)- }- }-- if err := rows.Err(); err != nil {- return fmt.Errorf("sqlite_eventlog: iterate conversation: %w", err)- }-- // Delete from execution_log- for _, execID := range execIDs {- if _, err := tx.ExecContext(ctx, "DELETE FROM execution_log WHERE exec_id = ?", execID); err != nil {- return fmt.Errorf("sqlite_eventlog: delete exec %s: %w", execID, err)- }- }-- // Delete from conversation_log- if _, err := tx.ExecContext(ctx, "DELETE FROM conversation_log WHERE conversation_id = ?", conversationID); err != nil {- return fmt.Errorf("sqlite_eventlog: delete conversation: %w", err)- }-- if err := tx.Commit(); err != nil {- return fmt.Errorf("sqlite_eventlog: commit tx: %w", err)- }-- return nil-}--// Close releases the database connection.-func (l *SQLiteEventLog) Close() error {- return l.db.Close()+ return &sqlEventLog{db: db}, nil}diff --git a/internal/controller2/eventlog/sqlite_test.go b/internal/controller2/eventlog/sqlite_test.godeleted file mode 100644index 3fd8085..0000000--- a/internal/controller2/eventlog/sqlite_test.go+++ /dev/null@@ -1,256 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package eventlog--import (- "context"- "os"- "path/filepath"- "sync"- "testing"-- "github.com/google/ax/proto"- "google.golang.org/protobuf/types/known/timestamppb"-)--func TestSQLiteEventLog_AppendAndEvents(t *testing.T) {- ctx := context.Background()- dbPath := filepath.Join(t.TempDir(), "test.db")-- log, err := OpenSQLiteEventLog(dbPath)- if err != nil {- t.Fatalf("failed to open sqlite event log: %v", err)- }- defer log.Close()-- // 1. Test Conversation Log- cev1 := &proto.ConversationEvent{- ConversationId: "conv-1",- Seq: 1,- ExecId: "task-1",- }-- cev2 := &proto.ConversationEvent{- ConversationId: "conv-1",- Seq: 2,- ExecId: "task-2",- }-- if _, err := log.Append(ctx, cev1); err != nil {- t.Fatalf("failed to append cev1: %v", err)- }- if _, err := log.Append(ctx, cev2); err != nil {- t.Fatalf("failed to append cev2: %v", err)- }-- cEvents, err := log.Events(ctx, "conv-1")- if err != nil {- t.Fatalf("failed to read conversation events: %v", err)- }-- if len(cEvents) != 2 {- t.Fatalf("expected 2 conversation events, got %d", len(cEvents))- }-- if cEvents[0].ExecId != "task-1" || cEvents[1].ExecId != "task-2" {- t.Errorf("conversation events mismatch")- }-- // 2. Test Execution Log- ee1 := &proto.ExecutionEvent{- ExecId: "task-1",- State: proto.State_STATE_PENDING,- Timestamp: timestamppb.Now(),- Inputs: []*proto.Message{- {Role: "user", Content: &proto.Content{Type: &proto.Content_Text{Text: &proto.TextContent{Text: "hello"}}}},- },- }-- if err := log.AppendExec(ctx, ee1); err != nil {- t.Fatalf("failed to append ee1: %v", err)- }-- eEvents, err := log.ExecEvents(ctx, "task-1")- if err != nil {- t.Fatalf("failed to read execution events: %v", err)- }-- if len(eEvents) != 1 {- t.Fatalf("expected 1 execution event, got %d", len(eEvents))- }-- if eEvents[0].ExecId != "task-1" || eEvents[0].State != proto.State_STATE_PENDING {- t.Errorf("execution event mismatch")- }-}--func TestSQLiteEventLog_ConcurrentAppend(t *testing.T) {- ctx := context.Background()- dbPath := filepath.Join(t.TempDir(), "test.db")-- log, err := OpenSQLiteEventLog(dbPath)- if err != nil {- t.Fatalf("failed to open sqlite event log: %v", err)- }- defer log.Close()-- var wg sync.WaitGroup- numRoutines := 10- numEvents := 100-- for i := range numRoutines {- wg.Add(1)- go func(agentIdx int) {- defer wg.Done()- for i := range numEvents {- ev := &proto.ConversationEvent{- ConversationId: "conv-concurrent",- Seq: int32(agentIdx*numEvents + i + 1),- ExecId: "task-concurrent",- }- if _, err := log.Append(ctx, ev); err != nil {- t.Errorf("concurrent append failed: %v", err)- }- }- }(i)- }-- wg.Wait()-- events, err := log.Events(ctx, "conv-concurrent")- if err != nil {- t.Fatalf("failed to read events: %v", err)- }-- if len(events) != numRoutines*numEvents {- t.Fatalf("expected %d events, got %d", numRoutines*numEvents, len(events))- }-}--func TestSQLiteEventLog_Empty(t *testing.T) {- ctx := context.Background()- dbPath := filepath.Join(t.TempDir(), "test.db")-- log, err := OpenSQLiteEventLog(dbPath)- if err != nil {- t.Fatalf("failed to open sqlite event log: %v", err)- }- defer log.Close()-- events, err := log.Events(ctx, "conv-1")- if err != nil {- t.Fatalf("failed to read events: %v", err)- }-- if len(events) != 0 {- t.Fatalf("expected 0 events, got %d", len(events))- }-}--func TestSQLiteEventLog_DeleteEvents(t *testing.T) {- ctx := context.Background()- dbPath := filepath.Join(t.TempDir(), "test.db")-- log, err := OpenSQLiteEventLog(dbPath)- if err != nil {- t.Fatalf("failed to open sqlite event log: %v", err)- }- defer log.Close()-- // Setup data- cev1 := &proto.ConversationEvent{ConversationId: "conv-1", Seq: 1, ExecId: "task-1"}- cev2 := &proto.ConversationEvent{ConversationId: "conv-1", Seq: 2, ExecId: "task-2"}- cev3 := &proto.ConversationEvent{ConversationId: "conv-2", Seq: 1, ExecId: "task-3"}-- log.Append(ctx, cev1)- log.Append(ctx, cev2)- log.Append(ctx, cev3)-- ee1 := &proto.ExecutionEvent{ExecId: "task-1", State: proto.State_STATE_PENDING}- ee2 := &proto.ExecutionEvent{ExecId: "task-2", State: proto.State_STATE_COMPLETED}- ee3 := &proto.ExecutionEvent{ExecId: "task-3", State: proto.State_STATE_PENDING}-- log.AppendExec(ctx, ee1)- log.AppendExec(ctx, ee2)- log.AppendExec(ctx, ee3)-- // Delete conv-1- if err := log.DeleteAll(ctx, "conv-1"); err != nil {- t.Fatalf("failed to delete events: %v", err)- }-- // Verify conversation events- events, err := log.Events(ctx, "conv-1")- if err != nil {- t.Fatalf("failed to read events: %v", err)- }- if len(events) != 0 {- t.Errorf("expected 0 events for conv-1, got %d", len(events))- }-- events, err = log.Events(ctx, "conv-2")- if err != nil {- t.Fatalf("failed to read events: %v", err)- }- if len(events) != 1 {- t.Errorf("expected 1 event for conv-2, got %d", len(events))- }-- // Verify execution events- eEvents, err := log.ExecEvents(ctx, "task-1")- if err != nil {- t.Fatalf("failed to read exec events: %v", err)- }- if len(eEvents) != 0 {- t.Errorf("expected 0 exec events for task-1, got %d", len(eEvents))- }-- eEvents, err = log.ExecEvents(ctx, "task-2")- if err != nil {- t.Fatalf("failed to read exec events: %v", err)- }- if len(eEvents) != 0 {- t.Errorf("expected 0 exec events for task-2, got %d", len(eEvents))- }-- eEvents, err = log.ExecEvents(ctx, "task-3")- if err != nil {- t.Fatalf("failed to read exec events: %v", err)- }- if len(eEvents) != 1 {- t.Errorf("expected 1 exec event for task-3, got %d", len(eEvents))- }-}--func TestSQLiteEventLog_CreatesParentDirectory(t *testing.T) {- // Create a path with a non-existent parent directory- dbPath := filepath.Join(t.TempDir(), "newdir", "test.db")-- log, err := OpenSQLiteEventLog(dbPath)- if err != nil {- t.Fatalf("failed to open sqlite event log and create directory: %v", err)- }- defer log.Close()-- // Verify that the parent directory actually exists- if _, err := os.Stat(filepath.Dir(dbPath)); os.IsNotExist(err) {- t.Fatalf("expected parent directory to be created, but it does not exist")- }-- // Verify that the database file was created- if _, err := os.Stat(dbPath); os.IsNotExist(err) {- t.Fatalf("expected database file to be created, but it does not exist")- }-}diff --git a/internal/hack/install-ax.sh b/internal/hack/install-ax.shindex 31a15d3..4cf59a3 100755--- a/internal/hack/install-ax.sh+++ b/internal/hack/install-ax.sh@@ -72,7 +72,7 @@ function usage() {echo ""echo "Options:"echo " --deploy-ax-server Build images and deploy AX server and components"- echo " --delete-ax-server Delete AX server and components from cluster"+ echo " --delete-ax-server Delete AX server and components, preserving the event-log database"echo " -h, --help Show this help message"}@@ -204,11 +204,22 @@ deploy_ax_server() {ax_image=$(build_ax_image)ateom_image=$(build_ateom_image)+ # Resolve a stable Postgres password for the event log.+ local pg_password="${POSTGRES_PASSWORD:-}"+ local existing_pw+ existing_pw="$(run_kubectl -n ax get secret ax-eventlog-postgres -o go-template='{{.data.password | base64decode}}' 2>/dev/null || true)"+ if [[ -n "${existing_pw}" ]]; then+ pg_password="${existing_pw}"+ elif [[ -z "${pg_password}" ]]; then+ pg_password="$(openssl rand -hex 16)"+ fi+# Render the manifest and apply it.if ! sed -e "s|\${GEMINI_API_KEY}|${GEMINI_API_KEY}|g" \-e "s|\${BUCKET_NAME}|${BUCKET_NAME}|g" \-e "s|\${AX_IMAGE}|${ax_image}|g" \-e "s|\${ATEOM_IMAGE}|${ateom_image}|g" \+ -e "s|\${POSTGRES_PASSWORD}|${pg_password}|g" \internal/manifests/ax-deployment2.yaml \| run_kubectl apply -f -; thenecho >&2@@ -218,6 +229,12 @@ deploy_ax_server() {exit 1fi+ # Wait for the event-log Postgres to be ready before ax-server relies on it.+ log_step "wait for statefulset/ax-eventlog-postgres to be ready"+ wait_with_spinner "waiting for postgres (timeout ${AX_WAIT_TIMEOUT:-5m})" \+ run_kubectl -n ax rollout status statefulset/ax-eventlog-postgres \+ --timeout="${AX_WAIT_TIMEOUT:-5m}"+# Wait for the antigravity ActorTemplate's golden snapshot to be ready.log_step "wait for actortemplate/ax-harness-template to be Ready"wait_with_spinner "waiting for golden snapshot (timeout ${AX_WAIT_TIMEOUT:-5m})" \@@ -225,16 +242,18 @@ deploy_ax_server() {-n ax --timeout="${AX_WAIT_TIMEOUT:-5m}"}+# delete_ax_server removes the AX server and harness resources but preserves the+# event-log database: it leaves the namespace and the Postgres subsystem+# (Service/Secret/StatefulSet and its PVC) intact so a later redeploy reuses the+# existing data.delete_ax_server() {log_step "delete_ax_server"- # Delete resources using dummy values so credentials aren't required for deletion- sed -e "s|\${GEMINI_API_KEY}|dummy-key|g" \- -e "s|\${BUCKET_NAME}|dummy-bucket|g" \- -e "s|\${AX_IMAGE}|dummy-image|g" \- -e "s|\${ATEOM_IMAGE}|dummy-image|g" \- internal/manifests/ax-deployment2.yaml \- | run_kubectl delete --ignore-not-found -f -+ run_kubectl -n ax delete --ignore-not-found \+ replicaset/ax-server \+ configmap/ax-server-config \+ actortemplate/ax-harness-template \+ workerpool/ax-harness-workerpool}if [ "$#" -eq 0 ]; thendiff --git a/internal/manifests/README.md b/internal/manifests/README.mdindex de98305..4404a08 100644--- a/internal/manifests/README.md+++ b/internal/manifests/README.md@@ -120,12 +120,21 @@ The weather in New York is sunny with a temperature of 25 degrees Celsius (77 de## 🧹 How to Uninstall-To remove AX resources from your cluster, run:+To remove the AX server and its components, run:```bash./internal/hack/install-ax.sh --delete-ax-server```+> [!NOTE]+> The event-log database is preserved by default. If you want to+> delete everything including the data, after the command above, be careful and+> run:+>+> ```bash+> kubectl delete namespace ax+> ```+---## 🛠️ Inspection & Diagnosticsdiff --git a/internal/manifests/ax-deployment2.yaml b/internal/manifests/ax-deployment2.yamlindex af24a4d..e3ca367 100644--- a/internal/manifests/ax-deployment2.yaml+++ b/internal/manifests/ax-deployment2.yaml@@ -68,6 +68,86 @@ spec:location: gs://${BUCKET_NAME}/antigravity/---+# ---------------------------------------------------------------------------+# Event log storage (PostgreSQL)+# ---------------------------------------------------------------------------+apiVersion: v1+kind: Service+metadata:+ name: ax-eventlog-postgres+ namespace: ax+ labels:+ app: ax-eventlog-postgres+spec:+ selector:+ app: ax-eventlog-postgres+ ports:+ - port: 5432+ targetPort: 5432+---+apiVersion: v1+kind: Secret+metadata:+ name: ax-eventlog-postgres+ namespace: ax+type: Opaque+stringData:+ password: "${POSTGRES_PASSWORD}"+ dsn: "postgres://axuser:${POSTGRES_PASSWORD}@ax-eventlog-postgres.ax.svc:5432/axeventlog?sslmode=disable"+---+apiVersion: apps/v1+kind: StatefulSet+metadata:+ name: ax-eventlog-postgres+ namespace: ax+ labels:+ app: ax-eventlog-postgres+spec:+ serviceName: ax-eventlog-postgres+ replicas: 1+ selector:+ matchLabels:+ app: ax-eventlog-postgres+ template:+ metadata:+ labels:+ app: ax-eventlog-postgres+ spec:+ containers:+ - name: postgres+ image: postgres:16+ ports:+ - containerPort: 5432+ env:+ - name: POSTGRES_DB+ value: axeventlog+ - name: POSTGRES_USER+ value: axuser+ - name: POSTGRES_PASSWORD+ valueFrom:+ secretKeyRef:+ name: ax-eventlog-postgres+ key: password+ - name: PGDATA+ value: /var/lib/postgresql/data/pgdata+ volumeMounts:+ - name: data+ mountPath: /var/lib/postgresql/data+ readinessProbe:+ exec:+ command: ["pg_isready", "-U", "axuser", "-d", "axeventlog"]+ initialDelaySeconds: 5+ periodSeconds: 5+ volumeClaimTemplates:+ - metadata:+ name: data+ spec:+ accessModes: ["ReadWriteOnce"]+ resources:+ requests:+ storage: 10Gi+---+# ---------------------------------------------------------------------------# AX Server# ---------------------------------------------------------------------------@@ -99,6 +179,11 @@ spec:value: "${GEMINI_API_KEY}"- name: AX_SUBSTRATEvalue: "1"+ - name: AX_EVENTLOG_DSN+ valueFrom:+ secretKeyRef:+ name: ax-eventlog-postgres+ key: dsnvolumeMounts:- name: ax-configmountPath: /etc/ax@@ -114,6 +199,9 @@ metadata:namespace: axdata:ax.yaml: |+ eventlog:+ postgres:+ dsn: ${AX_EVENTLOG_DSN}harnesses:antigravity:default: true