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): surface the update notice in the TUI, add /update

Akarshan Biswas98d906e1a4d7
diff --git a/src-tauri/src/bin/jan.rs b/src-tauri/src/bin/jan.rs
index 5d6331b77..188739823 100644
--- a/src-tauri/src/bin/jan.rs
+++ b/src-tauri/src/bin/jan.rs
@@ -362,11 +362,6 @@ async fn main() {
}))
.init();
- // `jan update` reports the same thing itself, in more detail.
- if !std::env::args().any(|a| a == "update") {
- app_lib::core::cli::updater::print_update_notice_if_available().await;
- }
-
// Inject the logo at runtime so we can use ANSI styling.
let logo = make_logo();
let matches = Cli::command()
@@ -376,6 +371,9 @@ async fn main() {
let cli = Cli::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
let Some(command) = cli.command else {
+ // No stderr notice on this path: the TUI's alternate screen would wipe
+ // it, and blocking on the check here would delay the first frame. The
+ // TUI runs the same check itself and notes it in the transcript.
let overrides = cli.providers.into_overrides();
if let Err(e) = cli_agent_ui(
&cli.project,
@@ -396,6 +394,11 @@ async fn main() {
return;
};
+ // `jan update` reports the same thing itself, in more detail.
+ if !matches!(command, Commands::Update { .. }) {
+ app_lib::core::cli::updater::print_update_notice_if_available().await;
+ }
+
match command {
Commands::Cli { cmd } => handle_cli(cmd).await,
Commands::Login => {
@@ -424,10 +427,7 @@ async fn handle_update(check: bool, force: bool) {
.await
.map(|u| {
if u.is_newer() {
- println!(
- "A new {} build is available: {} -> {}",
- u.channel, u.current, u.latest
- );
+ println!("{}", u.summary());
} else {
println!("Already on the latest {} build ({})", u.channel, u.current);
}
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index b7246c09e..6b896c2e2 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -573,6 +573,11 @@ struct App {
login: Option<LoginPrompt>,
/// Key handed off to the loop to verify off the render loop. Taken once.
login_submit: Option<String>,
+ /// `/update` handed off to the loop, which spawns the install. Taken once.
+ update_requested: bool,
+ /// An install is in flight: `/update` is refused (two processes must not
+ /// rewrite the same binary) and the footer shows progress.
+ update_installing: bool,
/// Lines scrolled back from the tail; 0 pins the view to the bottom so new
/// content follows. Non-zero survives streaming so scroll-back stays usable.
scrollback: u16,
@@ -793,6 +798,8 @@ impl App {
picker: None,
login: None,
login_submit: None,
+ update_requested: false,
+ update_installing: false,
scrollback: 0,
want_start: false,
view_width: 0,
@@ -3080,6 +3087,75 @@ async fn await_login(
}
}
+/// Await the startup update check once, clearing the slot. Same cancel-safe
+/// borrow as `await_mcp`; pends forever before it is spawned and after it has
+/// been consumed, so it can sit in the loop's `select!` unconditionally.
+async fn await_update_check(
+ task: &mut Option<tokio::task::JoinHandle<Option<super::updater::AvailableUpdate>>>,
+) -> Option<super::updater::AvailableUpdate> {
+ let joined = match task.as_mut() {
+ Some(h) => h.await,
+ None => return pending().await,
+ };
+ *task = None;
+ joined.ok().flatten()
+}
+
+/// Surface a newer published build in the transcript. The stderr notice the
+/// non-interactive commands print is invisible here (the alternate screen wipes
+/// it), so the TUI has to say it itself.
+fn note_update(app: &mut App, update: Option<super::updater::AvailableUpdate>) {
+ if let Some(update) = update {
+ app.note(&format!("{}; run /update to install it", update.summary()));
+ }
+}
+
+/// Hand `/update` to the loop, which downloads and swaps the binary off the
+/// render loop. Refused while one install is already in flight.
+fn update_command(app: &mut App) {
+ if app.update_installing {
+ app.note("an update is already installing");
+ return;
+ }
+ app.update_requested = true;
+ app.note("downloading the latest build...");
+}
+
+/// Await an in-flight `/update` install, parking forever when none is running.
+/// Same cancel-safe borrow as `await_mcp`.
+async fn await_update_install(
+ task: &mut Option<tokio::task::JoinHandle<Result<super::updater::UpdateOutcome, String>>>,
+) -> Result<super::updater::UpdateOutcome, String> {
+ let joined = match task.as_mut() {
+ Some(h) => h.await,
+ None => return pending().await,
+ };
+ *task = None;
+ match joined {
+ Ok(inner) => inner,
+ Err(e) => Err(format!("update task failed: {e}")),
+ }
+}
+
+/// Report an install. The swap replaced the file on disk, not this process's
+/// image, so the new build only runs after a restart. A failure clears the
+/// in-flight flag too, so `/update` can be retried.
+fn finish_update_install(app: &mut App, result: Result<super::updater::UpdateOutcome, String>) {
+ use super::updater::UpdateOutcome;
+ app.update_installing = false;
+ app.detail.clear();
+ match result {
+ Ok(UpdateOutcome::Installed { from, to, path }) => app.note(&format!(
+ "updated {} from {from} -> {to}; restart jan to run it",
+ tilde_path(&path)
+ )),
+ Ok(UpdateOutcome::UpToDate { version }) => {
+ app.note(&format!("already up to date ({version})"))
+ }
+ Err(e) => app.note(&format!("update failed: {e}")),
+ }
+}
+
pub async fn run(
session: AgentSession,
agent_dir: std::path::PathBuf,
@@ -3178,6 +3254,12 @@ pub async fn run(
);
let _ = terminal.show_cursor();
log::set_max_level(prev_log_level);
+ // Quitting cancels an in-flight `/update` (the runtime drops the task), so
+ // say so on the real terminal now that the alternate screen is gone -- a
+ // transcript note would vanish with it.
+ if app.update_installing {
+ eprintln!("the update was still installing when jan exited; run `jan update` to finish it");
+ }
res
}
@@ -3214,6 +3296,16 @@ async fn chat_loop<B: Backend>(
tokio::task::JoinHandle<Result<super::tokamak::Login, String>>,
> = None;
+ // The update check is a network round trip, so it runs off the render loop
+ // and notes itself whenever it lands rather than delaying the first frame.
+ let mut update_task = Some(tokio::spawn(super::updater::available_update()));
+
+ // `/update` downloads tens of megabytes and rewrites the binary; off the
+ // render loop for the same reason, and one at a time.
+ let mut update_install_task: Option<
+ tokio::task::JoinHandle<Result<super::updater::UpdateOutcome, String>>,
+ > = None;
+
match initial_task {
Some(task) if !task.trim().is_empty() => app.submit_user(task.trim().to_string()),
_ => app.note("type a message to start, or /help for commands"),
@@ -3250,6 +3342,18 @@ async fn chat_loop<B: Backend>(
}
}
+ // `/update` was typed: install off-loop. The flag is only honored when
+ // no install is in flight, so a repeated request can't spawn a second
+ // process rewriting the same binary.
+ if app.update_requested {
+ app.update_requested = false;
+ if update_install_task.is_none() {
+ app.update_installing = true;
+ app.detail = "installing update...".to_string();
+ update_install_task = Some(tokio::spawn(super::updater::self_update(false)));
+ }
+ }
+
// A turn finished under an active goal: run the (stateless) evaluator
// before anything else. It either auto-submits the next turn (setting
// want_start) or hands control back. Gated on an idle, run-free state so
@@ -3343,6 +3447,12 @@ async fn chat_loop<B: Backend>(
login_res = await_login(&mut login_task) => {
finish_login(app, login_res);
}
+ update = await_update_check(&mut update_task) => {
+ note_update(app, update);
+ }
+ install = await_update_install(&mut update_install_task) => {
+ finish_update_install(app, install);
+ }
snap_res = await_snapshot(&mut snap_task) => {
match (snap_inflight.take(), snap_res) {
(Some(SnapshotJob::Base), Ok(sha)) => {
@@ -4127,6 +4237,11 @@ const SLASH_COMMANDS: &[SlashCommand] = &[
hint: "",
description: "View provider config (~/.jan/config.toml)",
},
+ SlashCommand {
+ name: "/update",
+ hint: "",
+ description: "Install the latest published build (takes effect on restart)",
+ },
SlashCommand {
name: "/quit",
hint: "",
@@ -4239,6 +4354,7 @@ async fn run_command(app: &mut App, line: &str) {
}
"mcp" => open_mcp_picker(app),
"login" => open_login_prompt(app),
+ "update" => update_command(app),
"config" => open_config_screen(app),
"goal" => goal_command(app, arg),
"plan" => plan_command(app, arg),
@@ -6549,7 +6665,8 @@ mod tests {
apply_resume, assistant_is_awaiting_user_answer, build_user_message, clipboard_path,
diff_lines, group_activity, group_detail_lines, group_summary, handle_ask_key,
handle_ask_mouse, handle_key, handle_mouse, image_mime, input_content_lines,
- compact_tokens, finish_login, load_image_file, message_text, open_config_screen,
+ compact_tokens, finish_login, finish_update_install, load_image_file, message_text,
+ note_update, open_config_screen,
parse_command, partial_json_field, restore_goal, restore_run_mode, restore_todos,
run_command, starting_call_lines, unescape_partial_json_string,
running_group_row, split_reasoning, subagent_activity, subagent_name_from_run_id,
@@ -6566,6 +6683,7 @@ mod tests {
use ratatui::layout::Rect;
use ratatui::{style::Modifier, text::Line};
use crate::core::agent::events::{StreamEvent, Usage};
+ use crate::core::cli::updater::{AvailableUpdate, UpdateOutcome};
use crate::core::agent::r#loop::PermissionRegistry;
use crate::core::agent::tools::gate::PermissionDecision;
use serde_json::json;
@@ -6625,6 +6743,104 @@ mod tests {
assert_eq!(tokens_per_second(0, 500), 0.0);
}
+ fn available_update(current: &str, latest: &str) -> AvailableUpdate {
+ AvailableUpdate {
+ channel: "agent-nightly",
+ current: current.into(),
+ latest: latest.into(),
+ url: None,
+ sha256: None,
+ }
+ }
+
+ #[test]
+ fn a_newer_build_is_noted_in_the_transcript() {
+ let mut app = test_app();
+ note_update(&mut app, Some(available_update("0.8.4-10", "0.8.4-11")));
+ let text = app
+ .transcript
+ .iter()
+ .map(message_text_of)
+ .collect::<String>();
+ assert!(text.contains("0.8.4-10 -> 0.8.4-11"), "{text}");
+ assert!(text.contains("/update"), "{text}");
+ }
+
+ /// A local build, an unreachable manifest and an already-current binary all
+ /// arrive as `None`, and must leave the transcript untouched.
+ #[test]
+ fn no_update_available_notes_nothing() {
+ let mut app = test_app();
+ let before = app.transcript.len();
+ note_update(&mut app, None);
+ assert_eq!(app.transcript.len(), before);
+ }
+
+ fn transcript_text(app: &App) -> String {
+ app.transcript.iter().map(message_text_of).collect()
+ }
+
+ #[tokio::test]
+ async fn update_command_requests_an_install_once() {
+ let mut app = test_app();
+ run_command(&mut app, "update").await;
+ assert!(app.update_requested, "the loop should pick up the request");
+ assert!(transcript_text(&app).contains("downloading"), "no progress note");
+
+ // A second /update while the first is still downloading must not queue a
+ // concurrent install (two processes rewriting the same binary).
+ app.update_installing = true;
+ app.update_requested = false;
+ run_command(&mut app, "update").await;
+ assert!(!app.update_requested);
+ assert!(transcript_text(&app).contains("already installing"));
+ }
+
+ #[test]
+ fn install_outcome_reports_the_new_version_and_clears_the_flag() {
+ let mut app = test_app();
+ app.update_installing = true;
+ finish_update_install(
+ &mut app,
+ Ok(UpdateOutcome::Installed {
+ from: "0.8.4-10".into(),
+ to: "0.8.4-11".into(),
+ path: std::path::PathBuf::from("/home/u/.local/bin/jan"),
+ }),
+ );
+ assert!(!app.update_installing);
+ let text = transcript_text(&app);
+ assert!(text.contains("0.8.4-10 -> 0.8.4-11"), "{text}");
+ assert!(text.contains("restart"), "must say the swap needs a restart: {text}");
+ }
+
+ #[test]
+ fn a_failed_install_is_reported_and_retryable() {
+ let mut app = test_app();
+ app.update_installing = true;
+ finish_update_install(&mut app, Err("checksum mismatch".into()));
+ assert!(!app.update_installing, "a failure must allow a retry");
+ assert!(transcript_text(&app).contains("checksum mismatch"));
+ }
+
+ #[test]
+ fn an_already_current_binary_is_not_reinstalled() {
+ let mut app = test_app();
+ app.update_installing = true;
+ finish_update_install(
+ &mut app,
+ Ok(UpdateOutcome::UpToDate {
+ version: "0.8.4-11".into(),
+ }),
+ );
+ assert!(!app.update_installing);
+ assert!(transcript_text(&app).contains("0.8.4-11"));
+ }
+
+ fn message_text_of(line: &Line<'static>) -> String {
+ line.spans.iter().map(|s| s.content.as_ref()).collect()
+ }
+
#[test]
fn spinner_advances_only_after_a_full_frame_elapses() {
let mut app = test_app();
diff --git a/src-tauri/src/core/cli/updater.rs b/src-tauri/src/core/cli/updater.rs
index c1491e38f..c9d50ad88 100644
--- a/src-tauri/src/core/cli/updater.rs
+++ b/src-tauri/src/core/cli/updater.rs
@@ -150,25 +150,36 @@ impl AvailableUpdate {
pub fn is_newer(&self) -> bool {
compare_versions(&self.latest, &self.current) == Ordering::Greater
}
+
+ /// One-line "what is newer", shared by the stderr notice, the TUI note and
+ /// `jan update --check`.
+ pub fn summary(&self) -> String {
+ format!(
+ "A new {} build is available: {} -> {}",
+ self.channel, self.current, self.latest
+ )
+ }
}
-/// Check for a newer nightly build and print a one-line notice to stderr.
-/// Best-effort: any network error, timeout, or missing embed is silently
-/// ignored so it never blocks or breaks CLI startup.
-pub async fn print_update_notice_if_available() {
+/// A published build strictly newer than this one, or `None` when the check is
+/// opted out of, this is a local build, the manifest is unreachable, or we are
+/// already current. Best-effort by design: every failure is a silent `None` so
+/// the check never blocks or breaks startup.
+pub async fn available_update() -> Option<AvailableUpdate> {
if std::env::var_os("JAN_CLI_NO_UPDATE_CHECK").is_some() {
- return;
+ return None;
}
- let Ok(update) = check_for_update(CHECK_TIMEOUT).await else {
- return;
- };
- if !update.is_newer() {
- return;
+ let update = check_for_update(CHECK_TIMEOUT).await.ok()?;
+ update.is_newer().then_some(update)
+}
+
+/// Print the startup update notice to stderr. Used by the non-interactive
+/// commands; the TUI notes it in the transcript instead (`tui::note_update`),
+/// since anything written here is lost to the alternate screen.
+pub async fn print_update_notice_if_available() {
+ if let Some(update) = available_update().await {
+ eprintln!("{}. Run `jan update` to install it.", update.summary());
}
- eprintln!(
- "A new {} build is available: {} -> {}. Run `jan update` to install it.",
- update.channel, update.current, update.latest
- );
}
// ── Self-update ────────────────────────────────────────────────────────────
@@ -337,12 +348,41 @@ fn replace_binary(staged: &Path, exe: &Path) -> Result<(), String> {
fs::rename(staged, exe).map_err(|e| format!("{}: {e}", exe.display()))
}
-/// Leftover from a previous Windows update; the file is unlocked once the old
-/// process has exited, so clean it up opportunistically.
-fn clean_stale_backup(exe: &Path) {
+/// Staging-file prefix; the pid suffix keeps two concurrent installs apart.
+const STAGING_PREFIX: &str = ".jan-update-";
+
+/// Drop leftovers from an earlier update: the Windows backup (unlocked once the
+/// old process has exited) and any staging files from a run that was killed
+/// mid-download. `/update` makes that reachable -- quitting the TUI cancels the
+/// task between `download_to`'s awaits, so nothing gets to clean up after it.
+fn clean_stale_files(exe: &Path) {
if cfg!(windows) {
let _ = fs::remove_file(exe.with_extension("old"));
}
+ let Some(dir) = exe.parent() else {
+ return;
+ };
+ let Ok(entries) = fs::read_dir(dir) else {
+ return;
+ };
+ for entry in entries.flatten() {
+ if entry.file_name().to_string_lossy().starts_with(STAGING_PREFIX) && is_stale(&entry) {
+ let _ = fs::remove_file(entry.path());
+ }
+ }
+}
+
+/// True when a staging file is too old to belong to a download still running in
+/// another process: any live one is younger than `DOWNLOAD_TIMEOUT`, since that
+/// is when the request itself gives up. Unreadable timestamps count as fresh so
+/// the sweep never removes a file it can't reason about.
+fn is_stale(entry: &fs::DirEntry) -> bool {
+ entry
+ .metadata()
+ .and_then(|m| m.modified())
+ .ok()
+ .and_then(|t| t.elapsed().ok())
+ .is_some_and(|age| age > DOWNLOAD_TIMEOUT)
}
/// Download the newest build for this channel and replace the running binary
@@ -364,13 +404,13 @@ pub async fn self_update(force: bool) -> Result<UpdateOutcome, String> {
})?;
let exe = target_binary_path()?;
- clean_stale_backup(&exe);
+ clean_stale_files(&exe);
ensure_writable(&exe)?;
let dir = exe.parent().unwrap_or(Path::new("."));
let pid = std::process::id();
- let archive = dir.join(format!(".jan-update-{pid}{}", archive_suffix(&url)));
- let staged = dir.join(format!(".jan-update-{pid}.bin"));
+ let archive = dir.join(format!("{STAGING_PREFIX}{pid}{}", archive_suffix(&url)));
+ let staged = dir.join(format!("{STAGING_PREFIX}{pid}.bin"));
let result = install(&url, update.sha256.as_deref(), &archive, &staged, &exe).await;
let _ = fs::remove_file(&archive);
let _ = fs::remove_file(&staged);
@@ -572,6 +612,36 @@ mod tests {
assert!(!staged.exists());
}
+ /// Quitting the TUI mid-download cancels the install task, leaving staging
+ /// files nothing cleans up; the next install sweeps them. A fresh one may
+ /// belong to a download still running in another process, so it stays.
+ #[test]
+ fn stale_staging_files_are_swept_but_live_ones_are_kept() {
+ let dir = tempfile::tempdir().unwrap();
+ let exe = dir.path().join(BINARY_NAME);
+ fs::write(&exe, b"old").unwrap();
+ let stale = dir.path().join(format!("{STAGING_PREFIX}999.tar.gz"));
+ let live = dir.path().join(format!("{STAGING_PREFIX}1000.tar.gz"));
+ let unrelated = dir.path().join("keep-me.tar.gz");
+ for path in [&stale, &live, &unrelated] {
+ fs::write(path, b"x").unwrap();
+ }
+ let old = std::time::SystemTime::now() - (DOWNLOAD_TIMEOUT + Duration::from_secs(60));
+ File::options()
+ .write(true)
+ .open(&stale)
+ .unwrap()
+ .set_modified(old)
+ .unwrap();
+
+ clean_stale_files(&exe);
+
+ assert!(!stale.exists(), "an abandoned download must be removed");
+ assert!(live.exists(), "a download still in flight must be left alone");
+ assert!(unrelated.exists());
+ assert!(exe.exists(), "the binary itself is never swept");
+ }
+
#[test]
fn unwritable_install_dir_is_rejected_before_downloading() {
let dir = tempfile::tempdir().unwrap();