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 trusted capability derivation (#1016)
GDRS34894b5b85c2
diff --git a/Cargo.lock b/Cargo.lockindex c1664fc83..bb56d7966 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3410,6 +3410,7 @@ dependencies = ["http","serde","serde_json",+ "strum_macros","thiserror 2.0.18",]diff --git a/crates/libsy/src/algorithms.rs b/crates/libsy/src/algorithms.rsindex cd2d36b50..00fcbea92 100644--- a/crates/libsy/src/algorithms.rs+++ b/crates/libsy/src/algorithms.rs@@ -17,6 +17,7 @@ pub mod plan_execute;pub mod rand;pub mod stage;pub mod subagent;+pub mod vgr;pub mod util;diff --git a/crates/libsy/src/algorithms/vgr.rs b/crates/libsy/src/algorithms/vgr.rsnew file mode 100644index 000000000..a99e08eb1--- /dev/null+++ b/crates/libsy/src/algorithms/vgr.rs@@ -0,0 +1,107 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++//! Verification-gated routing: fail-closed capability derivation.+//!+//! Capabilities are never client-declared. They come from the router's own+//! typing of the request, the attempt it produced locally, and its own reading+//! of the tool results in the conversation. No client-reachable input selects+//! a weaker regime than the default one; unsupported content and missing+//! evidence select [`Branch::Unknown`], which never commits.++#![allow(dead_code)]++use switchyard_protocol::Request;++use self::text::ToolRecord;++mod render;+mod text;++#[cfg(test)]+mod tests;++/// The verification regime a request's capabilities license.+#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]+enum Branch {+ /// Code or test activity, with no sandboxed checker to appeal to.+ Coding,+ /// A single-turn request typed as answer-seeking.+ Answer,+ /// Conversational traffic.+ Chat,+ /// A tool-using session verified from its trajectory.+ Agentic,+ /// Anything else with an attempt to verify.+ DefaultVerified,+ /// Nothing to verify.+ #[default]+ Unknown,+}++/// A task type produced by the router's own typing call; `None` abstains.+#[derive(Clone, Copy, Debug, Eq, PartialEq)]+enum TaskType {+ Coding,+ Agentic,+ Answer,+ Chat,+}++/// The complete input the decision core sees.+#[derive(Default)]+struct Capabilities {+ branch: Branch,+ /// The judged view of the request and attempt.+ transcript: Option<String>,+ /// The router's own tool-result record; unused by the answer regime.+ tools: Option<ToolRecord>,+}++/// Derives capabilities from the router's own evidence, failing closed.+fn derive_capabilities(+ request: &Request,+ attempt: &str,+ task_type: Option<TaskType>,+) -> Capabilities {+ let (turns, unsupported) = text::turns(request);+ if unsupported || text::user_task_text(&turns).trim().is_empty() || attempt.trim().is_empty() {+ return Capabilities::default();+ }+ let caps = |branch, transcript| Capabilities {+ branch,+ transcript,+ tools: Some(ToolRecord::from_request(request)),+ };++ if task_type == Some(TaskType::Answer) && !text::has_assistant_turn(&turns) {+ return Capabilities {+ tools: None,+ ..caps(+ Branch::Answer,+ Some(render::render_session(&turns, attempt)),+ )+ };+ }+ // A tool trajectory is judged as agentic work unless typed conversational.+ if task_type == Some(TaskType::Agentic)+ || (text::has_tool_trajectory(request)+ && !matches!(task_type, Some(TaskType::Answer | TaskType::Chat)))+ {+ return caps(+ Branch::Agentic,+ Some(render::render_agentic_view(request, &turns, attempt)),+ );+ }+ if text::observed_hardening(attempt) || task_type == Some(TaskType::Coding) {+ return caps(Branch::Coding, None);+ }+ let transcript = Some(render::render_session(&turns, attempt));+ if text::observed_tool_activity(attempt) {+ caps(Branch::Agentic, transcript)+ } else if matches!(task_type, Some(TaskType::Chat | TaskType::Answer)) {+ caps(Branch::Chat, transcript)+ } else {+ caps(Branch::DefaultVerified, transcript)+ }+}diff --git a/crates/libsy/src/algorithms/vgr/render.rs b/crates/libsy/src/algorithms/vgr/render.rsnew file mode 100644index 000000000..560ae3de3--- /dev/null+++ b/crates/libsy/src/algorithms/vgr/render.rs@@ -0,0 +1,114 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++//! The judged views a verifier reads.+//!+//! Each section is redacted before its budget applies, and user requirements+//! render in full while trajectory and attempt evidence clip.++use switchyard_protocol::{ContentBlock, Request, Role};++use super::text::{Turn, clip_mid, content_text, redact, tool_result_failed};++const ASSISTANT_TURN_BUDGET: usize = 1200;+pub(super) const ATTEMPT_BUDGET: usize = 2200;+const AGENTIC_TRAJECTORY_BUDGET: usize = 12_000;+const AGENTIC_EVENT_BUDGET: usize = 1400;++/// Earlier turns first, then the latest user instruction and the attempt.+pub(super) fn render_session(turns: &[Turn], attempt: &str) -> String {+ let latest_user = turns+ .iter()+ .rposition(|turn| turn.role == Role::User && !turn.text.trim().is_empty());+ let mut lines = Vec::new();+ for (index, turn) in turns.iter().enumerate() {+ if turn.text.trim().is_empty() || Some(index) == latest_user {+ continue;+ }+ let mut text = redact(&turn.text);+ if matches!(turn.role, Role::Assistant | Role::Tool) {+ text = clip_mid(&text, ASSISTANT_TURN_BUDGET, 0.5);+ }+ lines.push(format!("[{}] {text}", turn.role));+ }+ if let Some(index) = latest_user {+ let text = redact(&turns[index].text);+ lines.push(format!("[user (latest)] {text}"));+ }+ let attempt = redact(attempt);+ let attempt = clip_mid(&attempt, ATTEMPT_BUDGET, 1.0 / 3.0);+ lines.push(format!("[assistant attempt] {attempt}"));+ lines.join("\n")+}++/// The user task and updates, the tool trajectory, and the attempt.+///+/// Framework instructions are omitted.+pub(super) fn render_agentic_view(request: &Request, turns: &[Turn], attempt: &str) -> String {+ let user_turns = turns+ .iter()+ .filter(|turn| turn.role == Role::User && !turn.text.trim().is_empty());+ let mut requirements = Vec::new();+ for (index, turn) in user_turns.enumerate() {+ let text = redact(&turn.text);+ requirements.push(match index {+ 0 => format!("[user task] {text}"),+ _ => format!("[user update {index}] {text}"),+ });+ }+ let requirements = requirements.join("\n");+ let trajectory = agentic_trajectory(request);+ let mut parts = vec![format!("USER TASK AND UPDATES:\n{requirements}")];+ if !trajectory.is_empty() {+ parts.push(format!(+ "TOOL TRAJECTORY:\n{}",+ clip_mid(&trajectory.join("\n"), AGENTIC_TRAJECTORY_BUDGET, 1.0 / 3.0)+ ));+ }+ let attempt = redact(attempt);+ let attempt = clip_mid(&attempt, ATTEMPT_BUDGET, 1.0 / 3.0);+ parts.push(format!("CURRENT ATTEMPT:\n{attempt}"));+ parts.join("\n\n")+}++/// Bounded assistant and tool events after the first user task.+fn agentic_trajectory(request: &Request) -> Vec<String> {+ let mut messages = request.llm_request.messages.iter();+ for message in messages.by_ref() {+ if message.role == Role::User && !content_text(&message.content).0.trim().is_empty() {+ break;+ }+ }+ let mut events = Vec::new();+ for message in messages {+ for block in &message.content {+ let event = match block {+ ContentBlock::Text { text } | ContentBlock::Refusal { text } => {+ match message.role {+ Role::Assistant => Some(("assistant", text.clone())),+ Role::Tool => Some(("tool result", text.clone())),+ _ => None,+ }+ }+ ContentBlock::ToolCall(call) => {+ Some(("tool call", format!("{}({})", call.name, call.arguments)))+ }+ ContentBlock::ToolResult(result) => Some((+ if tool_result_failed(result) {+ "tool error"+ } else {+ "tool result"+ },+ content_text(&result.content).0,+ )),+ _ => None,+ };+ if let Some((label, text)) = event {+ let text = redact(&text);+ let text = clip_mid(&text, AGENTIC_EVENT_BUDGET, 0.5);+ events.push(format!("[{label}] {text}"));+ }+ }+ }+ events+}diff --git a/crates/libsy/src/algorithms/vgr/tests.rs b/crates/libsy/src/algorithms/vgr/tests.rsnew file mode 100644index 000000000..82d73e5ed--- /dev/null+++ b/crates/libsy/src/algorithms/vgr/tests.rs@@ -0,0 +1,259 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++use switchyard_protocol::{+ ContentBlock, ImageSource, LlmRequest, Message, Request, Role, ToolCall, ToolResult,+};++use super::text::ToolRecord;+use super::{Branch, TaskType, derive_capabilities};++pub(super) fn request(messages: Vec<Message>) -> Request {+ Request {+ llm_request: LlmRequest {+ messages,+ ..LlmRequest::default()+ },+ ..Default::default()+ }+}++pub(super) fn call(id: &str) -> Message {+ Message {+ role: Role::Assistant,+ content: vec![ContentBlock::ToolCall(ToolCall {+ id: id.into(),+ name: "bash".into(),+ arguments: serde_json::json!({"command": "ls"}),+ })],+ }+}++pub(super) 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 branch(request: &Request, attempt: &str, task_type: Option<TaskType>) -> Branch {+ derive_capabilities(request, attempt, task_type).branch+}++#[test]+// Router typing selects a branch; tool history takes precedence over coding.+fn router_typing_selects_the_verification_regime() {+ let single = request(vec![Message::text(Role::User, "Help with this task")]);+ for (task_type, expected) in [+ (Some(TaskType::Coding), Branch::Coding),+ (Some(TaskType::Agentic), Branch::Agentic),+ (Some(TaskType::Answer), Branch::Answer),+ (Some(TaskType::Chat), Branch::Chat),+ (None, Branch::DefaultVerified),+ ] {+ assert_eq!(branch(&single, "done", task_type), expected);+ }++ // A tool trajectory outranks a coding type; only conversation types keep it off.+ let session = request(vec![+ Message::text(Role::User, "fix the build"),+ call("c1"),+ result("c1", "ok"),+ ]);+ assert_eq!(+ branch(&session, "done", Some(TaskType::Coding)),+ Branch::Agentic+ );+ assert_eq!(branch(&session, "done", Some(TaskType::Chat)), Branch::Chat);+ assert_eq!(branch(&single, "```\nx\n```", None), Branch::Coding);+}++#[test]+// Missing text and unsupported content cannot license a local answer.+fn missing_or_unrepresentable_evidence_fails_closed() {+ let task = request(vec![Message::text(Role::User, "task")]);+ assert_eq!(branch(&task, " ", None), Branch::Unknown);+ assert_eq!(branch(&request(Vec::new()), "done", None), Branch::Unknown);++ let image = request(vec![Message {+ role: Role::User,+ content: vec![ContentBlock::Image {+ source: ImageSource::Url {+ url: "https://example.test/a.png".into(),+ detail: None,+ },+ }],+ }]);+ assert_eq!(branch(&image, "done", None), Branch::Unknown);+}++#[test]+// Executor success takes precedence over inferred tool errors.+fn a_zero_exit_code_overrules_every_error_inference() {+ let record = ToolRecord::from_request(&request(vec![+ result(+ "a",+ r#"{"output": "cat: HEAD: No such file", "exit_code": 0, "error": null}"#,+ ),+ result(+ "b",+ r#"{"output": "boom", "exit_code": 127, "error": "spawn failed"}"#,+ ),+ result("c", r#"{"error": "record not found"}"#),+ result("d", "Traceback (most recent call last):\n ValueError"),+ result("e", "done"),+ ]));+ assert_eq!(+ record,+ ToolRecord {+ errors: 3,+ results: 5,+ clean_tail: 1,+ }+ );+}++#[test]+fn trajectory_labels_agree_with_tool_failure_counts() {+ for (output, is_error, failed) in [+ (r#"{"error":"spawn failed"}"#, None, true),+ (+ "Traceback (most recent call last):\n ValueError",+ None,+ true,+ ),+ ("done", Some(true), true),+ (r#"{"exit_code":0,"error":"expected failure"}"#, None, false),+ (r#"{"exit_code":0}"#, Some(true), false),+ ("done", None, false),+ ] {+ let session = request(vec![+ Message::text(Role::User, "check the task"),+ call("c1"),+ Message {+ role: Role::Tool,+ content: vec![ContentBlock::ToolResult(ToolResult {+ tool_call_id: "c1".into(),+ content: vec![ContentBlock::Text {+ text: output.into(),+ }],+ is_error,+ })],+ },+ ]);+ let caps = derive_capabilities(&session, "checked", Some(TaskType::Agentic));+ assert_eq!(caps.tools.unwrap().errors, i32::from(failed), "{output}");+ let view = caps.transcript.unwrap();+ let label = if failed { "tool error" } else { "tool result" };+ assert!(+ view.contains(&format!("[{label}] {output}")),+ "{output}: {view}"+ );+ }+}++#[test]+// Verifiers see user requirements and redacted tool history without framework instructions.+fn the_agentic_view_keeps_the_task_and_redacts_the_trajectory() {+ let session = request(vec![+ Message::text(Role::System, "framework boilerplate"),+ Message::text(Role::User, "rotate the key"),+ call("c1"),+ result("c1", "Authorization: Bearer abcdefghijklmnopqrstuvwxyz"),+ ]);+ let caps = derive_capabilities(&session, "rotated", None);+ let view = caps.transcript.unwrap_or_default();+ assert_eq!(caps.branch, Branch::Agentic);+ assert!(view.starts_with("USER TASK AND UPDATES:\n[user task] rotate the key"));+ assert!(view.contains("[tool call] bash"));+ assert!(view.contains("[REDACTED]") && !view.contains("abcdefghij"));+ assert!(!view.contains("boilerplate"));+ assert!(view.ends_with("CURRENT ATTEMPT:\nrotated"));+}++#[test]+fn redaction_covers_keys_tokens_and_email_and_is_idempotent() {+ for (input, expected) in [+ ("sk-abcdefghijklmnop", "[REDACTED]"),+ ("rk-abcdefghijklmnop", "[REDACTED]"),+ ("pk-abcdefghijklmnop", "[REDACTED]"),+ ("AKIA0123456789ABCDEF", "[REDACTED]"),+ (+ "Authorization: Bearer abcdefghijklmnop",+ "Authorization: [REDACTED]",+ ),+ ("BEARER abcdefghijklmnop", "[REDACTED]"),+ ("contact [email protected]", "contact [REDACTED]"),+ ("ordinary task text", "ordinary task text"),+ ("sk-abcdefghijklmno", "sk-abcdefghijklmno"),+ ("Bearer abcdefghijklmno", "Bearer abcdefghijklmno"),+ ] {+ let redacted = super::text::redact(input);+ assert_eq!(redacted, expected, "{input}");+ assert_eq!(super::text::redact(&redacted), expected, "{input}");+ }+}++#[test]+fn clipping_preserves_character_budgets_and_utf8_boundaries() {+ for (input, budget, fraction, expected) in [+ ("abcdef", 6, 0.5, "abcdef"),+ ("abcdef", 4, 0.5, "ab\n...[2 chars omitted]...\nef"),+ ("aé\u{1f642}中z", 4, 0.5, "aé\n...[1 chars omitted]...\n中z"),+ (+ "aé\u{1f642}中z",+ 3,+ 1.0 / 3.0,+ "a\n...[2 chars omitted]...\n中z",+ ),+ ("aé\u{1f642}中z", 2, 1.0, "aé\n...[3 chars omitted]...\n"),+ ("aé\u{1f642}中z", 1, 0.5, "a\n...[4 chars omitted]...\n"),+ ] {+ assert_eq!(super::text::clip_mid(input, budget, fraction), expected);+ }+}++#[test]+fn session_rendering_preserves_roles_and_moves_the_latest_user_turn() {+ let session = request(vec![+ Message::text(Role::System, "system rule"),+ Message::text(Role::Developer, "developer rule"),+ Message::text(Role::User, "first task"),+ Message::text(Role::Assistant, "first reply"),+ Message::text(Role::User, "latest task"),+ Message::text(Role::Tool, "tool output"),+ Message::text(Role::User, " "),+ ]);+ let (turns, _) = super::text::turns(&session);+ assert_eq!(+ super::render::render_session(&turns, "done"),+ "[system] system rule\n[developer] developer rule\n[user] first task\n[assistant] first reply\n[tool] tool output\n[user (latest)] latest task\n[assistant attempt] done"+ );+}++#[test]+fn agentic_rendering_preserves_long_user_updates_and_clips_redacted_attempts() {+ let task = "é".repeat(2500);+ let session = request(vec![+ Message::text(Role::System, "framework"),+ Message::text(Role::User, &task),+ Message::text(Role::Assistant, "previous reply"),+ Message::text(Role::User, " "),+ Message::text(Role::User, "update sk-abcdefghijklmnop"),+ ]);+ let (turns, _) = super::text::turns(&session);+ // A large secret must be redacted before clipping can expose fragments of it.+ let attempt = format!("Bearer {}", "x".repeat(25_000));+ let view = super::render::render_agentic_view(&session, &turns, &attempt);+ assert!(view.starts_with(&format!(+ "USER TASK AND UPDATES:\n[user task] {task}\n[user update 1] update [REDACTED]"+ )));+ assert!(view.contains("TOOL TRAJECTORY:\n[assistant] previous reply"));+ assert!(!view.contains("framework"));+ assert!(view.ends_with("CURRENT ATTEMPT:\n[REDACTED]"));+ assert!(!view.contains("chars omitted"));+}diff --git a/crates/libsy/src/algorithms/vgr/text.rs b/crates/libsy/src/algorithms/vgr/text.rsnew file mode 100644index 000000000..981b9649b--- /dev/null+++ b/crates/libsy/src/algorithms/vgr/text.rs@@ -0,0 +1,228 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++//! Pure text primitives shared by derivation and the judged views.+//!+//! Budgets and clipping count characters, not bytes.++use std::{borrow::Cow, sync::LazyLock};++use regex::Regex;+use serde_json::Value;+use switchyard_protocol::{ContentBlock, InstructionBlock, Message, Request, Role, ToolResult};++use crate::algorithms::util::tool_signals::classify_text;++/// One flattened turn: instructions first, then messages.+pub(super) struct Turn {+ pub(super) role: Role,+ pub(super) text: String,+}++// API keys, AWS access keys, bearer tokens, and email addresses.+// The 16-character key/token minimum avoids matching short ordinary identifiers.+static REDACT_PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {+ [+ r"\b(sk|rk|pk)-[A-Za-z0-9_\-]{16,}\b",+ r"\bAKIA[0-9A-Z]{16}\b",+ r"(?i)\bbearer\s+[A-Za-z0-9._\-]{16,}",+ r"\b[\w.+-]+@[\w-]+\.[\w.]+\b",+ ]+ .iter()+ .map(|pattern| Regex::new(pattern).expect("valid redaction pattern"))+ .collect()+});++// Heuristic code/test markers: fences, shell commands, diffs, tracebacks, and test counts.+static CODE_ACTIVITY: LazyLock<Option<Regex>> = LazyLock::new(|| {+ Regex::new(concat!(+ r"(?m)```|^\$ |^diff --git|^@@ |",+ r"\bTraceback \(most recent call last\)|\b\d+ (?:passed|failed)\b|",+ r"^(?:\$ |> )?(?:pytest|unittest|npm (?:test|run)|cargo (?:test|build)|",+ r"go test|make test)\b",+ ))+ .ok()+});++// Tool transcript labels and JSON tool-call fields.+static TOOL_ACTIVITY: LazyLock<Option<Regex>> = LazyLock::new(|| {+ Regex::new(r#"(?m)^\[tool\b|^\[[^\]\n]* calls tools?:|"tool_calls?"\s*:"#).ok()+});++/// Text of a content sequence, and whether it carried media or unknown blocks.+/// Tool and reasoning blocks are skipped; unsupported blocks make derivation fail closed.+pub(super) fn content_text(content: &[ContentBlock]) -> (String, bool) {+ let mut parts: Vec<&str> = Vec::new();+ let mut unsupported = false;+ for block in content {+ match block {+ ContentBlock::Text { text } | ContentBlock::Refusal { text } => parts.push(text),+ ContentBlock::ToolCall(_)+ | ContentBlock::ToolResult(_)+ | ContentBlock::Reasoning { .. } => {}+ _ => unsupported = true,+ }+ }+ (parts.join("\n"), unsupported)+}++pub(super) fn turns(request: &Request) -> (Vec<Turn>, bool) {+ let mut flattened = Vec::new();+ let mut unsupported = false;+ let mut push = |role: Role, content: &[ContentBlock]| {+ let (text, block_unsupported) = content_text(content);+ unsupported |= block_unsupported;+ flattened.push(Turn { role, text });+ };+ for InstructionBlock { role, content } in &request.llm_request.instructions {+ push(*role, content);+ }+ for Message { role, content } in &request.llm_request.messages {+ push(*role, content);+ }+ (flattened, unsupported)+}++/// The latest non-empty user turn.+pub(super) fn user_task_text(turns: &[Turn]) -> &str {+ turns+ .iter()+ .rev()+ .find(|turn| turn.role == Role::User && !turn.text.trim().is_empty())+ .map(|turn| turn.text.as_str())+ .unwrap_or_default()+}++pub(super) fn has_assistant_turn(turns: &[Turn]) -> bool {+ turns.iter().any(|turn| turn.role == Role::Assistant)+}++/// Whether the conversation contains an executed or proposed tool step.+pub(super) fn has_tool_trajectory(request: &Request) -> bool {+ request.llm_request.messages.iter().any(|message| {+ message.role == Role::Tool+ || message.content.iter().any(|block| {+ matches!(+ block,+ ContentBlock::ToolCall(_) | ContentBlock::ToolResult(_)+ )+ })+ })+}++/// Idempotent secret and PII scrub applied before text reaches a verifier.+pub(super) fn redact(text: &str) -> String {+ let mut out = text.to_string();+ for pattern in REDACT_PATTERNS.iter() {+ if let Cow::Owned(redacted) = pattern.replace_all(&out, "[REDACTED]") {+ out = redacted;+ }+ }+ out+}++pub(super) fn char_len(text: &str) -> usize {+ text.chars().count()+}++/// Head-and-tail clip to `budget` characters, keeping `head_frac` as head.+pub(super) fn clip_mid(text: &str, budget: usize, head_frac: f64) -> String {+ let len = char_len(text);+ if len <= budget {+ return text.to_string();+ }+ let head = ((budget as f64 * head_frac) as usize).max(1).min(budget);+ let tail = budget.saturating_sub(head);+ let omitted = len - budget;+ let head_end = text+ .char_indices()+ .nth(head)+ .map_or(text.len(), |(index, _)| index);+ let tail_start = if tail == 0 {+ text.len()+ } else {+ text.char_indices()+ .rev()+ .nth(tail - 1)+ .map_or(0, |(index, _)| index)+ };+ let head_text = &text[..head_end];+ let tail_text = &text[tail_start..];+ format!("{head_text}\n...[{omitted} chars omitted]...\n{tail_text}")+}++/// Detects heuristic code/test markers in the local attempt.+pub(super) fn observed_hardening(attempt: &str) -> bool {+ CODE_ACTIVITY+ .as_ref()+ .is_some_and(|pattern| pattern.is_match(attempt))+}++/// Detects tool transcript labels and JSON tool-call fields in the local attempt.+pub(super) fn observed_tool_activity(attempt: &str) -> bool {+ TOOL_ACTIVITY+ .as_ref()+ .is_some_and(|pattern| pattern.is_match(attempt))+}++/// The router's own summary of every tool result in the conversation.+#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]+pub(super) struct ToolRecord {+ pub(super) errors: i32,+ pub(super) results: i32,+ /// Consecutive clean results ending the conversation.+ pub(super) clean_tail: i32,+}++/// Uses executor status and inferred failures, with a successful exit taking precedence.+pub(super) fn tool_result_failed(result: &ToolResult) -> bool {+ let text = result+ .content+ .iter()+ .filter_map(|block| match block {+ ContentBlock::Text { text } | ContentBlock::Refusal { text } => Some(text),+ _ => None,+ })+ .map(String::as_str)+ .collect::<Vec<_>>()+ .join("\n");+ let body = serde_json::from_str::<Value>(&text).ok();+ let object = body.as_ref().and_then(Value::as_object);+ // The executor's own `exit_code: 0` overrules every inferred failure:+ // harnesses send `"error": null` on success, and negative checks print+ // error-shaped text.+ let exit_ok = object+ .and_then(|object| object.get("exit_code"))+ .and_then(Value::as_i64)+ == Some(0);+ (result.is_error == Some(true)+ || object.is_some_and(|object| object.contains_key("error"))+ || classify_text(&text).0 > 0.0)+ && !exit_ok+}++impl ToolRecord {+ pub(super) fn from_request(request: &Request) -> Self {+ let mut record = Self::default();+ let results = request+ .llm_request+ .messages+ .iter()+ .flat_map(|message| &message.content)+ .filter_map(|block| match block {+ ContentBlock::ToolResult(result) => Some(result),+ _ => None,+ });+ for result in results {+ let failed = tool_result_failed(result);+ record.results = record.results.saturating_add(1);+ record.errors = record.errors.saturating_add(i32::from(failed));+ record.clean_tail = if failed {+ 0+ } else {+ record.clean_tail.saturating_add(1)+ };+ }+ record+ }+}diff --git a/crates/protocol/Cargo.toml b/crates/protocol/Cargo.tomlindex 5db728c06..208815902 100644--- a/crates/protocol/Cargo.toml+++ b/crates/protocol/Cargo.toml@@ -21,4 +21,5 @@ futures.workspace = truehttp.workspace = trueserde.workspace = trueserde_json.workspace = true+strum_macros.workspace = truethiserror.workspace = truediff --git a/crates/protocol/src/llm.rs b/crates/protocol/src/llm.rsindex a284136b9..9429354e5 100644--- a/crates/protocol/src/llm.rs+++ b/crates/protocol/src/llm.rs@@ -16,8 +16,9 @@ use serde_json::{Map, Value};use crate::format::FormatId;/// Actor role normalized across provider APIs.-#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]+#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, strum_macros::Display)]#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]pub enum Role {/// System-level instructions.System,