Skip to content

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

feat(config): migrate legacy component config keys

Steven Enamakel4027f8fb8552
diff --git a/crates/openhuman-core/src/config/schema/load/migrate.rs b/crates/openhuman-core/src/config/schema/load/migrate.rs
index b8cc3afd0a..a8cb802e83 100644
--- a/crates/openhuman-core/src/config/schema/load/migrate.rs
+++ b/crates/openhuman-core/src/config/schema/load/migrate.rs
@@ -334,6 +334,15 @@ pub(crate) fn migrate_components_with(config: &mut Config, raw: &str, tinymemes:
#[cfg(not(feature = "tinymemes"))]
let memes_on = tinymemes.and(None::<bool>);
disable(config, "memes", memes_on);
+ // The old tool-output compaction master switch, `[tokenjuice]
+ // router_enabled = false`, becomes `[components] juice = false`. The
+ // switch stays as a sub-setting of the on state.
+ let router_enabled = value
+ .as_ref()
+ .and_then(|v| v.get("tokenjuice"))
+ .and_then(|t| t.get("router_enabled"))
+ .and_then(toml::Value::as_bool);
+ disable(config, "juice", router_enabled);
if !moved.is_empty() {
tracing::info!(
"[config][migrate] legacy switches moved to [components]: {}",
diff --git a/crates/openhuman-core/src/config/schema/load_components_migration_tests.rs b/crates/openhuman-core/src/config/schema/load_components_migration_tests.rs
index d70b6fed84..c4817cdddb 100644
--- a/crates/openhuman-core/src/config/schema/load_components_migration_tests.rs
+++ b/crates/openhuman-core/src/config/schema/load_components_migration_tests.rs
@@ -96,3 +96,31 @@ fn composio_enabled_is_accepted_and_dropped() {
let v: toml::Value = toml::from_str(&out).unwrap();
assert!(v["composio"].get("enabled").is_none());
}
+
+#[test]
+fn legacy_tokenjuice_router_off_maps_to_components_juice() {
+ let c = migrated("[tokenjuice]\nrouter_enabled = false\n", None);
+ assert!(!c.components.juice);
+ assert!(c.components.search && c.components.mcp && c.components.memory);
+}
+
+#[test]
+fn tokenjuice_router_on_or_absent_keeps_juice_on() {
+ assert!(migrated("[tokenjuice]\nrouter_enabled = true\n", None).components.juice);
+ assert!(migrated("[tokenjuice]\nccr_enabled = false\n", None).components.juice);
+}
+
+#[test]
+fn explicit_components_juice_beats_legacy_router_flag() {
+ let raw = "[components]\njuice = true\n[tokenjuice]\nrouter_enabled = false\n";
+ assert!(migrated(raw, None).components.juice);
+}
+
+#[test]
+fn juice_migration_round_trip_is_idempotent() {
+ let first = migrated("[tokenjuice]\nrouter_enabled = false\n", None);
+ let saved = toml::to_string(&first).unwrap();
+ let second = migrated(&saved, None);
+ assert!(!second.components.juice);
+ assert_eq!(saved, toml::to_string(&second).unwrap());
+}
diff --git a/crates/openhuman-core/src/inference/tokenjuice/README.md b/crates/openhuman-core/src/inference/tokenjuice/README.md
index 5d969764ac..0b6a68f0f5 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/README.md
+++ b/crates/openhuman-core/src/inference/tokenjuice/README.md
@@ -97,5 +97,7 @@ callback is not served, `juice_*` / `retrieve_tool_output` tools are not
registered, the tool-output middleware runs with compaction and the summary
stage off (per-tool cap and byte-budget truncation only), `install_from_config`
is a no-op, and every `tokenjuice.*` RPC answers `COMPONENT_DISABLED`.
-`[tokenjuice] router_enabled` stays a sub-setting of the on state. There is no
-legacy flag for this component, so there is nothing to migrate.
+`[tokenjuice] router_enabled` stays a sub-setting of the on state. A legacy
+config with `[tokenjuice] router_enabled = false` and no `[components] juice`
+key is migrated on load to `[components] juice = false` (an explicit
+`[components]` key always wins).
diff --git a/crates/openhuman-core/src/inference/tokenjuice/savings.rs b/crates/openhuman-core/src/inference/tokenjuice/savings.rs
index 1a07411427..8ad3118f2d 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/savings.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/savings.rs
@@ -67,11 +67,20 @@ fn request(
compressor: CompressorKind,
original_tokens: u64,
compacted_tokens: u64,
+) -> Option<RecordSavingsRequest> {
+ request_for(attribution_model(), compressor, original_tokens, compacted_tokens)
+}
+
+/// [`request`] for an explicit attribution `model`.
+fn request_for(
+ model: String,
+ compressor: CompressorKind,
+ original_tokens: u64,
+ compacted_tokens: u64,
) -> Option<RecordSavingsRequest> {
if original_tokens <= compacted_tokens {
return None;
}
- let model = attribution_model();
Some(RecordSavingsRequest {
input_per_mtok_usd: input_per_mtok_usd(&model),
model,
diff --git a/crates/openhuman-core/src/inference/tokenjuice/savings_tests.rs b/crates/openhuman-core/src/inference/tokenjuice/savings_tests.rs
index 953a9842c7..133f2d67b9 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/savings_tests.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/savings_tests.rs
@@ -24,3 +24,62 @@ fn request_carries_attribution_and_token_counts() {
assert_eq!(req.compressor, CompressorKind::SmartCrusher.as_str());
assert_eq!((req.original_tokens, req.compacted_tokens), (2000, 1000));
}
+
+/// Reference totals computed with the pre-module host ledger's formula
+/// (`tokens_saved * input_per_token` per event, summed). The module now owns
+/// the aggregation (`tinyjuice::savings::SavingsLedger::record`: `saved /
+/// 1e6 * input_per_mtok_usd`); the host only supplies the price, so folding
+/// host-built requests through the module's documented math must reproduce
+/// these totals.
+#[test]
+fn host_pricing_over_module_math_reproduces_the_old_totals() {
+ use std::collections::HashMap;
+
+ // (model, compressor, original, compacted)
+ let events: [(&str, CompressorKind, u64, u64); 5] = [
+ ("claude-sonnet-4-6", CompressorKind::SmartCrusher, 2000, 1000),
+ ("claude-sonnet-4-6", CompressorKind::SmartCrusher, 10000, 2500),
+ ("claude-sonnet-4-6", CompressorKind::TextCrusher, 5000, 5000),
+ ("no-such-model-xyz", CompressorKind::TextCrusher, 4000, 1000),
+ ("claude-sonnet-4-6", CompressorKind::TextCrusher, 800, 900),
+ ];
+
+ // key -> (events, original, compacted, saved, cost)
+ let mut total = (0u64, 0u64, 0u64, 0u64, 0f64);
+ let mut by_model: HashMap<String, (u64, u64, f64)> = HashMap::new();
+ let mut by_compressor: HashMap<String, (u64, u64, f64)> = HashMap::new();
+ for (model, compressor, original, compacted) in events {
+ let Some(req) = request_for(model.to_string(), compressor, original, compacted) else {
+ continue;
+ };
+ // The module's per-event math over the host-supplied price.
+ let saved = req.original_tokens - req.compacted_tokens;
+ let cost = req
+ .input_per_mtok_usd
+ .map_or(0.0, |price| saved as f64 / 1_000_000.0 * price);
+ total.0 += 1;
+ total.1 += req.original_tokens;
+ total.2 += req.compacted_tokens;
+ total.3 += saved;
+ total.4 += cost;
+ let m = by_model.entry(req.model.clone()).or_default();
+ *m = (m.0 + 1, m.1 + saved, m.2 + cost);
+ let c = by_compressor.entry(req.compressor.clone()).or_default();
+ *c = (c.0 + 1, c.1 + saved, c.2 + cost);
+ }
+
+ // Old totals: 3 shrinking events (equal and expanded are ignored).
+ assert_eq!((total.0, total.1, total.2, total.3), (3, 16000, 4500, 11500));
+ // (1000 + 7500) tokens priced at $3 / 1M; the unpriced model adds none.
+ assert!((total.4 - 0.0255).abs() < 1e-12, "cost {}", total.4);
+
+ let sonnet = by_model["claude-sonnet-4-6"];
+ assert_eq!((sonnet.0, sonnet.1), (2, 8500));
+ assert!((sonnet.2 - 0.0255).abs() < 1e-12);
+ assert_eq!(by_model["no-such-model-xyz"], (1, 3000, 0.0));
+
+ let smart = by_compressor[CompressorKind::SmartCrusher.as_str()];
+ assert_eq!((smart.0, smart.1), (2, 8500));
+ assert!((smart.2 - 0.0255).abs() < 1e-12);
+ assert_eq!(by_compressor[CompressorKind::TextCrusher.as_str()], (1, 3000, 0.0));
+}
diff --git a/crates/openhuman-core/src/modules/ops_components_tests.rs b/crates/openhuman-core/src/modules/ops_components_tests.rs
index 4f9b031bd7..becf15aa42 100644
--- a/crates/openhuman-core/src/modules/ops_components_tests.rs
+++ b/crates/openhuman-core/src/modules/ops_components_tests.rs
@@ -75,8 +75,13 @@ async fn juice_off_refuses_tinyjuice_before_download_or_load() {
.await
.unwrap_err();
assert_eq!(err, LoadError::Disabled("juice"));
- assert_eq!(state_of("tinyjuice"), ModuleState::Available);
+ // The resolution slot is process-global, and other tests legitimately
+ // load tinyjuice into it while this one runs, so `state_of("tinyjuice")`
+ // cannot be asserted here. The refusal itself is the proof: a claim on
+ // the slot would have answered with a download or load outcome, never
+ // `Disabled`, and the listing below reports `Disabled` with no source.
let statuses = list(&config);
let status = statuses.iter().find(|s| s.id == "tinyjuice").unwrap();
assert_eq!(status.state, ModuleState::Disabled);
+ assert!(status.source.is_none());
}