open-standards/joule-code/examples/l3-jouleclaw
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Transaction Science b6ad1b0763
All checks were successful
jouleclaw fabrics / cuda (linux, RTX 4050 + Vulkan loader) (pull_request) Successful in 20s
jouleclaw fabrics / vulkan (linux, RTX 4050 + Vulkan loader) (pull_request) Successful in 20s
jouleclaw public surface must stay documented / Public-surface audit (jouleclaw-rs) (pull_request) Successful in 1m7s
joule-code conformance gate / Build + round-trip C1-C4 vectors (pull_request) Successful in 2m40s
repository checks / lockfiles, tests in CI, workflow hygiene, spec and section references, public text (pull_request) Successful in 1m39s
standards workspaces build + test / which workspaces this change touches (pull_request) Successful in 7s
joule-code conformance gate / joule-moq-ingest, every target (pull_request) Successful in 2m12s
joule-code conformance gate / joule-moq-relay, every target (pull_request) Successful in 2m32s
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 3m27s
standards workspaces build + test / joule-code-rs (joule-code/joule-code-rs, joule-code-rs, linux-arm64, true) (pull_request) Successful in 4m9s
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 1m4s
jouleclaw fabrics / metal (macOS, Apple GPU) (pull_request) Successful in 58s
standards workspaces build + test / macOS energy backends (pull_request) Successful in 10m50s
standards workspaces build + test / jouledesk-rs (jouledesk/jouledesk-rs, jouledesk-rs, macos) (pull_request) Successful in 3m12s
jouleclaw: a routing plan carries the class of its energy figure
#390 said the enforceable fix for `router_joules` was to give `RoutingPlan` a
provenance, so a declared constant stops sharing a type with a measurement.
This is that change.

`RoutingPlan` gains `router_energy_class: Provenance`. Every router in this
workspace sets `Estimator`, because every one of them charges a constant or a
sum of constants — **nothing in routing is measured today**, and the type now
says so rather than leaving a reader to find out. It matters because the figure
is debited: `tier.rs` does `spent += plan.router_joules`, and a consumer that
cannot tell a measurement from a constant cannot tell whether the budget it is
enforcing means anything.

Three alternatives were rejected on the merits, not on taste:

- `JouleClass` is a tier taxonomy (Cache/Lawful/Embed/Model/Wire), not a grade.
- Moving `Provenance` into `jouleclaw-core` would be the tidier home, but it has
  **61 importers**; churning them to serve two crates is not proportionate.
- A local grade enum in cascade would make a third spelling of a vocabulary the
  family already has.

So cascade and routing depend on `jouleclaw-energy` with
`default-features = false`. Every hardware backend there is already opt-in, so
this adds no meter to the cascade's dependency graph — just the type.

**The magnitude is unchanged, deliberately.** `ROUTING_JOULES = 8e-9` J is
0.008 µJ: below the integer-microjoule unit the protocol uses (`EnergyReading`
is `u64` µJ, and "floating-point is forbidden in the protocol for
determinism"), so it rounds to zero there, and six orders of magnitude below
the ~1 mJ floor `jouleclaw-energy` itself calls realistic for an `Estimator`
source. The significand is unjustified. Correcting it needs a measurement;
substituting a different invented number would repeat the error this change
exists to label.

Falsification found two real gaps in my own tests before this was right:

- Mutating `LearnedRouter`'s and `PhasorRouter`'s own `RoutingPlan` literals
  changed **nothing**, because the test exercised `RoutingPlan::fallback` for
  both — their constructions were never reached. Fixed by driving each past
  cold start with `record_outcome` and asserting `!plan.is_fallback()` *first*,
  so the test cannot silently slide back onto the path that pins nothing.
- Mutating `PrivacyRouter`'s composition to take the inner plan's class was
  **invisible**, because with every shipped router on `Estimator` the two
  expressions agree. Separating them needed a stub inner router claiming
  `HwShunt`, which pins the actual rule: a sum containing an unmeasured
  constant is an estimate, however good the measured term was.

All four mutations now fail exactly one named test each.

Lockfiles: both workspaces that name these crates are refreshed
(`jouleclaw/jouleclaw-rs`, `joule-code/examples/l3-jouleclaw`); 47 lockfiles
current.

Verified: workspace check 0 errors, 486 test binaries green, 0 failures;
`l3-jouleclaw` check 0 errors. The 10 clippy findings in these crates are
pre-existing, unchanged in count, and all in lines this change did not author
(`coord_router_impl.rs:241` shifted to `:242` behind an inserted line); neither
crate is in CI's clippy list.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-10-21 19:44:14 -04:00
..
src l3-jouleclaw: grade a fallback span as no reading; never read the llama engine whole 2026-10-10 22:24:01 -04:00
tests jouleclaw-fabric: seed the CPU and AMX matmul figures Estimated, not Benchmarked 2026-10-11 10:11:54 -04:00
Cargo.lock jouleclaw: a routing plan carries the class of its energy figure 2026-10-21 19:44:14 -04:00
Cargo.toml l3-jouleclaw: meter each engine on its own process, graded by coverage 2026-10-10 22:24:01 -04:00
README.md l3-jouleclaw: grade a fallback span as no reading; never read the llama engine whole 2026-10-10 22:24:01 -04:00

L3 binding — JouleClaw model tier → Joule Code cascade

A consumer binding example: how to wire JouleClaw's L3 (Model) tier into the Joule Code cascade. It is a standalone workspace, so the standard's reference library keeps no JouleClaw dependency.

A coding request that misses the cheaper tiers (L0 cache, L1 lawful, L2 embed) is generated by a local model at L3, producing a content-addressed CodingOp and a joules-class Receipt.

Three backends, same slot

All implement JouleClaw's jouleclaw_llm_cheap::LlmBackend, so they drop into the same cascade stage:

  • EchoBackend (from JouleClaw) — deterministic, model-free; used by the unit tests so the wiring is verifiable with no model present.
  • JouleclawRuntimeBackend (here) — runs the model in-process on JouleClaw's own from-scratch GGUF tensor runtime (jouleclaw-runtime): no external binary, no FFI. Tokenize → build the decode-step graph → compile against the registered kernels (jouleclaw-backend-reference / -apple) → autoregressive decode → detokenize, all in Rust the family wrote. This is the default backend for the run binary.
  • LlamaCompletionBackend (here) — drives a real on-device GGUF via llama-completion (llama.cpp). Set JOULE_CODE_L3_ENGINE=llama to use it.

Either local path: no datacenter, no network. In production the cheapest capable backend is chosen by jouleclaw_dispatch::ModelBank's cost table.

Run it against a local model

# Default: in-process on jouleclaw-runtime (the from-scratch tensor engine).
cargo run --release --bin run -- /path/to/model.gguf "Write a Rust function that reverses a string."
# or: JOULE_CODE_L3_MODEL=/path/to/model.gguf cargo run --release --bin run

# Use the llama.cpp shell-out backend instead:
JOULE_CODE_L3_ENGINE=llama cargo run --release --bin run -- /path/to/model.gguf "..."

The run binary generates with the local model, content-addresses the result as a CodingOp (tier L3), and puts the energy of the inference on the receipt, read as close to the work as the host allows and graded by how much of the inference the reading covers:

  • In-process engine (default) — metered on this process's own CPU and GPU (jouleclaw_energy::span::ProcessSpanMeter): the CPU energy the kernel counts for this process's threads, plus its GPU energy — an exact zero where it spent no GPU time, the GPU rail where it had the GPU to itself. A span that covers the whole inference is a hardware reading, HwShunt with measured_uj set. One that covers part of it (the GPU shared with another process, so the CPU alone) is an estimate of the whole, Estimator: the reading plus the host meter's figure over the same window.
  • llama engine — the subprocess is read on its own process tree (sandbox_meter::TreeMeter) where the host counts each process's CPU energy (macOS). That reading is never whole: it is the process's CPU only, not the GPU the binary may offload to, and the process exits in its window, so its record lacks its last time on a core — a lower bound. So it is always an estimate of the whole, the reading plus the host meter's figure, never HwShunt.
  • No reading of the engine's own process — no per-process counter, or, for the in-process engine, this process's counters not live (the span then falls back to a host meter, whose figure is the machine's, not the engine's): the host meter's figure as it is. That is JouleClaw's IOReport counter where its CPU energy rail is live (HwShunt), its load-average estimator where it is not (macOS 27.0.1 for an unprivileged process, Estimator), and the token-count estimate anywhere else (Linux and Windows included; this binding builds jouleclaw-energy with the ioreport feature only).

The receipt has no coverage field, so a partial reading is never sealed as a hardware reading of the whole. The printout names the engine, the host meter and the IOReport status, and says how the figure was obtained.

Verified live: the llama.cpp path against a local 350M GGUF, and the in-process from-scratch runtime against ternary-bonsai-1.7b-q2_0 (qwen3, Q2_0) — real text generated, a real op_id, the cascade closing at L3.

Tests

cargo test   # EchoBackend path — no model required

# In-process from-scratch runtime against a real GGUF (opt-in):
JOULE_BONSAI=/path/to/ternary-bonsai-1.7b-q2_0.gguf \
  cargo test --release --test runtime_l3 -- --ignored --nocapture

The run binary's meter tests check each engine's reading against an oracle sharing no code with the meter — the process's own energy count, read from inside it — and assert it only where this host counts each process's energy (Apple Silicon macOS, the per-process counter live); elsewhere they check the fallback. CI tests this workspace on Linux only, where they take the fallback, so the oracle assertions are not in CI and run only locally: run cargo test -- --nocapture on an Apple Silicon Mac to see them.