chore: fix formatting and clippy lints

This commit is contained in:
Riz Ashraf committed 2026-10-08 23:01:38 +01:00
1 parent 8952bd5399
commit 9d9e959744
35 files changed
+1091 -582

No files matched your search

+72 -28
View File
@@ -16,6 +16,26 @@ pub struct SearchResult {
pub type SearchResultTuple = (String, String, String, String, f32);
fn calculate_time_decay(timestamp_sec: Option<u64>) -> f32 {
let Some(ts) = timestamp_sec else { return 1.0 };
if ts == 0 {
return 1.0;
} // Avoid decaying default unset timestamps
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
if ts > now {
return 1.0;
}
let age_days = (now - ts) as f32 / 86400.0;
// 1% decay per day
(-0.01 * age_days).exp()
}
#[derive(Clone)]
pub struct MemoryIndex {
pub index: Index,
@@ -55,7 +75,8 @@ impl MemoryIndex {
.reload_policy(ReloadPolicy::OnCommitWithDelay)
.try_into()?;
let query_parser = tantivy::query::QueryParser::for_index(&index, vec![title_field, body_field]);
let query_parser =
tantivy::query::QueryParser::for_index(&index, vec![title_field, body_field]);
Ok(Self {
index,
@@ -87,7 +108,8 @@ impl MemoryIndex {
.reload_policy(ReloadPolicy::OnCommitWithDelay)
.try_into()?;
let query_parser = tantivy::query::QueryParser::for_index(&index, vec![title_field, body_field]);
let query_parser =
tantivy::query::QueryParser::for_index(&index, vec![title_field, body_field]);
Ok(Self {
index,
@@ -117,7 +139,10 @@ impl MemoryIndex {
self.index_entities_batch(std::slice::from_ref(e))
}
pub fn index_entities_batch(&self, entities: &[Entity]) -> tokio::task::JoinHandle<tantivy::Result<()>> {
pub fn index_entities_batch(
&self,
entities: &[Entity],
) -> tokio::task::JoinHandle<tantivy::Result<()>> {
let writer = Arc::clone(&self.writer);
let id_field = self.id_field;
let needs_commit = Arc::clone(&self.needs_commit);
@@ -154,13 +179,16 @@ impl MemoryIndex {
let id_val = t.id.clone();
let needs_commit = Arc::clone(&self.needs_commit);
let criteria_body = t.acceptance_criteria.iter().fold(String::new(), |mut acc, c| {
if !acc.is_empty() {
acc.push('\n');
}
acc.push_str(&c.description);
acc
});
let criteria_body = t
.acceptance_criteria
.iter()
.fold(String::new(), |mut acc, c| {
if !acc.is_empty() {
acc.push('\n');
}
acc.push_str(&c.description);
acc
});
let doc = doc!(
self.id_field => t.id.as_str(),
@@ -231,7 +259,10 @@ impl MemoryIndex {
if let Err(first_err) = writer.commit() {
std::thread::sleep(std::time::Duration::from_millis(100));
writer.commit().map_err(|second_err| {
tantivy::TantivyError::SystemError(format!("First commit error: {}; Second commit error: {}", first_err, second_err))
tantivy::TantivyError::SystemError(format!(
"First commit error: {}; Second commit error: {}",
first_err, second_err
))
})?;
}
let _ = reader.reload();
@@ -246,9 +277,11 @@ impl MemoryIndex {
})
}
pub fn commit_if_needed(&self) -> tantivy::Result<()> {
if self.needs_commit.swap(false, std::sync::atomic::Ordering::SeqCst) {
if self
.needs_commit
.swap(false, std::sync::atomic::Ordering::SeqCst)
{
let mut writer = self.writer.lock().unwrap_or_else(|e| e.into_inner());
writer.commit()?;
let _ = self.reader.reload();
@@ -268,8 +301,8 @@ impl MemoryIndex {
let sanitized: String = query
.chars()
.map(|c| match c {
'+' | '-' | '&' | '|' | '!' | '(' | ')' | '{' | '}' | '[' | ']' | '^'
| '"' | '~' | '*' | '?' | ':' | '\\' | '/' => ' ',
'+' | '-' | '&' | '|' | '!' | '(' | ')' | '{' | '}' | '[' | ']' | '^' | '"'
| '~' | '*' | '?' | ':' | '\\' | '/' => ' ',
_ => c,
})
.collect();
@@ -284,10 +317,11 @@ impl MemoryIndex {
let target_limit = if is_wildcard_fallback { 10 } else { 50 };
let final_query: Box<dyn tantivy::query::Query> = if let Some(ns) = namespace {
let ns_term_query: Box<dyn tantivy::query::Query> = Box::new(tantivy::query::TermQuery::new(
tantivy::Term::from_field_text(self.namespace_field, ns),
tantivy::schema::IndexRecordOption::Basic,
));
let ns_term_query: Box<dyn tantivy::query::Query> =
Box::new(tantivy::query::TermQuery::new(
tantivy::Term::from_field_text(self.namespace_field, ns),
tantivy::schema::IndexRecordOption::Basic,
));
let ns_filter: Box<dyn tantivy::query::Query> = if ns != "global" {
let global_term_query = Box::new(tantivy::query::TermQuery::new(
tantivy::Term::from_field_text(self.namespace_field, "global"),
@@ -491,7 +525,8 @@ impl SearchService {
self.state.code.snippets.read_with(|snips| {
for snippet in snips.iter() {
if let Some(ref emb) = snippet.embedding {
let sim = cosine_similarity(&query_emb, emb);
let base_sim = cosine_similarity(&query_emb, emb);
let sim = base_sim * calculate_time_decay(Some(snippet.updated_at));
results.push(SearchResult {
id: snippet.name.clone(),
doc_type: "snippet".to_string(),
@@ -508,6 +543,7 @@ impl SearchService {
snippet.name.clone(),
"snippet".to_string(),
snippet.description.clone(),
Some(snippet.updated_at),
));
}
}
@@ -515,15 +551,15 @@ impl SearchService {
self.state.read_graph(|graph| {
for entity in graph.entities.values() {
if let Some(ns) = filter_namespace {
if entity.namespace != ns {
if let Some(ns) = filter_namespace
&& entity.namespace != ns {
continue;
}
}
let obs = entity.observations.join("; ");
let desc = format!("{}: {}", entity.entity_type, obs);
if let Some(ref emb) = entity.embedding {
let sim = cosine_similarity(&query_emb, emb);
let base_sim = cosine_similarity(&query_emb, emb);
let sim = base_sim * calculate_time_decay(entity.updated_at);
results.push(SearchResult {
id: entity.name.clone(),
doc_type: "entity".to_string(),
@@ -533,7 +569,12 @@ impl SearchService {
});
} else if uncached_texts.len() < 50 {
uncached_texts.push(format!("{} {} {}", entity.name, entity.entity_type, obs));
uncached_meta.push((entity.name.clone(), "entity".to_string(), desc));
uncached_meta.push((
entity.name.clone(),
"entity".to_string(),
desc,
entity.updated_at,
));
}
}
});
@@ -541,7 +582,8 @@ impl SearchService {
self.state.code.error_fixes.read_with(|fixes| {
for fix in fixes.iter() {
if let Some(ref emb) = fix.embedding {
let sim = cosine_similarity(&query_emb, emb);
let base_sim = cosine_similarity(&query_emb, emb);
let sim = base_sim * calculate_time_decay(Some(fix.timestamp));
results.push(SearchResult {
id: fix.signature.clone(),
doc_type: "error_fix".to_string(),
@@ -555,6 +597,7 @@ impl SearchService {
fix.signature.clone(),
"error_fix".to_string(),
fix.solution.clone(),
Some(fix.timestamp),
));
}
}
@@ -563,8 +606,9 @@ impl SearchService {
if !uncached_texts.is_empty()
&& let Ok(embeddings) = generate_embeddings_async(uncached_texts).await
{
for (emb, (title, doc_type, body)) in embeddings.into_iter().zip(uncached_meta) {
let sim = cosine_similarity(&query_emb, &emb);
for (emb, (title, doc_type, body, ts)) in embeddings.into_iter().zip(uncached_meta) {
let base_sim = cosine_similarity(&query_emb, &emb);
let sim = base_sim * calculate_time_decay(ts);
results.push(SearchResult {
id: title.clone(),
doc_type,