Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ dev.termherd.lock

# Contributor benchmark harness output (docs/BENCH.md); results stay local.
/bench/results/

# tokenade — per-project cache / scaffold flag / dedup refs
.tokenade/

# tokenade — legacy per-project embedding model cache (now global)
.fastembed_cache/

# a second target dir, for building while termherd runs from target/
/target-alt/
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and the project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed (a dead pane says it is dead)

- A pane whose shell exited with a failure kept its cursor and looked alive.
It now ends with `[process exited with code N. Press Enter to close]`, and
Enter closes it. Under `cmd.exe` a bare `exit` after a failed command is
such an exit.

### Added (`termherd mcp-proxy` for Antigravity and non-`--mcp-config` CLIs)

- Added the `termherd mcp-proxy` subcommand, a stdio-to-HTTP bridge that reads
Expand Down
4 changes: 1 addition & 3 deletions crates/core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ impl App {
Vec::new()
}
Event::LaunchSession(spec) => self.launch(spec),
Event::TerminalInput { session, bytes } => {
self.if_live(session, Effect::Write { session, bytes })
}
Event::TerminalInput { session, bytes } => self.terminal_input(session, bytes),
Event::Paste {
session,
text,
Expand Down
20 changes: 20 additions & 0 deletions crates/core/src/app/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,20 @@ impl App {
self.sessions.get(&session)?.live_version()
}

/// The user typed `bytes` into `session`: written while it lives. Once it
/// has exited, Enter closes its pane — the pane an unclean exit kept open
/// (see [`App::pty_exited`]) — and any other key does nothing.
pub(super) fn terminal_input(&mut self, session: SessionId, bytes: Vec<u8>) -> Vec<Effect> {
let exited = self
.sessions
.get(&session)
.is_some_and(|s| s.status == SessionStatus::Exited);
if exited && bytes == b"\r" {
return self.close_pane_of(session).unwrap_or_default();
}
self.if_live(session, Effect::Write { session, bytes })
}

/// A session's PTY ended. A *clean* exit — the user typed `exit` at a
/// prompt — leaves nothing worth reading, so its pane closes on its own;
/// an unclean exit keeps the dead terminal visible: a failure's last
Expand Down Expand Up @@ -1213,6 +1227,12 @@ mod tests {
})
.is_empty()
);
// Enter closes the pane the unclean exit kept open.
app.apply(Event::TerminalInput {
session: id,
bytes: b"\r".to_vec(),
});
assert_eq!(app.workspace.tab_of(id), None, "Enter closes the dead pane");
assert!(
app.apply(Event::TerminalSubmit { session: id }).is_empty(),
"an exited session is not pressed Enter in"
Expand Down
42 changes: 42 additions & 0 deletions crates/pty/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,48 @@ mod tests {
mgr.kill(id).expect("kill releases the dead session");
}

/// Exit a spawned shell with a failing status and assert its last screen
/// says so: the pane an unclean exit keeps open must not look alive.
#[test]
fn an_unclean_exit_writes_its_code_under_the_last_screen() {
let (tx, rx) = mpsc::channel::<PtyEvent>();
let sink: EventSink = Arc::new(move |ev| {
let _ = tx.send(ev);
});
let (_private, mgr) = manager(sink, Palette::default());
let id = sid(3);
mgr.spawn(spec(id)).expect("spawn");
mgr.write(id, b"exit 3\r\n").expect("write");

let deadline = Instant::now() + Duration::from_secs(15);
let mut last = String::new();
let mut reaped = None;
while Instant::now() < deadline {
match rx.recv_timeout(Duration::from_millis(500)) {
Ok(PtyEvent::Output { screen, .. }) => {
last = screen
.lines
.iter()
.map(|line| line.iter().map(|cell| cell.c).collect::<String>())
.collect::<Vec<_>>()
.join("\n");
}
Ok(PtyEvent::Exited { clean, .. }) => {
reaped = Some(clean);
break;
}
Ok(_) | Err(mpsc::RecvTimeoutError::Timeout) => continue,
Err(_) => break,
}
}
assert_eq!(reaped, Some(false), "`exit 3` must reap as unclean");
assert!(
last.contains("[process exited with code 3. Press Enter to close]"),
"the last screen must carry the exit banner, saw:\n{last}"
);
mgr.kill(id).expect("kill");
}

/// Spawn a real PTY with a custom palette and assert the streamed screens
/// render in it — exercising manager → terminal thread → snapshot with the
/// injected colours, the path the GUI consumes.
Expand Down
40 changes: 35 additions & 5 deletions crates/pty/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@ pub(crate) enum TermCmd {
/// The shared palette changed: emit a fresh screen in the new colours.
Repaint,
/// The PTY reached end of file; the process is gone. `clean` carries the
/// reaped exit status (see [`PtyEvent::Exited`]).
Eof { clean: bool },
/// reaped exit status (see [`PtyEvent::Exited`]), `code` its exit code
/// when the process could be reaped.
Eof { clean: bool, code: Option<u32> },
}

/// The control-side handle to one session. The grid lives in the terminal
Expand Down Expand Up @@ -281,8 +282,13 @@ pub(crate) fn spawn_waiter(
std::thread::Builder::new()
.name(format!("pty-wt-{}", session.0.get()))
.spawn(move || {
let clean = child.wait().map(|status| status.success()).unwrap_or(false);
let _ = ctrl.send(TermCmd::Eof { clean });
let status = child.wait().ok();
let _ = ctrl.send(TermCmd::Eof {
clean: status
.as_ref()
.is_some_and(portable_pty::ExitStatus::success),
code: status.map(|status| status.exit_code()),
});
})
.unwrap_or_else(|_| {
// Without a waiter the exit would never be observed; declare the
Expand All @@ -295,6 +301,16 @@ pub(crate) fn spawn_waiter(
})
}

/// The line written under a session's last output when its process exits
/// uncleanly, since its pane stays open (`core` closes it on the next Enter).
fn exit_banner(code: Option<u32>) -> String {
let status = code.map_or_else(
|| "process exited".to_owned(),
|code| format!("process exited with code {code}"),
);
format!("\r\n\x1b[0;2m[{status}. Press Enter to close]\x1b[0m")
}

/// Record what a chunk reported for a value the terminal keeps announcing —
/// its directory, its title — and hand it back **only when it is news**,
/// updating `seen` as it goes. `None` in means the chunk said nothing, which is
Expand Down Expand Up @@ -638,7 +654,7 @@ pub(crate) fn spawn_term(
// a second for nothing.
continue;
}
TermCmd::Eof { clean: c } => {
TermCmd::Eof { clean: c, code } => {
clean = c;
// The waiter races the reader: the exit is observed on
// the process while the last output chunks may still be
Expand All @@ -657,6 +673,11 @@ pub(crate) fn spawn_term(
Err(_) => break,
}
}
// A dead pane looks alive until something says so;
// a clean exit closes its pane and needs no word.
if !clean {
parser.advance(&mut term, exit_banner(code).as_bytes());
}
break;
}
}
Expand Down Expand Up @@ -684,6 +705,15 @@ pub(crate) fn spawn_term(

#[cfg(test)]
mod tests {
#[test]
fn exit_banner_names_the_code_and_how_to_close() {
assert_eq!(
super::exit_banner(Some(9009)),
"\r\n\x1b[0;2m[process exited with code 9009. Press Enter to close]\x1b[0m"
);
assert!(super::exit_banner(None).contains("[process exited. Press Enter"));
}

mod pacer {
use super::super::*;

Expand Down
6 changes: 5 additions & 1 deletion docs/src/workspace/tabs-and-splits.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,8 @@ will be force-stopped, and always asks while the doc editor holds unsaved
changes.

A pane whose **shell exits cleanly closes itself**; one whose shell exited with
a failure stays on screen so you can read what happened.
a failure stays on screen so you can read what happened, under a line naming
its exit code: `[process exited with code 1. Press Enter to close]`.
**Enter** closes it; any other key does nothing. Under `cmd.exe`, a bare
`exit` returns the last command's error level, so `exit` after a failed
command leaves the pane open: type `exit 0` to close it at once.