Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(auth): surface Claude Code keychain alias use
thinhlpg51865c18a3a5
diff --git a/.gitignore b/.gitignoreindex b75dc9e69..221ddba0d 100644--- a/.gitignore+++ b/.gitignore@@ -78,10 +78,3 @@ JAN.md## agent.jan-## local working artifacts (not source)-.pi/-.vscode/-docs/superpowers/-do-not-commit-*.png--.worktrees/\ No newline at end of filediff --git a/src-tauri/src/core/agent/global_config.rs b/src-tauri/src/core/agent/global_config.rsindex aa446bc20..711324de7 100644--- a/src-tauri/src/core/agent/global_config.rs+++ b/src-tauri/src/core/agent/global_config.rs@@ -33,6 +33,8 @@ const GLOBAL_CONFIG_TEMPLATE: &str = r#"# Jan Agent global provider config.# # /terminal-setup when this terminal is# # dropping Shift+Enter or Option+Delete.# # On by default+# claude_code_alias = false # allow Jan to reuse Claude Code's+# # keychain login; on by default# wave = "π" # sweep this glyph along the working row# # instead of the static throbber. Up to# # 3 characters ("π", "~", "ποΈπποΈ").@@ -81,6 +83,10 @@ struct GlobalConfigToml {/// for the check to know it was answered.#[serde(default, skip_serializing_if = "Option::is_none")]terminal_hint: Option<bool>,+ /// Allow Jan to reuse Claude Code's keychain login. `None` = the default,+ /// on; set false to keep Jan from reading or refreshing that credential.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ claude_code_alias: Option<bool>,/// Glyph swept along the working row while a turn runs, in place of the/// static Braille throbber. Absent = `WAVE_DEFAULT`; `""` = off, the/// throbber. See `wave_glyph` for why those are two different things.@@ -216,6 +222,17 @@ pub(crate) fn terminal_hint_enabled() -> bool {.unwrap_or(true)}+/// Whether Jan may reuse Claude Code's keychain login+/// (`claude_code_alias` in `~/.jan/config.toml`), defaulting to on. This+/// setting is deliberately opt-out to preserve existing behavior while making+/// the cross-tool credential reuse explicit and reversible.+pub(crate) fn claude_code_alias_enabled() -> bool {+ load_raw()+ .ok()+ .and_then(|config| config.claude_code_alias)+ .unwrap_or(true)+}+/// Whether `bash` runs sandboxed by default (`sandbox` in `~/.jan/config.toml`)./// `None` when unset, so the caller can let a project's `agent.toml` or the/// `--sandbox` flag decide before falling back to the surface default.@@ -545,6 +562,26 @@ mod tests {});}+ #[test]+ fn claude_code_alias_defaults_on_and_reads_the_toml_key() {+ with_temp_home(|_| {+ assert!(claude_code_alias_enabled(), "missing file -> alias on");+ let path = ensure_global_config().expect("ensure");+ assert!(claude_code_alias_enabled(), "scaffolded file -> alias on");++ std::fs::write(&path, "claude_code_alias = false\n").unwrap();+ assert!(!claude_code_alias_enabled());+ std::fs::write(&path, "claude_code_alias = true\n").unwrap();+ assert!(claude_code_alias_enabled());++ std::fs::write(&path, "not valid toml [[[").unwrap();+ assert!(+ claude_code_alias_enabled(),+ "an unreadable config keeps the default"+ );+ });+ }+#[test]fn think_tags_default_on_and_read_from_the_toml_key() {with_temp_home(|_| {diff --git a/src-tauri/src/core/cli/auth/account.rs b/src-tauri/src/core/cli/auth/account.rsindex fafe41ef5..fba296830 100644--- a/src-tauri/src/core/cli/auth/account.rs+++ b/src-tauri/src/core/cli/auth/account.rs@@ -679,6 +679,9 @@ fn claude_code_keychain_entry() -> Option<keyring::Entry> {/// momentarily exhausted is not misreported as a free personal plan. Best-effort:/// returns `None` when the keychain entry is absent or the field is missing.fn claude_code_subscription_type() -> Option<String> {+ if !claude_alias_enabled() {+ return None;+ }claude_code_keychain_entry()?.get_password().ok()@@ -691,6 +694,12 @@ fn claude_code_subscription_type() -> Option<String> {})}+pub(crate) const CLAUDE_ALIAS_NOTICE: &str =+ "Claude Code keychain login is active: Jan may use and refresh that credential for Claude requests. Disable with `claude_code_alias = false` in ~/.jan/config.toml or `/settings`. Jan is a third-party client; review Anthropic's Terms of Service.";++static CLAUDE_ALIAS_ENGAGED: std::sync::atomic::AtomicBool =+ std::sync::atomic::AtomicBool::new(false);+#[cfg(test)]static CLAUDE_ALIAS_ENABLED: std::sync::atomic::AtomicBool =std::sync::atomic::AtomicBool::new(false);@@ -702,7 +711,7 @@ fn claude_alias_enabled() -> bool {#[cfg(not(test))]fn claude_alias_enabled() -> bool {- true+ crate::core::agent::global_config::claude_code_alias_enabled()}/// Test-only: flip whether the Claude Code alias is consulted in production@@ -712,6 +721,16 @@ fn claude_alias_enabled() -> bool {pub(crate) fn set_claude_alias(enabled: bool) {CLAUDE_ALIAS_ENABLED.store(enabled, std::sync::atomic::Ordering::Relaxed);}++pub(crate) fn take_claude_alias_engaged() -> bool {+ CLAUDE_ALIAS_ENGAGED.swap(false, std::sync::atomic::Ordering::Relaxed)+}++#[cfg(test)]+pub(crate) fn mark_claude_alias_engaged_for_test() {+ CLAUDE_ALIAS_ENGAGED.store(true, std::sync::atomic::Ordering::Relaxed);+}+/// Resolve a working access token from Claude Code's keychain entry, refreshing/// it and writing the rotated token back into the same keychain entry (the/// single source of truth omp also reads) when it has expired. This keeps Jan@@ -729,7 +748,7 @@ async fn claude_code_access_token() -> Option<String> {.duration_since(std::time::UNIX_EPOCH).ok()?.as_secs() as i64;- if oauth.expires_at.is_some_and(|expires_at| expires_at <= now + 300) {+ if oauth.expires_at.is_some_and(|expires_at| expires_at <= now) {match refresh(AccountProvider::Claude, &oauth).await {Ok(fresh) => {// Write the rotated token back into omp's keychain entry so the@@ -739,6 +758,8 @@ async fn claude_code_access_token() -> Option<String> {if write_claude_code_keychain(&entry, &raw, &fresh).is_err() {debug_log("claude alias: refreshed but could not write back to the Claude Code keychain");}+ #[cfg(not(test))]+ CLAUDE_ALIAS_ENGAGED.store(true, std::sync::atomic::Ordering::Relaxed);return Some(fresh.access_token);}Err(error) => {@@ -749,6 +770,8 @@ async fn claude_code_access_token() -> Option<String> {}}}+ #[cfg(not(test))]+ CLAUDE_ALIAS_ENGAGED.store(true, std::sync::atomic::Ordering::Relaxed);Some(oauth.access_token)}@@ -2034,6 +2057,13 @@ mod tests {assert_eq!(claude_code_access_token().await, None);}+ #[test]+ fn claude_alias_engagement_latch_is_one_shot() {+ CLAUDE_ALIAS_ENGAGED.store(true, std::sync::atomic::Ordering::Relaxed);+ assert!(take_claude_alias_engaged());+ assert!(!take_claude_alias_engaged());+ }+#[tokio::test(flavor = "current_thread")]async fn claude_alias_toggle_controls_the_lookup_gate() {let _guard = alias_test_lock().await;diff --git a/src-tauri/src/core/cli/mod.rs b/src-tauri/src/core/cli/mod.rsindex f7af36a8d..02f9fe950 100644--- a/src-tauri/src/core/cli/mod.rs+++ b/src-tauri/src/core/cli/mod.rs@@ -1298,6 +1298,12 @@ pub fn agent_dir_for(project_root: &std::path::Path) -> PathBuf {/// run can be piped; progress/diagnostics go to stderr. `PermissionRequest` is/// resolved via the terminal (deny when non-interactive).async fn print_event(ev: StreamEvent, registry: &PermissionRegistry) {+ if crate::core::cli::auth::account::take_claude_alias_engaged() {+ eprintln!(+ "\x1b[33m[warning] {}\x1b[0m",+ crate::core::cli::auth::account::CLAUDE_ALIAS_NOTICE+ );+ }match ev {StreamEvent::Token { text } => {print!("{text}");diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rsindex 91e95916e..bbf392161 100644--- a/src-tauri/src/core/cli/tui.rs+++ b/src-tauri/src/core/cli/tui.rs@@ -6551,14 +6551,24 @@ pub async fn run(res}+const CLAUDE_ALIAS_NOTICE: &str =+ crate::core::cli::auth::account::CLAUDE_ALIAS_NOTICE;++fn note_claude_alias_if_engaged(app: &mut App) {+ if crate::core::cli::auth::account::take_claude_alias_engaged() {+ app.system(Level::Warn, CLAUDE_ALIAS_NOTICE);+ }+}/// Apply one event from the active run's stream, clearing `current` on a/// terminal one. `None` is a stream that closed without a terminal event (an/// aborted task).+async fn apply_stream_event(app: &mut App,ev: Option<StreamEvent>,current: &mut Option<CurrentRun>,) {+ note_claude_alias_if_engaged(app);match ev {Some(StreamEvent::Done { stop_reason, usage }) => {app.on_done(stop_reason, usage);@@ -9158,6 +9168,13 @@ const AGENT_SETTINGS: &[AgentSettingDef] = &[kind: AgentSettingKind::Bool { default: true },scope: SettingScope::Project,},+ AgentSettingDef {+ key: "claude_code_alias",+ label: "claude_code_alias",+ desc: "reuse and refresh Claude Code's keychain login for Claude requests",+ kind: AgentSettingKind::Bool { default: true },+ scope: SettingScope::Global,+ },AgentSettingDef {key: "wave",label: "wave",@@ -14688,7 +14705,11 @@ mod tests {ProviderField,autoscroll_selection, selection_text, Selection, SelectionMode, COPY_NOTICE,startup_modes, KITTY_KEYS_OFF, KITTY_KEYS_ON,- backgrounded_job_id, drain_stream_events, run_command, starting_call_lines,+ backgrounded_job_id,+ apply_stream_event,+ drain_stream_events,+ run_command,+ starting_call_lines,unescape_partial_json_string,running_group_rows, split_reasoning, strip_system_xml_tags, subagent_activity,answer_without_reasoning, assistant_runs, replay_display_log, thinking_open,@@ -14921,6 +14942,30 @@ mod tests {assert_eq!(app.transcript.len(), before);}+ #[tokio::test]+ async fn claude_alias_notice_is_rendered_once() {+ crate::core::cli::auth::account::mark_claude_alias_engaged_for_test();+ let mut app = test_app();+ let mut current = None;++ apply_stream_event(+ &mut app,+ Some(StreamEvent::Step { index: 1, max: 0 }),+ &mut current,+ )+ .await;+ let first = transcript_text(&app);+ assert!(first.contains("Claude Code keychain"));++ apply_stream_event(+ &mut app,+ Some(StreamEvent::Step { index: 2, max: 0 }),+ &mut current,+ )+ .await;+ let second = transcript_text(&app);+ assert_eq!(second.matches("Claude Code keychain").count(), 1);+ }fn transcript_text(app: &App) -> String {app.transcript.iter().map(message_text_of).collect()}