Incremental memory scoring with per-score persistence

score_memories_incremental now takes an async callback that fires
after each memory is scored. The callback:
- Writes the score to the conversation entry via set_score()
- Persists to memory-scores.json immediately
- Notifies the UI so the context screen updates live

Scoring no longer batches — each score is visible and persisted
as it completes. Does not touch the memory store.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
Kent Overstreet 2026-04-07 21:10:09 -04:00
parent e213644514
commit fd58386951
2 changed files with 28 additions and 20 deletions

View file

@ -299,13 +299,18 @@ pub async fn score_memory(
/// Updates the graph weight (EWMA) and last_scored after each.
///
/// Returns the number of nodes scored and their (key, score) pairs.
pub async fn score_memories_incremental(
pub async fn score_memories_incremental<F, Fut>(
context: &ContextState,
max_age_secs: i64,
response_window: usize,
client: &ApiClient,
agent: &std::sync::Arc<tokio::sync::Mutex<crate::agent::Agent>>,
) -> anyhow::Result<Vec<(String, f64)>> {
mut on_score: F,
) -> anyhow::Result<usize>
where
F: FnMut(String, f64) -> Fut,
Fut: std::future::Future<Output = ()>,
{
let now = chrono::Utc::now().timestamp();
// Collect unique memory keys with their first position
@ -330,7 +335,7 @@ pub async fn score_memories_incremental(
candidates.sort_by_key(|&(_, _, last)| last);
let http = http_client();
let mut results = Vec::new();
let mut scored = 0;
let total_entries = context.conversation.entries().len();
let first_quarter = total_entries / 4;
@ -355,7 +360,8 @@ pub async fn score_memories_incremental(
dbglog!(
"[scoring] {} max:{:.3} ({} responses)", key, max_div, n_responses,
);
results.push((key.clone(), max_div));
on_score(key.clone(), max_div).await;
scored += 1;
}
Err(e) => {
dbglog!(
@ -365,7 +371,7 @@ pub async fn score_memories_incremental(
}
}
Ok(results)
Ok(scored)
}
// ── Fine-tuning scoring ─────────────────────────────────────────