1use std::{
2 collections::BTreeMap,
3 fmt::Write as _,
4 time::{Duration, SystemTime, UNIX_EPOCH},
5};
6
7use reqwest::StatusCode;
8use serde::{Deserialize, Serialize};
9use serde_json::{Value, json};
10use sha2::{Digest, Sha256};
11use thiserror::Error;
12use uuid::Uuid;
13
14use crate::{Config, DecisionSource, JevConfig, RouteDecision, RouteRecord};
15
16mod history;
17pub use history::{RoutingCandidate, RoutingHistory};
18
19const MAX_RESPONSE_BYTES: usize = 1024 * 1024;
21
22pub struct JevClient {
25 http: reqwest::Client,
26 api_key: String,
27 endpoint: String,
28}
29
30impl JevClient {
31 pub fn new(api_key: impl Into<String>, config: &JevConfig) -> Result<Self, JevError> {
32 let api_key = api_key.into();
33 if api_key.trim().is_empty() {
34 return Err(JevError::MissingApiKey);
35 }
36
37 let http = reqwest::Client::builder()
38 .timeout(Duration::from_millis(config.timeout_ms))
39 .user_agent(concat!("jevia/", env!("CARGO_PKG_VERSION")))
40 .build()?;
41 let endpoint = format!(
42 "{}/v1/systemone",
43 config.base_url.trim().trim_end_matches('/')
44 );
45
46 Ok(Self {
47 http,
48 api_key,
49 endpoint,
50 })
51 }
52
53 pub async fn route(
55 &self,
56 task: &str,
57 config: &Config,
58 history: &[RouteRecord],
59 ) -> Result<RouteDecision, JevError> {
60 self.route_with_context(task, None, config, history).await
61 }
62
63 pub async fn route_for_harness(
66 &self,
67 task: &str,
68 harness_name: &str,
69 config: &Config,
70 history: &[RouteRecord],
71 ) -> Result<RouteDecision, JevError> {
72 self.route_with_context(task, Some(harness_name), config, history)
73 .await
74 }
75
76 async fn route_with_context(
77 &self,
78 task: &str,
79 harness_name: Option<&str>,
80 config: &Config,
81 history: &[RouteRecord],
82 ) -> Result<RouteDecision, JevError> {
83 config.validate()?;
84 if task.trim().is_empty() {
85 return Err(JevError::EmptyTask);
86 }
87
88 let request = build_request(task, config, history, harness_name)?;
89 self.send_request(request, config).await
90 }
91
92 pub async fn route_with_history(
94 &self,
95 task: &str,
96 harness_name: Option<&str>,
97 config: &Config,
98 history: &RoutingHistory,
99 ) -> Result<RouteDecision, JevError> {
100 validate_history(task, config, history)?;
101 let request = build_projected_request(task, config, history.context.clone(), harness_name)?;
102 self.send_request(request, config).await
103 }
104
105 async fn send_request(
106 &self,
107 request: SystemOneRequest<'_>,
108 config: &Config,
109 ) -> Result<RouteDecision, JevError> {
110 let response = self
111 .http
112 .post(&self.endpoint)
113 .bearer_auth(&self.api_key)
114 .header("Idempotency-Key", Uuid::new_v4().to_string())
115 .json(&request)
116 .send()
117 .await?;
118
119 let status = response.status();
120 if !status.is_success() {
121 return Err(JevError::ApiStatus(status.as_u16()));
122 }
123
124 let body = read_response(response).await?;
125 decode_decision(&body, config)
126 }
127}
128
129async fn read_response(mut response: reqwest::Response) -> Result<Vec<u8>, JevError> {
130 let too_large = || JevError::Request("response exceeds 1 MiB limit");
131 if response
132 .content_length()
133 .is_some_and(|length| length > MAX_RESPONSE_BYTES as u64)
134 {
135 return Err(too_large());
136 }
137 let mut body = Vec::new();
138 while let Some(chunk) = response.chunk().await? {
139 if chunk.len() > MAX_RESPONSE_BYTES - body.len() {
142 return Err(too_large());
143 }
144 body.extend_from_slice(&chunk);
145 }
146 Ok(body)
147}
148
149pub fn route_cache_key(
152 task: &str,
153 harness_name: Option<&str>,
154 config: &Config,
155 history: &[RouteRecord],
156) -> Result<String, JevError> {
157 config.validate()?;
158 if task.trim().is_empty() {
159 return Err(JevError::EmptyTask);
160 }
161
162 let request = build_request(task, config, history, harness_name)?;
163 cache_key(config, harness_name, request)
164}
165
166pub fn route_cache_key_with_history(
168 task: &str,
169 harness_name: Option<&str>,
170 config: &Config,
171 history: &RoutingHistory,
172) -> Result<String, JevError> {
173 validate_history(task, config, history)?;
174 cache_key(
175 config,
176 harness_name,
177 build_projected_request(task, config, history.context.clone(), harness_name)?,
178 )
179}
180
181fn validate_history(task: &str, config: &Config, history: &RoutingHistory) -> Result<(), JevError> {
182 config.validate()?;
183 if task.trim().is_empty() {
184 return Err(JevError::EmptyTask);
185 }
186 if history.limit != config.router.history_limit {
187 return Err(JevError::Request(
188 "routing history limit does not match policy",
189 ));
190 }
191 Ok(())
192}
193
194fn cache_key(
195 config: &Config,
196 harness_name: Option<&str>,
197 request: SystemOneRequest<'_>,
198) -> Result<String, JevError> {
199 let harness = harness_name.map(|name| {
200 json!({
201 "name": name,
202 "config": config.harnesses.get(name),
203 })
204 });
205 let material = json!({
206 "cache_schema_version": 3,
207 "api_base_url": config.jev.base_url.trim().trim_end_matches('/'),
208 "router": &config.router,
209 "harness": harness,
210 "request": request,
211 });
212 let encoded = serde_json::to_vec(&material).map_err(|_| JevError::CacheKey)?;
213 let digest = Sha256::digest(encoded);
214 let mut key = String::with_capacity(digest.len() * 2);
215 for byte in digest {
216 write!(&mut key, "{byte:02x}").expect("writing to a string cannot fail");
217 }
218 Ok(key)
219}
220
221#[derive(Debug, Serialize)]
222struct SystemOneRequest<'a> {
223 model: &'a str,
224 state: Value,
225 questions: BTreeMap<&'static str, ChoiceQuestion>,
226}
227
228#[derive(Debug, Serialize)]
229struct ChoiceQuestion {
230 #[serde(rename = "type")]
231 kind: &'static str,
232 instructions: &'static str,
233 criteria: BTreeMap<String, String>,
234}
235
236fn build_request<'a>(
237 task: &str,
238 config: &'a Config,
239 history: &[RouteRecord],
240 harness_name: Option<&str>,
241) -> Result<SystemOneRequest<'a>, JevError> {
242 build_projected_request(
243 task,
244 config,
245 history::project(history, config.router.history_limit),
246 harness_name,
247 )
248}
249
250fn build_projected_request<'a>(
251 task: &str,
252 config: &'a Config,
253 history: history::HistoryContext,
254 harness_name: Option<&str>,
255) -> Result<SystemOneRequest<'a>, JevError> {
256 let harness = harness_name
257 .map(|name| {
258 let harness = config.harnesses.get(name).ok_or(JevError::UnknownHarness)?;
259 let tier_models: BTreeMap<_, _> = config
260 .tiers
261 .keys()
262 .map(|tier| (tier, &harness.models[tier]))
263 .collect();
264 Ok::<_, JevError>(json!({"name": name, "tier_models": tier_models}))
265 })
266 .transpose()?;
267
268 let mut state = json!({
269 "current_task": task,
270 "recent_completed_outcomes": history.outcomes,
271 "recent_execution_observations": history.observations,
272 "history_budget": history.budget,
273 "policy": {
274 "goal": "Select the least expensive capability tier likely to complete the task successfully.",
275 "use_outcomes": "Treat relevant successes and failures as evidence, not absolute rules. Prefer the safer tier when evidence conflicts.",
276 "use_history_budget": "Historical tasks marked task_truncated are prefix excerpts, not complete tasks. History windows prioritize newer candidates within byte limits; omitted records and missing text are not evidence of failure or absence of past work.",
277 "use_observations": "Execution observations are operational context, not task-success labels. A process exit, duration, or agent-reported completion does not prove correctness. The requested model is not proof of which models actually executed. A model_observed event reports request selection, not a provider completion. tool_completed is neutral and does not establish tool success. Do not infer quality or model capability from missing feedback."
278 }
279 });
280 if let Some(harness) = harness {
281 state["current_harness"] = harness;
282 state["policy"]["use_current_harness"] = json!(
283 "The tier/model mapping describes configured candidates for this harness, not proof of execution or success. Use the tier descriptions and relevant recorded evidence. Model names alone do not establish quality, cost, or capability; do not transfer outcomes between different models just because their tier labels match."
284 );
285 }
286
287 let criteria = config
288 .tiers
289 .iter()
290 .map(|(name, tier)| (name.clone(), tier.description.clone()))
291 .collect();
292 let questions = [(
293 "tier",
294 ChoiceQuestion {
295 kind: "choice",
296 instructions: "Which capability tier should handle the current task?",
297 criteria,
298 },
299 )]
300 .into_iter()
301 .collect();
302
303 Ok(SystemOneRequest {
304 model: &config.jev.model,
305 state,
306 questions,
307 })
308}
309
310#[derive(Debug, Deserialize)]
311struct SystemOneResponse {
312 model: String,
313 answers: BTreeMap<String, Value>,
314}
315
316#[derive(Debug, Deserialize)]
317struct ChoiceAnswer {
318 #[serde(rename = "type")]
319 kind: String,
320 choice: String,
321 confidence: f64,
322 #[serde(default)]
323 probabilities: BTreeMap<String, f64>,
324}
325
326fn decode_decision(body: &[u8], config: &Config) -> Result<RouteDecision, JevError> {
327 let response: SystemOneResponse = serde_json::from_slice(body)?;
328 let answer = response
329 .answers
330 .get("tier")
331 .ok_or(JevError::MissingTierAnswer)?;
332 let answer: ChoiceAnswer = serde_json::from_value(answer.clone())?;
333
334 if answer.kind != "choice" {
335 return Err(JevError::UnexpectedAnswerType);
336 }
337 if !answer.confidence.is_finite() || !(0.0..=1.0).contains(&answer.confidence) {
338 return Err(JevError::InvalidConfidence);
339 }
340 if !config.tiers.contains_key(&answer.choice) {
341 return Err(JevError::UnknownTier);
342 }
343
344 let fallback_applied = answer.confidence < config.router.confidence_floor;
345 let tier = if fallback_applied {
346 config.router.fallback_tier.clone()
347 } else {
348 answer.choice.clone()
349 };
350
351 let decision = RouteDecision {
352 run_id: Uuid::new_v4().to_string(),
353 tier,
354 suggested_tier: answer.choice,
355 confidence: answer.confidence,
356 probabilities: answer.probabilities,
357 fallback_applied,
358 jev_model: response.model,
359 created_at_ms: now_ms(),
360 source: DecisionSource::Live,
361 };
362 decision.validate().map_err(JevError::InvalidDecision)?;
363 Ok(decision)
364}
365
366fn now_ms() -> u64 {
367 let millis = SystemTime::now()
368 .duration_since(UNIX_EPOCH)
369 .unwrap_or_default()
370 .as_millis();
371 u64::try_from(millis).unwrap_or(u64::MAX)
372}
373
374#[derive(Debug, Error)]
375pub enum JevError {
376 #[error("Jev returned an invalid routing decision: {0} (values redacted)")]
377 InvalidDecision(&'static str),
378 #[error("TYPESAFE_API_KEY is missing or empty")]
379 MissingApiKey,
380 #[error("task cannot be empty")]
381 EmptyTask,
382 #[error("routing harness is not configured (name redacted)")]
383 UnknownHarness,
384 #[error("Jev request failed: {0}")]
385 Request(&'static str),
386 #[error("Jev returned HTTP status {0}")]
387 ApiStatus(u16),
388 #[error("Jev returned invalid JSON at line {line}, column {column} (values redacted)")]
389 InvalidJson { line: usize, column: usize },
390 #[error("could not encode a routing cache key")]
391 CacheKey,
392 #[error("Jev response did not contain the `tier` answer")]
393 MissingTierAnswer,
394 #[error("Jev returned an unexpected tier answer type; expected `choice` (value redacted)")]
395 UnexpectedAnswerType,
396 #[error("Jev returned invalid confidence; expected a number between 0 and 1 (value redacted)")]
397 InvalidConfidence,
398 #[error("Jev selected an undefined tier (value redacted)")]
399 UnknownTier,
400 #[error(transparent)]
401 Config(#[from] crate::ConfigError),
402}
403
404impl From<reqwest::Error> for JevError {
408 fn from(error: reqwest::Error) -> Self {
409 Self::Request(if error.is_timeout() {
410 "timeout"
411 } else if error.is_connect() {
412 "connection failed"
413 } else if error.is_builder() {
414 "invalid request configuration"
415 } else if error.is_body() {
416 "response body failed"
417 } else if error.is_decode() {
418 "response decoding failed"
419 } else {
420 "transport failure"
421 })
422 }
423}
424
425impl From<serde_json::Error> for JevError {
426 fn from(error: serde_json::Error) -> Self {
427 Self::InvalidJson {
428 line: error.line(),
429 column: error.column(),
430 }
431 }
432}
433
434impl From<StatusCode> for JevError {
435 fn from(status: StatusCode) -> Self {
436 Self::ApiStatus(status.as_u16())
437 }
438}
439
440#[cfg(test)]
441mod tests {
442 use crate::{ExecutionEvidence, Outcome, OutcomeSource, VerificationEvidence};
443
444 use super::*;
445
446 fn harness(model: &str) -> crate::HarnessConfig {
447 crate::HarnessConfig {
448 command: "PRIVATE-command".into(),
449 args: vec!["PRIVATE-argument".into(), "{task}".into(), "{model}".into()],
450 models: Config::default()
451 .tiers
452 .keys()
453 .map(|tier| (tier.clone(), model.into()))
454 .collect(),
455 auto_verify: false,
456 observations: Default::default(),
457 verification: Some(crate::VerificationConfig {
458 command: "PRIVATE-verifier".into(),
459 args: vec!["PRIVATE-verifier-argument".into()],
460 }),
461 }
462 }
463
464 #[test]
465 fn current_harness_context_is_allowlisted_and_changes_with_model_mapping() {
466 let mut config = Config::default();
467 config
468 .harnesses
469 .insert("alpha".into(), harness("provider/alpha"));
470 config.harnesses.insert(
471 "PRIVATE-other-harness".into(),
472 harness("PRIVATE-other-model"),
473 );
474 config
475 .harnesses
476 .get_mut("alpha")
477 .unwrap()
478 .models
479 .insert("unused".into(), "PRIVATE-unused-model".into());
480 config.validate().unwrap();
481 let request = build_request("task", &config, &[], Some("alpha")).unwrap();
482 assert_eq!(
483 request.state["current_harness"],
484 json!({
485 "name": "alpha", "tier_models": {
486 "fast": "provider/alpha", "balanced": "provider/alpha", "strong": "provider/alpha"
487 }
488 })
489 );
490 assert!(!serde_json::to_string(&request).unwrap().contains("PRIVATE"));
491 let key = route_cache_key("task", Some("alpha"), &config, &[]).unwrap();
492 let plain = build_request("task", &config, &[], None).unwrap();
493 assert!(plain.state.get("current_harness").is_none());
494 let plain = serde_json::to_value(plain).unwrap();
495 config
496 .harnesses
497 .get_mut("alpha")
498 .unwrap()
499 .models
500 .insert("fast".into(), "provider/new".into());
501 let changed = build_request("task", &config, &[], Some("alpha")).unwrap();
502 assert_eq!(
503 changed.state["current_harness"]["tier_models"]["fast"],
504 "provider/new"
505 );
506 assert_ne!(
507 key,
508 route_cache_key("task", Some("alpha"), &config, &[]).unwrap()
509 );
510 assert_eq!(
511 plain,
512 serde_json::to_value(build_request("task", &config, &[], None).unwrap()).unwrap()
513 );
514 }
515
516 #[test]
517 fn unknown_routing_harness_is_rejected_without_echoing_its_name() {
518 let config = Config::default();
519 let error = build_request("task", &config, &[], Some("PRIVATE-harness")).unwrap_err();
520 assert!(matches!(error, JevError::UnknownHarness));
521 assert!(!format!("{error} {error:?}").contains("PRIVATE"));
522 assert!(route_cache_key("task", Some("PRIVATE-harness"), &config, &[]).is_err());
523 }
524
525 #[test]
526 fn rejects_invalid_probabilities_and_blank_models_without_echoing_values() {
527 let valid = json!({"model": "test", "answers": {"tier": {
528 "type": "choice", "choice": "fast", "confidence": 0.9,
529 "probabilities": {"private-tier": 0.9}
530 }}});
531 for probability in [-1.0, 1.01, 2.0] {
532 let mut body = valid.clone();
533 body["answers"]["tier"]["probabilities"]["private-tier"] = probability.into();
534 let error = decode_decision(&serde_json::to_vec(&body).unwrap(), &Config::default())
535 .unwrap_err();
536 assert!(matches!(error, JevError::InvalidDecision(_)));
537 assert!(!format!("{error:?} {error}").contains("private-tier"));
538 }
539 for model in ["", " \n\t"] {
540 let mut body = valid.clone();
541 body["model"] = model.into();
542 assert!(
543 decode_decision(&serde_json::to_vec(&body).unwrap(), &Config::default()).is_err()
544 );
545 }
546 for probabilities in [json!({}), json!({"fast": 0.0, "strong": 1.0})] {
547 let mut body = valid.clone();
548 body["answers"]["tier"]["probabilities"] = probabilities;
549 let mut decision =
550 decode_decision(&serde_json::to_vec(&body).unwrap(), &Config::default()).unwrap();
551 for number in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
552 decision.probabilities.insert("private-tier".into(), number);
553 assert!(decision.validate().is_err());
554 }
555 }
556 }
557
558 #[test]
559 fn provider_errors_discard_private_values_in_all_formats() {
560 use std::error::Error as _;
561 const PRIVATE: &str = "PRIVATE_PROVIDER_SENTINEL";
562 let valid = json!({
563 "model": "test", "answers": {"tier": {
564 "type": "choice", "choice": "fast", "confidence": 0.9
565 }}
566 });
567 for field in ["type", "choice", "confidence", "probabilities"] {
568 let mut invalid = valid.clone();
569 invalid["answers"]["tier"][field] = PRIVATE.into();
570 let error = decode_decision(&serde_json::to_vec(&invalid).unwrap(), &Config::default())
571 .unwrap_err();
572 assert!(!format!("{error} {error:?}").contains(PRIVATE));
573 assert!(error.source().is_none());
574 }
575 let error: JevError = reqwest::Client::new()
576 .get(format!("invalid-url-{PRIVATE}"))
577 .build()
578 .unwrap_err()
579 .into();
580 assert!(!format!("{error} {error:?}").contains(PRIVATE));
581 assert!(error.source().is_none());
582 }
583
584 #[test]
585 fn request_includes_only_recent_completed_outcomes() {
586 let mut config = Config::default();
587 config.router.history_limit = 1;
588 let mut completed = record("last", "strong", Outcome::Failure);
589 completed.execution = Some(ExecutionEvidence {
590 observations: None,
591 harness: "agent".to_owned(),
592 model: "provider/frontier".to_owned(),
593 duration_ms: 42,
594 exit_code: Some(1),
595 verification: Some(VerificationEvidence {
596 command: "cargo".to_owned(),
597 launched: true,
598 duration_ms: 21,
599 exit_code: Some(1),
600 }),
601 });
602 let history = vec![
603 record("first", "fast", Outcome::Success),
604 record("pending", "balanced", Outcome::Unknown),
605 completed,
606 ];
607
608 let request = build_request("current", &config, &history, None).unwrap();
609 let outcomes = request.state["recent_completed_outcomes"]
610 .as_array()
611 .expect("outcomes are an array");
612
613 assert_eq!(outcomes.len(), 1);
614 assert_eq!(outcomes[0]["task"], "last");
615 assert_eq!(outcomes[0]["outcome"], "failure");
616 assert_eq!(outcomes[0]["execution"]["harness"], "agent");
617 assert_eq!(outcomes[0]["execution"]["model"], "provider/frontier");
618 assert_eq!(outcomes[0]["execution"]["duration_ms"], 42);
619 assert_eq!(outcomes[0]["execution"]["exit_code"], 1);
620 assert_eq!(outcomes[0]["execution"]["verification"]["command"], "cargo");
621 assert_eq!(outcomes[0]["execution"]["verification"]["exit_code"], 1);
622 assert_eq!(outcomes[0]["execution"]["verification"]["launched"], true);
623 }
624
625 #[test]
626 fn passive_history_changes_routing_context_without_inventing_success() {
627 let config = Config::default();
628 let mut observed = record("passive", "fast", Outcome::Unknown);
629 observed.lifecycle = Some(crate::RunLifecycle {
630 state: crate::RunState::Completed,
631 started_at_ms: Some(1),
632 finished_at_ms: Some(2),
633 });
634 observed.execution = Some(ExecutionEvidence {
635 observations: None,
636 harness: "agent".into(),
637 model: "requested-model".into(),
638 duration_ms: 42,
639 exit_code: Some(0),
640 verification: None,
641 });
642 let baseline = route_cache_key("task", None, &config, &[]).unwrap();
643 let with_history = route_cache_key("task", None, &config, &[observed.clone()]).unwrap();
644 assert_ne!(baseline, with_history);
645 let request = build_request("task", &config, &[observed.clone()], None).unwrap();
646 assert_eq!(request.state["recent_completed_outcomes"], json!([]));
647 let facts = &request.state["recent_execution_observations"][0];
648 assert_eq!(facts["requested_model"], "requested-model");
649 assert_eq!(facts["process_exit_code"], 0);
650 assert!(facts.get("outcome").is_none());
651 observed.lifecycle.as_mut().unwrap().state = crate::RunState::Running;
652 assert_eq!(
653 baseline,
654 route_cache_key("task", None, &config, &[observed.clone()]).unwrap()
655 );
656 observed.lifecycle.as_mut().unwrap().state = crate::RunState::Cancelled;
657 observed.task = None;
658 let mut disabled = config;
659 disabled.router.history_limit = 0;
660 let request = build_request("task", &disabled, &[observed], None).unwrap();
661 assert_eq!(request.state["recent_execution_observations"], json!([]));
662 }
663
664 #[test]
665 fn low_confidence_uses_the_safe_fallback() {
666 let config = Config::default();
667 let body = br#"{
668 "model": "jev-1.13.0",
669 "answers": {
670 "tier": {
671 "type": "choice",
672 "choice": "fast",
673 "confidence": 0.42,
674 "probabilities": {"fast": 0.42, "balanced": 0.35, "strong": 0.23}
675 }
676 }
677 }"#;
678
679 let decision = decode_decision(body, &config).expect("response decodes");
680
681 assert_eq!(decision.suggested_tier, "fast");
682 assert_eq!(decision.tier, "strong");
683 assert!(decision.fallback_applied);
684 }
685
686 #[test]
687 fn routing_excludes_unverified_results_and_local_feedback_notes() {
688 let mut unverified = record("process only", "fast", Outcome::Success);
689 unverified.outcome_evidence.as_mut().unwrap().source = OutcomeSource::ProcessExit;
690 let mut legacy = record("legacy", "fast", Outcome::Success);
691 legacy.outcome_evidence = None;
692 let mut verified = record("verified", "fast", Outcome::Success);
693 verified.outcome_evidence.as_mut().unwrap().source = OutcomeSource::Verification;
694 let mut manual = record("manual", "fast", Outcome::Failure);
695 manual.feedback.push(crate::FeedbackEvent {
696 previous_outcome: Outcome::Success,
697 previous_source: Some(OutcomeSource::ProcessExit),
698 outcome: Outcome::Failure,
699 recorded_at_ms: 2,
700 reason: Some("private-note".into()),
701 });
702 let config = Config::default();
703 let baseline = route_cache_key("task", None, &config, &[]).unwrap();
704 assert_eq!(
705 baseline,
706 route_cache_key("task", None, &config, &[unverified.clone(), legacy.clone()]).unwrap()
707 );
708 let request = build_request(
709 "task",
710 &config,
711 &[unverified, legacy, verified, manual],
712 None,
713 )
714 .unwrap();
715 let outcomes = request.state["recent_completed_outcomes"]
716 .as_array()
717 .unwrap();
718 assert_eq!(outcomes.len(), 2);
719 assert_eq!(outcomes[0]["outcome_source"], "verification");
720 assert_eq!(outcomes[1]["outcome_source"], "manual");
721 assert!(
722 !serde_json::to_string(&request)
723 .unwrap()
724 .contains("private-note")
725 );
726 }
727
728 #[test]
729 fn confident_answer_keeps_the_selected_tier() {
730 let config = Config::default();
731 let body = br#"{
732 "model": "jev-1.13.0",
733 "answers": {
734 "tier": {
735 "type": "choice",
736 "choice": "balanced",
737 "confidence": 0.87,
738 "probabilities": {"fast": 0.05, "balanced": 0.87, "strong": 0.08}
739 }
740 }
741 }"#;
742
743 let decision = decode_decision(body, &config).expect("response decodes");
744
745 assert_eq!(decision.tier, "balanced");
746 assert!(!decision.fallback_applied);
747 assert_eq!(decision.source, DecisionSource::Live);
748 }
749
750 #[test]
751 fn cache_key_changes_only_when_routing_inputs_change() {
752 let mut config = Config::default();
753 config
754 .harnesses
755 .insert("agent".into(), harness("agent-model"));
756 config
757 .harnesses
758 .insert("other".into(), harness("other-model"));
759 let pending = record("pending", "balanced", Outcome::Unknown);
760 let completed = record("completed", "balanced", Outcome::Success);
761
762 let baseline = route_cache_key("fix parser", Some("agent"), &config, &[])
763 .expect("cache key is created");
764 let repeated = route_cache_key("fix parser", Some("agent"), &config, &[pending])
765 .expect("cache key is created");
766 let with_evidence = route_cache_key("fix parser", Some("agent"), &config, &[completed])
767 .expect("cache key is created");
768 let other_task = route_cache_key("fix lexer", Some("agent"), &config, &[])
769 .expect("cache key is created");
770 let other_harness = route_cache_key("fix parser", Some("other"), &config, &[])
771 .expect("cache key is created");
772
773 assert_eq!(baseline, repeated);
774 assert_ne!(baseline, with_evidence);
775 assert_ne!(baseline, other_task);
776 assert_ne!(baseline, other_harness);
777 assert_eq!(baseline.len(), 64);
778 assert!(!baseline.contains("fix parser"));
779 }
780
781 #[test]
782 fn cache_key_changes_with_local_routing_policy() {
783 let config = Config::default();
784 let baseline =
785 route_cache_key("fix parser", None, &config, &[]).expect("cache key is created");
786 let mut changed = config.clone();
787 changed.router.confidence_floor = 0.9;
788
789 let changed =
790 route_cache_key("fix parser", None, &changed, &[]).expect("cache key is created");
791
792 assert_ne!(baseline, changed);
793 }
794
795 pub(super) fn record(task: &str, tier: &str, outcome: Outcome) -> RouteRecord {
796 RouteRecord {
797 schema_version: 1,
798 decision: RouteDecision {
799 run_id: Uuid::new_v4().to_string(),
800 tier: tier.to_owned(),
801 suggested_tier: tier.to_owned(),
802 confidence: 0.9,
803 probabilities: BTreeMap::new(),
804 fallback_applied: false,
805 jev_model: "jev-test".to_owned(),
806 created_at_ms: 1,
807 source: DecisionSource::Live,
808 },
809 task: Some(task.to_owned()),
810 outcome,
811 execution: None,
812 lifecycle: None,
813 outcome_evidence: Some(crate::OutcomeEvidence {
814 source: crate::OutcomeSource::Manual,
815 recorded_at_ms: 1,
816 }),
817 feedback: Vec::new(),
818 }
819 }
820}