Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent): keep git off the user's terminal
James Damc6d4edc116cf
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rsindex 0f865af00..182ab8829 100644--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs@@ -742,6 +742,13 @@ fn set_process_group(cmd: &mut Command) {/// process that already leads a group.#[cfg(unix)]pub fn detach_from_terminal(cmd: &mut Command) {+ detach_std_from_terminal(cmd.as_std_mut());+}++/// [`detach_from_terminal`] for a blocking `std::process::Command`.+#[cfg(unix)]+pub fn detach_std_from_terminal(cmd: &mut std::process::Command) {+ use std::os::unix::process::CommandExt;// # Safety: runs in the forked child before exec; `setsid` is// async-signal-safe. It can only fail if the child already leads a process// group, which a freshly forked child never does; failing the spawn then isdiff --git a/src-tauri/src/core/agent/git.rs b/src-tauri/src/core/agent/git.rsindex 99eb3483b..61a8e4bf2 100644--- a/src-tauri/src/core/agent/git.rs+++ b/src-tauri/src/core/agent/git.rs@@ -30,10 +30,21 @@ use std::process::Command;/// A `git` command, resolved by [`jan_utils::git_program`] so Windows never/// runs a `git.exe` from Jan's own folder.-fn git_command() -> Result<Command, String> {- jan_utils::git_program()+///+/// It never talks to the user's terminal: no stdin, `GIT_TERMINAL_PROMPT=0`,+/// and (unix) its own session. Under the TUI, git asking for credentials on+/// `/dev/tty` -- or ssh asking to trust a host key -- would otherwise wait on+/// a prompt the TUI hides, with the TUI and git both reading the same+/// keystrokes. Now such a command fails with git's own error instead.+pub(crate) fn git_command() -> Result<Command, String> {+ let mut cmd = jan_utils::git_program().map(Command::new)- .map_err(|e| format!("failed to launch git: {e}"))+ .map_err(|e| format!("failed to launch git: {e}"))?;+ cmd.stdin(std::process::Stdio::null())+ .env("GIT_TERMINAL_PROMPT", "0");+ #[cfg(unix)]+ tauri_plugin_agent_tools::tools::proc::detach_std_from_terminal(&mut cmd);+ Ok(cmd)}/// Run `git` with literal args (callers pass their own `-C`). Returns trimmed@@ -412,6 +423,23 @@ mod tests {static COUNTER: AtomicU32 = AtomicU32::new(0);+ /// git, and whatever it runs (credential helpers, ssh, aliases), cannot+ /// prompt on the TUI's terminal. The `/dev/tty` probe is only meaningful+ /// when the test runner has a terminal (it says "opened" for a git that+ /// shares Jan's session); `GIT_TERMINAL_PROMPT` is checked regardless.+ #[cfg(unix)]+ #[test]+ fn git_never_prompts_on_the_terminal() {+ let Ok(mut cmd) = git_command() else { return };+ let probe = "!echo \"prompt=$GIT_TERMINAL_PROMPT\"; \+ if (: </dev/tty) 2>/dev/null; then echo opened; else echo denied; fi";+ let Ok(out) = cmd.args(["-c", &format!("alias.probe={probe}"), "probe"]).output() else {+ return;+ };+ assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));+ assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "prompt=0\ndenied");+ }+/// Init a throwaway repo with one commit, or `None` if git is unavailable so/// the suite skips instead of failing on a box without git.fn init_repo() -> Option<PathBuf> {diff --git a/src-tauri/src/core/agent/plugins.rs b/src-tauri/src/core/agent/plugins.rsindex 1ac888dc1..1ea076142 100644--- a/src-tauri/src/core/agent/plugins.rs+++ b/src-tauri/src/core/agent/plugins.rs@@ -26,7 +26,6 @@use std::io::{self, Write};use std::path::{Path, PathBuf};-use std::process::Command;use serde::Deserialize;@@ -792,8 +791,10 @@ fn repo_dir_name(url: &str) -> Option<&str> {}fn git(args: &[&str]) -> Result<String, String> {- let program = jan_utils::git_program().map_err(|e| format!("ERROR: git: {e}"))?;- let out = Command::new(program)+ // Shared with the snapshot code, so a clone that needs credentials fails+ // with git's error rather than prompting on the TUI's terminal.+ let out = super::git::git_command()+ .map_err(|e| format!("ERROR: {e}"))?.args(args).output().map_err(|e| format!("ERROR: git: {e}"))?;