Skip to main content

agent_hub_runtime/core/
audit_log.rs

1//! Append-only audit log of user/system actions.
2//!
3//! Stored in the existing SQLite database (table `audit_log`). Writes are
4//! best-effort: failures are swallowed so they never block the user action
5//! they accompany. Reads return newest-first.
6//!
7//! The log is auto-pruned to `MAX_ENTRIES` rows on each write so the table
8//! cannot grow unbounded.
9
10use serde::Serialize;
11
12/// Hard cap on log size. Older rows are dropped on insert.
13pub const MAX_ENTRIES: i64 = 10_000;
14
15/// One audit log entry as exposed to the frontend / exports.
16#[derive(Debug, Clone, Serialize)]
17pub struct AuditEntry {
18    pub id: i64,
19    /// Unix timestamp in seconds.
20    pub ts: i64,
21    /// Short action verb, e.g. "install", "remove", "enable", "sync".
22    pub action: String,
23    pub skill_id: Option<String>,
24    pub skill_name: Option<String>,
25    /// Affected tool/agent key when the action targets one, e.g. "claude_code".
26    pub tool: Option<String>,
27    pub success: bool,
28    /// Free-form detail. Error message on failure, optional context otherwise.
29    pub detail: Option<String>,
30}
31
32/// Payload used when recording a new entry.
33#[derive(Debug, Default, Clone)]
34pub struct AuditDraft {
35    pub action: String,
36    pub skill_id: Option<String>,
37    pub skill_name: Option<String>,
38    pub tool: Option<String>,
39    pub success: bool,
40    pub detail: Option<String>,
41}
42
43impl AuditDraft {
44    pub fn new(action: impl Into<String>) -> Self {
45        Self {
46            action: action.into(),
47            ..Default::default()
48        }
49    }
50
51    pub fn ok(mut self) -> Self {
52        self.success = true;
53        self
54    }
55
56    pub fn fail(mut self, error: impl Into<String>) -> Self {
57        self.success = false;
58        self.detail = Some(error.into());
59        self
60    }
61
62    pub fn skill(mut self, id: impl Into<String>, name: impl Into<String>) -> Self {
63        self.skill_id = Some(id.into());
64        self.skill_name = Some(name.into());
65        self
66    }
67
68    pub fn tool(mut self, tool: impl Into<String>) -> Self {
69        self.tool = Some(tool.into());
70        self
71    }
72
73    pub fn detail(mut self, detail: impl Into<String>) -> Self {
74        self.detail = Some(detail.into());
75        self
76    }
77}