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

454
app/src/logs.rs Normal file
View File

@@ -0,0 +1,454 @@
//! The log hub behind the log screen (`app/src/admin.spec.md`, "Logs"): every line the app logs,
//! by service tab, kept in memory for the screen and in daily files under `logs/`.
use std::collections::{HashMap, VecDeque};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use chrono::{DateTime, Local, NaiveDate};
/// Lines kept in memory for the screen.
pub const KEPT_LINES: usize = 5_000;
/// Log files older than this are removed.
pub const RETENTION_DAYS: i64 = 14;
/// Discord's message limit: a longer error report is offered as a download.
pub const DISCORD_MESSAGE_CHARS: usize = 2_000;
/// Discord's file limit for the zip export.
pub const ZIP_MAX_BYTES: usize = 10 * 1024 * 1024;
const REPORT_BEFORE: usize = 30;
const REPORT_AFTER: usize = 10;
pub const SECRET_MARK: &str = "[비밀값]";
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Level {
Debug,
Info,
Warn,
Error,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct LogLine {
pub seq: u64,
/// Milliseconds since the Unix epoch.
pub at_ms: i64,
pub level: Level,
pub tab: String,
pub text: String,
}
/// The screen's tab for a log category (worker lines: by the worker's own category, see `push_worker`).
pub fn tab_of(category: &str) -> &'static str {
match category {
"voice" | "receive" | "play" | "discord" => "discord",
"chat" => "chat",
"conversation" => "conversation",
"worker" | "voice_worker" => "worker",
"stt" => "stt",
"llm" => "llm",
"tts" => "tts",
"browser" => "browser",
"capture" => "capture",
"game" => "game",
_ => "app",
}
}
#[derive(Default)]
struct State {
lines: VecDeque<LogLine>,
next_seq: u64,
files: HashMap<(String, NaiveDate), std::fs::File>,
secrets: Vec<String>,
last_cleanup: Option<NaiveDate>,
}
pub struct LogHub {
dir: Option<PathBuf>,
state: Mutex<State>,
}
impl LogHub {
/// A hub writing daily files to `dir` (None: memory only).
pub fn new(dir: Option<&Path>) -> Arc<Self> {
let hub = Arc::new(Self { dir: dir.map(Path::to_path_buf), state: Mutex::default() });
hub.cleanup(Local::now().date_naive());
hub
}
/// The stored secrets' values, masked in every copy and export.
pub fn set_secrets(&self, values: Vec<String>) {
self.state.lock().unwrap().secrets = values;
}
pub fn push(&self, level: Level, category: &str, text: &str) {
self.push_at(Local::now(), level, tab_of(category), text);
}
/// A line of the voice worker's output: its `[ category ]` prefix names the tab (stt, llm, tts,
/// else worker); a Python warning or traceback line is a warning.
pub fn push_worker(&self, line: &str) {
let (cat, text) = match line.strip_prefix('[').and_then(|r| r.split_once(']')) {
Some((c, rest)) => (c.trim(), rest.trim_start()),
None => ("worker", line),
};
let tab = match tab_of(cat) {
t @ ("stt" | "llm" | "tts") => t,
_ => "worker",
};
let level = if text.contains("Error") || text.contains("Traceback") { Level::Error }
else if text.contains("Warning") { Level::Warn } else { Level::Info };
self.push_at(Local::now(), level, tab, text);
}
fn push_at(&self, at: DateTime<Local>, level: Level, tab: &str, text: &str) {
let mut s = self.state.lock().unwrap();
let line = LogLine { seq: s.next_seq, at_ms: at.timestamp_millis(), level, tab: tab.into(), text: text.into() };
s.next_seq += 1;
if let Some(dir) = &self.dir {
let day = at.date_naive();
let key = (tab.to_owned(), day);
if !s.files.contains_key(&key) {
s.files.retain(|(_, d), _| *d == day); // yesterday's files are done
let path = dir.join(format!("{tab}-{day}.log"));
if let Ok(f) = std::fs::OpenOptions::new().create(true).append(true).open(path) {
s.files.insert(key.clone(), f);
}
}
if let Some(f) = s.files.get_mut(&key) {
let _ = writeln!(f, "{}", format_line(&line));
}
}
s.lines.push_back(line);
if s.lines.len() > KEPT_LINES {
s.lines.pop_front();
}
let today = at.date_naive();
let due = s.last_cleanup != Some(today);
drop(s);
if due {
self.cleanup(today);
}
}
/// Remove log files older than the retention.
fn cleanup(&self, today: NaiveDate) {
self.state.lock().unwrap().last_cleanup = Some(today);
let Some(dir) = &self.dir else { return };
let Ok(entries) = std::fs::read_dir(dir) else { return };
for e in entries.flatten() {
let name = e.file_name().to_string_lossy().into_owned();
if let Some(day) = file_day(&name) {
if (today - day).num_days() > RETENTION_DAYS {
let _ = std::fs::remove_file(e.path());
}
}
}
}
/// Lines for the screen: one tab (None: all), this level and above, containing `find`, after
/// sequence number `after` (live follow), the last `limit`.
pub fn query(&self, tab: Option<&str>, min_level: Level, find: Option<&str>, after: Option<u64>, limit: usize)
-> Vec<LogLine> {
let s = self.state.lock().unwrap();
let find = find.map(str::to_lowercase);
let mut out: Vec<LogLine> = s.lines.iter()
.filter(|l| tab.is_none_or(|t| l.tab == t) && l.level >= min_level
&& after.is_none_or(|a| l.seq > a)
&& find.as_deref().is_none_or(|f| l.text.to_lowercase().contains(f)))
.cloned().collect();
if out.len() > limit {
out.drain(..out.len() - limit);
}
out
}
/// The last error with the lines around it, the app version and a PC summary, masked; None
/// without an error.
pub fn recent_error_report(&self, app_version: &str) -> Option<String> {
let s = self.state.lock().unwrap();
let i = s.lines.iter().rposition(|l| l.level == Level::Error)?;
let from = i.saturating_sub(REPORT_BEFORE);
let to = (i + 1 + REPORT_AFTER).min(s.lines.len());
let mut text = format!("Jarvis {app_version} | {}\n", pc_summary());
for l in s.lines.range(from..to) {
text.push_str(&format_line(l));
text.push('\n');
}
Some(redact(&text, &s.secrets))
}
/// The log files of days `from..=to` in a zip at `out`, masked, at most `ZIP_MAX_BYTES`: when
/// larger, the oldest lines are left out and NOTE.txt says how many. Returns the lines left out.
pub fn export_zip(&self, from: NaiveDate, to: NaiveDate, out: &Path) -> std::io::Result<usize> {
let secrets = self.state.lock().unwrap().secrets.clone();
let Some(dir) = &self.dir else { return Err(std::io::Error::other("no log folder")) };
let mut files: Vec<(NaiveDate, String, Vec<String>)> = vec![];
for e in std::fs::read_dir(dir)?.flatten() {
let name = e.file_name().to_string_lossy().into_owned();
let Some(day) = file_day(&name) else { continue };
if day < from || day > to {
continue;
}
let text = std::fs::read_to_string(e.path()).unwrap_or_default();
files.push((day, name, redact(&text, &secrets).lines().map(str::to_owned).collect()));
}
files.sort();
let mut dropped = 0usize;
loop {
let bytes = build_zip(&files, dropped)?;
if bytes.len() <= ZIP_MAX_BYTES {
std::fs::write(out, bytes)?;
return Ok(dropped);
}
let total: usize = files.iter().map(|f| f.2.len()).sum();
if dropped >= total {
return Err(std::io::Error::other("the logs do not fit in the zip limit"));
}
// Drop a fifth more of the oldest lines and try again.
dropped = (dropped + total.div_ceil(5)).min(total);
}
}
}
/// The zip with the oldest `dropped` lines (oldest files first) left out.
fn build_zip(files: &[(NaiveDate, String, Vec<String>)], dropped: usize) -> std::io::Result<Vec<u8>> {
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
let mut skip = dropped;
for (_, name, lines) in files {
let cut = skip.min(lines.len());
skip -= cut;
if cut == lines.len() {
continue;
}
zip.start_file(name.as_str(), opts).map_err(std::io::Error::other)?;
for l in &lines[cut..] {
zip.write_all(l.as_bytes())?;
zip.write_all(b"\n")?;
}
}
if dropped > 0 {
zip.start_file("NOTE.txt", opts).map_err(std::io::Error::other)?;
zip.write_all(format!("The oldest {dropped} lines were left out to stay under 10 MB.\n").as_bytes())?;
}
Ok(zip.finish().map_err(std::io::Error::other)?.into_inner())
}
fn file_day(name: &str) -> Option<NaiveDate> {
let stem = name.strip_suffix(".log")?;
let date = stem.get(stem.len().checked_sub(10)?..)?;
NaiveDate::parse_from_str(date, "%Y-%m-%d").ok()
}
fn format_line(l: &LogLine) -> String {
let at = DateTime::from_timestamp_millis(l.at_ms).map(|t| t.with_timezone(&Local).format("%Y-%m-%d %H:%M:%S%.3f").to_string())
.unwrap_or_default();
let level = match l.level { Level::Debug => "DEBUG", Level::Info => "INFO", Level::Warn => "WARN", Level::Error => "ERROR" };
format!("{at} {level:5} [{}] {}", l.tab, l.text)
}
fn token_char(c: char) -> bool {
c.is_ascii_alphanumeric() || c == '_' || c == '-'
}
/// A Discord bot token: three base64url parts joined by dots (id, time, signature).
fn looks_like_token(word: &str) -> bool {
let parts: Vec<&str> = word.split('.').collect();
parts.len() == 3 && parts[0].len() >= 20 && parts[1].len() >= 5 && parts[2].len() >= 25
&& parts.iter().all(|p| p.chars().all(token_char))
}
/// Mask each stored secret and anything shaped like a Discord bot token.
pub fn redact(text: &str, secrets: &[String]) -> String {
let mut out = text.to_owned();
for s in secrets.iter().filter(|s| s.len() >= 6) {
out = out.replace(s.as_str(), SECRET_MARK);
}
let mut result = String::with_capacity(out.len());
let mut word = String::new();
let flush = |word: &mut String, result: &mut String| {
if looks_like_token(word) { result.push_str(SECRET_MARK) } else { result.push_str(word) }
word.clear();
};
for c in out.chars() {
if token_char(c) || c == '.' {
word.push(c);
} else {
flush(&mut word, &mut result);
result.push(c);
}
}
flush(&mut word, &mut result);
result
}
/// OS, CPU threads and memory, for error reports.
pub fn pc_summary() -> String {
let cpus = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(0);
let mem = total_memory_bytes().map(|b| format!("{:.1} GB RAM", b as f64 / 1e9)).unwrap_or_default();
format!("{} {} | {cpus} CPU threads | {mem}", std::env::consts::OS, std::env::consts::ARCH)
}
#[cfg(target_os = "linux")]
fn total_memory_bytes() -> Option<u64> {
let s = std::fs::read_to_string("/proc/meminfo").ok()?;
let kb: u64 = s.lines().find(|l| l.starts_with("MemTotal:"))?.split_whitespace().nth(1)?.parse().ok()?;
Some(kb * 1024)
}
#[cfg(windows)]
fn total_memory_bytes() -> Option<u64> {
use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX};
// SAFETY: the struct is sized and zeroed as the API asks.
let mut m: MEMORYSTATUSEX = unsafe { std::mem::zeroed() };
m.dwLength = std::mem::size_of::<MEMORYSTATUSEX>() as u32;
(unsafe { GlobalMemoryStatusEx(&mut m) } != 0).then_some(m.ullTotalPhys)
}
#[cfg(not(any(target_os = "linux", windows)))]
fn total_memory_bytes() -> Option<u64> {
None
}
static HUB: OnceLock<Arc<LogHub>> = OnceLock::new();
/// Make the log macros feed `hub` too (once per process).
pub fn install(hub: Arc<LogHub>) {
let _ = HUB.set(hub);
}
pub fn installed() -> Option<&'static Arc<LogHub>> {
HUB.get()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::datadir::temp_dir;
use chrono::TimeZone;
fn at(y: i32, m: u32, d: u32) -> DateTime<Local> {
Local.with_ymd_and_hms(y, m, d, 12, 0, 0).unwrap()
}
#[test]
fn categories_map_to_tabs_and_worker_lines_to_their_own() {
let hub = LogHub::new(None);
hub.push(Level::Debug, "play", "say id=r1");
hub.push(Level::Error, "nonsense", "x");
hub.push_worker("[ voice_worker ] brain reply ms=600");
hub.push_worker("[ stt ] batch=3");
hub.push_worker("/x.py:9: FutureWarning: deprecated");
let tabs: Vec<_> = hub.query(None, Level::Debug, None, None, 99).into_iter().map(|l| (l.tab, l.level)).collect();
assert_eq!(tabs, vec![("discord".into(), Level::Debug), ("app".into(), Level::Error),
("worker".into(), Level::Info), ("stt".into(), Level::Info), ("worker".into(), Level::Warn)]);
}
#[test]
fn query_filters_by_tab_level_text_and_follows() {
let hub = LogHub::new(None);
for i in 0..10 {
hub.push(if i % 2 == 0 { Level::Debug } else { Level::Error }, "chat", &format!("line {i}"));
}
hub.push(Level::Error, "voice", "joined");
assert_eq!(hub.query(Some("chat"), Level::Error, None, None, 99).len(), 5);
assert_eq!(hub.query(None, Level::Debug, Some("LINE 3"), None, 99)[0].text, "line 3");
let last = hub.query(None, Level::Debug, None, None, 3);
assert_eq!(last.iter().map(|l| l.seq).collect::<Vec<_>>(), vec![8, 9, 10]);
assert_eq!(hub.query(None, Level::Debug, None, Some(9), 99).len(), 1, "only lines after seq 9");
for i in 0..KEPT_LINES {
hub.push(Level::Debug, "app", &i.to_string());
}
assert_eq!(hub.query(None, Level::Debug, None, None, usize::MAX).len(), KEPT_LINES);
}
#[test]
fn lines_go_to_daily_files_and_old_files_are_removed() {
let dir = temp_dir("logs");
std::fs::write(dir.join("chat-2026-01-01.log"), "old\n").unwrap();
std::fs::write(dir.join("notes.txt"), "not a log").unwrap();
let hub = LogHub::new(Some(&dir));
hub.push_at(at(2026, 1, 10), Level::Info, "chat", "kept");
hub.push_at(at(2026, 1, 20), Level::Info, "chat", "next day");
assert!(std::fs::read_to_string(dir.join("chat-2026-01-10.log")).unwrap().contains("kept"));
assert!(std::fs::read_to_string(dir.join("chat-2026-01-20.log")).unwrap().contains("next day"));
// The 20th is more than 14 days after the 1st.
assert!(!dir.join("chat-2026-01-01.log").exists());
assert!(dir.join("notes.txt").exists());
}
#[test]
fn the_error_report_holds_the_last_error_and_its_neighbours_masked() {
let hub = LogHub::new(None);
assert_eq!(hub.recent_error_report("0.1.0"), None);
hub.set_secrets(vec!["s3cr3t-value".into()]);
for i in 0..50 {
hub.push(Level::Debug, "app", &format!("before {i}"));
}
hub.push(Level::Error, "discord", "login failed with s3cr3t-value");
for i in 0..20 {
hub.push(Level::Debug, "app", &format!("after {i}"));
}
let r = hub.recent_error_report("0.1.0").unwrap();
assert!(r.starts_with("Jarvis 0.1.0 | "));
assert!(r.contains("before 49") && r.contains("before 20") && !r.contains("before 19"));
assert!(r.contains("after 9") && !r.contains("after 10"));
assert!(r.contains("login failed with [비밀값]") && !r.contains("s3cr3t"));
}
#[test]
fn redaction_masks_secrets_and_token_shaped_text() {
let token = format!("{}.{}.{}", "M".repeat(24), "GabcdE", "x".repeat(30));
let out = redact(&format!("token={token}, ok id=123456789012345678 a.b.c"), &[]);
assert_eq!(out, "token=[비밀값], ok id=123456789012345678 a.b.c");
}
#[test]
fn the_zip_stays_under_the_limit_by_leaving_out_the_oldest_lines() {
let dir = temp_dir("logzip");
let hub = LogHub::new(Some(&dir));
hub.set_secrets(vec!["hidden-token".into()]);
hub.push_at(at(2026, 3, 1), Level::Info, "app", "day one hidden-token");
hub.push_at(at(2026, 3, 2), Level::Info, "app", "day two");
hub.push_at(at(2026, 3, 5), Level::Info, "app", "outside the range");
let out = dir.join("small.zip");
assert_eq!(hub.export_zip(NaiveDate::from_ymd_opt(2026, 3, 1).unwrap(),
NaiveDate::from_ymd_opt(2026, 3, 2).unwrap(), &out).unwrap(), 0);
let mut z = zip::ZipArchive::new(std::fs::File::open(&out).unwrap()).unwrap();
let mut names: Vec<String> = (0..z.len()).map(|i| z.by_index(i).unwrap().name().unwrap().to_string()).collect();
names.sort();
assert_eq!(names, vec!["app-2026-03-01.log", "app-2026-03-02.log"]);
let mut first = String::new();
std::io::Read::read_to_string(&mut z.by_name("app-2026-03-01.log").unwrap(), &mut first).unwrap();
assert!(first.contains("day one [비밀값]"));
// Incompressible lines past 10 MB: the oldest go, the newest stay.
let big = temp_dir("logbig");
let hub = LogHub::new(Some(&big));
let mut seed = 1u64;
for day in 1..=3 {
for _ in 0..40_000 {
let mut s = String::new();
for _ in 0..140 {
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
s.push(char::from(b'!' + (seed >> 58) as u8));
}
hub.push_at(at(2026, 4, day), Level::Info, "app", &s);
}
}
hub.push_at(at(2026, 4, 3), Level::Info, "app", "the newest line");
let out = big.join("big.zip");
let dropped = hub.export_zip(NaiveDate::from_ymd_opt(2026, 4, 1).unwrap(),
NaiveDate::from_ymd_opt(2026, 4, 3).unwrap(), &out).unwrap();
assert!(dropped > 0);
assert!(std::fs::metadata(&out).unwrap().len() as usize <= ZIP_MAX_BYTES);
let mut z = zip::ZipArchive::new(std::fs::File::open(&out).unwrap()).unwrap();
let mut last = String::new();
std::io::Read::read_to_string(&mut z.by_name("app-2026-04-03.log").unwrap(), &mut last).unwrap();
assert!(last.contains("the newest line"));
let mut note = String::new();
std::io::Read::read_to_string(&mut z.by_name("NOTE.txt").unwrap(), &mut note).unwrap();
assert!(note.contains(&dropped.to_string()));
}
}