Files
codex/codex-rs/tui/src/chatwidget/turn_lifecycle.rs
T
Eric Traut a7a9d82f15 Collapse completed turn activity beneath transcript prompts (#60557)
## What changed

- Fold earlier commentary, reasoning, and tool activity beneath an expandable prompt control in the full-screen transcript, keeping partial and final answers visible.
- Label the control with completion timing when available, or a previous-message count otherwise. Keep tool groups independently expandable within the turn.
- Preserve assistant message phases during live completion and replay, and retain untimed completion markers to identify completed turns.

## Testing

Add transcript coverage for nested expansion, keyboard and mouse controls, narrow viewports, search anchor preservation, detailed mode, and legacy messages without phase or timing metadata.

GitOrigin-RevId: b17d9d7c870990178d76d12a13d463ff9c32b96c
2026-11-10 08:36:09 +00:00

115 lines
3.9 KiB
Rust

//! Agent-turn lifecycle state for `ChatWidget`.
use std::collections::HashMap;
use std::collections::HashSet;
use std::time::Instant;
use codex_utils_sleep_inhibitor::SleepInhibitor;
#[derive(Debug)]
pub(super) struct TurnLifecycleState {
pub(super) sleep_inhibitor: SleepInhibitor,
/// Tracks whether codex-core currently considers an agent turn to be in progress.
pub(super) agent_turn_running: bool,
pub(super) last_turn_id: Option<String>,
/// A live or restored turn makes the parent available for side-chat creation.
pub(super) has_turn_history: bool,
/// Cloud confirmed admission to a turn whose live start we have yet to process.
pub(super) admitted_turn_id: Option<String>,
pub(super) budget_limited_turn_ids: HashSet<String>,
/// False records an untimed turn boundary that may still receive a completion label.
pub(super) rendered_completion_turn_ids: HashMap<String, bool>,
pub(super) goal_status_active_turn_started_at: Option<Instant>,
}
impl TurnLifecycleState {
pub(super) fn new(prevent_idle_sleep: bool) -> Self {
Self {
sleep_inhibitor: SleepInhibitor::new(prevent_idle_sleep),
agent_turn_running: false,
last_turn_id: None,
has_turn_history: false,
admitted_turn_id: None,
budget_limited_turn_ids: HashSet::new(),
rendered_completion_turn_ids: HashMap::new(),
goal_status_active_turn_started_at: None,
}
}
pub(super) fn start(&mut self, now: Instant) {
self.agent_turn_running = true;
self.goal_status_active_turn_started_at = Some(now);
self.sleep_inhibitor.set_turn_running(/*turn_running*/ true);
}
pub(super) fn finish(&mut self) {
self.agent_turn_running = false;
self.goal_status_active_turn_started_at = None;
self.sleep_inhibitor
.set_turn_running(/*turn_running*/ false);
}
pub(super) fn is_running_or_admitted(&self) -> bool {
self.agent_turn_running || self.admitted_turn_id.is_some()
}
pub(super) fn restore_running(&mut self, running: bool, now: Instant) {
self.agent_turn_running = running;
self.goal_status_active_turn_started_at = running.then_some(now);
self.sleep_inhibitor.set_turn_running(running);
}
pub(super) fn reset_thread(&mut self) {
self.finish();
self.last_turn_id = None;
self.has_turn_history = false;
self.admitted_turn_id = None;
self.budget_limited_turn_ids.clear();
self.rendered_completion_turn_ids.clear();
}
pub(super) fn set_prevent_idle_sleep(&mut self, enabled: bool) {
self.sleep_inhibitor = SleepInhibitor::new(enabled);
self.sleep_inhibitor
.set_turn_running(self.agent_turn_running);
}
pub(super) fn mark_budget_limited(&mut self, turn_id: String) {
self.budget_limited_turn_ids.insert(turn_id);
}
pub(super) fn take_budget_limited(&mut self, turn_id: &str) -> bool {
self.budget_limited_turn_ids.remove(turn_id)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn start_and_finish_update_running_state() {
let mut state = TurnLifecycleState::new(/*prevent_idle_sleep*/ false);
state.start(Instant::now());
assert!(state.agent_turn_running);
assert!(state.goal_status_active_turn_started_at.is_some());
assert!(state.sleep_inhibitor.is_turn_running());
state.finish();
assert!(!state.agent_turn_running);
assert!(state.goal_status_active_turn_started_at.is_none());
assert!(!state.sleep_inhibitor.is_turn_running());
}
#[test]
fn budget_limited_turn_ids_are_consumed() {
let mut state = TurnLifecycleState::new(/*prevent_idle_sleep*/ false);
state.mark_budget_limited("turn-1".to_string());
assert!(state.take_budget_limited("turn-1"));
assert!(!state.take_budget_limited("turn-1"));
}
}