[codex] Improve hook status rendering (#17266)

# Motivation

Make hook display less noisy and more useful by keeping transient hook
activity out of permanent history unless there is useful output,
preserving visibility for meaningful hook work, and making completed
hook severity easier to scan.

Also addresses some of the concerns in
https://github.com/openai/codex/issues/15497

# Changes

## Demo


https://github.com/user-attachments/assets/9d8cebd4-a502-4c95-819c-c806c0731288

Reverse spec for the behavior changes in this branch:

## Hook Lifecycle Rendering
- Hook start events no longer write permanent history rows like `Running
PreToolUse hook`.
- Running hooks now render in a dedicated live hook area above the
composer. It's similar to the active cell we use for tool calls but its
a separate lane.
- Running hook rows use the existing animation setting.

## Hook Reveal Timing
- We wait 300ms before showing running hook rows and linger for up to
600ms once visible.
- This is so fast hooks don't flash a transient `Running hook` row
before user can read it every time.
- If a fast hook completes with meaningful output, only the completed
hook result is written to history.
- If a fast hook completes successfully with no output, it leaves no
visible trace.

## Completed Hook Output
- Completed hooks with output are sticky, for example `• SessionStart
hook (completed)`.
- Hook output entries are rendered under that row with stable prefixes:
`warning:`, `stop:`, `feedback:`, `hook context:`, and `error:`.
- Blocked hooks show feedback entries, for example `• PreToolUse hook
(blocked)` followed by `feedback: ...`.
- Failed hooks show error entries, for example `• PostToolUse hook
(failed)` followed by `error: ...`.
- Stopped hooks show stop entries and remain visually treated as
non-success.

## Parallel Hook Behavior
- Multiple simultaneously running hooks can be tracked in one live hook
cell.
- Adjacent running hooks with the same hook event name and same status
message collapse into a count, for example `• Running 3 PreToolUse
hooks: checking command policy`.
- Running hooks with different event names or different status messages
remain separate rows.

## Hook Run Identity
- `PreToolUse` and `PostToolUse` hook run IDs now include the tool call
ID which prevents concurrent tool-use hooks from sharing a run ID and
clobbering each other in the UI.
- This ID scoping applies to tool-use hooks only; other hook event types
keep their existing run identity behavior.

## App-Server Hook Notifications
- App-server `HookStarted` and `HookCompleted` notifications use the
same live hook rendering path as core hook events.
- `UserPromptSubmit` hook notifications now render through the same
completed hook output format, including warning and stop entries.
This commit is contained in:
Abhinav authored and GitHub committed 2026-04-10 14:05:47 -07:00
1 parent 147cb84112
commit 87b9275fff
21 files changed
+1839 -65

No files matched your search

+155 -36
View File
@@ -333,6 +333,7 @@ use crate::history_cell;
#[cfg(test)]
use crate::history_cell::AgentMessageCell;
use crate::history_cell::HistoryCell;
use crate::history_cell::HookCell;
use crate::history_cell::McpToolCallCell;
use crate::history_cell::PlainHistoryCell;
use crate::history_cell::WebSearchCell;
@@ -878,6 +879,8 @@ pub(crate) struct ChatWidget {
// Guardian review keeps its own pending set so it can derive a single
// footer summary from one or more in-flight review events.
pending_guardian_review_status: PendingGuardianReviewStatus,
// Active hook runs render in a dedicated live cell so they can run alongside tools.
active_hook_cell: Option<HookCell>,
// Semantic status used for terminal-title status rendering.
terminal_title_status_kind: TerminalTitleStatusKind,
// Previous status header to restore after a transient stream retry.
@@ -2360,6 +2363,9 @@ impl ChatWidget {
self.quit_shortcut_key = None;
self.update_task_running_state();
self.retry_status_header = None;
if self.active_hook_cell.take().is_some() {
self.bump_active_cell_revision();
}
self.pending_status_indicator_restore = false;
self.bottom_pane
.set_interrupt_hint_visible(/*visible*/ true);
@@ -4061,36 +4067,127 @@ impl ChatWidget {
}
fn on_hook_started(&mut self, event: codex_protocol::protocol::HookStartedEvent) {
let label = hook_event_label(event.run.event_name);
let mut message = format!("Running {label} hook");
if let Some(status_message) = event.run.status_message
&& !status_message.is_empty()
{
message.push_str(": ");
message.push_str(&status_message);
self.flush_answer_stream_with_separator();
self.flush_completed_hook_output();
match self.active_hook_cell.as_mut() {
Some(cell) => {
cell.start_run(event.run);
self.bump_active_cell_revision();
}
None => {
self.active_hook_cell = Some(history_cell::new_active_hook_cell(
event.run,
self.config.animations,
));
self.bump_active_cell_revision();
}
}
self.add_to_history(history_cell::new_info_event(message, /*hint*/ None));
self.request_redraw();
}
fn on_hook_completed(&mut self, event: codex_protocol::protocol::HookCompletedEvent) {
let status = format!("{:?}", event.run.status).to_lowercase();
let header = format!("{} hook ({status})", hook_event_label(event.run.event_name));
let mut lines: Vec<ratatui::text::Line<'static>> = vec![header.into()];
for entry in event.run.entries {
let prefix = match entry.kind {
codex_protocol::protocol::HookOutputEntryKind::Warning => "warning: ",
codex_protocol::protocol::HookOutputEntryKind::Stop => "stop: ",
codex_protocol::protocol::HookOutputEntryKind::Feedback => "feedback: ",
codex_protocol::protocol::HookOutputEntryKind::Context => "hook context: ",
codex_protocol::protocol::HookOutputEntryKind::Error => "error: ",
};
lines.push(format!(" {prefix}{}", entry.text).into());
let completed = event.run;
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, self.config.animations);
if !cell.is_empty() {
self.active_hook_cell = Some(cell);
self.bump_active_cell_revision();
}
}
}
}
self.add_to_history(PlainHistoryCell::new(lines));
self.flush_completed_hook_output();
self.finish_active_hook_cell_if_idle();
self.request_redraw();
}
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.bump_active_cell_revision();
self.needs_final_message_separator = true;
self.app_event_tx
.send(AppEvent::InsertHistoryCell(Box::new(completed_cell)));
}
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();
return;
}
if cell.should_flush()
&& let Some(cell) = self.active_hook_cell.take()
{
self.bump_active_cell_revision();
self.needs_final_message_separator = true;
self.app_event_tx
.send(AppEvent::InsertHistoryCell(Box::new(cell)));
}
}
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();
}
fn schedule_hook_timer_if_needed(&self) {
if self.config.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);
}
#[cfg(test)]
fn on_undo_started(&mut self, event: UndoStartedEvent) {
self.bottom_pane.ensure_status_indicator();
@@ -4138,6 +4235,8 @@ impl ChatWidget {
}
pub(crate) fn pre_draw_tick(&mut self) {
self.update_due_hook_visibility();
self.schedule_hook_timer_if_needed();
self.bottom_pane.pre_draw_tick();
if self.should_animate_terminal_title_spinner() {
self.refresh_terminal_title();
@@ -4792,6 +4891,7 @@ impl ChatWidget {
full_reasoning_buffer: String::new(),
current_status: StatusIndicatorState::working(),
pending_guardian_review_status: PendingGuardianReviewStatus::default(),
active_hook_cell: None,
terminal_title_status_kind: TerminalTitleStatusKind::Working,
retry_status_header: None,
pending_status_indicator_restore: false,
@@ -11225,11 +11325,21 @@ impl ChatWidget {
/// providing an appropriate animation tick), the overlay will keep showing a stale tail while
/// the main viewport updates.
pub(crate) fn active_cell_transcript_key(&self) -> Option<ActiveCellTranscriptKey> {
let cell = self.active_cell.as_ref()?;
let cell = self.active_cell.as_ref();
let hook_cell = self.active_hook_cell.as_ref();
if cell.is_none() && hook_cell.is_none() {
return None;
}
Some(ActiveCellTranscriptKey {
revision: self.active_cell_revision,
is_stream_continuation: cell.is_stream_continuation(),
animation_tick: cell.transcript_animation_tick(),
is_stream_continuation: cell
.map(|cell| cell.is_stream_continuation())
.unwrap_or(false),
animation_tick: cell
.and_then(|cell| cell.transcript_animation_tick())
.or_else(|| {
hook_cell.and_then(super::history_cell::HistoryCell::transcript_animation_tick)
}),
})
}
@@ -11240,8 +11350,18 @@ impl ChatWidget {
/// should pass the same width the overlay uses; using a different width will cause wrapping
/// mismatches between the main viewport and the transcript overlay.
pub(crate) fn active_cell_transcript_lines(&self, width: u16) -> Option<Vec<Line<'static>>> {
let cell = self.active_cell.as_ref()?;
let lines = cell.transcript_lines(width);
let mut lines = Vec::new();
if let Some(cell) = self.active_cell.as_ref() {
lines.extend(cell.transcript_lines(width));
}
if let Some(hook_cell) = self.active_hook_cell.as_ref() {
// Compute hook lines first so hidden hooks do not add a separator.
let hook_lines = hook_cell.transcript_lines(width);
if !hook_lines.is_empty() && !lines.is_empty() {
lines.push("".into());
}
lines.extend(hook_lines);
}
(!lines.is_empty()).then_some(lines)
}
@@ -11267,8 +11387,17 @@ impl ChatWidget {
)),
None => RenderableItem::Owned(Box::new(())),
};
let active_hook_cell_renderable = match &self.active_hook_cell {
Some(cell) if cell.should_render() => {
RenderableItem::Borrowed(cell).inset(Insets::tlbr(
/*top*/ 1, /*left*/ 0, /*bottom*/ 0, /*right*/ 0,
))
}
_ => RenderableItem::Owned(Box::new(())),
};
let mut flex = FlexRenderable::new();
flex.push(/*flex*/ 1, active_cell_renderable);
flex.push(/*flex*/ 0, active_hook_cell_renderable);
flex.push(
/*flex*/ 0,
RenderableItem::Borrowed(&self.bottom_pane).inset(Insets::tlbr(
@@ -11497,16 +11626,6 @@ fn extract_first_bold(s: &str) -> Option<String> {
None
}
fn hook_event_label(event_name: codex_protocol::protocol::HookEventName) -> &'static str {
match event_name {
codex_protocol::protocol::HookEventName::PreToolUse => "PreToolUse",
codex_protocol::protocol::HookEventName::PostToolUse => "PostToolUse",
codex_protocol::protocol::HookEventName::SessionStart => "SessionStart",
codex_protocol::protocol::HookEventName::UserPromptSubmit => "UserPromptSubmit",
codex_protocol::protocol::HookEventName::Stop => "Stop",
}
}
#[cfg(test)]
pub(crate) fn show_review_commit_picker_with_entries(
chat: &mut ChatWidget,