Skip to content

ORM date handling: TEXT columns, missing cursor pagination, unused composite indexes #360

Closed

Description

@joelteply

Problem

Multiple interconnected issues with how the ORM handles dates, pagination, and indexing.

1. Dates stored as TEXT, not native TIMESTAMP

All @DateField decorated fields are stored as TEXT columns containing ISO 8601 strings. This sorts correctly by accident (ISO is lexicographically sortable), but is fragile:

  • Any code that writes Date.now() (unix ms integer) to the same column breaks sort order
  • Mixed formats in one column = wrong ordering
  • No database-level date operations (BETWEEN, date math)

Locations:

  • workers/continuum-core/src/orm/sqlite.rs — "created_at TEXT NOT NULL"
  • workers/continuum-core/src/orm/postgres.rs — has TIMESTAMPTZ in pg_type_from_field_type() but only for schema-defined fields, not dynamic ones

2. Cursor pagination defined but NEVER implemented

query.rs defines Cursor, CursorDirection, ComparableValue types. StorageQuery has a cursor: Option<Cursor> field. But zero query code in sqlite.rs or postgres.rs ever reads it. All pagination is OFFSET-based, which gets slow on large tables.

3. Composite indexes declared but never created

@CompositeIndex decorator collects metadata via getCompositeIndexes(), but no ORM adapter calls this function or creates the indexes. Example:

@CompositeIndex({
  name: 'idx_chat_messages_room_timestamp',
  fields: ['roomId', 'timestamp'],
  direction: 'DESC'
})
export class ChatMessageEntity extends BaseEntity { ... }

This index would massively speed up chat room pagination queries but it's never created.

4. Widget-level cache may have its own sort issues

The chat widget (EntityScroller, chat-widget) maintains its own message cache. If the cache sorts by string representation of dates rather than actual Date comparison, ordering would be wrong even if the DB returns correctly ordered results.

Fix Strategy

These should be addressed at the Rust ORM layer (single source of truth):

  1. Date columns: Use TIMESTAMPTZ (Postgres) / store as ISO but validate format on write (SQLite). Consider integer unix ms for SQLite (native integer comparison).
  2. Cursor pagination: Implement keyset pagination using the existing Cursor types — WHERE timestamp < ? ORDER BY timestamp DESC LIMIT 50
  3. Composite indexes: Have ensure_table_exists read @CompositeIndex metadata and CREATE INDEX
  4. Widget cache: Audit EntityScroller sort comparators

Impact

  • Chat message ordering may be wrong in edge cases
  • Pagination performance degrades on large tables (OFFSET scanning)
  • Missing indexes = full table scans on common queries

🤖 Generated with Claude Code

Activity

  1. joelteply commented on Mar 25, 2026

    @joelteply
    ContributorAuthor

    Investigation Results (2026-03-24)

    1. Dates stored as TEXT — NOT A BUG

    • Postgres: Uses real TIMESTAMPTZ columns for created_at/updated_at. DateTime<Utc> passed as native typed parameter. All date operations work correctly at the DB level.
    • SQLite: Uses TEXT with RFC 3339 strings from chrono::Utc::now().to_rfc3339(). Lexicographic sort = chronological for zero-padded UTC ISO dates. No code path writes non-RFC-3339 formats through the ORM.
    • Verdict: Working correctly. No fix needed.

    2. Composite indexes — MOSTLY WORKING

    Verified in Postgres:

    • chat_messages: 4 indexes (including idx_chat_messages_room_created composite) ✅
    • memories: 4 indexes ✅
    • timeline_events: 4 indexes ✅
    • cognition_state_snapshots: 2 indexes ✅

    Some low-traffic tables (skills, academy_benchmark_results) missing composite indexes because ensureSchema runs lazily on first access — tables that haven't been written to yet don't get indexes.

    Minor issue: CompositeIndex.direction field (ASC/DESC) is declared in TypeScript but dropped by DataSchemaManager — all indexes are ascending. Not critical for alpha.

    3. Cursor pagination — TYPES EXIST, NOT IMPLEMENTED

    Cursor, CursorDirection, ComparableValue defined in query.rs. StorageQuery.cursor field is deserialized but neither sqlite.rs nor postgres.rs reads it. All pagination is OFFSET-based. This is fine at alpha data volumes.

    Remaining work

    • Eager schema ensure on startup (iterate registered entities, not just on first access)
    • Pass direction from CompositeIndex metadata through to SQL
    • Cursor pagination (post-alpha — OFFSET works fine for now)

    Downgrading from Phase 0 to Phase 1 — this is not a ship-blocker.

  2. added a commit that references this issue on Mar 25, 2026
  3. joelteply commented on Mar 25, 2026

    @joelteply
    ContributorAuthor

    Investigated: dates work correctly (TIMESTAMPTZ/RFC3339), composite indexes working for high-traffic tables, cursor pagination not needed for alpha (OFFSET sufficient). Schema auto-evolution added (auto-adds missing columns on Postgres UPDATE). No remaining work for alpha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions