Clean up mind loop: fix double locks, async agent triggers, input peek
- push_node: notify before dropping state lock instead of relocking - Mind::run: single lock for timeout + turn_active + has_input; single lock for turn_handle + complete_turn - Agent triggers (subconscious/unconscious) spawned as async tasks so they don't block the select loop - has_pending_input() peek for DMN sleep guard — don't sleep when there's user input waiting - unconscious: merge collect_results into trigger, single store load Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
0314619579
commit
d82a2ae90d
3 changed files with 49 additions and 52 deletions
|
|
@ -114,32 +114,19 @@ impl Unconscious {
|
|||
}).collect()
|
||||
}
|
||||
|
||||
/// Collect results from finished agents.
|
||||
pub async fn collect_results(&mut self) {
|
||||
for agent in &mut self.agents {
|
||||
if agent.handle.as_ref().is_some_and(|h| h.is_finished()) {
|
||||
let handle = agent.handle.take().unwrap();
|
||||
agent.last_run = Some(Instant::now());
|
||||
agent.completed += 1;
|
||||
|
||||
match handle.await {
|
||||
Ok((_auto, Ok(text))) => {
|
||||
let preview = &text[..text.floor_char_boundary(text.len().min(100))];
|
||||
dbglog!("[unconscious] {} completed: {}", agent.name, preview);
|
||||
}
|
||||
Ok((_auto, Err(e))) => {
|
||||
dbglog!("[unconscious] {} failed: {}", agent.name, e);
|
||||
}
|
||||
Err(e) => {
|
||||
dbglog!("[unconscious] {} panicked: {}", agent.name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Trigger agents that are due to run.
|
||||
pub fn trigger(&mut self) {
|
||||
// Reap finished agents
|
||||
for agent in &mut self.agents {
|
||||
if agent.handle.as_ref().is_some_and(|h| h.is_finished()) {
|
||||
agent.last_run = Some(Instant::now());
|
||||
agent.completed += 1;
|
||||
dbglog!("[unconscious] {} completed ({}/{})",
|
||||
agent.name, agent.completed, agent.budget);
|
||||
agent.handle = None;
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh plan every 30 minutes (or on first call)
|
||||
let should_refresh = self.last_plan_refresh
|
||||
.map(|t| t.elapsed() > Duration::from_secs(1800))
|
||||
|
|
@ -187,8 +174,8 @@ impl Unconscious {
|
|||
.collect()
|
||||
};
|
||||
|
||||
// Run query and resolve placeholders synchronously
|
||||
let store = match crate::store::Store::load() {
|
||||
// Run query, resolve placeholders, record visits
|
||||
let mut store = match crate::store::Store::load() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
dbglog!("[unconscious] store load failed: {}", e);
|
||||
|
|
@ -196,10 +183,7 @@ impl Unconscious {
|
|||
}
|
||||
};
|
||||
|
||||
// Track which nodes other running agents are working on
|
||||
// to avoid concurrent collisions
|
||||
let exclude: std::collections::HashSet<String> = std::collections::HashSet::new();
|
||||
|
||||
let batch = match defs::run_agent(
|
||||
&store, &def, def.count.unwrap_or(5), &exclude,
|
||||
) {
|
||||
|
|
@ -210,13 +194,8 @@ impl Unconscious {
|
|||
}
|
||||
};
|
||||
|
||||
// Record visits
|
||||
if !batch.node_keys.is_empty() {
|
||||
let mut store_mut = match crate::store::Store::load() {
|
||||
Ok(s) => s,
|
||||
Err(_) => return,
|
||||
};
|
||||
store_mut.record_agent_visits(&batch.node_keys, &name).ok();
|
||||
store.record_agent_visits(&batch.node_keys, &name).ok();
|
||||
}
|
||||
|
||||
let steps: Vec<AutoStep> = batch.steps.iter().map(|s| AutoStep {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue