Skip to content

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

fix(agent): tell the model when a tool result was scrubbed

M3gA-Mind054ade895cb8
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 bdfe964349..ec99eb1cbe 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub.rs
@@ -25,6 +25,59 @@ use tinyinference_llm::tool::ToolCall as TaToolCall;
/// 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 = crate::agent::harness::credentials::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,
+ ))
+}
+
pub(crate) struct CredentialScrubMiddleware;
impl CredentialScrubMiddleware {
@@ -59,18 +112,22 @@ impl ToolMiddleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
};
let content = crate::agent::tinyagents::middleware::tool_result_text(&result);
- let scrubbed_content = crate::agent::harness::credentials::scrub_credentials(&content);
- if scrubbed_content != content {
+ if let Some((annotated, redactions)) = scrub_with_notice(&content) {
tracing::warn!(
tool = %tool_name,
+ redactions,
"[tinyagents::mw] credential_scrub redacted secret(s) from tool result content"
);
- crate::agent::tinyagents::middleware::replace_tool_result_text(
- &mut result,
- scrubbed_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))
}
}
+
+#[cfg(test)]
+#[path = "credential_scrub_tests.rs"]
+mod tests;
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
new file mode 100644
index 0000000000..1d7499c1f5
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/credential_scrub_tests.rs
@@ -0,0 +1,128 @@
+//! #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.
+
+use super::*;
+use crate::agent::harness::credentials::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"
+ );
+}