Skip to main content

agentmux_srv_core/backend/jobs/
cloud_hub.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Where cloud job events enter this AgentMux (docs/specs/SPEC_JOBS_AND_TEMPLATES_2026_10_24.md ยง4.2).
5//!
6//! MuxBus's subscriber hands each `job_event` frame here. The frame is never offered to an agent: it goes to the run
7//! ledger if its reporter is pinned, its signature holds and an approved cloud job names it, and is counted and
8//! dropped otherwise (`cloud_events`).
9
10use std::sync::{Arc, OnceLock};
11
12use agentmux_common::time::now_ms;
13
14use super::cloud_events::{Applied, CloudJob, CloudJobs, Dropped, SignedJobEvent};
15use crate::backend::job_templates;
16use super::reporters::{Reporters, REPORTERS_FILE};
17use super::RunLedger;
18
19/// No cloud job is known while job templates are not running: every event is dropped as "unknown job".
20struct NoneApproved;
21
22impl CloudJobs for NoneApproved {
23    fn cloud_job(&self, _job_id: &str) -> Option<CloudJob> {
24        None
25    }
26}
27
28struct Hub {
29    ledger: Arc<RunLedger>,
30    reporters: Arc<Reporters>,
31    jobs: Box<dyn CloudJobs + Send + Sync>,
32}
33
34static HUB: OnceLock<Hub> = OnceLock::new();
35
36/// Start taking cloud job events into `ledger`. The reporters are the job templates' own, so approving a cloud
37/// template pins its reporter where events are checked; they are read from the data folder if templates are not
38/// running.
39pub fn install(ledger: Arc<RunLedger>) {
40    let reporters = job_templates::service()
41        .map(|s| s.reporters().clone())
42        .unwrap_or_else(|| Arc::new(Reporters::open(&crate::backend::base::get_mux_data_dir().join(REPORTERS_FILE))));
43    let jobs: Box<dyn CloudJobs + Send + Sync> = match job_templates::service() {
44        Some(templates) => Box::new(templates.clone()),
45        None => Box::new(NoneApproved),
46    };
47    let _ = HUB.set(Hub { ledger, reporters, jobs });
48}
49
50/// The reporters pinned on this computer, once the hub is installed.
51pub fn reporters() -> Option<&'static Reporters> {
52    HUB.get().map(|h| &*h.reporters)
53}
54
55/// One event off MuxBus. `None` before the hub is installed (the event is not kept: the relay holds it).
56pub fn ingest(event: &SignedJobEvent) -> Option<Result<Applied, Dropped>> {
57    let hub = HUB.get()?;
58    Some(hub.ledger.ingest_cloud_event(event, &hub.reporters, hub.jobs.as_ref(), now_ms()))
59}