Gather candidate memory nodes from all seeds: an intent's terms and graph neighbors, file neighbors, keyword search, body-term matches. With no seeds at all, falls back to the most recently updated memories so a bare context request is still useful.
(
repo: &Repository,
query: &[String],
intent: Option<&str>,
files: &[String],
limit: usize,
)
| 342 | /// With no seeds at all, falls back to the most recently updated |
| 343 | /// memories so a bare context request is still useful. |
| 344 | fn gather_candidates( |
| 345 | repo: &Repository, |
| 346 | query: &[String], |
| 347 | intent: Option<&str>, |
| 348 | files: &[String], |
| 349 | limit: usize, |
| 350 | ) -> Result<HashMap<String, CandidateScore>, Status> { |
| 351 | let mut candidates: HashMap<String, CandidateScore> = HashMap::new(); |
| 352 | let mut seed_terms: Vec<String> = query.to_vec(); |
| 353 | let mut has_explicit_seeds = |
| 354 | query.iter().any(|term| !term.trim().is_empty()) || intent.is_some(); |
| 355 | |
| 356 | if let Some(intent_id) = intent { |
| 357 | seed_from_intent(repo, intent_id, &mut seed_terms, &mut candidates)?; |
| 358 | has_explicit_seeds = true; |
| 359 | } |
| 360 | |
| 361 | for file in files { |
| 362 | let node_id = iri::mint_file( |
| 363 | &repo.project_id().map_err(repository_error)?, |
| 364 | file.trim_start_matches("./"), |
| 365 | ); |
| 366 | add_memory_neighbors(repo, &node_id, 1, &mut candidates)?; |
| 367 | has_explicit_seeds = true; |
| 368 | } |
| 369 | |
| 370 | let seed_query = seed_terms.join(" "); |
| 371 | if !seed_query.trim().is_empty() { |
| 372 | let pool = limit.saturating_mul(SEARCH_POOL_MULTIPLIER); |
| 373 | let nodes = repo |
| 374 | .vault_kg_search_by_kind( |
| 375 | &seed_query, |
| 376 | pool, |
| 377 | atomic_core::kg::ontology::Class::Memory, |
| 378 | ) |
| 379 | .map_err(repository_error)?; |
| 380 | for (rank, node) in nodes.iter().enumerate() { |
| 381 | let base = rank_score(rank); |
| 382 | merge_candidate( |
| 383 | &mut candidates, |
| 384 | &node.id, |
| 385 | memory_path_from_node(node), |
| 386 | base, |
| 387 | false, |
| 388 | ); |
| 389 | } |
| 390 | scan_memory_bodies(repo, &seed_query, &mut candidates)?; |
| 391 | } |
| 392 | |
| 393 | // No seeds of any kind: fall back to the most recent memories. |
| 394 | if candidates.is_empty() && !has_explicit_seeds { |
| 395 | let mut metas = repo |
| 396 | .vault_list("memory/", Some(VaultEntryType::Memory)) |
| 397 | .map_err(repository_error)?; |
| 398 | metas.sort_by(|a, b| b.updated_at.cmp(&a.updated_at)); |
| 399 | for meta in metas { |
| 400 | let Some(entry) = repo.vault_retrieve(&meta.path).map_err(repository_error)? else { |
| 401 | continue; |
no test coverage detected