//! An agentic-fleet efficiency demo: five coding tasks, each sealed as an //! `efficiency/1` receipt, then verified and aggregated — surfacing the waste a //! "cost per token" number is designed to hide. //! //! cargo run -p jcp-efficiency --example session //! //! The fleet is illustrative: no task runs here, so its token counts, wall //! times and energy figures are chosen, not read. Each energy figure is //! sealed as what it is, an estimate (`Estimator`, no measured figure) — //! never a hardware reading, which would claim a meter read work that never //! happened. A binding that meters real work seals its meter's figure at the //! meter's grade. use ed25519_dalek::SigningKey; use jcp_efficiency::{aggregate, verify_chain, Efficiency, EfficiencyLog}; use jcp_schema::Energy; /// One illustrative task's `efficiency/1` record: its energy figure is an /// estimate, since no task runs here. pub fn task( id: &str, completed: bool, tokens_out: u64, uj: u64, wall_ns: u64, value_uusd: Option, ) -> Efficiency { Efficiency { task: id.into(), completed, verdict: if completed { "tests-pass".into() } else { "gate-failed".into() }, tokens_in: tokens_out * 8, // agentic loops: accumulated context dwarfs output tokens_out, energy: Energy::estimator(uj), // illustrative: nothing ran, nothing was read wall_ns, value_uusd, energy_attestation: None, } } fn main() { let sk = SigningKey::from_bytes(&[0xed; 32]); let mut log = EfficiencyLog::new(sk, "did:jcp:efficiency-demo"); // A fleet running five coding tasks. Two burn tokens and never reach green. let tasks = [ task("fix-auth-bug", true, 1_200, 3_500_000, 42_000_000_000, Some(5_000_000)), task("add-rate-limit", true, 900, 2_800_000, 31_000_000_000, Some(4_000_000)), task("refactor-parser", false, 4_500, 9_800_000, 120_000_000_000, None), // gave up task("upgrade-deps", true, 600, 1_900_000, 18_000_000_000, Some(2_000_000)), task("migrate-schema", false, 3_800, 8_200_000, 95_000_000_000, None), // wrong → reverted ]; println!("sealing {} efficiency/1 receipts (illustrative tasks: energy sealed as estimates)…\n", tasks.len()); for t in &tasks { log.append(t); let mark = if t.completed { "✓" } else { "✗ spend bought nothing" }; println!( " {:16} {:>5} tok {:>7} µJ/tok {:>6.0} tok/s {}", t.task, t.tokens_out, dash(t.uj_per_accepted_token()), t.tokens_per_sec(), mark ); } // Verify the whole chain, then aggregate — the honest efficiency ledger. let recs = verify_chain(log.receipts(), log.steward(), &log.steward_pubkey()) .expect("chain verifies under the steward key"); let t = aggregate(&recs); println!("\n── the honest efficiency ledger ({} receipts, all verified) ──", recs.len()); println!(" completed units : {}", t.completed_units); println!(" wasted units : {} (tokens burned, no result)", t.wasted_units); println!( " tokens/completed : {} ← charges EVERY attempt (incl. failures) to results delivered", dash(t.tokens_per_completed_unit()) ); println!(" µJ/completed : {}", dash(t.uj_per_completed_unit())); println!(" value / joule : {:.4} µUSD/J", t.value_per_joule()); println!( " WASTE FRACTION : {} ← the number a 'cost per token' rate hides", t.waste_fraction() .map_or_else(|| "— (no tokens spent)".to_string(), |f| format!("{:.0}%", f * 100.0)) ); } /// Render a figure that may be undefined. An undefined cost-per-result ratio is /// shown as "—", never as 0: zero is the optimum of these figures, so printing it /// would rank spend that bought nothing above every run that delivered. fn dash(v: Option) -> String { v.map_or_else(|| "—".to_string(), |n| n.to_string()) }