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:
Kent Overstreet 2026-04-07 19:03:14 -04:00
parent edfa1c37f5
commit 818cdcc4e5
4 changed files with 399 additions and 288 deletions

View file

@ -1,120 +1,35 @@
// context_screen.rs — F2 context/debug overlay
//
// Full-screen overlay showing model info, context window breakdown,
// and runtime state. Supports tree navigation with expand/collapse.
// and runtime state. Uses SectionTree for the expand/collapse tree.
use ratatui::{
layout::Rect,
style::{Color, Modifier, Style},
style::{Color, Style},
text::Line,
widgets::{Block, Borders, Paragraph, Wrap},
widgets::{Paragraph, Wrap},
Frame,
crossterm::event::KeyCode,
};
use super::{App, ScreenView, screen_legend};
use crate::agent::context::ContextSection;
use super::widgets::{SectionTree, pane_block};
pub(crate) struct ConsciousScreen {
agent: std::sync::Arc<tokio::sync::Mutex<crate::agent::Agent>>,
scroll: u16,
selected: Option<usize>,
expanded: std::collections::HashSet<usize>,
tree: SectionTree,
}
impl ConsciousScreen {
pub fn new(agent: std::sync::Arc<tokio::sync::Mutex<crate::agent::Agent>>) -> Self {
Self { agent, scroll: 0, selected: None, expanded: std::collections::HashSet::new() }
Self { agent, tree: SectionTree::new() }
}
fn read_context_state(&self) -> Vec<ContextSection> {
fn read_context_state(&self) -> Vec<crate::agent::context::ContextSection> {
match self.agent.try_lock() {
Ok(ag) => ag.context_state_summary(),
Err(_) => Vec::new(),
}
}
fn item_count(&self, context_state: &[ContextSection]) -> usize {
fn count_section(section: &ContextSection, expanded: &std::collections::HashSet<usize>, idx: &mut usize) -> usize {
let my_idx = *idx;
*idx += 1;
let mut total = 1;
if expanded.contains(&my_idx) {
for child in &section.children {
total += count_section(child, expanded, idx);
}
}
total
}
let mut idx = 0;
let mut total = 0;
for section in context_state {
total += count_section(section, &self.expanded, &mut idx);
}
total
}
fn scroll_to_selected(&mut self, _item_count: usize) {
let header_lines = 8u16;
if let Some(sel) = self.selected {
let sel_line = header_lines + sel as u16;
if sel_line < self.scroll + 2 {
self.scroll = sel_line.saturating_sub(2);
} else if sel_line > self.scroll + 30 {
self.scroll = sel_line.saturating_sub(15);
}
}
}
fn render_section(
&self,
section: &ContextSection,
depth: usize,
lines: &mut Vec<Line>,
idx: &mut usize,
) {
let my_idx = *idx;
let selected = self.selected == Some(my_idx);
let expanded = self.expanded.contains(&my_idx);
let has_children = !section.children.is_empty();
let has_content = !section.content.is_empty();
let expandable = has_children || has_content;
let indent = " ".repeat(depth + 1);
let marker = if !expandable { " " } else if expanded { "" } else { "" };
let label = format!("{}{} {:30} {:>6} tokens", indent, marker, section.name, section.tokens);
let style = if selected {
Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
} else {
Style::default()
};
lines.push(Line::styled(label, style));
*idx += 1;
if expanded {
if has_children {
for child in &section.children {
self.render_section(child, depth + 1, lines, idx);
}
} else if has_content {
let content_indent = format!("{}", " ".repeat(depth + 1));
let content_lines: Vec<&str> = section.content.lines().collect();
let show = content_lines.len().min(50);
for line in &content_lines[..show] {
lines.push(Line::styled(
format!("{}{}", content_indent, line),
Style::default().fg(Color::DarkGray),
));
}
if content_lines.len() > 50 {
lines.push(Line::styled(
format!("{}... ({} more lines)", content_indent, content_lines.len() - 50),
Style::default().fg(Color::DarkGray),
));
}
}
}
}
}
impl ScreenView for ConsciousScreen {
@ -126,32 +41,7 @@ impl ScreenView for ConsciousScreen {
if let ratatui::crossterm::event::Event::Key(key) = event {
if key.kind != ratatui::crossterm::event::KeyEventKind::Press { continue; }
let context_state = self.read_context_state();
let item_count = self.item_count(&context_state);
match key.code {
KeyCode::Up => {
self.selected = Some(self.selected.unwrap_or(0).saturating_sub(1));
self.scroll_to_selected(item_count);
}
KeyCode::Down => {
let max = item_count.saturating_sub(1);
self.selected = Some(self.selected.map_or(0, |s| (s + 1).min(max)));
self.scroll_to_selected(item_count);
}
KeyCode::Right | KeyCode::Enter => {
if let Some(sel) = self.selected {
self.expanded.insert(sel);
}
}
KeyCode::Left => {
if let Some(sel) = self.selected {
self.expanded.remove(&sel);
}
}
KeyCode::PageUp => { self.scroll = self.scroll.saturating_sub(20); }
KeyCode::PageDown => { self.scroll += 20; }
_ => {}
}
self.tree.handle_nav(key.code, &context_state);
}
}
@ -185,10 +75,7 @@ impl ScreenView for ConsciousScreen {
));
lines.push(Line::raw(""));
let mut flat_idx = 0usize;
for section in &context_state {
self.render_section(section, 0, &mut lines, &mut flat_idx);
}
self.tree.render_sections(&context_state, &mut lines);
lines.push(Line::raw(format!(" {:23} {:>6} tokens", "────────", "──────")));
lines.push(Line::raw(format!(" {:23} {:>6} tokens", "Total", total)));
@ -207,16 +94,13 @@ impl ScreenView for ConsciousScreen {
lines.push(Line::raw(format!(" Running processes: {}", app.running_processes)));
lines.push(Line::raw(format!(" Active tools: {}", app.active_tools.lock().unwrap().len())));
let block = Block::default()
.title_top(Line::from(screen_legend()).left_aligned())
.title_top(Line::from(" context ").right_aligned())
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Cyan));
let block = pane_block("context")
.title_top(Line::from(screen_legend()).left_aligned());
let para = Paragraph::new(lines)
.block(block)
.wrap(Wrap { trim: false })
.scroll((self.scroll, 0));
.scroll((self.tree.scroll, 0));
frame.render_widget(para, area);
}