use crate::agent::jobs::Job;
use crate::agent::summary::{
Account, HourlyHistory, HubStatus, Links, MeshStatus, NetworkCapacity, PriceRange, Prices,
Problem, Requests, Summary, WorkerCounts,
};
use serde::{Deserialize, Serialize};
pub const SUMMARY_V2_VERSION: u32 = 2;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SummaryV2 {
pub summary_version: u32,
pub agent_version: String,
pub as_of: String,
pub env: String,
pub hub: HubStatus,
pub problems: Vec<Problem>,
pub account: Option<Account>,
pub nodes: Vec<Node>,
pub jobs: Jobs,
pub ledger: Option<Ledger>,
pub squads: Option<Vec<serde_json::Value>>,
pub sandbox: Option<serde_json::Value>,
pub policy: Option<serde_json::Value>,
pub prices: Prices,
pub mesh: MeshStatus,
pub links: Links,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub history: Option<HourlyHistory>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub network: Option<NetworkCapacity>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Node {
pub node_pubkey: String,
pub name: String,
pub this_node: bool,
pub freshness: Option<String>,
pub last_seen_at: Option<String>,
pub backend: Option<String>,
pub budget: Option<serde_json::Value>,
pub hub_workers: u32,
pub effective_price_per_hour: Option<PriceRange>,
pub link: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub local: Option<LocalNode>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LocalNode {
pub sharing: bool,
pub broker: String,
pub workers: WorkerCounts,
pub requests: Requests,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Jobs {
pub live: Vec<Job>,
pub done_24h: u32,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Ledger {
pub unit: String,
pub today: LedgerPeriod,
pub month: LedgerPeriod,
pub tz: String,
pub as_of: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LedgerPeriod {
pub earned: f64,
pub spent: Option<f64>,
pub by_source: Option<serde_json::Value>,
}
pub struct Extras {
pub backend: &'static str,
pub jobs: Jobs,
pub ledger: Option<Ledger>,
}
pub fn build(v1: Summary, x: Extras) -> SummaryV2 {
let me = &v1.this_mac;
let local = LocalNode {
sharing: me.sharing,
broker: me.broker.clone(),
workers: me.workers,
requests: me.requests,
};
let mut nodes: Vec<Node> = vec![];
let mut this_seen = false;
for d in v1.devices.iter().flatten() {
let this = d.this_mac;
this_seen |= this;
nodes.push(Node {
node_pubkey: d.node_pubkey.clone(),
name: d.name.clone(),
this_node: this,
freshness: Some(d.freshness.clone()),
last_seen_at: d.last_seen_at.clone(),
backend: this.then(|| x.backend.to_string()),
budget: None,
hub_workers: d.workers,
effective_price_per_hour: d.effective_price_per_hour,
link: d.link.clone(),
local: this.then(|| local.clone()),
});
}
if !this_seen {
nodes.push(Node {
node_pubkey: me.node_pubkey.clone(),
name: me.name.clone(),
this_node: true,
freshness: None,
last_seen_at: None,
backend: Some(x.backend.to_string()),
budget: None,
hub_workers: 0,
effective_price_per_hour: None,
link: None,
local: Some(local),
});
}
nodes.sort_by_key(|n| !n.this_node);
SummaryV2 {
summary_version: SUMMARY_V2_VERSION,
agent_version: v1.agent_version,
as_of: v1.as_of,
env: v1.env,
hub: v1.hub,
problems: v1.problems,
account: v1.account,
nodes,
jobs: x.jobs,
ledger: x.ledger,
squads: None,
sandbox: None,
policy: None,
prices: v1.prices,
mesh: v1.mesh,
links: v1.links,
history: v1.history,
network: v1.network,
}
}
#[cfg(test)]
mod tests {
use super::*;
const V1: &str = include_str!("../../docs/agent-api/summary.v1.json");
const V2: &str = include_str!("../../docs/agent-api/summary.v2.json");
fn fixture() -> SummaryV2 {
let v1: Summary = serde_json::from_str(V1).unwrap();
build(
v1,
Extras {
backend: "container",
jobs: Jobs {
live: vec![Job {
id: "j42".into(),
state: "live".into(),
source: None,
squad: None,
worker: "jeans-mbp-w0".into(),
started_at: "2026-09-13T01:41:50Z".into(),
finished_at: None,
requests: 3,
}],
done_24h: 17,
},
ledger: Some(Ledger {
unit: "credits".into(),
today: LedgerPeriod {
earned: 12.4,
spent: None,
by_source: None,
},
month: LedgerPeriod {
earned: 310.2,
spent: Some(42.0),
by_source: None,
},
tz: "Asia/Tokyo".into(),
as_of: "2026-09-13T01:41:30Z".into(),
}),
},
)
}
#[test]
fn serializes_to_the_v2_fixture() {
let ours = serde_json::to_value(fixture()).unwrap();
let theirs: serde_json::Value = serde_json::from_str(V2).unwrap();
assert_eq!(
ours,
theirs,
"{}",
serde_json::to_string_pretty(&ours).unwrap()
);
let back: SummaryV2 = serde_json::from_str(V2).unwrap();
assert_eq!(back, fixture());
}
#[test]
fn this_node_comes_first_and_is_there_before_the_hub_answers() {
let s = fixture();
assert!(s.nodes[0].this_node && s.nodes[0].local.is_some());
assert_eq!(s.nodes.iter().filter(|n| n.this_node).count(), 1);
assert!(s.nodes[1..]
.iter()
.all(|n| n.local.is_none() && n.backend.is_none()));
let mut v1: Summary = serde_json::from_str(V1).unwrap();
v1.devices = None;
let s = build(
v1,
Extras {
backend: "host",
jobs: Jobs {
live: vec![],
done_24h: 0,
},
ledger: None,
},
);
assert_eq!(s.nodes.len(), 1);
assert_eq!(
(
s.nodes[0].this_node,
s.nodes[0].backend.as_deref(),
s.nodes[0].freshness.as_deref()
),
(true, Some("host"), None)
);
let wire = serde_json::to_value(&s).unwrap();
for k in ["squads", "sandbox", "policy", "ledger"] {
assert!(wire[k].is_null(), "{k} is null, not absent or empty");
assert!(wire.as_object().unwrap().contains_key(k));
}
}
}