Skip to main content

agentmux_srv_core/backend/audit_query/
tasks.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The task an entry happened under: each row of the Audit links to the Kanban task its agent held at that time, and
5//! to that task's PR (SPEC_AUDIT_LIVE_LOG_OF_EVERY_AGENT_ACTION_2026_11_03.md ยง3.4). The Kanban's own history knows
6//! who held a task and when (`Store::work_held_at`); this puts the answer on the rows of a page.
7
8use std::collections::HashMap;
9
10use crate::backend::rpc_types::{AuditEntry, AuditTaskLink};
11use crate::backend::storage::store::Store;
12
13/// Give each entry (and each end of a jekt) the task its agent held when it happened, where `held` knows one.
14pub fn attach(entries: &mut [AuditEntry], held: &dyn Fn(&str, u64) -> Option<AuditTaskLink>) {
15    for e in entries {
16        let at = e.ts_ms;
17        if let Some(agent) = e.agent_id.as_deref() {
18            e.links.task = held(agent, at);
19        }
20        for end in [e.from.as_mut(), e.to.as_mut()].into_iter().flatten() {
21            end.links.task = held(&end.agent_id, at);
22        }
23    }
24}
25
26/// What the Kanban says about `agent` at `at_ms`: the task it held, as a link. A task is looked up once per page.
27pub fn from_store(identity: &Store) -> impl Fn(&str, u64) -> Option<AuditTaskLink> + '_ {
28    let cards: std::cell::RefCell<HashMap<String, Option<AuditTaskLink>>> = Default::default();
29    move |agent, at_ms| {
30        let id = identity.work_held_at(agent, i64::try_from(at_ms).ok()?).ok().flatten()?;
31        cards
32            .borrow_mut()
33            .entry(id.clone())
34            .or_insert_with(|| {
35                let task = identity.work_board_get(&id).ok().flatten()?;
36                let pr_url = Some(task.card.pr_url.trim().to_string()).filter(|u| !u.is_empty());
37                Some(AuditTaskLink { num: task.card.num, task_ref: task.card.task_ref.clone(), pr_url })
38            })
39            .clone()
40    }
41}
42
43#[cfg(test)]
44#[path = "tasks_tests.rs"]
45mod tests;