Files
codex/codex-rs/tui/src/chatwidget/hook_lifecycle.rs
T
Eric Traut 9d57be71ba Separate TUI preferences from server configuration (#42202)
## Why

TUI preferences and persistence paths belong to the local client, while thread
configuration and account requirements may come from the active app server.
Keeping both in the same resolved `Config` can replace live local preferences
when a thread is resumed, forked, reconnected, or switched.

## What changed

- Add `LocalSettings` as the TUI-owned source for UI, history, notice, and local
  persistence settings, and preserve it across session lifecycle transitions.
- Write preference changes to the selected user config file and reload local
  settings from disk when changing roots.
- Use the app server's account response for authentication-dependent onboarding
  and status UI instead of the local model-provider configuration.
- Retain platform family and OS metadata reported by remote app servers.

## Testing

- Cover local setting defaults, overrides, persistence destinations, reloads,
  and preservation across widget replacement and root switching.
- Cover remote platform metadata and server-controlled authentication UI.

GitOrigin-RevId: dbc2965fb1d6cbecb3f973f6133f3dcd4467b753
2026-09-02 04:20:38 +00:00

153 lines
5.1 KiB
Rust

//! Hook run lifecycle handling for `ChatWidget`.
//!
//! This module keeps hook timers, compact activity-row progress, and durable
//! hook completion output together.
use super::*;
impl ChatWidget {
/// Drop transient live hook status without flushing it into history.
pub(super) fn clear_active_hook_cell(&mut self) {
self.bottom_pane.set_hook_status_message(/*message*/ None);
if self.active_hook_cell.take().is_some() {
self.bump_active_cell_revision();
self.request_pending_usage_output_insertion();
}
}
pub(super) fn on_hook_started(&mut self, run: codex_app_server_protocol::HookRunSummary) {
self.flush_answer_stream_with_separator();
self.flush_completed_hook_output();
match self.active_hook_cell.as_mut() {
Some(cell) => {
cell.start_run(run);
self.bump_active_cell_revision();
}
None => {
self.active_hook_cell = Some(history_cell::new_active_hook_cell(run));
self.bump_active_cell_revision();
}
}
self.sync_hook_status_message();
self.request_redraw();
}
pub(super) fn on_hook_completed(
&mut self,
completed: codex_app_server_protocol::HookRunSummary,
) {
let completed_existing_run = self
.active_hook_cell
.as_mut()
.map(|cell| cell.complete_run(completed.clone()))
.unwrap_or(false);
if completed_existing_run {
self.bump_active_cell_revision();
} else {
match self.active_hook_cell.as_mut() {
Some(cell) => {
cell.add_completed_run(completed);
self.bump_active_cell_revision();
}
None => {
let cell = history_cell::new_completed_hook_cell(completed);
if !cell.is_empty() {
self.active_hook_cell = Some(cell);
self.bump_active_cell_revision();
}
}
}
}
self.flush_completed_hook_output();
self.finish_active_hook_cell_if_idle();
self.sync_hook_status_message();
self.request_redraw();
}
pub(super) fn flush_completed_hook_output(&mut self) {
let Some(completed_cell) = self
.active_hook_cell
.as_mut()
.and_then(HookCell::take_completed_persistent_runs)
else {
return;
};
let active_cell_is_empty = self
.active_hook_cell
.as_ref()
.is_some_and(HookCell::is_empty);
if active_cell_is_empty {
self.active_hook_cell = None;
}
self.flush_completed_command_activity();
self.bump_active_cell_revision();
self.transcript.needs_final_message_separator = true;
self.app_event_tx
.send(AppEvent::InsertHistoryCell(Box::new(completed_cell)));
self.request_pending_usage_output_insertion();
}
pub(super) fn finish_active_hook_cell_if_idle(&mut self) {
let Some(cell) = self.active_hook_cell.as_ref() else {
return;
};
if cell.is_empty() {
self.active_hook_cell = None;
self.bump_active_cell_revision();
self.request_pending_usage_output_insertion();
return;
}
if cell.should_flush()
&& let Some(cell) = self.active_hook_cell.take()
{
self.bump_active_cell_revision();
self.transcript.needs_final_message_separator = true;
self.app_event_tx
.send(AppEvent::InsertHistoryCell(Box::new(cell)));
self.request_pending_usage_output_insertion();
}
}
pub(super) fn update_due_hook_visibility(&mut self) {
let Some(cell) = self.active_hook_cell.as_mut() else {
return;
};
let now = Instant::now();
if cell.advance_time(now) {
self.bump_active_cell_revision();
}
self.finish_active_hook_cell_if_idle();
self.sync_hook_status_message();
}
fn sync_hook_status_message(&mut self) {
let status_message = self
.active_hook_cell
.as_ref()
.and_then(HookCell::running_status_summary);
self.bottom_pane.set_hook_status_message(status_message);
}
pub(super) fn schedule_hook_timer_if_needed(&self) {
if self.local_settings.tui.animations
&& self
.active_hook_cell
.as_ref()
.is_some_and(HookCell::has_visible_running_run)
{
self.frame_requester
.schedule_frame_in(Duration::from_millis(50));
}
let Some(deadline) = self
.active_hook_cell
.as_ref()
.and_then(HookCell::next_timer_deadline)
else {
return;
};
let delay = deadline.saturating_duration_since(Instant::now());
self.frame_requester.schedule_frame_in(delay);
}
}