diff --git a/internal/store/claims.go b/internal/store/claims.go index fbd37d38..79a092b7 100644 --- a/internal/store/claims.go +++ b/internal/store/claims.go @@ -92,7 +92,7 @@ func (s *SQLite) ConsumeClaim(ctx context.Context, token string) (string, error) hash := HashKey(token) now := time.Now() - tx, err := s.db.BeginTx(ctx, nil) + q, tx, err := s.begin(ctx) if err != nil { return "", err } @@ -100,7 +100,7 @@ func (s *SQLite) ConsumeClaim(ctx context.Context, token string) (string, error) var sealed []byte var expires int64 - err = tx.QueryRowContext(ctx, + err = q.QueryRowContext(ctx, `SELECT sealed, expires_at FROM key_claims WHERE token_hash = ? AND claimed_at IS NULL`, hash).Scan(&sealed, &expires) if errors.Is(err, sql.ErrNoRows) { @@ -120,7 +120,7 @@ func (s *SQLite) ConsumeClaim(ctx context.Context, token string) (string, error) // Blank the sealed value as well as marking it claimed, so a used link // leaves nothing behind even before the row is purged. - res, err := tx.ExecContext(ctx, + res, err := q.ExecContext(ctx, `UPDATE key_claims SET claimed_at = ?, sealed = x'' WHERE token_hash = ? AND claimed_at IS NULL`, millis(now), hash) if err != nil { diff --git a/internal/store/claims_test.go b/internal/store/claims_test.go index 3bf12fc6..b9b64b95 100644 --- a/internal/store/claims_test.go +++ b/internal/store/claims_test.go @@ -67,7 +67,7 @@ func TestTheDatabaseNeverHoldsAUsableKey(t *testing.T) { s, token, secret := claimFixture(t) var sealed []byte - if err := s.db.QueryRow(`SELECT sealed FROM key_claims`).Scan(&sealed); err != nil { + if err := s.db.QueryRowContext(context.Background(), `SELECT sealed FROM key_claims`).Scan(&sealed); err != nil { t.Fatal(err) } if len(sealed) == 0 { @@ -79,7 +79,7 @@ func TestTheDatabaseNeverHoldsAUsableKey(t *testing.T) { // And the token itself is not there either, so nothing in the database can // derive the key that opens it. var count int - if err := s.db.QueryRow(`SELECT COUNT(*) FROM key_claims WHERE token_hash = ?`, []byte(token)). + if err := s.db.QueryRowContext(context.Background(), `SELECT COUNT(*) FROM key_claims WHERE token_hash = ?`, []byte(token)). Scan(&count); err != nil { t.Fatal(err) } @@ -98,7 +98,7 @@ func TestAClaimedLinkLeavesNothingBehind(t *testing.T) { t.Fatal(err) } var sealed []byte - if err := s.db.QueryRow(`SELECT sealed FROM key_claims`).Scan(&sealed); err != nil { + if err := s.db.QueryRowContext(context.Background(), `SELECT sealed FROM key_claims`).Scan(&sealed); err != nil { t.Fatal(err) } if len(sealed) != 0 { @@ -149,7 +149,7 @@ func TestAnExpiredLinkIsRefused(t *testing.T) { // Age it directly. CreateClaim treats a non-positive TTL as "use the // default", which is the right thing for a caller and no way to build the // row this test needs. - if _, err := s.db.Exec(`UPDATE key_claims SET expires_at = ?`, + if _, err := s.db.ExecContext(context.Background(), `UPDATE key_claims SET expires_at = ?`, time.Now().Add(-time.Hour).UnixMilli()); err != nil { t.Fatal(err) } diff --git a/internal/store/dialect.go b/internal/store/dialect.go new file mode 100644 index 00000000..6e32b19e --- /dev/null +++ b/internal/store/dialect.go @@ -0,0 +1,103 @@ +package store + +import ( + "context" + "database/sql" + "strconv" + "strings" +) + +// Dialect is which SQL database is underneath. +// +// There is one set of queries, not one per database, because the SQL this +// store writes is almost entirely portable — the only construct that genuinely +// differs is the placeholder, and `ON CONFLICT` upserts work in both. Two +// copies of every query would drift, and the drift would be discovered one +// query at a time in production. +type Dialect string + +// The supported databases. +const ( + SQLiteDialect Dialect = "sqlite" + PostgresDialect Dialect = "postgres" +) + +// rebind rewrites `?` placeholders for the dialect in use. +// +// SQLite takes `?`, PostgreSQL takes `$1`, `$2` and so on. Queries are written +// with `?` because that is what the majority of them were written with +// already, and rewriting is cheaper and safer than maintaining two spellings +// of each. +// +// A `?` inside a string literal would be rewritten too, which would be a bug — +// so this skips over quoted sections rather than scanning blindly. No query +// here contains one today; this exists so that adding one is not a silent +// corruption. +func (d Dialect) rebind(query string) string { + if d != PostgresDialect || !strings.Contains(query, "?") { + return query + } + var b strings.Builder + b.Grow(len(query) + 8) + + n := 0 + inSingle, inDouble := false, false + for i := 0; i < len(query); i++ { + c := query[i] + switch { + case c == '\'' && !inDouble: + inSingle = !inSingle + case c == '"' && !inSingle: + inDouble = !inDouble + case c == '?' && !inSingle && !inDouble: + n++ + b.WriteByte('$') + b.WriteString(strconv.Itoa(n)) + continue + } + b.WriteByte(c) + } + return b.String() +} + +// conn is the part of *sql.DB and *sql.Tx this store uses. +// +// Named so the same query helpers work inside and outside a transaction, and +// so every path goes through the rebinding wrapper rather than some of them +// reaching the driver directly with the wrong placeholders. +type conn interface { + ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) + QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) + QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row + // PrepareContext is here because a prepared statement carries the query + // with it: rebinding at Exec time would be too late, the placeholders are + // fixed when it is prepared. + PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) +} + +// bound is a conn that rewrites placeholders on the way through. +// +// A wrapper rather than a change at each call site: there are sixty of those, +// and sixty chances to forget one. Here the only way to reach the driver is +// through the rewrite. +type bound struct { + c Dialect + // to is the underlying *sql.DB or *sql.Tx. + to conn +} + +func (b bound) ExecContext(ctx context.Context, q string, args ...any) (sql.Result, error) { + return b.to.ExecContext(ctx, b.c.rebind(q), args...) +} + +func (b bound) QueryContext(ctx context.Context, q string, args ...any) (*sql.Rows, error) { + return b.to.QueryContext(ctx, b.c.rebind(q), args...) +} + +func (b bound) QueryRowContext(ctx context.Context, q string, args ...any) *sql.Row { + return b.to.QueryRowContext(ctx, b.c.rebind(q), args...) +} + +func (b bound) PrepareContext(ctx context.Context, q string) (*sql.Stmt, error) { + return b.to.PrepareContext(ctx, b.c.rebind(q)) +} diff --git a/internal/store/dialect_test.go b/internal/store/dialect_test.go new file mode 100644 index 00000000..a15af61d --- /dev/null +++ b/internal/store/dialect_test.go @@ -0,0 +1,44 @@ +package store + +import "testing" + +func TestSQLiteKeepsItsPlaceholders(t *testing.T) { + q := "SELECT a FROM t WHERE b = ? AND c = ?" + if got := SQLiteDialect.rebind(q); got != q { + t.Errorf("sqlite query was rewritten: %s", got) + } +} + +func TestPostgresGetsNumberedPlaceholders(t *testing.T) { + got := PostgresDialect.rebind("SELECT a FROM t WHERE b = ? AND c = ?") + want := "SELECT a FROM t WHERE b = $1 AND c = $2" + if got != want { + t.Errorf("rebind = %q, want %q", got, want) + } +} + +func TestAQuestionMarkInsideALiteralIsLeftAlone(t *testing.T) { + // No query here contains one today. This exists so that adding one is a + // test failure rather than a query that silently reads the wrong argument + // — or, worse, one that still parses and returns the wrong rows. + got := PostgresDialect.rebind(`SELECT a FROM t WHERE name = 'why?' AND b = ?`) + want := `SELECT a FROM t WHERE name = 'why?' AND b = $1` + if got != want { + t.Errorf("rebind = %q, want %q", got, want) + } +} + +func TestDoubleQuotedIdentifiersAreAlsoSkipped(t *testing.T) { + got := PostgresDialect.rebind(`SELECT "odd?name" FROM t WHERE b = ?`) + want := `SELECT "odd?name" FROM t WHERE b = $1` + if got != want { + t.Errorf("rebind = %q, want %q", got, want) + } +} + +func TestAQueryWithNoPlaceholdersIsUntouched(t *testing.T) { + q := "DELETE FROM t" + if got := PostgresDialect.rebind(q); got != q { + t.Errorf("rebind = %q", got) + } +} diff --git a/internal/store/memory.go b/internal/store/memory.go index cc00bc7c..68a40df6 100644 --- a/internal/store/memory.go +++ b/internal/store/memory.go @@ -138,14 +138,14 @@ func (s *SQLite) PutNote(ctx context.Context, projectID, agentID string, n Note) return ErrMemoryNotEnabled } - tx, err := s.db.BeginTx(ctx, nil) + q, tx, err := s.begin(ctx) if err != nil { return err } defer func() { _ = tx.Rollback() }() now := time.Now().UnixMilli() - res, err := tx.ExecContext(ctx, + res, err := q.ExecContext(ctx, `UPDATE memory_notes SET text = ?, tags = ?, updated_at = ? WHERE project_id = ? AND agent_id = ? AND key = ?`, n.Text, tags, now, projectID, agentID, n.Key) @@ -154,7 +154,7 @@ func (s *SQLite) PutNote(ctx context.Context, projectID, agentID string, n Note) } if updated, _ := res.RowsAffected(); updated == 0 { var count int - if err := tx.QueryRowContext(ctx, + if err := q.QueryRowContext(ctx, `SELECT COUNT(*) FROM memory_notes WHERE project_id = ? AND agent_id = ?`, projectID, agentID).Scan(&count); err != nil { return err @@ -162,7 +162,7 @@ func (s *SQLite) PutNote(ctx context.Context, projectID, agentID string, n Note) if count >= MaxNotesPerAgent { return ErrMemoryFull } - if _, err := tx.ExecContext(ctx, + if _, err := q.ExecContext(ctx, `INSERT INTO memory_notes (project_id, agent_id, key, text, tags, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)`, projectID, agentID, n.Key, n.Text, tags, now, now); err != nil { diff --git a/internal/store/sqlite.go b/internal/store/sqlite.go index 30b56209..2ac85a87 100644 --- a/internal/store/sqlite.go +++ b/internal/store/sqlite.go @@ -17,7 +17,14 @@ import ( // SQLite is the single-instance store. type SQLite struct { - db *sql.DB + // raw is for the things that are not queries: transactions, pings, close. + raw *sql.DB + dialect Dialect + // db is the same handle with placeholders rewritten for the dialect. Every + // query goes through it, which is the point — a call site that reached the + // driver directly would work on SQLite and fail on PostgreSQL, and only on + // the path nobody exercised before deploying. + db bound // mu guards the default limits, which are set once at wiring but read on // every authentication. @@ -63,11 +70,11 @@ func OpenSQLite(ctx context.Context, path string) (*SQLite, error) { return nil, fmt.Errorf("securing %s: %w", f, err) } } - return &SQLite{db: db}, nil + return &SQLite{raw: db, dialect: SQLiteDialect, db: bound{c: SQLiteDialect, to: db}}, nil } // Close releases the database. -func (s *SQLite) Close() error { return s.db.Close() } +func (s *SQLite) Close() error { return s.raw.Close() } // --- tenancy --- @@ -193,7 +200,7 @@ func (s *SQLite) TouchKeys(ctx context.Context, seen map[string]time.Time) error if len(seen) == 0 { return nil } - return s.inTx(ctx, func(tx *sql.Tx) error { + return s.inTx(ctx, func(tx conn) error { stmt, err := tx.PrepareContext(ctx, `UPDATE api_keys SET last_used_at = ? WHERE id = ? AND (last_used_at IS NULL OR last_used_at < ?)`) if err != nil { @@ -378,7 +385,7 @@ func (s *SQLite) WriteUsage(ctx context.Context, records []Usage) error { if len(records) == 0 { return nil } - return s.inTx(ctx, func(tx *sql.Tx) error { + return s.inTx(ctx, func(tx conn) error { stmt, err := tx.PrepareContext(ctx, `INSERT INTO usage (ts, key_id, project_id, model, served_model, prompt_tokens, completion_tokens, cost_micros, status, latency_ms) @@ -421,13 +428,26 @@ func (s *SQLite) SpendSince(ctx context.Context, since time.Time) (map[string]in // --- helpers --- -func (s *SQLite) inTx(ctx context.Context, fn func(*sql.Tx) error) error { - tx, err := s.db.BeginTx(ctx, nil) +// begin starts a transaction and returns it twice: bound for queries, raw for +// commit and rollback. The bound half is what callers must use, so a statement +// inside a transaction is rewritten exactly as one outside it is. +func (s *SQLite) begin(ctx context.Context) (conn, *sql.Tx, error) { + tx, err := s.raw.BeginTx(ctx, nil) + if err != nil { + return nil, nil, err + } + return bound{c: s.dialect, to: tx}, tx, nil +} + +// inTx runs fn in a transaction, with the same placeholder rewriting the +// outside-transaction path gets. +func (s *SQLite) inTx(ctx context.Context, fn func(conn) error) error { + tx, err := s.raw.BeginTx(ctx, nil) if err != nil { return err } defer func() { _ = tx.Rollback() }() - if err := fn(tx); err != nil { + if err := fn(bound{c: s.dialect, to: tx}); err != nil { return err } return tx.Commit() @@ -453,7 +473,7 @@ func scanKey(sc scanner) (Key, error) { return k, nil } -func queryAll[T any](ctx context.Context, db *sql.DB, query string, args []any, +func queryAll[T any](ctx context.Context, db conn, query string, args []any, scan func(*sql.Rows) (T, error), ) ([]T, error) { rows, err := db.QueryContext(ctx, query, args...) diff --git a/internal/store/store_test.go b/internal/store/store_test.go index 87f6dd04..b2222155 100644 --- a/internal/store/store_test.go +++ b/internal/store/store_test.go @@ -85,7 +85,7 @@ func TestTheKeyItselfIsNeverStored(t *testing.T) { project := tree(t, s) secret, _ := mustKey(t, s, "key1", project) - rows, err := s.db.Query(`SELECT id, key_hash, key_prefix, name FROM api_keys`) + rows, err := s.db.QueryContext(context.Background(), `SELECT id, key_hash, key_prefix, name FROM api_keys`) if err != nil { t.Fatal(err) } diff --git a/internal/store/threads.go b/internal/store/threads.go index 7c2d0425..fca9548e 100644 --- a/internal/store/threads.go +++ b/internal/store/threads.go @@ -137,7 +137,7 @@ func (s *SQLite) AppendThread(ctx context.Context, projectID, threadID string, m return ErrThreadsNotEnabled } - tx, err := s.db.BeginTx(ctx, nil) + q, tx, err := s.begin(ctx) if err != nil { return err } @@ -145,10 +145,10 @@ func (s *SQLite) AppendThread(ctx context.Context, projectID, threadID string, m now := time.Now().UnixMilli() var owner string - err = tx.QueryRowContext(ctx, `SELECT project_id FROM threads WHERE id = ?`, threadID).Scan(&owner) + err = q.QueryRowContext(ctx, `SELECT project_id FROM threads WHERE id = ?`, threadID).Scan(&owner) switch { case errors.Is(err, sql.ErrNoRows): - if _, err := tx.ExecContext(ctx, + if _, err := q.ExecContext(ctx, `INSERT INTO threads (id, project_id, created_at, updated_at) VALUES (?, ?, ?, ?)`, threadID, projectID, now, now); err != nil { return err @@ -163,7 +163,7 @@ func (s *SQLite) AppendThread(ctx context.Context, projectID, threadID string, m } var next int64 - if err := tx.QueryRowContext(ctx, + if err := q.QueryRowContext(ctx, `SELECT COALESCE(MAX(seq), -1) + 1 FROM thread_messages WHERE thread_id = ?`, threadID).Scan(&next); err != nil { return err @@ -173,14 +173,14 @@ func (s *SQLite) AppendThread(ctx context.Context, projectID, threadID string, m if m.Model != "" { model = m.Model } - if _, err := tx.ExecContext(ctx, + if _, err := q.ExecContext(ctx, `INSERT INTO thread_messages (thread_id, seq, role, content, model) VALUES (?, ?, ?, ?, ?)`, threadID, next+int64(i), m.Role, m.Content, model); err != nil { return err } } // Touched on every append, so retention measures idleness rather than age. - if _, err := tx.ExecContext(ctx, + if _, err := q.ExecContext(ctx, `UPDATE threads SET updated_at = ? WHERE id = ?`, now, threadID); err != nil { return err }