Charlie Roffe

Principal Architect & Engineering Director · Cloud & AI

Madrid, Spain · LinkedIn · GitHub

ADR-024: Stateful Multi-Agent Architecture Copilot

Context

The Visual Diagram Generator was a single-shot demo: one prompt in, one topology out. That demonstrates structured output, but not the agentic patterns that matter in current GenAI engineering — specialist agents reviewing each other's work, durable execution state, and a human approving changes before they are applied. The upgraded Architecture Copilot needs multiple specialist roles (an architect drafting, security and FinOps auditors critiquing with machine-applicable fixes), human-in-the-loop approval that pauses the run and resumes it later with the visitor's decisions, and survival on scale-to-zero compute — the resume request may hit a different Cloud Run replica minutes later. This collides with ADR-006: a paused agent graph is not a chat transcript but mid-execution machine state, and round-tripping it through the browser would expose internal checkpoint structures as API surface.

Decision

Build the copilot as a stateful multi-agent system, checkpointed server-side in Firestore, while keeping every model invocation on the official @google/genai SDK.

1. Agent topology: an architect node drafts the diagram; security and FinOps auditors run in parallel from the draft, with deduplicated recommendations; /api/ai/copilot/start returns the draft plus pending suggestions and a threadId, and /resume applies the approved/dismissed IDs. Approved action blocks (addNodes/removeNodes/addLinks/removeLinks) are applied deterministically in code — the LLM proposes, plain TypeScript mutates with zero model calls on resume.
2. Durable checkpoints: session state persists to the existing Firestore layer (ADR-007) under checkpoints/{threadId} with an in-memory L1 cache; local development falls back to in-memory caching. Zero new infrastructure, zero standby cost, EU-resident.
3. Direct model integration: every model call goes through @google/genai directly with structured schemas (ADR-005, ADR-013).

Amended 2026-10-16: checkpoint documents carry an expiresAt 7 days out, and Firestore TTL policies delete them. threadId must be a UUID, and upstream model timeouts return 503 service-unavailable.

Amended 2026-11-01: LangGraph was replaced with a direct TypeScript agent state machine (server/ai/diagramAgent.ts). This eliminated heavy dependencies (@langchain/langgraph, @langchain/core) and ~1s lazy compilation latency, keeping server memory small (~30MB) and cold starts instant.

Consequences

Benefits:
* A genuine agentic showcase: parallel specialist agents, durable checkpoints, and human-in-the-loop approval — not a prompt with extra steps.
* Pause/resume survives replica recycling, scale-to-zero, and a visitor returning minutes later — the checkpoint lives in Firestore, not process memory.
* Deterministic mutation: approved recommendations are applied by code, so the reviewed diff is exactly what happens.

Trade-offs:
* The backend is no longer uniformly stateless — one route family owns durable execution state, as a deliberate, bounded, documented exception (ADR-006 amendment).
* Checkpoint documents originally had no TTL, so abandoned threads persisted (resolved 2026-10-16: they now expire after 7 days).
* Heavy dependency weight and lazy compilation overhead from the initial LangGraph implementation (resolved 2026-11-01: replaced with a direct TypeScript state machine).

Alternatives considered

1. Round-tripping graph state through the client (pure ADR-006): Rejected — exposes LangGraph's internal checkpoint format as public API, bloats payloads, and lets clients forge execution states the graph never produced.
2. One mega-prompt asking for design + critiques + final diagram: Rejected — no real multi-agent structure, nothing to pause, and human approval becomes cosmetic.
3. Keeping LangChain ChatVertexAI for model calls: Rejected after production 404s and timeouts on the Vertex eu endpoint; the direct SDK is proven by every other route.
4. MemorySaver in production: Rejected — checkpoints evaporate on scale-to-zero or replica switch, precisely the failure the feature must survive.

Architecture Decision Records