Files
herdr/src/pane.rs
T

3828 lines
134 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use std::cell::Cell;
use std::io;
use std::path::Path;
use std::sync::{
atomic::{AtomicBool, AtomicU16, AtomicU32, AtomicU64, Ordering},
Arc, Mutex,
};
use bytes::Bytes;
use portable_pty::CommandBuilder;
#[cfg(all(test, unix))]
use portable_pty::{native_pty_system, PtySize};
use ratatui::{layout::Rect, Frame};
#[cfg(test)]
use tokio::sync::watch;
use tokio::sync::{mpsc, Notify};
use tracing::{debug, error, info, warn};
use crate::detect::{Agent, AgentState};
use crate::events::AppEvent;
use crate::layout::PaneId;
use crate::pty::actor::{PtyIoActor, PtyIoActorConfig, PtyIoActorHandle, PtyReadResult};
mod input;
mod kitty_keyboard;
mod osc;
mod state;
mod terminal;
mod xtgettcap;
use self::terminal::{GhosttyPaneTerminal, PaneTerminal};
pub(crate) use self::terminal::{TerminalDirtyPatch, TerminalDirtyPatchOutcome};
pub use self::{
state::PaneState,
terminal::{InputState, ScrollMetrics, TerminalCursorState},
};
const RELEASE_REACQUIRE_SUPPRESSION: std::time::Duration = std::time::Duration::from_secs(1);
const PANE_TERM: &str = "xterm-256color";
const PANE_COLORTERM: &str = "truecolor";
fn apply_pane_terminal_env(cmd: &mut CommandBuilder) {
// Each pane is rendered by herdr's own terminal layer, not the outer terminal
// that launched the app. Advertising the inherited TERM leaks the host terminal
// identity into shells and across SSH, which breaks redraw and cursor movement
// when the remote side lacks matching terminfo entries.
cmd.env("TERM", PANE_TERM);
cmd.env("COLORTERM", PANE_COLORTERM);
}
#[derive(Debug, Clone, Copy)]
struct PendingAgentRelease {
agent: Agent,
until: std::time::Instant,
}
#[derive(Clone, Copy, Default)]
struct SpawnInitialState<'a> {
detected_agent: Option<Agent>,
history_ansi: Option<&'a str>,
}
fn active_pending_release(
pending_release: &Mutex<Option<PendingAgentRelease>>,
now: std::time::Instant,
) -> Option<Agent> {
let mut pending_release = pending_release.lock().ok()?;
match *pending_release {
Some(pending) if now < pending.until => Some(pending.agent),
Some(_) => {
*pending_release = None;
None
}
None => None,
}
}
async fn publish_state_changed_event(
state_events: mpsc::Sender<AppEvent>,
pane_id: PaneId,
agent: Option<Agent>,
state: AgentState,
visible_blocker: bool,
visible_working: bool,
process_exited: bool,
observed_at: std::time::Instant,
) {
// This runs on the async detector task, not the PTY reader thread.
// Waiting for queue space here preserves correctness-critical state transitions
// without blocking pane I/O.
if let Err(e) = state_events
.send(AppEvent::StateChanged {
pane_id,
agent,
state,
visible_blocker,
visible_working,
process_exited,
observed_at,
})
.await
{
warn!(
pane = pane_id.raw(),
err = %e,
"failed to deliver StateChanged event"
);
}
}
const AGENT_MISS_CONFIRMATION_ATTEMPTS: u8 = 6;
const PROCESS_RECHECK_IDENTIFIED: std::time::Duration = std::time::Duration::from_secs(5);
const PROCESS_RECHECK_MISSING_FOREGROUND_GROUP: std::time::Duration =
std::time::Duration::from_secs(30);
const PROCESS_ACQUISITION_WINDOW: std::time::Duration = std::time::Duration::from_secs(8);
const PROCESS_ACQUISITION_FAST_WINDOW: std::time::Duration = std::time::Duration::from_millis(1500);
const PROCESS_ACQUISITION_FAST_RECHECK: std::time::Duration = std::time::Duration::from_millis(500);
const PROCESS_ACQUISITION_SLOW_RECHECK: std::time::Duration = std::time::Duration::from_secs(2);
const PROCESS_ACQUISITION_IDLE_RESET: std::time::Duration = std::time::Duration::from_secs(2);
const STABLE_VISIBLE_SIGNAL_REFRESH: std::time::Duration = std::time::Duration::from_millis(800);
const AGENT_PTY_ACTIVITY_WINDOW: std::time::Duration = std::time::Duration::from_secs(1);
const AGENT_INPUT_TAINT_WINDOW: std::time::Duration = std::time::Duration::from_millis(1200);
const AGENT_STARTUP_GRACE_WINDOW: std::time::Duration = std::time::Duration::from_secs(3);
const AGENT_PENDING_IDLE_RECHECK: std::time::Duration = std::time::Duration::from_millis(100);
const AGENT_PENDING_IDLE_CONFIRMATIONS: u8 = 3;
const AGENT_PENDING_IDLE_CAP: std::time::Duration = std::time::Duration::from_millis(700);
#[derive(Debug, Clone, Copy)]
struct AgentDetectionPresence {
current_agent: Option<Agent>,
consecutive_misses: u8,
}
fn should_clear_agent_for_foreground_shell(
previous_agent: Option<Agent>,
new_agent: Option<Agent>,
foreground_is_pane_shell: bool,
) -> bool {
previous_agent.is_some() && new_agent.is_none() && foreground_is_pane_shell
}
#[cfg(unix)]
fn usable_process_cwd(pid: u32) -> Option<std::path::PathBuf> {
crate::platform::process_cwd(pid).filter(|cwd| cwd.is_absolute() && cwd.is_dir())
}
#[cfg(unix)]
fn foreground_member_cwd_different_from_shell(
shell_pid: u32,
shell_cwd: Option<&std::path::PathBuf>,
) -> Option<std::path::PathBuf> {
let job = crate::detect::foreground_job(shell_pid)?;
for process in job.processes {
if process.pid == shell_pid {
continue;
}
let Some(cwd) = usable_process_cwd(process.pid) else {
continue;
};
if shell_cwd != Some(&cwd) {
return Some(cwd);
}
}
None
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ForegroundShellAgentAction {
ObserveProbe,
ReportProcessExit,
ClearAgent,
}
fn foreground_shell_agent_action(
previous_agent: Option<Agent>,
new_agent: Option<Agent>,
foreground_is_pane_shell: bool,
process_exit_reported: bool,
) -> ForegroundShellAgentAction {
if !should_clear_agent_for_foreground_shell(previous_agent, new_agent, foreground_is_pane_shell)
{
return ForegroundShellAgentAction::ObserveProbe;
}
// Do not clear identity immediately. First publish an idle process-exit
// transition for the previous agent so notifications and wait-agent callers
// observe completion before the pane becomes unknown.
if process_exit_reported {
ForegroundShellAgentAction::ClearAgent
} else {
ForegroundShellAgentAction::ReportProcessExit
}
}
#[derive(Debug, Clone, Copy)]
struct ProcessProbeInput {
current_agent: Option<Agent>,
suppressed_agent: Option<Agent>,
foreground_pgid: Option<u32>,
last_foreground_pgid: Option<u32>,
has_process_probe: bool,
acquisition_age: Option<std::time::Duration>,
pending_foreground_shell_clear: bool,
pending_restore_probe: bool,
elapsed_since_process_check: std::time::Duration,
}
fn foreground_group_changed(
foreground_pgid: Option<u32>,
last_foreground_pgid: Option<u32>,
) -> bool {
foreground_pgid != last_foreground_pgid
&& (foreground_pgid.is_some() || last_foreground_pgid.is_some())
}
fn should_probe_foreground_job(input: ProcessProbeInput) -> bool {
if input.pending_foreground_shell_clear || input.pending_restore_probe {
return true;
}
let foreground_group_changed =
foreground_group_changed(input.foreground_pgid, input.last_foreground_pgid);
if input.suppressed_agent.is_some() {
return !input.has_process_probe || foreground_group_changed;
}
if let Some(acquisition_age) = input.acquisition_age {
let acquisition_interval = if acquisition_age <= PROCESS_ACQUISITION_FAST_WINDOW {
PROCESS_ACQUISITION_FAST_RECHECK
} else {
PROCESS_ACQUISITION_SLOW_RECHECK
};
if acquisition_age <= PROCESS_ACQUISITION_WINDOW
&& input.elapsed_since_process_check >= acquisition_interval
{
return true;
}
}
if input.current_agent.is_none() {
return !input.has_process_probe
|| foreground_group_changed
|| (input.foreground_pgid.is_none()
&& input.elapsed_since_process_check >= PROCESS_RECHECK_MISSING_FOREGROUND_GROUP);
}
foreground_group_changed || input.elapsed_since_process_check >= PROCESS_RECHECK_IDENTIFIED
}
fn sync_content_change_acquisition(
current_agent: Option<Agent>,
suppressed_agent: Option<Agent>,
process_group_changed: bool,
content_changed: bool,
now: std::time::Instant,
acquisition_started_at: &mut Option<std::time::Instant>,
last_content_change_at: &mut Option<std::time::Instant>,
) {
if current_agent.is_some() || suppressed_agent.is_some() || process_group_changed {
return;
}
if content_changed {
let should_start = acquisition_started_at.is_none_or(|started| {
now.duration_since(started) > PROCESS_ACQUISITION_WINDOW
&& last_content_change_at.is_none_or(|last_change| {
now.duration_since(last_change) >= PROCESS_ACQUISITION_IDLE_RESET
})
});
if should_start {
*acquisition_started_at = Some(now);
}
*last_content_change_at = Some(now);
return;
}
let Some(acquisition_started) = *acquisition_started_at else {
return;
};
let Some(last_content_change) = *last_content_change_at else {
return;
};
if now.duration_since(acquisition_started) > PROCESS_ACQUISITION_WINDOW
&& now.duration_since(last_content_change) >= PROCESS_ACQUISITION_IDLE_RESET
{
*acquisition_started_at = None;
*last_content_change_at = None;
}
}
#[derive(Debug, Clone)]
struct ProcessProbeResult {
process_group_id: Option<u32>,
foreground_is_pane_shell: bool,
agent: Option<Agent>,
process_name: Option<String>,
}
fn probe_foreground_process(pid: u32, foreground_pgid: Option<u32>) -> ProcessProbeResult {
if let Some(job) = foreground_pgid.and_then(crate::detect::foreground_group_leader_job) {
if let Some((agent, process_name)) = crate::detect::identify_agent_in_job(&job) {
return ProcessProbeResult {
process_group_id: Some(job.process_group_id),
foreground_is_pane_shell: job.processes.iter().any(|p| p.pid == pid),
agent: Some(agent),
process_name: Some(process_name),
};
}
}
if let Some(job) = crate::detect::foreground_job(pid) {
let identified = crate::detect::identify_agent_in_job(&job);
return ProcessProbeResult {
process_group_id: Some(job.process_group_id),
foreground_is_pane_shell: job.processes.iter().any(|p| p.pid == pid),
agent: identified.as_ref().map(|(agent, _)| *agent),
process_name: identified.map(|(_, process_name)| process_name),
};
}
ProcessProbeResult {
process_group_id: foreground_pgid,
foreground_is_pane_shell: false,
agent: None,
process_name: None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct DetectionPublishState {
state: AgentState,
visible_blocker: bool,
visible_working: bool,
}
#[derive(Debug, Default)]
struct PendingIdleConfirmation {
started_at: Option<std::time::Instant>,
confirmations: u8,
}
impl PendingIdleConfirmation {
fn active(&self) -> bool {
self.started_at.is_some()
}
fn clear(&mut self) {
self.started_at = None;
self.confirmations = 0;
}
fn should_hold_working_to_idle(
&mut self,
previous: DetectionPublishState,
next: DetectionPublishState,
agent_changed: bool,
process_exited: bool,
now: std::time::Instant,
) -> bool {
let is_working_to_plain_idle = previous.state == AgentState::Working
&& next.state == AgentState::Idle
&& !next.visible_blocker
&& !agent_changed
&& !process_exited;
if !is_working_to_plain_idle {
self.clear();
return false;
}
let Some(started_at) = self.started_at else {
self.started_at = Some(now);
self.confirmations = 0;
return true;
};
if now.duration_since(started_at) >= AGENT_PENDING_IDLE_CAP {
self.clear();
return false;
}
self.confirmations = self.confirmations.saturating_add(1);
if self.confirmations >= AGENT_PENDING_IDLE_CONFIRMATIONS {
self.clear();
return false;
}
true
}
}
fn pty_working_transition_is_vetoed(
agent: Option<Agent>,
previous: DetectionPublishState,
next: DetectionPublishState,
content: &str,
) -> bool {
previous.state == AgentState::Idle
&& next.state == AgentState::Working
&& !next.visible_blocker
&& crate::detect::agent_activity_veto(agent, content).is_some()
}
fn should_publish_detection_update(
previous: DetectionPublishState,
next: DetectionPublishState,
agent_changed: bool,
process_exited: bool,
stable_visible_signal_refresh_due: bool,
) -> bool {
next.state != previous.state
|| next.visible_blocker != previous.visible_blocker
|| next.visible_working != previous.visible_working
|| agent_changed
|| process_exited
|| (stable_visible_signal_refresh_due && next.visible_blocker && previous.visible_blocker)
}
fn stable_visible_signal_refresh_due(
previous: DetectionPublishState,
next: DetectionPublishState,
last_refresh: Option<std::time::Instant>,
now: std::time::Instant,
) -> bool {
let stable_visible_signal = next.visible_blocker && previous.visible_blocker;
stable_visible_signal
&& last_refresh.is_none_or(|last_refresh| {
now.duration_since(last_refresh) >= STABLE_VISIBLE_SIGNAL_REFRESH
})
}
fn detection_update_for_publish(
agent: Option<Agent>,
content: &str,
process_exited: bool,
) -> Option<crate::detect::AgentDetection> {
if crate::detect::should_skip_state_update(agent, content) {
return None;
}
if process_exited {
return Some(crate::detect::AgentDetection {
state: AgentState::Idle,
skip_state_update: false,
visible_blocker: false,
visible_working: false,
});
}
let detection = crate::detect::detect_agent(agent, content);
(!detection.skip_state_update).then_some(detection)
}
#[derive(Debug, Default)]
struct PtyCausalityTracker {
last_pty_render_seq: u64,
last_input_seq: u64,
input_tainted_until: Option<std::time::Instant>,
last_agent_pty_at: Option<std::time::Instant>,
}
#[derive(Debug, Clone, Copy)]
struct PtyActivitySignal {
active: bool,
tainted: bool,
}
fn baseline_pty_causality(tracker: &mut PtyCausalityTracker, pty_render_seq: u64, input_seq: u64) {
tracker.last_pty_render_seq = pty_render_seq;
tracker.last_input_seq = input_seq;
tracker.input_tainted_until = None;
tracker.last_agent_pty_at = None;
}
fn agent_caused_pty_activity_active(
pty_render_seq: u64,
input_seq: u64,
tracker: &mut PtyCausalityTracker,
now: std::time::Instant,
) -> PtyActivitySignal {
if input_seq != tracker.last_input_seq {
tracker.last_input_seq = input_seq;
tracker.input_tainted_until = Some(now + AGENT_INPUT_TAINT_WINDOW);
tracker.last_agent_pty_at = None;
}
let tainted = tracker.input_tainted_until.is_some_and(|until| now < until);
if pty_render_seq != tracker.last_pty_render_seq {
tracker.last_pty_render_seq = pty_render_seq;
if !tainted {
tracker.last_agent_pty_at = Some(now);
}
}
if tainted {
return PtyActivitySignal {
active: false,
tainted: true,
};
}
let active = tracker
.last_agent_pty_at
.is_some_and(|last| now.duration_since(last) < AGENT_PTY_ACTIVITY_WINDOW);
PtyActivitySignal {
active,
tainted: false,
}
}
#[cfg(unix)]
fn spawn_basic_detection_task(
pane_id: PaneId,
child_pid: Arc<AtomicU32>,
terminal: Arc<PaneTerminal>,
pty_render_seq: Arc<AtomicU64>,
input_write_seq: Arc<AtomicU64>,
state_events: mpsc::Sender<AppEvent>,
) -> (
tokio::task::AbortHandle,
Arc<Notify>,
Arc<Mutex<Option<PendingAgentRelease>>>,
) {
let detect_reset_notify = Arc::new(Notify::new());
let detect_reset = detect_reset_notify.clone();
let pending_release = Arc::new(Mutex::new(None));
let pending_release_for_task = pending_release.clone();
let handle = tokio::spawn(async move {
let mut agent_presence = AgentDetectionPresence::from_agent(None);
let mut state = AgentState::Unknown;
let mut last_visible_blocker = false;
let mut last_visible_working = false;
let mut last_visible_signal_refresh = None;
let mut last_process_check = std::time::Instant::now();
let mut last_foreground_pgid = None;
let mut has_process_probe = false;
let mut acquisition_started_at = None;
let mut last_content_change_at = None;
let mut pending_foreground_shell_clear = false;
let mut foreground_shell_exit_reported = false;
let mut release_was_active = false;
let mut last_detection_text = String::new();
let mut pty_causality = PtyCausalityTracker::default();
let mut agent_startup_grace_until = None;
let mut pending_idle = PendingIdleConfirmation::default();
loop {
let sleep_duration = if pending_idle.active() {
AGENT_PENDING_IDLE_RECHECK
} else {
std::time::Duration::from_millis(300)
};
tokio::select! {
_ = tokio::time::sleep(sleep_duration) => {}
_ = detect_reset.notified() => {
agent_presence = AgentDetectionPresence::from_agent(None);
state = AgentState::Unknown;
last_visible_blocker = false;
last_visible_working = false;
last_visible_signal_refresh = None;
last_process_check = std::time::Instant::now();
last_foreground_pgid = None;
has_process_probe = false;
acquisition_started_at = None;
last_content_change_at = None;
pending_foreground_shell_clear = false;
foreground_shell_exit_reported = false;
release_was_active = false;
last_detection_text.clear();
pty_causality = PtyCausalityTracker::default();
agent_startup_grace_until = None;
pending_idle.clear();
}
}
let now = std::time::Instant::now();
let suppressed_agent = active_pending_release(&pending_release_for_task, now);
if suppressed_agent.is_none() && release_was_active {
has_process_probe = false;
acquisition_started_at = None;
last_content_change_at = None;
}
release_was_active = suppressed_agent.is_some();
let pid = child_pid.load(Ordering::Acquire);
let mut agent_changed = false;
let mut agent = agent_presence.current_agent();
let foreground_pgid = (pid > 0)
.then(|| crate::detect::foreground_process_group_id(pid))
.flatten();
let process_group_changed =
foreground_group_changed(foreground_pgid, last_foreground_pgid);
let should_check_process = pid > 0
&& should_probe_foreground_job(ProcessProbeInput {
current_agent: agent_presence.current_agent(),
suppressed_agent,
foreground_pgid,
last_foreground_pgid,
has_process_probe,
acquisition_age: acquisition_started_at
.map(|started| now.duration_since(started)),
pending_foreground_shell_clear,
pending_restore_probe: false,
elapsed_since_process_check: now.duration_since(last_process_check),
});
if should_check_process {
last_process_check = now;
let had_process_probe = has_process_probe;
has_process_probe = true;
let probe = probe_foreground_process(pid, foreground_pgid);
let process_group_id = probe.process_group_id;
let foreground_is_pane_shell = probe.foreground_is_pane_shell;
let mut new_agent = probe.agent;
if let Some(suppressed_agent) = suppressed_agent {
if new_agent == Some(suppressed_agent) {
new_agent = None;
} else if let Ok(mut pending_release) = pending_release_for_task.lock() {
*pending_release = None;
}
}
let previous_agent = agent_presence.current_agent();
let changed = match foreground_shell_agent_action(
previous_agent,
new_agent,
foreground_is_pane_shell,
foreground_shell_exit_reported,
) {
ForegroundShellAgentAction::ReportProcessExit => {
pending_foreground_shell_clear = true;
false
}
ForegroundShellAgentAction::ClearAgent => {
pending_foreground_shell_clear = false;
foreground_shell_exit_reported = false;
agent_presence.clear_current_agent()
}
ForegroundShellAgentAction::ObserveProbe => {
pending_foreground_shell_clear = false;
foreground_shell_exit_reported = false;
agent_presence.observe_process_probe(new_agent)
}
};
if new_agent.is_some() {
last_foreground_pgid = process_group_id.or(foreground_pgid);
acquisition_started_at = None;
last_content_change_at = None;
} else if agent_presence.current_agent().is_none() {
last_foreground_pgid = process_group_id.or(foreground_pgid);
if had_process_probe && process_group_changed {
acquisition_started_at = Some(now);
}
} else {
last_foreground_pgid = process_group_id.or(foreground_pgid);
}
if changed {
agent = agent_presence.current_agent();
agent_changed = previous_agent != agent;
if agent_changed {
pending_idle.clear();
if agent.is_some() {
agent_startup_grace_until = Some(now + AGENT_STARTUP_GRACE_WINDOW);
baseline_pty_causality(
&mut pty_causality,
pty_render_seq.load(Ordering::Relaxed),
input_write_seq.load(Ordering::Relaxed),
);
state = AgentState::Idle;
last_visible_blocker = false;
last_visible_working = false;
last_visible_signal_refresh = None;
publish_state_changed_event(
state_events.clone(),
pane_id,
agent,
AgentState::Idle,
false,
false,
false,
now,
)
.await;
} else {
agent_startup_grace_until = None;
}
}
}
}
let process_exited = pending_foreground_shell_clear
&& agent.is_some()
&& !foreground_shell_exit_reported;
if let Some(until) = agent_startup_grace_until {
if process_exited {
agent_startup_grace_until = None;
pending_idle.clear();
} else {
if now < until {
pending_idle.clear();
continue;
}
baseline_pty_causality(
&mut pty_causality,
pty_render_seq.load(Ordering::Relaxed),
input_write_seq.load(Ordering::Relaxed),
);
agent_startup_grace_until = None;
pending_idle.clear();
continue;
}
}
let content = terminal.detection_text();
let content_changed = content != last_detection_text;
last_detection_text.clone_from(&content);
if !process_exited && crate::detect::should_skip_state_update(agent, &content) {
pending_idle.clear();
continue;
}
sync_content_change_acquisition(
agent_presence.current_agent(),
suppressed_agent,
process_group_changed,
content_changed,
now,
&mut acquisition_started_at,
&mut last_content_change_at,
);
let Some(screen_detection) =
detection_update_for_publish(agent, &content, process_exited)
else {
continue;
};
let pty_signal = if agent.is_some() {
let signal = agent_caused_pty_activity_active(
pty_render_seq.load(Ordering::Relaxed),
input_write_seq.load(Ordering::Relaxed),
&mut pty_causality,
now,
);
Some(crate::agent_detection_policy::PtySignal {
active: signal.active,
tainted: signal.tainted,
})
} else {
None
};
let detection = match crate::agent_detection_policy::apply_detection_policy(
crate::agent_detection_policy::DetectionPolicyInput {
agent,
screen_detection,
process_exited,
startup_grace_active: false,
pty_signal,
},
) {
crate::agent_detection_policy::DetectionPolicyDecision::Publish(detection) => {
detection
}
crate::agent_detection_policy::DetectionPolicyDecision::Freeze => {
pending_idle.clear();
continue;
}
};
let new_state = crate::terminal::state::stabilize_agent_detection(detection);
let visible_blocker = detection.visible_blocker && new_state == AgentState::Blocked;
let visible_working = detection.visible_working && new_state == AgentState::Working;
let previous_publish = DetectionPublishState {
state,
visible_blocker: last_visible_blocker,
visible_working: last_visible_working,
};
let next_publish = DetectionPublishState {
state: new_state,
visible_blocker,
visible_working,
};
let stable_refresh_due = stable_visible_signal_refresh_due(
previous_publish,
next_publish,
last_visible_signal_refresh,
now,
);
if pty_working_transition_is_vetoed(agent, previous_publish, next_publish, &content) {
pending_idle.clear();
continue;
}
if pending_idle.should_hold_working_to_idle(
previous_publish,
next_publish,
agent_changed,
process_exited,
now,
) {
continue;
}
if should_publish_detection_update(
previous_publish,
next_publish,
agent_changed,
process_exited,
stable_refresh_due,
) {
state = new_state;
last_visible_blocker = visible_blocker;
last_visible_working = visible_working;
if visible_blocker || visible_working {
last_visible_signal_refresh = Some(now);
} else {
last_visible_signal_refresh = None;
}
publish_state_changed_event(
state_events.clone(),
pane_id,
agent,
new_state,
visible_blocker,
visible_working,
process_exited,
now,
)
.await;
if process_exited {
foreground_shell_exit_reported = true;
}
}
}
});
(handle.abort_handle(), detect_reset_notify, pending_release)
}
impl AgentDetectionPresence {
fn from_agent(current_agent: Option<Agent>) -> Self {
Self {
current_agent,
consecutive_misses: 0,
}
}
fn current_agent(&self) -> Option<Agent> {
self.current_agent
}
fn clear_current_agent(&mut self) -> bool {
if self.current_agent.is_none() {
self.consecutive_misses = 0;
return false;
}
self.current_agent = None;
self.consecutive_misses = 0;
true
}
fn observe_process_probe(&mut self, identified_agent: Option<Agent>) -> bool {
match identified_agent {
Some(agent) => {
self.consecutive_misses = 0;
if Some(agent) == self.current_agent {
return false;
}
self.current_agent = Some(agent);
true
}
None => {
if self.current_agent.is_none() {
self.consecutive_misses = 0;
return false;
}
self.consecutive_misses = self.consecutive_misses.saturating_add(1);
if self.consecutive_misses < AGENT_MISS_CONFIRMATION_ATTEMPTS {
return false;
}
self.current_agent = None;
self.consecutive_misses = 0;
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 {
pane_id: PaneId,
terminal: Arc<PaneTerminal>,
io: PaneRuntimeIo,
current_size: Cell<(u16, u16, u32, u32)>,
child_pid: Arc<AtomicU32>,
reported_cwd: Arc<Mutex<Option<std::path::PathBuf>>>,
child_wait_completed: Option<Arc<AtomicBool>>,
kitty_keyboard_flags: Arc<AtomicU16>,
input_write_seq: Arc<AtomicU64>,
detect_reset_notify: Arc<Notify>,
pending_release: Arc<Mutex<Option<PendingAgentRelease>>>,
preserve_processes_on_drop: bool,
// Task handles for deterministic shutdown
detect_handle: tokio::task::AbortHandle,
}
enum PaneRuntimeIo {
Actor(PtyIoActorHandle),
#[cfg(test)]
TestChannel {
sender: mpsc::Sender<Bytes>,
resize_tx: watch::Sender<(u16, u16, u32, u32)>,
},
}
impl PaneRuntimeIo {
fn shutdown(&self) {
match self {
PaneRuntimeIo::Actor(actor) => actor.shutdown(),
#[cfg(test)]
PaneRuntimeIo::TestChannel { .. } => {}
}
}
#[cfg(unix)]
fn duplicate_handoff_fd(&self) -> std::io::Result<std::os::fd::RawFd> {
match self {
PaneRuntimeIo::Actor(actor) => actor.duplicate_for_handoff(),
#[cfg(test)]
PaneRuntimeIo::TestChannel { .. } => {
Err(std::io::Error::other("test runtime has no PTY master fd"))
}
}
}
#[cfg(unix)]
fn foreground_process_group_id(&self) -> Option<u32> {
match self {
PaneRuntimeIo::Actor(actor) => actor.foreground_process_group_id(),
#[cfg(test)]
PaneRuntimeIo::TestChannel { .. } => None,
}
}
#[cfg(unix)]
fn begin_handoff(&self, timeout: std::time::Duration) -> std::io::Result<()> {
match self {
PaneRuntimeIo::Actor(actor) => actor.begin_handoff(timeout),
#[cfg(test)]
PaneRuntimeIo::TestChannel { .. } => Ok(()),
}
}
#[cfg(unix)]
fn set_handoff_paused(&self, paused: bool) -> std::io::Result<()> {
match self {
PaneRuntimeIo::Actor(actor) => {
if paused {
actor.begin_handoff(std::time::Duration::from_secs(1))
} else {
actor.rollback_handoff()
}
}
#[cfg(test)]
PaneRuntimeIo::TestChannel { .. } => Ok(()),
}
}
#[cfg(unix)]
fn release_after_commit(&self) -> std::io::Result<()> {
match self {
PaneRuntimeIo::Actor(actor) => actor.release_after_commit(),
#[cfg(test)]
PaneRuntimeIo::TestChannel { .. } => Ok(()),
}
}
fn resize(
&self,
rows: u16,
cols: u16,
cell_width_px: u32,
cell_height_px: u32,
terminal_responses: Vec<Bytes>,
) {
match self {
PaneRuntimeIo::Actor(actor) => {
actor.resize(
rows,
cols,
cell_width_px,
cell_height_px,
terminal_responses,
);
}
#[cfg(test)]
PaneRuntimeIo::TestChannel { resize_tx, .. } => {
let _ = resize_tx.send((rows, cols, cell_width_px, cell_height_px));
}
}
}
#[cfg(unix)]
fn nudge_child_redraw_after_handoff(
&self,
rows: u16,
cols: u16,
cell_width_px: u32,
cell_height_px: u32,
) {
match self {
PaneRuntimeIo::Actor(actor) => {
actor.nudge_child_redraw_after_handoff(rows, cols, cell_width_px, cell_height_px);
}
#[cfg(test)]
PaneRuntimeIo::TestChannel { .. } => {}
}
}
async fn send_bytes(&self, bytes: Bytes) -> Result<(), mpsc::error::SendError<Bytes>> {
match self {
PaneRuntimeIo::Actor(actor) => actor.write_user_input(bytes).await,
#[cfg(test)]
PaneRuntimeIo::TestChannel { sender, .. } => sender.send(bytes).await,
}
}
fn try_send_bytes(&self, bytes: Bytes) -> Result<(), mpsc::error::TrySendError<Bytes>> {
match self {
PaneRuntimeIo::Actor(actor) => actor.try_write_user_input(bytes),
#[cfg(test)]
PaneRuntimeIo::TestChannel { sender, .. } => sender.try_send(bytes),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WheelRouting {
HostScroll,
MouseReport,
AlternateScroll,
}
impl Drop for PaneRuntime {
fn drop(&mut self) {
// Abort detection task immediately and terminate the owned session.
// The PTY actor shuts down before the process/session policy runs.
self.detect_handle.abort();
self.io.shutdown();
if !self.preserve_processes_on_drop {
shutdown_pane_processes(
self.pane_id,
self.child_pid.load(Ordering::Acquire),
self.child_wait_completed.as_deref(),
);
}
}
}
fn process_alive_for_shutdown(
pid: u32,
child_pid: u32,
child_wait_completed: bool,
process_exists: impl FnOnce(u32) -> bool,
) -> bool {
if pid == child_pid && child_wait_completed {
return false;
}
process_exists(pid)
}
fn wait_for_processes_to_exit(
pids: &[u32],
child_pid: u32,
child_wait_completed: Option<&AtomicBool>,
timeout: std::time::Duration,
) -> bool {
let deadline = std::time::Instant::now() + timeout;
loop {
let child_wait_completed =
child_wait_completed.is_some_and(|flag| flag.load(Ordering::Acquire));
if pids.iter().all(|pid| {
!process_alive_for_shutdown(
*pid,
child_pid,
child_wait_completed,
crate::platform::process_exists,
)
}) {
return true;
}
if std::time::Instant::now() >= deadline {
return false;
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
}
fn shutdown_pane_processes(
pane_id: PaneId,
child_pid: u32,
child_wait_completed: Option<&AtomicBool>,
) {
if child_pid == 0 {
return;
}
let mut pids = crate::platform::session_processes(child_pid);
if pids.is_empty() {
pids.push(child_pid);
}
pids.sort_unstable();
pids.dedup();
for (signal, grace) in [
(
crate::platform::Signal::Hangup,
std::time::Duration::from_millis(250),
),
(
crate::platform::Signal::Terminate,
std::time::Duration::from_millis(250),
),
(
crate::platform::Signal::Kill,
std::time::Duration::from_millis(250),
),
] {
crate::platform::signal_processes(&pids, signal);
if wait_for_processes_to_exit(&pids, child_pid, child_wait_completed, grace) {
info!(
pane = pane_id.raw(),
pid = child_pid,
?signal,
"pane session terminated"
);
return;
}
}
warn!(
pane = pane_id.raw(),
pid = child_pid,
pids = ?pids,
"pane session still alive after forced shutdown"
);
}
#[cfg(unix)]
fn truncate_handoff_history(history: String, max_bytes: usize) -> String {
if history.len() <= max_bytes {
return history;
}
let mut start = history.len().saturating_sub(max_bytes);
while !history.is_char_boundary(start) {
start += 1;
}
let Some(newline_offset) = history[start..].find('\n') else {
return String::new();
};
start += newline_offset + 1;
history[start..].to_owned()
}
fn pane_shell(configured_shell: &str) -> String {
pane_shell_from(configured_shell, std::env::var("SHELL").ok())
}
fn pane_shell_from(configured_shell: &str, env_shell: Option<String>) -> String {
let configured_shell = configured_shell.trim();
if !configured_shell.is_empty() {
return configured_shell.to_string();
}
#[cfg(windows)]
{
let _ = env_shell;
default_pane_shell()
}
#[cfg(not(windows))]
env_shell
.map(|shell| shell.trim().to_string())
.filter(|shell| !shell.is_empty())
.unwrap_or_else(default_pane_shell)
}
#[cfg(windows)]
fn default_pane_shell() -> String {
"powershell.exe".into()
}
#[cfg(not(windows))]
fn default_pane_shell() -> String {
"/bin/sh".into()
}
#[derive(Clone, Copy)]
pub(crate) struct PaneShellConfig<'a> {
pub(crate) default_shell: &'a str,
pub(crate) mode: crate::config::ShellModeConfig,
}
impl<'a> PaneShellConfig<'a> {
pub(crate) fn new(default_shell: &'a str, mode: crate::config::ShellModeConfig) -> Self {
Self {
default_shell,
mode,
}
}
}
fn shell_mode_uses_login_shell(
mode: crate::config::ShellModeConfig,
target_is_macos: bool,
) -> bool {
match mode {
crate::config::ShellModeConfig::Auto => target_is_macos,
crate::config::ShellModeConfig::Login => true,
crate::config::ShellModeConfig::NonLogin => false,
}
}
fn is_executable_file(path: &Path) -> bool {
let Ok(metadata) = path.metadata() else {
return false;
};
if !metadata.is_file() {
return false;
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
metadata.permissions().mode() & 0o111 != 0
}
#[cfg(not(unix))]
{
true
}
}
fn resolve_shell_for_login_mode(shell: &str) -> io::Result<String> {
if shell.contains(std::path::MAIN_SEPARATOR) {
let path = Path::new(shell);
return is_executable_file(path)
.then(|| shell.to_string())
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
format!("login shell {shell:?} is not executable"),
)
});
}
std::env::var_os("PATH")
.and_then(|path| {
std::env::split_paths(&path)
.map(|dir| dir.join(shell))
.find(|candidate| is_executable_file(candidate))
})
.and_then(|path| path.into_os_string().into_string().ok())
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
format!("login shell {shell:?} was not found on PATH"),
)
})
}
fn pane_shell_command_builder_for_target(
shell_config: PaneShellConfig<'_>,
target_is_macos: bool,
) -> io::Result<CommandBuilder> {
let shell = pane_shell(shell_config.default_shell);
if shell_mode_uses_login_shell(shell_config.mode, target_is_macos) {
let mut cmd = CommandBuilder::new_default_prog();
cmd.env("SHELL", resolve_shell_for_login_mode(&shell)?);
Ok(cmd)
} else {
let mut cmd = CommandBuilder::new(&shell);
apply_windows_powershell_cwd_reporting(&mut cmd, &shell);
Ok(cmd)
}
}
fn pane_shell_command_builder(shell_config: PaneShellConfig<'_>) -> io::Result<CommandBuilder> {
pane_shell_command_builder_for_target(shell_config, cfg!(target_os = "macos"))
}
#[cfg(windows)]
fn apply_windows_powershell_cwd_reporting(cmd: &mut CommandBuilder, shell: &str) {
if !is_windows_powershell_shell(shell) {
return;
}
cmd.arg("-NoExit");
cmd.arg("-Command");
cmd.arg(windows_powershell_cwd_prompt_wrapper());
}
#[cfg(not(windows))]
fn apply_windows_powershell_cwd_reporting(cmd: &mut CommandBuilder, shell: &str) {
let _ = (cmd, shell);
}
#[cfg(windows)]
fn is_windows_powershell_shell(shell: &str) -> bool {
let name = Path::new(shell)
.file_name()
.and_then(std::ffi::OsStr::to_str)
.unwrap_or(shell)
.to_ascii_lowercase();
matches!(
name.as_str(),
"powershell" | "powershell.exe" | "pwsh" | "pwsh.exe"
)
}
#[cfg(windows)]
fn windows_powershell_cwd_prompt_wrapper() -> &'static str {
r#"$global:__HERDR_ORIGINAL_PROMPT = if (Test-Path Function:\prompt) { (Get-Command prompt -CommandType Function).ScriptBlock } else { { "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " } }; function global:prompt { try { if ($PWD.Provider.Name -eq 'FileSystem') { $uri = ([System.Uri]$PWD.ProviderPath).AbsoluteUri; [Console]::Write("$([char]27)]7;$uri$([char]7)") } } catch {}; & $global:__HERDR_ORIGINAL_PROMPT }"#
}
fn usable_reported_cwd(cwd: std::path::PathBuf) -> Option<std::path::PathBuf> {
(cwd.is_absolute() && cwd.is_dir()).then_some(cwd)
}
fn publish_reported_cwd(
pane_id: PaneId,
cwd: std::path::PathBuf,
reported_cwd: &Arc<Mutex<Option<std::path::PathBuf>>>,
events: &mpsc::Sender<AppEvent>,
) {
let Some(cwd) = usable_reported_cwd(cwd) else {
return;
};
if let Ok(mut current) = reported_cwd.lock() {
if current.as_ref() == Some(&cwd) {
return;
}
*current = Some(cwd.clone());
}
if let Err(err) = events.try_send(AppEvent::TerminalCwdReported { pane_id, cwd }) {
warn!(
pane = pane_id.raw(),
err = %err,
"failed to send terminal cwd report"
);
}
}
impl PaneRuntime {
pub fn shutdown(mut self) {
self.detect_handle.abort();
self.io.shutdown();
shutdown_pane_processes(
self.pane_id,
self.child_pid.load(Ordering::Acquire),
self.child_wait_completed.as_deref(),
);
self.preserve_processes_on_drop = true;
}
#[cfg(unix)]
pub fn duplicate_handoff_fd(&self) -> std::io::Result<std::os::fd::RawFd> {
self.io.duplicate_handoff_fd()
}
#[cfg(unix)]
pub fn preserve_for_handoff(mut self) {
if let Err(err) = self.io.release_after_commit() {
warn!(
pane = self.pane_id.raw(),
err = %err,
"failed to release PTY actor after handoff commit; dropping runtime will still close the actor handle"
);
}
self.detect_handle.abort();
self.preserve_processes_on_drop = true;
}
#[cfg(unix)]
pub fn assume_handoff_ownership(&mut self) {
self.preserve_processes_on_drop = false;
}
#[cfg(unix)]
pub fn set_handoff_reader_paused(&self, paused: bool) {
if let Err(err) = self.io.set_handoff_paused(paused) {
warn!(
pane = self.pane_id.raw(),
err = %err,
paused,
"failed to update PTY actor handoff pause state"
);
}
}
#[cfg(unix)]
pub fn pause_handoff_reader(&self, timeout: std::time::Duration) -> std::io::Result<()> {
self.io.begin_handoff(timeout)
}
#[cfg(unix)]
pub fn handoff_runtime_state(
&self,
pane_id: u32,
) -> crate::handoff_runtime::HandoffRuntimeState {
let child_pid = self.child_pid.load(Ordering::Acquire);
let (rows, cols, cell_width_px, cell_height_px) = self.current_size.get();
crate::handoff_runtime::HandoffRuntimeState {
pane_id,
child_pid,
rows,
cols,
cell_width_px,
cell_height_px,
keyboard_protocol_flags: match self.keyboard_protocol() {
crate::input::KeyboardProtocol::Legacy => 0,
crate::input::KeyboardProtocol::Kitty { flags } => flags,
},
keyboard_protocol_ansi: self.terminal.kitty_keyboard_state_ansi(),
input_state: self.input_state(),
initial_history_ansi: None,
}
}
#[cfg(unix)]
pub fn handoff_history_ansi(&self) -> Option<String> {
if self
.terminal
.input_state()
.is_some_and(|input_state| input_state.alternate_screen)
{
return None;
}
self.snapshot_history().map(|history| {
truncate_handoff_history(history, crate::server::handoff::MAX_REPLAY_BYTES_PER_PANE)
})
}
pub fn apply_host_terminal_theme(&self, theme: crate::terminal_theme::TerminalTheme) {
self.terminal.apply_host_terminal_theme(theme);
}
pub fn spawn(
pane_id: PaneId,
rows: u16,
cols: u16,
cwd: std::path::PathBuf,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
shell_config: PaneShellConfig<'_>,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<AtomicBool>,
) -> std::io::Result<Self> {
Self::spawn_with_initial_history(
pane_id,
rows,
cols,
cwd,
scrollback_limit_bytes,
host_terminal_theme,
shell_config,
None,
events,
render_notify,
render_dirty,
)
}
pub(crate) fn spawn_with_initial_history(
pane_id: PaneId,
rows: u16,
cols: u16,
cwd: std::path::PathBuf,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
shell_config: PaneShellConfig<'_>,
initial_history_ansi: Option<&str>,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<AtomicBool>,
) -> std::io::Result<Self> {
let mut cmd = pane_shell_command_builder(shell_config)?;
cmd.cwd(cwd);
cmd.env(crate::HERDR_ENV_VAR, crate::HERDR_ENV_VALUE);
apply_pane_terminal_env(&mut cmd);
crate::integration::apply_pane_env(&mut cmd, pane_id);
Self::spawn_command_builder(
pane_id,
rows,
cols,
scrollback_limit_bytes,
host_terminal_theme,
events,
render_notify,
render_dirty,
cmd,
"failed to spawn shell",
SpawnInitialState {
detected_agent: None,
history_ansi: initial_history_ansi,
},
)
}
pub fn spawn_shell_command(
pane_id: PaneId,
rows: u16,
cols: u16,
cwd: std::path::PathBuf,
command: &str,
extra_env: &[(String, String)],
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<AtomicBool>,
) -> std::io::Result<Self> {
let mut cmd = CommandBuilder::new("/bin/sh");
cmd.arg("-c");
cmd.arg(command);
cmd.cwd(cwd);
cmd.env(crate::HERDR_ENV_VAR, crate::HERDR_ENV_VALUE);
apply_pane_terminal_env(&mut cmd);
crate::integration::apply_pane_env(&mut cmd, pane_id);
for (key, value) in extra_env {
cmd.env(key, value);
}
Self::spawn_command_builder(
pane_id,
rows,
cols,
scrollback_limit_bytes,
host_terminal_theme,
events,
render_notify,
render_dirty,
cmd,
"failed to spawn command pane",
SpawnInitialState::default(),
)
}
pub fn spawn_argv_command(
pane_id: PaneId,
rows: u16,
cols: u16,
cwd: std::path::PathBuf,
argv: &[String],
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<AtomicBool>,
) -> std::io::Result<Self> {
let Some((program, args)) = argv.split_first() else {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"argv must not be empty",
));
};
let mut cmd = CommandBuilder::new(program);
for arg in args {
cmd.arg(arg);
}
cmd.cwd(cwd);
cmd.env(crate::HERDR_ENV_VAR, crate::HERDR_ENV_VALUE);
apply_pane_terminal_env(&mut cmd);
crate::integration::apply_pane_env(&mut cmd, pane_id);
Self::spawn_command_builder(
pane_id,
rows,
cols,
scrollback_limit_bytes,
host_terminal_theme,
events,
render_notify,
render_dirty,
cmd,
"failed to spawn argv command pane",
SpawnInitialState::default(),
)
}
#[cfg(unix)]
pub fn from_handoff_fd(
import: crate::handoff_runtime::ImportedHandoffRuntime,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<AtomicBool>,
) -> std::io::Result<Self> {
let crate::handoff_runtime::ImportedHandoffRuntime { master_fd, state } = import;
let crate::handoff_runtime::HandoffRuntimeState {
pane_id,
child_pid,
rows,
cols,
cell_width_px,
cell_height_px,
keyboard_protocol_flags,
keyboard_protocol_ansi,
input_state,
initial_history_ansi,
} = state;
let pane_id = PaneId::from_raw(pane_id);
use std::os::fd::FromRawFd;
let master_fd = unsafe { std::os::fd::OwnedFd::from_raw_fd(master_fd) };
let (response_tx, _response_rx) = mpsc::channel::<Bytes>(1);
let mut terminal = crate::ghostty::Terminal::new(cols, rows, scrollback_limit_bytes)
.map_err(|e| std::io::Error::other(e.to_string()))?;
terminal
.enable_grapheme_cluster_mode()
.map_err(|e| std::io::Error::other(e.to_string()))?;
if crate::kitty_graphics::is_enabled() {
terminal
.enable_kitty_graphics()
.map_err(|e| std::io::Error::other(e.to_string()))?;
}
let pane_terminal = GhosttyPaneTerminal::new(terminal, response_tx.clone())?;
pane_terminal.apply_host_terminal_theme(host_terminal_theme);
if let Some(input_state) = input_state {
pane_terminal.seed_handoff_input_state(input_state);
}
if let Some(ansi) = keyboard_protocol_ansi.as_deref() {
pane_terminal.seed_keyboard_protocol_ansi(ansi);
} else {
pane_terminal.seed_keyboard_protocol_flags(keyboard_protocol_flags);
}
if let Some(ansi) = initial_history_ansi.as_deref() {
pane_terminal.seed_history_ansi(ansi);
}
let terminal = Arc::new(PaneTerminal::new(pane_terminal));
let child_pid = Arc::new(AtomicU32::new(child_pid));
let reported_cwd = Arc::new(Mutex::new(None));
let kitty_keyboard_flags = Arc::new(AtomicU16::new(keyboard_protocol_flags));
let input_write_seq = Arc::new(AtomicU64::new(0));
let pty_render_seq = Arc::new(AtomicU64::new(0));
let io = {
let terminal = terminal.clone();
let response_writer = response_tx.clone();
let render_notify = render_notify.clone();
let render_dirty = render_dirty.clone();
let pty_render_seq = pty_render_seq.clone();
let child_pid = child_pid.clone();
let read_events = events.clone();
let reported_cwd = reported_cwd.clone();
let rt = tokio::runtime::Handle::current();
let delay_rt = rt.clone();
let on_read = Box::new(move |bytes: &[u8]| {
let shell_pid = child_pid.load(Ordering::Acquire);
let result =
terminal.process_pty_bytes(pane_id, shell_pid, bytes, &response_writer);
if result.request_render {
pty_render_seq.fetch_add(1, Ordering::Relaxed);
if !render_dirty.swap(true, Ordering::AcqRel) {
render_notify.notify_one();
}
}
if let Some(delay) = result.render_delay {
let render_notify = render_notify.clone();
let render_dirty = render_dirty.clone();
delay_rt.spawn(async move {
tokio::time::sleep(delay).await;
if !render_dirty.swap(true, Ordering::AcqRel) {
render_notify.notify_one();
}
});
}
if let Some(cwd) = result.reported_cwd.clone() {
publish_reported_cwd(pane_id, cwd, &reported_cwd, &read_events);
}
for content in result.clipboard_writes {
if let Err(err) = read_events.try_send(AppEvent::ClipboardWrite { content }) {
warn!(
pane = pane_id.raw(),
err = %err,
"failed to queue OSC 52 clipboard write"
);
}
}
PtyReadResult {
terminal_responses: result.terminal_responses,
}
});
let exit_events = events.clone();
let on_reader_exit = Box::new(move || {
let _ = rt.block_on(exit_events.send(AppEvent::PaneDied { pane_id }));
debug!(pane = pane_id.raw(), "handoff PTY actor exiting");
});
PaneRuntimeIo::Actor(PtyIoActor::spawn(PtyIoActorConfig {
pane_id: pane_id.raw(),
master_fd,
initially_quiesced: true,
on_read,
on_reader_exit: Some(on_reader_exit),
})?)
};
let (detect_handle, detect_reset_notify, pending_release) = spawn_basic_detection_task(
pane_id,
child_pid.clone(),
terminal.clone(),
pty_render_seq,
input_write_seq.clone(),
events,
);
Ok(Self {
pane_id,
terminal,
io,
current_size: Cell::new((rows, cols, cell_width_px, cell_height_px)),
child_pid,
reported_cwd,
child_wait_completed: None,
kitty_keyboard_flags,
input_write_seq,
detect_reset_notify,
pending_release,
preserve_processes_on_drop: true,
detect_handle,
})
}
fn spawn_command_builder(
pane_id: PaneId,
rows: u16,
cols: u16,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<AtomicBool>,
cmd: CommandBuilder,
spawn_error_message: &'static str,
initial_state: SpawnInitialState<'_>,
) -> std::io::Result<Self> {
crate::logging::pane_spawn_started(pane_id.raw(), rows, cols, scrollback_limit_bytes);
let (response_tx, _response_rx) = mpsc::channel::<Bytes>(1);
let mut terminal = crate::ghostty::Terminal::new(cols, rows, scrollback_limit_bytes)
.map_err(|e| std::io::Error::other(e.to_string()))?;
terminal
.enable_grapheme_cluster_mode()
.map_err(|e| std::io::Error::other(e.to_string()))?;
if crate::kitty_graphics::is_enabled() {
terminal
.enable_kitty_graphics()
.map_err(|e| std::io::Error::other(e.to_string()))?;
}
let pane_terminal = GhosttyPaneTerminal::new(terminal, response_tx.clone())?;
pane_terminal.apply_host_terminal_theme(host_terminal_theme);
if let Some(ansi) = initial_state.history_ansi {
pane_terminal.seed_history_ansi(ansi);
}
let terminal = Arc::new(PaneTerminal::new(pane_terminal));
let kitty_keyboard_flags = Arc::new(AtomicU16::new(0));
let spawned = crate::pty::backend::spawn_with_portable_pty(rows, cols, cmd)
.inspect_err(|err| error!(pane = pane_id.raw(), err = %err, "{spawn_error_message}"))?;
// --- Child watcher task ---
let child_pid = Arc::new(AtomicU32::new(0));
let reported_cwd = Arc::new(Mutex::new(None));
let child_wait_completed = Arc::new(AtomicBool::new(false));
let input_write_seq = Arc::new(AtomicU64::new(0));
let pty_render_seq = Arc::new(AtomicU64::new(0));
{
let child_pid = child_pid.clone();
let child_wait_completed = child_wait_completed.clone();
let events = events.clone();
let rt = tokio::runtime::Handle::current();
let mut child = spawned.child;
if let Some(pid) = child.process_id() {
child_pid.store(pid, Ordering::Release);
crate::logging::pane_spawned(pane_id.raw(), pid);
}
tokio::task::spawn_blocking(move || {
match child.wait() {
Ok(status) => {
let status_text = format!("{status:?}");
crate::logging::pane_exited(pane_id.raw(), &status_text);
}
Err(e) => crate::logging::pane_exit_failed(pane_id.raw(), &e.to_string()),
}
child_wait_completed.store(true, Ordering::Release);
// 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");
}
});
}
let io = {
let terminal = terminal.clone();
let response_writer = response_tx.clone();
let render_notify = render_notify.clone();
let render_dirty = render_dirty.clone();
let pty_render_seq = pty_render_seq.clone();
let child_pid = child_pid.clone();
let events = events.clone();
let reported_cwd = reported_cwd.clone();
let rt = tokio::runtime::Handle::current();
let on_read = Box::new(move |bytes: &[u8]| {
let shell_pid = child_pid.load(Ordering::Acquire);
let result =
terminal.process_pty_bytes(pane_id, shell_pid, bytes, &response_writer);
if result.request_render {
pty_render_seq.fetch_add(1, Ordering::Relaxed);
if !render_dirty.swap(true, Ordering::AcqRel) {
render_notify.notify_one();
}
}
if let Some(delay) = result.render_delay {
let render_notify = render_notify.clone();
let render_dirty = render_dirty.clone();
rt.spawn(async move {
tokio::time::sleep(delay).await;
if !render_dirty.swap(true, Ordering::AcqRel) {
render_notify.notify_one();
}
});
}
if let Some(cwd) = result.reported_cwd.clone() {
publish_reported_cwd(pane_id, cwd, &reported_cwd, &events);
}
for content in result.clipboard_writes {
if let Err(err) = events.try_send(AppEvent::ClipboardWrite { content }) {
warn!(
pane = pane_id.raw(),
err = %err,
"failed to send OSC 52 clipboard write"
);
}
}
PtyReadResult {
terminal_responses: result.terminal_responses,
}
});
PaneRuntimeIo::Actor(PtyIoActor::spawn(PtyIoActorConfig {
pane_id: pane_id.raw(),
#[cfg(unix)]
master_fd: spawned.master_fd,
#[cfg(windows)]
master: spawned.master,
initially_quiesced: false,
on_read,
on_reader_exit: None,
})?)
};
// --- Detection task ---
let (detect_handle, detect_reset_notify, pending_release) = {
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 TICK_PENDING_RELEASE: Duration = Duration::from_millis(50);
let child_pid = child_pid.clone();
let terminal = terminal.clone();
let state_events = events.clone();
let pty_render_seq = pty_render_seq.clone();
let input_write_seq_for_task = input_write_seq.clone();
let render_notify = render_notify.clone();
let render_dirty = render_dirty.clone();
let detect_reset_notify = Arc::new(Notify::new());
let detect_reset = detect_reset_notify.clone();
let pending_release = Arc::new(Mutex::new(None));
let pending_release_for_task = pending_release.clone();
let handle = tokio::spawn(async move {
let mut agent_presence =
AgentDetectionPresence::from_agent(initial_state.detected_agent);
let mut state = AgentState::Idle;
let mut last_process_check = Instant::now();
let mut last_foreground_pgid = None;
let mut has_process_probe = false;
let mut acquisition_started_at = None;
let mut last_content_change_at = None;
let mut pending_foreground_shell_clear = false;
let mut foreground_shell_exit_reported = false;
let mut release_was_active = false;
let mut pending_restore_probe = initial_state.detected_agent.is_some();
let mut last_visible_blocker = false;
let mut last_visible_working = false;
let mut last_visible_signal_refresh = None;
let mut last_detection_text = String::new();
let mut pty_causality = PtyCausalityTracker::default();
let mut agent_startup_grace_until = None;
let mut pending_idle = PendingIdleConfirmation::default();
tokio::time::sleep(Duration::from_millis(50)).await;
loop {
let tick = if active_pending_release(&pending_release_for_task, Instant::now())
.is_some()
|| terminal.has_transient_default_color_override()
{
TICK_PENDING_RELEASE
} else if pending_idle.active() {
AGENT_PENDING_IDLE_RECHECK
} else if agent_presence.current_agent().is_none() {
TICK_UNIDENTIFIED
} else {
TICK_IDENTIFIED
};
tokio::select! {
_ = tokio::time::sleep(tick) => {}
_ = detect_reset.notified() => {
agent_presence = AgentDetectionPresence::from_agent(None);
state = AgentState::Unknown;
last_foreground_pgid = None;
has_process_probe = false;
acquisition_started_at = None;
last_content_change_at = None;
pending_foreground_shell_clear = false;
foreground_shell_exit_reported = false;
release_was_active = false;
pending_restore_probe = false;
last_visible_blocker = false;
last_visible_working = false;
last_visible_signal_refresh = None;
last_detection_text.clear();
pty_causality = PtyCausalityTracker::default();
agent_startup_grace_until = None;
pending_idle.clear();
}
}
let now = Instant::now();
let suppressed_agent = active_pending_release(&pending_release_for_task, now);
if suppressed_agent.is_none() && release_was_active {
has_process_probe = false;
acquisition_started_at = None;
last_content_change_at = None;
}
release_was_active = suppressed_agent.is_some();
let pid = child_pid.load(Ordering::Acquire);
let foreground_pgid = (pid > 0)
.then(|| detect::foreground_process_group_id(pid))
.flatten();
let process_group_changed =
foreground_group_changed(foreground_pgid, last_foreground_pgid);
let should_check_process = pid > 0
&& should_probe_foreground_job(ProcessProbeInput {
current_agent: agent_presence.current_agent(),
suppressed_agent,
foreground_pgid,
last_foreground_pgid,
has_process_probe,
acquisition_age: acquisition_started_at
.map(|started| now.duration_since(started)),
pending_foreground_shell_clear,
pending_restore_probe,
elapsed_since_process_check: now.duration_since(last_process_check),
});
let mut agent_changed = false;
let mut agent = agent_presence.current_agent();
if should_check_process {
last_process_check = now;
let had_process_probe = has_process_probe;
has_process_probe = true;
if pid > 0 {
let probe = probe_foreground_process(pid, foreground_pgid);
let process_name = probe.process_name;
let process_group_id = probe.process_group_id;
let foreground_is_pane_shell = probe.foreground_is_pane_shell;
let mut new_agent = probe.agent;
if let Some(suppressed_agent) = suppressed_agent {
if new_agent == Some(suppressed_agent) {
new_agent = None;
} else if let Ok(mut pending_release) =
pending_release_for_task.lock()
{
*pending_release = None;
}
}
let previous_agent = agent_presence.current_agent();
let changed = match foreground_shell_agent_action(
previous_agent,
new_agent,
foreground_is_pane_shell,
foreground_shell_exit_reported,
) {
ForegroundShellAgentAction::ReportProcessExit => {
pending_foreground_shell_clear = true;
false
}
ForegroundShellAgentAction::ClearAgent => {
pending_foreground_shell_clear = false;
foreground_shell_exit_reported = false;
agent_presence.clear_current_agent()
}
ForegroundShellAgentAction::ObserveProbe => {
pending_foreground_shell_clear = false;
foreground_shell_exit_reported = false;
agent_presence.observe_process_probe(new_agent)
}
};
if new_agent.is_some() {
last_foreground_pgid = process_group_id;
acquisition_started_at = None;
last_content_change_at = None;
pending_restore_probe = false;
} else if agent_presence.current_agent().is_none() {
last_foreground_pgid = process_group_id.or(foreground_pgid);
if had_process_probe && process_group_changed {
acquisition_started_at = Some(now);
}
pending_restore_probe = false;
} else {
last_foreground_pgid = process_group_id.or(foreground_pgid);
}
if changed {
agent = agent_presence.current_agent();
if agent != previous_agent {
pending_idle.clear();
if agent.is_some() {
agent_startup_grace_until =
Some(now + AGENT_STARTUP_GRACE_WINDOW);
baseline_pty_causality(
&mut pty_causality,
pty_render_seq.load(Ordering::Relaxed),
input_write_seq_for_task.load(Ordering::Relaxed),
);
state = AgentState::Idle;
last_visible_blocker = false;
last_visible_working = false;
last_visible_signal_refresh = None;
publish_state_changed_event(
state_events.clone(),
pane_id,
agent,
AgentState::Idle,
false,
false,
false,
now,
)
.await;
} else {
agent_startup_grace_until = None;
}
}
if let Some(process_name) = process_name {
info!(
pane = pane_id.raw(),
previous_agent = ?previous_agent,
?agent,
process = %process_name,
pgid = ?process_group_id,
"agent changed"
);
} else {
info!(
pane = pane_id.raw(),
previous_agent = ?previous_agent,
?agent,
pgid = ?process_group_id,
"agent changed"
);
}
agent_changed = true;
}
}
}
let pid = child_pid.load(Ordering::Acquire);
// Keep the terminal restore side effect separate from render notification state.
#[allow(clippy::collapsible_if)]
if pid > 0 && terminal.maybe_restore_host_terminal_theme(pane_id, pid) {
if !render_dirty.swap(true, Ordering::AcqRel) {
render_notify.notify_one();
}
}
let process_exited = pending_foreground_shell_clear
&& agent.is_some()
&& !foreground_shell_exit_reported;
if let Some(until) = agent_startup_grace_until {
if process_exited {
agent_startup_grace_until = None;
pending_idle.clear();
} else {
if now < until {
pending_idle.clear();
continue;
}
baseline_pty_causality(
&mut pty_causality,
pty_render_seq.load(Ordering::Relaxed),
input_write_seq_for_task.load(Ordering::Relaxed),
);
agent_startup_grace_until = None;
pending_idle.clear();
continue;
}
}
let content = terminal.detection_text();
let content_changed = content != last_detection_text;
last_detection_text.clone_from(&content);
if detect::should_skip_state_update(agent, &content) {
pending_idle.clear();
continue;
}
sync_content_change_acquisition(
agent_presence.current_agent(),
suppressed_agent,
process_group_changed,
content_changed,
now,
&mut acquisition_started_at,
&mut last_content_change_at,
);
let Some(screen_detection) =
detection_update_for_publish(agent, &content, process_exited)
else {
continue;
};
let pty_signal = if agent.is_some() {
let signal = agent_caused_pty_activity_active(
pty_render_seq.load(Ordering::Relaxed),
input_write_seq_for_task.load(Ordering::Relaxed),
&mut pty_causality,
now,
);
Some(crate::agent_detection_policy::PtySignal {
active: signal.active,
tainted: signal.tainted,
})
} else {
None
};
let detection = match crate::agent_detection_policy::apply_detection_policy(
crate::agent_detection_policy::DetectionPolicyInput {
agent,
screen_detection,
process_exited,
startup_grace_active: false,
pty_signal,
},
) {
crate::agent_detection_policy::DetectionPolicyDecision::Publish(
detection,
) => detection,
crate::agent_detection_policy::DetectionPolicyDecision::Freeze => {
pending_idle.clear();
continue;
}
};
let new_state = crate::terminal::state::stabilize_agent_detection(detection);
let visible_blocker =
detection.visible_blocker && new_state == AgentState::Blocked;
let visible_working =
detection.visible_working && new_state == AgentState::Working;
let previous_publish = DetectionPublishState {
state,
visible_blocker: last_visible_blocker,
visible_working: last_visible_working,
};
let next_publish = DetectionPublishState {
state: new_state,
visible_blocker,
visible_working,
};
let stable_refresh_due = stable_visible_signal_refresh_due(
previous_publish,
next_publish,
last_visible_signal_refresh,
now,
);
if pty_working_transition_is_vetoed(
agent,
previous_publish,
next_publish,
&content,
) {
pending_idle.clear();
continue;
}
if pending_idle.should_hold_working_to_idle(
previous_publish,
next_publish,
agent_changed,
process_exited,
now,
) {
continue;
}
if should_publish_detection_update(
previous_publish,
next_publish,
agent_changed,
process_exited,
stable_refresh_due,
) {
state = new_state;
last_visible_blocker = visible_blocker;
last_visible_working = visible_working;
if visible_blocker || visible_working {
last_visible_signal_refresh = Some(now);
} else {
last_visible_signal_refresh = None;
}
publish_state_changed_event(
state_events.clone(),
pane_id,
agent,
new_state,
visible_blocker,
visible_working,
process_exited,
now,
)
.await;
if process_exited {
foreground_shell_exit_reported = true;
}
}
}
});
(handle.abort_handle(), detect_reset_notify, pending_release)
};
Ok(Self {
pane_id,
terminal,
io,
current_size: Cell::new((rows, cols, 0, 0)),
child_pid,
reported_cwd,
child_wait_completed: Some(child_wait_completed),
kitty_keyboard_flags,
input_write_seq,
detect_reset_notify,
pending_release,
preserve_processes_on_drop: false,
detect_handle,
})
}
pub fn begin_graceful_release(&self, agent: Agent) {
if let Ok(mut pending_release) = self.pending_release.lock() {
*pending_release = Some(PendingAgentRelease {
agent,
until: std::time::Instant::now() + RELEASE_REACQUIRE_SUPPRESSION,
});
}
self.detect_reset_notify.notify_one();
}
pub(crate) fn current_size(&self) -> (u16, u16) {
let (rows, cols, _, _) = self.current_size.get();
(rows, cols)
}
/// Resize if the dimensions actually changed.
pub fn resize(&self, rows: u16, cols: u16, cell_width_px: u32, cell_height_px: u32) {
let rows = rows.max(2);
let cols = cols.max(4);
let size = (rows, cols, cell_width_px, cell_height_px);
if self.current_size.get() == size {
return;
}
self.input_write_seq.fetch_add(1, Ordering::Relaxed);
self.current_size.set(size);
let terminal_responses = self
.terminal
.resize(rows, cols, cell_width_px, cell_height_px);
self.io.resize(
rows,
cols,
cell_width_px,
cell_height_px,
terminal_responses,
);
}
#[cfg(unix)]
pub fn nudge_child_redraw_after_handoff(&self) {
self.input_write_seq.fetch_add(1, Ordering::Relaxed);
let (rows, cols, cell_width_px, cell_height_px) = self.current_size.get();
self.io
.nudge_child_redraw_after_handoff(rows, cols, cell_width_px, cell_height_px);
}
/// Scroll up by N lines (into scrollback history).
pub fn scroll_up(&self, lines: usize) {
self.terminal.scroll_up(lines);
}
/// Scroll down by N lines (toward live output).
pub fn scroll_down(&self, lines: usize) {
self.terminal.scroll_down(lines);
}
/// Reset scroll to live view (offset = 0).
pub fn scroll_reset(&self) {
self.terminal.scroll_reset();
}
/// Set scrollback offset measured from the live bottom of the terminal.
pub fn set_scroll_offset_from_bottom(&self, lines: usize) {
self.terminal.set_scroll_offset_from_bottom(lines);
}
pub fn scroll_metrics(&self) -> Option<ScrollMetrics> {
self.terminal.scroll_metrics()
}
pub fn input_state(&self) -> Option<InputState> {
self.terminal.input_state()
}
pub fn cursor_state(&self, area: Rect, show_cursor: bool) -> Option<TerminalCursorState> {
if !show_cursor {
return None;
}
let cursor = self.terminal.cursor_state()?;
if cursor.x >= area.width || cursor.y >= area.height {
return None;
}
Some(TerminalCursorState {
x: area.x + cursor.x,
y: area.y + cursor.y,
visible: cursor.visible,
shape: cursor.shape,
})
}
pub fn synchronized_output_active(&self) -> bool {
self.terminal.synchronized_output_active()
}
pub fn visible_text(&self) -> String {
self.terminal.visible_text()
}
pub fn visible_ansi(&self) -> String {
self.terminal.visible_ansi()
}
pub fn recent_text(&self, lines: usize) -> String {
self.terminal.recent_text(lines)
}
pub fn recent_ansi(&self, lines: usize) -> String {
self.terminal.recent_ansi(lines)
}
pub fn recent_unwrapped_text(&self, lines: usize) -> String {
self.terminal.recent_unwrapped_text(lines)
}
pub fn recent_unwrapped_ansi(&self, lines: usize) -> String {
self.terminal.recent_unwrapped_ansi(lines)
}
pub fn snapshot_history(&self) -> Option<String> {
let ansi = self.recent_unwrapped_ansi(usize::MAX);
(!ansi.trim().is_empty()).then_some(ansi)
}
pub fn extract_selection(&self, selection: &crate::selection::Selection) -> Option<String> {
self.terminal.extract_selection(selection)
}
pub fn render(&self, frame: &mut Frame, area: Rect, show_cursor: bool) {
self.terminal.render(frame, area, show_cursor);
}
pub(crate) fn collect_dirty_patch(
&self,
area_width: u16,
area_height: u16,
) -> TerminalDirtyPatchOutcome {
self.terminal.collect_dirty_patch(area_width, area_height)
}
pub fn visible_hyperlinks(&self, area: Rect) -> Vec<((u16, u16), String, String)> {
self.terminal.visible_hyperlinks(area)
}
pub fn kitty_image_placements_with_data_filter<F>(
&self,
needs_data: F,
) -> Vec<crate::ghostty::KittyImagePlacement>
where
F: FnMut(crate::ghostty::KittyImageDescriptor) -> bool,
{
self.terminal
.kitty_image_placements_with_data_filter(needs_data)
}
pub fn keyboard_protocol(&self) -> crate::input::KeyboardProtocol {
let fallback = crate::input::KeyboardProtocol::from_kitty_flags(
self.kitty_keyboard_flags.load(Ordering::Relaxed),
);
self.terminal.keyboard_protocol(fallback)
}
pub fn encode_terminal_key(&self, key: crate::input::TerminalKey) -> Vec<u8> {
self.terminal
.encode_terminal_key(key, self.keyboard_protocol())
}
pub async fn send_bytes(&self, bytes: Bytes) -> Result<(), mpsc::error::SendError<Bytes>> {
let result = self.io.send_bytes(bytes).await;
if result.is_ok() {
self.input_write_seq.fetch_add(1, Ordering::Relaxed);
}
result
}
pub fn try_send_bytes(&self, bytes: Bytes) -> Result<(), mpsc::error::TrySendError<Bytes>> {
let result = self.io.try_send_bytes(bytes);
if result.is_ok() {
self.input_write_seq.fetch_add(1, Ordering::Relaxed);
}
result
}
pub async fn send_paste(&self, text: String) -> Result<(), mpsc::error::SendError<Bytes>> {
let bracketed = self
.input_state()
.map(|state| state.bracketed_paste)
.unwrap_or(false);
let payload = if bracketed {
format!("\x1b[200~{text}\x1b[201~")
} else {
text
};
self.send_bytes(Bytes::from(payload)).await
}
pub fn try_send_focus_event(&self, event: crate::ghostty::FocusEvent) -> bool {
if !self
.input_state()
.map(|state| state.focus_reporting)
.unwrap_or(false)
{
return false;
}
let Ok(bytes) = crate::ghostty::encode_focus(event) else {
return false;
};
if let Err(err) = self.try_send_bytes(Bytes::from(bytes)) {
warn!(err = %err, ?event, "failed to forward pane focus event");
}
true
}
pub fn wheel_routing(&self) -> Option<WheelRouting> {
self.terminal.wheel_routing()
}
pub fn encode_mouse_button(
&self,
kind: crossterm::event::MouseEventKind,
column: u16,
row: u16,
modifiers: crossterm::event::KeyModifiers,
) -> Option<Vec<u8>> {
if !self.input_state()?.mouse_protocol_mode.reporting_enabled() {
return None;
}
self.terminal
.encode_mouse_button(kind, column, row, modifiers)
}
pub fn encode_mouse_motion(
&self,
kind: crossterm::event::MouseEventKind,
column: u16,
row: u16,
modifiers: crossterm::event::KeyModifiers,
) -> Option<Vec<u8>> {
self.terminal
.encode_mouse_motion(kind, column, row, modifiers)
}
pub fn encode_mouse_wheel(
&self,
kind: crossterm::event::MouseEventKind,
column: u16,
row: u16,
modifiers: crossterm::event::KeyModifiers,
) -> Option<Vec<u8>> {
if self.wheel_routing()? != WheelRouting::MouseReport {
return None;
}
self.terminal
.encode_mouse_wheel(kind, column, row, modifiers)
}
pub fn encode_alternate_scroll(
&self,
kind: crossterm::event::MouseEventKind,
) -> Option<Vec<u8>> {
self.input_state()?;
if self.wheel_routing()? != WheelRouting::AlternateScroll {
return None;
}
let key = match kind {
crossterm::event::MouseEventKind::ScrollUp => crossterm::event::KeyCode::Up,
crossterm::event::MouseEventKind::ScrollDown => crossterm::event::KeyCode::Down,
_ => return None,
};
Some(self.encode_terminal_key(crate::input::TerminalKey::new(
key,
crossterm::event::KeyModifiers::empty(),
)))
}
/// Get the current working directory of the child shell process.
pub fn cwd(&self) -> Option<std::path::PathBuf> {
if let Some(cwd) = self
.reported_cwd
.lock()
.ok()
.and_then(|reported_cwd| reported_cwd.clone())
.and_then(usable_reported_cwd)
{
return Some(cwd);
}
let pid = self.child_pid.load(Ordering::Relaxed);
crate::platform::process_cwd(pid)
}
/// Get the current working directory of the process group controlling the pane PTY.
pub fn foreground_cwd(&self) -> Option<std::path::PathBuf> {
#[cfg(unix)]
{
let pid = self.child_pid.load(Ordering::Acquire);
let shell_cwd = usable_process_cwd(pid);
let foreground_pgid = self
.io
.foreground_process_group_id()
.or_else(|| crate::platform::foreground_process_group_id(pid));
let leader_cwd = foreground_pgid.and_then(usable_process_cwd);
if leader_cwd.as_ref() == shell_cwd.as_ref() {
foreground_member_cwd_different_from_shell(pid, shell_cwd.as_ref()).or(leader_cwd)
} else {
leader_cwd
.or_else(|| foreground_member_cwd_different_from_shell(pid, shell_cwd.as_ref()))
}
}
#[cfg(not(unix))]
{
None
}
}
}
#[cfg(test)]
impl PaneRuntime {
pub(crate) fn test_with_channel(cols: u16, rows: u16) -> (Self, mpsc::Receiver<Bytes>) {
Self::test_with_channel_and_scrollback_bytes(cols, rows, 0, &[], 4)
}
pub(crate) fn test_with_channel_capacity(
cols: u16,
rows: u16,
capacity: usize,
) -> (Self, mpsc::Receiver<Bytes>) {
Self::test_with_channel_and_scrollback_bytes(cols, rows, 0, &[], capacity)
}
pub(crate) fn test_with_screen_bytes(cols: u16, rows: u16, bytes: &[u8]) -> Self {
Self::test_with_scrollback_bytes(cols, rows, 0, bytes)
}
pub(crate) fn test_process_pty_bytes(&self, bytes: &[u8]) {
let (tx, _rx) = mpsc::channel(1);
let _ = self.terminal.process_pty_bytes(self.pane_id, 0, bytes, &tx);
}
pub(crate) fn test_with_scrollback_bytes(
cols: u16,
rows: u16,
scrollback_limit_bytes: usize,
bytes: &[u8],
) -> Self {
Self::test_with_channel_and_scrollback_bytes(cols, rows, scrollback_limit_bytes, bytes, 4).0
}
pub(crate) fn test_with_channel_and_scrollback_bytes(
cols: u16,
rows: u16,
scrollback_limit_bytes: usize,
bytes: &[u8],
channel_capacity: usize,
) -> (Self, mpsc::Receiver<Bytes>) {
let (tx, rx) = mpsc::channel(channel_capacity);
let (resize_tx, _resize_rx) = watch::channel((rows, cols, 0, 0));
let mut terminal =
crate::ghostty::Terminal::new(cols, rows, scrollback_limit_bytes).unwrap();
terminal.write(bytes);
(
Self {
pane_id: PaneId::from_raw(0),
terminal: Arc::new(PaneTerminal::new(
GhosttyPaneTerminal::new(terminal, tx.clone()).unwrap(),
)),
io: PaneRuntimeIo::TestChannel {
sender: tx,
resize_tx,
},
current_size: Cell::new((rows, cols, 0, 0)),
child_pid: Arc::new(AtomicU32::new(0)),
reported_cwd: Arc::new(Mutex::new(None)),
child_wait_completed: None,
kitty_keyboard_flags: Arc::new(AtomicU16::new(0)),
input_write_seq: Arc::new(AtomicU64::new(0)),
detect_reset_notify: Arc::new(Notify::new()),
pending_release: Arc::new(Mutex::new(None)),
preserve_processes_on_drop: true,
detect_handle: tokio::spawn(async {}).abort_handle(),
},
rx,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shutdown_liveness_treats_reaped_direct_child_as_gone() {
assert!(!process_alive_for_shutdown(42, 42, true, |_| true));
}
#[test]
fn shutdown_liveness_keeps_unreaped_direct_child_alive() {
assert!(process_alive_for_shutdown(42, 42, false, |_| true));
}
#[test]
fn shutdown_liveness_keeps_other_session_processes_alive() {
assert!(process_alive_for_shutdown(43, 42, true, |_| true));
}
#[test]
fn shutdown_liveness_treats_missing_process_as_gone() {
assert!(!process_alive_for_shutdown(43, 42, false, |_| false));
}
#[cfg(unix)]
fn capture_shell_output(command: &str, extra_env: &[(&str, &str)]) -> String {
let pair = native_pty_system()
.openpty(PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
})
.unwrap();
let output_path = std::env::temp_dir().join(format!(
"herdr-pane-term-test-{}-{}.txt",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let mut cmd = CommandBuilder::new("/bin/sh");
cmd.arg("-c");
cmd.arg(format!("{command} > '{}'", output_path.display()));
cmd.cwd(std::env::current_dir().unwrap());
cmd.env("TERM", "xterm-ghostty");
cmd.env("COLORTERM", "falsecolor");
apply_pane_terminal_env(&mut cmd);
for (key, value) in extra_env {
cmd.env(key, value);
}
let mut child = pair.slave.spawn_command(cmd).unwrap();
let status = child.wait().unwrap();
assert!(status.success(), "shell command failed: {status:?}");
let output = std::fs::read_to_string(&output_path).unwrap();
let _ = std::fs::remove_file(output_path);
output
}
#[test]
fn pane_shell_prefers_configured_shell() {
assert_eq!(
pane_shell_from("/usr/bin/nu", Some("/bin/bash".to_string())),
"/usr/bin/nu"
);
}
#[cfg(not(windows))]
#[test]
fn pane_shell_falls_back_to_shell_env() {
assert_eq!(
pane_shell_from("", Some("/bin/bash".to_string())),
"/bin/bash"
);
}
#[cfg(windows)]
#[test]
fn pane_shell_ignores_shell_env_on_windows() {
assert_eq!(
pane_shell_from("", Some("c:\\windows\\system32\\cmd.exe".to_string())),
default_pane_shell()
);
}
#[test]
fn pane_shell_ignores_empty_values() {
assert_eq!(
pane_shell_from(" ", Some(" ".to_string())),
default_pane_shell()
);
assert_eq!(pane_shell_from("", None), default_pane_shell());
}
#[test]
fn shell_mode_auto_uses_login_shell_only_on_macos() {
assert!(shell_mode_uses_login_shell(
crate::config::ShellModeConfig::Auto,
true
));
assert!(!shell_mode_uses_login_shell(
crate::config::ShellModeConfig::Auto,
false
));
assert!(shell_mode_uses_login_shell(
crate::config::ShellModeConfig::Login,
false
));
assert!(!shell_mode_uses_login_shell(
crate::config::ShellModeConfig::NonLogin,
true
));
}
#[cfg(unix)]
#[test]
fn login_shell_builder_uses_default_prog_with_resolved_shell_env() {
let cmd = pane_shell_command_builder_for_target(
PaneShellConfig::new("/bin/sh", crate::config::ShellModeConfig::Login),
false,
)
.unwrap();
assert!(cmd.is_default_prog());
assert_eq!(
cmd.get_env("SHELL").and_then(std::ffi::OsStr::to_str),
Some("/bin/sh")
);
}
#[cfg(unix)]
#[test]
fn auto_shell_builder_uses_login_shell_on_macos_target() {
let cmd = pane_shell_command_builder_for_target(
PaneShellConfig::new("/bin/sh", crate::config::ShellModeConfig::Auto),
true,
)
.unwrap();
assert!(cmd.is_default_prog());
assert_eq!(
cmd.get_env("SHELL").and_then(std::ffi::OsStr::to_str),
Some("/bin/sh")
);
}
#[test]
fn auto_shell_builder_keeps_direct_shell_on_non_macos_target() {
let cmd = pane_shell_command_builder_for_target(
PaneShellConfig::new("/bin/sh", crate::config::ShellModeConfig::Auto),
false,
)
.unwrap();
assert!(!cmd.is_default_prog());
assert_eq!(cmd.get_argv(), &[std::ffi::OsString::from("/bin/sh")]);
}
#[cfg(windows)]
#[test]
fn windows_powershell_shell_builder_wraps_cwd_reporting_prompt() {
let cmd = pane_shell_command_builder_for_target(
PaneShellConfig::new("powershell.exe", crate::config::ShellModeConfig::NonLogin),
false,
)
.unwrap();
let argv: Vec<_> = cmd
.get_argv()
.iter()
.map(|arg| arg.to_string_lossy().into_owned())
.collect();
assert_eq!(argv[0], "powershell.exe");
assert!(argv.iter().any(|arg| arg == "-NoExit"));
assert!(argv
.iter()
.any(|arg| arg.contains("]7;") && arg.contains("Function:\\prompt")));
}
#[test]
fn login_shell_builder_rejects_missing_shell_instead_of_falling_back() {
let err = pane_shell_command_builder_for_target(
PaneShellConfig::new(
"/__herdr_missing_shell__",
crate::config::ShellModeConfig::Login,
),
false,
)
.unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::NotFound);
}
#[cfg(unix)]
#[test]
fn login_shell_builder_resolves_bare_shell_names_from_path() {
let _lock = crate::integration::integration_env_lock();
let base = std::env::temp_dir().join(format!(
"herdr-login-shell-path-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let bin = base.join("bin");
std::fs::create_dir_all(&bin).unwrap();
let shell = bin.join("fake-shell");
std::fs::write(&shell, "#!/bin/sh\nexit 0\n").unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&shell, std::fs::Permissions::from_mode(0o755)).unwrap();
}
let original_path = std::env::var_os("PATH");
std::env::set_var("PATH", &bin);
let cmd = pane_shell_command_builder_for_target(
PaneShellConfig::new("fake-shell", crate::config::ShellModeConfig::Login),
false,
)
.unwrap();
assert!(cmd.is_default_prog());
assert_eq!(
cmd.get_env("SHELL").and_then(std::ffi::OsStr::to_str),
shell.to_str()
);
match original_path {
Some(path) => std::env::set_var("PATH", path),
None => std::env::remove_var("PATH"),
}
let _ = std::fs::remove_dir_all(base);
}
#[cfg(unix)]
#[test]
fn login_shell_resolution_preserves_shell_paths() {
assert_eq!(resolve_shell_for_login_mode("/bin/sh").unwrap(), "/bin/sh");
}
#[test]
fn non_login_shell_builder_execs_resolved_shell_directly() {
let cmd = pane_shell_command_builder(PaneShellConfig::new(
"/bin/sh",
crate::config::ShellModeConfig::NonLogin,
))
.unwrap();
assert!(!cmd.is_default_prog());
assert_eq!(cmd.get_argv(), &[std::ffi::OsString::from("/bin/sh")]);
}
#[cfg(unix)]
#[test]
fn pane_terminal_identity_overrides_outer_terminal_env() {
let output = capture_shell_output("printf '%s\\n%s\\n' \"$TERM\" \"$COLORTERM\"", &[]);
assert_eq!(output, "xterm-256color\ntruecolor\n");
}
#[cfg(unix)]
#[test]
fn pane_terminal_identity_allows_explicit_override() {
let output = capture_shell_output(
"printf '%s\\n%s\\n' \"$TERM\" \"$COLORTERM\"",
&[("TERM", "vt100"), ("COLORTERM", "24bit")],
);
assert_eq!(output, "vt100\n24bit\n");
}
#[cfg(unix)]
#[tokio::test]
async fn handoff_history_ansi_captures_primary_screen() {
let runtime =
PaneRuntime::test_with_scrollback_bytes(40, 5, 4096, b"handoff-primary-history\r\n");
let history = runtime.handoff_history_ansi().unwrap();
assert!(history.contains("handoff-primary-history"));
}
#[cfg(unix)]
#[tokio::test]
async fn handoff_history_ansi_skips_alternate_screen() {
let runtime = PaneRuntime::test_with_scrollback_bytes(
40,
5,
4096,
b"primary\r\n\x1b[?1049halt-screen",
);
assert!(runtime.handoff_history_ansi().is_none());
}
#[cfg(unix)]
#[tokio::test]
async fn handoff_runtime_state_captures_terminal_input_state() {
let runtime = PaneRuntime::test_with_screen_bytes(
80,
24,
b"\x1b[>5u\x1b[>4;2m\x1b[?1h\x1b[?2004h\x1b[?1004h\x1b[?1002h\x1b[?1006h",
);
let pane = runtime.handoff_runtime_state(12);
assert_eq!(pane.keyboard_protocol_flags, 5);
assert_eq!(
pane.input_state,
Some(InputState {
alternate_screen: false,
application_cursor: true,
bracketed_paste: true,
focus_reporting: true,
mouse_protocol_mode: crate::input::MouseProtocolMode::ButtonMotion,
mouse_protocol_encoding: crate::input::MouseProtocolEncoding::Sgr,
mouse_alternate_scroll: true,
modify_other_keys: true,
})
);
}
#[cfg(unix)]
#[test]
fn truncate_handoff_history_keeps_recent_utf8_boundary() {
let history = format!("old\n{}\nrecent\n", "é".repeat(8));
let truncated = truncate_handoff_history(history, 20);
assert_eq!(truncated, "recent\n");
assert!(truncated.is_char_boundary(0));
}
#[cfg(unix)]
#[test]
fn truncate_handoff_history_drops_partial_long_line() {
let history = format!("old\n{}", "x".repeat(64));
let truncated = truncate_handoff_history(history, 12);
assert!(truncated.is_empty());
}
#[tokio::test]
async fn focus_events_are_forwarded_when_enabled() {
let (tx, mut rx) = mpsc::channel(4);
let (resize_tx, _resize_rx) = watch::channel((80, 24, 0, 0));
let mut terminal = crate::ghostty::Terminal::new(80, 24, 0).unwrap();
terminal
.mode_set(crate::ghostty::MODE_FOCUS_EVENT, true)
.unwrap();
let runtime = PaneRuntime {
pane_id: PaneId::from_raw(0),
terminal: Arc::new(PaneTerminal::new(
GhosttyPaneTerminal::new(terminal, tx.clone()).unwrap(),
)),
io: PaneRuntimeIo::TestChannel {
sender: tx,
resize_tx,
},
current_size: Cell::new((80, 24, 0, 0)),
child_pid: Arc::new(AtomicU32::new(0)),
reported_cwd: Arc::new(Mutex::new(None)),
child_wait_completed: None,
kitty_keyboard_flags: Arc::new(AtomicU16::new(0)),
input_write_seq: Arc::new(AtomicU64::new(0)),
detect_reset_notify: Arc::new(Notify::new()),
pending_release: Arc::new(Mutex::new(None)),
preserve_processes_on_drop: true,
detect_handle: tokio::spawn(async {}).abort_handle(),
};
assert!(runtime.try_send_focus_event(crate::ghostty::FocusEvent::Gained));
assert_eq!(rx.recv().await.unwrap(), Bytes::from_static(b"\x1b[I"));
}
#[tokio::test]
async fn focus_events_are_suppressed_when_disabled() {
let (tx, mut rx) = mpsc::channel(4);
let (resize_tx, _resize_rx) = watch::channel((80, 24, 0, 0));
let terminal = crate::ghostty::Terminal::new(80, 24, 0).unwrap();
let runtime = PaneRuntime {
pane_id: PaneId::from_raw(0),
terminal: Arc::new(PaneTerminal::new(
GhosttyPaneTerminal::new(terminal, tx.clone()).unwrap(),
)),
io: PaneRuntimeIo::TestChannel {
sender: tx,
resize_tx,
},
current_size: Cell::new((80, 24, 0, 0)),
child_pid: Arc::new(AtomicU32::new(0)),
reported_cwd: Arc::new(Mutex::new(None)),
child_wait_completed: None,
kitty_keyboard_flags: Arc::new(AtomicU16::new(0)),
input_write_seq: Arc::new(AtomicU64::new(0)),
detect_reset_notify: Arc::new(Notify::new()),
pending_release: Arc::new(Mutex::new(None)),
preserve_processes_on_drop: true,
detect_handle: tokio::spawn(async {}).abort_handle(),
};
assert!(!runtime.try_send_focus_event(crate::ghostty::FocusEvent::Gained));
assert!(
tokio::time::timeout(std::time::Duration::from_millis(10), rx.recv())
.await
.is_err()
);
}
#[test]
fn foreground_shell_without_agent_is_immediate_clear_signal() {
assert!(should_clear_agent_for_foreground_shell(
Some(Agent::Claude),
None,
true
));
}
#[test]
fn foreground_shell_reports_process_exit_before_clearing_agent() {
assert_eq!(
foreground_shell_agent_action(Some(Agent::Codex), None, true, false),
ForegroundShellAgentAction::ReportProcessExit
);
assert_eq!(
foreground_shell_agent_action(Some(Agent::Codex), None, true, true),
ForegroundShellAgentAction::ClearAgent
);
}
#[test]
fn codex_transcript_viewer_suppresses_prompt_idle_publish() {
let content = "/ T R A N S C R I P T /\n\n› yeah go ahead\n────────────────────────────────────────────────────────────────────────────────── 100% ─\n ↑/↓ to scroll pgup/pgdn to page home/end to jump\n q to quit esc to edit prev";
assert!(detection_update_for_publish(Some(Agent::Codex), content, false).is_none());
}
#[test]
fn codex_transcript_viewer_suppresses_process_exit_idle_publish() {
let content = "/ T R A N S C R I P T /\n\n› yeah go ahead\n────────────────────────────────────────────────────────────────────────────────── 100% ─\n ↑/↓ to scroll pgup/pgdn to page home/end to jump\n q to quit esc to edit prev";
assert!(detection_update_for_publish(Some(Agent::Codex), content, true).is_none());
}
#[test]
fn process_exit_without_transcript_still_reports_idle() {
let detection =
detection_update_for_publish(Some(Agent::Codex), "Codex finished\n› ", true)
.expect("process exit should publish idle outside transcript viewer");
assert_eq!(detection.state, AgentState::Idle);
assert!(!detection.skip_state_update);
}
#[test]
fn stable_plain_idle_does_not_republish() {
let now = std::time::Instant::now();
let previous = DetectionPublishState {
state: AgentState::Idle,
visible_blocker: false,
visible_working: false,
};
let refresh_due = stable_visible_signal_refresh_due(
previous,
previous,
Some(now - STABLE_VISIBLE_SIGNAL_REFRESH),
now,
);
assert!(!should_publish_detection_update(
previous,
previous,
false,
false,
refresh_due
));
}
#[test]
fn stable_visible_working_does_not_republish() {
let now = std::time::Instant::now();
let previous = DetectionPublishState {
state: AgentState::Working,
visible_blocker: false,
visible_working: true,
};
let refresh_due = stable_visible_signal_refresh_due(
previous,
previous,
Some(now - STABLE_VISIBLE_SIGNAL_REFRESH),
now,
);
assert!(!should_publish_detection_update(
previous,
previous,
false,
false,
refresh_due
));
}
#[test]
fn stable_visible_blocker_republishes_for_hook_override_refresh() {
let now = std::time::Instant::now();
let previous = DetectionPublishState {
state: AgentState::Blocked,
visible_blocker: true,
visible_working: false,
};
let refresh_due = stable_visible_signal_refresh_due(
previous,
previous,
Some(now - STABLE_VISIBLE_SIGNAL_REFRESH),
now,
);
assert!(should_publish_detection_update(
previous,
previous,
false,
false,
refresh_due
));
}
#[test]
fn pending_idle_holds_working_to_plain_idle_until_confirmed() {
let now = std::time::Instant::now();
let previous = DetectionPublishState {
state: AgentState::Working,
visible_blocker: false,
visible_working: false,
};
let next = DetectionPublishState {
state: AgentState::Idle,
visible_blocker: false,
visible_working: false,
};
let mut pending = PendingIdleConfirmation::default();
assert!(pending.should_hold_working_to_idle(previous, next, false, false, now));
assert!(pending.should_hold_working_to_idle(
previous,
next,
false,
false,
now + AGENT_PENDING_IDLE_RECHECK
));
assert!(pending.should_hold_working_to_idle(
previous,
next,
false,
false,
now + AGENT_PENDING_IDLE_RECHECK * 2
));
assert!(!pending.should_hold_working_to_idle(
previous,
next,
false,
false,
now + AGENT_PENDING_IDLE_RECHECK * 3
));
}
#[test]
fn pending_idle_cap_publishes_idle_when_still_quiet() {
let now = std::time::Instant::now();
let previous = DetectionPublishState {
state: AgentState::Working,
visible_blocker: false,
visible_working: false,
};
let next = DetectionPublishState {
state: AgentState::Idle,
visible_blocker: false,
visible_working: false,
};
let mut pending = PendingIdleConfirmation::default();
assert!(pending.should_hold_working_to_idle(previous, next, false, false, now));
assert!(!pending.should_hold_working_to_idle(
previous,
next,
false,
false,
now + AGENT_PENDING_IDLE_CAP
));
}
#[test]
fn pending_idle_clears_when_work_resumes_or_blocker_appears() {
let now = std::time::Instant::now();
let previous = DetectionPublishState {
state: AgentState::Working,
visible_blocker: false,
visible_working: false,
};
let idle = DetectionPublishState {
state: AgentState::Idle,
visible_blocker: false,
visible_working: false,
};
let working = DetectionPublishState {
state: AgentState::Working,
visible_blocker: false,
visible_working: false,
};
let blocked = DetectionPublishState {
state: AgentState::Blocked,
visible_blocker: true,
visible_working: false,
};
let mut pending = PendingIdleConfirmation::default();
assert!(pending.should_hold_working_to_idle(previous, idle, false, false, now));
assert!(pending.active());
assert!(!pending.should_hold_working_to_idle(previous, working, false, false, now));
assert!(!pending.active());
assert!(pending.should_hold_working_to_idle(previous, idle, false, false, now));
assert!(!pending.should_hold_working_to_idle(previous, blocked, false, false, now));
assert!(!pending.active());
}
#[test]
fn claude_recap_veto_only_applies_to_idle_to_working() {
let screen =
"※ recap: Done. (disable recaps in /config)\n\n─────────────\n❯ \n─────────────";
let idle = DetectionPublishState {
state: AgentState::Idle,
visible_blocker: false,
visible_working: false,
};
let working = DetectionPublishState {
state: AgentState::Working,
visible_blocker: false,
visible_working: false,
};
assert!(pty_working_transition_is_vetoed(
Some(Agent::Claude),
idle,
working,
screen
));
assert!(!pty_working_transition_is_vetoed(
Some(Agent::Claude),
working,
idle,
screen
));
assert!(!pty_working_transition_is_vetoed(
Some(Agent::Codex),
idle,
working,
screen
));
}
#[test]
fn unknown_non_shell_foreground_job_is_not_immediate_clear_signal() {
assert!(!should_clear_agent_for_foreground_shell(
Some(Agent::Claude),
None,
false
));
}
#[test]
fn foreground_agent_job_is_not_clear_signal() {
assert!(!should_clear_agent_for_foreground_shell(
Some(Agent::Claude),
Some(Agent::OpenCode),
true
));
}
fn process_probe_input() -> ProcessProbeInput {
ProcessProbeInput {
current_agent: None,
suppressed_agent: None,
foreground_pgid: Some(42),
last_foreground_pgid: Some(42),
has_process_probe: true,
acquisition_age: None,
pending_foreground_shell_clear: false,
pending_restore_probe: false,
elapsed_since_process_check: std::time::Duration::from_secs(1),
}
}
#[test]
fn unchanged_unidentified_foreground_group_skips_full_process_probe() {
assert!(!should_probe_foreground_job(process_probe_input()));
}
#[test]
fn unidentified_foreground_group_change_runs_full_process_probe() {
assert!(should_probe_foreground_job(ProcessProbeInput {
foreground_pgid: Some(43),
..process_probe_input()
}));
}
#[test]
fn unidentified_pane_gets_initial_process_probe() {
assert!(should_probe_foreground_job(ProcessProbeInput {
has_process_probe: false,
..process_probe_input()
}));
}
#[test]
fn stable_unidentified_foreground_group_has_no_safety_process_probe() {
assert!(!should_probe_foreground_job(ProcessProbeInput {
elapsed_since_process_check: PROCESS_RECHECK_MISSING_FOREGROUND_GROUP,
..process_probe_input()
}));
}
#[test]
fn unidentified_pane_without_foreground_group_uses_safety_process_probe() {
assert!(!should_probe_foreground_job(ProcessProbeInput {
foreground_pgid: None,
last_foreground_pgid: None,
..process_probe_input()
}));
assert!(should_probe_foreground_job(ProcessProbeInput {
foreground_pgid: None,
last_foreground_pgid: None,
elapsed_since_process_check: PROCESS_RECHECK_MISSING_FOREGROUND_GROUP,
..process_probe_input()
}));
}
#[test]
fn unidentified_pane_probes_when_foreground_group_disappears() {
assert!(should_probe_foreground_job(ProcessProbeInput {
foreground_pgid: None,
last_foreground_pgid: Some(42),
..process_probe_input()
}));
}
#[test]
fn pending_shell_clear_and_restore_force_process_probes() {
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: Some(Agent::Codex),
pending_foreground_shell_clear: true,
..process_probe_input()
}));
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: Some(Agent::Codex),
pending_restore_probe: true,
..process_probe_input()
}));
}
#[test]
fn pending_release_forces_initial_process_probe() {
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: Some(Agent::Codex),
suppressed_agent: Some(Agent::Codex),
has_process_probe: false,
..process_probe_input()
}));
}
#[test]
fn pending_release_forces_process_probe_after_runtime_identity_clears() {
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: None,
suppressed_agent: Some(Agent::Codex),
has_process_probe: false,
..process_probe_input()
}));
}
#[test]
fn pending_release_skips_repeated_probe_when_foreground_group_is_stable() {
assert!(!should_probe_foreground_job(ProcessProbeInput {
current_agent: None,
suppressed_agent: Some(Agent::Codex),
..process_probe_input()
}));
}
#[test]
fn pending_release_probes_when_foreground_group_changes() {
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: None,
suppressed_agent: Some(Agent::Codex),
foreground_pgid: Some(43),
..process_probe_input()
}));
}
#[test]
fn acquisition_window_catches_delayed_same_group_wrapper_startup() {
assert!(!should_probe_foreground_job(ProcessProbeInput {
current_agent: None,
acquisition_age: Some(std::time::Duration::from_millis(1250)),
elapsed_since_process_check: PROCESS_ACQUISITION_FAST_RECHECK
- std::time::Duration::from_millis(1),
..process_probe_input()
}));
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: None,
acquisition_age: Some(std::time::Duration::from_millis(1250)),
elapsed_since_process_check: PROCESS_ACQUISITION_FAST_RECHECK,
..process_probe_input()
}));
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: None,
acquisition_age: Some(std::time::Duration::from_secs(5)),
elapsed_since_process_check: PROCESS_ACQUISITION_SLOW_RECHECK,
..process_probe_input()
}));
assert!(!should_probe_foreground_job(ProcessProbeInput {
current_agent: None,
acquisition_age: Some(PROCESS_ACQUISITION_WINDOW + std::time::Duration::from_millis(1),),
elapsed_since_process_check: PROCESS_ACQUISITION_SLOW_RECHECK,
..process_probe_input()
}));
}
#[test]
fn content_change_starts_bounded_unidentified_acquisition_window() {
let now = std::time::Instant::now();
let mut acquisition_started_at = None;
let mut last_content_change_at = None;
sync_content_change_acquisition(
None,
None,
false,
true,
now,
&mut acquisition_started_at,
&mut last_content_change_at,
);
assert_eq!(acquisition_started_at, Some(now));
assert_eq!(last_content_change_at, Some(now));
let later = now + std::time::Duration::from_secs(1);
sync_content_change_acquisition(
None,
None,
false,
true,
later,
&mut acquisition_started_at,
&mut last_content_change_at,
);
assert_eq!(
acquisition_started_at,
Some(now),
"changed frames should not refresh the acquisition window"
);
assert_eq!(last_content_change_at, Some(later));
let quiet_after_window =
later + PROCESS_ACQUISITION_WINDOW + PROCESS_ACQUISITION_IDLE_RESET;
sync_content_change_acquisition(
None,
None,
false,
false,
quiet_after_window,
&mut acquisition_started_at,
&mut last_content_change_at,
);
assert_eq!(acquisition_started_at, None);
assert_eq!(last_content_change_at, None);
let next_burst = quiet_after_window + std::time::Duration::from_secs(1);
sync_content_change_acquisition(
None,
None,
false,
true,
next_burst,
&mut acquisition_started_at,
&mut last_content_change_at,
);
assert_eq!(acquisition_started_at, Some(next_burst));
assert_eq!(last_content_change_at, Some(next_burst));
}
#[test]
fn content_change_does_not_start_acquisition_when_process_probe_has_other_signal() {
let now = std::time::Instant::now();
let mut acquisition_started_at = None;
let mut last_content_change_at = None;
sync_content_change_acquisition(
Some(Agent::Codex),
None,
false,
true,
now,
&mut acquisition_started_at,
&mut last_content_change_at,
);
assert_eq!(acquisition_started_at, None);
assert_eq!(last_content_change_at, None);
sync_content_change_acquisition(
None,
Some(Agent::Codex),
false,
true,
now,
&mut acquisition_started_at,
&mut last_content_change_at,
);
assert_eq!(acquisition_started_at, None);
assert_eq!(last_content_change_at, None);
sync_content_change_acquisition(
None,
None,
true,
true,
now,
&mut acquisition_started_at,
&mut last_content_change_at,
);
assert_eq!(acquisition_started_at, None);
assert_eq!(last_content_change_at, None);
}
#[test]
fn content_change_restarts_stale_process_group_acquisition_window() {
let now = std::time::Instant::now();
let stale_start = now - PROCESS_ACQUISITION_WINDOW - std::time::Duration::from_millis(1);
let mut acquisition_started_at = Some(stale_start);
let mut last_content_change_at = None;
sync_content_change_acquisition(
None,
None,
false,
true,
now,
&mut acquisition_started_at,
&mut last_content_change_at,
);
assert_eq!(acquisition_started_at, Some(now));
assert_eq!(last_content_change_at, Some(now));
}
#[test]
fn release_expiry_can_force_reacquire_probe_by_resetting_probe_state() {
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: None,
has_process_probe: false,
..process_probe_input()
}));
}
#[test]
fn identified_agent_uses_shorter_safety_process_probe() {
assert!(!should_probe_foreground_job(ProcessProbeInput {
current_agent: Some(Agent::Codex),
elapsed_since_process_check: PROCESS_RECHECK_IDENTIFIED
- std::time::Duration::from_millis(1),
..process_probe_input()
}));
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: Some(Agent::Codex),
elapsed_since_process_check: PROCESS_RECHECK_IDENTIFIED,
..process_probe_input()
}));
}
#[test]
fn identified_agent_probes_when_foreground_group_disappears() {
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: Some(Agent::Codex),
foreground_pgid: None,
last_foreground_pgid: Some(42),
elapsed_since_process_check: PROCESS_RECHECK_IDENTIFIED
- std::time::Duration::from_millis(1),
..process_probe_input()
}));
}
#[test]
fn stable_missing_foreground_group_uses_safety_process_probe() {
assert!(!should_probe_foreground_job(ProcessProbeInput {
current_agent: Some(Agent::Codex),
foreground_pgid: None,
last_foreground_pgid: None,
elapsed_since_process_check: PROCESS_RECHECK_IDENTIFIED
- std::time::Duration::from_millis(1),
..process_probe_input()
}));
assert!(should_probe_foreground_job(ProcessProbeInput {
current_agent: Some(Agent::Codex),
foreground_pgid: None,
last_foreground_pgid: None,
elapsed_since_process_check: PROCESS_RECHECK_IDENTIFIED,
..process_probe_input()
}));
}
#[test]
fn transient_process_miss_keeps_current_agent_detected() {
let mut presence = AgentDetectionPresence::from_agent(Some(Agent::Pi));
let changed = presence.observe_process_probe(None);
assert!(!changed, "one miss should not clear the detected agent");
assert_eq!(presence.current_agent(), Some(Agent::Pi));
}
#[test]
fn agent_only_clears_after_confirmation_misses() {
let mut presence = AgentDetectionPresence::from_agent(Some(Agent::Pi));
for attempt in 1..AGENT_MISS_CONFIRMATION_ATTEMPTS {
let changed = presence.observe_process_probe(None);
assert!(
!changed,
"miss {attempt} should stay in the confirmation window"
);
assert_eq!(presence.current_agent(), Some(Agent::Pi));
}
let changed = presence.observe_process_probe(None);
assert!(changed, "last confirmation miss should clear the agent");
assert_eq!(presence.current_agent(), None);
}
#[test]
fn pty_activity_outside_taint_reports_active_until_hold_expires() {
let now = std::time::Instant::now();
let mut tracker = PtyCausalityTracker::default();
baseline_pty_causality(&mut tracker, 1, 1);
let active = agent_caused_pty_activity_active(2, 1, &mut tracker, now);
assert!(active.active);
assert!(!active.tainted);
let held = agent_caused_pty_activity_active(
2,
1,
&mut tracker,
now + AGENT_PTY_ACTIVITY_WINDOW - std::time::Duration::from_millis(1),
);
assert!(held.active);
assert!(!held.tainted);
let expired = agent_caused_pty_activity_active(
2,
1,
&mut tracker,
now + AGENT_PTY_ACTIVITY_WINDOW + std::time::Duration::from_millis(1),
);
assert!(!expired.active);
assert!(!expired.tainted);
}
#[test]
fn input_taint_discards_pty_activity_until_fresh_post_taint_render() {
let now = std::time::Instant::now();
let mut tracker = PtyCausalityTracker::default();
baseline_pty_causality(&mut tracker, 1, 1);
let tainted = agent_caused_pty_activity_active(2, 2, &mut tracker, now);
assert!(!tainted.active);
assert!(tainted.tainted);
let after_taint = agent_caused_pty_activity_active(
2,
2,
&mut tracker,
now + AGENT_INPUT_TAINT_WINDOW + std::time::Duration::from_millis(1),
);
assert!(!after_taint.active);
assert!(!after_taint.tainted);
let fresh_render = agent_caused_pty_activity_active(
3,
2,
&mut tracker,
now + AGENT_INPUT_TAINT_WINDOW + std::time::Duration::from_millis(2),
);
assert!(fresh_render.active);
assert!(!fresh_render.tainted);
}
#[test]
fn startup_grace_rebaseline_discards_accumulated_pty_activity() {
let now = std::time::Instant::now();
let mut tracker = PtyCausalityTracker::default();
baseline_pty_causality(&mut tracker, 1, 1);
let startup_render = agent_caused_pty_activity_active(2, 1, &mut tracker, now);
assert!(startup_render.active);
baseline_pty_causality(&mut tracker, 2, 1);
let after_grace =
agent_caused_pty_activity_active(2, 1, &mut tracker, now + AGENT_STARTUP_GRACE_WINDOW);
assert!(!after_grace.active);
assert!(!after_grace.tainted);
}
#[tokio::test]
async fn state_changed_event_waits_for_queue_space_instead_of_dropping() {
let (tx, mut rx) = mpsc::channel(1);
let pane_id = PaneId::from_raw(42);
tx.try_send(AppEvent::UpdateReady {
version: "9.9.9".into(),
install_command: "herdr update".into(),
})
.unwrap();
let publish = publish_state_changed_event(
tx.clone(),
pane_id,
Some(Agent::Pi),
AgentState::Idle,
false,
false,
false,
std::time::Instant::now(),
);
tokio::pin!(publish);
let blocked = tokio::time::timeout(std::time::Duration::from_millis(20), async {
(&mut publish).await;
})
.await;
assert!(
blocked.is_err(),
"publisher should wait for queue space instead of dropping StateChanged"
);
let first = tokio::time::timeout(std::time::Duration::from_millis(50), rx.recv())
.await
.expect("queue should yield first event")
.expect("sender still alive");
assert!(matches!(first, AppEvent::UpdateReady { .. }));
tokio::time::timeout(std::time::Duration::from_millis(50), async {
(&mut publish).await;
})
.await
.expect("publisher should complete once queue space is available");
let second = tokio::time::timeout(std::time::Duration::from_millis(50), rx.recv())
.await
.expect("queue should yield second event")
.expect("sender still alive");
assert!(matches!(
second,
AppEvent::StateChanged {
pane_id: delivered_pane,
agent: Some(Agent::Pi),
state: AgentState::Idle,
visible_blocker: false,
visible_working: false,
process_exited: false,
observed_at: _,
} if delivered_pane == pane_id
));
}
}