feat(app): an activity log of what Jarvis did (chat actions, schedule deliveries)
Each chat channel action the brain's tool asked for and each schedule delivery is recorded in activity/<day>.jsonl by kind, target, who asked and outcome, never its text, kept 30 days. The worker's chat_action carries the asker. A 한 일 screen lists the newest entries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
143
app/src/activity.rs
Normal file
143
app/src/activity.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
//! 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`.
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
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 a = Self { dir: data.join("activity") };
|
||||
a.prune(Local::now().date_naive());
|
||||
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 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 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user