agent: don't hold agent lock across I/O

The agent lock was held for the entire duration of turn() — including
API streaming and tool dispatch awaits. This blocked the UI thread
whenever it needed the lock (render tick, compaction check, etc.),
causing 20+ second freezes.

Fix: turn() takes Arc<Mutex<Agent>> and manages locking internally.
Lock is held briefly for prepare/process phases, released during all
I/O (streaming, tool awaits, sleep retries). Also:

- check_compaction: spawns task instead of awaiting on event loop
- start_memory_scoring: already spawned, no change needed
- dispatch_tool_call_unlocked: drops lock before tool handle await
- Subconscious screen: renders all agents from state dynamically
  (no more hardcoded SUBCONSCIOUS_AGENTS list)
- Memory scoring shows n/m progress in snapshots

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
ProofOfConcept 2026-04-04 04:23:29 -04:00
parent 6fa881f811
commit fb54488f30
5 changed files with 301 additions and 269 deletions

View file

@ -11,7 +11,7 @@ use ratatui::{
Frame,
};
use super::{App, SUBCONSCIOUS_AGENTS, SCREEN_LEGEND};
use super::{App, SCREEN_LEGEND};
impl App {
pub(crate) fn draw_agents(&self, frame: &mut Frame, size: Rect) {
@ -24,7 +24,6 @@ impl App {
let mut lines: Vec<Line> = Vec::new();
let section = Style::default().fg(Color::Yellow);
let _dim = Style::default().fg(Color::DarkGray);
let hint = Style::default().fg(Color::DarkGray).add_modifier(Modifier::ITALIC);
lines.push(Line::raw(""));
@ -32,29 +31,35 @@ impl App {
lines.push(Line::styled(" (↑/↓ select, Enter/→ view log, Esc back)", hint));
lines.push(Line::raw(""));
for (i, &name) in SUBCONSCIOUS_AGENTS.iter().enumerate() {
for (i, agent) in self.agent_state.iter().enumerate() {
let selected = i == self.agent_selected;
let prefix = if selected { "" } else { " " };
let bg = if selected { Style::default().bg(Color::DarkGray) } else { Style::default() };
let agent = self.agent_state.iter().find(|a| a.name == name);
match agent.and_then(|a| a.pid) {
Some(pid) => {
let phase = agent.and_then(|a| a.phase.as_deref()).unwrap_or("?");
lines.push(Line::from(vec![
Span::styled(format!("{}{:<20}", prefix, name), bg.fg(Color::Green)),
let status = match (&agent.pid, &agent.phase) {
(Some(pid), Some(phase)) => {
vec![
Span::styled(format!("{}{:<20}", prefix, agent.name), bg.fg(Color::Green)),
Span::styled("", bg.fg(Color::Green)),
Span::styled(format!("pid {} phase: {}", pid, phase), bg),
]));
Span::styled(format!("pid {} {}", pid, phase), bg),
]
}
None => {
lines.push(Line::from(vec![
Span::styled(format!("{}{:<20}", prefix, name), bg.fg(Color::Gray)),
(None, Some(phase)) => {
// No pid but has phase — async task (e.g. memory-scoring)
vec![
Span::styled(format!("{}{:<20}", prefix, agent.name), bg.fg(Color::Cyan)),
Span::styled("", bg.fg(Color::Cyan)),
Span::styled(phase.clone(), bg),
]
}
_ => {
vec![
Span::styled(format!("{}{:<20}", prefix, agent.name), bg.fg(Color::Gray)),
Span::styled("○ idle", bg.fg(Color::DarkGray)),
]));
]
}
}
};
lines.push(Line::from(status));
}
let block = Block::default()
@ -70,8 +75,8 @@ impl App {
}
fn draw_agent_log(&self, frame: &mut Frame, size: Rect, _output_dir: &std::path::Path) {
let name = SUBCONSCIOUS_AGENTS.get(self.agent_selected).unwrap_or(&"?");
let agent = self.agent_state.iter().find(|a| a.name == *name);
let agent = self.agent_state.get(self.agent_selected);
let name = agent.map(|a| a.name.as_str()).unwrap_or("?");
let mut lines: Vec<Line> = Vec::new();
let section = Style::default().fg(Color::Yellow);
let hint = Style::default().fg(Color::DarkGray).add_modifier(Modifier::ITALIC);