Skip to content

Commit 02e6782

Browse files
authored
style: apply cargo fmt to runtime drivers (librefang#2380)
* style: apply cargo fmt to runtime drivers * test(fallback): seed primary health state directly in cooldown recovery test The dispatch loop never accrued three errors because health_order() reroutes to the healthy secondary after the first failure. Seed the counters directly so the test exercises what it claims to.
1 parent 141b559 commit 02e6782

2 files changed

Lines changed: 30 additions & 24 deletions

File tree

crates/librefang-runtime/src/drivers/fallback.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,10 @@ mod tests {
330330

331331
#[tokio::test]
332332
async fn unhealthy_driver_recovers_after_cooldown() {
333-
// Two drivers: primary fails, secondary succeeds. After a few primary
334-
// failures we simulate the recovery window elapsing and verify that
335-
// `maybe_recover` clears both the error counter and the EWMA penalty.
333+
// Seed the primary's health state directly to simulate three
334+
// accumulated failures. We can't get there via dispatch: after the
335+
// first failure health_order() reroutes to the healthy secondary,
336+
// so the primary would never accrue a second error through complete().
336337
let fb = FallbackDriver::with_models(vec![
337338
(
338339
Arc::new(FailDriver) as Arc<dyn LlmDriver>,
@@ -341,20 +342,22 @@ mod tests {
341342
(Arc::new(OkDriver) as Arc<dyn LlmDriver>, "ok".to_string()),
342343
]);
343344

344-
// Three dispatches → primary accumulates 3 errors and 3×ERROR_PENALTY_MS.
345-
for _ in 0..3 {
346-
let _ = fb.complete(test_request()).await;
347-
}
348345
let primary = &fb.drivers[0];
346+
primary.consecutive_errors.store(3, Ordering::Relaxed);
347+
primary
348+
.last_failure_at_ms
349+
.store(FallbackDriver::now_ms(), Ordering::Relaxed);
350+
primary
351+
.ewma_latency_ms
352+
.store(ERROR_PENALTY_MS * 3, Ordering::Relaxed);
353+
349354
assert_eq!(primary.consecutive_errors.load(Ordering::Relaxed), 3);
350355
let penalised = primary.ewma_latency_ms.load(Ordering::Relaxed);
351356
assert!(penalised >= ERROR_PENALTY_MS * 3);
352357

353358
// Simulate the cooldown elapsing by calling maybe_recover with a
354359
// fabricated future timestamp.
355-
let future = primary.last_failure_at_ms.load(Ordering::Relaxed)
356-
+ HEALTH_RECOVERY_MS
357-
+ 1;
360+
let future = primary.last_failure_at_ms.load(Ordering::Relaxed) + HEALTH_RECOVERY_MS + 1;
358361
FallbackDriver::maybe_recover(primary, future);
359362

360363
assert_eq!(

crates/librefang-runtime/src/drivers/qwen_code.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -558,19 +558,19 @@ impl LlmDriver for QwenCodeDriver {
558558
// Qwen CLI 0.14+ sometimes emits a full JSON array on a single
559559
// line instead of one event per line. Unwrap it into individual
560560
// events before the normal line-by-line handler runs.
561-
let events: Vec<QwenStreamEvent> =
562-
if trimmed.starts_with('[') && trimmed.ends_with(']') {
563-
serde_json::from_str(trimmed).unwrap_or_default()
564-
} else if let Ok(single) = serde_json::from_str::<QwenStreamEvent>(trimmed) {
565-
vec![single]
566-
} else {
567-
// Not valid JSON. This used to be forwarded to the UI as
568-
// a TextDelta, which surfaced raw stderr/preamble/garbage
569-
// in the chat. Log and drop — assistant text only comes
570-
// from structured events.
571-
warn!(line = %trimmed, "Dropping non-JSON line from Qwen CLI stdout");
572-
continue;
573-
};
561+
let events: Vec<QwenStreamEvent> = if trimmed.starts_with('[') && trimmed.ends_with(']')
562+
{
563+
serde_json::from_str(trimmed).unwrap_or_default()
564+
} else if let Ok(single) = serde_json::from_str::<QwenStreamEvent>(trimmed) {
565+
vec![single]
566+
} else {
567+
// Not valid JSON. This used to be forwarded to the UI as
568+
// a TextDelta, which surfaced raw stderr/preamble/garbage
569+
// in the chat. Log and drop — assistant text only comes
570+
// from structured events.
571+
warn!(line = %trimmed, "Dropping non-JSON line from Qwen CLI stdout");
572+
continue;
573+
};
574574

575575
for event in events {
576576
match event.r#type.as_str() {
@@ -752,7 +752,10 @@ mod tests {
752752
// object shape — must not leak raw text into the chat.
753753
let out = r#"{"totally":"unexpected","shape":123}"#;
754754
let (t, _) = extract_text_from_qwen_output(out);
755-
assert_eq!(t, "", "unrecognised JSON shape must produce empty text, not leak raw JSON into chat");
755+
assert_eq!(
756+
t, "",
757+
"unrecognised JSON shape must produce empty text, not leak raw JSON into chat"
758+
);
756759
}
757760

758761
#[test]

0 commit comments

Comments
 (0)