Skip to content

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

fix(tokenjuice): correct module stub test structure

Steven Enamakel398e005b2eed
diff --git a/crates/openhuman-core/src/inference/tokenjuice/mod.rs b/crates/openhuman-core/src/inference/tokenjuice/mod.rs
index 28916caf8c..33feccb25f 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/mod.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/mod.rs
@@ -219,9 +219,26 @@ pub async fn compact_tool_output(call: ToolOutputCompaction<'_>) -> CompactedToo
context_token,
scope,
} = call;
- if !enabled || profile == AgentTokenjuiceCompression::Off {
+ // Nothing to ask the module for: the router is off and no summary call is
+ // registered, or this agent bypasses TinyJuice entirely.
+ if (!enabled && context_token.is_none()) || profile == AgentTokenjuiceCompression::Off {
return CompactedToolOutput::unchanged(content);
}
+ #[cfg(test)]
+ if let Ok(stub) = module_stub::STUB.try_with(std::sync::Arc::clone) {
+ let response = stub(types::CompactRequest {
+ content,
+ tool_name: tool_name.to_string(),
+ enabled,
+ profile,
+ arguments,
+ focus,
+ context_token,
+ scope,
+ })
+ .await;
+ return compacted_from(response);
+ }
let config = match runtime_config {
Some(config) => std::sync::Arc::clone(config),
None => match crate::config::Config::load_or_init().await {
@@ -289,6 +306,10 @@ pub async fn compact_tool_output(call: ToolOutputCompaction<'_>) -> CompactedToo
}
};
record_savings(&response);
+ compacted_from(response)
+}
+
+fn compacted_from(response: types::CompactResponse) -> CompactedToolOutput {
let summarized_from_bytes = (response.compressor == CompressorKind::LlmSummary.as_str())
.then_some(response.original_bytes);
CompactedToolOutput {
@@ -381,3 +402,7 @@ pub fn all_tokenjuice_controller_schemas() -> Vec<crate::core::ControllerSchema>
#[cfg(test)]
#[path = "mod_tests.rs"]
mod tests;
+
+#[cfg(test)]
+#[path = "module_stub_tests.rs"]
+pub(crate) mod module_stub;
diff --git a/crates/openhuman-core/src/inference/tokenjuice/module_stub_tests.rs b/crates/openhuman-core/src/inference/tokenjuice/module_stub_tests.rs
new file mode 100644
index 0000000000..f08a55f87f
--- /dev/null
+++ b/crates/openhuman-core/src/inference/tokenjuice/module_stub_tests.rs
@@ -0,0 +1,85 @@
+//! A stand-in for the TinyJuice module in host tests.
+//!
+//! [`compact_tool_output`](super::compact_tool_output) answers from [`STUB`]
+//! instead of the loaded module when a test scopes one, so middleware tests can
+//! drive the summary and compaction paths without a cdylib.
+
+use std::sync::{Arc, Mutex};
+
+use futures::future::BoxFuture;
+
+use super::types::{CompactRequest, CompactResponse, GenerateRequest};
+
+pub(crate) type StubCompactor =
+ Arc<dyn Fn(CompactRequest) -> BoxFuture<'static, CompactResponse> + Send + Sync>;
+
+tokio::task_local! {
+ pub(crate) static STUB: StubCompactor;
+}
+
+/// Run `fut` with `stub` answering every compaction it makes.
+pub(crate) async fn with_stub<F: std::future::Future>(stub: StubCompactor, fut: F) -> F::Output {
+ STUB.scope(stub, fut).await
+}
+
+/// What a stubbed module response looks like when nothing changed.
+pub(crate) fn passthrough(request: &CompactRequest) -> CompactResponse {
+ let bytes = request.content.len();
+ CompactResponse {
+ text: request.content.clone(),
+ original_bytes: bytes,
+ compacted_bytes: bytes,
+ rule_id: "none/plain_text".into(),
+ applied: false,
+ content_kind: "plain_text".into(),
+ compressor: "none".into(),
+ original_tokens: bytes.div_ceil(4) as u64,
+ compacted_tokens: bytes.div_ceil(4) as u64,
+ notice: None,
+ }
+}
+
+/// The notice TinyJuice sends when the summary stage fails.
+pub(crate) const FAILED_NOTICE: &str = "[summarization unavailable — stub]";
+
+/// Emulate the module's summary stage: when the request carries a context
+/// token, call back through `MlHost.Generate` the way the module does and
+/// answer with the summary, a failure notice, or the content unchanged.
+/// Every request is recorded.
+pub(crate) fn summarizing(recorded: Arc<Mutex<Vec<CompactRequest>>>) -> StubCompactor {
+ Arc::new(move |request: CompactRequest| {
+ recorded.lock().unwrap().push(request.clone());
+ Box::pin(async move {
+ let Some(token) = request.context_token.clone() else {
+ return passthrough(&request);
+ };
+ let reply = super::generate::serve(GenerateRequest {
+ context_token: token,
+ purpose: "tool_output_summary".into(),
+ system: "stub system".into(),
+ prompt: format!(
+ "focus={} content={}",
+ request.focus.clone().unwrap_or_default(),
+ request.content
+ ),
+ max_output_tokens: 64,
+ })
+ .await;
+ match reply {
+ Ok(Some(summary)) => CompactResponse {
+ compacted_bytes: summary.len(),
+ text: summary,
+ rule_id: "llm_summary".into(),
+ applied: true,
+ compressor: "llm_summary".into(),
+ ..passthrough(&request)
+ },
+ Ok(None) => passthrough(&request),
+ Err(_) => CompactResponse {
+ notice: Some(FAILED_NOTICE.into()),
+ ..passthrough(&request)
+ },
+ }
+ })
+ })
+}