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(advisor): re-arm the stall checkpoint after a refunded review (#691)
eric-liu-nvidia2602a35e1384
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 88cd89a7b..3d5be1861 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -121,6 +121,11 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).### Fixed+- **Advisor stall checkpoint re-arms after a refunded review** — a+ stall-triggered consult that failed open or returned an unparseable verdict+ refunded the review budget but left the conversation's stall latch set, so+ every later eligible turn silently bypassed the advisor. The latch now clears+ whenever the reserved review is refunded or the budget is already spent.- **Encrypted-only reasoning items open no summary part** — the Responsesstream encoder opened a `reasoning_summary_part` for every reasoning item andclosed it only when text had streamed, so an encrypted-only item left a partdiff --git a/crates/libsy/src/algorithms/advisor_gate.rs b/crates/libsy/src/algorithms/advisor_gate.rsindex da4062ad0..4afd732a3 100644--- a/crates/libsy/src/algorithms/advisor_gate.rs+++ b/crates/libsy/src/algorithms/advisor_gate.rs@@ -249,11 +249,14 @@ impl AdvisorGate {let decision = self.trigger.classify(&signals);// The stall checkpoint fires once per conversation regardless of the// turn's shape. Only a stall with no simultaneous trigger latches- // (atomically — one winner per conversation), so a refunded review- // leaves the checkpoint re-armed.+ // (atomically — one winner per conversation). The latch is provisional+ // until a review completes: a refunded consult or a spent budget+ // re-arms it so a later eligible turn is reviewed instead of silently+ // passing through.+ let stall_key = stall_key(&request);let stall = decision.fired.is_none()&& decision.stalled- && self.budget.try_mark_stall_fired(stall_key(&request));+ && self.budget.try_mark_stall_fired(stall_key);if decision.fired.is_none() && !stall {return Ok(RoutingOutcome::answered(served_executor.clone(),@@ -262,6 +265,9 @@ impl AdvisorGate {));}if !self.budget.try_reserve(scope) {+ if stall {+ self.budget.clear_stall_fired(stall_key);+ }return Ok(RoutingOutcome::answered(served_executor.clone(),request,@@ -306,6 +312,9 @@ impl AdvisorGate {}Ok(ConsultOutcome::Failed { reason }) => {self.budget.refund_failure(scope);+ if stall {+ self.budget.clear_stall_fired(stall_key);+ }driver.set_evidence(serde_json::json!({"source": "advisor","verdict": "fail_open",@@ -320,6 +329,9 @@ impl AdvisorGate {}Err(error) => {self.budget.refund_failure(scope);+ if stall {+ self.budget.clear_stall_fired(stall_key);+ }Err(error)}}diff --git a/crates/libsy/src/algorithms/advisor_gate/budget.rs b/crates/libsy/src/algorithms/advisor_gate/budget.rsindex 6892f3548..39e29b219 100644--- a/crates/libsy/src/algorithms/advisor_gate/budget.rs+++ b/crates/libsy/src/algorithms/advisor_gate/budget.rs@@ -132,6 +132,12 @@ impl ReviewBudget {}state.stall_fired.insert(key)}++ /// Re-arms the stall checkpoint for a conversation key after a review+ /// that was reserved but never completed (the budget was refunded).+ pub(super) fn clear_stall_fired(&self, key: u64) {+ self.state.lock().stall_fired.remove(&key);+ }}/// Resolves the review budget scope: the benchmark harness header, then the@@ -217,4 +223,13 @@ mod tests {assert!(!budget.try_mark_stall_fired(7));assert!(budget.try_mark_stall_fired(8));}++ #[test]+ fn clearing_the_stall_latch_readmits_the_key() {+ let budget = ReviewBudget::new(1);+ assert!(budget.try_mark_stall_fired(7));+ budget.clear_stall_fired(7);+ assert!(budget.try_mark_stall_fired(7));+ assert!(!budget.try_mark_stall_fired(7));+ }}diff --git a/crates/libsy/src/algorithms/advisor_gate/tests.rs b/crates/libsy/src/algorithms/advisor_gate/tests.rsindex 8a5075756..a4f771fcb 100644--- a/crates/libsy/src/algorithms/advisor_gate/tests.rs+++ b/crates/libsy/src/algorithms/advisor_gate/tests.rs@@ -949,6 +949,109 @@ async fn stall_checkpoint_reviews_mid_task_once() {assert_eq!(script.advisor_consults(), 1);}+/// Serve that answers the executor with a tool-call turn — keeping the+/// terminal trigger quiet so only the stall checkpoint can fire — and the+/// advisor with `verdict` (`Err` = advisor down), recording every call.+fn stalled_serve(+ script: &Script,+ verdict: std::result::Result<&'static str, ()>,+) -> impl Fn(+ ModelId,+ Request,+) -> futures::future::BoxFuture<'static, std::result::Result<Response, LlmClientError>>++ Send++ Sync++ 'static {+ let calls = Arc::clone(&script.calls);+ move |model: ModelId, request: Request| {+ let calls = Arc::clone(&calls);+ Box::pin(async move {+ let model = model.to_string();+ calls.lock().push((model.clone(), request));+ if model != ADVISOR {+ Ok(tool_call_turn())+ } else if let Ok(verdict) = verdict {+ Ok(reply(verdict))+ } else {+ Err(LlmClientError::General("advisor down".to_string()))+ }+ })+ }+}++#[tokio::test]+async fn refunded_stall_review_rearms_the_checkpoint() {+ // A stall review that is refunded — unparseable verdict, fail-open error,+ // or fail-closed error — must re-arm the conversation's stall latch too;+ // otherwise every later eligible turn silently bypasses the advisor.+ let config = |fail_open| AdvisorGateConfig {+ gate_stall_turns: 1,+ max_reviews: 2,+ fail_open,+ ..AdvisorGateConfig::default()+ };+ let grinding = || {+ request(vec![+ Message::text(Role::User, "build X"),+ Message::text(Role::Assistant, "step 1"),+ ])+ };++ // Fail-open gate: an unparseable verdict, then an advisor error.+ let script = Script::new();+ let open_gate = gate(config(true));+ test_drive(+ Arc::clone(&open_gate),+ grinding(),+ stalled_serve(&script, Ok("MAYBE")),+ )+ .await+ .expect("routes");+ assert_eq!(script.advisor_consults(), 1);+ test_drive(+ Arc::clone(&open_gate),+ grinding(),+ stalled_serve(&script, Err(())),+ )+ .await+ .expect("fail-open run");+ assert_eq!(script.advisor_consults(), 2);+ // Each refund re-armed the checkpoint: the next eligible turn is reviewed.+ test_drive(+ Arc::clone(&open_gate),+ grinding(),+ stalled_serve(&script, Ok("APPROVE")),+ )+ .await+ .expect("routes");+ assert_eq!(script.advisor_consults(), 3);+ // A completed review latches the checkpoint as before.+ test_drive(open_gate, grinding(), stalled_serve(&script, Ok("APPROVE")))+ .await+ .expect("routes");+ assert_eq!(script.advisor_consults(), 3);++ // Fail-closed gate: the error propagates, and the checkpoint re-arms.+ let script = Script::new();+ let closed_gate = gate(config(false));+ let result = test_drive(+ Arc::clone(&closed_gate),+ grinding(),+ stalled_serve(&script, Err(())),+ )+ .await;+ assert!(result.is_err(), "fail-closed surfaces the advisor error");+ assert_eq!(script.advisor_consults(), 1);+ test_drive(+ closed_gate,+ grinding(),+ stalled_serve(&script, Ok("APPROVE")),+ )+ .await+ .expect("routes");+ assert_eq!(script.advisor_consults(), 2);+}+#[tokio::test]async fn simultaneous_trigger_does_not_latch_stall() {let script = Script::new();