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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use std::io::{BufWriter, Read, Write};
use std::cell::Cell;
use std::sync::{
atomic::{AtomicBool, AtomicU32, Ordering},
Arc, RwLock,
};
use bytes::Bytes;
use portable_pty::{native_pty_system, CommandBuilder, PtySize};
use tokio::sync::mpsc;
use tracing::{debug, error, info, warn};
use crate::detect::{Agent, AgentState};
use crate::events::AppEvent;
use crate::layout::PaneId;
use crate::pty_callbacks::PtyResponses;
// ---------------------------------------------------------------------------
// PaneState — pure data, constructable without PTYs, testable
// ---------------------------------------------------------------------------
/// Observable state for a single pane.
/// This is the only part of a pane that workspace logic and tests need.
pub struct PaneState {
pub detected_agent: Option<Agent>,
pub state: AgentState,
/// Whether the user has seen this pane since its last state change to Idle.
/// False = "Done" (agent finished while user was in another workspace).
pub seen: bool,
}
impl PaneState {
pub fn new() -> Self {
Self {
detected_agent: None,
state: AgentState::Unknown,
seen: true,
}
}
}
// ---------------------------------------------------------------------------
// PaneRuntime — PTY, parser, channels, background tasks
// ---------------------------------------------------------------------------
/// PTY runtime for a pane. Owns the terminal, I/O channels, and background tasks.
/// Dropping this shuts down all background tasks and closes the PTY.
pub struct PaneRuntime {
pub parser: Arc<RwLock<vt100::Parser<PtyResponses>>>,
pub sender: mpsc::Sender<Bytes>,
resize_tx: mpsc::Sender<(u16, u16)>,
current_size: Cell<(u16, u16)>,
child_pid: Arc<AtomicU32>,
pub kitty_keyboard: Arc<AtomicBool>,
/// Live screen content snapshot — updated by reader, read by detector.
/// Decouples detection from parser viewport state (scrollback).
/// Kept alive here so the Arc isn't dropped; tasks hold their own clones.
#[allow(dead_code)]
screen_content: Arc<RwLock<String>>,
// Task handles for deterministic shutdown
detect_handle: tokio::task::AbortHandle,
}
impl Drop for PaneRuntime {
fn drop(&mut self) {
// Abort detection task immediately.
// Reader/writer/resize tasks shut down naturally via channel close
// and PTY EOF when the rest of PaneRuntime is dropped.
self.detect_handle.abort();
}
}
impl PaneRuntime {
pub fn spawn(
pane_id: PaneId,
rows: u16,
cols: u16,
cwd: std::path::PathBuf,
events: mpsc::Sender<AppEvent>,
) -> std::io::Result<Self> {
let pty_system = native_pty_system();
let pair = pty_system
.openpty(PtySize {
rows,
cols,
pixel_width: 0,
pixel_height: 0,
})
.map_err(|e| std::io::Error::other(e.to_string()))?;
let responses = PtyResponses::new();
let kitty_keyboard = responses.kitty_keyboard.clone();
let parser = Arc::new(RwLock::new(vt100::Parser::new_with_callbacks(
rows, cols, 10000, responses.clone(),
)));
let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".into());
let mut cmd = CommandBuilder::new(&shell);
cmd.cwd(cwd);
let reader = pair
.master
.try_clone_reader()
.map_err(|e| std::io::Error::other(e.to_string()))?;
let writer = pair
.master
.take_writer()
.map_err(|e| std::io::Error::other(e.to_string()))?;
// --- Child watcher task ---
let child_pid = Arc::new(AtomicU32::new(0));
{
let child_pid = child_pid.clone();
let slave = pair.slave;
let events = events.clone();
let rt = tokio::runtime::Handle::current();
tokio::task::spawn_blocking(move || {
match slave.spawn_command(cmd) {
Ok(mut child) => {
if let Some(pid) = child.process_id() {
child_pid.store(pid, Ordering::Release);
info!(pane = pane_id.raw(), pid, "child spawned");
}
match child.wait() {
Ok(status) => info!(pane = pane_id.raw(), ?status, "child exited"),
Err(e) => error!(pane = pane_id.raw(), err = %e, "child wait failed"),
}
}
Err(e) => error!(pane = pane_id.raw(), err = %e, "failed to spawn shell"),
}
// Use blocking send — PaneDied is critical, must not be dropped
if let Err(e) = rt.block_on(events.send(AppEvent::PaneDied { pane_id })) {
error!(pane = pane_id.raw(), err = %e, "failed to send PaneDied event");
}
});
}
// --- Writer channel ---
let (input_tx, mut input_rx) = mpsc::channel::<Bytes>(32);
// Live screen snapshot for detection (decoupled from parser scrollback)
let screen_content = Arc::new(RwLock::new(String::new()));
// --- Reader task: PTY → parser + screen snapshot + terminal query responses ---
{
let mut reader = reader;
let parser = parser.clone();
let screen_content = screen_content.clone();
let response_writer = input_tx.clone();
tokio::task::spawn_blocking(move || {
let mut buf = [0u8; 8192];
loop {
match reader.read(&mut buf) {
Ok(0) => break,
Err(e) => {
debug!(pane = pane_id.raw(), err = %e, "pty reader closed");
break;
}
Ok(n) => {
if let Ok(mut p) = parser.write() {
p.process(&buf[..n]);
// Snapshot live screen content for detection.
// Always reads at scrollback 0 (current view),
// without touching the user's scroll position.
let scrollback = p.screen().scrollback();
if scrollback > 0 {
p.screen_mut().set_scrollback(0);
}
let content = p.screen().contents();
if scrollback > 0 {
p.screen_mut().set_scrollback(scrollback);
}
if let Ok(mut sc) = screen_content.write() {
*sc = content;
}
} else {
error!(pane = pane_id.raw(), "parser lock poisoned in reader");
break;
}
let resp = responses.take();
if !resp.is_empty() {
if let Err(e) = response_writer.try_send(Bytes::from(resp)) {
warn!(pane = pane_id.raw(), err = %e, "dropped terminal query response");
}
}
}
}
}
debug!(pane = pane_id.raw(), "reader task exiting");
});
}
// --- Detection task ---
let detect_handle = {
use crate::detect;
use std::time::{Duration, Instant};
const TICK_UNIDENTIFIED: Duration = Duration::from_millis(500);
const TICK_IDENTIFIED: Duration = Duration::from_millis(300);
const PROCESS_RECHECK: Duration = Duration::from_secs(5);
let child_pid = child_pid.clone();
let screen_content = screen_content.clone();
let state_events = events.clone();
let handle = tokio::spawn(async move {
let mut agent: Option<Agent> = None;
let mut state = AgentState::Unknown;
let mut last_process_check = Instant::now();
tokio::time::sleep(Duration::from_millis(50)).await;
loop {
let tick = if agent.is_none() {
TICK_UNIDENTIFIED
} else {
TICK_IDENTIFIED
};
tokio::time::sleep(tick).await;
let now = Instant::now();
let should_check_process = agent.is_none()
|| now.duration_since(last_process_check) >= PROCESS_RECHECK;
let mut agent_changed = false;
if should_check_process {
last_process_check = now;
let pid = child_pid.load(Ordering::Acquire);
if pid > 0 {
if let Some(name) = detect::foreground_process_name(pid) {
let new_agent = detect::identify_agent(&name);
if new_agent != agent {
info!(
pane = pane_id.raw(),
?new_agent,
process = %name,
"agent changed"
);
agent = new_agent;
agent_changed = true;
}
}
}
}
let new_state = if let Ok(content) = screen_content.read() {
detect::detect_state(agent, &content)
} else {
continue;
};
if new_state != state || agent_changed {
debug!(
pane = pane_id.raw(),
?state,
?new_state,
?agent,
"state changed"
);
state = new_state;
if let Err(e) = state_events.try_send(AppEvent::StateChanged {
pane_id,
agent,
state: new_state,
}) {
warn!(
pane = pane_id.raw(),
err = %e,
"dropped StateChanged event"
);
}
}
}
});
handle.abort_handle()
};
// --- Writer task: channel → PTY ---
{
let mut writer = BufWriter::new(writer);
tokio::task::spawn_blocking(move || {
let rt = tokio::runtime::Handle::current();
while let Some(bytes) = rt.block_on(input_rx.recv()) {
if let Err(e) = writer.write_all(&bytes) {
warn!(pane = pane_id.raw(), err = %e, "pty write failed");
break;
}
if let Err(e) = writer.flush() {
warn!(pane = pane_id.raw(), err = %e, "pty flush failed");
break;
}
}
debug!(pane = pane_id.raw(), "writer task exiting");
});
}
// --- Resize task ---
let (resize_tx, mut resize_rx) = mpsc::channel::<(u16, u16)>(4);
{
let master = pair.master;
tokio::task::spawn_blocking(move || {
let rt = tokio::runtime::Handle::current();
while let Some((rows, cols)) = rt.block_on(resize_rx.recv()) {
if let Err(e) = master.resize(PtySize {
rows,
cols,
pixel_width: 0,
pixel_height: 0,
}) {
warn!(pane = pane_id.raw(), err = %e, rows, cols, "pty resize failed");
}
}
});
}
Ok(Self {
parser,
sender: input_tx,
resize_tx,
current_size: Cell::new((rows, cols)),
child_pid,
kitty_keyboard,
screen_content,
detect_handle,
})
}
/// Resize if the dimensions actually changed.
pub fn resize(&self, rows: u16, cols: u16) {
let rows = rows.max(2);
let cols = cols.max(4);
if self.current_size.get() == (rows, cols) {
return;
}
self.current_size.set((rows, cols));
if let Ok(mut p) = self.parser.write() {
p.screen_mut().set_size(rows, cols);
}
let _ = self.resize_tx.try_send((rows, cols));
}
/// Scroll up by N lines (into scrollback history).
pub fn scroll_up(&self, lines: usize) {
if let Ok(mut p) = self.parser.write() {
let current = p.screen().scrollback();
p.screen_mut().set_scrollback(current + lines);
}
}
/// Scroll down by N lines (toward live output).
pub fn scroll_down(&self, lines: usize) {
if let Ok(mut p) = self.parser.write() {
let current = p.screen().scrollback();
p.screen_mut().set_scrollback(current.saturating_sub(lines));
}
}
/// Reset scroll to live view (offset = 0).
pub fn scroll_reset(&self) {
if let Ok(mut p) = self.parser.write() {
p.screen_mut().set_scrollback(0);
}
}
/// Get the current working directory of the child shell process.
pub fn cwd(&self) -> Option<std::path::PathBuf> {
let pid = self.child_pid.load(Ordering::Relaxed);
crate::platform::process_cwd(pid)
}
}