Fix input blocked during scoring: release agent lock before disk write
The scoring callback was holding the agent lock while doing a synchronous file write (save_memory_scores). This blocked the event loop from acquiring the lock to process user input. Fix: collect the scores snapshot while holding the lock, drop the lock, then write to disk. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
1be16b9f7b
commit
7c5fddcb19
1 changed files with 22 additions and 13 deletions
|
|
@ -56,9 +56,9 @@ fn load_memory_scores(section: &mut ContextSection, path: &std::path::Path) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Save all memory scores to disk.
|
/// Collect scored memory keys from conversation entries.
|
||||||
fn save_memory_scores(section: &ContextSection, path: &std::path::Path) {
|
fn collect_memory_scores(section: &ContextSection) -> std::collections::BTreeMap<String, f64> {
|
||||||
let scores: std::collections::BTreeMap<String, f64> = section.entries().iter()
|
section.entries().iter()
|
||||||
.filter_map(|ce| {
|
.filter_map(|ce| {
|
||||||
if let ConversationEntry::Memory { key, score: Some(s), .. } = &ce.entry {
|
if let ConversationEntry::Memory { key, score: Some(s), .. } = &ce.entry {
|
||||||
Some((key.clone(), *s))
|
Some((key.clone(), *s))
|
||||||
|
|
@ -66,8 +66,12 @@ fn save_memory_scores(section: &ContextSection, path: &std::path::Path) {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect()
|
||||||
if let Ok(json) = serde_json::to_string_pretty(&scores) {
|
}
|
||||||
|
|
||||||
|
/// Save memory scores to disk.
|
||||||
|
fn save_memory_scores(scores: &std::collections::BTreeMap<String, f64>, path: &std::path::Path) {
|
||||||
|
if let Ok(json) = serde_json::to_string_pretty(scores) {
|
||||||
let _ = std::fs::write(path, json);
|
let _ = std::fs::write(path, json);
|
||||||
dbglog!("[scoring] saved {} scores to {}", scores.len(), path.display());
|
dbglog!("[scoring] saved {} scores to {}", scores.len(), path.display());
|
||||||
}
|
}
|
||||||
|
|
@ -402,16 +406,21 @@ impl Mind {
|
||||||
let agent = agent.clone();
|
let agent = agent.clone();
|
||||||
let path = scores_path.clone();
|
let path = scores_path.clone();
|
||||||
async move {
|
async move {
|
||||||
let mut ag = agent.lock().await;
|
let scores_snapshot = {
|
||||||
for i in 0..ag.context.conversation.len() {
|
let mut ag = agent.lock().await;
|
||||||
if let ConversationEntry::Memory { key: k, .. } = &ag.context.conversation.entries()[i].entry {
|
for i in 0..ag.context.conversation.len() {
|
||||||
if *k == key {
|
if let ConversationEntry::Memory { key: k, .. } = &ag.context.conversation.entries()[i].entry {
|
||||||
ag.context.conversation.set_score(i, Some(score));
|
if *k == key {
|
||||||
|
ag.context.conversation.set_score(i, Some(score));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
ag.changed.notify_one();
|
||||||
save_memory_scores(&ag.context.conversation, &path);
|
// Snapshot scores while we have the lock
|
||||||
ag.changed.notify_one();
|
collect_memory_scores(&ag.context.conversation)
|
||||||
|
};
|
||||||
|
// Write to disk after releasing the lock
|
||||||
|
save_memory_scores(&scores_snapshot, &path);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
).await;
|
).await;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue