Spec: LangChain + LangGraph as the Framework-Wide Engine¶
- ID: 027-langchain-langgraph-default
- Status: shipped
- Owner: @bamr87
- Created: 2026-08-10
- Roadmap Epic: continuous-evolution hardening (follow-on to spec 026)
- Constitution Compliance: confirmed against docs/constitution.md — §2 (CrewAI + LangGraph locked stack; LangChain core already ships via
langchain-ollama/langchain-core, no new framework), §4 (provenance), §8 (httpx + tenacity retries — LangChain'swith_retryis tenacity-backed), §13 (honest refusals)
1. Problem & Motivation¶
The stack lock names LangGraph as the workflow engine, but before this spec it was fringe: LegalWorkflow was opt-in (use_langgraph=false default, so the flagship endpoint served the no-engine stub), TopicFrontier claimed to be a LangGraph StateGraph while executing nodes sequentially, and three call sites (TestSynthesizer, TopicFrontier, plus the workflow's own factory) each hand-rolled their own Ollama transport (raw urllib, no retries — while the dev-only Claude client had full backoff). Result: the constitutionally mandated product runtime was reached through the least robust, least uniform code paths, and the "LangGraph platform" claim was mostly aspiration.
2. Goals¶
- G1: One shared LangChain transport (
ChatOllama+ tenacity-backed retry) used by every Ollama-backed agent path (extends BL-20260810-05). - G2: LangGraph (
LegalWorkflow) is the DEFAULT engine for/api/analysis, wrapped in a resilience boundary that degrades honestly (never 500s) when Ollama or the langgraph stack is unavailable. - G3:
TopicFrontierbecomes a real compiledStateGraph(fulfilling spec 018's claim), with a sequential fallback for bare installs, and its LLM call logged throughcore.provenance. - G4: Frontend defaults match the backend default (LangGraph toggles on).
3. Non-Goals¶
- NG1: Durable LangGraph checkpointer threads on the default API path (follow-up — requires checkpointer-schema lifecycle on every request).
- NG2: Migrating the LSAT tutor / LSATContentAuthor / factory executors onto the shared transport (follow-up; they carry dual-provider claude_code logic).
- NG3: Real GraphRAG retrieval (BL-20260810-01) — the default engine still refuses on an empty corpus, now visibly via the coverage envelope.
- NG4: LangChain agents/chains beyond the runnable transport — CrewAI remains the crew abstraction per §2.
4. User Stories¶
- As a user of Analysis Studio, I want the real retrieval-backed workflow by default, so that answers come from the local LLM + knowledge graph instead of a stub.
- As a developer, I want every agent LLM call to share one retrying LangChain transport, so that transient Ollama hiccups don't fail whole workflows.
- As a maintainer, I want TopicFrontier to be an actual StateGraph, so that the codebase matches its specs and gains LangGraph's execution semantics.
5. Functional Requirements¶
- FR-1: New
law_ai.agents_runtime.llmmodule:build_chat_ollama()returns aChatOllamarunnable wrapped withwith_retry(3 attempts, exponential jitter, transient-error allowlist);generate_text_sync()for sync call sites;coerce_text(); import-safe without thelocal-aiextra (LLMUnavailableError). - FR-2: TTL-cached Ollama availability probes
ollama_available()/ollama_available_async()(1 s timeout, 15 s cache, shared cache). - FR-3:
AnalysisRequest.use_langgraphdefaults totrue;LegalAnalyst._run_via_langgraphprobes availability first and catches engine failures (ImportError, transport errors), returning a degraded[ENGINE UNAVAILABLE]refusal logged asfallback/offlineinstead of raising. - FR-4:
LegalWorkflow._default_llm_factoryroutes throughbuild_chat_ollama(). - FR-5:
TestSynthesizer._call_ollamaandTopicFrontier._call_ollamakeep their signatures +RuntimeErrorcontracts but transport viagenerate_text_sync(rawurllibremoved). - FR-6:
TopicFrontier.runexecutes a compiledStateGraph(TopicFrontierState)(scan_graph → prioritize → dispatch → summarize) when langgraph is importable, sequential fallback otherwise; node functions keep DI-kwarg signatures for direct unit testing. - FR-7:
TopicFrontier's prioritize LLM call is logged viaprovenance.log_agent_callwith model, latency, prompt id + version (§4). - FR-8: Frontend LangGraph toggles default ON;
AnalysisStudiosends the explicit boolean (an omitted flag would silently re-enable the engine server-side after opt-out).
6. Non-Functional Requirements¶
- NFR-PERF-1: When Ollama is down,
/api/analysisstill responds fast: the probe fails in ≤1 s once per 15 s window (cached thereafter) — no retry-budget burn on the request path. - NFR-AUDIT-1: All new logging goes through
core.provenance; fallback outputs are logged asfallback/offline, never a real model name. - NFR-SEC-1: LangChain
with_retryis tenacity-backed and the probe useshttpx(constitution §8); no new dependencies added (langchain-core and langchain-ollama already ship in thelocal-aiextra).
7. Data Model Impact¶
None. No schema changes; API request default flip only (use_langgraph).
8. API Surface¶
POST /api/analysis: use_langgraph now defaults to true. Response shape unchanged from spec 026 (degraded fallbacks reuse the spec-026 fields). Explicit use_langgraph: false preserves the previous non-engine behavior.
9. Agent / Crew Impact¶
LegalAnalyst (default routing + resilience boundary), LegalWorkflow (shared transport), TopicFrontier (real StateGraph + provenance + shared transport), TestSynthesizer (shared transport). No new agents; no prompt changes.
10. Acceptance Criteria¶
- AC-1:
AnalysisRequest(text="x").use_langgraph is True, and a POST with no flag routes through the LangGraph path (verified by the probe/fallback unit tests). - AC-2: With the availability probe returning False,
LegalAnalyst.runreturnsdegraded=true,confidence=0.0,requires_review=true, rule containingENGINE UNAVAILABLE— and does not raise. - AC-3: With the probe True and
LegalWorkflow.invokeraisingOSError, the same degraded contract holds (no exception escapes). - AC-4:
build_chat_ollama()returns aRunnableRetrywith 3 attempts;retries=1returns the bareChatOllama;output_format="json"sets Ollama JSON mode. - AC-5:
TopicFrontier._build_graph()compiles with nodes{scan_graph, prioritize, dispatch, summarize}andrun()completes throughgraph.ainvokewith injected fakes. - AC-6: The probe caches: two consecutive
ollama_available()calls issue exactly one HTTP request; the async twin serves the shared cache.
11. LegalBench Impact¶
- Tasks affected: none directly (transport + routing change, prompts unchanged). The eval harness still calls its own path.
- Expected delta: 0 pp. Retries can only reduce spurious infra failures.
12. Risks & Open Questions¶
- R-1: Default-on LangGraph makes
/api/analysislatency depend on the local Ollama when it IS up (real model inference, seconds). This is the intended product behavior; the UI toggle remains for the stub path. - R-2: The probe adds one 1 s-bounded HTTP call per 15 s window per process — negligible, but a Redis-shared probe would suit multi-worker deployments (follow-up).
13. Out of Scope / Follow-ups¶
See tasks.md → ## Follow-ups Discovered (mirrored into BACKLOG.md as BL-20260810-13 … BL-20260810-16).