1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
use super::super::{has_confirmation_prompt, has_interrupt_pattern, AgentState};
pub(super) fn detect(content: &str) -> AgentState {
// Strong blocked patterns are structural Codex UI chrome, so they can win
// even when the prompt region is visible.
if has_codex_strong_blocked_prompt(content) {
return AgentState::Blocked;
}
let lower = content.to_lowercase();
// Working
if has_codex_working_status_at_current_prompt(content) {
return AgentState::Working;
}
// Idle
if has_codex_current_prompt(content) {
return AgentState::Idle;
}
// Weak blocked patterns are too broad to scan above a visible prompt. They
// only apply when Codex does not currently show an idle prompt region.
if has_codex_weak_blocked_prompt(&lower) {
return AgentState::Blocked;
}
// Fallback working signals for narrow captures where the footer scrolled
// out or the working row is the only Codex chrome visible.
if has_interrupt_pattern(&lower) || has_codex_working_header(content) {
return AgentState::Working;
}
AgentState::Idle
}
pub(super) fn has_visible_blocker(content: &str) -> bool {
has_codex_strong_blocked_prompt(content)
}
pub(super) fn has_visible_working(content: &str) -> bool {
has_codex_live_working_at_current_prompt(content)
|| (!has_codex_current_prompt(content) && has_codex_visible_working_without_prompt(content))
}
pub(super) fn is_transcript_viewer(content: &str) -> bool {
let bottom_lines = bottom_non_empty_lines(content, 3);
let Some(last_line) = bottom_lines.last() else {
return false;
};
let bottom_text = normalize_lines(&bottom_lines);
bottom_text.contains("↑/↓ to scroll")
&& bottom_text.contains("pgup/pgdn to page")
&& bottom_text.contains("home/end to jump")
&& bottom_text.contains("q to quit")
&& has_codex_edit_prev_controls(&bottom_text)
&& transcript_control_tail(last_line)
}
fn has_codex_edit_prev_controls(bottom_text: &str) -> bool {
bottom_text.contains("esc to edit prev") || bottom_text.contains("esc/← to edit prev")
}
fn has_codex_visible_working_without_prompt(content: &str) -> bool {
let mut recent_lines = content.lines().rev().filter(|line| !line.trim().is_empty());
let Some(last_line) = recent_lines.next() else {
return false;
};
if codex_live_working_line(last_line) {
return true;
}
codex_status_detail_line(last_line)
&& recent_lines
.take(4)
.find(|line| codex_block_marker_line(line))
.is_some_and(codex_live_working_line)
}
fn has_codex_strong_blocked_prompt(content: &str) -> bool {
let lines: Vec<&str> = content.lines().collect();
let live_region = lines
.iter()
.rposition(|line| codex_prompt_line(line))
.map(|prompt_index| lines[prompt_index + 1..].join("\n"))
.unwrap_or_else(|| content.to_string());
let lower_content = live_region.to_lowercase();
lower_content.contains("press enter to confirm or esc to cancel")
|| lower_content.contains("enter to submit answer")
|| lower_content.contains("enter to submit all")
|| lower_content.contains("allow command?")
}
fn has_codex_weak_blocked_prompt(lower_content: &str) -> bool {
lower_content.contains("[y/n]")
|| lower_content.contains("yes (y)")
|| has_confirmation_prompt(lower_content)
}
fn has_codex_live_working_at_current_prompt(content: &str) -> bool {
codex_last_block_marker_before_current_prompt(content).is_some_and(codex_live_working_line)
}
fn has_codex_working_status_at_current_prompt(content: &str) -> bool {
codex_last_block_marker_before_current_prompt(content).is_some_and(codex_working_status_line)
}
fn codex_last_block_marker_before_current_prompt(content: &str) -> Option<&str> {
let (lines, prompt_index) = codex_current_prompt_region(content)?;
lines[..prompt_index]
.iter()
.rev()
.find(|line| codex_block_marker_line(line))
.copied()
}
fn has_codex_working_header(content: &str) -> bool {
content.lines().any(codex_working_status_line)
}
fn codex_live_working_line(line: &str) -> bool {
if codex_queued_input_header_line(line) {
return true;
}
let trimmed = line.trim_start();
let lower = trimmed.to_lowercase();
codex_working_status_line(line)
&& (trimmed.contains("Waiting for background terminal")
|| has_codex_status_interrupt_hint(&lower)
|| lower.contains("background terminal running")
|| lower.contains("/ps to view")
|| lower.contains("/stop to close"))
}
fn codex_working_status_line(line: &str) -> bool {
if codex_queued_input_header_line(line) {
return true;
}
let trimmed = line.trim_start();
let lower = trimmed.to_lowercase();
trimmed.starts_with('•')
&& (trimmed.contains("Working (")
|| trimmed.contains("Waiting for background terminal (")
|| has_codex_status_interrupt_hint(&lower)
|| lower.contains("reviewing approval request (")
|| (lower.contains("reviewing ") && lower.contains(" approval requests ("))
|| trimmed.contains("Booting MCP server:"))
}
fn has_codex_status_interrupt_hint(lower_line: &str) -> bool {
let Some((before_escape, after_escape)) = lower_line.split_once(" • esc") else {
return false;
};
has_codex_status_elapsed(before_escape) && has_codex_status_escape_suffix(after_escape)
}
fn has_codex_status_elapsed(before_escape: &str) -> bool {
let Some((_, elapsed)) = before_escape.rsplit_once('(') else {
return false;
};
let parts: Vec<&str> = elapsed.split_whitespace().collect();
(1..=3).contains(&parts.len())
&& parts.iter().all(|part| {
part.len() >= 2
&& matches!(part.as_bytes().last(), Some(b'h' | b'm' | b's'))
&& part[..part.len() - 1].chars().all(|ch| ch.is_ascii_digit())
})
}
fn has_codex_status_escape_suffix(after_escape: &str) -> bool {
let suffix = after_escape.trim_start();
if suffix.starts_with('…') || suffix.starts_with("to interrupt") {
return true;
}
let Some(rest) = suffix.strip_prefix("to ") else {
return false;
};
if rest.starts_with('…') {
return true;
}
rest.split_once('…')
.map(|(fragment, _)| !fragment.is_empty() && "interrupt".starts_with(fragment.trim_end()))
.unwrap_or(false)
}
fn has_codex_current_prompt(content: &str) -> bool {
codex_current_prompt_region(content).is_some()
}
fn codex_current_prompt_region(content: &str) -> Option<(Vec<&str>, usize)> {
let lines: Vec<&str> = content.lines().collect();
let prompt_index = lines.iter().rposition(|line| codex_prompt_line(line))?;
if lines[prompt_index + 1..]
.iter()
.any(|line| codex_block_marker_line(line))
{
return None;
}
Some((lines, prompt_index))
}
fn codex_prompt_line(line: &str) -> bool {
line == "›" || line.starts_with("› ")
}
fn codex_block_marker_line(line: &str) -> bool {
line.starts_with('•') || line.starts_with('■') || line.starts_with('✗') || line.starts_with('✓')
}
fn codex_status_detail_line(line: &str) -> bool {
line.trim_start().starts_with('└')
}
fn codex_queued_input_header_line(line: &str) -> bool {
let trimmed = line.trim_start();
if !trimmed.starts_with('•') {
return false;
}
let lower = trimmed.to_lowercase();
lower.starts_with("• queued follow-up inputs")
|| lower.starts_with("• messages to be submitted after next tool call")
}
fn bottom_non_empty_lines(content: &str, max_lines: usize) -> Vec<&str> {
let mut lines: Vec<&str> = content
.lines()
.rev()
.filter(|line| !line.trim().is_empty())
.take(max_lines)
.collect();
lines.reverse();
lines
}
fn normalize_lines(lines: &[&str]) -> String {
lines
.iter()
.flat_map(|line| line.split_whitespace())
.collect::<Vec<_>>()
.join(" ")
.to_lowercase()
}
fn transcript_control_tail(line: &str) -> bool {
let lower = line.to_lowercase();
lower.contains("q to quit")
|| lower.contains("esc to edit")
|| lower.contains("esc/← to edit")
|| lower.contains("edit message")
}
|