Files
javis_bot/app/src/activity.rs
javis-bot e4b6327cf9 fix(app): the activity log keeps ids and known words only and prunes on each new day
A chat action's channel, message and asker are recorded only when they are
ids (else '(잘못된 ID)'), an unknown action as chat.other, and a failure as a
fixed note, so text the model put in those fields never reaches the log. Old
day files are also deleted on the first record of each new day.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 16:55:41 +09:00

206 lines
8.3 KiB
Rust

//! What Jarvis did outside its answers (`app/src/admin.spec.md`, "Activity"; `docs/v2/proactive.md`
//! 제안 5): chat channel actions and schedule deliveries, by kind, target, who asked and outcome,
//! never their text. Day files `activity/<YYYY-MM-DD>.jsonl` in the data folder, kept `KEEP_DAYS`
//! (old ones deleted at the start and on the first record of each new day).
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use chrono::{Local, NaiveDate};
use serde::{Deserialize, Serialize};
pub const KEEP_DAYS: i64 = 30;
static FILE_LOCK: Mutex<()> = Mutex::new(());
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Entry {
/// Local time, RFC 3339.
pub at: String,
/// What was done: `chat.send`, `chat.edit`, `chat.react`, `chat.delete`, `chat.pin`,
/// `chat.unpin`, `schedule.remind`, `schedule.task`.
pub kind: String,
/// What it was done to: a channel or message id, or the schedule's id and where it went.
pub target: String,
/// Who asked (Discord user id and name), when known.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub by: Option<String>,
pub ok: bool,
/// Why it failed (never content).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
}
pub struct Activity {
dir: PathBuf,
/// The day old files were last deleted; a record on a later day deletes again.
pruned: Mutex<NaiveDate>,
}
/// Recorded instead of an id that is not one (the model may put any text there).
pub const INVALID_ID: &str = "(잘못된 ID)";
/// Recorded instead of a chat action's error (it may quote what the model wrote).
pub const CHAT_FAILED: &str = "채팅 동작 실패";
const CHAT_ACTIONS: &[&str] = &["send", "edit", "react", "delete", "pin", "unpin"];
/// `s` when it is a Discord id (digits only), else `INVALID_ID`.
pub fn id_or_mark(s: &str) -> String {
if (1..=20).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_digit()) { s.to_owned() } else { INVALID_ID.into() }
}
/// The kind of a chat action: `chat.<action>` for the known ones, else `chat.other`.
pub fn chat_kind(action: &str) -> String {
format!("chat.{}", if CHAT_ACTIONS.contains(&action) { action } else { "other" })
}
/// The target of a chat action, from ids only.
pub fn chat_target(channel: &str, message: Option<&str>) -> String {
match message {
Some(m) => format!("채널 {} 메시지 {}", id_or_mark(channel), id_or_mark(m)),
None => format!("채널 {}", id_or_mark(channel)),
}
}
fn day_name(d: NaiveDate) -> String {
format!("{d}.jsonl")
}
impl Activity {
/// The log in `data`; day files older than `KEEP_DAYS` are deleted.
pub fn new(data: &Path) -> Self {
let today = Local::now().date_naive();
let a = Self { dir: data.join("activity"), pruned: Mutex::new(today) };
a.prune(today);
a
}
fn days(&self) -> Vec<(NaiveDate, PathBuf)> {
let mut out: Vec<(NaiveDate, PathBuf)> = std::fs::read_dir(&self.dir).into_iter().flatten().flatten()
.filter_map(|e| {
let name = e.file_name().to_str()?.to_owned();
let d = NaiveDate::parse_from_str(name.strip_suffix(".jsonl")?, "%Y-%m-%d").ok()?;
Some((d, e.path()))
})
.collect();
out.sort();
out
}
fn prune(&self, today: NaiveDate) {
let _g = FILE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
for (d, f) in self.days() {
if (today - d).num_days() > KEEP_DAYS {
let _ = std::fs::remove_file(f);
}
}
}
/// Append one entry (a failure to write is logged, never raised: the action itself happened).
pub fn record(&self, kind: &str, target: &str, by: Option<String>, ok: bool, note: Option<String>) {
let now = Local::now();
let today = now.date_naive();
let rolled = {
let mut p = self.pruned.lock().unwrap_or_else(|e| e.into_inner());
std::mem::replace(&mut *p, today) != today
};
if rolled {
self.prune(today);
}
let e = Entry { at: now.to_rfc3339_opts(chrono::SecondsFormat::Secs, false), kind: kind.into(),
target: target.into(), by, ok, note };
let _g = FILE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let file = self.dir.join(day_name(now.date_naive()));
let line = serde_json::to_string(&e).unwrap() + "\n";
let wrote = std::fs::create_dir_all(&self.dir).and_then(|_| {
use std::io::Write;
std::fs::OpenOptions::new().create(true).append(true).open(&file)?.write_all(line.as_bytes())
});
match wrote {
Ok(()) => {
crate::debug_log!("conversation", "activity {kind} ok={ok}");
}
Err(err) => {
crate::warn_log!("conversation", "Warning: could not record activity {kind}: {err}");
}
}
}
/// The newest `limit` entries, newest first.
pub fn recent(&self, limit: usize) -> Vec<Entry> {
let _g = FILE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut out = vec![];
for (_, f) in self.days().into_iter().rev() {
let text = std::fs::read_to_string(&f).unwrap_or_default();
for line in text.lines().rev() {
if let Ok(e) = serde_json::from_str::<Entry>(line) {
out.push(e);
if out.len() == limit {
return out;
}
}
}
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entries_come_back_newest_first_up_to_the_limit() {
let data = crate::datadir::temp_dir("activity");
let a = Activity::new(&data);
a.record("chat.send", "123", Some("1 가".into()), true, None);
a.record("schedule.remind", "7 → 음성", None, false, Some("음성 채널에 없음".into()));
let got = a.recent(10);
assert_eq!(got.iter().map(|e| e.kind.as_str()).collect::<Vec<_>>(), ["schedule.remind", "chat.send"]);
assert_eq!(got[1].by.as_deref(), Some("1 가"));
assert!(!got[0].ok && got[0].note.is_some());
assert_eq!(a.recent(1).len(), 1);
}
#[test]
fn a_chat_action_is_recorded_by_ids_and_known_words_only() {
// What the model put in the fields never reaches the log, valid or not.
assert_eq!(chat_kind("edit"), "chat.edit");
assert_eq!(chat_kind("비밀 회의"), "chat.other");
assert_eq!(chat_target("223456789012345678", Some("323456789012345678")),
"채널 223456789012345678 메시지 323456789012345678");
let bad = chat_target("일정 채널", Some("비밀 회의"));
assert!(!bad.contains("비밀") && !bad.contains("일정"), "{bad}");
assert_eq!(chat_target("223456789012345678", None), "채널 223456789012345678");
assert_eq!(id_or_mark("12a"), INVALID_ID);
}
#[test]
fn a_long_run_still_deletes_old_day_files_when_the_day_changes() {
let data = crate::datadir::temp_dir("activity-rollover");
let a = Activity::new(&data);
let old = Local::now().date_naive() - chrono::Duration::days(KEEP_DAYS + 1);
std::fs::create_dir_all(data.join("activity")).unwrap();
std::fs::write(data.join("activity").join(day_name(old)), "{}\n").unwrap();
// The app has been running since yesterday.
*a.pruned.lock().unwrap() = Local::now().date_naive() - chrono::Duration::days(1);
a.record("chat.send", "채널 1", None, true, None);
assert!(!data.join("activity").join(day_name(old)).exists());
}
#[test]
fn day_files_past_the_keep_days_are_deleted() {
let data = crate::datadir::temp_dir("activity-prune");
let dir = data.join("activity");
std::fs::create_dir_all(&dir).unwrap();
let today = Local::now().date_naive();
let old = today - chrono::Duration::days(KEEP_DAYS + 1);
let kept = today - chrono::Duration::days(KEEP_DAYS);
let line = r#"{"at":"x","kind":"chat.send","target":"1","ok":true}"#;
std::fs::write(dir.join(day_name(old)), line).unwrap();
std::fs::write(dir.join(day_name(kept)), line).unwrap();
let a = Activity::new(&data);
assert!(!dir.join(day_name(old)).exists());
assert_eq!(a.recent(10).len(), 1);
}
}