Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions crates/libsy/src/algorithms/advisor_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,34 @@ impl AdvisorGate {
.consult(driver, &request, review_tail.as_deref(), trigger_label)
.await
{
Ok(ConsultOutcome::Approve) => Ok(RoutingOutcome::answered(
self.executor.clone(),
request,
turn.into_response(),
)),
Ok(ConsultOutcome::Redo { plan }) => Ok(self.redo(request, turn, &plan)),
Ok(ConsultOutcome::Failed) => {
Ok(ConsultOutcome::Approve) => {
driver.set_evidence(serde_json::json!({
"source": "advisor",
"verdict": "approve",
"trigger": trigger_label,
}));
Ok(RoutingOutcome::answered(
self.executor.clone(),
request,
turn.into_response(),
))
}
Ok(ConsultOutcome::Redo { plan }) => {
driver.set_evidence(serde_json::json!({
"source": "advisor",
"verdict": "redo",
"trigger": trigger_label,
}));
Ok(self.redo(request, turn, &plan))
}
Ok(ConsultOutcome::Failed { reason }) => {
self.budget.refund_failure(scope);
driver.set_evidence(serde_json::json!({
"source": "advisor",
"verdict": "fail_open",
"trigger": trigger_label,
"reason_code": reason,
}));
Ok(RoutingOutcome::answered(
self.executor.clone(),
request,
Expand Down Expand Up @@ -361,9 +381,8 @@ impl AdvisorGate {
let agg = match reply {
Ok(agg) => agg,
Err(error) => {
record_consult_failure(crate::algorithms::util::llm_judge::libsy_error_reason(
&error,
));
let reason = crate::algorithms::util::llm_judge::libsy_error_reason(&error);
record_consult_failure(reason);
if !self.config.fail_open {
// Surface as an algorithm failure (5xx), never as the
// advisor's own client error: a typed ContextWindowExceeded
Expand All @@ -386,7 +405,7 @@ impl AdvisorGate {
reply_head: None,
usage: None,
});
return Ok(ConsultOutcome::Failed);
return Ok(ConsultOutcome::Failed { reason });
}
};
let reply_text = advisor_reply_text(&agg);
Expand Down Expand Up @@ -426,7 +445,9 @@ impl AdvisorGate {
reply_head: Some(reply_head),
usage: Some(&agg.usage),
});
Ok(ConsultOutcome::Failed)
Ok(ConsultOutcome::Failed {
reason: "parse_error",
})
}
}
}
Expand Down Expand Up @@ -485,7 +506,7 @@ impl Algorithm for AdvisorGate {
enum ConsultOutcome {
Approve,
Redo { plan: String },
Failed,
Failed { reason: &'static str },
}

fn algorithm_error(message: impl Into<String>) -> LibsyError {
Expand Down
3 changes: 3 additions & 0 deletions crates/libsy/src/algorithms/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ impl Processor<State> for TierSetter {
if let Some(tier) = identity.and_then(|identity| self.tiers.lock().get(&identity).copied())
{
set_fall_open(state, tier);
if let Some(driver) = driver {
driver.set_evidence_if_empty(serde_json::json!({"source": "retained"}));
}
}
Ok(())
}
Expand Down
27 changes: 26 additions & 1 deletion crates/libsy/src/algorithms/escalation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ impl Classifier<State> for EscalationClassifier {

// A confirmed session stays capable without a judge call.
if streak(state) >= self.confirmations {
driver.set_evidence(serde_json::json!({
"source": "escalation",
"verdict": "latched",
}));
return Ok((decisive(&self.capable), None));
}

Expand All @@ -111,14 +115,24 @@ impl Classifier<State> for EscalationClassifier {
Err(LibsyError::ClientCall {
source: LlmClientError::ContextWindowExceeded { .. },
..
}) => return Ok((decisive(&self.capable), None)),
}) => {
driver.set_evidence(serde_json::json!({
"source": "fallback",
"reason_code": "context_window",
}));
return Ok((decisive(&self.capable), None));
}
Err(e) => return Err(e),
};
// The call resolves when its stream handle arrives; transport can still fail while
// buffering. Fall back only for that availability failure and keep other errors typed.
let agg = match efficient_response.llm_response.into_agg().await {
Ok(agg) => agg,
Err(LlmClientError::Transport { .. }) => {
driver.set_evidence(serde_json::json!({
"source": "fallback",
"reason_code": "transport",
}));
return Ok((decisive(&self.capable), None));
}
Err(source) => {
Expand Down Expand Up @@ -158,9 +172,20 @@ impl Classifier<State> for EscalationClassifier {

if escalate && pending >= self.confirmations {
// Streak confirmed: drop the efficient response, caller will serve capable.
driver.set_evidence(serde_json::json!({
"source": "escalation",
"verdict": "escalate",
}));
return Ok((decisive(&self.capable), None));
}

if escalate {
driver.set_evidence(serde_json::json!({
"source": "escalation",
"verdict": "pending",
}));
}

Ok((decisive(&self.efficient), Some(efficient_response)))
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/libsy/src/algorithms/fall_through.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ impl<S: Send> Classifier<S> for DefaultTarget {
&self,
_state: &mut S,
_request: &mut Request,
_driver: Option<&Driver>,
driver: Option<&Driver>,
) -> Result<(Classification, Option<Response>)> {
// Zero confidence: this is a fallback, not a judgement.
if let Some(driver) = driver {
driver.set_evidence_if_empty(serde_json::json!({"source": "fall_open"}));
}
Ok((
Classification::Scores(vec![Score {
target: self.target.clone(),
Expand Down
26 changes: 25 additions & 1 deletion crates/libsy/src/algorithms/llm_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,29 @@ impl JudgePolicy for TaskClassifierPolicy {
}
}

/// Maps valid verdicts to scores, invalid verdicts to a reason, and leaves absent verdicts alone.
fn capability_evidence(
policy: &TaskClassifierPolicy,
verdict: Option<&TaskClassifierVerdict>,
) -> Option<Value> {
let verdict = verdict?;
let Some(threshold) = verdict
.is_valid()
.then(|| policy.threshold(verdict))
.flatten()
else {
return Some(serde_json::json!({
"source": "fail_open",
"reason_code": "invalid_verdict",
}));
};
Some(serde_json::json!({
"source": "llm_classifier",
"score": verdict.p_solve,
"threshold": threshold,
}))
}

#[derive(Clone, Debug)]
/// Settings that control capability classifier prompting and routing.
pub struct TaskClassifierConfig {
Expand Down Expand Up @@ -605,7 +628,8 @@ impl LlmTaskClassifier {
capable_target.clone(),
&config,
),
),
)
.with_evidence(capability_evidence),
capable_target: capable_target.clone(),
});
let inner: Arc<dyn Classifier<State>> = classifier.clone();
Expand Down
14 changes: 13 additions & 1 deletion crates/libsy/src/algorithms/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ impl Classifier<State> for SourceStamp {
if let Some(winner) = classification.argmax(false)? {
record_decision_source(state, self.source);
record_routing_decision(self.source, &winner.target);
if let Some(driver) = driver {
let source = match self.source {
DecisionSource::LlmClassifier => "llm_classifier",
source => source.as_str(),
};
driver.set_evidence_if_empty(serde_json::json!({
"source": source,
}));
}
}
Ok((classification, served))
}
Expand All @@ -73,10 +82,13 @@ impl Classifier<State> for FallOpen {
&self,
state: &mut State,
_request: &mut Request,
_driver: Option<&Driver>,
driver: Option<&Driver>,
) -> Result<(Classification, Option<Response>)> {
let tier = fall_open_tier(state).unwrap_or(self.default_tier);
let target = self.targets.name(tier).clone();
if let Some(driver) = driver {
driver.set_evidence_if_empty(serde_json::json!({"source": "fall_open"}));
}
Ok((
Classification::Scores(vec![Score {
target,
Expand Down
7 changes: 6 additions & 1 deletion crates/libsy/src/algorithms/util/affinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ where
&self,
_state: &mut S,
request: &mut Request,
_driver: Option<&Driver>,
driver: Option<&Driver>,
) -> crate::Result<(Classification, Option<switchyard_protocol::Response>)> {
let Some(key) = self.affinity_key(request) else {
return Ok((Classification::Scores(Vec::new()), None));
Expand All @@ -243,6 +243,11 @@ where
return Ok((Classification::Scores(Vec::new()), None));
}
let assigned = self.assignments.lock().get(&key).cloned();
if assigned.is_some()
&& let Some(driver) = driver
{
driver.set_evidence(serde_json::json!({"source": "retained"}));
}
Ok((
Classification::Scores(match assigned {
Some(target) => vec![Score {
Expand Down
17 changes: 16 additions & 1 deletion crates/libsy/src/algorithms/util/escalation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! lives with the assembled algorithm in [`crate::algorithms::escalation`].

use serde::Deserialize;
use serde_json::Value;
use switchyard_protocol::{ContentBlock, Message, ModelId, Role};

use super::classifier_contract::{ClassifierContract, ClassifierContractConfig};
Expand Down Expand Up @@ -139,6 +140,19 @@ impl JudgePolicy for EscalationPolicy {
}
}

/// Maps present verdicts to stable `escalate` or `continue` values; absent verdicts add nothing.
fn escalation_evidence(
_policy: &EscalationPolicy,
verdict: Option<&EscalationVerdict>,
) -> Option<Value> {
verdict.map(|verdict| {
serde_json::json!({
"source": "escalation",
"verdict": if verdict.escalate { "escalate" } else { "continue" },
})
})
}

/// Builds the trajectory judge over `judge_target`, scoring `capable` when it escalates.
///
/// Loads the packaged prompt and schema, so an unusable asset or an unusable `config` value
Expand All @@ -163,7 +177,8 @@ pub(crate) fn build_judge(
),
judge_target,
EscalationPolicy { capable, efficient },
))
)
.with_evidence(escalation_evidence))
}

/// The 1-indexed model invocation the transcript ends on: one per assistant reply.
Expand Down
47 changes: 35 additions & 12 deletions crates/libsy/src/algorithms/util/llm_judge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,18 @@ pub trait JudgePolicy: Send + Sync {
fn to_classification(&self, verdict: Option<&Self::Verdict>) -> Classification;
}

type EvidenceFn<V, P> = fn(&P, Option<&V>) -> Option<Value>;

/// A classifier that calls one judge target and routes through its verdict policy.
pub struct JudgeClassifier<J, P> {
pub struct JudgeClassifier<J, P>
where
J: Judge,
P: JudgePolicy<Verdict = J::Verdict>,
{
judge: J,
target: ModelId,
policy: P,
evidence: Option<EvidenceFn<J::Verdict, P>>,
}

impl<J, P> JudgeClassifier<J, P>
Expand All @@ -220,6 +227,24 @@ where
judge,
target,
policy,
evidence: None,
}
}

/// Enables bounded evidence for built-in judges without widening the public policy trait.
pub(crate) fn with_evidence(mut self, evidence: EvidenceFn<J::Verdict, P>) -> Self {
self.evidence = Some(evidence);
self
}

/// Replaces run evidence only for judges that opted into structured evidence.
fn report_fail_open(&self, driver: &Driver, error: String, reason: &'static str) {
report_fail_open(self.target.as_str(), error, reason);
if self.evidence.is_some() {
driver.set_evidence(serde_json::json!({
"source": "fail_open",
"reason_code": reason,
}));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}

Expand All @@ -246,29 +271,21 @@ where
)
.await
.inspect_err(|error| {
report_fail_open(
judge_model,
safe_error_summary(error),
libsy_error_reason(error),
)
self.report_fail_open(driver, safe_error_summary(error), libsy_error_reason(error));
})
.ok()?;
let aggregate = response
.llm_response
.into_agg()
.await
.inspect_err(|error| {
report_fail_open(
judge_model,
safe_client_error(error),
client_error_reason(error),
)
self.report_fail_open(driver, safe_client_error(error), client_error_reason(error));
})
.ok()?;
self.judge
.parse(&aggregate)
.inspect_err(|error| {
report_fail_open(judge_model, safe_error_summary(error), "parse_error")
self.report_fail_open(driver, safe_error_summary(error), "parse_error");
})
.ok()
}
Expand Down Expand Up @@ -333,6 +350,12 @@ where
});
};
let verdict = self.verdict(state, request, driver).await;
if let Some(evidence) = self
.evidence
.and_then(|evidence| evidence(&self.policy, verdict.as_ref()))
{
driver.set_evidence(evidence);
}
// A judge consultation is a side call, never the turn's answer.
Ok((self.policy.to_classification(verdict.as_ref()), None))
}
Expand Down
Loading
Loading