Load candidate bodies from the vault, score, and rank them.
(
repo: &Repository,
candidates: HashMap<String, CandidateScore>,
limit: usize,
)
| 434 | |
| 435 | /// Load candidate bodies from the vault, score, and rank them. |
| 436 | fn resolve_and_rank( |
| 437 | repo: &Repository, |
| 438 | candidates: HashMap<String, CandidateScore>, |
| 439 | limit: usize, |
| 440 | ) -> CliResult<Vec<MemoryItem>> { |
| 441 | let now = chrono::Utc::now(); |
| 442 | let mut items: Vec<MemoryItem> = Vec::new(); |
| 443 | |
| 444 | for (node_id, candidate) in candidates { |
| 445 | let Some(path) = candidate |
| 446 | .path |
| 447 | .clone() |
| 448 | .or_else(|| legacy_memory_node_id_to_path(&node_id)) |
| 449 | else { |
| 450 | continue; |
| 451 | }; |
| 452 | let Some(entry) = repo.vault_retrieve(&path).map_err(CliError::Repository)? else { |
| 453 | continue; // stale KG node — the entry no longer exists |
| 454 | }; |
| 455 | |
| 456 | if !is_eligible_memory(&entry) { |
| 457 | continue; |
| 458 | } |
| 459 | let kind = frontmatter_kind(&entry.frontmatter_json); |
| 460 | let Some(status) = frontmatter_status(&entry.frontmatter_json) else { |
| 461 | continue; |
| 462 | }; |
| 463 | |
| 464 | let memory_id = |
| 465 | frontmatter_memory_id(&entry.frontmatter_json).unwrap_or_else(|| node_id.clone()); |
| 466 | let name = frontmatter_name(&entry.frontmatter_json).unwrap_or_else(|| { |
| 467 | node_id |
| 468 | .rsplit_once(':') |
| 469 | .map(|(_, n)| n) |
| 470 | .unwrap_or(&node_id) |
| 471 | .to_string() |
| 472 | }); |
| 473 | let recency = recency_score(&entry.updated_at, now); |
| 474 | items.push(MemoryItem { |
| 475 | memory_id, |
| 476 | kg_node_id: node_id, |
| 477 | revision_hash: vault_revision_hash(&entry), |
| 478 | content_hash: Hash::from_bytes(entry.content_hash).to_base32(), |
| 479 | path, |
| 480 | name, |
| 481 | kind, |
| 482 | status, |
| 483 | updated_at: entry.updated_at.clone(), |
| 484 | introduced_by: entry.introduced_by, |
| 485 | score: final_score(&candidate, recency), |
| 486 | body: String::from_utf8_lossy(&entry.content_bytes) |
| 487 | .trim() |
| 488 | .to_string(), |
| 489 | truncated: false, |
| 490 | }); |
| 491 | } |
| 492 | |
| 493 | items.sort_by(|a, b| { |