Skip to content

Self-improving AI Employees for everyone

refactor(llm)!: remove LangChain

trohitg24d333c346be
diff --git a/.env.template b/.env.template
index 26eac11a..cafcda5d 100644
--- a/.env.template
+++ b/.env.template
@@ -187,11 +187,6 @@ COMPACTION_RATIO=0.8
# Per-user overrides via the UserSettings row (Settings tab).
AGENT_RECURSION_LIMIT=200
-# Temporary native-SDK rollout switch. This is read only by
-# agent.prepare_payload.v1, so changing it affects newly prepared Temporal
-# executions and never changes the engine of an existing history.
-AGENT_LLM_ENGINE=native
-
# Multi-agent delegation. The concurrency limit counts all active children
# and grandchildren in one root execution; the root agent is not counted.
MAX_CONCURRENT_SUBAGENTS=3
diff --git a/CLAUDE.md b/CLAUDE.md
index e1d931b1..74d5c83b 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -51,7 +51,7 @@ This is a React Flow-based workflow automation platform implementing n8n-inspire
| **[Onboarding Service](./docs-internal/onboarding.md)** | First-launch welcome wizard with 5 steps, database persistence, and replay from Settings |
| **[CLI Services Integration](./docs-internal/cli_services_integration.md)** | Guide for integrating CLI-based services (Temporal, etc.) with proper lifecycle management |
| **[Temporal Architecture](./docs-internal/TEMPORAL_ARCHITECTURE.md)** | Distributed workflow execution: activities, FIRST_COMPLETED scheduling, horizontal scaling |
-| **[Native LLM SDK](./docs-internal/native_llm_sdk.md)** | Native SDK layer in `services/llm/`: protocol-based providers, config-driven base URLs, and 13 providers (11 cloud plus Ollama / LM Studio; xAI is agent-selectable without a standalone chat-model node). A JSON-driven `supports_model_listing: false` flag covers endpoints that speak the OpenAI chat wire format but ship no `/v1/models` route (Sarvam) — `OpenAIProvider.fetch_models` then serves the curated list and probes the key with a one-token completion. LangChain is isolated to replay compatibility for legacy Temporal histories. |
+| **[Native LLM SDK](./docs-internal/native_llm_sdk.md)** | Native SDK layer in `services/llm/`: protocol-based providers, config-driven base URLs, and 13 providers (11 cloud plus Ollama / LM Studio; xAI is agent-selectable without a standalone chat-model node). A JSON-driven `supports_model_listing: false` flag covers endpoints that speak the OpenAI chat wire format but ship no `/v1/models` route (Sarvam) — `OpenAIProvider.fetch_models` then serves the curated list and probes the key with a one-token completion. Every provider is native; `tests/llm/test_langchain_removed.py` locks the retired-dependency end state. |
| **[Event Waiter System](./docs-internal/event_waiter_system.md)** | In-memory asyncio.Future waiter for push-based trigger nodes on the canvas-Run path (WhatsApp, Telegram, Webhook, Chat, Task completion). Deployed triggers ride the Temporal canary path; the Redis-Streams backend was retired in Wave 15.3 |
| **[Credentials Encryption](./docs-internal/credentials_encryption.md)** | Fernet + PBKDF2 encryption pipeline, separate credentials.db, two credential systems (OAuth vs API keys), multi-backend abstraction |
| **[Status Broadcaster](./docs-internal/status_broadcaster.md)** | WebSocket-first communication: StatusBroadcaster singleton, live count via `len(MESSAGE_HANDLERS) + len(get_ws_handlers())`, broadcast message types, Android two-state model |
@@ -1142,7 +1142,7 @@ deploy_workflow() -> Sets up triggers, returns immediately
### AI Chat Model System (5-Layer Architecture)
-Chat-model nodes (`openaiChatModel`, `anthropicChatModel`, ...) render through `SquareNode` from the backend NodeSpec. Direct chat and every new agent execution route through the native SDK facade (`ChatUnifier` in `services/llm/`); the 12-provider surface is 10 cloud providers plus Ollama and LM Studio. Eleven providers have standalone chat-model nodes, while xAI is selected directly from agent parameters. Model params (max output, context length, thinking type, temperature range) come from `ModelRegistryService`. The provider architecture, proxy/local-LLM routing, per-provider model + thinking/reasoning matrix (budget / effort / format), and the replay-only LangChain boundary for legacy Temporal histories all live in **[Native LLM SDK](./docs-internal/native_llm_sdk.md)**. The runtime output schema (`thinking` field for downstream nodes) is backend-served per [Schema Source of Truth RFC](./docs-internal/schema_source_of_truth_rfc.md).
+Chat-model nodes (`openaiChatModel`, `anthropicChatModel`, ...) render through `SquareNode` from the backend NodeSpec. Direct chat and every new agent execution route through the native SDK facade (`ChatUnifier` in `services/llm/`); the 12-provider surface is 10 cloud providers plus Ollama and LM Studio. Eleven providers have standalone chat-model nodes, while xAI is selected directly from agent parameters. Model params (max output, context length, thinking type, temperature range) come from `ModelRegistryService`. The provider architecture, proxy/local-LLM routing, and the per-provider model + thinking/reasoning matrix (budget / effort / format) all live in **[Native LLM SDK](./docs-internal/native_llm_sdk.md)**. A Temporal history recorded before the native cutover carries no `llm_engine` marker and messages in a retired wire format; `agent.execute_llm_step.v1` refuses it with a non-retryable `InvalidAgentLLMEngine` rather than misreading it, and the deployment must be Reset. The runtime output schema (`thinking` field for downstream nodes) is backend-served per [Schema Source of Truth RFC](./docs-internal/schema_source_of_truth_rfc.md).
## AI Agent Node Architecture
diff --git a/docs-internal/native_llm_sdk.md b/docs-internal/native_llm_sdk.md
index 346f083e..c5b63e0e 100644
--- a/docs-internal/native_llm_sdk.md
+++ b/docs-internal/native_llm_sdk.md
@@ -2,17 +2,16 @@
> **Authoring model (post-Wave-11):** each chat-model node is a self-contained Python plugin folder under `server/nodes/model/<provider>_chat_model/` that emits a `NodeSpec`. The frontend reads specs via [client/src/lib/nodeSpec.ts](../client/src/lib/nodeSpec.ts) + [adapters/nodeSpecToDescription.ts](../client/src/adapters/nodeSpecToDescription.ts) and renders through `SquareNode` with zero TS edits. See [plugin_system.md](./plugin_system.md) and [server/nodes/README.md](../server/nodes/README.md) for the plugin model, and "Adding a New Provider" below for the chat-model recipe.
-OpenCompany routes chat-model nodes and all new agent executions through the
+OpenCompany routes chat-model nodes and every agent execution through the
native SDK layer in `server/services/llm/`. The shared buffered tool loop lives
-in `server/services/agent_runtime.py`. A narrowly isolated LangChain branch is
-retained only while Temporal histories recorded before the cutover drain.
+in `server/services/agent_runtime.py`.
## Why a Native Layer
-LangChain added a useful abstraction early on, but it introduced three recurring problems:
+Talking to each vendor SDK directly avoids three problems a translation layer kept reintroducing:
-1. **Windows/Python 3.13 hangs**: `langchain_google_genai` deadlocks on gRPC init. The native Gemini path bypasses LangChain entirely and uses `google.genai.Client` directly.
-2. **Parameter translation loss**: LangChain's `max_completion_tokens` rewrite breaks OpenAI-compatible providers (DeepSeek, Kimi, Mistral) that expect `max_tokens`.
+1. **Windows/Python 3.13 hangs**: the wrapped Google adapter deadlocked on gRPC init. The native Gemini path uses `google.genai.Client` directly.
+2. **Parameter translation loss**: a `max_completion_tokens` rewrite breaks OpenAI-compatible providers (DeepSeek, Kimi, Mistral) that expect `max_tokens`.
3. **Endpoint control**: OpenAI-compatible endpoint URLs are declared in
`server/config/llm_defaults.json` and registered through `_compat.py`.
Adding one requires the JSON entry plus one `_COMPAT_PROVIDERS` entry, not a
@@ -61,7 +60,7 @@ The native layer currently supports **13 providers**, grouped by implementation:
|---|---|---|---|
| `anthropic` | `providers/anthropic.py` | `anthropic` | Extended thinking via `budget_tokens` |
| `openai` | `providers/openai.py` | `openai` | Reasoning models (o1/o3) and GPT-5 hybrid thinking |
-| `gemini` | `providers/gemini.py` | `google-genai` | Bypasses LangChain (Windows hang fix) |
+| `gemini` | `providers/gemini.py` | `google-genai` | Direct SDK (Windows hang fix) |
| `openrouter` | `providers/openrouter.py` | `openai` | Sets `HTTP-Referer` + `X-Title` headers |
| `xai` | `providers/openai.py` + base_url | `openai` | OpenAI-compatible at `api.x.ai/v1` |
| `deepseek` | `providers/openai.py` + base_url | `openai` | OpenAI-compatible at `api.deepseek.com` |
@@ -69,15 +68,15 @@ The native layer currently supports **13 providers**, grouped by implementation:
| `mistral` | `providers/openai.py` + base_url | `openai` | OpenAI-compatible |
| `ollama` | `providers/openai.py` + `{provider}_proxy` URL | `openai` (chat) + `ollama` (probe) | Local server. Validator probes via `ollama.AsyncClient.ps()` for typed `context_length` per loaded model. Runtime uses `OpenAIProvider` with `base_url={user URL}` so traffic stays on `localhost`. |
| `lmstudio` | `providers/openai.py` + `{provider}_proxy` URL | `openai` (chat) + `lmstudio` (probe) | Local server. Validator probes via `lmstudio.AsyncClient.llm.list_loaded()` for typed `LlmInstanceInfo.context_length`. Same OpenAI-compat runtime path as Ollama. |
-| `groq` | `providers/_compat.py` + base_url | `openai` | OpenAI-compatible (chat-path LangChain fallback retired in Phase D) |
-| `cerebras` | `providers/_compat.py` + base_url | `openai` | OpenAI-compatible (chat-path LangChain fallback retired in Phase D) |
+| `groq` | `providers/_compat.py` + base_url | `openai` | OpenAI-compatible |
+| `cerebras` | `providers/_compat.py` + base_url | `openai` | OpenAI-compatible |
| `sarvam` | `providers/_compat.py` + base_url | `openai` | Indic-first (`sarvam-105b` 128K, `sarvam-30b` 64K) at `api.sarvam.ai/v1`. Ships **no model-list route**, so it sets `supports_model_listing: false` — see below. Reasoning is on by default and returns in `reasoning_content`, which `OpenAIProvider._normalize` already reads. |
Source of truth for this list: `server/config/llm_defaults.json` (the `providers` dict) and the `register_provider(ProviderSpec(...))` calls in `server/services/llm/providers/` (each provider module registers itself at import; the legacy `factory.py` was removed).
### Native chat path vs agent dropdown — two different counts
-- **Native chat path (`execute_chat` / `fetch_models`) supports 13 providers, all native** — Anthropic and Gemini via their own SDKs; OpenAI and OpenRouter via the openai SDK; and 9 more through the shared OpenAI-compatible client with a per-provider `base_url` (xai, deepseek, kimi, mistral, groq, cerebras, ollama, lmstudio, sarvam — registered in `providers/_compat.py`). `execute_chat` delegates every provider to `ChatUnifier.chat`; there is no per-provider branch and no chat-path LangChain fallback. `xai` lives here.
+- **Native chat path (`execute_chat` / `fetch_models`) supports 13 providers, all native** — Anthropic and Gemini via their own SDKs; OpenAI and OpenRouter via the openai SDK; and 9 more through the shared OpenAI-compatible client with a per-provider `base_url` (xai, deepseek, kimi, mistral, groq, cerebras, ollama, lmstudio, sarvam — registered in `providers/_compat.py`). `execute_chat` delegates every provider to `ChatUnifier.chat`; there is no per-provider branch. `xai` lives here.
- **The agent dropdown exposes the same 13 providers** for `aiAgent`,
`chatAgent` (Zeenie), and all specialized agents, including `xai`.
`test_plugin_shape.py` asserts exact set equality between the registry and
@@ -268,11 +267,9 @@ ChatUnifier.chat(provider=..., Message[], ToolDef[])
`run_native_agent_loop` appends the provider's exact assistant envelope before
executing tools. This preserves Gemini thought signatures, Anthropic signed and
redacted thinking blocks, and OpenAI reasoning continuation state across
-in-process and Temporal turns. A markerless prepare result identifies a
-pre-cutover history and selects the legacy branch. New executions normally
-record `llm_engine=native` and wire version 2; the temporary emergency setting
-can instead record `llm_engine=langchain`. Either recorded choice remains
-deterministic on replay.
+in-process and Temporal turns. Executions record `llm_engine=native` and wire
+version 2. A markerless prepare result identifies a pre-cutover history, which
+is refused rather than executed (see the end of this document).
## Thinking and Reasoning
@@ -383,10 +380,8 @@ AI providers support optional proxy-based authentication — requests route thro
**Configuration:** proxy URLs are stored in the credentials DB under the `{provider}_proxy` pattern (e.g., `anthropic_proxy`, `openai_proxy`). Falls back to direct API key if no proxy configured. This is the SAME mechanism the native Ollama / LM Studio path uses (see "Local LLM Providers" above) — the validator persists the user's server URL under `{provider}_proxy`, and at runtime it carries into `OpenAIProvider`'s `base_url`.
-For every new chat and agent run, `proxy_url` flows through `ChatUnifier` into
-the cached native provider factory. The old `create_model()` proxy path exists
-only for markerless pre-cutover histories or emergency executions whose
-prepare result is explicitly pinned to `llm_engine=langchain`.
+For every chat and agent run, `proxy_url` flows through `ChatUnifier` into the
+cached native provider factory.
**Use cases:** Claude Code CLI proxy for Anthropic models; native Ollama / LM Studio support; custom auth proxies; dev/testing with mock servers.
@@ -486,17 +481,16 @@ the plugin folder or add a `visuals.json` entry (`"openrouterChatModel":
## Temporal migration and dependency lifecycle
-`agent.prepare_payload.v1` records both `llm_engine` and
-`message_wire_version`. New histories default to `native`/`2`; histories whose
-recorded result lacks those fields deterministically keep the legacy activity
-payload and LangChain replay path. `AGENT_LLM_ENGINE=langchain` is an emergency
-switch for newly prepared runs only.
-
-The remaining LangChain adapter pins in `server/pyproject.toml` are therefore a
-temporary deployment compatibility requirement, not a runtime dependency of
-new executions. Remove the legacy activity branch, compatibility factory and
-pins together only after Temporal visibility confirms that no retained or
-retryable history can still select the legacy engine. The source-boundary test
-keeps `langchain*` confined to the three explicitly allowlisted legacy bridge
-modules, rejects `langgraph*`/`langsmith` everywhere else, and rejects
-`deepagents` throughout production during that drain window.
+`agent.prepare_payload.v1` records `llm_engine` and `message_wire_version`.
+The marker is a discriminator, not a switch: there is one engine.
+
+A history recorded before the native cutover carries neither field, and its
+messages are in a retired wire format the native `messages_from_wire` reader
+cannot interpret. `execute_llm_step` refuses such a payload with a
+non-retryable `ApplicationError(type="InvalidAgentLLMEngine")` rather than
+reinterpreting the messages and corrupting the conversation. The operator fix
+is to Reset the deployment, which starts a fresh generation.
+
+`tests/llm/test_langchain_removed.py` enforces the dependency end state via an
+AST walk over production sources, the `pyproject.toml` declarations, runtime
+importability, and a guarded `core.container` boot.
diff --git a/server/nodes/model/_credentials.py b/server/nodes/model/_credentials.py
index c592583b..87242106 100644
--- a/server/nodes/model/_credentials.py
+++ b/server/nodes/model/_credentials.py
@@ -4,7 +4,7 @@ One :class:`ApiKeyCredential` per provider. Used by the chat-model
plugins in this folder (openai, anthropic, gemini, openrouter, groq,
cerebras, deepseek, kimi, mistral, ollama, lmstudio) plus the xAI
credential referenced by agent plugins. At execution time the plugin's
-LangChain / native SDK client pulls the key directly from
+The native SDK client pulls the key directly from
:mod:`services.auth`; this class is the Credentials-modal + discovery
manifest, not the runtime client.
diff --git a/server/nodes/tool/write_todos/__init__.py b/server/nodes/tool/write_todos/__init__.py
index fde81a91..ec46c972 100644
--- a/server/nodes/tool/write_todos/__init__.py
+++ b/server/nodes/tool/write_todos/__init__.py
@@ -123,7 +123,7 @@ class WriteTodosNode(ToolNode):
# call leaked TodoService's raw JSON STRING into this key, which
# the Output validation now rejects; the LLM-facing serialization
# happens downstream anyway (``_serialise_tool_result`` /
- # LangChain dump the whole dict to JSON).
+ # providers dump the whole dict to JSON).
return {
"message": f"Updated todo list ({len(stored)} items)",
"todos": stored,
diff --git a/server/pyproject.toml b/server/pyproject.toml
index 09b5854e..fa1fd7f2 100644
--- a/server/pyproject.toml
+++ b/server/pyproject.toml
@@ -66,18 +66,8 @@ dependencies = [
"anthropic==0.96.0",
"google-genai==2.11.0",
- # Temporary pre-cutover Temporal-history compatibility. New chat and agent
- # executions do not import these adapters. Remove this block together with
- # the legacy activity branch after all recorded histories have drained.
- "langchain-core==1.4.9",
- "langchain-openai==1.1.14",
- "langchain-anthropic==1.4.6",
- "langchain-google-genai==4.2.5",
- "langchain-groq==1.1.2",
- "langchain-cerebras==0.8.2",
-
# Pydantic 2.13 changed validator ordering; pin to main's 2.12 line
- # to keep AIMessage construction/tool-schema generation identical.
+ # to keep tool-schema generation identical.
"pydantic==2.12.5",
# Dependency Injection
diff --git a/server/requirements.txt b/server/requirements.txt
index 449a0151..ecb33830 100644
--- a/server/requirements.txt
+++ b/server/requirements.txt
@@ -14,17 +14,14 @@ annotated-types==0.7.0
# via pydantic
anthropic==0.96.0
# via
- # langchain-anthropic
# opencompany-server
# rlms
anyio==4.14.2
# via
# anthropic
# google-genai
- # groq
# httpx
# httpx-ws
- # langsmith
# lmstudio
# mcp
# openai
@@ -91,8 +88,6 @@ distro==1.9.0
# via
# anthropic
# google-genai
- # groq
- # langsmith
# openai
# portkey-ai
dnspython==2.8.0
@@ -109,8 +104,6 @@ fastapi==0.140.13
# via opencompany-server
filelock==3.32.0
# via tldextract
-filetype==1.2.0
- # via langchain-google-genai
frozenlist==1.8.0
# via
# aiohttp
@@ -135,7 +128,6 @@ google-auth-oauthlib==1.4.0
# via opencompany-server
google-genai==2.11.0
# via
- # langchain-google-genai
# opencompany-server
# rlms
googleapis-common-protos==1.75.0
@@ -146,8 +138,6 @@ greenlet==3.5.3
# via
# opencompany-server
# sqlalchemy
-groq==0.37.1
- # via langchain-groq
h11==0.16.0
# via
# httpcore
@@ -176,9 +166,7 @@ httpx==0.28.1
# anthropic
# ddgs
# google-genai
- # groq
# httpx-ws
- # langsmith
# lmstudio
# mcp
# ollama
@@ -211,38 +199,10 @@ jiter==0.16.0
# anthropic
# openai
# portkey-ai
-jsonpatch==1.33
- # via langchain-core
-jsonpointer==3.1.1
- # via jsonpatch
jsonschema==4.26.0
# via mcp
jsonschema-specifications==2025.9.1
# via jsonschema
-langchain-anthropic==1.4.6
- # via opencompany-server
-langchain-cerebras==0.8.2
- # via opencompany-server
-langchain-core==1.4.9
- # via
- # langchain-anthropic
- # langchain-cerebras
- # langchain-google-genai
- # langchain-groq
- # langchain-openai
- # opencompany-server
-langchain-google-genai==4.2.5
- # via opencompany-server
-langchain-groq==1.1.2
- # via opencompany-server
-langchain-openai==1.1.14
- # via
- # langchain-cerebras
- # opencompany-server
-langchain-protocol==0.0.18
- # via langchain-core
-langsmith==0.10.2
- # via langchain-core
lmstudio==1.5.0
# via opencompany-server
lxml==6.1.1
@@ -275,7 +235,6 @@ ollama==0.6.2
# via opencompany-server
openai==2.45.0
# via
- # langchain-openai
# opencompany-server
# rlms
opentelemetry-api==1.44.0
@@ -288,13 +247,9 @@ opentelemetry-sdk==1.44.0
opentelemetry-semantic-conventions==0.65b0
# via opentelemetry-sdk
orjson==3.11.9
- # via
- # langsmith
- # opencompany-server
+ # via opencompany-server
packaging==26.2
# via
- # langchain-core
- # langsmith
# pooch
# pytest
pillow==12.3.0
@@ -342,11 +297,6 @@ pydantic==2.12.5
# crawlee
# fastapi
# google-genai
- # groq
- # langchain-anthropic
- # langchain-core
- # langchain-google-genai
- # langsmith
# mcp
# ollama
# openai
@@ -400,7 +350,6 @@ pywinpty==3.0.5 ; sys_platform == 'win32'
# via opencompany-server
pyyaml==6.0.3
# via
- # langchain-core
# opencompany-server
# uvicorn
qrcode==8.2
@@ -409,21 +358,16 @@ referencing==0.37.0
# via
# jsonschema
# jsonschema-specifications
-regex==2026.7.10
- # via tiktoken
requests==2.34.2
# via
# google-api-core
# google-auth
# google-genai
# googlemaps
- # langsmith
# pooch
# requests-file
# requests-oauthlib
- # requests-toolbelt
# rlms
- # tiktoken
# tldextract
# xdk
requests-file==3.0.1
@@ -432,8 +376,6 @@ requests-oauthlib==2.0.0
# via
# google-auth-oauthlib
# xdk
-requests-toolbelt==1.0.0
- # via langsmith
rich==15.0.0
# via rlms
rlms==0.1.3
@@ -448,8 +390,6 @@ sniffio==1.3.1
# via
# anthropic
# google-genai
- # groq
- # langsmith
# openai
# portkey-ai
socksio==1.0.0
@@ -472,13 +412,9 @@ structlog==26.1.0
temporalio==1.30.0
# via opencompany-server
tenacity==9.1.4
- # via
- # google-genai
- # langchain-core
+ # via google-genai
text-unidecode==1.3
# via python-slugify
-tiktoken==0.13.0
- # via langchain-openai
tinytag==2.2.1
# via opencompany-server
tldextract==5.3.1
@@ -502,10 +438,6 @@ typing-extensions==4.16.0
# dependency-injector
# fastapi
# google-genai
- # groq
- # langchain-core
- # langchain-protocol
- # langsmith
# lmstudio
# mcp
# nexus-rpc
@@ -538,10 +470,6 @@ urllib3==2.7.0
# via
# requests
# types-requests
-uuid-utils==0.17.0
- # via
- # langchain-core
- # langsmith
uvicorn==0.51.0
# via
# mcp
@@ -555,18 +483,13 @@ webencodings==0.5.1
websockets==16.1.1
# via
# google-genai
- # langsmith
# opencompany-server
# uvicorn
wsproto==1.3.2
# via httpx-ws
xdk==0.9.0
# via opencompany-server
-xxhash==3.8.1
- # via langsmith
yarl==1.24.2
# via
# aiohttp
# crawlee
-zstandard==0.25.0
- # via langsmith
diff --git a/server/services/agent_runtime.py b/server/services/agent_runtime.py
index ef1cf177..e8cce5ec 100644
--- a/server/services/agent_runtime.py
+++ b/server/services/agent_runtime.py
@@ -2,9 +2,6 @@
This module is the shared boundary between orchestration (the in-process
``AIService`` loop and Temporal activities) and the native provider SDK layer.
-It deliberately has no eager LangChain imports. ``AgentToolSpec.to_langchain``
-exists only so Temporal histories recorded before the native cutover can drain;
-it is lazy and is removed with the legacy activity branch.
"""
from __future__ import annotations
@@ -52,21 +49,6 @@ class AgentToolSpec:
def parameters(self) -> Dict[str, Any]:
return self.definition.parameters
- def to_langchain(self):
- """Build the legacy adapter used only by pre-cutover Temporal runs."""
-
- from langchain_core.tools import StructuredTool
-
- def _placeholder(**kwargs):
- return kwargs
-
- return StructuredTool.from_function(
- name=self.name,
- description=self.description,
- func=_placeholder,
- args_schema=self.args_schema,
- )
-
def _tool_definition(tool: ToolDef | AgentToolSpec) -> ToolDef:
return tool.definition if isinstance(tool, AgentToolSpec) else tool
diff --git a/server/services/ai.py b/server/services/ai.py
index 614c3b7b..a29f1986 100644
--- a/server/services/ai.py
+++ b/server/services/ai.py
@@ -1,8 +1,7 @@
"""AI service backed by the native provider SDK layer.
-The LangChain model factory and loop below are retained temporarily and are
-used only by pre-cutover Temporal histories. Every new in-process or durable
-agent execution uses :mod:`services.agent_runtime`.
+Every in-process and durable agent execution goes through
+:mod:`services.agent_runtime` and :mod:`services.llm`.
"""
from __future__ import annotations
@@ -13,19 +12,6 @@ from dataclasses import dataclass
from datetime import datetime
from typing import Awaitable, Dict, Any, List, Optional, Callable, Type, TYPE_CHECKING
-# ---------------------------------------------------------------------------
-# LangChain imports — fully lazy and retained only for old Temporal histories.
-#
-# Cold-start measurement (Windows, fresh disk):
-# ``from langchain_openai import ChatOpenAI`` ~20.8s (pulls openai SDK + tiktoken)
-# Deferring the heavy imports until the first agent run trims server-ready time.
-#
-# ``services/llm/`` and ``services/agent_runtime.py`` handle every new chat
-# and agent execution. The imports below are type-checking-only or function-
-# local so a native process never loads LangChain unless it must execute an
-# already-recorded legacy Temporal activity.
-# ---------------------------------------------------------------------------
-
import json
# ``NodeUserError`` is the framework's "user-correctable, no-traceback"
@@ -35,82 +21,13 @@ from services.plugin import NodeUserError
from services.tool_identity import DuplicateToolNameError, ensure_unique_tool_names
if TYPE_CHECKING:
- from langchain_openai import ChatOpenAI # noqa: F401
- from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage, ToolMessage # noqa: F401
- from langchain_core.tools import StructuredTool # noqa: F401
from pydantic import BaseModel, Field, create_model # noqa: F401
- from langchain_anthropic import ChatAnthropic # noqa: F401
- from langchain_groq import ChatGroq # noqa: F401
- from langchain_cerebras import ChatCerebras # noqa: F401
-
-
-def _get_chat_anthropic() -> Type:
- """Lazy import: ``langchain_anthropic`` is ~800ms cold."""
- from langchain_anthropic import ChatAnthropic
-
- return ChatAnthropic
-
-
-def _get_chat_groq() -> Type:
- """Lazy import: ``langchain_groq`` adds ~270ms cold."""
- from langchain_groq import ChatGroq
- return ChatGroq
-
-
-def _get_chat_cerebras() -> Optional[Type]:
- """Lazy import: ``langchain_cerebras`` is optional (Python <3.13).
-
- Returns the class if importable, ``None`` otherwise. Cached so the
- ImportError path runs once.
- """
- try:
- from langchain_cerebras import ChatCerebras
-
- return ChatCerebras
- except ImportError:
- return None
-
-# Lazy import for Google GenAI (google-generativeai init hangs on Windows/Python 3.13)
-def _get_google_genai_class() -> Type:
- from langchain_google_genai import ChatGoogleGenerativeAI
-
- return ChatGoogleGenerativeAI
-
-
-def _is_cerebras_available() -> bool:
- """Whether the langchain-cerebras package is importable."""
- return _get_chat_cerebras() is not None
-
-
-# Backwards-compat shims (PEP 562 module-level __getattr__) for code that
-# historically read these as module-level constants. The "import" cost is paid
-# only on first access. ``PROVIDER_CONFIGS`` resolves through ``get_provider_configs()``
-# so any external `from services.ai import PROVIDER_CONFIGS` keeps working
-# without triggering the heavy LangChain imports until first use.
+# Backwards-compat shim (PEP 562 module-level ``__getattr__``) for code that
+# historically read ``PROVIDER_CONFIGS`` as a module-level constant here. It
+# resolves to the native config map.
def __getattr__(name: str):
- if name == "ChatAnthropic":
- return _get_chat_anthropic()
- if name == "ChatGroq":
- return _get_chat_groq()
- if name == "ChatCerebras":
- return _get_chat_cerebras()
- if name == "CEREBRAS_AVAILABLE":
- return _is_cerebras_available()
- if name == "CEREBRAS_IMPORT_ERROR":
- # Reproduce the error string without re-raising; helpful for log messages.
- if _get_chat_cerebras() is not None:
- return None
- try:
- __import__("langchain_cerebras")
- return None
- except ImportError as e:
- return str(e)
if name == "PROVIDER_CONFIGS":
return NATIVE_PROVIDER_CONFIGS
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
@@ -227,78 +144,10 @@ async def _get_configured_memory_vector_store(
# =============================================================================
-@dataclass
-class ProviderConfig:
- """Configuration for an AI provider."""
-
- name: str
- model_class: Optional[Type]
- api_key_param: str # Parameter name for API key in model constructor
- max_tokens_param: str # Parameter name for max tokens
- detection_patterns: tuple # Patterns to detect this provider from model name
- default_model: str # Default model when none specified
- models_endpoint: str # API endpoint to fetch models
- models_header_fn: Callable[[str], dict] # Function to create headers
- base_url: str = "" # Custom base URL for OpenAI-compatible providers
- extra_headers: Optional[Dict[str, str]] = None # Default headers for API calls
- supported_params: Optional[List[str]] = None # API-supported params (from llm_defaults.json)
-
-
# Compatibility export. Native configuration is the single source of truth.
ThinkingConfig = NativeThinkingConfig
-def _openai_headers(api_key: str) -> dict:
- return {"Authorization": f"Bearer {api_key}"}
-
-
-def _anthropic_headers(api_key: str) -> dict:
- return {"x-api-key": api_key, "anthropic-version": "2023-06-01"}
-
-
-def _gemini_headers(api_key: str) -> dict:
- return {} # API key in URL for Gemini
-
-
-def _openrouter_headers(api_key: str) -> dict:
- return {"Authorization": f"Bearer {api_key}", "HTTP-Referer": "http://localhost:5678", "X-Title": "OpenCompany"}
-
-
-def _groq_headers(api_key: str) -> dict:
- return {"Authorization": f"Bearer {api_key}"}
-
-
-def _cerebras_headers(api_key: str) -> dict:
- return {"Authorization": f"Bearer {api_key}"}
-
-
-def _bearer_headers(api_key: str) -> dict:
- """Generic Bearer auth headers for OpenAI-compatible providers."""
- return {"Authorization": f"Bearer {api_key}"}
-
-
-# Map of provider -> (model_class_factory, header_fn) for providers with custom
-# LangChain classes. Factories let us defer the heavy ``langchain_anthropic`` /
-# ``langchain_groq`` / ``langchain_cerebras`` imports until an agent actually
-# runs. Providers NOT in this map use ChatOpenAI + _bearer_headers
-# (OpenAI-compatible). ``model_class_factory`` returns the resolved class
-# (or None for gemini, whose lazy resolver lives at the call site).
-def _build_provider_class_map() -> Dict[str, tuple]:
- from langchain_openai import ChatOpenAI
-
- mapping: Dict[str, tuple] = {
- "openai": (ChatOpenAI, _openai_headers),
- "anthropic": (_get_chat_anthropic(), _anthropic_headers),
- "gemini": (None, _gemini_headers), # resolved via _get_google_genai_class()
- "openrouter": (ChatOpenAI, _openrouter_headers),
- "groq": (_get_chat_groq(), _groq_headers),
- }
- cerebras_class = _get_chat_cerebras()
- if cerebras_class is not None:
- mapping["cerebras"] = (cerebras_class, _cerebras_headers)
- return mapping
-
-
# =============================================================================
# LLM DEFAULTS CONFIGURATION - Load from config/llm_defaults.json
# =============================================================================
@@ -349,55 +198,6 @@ def _resolve_temperature(flattened: dict, model: str, provider: str, thinking_en
)
-# Provider configurations - built from config/llm_defaults.json on first use.
-# Building the map triggers the heavy LangChain imports (~1.05s of
-# langchain_anthropic + langchain_groq), so we defer it until a caller asks
-# for a config (i.e. an AI agent actually runs). The native LLM SDK path
-# (services/llm/) bypasses this entirely.
-def _build_provider_configs() -> Dict[str, ProviderConfig]:
- """Build PROVIDER_CONFIGS from llm_defaults.json + provider-class map."""
- from langchain_openai import ChatOpenAI
-
- providers = _LLM_DEFAULTS.get("providers", {})
- configs: Dict[str, ProviderConfig] = {}
- class_map = _build_provider_class_map()
- cerebras_available = _is_cerebras_available()
-
- for name, prov in providers.items():
- # Skip cerebras if not available
- if name == "cerebras" and not cerebras_available:
- logger.warning("Cerebras provider not available: langchain-cerebras package not installed (Python <3.13 only)")
- continue
-
- model_class, header_fn = class_map.get(name, (ChatOpenAI, _bearer_headers))
- configs[name] = ProviderConfig(
- name=name,
- model_class=model_class,
- api_key_param=prov.get("api_key_param", "api_key"),
- max_tokens_param=prov.get("max_tokens_param", "max_tokens"),
- detection_patterns=tuple(prov.get("detection_patterns", [name])),
- default_model=prov.get("default_model", ""),
- models_endpoint=prov.get("models_endpoint", ""),
- models_header_fn=header_fn,
- base_url=prov.get("base_url", ""),
- extra_headers=prov.get("extra_headers"),
- supported_params=prov.get("supported_params"),
- )
-
- return configs
-
-
-def get_provider_configs() -> Dict[str, ProviderConfig]:
- """Return PROVIDER_CONFIGS, building on first call (cached thereafter).
-
- Lazy build defers the LangChain agent-class imports until the first
- AI agent execution — saves ~1.0s of cold-start time on the
- chat-only path used by ``execute_chat()``.
- """
- return _build_provider_configs()
-
-
def detect_provider_from_model(model: str) -> str:
"""Detect AI provider from model name using registry patterns."""
return native_detect_provider_from_model(model)
@@ -473,298 +273,6 @@ def is_valid_message_content(content: Any) -> bool:
return bool(content)
-def filter_empty_messages(messages: List[Any]) -> List[Any]:
- """Compatibility wrapper around the provider-neutral message filter."""
-
- return _filter_native_messages(messages)
-
-
-def extract_thinking_from_response(response) -> tuple:
- """Extract text and thinking content from LLM response.
-
- Handles multiple formats:
- - LangChain content_blocks API (Claude, Gemini)
- - OpenAI responses/v1 format (content list with reasoning blocks containing summary)
- - Groq additional_kwargs.reasoning_content
- - Raw string content
-
- Returns:
- Tuple of (text_content: str, thinking_content: Optional[str])
- """
- if isinstance(response, LLMResponse):
- return response.content, response.thinking
-
- text_parts = []
- thinking_parts = []
-
- logger.debug(f"[extract_thinking] Starting extraction, response type: {type(response).__name__}")
- logger.debug(
- f"[extract_thinking] has content_blocks: {hasattr(response, 'content_blocks')}, value: {getattr(response, 'content_blocks', None)}"
- )
- logger.debug(f"[extract_thinking] has content: {hasattr(response, 'content')}, type: {type(getattr(response, 'content', None))}")
- logger.debug(
- f"[extract_thinking] has additional_kwargs: {hasattr(response, 'additional_kwargs')}, value: {getattr(response, 'additional_kwargs', None)}"
- )
- logger.debug(
- f"[extract_thinking] has response_metadata: {hasattr(response, 'response_metadata')}, keys: {list(getattr(response, 'response_metadata', {}).keys()) if hasattr(response, 'response_metadata') else None}"
- )
-
- # Use content_blocks API (LangChain 1.0+) for Claude/Gemini
- if hasattr(response, "content_blocks") and response.content_blocks:
- for block in response.content_blocks:
- if isinstance(block, dict):
- block_type = block.get("type", "")
- if block_type == "reasoning":
- thinking_parts.append(block.get("reasoning", ""))
- elif block_type == "thinking":
- thinking_parts.append(block.get("thinking", ""))
- elif block_type == "text":
- text_parts.append(block.get("text", ""))
-
- # Check additional_kwargs for reasoning_content (Groq, older OpenAI responses)
- if not thinking_parts and hasattr(response, "additional_kwargs"):
- reasoning = response.additional_kwargs.get("reasoning_content")
- if reasoning:
- thinking_parts.append(reasoning)
-
- # Check response_metadata for OpenAI o-series reasoning (responses/v1 format)
- # The output array contains reasoning items with summaries
- if not thinking_parts and hasattr(response, "response_metadata"):
- metadata = response.response_metadata
- output = metadata.get("output", [])
- if isinstance(output, list):
- for item in output:
- if isinstance(item, dict) and item.get("type") == "reasoning":
- summary = item.get("summary", [])
- if isinstance(summary, list):
- for s in summary:
- if isinstance(s, dict):
- # Handle both summary_text and text types
- text = s.get("text", "")
- if text:
- thinking_parts.append(text)
- elif isinstance(s, str):
- thinking_parts.append(s)
-
- # Check raw content for OpenAI responses/v1 format and other list formats
- if hasattr(response, "content"):
- content = response.content
- if isinstance(content, str):
- if not text_parts:
- text_parts.append(content)
- elif isinstance(content, list):
- for block in content:
- if isinstance(block, dict):
- block_type = block.get("type", "")
- if block_type == "text" or block_type == "output_text":
- # Handle both 'text' and 'output_text' (responses/v1 format)
- if not text_parts: # Only add if not already extracted
- text_parts.append(block.get("text", ""))
- elif block_type == "reasoning":
- # OpenAI responses/v1 format: reasoning block with summary array
- # Format: {"type": "reasoning", "summary": [{"type": "text", "text": "..."}, {"type": "summary_text", "text": "..."}]}
- summary = block.get("summary", [])
- if isinstance(summary, list):
- for s in summary:
- if isinstance(s, dict):
- s_type = s.get("type", "")
- if s_type in ("text", "summary_text"):
- thinking_parts.append(s.get("text", ""))
- elif isinstance(s, str):
- thinking_parts.append(s)
- elif isinstance(summary, str):
- thinking_parts.append(summary)
- # Also check direct reasoning field
- if block.get("reasoning"):
- thinking_parts.append(block.get("reasoning", ""))
- elif block_type == "thinking":
- thinking_parts.append(block.get("thinking", ""))
- elif isinstance(block, str) and not text_parts:
- text_parts.append(block)
-
- text = "\n".join(filter(None, text_parts))
- thinking = "\n".join(filter(None, thinking_parts)) if thinking_parts else None
-
- logger.debug(f"[extract_thinking] Final text_parts: {text_parts}")
- logger.debug(f"[extract_thinking] Final thinking_parts: {thinking_parts}")
- logger.debug(
- f"[extract_thinking] Returning text={repr(text[:100] if text else None)}, thinking={repr(thinking[:100] if thinking else None)}"
- )
-
- return text, thinking
-
-
-async def _run_agent_loop(
- chat_model,
- initial_messages: List[BaseMessage],
- *,
- tools: Optional[List[Any]] = None,
- tool_executor: Optional[Callable] = None,
- max_iterations: int = 500,
- progress_callback: Optional[Callable[[int], Any]] = None,
- rebind_from_operations: Optional[Callable[[List[Dict[str, Any]]], Awaitable[List[Any]]]] = None,
-) -> Dict[str, Any]:
- """Drive an LLM agent loop with optional tool calling.
-
- Plain-async ``while iteration < max:`` loop. Each iteration:
-
- 1. Invoke ``chat_model.ainvoke(filtered_messages)`` and append the
- returned assistant message verbatim. Appending the message object
- itself preserves Gemini ``thought_signature``, Anthropic cache
- markers, OpenAI ``reasoning_content``, etc.
- 2. Extract thinking content; accumulate across iterations with the
- ``--- Iteration N ---`` separator.
- 3. If the response carries ``tool_calls``, dispatch each via the
- supplied ``tool_executor`` and append a ``ToolMessage`` per call.
- Executor failures get surfaced to the model as an error JSON so
- the loop can recover rather than abort.
- 4. If no tool calls, return.
-
- On hitting ``max_iterations``, append a terminal ``AIMessage`` with
- a truncation note so downstream extraction has a usable response.
-
- ``progress_callback(iteration)`` is awaited at the top of each turn
- so callers can drive per-iteration broadcasts (e.g. the FE iteration
- badge).
-
- Returns ``{messages, iteration, thinking_content, truncated}``.
- ``messages`` is the full accumulated list (system + history +
- interleaved AI / Tool messages); callers extract the last AIMessage
- as the final response.
- """
- from langchain_core.messages import AIMessage, ToolMessage
-
- # Local mutable tools list so canvas-mutating tools (agentBuilder)
- # can extend the bound surface mid-loop via ``rebind_from_operations``.
- current_tools: List[Any] = list(tools or [])
- model = chat_model.bind_tools(current_tools) if current_tools else chat_model
- messages: List[BaseMessage] = list(initial_messages)
- thinking_accumulated = ""
- iteration = 0
-
- for iteration in range(1, max_iterations + 1):
- if progress_callback is not None:
- try:
- await progress_callback(iteration)
- except Exception as e:
- logger.debug(f"[Agent loop] progress_callback raised: {e}")
-
- filtered = filter_empty_messages(messages)
- response = await model.ainvoke(filtered)
- messages.append(response)
-
- _, new_thinking = extract_thinking_from_response(response)
- if new_thinking:
- if thinking_accumulated:
- thinking_accumulated = f"{thinking_accumulated}\n\n--- Iteration {iteration} ---\n{new_thinking}"
- else:
- thinking_accumulated = new_thinking
-
- # Gemini occasionally returns a blocked / safety-stopped response;
- # surface the cause so operators can spot it in the logs.
- if hasattr(response, "response_metadata"):
- meta = response.response_metadata or {}
- if meta.get("finish_reason") == "SAFETY":
- logger.warning("[Agent loop] Gemini response blocked by safety filters")
- if meta.get("block_reason"):
- logger.warning(f"[Agent loop] Gemini block reason: {meta.get('block_reason')}")
-
- tool_calls = getattr(response, "tool_calls", None) or []
- if not tool_calls:
- return {
- "messages": messages,
- "iteration": iteration,
- "thinking_content": thinking_accumulated or None,
- "truncated": False,
- }
-
- if tool_executor is None:
- logger.warning(
- "[Agent loop] LLM emitted %d tool_call(s) but no tool_executor " "configured; treating response as final",
- len(tool_calls),
- )
- return {
- "messages": messages,
- "iteration": iteration,
- "thinking_content": thinking_accumulated or None,
- "truncated": False,
- }
-
- iteration_new_tools: List[Any] = []
- for call in tool_calls:
- tool_name = call.get("name", "")
- tool_args = call.get("args", {}) or {}
- tool_id = call.get("id", "")
- try:
- result = await tool_executor(tool_name, tool_args)
- except Exception as e:
- logger.error(f"[Agent loop] Tool {tool_name!r} raised: {e}")
- result = {"error": str(e)}
-
- # Canvas-mutation rebind: any tool can return an ``operations``
- # field (workflow_ops protocol). When the agent loop has a
- # ``rebind_from_operations`` callback wired AND the toggle is
- # on, build new StructuredTools off the ops list so the LLM
- # can call them in the very next iteration without a restart.
- if (
- rebind_from_operations is not None
- and isinstance(result, dict)
- and result.get("operations")
- ):
- try:
- added = await rebind_from_operations(result["operations"])
- if added:
- iteration_new_tools.extend(added)
- except DuplicateToolNameError as exc:
- # Reject the whole hot-rebind batch before mutating the
- # bound surface. Return the structured conflict to the
- # model so it can rename/rebuild the ambiguous tool.
- logger.warning("[Agent loop] rejected ambiguous tool rebind: %s", exc)
- result = dict(result)
- result["rebind_error"] = exc.as_dict()
- except Exception as exc: # noqa: BLE001 — defensive; rebind is opt-in
- logger.warning(
- "[Agent loop] rebind_from_operations raised: %s", exc, exc_info=True
- )
-
- messages.append(
- ToolMessage(
- content=json.dumps(result, default=str),
- tool_call_id=tool_id,
- name=tool_name,
- )
- )
-
- if iteration_new_tools:
- current_tools.extend(iteration_new_tools)
- model = chat_model.bind_tools(current_tools)
- logger.info(
- "[Agent loop] rebound %d tool(s) after canvas mutation (total bound=%d)",
- len(iteration_new_tools),
- len(current_tools),
- )
-
- # Loop exited without returning -- hit max_iterations. Append a
- # terminal AIMessage so downstream extraction (and post-loop token
- # tracking / memory persistence) still have a usable state.
- messages.append(
- AIMessage(
- content=(
- f"[Recursion limit reached: {max_iterations} iterations. "
- f"Adjust agent.recursion_limit in llm_defaults.json or "
- f"simplify the task.]"
- )
- )
- )
- logger.warning(f"[Agent loop] max_iterations hit ({max_iterations}); " f"returning partial response with {len(messages)} message(s)")
- return {
- "messages": messages,
- "iteration": iteration,
- "thinking_content": thinking_accumulated or None,
- "truncated": True,
- }
-
-
def _build_skill_system_prompt(skill_data: List[Dict[str, Any]], log_prefix: str = "[Agent]") -> tuple:
"""Build skill injection text for the system message.
@@ -788,7 +296,7 @@ def _build_skill_system_prompt(skill_data: List[Dict[str, Any]], log_prefix: str
class AIService:
- """AI model service for LangChain operations."""
+ """AI model service backed by the native provider SDK layer."""
def __init__(
self,
@@ -857,7 +365,7 @@ class AIService:
if isinstance(content, str) and content.strip():
return content
- # LangChain standard: use content_blocks property for typed content extraction
+ # Use the content_blocks property for typed content extraction
if ai_response and hasattr(ai_response, "content_blocks") and ai_response.content_blocks:
text_parts = []
for block in ai_response.content_blocks:
@@ -937,7 +445,7 @@ class AIService:
) -> Optional[Dict[str, Any]]:
"""Track token usage and trigger compaction if threshold exceeded.
- Extracts usage_metadata from LangChain AIMessage and tracks it
+ Extracts usage_metadata from the assistant message and tracks it
in the compaction service for session token monitoring.
Args:
@@ -962,8 +470,9 @@ class AIService:
if not svc:
return
- # Native responses expose a normalized ``Usage`` object. The
- # LangChain branches below are retained only for pre-cutover histories.
+ # Native responses expose a normalized ``Usage`` object. The
+ # attribute probes below stay because callers also hand this method
+ # raw provider payloads, which carry usage under either name.
usage = None
if isinstance(ai_response, Usage):
usage = ai_response
@@ -1044,154 +553,6 @@ class AIService:
return None
- def create_model(
- self,
- provider: str,
- api_key: str,
- model: str,
- temperature: float,
- max_tokens: int,
- thinking: Optional[ThinkingConfig] = None,
- proxy_url: Optional[str] = None,
- ):
- """Create LangChain model instance using provider registry.
-
- Args:
- provider: AI provider name (openai, anthropic, gemini, groq, openrouter)
- api_key: Provider API key
- model: Model name/ID
- temperature: Sampling temperature
- max_tokens: Maximum response tokens
- thinking: Optional thinking/reasoning configuration
- proxy_url: Optional proxy URL (Ollama-style auth delegation)
-
- Returns:
- Configured LangChain chat model instance
- """
- from langchain_openai import ChatOpenAI
-
- config = get_provider_configs().get(provider)
- if not config:
- # Provide helpful error for Cerebras if import failed
- if provider == "cerebras" and not _is_cerebras_available():
- error_msg = "Cerebras provider not available: langchain-cerebras package not installed"
- logger.error(error_msg)
- raise ValueError(error_msg)
- raise ValueError(f"Unsupported provider: {provider}")
-
- # Strip [FREE] prefix if present (added by OpenRouter model list for display)
- if model.startswith("[FREE] "):
- model = model[7:]
-
- # Strip owner prefix for non-OpenRouter providers (e.g. "openai/gpt-oss-120b" → "gpt-oss-120b")
- if provider != "openrouter" and "/" in model:
- model = model.split("/", 1)[-1]
-
- # Build kwargs dynamically from registry config
- kwargs = {config.api_key_param: api_key, "model": model, "temperature": temperature, config.max_tokens_param: max_tokens}
-
- # Agent Platform / Vertex Express keys ("AQ." prefix) route the same
- # ChatGoogleGenerativeAI to the Vertex backend — billed to the key's
- # GCP project instead of personal AI Studio credits. The library
- # handles endpoint construction and key transport.
- if provider == "gemini" and is_vertex_express_key(api_key):
- kwargs["vertexai"] = True
- if proxy_url:
- logger.warning("[AI] gemini: proxy_url ignored in Vertex AI mode")
- proxy_url = None
-
- # Proxy mode: route through local proxy that handles auth (Ollama pattern)
- # Proxy URL stored as {provider}_proxy credential
- if proxy_url:
- kwargs["base_url"] = proxy_url
- kwargs[config.api_key_param] = "ollama" # Ollama-style token
- logger.info(f"[AI] Using proxy for {provider}: {proxy_url}")
- # OpenAI-compatible providers: apply base_url + pass only supported params
- elif config.base_url and config.model_class == ChatOpenAI and provider != "openai":
- kwargs["base_url"] = config.base_url
- if config.extra_headers:
- kwargs["default_headers"] = config.extra_headers
- # LangChain ChatOpenAI converts max_tokens -> max_completion_tokens (OpenAI-specific).
- # Non-OpenAI providers reject this, so pass supported params via extra_body.
- del kwargs[config.max_tokens_param]
- extra_body: Dict[str, Any] = {"max_tokens": max_tokens}
- if config.supported_params:
- if "temperature" not in config.supported_params:
- kwargs.pop("temperature", None)
- if "frequency_penalty" not in config.supported_params:
- kwargs.pop("frequency_penalty", None)
- if "presence_penalty" not in config.supported_params:
- kwargs.pop("presence_penalty", None)
- # Handle provider-specific constraints from llm_defaults.json
- prov_json = _LLM_DEFAULTS.get("providers", {}).get(provider, {})
-
- # Disable thinking for models that have it ON by default (e.g. kimi-k2.5)
- default_on = prov_json.get("thinking_default_on", [])
- if default_on and any(model.startswith(m) for m in default_on):
- if not (thinking and thinking.enabled):
- extra_body["thinking"] = {"type": "disabled"}
-
- # Apply fixed temperature per model (e.g. kimi-k2.5 requires exactly 0.6)
- fixed_temps = prov_json.get("fixed_temperature", {})
- for prefix, fixed_temp in fixed_temps.items():
- if model.startswith(prefix):
- kwargs["temperature"] = fixed_temp
- break
-
- kwargs["extra_body"] = extra_body
-
- # Apply thinking/reasoning configuration using model registry
- # The registry determines thinking_type based on model metadata
- from services.model_registry import get_model_registry
-
- registry = get_model_registry()
-
- # Reasoning models (listed in ``reasoning_models`` per provider in
- # llm_defaults.json) do not support the ``temperature`` parameter —
- # the API rejects requests that send any value, including 1. Drop
- # it entirely rather than pass an explicit value.
- if registry.is_reasoning_model(model, provider):
- if "temperature" in kwargs:
- logger.info(f"[AI] Reasoning model '{model}': omitting temperature param")
- kwargs.pop("temperature", None)
-
- if thinking and thinking.enabled:
- thinking_type = registry.get_thinking_type(model, provider)
-
- if thinking_type == "budget":
- # Claude, Gemini, Cerebras: budget_tokens approach
- budget = max(1024, thinking.budget)
- if provider == "anthropic":
- # Claude: max_tokens must be > budget_tokens
- if max_tokens <= budget:
- kwargs[config.max_tokens_param] = budget + max(1024, max_tokens)
- logger.info(
- f"[AI] Claude thinking: adjusted max_tokens from {max_tokens} to {kwargs[config.max_tokens_param]} (budget={budget})"
- )
- kwargs["thinking"] = {"type": "enabled", "budget_tokens": budget}
- kwargs["temperature"] = 1
- elif provider == "gemini":
- kwargs["thinking_budget"] = thinking.budget
- kwargs["include_thoughts"] = True
- logger.info(f"[AI] Gemini thinking: budget={thinking.budget}")
- elif provider == "cerebras":
- kwargs["thinking_budget"] = thinking.budget
- logger.info(f"[AI] Cerebras thinking: budget={thinking.budget}")
- elif thinking_type == "effort":
- # OpenAI o-series: reasoning_effort parameter
- kwargs["reasoning_effort"] = thinking.effort
- kwargs["temperature"] = 1
- logger.info(f"[AI] OpenAI reasoning: effort={thinking.effort}, temperature=1")
- elif thinking_type == "format":
- # Groq Qwen/QwQ: reasoning_format parameter
- format_val = thinking.format if thinking.format in ("parsed", "hidden") else "parsed"
- kwargs["reasoning_format"] = format_val
- elif thinking_type == "none":
- logger.warning(f"[AI] Model '{model}' ({provider}) may not support thinking mode")
-
- # Resolve lazy-loaded model class (gemini)
- model_class = config.model_class or _get_google_genai_class()
- return model_class(**kwargs)
def _get_curated_models(self, provider: str) -> List[str]:
"""Get curated model list from llm_defaults.json for a provider.
@@ -1211,7 +572,7 @@ class AIService:
exception translation, and the JSON-driven ``incompatible_models``
filter. All 12 providers (4 dedicated + 8 OpenAI-compat
including groq + cerebras) route through here — Phase D removed
- the legacy LangChain fallback path.
+ the legacy per-provider fallback path.
On API failure, falls back to the curated list from
``llm_defaults.json``. When the provider has no ``default_model``
@@ -1295,9 +656,8 @@ class AIService:
# errors so ``BaseNode.execute()`` logs one WARN line with
# no traceback). No per-provider Python lives here.
#
- # The LangChain fallback for groq + cerebras was deleted in
- # Phase D — both providers now register through the
- # OpenAI-compat path in ``services.llm.providers._compat``.
+ # groq + cerebras register through the OpenAI-compat path in
+ # ``services.llm.providers._compat``.
if self.chat_unifier is None:
raise NodeUserError(
"ChatUnifier is not injected. AIService must be constructed via "
@@ -2874,7 +2234,7 @@ class AIService:
)
logger.info(f"[Agent] Enhanced tool description for {node_type} with {len(child_tools)} child tools")
- # Clean tool name (LangChain requires alphanumeric + underscores)
+ # Clean tool name (providers require alphanumeric + underscores)
import re
tool_name = re.sub(r"[^a-zA-Z0-9_]", "_", tool_name)
diff --git a/server/services/llm/config.py b/server/services/llm/config.py
index d28c08d8..45060f6e 100644
--- a/server/services/llm/config.py
+++ b/server/services/llm/config.py
@@ -1,7 +1,7 @@
"""Provider configuration and model resolution.
Loads provider metadata from config/llm_defaults.json.
-No LangChain imports -- pure config and resolution logic.
+Pure config and resolution logic.
"""
import json
diff --git a/server/services/llm/messages.py b/server/services/llm/messages.py
index ace1c757..fd3703b5 100644
--- a/server/services/llm/messages.py
+++ b/server/services/llm/messages.py
@@ -20,11 +20,11 @@ def is_valid_message_content(content: Any) -> bool:
def filter_empty_messages(messages: Sequence) -> List:
"""Filter out messages with empty content.
- Works with both LangChain BaseMessage objects and native Message dataclasses.
+ Works with native Message dataclasses and raw provider payloads.
"""
filtered = []
for m in messages:
- # Detect role (works for both LangChain and native messages)
+ # Detect role across native messages and raw provider payloads
role = getattr(m, "role", None) or getattr(m, "type", "")
# Tool messages -- always keep
diff --git a/server/services/llm/providers/_compat.py b/server/services/llm/providers/_compat.py
index 7100a771..73ff7956 100644
--- a/server/services/llm/providers/_compat.py
+++ b/server/services/llm/providers/_compat.py
@@ -34,8 +34,7 @@ logger = get_logger(__name__)
# Groq + Cerebras moved here in Phase D (May 2026). Both expose
# OpenAI-compatible `/v1` endpoints (``api.groq.com/openai/v1`` /
# ``api.cerebras.ai/v1``) so they share the ``OpenAIProvider`` factory
-# with the other compat providers. The only remaining LangChain path is the
-# temporary Temporal-history compatibility branch.
+# with the other compat providers.
_COMPAT_PROVIDERS: Tuple[str, ...] = (
"xai",
"deepseek",
diff --git a/server/services/llm/vertex.py b/server/services/llm/vertex.py
index a9478afe..9e3131d5 100644
--- a/server/services/llm/vertex.py
+++ b/server/services/llm/vertex.py
@@ -8,7 +8,7 @@ Gemini accepts two kinds of API keys:
billed to the key's GCP project (eligible for Cloud credits) via
``aiplatform.googleapis.com``.
-Both the native ``google-genai`` SDK and ``langchain-google-genai``
+The native ``google-genai`` SDK
route to the Vertex backend with ``vertexai=True`` plus the same
``api_key`` — endpoint construction, auth headers, and backend parity
are entirely handled by the official libraries. The only application
diff --git a/server/services/temporal/agent_activities.py b/server/services/temporal/agent_activities.py
index 498eafbb..0f3a92ba 100644
--- a/server/services/temporal/agent_activities.py
+++ b/server/services/temporal/agent_activities.py
@@ -27,7 +27,7 @@ and may read frozen registry dicts deterministically.
References:
- Temporal AI Cookbook -- https://docs.temporal.io/ai-cookbook
- ``temporalio.contrib.openai_agents.activity_as_tool`` mirrors this
- pattern; we re-implement manually because OpenCompany uses LangChain
+ pattern; we re-implement manually because OpenCompany uses native SDK
(``chat_model.bind_tools`` for schema generation) rather than the
OpenAI Agents SDK.
"""
@@ -38,16 +38,16 @@ import asyncio
from dataclasses import asdict
import hashlib
import logging
-import os
from typing import Any, Dict, List, Optional
from temporalio import activity
logger = logging.getLogger(__name__)
+# The only engine there is. Kept as a named constant rather than inlined
+# because it is written into Temporal history by ``prepare_agent_payload``
+# and read back by ``execute_llm_step`` to detect pre-cutover runs.
_NATIVE_LLM_ENGINE = "native"
-_LEGACY_LLM_ENGINE = "langchain"
-_LLM_ENGINES = frozenset({_NATIVE_LLM_ENGINE, _LEGACY_LLM_ENGINE})
# Activity result shapes — keep these in sync with AgentWorkflow's
@@ -89,18 +89,6 @@ def _ensure_llm_contents(messages: List[Any]) -> None:
)
-def _configured_llm_engine() -> str:
- """Return the engine that new prepare activity results will record."""
-
- engine = os.getenv("AGENT_LLM_ENGINE", _NATIVE_LLM_ENGINE).strip().lower()
- if engine not in _LLM_ENGINES:
- raise ValueError(
- "AGENT_LLM_ENGINE must be either 'native' or 'langchain'; "
- f"received {engine!r}"
- )
- return engine
-
-
async def _resolve_activity_api_key(payload: Dict[str, Any]) -> str:
"""Resolve a provider credential inside an activity, never workflow state.
@@ -388,137 +376,31 @@ async def execute_llm_step(payload: Dict[str, Any]) -> Dict[str, Any]:
)
activity.heartbeat(f"LLM step starting: {payload.get('model')}")
- # The engine marker is recorded in ``agent.prepare_payload.v1`` output.
- # Its absence is the migration sentinel for histories started before the
- # native cutover and MUST continue through the untouched LangChain path.
- engine_value = payload.get("llm_engine")
- engine = (
- _LEGACY_LLM_ENGINE
- if engine_value is None
- else str(engine_value).strip().lower()
- )
- if engine not in _LLM_ENGINES:
+ # ``agent.prepare_payload.v1`` records the engine marker. Its absence
+ # means the run predates the native cutover, when conversation state was
+ # serialised in a retired wire format the native reader cannot
+ # interpret. Failing loudly is the honest outcome: reinterpreting those
+ # messages would silently corrupt the conversation.
+ engine = str(payload.get("llm_engine") or "").strip().lower()
+ if engine != _NATIVE_LLM_ENGINE:
from temporalio.exceptions import ApplicationError
+ detail = (
+ "was started before the native LLM cutover, so its conversation "
+ "state is in a retired wire format"
+ if not engine
+ else f"records an unsupported LLM engine {engine!r}"
+ )
raise ApplicationError(
- f"Unsupported agent LLM engine {engine!r}",
+ f"This agent run {detail} and cannot continue. Reset the workflow "
+ "to start a new generation.",
type="InvalidAgentLLMEngine",
non_retryable=True,
)
- if engine == _NATIVE_LLM_ENGINE:
- result = await _execute_native_llm_step(payload)
- activity.heartbeat("LLM step: model returned")
- return result
-
- # Lazy import — keep top-level light so the worker can register this
- # activity without pulling LangChain in for every plugin.
- from core.container import container
- from services.ai import extract_thinking_from_response
-
- provider = payload["provider"]
- model = payload["model"]
- api_key = await _resolve_activity_api_key(payload)
- messages = payload.get("messages", [])
- tool_data = payload.get("tool_data", [])
- temperature = payload.get("temperature", 0.7)
- max_tokens = payload.get("max_tokens", 4096)
- thinking_config = payload.get("thinking_config")
-
- ai_service = container.ai_service()
-
- chat_model = ai_service.create_model(
- provider=provider,
- api_key=api_key,
- model=model,
- temperature=temperature,
- max_tokens=max_tokens,
- thinking=thinking_config,
- )
-
- # Reuse the same tool-binding path execute_agent uses. The returned
- # StructuredTool has a proper Pydantic args_schema every provider's
- # bind_tools knows how to convert. The tool's callback is never
- # invoked here — the workflow schedules per-type activities for each
- # tool_call the model emits.
- bound_tools: List[Any] = []
- for tool_info in tool_data:
- tool, _config = await ai_service._build_tool_from_node(tool_info)
- if tool is not None:
- # Stage-1 compatibility: the canonical builder now returns the
- # provider-neutral AgentToolSpec. Old histories still need the
- # exact LangChain binding surface until they drain.
- to_langchain = getattr(tool, "to_langchain", None)
- bound_tools.append(
- to_langchain() if callable(to_langchain) else tool
- )
- if bound_tools:
- chat_model = chat_model.bind_tools(bound_tools)
-
- # Workflow state is serialisable JSON dicts. Use LangChain's own
- # ``messages_from_dict`` / ``messages_to_dict`` helpers so
- # provider-specific metadata (Gemini ``thought_signature``,
- # Anthropic cache fields, OpenAI ``reasoning_content``) survives
- # the workflow ↔ activity round-trip. Manually constructing
- # AIMessage(content=...) from a stripped dict loses these and
- # blows up Gemini's ``Function call is missing a thought_signature``
- # on the next turn.
- from langchain_core.messages import messages_from_dict
- from services.llm.messages import filter_empty_messages
-
- rehydrated = messages_from_dict(messages)
- # Same filter ``_run_agent_loop`` runs in services/ai.py — empty-content
- # messages trigger 400s on Gemini / Anthropic.
- rehydrated = filter_empty_messages(rehydrated)
- _ensure_llm_contents(rehydrated)
- activity.heartbeat("LLM step: invoking model")
- response = await _await_with_llm_heartbeats(
- chat_model.ainvoke(rehydrated),
- detail=f"LLM step waiting: {model}",
- )
+ result = await _execute_native_llm_step(payload)
activity.heartbeat("LLM step: model returned")
-
- # Extract usage if present (anthropic-style token counts).
- usage: Dict[str, Any] = {}
- if hasattr(response, "usage_metadata") and response.usage_metadata:
- usage = dict(response.usage_metadata)
- elif hasattr(response, "response_metadata"):
- meta_usage = response.response_metadata.get("usage", {})
- if meta_usage:
- usage = dict(meta_usage)
-
- # Serialise the full assistant message so the workflow can append
- # it verbatim to its messages list (preserves thought_signature,
- # cache metadata, etc.). The workflow extracts ``tool_calls`` from
- # ``response.tool_calls`` separately for scheduling.
- from langchain_core.messages import messages_to_dict
-
- assistant_dict = messages_to_dict([response])[0]
-
- if hasattr(response, "tool_calls") and response.tool_calls:
- return {
- "kind": "tool_calls",
- "assistant_message": assistant_dict,
- "calls": [
- {
- "id": tc.get("id", ""),
- "name": tc.get("name", ""),
- "args": tc.get("args", {}),
- }
- for tc in response.tool_calls
- ],
- "usage": usage,
- }
-
- # No tool calls → final response.
- text, thinking = extract_thinking_from_response(response)
- return {
- "kind": "final",
- "assistant_message": assistant_dict,
- "content": text or "",
- "thinking": thinking,
- "usage": usage,
- }
+ return result
@activity.defn(name="agent.persist_turn.v1")
@@ -813,7 +695,7 @@ async def store_agent_output(payload: Dict[str, Any]) -> Dict[str, Any]:
async def prepare_agent_payload(context: Dict[str, Any]) -> Dict[str, Any]:
"""Resolve everything ``AgentWorkflow`` needs from the canvas + DB.
- The workflow itself cannot do DB lookups or LangChain tool builds
+ The workflow itself cannot do DB lookups or tool builds
(deterministic-replay constraint). This activity runs *before* the
workflow is scheduled and returns the fully-resolved payload.
@@ -1270,12 +1152,9 @@ async def prepare_agent_payload(context: Dict[str, Any]) -> Dict[str, Any]:
except Exception: # noqa: BLE001 — last-resort fallback
effective_recursion_limit = 200
- llm_engine = _configured_llm_engine()
- message_wire_version = 1
- if llm_engine == _NATIVE_LLM_ENGINE:
- from services.llm.protocol import MESSAGE_WIRE_VERSION
+ from services.llm.protocol import MESSAGE_WIRE_VERSION
- message_wire_version = MESSAGE_WIRE_VERSION
+ message_wire_version = MESSAGE_WIRE_VERSION
return {
"node_id": node_id,
@@ -1306,10 +1185,10 @@ async def prepare_agent_payload(context: Dict[str, Any]) -> Dict[str, Any]:
or context.get("execution_id")
or ""
),
- # Sticky execution-engine selection: this activity result is recorded
- # in Temporal history, so environment changes cannot flip a run
- # halfway through its agent loop.
- "llm_engine": llm_engine,
+ # The marker `execute_llm_step` checks: a history without it
+ # predates the native cutover and carries messages in a retired wire
+ # format that can no longer be read.
+ "llm_engine": _NATIVE_LLM_ENGINE,
"message_wire_version": message_wire_version,
}
diff --git a/server/services/temporal/agent_workflow.py b/server/services/temporal/agent_workflow.py
index e2257411..e69c2358 100644
--- a/server/services/temporal/agent_workflow.py
+++ b/server/services/temporal/agent_workflow.py
@@ -187,7 +187,7 @@ def _append_tool_result_message(
return
# Do not change this legacy shape: histories created before the cutover
- # pass it through LangChain's messages_from_dict on replay.
+ # pass it through the legacy message reader on replay.
messages.append(
{
"type": "tool",
@@ -752,7 +752,7 @@ class AgentWorkflow:
# ---- Build initial message list ---------------------------------
# Native executions use the SDK-neutral v2 wire codec. Legacy
- # histories keep LangChain's canonical {type, data} shape exactly.
+ # histories keep the legacy canonical {type, data} shape exactly.
messages: List[Dict[str, Any]] = []
system = payload.get("system_message") or ""
@@ -1017,7 +1017,7 @@ class AgentWorkflow:
kind = step_result.get("kind")
# The activity returns the FULL serialized assistant message
- # (LangChain's canonical {type, data} shape from messages_to_dict).
+ # (the legacy canonical {type, data} shape).
# Appending verbatim preserves Gemini thought_signature, Anthropic
# cache markers, OpenAI reasoning content — everything the next
# turn's request needs.
diff --git a/server/tests/llm/test_langchain_removed.py b/server/tests/llm/test_langchain_removed.py
new file mode 100644
index 00000000..3cbc4243
--- /dev/null
+++ b/server/tests/llm/test_langchain_removed.py
@@ -0,0 +1,176 @@
+"""LangChain is gone. Keep it gone.
+
+This file replaces ``test_legacy_langchain_boundary.py``, which allowed
+LangChain inside a named set of pre-cutover compatibility scopes. Those scopes
+were deleted along with the dependency, so the invariant inverts: nothing in
+the server may import LangChain, and it must not be declared or installed.
+
+The AST walk (rather than a text grep) is deliberate -- it catches
+``importlib.import_module("langchain_core")`` and ``__import__(...)``, which a
+grep for ``import langchain`` would miss, and which is exactly how the old
+compatibility code deferred these imports.
+"""
+
+from __future__ import annotations
+
+import ast
+import os
+from pathlib import Path
+import subprocess
+import sys
+from typing import Iterable, NamedTuple
+
+import pytest
+
+pytestmark = pytest.mark.unit
+
+SERVER_ROOT = Path(__file__).resolve().parents[2]
+
+FORBIDDEN_PREFIXES = ("langchain", "langgraph", "langsmith", "deepagents")
+
+
+class _ImportUse(NamedTuple):
+ module: str
+ scope: str
+ line: int
+
+
+class _ImportVisitor(ast.NodeVisitor):
+ def __init__(self) -> None:
+ self.uses: list[_ImportUse] = []
+ self._scope: list[str] = []
+
+ @property
+ def scope(self) -> str:
+ return ".".join(self._scope) if self._scope else "<module>"
+
+ def visit_ClassDef(self, node: ast.ClassDef) -> None:
+ self._scope.append(node.name)
+ self.generic_visit(node)
+ self._scope.pop()
+
+ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
+ self._scope.append(node.name)
+ self.generic_visit(node)
+ self._scope.pop()
+
+ visit_AsyncFunctionDef = visit_FunctionDef
+
+ def visit_Import(self, node: ast.Import) -> None:
+ self.uses.extend(
+ _ImportUse(alias.name, self.scope, node.lineno) for alias in node.names
+ )
+
+ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
+ if node.module:
+ self.uses.append(_ImportUse(node.module, self.scope, node.lineno))
+
+ def visit_Call(self, node: ast.Call) -> None:
+ module: str | None = None
+ if isinstance(node.func, ast.Name) and node.func.id == "__import__":
+ module = _literal_first_argument(node)
+ elif isinstance(node.func, ast.Attribute) and node.func.attr in {
+ "import_module",
+ "resolve_name",
+ }:
+ module = _literal_first_argument(node)
+ if module:
+ # pkgutil.resolve_name accepts ``package:object`` references.
+ self.uses.append(_ImportUse(module.split(":", 1)[0], self.scope, node.lineno))
+ self.generic_visit(node)
+
+
+def _literal_first_argument(node: ast.Call) -> str | None:
+ if not node.args:
+ return None
+ value = node.args[0]
+ return (
+ value.value
+ if isinstance(value, ast.Constant) and isinstance(value.value, str)
+ else None
+ )
+
+
+def _imports(path: Path) -> Iterable[_ImportUse]:
+ visitor = _ImportVisitor()
+ visitor.visit(ast.parse(path.read_text(encoding="utf-8"), filename=str(path)))
+ return visitor.uses
+
+
+def _production_sources() -> list[Path]:
+ return [
+ path
+ for path in SERVER_ROOT.rglob("*.py")
+ if "tests" not in path.parts and ".venv" not in path.parts
+ ]
+
+
+def test_no_module_imports_langchain():
+ sources = _production_sources()
+ assert len(sources) > 100, f"source walk found only {len(sources)} files"
+
+ violations = [
+ f"{path.relative_to(SERVER_ROOT)}:{use.line} [{use.scope}]: {use.module}"
+ for path in sources
+ for use in _imports(path)
+ if use.module.startswith(FORBIDDEN_PREFIXES)
+ ]
+
+ assert not violations, (
+ "LangChain was removed from this project. Reintroducing it needs a "
+ "deliberate decision, not an import:\n" + "\n".join(sorted(violations))
+ )
+
+
+def test_not_declared_as_a_dependency():
+ pyproject = (SERVER_ROOT / "pyproject.toml").read_text(encoding="utf-8")
+ offenders = [
+ line.strip()
+ for line in pyproject.splitlines()
+ if line.strip().startswith('"')
+ and line.strip().lstrip('"').startswith(FORBIDDEN_PREFIXES)
+ ]
+ assert not offenders, offenders
+
+
+def test_not_importable_at_runtime():
+ """The packages are uninstalled, not merely unreferenced."""
+ import importlib.util
+
+ for name in ("langchain_core", "langchain_openai", "langsmith"):
+ assert importlib.util.find_spec(name) is None, (
+ f"{name} is still installed; run `uv sync` against the current lock"
+ )
+
+
+def test_container_import_does_not_load_it():
+ """Belt and braces: nothing pulls it in transitively at boot either."""
+ code = """
+import builtins
+import sys
+
+blocked = ("langchain", "langgraph", "langsmith", "deepagents")
+real_import = builtins.__import__
+
+def guarded_import(name, *args, **kwargs):
+ if name.startswith(blocked):
+ raise AssertionError(f"removed dependency imported at boot: {name}")
+ return real_import(name, *args, **kwargs)
+
+builtins.__import__ = guarded_import
+import core.container # noqa: F401
+loaded = sorted(name for name in sys.modules if name.startswith(blocked))
+assert not loaded, loaded
+"""
+ env = dict(os.environ)
+ env["DEBUG"] = "false"
+ result = subprocess.run(
+ [sys.executable, "-c", code],
+ cwd=SERVER_ROOT,
+ env=env,
+ capture_output=True,
+ text=True,
+ timeout=120,
+ check=False,
+ )
+ assert result.returncode == 0, result.stdout + result.stderr
diff --git a/server/tests/llm/test_legacy_langchain_boundary.py b/server/tests/llm/test_legacy_langchain_boundary.py
deleted file mode 100644
index 33e1d277..00000000
--- a/server/tests/llm/test_legacy_langchain_boundary.py
+++ /dev/null
@@ -1,215 +0,0 @@
-"""Keep LangChain isolated to the pre-cutover Temporal compatibility path."""
-
-from __future__ import annotations
-
-import ast
-import os
-from pathlib import Path
-import subprocess
-import sys
-from typing import Iterable, NamedTuple
-
-
-SERVER_ROOT = Path(__file__).resolve().parents[2]
-LEGACY_IMPORT_SCOPES = {
- Path("services/ai.py"): {
- "TYPE_CHECKING",
- "_get_chat_anthropic",
- "_get_chat_groq",
- "_get_chat_cerebras",
- "_get_google_genai_class",
- "__getattr__",
- "_build_provider_class_map",
- "_build_provider_configs",
- "_run_agent_loop",
- "AIService.create_model",
- },
- Path("services/agent_runtime.py"): {
- "AgentToolSpec.to_langchain",
- },
- Path("services/temporal/agent_activities.py"): {
- "execute_llm_step",
- },
-}
-
-
-class _ImportUse(NamedTuple):
- module: str
- scope: str
- line: int
-
-
-class _ImportVisitor(ast.NodeVisitor):
- def __init__(self) -> None:
- self.uses: list[_ImportUse] = []
- self._scope: list[str] = []
-
- @property
- def scope(self) -> str:
- return ".".join(self._scope) if self._scope else "<module>"
-
- def visit_ClassDef(self, node: ast.ClassDef) -> None:
- self._scope.append(node.name)
- self.generic_visit(node)
- self._scope.pop()
-
- def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
- self._scope.append(node.name)
- self.generic_visit(node)
- self._scope.pop()
-
- visit_AsyncFunctionDef = visit_FunctionDef
-
- def visit_If(self, node: ast.If) -> None:
- is_type_checking = (
- isinstance(node.test, ast.Name)
- and node.test.id == "TYPE_CHECKING"
- )
- if is_type_checking:
- self._scope.append("TYPE_CHECKING")
- for statement in node.body:
- self.visit(statement)
- if is_type_checking:
- self._scope.pop()
- for statement in node.orelse:
- self.visit(statement)
-
- def visit_Import(self, node: ast.Import) -> None:
- self.uses.extend(
- _ImportUse(alias.name, self.scope, node.lineno)
- for alias in node.names
- )
-
- def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
- if node.module:
- self.uses.append(
- _ImportUse(node.module, self.scope, node.lineno)
- )
-
- def visit_Call(self, node: ast.Call) -> None:
- module: str | None = None
- if (
- isinstance(node.func, ast.Name)
- and node.func.id == "__import__"
- ):
- module = _literal_first_argument(node)
- elif (
- isinstance(node.func, ast.Attribute)
- and node.func.attr in {"import_module", "resolve_name"}
- ):
- module = _literal_first_argument(node)
- if module:
- # pkgutil.resolve_name accepts ``package:object`` references.
- self.uses.append(
- _ImportUse(module.split(":", 1)[0], self.scope, node.lineno)
- )
- self.generic_visit(node)
-
-
-def _literal_first_argument(node: ast.Call) -> str | None:
- if not node.args:
- return None
- value = node.args[0]
- return value.value if isinstance(value, ast.Constant) and isinstance(
- value.value, str
- ) else None
-
-
-def _third_party_imports(path: Path) -> Iterable[_ImportUse]:
- tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
- visitor = _ImportVisitor()
- visitor.visit(tree)
- return visitor.uses
-
-
-def _is_forbidden(module: str) -> bool:
- return (
- module == "deepagents"
- or module.startswith("deepagents.")
- or module.startswith(("langgraph", "langsmith"))
- )
-
-
-def test_langchain_is_confined_to_legacy_temporal_compatibility():
- violations: list[str] = []
- for path in SERVER_ROOT.rglob("*.py"):
- if "tests" in path.parts or ".venv" in path.parts:
- continue
- relative = path.relative_to(SERVER_ROOT)
- allowed_scopes = LEGACY_IMPORT_SCOPES.get(relative, set())
- for use in _third_party_imports(path):
- if _is_forbidden(use.module):
- violations.append(
- f"{relative}:{use.line} [{use.scope}]: {use.module}"
- )
- elif (
- use.module.startswith("langchain")
- and use.scope not in allowed_scopes
- ):
- violations.append(
- f"{relative}:{use.line} [{use.scope}]: {use.module}"
- )
-
- assert not violations, (
- "LangChain may remain only in the explicitly temporary pre-cutover "
- "Temporal compatibility surface:\n" + "\n".join(sorted(violations))
- )
-
-
-def test_native_agent_entrypoints_do_not_use_langchain_shaped_calls():
- tree = ast.parse(
- (SERVER_ROOT / "services" / "ai.py").read_text(encoding="utf-8")
- )
- violations: list[str] = []
- for node in ast.walk(tree):
- if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
- continue
- if node.name not in {"execute_agent", "execute_chat_agent"}:
- continue
- for descendant in ast.walk(node):
- if (
- isinstance(descendant, ast.Attribute)
- and descendant.attr
- in {"ainvoke", "bind_tools", "get_input_schema"}
- ):
- violations.append(
- f"{node.name}:{descendant.lineno}: {descendant.attr}"
- )
- assert not violations, (
- "Native agent entrypoints used a LangChain-shaped API:\n"
- + "\n".join(violations)
- )
-
-
-def test_container_import_does_not_load_legacy_dependency_tree():
- code = """
-import builtins
-import sys
-
-blocked = ("langchain", "langgraph", "langsmith", "deepagents")
-real_import = builtins.__import__
-
-def guarded_import(name, *args, **kwargs):
- if name.startswith(blocked):
- raise AssertionError(f"legacy dependency imported at boot: {name}")
- return real_import(name, *args, **kwargs)
-
-builtins.__import__ = guarded_import
-import core.container # noqa: F401
-loaded = sorted(
- name for name in sys.modules if name.startswith(blocked)
-)
-assert not loaded, loaded
-"""
- env = dict(os.environ)
- env["DEBUG"] = "false"
- result = subprocess.run(
- [sys.executable, "-c", code],
- cwd=SERVER_ROOT,
- env=env,
- capture_output=True,
- text=True,
- timeout=60,
- check=False,
- )
- assert result.returncode == 0, result.stdout + result.stderr
diff --git a/server/tests/llm/test_vertex_key.py b/server/tests/llm/test_vertex_key.py
index 9dd56733..172cafc6 100644
--- a/server/tests/llm/test_vertex_key.py
+++ b/server/tests/llm/test_vertex_key.py
@@ -259,69 +259,3 @@ class TestGeminiProviderVertexMode:
"include_thoughts": True,
"thinking_budget": 1024,
}
-
-
-# ---------------------------------------------------------------------------
-# LangChain agent path: AIService.create_model
-# ---------------------------------------------------------------------------
-
-
-class _KwargsCapture:
- """Stand-in for ChatGoogleGenerativeAI that records its kwargs."""
-
- def __init__(self, **kwargs):
- self.kwargs = kwargs
-
-
-class TestCreateModelVertexMode:
- def _create(self, api_key, *, thinking=None, proxy_url=None):
- from services.ai import AIService
-
- with patch("services.ai._get_google_genai_class", return_value=_KwargsCapture):
- model = AIService.create_model(
- MagicMock(), # create_model touches no instance state
- provider="gemini",
- api_key=api_key,
- model="gemini-2.5-flash",
- temperature=0.7,
- max_tokens=4096,
- thinking=thinking,
- proxy_url=proxy_url,
- )
- return model.kwargs
-
- def test_vertex_key_sets_vertexai_flag(self):
- kwargs = self._create("AQ.test-key")
- assert kwargs["vertexai"] is True
- assert kwargs["google_api_key"] == "AQ.test-key"
- assert kwargs["max_output_tokens"] == 4096
-
- def test_vertex_key_nulls_proxy(self):
- kwargs = self._create("AQ.test-key", proxy_url="http://localhost:11434")
- assert "base_url" not in kwargs
- assert kwargs["google_api_key"] == "AQ.test-key"
-
- def test_vertex_key_does_not_alter_thinking_handling(self):
- """The vertex branch only adds ``vertexai=True`` — thinking kwargs
- (resolved by the model registry) must be byte-identical to the
- AI-Studio path for the same model + thinking config."""
- from services.ai import ThinkingConfig
-
- thinking = ThinkingConfig(enabled=True, budget=2048)
- vertex_kwargs = self._create("AQ.test-key", thinking=thinking)
- studio_kwargs = self._create("AIzaSyTest", thinking=thinking)
-
- assert vertex_kwargs.pop("vertexai") is True
- assert vertex_kwargs.pop("google_api_key") == "AQ.test-key"
- assert studio_kwargs.pop("google_api_key") == "AIzaSyTest"
- assert vertex_kwargs == studio_kwargs
-
- def test_ai_studio_key_has_no_vertexai_flag(self):
- kwargs = self._create("AIzaSyTest")
- assert "vertexai" not in kwargs
- assert kwargs["google_api_key"] == "AIzaSyTest"
-
- def test_ai_studio_key_proxy_still_applies(self):
- kwargs = self._create("AIzaSyTest", proxy_url="http://localhost:11434")
- assert kwargs["base_url"] == "http://localhost:11434"
- assert "vertexai" not in kwargs
diff --git a/server/tests/services/test_agent_loop_rebind.py b/server/tests/services/test_agent_loop_rebind.py
deleted file mode 100644
index 3a2dff7b..00000000
--- a/server/tests/services/test_agent_loop_rebind.py
+++ /dev/null
@@ -1,393 +0,0 @@
-"""Unit tests for the mid-loop tool-rebind path in ``_run_agent_loop``.
-
-The agent loop, after each tool call, must:
-
-* Detect a ``operations`` field in the tool result (canvas-mutating
- tools like ``agentBuilder`` emit a ``workflow_ops`` batch there).
-* Invoke the supplied ``rebind_from_operations`` callback with the ops.
-* Append the returned tools to the bound LLM surface and re-call
- ``chat_model.bind_tools`` so the LLM sees the new wiring on the
- NEXT iteration (no Run-stop-Run cycle).
-
-Driven by source-aware mocks of ``chat_model`` so the test runs
-without a live LLM. The ``execute_agent`` + ``execute_chat_agent``
-wirings get source-introspection coverage in
-``test_agent_loop_rebind_wiring`` below — full end-to-end live testing
-of those happens in the existing integration suite.
-"""
-
-from __future__ import annotations
-
-import inspect
-from typing import Any, Dict, List
-
-import pytest
-
-from langchain_core.messages import AIMessage, HumanMessage
-
-from services import ai as ai_module
-from services.tool_identity import DuplicateToolNameError
-
-
-# ----------------------------------------------------------------------------
-# Mock chat-model harness
-# ----------------------------------------------------------------------------
-
-
-class _FakeBoundModel:
- """Returns scripted ``AIMessage`` responses. The response queue is
- a SHARED list reference across all bound models built from the
- same _FakeChatModel — so a rebind doesn't restart the script."""
-
- def __init__(self, shared_responses: List[AIMessage]):
- self._responses = shared_responses # SHARED ref, not a copy
- self.invocations: List[List[Any]] = []
-
- async def ainvoke(self, messages):
- self.invocations.append(messages)
- if not self._responses:
- return AIMessage(content="done")
- return self._responses.pop(0)
-
-
-class _FakeChatModel:
- """Toplevel chat model. ``bind_tools`` returns a _FakeBoundModel
- that pops from a single shared response queue."""
-
- def __init__(self, responses: List[AIMessage]):
- self._responses = list(responses) # local mutable copy
- self.bind_calls: List[List[Any]] = []
- self.last_bound: _FakeBoundModel | None = None
-
- def bind_tools(self, tools):
- self.bind_calls.append(list(tools))
- self.last_bound = _FakeBoundModel(self._responses)
- return self.last_bound
-
- async def ainvoke(self, messages):
- # Only reached when no tools wired — _run_agent_loop binds tools
- # if any are present.
- if not self._responses:
- return AIMessage(content="done")
- return self._responses.pop(0)
-
-
-class _FakeTool:
- """Minimal stand-in matching ``StructuredTool`` surface that
- ``chat_model.bind_tools`` accepts. Just needs a ``name``."""
-
- def __init__(self, name: str):
- self.name = name
-
-
-# ----------------------------------------------------------------------------
-# Tests
-# ----------------------------------------------------------------------------
-
-
-class TestAgentLoopRebind:
- async def test_rebind_callback_invoked_with_operations(self):
- """When a tool result carries ``operations``, the loop must
- invoke ``rebind_from_operations`` with that list."""
- # Iteration 1: LLM emits a tool_call. Iteration 2: returns final.
- responses = [
- AIMessage(content="", tool_calls=[{"name": "agentBuilder", "args": {}, "id": "c1"}]),
- AIMessage(content="done"),
- ]
- chat_model = _FakeChatModel(responses)
- initial_tool = _FakeTool("agentBuilder")
-
- async def tool_executor(name: str, args: Dict[str, Any]) -> Any:
- # Return ops so the loop triggers rebind.
- return {
- "operation": "add_tool",
- "summary": "Added 'httpRequest'.",
- "operations": [
- {"type": "add_node", "client_ref": "new_httpRequest", "node_type": "httpRequest"},
- {"type": "add_edge", "source": {"client_ref": "new_httpRequest"}, "target": "agent-1"},
- ],
- }
-
- captured_ops: List[List[Dict[str, Any]]] = []
-
- async def rebind_from_operations(ops: List[Dict[str, Any]]) -> List[Any]:
- captured_ops.append(ops)
- return [_FakeTool("httpRequest")]
-
- await ai_module._run_agent_loop(
- chat_model,
- [HumanMessage(content="go")],
- tools=[initial_tool],
- tool_executor=tool_executor,
- max_iterations=5,
- rebind_from_operations=rebind_from_operations,
- )
-
- assert len(captured_ops) == 1, "rebind_from_operations must be called once per tool result with operations"
- assert captured_ops[0][0]["type"] == "add_node"
- assert captured_ops[0][0]["node_type"] == "httpRequest"
-
- async def test_bind_tools_called_again_after_rebind(self):
- """Returned tools from ``rebind_from_operations`` must extend
- the bound surface — ``chat_model.bind_tools`` is invoked a
- second time with the larger list."""
- responses = [
- AIMessage(content="", tool_calls=[{"name": "agentBuilder", "args": {}, "id": "c1"}]),
- AIMessage(content="done"),
- ]
- chat_model = _FakeChatModel(responses)
- initial_tool = _FakeTool("agentBuilder")
- new_tool = _FakeTool("httpRequest")
-
- async def tool_executor(name, args):
- return {"operations": [{"type": "add_node", "node_type": "httpRequest", "client_ref": "x"}]}
-
- async def rebind_from_operations(ops):
- return [new_tool]
-
- await ai_module._run_agent_loop(
- chat_model,
- [HumanMessage(content="go")],
- tools=[initial_tool],
- tool_executor=tool_executor,
- max_iterations=5,
- rebind_from_operations=rebind_from_operations,
- )
-
- # bind_tools must be called twice: once at loop start, once
- # after the canvas mutation.
- assert len(chat_model.bind_calls) == 2
- # First call: only the initial tool.
- assert chat_model.bind_calls[0] == [initial_tool]
- # Second call: initial + new tool (extending, not replacing).
- assert chat_model.bind_calls[1] == [initial_tool, new_tool]
-
- async def test_no_rebind_when_callback_omitted(self):
- """Without ``rebind_from_operations`` (toggle off), the loop
- does NOT rebind even when ops are present — preserves the
- legacy "wait for next run" behaviour."""
- responses = [
- AIMessage(content="", tool_calls=[{"name": "agentBuilder", "args": {}, "id": "c1"}]),
- AIMessage(content="done"),
- ]
- chat_model = _FakeChatModel(responses)
- initial_tool = _FakeTool("agentBuilder")
-
- async def tool_executor(name, args):
- return {"operations": [{"type": "add_node", "node_type": "httpRequest", "client_ref": "x"}]}
-
- await ai_module._run_agent_loop(
- chat_model,
- [HumanMessage(content="go")],
- tools=[initial_tool],
- tool_executor=tool_executor,
- max_iterations=5,
- rebind_from_operations=None,
- )
-
- # Only the initial bind_tools call — no rebind.
- assert len(chat_model.bind_calls) == 1
-
- async def test_no_rebind_when_tool_returns_no_operations(self):
- """A tool that returns a regular result (no ``operations`` key)
- must not trigger a rebind even when the callback is wired."""
- responses = [
- AIMessage(content="", tool_calls=[{"name": "calculator", "args": {}, "id": "c1"}]),
- AIMessage(content="done"),
- ]
- chat_model = _FakeChatModel(responses)
- initial_tool = _FakeTool("calculator")
-
- async def tool_executor(name, args):
- return {"result": 42} # No ``operations`` field.
-
- called = False
-
- async def rebind_from_operations(ops):
- nonlocal called
- called = True
- return []
-
- await ai_module._run_agent_loop(
- chat_model,
- [HumanMessage(content="go")],
- tools=[initial_tool],
- tool_executor=tool_executor,
- max_iterations=5,
- rebind_from_operations=rebind_from_operations,
- )
-
- assert not called
- assert len(chat_model.bind_calls) == 1
-
- async def test_duplicate_hot_rebind_is_structured_and_not_bound(self):
- """An ambiguous hot refresh is rejected before the model surface
- changes, and the conflict is returned in the originating tool result."""
- responses = [
- AIMessage(content="", tool_calls=[{"name": "agentBuilder", "args": {}, "id": "c1"}]),
- AIMessage(content="done"),
- ]
- chat_model = _FakeChatModel(responses)
- initial_tool = _FakeTool("agentBuilder")
-
- async def tool_executor(name, args):
- return {"operations": [{"type": "add_node", "node_type": "agentBuilder"}]}
-
- async def rebind_from_operations(ops):
- raise DuplicateToolNameError(
- {
- "agentBuilder": [
- {"node_id": "builder-a", "label": "Builder A"},
- {"node_id": "builder-b", "label": "Builder B"},
- ]
- }
- )
-
- state = await ai_module._run_agent_loop(
- chat_model,
- [HumanMessage(content="go")],
- tools=[initial_tool],
- tool_executor=tool_executor,
- max_iterations=5,
- rebind_from_operations=rebind_from_operations,
- )
-
- assert len(chat_model.bind_calls) == 1
- tool_message = state["messages"][2]
- assert "DuplicateToolNameError" in tool_message.content
- assert "builder-a" in tool_message.content
-
-
-# ----------------------------------------------------------------------------
-# Wiring source-introspection — confirm execute_agent + execute_chat_agent
-# provide the rebind callback to _run_agent_loop.
-# ----------------------------------------------------------------------------
-
-
-class TestRebindAcceptsDualPurposePlugins:
- """The rebind path (both F4.A in-process closures and the F4.B
- ``agent.refresh_tools.v1`` activity) must build StructuredTools
- for **dual-purpose** ActionNode plugins (``usable_as_tool=True``),
- not only pure ToolNodes (``component_kind == 'tool'``).
-
- Without this, every ``twitterSearch`` / ``googleGmail`` /
- ``pythonExecutor`` add_tool call returns success on the LLM-visible
- surface but the tool never makes it into ``chat_model.bind_tools``
- — so the next iteration's LLM calls an unknown tool and the run
- burns iterations on retries.
- """
-
- def test_ai_rebind_accepts_dual_purpose(self):
- import inspect
-
- from services.ai import AIService
-
- src = inspect.getsource(AIService.execute_agent)
- # Must use the broader filter — pure tool OR dual-purpose
- # ActionNode (usable_as_tool=True), excluding chat models.
- assert "usable_as_tool" in src, (
- "execute_agent's rebind closure must look at usable_as_tool, "
- "not just component_kind=='tool'."
- )
- # Explicit ``!= 'model'`` guard so the broadening doesn't sweep
- # in chat-model plugins like openaiChatModel that carry
- # ``usable_as_tool=True`` but aren't agent tools.
- assert '"model"' in src or "'model'" in src, (
- "Rebind broadening must explicitly exclude component_kind='model'."
- )
-
- def test_ai_rebind_excludes_chat_models(self):
- """execute_chat_agent's rebind closure shares the same broadened
- filter — locks both call sites."""
- import inspect
-
- from services.ai import AIService
-
- src = inspect.getsource(AIService.execute_chat_agent)
- assert "usable_as_tool" in src
-
- def test_temporal_refresh_accepts_dual_purpose(self):
- import inspect
-
- from services.temporal.agent_activities import refresh_agent_tools
-
- src = inspect.getsource(refresh_agent_tools)
- assert "usable_as_tool" in src, (
- "agent.refresh_tools.v1 must build tools for dual-purpose "
- "plugins (twitterSearch, googleGmail, pythonExecutor, …) — "
- "the bulk of useful LLM-callable tools live there, not in "
- "pure ToolNode plugins."
- )
-
-
-class TestRebindIdAlignment:
- """The rebind path must use the BE-minted ``minted_id`` (set by
- agentBuilder so the FE applier adopts it verbatim) as the new
- tool's ``node_id``. Without this the dispatcher's status
- broadcasts target a synthesized id that doesn't match any React
- Flow node → canvas doesn't glow on the rebound tool's first run.
- """
-
- def test_ai_rebind_prefers_minted_id(self):
- import inspect
-
- from services.ai import AIService
-
- src = inspect.getsource(AIService.execute_agent)
- assert 'op.get("minted_id")' in src, (
- "execute_agent's rebind closure must prefer op['minted_id'] over "
- "op['client_ref'] when synthesizing the tool's node_id, otherwise "
- "the canvas can't glow on the rebound tool."
- )
- src = inspect.getsource(AIService.execute_chat_agent)
- assert 'op.get("minted_id")' in src, (
- "execute_chat_agent's rebind closure must prefer op['minted_id']."
- )
-
- def test_temporal_refresh_tools_prefers_minted_id(self):
- import inspect
-
- from services.temporal.agent_activities import refresh_agent_tools
-
- src = inspect.getsource(refresh_agent_tools)
- assert 'op.get("minted_id")' in src, (
- "agent.refresh_tools.v1 activity must prefer op['minted_id'] when "
- "synthesizing the tool's node_id so F4.B status broadcasts align "
- "with the FE-adopted React Flow id."
- )
-
-
-class TestAgentLoopRebindWiring:
- def test_execute_agent_passes_rebind_callback(self):
- from services.ai import AIService
-
- src = inspect.getsource(AIService.execute_agent)
- assert "_rebind_from_operations" in src, (
- "execute_agent must define a rebind closure backed by "
- "_build_tool_from_node and pass it into _run_agent_loop."
- )
- assert "rebind_from_operations=" in src, "execute_agent must wire the rebind callback into _run_agent_loop."
- assert "auto_rebind_tools_after_canvas_change" in src, (
- "execute_agent must gate the rebind on the UserSettings flag."
- )
-
- def test_execute_chat_agent_passes_rebind_callback(self):
- from services.ai import AIService
-
- src = inspect.getsource(AIService.execute_chat_agent)
- assert "_rebind_from_operations" in src
- assert "rebind_from_operations=" in src
- assert "auto_rebind_tools_after_canvas_change" in src
-
- def test_both_agents_inject_flag_into_tool_config(self):
- """Both agent paths must inject ``auto_rebind_tools`` into the
- per-tool config so agentBuilder's summary text reflects the
- user's preference."""
- from services.ai import AIService
-
- for func in (AIService.execute_agent, AIService.execute_chat_agent):
- src = inspect.getsource(func)
- assert 'config["auto_rebind_tools"] = auto_rebind_enabled' in src, (
- f"{func.__name__} must inject the auto_rebind_tools flag into the tool config."
- )
diff --git a/server/tests/temporal/test_agent_history_compat.py b/server/tests/temporal/test_agent_history_compat.py
deleted file mode 100644
index eacacbc0..00000000
--- a/server/tests/temporal/test_agent_history_compat.py
+++ /dev/null
@@ -1,151 +0,0 @@
-"""LangChain message serialization must stay readable across upgrades.
-
-`services/temporal/agent_activities.py` round-trips agent conversations
-through `messages_to_dict` / `messages_from_dict`. Those dicts are what
-lands in Temporal's Event History, so they outlive the process that wrote
-them: a workflow started under one langchain-core release can be replayed
-weeks later by a worker running a newer one.
-
-That is the entire reason all six langchain packages are pinned with `==`
-in `pyproject.toml` rather than given floors. The pin states the caution;
-this file is what actually checks it, so a proposed bump can be evaluated by
-running the tests instead of by reasoning about it.
-
-GOLDEN is a real payload captured from langchain-core 1.4.7. If a future
-release changes the shape, `test_a_stored_history_still_deserializes` fails
-and the bump needs a migration for existing histories -- not a version
-change.
-"""
-
-from __future__ import annotations
-
-import json
-
-import pytest
-
-pytestmark = pytest.mark.unit
-
-
-# Captured from langchain-core 1.4.7 via messages_to_dict(...).
-GOLDEN = [
- {
- "data": {
- "additional_kwargs": {},
- "content": "sys",
- "id": None,
- "name": None,
- "response_metadata": {},
- "type": "system",
- },
- "type": "system",
- },
- {
- "data": {
- "additional_kwargs": {},
- "content": "hi",
- "id": None,
- "name": None,
- "response_metadata": {},
- "type": "human",
- },
- "type": "human",
- },
- {
- "data": {
- "additional_kwargs": {},
- "content": "",
- "id": None,
- "invalid_tool_calls": [],
- "name": None,
- "response_metadata": {},
- "tool_calls": [
- {"args": {"a": 1}, "id": "call_1", "name": "calc", "type": "tool_call"}
- ],
- "type": "ai",
- "usage_metadata": None,
- },
- "type": "ai",
- },
- {
- "data": {
- "additional_kwargs": {},
- "artifact": None,
- "content": "42",
- "id": None,
- "name": None,
- "response_metadata": {},
- "status": "success",
- "tool_call_id": "call_1",
- "type": "tool",
- },
- "type": "tool",
- },
-]
-
-
-class TestAgentHistoryCompatibility:
- def test_a_stored_history_still_deserializes(self):
- """The load-bearing one: old history, current library."""
- from langchain_core.messages import messages_from_dict
-
- messages = messages_from_dict(GOLDEN)
-
- assert [type(m).__name__ for m in messages] == [
- "SystemMessage",
- "HumanMessage",
- "AIMessage",
- "ToolMessage",
- ]
-
- def test_reserializing_a_stored_history_is_byte_identical(self):
- """No silent format drift: what we write back matches what we read.
-
- A worker that rewrote histories in a subtly different shape would
- corrupt them for any peer still on the previous release.
- """
- from langchain_core.messages import messages_from_dict, messages_to_dict
-
- reserialized = messages_to_dict(messages_from_dict(GOLDEN))
-
- assert json.dumps(reserialized, sort_keys=True) == json.dumps(
- GOLDEN, sort_keys=True
- )
-
- def test_tool_call_wiring_survives_the_round_trip(self):
- """Agent loops match tool results to calls by id; losing it breaks them."""
- from langchain_core.messages import messages_from_dict
-
- messages = messages_from_dict(GOLDEN)
- ai, tool = messages[2], messages[3]
-
- assert ai.tool_calls[0]["id"] == "call_1"
- assert ai.tool_calls[0]["name"] == "calc"
- assert ai.tool_calls[0]["args"] == {"a": 1}
- assert tool.tool_call_id == "call_1"
-
- def test_usage_metadata_round_trips(self):
- """Token accounting rides in the message; CompactionService reads it."""
- from langchain_core.messages import AIMessage, messages_from_dict, messages_to_dict
-
- original = AIMessage(
- content="done",
- usage_metadata={"input_tokens": 1, "output_tokens": 2, "total_tokens": 3},
- )
- restored = messages_from_dict(messages_to_dict([original]))[0]
-
- assert restored.usage_metadata == {
- "input_tokens": 1,
- "output_tokens": 2,
- "total_tokens": 3,
- }
-
- def test_the_pinned_version_is_the_one_under_test(self):
- """Guards against the golden payload silently describing a stale release."""
- import importlib.metadata as md
-
- installed = md.version("langchain-core")
- assert installed.startswith("1."), (
- f"langchain-core {installed} is a new major. The pins in "
- "pyproject.toml exist for Temporal replay compatibility -- "
- "re-capture GOLDEN and re-read this file before lifting them."
- )
diff --git a/server/tests/temporal/test_agent_workflow.py b/server/tests/temporal/test_agent_workflow.py
index 9dedaf9e..32f2e6d2 100644
--- a/server/tests/temporal/test_agent_workflow.py
+++ b/server/tests/temporal/test_agent_workflow.py
@@ -755,13 +755,13 @@ class TestEmptyPromptGuard:
def test_raises_non_retryable_on_system_only_list(self):
import pytest
- from langchain_core.messages import SystemMessage
from temporalio.exceptions import ApplicationError
+ from services.llm.protocol import Message
from services.temporal.agent_activities import _ensure_llm_contents
with pytest.raises(ApplicationError) as excinfo:
- _ensure_llm_contents([SystemMessage(content="you are helpful")])
+ _ensure_llm_contents([Message(role="system", content="you are helpful")])
assert excinfo.value.non_retryable is True
assert excinfo.value.type == "EmptyAgentPrompt"
@@ -775,20 +775,23 @@ class TestEmptyPromptGuard:
_ensure_llm_contents([])
def test_passes_with_human_message(self):
- from langchain_core.messages import HumanMessage, SystemMessage
-
+ from services.llm.protocol import Message
from services.temporal.agent_activities import _ensure_llm_contents
- _ensure_llm_contents([SystemMessage(content="sys"), HumanMessage(content="hi")])
+ _ensure_llm_contents(
+ [Message(role="system", content="sys"), Message(role="user", content="hi")]
+ )
def test_passes_with_tool_message(self):
"""Mid-loop turns may legitimately be tool-result-only."""
- from langchain_core.messages import SystemMessage, ToolMessage
-
+ from services.llm.protocol import Message
from services.temporal.agent_activities import _ensure_llm_contents
_ensure_llm_contents(
- [SystemMessage(content="sys"), ToolMessage(content="42", tool_call_id="c1")]
+ [
+ Message(role="system", content="sys"),
+ Message(role="tool", content="42", tool_call_id="c1"),
+ ]
)
def test_guard_runs_after_empty_message_filter(self):
@@ -798,12 +801,12 @@ class TestEmptyPromptGuard:
would miss exactly the failing case."""
import inspect
- from services.temporal.agent_activities import execute_llm_step
+ from services.temporal.agent_activities import _execute_native_llm_step
- src = inspect.getsource(execute_llm_step)
- assert "_ensure_llm_contents(rehydrated)" in src
- assert src.index("filter_empty_messages(rehydrated)") < src.index(
- "_ensure_llm_contents(rehydrated)"
+ src = inspect.getsource(_execute_native_llm_step)
+ assert "_ensure_llm_contents(messages)" in src
+ assert src.index("filter_empty_messages(") < src.index(
+ "_ensure_llm_contents(messages)"
)
diff --git a/server/tests/temporal/test_native_llm_cutover.py b/server/tests/temporal/test_native_llm_cutover.py
index da64ca2c..94c420b1 100644
--- a/server/tests/temporal/test_native_llm_cutover.py
+++ b/server/tests/temporal/test_native_llm_cutover.py
@@ -94,21 +94,65 @@ def patched_workflow(monkeypatch):
class TestEngineSelection:
- def test_new_runs_default_to_native(self, monkeypatch):
- from services.temporal.agent_activities import _configured_llm_engine
+ """After the LangChain removal there is exactly one engine.
- monkeypatch.delenv("AGENT_LLM_ENGINE", raising=False)
- assert _configured_llm_engine() == "native"
+ The marker survives because it is the discriminator: a history recorded
+ before the cutover carries LangChain-serialised messages and no
+ ``llm_engine`` key, and those messages can no longer be read.
+ """
- def test_emergency_switch_is_explicit_and_validated(self, monkeypatch):
- from services.temporal.agent_activities import _configured_llm_engine
+ @pytest.fixture
+ def outside_activity_context(self, monkeypatch):
+ """The guard runs after logging/heartbeat, which need a live activity."""
+ from services.temporal import agent_activities
- monkeypatch.setenv("AGENT_LLM_ENGINE", " LangChain ")
- assert _configured_llm_engine() == "langchain"
+ monkeypatch.setattr(
+ agent_activities.activity, "logger", MagicMock(), raising=False
+ )
+ monkeypatch.setattr(
+ agent_activities.activity, "heartbeat", lambda *a, **k: None
+ )
+
+ @pytest.mark.asyncio
+ async def test_execute_llm_step_refuses_a_pre_cutover_history(
+ self, outside_activity_context
+ ):
+ from temporalio.exceptions import ApplicationError
+
+ from services.temporal.agent_activities import execute_llm_step
+
+ payload = _payload(native=True)
+ payload.pop("llm_engine") # the pre-cutover shape
+
+ with pytest.raises(ApplicationError) as excinfo:
+ await execute_llm_step(payload)
+
+ assert excinfo.value.type == "InvalidAgentLLMEngine"
+ assert excinfo.value.non_retryable is True
+ assert "before the native LLM cutover" in str(excinfo.value)
+
+ @pytest.mark.asyncio
+ async def test_execute_llm_step_refuses_the_retired_langchain_engine(
+ self, outside_activity_context
+ ):
+ from temporalio.exceptions import ApplicationError
+
+ from services.temporal.agent_activities import execute_llm_step
+
+ with pytest.raises(ApplicationError) as excinfo:
+ await execute_llm_step(_emergency_langchain_payload())
+
+ assert excinfo.value.type == "InvalidAgentLLMEngine"
+ assert excinfo.value.non_retryable is True
+
+ @pytest.mark.asyncio
+ async def test_prepare_payload_always_records_native(self, monkeypatch):
+ """No env switch remains; the marker is a constant now."""
+ import services.temporal.agent_activities as activities
- monkeypatch.setenv("AGENT_LLM_ENGINE", "surprise")
- with pytest.raises(ValueError, match="AGENT_LLM_ENGINE"):
- _configured_llm_engine()
+ assert activities._NATIVE_LLM_ENGINE == "native"
+ assert not hasattr(activities, "_configured_llm_engine")
+ assert not hasattr(activities, "_LEGACY_LLM_ENGINE")
class TestWorkflowBranchContract: