Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
Fix Anthropic thinking replay after conversation prefix changes
trohitg1db7d2b9d5d6
diff --git a/docs-internal/errors.md b/docs-internal/errors.mdindex 2ca1de7d..46418b6a 100644--- a/docs-internal/errors.md+++ b/docs-internal/errors.md@@ -691,3 +691,40 @@ With a login that can see several Cloudflare accounts, operations fail instead w- cf has no `--account-id` flag on most commands. It takes the account from `CLOUDFLARE_ACCOUNT_ID`, or uses the only one the login can see, and with no terminal to ask it stops when there are several.**Fix**: log in again (Credentials → Cloudflare → Login: the modal shows a one-time code to approve on Cloudflare's page, from any browser), or paste an API token there. With several accounts, set the node's `account_id` to the account to use; the `whoami` operation lists them. See [Cloudflare Service](./cloudflare_service.md).++## 34. Anthropic rejects a resumed delegated agent with HTTP 400++**Symptom**: the first LLM step of a new delegated task fails with+`LLMError.invalid_request: Anthropic rejected the model request configuration.`+The same agent completed an earlier task successfully, and its Context is+connected.++**Evidence**: the reported Opus 5.5 failure replayed 21 messages and five tools.+The preceding successful run saved 20 messages, including four assistant+messages with signed thinking. The new delegation replaced the 829-character+system prompt with a 1,498-character prompt; tool definitions were identical,+and all tool results were paired. Temporal retained only the normalized+failure and request ID, so the original provider rejection text is unavailable.++**Likely cause**: these newer Claude models bind thinking to its original+conversation prefix. The delegation contract makes the new mission the system+message, and conversation seeding keeps that current system authoritative+while replaying prior assistant state. This combination can invalidate prior+thinking signatures. Anthropic documents it as a 400+`invalid_request_error`; the generic category alone cannot confirm it.++**Fix**: the Anthropic adapter opts supported model families into the+`thinking-binding-controls-2026-08-01` beta with+`thinking.block_binding.prefix_mismatch_behavior: "drop_block"`. Anthropic can+discard incompatible thinking while the agent keeps its conversation and+tool history. The setting also applies when `thinking_config` is absent,+because Opus 5.5 thinks by default. Model families without this capability+retain their existing request shape. See [Native LLM SDK → Anthropic thinking+after a prefix change](./native_llm_sdk.md#anthropic-thinking-after-a-prefix-change)+and [Anthropic's documented recovery](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking).++Subsequent recognized signature failures include a safe `diagnostic_code` in+logs and Temporal details, distinguishing `thinking_prefix_mismatch` from+`thinking_signature_invalid` without exposing provider response text. Restart+the backend/workers to load the adapter and configuration, then retry the+failed task; terminal failures do not retry themselves.diff --git a/docs-internal/native_llm_sdk.md b/docs-internal/native_llm_sdk.mdindex 44965dec..b942a39b 100644--- a/docs-internal/native_llm_sdk.md+++ b/docs-internal/native_llm_sdk.md@@ -463,6 +463,34 @@ in-process and Temporal turns. There is one wire standard (`MessageWire`) andno engine or wire-version discriminator is recorded (see the end of thisdocument).+### Anthropic thinking after a prefix change++Claude Opus/Sonnet/Haiku 5.5 and Fable/Mythos 5.1 bind signed thinking to+the system instructions, tools, and preceding messages. A delegated task can+replace the system instructions while loading the same agent's stored+conversation; changed policy, skills, or tool definitions can also change that+prefix. Replaying the old signed blocks then fails with HTTP 400 even when+every tool call has its result.++For these configured model families, `AnthropicProvider` sends the+`thinking-binding-controls-2026-08-01` beta and+`thinking.block_binding.prefix_mismatch_behavior: "drop_block"`, including+requests without an explicit `ThinkingConfig`. The local transcript stays+intact; Anthropic discards thinking it cannot reuse under the new prefix.+This also allows already affected conversations to continue without clearing+their context. Other request betas and gateway attribution remain in place.+This compatibility setting can increase generated thinking and cost after+prefix edits. Keep prefixes append-only where possible, while preserving the+application's requirement that current system instructions take effect.+See Anthropic's [preserved thinking guide](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking).++Known Anthropic thinking-signature rejections carry a fixed `diagnostic_code`+in the provider failure log and Temporal failure details:+`thinking_prefix_mismatch` or `thinking_signature_invalid`. Raw SDK rejection+text remains excluded from public errors, logs, and Temporal history because+it can contain request data. An unrecognized 400 retains the general+`invalid_request` category.+## Thinking and ReasoningThe unified `ThinkingConfig` dataclass is translated per provider:@@ -483,7 +511,7 @@ Each provider's `chat()` method reads only the fields it supports. The extracted| Provider | Models | Parameter | Thinking Type | Notes ||----------|--------|-----------|---------------|-------|-| **Claude** (adaptive) | `claude-opus-5*`, `claude-fable-5*`, `claude-mythos-5`, `claude-sonnet-5*`, `claude-opus-4-8`, `claude-opus-4-7` (`adaptive_thinking_models`, prefix-matched) | `thinkingEnabled` only — the budget is ignored | adaptive | Sends `{"type":"adaptive","display":"summarized"}` when enabled; when disabled the field is omitted (`disabled` is itself rejected on Fable/Mythos). `budget_tokens` is a 400 on these generations. No `temperature` / `top_p` / `top_k` is sent at all (`sampling_params_removed`). `anthropic.py:72-92`. |+| **Claude** (adaptive) | `claude-opus-5*`, `claude-fable-5*`, `claude-mythos-5`, `claude-sonnet-5*`, `claude-opus-4-8`, `claude-opus-4-7` (`adaptive_thinking_models`, prefix-matched) | `thinkingEnabled` only — the budget is ignored | adaptive | Sends `{"type":"adaptive","display":"summarized"}` when enabled. Otherwise the field is omitted, except when the model's prefix-binding policy requires adaptive thinking with `block_binding` (see above). `budget_tokens` is a 400 on these generations. No `temperature` / `top_p` / `top_k` is sent at all (`sampling_params_removed`). || **Claude** (budget) | `claude-sonnet-4-6`, `claude-haiku-4-5` and older 4.x/3.5 | `thinkingBudget` (1024-16000 tokens) | budget | `{"type":"enabled","budget_tokens":N}`; provider bumps `max_tokens` to `budget + 1024` if it is not already larger. Temperature auto-set to 1. || **Gemini** | gemini-3.x, gemini-2.5-pro/flash | `thinkingBudget` (token count); `thinkingLevel` on 3.x when set explicitly | budget | Uses `thinking_budget` API parameter (`thinking_level` only when the user set it — Vertex rejects an unsolicited level on 2.5-era models) || **OpenAI** | `o3`, `o4-mini` (reasoning-only; both on the API-shutdown path per `_models_note`, `o1`/`o3`/`o4` prefixes still detected) | `reasoningEffort` (low/medium/high) | effort | Reasoning-only models. Temperature omitted. |diff --git a/server/config/llm_defaults.json b/server/config/llm_defaults.jsonindex 13557b2e..6a4d2623 100644--- a/server/config/llm_defaults.json+++ b/server/config/llm_defaults.json@@ -155,6 +155,14 @@"claude-sonnet-5","claude-haiku-5"],+ "_thinking_prefix_binding_note": "These models bind replayed thinking to system/tools/earlier messages. Delegation and client-side compaction can intentionally change that prefix. AnthropicProvider sends thinking-binding-controls-2026-08-01 with block_binding.prefix_mismatch_behavior=drop_block so the API drops only incompatible reasoning while preserving stored signed blocks. Dropped reasoning can increase output tokens. Older models retain their existing request shape.",+ "thinking_prefix_binding_models": [+ "claude-fable-5-1",+ "claude-mythos-5-1",+ "claude-opus-5-5",+ "claude-sonnet-5-5",+ "claude-haiku-5-5"+ ],"_effort_models_note": "effort_models (prefix-matched): the models that take the chat picker's effort as output_config.effort, beside any adaptive thinking. The adaptive generations; the budget-thinking models have no effort setting.","effort_models": ["claude-fable-5",diff --git a/server/services/llm/protocol.py b/server/services/llm/protocol.pyindex 4be87292..57c77bf2 100644--- a/server/services/llm/protocol.py+++ b/server/services/llm/protocol.py@@ -369,6 +369,9 @@ class LLMError(Exception):# (services/gateway/errors.py): "check the provider's billing" would# point at the wrong account.public_hint: Optional[str] = None+ # Fixed diagnostic identifiers, never copied from a provider response.+ # These may cross logging and durable execution boundaries safely.+ diagnostic_code: Optional[str] = Nonedef __post_init__(self) -> None:Exception.__init__(self, self.message)@@ -563,6 +566,7 @@ class LLMError(Exception):if retry_after_value is not Noneelse None),+ diagnostic_code=_thinking_signature_diagnostic(provider, status, body),)@@ -829,6 +833,28 @@ def _http_date(value: Any) -> Optional[datetime]:return None+def _thinking_signature_diagnostic(provider: str, status: Optional[int], body: Mapping[str, Any]) -> Optional[str]:+ """Recognize Anthropic's stable signature errors without exposing text.++ The trailing explanation of which prefix changed is deliberately ignored:+ it can contain conversation content and its wording is not API-stable.+ """+ if provider != "anthropic" or status != 400 or body.get("type") != "invalid_request_error":+ return None+ message = body.get("message")+ if not isinstance(message, str):+ return None+ match = re.match(+ r"(?:messages\.\d+\.content\.\d+:\s*)?Invalid `signature` in `thinking` block(?:\.|$)",+ message,+ )+ if match is None:+ return None+ if message[match.end():].lstrip().startswith("The block is bound to a different conversation."):+ return "thinking_prefix_mismatch"+ return "thinking_signature_invalid"++def _error_body(exc: BaseException) -> Mapping[str, Any]:for attribute in ("body", "details"):body = getattr(exc, attribute, None)diff --git a/server/services/llm/providers/anthropic.py b/server/services/llm/providers/anthropic.pyindex 6144e476..e2e9a73d 100644--- a/server/services/llm/providers/anthropic.py+++ b/server/services/llm/providers/anthropic.py@@ -75,6 +75,7 @@ class AnthropicProvider:params["system"] = systemthinking_on = bool(thinking and thinking.enabled)+ betas: List[str] = []if policy["adaptive_thinking"]:# ``budget_tokens`` was removed on these generations and is a 400,@@ -94,6 +95,18 @@ class AnthropicProvider:params["max_tokens"] = budget + 1024params["thinking"] = {"type": "enabled", "budget_tokens": budget}+ if policy["thinking_prefix_binding"]:+ # Context can deliberately change between delegated missions or+ # at client-side compaction. New Claude models bind thinking to+ # that prefix. Let the API discard only incompatible reasoning,+ # preserving the signed history locally and valid blocks in tool+ # loops. Dropped reasoning can increase output-token usage.+ # The pinned SDK passes this beta field through unchanged.+ params.setdefault("thinking", {"type": "adaptive"})[+ "block_binding"+ ] = {"prefix_mismatch_behavior": "drop_block"}+ betas.append("thinking-binding-controls-2026-08-01")+if policy["sampling_params"]:# Anthropic requires temperature=1 alongside budget thinking.params["temperature"] = 1 if thinking_on else temperature@@ -110,8 +123,15 @@ class AnthropicProvider:# Per-request extras: the LLM gateway's attribution headers and# replay-cache body (ChatUnifier sends them for gateway calls only).- if extra_headers:- params["extra_headers"] = dict(extra_headers)+ headers = dict(extra_headers or {})+ # The beta SDK lets extra_headers overwrite its generated beta header.+ # Combine caller betas first, including differently cased headers, so+ # gateway attribution and every requested capability survive.+ for name in list(headers):+ if name.lower() == "anthropic-beta":+ betas.extend(value.strip() for value in headers.pop(name).split(",") if value.strip())+ if headers:+ params["extra_headers"] = headersif extra_body:params["extra_body"] = dict(extra_body)@@ -142,10 +162,12 @@ class AnthropicProvider:"type": "input_tokens","value": threshold,}+ betas.append("compact-2026-01-12")+ params["context_management"] = {"edits": [edit]}++ if betas:stream_ctx = self._client.beta.messages.stream(- betas=["compact-2026-01-12"],- context_management={"edits": [edit]},- **params,+ betas=list(dict.fromkeys(betas)), **params)else:stream_ctx = self._client.messages.stream(**params)@@ -213,6 +235,7 @@ class AnthropicProvider:return {"adaptive_thinking": matches("adaptive_thinking_models"),"sampling_params": not matches("sampling_params_removed"),+ "thinking_prefix_binding": matches("thinking_prefix_binding_models"),}def _split_system(self, messages: List[Message]):diff --git a/server/services/llm/unifier.py b/server/services/llm/unifier.pyindex a9c245c9..37b466fe 100644--- a/server/services/llm/unifier.py+++ b/server/services/llm/unifier.py@@ -562,6 +562,7 @@ class ChatUnifier:retry_after=error.retry_after,url=redact_url(getattr(client, "endpoint_url", None)),url_source=getattr(client, "url_source", None),+ **({"diagnostic_code": error.diagnostic_code} if error.diagnostic_code else {}),)diff --git a/server/services/temporal/agent_activities.py b/server/services/temporal/agent_activities.pyindex 242f9323..56246eed 100644--- a/server/services/temporal/agent_activities.py+++ b/server/services/temporal/agent_activities.py@@ -247,7 +247,7 @@ def _as_temporal_llm_error(error: Any, *, attempt: Optional[int] = None):"""Translate a structured SDK failure at the Temporal boundary.Raw provider messages can contain payload fragments or internal endpoint- details. The user-facing message is category-based, while the complete+ details. The user-facing message is category-based, while safestructured diagnostics remain JSON-safe in ``ApplicationError.details``and therefore in Temporal history for operators."""@@ -279,6 +279,7 @@ def _as_temporal_llm_error(error: Any, *, attempt: Optional[int] = None):"request_id": getattr(error, "request_id", None),"retry_after": provider_delay.total_seconds() if provider_delay is not None else None,"retry_after_raw": getattr(error, "retry_after_raw", None),+ **({"diagnostic_code": error.diagnostic_code} if getattr(error, "diagnostic_code", None) else {}),**({"hint": error.hint} if getattr(error, "hint", None) else {}),**({"requires_user_action": True} if getattr(error, "requires_user_action", False) else {}),}diff --git a/server/tests/llm/test_anthropic_thinking_binding.py b/server/tests/llm/test_anthropic_thinking_binding.pynew file mode 100644index 00000000..ed479369--- /dev/null+++ b/server/tests/llm/test_anthropic_thinking_binding.py@@ -0,0 +1,150 @@+"""Exercise prefix-binding recovery through the pinned Anthropic SDK transport.++These requests reuse signed thinking under a new delegated system prompt,+the condition that otherwise gets a 400 on current Claude models. Assert+the actual HTTP body/header, since older SDK TypedDicts omit the beta field.+"""++from copy import deepcopy+import json++import anthropic+import httpx+import pytest++from services.llm.protocol import Message, ThinkingConfig, ToolCall, ToolDef, message_to_wire+from services.llm.providers.anthropic import AnthropicProvider+++BINDING_BETA = "thinking-binding-controls-2026-08-01"+++def _stream_response(model):+ events = [+ {"type": "message_start", "message": {+ "id": "msg_recovered", "type": "message", "role": "assistant",+ "model": model, "content": [], "stop_reason": None,+ "stop_sequence": None, "usage": {"input_tokens": 10, "output_tokens": 0},+ }},+ {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}},+ {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Recovered"}},+ {"type": "content_block_stop", "index": 0},+ {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": None},+ "usage": {"output_tokens": 1}},+ {"type": "message_stop"},+ ]+ data = "".join(f"event: {event['type']}\ndata: {json.dumps(event)}\n\n" for event in events)+ return httpx.Response(200, headers={"content-type": "text/event-stream"}, content=data)+++async def transport_provider(monkeypatch):+ requests = []++ def handle(request):+ body = json.loads(request.content)+ requests.append((request, body))+ return _stream_response(body["model"])++ sdk_client = anthropic.AsyncAnthropic+ monkeypatch.setattr(anthropic, "AsyncAnthropic", lambda **kwargs: sdk_client(+ **kwargs, http_client=httpx.AsyncClient(transport=httpx.MockTransport(handle)),+ ))+ provider = AnthropicProvider("sk-test", max_retries=0)+ try:+ yield provider, requests+ finally:+ await provider._client.close()+++def _delegated_history():+ # Empty thinking text still carries the original signature and must not+ # be stripped or rewritten. Text, tool_use and tool_result also survive.+ content = [+ {"type": "thinking", "thinking": "", "signature": "original-signature"},+ {"type": "redacted_thinking", "data": "original-redacted-data"},+ {"type": "text", "text": "Checking the source"},+ {"type": "tool_use", "id": "call_1", "name": "source", "input": {}},+ ]+ return [+ Message(role="system", content="A new delegated mission"),+ Message(role="user", content="The earlier mission"),+ Message(role="assistant", content="Checking the source",+ tool_calls=[ToolCall(id="call_1", name="source", args={})],+ provider_state={"provider": "anthropic", "payload": {"content": content}}),+ Message(role="tool", content="Source material", tool_call_id="call_1"),+ Message(role="user", content="Continue with the new mission"),+ ]++[email protected]("thinking", [None, ThinkingConfig(enabled=False), ThinkingConfig(enabled=True)])+async def test_changed_delegated_prefix_keeps_signed_history_and_sends_recovery(transport_provider, thinking):+ provider, requests = transport_provider+ messages = _delegated_history()+ before = deepcopy([message_to_wire(message) for message in messages])+ result = await provider.chat(+ messages, model="claude-opus-5-5", max_tokens=128_000,+ thinking=thinking, tools=[ToolDef(name="source", description="Read", parameters={"type": "object"})],+ )++ assert result.content == "Recovered"+ assert len(requests) == 1+ request, body = requests[0]+ assert BINDING_BETA in request.headers["anthropic-beta"].split(",")+ assert body["thinking"]["type"] == "adaptive"+ assert body["thinking"]["block_binding"] == {"prefix_mismatch_behavior": "drop_block"}+ assert body["thinking"].get("display") == ("summarized" if thinking and thinking.enabled else None)+ assert "budget_tokens" not in body["thinking"]+ assert "temperature" not in body+ assert body["system"] == "A new delegated mission"+ assert body["messages"][1]["content"] == before[2]["provider_state"]["payload"]["content"]+ assert body["messages"][2]["content"][0]["tool_use_id"] == "call_1"+ assert [message_to_wire(message) for message in messages] == before++[email protected]("model", [+ "claude-fable-5-1", "claude-mythos-5-1", "claude-opus-5-5",+ "claude-sonnet-5-5", "claude-haiku-5-5",+])+async def test_prefix_binding_is_enabled_for_configured_models(transport_provider, model):+ provider, requests = transport_provider+ await provider.chat([Message(role="user", content="Hello")], model=model)+ request, body = requests[0]+ assert body["thinking"]["block_binding"]["prefix_mismatch_behavior"] == "drop_block"+ assert BINDING_BETA in request.headers["anthropic-beta"]++[email protected]("model", ["claude-fable-5", "claude-opus-5", "claude-sonnet-5", "claude-sonnet-4-6"])+async def test_older_models_keep_existing_request_shape(transport_provider, model):+ provider, requests = transport_provider+ await provider.chat([Message(role="user", content="Hello")], model=model)+ request, body = requests[0]+ assert "thinking" not in body+ assert "anthropic-beta" not in request.headers+ assert body.get("temperature") == (0.7 if model == "claude-sonnet-4-6" else None)+++async def test_binding_compaction_and_gateway_betas_are_merged_without_mutation(transport_provider):+ provider, requests = transport_provider+ headers = {+ "x-litellm-session-id": "session-1",+ "anthropic-beta": f"custom-beta,{BINDING_BETA}",+ "Anthropic-Beta": "other-beta",+ }+ extras = {"cache": {"use-cache": True, "namespace": "test-replay", "ttl": 172800}}+ before = deepcopy((headers, extras))+ await provider.chat(+ _delegated_history(), model="claude-opus-5-5",+ context_management={"type": "compaction", "compact_threshold": 150000},+ extra_headers=headers, extra_body=extras,+ )++ request, body = requests[0]+ betas = request.headers["anthropic-beta"].split(",")+ assert set(betas) == {BINDING_BETA, "compact-2026-01-12", "custom-beta", "other-beta"}+ assert len(betas) == len(set(betas))+ assert request.headers["x-litellm-session-id"] == "session-1"+ assert body["cache"] == extras["cache"]+ assert body["thinking"]["block_binding"] == {"prefix_mismatch_behavior": "drop_block"}+ assert body["context_management"]["edits"][0]["trigger"] == {"type": "input_tokens", "value": 150000}+ assert (headers, extras) == beforediff --git a/server/tests/llm/test_chat_effort.py b/server/tests/llm/test_chat_effort.pyindex 0ad1c024..e663c6e7 100644--- a/server/tests/llm/test_chat_effort.py+++ b/server/tests/llm/test_chat_effort.py@@ -28,8 +28,8 @@ def _anthropic_stream(provider):ctx = MagicMock()ctx.__aenter__ = AsyncMock(return_value=stream)ctx.__aexit__ = AsyncMock(return_value=False)- provider._client.messages.stream = MagicMock(return_value=ctx)- return provider._client.messages.stream+ provider._client.beta.messages.stream = MagicMock(return_value=ctx)+ return provider._client.beta.messages.stream@pytest.mark.asyncio@@ -38,11 +38,14 @@ async def test_anthropic_sends_effort_as_output_config():stream = _anthropic_stream(provider)await provider.chat([Message(role="user", content="Hi")], model="claude-sonnet-5-5", effort="high")assert stream.call_args.kwargs["output_config"] == {"effort": "high"}- # Thinking stays the agent's own: effort alone adds none.- assert "thinking" not in stream.call_args.kwargs+ # Prefix-binding recovery requires adaptive thinking on this model.+ # Effort changes depth without changing that recovery configuration.+ thinking = stream.call_args.kwargs["thinking"]+ assert thinking == {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "drop_block"}}await provider.chat([Message(role="user", content="Hi")], model="claude-sonnet-5-5")assert "output_config" not in stream.call_args.kwargs+ assert stream.call_args.kwargs["thinking"] == thinkingdef _openai():diff --git a/server/tests/llm/test_thinking_error_diagnostics.py b/server/tests/llm/test_thinking_error_diagnostics.pynew file mode 100644index 00000000..afd0afe8--- /dev/null+++ b/server/tests/llm/test_thinking_error_diagnostics.py@@ -0,0 +1,99 @@+"""Thinking replay failures expose fixed identifiers without provider text."""++from unittest.mock import MagicMock++import anthropic+import httpx+import pytest++from services.llm.protocol import LLMError, LLMErrorCategory+++INVALID_SIGNATURE = "messages.12.content.0: Invalid `signature` in `thinking` block."+PREFIX_MISMATCH = f"{INVALID_SIGNATURE} The block is bound to a different conversation."+SECRET = "private prompt sk-secret https://internal.test/?token=secret"+++def provider_exception(message, *, status=400, error_type="invalid_request_error"):+ response = httpx.Response(+ status,+ request=httpx.Request("POST", "https://api.anthropic.com/v1/messages"),+ headers={"request-id": "req-thinking"},+ )+ return anthropic.BadRequestError(+ message,+ response=response,+ body={"error": {"type": error_type, "message": message}},+ )++[email protected](("message", "expected"), [+ (PREFIX_MISMATCH, "thinking_prefix_mismatch"),+ (f"{PREFIX_MISMATCH} The system prompt changed: {SECRET}", "thinking_prefix_mismatch"),+ (f"{PREFIX_MISMATCH} New provider explanation: {SECRET}", "thinking_prefix_mismatch"),+ (INVALID_SIGNATURE, "thinking_signature_invalid"),+ ("Invalid `signature` in `thinking` block", "thinking_signature_invalid"),+ (f"{INVALID_SIGNATURE} {SECRET}", "thinking_signature_invalid"),+ (f"{INVALID_SIGNATURE} Unrelated explanation mentions: The block is bound to a different conversation.",+ "thinking_signature_invalid"),+])+def test_recognizes_only_stable_signature_sentences(message, expected):+ error = LLMError.from_exception("anthropic", provider_exception(message))++ assert error.diagnostic_code == expected+ assert error.category == LLMErrorCategory.INVALID_REQUEST+ assert error.retryable is False+ assert error.request_id == "req-thinking"+ assert error.message == message # Retained internally, never exported below.+ assert SECRET not in error.user_message+ assert SECRET not in error.hint++[email protected](("provider", "status", "error_type", "message"), [+ ("openai", 400, "invalid_request_error", PREFIX_MISMATCH),+ ("anthropic", 404, "invalid_request_error", PREFIX_MISMATCH),+ ("anthropic", 400, "other_error", PREFIX_MISMATCH),+ ("anthropic", 400, "invalid_request_error", "Invalid tool schema"),+ ("anthropic", 400, "invalid_request_error", f"Prompt contents include {PREFIX_MISMATCH}"),+ ("anthropic", 400, "invalid_request_error", None),+])+def test_other_failures_have_no_thinking_diagnostic(provider, status, error_type, message):+ exc = provider_exception(message, status=status, error_type=error_type)+ error = LLMError.from_exception(provider, exc)++ assert error.diagnostic_code is None+++def test_sdk_text_alone_cannot_introduce_a_diagnostic():+ exc = provider_exception(PREFIX_MISMATCH)+ exc.body = None++ assert LLMError.from_exception("anthropic", exc).diagnostic_code is None+++def test_failure_log_reports_fixed_diagnostic_without_raw_provider_text(monkeypatch):+ import services.llm.unifier as unifier_module++ logger = MagicMock()+ monkeypatch.setattr(unifier_module, "logger", logger)+ error = LLMError.from_exception("anthropic", provider_exception(f"{PREFIX_MISMATCH} {SECRET}"))++ unifier_module.ChatUnifier._log_failure("LLM provider request failed", error, None, "anthropic")++ fields = logger.warning.call_args.kwargs+ assert fields["diagnostic_code"] == "thinking_prefix_mismatch"+ assert fields["request_id"] == "req-thinking"+ assert SECRET not in str(logger.warning.call_args)+ assert PREFIX_MISMATCH not in str(logger.warning.call_args)+++def test_unrelated_failure_log_omits_diagnostic(monkeypatch):+ import services.llm.unifier as unifier_module++ logger = MagicMock()+ monkeypatch.setattr(unifier_module, "logger", logger)+ error = LLMError.from_exception("anthropic", provider_exception("Invalid tool schema"))++ unifier_module.ChatUnifier._log_failure("LLM provider request failed", error, None, "anthropic")++ assert "diagnostic_code" not in logger.warning.call_args.kwargsdiff --git a/server/tests/temporal/test_agent_llm_contract.py b/server/tests/temporal/test_agent_llm_contract.pyindex 3db0bb05..af1afdf3 100644--- a/server/tests/temporal/test_agent_llm_contract.py+++ b/server/tests/temporal/test_agent_llm_contract.py@@ -400,6 +400,34 @@ class TestWorkflowLoopContract:class TestLlmStepActivity:+ def test_thinking_diagnostic_crosses_temporal_boundary_without_provider_text(self):+ import anthropic+ import httpx++ from services.llm.protocol import LLMError+ from services.temporal.agent_activities import _as_temporal_llm_error++ secret = "private prompt sk-secret https://internal.test/?token=secret"+ message = (+ "messages.12.content.0: Invalid `signature` in `thinking` block. "+ "The block is bound to a different conversation. " + secret+ )+ response = httpx.Response(400, request=httpx.Request("POST", "https://api.anthropic.com/v1/messages"),+ headers={"request-id": "req-thinking"})+ sdk_error = anthropic.BadRequestError(message, response=response,+ body={"error": {"type": "invalid_request_error", "message": message}})++ temporal_error = _as_temporal_llm_error(LLMError.from_exception("anthropic", sdk_error))++ assert temporal_error.type == "LLMError.invalid_request"+ assert temporal_error.non_retryable is True+ assert temporal_error.next_retry_delay is None+ assert temporal_error.details[0]["diagnostic_code"] == "thinking_prefix_mismatch"+ assert temporal_error.details[0]["request_id"] == "req-thinking"+ assert secret not in str(temporal_error)+ assert secret not in str(temporal_error.details)+ assert message not in str(temporal_error.details)+def test_structured_llm_error_becomes_safe_temporal_failure(self):from services.llm.protocol import LLMError, LLMErrorCategoryfrom services.temporal.agent_activities import (