Fix bounds check panic and batched lock in collect_results

- subconscious.rs: use .get(fork_point..) instead of direct slice
  to avoid panic when fork_point > entries.len()
- dmn.rs: batch all output injections (surface, reflection, thalamus)
  under a single agent lock acquisition instead of three separate ones
- dmn.rs: use Store::cached() instead of Store::load() when rendering
  surfaced memories
- Add scoring persistence analysis notes

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
Kent Overstreet 2026-04-07 03:49:49 -04:00
parent 03d2d070f9
commit 0df5ec11d1
3 changed files with 87 additions and 31 deletions

View file

@ -416,42 +416,52 @@ impl Subconscious {
.collect();
}
if let Some(surface_str) = outputs.get("surface") {
// Inject all outputs into the conscious agent under one lock
let has_outputs = outputs.contains_key("surface")
|| outputs.contains_key("reflection")
|| outputs.contains_key("thalamus");
if has_outputs {
let mut ag = agent.lock().await;
for key in surface_str.lines().map(|l| l.trim()).filter(|l| !l.is_empty()) {
if let Some(rendered) = crate::cli::node::render_node(
&crate::store::Store::load().unwrap_or_default(), key,
) {
let mut msg = crate::agent::api::types::Message::user(format!(
"<system-reminder>\n--- {} (surfaced) ---\n{}\n</system-reminder>",
key, rendered,
));
msg.stamp();
ag.push_entry(ConversationEntry::Memory {
key: key.to_string(), message: msg, score: None,
});
if let Some(surface_str) = outputs.get("surface") {
let store = crate::store::Store::cached().await.ok();
let store_guard = match &store {
Some(s) => Some(s.lock().await),
None => None,
};
for key in surface_str.lines().map(|l| l.trim()).filter(|l| !l.is_empty()) {
let rendered = store_guard.as_ref()
.and_then(|s| crate::cli::node::render_node(s, key));
if let Some(rendered) = rendered {
let mut msg = crate::agent::api::types::Message::user(format!(
"<system-reminder>\n--- {} (surfaced) ---\n{}\n</system-reminder>",
key, rendered,
));
msg.stamp();
ag.push_entry(ConversationEntry::Memory {
key: key.to_string(), message: msg, score: None,
});
}
}
}
}
if let Some(reflection) = outputs.get("reflection") {
if !reflection.trim().is_empty() {
let mut ag = agent.lock().await;
ag.push_message(crate::agent::api::types::Message::user(format!(
"<system-reminder>\n--- subconscious reflection ---\n{}\n</system-reminder>",
reflection.trim(),
)));
if let Some(reflection) = outputs.get("reflection") {
if !reflection.trim().is_empty() {
ag.push_message(crate::agent::api::types::Message::user(format!(
"<system-reminder>\n--- subconscious reflection ---\n{}\n</system-reminder>",
reflection.trim(),
)));
}
}
}
if let Some(nudge) = outputs.get("thalamus") {
let nudge = nudge.trim();
if !nudge.is_empty() && nudge != "ok" {
let mut ag = agent.lock().await;
ag.push_message(crate::agent::api::types::Message::user(format!(
"<system-reminder>\n--- thalamus ---\n{}\n</system-reminder>",
nudge,
)));
if let Some(nudge) = outputs.get("thalamus") {
let nudge = nudge.trim();
if !nudge.is_empty() && nudge != "ok" {
ag.push_message(crate::agent::api::types::Message::user(format!(
"<system-reminder>\n--- thalamus ---\n{}\n</system-reminder>",
nudge,
)));
}
}
}

View file

@ -166,7 +166,7 @@ impl SubconsciousScreen {
// Read entries from the forked agent (from fork point onward)
let entries: Vec<ConversationEntry> = snap.forked_agent.as_ref()
.and_then(|agent| agent.try_lock().ok())
.map(|ag| ag.context.entries[snap.fork_point..].to_vec())
.map(|ag| ag.context.entries.get(snap.fork_point..).unwrap_or(&[]).to_vec())
.unwrap_or_default();
if entries.is_empty() {