Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

anthropic: stop replaying unsigned thinking as text (#3759)

kte3e21add5aac
diff --git a/crates/pa-ai/src/providers/anthropic/convert.rs b/crates/pa-ai/src/providers/anthropic/convert.rs
index 1b9aa84a6e..1826d3cbf8 100644
--- a/crates/pa-ai/src/providers/anthropic/convert.rs
+++ b/crates/pa-ai/src/providers/anthropic/convert.rs
@@ -2,7 +2,7 @@
use serde_json::{json, Map, Value};
-use crate::providers::anthropic::{to_claude_code_name, CacheControl};
+use crate::providers::anthropic::{get_anthropic_compat, to_claude_code_name, CacheControl};
use crate::types::{
AssistantContent, Context, Message, Model, StopReason, Tool, UserMessageContent,
UserOrToolContent,
@@ -77,6 +77,7 @@ pub fn convert_messages(
cache_control: Option<&CacheControl>,
) -> Vec<Value> {
let mut params: Vec<Value> = Vec::new();
+ let requires_thinking_signature = get_anthropic_compat(model).requires_thinking_signature;
let transformed = crate::providers::transform_messages::transform_messages_with_normalizer(
&context.messages,
model,
@@ -164,19 +165,31 @@ pub fn convert_messages(
if thinking.thinking.trim().is_empty() {
continue;
}
+ // Unsigned thinking comes from an Anthropic-compatible endpoint that
+ // never signs (MiMo, Kimi, DeepSeek), or from a Claude turn that hit
+ // `max_tokens` before the signature arrived. (Aborted and errored turns
+ // never get here: the transform step drops them, and turns thinking
+ // from another model into text.) It is never replayed as `text`: the
+ // model reads that as its own earlier reply and starts echoing its
+ // reasoning (discussion #3391). Non-signing endpoints expect their
+ // reasoning passed back, so it replays as an unsigned `thinking`
+ // block; Claude rejects a thinking block without a valid signature, so
+ // there it is dropped.
let signature = thinking.thinking_signature.as_deref().unwrap_or("");
if signature.trim().is_empty() {
- blocks.push(json!({
- "type": "text",
- "text": sanitize_surrogates(&thinking.thinking),
- }));
- } else {
- blocks.push(json!({
- "type": "thinking",
- "thinking": sanitize_surrogates(&thinking.thinking),
- "signature": signature,
- }));
+ if !requires_thinking_signature {
+ blocks.push(json!({
+ "type": "thinking",
+ "thinking": sanitize_surrogates(&thinking.thinking),
+ }));
+ }
+ continue;
}
+ blocks.push(json!({
+ "type": "thinking",
+ "thinking": sanitize_surrogates(&thinking.thinking),
+ "signature": signature,
+ }));
}
AssistantContent::ToolCall(tool_call) => {
blocks.push(json!({
@@ -317,3 +330,123 @@ pub(crate) fn map_stop_reason(reason: &str) -> Result<StopReason, String> {
other => Err(format!("Unhandled stop reason: {other}")),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::convert_messages;
+ use crate::types::{Context, Message, Model};
+ use serde_json::{json, Value};
+
+ /// A model on a non-signing Anthropic-compatible endpoint (discussion #3391).
+ fn mimo() -> Model {
+ model(&json!({"id": "mimo-v2.6-pro", "provider": "xiaomi"}))
+ }
+
+ fn claude() -> Model {
+ model(&json!({"id": "claude-sonnet-5", "provider": "anthropic"}))
+ }
+
+ fn model(fields: &Value) -> Model {
+ let mut model = json!({
+ "name": "model", "api": "anthropic-messages", "baseUrl": "http://localhost",
+ "reasoning": true, "input": ["text"],
+ "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0},
+ "contextWindow": 128_000, "maxTokens": 8192
+ });
+ model
+ .as_object_mut()
+ .expect("model object")
+ .extend(fields.as_object().expect("fields object").clone());
+ serde_json::from_value(model).expect("model json")
+ }
+
+ /// The request messages for a user prompt, one same-model assistant turn of `blocks`, and a
+ /// follow-up prompt.
+ fn convert(model: &Model, blocks: &Value) -> Vec<Value> {
+ let messages: Vec<Message> = serde_json::from_value(json!([
+ {"role": "user", "content": "rebuild", "timestamp": 1},
+ {
+ "role": "assistant", "content": blocks,
+ "api": "anthropic-messages", "provider": model.provider, "model": model.id,
+ "usage": {
+ "input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0, "totalTokens": 0,
+ "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0, "total": 0}
+ },
+ "stopReason": "stop", "timestamp": 2
+ },
+ {"role": "user", "content": "again", "timestamp": 3}
+ ]))
+ .expect("messages json");
+ let context = Context {
+ system_prompt: None,
+ messages,
+ tools: None,
+ };
+ convert_messages(&context, model, false, None)
+ }
+
+ fn assistant_turn(model: &Model, blocks: &Value) -> Value {
+ convert(model, blocks)
+ .into_iter()
+ .find(|message| message["role"] == "assistant")
+ .unwrap_or(Value::Null)
+ }
+
+ fn unsigned_turn() -> Value {
+ json!([
+ {"type": "thinking", "thinking": "Let me do it fast.", "thinkingSignature": ""},
+ {"type": "thinking", "thinking": "No signature field at all."},
+ {"type": "text", "text": "Rebuilt."}
+ ])
+ }
+
+ /// Discussion #3391: a non-signing endpoint gets its unsigned thinking back as `thinking`
+ /// blocks, never as visible text the model then imitates.
+ #[test]
+ fn unsigned_thinking_replays_as_thinking_on_a_non_signing_endpoint() {
+ assert_eq!(
+ assistant_turn(&mimo(), &unsigned_turn()),
+ json!({"role": "assistant", "content": [
+ {"type": "thinking", "thinking": "Let me do it fast."},
+ {"type": "thinking", "thinking": "No signature field at all."},
+ {"type": "text", "text": "Rebuilt."}
+ ]})
+ );
+ }
+
+ /// Claude rejects a thinking block without a valid signature, so unsigned thinking is dropped
+ /// there, and on any model whose compat says it requires one.
+ #[test]
+ fn unsigned_thinking_is_dropped_where_a_signature_is_required() {
+ let mut flagged = mimo();
+ flagged.compat = serde_json::from_value(json!({"requiresThinkingSignature": true}))
+ .expect("compat json");
+ for model in [claude(), flagged] {
+ assert_eq!(
+ assistant_turn(&model, &unsigned_turn()),
+ json!({"role": "assistant", "content": [{"type": "text", "text": "Rebuilt."}]}),
+ "{}",
+ model.id
+ );
+ }
+ }
+
+ /// Signed thinking still replays as a `thinking` block with its signature.
+ #[test]
+ fn signed_thinking_replays_as_thinking() {
+ let turn = assistant_turn(
+ &claude(),
+ &json!([
+ {"type": "thinking", "thinking": "Plan.", "thinkingSignature": "sig"},
+ {"type": "text", "text": "Rebuilt."}
+ ]),
+ );
+ assert_eq!(
+ turn,
+ json!({"role": "assistant", "content": [
+ {"type": "thinking", "thinking": "Plan.", "signature": "sig"},
+ {"type": "text", "text": "Rebuilt."}
+ ]})
+ );
+ }
+}
diff --git a/crates/pa-ai/src/providers/anthropic/mod.rs b/crates/pa-ai/src/providers/anthropic/mod.rs
index a5db5f927a..ba705bc88a 100644
--- a/crates/pa-ai/src/providers/anthropic/mod.rs
+++ b/crates/pa-ai/src/providers/anthropic/mod.rs
@@ -151,6 +151,15 @@ pub struct AnthropicOptions {
pub struct ResolvedAnthropicCompat {
pub supports_eager_tool_input_streaming: bool,
pub supports_long_cache_retention: bool,
+ /// Unsigned thinking is dropped from replayed history instead of replayed as a `thinking`
+ /// block. Claude rejects a thinking block without a valid signature; Anthropic-compatible
+ /// endpoints that never sign (`MiMo`, Kimi, `DeepSeek`) expect their reasoning passed back.
+ pub requires_thinking_signature: bool,
+}
+
+/// Whether `model` is a Claude model, whichever provider routes it.
+fn is_claude_model(model: &Model) -> bool {
+ model.provider == "anthropic" || model.id.to_ascii_lowercase().contains("claude")
}
pub fn get_anthropic_compat(model: &Model) -> ResolvedAnthropicCompat {
@@ -167,6 +176,10 @@ pub fn get_anthropic_compat(model: &Model) -> ResolvedAnthropicCompat {
.as_ref()
.and_then(|c| c.supports_long_cache_retention)
.unwrap_or(true),
+ requires_thinking_signature: compat
+ .as_ref()
+ .and_then(|c| c.requires_thinking_signature)
+ .unwrap_or_else(|| is_claude_model(model)),
}
}
diff --git a/crates/pa-models/src/compat.rs b/crates/pa-models/src/compat.rs
index 2bcce3c4a7..4a1669795e 100644
--- a/crates/pa-models/src/compat.rs
+++ b/crates/pa-models/src/compat.rs
@@ -30,6 +30,7 @@ const OPENAI_RESPONSES_KEYS: &[&str] = &["sendSessionIdHeader", "supportsLongCac
const ANTHROPIC_MESSAGES_KEYS: &[&str] = &[
"supportsEagerToolInputStreaming",
"supportsLongCacheRetention",
+ "requiresThinkingSignature",
];
const THINKING_FORMATS: &[&str] = &[
@@ -237,6 +238,10 @@ mod tests {
"anthropic-messages",
Some(&compat(json!({"supportsEagerToolInputStreaming": true})))
));
+ assert!(is_model_compat(
+ "anthropic-messages",
+ Some(&compat(json!({"requiresThinkingSignature": false})))
+ ));
assert!(!is_model_compat(
"anthropic-messages",
Some(&compat(json!({"sendSessionIdHeader": true})))
diff --git a/crates/pa-types/src/ai/compat.rs b/crates/pa-types/src/ai/compat.rs
index 8d45ed2372..21cbedb259 100644
--- a/crates/pa-types/src/ai/compat.rs
+++ b/crates/pa-types/src/ai/compat.rs
@@ -85,6 +85,11 @@ pub struct AnthropicMessagesCompat {
pub supports_eager_tool_input_streaming: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub supports_long_cache_retention: Option<bool>,
+ /// Whether replayed thinking blocks must carry the provider's signature. When true, unsigned
+ /// thinking is left out of the replayed history; when false, it is replayed as an unsigned
+ /// `thinking` block. Default: true for Claude models, false otherwise.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub requires_thinking_signature: Option<bool>,
}
/// TS models `Model.compat` as an API-dependent conditional type; on the wire it is one of the
@@ -99,7 +104,10 @@ pub struct ModelCompat {
pub raw: JsonMap,
}
-const ANTHROPIC_COMPAT_KEYS: &[&str] = &["supportsEagerToolInputStreaming"];
+const ANTHROPIC_COMPAT_KEYS: &[&str] = &[
+ "supportsEagerToolInputStreaming",
+ "requiresThinkingSignature",
+];
const RESPONSES_COMPAT_KEYS: &[&str] = &["sendSessionIdHeader"];
/// Which compat object a `Model.compat` wire value carries.
#[derive(Debug, Clone, PartialEq)]