Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.
Rust
feat(vgr): restore optional handoff context compaction (#1043)
GDRS13a4e8063caf
diff --git a/crates/libsy/src/algorithms/vgr.rs b/crates/libsy/src/algorithms/vgr.rsindex 7a94338f4..d2db4fa77 100644--- a/crates/libsy/src/algorithms/vgr.rs+++ b/crates/libsy/src/algorithms/vgr.rs@@ -19,6 +19,7 @@ use crate::algorithms::fall_through::FallThrough;use crate::algorithms::util::affinity::AffinityRouter;use crate::core::algorithm::{Algorithm, Driver, RoutingOutcome};+mod compaction;mod config;mod decide;mod readout;@@ -52,12 +53,15 @@ impl Vgr {"turn_verification_latched"};let affinity = Arc::new(affinity);+ let mut route = FallThrough::new_with_state().with_name("vgr");+ if config.compact_handoff {+ route = route.with_classifier(Arc::new(compaction::Compactor));+ }let classifier = Arc::new(runtime::VgrClassifier {breaker: safety::CircuitBreaker::new(config.breaker),config,});- let route = FallThrough::new_with_state()- .with_name("vgr")+ let route = route.with_processor(affinity.clone()).with_classifier(Arc::new(runtime::Held(affinity, reason))).with_classifier(classifier.clone());diff --git a/crates/libsy/src/algorithms/vgr/compaction.rs b/crates/libsy/src/algorithms/vgr/compaction.rsnew file mode 100644index 000000000..2329da797--- /dev/null+++ b/crates/libsy/src/algorithms/vgr/compaction.rs@@ -0,0 +1,480 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++//! Condenses local history into a recorded handoff digest.+//!+//! Later requests reuse the digest only when their original history prefix+//! matches. User updates and tool content are clipped, so the digest is lossy.++use std::collections::hash_map::DefaultHasher;+use std::hash::{Hash, Hasher};++use async_trait::async_trait;+use switchyard_protocol::{ContentBlock, Message, Request, Response, Role, ToolCall, ToolResult};++use super::text::{clip_mid, turn_start};+use crate::Result;+use crate::algorithms::util::affinity::{has_new_user_turn, initiated_by_user};+use crate::algorithms::util::prompts::drop_exact_replay;+use crate::core::algorithm::Driver;+use crate::core::classifier::{Classification, Classifier};+use crate::core::state::{State, StateValue};+use serde::{Deserialize, Serialize};++const RECORD_KEY: &str = "vgr_compaction";++#[derive(Serialize, Deserialize)]+struct Recorded {+ head: u32,+ cut: u32,+ anchor: String,+ summary: String,+}++/// Tool steps shown in detail; earlier steps receive short entries.+const RECENT_STEPS: usize = 3;+const RECENT_ARGUMENT_BUDGET: usize = 600;+const RECENT_RESULT_BUDGET: usize = 800;+const EARLY_ARGUMENT_BUDGET: usize = 100;+const EARLY_RESULT_BUDGET: usize = 80;+const EARLY_SECTION_BUDGET: usize = 2_500;+const NARRATION_BUDGET: usize = 200;+const RECENT_USER_TEXTS: usize = 2;++/// Condenses everything after the task message and records how, so later+/// requests of this user turn can be rewritten identically.+///+/// Returns `false`, leaving the request untouched, when there is no task+/// message with local work after it.+pub(super) fn compact(request: &mut Request, state: &mut State, note: &str) -> bool {+ let messages = &request.llm_request.messages;+ let start = turn_start(request);+ let Some(task) = messages[start..].iter().position(is_task_message) else {+ return false;+ };+ let head = start + task + 1;+ let cut = messages.len();+ if cut <= head {+ return false;+ }+ let mut summary = digest(&messages[head..cut]);+ summary.push_str(note);+ let anchor = fingerprint(&messages[..cut]);+ let (Ok(head_count), Ok(cut_count)) = (u32::try_from(head), u32::try_from(cut)) else {+ return false;+ };+ let record = Recorded {+ head: head_count,+ cut: cut_count,+ anchor,+ summary,+ };+ let Ok(serialized) = serde_json::to_string(&record) else {+ return false;+ };+ rewrite(request, head, cut, &record.summary);+ state+ .extra+ .insert(RECORD_KEY.into(), StateValue::String(serialized));+ true+}++/// Reapplies a recorded compaction to a later request of the same user turn.+///+/// Registered ahead of every other classifier and never votes.+pub(super) struct Compactor;++#[async_trait]+impl Classifier for Compactor {+ async fn score(+ &self,+ state: &mut State,+ request: &mut Request,+ _driver: &Driver,+ ) -> Result<(Classification, Option<Response>)> {+ if initiated_by_user(request)+ .unwrap_or_else(|| has_new_user_turn(&request.llm_request.messages))+ {+ state.extra.remove(RECORD_KEY);+ } else if let Some((head, cut, summary)) = recorded(state, request) {+ rewrite(request, head, cut, &summary);+ }+ Ok((Classification::Scores(Vec::new()), None))+ }+}++/// The recorded compaction, when the request still begins with the condensed history.+fn recorded(state: &State, request: &Request) -> Option<(usize, usize, String)> {+ let StateValue::String(serialized) = state.extra.get(RECORD_KEY)? else {+ return None;+ };+ let record: Recorded = serde_json::from_str(serialized).ok()?;+ let head = usize::try_from(record.head).ok()?;+ let cut = usize::try_from(record.cut).ok()?;+ let messages = &request.llm_request.messages;+ (head > 0+ && head < cut+ && cut <= messages.len()+ && fingerprint(&messages[..cut]) == record.anchor)+ .then(|| (head, cut, record.summary.clone()))+}++fn rewrite(request: &mut Request, head: usize, cut: usize, summary: &str) {+ let messages = &mut request.llm_request.messages;+ messages[head - 1].content.push(ContentBlock::Text {+ text: summary.to_string(),+ });+ messages.drain(head..cut);+ drop_exact_replay(request);+}++fn is_task_message(message: &Message) -> bool {+ message.role == Role::User+ && message+ .content+ .iter()+ .any(|block| matches!(block, ContentBlock::Text { .. }))+}++fn fingerprint(messages: &[Message]) -> String {+ let mut hasher = DefaultHasher::new();+ serde_json::to_string(messages)+ .unwrap_or_default()+ .hash(&mut hasher);+ format!("{:016x}", hasher.finish())+}++/// One tool call and what came back, in conversation order.+struct Step<'a> {+ narration: Vec<&'a str>,+ call: &'a ToolCall,+ result: Option<&'a ToolResult>,+}++fn result_text(result: &ToolResult) -> String {+ result+ .content+ .iter()+ .map(|part| match part {+ ContentBlock::Text { text } => text.as_str(),+ _ => "[non-text content]",+ })+ .collect::<Vec<_>>()+ .join("\n")+}++fn digest(messages: &[Message]) -> String {+ let mut steps = Vec::<Step<'_>>::new();+ let mut narration = Vec::new();+ let mut user_text = Vec::new();+ for message in messages {+ for block in &message.content {+ match block {+ ContentBlock::Text { text } if message.role == Role::Assistant => {+ narration.push(text.as_str());+ }+ ContentBlock::Text { text } if message.role == Role::User => {+ user_text.push(clip_mid(text, RECENT_RESULT_BUDGET, 0.5));+ }+ ContentBlock::ToolCall(call) => steps.push(Step {+ narration: std::mem::take(&mut narration),+ call,+ result: None,+ }),+ ContentBlock::ToolResult(result) => {+ if let Some(step) = steps+ .iter_mut()+ .rev()+ .find(|step| step.call.id == result.tool_call_id && step.result.is_none())+ {+ step.result = Some(result);+ }+ }+ _ => {}+ }+ }+ }++ let recent_from = steps.len().saturating_sub(RECENT_STEPS);+ let mut early = String::new();+ for (index, step) in steps[..recent_from].iter().enumerate() {+ let body = step.result.map(result_text).unwrap_or_default();+ let status = match step.result {+ Some(result) if result.is_error == Some(true) => "error",+ Some(_) => "ok",+ None => "no result",+ };+ let first_line = body+ .lines()+ .find(|line| !line.trim().is_empty())+ .unwrap_or("");+ early.push_str(&format!(+ "[{}] {} {} -> {status}: {}\n",+ index + 1,+ step.call.name,+ clip_mid(&step.call.arguments.to_string(), EARLY_ARGUMENT_BUDGET, 0.7),+ clip_mid(first_line, EARLY_RESULT_BUDGET, 0.7),+ ));+ }+ let mut out = String::from(+ "\n\n[Context note from the serving infrastructure, not from the user] The tool-using \+ work that followed this message was condensed into the digest below. Earlier steps \+ are one line each; the most recent steps are shown in detail.\n<condensed_history>\n",+ );+ out.push_str(&clip_mid(&early, EARLY_SECTION_BUDGET, 0.3));+ for (offset, step) in steps[recent_from..].iter().enumerate() {+ let narration = step.narration.concat();+ if !narration.trim().is_empty() {+ out.push_str(&format!(+ "assistant: {}\n",+ clip_mid(narration.trim(), NARRATION_BUDGET, 0.5)+ ));+ }+ out.push_str(&format!(+ "[{}] {} {}\n",+ recent_from + offset + 1,+ step.call.name,+ clip_mid(+ &step.call.arguments.to_string(),+ RECENT_ARGUMENT_BUDGET,+ 0.5+ )+ ));+ match step.result {+ Some(result) => out.push_str(&format!(+ "result{}:\n{}\n",+ if result.is_error == Some(true) {+ " (error)"+ } else {+ ""+ },+ clip_mid(&result_text(result), RECENT_RESULT_BUDGET, 0.3)+ )),+ None => out.push_str("result: none\n"),+ }+ }+ let narration = narration.concat();+ if !narration.trim().is_empty() {+ out.push_str(&format!(+ "assistant: {}\n",+ clip_mid(narration.trim(), NARRATION_BUDGET, 0.5)+ ));+ }+ for text in &user_text[user_text.len().saturating_sub(RECENT_USER_TEXTS)..] {+ out.push_str(&format!("user: {text}\n"));+ }+ out.push_str("</condensed_history>");+ out+}++#[cfg(test)]+mod tests {+ use super::*;+ use switchyard_protocol::{LlmRequest, ToolCall, ToolResult};++ fn call(id: &str, command: &str) -> Message {+ Message {+ role: Role::Assistant,+ content: vec![ContentBlock::ToolCall(ToolCall {+ id: id.into(),+ name: "bash".into(),+ arguments: serde_json::json!({ "command": command }),+ })],+ }+ }++ fn result(id: &str, text: &str) -> Message {+ Message {+ role: Role::Tool,+ content: vec![ContentBlock::ToolResult(ToolResult {+ tool_call_id: id.into(),+ content: vec![ContentBlock::Text { text: text.into() }],+ is_error: None,+ })],+ }+ }++ fn session(steps: usize) -> Request {+ let mut messages = vec![Message::text(Role::User, "build the thing")];+ for index in 0..steps {+ let id = format!("call-{index}");+ messages.push(call(&id, &format!("step {index}")));+ messages.push(result(&id, &format!("output {index}")));+ }+ Request {+ llm_request: LlmRequest {+ messages,+ ..LlmRequest::default()+ },+ raw_request: None,+ metadata: None,+ }+ }++ async fn reapply(state: &mut State, request: &mut Request) {+ let driver = Driver::new(+ "test",+ std::sync::Arc::new(crate::core::algorithm::RuntimeModels::new(+ std::collections::HashMap::new(),+ )),+ )+ .0;+ Compactor+ .score(state, request, &driver)+ .await+ .expect("the compactor never fails");+ }++ #[tokio::test]+ async fn later_requests_see_the_same_condensed_prefix() {+ let mut state = State::default();+ let mut handoff = session(12);+ assert!(compact(&mut handoff, &mut state, "NOTE"));+ assert_eq!(handoff.llm_request.messages.len(), 1);+ let condensed = handoff.llm_request.messages[0].clone();+ let digest = condensed.text_content("").expect("task text");+ assert!(digest.contains("step 11") && digest.ends_with("NOTE"));++ let saved = serde_json::to_string(&state).expect("serializable routing state");+ let mut state: State = serde_json::from_str(&saved).expect("restored routing state");++ let mut follow_up = session(12);+ follow_up.llm_request.messages.push(call("cloud-1", "ls"));+ follow_up+ .llm_request+ .messages+ .push(result("cloud-1", "a b"));+ reapply(&mut state, &mut follow_up).await;++ let messages = &follow_up.llm_request.messages;+ assert_eq!(+ messages.len(),+ 3,+ "task, then only the capable tier's own work"+ );+ assert_eq!(+ messages[0], condensed,+ "the cached prefix is byte-identical"+ );+ }++ #[tokio::test]+ async fn a_rewritten_history_is_sent_untouched() {+ let mut state = State::default();+ let mut handoff = session(4);+ assert!(compact(&mut handoff, &mut state, "NOTE"));++ let mut edited = session(4);+ edited.llm_request.messages[2] = result("call-0", "the client rewrote this");+ let before = edited.llm_request.messages.clone();+ reapply(&mut state, &mut edited).await;+ assert_eq!(edited.llm_request.messages, before);+ }++ #[test]+ fn compaction_keeps_agent_nudges_in_the_current_turn() {+ let mut request = session(4);+ request+ .llm_request+ .messages+ .push(Message::text(Role::Assistant, "done"));+ request+ .llm_request+ .messages+ .push(Message::text(Role::User, "verify the change"));+ let mut headers = http::HeaderMap::new();+ headers.insert("x-initiator", "agent".parse().expect("header"));+ request.metadata = Some(switchyard_protocol::Metadata {+ http_headers: Some(headers),+ ..Default::default()+ });+ assert!(compact(&mut request, &mut State::default(), "NOTE"));+ assert_eq!(request.llm_request.messages.len(), 1);+ let digest = request.llm_request.messages[0].text_content("").unwrap();+ assert!(digest.starts_with("build the thing"));+ assert!(digest.contains("verify the change"));+ }++ #[tokio::test]+ async fn a_new_user_turn_forgets_the_compaction() {+ let mut state = State::default();+ let mut handoff = session(4);+ assert!(compact(&mut handoff, &mut state, "NOTE"));++ let mut next_turn = session(4);+ next_turn+ .llm_request+ .messages+ .push(Message::text(Role::User, "now do another thing"));+ let before = next_turn.llm_request.messages.clone();+ reapply(&mut state, &mut next_turn).await;+ assert_eq!(next_turn.llm_request.messages, before);+ assert!(!state.extra.contains_key(RECORD_KEY));+ }++ #[tokio::test]+ async fn initiator_tags_keep_agent_nudges_on_the_compacted_prefix() {+ for initiator in ["agent", "user"] {+ let mut state = State::default();+ assert!(compact(&mut session(4), &mut state, "NOTE"));+ let mut next = session(4);+ next.llm_request+ .messages+ .push(Message::text(Role::User, "verify"));+ let mut headers = http::HeaderMap::new();+ headers.insert("x-initiator", initiator.parse().expect("header"));+ next.metadata = Some(switchyard_protocol::Metadata {+ http_headers: Some(headers),+ ..Default::default()+ });+ let original = next.llm_request.messages.clone();+ reapply(&mut state, &mut next).await;+ assert_eq!(state.extra.contains_key(RECORD_KEY), initiator == "agent");+ if initiator == "agent" {+ assert_eq!(next.llm_request.messages.len(), 2);+ } else {+ assert_eq!(next.llm_request.messages, original);+ }+ }+ }++ #[test]+ fn batched_results_are_paired_by_tool_call_id() {+ let calls = Message {+ role: Role::Assistant,+ content: ["a", "b"]+ .into_iter()+ .map(|id| {+ ContentBlock::ToolCall(ToolCall {+ id: id.into(),+ name: format!("tool_{id}"),+ arguments: serde_json::json!({}),+ })+ })+ .collect(),+ };+ for order in [["a", "b"], ["b", "a"]] {+ let results = Message {+ role: Role::Tool,+ content: order+ .into_iter()+ .map(|id| {+ ContentBlock::ToolResult(ToolResult {+ tool_call_id: id.into(),+ content: vec![ContentBlock::Text {+ text: format!("output_{id}"),+ }],+ is_error: Some(id == "b"),+ })+ })+ .collect(),+ };+ let view = digest(&[calls.clone(), results]);+ assert!(view.contains("tool_a {}\nresult:\noutput_a"), "{view}");+ assert!(+ view.contains("tool_b {}\nresult (error):\noutput_b"),+ "{view}"+ );+ }+ }+}diff --git a/crates/libsy/src/algorithms/vgr/config.rs b/crates/libsy/src/algorithms/vgr/config.rsindex 987318c03..dd1228c85 100644--- a/crates/libsy/src/algorithms/vgr/config.rs+++ b/crates/libsy/src/algorithms/vgr/config.rs@@ -64,6 +64,8 @@ pub struct VgrConfig {/// Tells the capable tier, once per user turn, that it inherits unverified/// tool-using work.pub agentic_handoff: bool,+ /// Condenses local history at handoff. Requires `agentic_handoff`.+ pub compact_handoff: bool,/// Wall-clock budget for the local tier within one user turn, including the/// client's tool execution.pub local_turn_budget: Option<Duration>,@@ -90,12 +92,18 @@ impl VgrConfig {local_supports_images: false,confirmed_recovery_min_clean_tail: None,agentic_handoff: false,+ compact_handoff: false,local_turn_budget: None,latch_escalation: false,}}pub(super) fn validate(&self) -> Result<()> {+ if self.compact_handoff && !self.agentic_handoff {+ return Err(LibsyError::AlgorithmError {+ message: "compact_handoff requires agentic_handoff".into(),+ });+ }if self.targets.local == self.targets.cloud {return Err(LibsyError::AlgorithmError {message: "vgr requires distinct local and cloud model IDs".into(),@@ -132,6 +140,16 @@ impl VgrConfig {mod tests {use super::*;+ #[test]+ fn compaction_requires_handoff_for_direct_callers() {+ let mut config = VgrConfig::new("local".into(), "cloud".into());+ assert!(!config.compact_handoff);+ config.compact_handoff = true;+ assert!(super::super::Vgr::new(config.clone()).is_err());+ config.agentic_handoff = true;+ assert!(super::super::Vgr::new(config).is_ok());+ }+#[test]fn direct_callers_must_use_distinct_local_and_cloud_ids() {assert!(super::super::Vgr::new(VgrConfig::new("same".into(), "same".into())).is_err());diff --git a/crates/libsy/src/algorithms/vgr/runtime.rs b/crates/libsy/src/algorithms/vgr/runtime.rsindex b00647ce4..c8e350819 100644--- a/crates/libsy/src/algorithms/vgr/runtime.rs+++ b/crates/libsy/src/algorithms/vgr/runtime.rs@@ -305,7 +305,9 @@ impl VgrClassifier {set_flag(state, HANDOFF_SENT_KEY, true);set_flag(state, LOCAL_SERVED_KEY, false);let note = handoff_note(unverified_final);- append_note(request, ¬e);+ if !(self.config.compact_handoff && super::compaction::compact(request, state, ¬e)) {+ append_note(request, ¬e);+ }}/// Judges one proposed tool call; `true` escalates the rest of the session.diff --git a/crates/libsy/src/algorithms/vgr/runtime/tests.rs b/crates/libsy/src/algorithms/vgr/runtime/tests.rsindex f8018166e..2708e65b5 100644--- a/crates/libsy/src/algorithms/vgr/runtime/tests.rs+++ b/crates/libsy/src/algorithms/vgr/runtime/tests.rs@@ -505,6 +505,116 @@ async fn latched_sessions_do_not_repeat_the_handoff_on_later_user_turns() -> ResOk(())}+#[tokio::test]+async fn compact_handoff_is_optional_and_replays_before_retained_cloud_routing() -> Result<()> {+ let previous = vec![+ Message::text(Role::User, "inspect the repository"),+ call("old"),+ result("old", "previous output"),+ Message::text(Role::Assistant, "inspection complete"),+ ];+ for compact_handoff in [None, Some(false), Some(true)] {+ let route = route(|config| {+ config.agentic_handoff = true;+ if let Some(enabled) = compact_handoff {+ config.compact_handoff = enabled;+ }+ })?;+ assert_eq!(+ drive(+ &route,+ session(+ previous+ .iter()+ .cloned()+ .chain([Message::text(Role::User, "fix the build")])+ .collect(),+ ),+ Arc::new(Calls::default()),+ complete_call,+ 0.0,+ )+ .await?,+ "local"+ );+ let sent = Arc::new(parking_lot::Mutex::new(Vec::new()));+ let local_calls = Arc::new(AtomicUsize::new(0));+ for follow_up in [false, true] {+ let mut messages = previous.clone();+ messages.extend([+ Message::text(Role::User, "fix the build"),+ call("c1"),+ result("c1", "ok"),+ ]);+ if follow_up {+ messages.extend([call("cloud-1"), result("cloud-1", "cloud output")]);+ }+ let sent = Arc::clone(&sent);+ let local_calls = Arc::clone(&local_calls);+ let (selected, _) = test_drive_with_models(+ Arc::clone(&route),+ session(messages),+ category_models(Category::Any, &["local", "cloud"]),+ move |target: ModelId, request: Request| {+ let sent = Arc::clone(&sent);+ let local_calls = Arc::clone(&local_calls);+ async move {+ Ok(match target.as_str() {+ "local" => {+ local_calls.fetch_add(1, Ordering::Relaxed);+ reply("the build passes")+ }+ "judge" => scored(0.0),+ "cloud" => {+ sent.lock().push(request.llm_request.messages);+ reply("cloud answer")+ }+ _ => reply("no"),+ })+ }+ },+ )+ .await?;+ assert_eq!(selected, "cloud");+ }+ assert_eq!(local_calls.load(Ordering::Relaxed), 1);+ let sent = sent.lock();+ assert_eq!(sent.len(), 2);+ if compact_handoff == Some(true) {+ assert_eq!(sent[0].len(), 5);+ assert_eq!(&sent[0][..4], previous.as_slice());+ assert_eq!(sent[1].len(), 7);+ assert_eq!(sent[0], sent[1][..5]);+ let digest = sent[0][4].text_content("").unwrap_or_default();+ assert!(digest.starts_with("fix the build"));+ assert!(digest.contains("<condensed_history>"));+ assert!(digest.contains("the build passes"));+ assert!(digest.contains("Routing notice"));+ assert_eq!(sent[1][5], call("cloud-1"));+ assert_eq!(sent[1][6], result("cloud-1", "cloud output"));+ } else {+ let mut original = previous.clone();+ original.extend([+ Message::text(Role::User, "fix the build"),+ call("c1"),+ result("c1", "ok"),+ ]);+ assert_eq!(&sent[0][..7], original.as_slice());+ assert_eq!(sent[0].len(), 8);+ assert!(+ sent[0][7]+ .text_content("")+ .unwrap_or_default()+ .contains("Routing notice")+ );+ let mut follow_up = original;+ follow_up.extend([call("cloud-1"), result("cloud-1", "cloud output")]);+ assert_eq!(sent[1], follow_up);+ }+ }+ Ok(())+}+#[tokio::test]async fn an_escalated_agentic_run_hands_off_once_with_the_unverified_claim() -> Result<()> {let route = route(|config| config.agentic_handoff = true)?;diff --git a/crates/libsy/src/algorithms/vgr/tests.rs b/crates/libsy/src/algorithms/vgr/tests.rsindex 6760420df..e42f90a90 100644--- a/crates/libsy/src/algorithms/vgr/tests.rs+++ b/crates/libsy/src/algorithms/vgr/tests.rs@@ -44,6 +44,43 @@ fn branch(request: &Request, attempt: &str, task_type: Option<TaskType>) -> Branderive_capabilities(request, attempt, task_type).branch}+#[test]+fn tool_trajectory_respects_the_current_request_initiator() {+ for (finished, initiator, expected) in [+ (true, Some("agent"), true),+ (true, Some("user"), false),+ (true, None, false),+ (false, Some("agent"), true),+ (false, Some("user"), false),+ (false, None, true),+ ] {+ let mut messages = vec![+ Message::text(Role::User, "fix the build"),+ call("c1"),+ result("c1", "ok"),+ ];+ if finished {+ messages.push(Message::text(Role::Assistant, "done"));+ }+ messages.push(Message::text(Role::User, "verify the change"));+ let mut request = request(messages);+ if let Some(initiator) = initiator {+ let mut headers = http::HeaderMap::new();+ headers.insert("x-initiator", initiator.parse().expect("header"));+ request.metadata = Some(switchyard_protocol::Metadata {+ http_headers: Some(headers),+ ..Default::default()+ });+ }+ assert_eq!(+ super::text::turn_has_tool_trajectory(&request),+ expected,+ "finished={finished}, initiator={initiator:?}"+ );+ }+ assert!(!super::text::turn_has_tool_trajectory(&Request::default()));+}+#[test]// Router typing selects a branch; tool history takes precedence over coding.fn router_typing_selects_the_verification_regime() {diff --git a/crates/libsy/src/algorithms/vgr/text.rs b/crates/libsy/src/algorithms/vgr/text.rsindex b011f058f..16dfb8cfc 100644--- a/crates/libsy/src/algorithms/vgr/text.rs+++ b/crates/libsy/src/algorithms/vgr/text.rs@@ -11,7 +11,7 @@ use regex::Regex;use serde_json::Value;use switchyard_protocol::{ContentBlock, InstructionBlock, Message, Request, Role, ToolResult};-use crate::algorithms::util::affinity::is_user_turn;+use crate::algorithms::util::affinity::{initiated_by_user, is_user_turn};use crate::algorithms::util::tool_signals::classify_text;/// One flattened turn: instructions first, then messages.@@ -144,14 +144,25 @@ pub(super) fn starts_user_turn(messages: &[Message], index: usize) -> bool {.any(|block| matches!(block, ContentBlock::ToolCall(_)))}-/// Whether the current user turn contains an executed or proposed tool step.-pub(super) fn turn_has_tool_trajectory(request: &Request) -> bool {+/// The current turn's start, using the initiator header for the newest message.+pub(super) fn turn_start(request: &Request) -> usize {let messages = &request.llm_request.messages;- let start = (0..messages.len())+ let end = match initiated_by_user(request) {+ Some(true) => return messages.len().saturating_sub(1),+ Some(false) => messages.len().saturating_sub(1),+ None => messages.len(),+ };+ (0..end).rev().find(|&index| starts_user_turn(messages, index))- .unwrap_or(0);- messages[start..].iter().any(is_tool_step)+ .unwrap_or(0)+}++/// Whether the current user turn contains an executed or proposed tool step.+pub(super) fn turn_has_tool_trajectory(request: &Request) -> bool {+ request.llm_request.messages[turn_start(request)..]+ .iter()+ .any(is_tool_step)}fn is_tool_step(message: &Message) -> bool {diff --git a/crates/switchyard-runner/src/algorithm.rs b/crates/switchyard-runner/src/algorithm.rsindex 11220076e..a607081aa 100644--- a/crates/switchyard-runner/src/algorithm.rs+++ b/crates/switchyard-runner/src/algorithm.rs@@ -549,6 +549,9 @@ pub struct VgrRouteConfig {/// Tells the capable tier it inherits unverified tool-using work.#[serde(default)]pub agentic_handoff: bool,+ /// Condenses local history at handoff. Requires `agentic_handoff`.+ #[serde(default)]+ pub compact_handoff: bool,/// Wall-clock budget for the local tier within one user turn.#[serde(default)]pub local_turn_budget_seconds: Option<f64>,@@ -1815,6 +1818,7 @@ fn build_vgr(runtime.task_typing = config.task_typing;runtime.confirmed_recovery_min_clean_tail = config.confirmed_recovery_min_clean_tail;runtime.agentic_handoff = config.agentic_handoff;+ runtime.compact_handoff = config.compact_handoff;runtime.latch_escalation = config.latch_escalation;runtime.local_turn_budget = config.local_turn_budget_secondsdiff --git a/crates/switchyard-runner/src/config.rs b/crates/switchyard-runner/src/config.rsindex 096af0136..5d7b2c8fc 100644--- a/crates/switchyard-runner/src/config.rs+++ b/crates/switchyard-runner/src/config.rs@@ -1165,13 +1165,33 @@ task_typing = false)}+ #[test]+ fn vgr_compaction_is_opt_in_from_toml() -> RunnerResult<()> {+ for setting in ["", "compact_handoff = false", "compact_handoff = true"] {+ let source = format!("{}\nagentic_handoff = true\n{setting}\n", vgr_config());+ let parsed: DeploymentConfig = toml::from_str(&source).expect("valid VGR config");+ let AlgorithmSpec::Vgr { config } = &parsed.routes["vgr"].algorithm else {+ panic!("expected VGR route");+ };+ assert_eq!(config.compact_handoff, setting.ends_with("= true"));+ assert!(runner_from_toml(&source)?.route("switchyard/vgr").is_some());+ }+ Ok(())+ }+#[test]fn vgr_route_builds_and_rejects_unsafe_configuration() -> RunnerResult<()> {let runner = runner_from_toml(&vgr_config())?;assert!(runner.route("switchyard/vgr").is_some());let compact = format!("{}\ncompact_handoff = true\n", vgr_config());- assert!(error_message(&compact).contains("unknown field `compact_handoff`"));+ assert!(error_message(&compact).contains("compact_handoff requires agentic_handoff"));+ let compact = format!("{compact}\nagentic_handoff = true\n");+ assert!(+ runner_from_toml(&compact)?+ .route("switchyard/vgr")+ .is_some()+ );let identical_ids =vgr_config().replace("local_target = \"weak\"", "local_target = \"strong\"");diff --git a/docs/routing_algorithms/vgr_routing.md b/docs/routing_algorithms/vgr_routing.mdindex f8f7953f7..e9e72de54 100644--- a/docs/routing_algorithms/vgr_routing.md+++ b/docs/routing_algorithms/vgr_routing.md@@ -72,6 +72,26 @@ new local work has been served since the previous handoff. The handoff preservesthe conversation's existing messages and content. `local_turn_budget_seconds`escalates a user turn once the local tier has spent that much wall-clock time on it.+Optional experimental compaction condenses local history into a cached digest+when handing off. Enable it on the VGR route:++```toml+[routes.assistant]+agentic_handoff = true+compact_handoff = true+```++`compact_handoff` defaults to `false` and requires `agentic_handoff = true`.+Omitting it or setting it to `false` preserves the full history. Later requests+reuse a digest only when their original history prefix matches; a new user turn+clears it. An `x-initiator: agent` message keeps the digest, while+`x-initiator: user` clears it.++The digest is lossy: user updates and tool output are clipped. Non-text tool+results become `[non-text content]`; non-text user updates, including images,+are omitted without a marker. Cache savings and task-success effects have not+been measured in a matched evaluation.+The notice covers the current request and asks the cloud tier to preserve earlierwork, inspect its effects, and repeat or undo steps only when needed.