open-standards/jcp/jcp-rs/crates/jcp-efficiency/examples/session.rs
Transaction Science b0f174bb8c jcp-efficiency: the session example's illustrative figures are estimates
The `session` example seals five efficiency/1 receipts for an
illustrative fleet — no task runs — and built each energy figure with
Energy::measured(uj), commented "a HwShunt reading in this demo". Every
receipt claimed a hardware reading nothing took.

`task()` now seals Energy::estimator(uj), the module docs say the fleet is
illustrative and that a binding metering real work seals its meter's
grade, and the run prints that the energy is sealed as estimates. The
figures, and so every rate the example prints, are unchanged.

tests/session_example.rs compiles the example in as a module, builds its
records and seals them: each record must be Estimator with no measured
figure, and each verified receipt's provenance "Estimator".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-10 17:47:11 -04:00

86 lines
3.5 KiB
Rust

//! 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<u64>,
) -> 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,
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",
t.tokens_per_completed_unit()
);
println!(" µJ/completed : {}", t.uj_per_completed_unit());
println!(" value / joule : {:.4} µUSD/J", t.value_per_joule());
println!(
" WASTE FRACTION : {:.0}% ← the number a 'cost per token' rate hides",
t.waste_fraction() * 100.0
);
}