ADR-006: AI Conversation State Management
Context
To support multi-turn conversational dialogs (e.g. Chatting about a parsed SSL certificate or asking follow-up questions about a generated architecture diagram), the server must maintain context.
However, storing conversation session states in server-side memory would violate the stateless, scale-to-zero container hosting model of Cloud Run.
Decision
Maintain conversation session states entirely on the client side, passed back-and-forth as historical message payloads within the API requests.
The Express backend remains stateless, acting as a secure gateway that reformats the client-provided message arrays into the structured prompt format required by the Gemini SDK.
The multi-turn endpoints are POST /api/ai/cert-chat and POST /api/ai/contact. The server validates and trims the replayed history (cert-chat: last 20 turns within 12,000 characters; contact: last 16 turns within 12,000 characters, 2,000 per turn), and every /api JSON body is capped at 10 KB. RAG Q&A is single-turn.
Consequences
Benefits:
* Cloud Run instances remain fully stateless, enabling immediate scaling and rapid container recycling.
* Eliminates the need for server-side state databases, session stores, or sticky sessions.
* Client-side state persistence survives server cold starts and container recycles seamlessly.
Alternatives considered
1. Redis / Cloud Memorystore: Rejected as it introduces continuous standby running costs that violate our zero-budget baseline.
2. Firestore sessions: Rejected for per-turn conversation state specifically — an extra database hop and query billing on every chat turn, for data the client already holds. Firestore remains the platform persistence layer where durability pays for itself: the two-tier cache (ADR-007) and shareable results (ADR-017). Conversation state is per-user and ephemeral (client-owned); cached artifacts and shared results are cross-user and durable (Firestore-owned).
Amended (ADR-024): the Architecture Copilot is a scoped exception — its LangGraph execution state is checkpointed server-side in Firestore, because a run paused for human approval must resume across requests, replicas, and scale-to-zero. Chat-style demos remain fully client-owned per this ADR.
Amended (2026-10-16): Firestore also holds the site-wide daily AI usage counter (ai_usage) and the messages sent through the contact concierge (contact_messages). Neither is a conversation transcript, so the dividing line holds.