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
fix(desktop): ignore routing outcomes in usage logs (#1227)
Rainaxyzc2b155e7750d
diff --git a/experimental/switchyard-desktop/Cargo.lock b/experimental/switchyard-desktop/Cargo.lockindex 7ab21286f..471370925 100644--- a/experimental/switchyard-desktop/Cargo.lock+++ b/experimental/switchyard-desktop/Cargo.lock@@ -4674,6 +4674,7 @@ dependencies = [name = "switchyard-runner"version = "0.3.0"dependencies = [+ "parking_lot","reqwest","serde","serde_json",@@ -4693,6 +4694,7 @@ dependencies = ["async-stream","base64 0.22.1","futures",+ "http","serde","serde_json","switchyard-protocol",diff --git a/experimental/switchyard-desktop/src/history.rs b/experimental/switchyard-desktop/src/history.rsindex c498e4e0b..eae4afcb9 100644--- a/experimental/switchyard-desktop/src/history.rs+++ b/experimental/switchyard-desktop/src/history.rs@@ -38,6 +38,27 @@ pub struct Entry {pub cost: Option<switchyard_costs::RecordedCost>,}+/// Outcome-only records describe routing, not additional model calls or usage.+pub(crate) fn parse_entry(line: &[u8]) -> serde_json::Result<Option<Entry>> {+ match serde_json::from_slice(line) {+ Ok(entry) => Ok(Some(entry)),+ Err(error) => {+ let is_outcome =+ serde_json::from_slice::<serde_json::Value>(line).is_ok_and(|record| {+ record.as_object().is_some_and(|record| {+ record+ .get("outcome")+ .is_some_and(serde_json::Value::is_object)+ && record+ .keys()+ .all(|key| matches!(key.as_str(), "session_id" | "outcome"))+ })+ });+ if is_outcome { Ok(None) } else { Err(error) }+ }+ }+}+#[derive(Default)]pub struct History {pub entries: Vec<Entry>,@@ -100,8 +121,8 @@ fn parse(bytes: &[u8], truncated: bool) -> History {if !line.ends_with(b"\n") {break;}- match serde_json::from_slice::<Entry>(line) {- Ok(entry)+ match parse_entry(line) {+ Ok(Some(entry))if !entry.model.trim().is_empty()&& chrono::DateTime::parse_from_rfc3339(&entry.ts).is_ok() =>{@@ -111,6 +132,7 @@ fn parse(bytes: &[u8], truncated: bool) -> History {}entries.push_back(entry);}+ Ok(None) => {}_ => result.skipped += 1,}}@@ -136,6 +158,30 @@ impl History {mod tests {use super::{MAX_BYTES, MAX_RECORDS, load};+ #[test]+ fn ignores_outcomes_without_counting_them_as_unreadable_calls() {+ let directory = tempfile::tempdir().expect("directory");+ let path = directory.path().join("routing.jsonl");+ let outcome = r#"{"session_id":"s","outcome":{"ts":"2026-10-08T12:00:00Z","route_id":"smart","judgments":[]}}"#;+ let first = r#"{"ts":"2026-10-08T12:00:00Z","model":"luna"}"#;+ let second = r#"{"ts":"2026-10-08T12:00:01Z","model":"astra"}"#;+ let invalid_usage = r#"{"outcome":{},"prompt_tokens":100}"#;+ let invalid_cost = r#"{"outcome":{},"cost":{}}"#;+ std::fs::write(+ &path,+ format!(+ "{outcome}\n{first}\n{outcome}\nnot json\n{invalid_usage}\n{second}\n{invalid_cost}\n{outcome}\n"+ ),+ )+ .expect("write log");+ let history = load(&path).expect("history");+ assert_eq!(history.entries.len(), 2);+ assert_eq!(history.entries[0].model, "luna");+ assert_eq!(history.entries[1].model, "astra");+ assert_eq!(history.skipped, 3);+ assert!(!history.limited);+ }+#[test]fn retains_recent_records_and_complete_byte_limited_records() {let directory = tempfile::tempdir().expect("directory");diff --git a/experimental/switchyard-desktop/src/rollup.rs b/experimental/switchyard-desktop/src/rollup.rsindex 2a87e0067..3f0b2ec33 100644--- a/experimental/switchyard-desktop/src/rollup.rs+++ b/experimental/switchyard-desktop/src/rollup.rs@@ -8,7 +8,7 @@ use std::fs::File;use std::io::{BufRead, BufReader, Seek, SeekFrom};use std::path::Path;-use crate::history::Entry;+use crate::history::{Entry, parse_entry};use chrono::{DateTime, Local, NaiveDate};use serde::{Deserialize, Serialize};use switchyard_costs::CostTotals;@@ -229,8 +229,11 @@ impl Reader {format!("invalid routing record at byte {}: {message}", self.offset),)};- let record: Entry =- serde_json::from_slice(&line).map_err(|error| invalid(error.to_string()))?;+ let Some(record) = parse_entry(&line).map_err(|error| invalid(error.to_string()))?+ else {+ self.offset += bytes as u64;+ continue;+ };if record.model.trim().is_empty() {return Err(invalid("model must not be empty".into()));}@@ -312,12 +315,13 @@ mod tests {}#[test]- fn reads_appends_once_and_retries_an_unfinished_line() {+ fn reads_calls_among_outcomes_and_retries_an_unfinished_line() {use std::io::Write;+ let outcome = r#"{"session_id":"s","outcome":{"ts":"2026-09-28T10:00:00Z","route_id":"smart","judgments":[]}}"#;let first = line("2026-09-28T10:00:00.000", "luna", "", 100, 10);let second = line("2026-09-28T10:00:01.000", "terra", "classifier", 30, 1);- let (_dir, path) = log(&format!("{first}\n"));+ let (_dir, path) = log(&format!("{outcome}\n{first}\n{outcome}\n"));let today = date("2026-09-28");let mut reader = Reader::default();assert_eq!(reader.read(&path, today).expect("read").today.tokens(), 110);@@ -342,10 +346,34 @@ mod tests {110);writeln!(file, "{}", &second[split..]).expect("finish append");+ writeln!(file, "{outcome}").expect("append outcome");let usage = reader.read(&path, today).expect("read append");assert_eq!(usage.today.tokens(), 141);assert_eq!(usage.today.requests(), 1);assert_eq!(usage.today.classifier["terra"].requests, 1);+ assert_eq!(reader.read(&path, today).expect("reread").all, usage.all);+ }++ #[test]+ fn rejects_invalid_calls_even_when_they_contain_an_outcome() {+ for contents in [+ "not json\n",+ "{\"outcome\":null}\n",+ "{\"outcome\":{},\"prompt_tokens\":100}\n",+ "{\"outcome\":{},\"cost\":{}}\n",+ "{\"outcome\":{},\"ts\":\"2026-09-28T10:00:00Z\",\"model\":42}\n",+ ] {+ let (_dir, path) = log(contents);+ let error = Reader::default()+ .read(&path, date("2026-09-28"))+ .expect_err("invalid call");+ assert_eq!(error.kind(), std::io::ErrorKind::InvalidData);+ assert!(+ error+ .to_string()+ .contains("invalid routing record at byte 0")+ );+ }}#[test]