feat(app): M5 step 1, the admin backend under the management app

- datadir: the data folder layout, the location file (JARVIS_DATA_DIR wins),
  free space for the wizard.
- settings_edit: the fields the five settings screens edit; apply writes only
  non-defaults, keeps unknown keys and writes nothing unless the app's own
  check passes. A memory section reaches the worker's hello.
- secrets: config/secrets.dat, DPAPI for the Windows user; the services take
  the bot token from it before JARVIS_DISCORD_TOKEN.
- logs: the log hub behind the log screen (tabs, daily files kept 14 days,
  the recent error report, a zip under 10 MB, secrets and token-shaped text
  masked); the log macros and the worker's output feed it.

Spec: app/src/admin.spec.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 13:35:59 +09:00
parent 07c701be54
commit 390f6f0b86
18 changed files with 1457 additions and 19 deletions

175
app/src/datadir.rs Normal file
View File

@@ -0,0 +1,175 @@
//! The data folder and the file that remembers where it is (`app/src/admin.spec.md`).
use std::path::{Path, PathBuf};
use serde_json::json;
/// The wizard warns below this much free space (desktop-app.md: 200 GB recommended).
pub const RECOMMENDED_FREE_BYTES: u64 = 200 * 1_000_000_000;
/// Folders inside the data folder (desktop-app.md, "데이터 폴더 구조").
pub const LAYOUT: &[&str] = &[
"config", "runtime", "models/llm", "models/stt", "models/tts", "models/vad", "memory", "browser",
"collect", "cache", "logs",
];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataDir(PathBuf);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataDirError(pub String);
impl std::fmt::Display for DataDirError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for DataDirError {}
fn io(what: &'static str, p: &Path) -> impl FnOnce(std::io::Error) -> DataDirError {
let p = p.display().to_string();
move |e| DataDirError(format!("{what} {p}: {e}"))
}
impl DataDir {
/// Make the layout inside `path` (new or existing); nothing is ever deleted.
pub fn create(path: &Path) -> Result<Self, DataDirError> {
if !path.is_absolute() {
return Err(DataDirError(format!("the data folder must be an absolute path: {}", path.display())));
}
if path.is_file() {
return Err(DataDirError(format!("the data folder is a file: {}", path.display())));
}
for sub in LAYOUT {
let d = path.join(sub);
std::fs::create_dir_all(&d).map_err(io("could not create", &d))?;
}
Ok(Self(path.to_path_buf()))
}
/// Create the folder and remember it in the location file.
pub fn choose(path: &Path, location_file: &Path) -> Result<Self, DataDirError> {
let dir = Self::create(path)?;
let body = json!({"dataDir": path.to_string_lossy()}).to_string();
write_atomic(location_file, body.as_bytes()).map_err(io("could not write", location_file))?;
Ok(dir)
}
/// The folder in use: `JARVIS_DATA_DIR`, else the location file; None before the wizard finished.
pub fn locate(location_file: &Path) -> Option<Self> {
Self::locate_with(std::env::var("JARVIS_DATA_DIR").ok(), location_file)
}
/// `locate` with the environment variable's value given.
pub fn locate_with(env: Option<String>, location_file: &Path) -> Option<Self> {
if let Some(p) = env.filter(|p| !p.is_empty()) {
return Some(Self(PathBuf::from(p)));
}
let raw = std::fs::read_to_string(location_file).ok()?;
let v: serde_json::Value = serde_json::from_str(raw.trim_start_matches('\u{feff}')).ok()?;
let p = PathBuf::from(v.get("dataDir")?.as_str()?);
p.is_absolute().then_some(Self(p))
}
pub fn path(&self) -> &Path {
&self.0
}
pub fn config(&self) -> PathBuf {
self.0.join("config")
}
pub fn settings_file(&self) -> PathBuf {
self.config().join("settings.json")
}
pub fn secrets_file(&self) -> PathBuf {
self.config().join("secrets.dat")
}
pub fn logs(&self) -> PathBuf {
self.0.join("logs")
}
}
/// Where the location file lives on this OS (`%APPDATA%\JarvisBot`, else the XDG config folder).
pub fn default_location_file() -> Option<PathBuf> {
if cfg!(windows) {
let appdata = std::env::var_os("APPDATA")?;
return Some(PathBuf::from(appdata).join("JarvisBot").join("location.json"));
}
let base = std::env::var_os("XDG_CONFIG_HOME").map(PathBuf::from)
.or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".config")))?;
Some(base.join("jarvis-bot").join("location.json"))
}
/// Free bytes on the disk that holds `path` (or its nearest existing parent).
pub fn free_bytes(path: &Path) -> Result<u64, DataDirError> {
let mut p = path;
while !p.exists() {
p = p.parent().ok_or_else(|| DataDirError(format!("no existing parent for {}", path.display())))?;
}
fs4::available_space(p).map_err(io("could not read the free space of", p))
}
/// Write `bytes` to `path` through a temporary file in the same folder and a rename: a crash leaves
/// the old file or the new one, never half of one.
pub fn write_atomic(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let tmp = path.with_extension(format!("tmp{}", std::process::id()));
std::fs::write(&tmp, bytes)?;
std::fs::rename(&tmp, path).inspect_err(|_| {
let _ = std::fs::remove_file(&tmp);
})
}
#[cfg(test)]
pub(crate) fn temp_dir(tag: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("jarvis-{tag}-{}-{}", std::process::id(), rand::random::<u32>()));
std::fs::create_dir_all(&d).unwrap();
d
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_makes_the_layout_and_keeps_what_is_there() {
let root = temp_dir("dd").join("data");
std::fs::create_dir_all(root.join("memory")).unwrap();
std::fs::write(root.join("memory").join("keep.db"), b"x").unwrap();
let d = DataDir::create(&root).unwrap();
for sub in LAYOUT {
assert!(root.join(sub).is_dir(), "{sub}");
}
assert_eq!(std::fs::read(root.join("memory").join("keep.db")).unwrap(), b"x");
assert_eq!(d.settings_file(), root.join("config").join("settings.json"));
}
#[test]
fn a_relative_path_or_a_file_is_refused() {
assert!(DataDir::create(Path::new("relative/data")).unwrap_err().0.contains("absolute"));
let f = temp_dir("ddf").join("a-file");
std::fs::write(&f, b"x").unwrap();
assert!(DataDir::create(&f).unwrap_err().0.contains("file"));
}
#[test]
fn choose_remembers_the_folder_and_locate_finds_it() {
let base = temp_dir("ddl");
let loc = base.join("cfg").join("location.json");
assert_eq!(DataDir::locate_with(None, &loc), None, "no wizard yet");
let data = base.join("데이터");
DataDir::choose(&data, &loc).unwrap();
assert_eq!(DataDir::locate_with(None, &loc), Some(DataDir(data)));
// The environment variable wins (tests, development); an empty one does not count.
assert_eq!(DataDir::locate_with(Some("/elsewhere".into()), &loc).unwrap().path(), Path::new("/elsewhere"));
assert!(DataDir::locate_with(Some(String::new()), &loc).is_some());
}
#[test]
fn free_space_is_read_from_the_nearest_existing_parent() {
let base = temp_dir("ddfree");
let n = free_bytes(&base.join("not").join("yet")).unwrap();
assert!(n > 0);
}
}