Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(agent-tui): suspend and resume on Ctrl-Z

Akarshan Biswas1b740a21f9a9
diff --git a/src-tauri/jan-cli/Cargo.toml b/src-tauri/jan-cli/Cargo.toml
index 14d159aca..b6d8c311a 100644
--- a/src-tauri/jan-cli/Cargo.toml
+++ b/src-tauri/jan-cli/Cargo.toml
@@ -33,10 +33,11 @@ env_logger = "0.11"
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
-# Real-PTY panic-recovery test (tests/tui_panic.rs): nix's safe `openpty`
-# keeps the test free of `unsafe`. Pinned to the version ../Cargo.toml uses.
+# Real-PTY tests (tests/tui_panic.rs, tests/tui_suspend.rs): nix's safe
+# `openpty`, `kill` and `waitpid` keep them free of `unsafe`. Pinned to the
+# version ../Cargo.toml uses.
[target.'cfg(unix)'.dev-dependencies]
-nix = { version = "=0.30.1", features = ["term"] }
+nix = { version = "=0.30.1", features = ["term", "signal", "process"] }
# Mirror the desktop crate's release profile: the CLI inherited it while it was a
# bin of ../Cargo.toml, and a standalone crate would otherwise fall back to the
diff --git a/src-tauri/jan-cli/tests/tui_suspend.rs b/src-tauri/jan-cli/tests/tui_suspend.rs
new file mode 100644
index 000000000..8ff974c46
--- /dev/null
+++ b/src-tauri/jan-cli/tests/tui_suspend.rs
@@ -0,0 +1,181 @@
+//! Ctrl-Z in the TUI (and an external SIGTSTP) must hand a real terminal back
+//! clean before the process stops, and take it back on SIGCONT: raw mode,
+//! alternate screen, mouse tracking and the Kitty keyboard protocol off while
+//! stopped, all back on after. Spawns the real `jan` binary under a PTY the
+//! same way `tui_panic.rs` does, and watches the stop with `waitpid`.
+//!
+//! unix-only: job control is a POSIX concept, and the suspend is compiled out
+//! elsewhere.
+#![cfg(unix)]
+
+use std::fs::File;
+use std::io::{Read, Write};
+use std::os::unix::process::CommandExt;
+use std::process::{Child, Command, Stdio};
+use std::sync::{Arc, Mutex};
+use std::time::{Duration, Instant};
+
+use nix::pty::{openpty, Winsize};
+use nix::sys::signal::{kill, Signal};
+use nix::sys::termios::{tcgetattr, LocalFlags};
+use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
+use nix::unistd::Pid;
+
+/// Generous for a debug build on a loaded CI runner.
+const DEADLINE: Duration = Duration::from_secs(60);
+
+const ENTER: [&str; 4] = ["\x1b[?1049h", "\x1b[?2004h", "\x1b[>5u", "\x1b[?1000h"];
+const LEAVE: [&str; 5] = [
+ "\x1b[?1049l",
+ "\x1b[?2004l",
+ "\x1b[<u",
+ "\x1b[?1000l",
+ "\x1b[?25h",
+];
+
+struct Session {
+ child: Child,
+ pid: Pid,
+ master: File,
+ output: Arc<Mutex<Vec<u8>>>,
+ home: std::path::PathBuf,
+}
+
+impl Session {
+ fn text(&self) -> String {
+ String::from_utf8_lossy(&self.output.lock().unwrap()).into_owned()
+ }
+
+ /// Wait until every `seqs` entry appears in the output past `from`.
+ fn wait_for(&self, from: usize, seqs: &[&str], what: &str) {
+ let deadline = Instant::now() + DEADLINE;
+ loop {
+ let text = self.text();
+ let tail = text.get(from..).unwrap_or("");
+ if seqs.iter().all(|s| tail.contains(s)) {
+ return;
+ }
+ assert!(Instant::now() < deadline, "timed out waiting for {what}: {text:?}");
+ std::thread::sleep(Duration::from_millis(50));
+ }
+ }
+
+ fn wait_stopped(&self) {
+ let deadline = Instant::now() + DEADLINE;
+ loop {
+ let flags = WaitPidFlag::WUNTRACED | WaitPidFlag::WNOHANG;
+ match waitpid(self.pid, Some(flags)).expect("waitpid") {
+ WaitStatus::Stopped(..) => return,
+ WaitStatus::StillAlive => {}
+ other => panic!("jan did not stop: {other:?}: {:?}", self.text()),
+ }
+ assert!(Instant::now() < deadline, "jan never stopped: {:?}", self.text());
+ std::thread::sleep(Duration::from_millis(50));
+ }
+ }
+
+ fn alive(&self) -> bool {
+ let flags = WaitPidFlag::WNOHANG;
+ matches!(waitpid(self.pid, Some(flags)), Ok(WaitStatus::StillAlive))
+ }
+
+ fn raw(&self) -> bool {
+ let termios = tcgetattr(&self.master).expect("read PTY termios");
+ !termios
+ .local_flags
+ .intersects(LocalFlags::ICANON | LocalFlags::ECHO)
+ }
+
+ /// Stop by `stop`, check the terminal is clean while stopped, resume, and
+ /// check the modes and raw mode come back.
+ fn cycle(&self, stop: impl FnOnce(), what: &str) {
+ let before = self.text().len();
+ stop();
+ self.wait_stopped();
+ self.wait_for(before, &LEAVE, &format!("{what}: modes restored before stopping"));
+ assert!(!self.raw(), "{what}: the shell got a raw-mode terminal");
+ let stopped_at = self.text().len();
+ kill(self.pid, Signal::SIGCONT).expect("SIGCONT");
+ self.wait_for(stopped_at, &ENTER, &format!("{what}: modes re-entered on resume"));
+ let deadline = Instant::now() + DEADLINE;
+ while !self.raw() {
+ assert!(Instant::now() < deadline, "{what}: raw mode not re-enabled");
+ std::thread::sleep(Duration::from_millis(50));
+ }
+ }
+}
+
+impl Drop for Session {
+ fn drop(&mut self) {
+ let _ = self.child.kill();
+ let _ = self.child.wait();
+ let _ = std::fs::remove_dir_all(&self.home);
+ }
+}
+
+fn spawn() -> Session {
+ let size = Winsize {
+ ws_row: 24,
+ ws_col: 80,
+ ws_xpixel: 0,
+ ws_ypixel: 0,
+ };
+ let pty = openpty(&size, None).expect("open test PTY");
+ let mut reader = File::from(pty.master);
+ let master = reader.try_clone().expect("clone PTY master");
+ let slave = File::from(pty.slave);
+ let home = std::env::temp_dir().join(format!("jan-tui-suspend-{}", std::process::id()));
+ std::fs::create_dir_all(&home).unwrap();
+ let child = Command::new(env!("CARGO_BIN_EXE_jan"))
+ .env("HOME", &home)
+ .env("JAN_CLI_NO_UPDATE_CHECK", "1")
+ .stdin(Stdio::from(slave.try_clone().expect("clone PTY slave")))
+ .stdout(Stdio::from(slave.try_clone().expect("clone PTY slave")))
+ .stderr(Stdio::from(slave))
+ // The suspend stops jan's whole process group; without a group of its
+ // own that would be this test runner's.
+ .process_group(0)
+ .spawn()
+ .expect("spawn jan under the test PTY");
+ let pid = Pid::from_raw(child.id() as i32);
+ let output = Arc::new(Mutex::new(Vec::new()));
+ let sink = Arc::clone(&output);
+ std::thread::spawn(move || {
+ let mut buffer = [0u8; 4096];
+ // Ends with EIO once the child is gone and the last slave closes.
+ while let Ok(n) = reader.read(&mut buffer) {
+ if n == 0 {
+ break;
+ }
+ sink.lock().unwrap().extend_from_slice(&buffer[..n]);
+ }
+ });
+ Session {
+ child,
+ pid,
+ master,
+ output,
+ home,
+ }
+}
+
+#[test]
+fn ctrl_z_and_sigtstp_suspend_and_resume_cleanly() {
+ let session = spawn();
+ session.wait_for(0, &ENTER, "the TUI to start");
+ assert!(session.raw(), "the TUI runs in raw mode");
+
+ let mut master = session.master.try_clone().expect("clone PTY master");
+ session.cycle(
+ || {
+ // Ctrl-Z, as a raw-mode terminal delivers it: a plain 0x1a byte.
+ master.write_all(b"\x1a").expect("type Ctrl-Z");
+ },
+ "Ctrl-Z",
+ );
+ session.cycle(
+ || kill(session.pid, Signal::SIGTSTP).expect("SIGTSTP"),
+ "external SIGTSTP",
+ );
+ assert!(session.alive(), "jan exited after resuming: {:?}", session.text());
+}
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index 53fa249b4..24df70c8a 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -145,14 +145,40 @@ fn use_synchronized_output() -> bool {
sync_output_for(super::terminal_setup::identify(|k| std::env::var(k).ok()))
}
+/// Every terminal mode the session runs under, short of raw mode itself: the
+/// alternate screen, bracketed paste, then `startup_modes`. The one entry point
+/// for both startup and resuming from a suspend, so a resumed session gets back
+/// exactly what it started with; `write_restore` is its inverse.
+fn enter_terminal_modes(out: &mut impl Write, mouse: bool) -> io::Result<()> {
+ execute!(out, EnterAlternateScreen, EnableBracketedPaste)?;
+ out.write_all(startup_modes(mouse).as_bytes())?;
+ out.flush()
+}
+
+/// Undo `enter_terminal_modes` in one write. Mouse tracking is turned off
+/// whether or not it was on, which is harmless and keeps this independent of
+/// the config. Best-effort, see `restore_terminal_modes`.
+fn write_restore(out: &mut impl Write) {
+ let _ = out.write_all(
+ format!(
+ "{END_SYNC_UPDATE}{DISABLE_BRACKETED_PASTE}{DISABLE_MOUSE_CAPTURE}\
+ {KITTY_KEYS_OFF}{}{LEAVE_ALT_SCREEN}",
+ alt_scroll_restore(),
+ )
+ .as_bytes(),
+ );
+ let _ = out.write_all(SHOW_CURSOR.as_bytes());
+}
+
/// Everything `run` turns on between `enable_raw_mode` and the first frame,
-/// undone in one write. Shared by the clean-shutdown path and the panic hook
-/// (`install_panic_hook`) so the two can never drift: whichever one runs, the
-/// shell gets back exactly what it had -- no raw mode, no alternate screen, no
-/// mouse tracking, no Kitty keyboard protocol, alternate scroll restored, and a
-/// visible cursor. Plain writes rather than `execute!`/`Terminal`, since the
-/// panic hook has no `&mut Terminal` to hand (the panic can land while one is
-/// borrowed) and raw mode's own disable is independent of stdout entirely.
+/// undone in one write. Shared by the clean-shutdown path, the panic hook
+/// (`install_panic_hook`) and the Ctrl-Z suspend (`suspend_to_shell`) so they
+/// can never drift: whichever one runs, the shell gets back exactly what it
+/// had -- no raw mode, no alternate screen, no mouse tracking, no Kitty
+/// keyboard protocol, alternate scroll restored, and a visible cursor. Plain
+/// writes rather than `Terminal`, since the panic hook has no `&mut Terminal`
+/// to hand (the panic can land while one is borrowed) and raw mode's own
+/// disable is independent of stdout entirely.
///
/// Best-effort: a panic is already an error path, and a second one here (an
/// already-closed stdout, say) must not stop the first panic's message from
@@ -160,18 +186,84 @@ fn use_synchronized_output() -> bool {
fn restore_terminal_modes() {
let _ = disable_raw_mode();
let mut stdout = io::stdout();
- let _ = stdout.write_all(
- format!(
- "{END_SYNC_UPDATE}{DISABLE_BRACKETED_PASTE}{DISABLE_MOUSE_CAPTURE}\
- {KITTY_KEYS_OFF}{}{LEAVE_ALT_SCREEN}",
- alt_scroll_restore(),
- )
- .as_bytes(),
- );
- let _ = stdout.write_all(SHOW_CURSOR.as_bytes());
+ write_restore(&mut stdout);
let _ = stdout.flush();
}
+/// Raw mode clears `ISIG`, so the terminal never turns Ctrl-Z into SIGTSTP:
+/// it arrives as an ordinary key, and job control is the app's to do.
+#[cfg(unix)]
+fn is_suspend_key(key: &KeyEvent) -> bool {
+ key.kind == KeyEventKind::Press
+ && key.modifiers.contains(KeyModifiers::CONTROL)
+ && key.code == KeyCode::Char('z')
+}
+
+/// Hand the terminal back to the shell and stop, as Ctrl-Z does to any cooked
+/// program, then take it back once `fg` (or any SIGCONT) resumes the process.
+/// SIGSTOP rather than SIGTSTP: `chat_loop` catches SIGTSTP (`stop_signal`) so
+/// an external `kill -TSTP` restores the terminal first, and a caught signal no
+/// longer stops anything. A self-sent stop takes effect before `kill` returns,
+/// so the line after it runs on resume. It goes to the whole process group
+/// (pid 0), as the terminal's own Ctrl-Z would, so a wrapper `jan` was
+/// launched through stops with it and the shell sees the job stop.
+/// `enable_raw_mode` re-reads the termios rather than reusing the pre-suspend
+/// one: the shell owned the terminal in between. The caller owes a full
+/// repaint, since the alternate screen comes back blank.
+#[cfg(unix)]
+fn suspend_to_shell(mouse: bool) -> io::Result<()> {
+ use nix::sys::signal::{kill, Signal};
+ use nix::unistd::Pid;
+ restore_terminal_modes();
+ let stopped = kill(Pid::from_raw(0), Signal::SIGSTOP).map_err(io::Error::from);
+ enable_raw_mode()?;
+ enter_terminal_modes(&mut io::stdout(), mouse)?;
+ stopped
+}
+
+/// Suspend from the render loop, noting a failure in the transcript rather
+/// than ending the session over it.
+#[cfg(unix)]
+fn suspend_session(app: &mut App, mouse: bool) {
+ if let Err(e) = suspend_to_shell(mouse) {
+ app.note(&format!("could not suspend: {e}"));
+ }
+ app.request_repaint();
+}
+
+/// SIGTSTP from outside (`kill -TSTP`, a job-control `stop`) would otherwise
+/// stop the process with every mode still on and the shell unusable. Caught
+/// here, it takes the same path as Ctrl-Z. `None` where it cannot be caught.
+#[cfg(unix)]
+type StopSignal = Option<tokio::signal::unix::Signal>;
+#[cfg(not(unix))]
+type StopSignal = Option<std::convert::Infallible>;
+
+#[cfg(unix)]
+fn stop_signal() -> StopSignal {
+ use tokio::signal::unix::{signal, SignalKind};
+ signal(SignalKind::from_raw(nix::sys::signal::Signal::SIGTSTP as i32)).ok()
+}
+#[cfg(not(unix))]
+fn stop_signal() -> StopSignal {
+ None
+}
+
+/// Resolve once per external SIGTSTP; pends forever when there is no handler.
+async fn await_stop_signal(signal: &mut StopSignal) {
+ #[cfg(unix)]
+ if let Some(s) = signal.as_mut() {
+ if s.recv().await.is_some() {
+ return;
+ }
+ // The stream ended: never fire again.
+ *signal = None;
+ }
+ #[cfg(not(unix))]
+ let _ = signal;
+ pending().await
+}
+
/// Raw escape sequences standing in for the `crossterm::Command` types used
/// elsewhere (`DisableBracketedPaste`, `DisableMouseCapture`,
/// `LeaveAlternateScreen`, cursor show): `restore_terminal_modes` has no
@@ -10058,10 +10150,8 @@ pub async fn run(
log::set_max_level(prev_log_level);
e.to_string()
};
- execute!(stdout, EnterAlternateScreen, EnableBracketedPaste).map_err(abort_startup)?;
- let modes = startup_modes(crate::core::agent::global_config::mouse_enabled());
- let _ = stdout.write_all(modes.as_bytes());
- let _ = stdout.flush();
+ let mouse = crate::core::agent::global_config::mouse_enabled();
+ enter_terminal_modes(&mut stdout, mouse).map_err(abort_startup)?;
// Test-only hook for the real-PTY panic-recovery integration test
// (`jan-cli/tests/tui_panic.rs`): an in-memory unit test can prove the hook
// fires, but only a genuine panic on a genuine terminal, with every mode
@@ -10196,6 +10286,7 @@ pub async fn run(
initial_task,
mcp_task,
&mcp_servers,
+ mouse,
)
.await;
@@ -10338,7 +10429,12 @@ async fn chat_loop<B: Backend>(
initial_task: Option<String>,
mut mcp_task: Option<tokio::task::JoinHandle<crate::core::cli::mcp::ConnectOutcome>>,
mcp_servers: &crate::core::state::SharedMcpServers,
+ mouse: bool,
) -> Result<(), String> {
+ // Only a suspend re-enters the startup modes from inside the loop.
+ #[cfg(not(unix))]
+ let _ = mouse;
+ let mut stop = stop_signal();
let mut current: Option<CurrentRun> = None;
let mut ticker = tokio::time::interval(Duration::from_millis(50));
// Cloned out of `app` so the select arm below can await it while other
@@ -10727,6 +10823,11 @@ async fn chat_loop<B: Backend>(
while event::poll(Duration::ZERO).unwrap_or(false) {
match event::read() {
Ok(Event::Key(key)) => {
+ #[cfg(unix)]
+ if is_suspend_key(&key) {
+ suspend_session(app, mouse);
+ continue;
+ }
// Typing moves the content under a highlight, and the
// copy already happened on release.
app.clear_selection();
@@ -10748,6 +10849,10 @@ async fn chat_loop<B: Backend>(
}
}
}
+ _ = await_stop_signal(&mut stop) => {
+ #[cfg(unix)]
+ suspend_session(app, mouse);
+ }
_ = branch_poll.tick() => {
if branch_task.is_none() {
branch_task = Some(spawn_branch_poll(&app.project_root));
@@ -13366,6 +13471,8 @@ const KEY_BINDINGS: &[(&str, &str)] = &[
("Ctrl-O", "Expand or collapse all tool calls"),
("Ctrl-V", "Paste an image from the clipboard"),
("Ctrl-L", "Redraw the screen (repairs a broadcast over it)"),
+ #[cfg(unix)]
+ ("Ctrl-Z", "Suspend to the shell (resume with fg)"),
("Shift+Tab", "Cycle reasoning effort (low/medium/high)"),
("Alt+T", "Toggle reasoning effort (low / last)"),
(
@@ -33151,6 +33258,53 @@ mod tests {
assert!(!super::release_terminal(), "and only once");
}
+ /// Startup, resume-after-suspend and every restore (clean exit, startup
+ /// error, panic, suspend) go through one enter/restore pair, so each mode
+ /// the enter turns on must have its disable in the restore.
+ #[test]
+ fn every_entered_mode_has_its_restore() {
+ for mouse in [true, false] {
+ let mut entered = Vec::new();
+ super::enter_terminal_modes(&mut entered, mouse).unwrap();
+ let entered = String::from_utf8(entered).unwrap();
+ let mut restored = Vec::new();
+ super::write_restore(&mut restored);
+ let restored = String::from_utf8(restored).unwrap();
+
+ assert!(entered.contains("\x1b[?1049h"), "alternate screen");
+ assert!(restored.contains("\x1b[?1049l"));
+ assert!(entered.contains("\x1b[?2004h"), "bracketed paste");
+ assert!(restored.contains("\x1b[?2004l"));
+ assert!(entered.contains(KITTY_KEYS_ON) && restored.contains(KITTY_KEYS_OFF));
+ assert!(entered.contains(alt_scroll_save_off()));
+ assert!(restored.contains(alt_scroll_restore()));
+ assert_eq!(entered.contains(MOUSE_TRACK_ON), mouse, "mouse={mouse}");
+ for seq in ["\x1b[?1000l", "\x1b[?1002l", "\x1b[?1006l"] {
+ assert!(restored.contains(seq), "mouse off is unconditional: {seq:?}");
+ }
+ assert!(restored.ends_with("\x1b[?25h"), "cursor shown last: {restored:?}");
+ assert!(restored.starts_with("\x1b[?2026l"), "held frame ended first");
+ }
+ }
+
+ /// Raw mode keeps the terminal from turning Ctrl-Z into SIGTSTP, so it
+ /// arrives as a key: only that press suspends, not a plain `z` or a
+ /// release, and it is not advertised as anything else.
+ #[cfg(unix)]
+ #[test]
+ fn only_ctrl_z_press_suspends() {
+ let ctrl_z = KeyEvent::new(KeyCode::Char('z'), KeyModifiers::CONTROL);
+ assert!(super::is_suspend_key(&ctrl_z));
+ assert!(!super::is_suspend_key(&KeyEvent::new(
+ KeyCode::Char('z'),
+ KeyModifiers::NONE
+ )));
+ let mut release = ctrl_z;
+ release.kind = KeyEventKind::Release;
+ assert!(!super::is_suspend_key(&release));
+ assert!(KEY_BINDINGS.iter().any(|(k, d)| *k == "Ctrl-Z" && d.contains("fg")));
+ }
+
/// Keyboard enhancement is not the mouse: it goes out whether or not
/// tracking is on, since the composer's editing keys depend on it.
#[test]