SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Assistant: drop earlier thinking blocks when history is trimmed, so Claude requests stop failing (#9417) (#9448)
AsTechEvolvesb1c132bc9a09
diff --git a/crates/ui-egui/src/agent_chat/api_client.rs b/crates/ui-egui/src/agent_chat/api_client.rsindex 2c9b57150e..0c8535992d 100644--- a/crates/ui-egui/src/agent_chat/api_client.rs+++ b/crates/ui-egui/src/agent_chat/api_client.rs@@ -30,6 +30,7 @@ const MAX_HISTORY: usize = 240;const MAX_HISTORY_IMAGES: usize = 2;/// What an older view becomes in the history.const DROPPED_VIEW_NOTE: &str = "[an earlier picture of the view, removed to keep the request small]";+const DROPPED_THINKING_NOTE: &str = "[earlier reasoning, removed with older history]";/// How long one tool call may take (it may wait for the user's Allow, declined after four/// minutes). It does not depend on the endpoint's request timeout: a tool call is SolidCraft's own/// work and the user's answer, never a wait for the model, so a longer request timeout does not@@ -876,8 +877,11 @@ impl Worker {fn turn(&mut self, parts: Vec<PromptPart>) {self.history.push(user_message(self.cfg.protocol, &parts));- drop_old_views(&mut self.history, MAX_HISTORY_IMAGES);- self.trim_history();+ let dropped = drop_old_views(&mut self.history, MAX_HISTORY_IMAGES);+ let trimmed = self.trim_history();+ if (dropped || trimmed) && self.cfg.protocol == Protocol::Anthropic {+ drop_thinking(&mut self.history);+ }// No cap on requests: Stop sets `cancel`, checked at the top of every round.loop {// The history as of here is complete (every tool call answered): a cancelled request@@ -964,14 +968,16 @@ impl Worker {}/// Keep the conversation bounded and starting at a user message, so neither protocol sees a- /// reply or a tool result without the message it answers.- fn trim_history(&mut self) {+ /// reply or a tool result without the message it answers. True when a message was removed.+ fn trim_history(&mut self) -> bool {+ let before = self.history.len();while self.history.len() > MAX_HISTORY {self.history.remove(0);}while self.history.first().is_some_and(|m| !starts_a_turn(m)) {self.history.remove(0);}+ self.history.len() != before}fn request(&self) -> Result<Reply, String> {@@ -1443,9 +1449,11 @@ fn anthropic_reply(body: &Value) -> Reply {/// Keeps the newest `keep` pictures of the view in the history and swaps every older one for a/// short note, so the model still knows a view was shown. Text is never touched. Pictures come-/// as OpenAI `image_url` parts or Anthropic `image` blocks, depending on the endpoint.-fn drop_old_views(history: &mut [Value], keep: usize) {+/// as OpenAI `image_url` parts or Anthropic `image` blocks, depending on the endpoint. True when+/// a picture was swapped.+fn drop_old_views(history: &mut [Value], keep: usize) -> bool {let mut seen = 0;+ let mut dropped = false;for msg in history.iter_mut().rev() {let Some(content) = msg.get_mut("content").and_then(Value::as_array_mut) else { continue };for part in content.iter_mut().rev() {@@ -1455,9 +1463,26 @@ fn drop_old_views(history: &mut [Value], keep: usize) {seen += 1;if seen > keep {*part = json!({"type": "text", "text": DROPPED_VIEW_NOTE});+ dropped = true;}}}+ dropped+}++/// Takes Anthropic thinking blocks out of every earlier reply. Each one is signed against all+/// the conversation before it, so once a dropped view or trimmed message changes that, the API+/// refuses the whole request (#9417); leaving earlier thinking out is allowed. A reply that was+/// only thinking keeps a note, as the API takes no assistant message without content.+fn drop_thinking(history: &mut [Value]) {+ for msg in history.iter_mut().filter(|m| m["role"] == "assistant") {+ let Some(content) = msg.get_mut("content").and_then(Value::as_array_mut) else { continue };+ let before = content.len();+ content.retain(|b| !matches!(b.get("type").and_then(Value::as_str), Some("thinking" | "redacted_thinking")));+ if content.is_empty() && before > 0 {+ content.push(json!({"type": "text", "text": DROPPED_THINKING_NOTE}));+ }+ }}/// MCP `tools/list` as OpenAI function tools (the hidden ones left out).@@ -1731,12 +1756,12 @@ mod tests {#[test]fn only_the_newest_views_stay_in_the_history_and_older_ones_become_a_note() {let mut h = vec![json!({"role": "system", "content": "note"}), view(1), json!({"role": "assistant", "content": "ok"}), view(2)];- drop_old_views(&mut h, 2);+ assert!(!drop_old_views(&mut h, 2));assert_eq!(pictures(&h).len(), 2, "two views fit, nothing is dropped");h.push(json!({"role": "user", "content": "plain text"}));h.push(view(3));- drop_old_views(&mut h, 2);+ assert!(drop_old_views(&mut h, 2));assert_eq!(pictures(&h), vec!["data:image/png;base64,V2", "data:image/png;base64,V3"]);// The oldest view is a note now; the words sent with it, and every other message, stay.assert_eq!(h[1]["content"][0]["text"], "look 1");@@ -2708,6 +2733,66 @@ mod tests {b.shutdown(true);}+ /// Thinking blocks are signed against the conversation before them: once an older view is+ /// dropped, the earlier ones go too, or Claude refuses the request (#9417). Until then, and+ /// for replies after the drop, they are sent back as they came.+ #[test]+ fn a_dropped_view_takes_earlier_thinking_with_it() {+ let seen = Arc::new(std::sync::Mutex::new(Vec::<Value>::new()));+ let log = seen.clone();+ let http: HttpFn = Arc::new(move |r: &HttpRequest| {+ if r.method == "GET" {+ return Ok((200, b"{\"data\":[]}".to_vec()));+ }+ let n = {+ let mut l = log.lock().unwrap();+ l.push(serde_json::from_slice(r.body.as_deref().unwrap_or(b"null")).unwrap());+ l.len()+ };+ let content = if n == 2 {+ json!([{"type": "redacted_thinking", "data": "r2"}])+ } else {+ json!([{"type": "thinking", "thinking": "", "signature": format!("s{n}")}, {"type": "text", "text": format!("Seen {n}.")}])+ };+ Ok((200, json!({"content": content, "stop_reason": "end_turn"}).to_string().into_bytes()))+ });+ let cfg = ApiConfig {+ name: "Test".into(),+ protocol: Protocol::Anthropic,+ base_url: "http://localhost:1".into(),+ model: "m".into(),+ key: Some("sk-secret".into()),+ http,+ request_timeout_s: 300,+ };+ let ctx = StartContext { cwd: std::env::temp_dir(), bridge: None, tools: None, wake: Box::new(|| {}) };+ let mut b = ApiBackend::start(cfg, ctx).unwrap();+ for n in 1..=3 {+ b.prompt(&[PromptPart::Text(format!("look {n}")), PromptPart::Image { mime: "image/png".into(), base64: format!("V{n}") }]);+ wait_for_turn(&mut b);+ }+ b.prompt(&[PromptPart::Text("and now?".into())]);+ wait_for_turn(&mut b);+ b.shutdown(true);+ let sent = seen.lock().unwrap();+ assert_eq!(sent.len(), 4);+ let replies = |i: usize| -> Vec<Value> {+ sent[i]["messages"].as_array().unwrap().iter().filter(|m| m["role"] == "assistant").map(|m| m["content"].clone()).collect()+ };+ // Two views fit: the reply goes back untouched, signature and all.+ assert_eq!(replies(1)[0][0]["signature"], "s1");+ // The third view drops the first: no earlier thinking is left, a thinking-only reply+ // keeps a note, and the words stay.+ let third = replies(2);+ assert_eq!(sent[2]["messages"][0]["content"][1]["text"], DROPPED_VIEW_NOTE);+ assert_eq!(third[0], json!([{"type": "text", "text": "Seen 1."}]));+ assert_eq!(third[1], json!([{"type": "text", "text": DROPPED_THINKING_NOTE}]));+ // Nothing is dropped for the next message, so the reply after the drop keeps its thinking.+ let fourth = replies(3);+ assert_eq!(fourth[..2], third[..]);+ assert_eq!(fourth[2][0]["signature"], "s3");+ }+#[test]fn the_anthropic_test_sends_one_small_message_and_checks_the_key_rule() {let seen = Arc::new(std::sync::Mutex::new(Vec::<HttpRequest>::new()));