Skip to content

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

fix(agent): remove credential scrub middleware

Steven Enamakele7797cbf9d6b
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware.rs b/crates/openhuman-core/src/agent/tinyagents/middleware.rs
index 8f2451204d..bd2e2f2ce7 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware.rs
@@ -25,19 +25,14 @@
//! re-exports them so `tinyagents::middleware::*` paths stay stable.
mod approval;
-mod arg_recovery;
-mod artifact_index_toc;
mod cli_rpc_only;
mod cost_budget;
mod credential_scrub;
mod embedder_hooks;
-mod final_call_wrap_up;
mod loop_guards;
mod memory_protocol;
-mod message_trim;
mod packed_tool_route;
mod prompt_cache;
-mod repeat_progress;
mod repeated_failure;
mod research_budget;
mod tool_exposure;
@@ -47,18 +42,13 @@ mod tool_policy;
mod turn_context;
pub(crate) use approval::ApprovalSecurityMiddleware;
-pub(crate) use arg_recovery::ArgRecoveryMiddleware;
-pub(crate) use artifact_index_toc::{split_input_allowance, ArtifactIndexTocMiddleware};
pub(crate) use cli_rpc_only::CliRpcOnlyMiddleware;
pub(crate) use cost_budget::CostBudgetMiddleware;
-pub(crate) use credential_scrub::CredentialScrubMiddleware;
+pub(crate) use credential_scrub::credential_scrub_middleware;
pub(crate) use embedder_hooks::EmbedderToolHooksMiddleware;
-pub(crate) use final_call_wrap_up::FinalCallWrapUpMiddleware;
pub use memory_protocol::MemoryProtocolMiddleware;
-pub(crate) use message_trim::{legacy_max_input_tokens, ImageAwareMessageTrimMiddleware};
pub(crate) use packed_tool_route::PackedToolRouteMiddleware;
pub(crate) use prompt_cache::PromptCacheSegmentMiddleware;
-pub(crate) use repeat_progress::RepeatProgressMiddleware;
pub(crate) use repeated_failure::RepeatedToolFailureMiddleware;
pub(crate) use research_budget::ResearchBudgetMiddleware;
pub(crate) use tool_exposure::OpenHumanToolExposureShadowMiddleware;
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub.rs
index 948a6f9949..fd1a0f8614 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub.rs
@@ -1,82 +1,11 @@
-//! [`CredentialScrubMiddleware`]: scrub credential-shaped secrets out of every
-//! tool result before it leaves the tool boundary.
+//! Host wiring for the harness `CredentialScrubMiddleware`: the scrub itself
+//! lives upstream; the only host policy is the browser task's confirmation
+//! token, which is minted by the host and must survive the scrubber.
-use async_trait::async_trait;
-
-use tinyagents_harness::context::RunContext;
-use tinyagents_harness::error::Result as TaResult;
-use tinyagents_harness::middleware::{MiddlewareToolOutcome, ToolHandler, ToolMiddleware};
-use tinyinference_llm::tool::ToolCall as TaToolCall;
-
-/// `wrap_tool`: scrub credential-shaped secrets out of every tool result before
-/// it leaves the tool boundary (issue #4453). The legacy engine ran
-/// `scrub_credentials` over **every** tool output before it entered model
-/// context (`engine/tools.rs`); the tinyagents path dropped that call site, so
-/// secrets in tool output (env dumps, config reads, API responses, shell output)
-/// reached model context, on-disk `session_raw` transcripts, worker-thread
-/// mirrors, and the tool-outcome capture sink — violating "Never log secrets or
-/// full PII".
-///
-/// Installed as the **innermost** tool wrap (pushed last), so it observes the
-/// RAW tool result first and scrubs it before any outer wrap, the `after_tool`
-/// chain (summarization/caps in [`ToolOutputMiddleware`]), the transcript push,
-/// or the [`ToolOutcomeCaptureMiddleware`] sink can see the unredacted content.
-/// Scrubbing here — rather than inside tool dispatch — covers the
-/// parent chat path, sub-agent paths, the persisted transcript, and
-/// `ToolCallOutcome` records by construction, since every path runs the same
-/// `assemble_turn_harness` seam.
-/// The placeholder `scrub_credentials` emits once per redacted value. Counted
-/// to tell the model *how many* values went, which is the difference between
-/// "something was withheld" and a number it can relay.
-const REDACTION_PLACEHOLDER: &str = "*[REDACTED]";
-
-/// Appended to a scrubbed tool result so the **model** learns what the log
-/// already knew.
-///
-/// Without it the model receives a result that silently differs from what the
-/// tool returned: it cannot find the content it was asked for, re-runs the same
-/// call, gets an identically scrubbed result, and never converges — until the
-/// successful-repeat tracker halts the run and the user is told "Incomplete"
-/// with no reason (#6416). The redaction itself is correct and unchanged; only
-/// its silence was the defect.
-///
-/// The "do not retry" clause is load-bearing: a retry is guaranteed to be
-/// scrubbed identically, so it is the one action that cannot help.
-///
-/// Deliberately contains no `<keyword>: <value>` shape, so it cannot match
-/// `SENSITIVE_KV_REGEX` and scrub itself on a second pass — pinned by
-/// `the_notice_does_not_scrub_itself`.
-fn redaction_notice(count: usize) -> String {
- format!(
- "[credential_scrub] {count} value(s) in this result were redacted as credentials. \
- Re-running this tool returns the same redaction, so do not retry — tell the user \
- which values were withheld and that they can view them directly in the source app."
- )
-}
-
-/// Scrub `content`, returning the replacement text **and** how many values
-/// went — or `None` when nothing was credential-shaped.
-///
-/// Split out of `wrap_tool` so the decision and the composed result are
-/// directly testable. Exercising this is the difference between proving the
-/// notice text is well-formed and proving a scrubbed result actually carries
-/// it; only `replace_tool_result_text` plumbing stays untested.
-fn scrub_with_notice(content: &str) -> Option<(String, usize)> {
- let scrubbed = tinyinference_core::sanitize::scrub_credentials(content);
- if scrubbed == content {
- return None;
- }
- // Count what this pass removed, not what the text already carried — a
- // result may legitimately contain the placeholder already.
- let redactions = scrubbed
- .matches(REDACTION_PLACEHOLDER)
- .count()
- .saturating_sub(content.matches(REDACTION_PLACEHOLDER).count());
- Some((
- format!("{scrubbed}\n\n{}", redaction_notice(redactions)),
- redactions,
- ))
-}
+use tinyagents_harness::middleware::{
+ redaction_notice, scrub_with_notice, CredentialScrubMiddleware, ToolScrubber,
+ REDACTION_PLACEHOLDER,
+};
/// The browser task's `pending.token` is a host-minted, one-time confirmation
/// handle, not a credential from page content. Protect only that field in a
@@ -122,54 +51,10 @@ fn scrub_with_notice_for_tool(tool_name: &str, content: &str) -> Option<(String,
))
}
-pub(crate) struct CredentialScrubMiddleware;
-
-impl CredentialScrubMiddleware {
- pub(crate) fn new() -> Self {
- Self
- }
-}
-
-#[async_trait]
-impl ToolMiddleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
- for CredentialScrubMiddleware
-{
- fn name(&self) -> &str {
- "credential_scrub"
- }
-
- async fn wrap_tool(
- &self,
- ctx: &mut RunContext<crate::agent::tinyagents::host::OpenHumanRunContext>,
- state: &(),
- call: TaToolCall,
- next: ToolHandler<'_, (), crate::agent::tinyagents::host::OpenHumanRunContext>,
- ) -> TaResult<MiddlewareToolOutcome> {
- let tool_name = call.name.clone();
- let outcome = next.run(ctx, state, call).await?;
- // `MiddlewareToolOutcome` is `#[non_exhaustive]`; today it only carries a
- // `Result`, but match rather than irrefutable-let so a future variant
- // fails loud instead of silently bypassing scrubbing.
- let mut result = match outcome {
- MiddlewareToolOutcome::Result(result) => result,
- other => return Ok(other),
- };
-
- let content = crate::agent::tinyagents::middleware::tool_result_text(&result);
- if let Some((annotated, redactions)) = scrub_with_notice_for_tool(&tool_name, &content) {
- tracing::warn!(
- tool = %tool_name,
- redactions,
- "[tinyagents::mw] credential_scrub redacted secret(s) from tool result content"
- );
- // The notice goes to the model, in the result itself. The warning
- // above goes to the log, where no model will ever read it — which
- // was the whole defect (#6416).
- crate::agent::tinyagents::middleware::replace_tool_result_text(&mut result, annotated);
- }
-
- Ok(MiddlewareToolOutcome::Result(result))
- }
+/// The credential scrub wrap the turn harness installs innermost.
+pub(crate) fn credential_scrub_middleware() -> CredentialScrubMiddleware {
+ let scrubber: ToolScrubber = std::sync::Arc::new(scrub_with_notice_for_tool);
+ CredentialScrubMiddleware::with_scrubber(scrubber)
}
#[cfg(test)]
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub_tests.rs
index 0fdb0674b6..b3c3eea611 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub_tests.rs
@@ -1,131 +1,8 @@
-//! #6416: the redaction must reach the model, not only the log.
-//!
-//! The scrubber's behaviour is deliberately NOT relaxed here — a signed
-//! magic-link token in an email is a credential, and letting it through would
-//! re-open #4453 for the commonest credential shape in mail. What changes is
-//! that a scrubbed result now says so, so the agent stops re-fetching content
-//! that will be redacted identically every time.
-//!
-//! The first test is the one that matters: it proves the redaction still
-//! happens. A notice that appeared while the scrubbing silently weakened would
-//! be the worst outcome this change could have.
+//! The browser confirmation-token exemption: the one piece of credential
+//! scrubbing policy the host owns. The scrubber, its notice and their tests
+//! live in `tinyagents-harness` (`middleware::library::credential_scrub`).
use super::*;
-use tinyinference_core::sanitize::scrub_credentials;
-
-/// A magic-link token — exactly the shape QA hit in #6416 — must still be
-/// removed. This is the security half of the contract and comes first.
-#[test]
-fn a_link_token_in_email_content_is_still_redacted() {
- let email = "Click https://acme.example/verify?token=aB3dE5fG7hJ9kL1mN3pQ to sign in.";
- let scrubbed = scrub_credentials(email);
-
- assert!(
- !scrubbed.contains("aB3dE5fG7hJ9kL1mN3pQ"),
- "the token body must not survive scrubbing: {scrubbed}"
- );
- assert!(
- scrubbed.contains(REDACTION_PLACEHOLDER),
- "a redaction must leave its placeholder: {scrubbed}"
- );
-}
-
-/// The notice must not match the scrubber's own patterns, or a second pass
-/// would redact the explanation and the model would be told even less than
-/// before. Verified rather than reasoned about: `credential` is followed by
-/// `]` and a space here, never by `["']?\s*[:=]`.
-#[test]
-fn the_notice_does_not_scrub_itself() {
- let notice = redaction_notice(2);
- assert_eq!(
- scrub_credentials(&notice),
- notice,
- "the notice must survive the scrubber unchanged, or it would redact its own explanation"
- );
-}
-
-/// Scrubbing already-scrubbed content must be a no-op, which is what makes the
-/// middleware idempotent: the `scrubbed != content` guard is false on a second
-/// pass, so no second notice is appended and the count is not restated.
-#[test]
-fn scrubbing_is_idempotent_so_a_second_pass_adds_no_second_notice() {
- let email = "Click https://acme.example/verify?token=aB3dE5fG7hJ9kL1mN3pQ to sign in.";
- let once = scrub_credentials(email);
- let twice = scrub_credentials(&once);
- assert_eq!(twice, once, "scrub must be stable on its own output");
-
- // The value the middleware would hand on: scrubbed body plus the notice.
- let annotated = format!("{once}\n\n{}", redaction_notice(1));
- assert_eq!(
- scrub_credentials(&annotated),
- annotated,
- "a result already carrying the notice must pass through untouched"
- );
-}
-
-/// The count is what the model relays to the user, so it must describe this
-/// pass rather than every placeholder in the text.
-#[test]
-fn the_notice_counts_only_what_this_pass_removed() {
- let two = "api_key=aB3dE5fG7hJ9kL1mN3pQ and token=zX9yW8vU7tS6rQ5pO4nM";
- let scrubbed = scrub_credentials(two);
- let removed = scrubbed.matches(REDACTION_PLACEHOLDER).count()
- - two.matches(REDACTION_PLACEHOLDER).count();
- assert_eq!(removed, 2, "both values were redacted: {scrubbed}");
-
- assert!(redaction_notice(removed).contains("2 value(s)"));
- assert!(
- redaction_notice(removed).contains("do not retry"),
- "the notice must tell the model retrying cannot help — that loop is the defect"
- );
-}
-
-/// Ordinary email content with nothing credential-shaped must be untouched, so
-/// the notice never appears on a result that lost nothing.
-#[test]
-fn unremarkable_content_is_left_alone_and_gets_no_notice() {
- let email = "Lunch at 12:30 tomorrow? The meeting room is booked until 2pm.";
- assert_eq!(
- scrub_credentials(email),
- email,
- "nothing here is credential-shaped; a notice would be a lie"
- );
-}
-
-/// The behaviour this change exists for: a scrubbed result **carries** the
-/// notice. The tests above prove the notice text is well-formed and that the
-/// redaction still happens; this proves the two are actually joined, which is
-/// the part a caller sees.
-#[test]
-fn a_scrubbed_result_carries_the_notice_and_the_count() {
- let email = "Click https://acme.example/verify?token=aB3dE5fG7hJ9kL1mN3pQ to sign in.";
- let (annotated, redactions) =
- scrub_with_notice(email).expect("credential-shaped content must be scrubbed");
-
- assert_eq!(redactions, 1);
- assert!(
- !annotated.contains("aB3dE5fG7hJ9kL1mN3pQ"),
- "the token must not survive: {annotated}"
- );
- assert!(
- annotated.contains("[credential_scrub]"),
- "the model must be told the result was altered: {annotated}"
- );
- assert!(
- annotated.contains("do not retry"),
- "the model must be told retrying cannot help: {annotated}"
- );
-}
-
-/// And a result that lost nothing must be left exactly alone — no notice, and
-/// no rewrite of the tool's own output.
-#[test]
-fn an_unscrubbed_result_is_not_annotated_at_all() {
- assert!(
- scrub_with_notice("Lunch at 12:30 tomorrow? Room booked until 2pm.").is_none(),
- "nothing was redacted, so the result must pass through untouched"
- );
-}
#[test]
fn browser_confirmation_token_survives_without_exempting_page_credentials() {