From 5f0ed66a6ba6838fd4f9b057e7346a4a6467aac3 Mon Sep 17 00:00:00 2001 From: Nige Date: Sat, 14 Mar 2026 20:06:36 +0000 Subject: [PATCH] perf(routines): avoid full message history clone each tool iteration (#1172) * perf(routines): bound tool-loop history snapshot clone cost * test(ci): annotate snapshot assertions for no-panics matcher * test(ci): keep no-panics suppression on single-line assertion * test(ci): keep snapshot tail assert single-line for no-panics * Update src/agent/routine_engine.rs Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * chore(deps): bump yanked uds_windows in lockfile for cargo-deny --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- Cargo.lock | 26 ++++++++--------- src/agent/routine_engine.rs | 57 ++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c6b3e6f1..f51c3e65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,7 +151,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -162,7 +162,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -2077,7 +2077,7 @@ dependencies = [ "libc", "option-ext", "redox_users 0.5.2", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -2264,7 +2264,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -4089,7 +4089,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -5433,7 +5433,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -6115,7 +6115,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -6337,10 +6337,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.2", + "getrandom 0.3.4", "once_cell", "rustix 1.1.4", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -7134,13 +7134,13 @@ checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" [[package]] name = "uds_windows" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b70b87d15e91f553711b40df3048faf27a7a04e01e0ddc0cf9309f0af7c2ca" +checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ "memoffset", "tempfile", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -7996,7 +7996,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.48.0", ] [[package]] diff --git a/src/agent/routine_engine.rs b/src/agent/routine_engine.rs index a34654e9..53b5c883 100644 --- a/src/agent/routine_engine.rs +++ b/src/agent/routine_engine.rs @@ -925,7 +925,8 @@ async fn execute_lightweight_with_tools( .tool_definitions_excluding(ROUTINE_TOOL_DENYLIST) .await; - let request = ToolCompletionRequest::new(messages.clone(), tool_defs) + let request_messages = snapshot_messages_for_tool_iteration(&messages); + let request = ToolCompletionRequest::new(request_messages, tool_defs) .with_max_tokens(effective_max_tokens) .with_temperature(0.3); @@ -1001,6 +1002,31 @@ async fn execute_lightweight_with_tools( } } +// Bound per-iteration context copy cost for lightweight tool loops. +const MAX_TOOL_LOOP_MESSAGES: usize = 32; + +fn snapshot_messages_for_tool_iteration(messages: &[ChatMessage]) -> Vec { + if messages.len() <= MAX_TOOL_LOOP_MESSAGES { + return messages.to_vec(); + } + + let mut snapshot = Vec::with_capacity(MAX_TOOL_LOOP_MESSAGES); + + if let Some(first) = messages.first() + && first.role == crate::llm::Role::System + { + snapshot.push(first.clone()); + let tail_len = MAX_TOOL_LOOP_MESSAGES - 1; + let tail_start = (messages.len() - tail_len).max(1); + snapshot.extend_from_slice(&messages[tail_start..]); + } else { + let tail_start = messages.len() - MAX_TOOL_LOOP_MESSAGES; + snapshot.extend_from_slice(&messages[tail_start..]); + } + + snapshot +} + /// Tools that must never be callable from lightweight routines. /// /// These tools pose autonomy-escalation risks: a routine could self-replicate, @@ -1386,4 +1412,33 @@ mod tests { let out = super::truncate(input, 5); assert_eq!(out, "abcde..."); } + + #[test] + fn test_snapshot_messages_keeps_system_and_recent_tail() { + let mut messages = vec![crate::llm::ChatMessage::system("sys")]; + for i in 0..80 { + messages.push(crate::llm::ChatMessage::user(format!("u{i}"))); + } + + let snapshot = super::snapshot_messages_for_tool_iteration(&messages); + assert_eq!(snapshot.len(), super::MAX_TOOL_LOOP_MESSAGES); // safety: test-only no-panics CI false positive + assert_eq!(snapshot[0].role, crate::llm::Role::System); // safety: test-only no-panics CI false positive + assert_eq!(snapshot[0].content, "sys"); // safety: test-only no-panics CI false positive + let last_content = snapshot.last().map(|m| m.content.as_str()); + assert_eq!(last_content, Some("u79")); // safety: test-only no-panics CI false positive + } + + #[test] + fn test_snapshot_messages_unchanged_when_within_limit() { + let messages = vec![ + crate::llm::ChatMessage::system("sys"), + crate::llm::ChatMessage::user("a"), + crate::llm::ChatMessage::assistant("b"), + ]; + let snapshot = super::snapshot_messages_for_tool_iteration(&messages); + assert_eq!(snapshot.len(), messages.len()); // safety: test-only no-panics CI false positive + assert_eq!(snapshot[0].role, crate::llm::Role::System); // safety: test-only no-panics CI false positive + assert_eq!(snapshot[1].content, "a"); // safety: test-only no-panics CI false positive + assert_eq!(snapshot[2].content, "b"); // safety: test-only no-panics CI false positive + } }