Kill publish_context_state() — screens lock the agent directly

F1 and F2 screens now call agent.context_state_summary() directly
via try_lock/lock instead of reading from a shared RwLock cache.
Removes SharedContextState, publish_context_state(), and
publish_context_state_with_scores().

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
Kent Overstreet 2026-04-07 03:03:24 -04:00
parent 48c843234d
commit 04e260c081
6 changed files with 30 additions and 64 deletions

View file

@ -863,7 +863,7 @@ impl ScreenView for InteractScreen {
agent.expire_activities();
app.status.prompt_tokens = agent.last_prompt_tokens();
app.status.model = agent.model().to_string();
let sections = agent.shared_context.read().map(|s| s.clone()).unwrap_or_default();
let sections = agent.context_state_summary(None);
app.status.context_budget = crate::agent::context::sections_budget_string(&sections);
app.activity = agent.activities.last()
.map(|a| a.label.clone())

View file

@ -16,18 +16,22 @@ use super::{App, ScreenView, screen_legend};
use crate::agent::context::ContextSection;
pub(crate) struct ConsciousScreen {
agent: std::sync::Arc<tokio::sync::Mutex<crate::agent::Agent>>,
scroll: u16,
selected: Option<usize>,
expanded: std::collections::HashSet<usize>,
}
impl ConsciousScreen {
pub fn new() -> Self {
Self { scroll: 0, selected: None, expanded: std::collections::HashSet::new() }
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() }
}
fn read_context_state(&self, app: &App) -> Vec<ContextSection> {
app.shared_context.read().map_or_else(|_| Vec::new(), |s| s.clone())
fn read_context_state(&self) -> Vec<ContextSection> {
match self.agent.try_lock() {
Ok(ag) => ag.context_state_summary(None),
Err(_) => Vec::new(),
}
}
fn item_count(&self, context_state: &[ContextSection]) -> usize {
@ -121,7 +125,7 @@ impl ScreenView for ConsciousScreen {
for event in events {
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(app);
let context_state = self.read_context_state();
let item_count = self.item_count(&context_state);
match key.code {
@ -171,7 +175,7 @@ impl ScreenView for ConsciousScreen {
if !app.status.context_budget.is_empty() {
lines.push(Line::raw(format!(" Budget: {}", app.status.context_budget)));
}
let context_state = self.read_context_state(app);
let context_state = self.read_context_state();
if !context_state.is_empty() {
let total: usize = context_state.iter().map(|s| s.tokens).sum();
lines.push(Line::raw(""));

View file

@ -27,7 +27,6 @@ use ratatui::{
};
use std::io;
use crate::agent::context::SharedContextState;
/// Status info for the bottom status bar.
#[derive(Debug, Clone)]
@ -126,7 +125,6 @@ pub struct App {
pub should_quit: bool,
pub submitted: Vec<String>,
pub(crate) context_info: Option<ContextInfo>,
pub(crate) shared_context: SharedContextState,
pub(crate) agent_state: Vec<crate::mind::SubconsciousSnapshot>,
pub(crate) walked_count: usize,
pub(crate) channel_status: Vec<ChannelStatus>,
@ -134,7 +132,7 @@ pub struct App {
}
impl App {
pub fn new(model: String, shared_context: SharedContextState, active_tools: crate::agent::tools::SharedActiveTools) -> Self {
pub fn new(model: String, active_tools: crate::agent::tools::SharedActiveTools) -> Self {
Self {
status: StatusInfo {
dmn_state: "resting".into(), dmn_turns: 0, dmn_max_turns: 20,
@ -149,7 +147,7 @@ impl App {
top_k: 20,
active_tools,
should_quit: false, submitted: Vec::new(),
context_info: None, shared_context,
context_info: None,
agent_state: Vec::new(),
walked_count: 0,
channel_status: Vec::new(), idle_info: None,
@ -202,7 +200,6 @@ pub async fn start(cli: crate::user::CliArgs) -> Result<()> {
let mind = crate::mind::Mind::new(config, turn_tx);
let shared_context = mind.agent.lock().await.shared_context.clone();
let shared_active_tools = mind.agent.lock().await.active_tools.clone();
let mut result = Ok(());
@ -216,7 +213,7 @@ pub async fn start(cli: crate::user::CliArgs) -> Result<()> {
// UI event loop
s.spawn(async {
result = run(
tui::App::new(String::new(), shared_context, shared_active_tools),
tui::App::new(String::new(), shared_active_tools),
&mind, mind_tx,
).await;
});
@ -337,7 +334,7 @@ pub async fn run(
Box::new(crate::user::chat::InteractScreen::new(
mind.agent.clone(), mind.shared.clone(), mind_tx.clone(),
)),
Box::new(crate::user::context::ConsciousScreen::new()),
Box::new(crate::user::context::ConsciousScreen::new(mind.agent.clone())),
Box::new(crate::user::subconscious::SubconsciousScreen::new()),
Box::new(crate::user::unconscious::UnconsciousScreen::new()),
Box::new(crate::user::thalamus::ThalamusScreen::new()),
@ -455,12 +452,6 @@ pub async fn run(
let idx = n as usize;
if idx >= 1 && idx <= screens.len() {
active_screen = idx;
// Refresh context state when switching to the conscious screen
if idx == 2 {
if let Ok(mut ag) = agent.try_lock() {
ag.publish_context_state();
}
}
}
} else if key.modifiers.contains(KeyModifiers::CONTROL) {
match key.code {