The brain gets a schedule tool (add, list, delete the asker's own reminders, timers and tasks) offered only with a schedule backend. The voice worker asks the app through schedule_action and runs fired tasks as reply via task with read-only tools, no MCP and a fresh short-term memory. The app keeps the schedules in config/schedules.json, fires them every 5 s, speaks them when the person is in Jarvis's voice channel and otherwise posts in the chat channel they asked in (or schedule.chatChannel for voice). A feature switch and a 예약 screen list and delete everyone's schedules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
622 lines
28 KiB
Rust
622 lines
28 KiB
Rust
//! 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" | "schedule" => "conversation",
|
|
"worker" | "voice_worker" => "worker",
|
|
"stt" => "stt",
|
|
"llm" => "llm",
|
|
"tts" => "tts",
|
|
"browser" => "browser",
|
|
"capture" => "capture",
|
|
"game" => "game",
|
|
"install" => "install",
|
|
_ => "app",
|
|
}
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct State {
|
|
/// Where the daily files go; None: memory only (no data folder yet).
|
|
dir: Option<PathBuf>,
|
|
lines: VecDeque<LogLine>,
|
|
next_seq: u64,
|
|
files: HashMap<(String, NaiveDate), std::fs::File>,
|
|
secrets: Vec<String>,
|
|
/// The values the running services were started with (`set_secrets_in_use`).
|
|
in_use: Vec<String>,
|
|
last_cleanup: Option<NaiveDate>,
|
|
}
|
|
|
|
impl State {
|
|
/// Every value to mask: the stored secrets and those the running services use.
|
|
fn masked(&self) -> Vec<String> {
|
|
let mut all = self.secrets.clone();
|
|
all.extend(self.in_use.iter().filter(|v| !self.secrets.contains(v)).cloned());
|
|
all
|
|
}
|
|
}
|
|
|
|
pub struct LogHub {
|
|
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 { state: Mutex::new(State { dir: dir.map(Path::to_path_buf), ..State::default() }) });
|
|
hub.cleanup(Local::now().date_naive());
|
|
hub
|
|
}
|
|
|
|
/// Start writing daily files to `dir` (the data folder was just chosen); lines kept in memory
|
|
/// stay there, the next ones also go to the files.
|
|
pub fn set_dir(&self, dir: &Path) {
|
|
{
|
|
let mut s = self.state.lock().unwrap();
|
|
s.dir = Some(dir.to_path_buf());
|
|
s.files.clear();
|
|
}
|
|
self.cleanup(Local::now().date_naive());
|
|
}
|
|
|
|
/// The stored secrets' values, masked in every line from now on (and again in copies and exports).
|
|
pub fn set_secrets(&self, values: Vec<String>) {
|
|
self.state.lock().unwrap().secrets = values;
|
|
}
|
|
|
|
/// The secret values the running services were started with (the bot token, the MCP servers'
|
|
/// values). They stay masked until the next start, also after one is replaced or removed in
|
|
/// the store: the running worker and MCP servers keep using them until they are restarted.
|
|
pub fn set_secrets_in_use(&self, values: Vec<String>) {
|
|
self.state.lock().unwrap().in_use = 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.
|
|
/// Every value masked right now (stored and in use). A worker run keeps these for its lines
|
|
/// (`push_worker_masked`), so a line it writes late is masked even after the next start.
|
|
pub fn masked_values(&self) -> Vec<String> {
|
|
self.state.lock().unwrap().masked()
|
|
}
|
|
|
|
pub fn push_worker(&self, line: &str) {
|
|
self.push_worker_masked(line, &[]);
|
|
}
|
|
|
|
/// `push_worker`, also masking `extra` (the values of the run that wrote the line).
|
|
pub fn push_worker_masked(&self, line: &str, extra: &[String]) {
|
|
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_masked(Local::now(), level, tab, text, extra);
|
|
}
|
|
|
|
fn push_at(&self, at: DateTime<Local>, level: Level, tab: &str, text: &str) {
|
|
self.push_masked(at, level, tab, text, &[]);
|
|
}
|
|
|
|
fn push_masked(&self, at: DateTime<Local>, level: Level, tab: &str, text: &str, extra: &[String]) {
|
|
let mut s = self.state.lock().unwrap();
|
|
// Masked before it is kept or written, so a secret replaced or removed later (the hub then
|
|
// no longer knows the old value) never shows on the screen, in a copy, a file or an export.
|
|
let mut masks = s.masked();
|
|
masks.extend(extra.iter().cloned());
|
|
let text = redact(text, &masks);
|
|
let line = LogLine { seq: s.next_seq, at_ms: at.timestamp_millis(), level, tab: tab.into(), text };
|
|
s.next_seq += 1;
|
|
if let Some(dir) = s.dir.clone() {
|
|
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) {
|
|
let dir = {
|
|
let mut s = self.state.lock().unwrap();
|
|
s.last_cleanup = Some(today);
|
|
s.dir.clone()
|
|
};
|
|
let Some(dir) = 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.masked()))
|
|
}
|
|
|
|
/// 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, dir) = {
|
|
let s = self.state.lock().unwrap();
|
|
(s.masked(), s.dir.clone())
|
|
};
|
|
let Some(dir) = 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 `dropped` oldest lines left out: oldest by the time each line was logged, across
|
|
/// every tab's file (a line's time is its first 23 characters, `YYYY-MM-DD HH:MM:SS.mmm`; lines
|
|
/// without one count as oldest).
|
|
fn build_zip(files: &[(NaiveDate, String, Vec<String>)], dropped: usize) -> std::io::Result<Vec<u8>> {
|
|
let mut order: Vec<(&str, usize, usize)> = files.iter().enumerate()
|
|
.flat_map(|(fi, (_, _, lines))| lines.iter().enumerate()
|
|
.map(move |(li, l)| (l.get(..23).filter(|t| line_time_ok(t)).unwrap_or(""), fi, li)))
|
|
.collect();
|
|
order.sort();
|
|
let gone: std::collections::HashSet<(usize, usize)> = order.iter().take(dropped).map(|&(_, f, l)| (f, l)).collect();
|
|
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
|
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
|
for (fi, (_, name, lines)) in files.iter().enumerate() {
|
|
let kept: Vec<&String> = lines.iter().enumerate().filter(|(li, _)| !gone.contains(&(fi, *li))).map(|(_, l)| l).collect();
|
|
if kept.is_empty() {
|
|
continue;
|
|
}
|
|
zip.start_file(name.as_str(), opts).map_err(std::io::Error::other)?;
|
|
for l in kept {
|
|
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 line_time_ok(t: &str) -> bool {
|
|
chrono::NaiveDateTime::parse_from_str(t, "%Y-%m-%d %H:%M:%S%.3f").is_ok()
|
|
}
|
|
|
|
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();
|
|
// A word may carry sentence dots around it ("token=<token>."): they are not part of the token.
|
|
let flush = |word: &mut String, result: &mut String| {
|
|
let core = word.trim_matches('.');
|
|
if looks_like_token(core) {
|
|
let lead = word.len() - word.trim_start_matches('.').len();
|
|
let trail = word.len() - word.trim_end_matches('.').len();
|
|
result.push_str(&word[..lead]);
|
|
result.push_str(SECRET_MARK);
|
|
result.push_str(&word[word.len() - trail..]);
|
|
} 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");
|
|
// Sentence dots around it do not hide it.
|
|
assert_eq!(redact(&format!("token={token}. next ...{token}..."), &[]), "token=[비밀값]. next ...[비밀값]...");
|
|
}
|
|
|
|
#[test]
|
|
fn the_zip_leaves_out_the_oldest_lines_by_time_across_tabs() {
|
|
let at_hm = |d: u32, h: u32, m: u32| Local.with_ymd_and_hms(2026, 5, d, h, m, 0).unwrap();
|
|
let dir = temp_dir("logorder");
|
|
let hub = LogHub::new(Some(&dir));
|
|
hub.push_at(at_hm(1, 0, 1), Level::Info, "chat", "first chat line");
|
|
hub.push_at(at_hm(1, 23, 59), Level::Info, "app", "late on the first");
|
|
hub.push_at(at_hm(2, 0, 1), Level::Info, "chat", "early on the second");
|
|
let mut files: Vec<(NaiveDate, String, Vec<String>)> = vec![];
|
|
for e in std::fs::read_dir(&dir).unwrap().flatten() {
|
|
let name = e.file_name().to_string_lossy().into_owned();
|
|
files.push((file_day(&name).unwrap(), name, std::fs::read_to_string(e.path()).unwrap().lines().map(str::to_owned).collect()));
|
|
}
|
|
files.sort();
|
|
// Leave out one line: the 00:01 chat line of the 1st, not a line of the app file.
|
|
let z = build_zip(&files, 1).unwrap();
|
|
let mut z = zip::ZipArchive::new(std::io::Cursor::new(z)).unwrap();
|
|
let mut all = String::new();
|
|
for i in 0..z.len() {
|
|
std::io::Read::read_to_string(&mut z.by_index(i).unwrap(), &mut all).unwrap();
|
|
}
|
|
assert!(!all.contains("first chat line"));
|
|
assert!(all.contains("late on the first") && all.contains("early on the second"));
|
|
// Two: then the 23:59 app line goes and the next day's chat line stays.
|
|
let z = build_zip(&files, 2).unwrap();
|
|
let mut z = zip::ZipArchive::new(std::io::Cursor::new(z)).unwrap();
|
|
let mut all = String::new();
|
|
for i in 0..z.len() {
|
|
std::io::Read::read_to_string(&mut z.by_index(i).unwrap(), &mut all).unwrap();
|
|
}
|
|
assert!(!all.contains("late on the first") && all.contains("early on the second"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_secret_is_masked_when_the_line_is_written_so_removing_or_replacing_it_later_shows_nothing() {
|
|
let dir = temp_dir("logwrite");
|
|
let hub = LogHub::new(Some(&dir));
|
|
hub.set_secrets(vec!["old-api-key-1".into()]);
|
|
hub.push_at(Local::now(), Level::Error, "llm", "server refused old-api-key-1");
|
|
// The key is replaced (or its server removed): the hub no longer knows the old value.
|
|
hub.set_secrets(vec!["new-api-key-2".into()]);
|
|
let shown: String = hub.query(None, Level::Debug, None, None, 100).iter().map(|l| l.text.clone()).collect();
|
|
assert!(shown.contains("server refused [비밀값]") && !shown.contains("old-api-key-1"), "{shown}");
|
|
assert!(!hub.recent_error_report("0.1.0").unwrap().contains("old-api-key-1"));
|
|
// After a restart a new hub reads the files: the value was never written.
|
|
// Today's file (a new hub removes files older than the retention).
|
|
let today = Local::now().date_naive();
|
|
let file = std::fs::read_to_string(dir.join(format!("llm-{today}.log"))).unwrap();
|
|
assert!(!file.contains("old-api-key-1"), "{file}");
|
|
let out = dir.join("after.zip");
|
|
LogHub::new(Some(&dir)).export_zip(today, today, &out).unwrap();
|
|
let mut z = zip::ZipArchive::new(std::fs::File::open(&out).unwrap()).unwrap();
|
|
let mut text = String::new();
|
|
std::io::Read::read_to_string(&mut z.by_name(&format!("llm-{today}.log")).unwrap(), &mut text).unwrap();
|
|
assert!(!text.contains("old-api-key-1"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_key_replaced_while_the_services_run_stays_masked_in_their_late_lines_until_the_next_start() {
|
|
let dir = temp_dir("loginuse");
|
|
let hub = LogHub::new(Some(&dir));
|
|
let today = Local::now().date_naive();
|
|
// The services start with the old key.
|
|
hub.set_secrets(vec!["old-api-key-1".into()]);
|
|
hub.set_secrets_in_use(vec!["old-api-key-1".into()]);
|
|
// While they run the key is replaced (or its server removed): only the new one is stored.
|
|
hub.set_secrets(vec!["new-api-key-2".into()]);
|
|
// A late line of the running worker still carries the old key.
|
|
hub.push_worker("[ llm ] Error: MCP server brave failed: bad key old-api-key-1");
|
|
let shown: String = hub.query(None, Level::Debug, None, None, 100).iter().map(|l| l.text.clone()).collect();
|
|
assert!(shown.contains("bad key [비밀값]") && !shown.contains("old-api-key-1"), "{shown}");
|
|
assert!(!hub.recent_error_report("0.1.0").unwrap().contains("old-api-key-1"));
|
|
let file = std::fs::read_to_string(dir.join(format!("llm-{today}.log"))).unwrap();
|
|
assert!(!file.contains("old-api-key-1"), "{file}");
|
|
let out = dir.join("inuse.zip");
|
|
hub.export_zip(today, today, &out).unwrap();
|
|
let mut z = zip::ZipArchive::new(std::fs::File::open(&out).unwrap()).unwrap();
|
|
let mut text = String::new();
|
|
std::io::Read::read_to_string(&mut z.by_name(&format!("llm-{today}.log")).unwrap(), &mut text).unwrap();
|
|
assert!(!text.contains("old-api-key-1"));
|
|
// The next start gives the services only the new key: the old one is no longer held.
|
|
hub.set_secrets_in_use(vec!["new-api-key-2".into()]);
|
|
hub.push(Level::Info, "app", "after restart old-api-key-1 new-api-key-2");
|
|
let last = hub.query(None, Level::Debug, None, None, 1).pop().unwrap().text;
|
|
assert_eq!(last, "after restart old-api-key-1 [비밀값]");
|
|
}
|
|
|
|
#[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()));
|
|
}
|
|
}
|