Three-pane subconscious debug screen with shared widgets
New layout for F3 screen: - Top-left: agent list using ratatui List widget with ListState - Middle-left: expandable agent state (persistent across runs) - Bottom-left: memory store activity by provenance, walked keys - Right: context tree from fork point, reusing SectionTree Tab/Shift-Tab cycles focus clockwise between panes; focused pane gets white border. Each pane handles its own input when focused. Extracted user/widgets.rs: - SectionTree (moved from mod.rs): expand/collapse tree for ContextSection - pane_block_focused(): standard bordered block with focus indicator - format_age()/format_ts_age(): shared duration formatting Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
edfa1c37f5
commit
818cdcc4e5
4 changed files with 399 additions and 288 deletions
|
|
@ -1,27 +1,52 @@
|
|||
// subconscious_screen.rs — F3 subconscious agent overlay
|
||||
//
|
||||
// Three-pane layout:
|
||||
// Top-left: Agent list (↑/↓ select)
|
||||
// Bottom-left: Detail — outputs from selected agent's last run
|
||||
// Right: Context tree from fork point (→/Enter expand, ← collapse)
|
||||
|
||||
use ratatui::{
|
||||
layout::Rect,
|
||||
layout::{Constraint, Layout, Rect},
|
||||
style::{Color, Modifier, Style},
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders, Paragraph, Wrap},
|
||||
widgets::{List, ListItem, ListState, Paragraph, Wrap},
|
||||
Frame,
|
||||
crossterm::event::KeyCode,
|
||||
};
|
||||
|
||||
use super::{App, ScreenView, screen_legend};
|
||||
use crate::agent::context::ConversationEntry;
|
||||
use crate::agent::api::Role;
|
||||
use super::widgets::{SectionTree, pane_block_focused, format_age, format_ts_age};
|
||||
use crate::agent::context::ContextSection;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Pane { Agents, Outputs, History, Context }
|
||||
|
||||
// Clockwise: top-left → right → bottom-left → middle-left
|
||||
const PANE_ORDER: &[Pane] = &[Pane::Agents, Pane::Context, Pane::History, Pane::Outputs];
|
||||
|
||||
pub(crate) struct SubconsciousScreen {
|
||||
selected: usize,
|
||||
detail: bool,
|
||||
scroll: u16,
|
||||
focus: Pane,
|
||||
list_state: ListState,
|
||||
output_tree: SectionTree,
|
||||
context_tree: SectionTree,
|
||||
history_scroll: u16,
|
||||
}
|
||||
|
||||
impl SubconsciousScreen {
|
||||
pub fn new() -> Self {
|
||||
Self { selected: 0, detail: false, scroll: 0 }
|
||||
let mut list_state = ListState::default();
|
||||
list_state.select(Some(0));
|
||||
Self {
|
||||
focus: Pane::Agents,
|
||||
list_state,
|
||||
output_tree: SectionTree::new(),
|
||||
context_tree: SectionTree::new(),
|
||||
history_scroll: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn selected(&self) -> usize {
|
||||
self.list_state.selected().unwrap_or(0)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -30,206 +55,232 @@ impl ScreenView for SubconsciousScreen {
|
|||
|
||||
fn tick(&mut self, frame: &mut Frame, area: Rect,
|
||||
events: &[ratatui::crossterm::event::Event], app: &mut App) {
|
||||
let context_sections = self.read_sections(app);
|
||||
let output_sections = self.output_sections(app);
|
||||
|
||||
for event in events {
|
||||
if let ratatui::crossterm::event::Event::Key(key) = event {
|
||||
if key.kind != ratatui::crossterm::event::KeyEventKind::Press { continue; }
|
||||
match key.code {
|
||||
KeyCode::Up if !self.detail => {
|
||||
self.selected = self.selected.saturating_sub(1);
|
||||
KeyCode::Tab => {
|
||||
let idx = PANE_ORDER.iter().position(|p| *p == self.focus).unwrap_or(0);
|
||||
self.focus = PANE_ORDER[(idx + 1) % PANE_ORDER.len()];
|
||||
}
|
||||
KeyCode::Down if !self.detail => {
|
||||
self.selected = (self.selected + 1)
|
||||
.min(app.agent_state.len().saturating_sub(1));
|
||||
KeyCode::BackTab => {
|
||||
let idx = PANE_ORDER.iter().position(|p| *p == self.focus).unwrap_or(0);
|
||||
self.focus = PANE_ORDER[(idx + PANE_ORDER.len() - 1) % PANE_ORDER.len()];
|
||||
}
|
||||
KeyCode::Enter | KeyCode::Right if !self.detail => {
|
||||
self.detail = true;
|
||||
self.scroll = 0;
|
||||
code => match self.focus {
|
||||
Pane::Agents => match code {
|
||||
KeyCode::Up => {
|
||||
self.list_state.select_previous();
|
||||
self.reset_pane_state();
|
||||
}
|
||||
KeyCode::Down => {
|
||||
self.list_state.select_next();
|
||||
self.reset_pane_state();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Pane::Outputs => self.output_tree.handle_nav(code, &output_sections),
|
||||
Pane::History => match code {
|
||||
KeyCode::Up => self.history_scroll = self.history_scroll.saturating_sub(3),
|
||||
KeyCode::Down => self.history_scroll += 3,
|
||||
KeyCode::PageUp => self.history_scroll = self.history_scroll.saturating_sub(20),
|
||||
KeyCode::PageDown => self.history_scroll += 20,
|
||||
_ => {}
|
||||
}
|
||||
Pane::Context => self.context_tree.handle_nav(code, &context_sections),
|
||||
}
|
||||
KeyCode::Esc | KeyCode::Left if self.detail => {
|
||||
self.detail = false;
|
||||
}
|
||||
KeyCode::Up if self.detail => {
|
||||
self.scroll = self.scroll.saturating_sub(3);
|
||||
}
|
||||
KeyCode::Down if self.detail => {
|
||||
self.scroll += 3;
|
||||
}
|
||||
KeyCode::PageUp => { self.scroll = self.scroll.saturating_sub(20); }
|
||||
KeyCode::PageDown => { self.scroll += 20; }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if self.detail {
|
||||
self.draw_detail(frame, area, app);
|
||||
} else {
|
||||
self.draw_list(frame, area, app);
|
||||
}
|
||||
// Layout: left column (38%) | right column (62%)
|
||||
let [left, right] = Layout::horizontal([
|
||||
Constraint::Percentage(38),
|
||||
Constraint::Percentage(62),
|
||||
]).areas(area);
|
||||
|
||||
// Left column: agent list (top) | outputs (middle) | history (bottom, main)
|
||||
let agent_count = app.agent_state.len().max(1) as u16;
|
||||
let list_height = (agent_count + 2).min(left.height / 4);
|
||||
let output_lines = app.agent_state.get(self.selected())
|
||||
.map(|s| s.state.values().map(|v| v.lines().count() + 1).sum::<usize>())
|
||||
.unwrap_or(0);
|
||||
let output_height = (output_lines as u16 + 2).min(left.height / 4).max(3);
|
||||
let [list_area, output_area, history_area] = Layout::vertical([
|
||||
Constraint::Length(list_height),
|
||||
Constraint::Length(output_height),
|
||||
Constraint::Min(5),
|
||||
]).areas(left);
|
||||
|
||||
self.draw_list(frame, list_area, app);
|
||||
self.draw_outputs(frame, output_area, app);
|
||||
self.draw_history(frame, history_area, app);
|
||||
self.draw_context(frame, right, &context_sections, app);
|
||||
}
|
||||
}
|
||||
|
||||
impl SubconsciousScreen {
|
||||
fn draw_list(&self, frame: &mut Frame, area: Rect, app: &App) {
|
||||
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);
|
||||
fn reset_pane_state(&mut self) {
|
||||
self.output_tree = SectionTree::new();
|
||||
self.context_tree = SectionTree::new();
|
||||
self.history_scroll = 0;
|
||||
}
|
||||
|
||||
lines.push(Line::raw(""));
|
||||
let walked = app.walked_count;
|
||||
lines.push(Line::styled(
|
||||
format!("── Subconscious Agents ── walked: {}", walked), section));
|
||||
lines.push(Line::styled(" (↑/↓ select, Enter view log)", hint));
|
||||
lines.push(Line::raw(""));
|
||||
fn output_sections(&self, app: &App) -> Vec<ContextSection> {
|
||||
let snap = match app.agent_state.get(self.selected()) {
|
||||
Some(s) => s,
|
||||
None => return Vec::new(),
|
||||
};
|
||||
snap.state.iter().map(|(key, val)| {
|
||||
ContextSection {
|
||||
name: key.clone(),
|
||||
tokens: 0,
|
||||
content: val.clone(),
|
||||
children: Vec::new(),
|
||||
}
|
||||
}).collect()
|
||||
}
|
||||
|
||||
if app.agent_state.is_empty() {
|
||||
lines.push(Line::styled(" (no agents loaded)", hint));
|
||||
}
|
||||
fn read_sections(&self, app: &App) -> Vec<ContextSection> {
|
||||
let snap = match app.agent_state.get(self.selected()) {
|
||||
Some(s) => s,
|
||||
None => return Vec::new(),
|
||||
};
|
||||
snap.forked_agent.as_ref()
|
||||
.and_then(|agent| agent.try_lock().ok())
|
||||
.map(|ag| ag.conversation_sections_from(snap.fork_point))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
for (i, snap) in app.agent_state.iter().enumerate() {
|
||||
let selected = i == self.selected;
|
||||
let prefix = if selected { "▸ " } else { " " };
|
||||
let bg = if selected {
|
||||
Style::default().bg(Color::DarkGray)
|
||||
} else {
|
||||
Style::default()
|
||||
};
|
||||
|
||||
let status_spans = if snap.running {
|
||||
vec![
|
||||
fn draw_list(&mut self, frame: &mut Frame, area: Rect, app: &App) {
|
||||
let items: Vec<ListItem> = app.agent_state.iter().map(|snap| {
|
||||
if snap.running {
|
||||
ListItem::from(Line::from(vec![
|
||||
Span::styled(&snap.name, Style::default().fg(Color::Green)),
|
||||
Span::styled(" ● ", Style::default().fg(Color::Green)),
|
||||
Span::styled(
|
||||
format!("{}{:<30}", prefix, snap.name),
|
||||
bg.fg(Color::Green),
|
||||
format!("p:{} t:{}", snap.current_phase, snap.turn),
|
||||
Style::default().fg(Color::DarkGray),
|
||||
),
|
||||
Span::styled("● ", bg.fg(Color::Green)),
|
||||
Span::styled(
|
||||
format!("phase: {} turn: {}", snap.current_phase, snap.turn),
|
||||
bg,
|
||||
),
|
||||
]
|
||||
]))
|
||||
} else {
|
||||
let ago = snap.last_run_secs_ago
|
||||
.map(|s| {
|
||||
if s < 60.0 { format!("{:.0}s ago", s) }
|
||||
else if s < 3600.0 { format!("{:.0}m ago", s / 60.0) }
|
||||
else { format!("{:.1}h ago", s / 3600.0) }
|
||||
})
|
||||
.unwrap_or_else(|| "never".to_string());
|
||||
.map(|s| format_age(s))
|
||||
.unwrap_or_else(|| "—".to_string());
|
||||
let entries = snap.forked_agent.as_ref()
|
||||
.and_then(|a| a.try_lock().ok())
|
||||
.map(|ag| ag.context.entries.len().saturating_sub(snap.fork_point))
|
||||
.unwrap_or(0);
|
||||
vec![
|
||||
ListItem::from(Line::from(vec![
|
||||
Span::styled(&snap.name, Style::default().fg(Color::Gray)),
|
||||
Span::styled(" ○ ", Style::default().fg(Color::DarkGray)),
|
||||
Span::styled(
|
||||
format!("{}{:<30}", prefix, snap.name),
|
||||
bg.fg(Color::Gray),
|
||||
format!("{} {}e", ago, entries),
|
||||
Style::default().fg(Color::DarkGray),
|
||||
),
|
||||
Span::styled("○ ", bg.fg(Color::DarkGray)),
|
||||
Span::styled(
|
||||
format!("idle last: {} entries: {}",
|
||||
ago, entries),
|
||||
bg.fg(Color::DarkGray),
|
||||
),
|
||||
]
|
||||
};
|
||||
lines.push(Line::from(status_spans));
|
||||
]))
|
||||
}
|
||||
}).collect();
|
||||
|
||||
let list = List::new(items)
|
||||
.block(pane_block_focused("agents", self.focus == Pane::Agents)
|
||||
.title_top(Line::from(screen_legend()).left_aligned()))
|
||||
.highlight_symbol("▸ ")
|
||||
.highlight_style(Style::default().bg(Color::DarkGray));
|
||||
|
||||
frame.render_stateful_widget(list, area, &mut self.list_state);
|
||||
}
|
||||
|
||||
fn draw_outputs(&self, frame: &mut Frame, area: Rect, app: &App) {
|
||||
let sections = self.output_sections(app);
|
||||
let mut lines: Vec<Line> = Vec::new();
|
||||
|
||||
if sections.is_empty() {
|
||||
let dim = Style::default().fg(Color::DarkGray);
|
||||
let snap = app.agent_state.get(self.selected());
|
||||
let msg = if snap.is_some_and(|s| s.running) { "(running...)" } else { "—" };
|
||||
lines.push(Line::styled(format!(" {}", msg), dim));
|
||||
} else {
|
||||
self.output_tree.render_sections(§ions, &mut lines);
|
||||
}
|
||||
|
||||
let block = Block::default()
|
||||
.title_top(Line::from(screen_legend()).left_aligned())
|
||||
.title_top(Line::from(" subconscious ").right_aligned())
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(Color::Cyan));
|
||||
|
||||
let para = Paragraph::new(lines)
|
||||
.block(block)
|
||||
.block(pane_block_focused("state", self.focus == Pane::Outputs))
|
||||
.wrap(Wrap { trim: false })
|
||||
.scroll((self.scroll, 0));
|
||||
.scroll((self.output_tree.scroll, 0));
|
||||
frame.render_widget(para, area);
|
||||
}
|
||||
|
||||
fn draw_detail(&self, frame: &mut Frame, area: Rect, app: &App) {
|
||||
let snap = match app.agent_state.get(self.selected) {
|
||||
Some(s) => s,
|
||||
None => return,
|
||||
};
|
||||
fn draw_history(&self, frame: &mut Frame, area: Rect, app: &App) {
|
||||
let dim = Style::default().fg(Color::DarkGray);
|
||||
let key_style = Style::default().fg(Color::Yellow);
|
||||
|
||||
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);
|
||||
let mut title = "memory store activity".to_string();
|
||||
|
||||
lines.push(Line::raw(""));
|
||||
lines.push(Line::styled(format!("── {} ──", snap.name), section));
|
||||
lines.push(Line::styled(" (Esc/← back, ↑/↓/PgUp/PgDn scroll)", hint));
|
||||
lines.push(Line::raw(""));
|
||||
if let Some(snap) = app.agent_state.get(self.selected()) {
|
||||
let short_name = snap.name.strip_prefix("subconscious-").unwrap_or(&snap.name);
|
||||
title = format!("{} store activity", short_name);
|
||||
|
||||
// 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.get(snap.fork_point..).unwrap_or(&[]).to_vec())
|
||||
.unwrap_or_default();
|
||||
|
||||
if entries.is_empty() {
|
||||
lines.push(Line::styled(" (no run data)", hint));
|
||||
}
|
||||
|
||||
for entry in &entries {
|
||||
if entry.is_log() {
|
||||
if let ConversationEntry::Log(text) = entry {
|
||||
lines.push(Line::styled(
|
||||
format!(" [log] {}", text),
|
||||
Style::default().fg(Color::DarkGray),
|
||||
));
|
||||
if snap.history.is_empty() {
|
||||
lines.push(Line::styled(" (no store activity)", dim));
|
||||
} else {
|
||||
for (key, ts) in &snap.history {
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(format!(" {:>6} ", format_ts_age(*ts)), dim),
|
||||
Span::styled(key.as_str(), key_style),
|
||||
]));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
let msg = entry.message();
|
||||
let (role_str, role_color) = match msg.role {
|
||||
Role::User => ("user", Color::Cyan),
|
||||
Role::Assistant => ("assistant", Color::Reset),
|
||||
Role::Tool => ("tool", Color::DarkGray),
|
||||
Role::System => ("system", Color::Yellow),
|
||||
};
|
||||
|
||||
let text = msg.content_text();
|
||||
let tool_info = msg.tool_calls.as_ref().map(|tc| {
|
||||
tc.iter().map(|c| c.function.name.as_str())
|
||||
.collect::<Vec<_>>().join(", ")
|
||||
});
|
||||
|
||||
let header = match &tool_info {
|
||||
Some(tools) => format!(" [{} → {}]", role_str, tools),
|
||||
None => format!(" [{}]", role_str),
|
||||
};
|
||||
lines.push(Line::styled(header, Style::default().fg(role_color)));
|
||||
|
||||
if !text.is_empty() {
|
||||
for line in text.lines().take(20) {
|
||||
lines.push(Line::styled(
|
||||
format!(" {}", line),
|
||||
Style::default().fg(Color::Gray),
|
||||
));
|
||||
}
|
||||
if text.lines().count() > 20 {
|
||||
lines.push(Line::styled(
|
||||
format!(" ... ({} more lines)", text.lines().count() - 20),
|
||||
hint,
|
||||
));
|
||||
if !snap.walked.is_empty() {
|
||||
lines.push(Line::raw(""));
|
||||
lines.push(Line::styled(
|
||||
format!(" walked ({}):", snap.walked.len()),
|
||||
Style::default().fg(Color::Cyan),
|
||||
));
|
||||
for key in &snap.walked {
|
||||
lines.push(Line::styled(format!(" {}", key), dim));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let block = Block::default()
|
||||
.title_top(Line::from(screen_legend()).left_aligned())
|
||||
.title_top(Line::from(format!(" {} ", snap.name)).right_aligned())
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(Color::Cyan));
|
||||
|
||||
let para = Paragraph::new(lines)
|
||||
.block(block)
|
||||
.block(pane_block_focused(&title, self.focus == Pane::History))
|
||||
.wrap(Wrap { trim: false })
|
||||
.scroll((self.scroll, 0));
|
||||
.scroll((self.history_scroll, 0));
|
||||
frame.render_widget(para, area);
|
||||
}
|
||||
|
||||
fn draw_context(
|
||||
&self,
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
sections: &[ContextSection],
|
||||
app: &App,
|
||||
) {
|
||||
let mut lines: Vec<Line> = Vec::new();
|
||||
|
||||
if sections.is_empty() {
|
||||
lines.push(Line::styled(
|
||||
" (no conversation data)",
|
||||
Style::default().fg(Color::DarkGray).add_modifier(Modifier::ITALIC),
|
||||
));
|
||||
} else {
|
||||
self.context_tree.render_sections(sections, &mut lines);
|
||||
}
|
||||
|
||||
let title = app.agent_state.get(self.selected())
|
||||
.map(|s| s.name.as_str())
|
||||
.unwrap_or("—");
|
||||
|
||||
let para = Paragraph::new(lines)
|
||||
.block(pane_block_focused(title, self.focus == Pane::Context))
|
||||
.wrap(Wrap { trim: false })
|
||||
.scroll((self.context_tree.scroll, 0));
|
||||
frame.render_widget(para, area);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue