ai-powered-conversational-intelligence

Chat History & Authentication Audit

This document records what was actually found in the repository before any code was changed for the “universal server-side chat history” feature, the root cause of the cross-browser/cross-device history problem, and what was changed as a result. Written from direct inspection of the code (not assumed) — anywhere a fact could not be verified is marked [assumption].


1. Current architecture (before this feature)

2. Current authentication flow (as found)

  1. POST /auth/register (api/identity_auth.py) creates a row in identity.users (UUID primary key) and, unless email verification is required, immediately issues an access token + refresh-token cookie.
  2. POST /auth/login verifies the password and issues the same token pair.
  3. The access token (identity/security.py::create_access_token) carries sub (the user’s UUID, as a string) and roles — nothing else sensitive.
  4. api/auth.py::verify_api_key validates the token and attaches security.oidc.AuthIdentity(subject=<user id>, roles=..., mode="local") to the request.
  5. Frontend: frontend/src/store/localAuthStore.ts calls POST /auth/refresh on load to silently restore a session from the refresh cookie (in-memory-only access token, never localStorage).

This part was already correct and needed no changes — the authenticated user’s identity (AuthIdentity.subject, a stable UUID) was already available to every route; it just wasn’t being used for chat history.

3. Current chat-history flow (before this feature) — root cause

Directly inspected: frontend/src/store/chatStore.ts and frontend/src/lib/history.ts.

// chatStore.ts, before this feature
interface ChatState {
  queryHistory: QueryHistoryEntry[]
  activeConversationId: string           // crypto.randomUUID(), client-only
  conversations: Record<string, ConversationSummary>  // in-memory only
  ...
}

Root cause of “same user, different browser, no history”: there was no server-side storage of chat data at all. This is not a bug in an existing sync mechanism — the mechanism didn’t exist. Confirmed directly: identity/models.py already defined Conversation/Prompt/AiOutput tables (added in the same prior session that built identity/), but no code path anywhere in the repository ever read or wrote them — verified by a repo-wide search for identity.repositories.history (the module that would need to exist to use them) before this feature, which returned nothing.

Was anything linked to a stable user ID, a browser ID, or a session ID?

Nothing was linked to any persistent identifier at all, because nothing was persisted. activeConversationId (browser-generated) was the closest thing to an identifier, and it was discarded on every page reload.

Current search behavior (before this feature)

// HistoryDrawer.tsx, before this feature
const filtered = useMemo(() => {
  const query = search.trim().toLowerCase()
  if (!query) return allConversations
  return allConversations.filter((c) => c.title.toLowerCase().includes(query))
}, [allConversations, search])

Current authorization behavior (before this feature)

N/A for chat data specifically — there was no server-side chat data to authorize access to. Every other authenticated route (/execute, /documents, etc.) already enforced ownership/permissions correctly (agent/authz.py, api/authz.py) and was unaffected by this gap.

Current vector-retrieval behavior (before this feature)

Unrelated to, and unaffected by, the chat-history gap — the business-context vector retrieval layer (retrieval/, built in an earlier session, see docs/vector-retrieval-design.md) operates on schema/glossary/metric/ documentation chunks, never on chat history. Preserved as-is by this feature; re-verified working after every change in this pass (see the final validation section of this feature’s implementation report).


4. Required changes (what this audit led to)

5. Migration strategy

No migration of existing conversation/message data was performed or is needed, and this is not an oversight — it follows directly from §3 above: there was no existing server-side chat data to migrate. The identity.conversations/identity.prompts/identity.ai_outputs tables existed in the schema (from a prior session) but no code path anywhere in the repository ever wrote to them before this feature — verified directly by a repo-wide search for identity.repositories.history (the module that would need to exist to use them), which returned nothing prior to this pass. Given that, the real deployment’s tables could only have contained rows if something inserted them by hand outside the application entirely, which this audit has no evidence of and treats as not applicable. The only database change needed was therefore additive (new columns + indexes on tables no application code had ever populated), not a data-migration problem (no “identify records that can be safely mapped to a user” step applies, since there are no ambiguous/unmapped/ anonymous records to sort through in the first place). Directly queried after this feature’s own work (and its own test data cleaned up): conversations/prompts/ai_outputs hold only the rows this feature’s own live end-to-end testing created, all removed again afterward.

[assumption]: A different deployment of this codebase that had already been running with an older, in-memory-only frontend for a long time would have nothing to migrate either, by construction — chat data never left the browser tab it was created in, so there is no server-side row of any kind, anywhere, that this feature’s migration could have found and mapped. If a deployment separately exported/backed up browser localStorage chat data through some out-of-band mechanism this audit has no visibility into, importing it is out of scope for this pass and not attempted.

6. Security risks identified and addressed

7. Assumptions and limitations