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>
This commit is contained in:
Nige
2026-03-14 13:06:36 -07:00
committed by GitHub
co-authored by gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
parent 3f2796b745
commit 5f0ed66a6b
2 changed files with 69 additions and 14 deletions
Generated
+13 -13
View File
@@ -151,7 +151,7 @@ version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.60.2",
] ]
[[package]] [[package]]
@@ -162,7 +162,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [ dependencies = [
"anstyle", "anstyle",
"once_cell_polyfill", "once_cell_polyfill",
"windows-sys 0.61.2", "windows-sys 0.60.2",
] ]
[[package]] [[package]]
@@ -2077,7 +2077,7 @@ dependencies = [
"libc", "libc",
"option-ext", "option-ext",
"redox_users 0.5.2", "redox_users 0.5.2",
"windows-sys 0.61.2", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@@ -2264,7 +2264,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.61.2", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@@ -4089,7 +4089,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@@ -5433,7 +5433,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys 0.12.1", "linux-raw-sys 0.12.1",
"windows-sys 0.61.2", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@@ -6115,7 +6115,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.61.2", "windows-sys 0.60.2",
] ]
[[package]] [[package]]
@@ -6337,10 +6337,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
dependencies = [ dependencies = [
"fastrand", "fastrand",
"getrandom 0.4.2", "getrandom 0.3.4",
"once_cell", "once_cell",
"rustix 1.1.4", "rustix 1.1.4",
"windows-sys 0.61.2", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@@ -7134,13 +7134,13 @@ checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971"
[[package]] [[package]]
name = "uds_windows" name = "uds_windows"
version = "1.2.0" version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b70b87d15e91f553711b40df3048faf27a7a04e01e0ddc0cf9309f0af7c2ca" checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e"
dependencies = [ dependencies = [
"memoffset", "memoffset",
"tempfile", "tempfile",
"windows-sys 0.61.2", "windows-sys 0.60.2",
] ]
[[package]] [[package]]
@@ -7996,7 +7996,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
+56 -1
View File
@@ -925,7 +925,8 @@ async fn execute_lightweight_with_tools(
.tool_definitions_excluding(ROUTINE_TOOL_DENYLIST) .tool_definitions_excluding(ROUTINE_TOOL_DENYLIST)
.await; .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_max_tokens(effective_max_tokens)
.with_temperature(0.3); .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<ChatMessage> {
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. /// Tools that must never be callable from lightweight routines.
/// ///
/// These tools pose autonomy-escalation risks: a routine could self-replicate, /// These tools pose autonomy-escalation risks: a routine could self-replicate,
@@ -1386,4 +1412,33 @@ mod tests {
let out = super::truncate(input, 5); let out = super::truncate(input, 5);
assert_eq!(out, "abcde..."); 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
}
} }