Skip to main content

agentmux_srv_core/backend/
audit_activity.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The Audit's index of what agents do (docs/specs/SPEC_AUDIT_LIVE_LOG_OF_EVERY_AGENT_ACTION_2026_11_03.md ยง3.2).
5//!
6//! Every output line of every agent is appended in one place (`transcript::append`), and its tool calls, thinking
7//! blocks and turn ends are already in it. This module reads those lines as they pass and writes one small row for
8//! each to `activity-YYYY-MM-DD.jsonl`, one file a day, so a day can be dropped by date. The row holds the agent,
9//! the kind, a scrubbed one-line summary and where the line landed in the agent's history; the transcript keeps the
10//! full call. Nothing is recorded twice, only indexed.
11//!
12//! What is kept: the agent, the kind, a summary that never copies a raw call (see [`scrub`]), times, outcome, cost
13//! and byte positions. Never: a Vault value, a secret, a thought's text, a tool's result.
14//!
15//! The rows go to the same place the jekt audit file does ([`reactive::audit_store::write_path`]'s folder): the
16//! shared `audit` folder, or the srv's own while it does not use the shared one.
17
18pub mod line;
19pub mod scrub;
20pub mod store;
21
22use serde::{Deserialize, Serialize};
23
24pub use store::{day_files, install_for_this_srv, record, DayFile};
25
26use crate::backend::rpc_types::AuditCost;
27
28/// How long a day's file is kept (days). The transcript still holds the full call, so what goes is only the index.
29pub const KEEP_DAYS: u64 = 30;
30
31/// One indexed action. `agent` is the agent's UID (the id in its transcript zone), turned into a name when listed.
32#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33pub struct ActivityRow {
34    /// Unix ms the line was written.
35    pub ts: u64,
36    /// `tool`, `turn` or `thought`.
37    pub kind: String,
38    pub agent: String,
39    pub block: String,
40    /// The transcript zone the line is in, and where in it (`at_byte` of the History link).
41    pub zone: String,
42    #[serde(default, skip_serializing_if = "Option::is_none")]
43    pub byte: Option<u64>,
44    /// One line, scrubbed.
45    pub summary: String,
46    #[serde(default, skip_serializing_if = "Option::is_none")]
47    pub tool: Option<String>,
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    pub outcome: Option<String>,
50    /// A turn's length.
51    #[serde(default, skip_serializing_if = "Option::is_none")]
52    pub ms: Option<u64>,
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub cost: Option<AuditCost>,
55    /// The channel that wrote the row; set by the writer.
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub channel: Option<String>,
58}
59
60/// Indexes the transcript lines `records` (newline-terminated, as written) that landed in `zone` from byte `at`.
61/// Called for every transcript write; a line that is not a tool call, a thinking block or a turn end costs a
62/// substring test.
63pub fn note_records(block_id: &str, zone: &str, records: &[u8], at: Option<u64>) {
64    let now = agentmux_common::time::now_ms_u64();
65    for row in rows_of(block_id, zone, records, at, now) {
66        record(row);
67    }
68}
69
70/// The rows in `records`, which were written to `zone` from byte `at` at time `now`.
71fn rows_of(block_id: &str, zone: &str, records: &[u8], at: Option<u64>, now: u64) -> Vec<ActivityRow> {
72    let Some(agent) = agent_of_zone(zone) else { return Vec::new() };
73    let mut rows = Vec::new();
74    let mut offset = at;
75    for raw in records.split_inclusive(|b| *b == b'\n') {
76        let start = offset;
77        offset = offset.map(|o| o + raw.len() as u64);
78        let Ok(text) = std::str::from_utf8(raw) else { continue };
79        for parsed in line::parse(text.trim_end()) {
80            rows.push(ActivityRow {
81                ts: now,
82                kind: parsed.kind.to_string(),
83                agent: agent.to_string(),
84                block: block_id.to_string(),
85                zone: zone.to_string(),
86                byte: start,
87                summary: parsed.summary,
88                tool: parsed.tool,
89                outcome: parsed.outcome,
90                ms: parsed.ms,
91                cost: parsed.cost,
92                channel: None,
93            });
94        }
95    }
96    rows
97}
98
99/// The agent a transcript zone belongs to: `agent:<id>:current`, for a defined agent. A quick-launch provider's zone
100/// is shared by every pane of that provider, so no one agent made what is in it.
101pub(crate) fn agent_of_zone(zone: &str) -> Option<&str> {
102    let id = zone.strip_prefix("agent:")?.strip_suffix(":current")?;
103    (crate::backend::providers::get_provider(id).is_none()).then_some(id)
104}
105
106#[cfg(test)]
107mod tests;