diff --git a/.gitignore b/.gitignore index 52bf9dd..7ba97c1 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 590ebdc..10b2ed4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/core/src/app.rs b/crates/core/src/app.rs index 7977201..13b1d51 100644 --- a/crates/core/src/app.rs +++ b/crates/core/src/app.rs @@ -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, diff --git a/crates/core/src/app/session.rs b/crates/core/src/app/session.rs index 85efe85..badc7ca 100644 --- a/crates/core/src/app/session.rs +++ b/crates/core/src/app/session.rs @@ -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) -> Vec { + 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 @@ -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" diff --git a/crates/pty/src/manager.rs b/crates/pty/src/manager.rs index b4e0118..14b4b58 100644 --- a/crates/pty/src/manager.rs +++ b/crates/pty/src/manager.rs @@ -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::(); + 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::()) + .collect::>() + .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. diff --git a/crates/pty/src/session.rs b/crates/pty/src/session.rs index 159439b..2727ca4 100644 --- a/crates/pty/src/session.rs +++ b/crates/pty/src/session.rs @@ -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 }, } /// The control-side handle to one session. The grid lives in the terminal @@ -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 @@ -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) -> 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 @@ -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 @@ -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; } } @@ -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::*; diff --git a/docs/src/workspace/tabs-and-splits.md b/docs/src/workspace/tabs-and-splits.md index 61c0565..5006200 100644 --- a/docs/src/workspace/tabs-and-splits.md +++ b/docs/src/workspace/tabs-and-splits.md @@ -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.