fix: search_bm25 NUMERIC→f64 decode panic + rerank UTF-8 truncation

- postgres_db.rs: CAST source_weight to ::float8 (NUMERIC→FLOAT8)
- rerank.rs: char-based truncation for UTF-8 safety
- Both fixes prevent tokio worker panics that killed search requests

Bug root causes:
- keyword/BM25: sqlx decode f64 from NUMERIC panicked at postgres_db.rs:3758
- llm-smart: &text[..100] byte slice panicked at rerank.rs:52 on Chinese text

Verified: both /api/v1/search/keyword and /api/v1/search/llm-smart now return 200
This commit is contained in:
Accusys
2026-09-17 16:55:48 +08:00
parent e25c279eeb
commit 791c782606
2 changed files with 195 additions and 43 deletions
+55 -4
View File
@@ -48,8 +48,9 @@ pub async fn rerank_search_results(
let mut chunks_text = String::new();
for (i, (_, text)) in candidates.iter().enumerate() {
let display = if text.len() > 100 {
format!("{}...", &text[..100])
let display = if text.chars().count() > 100 {
let truncated: String = text.chars().take(100).collect();
format!("{}...", truncated)
} else {
text.to_string()
};
@@ -171,9 +172,59 @@ Include every chunk number exactly once. Only respond with the JSON."#,
}
}
warn!(
warn!(
"LLM rerank: could not parse response — content: {}",
&content[..content.len().min(200)]
);
Ok(candidates.iter().map(|(idx, _)| *idx).collect())
Ok((0..candidates.len()).collect())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_utf8_truncation_safe() {
let long_chinese = "這是一段很長的中文文字,用來測試字元截斷是否正確運作,避免在多位元組字元中間切割造成恐慌。".repeat(3);
let text = long_chinese.as_str();
let display = if text.chars().count() > 100 {
let truncated: String = text.chars().take(100).collect();
format!("{}...", truncated)
} else {
text.to_string()
};
assert!(display.chars().count() <= 103);
assert!(display.starts_with("這是一段很長"));
assert!(display.ends_with("..."));
}
#[test]
fn test_utf8_truncation_short() {
let short_text = "短文字";
let display = if short_text.chars().count() > 100 {
let truncated: String = short_text.chars().take(100).collect();
format!("{}...", truncated)
} else {
short_text.to_string()
};
assert_eq!(display, "短文字");
assert!(!display.ends_with("..."));
}
#[test]
fn test_utf8_truncation_mixed() {
let mixed = "Mixed 中英文 text 混合 content".repeat(10);
let display = if mixed.chars().count() > 100 {
let truncated: String = mixed.chars().take(100).collect();
format!("{}...", truncated)
} else {
mixed.to_string()
};
assert!(display.chars().count() <= 103);
assert!(display.ends_with("..."));
}
}