Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(tokenjuice): move savings persistence into module storage

Steven Enamakel2b8114b8606b
diff --git a/crates/openhuman-core/src/inference/tokenjuice/README.md b/crates/openhuman-core/src/inference/tokenjuice/README.md
index b09ea3e2f1..746b593277 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/README.md
+++ b/crates/openhuman-core/src/inference/tokenjuice/README.md
@@ -23,7 +23,7 @@ OpenHuman-owned files:
| [`repl_tools.rs`](./repl_tools.rs) | The three REPL tools (`juice_find`, `juice_extract`, `juice_summarize`) over a stored result. Declarations come from `tinyjuice-bus`; operations execute over `Query` inside the module CCR store. Registered by [`tools/ops.rs`](../../tools/ops.rs) only while `repl_handle_active(config)`. |
| [`tools.rs`](./tools.rs) | OpenHuman agent tool implementation for the retrieve tool (`tinyjuice_bus::tools::RETRIEVE` (`"juice_retrieve"`); `"tokenjuice_retrieve"` is a recognized recovery-tool alias, not the tool's registered name, see `RECOVERY_TOOL_NAMES`). |
| Plain text | Compressed with TinyJuice's deterministic TextCrusher. |
-| [`savings.rs`](./savings.rs) | Thin adapter: attribution model and `agent::cost` price per input token; the module (`RecordSavings`, `SavingsStats`, `ResetSavings`) does the aggregate math and persists `state/tokenjuice_savings.json`. |
+| [`savings.rs`](./savings.rs) | Thin adapter: attribution model and `agent::cost` price per input token; the module (`RecordSavings`, `SavingsStats`, `ResetSavings`) does the aggregate math and persists the `juice.savings` document in its storage (a legacy `state/tokenjuice_savings.json` is imported once). |
TinyJuice-owned engine pieces:
diff --git a/crates/openhuman-core/src/inference/tokenjuice/mod.rs b/crates/openhuman-core/src/inference/tokenjuice/mod.rs
index 372b8e4eb0..5c75a22f79 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/mod.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/mod.rs
@@ -137,15 +137,19 @@ pub async fn install_from_config(config: &crate::config::Config) -> Result<(), S
return Ok(());
}
savings::configure(
- &client(config),
config
.default_model
.clone()
.unwrap_or_else(|| crate::config::DEFAULT_MODEL.to_string()),
+ );
+ // Totals the old build kept in a snapshot file move into the module's
+ // storage first (once per file, best effort).
+ crate::modules::import_legacy_tinyjuice_savings(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &client(config),
&config.workspace_dir,
)
- .await
- .unwrap_or_else(|e| log::debug!("[tokenjuice][savings] configure failed: {e}"));
+ .await;
let request = install_request(config);
// The module keeps the durable tier on its blob storage; originals the old
// disk tier wrote move across first (once per directory, best effort).
diff --git a/crates/openhuman-core/src/inference/tokenjuice/savings.rs b/crates/openhuman-core/src/inference/tokenjuice/savings.rs
index f4e96f81f6..326ccc0345 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/savings.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/savings.rs
@@ -1,16 +1,12 @@
//! Host adapter for compaction savings accounting.
//!
-//! The aggregate math, the per-model and per-compressor buckets and the
-//! snapshot at `workspace_dir/state/tokenjuice_savings.json` live in the
-//! tinyjuice module (`ConfigureSavings` / `RecordSavings` / `SavingsStats` /
-//! `ResetSavings`). OpenHuman keeps only what is policy: which model the
+//! The aggregate math, the per-model and per-compressor buckets and their
+//! persistence (the `juice.savings` document in the module's storage) live in
+//! the tinyjuice module (`RecordSavings` / `SavingsStats` / `ResetSavings` /
+//! `ImportSavings`). OpenHuman keeps only what is policy: which model the
//! saving is attributed to and the USD price per input token from
//! [`crate::agent::cost`].
//!
-//! The snapshot file is written by the module itself (`ConfigureSavings` takes
-//! its path), not by the host, so it is outside the host's storage-bypass
-//! ratchet. Keeping it on the storage port needs a storage-backed savings
-//! contract in tinyjuice first; the host passes the path until then.
use std::sync::{Mutex, OnceLock};
@@ -26,25 +22,13 @@ fn model_slot() -> &'static Mutex<String> {
SLOT.get_or_init(|| Mutex::new(crate::config::DEFAULT_MODEL.to_string()))
}
-/// Install the attribution model (host policy) and point the module at the
-/// snapshot location; the module loads a prior snapshot once per path.
-pub async fn configure(
- client: &tinybus::module::ModuleClient,
- attribution_model: String,
- workspace_dir: &std::path::Path,
-) -> Result<(), String> {
+/// Install the attribution model (host policy). The module keeps the totals in
+/// its own `juice.savings` document; a legacy snapshot file is handed over by
+/// [`crate::modules::import_legacy_tinyjuice_savings`].
+pub fn configure(attribution_model: String) {
if !attribution_model.trim().is_empty() {
*model_slot().lock().unwrap_or_else(|p| p.into_inner()) = attribution_model;
}
- let path = workspace_dir.join("state").join("tokenjuice_savings.json");
- client
- .call::<()>(
- MODULE,
- methods::CONFIGURE_SAVINGS,
- (path.to_string_lossy().into_owned(),),
- )
- .await
- .map_err(|e| e.to_string())
}
/// The model savings are currently attributed to.
diff --git a/docs/gitbooks/en/features/token-compression.md b/docs/gitbooks/en/features/token-compression.md
index 9ae26e57bf..cb1e1991e7 100644
--- a/docs/gitbooks/en/features/token-compression.md
+++ b/docs/gitbooks/en/features/token-compression.md
@@ -104,7 +104,7 @@ The agent gets the cheap compacted view by default and can zoom in on the full t
## Savings tracking
-An OpenHuman savings callback meters every compression (`crates/openhuman-core/src/inference/tokenjuice/savings.rs`). TokenJuice reports events and token deltas. OpenHuman applies the configured default model's input pricing, aggregates `total`, `by_model` and `by_compressor`, and saves stats to `<workspace>/state/tokenjuice_savings.json`.
+An OpenHuman savings callback meters every compression (`crates/openhuman-core/src/inference/tokenjuice/savings.rs`). TokenJuice reports events and token deltas. OpenHuman applies the configured default model's input pricing, aggregates `total`, `by_model` and `by_compressor`, and the module saves stats in its `juice.savings` storage document (an older `<workspace>/state/tokenjuice_savings.json` is imported once).
Read them over RPC with `openhuman.tokenjuice_savings_stats`; clear them with `openhuman.tokenjuice_savings_reset`.