Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/environment-variables.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AstrBot Desktop 环境变量清单

更新时间:2026-11-14
更新时间:2026-11-15

主要来源:桌面运行时 Rust 模块(如 `src-tauri/src/backend/config.rs`、`src-tauri/src/update_channel.rs`、`src-tauri/src/bridge/updater_messages.rs`、`src-tauri/src/runtime_paths.rs`、`src-tauri/src/launch_plan.rs`)、资源准备脚本(`scripts/prepare-resources*.mjs`)和发布工作流(`.github/workflows/build-desktop-tauri.yml`)。以下按主要解析/写入阶段分组。

Expand Down Expand Up @@ -31,6 +31,14 @@
| `ASTRBOT_DESKTOP_UPDATER_STABLE_ENDPOINT` | stable 通道 manifest URL 覆盖 | 未设置则读 `plugins.updater.channelEndpoints.stable`,再回退 `plugins.updater.endpoints[0]` |
| `ASTRBOT_DESKTOP_UPDATER_NIGHTLY_ENDPOINT` | nightly 通道 manifest URL 覆盖 | 未设置则读 `plugins.updater.channelEndpoints.nightly` |

### 外部后端与托管后端的地址

`ASTRBOT_BACKEND_URL` 设置桌面端连接的地址,不会改变后端子进程实际使用的 dashboard host/port;后者由 `DASHBOARD_HOST` / `ASTRBOT_DASHBOARD_HOST`、对应端口变量及 `cmd_config.json` 决定。由桌面端托管后端时,这两组配置应指向同一个后端监听端点。

如果只需连接独立运行的本机或远端后端,请同时设置 `ASTRBOT_BACKEND_URL` 和 `ASTRBOT_BACKEND_AUTO_START=0`。首次连接时,地址可达就使用已有服务;地址不可达则报告错误,不自动创建替代后端。非托管后端通过后端 API 重启;重启超时后,桌面端不会自动启动一个托管进程来替代它。

托管进程停止后的重启及 Windows 更新准备,会检查配置地址是否仍有 TCP listener。非回环地址可能是本机局域网地址或指向本机的域名,因此不会仅因地址不是 `localhost` 就跳过检查。如果配置地址指向独立远端服务或持续监听的反向代理,TCP listener 仍存在时会保守拒绝替代启动;该检查无法判断代理后的旧进程是否已经退出。

## 2. 源码与资源准备(开发态运行时 / `prepare-resources` / backend build)

| 变量 | 用途 | 默认值/行为 |
Expand Down
7 changes: 7 additions & 0 deletions src-tauri/src/app_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ pub(crate) const DESKTOP_AUTH_REQUEST_TIMEOUT_MS: u64 = 2_500;
pub(crate) const GRACEFUL_RESTART_START_TIME_TIMEOUT_MS: u64 = 1_800;
pub(crate) const GRACEFUL_RESTART_POLL_INTERVAL_MS: u64 = 350;
pub(crate) const GRACEFUL_STOP_TIMEOUT_MS: u64 = 10_000;
// Upper bound for confirming the previously spawned backend has really left the OS
// before a restart is allowed to spawn a replacement. The stop path can spend up to
// GRACEFUL_STOP_TIMEOUT_MS plus the force-kill follow-up, so this only covers the
// final "PID is gone" hand-off; it must stay short enough that a genuine failure is
// reported to the user instead of turning a restart into a long hang.
pub(crate) const BACKEND_EXIT_CONFIRM_TIMEOUT_MS: u64 = 3_000;
pub(crate) const BACKEND_EXIT_CONFIRM_POLL_INTERVAL_MS: u64 = 100;
#[cfg(target_os = "windows")]
pub(crate) const SYSTEM_SHUTDOWN_STOP_TIMEOUT_MS: u64 = 2_000;
pub(crate) const DEFAULT_BACKEND_READY_POLL_INTERVAL_MS: u64 = 300;
Expand Down
33 changes: 16 additions & 17 deletions src-tauri/src/app_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use std::{
path::PathBuf,
process::Child,
sync::{
atomic::{AtomicBool, Ordering},
atomic::{AtomicBool, AtomicU32, Ordering},
Arc, Mutex,
},
};
use tauri::menu::{CheckMenuItem, MenuItem};

use crate::{backend, desktop_auth::DesktopSessionSecret, exit_state, DEFAULT_BACKEND_URL};
use crate::{
backend, desktop_auth::DesktopSessionSecret, exit_state, process_control, DEFAULT_BACKEND_URL,
};

#[derive(Clone)]
pub(crate) struct TrayMenuState {
Expand Down Expand Up @@ -67,14 +69,23 @@ pub(crate) struct LaunchPlan {
#[derive(Debug)]
pub(crate) struct BackendState {
pub(crate) child: Mutex<Option<Child>>,
/// Identity of the last process this shell spawned, captured while it was still
/// running. Kept separately from `child` because the `Child` handle is consumed or
/// dropped while the OS may still be tearing the process down.
pub(crate) last_spawned_identity: Mutex<Option<process_control::ProcessIdentity>>,
pub(crate) backend_url: String,
/// Keep a timed-out DNS worker reserved until it finishes, so retries stay bounded.
pub(crate) endpoint_resolver: Mutex<Option<std::thread::JoinHandle<()>>>,
pub(crate) restart_auth_token: Mutex<Option<String>>,
pub(crate) desktop_session_secret: DesktopSessionSecret,
pub(crate) startup_loading_mode: Mutex<Option<&'static str>>,
pub(crate) log_rotator_stop: Mutex<Option<Arc<AtomicBool>>>,
pub(crate) exit_state: Mutex<exit_state::ExitStateMachine>,
pub(crate) is_spawning: AtomicBool,
pub(crate) is_restarting: AtomicBool,
/// PID the current restart flow stopped, or 0 when nothing was stopped. Set before
/// the stop so the flow can confirm that exact process left the OS before respawning.
pub(crate) restart_stop_pid: AtomicU32,
}

#[derive(Debug, serde::Serialize)]
Expand Down Expand Up @@ -106,11 +117,6 @@ pub(crate) struct AtomicFlagGuard<'a> {
}

impl<'a> AtomicFlagGuard<'a> {
pub(crate) fn set(flag: &'a AtomicBool) -> Self {
flag.store(true, Ordering::Relaxed);
Self { flag }
}

pub(crate) fn try_set(flag: &'a AtomicBool) -> Option<Self> {
flag.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.ok()?;
Expand All @@ -128,11 +134,13 @@ impl Default for BackendState {
fn default() -> Self {
Self {
child: Mutex::new(None),
last_spawned_identity: Mutex::new(None),
backend_url: backend::config::normalize_backend_url(
&env::var("ASTRBOT_BACKEND_URL")
.unwrap_or_else(|_| DEFAULT_BACKEND_URL.to_string()),
DEFAULT_BACKEND_URL,
),
endpoint_resolver: Mutex::new(None),
restart_auth_token: Mutex::new(None),
desktop_session_secret: DesktopSessionSecret::generate()
.expect("failed to generate secure desktop session secret"),
Expand All @@ -141,6 +149,7 @@ impl Default for BackendState {
exit_state: Mutex::new(exit_state::ExitStateMachine::default()),
is_spawning: AtomicBool::new(false),
is_restarting: AtomicBool::new(false),
restart_stop_pid: AtomicU32::new(0),
}
}
}
Expand All @@ -151,16 +160,6 @@ mod tests {

use super::AtomicFlagGuard;

#[test]
fn atomic_flag_guard_set_resets_flag_on_drop() {
let flag = AtomicBool::new(false);
{
let _guard = AtomicFlagGuard::set(&flag);
assert!(flag.load(Ordering::Relaxed));
}
assert!(!flag.load(Ordering::Relaxed));
}

#[test]
fn atomic_flag_guard_try_set_rejects_double_set_until_drop() {
let flag = AtomicBool::new(false);
Expand Down
Loading
Loading