-- 0003_chat_history: persistent chat sessions + message stream (DevDocs/design/16 §2.1, §3.1). -- chat_sessions records the persistent sessions: one fixed main session plus arbitrarily -- many secondary (sub) sessions. chat_messages is the raw, durable message stream in -- millisecond-timestamp cursor order. The two are the backend truth for the Web frontend; -- they are NOT memory (design/16 §3.2 separates memory from the raw stream). -- -- Its canonical schema is owned by internal/runtime/history, which reuses these tables. The -- migration is the authoritative path; internal/runtime/history.NewStore re-issues the same -- DDL idempotently so the store is self-contained (matching memory/session). CREATE TABLE IF NOT EXISTS chat_sessions ( id TEXT PRIMARY KEY, -- main = fixed id ("web"); sub = "web:" type TEXT NOT NULL, -- 'main' | 'sub' title TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL -- ms timestamp ); CREATE TABLE IF NOT EXISTS chat_messages ( id TEXT PRIMARY KEY, -- message unique id (uuid) session_id TEXT NOT NULL, -- owning session (main or sub) role TEXT NOT NULL, -- user / assistant / system content TEXT NOT NULL, -- raw message body (incl.

/) mode TEXT, -- behavior mode (optional, bubble render) mood TEXT, -- mood (optional) image TEXT, -- attachment base64/URL (optional) thinking TEXT, -- chain of thought (optional, stored, not echoed by default) created_at INTEGER NOT NULL -- ms timestamp (pagination cursor) ); CREATE INDEX IF NOT EXISTS idx_chat_msgs_session_created ON chat_messages (session_id, created_at DESC);