ai-powered-conversational-intelligence

Chat History Architecture

How conversations and messages are stored, served, and rendered — server-side, authenticated, and universal across browsers/devices for a locally-authenticated user. See docs/chat-history-authentication-audit.md for the problem this replaces and why.

1. Model

User (identity.users)
  -> many Conversations (identity.conversations)
      -> many Prompt/AiOutput pairs (identity.prompts / identity.ai_outputs)

Prompt (the user’s question) and AiOutput (the assistant’s answer) are two separate, pre-existing tables (identity/models.py, added in an earlier session but never previously used) rather than one unified messages table — identity/repositories/history.py merges them into one chronological, role-tagged view (MessageRow) for the API to serve. This was a deliberate choice: Prompt/AiOutput already carry richer, asymmetric fields (voice-correction tracking on Prompt; model/token/ latency/error metadata on AiOutput) that a single generic role+ content table would either lose or need extra columns for anyway — reusing what already existed avoided a second migration to add a table that would just duplicate their shape.

Conversation fields

Field Purpose
id UUID primary key, the permanent conversation identifier
user_id FK to users.id, ON DELETE CASCADE — every query is scoped to this
title Nullable; auto-derived from the first question if not set explicitly
feature_type "text_to_sql" for chat-originated conversations
status "active" (existing column; not yet used to distinguish more states)
created_at / updated_at Standard timestamps
last_message_at New in this feature. Set only when a message is actually appended — drives recency ordering without being disturbed by a pure metadata edit (a rename)
archived_at New in this feature. Set/cleared via PATCH /conversations/{id} — an archived conversation is excluded from the default list but still fully readable/searchable
deleted_at Soft-delete marker (pre-existing column, now actually used)

Message fields (via Prompt/AiOutput, merged into MessageRow)

Field Source Purpose
id Prompt.id / AiOutput.id Row identity
conversation_id both FK
role derived ("user" for Prompt, "assistant" for AiOutput)  
content Prompt.final_content / AiOutput.content The rendered text
sequence_number New in this feature, on both tables Deterministic ordering — see below
created_at both  
status AiOutput.status ("completed"/"failed"/…); always "completed" for a user row  
model_name AiOutput.model_name  
error_code AiOutput.error_code  
metadata AiOutput.metadata_json — see “Universal conversation history” below The full, bounded, versioned per-turn snapshot a reload is reconstructed from

Deterministic ordering: sequence_number (new column on both prompts and ai_outputs) is assigned once, inside identity.repositories.history.append_turn’s single transaction, from the conversation’s own current max — a question always gets n, its answer n + 1. This is what makes ordering correct even when two rows’ created_at timestamps could theoretically collide (unlikely on PostgreSQL’s microsecond-precision TIMESTAMPTZ, but not something ordering should depend on regardless).

2. Indexes

Added by identity/migrations/versions/a1f3c9d84e21_chat_history_search_and_ordering.py:

3. API

All under api/chat_history.py, all requiring Depends(require_local_user) (the same dependency api/identity_auth.py’s own self-service routes use), all ownership-scoped inside the repository layer, never via a separate permission check on top of an unscoped fetch:

Method Path Purpose
GET /conversations Paginated list, most-recent-activity-first
POST /conversations Create (optional title)
GET /conversations/{id} Fetch one (404 if not owned/doesn’t exist)
PATCH /conversations/{id} Rename and/or archive/unarchive
DELETE /conversations/{id} Soft-delete
GET /conversations/{id}/messages Paginated messages, chronological
POST /conversations/{id}/messages Direct single-message append (API completeness; not the real /ask path — see §4)
GET /chat/search?q= Search across this user’s own conversations/messages

A wrong/forged conversation_id in any of these routes resolves to 404, never a 403 — this never confirms to a caller whether another user’s conversation exists at all (the same account-enumeration-avoidance principle identity.exceptions.InvalidCredentialsError already applies to login in this codebase).

Pagination: limit/offset, server-side-clamped to Settings.chat_history_max_page_size/chat_search_max_page_size (a client can request a smaller page but never a larger one than the configured cap) — a client-supplied limit of 0 or a missing value falls back to Settings.chat_history_page_size/chat_search_page_size.

4. How a turn actually gets persisted

POST /ask (api/main.py) is unchanged in its core behavior (schema retrieval, SQL generation/validation/execution all run exactly as before) — after computing the final agent state, it calls api/chat_persistence.py::persist_ask_turn, which:

  1. No-ops immediately unless the caller authenticated via a local account (AuthIdentity.mode == "local") — see §6’s “Known limitations.”
  2. Resolves (or creates, on the first turn) the target Conversation — AskRequest.conversation_id, if supplied and owned by this caller; otherwise a fresh one.
  3. Calls identity.repositories.history.append_turn, which persists the question (Prompt) and answer (AiOutput) together as one transaction, with adjacent sequence_numbers.
  4. Returns the conversation id, echoed back as AskResponse.conversation_id for the frontend to remember for the next turn.

This can never fail an otherwise-successful /ask response — every step is wrapped in the route handler’s own broad except Exception (api/chat_persistence.py’s own module docstring explains why this is the correct tradeoff: the SQL has already been generated/validated/ executed by the time persistence runs, and a transient identity-DB hiccup is a completely separate failure class from “was the question answered”).

5. Frontend

frontend/src/store/chatStore.ts now:

frontend/src/components/layout/HistoryDrawer.tsx’s search debounces into a real GET /chat/search call (see docs/chat-history-search.md) whenever local-auth history is active; otherwise it falls back to the original plain client-side title filter over the in-memory list — unchanged from before this feature, for a deployment where local auth isn’t configured at all.

6. Retention behavior

6.5. Universal conversation history (2026-09-27)

Closes the limitation this section used to describe (reproduced below, struck through, for anyone who read the earlier version): reopening a saved conversation could show no assistant answer, an empty SQL editor with “Confirm and Run”, or [object Object] — traced to api/ chat_persistence.py only ever persisting {"sql": "..."}" (or nothing) regardless of which source(s) actually answered, and the frontend reconstruction (frontend/src/lib/history.ts::serverMessagesToQueryHistory) fabricating a hardcoded sources_used: [] for every reloaded turn, which TurnCard.tsx’s own “empty means the SQL path” convention then misinterpreted for every non-SQL saved answer.

api/chat_persistence.py::_build_history_metadata now snapshots a bounded, redacted superset of the already-computed AskResponse into the exact same AiOutput.metadata_json column — no migration was needed: it was already an arbitrary-shape JSONB (JSON on SQLite) column with no fixed schema (see identity/models.py’s own docstring), so a richer payload is a pure application-layer change. A schema_version field (currently 2) lets the frontend tell a rich record from a pre-this-feature {"sql": ...} -or-nothing legacy one and degrade gracefully rather than guess:

A reloaded past turn is not a byte-for-byte reconstruction of a live one. Only the answer text and, if the turn produced SQL, that SQL (AiOutput.metadata_json) are persisted — not the full AskResponse (charts, per-source citations for a multi-source answer, the exact retrieved schema DDL, the query plan). — superseded above. One honest remaining gap: the retrieved schema’s DDL itself (not just table names) and a chart’s own free-text customization (title/axis labels, entered client-side, never sent to the backend) are still not restored on reload — disclosed, not silently dropped.

7. Known limitations