Skip to main content

agentmux_srv_core/backend/jobs/
cap_alert.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//
4//! The alert for a job's first cap skip of the day (docs/specs/
5//! SPEC_JOBS_AND_TEMPLATES_2026_10_24.md §5.3): one note to the owning
6//! leader and one notification to the operator. No Kanban task: a skip is the
7//! cap doing its work, not a failure. The job isn't paused and its runs
8//! resume the next day.
9
10use super::caps::Refusal;
11use super::RunLedger;
12use crate::backend::reactive::sanitize::quote_in_system_note;
13use crate::backend::storage::cron::CronJob;
14
15/// The notification's group: one per job, apart from its failure incident's.
16pub fn alert_key(job_id: &str) -> String {
17    format!("job:{job_id}:cap")
18}
19
20/// The `[AgentMux]` note to the owning leader. The job's name is quoted: an
21/// agent named it.
22pub fn note_text(job_name: &str, refusal: Refusal) -> String {
23    if refusal == Refusal::Rate {
24        return format!(
25            "[AgentMux] Job \"{}\" skipped events: {}. It isn't paused. The limit is the trigger's max_per_hour, and \
26             a source that sends more than it is stuck or looping.",
27            quote_in_system_note(job_name),
28            refusal.words()
29        );
30    }
31    format!(
32        "[AgentMux] Job \"{}\" skipped a run: {}. It isn't paused and runs again tomorrow (the day ends at the \
33         operator's local midnight). Only the operator raises a cap, in Settings → Jobs.",
34        quote_in_system_note(job_name),
35        refusal.words()
36    )
37}
38
39impl RunLedger {
40    /// Raise the once-a-day alert for `job`, skipped for `refusal`.
41    pub(super) fn alert_cap(&self, job: &CronJob, refusal: Refusal) {
42        #[cfg(any(test, feature = "test-support"))]
43        self.cap_alerts.lock().unwrap().push((job.id.clone(), refusal.reason().to_string()));
44        #[cfg(not(any(test, feature = "test-support")))]
45        {
46            use super::incidents::owning_leader;
47            if let Some(leader) = owning_leader(self.mstore.as_ref(), job) {
48                let note = note_text(&job.name, refusal);
49                if let Err(e) = crate::backend::reactive::get_global_handler().deliver_system_note(&leader.uid, &note) {
50                    tracing::info!(job_id = %job.id, error = %e, "jobs: the owning leader didn't get the cap note");
51                }
52            }
53            match crate::backend::notify::router::get(&self.broker) {
54                Some(router) => {
55                    let title = format!("{} ({})", job.name, refusal.label());
56                    router.job_failed(&alert_key(&job.id), &title, format!("Skipped a run: {}.", refusal.words()));
57                }
58                None => tracing::info!(job_id = %job.id, "jobs: no notification router yet; the cap alert is not shown"),
59            }
60        }
61    }
62}