-- SQLite schema for Weavegraph session and step checkpointing.
--
-- Two tables:
--   sessions — one row per session; carries a denormalized latest-step snapshot
--              so resume is a single point-lookup with no aggregate.
--   steps    — append-only checkpoint history; one row per barrier crossing.
--
-- All JSON columns are TEXT. Timestamps are RFC 3339 UTC.
-- Steps are immutable once written; monotonic step numbering is enforced by the
-- application layer, not the schema.

CREATE TABLE IF NOT EXISTS sessions (
    id                      TEXT    PRIMARY KEY,
    created_at              TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
    updated_at              TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
    concurrency_limit       INTEGER NOT NULL,
    last_step               INTEGER NOT NULL DEFAULT 0,
    last_state_json         TEXT,
    last_frontier_json      TEXT,
    last_versions_seen_json TEXT
);

CREATE INDEX IF NOT EXISTS idx_sessions_updated_at
    ON sessions(updated_at DESC);
END;
