open-standards/jcp/jcp-rs/crates/jcp-efficiency/examples/session.rs
Transaction Science 980db9acad
Some checks failed
repository checks / lockfiles, tests in CI, workflow hygiene, spec and section references, public text (pull_request) Successful in 1m19s
standards workspaces build + test / which workspaces this change touches (pull_request) Successful in 9s
standards workspaces build + test / jcp-rs (jcp-conformance, jcp/jcp-rs, -p jcp-energy --features nvml, jcp-rs, linux) (pull_request) Successful in 1m35s
standards workspaces build + test / joule-ui-rs (joule-ui/joule-ui-rs, joule-ui-rs, linux) (pull_request) Successful in 1m14s
standards workspaces build + test / joulehook-rs (joulehook/joulehook-rs, joulehook-rs, linux) (pull_request) Failing after 51s
standards workspaces build + test / sandbox-rs (sandbox-conformance, sandbox/sandbox-rs, sandbox-rs, linux) (pull_request) Failing after 55s
standards workspaces build + test / macOS energy backends (pull_request) Successful in 5m36s
standards workspaces build + test / jouledesk-rs (jouledesk/jouledesk-rs, jouledesk-rs, macos) (pull_request) Failing after 22s
standards workspaces build + test / jouleclaw-rs (-nodebug, jouleclaw/jouleclaw-rs, -p jouleclaw-energy --features rapl,nvml, jouleclaw-rs, linux-arm64, -C target-feature=+fp16) (pull_request) Successful in 2m44s
standards workspaces build + test / joule-code-rs (joule-code/joule-code-rs, joule-code-rs, linux-arm64, true) (pull_request) Successful in 1m40s
standards workspaces build + test / mesh-rs (mesh/mesh-rs, -p mesh-engine-gguf -p mesh-openai --features mesh-engine-gguf/gguf,mesh-openai/server, mesh-rs, linux-arm64) (pull_request) Successful in 58s
jcp-efficiency: an undefined ratio is not zero
`uj_per_accepted_token()` is the crate's headline figure, and it read

    self.energy.uj().checked_div(self.tokens_out).unwrap_or(0)

For a cost-per-result metric 0 is the *optimum*, so "the denominator
was zero" (uncomputable) was reported as "this cost nothing per token"
(perfect, and better than any successful run). It is not a convenience
accessor: `wire()` seals it into the signed core, and `verify()` reads
it straight back out rather than recomputing it, so the conflation was
sealed and consumed as the headline.

The case is the crate's own documented one. `completed: false` is
documented as meaning "the tokens/joules below bought nothing" —
exactly when `tokens_out` is 0 and the sealed figure became 0. A unit
that burned 10 J and accepted nothing sealed
`{"completed":0,"tokens_out":0,"energy_uj":10000000,
"uj_per_accepted_token":0}`, signed: indistinguishable to a dashboard
or an auditor from a run that used no energy.

The family already specifies the right shape, and this crate's doc
comment claimed to match it ("matching the family's
`uj_per_accepted_token` convention") while doing the opposite.
`mesh/SPEC.md` §6 says the figure is "omitted when no tokens were
accepted", `mesh/SPEC.md` §8 says it "is undefined when no tokens were
accepted", and `MeshReceipt::uj_per_accepted_token` returns
`Option<u64>` and omits the key from its sealed wire, with tests
pinning both branches. The fix brings jcp-efficiency onto that shape.

Changed, because the zero they returned is each figure's *optimum*:

- `Efficiency::uj_per_accepted_token` -> `Option<u64>`; omitted from
  the sealed core when undefined. `EfficiencyReceipt`'s field becomes
  `Option<u64>` and `verify()` reads it as optional, so an older
  receipt that carries the figure still round-trips.
- `uusd_per_accepted_token` already returned `Option` because "a value
  figure is never invented" — but `wire()` sealed
  `.unwrap_or(0)`, inventing one. It now flattens with `and_then` and
  the key is omitted. `value_uusd` is an input and still seals.
- `Totals::tokens_per_completed_unit` and `uj_per_completed_unit` ->
  `Option<u64>`. The first claims to "charge every attempt against the
  results actually delivered", which it stops doing precisely when
  nothing was delivered: 50 units and 500 J of spend reported 0 cost
  per completed unit.
- `Totals::waste_fraction` -> `Option<f64>`: 0.0 is a perfect
  zero-waste record, which is what an empty ledger used to report.

Deliberately unchanged: `value_per_joule` and `tokens_per_sec` keep
their `0.0`. Higher is better for both, so the fallback is their worst
value, not their optimum — a zero that understates is not this defect.
`a_fallback_toward_the_worst_value_is_left_alone` pins that choice.

`examples/session.rs` renders an undefined figure as "—" rather than a
number, which also makes the distinction visible in the demo it prints.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-10-17 04:49:02 -04:00

94 lines
3.9 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,
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<u64>) -> String {
v.map_or_else(|| "—".to_string(), |n| n.to_string())
}