jcp-efficiency: an undefined ratio is not zero #291

Merged
dcharlot merged 2 commits from fix/jcp-efficiency-undefined-ratio-is-not-zero into main 2026-10-17 16:02:53 -04:00
Owner

The defect

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 figure.

The triggering 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, signed:

{"completed":0,"tokens_out":0,"energy_uj":10000000,"uj_per_accepted_token":0}

Indistinguishable to a dashboard or an auditor from a run that used no energy at all.

The family already specifies the right shape

The doc comment claimed to match it — "matching the family's uj_per_accepted_token convention" — while doing the opposite:

  • mesh/SPEC.md §6: the figure is "omitted when no tokens were accepted".
  • mesh/SPEC.md §8: it "is undefined when no tokens were accepted".
  • MeshReceipt::uj_per_accepted_token returns Option<u64>, omits the key from its sealed wire, and has tests pinning both branches.

So this is a consistency fix onto a published family convention, not a new design. The eval/1 twin of it landed as #292.

What changed, and the criterion

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

figure was now
Efficiency::uj_per_accepted_token 0 Option<u64>, omitted from the sealed core when undefined
EfficiencyReceipt::uj_per_accepted_token u64 Option<u64>; verify() reads it as optional, so an older receipt carrying the figure still round-trips
uusd_per_accepted_token already Option, but wire() sealed .unwrap_or(0) — inventing the value figure its own doc says is "never invented" flattens with and_then; key omitted. value_uusd is an input and still seals
Totals::tokens_per_completed_unit 0 Option<u64>
Totals::uj_per_completed_unit 0 Option<u64>
Totals::waste_fraction 0.0 Option<f64>

tokens_per_completed_unit 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. waste_fraction's 0.0 is a perfect zero-waste record, which is what an empty ledger reported.

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 so it reads as a decision rather than an omission.

Tests

  • spend_that_bought_nothing_seals_no_headline_figure — 10 J, nothing accepted: the figure is None, the key is absent from the signed core, the envelope still verifies, and the 10 J is still on it.
  • a_value_figure_is_not_invented_without_accepted_tokens — value_uusd seals; the ratio derived from it does not.
  • a_ledger_with_nothing_completed_reports_no_cost_per_unit — through the real seal → verify → aggregate path; 30 J of spend, no cost-per-unit figure, and Totals::default().waste_fraction() is None.
  • a_fallback_toward_the_worst_value_is_left_alone — the kept zeros.

Falsification

mutation result
E1 restore Some(0) on the headline figure the sealed-figure and ledger tests FAILED (2)
E2 restore the invented value figure at seal time only the value test FAILED
E3 restore 0 on the aggregate cost-per-unit ratio only the ledger test FAILED

In all three, the_four_metrics_compute_from_the_inputs and seal_verify_round_trips_and_tamper_fails kept passing — the tests are specific to the undefined case.

Second commit: the cross-workspace consumers (CI caught this)

The first commit verified only jcp/jcp-rs. jcp-efficiency is a path dependency of three other workspaces, and cargo test --workspace inside jcp/jcp-rs never compiles them — so the jouledesk-rs, sandbox-rs and joulehook-rs legs all failed to build. Consumers:

jouledesk/.../jouledesk/Cargo.toml
jouledesk/.../jouledesk-livekit/Cargo.toml
jouledesk/.../jouledesk-github/Cargo.toml
sandbox/.../sandbox-tee/Cargo.toml
joulehook/.../joulehook-meter/Cargo.toml

Five call sites read a figure that is now an Option:

  • jouledesk/src/lib.rs — the assertion that waste inflates joules-per-completed-unit. Two units completed there, so the ratio is defined; .expect records that rather than hiding it.
  • jouledesk/examples/channel.rs, examples/room.rs, sandbox-tee/examples/attested_efficiency.rs — each prints — when undefined, for the same reason session.rs does.
  • joulehook-meter — assert_eq! on a receipt field; 900 tokens were accepted, so the figure is present and the assertion takes Some.

Efficiency's own fields did not change, so the Efficiency { .. } literals in all four consumers are untouched; only reads of EfficiencyReceipt::uj_per_accepted_token and Totals::uj_per_completed_unit needed work.

Verification

Each of the four workspaces that compile this crate, not just the one holding the edit:

workspace cargo check --workspace --all-targets cargo test --workspace
jcp/jcp-rs 0 0 — 0 failed, 64 ok suites
jouledesk/jouledesk-rs 0 0 — 0 failed, 9 ok suites
sandbox/sandbox-rs 0 0 — 0 failed, 48 ok suites
joulehook/joulehook-rs 0 0 — 0 failed, 8 ok suites

Scope

No conformance vector pins an efficiency/1 receipt (jcp/conformance/v1/ has none; the uj_per_accepted_token in mesh/conformance/ belongs to mesh-receipt, a different crate, which already omits it correctly). There is no jcp-side SPEC section listing efficiency/1 fields.

🤖 Generated with Claude Code

## The defect `uj_per_accepted_token()` is the crate's headline figure, and it read: ```rust 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 figure. The triggering 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, signed: ```json {"completed":0,"tokens_out":0,"energy_uj":10000000,"uj_per_accepted_token":0} ``` Indistinguishable to a dashboard or an auditor from a run that used no energy at all. ## The family already specifies the right shape The doc comment claimed to match it — *"matching the family's `uj_per_accepted_token` convention"* — while doing the opposite: - `mesh/SPEC.md` §6: the figure is **"omitted when no tokens were accepted"**. - `mesh/SPEC.md` §8: it **"is undefined when no tokens were accepted"**. - `MeshReceipt::uj_per_accepted_token` returns `Option<u64>`, omits the key from its sealed wire, and has tests pinning **both** branches. So this is a consistency fix onto a published family convention, not a new design. The `eval/1` twin of it landed as #292. ## What changed, and the criterion Changed — because the zero they returned is each figure's **optimum**: | figure | was | now | |---|---|---| | `Efficiency::uj_per_accepted_token` | `0` | `Option<u64>`, **omitted** from the sealed core when undefined | | `EfficiencyReceipt::uj_per_accepted_token` | `u64` | `Option<u64>`; `verify()` reads it as optional, so an older receipt carrying the figure still round-trips | | `uusd_per_accepted_token` | already `Option`, but `wire()` sealed `.unwrap_or(0)` — **inventing** the value figure its own doc says is "never invented" | flattens with `and_then`; key omitted. `value_uusd` is an *input* and still seals | | `Totals::tokens_per_completed_unit` | `0` | `Option<u64>` | | `Totals::uj_per_completed_unit` | `0` | `Option<u64>` | | `Totals::waste_fraction` | `0.0` | `Option<f64>` | `tokens_per_completed_unit` 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. `waste_fraction`'s `0.0` is a perfect zero-waste record, which is what an empty ledger reported. **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 so it reads as a decision rather than an omission. ## Tests - `spend_that_bought_nothing_seals_no_headline_figure` — 10 J, nothing accepted: the figure is `None`, the key is **absent from the signed core**, the envelope still verifies, and the 10 J is still on it. - `a_value_figure_is_not_invented_without_accepted_tokens` — `value_uusd` seals; the ratio derived from it does not. - `a_ledger_with_nothing_completed_reports_no_cost_per_unit` — through the real seal → verify → `aggregate` path; 30 J of spend, no cost-per-unit figure, and `Totals::default().waste_fraction()` is `None`. - `a_fallback_toward_the_worst_value_is_left_alone` — the kept zeros. ### Falsification | mutation | result | |---|---| | **E1** restore `Some(0)` on the headline figure | the sealed-figure and ledger tests FAILED (2) | | **E2** restore the invented value figure at seal time | **only** the value test FAILED | | **E3** restore `0` on the aggregate cost-per-unit ratio | **only** the ledger test FAILED | In all three, `the_four_metrics_compute_from_the_inputs` and `seal_verify_round_trips_and_tamper_fails` kept passing — the tests are specific to the undefined case. ## Second commit: the cross-workspace consumers (CI caught this) The first commit verified only `jcp/jcp-rs`. **`jcp-efficiency` is a path dependency of three other workspaces**, and `cargo test --workspace` inside `jcp/jcp-rs` never compiles them — so the `jouledesk-rs`, `sandbox-rs` and `joulehook-rs` legs all failed to build. Consumers: ``` jouledesk/.../jouledesk/Cargo.toml jouledesk/.../jouledesk-livekit/Cargo.toml jouledesk/.../jouledesk-github/Cargo.toml sandbox/.../sandbox-tee/Cargo.toml joulehook/.../joulehook-meter/Cargo.toml ``` Five call sites read a figure that is now an `Option`: - `jouledesk/src/lib.rs` — the assertion that waste inflates joules-per-completed-unit. Two units completed there, so the ratio is defined; `.expect` records that rather than hiding it. - `jouledesk/examples/channel.rs`, `examples/room.rs`, `sandbox-tee/examples/attested_efficiency.rs` — each prints `—` when undefined, for the same reason `session.rs` does. - `joulehook-meter` — `assert_eq!` on a receipt field; 900 tokens were accepted, so the figure is present and the assertion takes `Some`. `Efficiency`'s own fields did not change, so the `Efficiency { .. }` literals in all four consumers are untouched; only reads of `EfficiencyReceipt::uj_per_accepted_token` and `Totals::uj_per_completed_unit` needed work. ## Verification Each of the **four** workspaces that compile this crate, not just the one holding the edit: | workspace | `cargo check --workspace --all-targets` | `cargo test --workspace` | |---|---|---| | `jcp/jcp-rs` | 0 | 0 — 0 failed, 64 ok suites | | `jouledesk/jouledesk-rs` | 0 | 0 — 0 failed, 9 ok suites | | `sandbox/sandbox-rs` | 0 | 0 — 0 failed, 48 ok suites | | `joulehook/joulehook-rs` | 0 | 0 — 0 failed, 8 ok suites | ## Scope No conformance vector pins an `efficiency/1` receipt (`jcp/conformance/v1/` has none; the `uj_per_accepted_token` in `mesh/conformance/` belongs to `mesh-receipt`, a different crate, which already omits it correctly). There is no jcp-side SPEC section listing `efficiency/1` fields. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jcp-efficiency: an undefined ratio is not zero
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
980db9acad
`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]>
jcp-efficiency: carry the Option through its cross-workspace consumers
All checks were successful
standards workspaces build + test / which workspaces this change touches (pull_request) Successful in 29s
repository checks / lockfiles, tests in CI, workflow hygiene, spec and section references, public text (pull_request) Successful in 3m3s
standards workspaces build + test / jcp-rs (jcp-conformance, jcp/jcp-rs, -p jcp-energy --features nvml, jcp-rs, linux) (pull_request) Successful in 2m46s
standards workspaces build + test / joule-ui-rs (joule-ui/joule-ui-rs, joule-ui-rs, linux) (pull_request) Successful in 1m17s
standards workspaces build + test / joulehook-rs (joulehook/joulehook-rs, joulehook-rs, linux) (pull_request) Successful in 1m35s
standards workspaces build + test / proof-rs (proof-conformance, proof/proof-rs, proof-rs, linux, true) (pull_request) Successful in 1m39s
standards workspaces build + test / sandbox-rs (sandbox-conformance, sandbox/sandbox-rs, sandbox-rs, linux) (pull_request) Successful in 2m51s
standards workspaces build + test / macOS energy backends (pull_request) Successful in 9m1s
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 6m29s
standards workspaces build + test / joule-code-rs (joule-code/joule-code-rs, joule-code-rs, linux-arm64, true) (pull_request) Successful in 2m39s
standards workspaces build + test / joulecontract-rs (joulecontract/joulecontract-rs, joulecontract-rs, linux-arm64, true) (pull_request) Successful in 1m33s
standards workspaces build + test / jouledesk-rs (jouledesk/jouledesk-rs, jouledesk-rs, macos) (pull_request) Successful in 2m3s
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 1m26s
94597bc668
CI caught what the previous commit's verification could not: `jcp-efficiency`
is a **path dependency of three other workspaces**, and
`cargo test --workspace` inside `jcp/jcp-rs` never compiles them. The
jouledesk-rs, sandbox-rs and joulehook-rs legs all failed to build.

The consumers are:

    jouledesk/.../jouledesk/Cargo.toml
    jouledesk/.../jouledesk-livekit/Cargo.toml
    jouledesk/.../jouledesk-github/Cargo.toml
    sandbox/.../sandbox-tee/Cargo.toml
    joulehook/.../joulehook-meter/Cargo.toml

Five call sites read a figure that is now an `Option`:

- `jouledesk/src/lib.rs` — the assertion that waste inflates
  joules-per-completed-unit. Two units completed there, so the ratio is
  defined; `.expect` now records that rather than hiding it.
- `jouledesk/examples/channel.rs`, `examples/room.rs` and
  `sandbox-tee/examples/attested_efficiency.rs` — `println!` of the figure.
  Each prints "—" when undefined, for the same reason `session.rs` does:
  0 is the optimum of a cost-per-result figure, so printing it would name
  the cheapest possible cost for work that delivered none.
- `joulehook-meter` — `assert_eq!` on a receipt field; 900 tokens were
  accepted, so the figure is present and the assertion takes `Some`.

`Efficiency`'s own fields did not change, so the `Efficiency { .. }`
literals in all four consumers are untouched; only reads of
`EfficiencyReceipt::uj_per_accepted_token` and `Totals::uj_per_completed_unit`
needed work.

Verified by building and testing **each** of the four workspaces that
compile this crate — jcp-rs, jouledesk-rs, sandbox-rs, joulehook-rs — rather
than only the one holding the edit: `cargo check --workspace --all-targets`
and `cargo test --workspace` exit 0 in all four, with 0 failed suites.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Transaction-Science/open-standards!291
No description provided.